From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F00063C0A0F for ; Mon, 29 Jun 2026 11:21:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782732071; cv=none; b=d0B6CunDW1huc0VCveeT/3YYv0mVkr5vPfjLB4WPQ8mpNHod61a94Aknwr9WzAr2rUh2OGP92VUYiWWQd620Z0BoHq6hsR8KYPrwBp6SvYImfMZpuZbSfm+i9kcRHlcv3xquG1RcOf019q8XGOhtlSxcE4uup0AolJfIYOblF/A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782732071; c=relaxed/simple; bh=wIACqre+bEPqLGzHTLWsUym9jduABCcu6rw/oU6Ua9Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NdGDtvViZq4NI8+MRpMSNfWJoOuT5zm3/rQ1gyHBA7pYOO60pWBJX1je45Uj6Sk+ce6TbzFvFufifHRU6qYl+yTm0pYXh8DgXsr7FBgqDPaYi4JlEuTlGIGkjsUKRlz4wDb7SUT1gwUUJpIBB5uwvtkipKXJ4Y3in51Q1+0gA30= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=V72NNWjt; arc=none smtp.client-ip=209.85.214.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="V72NNWjt" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-2c82538b6c0so16789205ad.2 for ; Mon, 29 Jun 2026 04:21:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782732069; x=1783336869; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sO4sdWL/oCz6QEMC2wKysJsfeihvrWNSztOGuVnk/ZY=; b=V72NNWjtqYcJEHCSWTPe4fD3VWPFAuFrpJCt8tZX2kvF25tXFPJWhfKdbCe5Ab8l4U VdAc6K5ZvLc6Fg12HsEpaWfnZDebpQ7v1Zb021N7+ETN2J8F7xdkhdRS36u/Gmc1HuYP nQYkyss3TmKk6oaCda2FVHMaVVY5o1RwnwWrGfNBnssS4AbfZDnjZOjQRLj+XahQPYCP fXjwaqGza335hT0w0C32Z2+mQoHJWhC/wE0nt/rJrShDr43V06jeMI6araJJsR8Jiipx NdFe+dFy756UJVj6bK1owrQc+FXNhoUPPxAiABB/OCbYNI6tqsNbQggBFQgGLXzIPLC1 LCfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782732069; x=1783336869; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=sO4sdWL/oCz6QEMC2wKysJsfeihvrWNSztOGuVnk/ZY=; b=kYqJYD7bLhXdRh3GhcnYYn9nRPkTS+9x1tCeHzpjJ2ZnTAKJBfajc1GQL7669CqwCJ FItPsjh6PN6ks3s3hz1FGVAeLNkFWG/Z+yzVmGFrvI2lifdo+myhp5HBqmdqdFx7MYHs ejquhijRJGDccNdfBd9eeRVO4EqSB6dim2XmMP4OXS7vGOns3dliHbL6AC2qn52xNA10 1WaXQfZte8+RwkjZqEKLhp5BXPgvIGjvxHqKBA1gBlIhOwcgWJ81U4muFxWHJmbq4Pk5 CG4TUnU0VYH1pM5gvrTgxdIAZx61f294QfkjftYLEyqhn2i8LALpmjwuhblt7DEpFtlb IuPw== X-Forwarded-Encrypted: i=1; AHgh+RpHefxwQUtIFEfIk4DU0SXVPrCk8a8KebwkbI2kyxSq82+gBmbb54KexXXNdi3tSQFh43jjY+d/3AYxB6A=@vger.kernel.org X-Gm-Message-State: AOJu0Yx+GVxeTzki3krYYKxxQOXtBKnJt7tpkeL34hSYVRVfZh0T7d8w LJ7J7pGt4PjnpfvkajzaC5eMnXjCV6HovRPL08DvoEOxS2IwwPeaormj X-Gm-Gg: AfdE7cmLdCVZVPS6GEzrd+DwIwPf7s9v7zFzf9dRLkOEQYOsSLgcLPw2zvRHIe8/rRC A3obLTtNaGLXo7Q2Vbh0U0f2JgcUJg4g88Y8TUFfxlEMaQKiEpl+c8HzvUtb8XaoyNRmPtd25Hi SkISDr8A0TgpG1HV607fhYo974H6cVK1QMHr1lGjDLwBvNt3NBah89NUckTJNq+djf3x0rA5SH3 ILBFnWsZpvIR2yHwC/LsuhJF4H224C0NZ0uViRPvVrVxJpxlgICQQKzMZzY1ATJHJ1HkA9Du5af d60Yl+TrCZ7Hhxn0/jZ+oFdkNU5HB3ExLpjhNvmhJB9MtmB2jLAOu4X0ZA34fYhyFjV1gbc0lYa 8ZO2hypMzN3VEoW1p4uEzHZwhy8sybBeikox8B7jpLzHNAWdSlevIwidt88iPsA5wbCuO2hpaE8 D/Uyswk2zTLzyUArpiErDWGv7C1uRcpIgI6KNhARiw X-Received: by 2002:a17:902:d4c5:b0:2ca:be8:62d with SMTP id d9443c01a7336-2ca0be80814mr19308125ad.32.1782732069117; Mon, 29 Jun 2026 04:21:09 -0700 (PDT) Received: from localhost.localdomain ([210.184.73.204]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2c7f63d09f0sm92759085ad.56.2026.06.29.04.21.03 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 29 Jun 2026 04:21:08 -0700 (PDT) From: Hao Jia To: akpm@linux-foundation.org, tj@kernel.org, hannes@cmpxchg.org, shakeel.butt@linux.dev, mhocko@kernel.org, yosry@kernel.org, mkoutny@suse.com, nphamcs@gmail.com, chengming.zhou@linux.dev, muchun.song@linux.dev, roman.gushchin@linux.dev Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Hao Jia Subject: [PATCH v5 3/6] mm/zswap: Extract a reusable writeback helper from shrink_worker() Date: Mon, 29 Jun 2026 19:20:29 +0800 Message-Id: <20260629112032.20423-4-jiahao.kernel@gmail.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20260629112032.20423-1-jiahao.kernel@gmail.com> References: <20260629112032.20423-1-jiahao.kernel@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Hao Jia Extract a reusable writeback helper zswap_shrink_one_memcg() from shrink_worker(). This helper will be reused by the upcoming proactive writeback feature. zswap_shrink_one_memcg() takes one step of a memcg-tree writeback walk driven by the caller's iterator. Consequently, shrink_worker() now only needs to calculate the acceptance threshold, drive its own iteration based on this helper, and abort the walk when zswap_shrink_one_memcg() returns -EBUSY. Suggested-by: Yosry Ahmed Signed-off-by: Hao Jia --- mm/zswap.c | 118 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 69 insertions(+), 49 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index e2c2a3f1e061..ba01bf0e44e9 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1351,12 +1351,70 @@ static long shrink_memcg(struct mem_cgroup *memcg, unsigned long nr_to_scan) return walk_arg.bytes_written; } +/* Track progress of a memcg-tree writeback walk. */ +struct zswap_shrink_state { + int scans; + int failures; +}; + +/* + * Take one step of a memcg-tree writeback walk driven by the caller's + * iterator, and fold the result into @s, the retry bookkeeping shared + * across steps. @memcg is the iterator's current memcg, or NULL once + * it has wrapped around after a full pass over the tree. + * + * The function returns -EBUSY to signal the caller to abort the walk after + * encountering either of the following MAX_RECLAIM_RETRIES times: + * - No writeback-candidate memcgs were found in a memcg tree walk. + * - Shrinking a writeback-candidate memcg failed. + * + * Return: The number of compressed bytes written back (>= 0), or -EBUSY + * when the caller should abort the walk. + */ +static long zswap_shrink_one_memcg(struct mem_cgroup *memcg, + struct zswap_shrink_state *s) +{ + long shrunk; + + /* + * Reaching a NULL memcg means a full hierarchy pass completed. + * Exclude the memcg-disabled case, where it is always NULL, and + * fall through to shrink the root LRU directly. + */ + if (!memcg && !mem_cgroup_disabled()) { + /* + * Continue shrinking without incrementing failures if we found + * candidate memcgs in the last tree walk. + */ + if (!s->scans && ++s->failures == MAX_RECLAIM_RETRIES) + return -EBUSY; + s->scans = 0; + return 0; + } + + shrunk = shrink_memcg(memcg, NR_ZSWAP_WB_BATCH); + + /* + * There are no writeback-candidate pages in the memcg. With memcg + * enabled this is not an issue as long as we can find another memcg + * with pages in zswap, so skip without counting it as a candidate. + * With memcg disabled the root LRU is the only target, so we should + * abort if it has no writeback-candidate pages. + */ + if (shrunk == -ENOENT) + return mem_cgroup_disabled() ? -EBUSY : 0; + s->scans++; + + if (shrunk <= 0 && ++s->failures == MAX_RECLAIM_RETRIES) + return -EBUSY; + + return shrunk; +} + static void shrink_worker(struct work_struct *w) { - struct mem_cgroup *memcg; - int failures = 0, attempts = 0; + struct zswap_shrink_state s = {}; unsigned long thr; - long ret; /* Reclaim down to the accept threshold */ thr = zswap_accept_thr_pages(); @@ -1367,11 +1425,6 @@ static void shrink_worker(struct work_struct *w) * writeback-disabled memcgs (memory.zswap.writeback=0) are not * candidates for shrinking. * - * Shrinking will be aborted if we encounter the following - * MAX_RECLAIM_RETRIES times: - * - No writeback-candidate memcgs found in a memcg tree walk. - * - Shrinking a writeback-candidate memcg failed. - * * We save iteration cursor memcg into zswap_next_shrink, * which can be modified by the offline memcg cleaner * zswap_memcg_offline_cleanup(). @@ -1386,7 +1439,11 @@ static void shrink_worker(struct work_struct *w) * offline memcg left in zswap_next_shrink will hold the reference * until the next run of shrink_worker(). */ - do { + while (zswap_total_pages() > thr) { + struct mem_cgroup *memcg; + long ret; + + cond_resched(); /* * Start shrinking from the next memcg after zswap_next_shrink. * When the offline cleaner has already advanced the cursor, @@ -1405,49 +1462,12 @@ static void shrink_worker(struct work_struct *w) } while (memcg && !mem_cgroup_tryget_online(memcg)); spin_unlock(&zswap_shrink_lock); - /* - * Reaching a NULL memcg means a full hierarchy pass completed. - * Exclude the memcg-disabled case, where it is always NULL, and - * fall through to shrink the root LRU directly. - */ - if (!memcg && !mem_cgroup_disabled()) { - /* - * Continue shrinking without incrementing failures if - * we found candidate memcgs in the last tree walk. - */ - if (!attempts && ++failures == MAX_RECLAIM_RETRIES) - break; - - attempts = 0; - goto resched; - } - - ret = shrink_memcg(memcg, NR_ZSWAP_WB_BATCH); + ret = zswap_shrink_one_memcg(memcg, &s); /* drop the extra reference */ mem_cgroup_put(memcg); - - /* - * There are no writeback-candidate pages in the memcg. - * This is not an issue as long as we can find another memcg - * with pages in zswap. Skip this without incrementing attempts - * and failures. - */ - 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 <= 0 && ++failures == MAX_RECLAIM_RETRIES) + if (ret == -EBUSY) break; -resched: - cond_resched(); - } while (zswap_total_pages() > thr); + } } /********************************* -- 2.34.1