mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [Linux Kernel Bug] WARNING in alloc_eb_folio_array
@ 2026-12-06 12:18 Jiaming Zhang
  0 siblings, 0 replies; only message in thread
From: Jiaming Zhang @ 2026-12-06 12:18 UTC (permalink / raw)
  To: David Sterba, linux-btrfs; +Cc: Chris Mason, linux-kernel, Jiaming Zhang

Hi Linux kernel developers and maintainers,

We are writing to report a warning issue in the Btrfs subsystem,
reproducible on the latest version of Linux (v7.4-rc5, commit
fe6ff8d5d675) and btrfs/linux for-next branch (commit dd710377456a).

It is triggered by mounting a crafted Btrfs image containing directory
entries whose inodes have link count zero (requiring CAP_SYS_ADMIN to
mount), caching those dentries via lookup, and driving memory pressure
so kswapd shrinks the dentry cache.  Below is the kernel report:

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
Modules linked in:
CPU: 1 UID: 0 PID: 94 Comm: kswapd0 Not tainted 7.4.0-rc5 #62 PREEMPT(full)
Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix,
1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
RIP: 0010:__alloc_pages_slowpath+0x129f/0x12c0 mm/page_alloc.c:4837
Call Trace:
 <TASK>
 __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
 </TASK>

Following is the root cause analysis for this issue.  Note that the
analysis was performed with the assistance of an LLM, and we have tried
our best to ensure its accuracy.

The warning triggers in __alloc_pages_slowpath() because Btrfs allocates
folios for a B-tree metadata buffer with __GFP_NOFAIL while kswapd is
performing memory reclaim (PF_MEMALLOC set), and low free memory forces
that allocation into the page-allocator slow path.

During memory reclaim, kswapd (or a direct-reclaim task) runs with
PF_MEMALLOC set and shrinks cached dentries via shrink_dentry_list(),
dropping their inodes and entering btrfs_evict_inode().  When an evicted
inode has link count zero, btrfs_evict_inode() truncates its B-tree
items via btrfs_truncate_inode_items().

Truncating items modifies the B-tree through copy-on-write, which
creates a new metadata buffer in btrfs_init_new_buffer() and
alloc_extent_buffer().  Since commit d1b5c5671d01 ("btrfs: Prevent from
early transaction abort"), extent buffer allocation and page-cache
insertion have passed __GFP_NOFAIL without checking PF_MEMALLOC.

Because btrfs_init_new_buffer() leaves supports_nowait false,
alloc_extent_buffer() calls btrfs_init_eb_prealloc() with nowait false,
which sets __GFP_NOFAIL when calling alloc_eb_folio_array().
attach_eb_folio_to_filemap() likewise passes __GFP_NOFAIL to
filemap_add_folio().

A PF_MEMALLOC task cannot enter direct reclaim to free pages for itself,
so a __GFP_NOFAIL request under PF_MEMALLOC can loop indefinitely in
__alloc_pages_slowpath() once emergency reserves are exhausted,
triggering the page-allocator warning.

To fix this issue, we can omit __GFP_NOFAIL in btrfs_init_eb_prealloc()
and attach_eb_folio_to_filemap() when PF_MEMALLOC is set, and return
errors other than -EEXIST from filemap_add_folio() so reclaim-context
eviction fails cleanly instead of issuing __GFP_NOFAIL allocations:

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;

The kernel console output, kernel config, and C reproducer are available
on Google Drive:
https://drive.google.com/drive/folders/1eHY4rw7P-2EZz0WdlDfZterVglLdY2bU?usp=sharing

Best regards,
Jiaming Zhang

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-06 12:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-06 12:18 [Linux Kernel Bug] WARNING in alloc_eb_folio_array Jiaming Zhang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®