Skip to content

Commit 734e1a8

Browse files
damien-lemoalaxboe
authored andcommitted
block: Prevent deadlocks when switching elevators
Commit af28141 ("block: freeze the queue in queue_attr_store") changed queue_attr_store() to always freeze a sysfs attribute queue before calling the attribute store() method, to ensure that no IOs are in-flight when an attribute value is being updated. However, this change created a potential deadlock situation for the scheduler queue attribute as changing the queue elevator with elv_iosched_store() can result in a call to request_module() if the user requested module is not already registered. If the file of the requested module is stored on the block device of the frozen queue, a deadlock will happen as the read operations triggered by request_module() will wait for the queue freeze to end. Solve this issue by introducing the load_module method in struct queue_sysfs_entry, and to calling this method function in queue_attr_store() before freezing the attribute queue. The macro definition QUEUE_RW_LOAD_MODULE_ENTRY() is added to define a queue sysfs attribute that needs loading a module. The definition of the scheduler atrribute is changed to using QUEUE_RW_LOAD_MODULE_ENTRY(), with the function elv_iosched_load_module() defined as the load_module method. elv_iosched_store() can then be simplified to remove the call to request_module(). Reported-by: Richard W.M. Jones <rjones@redhat.com> Reported-by: Jiri Jaburek <jjaburek@redhat.com> Closes: https://bugzilla.kernel.org/show_bug.cgi?id=219166 Fixes: af28141 ("block: freeze the queue in queue_attr_store") Cc: stable@vger.kernel.org Signed-off-by: Damien Le Moal <dlemoal@kernel.org> Tested-by: Richard W.M. Jones <rjones@redhat.com> Link: https://lore.kernel.org/r/20240908000704.414538-1-dlemoal@kernel.org Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 4ba032b commit 734e1a8

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

block/blk-sysfs.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
struct queue_sysfs_entry {
2424
struct attribute attr;
2525
ssize_t (*show)(struct gendisk *disk, char *page);
26+
int (*load_module)(struct gendisk *disk, const char *page, size_t count);
2627
ssize_t (*store)(struct gendisk *disk, const char *page, size_t count);
2728
};
2829

@@ -413,14 +414,22 @@ static struct queue_sysfs_entry _prefix##_entry = { \
413414
.store = _prefix##_store, \
414415
};
415416

417+
#define QUEUE_RW_LOAD_MODULE_ENTRY(_prefix, _name) \
418+
static struct queue_sysfs_entry _prefix##_entry = { \
419+
.attr = { .name = _name, .mode = 0644 }, \
420+
.show = _prefix##_show, \
421+
.load_module = _prefix##_load_module, \
422+
.store = _prefix##_store, \
423+
}
424+
416425
QUEUE_RW_ENTRY(queue_requests, "nr_requests");
417426
QUEUE_RW_ENTRY(queue_ra, "read_ahead_kb");
418427
QUEUE_RW_ENTRY(queue_max_sectors, "max_sectors_kb");
419428
QUEUE_RO_ENTRY(queue_max_hw_sectors, "max_hw_sectors_kb");
420429
QUEUE_RO_ENTRY(queue_max_segments, "max_segments");
421430
QUEUE_RO_ENTRY(queue_max_integrity_segments, "max_integrity_segments");
422431
QUEUE_RO_ENTRY(queue_max_segment_size, "max_segment_size");
423-
QUEUE_RW_ENTRY(elv_iosched, "scheduler");
432+
QUEUE_RW_LOAD_MODULE_ENTRY(elv_iosched, "scheduler");
424433

425434
QUEUE_RO_ENTRY(queue_logical_block_size, "logical_block_size");
426435
QUEUE_RO_ENTRY(queue_physical_block_size, "physical_block_size");
@@ -670,6 +679,17 @@ queue_attr_store(struct kobject *kobj, struct attribute *attr,
670679
if (!entry->store)
671680
return -EIO;
672681

682+
/*
683+
* If the attribute needs to load a module, do it before freezing the
684+
* queue to ensure that the module file can be read when the request
685+
* queue is the one for the device storing the module file.
686+
*/
687+
if (entry->load_module) {
688+
res = entry->load_module(disk, page, length);
689+
if (res)
690+
return res;
691+
}
692+
673693
blk_mq_freeze_queue(q);
674694
mutex_lock(&q->sysfs_lock);
675695
res = entry->store(disk, page, length);

block/elevator.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -698,17 +698,26 @@ static int elevator_change(struct request_queue *q, const char *elevator_name)
698698
return 0;
699699

700700
e = elevator_find_get(q, elevator_name);
701-
if (!e) {
702-
request_module("%s-iosched", elevator_name);
703-
e = elevator_find_get(q, elevator_name);
704-
if (!e)
705-
return -EINVAL;
706-
}
701+
if (!e)
702+
return -EINVAL;
707703
ret = elevator_switch(q, e);
708704
elevator_put(e);
709705
return ret;
710706
}
711707

708+
int elv_iosched_load_module(struct gendisk *disk, const char *buf,
709+
size_t count)
710+
{
711+
char elevator_name[ELV_NAME_MAX];
712+
713+
if (!elv_support_iosched(disk->queue))
714+
return -EOPNOTSUPP;
715+
716+
strscpy(elevator_name, buf, sizeof(elevator_name));
717+
718+
return request_module("%s-iosched", strstrip(elevator_name));
719+
}
720+
712721
ssize_t elv_iosched_store(struct gendisk *disk, const char *buf,
713722
size_t count)
714723
{

block/elevator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ extern void elv_unregister(struct elevator_type *);
148148
* io scheduler sysfs switching
149149
*/
150150
ssize_t elv_iosched_show(struct gendisk *disk, char *page);
151+
int elv_iosched_load_module(struct gendisk *disk, const char *page,
152+
size_t count);
151153
ssize_t elv_iosched_store(struct gendisk *disk, const char *page, size_t count);
152154

153155
extern bool elv_bio_merge_ok(struct request *, struct bio *);

0 commit comments

Comments
 (0)