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

Chris Li
2025-09-15T15:07:51Z

On Wed, Sep 10, 2025 at 9:09 AM Kairui Song <ryncsn@gmail.com> wrote:
> @@ -2004,6 +2002,13 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
> bool hwpoisoned = false;
> int ret = 1;
>
> + /*
> + * If the folio is removed from swap cache by others, continue to
> + * unuse other PTEs. try_to_unuse may try again if we missed this one.
> + */

It took me a while to figure out why we add a
folio_matches_swap_entry() check here but we don't have an existing
check for folio swap entry matching in this function. Can you confirm
that if a race has happened causing the folio swap entry mismatch,
then try_to_usue() MUST try again rather than "may" try again. It
seems to me that it is a MUST rather than "may". I am curious in what
condition the mismatch happens and the try_to_unuse() does not need to
try again.

BTW, this function has three types of return value, 1, 0, and negative
for error. The 0 and 1 are ignored by the caller, the caller only
cares about the negative value. Overall this unuse_pte() and
try_to_unuse() walk feels complicated and maybe a candidate for later
clean up. That is not your patch's fault. I am not requesting a
cleanup in this series.

With that Nitpick,

Acked-by: Chris Li <chrisl@kernel.org>

Chris

> + if (!folio_matches_swap_entry(folio, entry))
> + return 0;
> +
> swapcache = folio;
> folio = ksm_might_need_to_copy(folio, vma, addr);
> if (unlikely(!folio))
> --
> 2.51.0
>