Bug 293198 - zfs deadlock while copying many files, vfs_deleteextattr_dir-related?
Summary: zfs deadlock while copying many files, vfs_deleteextattr_dir-related?
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 15.0-RELEASE
Hardware: amd64 Any
: --- Affects Many People
Assignee: Rick Macklem
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2026-02-15 22:59 UTC by Peter Eriksson
Modified: 2026-05-18 09:23 UTC (History)
6 users (show)

See Also:
rmacklem: mfc-stable15+
rmacklem: mfc-stable14+


Attachments
Output from procstat -kk -a (485.82 KB, text/plain)
2026-02-15 22:59 UTC, Peter Eriksson
no flags Details
Test to see if not doing VFS_MOUNT() for exports fixes his hang (580 bytes, patch)
2026-02-16 16:27 UTC, Rick Macklem
no flags Details | Diff
ksh script to test and provoke the issue (702 bytes, application/x-shellscript)
2026-02-16 17:56 UTC, Peter Eriksson
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Peter Eriksson 2026-02-15 22:59:31 UTC
Created attachment 268084 [details]
Output from procstat -kk -a

While copying many zfs (30000) filesystems (using a loop of zfs create ; tar cpf - | (cd xxx ; tar xpf -) the bsdtar writing the files (and eventually the whole system) deadlocks at after some 479 filesystems have been copied:

# fgrep bsdtar /tmp/procstat-kka-2.out 

48759 103729 bsdtar              -                   mi_switch+0xbc sleepq_catch_signals+0x27d sleepq_wait_sig+0x9 _sleep+0x197 uipc_sosend_stream_or_seqpacket+0x475 sousrsend+0x5f dofilewrite+0x81 sys_write+0x127 amd64_syscall+0x126 fast_syscall_common+0xf8 

48760 105220 bsdtar              -                   mi_switch+0xbc _sleep+0x19e rms_rlock_fallback+0x90 zfs_lookup+0x74 zfs_deleteextattr_dir+0x38 zfs_setextattr+0x7dc VOP_SETEXTATTR_APV+0x24 extattr_set_vp+0x11d kern_extattr_set_path+0x14e sys_extattr_set_link+0x6b amd64_syscall+0x126 fast_syscall_common+0xf8 

My reading of the deadlock is that it is related to some extattrs. 


ps auxww|grep bsdtar indicates that it was copying some stuff in a Documents/My Videos directory that looks like this in the source:

# ls -laR My\ Videos
total 28
drwxr-xr-x  2 agnra495 student-liu.se   3 Apr  9  2025 $RECYCLE.BIN
drwxr-xr-x  3 agnra495 student-liu.se   4 Apr 15  2025 .
drwxr-xr-x  6 agnra495 student-liu.se   7 Apr 15  2025 ..
-rw-r--r--  1 agnra495 student-liu.se 504 Apr 15  2025 desktop.ini

My Videos/$RECYCLE.BIN:
total 11
drwxr-xr-x  2 agnra495 student-liu.se   3 Apr  9  2025 .
drwxr-xr-x  3 agnra495 student-liu.se   4 Apr 15  2025 ..
-rw-r--r--  1 agnra495 student-liu.se 129 Apr  9  2025 desktop.ini

There are some extattrs set on the files but nothing strange:

# find . -print | xargs lsextattr user 
.	DOSATTRIB
./desktop.ini	DOSATTRIB
./$RECYCLE.BIN	DOSATTRIB
./$RECYCLE.BIN/desktop.ini	DOSATTRIB

# find . -print | xargs lsextattr system
.	
./desktop.ini	
./$RECYCLE.BIN	
./$RECYCLE.BIN/desktop.ini	

Attaching the full "procstat -kk -a" output.

The copying is being done inside a Zpool.
Comment 1 Rick Macklem freebsd_committer freebsd_triage 2026-02-16 00:05:04 UTC
I looked at this and replied to his email
to freebsd-fs@.

It looks like the hang occurs when zfs_enter() tries
to acquire a read lock on z_teardown_lock via
ZFS_TEARDOWN_ENTER_READ().
There are a bunch of these near the end of the procstat -kk -a.
(The extattr one is just one of them.)

I think this means that something holds a write
lock on z_teardown_lock for the file system.
At a glance, a call to zfs_suspend_fs() seems
like a possible culprit, but I really have no
idea what might acquire a write lock on z_teardown_lock
or if there is another reason that ZFS_TEARDOWN_ENTER_READ()
wouldn't be able to get the read lock?
--> There is this line in the procstat...
0 100204 kernel              deferred_unmount ta mi_switch+0xbc _sleep+0x19e taskqueue_thread_loop+0xb1 fork_exit+0x7b fork_trampoline+0xe
--> I have no idea what this deferred_unmount is referring to?
Comment 2 Peter Eriksson 2026-02-16 00:19:13 UTC
I noticed that the only thing holding a rms_wlock is the mountd daemon.

# egrep rms procstat-kka-2.out 
23314 104597 mountd              -                   mi_switch+0xbc _sleep+0x19e rms_wlock+0xb0 zfs_mount+0x38c vfs_domount_update+0x3b6 vfs_domount+0x289 vfs_donmount+0x947 sys_nmount+0x60 amd64_syscall+0x126 fast_syscall_common+0xf8 

I'm running a test right now with xattr=dir, but if that hangs too, then next I run a test with sharenfs=off
Comment 3 Rick Macklem freebsd_committer freebsd_triage 2026-02-16 00:34:50 UTC
(In reply to Peter Eriksson from comment #2)
I didn't spot the mountd one..

Unless they really need to be running, you could
just shut down mountd and nfsd while doing this.
(It makes sense that mountd would be updating
exports while this is happening, if it is running.)
Comment 4 Peter Eriksson 2026-02-16 00:53:34 UTC
Yeah, these filesystems (at this time) really shouldn't have been nfs-exported so I'm going to stop that when I've stopped testing for this problem.

The exports file is located on a separate zpool/filsystem from the zpool where we are doing these copying. (I'm also using my modified mountd/zfs that uses the BerkeleyDB-database-exports.db but that just makes things (much) faster...)

With xattr=dir the copying now has copied more than 800 filesystems, whereas it previously hanged around 480... Now it's time to sleep though :-)
Comment 5 Peter Eriksson 2026-02-16 07:05:08 UTC
Nope, that wasn't it - still deadlocked (literally seconds after I left the computer - after 882 filesystems copied...)

38035 105881 rsync               -                   mi_switch+0xbc _sleep+0x19e rms_rlock_fallback+0x90 zfs_freebsd_open+0x34 VOP_OPEN_APV+0x19 vn_open_vnode+0x1e0 vn_open_cred+0x6f2 zfs_setextattr+0x4ec VOP_SETEXTATTR_APV+0x24 extattr_set_vp+0x11d kern_extattr_set_path+0x14e sys_extattr_set_link+0x6b amd64_syscall+0x126 fast_syscall_common+0xf8 

23239 104623 mountd              -                   mi_switch+0xbc _sleep+0x19e rms_wlock+0xb0 zfs_mount+0x38c vfs_domount_update+0x3b6 vfs_domount+0x289 vfs_donmount+0x947 sys_nmount+0x60 amd64_syscall+0x126 fast_syscall_common+0xf8
Comment 6 Konstantin Belousov freebsd_committer freebsd_triage 2026-02-16 07:13:22 UTC
(In reply to Peter Eriksson from comment #5)
I _suspect_ that was is happen there is lock recursion.

Note that the thread 105881 entered VOP_SETEXTATTR(), which (there is my guess)
taken the read lock, and after that it called VOP_OPEN(), which tries to take the
read lock once more.

Meantime, the thread 104623 tries to take the same lock in the write mode, that
1. blocks further readers
2. blocks the thread itself because there is waiting writer

And the system deadlocks.  Either zfs should stop recursing into VFS on extattr ops,
or the lock should be not taken recursively, or it should be changed to allow
readers to recurse even in presence of writers contenders.  The later would mean
that writers cannot make progress if there is steady number of readers.
Comment 7 Peter Eriksson 2026-02-16 11:53:41 UTC
(In reply to Konstantin Belousov from comment #6)

Setting “sharenfs=off” on the new directories seems make the copying to better, Have now copied 5140 filesystems without a deadlock.

I agree that it feels like there is a double lock/deadlock somewhere in the extattr handling code. Here’s procstat entries from three different runs that all ended in a deadlock btw:

# egrep 'VOP|rms_wlock' /tmp/procstat-kka.out

22829 103712 mountd              -                   mi_switch+0xbc _sleep+0x19e rms_wlock+0xb0 zfs_mount+0x38c vfs_domount_update+0x3b6 vfs_domount+0x289 vfs_donmount+0x947 sys_nmount+0x60 amd64_syscall+0x126 fast_syscall_common+0xf8

63998 290883 bsdtar              -                   mi_switch+0xbc _sleep+0x19e rms_rlock_fallback+0x90 zfs_lookup+0x74 zfs_deleteextattr_dir+0x38 zfs_setextattr+0x7dc VOP_SETEXTATTR_APV+0x24 extattr_set_vp+0x11d kern_extattr_set_path+0x14e sys_extattr_set_link+0x6b amd64_syscall+0x126 fast_syscall_common+0xf8


# egrep 'VOP|rms_wlock' /tmp/procstat-kka-2.out

23239 104623 mountd              -                   mi_switch+0xbc _sleep+0x19e rms_wlock+0xb0 zfs_mount+0x38c vfs_domount_update+0x3b6 vfs_domount+0x289 vfs_donmount+0x947 sys_nmount+0x60 amd64_syscall+0x126 fast_syscall_common+0xf8

38035 105881 rsync               -                   mi_switch+0xbc _sleep+0x19e rms_rlock_fallback+0x90 zfs_freebsd_open+0x34 VOP_OPEN_APV+0x19 vn_open_vnode+0x1e0 vn_open_cred+0x6f2 zfs_setextattr+0x4ec VOP_SETEXTATTR_APV+0x24 extattr_set_vp+0x11d kern_extattr_set_path+0x14e sys_extattr_set_link+0x6b amd64_syscall+0x126 fast_syscall_common+0xf8


# egrep 'VOP|rms_wlock' /tmp/procstat-kka-3.out

20872 103630 mountd              -                   mi_switch+0xbc _sleep+0x19e rms_wlock+0xb0 zfs_mount+0x38c vfs_domount_update+0x3b6 vfs_domount+0x289 vfs_donmount+0x947 sys_nmount+0x60 amd64_syscall+0x126 fast_syscall_common+0xf8

40121 103746 rsync               -                   mi_switch+0xbc _sleep+0x19e rms_rlock_fallback+0x90 zfs_setattr+0x96 zfs_freebsd_setattr+0x411 VOP_SETATTR_APV+0x24 setfown+0xb1 kern_fchownat+0x159 sys_lchown+0x26 amd64_syscall+0x126 fast_syscall_common+0xf8

40122 104328 rsync               -                   mi_switch+0xbc _sleep+0x19e rms_rlock_fallback+0x90 zfs_lookup+0x74 zfs_deleteextattr_dir+0x38 zfs_setextattr+0x7dc VOP_SETEXTATTR_APV+0x24 extattr_set_vp+0x11d kern_extattr_set_path+0x14e sys_extattr_set_link+0x6b amd64_syscall+0x126 fast_syscall_common+0xf8


(I switched from using tar to rsync after the first run)
Comment 8 Peter Eriksson 2026-02-16 12:23:23 UTC
I submitted a bug report to OpenZFS also:

https://github.com/openzfs/zfs/issues/18228
Comment 9 Rick Macklem freebsd_committer freebsd_triage 2026-02-16 14:11:30 UTC
(In reply to Konstantin Belousov from comment #6)
Yep, my assessment is about the same.
zfs_enter_verify_zp()/zfs_exit() gets called multiple
times during zfs_setextattr(), which allows the mountd
thread doing nmount() to put the exports on the mount
point to acquire the write (exclusive) lock while
zfs_setextattr() is in progress.

I think the fix involves adding an extra "already locked"
argument to zfs_lookup(), zfs_open() { and maybe others }
to avoid re-locking via zfs_enter_verify_zp()/zfs_exit().

But, as Kostik notes, it is not going to be a small patch,
because all the VOP_xxx() calls need to be replaced by the
zfs_xxx() calls below them, so the extra argument can be
passed in correctly. (The VOP_xxx() calls don't know they
are being called from zfs_setextattr().)
--> If there was a way to do rm_rowned() { test for ownership
    of a read lock }, this could be patched much more easily.
    Kostik, I'm guessing that isn't something easily done?
Comment 10 Rick Macklem freebsd_committer freebsd_triage 2026-02-16 14:11:32 UTC
(In reply to Konstantin Belousov from comment #6)
Yep, my assessment is about the same.
zfs_enter_verify_zp()/zfs_exit() gets called multiple
times during zfs_setextattr(), which allows the mountd
thread doing nmount() to put the exports on the mount
point to acquire the write (exclusive) lock while
zfs_setextattr() is in progress.

I think the fix involves adding an extra "already locked"
argument to zfs_lookup(), zfs_open() { and maybe others }
to avoid re-locking via zfs_enter_verify_zp()/zfs_exit().

But, as Kostik notes, it is not going to be a small patch,
because all the VOP_xxx() calls need to be replaced by the
zfs_xxx() calls below them, so the extra argument can be
passed in correctly. (The VOP_xxx() calls don't know they
are being called from zfs_setextattr().)
--> If there was a way to do rm_rowned() { test for ownership
    of a read lock }, this could be patched much more easily.
    Kostik, I'm guessing that isn't something easily done?
Comment 11 Konstantin Belousov freebsd_committer freebsd_triage 2026-02-16 14:23:45 UTC
(In reply to Rick Macklem from comment #10)
It uses rms (read mostly sleepable) locks, if I read the maze of all defines
correctly.  That locks do not track readers at all, it is impossible to know
if curthread already owns the read section.
Comment 12 Mark Johnston freebsd_committer freebsd_triage 2026-02-16 14:35:10 UTC
I believe the "solution" for other lock types is to count the number of read locks held by the current thread, e.g., in td_rw_rlocks.  If the current thread's counter is greater than 0, then it's allowed to bypass writes.
Comment 13 Rick Macklem freebsd_committer freebsd_triage 2026-02-16 16:27:19 UTC
Created attachment 268106 [details]
Test to see if not doing VFS_MOUNT() for exports fixes his hang

This patch avoids a call to VFS_MOUNT()
when doing exports updates.
It does not fix the "general" problem,
but might fix this case, since I don't
think a VFS_MOUNT() call is ever needed
for exports updates.

Please test this patch with sharenfs=on
if possible.
Comment 14 Peter Eriksson 2026-02-16 17:56:30 UTC
Created attachment 268108 [details]
ksh script to test and provoke the issue

Attached a small test script to test and provoke this bug. Modify and run, for me it crashed in around 200 loops...
Comment 15 Peter Eriksson 2026-02-16 18:13:27 UTC
(In reply to Rick Macklem from comment #13)

Yep. A kernel with that patch seems to not deadlock with my test-script.

I'm not sure (haven't measured, just watching the count increase), but I also have a feeling that it runs faster than without that patch as the number of filesystems grow. 

Previously it visibly slowed down when it got up to around 2000 filesystems - but that version of the test script didn't manage to provoke the deadlock (used touch/mkdir/setextattr on files instead of using rsync to clone a template filesystem).

Currently at loop # 7000 and no deadlock.
Comment 16 commit-hook freebsd_committer freebsd_triage 2026-02-17 23:01:46 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=935cf3284f520c90a63baaadb762caaa30084f5c

commit 935cf3284f520c90a63baaadb762caaa30084f5c
Author:     Rick Macklem <rmacklem@FreeBSD.org>
AuthorDate: 2026-02-17 22:57:42 +0000
Commit:     Rick Macklem <rmacklem@FreeBSD.org>
CommitDate: 2026-02-17 22:59:57 +0000

    vfs_mount.c: Don't call VFS_MOUNT() if only exports are being updated

    PR#293198 reports a hang within ZFS when exports
    are being updated concurrently with a VOP_SETEXTATTR().
    The hang appears to be caused by mishandling of the
    z_teardown_lock, but fixing handling of this lock appears
    to be a major effort. Since the hang occurs when
    VFS_MOUNT() acquires a write/exclusive z_teardown_lock,
    which rarely occurs, except when exports are being updated,
    this patch avoids the VFS_MOUNT() call for this case.

    Avoiding a VFS_MOUNT() call fixes the hang for the case
    reported by PR#293198 and is also an optimization.
    As such, this patch avoids the VFS_MOUNT() call when only exports
    are being updated similar to what was already being done
    within vnet prisons.

    PR:     293198
    Reviewed by:    kib, markj
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D55318

 sys/kern/vfs_mount.c | 52 +++++++++++++++++++++++++++++++---------------------
 1 file changed, 31 insertions(+), 21 deletions(-)
Comment 17 Rick Macklem freebsd_committer freebsd_triage 2026-02-17 23:09:33 UTC
I have committed the patch to main and
will MFC it.

I will make a comment on the OpenZFS problem
report, but will leave that one Open, since the
more general problem of a race between updating
a ZFS file system and setting an extended attribute
is not fixed (and that will not be easy).
Comment 18 commit-hook freebsd_committer freebsd_triage 2026-02-24 01:45:29 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=7ae432e6fbb2350fbb6a805dd0e14b73f8e0c9ef

commit 7ae432e6fbb2350fbb6a805dd0e14b73f8e0c9ef
Author:     Rick Macklem <rmacklem@FreeBSD.org>
AuthorDate: 2026-02-17 22:57:42 +0000
Commit:     Rick Macklem <rmacklem@FreeBSD.org>
CommitDate: 2026-02-24 01:41:58 +0000

    vfs_mount.c: Don't call VFS_MOUNT() if only exports are being updated

    PR#293198 reports a hang within ZFS when exports
    are being updated concurrently with a VOP_SETEXTATTR().
    The hang appears to be caused by mishandling of the
    z_teardown_lock, but fixing handling of this lock appears
    to be a major effort. Since the hang occurs when
    VFS_MOUNT() acquires a write/exclusive z_teardown_lock,
    which rarely occurs, except when exports are being updated,
    this patch avoids the VFS_MOUNT() call for this case.

    Avoiding a VFS_MOUNT() call fixes the hang for the case
    reported by PR#293198 and is also an optimization.
    As such, this patch avoids the VFS_MOUNT() call when only exports
    are being updated similar to what was already being done
    within vnet prisons.

    PR:     293198

    (cherry picked from commit 935cf3284f520c90a63baaadb762caaa30084f5c)

 sys/kern/vfs_mount.c | 52 +++++++++++++++++++++++++++++++---------------------
 1 file changed, 31 insertions(+), 21 deletions(-)
Comment 19 commit-hook freebsd_committer freebsd_triage 2026-02-24 22:09:09 UTC
A commit in branch stable/14 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=5153ca331b4448e062cebad987773756c0aff2dc

commit 5153ca331b4448e062cebad987773756c0aff2dc
Author:     Rick Macklem <rmacklem@FreeBSD.org>
AuthorDate: 2026-02-17 22:57:42 +0000
Commit:     Rick Macklem <rmacklem@FreeBSD.org>
CommitDate: 2026-02-24 22:05:42 +0000

    vfs_mount.c: Don't call VFS_MOUNT() if only exports are being updated

    PR#293198 reports a hang within ZFS when exports
    are being updated concurrently with a VOP_SETEXTATTR().
    The hang appears to be caused by mishandling of the
    z_teardown_lock, but fixing handling of this lock appears
    to be a major effort. Since the hang occurs when
    VFS_MOUNT() acquires a write/exclusive z_teardown_lock,
    which rarely occurs, except when exports are being updated,
    this patch avoids the VFS_MOUNT() call for this case.

    Avoiding a VFS_MOUNT() call fixes the hang for the case
    reported by PR#293198 and is also an optimization.
    As such, this patch avoids the VFS_MOUNT() call when only exports
    are being updated similar to what was already being done
    within vnet prisons.

    PR:     293198

    (cherry picked from commit 935cf3284f520c90a63baaadb762caaa30084f5c)

 sys/kern/vfs_mount.c | 52 +++++++++++++++++++++++++++++++---------------------
 1 file changed, 31 insertions(+), 21 deletions(-)
Comment 20 Rick Macklem freebsd_committer freebsd_triage 2026-02-24 22:37:55 UTC
I have committed the "don't call VFS_MOUNT()"
patch and MFC'd it. I am closing this PR, but
will leave the one in OpenZFS open, since the
underlying ZFS problem is not fixed.

It might be possible to fix the bug in OpenZFS
by changing the lock to be an sx(9) lock and then
use td_sx_slocks to check to see if the lock
has already been acquired. (I have not tried this
yet and it has the ugly side affect that the threads
calling VOP_SETEXTATTR() and friends can never do
so with any sx read locks held.)

This fixes the reported case, which is probably
the one users will encounter.