Skip to content

Commit fea9f92

Browse files
John Garryaxboe
authored andcommitted
blk-mq: Optimise blk_mq_queue_tag_busy_iter() for shared tags
Kashyap reports high CPU usage in blk_mq_queue_tag_busy_iter() and callees using megaraid SAS RAID card since moving to shared tags [0]. Previously, when shared tags was shared sbitmap, this function was less than optimum since we would iter through all tags for all hctx's, yet only ever match upto tagset depth number of rqs. Since the change to shared tags, things are even less efficient if we have parallel callers of blk_mq_queue_tag_busy_iter(). This is because in bt_iter() -> blk_mq_find_and_get_req() there would be more contention on accessing each request ref and tags->lock since they are now shared among all HW queues. Optimise by having separate calls to bt_for_each() for when we're using shared tags. In this case no longer pass a hctx, as it is no longer relevant, and teach bt_iter() about this. Ming suggested something along the lines of this change, apart from a different implementation. [0] https://lore.kernel.org/linux-block/e4e92abbe9d52bcba6b8cc6c91c442cc@mail.gmail.com/ Signed-off-by: John Garry <john.garry@huawei.com> Reviewed-by: Hannes Reinecke <hare@suse.de> Reviewed-by: Ming Lei <ming.lei@redhat.com> Reported-and-tested-by: Kashyap Desai <kashyap.desai@broadcom.com> Fixes: e155b0c ("blk-mq: Use shared tags for shared sbitmap support") Link: https://lore.kernel.org/r/1638794990-137490-4-git-send-email-john.garry@huawei.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent fc39f8d commit fea9f92

1 file changed

Lines changed: 41 additions & 18 deletions

File tree

‎block/blk-mq-tag.c‎

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ void blk_mq_put_tags(struct blk_mq_tags *tags, int *tag_array, int nr_tags)
215215

216216
struct bt_iter_data {
217217
struct blk_mq_hw_ctx *hctx;
218+
struct request_queue *q;
218219
busy_tag_iter_fn *fn;
219220
void *data;
220221
bool reserved;
@@ -238,11 +239,18 @@ static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
238239
{
239240
struct bt_iter_data *iter_data = data;
240241
struct blk_mq_hw_ctx *hctx = iter_data->hctx;
241-
struct blk_mq_tags *tags = hctx->tags;
242+
struct request_queue *q = iter_data->q;
243+
struct blk_mq_tag_set *set = q->tag_set;
242244
bool reserved = iter_data->reserved;
245+
struct blk_mq_tags *tags;
243246
struct request *rq;
244247
bool ret = true;
245248

249+
if (blk_mq_is_shared_tags(set->flags))
250+
tags = set->shared_tags;
251+
else
252+
tags = hctx->tags;
253+
246254
if (!reserved)
247255
bitnr += tags->nr_reserved_tags;
248256
/*
@@ -253,7 +261,7 @@ static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
253261
if (!rq)
254262
return true;
255263

256-
if (rq->q == hctx->queue && rq->mq_hctx == hctx)
264+
if (rq->q == q && (!hctx || rq->mq_hctx == hctx))
257265
ret = iter_data->fn(rq, iter_data->data, reserved);
258266
blk_mq_put_rq_ref(rq);
259267
return ret;
@@ -262,6 +270,7 @@ static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
262270
/**
263271
* bt_for_each - iterate over the requests associated with a hardware queue
264272
* @hctx: Hardware queue to examine.
273+
* @q: Request queue to examine.
265274
* @bt: sbitmap to examine. This is either the breserved_tags member
266275
* or the bitmap_tags member of struct blk_mq_tags.
267276
* @fn: Pointer to the function that will be called for each request
@@ -273,14 +282,16 @@ static bool bt_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data)
273282
* @reserved: Indicates whether @bt is the breserved_tags member or the
274283
* bitmap_tags member of struct blk_mq_tags.
275284
*/
276-
static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct sbitmap_queue *bt,
277-
busy_tag_iter_fn *fn, void *data, bool reserved)
285+
static void bt_for_each(struct blk_mq_hw_ctx *hctx, struct request_queue *q,
286+
struct sbitmap_queue *bt, busy_tag_iter_fn *fn,
287+
void *data, bool reserved)
278288
{
279289
struct bt_iter_data iter_data = {
280290
.hctx = hctx,
281291
.fn = fn,
282292
.data = data,
283293
.reserved = reserved,
294+
.q = q,
284295
};
285296

286297
sbitmap_for_each_set(&bt->sb, bt_iter, &iter_data);
@@ -460,9 +471,6 @@ EXPORT_SYMBOL(blk_mq_tagset_wait_completed_request);
460471
void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn,
461472
void *priv)
462473
{
463-
struct blk_mq_hw_ctx *hctx;
464-
int i;
465-
466474
/*
467475
* __blk_mq_update_nr_hw_queues() updates nr_hw_queues and queue_hw_ctx
468476
* while the queue is frozen. So we can use q_usage_counter to avoid
@@ -471,19 +479,34 @@ void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn,
471479
if (!percpu_ref_tryget(&q->q_usage_counter))
472480
return;
473481

474-
queue_for_each_hw_ctx(q, hctx, i) {
475-
struct blk_mq_tags *tags = hctx->tags;
476-
477-
/*
478-
* If no software queues are currently mapped to this
479-
* hardware queue, there's nothing to check
480-
*/
481-
if (!blk_mq_hw_queue_mapped(hctx))
482-
continue;
482+
if (blk_mq_is_shared_tags(q->tag_set->flags)) {
483+
struct blk_mq_tags *tags = q->tag_set->shared_tags;
484+
struct sbitmap_queue *bresv = &tags->breserved_tags;
485+
struct sbitmap_queue *btags = &tags->bitmap_tags;
483486

484487
if (tags->nr_reserved_tags)
485-
bt_for_each(hctx, &tags->breserved_tags, fn, priv, true);
486-
bt_for_each(hctx, &tags->bitmap_tags, fn, priv, false);
488+
bt_for_each(NULL, q, bresv, fn, priv, true);
489+
bt_for_each(NULL, q, btags, fn, priv, false);
490+
} else {
491+
struct blk_mq_hw_ctx *hctx;
492+
int i;
493+
494+
queue_for_each_hw_ctx(q, hctx, i) {
495+
struct blk_mq_tags *tags = hctx->tags;
496+
struct sbitmap_queue *bresv = &tags->breserved_tags;
497+
struct sbitmap_queue *btags = &tags->bitmap_tags;
498+
499+
/*
500+
* If no software queues are currently mapped to this
501+
* hardware queue, there's nothing to check
502+
*/
503+
if (!blk_mq_hw_queue_mapped(hctx))
504+
continue;
505+
506+
if (tags->nr_reserved_tags)
507+
bt_for_each(hctx, q, bresv, fn, priv, true);
508+
bt_for_each(hctx, q, btags, fn, priv, false);
509+
}
487510
}
488511
blk_queue_exit(q);
489512
}

0 commit comments

Comments
 (0)