blob: c7c23947cbcd7b64e884073b396efdc10a47a917 [file] [log] [blame]
Jens Axboe771b53d02019-10-22 10:25:58 -06001#ifndef INTERNAL_IO_WQ_H
2#define INTERNAL_IO_WQ_H
3
Jens Axboee9418942021-02-19 12:33:30 -07004#include <linux/refcount.h>
Jens Axboe98447d62020-10-14 10:48:51 -06005
Jens Axboe771b53d02019-10-22 10:25:58 -06006struct io_wq;
7
8enum {
9 IO_WQ_WORK_CANCEL = 1,
Pavel Begunkove883a792020-06-25 18:20:53 +030010 IO_WQ_WORK_HASHED = 2,
11 IO_WQ_WORK_UNBOUND = 4,
Pavel Begunkove883a792020-06-25 18:20:53 +030012 IO_WQ_WORK_CONCURRENT = 16,
Jens Axboe771b53d02019-10-22 10:25:58 -060013
14 IO_WQ_HASH_SHIFT = 24, /* upper 8 bits are used for hash key */
15};
16
17enum io_wq_cancel {
18 IO_WQ_CANCEL_OK, /* cancelled before started */
19 IO_WQ_CANCEL_RUNNING, /* found, running, and attempted cancelled */
20 IO_WQ_CANCEL_NOTFOUND, /* work not found */
21};
22
Stefan Metzmacher53e043b2021-03-15 12:56:56 +010023struct io_wq_work_node {
24 struct io_wq_work_node *next;
25};
26
27struct io_wq_work_list {
28 struct io_wq_work_node *first;
29 struct io_wq_work_node *last;
30};
31
Pavel Begunkov0d9521b2021-09-24 21:59:46 +010032#define wq_list_for_each(pos, prv, head) \
33 for (pos = (head)->first, prv = NULL; pos; prv = pos, pos = (pos)->next)
34
Pavel Begunkov5eef4e82021-09-24 21:59:49 +010035#define wq_list_for_each_resume(pos, prv) \
36 for (; pos; prv = pos, pos = (pos)->next)
37
Pavel Begunkov0d9521b2021-09-24 21:59:46 +010038#define wq_list_empty(list) (READ_ONCE((list)->first) == NULL)
39#define INIT_WQ_LIST(list) do { \
40 (list)->first = NULL; \
Pavel Begunkov0d9521b2021-09-24 21:59:46 +010041} while (0)
42
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030043static inline void wq_list_add_after(struct io_wq_work_node *node,
44 struct io_wq_work_node *pos,
45 struct io_wq_work_list *list)
46{
47 struct io_wq_work_node *next = pos->next;
48
49 pos->next = node;
50 node->next = next;
51 if (!next)
52 list->last = node;
53}
54
Jens Axboe6206f0e2019-11-26 11:59:32 -070055static inline void wq_list_add_tail(struct io_wq_work_node *node,
56 struct io_wq_work_list *list)
57{
Pavel Begunkov8724dd82021-08-09 13:04:07 +010058 node->next = NULL;
Jens Axboe6206f0e2019-11-26 11:59:32 -070059 if (!list->first) {
Jens Axboee995d512019-12-07 21:06:46 -070060 list->last = node;
61 WRITE_ONCE(list->first, node);
Jens Axboe6206f0e2019-11-26 11:59:32 -070062 } else {
63 list->last->next = node;
64 list->last = node;
65 }
66}
67
Pavel Begunkov0d9521b2021-09-24 21:59:46 +010068static inline void wq_list_add_head(struct io_wq_work_node *node,
69 struct io_wq_work_list *list)
70{
71 node->next = list->first;
72 if (!node->next)
73 list->last = node;
74 WRITE_ONCE(list->first, node);
75}
76
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030077static inline void wq_list_cut(struct io_wq_work_list *list,
78 struct io_wq_work_node *last,
79 struct io_wq_work_node *prev)
80{
81 /* first in the list, if prev==NULL */
82 if (!prev)
83 WRITE_ONCE(list->first, last->next);
84 else
85 prev->next = last->next;
86
87 if (last == list->last)
88 list->last = prev;
89 last->next = NULL;
90}
91
Pavel Begunkov0d9521b2021-09-24 21:59:46 +010092static inline void __wq_list_splice(struct io_wq_work_list *list,
93 struct io_wq_work_node *to)
94{
95 list->last->next = to->next;
96 to->next = list->first;
97 INIT_WQ_LIST(list);
98}
99
100static inline bool wq_list_splice(struct io_wq_work_list *list,
101 struct io_wq_work_node *to)
102{
103 if (!wq_list_empty(list)) {
104 __wq_list_splice(list, to);
105 return true;
106 }
107 return false;
108}
109
110static inline void wq_stack_add_head(struct io_wq_work_node *node,
111 struct io_wq_work_node *stack)
112{
113 node->next = stack->next;
114 stack->next = node;
115}
116
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300117static inline void wq_list_del(struct io_wq_work_list *list,
Jens Axboe6206f0e2019-11-26 11:59:32 -0700118 struct io_wq_work_node *node,
119 struct io_wq_work_node *prev)
120{
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300121 wq_list_cut(list, node, prev);
Jens Axboe6206f0e2019-11-26 11:59:32 -0700122}
123
Pavel Begunkov0d9521b2021-09-24 21:59:46 +0100124static inline
125struct io_wq_work_node *wq_stack_extract(struct io_wq_work_node *stack)
126{
127 struct io_wq_work_node *node = stack->next;
Jens Axboe6206f0e2019-11-26 11:59:32 -0700128
Pavel Begunkov0d9521b2021-09-24 21:59:46 +0100129 stack->next = node->next;
130 return node;
131}
Jens Axboe6206f0e2019-11-26 11:59:32 -0700132
Jens Axboe771b53d02019-10-22 10:25:58 -0600133struct io_wq_work {
Pavel Begunkov18a542f2020-03-23 00:23:29 +0300134 struct io_wq_work_node list;
Jens Axboe6206f0e2019-11-26 11:59:32 -0700135 unsigned flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600136};
137
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300138static inline struct io_wq_work *wq_next_work(struct io_wq_work *work)
139{
140 if (!work->list.next)
141 return NULL;
142
143 return container_of(work->list.next, struct io_wq_work, list);
144}
145
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000146typedef struct io_wq_work *(free_work_fn)(struct io_wq_work *);
147typedef void (io_wq_work_fn)(struct io_wq_work *);
Jens Axboe7d723062019-11-12 22:31:31 -0700148
Jens Axboee9418942021-02-19 12:33:30 -0700149struct io_wq_hash {
150 refcount_t refs;
151 unsigned long map;
152 struct wait_queue_head wait;
153};
154
155static inline void io_wq_put_hash(struct io_wq_hash *hash)
156{
157 if (refcount_dec_and_test(&hash->refs))
158 kfree(hash);
159}
160
Jens Axboe576a3472019-11-25 08:49:20 -0700161struct io_wq_data {
Jens Axboee9418942021-02-19 12:33:30 -0700162 struct io_wq_hash *hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -0700163 struct task_struct *task;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300164 io_wq_work_fn *do_work;
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300165 free_work_fn *free_work;
Jens Axboe576a3472019-11-25 08:49:20 -0700166};
167
168struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data);
Pavel Begunkov17a91052021-05-23 15:48:39 +0100169void io_wq_exit_start(struct io_wq *wq);
Jens Axboeafcc4012021-02-26 13:48:19 -0700170void io_wq_put_and_exit(struct io_wq *wq);
Jens Axboe771b53d02019-10-22 10:25:58 -0600171
172void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work);
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300173void io_wq_hash_work(struct io_wq_work *work, void *val);
174
Jens Axboefe764212021-06-17 10:19:54 -0600175int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask);
Jens Axboe2e480052021-08-27 11:33:19 -0600176int io_wq_max_workers(struct io_wq *wq, int *new_count);
Jens Axboefe764212021-06-17 10:19:54 -0600177
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300178static inline bool io_wq_is_hashed(struct io_wq_work *work)
179{
180 return work->flags & IO_WQ_WORK_HASHED;
181}
Jens Axboe771b53d02019-10-22 10:25:58 -0600182
Jens Axboe62755e32019-10-28 21:49:21 -0600183typedef bool (work_cancel_fn)(struct io_wq_work *, void *);
184
185enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300186 void *data, bool cancel_all);
Jens Axboe62755e32019-10-28 21:49:21 -0600187
Jens Axboe771b53d02019-10-22 10:25:58 -0600188#if defined(CONFIG_IO_WQ)
189extern void io_wq_worker_sleeping(struct task_struct *);
190extern void io_wq_worker_running(struct task_struct *);
191#else
192static inline void io_wq_worker_sleeping(struct task_struct *tsk)
193{
194}
195static inline void io_wq_worker_running(struct task_struct *tsk)
196{
197}
Jens Axboe525b3052019-12-17 14:13:37 -0700198#endif
Jens Axboe771b53d02019-10-22 10:25:58 -0600199
Jens Axboe525b3052019-12-17 14:13:37 -0700200static inline bool io_wq_current_is_worker(void)
201{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700202 return in_task() && (current->flags & PF_IO_WORKER) &&
Eric W. Biedermane32cf5d2021-12-22 22:10:09 -0600203 current->worker_private;
Jens Axboe525b3052019-12-17 14:13:37 -0700204}
205#endif