From patchwork Sat Jul 20 04:41:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takero Funaki X-Patchwork-Id: 13737609 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 98857C3DA59 for ; Sat, 20 Jul 2024 04:41:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D7C496B0085; Sat, 20 Jul 2024 00:41:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D4FCA6B0088; Sat, 20 Jul 2024 00:41:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BC98C6B0089; Sat, 20 Jul 2024 00:41:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 9CD6E6B0085 for ; Sat, 20 Jul 2024 00:41:41 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 3D11C40AA3 for ; Sat, 20 Jul 2024 04:41:41 +0000 (UTC) X-FDA: 82358882802.13.3E31BE9 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf15.hostedemail.com (Postfix) with ESMTP id 6733FA0003 for ; Sat, 20 Jul 2024 04:41:39 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nlM+9h8G; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf15.hostedemail.com: domain of flintglass@gmail.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=flintglass@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721450451; 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=he3tUu8hce94zy7aE7UWp0ZYRUcHVjdNDmthPwwFIA0=; b=VFM4WCCcxTkjcMybH4cuxvhTcX7LyAPXTkcr57H3mf8KTytxM8MHyMFUCKdwoLK6Fvf9HL wesoqgHBH0UgA5v6PrPz56sM9VoXLJCXCU57TMkrizibEltX1QAx9a/S3dl+drTGGI5ZeK hr4mgZc9CiEbx15myVGj+1G3IaeHRWg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721450451; a=rsa-sha256; cv=none; b=tFsXF9270qJmABm6Qdwyt/HRBzRyOORGU55RmAB5+AHbr4nRpdg1NschfUvusMDPaJCFdv kvIwWlGz/ecOzKzwqZmKT3VlzBsdx+eniuMVHiwX9as6lx5aCbOAXseWoi8Cau+8oX+v6t dIIFNLX9X/o/3ISBn7hAO7ah0l7BBDk= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nlM+9h8G; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf15.hostedemail.com: domain of flintglass@gmail.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=flintglass@gmail.com Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1fc5296e214so21508355ad.0 for ; Fri, 19 Jul 2024 21:41:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721450498; x=1722055298; 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=he3tUu8hce94zy7aE7UWp0ZYRUcHVjdNDmthPwwFIA0=; b=nlM+9h8Ggf5LQQOKg4ZQLniNc5R2pNrkdyRL1fKvGuaXLgj82wMtgUmyyUGoRHzJaM HJBz+VCZPueNOcAHlBFTAzfxQ9Zc+OYC5REfGQqlgo3bZPzkPIJKjMDeVX+3nWLlSBxg kYJbM2p3b+FAO6OkEctgBJ/AJb4P7unfC7oc9Oyye14ZLCIHh/MxJAVjZklCmwc+tF1X BguscAjHPsE1HjTF7PIilVNosoCLe+4cIfxVa7mpH/9UqX6mutifrPq2/aqN7w59LhdP w7pK9uBtbQcycYfUWW7emqftxBOMpAUJTWgpelDdWBHSYOBX++orI3oB6NaM3AYXMRWf QOUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721450498; x=1722055298; 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=he3tUu8hce94zy7aE7UWp0ZYRUcHVjdNDmthPwwFIA0=; b=WswTgx54spHUxpH23mrzgRxuJEG4woZXawwVBYfxetTmtNWFPI923CA3Yg3GU2Tb8U BmR/Ztw4oTaqcuqh75HdyrmaE4y6YTDzjkSV6GsBdOE24LbYzDwGsp7j1PoJzNGuiDVz KL7RwNZACRMxvuUm0fFOzZG+7EroBA8abX4CBUxevFCHEViBHUfpj+EixFlrG96S7q+P ZXWLjLFq9SfuTAueiZYLjQMOfkRs0otyEt9yMf3/faxbL8Yuwmre6vHX51ynGz1N27tI pENfPhU4Dj/BWd+3PjhheYdabhVVJ9J1OsFSQn7DZJkUvWuyrWqd31zNwVLCqMEwKJau ol8g== X-Forwarded-Encrypted: i=1; AJvYcCWTSL4JzEcauVYV3G322BlM6eFnsqB/fqNQg3qzVc94Ma+25CtObTkTxfH3ATTxBHVIPhKoXjXRU9zc1s6bZM5DUnQ= X-Gm-Message-State: AOJu0YzjbpZoqpgsjgUntrBX25jicU4nxGipfQ4/HdKBgHb3ajXhf3yG vJ5MjAJVe+WeT/tT7nD2fpb7FhyHOKQGL0btmbVBuQuhvBtOyhn5 X-Google-Smtp-Source: AGHT+IFvM2lNyXUN9LVAOKq+J4/wuDMlKQi87gVV6VjvwsGhIKSM7HeB8GT80zfsb38iH5tBT6b/VQ== X-Received: by 2002:a17:902:c793:b0:1fa:ff88:891a with SMTP id d9443c01a7336-1fd745f81b3mr11825855ad.48.1721450497927; Fri, 19 Jul 2024 21:41:37 -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-1fd6f315c11sm13023465ad.125.2024.07.19.21.41.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jul 2024 21:41:37 -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 v3 1/2] mm: zswap: fix global shrinker memcg iteration Date: Sat, 20 Jul 2024 04:41:24 +0000 Message-ID: <20240720044127.508042-2-flintglass@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240720044127.508042-1-flintglass@gmail.com> References: <20240720044127.508042-1-flintglass@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 6733FA0003 X-Stat-Signature: zehdgkyax8dnjwam3b1pa6wz4fpskgzu X-Rspam-User: X-HE-Tag: 1721450499-919892 X-HE-Meta: U2FsdGVkX19Amq7K4Ea6OelpFgPo9Spjw54t4Jo/XPNNbMEh2L+zyiBEkeI/gYInNqzRkVDIeb7T6NNWl0wPyl1TN2RCCqEmxJWpplXpeCpT5b7jHudSjNJK5VR1sWHLkBafrWI2GJBAPXyYxjnAVoOASN4TkYI93dZRgcvOAQ+5FxAETuFyrjWj6PhrYFN06tmEyawwPA2DO+swaowrbiF22G9F8JvAHEd9aCrU7QJknV/5B5JVNNL69kXNJiupxjnkxfjHHdgvqIcfNmJRLebORGjIymX4H7qdvjEOaeoIjO/tEO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ender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This patch fixes an issue where the zswap global shrinker stopped iterating through the memcg tree. The problem was that shrink_worker() would stop iterating when a memcg was being offlined and restart from the tree root. Now, it properly handles the offline memcg and continues shrinking with the next memcg. To avoid holding refcount of offline memcg encountered during the memcg tree walking, shrink_worker() must continue iterating to release the offline memcg to ensure the next memcg stored in the cursor is online. The offline memcg cleaner has also been changed to avoid the same issue. When the next memcg of the offlined memcg is also offline, the refcount stored in the iteration cursor was held until the next shrink_worker() run. The cleaner must release the offline memcg recursively. Fixes: a65b0e7607cc ("zswap: make shrinking memcg-aware") Signed-off-by: Takero Funaki Reviewed-by: Nhat Pham Acked-by: Yosry Ahmed Reviewed-by: Chengming Zhou --- mm/zswap.c | 77 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 21 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index a50e2986cd2f..6528668c9af3 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -775,12 +775,33 @@ void zswap_folio_swapin(struct folio *folio) } } +/* + * This function should be called when a memcg is being offlined. + * + * Since the global shrinker shrink_worker() may hold a reference + * of the memcg, we must check and release the reference in + * zswap_next_shrink. + * + * shrink_worker() must handle the case where this function releases + * the reference of memcg being shrunk. + */ void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) { /* lock out zswap shrinker walking memcg tree */ spin_lock(&zswap_shrink_lock); - if (zswap_next_shrink == memcg) - zswap_next_shrink = mem_cgroup_iter(NULL, zswap_next_shrink, NULL); + if (zswap_next_shrink == memcg) { + do { + zswap_next_shrink = mem_cgroup_iter(NULL, + zswap_next_shrink, NULL); + } while (zswap_next_shrink && + !mem_cgroup_online(zswap_next_shrink)); + /* + * We verified the next memcg is online. Even if the next + * memcg is being offlined here, another cleaner must be + * waiting for our lock. We can leave the online memcg + * reference. + */ + } spin_unlock(&zswap_shrink_lock); } @@ -1319,18 +1340,38 @@ static void shrink_worker(struct work_struct *w) /* 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. + * + * We save iteration cursor memcg into zswap_next_shrink, + * which can be modified by the offline memcg cleaner + * zswap_memcg_offline_cleanup(). + * + * Since the offline cleaner is called only once, we cannot leave an + * offline memcg reference in zswap_next_shrink. + * We can rely on the cleaner only if we get online memcg under lock. + * + * If we get an offline memcg, we cannot determine if the cleaner has + * already been called or will be called later. We must put back the + * reference before returning from this function. Otherwise, the + * offline memcg left in zswap_next_shrink will hold the reference + * until the next run of shrink_worker(). + */ do { spin_lock(&zswap_shrink_lock); - zswap_next_shrink = mem_cgroup_iter(NULL, zswap_next_shrink, NULL); - memcg = zswap_next_shrink; /* - * We need to retry if we have gone through a full round trip, or if we - * got an offline memcg (or else we risk undoing the effect of the - * zswap memcg offlining cleanup callback). This is not catastrophic - * per se, but it will keep the now offlined memcg hostage for a while. - * + * Start shrinking from the next memcg after zswap_next_shrink. + * When the offline cleaner has already advanced the cursor, + * advancing the cursor here overlooks one memcg, but this + * should be negligibly rare. + */ + do { + zswap_next_shrink = mem_cgroup_iter(NULL, + zswap_next_shrink, NULL); + memcg = zswap_next_shrink; + } while (memcg && !mem_cgroup_tryget_online(memcg)); + + /* * Note that if we got an online memcg, we will keep the extra * reference in case the original reference obtained by mem_cgroup_iter * is dropped by the zswap memcg offlining callback, ensuring that the @@ -1344,17 +1385,11 @@ static void shrink_worker(struct work_struct *w) goto resched; } - if (!mem_cgroup_tryget_online(memcg)) { - /* drop the reference from mem_cgroup_iter() */ - mem_cgroup_iter_break(NULL, memcg); - zswap_next_shrink = NULL; - spin_unlock(&zswap_shrink_lock); - - if (++failures == MAX_RECLAIM_RETRIES) - break; - - goto resched; - } + /* + * We verified the memcg is online and got an extra memcg + * reference. Our memcg might be offlined concurrently but the + * respective offline cleaner must be waiting for our lock. + */ spin_unlock(&zswap_shrink_lock); ret = shrink_memcg(memcg); From patchwork Sat Jul 20 04:41:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takero Funaki X-Patchwork-Id: 13737610 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 0A706C3DA64 for ; Sat, 20 Jul 2024 04:41:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8BF596B0089; Sat, 20 Jul 2024 00:41:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8208F6B008C; Sat, 20 Jul 2024 00:41:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 657F96B0092; Sat, 20 Jul 2024 00:41:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 4411D6B0089 for ; Sat, 20 Jul 2024 00:41:44 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B8E2A140A7F for ; Sat, 20 Jul 2024 04:41:43 +0000 (UTC) X-FDA: 82358882886.28.5AB86B0 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by imf26.hostedemail.com (Postfix) with ESMTP id DC8EA140013 for ; Sat, 20 Jul 2024 04:41:41 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=gDv33nXh; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf26.hostedemail.com: domain of flintglass@gmail.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=flintglass@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721450467; a=rsa-sha256; cv=none; b=u/eJbI9CRb1UQudoE/ZHvuoUeTpKo7rBpB4S7/jSQB2lfRKbiT9A/REVza7rlTi9XsepOZ 1r6Kb6saReMo5VzRn/HvB7J3iwkF/g1kang0YcPSuX8Pm73bgpOYxt7cjTtN/dhEyGEG0n FzdM7tohw+lkGpHlUdXwpSQTOnXE3o8= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=gDv33nXh; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf26.hostedemail.com: domain of flintglass@gmail.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=flintglass@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721450467; 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=vVw0vATACj76JfvzKQY4usxRkR0Oycm1bE+vKC2rk9g=; b=zmkUBtPo9zHsohPG6HT6LUHuJbZ5ST+SLQWYo55XFDgQuAvgqf9AI2tw8R7KRxZclxcD7J Kx3/PVh4BY865E3sLvteH1CQHGNslAfNnSbXI39wtcGwTwCRmfGKLZV+BCPFz/Hvkh1cmw wcdH3XXeP8qa/GBbyFxFkPwPRV+TxpI= Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-70b0e7f6f8bso1319839b3a.3 for ; Fri, 19 Jul 2024 21:41:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721450500; x=1722055300; 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=vVw0vATACj76JfvzKQY4usxRkR0Oycm1bE+vKC2rk9g=; b=gDv33nXhTWbZ/1h8n9MTQhfdVRxhkGYQzsi4GDGJ2Pllqn1yvKcBe7yhuI/zjcfMTS n5b07+MYlPsfFtnw5D4rwS1lHKGO4U/PQIgfMSfaPKKS1t4Nb2pichAh/Xaazl2Txm34 f4AQgcGCVpb/Xq5kWMDm/uuwGgXApY1FsVPD6eUGD196963qplKbM8/fCLW532Nt90n6 h6Va61tOXbNFlqgx91v8EuuSV6/23Vwv3+OjW+e6YRZSor9KoeNYPxu5ulLGkJRINqSW 0fWjHJr+wiz6MXcoPVZiVkQhasNFs22/DlBYDJf8PeiUTfAHRvvJuOOgMPHCcUUU/Cqx TR2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721450500; x=1722055300; 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=vVw0vATACj76JfvzKQY4usxRkR0Oycm1bE+vKC2rk9g=; b=gxdrEmkNIoQbseE9fD10lI1PWR2rPZyzxPQi2cLZWAPQ3pbh1lK77zXxzoADPHdqgZ MP4AQuykigw3HMkz7colB35dXRZnvMpZHoiwo9DWcT6gdfFfncRvM4TZzv53WnQYfD1/ AhmYdpk12u7CIXgAFO+DBFSxA5+VtRsdP+1/YH468KWP6zZsbxD1TIXMo4YdJ+3fDx+A rfRUZdKllsQ+kqDuH4YaXQPtYoZ09bM0jkv0NJh4PZNJBBYL8zcHyCYU5uPI774DEotM y4ZDGFJ7yQPTbpqclgTzp4Aw1oGPL5Wbu+P5d67trTqGDtGbE6QE+24nhAvzYHplavf+ 7h3Q== X-Forwarded-Encrypted: i=1; AJvYcCXjl7+Wcg+6GyntsFYEDycTzbsUUmds+ED/Xjz6fYkxm55VJL/ne5nII52pJAiljeBT+jZNzTpQcBBtRH8dctxVnSg= X-Gm-Message-State: AOJu0YxHCUgmbBmna6upIoUMfvxY60kyVfbwDWFa69Yv4bhI3eL1/emv e4kjDpW0NliSSKeiX6NtdFUQQ8/I9PssYc2Qcep12dHLe+aJ3QZm X-Google-Smtp-Source: AGHT+IEBS8Y5Ko/VqlXvrI1x+MdtVUYash+4xiAPzeysJsdDGDKxK2XNTIGZDknK0HVwrl0eHrrHzA== X-Received: by 2002:a05:6a21:710a:b0:1c2:92ad:3331 with SMTP id adf61e73a8af0-1c4285b7637mr683649637.2.1721450500245; Fri, 19 Jul 2024 21:41:40 -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-1fd6f315c11sm13023465ad.125.2024.07.19.21.41.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jul 2024 21:41:39 -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 v3 2/2] mm: zswap: fix global shrinker error handling logic Date: Sat, 20 Jul 2024 04:41:25 +0000 Message-ID: <20240720044127.508042-3-flintglass@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240720044127.508042-1-flintglass@gmail.com> References: <20240720044127.508042-1-flintglass@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: DC8EA140013 X-Stat-Signature: umsp11o5kkh5ruothw5xoepphrzigx5r X-Rspam-User: X-HE-Tag: 1721450501-820955 X-HE-Meta: U2FsdGVkX18g/nOkZJby2XpRwPcxlHNSf+zu8qoNPCPWS/Bj/zZYcBsUMJgDOnclPpKDZ+gCxGA9ZKFhXG5cK1zYjBsrq7hsdbKOqKwVnm/xe3g8aPPz4u8JyMOdeZHe5+UhPyoqSAF3CXs5XEtXqEy25ngx/o2LzajZgUCVhBUL7dp69o+soGETEKr/MIz/Bo1vVoRrbhLllJA5SCMwqu5UVvebXOwsUfMYFD8hyrEwM7e9ZID/3XwUkAgdhXbqMpSHfb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wRdyco X-Bogosity: 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 zswap global shrinker that did not shrink zpool as expected. The issue it addresses is that `shrink_worker()` did not distinguish between unexpected errors and expected error codes that should be skipped, such as when there is no stored page in a memcg. This led to the shrinking process being aborted on the expected error codes. The shrinker should ignore these cases and skip to the next memcg. However, skipping all memcgs presents another problem. To address this, this patch tracks progress while walking the memcg tree and checks for progress once the tree walk is completed. 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 | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 6528668c9af3..053d5be81d9a 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1310,10 +1310,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 @@ -1327,14 +1327,19 @@ 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, progress = 0; unsigned long thr; /* Reclaim down to the accept threshold */ @@ -1379,9 +1384,12 @@ static void shrink_worker(struct work_struct *w) */ if (!memcg) { spin_unlock(&zswap_shrink_lock); - if (++failures == MAX_RECLAIM_RETRIES) + + /* tree walk completed but no progress */ + if (!progress && ++failures == MAX_RECLAIM_RETRIES) break; + progress = 0; goto resched; } @@ -1396,10 +1404,13 @@ static void shrink_worker(struct work_struct *w) /* drop the extra reference */ mem_cgroup_put(memcg); - if (ret == -EINVAL) - break; + if (ret == -ENOENT) + continue; + if (ret && ++failures == MAX_RECLAIM_RETRIES) break; + + ++progress; resched: cond_resched(); } while (zswap_total_pages() > thr);