Received: by 2002:ab2:68c1:0:b0:1fd:9a81:d0e4 with SMTP id e1csp128991lqp; Sat, 8 Jun 2024 08:54:17 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXdci2TThFnysYiPXkLSUF3v1JcaVAoz7M2kuK3NEwa1NMaG1i4jJzj79YeE6GT7clKGcgq4VoNsZCfSYAppSlVGGJ/WmxCDQJpnTfGCQ== X-Google-Smtp-Source: AGHT+IG5bxQvqw0oaTIv6jgRyvqQKPX5ddYt9ZAXgh8INtn5jQBg5qU9VDhXvKcve9i4G1NOCkVh X-Received: by 2002:a05:6a20:6a24:b0:1a3:e2c4:956e with SMTP id adf61e73a8af0-1b2f9a379ccmr6788665637.25.1717862057664; Sat, 08 Jun 2024 08:54:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717862057; cv=pass; d=google.com; s=arc-20160816; b=S+SXjpg8oY9iuzXjZcYIVr0Qqa5VzMSHZahsp1DerGEvW2IwPmDhOEzGPqGlD5Q9f8 M8mtcRUsRlCt+TEZqO/ka6toltxmQGIr0DVzmVJTH+bYSeYvvczYqeCvV9qffsOXoNhA yC8VeYyc0HA5yhVSqMzRGguVXE/77dGpZkN9+D1AGenU2RK31vyC6pQoseboq6v6tlAv rkC9nWj5DWEJSmzWKLXC3LluzkETZDA1mLciOd/tEZCQlCXWwJprTRzGbGqd/zdV1P+t SFtDUo8qQ5AyfQ0qltUC5UIO6gv2xGoB8L1NisETU4wWPINYWz3BofWBqo9mqT67bEKH L+/w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=HrIr32TNVXDKH7oG1xumlOKq/cguM0V8sG+9FXVj9vY=; fh=n1Q6cyz3iTXuhkusy82NYyZvK3uZX9exOa5gQO09PjI=; b=f+wMZz/qcUPuUlZVZ4NmLbu3UOJnFhEHcpDrwo5qjl989Ry9CAImmv1mRA3pOAA46e 4ZrRauZsXbTbIC8+HW/LwJGxYx2ckwjivFsqTIqukRhcDSBZ5Rk946l24sr0HKEfDTtq hUmV6KPdIQbD+BL6Tvy68iAA47P3YNPZXUXHazgba2hAiw0czCnzyOnJ+0MKNOn5j4W5 2wSvarSIOgONLJxpxRkRdGrGpFEmvMiWSo1tfYSDbWsrg984HpLcwfesnJbfWi1qZdcH ljijMOCbeHSY1RVrc+Iz6ZPfL7zKpFjBkRrbxI/4A38hWpaE0cQ1jxf/SwoARI1NuKQ1 P0/g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=UmK+Nmx+; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-207108-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-207108-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-6de2a376948si854908a12.808.2024.06.08.08.54.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Jun 2024 08:54:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-207108-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=UmK+Nmx+; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-207108-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-207108-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 399F3282B5E for ; Sat, 8 Jun 2024 15:54:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 20C0217BB22; Sat, 8 Jun 2024 15:53:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UmK+Nmx+" Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 779831E888; Sat, 8 Jun 2024 15:53:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717862014; cv=none; b=tf16ugce+O1PBYH4eCzXZ5MBv8mneXkAZPryvs06l0evfzIgLv9y5qLbrOas55chna6Abf8eH+QgIXHJDurM6FILPaUqbnv/piMKHFG5p99ney1l7VcR42dvWp0X14Vs5N/GfXHbx5PfbOOS0v6QXQ8NVVz0Nz5P7i35l7CmrHA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717862014; c=relaxed/simple; bh=Snco5oqloL4GXcU+40C9q1nAQ6XQ+70U2BqTJl7GweE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Vvf2W7o0e50xpBwtRWNq8nG0aSwFPtsQluM85q4+sHjOv8MHX6lOoYhPVu1gO6EPwQ58DL8KPxkY+hDJr96AKmMc6s5mBHk3qlInSSOnKWiNcgOeF0i50bfkpgD/W+fEGICcIT7OWPhLSu1GpNBDFwmmH+xtfVfguN/YMKUxuGo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UmK+Nmx+; arc=none smtp.client-ip=209.85.210.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-703ed15b273so2494599b3a.1; Sat, 08 Jun 2024 08:53:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717862012; x=1718466812; darn=vger.kernel.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=HrIr32TNVXDKH7oG1xumlOKq/cguM0V8sG+9FXVj9vY=; b=UmK+Nmx+x+eWQwNR3vc4pir6nM8Bm5id1m8cKxsO2NpiMA/U6kFUWdj9RQgLVHOB0F 79CJn5ogSuL88/cI3iniNjSUYIftue4EmWwWVN+Hh8mpfG1w6GhawpA3WNVUvlb6v9Q7 i2R95D6Oil7zD15L3MYiSWKz+QCZMQSNOFJwZ4ay10ZuI63r2KhR/mfEp1L8qq7bHkHe YtsHkngcbH6CXbfcwLxeg3xgb2Ha5e8IswYTfuytcQYiYhWATeK9zifRIb8DMf+gCh1J yzZ2KK+in/MzvHSPr36T1gqDgatVMAglrx5uAkrTmROzEsuDj0T+J38sV8zMSRMDvttv q4Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717862012; x=1718466812; 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=HrIr32TNVXDKH7oG1xumlOKq/cguM0V8sG+9FXVj9vY=; b=Z2hAL2t/j2GVFF6iN30Suo4Q5Ir3cTBBcJ2yVhR838cxnWSEFuBb5oBMMk1Go5pkBo RffgivYUQH1MDdeJ0wBzvfKNCzlHnqwTTf8Mhe+9wU06Pr+RjWsvCvaLYct60H90pYY0 hy9NcGq/VwtK6CoaG3BZ5M0g3glNgtw9V41PD5XWj9sMTzc+12Z0SSZdy1QZiJlvFepx rXTfAAuhH4r/nGqBhGXY0eFHoLbS5ZrymHYhZxBvVXewxYm48m8G2H5rbtxrpgwkaTCe QRIDZVnhWIENidOZkZaaTmfmX0X379qmJS2yBdZedemlfYxwRdeYoQ2R5rXiX/0gcWC1 vaoQ== X-Forwarded-Encrypted: i=1; AJvYcCVHiUoTjxFpp8OoklnmRA8pTyLDaex/msPtvpjeyACAaEcU0SguM2l2TGcFK8g08+oFPtuWkzdLpf9Tb0bz3JY1uZuBQ+vOM0nEAMgtrCljkuDeSS0zzIJlBUEFWJD9ZfLT+XUPay4G X-Gm-Message-State: AOJu0YzC0kGDXYs1qPLryVY/bDO1Yo8VJwy/QaJ5pnx7yjRXz5glXgyW wxBKBsizwNljsPM/pUy55MmmBK4nRYWLFtiJgJuZMi/FPAGffSgw X-Received: by 2002:a05:6a20:43a2:b0:1b4:2a8:629 with SMTP id adf61e73a8af0-1b402a8081dmr4737912637.53.1717862011509; Sat, 08 Jun 2024 08:53:31 -0700 (PDT) Received: from cbuild.srv.usb0.net (uw2.srv.usb0.net. [185.197.30.200]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-703fd4d9d8fsm4335209b3a.149.2024.06.08.08.53.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Jun 2024 08:53:31 -0700 (PDT) From: Takero Funaki To: Johannes Weiner , Yosry Ahmed , Nhat Pham , Chengming Zhou , Jonathan Corbet , Andrew Morton , Domenico Cerasuolo Cc: Takero Funaki , linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v1 3/3] mm: zswap: proactive shrinking before pool size limit is hit Date: Sat, 8 Jun 2024 15:53:10 +0000 Message-ID: <20240608155316.451600-4-flintglass@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240608155316.451600-1-flintglass@gmail.com> References: <20240608155316.451600-1-flintglass@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 between 100% and accept_thr_percent, instead of the max pool size. The pool size will be controlled between 90% to 91% 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. Now, reclaims smaller than the proactive shrinking amount finish instantly and trigger background shrinking. Admins can check if new pages are buffered by zswap by monitoring the pool_limit_hit counter. 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 | 54 ++++++++++++++++---------- 2 files changed, 42 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 1a90f434f247..e957bfdeaf70 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); @@ -539,6 +540,20 @@ 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) +{ + /* + * Shrinker will evict pages to the accept threshold. + * We add 1% to not schedule shrinker too frequently + * for small swapout. + */ + return zswap_max_pages() * + min(100, zswap_accept_thr_percent + 1) / 100; +} + unsigned long zswap_total_pages(void) { struct zswap_pool *pool; @@ -556,21 +571,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 **********************************/ @@ -1577,6 +1577,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)); @@ -1599,8 +1601,17 @@ 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()) + queue_work(shrink_wq, &zswap_shrink_work); /* allocate entry */ entry = zswap_entry_cache_alloc(GFP_KERNEL, folio_nid(folio)); @@ -1643,6 +1654,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; } @@ -1692,7 +1706,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) queue_work(shrink_wq, &zswap_shrink_work); check_old: /* -- 2.43.0