Skip to contentSkip to conversation

Fix softlockup when adding inotify watch

PATCH Superseded Stephen Brennan 2 participants 8 messages Jan 31, 2022 – Feb 16, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover Fix softlockup when adding inotify watch

Hello,

I'd like to request re-testing this series on LKP, before posting my v2 more broadly. The 0-day bot detected a warning on my v1 posting[1]. I believe v2 should address the warning but I'm still working on reproducing it via the information included in the email. According to the LKP website [2] it's possible to request testing by directly mailing this list, so I wanted to try that in case I can't reproduce the original.

[1]: https://lore.kernel.org/linux-fsdevel/20211218081736.GA1071(a)xsang-OptiPlex-9020/
[2]: https://01.org/lkp/documentation/0-day-test-service

Thanks,
Stephen

(series summary below)

When a system with large amounts of memory has several millions of negative dentries in a single directory, a softlockup can occur while adding an inotify watch:

 watchdog: BUG: soft lockup - CPU#20 stuck for 9s! [inotifywait:9528]
 CPU: 20 PID: 9528 Comm: inotifywait Kdump: loaded Not tainted 5.16.0-rc4.20211208.el8uek.rc1.x86_64 #1
 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.4.1 12/03/2020
 RIP: 0010:__fsnotify_update_child_dentry_flags+0xad/0x120
 Call Trace:
  <TASK>
  fsnotify_add_mark_locked+0x113/0x160
  inotify_new_watch+0x130/0x190
  inotify_update_watch+0x11a/0x140
  __x64_sys_inotify_add_watch+0xef/0x140
  do_syscall_64+0x3b/0x90
  entry_SYSCALL_64_after_hwframe+0x44/0xae

v1 of this series can be found at:
https://lore.kernel.org/linux-fsdevel/20211214005337.161885-1-stephen.s.brennan(a)oracle.com/
v1 itself is based on a prior series:
https://lore.kernel.org/linux-fsdevel/1611235185-1685-1-git-send-email-gautham.ananthakrishna(a)oracle.com/

The strategy employed by this series is to move negative dentries to the end of the d_subdirs list, and mark them with a flag as "tail negative". Then, readers of the d_subdirs list, which are only interested in positive dentries, can stop reading once they reach the first tail negative dentry. By applying this patch, I'm able to avoid the above softlockup caused by 200 million negative dentries on my test system. Inotify watches are set up nearly instantly.

Previously, Al expressed concern for:

1. Possible memory corruption due to use of lock_parent() in sweep_negative(), see patch 01 for fix.
2. The previous patch didn't catch all ways a negative dentry could become positive (d_add, d_instantiate_new), see patch 01.
3. The previous series contained a new negative dentry limit, which capped the negative dentry count at around 3 per hash bucket. I've dropped this patch from the series.

Patches 2-4 are unmodified from the previous posting.

Konstantin Khlebnikov (3):
fsnotify: stop walking child dentries if remaining tail is negative
dcache: add action D_WALK_SKIP_SIBLINGS to d_walk()
dcache: stop walking siblings if remaining dentries all negative

Stephen Brennan (1):
dcache: sweep cached negative dentries to the end of list of siblings

 fs/dcache.c            | 96 ++++++++++++++++++++++++++++++++++++++++--
 fs/libfs.c             |  3 ++
 fs/notify/fsnotify.c   |  6 ++-
 include/linux/dcache.h |  6 +++
 4 files changed, 106 insertions(+), 5 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 1/4 dcache: sweep cached negative dentries to the end of list of siblings

For disk filesystems result of every negative lookup is cached, content of directories is usually cached too. Production of negative dentries isn't limited with disk speed. It's really easy to generate millions of them if system has enough memory. Negative dentries are linked into siblings list along with normal positive dentries. Some operations walks dcache tree but looks only for positive dentries: most important is fsnotify/inotify.

This patch sweeps negative dentries to the end of list at final dput() and marks with flag which tells that all following dentries are negative too. We do this carefully to avoid corruption in case the dentry is killed when we try to lock its parent. Reverse operation (recycle) is required before instantiating tail negative dentry, or calling d_add() with non-NULL inode.

(Fix issue in original patch reported by kernel test robot).

---

Notes:
v2: Remove the sweep_negative() call in __d_add() when inode is NULL. Should address warning detected by 0-day bot.

 fs/dcache.c            | 80 +++++++++++++++++++++++++++++++++++++++---
 include/linux/dcache.h |  6 ++++
 2 files changed, 82 insertions(+), 4 deletions(-)
fs/dcache.c +76 -4
index c84269c6e8bf..26b28ac3de70 100644
@@ -660,6 +660,58 @@ static inline struct dentry *lock_parent(struct dentry *dentry)
 	return __lock_parent(dentry);
 }
 
