Linux EXT4 FS development
 help / color / mirror / Atom feed
* [BUG] ext4: NULL deref in ext4_mb_load_buddy_gfp() on 6.1.y
@ 2026-11-16  9:30 Ivan Tiukov
  2026-11-16  9:36 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Ivan Tiukov @ 2026-11-16  9:30 UTC (permalink / raw)
  To: linux-ext4; +Cc: stable, tytso, jack, adilger, dan.carpenter, willy

Hello,

Linux 6.1.y panics in ext4 under heavy unlink/truncate load. I believe this
is a stable-only regression from the mballoc folio conversion in 6.1.191:
the converted code assumes __filemap_get_folio() returns an ERR_PTR when it
cannot hand out a folio, but 6.1.y still returns a plain NULL, so the NULL
check that IS_ERR() is supposed to provide is not there.

The crash reproduced three times on one host within eight hours; the trace
below is from the third one, captured over netconsole because the machine
went down before journald could persist anything.


= Crash
--------

Kernel: 6.1.192-1 (Debian 6.1.0-55-amd64), x86_64, 4 vCPU KVM guest, 8 GB.
Process: rpmsign, doing unlink() of large RPM files on ext4 root.

  BUG: kernel NULL pointer dereference, address: 0000000000000000
  #PF: supervisor read access in kernel mode
  #PF: error_code(0x0000) - not-present page
  PGD 0 P4D 0
  Oops: 0000 [#1] PREEMPT SMP NOPTI
  CPU: 3 PID: 12736 Comm: rpmsign Not tainted 6.1.0-55-amd64 #1 Debian 6.1.192-1
  Hardware name: Red Hat KVM, BIOS 1.16.0-4.module_el8.9.0+3659+9c8643f3 04/01/2014
  RIP: 0010:ext4_mb_load_buddy_gfp+0xbc/0x380 [ext4]
  Code: 89 44 24 08 f7 3c 24 48 63 d8 89 54 24 0c ba 01 00 00 00 48 89 de
        e8 d3 6e ff f2 49 89 c7 48 3d 00 f0 ff ff 0f 87 28 01 00 00 <48>
        8b 00 a8 04 0f 84 12 01 00 00 49 8b 07 a8 01 0f 85 07 01 00 00
  RSP: 0018:ffffd065089bfae8 EFLAGS: 00010207
  RAX: 0000000000000000 RBX: 000000000000004c RCX: 0000000000000003
  RDX: 000000000000000c RSI: ffff8ecec175f908 RDI: ffff8ecdd505e000
  RBP: ffff8ecee0983000 R08: ffff8ecec1582f70 R09: 0000000000000000
  R10: 0000000000000000 R11: ffff8ece2a13eff0 R12: ffffd065089bfb80
  R13: 0000000000008c40 R14: ffff8ecec0420a78 R15: 0000000000000000
  CR2: 0000000000000000 CR3: 00000000150b2000 CR4: 0000000000750ee0
  Call Trace:
   <TASK>
   ext4_free_blocks+0x410/0xbb0 [ext4]
   ext4_ext_remove_space+0x7ff/0x15d0 [ext4]
   ext4_ext_truncate+0xb4/0xd0 [ext4]
   ext4_truncate+0x314/0x490 [ext4]
   ? __ext4_journal_start_sb+0x105/0x120 [ext4]
   ext4_evict_inode+0x2f8/0x710 [ext4]
   evict+0x112/0x2c0
   do_unlinkat+0x173/0x320
   __x64_sys_unlink+0x3e/0x70
   do_syscall_64+0x5d/0xb0
   entry_SYSCALL_64_after_hwframe+0x6e/0xd8
   </TASK>
  Kernel panic - not syncing: Fatal exception


= Analysis
----------

The faulting instruction is in the fast path of ext4_mb_load_buddy_gfp():

  folio = __filemap_get_folio(inode->i_mapping, pnum, FGP_ACCESSED, 0);
  if (IS_ERR(folio) || !folio_test_uptodate(folio) || folio_test_locked(folio)) {

No FGP_CREAT is passed, so when the folio is not in the page cache
__filemap_get_folio() reaches no_page: and, on 6.1.y, simply does
"return folio;" with folio == NULL.

  IS_ERR(NULL) is false, so the guard falls through to
  folio_test_locked(folio), which dereferences NULL.

RAX == 0 at the fault and CR2 == 0 confirm the returned value was NULL. I
also disassembled the shipped ext4.ko to make sure this is the code being
executed:

  315c8: call  __filemap_get_folio
  315cd: mov   %rax,%r15
  315d0: cmp   $0xfffffffffffff000,%rax    # IS_ERR() only
  315d6: ja    <slow path>
  315dc: mov   (%rax),%rax                 # +0xbc, faults with RAX == NULL

There is no NULL test anywhere between the call and the dereference.

Commit a8d83471c560 ("ext4: fix error pointer dereference in
ext4_mb_load_buddy_gfp()") added the IS_ERR() checks. That is correct for
mainline, where __filemap_get_folio() returns ERR_PTR(-ENOENT), but it does
not cover 6.1.y, where the function returns NULL for the same situation.


= Why this is 6.1.y only
-------------------------

__filemap_get_folio() changed its no-folio convention in v6.4:

  66dabbb65d67  mm: return an ERR_PTR from __filemap_get_folio
  38a55db9877c  filemap: Handle error return from __filemap_get_folio()

That change is a semantics change for every caller, so it was never a
stable candidate and 6.1.y does not have it. What 6.1.y did get, in
6.1.191, is the ext4 side of the folio conversion (862483175d3d, d34e4fd4e73f
and the a8d83471c560 follow-up), which was written against the post-6.4
semantics.

I checked the tree heads:

  branch         ext4 guard               __filemap_get_folio   result
  linux-6.6.y    IS_ERR(folio) || ...     ERR_PTR(-ENOENT)      ok
  linux-6.12.y   IS_ERR(folio) || ...     ERR_PTR(-ENOENT)      ok
  linux-6.1.y    IS_ERR(folio) || ...     NULL                  NULL deref

So 6.1.y is the only affected stable branch, and it stays broken in 6.1.192
and in linux-6.1.y HEAD. 5.15.y still uses find_get_page_flags() here and is
not affected.


= Minor: a second latent site in the same file
-----------------------------------------------

ext4_mb_get_buddy_page_lock() has the same assumption:

  folio = __filemap_get_folio(inode->i_mapping, pnum,
                  FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp);
  if (IS_ERR(folio))
          return PTR_ERR(folio);
  BUG_ON(folio->mapping != inode->i_mapping);

Here NULL does come back from FGP_CREAT when the allocation fails, and
PTR_ERR(NULL) is 0, so the function reports success and then dereferences
NULL in BUG_ON(). Same root cause, different symptom. I folded it into the
patch below.

No other file under fs/ext4/ calls __filemap_get_folio() in 6.1.192, so
mballoc.c is the whole surface.


= Proposed fix (against 6.1.192)
---------------------------------

Use IS_ERR_OR_NULL() and translate NULL into -ENOMEM. This keeps working
unchanged if 6.1.y ever picks up the ERR_PTR() semantics, and it is a no-op
for mainline.

--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -1375,8 +1375,8 @@ static int ext4_mb_get_buddy_page_lock(s
 	poff = block % blocks_per_page;
 	folio = __filemap_get_folio(inode->i_mapping, pnum,
 			FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp);
-	if (IS_ERR(folio))
-		return PTR_ERR(folio);
+	if (IS_ERR_OR_NULL(folio))
+		return folio ? PTR_ERR(folio) : -ENOMEM;
 	BUG_ON(folio->mapping != inode->i_mapping);
 	e4b->bd_bitmap_folio = folio;
 	e4b->bd_bitmap = folio_address(folio) + (poff * sb->s_blocksize);
@@ -1389,8 +1389,8 @@ static int ext4_mb_get_buddy_page_lock(s
 	/* blocks_per_page == 1, hence we need another page for the buddy */
 	folio = __filemap_get_folio(inode->i_mapping, block + 1,
 			FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp);
-	if (IS_ERR(folio))
-		return PTR_ERR(folio);
+	if (IS_ERR_OR_NULL(folio))
+		return folio ? PTR_ERR(folio) : -ENOMEM;
 	BUG_ON(folio->mapping != inode->i_mapping);
 	e4b->bd_buddy_folio = folio;
 	return 0;
@@ -1533,7 +1533,8 @@ ext4_mb_load_buddy_gfp(struct super_bloc
 
 	/* Avoid locking the folio in the fast path ... */
 	folio = __filemap_get_folio(inode->i_mapping, pnum, FGP_ACCESSED, 0);
-	if (IS_ERR(folio) || !folio_test_uptodate(folio) || folio_test_locked(folio)) {
+	if (IS_ERR_OR_NULL(folio) || !folio_test_uptodate(folio) ||
+	    folio_test_locked(folio)) {
 		/*
 		 * folio_test_locked is employed to detect ongoing folio
 		 * migrations, since concurrent migrations can lead to
@@ -1543,11 +1544,11 @@ ext4_mb_load_buddy_gfp(struct super_bloc
 		 * try to get the folio with lock in both cases to avoid
 		 * concurrency.
 		 */
-		if (!IS_ERR(folio))
+		if (!IS_ERR_OR_NULL(folio))
 			folio_put(folio);
 		folio = __filemap_get_folio(inode->i_mapping, pnum,
 				FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp);
-		if (!IS_ERR(folio)) {
+		if (!IS_ERR_OR_NULL(folio)) {
 			if (WARN_RATELIMIT(folio->mapping != inode->i_mapping,
 	"ext4: bitmap's mapping != inode->i_mapping\n")) {
 				/* should never happen */
@@ -1567,8 +1568,8 @@ ext4_mb_load_buddy_gfp(struct super_bloc
 			folio_unlock(folio);
 		}
 	}
-	if (IS_ERR(folio)) {
-		ret = PTR_ERR(folio);
+	if (IS_ERR_OR_NULL(folio)) {
+		ret = folio ? PTR_ERR(folio) : -ENOMEM;
 		goto err;
 	}
 	if (!folio_test_uptodate(folio)) {
@@ -1585,12 +1586,13 @@ ext4_mb_load_buddy_gfp(struct super_bloc
 	poff = block % blocks_per_page;
 
 	folio = __filemap_get_folio(inode->i_mapping, pnum, FGP_ACCESSED, 0);
-	if (IS_ERR(folio) || !folio_test_uptodate(folio) || folio_test_locked(folio)) {
-		if (!IS_ERR(folio))
+	if (IS_ERR_OR_NULL(folio) || !folio_test_uptodate(folio) ||
+	    folio_test_locked(folio)) {
+		if (!IS_ERR_OR_NULL(folio))
 			folio_put(folio);
 		folio = __filemap_get_folio(inode->i_mapping, pnum,
 				FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp);
-		if (!IS_ERR(folio)) {
+		if (!IS_ERR_OR_NULL(folio)) {
 			if (WARN_RATELIMIT(folio->mapping != inode->i_mapping,
 	"ext4: buddy bitmap's mapping != inode->i_mapping\n")) {
 				/* should never happen */
@@ -1609,8 +1611,8 @@ ext4_mb_load_buddy_gfp(struct super_bloc
 			folio_unlock(folio);
 		}
 	}
-	if (IS_ERR(folio)) {
-		ret = PTR_ERR(folio);
+	if (IS_ERR_OR_NULL(folio)) {
+		ret = folio ? PTR_ERR(folio) : -ENOMEM;
 		goto err;
 	}
 	if (!folio_test_uptodate(folio)) {


= Trigger and testing
----------------------

The window is narrow but not exotic: the block group must be initialised
while its buddy folio has been evicted from the page cache, and then any
free/alloc in that group takes the fast path with a cold folio. On this host
it happened under rm -rf of a large tree and under rpm --addsign of several
hundred MB of RPMs, both with the page cache under pressure.

I have not boot-tested a 6.1.y build with the patch applied. The diagnosis
itself is confirmed on the affected kernel: the oops, the NULL in RAX/CR2,
and the disassembly of the shipped module. I am happy to build and run a
patched 6.1.y if that helps; the host is available for retesting.

Workaround for anyone hitting this: stay on 6.1.190 or earlier until 6.1.y
carries a fix, or run 6.6.y/6.12.y.

-- 
Ivan Tiukov (www.tiukov.com)

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-16  9:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16  9:30 [BUG] ext4: NULL deref in ext4_mb_load_buddy_gfp() on 6.1.y Ivan Tiukov
2026-11-16  9:36 ` sashiko-bot

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