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. Triggering this requires memory pressure while reclaiming unlinked Btrfs dentries (CAP_SYS_ADMIN to mount an image). 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 --- 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