From nobody Sun Feb 8 06:05:17 2026 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 D078FD50F for ; Sat, 20 Jul 2024 04:41:38 +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=1721450500; cv=none; b=bMz7hNVmOz2CL6ukVxDi7SjdquECsbqs4Hz4Mk7NAhXftD2Gle8hAKj04QCZSmo5IJSHypHXyg4FA3pLnp2IiNXm8laRaL+Yn87xXeHgugY1e0jH9y7burIaPWa+ViyJNOie95JtkbpDguEBPH2dBIl4ih5vMJSfF17rR+XIXWo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721450500; c=relaxed/simple; bh=UaIs/aK6bf4wokEFWlwvfAn9h/tzBl6ekF80mPAEMXI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=J4BR5mTvpmMAHxt3vCuNX3cWhVWgkzgkes23Et/Oz/3RBCYdn8WZ01ZrRzzxeQTp3X8ie5bZvMGCDjW8LeVskXIAMrmG7r2lewdEYPOOUM7bkZAhLOpE4+QZCfQe0V7dgRiI/p49NVYEDH+2CsxB3YNf11ZrAVOp7hiRht62kws= 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=DCD8SJiV; 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="DCD8SJiV" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1fd78c165eeso2328685ad.2 for ; Fri, 19 Jul 2024 21:41:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721450498; x=1722055298; 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=he3tUu8hce94zy7aE7UWp0ZYRUcHVjdNDmthPwwFIA0=; b=DCD8SJiVv1RUr25Cs0zQ+jFQfDgOsQoCjr83S4ByWzPmpx2UcS9fOGW5In7LORzUwT 5ioRYRDpTWsbjGYljQd0Hc/OlO71al1GYi7aMky2dXmSQC3PHxPsWxk3xn6cp80kEigQ npJNaay4uGO8CHgd941i4LTV6CnzL+zdQBwAugJicmWT0ZbfT1OnU2c12+KbIVywU+R4 /Re9c0CPKaCXnjMqyZ5QTWEPbbS6HwiuABs9hZdqfu4LnblNV9qbzJKGCCJsOTK+eiBy A2yOBBdSB9jY/o/knE27Cd45reuenc0ikJRt5yG56btOkY7Nqbr6u4YGo0YdPgo0V4Km Y7dA== 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=TzuOaOKy5nE/UpGVpyCwoawxlv7K8r29eN0pTIiQ5eJG9+pfZpMlffspylvy99c04D n6B0BLcIK/dtdjqLYKrThihSvPlMepd8pp2xh0jEVk5a+7AfyB3DM8zfcESiZ35bud3o UwICOZBwrh2KqbfZBz8iTO6EJCBIgOij55sNrhegdTVYeYZxtiEnpVaxV2vP1h5X/UHh axQ3KE+jKoLxwlVJFAQ2SEewbTJ+woSNNeTOqPDMd+0dQR7lAFnFuRzDUPg1QaIlaEOJ MOic4MU+Q/rYXl2evka1VwZPcoXYhptpIspymSQ/2gY/RwsZh/ORy0F75aYJAKShs7zH ULOg== X-Forwarded-Encrypted: i=1; AJvYcCVMKRlTUb6XT+lLWE4mzOKZ2NfDmO27J1F/MehKpKHkWifhoyw6P8rw7WMiKIdtvNTES22s1bq7ePLCMN9k8fpc/WusVso795Pm8rIX X-Gm-Message-State: AOJu0Yw2iRzGi94K6/IueUS2cUl2VihyH/GFAFNlm/LllZLttRYmbNuC ojpmEPTwq9UfYdFvaqgz3cVippsGNG+55WGV0+QQ2Xb6PHuWy5u0 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Acked-by: Yosry Ahmed Reviewed-by: Chengming Zhou Reviewed-by: Nhat Pham --- 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) } } =20 +/* + * 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 =3D=3D memcg) - zswap_next_shrink =3D mem_cgroup_iter(NULL, zswap_next_shrink, NULL); + if (zswap_next_shrink =3D=3D memcg) { + do { + zswap_next_shrink =3D 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); } =20 @@ -1319,18 +1340,38 @@ static void shrink_worker(struct work_struct *w) /* Reclaim down to the accept threshold */ thr =3D zswap_accept_thr_pages(); =20 - /* 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 =3D mem_cgroup_iter(NULL, zswap_next_shrink, NULL); - memcg =3D zswap_next_shrink; =20 /* - * 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 =3D mem_cgroup_iter(NULL, + zswap_next_shrink, NULL); + memcg =3D 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; } =20 - if (!mem_cgroup_tryget_online(memcg)) { - /* drop the reference from mem_cgroup_iter() */ - mem_cgroup_iter_break(NULL, memcg); - zswap_next_shrink =3D NULL; - spin_unlock(&zswap_shrink_lock); - - if (++failures =3D=3D 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); =20 ret =3D shrink_memcg(memcg); --=20 2.43.0 From nobody Sun Feb 8 06:05:17 2026 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (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 0C6EC12E61 for ; Sat, 20 Jul 2024 04:41:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721450502; cv=none; b=Q9sYxdVHUXs+xl7AubKyp9neodDs2r15fBWFtyMkPMg9hXNLzdj4+iBP4ziOAz0KEX6S6WkFtqujDPXQV4WSrHdetq76d1uqMTRL319JdY28TqTdvI1UORVb493tlzda1SuMAFsUeB/c2hdE98pqqIYzsoMYb4lBSPHPGJo6Zsk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721450502; c=relaxed/simple; bh=sA3RiZ1oJA7H4jOOExLeQTxHrL4N4UV7cJ2ZeT0u6E0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DjC2FrTsSyPixu2z9pkCZ0fwe1T/KAabGLpjVfjRAoXc/4jCB2RPjeDTGp/9voV+igWoKweOQhOe+bxUekc60cnmCMy3UVhFJIkDyBIexrPGS2PXOrAEhjShJD2bb+psW6EB+v1fSEk4ip9UXhzl2OR7/dlKTMvGOlcCTmAKk2k= 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=lmq4mDG2; arc=none smtp.client-ip=209.85.210.178 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="lmq4mDG2" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-70af22a9c19so1182392b3a.2 for ; Fri, 19 Jul 2024 21:41:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721450500; x=1722055300; 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=vVw0vATACj76JfvzKQY4usxRkR0Oycm1bE+vKC2rk9g=; b=lmq4mDG2bSV1sek1BGh+GwgSv/G3ulo5TBkX4P+b5OPO6Me+iwOzBbZCcSYfoAdC/H vZpHSPPBDxOdonxEsMi7G64QylCddYDgUglXIVtTuYmhwiqUG0swFlayENnpqJYYSpAl DnXeElytmrb2FX5X5QX0ybdlbzeXiQ7JHVOuWNp3+jrjAgYj7CMjVmdt4CB702+X6H97 UyU4E3nQjmQeTkf9On9IKa2l5bpASvxdVhYi9qUQMsEvl9oP/s7bHFehKOToqAkov3Wf qcBNBXyQ9IVe5SBqjq4jtBtLgf+ckEF7IOrwMs0XpPY02aFmy5Jq+xUVBJ89X/rGohNo F4aw== 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=Xry+oR8oulLyrCysaEnvEhSCF291K0W91NTrft4ERxWbdByZ+9BAkAqlESyUAsLa4v PsprYCD4cwhmjW2wX+DJvCnLLKfC82VjXzY+fxoDAfGSMpQDNsMOG/+gY6ZvGYZgCQDk LqkZcd5/d+cZ7C2mA6yU//8sYHl+t3eirK7wOVEGNwh8r+vKnSuBNQK5ltQTgUr2U2hb fPtZsi1IqlND+TJgeARKccx2/5ME0eGog/43BihN+5R4M5ejH1796Ge39joPsh0o1BZc 5uJ9SwaAE8Cm+QA5c6AzftlDIxbMO2xc4bLqt0Rvsy8VdtheyM3mZy/px1o6B/UPvyFM J2mw== X-Forwarded-Encrypted: i=1; AJvYcCUfUybLTUKNkEGTJs7h2dxipM5gPow7gZbhevxzGtBH97x+CPdGeUBnhNVGu8YncGim1f4juQX+md7TRkHyY7/39O62P9YF6iEPVabg X-Gm-Message-State: AOJu0YxZJXj9/BZVNcMNIozuaLm3WKxGjgM2skuXVVaKBEw3bHL+H8MB ukuDzzgnxPL3XzTftzfJyGR/w0czDlt36mODDMdSrkSuJ7kfM8T6 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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) =20 static int shrink_memcg(struct mem_cgroup *memcg) { - int nid, shrunk =3D 0; + int nid, shrunk =3D 0, scanned =3D 0; =20 if (!mem_cgroup_zswap_writeback_enabled(memcg)) - return -EINVAL; + return -ENOENT; =20 /* * Skip zombies because their LRUs are reparented and we would be @@ -1327,14 +1327,19 @@ static int shrink_memcg(struct mem_cgroup *memcg) =20 shrunk +=3D list_lru_walk_one(&zswap_list_lru, nid, memcg, &shrink_memcg_cb, NULL, &nr_to_walk); + scanned +=3D 1 - nr_to_walk; } + + if (!scanned) + return -ENOENT; + return shrunk ? 0 : -EAGAIN; } =20 static void shrink_worker(struct work_struct *w) { struct mem_cgroup *memcg; - int ret, failures =3D 0; + int ret, failures =3D 0, progress =3D 0; unsigned long thr; =20 /* 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 =3D=3D MAX_RECLAIM_RETRIES) + + /* tree walk completed but no progress */ + if (!progress && ++failures =3D=3D MAX_RECLAIM_RETRIES) break; =20 + progress =3D 0; goto resched; } =20 @@ -1396,10 +1404,13 @@ static void shrink_worker(struct work_struct *w) /* drop the extra reference */ mem_cgroup_put(memcg); =20 - if (ret =3D=3D -EINVAL) - break; + if (ret =3D=3D -ENOENT) + continue; + if (ret && ++failures =3D=3D MAX_RECLAIM_RETRIES) break; + + ++progress; resched: cond_resched(); } while (zswap_total_pages() > thr); --=20 2.43.0