From patchwork Wed Jul 31 00:49:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takero Funaki X-Patchwork-Id: 13747987 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 4DDBFC3DA7F for ; Wed, 31 Jul 2024 00:49:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B00836B0089; Tue, 30 Jul 2024 20:49:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AAEF06B008A; Tue, 30 Jul 2024 20:49:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 951AF6B0092; Tue, 30 Jul 2024 20:49:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 6C5806B0089 for ; Tue, 30 Jul 2024 20:49:33 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 1835D140162 for ; Wed, 31 Jul 2024 00:49:33 +0000 (UTC) X-FDA: 82398214626.03.67600B0 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) by imf30.hostedemail.com (Postfix) with ESMTP id 417FD80006 for ; Wed, 31 Jul 2024 00:49:31 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=OBpRPZ8O; spf=pass (imf30.hostedemail.com: domain of flintglass@gmail.com designates 209.85.210.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=1722386916; 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=iHWVkWtAJLeMRIP1sg21d7QalxJky60cE7fLnsOK5JE=; b=ZB1h38EMis9JIKJxFSquI/YVkHgj1Jbwv6ZDDoGzLRazlD5Tq2DF62N7u31nQ3yUJ6s+dc 3eHzYSUJ1fKZs230BOUsRN2fp6PTi8oGUQHXLc7Vju2A6l2HZLIs9PlfxxS+HVgD8AfB6D cf9FyGZbSHgpUaNiOMZTAQVnW+s18YY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722386916; a=rsa-sha256; cv=none; b=q7fc8u+cKpUMc8lB3HDab84RuBGSHDMJlw6+evKCaHyi8IxjmjMf3JgHP55mb6f6TdQYa0 yZtkeWqP7fWxDQdoW7FgPesl6E5IdCywhq1ov5Pd97SNp2ViwH/fvbquQfRuWVvpNOgswp YFvvHGskxCcqPbTU1ffYVLhTKRHvG3Y= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=OBpRPZ8O; spf=pass (imf30.hostedemail.com: domain of flintglass@gmail.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=flintglass@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-70d18112b60so359469b3a.1 for ; Tue, 30 Jul 2024 17:49:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722386970; x=1722991770; 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=iHWVkWtAJLeMRIP1sg21d7QalxJky60cE7fLnsOK5JE=; b=OBpRPZ8OCU/6DEFYl6PFh1tHdp1Ht8oyzZZvEWTyo579ohL97fFd7QOvZjvwQJrwi3 UBJ4CIc9tI/1+Ix9tf6OUk8UCU6q11TeZMFfKpInNTeZF/d9WSXx3S3PYtRqCGe4yu9c hj42f28cH/wSOY1Qa3pBmArF1edKWf1sftPB7AHjxC5BpAOzUhxoyJfkBxIUW5EEcDY/ tAxiN9FoXWaTiQJY32UmDkUP+GRbyckgc1QTF19U5nfqShrZkfnM0K2g6f6QL53Xh69d oQncFT+ae6hVWBvpK2RFDH3iqnF4uAsKN2cfbMEhzx/TEVaAezWX8b+oyPN8jwRDTVRg 4KCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722386970; x=1722991770; 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=iHWVkWtAJLeMRIP1sg21d7QalxJky60cE7fLnsOK5JE=; b=SFS+etMUbEXx67uZeUzBZgv0lkQ5jdJAY499SxLsh8UwhTz8tpM0fXCfx4F3O3Z4Rq elaeHPeh291y5wYYNUYOWkjYZegDV7Jy16Vlxw//31ZnWbIwp6T4ffOC6oAvJEGv/0jl LfesOBDqpjK0nkHOm1IQ/FeN0ZDuV0sNaxc6eBi/0at8ZEBARSxpe4cF7+DI+b7E3ooK +EGLgyli/ja/G6r1ANzHqK86hy26j1TTkcTY+jvV+ubQzRpYXEcvX+qdfIwO0r0CURIv uR3Qb7WlGUgnHfltqnbujqRP8oS4rJjS2eJ4x6miGmqS3gvAl1VM+pSED2dmHzEJ6JDH W5Dg== X-Forwarded-Encrypted: i=1; AJvYcCX6A6DJsN1Mwg/Bor5zgqaJFvBSJi9fKmybgyuSQZPsNFuG5axrd9Ea5y6FN8ex3TX6ozQMUyTuA8puiIfTVDjlJgU= X-Gm-Message-State: AOJu0YxfDiY/aDoLp3jtcS0Oh278p5Hh1NFd6ndqFa2+dRUaMkRpwu/2 AzKnH7029VVOlXpfF0gPZTEscLSml/iAoWQhHSCRSVzJui/Jhnb3 X-Google-Smtp-Source: AGHT+IGxh+nOGAXTweiYx7CpvYGWrlgN26RD1qgQah0xJQsagfw5xIafFAFOhPQthqNbvggCal/Chw== X-Received: by 2002:a05:6a00:2ea3:b0:70e:aa44:39df with SMTP id d2e1a72fcca58-70efe4074c5mr6088259b3a.1.1722386969877; Tue, 30 Jul 2024 17:49:29 -0700 (PDT) Received: from localhost.localdomain (h101-111-009-128.hikari.itscom.jp. [101.111.9.128]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70ead8837edsm9344745b3a.148.2024.07.30.17.49.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jul 2024 17:49:29 -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 v5 2/2] mm: zswap: fix global shrinker error handling logic Date: Wed, 31 Jul 2024 00:49:10 +0000 Message-ID: <20240731004918.33182-3-flintglass@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240731004918.33182-1-flintglass@gmail.com> References: <20240731004918.33182-1-flintglass@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 417FD80006 X-Stat-Signature: 4d9ysg4pr3z5tazznku1oogbmnmhdjys X-HE-Tag: 1722386971-235445 X-HE-Meta: U2FsdGVkX19ViHM7VVL4Ju3tBSgYGgLMRnbkE85MDQCfCaRm9ssteanhJ6wVr/oBGCIXSJykU3Kw7t3caBCts8NsNqU9rUS7UNqCtVi7+ZmYPqjOZHW1vihAHvbjN8DgTQPHB4ALYLaARZHNXZP30fteMRFcbGcxExNAX7K+AsZ6qxSkPGTBBOmFdC4nRjkmzVQS1pmLTbaOdemqn/PcSnAZrXr0k+wZusf7T5iFDWd5tF8LpZBp+CYqhDU1YGbHNb9I3Urd4tpzgDAT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ity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 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 Reviewed-by: Chengming Zhou Reviewed-by: Nhat Pham --- mm/zswap.c | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 3c16a1192252..d46caa42ed4f 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1287,10 +1287,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 @@ -1304,21 +1304,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 @@ -1356,9 +1369,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; } @@ -1366,8 +1384,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: