Re: [PATCH v4 2/5] mm/zswap: Factor writeback loop out of shrink_worker()

From: Yosry Ahmed

Date: Mon Jun 22 2026 - 19:37:12 EST


> +/*
> + * Walk the memcg tree and write back zswap pages until the
> + * (lower_pages, upper_pages) window closes, or abort encounter
> + * MAX_RECLAIM_RETRIES times of the following conditions:
> + * - No writeback-candidate memcgs found in a memcg tree walk.
> + * - Shrinking a writeback-candidate memcg failed.
> + *
> + * For shrink_worker(), it passes lower=thr and upper=zswap_total_pages().
> + * The @upper limit is refreshed in each iteration by re-evaluating
> + * zswap_total_pages(), and the window closes once the total falls
> + * below the threshold.

This is the wrong abstraction level, and it's obvious by the fact that
the function calls zswap_total_pages() again to recalcualte
'upper_pages'. It gets much worse in the next patch as well.

The lower_pages and upper_pages thing is also unnecessarily hard to
follow.

The core of the reuse here is the retry logic. So maybe keep the memcg
iteration in the callers, and define a function that takes in one memcg
and reclaims one batch from it? failures and attempts can be passed into
the function to maintain the state across scans of different memcgs,
like zswap_shrink_walk_arg?

WDYT?

> + */
> +static void zswap_try_to_writeback(unsigned long lower_pages,
> + unsigned long upper_pages)
> +{
> + int failures = 0, attempts = 0;
> + struct mem_cgroup *iter_memcg;
> +
> + while (lower_pages < upper_pages) {
> + unsigned long batch_size;
> + long shrunk;
>
> - if (!memcg) {
> + cond_resched();
> +
> + iter_memcg = zswap_iter_global();
> + if (!iter_memcg) {
> /*
> * Continue shrinking without incrementing failures if
> * we found candidate memcgs in the last tree walk.
> @@ -1443,12 +1457,16 @@ static void shrink_worker(struct work_struct *w)
> break;
>
> attempts = 0;
> - goto resched;
> + continue;
> }
>
> - ret = shrink_memcg(memcg, NR_ZSWAP_WB_BATCH);
> + batch_size = min(upper_pages - lower_pages, NR_ZSWAP_WB_BATCH);
> + shrunk = shrink_memcg(iter_memcg, batch_size);
> /* drop the extra reference */
> - mem_cgroup_put(memcg);
> + mem_cgroup_put(iter_memcg);
> +
> + /* zswap total pages might have changed, refresh it. */
> + upper_pages = zswap_total_pages();
>
> /*
> * There are no writeback-candidate pages in the memcg.
> @@ -1456,15 +1474,23 @@ static void shrink_worker(struct work_struct *w)
> * with pages in zswap. Skip this without incrementing attempts
> * and failures.
> */
> - if (ret == -ENOENT)
> + if (shrunk == -ENOENT)
> continue;
> ++attempts;
>
> - if (ret <= 0 && ++failures == MAX_RECLAIM_RETRIES)
> + if (shrunk <= 0 && ++failures == MAX_RECLAIM_RETRIES)
> break;
> -resched:
> - cond_resched();
> - } while (zswap_total_pages() > thr);
> + }
> +}
> +
> +static void shrink_worker(struct work_struct *w)
> +{
> + unsigned long thr;
> +
> + /* Reclaim down to the accept threshold */
> + thr = zswap_accept_thr_pages();
> +
> + zswap_try_to_writeback(thr, zswap_total_pages());
> }
>
> /*********************************
> --
> 2.34.1
>