Re: [PATCH v3 05/15] mm, swap: always lock and check the swap cache folio before use

David Hildenbrand
2025-09-12T16:04:25Z

On 10.09.25 18:08, Kairui Song wrote:
> From: Kairui Song <kasong@tencent.com>
>
> Swap cache lookup only increases the reference count of the returned
> folio. That's not enough to ensure a folio is stable in the swap
> cache, so the folio could be removed from the swap cache at any
> time. The caller should always lock and check the folio before using it.
>
> We have just documented this in kerneldoc, now introduce a helper for swap
> cache folio verification with proper sanity checks.
>
> Also, sanitize a few current users to use this convention and the new
> helper for easier debugging. They were not having observable problems
> yet, only trivial issues like wasted CPU cycles on swapoff or
> reclaiming. They would fail in some other way, but it is still better to
> always follow this convention to make things robust and make later
> commits easier to do.
>
> Signed-off-by: Kairui Song <kasong@tencent.com>
> ---

[...]

> index 4baebd8b48f4..f1a4d381d719 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -240,14 +240,12 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si,
> * Offset could point to the middle of a large folio, or folio
> * may no longer point to the expected offset before it's locked.
> */
> - if (offset < swp_offset(folio->swap) ||
> - offset >= swp_offset(folio->swap) + nr_pages) {
> + if (!folio_matches_swap_entry(folio, entry)) {
> folio_unlock(folio);
> folio_put(folio);
> goto again;
> }
> offset = swp_offset(folio->swap);
> -

Nit: unrelated change.

Acked-by: David Hildenbrand <david@redhat.com>

--
Cheers

David / dhildenb