From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Alexandre Ghiti <alexghiti@meta.com>
Cc: Alexandre Ghiti <alex@ghiti.fr>,
Andrew Morton <akpm@linux-foundation.org>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
Barry Song <baohua@kernel.org>, Kairui Song <kasong@tencent.com>,
Chris Li <chrisl@kernel.org>, Nhat Pham <nphamcs@gmail.com>,
Johannes Weiner <hannes@cmpxchg.org>,
Yosry Ahmed <yosry@kernel.org>,
Chengming Zhou <chengming.zhou@linux.dev>,
Baoquan He <baoquan.he@linux.dev>,
Kemeng Shi <shikemeng@huaweicloud.com>,
Youngjun Park <youngjun.park@lge.com>,
"Liam R . Howlett" <liam@infradead.org>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Michal Hocko <mhocko@suse.com>
Subject: Re: [PATCH] mm: free the swap slot of a dirty swapin folio
Date: Thu, 26 Nov 2026 17:20:23 +0100 [thread overview]
Message-ID: <0b7e81a8-d291-4cbf-8da0-e65e9a0b40a0@kernel.org> (raw)
In-Reply-To: <CAEmasaXWwWeuQnMbA+g8OyvNwe721Dro+1h+5pfTpzLuV6dx3g@mail.gmail.com>
On 11/26/26 16:01, Alexandre Ghiti wrote:
> Hi David,
>
> On Thu, Nov 26, 2026 at 1:54 PM David Hildenbrand (Arm)
> <david@kernel.org> wrote:
>>
>>>
>> On 11/26/26 10:42, Alexandre Ghiti wrote:
>>> do_swap_page() frees the swap cache of an exclusive folio on a write
>>> fault only. A folio loaded from zswap is dirty: zswap drops its copy on
>>> load, so the slot no longer holds the data and only reserves swap space
>>> until the folio is reclaimed again or its owner exits.
>>>
>>> Such folios add up, and they are expensive to free at exit: on a large
>>> production workload, the exit time doubled when zswap was enabled. A
>>> third of the increase comes from the folios left in the swap cache,
>>> each costing about 3x a resident page to tear down; the rest comes from
>>> freeing the zswap entries.
>>>
>>> So free the swap cache of an exclusive dirty folio as well, so that the
>>> slot is released at swapin, as is already done on an exclusive write
>>> fault. The folio is dirty, so it would get written out again anyway:
>>> the only cost is a new slot allocation on its next reclaim, which write
>>> faults already pay.
>>>
>>> On the production workload, this patch removed that third: the exit time
>>> decreased by about 15%. On a simple microbenchmark, 4 GiB of anonymous
>>> memory in a memcg pushed to swap, read back, then SIGKILLed in a QEMU
>>> guest (mean +- stddev of 9 runs):
>>>
>>> base patched
>>> exit time 989 +- 24 ms 502 +- 28 ms (-49%)
>>>
>>> The cost is paid by folios that keep cycling between zswap and memory:
>>> each reclaim allocates a new slot and adds the folio to the swap cache
>>> again. On a kernel build in a 600 MiB memcg (-j4, defconfig), this is
>>> not measurable (mean +- stddev of 5 runs on bare metal):
>>>
>>> base patched
>>> time (s) 592.4 +- 1.1 592.4 +- 0.9 (+0.0%)
>>> sys (s) 256.6 +- 1.5 257.5 +- 1.4 (+0.3%)
>>>
>>> In the worst case, random reads over 4 GiB of anonymous memory in a
>>> 3 GiB memcg, so that half the reads are zswap read faults and the task
>>> spends all its time in the kernel, throughput drops by 1.1-1.4%
>>> (mean +- stddev of 5 runs on bare metal):
>>>
>>> base patched
>>> reads/s, 1 thread 37310 +- 182 36889 +- 77 (-1.1%)
>>> reads/s, 8 threads 291937 +- 802 287954 +- 373 (-1.4%)
>>>
>>> Signed-off-by: Alexandre Ghiti <alex@ghiti.fr>
>>> ---
>>>
>>> Notes:
>>> Previous discussion:
>>> https://lore.kernel.org/all/CAGsJ_4xBtKGqXFP4zvp+Qn+tb4X7aCvXn+wqDqot_cXR92DjhQ@mail.gmail.com/
>>>
>>> mm/memory.c | 9 ++++++---
>>> 1 file changed, 6 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/mm/memory.c b/mm/memory.c
>>> index d477a4f986bc..dac0f0a24bc7 100644
>>> --- a/mm/memory.c
>>> +++ b/mm/memory.c
>>> @@ -4711,10 +4711,13 @@ static inline bool should_try_to_free_swap(struct swap_info_struct *si,
>>> return true;
>>>
>>> /*
>>> - * Free the swapcache only if we are the exclusive user and
>>> - * this is a write fault.
>>> + * Free the swapcache only if we are the exclusive user and this is a
>>> + * write fault, or the folio is dirty: its copy in the swap slot is
>>> + * stale then (zswap loads are exclusive and dirty the folio), so the
>>> + * slot only holds swap space.
>>> */
>>> - return (fault_flags & FAULT_FLAG_WRITE) && exclusive;
>>> + return exclusive && ((fault_flags & FAULT_FLAG_WRITE) ||
>>> + folio_test_dirty(folio));
>>
>> Why not more readable as
>>
>> if (!exclusive)
>> return false;
>> return (fault_flags & FAULT_FLAG_WRITE) || folio_test_dirty(folio);
>
> Agree, I'll change that for v2.
>
>>
>> But ... isn't this zswap specific? Why do we have zswap specific stuff in this
>> common code?
>>
>> The comment already gives away that something is not quite right here.
>
> I am mostly interested in the zswap benefits, but nothing is zswap
> specific here: do_swap_page() can also find a dirty folio in the swap
> cache when reclaim bailed out before writeout, or when the swap write
> failed. In all those cases, the copy in the slot is stale, so the slot
> only reserves swap space. zswap is simply the common case, since
> zswap_load() dirties the folio.
>
> What do you think?
If it's generic, then the comment shouldn't mention zswap in the comment :)
And the patch description should be all about zswap.
And we should measure the effect without zswap.
So, folio dirty -> We'd need another writeback because the content on disk is stale.
If we reclaim swap, the next reclaim run will have to re-allocate one.
But why is this "is dirty" linked to exclusive?
What if we did
/*
* Free the swapcache if the folio is already dirty, or if we will dirty it
* immediately afterwards by writing to it.
*/
if (folio_test_dirty(folio))
return true;
return (fault_flags & FAULT_FLAG_WRITE) && exclusive;
--
Cheers,
David
prev parent reply other threads:[~2026-11-26 16:20 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 9:42 [PATCH] mm: free the swap slot of a dirty swapin folio Alexandre Ghiti
2026-11-26 12:54 ` David Hildenbrand (Arm)
2026-11-26 15:01 ` Alexandre Ghiti
2026-11-26 16:20 ` David Hildenbrand (Arm) [this message]
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=0b7e81a8-d291-4cbf-8da0-e65e9a0b40a0@kernel.org \
--to=david@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=alex@ghiti.fr \
--cc=alexghiti@meta.com \
--cc=baohua@kernel.org \
--cc=baoquan.he@linux.dev \
--cc=chengming.zhou@linux.dev \
--cc=chrisl@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=kasong@tencent.com \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.com \
--cc=nphamcs@gmail.com \
--cc=rppt@kernel.org \
--cc=shikemeng@huaweicloud.com \
--cc=surenb@google.com \
--cc=vbabka@kernel.org \
--cc=yosry@kernel.org \
--cc=youngjun.park@lge.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