From mboxrd@z Thu Jan 1 00:00:00 1970 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 AAE8DC27C43 for ; Wed, 29 May 2024 13:28:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 32EAB6B0099; Wed, 29 May 2024 09:28:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2DF966B009A; Wed, 29 May 2024 09:28:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 17F1E6B009C; Wed, 29 May 2024 09:28:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id EF8C26B0099 for ; Wed, 29 May 2024 09:28:07 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 75B72121175 for ; Wed, 29 May 2024 13:28:07 +0000 (UTC) X-FDA: 82171511814.24.AF82F24 Received: from mail-yw1-f172.google.com (mail-yw1-f172.google.com [209.85.128.172]) by imf12.hostedemail.com (Postfix) with ESMTP id A534D4001F for ; Wed, 29 May 2024 13:28:05 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=i5q21qQl; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf12.hostedemail.com: domain of flintglass@gmail.com designates 209.85.128.172 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=1716989285; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=feB/56QcRvQlMzL2KSQZZ9GD4whRaX0bA2BQ/DYpEpE=; b=xIKQxBeiwUcBRjpbKgWRG7G+hqzzYIT8d8XWd4mbTuSCdwpx09u8Rv6BJJ/uk0IxTNCkOD 8qIM5jp6MRqT59la3XqO3rpYIR0AEy4B+KUWdUFYxMRuqL1FEzO/mvr7U4GFFBn1Jz+ROs bPeJlDbX+mpNtlGl0Ri5idjjx9r6Kco= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=i5q21qQl; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf12.hostedemail.com: domain of flintglass@gmail.com designates 209.85.128.172 as permitted sender) smtp.mailfrom=flintglass@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716989285; a=rsa-sha256; cv=none; b=JIAEMQKXR+ftOBUnQOFTkTLaKuCkf1cadjL6ZwwPs514wurnpAnbH+mHioQvlRGQHYFDE2 BoIn7tvAPlpikSkj04UaF/YYLxzJjZtUhpaSKCCbYd9Kpc1Y1UYFRZG2pp8GNa4hn8rxJl MhtaYQuOeUMx+DHSO7tN1rBfu2k5Pe0= Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-62a2424ed00so19230737b3.1 for ; Wed, 29 May 2024 06:28:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716989285; x=1717594085; darn=kvack.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=feB/56QcRvQlMzL2KSQZZ9GD4whRaX0bA2BQ/DYpEpE=; b=i5q21qQl/A88vHNp8d6GqeioW/BB46dUvrxqx84yXg6+4zseYz9pH7nzH0/u9Gxa+Z vH0w0yglb057nlNoH5rt0gbtVyXfmuFosHGuq/89uG+KzYpR7kCxkeWPwfVl9UQhhBSi LVjjtCG63rSW64vBzQyrRxfow/ioALIM3wFU5I8C1/aI32ZKeJaYND4l+V3pu1dOMDOZ RFRiEh06ybUoYJ009BU8BR2Sp4YwKXRLkuEx55Q9SGflzxvmYOrLcHPZcyZrbkNj6bVC zoJVknO8wj8SWqAIejD7131Drm7NAW3ppCSdiLhL4aSUkN1vd7D59rb+mbn/54yXBLVf uk1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716989285; x=1717594085; 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=feB/56QcRvQlMzL2KSQZZ9GD4whRaX0bA2BQ/DYpEpE=; b=veeRPeC74uY7ibY7oFVorBrYjic65slrACC11FfZZ53XqKHxz1NZQqEIDx0OwxQZdF tEfhUkeXZmhV429ZqhrpLYlP+egibbdzvL/uPY+n4vrlYOZEMjY115tnoP3cO3rMjO0+ 16jpKQS6LdMKcxv92x2Jb43yNTZ6Z5k1tx0Ydcmwrw07gZgDpAd0fJLOc4U1XyTCyQ7e DRAJ3VZHB9AT8/3ppIsrDCteZcEZI5tmemmG8RTB8dDyeLOTRA9fnCV/XsWmSObUD45Q hfDzAMD13iovlVlb37yfZ6UQjyGo8T/q7zEXOsoyLu3y5AF9+GQKi+11q1ygVO66OHkO nmpA== X-Forwarded-Encrypted: i=1; AJvYcCVHCc5dKSlNzri7Z7boGZR1K9gfmpIwi0w4Jd2aMRDgyo8uiGEg0/ghWA20rUNLnCjB+AYS7iLAmlFLqwLm2IkVBhA= X-Gm-Message-State: AOJu0YwoopWVYt5nmuiA+FXU1YL1DdkMbhQkvLWLn1000ZApRF2qdrn6 KfMhdPOKh8W3d9vzMi5nnyASMLkqnPAli+MWta7GF9P0rUipl2Vp88tc3b5rw0hmwXbpJDfcxi+ VQ3zgrqvi8Tjqj3CEKce3IXyvUnE= X-Google-Smtp-Source: AGHT+IFnI9mmjeBQWyXqd2gNmeCooUG1lmc3tLc06VpUQ3YLD+iLEKKuHLgn24xSsUDXY/kOhkHfBCiukUoSkgposW8= X-Received: by 2002:a0d:e2cd:0:b0:61b:33b6:41dd with SMTP id 00721157ae682-62a08ea7e34mr149139207b3.37.1716989284656; Wed, 29 May 2024 06:28:04 -0700 (PDT) MIME-Version: 1.0 References: <20240528043404.39327-2-flintglass@gmail.com> <20240528043404.39327-5-flintglass@gmail.com> In-Reply-To: From: Takero Funaki Date: Wed, 29 May 2024 22:27:53 +0900 Message-ID: Subject: Re: [PATCH 3/3] mm: zswap: proactive shrinking before pool size limit is hit To: Nhat Pham Cc: Johannes Weiner , Yosry Ahmed , 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 X-Rspamd-Queue-Id: A534D4001F X-Stat-Signature: or5zbxx4w7ibpqkoiah31mni3whcpzmj X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1716989285-419697 X-HE-Meta: U2FsdGVkX1+tXgIyAcF3edI5pQjQjSi7FH2/NmAOVMRpUbRF8qJo6YrfzwzXIK+mUhbS8+uAJ1aepyUSrvtKVX983Zu/OUfb3iNvCN46RXJUw1aweKdaRvBEia306RbY8vKbb1HLilCnqP7FiECbM7j6S6HaDjjGdhMpzdiPpnUGxbA2J9OB6Zi/wo1rWmFXUz/UZHARrHwCoBHFGkAYCkx376XyrjzDSjj4nqfmXWjFYvMMhGfI50PBRdrPm+Jmzb3+6ia5JZ6JT9RVs00OeLiJnxOQ/5uYC1ZB7dttVAIjvIpGv71jc8C1lNd/Ids+lFon7SQaAXopMrCFruzQlmNjixp1qI+XmNNSUAXYa4Gf5mrK7lQ3ekSJog8qwHB9HbxxBpKztKCq9B+M5zBvsBaUh6N///sS7UwpoMYFPnYHBEbUMhFqgVQRFNDCJDleBNx/WUuhjrm4FuvbpYdkhgTwmJi29DOOBxuwhDyey9Z4fpEM7KkhoYsabaxC2OU+dozI3Yw5DR+SFILhsiiaiV3SMaN0Cw6s0PBjmad6pwMsqtqNWahV2p5xQ7Ut+gpJrFkoiy+8tYdxXAnhUxZBhfX1BIwn2B32yHwKndtzUb/TFnIu0xlWKGFnAxXn9eVFwVHBK9Ok4QB2bJp/ibDQZAYfUAV7gTRN8QmCBg+q+0Lo3XN+Lrjo+/aycnhXPqgQtpRmIMVbw414QT6S8Fdt0Q0D51bIIxS/cO/NlLYzOmq/YGnx1goqDJMIg9uW5W2jUrGtT5sbDU4vQkH/5Wv6kBcQ86Jd+/TRz5dEemL7Zhf+Oq9yU1cqIQQT+9spe2VHkWieDvY/QhxKDSDMT8x8clfactZ7gbkozzwLN+vYChIDvaBLgcpRpGGPDC9CtQ5er9G/sWc6CB6LdQqbM8JeLYH4uMEIpOxH4XM9SeZhr+XWx1RRSWLDZAmWU4H8KsCUymr2jfVvxPaYFYk5DTS kNNXv/WI dM60ZnPb07vVMnnH7IBjbfixWtvY4neszCEeRE1X+otmdQvE9tQf+0082S5Aj8JT4Haw/KBKUyeokX8A4Mf7RP5BzlsrjybM/EP6yqqLMDlLlLeV0ZQh+uIdw2dKBHSdaMojBdFRyYH0Nkvjg6Tuy97AyEFIxRMK/PWoTdwMtuBT7ud4kGSr3NRKOaCsIQAGN0VoVTR5K/ymaXMa4vU2UuDijTv1w8UsOoSckqiJalxl9ey4icmBrXeHXT4TgZRwrNgVz6IntT4FUDqWx7mQL8hoc9B3+QMkZcncx4pXxGrFzDgUAkBTLnpSFDEMo7QZqJnnJ+JlQnBhmL8CtMTZN9xZA80n5SU6EjOJ/VwSTIsYsRdkrNLPDa4OLYQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 2024=E5=B9=B45=E6=9C=8829=E6=97=A5(=E6=B0=B4) 1:01 Nhat Pham : > > On Mon, May 27, 2024 at 9:34=E2=80=AFPM Takero Funaki wrote: > > > > This patch implements proactive shrinking of zswap pool before the max > > pool size limit is reached. This also changes zswap to accept new pages > > while the shrinker is running. > > > > To prevent zswap from rejecting new pages and incurring latency when > > zswap is full, this patch queues the global shrinker by a pool usage > > threshold at the middle of 100% and accept_thr_percent, instead of the > > max pool size. The pool size will be controlled between 90% to 95% for > > the default accept_thr_percent=3D90. Since the current global shrinker > > continues to shrink until accept_thr_percent, we do not need to maintai= n > > the hysteresis variable tracking the pool limit overage in > > zswap_store(). > > > > Before this patch, zswap rejected pages while the shrinker is running > > without incrementing zswap_pool_limit_hit counter. It could be a reason > > why zswap writethrough new pages before writeback old pages. With this > > patch, zswap accepts new pages while shrinking, and zswap increments > > the counter when and only when zswap rejects pages by the max pool size= . > > > > The name of sysfs tunable accept_thr_percent is unchanged as it is stil= l > > the stop condition of the shrinker. > > The respective documentation is updated to describe the new behavior. > > I'm a bit unsure about using this tunable. How would the user > determine the level at which the zswap pool should be kept empty? > > I was actually thinking of removing this knob altogether :) > If we see a large pool_limit_hit, that indicates we should lower the accept threshold to make more space proactively, to store new pages from active processes rather than rejecting. If not, we can set a higher accept threshold for swapin, s.t. low-activity background processes. That depends on one's workload, and can be tuned by the admin, I think. > > > > Signed-off-by: Takero Funaki > > --- > > Documentation/admin-guide/mm/zswap.rst | 17 +++++---- > > mm/zswap.c | 49 +++++++++++++++----------- > > 2 files changed, 37 insertions(+), 29 deletions(-) > > > > diff --git a/Documentation/admin-guide/mm/zswap.rst b/Documentation/adm= in-guide/mm/zswap.rst > > index 3598dcd7dbe7..a1d8f167a27a 100644 > > --- a/Documentation/admin-guide/mm/zswap.rst > > +++ b/Documentation/admin-guide/mm/zswap.rst > > @@ -111,18 +111,17 @@ checked if it is a same-value filled page before = compressing it. If true, the > > compressed length of the page is set to zero and the pattern or same-f= illed > > value is stored. > > > > -To prevent zswap from shrinking pool when zswap is full and there's a = high > > -pressure on swap (this will result in flipping pages in and out zswap = pool > > -without any real benefit but with a performance drop for the system), = a > > -special parameter has been introduced to implement a sort of hysteresi= s to > > -refuse taking pages into zswap pool until it has sufficient space if t= he limit > > -has been hit. To set the threshold at which zswap would start acceptin= g pages > > -again after it became full, use the sysfs ``accept_threshold_percent`` > > -attribute, e. g.:: > > +To prevent zswap from rejecting new pages and incurring latency when z= swap is > > +full, zswap initiates a worker called global shrinker that proactively= evicts > > +some pages from the pool to swap devices while the pool is reaching th= e limit. > > +The global shrinker continues to evict pages until there is sufficient= space to > > +accept new pages. To control how many pages should remain in the pool,= use the > > +sysfs ``accept_threshold_percent`` attribute as a percentage of the ma= x pool > > +size, e. g.:: > > > > echo 80 > /sys/module/zswap/parameters/accept_threshold_percent > > > > -Setting this parameter to 100 will disable the hysteresis. > > +Setting this parameter to 100 will disable the proactive shrinking. > > > > Some users cannot tolerate the swapping that comes with zswap store fa= ilures > > and zswap writebacks. Swapping can be disabled entirely (without disab= ling > > diff --git a/mm/zswap.c b/mm/zswap.c > > index 08a6f5a6bf62..0186224be8fc 100644 > > --- a/mm/zswap.c > > +++ b/mm/zswap.c > > @@ -71,8 +71,6 @@ static u64 zswap_reject_kmemcache_fail; > > > > /* Shrinker work queue */ > > static struct workqueue_struct *shrink_wq; > > -/* Pool limit was hit, we need to calm down */ > > -static bool zswap_pool_reached_full; > > > > /********************************* > > * tunables > > @@ -118,7 +116,10 @@ module_param_cb(zpool, &zswap_zpool_param_ops, &zs= wap_zpool_type, 0644); > > static unsigned int zswap_max_pool_percent =3D 20; > > module_param_named(max_pool_percent, zswap_max_pool_percent, uint, 064= 4); > > > > -/* The threshold for accepting new pages after the max_pool_percent wa= s hit */ > > +/* > > + * The percentage of pool size that the global shrinker keeps in memor= y. > > + * It does not protect old pages from the dynamic shrinker. > > + */ > > static unsigned int zswap_accept_thr_percent =3D 90; /* of max pool si= ze */ > > module_param_named(accept_threshold_percent, zswap_accept_thr_percent, > > uint, 0644); > > @@ -487,6 +488,14 @@ static unsigned long zswap_accept_thr_pages(void) > > return zswap_max_pages() * zswap_accept_thr_percent / 100; > > } > > > > +/* > > + * Returns threshold to start proactive global shrinking. > > + */ > > +static inline unsigned long zswap_shrink_start_pages(void) > > +{ > > + return zswap_max_pages() * (100 - (100 - zswap_accept_thr_perce= nt)/2) / 100; > > +} > > + > > unsigned long zswap_total_pages(void) > > { > > struct zswap_pool *pool; > > @@ -504,21 +513,6 @@ unsigned long zswap_total_pages(void) > > return total; > > } > > > > -static bool zswap_check_limits(void) > > -{ > > - unsigned long cur_pages =3D zswap_total_pages(); > > - unsigned long max_pages =3D zswap_max_pages(); > > - > > - if (cur_pages >=3D max_pages) { > > - zswap_pool_limit_hit++; > > - zswap_pool_reached_full =3D true; > > - } else if (zswap_pool_reached_full && > > - cur_pages <=3D zswap_accept_thr_pages()) { > > - zswap_pool_reached_full =3D false; > > - } > > - return zswap_pool_reached_full; > > -} > > - > > /********************************* > > * param callbacks > > **********************************/ > > @@ -1475,6 +1469,8 @@ bool zswap_store(struct folio *folio) > > struct obj_cgroup *objcg =3D NULL; > > struct mem_cgroup *memcg =3D NULL; > > unsigned long value; > > + unsigned long cur_pages; > > + bool need_global_shrink =3D false; > > > > VM_WARN_ON_ONCE(!folio_test_locked(folio)); > > VM_WARN_ON_ONCE(!folio_test_swapcache(folio)); > > @@ -1497,8 +1493,18 @@ bool zswap_store(struct folio *folio) > > mem_cgroup_put(memcg); > > } > > > > - if (zswap_check_limits()) > > + cur_pages =3D zswap_total_pages(); > > + > > + if (cur_pages >=3D zswap_max_pages()) { > > + zswap_pool_limit_hit++; > > + need_global_shrink =3D true; > > goto reject; > > + } > > + > > + /* schedule shrink for incoming pages */ > > + if (cur_pages >=3D zswap_shrink_start_pages() > > + && !work_pending(&zswap_shrink_work)) > > + queue_work(shrink_wq, &zswap_shrink_work); > > I think work_pending() check here is redundant. If you look at the > documentation, queue_work only succeeds if zswap_shrink_work is not > already on the shrink_wq workqueue. > > More specifically, if you check the code, queue_work calls > queue_work_on, which has this check: > > if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) && > !clear_pending_if_disabled(work)) { > > This is the same bit-check as work_pending, which is defined as: > > #define work_pending(work) \ > test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) > Thanks for the review and info. I will remove the tests. > > > > > /* allocate entry */ > > entry =3D zswap_entry_cache_alloc(GFP_KERNEL, folio_nid(folio))= ; > > @@ -1541,6 +1547,9 @@ bool zswap_store(struct folio *folio) > > > > WARN_ONCE(err !=3D -ENOMEM, "unexpected xarray error: %= d\n", err); > > zswap_reject_alloc_fail++; > > + > > + /* reduce entry in array */ > > + need_global_shrink =3D true; > > goto store_failed; > > } > > > > @@ -1590,7 +1599,7 @@ bool zswap_store(struct folio *folio) > > zswap_entry_cache_free(entry); > > reject: > > obj_cgroup_put(objcg); > > - if (zswap_pool_reached_full) > > + if (need_global_shrink && !work_pending(&zswap_shrink_work)) > > queue_work(shrink_wq, &zswap_shrink_work); > > check_old: > > /* > > -- > > 2.43.0 > > --=20