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 7C5BDC25B78 for ; Tue, 28 May 2024 16:01:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 003146B0085; Tue, 28 May 2024 12:01:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EF54D6B0098; Tue, 28 May 2024 12:01:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D95CD6B0099; Tue, 28 May 2024 12:01:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id B99A96B0085 for ; Tue, 28 May 2024 12:01:51 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6A8BD121766 for ; Tue, 28 May 2024 16:01:51 +0000 (UTC) X-FDA: 82168270422.07.D450691 Received: from mail-qv1-f42.google.com (mail-qv1-f42.google.com [209.85.219.42]) by imf10.hostedemail.com (Postfix) with ESMTP id 1790FC0030 for ; Tue, 28 May 2024 16:01:48 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=LV0pd3XO; spf=pass (imf10.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.219.42 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716912109; a=rsa-sha256; cv=none; b=0mCoq/EzSTSSBGmSneY1GzK6JDrvXT7ad5qjWCNExMI9lNFrLvUUUUcfUq3kdHiRuitQoa nyT0K4KLqZ83nydLnsAwzaHEuZeAhJOYoSSebAyz7AtKgoo4io7lJsFSp+u+ffnosHdT1+ m1jOc30Lvwnyy3ARZ+FgAdEtOflzjdg= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=LV0pd3XO; spf=pass (imf10.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.219.42 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1716912109; 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=IgAQEmp/9ugkOjEiZVdx8d+ta4a/jU4FQbSX/JXp4y4=; b=Py6pjfkyujsfpEzfEcCOY7SfSQ5UY3SqUx8mEbnY5N+I8bHJdIxFMS6+9NSlPJlep5dJp7 duqpmgkZ1WS/+21ft4pxSEBx2iY777wD4cESQ3+eWHGyoyv4gPMHoJZToFvuk8zBsQxVTm yxQtK8gPrTYd9De+FYFjfh6tBv27/zI= Received: by mail-qv1-f42.google.com with SMTP id 6a1803df08f44-6ab9ce64683so6014106d6.0 for ; Tue, 28 May 2024 09:01:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716912108; x=1717516908; 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=IgAQEmp/9ugkOjEiZVdx8d+ta4a/jU4FQbSX/JXp4y4=; b=LV0pd3XOz7QT4X9bcU8Y3azP77v0npvx4oWf0pqoV8YN2CyhhxlYe3NpjbwcAHomvm OWnMJ9gS4sfZi2vhqRYlVlDZ9VBFMt7wqdJ/jf92upOZXo0i2DTaAfK5QbUj88GVSMz9 DNIRO2OuPLCHvAh25L4xlqxp0NaB4GegTFuixIGdnGNQJGag3Nza/OjX2Qsqhn5ybcuJ rtcKDeDfdHjWctShNH6CAuShDyIhWNEu4oRaGucCQmSBzxhZ3F0qeo4fr/icIqfH3rsb UZXS2pBa499UBFqKdkvTChb687cFSXCkhovJdryv3xiaI9H4OkwQFbnFn/grXtgT0Fgq 6MWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716912108; x=1717516908; 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=IgAQEmp/9ugkOjEiZVdx8d+ta4a/jU4FQbSX/JXp4y4=; b=Ql2rYQ4l1Mvdx765W3Ykjpf8Ot/BbEk7FfG+vqJsL9JwAwkopLrwy1F6NNhIDp9kLD bJiUkg9irGdtEFLcd5odo/UVsH8adk469NiNVrjWydGu0A8hiMYnG1MFDi84W7fAunqO WG6AEpdP4NfV5C7xM+44u1kiLzOcyaFDf/KYsOst2PL9mjhYSVlQSen0ee6e8968n9Au IkGzhDNltk7/O33m696XkYG/yIVljsLHOUu2/L3GIAaqSyv3JsBmjG1WxID3kZ/qTNxH vowwFrb9Csx/hsM94ZdWFrCmKb7B6AWuC/dz/+3086EXcaW7cY6sbTSNxNY94IEDEeBP YVTQ== X-Forwarded-Encrypted: i=1; AJvYcCXd7rBBw1RPoWaAP9Ph4a4MoszOVcy+YOb7e+U4a0woSkOHL3GxSJkyFUyVpip9rrf3ZasRHAEtXrU6N2PT8UPL4DA= X-Gm-Message-State: AOJu0YxXbHn8+Xlp6BwyZ1VkELiQ7HZ4/hBdsBncGQ9ikF83Jkayr16i cqP/d5HVyDbqA9Ya/0vgUYbtlgEtSSvkZnxwL3DKjM6IXxVJmiCnn3yEXLkjQt1XAfH/jybY+4y ajwOVEH5I0UxFzM1fNK5b4csd95fXROWV X-Google-Smtp-Source: AGHT+IErq1RgS2UwbDZJYY8Aq3VeTlaBalztTGCZ2nGKJoFHybIwsc/cJ515dPOkBjiscIGbF1j71blg3eIfF8RbsI0= X-Received: by 2002:a05:6214:4b08:b0:6ad:8073:9043 with SMTP id 6a1803df08f44-6ad807391ccmr67368856d6.11.1716912107862; Tue, 28 May 2024 09:01:47 -0700 (PDT) MIME-Version: 1.0 References: <20240528043404.39327-2-flintglass@gmail.com> <20240528043404.39327-5-flintglass@gmail.com> In-Reply-To: <20240528043404.39327-5-flintglass@gmail.com> From: Nhat Pham Date: Tue, 28 May 2024 09:01:36 -0700 Message-ID: Subject: Re: [PATCH 3/3] mm: zswap: proactive shrinking before pool size limit is hit To: Takero Funaki 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: 1790FC0030 X-Rspam-User: X-Rspamd-Server: rspam12 X-Stat-Signature: p9schhfoiez8zz6bje91f765hsdhgbkz X-HE-Tag: 1716912108-621302 X-HE-Meta: U2FsdGVkX19khhl6apwGMbi/Vni2NGx21R8WQdb+78jT5AMjRY+Gy/yQDXqsZlUKUD1bHJSV+p+KuqMqcBtGLq6zspyjee3GRbVI+ZhM0cY3IHeFR8SyEBsuU9j42dhh0zEgQjhgWJndRh+eP964+4t3HZNOZSHrCUkx3+ktZr00bVRDULtiOoR6jj0LE+cXoKHoASdQl3OAA0t4z4bxQwmcUEyiVee7YzVz0QAi6IBm3Wu7u01vhZ3Un28YXtF5v6sjmftnNkBx4kNz5iSmBVl8ydWcDfFIOwaRkWRf+HxJ1Vu/nqgJ99MxZMi75pCPQtwlEfkShL/r5sCR90duGsuST0dJ4oH1cIno+F5hpqy3iWgo913dZ45UNvMSyZ3UtU7+U/XPVb918V1zsEifQWy/mzSX853d1jkvGqce5lx7ueF8NSXUA3ozkv0PeRHRB5vRyw87ay2PCwCjRI6JzbSIh0qRWoT7vQF7xhUNSypbJT+U1eLz8QBPUcoY3o8BOWzK2fUwCsYtHrrSbHs9bla0MilUkznk6bhVIy4l2Yn0/M3B3ZjAyHjWyut5yHDzO6hNfKAQclfYTHwDVafdvOa4lfJwx4Oz9sMKzs5okSYw6F4I8Cvppym565UwG0GAnYxAVaRHb+xlMLPt2tr3vd5tGQtowcI21ha+KUBOjRotCp0onhKXMUe4wxDfBK16SgvY0Fuh45i4paFOyhdZE+3TF83/zTQITNmeqI9LjSkFMF+DFUvY6UvLGVYz1uWerpVcBlKo3VJM+oVZllyayNII4LHaMkmwDbW2Vj00m8qZWCbDT1L5kOcqWhn9xYibyRFLaS5ZtbfFXiiebEECQkPgufvdlPQG5ew6TObATOabVSOkPwvDpX/8R1fh/fOn4eVX5G0i7A58qZjxBEZS6ZcKtzyxWKuGWkjNAlNcLlJSE2Lc5C9ocR+LCF42oVA6/8NVSRxfQap7Yfzpq3h kuKLr8r3 LjuhM0DLadbpVa/pCwKfbXCFPHt42FZ4l/uiIuqMTrsPg8cm4Pe5BBgzEPGiojEZGN2R9rnuo0TIyhUYwqDioNF8vzgdQZNKlTHbpu9v3+WPzpmnXh0DrzF1IozRZzSNz7HBzIz0sOdypT0kQmbKCWybCkhtwgyr0yGP8QwVO4XCxi17FD83t+CnoS6CaqNR6QG5JC9+SqVcurm3MI0kaxfsj2TX89ApC6+6nvZlLZ/OysRdMBGDQyrRzcGM6N8QVrTj2QaC/ZwaKQxLq8XzkivVz+XZXl3D/9NXy/dhotEwbl9Yg/i07HIkf+VwKt1KaS7nSjleMGSNnvfCilDgqEVYucDsqqRMGEZReS+T9V++k/MyfotewQFy1nA== 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: 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 maintain > 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 still > 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 :) > > 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/admin= -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 co= mpressing it. If true, the > compressed length of the page is set to zero and the pattern or same-fil= led > value is stored. > > -To prevent zswap from shrinking pool when zswap is full and there's a hi= gh > -pressure on swap (this will result in flipping pages in and out zswap po= ol > -without any real benefit but with a performance drop for the system), a > -special parameter has been introduced to implement a sort of hysteresis = to > -refuse taking pages into zswap pool until it has sufficient space if the= limit > -has been hit. To set the threshold at which zswap would start accepting = 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 zsw= ap is > +full, zswap initiates a worker called global shrinker that proactively e= victs > +some pages from the pool to swap devices while the pool is reaching the = limit. > +The global shrinker continues to evict pages until there is sufficient s= pace to > +accept new pages. To control how many pages should remain in the pool, u= se the > +sysfs ``accept_threshold_percent`` attribute as a percentage of the max = 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 fail= ures > and zswap writebacks. Swapping can be disabled entirely (without disabli= ng > 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, &zswa= p_zpool_type, 0644); > static unsigned int zswap_max_pool_percent =3D 20; > module_param_named(max_pool_percent, zswap_max_pool_percent, uint, 0644)= ; > > -/* The threshold for accepting new pages after the max_pool_percent was = hit */ > +/* > + * The percentage of pool size that the global shrinker keeps in memory. > + * It does not protect old pages from the dynamic shrinker. > + */ > static unsigned int zswap_accept_thr_percent =3D 90; /* of max pool size= */ > 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_percent= )/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)) > > /* 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 >