xref: /openbmc/linux/block/blk-ioc.c (revision bd166ef1)
186db1e29SJens Axboe /*
286db1e29SJens Axboe  * Functions related to io context handling
386db1e29SJens Axboe  */
486db1e29SJens Axboe #include <linux/kernel.h>
586db1e29SJens Axboe #include <linux/module.h>
686db1e29SJens Axboe #include <linux/init.h>
786db1e29SJens Axboe #include <linux/bio.h>
886db1e29SJens Axboe #include <linux/blkdev.h>
95a0e3ad6STejun Heo #include <linux/slab.h>
1086db1e29SJens Axboe 
1186db1e29SJens Axboe #include "blk.h"
1286db1e29SJens Axboe 
1386db1e29SJens Axboe /*
1486db1e29SJens Axboe  * For io context allocations
1586db1e29SJens Axboe  */
1686db1e29SJens Axboe static struct kmem_cache *iocontext_cachep;
1786db1e29SJens Axboe 
186e736be7STejun Heo /**
196e736be7STejun Heo  * get_io_context - increment reference count to io_context
206e736be7STejun Heo  * @ioc: io_context to get
216e736be7STejun Heo  *
226e736be7STejun Heo  * Increment reference count to @ioc.
236e736be7STejun Heo  */
246e736be7STejun Heo void get_io_context(struct io_context *ioc)
256e736be7STejun Heo {
266e736be7STejun Heo 	BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
276e736be7STejun Heo 	atomic_long_inc(&ioc->refcount);
286e736be7STejun Heo }
296e736be7STejun Heo EXPORT_SYMBOL(get_io_context);
306e736be7STejun Heo 
317e5a8794STejun Heo static void icq_free_icq_rcu(struct rcu_head *head)
327e5a8794STejun Heo {
337e5a8794STejun Heo 	struct io_cq *icq = container_of(head, struct io_cq, __rcu_head);
347e5a8794STejun Heo 
357e5a8794STejun Heo 	kmem_cache_free(icq->__rcu_icq_cache, icq);
367e5a8794STejun Heo }
377e5a8794STejun Heo 
38621032adSTejun Heo /* Exit an icq. Called with both ioc and q locked. */
397e5a8794STejun Heo static void ioc_exit_icq(struct io_cq *icq)
407e5a8794STejun Heo {
41621032adSTejun Heo 	struct elevator_type *et = icq->q->elevator->type;
42621032adSTejun Heo 
43621032adSTejun Heo 	if (icq->flags & ICQ_EXITED)
44621032adSTejun Heo 		return;
45621032adSTejun Heo 
46bd166ef1SJens Axboe 	if (et->uses_mq && et->ops.mq.exit_icq)
47bd166ef1SJens Axboe 		et->ops.mq.exit_icq(icq);
48bd166ef1SJens Axboe 	else if (!et->uses_mq && et->ops.sq.elevator_exit_icq_fn)
49c51ca6cfSJens Axboe 		et->ops.sq.elevator_exit_icq_fn(icq);
50621032adSTejun Heo 
51621032adSTejun Heo 	icq->flags |= ICQ_EXITED;
52621032adSTejun Heo }
53621032adSTejun Heo 
54621032adSTejun Heo /* Release an icq.  Called with both ioc and q locked. */
55621032adSTejun Heo static void ioc_destroy_icq(struct io_cq *icq)
56621032adSTejun Heo {
577e5a8794STejun Heo 	struct io_context *ioc = icq->ioc;
587e5a8794STejun Heo 	struct request_queue *q = icq->q;
597e5a8794STejun Heo 	struct elevator_type *et = q->elevator->type;
607e5a8794STejun Heo 
617e5a8794STejun Heo 	lockdep_assert_held(&ioc->lock);
627e5a8794STejun Heo 	lockdep_assert_held(q->queue_lock);
637e5a8794STejun Heo 
647e5a8794STejun Heo 	radix_tree_delete(&ioc->icq_tree, icq->q->id);
657e5a8794STejun Heo 	hlist_del_init(&icq->ioc_node);
667e5a8794STejun Heo 	list_del_init(&icq->q_node);
677e5a8794STejun Heo 
687e5a8794STejun Heo 	/*
697e5a8794STejun Heo 	 * Both setting lookup hint to and clearing it from @icq are done
707e5a8794STejun Heo 	 * under queue_lock.  If it's not pointing to @icq now, it never
717e5a8794STejun Heo 	 * will.  Hint assignment itself can race safely.
727e5a8794STejun Heo 	 */
73ec6c676aSPaul E. McKenney 	if (rcu_access_pointer(ioc->icq_hint) == icq)
747e5a8794STejun Heo 		rcu_assign_pointer(ioc->icq_hint, NULL);
757e5a8794STejun Heo 
76621032adSTejun Heo 	ioc_exit_icq(icq);
777e5a8794STejun Heo 
787e5a8794STejun Heo 	/*
797e5a8794STejun Heo 	 * @icq->q might have gone away by the time RCU callback runs
807e5a8794STejun Heo 	 * making it impossible to determine icq_cache.  Record it in @icq.
817e5a8794STejun Heo 	 */
827e5a8794STejun Heo 	icq->__rcu_icq_cache = et->icq_cache;
837e5a8794STejun Heo 	call_rcu(&icq->__rcu_head, icq_free_icq_rcu);
847e5a8794STejun Heo }
857e5a8794STejun Heo 
86b2efa052STejun Heo /*
87b2efa052STejun Heo  * Slow path for ioc release in put_io_context().  Performs double-lock
88c5869807STejun Heo  * dancing to unlink all icq's and then frees ioc.
89b2efa052STejun Heo  */
90b2efa052STejun Heo static void ioc_release_fn(struct work_struct *work)
91b2efa052STejun Heo {
92b2efa052STejun Heo 	struct io_context *ioc = container_of(work, struct io_context,
93b2efa052STejun Heo 					      release_work);
94d8c66c5dSTejun Heo 	unsigned long flags;
95b2efa052STejun Heo 
96d8c66c5dSTejun Heo 	/*
97d8c66c5dSTejun Heo 	 * Exiting icq may call into put_io_context() through elevator
98d8c66c5dSTejun Heo 	 * which will trigger lockdep warning.  The ioc's are guaranteed to
99d8c66c5dSTejun Heo 	 * be different, use a different locking subclass here.  Use
100d8c66c5dSTejun Heo 	 * irqsave variant as there's no spin_lock_irq_nested().
101d8c66c5dSTejun Heo 	 */
102d8c66c5dSTejun Heo 	spin_lock_irqsave_nested(&ioc->lock, flags, 1);
103b2efa052STejun Heo 
104c5869807STejun Heo 	while (!hlist_empty(&ioc->icq_list)) {
105c5869807STejun Heo 		struct io_cq *icq = hlist_entry(ioc->icq_list.first,
106c5869807STejun Heo 						struct io_cq, ioc_node);
1072274b029STejun Heo 		struct request_queue *q = icq->q;
108b2efa052STejun Heo 
1092274b029STejun Heo 		if (spin_trylock(q->queue_lock)) {
110621032adSTejun Heo 			ioc_destroy_icq(icq);
1112274b029STejun Heo 			spin_unlock(q->queue_lock);
112b2efa052STejun Heo 		} else {
113d8c66c5dSTejun Heo 			spin_unlock_irqrestore(&ioc->lock, flags);
1142274b029STejun Heo 			cpu_relax();
1152274b029STejun Heo 			spin_lock_irqsave_nested(&ioc->lock, flags, 1);
116b2efa052STejun Heo 		}
1172274b029STejun Heo 	}
1182274b029STejun Heo 
1192274b029STejun Heo 	spin_unlock_irqrestore(&ioc->lock, flags);
120b2efa052STejun Heo 
121b2efa052STejun Heo 	kmem_cache_free(iocontext_cachep, ioc);
12286db1e29SJens Axboe }
12386db1e29SJens Axboe 
12442ec57a8STejun Heo /**
12542ec57a8STejun Heo  * put_io_context - put a reference of io_context
12642ec57a8STejun Heo  * @ioc: io_context to put
12742ec57a8STejun Heo  *
12842ec57a8STejun Heo  * Decrement reference count of @ioc and release it if the count reaches
12911a3122fSTejun Heo  * zero.
13086db1e29SJens Axboe  */
13111a3122fSTejun Heo void put_io_context(struct io_context *ioc)
13286db1e29SJens Axboe {
133b2efa052STejun Heo 	unsigned long flags;
134ff8c1474SXiaotian Feng 	bool free_ioc = false;
135b2efa052STejun Heo 
13686db1e29SJens Axboe 	if (ioc == NULL)
13742ec57a8STejun Heo 		return;
13886db1e29SJens Axboe 
13942ec57a8STejun Heo 	BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
14042ec57a8STejun Heo 
141b2efa052STejun Heo 	/*
14211a3122fSTejun Heo 	 * Releasing ioc requires reverse order double locking and we may
14311a3122fSTejun Heo 	 * already be holding a queue_lock.  Do it asynchronously from wq.
144b2efa052STejun Heo 	 */
14511a3122fSTejun Heo 	if (atomic_long_dec_and_test(&ioc->refcount)) {
14611a3122fSTejun Heo 		spin_lock_irqsave(&ioc->lock, flags);
14711a3122fSTejun Heo 		if (!hlist_empty(&ioc->icq_list))
148695588f9SViresh Kumar 			queue_work(system_power_efficient_wq,
149695588f9SViresh Kumar 					&ioc->release_work);
150ff8c1474SXiaotian Feng 		else
151ff8c1474SXiaotian Feng 			free_ioc = true;
15211a3122fSTejun Heo 		spin_unlock_irqrestore(&ioc->lock, flags);
15311a3122fSTejun Heo 	}
154ff8c1474SXiaotian Feng 
155ff8c1474SXiaotian Feng 	if (free_ioc)
156ff8c1474SXiaotian Feng 		kmem_cache_free(iocontext_cachep, ioc);
15786db1e29SJens Axboe }
15886db1e29SJens Axboe EXPORT_SYMBOL(put_io_context);
15986db1e29SJens Axboe 
160f6e8d01bSTejun Heo /**
161f6e8d01bSTejun Heo  * put_io_context_active - put active reference on ioc
162f6e8d01bSTejun Heo  * @ioc: ioc of interest
163f6e8d01bSTejun Heo  *
164f6e8d01bSTejun Heo  * Undo get_io_context_active().  If active reference reaches zero after
165f6e8d01bSTejun Heo  * put, @ioc can never issue further IOs and ioscheds are notified.
166f6e8d01bSTejun Heo  */
167f6e8d01bSTejun Heo void put_io_context_active(struct io_context *ioc)
16886db1e29SJens Axboe {
169621032adSTejun Heo 	unsigned long flags;
170f6e8d01bSTejun Heo 	struct io_cq *icq;
17186db1e29SJens Axboe 
172f6e8d01bSTejun Heo 	if (!atomic_dec_and_test(&ioc->active_ref)) {
173621032adSTejun Heo 		put_io_context(ioc);
174621032adSTejun Heo 		return;
175621032adSTejun Heo 	}
176621032adSTejun Heo 
177621032adSTejun Heo 	/*
178621032adSTejun Heo 	 * Need ioc lock to walk icq_list and q lock to exit icq.  Perform
179621032adSTejun Heo 	 * reverse double locking.  Read comment in ioc_release_fn() for
180621032adSTejun Heo 	 * explanation on the nested locking annotation.
181621032adSTejun Heo 	 */
182621032adSTejun Heo retry:
183621032adSTejun Heo 	spin_lock_irqsave_nested(&ioc->lock, flags, 1);
184b67bfe0dSSasha Levin 	hlist_for_each_entry(icq, &ioc->icq_list, ioc_node) {
185621032adSTejun Heo 		if (icq->flags & ICQ_EXITED)
186621032adSTejun Heo 			continue;
187621032adSTejun Heo 		if (spin_trylock(icq->q->queue_lock)) {
188621032adSTejun Heo 			ioc_exit_icq(icq);
189621032adSTejun Heo 			spin_unlock(icq->q->queue_lock);
190621032adSTejun Heo 		} else {
191621032adSTejun Heo 			spin_unlock_irqrestore(&ioc->lock, flags);
192621032adSTejun Heo 			cpu_relax();
193621032adSTejun Heo 			goto retry;
194621032adSTejun Heo 		}
195621032adSTejun Heo 	}
196621032adSTejun Heo 	spin_unlock_irqrestore(&ioc->lock, flags);
197621032adSTejun Heo 
19811a3122fSTejun Heo 	put_io_context(ioc);
19986db1e29SJens Axboe }
20086db1e29SJens Axboe 
201f6e8d01bSTejun Heo /* Called by the exiting task */
202f6e8d01bSTejun Heo void exit_io_context(struct task_struct *task)
203f6e8d01bSTejun Heo {
204f6e8d01bSTejun Heo 	struct io_context *ioc;
205f6e8d01bSTejun Heo 
206f6e8d01bSTejun Heo 	task_lock(task);
207f6e8d01bSTejun Heo 	ioc = task->io_context;
208f6e8d01bSTejun Heo 	task->io_context = NULL;
209f6e8d01bSTejun Heo 	task_unlock(task);
210f6e8d01bSTejun Heo 
211f6e8d01bSTejun Heo 	atomic_dec(&ioc->nr_tasks);
212f6e8d01bSTejun Heo 	put_io_context_active(ioc);
213f6e8d01bSTejun Heo }
214f6e8d01bSTejun Heo 
2157e5a8794STejun Heo /**
2167e5a8794STejun Heo  * ioc_clear_queue - break any ioc association with the specified queue
2177e5a8794STejun Heo  * @q: request_queue being cleared
2187e5a8794STejun Heo  *
2197e5a8794STejun Heo  * Walk @q->icq_list and exit all io_cq's.  Must be called with @q locked.
2207e5a8794STejun Heo  */
2217e5a8794STejun Heo void ioc_clear_queue(struct request_queue *q)
2227e5a8794STejun Heo {
2237e5a8794STejun Heo 	lockdep_assert_held(q->queue_lock);
2247e5a8794STejun Heo 
2257e5a8794STejun Heo 	while (!list_empty(&q->icq_list)) {
2267e5a8794STejun Heo 		struct io_cq *icq = list_entry(q->icq_list.next,
2277e5a8794STejun Heo 					       struct io_cq, q_node);
2287e5a8794STejun Heo 		struct io_context *ioc = icq->ioc;
2297e5a8794STejun Heo 
2307e5a8794STejun Heo 		spin_lock(&ioc->lock);
231621032adSTejun Heo 		ioc_destroy_icq(icq);
2327e5a8794STejun Heo 		spin_unlock(&ioc->lock);
2337e5a8794STejun Heo 	}
2347e5a8794STejun Heo }
2357e5a8794STejun Heo 
23624acfc34STejun Heo int create_task_io_context(struct task_struct *task, gfp_t gfp_flags, int node)
23786db1e29SJens Axboe {
238df415656SPaul Bolle 	struct io_context *ioc;
2393c9c708cSEric Dumazet 	int ret;
24086db1e29SJens Axboe 
24142ec57a8STejun Heo 	ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO,
24242ec57a8STejun Heo 				    node);
24342ec57a8STejun Heo 	if (unlikely(!ioc))
24424acfc34STejun Heo 		return -ENOMEM;
24542ec57a8STejun Heo 
24642ec57a8STejun Heo 	/* initialize */
247df415656SPaul Bolle 	atomic_long_set(&ioc->refcount, 1);
2484638a83eSOlof Johansson 	atomic_set(&ioc->nr_tasks, 1);
249f6e8d01bSTejun Heo 	atomic_set(&ioc->active_ref, 1);
250df415656SPaul Bolle 	spin_lock_init(&ioc->lock);
251c5869807STejun Heo 	INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC | __GFP_HIGH);
252c5869807STejun Heo 	INIT_HLIST_HEAD(&ioc->icq_list);
253b2efa052STejun Heo 	INIT_WORK(&ioc->release_work, ioc_release_fn);
25486db1e29SJens Axboe 
255fd638368STejun Heo 	/*
256fd638368STejun Heo 	 * Try to install.  ioc shouldn't be installed if someone else
257fd638368STejun Heo 	 * already did or @task, which isn't %current, is exiting.  Note
258fd638368STejun Heo 	 * that we need to allow ioc creation on exiting %current as exit
259fd638368STejun Heo 	 * path may issue IOs from e.g. exit_files().  The exit path is
260fd638368STejun Heo 	 * responsible for not issuing IO after exit_io_context().
261fd638368STejun Heo 	 */
2626e736be7STejun Heo 	task_lock(task);
263fd638368STejun Heo 	if (!task->io_context &&
264fd638368STejun Heo 	    (task == current || !(task->flags & PF_EXITING)))
2656e736be7STejun Heo 		task->io_context = ioc;
266f2dbd76aSTejun Heo 	else
2676e736be7STejun Heo 		kmem_cache_free(iocontext_cachep, ioc);
2683c9c708cSEric Dumazet 
2693c9c708cSEric Dumazet 	ret = task->io_context ? 0 : -EBUSY;
2703c9c708cSEric Dumazet 
2716e736be7STejun Heo 	task_unlock(task);
27224acfc34STejun Heo 
2733c9c708cSEric Dumazet 	return ret;
27486db1e29SJens Axboe }
27586db1e29SJens Axboe 
2766e736be7STejun Heo /**
2776e736be7STejun Heo  * get_task_io_context - get io_context of a task
2786e736be7STejun Heo  * @task: task of interest
2796e736be7STejun Heo  * @gfp_flags: allocation flags, used if allocation is necessary
2806e736be7STejun Heo  * @node: allocation node, used if allocation is necessary
28186db1e29SJens Axboe  *
2826e736be7STejun Heo  * Return io_context of @task.  If it doesn't exist, it is created with
2836e736be7STejun Heo  * @gfp_flags and @node.  The returned io_context has its reference count
2846e736be7STejun Heo  * incremented.
2856e736be7STejun Heo  *
2866e736be7STejun Heo  * This function always goes through task_lock() and it's better to use
287f2dbd76aSTejun Heo  * %current->io_context + get_io_context() for %current.
28886db1e29SJens Axboe  */
2896e736be7STejun Heo struct io_context *get_task_io_context(struct task_struct *task,
2906e736be7STejun Heo 				       gfp_t gfp_flags, int node)
29186db1e29SJens Axboe {
2926e736be7STejun Heo 	struct io_context *ioc;
29386db1e29SJens Axboe 
294d0164adcSMel Gorman 	might_sleep_if(gfpflags_allow_blocking(gfp_flags));
29586db1e29SJens Axboe 
296f2dbd76aSTejun Heo 	do {
2976e736be7STejun Heo 		task_lock(task);
2986e736be7STejun Heo 		ioc = task->io_context;
2996e736be7STejun Heo 		if (likely(ioc)) {
3006e736be7STejun Heo 			get_io_context(ioc);
3016e736be7STejun Heo 			task_unlock(task);
302df415656SPaul Bolle 			return ioc;
30386db1e29SJens Axboe 		}
3046e736be7STejun Heo 		task_unlock(task);
30524acfc34STejun Heo 	} while (!create_task_io_context(task, gfp_flags, node));
3066e736be7STejun Heo 
307f2dbd76aSTejun Heo 	return NULL;
3086e736be7STejun Heo }
3096e736be7STejun Heo EXPORT_SYMBOL(get_task_io_context);
31086db1e29SJens Axboe 
31147fdd4caSTejun Heo /**
31247fdd4caSTejun Heo  * ioc_lookup_icq - lookup io_cq from ioc
31347fdd4caSTejun Heo  * @ioc: the associated io_context
31447fdd4caSTejun Heo  * @q: the associated request_queue
31547fdd4caSTejun Heo  *
31647fdd4caSTejun Heo  * Look up io_cq associated with @ioc - @q pair from @ioc.  Must be called
31747fdd4caSTejun Heo  * with @q->queue_lock held.
31847fdd4caSTejun Heo  */
31947fdd4caSTejun Heo struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q)
32047fdd4caSTejun Heo {
32147fdd4caSTejun Heo 	struct io_cq *icq;
32247fdd4caSTejun Heo 
32347fdd4caSTejun Heo 	lockdep_assert_held(q->queue_lock);
32447fdd4caSTejun Heo 
32547fdd4caSTejun Heo 	/*
32647fdd4caSTejun Heo 	 * icq's are indexed from @ioc using radix tree and hint pointer,
32747fdd4caSTejun Heo 	 * both of which are protected with RCU.  All removals are done
32847fdd4caSTejun Heo 	 * holding both q and ioc locks, and we're holding q lock - if we
32947fdd4caSTejun Heo 	 * find a icq which points to us, it's guaranteed to be valid.
33047fdd4caSTejun Heo 	 */
33147fdd4caSTejun Heo 	rcu_read_lock();
33247fdd4caSTejun Heo 	icq = rcu_dereference(ioc->icq_hint);
33347fdd4caSTejun Heo 	if (icq && icq->q == q)
33447fdd4caSTejun Heo 		goto out;
33547fdd4caSTejun Heo 
33647fdd4caSTejun Heo 	icq = radix_tree_lookup(&ioc->icq_tree, q->id);
33747fdd4caSTejun Heo 	if (icq && icq->q == q)
33847fdd4caSTejun Heo 		rcu_assign_pointer(ioc->icq_hint, icq);	/* allowed to race */
33947fdd4caSTejun Heo 	else
34047fdd4caSTejun Heo 		icq = NULL;
34147fdd4caSTejun Heo out:
34247fdd4caSTejun Heo 	rcu_read_unlock();
34347fdd4caSTejun Heo 	return icq;
34447fdd4caSTejun Heo }
34547fdd4caSTejun Heo EXPORT_SYMBOL(ioc_lookup_icq);
34647fdd4caSTejun Heo 
347f1f8cc94STejun Heo /**
348f1f8cc94STejun Heo  * ioc_create_icq - create and link io_cq
34924acfc34STejun Heo  * @ioc: io_context of interest
350f1f8cc94STejun Heo  * @q: request_queue of interest
351f1f8cc94STejun Heo  * @gfp_mask: allocation mask
352f1f8cc94STejun Heo  *
35324acfc34STejun Heo  * Make sure io_cq linking @ioc and @q exists.  If icq doesn't exist, they
35424acfc34STejun Heo  * will be created using @gfp_mask.
355f1f8cc94STejun Heo  *
356f1f8cc94STejun Heo  * The caller is responsible for ensuring @ioc won't go away and @q is
357f1f8cc94STejun Heo  * alive and will stay alive until this function returns.
358f1f8cc94STejun Heo  */
35924acfc34STejun Heo struct io_cq *ioc_create_icq(struct io_context *ioc, struct request_queue *q,
36024acfc34STejun Heo 			     gfp_t gfp_mask)
361f1f8cc94STejun Heo {
362f1f8cc94STejun Heo 	struct elevator_type *et = q->elevator->type;
363f1f8cc94STejun Heo 	struct io_cq *icq;
364f1f8cc94STejun Heo 
365f1f8cc94STejun Heo 	/* allocate stuff */
366f1f8cc94STejun Heo 	icq = kmem_cache_alloc_node(et->icq_cache, gfp_mask | __GFP_ZERO,
367f1f8cc94STejun Heo 				    q->node);
368f1f8cc94STejun Heo 	if (!icq)
369f1f8cc94STejun Heo 		return NULL;
370f1f8cc94STejun Heo 
3715e4c0d97SJan Kara 	if (radix_tree_maybe_preload(gfp_mask) < 0) {
372f1f8cc94STejun Heo 		kmem_cache_free(et->icq_cache, icq);
373f1f8cc94STejun Heo 		return NULL;
374f1f8cc94STejun Heo 	}
375f1f8cc94STejun Heo 
376f1f8cc94STejun Heo 	icq->ioc = ioc;
377f1f8cc94STejun Heo 	icq->q = q;
378f1f8cc94STejun Heo 	INIT_LIST_HEAD(&icq->q_node);
379f1f8cc94STejun Heo 	INIT_HLIST_NODE(&icq->ioc_node);
380f1f8cc94STejun Heo 
381f1f8cc94STejun Heo 	/* lock both q and ioc and try to link @icq */
382f1f8cc94STejun Heo 	spin_lock_irq(q->queue_lock);
383f1f8cc94STejun Heo 	spin_lock(&ioc->lock);
384f1f8cc94STejun Heo 
385f1f8cc94STejun Heo 	if (likely(!radix_tree_insert(&ioc->icq_tree, q->id, icq))) {
386f1f8cc94STejun Heo 		hlist_add_head(&icq->ioc_node, &ioc->icq_list);
387f1f8cc94STejun Heo 		list_add(&icq->q_node, &q->icq_list);
388bd166ef1SJens Axboe 		if (et->uses_mq && et->ops.mq.init_icq)
389bd166ef1SJens Axboe 			et->ops.mq.init_icq(icq);
390bd166ef1SJens Axboe 		else if (!et->uses_mq && et->ops.sq.elevator_init_icq_fn)
391c51ca6cfSJens Axboe 			et->ops.sq.elevator_init_icq_fn(icq);
392f1f8cc94STejun Heo 	} else {
393f1f8cc94STejun Heo 		kmem_cache_free(et->icq_cache, icq);
394f1f8cc94STejun Heo 		icq = ioc_lookup_icq(ioc, q);
395f1f8cc94STejun Heo 		if (!icq)
396f1f8cc94STejun Heo 			printk(KERN_ERR "cfq: icq link failed!\n");
397f1f8cc94STejun Heo 	}
398f1f8cc94STejun Heo 
399f1f8cc94STejun Heo 	spin_unlock(&ioc->lock);
400f1f8cc94STejun Heo 	spin_unlock_irq(q->queue_lock);
401f1f8cc94STejun Heo 	radix_tree_preload_end();
402f1f8cc94STejun Heo 	return icq;
403f1f8cc94STejun Heo }
404f1f8cc94STejun Heo 
40513341598SAdrian Bunk static int __init blk_ioc_init(void)
40686db1e29SJens Axboe {
40786db1e29SJens Axboe 	iocontext_cachep = kmem_cache_create("blkdev_ioc",
40886db1e29SJens Axboe 			sizeof(struct io_context), 0, SLAB_PANIC, NULL);
40986db1e29SJens Axboe 	return 0;
41086db1e29SJens Axboe }
41186db1e29SJens Axboe subsys_initcall(blk_ioc_init);
412