The patch below does not apply to the 5.4-stable tree. If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <stable@xxxxxxxxxxxxxxx>. Possible dependencies: 162d053e15fe ("btrfs: do not BUG_ON() on ENOMEM when dropping extent items for a range") 2766ff61762c ("btrfs: update the number of bytes used by an inode atomically") 5893dfb98f25 ("btrfs: refactor btrfs_drop_extents() to make it easier to extend") ac5887c8e013 ("btrfs: locking: remove all the blocking helpers") a14b78ad06ab ("btrfs: introduce btrfs_inode_lock()/unlock()") b8d8e1fd570a ("btrfs: introduce btrfs_write_check()") c86537a42f86 ("btrfs: check FS error state bit early during write") 5e8b9ef30392 ("btrfs: move pos increment and pagecache extension to btrfs_buffered_write") 4e4cabece9f9 ("btrfs: split btrfs_direct_IO to read and write") 196d59ab9ccc ("btrfs: switch extent buffer tree lock to rw_semaphore") 0425e7badbdc ("btrfs: don't fallback to buffered read if we don't need to") 3c38c877fcb9 ("btrfs: sink inode argument in insert_ordered_extent_file_extent") fc0d82e103c7 ("btrfs: sink total_data parameter in setup_items_for_insert") 3dc9dc8969dc ("btrfs: eliminate total_size parameter from setup_items_for_insert") 0cbb5bdfea26 ("btrfs: rename btrfs_insert_clone_extent() to a more generic name") 306bfec02b10 ("btrfs: rename btrfs_punch_hole_range() to a more generic name") bf385648fa48 ("btrfs: rename struct btrfs_clone_extent_info to a more generic name") fb870f6cdd72 ("btrfs: remove item_size member of struct btrfs_clone_extent_info") 8fccebfa534c ("btrfs: fix metadata reservation for fallocate that leads to transaction aborts") 53ac7ead2446 ("btrfs: make btrfs_invalidatepage work on btrfs_inode") thanks, greg k-h ------------------ original commit in Linus's tree ------------------ >From 162d053e15fe985f754ef495a96eb3db970c43ed Mon Sep 17 00:00:00 2001 From: Filipe Manana <fdmanana@xxxxxxxx> Date: Mon, 28 Nov 2022 15:07:30 +0000 Subject: [PATCH] btrfs: do not BUG_ON() on ENOMEM when dropping extent items for a range If we get -ENOMEM while dropping file extent items in a given range, at btrfs_drop_extents(), due to failure to allocate memory when attempting to increment the reference count for an extent or drop the reference count, we handle it with a BUG_ON(). This is excessive, instead we can simply abort the transaction and return the error to the caller. In fact most callers of btrfs_drop_extents(), directly or indirectly, already abort the transaction if btrfs_drop_extents() returns any error. Also, we already have error paths at btrfs_drop_extents() that may return -ENOMEM and in those cases we abort the transaction, like for example anything that changes the b+tree may return -ENOMEM due to a failure to allocate a new extent buffer when COWing an existing extent buffer, such as a call to btrfs_duplicate_item() for example. So replace the BUG_ON() calls with proper logic to abort the transaction and return the error. Reported-by: syzbot+0b1fb6b0108c27419f9f@xxxxxxxxxxxxxxxxxxxxxxxxx Link: https://lore.kernel.org/linux-btrfs/00000000000089773e05ee4b9cb4@xxxxxxxxxx/ CC: stable@xxxxxxxxxxxxxxx # 5.4+ Reviewed-by: Josef Bacik <josef@xxxxxxxxxxxxxx> Signed-off-by: Filipe Manana <fdmanana@xxxxxxxx> Reviewed-by: David Sterba <dsterba@xxxxxxxx> Signed-off-by: David Sterba <dsterba@xxxxxxxx> diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c index 448b143a5cb2..91b00eb2440e 100644 --- a/fs/btrfs/file.c +++ b/fs/btrfs/file.c @@ -380,7 +380,10 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans, args->start - extent_offset, 0, false); ret = btrfs_inc_extent_ref(trans, &ref); - BUG_ON(ret); /* -ENOMEM */ + if (ret) { + btrfs_abort_transaction(trans, ret); + break; + } } key.offset = args->start; } @@ -467,7 +470,10 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans, key.offset - extent_offset, 0, false); ret = btrfs_free_extent(trans, &ref); - BUG_ON(ret); /* -ENOMEM */ + if (ret) { + btrfs_abort_transaction(trans, ret); + break; + } args->bytes_found += extent_end - key.offset; }