blob: 04f3d2b0ca7db4636b42b58163de7f16e15b4129 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jens Axboe86db1e22008-01-29 14:53:40 +01002/*
3 * Functions related to io context handling
4 */
5#include <linux/kernel.h>
6#include <linux/module.h>
7#include <linux/init.h>
8#include <linux/bio.h>
9#include <linux/blkdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Ingo Molnarf719ff9b2017-02-06 10:57:33 +010011#include <linux/sched/task.h>
Jens Axboe86db1e22008-01-29 14:53:40 +010012
13#include "blk.h"
Christoph Hellwig2aa77452021-11-23 19:53:08 +010014#include "blk-mq-sched.h"
Jens Axboe86db1e22008-01-29 14:53:40 +010015
16/*
17 * For io context allocations
18 */
19static struct kmem_cache *iocontext_cachep;
20
Tejun Heo6e736be2011-12-14 00:33:38 +010021/**
22 * get_io_context - increment reference count to io_context
23 * @ioc: io_context to get
24 *
25 * Increment reference count to @ioc.
26 */
Christoph Hellwig87dd1d62021-11-26 12:58:10 +010027static void get_io_context(struct io_context *ioc)
Tejun Heo6e736be2011-12-14 00:33:38 +010028{
29 BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
30 atomic_long_inc(&ioc->refcount);
31}
Tejun Heo6e736be2011-12-14 00:33:38 +010032
Tejun Heo7e5a8792011-12-14 00:33:42 +010033static void icq_free_icq_rcu(struct rcu_head *head)
34{
35 struct io_cq *icq = container_of(head, struct io_cq, __rcu_head);
36
37 kmem_cache_free(icq->__rcu_icq_cache, icq);
38}
39
Omar Sandoval3d492c22017-02-10 10:32:34 -080040/*
Jens Axboe7b36a712017-03-02 13:59:08 -070041 * Exit an icq. Called with ioc locked for blk-mq, and with both ioc
42 * and queue locked for legacy.
Omar Sandoval3d492c22017-02-10 10:32:34 -080043 */
Tejun Heo7e5a8792011-12-14 00:33:42 +010044static void ioc_exit_icq(struct io_cq *icq)
45{
Tejun Heo621032a2012-02-15 09:45:53 +010046 struct elevator_type *et = icq->q->elevator->type;
47
48 if (icq->flags & ICQ_EXITED)
49 return;
50
Jens Axboef9cd4bf2018-11-01 16:41:41 -060051 if (et->ops.exit_icq)
52 et->ops.exit_icq(icq);
Tejun Heo621032a2012-02-15 09:45:53 +010053
54 icq->flags |= ICQ_EXITED;
55}
56
Christoph Hellwig4be8a2e2021-12-09 07:31:23 +010057static void ioc_exit_icqs(struct io_context *ioc)
58{
59 struct io_cq *icq;
60
61 spin_lock_irq(&ioc->lock);
62 hlist_for_each_entry(icq, &ioc->icq_list, ioc_node)
63 ioc_exit_icq(icq);
64 spin_unlock_irq(&ioc->lock);
65}
66
Jens Axboe7b36a712017-03-02 13:59:08 -070067/*
68 * Release an icq. Called with ioc locked for blk-mq, and with both ioc
69 * and queue locked for legacy.
70 */
Tejun Heo621032a2012-02-15 09:45:53 +010071static void ioc_destroy_icq(struct io_cq *icq)
72{
Tejun Heo7e5a8792011-12-14 00:33:42 +010073 struct io_context *ioc = icq->ioc;
74 struct request_queue *q = icq->q;
75 struct elevator_type *et = q->elevator->type;
76
77 lockdep_assert_held(&ioc->lock);
Tejun Heo7e5a8792011-12-14 00:33:42 +010078
79 radix_tree_delete(&ioc->icq_tree, icq->q->id);
80 hlist_del_init(&icq->ioc_node);
81 list_del_init(&icq->q_node);
82
83 /*
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.
87 */
Paul E. McKenneyec6c676a2014-02-17 13:35:57 -080088 if (rcu_access_pointer(ioc->icq_hint) == icq)
Tejun Heo7e5a8792011-12-14 00:33:42 +010089 rcu_assign_pointer(ioc->icq_hint, NULL);
90
Tejun Heo621032a2012-02-15 09:45:53 +010091 ioc_exit_icq(icq);
Tejun Heo7e5a8792011-12-14 00:33:42 +010092
93 /*
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.
96 */
97 icq->__rcu_icq_cache = et->icq_cache;
Sahitya Tummala30a2da72020-03-11 16:07:50 +053098 icq->flags |= ICQ_DESTROYED;
Tejun Heo7e5a8792011-12-14 00:33:42 +010099 call_rcu(&icq->__rcu_head, icq_free_icq_rcu);
100}
101
Tejun Heob2efa052011-12-14 00:33:39 +0100102/*
103 * Slow path for ioc release in put_io_context(). Performs double-lock
Tejun Heoc5869802011-12-14 00:33:41 +0100104 * dancing to unlink all icq's and then frees ioc.
Tejun Heob2efa052011-12-14 00:33:39 +0100105 */
106static void ioc_release_fn(struct work_struct *work)
107{
108 struct io_context *ioc = container_of(work, struct io_context,
109 release_work);
John Ognessa43f0852020-06-19 17:23:17 +0206110 spin_lock_irq(&ioc->lock);
Tejun Heob2efa052011-12-14 00:33:39 +0100111
Tejun Heoc5869802011-12-14 00:33:41 +0100112 while (!hlist_empty(&ioc->icq_list)) {
113 struct io_cq *icq = hlist_entry(ioc->icq_list.first,
114 struct io_cq, ioc_node);
Tejun Heo2274b022012-02-15 09:45:52 +0100115 struct request_queue *q = icq->q;
Tejun Heob2efa052011-12-14 00:33:39 +0100116
Christoph Hellwig0d945c12018-11-15 12:17:28 -0700117 if (spin_trylock(&q->queue_lock)) {
Tejun Heo621032a2012-02-15 09:45:53 +0100118 ioc_destroy_icq(icq);
Christoph Hellwig0d945c12018-11-15 12:17:28 -0700119 spin_unlock(&q->queue_lock);
Tejun Heo2274b022012-02-15 09:45:52 +0100120 } else {
John Ognessab96bba2020-06-19 17:23:18 +0206121 /* Make sure q and icq cannot be freed. */
122 rcu_read_lock();
123
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);
128
129 /*
130 * The icq may have been destroyed when the ioc lock
131 * was released.
132 */
133 if (!(icq->flags & ICQ_DESTROYED))
134 ioc_destroy_icq(icq);
135
136 spin_unlock(&q->queue_lock);
137 rcu_read_unlock();
Tejun Heob2efa052011-12-14 00:33:39 +0100138 }
Jens Axboeffc4e752008-02-19 10:02:29 +0100139 }
Tejun Heob2efa052011-12-14 00:33:39 +0100140
John Ognessa43f0852020-06-19 17:23:17 +0206141 spin_unlock_irq(&ioc->lock);
Tejun Heob2efa052011-12-14 00:33:39 +0100142
143 kmem_cache_free(iocontext_cachep, ioc);
Jens Axboe86db1e22008-01-29 14:53:40 +0100144}
145
Tejun Heo42ec57a2011-12-14 00:33:37 +0100146/**
147 * put_io_context - put a reference of io_context
148 * @ioc: io_context to put
149 *
150 * Decrement reference count of @ioc and release it if the count reaches
Tejun Heo11a31222012-02-07 07:51:30 +0100151 * zero.
Jens Axboe86db1e22008-01-29 14:53:40 +0100152 */
Tejun Heo11a31222012-02-07 07:51:30 +0100153void put_io_context(struct io_context *ioc)
Jens Axboe86db1e22008-01-29 14:53:40 +0100154{
Tejun Heob2efa052011-12-14 00:33:39 +0100155 unsigned long flags;
Xiaotian Fengff8c1472012-03-14 15:34:48 +0100156 bool free_ioc = false;
Tejun Heob2efa052011-12-14 00:33:39 +0100157
Tejun Heo42ec57a2011-12-14 00:33:37 +0100158 BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
Jens Axboe86db1e22008-01-29 14:53:40 +0100159
Tejun Heob2efa052011-12-14 00:33:39 +0100160 /*
Tejun Heo11a31222012-02-07 07:51:30 +0100161 * Releasing ioc requires reverse order double locking and we may
162 * already be holding a queue_lock. Do it asynchronously from wq.
Tejun Heob2efa052011-12-14 00:33:39 +0100163 */
Tejun Heo11a31222012-02-07 07:51:30 +0100164 if (atomic_long_dec_and_test(&ioc->refcount)) {
165 spin_lock_irqsave(&ioc->lock, flags);
166 if (!hlist_empty(&ioc->icq_list))
Viresh Kumar695588f2013-04-24 17:12:56 +0530167 queue_work(system_power_efficient_wq,
168 &ioc->release_work);
Xiaotian Fengff8c1472012-03-14 15:34:48 +0100169 else
170 free_ioc = true;
Tejun Heo11a31222012-02-07 07:51:30 +0100171 spin_unlock_irqrestore(&ioc->lock, flags);
Tejun Heob2efa052011-12-14 00:33:39 +0100172 }
Xiaotian Fengff8c1472012-03-14 15:34:48 +0100173
174 if (free_ioc)
175 kmem_cache_free(iocontext_cachep, ioc);
Jens Axboe86db1e22008-01-29 14:53:40 +0100176}
Christoph Hellwig222ee582021-11-26 12:58:11 +0100177EXPORT_SYMBOL_GPL(put_io_context);
Jens Axboe86db1e22008-01-29 14:53:40 +0100178
Tejun Heof6e8d012012-03-05 13:15:26 -0800179/* Called by the exiting task */
180void exit_io_context(struct task_struct *task)
181{
182 struct io_context *ioc;
183
184 task_lock(task);
185 ioc = task->io_context;
186 task->io_context = NULL;
187 task_unlock(task);
188
Christoph Hellwig4be8a2e2021-12-09 07:31:23 +0100189 if (atomic_dec_and_test(&ioc->active_ref)) {
190 ioc_exit_icqs(ioc);
191 put_io_context(ioc);
192 }
Tejun Heof6e8d012012-03-05 13:15:26 -0800193}
194
Jens Axboe7b36a712017-03-02 13:59:08 -0700195static void __ioc_clear_queue(struct list_head *icq_list)
196{
197 unsigned long flags;
198
Sahitya Tummala30a2da72020-03-11 16:07:50 +0530199 rcu_read_lock();
Jens Axboe7b36a712017-03-02 13:59:08 -0700200 while (!list_empty(icq_list)) {
201 struct io_cq *icq = list_entry(icq_list->next,
Jens Axboea1ce35f2018-10-29 10:23:51 -0600202 struct io_cq, q_node);
Jens Axboe7b36a712017-03-02 13:59:08 -0700203 struct io_context *ioc = icq->ioc;
204
205 spin_lock_irqsave(&ioc->lock, flags);
Sahitya Tummala30a2da72020-03-11 16:07:50 +0530206 if (icq->flags & ICQ_DESTROYED) {
207 spin_unlock_irqrestore(&ioc->lock, flags);
208 continue;
209 }
Jens Axboe7b36a712017-03-02 13:59:08 -0700210 ioc_destroy_icq(icq);
211 spin_unlock_irqrestore(&ioc->lock, flags);
212 }
Sahitya Tummala30a2da72020-03-11 16:07:50 +0530213 rcu_read_unlock();
Jens Axboe7b36a712017-03-02 13:59:08 -0700214}
215
Tejun Heo7e5a8792011-12-14 00:33:42 +0100216/**
217 * ioc_clear_queue - break any ioc association with the specified queue
218 * @q: request_queue being cleared
219 *
Jens Axboe7b36a712017-03-02 13:59:08 -0700220 * Walk @q->icq_list and exit all io_cq's.
Tejun Heo7e5a8792011-12-14 00:33:42 +0100221 */
222void ioc_clear_queue(struct request_queue *q)
223{
Jens Axboe7b36a712017-03-02 13:59:08 -0700224 LIST_HEAD(icq_list);
Tejun Heo7e5a8792011-12-14 00:33:42 +0100225
Christoph Hellwig0d945c12018-11-15 12:17:28 -0700226 spin_lock_irq(&q->queue_lock);
Jens Axboe7b36a712017-03-02 13:59:08 -0700227 list_splice_init(&q->icq_list, &icq_list);
Christoph Hellwig0d945c12018-11-15 12:17:28 -0700228 spin_unlock_irq(&q->queue_lock);
Tejun Heo7e5a8792011-12-14 00:33:42 +0100229
Jens Axboea1ce35f2018-10-29 10:23:51 -0600230 __ioc_clear_queue(&icq_list);
Tejun Heo7e5a8792011-12-14 00:33:42 +0100231}
232
Christoph Hellwiga0f14d82021-11-26 12:58:13 +0100233static struct io_context *alloc_io_context(gfp_t gfp_flags, int node)
Jens Axboe86db1e22008-01-29 14:53:40 +0100234{
Paul Bolledf415652011-06-06 05:11:34 +0200235 struct io_context *ioc;
Jens Axboe86db1e22008-01-29 14:53:40 +0100236
Tejun Heo42ec57a2011-12-14 00:33:37 +0100237 ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO,
238 node);
239 if (unlikely(!ioc))
Christoph Hellwiga0f14d82021-11-26 12:58:13 +0100240 return NULL;
Tejun Heo42ec57a2011-12-14 00:33:37 +0100241
Tejun Heo42ec57a2011-12-14 00:33:37 +0100242 atomic_long_set(&ioc->refcount, 1);
Tejun Heof6e8d012012-03-05 13:15:26 -0800243 atomic_set(&ioc->active_ref, 1);
Tejun Heo42ec57a2011-12-14 00:33:37 +0100244 spin_lock_init(&ioc->lock);
Shakeel Buttc1379692018-07-03 10:14:46 -0700245 INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC);
Tejun Heoc5869802011-12-14 00:33:41 +0100246 INIT_HLIST_HEAD(&ioc->icq_list);
Tejun Heob2efa052011-12-14 00:33:39 +0100247 INIT_WORK(&ioc->release_work, ioc_release_fn);
Christoph Hellwiga0f14d82021-11-26 12:58:13 +0100248 return ioc;
249}
250
Christoph Hellwigd538ea42021-11-26 12:58:15 +0100251static struct io_context *create_task_io_context(struct task_struct *task,
252 gfp_t gfp_flags, int node)
Christoph Hellwiga0f14d82021-11-26 12:58:13 +0100253{
254 struct io_context *ioc;
Christoph Hellwiga0f14d82021-11-26 12:58:13 +0100255
256 ioc = alloc_io_context(gfp_flags, node);
257 if (!ioc)
Christoph Hellwigd538ea42021-11-26 12:58:15 +0100258 return NULL;
Jens Axboe86db1e22008-01-29 14:53:40 +0100259
Tejun Heofd638362011-12-25 14:29:14 +0100260 /*
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().
266 */
Tejun Heo6e736be2011-12-14 00:33:38 +0100267 task_lock(task);
Tejun Heofd638362011-12-25 14:29:14 +0100268 if (!task->io_context &&
269 (task == current || !(task->flags & PF_EXITING)))
Tejun Heo6e736be2011-12-14 00:33:38 +0100270 task->io_context = ioc;
Tejun Heof2dbd762011-12-14 00:33:40 +0100271 else
Tejun Heo6e736be2011-12-14 00:33:38 +0100272 kmem_cache_free(iocontext_cachep, ioc);
Eric Dumazet3c9c7082012-05-31 13:39:05 +0200273
Christoph Hellwigd538ea42021-11-26 12:58:15 +0100274 ioc = task->io_context;
275 if (ioc)
276 get_io_context(ioc);
Tejun Heo6e736be2011-12-14 00:33:38 +0100277 task_unlock(task);
Christoph Hellwigd538ea42021-11-26 12:58:15 +0100278 return ioc;
Jens Axboe86db1e22008-01-29 14:53:40 +0100279}
Jens Axboe86db1e22008-01-29 14:53:40 +0100280
Tejun Heo6e736be2011-12-14 00:33:38 +0100281/**
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
Jens Axboe86db1e22008-01-29 14:53:40 +0100286 *
Tejun Heo6e736be2011-12-14 00:33:38 +0100287 * 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
289 * incremented.
290 *
291 * This function always goes through task_lock() and it's better to use
Tejun Heof2dbd762011-12-14 00:33:40 +0100292 * %current->io_context + get_io_context() for %current.
Jens Axboe86db1e22008-01-29 14:53:40 +0100293 */
Tejun Heo6e736be2011-12-14 00:33:38 +0100294struct io_context *get_task_io_context(struct task_struct *task,
295 gfp_t gfp_flags, int node)
Jens Axboe86db1e22008-01-29 14:53:40 +0100296{
Tejun Heo6e736be2011-12-14 00:33:38 +0100297 struct io_context *ioc;
Jens Axboe86db1e22008-01-29 14:53:40 +0100298
Mel Gormand0164ad2015-11-06 16:28:21 -0800299 might_sleep_if(gfpflags_allow_blocking(gfp_flags));
Jens Axboe86db1e22008-01-29 14:53:40 +0100300
Christoph Hellwigd538ea42021-11-26 12:58:15 +0100301 task_lock(task);
302 ioc = task->io_context;
303 if (unlikely(!ioc)) {
Tejun Heo6e736be2011-12-14 00:33:38 +0100304 task_unlock(task);
Christoph Hellwigd538ea42021-11-26 12:58:15 +0100305 return create_task_io_context(task, gfp_flags, node);
306 }
307 get_io_context(ioc);
308 task_unlock(task);
309 return ioc;
Jens Axboe86db1e22008-01-29 14:53:40 +0100310}
Jens Axboe86db1e22008-01-29 14:53:40 +0100311
Christoph Hellwig88c9a2c2021-11-26 12:58:05 +0100312int __copy_io(unsigned long clone_flags, struct task_struct *tsk)
313{
314 struct io_context *ioc = current->io_context;
Christoph Hellwig88c9a2c2021-11-26 12:58:05 +0100315
316 /*
317 * Share io context with parent, if CLONE_IO is set
318 */
319 if (clone_flags & CLONE_IO) {
Christoph Hellwig50569c22021-11-26 12:58:12 +0100320 atomic_inc(&ioc->active_ref);
Christoph Hellwig88c9a2c2021-11-26 12:58:05 +0100321 tsk->io_context = ioc;
322 } else if (ioprio_valid(ioc->ioprio)) {
Christoph Hellwig8ffc1362021-11-26 12:58:14 +0100323 tsk->io_context = alloc_io_context(GFP_KERNEL, NUMA_NO_NODE);
324 if (!tsk->io_context)
Christoph Hellwig88c9a2c2021-11-26 12:58:05 +0100325 return -ENOMEM;
Christoph Hellwig8ffc1362021-11-26 12:58:14 +0100326 tsk->io_context->ioprio = ioc->ioprio;
Christoph Hellwig88c9a2c2021-11-26 12:58:05 +0100327 }
328
329 return 0;
330}
331
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100332/**
333 * ioc_lookup_icq - lookup io_cq from ioc
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100334 * @q: the associated request_queue
335 *
336 * Look up io_cq associated with @ioc - @q pair from @ioc. Must be called
337 * with @q->queue_lock held.
338 */
Christoph Hellwigeca58922021-11-26 12:58:17 +0100339struct io_cq *ioc_lookup_icq(struct request_queue *q)
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100340{
Christoph Hellwigeca58922021-11-26 12:58:17 +0100341 struct io_context *ioc = current->io_context;
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100342 struct io_cq *icq;
343
Christoph Hellwig0d945c12018-11-15 12:17:28 -0700344 lockdep_assert_held(&q->queue_lock);
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100345
346 /*
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.
351 */
352 rcu_read_lock();
353 icq = rcu_dereference(ioc->icq_hint);
354 if (icq && icq->q == q)
355 goto out;
356
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 */
360 else
361 icq = NULL;
362out:
363 rcu_read_unlock();
364 return icq;
365}
366EXPORT_SYMBOL(ioc_lookup_icq);
367
Tejun Heof1f8cc92011-12-14 00:33:42 +0100368/**
369 * ioc_create_icq - create and link io_cq
370 * @q: request_queue of interest
Tejun Heof1f8cc92011-12-14 00:33:42 +0100371 *
Tejun Heo24acfc32012-03-05 13:15:24 -0800372 * Make sure io_cq linking @ioc and @q exists. If icq doesn't exist, they
373 * will be created using @gfp_mask.
Tejun Heof1f8cc92011-12-14 00:33:42 +0100374 *
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.
377 */
Christoph Hellwig18b74c42021-11-26 12:58:16 +0100378static struct io_cq *ioc_create_icq(struct request_queue *q)
Tejun Heof1f8cc92011-12-14 00:33:42 +0100379{
Christoph Hellwig18b74c42021-11-26 12:58:16 +0100380 struct io_context *ioc = current->io_context;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100381 struct elevator_type *et = q->elevator->type;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100382 struct io_cq *icq;
383
384 /* allocate stuff */
Christoph Hellwig18b74c42021-11-26 12:58:16 +0100385 icq = kmem_cache_alloc_node(et->icq_cache, GFP_ATOMIC | __GFP_ZERO,
Tejun Heof1f8cc92011-12-14 00:33:42 +0100386 q->node);
387 if (!icq)
388 return NULL;
389
Christoph Hellwig18b74c42021-11-26 12:58:16 +0100390 if (radix_tree_maybe_preload(GFP_ATOMIC) < 0) {
Tejun Heof1f8cc92011-12-14 00:33:42 +0100391 kmem_cache_free(et->icq_cache, icq);
392 return NULL;
393 }
394
395 icq->ioc = ioc;
396 icq->q = q;
397 INIT_LIST_HEAD(&icq->q_node);
398 INIT_HLIST_NODE(&icq->ioc_node);
399
400 /* lock both q and ioc and try to link @icq */
Christoph Hellwig0d945c12018-11-15 12:17:28 -0700401 spin_lock_irq(&q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100402 spin_lock(&ioc->lock);
403
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);
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600407 if (et->ops.init_icq)
408 et->ops.init_icq(icq);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100409 } else {
410 kmem_cache_free(et->icq_cache, icq);
Christoph Hellwigeca58922021-11-26 12:58:17 +0100411 icq = ioc_lookup_icq(q);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100412 if (!icq)
413 printk(KERN_ERR "cfq: icq link failed!\n");
414 }
415
416 spin_unlock(&ioc->lock);
Christoph Hellwig0d945c12018-11-15 12:17:28 -0700417 spin_unlock_irq(&q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100418 radix_tree_preload_end();
419 return icq;
420}
421
Christoph Hellwig87dd1d62021-11-26 12:58:10 +0100422struct io_cq *ioc_find_get_icq(struct request_queue *q)
423{
Christoph Hellwigd538ea42021-11-26 12:58:15 +0100424 struct io_context *ioc = current->io_context;
425 struct io_cq *icq = NULL;
Christoph Hellwig87dd1d62021-11-26 12:58:10 +0100426
Christoph Hellwigd538ea42021-11-26 12:58:15 +0100427 if (unlikely(!ioc)) {
428 ioc = create_task_io_context(current, GFP_ATOMIC, q->node);
429 if (!ioc)
430 return NULL;
431 } else {
432 get_io_context(ioc);
Christoph Hellwig87dd1d62021-11-26 12:58:10 +0100433
Christoph Hellwigd538ea42021-11-26 12:58:15 +0100434 spin_lock_irq(&q->queue_lock);
Christoph Hellwigeca58922021-11-26 12:58:17 +0100435 icq = ioc_lookup_icq(q);
Christoph Hellwigd538ea42021-11-26 12:58:15 +0100436 spin_unlock_irq(&q->queue_lock);
437 }
Christoph Hellwig87dd1d62021-11-26 12:58:10 +0100438
439 if (!icq) {
Christoph Hellwig18b74c42021-11-26 12:58:16 +0100440 icq = ioc_create_icq(q);
Christoph Hellwigd538ea42021-11-26 12:58:15 +0100441 if (!icq) {
442 put_io_context(ioc);
Christoph Hellwig87dd1d62021-11-26 12:58:10 +0100443 return NULL;
Christoph Hellwigd538ea42021-11-26 12:58:15 +0100444 }
Christoph Hellwig87dd1d62021-11-26 12:58:10 +0100445 }
Christoph Hellwig87dd1d62021-11-26 12:58:10 +0100446 return icq;
447}
448EXPORT_SYMBOL_GPL(ioc_find_get_icq);
449
Adrian Bunk13341592008-02-18 13:45:53 +0100450static int __init blk_ioc_init(void)
Jens Axboe86db1e22008-01-29 14:53:40 +0100451{
452 iocontext_cachep = kmem_cache_create("blkdev_ioc",
453 sizeof(struct io_context), 0, SLAB_PANIC, NULL);
454 return 0;
455}
456subsys_initcall(blk_ioc_init);