Skip to content

Commit 17e2167

Browse files
committed
Linux: Fix zfs_prune panics v2 (openzfs#17121)
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]> Reviewed-by: Tony Hutter <[email protected]> Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Pavel Snajdr <[email protected]>
1 parent b1f0e92 commit 17e2167

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

config/kernel-sb-dying.m4

Lines changed: 19 additions & 0 deletions
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+
AC_MSG_RESULT(no)
18+
])
19+
])

config/kernel.m4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ AC_DEFUN([ZFS_AC_KERNEL_TEST_SRC], [
103103
ZFS_AC_KERNEL_SRC_SECURITY_INODE
104104
ZFS_AC_KERNEL_SRC_FST_MOUNT
105105
ZFS_AC_KERNEL_SRC_BDI
106+
ZFS_AC_KERNEL_SRC_SB_DYING
106107
ZFS_AC_KERNEL_SRC_SET_NLINK
107108
ZFS_AC_KERNEL_SRC_SGET
108109
ZFS_AC_KERNEL_SRC_LSEEK_EXECUTE
@@ -255,6 +256,7 @@ AC_DEFUN([ZFS_AC_KERNEL_TEST_RESULT], [
255256
ZFS_AC_KERNEL_SECURITY_INODE
256257
ZFS_AC_KERNEL_FST_MOUNT
257258
ZFS_AC_KERNEL_BDI
259+
ZFS_AC_KERNEL_SB_DYING
258260
ZFS_AC_KERNEL_SET_NLINK
259261
ZFS_AC_KERNEL_SGET
260262
ZFS_AC_KERNEL_LSEEK_EXECUTE

module/os/linux/zfs/zpl_super.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -381,17 +381,25 @@ zpl_prune_sb(uint64_t nr_to_scan, void *arg)
381381
int objects = 0;
382382

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

397405
const struct super_operations zpl_super_operations = {

0 commit comments

Comments
 (0)