blob: 071f1a9978002675c48294ad518a725c52c0bd05 [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,
Jens Axboe771b53d02019-10-22 10:25:58 -06008 IO_WQ_WORK_HASHED = 4,
Jens Axboec5def4a2019-11-07 11:41:16 -07009 IO_WQ_WORK_UNBOUND = 32,
Jens Axboe0c9d5cc2019-12-11 19:29:43 -070010 IO_WQ_WORK_NO_CANCEL = 256,
Jens Axboe895e2ca2019-12-17 08:46:33 -070011 IO_WQ_WORK_CONCURRENT = 512,
Jens Axboe771b53d02019-10-22 10:25:58 -060012
13 IO_WQ_HASH_SHIFT = 24, /* upper 8 bits are used for hash key */
14};
15
16enum io_wq_cancel {
17 IO_WQ_CANCEL_OK, /* cancelled before started */
18 IO_WQ_CANCEL_RUNNING, /* found, running, and attempted cancelled */
19 IO_WQ_CANCEL_NOTFOUND, /* work not found */
20};
21
Jens Axboe6206f0e2019-11-26 11:59:32 -070022struct io_wq_work_node {
23 struct io_wq_work_node *next;
24};
25
26struct io_wq_work_list {
27 struct io_wq_work_node *first;
28 struct io_wq_work_node *last;
29};
30
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030031static inline void wq_list_add_after(struct io_wq_work_node *node,
32 struct io_wq_work_node *pos,
33 struct io_wq_work_list *list)
34{
35 struct io_wq_work_node *next = pos->next;
36
37 pos->next = node;
38 node->next = next;
39 if (!next)
40 list->last = node;
41}
42
Jens Axboe6206f0e2019-11-26 11:59:32 -070043static inline void wq_list_add_tail(struct io_wq_work_node *node,
44 struct io_wq_work_list *list)
45{
46 if (!list->first) {
Jens Axboee995d512019-12-07 21:06:46 -070047 list->last = node;
48 WRITE_ONCE(list->first, node);
Jens Axboe6206f0e2019-11-26 11:59:32 -070049 } else {
50 list->last->next = node;
51 list->last = node;
52 }
53}
54
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030055static inline void wq_list_cut(struct io_wq_work_list *list,
56 struct io_wq_work_node *last,
57 struct io_wq_work_node *prev)
58{
59 /* first in the list, if prev==NULL */
60 if (!prev)
61 WRITE_ONCE(list->first, last->next);
62 else
63 prev->next = last->next;
64
65 if (last == list->last)
66 list->last = prev;
67 last->next = NULL;
68}
69
70static inline void wq_list_del(struct io_wq_work_list *list,
Jens Axboe6206f0e2019-11-26 11:59:32 -070071 struct io_wq_work_node *node,
72 struct io_wq_work_node *prev)
73{
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030074 wq_list_cut(list, node, prev);
Jens Axboe6206f0e2019-11-26 11:59:32 -070075}
76
77#define wq_list_for_each(pos, prv, head) \
78 for (pos = (head)->first, prv = NULL; pos; prv = pos, pos = (pos)->next)
79
Jens Axboee995d512019-12-07 21:06:46 -070080#define wq_list_empty(list) (READ_ONCE((list)->first) == NULL)
Jens Axboe6206f0e2019-11-26 11:59:32 -070081#define INIT_WQ_LIST(list) do { \
82 (list)->first = NULL; \
83 (list)->last = NULL; \
84} while (0)
85
Jens Axboe771b53d02019-10-22 10:25:58 -060086struct io_wq_work {
Pavel Begunkov18a542f2020-03-23 00:23:29 +030087 struct io_wq_work_node list;
Jens Axboefcb323c2019-10-24 12:39:47 -060088 struct files_struct *files;
Jens Axboecccf0ee2020-01-27 16:34:48 -070089 struct mm_struct *mm;
90 const struct cred *creds;
Jens Axboe9392a272020-02-06 21:42:51 -070091 struct fs_struct *fs;
Jens Axboe6206f0e2019-11-26 11:59:32 -070092 unsigned flags;
Jens Axboe771b53d02019-10-22 10:25:58 -060093};
94
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030095static inline struct io_wq_work *wq_next_work(struct io_wq_work *work)
96{
97 if (!work->list.next)
98 return NULL;
99
100 return container_of(work->list.next, struct io_wq_work, list);
101}
102
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300103typedef void (free_work_fn)(struct io_wq_work *);
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300104typedef void (io_wq_work_fn)(struct io_wq_work **);
Jens Axboe7d723062019-11-12 22:31:31 -0700105
Jens Axboe576a3472019-11-25 08:49:20 -0700106struct io_wq_data {
Jens Axboe576a3472019-11-25 08:49:20 -0700107 struct user_struct *user;
108
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300109 io_wq_work_fn *do_work;
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300110 free_work_fn *free_work;
Jens Axboe576a3472019-11-25 08:49:20 -0700111};
112
113struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data);
Pavel Begunkoveba6f5a2020-01-28 03:15:47 +0300114bool io_wq_get(struct io_wq *wq, struct io_wq_data *data);
Jens Axboe771b53d02019-10-22 10:25:58 -0600115void io_wq_destroy(struct io_wq *wq);
116
117void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work);
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300118void io_wq_hash_work(struct io_wq_work *work, void *val);
119
120static inline bool io_wq_is_hashed(struct io_wq_work *work)
121{
122 return work->flags & IO_WQ_WORK_HASHED;
123}
Jens Axboe771b53d02019-10-22 10:25:58 -0600124
125void io_wq_cancel_all(struct io_wq *wq);
126enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork);
127
Jens Axboe62755e32019-10-28 21:49:21 -0600128typedef bool (work_cancel_fn)(struct io_wq_work *, void *);
129
130enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300131 void *data, bool cancel_all);
Jens Axboe62755e32019-10-28 21:49:21 -0600132
Jens Axboeaa96bf82020-04-03 11:26:26 -0600133struct task_struct *io_wq_get_task(struct io_wq *wq);
134
Jens Axboe771b53d02019-10-22 10:25:58 -0600135#if defined(CONFIG_IO_WQ)
136extern void io_wq_worker_sleeping(struct task_struct *);
137extern void io_wq_worker_running(struct task_struct *);
138#else
139static inline void io_wq_worker_sleeping(struct task_struct *tsk)
140{
141}
142static inline void io_wq_worker_running(struct task_struct *tsk)
143{
144}
Jens Axboe525b3052019-12-17 14:13:37 -0700145#endif
Jens Axboe771b53d02019-10-22 10:25:58 -0600146
Jens Axboe525b3052019-12-17 14:13:37 -0700147static inline bool io_wq_current_is_worker(void)
148{
149 return in_task() && (current->flags & PF_IO_WORKER);
150}
151#endif