7490b6da24539d84c646901d7ff9b4e905bbbec2
[linux-flexiantxendom0-3.2.10.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/bootmem.h>      /* for max_pfn/max_low_pfn */
10 #include <linux/slab.h>
11
12 #include "blk.h"
13
14 /*
15  * For io context allocations
16  */
17 static struct kmem_cache *iocontext_cachep;
18
19 /**
20  * get_io_context - increment reference count to io_context
21  * @ioc: io_context to get
22  *
23  * Increment reference count to @ioc.
24  */
25 void get_io_context(struct io_context *ioc)
26 {
27         BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
28         atomic_long_inc(&ioc->refcount);
29 }
30 EXPORT_SYMBOL(get_io_context);
31
32 /*
33  * Releasing ioc may nest into another put_io_context() leading to nested
34  * fast path release.  As the ioc's can't be the same, this is okay but
35  * makes lockdep whine.  Keep track of nesting and use it as subclass.
36  */
37 #ifdef CONFIG_LOCKDEP
38 #define ioc_release_depth(q)            ((q) ? (q)->ioc_release_depth : 0)
39 #define ioc_release_depth_inc(q)        (q)->ioc_release_depth++
40 #define ioc_release_depth_dec(q)        (q)->ioc_release_depth--
41 #else
42 #define ioc_release_depth(q)            0
43 #define ioc_release_depth_inc(q)        do { } while (0)
44 #define ioc_release_depth_dec(q)        do { } while (0)
45 #endif
46
47 static void icq_free_icq_rcu(struct rcu_head *head)
48 {
49         struct io_cq *icq = container_of(head, struct io_cq, __rcu_head);
50
51         kmem_cache_free(icq->__rcu_icq_cache, icq);
52 }
53
54 /*
55  * Exit and free an icq.  Called with both ioc and q locked.
56  */
57 static void ioc_exit_icq(struct io_cq *icq)
58 {
59         struct io_context *ioc = icq->ioc;
60         struct request_queue *q = icq->q;
61         struct elevator_type *et = q->elevator->type;
62
63         lockdep_assert_held(&ioc->lock);
64         lockdep_assert_held(q->queue_lock);
65
66         radix_tree_delete(&ioc->icq_tree, icq->q->id);
67         hlist_del_init(&icq->ioc_node);
68         list_del_init(&icq->q_node);
69
70         /*
71          * Both setting lookup hint to and clearing it from @icq are done
72          * under queue_lock.  If it's not pointing to @icq now, it never
73          * will.  Hint assignment itself can race safely.
74          */
75         if (rcu_dereference_raw(ioc->icq_hint) == icq)
76                 rcu_assign_pointer(ioc->icq_hint, NULL);
77
78         if (et->ops.elevator_exit_icq_fn) {
79                 ioc_release_depth_inc(q);
80                 et->ops.elevator_exit_icq_fn(icq);
81                 ioc_release_depth_dec(q);
82         }
83
84         /*
85          * @icq->q might have gone away by the time RCU callback runs
86          * making it impossible to determine icq_cache.  Record it in @icq.
87          */
88         icq->__rcu_icq_cache = et->icq_cache;
89         call_rcu(&icq->__rcu_head, icq_free_icq_rcu);
90 }
91
92 /*
93  * Slow path for ioc release in put_io_context().  Performs double-lock
94  * dancing to unlink all icq's and then frees ioc.
95  */
96 static void ioc_release_fn(struct work_struct *work)
97 {
98         struct io_context *ioc = container_of(work, struct io_context,
99                                               release_work);
100         struct request_queue *last_q = NULL;
101
102         spin_lock_irq(&ioc->lock);
103
104         while (!hlist_empty(&ioc->icq_list)) {
105                 struct io_cq *icq = hlist_entry(ioc->icq_list.first,
106                                                 struct io_cq, ioc_node);
107                 struct request_queue *this_q = icq->q;
108
109                 if (this_q != last_q) {
110                         /*
111                          * Need to switch to @this_q.  Once we release
112                          * @ioc->lock, it can go away along with @cic.
113                          * Hold on to it.
114                          */
115                         __blk_get_queue(this_q);
116
117                         /*
118                          * blk_put_queue() might sleep thanks to kobject
119                          * idiocy.  Always release both locks, put and
120                          * restart.
121                          */
122                         if (last_q) {
123                                 spin_unlock(last_q->queue_lock);
124                                 spin_unlock_irq(&ioc->lock);
125                                 blk_put_queue(last_q);
126                         } else {
127                                 spin_unlock_irq(&ioc->lock);
128                         }
129
130                         last_q = this_q;
131                         spin_lock_irq(this_q->queue_lock);
132                         spin_lock(&ioc->lock);
133                         continue;
134                 }
135                 ioc_exit_icq(icq);
136         }
137
138         if (last_q) {
139                 spin_unlock(last_q->queue_lock);
140                 spin_unlock_irq(&ioc->lock);
141                 blk_put_queue(last_q);
142         } else {
143                 spin_unlock_irq(&ioc->lock);
144         }
145
146         kmem_cache_free(iocontext_cachep, ioc);
147 }
148
149 /**
150  * put_io_context - put a reference of io_context
151  * @ioc: io_context to put
152  * @locked_q: request_queue the caller is holding queue_lock of (hint)
153  *
154  * Decrement reference count of @ioc and release it if the count reaches
155  * zero.  If the caller is holding queue_lock of a queue, it can indicate
156  * that with @locked_q.  This is an optimization hint and the caller is
157  * allowed to pass in %NULL even when it's holding a queue_lock.
158  */
159 void put_io_context(struct io_context *ioc, struct request_queue *locked_q)
160 {
161         struct request_queue *last_q = locked_q;
162         unsigned long flags;
163
164         if (ioc == NULL)
165                 return;
166
167         BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
168         if (locked_q)
169                 lockdep_assert_held(locked_q->queue_lock);
170
171         if (!atomic_long_dec_and_test(&ioc->refcount))
172                 return;
173
174         /*
175          * Destroy @ioc.  This is a bit messy because icq's are chained
176          * from both ioc and queue, and ioc->lock nests inside queue_lock.
177          * The inner ioc->lock should be held to walk our icq_list and then
178          * for each icq the outer matching queue_lock should be grabbed.
179          * ie. We need to do reverse-order double lock dancing.
180          *
181          * Another twist is that we are often called with one of the
182          * matching queue_locks held as indicated by @locked_q, which
183          * prevents performing double-lock dance for other queues.
184          *
185          * So, we do it in two stages.  The fast path uses the queue_lock
186          * the caller is holding and, if other queues need to be accessed,
187          * uses trylock to avoid introducing locking dependency.  This can
188          * handle most cases, especially if @ioc was performing IO on only
189          * single device.
190          *
191          * If trylock doesn't cut it, we defer to @ioc->release_work which
192          * can do all the double-locking dancing.
193          */
194         spin_lock_irqsave_nested(&ioc->lock, flags,
195                                  ioc_release_depth(locked_q));
196
197         while (!hlist_empty(&ioc->icq_list)) {
198                 struct io_cq *icq = hlist_entry(ioc->icq_list.first,
199                                                 struct io_cq, ioc_node);
200                 struct request_queue *this_q = icq->q;
201
202                 if (this_q != last_q) {
203                         if (last_q && last_q != locked_q)
204                                 spin_unlock(last_q->queue_lock);
205                         last_q = NULL;
206
207                         /* spin_trylock() always successes in UP case */
208                         if (this_q != locked_q &&
209                             !spin_trylock(this_q->queue_lock))
210                                 break;
211                         last_q = this_q;
212                         continue;
213                 }
214                 ioc_exit_icq(icq);
215         }
216
217         if (last_q && last_q != locked_q)
218                 spin_unlock(last_q->queue_lock);
219
220         spin_unlock_irqrestore(&ioc->lock, flags);
221
222         /* if no icq is left, we're done; otherwise, kick release_work */
223         if (hlist_empty(&ioc->icq_list))
224                 kmem_cache_free(iocontext_cachep, ioc);
225         else
226                 schedule_work(&ioc->release_work);
227 }
228 EXPORT_SYMBOL(put_io_context);
229
230 /* Called by the exiting task */
231 void exit_io_context(struct task_struct *task)
232 {
233         struct io_context *ioc;
234
235         task_lock(task);
236         ioc = task->io_context;
237         task->io_context = NULL;
238         task_unlock(task);
239
240         atomic_dec(&ioc->nr_tasks);
241         put_io_context(ioc, NULL);
242 }
243
244 /**
245  * ioc_clear_queue - break any ioc association with the specified queue
246  * @q: request_queue being cleared
247  *
248  * Walk @q->icq_list and exit all io_cq's.  Must be called with @q locked.
249  */
250 void ioc_clear_queue(struct request_queue *q)
251 {
252         lockdep_assert_held(q->queue_lock);
253
254         while (!list_empty(&q->icq_list)) {
255                 struct io_cq *icq = list_entry(q->icq_list.next,
256                                                struct io_cq, q_node);
257                 struct io_context *ioc = icq->ioc;
258
259                 spin_lock(&ioc->lock);
260                 ioc_exit_icq(icq);
261                 spin_unlock(&ioc->lock);
262         }
263 }
264
265 void create_io_context_slowpath(struct task_struct *task, gfp_t gfp_flags,
266                                 int node)
267 {
268         struct io_context *ioc;
269
270         ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO,
271                                     node);
272         if (unlikely(!ioc))
273                 return;
274
275         /* initialize */
276         atomic_long_set(&ioc->refcount, 1);
277         atomic_set(&ioc->nr_tasks, 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         task_unlock(task);
297 }
298
299 /**
300  * get_task_io_context - get io_context of a task
301  * @task: task of interest
302  * @gfp_flags: allocation flags, used if allocation is necessary
303  * @node: allocation node, used if allocation is necessary
304  *
305  * Return io_context of @task.  If it doesn't exist, it is created with
306  * @gfp_flags and @node.  The returned io_context has its reference count
307  * incremented.
308  *
309  * This function always goes through task_lock() and it's better to use
310  * %current->io_context + get_io_context() for %current.
311  */
312 struct io_context *get_task_io_context(struct task_struct *task,
313                                        gfp_t gfp_flags, int node)
314 {
315         struct io_context *ioc;
316
317         might_sleep_if(gfp_flags & __GFP_WAIT);
318
319         do {
320                 task_lock(task);
321                 ioc = task->io_context;
322                 if (likely(ioc)) {
323                         get_io_context(ioc);
324                         task_unlock(task);
325                         return ioc;
326                 }
327                 task_unlock(task);
328         } while (create_io_context(task, gfp_flags, node));
329
330         return NULL;
331 }
332 EXPORT_SYMBOL(get_task_io_context);
333
334 /**
335  * ioc_lookup_icq - lookup io_cq from ioc
336  * @ioc: the associated io_context
337  * @q: the associated request_queue
338  *
339  * Look up io_cq associated with @ioc - @q pair from @ioc.  Must be called
340  * with @q->queue_lock held.
341  */
342 struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q)
343 {
344         struct io_cq *icq;
345
346         lockdep_assert_held(q->queue_lock);
347
348         /*
349          * icq's are indexed from @ioc using radix tree and hint pointer,
350          * both of which are protected with RCU.  All removals are done
351          * holding both q and ioc locks, and we're holding q lock - if we
352          * find a icq which points to us, it's guaranteed to be valid.
353          */
354         rcu_read_lock();
355         icq = rcu_dereference(ioc->icq_hint);
356         if (icq && icq->q == q)
357                 goto out;
358
359         icq = radix_tree_lookup(&ioc->icq_tree, q->id);
360         if (icq && icq->q == q)
361                 rcu_assign_pointer(ioc->icq_hint, icq); /* allowed to race */
362         else
363                 icq = NULL;
364 out:
365         rcu_read_unlock();
366         return icq;
367 }
368 EXPORT_SYMBOL(ioc_lookup_icq);
369
370 /**
371  * ioc_create_icq - create and link io_cq
372  * @q: request_queue of interest
373  * @gfp_mask: allocation mask
374  *
375  * Make sure io_cq linking %current->io_context and @q exists.  If either
376  * io_context and/or icq don't exist, they will be created using @gfp_mask.
377  *
378  * The caller is responsible for ensuring @ioc won't go away and @q is
379  * alive and will stay alive until this function returns.
380  */
381 struct io_cq *ioc_create_icq(struct request_queue *q, gfp_t gfp_mask)
382 {
383         struct elevator_type *et = q->elevator->type;
384         struct io_context *ioc;
385         struct io_cq *icq;
386
387         /* allocate stuff */
388         ioc = create_io_context(current, gfp_mask, q->node);
389         if (!ioc)
390                 return NULL;
391
392         icq = kmem_cache_alloc_node(et->icq_cache, gfp_mask | __GFP_ZERO,
393                                     q->node);
394         if (!icq)
395                 return NULL;
396
397         if (radix_tree_preload(gfp_mask) < 0) {
398                 kmem_cache_free(et->icq_cache, icq);
399                 return NULL;
400         }
401
402         icq->ioc = ioc;
403         icq->q = q;
404         INIT_LIST_HEAD(&icq->q_node);
405         INIT_HLIST_NODE(&icq->ioc_node);
406
407         /* lock both q and ioc and try to link @icq */
408         spin_lock_irq(q->queue_lock);
409         spin_lock(&ioc->lock);
410
411         if (likely(!radix_tree_insert(&ioc->icq_tree, q->id, icq))) {
412                 hlist_add_head(&icq->ioc_node, &ioc->icq_list);
413                 list_add(&icq->q_node, &q->icq_list);
414                 if (et->ops.elevator_init_icq_fn)
415                         et->ops.elevator_init_icq_fn(icq);
416         } else {
417                 kmem_cache_free(et->icq_cache, icq);
418                 icq = ioc_lookup_icq(ioc, q);
419                 if (!icq)
420                         printk(KERN_ERR "cfq: icq link failed!\n");
421         }
422
423         spin_unlock(&ioc->lock);
424         spin_unlock_irq(q->queue_lock);
425         radix_tree_preload_end();
426         return icq;
427 }
428
429 void ioc_set_changed(struct io_context *ioc, int which)
430 {
431         struct io_cq *icq;
432         struct hlist_node *n;
433
434         hlist_for_each_entry(icq, n, &ioc->icq_list, ioc_node)
435                 set_bit(which, &icq->changed);
436 }
437
438 /**
439  * ioc_ioprio_changed - notify ioprio change
440  * @ioc: io_context of interest
441  * @ioprio: new ioprio
442  *
443  * @ioc's ioprio has changed to @ioprio.  Set %ICQ_IOPRIO_CHANGED for all
444  * icq's.  iosched is responsible for checking the bit and applying it on
445  * request issue path.
446  */
447 void ioc_ioprio_changed(struct io_context *ioc, int ioprio)
448 {
449         unsigned long flags;
450
451         spin_lock_irqsave(&ioc->lock, flags);
452         ioc->ioprio = ioprio;
453         ioc_set_changed(ioc, ICQ_IOPRIO_CHANGED);
454         spin_unlock_irqrestore(&ioc->lock, flags);
455 }
456
457 /**
458  * ioc_cgroup_changed - notify cgroup change
459  * @ioc: io_context of interest
460  *
461  * @ioc's cgroup has changed.  Set %ICQ_CGROUP_CHANGED for all icq's.
462  * iosched is responsible for checking the bit and applying it on request
463  * issue path.
464  */
465 void ioc_cgroup_changed(struct io_context *ioc)
466 {
467         unsigned long flags;
468
469         spin_lock_irqsave(&ioc->lock, flags);
470         ioc_set_changed(ioc, ICQ_CGROUP_CHANGED);
471         spin_unlock_irqrestore(&ioc->lock, flags);
472 }
473 EXPORT_SYMBOL(ioc_cgroup_changed);
474
475 static int __init blk_ioc_init(void)
476 {
477         iocontext_cachep = kmem_cache_create("blkdev_ioc",
478                         sizeof(struct io_context), 0, SLAB_PANIC, NULL);
479         return 0;
480 }
481 subsys_initcall(blk_ioc_init);