io-wq: add io_wq_work_node based stack
[linux-block.git] / fs / io-wq.h
CommitLineData
771b53d0
JA
1#ifndef INTERNAL_IO_WQ_H
2#define INTERNAL_IO_WQ_H
3
e941894e 4#include <linux/refcount.h>
98447d65 5
771b53d0
JA
6struct io_wq;
7
8enum {
9 IO_WQ_WORK_CANCEL = 1,
e883a79d
PB
10 IO_WQ_WORK_HASHED = 2,
11 IO_WQ_WORK_UNBOUND = 4,
e883a79d 12 IO_WQ_WORK_CONCURRENT = 16,
771b53d0
JA
13
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
53e043b2
SM
23struct 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
0d9521b9
PB
32#define wq_list_for_each(pos, prv, head) \
33 for (pos = (head)->first, prv = NULL; pos; prv = pos, pos = (pos)->next)
34
35#define wq_list_empty(list) (READ_ONCE((list)->first) == NULL)
36#define INIT_WQ_LIST(list) do { \
37 (list)->first = NULL; \
38 (list)->last = NULL; \
39} while (0)
40
86f3cd1b
PB
41static inline void wq_list_add_after(struct io_wq_work_node *node,
42 struct io_wq_work_node *pos,
43 struct io_wq_work_list *list)
44{
45 struct io_wq_work_node *next = pos->next;
46
47 pos->next = node;
48 node->next = next;
49 if (!next)
50 list->last = node;
51}
52
6206f0e1
JA
53static inline void wq_list_add_tail(struct io_wq_work_node *node,
54 struct io_wq_work_list *list)
55{
8724dd8c 56 node->next = NULL;
6206f0e1 57 if (!list->first) {
e995d512
JA
58 list->last = node;
59 WRITE_ONCE(list->first, node);
6206f0e1
JA
60 } else {
61 list->last->next = node;
62 list->last = node;
63 }
64}
65
0d9521b9
PB
66static inline void wq_list_add_head(struct io_wq_work_node *node,
67 struct io_wq_work_list *list)
68{
69 node->next = list->first;
70 if (!node->next)
71 list->last = node;
72 WRITE_ONCE(list->first, node);
73}
74
86f3cd1b
PB
75static inline void wq_list_cut(struct io_wq_work_list *list,
76 struct io_wq_work_node *last,
6206f0e1
JA
77 struct io_wq_work_node *prev)
78{
86f3cd1b
PB
79 /* first in the list, if prev==NULL */
80 if (!prev)
81 WRITE_ONCE(list->first, last->next);
82 else
83 prev->next = last->next;
84
85 if (last == list->last)
6206f0e1 86 list->last = prev;
86f3cd1b
PB
87 last->next = NULL;
88}
89
0d9521b9
PB
90static inline void __wq_list_splice(struct io_wq_work_list *list,
91 struct io_wq_work_node *to)
92{
93 list->last->next = to->next;
94 to->next = list->first;
95 INIT_WQ_LIST(list);
96}
97
98static inline bool wq_list_splice(struct io_wq_work_list *list,
99 struct io_wq_work_node *to)
100{
101 if (!wq_list_empty(list)) {
102 __wq_list_splice(list, to);
103 return true;
104 }
105 return false;
106}
107
108static inline void wq_stack_add_head(struct io_wq_work_node *node,
109 struct io_wq_work_node *stack)
110{
111 node->next = stack->next;
112 stack->next = node;
113}
114
86f3cd1b
PB
115static inline void wq_list_del(struct io_wq_work_list *list,
116 struct io_wq_work_node *node,
117 struct io_wq_work_node *prev)
118{
119 wq_list_cut(list, node, prev);
6206f0e1
JA
120}
121
0d9521b9
PB
122static inline
123struct io_wq_work_node *wq_stack_extract(struct io_wq_work_node *stack)
124{
125 struct io_wq_work_node *node = stack->next;
6206f0e1 126
0d9521b9
PB
127 stack->next = node->next;
128 return node;
129}
6206f0e1 130
771b53d0 131struct io_wq_work {
18a542ff 132 struct io_wq_work_node list;
6206f0e1 133 unsigned flags;
771b53d0
JA
134};
135
86f3cd1b
PB
136static inline struct io_wq_work *wq_next_work(struct io_wq_work *work)
137{
138 if (!work->list.next)
139 return NULL;
140
141 return container_of(work->list.next, struct io_wq_work, list);
142}
143
5280f7e5
PB
144typedef struct io_wq_work *(free_work_fn)(struct io_wq_work *);
145typedef void (io_wq_work_fn)(struct io_wq_work *);
7d723065 146
e941894e
JA
147struct io_wq_hash {
148 refcount_t refs;
149 unsigned long map;
150 struct wait_queue_head wait;
151};
152
153static inline void io_wq_put_hash(struct io_wq_hash *hash)
154{
155 if (refcount_dec_and_test(&hash->refs))
156 kfree(hash);
157}
158
576a347b 159struct io_wq_data {
e941894e 160 struct io_wq_hash *hash;
685fe7fe 161 struct task_struct *task;
f5fa38c5 162 io_wq_work_fn *do_work;
e9fd9396 163 free_work_fn *free_work;
576a347b
JA
164};
165
166struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data);
17a91051 167void io_wq_exit_start(struct io_wq *wq);
afcc4015 168void io_wq_put_and_exit(struct io_wq *wq);
771b53d0
JA
169
170void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work);
8766dd51
PB
171void io_wq_hash_work(struct io_wq_work *work, void *val);
172
fe76421d 173int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask);
2e480058 174int io_wq_max_workers(struct io_wq *wq, int *new_count);
fe76421d 175
8766dd51
PB
176static inline bool io_wq_is_hashed(struct io_wq_work *work)
177{
178 return work->flags & IO_WQ_WORK_HASHED;
179}
771b53d0 180
62755e35
JA
181typedef bool (work_cancel_fn)(struct io_wq_work *, void *);
182
183enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
4f26bda1 184 void *data, bool cancel_all);
62755e35 185
771b53d0
JA
186#if defined(CONFIG_IO_WQ)
187extern void io_wq_worker_sleeping(struct task_struct *);
188extern void io_wq_worker_running(struct task_struct *);
189#else
190static inline void io_wq_worker_sleeping(struct task_struct *tsk)
191{
192}
193static inline void io_wq_worker_running(struct task_struct *tsk)
194{
195}
525b305d 196#endif
771b53d0 197
525b305d
JA
198static inline bool io_wq_current_is_worker(void)
199{
3bfe6106
JA
200 return in_task() && (current->flags & PF_IO_WORKER) &&
201 current->pf_io_worker;
525b305d
JA
202}
203#endif