]>
Commit | Line | Data |
---|---|---|
771b53d0 JA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Basic worker thread pool for io_uring | |
4 | * | |
5 | * Copyright (C) 2019 Jens Axboe | |
6 | * | |
7 | */ | |
8 | #include <linux/kernel.h> | |
9 | #include <linux/init.h> | |
10 | #include <linux/errno.h> | |
11 | #include <linux/sched/signal.h> | |
12 | #include <linux/mm.h> | |
771b53d0 JA |
13 | #include <linux/sched/mm.h> |
14 | #include <linux/percpu.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/kthread.h> | |
17 | #include <linux/rculist_nulls.h> | |
9392a27d | 18 | #include <linux/fs_struct.h> |
91d8f519 | 19 | #include <linux/blk-cgroup.h> |
4ea33a97 | 20 | #include <linux/audit.h> |
43c01fbe | 21 | #include <linux/cpu.h> |
771b53d0 | 22 | |
43c01fbe | 23 | #include "../kernel/sched/sched.h" |
771b53d0 JA |
24 | #include "io-wq.h" |
25 | ||
26 | #define WORKER_IDLE_TIMEOUT (5 * HZ) | |
27 | ||
28 | enum { | |
29 | IO_WORKER_F_UP = 1, /* up and active */ | |
30 | IO_WORKER_F_RUNNING = 2, /* account as running */ | |
31 | IO_WORKER_F_FREE = 4, /* worker on free list */ | |
145cc8c6 JA |
32 | IO_WORKER_F_FIXED = 8, /* static idle worker */ |
33 | IO_WORKER_F_BOUND = 16, /* is doing bounded work */ | |
771b53d0 JA |
34 | }; |
35 | ||
36 | enum { | |
37 | IO_WQ_BIT_EXIT = 0, /* wq exiting */ | |
446bc1c2 | 38 | IO_WQ_BIT_ERROR = 1, /* error on setup */ |
771b53d0 JA |
39 | }; |
40 | ||
41 | enum { | |
42 | IO_WQE_FLAG_STALLED = 1, /* stalled on hash */ | |
43 | }; | |
44 | ||
45 | /* | |
46 | * One for each thread in a wqe pool | |
47 | */ | |
48 | struct io_worker { | |
49 | refcount_t ref; | |
50 | unsigned flags; | |
51 | struct hlist_nulls_node nulls_node; | |
e61df66c | 52 | struct list_head all_list; |
771b53d0 | 53 | struct task_struct *task; |
771b53d0 | 54 | struct io_wqe *wqe; |
36c2f922 | 55 | |
771b53d0 | 56 | struct io_wq_work *cur_work; |
36c2f922 | 57 | spinlock_t lock; |
771b53d0 JA |
58 | |
59 | struct rcu_head rcu; | |
60 | struct mm_struct *mm; | |
91d8f519 DZ |
61 | #ifdef CONFIG_BLK_CGROUP |
62 | struct cgroup_subsys_state *blkcg_css; | |
63 | #endif | |
cccf0ee8 JA |
64 | const struct cred *cur_creds; |
65 | const struct cred *saved_creds; | |
9b828492 | 66 | struct nsproxy *restore_nsproxy; |
771b53d0 JA |
67 | }; |
68 | ||
771b53d0 JA |
69 | #if BITS_PER_LONG == 64 |
70 | #define IO_WQ_HASH_ORDER 6 | |
71 | #else | |
72 | #define IO_WQ_HASH_ORDER 5 | |
73 | #endif | |
74 | ||
86f3cd1b PB |
75 | #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER) |
76 | ||
c5def4ab JA |
77 | struct io_wqe_acct { |
78 | unsigned nr_workers; | |
79 | unsigned max_workers; | |
80 | atomic_t nr_running; | |
81 | }; | |
82 | ||
83 | enum { | |
84 | IO_WQ_ACCT_BOUND, | |
85 | IO_WQ_ACCT_UNBOUND, | |
86 | }; | |
87 | ||
771b53d0 JA |
88 | /* |
89 | * Per-node worker thread pool | |
90 | */ | |
91 | struct io_wqe { | |
92 | struct { | |
95da8465 | 93 | raw_spinlock_t lock; |
6206f0e1 | 94 | struct io_wq_work_list work_list; |
771b53d0 JA |
95 | unsigned long hash_map; |
96 | unsigned flags; | |
97 | } ____cacheline_aligned_in_smp; | |
98 | ||
99 | int node; | |
c5def4ab | 100 | struct io_wqe_acct acct[2]; |
771b53d0 | 101 | |
021d1cdd | 102 | struct hlist_nulls_head free_list; |
e61df66c | 103 | struct list_head all_list; |
771b53d0 JA |
104 | |
105 | struct io_wq *wq; | |
86f3cd1b | 106 | struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS]; |
771b53d0 JA |
107 | }; |
108 | ||
109 | /* | |
110 | * Per io_wq state | |
111 | */ | |
112 | struct io_wq { | |
113 | struct io_wqe **wqes; | |
114 | unsigned long state; | |
771b53d0 | 115 | |
e9fd9396 | 116 | free_work_fn *free_work; |
f5fa38c5 | 117 | io_wq_work_fn *do_work; |
7d723065 | 118 | |
771b53d0 | 119 | struct task_struct *manager; |
c5def4ab | 120 | struct user_struct *user; |
771b53d0 JA |
121 | refcount_t refs; |
122 | struct completion done; | |
848f7e18 | 123 | |
43c01fbe | 124 | struct hlist_node cpuhp_node; |
771b53d0 JA |
125 | }; |
126 | ||
43c01fbe JA |
127 | static enum cpuhp_state io_wq_online; |
128 | ||
771b53d0 JA |
129 | static bool io_worker_get(struct io_worker *worker) |
130 | { | |
131 | return refcount_inc_not_zero(&worker->ref); | |
132 | } | |
133 | ||
134 | static void io_worker_release(struct io_worker *worker) | |
135 | { | |
136 | if (refcount_dec_and_test(&worker->ref)) | |
137 | wake_up_process(worker->task); | |
138 | } | |
139 | ||
140 | /* | |
141 | * Note: drops the wqe->lock if returning true! The caller must re-acquire | |
142 | * the lock in that case. Some callers need to restart handling if this | |
143 | * happens, so we can't just re-acquire the lock on behalf of the caller. | |
144 | */ | |
145 | static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker) | |
146 | { | |
fcb323cc JA |
147 | bool dropped_lock = false; |
148 | ||
cccf0ee8 JA |
149 | if (worker->saved_creds) { |
150 | revert_creds(worker->saved_creds); | |
151 | worker->cur_creds = worker->saved_creds = NULL; | |
181e448d JA |
152 | } |
153 | ||
e06aa2e9 | 154 | if (current->files) { |
fcb323cc | 155 | __acquire(&wqe->lock); |
95da8465 | 156 | raw_spin_unlock_irq(&wqe->lock); |
fcb323cc JA |
157 | dropped_lock = true; |
158 | ||
159 | task_lock(current); | |
e06aa2e9 | 160 | current->files = NULL; |
9b828492 | 161 | current->nsproxy = worker->restore_nsproxy; |
fcb323cc JA |
162 | task_unlock(current); |
163 | } | |
164 | ||
e06aa2e9 JA |
165 | if (current->fs) |
166 | current->fs = NULL; | |
9392a27d | 167 | |
771b53d0 JA |
168 | /* |
169 | * If we have an active mm, we need to drop the wq lock before unusing | |
170 | * it. If we do, return true and let the caller retry the idle loop. | |
171 | */ | |
172 | if (worker->mm) { | |
fcb323cc JA |
173 | if (!dropped_lock) { |
174 | __acquire(&wqe->lock); | |
95da8465 | 175 | raw_spin_unlock_irq(&wqe->lock); |
fcb323cc JA |
176 | dropped_lock = true; |
177 | } | |
771b53d0 | 178 | __set_current_state(TASK_RUNNING); |
f5678e7f | 179 | kthread_unuse_mm(worker->mm); |
771b53d0 JA |
180 | mmput(worker->mm); |
181 | worker->mm = NULL; | |
771b53d0 JA |
182 | } |
183 | ||
91d8f519 DZ |
184 | #ifdef CONFIG_BLK_CGROUP |
185 | if (worker->blkcg_css) { | |
186 | kthread_associate_blkcg(NULL); | |
187 | worker->blkcg_css = NULL; | |
188 | } | |
189 | #endif | |
69228338 JA |
190 | if (current->signal->rlim[RLIMIT_FSIZE].rlim_cur != RLIM_INFINITY) |
191 | current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; | |
fcb323cc | 192 | return dropped_lock; |
771b53d0 JA |
193 | } |
194 | ||
c5def4ab JA |
195 | static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe, |
196 | struct io_wq_work *work) | |
197 | { | |
198 | if (work->flags & IO_WQ_WORK_UNBOUND) | |
199 | return &wqe->acct[IO_WQ_ACCT_UNBOUND]; | |
200 | ||
201 | return &wqe->acct[IO_WQ_ACCT_BOUND]; | |
202 | } | |
203 | ||
204 | static inline struct io_wqe_acct *io_wqe_get_acct(struct io_wqe *wqe, | |
205 | struct io_worker *worker) | |
206 | { | |
207 | if (worker->flags & IO_WORKER_F_BOUND) | |
208 | return &wqe->acct[IO_WQ_ACCT_BOUND]; | |
209 | ||
210 | return &wqe->acct[IO_WQ_ACCT_UNBOUND]; | |
211 | } | |
212 | ||
771b53d0 JA |
213 | static void io_worker_exit(struct io_worker *worker) |
214 | { | |
215 | struct io_wqe *wqe = worker->wqe; | |
c5def4ab | 216 | struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker); |
771b53d0 JA |
217 | |
218 | /* | |
219 | * If we're not at zero, someone else is holding a brief reference | |
220 | * to the worker. Wait for that to go away. | |
221 | */ | |
222 | set_current_state(TASK_INTERRUPTIBLE); | |
223 | if (!refcount_dec_and_test(&worker->ref)) | |
224 | schedule(); | |
225 | __set_current_state(TASK_RUNNING); | |
226 | ||
227 | preempt_disable(); | |
228 | current->flags &= ~PF_IO_WORKER; | |
229 | if (worker->flags & IO_WORKER_F_RUNNING) | |
c5def4ab JA |
230 | atomic_dec(&acct->nr_running); |
231 | if (!(worker->flags & IO_WORKER_F_BOUND)) | |
232 | atomic_dec(&wqe->wq->user->processes); | |
771b53d0 JA |
233 | worker->flags = 0; |
234 | preempt_enable(); | |
235 | ||
95da8465 | 236 | raw_spin_lock_irq(&wqe->lock); |
771b53d0 | 237 | hlist_nulls_del_rcu(&worker->nulls_node); |
e61df66c | 238 | list_del_rcu(&worker->all_list); |
771b53d0 JA |
239 | if (__io_worker_unuse(wqe, worker)) { |
240 | __release(&wqe->lock); | |
95da8465 | 241 | raw_spin_lock_irq(&wqe->lock); |
771b53d0 | 242 | } |
c5def4ab | 243 | acct->nr_workers--; |
95da8465 | 244 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 | 245 | |
364b05fd | 246 | kfree_rcu(worker, rcu); |
c4068bf8 HD |
247 | if (refcount_dec_and_test(&wqe->wq->refs)) |
248 | complete(&wqe->wq->done); | |
771b53d0 JA |
249 | } |
250 | ||
c5def4ab JA |
251 | static inline bool io_wqe_run_queue(struct io_wqe *wqe) |
252 | __must_hold(wqe->lock) | |
253 | { | |
6206f0e1 JA |
254 | if (!wq_list_empty(&wqe->work_list) && |
255 | !(wqe->flags & IO_WQE_FLAG_STALLED)) | |
c5def4ab JA |
256 | return true; |
257 | return false; | |
258 | } | |
259 | ||
260 | /* | |
261 | * Check head of free list for an available worker. If one isn't available, | |
262 | * caller must wake up the wq manager to create one. | |
263 | */ | |
264 | static bool io_wqe_activate_free_worker(struct io_wqe *wqe) | |
265 | __must_hold(RCU) | |
266 | { | |
267 | struct hlist_nulls_node *n; | |
268 | struct io_worker *worker; | |
269 | ||
021d1cdd | 270 | n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list)); |
c5def4ab JA |
271 | if (is_a_nulls(n)) |
272 | return false; | |
273 | ||
274 | worker = hlist_nulls_entry(n, struct io_worker, nulls_node); | |
275 | if (io_worker_get(worker)) { | |
506d95ff | 276 | wake_up_process(worker->task); |
c5def4ab JA |
277 | io_worker_release(worker); |
278 | return true; | |
279 | } | |
280 | ||
281 | return false; | |
282 | } | |
283 | ||
284 | /* | |
285 | * We need a worker. If we find a free one, we're good. If not, and we're | |
286 | * below the max number of workers, wake up the manager to create one. | |
287 | */ | |
288 | static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct) | |
289 | { | |
290 | bool ret; | |
291 | ||
292 | /* | |
293 | * Most likely an attempt to queue unbounded work on an io_wq that | |
294 | * wasn't setup with any unbounded workers. | |
295 | */ | |
296 | WARN_ON_ONCE(!acct->max_workers); | |
297 | ||
298 | rcu_read_lock(); | |
299 | ret = io_wqe_activate_free_worker(wqe); | |
300 | rcu_read_unlock(); | |
301 | ||
302 | if (!ret && acct->nr_workers < acct->max_workers) | |
303 | wake_up_process(wqe->wq->manager); | |
304 | } | |
305 | ||
306 | static void io_wqe_inc_running(struct io_wqe *wqe, struct io_worker *worker) | |
307 | { | |
308 | struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker); | |
309 | ||
310 | atomic_inc(&acct->nr_running); | |
311 | } | |
312 | ||
313 | static void io_wqe_dec_running(struct io_wqe *wqe, struct io_worker *worker) | |
314 | __must_hold(wqe->lock) | |
315 | { | |
316 | struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker); | |
317 | ||
318 | if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe)) | |
319 | io_wqe_wake_worker(wqe, acct); | |
320 | } | |
321 | ||
771b53d0 JA |
322 | static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker) |
323 | { | |
324 | allow_kernel_signal(SIGINT); | |
325 | ||
326 | current->flags |= PF_IO_WORKER; | |
e06aa2e9 JA |
327 | current->fs = NULL; |
328 | current->files = NULL; | |
771b53d0 JA |
329 | |
330 | worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING); | |
9b828492 | 331 | worker->restore_nsproxy = current->nsproxy; |
c5def4ab | 332 | io_wqe_inc_running(wqe, worker); |
771b53d0 JA |
333 | } |
334 | ||
335 | /* | |
336 | * Worker will start processing some work. Move it to the busy list, if | |
337 | * it's currently on the freelist | |
338 | */ | |
339 | static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker, | |
340 | struct io_wq_work *work) | |
341 | __must_hold(wqe->lock) | |
342 | { | |
c5def4ab JA |
343 | bool worker_bound, work_bound; |
344 | ||
771b53d0 JA |
345 | if (worker->flags & IO_WORKER_F_FREE) { |
346 | worker->flags &= ~IO_WORKER_F_FREE; | |
347 | hlist_nulls_del_init_rcu(&worker->nulls_node); | |
771b53d0 | 348 | } |
c5def4ab JA |
349 | |
350 | /* | |
351 | * If worker is moving from bound to unbound (or vice versa), then | |
352 | * ensure we update the running accounting. | |
353 | */ | |
b2e9c7d6 DC |
354 | worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0; |
355 | work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0; | |
356 | if (worker_bound != work_bound) { | |
c5def4ab JA |
357 | io_wqe_dec_running(wqe, worker); |
358 | if (work_bound) { | |
359 | worker->flags |= IO_WORKER_F_BOUND; | |
360 | wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--; | |
361 | wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++; | |
362 | atomic_dec(&wqe->wq->user->processes); | |
363 | } else { | |
364 | worker->flags &= ~IO_WORKER_F_BOUND; | |
365 | wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++; | |
366 | wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--; | |
367 | atomic_inc(&wqe->wq->user->processes); | |
368 | } | |
369 | io_wqe_inc_running(wqe, worker); | |
370 | } | |
771b53d0 JA |
371 | } |
372 | ||
373 | /* | |
374 | * No work, worker going to sleep. Move to freelist, and unuse mm if we | |
375 | * have one attached. Dropping the mm may potentially sleep, so we drop | |
376 | * the lock in that case and return success. Since the caller has to | |
377 | * retry the loop in that case (we changed task state), we don't regrab | |
378 | * the lock if we return success. | |
379 | */ | |
380 | static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker) | |
381 | __must_hold(wqe->lock) | |
382 | { | |
383 | if (!(worker->flags & IO_WORKER_F_FREE)) { | |
384 | worker->flags |= IO_WORKER_F_FREE; | |
021d1cdd | 385 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list); |
771b53d0 JA |
386 | } |
387 | ||
388 | return __io_worker_unuse(wqe, worker); | |
389 | } | |
390 | ||
60cf46ae PB |
391 | static inline unsigned int io_get_work_hash(struct io_wq_work *work) |
392 | { | |
393 | return work->flags >> IO_WQ_HASH_SHIFT; | |
394 | } | |
395 | ||
396 | static struct io_wq_work *io_get_next_work(struct io_wqe *wqe) | |
771b53d0 JA |
397 | __must_hold(wqe->lock) |
398 | { | |
6206f0e1 | 399 | struct io_wq_work_node *node, *prev; |
86f3cd1b | 400 | struct io_wq_work *work, *tail; |
60cf46ae | 401 | unsigned int hash; |
771b53d0 | 402 | |
6206f0e1 JA |
403 | wq_list_for_each(node, prev, &wqe->work_list) { |
404 | work = container_of(node, struct io_wq_work, list); | |
405 | ||
771b53d0 | 406 | /* not hashed, can run anytime */ |
8766dd51 | 407 | if (!io_wq_is_hashed(work)) { |
86f3cd1b | 408 | wq_list_del(&wqe->work_list, node, prev); |
771b53d0 JA |
409 | return work; |
410 | } | |
411 | ||
412 | /* hashed, can run if not already running */ | |
60cf46ae PB |
413 | hash = io_get_work_hash(work); |
414 | if (!(wqe->hash_map & BIT(hash))) { | |
415 | wqe->hash_map |= BIT(hash); | |
86f3cd1b PB |
416 | /* all items with this hash lie in [work, tail] */ |
417 | tail = wqe->hash_tail[hash]; | |
418 | wqe->hash_tail[hash] = NULL; | |
419 | wq_list_cut(&wqe->work_list, &tail->list, prev); | |
771b53d0 JA |
420 | return work; |
421 | } | |
422 | } | |
423 | ||
424 | return NULL; | |
425 | } | |
426 | ||
cccf0ee8 JA |
427 | static void io_wq_switch_mm(struct io_worker *worker, struct io_wq_work *work) |
428 | { | |
429 | if (worker->mm) { | |
f5678e7f | 430 | kthread_unuse_mm(worker->mm); |
cccf0ee8 JA |
431 | mmput(worker->mm); |
432 | worker->mm = NULL; | |
433 | } | |
37c54f9b | 434 | |
98447d65 JA |
435 | if (mmget_not_zero(work->identity->mm)) { |
436 | kthread_use_mm(work->identity->mm); | |
437 | worker->mm = work->identity->mm; | |
cccf0ee8 JA |
438 | return; |
439 | } | |
440 | ||
441 | /* failed grabbing mm, ensure work gets cancelled */ | |
442 | work->flags |= IO_WQ_WORK_CANCEL; | |
443 | } | |
444 | ||
91d8f519 DZ |
445 | static inline void io_wq_switch_blkcg(struct io_worker *worker, |
446 | struct io_wq_work *work) | |
447 | { | |
448 | #ifdef CONFIG_BLK_CGROUP | |
0f203765 JA |
449 | if (!(work->flags & IO_WQ_WORK_BLKCG)) |
450 | return; | |
98447d65 JA |
451 | if (work->identity->blkcg_css != worker->blkcg_css) { |
452 | kthread_associate_blkcg(work->identity->blkcg_css); | |
453 | worker->blkcg_css = work->identity->blkcg_css; | |
91d8f519 DZ |
454 | } |
455 | #endif | |
456 | } | |
457 | ||
cccf0ee8 JA |
458 | static void io_wq_switch_creds(struct io_worker *worker, |
459 | struct io_wq_work *work) | |
460 | { | |
98447d65 | 461 | const struct cred *old_creds = override_creds(work->identity->creds); |
cccf0ee8 | 462 | |
98447d65 | 463 | worker->cur_creds = work->identity->creds; |
cccf0ee8 JA |
464 | if (worker->saved_creds) |
465 | put_cred(old_creds); /* creds set by previous switch */ | |
466 | else | |
467 | worker->saved_creds = old_creds; | |
468 | } | |
469 | ||
dc026a73 PB |
470 | static void io_impersonate_work(struct io_worker *worker, |
471 | struct io_wq_work *work) | |
472 | { | |
98447d65 JA |
473 | if ((work->flags & IO_WQ_WORK_FILES) && |
474 | current->files != work->identity->files) { | |
dc026a73 | 475 | task_lock(current); |
98447d65 JA |
476 | current->files = work->identity->files; |
477 | current->nsproxy = work->identity->nsproxy; | |
dc026a73 | 478 | task_unlock(current); |
3dd1680d JA |
479 | if (!work->identity->files) { |
480 | /* failed grabbing files, ensure work gets cancelled */ | |
481 | work->flags |= IO_WQ_WORK_CANCEL; | |
482 | } | |
dc026a73 | 483 | } |
98447d65 JA |
484 | if ((work->flags & IO_WQ_WORK_FS) && current->fs != work->identity->fs) |
485 | current->fs = work->identity->fs; | |
486 | if ((work->flags & IO_WQ_WORK_MM) && work->identity->mm != worker->mm) | |
dc026a73 | 487 | io_wq_switch_mm(worker, work); |
98447d65 JA |
488 | if ((work->flags & IO_WQ_WORK_CREDS) && |
489 | worker->cur_creds != work->identity->creds) | |
dc026a73 | 490 | io_wq_switch_creds(worker, work); |
69228338 JA |
491 | if (work->flags & IO_WQ_WORK_FSIZE) |
492 | current->signal->rlim[RLIMIT_FSIZE].rlim_cur = work->identity->fsize; | |
493 | else if (current->signal->rlim[RLIMIT_FSIZE].rlim_cur != RLIM_INFINITY) | |
494 | current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; | |
91d8f519 | 495 | io_wq_switch_blkcg(worker, work); |
4ea33a97 JA |
496 | #ifdef CONFIG_AUDIT |
497 | current->loginuid = work->identity->loginuid; | |
498 | current->sessionid = work->identity->sessionid; | |
499 | #endif | |
dc026a73 PB |
500 | } |
501 | ||
502 | static void io_assign_current_work(struct io_worker *worker, | |
503 | struct io_wq_work *work) | |
504 | { | |
d78298e7 PB |
505 | if (work) { |
506 | /* flush pending signals before assigning new work */ | |
507 | if (signal_pending(current)) | |
508 | flush_signals(current); | |
509 | cond_resched(); | |
510 | } | |
dc026a73 | 511 | |
4ea33a97 JA |
512 | #ifdef CONFIG_AUDIT |
513 | current->loginuid = KUIDT_INIT(AUDIT_UID_UNSET); | |
514 | current->sessionid = AUDIT_SID_UNSET; | |
515 | #endif | |
516 | ||
dc026a73 PB |
517 | spin_lock_irq(&worker->lock); |
518 | worker->cur_work = work; | |
519 | spin_unlock_irq(&worker->lock); | |
520 | } | |
521 | ||
60cf46ae PB |
522 | static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work); |
523 | ||
771b53d0 JA |
524 | static void io_worker_handle_work(struct io_worker *worker) |
525 | __releases(wqe->lock) | |
526 | { | |
771b53d0 JA |
527 | struct io_wqe *wqe = worker->wqe; |
528 | struct io_wq *wq = wqe->wq; | |
529 | ||
530 | do { | |
86f3cd1b | 531 | struct io_wq_work *work; |
f462fd36 | 532 | get_next: |
771b53d0 JA |
533 | /* |
534 | * If we got some work, mark us as busy. If we didn't, but | |
535 | * the list isn't empty, it means we stalled on hashed work. | |
536 | * Mark us stalled so we don't keep looking for work when we | |
537 | * can't make progress, any work completion or insertion will | |
538 | * clear the stalled flag. | |
539 | */ | |
60cf46ae | 540 | work = io_get_next_work(wqe); |
771b53d0 JA |
541 | if (work) |
542 | __io_worker_busy(wqe, worker, work); | |
6206f0e1 | 543 | else if (!wq_list_empty(&wqe->work_list)) |
771b53d0 JA |
544 | wqe->flags |= IO_WQE_FLAG_STALLED; |
545 | ||
95da8465 | 546 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 JA |
547 | if (!work) |
548 | break; | |
58e39319 | 549 | io_assign_current_work(worker, work); |
36c2f922 | 550 | |
dc026a73 PB |
551 | /* handle a whole dependent link */ |
552 | do { | |
5280f7e5 | 553 | struct io_wq_work *next_hashed, *linked; |
b089ed39 | 554 | unsigned int hash = io_get_work_hash(work); |
dc026a73 | 555 | |
86f3cd1b | 556 | next_hashed = wq_next_work(work); |
58e39319 | 557 | io_impersonate_work(worker, work); |
5280f7e5 PB |
558 | wq->do_work(work); |
559 | io_assign_current_work(worker, NULL); | |
dc026a73 | 560 | |
5280f7e5 | 561 | linked = wq->free_work(work); |
86f3cd1b PB |
562 | work = next_hashed; |
563 | if (!work && linked && !io_wq_is_hashed(linked)) { | |
564 | work = linked; | |
565 | linked = NULL; | |
566 | } | |
567 | io_assign_current_work(worker, work); | |
86f3cd1b PB |
568 | if (linked) |
569 | io_wqe_enqueue(wqe, linked); | |
570 | ||
571 | if (hash != -1U && !next_hashed) { | |
95da8465 | 572 | raw_spin_lock_irq(&wqe->lock); |
dc026a73 PB |
573 | wqe->hash_map &= ~BIT_ULL(hash); |
574 | wqe->flags &= ~IO_WQE_FLAG_STALLED; | |
f462fd36 PB |
575 | /* skip unnecessary unlock-lock wqe->lock */ |
576 | if (!work) | |
577 | goto get_next; | |
95da8465 | 578 | raw_spin_unlock_irq(&wqe->lock); |
7d723065 | 579 | } |
58e39319 | 580 | } while (work); |
7d723065 | 581 | |
95da8465 | 582 | raw_spin_lock_irq(&wqe->lock); |
771b53d0 JA |
583 | } while (1); |
584 | } | |
585 | ||
771b53d0 JA |
586 | static int io_wqe_worker(void *data) |
587 | { | |
588 | struct io_worker *worker = data; | |
589 | struct io_wqe *wqe = worker->wqe; | |
590 | struct io_wq *wq = wqe->wq; | |
771b53d0 JA |
591 | |
592 | io_worker_start(wqe, worker); | |
593 | ||
594 | while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) { | |
506d95ff | 595 | set_current_state(TASK_INTERRUPTIBLE); |
e995d512 | 596 | loop: |
95da8465 | 597 | raw_spin_lock_irq(&wqe->lock); |
771b53d0 JA |
598 | if (io_wqe_run_queue(wqe)) { |
599 | __set_current_state(TASK_RUNNING); | |
600 | io_worker_handle_work(worker); | |
e995d512 | 601 | goto loop; |
771b53d0 JA |
602 | } |
603 | /* drops the lock on success, retry */ | |
604 | if (__io_worker_idle(wqe, worker)) { | |
605 | __release(&wqe->lock); | |
e995d512 | 606 | goto loop; |
771b53d0 | 607 | } |
95da8465 | 608 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 JA |
609 | if (signal_pending(current)) |
610 | flush_signals(current); | |
611 | if (schedule_timeout(WORKER_IDLE_TIMEOUT)) | |
612 | continue; | |
613 | /* timed out, exit unless we're the fixed worker */ | |
614 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state) || | |
615 | !(worker->flags & IO_WORKER_F_FIXED)) | |
616 | break; | |
617 | } | |
618 | ||
771b53d0 | 619 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) { |
95da8465 | 620 | raw_spin_lock_irq(&wqe->lock); |
6206f0e1 | 621 | if (!wq_list_empty(&wqe->work_list)) |
771b53d0 JA |
622 | io_worker_handle_work(worker); |
623 | else | |
95da8465 | 624 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 JA |
625 | } |
626 | ||
627 | io_worker_exit(worker); | |
628 | return 0; | |
629 | } | |
630 | ||
771b53d0 JA |
631 | /* |
632 | * Called when a worker is scheduled in. Mark us as currently running. | |
633 | */ | |
634 | void io_wq_worker_running(struct task_struct *tsk) | |
635 | { | |
636 | struct io_worker *worker = kthread_data(tsk); | |
637 | struct io_wqe *wqe = worker->wqe; | |
638 | ||
639 | if (!(worker->flags & IO_WORKER_F_UP)) | |
640 | return; | |
641 | if (worker->flags & IO_WORKER_F_RUNNING) | |
642 | return; | |
643 | worker->flags |= IO_WORKER_F_RUNNING; | |
c5def4ab | 644 | io_wqe_inc_running(wqe, worker); |
771b53d0 JA |
645 | } |
646 | ||
647 | /* | |
648 | * Called when worker is going to sleep. If there are no workers currently | |
649 | * running and we have work pending, wake up a free one or have the manager | |
650 | * set one up. | |
651 | */ | |
652 | void io_wq_worker_sleeping(struct task_struct *tsk) | |
653 | { | |
654 | struct io_worker *worker = kthread_data(tsk); | |
655 | struct io_wqe *wqe = worker->wqe; | |
656 | ||
657 | if (!(worker->flags & IO_WORKER_F_UP)) | |
658 | return; | |
659 | if (!(worker->flags & IO_WORKER_F_RUNNING)) | |
660 | return; | |
661 | ||
662 | worker->flags &= ~IO_WORKER_F_RUNNING; | |
663 | ||
95da8465 | 664 | raw_spin_lock_irq(&wqe->lock); |
c5def4ab | 665 | io_wqe_dec_running(wqe, worker); |
95da8465 | 666 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 JA |
667 | } |
668 | ||
b60fda60 | 669 | static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index) |
771b53d0 | 670 | { |
c4068bf8 | 671 | struct io_wqe_acct *acct = &wqe->acct[index]; |
771b53d0 JA |
672 | struct io_worker *worker; |
673 | ||
ad6e005c | 674 | worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node); |
771b53d0 | 675 | if (!worker) |
b60fda60 | 676 | return false; |
771b53d0 JA |
677 | |
678 | refcount_set(&worker->ref, 1); | |
679 | worker->nulls_node.pprev = NULL; | |
771b53d0 | 680 | worker->wqe = wqe; |
36c2f922 | 681 | spin_lock_init(&worker->lock); |
771b53d0 JA |
682 | |
683 | worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node, | |
c5def4ab | 684 | "io_wqe_worker-%d/%d", index, wqe->node); |
771b53d0 JA |
685 | if (IS_ERR(worker->task)) { |
686 | kfree(worker); | |
b60fda60 | 687 | return false; |
771b53d0 | 688 | } |
a8b595b2 | 689 | kthread_bind_mask(worker->task, cpumask_of_node(wqe->node)); |
771b53d0 | 690 | |
95da8465 | 691 | raw_spin_lock_irq(&wqe->lock); |
021d1cdd | 692 | hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list); |
e61df66c | 693 | list_add_tail_rcu(&worker->all_list, &wqe->all_list); |
771b53d0 | 694 | worker->flags |= IO_WORKER_F_FREE; |
c5def4ab JA |
695 | if (index == IO_WQ_ACCT_BOUND) |
696 | worker->flags |= IO_WORKER_F_BOUND; | |
697 | if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND)) | |
771b53d0 | 698 | worker->flags |= IO_WORKER_F_FIXED; |
c5def4ab | 699 | acct->nr_workers++; |
95da8465 | 700 | raw_spin_unlock_irq(&wqe->lock); |
771b53d0 | 701 | |
c5def4ab JA |
702 | if (index == IO_WQ_ACCT_UNBOUND) |
703 | atomic_inc(&wq->user->processes); | |
704 | ||
c4068bf8 | 705 | refcount_inc(&wq->refs); |
771b53d0 | 706 | wake_up_process(worker->task); |
b60fda60 | 707 | return true; |
771b53d0 JA |
708 | } |
709 | ||
c5def4ab | 710 | static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index) |
771b53d0 JA |
711 | __must_hold(wqe->lock) |
712 | { | |
c5def4ab | 713 | struct io_wqe_acct *acct = &wqe->acct[index]; |
771b53d0 | 714 | |
c5def4ab | 715 | /* if we have available workers or no work, no need */ |
021d1cdd | 716 | if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe)) |
c5def4ab JA |
717 | return false; |
718 | return acct->nr_workers < acct->max_workers; | |
771b53d0 JA |
719 | } |
720 | ||
c4068bf8 HD |
721 | /* |
722 | * Iterate the passed in list and call the specific function for each | |
723 | * worker that isn't exiting | |
724 | */ | |
725 | static bool io_wq_for_each_worker(struct io_wqe *wqe, | |
726 | bool (*func)(struct io_worker *, void *), | |
727 | void *data) | |
728 | { | |
729 | struct io_worker *worker; | |
730 | bool ret = false; | |
731 | ||
732 | list_for_each_entry_rcu(worker, &wqe->all_list, all_list) { | |
733 | if (io_worker_get(worker)) { | |
734 | /* no task if node is/was offline */ | |
735 | if (worker->task) | |
736 | ret = func(worker, data); | |
737 | io_worker_release(worker); | |
738 | if (ret) | |
739 | break; | |
740 | } | |
741 | } | |
742 | ||
743 | return ret; | |
744 | } | |
745 | ||
746 | static bool io_wq_worker_wake(struct io_worker *worker, void *data) | |
747 | { | |
748 | wake_up_process(worker->task); | |
749 | return false; | |
750 | } | |
751 | ||
771b53d0 JA |
752 | /* |
753 | * Manager thread. Tasked with creating new workers, if we need them. | |
754 | */ | |
755 | static int io_wq_manager(void *data) | |
756 | { | |
757 | struct io_wq *wq = data; | |
3fc50ab5 | 758 | int node; |
771b53d0 | 759 | |
c4068bf8 | 760 | refcount_set(&wq->refs, 1); |
b60fda60 JA |
761 | complete(&wq->done); |
762 | ||
763 | while (!kthread_should_stop()) { | |
3fc50ab5 JH |
764 | for_each_node(node) { |
765 | struct io_wqe *wqe = wq->wqes[node]; | |
c5def4ab | 766 | bool fork_worker[2] = { false, false }; |
771b53d0 | 767 | |
7563439a JA |
768 | if (!node_online(node)) |
769 | continue; | |
770 | ||
95da8465 | 771 | raw_spin_lock_irq(&wqe->lock); |
c5def4ab JA |
772 | if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND)) |
773 | fork_worker[IO_WQ_ACCT_BOUND] = true; | |
774 | if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND)) | |
775 | fork_worker[IO_WQ_ACCT_UNBOUND] = true; | |
95da8465 | 776 | raw_spin_unlock_irq(&wqe->lock); |
c5def4ab JA |
777 | if (fork_worker[IO_WQ_ACCT_BOUND]) |
778 | create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND); | |
779 | if (fork_worker[IO_WQ_ACCT_UNBOUND]) | |
780 | create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND); | |
771b53d0 JA |
781 | } |
782 | set_current_state(TASK_INTERRUPTIBLE); | |
783 | schedule_timeout(HZ); | |
784 | } | |
785 | ||
c4068bf8 | 786 | if (refcount_dec_and_test(&wq->refs)) { |
b60fda60 | 787 | complete(&wq->done); |
c4068bf8 HD |
788 | return 0; |
789 | } | |
790 | /* if ERROR is set and we get here, we have workers to wake */ | |
791 | if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) { | |
792 | rcu_read_lock(); | |
793 | for_each_node(node) | |
794 | io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL); | |
795 | rcu_read_unlock(); | |
796 | } | |
b60fda60 | 797 | return 0; |
771b53d0 JA |
798 | } |
799 | ||
c5def4ab JA |
800 | static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct, |
801 | struct io_wq_work *work) | |
802 | { | |
803 | bool free_worker; | |
804 | ||
805 | if (!(work->flags & IO_WQ_WORK_UNBOUND)) | |
806 | return true; | |
807 | if (atomic_read(&acct->nr_running)) | |
808 | return true; | |
809 | ||
810 | rcu_read_lock(); | |
021d1cdd | 811 | free_worker = !hlist_nulls_empty(&wqe->free_list); |
c5def4ab JA |
812 | rcu_read_unlock(); |
813 | if (free_worker) | |
814 | return true; | |
815 | ||
816 | if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers && | |
817 | !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN))) | |
818 | return false; | |
819 | ||
820 | return true; | |
821 | } | |
822 | ||
e9fd9396 | 823 | static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe) |
fc04c39b | 824 | { |
e9fd9396 PB |
825 | struct io_wq *wq = wqe->wq; |
826 | ||
fc04c39b | 827 | do { |
fc04c39b | 828 | work->flags |= IO_WQ_WORK_CANCEL; |
5280f7e5 PB |
829 | wq->do_work(work); |
830 | work = wq->free_work(work); | |
fc04c39b PB |
831 | } while (work); |
832 | } | |
833 | ||
86f3cd1b PB |
834 | static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work) |
835 | { | |
836 | unsigned int hash; | |
837 | struct io_wq_work *tail; | |
838 | ||
839 | if (!io_wq_is_hashed(work)) { | |
840 | append: | |
841 | wq_list_add_tail(&work->list, &wqe->work_list); | |
842 | return; | |
843 | } | |
844 | ||
845 | hash = io_get_work_hash(work); | |
846 | tail = wqe->hash_tail[hash]; | |
847 | wqe->hash_tail[hash] = work; | |
848 | if (!tail) | |
849 | goto append; | |
850 | ||
851 | wq_list_add_after(&work->list, &tail->list, &wqe->work_list); | |
852 | } | |
853 | ||
771b53d0 JA |
854 | static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work) |
855 | { | |
c5def4ab | 856 | struct io_wqe_acct *acct = io_work_get_acct(wqe, work); |
895e2ca0 | 857 | int work_flags; |
771b53d0 JA |
858 | unsigned long flags; |
859 | ||
c5def4ab JA |
860 | /* |
861 | * Do early check to see if we need a new unbound worker, and if we do, | |
862 | * if we're allowed to do so. This isn't 100% accurate as there's a | |
863 | * gap between this check and incrementing the value, but that's OK. | |
864 | * It's close enough to not be an issue, fork() has the same delay. | |
865 | */ | |
866 | if (unlikely(!io_wq_can_queue(wqe, acct, work))) { | |
e9fd9396 | 867 | io_run_cancel(work, wqe); |
c5def4ab JA |
868 | return; |
869 | } | |
870 | ||
895e2ca0 | 871 | work_flags = work->flags; |
95da8465 | 872 | raw_spin_lock_irqsave(&wqe->lock, flags); |
86f3cd1b | 873 | io_wqe_insert_work(wqe, work); |
771b53d0 | 874 | wqe->flags &= ~IO_WQE_FLAG_STALLED; |
95da8465 | 875 | raw_spin_unlock_irqrestore(&wqe->lock, flags); |
771b53d0 | 876 | |
895e2ca0 JA |
877 | if ((work_flags & IO_WQ_WORK_CONCURRENT) || |
878 | !atomic_read(&acct->nr_running)) | |
c5def4ab | 879 | io_wqe_wake_worker(wqe, acct); |
771b53d0 JA |
880 | } |
881 | ||
882 | void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work) | |
883 | { | |
884 | struct io_wqe *wqe = wq->wqes[numa_node_id()]; | |
885 | ||
886 | io_wqe_enqueue(wqe, work); | |
887 | } | |
888 | ||
889 | /* | |
8766dd51 PB |
890 | * Work items that hash to the same value will not be done in parallel. |
891 | * Used to limit concurrent writes, generally hashed by inode. | |
771b53d0 | 892 | */ |
8766dd51 | 893 | void io_wq_hash_work(struct io_wq_work *work, void *val) |
771b53d0 | 894 | { |
8766dd51 | 895 | unsigned int bit; |
771b53d0 JA |
896 | |
897 | bit = hash_ptr(val, IO_WQ_HASH_ORDER); | |
898 | work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT)); | |
771b53d0 JA |
899 | } |
900 | ||
62755e35 | 901 | struct io_cb_cancel_data { |
2293b419 PB |
902 | work_cancel_fn *fn; |
903 | void *data; | |
4f26bda1 PB |
904 | int nr_running; |
905 | int nr_pending; | |
906 | bool cancel_all; | |
62755e35 JA |
907 | }; |
908 | ||
2293b419 | 909 | static bool io_wq_worker_cancel(struct io_worker *worker, void *data) |
62755e35 | 910 | { |
2293b419 | 911 | struct io_cb_cancel_data *match = data; |
6f72653e | 912 | unsigned long flags; |
62755e35 JA |
913 | |
914 | /* | |
915 | * Hold the lock to avoid ->cur_work going out of scope, caller | |
36c2f922 | 916 | * may dereference the passed in work. |
62755e35 | 917 | */ |
36c2f922 | 918 | spin_lock_irqsave(&worker->lock, flags); |
62755e35 | 919 | if (worker->cur_work && |
2293b419 | 920 | match->fn(worker->cur_work, match->data)) { |
771b53d0 | 921 | send_sig(SIGINT, worker->task, 1); |
4f26bda1 | 922 | match->nr_running++; |
771b53d0 | 923 | } |
36c2f922 | 924 | spin_unlock_irqrestore(&worker->lock, flags); |
771b53d0 | 925 | |
4f26bda1 | 926 | return match->nr_running && !match->cancel_all; |
771b53d0 JA |
927 | } |
928 | ||
204361a7 PB |
929 | static inline void io_wqe_remove_pending(struct io_wqe *wqe, |
930 | struct io_wq_work *work, | |
931 | struct io_wq_work_node *prev) | |
932 | { | |
933 | unsigned int hash = io_get_work_hash(work); | |
934 | struct io_wq_work *prev_work = NULL; | |
935 | ||
936 | if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) { | |
937 | if (prev) | |
938 | prev_work = container_of(prev, struct io_wq_work, list); | |
939 | if (prev_work && io_get_work_hash(prev_work) == hash) | |
940 | wqe->hash_tail[hash] = prev_work; | |
941 | else | |
942 | wqe->hash_tail[hash] = NULL; | |
943 | } | |
944 | wq_list_del(&wqe->work_list, &work->list, prev); | |
945 | } | |
946 | ||
4f26bda1 | 947 | static void io_wqe_cancel_pending_work(struct io_wqe *wqe, |
f4c2665e | 948 | struct io_cb_cancel_data *match) |
771b53d0 | 949 | { |
6206f0e1 | 950 | struct io_wq_work_node *node, *prev; |
771b53d0 | 951 | struct io_wq_work *work; |
6f72653e | 952 | unsigned long flags; |
771b53d0 | 953 | |
4f26bda1 | 954 | retry: |
95da8465 | 955 | raw_spin_lock_irqsave(&wqe->lock, flags); |
6206f0e1 JA |
956 | wq_list_for_each(node, prev, &wqe->work_list) { |
957 | work = container_of(node, struct io_wq_work, list); | |
4f26bda1 PB |
958 | if (!match->fn(work, match->data)) |
959 | continue; | |
204361a7 | 960 | io_wqe_remove_pending(wqe, work, prev); |
95da8465 | 961 | raw_spin_unlock_irqrestore(&wqe->lock, flags); |
4f26bda1 PB |
962 | io_run_cancel(work, wqe); |
963 | match->nr_pending++; | |
964 | if (!match->cancel_all) | |
965 | return; | |
966 | ||
967 | /* not safe to continue after unlock */ | |
968 | goto retry; | |
771b53d0 | 969 | } |
95da8465 | 970 | raw_spin_unlock_irqrestore(&wqe->lock, flags); |
f4c2665e PB |
971 | } |
972 | ||
4f26bda1 | 973 | static void io_wqe_cancel_running_work(struct io_wqe *wqe, |
f4c2665e PB |
974 | struct io_cb_cancel_data *match) |
975 | { | |
771b53d0 | 976 | rcu_read_lock(); |
4f26bda1 | 977 | io_wq_for_each_worker(wqe, io_wq_worker_cancel, match); |
771b53d0 | 978 | rcu_read_unlock(); |
771b53d0 JA |
979 | } |
980 | ||
2293b419 | 981 | enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel, |
4f26bda1 | 982 | void *data, bool cancel_all) |
771b53d0 | 983 | { |
2293b419 | 984 | struct io_cb_cancel_data match = { |
4f26bda1 PB |
985 | .fn = cancel, |
986 | .data = data, | |
987 | .cancel_all = cancel_all, | |
00bcda13 | 988 | }; |
3fc50ab5 | 989 | int node; |
771b53d0 | 990 | |
f4c2665e PB |
991 | /* |
992 | * First check pending list, if we're lucky we can just remove it | |
993 | * from there. CANCEL_OK means that the work is returned as-new, | |
994 | * no completion will be posted for it. | |
995 | */ | |
3fc50ab5 JH |
996 | for_each_node(node) { |
997 | struct io_wqe *wqe = wq->wqes[node]; | |
771b53d0 | 998 | |
4f26bda1 PB |
999 | io_wqe_cancel_pending_work(wqe, &match); |
1000 | if (match.nr_pending && !match.cancel_all) | |
f4c2665e | 1001 | return IO_WQ_CANCEL_OK; |
771b53d0 JA |
1002 | } |
1003 | ||
f4c2665e PB |
1004 | /* |
1005 | * Now check if a free (going busy) or busy worker has the work | |
1006 | * currently running. If we find it there, we'll return CANCEL_RUNNING | |
1007 | * as an indication that we attempt to signal cancellation. The | |
1008 | * completion will run normally in this case. | |
1009 | */ | |
1010 | for_each_node(node) { | |
1011 | struct io_wqe *wqe = wq->wqes[node]; | |
1012 | ||
4f26bda1 PB |
1013 | io_wqe_cancel_running_work(wqe, &match); |
1014 | if (match.nr_running && !match.cancel_all) | |
f4c2665e PB |
1015 | return IO_WQ_CANCEL_RUNNING; |
1016 | } | |
1017 | ||
4f26bda1 PB |
1018 | if (match.nr_running) |
1019 | return IO_WQ_CANCEL_RUNNING; | |
1020 | if (match.nr_pending) | |
1021 | return IO_WQ_CANCEL_OK; | |
f4c2665e | 1022 | return IO_WQ_CANCEL_NOTFOUND; |
771b53d0 JA |
1023 | } |
1024 | ||
576a347b | 1025 | struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data) |
771b53d0 | 1026 | { |
3fc50ab5 | 1027 | int ret = -ENOMEM, node; |
771b53d0 JA |
1028 | struct io_wq *wq; |
1029 | ||
f5fa38c5 | 1030 | if (WARN_ON_ONCE(!data->free_work || !data->do_work)) |
e9fd9396 PB |
1031 | return ERR_PTR(-EINVAL); |
1032 | ||
ad6e005c | 1033 | wq = kzalloc(sizeof(*wq), GFP_KERNEL); |
771b53d0 JA |
1034 | if (!wq) |
1035 | return ERR_PTR(-ENOMEM); | |
1036 | ||
3fc50ab5 | 1037 | wq->wqes = kcalloc(nr_node_ids, sizeof(struct io_wqe *), GFP_KERNEL); |
43c01fbe JA |
1038 | if (!wq->wqes) |
1039 | goto err_wq; | |
1040 | ||
1041 | ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node); | |
1042 | if (ret) | |
1043 | goto err_wqes; | |
771b53d0 | 1044 | |
e9fd9396 | 1045 | wq->free_work = data->free_work; |
f5fa38c5 | 1046 | wq->do_work = data->do_work; |
7d723065 | 1047 | |
c5def4ab | 1048 | /* caller must already hold a reference to this */ |
576a347b | 1049 | wq->user = data->user; |
c5def4ab | 1050 | |
43c01fbe | 1051 | ret = -ENOMEM; |
3fc50ab5 | 1052 | for_each_node(node) { |
771b53d0 | 1053 | struct io_wqe *wqe; |
7563439a | 1054 | int alloc_node = node; |
771b53d0 | 1055 | |
7563439a JA |
1056 | if (!node_online(alloc_node)) |
1057 | alloc_node = NUMA_NO_NODE; | |
1058 | wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node); | |
771b53d0 | 1059 | if (!wqe) |
3fc50ab5 JH |
1060 | goto err; |
1061 | wq->wqes[node] = wqe; | |
7563439a | 1062 | wqe->node = alloc_node; |
c5def4ab JA |
1063 | wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded; |
1064 | atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0); | |
576a347b | 1065 | if (wq->user) { |
c5def4ab JA |
1066 | wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers = |
1067 | task_rlimit(current, RLIMIT_NPROC); | |
1068 | } | |
1069 | atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0); | |
771b53d0 | 1070 | wqe->wq = wq; |
95da8465 | 1071 | raw_spin_lock_init(&wqe->lock); |
6206f0e1 | 1072 | INIT_WQ_LIST(&wqe->work_list); |
021d1cdd | 1073 | INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0); |
e61df66c | 1074 | INIT_LIST_HEAD(&wqe->all_list); |
771b53d0 JA |
1075 | } |
1076 | ||
1077 | init_completion(&wq->done); | |
1078 | ||
771b53d0 JA |
1079 | wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager"); |
1080 | if (!IS_ERR(wq->manager)) { | |
1081 | wake_up_process(wq->manager); | |
b60fda60 JA |
1082 | wait_for_completion(&wq->done); |
1083 | if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) { | |
1084 | ret = -ENOMEM; | |
1085 | goto err; | |
1086 | } | |
1087 | reinit_completion(&wq->done); | |
771b53d0 JA |
1088 | return wq; |
1089 | } | |
1090 | ||
1091 | ret = PTR_ERR(wq->manager); | |
771b53d0 | 1092 | complete(&wq->done); |
b60fda60 | 1093 | err: |
43c01fbe | 1094 | cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
3fc50ab5 JH |
1095 | for_each_node(node) |
1096 | kfree(wq->wqes[node]); | |
43c01fbe | 1097 | err_wqes: |
b60fda60 | 1098 | kfree(wq->wqes); |
43c01fbe | 1099 | err_wq: |
b60fda60 | 1100 | kfree(wq); |
771b53d0 JA |
1101 | return ERR_PTR(ret); |
1102 | } | |
1103 | ||
3b094e72 | 1104 | void io_wq_destroy(struct io_wq *wq) |
771b53d0 | 1105 | { |
3fc50ab5 | 1106 | int node; |
771b53d0 | 1107 | |
43c01fbe JA |
1108 | cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
1109 | ||
b60fda60 JA |
1110 | set_bit(IO_WQ_BIT_EXIT, &wq->state); |
1111 | if (wq->manager) | |
771b53d0 | 1112 | kthread_stop(wq->manager); |
771b53d0 JA |
1113 | |
1114 | rcu_read_lock(); | |
3fc50ab5 JH |
1115 | for_each_node(node) |
1116 | io_wq_for_each_worker(wq->wqes[node], io_wq_worker_wake, NULL); | |
771b53d0 JA |
1117 | rcu_read_unlock(); |
1118 | ||
1119 | wait_for_completion(&wq->done); | |
1120 | ||
3fc50ab5 JH |
1121 | for_each_node(node) |
1122 | kfree(wq->wqes[node]); | |
771b53d0 JA |
1123 | kfree(wq->wqes); |
1124 | kfree(wq); | |
1125 | } | |
848f7e18 | 1126 | |
43c01fbe JA |
1127 | static bool io_wq_worker_affinity(struct io_worker *worker, void *data) |
1128 | { | |
1129 | struct task_struct *task = worker->task; | |
1130 | struct rq_flags rf; | |
1131 | struct rq *rq; | |
1132 | ||
1133 | rq = task_rq_lock(task, &rf); | |
1134 | do_set_cpus_allowed(task, cpumask_of_node(worker->wqe->node)); | |
1135 | task->flags |= PF_NO_SETAFFINITY; | |
1136 | task_rq_unlock(rq, task, &rf); | |
1137 | return false; | |
1138 | } | |
1139 | ||
1140 | static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node) | |
1141 | { | |
1142 | struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node); | |
1143 | int i; | |
1144 | ||
1145 | rcu_read_lock(); | |
1146 | for_each_node(i) | |
1147 | io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, NULL); | |
1148 | rcu_read_unlock(); | |
1149 | return 0; | |
1150 | } | |
1151 | ||
1152 | static __init int io_wq_init(void) | |
1153 | { | |
1154 | int ret; | |
1155 | ||
1156 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online", | |
1157 | io_wq_cpu_online, NULL); | |
1158 | if (ret < 0) | |
1159 | return ret; | |
1160 | io_wq_online = ret; | |
1161 | return 0; | |
1162 | } | |
1163 | subsys_initcall(io_wq_init); |