From: Nick Piggin get_io_context needlessly turned off interrupts and checked for racing io context creations. Both of which aren't needed, because the io context can only be created while in process context of the current process. Also, split the function in 2. A light version, current_io_context does not elevate the reference count specifically, but can be used when in process context, because the process holds a reference itself. Signed-off-by: Nick Piggin Cc: Jens Axboe Signed-off-by: Andrew Morton --- drivers/block/ll_rw_blk.c | 56 ++++++++++++++++++++-------------------------- include/linux/blkdev.h | 1 2 files changed, 26 insertions(+), 31 deletions(-) diff -puN drivers/block/ll_rw_blk.c~blk-light-iocontext-ops drivers/block/ll_rw_blk.c --- 25/drivers/block/ll_rw_blk.c~blk-light-iocontext-ops 2005-05-03 20:53:50.000000000 -0700 +++ 25-akpm/drivers/block/ll_rw_blk.c 2005-05-03 20:53:50.000000000 -0700 @@ -1879,7 +1879,7 @@ static struct request *get_request(reque { struct request *rq = NULL; struct request_list *rl = &q->rq; - struct io_context *ioc = get_io_context(GFP_ATOMIC); + struct io_context *ioc = current_io_context(GFP_ATOMIC); if (unlikely(test_bit(QUEUE_FLAG_DRAIN, &q->queue_flags))) goto out; @@ -1953,7 +1953,6 @@ rq_starved: rq_init(q, rq); rq->rl = rl; out: - put_io_context(ioc); return rq; } @@ -1991,9 +1990,8 @@ static struct request *get_request_wait( * up to a big batch of them for a small period time. * See ioc_batching, ioc_set_batching */ - ioc = get_io_context(GFP_NOIO); + ioc = current_io_context(GFP_NOIO); ioc_set_batching(q, ioc); - put_io_context(ioc); spin_lock_irq(q->queue_lock); } @@ -3324,24 +3322,20 @@ void exit_io_context(void) /* * If the current task has no IO context then create one and initialise it. - * If it does have a context, take a ref on it. + * Otherwise, return its existing IO context. * - * This is always called in the context of the task which submitted the I/O. - * But weird things happen, so we disable local interrupts to ensure exclusive - * access to *current. + * This returned IO context doesn't have a specifically elevated refcount, + * but since the current task itself holds a reference, the context can be + * used in general code, so long as it stays within `current` context. */ -struct io_context *get_io_context(int gfp_flags) +struct io_context *current_io_context(int gfp_flags) { struct task_struct *tsk = current; - unsigned long flags; struct io_context *ret; - local_irq_save(flags); ret = tsk->io_context; - if (ret) - goto out; - - local_irq_restore(flags); + if (likely(ret)) + return ret; ret = kmem_cache_alloc(iocontext_cachep, gfp_flags); if (ret) { @@ -3352,25 +3346,25 @@ struct io_context *get_io_context(int gf ret->nr_batch_requests = 0; /* because this is 0 */ ret->aic = NULL; ret->cic = NULL; + tsk->io_context = ret; + } - local_irq_save(flags); - - /* - * very unlikely, someone raced with us in setting up the task - * io context. free new context and just grab a reference. - */ - if (!tsk->io_context) - tsk->io_context = ret; - else { - kmem_cache_free(iocontext_cachep, ret); - ret = tsk->io_context; - } + return ret; +} +EXPORT_SYMBOL(current_io_context); -out: +/* + * If the current task has no IO context then create one and initialise it. + * If it does have a context, take a ref on it. + * + * This is always called in the context of the task which submitted the I/O. + */ +struct io_context *get_io_context(int gfp_flags) +{ + struct io_context *ret; + ret = current_io_context(gfp_flags); + if (likely(ret)) atomic_inc(&ret->refcount); - local_irq_restore(flags); - } - return ret; } EXPORT_SYMBOL(get_io_context); diff -puN include/linux/blkdev.h~blk-light-iocontext-ops include/linux/blkdev.h --- 25/include/linux/blkdev.h~blk-light-iocontext-ops 2005-05-03 20:53:50.000000000 -0700 +++ 25-akpm/include/linux/blkdev.h 2005-05-03 20:53:50.000000000 -0700 @@ -96,6 +96,7 @@ struct io_context { void put_io_context(struct io_context *ioc); void exit_io_context(void); +struct io_context *current_io_context(int gfp_flags); struct io_context *get_io_context(int gfp_flags); void copy_io_context(struct io_context **pdst, struct io_context **psrc); void swap_io_context(struct io_context **ioc1, struct io_context **ioc2); _