git: 6d9339c7b1b6 - stable/14 - inotify: Work around the vput() bug directly

Mark Johnston <[email protected]>
Newsgroups gmane.os.freebsd.devel.cvs.src
Message-ID <6a7353e0.46d94.699a2707__17323.1767884985$1785943273$gmane$org@gitrepo.freebsd.org>
The branch stable/14 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=6d9339c7b1b60b97ea1e9391af31b7f4e8de0d00

commit 6d9339c7b1b60b97ea1e9391af31b7f4e8de0d00
Author:     Mark Johnston <[email protected]>
AuthorDate: 2025-11-15 18:00:44 +0000
Commit:     Mark Johnston <[email protected]>
CommitDate: 2026-08-05 15:15:16 +0000

    inotify: Work around the vput() bug directly
    
    For 15.0, apply a minimal fix which at least ensures that inotify can't
    trigger the latent race described in commit 99cb3dca4773 ("vnode: Rework
    vput() to avoid holding the vnode lock after decrementing").
    
    Reviewed by:    olce, kib
    MFC after:      3 days
    Differential Revision:  https://reviews.freebsd.org/D53774
    
    (cherry picked from commit ebc17879f0885ca87644980f6275b9759b311eb3)
---
 sys/kern/vfs_inotify.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/sys/kern/vfs_inotify.c b/sys/kern/vfs_inotify.c
index eba2f0147409..6b82d5e9ab96 100644
--- a/sys/kern/vfs_inotify.c
+++ b/sys/kern/vfs_inotify.c
@@ -377,7 +377,14 @@ inotify_unlink_watch_locked(struct inotify_softc *sc, struct inotify_watch *watc
 static void
 inotify_free_watch(struct inotify_watch *watch)
 {
-	vrele(watch->vp);
+	/*
+	 * Formally, we don't need to lock the vnode here.  However, if we
+	 * don't, and vrele() releases the last reference, it's possible the
+	 * vnode will be recycled while a different thread holds the vnode lock.
+	 * Work around this bug by acquiring the lock here.
+	 */
+	(void)vn_lock(watch->vp, LK_EXCLUSIVE | LK_RETRY);
+	vput(watch->vp);
 	free(watch, M_INOTIFY);
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.