* [PATCH] mm/readahead: read folio order limits under the invalidate lock
@ 2026-11-27 7:23 Zhao Gongyi
2026-11-27 14:41 ` Jan Kara
2026-11-29 3:22 ` + mm-readahead-read-folio-order-limits-under-the-invalidate-lock.patch added to mm-new branch Andrew Morton
0 siblings, 2 replies; 3+ messages in thread
From: Zhao Gongyi @ 2026-11-27 7:23 UTC (permalink / raw)
To: Matthew Wilcox, Jan Kara, Andrew Morton
Cc: Jens Axboe, Jinchao Wang, linux-mm, linux-fsdevel, linux-block,
linux-kernel, stable
Block device mmap readahead can race with set_blocksize(), which updates
the block size and minimum folio order under the exclusive invalidate
lock. page_cache_ra_order() reads the minimum before taking the shared
lock and can allocate a folio smaller than the new block size. On the
buffer-head path this can cause a NULL dereference in guard_bio_eod().
Take the existing shared invalidate lock before computing the folio order
limits so they stay valid through allocation and I/O submission.
Fixes: 47dd67532303 ("block/bdev: lift block size restrictions to 64k")
Cc: stable@vger.kernel.org # 7.3+
Signed-off-by: Zhao Gongyi <zhaogongyi@bytedance.com>
---
Earlier proposals:
https://lkml.iu.edu/hypermail/linux/kernel/2511.3/04090.html
https://lore.kernel.org/r/aUN9BFdxHQj8ThMS@ndev
The earlier syzbot retest hit a bug in the fallback path. Since
b244c89a70b3, the caller holds invalidate_lock before the fallback reads
min_nrpages. This patch covers the remaining mmap readahead path.
A/B testing on arm64/QEMU at the declared base commit gave 10/10
baseline hits via madvise and 6/10 via MAP_POPULATE. Both paths had
0/10 hits with this patch. The 20 patched serial logs showed no kernel
warnings or crashes.
mm/readahead.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/mm/readahead.c b/mm/readahead.c
index 6e5563290..80f3e141d 100644
--- a/mm/readahead.c
+++ b/mm/readahead.c
@@ -484,7 +484,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;
unsigned int nofs;
@@ -506,15 +506,17 @@ void page_cache_ra_order(struct readahead_control *ractl,
mark = ULONG_MAX;
}
+ /* See comment in page_cache_ra_unbounded() */
+ nofs = memalloc_nofs_save();
+ filemap_invalidate_lock_shared(mapping);
+
+ /* Serialize with set_blocksize() updating the minimum folio order. */
+ 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
base-commit: 29378f5cd79a27718ead9717fe7b094b651a8e1e
--
2.39.5 (Apple Git-154)
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] mm/readahead: read folio order limits under the invalidate lock
2026-11-27 7:23 [PATCH] mm/readahead: read folio order limits under the invalidate lock Zhao Gongyi
@ 2026-11-27 14:41 ` Jan Kara
2026-11-29 3:22 ` + mm-readahead-read-folio-order-limits-under-the-invalidate-lock.patch added to mm-new branch Andrew Morton
1 sibling, 0 replies; 3+ messages in thread
From: Jan Kara @ 2026-11-27 14:41 UTC (permalink / raw)
To: Zhao Gongyi
Cc: Matthew Wilcox, Jan Kara, Andrew Morton, Jens Axboe, Jinchao Wang,
linux-mm, linux-fsdevel, linux-block, linux-kernel, stable
On Fri 27-11-26 15:23:40, Zhao Gongyi wrote:
> Block device mmap readahead can race with set_blocksize(), which updates
> the block size and minimum folio order under the exclusive invalidate
> lock. page_cache_ra_order() reads the minimum before taking the shared
> lock and can allocate a folio smaller than the new block size. On the
> buffer-head path this can cause a NULL dereference in guard_bio_eod().
>
> Take the existing shared invalidate lock before computing the folio order
> limits so they stay valid through allocation and I/O submission.
>
> Fixes: 47dd67532303 ("block/bdev: lift block size restrictions to 64k")
> Cc: stable@vger.kernel.org # 7.3+
> Signed-off-by: Zhao Gongyi <zhaogongyi@bytedance.com>
Ups, this is hairy. The fix looks good. Feel free to add:
Reviewed-by: Jan Kara <jack@suse.cz>
Honza
> ---
> Earlier proposals:
> https://lkml.iu.edu/hypermail/linux/kernel/2511.3/04090.html
> https://lore.kernel.org/r/aUN9BFdxHQj8ThMS@ndev
>
> The earlier syzbot retest hit a bug in the fallback path. Since
> b244c89a70b3, the caller holds invalidate_lock before the fallback reads
> min_nrpages. This patch covers the remaining mmap readahead path.
>
> A/B testing on arm64/QEMU at the declared base commit gave 10/10
> baseline hits via madvise and 6/10 via MAP_POPULATE. Both paths had
> 0/10 hits with this patch. The 20 patched serial logs showed no kernel
> warnings or crashes.
>
> mm/readahead.c | 12 +++++++-----
> 1 file changed, 7 insertions(+), 5 deletions(-)
>
> diff --git a/mm/readahead.c b/mm/readahead.c
> index 6e5563290..80f3e141d 100644
> --- a/mm/readahead.c
> +++ b/mm/readahead.c
> @@ -484,7 +484,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;
> unsigned int nofs;
> @@ -506,15 +506,17 @@ void page_cache_ra_order(struct readahead_control *ractl,
> mark = ULONG_MAX;
> }
>
> + /* See comment in page_cache_ra_unbounded() */
> + nofs = memalloc_nofs_save();
> + filemap_invalidate_lock_shared(mapping);
> +
> + /* Serialize with set_blocksize() updating the minimum folio order. */
> + 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
>
> base-commit: 29378f5cd79a27718ead9717fe7b094b651a8e1e
> --
> 2.39.5 (Apple Git-154)
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 3+ messages in thread
* + mm-readahead-read-folio-order-limits-under-the-invalidate-lock.patch added to mm-new branch
2026-11-27 7:23 [PATCH] mm/readahead: read folio order limits under the invalidate lock Zhao Gongyi
2026-11-27 14:41 ` Jan Kara
@ 2026-11-29 3:22 ` Andrew Morton
1 sibling, 0 replies; 3+ messages in thread
From: Andrew Morton @ 2026-11-29 3:22 UTC (permalink / raw)
To: mm-commits, willy, wangjinchao600, stable, jack, axboe,
zhaogongyi, akpm
The patch titled
Subject: mm/readahead: read folio order limits under the invalidate lock
has been added to the -mm mm-new branch. Its filename is
mm-readahead-read-folio-order-limits-under-the-invalidate-lock.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-readahead-read-folio-order-limits-under-the-invalidate-lock.patch
This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Note, mm-new is a provisional staging ground for work-in-progress
patches, and acceptance into mm-new is a notification for others take
notice and to finish up reviews. Please do not hesitate to respond to
review feedback and post updated versions to replace or incrementally
fixup patches in mm-new.
The mm-new branch of mm.git is not included in linux-next
If a few days of testing in mm-new is successful, the patch will me moved
into mm.git's mm-unstable branch, which is included in linux-next
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a
reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Zhao Gongyi <zhaogongyi@bytedance.com>
Subject: mm/readahead: read folio order limits under the invalidate lock
Date: Fri, 27 Nov 2026 15:23:40 +0800
Block device mmap readahead can race with set_blocksize(), which updates
the block size and minimum folio order under the exclusive invalidate
lock. page_cache_ra_order() reads the minimum before taking the shared
lock and can allocate a folio smaller than the new block size. On the
buffer-head path this can cause a NULL dereference in guard_bio_eod().
Take the existing shared invalidate lock before computing the folio order
limits so they stay valid through allocation and I/O submission.
Link: https://patch.msgid.link/20261127072340.73758-1-zhaogongyi@bytedance.com
Fixes: 47dd67532303 ("block/bdev: lift block size restrictions to 64k")
Signed-off-by: Zhao Gongyi <zhaogongyi@bytedance.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Jan Kara <jack@suse.cz>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Jinchao Wang <wangjinchao600@gmail.com>
Cc: <stable@vger.kernel.org> # 7.3+
mm/readahead.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
--- a/mm/readahead.c~mm-readahead-read-folio-order-limits-under-the-invalidate-lock
+++ a/mm/readahead.c
@@ -484,7 +484,7 @@ void page_cache_ra_order(struct readahea
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;
unsigned int nofs;
@@ -506,15 +506,17 @@ void page_cache_ra_order(struct readahea
mark = ULONG_MAX;
}
+ /* See comment in page_cache_ra_unbounded() */
+ nofs = memalloc_nofs_save();
+ filemap_invalidate_lock_shared(mapping);
+
+ /* Serialize with set_blocksize() updating the minimum folio order. */
+ 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
_
Patches currently in -mm which might be from zhaogongyi@bytedance.com are
mm-readahead-read-folio-order-limits-under-the-invalidate-lock.patch
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-29 3:22 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 7:23 [PATCH] mm/readahead: read folio order limits under the invalidate lock Zhao Gongyi
2026-11-27 14:41 ` Jan Kara
2026-11-29 3:22 ` + mm-readahead-read-folio-order-limits-under-the-invalidate-lock.patch added to mm-new branch Andrew Morton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox