Skip to content

Commit 995412e

Browse files
Ming Leiaxboe
authored andcommitted
blk-mq: Replace tags->lock with SRCU for tag iterators
Replace the spinlock in blk_mq_find_and_get_req() with an SRCU read lock around the tag iterators. This is done by: - Holding the SRCU read lock in blk_mq_queue_tag_busy_iter(), blk_mq_tagset_busy_iter(), and blk_mq_hctx_has_requests(). - Removing the now-redundant tags->lock from blk_mq_find_and_get_req(). This change fixes lockup issue in scsi_host_busy() in case of shost->host_blocked. Also avoids big tags->lock when reading disk sysfs attribute `inflight`. Reviewed-by: Hannes Reinecke <hare@suse.de> Signed-off-by: Ming Lei <ming.lei@redhat.com> Reviewed-by: Yu Kuai <yukuai3@huawei.com> Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 135b852 commit 995412e

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

block/blk-mq-tag.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,10 @@ static struct request *blk_mq_find_and_get_req(struct blk_mq_tags *tags,
256256
unsigned int bitnr)
257257
{
258258
struct request *rq;
259-
unsigned long flags;
260259

261-
spin_lock_irqsave(&tags->lock, flags);
262260
rq = tags->rqs[bitnr];
263261
if (!rq || rq->tag != bitnr || !req_ref_inc_not_zero(rq))
264262
rq = NULL;
265-
spin_unlock_irqrestore(&tags->lock, flags);
266263
return rq;
267264
}
268265

@@ -440,7 +437,9 @@ void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
440437
busy_tag_iter_fn *fn, void *priv)
441438
{
442439
unsigned int flags = tagset->flags;
443-
int i, nr_tags;
440+
int i, nr_tags, srcu_idx;
441+
442+
srcu_idx = srcu_read_lock(&tagset->tags_srcu);
444443

445444
nr_tags = blk_mq_is_shared_tags(flags) ? 1 : tagset->nr_hw_queues;
446445

@@ -449,6 +448,7 @@ void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
449448
__blk_mq_all_tag_iter(tagset->tags[i], fn, priv,
450449
BT_TAG_ITER_STARTED);
451450
}
451+
srcu_read_unlock(&tagset->tags_srcu, srcu_idx);
452452
}
453453
EXPORT_SYMBOL(blk_mq_tagset_busy_iter);
454454

@@ -499,6 +499,8 @@ EXPORT_SYMBOL(blk_mq_tagset_wait_completed_request);
499499
void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn,
500500
void *priv)
501501
{
502+
int srcu_idx;
503+
502504
/*
503505
* __blk_mq_update_nr_hw_queues() updates nr_hw_queues and hctx_table
504506
* while the queue is frozen. So we can use q_usage_counter to avoid
@@ -507,6 +509,7 @@ void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn,
507509
if (!percpu_ref_tryget(&q->q_usage_counter))
508510
return;
509511

512+
srcu_idx = srcu_read_lock(&q->tag_set->tags_srcu);
510513
if (blk_mq_is_shared_tags(q->tag_set->flags)) {
511514
struct blk_mq_tags *tags = q->tag_set->shared_tags;
512515
struct sbitmap_queue *bresv = &tags->breserved_tags;
@@ -536,6 +539,7 @@ void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn,
536539
bt_for_each(hctx, q, btags, fn, priv, false);
537540
}
538541
}
542+
srcu_read_unlock(&q->tag_set->tags_srcu, srcu_idx);
539543
blk_queue_exit(q);
540544
}
541545

block/blk-mq.c

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3415,7 +3415,6 @@ static void blk_mq_clear_rq_mapping(struct blk_mq_tags *drv_tags,
34153415
struct blk_mq_tags *tags)
34163416
{
34173417
struct page *page;
3418-
unsigned long flags;
34193418

34203419
/*
34213420
* There is no need to clear mapping if driver tags is not initialized
@@ -3439,15 +3438,6 @@ static void blk_mq_clear_rq_mapping(struct blk_mq_tags *drv_tags,
34393438
}
34403439
}
34413440
}
3442-
3443-
/*
3444-
* Wait until all pending iteration is done.
3445-
*
3446-
* Request reference is cleared and it is guaranteed to be observed
3447-
* after the ->lock is released.
3448-
*/
3449-
spin_lock_irqsave(&drv_tags->lock, flags);
3450-
spin_unlock_irqrestore(&drv_tags->lock, flags);
34513441
}
34523442

34533443
void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
@@ -3670,8 +3660,12 @@ static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)
36703660
struct rq_iter_data data = {
36713661
.hctx = hctx,
36723662
};
3663+
int srcu_idx;
36733664

3665+
srcu_idx = srcu_read_lock(&hctx->queue->tag_set->tags_srcu);
36743666
blk_mq_all_tag_iter(tags, blk_mq_has_request, &data);
3667+
srcu_read_unlock(&hctx->queue->tag_set->tags_srcu, srcu_idx);
3668+
36753669
return data.has_rq;
36763670
}
36773671

@@ -3891,7 +3885,6 @@ static void blk_mq_clear_flush_rq_mapping(struct blk_mq_tags *tags,
38913885
unsigned int queue_depth, struct request *flush_rq)
38923886
{
38933887
int i;
3894-
unsigned long flags;
38953888

38963889
/* The hw queue may not be mapped yet */
38973890
if (!tags)
@@ -3901,15 +3894,6 @@ static void blk_mq_clear_flush_rq_mapping(struct blk_mq_tags *tags,
39013894

39023895
for (i = 0; i < queue_depth; i++)
39033896
cmpxchg(&tags->rqs[i], flush_rq, NULL);
3904-
3905-
/*
3906-
* Wait until all pending iteration is done.
3907-
*
3908-
* Request reference is cleared and it is guaranteed to be observed
3909-
* after the ->lock is released.
3910-
*/
3911-
spin_lock_irqsave(&tags->lock, flags);
3912-
spin_unlock_irqrestore(&tags->lock, flags);
39133897
}
39143898

39153899
static void blk_free_flush_queue_callback(struct rcu_head *head)

0 commit comments

Comments
 (0)