When switching io scheduler via sysfs, 'request_module' may be called if the specified scheduler doesn't exist. This was has deadlock risk because the module may be stored on FS behind our disk since request queue is frozen before switching its elevator. Fix it by returning -EDEADLK in case that the disk is claimed, which can be thought as one signal that the disk is mounted. Some distributions(Fedora) simulates the original kernel command line of 'elevator=foo' via 'echo foo > /sys/block/$DISK/queue/scheduler', and boot hang is triggered. Cc: Richard Jones <rjones@xxxxxxxxxx> Cc: Jeff Moyer <jmoyer@xxxxxxxxxx> Cc: Jiri Jaburek <jjaburek@xxxxxxxxxx> Signed-off-by: Ming Lei <ming.lei@xxxxxxxxxx> --- block/elevator.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/block/elevator.c b/block/elevator.c index f13d552a32c8..2b0432f4ac33 100644 --- a/block/elevator.c +++ b/block/elevator.c @@ -676,6 +676,13 @@ void elevator_disable(struct request_queue *q) blk_mq_unfreeze_queue(q); } +static bool disk_is_claimed(struct gendisk *disk) +{ + if (disk->part0->bd_holder) + return true; + return false; +} + /* * Switch this queue to the given IO scheduler. */ @@ -699,6 +706,13 @@ static int elevator_change(struct request_queue *q, const char *elevator_name) e = elevator_find_get(q, elevator_name); if (!e) { + /* + * Try to avoid to load iosched module from FS behind our + * disk, otherwise deadlock may be triggered + */ + if (disk_is_claimed(q->disk)) + return -EDEADLK; + request_module("%s-iosched", elevator_name); e = elevator_find_get(q, elevator_name); if (!e) -- 2.46.0