To prevent early transaction aborts under low memory, Btrfs allocates
extent buffers in btrfs_init_eb_prealloc() and inserts their folios into
the btree mapping using __GFP_NOFAIL whenever blocking allocation is
allowed, assuming the caller is not running memory reclaim:
gfp_t gfp = nowait ? GFP_NOWAIT : GFP_NOFS | __GFP_NOFAIL;
Since commit d1b5c5671d01 ("btrfs: Prevent from early transaction
abort"), extent buffer allocation and page-cache attachment have passed
__GFP_NOFAIL unconditionally (now in btrfs_init_eb_prealloc() and
attach_eb_folio_to_filemap()). However, kswapd and direct-reclaim tasks
run with PF_MEMALLOC set while shrinking dentries in shrink_dentry_list().
When dcache reclaim evicts an unlinked inode (i_nlink == 0),
btrfs_evict_inode() truncates its B-tree items, triggering copy-on-write
via btrfs_init_new_buffer() and alloc_extent_buffer(). Because
btrfs_init_new_buffer() sets supports_nowait to false, both
btrfs_init_eb_prealloc() and attach_eb_folio_to_filemap() use __GFP_NOFAIL.
A PF_MEMALLOC task cannot run direct reclaim for itself, so low free
memory sends that __GFP_NOFAIL request into __alloc_pages_slowpath(),
hitting WARN_ON_ONCE(current->flags & PF_MEMALLOC) and risking a lockup
if reserves run out:
current->flags & PF_MEMALLOC
WARNING: mm/page_alloc.c:4837 at __alloc_pages_slowpath+0x129f/0x12c0 mm/page_alloc.c:4837, CPU#1: kswapd0/94
CPU: 1 UID: 0 PID: 94 Comm: kswapd0 Not tainted 7.4.0-rc5 #62 PREEMPT(full)
RIP: 0010:__alloc_pages_slowpath+0x129f/0x12c0 mm/page_alloc.c:4837
Call Trace:
__alloc_frozen_pages_noprof+0x3f8/0x450 mm/page_alloc.c:5523
__alloc_pages_noprof+0x10/0x100 mm/page_alloc.c:5545
alloc_pages_bulk_noprof+0x6d5/0x8f0 mm/page_alloc.c:5362
btrfs_alloc_page_array fs/btrfs/extent_io.c:691 [inline]
alloc_eb_folio_array+0xfc/0x410 fs/btrfs/extent_io.c:715
btrfs_init_eb_prealloc+0x1de/0x3c0 fs/btrfs/extent_io.c:3790
alloc_extent_buffer+0x1538/0x26f0 fs/btrfs/extent_io.c:3863
btrfs_init_new_buffer fs/btrfs/extent-tree.c:5264 [inline]
btrfs_alloc_tree_block+0x27c/0x1110 fs/btrfs/extent-tree.c:5377
btrfs_force_cow_block+0x573/0x1d10 fs/btrfs/ctree.c:501
btrfs_cow_block+0x3cf/0xa80 fs/btrfs/ctree.c:696
btrfs_search_slot+0xd49/0x2ca0 fs/btrfs/ctree.c:-1
btrfs_truncate_inode_items+0x44d/0x1ec0 fs/btrfs/inode-item.c:498
btrfs_evict_inode+0x91d/0xe50 fs/btrfs/inode.c:5650
evict+0x66b/0xb90 fs/inode.c:829
dentry_kill+0x22e/0x9b0 fs/dcache.c:857
shrink_kill fs/dcache.c:1317 [inline]
shrink_dentry_list+0x209/0x310 fs/dcache.c:1334
prune_dcache_sb+0x10e/0x180 fs/dcache.c:1416
super_cache_scan+0x36f/0x4b0 fs/super.c:225
do_shrink_slab+0x6e7/0x1100 mm/shrinker.c:452
shrink_slab_memcg mm/shrinker.c:569 [inline]
shrink_slab+0x7f4/0x1170 mm/shrinker.c:647
shrink_one+0x2fa/0x780 mm/vmscan.c:5327
shrink_many mm/vmscan.c:5388 [inline]
lru_gen_shrink_node mm/vmscan.c:5466 [inline]
shrink_node+0x57f4/0x60a0 mm/vmscan.c:6472
kswapd_shrink_node mm/vmscan.c:7265 [inline]
balance_pgdat mm/vmscan.c:7436 [inline]
kswapd+0x1496/0x2eb0 mm/vmscan.c:7712
kthread+0x38c/0x480 kernel/kthread.c:436
ret_from_fork+0x509/0xb70 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
Omit __GFP_NOFAIL in btrfs_init_eb_prealloc() and
attach_eb_folio_to_filemap() when PF_MEMALLOC is set on current->flags,
and propagate non--EEXIST errors from filemap_add_folio(). Callers
already handle -ENOMEM, and btrfs_evict_inode() stops truncation cleanly
so mount-time orphan cleanup handles the inode later.
Fixes: d1b5c5671d01 ("btrfs: Prevent from early transaction abort")
Closes: https://lore.kernel.org/lkml/CANypQFaT-R48PR04tTZRETT+QHN2T4rUD2vfg2Si5gb4_W2eCg@mail.gmail.com/
Cc: stable@vger.kernel.org
Assisted-by: Antigravity:Gemini
Signed-off-by: Jiaming Zhang
---
Changes in v2:
- Remove the mount/CAP_SYS_ADMIN note from the changelog.
- Include the stack trace in the changelog.
- v1: https://lore.kernel.org/lkml/20261206122013.496614-1-r772577952@gmail.com/
fs/btrfs/extent_io.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index b2739838c646..12918ecd331f 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -3646,6 +3646,7 @@ static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i,
struct address_space *mapping = fs_info->btree_inode->i_mapping;
const pgoff_t index = eb->start >> PAGE_SHIFT;
struct folio *existing_folio;
+ gfp_t gfp = GFP_NOFS;
int ret;
ASSERT(found_eb_ret);
@@ -3653,12 +3654,16 @@ static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i,
/* Caller should ensure the folio exists. */
ASSERT(eb->folios[i]);
+ if (!(current->flags & PF_MEMALLOC))
+ gfp |= __GFP_NOFAIL;
+
retry:
existing_folio = NULL;
- ret = filemap_add_folio(mapping, eb->folios[i], index + i,
- GFP_NOFS | __GFP_NOFAIL);
+ ret = filemap_add_folio(mapping, eb->folios[i], index + i, gfp);
if (!ret)
goto finish;
+ if (ret != -EEXIST)
+ return ret;
/* ret == -EEXIST: a folio already lives at this index. */
existing_folio = filemap_lock_folio(mapping, index + i);
@@ -3751,9 +3756,12 @@ static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i,
int btrfs_init_eb_prealloc(struct btrfs_fs_info *fs_info,
struct btrfs_eb_prealloc *pa, bool nowait)
{
- gfp_t gfp = nowait ? GFP_NOWAIT : GFP_NOFS | __GFP_NOFAIL;
+ gfp_t gfp = nowait ? GFP_NOWAIT : GFP_NOFS;
int ret;
+ if (!nowait && !(current->flags & PF_MEMALLOC))
+ gfp |= __GFP_NOFAIL;
+
ASSERT(!pa->eb, "unexpected non-null eb: %p", pa->eb);
ASSERT(!pa->bfs, "unexpected non-null bfs: %p", pa->bfs);
pa->needs_prealloc = false;
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
--
2.43.0