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 9CB6CC25B78 for ; Tue, 28 May 2024 04:34:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1CCA16B0092; Tue, 28 May 2024 00:34:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 156916B0093; Tue, 28 May 2024 00:34:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC0AB6B0095; Tue, 28 May 2024 00:34:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id BD74E6B0092 for ; Tue, 28 May 2024 00:34:50 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7D4821215C7 for ; Tue, 28 May 2024 04:34:50 +0000 (UTC) X-FDA: 82166539140.18.9F169C3 Received: from mail-oa1-f49.google.com (mail-oa1-f49.google.com [209.85.160.49]) by imf05.hostedemail.com (Postfix) with ESMTP id AC720100016 for ; Tue, 28 May 2024 04:34:48 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hNugrPXS; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf05.hostedemail.com: domain of flintglass@gmail.com designates 209.85.160.49 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=1716870888; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Y5YtUenkWYqRuj/8vIRZQcV5VdqpUv/GG6aiR2ClnWI=; b=i25n0UTsu+7Rh1Hjc77hyP3zp1TtF3DQIZQaZ632LU9XO8R1tyLkKiM7CcHr/W5RW8lKrJ eXIId5TALLZ1uoI5FvGvfZsg77OVvgPb7yc5Qupt1R0fmoHloe+KC51rJhHoTiON0NEGhR dFlLvSefA0En8I+rh9BaJvSZz0JCF7k= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716870888; a=rsa-sha256; cv=none; b=ADj62/yy1l1xTIVL1HnEBTn7xFsNABX8NRGDz7nHyi6mzAI6F/uHZt4cfcthK+xH2v9tsR Je4xkUhGcmOUGWDK1HSORjbXkNODh4XtsmlggR+H8mtO0Zkld1Kr6LQVkPjQCA2chJegId lxQaZJnSG9mybiRhQl5/kV1HNnk89S4= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hNugrPXS; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf05.hostedemail.com: domain of flintglass@gmail.com designates 209.85.160.49 as permitted sender) smtp.mailfrom=flintglass@gmail.com Received: by mail-oa1-f49.google.com with SMTP id 586e51a60fabf-24d8b372911so237934fac.1 for ; Mon, 27 May 2024 21:34:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716870888; x=1717475688; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Y5YtUenkWYqRuj/8vIRZQcV5VdqpUv/GG6aiR2ClnWI=; b=hNugrPXSRj10IOM7nRER51wmk06OB9RxZXeLyP+t7x8WeeRsaZpsgNi9ZqU4Tw8LRs 6TKE8zgMgPFRue9V4X7L5FTqgI/buYLbsv4Pdq7RjVfwyWo4T9RN+p+Oqd0wZs6Wrxba lX/uKMnXhwBu9KbR+yqppNZnXoTgLKo3unki5ahCRIfgWgF4hAZOcmnt1kFI8tT1epLu AxH/zEtixZqjVltKyuq83CQoQWfSnPapDRjFw3LTwL+KDTUZKLtwTzfYsthHlPTnXMTL uXvRJrVerrlVwlDhdhs+rgXBUZxC0Y4N27GJf33arPW3fCqG1hQuhoaolPFuoaM0YplD vDKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716870888; x=1717475688; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Y5YtUenkWYqRuj/8vIRZQcV5VdqpUv/GG6aiR2ClnWI=; b=ekvKve3PAuiNwb2gayE70MuFCXBdmvM+3yqHvIFsCtsajPSNwXHlJCtLVxqP0P5TWD gcKSfhI3TISBhx1p77zIjYJggIsifdCBMzw34hvQHT0jxQkFeFbF1fklpKQsYe0qJ9qj 1V44y70oa3RLhDnVuPLOHAmm+Jzlpw55yngxlGsZPtJbVg47IrZpzZlmPFsq3qdSGP3h 18l9a6BpHcQnHugEP8isuRWtwKsoTYX0us244zA/7wcxHnaYUBFan491NM5nw7QGLnW8 fgAG5I+X8DD57AKTyEU9CYXJlv8N2gJT3bywEfHjZzjfu6EK4+SOzTTkXdZdn3kH2Avh +N6Q== X-Gm-Message-State: AOJu0Yyva/Jp4LN7EWpKPca8mrZuWBim2U+T7dtWUuBjW0R/oDSCfgHH zZPYrWL97/LaWya9rtx8hayidUvmKAZ9kIbU2mhYXTeKIMUnclPt X-Google-Smtp-Source: AGHT+IEA5dpWIDmAdAoZnZYsMSO/exqse9ObdcYrZsrnAfhtP9a2MtPabAeewk8HJFO0ba4xb3Sn6A== X-Received: by 2002:a05:6871:590:b0:24f:e6a4:991e with SMTP id 586e51a60fabf-24fe6a4d0c3mr6179100fac.6.1716870887574; Mon, 27 May 2024 21:34:47 -0700 (PDT) Received: from cbuild.srv.usb0.net (uw2.srv.usb0.net. [185.197.30.200]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f8fd4d57b9sm5620036b3a.193.2024.05.27.21.34.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 May 2024 21:34:47 -0700 (PDT) From: Takero Funaki To: flintglass@gmail.com, Johannes Weiner , Yosry Ahmed , Nhat Pham , Chengming Zhou , Jonathan Corbet , Andrew Morton , Domenico Cerasuolo Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/3] mm: zswap: proactive shrinking before pool size limit is hit Date: Tue, 28 May 2024 04:34:04 +0000 Message-ID: <20240528043404.39327-5-flintglass@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240528043404.39327-2-flintglass@gmail.com> References: <20240528043404.39327-2-flintglass@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: iaeji8hztfse3ccftfr5hgkbd9px9pwg X-Rspamd-Queue-Id: AC720100016 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1716870888-475058 X-HE-Meta: U2FsdGVkX1++mTGQVdvGFvbq/2Kg0YnX6UCa7/aCztY67JqJcwL0gHkw0BBfvMAGgFNG8oxQFQZOnD1gtJixBHiC3zCxjfe0iyAXdepHDJg3ehL6fHViiZiNUatCgT44w3GKyn8LOElHj/c/f+HnGXvyNMv9xGzpr5NNYx1jmgLEhIcGM/0RxgR6QN1sC23lgH7zafNbKWLz3BiRKM79lHJ7yb2F6kQoBIigbhDnbEwjqGa5UhRBoanyJ1ebHE5Tv8J6gsALccHh9ZsrSclVi2+Vq4KukqHPzUrDxUD1vocwbJPZ5nJyU6o8KRPdlPqcom98aO3ZTizQ3pgoNoNofFZ7iF8U9htBJen5a2csp5JCatYNq/T5x0lps8JlFGUZGtF0L/4LlbYPdhZPFtWKfosNn0Rl6fga5EvNTnA2xrHVkDRRqVP6WpJVEAJPPVMrnOIuGwXbVBaoMogBNMxN8yfmQ8dI/UODitWhS4pnFWJryfwfB5kSF4ncv6xNLrW9kHcS7Kv4jN0AkLII6GfJxIQ4zf8zglxd6jrx3AWvLRbCuTN65TfRwsj5er50TIN/bnWSFJArrD53Pg0Uni9zEBE41eeEkv8tu1BRlCK82HV3YyRLIvyMz9wZiGaUAUt5W2doSDqgp2z+5WOn4v0HBUV1dA9hhwJGorCFopK4+kh9N2Den0bRmQQjItJ8lJwHtwq287GFWX7aG7gUu0b+Fbhfsej4AcgTTPvIU4RvvOFum0Opp8JgsMgqV2aKkl78lvpNKhiJ4QcfUDoeGDVY+IRs2IyLJ08pcmBlKaE1PC5vlrrU4k/wpNVGAPI4/K0tvHGUpjZwyGtl7ehI0ib3D7CkXlkNegeGLwtDd0BPsCO8AZ0HpKE85RvUrt2yTJV39sCtvMh8YKh3jP264b6ykooYQJctv6LjJtM3QknIF82BPyjUUHlc37RfnHLMNZcyYu2EnpKo55yXcMkdWoN +n3DXlk1 ruKB9c2/53sHEebC5tSKdpm/YXi7cJ30EbTxFNOcDFLTfhL6NCwCM2l+Xyr9CC9/32KOoPZzbW3L1dCq2J3HdINh1oIEnW3W3CyhIv5qRPs0FchgmHcs2A0blH9UPu/IPPq8gWRye4rByiUCtNrAkYVVN0bbM3ROeY6y7vvwwesQgkPDWIekMEfEqnuAH+dttMVdTp7q/YiSlUPvqrKxRZfmYgx+wnxLxcVSLMlPVakaELmGtJ/oTjU7WTMJZy1YMcriixX1s97vvF02jeTXEByiViiFu6KnUNlYg1KgWBoR9Be0EVtoHwsUasVsgI1AGlzIxQ/bLeAcJMZ1BoyeQq5chHaLBRaMcLQjg 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: 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=90. 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. 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 compressing it. If true, the compressed length of the page is set to zero and the pattern or same-filled 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 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 zswap 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 the 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 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 failures and zswap writebacks. Swapping can be disabled entirely (without disabling 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, &zswap_zpool_type, 0644); static unsigned int zswap_max_pool_percent = 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 = 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 = zswap_total_pages(); - unsigned long max_pages = zswap_max_pages(); - - if (cur_pages >= max_pages) { - zswap_pool_limit_hit++; - zswap_pool_reached_full = true; - } else if (zswap_pool_reached_full && - cur_pages <= zswap_accept_thr_pages()) { - zswap_pool_reached_full = false; - } - return zswap_pool_reached_full; -} - /********************************* * param callbacks **********************************/ @@ -1475,6 +1469,8 @@ bool zswap_store(struct folio *folio) struct obj_cgroup *objcg = NULL; struct mem_cgroup *memcg = NULL; unsigned long value; + unsigned long cur_pages; + bool need_global_shrink = 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 = zswap_total_pages(); + + if (cur_pages >= zswap_max_pages()) { + zswap_pool_limit_hit++; + need_global_shrink = true; goto reject; + } + + /* schedule shrink for incoming pages */ + if (cur_pages >= zswap_shrink_start_pages() + && !work_pending(&zswap_shrink_work)) + queue_work(shrink_wq, &zswap_shrink_work); /* allocate entry */ entry = zswap_entry_cache_alloc(GFP_KERNEL, folio_nid(folio)); @@ -1541,6 +1547,9 @@ bool zswap_store(struct folio *folio) WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err); zswap_reject_alloc_fail++; + + /* reduce entry in array */ + need_global_shrink = 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