2 * Functions related to io context handling
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/init.h>
8 #include <linux/blkdev.h>
9 #include <linux/bootmem.h> /* for max_pfn/max_low_pfn */
10 #include <linux/slab.h>
15 * For io context allocations
17 static struct kmem_cache
*iocontext_cachep
;
20 * get_io_context - increment reference count to io_context
21 * @ioc: io_context to get
23 * Increment reference count to @ioc.
25 void get_io_context(struct io_context
*ioc
)
27 BUG_ON(atomic_long_read(&ioc
->refcount
) <= 0);
28 atomic_long_inc(&ioc
->refcount
);
30 EXPORT_SYMBOL(get_io_context
);
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.
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--
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)
47 static void icq_free_icq_rcu(struct rcu_head
*head
)
49 struct io_cq
*icq
= container_of(head
, struct io_cq
, __rcu_head
);
51 kmem_cache_free(icq
->__rcu_icq_cache
, icq
);
55 * Exit and free an icq. Called with both ioc and q locked.
57 static void ioc_exit_icq(struct io_cq
*icq
)
59 struct io_context
*ioc
= icq
->ioc
;
60 struct request_queue
*q
= icq
->q
;
61 struct elevator_type
*et
= q
->elevator
->type
;
63 lockdep_assert_held(&ioc
->lock
);
64 lockdep_assert_held(q
->queue_lock
);
66 radix_tree_delete(&ioc
->icq_tree
, icq
->q
->id
);
67 hlist_del_init(&icq
->ioc_node
);
68 list_del_init(&icq
->q_node
);
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.
75 if (rcu_dereference_raw(ioc
->icq_hint
) == icq
)
76 rcu_assign_pointer(ioc
->icq_hint
, NULL
);
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
);
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.
88 icq
->__rcu_icq_cache
= et
->icq_cache
;
89 call_rcu(&icq
->__rcu_head
, icq_free_icq_rcu
);
93 * Slow path for ioc release in put_io_context(). Performs double-lock
94 * dancing to unlink all icq's and then frees ioc.
96 static void ioc_release_fn(struct work_struct
*work
)
98 struct io_context
*ioc
= container_of(work
, struct io_context
,
100 struct request_queue
*last_q
= NULL
;
102 spin_lock_irq(&ioc
->lock
);
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
;
109 if (this_q
!= last_q
) {
111 * Need to switch to @this_q. Once we release
112 * @ioc->lock, it can go away along with @cic.
115 __blk_get_queue(this_q
);
118 * blk_put_queue() might sleep thanks to kobject
119 * idiocy. Always release both locks, put and
123 spin_unlock(last_q
->queue_lock
);
124 spin_unlock_irq(&ioc
->lock
);
125 blk_put_queue(last_q
);
127 spin_unlock_irq(&ioc
->lock
);
131 spin_lock_irq(this_q
->queue_lock
);
132 spin_lock(&ioc
->lock
);
139 spin_unlock(last_q
->queue_lock
);
140 spin_unlock_irq(&ioc
->lock
);
141 blk_put_queue(last_q
);
143 spin_unlock_irq(&ioc
->lock
);
146 kmem_cache_free(iocontext_cachep
, ioc
);
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)
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.
159 void put_io_context(struct io_context
*ioc
, struct request_queue
*locked_q
)
161 struct request_queue
*last_q
= locked_q
;
167 BUG_ON(atomic_long_read(&ioc
->refcount
) <= 0);
169 lockdep_assert_held(locked_q
->queue_lock
);
171 if (!atomic_long_dec_and_test(&ioc
->refcount
))
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.
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.
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
191 * If trylock doesn't cut it, we defer to @ioc->release_work which
192 * can do all the double-locking dancing.
194 spin_lock_irqsave_nested(&ioc
->lock
, flags
,
195 ioc_release_depth(locked_q
));
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
;
202 if (this_q
!= last_q
) {
203 if (last_q
&& last_q
!= locked_q
)
204 spin_unlock(last_q
->queue_lock
);
207 if (!spin_trylock(this_q
->queue_lock
))
215 if (last_q
&& last_q
!= locked_q
)
216 spin_unlock(last_q
->queue_lock
);
218 spin_unlock_irqrestore(&ioc
->lock
, flags
);
220 /* if no icq is left, we're done; otherwise, kick release_work */
221 if (hlist_empty(&ioc
->icq_list
))
222 kmem_cache_free(iocontext_cachep
, ioc
);
224 schedule_work(&ioc
->release_work
);
226 EXPORT_SYMBOL(put_io_context
);
228 /* Called by the exiting task */
229 void exit_io_context(struct task_struct
*task
)
231 struct io_context
*ioc
;
233 /* PF_EXITING prevents new io_context from being attached to @task */
234 WARN_ON_ONCE(!(current
->flags
& PF_EXITING
));
237 ioc
= task
->io_context
;
238 task
->io_context
= NULL
;
241 atomic_dec(&ioc
->nr_tasks
);
242 put_io_context(ioc
, NULL
);
246 * ioc_clear_queue - break any ioc association with the specified queue
247 * @q: request_queue being cleared
249 * Walk @q->icq_list and exit all io_cq's. Must be called with @q locked.
251 void ioc_clear_queue(struct request_queue
*q
)
253 lockdep_assert_held(q
->queue_lock
);
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
;
260 spin_lock(&ioc
->lock
);
262 spin_unlock(&ioc
->lock
);
266 void create_io_context_slowpath(struct task_struct
*task
, gfp_t gfp_flags
,
269 struct io_context
*ioc
;
271 ioc
= kmem_cache_alloc_node(iocontext_cachep
, gfp_flags
| __GFP_ZERO
,
277 atomic_long_set(&ioc
->refcount
, 1);
278 atomic_set(&ioc
->nr_tasks
, 1);
279 spin_lock_init(&ioc
->lock
);
280 INIT_RADIX_TREE(&ioc
->icq_tree
, GFP_ATOMIC
| __GFP_HIGH
);
281 INIT_HLIST_HEAD(&ioc
->icq_list
);
282 INIT_WORK(&ioc
->release_work
, ioc_release_fn
);
285 * Try to install. ioc shouldn't be installed if someone else
286 * already did or @task, which isn't %current, is exiting. Note
287 * that we need to allow ioc creation on exiting %current as exit
288 * path may issue IOs from e.g. exit_files(). The exit path is
289 * responsible for not issuing IO after exit_io_context().
292 if (!task
->io_context
&&
293 (task
== current
|| !(task
->flags
& PF_EXITING
)))
294 task
->io_context
= ioc
;
296 kmem_cache_free(iocontext_cachep
, ioc
);
301 * get_task_io_context - get io_context of a task
302 * @task: task of interest
303 * @gfp_flags: allocation flags, used if allocation is necessary
304 * @node: allocation node, used if allocation is necessary
306 * Return io_context of @task. If it doesn't exist, it is created with
307 * @gfp_flags and @node. The returned io_context has its reference count
310 * This function always goes through task_lock() and it's better to use
311 * %current->io_context + get_io_context() for %current.
313 struct io_context
*get_task_io_context(struct task_struct
*task
,
314 gfp_t gfp_flags
, int node
)
316 struct io_context
*ioc
;
318 might_sleep_if(gfp_flags
& __GFP_WAIT
);
322 ioc
= task
->io_context
;
329 } while (create_io_context(task
, gfp_flags
, node
));
333 EXPORT_SYMBOL(get_task_io_context
);
336 * ioc_lookup_icq - lookup io_cq from ioc
337 * @ioc: the associated io_context
338 * @q: the associated request_queue
340 * Look up io_cq associated with @ioc - @q pair from @ioc. Must be called
341 * with @q->queue_lock held.
343 struct io_cq
*ioc_lookup_icq(struct io_context
*ioc
, struct request_queue
*q
)
347 lockdep_assert_held(q
->queue_lock
);
350 * icq's are indexed from @ioc using radix tree and hint pointer,
351 * both of which are protected with RCU. All removals are done
352 * holding both q and ioc locks, and we're holding q lock - if we
353 * find a icq which points to us, it's guaranteed to be valid.
356 icq
= rcu_dereference(ioc
->icq_hint
);
357 if (icq
&& icq
->q
== q
)
360 icq
= radix_tree_lookup(&ioc
->icq_tree
, q
->id
);
361 if (icq
&& icq
->q
== q
)
362 rcu_assign_pointer(ioc
->icq_hint
, icq
); /* allowed to race */
369 EXPORT_SYMBOL(ioc_lookup_icq
);
372 * ioc_create_icq - create and link io_cq
373 * @q: request_queue of interest
374 * @gfp_mask: allocation mask
376 * Make sure io_cq linking %current->io_context and @q exists. If either
377 * io_context and/or icq don't exist, they will be created using @gfp_mask.
379 * The caller is responsible for ensuring @ioc won't go away and @q is
380 * alive and will stay alive until this function returns.
382 struct io_cq
*ioc_create_icq(struct request_queue
*q
, gfp_t gfp_mask
)
384 struct elevator_type
*et
= q
->elevator
->type
;
385 struct io_context
*ioc
;
389 ioc
= create_io_context(current
, gfp_mask
, q
->node
);
393 icq
= kmem_cache_alloc_node(et
->icq_cache
, gfp_mask
| __GFP_ZERO
,
398 if (radix_tree_preload(gfp_mask
) < 0) {
399 kmem_cache_free(et
->icq_cache
, icq
);
405 INIT_LIST_HEAD(&icq
->q_node
);
406 INIT_HLIST_NODE(&icq
->ioc_node
);
408 /* lock both q and ioc and try to link @icq */
409 spin_lock_irq(q
->queue_lock
);
410 spin_lock(&ioc
->lock
);
412 if (likely(!radix_tree_insert(&ioc
->icq_tree
, q
->id
, icq
))) {
413 hlist_add_head(&icq
->ioc_node
, &ioc
->icq_list
);
414 list_add(&icq
->q_node
, &q
->icq_list
);
415 if (et
->ops
.elevator_init_icq_fn
)
416 et
->ops
.elevator_init_icq_fn(icq
);
418 kmem_cache_free(et
->icq_cache
, icq
);
419 icq
= ioc_lookup_icq(ioc
, q
);
421 printk(KERN_ERR
"cfq: icq link failed!\n");
424 spin_unlock(&ioc
->lock
);
425 spin_unlock_irq(q
->queue_lock
);
426 radix_tree_preload_end();
430 void ioc_set_changed(struct io_context
*ioc
, int which
)
433 struct hlist_node
*n
;
435 hlist_for_each_entry(icq
, n
, &ioc
->icq_list
, ioc_node
)
436 set_bit(which
, &icq
->changed
);
440 * ioc_ioprio_changed - notify ioprio change
441 * @ioc: io_context of interest
442 * @ioprio: new ioprio
444 * @ioc's ioprio has changed to @ioprio. Set %ICQ_IOPRIO_CHANGED for all
445 * icq's. iosched is responsible for checking the bit and applying it on
446 * request issue path.
448 void ioc_ioprio_changed(struct io_context
*ioc
, int ioprio
)
452 spin_lock_irqsave(&ioc
->lock
, flags
);
453 ioc
->ioprio
= ioprio
;
454 ioc_set_changed(ioc
, ICQ_IOPRIO_CHANGED
);
455 spin_unlock_irqrestore(&ioc
->lock
, flags
);
459 * ioc_cgroup_changed - notify cgroup change
460 * @ioc: io_context of interest
462 * @ioc's cgroup has changed. Set %ICQ_CGROUP_CHANGED for all icq's.
463 * iosched is responsible for checking the bit and applying it on request
466 void ioc_cgroup_changed(struct io_context
*ioc
)
470 spin_lock_irqsave(&ioc
->lock
, flags
);
471 ioc_set_changed(ioc
, ICQ_CGROUP_CHANGED
);
472 spin_unlock_irqrestore(&ioc
->lock
, flags
);
474 EXPORT_SYMBOL(ioc_cgroup_changed
);
476 static int __init
blk_ioc_init(void)
478 iocontext_cachep
= kmem_cache_create("blkdev_ioc",
479 sizeof(struct io_context
), 0, SLAB_PANIC
, NULL
);
482 subsys_initcall(blk_ioc_init
);