xref: /linux/block/blk-ioc.c (revision a4a508df2aa34f8650afde54ea804321c618f45f)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
286db1e29SJens Axboe /*
386db1e29SJens Axboe  * Functions related to io context handling
486db1e29SJens Axboe  */
586db1e29SJens Axboe #include <linux/kernel.h>
686db1e29SJens Axboe #include <linux/module.h>
786db1e29SJens Axboe #include <linux/init.h>
886db1e29SJens Axboe #include <linux/bio.h>
986db1e29SJens Axboe #include <linux/blkdev.h>
105a0e3ad6STejun Heo #include <linux/slab.h>
11a411cd3cSChristoph Hellwig #include <linux/security.h>
12f719ff9bSIngo Molnar #include <linux/sched/task.h>
1386db1e29SJens Axboe 
1486db1e29SJens Axboe #include "blk.h"
152aa7745bSChristoph Hellwig #include "blk-mq-sched.h"
1686db1e29SJens Axboe 
1786db1e29SJens Axboe /*
1886db1e29SJens Axboe  * For io context allocations
1986db1e29SJens Axboe  */
2086db1e29SJens Axboe static struct kmem_cache *iocontext_cachep;
2186db1e29SJens Axboe 
225ef16305SChristoph Hellwig #ifdef CONFIG_BLK_ICQ
236e736be7STejun Heo /**
246e736be7STejun Heo  * get_io_context - increment reference count to io_context
256e736be7STejun Heo  * @ioc: io_context to get
266e736be7STejun Heo  *
276e736be7STejun Heo  * Increment reference count to @ioc.
286e736be7STejun Heo  */
get_io_context(struct io_context * ioc)2987dd1d63SChristoph Hellwig static void get_io_context(struct io_context *ioc)
306e736be7STejun Heo {
316e736be7STejun Heo 	BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
326e736be7STejun Heo 	atomic_long_inc(&ioc->refcount);
336e736be7STejun Heo }
346e736be7STejun Heo 
353d492c2eSOmar Sandoval /*
367b36a718SJens Axboe  * Exit an icq. Called with ioc locked for blk-mq, and with both ioc
377b36a718SJens Axboe  * and queue locked for legacy.
383d492c2eSOmar Sandoval  */
ioc_exit_icq(struct io_cq * icq)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 
46f9cd4bfeSJens Axboe 	if (et->ops.exit_icq)
47f9cd4bfeSJens Axboe 		et->ops.exit_icq(icq);
48621032adSTejun Heo 
49621032adSTejun Heo 	icq->flags |= ICQ_EXITED;
50621032adSTejun Heo }
51621032adSTejun Heo 
ioc_exit_icqs(struct io_context * ioc)524be8a2eaSChristoph Hellwig static void ioc_exit_icqs(struct io_context *ioc)
534be8a2eaSChristoph Hellwig {
544be8a2eaSChristoph Hellwig 	struct io_cq *icq;
554be8a2eaSChristoph Hellwig 
564be8a2eaSChristoph Hellwig 	spin_lock_irq(&ioc->lock);
574be8a2eaSChristoph Hellwig 	hlist_for_each_entry(icq, &ioc->icq_list, ioc_node)
584be8a2eaSChristoph Hellwig 		ioc_exit_icq(icq);
594be8a2eaSChristoph Hellwig 	spin_unlock_irq(&ioc->lock);
604be8a2eaSChristoph Hellwig }
614be8a2eaSChristoph Hellwig 
627b36a718SJens Axboe /*
637b36a718SJens Axboe  * Release an icq. Called with ioc locked for blk-mq, and with both ioc
647b36a718SJens Axboe  * and queue locked for legacy.
657b36a718SJens Axboe  */
ioc_destroy_icq(struct io_cq * icq)66621032adSTejun Heo static void ioc_destroy_icq(struct io_cq *icq)
67621032adSTejun Heo {
687e5a8794STejun Heo 	struct io_context *ioc = icq->ioc;
697e5a8794STejun Heo 	struct request_queue *q = icq->q;
707e5a8794STejun Heo 	struct elevator_type *et = q->elevator->type;
717e5a8794STejun Heo 
727e5a8794STejun Heo 	lockdep_assert_held(&ioc->lock);
735a0ac57cSYu Kuai 	lockdep_assert_held(&q->queue_lock);
745a0ac57cSYu Kuai 
755a0ac57cSYu Kuai 	if (icq->flags & ICQ_DESTROYED)
765a0ac57cSYu Kuai 		return;
777e5a8794STejun Heo 
787e5a8794STejun Heo 	radix_tree_delete(&ioc->icq_tree, icq->q->id);
797e5a8794STejun Heo 	hlist_del_init(&icq->ioc_node);
807e5a8794STejun Heo 	list_del_init(&icq->q_node);
817e5a8794STejun Heo 
827e5a8794STejun Heo 	/*
837e5a8794STejun Heo 	 * Both setting lookup hint to and clearing it from @icq are done
847e5a8794STejun Heo 	 * under queue_lock.  If it's not pointing to @icq now, it never
857e5a8794STejun Heo 	 * will.  Hint assignment itself can race safely.
867e5a8794STejun Heo 	 */
87ec6c676aSPaul E. McKenney 	if (rcu_access_pointer(ioc->icq_hint) == icq)
887e5a8794STejun Heo 		rcu_assign_pointer(ioc->icq_hint, NULL);
897e5a8794STejun Heo 
90621032adSTejun Heo 	ioc_exit_icq(icq);
917e5a8794STejun Heo 
927e5a8794STejun Heo 	/*
937e5a8794STejun Heo 	 * @icq->q might have gone away by the time RCU callback runs
947e5a8794STejun Heo 	 * making it impossible to determine icq_cache.  Record it in @icq.
957e5a8794STejun Heo 	 */
967e5a8794STejun Heo 	icq->__rcu_icq_cache = et->icq_cache;
9730a2da7bSSahitya Tummala 	icq->flags |= ICQ_DESTROYED;
9828878733SJulia Lawall 	kfree_rcu(icq, __rcu_head);
997e5a8794STejun Heo }
1007e5a8794STejun Heo 
101b2efa052STejun Heo /*
102b2efa052STejun Heo  * Slow path for ioc release in put_io_context().  Performs double-lock
103c5869807STejun Heo  * dancing to unlink all icq's and then frees ioc.
104b2efa052STejun Heo  */
ioc_release_fn(struct work_struct * work)105b2efa052STejun Heo static void ioc_release_fn(struct work_struct *work)
106b2efa052STejun Heo {
107b2efa052STejun Heo 	struct io_context *ioc = container_of(work, struct io_context,
108b2efa052STejun Heo 					      release_work);
109a43f085fSJohn Ogness 	spin_lock_irq(&ioc->lock);
110b2efa052STejun Heo 
111c5869807STejun Heo 	while (!hlist_empty(&ioc->icq_list)) {
112c5869807STejun Heo 		struct io_cq *icq = hlist_entry(ioc->icq_list.first,
113c5869807STejun Heo 						struct io_cq, ioc_node);
1142274b029STejun Heo 		struct request_queue *q = icq->q;
115b2efa052STejun Heo 
1160d945c1fSChristoph Hellwig 		if (spin_trylock(&q->queue_lock)) {
117621032adSTejun Heo 			ioc_destroy_icq(icq);
1180d945c1fSChristoph Hellwig 			spin_unlock(&q->queue_lock);
119b2efa052STejun Heo 		} else {
120ab96bbabSJohn Ogness 			/* Make sure q and icq cannot be freed. */
121ab96bbabSJohn Ogness 			rcu_read_lock();
122ab96bbabSJohn Ogness 
123ab96bbabSJohn Ogness 			/* Re-acquire the locks in the correct order. */
124ab96bbabSJohn Ogness 			spin_unlock(&ioc->lock);
125ab96bbabSJohn Ogness 			spin_lock(&q->queue_lock);
126ab96bbabSJohn Ogness 			spin_lock(&ioc->lock);
127ab96bbabSJohn Ogness 
128ab96bbabSJohn Ogness 			ioc_destroy_icq(icq);
129ab96bbabSJohn Ogness 
130ab96bbabSJohn Ogness 			spin_unlock(&q->queue_lock);
131ab96bbabSJohn Ogness 			rcu_read_unlock();
132b2efa052STejun Heo 		}
1332274b029STejun Heo 	}
1342274b029STejun Heo 
135a43f085fSJohn Ogness 	spin_unlock_irq(&ioc->lock);
136b2efa052STejun Heo 
137b2efa052STejun Heo 	kmem_cache_free(iocontext_cachep, ioc);
13886db1e29SJens Axboe }
13986db1e29SJens Axboe 
140edf70ff5SChristoph Hellwig /*
141edf70ff5SChristoph Hellwig  * Releasing icqs requires reverse order double locking and we may already be
142edf70ff5SChristoph Hellwig  * holding a queue_lock.  Do it asynchronously from a workqueue.
143edf70ff5SChristoph Hellwig  */
ioc_delay_free(struct io_context * ioc)144edf70ff5SChristoph Hellwig static bool ioc_delay_free(struct io_context *ioc)
145edf70ff5SChristoph Hellwig {
146edf70ff5SChristoph Hellwig 	unsigned long flags;
147edf70ff5SChristoph Hellwig 
148edf70ff5SChristoph Hellwig 	spin_lock_irqsave(&ioc->lock, flags);
149edf70ff5SChristoph Hellwig 	if (!hlist_empty(&ioc->icq_list)) {
150edf70ff5SChristoph Hellwig 		queue_work(system_power_efficient_wq, &ioc->release_work);
151edf70ff5SChristoph Hellwig 		spin_unlock_irqrestore(&ioc->lock, flags);
152edf70ff5SChristoph Hellwig 		return true;
153edf70ff5SChristoph Hellwig 	}
154edf70ff5SChristoph Hellwig 	spin_unlock_irqrestore(&ioc->lock, flags);
155edf70ff5SChristoph Hellwig 	return false;
156edf70ff5SChristoph Hellwig }
157edf70ff5SChristoph Hellwig 
15842ec57a8STejun Heo /**
1595ef16305SChristoph Hellwig  * ioc_clear_queue - break any ioc association with the specified queue
1605ef16305SChristoph Hellwig  * @q: request_queue being cleared
1615ef16305SChristoph Hellwig  *
1625ef16305SChristoph Hellwig  * Walk @q->icq_list and exit all io_cq's.
1635ef16305SChristoph Hellwig  */
ioc_clear_queue(struct request_queue * q)1645ef16305SChristoph Hellwig void ioc_clear_queue(struct request_queue *q)
1655ef16305SChristoph Hellwig {
1665ef16305SChristoph Hellwig 	spin_lock_irq(&q->queue_lock);
1675a0ac57cSYu Kuai 	while (!list_empty(&q->icq_list)) {
1685ef16305SChristoph Hellwig 		struct io_cq *icq =
1695a0ac57cSYu Kuai 			list_first_entry(&q->icq_list, struct io_cq, q_node);
1705ef16305SChristoph Hellwig 
1715a0ac57cSYu Kuai 		/*
1725a0ac57cSYu Kuai 		 * Other context won't hold ioc lock to wait for queue_lock, see
1735a0ac57cSYu Kuai 		 * details in ioc_release_fn().
1745a0ac57cSYu Kuai 		 */
175a7cfa0afSYu Kuai 		spin_lock(&icq->ioc->lock);
1765ef16305SChristoph Hellwig 		ioc_destroy_icq(icq);
177a7cfa0afSYu Kuai 		spin_unlock(&icq->ioc->lock);
1785ef16305SChristoph Hellwig 	}
1795a0ac57cSYu Kuai 	spin_unlock_irq(&q->queue_lock);
1805ef16305SChristoph Hellwig }
1815ef16305SChristoph Hellwig #else /* CONFIG_BLK_ICQ */
ioc_exit_icqs(struct io_context * ioc)1825ef16305SChristoph Hellwig static inline void ioc_exit_icqs(struct io_context *ioc)
1835ef16305SChristoph Hellwig {
1845ef16305SChristoph Hellwig }
ioc_delay_free(struct io_context * ioc)1855ef16305SChristoph Hellwig static inline bool ioc_delay_free(struct io_context *ioc)
1865ef16305SChristoph Hellwig {
1875ef16305SChristoph Hellwig 	return false;
1885ef16305SChristoph Hellwig }
1895ef16305SChristoph Hellwig #endif /* CONFIG_BLK_ICQ */
1905ef16305SChristoph Hellwig 
1915ef16305SChristoph Hellwig /**
19242ec57a8STejun Heo  * put_io_context - put a reference of io_context
19342ec57a8STejun Heo  * @ioc: io_context to put
19442ec57a8STejun Heo  *
19542ec57a8STejun Heo  * Decrement reference count of @ioc and release it if the count reaches
19611a3122fSTejun Heo  * zero.
19786db1e29SJens Axboe  */
put_io_context(struct io_context * ioc)19811a3122fSTejun Heo void put_io_context(struct io_context *ioc)
19986db1e29SJens Axboe {
20042ec57a8STejun Heo 	BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
201edf70ff5SChristoph Hellwig 	if (atomic_long_dec_and_test(&ioc->refcount) && !ioc_delay_free(ioc))
202ff8c1474SXiaotian Feng 		kmem_cache_free(iocontext_cachep, ioc);
20386db1e29SJens Axboe }
204222ee581SChristoph Hellwig EXPORT_SYMBOL_GPL(put_io_context);
20586db1e29SJens Axboe 
206f6e8d01bSTejun Heo /* Called by the exiting task */
exit_io_context(struct task_struct * task)207f6e8d01bSTejun Heo void exit_io_context(struct task_struct *task)
208f6e8d01bSTejun Heo {
209f6e8d01bSTejun Heo 	struct io_context *ioc;
210f6e8d01bSTejun Heo 
211f6e8d01bSTejun Heo 	task_lock(task);
212f6e8d01bSTejun Heo 	ioc = task->io_context;
213f6e8d01bSTejun Heo 	task->io_context = NULL;
214f6e8d01bSTejun Heo 	task_unlock(task);
215f6e8d01bSTejun Heo 
2164be8a2eaSChristoph Hellwig 	if (atomic_dec_and_test(&ioc->active_ref)) {
2174be8a2eaSChristoph Hellwig 		ioc_exit_icqs(ioc);
2184be8a2eaSChristoph Hellwig 		put_io_context(ioc);
2194be8a2eaSChristoph Hellwig 	}
220f6e8d01bSTejun Heo }
221f6e8d01bSTejun Heo 
alloc_io_context(gfp_t gfp_flags,int node)222a0f14d8bSChristoph Hellwig static struct io_context *alloc_io_context(gfp_t gfp_flags, int node)
22386db1e29SJens Axboe {
224df415656SPaul Bolle 	struct io_context *ioc;
22586db1e29SJens Axboe 
22642ec57a8STejun Heo 	ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO,
22742ec57a8STejun Heo 				    node);
22842ec57a8STejun Heo 	if (unlikely(!ioc))
229a0f14d8bSChristoph Hellwig 		return NULL;
23042ec57a8STejun Heo 
231df415656SPaul Bolle 	atomic_long_set(&ioc->refcount, 1);
232f6e8d01bSTejun Heo 	atomic_set(&ioc->active_ref, 1);
2335ef16305SChristoph Hellwig #ifdef CONFIG_BLK_ICQ
234df415656SPaul Bolle 	spin_lock_init(&ioc->lock);
235c137969bSShakeel Butt 	INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC);
236c5869807STejun Heo 	INIT_HLIST_HEAD(&ioc->icq_list);
237b2efa052STejun Heo 	INIT_WORK(&ioc->release_work, ioc_release_fn);
2385ef16305SChristoph Hellwig #endif
239e589f464SJan Kara 	ioc->ioprio = IOPRIO_DEFAULT;
240e589f464SJan Kara 
241a0f14d8bSChristoph Hellwig 	return ioc;
242a0f14d8bSChristoph Hellwig }
243a0f14d8bSChristoph Hellwig 
set_task_ioprio(struct task_struct * task,int ioprio)244a411cd3cSChristoph Hellwig int set_task_ioprio(struct task_struct *task, int ioprio)
245a411cd3cSChristoph Hellwig {
246a411cd3cSChristoph Hellwig 	int err;
247a411cd3cSChristoph Hellwig 	const struct cred *cred = current_cred(), *tcred;
248a411cd3cSChristoph Hellwig 
249a411cd3cSChristoph Hellwig 	rcu_read_lock();
250a411cd3cSChristoph Hellwig 	tcred = __task_cred(task);
251a411cd3cSChristoph Hellwig 	if (!uid_eq(tcred->uid, cred->euid) &&
252a411cd3cSChristoph Hellwig 	    !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) {
253a411cd3cSChristoph Hellwig 		rcu_read_unlock();
254a411cd3cSChristoph Hellwig 		return -EPERM;
255a411cd3cSChristoph Hellwig 	}
256a411cd3cSChristoph Hellwig 	rcu_read_unlock();
257a411cd3cSChristoph Hellwig 
258a411cd3cSChristoph Hellwig 	err = security_task_setioprio(task, ioprio);
259a411cd3cSChristoph Hellwig 	if (err)
260a411cd3cSChristoph Hellwig 		return err;
261a411cd3cSChristoph Hellwig 
2628472161bSChristoph Hellwig 	task_lock(task);
2638472161bSChristoph Hellwig 	if (unlikely(!task->io_context)) {
2648472161bSChristoph Hellwig 		struct io_context *ioc;
2658472161bSChristoph Hellwig 
2668472161bSChristoph Hellwig 		task_unlock(task);
2675fc11eebSChristoph Hellwig 
2685fc11eebSChristoph Hellwig 		ioc = alloc_io_context(GFP_ATOMIC, NUMA_NO_NODE);
2695fc11eebSChristoph Hellwig 		if (!ioc)
2705fc11eebSChristoph Hellwig 			return -ENOMEM;
2715fc11eebSChristoph Hellwig 
2725fc11eebSChristoph Hellwig 		task_lock(task);
273a957b612SJens Axboe 		if (task->flags & PF_EXITING) {
274a957b612SJens Axboe 			kmem_cache_free(iocontext_cachep, ioc);
275a957b612SJens Axboe 			goto out;
276a957b612SJens Axboe 		}
277669a0646SLukas Bulwahn 		if (task->io_context)
2785fc11eebSChristoph Hellwig 			kmem_cache_free(iocontext_cachep, ioc);
279669a0646SLukas Bulwahn 		else
2805fc11eebSChristoph Hellwig 			task->io_context = ioc;
281a411cd3cSChristoph Hellwig 	}
2828472161bSChristoph Hellwig 	task->io_context->ioprio = ioprio;
283a957b612SJens Axboe out:
2848472161bSChristoph Hellwig 	task_unlock(task);
28515583a56SJiri Slaby 	return 0;
286a411cd3cSChristoph Hellwig }
287a411cd3cSChristoph Hellwig EXPORT_SYMBOL_GPL(set_task_ioprio);
288a411cd3cSChristoph Hellwig 
__copy_io(u64 clone_flags,struct task_struct * tsk)289*edd3cb05SSimon Schuster int __copy_io(u64 clone_flags, struct task_struct *tsk)
29088c9a2ceSChristoph Hellwig {
29188c9a2ceSChristoph Hellwig 	struct io_context *ioc = current->io_context;
29288c9a2ceSChristoph Hellwig 
29388c9a2ceSChristoph Hellwig 	/*
29488c9a2ceSChristoph Hellwig 	 * Share io context with parent, if CLONE_IO is set
29588c9a2ceSChristoph Hellwig 	 */
29688c9a2ceSChristoph Hellwig 	if (clone_flags & CLONE_IO) {
29750569c24SChristoph Hellwig 		atomic_inc(&ioc->active_ref);
29888c9a2ceSChristoph Hellwig 		tsk->io_context = ioc;
29988c9a2ceSChristoph Hellwig 	} else if (ioprio_valid(ioc->ioprio)) {
3008ffc1368SChristoph Hellwig 		tsk->io_context = alloc_io_context(GFP_KERNEL, NUMA_NO_NODE);
3018ffc1368SChristoph Hellwig 		if (!tsk->io_context)
30288c9a2ceSChristoph Hellwig 			return -ENOMEM;
3038ffc1368SChristoph Hellwig 		tsk->io_context->ioprio = ioc->ioprio;
30488c9a2ceSChristoph Hellwig 	}
30588c9a2ceSChristoph Hellwig 
30688c9a2ceSChristoph Hellwig 	return 0;
30788c9a2ceSChristoph Hellwig }
30888c9a2ceSChristoph Hellwig 
3095ef16305SChristoph Hellwig #ifdef CONFIG_BLK_ICQ
31047fdd4caSTejun Heo /**
3115421681bSYu Kuai  * ioc_lookup_icq - lookup io_cq from ioc in io issue path
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
3155421681bSYu Kuai  * from io issue path, either return NULL if current issue io to @q for the
3165421681bSYu Kuai  * first time, or return a valid icq.
31747fdd4caSTejun Heo  */
ioc_lookup_icq(struct request_queue * q)318eca5892aSChristoph Hellwig struct io_cq *ioc_lookup_icq(struct request_queue *q)
31947fdd4caSTejun Heo {
320eca5892aSChristoph Hellwig 	struct io_context *ioc = current->io_context;
32147fdd4caSTejun Heo 	struct io_cq *icq;
32247fdd4caSTejun Heo 
32347fdd4caSTejun Heo 	/*
32447fdd4caSTejun Heo 	 * icq's are indexed from @ioc using radix tree and hint pointer,
3255421681bSYu Kuai 	 * both of which are protected with RCU, io issue path ensures that
3265421681bSYu Kuai 	 * both request_queue and current task are valid, the found icq
3275421681bSYu Kuai 	 * is guaranteed to be valid until the io is done.
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
347f1f8cc94STejun Heo  * @q: request_queue of interest
348f1f8cc94STejun Heo  *
34924acfc34STejun Heo  * Make sure io_cq linking @ioc and @q exists.  If icq doesn't exist, they
35024acfc34STejun Heo  * will be created using @gfp_mask.
351f1f8cc94STejun Heo  *
352f1f8cc94STejun Heo  * The caller is responsible for ensuring @ioc won't go away and @q is
353f1f8cc94STejun Heo  * alive and will stay alive until this function returns.
354f1f8cc94STejun Heo  */
ioc_create_icq(struct request_queue * q)35518b74c4dSChristoph Hellwig static struct io_cq *ioc_create_icq(struct request_queue *q)
356f1f8cc94STejun Heo {
35718b74c4dSChristoph Hellwig 	struct io_context *ioc = current->io_context;
358f1f8cc94STejun Heo 	struct elevator_type *et = q->elevator->type;
359f1f8cc94STejun Heo 	struct io_cq *icq;
360f1f8cc94STejun Heo 
361f1f8cc94STejun Heo 	/* allocate stuff */
36218b74c4dSChristoph Hellwig 	icq = kmem_cache_alloc_node(et->icq_cache, GFP_ATOMIC | __GFP_ZERO,
363f1f8cc94STejun Heo 				    q->node);
364f1f8cc94STejun Heo 	if (!icq)
365f1f8cc94STejun Heo 		return NULL;
366f1f8cc94STejun Heo 
36718b74c4dSChristoph Hellwig 	if (radix_tree_maybe_preload(GFP_ATOMIC) < 0) {
368f1f8cc94STejun Heo 		kmem_cache_free(et->icq_cache, icq);
369f1f8cc94STejun Heo 		return NULL;
370f1f8cc94STejun Heo 	}
371f1f8cc94STejun Heo 
372f1f8cc94STejun Heo 	icq->ioc = ioc;
373f1f8cc94STejun Heo 	icq->q = q;
374f1f8cc94STejun Heo 	INIT_LIST_HEAD(&icq->q_node);
375f1f8cc94STejun Heo 	INIT_HLIST_NODE(&icq->ioc_node);
376f1f8cc94STejun Heo 
377f1f8cc94STejun Heo 	/* lock both q and ioc and try to link @icq */
3780d945c1fSChristoph Hellwig 	spin_lock_irq(&q->queue_lock);
379f1f8cc94STejun Heo 	spin_lock(&ioc->lock);
380f1f8cc94STejun Heo 
381f1f8cc94STejun Heo 	if (likely(!radix_tree_insert(&ioc->icq_tree, q->id, icq))) {
382f1f8cc94STejun Heo 		hlist_add_head(&icq->ioc_node, &ioc->icq_list);
383f1f8cc94STejun Heo 		list_add(&icq->q_node, &q->icq_list);
384f9cd4bfeSJens Axboe 		if (et->ops.init_icq)
385f9cd4bfeSJens Axboe 			et->ops.init_icq(icq);
386f1f8cc94STejun Heo 	} else {
387f1f8cc94STejun Heo 		kmem_cache_free(et->icq_cache, icq);
388eca5892aSChristoph Hellwig 		icq = ioc_lookup_icq(q);
389f1f8cc94STejun Heo 		if (!icq)
390f1f8cc94STejun Heo 			printk(KERN_ERR "cfq: icq link failed!\n");
391f1f8cc94STejun Heo 	}
392f1f8cc94STejun Heo 
393f1f8cc94STejun Heo 	spin_unlock(&ioc->lock);
3940d945c1fSChristoph Hellwig 	spin_unlock_irq(&q->queue_lock);
395f1f8cc94STejun Heo 	radix_tree_preload_end();
396f1f8cc94STejun Heo 	return icq;
397f1f8cc94STejun Heo }
398f1f8cc94STejun Heo 
ioc_find_get_icq(struct request_queue * q)39987dd1d63SChristoph Hellwig struct io_cq *ioc_find_get_icq(struct request_queue *q)
40087dd1d63SChristoph Hellwig {
401d538ea4cSChristoph Hellwig 	struct io_context *ioc = current->io_context;
402d538ea4cSChristoph Hellwig 	struct io_cq *icq = NULL;
40387dd1d63SChristoph Hellwig 
404d538ea4cSChristoph Hellwig 	if (unlikely(!ioc)) {
40590b627f5SChristoph Hellwig 		ioc = alloc_io_context(GFP_ATOMIC, q->node);
40687dd1d63SChristoph Hellwig 		if (!ioc)
40787dd1d63SChristoph Hellwig 			return NULL;
40890b627f5SChristoph Hellwig 
40990b627f5SChristoph Hellwig 		task_lock(current);
41090b627f5SChristoph Hellwig 		if (current->io_context) {
41190b627f5SChristoph Hellwig 			kmem_cache_free(iocontext_cachep, ioc);
41290b627f5SChristoph Hellwig 			ioc = current->io_context;
41390b627f5SChristoph Hellwig 		} else {
41490b627f5SChristoph Hellwig 			current->io_context = ioc;
41590b627f5SChristoph Hellwig 		}
41690b627f5SChristoph Hellwig 
41790b627f5SChristoph Hellwig 		get_io_context(ioc);
41890b627f5SChristoph Hellwig 		task_unlock(current);
419d538ea4cSChristoph Hellwig 	} else {
420d538ea4cSChristoph Hellwig 		get_io_context(ioc);
421eca5892aSChristoph Hellwig 		icq = ioc_lookup_icq(q);
422d538ea4cSChristoph Hellwig 	}
42387dd1d63SChristoph Hellwig 
42487dd1d63SChristoph Hellwig 	if (!icq) {
42518b74c4dSChristoph Hellwig 		icq = ioc_create_icq(q);
426d538ea4cSChristoph Hellwig 		if (!icq) {
427d538ea4cSChristoph Hellwig 			put_io_context(ioc);
42887dd1d63SChristoph Hellwig 			return NULL;
42987dd1d63SChristoph Hellwig 		}
430d538ea4cSChristoph Hellwig 	}
43187dd1d63SChristoph Hellwig 	return icq;
43287dd1d63SChristoph Hellwig }
43387dd1d63SChristoph Hellwig EXPORT_SYMBOL_GPL(ioc_find_get_icq);
4345ef16305SChristoph Hellwig #endif /* CONFIG_BLK_ICQ */
43587dd1d63SChristoph Hellwig 
blk_ioc_init(void)43613341598SAdrian Bunk static int __init blk_ioc_init(void)
43786db1e29SJens Axboe {
43886db1e29SJens Axboe 	iocontext_cachep = kmem_cache_create("blkdev_ioc",
43986db1e29SJens Axboe 			sizeof(struct io_context), 0, SLAB_PANIC, NULL);
44086db1e29SJens Axboe 	return 0;
44186db1e29SJens Axboe }
44286db1e29SJens Axboe subsys_initcall(blk_ioc_init);
443