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