Re: [PATCH 2/2] mm/swap: add BUG_ON(folio==NULL) to folios_put_refs()

From: Zi Yan
Date: Tue Aug 26 2025 - 21:44:01 EST


On 26 Aug 2025, at 19:16, Max Kellermann wrote:

> It is not legal to have NULL pointers in a folio_batch.
>
> However, the Ceph code does exactly this, and a refactoring patch gone
> wrong has exposed this to folios_put_refs(), see
> https://lore.kernel.org/ceph-devel/aK4v548CId5GIKG1@xxxxxxxxxxxxxx/
>
> I believe this should Oops instead of crashing due to NULL pointer
> reference (guarded by is_huge_zero_folio(), which may silently hide
> the bug).
>
> Signed-off-by: Max Kellermann <max.kellermann@xxxxxxxxx>
> ---
> mm/swap.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/mm/swap.c b/mm/swap.c
> index 3632dd061beb..07ccda00e7ee 100644
> --- a/mm/swap.c
> +++ b/mm/swap.c
> @@ -952,6 +952,7 @@ void folios_put_refs(struct folio_batch *folios, unsigned int *refs)
> for (i = 0, j = 0; i < folios->nr; i++) {
> struct folio *folio = folios->folios[i];
> unsigned int nr_refs = refs ? refs[i] : 1;
> + BUG_ON(folio == NULL);
>
> if (is_huge_zero_folio(folio))
> continue;

We are moving away from BUG_ON. It is better to use WARN_ON_ONCE and skip
NULL values:

if (WARN_ON_ONCE(!folio))
continue;


--
Best Regards,
Yan, Zi