blob: e5e15f2c93eca47965667fbddfa08b36de5ac3fd [file] [log] [blame]
Jens Axboe771b53d02019-10-22 10:25:58 -06001#ifndef INTERNAL_IO_WQ_H
2#define INTERNAL_IO_WQ_H
3
4struct io_wq;
5
6enum {
7 IO_WQ_WORK_CANCEL = 1,
8 IO_WQ_WORK_HAS_MM = 2,
9 IO_WQ_WORK_HASHED = 4,
Jens Axboec5def4a2019-11-07 11:41:16 -070010 IO_WQ_WORK_UNBOUND = 32,
Jens Axboeb76da702019-11-20 13:05:32 -070011 IO_WQ_WORK_CB = 128,
Jens Axboe0c9d5cc2019-12-11 19:29:43 -070012 IO_WQ_WORK_NO_CANCEL = 256,
Jens Axboe895e2ca2019-12-17 08:46:33 -070013 IO_WQ_WORK_CONCURRENT = 512,
Jens Axboe771b53d02019-10-22 10:25:58 -060014
15 IO_WQ_HASH_SHIFT = 24, /* upper 8 bits are used for hash key */
16};
17
18enum io_wq_cancel {
19 IO_WQ_CANCEL_OK, /* cancelled before started */
20 IO_WQ_CANCEL_RUNNING, /* found, running, and attempted cancelled */
21 IO_WQ_CANCEL_NOTFOUND, /* work not found */
22};
23
Jens Axboe6206f0e2019-11-26 11:59:32 -070024struct io_wq_work_node {
25 struct io_wq_work_node *next;
26};
27
28struct io_wq_work_list {
29 struct io_wq_work_node *first;
30 struct io_wq_work_node *last;
31};
32
33static inline void wq_list_add_tail(struct io_wq_work_node *node,
34 struct io_wq_work_list *list)
35{
36 if (!list->first) {
Jens Axboee995d512019-12-07 21:06:46 -070037 list->last = node;
38 WRITE_ONCE(list->first, node);
Jens Axboe6206f0e2019-11-26 11:59:32 -070039 } else {
40 list->last->next = node;
41 list->last = node;
42 }
43}
44
45static inline void wq_node_del(struct io_wq_work_list *list,
46 struct io_wq_work_node *node,
47 struct io_wq_work_node *prev)
48{
49 if (node == list->first)
Jens Axboee995d512019-12-07 21:06:46 -070050 WRITE_ONCE(list->first, node->next);
Jens Axboe6206f0e2019-11-26 11:59:32 -070051 if (node == list->last)
52 list->last = prev;
53 if (prev)
54 prev->next = node->next;
Jens Axboe08bdcc32019-12-04 17:19:44 -070055 node->next = NULL;
Jens Axboe6206f0e2019-11-26 11:59:32 -070056}
57
58#define wq_list_for_each(pos, prv, head) \
59 for (pos = (head)->first, prv = NULL; pos; prv = pos, pos = (pos)->next)
60
Jens Axboee995d512019-12-07 21:06:46 -070061#define wq_list_empty(list) (READ_ONCE((list)->first) == NULL)
Jens Axboe6206f0e2019-11-26 11:59:32 -070062#define INIT_WQ_LIST(list) do { \
63 (list)->first = NULL; \
64 (list)->last = NULL; \
65} while (0)
66
Jens Axboe771b53d02019-10-22 10:25:58 -060067struct io_wq_work {
Jens Axboeb76da702019-11-20 13:05:32 -070068 union {
Jens Axboe6206f0e2019-11-26 11:59:32 -070069 struct io_wq_work_node list;
Jens Axboeb76da702019-11-20 13:05:32 -070070 void *data;
71 };
Jens Axboe771b53d02019-10-22 10:25:58 -060072 void (*func)(struct io_wq_work **);
Jens Axboefcb323c2019-10-24 12:39:47 -060073 struct files_struct *files;
Jens Axboecccf0ee2020-01-27 16:34:48 -070074 struct mm_struct *mm;
75 const struct cred *creds;
Jens Axboe9392a272020-02-06 21:42:51 -070076 struct fs_struct *fs;
Jens Axboe6206f0e2019-11-26 11:59:32 -070077 unsigned flags;
Jens Axboe36282882020-02-08 19:16:39 -070078 pid_t task_pid;
Jens Axboe771b53d02019-10-22 10:25:58 -060079};
80
Jens Axboe2d141dd2020-02-25 11:52:56 -070081#define INIT_IO_WORK(work, _func) \
82 do { \
83 *(work) = (struct io_wq_work){ .func = _func }; \
84 } while (0) \
Jens Axboe771b53d02019-10-22 10:25:58 -060085
Jens Axboe7d723062019-11-12 22:31:31 -070086typedef void (get_work_fn)(struct io_wq_work *);
87typedef void (put_work_fn)(struct io_wq_work *);
88
Jens Axboe576a3472019-11-25 08:49:20 -070089struct io_wq_data {
Jens Axboe576a3472019-11-25 08:49:20 -070090 struct user_struct *user;
91
92 get_work_fn *get_work;
93 put_work_fn *put_work;
94};
95
96struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data);
Pavel Begunkoveba6f5a2020-01-28 03:15:47 +030097bool io_wq_get(struct io_wq *wq, struct io_wq_data *data);
Jens Axboe771b53d02019-10-22 10:25:58 -060098void io_wq_destroy(struct io_wq *wq);
99
100void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work);
101void io_wq_enqueue_hashed(struct io_wq *wq, struct io_wq_work *work, void *val);
Jens Axboe771b53d02019-10-22 10:25:58 -0600102
103void io_wq_cancel_all(struct io_wq *wq);
104enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork);
Jens Axboe36282882020-02-08 19:16:39 -0700105enum io_wq_cancel io_wq_cancel_pid(struct io_wq *wq, pid_t pid);
Jens Axboe771b53d02019-10-22 10:25:58 -0600106
Jens Axboe62755e32019-10-28 21:49:21 -0600107typedef bool (work_cancel_fn)(struct io_wq_work *, void *);
108
109enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
110 void *data);
111
Jens Axboe771b53d02019-10-22 10:25:58 -0600112#if defined(CONFIG_IO_WQ)
113extern void io_wq_worker_sleeping(struct task_struct *);
114extern void io_wq_worker_running(struct task_struct *);
115#else
116static inline void io_wq_worker_sleeping(struct task_struct *tsk)
117{
118}
119static inline void io_wq_worker_running(struct task_struct *tsk)
120{
121}
Jens Axboe525b3052019-12-17 14:13:37 -0700122#endif
Jens Axboe771b53d02019-10-22 10:25:58 -0600123
Jens Axboe525b3052019-12-17 14:13:37 -0700124static inline bool io_wq_current_is_worker(void)
125{
126 return in_task() && (current->flags & PF_IO_WORKER);
127}
128#endif