]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - block/blk-ioc.c
blk-mq: ensure that bd->last is always set correctly
[mirror_ubuntu-artful-kernel.git] / block / blk-ioc.c
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>
9 #include <linux/slab.h>
10
11 #include "blk.h"
12
13 /*
14 * For io context allocations
15 */
16 static struct kmem_cache *iocontext_cachep;
17
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 */
24 void get_io_context(struct io_context *ioc)
25 {
26 BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
27 atomic_long_inc(&ioc->refcount);
28 }
29 EXPORT_SYMBOL(get_io_context);
30
31 static 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
38 /*
39 * Exit an icq. Called with ioc locked for blk-mq, and with both ioc
40 * and queue locked for legacy.
41 */
42 static void ioc_exit_icq(struct io_cq *icq)
43 {
44 struct elevator_type *et = icq->q->elevator->type;
45
46 if (icq->flags & ICQ_EXITED)
47 return;
48
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)
52 et->ops.sq.elevator_exit_icq_fn(icq);
53
54 icq->flags |= ICQ_EXITED;
55 }
56
57 /*
58 * Release an icq. Called with ioc locked for blk-mq, and with both ioc
59 * and queue locked for legacy.
60 */
61 static void ioc_destroy_icq(struct io_cq *icq)
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);
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 */
78 if (rcu_access_pointer(ioc->icq_hint) == icq)
79 rcu_assign_pointer(ioc->icq_hint, NULL);
80
81 ioc_exit_icq(icq);
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
91 /*
92 * Slow path for ioc release in put_io_context(). Performs double-lock
93 * dancing to unlink all icq's and then frees ioc.
94 */
95 static void ioc_release_fn(struct work_struct *work)
96 {
97 struct io_context *ioc = container_of(work, struct io_context,
98 release_work);
99 unsigned long flags;
100
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);
108
109 while (!hlist_empty(&ioc->icq_list)) {
110 struct io_cq *icq = hlist_entry(ioc->icq_list.first,
111 struct io_cq, ioc_node);
112 struct request_queue *q = icq->q;
113
114 if (spin_trylock(q->queue_lock)) {
115 ioc_destroy_icq(icq);
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);
121 }
122 }
123
124 spin_unlock_irqrestore(&ioc->lock, flags);
125
126 kmem_cache_free(iocontext_cachep, ioc);
127 }
128
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
134 * zero.
135 */
136 void put_io_context(struct io_context *ioc)
137 {
138 unsigned long flags;
139 bool free_ioc = false;
140
141 if (ioc == NULL)
142 return;
143
144 BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
145
146 /*
147 * Releasing ioc requires reverse order double locking and we may
148 * already be holding a queue_lock. Do it asynchronously from wq.
149 */
150 if (atomic_long_dec_and_test(&ioc->refcount)) {
151 spin_lock_irqsave(&ioc->lock, flags);
152 if (!hlist_empty(&ioc->icq_list))
153 queue_work(system_power_efficient_wq,
154 &ioc->release_work);
155 else
156 free_ioc = true;
157 spin_unlock_irqrestore(&ioc->lock, flags);
158 }
159
160 if (free_ioc)
161 kmem_cache_free(iocontext_cachep, ioc);
162 }
163 EXPORT_SYMBOL(put_io_context);
164
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 */
172 void put_io_context_active(struct io_context *ioc)
173 {
174 struct elevator_type *et;
175 unsigned long flags;
176 struct io_cq *icq;
177
178 if (!atomic_dec_and_test(&ioc->active_ref)) {
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 */
188 retry:
189 spin_lock_irqsave_nested(&ioc->lock, flags, 1);
190 hlist_for_each_entry(icq, &ioc->icq_list, ioc_node) {
191 if (icq->flags & ICQ_EXITED)
192 continue;
193
194 et = icq->q->elevator->type;
195 if (et->uses_mq) {
196 ioc_exit_icq(icq);
197 } else {
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 }
206 }
207 }
208 spin_unlock_irqrestore(&ioc->lock, flags);
209
210 put_io_context(ioc);
211 }
212
213 /* Called by the exiting task */
214 void 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
227 static 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
242 /**
243 * ioc_clear_queue - break any ioc association with the specified queue
244 * @q: request_queue being cleared
245 *
246 * Walk @q->icq_list and exit all io_cq's.
247 */
248 void ioc_clear_queue(struct request_queue *q)
249 {
250 LIST_HEAD(icq_list);
251
252 spin_lock_irq(q->queue_lock);
253 list_splice_init(&q->icq_list, &icq_list);
254
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);
261 }
262 }
263
264 int create_task_io_context(struct task_struct *task, gfp_t gfp_flags, int node)
265 {
266 struct io_context *ioc;
267 int ret;
268
269 ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO,
270 node);
271 if (unlikely(!ioc))
272 return -ENOMEM;
273
274 /* initialize */
275 atomic_long_set(&ioc->refcount, 1);
276 atomic_set(&ioc->nr_tasks, 1);
277 atomic_set(&ioc->active_ref, 1);
278 spin_lock_init(&ioc->lock);
279 INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC | __GFP_HIGH);
280 INIT_HLIST_HEAD(&ioc->icq_list);
281 INIT_WORK(&ioc->release_work, ioc_release_fn);
282
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 */
290 task_lock(task);
291 if (!task->io_context &&
292 (task == current || !(task->flags & PF_EXITING)))
293 task->io_context = ioc;
294 else
295 kmem_cache_free(iocontext_cachep, ioc);
296
297 ret = task->io_context ? 0 : -EBUSY;
298
299 task_unlock(task);
300
301 return ret;
302 }
303
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.
313 *
314 * This function always goes through task_lock() and it's better to use
315 * %current->io_context + get_io_context() for %current.
316 */
317 struct io_context *get_task_io_context(struct task_struct *task,
318 gfp_t gfp_flags, int node)
319 {
320 struct io_context *ioc;
321
322 might_sleep_if(gfpflags_allow_blocking(gfp_flags));
323
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 }
332 task_unlock(task);
333 } while (!create_task_io_context(task, gfp_flags, node));
334
335 return NULL;
336 }
337 EXPORT_SYMBOL(get_task_io_context);
338
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 */
347 struct 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;
369 out:
370 rcu_read_unlock();
371 return icq;
372 }
373 EXPORT_SYMBOL(ioc_lookup_icq);
374
375 /**
376 * ioc_create_icq - create and link io_cq
377 * @ioc: io_context of interest
378 * @q: request_queue of interest
379 * @gfp_mask: allocation mask
380 *
381 * Make sure io_cq linking @ioc and @q exists. If icq doesn't exist, they
382 * will be created using @gfp_mask.
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 */
387 struct io_cq *ioc_create_icq(struct io_context *ioc, struct request_queue *q,
388 gfp_t gfp_mask)
389 {
390 struct elevator_type *et = q->elevator->type;
391 struct io_cq *icq;
392
393 /* allocate stuff */
394 icq = kmem_cache_alloc_node(et->icq_cache, gfp_mask | __GFP_ZERO,
395 q->node);
396 if (!icq)
397 return NULL;
398
399 if (radix_tree_maybe_preload(gfp_mask) < 0) {
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);
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)
419 et->ops.sq.elevator_init_icq_fn(icq);
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
433 static int __init blk_ioc_init(void)
434 {
435 iocontext_cachep = kmem_cache_create("blkdev_ioc",
436 sizeof(struct io_context), 0, SLAB_PANIC, NULL);
437 return 0;
438 }
439 subsys_initcall(blk_ioc_init);