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
icq_free_icq_rcu(struct rcu_head * head)357e5a8794STejun Heo static void icq_free_icq_rcu(struct rcu_head *head)
367e5a8794STejun Heo {
377e5a8794STejun Heo struct io_cq *icq = container_of(head, struct io_cq, __rcu_head);
387e5a8794STejun Heo
397e5a8794STejun Heo kmem_cache_free(icq->__rcu_icq_cache, icq);
407e5a8794STejun Heo }
417e5a8794STejun Heo
423d492c2eSOmar Sandoval /*
437b36a718SJens Axboe * Exit an icq. Called with ioc locked for blk-mq, and with both ioc
447b36a718SJens Axboe * and queue locked for legacy.
453d492c2eSOmar Sandoval */
ioc_exit_icq(struct io_cq * icq)467e5a8794STejun Heo static void ioc_exit_icq(struct io_cq *icq)
477e5a8794STejun Heo {
48621032adSTejun Heo struct elevator_type *et = icq->q->elevator->type;
49621032adSTejun Heo
50621032adSTejun Heo if (icq->flags & ICQ_EXITED)
51621032adSTejun Heo return;
52621032adSTejun Heo
53f9cd4bfeSJens Axboe if (et->ops.exit_icq)
54f9cd4bfeSJens Axboe et->ops.exit_icq(icq);
55621032adSTejun Heo
56621032adSTejun Heo icq->flags |= ICQ_EXITED;
57621032adSTejun Heo }
58621032adSTejun Heo
ioc_exit_icqs(struct io_context * ioc)594be8a2eaSChristoph Hellwig static void ioc_exit_icqs(struct io_context *ioc)
604be8a2eaSChristoph Hellwig {
614be8a2eaSChristoph Hellwig struct io_cq *icq;
624be8a2eaSChristoph Hellwig
634be8a2eaSChristoph Hellwig spin_lock_irq(&ioc->lock);
644be8a2eaSChristoph Hellwig hlist_for_each_entry(icq, &ioc->icq_list, ioc_node)
654be8a2eaSChristoph Hellwig ioc_exit_icq(icq);
664be8a2eaSChristoph Hellwig spin_unlock_irq(&ioc->lock);
674be8a2eaSChristoph Hellwig }
684be8a2eaSChristoph Hellwig
697b36a718SJens Axboe /*
707b36a718SJens Axboe * Release an icq. Called with ioc locked for blk-mq, and with both ioc
717b36a718SJens Axboe * and queue locked for legacy.
727b36a718SJens Axboe */
ioc_destroy_icq(struct io_cq * icq)73621032adSTejun Heo static void ioc_destroy_icq(struct io_cq *icq)
74621032adSTejun Heo {
757e5a8794STejun Heo struct io_context *ioc = icq->ioc;
767e5a8794STejun Heo struct request_queue *q = icq->q;
777e5a8794STejun Heo struct elevator_type *et = q->elevator->type;
787e5a8794STejun Heo
797e5a8794STejun Heo lockdep_assert_held(&ioc->lock);
805a0ac57cSYu Kuai lockdep_assert_held(&q->queue_lock);
815a0ac57cSYu Kuai
825a0ac57cSYu Kuai if (icq->flags & ICQ_DESTROYED)
835a0ac57cSYu Kuai return;
847e5a8794STejun Heo
857e5a8794STejun Heo radix_tree_delete(&ioc->icq_tree, icq->q->id);
867e5a8794STejun Heo hlist_del_init(&icq->ioc_node);
877e5a8794STejun Heo list_del_init(&icq->q_node);
887e5a8794STejun Heo
897e5a8794STejun Heo /*
907e5a8794STejun Heo * Both setting lookup hint to and clearing it from @icq are done
917e5a8794STejun Heo * under queue_lock. If it's not pointing to @icq now, it never
927e5a8794STejun Heo * will. Hint assignment itself can race safely.
937e5a8794STejun Heo */
94ec6c676aSPaul E. McKenney if (rcu_access_pointer(ioc->icq_hint) == icq)
957e5a8794STejun Heo rcu_assign_pointer(ioc->icq_hint, NULL);
967e5a8794STejun Heo
97621032adSTejun Heo ioc_exit_icq(icq);
987e5a8794STejun Heo
997e5a8794STejun Heo /*
1007e5a8794STejun Heo * @icq->q might have gone away by the time RCU callback runs
1017e5a8794STejun Heo * making it impossible to determine icq_cache. Record it in @icq.
1027e5a8794STejun Heo */
1037e5a8794STejun Heo icq->__rcu_icq_cache = et->icq_cache;
10430a2da7bSSahitya Tummala icq->flags |= ICQ_DESTROYED;
1057e5a8794STejun Heo call_rcu(&icq->__rcu_head, icq_free_icq_rcu);
1067e5a8794STejun Heo }
1077e5a8794STejun Heo
108b2efa052STejun Heo /*
109b2efa052STejun Heo * Slow path for ioc release in put_io_context(). Performs double-lock
110c5869807STejun Heo * dancing to unlink all icq's and then frees ioc.
111b2efa052STejun Heo */
ioc_release_fn(struct work_struct * work)112b2efa052STejun Heo static void ioc_release_fn(struct work_struct *work)
113b2efa052STejun Heo {
114b2efa052STejun Heo struct io_context *ioc = container_of(work, struct io_context,
115b2efa052STejun Heo release_work);
116a43f085fSJohn Ogness spin_lock_irq(&ioc->lock);
117b2efa052STejun Heo
118c5869807STejun Heo while (!hlist_empty(&ioc->icq_list)) {
119c5869807STejun Heo struct io_cq *icq = hlist_entry(ioc->icq_list.first,
120c5869807STejun Heo struct io_cq, ioc_node);
1212274b029STejun Heo struct request_queue *q = icq->q;
122b2efa052STejun Heo
1230d945c1fSChristoph Hellwig if (spin_trylock(&q->queue_lock)) {
124621032adSTejun Heo ioc_destroy_icq(icq);
1250d945c1fSChristoph Hellwig spin_unlock(&q->queue_lock);
126b2efa052STejun Heo } else {
127ab96bbabSJohn Ogness /* Make sure q and icq cannot be freed. */
128ab96bbabSJohn Ogness rcu_read_lock();
129ab96bbabSJohn Ogness
130ab96bbabSJohn Ogness /* Re-acquire the locks in the correct order. */
131ab96bbabSJohn Ogness spin_unlock(&ioc->lock);
132ab96bbabSJohn Ogness spin_lock(&q->queue_lock);
133ab96bbabSJohn Ogness spin_lock(&ioc->lock);
134ab96bbabSJohn Ogness
135ab96bbabSJohn Ogness ioc_destroy_icq(icq);
136ab96bbabSJohn Ogness
137ab96bbabSJohn Ogness spin_unlock(&q->queue_lock);
138ab96bbabSJohn Ogness rcu_read_unlock();
139b2efa052STejun Heo }
1402274b029STejun Heo }
1412274b029STejun Heo
142a43f085fSJohn Ogness spin_unlock_irq(&ioc->lock);
143b2efa052STejun Heo
144b2efa052STejun Heo kmem_cache_free(iocontext_cachep, ioc);
14586db1e29SJens Axboe }
14686db1e29SJens Axboe
147edf70ff5SChristoph Hellwig /*
148edf70ff5SChristoph Hellwig * Releasing icqs requires reverse order double locking and we may already be
149edf70ff5SChristoph Hellwig * holding a queue_lock. Do it asynchronously from a workqueue.
150edf70ff5SChristoph Hellwig */
ioc_delay_free(struct io_context * ioc)151edf70ff5SChristoph Hellwig static bool ioc_delay_free(struct io_context *ioc)
152edf70ff5SChristoph Hellwig {
153edf70ff5SChristoph Hellwig unsigned long flags;
154edf70ff5SChristoph Hellwig
155edf70ff5SChristoph Hellwig spin_lock_irqsave(&ioc->lock, flags);
156edf70ff5SChristoph Hellwig if (!hlist_empty(&ioc->icq_list)) {
157edf70ff5SChristoph Hellwig queue_work(system_power_efficient_wq, &ioc->release_work);
158edf70ff5SChristoph Hellwig spin_unlock_irqrestore(&ioc->lock, flags);
159edf70ff5SChristoph Hellwig return true;
160edf70ff5SChristoph Hellwig }
161edf70ff5SChristoph Hellwig spin_unlock_irqrestore(&ioc->lock, flags);
162edf70ff5SChristoph Hellwig return false;
163edf70ff5SChristoph Hellwig }
164edf70ff5SChristoph Hellwig
16542ec57a8STejun Heo /**
1665ef16305SChristoph Hellwig * ioc_clear_queue - break any ioc association with the specified queue
1675ef16305SChristoph Hellwig * @q: request_queue being cleared
1685ef16305SChristoph Hellwig *
1695ef16305SChristoph Hellwig * Walk @q->icq_list and exit all io_cq's.
1705ef16305SChristoph Hellwig */
ioc_clear_queue(struct request_queue * q)1715ef16305SChristoph Hellwig void ioc_clear_queue(struct request_queue *q)
1725ef16305SChristoph Hellwig {
1735ef16305SChristoph Hellwig spin_lock_irq(&q->queue_lock);
1745a0ac57cSYu Kuai while (!list_empty(&q->icq_list)) {
1755ef16305SChristoph Hellwig struct io_cq *icq =
1765a0ac57cSYu Kuai list_first_entry(&q->icq_list, struct io_cq, q_node);
1775ef16305SChristoph Hellwig
1785a0ac57cSYu Kuai /*
1795a0ac57cSYu Kuai * Other context won't hold ioc lock to wait for queue_lock, see
1805a0ac57cSYu Kuai * details in ioc_release_fn().
1815a0ac57cSYu Kuai */
182*a7cfa0afSYu Kuai spin_lock(&icq->ioc->lock);
1835ef16305SChristoph Hellwig ioc_destroy_icq(icq);
184*a7cfa0afSYu Kuai spin_unlock(&icq->ioc->lock);
1855ef16305SChristoph Hellwig }
1865a0ac57cSYu Kuai spin_unlock_irq(&q->queue_lock);
1875ef16305SChristoph Hellwig }
1885ef16305SChristoph Hellwig #else /* CONFIG_BLK_ICQ */
ioc_exit_icqs(struct io_context * ioc)1895ef16305SChristoph Hellwig static inline void ioc_exit_icqs(struct io_context *ioc)
1905ef16305SChristoph Hellwig {
1915ef16305SChristoph Hellwig }
ioc_delay_free(struct io_context * ioc)1925ef16305SChristoph Hellwig static inline bool ioc_delay_free(struct io_context *ioc)
1935ef16305SChristoph Hellwig {
1945ef16305SChristoph Hellwig return false;
1955ef16305SChristoph Hellwig }
1965ef16305SChristoph Hellwig #endif /* CONFIG_BLK_ICQ */
1975ef16305SChristoph Hellwig
1985ef16305SChristoph Hellwig /**
19942ec57a8STejun Heo * put_io_context - put a reference of io_context
20042ec57a8STejun Heo * @ioc: io_context to put
20142ec57a8STejun Heo *
20242ec57a8STejun Heo * Decrement reference count of @ioc and release it if the count reaches
20311a3122fSTejun Heo * zero.
20486db1e29SJens Axboe */
put_io_context(struct io_context * ioc)20511a3122fSTejun Heo void put_io_context(struct io_context *ioc)
20686db1e29SJens Axboe {
20742ec57a8STejun Heo BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
208edf70ff5SChristoph Hellwig if (atomic_long_dec_and_test(&ioc->refcount) && !ioc_delay_free(ioc))
209ff8c1474SXiaotian Feng kmem_cache_free(iocontext_cachep, ioc);
21086db1e29SJens Axboe }
211222ee581SChristoph Hellwig EXPORT_SYMBOL_GPL(put_io_context);
21286db1e29SJens Axboe
213f6e8d01bSTejun Heo /* Called by the exiting task */
exit_io_context(struct task_struct * task)214f6e8d01bSTejun Heo void exit_io_context(struct task_struct *task)
215f6e8d01bSTejun Heo {
216f6e8d01bSTejun Heo struct io_context *ioc;
217f6e8d01bSTejun Heo
218f6e8d01bSTejun Heo task_lock(task);
219f6e8d01bSTejun Heo ioc = task->io_context;
220f6e8d01bSTejun Heo task->io_context = NULL;
221f6e8d01bSTejun Heo task_unlock(task);
222f6e8d01bSTejun Heo
2234be8a2eaSChristoph Hellwig if (atomic_dec_and_test(&ioc->active_ref)) {
2244be8a2eaSChristoph Hellwig ioc_exit_icqs(ioc);
2254be8a2eaSChristoph Hellwig put_io_context(ioc);
2264be8a2eaSChristoph Hellwig }
227f6e8d01bSTejun Heo }
228f6e8d01bSTejun Heo
alloc_io_context(gfp_t gfp_flags,int node)229a0f14d8bSChristoph Hellwig static struct io_context *alloc_io_context(gfp_t gfp_flags, int node)
23086db1e29SJens Axboe {
231df415656SPaul Bolle struct io_context *ioc;
23286db1e29SJens Axboe
23342ec57a8STejun Heo ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO,
23442ec57a8STejun Heo node);
23542ec57a8STejun Heo if (unlikely(!ioc))
236a0f14d8bSChristoph Hellwig return NULL;
23742ec57a8STejun Heo
238df415656SPaul Bolle atomic_long_set(&ioc->refcount, 1);
239f6e8d01bSTejun Heo atomic_set(&ioc->active_ref, 1);
2405ef16305SChristoph Hellwig #ifdef CONFIG_BLK_ICQ
241df415656SPaul Bolle spin_lock_init(&ioc->lock);
242c137969bSShakeel Butt INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC);
243c5869807STejun Heo INIT_HLIST_HEAD(&ioc->icq_list);
244b2efa052STejun Heo INIT_WORK(&ioc->release_work, ioc_release_fn);
2455ef16305SChristoph Hellwig #endif
246e589f464SJan Kara ioc->ioprio = IOPRIO_DEFAULT;
247e589f464SJan Kara
248a0f14d8bSChristoph Hellwig return ioc;
249a0f14d8bSChristoph Hellwig }
250a0f14d8bSChristoph Hellwig
set_task_ioprio(struct task_struct * task,int ioprio)251a411cd3cSChristoph Hellwig int set_task_ioprio(struct task_struct *task, int ioprio)
252a411cd3cSChristoph Hellwig {
253a411cd3cSChristoph Hellwig int err;
254a411cd3cSChristoph Hellwig const struct cred *cred = current_cred(), *tcred;
255a411cd3cSChristoph Hellwig
256a411cd3cSChristoph Hellwig rcu_read_lock();
257a411cd3cSChristoph Hellwig tcred = __task_cred(task);
258a411cd3cSChristoph Hellwig if (!uid_eq(tcred->uid, cred->euid) &&
259a411cd3cSChristoph Hellwig !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) {
260a411cd3cSChristoph Hellwig rcu_read_unlock();
261a411cd3cSChristoph Hellwig return -EPERM;
262a411cd3cSChristoph Hellwig }
263a411cd3cSChristoph Hellwig rcu_read_unlock();
264a411cd3cSChristoph Hellwig
265a411cd3cSChristoph Hellwig err = security_task_setioprio(task, ioprio);
266a411cd3cSChristoph Hellwig if (err)
267a411cd3cSChristoph Hellwig return err;
268a411cd3cSChristoph Hellwig
2698472161bSChristoph Hellwig task_lock(task);
2708472161bSChristoph Hellwig if (unlikely(!task->io_context)) {
2718472161bSChristoph Hellwig struct io_context *ioc;
2728472161bSChristoph Hellwig
2738472161bSChristoph Hellwig task_unlock(task);
2745fc11eebSChristoph Hellwig
2755fc11eebSChristoph Hellwig ioc = alloc_io_context(GFP_ATOMIC, NUMA_NO_NODE);
2765fc11eebSChristoph Hellwig if (!ioc)
2775fc11eebSChristoph Hellwig return -ENOMEM;
2785fc11eebSChristoph Hellwig
2795fc11eebSChristoph Hellwig task_lock(task);
280a957b612SJens Axboe if (task->flags & PF_EXITING) {
281a957b612SJens Axboe kmem_cache_free(iocontext_cachep, ioc);
282a957b612SJens Axboe goto out;
283a957b612SJens Axboe }
284669a0646SLukas Bulwahn if (task->io_context)
2855fc11eebSChristoph Hellwig kmem_cache_free(iocontext_cachep, ioc);
286669a0646SLukas Bulwahn else
2875fc11eebSChristoph Hellwig task->io_context = ioc;
288a411cd3cSChristoph Hellwig }
2898472161bSChristoph Hellwig task->io_context->ioprio = ioprio;
290a957b612SJens Axboe out:
2918472161bSChristoph Hellwig task_unlock(task);
29215583a56SJiri Slaby return 0;
293a411cd3cSChristoph Hellwig }
294a411cd3cSChristoph Hellwig EXPORT_SYMBOL_GPL(set_task_ioprio);
295a411cd3cSChristoph Hellwig
__copy_io(unsigned long clone_flags,struct task_struct * tsk)29688c9a2ceSChristoph Hellwig int __copy_io(unsigned long clone_flags, struct task_struct *tsk)
29788c9a2ceSChristoph Hellwig {
29888c9a2ceSChristoph Hellwig struct io_context *ioc = current->io_context;
29988c9a2ceSChristoph Hellwig
30088c9a2ceSChristoph Hellwig /*
30188c9a2ceSChristoph Hellwig * Share io context with parent, if CLONE_IO is set
30288c9a2ceSChristoph Hellwig */
30388c9a2ceSChristoph Hellwig if (clone_flags & CLONE_IO) {
30450569c24SChristoph Hellwig atomic_inc(&ioc->active_ref);
30588c9a2ceSChristoph Hellwig tsk->io_context = ioc;
30688c9a2ceSChristoph Hellwig } else if (ioprio_valid(ioc->ioprio)) {
3078ffc1368SChristoph Hellwig tsk->io_context = alloc_io_context(GFP_KERNEL, NUMA_NO_NODE);
3088ffc1368SChristoph Hellwig if (!tsk->io_context)
30988c9a2ceSChristoph Hellwig return -ENOMEM;
3108ffc1368SChristoph Hellwig tsk->io_context->ioprio = ioc->ioprio;
31188c9a2ceSChristoph Hellwig }
31288c9a2ceSChristoph Hellwig
31388c9a2ceSChristoph Hellwig return 0;
31488c9a2ceSChristoph Hellwig }
31588c9a2ceSChristoph Hellwig
3165ef16305SChristoph Hellwig #ifdef CONFIG_BLK_ICQ
31747fdd4caSTejun Heo /**
31847fdd4caSTejun Heo * ioc_lookup_icq - lookup io_cq from ioc
31947fdd4caSTejun Heo * @q: the associated request_queue
32047fdd4caSTejun Heo *
32147fdd4caSTejun Heo * Look up io_cq associated with @ioc - @q pair from @ioc. Must be called
32247fdd4caSTejun Heo * with @q->queue_lock held.
32347fdd4caSTejun Heo */
ioc_lookup_icq(struct request_queue * q)324eca5892aSChristoph Hellwig struct io_cq *ioc_lookup_icq(struct request_queue *q)
32547fdd4caSTejun Heo {
326eca5892aSChristoph Hellwig struct io_context *ioc = current->io_context;
32747fdd4caSTejun Heo struct io_cq *icq;
32847fdd4caSTejun Heo
3290d945c1fSChristoph Hellwig lockdep_assert_held(&q->queue_lock);
33047fdd4caSTejun Heo
33147fdd4caSTejun Heo /*
33247fdd4caSTejun Heo * icq's are indexed from @ioc using radix tree and hint pointer,
33347fdd4caSTejun Heo * both of which are protected with RCU. All removals are done
33447fdd4caSTejun Heo * holding both q and ioc locks, and we're holding q lock - if we
33547fdd4caSTejun Heo * find a icq which points to us, it's guaranteed to be valid.
33647fdd4caSTejun Heo */
33747fdd4caSTejun Heo rcu_read_lock();
33847fdd4caSTejun Heo icq = rcu_dereference(ioc->icq_hint);
33947fdd4caSTejun Heo if (icq && icq->q == q)
34047fdd4caSTejun Heo goto out;
34147fdd4caSTejun Heo
34247fdd4caSTejun Heo icq = radix_tree_lookup(&ioc->icq_tree, q->id);
34347fdd4caSTejun Heo if (icq && icq->q == q)
34447fdd4caSTejun Heo rcu_assign_pointer(ioc->icq_hint, icq); /* allowed to race */
34547fdd4caSTejun Heo else
34647fdd4caSTejun Heo icq = NULL;
34747fdd4caSTejun Heo out:
34847fdd4caSTejun Heo rcu_read_unlock();
34947fdd4caSTejun Heo return icq;
35047fdd4caSTejun Heo }
35147fdd4caSTejun Heo EXPORT_SYMBOL(ioc_lookup_icq);
35247fdd4caSTejun Heo
353f1f8cc94STejun Heo /**
354f1f8cc94STejun Heo * ioc_create_icq - create and link io_cq
355f1f8cc94STejun Heo * @q: request_queue of interest
356f1f8cc94STejun Heo *
35724acfc34STejun Heo * Make sure io_cq linking @ioc and @q exists. If icq doesn't exist, they
35824acfc34STejun Heo * will be created using @gfp_mask.
359f1f8cc94STejun Heo *
360f1f8cc94STejun Heo * The caller is responsible for ensuring @ioc won't go away and @q is
361f1f8cc94STejun Heo * alive and will stay alive until this function returns.
362f1f8cc94STejun Heo */
ioc_create_icq(struct request_queue * q)36318b74c4dSChristoph Hellwig static struct io_cq *ioc_create_icq(struct request_queue *q)
364f1f8cc94STejun Heo {
36518b74c4dSChristoph Hellwig struct io_context *ioc = current->io_context;
366f1f8cc94STejun Heo struct elevator_type *et = q->elevator->type;
367f1f8cc94STejun Heo struct io_cq *icq;
368f1f8cc94STejun Heo
369f1f8cc94STejun Heo /* allocate stuff */
37018b74c4dSChristoph Hellwig icq = kmem_cache_alloc_node(et->icq_cache, GFP_ATOMIC | __GFP_ZERO,
371f1f8cc94STejun Heo q->node);
372f1f8cc94STejun Heo if (!icq)
373f1f8cc94STejun Heo return NULL;
374f1f8cc94STejun Heo
37518b74c4dSChristoph Hellwig if (radix_tree_maybe_preload(GFP_ATOMIC) < 0) {
376f1f8cc94STejun Heo kmem_cache_free(et->icq_cache, icq);
377f1f8cc94STejun Heo return NULL;
378f1f8cc94STejun Heo }
379f1f8cc94STejun Heo
380f1f8cc94STejun Heo icq->ioc = ioc;
381f1f8cc94STejun Heo icq->q = q;
382f1f8cc94STejun Heo INIT_LIST_HEAD(&icq->q_node);
383f1f8cc94STejun Heo INIT_HLIST_NODE(&icq->ioc_node);
384f1f8cc94STejun Heo
385f1f8cc94STejun Heo /* lock both q and ioc and try to link @icq */
3860d945c1fSChristoph Hellwig spin_lock_irq(&q->queue_lock);
387f1f8cc94STejun Heo spin_lock(&ioc->lock);
388f1f8cc94STejun Heo
389f1f8cc94STejun Heo if (likely(!radix_tree_insert(&ioc->icq_tree, q->id, icq))) {
390f1f8cc94STejun Heo hlist_add_head(&icq->ioc_node, &ioc->icq_list);
391f1f8cc94STejun Heo list_add(&icq->q_node, &q->icq_list);
392f9cd4bfeSJens Axboe if (et->ops.init_icq)
393f9cd4bfeSJens Axboe et->ops.init_icq(icq);
394f1f8cc94STejun Heo } else {
395f1f8cc94STejun Heo kmem_cache_free(et->icq_cache, icq);
396eca5892aSChristoph Hellwig icq = ioc_lookup_icq(q);
397f1f8cc94STejun Heo if (!icq)
398f1f8cc94STejun Heo printk(KERN_ERR "cfq: icq link failed!\n");
399f1f8cc94STejun Heo }
400f1f8cc94STejun Heo
401f1f8cc94STejun Heo spin_unlock(&ioc->lock);
4020d945c1fSChristoph Hellwig spin_unlock_irq(&q->queue_lock);
403f1f8cc94STejun Heo radix_tree_preload_end();
404f1f8cc94STejun Heo return icq;
405f1f8cc94STejun Heo }
406f1f8cc94STejun Heo
ioc_find_get_icq(struct request_queue * q)40787dd1d63SChristoph Hellwig struct io_cq *ioc_find_get_icq(struct request_queue *q)
40887dd1d63SChristoph Hellwig {
409d538ea4cSChristoph Hellwig struct io_context *ioc = current->io_context;
410d538ea4cSChristoph Hellwig struct io_cq *icq = NULL;
41187dd1d63SChristoph Hellwig
412d538ea4cSChristoph Hellwig if (unlikely(!ioc)) {
41390b627f5SChristoph Hellwig ioc = alloc_io_context(GFP_ATOMIC, q->node);
41487dd1d63SChristoph Hellwig if (!ioc)
41587dd1d63SChristoph Hellwig return NULL;
41690b627f5SChristoph Hellwig
41790b627f5SChristoph Hellwig task_lock(current);
41890b627f5SChristoph Hellwig if (current->io_context) {
41990b627f5SChristoph Hellwig kmem_cache_free(iocontext_cachep, ioc);
42090b627f5SChristoph Hellwig ioc = current->io_context;
42190b627f5SChristoph Hellwig } else {
42290b627f5SChristoph Hellwig current->io_context = ioc;
42390b627f5SChristoph Hellwig }
42490b627f5SChristoph Hellwig
42590b627f5SChristoph Hellwig get_io_context(ioc);
42690b627f5SChristoph Hellwig task_unlock(current);
427d538ea4cSChristoph Hellwig } else {
428d538ea4cSChristoph Hellwig get_io_context(ioc);
42987dd1d63SChristoph Hellwig
43087dd1d63SChristoph Hellwig spin_lock_irq(&q->queue_lock);
431eca5892aSChristoph Hellwig icq = ioc_lookup_icq(q);
43287dd1d63SChristoph Hellwig spin_unlock_irq(&q->queue_lock);
433d538ea4cSChristoph Hellwig }
43487dd1d63SChristoph Hellwig
43587dd1d63SChristoph Hellwig if (!icq) {
43618b74c4dSChristoph Hellwig icq = ioc_create_icq(q);
437d538ea4cSChristoph Hellwig if (!icq) {
438d538ea4cSChristoph Hellwig put_io_context(ioc);
43987dd1d63SChristoph Hellwig return NULL;
44087dd1d63SChristoph Hellwig }
441d538ea4cSChristoph Hellwig }
44287dd1d63SChristoph Hellwig return icq;
44387dd1d63SChristoph Hellwig }
44487dd1d63SChristoph Hellwig EXPORT_SYMBOL_GPL(ioc_find_get_icq);
4455ef16305SChristoph Hellwig #endif /* CONFIG_BLK_ICQ */
44687dd1d63SChristoph Hellwig
blk_ioc_init(void)44713341598SAdrian Bunk static int __init blk_ioc_init(void)
44886db1e29SJens Axboe {
44986db1e29SJens Axboe iocontext_cachep = kmem_cache_create("blkdev_ioc",
45086db1e29SJens Axboe sizeof(struct io_context), 0, SLAB_PANIC, NULL);
45186db1e29SJens Axboe return 0;
45286db1e29SJens Axboe }
45386db1e29SJens Axboe subsys_initcall(blk_ioc_init);
454