mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] mm: free the swap slot of a dirty swapin folio
@ 2026-11-26  9:42 Alexandre Ghiti
  2026-11-26 12:54 ` David Hildenbrand (Arm)
  2026-11-26 18:14 ` Nhat Pham
  0 siblings, 2 replies; 6+ messages in thread
From: Alexandre Ghiti @ 2026-11-26  9:42 UTC (permalink / raw)
  To: Andrew Morton, linux-mm
  Cc: linux-kernel, Barry Song, David Hildenbrand, Kairui Song,
	Chris Li, Nhat Pham, Johannes Weiner, Yosry Ahmed,
	Chengming Zhou, Baoquan He, Kemeng Shi, Youngjun Park,
	Liam R . Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Alexandre Ghiti

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));
 }
 
 static vm_fault_t pte_marker_clear(struct vm_fault *vmf)
-- 
2.53.1-Meta


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm: free the swap slot of a dirty swapin folio
  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 18:14 ` Nhat Pham
  1 sibling, 1 reply; 6+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-26 12:54 UTC (permalink / raw)
  To: Alexandre Ghiti, Andrew Morton, linux-mm
  Cc: linux-kernel, Barry Song, Kairui Song, Chris Li, Nhat Pham,
	Johannes Weiner, Yosry Ahmed, Chengming Zhou, Baoquan He,
	Kemeng Shi, Youngjun Park, Liam R . Howlett, Vlastimil Babka,
	Mike Rapoport, Suren Baghdasaryan, Michal Hocko

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);

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.

-- 
Cheers,

David

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm: free the swap slot of a dirty swapin folio
  2026-11-26 12:54 ` David Hildenbrand (Arm)
@ 2026-11-26 15:01   ` Alexandre Ghiti
  2026-11-26 16:20     ` David Hildenbrand (Arm)
  0 siblings, 1 reply; 6+ messages in thread
From: Alexandre Ghiti @ 2026-11-26 15:01 UTC (permalink / raw)
  To: David Hildenbrand (Arm)
  Cc: Alexandre Ghiti, Andrew Morton, linux-mm, linux-kernel,
	Barry Song, Kairui Song, Chris Li, Nhat Pham, Johannes Weiner,
	Yosry Ahmed, Chengming Zhou, Baoquan He, Kemeng Shi,
	Youngjun Park, Liam R . Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko

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?

Thanks,

Alex


>
> --
> Cheers,
>
> David
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm: free the swap slot of a dirty swapin folio
  2026-11-26 15:01   ` Alexandre Ghiti
@ 2026-11-26 16:20     ` David Hildenbrand (Arm)
  2026-11-26 17:54       ` Nhat Pham
  0 siblings, 1 reply; 6+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-26 16:20 UTC (permalink / raw)
  To: Alexandre Ghiti
  Cc: Alexandre Ghiti, Andrew Morton, linux-mm, linux-kernel,
	Barry Song, Kairui Song, Chris Li, Nhat Pham, Johannes Weiner,
	Yosry Ahmed, Chengming Zhou, Baoquan He, Kemeng Shi,
	Youngjun Park, Liam R . Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm: free the swap slot of a dirty swapin folio
  2026-11-26 16:20     ` David Hildenbrand (Arm)
@ 2026-11-26 17:54       ` Nhat Pham
  0 siblings, 0 replies; 6+ messages in thread
From: Nhat Pham @ 2026-11-26 17:54 UTC (permalink / raw)
  To: David Hildenbrand (Arm)
  Cc: Alexandre Ghiti, Alexandre Ghiti, Andrew Morton, linux-mm,
	linux-kernel, Barry Song, Kairui Song, Chris Li, Johannes Weiner,
	Yosry Ahmed, Chengming Zhou, Baoquan He, Kemeng Shi,
	Youngjun Park, Liam R . Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko

On Thu, Nov 26, 2026 at 9:20 AM David Hildenbrand (Arm)
<david@kernel.org> wrote:
>
> On 11/26/26 16:01, Alexandre Ghiti wrote:
> >
> >>
> >> 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 :)

+1. Maybe mention zswap as an example of when this can happen rather
than the sole motivation. I suspect this is a generic problem, with
zswap being a common/easy to trigger case :)

In general, seems like the whole point of caching these slots
(especially after Kairui optimized the swap allocator) is just to
avoid repeating pageout work in subsequent reclaim attempts. In zswap
case (where we invalidate the fault after page-in), that trade-off is
not worth it.

FWIW, zram is also doing the same thing:

/*
 * Always try to free swap cache for SWP_SYNCHRONOUS_IO devices. Swap
 * cache can help save some IO or memory overhead, but these devices
 * are fast, and meanwhile, swap cache pinning the slot deferring the
 * release of metadata or fragmentation is a more critical issue.
 */
if (data_race(si->flags & SWP_SYNCHRONOUS_IO))
    return true;

so we kinda already have some backend-specific logic bled out to this
location. Maybe we can genericize this in the future :)

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

I don't think we need exclusive here at all yeah. folio_free_swap()
already does all the necessary safety checks.

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

LGTM.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm: free the swap slot of a dirty swapin folio
  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 18:14 ` Nhat Pham
  1 sibling, 0 replies; 6+ messages in thread
From: Nhat Pham @ 2026-11-26 18:14 UTC (permalink / raw)
  To: Alexandre Ghiti
  Cc: Andrew Morton, linux-mm, linux-kernel, Barry Song,
	David Hildenbrand, Kairui Song, Chris Li, Johannes Weiner,
	Yosry Ahmed, Chengming Zhou, Baoquan He, Kemeng Shi,
	Youngjun Park, Liam R . Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko

On Thu, Nov 26, 2026 at 2:43 AM Alexandre Ghiti <alex@ghiti.fr> 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%)

I think this worst-case throughput drop fine.

With SSD swap, I think the number is even going to be less
significant, and furthermore it will be much rarer to hit this case.
When SSD swap IO completes, it's going to be in clean state. I guess
you need to have a reader race with a writer who wins the race and
dirties that page before the reader?

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-26 18:14 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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)
2026-11-26 17:54       ` Nhat Pham
2026-11-26 18:14 ` Nhat Pham

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®