Skip to content

Commit b445547

Browse files
kadesai16axboe
authored andcommitted
blk-mq, elevator: Count requests per hctx to improve performance
High CPU utilization on "native_queued_spin_lock_slowpath" due to lock contention is possible for mq-deadline and bfq IO schedulers when nr_hw_queues is more than one. It is because kblockd work queue can submit IO from all online CPUs (through blk_mq_run_hw_queues()) even though only one hctx has pending commands. The elevator callback .has_work for mq-deadline and bfq scheduler considers pending work if there are any IOs on request queue but it does not account hctx context. Add a per-hctx 'elevator_queued' count to the hctx to avoid triggering the elevator even though there are no requests queued. [jpg: Relocated atomic_dec() in dd_dispatch_request(), update commit message per Kashyap] Signed-off-by: Kashyap Desai <[email protected]> Signed-off-by: Hannes Reinecke <[email protected]> Signed-off-by: John Garry <[email protected]> Tested-by: Douglas Gilbert <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent f1b49fd commit b445547

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

block/bfq-iosched.c

+5
Original file line numberDiff line numberDiff line change
@@ -4640,6 +4640,9 @@ static bool bfq_has_work(struct blk_mq_hw_ctx *hctx)
46404640
{
46414641
struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
46424642

4643+
if (!atomic_read(&hctx->elevator_queued))
4644+
return false;
4645+
46434646
/*
46444647
* Avoiding lock: a race on bfqd->busy_queues should cause at
46454648
* most a call to dispatch for nothing
@@ -5554,6 +5557,7 @@ static void bfq_insert_requests(struct blk_mq_hw_ctx *hctx,
55545557
rq = list_first_entry(list, struct request, queuelist);
55555558
list_del_init(&rq->queuelist);
55565559
bfq_insert_request(hctx, rq, at_head);
5560+
atomic_inc(&hctx->elevator_queued);
55575561
}
55585562
}
55595563

@@ -5933,6 +5937,7 @@ static void bfq_finish_requeue_request(struct request *rq)
59335937

59345938
bfq_completed_request(bfqq, bfqd);
59355939
bfq_finish_requeue_request_body(bfqq);
5940+
atomic_dec(&rq->mq_hctx->elevator_queued);
59365941

59375942
spin_unlock_irqrestore(&bfqd->lock, flags);
59385943
} else {

block/blk-mq.c

+1
Original file line numberDiff line numberDiff line change
@@ -2660,6 +2660,7 @@ blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
26602660
goto free_hctx;
26612661

26622662
atomic_set(&hctx->nr_active, 0);
2663+
atomic_set(&hctx->elevator_queued, 0);
26632664
if (node == NUMA_NO_NODE)
26642665
node = set->numa_node;
26652666
hctx->numa_node = node;

block/mq-deadline.c

+6
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ static struct request *dd_dispatch_request(struct blk_mq_hw_ctx *hctx)
386386
spin_lock(&dd->lock);
387387
rq = __dd_dispatch_request(dd);
388388
spin_unlock(&dd->lock);
389+
if (rq)
390+
atomic_dec(&rq->mq_hctx->elevator_queued);
389391

390392
return rq;
391393
}
@@ -533,6 +535,7 @@ static void dd_insert_requests(struct blk_mq_hw_ctx *hctx,
533535
rq = list_first_entry(list, struct request, queuelist);
534536
list_del_init(&rq->queuelist);
535537
dd_insert_request(hctx, rq, at_head);
538+
atomic_inc(&hctx->elevator_queued);
536539
}
537540
spin_unlock(&dd->lock);
538541
}
@@ -579,6 +582,9 @@ static bool dd_has_work(struct blk_mq_hw_ctx *hctx)
579582
{
580583
struct deadline_data *dd = hctx->queue->elevator->elevator_data;
581584

585+
if (!atomic_read(&hctx->elevator_queued))
586+
return false;
587+
582588
return !list_empty_careful(&dd->dispatch) ||
583589
!list_empty_careful(&dd->fifo_list[0]) ||
584590
!list_empty_careful(&dd->fifo_list[1]);

include/linux/blk-mq.h

+4
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ struct blk_mq_hw_ctx {
139139
* shared across request queues.
140140
*/
141141
atomic_t nr_active;
142+
/**
143+
* @elevator_queued: Number of queued requests on hctx.
144+
*/
145+
atomic_t elevator_queued;
142146

143147
/** @cpuhp_online: List to store request if CPU is going to die */
144148
struct hlist_node cpuhp_online;

0 commit comments

Comments
 (0)