mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] jfs: handle NULL folio->mapping in metapage_write_one() and release_metapage()
@ 2026-11-22 19:57 Arbab Haider
  0 siblings, 0 replies; only message in thread
From: Arbab Haider @ 2026-11-22 19:57 UTC (permalink / raw)
  To: Dave Kleikamp
  Cc: jfs-discussion, linux-kernel, syzbot+d7ffeb5538fe5c793f74,
	stable, Arbab Haider

syzbot reported a NULL pointer dereference in metapage_write_folio()
called from metapage_write_one() during lazy commit teardown:

  KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007]
  RIP: 0010:metapage_write_folio+0xa7/0xcf0 fs/jfs/jfs_metapage.c:428
  Call Trace:
   metapage_write_one+0x2c8/0x520 fs/jfs/jfs_metapage.c:818
   release_metapage+0x5b9/0xab0 fs/jfs/jfs_metapage.c:884
   txUnlock+0x509/0xdc0 fs/jfs/jfs_txnmgr.c:948
   jfs_lazycommit+0x52c/0xa10 fs/jfs/jfs_txnmgr.c:2734

During unmount or inode eviction, truncate_inode_pages_final() can
remove a folio from its address_space (setting folio->mapping to NULL)
while jfsCommit still holds a reference to a dirty metapage on that
folio.  When jfsCommit subsequently runs txUnlock() -> release_metapage():

1. If META_sync is set, metapage_write_one() proceeds with a NULL
   mapping and calls metapage_write_folio(), which dereferences
   folio->mapping->host (and filemap_check_errors(mapping) would also
   dereference NULL).
2. If META_dirty is set without META_sync, folio_mark_dirty() is a
   no-op on an unmapped folio, leaving META_dirty set and the metapage
   stranded on log->synclist; drop_metapage() then skips freeing it and
   jfs_flush_journal() stalls waiting for log->synclist to empty.

Fix both paths under the folio lock:
- In metapage_write_one(), if !mapping, clear META_dirty and call
  remove_from_logsync() on any attached metapages, clear the folio
  dirty state, unlock the folio, and return 0.
- In release_metapage(), if META_dirty is set on an unmapped folio,
  clear META_dirty and META_sync and remove the metapage from logsync
  so drop_metapage() can release it cleanly.

Tested locally in QEMU (x86_64, 4 CPUs, KASAN+UBSAN) using syzbot's C
reproducer with per-iteration mount namespaces: crashed in <1s before
the patch, and completed 56 mount/remount/unmount iterations in 15s
with zero hangs, KASAN, Oops, or synclist warnings after the patch.

Reported-by: syzbot+d7ffeb5538fe5c793f74@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=d7ffeb5538fe5c793f74
Fixes: 35474d52c605 ("jfs: Convert metapage_writepage to metapage_write_folio")
Cc: stable@vger.kernel.org
Signed-off-by: Arbab Haider <arbabhaider649@gmail.com>
---
 fs/jfs/jfs_metapage.c | 34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)

diff --git a/fs/jfs/jfs_metapage.c b/fs/jfs/jfs_metapage.c
index 41fe12e64..9df810c97 100644
--- a/fs/jfs/jfs_metapage.c
+++ b/fs/jfs/jfs_metapage.c
@@ -812,6 +812,23 @@ static int metapage_write_one(struct folio *folio)
 
 	BUG_ON(!folio_test_locked(folio));
 
+	if (unlikely(!mapping)) {
+		struct metapage *mp;
+		unsigned int offset;
+
+		for (offset = 0; offset < PAGE_SIZE; offset += PSIZE) {
+			mp = folio_to_mp(folio, offset);
+			if (!mp)
+				continue;
+			clear_bit(META_dirty, &mp->flag);
+			if (mp->lsn)
+				remove_from_logsync(mp);
+		}
+		folio_clear_dirty_for_io(folio);
+		folio_unlock(folio);
+		return 0;
+	}
+
 	folio_wait_writeback(folio);
 
 	if (folio_clear_dirty_for_io(folio)) {
@@ -879,12 +896,19 @@ void release_metapage(struct metapage * mp)
 	}
 
 	if (test_bit(META_dirty, &mp->flag)) {
-		folio_mark_dirty(folio);
-		if (test_bit(META_sync, &mp->flag)) {
+		if (unlikely(!folio->mapping)) {
+			clear_bit(META_dirty, &mp->flag);
 			clear_bit(META_sync, &mp->flag);
-			if (metapage_write_one(folio))
-				jfs_error(mp->sb, "metapage_write_one() failed\n");
-			folio_lock(folio);
+			if (mp->lsn)
+				remove_from_logsync(mp);
+		} else {
+			folio_mark_dirty(folio);
+			if (test_bit(META_sync, &mp->flag)) {
+				clear_bit(META_sync, &mp->flag);
+				if (metapage_write_one(folio))
+					jfs_error(mp->sb, "metapage_write_one() failed\n");
+				folio_lock(folio);
+			}
 		}
 	} else if (mp->lsn)	/* discard_metapage doesn't remove it */
 		remove_from_logsync(mp);
-- 
2.53.0


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

only message in thread, other threads:[~2026-11-22 19:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-22 19:57 [PATCH] jfs: handle NULL folio->mapping in metapage_write_one() and release_metapage() Arbab Haider

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®