Skip to contentSkip to conversation

pidfs: reduce wait_pidfd lock scope

PATCH New Christian Brauner 1 participant 1 message Nov 4, 2025 VFS

lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

PATCH 1/1 pidfs: reduce wait_pidfd lock scope

There's no need to hold the lock after we realized that pid->attr is set. We're holding a reference to struct pid so it won't go away and pidfs_exit() is called once per struct pid.

---
 fs/pidfs.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)
fs/pidfs.c +13 -11
index 0ef5b47d796a..d2e74c069b4f 100644
@@ -613,17 +613,19 @@ void pidfs_exit(struct task_struct *tsk)
 
 	might_sleep();
 
-	guard(spinlock_irq)(&pid->wait_pidfd.lock);
-	attr = pid->attr;
-	if (!attr) {
-		/*
-		 * No one ever held a pidfd for this struct pid.
-		 * Mark it as dead so no one can add a pidfs
-		 * entry anymore. We're about to be reaped and
-		 * so no exit information would be available.
-		 */
-		pid->attr = PIDFS_PID_DEAD;
-		return;
+	/* Synchronize with pidfs_register_pid(). */
+	scoped_guard(spinlock_irq, &pid->wait_pidfd.lock) {
+		attr = pid->attr;
+		if (!attr) {
+			/*
+			 * No one ever held a pidfd for this struct pid.
+			 * Mark it as dead so no one can add a pidfs
+			 * entry anymore. We're about to be reaped and
+			 * so no exit information would be available.
+			 */
+			pid->attr = PIDFS_PID_DEAD;
+			return;
+		}
 	}
 
 	/*

---
base-commit: a20432b6571ddc02e86c549f582d61ac5a89ca40
change-id: 20251105-work-pidfs-wait_pidfd-lock-a1b0d38cf13d