mm/zswap: Fixes and improves the zswap global shrinker
cover mm/zswap: Fixes and improves the zswap global shrinker
From: Hao Jia <[email protected]>
This is the first half of the patch series[1]. The remaining part, which covers proactive writeback, will be submitted later once the swap tiering design is finalized.
This series fixes and improves the zswap global shrinker (shrink_worker()):
Patch 1: Fix missing global shrinker when memory cgroup is disabled. Patch 2: Extend shrink_memcg() to support batch writeback and update its
return value semantics, thereby improving the writeback efficiency in the shrink_worker() path.
[1] https://lore.kernel.org/all/[email protected]
Hao Jia (2):
mm/zswap: Fix global shrinker when memory cgroup is disabled mm/zswap: Support batch writeback in shrink_memcg()
mm/zswap.c | 100 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 75 insertions(+), 25 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/2 mm/zswap: Fix global shrinker when memory cgroup is disabled
From: Hao Jia <[email protected]>
When memory cgroup is disabled, mem_cgroup_iter() always returns NULL. Therefore, the global shrinker shrink_worker() always takes the !memcg branch. After MAX_RECLAIM_RETRIES empty walks, the worker simply gives up, so it fails to write back anything.
Therefore, when memory cgroup is disabled, fall through with the !memcg branch and shrink the root memcg directly.
With memcg disabled, shrink_memcg() only returns -ENOENT when the root LRU is empty, which means the total pages are already below thr. The loop then safely bails out via the zswap_total_pages() <= thr check. For any other return value from shrink_memcg(), the loop is guaranteed to terminate, either after MAX_RECLAIM_RETRIES failures or once the threshold is met.
--- mm/zswap.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
mm/zswap.c +6 -5
@@ -1356,11 +1356,12 @@ static void shrink_worker(struct work_struct *w) }while (memcg && !mem_cgroup_tryget_online (memcg));spin_unlock (&zswap_shrink_lock); -if (!memcg) { -/* -* Continue shrinking without incrementing failures if -* we found candidate memcgs in the last tree walk. -*/ +/* +* A NULL memcg ends a full hierarchy pass (except when memcg is +* disabled, where it is always NULL: fall through to the root LRU). +* Count a failure only if the last pass found no candidates. +*/ +if (!memcg && !mem_cgroup_disabled ()) {if (!attempts && ++failures ==MAX_RECLAIM_RETRIES )break ;
Signature · 1 line
2.34.1
Yosry Ahmed
maintainerRe: [PATCH 1/2] mm/zswap: Fix global shrinker when memory cgroup is disabled
Quoted 18 lines · Hao Jia
From: Hao Jia <[email protected]>
When memory cgroup is disabled,
mem_cgroup_iter()always returns NULL. Therefore, the global shrinkershrink_worker()always takes the !memcg branch. AfterMAX_RECLAIM_RETRIESempty walks, the worker simply gives up, so it fails to write back anything.Therefore, when memory cgroup is disabled, fall through with the !memcg branch and shrink the root memcg directly.
With memcg disabled,
shrink_memcg()only returns-ENOENTwhen the root LRU is empty, which means the total pages are already below thr. The loop then safely bails out via thezswap_total_pages()<= thr check. For any other return value fromshrink_memcg(), the loop is guaranteed to terminate, either afterMAX_RECLAIM_RETRIESfailures or once the threshold is met.
Quoted 8 lines
Fixes a65b0e7607cc ("zswap: make shrinking memcg-aware")Suggested-by Nhat Pham <[email protected]>Acked-by Nhat Pham <[email protected]>Acked-by Yosry Ahmed <[email protected]>Reported-by Yosry Ahmed <[email protected]>Closes https://lore.kernel.org/all/CAO9r8zPVzMKFbCixxD-qgtRrkFxWVrHiZZeLc=eyTPKPVQgX4g@mail.gmail.com open hereSigned-off-by Hao Jia <[email protected]>
Patch 2 doesn't really depend on this one, right?
If that's the case I think this can (and should be) picked up separately as a hotfix. Andrew, WDYT?
Quoted 29 lines
--- mm/zswap.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)mm/zswap.c +6
-6@@ -1356,11 +1356,12 @@ static void shrink_worker(struct work_struct *w) }while (memcg && !mem_cgroup_tryget_online (memcg));spin_unlock (&zswap_shrink_lock); -if (!memcg) { -/* -* Continue shrinking without incrementing failures if -* we found candidate memcgs in the last tree walk. -*/ +/* +* A NULL memcg ends a full hierarchy pass (except when memcg is +* disabled, where it is always NULL: fall through to the root LRU). +* Count a failure only if the last pass found no candidates. +*/ +if (!memcg && !mem_cgroup_disabled ()) {if (!attempts && ++failures ==MAX_RECLAIM_RETRIES )break ; --2.34.1
Andrew Morton
maintainerRe: [PATCH 1/2] mm/zswap: Fix global shrinker when memory cgroup is disabled
Quoted 24 lines · On Tue, 14 Jul 2026 09:52:59 -0700 Yosry Ahmed <[email protected]> wrote:
Quoted 15 lines
When memory cgroup is disabled,
mem_cgroup_iter()always returns NULL. Therefore, the global shrinkershrink_worker()always takes the !memcg branch. AfterMAX_RECLAIM_RETRIESempty walks, the worker simply gives up, so it fails to write back anything.Therefore, when memory cgroup is disabled, fall through with the !memcg branch and shrink the root memcg directly.
With memcg disabled,
shrink_memcg()only returns-ENOENTwhen the root LRU is empty, which means the total pages are already below thr. The loop then safely bails out via thezswap_total_pages()<= thr check. For any other return value fromshrink_memcg(), the loop is guaranteed to terminate, either afterMAX_RECLAIM_RETRIESfailures or once the threshold is met.Quoted 8 lines
Fixes a65b0e7607cc ("zswap: make shrinking memcg-aware")Suggested-by Nhat Pham <[email protected]>Acked-by Nhat Pham <[email protected]>Acked-by Yosry Ahmed <[email protected]>Reported-by Yosry Ahmed <[email protected]>Closes https://lore.kernel.org/all/CAO9r8zPVzMKFbCixxD-qgtRrkFxWVrHiZZeLc=eyTPKPVQgX4g@mail.gmail.com open hereSigned-off-by Hao Jia <[email protected]>
Quoted 4 lines
Patch 2 doesn't really depend on this one, right?
If that's the case I think this can (and should be) picked up separately as a hotfix. Andrew, WDYT?
Please update the changelog to clearly describe the userspace-visible effects of the bug, thanks.
Also, AI review has flagged several possible issues, all appear to be serious:
https://sashiko.dev/#/patchset/[email protected]
Re: [PATCH 1/2] mm/zswap: Fix global shrinker when memory cgroup is disabled
Quoted 26 lines · Andrew Morton
Quoted 24 lines · On Tue, 14 Jul 2026 09:52:59 -0700 Yosry Ahmed <[email protected]> wrote:
Quoted 23 lines
When memory cgroup is disabled,
mem_cgroup_iter()always returns NULL. Therefore, the global shrinkershrink_worker()always takes the !memcg branch. AfterMAX_RECLAIM_RETRIESempty walks, the worker simply gives up, so it fails to write back anything.Therefore, when memory cgroup is disabled, fall through with the !memcg branch and shrink the root memcg directly.
With memcg disabled,
shrink_memcg()only returns-ENOENTwhen the root LRU is empty, which means the total pages are already below thr. The loop then safely bails out via thezswap_total_pages()<= thr check. For any other return value fromshrink_memcg(), the loop is guaranteed to terminate, either afterMAX_RECLAIM_RETRIESfailures or once the threshold is met.Fixes a65b0e7607cc ("zswap: make shrinking memcg-aware")Suggested-by Nhat Pham <[email protected]>Acked-by Nhat Pham <[email protected]>Acked-by Yosry Ahmed <[email protected]>Reported-by Yosry Ahmed <[email protected]>Closes https://lore.kernel.org/all/CAO9r8zPVzMKFbCixxD-qgtRrkFxWVrHiZZeLc=eyTPKPVQgX4g@mail.gmail.com open hereSigned-off-by Hao Jia <[email protected]>
Quoted 7 lines
Quoted 4 lines
Patch 2 doesn't really depend on this one, right?
If that's the case I think this can (and should be) picked up separately as a hotfix. Andrew, WDYT?
Please update the changelog to clearly describe the userspace-visible effects of the bug, thanks.
I am not entirely sure if my understanding is correct here, but maybe I should add something like this to the commit message?
When cgroup_disable=memory is used (or with CONFIG_MEMCG=n), the global shrinker fails to write back any pages. Consequently, the zswap pool fills up to its limit and rejects further storage, preventing memory pressure from being offloaded to the backing swap device.
Quoted 3 lines
Also, AI review has flagged several possible issues, all appear to be serious:
https://sashiko.dev/#/patchset/[email protected]
For AI review comments on this patch:
I suspect this scenario might only exist in theory. For zswap LRU to be empty while zswap_total_pages() > thr holds true, it would require a prolonged state where there are always more than thr zswap entries on the zswap LRU whenever zswap_total_pages() > thr is evaluated, yet the zswap LRU happens to be empty during shrink_memcg(root_memcg).
If we want to fix this, perhaps we could do something like this?
Yosry, Nhat, what are your thoughts on this?
mm/zswap.c +6 -5
@@ -1356,11 +1356,12 @@ static void shrink_worker(struct work_struct *w) }while (memcg && !mem_cgroup_tryget_online (memcg));spin_unlock (&zswap_shrink_lock); -if (!memcg) { -/* -* Continue shrinking without incrementing failures if -* we found candidate memcgs in the last tree walk. -*/ +/* +* A NULL memcg ends a full hierarchy pass (except when memcg is +* disabled, where it is always NULL: fall through to the root LRU). +* Count a failure only if the last pass found no candidates. +*/ +if (!memcg && !mem_cgroup_disabled ()) {if (!attempts && ++failures ==MAX_RECLAIM_RETRIES )break ;
@@ -1378,8 +1379,15 @@ static void shrink_worker(struct work_struct *w)* with pages in zswap. Skip this without incrementing
attempts
* and failures.
*/
- if (ret == -ENOENT)
+ if (ret == -ENOENT) {
+ /*
+ * With memcg disabled the root LRU is the only target, so
+ * we should abort if it has no writeback-candidate pages.
+ */
+ if (mem_cgroup_disabled())
+ break;
continue;
+ }
++attempts;
if (ret && ++failures == MAX_RECLAIM_RETRIES)Thanks,
Hao
Yosry Ahmed
maintainerRe: [PATCH 1/2] mm/zswap: Fix global shrinker when memory cgroup is disabled
Quoted 38 lines · Hao Jia
Quoted 26 lines · Andrew Morton
Quoted 24 lines · On Tue, 14 Jul 2026 09:52:59 -0700 Yosry Ahmed <[email protected]> wrote:
Quoted 23 lines
When memory cgroup is disabled,
mem_cgroup_iter()always returns NULL. Therefore, the global shrinkershrink_worker()always takes the !memcg branch. AfterMAX_RECLAIM_RETRIESempty walks, the worker simply gives up, so it fails to write back anything.Therefore, when memory cgroup is disabled, fall through with the !memcg branch and shrink the root memcg directly.
With memcg disabled,
shrink_memcg()only returns-ENOENTwhen the root LRU is empty, which means the total pages are already below thr. The loop then safely bails out via thezswap_total_pages()<= thr check. For any other return value fromshrink_memcg(), the loop is guaranteed to terminate, either afterMAX_RECLAIM_RETRIESfailures or once the threshold is met.Fixes a65b0e7607cc ("zswap: make shrinking memcg-aware")Suggested-by Nhat Pham <[email protected]>Acked-by Nhat Pham <[email protected]>Acked-by Yosry Ahmed <[email protected]>Reported-by Yosry Ahmed <[email protected]>Closes https://lore.kernel.org/all/CAO9r8zPVzMKFbCixxD-qgtRrkFxWVrHiZZeLc=eyTPKPVQgX4g@mail.gmail.com open hereSigned-off-by Hao Jia <[email protected]>Quoted 7 lines
Quoted 4 lines
Patch 2 doesn't really depend on this one, right?
If that's the case I think this can (and should be) picked up separately as a hotfix. Andrew, WDYT?
Please update the changelog to clearly describe the userspace-visible effects of the bug, thanks.
Quoted 7 lines
I am not entirely sure if my understanding is correct here, but maybe I should add something like this to the commit message?
When cgroup_disable=memory is used (or with
CONFIG_MEMCG=n), the global shrinker fails to write back any pages. Consequently, the zswap pool fills up to its limit and rejects further storage, preventing memory pressure from being offloaded to the backing swap device.
I think you can simply write that zswap writeback when the limit is hit is broken when memcg is disabled.
Quoted 12 lines
Quoted 4 lines
Also, AI review has flagged several possible issues, all appear to be serious:
https://sashiko.dev/#/patchset/[email protected]For AI review comments on this patch:
I suspect this scenario might only exist in theory. For zswap LRU to be empty whilezswap_total_pages()> thr holds true, it would require a prolonged state where there are always more than thr zswap entries on the zswap LRU wheneverzswap_total_pages()> thr is evaluated, yet the zswap LRU happens to be empty duringshrink_memcg(root_memcg).
Quoted 3 lines
If we want to fix this, perhaps we could do something like this?
Yosry, Nhat, what are your thoughts on this?
Do we need to do this? The last paragraph in your changelog explains why this can't happen because zswap_total_pages() should be 0 in this case. Did I miss something?
Re: [PATCH 1/2] mm/zswap: Fix global shrinker when memory cgroup is disabled
Quoted 42 lines · Yosry Ahmed
Quoted 41 lines · Hao Jia
Quoted 26 lines · Andrew Morton
Quoted 24 lines · On Tue, 14 Jul 2026 09:52:59 -0700 Yosry Ahmed <[email protected]> wrote:
Quoted 23 lines
When memory cgroup is disabled,
mem_cgroup_iter()always returns NULL. Therefore, the global shrinkershrink_worker()always takes the !memcg branch. AfterMAX_RECLAIM_RETRIESempty walks, the worker simply gives up, so it fails to write back anything.Therefore, when memory cgroup is disabled, fall through with the !memcg branch and shrink the root memcg directly.
With memcg disabled,
shrink_memcg()only returns-ENOENTwhen the root LRU is empty, which means the total pages are already below thr. The loop then safely bails out via thezswap_total_pages()<= thr check. For any other return value fromshrink_memcg(), the loop is guaranteed to terminate, either afterMAX_RECLAIM_RETRIESfailures or once the threshold is met.Fixes a65b0e7607cc ("zswap: make shrinking memcg-aware")Suggested-by Nhat Pham <[email protected]>Acked-by Nhat Pham <[email protected]>Acked-by Yosry Ahmed <[email protected]>Reported-by Yosry Ahmed <[email protected]>Closes https://lore.kernel.org/all/CAO9r8zPVzMKFbCixxD-qgtRrkFxWVrHiZZeLc=eyTPKPVQgX4g@mail.gmail.com open hereSigned-off-by Hao Jia <[email protected]>Quoted 7 lines
Quoted 4 lines
Patch 2 doesn't really depend on this one, right?
If that's the case I think this can (and should be) picked up separately as a hotfix. Andrew, WDYT?
Please update the changelog to clearly describe the userspace-visible effects of the bug, thanks.
I am not entirely sure if my understanding is correct here, but maybe I should add something like this to the commit message?
Quoted 7 lines
Quoted 4 lines
When cgroup_disable=memory is used (or with
CONFIG_MEMCG=n), the global shrinker fails to write back any pages. Consequently, the zswap pool fills up to its limit and rejects further storage, preventing memory pressure from being offloaded to the backing swap device.I think you can simply write that zswap writeback when the limit is hit is broken when memcg is disabled.
Will do. Thanks!
Quoted 13 lines
Quoted 13 lines
Quoted 5 lines
Also, AI review has flagged several possible issues, all appear to be serious:
https://sashiko.dev/#/patchset/[email protected]For AI review comments on this patch:
I suspect this scenario might only exist in theory. For zswap LRU to be empty whilezswap_total_pages()> thr holds true, it would require a prolonged state where there are always more than thr zswap entries on the zswap LRU wheneverzswap_total_pages()> thr is evaluated, yet the zswap LRU happens to be empty duringshrink_memcg(root_memcg).
Quoted 7 lines
Quoted 3 lines
If we want to fix this, perhaps we could do something like this?
Yosry, Nhat, what are your thoughts on this?
Do we need to do this? The last paragraph in your changelog explains why this can't happen because
zswap_total_pages()should be 0 in this case. Did I miss something?
The loop would require the following sequence to repeat indefinitely:
1、zswap_total_pages() > thr evaluates to true.
2、During shrink_memcg(root_memcg), the zswap LRU is concurrently drained to empty.
3、Before zswap_total_pages() > thr is evaluated again, the zswap LRU is heavily refilled such that zswap_total_pages() > thr holds true once more.
For our case to manifest, it would require zswap_total_pages() and the zswap LRU state to repeatedly hit this exact window with perfect alignment over a **prolonged period**. Therefore, I suspect this scenario might only exist in theory.
Thanks,
Hao
Nhat Pham
maintainerRe: [PATCH 1/2] mm/zswap: Fix global shrinker when memory cgroup is disabled
Quoted 84 lines · Hao Jia
Quoted 42 lines · Yosry Ahmed
Quoted 41 lines · Hao Jia
Quoted 26 lines · Andrew Morton
Quoted 24 lines · On Tue, 14 Jul 2026 09:52:59 -0700 Yosry Ahmed <[email protected]> wrote:
Quoted 23 lines
When memory cgroup is disabled,
mem_cgroup_iter()always returns NULL. Therefore, the global shrinkershrink_worker()always takes the !memcg branch. AfterMAX_RECLAIM_RETRIESempty walks, the worker simply gives up, so it fails to write back anything.Therefore, when memory cgroup is disabled, fall through with the !memcg branch and shrink the root memcg directly.
With memcg disabled,
shrink_memcg()only returns-ENOENTwhen the root LRU is empty, which means the total pages are already below thr. The loop then safely bails out via thezswap_total_pages()<= thr check. For any other return value fromshrink_memcg(), the loop is guaranteed to terminate, either afterMAX_RECLAIM_RETRIESfailures or once the threshold is met.Fixes a65b0e7607cc ("zswap: make shrinking memcg-aware")Suggested-by Nhat Pham <[email protected]>Acked-by Nhat Pham <[email protected]>Acked-by Yosry Ahmed <[email protected]>Reported-by Yosry Ahmed <[email protected]>Closes https://lore.kernel.org/all/CAO9r8zPVzMKFbCixxD-qgtRrkFxWVrHiZZeLc=eyTPKPVQgX4g@mail.gmail.com open hereSigned-off-by Hao Jia <[email protected]>Quoted 7 lines
Quoted 4 lines
Patch 2 doesn't really depend on this one, right?
If that's the case I think this can (and should be) picked up separately as a hotfix. Andrew, WDYT?
Please update the changelog to clearly describe the userspace-visible effects of the bug, thanks.
I am not entirely sure if my understanding is correct here, but maybe I should add something like this to the commit message?
Quoted 7 lines
Quoted 4 lines
When cgroup_disable=memory is used (or with
CONFIG_MEMCG=n), the global shrinker fails to write back any pages. Consequently, the zswap pool fills up to its limit and rejects further storage, preventing memory pressure from being offloaded to the backing swap device.I think you can simply write that zswap writeback when the limit is hit is broken when memcg is disabled.
Will do. Thanks!
Quoted 13 lines
Quoted 13 lines
Quoted 5 lines
Also, AI review has flagged several possible issues, all appear to be serious:
https://sashiko.dev/#/patchset/[email protected]For AI review comments on this patch:
I suspect this scenario might only exist in theory. For zswap LRU to be empty whilezswap_total_pages()> thr holds true, it would require a prolonged state where there are always more than thr zswap entries on the zswap LRU wheneverzswap_total_pages()> thr is evaluated, yet the zswap LRU happens to be empty duringshrink_memcg(root_memcg).Quoted 7 lines
Quoted 3 lines
If we want to fix this, perhaps we could do something like this?
Yosry, Nhat, what are your thoughts on this?
Do we need to do this? The last paragraph in your changelog explains why this can't happen because
zswap_total_pages()should be 0 in this case. Did I miss something?The loop would require the following sequence to repeat indefinitely:
1、zswap_total_pages() > thr evaluates to true.
2、Duringshrink_memcg(root_memcg), the zswap LRU is concurrently drained to empty.
3、Beforezswap_total_pages()> thr is evaluated again, the zswap LRU is heavily refilled such thatzswap_total_pages()> thr holds true once more.
Quoted 4 lines
For our case to manifest, it would require
zswap_total_pages()and the zswap LRU state to repeatedly hit this exact window with perfect alignment over a **prolonged period**. Therefore, I suspect this scenario might only exist in theory.
Yeah seems very artificial indeed.
If we want to be extra careful here, maybe we can put a cond_resched() there or replace the continue with goto resched or sth?
Yosry Ahmed
maintainerRe: [PATCH 1/2] mm/zswap: Fix global shrinker when memory cgroup is disabled
Quoted 91 lines · Nhat Pham
Quoted 84 lines · Hao Jia
Quoted 42 lines · Yosry Ahmed
Quoted 41 lines · Hao Jia
Quoted 26 lines · Andrew Morton
Quoted 24 lines · On Tue, 14 Jul 2026 09:52:59 -0700 Yosry Ahmed <[email protected]> wrote:
Quoted 23 lines
When memory cgroup is disabled,
mem_cgroup_iter()always returns NULL. Therefore, the global shrinkershrink_worker()always takes the !memcg branch. AfterMAX_RECLAIM_RETRIESempty walks, the worker simply gives up, so it fails to write back anything.Therefore, when memory cgroup is disabled, fall through with the !memcg branch and shrink the root memcg directly.
With memcg disabled,
shrink_memcg()only returns-ENOENTwhen the root LRU is empty, which means the total pages are already below thr. The loop then safely bails out via thezswap_total_pages()<= thr check. For any other return value fromshrink_memcg(), the loop is guaranteed to terminate, either afterMAX_RECLAIM_RETRIESfailures or once the threshold is met.Fixes a65b0e7607cc ("zswap: make shrinking memcg-aware")Suggested-by Nhat Pham <[email protected]>Acked-by Nhat Pham <[email protected]>Acked-by Yosry Ahmed <[email protected]>Reported-by Yosry Ahmed <[email protected]>Closes https://lore.kernel.org/all/CAO9r8zPVzMKFbCixxD-qgtRrkFxWVrHiZZeLc=eyTPKPVQgX4g@mail.gmail.com open hereSigned-off-by Hao Jia <[email protected]>Quoted 7 lines
Quoted 4 lines
Patch 2 doesn't really depend on this one, right?
If that's the case I think this can (and should be) picked up separately as a hotfix. Andrew, WDYT?
Please update the changelog to clearly describe the userspace-visible effects of the bug, thanks.
I am not entirely sure if my understanding is correct here, but maybe I should add something like this to the commit message?
Quoted 7 lines
Quoted 4 lines
When cgroup_disable=memory is used (or with
CONFIG_MEMCG=n), the global shrinker fails to write back any pages. Consequently, the zswap pool fills up to its limit and rejects further storage, preventing memory pressure from being offloaded to the backing swap device.I think you can simply write that zswap writeback when the limit is hit is broken when memcg is disabled.
Will do. Thanks!
Quoted 13 lines
Quoted 13 lines
Quoted 5 lines
Also, AI review has flagged several possible issues, all appear to be serious:
https://sashiko.dev/#/patchset/[email protected]For AI review comments on this patch:
I suspect this scenario might only exist in theory. For zswap LRU to be empty whilezswap_total_pages()> thr holds true, it would require a prolonged state where there are always more than thr zswap entries on the zswap LRU wheneverzswap_total_pages()> thr is evaluated, yet the zswap LRU happens to be empty duringshrink_memcg(root_memcg).Quoted 7 lines
Quoted 3 lines
If we want to fix this, perhaps we could do something like this?
Yosry, Nhat, what are your thoughts on this?
Do we need to do this? The last paragraph in your changelog explains why this can't happen because
zswap_total_pages()should be 0 in this case. Did I miss something?The loop would require the following sequence to repeat indefinitely:
1、zswap_total_pages() > thr evaluates to true.
2、Duringshrink_memcg(root_memcg), the zswap LRU is concurrently drained to empty.
3、Beforezswap_total_pages()> thr is evaluated again, the zswap LRU is heavily refilled such thatzswap_total_pages()> thr holds true once more.Quoted 4 lines
For our case to manifest, it would require
zswap_total_pages()and the zswap LRU state to repeatedly hit this exact window with perfect alignment over a **prolonged period**. Therefore, I suspect this scenario might only exist in theory.
Quoted 4 lines
Yeah seems very artificial indeed.
If we want to be extra careful here, maybe we can put a
cond_resched()there or replace the continue with goto resched or sth?
I think it's also possible with the current code with memcg enabled. It's still possible that the shrinker puts usage under the acceptance threshold and then a new zswap stores puts it back above the threshold, and so on.
Perhaps as Nhat said, just goto resched instead of continue if we're really worried, but I think even that is not really necessary.
I think we generally want to clean up and simplify the shrinking loop in zswap_shrinker(), but I don't have any great ideas.
Re: [PATCH 1/2] mm/zswap: Fix global shrinker when memory cgroup is disabled
Quoted 97 lines · Yosry Ahmed
Quoted 91 lines · Nhat Pham
Quoted 84 lines · Hao Jia
Quoted 42 lines · Yosry Ahmed
Quoted 41 lines · Hao Jia
Quoted 26 lines · Andrew Morton
Quoted 24 lines · On Tue, 14 Jul 2026 09:52:59 -0700 Yosry Ahmed <[email protected]> wrote:
Quoted 23 lines
When memory cgroup is disabled,
mem_cgroup_iter()always returns NULL. Therefore, the global shrinkershrink_worker()always takes the !memcg branch. AfterMAX_RECLAIM_RETRIESempty walks, the worker simply gives up, so it fails to write back anything.Therefore, when memory cgroup is disabled, fall through with the !memcg branch and shrink the root memcg directly.
With memcg disabled,
shrink_memcg()only returns-ENOENTwhen the root LRU is empty, which means the total pages are already below thr. The loop then safely bails out via thezswap_total_pages()<= thr check. For any other return value fromshrink_memcg(), the loop is guaranteed to terminate, either afterMAX_RECLAIM_RETRIESfailures or once the threshold is met.Fixes a65b0e7607cc ("zswap: make shrinking memcg-aware")Suggested-by Nhat Pham <[email protected]>Acked-by Nhat Pham <[email protected]>Acked-by Yosry Ahmed <[email protected]>Reported-by Yosry Ahmed <[email protected]>Closes https://lore.kernel.org/all/CAO9r8zPVzMKFbCixxD-qgtRrkFxWVrHiZZeLc=eyTPKPVQgX4g@mail.gmail.com open hereSigned-off-by Hao Jia <[email protected]>Quoted 7 lines
Quoted 4 lines
Patch 2 doesn't really depend on this one, right?
If that's the case I think this can (and should be) picked up separately as a hotfix. Andrew, WDYT?
Please update the changelog to clearly describe the userspace-visible effects of the bug, thanks.
I am not entirely sure if my understanding is correct here, but maybe I should add something like this to the commit message?
Quoted 7 lines
Quoted 4 lines
When cgroup_disable=memory is used (or with
CONFIG_MEMCG=n), the global shrinker fails to write back any pages. Consequently, the zswap pool fills up to its limit and rejects further storage, preventing memory pressure from being offloaded to the backing swap device.I think you can simply write that zswap writeback when the limit is hit is broken when memcg is disabled.
Will do. Thanks!
Quoted 13 lines
Quoted 13 lines
Quoted 5 lines
Also, AI review has flagged several possible issues, all appear to be serious:
https://sashiko.dev/#/patchset/[email protected]For AI review comments on this patch:
I suspect this scenario might only exist in theory. For zswap LRU to be empty whilezswap_total_pages()> thr holds true, it would require a prolonged state where there are always more than thr zswap entries on the zswap LRU wheneverzswap_total_pages()> thr is evaluated, yet the zswap LRU happens to be empty duringshrink_memcg(root_memcg).Quoted 7 lines
Quoted 3 lines
If we want to fix this, perhaps we could do something like this?
Yosry, Nhat, what are your thoughts on this?
Do we need to do this? The last paragraph in your changelog explains why this can't happen because
zswap_total_pages()should be 0 in this case. Did I miss something?The loop would require the following sequence to repeat indefinitely:
1、zswap_total_pages() > thr evaluates to true.
2、Duringshrink_memcg(root_memcg), the zswap LRU is concurrently drained to empty.
3、Beforezswap_total_pages()> thr is evaluated again, the zswap LRU is heavily refilled such thatzswap_total_pages()> thr holds true once more.Quoted 4 lines
For our case to manifest, it would require
zswap_total_pages()and the zswap LRU state to repeatedly hit this exact window with perfect alignment over a **prolonged period**. Therefore, I suspect this scenario might only exist in theory.Quoted 4 lines
Yeah seems very artificial indeed.
If we want to be extra careful here, maybe we can put a
cond_resched()there or replace the continue with goto resched or sth?
Quoted 8 lines
I think it's also possible with the current code with memcg enabled. It's still possible that the shrinker puts usage under the acceptance threshold and then a new zswap stores puts it back above the threshold, and so on.
Perhaps as Nhat said, just goto resched instead of continue if we're really worried, but I think even that is not really necessary.
Yes, but to be strictly rigorous, let's add a goto resched; anyway.
Thanks,
Hao
Quoted 2 lines
I think we generally want to clean up and simplify the shrinking loop in
zswap_shrinker(), but I don't have any great ideas.
PATCH 2/2 mm/zswap: Support batch writeback in shrink_memcg()
In reply to Hao JiaFrom: Hao Jia <[email protected]>
Currently, shrink_memcg() writes back at most one entry per-node during its traversal. This makes shrink_worker() inefficient, as it must repeatedly re-enter shrink_memcg() to make any substantial progress.
To address this, extend shrink_memcg() and rewrite its LRU iteration logic to support batch writeback. Introduce the nr_to_scan parameter to bound how many pages are scanned per call. This enables batch writeback in the shrink_worker() path, while maintaining a low scan budget in the zswap_store() path.
Additionally, to prepare for future proactive writeback, update the return value semantics of shrink_memcg(): a positive value now represents the actual number of compressed bytes written back, 0 indicates that candidates existed but no writeback succeeded, and a negative value represents an error code.
Test Setup:
Total memory: 32 GB.
zswap settings: max_pool_percent=1, accept_threshold_percent=50, shrinker_enabled=N.
Allocate 512MB of anonymous pages and fill them with random data (to avoid compression), then use cgroup memory.reclaim to force a large amount of anonymous pages into zswap. At an interval of 2ms, allocate a 4K anonymous page where the first 4 bytes are random numbers and the rest are zeros, and then trigger a reclamation of this 4K anonymous page through cgroup memory.reclaim. When the pool threshold is reached, shrink_memcg() will be triggered.
The test data after running for 120s is as follows:
Baseline Patchedshrink_worker wakeups 5363 85shrink_memcg calls 11,345,012 188,264written_back 40214 40275
Conclusion:
Under the same workload and run duration, the patched kernel shows a significant reduction in both shrink_worker wakeups and shrink_memcg calls.
--- mm/zswap.c | 89 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 69 insertions(+), 20 deletions(-)
mm/zswap.c +69 -20
@@ -160,6 +160,11 @@ struct zswap_pool {char tfm_name[CRYPTO_MAX_ALG_NAME ]; }; +struct zswap_shrink_walk_arg { +unsigned long bytes_written; +bool encountered_page_in_swapcache; +}; +/* Global LRU lists shared by all zswap pools. */ static struct list_lru zswap_list_lru;
@@ -1089,8 +1094,9 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o void *arg) { struct zswap_entry *entry = container_of(item, struct zswap_entry, lru); -bool *encountered_page_in_swapcache = (bool *)arg; +struct zswap_shrink_walk_arg *walk_arg = arg;swp_entry_t swpentry; +unsigned int length;enum lru_status ret =LRU_REMOVED_RETRY ;int writeback_result;
@@ -1133,10 +1139,11 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o/* * Once the lru lock is dropped, the entry might get freed. The -* swpentry is copied to the stack, and entry isn't deref'd again -* until the entry is verified to still be alive in the tree. +* needed fields are copied to the stack, and entry isn't deref'd +* again until it is verified to still be alive in the tree. */ swpentry = entry->swpentry; + length = entry->length;/* * It's safe to drop the lock here because we return either
@@ -1155,12 +1162,13 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o* into the warmer region. We should terminate shrinking (if we're in the dynamic * shrinker context). */ -if (writeback_result == -EEXIST && encountered_page_in_swapcache) { +if (writeback_result == -EEXIST ) { ret =LRU_STOP ; - *encountered_page_in_swapcache = true; + walk_arg->encountered_page_in_swapcache = true; } }else { zswap_written_back_pages++; + walk_arg->bytes_written += length; }return ret;
@@ -1169,8 +1177,11 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_ostatic unsigned long zswap_shrinker_scan (struct shrinker *shrinker,struct shrink_control *sc) { +struct zswap_shrink_walk_arg walk_arg = { + .bytes_written =0 , + .encountered_page_in_swapcache = false, + };unsigned long shrink_ret; -bool encountered_page_in_swapcache = false;if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled (sc->memcg)) {
@@ -1179,9 +1190,9 @@ static unsigned long zswap_shrinker_scan(struct shrinker *shrinker, } shrink_ret =list_lru_shrink_walk (&zswap_list_lru, sc, &shrink_memcg_cb, - &encountered_page_in_swapcache); + &walk_arg); -if (encountered_page_in_swapcache) +if (walk_arg.encountered_page_in_swapcache)return SHRINK_STOP ;return shrink_ret ? shrink_ret :SHRINK_STOP ;
@@ -1275,9 +1286,31 @@ static struct shrinker *zswap_alloc_shrinker(void)return shrinker; } -static int shrink_memcg (struct mem_cgroup *memcg) +#define NR_ZSWAP_WB_BATCH 64UL + +/* +* Scan up to @nr_to_scan pages across the per-node zswap LRUs of @memcg +* and write back the reclaimable ones. +* +* Since the second-chance algorithm rotates referenced entries to the +* LRU tail, the per-node scan is capped at the current LRU length so +* each entry is scanned at most once per call. It is up to the caller +* to handle retries, deciding whether to scan another memcg to complete +* the full iteration, or to rescan the current memcg to drain its zswap +* entries. +* +* Return: The number of compressed bytes written back (>= 0), or -ENOENT +* if @memcg has writeback disabled, is a zombie cgroup, or has empty +* zswap LRUs. +*/ +static long shrink_memcg (struct mem_cgroup *memcg,unsigned long nr_to_scan) { -int nid, shrunk =0 , scanned =0 ; +struct zswap_shrink_walk_arg walk_arg = { + .bytes_written =0 , + .encountered_page_in_swapcache = false, + }; +unsigned long nr_remaining = nr_to_scan; +int nid;if (!mem_cgroup_zswap_writeback_enabled (memcg))return -ENOENT ;
@@ -1290,24 +1323,40 @@ static int shrink_memcg(struct mem_cgroup *memcg)return -ENOENT ;for_each_node_state (nid,N_NORMAL_MEMORY ) { -unsigned long nr_to_walk =1 ; +unsigned long nr_to_walk; - shrunk +=list_lru_walk_one (&zswap_list_lru, nid, memcg, - &shrink_memcg_cb,NULL , &nr_to_walk); - scanned +=1 - nr_to_walk; +/* +* Cap the scan at per-node LRU length so each entry is scanned +* at most once per call. +*/ + nr_to_walk =min (nr_remaining, +list_lru_count_one (&zswap_list_lru, nid, memcg)); +if (!nr_to_walk) +continue ; + + nr_remaining -= nr_to_walk; +list_lru_walk_one (&zswap_list_lru, nid, memcg, &shrink_memcg_cb, + &walk_arg, &nr_to_walk); +/* Return the unused share of the budget to the pool. */ + nr_remaining += nr_to_walk; + +if (!nr_remaining) +break ; } -if (!scanned) +/* Nothing was scanned: every LRU under @memcg was empty. */ +if (nr_remaining == nr_to_scan)return -ENOENT ; -return shrunk ?0 : -EAGAIN ; +return walk_arg.bytes_written; }static void shrink_worker (struct work_struct *w) {struct mem_cgroup *memcg; -int ret, failures =0 , attempts =0 ; +int failures =0 , attempts =0 ;unsigned long thr; +long ret;/* Reclaim down to the accept threshold */ thr =zswap_accept_thr_pages ();
@@ -1369,7 +1418,7 @@ static void shrink_worker(struct work_struct *w) goto resched; } - ret =shrink_memcg (memcg); + ret =shrink_memcg (memcg,NR_ZSWAP_WB_BATCH );/* drop the extra reference */ mem_cgroup_put (memcg);
@@ -1383,7 +1432,7 @@ static void shrink_worker(struct work_struct *w)continue ; ++attempts; -if (ret && ++failures ==MAX_RECLAIM_RETRIES ) +if (ret <=0 && ++failures ==MAX_RECLAIM_RETRIES )break ; resched:cond_resched ();
@@ -1493,7 +1542,7 @@ bool zswap_store(struct folio *folio) objcg =get_obj_cgroup_from_folio (folio);if (objcg && !obj_cgroup_may_zswap (objcg)) { memcg =get_mem_cgroup_from_objcg (objcg); -if (shrink_memcg (memcg)) { +if (shrink_memcg (memcg,1 ) <=0 ) {mem_cgroup_put (memcg); goto put_objcg; }
Signature · 1 line
2.34.1
Yosry Ahmed
maintainerRe: [PATCH 2/2] mm/zswap: Support batch writeback in shrink_memcg()
Quoted 6 lines · Hao Jia
From: Hao Jia <[email protected]>
Currently,
shrink_memcg()writes back at most one entry per-node during its traversal. This makesshrink_worker()inefficient, as it must repeatedly re-entershrink_memcg()to make any substantial progress.
Please also mention the case about writeback being slow to keep up with refaults in some cases, leading to zswap store failures and pages skipping zswap and going directly to disk, which is an LRU inversion.
Quoted 7 lines
To address this, extend
shrink_memcg()and rewrite its LRU iteration logic to support batch writeback. Introduce thenr_to_scanparameter to bound how many pages are scanned per call. This enables batch writeback in theshrink_worker()path, while maintaining a low scan budget in thezswap_store()path.
Quoted 5 lines
Additionally, to prepare for future proactive writeback, update the return value semantics of
shrink_memcg(): a positive value now represents the actual number of compressed bytes written back, 0 indicates that candidates existed but no writeback succeeded, and a negative value represents an error code.
This part should be dropped for now, and added with the proactive writeback, as it's currently unused AFAICT. Removing zswap_shrink_walk_arg will simplify the patch and make it focused on the batching part.
Quoted 19 lines
Test Setup:
Total memory: 32 GB.
zswap settings: max_pool_percent=1, accept_threshold_percent=50, shrinker_enabled=N.
Allocate 512MB of anonymous pages and fill them with random data (to avoid compression), then use cgroup memory.reclaim to force a large amount of anonymous pages into zswap. At an interval of 2ms, allocate a 4K anonymous page where the first 4 bytes are random numbers and the rest are zeros, and then trigger a reclamation of this 4K anonymous page through cgroup memory.reclaim. When the pool threshold is reached,shrink_memcg()will be triggered.The test data after running for 120s is as follows:
Baseline Patchedshrink_workerwakeups 5363 85shrink_memcgcalls 11,345,012 188,264written_back40214 40275
Quoted 3 lines
Conclusion:
Under the same workload and run duration, the patched kernel shows a significant reduction in bothshrink_workerwakeups andshrink_memcgcalls.
Please also include data from the case where zswap store failures are observed and pages go to disk, and compare before and after this patch. I think that part is also really important.
Quoted 201 lines
Suggested-by Yosry Ahmed <[email protected]>Signed-off-by Hao Jia <[email protected]>--- mm/zswap.c | 89 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 69 insertions(+), 20 deletions(-)mm/zswap.c +69
-21@@ -160,6 +160,11 @@ struct zswap_pool {char tfm_name[CRYPTO_MAX_ALG_NAME ]; }; +struct zswap_shrink_walk_arg { +unsigned long bytes_written; +bool encountered_page_in_swapcache; +}; +/* Global LRU lists shared by all zswap pools. */ static struct list_lru zswap_list_lru;@@ -1089,8 +1094,9 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o void *arg) { struct zswap_entry *entry = container_of(item, struct zswap_entry, lru); -bool *encountered_page_in_swapcache = (bool *)arg; +struct zswap_shrink_walk_arg *walk_arg = arg;swp_entry_t swpentry; +unsigned int length;enum lru_status ret =LRU_REMOVED_RETRY ;int writeback_result;@@ -1133,10 +1139,11 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o/* * Once the lru lock is dropped, the entry might get freed. The -* swpentry is copied to the stack, and entry isn't deref'd again -* until the entry is verified to still be alive in the tree. +* needed fields are copied to the stack, and entry isn't deref'd +* again until it is verified to still be alive in the tree. */ swpentry = entry->swpentry; + length = entry->length;/* * It's safe to drop the lock here because we return either @@ -1155,12 +1162,13 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o* into the warmer region. We should terminate shrinking (if we're in the dynamic * shrinker context). */ -if (writeback_result == -EEXIST && encountered_page_in_swapcache) { +if (writeback_result == -EEXIST ) { ret =LRU_STOP ; - *encountered_page_in_swapcache = true; + walk_arg->encountered_page_in_swapcache = true; } }else { zswap_written_back_pages++; + walk_arg->bytes_written += length; }return ret;@@ -1169,8 +1177,11 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_ostatic unsigned long zswap_shrinker_scan (struct shrinker *shrinker,struct shrink_control *sc) { +struct zswap_shrink_walk_arg walk_arg = { + .bytes_written =0 , + .encountered_page_in_swapcache = false, + };unsigned long shrink_ret; -bool encountered_page_in_swapcache = false;if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled (sc->memcg)) {@@ -1179,9 +1190,9 @@ static unsigned long zswap_shrinker_scan(struct shrinker *shrinker, } shrink_ret =list_lru_shrink_walk (&zswap_list_lru, sc, &shrink_memcg_cb, - &encountered_page_in_swapcache); + &walk_arg); -if (encountered_page_in_swapcache) +if (walk_arg.encountered_page_in_swapcache)return SHRINK_STOP ;return shrink_ret ? shrink_ret :SHRINK_STOP ;@@ -1275,9 +1286,31 @@ static struct shrinker *zswap_alloc_shrinker(void)return shrinker; } -static int shrink_memcg (struct mem_cgroup *memcg) +#define NR_ZSWAP_WB_BATCH 64UL + +/* +* Scan up to @nr_to_scan pages across the per-node zswap LRUs of @memcg +* and write back the reclaimable ones. +* +* Since the second-chance algorithm rotates referenced entries to the +* LRU tail, the per-node scan is capped at the current LRU length so +* each entry is scanned at most once per call. It is up to the caller +* to handle retries, deciding whether to scan another memcg to complete +* the full iteration, or to rescan the current memcg to drain its zswap +* entries. +* +* Return: The number of compressed bytes written back (>= 0), or -ENOENT +* if @memcg has writeback disabled, is a zombie cgroup, or has empty +* zswap LRUs. +*/ +static long shrink_memcg (struct mem_cgroup *memcg,unsigned long nr_to_scan) { -int nid, shrunk =0 , scanned =0 ; +struct zswap_shrink_walk_arg walk_arg = { + .bytes_written =0 , + .encountered_page_in_swapcache = false, + }; +unsigned long nr_remaining = nr_to_scan; +int nid;if (!mem_cgroup_zswap_writeback_enabled (memcg))return -ENOENT ;@@ -1290,24 +1323,40 @@ static int shrink_memcg(struct mem_cgroup *memcg)return -ENOENT ;for_each_node_state (nid,N_NORMAL_MEMORY ) { -unsigned long nr_to_walk =1 ; +unsigned long nr_to_walk; - shrunk +=list_lru_walk_one (&zswap_list_lru, nid, memcg, - &shrink_memcg_cb,NULL , &nr_to_walk); - scanned +=1 - nr_to_walk; +/* +* Cap the scan at per-node LRU length so each entry is scanned +* at most once per call. +*/ + nr_to_walk =min (nr_remaining, +list_lru_count_one (&zswap_list_lru, nid, memcg)); +if (!nr_to_walk) +continue ; + + nr_remaining -= nr_to_walk; +list_lru_walk_one (&zswap_list_lru, nid, memcg, &shrink_memcg_cb, + &walk_arg, &nr_to_walk); +/* Return the unused share of the budget to the pool. */ + nr_remaining += nr_to_walk; + +if (!nr_remaining) +break ; } -if (!scanned) +/* Nothing was scanned: every LRU under @memcg was empty. */ +if (nr_remaining == nr_to_scan)return -ENOENT ; -return shrunk ?0 : -EAGAIN ; +return walk_arg.bytes_written; }static void shrink_worker (struct work_struct *w) {struct mem_cgroup *memcg; -int ret, failures =0 , attempts =0 ; +int failures =0 , attempts =0 ;unsigned long thr; +long ret;/* Reclaim down to the accept threshold */ thr =zswap_accept_thr_pages ();@@ -1369,7 +1418,7 @@ static void shrink_worker(struct work_struct *w) goto resched; } - ret =shrink_memcg (memcg); + ret =shrink_memcg (memcg,NR_ZSWAP_WB_BATCH );/* drop the extra reference */ mem_cgroup_put (memcg);@@ -1383,7 +1432,7 @@ static void shrink_worker(struct work_struct *w)continue ; ++attempts; -if (ret && ++failures ==MAX_RECLAIM_RETRIES ) +if (ret <=0 && ++failures ==MAX_RECLAIM_RETRIES )break ; resched:cond_resched ();@@ -1493,7 +1542,7 @@ bool zswap_store(struct folio *folio) objcg =get_obj_cgroup_from_folio (folio);if (objcg && !obj_cgroup_may_zswap (objcg)) { memcg =get_mem_cgroup_from_objcg (objcg); -if (shrink_memcg (memcg)) { +if (shrink_memcg (memcg,1 ) <=0 ) {mem_cgroup_put (memcg); goto put_objcg; } --2.34.1
Re: [PATCH 2/2] mm/zswap: Support batch writeback in shrink_memcg()
Quoted 4 lines · Yosry Ahmed
Quoted 3 lines · Hao Jia
From: Hao Jia <[email protected]>
Quoted 8 lines
Quoted 3 lines
Currently,
shrink_memcg()writes back at most one entry per-node during its traversal. This makesshrink_worker()inefficient, as it must repeatedly re-entershrink_memcg()to make any substantial progress.Please also mention the case about writeback being slow to keep up with refaults in some cases, leading to zswap store failures and pages skipping zswap and going directly to disk, which is an LRU inversion.
Will be done in the next version.
Quoted 13 lines
Quoted 7 lines
To address this, extend
shrink_memcg()and rewrite its LRU iteration logic to support batch writeback. Introduce thenr_to_scanparameter to bound how many pages are scanned per call. This enables batch writeback in theshrink_worker()path, while maintaining a low scan budget in thezswap_store()path.Quoted 5 lines
Additionally, to prepare for future proactive writeback, update the return value semantics of
shrink_memcg(): a positive value now represents the actual number of compressed bytes written back, 0 indicates that candidates existed but no writeback succeeded, and a negative value represents an error code.
Quoted 4 lines
This part should be dropped for now, and added with the proactive writeback, as it's currently unused AFAICT. Removing
zswap_shrink_walk_argwill simplify the patch and make it focused on the batching part.
Will be done in the next version.
Quoted 20 lines
Quoted 20 lines
Test Setup:
Total memory: 32 GB.
zswap settings: max_pool_percent=1, accept_threshold_percent=50, shrinker_enabled=N.
Allocate 512MB of anonymous pages and fill them with random data (to avoid compression), then use cgroup memory.reclaim to force a large amount of anonymous pages into zswap. At an interval of 2ms, allocate a 4K anonymous page where the first 4 bytes are random numbers and the rest are zeros, and then trigger a reclamation of this 4K anonymous page through cgroup memory.reclaim. When the pool threshold is reached,shrink_memcg()will be triggered.The test data after running for 120s is as follows:
Baseline Patchedshrink_workerwakeups 5363 85shrink_memcgcalls 11,345,012 188,264written_back40214 40275
Quoted 7 lines
Quoted 3 lines
Conclusion:
Under the same workload and run duration, the patched kernel shows a significant reduction in bothshrink_workerwakeups andshrink_memcgcalls.Please also include data from the case where zswap store failures are observed and pages go to disk, and compare before and after this patch. I think that part is also really important.
I retested and added some collected information. Perhaps pool_limit_hit and pswpout can explain that batch shrinking of zswap can reduce the number of pages that fail to be stored due to the pool limit, allowing zswap to skip zswap and go directly to disk.
Baseline Patchedshrink_worker wakeups 5,363 85shrink_memcg calls 11,373,201 180,928written_back pages 40,212 40,236
zswap_store calls 161,190 168,741 store succeeded (ret=1) 102,743 127,644 store rejected (ret=0) 58,447 41,097 store reject rate ~36% ~24% pool_limit_hit delta 55,826 14,062 pswpout 98,659 81,333
pswpin 2 1
Thanks,
Hao
Yosry Ahmed
maintainerRe: [PATCH 2/2] mm/zswap: Support batch writeback in shrink_memcg()
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Test Setup:
Total memory: 32 GB.
zswap settings: max_pool_percent=1, accept_threshold_percent=50, shrinker_enabled=N.
Allocate 512MB of anonymous pages and fill them with random data (to avoid compression), then use cgroup memory.reclaim to force a large amount of anonymous pages into zswap. At an interval of 2ms, allocate a 4K anonymous page where the first 4 bytes are random numbers and the rest are zeros, and then trigger a reclamation of this 4K anonymous page through cgroup memory.reclaim. When the pool threshold is reached,shrink_memcg()will be triggered.The test data after running for 120s is as follows:
Baseline Patchedshrink_workerwakeups 5363 85shrink_memcgcalls 11,345,012 188,264written_back40214 40275Conclusion:
Under the same workload and run duration, the patched kernel shows a significant reduction in bothshrink_workerwakeups andshrink_memcgcalls.
Quoted 8 lines
Quoted 3 lines
Please also include data from the case where zswap store failures are observed and pages go to disk, and compare before and after this patch. I think that part is also really important.
I retested and added some collected information. Perhaps
pool_limit_hitandpswpoutcan explain that batch shrinking of zswap can reduce the number of pages that fail to be stored due to the pool limit, allowing zswap to skip zswap and go directly to disk.
Oh I meant the other test case with high memory pressure where we saw a lot more writeback with this patch. Do you have similar data from that test case?
Quoted 16 lines
Baseline Patched
shrink_workerwakeups 5,363 85shrink_memcgcalls 11,373,201 180,928written_backpages 40,212 40,236zswap_storecalls 161,190 168,741
store succeeded (ret=1) 102,743 127,644
store rejected (ret=0) 58,447 41,097
store reject rate ~36% ~24%pool_limit_hit delta 55,826 14,062 pswpout 98,659 81,333pswpin 2 1
Thanks,
Hao
Re: [PATCH 2/2] mm/zswap: Support batch writeback in shrink_memcg()
Quoted 26 lines · Yosry Ahmed
Quoted 26 lines
Quoted 25 lines
Quoted 18 lines
Test Setup:
Total memory: 32 GB.
zswap settings: max_pool_percent=1, accept_threshold_percent=50, shrinker_enabled=N.
Allocate 512MB of anonymous pages and fill them with random data (to avoid compression), then use cgroup memory.reclaim to force a large amount of anonymous pages into zswap. At an interval of 2ms, allocate a 4K anonymous page where the first 4 bytes are random numbers and the rest are zeros, and then trigger a reclamation of this 4K anonymous page through cgroup memory.reclaim. When the pool threshold is reached,shrink_memcg()will be triggered.The test data after running for 120s is as follows:
Baseline Patchedshrink_workerwakeups 5363 85shrink_memcgcalls 11,345,012 188,264written_back40214 40275Quoted 3 lines
Conclusion:
Under the same workload and run duration, the patched kernel shows a significant reduction in bothshrink_workerwakeups andshrink_memcgcalls.Please also include data from the case where zswap store failures are observed and pages go to disk, and compare before and after this patch. I think that part is also really important.
Quoted 9 lines
Quoted 4 lines
I retested and added some collected information. Perhaps
pool_limit_hitandpswpoutcan explain that batch shrinking of zswap can reduce the number of pages that fail to be stored due to the pool limit, allowing zswap to skip zswap and go directly to disk.Oh I meant the other test case with high memory pressure where we saw a lot more writeback with this patch. Do you have similar data from that test case?
Below are the results from stress-ng (high memory pressure case), which lead to a similar conclusion:
Baseline Patchedshrink_worker wakeups 5,640 987shrink_memcg calls 8,481,500 2,504,818written_back pages 260 768,576zswap_store calls 2,742,756 2,301,414
store succeeded (ret=1) 934,640 1,308,686
store rejected (ret=0) 1,808,116 992,728 <-
store reject rate ~65% ~43%
pool_limit_hit delta 1,181,310 101,593 <- pswpout 1,808,376 1,761,304 pswpin 4,288,497 3,902,658
pswpout = store rejected + written_back pages
Note that pswpout comprises two parts: the store rejected count (where we skip zswap and go directly to disk) and the number of pages written back by shrink_memcg().
Therefore, we can directly leverage store rejected to evaluate the scenario where zswap is skipped and pages bypass it to disk. This provides a much clearer picture than looking at pswpout alone.
Do we need to include both sets of test results along with the comparison data in the final commit message?
Thanks,
Hao
Quoted 16 lines
Quoted 16 lines
Baseline Patched
shrink_workerwakeups 5,363 85shrink_memcgcalls 11,373,201 180,928written_backpages 40,212 40,236zswap_storecalls 161,190 168,741store succeeded (ret=1) 102,743 127,644 store rejected (ret=0) 58,447 41,097 store reject rate ~36% ~24% pool_limit_hit delta 55,826 14,062 pswpout 98,659 81,333pswpin 2 1
Thanks,
Hao
Nhat Pham
maintainerRe: [PATCH 2/2] mm/zswap: Support batch writeback in shrink_memcg()
Quoted 61 lines · Hao Jia
Quoted 26 lines · Yosry Ahmed
Quoted 26 lines
Quoted 25 lines
Quoted 18 lines
Test Setup:
Total memory: 32 GB.
zswap settings: max_pool_percent=1, accept_threshold_percent=50, shrinker_enabled=N.
Allocate 512MB of anonymous pages and fill them with random data (to avoid compression), then use cgroup memory.reclaim to force a large amount of anonymous pages into zswap. At an interval of 2ms, allocate a 4K anonymous page where the first 4 bytes are random numbers and the rest are zeros, and then trigger a reclamation of this 4K anonymous page through cgroup memory.reclaim. When the pool threshold is reached,shrink_memcg()will be triggered.The test data after running for 120s is as follows:
Baseline Patchedshrink_workerwakeups 5363 85shrink_memcgcalls 11,345,012 188,264written_back40214 40275Quoted 3 lines
Conclusion:
Under the same workload and run duration, the patched kernel shows a significant reduction in bothshrink_workerwakeups andshrink_memcgcalls.Please also include data from the case where zswap store failures are observed and pages go to disk, and compare before and after this patch. I think that part is also really important.
Quoted 9 lines
Quoted 4 lines
I retested and added some collected information. Perhaps
pool_limit_hitandpswpoutcan explain that batch shrinking of zswap can reduce the number of pages that fail to be stored due to the pool limit, allowing zswap to skip zswap and go directly to disk.Oh I meant the other test case with high memory pressure where we saw a lot more writeback with this patch. Do you have similar data from that test case?
Below are the results from stress-ng (high memory pressure case), which lead to a similar conclusion:
Baseline Patched
shrink_workerwakeups 5,640 987shrink_memcgcalls 8,481,500 2,504,818written_backpages 260 768,576zswap_storecalls 2,742,756 2,301,414
store succeeded (ret=1) 934,640 1,308,686
store rejected (ret=0) 1,808,116 992,728 <-
store reject rate ~65% ~43%pool_limit_hit delta 1,181,310 101,593 <- pswpout 1,808,376 1,761,304 pswpin 4,288,497 3,902,658pswpout = store rejected +
written_backpages
Note that pswpout comprises two parts: the store rejected count (where we skip zswap and go directly to disk) and the number of pages written back byshrink_memcg().
Quoted 6 lines
Therefore, we can directly leverage store rejected to evaluate the scenario where zswap is skipped and pages bypass it to disk. This provides a much clearer picture than looking at pswpout alone.
Do we need to include both sets of test results along with the comparison data in the final commit message?
I mean, it helps your case no? :)
But nicely done. My understanding is, this batching of the global shrinker reduces overall disk swap fallback, correct?
Other than Yosry's comment, the rest LGTM FWIW :) Thanks for fixing this, Hao!
Re: [PATCH 2/2] mm/zswap: Support batch writeback in shrink_memcg()
Quoted 62 lines · Nhat Pham
Quoted 61 lines · Hao Jia
Quoted 26 lines · Yosry Ahmed
Quoted 26 lines
Quoted 25 lines
Quoted 18 lines
Test Setup:
Total memory: 32 GB.
zswap settings: max_pool_percent=1, accept_threshold_percent=50, shrinker_enabled=N.
Allocate 512MB of anonymous pages and fill them with random data (to avoid compression), then use cgroup memory.reclaim to force a large amount of anonymous pages into zswap. At an interval of 2ms, allocate a 4K anonymous page where the first 4 bytes are random numbers and the rest are zeros, and then trigger a reclamation of this 4K anonymous page through cgroup memory.reclaim. When the pool threshold is reached,shrink_memcg()will be triggered.The test data after running for 120s is as follows:
Baseline Patchedshrink_workerwakeups 5363 85shrink_memcgcalls 11,345,012 188,264written_back40214 40275Quoted 3 lines
Conclusion:
Under the same workload and run duration, the patched kernel shows a significant reduction in bothshrink_workerwakeups andshrink_memcgcalls.Please also include data from the case where zswap store failures are observed and pages go to disk, and compare before and after this patch. I think that part is also really important.
Quoted 9 lines
Quoted 4 lines
I retested and added some collected information. Perhaps
pool_limit_hitandpswpoutcan explain that batch shrinking of zswap can reduce the number of pages that fail to be stored due to the pool limit, allowing zswap to skip zswap and go directly to disk.Oh I meant the other test case with high memory pressure where we saw a lot more writeback with this patch. Do you have similar data from that test case?
Below are the results from stress-ng (high memory pressure case), which lead to a similar conclusion:
Baseline Patched
shrink_workerwakeups 5,640 987shrink_memcgcalls 8,481,500 2,504,818written_backpages 260 768,576zswap_storecalls 2,742,756 2,301,414store succeeded (ret=1) 934,640 1,308,686 store rejected (ret=0) 1,808,116 992,728 <- store reject rate ~65% ~43% pool_limit_hit delta 1,181,310 101,593 <- pswpout 1,808,376 1,761,304 pswpin 4,288,497 3,902,658pswpout = store rejected +
written_backpages
Note that pswpout comprises two parts: the store rejected count (where we skip zswap and go directly to disk) and the number of pages written back byshrink_memcg().
Quoted 8 lines
Quoted 6 lines
Therefore, we can directly leverage store rejected to evaluate the scenario where zswap is skipped and pages bypass it to disk. This provides a much clearer picture than looking at pswpout alone.
Do we need to include both sets of test results along with the comparison data in the final commit message?
I mean, it helps your case no? :)
Yes, I will add both sets of test results to the commit message in the next version.
Quoted 5 lines
But nicely done. My understanding is, this batching of the global shrinker reduces overall disk swap fallback, correct?
Other than Yosry's comment, the rest LGTM FWIW :) Thanks for fixing this, Hao!
Thanks for the review, Nhat!
Thanks,
Hao
Yosry Ahmed
maintainerRe: [PATCH 2/2] mm/zswap: Support batch writeback in shrink_memcg()
In reply to Hao JiaQuoted 61 lines · Hao Jia
Quoted 26 lines · Yosry Ahmed
Quoted 26 lines
Quoted 25 lines
Quoted 18 lines
Test Setup:
Total memory: 32 GB.
zswap settings: max_pool_percent=1, accept_threshold_percent=50, shrinker_enabled=N.
Allocate 512MB of anonymous pages and fill them with random data (to avoid compression), then use cgroup memory.reclaim to force a large amount of anonymous pages into zswap. At an interval of 2ms, allocate a 4K anonymous page where the first 4 bytes are random numbers and the rest are zeros, and then trigger a reclamation of this 4K anonymous page through cgroup memory.reclaim. When the pool threshold is reached,shrink_memcg()will be triggered.The test data after running for 120s is as follows:
Baseline Patchedshrink_workerwakeups 5363 85shrink_memcgcalls 11,345,012 188,264written_back40214 40275Quoted 3 lines
Conclusion:
Under the same workload and run duration, the patched kernel shows a significant reduction in bothshrink_workerwakeups andshrink_memcgcalls.Please also include data from the case where zswap store failures are observed and pages go to disk, and compare before and after this patch. I think that part is also really important.
Quoted 9 lines
Quoted 4 lines
I retested and added some collected information. Perhaps
pool_limit_hitandpswpoutcan explain that batch shrinking of zswap can reduce the number of pages that fail to be stored due to the pool limit, allowing zswap to skip zswap and go directly to disk.Oh I meant the other test case with high memory pressure where we saw a lot more writeback with this patch. Do you have similar data from that test case?
Below are the results from stress-ng (high memory pressure case), which lead to a similar conclusion:
Baseline Patched
shrink_workerwakeups 5,640 987shrink_memcgcalls 8,481,500 2,504,818written_backpages 260 768,576zswap_storecalls 2,742,756 2,301,414
store succeeded (ret=1) 934,640 1,308,686
store rejected (ret=0) 1,808,116 992,728 <-
store reject rate ~65% ~43%pool_limit_hit delta 1,181,310 101,593 <- pswpout 1,808,376 1,761,304 pswpin 4,288,497 3,902,658pswpout = store rejected +
written_backpages
Note that pswpout comprises two parts: the store rejected count (where we skip zswap and go directly to disk) and the number of pages written back byshrink_memcg().
Quoted 6 lines
Therefore, we can directly leverage store rejected to evaluate the scenario where zswap is skipped and pages bypass it to disk. This provides a much clearer picture than looking at pswpout alone.
Do we need to include both sets of test results along with the comparison data in the final commit message?
Yeah, I think you can include a summarized version of both sets of results. I think the important part to highlight here is that batching results in a great reduction in store rejection rate, which translates to significantly more writeback, as the LRU ordering is maintained more correctly. We are also doing a bit less swapping from disk overall if I am reading this correctly, which means we are placing less hot data on disk as expected.
It is a bit alarming that the store rejection rate is still 43% with batching, but we can worry about this later :)
Nhat Pham
maintainerRe: [PATCH 2/2] mm/zswap: Support batch writeback in shrink_memcg()
Quoted 4 lines · Yosry Ahmed
It is a bit alarming that the store rejection rate is still 43% with batching, but we can worry about this later :)
Yeah I think it's unavoidable at the time the global shrinker is awaken, unfortunately :( We need to kick off shrinking way ahead of time if we want to avoid this, especially since the rate of zswap out is going to be faster than the rate of writeback (CPU vs IO).
Sort of why we implement the proactive reclaimers :)
Yosry Ahmed
maintainerRe: [PATCH 2/2] mm/zswap: Support batch writeback in shrink_memcg()
Quoted 7 lines · Nhat Pham
Quoted 4 lines · Yosry Ahmed
It is a bit alarming that the store rejection rate is still 43% with batching, but we can worry about this later :)
Quoted 6 lines
Yeah I think it's unavoidable at the time the global shrinker is awaken, unfortunately :( We need to kick off shrinking way ahead of time if we want to avoid this, especially since the rate of zswap out is going to be faster than the rate of writeback (CPU vs IO).
Sort of why we implement the proactive reclaimers :)
Arguably, reclaim should wait for zswap writeback and retry, similar to how allocators wakeup kswapd and retry, instead of just skipping zswap. I was actually thinking about doing this at least for the memcg limit case, eliminate the sync shrink_memcg(1) call in zswap_store() and instead wakeup an async flusher and sleep.
A problem for another day I guess :)