linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Kairui Song <ryncsn@gmail.com>
To: Chris Li <chrisl@kernel.org>
Cc: linux-mm@kvack.org, Andrew Morton <akpm@linux-foundation.org>,
	 Matthew Wilcox <willy@infradead.org>,
	Hugh Dickins <hughd@google.com>, Barry Song <baohua@kernel.org>,
	 Baoquan He <bhe@redhat.com>, Nhat Pham <nphamcs@gmail.com>,
	 Kemeng Shi <shikemeng@huaweicloud.com>,
	Baolin Wang <baolin.wang@linux.alibaba.com>,
	 Ying Huang <ying.huang@linux.alibaba.com>,
	Johannes Weiner <hannes@cmpxchg.org>,
	 David Hildenbrand <david@redhat.com>,
	Yosry Ahmed <yosryahmed@google.com>,
	 Lorenzo Stoakes <lorenzo.stoakes@oracle.com>,
	Zi Yan <ziy@nvidia.com>,
	 linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 05/15] mm, swap: always lock and check the swap cache folio before use
Date: Tue, 16 Sep 2025 00:34:55 +0800	[thread overview]
Message-ID: <CAMgjq7Cddet5BQorsc5gaLbNKJrZhCwi5qifpHz=sd6gb9dQ8g@mail.gmail.com> (raw)
In-Reply-To: <CACePvbX4juhR5jry0Bi202qo=nfFVZkztHzo8KxKMW_yKBseyA@mail.gmail.com>

On Mon, Sep 15, 2025 at 11:07 PM Chris Li <chrisl@kernel.org> wrote:
>
> 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.

It depends, I think it may, because: for example here we are unusing
folio A with entry S1, and a raced process just swapin folio A then
remove it from swap cache. If the raced process didn't swapout the PTE
again, then there is no need to retry as we are dong with this PTE.

There are many races, someone else swapped out folio A again using S2.
Then here we will see a !folio_matches_swap_entry. And that's OK.

But there have been many other subtle race conditions in other places,
e.g. another folio occupied the same PTE and got swapped out using S1,
causing PTE == S1 and here got a wrong folio A installed. This isn't
happening here, because we have removed the !SWP_WRITEOK flag from the
si during swapoff...

It's really complex and fragile, so just make it easier, check
folio_matches_swap_entry and abort early, is safer and follows the
convention.

> 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.

Right, totally agree, we can cleanup the swapoff part later.

> With that Nitpick,
>
> Acked-by: Chris Li <chrisl@kernel.org>

Thanks!


  reply	other threads:[~2025-09-15 16:35 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-10 16:08 [PATCH v3 00/15] mm, swap: introduce swap table as swap cache (phase I) Kairui Song
2025-09-10 16:08 ` [PATCH v3 01/15] docs/mm: add document for swap table Kairui Song
2025-09-10 16:13   ` Kairui Song
2025-09-10 16:37     ` Chris Li
2025-09-10 16:08 ` [PATCH v3 02/15] mm, swap: use unified helper for swap cache look up Kairui Song
2025-09-10 16:08 ` [PATCH v3 03/15] mm, swap: fix swap cache index error when retrying reclaim Kairui Song
2025-09-10 16:08 ` [PATCH v3 04/15] mm, swap: check page poison flag after locking it Kairui Song
2025-09-12 16:01   ` David Hildenbrand
2025-09-12 17:17   ` Nhat Pham
2025-09-10 16:08 ` [PATCH v3 05/15] mm, swap: always lock and check the swap cache folio before use Kairui Song
2025-09-12 16:04   ` David Hildenbrand
2025-09-15 15:07   ` Chris Li
2025-09-15 16:34     ` Kairui Song [this message]
2025-09-15 23:09   ` Nhat Pham
2025-09-10 16:08 ` [PATCH v3 06/15] mm, swap: rename and move some swap cluster definition and helpers Kairui Song
2025-09-10 16:08 ` [PATCH v3 07/15] mm, swap: tidy up swap device and cluster info helpers Kairui Song
2025-09-10 16:08 ` [PATCH v3 08/15] mm, swap: cleanup swap cache API and add kerneldoc Kairui Song
2025-09-10 16:08 ` [PATCH v3 09/15] mm/shmem, swap: remove redundant error handling for replacing folio Kairui Song
2025-09-12  8:22   ` Baolin Wang
2025-09-12 12:36     ` Kairui Song
2025-09-13  3:26       ` Baolin Wang
2025-09-15 15:13       ` Chris Li
2025-09-10 16:08 ` [PATCH v3 10/15] mm, swap: wrap swap cache replacement with a helper Kairui Song
2025-09-12 16:06   ` David Hildenbrand
2025-09-15 15:09   ` Chris Li
2025-09-10 16:08 ` [PATCH v3 11/15] mm, swap: use the swap table for the swap cache and switch API Kairui Song
2025-09-11  2:27   ` Lance Yang
2025-09-11  2:33     ` Lance Yang
2025-09-11  2:48       ` Kairui Song
2025-09-11  2:54         ` Lance Yang
2025-09-12  9:30           ` Lorenzo Stoakes
2025-09-12  9:42             ` Kairui Song
2025-09-12  9:47               ` Lorenzo Stoakes
2025-09-12  9:21     ` Lorenzo Stoakes
2025-09-10 16:08 ` [PATCH v3 12/15] mm, swap: mark swap address space ro and add context debug check Kairui Song
2025-09-10 16:08 ` [PATCH v3 13/15] mm, swap: remove contention workaround for swap cache Kairui Song
2025-09-10 16:08 ` [PATCH v3 14/15] mm, swap: implement dynamic allocation of swap table Kairui Song
2025-09-15 15:05   ` Chris Mason
2025-09-15 16:24     ` Kairui Song
2025-09-15 16:54       ` Chris Mason
2025-09-15 17:14         ` Chris Li
2025-09-15 18:03           ` Chris Li
2025-09-10 16:08 ` [PATCH v3 15/15] mm, swap: use a single page for swap table when the size fits Kairui Song
2025-09-10 22:40 ` [PATCH v3 00/15] mm, swap: introduce swap table as swap cache (phase I) Andrew Morton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAMgjq7Cddet5BQorsc5gaLbNKJrZhCwi5qifpHz=sd6gb9dQ8g@mail.gmail.com' \
    --to=ryncsn@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=bhe@redhat.com \
    --cc=chrisl@kernel.org \
    --cc=david@redhat.com \
    --cc=hannes@cmpxchg.org \
    --cc=hughd@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=nphamcs@gmail.com \
    --cc=shikemeng@huaweicloud.com \
    --cc=willy@infradead.org \
    --cc=ying.huang@linux.alibaba.com \
    --cc=yosryahmed@google.com \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox