Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp804370rdb; Tue, 5 Dec 2023 23:37:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IHg1gibH+QFnKCbOe+Y3Td0tTJ+qdMjSf8MptfxplfrDkUiGqYcxbX15XbJGyNjv8dmKAMC X-Received: by 2002:a05:6808:14cc:b0:3b8:b063:5d6e with SMTP id f12-20020a05680814cc00b003b8b0635d6emr745051oiw.85.1701848245243; Tue, 05 Dec 2023 23:37:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701848245; cv=none; d=google.com; s=arc-20160816; b=bJlpOkPLcJYXAf+SyWsgKUYMw+tTOR/yT9/NnwfEvXzkcPhP2Ght86tepEZggDyGqw NZwoJNGyHDOpeZ5HuRKhahqB7ypeCh3yvmJ0nfKIweI/T2z5Rx7Qj7mIKoN8y9AGnctr tyE5WA9rhP1SPecBVWsAnrCsDTVu2gAonZ5nenN8ybFDkuXdKX3dvXzV67nDjFPyhSPT HRLJb3AQ9/H3P1ZjxavTbUGerDWdQPbTPr5DXzMhccSuSVyjYKZbUlezCZvrIZ8jHClK S2XgJXWqqBw6cqYvfyGpkVenU5fu1Fhf9ZpUb7KbvUocFslJWyTTcWcrUUWpuNzjWyXB 3xCA== 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=Isn26eYQ7Abxs6xHNfpvbrXVMUNZJcXSAjCQgZHcKYQ=; fh=N7zqvpC0sH6SffAzLith03X75kFqJXtYa+YhEPwpuEc=; b=lxUIYe/nyKG8ZHofQ95Oc7sCeiiWGCixNgT0vfELvzngA6DdfQH9p8uIiNpvJJXoIP bPOKviQeH1SMZiJ8xup2hZmQS6r4L83W6m63VpnFvLn6MZS2hcdkR/iz6crWEw26QjAz U8scOHSOjsVWt/zt84jbinZn8acLSbUY2Rwb8jKQYoc7zlXMPzre+p6s5Fa+kMplEDYJ oDl8UVfuKQXihuum9eptK1FEo5LSzj/On3ePy9EVWTnIRPAKfVju2WXeFRf46c+Bh3jY 8AjMAXjm2TamNviAhxCTIDKN9vROcA1gqwcIgvsAmPLgIuUz6vAopNLKUSnC8oJ89Wmh yBlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ZlDbIHvK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id b34-20020a631b62000000b005c69bfbde6dsi3589111pgm.202.2023.12.05.23.37.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 23:37:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ZlDbIHvK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id A4F92805A78D; Tue, 5 Dec 2023 23:37:23 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229520AbjLFHhE (ORCPT + 99 others); Wed, 6 Dec 2023 02:37:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229459AbjLFHhD (ORCPT ); Wed, 6 Dec 2023 02:37:03 -0500 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86287D40 for ; Tue, 5 Dec 2023 23:37:08 -0800 (PST) Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-40b5155e154so71127725e9.3 for ; Tue, 05 Dec 2023 23:37:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701848227; x=1702453027; 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=Isn26eYQ7Abxs6xHNfpvbrXVMUNZJcXSAjCQgZHcKYQ=; b=ZlDbIHvKC241BSdnp1tWnowsWQb4XzbxxfZnm32/HBDiXXX739JZr0s+4Km7RyIWPw KEZlA5MrLate2bpCQL3EyoRiwmmZyX3DFt46HbBGTtmQrcBdQ7XWunacS3GcCK8suhFV J4VOVKq/LH3CD/iqqmh1MXmJg+RH4T+sr4TcIsHUUyDkoIR0L1IM+oYDSh0RQLwuCa5P RkeEnldmvU7GOaNC8DuY1OEFJM3z0s2mmZvvd4jItnQV61LokCwi+2EmfVbMYjgVcv5I 6hzIyeBl2/bKhDz8c/7pG77gGnvvSWl9jllVgwdXDiDUdHISvaKWmviW8r7VQcjcGS1p maag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701848227; x=1702453027; 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=Isn26eYQ7Abxs6xHNfpvbrXVMUNZJcXSAjCQgZHcKYQ=; b=Qil+hF3Hgsi1COSWabyRFhbOWWjowhDdEdoRY/On/klYqosoXUJJkmByecosjTblq6 ufwus61sdINKCNbKeuzcfyVLQViTLiFAorewxGGv4ZWEaxo0qtxtytUd6uEFWXrMj4m2 m/agDCohZWj7xi2UFi1auz6DDhp2eFVJdkdq6F/cxehb1TZvZyT/SnuEa6llPK7TrY7W /WQhluUsHfoRuc/HK6LucDYMWKqmoSdEqVrv6jdSl5auyMDIh3f9KTth9ihXIbWvYnYM kedSqVy9EV799RchA8Z5qLupFIM6ZjIDce7tnh4z+UfRZtqlfd3cY9OEvy8SNTkXugFI xS/Q== X-Gm-Message-State: AOJu0YxtHZxvHBnYBqzo/fqMwJtZoNAj7iOuPIOZWskeXPdaHTDHjIsd pFANv7cfLXE1luxJH79uKbx3G/13WUscHHvho1xWEA== X-Received: by 2002:a05:600c:3155:b0:40b:4c39:b4b with SMTP id h21-20020a05600c315500b0040b4c390b4bmr308415wmo.0.1701848226590; Tue, 05 Dec 2023 23:37:06 -0800 (PST) MIME-Version: 1.0 References: <20231130194023.4102148-1-nphamcs@gmail.com> <20231130194023.4102148-7-nphamcs@gmail.com> In-Reply-To: From: Yosry Ahmed Date: Tue, 5 Dec 2023 23:36:27 -0800 Message-ID: Subject: Re: [PATCH v8 6/6] zswap: shrinks zswap pool based on memory pressure To: Chengming Zhou Cc: Nhat Pham , 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, chrisl@kernel.org, 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=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Tue, 05 Dec 2023 23:37:23 -0800 (PST) On Tue, Dec 5, 2023 at 10:43=E2=80=AFPM Chengming Zhou wrote: > > On 2023/12/6 13:59, Yosry Ahmed wrote: > > [..] > >>> @@ -526,6 +582,102 @@ static struct zswap_entry *zswap_entry_find_get= (struct rb_root *root, > >>> return entry; > >>> } > >>> > >>> +/********************************* > >>> +* shrinker functions > >>> +**********************************/ > >>> +static enum lru_status shrink_memcg_cb(struct list_head *item, struc= t list_lru_one *l, > >>> + spinlock_t *lock, void *arg); > >>> + > >>> +static unsigned long zswap_shrinker_scan(struct shrinker *shrinker, > >>> + struct shrink_control *sc) > >>> +{ > >>> + struct lruvec *lruvec =3D mem_cgroup_lruvec(sc->memcg, NODE_DAT= A(sc->nid)); > >>> + unsigned long shrink_ret, nr_protected, lru_size; > >>> + struct zswap_pool *pool =3D shrinker->private_data; > >>> + bool encountered_page_in_swapcache =3D false; > >>> + > >>> + nr_protected =3D > >>> + atomic_long_read(&lruvec->zswap_lruvec_state.nr_zswap_p= rotected); > >>> + lru_size =3D list_lru_shrink_count(&pool->list_lru, sc); > >>> + > >>> + /* > >>> + * Abort if the shrinker is disabled or if we are shrinking int= o the > >>> + * protected region. > >>> + * > >>> + * This short-circuiting is necessary because if we have too ma= ny multiple > >>> + * concurrent reclaimers getting the freeable zswap object coun= ts at the > >>> + * same time (before any of them made reasonable progress), the= total > >>> + * number of reclaimed objects might be more than the number of= unprotected > >>> + * objects (i.e the reclaimers will reclaim into the protected = area of the > >>> + * zswap LRU). > >>> + */ > >>> + if (!zswap_shrinker_enabled || nr_protected >=3D lru_size - sc-= >nr_to_scan) { > >>> + sc->nr_scanned =3D 0; > >>> + return SHRINK_STOP; > >>> + } > >>> + > >>> + shrink_ret =3D list_lru_shrink_walk(&pool->list_lru, sc, &shrin= k_memcg_cb, > >>> + &encountered_page_in_swapcache); > >>> + > >>> + if (encountered_page_in_swapcache) > >>> + return SHRINK_STOP; > >>> + > >>> + return shrink_ret ? shrink_ret : SHRINK_STOP; > >>> +} > >>> + > >>> +static unsigned long zswap_shrinker_count(struct shrinker *shrinker, > >>> + struct shrink_control *sc) > >>> +{ > >>> + struct zswap_pool *pool =3D shrinker->private_data; > >>> + struct mem_cgroup *memcg =3D sc->memcg; > >>> + struct lruvec *lruvec =3D mem_cgroup_lruvec(memcg, NODE_DATA(sc= ->nid)); > >>> + unsigned long nr_backing, nr_stored, nr_freeable, nr_protected; > >>> + > >>> +#ifdef CONFIG_MEMCG_KMEM > >>> + cgroup_rstat_flush(memcg->css.cgroup); > >>> + nr_backing =3D memcg_page_state(memcg, MEMCG_ZSWAP_B) >> PAGE_S= HIFT; > >>> + nr_stored =3D memcg_page_state(memcg, MEMCG_ZSWAPPED); > >>> +#else > >>> + /* use pool stats instead of memcg stats */ > >>> + nr_backing =3D get_zswap_pool_size(pool) >> PAGE_SHIFT; > >>> + nr_stored =3D atomic_read(&pool->nr_stored); > >>> +#endif > >>> + > >>> + if (!zswap_shrinker_enabled || !nr_stored) > >> When I tested with this series, with !zswap_shrinker_enabled in the de= fault case, > >> I found the performance is much worse than that without this patch. > >> > >> Testcase: memory.max=3D2G, zswap enabled, kernel build -j32 in a tmpfs= directory. > >> > >> The reason seems the above cgroup_rstat_flush(), caused much rstat loc= k contention > >> to the zswap_store() path. And if I put the "zswap_shrinker_enabled" c= heck above > >> the cgroup_rstat_flush(), the performance become much better. > >> > >> Maybe we can put the "zswap_shrinker_enabled" check above cgroup_rstat= _flush()? > > > > Yes, we should do nothing if !zswap_shrinker_enabled. We should also > > use mem_cgroup_flush_stats() here like other places unless accuracy is > > crucial, which I doubt given that reclaim uses > > mem_cgroup_flush_stats(). > > > > Yes. After changing to use mem_cgroup_flush_stats() here, the performance > become much better. > > > mem_cgroup_flush_stats() has some thresholding to make sure we don't > > do flushes unnecessarily, and I have a pending series in mm-unstable > > that makes that thresholding per-memcg. Keep in mind that adding a > > call to mem_cgroup_flush_stats() will cause a conflict in mm-unstable, > > My test branch is linux-next 20231205, and it's all good after changing > to use mem_cgroup_flush_stats(memcg). Thanks for reporting back. We should still move the zswap_shrinker_enabled check ahead, no need to even call mem_cgroup_flush_stats() if we will do nothing anyway. > > > because the series there adds a memcg argument to > > mem_cgroup_flush_stats(). That should be easily amenable though, I can > > post a fixlet for my series to add the memcg argument there on top of > > users if needed. > > > > It's great. Thanks! >