Re: [PATCH] mm/readahead: read folio order limits under the invalidate lock

From: Jan Kara

Date: Fri Nov 27 2026 - 09:42:31 EST


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@xxxxxxxxxxxxxxx # 7.3+
> Signed-off-by: Zhao Gongyi <zhaogongyi@xxxxxxxxxxxxx>

Ups, this is hairy. The fix looks good. Feel free to add:

Reviewed-by: Jan Kara <jack@xxxxxxx>

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@xxxxxxxx>
SUSE Labs, CR