From patchwork Sun Feb 11 13:57:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13552455 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 19BAAC4829B for ; Sun, 11 Feb 2024 13:58:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EB3F66B0075; Sun, 11 Feb 2024 08:58:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E65256B0078; Sun, 11 Feb 2024 08:58:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CB7696B007B; Sun, 11 Feb 2024 08:58:03 -0500 (EST) 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 BC63C6B0075 for ; Sun, 11 Feb 2024 08:58:03 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9EB31A1D16 for ; Sun, 11 Feb 2024 13:58:03 +0000 (UTC) X-FDA: 81779676846.20.74E4094 Received: from out-181.mta0.migadu.com (out-181.mta0.migadu.com [91.218.175.181]) by imf11.hostedemail.com (Postfix) with ESMTP id 06DF740013 for ; Sun, 11 Feb 2024 13:58:01 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine); spf=pass (imf11.hostedemail.com: domain of chengming.zhou@linux.dev designates 91.218.175.181 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707659882; 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; bh=fLuFcG5SfuPq84iyFC7+zt3FPoyT762u4Fiqjstb1dc=; b=CmGExZw3A2aBvI7sCPQFD1tJF9d9H81XULz5SCwTmzvn3ewxdQkNEHPEQ1Ofxq1Krh2G50 9gJJWyIEwYoOSrnavYQDa5jr8X0wdhnkzBDS5Jxi1YpTugGMWHsQEXG2+mgNn4dz7nNqyu TB++Qu0BOojI98cb4tuaSMVO24U7MoQ= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine); spf=pass (imf11.hostedemail.com: domain of chengming.zhou@linux.dev designates 91.218.175.181 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707659882; a=rsa-sha256; cv=none; b=sXz+m3VAFtHZr8aC4fZtMtLRIhgxJ5bBofg5P8S3aThw1U+Ppm1sJ/3tGOi0O6DUq41hHv obf9TJ0EP32Yq43YscOnx9/76y7bG9Leeemqw8cjLfwHYdR+fhjUM70PYkyvKQoll986If 2O3ZxEKFTfkn9S5aIXo7xw81fcAvRoI= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Chengming Zhou Date: Sun, 11 Feb 2024 13:57:05 +0000 Subject: [PATCH 2/2] mm/zswap: change zswap_pool kref to percpu_ref MIME-Version: 1.0 Message-Id: <20240210-zswap-global-lru-v1-2-853473d7b0da@bytedance.com> References: <20240210-zswap-global-lru-v1-0-853473d7b0da@bytedance.com> In-Reply-To: <20240210-zswap-global-lru-v1-0-853473d7b0da@bytedance.com> To: Andrew Morton , Johannes Weiner , Yosry Ahmed , Nhat Pham Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Chengming Zhou X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 06DF740013 X-Stat-Signature: c8jd8kqsj713f988879arxs3n9ya4pta X-Rspamd-Pre-Result: action=add header; module=dmarc; Action set by DMARC X-Rspam-User: X-Rspam: Yes X-HE-Tag: 1707659881-476985 X-HE-Meta: U2FsdGVkX1+XYwFKxJ5fjB2b0Wt6xRzmvcEwgfxCK55smcxdXZ9sz+hOSfHMdRc6qg/KG9QKDObadweO4AgBN35ZY7zmnQUrb9Qa4fPsO1ifIsOKkecMUB5eRs/69qVVtYwqVyH5jQH4puKdpukF5JNMDGD2E11LTAYXs57suWF/5niUzvBmFnfOnRqaXBDKiXvbFSk0AYRQHoYsgynPJxgzTaEN1VOQxET9sEyrDvd52ftW6kejL82AwxYMxUrzNZbAl11UKJQOCV8PrDO7+v+1HVSYxDWMycwK3CIHm/Cm55c4TmujEsIZhRACKnpSGDNcHP82+/T0C+2SKORyznrPcmFfNQw7n8PpZ/s3yg8hmrjkxy3zJzVIvWfdHsMNctnmLQRbsCepGi0dTJzNEB9i9YP5EgsfpZkg6VvVo2NlzGEgpthRUjrKaczrzY5J0ZMPHvjehcZthKs7mUgCPbo7whwtf1xvx+vknl0sPE7bHOM4EUYeN0jz8Ws5TRbjwMkYAMYcVudhTX4wIaktR6htP63uSSET1FbPM0KYxzfWSETIjOniSJl2d8DBu/tQLWeyl+JeJPxtdpsrhEnxkqiopMPJYWnWch9CdOLyb9MZtqzYeAswxLSji66BtsiStpr9AH48l30T0zjB6nZzkWMt8DGpNBx50/0RlWSEbQh7+ElkaXfYNxLyf2haS2FgtHpj+exk3cQKZVi6ijDcdi3huGTn0PKpmcE3kxvb8WKM/mm1tK7wG6TL5M6GbrItRDqUDIeD/NkHW/PEWWmPFMQmw4RZBCcUyChmm7rQ5ZebHbH8faK8ii0P0dxcRKkqUqMUgSfj2NnRQKZI1s0mfHdJFAQEntmwu2R/iUZZ/naKxjP/zVqQy0eU8xSN6ZtUQWjwqA4XJmEr3aA6idaNv5gJ2Pd5bf3skcFHPqXJBE59+5YSmsjq1iBMNvtnfl9DQqCgLx1LMm/ao/HAiAb BgBGXW0u 7RrKrC5v0f+a/QClb2rNQvVVk0V1BVSp1VwxxHG2xVB+Q6j6p/HJNpBij6A== 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: All zswap entries will take a reference of zswap_pool when zswap_store(), and drop it when free. Change it to use the percpu_ref is better for scalability performance. Testing kernel build in tmpfs with memory.max=2GB (zswap shrinker and writeback enabled with one 50GB swapfile). mm-unstable zswap-global-lru real 63.20 63.12 user 1061.75 1062.95 sys 268.74 264.44 Signed-off-by: Chengming Zhou --- mm/zswap.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 7668db8c10e3..afb31904fb08 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -173,7 +173,7 @@ struct crypto_acomp_ctx { struct zswap_pool { struct zpool *zpools[ZSWAP_NR_ZPOOLS]; struct crypto_acomp_ctx __percpu *acomp_ctx; - struct kref kref; + struct percpu_ref ref; struct list_head list; struct work_struct release_work; struct hlist_node node; @@ -303,6 +303,7 @@ static void zswap_update_total_size(void) /********************************* * pool functions **********************************/ +static void __zswap_pool_empty(struct percpu_ref *ref); static struct zswap_pool *zswap_pool_create(char *type, char *compressor) { @@ -356,13 +357,18 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor) /* being the current pool takes 1 ref; this func expects the * caller to always add the new pool as the current pool */ - kref_init(&pool->kref); + ret = percpu_ref_init(&pool->ref, __zswap_pool_empty, + PERCPU_REF_ALLOW_REINIT, GFP_KERNEL); + if (ret) + goto ref_fail; INIT_LIST_HEAD(&pool->list); zswap_pool_debug("created", pool); return pool; +ref_fail: + cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node); error: if (pool->acomp_ctx) free_percpu(pool->acomp_ctx); @@ -435,8 +441,8 @@ static void __zswap_pool_release(struct work_struct *work) synchronize_rcu(); - /* nobody should have been able to get a kref... */ - WARN_ON(kref_get_unless_zero(&pool->kref)); + /* nobody should have been able to get a ref... */ + percpu_ref_exit(&pool->ref); /* pool is now off zswap_pools list and has no references. */ zswap_pool_destroy(pool); @@ -444,11 +450,11 @@ static void __zswap_pool_release(struct work_struct *work) static struct zswap_pool *zswap_pool_current(void); -static void __zswap_pool_empty(struct kref *kref) +static void __zswap_pool_empty(struct percpu_ref *ref) { struct zswap_pool *pool; - pool = container_of(kref, typeof(*pool), kref); + pool = container_of(ref, typeof(*pool), ref); spin_lock(&zswap_pools_lock); @@ -467,12 +473,12 @@ static int __must_check zswap_pool_get(struct zswap_pool *pool) if (!pool) return 0; - return kref_get_unless_zero(&pool->kref); + return percpu_ref_tryget(&pool->ref); } static void zswap_pool_put(struct zswap_pool *pool) { - kref_put(&pool->kref, __zswap_pool_empty); + percpu_ref_put(&pool->ref); } static struct zswap_pool *__zswap_pool_current(void) @@ -602,6 +608,12 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp, if (!pool) pool = zswap_pool_create(type, compressor); + else { + /* Resurrect percpu_ref to percpu mode. */ + percpu_ref_resurrect(&pool->ref); + /* Drop the ref from zswap_pool_find_get(). */ + zswap_pool_put(pool); + } if (pool) ret = param_set_charp(s, kp); @@ -640,7 +652,7 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp, * or the new pool we failed to add */ if (put_pool) - zswap_pool_put(put_pool); + percpu_ref_kill(&put_pool->ref); return ret; }