linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: shaurya <ssranevjti@gmail.com>
To: syzbot+4d3cc33ef7a77041efa6@syzkaller.appspotmail.com
Cc: akpm@linux-foundation.org, brauner@kernel.org, hare@suse.de,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-mm@kvack.org, mcgrof@kernel.org,
	syzkaller-bugs@googlegroups.com, willy@infradead.org
Subject: Re: [syzbot] [fs?] [mm?] kernel BUG in __filemap_add_folio
Date: Sun, 30 Nov 2025 20:33:33 +0530	[thread overview]
Message-ID: <ff6e08e2-a7c7-4ebf-8f93-03e5ece9b335@gmail.com> (raw)
In-Reply-To: <680ae31e.050a0220.2c0118.0c72.GAE@google.com>

[-- Attachment #1: Type: text/plain, Size: 83 bytes --]

#syz test:
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master

[-- Attachment #2: 0001-mm-readahead-fix-race-between-page_cache_ra_order-an.patch --]
[-- Type: text/x-patch, Size: 3056 bytes --]

From ec7ea9a1f03f36672cf5acb23761cfef6b948f21 Mon Sep 17 00:00:00 2001
From: Shaurya Rane <ssrane_b23@ee.vjti.ac.in>
Date: Sun, 30 Nov 2025 20:27:25 +0530
Subject: [PATCH] mm/readahead: fix race between page_cache_ra_order and
 set_blocksize

page_cache_ra_order() reads mapping_min_folio_order() before acquiring
the invalidate_lock, creating a time-of-check-time-of-use (TOCTOU) race
with set_blocksize() which can change the mapping's min_folio_order
while holding the invalidate_lock exclusively.

If set_blocksize() increases the mapping's min_folio_order after
page_cache_ra_order() reads the old value but before it adds folios
to the page cache, the VM_BUG_ON check in __filemap_add_folio() will
trigger:

  VM_BUG_ON_FOLIO(folio_order(folio) < mapping_min_folio_order(mapping),
                  folio);

This can happen because the stale min_order is used to calculate
new_order and constrain the folio order, but filemap_add_folio()
re-reads the (now increased) min_folio_order from the mapping.

Fix this by moving the read of mapping_min_folio_order() and the
new_order calculation to after the invalidate_lock is acquired in
shared mode.

Reported-by: syzbot+4d3cc33ef7a77041efa6@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug\?extid\=4d3cc33ef7a77041efa6
Fixes: 47dd67532303 ("block/bdev: lift block size restrictions to 64k")
Cc: stable@vger.kernel.org
Signed-off-by: Shaurya Rane <ssrane_b23@ee.vjti.ac.in>
---
 mm/readahead.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/mm/readahead.c b/mm/readahead.c
index 3a4b5d58eeb6..95718f87bd43 100644
--- a/mm/readahead.c
+++ b/mm/readahead.c
@@ -467,7 +467,7 @@ void page_cache_ra_order(struct readahead_control *ractl,
 	struct address_space *mapping = ractl->mapping;
 	pgoff_t start = readahead_index(ractl);
 	pgoff_t index = start;
-	unsigned int min_order = mapping_min_folio_order(mapping);
+	unsigned int min_order;
 	pgoff_t limit = (i_size_read(mapping->host) - 1) >> PAGE_SHIFT;
 	pgoff_t mark = index + ra->size - ra->async_size;
 	unsigned int nofs;
@@ -483,15 +483,22 @@ void page_cache_ra_order(struct readahead_control *ractl,
 
 	limit = min(limit, index + ra->size - 1);
 
+	/* See comment in page_cache_ra_unbounded() */
+	nofs = memalloc_nofs_save();
+	filemap_invalidate_lock_shared(mapping);
+
+	/*
+	 * Re-read min_order after acquiring the invalidate_lock to avoid a
+	 * race with set_blocksize() which can change the mapping's min_order
+	 * while holding the invalidate_lock exclusively.
+	 */
+	min_order = mapping_min_folio_order(mapping);
 	new_order = min(mapping_max_folio_order(mapping), new_order);
 	new_order = min_t(unsigned int, new_order, ilog2(ra->size));
 	new_order = max(new_order, min_order);
 
 	ra->order = new_order;
 
-	/* See comment in page_cache_ra_unbounded() */
-	nofs = memalloc_nofs_save();
-	filemap_invalidate_lock_shared(mapping);
 	/*
 	 * If the new_order is greater than min_order and index is
 	 * already aligned to new_order, then this will be noop as index
-- 
2.34.1


  reply	other threads:[~2025-11-30 15:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-25  1:19 syzbot
2025-11-30 15:03 ` shaurya [this message]
2025-11-30 15:51   ` syzbot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ff6e08e2-a7c7-4ebf-8f93-03e5ece9b335@gmail.com \
    --to=ssranevjti@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=brauner@kernel.org \
    --cc=hare@suse.de \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mcgrof@kernel.org \
    --cc=syzbot+4d3cc33ef7a77041efa6@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox