From: Baolin Wang <baolin.wang@linux.alibaba.com>
To: Kairui Song <ryncsn@gmail.com>
Cc: linux-mm@kvack.org, Andrew Morton <akpm@linux-foundation.org>,
Matthew Wilcox <willy@infradead.org>,
Hugh Dickins <hughd@google.com>, Chris Li <chrisl@kernel.org>,
Barry Song <baohua@kernel.org>, Baoquan He <bhe@redhat.com>,
Nhat Pham <nphamcs@gmail.com>,
Kemeng Shi <shikemeng@huaweicloud.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 09/15] mm/shmem, swap: remove redundant error handling for replacing folio
Date: Sat, 13 Sep 2025 11:26:22 +0800 [thread overview]
Message-ID: <59ddf3bc-cfc0-40ab-ae2d-859724ef6168@linux.alibaba.com> (raw)
In-Reply-To: <CAMgjq7Bc6r2=BcAPCeVPcVJ_hP8bXTs_pya2fWg8ZL-vTG9SAg@mail.gmail.com>
On 2025/9/12 20:36, Kairui Song wrote:
> On Fri, Sep 12, 2025 at 4:22 PM Baolin Wang
> <baolin.wang@linux.alibaba.com> wrote:
>> On 2025/9/11 00:08, Kairui Song wrote:
>>> From: Kairui Song <kasong@tencent.com>
>>>
>>> Shmem may replace a folio in the swap cache if the cached one doesn't
>>> fit the swapin's GFP zone. When doing so, shmem has already double
>>> checked that the swap cache folio is locked, still has the swap cache
>>> flag set, and contains the wanted swap entry. So it is impossible to
>>> fail due to an XArray mismatch. There is even a comment for that.
>>>
>>> Delete the defensive error handling path, and add a WARN_ON instead:
>>> if that happened, something has broken the basic principle of how the
>>> swap cache works, we should catch and fix that.
>>>
>>> Signed-off-by: Kairui Song <kasong@tencent.com>
>>> Reviewed-by: David Hildenbrand <david@redhat.com>
>>> ---
>>> mm/shmem.c | 42 ++++++++++++------------------------------
>>> 1 file changed, 12 insertions(+), 30 deletions(-)
>>>
>>> diff --git a/mm/shmem.c b/mm/shmem.c
>>> index 410f27bc4752..5f395fab489c 100644
>>> --- a/mm/shmem.c
>>> +++ b/mm/shmem.c
>>> @@ -1661,13 +1661,13 @@ int shmem_writeout(struct folio *folio, struct swap_iocb **plug,
>>> }
>>>
>>> /*
>>> - * The delete_from_swap_cache() below could be left for
>>> + * The swap_cache_del_folio() below could be left for
>>> * shrink_folio_list()'s folio_free_swap() to dispose of;
>>> * but I'm a little nervous about letting this folio out of
>>> * shmem_writeout() in a hybrid half-tmpfs-half-swap state
>>> * e.g. folio_mapping(folio) might give an unexpected answer.
>>> */
>>> - delete_from_swap_cache(folio);
>>> + swap_cache_del_folio(folio);
>>> goto redirty;
>>> }
>>> if (nr_pages > 1)
>>> @@ -2045,7 +2045,7 @@ static struct folio *shmem_swap_alloc_folio(struct inode *inode,
>>> new->swap = entry;
>>>
>>> memcg1_swapin(entry, nr_pages);
>>> - shadow = get_shadow_from_swap_cache(entry);
>>> + shadow = swap_cache_get_shadow(entry);
>>
>> Again, there are still some issues with the patch split. The swapcache
>> related APIs replacement should be placed in Patch 8, otherwise there
>> will be buidling errors after applying Patch 8.
>>
>> With this issue fixed:
>> Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
>>
>
> Hi Baolin
>
> Yeah you are right, I need to move these few changes to patch 8.
>
> BTW I also found that the WARN_ON and irq unlock needs following fix,
> the stats update need to be done with irq disabled:
Yes, indeed, I overlooked this.
> diff --git a/mm/shmem.c b/mm/shmem.c
> index 957e40caba6e..c4d491c93506 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
> @@ -2121,14 +2121,14 @@ static int shmem_replace_folio(struct folio
> **foliop, gfp_t gfp,
> /* Swap cache still stores N entries instead of a high-order entry */
> xa_lock_irq(&swap_mapping->i_pages);
> for (i = 0; i < nr_pages; i++) {
> - WARN_ON_ONCE(xas_store(&xas, new));
> + WARN_ON_ONCE(xas_store(&xas, new) != old);
> xas_next(&xas);
> }
> - xa_unlock_irq(&swap_mapping->i_pages);
>
> mem_cgroup_replace_folio(old, new);
> shmem_update_stats(new, nr_pages);
> shmem_update_stats(old, -nr_pages);
> + xa_unlock_irq(&swap_mapping->i_pages);
>
> folio_add_lru(new);
> *foliop = new;
next prev parent reply other threads:[~2025-09-13 3:26 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
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 [this message]
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=59ddf3bc-cfc0-40ab-ae2d-859724ef6168@linux.alibaba.com \
--to=baolin.wang@linux.alibaba.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--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=ryncsn@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