1 // SPDX-License-Identifier: GPL-2.0
3 * Functions related to io context handling
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
9 #include <linux/blkdev.h>
10 #include <linux/slab.h>
11 #include <linux/sched/task.h>
14 #include "blk-mq-sched.h"
17 * For io context allocations
19 static struct kmem_cache *iocontext_cachep;
22 * get_io_context - increment reference count to io_context
23 * @ioc: io_context to get
25 * Increment reference count to @ioc.
27 static void get_io_context(struct io_context *ioc)
29 BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
30 atomic_long_inc(&ioc->refcount);
33 static void icq_free_icq_rcu(struct rcu_head *head)
35 struct io_cq *icq = container_of(head, struct io_cq, __rcu_head);
37 kmem_cache_free(icq->__rcu_icq_cache, icq);
41 * Exit an icq. Called with ioc locked for blk-mq, and with both ioc
42 * and queue locked for legacy.
44 static void ioc_exit_icq(struct io_cq *icq)
46 struct elevator_type *et = icq->q->elevator->type;
48 if (icq->flags & ICQ_EXITED)
52 et->ops.exit_icq(icq);
54 icq->flags |= ICQ_EXITED;
57 static void ioc_exit_icqs(struct io_context *ioc)
61 spin_lock_irq(&ioc->lock);
62 hlist_for_each_entry(icq, &ioc->icq_list, ioc_node)
64 spin_unlock_irq(&ioc->lock);
68 * Release an icq. Called with ioc locked for blk-mq, and with both ioc
69 * and queue locked for legacy.
71 static void ioc_destroy_icq(struct io_cq *icq)
73 struct io_context *ioc = icq->ioc;
74 struct request_queue *q = icq->q;
75 struct elevator_type *et = q->elevator->type;
77 lockdep_assert_held(&ioc->lock);
79 radix_tree_delete(&ioc->icq_tree, icq->q->id);
80 hlist_del_init(&icq->ioc_node);
81 list_del_init(&icq->q_node);
84 * Both setting lookup hint to and clearing it from @icq are done
85 * under queue_lock. If it's not pointing to @icq now, it never
86 * will. Hint assignment itself can race safely.
88 if (rcu_access_pointer(ioc->icq_hint) == icq)
89 rcu_assign_pointer(ioc->icq_hint, NULL);
94 * @icq->q might have gone away by the time RCU callback runs
95 * making it impossible to determine icq_cache. Record it in @icq.
97 icq->__rcu_icq_cache = et->icq_cache;
98 icq->flags |= ICQ_DESTROYED;
99 call_rcu(&icq->__rcu_head, icq_free_icq_rcu);
103 * Slow path for ioc release in put_io_context(). Performs double-lock
104 * dancing to unlink all icq's and then frees ioc.
106 static void ioc_release_fn(struct work_struct *work)
108 struct io_context *ioc = container_of(work, struct io_context,
110 spin_lock_irq(&ioc->lock);
112 while (!hlist_empty(&ioc->icq_list)) {
113 struct io_cq *icq = hlist_entry(ioc->icq_list.first,
114 struct io_cq, ioc_node);
115 struct request_queue *q = icq->q;
117 if (spin_trylock(&q->queue_lock)) {
118 ioc_destroy_icq(icq);
119 spin_unlock(&q->queue_lock);
121 /* Make sure q and icq cannot be freed. */
124 /* Re-acquire the locks in the correct order. */
125 spin_unlock(&ioc->lock);
126 spin_lock(&q->queue_lock);
127 spin_lock(&ioc->lock);
130 * The icq may have been destroyed when the ioc lock
133 if (!(icq->flags & ICQ_DESTROYED))
134 ioc_destroy_icq(icq);
136 spin_unlock(&q->queue_lock);
141 spin_unlock_irq(&ioc->lock);
143 kmem_cache_free(iocontext_cachep, ioc);
147 * put_io_context - put a reference of io_context
148 * @ioc: io_context to put
150 * Decrement reference count of @ioc and release it if the count reaches
153 void put_io_context(struct io_context *ioc)
156 bool free_ioc = false;
158 BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
161 * Releasing ioc requires reverse order double locking and we may
162 * already be holding a queue_lock. Do it asynchronously from wq.
164 if (atomic_long_dec_and_test(&ioc->refcount)) {
165 spin_lock_irqsave(&ioc->lock, flags);
166 if (!hlist_empty(&ioc->icq_list))
167 queue_work(system_power_efficient_wq,
171 spin_unlock_irqrestore(&ioc->lock, flags);
175 kmem_cache_free(iocontext_cachep, ioc);
177 EXPORT_SYMBOL_GPL(put_io_context);
179 /* Called by the exiting task */
180 void exit_io_context(struct task_struct *task)
182 struct io_context *ioc;
185 ioc = task->io_context;
186 task->io_context = NULL;
189 if (atomic_dec_and_test(&ioc->active_ref)) {
195 static void __ioc_clear_queue(struct list_head *icq_list)
200 while (!list_empty(icq_list)) {
201 struct io_cq *icq = list_entry(icq_list->next,
202 struct io_cq, q_node);
203 struct io_context *ioc = icq->ioc;
205 spin_lock_irqsave(&ioc->lock, flags);
206 if (icq->flags & ICQ_DESTROYED) {
207 spin_unlock_irqrestore(&ioc->lock, flags);
210 ioc_destroy_icq(icq);
211 spin_unlock_irqrestore(&ioc->lock, flags);
217 * ioc_clear_queue - break any ioc association with the specified queue
218 * @q: request_queue being cleared
220 * Walk @q->icq_list and exit all io_cq's.
222 void ioc_clear_queue(struct request_queue *q)
226 spin_lock_irq(&q->queue_lock);
227 list_splice_init(&q->icq_list, &icq_list);
228 spin_unlock_irq(&q->queue_lock);
230 __ioc_clear_queue(&icq_list);
233 static struct io_context *alloc_io_context(gfp_t gfp_flags, int node)
235 struct io_context *ioc;
237 ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO,
242 atomic_long_set(&ioc->refcount, 1);
243 atomic_set(&ioc->active_ref, 1);
244 spin_lock_init(&ioc->lock);
245 INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC);
246 INIT_HLIST_HEAD(&ioc->icq_list);
247 INIT_WORK(&ioc->release_work, ioc_release_fn);
251 static struct io_context *create_task_io_context(struct task_struct *task,
252 gfp_t gfp_flags, int node)
254 struct io_context *ioc;
256 ioc = alloc_io_context(gfp_flags, node);
261 * Try to install. ioc shouldn't be installed if someone else
262 * already did or @task, which isn't %current, is exiting. Note
263 * that we need to allow ioc creation on exiting %current as exit
264 * path may issue IOs from e.g. exit_files(). The exit path is
265 * responsible for not issuing IO after exit_io_context().
268 if (!task->io_context &&
269 (task == current || !(task->flags & PF_EXITING)))
270 task->io_context = ioc;
272 kmem_cache_free(iocontext_cachep, ioc);
274 ioc = task->io_context;
282 * get_task_io_context - get io_context of a task
283 * @task: task of interest
284 * @gfp_flags: allocation flags, used if allocation is necessary
285 * @node: allocation node, used if allocation is necessary
287 * Return io_context of @task. If it doesn't exist, it is created with
288 * @gfp_flags and @node. The returned io_context has its reference count
291 * This function always goes through task_lock() and it's better to use
292 * %current->io_context + get_io_context() for %current.
294 struct io_context *get_task_io_context(struct task_struct *task,
295 gfp_t gfp_flags, int node)
297 struct io_context *ioc;
299 might_sleep_if(gfpflags_allow_blocking(gfp_flags));
302 ioc = task->io_context;
303 if (unlikely(!ioc)) {
305 return create_task_io_context(task, gfp_flags, node);
312 int __copy_io(unsigned long clone_flags, struct task_struct *tsk)
314 struct io_context *ioc = current->io_context;
317 * Share io context with parent, if CLONE_IO is set
319 if (clone_flags & CLONE_IO) {
320 atomic_inc(&ioc->active_ref);
321 tsk->io_context = ioc;
322 } else if (ioprio_valid(ioc->ioprio)) {
323 tsk->io_context = alloc_io_context(GFP_KERNEL, NUMA_NO_NODE);
324 if (!tsk->io_context)
326 tsk->io_context->ioprio = ioc->ioprio;
333 * ioc_lookup_icq - lookup io_cq from ioc
334 * @q: the associated request_queue
336 * Look up io_cq associated with @ioc - @q pair from @ioc. Must be called
337 * with @q->queue_lock held.
339 struct io_cq *ioc_lookup_icq(struct request_queue *q)
341 struct io_context *ioc = current->io_context;
344 lockdep_assert_held(&q->queue_lock);
347 * icq's are indexed from @ioc using radix tree and hint pointer,
348 * both of which are protected with RCU. All removals are done
349 * holding both q and ioc locks, and we're holding q lock - if we
350 * find a icq which points to us, it's guaranteed to be valid.
353 icq = rcu_dereference(ioc->icq_hint);
354 if (icq && icq->q == q)
357 icq = radix_tree_lookup(&ioc->icq_tree, q->id);
358 if (icq && icq->q == q)
359 rcu_assign_pointer(ioc->icq_hint, icq); /* allowed to race */
366 EXPORT_SYMBOL(ioc_lookup_icq);
369 * ioc_create_icq - create and link io_cq
370 * @q: request_queue of interest
372 * Make sure io_cq linking @ioc and @q exists. If icq doesn't exist, they
373 * will be created using @gfp_mask.
375 * The caller is responsible for ensuring @ioc won't go away and @q is
376 * alive and will stay alive until this function returns.
378 static struct io_cq *ioc_create_icq(struct request_queue *q)
380 struct io_context *ioc = current->io_context;
381 struct elevator_type *et = q->elevator->type;
385 icq = kmem_cache_alloc_node(et->icq_cache, GFP_ATOMIC | __GFP_ZERO,
390 if (radix_tree_maybe_preload(GFP_ATOMIC) < 0) {
391 kmem_cache_free(et->icq_cache, icq);
397 INIT_LIST_HEAD(&icq->q_node);
398 INIT_HLIST_NODE(&icq->ioc_node);
400 /* lock both q and ioc and try to link @icq */
401 spin_lock_irq(&q->queue_lock);
402 spin_lock(&ioc->lock);
404 if (likely(!radix_tree_insert(&ioc->icq_tree, q->id, icq))) {
405 hlist_add_head(&icq->ioc_node, &ioc->icq_list);
406 list_add(&icq->q_node, &q->icq_list);
407 if (et->ops.init_icq)
408 et->ops.init_icq(icq);
410 kmem_cache_free(et->icq_cache, icq);
411 icq = ioc_lookup_icq(q);
413 printk(KERN_ERR "cfq: icq link failed!\n");
416 spin_unlock(&ioc->lock);
417 spin_unlock_irq(&q->queue_lock);
418 radix_tree_preload_end();
422 struct io_cq *ioc_find_get_icq(struct request_queue *q)
424 struct io_context *ioc = current->io_context;
425 struct io_cq *icq = NULL;
427 if (unlikely(!ioc)) {
428 ioc = create_task_io_context(current, GFP_ATOMIC, q->node);
434 spin_lock_irq(&q->queue_lock);
435 icq = ioc_lookup_icq(q);
436 spin_unlock_irq(&q->queue_lock);
440 icq = ioc_create_icq(q);
448 EXPORT_SYMBOL_GPL(ioc_find_get_icq);
450 static int __init blk_ioc_init(void)
452 iocontext_cachep = kmem_cache_create("blkdev_ioc",
453 sizeof(struct io_context), 0, SLAB_PANIC, NULL);
456 subsys_initcall(blk_ioc_init);