1 #ifndef INTERNAL_IO_WQ_H
2 #define INTERNAL_IO_WQ_H
4 #include <linux/refcount.h>
10 IO_WQ_WORK_HASHED = 2,
11 IO_WQ_WORK_UNBOUND = 4,
12 IO_WQ_WORK_CONCURRENT = 16,
14 IO_WQ_HASH_SHIFT = 24, /* upper 8 bits are used for hash key */
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 */
23 struct io_wq_work_node {
24 struct io_wq_work_node *next;
27 struct io_wq_work_list {
28 struct io_wq_work_node *first;
29 struct io_wq_work_node *last;
32 #define wq_list_for_each(pos, prv, head) \
33 for (pos = (head)->first, prv = NULL; pos; prv = pos, pos = (pos)->next)
35 #define wq_list_for_each_resume(pos, prv) \
36 for (; pos; prv = pos, pos = (pos)->next)
38 #define wq_list_empty(list) (READ_ONCE((list)->first) == NULL)
39 #define INIT_WQ_LIST(list) do { \
40 (list)->first = NULL; \
43 static 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)
47 struct io_wq_work_node *next = pos->next;
55 static inline void wq_list_add_tail(struct io_wq_work_node *node,
56 struct io_wq_work_list *list)
61 WRITE_ONCE(list->first, node);
63 list->last->next = node;
68 static inline void wq_list_add_head(struct io_wq_work_node *node,
69 struct io_wq_work_list *list)
71 node->next = list->first;
74 WRITE_ONCE(list->first, node);
77 static 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)
81 /* first in the list, if prev==NULL */
83 WRITE_ONCE(list->first, last->next);
85 prev->next = last->next;
87 if (last == list->last)
92 static inline void __wq_list_splice(struct io_wq_work_list *list,
93 struct io_wq_work_node *to)
95 list->last->next = to->next;
96 to->next = list->first;
100 static inline bool wq_list_splice(struct io_wq_work_list *list,
101 struct io_wq_work_node *to)
103 if (!wq_list_empty(list)) {
104 __wq_list_splice(list, to);
110 static inline void wq_stack_add_head(struct io_wq_work_node *node,
111 struct io_wq_work_node *stack)
113 node->next = stack->next;
117 static inline void wq_list_del(struct io_wq_work_list *list,
118 struct io_wq_work_node *node,
119 struct io_wq_work_node *prev)
121 wq_list_cut(list, node, prev);
125 struct io_wq_work_node *wq_stack_extract(struct io_wq_work_node *stack)
127 struct io_wq_work_node *node = stack->next;
129 stack->next = node->next;
134 struct io_wq_work_node list;
138 static inline struct io_wq_work *wq_next_work(struct io_wq_work *work)
140 if (!work->list.next)
143 return container_of(work->list.next, struct io_wq_work, list);
146 typedef struct io_wq_work *(free_work_fn)(struct io_wq_work *);
147 typedef void (io_wq_work_fn)(struct io_wq_work *);
152 struct wait_queue_head wait;
155 static inline void io_wq_put_hash(struct io_wq_hash *hash)
157 if (refcount_dec_and_test(&hash->refs))
162 struct io_wq_hash *hash;
163 struct task_struct *task;
164 io_wq_work_fn *do_work;
165 free_work_fn *free_work;
168 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data);
169 void io_wq_exit_start(struct io_wq *wq);
170 void io_wq_put_and_exit(struct io_wq *wq);
172 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work);
173 void io_wq_hash_work(struct io_wq_work *work, void *val);
175 int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask);
176 int io_wq_max_workers(struct io_wq *wq, int *new_count);
178 static inline bool io_wq_is_hashed(struct io_wq_work *work)
180 return work->flags & IO_WQ_WORK_HASHED;
183 typedef bool (work_cancel_fn)(struct io_wq_work *, void *);
185 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
186 void *data, bool cancel_all);
188 #if defined(CONFIG_IO_WQ)
189 extern void io_wq_worker_sleeping(struct task_struct *);
190 extern void io_wq_worker_running(struct task_struct *);
192 static inline void io_wq_worker_sleeping(struct task_struct *tsk)
195 static inline void io_wq_worker_running(struct task_struct *tsk)
200 static inline bool io_wq_current_is_worker(void)
202 return in_task() && (current->flags & PF_IO_WORKER) &&
203 current->pf_io_worker;