From patchwork Sat Jul 27 23:06:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takero Funaki X-Patchwork-Id: 13743807 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B728AC3DA4A for ; Sat, 27 Jul 2024 23:06:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 44E146B0088; Sat, 27 Jul 2024 19:06:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3FC6F6B0089; Sat, 27 Jul 2024 19:06:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 279BC6B008A; Sat, 27 Jul 2024 19:06:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 022306B0088 for ; Sat, 27 Jul 2024 19:06:54 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 710EDC01C8 for ; Sat, 27 Jul 2024 23:06:54 +0000 (UTC) X-FDA: 82387069548.06.B7A0492 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf20.hostedemail.com (Postfix) with ESMTP id 9B5171C0003 for ; Sat, 27 Jul 2024 23:06:52 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=UVQ9BISp; spf=pass (imf20.hostedemail.com: domain of flintglass@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=flintglass@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722121586; a=rsa-sha256; cv=none; b=6veJgAcltbKzxF+hVQme40busH6nGDms+m3dMn5647QdjDREPvY7ir+FWRDr32HK5HVTeK 2TOyjvqNkYSvV45SF81YNcl7erqleSu7eYmXV/lsbZrcR9/6p2Ns8Hbs/GDv44axKHor/M vCJqtnHrtKEmRKYdM2BUt/Kfvv14L+4= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=UVQ9BISp; spf=pass (imf20.hostedemail.com: domain of flintglass@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=flintglass@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722121586; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Yd+/PkNsbnZGnxyGV+P8ksi9diuCZ2xuAbwQqloCs2E=; b=YrVwGEWRsiutgTxcr+00C7XKJbUB2f6IlT4NmNPUTDeiRYh3WrmEbnh9ipfqVhRQiIEN1d CqT7PENIeKZW3ETB6GAfA2GeYDQLSbS863LpaXyuvWIPKB81giR2iXL5Oy0K2+IVPIse5w BE5/9bVPAQG/C85DMDgXmKo0sMTBEUo= Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1fc611a0f8cso13073755ad.2 for ; Sat, 27 Jul 2024 16:06:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722121611; x=1722726411; darn=kvack.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=Yd+/PkNsbnZGnxyGV+P8ksi9diuCZ2xuAbwQqloCs2E=; b=UVQ9BISpfreTdNwotGS95uzSudx4eFCg3s5adSqUpmm/x93RUDw50iBPao/YTJSwwg f5Vs0kEfp/xUNmXqgXusUw5ijq2igi4r7vZ3GTDZRbbvx/ojmy4QcaeXIVJYwjuHSrtl EL+gscCw/M1imCDekihOFNjFYhCL/S55Lcpq+5woiF+InsMDQvYDWdWNx6HVcI+tzxuy qc8G+A7TpLEXzp6aJJKhQeBJUl6gSq7ni9O0+ERHja4I0yHN4q2EKdObo1VqFHBEmvPS 31u9SAFKsUW7nVzc4lv1iWLXfD3LTFuRpNSVjvm7vqX4tTKl9q0Wz+7LgQ8N1HdpzeZM Qblg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722121611; x=1722726411; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Yd+/PkNsbnZGnxyGV+P8ksi9diuCZ2xuAbwQqloCs2E=; b=cVJx0brOfMuHEhaUWjvMxk6Pqczs+bs/kH9VTXSAfPykk2Id8DuajD9rZZvloxWl28 u6fYYX1VyMOFyQKkjEobvbf1B7S6K4wfY5Xy4pBL8y85CKZXGnPjdyS6kEcdNfOmu/Ln 7vKief8uwyD8bPiUjNI2IXk8P6dKbIWq5gDX4X/sDbNBiFL0NSc/pEzVXhZ/kgibY8bq bxFmAjGK+KbsfSuf9JXZ2tZBxpBk3JqGZuwQvZrn87UhyYz+fISjl1/xJFkwYNpgw9Ar LhcnItbhHWQXuEPz+XaRpUQhWaImsxu6ZPsiVmlHSN2O9gMDZHCAdnq0lDsVcIXi2k4j OUZA== X-Forwarded-Encrypted: i=1; AJvYcCW3e8cshIbdKKhWD9cR15v4ydghKFb5RslR0olF/TduLf0gXEWHdLtMQs14sAgiWbcNwLVD2vSipD+IIYUXPc+l4V0= X-Gm-Message-State: AOJu0Yw6PifT7r50BZuspdAIzhO5y7baomDuuaC4gJJ7K4ZFQcYr+ynR 5n5XK/KSoo5mz+iRLk75PIdFJ5P/XjKtExgqndhZSt661NojzH3t X-Google-Smtp-Source: AGHT+IF/8gi3Mz4DsUNG9UiAYAkoJ0FEr706604n2lQS02Byp3AL/IAe8zts+2DvzxbR9MZ9cSUcCg== X-Received: by 2002:a17:902:e5ca:b0:1fb:3d7:1d01 with SMTP id d9443c01a7336-1ff048e4fd6mr30034875ad.59.1722121611214; Sat, 27 Jul 2024 16:06:51 -0700 (PDT) Received: from cbuild.incus (h101-111-009-128.hikari.itscom.jp. [101.111.9.128]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7f274a7sm55881145ad.209.2024.07.27.16.06.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Jul 2024 16:06:50 -0700 (PDT) From: Takero Funaki To: Johannes Weiner , Yosry Ahmed , Nhat Pham , Chengming Zhou , Andrew Morton Cc: Takero Funaki , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/2] mm: zswap: fix global shrinker error handling logic Date: Sat, 27 Jul 2024 23:06:30 +0000 Message-ID: <20240727230635.3170-3-flintglass@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240727230635.3170-1-flintglass@gmail.com> References: <20240727230635.3170-1-flintglass@gmail.com> MIME-Version: 1.0 X-Stat-Signature: uf85fdfzhf8tmrescuiwa7o7g7qjcem8 X-Rspamd-Queue-Id: 9B5171C0003 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1722121612-488276 X-HE-Meta: U2FsdGVkX1/Qnz7OrnLa0lWXjyc9k0jFWJMT+/2SmNYgX47GcX86V6mjhSMS/+cX19H6aF3lu+3QlF0Ny2y0J4jmcDinr6s67j5Xo5RoXX+oLZQurzxGMW2Kl9ICxV87fVV3qoDZv4MDrLDYZJMK/Y3tzVHH5IfT1pKWrul22fFN7eFqHHTni99ybqESBpyRdZ9m9dI2W9Of5cagekW8ilDN/GwG9so+56rXMWRPLjRh/L7vxZVRsl5psiKHlUUg6ARfO6LdhQlO0y0Bxw47ducn4e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Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This patch fixes the zswap global shrinker, which did not shrink the zpool as expected. The issue addressed is that shrink_worker() did not distinguish between unexpected errors and expected errors, such as failed writeback from an empty memcg. The shrinker would stop shrinking after iterating through the memcg tree 16 times, even if there was only one empty memcg. With this patch, the shrinker no longer considers encountering an empty memcg, encountering a memcg with writeback disabled, or reaching the end of a memcg tree walk as a failure, as long as there are memcgs that are candidates for writeback. Systems with one or more empty memcgs will now observe significantly higher zswap writeback activity after the zswap pool limit is hit. To avoid an infinite loop when there are no writeback candidates, this patch tracks writeback attempts during memcg tree walks and limits reties if no writeback candidates are found. To handle the empty memcg case, the helper function shrink_memcg() is modified to check if the memcg is empty and then return -ENOENT. Fixes: a65b0e7607cc ("zswap: make shrinking memcg-aware") Signed-off-by: Takero Funaki --- mm/zswap.c | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index e9b5343256cd..60c8b1232ec9 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1293,10 +1293,10 @@ static struct shrinker *zswap_alloc_shrinker(void) static int shrink_memcg(struct mem_cgroup *memcg) { - int nid, shrunk = 0; + int nid, shrunk = 0, scanned = 0; if (!mem_cgroup_zswap_writeback_enabled(memcg)) - return -EINVAL; + return -ENOENT; /* * Skip zombies because their LRUs are reparented and we would be @@ -1310,20 +1310,34 @@ static int shrink_memcg(struct mem_cgroup *memcg) shrunk += list_lru_walk_one(&zswap_list_lru, nid, memcg, &shrink_memcg_cb, NULL, &nr_to_walk); + scanned += 1 - nr_to_walk; } + + if (!scanned) + return -ENOENT; + return shrunk ? 0 : -EAGAIN; } static void shrink_worker(struct work_struct *w) { struct mem_cgroup *memcg; - int ret, failures = 0; + int ret, failures = 0, attempts = 0; unsigned long thr; /* Reclaim down to the accept threshold */ thr = zswap_accept_thr_pages(); - /* global reclaim will select cgroup in a round-robin fashion. + /* + * Global reclaim will select cgroup in a round-robin fashion from all + * online memcgs, but memcgs that have no pages in zswap and + * 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 @@ -1361,9 +1375,14 @@ static void shrink_worker(struct work_struct *w) spin_unlock(&zswap_shrink_lock); if (!memcg) { - if (++failures == MAX_RECLAIM_RETRIES) + /* + * 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; } @@ -1371,8 +1390,16 @@ static void shrink_worker(struct work_struct *w) /* drop the extra reference */ mem_cgroup_put(memcg); - if (ret == -EINVAL) - break; + /* + * 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) + continue; + ++attempts; + if (ret && ++failures == MAX_RECLAIM_RETRIES) break; resched: