Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Nilay Shroff and committed by
Jens Axboe
5abba4ce 5e40f445

+14 -8
+2 -2
block/blk-mq-sysfs.c
··· 61 61 if (!entry->show) 62 62 return -EIO; 63 63 64 - mutex_lock(&q->sysfs_lock); 64 + mutex_lock(&q->elevator_lock); 65 65 res = entry->show(hctx, page); 66 - mutex_unlock(&q->sysfs_lock); 66 + mutex_unlock(&q->elevator_lock); 67 67 return res; 68 68 } 69 69
+4
block/blk-mq.c
··· 4094 4094 struct blk_mq_ctx *ctx; 4095 4095 struct blk_mq_tag_set *set = q->tag_set; 4096 4096 4097 + mutex_lock(&q->elevator_lock); 4098 + 4097 4099 queue_for_each_hw_ctx(q, hctx, i) { 4098 4100 cpumask_clear(hctx->cpumask); 4099 4101 hctx->nr_ctx = 0; ··· 4200 4198 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx); 4201 4199 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH; 4202 4200 } 4201 + 4202 + mutex_unlock(&q->elevator_lock); 4203 4203 } 4204 4204 4205 4205 /*
+8 -6
include/linux/blkdev.h
··· 561 561 struct list_head flush_list; 562 562 563 563 /* 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 and wbt latency, this lock also 567 - * protects the sysfs attributes nr_requests and wbt_lat_usec. 568 - * To ensure proper locking order during an elevator update, first 569 - * freeze the queue, then acquire ->elevator_lock. 564 + * Protects against I/O scheduler switching, particularly when updating 565 + * q->elevator. Since the elevator update code path may also modify q-> 566 + * nr_requests and wbt latency, this lock also protects the sysfs attrs 567 + * nr_requests and wbt_lat_usec. Additionally the nr_hw_queues update 568 + * may modify hctx tags, reserved-tags and cpumask, so this lock also 569 + * helps protect the hctx attrs. To ensure proper locking order during 570 + * an elevator or nr_hw_queue update, first freeze the queue, then 571 + * acquire ->elevator_lock. 570 572 */ 571 573 struct mutex elevator_lock; 572 574