A full mkfs.ntfs run on a raw NVMe device can finish writing but leave hundreds of GiB of device mapping cache for final close cleanup. On an ARM64 system with about 1 TiB of RAM and a 1.92 TB NVMe device, a vendor 6.6 diagnostic kernel without reclaim optimizations reproduced long udev open blocking in three consecutive runs. All three mkfs runs and the subsequent NTFS information checks completed successfully. The final truncate_inode_pages() calls took 423.282, 447.064 and 420.785 seconds with about 805 GiB of mapping pages at close. The corresponding udev opens took 423.282, 447.061 and 420.785 seconds. The blocked udev stacks went through mutex_lock(), blkdev_get_by_dev() and blkdev_open(); the diagnostic open_mutex wait values agreed at millisecond resolution. Each run produced hung-task warnings and one udev watchdog restart. The examined close path holds disk->open_mutex while the last-opener blkdev_put_whole() calls blkdev_flush_mapping(), kill_bdev() and truncate_inode_pages(). A concurrent opener needs the same mutex. In the recorded runs, the long interval was concentrated in cache teardown, not the separately recorded final sync. A successful sync can leave clean cache pages and associated metadata for this later teardown. The problem is therefore prolonged exclusion of other device openers during final cache cleanup, even when formatting itself succeeds. The 120-second hung-task reporting threshold is not an open_mutex timeout. The objective is to reduce time spent in that critical section, not to make the complete mkfs or close finish within 120 seconds. The evidence is from a vendor diagnostic kernel, not current upstream reproduction. Explore best-effort reclaim before notification and original release: at final __fput of a block-device file, call vfs_fsync(), then vfs_fadvise(POSIX_FADV_DONTNEED), before fsnotify_close(). Keep the original release responsible for any remaining pages and concurrent activity. Skip O_PATH and missing fsync operations before interpreting the block-device mapping. Log sync errors and skip advice; log advice errors and continue release. No new close error return is introduced. The hook applies to block-device files with a fsync operation, excluding O_PATH handles. It runs at the last reference to each such struct file, including files opened read-only or with O_DIRECT. It does not wait for the last device opener. A read-only close can therefore initiate shared mapping writeback and a device flush. Reclaim can evict useful cache. Sync/reclaim can block in __fput and delayed fput, and placing it before fsnotify_close changes when observers run. DONTNEED does not guarantee an empty mapping or a bound on later lock hold time. This is a design RFC based on the recorded vendor 6.6 tree. It is not rebased or validated on current mainline, nor proposed as a stable backport. Please advise whether this should remain a userspace action, be placed elsewhere in the block lifecycle, or be addressed through a more fundamental close-locking change before pursuing a mergeable fix. Signed-off-by: Ethan --- Baseline: recorded vendor Linux 6.6 source snapshot, not current mainline. The baseline measurements describe the problem, not results of this proposed patch. No additional build or runtime test was run while preparing this posting. Questions for reviewers: - Is __fput an appropriate context for this blocking work? - Should implicit sync and cache eviction remain a userspace choice? - If a kernel policy is appropriate, what eligibility rules avoid repeated work and interference with other mapping users? - How should delayed fput and close notification ordering be handled? - Would a different block close locking design be preferable? block/Makefile | 2 ++ block/bdev-preclose.c | 29 +++++++++++++++++++++++++++++ fs/file_table.c | 4 ++++ include/linux/bdev-preclose.h | 14 ++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 block/bdev-preclose.c create mode 100644 include/linux/bdev-preclose.h diff --git a/block/Makefile b/block/Makefile index 46ada9d..df775b4 100644 --- a/block/Makefile +++ b/block/Makefile @@ -11,6 +11,8 @@ obj-y := bdev.o fops.o bio.o elevator.o blk-core.o blk-sysfs.o \ genhd.o ioprio.o badblocks.o partitions/ blk-rq-qos.o \ disk-events.o blk-ia-ranges.o early-lookup.o +obj-y += bdev-preclose.o + obj-$(CONFIG_BOUNCE) += bounce.o obj-$(CONFIG_BLK_DEV_BSG_COMMON) += bsg.o obj-$(CONFIG_BLK_DEV_BSGLIB) += bsg-lib.o diff --git a/block/bdev-preclose.c b/block/bdev-preclose.c new file mode 100644 index 0000000..ba3849e --- /dev/null +++ b/block/bdev-preclose.c @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Best-effort cache reclaim before final release of a block-device file. */ +#include +#include +#include +#include +#include +#include "blk.h" + +/* The caller is __fput, before fsnotify_close and the original release. */ +void bdev_preclose_reclaim(struct file *file) +{ + int ret; + + /* A block-special inode can also be opened as a path-only handle. */ + if ((file->f_mode & FMODE_PATH) || !file->f_op || !file->f_op->fsync) + return; + + lockdep_assert_not_held(&I_BDEV(file->f_mapping->host)->bd_disk->open_mutex); + ret = vfs_fsync(file, 0); + if (ret) { + pr_err_ratelimited("bdev preclose: fsync failed (%d); skipping DONTNEED\n", + ret); + return; + } + ret = vfs_fadvise(file, 0, 0, POSIX_FADV_DONTNEED); + if (ret) + pr_err_ratelimited("bdev preclose: DONTNEED failed (%d)\n", ret); +} diff --git a/fs/file_table.c b/fs/file_table.c index ee21b3d..0b9d7a8 100644 --- a/fs/file_table.c +++ b/fs/file_table.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -367,6 +368,9 @@ static void __fput(struct file *file) might_sleep(); + /* Reclaim before notifying observers; original release remains below. */ + if (S_ISBLK(inode->i_mode)) + bdev_preclose_reclaim(file); fsnotify_close(file); /* * The function eventpoll_release() should be the first called diff --git a/include/linux/bdev-preclose.h b/include/linux/bdev-preclose.h new file mode 100644 index 0000000..5e75ac3 --- /dev/null +++ b/include/linux/bdev-preclose.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _LINUX_BDEV_PRECLOSE_H +#define _LINUX_BDEV_PRECLOSE_H + +struct file; + +#ifdef CONFIG_BLOCK +void bdev_preclose_reclaim(struct file *file); +#else +static inline void bdev_preclose_reclaim(struct file *file) +{ +} +#endif +#endif