Re: [PATCH] blk-mq: use mutex_trylock to avoid lock inversion

From: Bart Van Assche
Date: Wed Jun 20 2018 - 12:19:07 EST


On Wed, 2018-06-20 at 10:09 +0800, jianchao.wang wrote:
> It is very easy to reproduce with following scripts.
>
> script 0
> while true
> do
> modprobe null_blk queue_mode=2 shared_tags=1
> sleep 0.1
> rmmod null_blk
> sleep 0.1
> done
>
> script 1
> file0="/sys/block/nullb0/mq/0/nr_tags"
> file1="/sys/block/nullb0/mq/0/cpu0/rq_list"
> while true;
> do
> if [ -e $file0 ];then
> cat $file0
> fi
> if [ -e $file1 ];then
> cat $file1
> fi
> done

Hello Jianchao,

Thanks for having shared a reproducer. However, the approach of the patch you
posted doesn't seem like the right approach to me. I propose to proceed as
follows:
* Convert the reproducer into a blktests test and submit is as a patch to the
blktests project (https://github.com/osandov/blktests).
* Remove the mutex_lock/unlock(&sysfs_lock) calls from blk_cleanup_queue().
These calls are useless. Block drivers are required to call del_gendisk()
before calling blk_cleanup_queue(). That means that sysfs attributes are
removed synchronously before blk_cleanup_queue() is called. The following
statement in blk_cleanup_queue() verifies that:
WARN_ON_ONCE(q->kobj.state_in_sysfs);
BTW, this also means that the blk_queue_dying() checks in various show and
store methods are superfluous.
* Introduce a new mutex to serialize blk_mq_register_dev() and
blk_mq_sysfs_register() and blk_mq_sysfs_unregister(). I think it is wrong
that these functions use sysfs_mutex.
* Document the purpose of sysfs_mutex in include/linux/blkdev.h, namely to
serialize the sysfs .show() and .store() callback functions and also to
serialize elevator changes.

Thanks,

Bart.