mbox series

[RFC,PATCHv3,0/2] block: remove q->sysfs_dir_lock and fix race updating nr_hw_queue

Message ID 20250128143436.874357-1-nilay@linux.ibm.com (mailing list archive)
Headers show
Series block: remove q->sysfs_dir_lock and fix race updating nr_hw_queue | expand

Message

Nilay Shroff Jan. 28, 2025, 2:34 p.m. UTC
Hi,

There're two patches in this patchest.
The first patch removes redundant q->sysfs_dir_lock.
The second patch fixes nr_hw_queue update racing with disk addition/
removal.

In the current implementation we use q->sysfs_dir_lock for protecting
kobject addition/deletion while we register/unregister blk-mq with
sysfs. However the sysfs/kernfs internal implementation already protects
against the simultaneous addtion/deletion of kobjects. So in that sense
use of q->sysfs_dir_lock appears redundant.

Furthermore, there're few other callsites in the current code where we
use q->sysfs_dir_lock along with q->sysfs_lock while addition/deletion
of independent access ranges for a disk under sysfs. Please refer, disk_
register_independent_access_ranges() and disk_unregister_independent_
access_ranges(). Here as well we could easily remove use of q->sysfs_dir_
lock.

The only thing which q->syfs_dir_lock appears to protect is the use of
variable q->mq_sysfs_init_done. However this could be solved by
converting q->mq_sysfs_init_done to an atomic variable.

In past few days, we have seen many lockdep splat in block layer and
getting rid of this one might help reduce some contention as well we
need to worry less about lock ordering wrt to q->sysfs_dir_lock. The
first patch helps fix this.

The second patch addresses a potential race between nr_hw_queue update
and disk addition/removal. The __blk_mq_update_nr_hw_queues function
removes and then adds hctx sysfs files. Similarly the disk addition/
removal code also adds or remove the hctx sysfs files. So it's quite
possible that disk addition/removal code could race with __blk_mq_update_
nr_hw_queues() while it adds/deletes hctx sysfs files. Apparently,
__blk_mq_update_nr_hw_queues() holds q->tag_list_lock while it runs,
and so to avoid race between __blk_mq_update_nr_hw_queues() and disk
addition/removal code, we should hold the same q->tag_list_lock while
we add/delete hctx sysfs files while registering/uregistering disk queue.
So the second patch in the series helps fix this race condition which may
manifests while we add/remove hctx sysfs files.

I ran blktests and xfstests with lockdep configured to ensure that above 
changes don't generate lockdep spalt.

Nilay Shroff (2):
  block: get rid of request queue ->sysfs_dir_lock
  block: fix nr_hw_queue update racing with disk addition/removal

Changes from v2:
  - few nitpicking fixes in the second patch (hch)
  - Link to v2: https://lore.kernel.org/all/20250123174124.24554-1-nilay@linux.ibm.com/

Changes from v1:
  - remove q->sysfs_init_done and replace it with registered queue
    flag (hch)
  - fix nr_hw_queue update racing with disk addition/removal (hch)
  - Link to v1: https://lore.kernel.org/all/20250120130413.789737-1-nilay@linux.ibm.com/

Nilay Shroff (2):
  block: get rid of request queue ->sysfs_dir_lock
  block: fix nr_hw_queue update racing with disk addition/removal

 block/blk-core.c       |  1 -
 block/blk-ia-ranges.c  |  4 ----
 block/blk-mq-sysfs.c   | 40 ++++++++++++++--------------------------
 block/blk-sysfs.c      |  5 -----
 include/linux/blkdev.h |  3 ---
 5 files changed, 14 insertions(+), 39 deletions(-)

Comments

Jens Axboe Jan. 29, 2025, 2:17 p.m. UTC | #1
On Tue, 28 Jan 2025 20:04:12 +0530, Nilay Shroff wrote:
> There're two patches in this patchest.
> The first patch removes redundant q->sysfs_dir_lock.
> The second patch fixes nr_hw_queue update racing with disk addition/
> removal.
> 
> In the current implementation we use q->sysfs_dir_lock for protecting
> kobject addition/deletion while we register/unregister blk-mq with
> sysfs. However the sysfs/kernfs internal implementation already protects
> against the simultaneous addtion/deletion of kobjects. So in that sense
> use of q->sysfs_dir_lock appears redundant.
> 
> [...]

Applied, thanks!

[1/2] block: get rid of request queue ->sysfs_dir_lock
      commit: fe6628608627424fb4a6d4c8d2235822457c5d9c
[2/2] block: fix nr_hw_queue update racing with disk addition/removal
      commit: 14ef49657ff3b7156952b2eadcf2e5bafd735795

Best regards,