Skip to content

Commit ce8ee85

Browse files
Ming Leiaxboe
authored andcommitted
block: use trylock to avoid lockdep circular dependency in sysfs
Use trylock instead of blocking lock acquisition for update_nr_hwq_lock in queue_requests_store() and elv_iosched_store() to avoid circular lock dependency with kernfs active reference during concurrent disk deletion: update_nr_hwq_lock -> kn->active (via del_gendisk -> kobject_del) kn->active -> update_nr_hwq_lock (via sysfs write path) Return -EBUSY when the lock is not immediately available. Reported-and-tested-by: Yi Zhang <[email protected]> Closes: https://lore.kernel.org/linux-block/CAHj4cs-em-4acsHabMdT=jJhXkCzjnprD-aQH1OgrZo4nTnmMw@mail.gmail.com/ Fixes: 626ff4f ("blk-mq: convert to serialize updating nr_requests with update_nr_hwq_lock") Signed-off-by: Ming Lei <[email protected]> Tested-by: Yi Zhang <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent d90c470 commit ce8ee85

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

block/blk-sysfs.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,14 @@ queue_requests_store(struct gendisk *disk, const char *page, size_t count)
7878
/*
7979
* Serialize updating nr_requests with concurrent queue_requests_store()
8080
* and switching elevator.
81+
*
82+
* Use trylock to avoid circular lock dependency with kernfs active
83+
* reference during concurrent disk deletion:
84+
* update_nr_hwq_lock -> kn->active (via del_gendisk -> kobject_del)
85+
* kn->active -> update_nr_hwq_lock (via this sysfs write path)
8186
*/
82-
down_write(&set->update_nr_hwq_lock);
87+
if (!down_write_trylock(&set->update_nr_hwq_lock))
88+
return -EBUSY;
8389

8490
if (nr == q->nr_requests)
8591
goto unlock;

block/elevator.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,16 @@ ssize_t elv_iosched_store(struct gendisk *disk, const char *buf,
807807
elv_iosched_load_module(ctx.name);
808808
ctx.type = elevator_find_get(ctx.name);
809809

810-
down_read(&set->update_nr_hwq_lock);
810+
/*
811+
* Use trylock to avoid circular lock dependency with kernfs active
812+
* reference during concurrent disk deletion:
813+
* update_nr_hwq_lock -> kn->active (via del_gendisk -> kobject_del)
814+
* kn->active -> update_nr_hwq_lock (via this sysfs write path)
815+
*/
816+
if (!down_read_trylock(&set->update_nr_hwq_lock)) {
817+
ret = -EBUSY;
818+
goto out;
819+
}
811820
if (!blk_queue_no_elv_switch(q)) {
812821
ret = elevator_change(q, &ctx);
813822
if (!ret)
@@ -817,6 +826,7 @@ ssize_t elv_iosched_store(struct gendisk *disk, const char *buf,
817826
}
818827
up_read(&set->update_nr_hwq_lock);
819828

829+
out:
820830
if (ctx.type)
821831
elevator_put(ctx.type);
822832
return ret;

0 commit comments

Comments
 (0)