2 * Functions related to io context handling
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/init.h>
8 #include <linux/blkdev.h>
9 #include <linux/bootmem.h> /* for max_pfn/max_low_pfn */
14 * For io context allocations
16 static struct kmem_cache *iocontext_cachep;
18 static void cfq_dtor(struct io_context *ioc)
20 struct cfq_io_context *cic[1];
24 * We don't have a specific key to lookup with, so use the gang
25 * lookup to just retrieve the first item stored. The cfq exit
26 * function will iterate the full tree, so any member will do.
28 r = radix_tree_gang_lookup(&ioc->radix_root, (void **) cic, 0, 1);
34 * IO Context helper functions. put_io_context() returns 1 if there are no
35 * more users of this io context, 0 otherwise.
37 int put_io_context(struct io_context *ioc)
42 BUG_ON(atomic_read(&ioc->refcount) == 0);
44 if (atomic_dec_and_test(&ioc->refcount)) {
46 if (ioc->aic && ioc->aic->dtor)
47 ioc->aic->dtor(ioc->aic);
51 kmem_cache_free(iocontext_cachep, ioc);
56 EXPORT_SYMBOL(put_io_context);
58 static void cfq_exit(struct io_context *ioc)
60 struct cfq_io_context *cic[1];
65 * See comment for cfq_dtor()
67 r = radix_tree_gang_lookup(&ioc->radix_root, (void **) cic, 0, 1);
74 /* Called by the exitting task */
75 void exit_io_context(void)
77 struct io_context *ioc;
80 ioc = current->io_context;
81 current->io_context = NULL;
84 if (atomic_dec_and_test(&ioc->nr_tasks)) {
85 if (ioc->aic && ioc->aic->exit)
86 ioc->aic->exit(ioc->aic);
93 struct io_context *alloc_io_context(gfp_t gfp_flags, int node)
95 struct io_context *ret;
97 ret = kmem_cache_alloc_node(iocontext_cachep, gfp_flags, node);
99 atomic_set(&ret->refcount, 1);
100 atomic_set(&ret->nr_tasks, 1);
101 spin_lock_init(&ret->lock);
102 ret->ioprio_changed = 0;
104 ret->last_waited = jiffies; /* doesn't matter... */
105 ret->nr_batch_requests = 0; /* because this is 0 */
107 INIT_RADIX_TREE(&ret->radix_root, GFP_ATOMIC | __GFP_HIGH);
108 ret->ioc_data = NULL;
115 * If the current task has no IO context then create one and initialise it.
116 * Otherwise, return its existing IO context.
118 * This returned IO context doesn't have a specifically elevated refcount,
119 * but since the current task itself holds a reference, the context can be
120 * used in general code, so long as it stays within `current` context.
122 struct io_context *current_io_context(gfp_t gfp_flags, int node)
124 struct task_struct *tsk = current;
125 struct io_context *ret;
127 ret = tsk->io_context;
131 ret = alloc_io_context(gfp_flags, node);
133 /* make sure set_task_ioprio() sees the settings above */
135 tsk->io_context = ret;
142 * If the current task has no IO context then create one and initialise it.
143 * If it does have a context, take a ref on it.
145 * This is always called in the context of the task which submitted the I/O.
147 struct io_context *get_io_context(gfp_t gfp_flags, int node)
149 struct io_context *ret = NULL;
152 * Check for unlikely race with exiting task. ioc ref count is
153 * zero when ioc is being detached.
156 ret = current_io_context(gfp_flags, node);
159 } while (!atomic_inc_not_zero(&ret->refcount));
163 EXPORT_SYMBOL(get_io_context);
165 void copy_io_context(struct io_context **pdst, struct io_context **psrc)
167 struct io_context *src = *psrc;
168 struct io_context *dst = *pdst;
171 BUG_ON(atomic_read(&src->refcount) == 0);
172 atomic_inc(&src->refcount);
177 EXPORT_SYMBOL(copy_io_context);
179 int __init blk_ioc_init(void)
181 iocontext_cachep = kmem_cache_create("blkdev_ioc",
182 sizeof(struct io_context), 0, SLAB_PANIC, NULL);
185 subsys_initcall(blk_ioc_init);