Received: by 2002:a89:413:0:b0:1fd:dba5:e537 with SMTP id m19csp578493lqs; Thu, 13 Jun 2024 21:40:28 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUshKRRxI/xb6zFyhwQBDog9N1LGavnenjZz/QMeawFsligsaO8Qz/b9KtEaBNgg7BzKRWeJkvhMtoYopOQm+uhnYW0bhemaQKFVn87Jg== X-Google-Smtp-Source: AGHT+IHzhPUmG1TPleCTSGbbBbLVHUQl4s14kqC9WkAYCa1xzD54fyRaUb23mTmq+lXu/SFwyaua X-Received: by 2002:a17:906:d157:b0:a6f:46f1:5434 with SMTP id a640c23a62f3a-a6f523ec1ccmr330806266b.6.1718340028421; Thu, 13 Jun 2024 21:40:28 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718340028; cv=pass; d=google.com; s=arc-20160816; b=vmUfUKDAwNZQ3OLbGp7UeUT1nq02gBGXrntVTjHHX4xW+RE+6ifaW7GhmVn14I334Z Sj56PfhDulBQjTyXP/N2Te5G7Rwke+QuDhq2rsZcC9ArwmG66gOBuA2eZ2Vc7BVLifJ0 n0JMaXnNKf48lk8cNXAA69sCmfnn6x2J5wZvLrSAqrx0x71A45jK0tneCp2Zh2ucLBTz Ax9Ujzin3lU0d3lmTPqzNLq7CuxHwaARifpeYxqPqYGT0yZkhyugRuRaN+2+pT3Fchdv 0e9KDiCav/UoyFKUaGAxu1PUXmEgxdFc+/p97yCMTsY0SB7MUcvCAs8zibhNIKjD2SrN z6Iw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=lbtquG4p0hVvqKFk1U/Le3nDHhfbk+WM+GokVxGfK54=; fh=KvIkyFcFWGJ6lJJ52GPpDUeQbwsimP3jPQH4Nmlmtuo=; b=qSvLER/nIpB6rF6drUk3Ld84HTB3F8B9Pl5ZP7ixczaU8H3L3Dy0IcMNZUP1zPHhPX +NVL3e+4qC7PnxLImb/yAyA8cixfNdOtVk1CvN/3HUKiYgLyVnaWyLfAJNIn/tF0M3hV Sr7ac4xGl1kz+lLtd+b22XavrKrIkegH0WfG4Kp9+5zyGLegiUuuoTCdTWsoMkgcoGSV A0dJTP7BxizHxvXKcY4L9o4pHKxpiK2PebmdfZuIIC1sHOOXoZzE3PBjqVbaxC/Su8kG 9+D72fpMXoY9vOAUGMlpcIJhiGCpTtX6rh4MOoRoOWXnIybF4EVBOdqtwwVrHLpn1wcj JhRg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=HBU3qLBW; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-214341-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-214341-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a6f56e7d60dsi134382866b.953.2024.06.13.21.40.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 21:40:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-214341-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=HBU3qLBW; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-214341-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-214341-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id F3E161F238CD for ; Fri, 14 Jun 2024 04:40:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D3B11146D65; Fri, 14 Jun 2024 04:40:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HBU3qLBW" Received: from mail-yb1-f177.google.com (mail-yb1-f177.google.com [209.85.219.177]) (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 5436E146582; Fri, 14 Jun 2024 04:40:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718340010; cv=none; b=SN6IRXXQDbn2Miih+Sqr2/f7cD8MGpY7RQppTMnm8tT6kNDjuRf1K9peWWCrxVonvru1wsX1TmG1MFcGcq0srpcCaA5yjp+2Ys5u7uem9CoMBMX3yiMJMiKLeBppSwaCwhrGhKYADVIojltVLeErS6YBIAiPDiijoGOg+6ykcI8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718340010; c=relaxed/simple; bh=Cj5KzMMadXsRnndd+TlVK0BUb+fjbSscqj3uM4ytUuo=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=rueTXhA5c1Dost4p6RTi8HRtGag4G9gDo2SNTp6iAuMqVbGsavyPvE5ho+57NxVB0Wnp2+Cv0BzSKd9rxrDF+4jpfdwA5kkhq1gk8sycFfrMlkc5n141tIgGjiAEk1oWU4RFd5dnMYUimlgp97ht4Ml4QXPzIM4oAJZ41AYMCG8= 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=HBU3qLBW; arc=none smtp.client-ip=209.85.219.177 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 Received: by mail-yb1-f177.google.com with SMTP id 3f1490d57ef6-dfac121b6a6so1406996276.0; Thu, 13 Jun 2024 21:40:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718340008; x=1718944808; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=lbtquG4p0hVvqKFk1U/Le3nDHhfbk+WM+GokVxGfK54=; b=HBU3qLBWlZqRg9Nnhj8P6lwfiVE9T2AHErZAmSPQ0Rs5eQyw18AQgP0vnX6HAsAfwg kgwYCzMDg4nb0d2+FW5NsdytcKqB2oBzniMMtPhucGI5sDGxRatRlK+1+88+d9MQLilc Sk6Tc+8fpBYsMM3lLgX/8sggKnAiZOnOTPbBP9k0B+bk1krhiU/l03C8MWuBy1tiLBHs 4oAstZJe2L1i6K7q1ILP7f7Dcdv7NaVWE38EPU/LDk7QOQtT2y2he9cEFACDnWVA8HjO I3EXskF63ElXl7ZZB835QwJuOJsZxEho2CrVqcK+M1XAJ7adZqxWYSyiSkU8u2usCvUY OdXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718340008; x=1718944808; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lbtquG4p0hVvqKFk1U/Le3nDHhfbk+WM+GokVxGfK54=; b=f1PmQgmvTkO4/iXefA1wiOOAyp6q7cPETfxAKcKQRhxFWfDA6xIfpRSBxoJNhVY3GO uCqFHsbqHv7l/PlzjHz64HvxMAzGGjBVn6hIAR8a2lH/oIvhTXIAcLnnkw9alHP5LLT0 /BekzH5/WD1V969QemkUCjkN+7+xvtjxdrWTJn5U5B2xWUYszrxbFlaeSwuROd8P0skK QMz6IXgqFfHsl6b0lomX6ssjnRsq7DAZdkO+XG581MR6TonelCoQ9sZt5mrpSni0lXcd Uw+QOeplj6b79cONRjs0Bq4P51S0btlLshfTb13asjY6U6PegJLTduFLdKfn0O+EAMDX XuTQ== X-Forwarded-Encrypted: i=1; AJvYcCXIm4W5GM4vK54QuL5T9FICdzxJ9HtCgfALS/OXJ68unzvEhsmVaoFRa5ZDzZEuyHnrwziFHT0d7Mf5z2NRXeech56Idu9xdz2A3VUAijq/iHGPUxhpzGZzmGVl+gZXppHsyGcggXOu X-Gm-Message-State: AOJu0YyUXuhU8zyh7CPgg2Zh9fEczbti0MqbDxmgwTS16vgPHT5uylGO CzyZU7BZMXSfSLxI/IwPSozOYGPN7eYWq526a7tZBlNBU1xaMmWKv2yxwnU1soGGjSiIf99yCyX dC+ei2iMew3a/8KTo60uVhcbJ4MWiokYar6Q9MA== X-Received: by 2002:a25:abb2:0:b0:dfb:f8c:39f2 with SMTP id 3f1490d57ef6-dfefebc9ca9mr2619063276.5.1718340008223; Thu, 13 Jun 2024 21:40:08 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240608155316.451600-1-flintglass@gmail.com> <20240608155316.451600-2-flintglass@gmail.com> In-Reply-To: From: Takero Funaki Date: Fri, 14 Jun 2024 13:39:57 +0900 Message-ID: Subject: Re: [PATCH v1 1/3] mm: zswap: fix global shrinker memcg iteration To: Shakeel Butt Cc: Nhat Pham , Yosry Ahmed , Johannes Weiner , Chengming Zhou , Jonathan Corbet , Andrew Morton , Domenico Cerasuolo , linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 2024=E5=B9=B46=E6=9C=8814=E6=97=A5(=E9=87=91) 1:49 Shakeel Butt : > > On Thu, Jun 13, 2024 at 08:04:39AM GMT, Nhat Pham wrote: > [...] > > > > > > > > > > Is the idea here to avoid moving the iterator to another offline = memcg > > > > > that zswap_memcg_offline_cleanup() was already called for, to avo= id > > > > > holding a ref on that memcg until the next run of zswap shrinking= ? > > > > > > > > > > If yes, I think it's probably worth doing. But why do we need to > > > > > release and reacquire the lock in the loop above? > > > > > > > > Yes, the existing cleaner might leave the offline, already-cleaned = memcg. > > > > > > > > The reacquiring lock is to not loop inside the critical section. > > > > In shrink_worker of v0 patch, the loop was restarted on offline mem= cg > > > > without releasing the lock. Nhat pointed out that we should drop th= e > > > > lock after every mem_cgroup_iter() call. v1 was changed to reacquir= e > > > > once per iteration like the cleaner code above. > > > > > > I am not sure how often we'll run into a situation where we'll be > > > holding the lock for too long tbh. It should be unlikely to keep > > > encountering offline memcgs for a long time. > > > > > > Nhat, do you think this could cause a problem in practice? > > > > I don't remember prescribing anything to be honest :) I think I was > > just asking why can't we just drop the lock, then "continue;". This is > > mostly for simplicity's sake. > > > > https://lore.kernel.org/linux-mm/CAKEwX=3DMwrRc43iM2050v5u-TPUK4Yn+a4G7= +h6ieKhpQ7WtQ=3DA@mail.gmail.com/ I apologize for misinterpreting your comments. Removing release/reacquire. > > > > But I think as Takero pointed out, it would still skip over the memcg > > that was (concurrently) updated to zswap_next_shrink by the memcg > > offline callback. > > What's the issue with keep traversing until an online memcg is found? > Something like the following: > > > spin_lock(&zswap_shrink_lock); > do { > zswap_next_shrink =3D mem_cgroup_iter(NULL, zswap_next_sh= rink, NULL); > } while (zswap_next_shrink && !mem_cgroup_online(zswap_next_shrin= k)); > > if (!zswap_next_shrink) > zswap_next_shrink =3D mem_cgroup_iter(NULL, NULL, NULL); > .... > > Is the concern that there can a lot of offlined memcgs which may cause > need resched warnings? To avoid using the goto-based loop, here's the new version, including Shakeel's suggestion: ```c do { spin_lock(&zswap_shrink_lock); /* * Advance the cursor to start shrinking from the next memcg * after zswap_next_shrink. One memcg might be skipped from * shrinking if the cleaner also advanced the cursor, but it * will happen at most once per offlining memcg. */ 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)); if (!memcg) { spin_unlock(&zswap_shrink_lock); ``` We can add or remove `spin_unlock();spin_lock();` just after mem_cgroup_iter(), if needed. I believe the behavior is identical to v1 except for the starting point of iteration. For Naht's comment, 2. No skipping over zswap_next_shrink updated by the memcg offline cleaner. While this was true for v1, I'm moved to accept this skipping as it's negligibly rare. As Yorsy commented, v1 retried the last memcg from the last shrink_worker() run. There are several options for shrink_worker where to start with: 1. Starting from the next memcg after zswap_next_shrink: It might skip one memcg, but this is quite rare. It is the current behavior before patch. 2. Starting from zswap_next_shrink: It might shrink one more page from the memcg in addition to the one by the last shrink_worker() run. This should also be rare, but probably more frequent than option 1. This is the v0 patch behavior. 3. Addressing both: Save the last memcg as well. The worker checks if it has been modified by the cleaner and advances only if it hasn't. Like this: ```c do { if (zswap_last_shrink =3D=3D zswap_next_shrink) { zswap_next_shrink =3D mem_cgroup_iter(NULL, zswap_next_shrink, NULL); } memcg =3D zswap_next_shrink; } while (memcg && !mem_cgroup_tryget_online(memcg)); zswap_last_shrink =3D memcg; ``` Which one would be better? or any other idea?