* [RFC PATCH 0/1] block: long udev open waits during cache teardown
@ 2026-11-17 10:51 Ethan
2026-11-17 10:51 ` [RFC PATCH 1/1] block: reclaim cache before block file close notification Ethan
2026-11-20 12:40 ` [RFC PATCH 0/1] block: long udev open waits during cache teardown Christian Brauner
0 siblings, 2 replies; 5+ messages in thread
From: Ethan @ 2026-11-17 10:51 UTC (permalink / raw)
To: Jens Axboe, Christian Brauner
Cc: Alexander Viro, Jan Kara, linux-block, linux-fsdevel, linux-mm,
linux-kernel, yangyizhen3198
Hi,
This is the first posting of this proposal.
I am requesting feedback on long device-open blocking during final cache
teardown after large buffered writes to a raw block device. In repeated
full mkfs.ntfs runs, formatting succeeds, but a udev process opening the
same device waits for several minutes on disk->open_mutex. The system
reports hung tasks and udev is restarted after watchdog timeouts.
The issue is not simply that formatting or close takes a long time.
The concern is that device lifecycle exclusion is held while large-scale
cache cleanup runs, preventing another device opener from progressing.
The attached patch is an early pre-reclaim prototype for discussing how
to address that exclusion. It is not a current-mainline merge request
or a 6.6.y stable backport request.
Reproduction context and observed impact
----------------------------------------
The recorded system is Phytium ARM64 with about 1 TiB RAM, a 1.92 TB NVMe
device and 512-byte logical sectors. The reproducer is a full, non-quick
mkfs.ntfs run using the original userspace binary and library. Such a
reproducer destroys the target; it was run only on a disposable device.
No userspace cache-drop option or changed write-size option was enabled.
The hung-task reporting threshold remained 120 seconds.
The baseline was 6.6.0.6.6-server-v11-bdevdbg: a vendor 6.6 kernel with
BDEVDBG diagnostics for observing the close path. No preclose cache
reclaim hook was present. This is not an unmodified upstream v6.6 binary.
Three consecutive runs on the same boot produced:
Run Mapping cache at close Final truncate Udev entire open
(4 KiB pages / GiB) (seconds) (seconds)
1 211073040 / 805.180 423.281554 423.281502
2 211110775 / 805.324 447.063839 447.061432
3 211255349 / 805.875 420.785212 420.785106
All three mkfs runs and subsequent NTFS information checks returned 0.
Nevertheless, each run recorded target udev hung-task reports and one
associated udev watchdog restart; the service recovered afterwards.
The three runs contained seven hung-task messages in total. These are
repeated reports of the waiting tasks, not seven independent failures.
No soft lockup, RCU stall, media I/O error or new AER was recorded in
this baseline batch. Other device-event latency was not quantified.
A shortened stack from the first run is:
INFO: task systemd-udevd:95598 blocked for more than 122 seconds.
task:systemd-udevd state:D
__mutex_lock.constprop.0
__mutex_lock_slowpath
mutex_lock
blkdev_get_by_dev.part.0
blkdev_get_by_dev
blkdev_open
do_dentry_open
The same task was subsequently reported blocked for over 245 seconds.
The diagnostic mutex wait was 423281 ms, agreeing with the paired udev
open at millisecond resolution. The entire open includes more than the
mutex wait, so it is not itself an exact mutex measurement. Likewise,
120 seconds is a hung-task reporting threshold, not a timed mutex that
returns an error after 120 seconds.
Where the time is spent
------------------------
The examined vendor block release path is:
mkfs final file release Concurrent udev open
blkdev_release blkdev_open
blkdev_put blkdev_get_by_dev
sync before lock where applicable mutex_lock(open_mutex)
mutex_lock(open_mutex) -> sleeps waiting
blkdev_put_whole
last opener reaches zero
blkdev_flush_mapping
sync_blockdev
kill_bdev
invalidate_bh_lrus
truncate_inode_pages
mutex_unlock(open_mutex) -> can proceed
The last-opener check is significant: the existing final cleanup is
performed when the opener count reaches zero. A new opener is serialized
by the same disk mutex while that cleanup completes. The proposed hook,
in contrast, is per struct file and is not a last-device-opener check.
The diagnostic logs bracket cache teardown and show the target mapping
falling from the page counts above to zero. Paired tracing places nearly
all the final release interval inside truncate_inode_pages. The final
sync was recorded as 0 ms at the diagnostic timer's resolution; this is
not zero cost or proof that every page was always clean. A completed
sync need not evict clean cache pages or remove all associated metadata,
so finishing writes does not eliminate the later cache teardown work.
The cache is a shared block-device mapping, not mkfs process-private RSS.
This supports cache teardown under lifecycle exclusion as the bottleneck
in these runs. It does not establish the separate CPU cost of every BH,
shadow entry or traversal, or prove the same delay on every system.
Proposed direction and scope of this RFC
----------------------------------------
Try vfs_fsync followed by vfs_fadvise(DONTNEED) before fsnotify_close and
the original release, outside open_mutex at the hook. Keep the original
release to clean up remaining pages and concurrent activity. Allow cache
residue; no zero-page invariant or fixed timeout bound is promised.
The objective is to reduce how much cleanup other openers wait behind,
not to eliminate reclaim cost or guarantee a shorter complete close.
The proposed hook applies to block-device files that have a fsync
operation, excluding O_PATH handles. It runs at the final reference to
each such struct file, including read-only and O_DIRECT opens; this is
not a last-device-opener hook. Syncing and evicting a shared mapping at
that point can affect other users. Sync/reclaim may be long and run in
task work, exit or delayed fput context. These are unresolved policy and
placement concerns. Placing the hook before fsnotify_close also defers
notification, which must be distinguished from reduced mutex hold time.
The main questions are whether __fput is an acceptable hook, whether
this should instead remain an explicit userspace action, and whether
a different block close locking design would better address the issue.
If a kernel pre-reclaim policy is appropriate, what eligibility and
accounting could prevent repeated work and interference with other users?
Validation boundaries
----------------------
The implementation is based on the recorded vendor Linux 6.6 source
snapshot. Patch application and comparison with the prepared source
files passed. checkpatch reports 0 errors and one new-file MAINTAINERS
warning. Current mainline has not been used to reproduce or validate
this RFC, and no additional build or runtime test was run while preparing
this posting. The three baseline runs above characterize the problem;
they are not performance or safety results for the proposed patch.
This posting does not claim that all workloads are free of regressions,
or that the change fixes soft lockups or RCU stalls. Buffered-cache and
concurrent-open behavior need dedicated validation.
If the direction is appropriate, the next step is to rebase to the
agreed current tree and validate lifecycle contexts, concurrent buffered
I/O and open/close, error/removal paths, lockdep, CONFIG_BLOCK=n and
complete close and command cost. I would appreciate design feedback
before pursuing a mergeable implementation.
Thanks,
Ethan
Ethan (1):
block: reclaim cache before block file close notification
block/Makefile | 2 ++
block/bdev-preclose.c | 29 +++++++++++++++++++++++++++++
fs/file_table.c | 4 ++++
include/linux/bdev-preclose.h | 14 ++++++++++++++
4 files changed, 49 insertions(+)
^ permalink raw reply [flat|nested] 5+ messages in thread
* [RFC PATCH 1/1] block: reclaim cache before block file close notification
2026-11-17 10:51 [RFC PATCH 0/1] block: long udev open waits during cache teardown Ethan
@ 2026-11-17 10:51 ` Ethan
2026-11-20 12:40 ` [RFC PATCH 0/1] block: long udev open waits during cache teardown Christian Brauner
1 sibling, 0 replies; 5+ messages in thread
From: Ethan @ 2026-11-17 10:51 UTC (permalink / raw)
To: Jens Axboe, Christian Brauner
Cc: Alexander Viro, Jan Kara, linux-block, linux-fsdevel, linux-mm,
linux-kernel, yangyizhen3198
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 <yangyizhen3198@phytium.com.cn>
---
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 <linux/bdev-preclose.h>
+#include <linux/blkdev.h>
+#include <linux/fadvise.h>
+#include <linux/fs.h>
+#include <linux/printk.h>
+#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 <linux/string.h>
#include <linux/slab.h>
#include <linux/file.h>
+#include <linux/bdev-preclose.h>
#include <linux/fdtable.h>
#include <linux/init.h>
#include <linux/module.h>
@@ -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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [RFC PATCH 0/1] block: long udev open waits during cache teardown
2026-11-17 10:51 [RFC PATCH 0/1] block: long udev open waits during cache teardown Ethan
2026-11-17 10:51 ` [RFC PATCH 1/1] block: reclaim cache before block file close notification Ethan
@ 2026-11-20 12:40 ` Christian Brauner
2026-11-26 11:04 ` 杨轶甄
1 sibling, 1 reply; 5+ messages in thread
From: Christian Brauner @ 2026-11-20 12:40 UTC (permalink / raw)
To: Ethan, Jens Axboe
Cc: Alexander Viro, Jan Kara, linux-block, linux-fsdevel, linux-mm,
linux-kernel
On Tue, Nov 17, 2026 at 06:51:06PM +0800, Ethan wrote:
> The main questions are whether __fput is an acceptable hook, whether
> this should instead remain an explicit userspace action, and whether
> a different block close locking design would better address the issue.
No, __fput() is very wrong for this. It runs for the final fput of
every block device file. So every close of any block device fd would
fsync the whole device and throw away its page cache, including the
cache of everyone else who has it open. udev opens and closes block
devices all the time. That makes the common case a lot worse.
Mainline already syncs before taking open_mutex when it looks like the
last opener is going away. Exactly to avoid multi-minute syncs under the
mutex, look at bdev_release().
What's left under open_mutex is the kill_bdev() in
blkdev_flush_mapping() and going by your numbers that's the truncate
you're waiting on. If that still reproduces on mainline it's a question
about the bdev release locking. It absolutely isn't something to hook
into the generic file release path.
And mkfs can just fsync() and POSIX_FADV_DONTNEED the device itself
before it closes it.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Re: [RFC PATCH 0/1] block: long udev open waits during cache teardown
2026-11-20 12:40 ` [RFC PATCH 0/1] block: long udev open waits during cache teardown Christian Brauner
@ 2026-11-26 11:04 ` 杨轶甄
2026-11-26 11:36 ` Jan Kara
0 siblings, 1 reply; 5+ messages in thread
From: 杨轶甄 @ 2026-11-26 11:04 UTC (permalink / raw)
To: Christian Brauner
Cc: Jens Axboe, Alexander Viro, Jan Kara, linux-block, linux-fsdevel,
linux-mm, linux-kernel
Hi Christian,
> If that still reproduces on mainline it's a question
> about the bdev release locking.
I reproduced the long udev open wait on upstream Linux v6.6 and on
Linux v7.0.14. On both kernels, udev blocked while trying to open the
device as mkfs.ntfs was closing it after a full format.
Thanks for pointing out the impact on other openers. I agree that a
udev probe closing its own file could trigger writeback and evict
cache that another opener is still using. I'll drop the __fput()
proposal.
I'd like to understand the locking requirements in the block device
last-close path. In particular, why does kill_bdev() need to complete
while open_mutex is held? What state and ordering requirements does
the lock protect during this cleanup?
I'm asking because the device mapping can still contain a large
amount of clean page cache after the early sync. In my tests on the
vendor 6.6 kernel, about 805 GiB remained at final teardown, and
the truncate took roughly seven minutes while udev was waiting.
Could the release path be changed to reduce the time open_mutex is
held while preserving those requirements?
> And mkfs can just fsync() and POSIX_FADV_DONTNEED the device itself
> before it closes it.
I've submitted a patch to the ntfs-3g project that calls fsync()
and then posix_fadvise(..., POSIX_FADV_DONTNEED) in userspace
before closing the device.
Thanks,
Ethan
信息安全声明:本邮件包含信息归发件人所在组织所有,发件人所在组织对该邮件拥有所有权利。请接收者注意保密,未经发件人书面许可,不得向任何第三方组织和个人透露本邮件所含信息。
Information Security Notice: The information contained in this mail is solely property of the sender's organization.This mail communication is confidential.Recipients named above are obligated to maintain secrecy and are not permitted to disclose the contents of this communication to others.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [RFC PATCH 0/1] block: long udev open waits during cache teardown
2026-11-26 11:04 ` 杨轶甄
@ 2026-11-26 11:36 ` Jan Kara
0 siblings, 0 replies; 5+ messages in thread
From: Jan Kara @ 2026-11-26 11:36 UTC (permalink / raw)
To: 杨轶甄
Cc: Christian Brauner, Jens Axboe, Alexander Viro, Jan Kara,
linux-block, linux-fsdevel, linux-mm, linux-kernel
On Thu 26-11-26 19:04:50, 杨轶甄 wrote:
> Hi Christian,
>
> > If that still reproduces on mainline it's a question
> > about the bdev release locking.
>
> I reproduced the long udev open wait on upstream Linux v6.6 and on
> Linux v7.0.14. On both kernels, udev blocked while trying to open the
> device as mkfs.ntfs was closing it after a full format.
>
> Thanks for pointing out the impact on other openers. I agree that a
> udev probe closing its own file could trigger writeback and evict
> cache that another opener is still using. I'll drop the __fput()
> proposal.
>
> I'd like to understand the locking requirements in the block device
> last-close path. In particular, why does kill_bdev() need to complete
> while open_mutex is held? What state and ordering requirements does
> the lock protect during this cleanup?
The page cache cleanup in kill_bdev() needs to complete before we allow new
openers to open the device and open_mutex serializes that. If the cleanup
would race, we could destroy data the new opener has written to the page
cache of the device.
> I'm asking because the device mapping can still contain a large
> amount of clean page cache after the early sync. In my tests on the
> vendor 6.6 kernel, about 805 GiB remained at final teardown, and
> the truncate took roughly seven minutes while udev was waiting.
> Could the release path be changed to reduce the time open_mutex is
> held while preserving those requirements?
Perhaps. We currently do:
if (atomic_read(&bdev->bd_openers) == 1)
sync_blockdev(bdev);
in bdev_release(). We could enhance it to:
if (atomic_read(&bdev->bd_openers) == 1) {
sync_blockdev(bdev);
/* Still the only opener after the writeback? */
if (atomic_read(&bdev->bd_openers) == 1)
invalidate_mapping_pages(bdev->bd_mapping, 0, -1);
}
This is safe to run without open_mutex and should get rid of 99.9% of pages
before we take open_mutex and do a reliable cleanup. Feel free to submit a
patch with this change describing your motivation.
> > And mkfs can just fsync() and POSIX_FADV_DONTNEED the device itself
> > before it closes it.
>
> I've submitted a patch to the ntfs-3g project that calls fsync()
> and then posix_fadvise(..., POSIX_FADV_DONTNEED) in userspace
> before closing the device.
Good.
Honza
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-26 11:36 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 10:51 [RFC PATCH 0/1] block: long udev open waits during cache teardown Ethan
2026-11-17 10:51 ` [RFC PATCH 1/1] block: reclaim cache before block file close notification Ethan
2026-11-20 12:40 ` [RFC PATCH 0/1] block: long udev open waits during cache teardown Christian Brauner
2026-11-26 11:04 ` 杨轶甄
2026-11-26 11:36 ` Jan Kara
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®