Re: [RFC PATCH 0/1] block: long udev open waits during cache teardown

From: Jan Kara

Date: Thu Nov 26 2026 - 06:36:47 EST


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@xxxxxxxx>
SUSE Labs, CR