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);