r

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 1/4/20 8:56 AM, Qu Wenruo wrote:
[BUG]
There are several different KASAN reports for balance + snapshot
workloads.
Involved call paths include:

    should_ignore_root+0x54/0xb0 [btrfs]
    build_backref_tree+0x11af/0x2280 [btrfs]
    relocate_tree_blocks+0x391/0xb80 [btrfs]
    relocate_block_group+0x3e5/0xa00 [btrfs]
    btrfs_relocate_block_group+0x240/0x4d0 [btrfs]
    btrfs_relocate_chunk+0x53/0xf0 [btrfs]
    btrfs_balance+0xc91/0x1840 [btrfs]
    btrfs_ioctl_balance+0x416/0x4e0 [btrfs]
    btrfs_ioctl+0x8af/0x3e60 [btrfs]
    do_vfs_ioctl+0x831/0xb10
    ksys_ioctl+0x67/0x90
    __x64_sys_ioctl+0x43/0x50
    do_syscall_64+0x79/0xe0
    entry_SYSCALL_64_after_hwframe+0x49/0xbe

    create_reloc_root+0x9f/0x460 [btrfs]
    btrfs_reloc_post_snapshot+0xff/0x6c0 [btrfs]
    create_pending_snapshot+0xa9b/0x15f0 [btrfs]
    create_pending_snapshots+0x111/0x140 [btrfs]
    btrfs_commit_transaction+0x7a6/0x1360 [btrfs]
    btrfs_mksubvol+0x915/0x960 [btrfs]
    btrfs_ioctl_snap_create_transid+0x1d5/0x1e0 [btrfs]
    btrfs_ioctl_snap_create_v2+0x1d3/0x270 [btrfs]
    btrfs_ioctl+0x241b/0x3e60 [btrfs]
    do_vfs_ioctl+0x831/0xb10
    ksys_ioctl+0x67/0x90
    __x64_sys_ioctl+0x43/0x50
    do_syscall_64+0x79/0xe0
    entry_SYSCALL_64_after_hwframe+0x49/0xbe

    btrfs_reloc_pre_snapshot+0x85/0xc0 [btrfs]
    create_pending_snapshot+0x209/0x15f0 [btrfs]
    create_pending_snapshots+0x111/0x140 [btrfs]
    btrfs_commit_transaction+0x7a6/0x1360 [btrfs]
    btrfs_mksubvol+0x915/0x960 [btrfs]
    btrfs_ioctl_snap_create_transid+0x1d5/0x1e0 [btrfs]
    btrfs_ioctl_snap_create_v2+0x1d3/0x270 [btrfs]
    btrfs_ioctl+0x241b/0x3e60 [btrfs]
    do_vfs_ioctl+0x831/0xb10
    ksys_ioctl+0x67/0x90
    __x64_sys_ioctl+0x43/0x50
    do_syscall_64+0x79/0xe0
    entry_SYSCALL_64_after_hwframe+0x49/0xbe

[CAUSE]
All these call sites are only relying on root->reloc_root, which can
undergo btrfs_drop_snapshot(), and since we don't have real refcount
based protection to reloc roots, we can reach already dropped reloc
root, triggering KASAN.

[FIX]
To avoid such access to unstable root->reloc_root, we should check
BTRFS_ROOT_DEAD_RELOC_TREE bit first.

This patch introduces a new wrapper, have_reloc_root(), to do the proper
check for most callers who don't distinguish merged reloc tree and no
reloc tree.

The only exception is should_ignore_root(), as merged reloc tree can be
ignored, while no reloc tree shouldn't.

Also, set_bit()/clear_bit()/test_bit() doesn't imply a memory barrier,
and BTRFS_ROOT_DEAD_RELOC_TREE is the only indicator, also add extra
memory barrier for that bit.

Reported-by: Zygo Blaxell <ce3g8jdj@xxxxxxxxxxxxxxxxxxxxx>
Fixes: d2311e698578 ("btrfs: relocation: Delay reloc tree deletion after merge_reloc_roots")
Singed-off-by: David Sterba <dsterba@xxxxxxxx>
Signed-off-by: Qu Wenruo <wqu@xxxxxxxx>
---
Difference between this and David's diff:
- Use proper smp_mb__after_atomic() for clear_bit()
- Use test_bit() only check for should_ignore_root()
   That call site is an except, can't go regular have_reloc_root() check
- Add extra comment for have_reloc_root()
---

This took me a minute to figure out, but from what I can tell you are doing the mb's around the BTRFS_ROOT_DEAD_RELOC_TREE flag so that in clean_dirty_subvols() where we clear the bit and then set root->reloc_root = NULL we are sure to either see the bit or that reloc_root == NULL.

That's fine, but man all these random memory barriers around the bit messing make 0 sense and confuse the issue, what we really want is the smp_mb__after_atomic() in clean_dirty_subvols() and the smp_mb__before_atomic() in have_reloc_root().

But instead since we really want to know the right answer for root->reloc_root, and we clear that _before_ we clear the BTRFS_ROOT_DEAD_RELOC_TREE let's just do READ_ONCE/WRITE_ONCE everywhere we access the reloc_root. In fact you could just do

static struct btrfs_root get_reloc_root(struct btrfs_root *root)
{
	if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state))
		return NULL;
	return READ_ONCE(root->reloc_root);
}

then instead of the patter of

if (!have_reloc_root)
	return;
reloc_root = root->reloc_root;

do

reloc_root = get_reloc_root(root);
if (!reloc_root)
	return;

Then you only have the READ_ONCE/WRITE_ONCE in one place.  Thanks,

Josef



[Index of Archives]     [Linux Filesystem Development]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux