Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
86db1e29 JA |
2 | /* |
3 | * Functions related to io context handling | |
4 | */ | |
5 | #include <linux/kernel.h> | |
6 | #include <linux/module.h> | |
7 | #include <linux/init.h> | |
8 | #include <linux/bio.h> | |
9 | #include <linux/blkdev.h> | |
5a0e3ad6 | 10 | #include <linux/slab.h> |
f719ff9b | 11 | #include <linux/sched/task.h> |
86db1e29 JA |
12 | |
13 | #include "blk.h" | |
2aa7745b | 14 | #include "blk-mq-sched.h" |
86db1e29 JA |
15 | |
16 | /* | |
17 | * For io context allocations | |
18 | */ | |
19 | static struct kmem_cache *iocontext_cachep; | |
20 | ||
6e736be7 TH |
21 | /** |
22 | * get_io_context - increment reference count to io_context | |
23 | * @ioc: io_context to get | |
24 | * | |
25 | * Increment reference count to @ioc. | |
26 | */ | |
87dd1d63 | 27 | static void get_io_context(struct io_context *ioc) |
6e736be7 TH |
28 | { |
29 | BUG_ON(atomic_long_read(&ioc->refcount) <= 0); | |
30 | atomic_long_inc(&ioc->refcount); | |
31 | } | |
6e736be7 | 32 | |
7e5a8794 TH |
33 | static void icq_free_icq_rcu(struct rcu_head *head) |
34 | { | |
35 | struct io_cq *icq = container_of(head, struct io_cq, __rcu_head); | |
36 | ||
37 | kmem_cache_free(icq->__rcu_icq_cache, icq); | |
38 | } | |
39 | ||
3d492c2e | 40 | /* |
7b36a718 JA |
41 | * Exit an icq. Called with ioc locked for blk-mq, and with both ioc |
42 | * and queue locked for legacy. | |
3d492c2e | 43 | */ |
7e5a8794 | 44 | static void ioc_exit_icq(struct io_cq *icq) |
621032ad TH |
45 | { |
46 | struct elevator_type *et = icq->q->elevator->type; | |
47 | ||
48 | if (icq->flags & ICQ_EXITED) | |
49 | return; | |
50 | ||
f9cd4bfe JA |
51 | if (et->ops.exit_icq) |
52 | et->ops.exit_icq(icq); | |
621032ad TH |
53 | |
54 | icq->flags |= ICQ_EXITED; | |
55 | } | |
56 | ||
4be8a2ea CH |
57 | static void ioc_exit_icqs(struct io_context *ioc) |
58 | { | |
59 | struct io_cq *icq; | |
60 | ||
61 | spin_lock_irq(&ioc->lock); | |
62 | hlist_for_each_entry(icq, &ioc->icq_list, ioc_node) | |
63 | ioc_exit_icq(icq); | |
64 | spin_unlock_irq(&ioc->lock); | |
65 | } | |
66 | ||
7b36a718 JA |
67 | /* |
68 | * Release an icq. Called with ioc locked for blk-mq, and with both ioc | |
69 | * and queue locked for legacy. | |
70 | */ | |
621032ad | 71 | static void ioc_destroy_icq(struct io_cq *icq) |
7e5a8794 TH |
72 | { |
73 | struct io_context *ioc = icq->ioc; | |
74 | struct request_queue *q = icq->q; | |
75 | struct elevator_type *et = q->elevator->type; | |
76 | ||
77 | lockdep_assert_held(&ioc->lock); | |
7e5a8794 TH |
78 | |
79 | radix_tree_delete(&ioc->icq_tree, icq->q->id); | |
80 | hlist_del_init(&icq->ioc_node); | |
81 | list_del_init(&icq->q_node); | |
82 | ||
83 | /* | |
84 | * Both setting lookup hint to and clearing it from @icq are done | |
85 | * under queue_lock. If it's not pointing to @icq now, it never | |
86 | * will. Hint assignment itself can race safely. | |
87 | */ | |
ec6c676a | 88 | if (rcu_access_pointer(ioc->icq_hint) == icq) |
7e5a8794 TH |
89 | rcu_assign_pointer(ioc->icq_hint, NULL); |
90 | ||
621032ad | 91 | ioc_exit_icq(icq); |
7e5a8794 TH |
92 | |
93 | /* | |
94 | * @icq->q might have gone away by the time RCU callback runs | |
95 | * making it impossible to determine icq_cache. Record it in @icq. | |
96 | */ | |
97 | icq->__rcu_icq_cache = et->icq_cache; | |
30a2da7b | 98 | icq->flags |= ICQ_DESTROYED; |
7e5a8794 TH |
99 | call_rcu(&icq->__rcu_head, icq_free_icq_rcu); |
100 | } | |
101 | ||
b2efa052 TH |
102 | /* |
103 | * Slow path for ioc release in put_io_context(). Performs double-lock | |
c5869807 | 104 | * dancing to unlink all icq's and then frees ioc. |
b2efa052 TH |
105 | */ |
106 | static void ioc_release_fn(struct work_struct *work) | |
86db1e29 | 107 | { |
b2efa052 TH |
108 | struct io_context *ioc = container_of(work, struct io_context, |
109 | release_work); | |
a43f085f | 110 | spin_lock_irq(&ioc->lock); |
b2efa052 | 111 | |
c5869807 TH |
112 | while (!hlist_empty(&ioc->icq_list)) { |
113 | struct io_cq *icq = hlist_entry(ioc->icq_list.first, | |
114 | struct io_cq, ioc_node); | |
2274b029 TH |
115 | struct request_queue *q = icq->q; |
116 | ||
0d945c1f | 117 | if (spin_trylock(&q->queue_lock)) { |
621032ad | 118 | ioc_destroy_icq(icq); |
0d945c1f | 119 | spin_unlock(&q->queue_lock); |
2274b029 | 120 | } else { |
ab96bbab JO |
121 | /* Make sure q and icq cannot be freed. */ |
122 | rcu_read_lock(); | |
123 | ||
124 | /* Re-acquire the locks in the correct order. */ | |
125 | spin_unlock(&ioc->lock); | |
126 | spin_lock(&q->queue_lock); | |
127 | spin_lock(&ioc->lock); | |
128 | ||
129 | /* | |
130 | * The icq may have been destroyed when the ioc lock | |
131 | * was released. | |
132 | */ | |
133 | if (!(icq->flags & ICQ_DESTROYED)) | |
134 | ioc_destroy_icq(icq); | |
135 | ||
136 | spin_unlock(&q->queue_lock); | |
137 | rcu_read_unlock(); | |
b2efa052 | 138 | } |
b2efa052 | 139 | } |
ffc4e759 | 140 | |
a43f085f | 141 | spin_unlock_irq(&ioc->lock); |
b2efa052 TH |
142 | |
143 | kmem_cache_free(iocontext_cachep, ioc); | |
86db1e29 JA |
144 | } |
145 | ||
42ec57a8 TH |
146 | /** |
147 | * put_io_context - put a reference of io_context | |
148 | * @ioc: io_context to put | |
149 | * | |
150 | * Decrement reference count of @ioc and release it if the count reaches | |
11a3122f | 151 | * zero. |
86db1e29 | 152 | */ |
11a3122f | 153 | void put_io_context(struct io_context *ioc) |
86db1e29 | 154 | { |
b2efa052 | 155 | unsigned long flags; |
ff8c1474 | 156 | bool free_ioc = false; |
b2efa052 | 157 | |
42ec57a8 | 158 | BUG_ON(atomic_long_read(&ioc->refcount) <= 0); |
86db1e29 | 159 | |
b2efa052 | 160 | /* |
11a3122f TH |
161 | * Releasing ioc requires reverse order double locking and we may |
162 | * already be holding a queue_lock. Do it asynchronously from wq. | |
b2efa052 | 163 | */ |
11a3122f TH |
164 | if (atomic_long_dec_and_test(&ioc->refcount)) { |
165 | spin_lock_irqsave(&ioc->lock, flags); | |
166 | if (!hlist_empty(&ioc->icq_list)) | |
695588f9 VK |
167 | queue_work(system_power_efficient_wq, |
168 | &ioc->release_work); | |
ff8c1474 XF |
169 | else |
170 | free_ioc = true; | |
11a3122f | 171 | spin_unlock_irqrestore(&ioc->lock, flags); |
b2efa052 | 172 | } |
ff8c1474 XF |
173 | |
174 | if (free_ioc) | |
175 | kmem_cache_free(iocontext_cachep, ioc); | |
86db1e29 | 176 | } |
222ee581 | 177 | EXPORT_SYMBOL_GPL(put_io_context); |
86db1e29 | 178 | |
f6e8d01b TH |
179 | /* Called by the exiting task */ |
180 | void exit_io_context(struct task_struct *task) | |
181 | { | |
182 | struct io_context *ioc; | |
183 | ||
184 | task_lock(task); | |
185 | ioc = task->io_context; | |
186 | task->io_context = NULL; | |
187 | task_unlock(task); | |
188 | ||
4be8a2ea CH |
189 | if (atomic_dec_and_test(&ioc->active_ref)) { |
190 | ioc_exit_icqs(ioc); | |
191 | put_io_context(ioc); | |
192 | } | |
f6e8d01b TH |
193 | } |
194 | ||
7b36a718 JA |
195 | static void __ioc_clear_queue(struct list_head *icq_list) |
196 | { | |
197 | unsigned long flags; | |
198 | ||
30a2da7b | 199 | rcu_read_lock(); |
7b36a718 JA |
200 | while (!list_empty(icq_list)) { |
201 | struct io_cq *icq = list_entry(icq_list->next, | |
a1ce35fa | 202 | struct io_cq, q_node); |
7b36a718 JA |
203 | struct io_context *ioc = icq->ioc; |
204 | ||
205 | spin_lock_irqsave(&ioc->lock, flags); | |
30a2da7b ST |
206 | if (icq->flags & ICQ_DESTROYED) { |
207 | spin_unlock_irqrestore(&ioc->lock, flags); | |
208 | continue; | |
209 | } | |
7b36a718 JA |
210 | ioc_destroy_icq(icq); |
211 | spin_unlock_irqrestore(&ioc->lock, flags); | |
212 | } | |
30a2da7b | 213 | rcu_read_unlock(); |
7b36a718 JA |
214 | } |
215 | ||
7e5a8794 TH |
216 | /** |
217 | * ioc_clear_queue - break any ioc association with the specified queue | |
218 | * @q: request_queue being cleared | |
219 | * | |
7b36a718 | 220 | * Walk @q->icq_list and exit all io_cq's. |
7e5a8794 TH |
221 | */ |
222 | void ioc_clear_queue(struct request_queue *q) | |
223 | { | |
7b36a718 | 224 | LIST_HEAD(icq_list); |
7e5a8794 | 225 | |
0d945c1f | 226 | spin_lock_irq(&q->queue_lock); |
7b36a718 | 227 | list_splice_init(&q->icq_list, &icq_list); |
0d945c1f | 228 | spin_unlock_irq(&q->queue_lock); |
7e5a8794 | 229 | |
a1ce35fa | 230 | __ioc_clear_queue(&icq_list); |
7e5a8794 TH |
231 | } |
232 | ||
a0f14d8b | 233 | static struct io_context *alloc_io_context(gfp_t gfp_flags, int node) |
86db1e29 | 234 | { |
df415656 | 235 | struct io_context *ioc; |
86db1e29 | 236 | |
42ec57a8 TH |
237 | ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO, |
238 | node); | |
239 | if (unlikely(!ioc)) | |
a0f14d8b | 240 | return NULL; |
42ec57a8 | 241 | |
42ec57a8 | 242 | atomic_long_set(&ioc->refcount, 1); |
f6e8d01b | 243 | atomic_set(&ioc->active_ref, 1); |
42ec57a8 | 244 | spin_lock_init(&ioc->lock); |
c137969b | 245 | INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC); |
c5869807 | 246 | INIT_HLIST_HEAD(&ioc->icq_list); |
b2efa052 | 247 | INIT_WORK(&ioc->release_work, ioc_release_fn); |
a0f14d8b CH |
248 | return ioc; |
249 | } | |
250 | ||
d538ea4c CH |
251 | static struct io_context *create_task_io_context(struct task_struct *task, |
252 | gfp_t gfp_flags, int node) | |
a0f14d8b CH |
253 | { |
254 | struct io_context *ioc; | |
a0f14d8b CH |
255 | |
256 | ioc = alloc_io_context(gfp_flags, node); | |
257 | if (!ioc) | |
d538ea4c | 258 | return NULL; |
86db1e29 | 259 | |
fd638368 TH |
260 | /* |
261 | * Try to install. ioc shouldn't be installed if someone else | |
262 | * already did or @task, which isn't %current, is exiting. Note | |
263 | * that we need to allow ioc creation on exiting %current as exit | |
264 | * path may issue IOs from e.g. exit_files(). The exit path is | |
265 | * responsible for not issuing IO after exit_io_context(). | |
266 | */ | |
6e736be7 | 267 | task_lock(task); |
fd638368 TH |
268 | if (!task->io_context && |
269 | (task == current || !(task->flags & PF_EXITING))) | |
6e736be7 | 270 | task->io_context = ioc; |
f2dbd76a | 271 | else |
6e736be7 | 272 | kmem_cache_free(iocontext_cachep, ioc); |
3c9c708c | 273 | |
d538ea4c CH |
274 | ioc = task->io_context; |
275 | if (ioc) | |
276 | get_io_context(ioc); | |
6e736be7 | 277 | task_unlock(task); |
d538ea4c | 278 | return ioc; |
86db1e29 | 279 | } |
86db1e29 | 280 | |
6e736be7 TH |
281 | /** |
282 | * get_task_io_context - get io_context of a task | |
283 | * @task: task of interest | |
284 | * @gfp_flags: allocation flags, used if allocation is necessary | |
285 | * @node: allocation node, used if allocation is necessary | |
286 | * | |
287 | * Return io_context of @task. If it doesn't exist, it is created with | |
288 | * @gfp_flags and @node. The returned io_context has its reference count | |
289 | * incremented. | |
86db1e29 | 290 | * |
6e736be7 | 291 | * This function always goes through task_lock() and it's better to use |
f2dbd76a | 292 | * %current->io_context + get_io_context() for %current. |
86db1e29 | 293 | */ |
6e736be7 TH |
294 | struct io_context *get_task_io_context(struct task_struct *task, |
295 | gfp_t gfp_flags, int node) | |
86db1e29 | 296 | { |
6e736be7 | 297 | struct io_context *ioc; |
86db1e29 | 298 | |
d0164adc | 299 | might_sleep_if(gfpflags_allow_blocking(gfp_flags)); |
6e736be7 | 300 | |
d538ea4c CH |
301 | task_lock(task); |
302 | ioc = task->io_context; | |
303 | if (unlikely(!ioc)) { | |
6e736be7 | 304 | task_unlock(task); |
d538ea4c CH |
305 | return create_task_io_context(task, gfp_flags, node); |
306 | } | |
307 | get_io_context(ioc); | |
308 | task_unlock(task); | |
309 | return ioc; | |
86db1e29 | 310 | } |
86db1e29 | 311 | |
88c9a2ce CH |
312 | int __copy_io(unsigned long clone_flags, struct task_struct *tsk) |
313 | { | |
314 | struct io_context *ioc = current->io_context; | |
88c9a2ce CH |
315 | |
316 | /* | |
317 | * Share io context with parent, if CLONE_IO is set | |
318 | */ | |
319 | if (clone_flags & CLONE_IO) { | |
50569c24 | 320 | atomic_inc(&ioc->active_ref); |
88c9a2ce CH |
321 | tsk->io_context = ioc; |
322 | } else if (ioprio_valid(ioc->ioprio)) { | |
8ffc1368 CH |
323 | tsk->io_context = alloc_io_context(GFP_KERNEL, NUMA_NO_NODE); |
324 | if (!tsk->io_context) | |
88c9a2ce | 325 | return -ENOMEM; |
8ffc1368 | 326 | tsk->io_context->ioprio = ioc->ioprio; |
88c9a2ce CH |
327 | } |
328 | ||
329 | return 0; | |
330 | } | |
331 | ||
47fdd4ca TH |
332 | /** |
333 | * ioc_lookup_icq - lookup io_cq from ioc | |
47fdd4ca TH |
334 | * @q: the associated request_queue |
335 | * | |
336 | * Look up io_cq associated with @ioc - @q pair from @ioc. Must be called | |
337 | * with @q->queue_lock held. | |
338 | */ | |
eca5892a | 339 | struct io_cq *ioc_lookup_icq(struct request_queue *q) |
47fdd4ca | 340 | { |
eca5892a | 341 | struct io_context *ioc = current->io_context; |
47fdd4ca TH |
342 | struct io_cq *icq; |
343 | ||
0d945c1f | 344 | lockdep_assert_held(&q->queue_lock); |
47fdd4ca TH |
345 | |
346 | /* | |
347 | * icq's are indexed from @ioc using radix tree and hint pointer, | |
348 | * both of which are protected with RCU. All removals are done | |
349 | * holding both q and ioc locks, and we're holding q lock - if we | |
350 | * find a icq which points to us, it's guaranteed to be valid. | |
351 | */ | |
352 | rcu_read_lock(); | |
353 | icq = rcu_dereference(ioc->icq_hint); | |
354 | if (icq && icq->q == q) | |
355 | goto out; | |
356 | ||
357 | icq = radix_tree_lookup(&ioc->icq_tree, q->id); | |
358 | if (icq && icq->q == q) | |
359 | rcu_assign_pointer(ioc->icq_hint, icq); /* allowed to race */ | |
360 | else | |
361 | icq = NULL; | |
362 | out: | |
363 | rcu_read_unlock(); | |
364 | return icq; | |
365 | } | |
366 | EXPORT_SYMBOL(ioc_lookup_icq); | |
367 | ||
f1f8cc94 TH |
368 | /** |
369 | * ioc_create_icq - create and link io_cq | |
370 | * @q: request_queue of interest | |
f1f8cc94 | 371 | * |
24acfc34 TH |
372 | * Make sure io_cq linking @ioc and @q exists. If icq doesn't exist, they |
373 | * will be created using @gfp_mask. | |
f1f8cc94 TH |
374 | * |
375 | * The caller is responsible for ensuring @ioc won't go away and @q is | |
376 | * alive and will stay alive until this function returns. | |
377 | */ | |
18b74c4d | 378 | static struct io_cq *ioc_create_icq(struct request_queue *q) |
f1f8cc94 | 379 | { |
18b74c4d | 380 | struct io_context *ioc = current->io_context; |
f1f8cc94 | 381 | struct elevator_type *et = q->elevator->type; |
f1f8cc94 TH |
382 | struct io_cq *icq; |
383 | ||
384 | /* allocate stuff */ | |
18b74c4d | 385 | icq = kmem_cache_alloc_node(et->icq_cache, GFP_ATOMIC | __GFP_ZERO, |
f1f8cc94 TH |
386 | q->node); |
387 | if (!icq) | |
388 | return NULL; | |
389 | ||
18b74c4d | 390 | if (radix_tree_maybe_preload(GFP_ATOMIC) < 0) { |
f1f8cc94 TH |
391 | kmem_cache_free(et->icq_cache, icq); |
392 | return NULL; | |
393 | } | |
394 | ||
395 | icq->ioc = ioc; | |
396 | icq->q = q; | |
397 | INIT_LIST_HEAD(&icq->q_node); | |
398 | INIT_HLIST_NODE(&icq->ioc_node); | |
399 | ||
400 | /* lock both q and ioc and try to link @icq */ | |
0d945c1f | 401 | spin_lock_irq(&q->queue_lock); |
f1f8cc94 TH |
402 | spin_lock(&ioc->lock); |
403 | ||
404 | if (likely(!radix_tree_insert(&ioc->icq_tree, q->id, icq))) { | |
405 | hlist_add_head(&icq->ioc_node, &ioc->icq_list); | |
406 | list_add(&icq->q_node, &q->icq_list); | |
f9cd4bfe JA |
407 | if (et->ops.init_icq) |
408 | et->ops.init_icq(icq); | |
f1f8cc94 TH |
409 | } else { |
410 | kmem_cache_free(et->icq_cache, icq); | |
eca5892a | 411 | icq = ioc_lookup_icq(q); |
f1f8cc94 TH |
412 | if (!icq) |
413 | printk(KERN_ERR "cfq: icq link failed!\n"); | |
414 | } | |
415 | ||
416 | spin_unlock(&ioc->lock); | |
0d945c1f | 417 | spin_unlock_irq(&q->queue_lock); |
f1f8cc94 TH |
418 | radix_tree_preload_end(); |
419 | return icq; | |
420 | } | |
421 | ||
87dd1d63 CH |
422 | struct io_cq *ioc_find_get_icq(struct request_queue *q) |
423 | { | |
d538ea4c CH |
424 | struct io_context *ioc = current->io_context; |
425 | struct io_cq *icq = NULL; | |
87dd1d63 | 426 | |
d538ea4c CH |
427 | if (unlikely(!ioc)) { |
428 | ioc = create_task_io_context(current, GFP_ATOMIC, q->node); | |
429 | if (!ioc) | |
430 | return NULL; | |
431 | } else { | |
432 | get_io_context(ioc); | |
87dd1d63 | 433 | |
d538ea4c | 434 | spin_lock_irq(&q->queue_lock); |
eca5892a | 435 | icq = ioc_lookup_icq(q); |
d538ea4c CH |
436 | spin_unlock_irq(&q->queue_lock); |
437 | } | |
87dd1d63 CH |
438 | |
439 | if (!icq) { | |
18b74c4d | 440 | icq = ioc_create_icq(q); |
d538ea4c CH |
441 | if (!icq) { |
442 | put_io_context(ioc); | |
87dd1d63 | 443 | return NULL; |
d538ea4c | 444 | } |
87dd1d63 | 445 | } |
87dd1d63 CH |
446 | return icq; |
447 | } | |
448 | EXPORT_SYMBOL_GPL(ioc_find_get_icq); | |
449 | ||
13341598 | 450 | static int __init blk_ioc_init(void) |
86db1e29 JA |
451 | { |
452 | iocontext_cachep = kmem_cache_create("blkdev_ioc", | |
453 | sizeof(struct io_context), 0, SLAB_PANIC, NULL); | |
454 | return 0; | |
455 | } | |
456 | subsys_initcall(blk_ioc_init); |