Commit 4bb659b1 authored by Jens Axboe's avatar Jens Axboe

blk-mq: implement new and more efficient tagging scheme

blk-mq currently uses percpu_ida for tag allocation. But that only
works well if the ratio between tag space and number of CPUs is
sufficiently high. For most devices and systems, that is not the
case. The end result if that we either only utilize the tag space
partially, or we end up attempting to fully exhaust it and run
into lots of lock contention with stealing between CPUs. This is
not optimal.

This new tagging scheme is a hybrid bitmap allocator. It uses
two tricks to both be SMP friendly and allow full exhaustion
of the space:

1) We cache the last allocated (or freed) tag on a per blk-mq
   software context basis. This allows us to limit the space
   we have to search. The key element here is not caching it
   in the shared tag structure, otherwise we end up dirtying
   more shared cache lines on each allocate/free operation.

2) The tag space is split into cache line sized groups, and
   each context will start off randomly in that space. Even up
   to full utilization of the space, this divides the tag users
   efficiently into cache line groups, avoiding dirtying the same
   one both between allocators and between allocator and freeer.

This scheme shows drastically better behaviour, both on small
tag spaces but on large ones as well. It has been tested extensively
to show better performance for all the cases blk-mq cares about.
Signed-off-by: default avatarJens Axboe <axboe@fb.com>
parent af76e555
This diff is collapsed.
#ifndef INT_BLK_MQ_TAG_H #ifndef INT_BLK_MQ_TAG_H
#define INT_BLK_MQ_TAG_H #define INT_BLK_MQ_TAG_H
#include <linux/percpu_ida.h> enum {
BT_WAIT_QUEUES = 8,
BT_WAIT_BATCH = 8,
};
struct bt_wait_state {
atomic_t wait_cnt;
wait_queue_head_t wait;
} ____cacheline_aligned_in_smp;
#define TAG_TO_INDEX(tag) ((tag) / BITS_PER_LONG)
#define TAG_TO_BIT(tag) ((tag) & (BITS_PER_LONG - 1))
struct blk_mq_bitmap {
unsigned long word;
unsigned long depth;
} ____cacheline_aligned_in_smp;
struct blk_mq_bitmap_tags {
unsigned int depth;
unsigned int wake_cnt;
struct blk_mq_bitmap *map;
unsigned int map_nr;
unsigned int wake_index;
struct bt_wait_state *bs;
};
/* /*
* Tag address space map. * Tag address space map.
...@@ -9,11 +36,9 @@ ...@@ -9,11 +36,9 @@
struct blk_mq_tags { struct blk_mq_tags {
unsigned int nr_tags; unsigned int nr_tags;
unsigned int nr_reserved_tags; unsigned int nr_reserved_tags;
unsigned int nr_batch_move;
unsigned int nr_max_cache;
struct percpu_ida free_tags; struct blk_mq_bitmap_tags bitmap_tags;
struct percpu_ida reserved_tags; struct blk_mq_bitmap_tags breserved_tags;
struct request **rqs; struct request **rqs;
struct list_head page_list; struct list_head page_list;
...@@ -23,12 +48,13 @@ struct blk_mq_tags { ...@@ -23,12 +48,13 @@ struct blk_mq_tags {
extern struct blk_mq_tags *blk_mq_init_tags(unsigned int nr_tags, unsigned int reserved_tags, int node); extern struct blk_mq_tags *blk_mq_init_tags(unsigned int nr_tags, unsigned int reserved_tags, int node);
extern void blk_mq_free_tags(struct blk_mq_tags *tags); extern void blk_mq_free_tags(struct blk_mq_tags *tags);
extern unsigned int blk_mq_get_tag(struct blk_mq_tags *tags, gfp_t gfp, bool reserved); extern unsigned int blk_mq_get_tag(struct blk_mq_tags *tags, struct blk_mq_hw_ctx *hctx, unsigned int *last_tag, gfp_t gfp, bool reserved);
extern void blk_mq_wait_for_tags(struct blk_mq_tags *tags, bool reserved); extern void blk_mq_wait_for_tags(struct blk_mq_tags *tags, struct blk_mq_hw_ctx *hctx, bool reserved);
extern void blk_mq_put_tag(struct blk_mq_tags *tags, unsigned int tag); extern void blk_mq_put_tag(struct blk_mq_tags *tags, unsigned int tag, unsigned int *last_tag);
extern void blk_mq_tag_busy_iter(struct blk_mq_tags *tags, void (*fn)(void *data, unsigned long *), void *data); extern void blk_mq_tag_busy_iter(struct blk_mq_tags *tags, void (*fn)(void *data, unsigned long *), void *data);
extern bool blk_mq_has_free_tags(struct blk_mq_tags *tags); extern bool blk_mq_has_free_tags(struct blk_mq_tags *tags);
extern ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page); extern ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page);
extern void blk_mq_tag_init_last_tag(struct blk_mq_tags *tags, unsigned int *last_tag);
enum { enum {
BLK_MQ_TAG_CACHE_MIN = 1, BLK_MQ_TAG_CACHE_MIN = 1,
......
...@@ -74,12 +74,13 @@ static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx, ...@@ -74,12 +74,13 @@ static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
} }
static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx, static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,
struct blk_mq_ctx *ctx,
gfp_t gfp, bool reserved) gfp_t gfp, bool reserved)
{ {
struct request *rq; struct request *rq;
unsigned int tag; unsigned int tag;
tag = blk_mq_get_tag(hctx->tags, gfp, reserved); tag = blk_mq_get_tag(hctx->tags, hctx, &ctx->last_tag, gfp, reserved);
if (tag != BLK_MQ_TAG_FAIL) { if (tag != BLK_MQ_TAG_FAIL) {
rq = hctx->tags->rqs[tag]; rq = hctx->tags->rqs[tag];
rq->tag = tag; rq->tag = tag;
...@@ -246,7 +247,8 @@ static struct request *blk_mq_alloc_request_pinned(struct request_queue *q, ...@@ -246,7 +247,8 @@ static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
struct blk_mq_ctx *ctx = blk_mq_get_ctx(q); struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu); struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved); rq = __blk_mq_alloc_request(hctx, ctx, gfp & ~__GFP_WAIT,
reserved);
if (rq) { if (rq) {
blk_mq_rq_ctx_init(q, ctx, rq, rw); blk_mq_rq_ctx_init(q, ctx, rq, rw);
break; break;
...@@ -260,7 +262,7 @@ static struct request *blk_mq_alloc_request_pinned(struct request_queue *q, ...@@ -260,7 +262,7 @@ static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
break; break;
} }
blk_mq_wait_for_tags(hctx->tags, reserved); blk_mq_wait_for_tags(hctx->tags, hctx, reserved);
} while (1); } while (1);
return rq; return rq;
...@@ -278,6 +280,7 @@ struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp) ...@@ -278,6 +280,7 @@ struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
blk_mq_put_ctx(rq->mq_ctx); blk_mq_put_ctx(rq->mq_ctx);
return rq; return rq;
} }
EXPORT_SYMBOL(blk_mq_alloc_request);
struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw, struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
gfp_t gfp) gfp_t gfp)
...@@ -301,7 +304,7 @@ static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx, ...@@ -301,7 +304,7 @@ static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
struct request_queue *q = rq->q; struct request_queue *q = rq->q;
clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags); clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
blk_mq_put_tag(hctx->tags, tag); blk_mq_put_tag(hctx->tags, tag, &ctx->last_tag);
blk_mq_queue_exit(q); blk_mq_queue_exit(q);
} }
...@@ -677,11 +680,6 @@ static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx) ...@@ -677,11 +680,6 @@ static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
queued++; queued++;
continue; continue;
case BLK_MQ_RQ_QUEUE_BUSY: case BLK_MQ_RQ_QUEUE_BUSY:
/*
* FIXME: we should have a mechanism to stop the queue
* like blk_stop_queue, otherwise we will waste cpu
* time
*/
list_add(&rq->queuelist, &rq_list); list_add(&rq->queuelist, &rq_list);
__blk_mq_requeue_request(rq); __blk_mq_requeue_request(rq);
break; break;
...@@ -873,6 +871,7 @@ static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, ...@@ -873,6 +871,7 @@ static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
list_add(&rq->queuelist, &ctx->rq_list); list_add(&rq->queuelist, &ctx->rq_list);
else else
list_add_tail(&rq->queuelist, &ctx->rq_list); list_add_tail(&rq->queuelist, &ctx->rq_list);
blk_mq_hctx_mark_pending(hctx, ctx); blk_mq_hctx_mark_pending(hctx, ctx);
/* /*
...@@ -1046,7 +1045,7 @@ static void blk_mq_make_request(struct request_queue *q, struct bio *bio) ...@@ -1046,7 +1045,7 @@ static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
if (is_sync) if (is_sync)
rw |= REQ_SYNC; rw |= REQ_SYNC;
trace_block_getrq(q, bio, rw); trace_block_getrq(q, bio, rw);
rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false); rq = __blk_mq_alloc_request(hctx, ctx, GFP_ATOMIC, false);
if (likely(rq)) if (likely(rq))
blk_mq_rq_ctx_init(q, ctx, rq, rw); blk_mq_rq_ctx_init(q, ctx, rq, rw);
else { else {
...@@ -1130,8 +1129,8 @@ EXPORT_SYMBOL(blk_mq_map_queue); ...@@ -1130,8 +1129,8 @@ EXPORT_SYMBOL(blk_mq_map_queue);
struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *set, struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *set,
unsigned int hctx_index) unsigned int hctx_index)
{ {
return kmalloc_node(sizeof(struct blk_mq_hw_ctx), return kzalloc_node(sizeof(struct blk_mq_hw_ctx), GFP_KERNEL,
GFP_KERNEL | __GFP_ZERO, set->numa_node); set->numa_node);
} }
EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue); EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
......
...@@ -12,6 +12,8 @@ struct blk_mq_ctx { ...@@ -12,6 +12,8 @@ struct blk_mq_ctx {
unsigned int cpu; unsigned int cpu;
unsigned int index_hw; unsigned int index_hw;
unsigned int last_tag ____cacheline_aligned_in_smp;
/* incremented at dispatch time */ /* incremented at dispatch time */
unsigned long rq_dispatched[2]; unsigned long rq_dispatched[2];
unsigned long rq_merged; unsigned long rq_merged;
...@@ -21,7 +23,7 @@ struct blk_mq_ctx { ...@@ -21,7 +23,7 @@ struct blk_mq_ctx {
struct request_queue *queue; struct request_queue *queue;
struct kobject kobj; struct kobject kobj;
}; } ____cacheline_aligned_in_smp;
void __blk_mq_complete_request(struct request *rq); void __blk_mq_complete_request(struct request *rq);
void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async); void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async);
......
...@@ -31,10 +31,12 @@ struct blk_mq_hw_ctx { ...@@ -31,10 +31,12 @@ struct blk_mq_hw_ctx {
void *driver_data; void *driver_data;
unsigned int nr_ctx;
struct blk_mq_ctx **ctxs;
unsigned int nr_ctx_map; unsigned int nr_ctx_map;
unsigned long *ctx_map; unsigned long *ctx_map;
unsigned int nr_ctx;
struct blk_mq_ctx **ctxs;
unsigned int wait_index;
struct blk_mq_tags *tags; struct blk_mq_tags *tags;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment