Skip to content

Commit 3efe757

Browse files
shroffniaxboe
authored andcommitted
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>
1 parent 1bf70d0 commit 3efe757

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

@@ -692,7 +692,6 @@ static struct attribute *blk_mq_queue_attrs[] = {
692692
/*
693693
* Attributes which are protected with q->sysfs_lock.
694694
*/
695-
&queue_requests_entry.attr,
696695
#ifdef CONFIG_BLK_WBT
697696
&queue_wb_lat_entry.attr,
698697
#endif
@@ -701,6 +700,7 @@ static struct attribute *blk_mq_queue_attrs[] = {
701700
* q->sysfs_lock.
702701
*/
703702
&elv_iosched_entry.attr,
703+
&queue_requests_entry.attr,
704704

705705
/*
706706
* Attributes which don't require locking.

include/linux/blkdev.h

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

563563
/*
564-
* Protects against I/O scheduler switching, specifically when
565-
* updating q->elevator. To ensure proper locking order during
566-
* an elevator update, first freeze the queue, then acquire
567-
* ->elevator_lock.
564+
* Protects against I/O scheduler switching, particularly when
565+
* updating q->elevator. Since the elevator update code path may
566+
* also modify q->nr_requests, this lock also protects the sysfs
567+
* attribute nr_requests.
568+
* To ensure proper locking order during an elevator update, first
569+
* freeze the queue, then acquire ->elevator_lock.
568570
*/
569571
struct mutex elevator_lock;
570572

0 commit comments

Comments
 (0)