Skip to content

Commit c0d9368

Browse files
author
Ming Lei
committed
block: protect hctx attributes/params using q->elevator_lock
JIRA: https://issues.redhat.com/browse/RHEL-112997 commit 5abba4c Author: Nilay Shroff <nilay@linux.ibm.com> Date: Thu Mar 6 15:09:53 2025 +0530 block: protect hctx attributes/params using q->elevator_lock Currently, hctx attributes (nr_tags, nr_reserved_tags, and cpu_list) are protected using `q->sysfs_lock`. However, these attributes can be updated in multiple scenarios: - During the driver's probe method. - When updating nr_hw_queues. - When writing to the sysfs attribute nr_requests, which can modify nr_tags. The nr_requests attribute is already protected using q->elevator_lock, but none of the update paths actually use q->sysfs_lock to protect hctx attributes. So to ensure proper synchronization, replace q->sysfs_lock with q->elevator_lock when reading hctx attributes through sysfs. Additionally, blk_mq_update_nr_hw_queues allocates and updates hctx. The allocation of hctx is protected using q->elevator_lock, however, updating hctx params happens without any protection, so safeguard hctx param update path by also using q->elevator_lock. Signed-off-by: Nilay Shroff <nilay@linux.ibm.com> Link: https://lore.kernel.org/r/20250306093956.2818808-1-nilay@linux.ibm.com [axboe: wrap comment at 80 chars] Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Ming Lei <ming.lei@redhat.com>
1 parent 7569354 commit c0d9368

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

block/blk-mq-sysfs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ static ssize_t blk_mq_hw_sysfs_show(struct kobject *kobj,
6161
if (!entry->show)
6262
return -EIO;
6363

64-
mutex_lock(&q->sysfs_lock);
64+
mutex_lock(&q->elevator_lock);
6565
res = entry->show(hctx, page);
66-
mutex_unlock(&q->sysfs_lock);
66+
mutex_unlock(&q->elevator_lock);
6767
return res;
6868
}
6969

block/blk-mq.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4095,6 +4095,8 @@ static void blk_mq_map_swqueue(struct request_queue *q)
40954095
struct blk_mq_ctx *ctx;
40964096
struct blk_mq_tag_set *set = q->tag_set;
40974097

4098+
mutex_lock(&q->elevator_lock);
4099+
40984100
queue_for_each_hw_ctx(q, hctx, i) {
40994101
cpumask_clear(hctx->cpumask);
41004102
hctx->nr_ctx = 0;
@@ -4199,6 +4201,8 @@ static void blk_mq_map_swqueue(struct request_queue *q)
41994201
hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
42004202
hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
42014203
}
4204+
4205+
mutex_unlock(&q->elevator_lock);
42024206
}
42034207

42044208
/*

include/linux/blkdev.h

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

582582
/*
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 and wbt latency, this lock also
586-
* protects the sysfs attributes nr_requests and wbt_lat_usec.
587-
* To ensure proper locking order during an elevator update, first
588-
* freeze the queue, then acquire ->elevator_lock.
583+
* Protects against I/O scheduler switching, particularly when updating
584+
* q->elevator. Since the elevator update code path may also modify q->
585+
* nr_requests and wbt latency, this lock also protects the sysfs attrs
586+
* nr_requests and wbt_lat_usec. Additionally the nr_hw_queues update
587+
* may modify hctx tags, reserved-tags and cpumask, so this lock also
588+
* helps protect the hctx attrs. To ensure proper locking order during
589+
* an elevator or nr_hw_queue update, first freeze the queue, then
590+
* acquire ->elevator_lock.
589591
*/
590592
struct mutex elevator_lock;
591593

0 commit comments

Comments
 (0)