From patchwork Sun Feb 11 13:57:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13552454 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 D0783C4829A for ; Sun, 11 Feb 2024 13:58:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 209276B0072; Sun, 11 Feb 2024 08:58:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1BA086B0074; Sun, 11 Feb 2024 08:58:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 059686B0075; Sun, 11 Feb 2024 08:58:01 -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 E985B6B0072 for ; Sun, 11 Feb 2024 08:58:01 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 681E8A08C6 for ; Sun, 11 Feb 2024 13:58:01 +0000 (UTC) X-FDA: 81779676762.28.7916854 Received: from out-180.mta0.migadu.com (out-180.mta0.migadu.com [91.218.175.180]) by imf25.hostedemail.com (Postfix) with ESMTP id 94672A0008 for ; Sun, 11 Feb 2024 13:57:59 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine); spf=pass (imf25.hostedemail.com: domain of chengming.zhou@linux.dev designates 91.218.175.180 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=1707659879; 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=dSkpL3FRCtrOE4nwTPi1hKkoSuhcrww4mJS5Y0Oil5A=; b=Z9G/d4IlPO256UI7iWjFwHBJfk2r/TKugm29xY9W2BdhiwHN2QnlCTK4tFW05KLXUQ0k7T OdEvuVd+u89PjJSGIyfUjVrdy7XRT5qVT/GPqcCJfYcnod9/9P4oNBweOVf0CjcHC/z70C MaOIYN/N9sdO4zIwk2BjcBsXQMx7WA0= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=bytedance.com (policy=quarantine); spf=pass (imf25.hostedemail.com: domain of chengming.zhou@linux.dev designates 91.218.175.180 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707659879; a=rsa-sha256; cv=none; b=ztoYw4iyEVqGg0dtVwviB1fkREwpOCzbp8jz3cDKgcF9jemnfgafNBElEUbmfVwVCgw3Es oCG3yOaLr5nsNcKgr2bta+Bmo/kL3pCqq2WYdf0FcsQmpIXr3cwUSvhfzwFGBYEWMTvL+9 wWdsDTOgN+VGCvPoWh1pFM1Nn400jaY= 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:04 +0000 Subject: [PATCH 1/2] mm/zswap: global lru and shrinker shared by all zswap_pools MIME-Version: 1.0 Message-Id: <20240210-zswap-global-lru-v1-1-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-Queue-Id: 94672A0008 X-Rspamd-Server: rspam02 X-Rspam-User: X-Stat-Signature: x57ib8xukc89kfyxn144jebqhmjumkbx X-Rspamd-Pre-Result: action=add header; module=dmarc; Action set by DMARC X-Rspam: Yes X-HE-Tag: 1707659879-733634 X-HE-Meta: U2FsdGVkX1/EvrmqmlypLLOzhcSshXFMLv4vvBX4pjAQtHok6Tjtgs/VNK1b2/8TtNnibwqW9QBj7wa0ECVTSvkG+IzKHaitmZDMsjtEfojPS+M9/atYOgp6q5JS212iaptAw2KEOeB2/KE2AwQ3rYqBsP8pz8SyDgG949JfzxKiCwH3zdm4Vv3u0EyA8UtGFBvm/bImey9+LbgLbha6S2SM6KJnT5CXBFn03owevToLMYohoLRlQ6+t5ddLt739zFVGsahtnZiI0EuWrcDepOhJsK/cvHNyka/qb5Io02LJMRLpZDwgIz2Nd+cMzSkP8Tf51S0BQkCFiVYPrSFoVsXqE8BB98tJZevmXIjRY/yfz/PWxfgoDRidbNUBTOxTTuZ3Tw3hM1xOR/shQbuCgSCHZx5yc6UTI1DPnbNQnFEch9Rg/lUvlu634nMVXX7uZh7/8drGYIf4y30nAmd/ud9rZeMXnJo43qUwUu3NaFVZ0eCHCh1LoqaBoxkBo0Tj3/tBH2/jYdS2QLPTVMF+YqbPcwzKH1sJIo9BVMHjyAMS948b8lYOeOsNlckZ+f5T0+ZSBfNBQr6RYdLOrvn2/xnkaH2aNNCfYiOnfEmHvVCNOE/sRsFUf6G0shSKvWuEBpf8GC0PdgcD4xkZTa+WMGdPe6GBkvH/0NbUt1ZbW8j6zh5tjhJUPQsET3B2VSd0V2+ifDP+GXzDDGuq8+7BZY7Ghn3zaninODQAMktnV7UoUcrPl8JDHvOXfO4/pZ2EeEzzZW1tCdwNjXENG2j4nU++BzTzrrCAa6I2QkB28+VHBw13Dv09ApERJ623wOOPSivZa51TG07vDdmbjaWGyxh2JmHaUYuyx/0I6ShCrfzdlurD9teMvwncCvvOmoerlRco5L8Vj6zJb05oUBt0HEZirx00vhUGsXF3xI32aWelqyKnFt/0vxMz+iAO0eQBi5xOqNvkVaL2L6GSlVb GA7cOwXs rxDHggX9Vy2gHp7FHyt/nuAG9oDEBrqIJgFOyB5TQ9oBDkHngYlvT9e4SS32N7KciQFdOMwVMyBqaKP9HGriAmWVhBNvs+LqbMPnFW+6U/SEgpYGzcc8PzEHvtBHHloFynxTa0uMM/0AfpyAjKRJs7S6bpwWoANKooaao4SDBwY60hI43/rMCjXt5SGZk68Ro6kNMn+oZsrZPe2NH6eaqvxNwQk4Xr8k0ZTplhh3eQIm9VFRr3ebWKG6Y9d5TfeXNg6Rz0ppLYs2zvWJZub4+DwN/4VNKC5thCDNANhE3AtgZN+6nzBft0sE8xZgMWkFcFbTPaoVABOxqMpCQ2nxIzi+u6Yafd8IJ4aop/cp/QYNvwFWarIf6WiEPaQ== 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: Dynamic zswap_pool creation may create/reuse to have multiple zswap_pools in a list, only the first will be current used. Each zswap_pool has its own lru and shrinker, which is not necessary and has its problem: 1. When memory has pressure, all shrinker of zswap_pools will try to shrink its own lru, there is no order between them. 2. When zswap limit hit, only the last zswap_pool's shrink_work will try to shrink its lru, which is inefficient. Anyway, having a global lru and shrinker shared by all zswap_pools is better and efficient. Signed-off-by: Chengming Zhou --- mm/zswap.c | 153 ++++++++++++++++++++++--------------------------------------- 1 file changed, 55 insertions(+), 98 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 62fe307521c9..7668db8c10e3 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -176,14 +176,17 @@ struct zswap_pool { struct kref kref; struct list_head list; struct work_struct release_work; - struct work_struct shrink_work; struct hlist_node node; char tfm_name[CRYPTO_MAX_ALG_NAME]; +}; + +struct { struct list_lru list_lru; - struct mem_cgroup *next_shrink; - struct shrinker *shrinker; atomic_t nr_stored; -}; + struct shrinker *shrinker; + struct work_struct shrink_work; + struct mem_cgroup *next_shrink; +} zswap; /* * struct zswap_entry @@ -301,9 +304,6 @@ static void zswap_update_total_size(void) * pool functions **********************************/ -static void zswap_alloc_shrinker(struct zswap_pool *pool); -static void shrink_worker(struct work_struct *w); - static struct zswap_pool *zswap_pool_create(char *type, char *compressor) { int i; @@ -353,30 +353,16 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor) if (ret) goto error; - zswap_alloc_shrinker(pool); - if (!pool->shrinker) - goto error; - - pr_debug("using %s compressor\n", pool->tfm_name); - /* 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); INIT_LIST_HEAD(&pool->list); - if (list_lru_init_memcg(&pool->list_lru, pool->shrinker)) - goto lru_fail; - shrinker_register(pool->shrinker); - INIT_WORK(&pool->shrink_work, shrink_worker); - atomic_set(&pool->nr_stored, 0); zswap_pool_debug("created", pool); return pool; -lru_fail: - list_lru_destroy(&pool->list_lru); - shrinker_free(pool->shrinker); error: if (pool->acomp_ctx) free_percpu(pool->acomp_ctx); @@ -434,15 +420,8 @@ static void zswap_pool_destroy(struct zswap_pool *pool) zswap_pool_debug("destroying", pool); - shrinker_free(pool->shrinker); cpuhp_state_remove_instance(CPUHP_MM_ZSWP_POOL_PREPARE, &pool->node); free_percpu(pool->acomp_ctx); - list_lru_destroy(&pool->list_lru); - - spin_lock(&zswap_pools_lock); - mem_cgroup_iter_break(NULL, pool->next_shrink); - pool->next_shrink = NULL; - spin_unlock(&zswap_pools_lock); for (i = 0; i < ZSWAP_NR_ZPOOLS; i++) zpool_destroy_pool(pool->zpools[i]); @@ -529,24 +508,6 @@ static struct zswap_pool *zswap_pool_current_get(void) return pool; } -static struct zswap_pool *zswap_pool_last_get(void) -{ - struct zswap_pool *pool, *last = NULL; - - rcu_read_lock(); - - list_for_each_entry_rcu(pool, &zswap_pools, list) - last = pool; - WARN_ONCE(!last && zswap_has_pool, - "%s: no page storage pool!\n", __func__); - if (!zswap_pool_get(last)) - last = NULL; - - rcu_read_unlock(); - - return last; -} - /* type and compressor must be null-terminated */ static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor) { @@ -816,14 +777,10 @@ void zswap_folio_swapin(struct folio *folio) void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) { - struct zswap_pool *pool; - - /* lock out zswap pools list modification */ + /* lock out zswap shrinker walking memcg tree */ spin_lock(&zswap_pools_lock); - list_for_each_entry(pool, &zswap_pools, list) { - if (pool->next_shrink == memcg) - pool->next_shrink = mem_cgroup_iter(NULL, pool->next_shrink, NULL); - } + if (zswap.next_shrink == memcg) + zswap.next_shrink = mem_cgroup_iter(NULL, zswap.next_shrink, NULL); spin_unlock(&zswap_pools_lock); } @@ -923,9 +880,9 @@ static void zswap_entry_free(struct zswap_entry *entry) if (!entry->length) atomic_dec(&zswap_same_filled_pages); else { - zswap_lru_del(&entry->pool->list_lru, entry); + zswap_lru_del(&zswap.list_lru, entry); zpool_free(zswap_find_zpool(entry), entry->handle); - atomic_dec(&entry->pool->nr_stored); + atomic_dec(&zswap.nr_stored); zswap_pool_put(entry->pool); } if (entry->objcg) { @@ -1288,7 +1245,6 @@ static unsigned long zswap_shrinker_scan(struct shrinker *shrinker, { struct lruvec *lruvec = mem_cgroup_lruvec(sc->memcg, NODE_DATA(sc->nid)); unsigned long shrink_ret, nr_protected, lru_size; - struct zswap_pool *pool = shrinker->private_data; bool encountered_page_in_swapcache = false; if (!zswap_shrinker_enabled || @@ -1299,7 +1255,7 @@ static unsigned long zswap_shrinker_scan(struct shrinker *shrinker, nr_protected = atomic_long_read(&lruvec->zswap_lruvec_state.nr_zswap_protected); - lru_size = list_lru_shrink_count(&pool->list_lru, sc); + lru_size = list_lru_shrink_count(&zswap.list_lru, sc); /* * Abort if we are shrinking into the protected region. @@ -1316,7 +1272,7 @@ static unsigned long zswap_shrinker_scan(struct shrinker *shrinker, return SHRINK_STOP; } - shrink_ret = list_lru_shrink_walk(&pool->list_lru, sc, &shrink_memcg_cb, + shrink_ret = list_lru_shrink_walk(&zswap.list_lru, sc, &shrink_memcg_cb, &encountered_page_in_swapcache); if (encountered_page_in_swapcache) @@ -1328,7 +1284,6 @@ static unsigned long zswap_shrinker_scan(struct shrinker *shrinker, static unsigned long zswap_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc) { - struct zswap_pool *pool = shrinker->private_data; struct mem_cgroup *memcg = sc->memcg; struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(sc->nid)); unsigned long nr_backing, nr_stored, nr_freeable, nr_protected; @@ -1343,7 +1298,7 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker, #else /* use pool stats instead of memcg stats */ nr_backing = get_zswap_pool_size(pool) >> PAGE_SHIFT; - nr_stored = atomic_read(&pool->nr_stored); + nr_stored = atomic_read(&zswap.nr_stored); #endif if (!nr_stored) @@ -1351,7 +1306,7 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker, nr_protected = atomic_long_read(&lruvec->zswap_lruvec_state.nr_zswap_protected); - nr_freeable = list_lru_shrink_count(&pool->list_lru, sc); + nr_freeable = list_lru_shrink_count(&zswap.list_lru, sc); /* * Subtract the lru size by an estimate of the number of pages * that should be protected. @@ -1367,23 +1322,24 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker, return mult_frac(nr_freeable, nr_backing, nr_stored); } -static void zswap_alloc_shrinker(struct zswap_pool *pool) +static struct shrinker *zswap_alloc_shrinker(void) { - pool->shrinker = + struct shrinker *shrinker; + + shrinker = shrinker_alloc(SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE, "mm-zswap"); - if (!pool->shrinker) - return; + if (!shrinker) + return NULL; - pool->shrinker->private_data = pool; - pool->shrinker->scan_objects = zswap_shrinker_scan; - pool->shrinker->count_objects = zswap_shrinker_count; - pool->shrinker->batch = 0; - pool->shrinker->seeks = DEFAULT_SEEKS; + shrinker->scan_objects = zswap_shrinker_scan; + shrinker->count_objects = zswap_shrinker_count; + shrinker->batch = 0; + shrinker->seeks = DEFAULT_SEEKS; + return shrinker; } static int shrink_memcg(struct mem_cgroup *memcg) { - struct zswap_pool *pool; int nid, shrunk = 0; if (!mem_cgroup_zswap_writeback_enabled(memcg)) @@ -1396,32 +1352,25 @@ static int shrink_memcg(struct mem_cgroup *memcg) if (memcg && !mem_cgroup_online(memcg)) return -ENOENT; - pool = zswap_pool_current_get(); - if (!pool) - return -EINVAL; - for_each_node_state(nid, N_NORMAL_MEMORY) { unsigned long nr_to_walk = 1; - shrunk += list_lru_walk_one(&pool->list_lru, nid, memcg, + shrunk += list_lru_walk_one(&zswap.list_lru, nid, memcg, &shrink_memcg_cb, NULL, &nr_to_walk); } - zswap_pool_put(pool); return shrunk ? 0 : -EAGAIN; } static void shrink_worker(struct work_struct *w) { - struct zswap_pool *pool = container_of(w, typeof(*pool), - shrink_work); struct mem_cgroup *memcg; int ret, failures = 0; /* global reclaim will select cgroup in a round-robin fashion. */ do { spin_lock(&zswap_pools_lock); - pool->next_shrink = mem_cgroup_iter(NULL, pool->next_shrink, NULL); - memcg = pool->next_shrink; + zswap.next_shrink = mem_cgroup_iter(NULL, zswap.next_shrink, NULL); + memcg = zswap.next_shrink; /* * We need to retry if we have gone through a full round trip, or if we @@ -1445,7 +1394,7 @@ static void shrink_worker(struct work_struct *w) if (!mem_cgroup_tryget_online(memcg)) { /* drop the reference from mem_cgroup_iter() */ mem_cgroup_iter_break(NULL, memcg); - pool->next_shrink = NULL; + zswap.next_shrink = NULL; spin_unlock(&zswap_pools_lock); if (++failures == MAX_RECLAIM_RETRIES) @@ -1467,7 +1416,6 @@ static void shrink_worker(struct work_struct *w) resched: cond_resched(); } while (!zswap_can_accept()); - zswap_pool_put(pool); } static int zswap_is_page_same_filled(void *ptr, unsigned long *value) @@ -1508,7 +1456,6 @@ bool zswap_store(struct folio *folio) struct zswap_entry *entry, *dupentry; struct obj_cgroup *objcg = NULL; struct mem_cgroup *memcg = NULL; - struct zswap_pool *shrink_pool; VM_WARN_ON_ONCE(!folio_test_locked(folio)); VM_WARN_ON_ONCE(!folio_test_swapcache(folio)); @@ -1576,7 +1523,7 @@ bool zswap_store(struct folio *folio) if (objcg) { memcg = get_mem_cgroup_from_objcg(objcg); - if (memcg_list_lru_alloc(memcg, &entry->pool->list_lru, GFP_KERNEL)) { + if (memcg_list_lru_alloc(memcg, &zswap.list_lru, GFP_KERNEL)) { mem_cgroup_put(memcg); goto put_pool; } @@ -1607,8 +1554,8 @@ bool zswap_store(struct folio *folio) } if (entry->length) { INIT_LIST_HEAD(&entry->lru); - zswap_lru_add(&entry->pool->list_lru, entry); - atomic_inc(&entry->pool->nr_stored); + zswap_lru_add(&zswap.list_lru, entry); + atomic_inc(&zswap.nr_stored); } spin_unlock(&tree->lock); @@ -1640,9 +1587,7 @@ bool zswap_store(struct folio *folio) return false; shrink: - shrink_pool = zswap_pool_last_get(); - if (shrink_pool && !queue_work(shrink_wq, &shrink_pool->shrink_work)) - zswap_pool_put(shrink_pool); + queue_work(shrink_wq, &zswap.shrink_work); goto reject; } @@ -1804,6 +1749,21 @@ static int zswap_setup(void) if (ret) goto hp_fail; + shrink_wq = alloc_workqueue("zswap-shrink", + WQ_UNBOUND|WQ_MEM_RECLAIM, 1); + if (!shrink_wq) + goto hp_fail; + + zswap.shrinker = zswap_alloc_shrinker(); + if (!zswap.shrinker) + goto shrinker_fail; + if (list_lru_init_memcg(&zswap.list_lru, zswap.shrinker)) + goto lru_fail; + shrinker_register(zswap.shrinker); + + INIT_WORK(&zswap.shrink_work, shrink_worker); + atomic_set(&zswap.nr_stored, 0); + pool = __zswap_pool_create_fallback(); if (pool) { pr_info("loaded using pool %s/%s\n", pool->tfm_name, @@ -1815,19 +1775,16 @@ static int zswap_setup(void) zswap_enabled = false; } - shrink_wq = alloc_workqueue("zswap-shrink", - WQ_UNBOUND|WQ_MEM_RECLAIM, 1); - if (!shrink_wq) - goto fallback_fail; - if (zswap_debugfs_init()) pr_warn("debugfs initialization failed\n"); zswap_init_state = ZSWAP_INIT_SUCCEED; return 0; -fallback_fail: - if (pool) - zswap_pool_destroy(pool); +lru_fail: + list_lru_destroy(&zswap.list_lru); + shrinker_free(zswap.shrinker); +shrinker_fail: + destroy_workqueue(shrink_wq); hp_fail: kmem_cache_destroy(zswap_entry_cache); cache_fail: 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; }