block, cfq: restructure io_cq creation path for io_context interface cleanup
[deliverable/linux.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>
9#include <linux/bootmem.h> /* for max_pfn/max_low_pfn */
5a0e3ad6 10#include <linux/slab.h>
86db1e29
JA
11
12#include "blk.h"
13
14/*
15 * For io context allocations
16 */
17static struct kmem_cache *iocontext_cachep;
18
6e736be7
TH
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 */
25void get_io_context(struct io_context *ioc)
26{
27 BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
28 atomic_long_inc(&ioc->refcount);
29}
30EXPORT_SYMBOL(get_io_context);
31
b2efa052
TH
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
7e5a8794
TH
47static 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 */
57static 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
b2efa052
TH
92/*
93 * Slow path for ioc release in put_io_context(). Performs double-lock
c5869807 94 * dancing to unlink all icq's and then frees ioc.
b2efa052
TH
95 */
96static void ioc_release_fn(struct work_struct *work)
86db1e29 97{
b2efa052
TH
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
c5869807
TH
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;
b2efa052
TH
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 }
7e5a8794 135 ioc_exit_icq(icq);
b2efa052 136 }
ffc4e759 137
b2efa052
TH
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);
ffc4e759 144 }
b2efa052
TH
145
146 kmem_cache_free(iocontext_cachep, ioc);
86db1e29
JA
147}
148
42ec57a8
TH
149/**
150 * put_io_context - put a reference of io_context
151 * @ioc: io_context to put
b2efa052 152 * @locked_q: request_queue the caller is holding queue_lock of (hint)
42ec57a8
TH
153 *
154 * Decrement reference count of @ioc and release it if the count reaches
b2efa052
TH
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.
86db1e29 158 */
b2efa052 159void put_io_context(struct io_context *ioc, struct request_queue *locked_q)
86db1e29 160{
b2efa052
TH
161 struct request_queue *last_q = locked_q;
162 unsigned long flags;
163
86db1e29 164 if (ioc == NULL)
42ec57a8 165 return;
86db1e29 166
42ec57a8 167 BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
b2efa052
TH
168 if (locked_q)
169 lockdep_assert_held(locked_q->queue_lock);
86db1e29 170
42ec57a8
TH
171 if (!atomic_long_dec_and_test(&ioc->refcount))
172 return;
86db1e29 173
b2efa052 174 /*
c5869807 175 * Destroy @ioc. This is a bit messy because icq's are chained
b2efa052 176 * from both ioc and queue, and ioc->lock nests inside queue_lock.
c5869807
TH
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.
b2efa052
TH
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
c5869807
TH
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;
b2efa052
TH
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 if (!spin_trylock(this_q->queue_lock))
208 break;
209 last_q = this_q;
210 continue;
211 }
7e5a8794 212 ioc_exit_icq(icq);
b2efa052 213 }
86db1e29 214
b2efa052
TH
215 if (last_q && last_q != locked_q)
216 spin_unlock(last_q->queue_lock);
86db1e29 217
b2efa052 218 spin_unlock_irqrestore(&ioc->lock, flags);
ffc4e759 219
c5869807
TH
220 /* if no icq is left, we're done; otherwise, kick release_work */
221 if (hlist_empty(&ioc->icq_list))
b2efa052
TH
222 kmem_cache_free(iocontext_cachep, ioc);
223 else
224 schedule_work(&ioc->release_work);
86db1e29 225}
b2efa052 226EXPORT_SYMBOL(put_io_context);
86db1e29 227
27667c99 228/* Called by the exiting task */
b69f2292 229void exit_io_context(struct task_struct *task)
86db1e29
JA
230{
231 struct io_context *ioc;
232
6e736be7
TH
233 /* PF_EXITING prevents new io_context from being attached to @task */
234 WARN_ON_ONCE(!(current->flags & PF_EXITING));
235
b69f2292
LR
236 task_lock(task);
237 ioc = task->io_context;
238 task->io_context = NULL;
239 task_unlock(task);
86db1e29 240
b2efa052
TH
241 atomic_dec(&ioc->nr_tasks);
242 put_io_context(ioc, NULL);
86db1e29
JA
243}
244
7e5a8794
TH
245/**
246 * ioc_clear_queue - break any ioc association with the specified queue
247 * @q: request_queue being cleared
248 *
249 * Walk @q->icq_list and exit all io_cq's. Must be called with @q locked.
250 */
251void ioc_clear_queue(struct request_queue *q)
252{
253 lockdep_assert_held(q->queue_lock);
254
255 while (!list_empty(&q->icq_list)) {
256 struct io_cq *icq = list_entry(q->icq_list.next,
257 struct io_cq, q_node);
258 struct io_context *ioc = icq->ioc;
259
260 spin_lock(&ioc->lock);
261 ioc_exit_icq(icq);
262 spin_unlock(&ioc->lock);
263 }
264}
265
f2dbd76a
TH
266void create_io_context_slowpath(struct task_struct *task, gfp_t gfp_flags,
267 int node)
86db1e29 268{
df415656 269 struct io_context *ioc;
86db1e29 270
42ec57a8
TH
271 ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO,
272 node);
273 if (unlikely(!ioc))
f2dbd76a 274 return;
42ec57a8
TH
275
276 /* initialize */
277 atomic_long_set(&ioc->refcount, 1);
278 atomic_set(&ioc->nr_tasks, 1);
279 spin_lock_init(&ioc->lock);
c5869807
TH
280 INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC | __GFP_HIGH);
281 INIT_HLIST_HEAD(&ioc->icq_list);
b2efa052 282 INIT_WORK(&ioc->release_work, ioc_release_fn);
86db1e29 283
6e736be7
TH
284 /* try to install, somebody might already have beaten us to it */
285 task_lock(task);
f2dbd76a 286 if (!task->io_context && !(task->flags & PF_EXITING))
6e736be7 287 task->io_context = ioc;
f2dbd76a 288 else
6e736be7 289 kmem_cache_free(iocontext_cachep, ioc);
6e736be7 290 task_unlock(task);
86db1e29 291}
f2dbd76a 292EXPORT_SYMBOL(create_io_context_slowpath);
86db1e29 293
6e736be7
TH
294/**
295 * get_task_io_context - get io_context of a task
296 * @task: task of interest
297 * @gfp_flags: allocation flags, used if allocation is necessary
298 * @node: allocation node, used if allocation is necessary
299 *
300 * Return io_context of @task. If it doesn't exist, it is created with
301 * @gfp_flags and @node. The returned io_context has its reference count
302 * incremented.
86db1e29 303 *
6e736be7 304 * This function always goes through task_lock() and it's better to use
f2dbd76a 305 * %current->io_context + get_io_context() for %current.
86db1e29 306 */
6e736be7
TH
307struct io_context *get_task_io_context(struct task_struct *task,
308 gfp_t gfp_flags, int node)
86db1e29 309{
6e736be7 310 struct io_context *ioc;
86db1e29 311
6e736be7
TH
312 might_sleep_if(gfp_flags & __GFP_WAIT);
313
f2dbd76a
TH
314 do {
315 task_lock(task);
316 ioc = task->io_context;
317 if (likely(ioc)) {
318 get_io_context(ioc);
319 task_unlock(task);
320 return ioc;
321 }
6e736be7 322 task_unlock(task);
f2dbd76a 323 } while (create_io_context(task, gfp_flags, node));
6e736be7 324
f2dbd76a 325 return NULL;
86db1e29 326}
6e736be7 327EXPORT_SYMBOL(get_task_io_context);
86db1e29 328
47fdd4ca
TH
329/**
330 * ioc_lookup_icq - lookup io_cq from ioc
331 * @ioc: the associated io_context
332 * @q: the associated request_queue
333 *
334 * Look up io_cq associated with @ioc - @q pair from @ioc. Must be called
335 * with @q->queue_lock held.
336 */
337struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q)
338{
339 struct io_cq *icq;
340
341 lockdep_assert_held(q->queue_lock);
342
343 /*
344 * icq's are indexed from @ioc using radix tree and hint pointer,
345 * both of which are protected with RCU. All removals are done
346 * holding both q and ioc locks, and we're holding q lock - if we
347 * find a icq which points to us, it's guaranteed to be valid.
348 */
349 rcu_read_lock();
350 icq = rcu_dereference(ioc->icq_hint);
351 if (icq && icq->q == q)
352 goto out;
353
354 icq = radix_tree_lookup(&ioc->icq_tree, q->id);
355 if (icq && icq->q == q)
356 rcu_assign_pointer(ioc->icq_hint, icq); /* allowed to race */
357 else
358 icq = NULL;
359out:
360 rcu_read_unlock();
361 return icq;
362}
363EXPORT_SYMBOL(ioc_lookup_icq);
364
dc86900e
TH
365void ioc_set_changed(struct io_context *ioc, int which)
366{
c5869807 367 struct io_cq *icq;
dc86900e
TH
368 struct hlist_node *n;
369
c5869807
TH
370 hlist_for_each_entry(icq, n, &ioc->icq_list, ioc_node)
371 set_bit(which, &icq->changed);
dc86900e
TH
372}
373
374/**
375 * ioc_ioprio_changed - notify ioprio change
376 * @ioc: io_context of interest
377 * @ioprio: new ioprio
378 *
c5869807
TH
379 * @ioc's ioprio has changed to @ioprio. Set %ICQ_IOPRIO_CHANGED for all
380 * icq's. iosched is responsible for checking the bit and applying it on
dc86900e
TH
381 * request issue path.
382 */
383void ioc_ioprio_changed(struct io_context *ioc, int ioprio)
384{
385 unsigned long flags;
386
387 spin_lock_irqsave(&ioc->lock, flags);
388 ioc->ioprio = ioprio;
c5869807 389 ioc_set_changed(ioc, ICQ_IOPRIO_CHANGED);
dc86900e
TH
390 spin_unlock_irqrestore(&ioc->lock, flags);
391}
392
393/**
394 * ioc_cgroup_changed - notify cgroup change
395 * @ioc: io_context of interest
396 *
c5869807 397 * @ioc's cgroup has changed. Set %ICQ_CGROUP_CHANGED for all icq's.
dc86900e
TH
398 * iosched is responsible for checking the bit and applying it on request
399 * issue path.
400 */
401void ioc_cgroup_changed(struct io_context *ioc)
402{
403 unsigned long flags;
404
405 spin_lock_irqsave(&ioc->lock, flags);
c5869807 406 ioc_set_changed(ioc, ICQ_CGROUP_CHANGED);
dc86900e
TH
407 spin_unlock_irqrestore(&ioc->lock, flags);
408}
409
13341598 410static int __init blk_ioc_init(void)
86db1e29
JA
411{
412 iocontext_cachep = kmem_cache_create("blkdev_ioc",
413 sizeof(struct io_context), 0, SLAB_PANIC, NULL);
414 return 0;
415}
416subsys_initcall(blk_ioc_init);
This page took 0.254835 seconds and 5 git commands to generate.