Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp56736rdb; Wed, 18 Oct 2023 18:12:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGTDKrCfqMd7Y/llpbLTYmJ1HwYWm3gUNIja3zgmZSCYV6ckMW+Vs/Mj1mX6+bRG5jcfcqd X-Received: by 2002:a17:902:d4cf:b0:1bf:6ad7:2286 with SMTP id o15-20020a170902d4cf00b001bf6ad72286mr848295plg.43.1697677967316; Wed, 18 Oct 2023 18:12:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697677967; cv=none; d=google.com; s=arc-20160816; b=GIdU4NbLDB9dLbumR5SmmNs7CfVUZJM2DnvF1WSSln5sUImc4m845SHYGMdVDkOJ6q ZIhlmPAv7SiBb8CFmxi0JkhPUsyGZYmzn7/LDLuHmsOmd8sTqlbVCQ998haCTuv+ydDj 4rr5euhNWTvknr7wyTbJD6mwhuagdTgTnWM+/BmdG6M4kcnJ8U9Phg16nrH4Ogh5JRB1 1GO9eGUACET4lnGhHM2h0g5MjXHJKnMFiuNHTwqWnra7SJGhzQAsJZ9O3M1cAks2Lt1u K3DJuf03/GiQXoRfknnA+wff3zO9KnIw9kex9e2Gv6BWqoniijF+obTW+XQeWI3sgFrN JVjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=dhz7SA9P+XJBcY68CPcxHuRXH5ke/B4swdMIdIEkvG8=; fh=DkjA2sPo1IHhj3dSLT/8nkkbEQExnFRA5si61kyVZaQ=; b=LiHVxeeA0YfuDTLhRLHUYhdwlLrFTcM6K205/SmCD9Pu7KPJn3ObZhrhV6WOKI4KuN ijmD36tVRkc+C0NuPStVinoYL842UbzCvdW49o61JEjM+ohG3vxrGa4mWfTHgxp5489e pl/rfwzJra/nWPDrNzk8i0ldF9CcouJCDfAJI9zGHwb2fbrV2eZRo/FzfNpLT9vIOkmF bZ/OBZOBH+vqZqjEO9XGlLHusjKUbZjOh1Zcb1WttaHZ7eU9H+YU74Djk+0zhzSgc1Zc q6BMphuFL/McLJTRPgZmP06YPy6Zn5E8hXObslgu6zi2G8/2t0UK5w4P98qym/rqutC3 yXmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=MBmhErMY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id k9-20020a170902694900b001bbc80a2a3asi1021475plt.299.2023.10.18.18.12.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 18:12:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=MBmhErMY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 566C88165393; Wed, 18 Oct 2023 18:12:42 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229456AbjJSBM0 (ORCPT + 99 others); Wed, 18 Oct 2023 21:12:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229632AbjJSBMZ (ORCPT ); Wed, 18 Oct 2023 21:12:25 -0400 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72F19122 for ; Wed, 18 Oct 2023 18:12:22 -0700 (PDT) Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-9b96c3b4be4so1165766566b.1 for ; Wed, 18 Oct 2023 18:12:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697677941; x=1698282741; 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=dhz7SA9P+XJBcY68CPcxHuRXH5ke/B4swdMIdIEkvG8=; b=MBmhErMYbMgj5TySQ41JVNBhYlVHOZBtnFLqJwR4aNq9e8AxEcLejpVsD0Dhkt+11N py9EvvUyiAJolKhQvj7tZwusHxXsF79EidRSdjVSERJNAtAAlRgLY8YmF0SUmrhoYa04 7FgaqORJU4Fq82sl+qmpO8cqALV6Hd88K4aCh0La2GkZKtnspLJqCuEPbKl3iV6nQ4bu wBaes+lpzyL1yglbbN+kWYWerRkodNAO71ArcZ0S6m7/CzODeuZvyZhkgPUauSb7v4VB 8xDmfvYGTCkw/TDXToUUXjXpDt1KHOdhHNIeHbsfiq84qXqgZ5Gb8EWJxtG2j9FQlUB1 soag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697677941; x=1698282741; 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=dhz7SA9P+XJBcY68CPcxHuRXH5ke/B4swdMIdIEkvG8=; b=rxoRRAyLUWUahj3mKodw6ZWnbA4M9GExUZ+sNczdt/NlHswrhe6yMdn+R8CfnMahaS 5dDKdPV7UdNaLZt1uFnH/J+yZoRT5dQDCllh0zAIrESItoV6jD1mruUtumDfEzzs7mfn EiloYOhoN46gxRmnfJ51SkpE9twqyvu85EFZUVySRx34jq95r/MAgAXDe1/2TvHHqskF Ud8SVU1ycCmceWHhYkg3dTpdb0q1b7u+smKByqxB5hPF9Ncq43SWWxPg6VjRTBJvRW+t tv6AWBfZOPDJbJp9UjeThO/ZxynKXKxnyzuTBtRXfUSStbf6MzeiohHOnYKlNgs68qvB 3hbA== X-Gm-Message-State: AOJu0YwHFKvzpFoRXfwaV47c8lEjM+AG04VYWtyNEvJqAIRMyPCmaOpy liC7DQsuUUu8SEI37rzktnSAxM7i2ch5ScAoOnRUgg== X-Received: by 2002:a17:907:c16:b0:9ae:59c9:b831 with SMTP id ga22-20020a1709070c1600b009ae59c9b831mr625646ejc.49.1697677940697; Wed, 18 Oct 2023 18:12:20 -0700 (PDT) MIME-Version: 1.0 References: <20231017232152.2605440-1-nphamcs@gmail.com> <20231017232152.2605440-3-nphamcs@gmail.com> In-Reply-To: From: Yosry Ahmed Date: Wed, 18 Oct 2023 18:11:41 -0700 Message-ID: Subject: Re: [PATCH v3 2/5] zswap: make shrinking memcg-aware To: Nhat Pham Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, cerasuolodomenico@gmail.com, sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, muchun.song@linux.dev, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, shuah@kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Wed, 18 Oct 2023 18:12:42 -0700 (PDT) On Wed, Oct 18, 2023 at 4:47=E2=80=AFPM Nhat Pham wrote= : > > On Wed, Oct 18, 2023 at 4:20=E2=80=AFPM Yosry Ahmed wrote: > > > > On Tue, Oct 17, 2023 at 4:21=E2=80=AFPM Nhat Pham w= rote: > > > > > > From: Domenico Cerasuolo > > > > > > Currently, we only have a single global LRU for zswap. This makes it > > > impossible to perform worload-specific shrinking - an memcg cannot > > > determine which pages in the pool it owns, and often ends up writing > > > pages from other memcgs. This issue has been previously observed in > > > practice and mitigated by simply disabling memcg-initiated shrinking: > > > > > > https://lore.kernel.org/all/20230530232435.3097106-1-nphamcs@gmail.co= m/T/#u > > > > > > This patch fully resolves the issue by replacing the global zswap LRU > > > with memcg- and NUMA-specific LRUs, and modify the reclaim logic: > > > > > > a) When a store attempt hits an memcg limit, it now triggers a > > > synchronous reclaim attempt that, if successful, allows the new > > > hotter page to be accepted by zswap. > > > b) If the store attempt instead hits the global zswap limit, it will > > > trigger an asynchronous reclaim attempt, in which an memcg is > > > selected for reclaim in a round-robin-like fashion. > > > > Could you explain the rationale behind the difference in behavior here > > between the global limit and the memcg limit? > > The global limit hit reclaim behavior was previously asynchronous too. > We just added the round-robin part because now the zswap LRU is > cgroup-aware :) > > For the cgroup limit hit, however, we cannot make it asynchronous, > as it is a bit hairy to add a per-cgroup shrink_work. So, we just > perform the reclaim synchronously. > > The question is whether it makes sense to make the global limit > reclaim synchronous too. That is a task of its own IMO. Let's add such context to the commit log, and perhaps an XXX comment in the code asking whether we should consider doing the reclaim synchronously for the global limit too. > > (FWIW, this somewhat mirrors the direct reclaimer v.s kswapd > story to me, but don't quote me too hard on this). > [..] > > > > > > /* Hold a reference to prevent a free during writeback */ > > > zswap_entry_get(entry); > > > spin_unlock(&tree->lock); > > > > > > - ret =3D zswap_writeback_entry(entry, tree); > > > + writeback_result =3D zswap_writeback_entry(entry, tree); > > > > > > spin_lock(&tree->lock); > > > - if (ret) { > > > - /* Writeback failed, put entry back on LRU */ > > > - spin_lock(&pool->lru_lock); > > > - list_move(&entry->lru, &pool->lru); > > > - spin_unlock(&pool->lru_lock); > > > + if (writeback_result) { > > > + zswap_reject_reclaim_fail++; > > > + memcg =3D get_mem_cgroup_from_entry(entry); > > > + spin_lock(lock); > > > + /* we cannot use zswap_lru_add here, because it incre= ments node's lru count */ > > > + list_lru_putback(&entry->pool->list_lru, item, entry_= to_nid(entry), memcg); > > > + spin_unlock(lock); > > > + mem_cgroup_put(memcg); > > > + ret =3D LRU_RETRY; > > > goto put_unlock; > > > } > > > + zswap_written_back_pages++; > > > > Why is this moved here from zswap_writeback_entry()? Also why is > > zswap_reject_reclaim_fail incremented here instead of inside > > zswap_writeback_entry()? > > Domenico should know this better than me, but my understanding > is that moving it here protects concurrent modifications of > zswap_written_back_pages with the tree lock. > > Is writeback single-threaded in the past? This counter is non-atomic, > and doesn't seem to be protected by any locks... > > There definitely can be concurrent stores now though - with > a synchronous reclaim from cgroup-limit hit and another > from the old shrink worker. > > (and with the new zswap shrinker, concurrent reclaim is > the expectation!) The comment above the stats definition stats that they are left unprotected purposefully. If we want to fix that let's do it separately. If this patch makes it significantly worse such that it would cause a regression, let's at least do it in a separate patch. The diff here is too large already. > > zswap_reject_reclaim_fail was previously incremented in > shrink_worker I think. We need it to be incremented > for the shrinker as well, so might as well move it here. Wouldn't moving it inside zswap_writeback_entry() near incrementing zswap_written_back_pages make it easier to follow?