Skip to content

Commit 3c70398

Browse files
author
Ming Lei
committed
block: protect nr_requests update using q->elevator_lock
JIRA: https://issues.redhat.com/browse/RHEL-112997 commit 3efe757 Author: Nilay Shroff <nilay@linux.ibm.com> Date: Tue Mar 4 15:52:34 2025 +0530 block: protect nr_requests update using q->elevator_lock The sysfs attribute nr_requests could be simultaneously updated from elevator switch/update or nr_hw_queue update code path. The update to nr_requests for each of those code paths runs holding q->elevator_lock. So we should protect access to sysfs attribute nr_requests using q-> elevator_lock instead of q->sysfs_lock. Reviewed-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Hannes Reinecke <hare@suse.de> Reviewed-by: Ming Lei <ming.lei@redhat.com> Signed-off-by: Nilay Shroff <nilay@linux.ibm.com> Link: https://lore.kernel.org/r/20250304102551.2533767-6-nilay@linux.ibm.com Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Ming Lei <ming.lei@redhat.com>
1 parent 33f5f61 commit 3c70398

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

block/blk-sysfs.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ static ssize_t queue_requests_show(struct gendisk *disk, char *page)
5555
{
5656
ssize_t ret;
5757

58-
mutex_lock(&disk->queue->sysfs_lock);
58+
mutex_lock(&disk->queue->elevator_lock);
5959
ret = queue_var_show(disk->queue->nr_requests, page);
60-
mutex_unlock(&disk->queue->sysfs_lock);
60+
mutex_unlock(&disk->queue->elevator_lock);
6161
return ret;
6262
}
6363

@@ -76,16 +76,16 @@ queue_requests_store(struct gendisk *disk, const char *page, size_t count)
7676
if (ret < 0)
7777
return ret;
7878

79-
mutex_lock(&q->sysfs_lock);
8079
memflags = blk_mq_freeze_queue(q);
80+
mutex_lock(&q->elevator_lock);
8181
if (nr < BLKDEV_MIN_RQ)
8282
nr = BLKDEV_MIN_RQ;
8383

8484
err = blk_mq_update_nr_requests(disk->queue, nr);
8585
if (err)
8686
ret = err;
87+
mutex_unlock(&q->elevator_lock);
8788
blk_mq_unfreeze_queue(q, memflags);
88-
mutex_unlock(&q->sysfs_lock);
8989
return ret;
9090
}
9191

@@ -698,7 +698,6 @@ static struct attribute *blk_mq_queue_attrs[] = {
698698
/*
699699
* Attributes which are protected with q->sysfs_lock.
700700
*/
701-
&queue_requests_entry.attr,
702701
#ifdef CONFIG_BLK_WBT
703702
&queue_wb_lat_entry.attr,
704703
#endif
@@ -707,6 +706,7 @@ static struct attribute *blk_mq_queue_attrs[] = {
707706
* q->sysfs_lock.
708707
*/
709708
&elv_iosched_entry.attr,
709+
&queue_requests_entry.attr,
710710

711711
/*
712712
* Attributes which don't require locking.

include/linux/blkdev.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -580,10 +580,12 @@ struct request_queue {
580580
struct list_head flush_list;
581581

582582
/*
583-
* Protects against I/O scheduler switching, specifically when
584-
* updating q->elevator. To ensure proper locking order during
585-
* an elevator update, first freeze the queue, then acquire
586-
* ->elevator_lock.
583+
* Protects against I/O scheduler switching, particularly when
584+
* updating q->elevator. Since the elevator update code path may
585+
* also modify q->nr_requests, this lock also protects the sysfs
586+
* attribute nr_requests.
587+
* To ensure proper locking order during an elevator update, first
588+
* freeze the queue, then acquire ->elevator_lock.
587589
*/
588590
struct mutex elevator_lock;
589591

0 commit comments

Comments
 (0)