Re: [PATCH v3 2/7] block: take i_rwsem for the direct I/O write fallback
From: Shin'ichiro Kawasaki
Date: Thu Sep 17 2026 - 22:56:30 EST
On Sep 09, 2026 / 17:59, Tal Zussman wrote:
> Commit c0e473a0d226 ("block: fix race between set_blocksize and read
> paths") closed a race between set_blocksize() and block device I/O: with
> large sector size support, set_blocksize() can change i_blkbits and the
> mapping's minimum folio order while a concurrent reader still holds a
> folio of the old, smaller order, leading to crashes. In particular, it
> made blkdev_write_iter() wrap buffered writes in inode_lock_shared().
>
> However, the direct I/O fallback path was missed in that conversion.
> blkdev_write_iter() passes blkdev_buffered_write() as an argument to
> direct_write_fallback() with no lock held. A direct write that completes
> only partially then finishes as a buffered write with no protection.
[...]
> Fix this by calling blkdev_buffered_write() in the fallback path under
> inode_lock_shared(), matching the plain buffered-write branch. With the
> fix the same workload runs clean.
>
> A short IOCB_NOWAIT direct write reaches the same fallback. Taking
> i_rwsem there can now block behind set_blocksize(), and the fallback
> already blocks on writeback of the data it copied in
> direct_write_fallback(). blkdev_write_iter() already rejects a purely
> buffered IOCB_NOWAIT write with -EOPNOTSUPP, so do not enter the
> fallback for IOCB_NOWAIT at all: return the bytes the direct path
> already wrote, or -EAGAIN if none, and let the caller retry.
>
> The reproducer used was written by an LLM, and is available at [1].
>
> [1] https://gist.github.com/tzussman/69d06bc57d42a42989eb038b1b5aeb74
>
> Fixes: 3c20917120ce ("block/bdev: enable large folio support for large logical block sizes")
> Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
> Link: https://sashiko.dev/#/patchset/20260730-blk-dontcache-v7-0-3e8e6850068d%40columbia.edu?part=5
> Assisted-by: Claude:claude-fable-5
> Reviewed-by: Hannes Reinecke <hare@xxxxxxxxxx>
> Reviewed-by: Christoph Hellwig <hch@xxxxxx>
> Signed-off-by: Tal Zussman <tz2294@xxxxxxxxxxxx>
I confirmed that this patch avoids the hang triggered by the corresponding, new
blktests test case [*].
Tested-by: Shin'ichiro Kawasaki <shinichiro.kawasaki@xxxxxxx>
[*] https://lore.kernel.org/linux-block/20260909-blkdev-fixes-tests-v1-1-1f8af8665d16@xxxxxxxxxxxx/