+/*
+ * Move cached negative dentry to the tail of parent->d_subdirs.
+ * This lets walkers skip them all together at first sight.
+ * Must be called at dput of negative dentry with d_lock held.
+ * Releases d_lock.
+ */
+static void sweep_negative(struct dentry *dentry)
+{
+	struct dentry *parent;
+
+	rcu_read_lock();
+	parent = lock_parent(dentry);
+	if (!parent) {
+		rcu_read_unlock();
+		return;
+	}
+
+	/*
+	 * If we did not hold a reference to dentry (as in the case of dput),
+	 * and dentry->d_lock was dropped in lock_parent(), then we could now be
+	 * holding onto a dead dentry. Be careful to check d_count and unlock
+	 * before dropping RCU lock, otherwise we could corrupt freed memory.
+	 */
+	if (!d_count(dentry) && d_is_negative(dentry) &&
+		!d_is_tail_negative(dentry)) {
+		dentry->d_flags |= DCACHE_TAIL_NEGATIVE;
+		list_move_tail(&dentry->d_child, &parent->d_subdirs);
+	}
+
+	spin_unlock(&parent->d_lock);
+	spin_unlock(&dentry->d_lock);
+	rcu_read_unlock();
+}
+
+/*
+ * Undo sweep_negative() and move to the head of parent->d_subdirs.
+ * Must be called before converting negative dentry into positive.
+ * Must hold dentry->d_lock, we may drop and re-grab it via lock_parent().
+ * Must be hold a reference or be otherwise sure the dentry cannot be killed.
+ */
+static void recycle_negative(struct dentry *dentry)
+{
+	struct dentry *parent;
+
+	parent = lock_parent(dentry);
+	dentry->d_flags &= ~DCACHE_TAIL_NEGATIVE;
+	if (parent) {
+		list_move(&dentry->d_child, &parent->d_subdirs);
+		spin_unlock(&parent->d_lock);
+	}
+}
+
 static inline bool retain_dentry(struct dentry *dentry)
 {
 	WARN_ON(d_in_lookup(dentry));
@@ -765,7 +817,7 @@ static struct dentry *dentry_kill(struct dentry *dentry)
 static inline bool fast_dput(struct dentry *dentry)
 {
 	int ret;
-	unsigned int d_flags;
+	unsigned int d_flags, required;
 
 	/*
 	 * If we have a d_op->d_delete() operation, we sould not
@@ -813,6 +865,8 @@ static inline bool fast_dput(struct dentry *dentry)
 	 * a 'delete' op, and it's referenced and already on
 	 * the LRU list.
 	 *
+	 * Cached negative dentry must be swept to the tail.
+	 *
 	 * NOTE! Since we aren't locked, these values are
 	 * not "stable". However, it is sufficient that at
 	 * some point after we dropped the reference the
@@ -830,11 +884,16 @@ static inline bool fast_dput(struct dentry *dentry)
 	 */
 	smp_rmb();
 	d_flags = READ_ONCE(dentry->d_flags);
+
+	required = DCACHE_REFERENCED | DCACHE_LRU_LIST |
+		(d_flags_negative(d_flags) ? DCACHE_TAIL_NEGATIVE : 0);
+
 	d_flags &= DCACHE_REFERENCED | DCACHE_LRU_LIST |
-			DCACHE_DISCONNECTED | DCACHE_DONTCACHE;
+			DCACHE_DISCONNECTED | DCACHE_DONTCACHE |
+			DCACHE_TAIL_NEGATIVE;
 
 	/* Nothing to do? Dropping the reference was all we needed? */
-	if (d_flags == (DCACHE_REFERENCED | DCACHE_LRU_LIST) && !d_unhashed(dentry))
+	if (d_flags == required && !d_unhashed(dentry))
 		return true;
 
 	/*
@@ -906,7 +965,10 @@ void dput(struct dentry *dentry)
 		rcu_read_unlock();
 
 		if (likely(retain_dentry(dentry))) {
-			spin_unlock(&dentry->d_lock);
+			if (d_is_negative(dentry) && !d_is_tail_negative(dentry))
+				sweep_negative(dentry); /* drops d_lock */
+			else
+				spin_unlock(&dentry->d_lock);
 			return;
 		}
 
@@ -1998,6 +2060,8 @@ static void __d_instantiate(struct dentry *dentry, struct inode *inode)
 	WARN_ON(d_in_lookup(dentry));
 
 	spin_lock(&dentry->d_lock);
+	if (d_is_tail_negative(dentry))
+		recycle_negative(dentry);
 	/*
 	 * Decrement negative dentry count if it was in the LRU list.
 	 */
@@ -2722,6 +2786,13 @@ static inline void __d_add(struct dentry *dentry, struct inode *inode)
 	struct inode *dir = NULL;
 	unsigned n;
 	spin_lock(&dentry->d_lock);
+	/*
+	 * Tail negative dentries must become positive before associating an
+	 * inode. recycle_negative() is safe to use because we hold a reference
+	 * to dentry.
+	 */
+	if (inode && d_is_tail_negative(dentry))
+		recycle_negative(dentry);
 	if (unlikely(d_in_lookup(dentry))) {
 		dir = dentry->d_parent->d_inode;
 		n = start_dir_add(dir);
@@ -2738,6 +2809,7 @@ static inline void __d_add(struct dentry *dentry, struct inode *inode)
 	__d_rehash(dentry);
 	if (dir)
 		end_dir_add(dir, n);
+
 	spin_unlock(&dentry->d_lock);
 	if (inode)
 		spin_unlock(&inode->i_lock);
include/linux/dcache.h +6 -0
index f5bba51480b2..7cc1bd384912 100644
@@ -211,6 +211,7 @@ struct dentry_operations {
 #define DCACHE_PAR_LOOKUP		0x10000000 /* being looked up (with parent locked shared) */
 #define DCACHE_DENTRY_CURSOR		0x20000000
 #define DCACHE_NORCU			0x40000000 /* No RCU delay for freeing */
+#define DCACHE_TAIL_NEGATIVE		0x80000000 /* All following siblings are negative */
 
 extern seqlock_t rename_lock;
 
@@ -489,6 +490,11 @@ static inline int simple_positive(const struct dentry *dentry)
 	return d_really_is_positive(dentry) && !d_unhashed(dentry);
 }
 
+static inline bool d_is_tail_negative(const struct dentry *dentry)
+{
+	return unlikely(dentry->d_flags & DCACHE_TAIL_NEGATIVE);
+}
+
 extern void d_set_fallthru(struct dentry *dentry);
 
 static inline bool d_is_fallthru(const struct dentry *dentry)
Signature · 1 line
2.30.2
#2 lore raw

PATCH v2 2/4 fsnotify: stop walking child dentries if remaining tail is negative

In reply to Stephen Brennan

From: Konstantin Khlebnikov <[email protected]>

When notification starts/stops listening events from inode's children it has to update dentry->d_flags of all positive child dentries. Scanning may take a long time if the directory has a lot of negative child dentries. Use the new tail negative flag to detect when the remainder of the children are negative, and skip them. This speeds up fsnotify/inotify watch creation, and in some extreme cases can avoid a soft lockup, for example, with 200 million negative dentries in a single directory:

 watchdog: BUG: soft lockup - CPU#20 stuck for 9s! [inotifywait:9528]
 CPU: 20 PID: 9528 Comm: inotifywait Kdump: loaded Not tainted 5.16.0-rc4.20211208.el8uek.rc1.x86_64 #1
 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.4.1 12/03/2020
 RIP: 0010:__fsnotify_update_child_dentry_flags+0xad/0x120
 Call Trace:
  <TASK>
  fsnotify_add_mark_locked+0x113/0x160
  inotify_new_watch+0x130/0x190
  inotify_update_watch+0x11a/0x140
  __x64_sys_inotify_add_watch+0xef/0x140
  do_syscall_64+0x3b/0x90
  entry_SYSCALL_64_after_hwframe+0x44/0xae
---
 fs/notify/fsnotify.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/notify/fsnotify.c +5 -1
index ab81a0776ece..1f314f85f4c1 100644
@@ -127,8 +127,12 @@ void __fsnotify_update_child_dentry_flags(struct inode *inode)
 		 * original inode) */
 		spin_lock(&alias->d_lock);
 		list_for_each_entry(child, &alias->d_subdirs, d_child) {
-			if (!child->d_inode)
+			if (!child->d_inode) {
+				/* all remaining children are negative */
+				if (d_is_tail_negative(child))
+					break;
 				continue;
+			}
 
 			spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
 			if (watched)
Signature · 1 line
2.30.2
#3 lore raw

PATCH v2 3/4 dcache: add action D_WALK_SKIP_SIBLINGS to d_walk()

In reply to Stephen Brennan

From: Konstantin Khlebnikov <[email protected]>

This lets skip remaining siblings at seeing d_is_tail_negative().

---
 fs/dcache.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/dcache.c +7 -0
index 26b28ac3de70..a5a6c7654006 100644
@@ -1391,12 +1391,14 @@ EXPORT_SYMBOL(shrink_dcache_sb);
  * @D_WALK_QUIT:	quit walk
  * @D_WALK_NORETRY:	quit when retry is needed
  * @D_WALK_SKIP:	skip this dentry and its children
+ * @D_WALK_SKIP_SIBLINGS: skip siblings and their children
  */
 enum d_walk_ret {
 	D_WALK_CONTINUE,
 	D_WALK_QUIT,
 	D_WALK_NORETRY,
 	D_WALK_SKIP,
+	D_WALK_SKIP_SIBLINGS,
 };
 
 /**
@@ -1427,6 +1429,7 @@ static void d_walk(struct dentry *parent, void *data,
 		break;
 	case D_WALK_QUIT:
 	case D_WALK_SKIP:
+	case D_WALK_SKIP_SIBLINGS:
 		goto out_unlock;
 	case D_WALK_NORETRY:
 		retry = false;
@@ -1458,6 +1461,9 @@ static void d_walk(struct dentry *parent, void *data,
 		case D_WALK_SKIP:
 			spin_unlock(&dentry->d_lock);
 			continue;
+		case D_WALK_SKIP_SIBLINGS:
+			spin_unlock(&dentry->d_lock);
+			goto skip_siblings;
 		}
 
 		if (!list_empty(&dentry->d_subdirs)) {
@@ -1469,6 +1475,7 @@ static void d_walk(struct dentry *parent, void *data,
 		}
 		spin_unlock(&dentry->d_lock);
 	}
+skip_siblings:
 	/*
 	 * All done at this level ... ascend and resume the search.
 	 */
Signature · 1 line
2.30.2
#4 lore raw

PATCH v2 4/4 dcache: stop walking siblings if remaining dentries all negative

In reply to Stephen Brennan

From: Konstantin Khlebnikov <[email protected]>

Most walkers are interested only in positive dentries.

Changes in simple_* libfs helpers are mostly cosmetic: it shouldn't cache negative dentries unless uses d_delete other than always_delete_dentry().

---
 fs/dcache.c | 9 +++++++++
 fs/libfs.c  | 3 +++
 2 files changed, 12 insertions(+)
fs/dcache.c +9 -0
index a5a6c7654006..fadbecf93f78 100644
@@ -1530,6 +1530,8 @@ static enum d_walk_ret path_check_mount(void *data, struct dentry *dentry)
 	struct check_mount *info = data;
 	struct path path = { .mnt = info->mnt, .dentry = dentry };
 
+	if (d_is_tail_negative(dentry))
+		return D_WALK_SKIP_SIBLINGS;
 	if (likely(!d_mountpoint(dentry)))
 		return D_WALK_CONTINUE;
 	if (__path_is_mountpoint(&path)) {
@@ -1776,6 +1778,10 @@ void shrink_dcache_for_umount(struct super_block *sb)
 static enum d_walk_ret find_submount(void *_data, struct dentry *dentry)
 {
 	struct dentry **victim = _data;
+
+	if (d_is_tail_negative(dentry))
+		return D_WALK_SKIP_SIBLINGS;
+
 	if (d_mountpoint(dentry)) {
 		__dget_dlock(dentry);
 		*victim = dentry;
@@ -3253,6 +3259,9 @@ static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry)
 {
 	struct dentry *root = data;
 	if (dentry != root) {
+		if (d_is_tail_negative(dentry))
+			return D_WALK_SKIP_SIBLINGS;
+
 		if (d_unhashed(dentry) || !dentry->d_inode)
 			return D_WALK_SKIP;
 
fs/libfs.c +3 -0
index ba7438ab9371..13cb44cf158e 100644
@@ -411,6 +411,9 @@ int simple_empty(struct dentry *dentry)
 
 	spin_lock(&dentry->d_lock);
 	list_for_each_entry(child, &dentry->d_subdirs, d_child) {
+		if (d_is_tail_negative(child))
+			break;
+
 		spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
 		if (simple_positive(child)) {
 			spin_unlock(&child->d_lock);
Signature · 1 line
2.30.2
#5 lore raw
In reply to Stephen Brennan

hi Stephen,

Quoted 9 lines · Stephen Brennan

Hello,

I'd like to request re-testing this series on LKP, before posting my v2 more broadly. The 0-day bot detected a warning on my v1 posting[1]. I believe v2 should address the warning but I'm still working on reproducing it via the information included in the email. According to the LKP website [2] it's possible to request testing by directly mailing this list, so I wanted to try that in case I can't reproduce the original.

the issue still exist on this v2. (detail dmesg attached)

[   50.129453][ T4056] ------------[ cut here ]------------
[   50.135243][ T4056] WARNING: CPU: 68 PID: 4056 at fs/nfsd/nfsctl.c:1263 nfsdfs_remove_files+0xd7/0x100 [nfsd]
[   50.145632][ T4056] Modules linked in: rpcsec_gss_krb5 acpi_cpufreq(-) nfsv4 dns_resolver nfsd auth_rpcgss xfs dm_mod brd binfmt_misc dax_pmem_compat(+) nd_pmem de

vice_dax nd_btt dax_pmem_core intel_rapl_msr intel_rapl_common btrfs blake2b_generic xor ipmi_ssif raid6_pq x86_pkg_temp_thermal intel_powerclamp zstd_compress corete mp libcrc32c sd_mod sg kvm_intel ast drm_vram_helper drm_ttm_helper kvm irqbypass crct10dif_pclmul crc32_pclmul ttm crc32c_intel ghash_clmulni_intel drm_kms_helper ra pl syscopyarea intel_cstate sysfillrect sysimgblt ahci nvme fb_sys_fops libahci intel_uncore nvme_core t10_pi ioatdma drm joydev libata dca wmi acpi_ipmi ipmi_si ipmi _devintf ipmi_msghandler nfit libnvdimm acpi_pad acpi_power_meter ip_tables

[   50.212667][ T4056] CPU: 68 PID: 4056 Comm: nfsd Not tainted 5.16.0-rc2-00001-gc04e4a06741f #1
[   50.221781][ T4056] RIP: 0010:nfsdfs_remove_files+0xd7/0x100 [nfsd]
[   50.228562][ T4056] Code: 83 90 00 00 00 48 8d 93 90 00 00 00 48 89 dd 48 2d 90 00 00 00 49 39 d4 74 1b 48 89 c3 48 8b 45 30 48 85 c0 0f 85 6e ff ff ff <0f> 0b eb
d0 ff d2 0f 1f 00 eb 98 5b 5d 41 5c 41 5d 41 5e 41 5f c3
[   50.249028][ T4056] RSP: 0018:ffa000002213fce8 EFLAGS: 00010246
[   50.255476][ T4056] RAX: 0000000000000000 RBX: ff1100010c2c4e40 RCX: ff1100010c2c4ec0
[   50.263828][ T4056] RDX: ff11000150659290 RSI: ff1100010c2c4ed0 RDI: ff1100010c2c5f18
[   50.272186][ T4056] RBP: ff11000150659200 R08: ff1100010c2c52a0 R09: ff1100010c2c52a0
[   50.280539][ T4056] R10: 000000000000013c R11: ff1100200012a404 R12: ff1100010c2c52a0
[   50.288926][ T4056] R13: ff1100010c2c5200 R14: 00000000ffffffff R15: 0000000000000000
[   50.297271][ T4056] FS:  0000000000000000(0000) GS:ff11002000100000(0000) knlGS:0000000000000000
[   50.306577][ T4056] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   50.313549][ T4056] CR2: 00007f518e6bb114 CR3: 000000407ec0a005 CR4: 0000000000771ee0
[   50.321898][ T4056] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[   50.330243][ T4056] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[   50.338596][ T4056] PKRU: 55555554
[   50.342543][ T4056] Call Trace:
[   50.346197][ T4056]  <TASK>
[   50.349501][ T4056]  nfsd_client_rmdir+0x32/0x100 [nfsd]
[   50.355378][ T4056]  free_client+0x9d/0x140 [nfsd]
[   50.360689][ T4056]  __destroy_client+0x1e8/0x240 [nfsd]
[   50.366521][ T4056]  nfsd4_setclientid_confirm+0x21d/0x480 [nfsd]
[   50.373108][ T4056]  nfsd4_proc_compound+0x3ae/0x680 [nfsd]
[   50.379171][ T4056]  nfsd_dispatch+0x146/0x280 [nfsd]
[   50.384729][ T4056]  svc_process_common+0x45a/0x5c0
[   50.390086][ T4056]  ? nfsd_svc+0x340/0x340 [nfsd]
[   50.395373][ T4056]  ? nfsd_shutdown_threads+0xc0/0xc0 [nfsd]
[   50.401611][ T4056]  svc_process+0xb7/0x100
[   50.406298][ T4056]  nfsd+0xf1/0x180 [nfsd]
[   50.410960][ T4056]  kthread+0x157/0x180
[   50.415360][ T4056]  ? set_kthread_struct+0x40/0x40
[   50.420716][ T4056]  ret_from_fork+0x1f/0x30
[   50.425460][ T4056]  </TASK>
[   50.428798][ T4056] ---[ end trace 2780c565d409a286 ]---

BTW,
the previous report upon 5384bd38a8 was based on v5.16-rc2:
* 5384bd38a814f1 dcache: sweep cached negative dentries to the end of list of siblings
* 136057256686de (tag: v5.16-rc2) Linux 5.16-rc2

so I still applied your v2 patch upon v5.16-rc2

* 4e96f210550b dcache: stop walking siblings if remaining dentries all negative
* b38cd5a2cebd dcache: add action D_WALK_SKIP_SIBLINGS to d_walk()
* 5aaf33a521ca fsnotify: stop walking child dentries if remaining tail is negative
* c04e4a06741f dcache: sweep cached negative dentries to the end of list of siblings
* 136057256686 (tag: v5.16-rc2) Linux 5.16-rc2

if you think there should be a different code base, please let us know. Thanks

Quoted 70 lines

[1]: https://lore.kernel.org/linux-fsdevel/20211218081736.GA1071(a)xsang-OptiPlex-9020/
[2]: https://01.org/lkp/documentation/0-day-test-service

Thanks,
Stephen

(series summary below)

When a system with large amounts of memory has several millions of negative dentries in a single directory, a softlockup can occur while adding an inotify watch:

 watchdog: BUG: soft lockup - CPU#20 stuck for 9s! [inotifywait:9528]
 CPU: 20 PID: 9528 Comm: inotifywait Kdump: loaded Not tainted 5.16.0-rc4.20211208.el8uek.rc1.x86_64 #1
 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.4.1 12/03/2020
 RIP: 0010:__fsnotify_update_child_dentry_flags+0xad/0x120
 Call Trace:
  <TASK>
  fsnotify_add_mark_locked+0x113/0x160
  inotify_new_watch+0x130/0x190
  inotify_update_watch+0x11a/0x140
  __x64_sys_inotify_add_watch+0xef/0x140
  do_syscall_64+0x3b/0x90
  entry_SYSCALL_64_after_hwframe+0x44/0xae

v1 of this series can be found at:
https://lore.kernel.org/linux-fsdevel/20211214005337.161885-1-stephen.s.brennan(a)oracle.com/
v1 itself is based on a prior series:
https://lore.kernel.org/linux-fsdevel/1611235185-1685-1-git-send-email-gautham.ananthakrishna(a)oracle.com/

The strategy employed by this series is to move negative dentries to the end of the d_subdirs list, and mark them with a flag as "tail negative". Then, readers of the d_subdirs list, which are only interested in positive dentries, can stop reading once they reach the first tail negative dentry. By applying this patch, I'm able to avoid the above softlockup caused by 200 million negative dentries on my test system. Inotify watches are set up nearly instantly.

Previously, Al expressed concern for:

1. Possible memory corruption due to use of lock_parent() in sweep_negative(), see patch 01 for fix.
2. The previous patch didn't catch all ways a negative dentry could become positive (d_add, d_instantiate_new), see patch 01.
3. The previous series contained a new negative dentry limit, which capped the negative dentry count at around 3 per hash bucket. I've dropped this patch from the series.

Patches 2-4 are unmodified from the previous posting.

Konstantin Khlebnikov (3):
fsnotify: stop walking child dentries if remaining tail is negative
dcache: add action D_WALK_SKIP_SIBLINGS to d_walk()
dcache: stop walking siblings if remaining dentries all negative

Stephen Brennan (1):
dcache: sweep cached negative dentries to the end of list of siblings

 fs/dcache.c            | 96 ++++++++++++++++++++++++++++++++++++++++--
 fs/libfs.c             |  3 ++
 fs/notify/fsnotify.c   |  6 ++-
 include/linux/dcache.h |  6 +++
 4 files changed, 106 insertions(+), 5 deletions(-)
Signature · 4 lines
2.30.2
_______________________________________________
LKP mailing list -- lkp(a)lists.01.org
To unsubscribe send an email to lkp-leave(a)lists.01.org
#6 lore raw

Hi Oliver,

Quoted 65 lines · Oliver Sang

hi Stephen,

Quoted 9 lines · Stephen Brennan

Hello,

I'd like to request re-testing this series on LKP, before posting my v2 more broadly. The 0-day bot detected a warning on my v1 posting[1]. I believe v2 should address the warning but I'm still working on reproducing it via the information included in the email. According to the LKP website [2] it's possible to request testing by directly mailing this list, so I wanted to try that in case I can't reproduce the original.

the issue still exist on this v2. (detail dmesg attached)

[   50.129453][ T4056] ------------[ cut here ]------------
[   50.135243][ T4056] WARNING: CPU: 68 PID: 4056 at fs/nfsd/nfsctl.c:1263 nfsdfs_remove_files+0xd7/0x100 [nfsd]
[   50.145632][ T4056] Modules linked in: rpcsec_gss_krb5 acpi_cpufreq(-) nfsv4 dns_resolver nfsd auth_rpcgss xfs dm_mod brd binfmt_misc dax_pmem_compat(+) nd_pmem de

vice_dax nd_btt dax_pmem_core intel_rapl_msr intel_rapl_common btrfs blake2b_generic xor ipmi_ssif raid6_pq x86_pkg_temp_thermal intel_powerclamp zstd_compress corete mp libcrc32c sd_mod sg kvm_intel ast drm_vram_helper drm_ttm_helper kvm irqbypass crct10dif_pclmul crc32_pclmul ttm crc32c_intel ghash_clmulni_intel drm_kms_helper ra pl syscopyarea intel_cstate sysfillrect sysimgblt ahci nvme fb_sys_fops libahci intel_uncore nvme_core t10_pi ioatdma drm joydev libata dca wmi acpi_ipmi ipmi_si ipmi _devintf ipmi_msghandler nfit libnvdimm acpi_pad acpi_power_meter ip_tables

[   50.212667][ T4056] CPU: 68 PID: 4056 Comm: nfsd Not tainted 5.16.0-rc2-00001-gc04e4a06741f #1
[   50.221781][ T4056] RIP: 0010:nfsdfs_remove_files+0xd7/0x100 [nfsd]
[   50.228562][ T4056] Code: 83 90 00 00 00 48 8d 93 90 00 00 00 48 89 dd 48 2d 90 00 00 00 49 39 d4 74 1b 48 89 c3 48 8b 45 30 48 85 c0 0f 85 6e ff ff ff <0f> 0b eb
d0 ff d2 0f 1f 00 eb 98 5b 5d 41 5c 41 5d 41 5e 41 5f c3
[   50.249028][ T4056] RSP: 0018:ffa000002213fce8 EFLAGS: 00010246
[   50.255476][ T4056] RAX: 0000000000000000 RBX: ff1100010c2c4e40 RCX: ff1100010c2c4ec0
[   50.263828][ T4056] RDX: ff11000150659290 RSI: ff1100010c2c4ed0 RDI: ff1100010c2c5f18
[   50.272186][ T4056] RBP: ff11000150659200 R08: ff1100010c2c52a0 R09: ff1100010c2c52a0
[   50.280539][ T4056] R10: 000000000000013c R11: ff1100200012a404 R12: ff1100010c2c52a0
[   50.288926][ T4056] R13: ff1100010c2c5200 R14: 00000000ffffffff R15: 0000000000000000
[   50.297271][ T4056] FS:  0000000000000000(0000) GS:ff11002000100000(0000) knlGS:0000000000000000
[   50.306577][ T4056] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   50.313549][ T4056] CR2: 00007f518e6bb114 CR3: 000000407ec0a005 CR4: 0000000000771ee0
[   50.321898][ T4056] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[   50.330243][ T4056] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[   50.338596][ T4056] PKRU: 55555554
[   50.342543][ T4056] Call Trace:
[   50.346197][ T4056]  <TASK>
[   50.349501][ T4056]  nfsd_client_rmdir+0x32/0x100 [nfsd]
[   50.355378][ T4056]  free_client+0x9d/0x140 [nfsd]
[   50.360689][ T4056]  __destroy_client+0x1e8/0x240 [nfsd]
[   50.366521][ T4056]  nfsd4_setclientid_confirm+0x21d/0x480 [nfsd]
[   50.373108][ T4056]  nfsd4_proc_compound+0x3ae/0x680 [nfsd]
[   50.379171][ T4056]  nfsd_dispatch+0x146/0x280 [nfsd]
[   50.384729][ T4056]  svc_process_common+0x45a/0x5c0
[   50.390086][ T4056]  ? nfsd_svc+0x340/0x340 [nfsd]
[   50.395373][ T4056]  ? nfsd_shutdown_threads+0xc0/0xc0 [nfsd]
[   50.401611][ T4056]  svc_process+0xb7/0x100
[   50.406298][ T4056]  nfsd+0xf1/0x180 [nfsd]
[   50.410960][ T4056]  kthread+0x157/0x180
[   50.415360][ T4056]  ? set_kthread_struct+0x40/0x40
[   50.420716][ T4056]  ret_from_fork+0x1f/0x30
[   50.425460][ T4056]  </TASK>
[   50.428798][ T4056] ---[ end trace 2780c565d409a286 ]---

BTW,
the previous report upon 5384bd38a8 was based on v5.16-rc2:
* 5384bd38a814f1 dcache: sweep cached negative dentries to the end of list of siblings
* 136057256686de (tag: v5.16-rc2) Linux 5.16-rc2

so I still applied your v2 patch upon v5.16-rc2

Quoted 8 lines

* 4e96f210550b dcache: stop walking siblings if remaining dentries all negative
* b38cd5a2cebd dcache: add action D_WALK_SKIP_SIBLINGS to d_walk()
* 5aaf33a521ca fsnotify: stop walking child dentries if remaining tail is negative
* c04e4a06741f dcache: sweep cached negative dentries to the end of list of siblings
* 136057256686 (tag: v5.16-rc2) Linux 5.16-rc2

if you think there should be a different code base, please let us know. Thanks

Thank you for re-running these! I had a tough time getting the tests running on Oracle Linux or CentOS. More recently after sending this to you, I tried Debian, which seems to be the main distribution for the tests. It was quite smooth to get the tests working on Debian. I reproduced the warning on my v2 patch series based on 5.16-rc2, but not 5.17 rc2+.

I was able to determine that the fix was 29044dae2e74 ("fsnotify: fix fsnotify hooks in pseudo filesystems") which is included in 5.17, which is why the warning no longer reproduces. From everything I've learned, NFS used a VFS method for deleting an in-memory file/dentry which is not ideal, and my patch merely surfaced a warning which may have been possible regardless. After the fix I cannot reproduce the warning.

I appreciate your re-running the tests!
Thank you,
Stephen

Quoted 71 lines
Quoted 71 lines

[1]: https://lore.kernel.org/linux-fsdevel/20211218081736.GA1071(a)xsang-OptiPlex-9020/
[2]: https://01.org/lkp/documentation/0-day-test-service

Thanks,
Stephen

(series summary below)

When a system with large amounts of memory has several millions of negative dentries in a single directory, a softlockup can occur while adding an inotify watch:

  watchdog: BUG: soft lockup - CPU#20 stuck for 9s! [inotifywait:9528]
  CPU: 20 PID: 9528 Comm: inotifywait Kdump: loaded Not tainted 5.16.0-rc4.20211208.el8uek.rc1.x86_64 #1
  Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.4.1 12/03/2020
  RIP: 0010:__fsnotify_update_child_dentry_flags+0xad/0x120
  Call Trace:
   <TASK>
   fsnotify_add_mark_locked+0x113/0x160
   inotify_new_watch+0x130/0x190
   inotify_update_watch+0x11a/0x140
   __x64_sys_inotify_add_watch+0xef/0x140
   do_syscall_64+0x3b/0x90
   entry_SYSCALL_64_after_hwframe+0x44/0xae

v1 of this series can be found at:
https://lore.kernel.org/linux-fsdevel/20211214005337.161885-1-stephen.s.brennan(a)oracle.com/
v1 itself is based on a prior series:
https://lore.kernel.org/linux-fsdevel/1611235185-1685-1-git-send-email-gautham.ananthakrishna(a)oracle.com/

The strategy employed by this series is to move negative dentries to the end of the d_subdirs list, and mark them with a flag as "tail negative". Then, readers of the d_subdirs list, which are only interested in positive dentries, can stop reading once they reach the first tail negative dentry. By applying this patch, I'm able to avoid the above softlockup caused by 200 million negative dentries on my test system. Inotify watches are set up nearly instantly.

Previously, Al expressed concern for:

1. Possible memory corruption due to use of lock_parent() in sweep_negative(), see patch 01 for fix.
2. The previous patch didn't catch all ways a negative dentry could become positive (d_add, d_instantiate_new), see patch 01.
3. The previous series contained a new negative dentry limit, which capped the negative dentry count at around 3 per hash bucket. I've dropped this patch from the series.

Patches 2-4 are unmodified from the previous posting.

Konstantin Khlebnikov (3):
fsnotify: stop walking child dentries if remaining tail is negative
dcache: add action D_WALK_SKIP_SIBLINGS to d_walk()
dcache: stop walking siblings if remaining dentries all negative

Stephen Brennan (1):
dcache: sweep cached negative dentries to the end of list of siblings

  fs/dcache.c            | 96 ++++++++++++++++++++++++++++++++++++++++--
  fs/libfs.c             |  3 ++
  fs/notify/fsnotify.c   |  6 ++-
  include/linux/dcache.h |  6 +++
  4 files changed, 106 insertions(+), 5 deletions(-)
Signature · 4 lines
2.30.2
_______________________________________________
LKP mailing list -- lkp(a)lists.01.org
To unsubscribe send an email to lkp-leave(a)lists.01.org
#7 lore raw

Hi Stephen,

Quoted 8 lines · Stephen Brennan

Hi Oliver,

Thank you for re-running these! I had a tough time getting the tests running on Oracle Linux or CentOS. More recently after sending this to you, I tried Debian, which seems to be the main distribution for the tests. It was quite smooth to get the tests working on Debian. I reproduced the warning on my v2 patch series based on 5.16-rc2, but not 5.17 rc2+.

Quoted 8 lines

I was able to determine that the fix was 29044dae2e74 ("fsnotify: fix fsnotify hooks in pseudo filesystems") which is included in 5.17, which is why the warning no longer reproduces. From everything I've learned, NFS used a VFS method for deleting an in-memory file/dentry which is not ideal, and my patch merely surfaced a warning which may have been possible regardless. After the fix I cannot reproduce the warning.

I appreciate your re-running the tests!

you are very welcome :) Thanks a lot for all these details!

Quoted 3 lines

Thank you,
Stephen