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