xref: /openbmc/linux/block/blk-ioc.c (revision c51ca6cf)
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 
46c51ca6cfSJens Axboe 	if (et->ops.sq.elevator_exit_icq_fn)
47c51ca6cfSJens Axboe 		et->ops.sq.elevator_exit_icq_fn(icq);
48621032adSTejun Heo 
49621032adSTejun Heo 	icq->flags |= ICQ_EXITED;
50621032adSTejun Heo }
51621032adSTejun Heo 
52621032adSTejun Heo /* Release an icq.  Called with both ioc and q locked. */
53621032adSTejun Heo static void ioc_destroy_icq(struct io_cq *icq)
54621032adSTejun Heo {
557e5a8794STejun Heo 	struct io_context *ioc = icq->ioc;
567e5a8794STejun Heo 	struct request_queue *q = icq->q;
577e5a8794STejun Heo 	struct elevator_type *et = q->elevator->type;
587e5a8794STejun Heo 
597e5a8794STejun Heo 	lockdep_assert_held(&ioc->lock);
607e5a8794STejun Heo 	lockdep_assert_held(q->queue_lock);
617e5a8794STejun Heo 
627e5a8794STejun Heo 	radix_tree_delete(&ioc->icq_tree, icq->q->id);
637e5a8794STejun Heo 	hlist_del_init(&icq->ioc_node);
647e5a8794STejun Heo 	list_del_init(&icq->q_node);
657e5a8794STejun Heo 
667e5a8794STejun Heo 	/*
677e5a8794STejun Heo 	 * Both setting lookup hint to and clearing it from @icq are done
687e5a8794STejun Heo 	 * under queue_lock.  If it's not pointing to @icq now, it never
697e5a8794STejun Heo 	 * will.  Hint assignment itself can race safely.
707e5a8794STejun Heo 	 */
71ec6c676aSPaul E. McKenney 	if (rcu_access_pointer(ioc->icq_hint) == icq)
727e5a8794STejun Heo 		rcu_assign_pointer(ioc->icq_hint, NULL);
737e5a8794STejun Heo 
74621032adSTejun Heo 	ioc_exit_icq(icq);
757e5a8794STejun Heo 
767e5a8794STejun Heo 	/*
777e5a8794STejun Heo 	 * @icq->q might have gone away by the time RCU callback runs
787e5a8794STejun Heo 	 * making it impossible to determine icq_cache.  Record it in @icq.
797e5a8794STejun Heo 	 */
807e5a8794STejun Heo 	icq->__rcu_icq_cache = et->icq_cache;
817e5a8794STejun Heo 	call_rcu(&icq->__rcu_head, icq_free_icq_rcu);
827e5a8794STejun Heo }
837e5a8794STejun Heo 
84b2efa052STejun Heo /*
85b2efa052STejun Heo  * Slow path for ioc release in put_io_context().  Performs double-lock
86c5869807STejun Heo  * dancing to unlink all icq's and then frees ioc.
87b2efa052STejun Heo  */
88b2efa052STejun Heo static void ioc_release_fn(struct work_struct *work)
89b2efa052STejun Heo {
90b2efa052STejun Heo 	struct io_context *ioc = container_of(work, struct io_context,
91b2efa052STejun Heo 					      release_work);
92d8c66c5dSTejun Heo 	unsigned long flags;
93b2efa052STejun Heo 
94d8c66c5dSTejun Heo 	/*
95d8c66c5dSTejun Heo 	 * Exiting icq may call into put_io_context() through elevator
96d8c66c5dSTejun Heo 	 * which will trigger lockdep warning.  The ioc's are guaranteed to
97d8c66c5dSTejun Heo 	 * be different, use a different locking subclass here.  Use
98d8c66c5dSTejun Heo 	 * irqsave variant as there's no spin_lock_irq_nested().
99d8c66c5dSTejun Heo 	 */
100d8c66c5dSTejun Heo 	spin_lock_irqsave_nested(&ioc->lock, flags, 1);
101b2efa052STejun Heo 
102c5869807STejun Heo 	while (!hlist_empty(&ioc->icq_list)) {
103c5869807STejun Heo 		struct io_cq *icq = hlist_entry(ioc->icq_list.first,
104c5869807STejun Heo 						struct io_cq, ioc_node);
1052274b029STejun Heo 		struct request_queue *q = icq->q;
106b2efa052STejun Heo 
1072274b029STejun Heo 		if (spin_trylock(q->queue_lock)) {
108621032adSTejun Heo 			ioc_destroy_icq(icq);
1092274b029STejun Heo 			spin_unlock(q->queue_lock);
110b2efa052STejun Heo 		} else {
111d8c66c5dSTejun Heo 			spin_unlock_irqrestore(&ioc->lock, flags);
1122274b029STejun Heo 			cpu_relax();
1132274b029STejun Heo 			spin_lock_irqsave_nested(&ioc->lock, flags, 1);
114b2efa052STejun Heo 		}
1152274b029STejun Heo 	}
1162274b029STejun Heo 
1172274b029STejun Heo 	spin_unlock_irqrestore(&ioc->lock, flags);
118b2efa052STejun Heo 
119b2efa052STejun Heo 	kmem_cache_free(iocontext_cachep, ioc);
12086db1e29SJens Axboe }
12186db1e29SJens Axboe 
12242ec57a8STejun Heo /**
12342ec57a8STejun Heo  * put_io_context - put a reference of io_context
12442ec57a8STejun Heo  * @ioc: io_context to put
12542ec57a8STejun Heo  *
12642ec57a8STejun Heo  * Decrement reference count of @ioc and release it if the count reaches
12711a3122fSTejun Heo  * zero.
12886db1e29SJens Axboe  */
12911a3122fSTejun Heo void put_io_context(struct io_context *ioc)
13086db1e29SJens Axboe {
131b2efa052STejun Heo 	unsigned long flags;
132ff8c1474SXiaotian Feng 	bool free_ioc = false;
133b2efa052STejun Heo 
13486db1e29SJens Axboe 	if (ioc == NULL)
13542ec57a8STejun Heo 		return;
13686db1e29SJens Axboe 
13742ec57a8STejun Heo 	BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
13842ec57a8STejun Heo 
139b2efa052STejun Heo 	/*
14011a3122fSTejun Heo 	 * Releasing ioc requires reverse order double locking and we may
14111a3122fSTejun Heo 	 * already be holding a queue_lock.  Do it asynchronously from wq.
142b2efa052STejun Heo 	 */
14311a3122fSTejun Heo 	if (atomic_long_dec_and_test(&ioc->refcount)) {
14411a3122fSTejun Heo 		spin_lock_irqsave(&ioc->lock, flags);
14511a3122fSTejun Heo 		if (!hlist_empty(&ioc->icq_list))
146695588f9SViresh Kumar 			queue_work(system_power_efficient_wq,
147695588f9SViresh Kumar 					&ioc->release_work);
148ff8c1474SXiaotian Feng 		else
149ff8c1474SXiaotian Feng 			free_ioc = true;
15011a3122fSTejun Heo 		spin_unlock_irqrestore(&ioc->lock, flags);
15111a3122fSTejun Heo 	}
152ff8c1474SXiaotian Feng 
153ff8c1474SXiaotian Feng 	if (free_ioc)
154ff8c1474SXiaotian Feng 		kmem_cache_free(iocontext_cachep, ioc);
15586db1e29SJens Axboe }
15686db1e29SJens Axboe EXPORT_SYMBOL(put_io_context);
15786db1e29SJens Axboe 
158f6e8d01bSTejun Heo /**
159f6e8d01bSTejun Heo  * put_io_context_active - put active reference on ioc
160f6e8d01bSTejun Heo  * @ioc: ioc of interest
161f6e8d01bSTejun Heo  *
162f6e8d01bSTejun Heo  * Undo get_io_context_active().  If active reference reaches zero after
163f6e8d01bSTejun Heo  * put, @ioc can never issue further IOs and ioscheds are notified.
164f6e8d01bSTejun Heo  */
165f6e8d01bSTejun Heo void put_io_context_active(struct io_context *ioc)
16686db1e29SJens Axboe {
167621032adSTejun Heo 	unsigned long flags;
168f6e8d01bSTejun Heo 	struct io_cq *icq;
16986db1e29SJens Axboe 
170f6e8d01bSTejun Heo 	if (!atomic_dec_and_test(&ioc->active_ref)) {
171621032adSTejun Heo 		put_io_context(ioc);
172621032adSTejun Heo 		return;
173621032adSTejun Heo 	}
174621032adSTejun Heo 
175621032adSTejun Heo 	/*
176621032adSTejun Heo 	 * Need ioc lock to walk icq_list and q lock to exit icq.  Perform
177621032adSTejun Heo 	 * reverse double locking.  Read comment in ioc_release_fn() for
178621032adSTejun Heo 	 * explanation on the nested locking annotation.
179621032adSTejun Heo 	 */
180621032adSTejun Heo retry:
181621032adSTejun Heo 	spin_lock_irqsave_nested(&ioc->lock, flags, 1);
182b67bfe0dSSasha Levin 	hlist_for_each_entry(icq, &ioc->icq_list, ioc_node) {
183621032adSTejun Heo 		if (icq->flags & ICQ_EXITED)
184621032adSTejun Heo 			continue;
185621032adSTejun Heo 		if (spin_trylock(icq->q->queue_lock)) {
186621032adSTejun Heo 			ioc_exit_icq(icq);
187621032adSTejun Heo 			spin_unlock(icq->q->queue_lock);
188621032adSTejun Heo 		} else {
189621032adSTejun Heo 			spin_unlock_irqrestore(&ioc->lock, flags);
190621032adSTejun Heo 			cpu_relax();
191621032adSTejun Heo 			goto retry;
192621032adSTejun Heo 		}
193621032adSTejun Heo 	}
194621032adSTejun Heo 	spin_unlock_irqrestore(&ioc->lock, flags);
195621032adSTejun Heo 
19611a3122fSTejun Heo 	put_io_context(ioc);
19786db1e29SJens Axboe }
19886db1e29SJens Axboe 
199f6e8d01bSTejun Heo /* Called by the exiting task */
200f6e8d01bSTejun Heo void exit_io_context(struct task_struct *task)
201f6e8d01bSTejun Heo {
202f6e8d01bSTejun Heo 	struct io_context *ioc;
203f6e8d01bSTejun Heo 
204f6e8d01bSTejun Heo 	task_lock(task);
205f6e8d01bSTejun Heo 	ioc = task->io_context;
206f6e8d01bSTejun Heo 	task->io_context = NULL;
207f6e8d01bSTejun Heo 	task_unlock(task);
208f6e8d01bSTejun Heo 
209f6e8d01bSTejun Heo 	atomic_dec(&ioc->nr_tasks);
210f6e8d01bSTejun Heo 	put_io_context_active(ioc);
211f6e8d01bSTejun Heo }
212f6e8d01bSTejun Heo 
2137e5a8794STejun Heo /**
2147e5a8794STejun Heo  * ioc_clear_queue - break any ioc association with the specified queue
2157e5a8794STejun Heo  * @q: request_queue being cleared
2167e5a8794STejun Heo  *
2177e5a8794STejun Heo  * Walk @q->icq_list and exit all io_cq's.  Must be called with @q locked.
2187e5a8794STejun Heo  */
2197e5a8794STejun Heo void ioc_clear_queue(struct request_queue *q)
2207e5a8794STejun Heo {
2217e5a8794STejun Heo 	lockdep_assert_held(q->queue_lock);
2227e5a8794STejun Heo 
2237e5a8794STejun Heo 	while (!list_empty(&q->icq_list)) {
2247e5a8794STejun Heo 		struct io_cq *icq = list_entry(q->icq_list.next,
2257e5a8794STejun Heo 					       struct io_cq, q_node);
2267e5a8794STejun Heo 		struct io_context *ioc = icq->ioc;
2277e5a8794STejun Heo 
2287e5a8794STejun Heo 		spin_lock(&ioc->lock);
229621032adSTejun Heo 		ioc_destroy_icq(icq);
2307e5a8794STejun Heo 		spin_unlock(&ioc->lock);
2317e5a8794STejun Heo 	}
2327e5a8794STejun Heo }
2337e5a8794STejun Heo 
23424acfc34STejun Heo int create_task_io_context(struct task_struct *task, gfp_t gfp_flags, int node)
23586db1e29SJens Axboe {
236df415656SPaul Bolle 	struct io_context *ioc;
2373c9c708cSEric Dumazet 	int ret;
23886db1e29SJens Axboe 
23942ec57a8STejun Heo 	ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO,
24042ec57a8STejun Heo 				    node);
24142ec57a8STejun Heo 	if (unlikely(!ioc))
24224acfc34STejun Heo 		return -ENOMEM;
24342ec57a8STejun Heo 
24442ec57a8STejun Heo 	/* initialize */
245df415656SPaul Bolle 	atomic_long_set(&ioc->refcount, 1);
2464638a83eSOlof Johansson 	atomic_set(&ioc->nr_tasks, 1);
247f6e8d01bSTejun Heo 	atomic_set(&ioc->active_ref, 1);
248df415656SPaul Bolle 	spin_lock_init(&ioc->lock);
249c5869807STejun Heo 	INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC | __GFP_HIGH);
250c5869807STejun Heo 	INIT_HLIST_HEAD(&ioc->icq_list);
251b2efa052STejun Heo 	INIT_WORK(&ioc->release_work, ioc_release_fn);
25286db1e29SJens Axboe 
253fd638368STejun Heo 	/*
254fd638368STejun Heo 	 * Try to install.  ioc shouldn't be installed if someone else
255fd638368STejun Heo 	 * already did or @task, which isn't %current, is exiting.  Note
256fd638368STejun Heo 	 * that we need to allow ioc creation on exiting %current as exit
257fd638368STejun Heo 	 * path may issue IOs from e.g. exit_files().  The exit path is
258fd638368STejun Heo 	 * responsible for not issuing IO after exit_io_context().
259fd638368STejun Heo 	 */
2606e736be7STejun Heo 	task_lock(task);
261fd638368STejun Heo 	if (!task->io_context &&
262fd638368STejun Heo 	    (task == current || !(task->flags & PF_EXITING)))
2636e736be7STejun Heo 		task->io_context = ioc;
264f2dbd76aSTejun Heo 	else
2656e736be7STejun Heo 		kmem_cache_free(iocontext_cachep, ioc);
2663c9c708cSEric Dumazet 
2673c9c708cSEric Dumazet 	ret = task->io_context ? 0 : -EBUSY;
2683c9c708cSEric Dumazet 
2696e736be7STejun Heo 	task_unlock(task);
27024acfc34STejun Heo 
2713c9c708cSEric Dumazet 	return ret;
27286db1e29SJens Axboe }
27386db1e29SJens Axboe 
2746e736be7STejun Heo /**
2756e736be7STejun Heo  * get_task_io_context - get io_context of a task
2766e736be7STejun Heo  * @task: task of interest
2776e736be7STejun Heo  * @gfp_flags: allocation flags, used if allocation is necessary
2786e736be7STejun Heo  * @node: allocation node, used if allocation is necessary
27986db1e29SJens Axboe  *
2806e736be7STejun Heo  * Return io_context of @task.  If it doesn't exist, it is created with
2816e736be7STejun Heo  * @gfp_flags and @node.  The returned io_context has its reference count
2826e736be7STejun Heo  * incremented.
2836e736be7STejun Heo  *
2846e736be7STejun Heo  * This function always goes through task_lock() and it's better to use
285f2dbd76aSTejun Heo  * %current->io_context + get_io_context() for %current.
28686db1e29SJens Axboe  */
2876e736be7STejun Heo struct io_context *get_task_io_context(struct task_struct *task,
2886e736be7STejun Heo 				       gfp_t gfp_flags, int node)
28986db1e29SJens Axboe {
2906e736be7STejun Heo 	struct io_context *ioc;
29186db1e29SJens Axboe 
292d0164adcSMel Gorman 	might_sleep_if(gfpflags_allow_blocking(gfp_flags));
29386db1e29SJens Axboe 
294f2dbd76aSTejun Heo 	do {
2956e736be7STejun Heo 		task_lock(task);
2966e736be7STejun Heo 		ioc = task->io_context;
2976e736be7STejun Heo 		if (likely(ioc)) {
2986e736be7STejun Heo 			get_io_context(ioc);
2996e736be7STejun Heo 			task_unlock(task);
300df415656SPaul Bolle 			return ioc;
30186db1e29SJens Axboe 		}
3026e736be7STejun Heo 		task_unlock(task);
30324acfc34STejun Heo 	} while (!create_task_io_context(task, gfp_flags, node));
3046e736be7STejun Heo 
305f2dbd76aSTejun Heo 	return NULL;
3066e736be7STejun Heo }
3076e736be7STejun Heo EXPORT_SYMBOL(get_task_io_context);
30886db1e29SJens Axboe 
30947fdd4caSTejun Heo /**
31047fdd4caSTejun Heo  * ioc_lookup_icq - lookup io_cq from ioc
31147fdd4caSTejun Heo  * @ioc: the associated io_context
31247fdd4caSTejun Heo  * @q: the associated request_queue
31347fdd4caSTejun Heo  *
31447fdd4caSTejun Heo  * Look up io_cq associated with @ioc - @q pair from @ioc.  Must be called
31547fdd4caSTejun Heo  * with @q->queue_lock held.
31647fdd4caSTejun Heo  */
31747fdd4caSTejun Heo struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q)
31847fdd4caSTejun Heo {
31947fdd4caSTejun Heo 	struct io_cq *icq;
32047fdd4caSTejun Heo 
32147fdd4caSTejun Heo 	lockdep_assert_held(q->queue_lock);
32247fdd4caSTejun Heo 
32347fdd4caSTejun Heo 	/*
32447fdd4caSTejun Heo 	 * icq's are indexed from @ioc using radix tree and hint pointer,
32547fdd4caSTejun Heo 	 * both of which are protected with RCU.  All removals are done
32647fdd4caSTejun Heo 	 * holding both q and ioc locks, and we're holding q lock - if we
32747fdd4caSTejun Heo 	 * find a icq which points to us, it's guaranteed to be valid.
32847fdd4caSTejun Heo 	 */
32947fdd4caSTejun Heo 	rcu_read_lock();
33047fdd4caSTejun Heo 	icq = rcu_dereference(ioc->icq_hint);
33147fdd4caSTejun Heo 	if (icq && icq->q == q)
33247fdd4caSTejun Heo 		goto out;
33347fdd4caSTejun Heo 
33447fdd4caSTejun Heo 	icq = radix_tree_lookup(&ioc->icq_tree, q->id);
33547fdd4caSTejun Heo 	if (icq && icq->q == q)
33647fdd4caSTejun Heo 		rcu_assign_pointer(ioc->icq_hint, icq);	/* allowed to race */
33747fdd4caSTejun Heo 	else
33847fdd4caSTejun Heo 		icq = NULL;
33947fdd4caSTejun Heo out:
34047fdd4caSTejun Heo 	rcu_read_unlock();
34147fdd4caSTejun Heo 	return icq;
34247fdd4caSTejun Heo }
34347fdd4caSTejun Heo EXPORT_SYMBOL(ioc_lookup_icq);
34447fdd4caSTejun Heo 
345f1f8cc94STejun Heo /**
346f1f8cc94STejun Heo  * ioc_create_icq - create and link io_cq
34724acfc34STejun Heo  * @ioc: io_context of interest
348f1f8cc94STejun Heo  * @q: request_queue of interest
349f1f8cc94STejun Heo  * @gfp_mask: allocation mask
350f1f8cc94STejun Heo  *
35124acfc34STejun Heo  * Make sure io_cq linking @ioc and @q exists.  If icq doesn't exist, they
35224acfc34STejun Heo  * will be created using @gfp_mask.
353f1f8cc94STejun Heo  *
354f1f8cc94STejun Heo  * The caller is responsible for ensuring @ioc won't go away and @q is
355f1f8cc94STejun Heo  * alive and will stay alive until this function returns.
356f1f8cc94STejun Heo  */
35724acfc34STejun Heo struct io_cq *ioc_create_icq(struct io_context *ioc, struct request_queue *q,
35824acfc34STejun Heo 			     gfp_t gfp_mask)
359f1f8cc94STejun Heo {
360f1f8cc94STejun Heo 	struct elevator_type *et = q->elevator->type;
361f1f8cc94STejun Heo 	struct io_cq *icq;
362f1f8cc94STejun Heo 
363f1f8cc94STejun Heo 	/* allocate stuff */
364f1f8cc94STejun Heo 	icq = kmem_cache_alloc_node(et->icq_cache, gfp_mask | __GFP_ZERO,
365f1f8cc94STejun Heo 				    q->node);
366f1f8cc94STejun Heo 	if (!icq)
367f1f8cc94STejun Heo 		return NULL;
368f1f8cc94STejun Heo 
3695e4c0d97SJan Kara 	if (radix_tree_maybe_preload(gfp_mask) < 0) {
370f1f8cc94STejun Heo 		kmem_cache_free(et->icq_cache, icq);
371f1f8cc94STejun Heo 		return NULL;
372f1f8cc94STejun Heo 	}
373f1f8cc94STejun Heo 
374f1f8cc94STejun Heo 	icq->ioc = ioc;
375f1f8cc94STejun Heo 	icq->q = q;
376f1f8cc94STejun Heo 	INIT_LIST_HEAD(&icq->q_node);
377f1f8cc94STejun Heo 	INIT_HLIST_NODE(&icq->ioc_node);
378f1f8cc94STejun Heo 
379f1f8cc94STejun Heo 	/* lock both q and ioc and try to link @icq */
380f1f8cc94STejun Heo 	spin_lock_irq(q->queue_lock);
381f1f8cc94STejun Heo 	spin_lock(&ioc->lock);
382f1f8cc94STejun Heo 
383f1f8cc94STejun Heo 	if (likely(!radix_tree_insert(&ioc->icq_tree, q->id, icq))) {
384f1f8cc94STejun Heo 		hlist_add_head(&icq->ioc_node, &ioc->icq_list);
385f1f8cc94STejun Heo 		list_add(&icq->q_node, &q->icq_list);
386c51ca6cfSJens Axboe 		if (et->ops.sq.elevator_init_icq_fn)
387c51ca6cfSJens Axboe 			et->ops.sq.elevator_init_icq_fn(icq);
388f1f8cc94STejun Heo 	} else {
389f1f8cc94STejun Heo 		kmem_cache_free(et->icq_cache, icq);
390f1f8cc94STejun Heo 		icq = ioc_lookup_icq(ioc, q);
391f1f8cc94STejun Heo 		if (!icq)
392f1f8cc94STejun Heo 			printk(KERN_ERR "cfq: icq link failed!\n");
393f1f8cc94STejun Heo 	}
394f1f8cc94STejun Heo 
395f1f8cc94STejun Heo 	spin_unlock(&ioc->lock);
396f1f8cc94STejun Heo 	spin_unlock_irq(q->queue_lock);
397f1f8cc94STejun Heo 	radix_tree_preload_end();
398f1f8cc94STejun Heo 	return icq;
399f1f8cc94STejun Heo }
400f1f8cc94STejun Heo 
40113341598SAdrian Bunk static int __init blk_ioc_init(void)
40286db1e29SJens Axboe {
40386db1e29SJens Axboe 	iocontext_cachep = kmem_cache_create("blkdev_ioc",
40486db1e29SJens Axboe 			sizeof(struct io_context), 0, SLAB_PANIC, NULL);
40586db1e29SJens Axboe 	return 0;
40686db1e29SJens Axboe }
40786db1e29SJens Axboe subsys_initcall(blk_ioc_init);
408