From patchwork Fri Apr 5 05:35:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13618545 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 4C17BCD1284 for ; Fri, 5 Apr 2024 05:35:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BD5F36B00E7; Fri, 5 Apr 2024 01:35:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B86066B00E9; Fri, 5 Apr 2024 01:35:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A09296B00EA; Fri, 5 Apr 2024 01:35:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 7CD7E6B00E7 for ; Fri, 5 Apr 2024 01:35:19 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 2AEC31A10D0 for ; Fri, 5 Apr 2024 05:35:19 +0000 (UTC) X-FDA: 81974365158.21.794552A Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf12.hostedemail.com (Postfix) with ESMTP id 6C0174000B for ; Fri, 5 Apr 2024 05:35:17 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1ulCEXsh; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf12.hostedemail.com: domain of 3lI0PZgoKCCUZPTSZBINFEHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3lI0PZgoKCCUZPTSZBINFEHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--yosryahmed.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712295317; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=kLsh2mavKyRbc6llROScpNs5yKk8UXbWOX30PW11RTM=; b=c+ZiiO3AkAed6S0QzZrXu8Lb2taUIH3OsX6P9n2CuzSANGgd9D9ddq7WKcVOtIFFMMDSBK n4Iyu0sy9QZmiAQF7ETVdcQ8iZiKgjAmHSK6cXnn3mpYAjR+osnnH4qVJAKsGGkPAeZvuW swvn3Hmip2KX4uDBrG1bHVptUvP04No= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1ulCEXsh; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf12.hostedemail.com: domain of 3lI0PZgoKCCUZPTSZBINFEHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3lI0PZgoKCCUZPTSZBINFEHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--yosryahmed.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712295317; a=rsa-sha256; cv=none; b=DLC0kzENLS+O5O6TV2cmawLfeNWEy5ecSTKSaoz6j8jkfNUEcH8EweHUsndnVilweisFLX LZQvf+HutKl9lFQWP8M38ukQY/N9DjRCYtNbb/WDHPJGPlOSLsVFwYxG3RQ00mCO29Jw0u k5/CBhz5zotrHnwINWvBrodukhZ4Xwc= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc693399655so3208981276.1 for ; Thu, 04 Apr 2024 22:35:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712295316; x=1712900116; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kLsh2mavKyRbc6llROScpNs5yKk8UXbWOX30PW11RTM=; b=1ulCEXsh7EFXmnd/T15yPB1wXc/M4Y7NRYhMhdxJXgaYpWMNmwjup9WqrmY/oPkp37 HLPpgWxBgh+HBoMijVqu5560UXVtPuA+g1LV+NlHYjVk0PNv34FtqDgpk65oY0r/Y/FJ oAZnvZzEjXyd6Rcru1mn4X1WcBgDDymf/G1IF1xaUn3Qx5wZUy4NzKUYC+syP/+n5d1f lOLB9APdUzFLwlbRM7V1uWv1FD2VmAyemM/+vV01sTWQ2kRD+gNc62+vvbpWOdZ6uWsL 1zkAO/4s8q70i6oPMUpPoTvzxXy/FK+V44FbqQEW0P8M4M+AOH6X8tHA5Sux+IaJyzTK F2VA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712295316; x=1712900116; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kLsh2mavKyRbc6llROScpNs5yKk8UXbWOX30PW11RTM=; b=CaJFf8L04xMDs5U5//Bx1ZZdENAlbIfD1brGbQkLNpEF9PPnk5u8YkJ9oTxbCeGdfP huHWro6eEUWpHPvmAkrGQ5PfDvCl02SRvKNJMsy2VMH4zfQjAa1yMtV4P0mmqQBIP9ao QBGF3YSXWvDhLmm3eY1+RGlPpTLY4fxyhIN0QTbLnogUkHoRuV46UnIMzfudL0qBX8xu hNjWy2NR0C0iloWqLowfiVirJ22vXBcMjl7LvKQhIAwD98qaI+ksR4bsD2h7eQBtg7da zapIoHZcCgkiTLOHm8pzFDpaLrIqj3ngoJKZCovWTjeADXCkP0vTJgbn0fcIrYDht1NB akIw== X-Forwarded-Encrypted: i=1; AJvYcCU5Ml/EJYQx/QG12jRdYgtB9q5MIM8ZwBDq8Tm7m86U2GdWV/+2kw9lu0K3hVkCIYvnP5wcNj56WU2CSpp9ykrZd+Y= X-Gm-Message-State: AOJu0Yxgx+73pDWRc6kmW91wTGjnTwrfJ9p8mU2ffX6bWw3tgzIb7Gkn apDEUjDyrjHMdvebH57ktdz7geHJC0MmQgKVq1pdXPozgCK+Pqjt5cfrWWnVU+43WHgWp5M1pAO mxJW1kfJOFAZOUKubmw== X-Google-Smtp-Source: AGHT+IH7oL3hUlxA/yZh4c4iQx4M23yR54gCIDKzNRckDvrgcltEIddaQ+ef2AO7ne3vgWVM1zEbXHHpxJ8/KEde X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a05:6902:f87:b0:dc7:8e30:e2e3 with SMTP id ft7-20020a0569020f8700b00dc78e30e2e3mr97855ybb.2.1712295316513; Thu, 04 Apr 2024 22:35:16 -0700 (PDT) Date: Fri, 5 Apr 2024 05:35:07 +0000 In-Reply-To: <20240405053510.1948982-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20240405053510.1948982-1-yosryahmed@google.com> X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240405053510.1948982-3-yosryahmed@google.com> Subject: [PATCH v2 2/5] mm: zswap: calculate limits only when updated From: Yosry Ahmed To: Andrew Morton Cc: Johannes Weiner , Nhat Pham , Chengming Zhou , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Rspamd-Queue-Id: 6C0174000B X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: w73uze81h3sd8m8rj15bxogj3fj9th5i X-HE-Tag: 1712295317-524621 X-HE-Meta: U2FsdGVkX18sMNT+Gvr/tRPwz+Qq88iLI1EzuYURGBf/UtAPVYDJNexVGgrhcNT4U2RxtQetNutxLhalN5qqM2xPVCGivpamvJJ8fEqaef0ctQJXMClxmwIDI7JLFILp/JPGlNbSHmkw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VUMX/CvVuU3fYwq9TVzK/UuijvdEZLdzOgKoc6jUw7iSA+aJ/QNPpvSLq72CkMUtBEMA23Zl+P2BKIqunHfffpna0OcTLAWhpuo+73vmMZig0wapD+12NBUeR+1bQlBotCN1t6vovWg7m9OE6oF2apnXe5DEd3EkvMrMvEAO3ChAZse3snkbpcWOLy7cCN0k8/Vg5GYBf7iPvGNKSZ9roIkEpjgYaEEVGULSIrxRO4eSQid37c75ADQDANo1AfpWGO6LTwHpaT6djp2oIttjUlBdeLIE8IHM3Jev81l/0bqMra+VdYIWinVdKEt3sgoJeQTMx6OVn3Y0J4mWZTtDkNfs8OgKrTmAmUpivtXU1pBIjwrZq1F8Th3DF0TxkabXAHj6rRqUKKzA9GgTKQyWupIr1XxtlN1ewiqTo7fGcb2A0UmUALfEZYmTXl/3837ZHj+EhzjzwgBUbEntm 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: Currently, we calculate the zswap global limit, and potentially the acceptance threshold in the zswap, in pages in the zswap store path. This is unnecessary because the values rarely change. Instead, precalculate the them when the module parameters are updated, which should be rare. Since we are adding custom handlers for setting the percentages now, add proper validation that they are <= 100. Suggested-by: Johannes Weiner Signed-off-by: Yosry Ahmed --- mm/zswap.c | 86 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 70 insertions(+), 16 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 1cf3ab4b22e64..832e3f56232f0 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -116,12 +116,29 @@ module_param_cb(zpool, &zswap_zpool_param_ops, &zswap_zpool_type, 0644); /* The maximum percentage of memory that the compressed pool can occupy */ static unsigned int zswap_max_pool_percent = 20; -module_param_named(max_pool_percent, zswap_max_pool_percent, uint, 0644); +static unsigned long zswap_max_pages; + +static int zswap_max_pool_param_set(const char *, + const struct kernel_param *); +static const struct kernel_param_ops zswap_max_pool_param_ops = { + .set = zswap_max_pool_param_set, + .get = param_get_uint, +}; +module_param_cb(max_pool_percent, &zswap_max_pool_param_ops, + &zswap_max_pool_percent, 0644); /* The threshold for accepting new pages after the max_pool_percent was hit */ static unsigned int zswap_accept_thr_percent = 90; /* of max pool size */ -module_param_named(accept_threshold_percent, zswap_accept_thr_percent, - uint, 0644); +unsigned long zswap_accept_thr_pages; + +static int zswap_accept_thr_param_set(const char *, + const struct kernel_param *); +static const struct kernel_param_ops zswap_accept_thr_param_ops = { + .set = zswap_accept_thr_param_set, + .get = param_get_uint, +}; +module_param_cb(accept_threshold_percent, &zswap_accept_thr_param_ops, + &zswap_accept_thr_percent, 0644); /* * Enable/disable handling same-value filled pages (enabled by default). @@ -490,14 +507,16 @@ static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor) return NULL; } -static unsigned long zswap_max_pages(void) +static void zswap_update_max_pages(void) { - return totalram_pages() * zswap_max_pool_percent / 100; + WRITE_ONCE(zswap_max_pages, + totalram_pages() * zswap_max_pool_percent / 100); } -static unsigned long zswap_accept_thr_pages(void) +static void zswap_update_accept_thr_pages(void) { - return zswap_max_pages() * zswap_accept_thr_percent / 100; + WRITE_ONCE(zswap_accept_thr_pages, + READ_ONCE(zswap_max_pages) * zswap_accept_thr_percent / 100); } unsigned long zswap_total_pages(void) @@ -684,6 +703,43 @@ static int zswap_enabled_param_set(const char *val, return ret; } +static int __zswap_percent_param_set(const char *val, + const struct kernel_param *kp) +{ + unsigned int n; + int ret; + + ret = kstrtouint(val, 10, &n); + if (ret || n > 100) + return -EINVAL; + + return param_set_uint(val, kp); +} + +static int zswap_max_pool_param_set(const char *val, + const struct kernel_param *kp) +{ + int err = __zswap_percent_param_set(val, kp); + + if (!err) { + zswap_update_max_pages(); + zswap_update_accept_thr_pages(); + } + + return err; +} + +static int zswap_accept_thr_param_set(const char *val, + const struct kernel_param *kp) +{ + int err = __zswap_percent_param_set(val, kp); + + if (!err) + zswap_update_accept_thr_pages(); + + return err; +} + /********************************* * lru functions **********************************/ @@ -1305,10 +1361,6 @@ static void shrink_worker(struct work_struct *w) { struct mem_cgroup *memcg; int ret, failures = 0; - unsigned long thr; - - /* Reclaim down to the accept threshold */ - thr = zswap_accept_thr_pages(); /* global reclaim will select cgroup in a round-robin fashion. */ do { @@ -1358,7 +1410,8 @@ static void shrink_worker(struct work_struct *w) break; resched: cond_resched(); - } while (zswap_total_pages() > thr); + /* Reclaim down to the accept threshold */ + } while (zswap_total_pages() > READ_ONCE(zswap_accept_thr_pages)); } static int zswap_is_page_same_filled(void *ptr, unsigned long *value) @@ -1424,16 +1477,14 @@ bool zswap_store(struct folio *folio) /* Check global limits */ cur_pages = zswap_total_pages(); - max_pages = zswap_max_pages(); - - if (cur_pages >= max_pages) { + if (cur_pages >= READ_ONCE(zswap_max_pages)) { zswap_pool_limit_hit++; zswap_pool_reached_full = true; goto reject; } if (zswap_pool_reached_full) { - if (cur_pages > zswap_accept_thr_pages()) + if (cur_pages > READ_ONCE(zswap_accept_thr_pages)) goto reject; else zswap_pool_reached_full = false; @@ -1734,6 +1785,9 @@ static int zswap_setup(void) zswap_enabled = false; } + zswap_update_max_pages(); + zswap_update_accept_thr_pages(); + if (zswap_debugfs_init()) pr_warn("debugfs initialization failed\n"); zswap_init_state = ZSWAP_INIT_SUCCEED;