git: 148d61e4d0ef - main - ffs_bdflush(): do not call VOP_FSYNC(devvp) for unlocked devvp

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Thu, 19 Nov 2026 12:18:26 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=148d61e4d0efada9626f665e30beb28c1ec83748

commit 148d61e4d0efada9626f665e30beb28c1ec83748
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-11-17 19:20:48 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-19 12:17:46 +0000

    ffs_bdflush(): do not call VOP_FSYNC(devvp) for unlocked devvp
    
    Instead, delegate the fsync to taskqueue, which can lock mntfs devvp.
    The asynchronous fsync there is fine because bdflush() is not guaranteed
    to write anything at all.
    
    Reported and tested by: pho
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
---
 sys/ufs/ffs/ffs_extern.h   |  1 +
 sys/ufs/ffs/ffs_snapshot.c | 27 +++++++++++++++++++++++----
 sys/ufs/ffs/ffs_vfsops.c   |  7 +++++++
 sys/ufs/ufs/ufsmount.h     |  1 +
 4 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/sys/ufs/ffs/ffs_extern.h b/sys/ufs/ffs/ffs_extern.h
index 0dc52ee48b1e..8ac2a6c44809 100644
--- a/sys/ufs/ffs/ffs_extern.h
+++ b/sys/ufs/ffs/ffs_extern.h
@@ -228,6 +228,7 @@ void	softdep_freework(struct workhead *);
 #define	DATA_ONLY		0x00000002
 
 int	ffs_rdonly(struct inode *);
+void	ffs_devvp_bdflush_fsync(void *arg, int pending);
 
 TAILQ_HEAD(snaphead, inode);
 
diff --git a/sys/ufs/ffs/ffs_snapshot.c b/sys/ufs/ffs/ffs_snapshot.c
index f1170d822131..c32033f5b28f 100644
--- a/sys/ufs/ffs/ffs_snapshot.c
+++ b/sys/ufs/ffs/ffs_snapshot.c
@@ -51,6 +51,7 @@
 #include <sys/rwlock.h>
 #include <sys/sched.h>
 #include <sys/stat.h>
+#include <sys/taskqueue.h>
 #include <sys/vnode.h>
 
 #include <vm/vm.h>
@@ -122,6 +123,11 @@ ffs_sync_snap(struct mount *mp, int waitfor)
 {
 }
 
+void
+ffs_devvp_bdflush_fsync(void *arg, int pending)
+{
+}
+
 #else
 FEATURE(ffs_snapshot, "FFS snapshot support");
 
@@ -2194,18 +2200,31 @@ ffs_bp_snapblk(struct vnode *devvp, struct buf *bp)
 	return (0);
 }
 
+void
+ffs_devvp_bdflush_fsync(void *arg, int pending)
+{
+	struct ufsmount *ump;
+	struct vnode *devvp;
+
+	ump = arg;
+	devvp = ump->um_devvp;
+	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
+	(void)VOP_FSYNC(devvp, MNT_NOWAIT, curthread);
+	VOP_UNLOCK(devvp);
+	altbufferflushes++;
+}
+
 void
 ffs_bdflush(struct bufobj *bo, struct buf *bp)
 {
-	struct thread *td;
 	struct vnode *vp, *devvp;
 	struct buf *nbp;
+	struct ufsmount *ump;
 	int bp_bdskip;
 
 	if (bo->bo_dirty.bv_cnt <= dirtybufthresh)
 		return;
 
-	td = curthread;
 	vp = bp->b_vp;
 	devvp = bo2vnode(bo);
 	KASSERT(vp == devvp, ("devvp != vp %p %p", bo, bp));
@@ -2216,8 +2235,8 @@ ffs_bdflush(struct bufobj *bo, struct buf *bp)
 		bdwriteskip++;
 	VI_UNLOCK(devvp);
 	if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10 && !bp_bdskip) {
-		(void) VOP_FSYNC(vp, MNT_NOWAIT, td);
-		altbufferflushes++;
+		ump = VFSTOUFS((struct mount *)devvp->v_data);
+		taskqueue_enqueue(taskqueue_thread, &ump->um_devvp_fsync_task);
 	} else {
 		BO_LOCK(bo);
 		/*
diff --git a/sys/ufs/ffs/ffs_vfsops.c b/sys/ufs/ffs/ffs_vfsops.c
index b692f9c49a2a..9f13f049f7a2 100644
--- a/sys/ufs/ffs/ffs_vfsops.c
+++ b/sys/ufs/ffs/ffs_vfsops.c
@@ -55,6 +55,7 @@
 #include <sys/mutex.h>
 #include <sys/rwlock.h>
 #include <sys/sysctl.h>
+#include <sys/taskqueue.h>
 #include <sys/vmmeter.h>
 
 #include <security/mac/mac_framework.h>
@@ -1144,6 +1145,9 @@ ffs_mountfs(struct vnode *odevvp, struct mount *mp, struct thread *td)
 #ifdef UFS_EXTATTR
 	ufs_extattr_uepm_init(&ump->um_extattr);
 #endif
+
+	TASK_INIT(&ump->um_devvp_fsync_task, 0, ffs_devvp_bdflush_fsync, ump);
+
 	/*
 	 * Set FS local "last mounted on" information (NULL pad)
 	 */
@@ -1202,6 +1206,8 @@ out:
 		free(fs->fs_si, M_UFSMNT);
 		free(fs, M_UFSMNT);
 	}
+	if (ump != NULL)
+		taskqueue_drain(taskqueue_thread, &ump->um_devvp_fsync_task);
 	if (cp != NULL)
 		g_vfs_close_unlocked(cp);
 	if (ump != NULL) {
@@ -1317,6 +1323,7 @@ ffs_unmount(struct mount *mp, int mntflags)
 		taskqueue_free(ump->um_trim_tq);
 		free (ump->um_trimhash, M_TRIM);
 	}
+	taskqueue_drain(taskqueue_thread, &ump->um_devvp_fsync_task);
 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
 	g_topology_lock();
 	g_vfs_close(ump->um_cp);
diff --git a/sys/ufs/ufs/ufsmount.h b/sys/ufs/ufs/ufsmount.h
index d33b01e4425e..4f57ef715865 100644
--- a/sys/ufs/ufs/ufsmount.h
+++ b/sys/ufs/ufs/ufsmount.h
@@ -117,6 +117,7 @@ struct ufsmount {
 	struct	trimlist_hashhead *um_trimhash;	/* (i) trimlist hash table */
 	u_long	um_trimlisthashsize;		/* (i) trim hash table size-1 */
 						/* (c) - below function ptrs */
+	struct	task um_devvp_fsync_task;
 	int	(*um_balloc)(struct vnode *, off_t, int, struct ucred *,
 		    int, struct buf **);
 	int	(*um_blkatoff)(struct vnode *, off_t, char **, struct buf **);