Skip to content

Commit fb3cc43

Browse files
Nick PigginLinus Torvalds
authored andcommitted
[PATCH] blk: light iocontext ops
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 <nickpiggin@yahoo.com.au> Cc: Jens Axboe <axboe@suse.de> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent d634453 commit fb3cc43

2 files changed

Lines changed: 26 additions & 31 deletions

File tree

‎drivers/block/ll_rw_blk.c‎

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,7 +1876,7 @@ static struct request *get_request(request_queue_t *q, int rw, struct bio *bio,
18761876
{
18771877
struct request *rq = NULL;
18781878
struct request_list *rl = &q->rq;
1879-
struct io_context *ioc = get_io_context(GFP_ATOMIC);
1879+
struct io_context *ioc = current_io_context(GFP_ATOMIC);
18801880

18811881
if (unlikely(test_bit(QUEUE_FLAG_DRAIN, &q->queue_flags)))
18821882
goto out;
@@ -1959,7 +1959,6 @@ static struct request *get_request(request_queue_t *q, int rw, struct bio *bio,
19591959
rq_init(q, rq);
19601960
rq->rl = rl;
19611961
out:
1962-
put_io_context(ioc);
19631962
return rq;
19641963
}
19651964

@@ -1997,9 +1996,8 @@ static struct request *get_request_wait(request_queue_t *q, int rw,
19971996
* up to a big batch of them for a small period time.
19981997
* See ioc_batching, ioc_set_batching
19991998
*/
2000-
ioc = get_io_context(GFP_NOIO);
1999+
ioc = current_io_context(GFP_NOIO);
20012000
ioc_set_batching(q, ioc);
2002-
put_io_context(ioc);
20032001

20042002
spin_lock_irq(q->queue_lock);
20052003
}
@@ -3282,24 +3280,20 @@ void exit_io_context(void)
32823280

32833281
/*
32843282
* If the current task has no IO context then create one and initialise it.
3285-
* If it does have a context, take a ref on it.
3283+
* Otherwise, return its existing IO context.
32863284
*
3287-
* This is always called in the context of the task which submitted the I/O.
3288-
* But weird things happen, so we disable local interrupts to ensure exclusive
3289-
* access to *current.
3285+
* This returned IO context doesn't have a specifically elevated refcount,
3286+
* but since the current task itself holds a reference, the context can be
3287+
* used in general code, so long as it stays within `current` context.
32903288
*/
3291-
struct io_context *get_io_context(int gfp_flags)
3289+
struct io_context *current_io_context(int gfp_flags)
32923290
{
32933291
struct task_struct *tsk = current;
3294-
unsigned long flags;
32953292
struct io_context *ret;
32963293

3297-
local_irq_save(flags);
32983294
ret = tsk->io_context;
3299-
if (ret)
3300-
goto out;
3301-
3302-
local_irq_restore(flags);
3295+
if (likely(ret))
3296+
return ret;
33033297

33043298
ret = kmem_cache_alloc(iocontext_cachep, gfp_flags);
33053299
if (ret) {
@@ -3310,25 +3304,25 @@ struct io_context *get_io_context(int gfp_flags)
33103304
ret->nr_batch_requests = 0; /* because this is 0 */
33113305
ret->aic = NULL;
33123306
ret->cic = NULL;
3307+
tsk->io_context = ret;
3308+
}
33133309

3314-
local_irq_save(flags);
3315-
3316-
/*
3317-
* very unlikely, someone raced with us in setting up the task
3318-
* io context. free new context and just grab a reference.
3319-
*/
3320-
if (!tsk->io_context)
3321-
tsk->io_context = ret;
3322-
else {
3323-
kmem_cache_free(iocontext_cachep, ret);
3324-
ret = tsk->io_context;
3325-
}
3310+
return ret;
3311+
}
3312+
EXPORT_SYMBOL(current_io_context);
33263313

3327-
out:
3314+
/*
3315+
* If the current task has no IO context then create one and initialise it.
3316+
* If it does have a context, take a ref on it.
3317+
*
3318+
* This is always called in the context of the task which submitted the I/O.
3319+
*/
3320+
struct io_context *get_io_context(int gfp_flags)
3321+
{
3322+
struct io_context *ret;
3323+
ret = current_io_context(gfp_flags);
3324+
if (likely(ret))
33283325
atomic_inc(&ret->refcount);
3329-
local_irq_restore(flags);
3330-
}
3331-
33323326
return ret;
33333327
}
33343328
EXPORT_SYMBOL(get_io_context);

‎include/linux/blkdev.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ struct io_context {
9696

9797
void put_io_context(struct io_context *ioc);
9898
void exit_io_context(void);
99+
struct io_context *current_io_context(int gfp_flags);
99100
struct io_context *get_io_context(int gfp_flags);
100101
void copy_io_context(struct io_context **pdst, struct io_context **psrc);
101102
void swap_io_context(struct io_context **ioc1, struct io_context **ioc2);

0 commit comments

Comments
 (0)