Skip to contentSkip to conversation

namespace: clear DCACHE_MOUNTED on live cant_mount dentries

PATCH New Pavee Zheng 1 participant 1 message Dec 5 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 namespace: clear DCACHE_MOUNTED on live cant_mount dentries

Commit 5e7c8942012b ("namespace: don't reveal overmounted dentries that lost their mounts") changed maybe_free_mountpoint() to skip clearing DCACHE_MOUNTED whenever cant_mount(dentry) is set, so that removed names continue to enter __traverse_mounts() and fail.

However, DCACHE_CANT_MOUNT is also set on live dentries that are not unlinked, including ephemeral procfs files under /proc/<pid>/fd, fdinfo, map_files, and net, /proc/self, /proc/thread-self, and HFS resource forks. Since commit aad0a8ec0312 ("namespace: allow bind mounts of ephemeral procfs files again") dropped the cant_mount() check from d_set_mounted(), attach_recursive_mnt() can pin such a dentry with get_mountpoint(). When unpin_mountpoint() calls maybe_free_mountpoint(), the mountpoint is unhashed and freed while DCACHE_MOUNTED remains set.

A subsequent get_mountpoint() on the same still-linked dentry sees d_mountpoint(dentry) with !d_unlinked(dentry), finds no entry in lookup_mountpoint(), receives -EBUSY from d_set_mounted(), and loops indefinitely while holding namespace_sem for writing. This hangs tools/testing/selftests/filesystems/mount_cycle/procfs_source_test in no_tuck_beneath_fdinfo_copy and blocks unrelated readers of /proc/self/mounts on namespace_sem.

Clear DCACHE_MOUNTED in maybe_free_mountpoint() whenever !cant_mount(dentry) || !d_unlinked(dentry), keeping the flag only for removed names that are both cant_mount() and d_unlinked(). Removed names are unhashed by __detach_mounts() before mounts are detached and are never root dentries, so they keep DCACHE_MOUNTED as intended. Checking d_unlinked() matches d_set_mounted() and get_mountpoint() (including for IS_ROOT() dentries), while keeping !cant_mount(dentry) ensures dentries dropped by d_invalidate() still clear DCACHE_MOUNTED so find_submount() terminates.

Fixes 5e7c8942012b ("namespace: don't reveal overmounted dentries that lost their mounts")

---
Found and prepared with the assistance of an AI coding assistant.

Based on vfs.git vfs.all (d8d9179592a5), which integrates both 5e7c8942012b (vfs-7.5.mount) and aad0a8ec0312 (vfs.fixes); also verified to apply cleanly to vfs-7.5.mount (63fab288c315). No Cc: stable tag is included because commit 5e7c8942012b is not yet in a released kernel.

Verification:
- Built out-of-tree for arm64 with gcc 12.2.0 (CONFIG_PROVE_LOCKING=y, CONFIG_DEBUG_ATOMIC_SLEEP=y, CONFIG_DETECT_HUNG_TASK=y, CONFIG_SOFTLOCKUP_DETECTOR=y, CONFIG_FUSE_FS=y): 0 warnings on normal build and make W=1 fs/namespace.o.
- Booted in QEMU (qemu-system-aarch64 -M virt -smp 4, Linux 7.4.0-rc4+ aarch64) and ran:
- tools/testing/selftests/filesystems/mount_cycle/procfs_source_test:
10/10 passed (previously livelocked in no_tuck_beneath_fdinfo_copy holding namespace_sem).
- tools/testing/selftests/filesystems/mount_cycle/detach_mounts_test:

    11/11 passed.
  - tools/testing/selftests/filesystems/fuse/unlink_reval_test:
    4/4 passed.
  - Remaining mount_cycle tests: mount_cover_test (11/11),
    unheld_umount_test (5/5), unmounted_tree_test (5/5),
    overmount_reparent_test (1/1), overmount_ns_file_test (1/1),
    nsfs_rbind_loop_test (1/1), locked_handle_test (3/3), and
    loop_cycle_test (5 passed, 6 skipped due to optional holder/fs
    configs not enabled in the minimal VM config).

- 0 warnings, lockdep splats, hung tasks, or softlockups in dmesg.

Scope limitations:
- Built and runtime-tested on arm64 QEMU only (not x86_64 or other architectures); CONFIG_KASAN was not enabled; 6 optional-config subtests in loop_cycle_test were skipped.

---
 fs/namespace.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/namespace.c +3 -2
index 1b163c7f5189..e9282af4db96 100644
@@ -976,14 +976,15 @@ static int get_mountpoint(struct dentry *dentry, struct pinned_mountpoint *m)
  *
  * A removed name keeps DCACHE_MOUNTED so that a walk which found the
  * name earlier continues walking mounts and fails. The bit dies with the
- * dentry or in d_delete().
+ * dentry or in d_delete(). Live dentries that also carry DCACHE_CANT_MOUNT
+ * (such as ephemeral procfs files) are not unlinked and must clear it.
  */
 static void maybe_free_mountpoint(struct mountpoint *mp, struct list_head *list)
 {
 	if (hlist_empty(&mp->m_list) && hlist_empty(&mp->m_covers)) {
 		struct dentry *dentry = mp->m_dentry;
 		spin_lock(&dentry->d_lock);
-		if (!cant_mount(dentry))
+		if (!cant_mount(dentry) || !d_unlinked(dentry))
 			dentry->d_flags &= ~DCACHE_MOUNTED;
 		spin_unlock(&dentry->d_lock);
 		dput_to_list(dentry, list);

---
base-commit: d8d9179592a52044a52591b31b914cfc0edbf36d
change-id: 20261205-patch-vfs-maybe-free-mountpoint-210f367af6d5

Best regards,

Signature · 1 line
Pavee Zheng <[email protected]>