Skip to content

Commit 66d74b1

Browse files
committed
Linux: Fix zfs_prune panics v2
It turns out that approach taken in the original version of the patch was wrong. So now, we're taking approach in-line with how kernel actually does it - when sb is being torn down, access to it is serialized via sb->s_umount rwsem, only when that lock is taken is it okay to work with s_flags - and the other mistake I was doing was trying to make SB_ACTIVE work, but apparently the kernel checks the negative variant - not SB_DYING and not SB_BORN. Kernels pre-6.6 don't have SB_DYING, but check if sb is hashed instead. Signed-off-by: Pavel Snajdr <[email protected]>
1 parent 57f192f commit 66d74b1

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

config/kernel-sb-dying.m4

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
dnl #
2+
dnl # SB_DYING exists since Linux 6.6
3+
dnl #
4+
AC_DEFUN([ZFS_AC_KERNEL_SRC_SB_DYING], [
5+
ZFS_LINUX_TEST_SRC([sb_dying], [
6+
#include <linux/fs.h>
7+
],[
8+
(void) SB_DYING;
9+
])
10+
])
11+
12+
AC_DEFUN([ZFS_AC_KERNEL_SB_DYING], [
13+
AC_MSG_CHECKING([whether SB_DYING is defined])
14+
ZFS_LINUX_TEST_RESULT([sb_dying], [
15+
AC_MSG_RESULT(yes)
16+
],[
17+
ZFS_LINUX_TEST_ERROR([sb_dying()])
18+
])
19+
])

config/kernel.m4

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ AC_DEFUN([ZFS_AC_KERNEL_TEST_SRC], [
7373
ZFS_AC_KERNEL_SRC_TRUNCATE_SETSIZE
7474
ZFS_AC_KERNEL_SRC_SECURITY_INODE
7575
ZFS_AC_KERNEL_SRC_FST_MOUNT
76+
ZFS_AC_KERNEL_SRC_SB_DYING
7677
ZFS_AC_KERNEL_SRC_SET_NLINK
7778
ZFS_AC_KERNEL_SRC_SGET
7879
ZFS_AC_KERNEL_SRC_VFS_FILEMAP_DIRTY_FOLIO
@@ -184,6 +185,7 @@ AC_DEFUN([ZFS_AC_KERNEL_TEST_RESULT], [
184185
ZFS_AC_KERNEL_TRUNCATE_SETSIZE
185186
ZFS_AC_KERNEL_SECURITY_INODE
186187
ZFS_AC_KERNEL_FST_MOUNT
188+
ZFS_AC_KERNEL_SB_DYING
187189
ZFS_AC_KERNEL_SET_NLINK
188190
ZFS_AC_KERNEL_SGET
189191
ZFS_AC_KERNEL_VFS_FILEMAP_DIRTY_FOLIO

module/os/linux/zfs/zpl_super.c

+17-9
Original file line numberDiff line numberDiff line change
@@ -376,17 +376,25 @@ zpl_prune_sb(uint64_t nr_to_scan, void *arg)
376376
int objects = 0;
377377

378378
/*
379-
* deactivate_locked_super calls shrinker_free and only then
380-
* sops->kill_sb cb, resulting in UAF on umount when trying to reach
381-
* for the shrinker functions in zpl_prune_sb of in-umount dataset.
382-
* Increment if s_active is not zero, but don't prune if it is -
383-
* umount could be underway.
379+
* Ensure the superblock is not in the process of being torn down.
384380
*/
385-
if (atomic_inc_not_zero(&sb->s_active)) {
386-
(void) -zfs_prune(sb, nr_to_scan, &objects);
387-
atomic_dec(&sb->s_active);
381+
#ifdef HAVE_SB_DYING
382+
if (down_read_trylock(&sb->s_umount)) {
383+
if (!(sb->s_flags & SB_DYING) && sb->s_root &&
384+
(sb->s_flags & SB_BORN)) {
385+
(void) zfs_prune(sb, nr_to_scan, &objects);
386+
}
387+
up_read(&sb->s_umount);
388388
}
389-
389+
#else
390+
if (down_read_trylock(&sb->s_umount)) {
391+
if (!hlist_unhashed(&sb->s_instances) &&
392+
sb->s_root && (sb->s_flags & SB_BORN)) {
393+
(void) zfs_prune(sb, nr_to_scan, &objects);
394+
}
395+
up_read(&sb->s_umount);
396+
}
397+
#endif
390398
}
391399

392400
const struct super_operations zpl_super_operations = {

0 commit comments

Comments
 (0)