linux/fs/btrfs
xiaoshoukui ceb9ba8e30 btrfs: fix BUG_ON condition in btrfs_cancel_balance
commit 29eefa6d0d upstream.

Pausing and canceling balance can race to interrupt balance lead to BUG_ON
panic in btrfs_cancel_balance. The BUG_ON condition in btrfs_cancel_balance
does not take this race scenario into account.

However, the race condition has no other side effects. We can fix that.

Reproducing it with panic trace like this:

  kernel BUG at fs/btrfs/volumes.c:4618!
  RIP: 0010:btrfs_cancel_balance+0x5cf/0x6a0
  Call Trace:
   <TASK>
   ? do_nanosleep+0x60/0x120
   ? hrtimer_nanosleep+0xb7/0x1a0
   ? sched_core_clone_cookie+0x70/0x70
   btrfs_ioctl_balance_ctl+0x55/0x70
   btrfs_ioctl+0xa46/0xd20
   __x64_sys_ioctl+0x7d/0xa0
   do_syscall_64+0x38/0x80
   entry_SYSCALL_64_after_hwframe+0x63/0xcd

  Race scenario as follows:
  > mutex_unlock(&fs_info->balance_mutex);
  > --------------------
  > .......issue pause and cancel req in another thread
  > --------------------
  > ret = __btrfs_balance(fs_info);
  >
  > mutex_lock(&fs_info->balance_mutex);
  > if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req)) {
  >         btrfs_info(fs_info, "balance: paused");
  >         btrfs_exclop_balance(fs_info, BTRFS_EXCLOP_BALANCE_PAUSED);
  > }

CC: stable@vger.kernel.org # 4.19+
Signed-off-by: xiaoshoukui <xiaoshoukui@ruijie.com.cn>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2023-08-23 17:52:31 +02:00
..
tests btrfs: convert btrfs_block_group::needs_free_space to runtime flag 2023-08-23 17:52:28 +02:00
acl.c
async-thread.c
async-thread.h
backref.c
backref.h
block-group.c btrfs: fix use-after-free of new block group that became unused 2023-08-23 17:52:29 +02:00
block-group.h btrfs: fix use-after-free of new block group that became unused 2023-08-23 17:52:29 +02:00
block-rsv.c btrfs: account block group tree when calculating global reserve size 2023-08-03 10:24:13 +02:00
block-rsv.h
btrfs_inode.h
check-integrity.c
check-integrity.h
compression.c
compression.h
ctree.c btrfs: do not BUG_ON() on tree mod log failure at __btrfs_cow_block() 2023-07-19 16:22:14 +02:00
ctree.h btrfs: rename BTRFS_FS_NO_OVERCOMMIT to BTRFS_FS_ACTIVE_ZONE_TRACKING 2023-04-06 12:10:35 +02:00
delalloc-space.c
delalloc-space.h
delayed-inode.c
delayed-inode.h
delayed-ref.c
delayed-ref.h
dev-replace.c
dev-replace.h
dir-item.c
discard.c btrfs: hold block group refcount during async discard 2023-03-10 09:34:06 +01:00
discard.h
disk-io.c btrfs: reject invalid reloc tree root keys with stack dump 2023-08-16 18:27:30 +02:00
disk-io.h btrfs: fix compat_ro checks against remount 2023-01-12 12:01:56 +01:00
export.c
export.h
extent_io.c btrfs: don't stop integrity writeback too early 2023-08-16 18:27:29 +02:00
extent_io.h
extent_map.c btrfs: fix incorrect splitting in btrfs_drop_extent_map_range 2023-08-23 17:52:31 +02:00
extent_map.h btrfs: get the next extent map during fiemap/lseek more efficiently 2023-04-26 14:28:38 +02:00
extent-io-tree.c
extent-io-tree.h
extent-tree.c btrfs: set cache_block_group_error if we find an error 2023-08-16 18:27:30 +02:00
file-item.c btrfs: handle memory allocation failure in btrfs_csum_one_bio 2023-06-21 16:00:52 +02:00
file.c btrfs: get the next extent map during fiemap/lseek more efficiently 2023-04-26 14:28:38 +02:00
free-space-cache.c btrfs: fix space cache inconsistency after error loading it from disk 2023-05-17 11:53:42 +02:00
free-space-cache.h
free-space-tree.c btrfs: convert btrfs_block_group::needs_free_space to runtime flag 2023-08-23 17:52:28 +02:00
free-space-tree.h btrfs: make clear_cache mount option to rebuild FST without disabling it 2023-05-17 11:53:42 +02:00
inode-item.c
inode-item.h
inode.c btrfs: properly clear end of the unreserved range in cow_file_range 2023-08-16 18:27:29 +02:00
ioctl.c btrfs: fix assertion of exclop condition when starting balance 2023-05-17 11:53:42 +02:00
Kconfig
locking.c btrfs: add block-group tree to lockdep classes 2023-07-19 16:22:13 +02:00
locking.h
lzo.c
Makefile
misc.h
ordered-data.c
ordered-data.h
orphan.c
print-tree.c btrfs: print-tree: parent bytenr must be aligned to sector size 2023-05-17 11:53:42 +02:00
print-tree.h
props.c
props.h
qgroup.c btrfs: fix race between quota disable and relocation 2023-08-03 10:23:49 +02:00
qgroup.h
raid56.c
raid56.h
rcu-string.h
ref-verify.c
ref-verify.h
reflink.c
reflink.h
relocation.c btrfs: exit gracefully if reloc roots don't match 2023-08-16 18:27:30 +02:00
root-tree.c
scrub.c btrfs: scrub: try harder to mark RAID56 block groups read-only 2023-06-21 16:00:52 +02:00
send.c btrfs: fix uninitialized variable warnings 2023-05-01 08:26:27 +09:00
send.h
space-info.c btrfs: rename BTRFS_FS_NO_OVERCOMMIT to BTRFS_FS_ACTIVE_ZONE_TRACKING 2023-04-06 12:10:35 +02:00
space-info.h
struct-funcs.c
subpage.c
subpage.h
super.c btrfs: make clear_cache mount option to rebuild FST without disabling it 2023-05-17 11:53:42 +02:00
sysfs.c
sysfs.h
transaction.c btrfs: check for commit error at btrfs_attach_transaction_barrier() 2023-08-03 10:24:13 +02:00
transaction.h
tree-checker.c btrfs: reject invalid reloc tree root keys with stack dump 2023-08-16 18:27:30 +02:00
tree-checker.h
tree-defrag.c btrfs: move the auto defrag code to defrag.c 2023-02-22 12:59:40 +01:00
tree-log.c btrfs: fix an uninitialized variable warning in btrfs_log_inode 2023-06-28 11:12:36 +02:00
tree-log.h btrfs: simplify update of last_dir_index_offset when logging a directory 2023-02-14 19:11:52 +01:00
tree-mod-log.c
tree-mod-log.h
ulist.c
ulist.h
uuid-tree.c
verity.c
volumes.c btrfs: fix BUG_ON condition in btrfs_cancel_balance 2023-08-23 17:52:31 +02:00
volumes.h
xattr.c
xattr.h
zlib.c btrfs: zlib: zero-initialize zlib workspace 2023-02-14 19:11:40 +01:00
zoned.c btrfs: convert btrfs_block_group::seq_zone to runtime flag 2023-08-23 17:52:28 +02:00
zoned.h
zstd.c