diff options
Diffstat (limited to 'drivers/block/ll_rw_blk.c')
-rw-r--r-- | drivers/block/ll_rw_blk.c | 151 |
1 files changed, 71 insertions, 80 deletions
diff --git a/drivers/block/ll_rw_blk.c b/drivers/block/ll_rw_blk.c index 234fdcfbdf01..692a5fced76e 100644 --- a/drivers/block/ll_rw_blk.c +++ b/drivers/block/ll_rw_blk.c | |||
@@ -1867,19 +1867,20 @@ static void freed_request(request_queue_t *q, int rw) | |||
1867 | 1867 | ||
1868 | #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist) | 1868 | #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist) |
1869 | /* | 1869 | /* |
1870 | * Get a free request, queue_lock must not be held | 1870 | * Get a free request, queue_lock must be held. |
1871 | * Returns NULL on failure, with queue_lock held. | ||
1872 | * Returns !NULL on success, with queue_lock *not held*. | ||
1871 | */ | 1873 | */ |
1872 | static struct request *get_request(request_queue_t *q, int rw, struct bio *bio, | 1874 | static struct request *get_request(request_queue_t *q, int rw, struct bio *bio, |
1873 | int gfp_mask) | 1875 | int gfp_mask) |
1874 | { | 1876 | { |
1875 | struct request *rq = NULL; | 1877 | struct request *rq = NULL; |
1876 | struct request_list *rl = &q->rq; | 1878 | struct request_list *rl = &q->rq; |
1877 | struct io_context *ioc = get_io_context(gfp_mask); | 1879 | struct io_context *ioc = current_io_context(GFP_ATOMIC); |
1878 | 1880 | ||
1879 | if (unlikely(test_bit(QUEUE_FLAG_DRAIN, &q->queue_flags))) | 1881 | if (unlikely(test_bit(QUEUE_FLAG_DRAIN, &q->queue_flags))) |
1880 | goto out; | 1882 | goto out; |
1881 | 1883 | ||
1882 | spin_lock_irq(q->queue_lock); | ||
1883 | if (rl->count[rw]+1 >= q->nr_requests) { | 1884 | if (rl->count[rw]+1 >= q->nr_requests) { |
1884 | /* | 1885 | /* |
1885 | * The queue will fill after this allocation, so set it as | 1886 | * The queue will fill after this allocation, so set it as |
@@ -1907,11 +1908,18 @@ static struct request *get_request(request_queue_t *q, int rw, struct bio *bio, | |||
1907 | * The queue is full and the allocating process is not a | 1908 | * The queue is full and the allocating process is not a |
1908 | * "batcher", and not exempted by the IO scheduler | 1909 | * "batcher", and not exempted by the IO scheduler |
1909 | */ | 1910 | */ |
1910 | spin_unlock_irq(q->queue_lock); | ||
1911 | goto out; | 1911 | goto out; |
1912 | } | 1912 | } |
1913 | 1913 | ||
1914 | get_rq: | 1914 | get_rq: |
1915 | /* | ||
1916 | * Only allow batching queuers to allocate up to 50% over the defined | ||
1917 | * limit of requests, otherwise we could have thousands of requests | ||
1918 | * allocated with any setting of ->nr_requests | ||
1919 | */ | ||
1920 | if (rl->count[rw] >= (3 * q->nr_requests / 2)) | ||
1921 | goto out; | ||
1922 | |||
1915 | rl->count[rw]++; | 1923 | rl->count[rw]++; |
1916 | rl->starved[rw] = 0; | 1924 | rl->starved[rw] = 0; |
1917 | if (rl->count[rw] >= queue_congestion_on_threshold(q)) | 1925 | if (rl->count[rw] >= queue_congestion_on_threshold(q)) |
@@ -1941,7 +1949,6 @@ rq_starved: | |||
1941 | if (unlikely(rl->count[rw] == 0)) | 1949 | if (unlikely(rl->count[rw] == 0)) |
1942 | rl->starved[rw] = 1; | 1950 | rl->starved[rw] = 1; |
1943 | 1951 | ||
1944 | spin_unlock_irq(q->queue_lock); | ||
1945 | goto out; | 1952 | goto out; |
1946 | } | 1953 | } |
1947 | 1954 | ||
@@ -1951,21 +1958,23 @@ rq_starved: | |||
1951 | rq_init(q, rq); | 1958 | rq_init(q, rq); |
1952 | rq->rl = rl; | 1959 | rq->rl = rl; |
1953 | out: | 1960 | out: |
1954 | put_io_context(ioc); | ||
1955 | return rq; | 1961 | return rq; |
1956 | } | 1962 | } |
1957 | 1963 | ||
1958 | /* | 1964 | /* |
1959 | * No available requests for this queue, unplug the device and wait for some | 1965 | * No available requests for this queue, unplug the device and wait for some |
1960 | * requests to become available. | 1966 | * requests to become available. |
1967 | * | ||
1968 | * Called with q->queue_lock held, and returns with it unlocked. | ||
1961 | */ | 1969 | */ |
1962 | static struct request *get_request_wait(request_queue_t *q, int rw, | 1970 | static struct request *get_request_wait(request_queue_t *q, int rw, |
1963 | struct bio *bio) | 1971 | struct bio *bio) |
1964 | { | 1972 | { |
1965 | DEFINE_WAIT(wait); | ||
1966 | struct request *rq; | 1973 | struct request *rq; |
1967 | 1974 | ||
1968 | do { | 1975 | rq = get_request(q, rw, bio, GFP_NOIO); |
1976 | while (!rq) { | ||
1977 | DEFINE_WAIT(wait); | ||
1969 | struct request_list *rl = &q->rq; | 1978 | struct request_list *rl = &q->rq; |
1970 | 1979 | ||
1971 | prepare_to_wait_exclusive(&rl->wait[rw], &wait, | 1980 | prepare_to_wait_exclusive(&rl->wait[rw], &wait, |
@@ -1976,7 +1985,8 @@ static struct request *get_request_wait(request_queue_t *q, int rw, | |||
1976 | if (!rq) { | 1985 | if (!rq) { |
1977 | struct io_context *ioc; | 1986 | struct io_context *ioc; |
1978 | 1987 | ||
1979 | generic_unplug_device(q); | 1988 | __generic_unplug_device(q); |
1989 | spin_unlock_irq(q->queue_lock); | ||
1980 | io_schedule(); | 1990 | io_schedule(); |
1981 | 1991 | ||
1982 | /* | 1992 | /* |
@@ -1985,12 +1995,13 @@ static struct request *get_request_wait(request_queue_t *q, int rw, | |||
1985 | * up to a big batch of them for a small period time. | 1995 | * up to a big batch of them for a small period time. |
1986 | * See ioc_batching, ioc_set_batching | 1996 | * See ioc_batching, ioc_set_batching |
1987 | */ | 1997 | */ |
1988 | ioc = get_io_context(GFP_NOIO); | 1998 | ioc = current_io_context(GFP_NOIO); |
1989 | ioc_set_batching(q, ioc); | 1999 | ioc_set_batching(q, ioc); |
1990 | put_io_context(ioc); | 2000 | |
2001 | spin_lock_irq(q->queue_lock); | ||
1991 | } | 2002 | } |
1992 | finish_wait(&rl->wait[rw], &wait); | 2003 | finish_wait(&rl->wait[rw], &wait); |
1993 | } while (!rq); | 2004 | } |
1994 | 2005 | ||
1995 | return rq; | 2006 | return rq; |
1996 | } | 2007 | } |
@@ -2001,14 +2012,18 @@ struct request *blk_get_request(request_queue_t *q, int rw, int gfp_mask) | |||
2001 | 2012 | ||
2002 | BUG_ON(rw != READ && rw != WRITE); | 2013 | BUG_ON(rw != READ && rw != WRITE); |
2003 | 2014 | ||
2004 | if (gfp_mask & __GFP_WAIT) | 2015 | spin_lock_irq(q->queue_lock); |
2016 | if (gfp_mask & __GFP_WAIT) { | ||
2005 | rq = get_request_wait(q, rw, NULL); | 2017 | rq = get_request_wait(q, rw, NULL); |
2006 | else | 2018 | } else { |
2007 | rq = get_request(q, rw, NULL, gfp_mask); | 2019 | rq = get_request(q, rw, NULL, gfp_mask); |
2020 | if (!rq) | ||
2021 | spin_unlock_irq(q->queue_lock); | ||
2022 | } | ||
2023 | /* q->queue_lock is unlocked at this point */ | ||
2008 | 2024 | ||
2009 | return rq; | 2025 | return rq; |
2010 | } | 2026 | } |
2011 | |||
2012 | EXPORT_SYMBOL(blk_get_request); | 2027 | EXPORT_SYMBOL(blk_get_request); |
2013 | 2028 | ||
2014 | /** | 2029 | /** |
@@ -2512,7 +2527,7 @@ EXPORT_SYMBOL(blk_attempt_remerge); | |||
2512 | 2527 | ||
2513 | static int __make_request(request_queue_t *q, struct bio *bio) | 2528 | static int __make_request(request_queue_t *q, struct bio *bio) |
2514 | { | 2529 | { |
2515 | struct request *req, *freereq = NULL; | 2530 | struct request *req; |
2516 | int el_ret, rw, nr_sectors, cur_nr_sectors, barrier, err, sync; | 2531 | int el_ret, rw, nr_sectors, cur_nr_sectors, barrier, err, sync; |
2517 | unsigned short prio; | 2532 | unsigned short prio; |
2518 | sector_t sector; | 2533 | sector_t sector; |
@@ -2540,14 +2555,9 @@ static int __make_request(request_queue_t *q, struct bio *bio) | |||
2540 | goto end_io; | 2555 | goto end_io; |
2541 | } | 2556 | } |
2542 | 2557 | ||
2543 | again: | ||
2544 | spin_lock_irq(q->queue_lock); | 2558 | spin_lock_irq(q->queue_lock); |
2545 | 2559 | ||
2546 | if (elv_queue_empty(q)) { | 2560 | if (unlikely(barrier) || elv_queue_empty(q)) |
2547 | blk_plug_device(q); | ||
2548 | goto get_rq; | ||
2549 | } | ||
2550 | if (barrier) | ||
2551 | goto get_rq; | 2561 | goto get_rq; |
2552 | 2562 | ||
2553 | el_ret = elv_merge(q, &req, bio); | 2563 | el_ret = elv_merge(q, &req, bio); |
@@ -2592,40 +2602,24 @@ again: | |||
2592 | elv_merged_request(q, req); | 2602 | elv_merged_request(q, req); |
2593 | goto out; | 2603 | goto out; |
2594 | 2604 | ||
2595 | /* | 2605 | /* ELV_NO_MERGE: elevator says don't/can't merge. */ |
2596 | * elevator says don't/can't merge. get new request | ||
2597 | */ | ||
2598 | case ELEVATOR_NO_MERGE: | ||
2599 | break; | ||
2600 | |||
2601 | default: | 2606 | default: |
2602 | printk("elevator returned crap (%d)\n", el_ret); | 2607 | ; |
2603 | BUG(); | ||
2604 | } | 2608 | } |
2605 | 2609 | ||
2610 | get_rq: | ||
2606 | /* | 2611 | /* |
2607 | * Grab a free request from the freelist - if that is empty, check | 2612 | * Grab a free request. This is might sleep but can not fail. |
2608 | * if we are doing read ahead and abort instead of blocking for | 2613 | * Returns with the queue unlocked. |
2609 | * a free slot. | 2614 | */ |
2615 | req = get_request_wait(q, rw, bio); | ||
2616 | |||
2617 | /* | ||
2618 | * After dropping the lock and possibly sleeping here, our request | ||
2619 | * may now be mergeable after it had proven unmergeable (above). | ||
2620 | * We don't worry about that case for efficiency. It won't happen | ||
2621 | * often, and the elevators are able to handle it. | ||
2610 | */ | 2622 | */ |
2611 | get_rq: | ||
2612 | if (freereq) { | ||
2613 | req = freereq; | ||
2614 | freereq = NULL; | ||
2615 | } else { | ||
2616 | spin_unlock_irq(q->queue_lock); | ||
2617 | if ((freereq = get_request(q, rw, bio, GFP_ATOMIC)) == NULL) { | ||
2618 | /* | ||
2619 | * READA bit set | ||
2620 | */ | ||
2621 | err = -EWOULDBLOCK; | ||
2622 | if (bio_rw_ahead(bio)) | ||
2623 | goto end_io; | ||
2624 | |||
2625 | freereq = get_request_wait(q, rw, bio); | ||
2626 | } | ||
2627 | goto again; | ||
2628 | } | ||
2629 | 2623 | ||
2630 | req->flags |= REQ_CMD; | 2624 | req->flags |= REQ_CMD; |
2631 | 2625 | ||
@@ -2654,10 +2648,11 @@ get_rq: | |||
2654 | req->rq_disk = bio->bi_bdev->bd_disk; | 2648 | req->rq_disk = bio->bi_bdev->bd_disk; |
2655 | req->start_time = jiffies; | 2649 | req->start_time = jiffies; |
2656 | 2650 | ||
2651 | spin_lock_irq(q->queue_lock); | ||
2652 | if (elv_queue_empty(q)) | ||
2653 | blk_plug_device(q); | ||
2657 | add_request(q, req); | 2654 | add_request(q, req); |
2658 | out: | 2655 | out: |
2659 | if (freereq) | ||
2660 | __blk_put_request(q, freereq); | ||
2661 | if (sync) | 2656 | if (sync) |
2662 | __generic_unplug_device(q); | 2657 | __generic_unplug_device(q); |
2663 | 2658 | ||
@@ -3284,24 +3279,20 @@ void exit_io_context(void) | |||
3284 | 3279 | ||
3285 | /* | 3280 | /* |
3286 | * If the current task has no IO context then create one and initialise it. | 3281 | * If the current task has no IO context then create one and initialise it. |
3287 | * If it does have a context, take a ref on it. | 3282 | * Otherwise, return its existing IO context. |
3288 | * | 3283 | * |
3289 | * This is always called in the context of the task which submitted the I/O. | 3284 | * This returned IO context doesn't have a specifically elevated refcount, |
3290 | * But weird things happen, so we disable local interrupts to ensure exclusive | 3285 | * but since the current task itself holds a reference, the context can be |
3291 | * access to *current. | 3286 | * used in general code, so long as it stays within `current` context. |
3292 | */ | 3287 | */ |
3293 | struct io_context *get_io_context(int gfp_flags) | 3288 | struct io_context *current_io_context(int gfp_flags) |
3294 | { | 3289 | { |
3295 | struct task_struct *tsk = current; | 3290 | struct task_struct *tsk = current; |
3296 | unsigned long flags; | ||
3297 | struct io_context *ret; | 3291 | struct io_context *ret; |
3298 | 3292 | ||
3299 | local_irq_save(flags); | ||
3300 | ret = tsk->io_context; | 3293 | ret = tsk->io_context; |
3301 | if (ret) | 3294 | if (likely(ret)) |
3302 | goto out; | 3295 | return ret; |
3303 | |||
3304 | local_irq_restore(flags); | ||
3305 | 3296 | ||
3306 | ret = kmem_cache_alloc(iocontext_cachep, gfp_flags); | 3297 | ret = kmem_cache_alloc(iocontext_cachep, gfp_flags); |
3307 | if (ret) { | 3298 | if (ret) { |
@@ -3312,25 +3303,25 @@ struct io_context *get_io_context(int gfp_flags) | |||
3312 | ret->nr_batch_requests = 0; /* because this is 0 */ | 3303 | ret->nr_batch_requests = 0; /* because this is 0 */ |
3313 | ret->aic = NULL; | 3304 | ret->aic = NULL; |
3314 | ret->cic = NULL; | 3305 | ret->cic = NULL; |
3306 | tsk->io_context = ret; | ||
3307 | } | ||
3315 | 3308 | ||
3316 | local_irq_save(flags); | 3309 | return ret; |
3317 | 3310 | } | |
3318 | /* | 3311 | EXPORT_SYMBOL(current_io_context); |
3319 | * very unlikely, someone raced with us in setting up the task | ||
3320 | * io context. free new context and just grab a reference. | ||
3321 | */ | ||
3322 | if (!tsk->io_context) | ||
3323 | tsk->io_context = ret; | ||
3324 | else { | ||
3325 | kmem_cache_free(iocontext_cachep, ret); | ||
3326 | ret = tsk->io_context; | ||
3327 | } | ||
3328 | 3312 | ||
3329 | out: | 3313 | /* |
3314 | * If the current task has no IO context then create one and initialise it. | ||
3315 | * If it does have a context, take a ref on it. | ||
3316 | * | ||
3317 | * This is always called in the context of the task which submitted the I/O. | ||
3318 | */ | ||
3319 | struct io_context *get_io_context(int gfp_flags) | ||
3320 | { | ||
3321 | struct io_context *ret; | ||
3322 | ret = current_io_context(gfp_flags); | ||
3323 | if (likely(ret)) | ||
3330 | atomic_inc(&ret->refcount); | 3324 | atomic_inc(&ret->refcount); |
3331 | local_irq_restore(flags); | ||
3332 | } | ||
3333 | |||
3334 | return ret; | 3325 | return ret; |
3335 | } | 3326 | } |
3336 | EXPORT_SYMBOL(get_io_context); | 3327 | EXPORT_SYMBOL(get_io_context); |