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 7D830C48297 for ; Mon, 12 Feb 2024 13:30:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 088276B0078; Mon, 12 Feb 2024 08:30:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 038726B007E; Mon, 12 Feb 2024 08:30:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E1B926B0080; Mon, 12 Feb 2024 08:30:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id CBB816B0078 for ; Mon, 12 Feb 2024 08:30:02 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 903CE1C1241 for ; Mon, 12 Feb 2024 13:30:02 +0000 (UTC) X-FDA: 81783235044.14.08803DB Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by imf24.hostedemail.com (Postfix) with ESMTP id BBA32180034 for ; Mon, 12 Feb 2024 13:29:59 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=GEnDFYa9; spf=pass (imf24.hostedemail.com: domain of zhouchengming@bytedance.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=zhouchengming@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707744600; 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:dkim-signature; bh=OKUN4IUfFGbPYG1DZS84NOyg5JfyapaOpinkqXF+JM0=; b=r9Jpd87KnmHCxFyykeM/JXwvXWB8vCG95eAjt1L5khgOb1IkQkBI7oU5XwU3gXmVVU5jVA 9ZAumHVlhLJz8641WUSu0vRqMItUCCjhZajA8ps1ku+6oSnfgPGOzdw9C6m5bd+77hpeSz XuRO6TLHV71eQH91RFa/3Zn670NDcoo= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=GEnDFYa9; spf=pass (imf24.hostedemail.com: domain of zhouchengming@bytedance.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=zhouchengming@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707744600; a=rsa-sha256; cv=none; b=h8Qp2ZCpHlOF38DaG+Ae+8hCc4u3Daaau+X/aj1bGVCQ8YGDbGx2taGYYcwDKGBCev8VhX K0uK61/WcN6XHjTUEp8IcxMU5CZYFJebksWRQgiHROWLxLjTEFpuE8NALAgBadCgIef46g 4POgTMlwSSsxkA9q0et8E3WSbCcNPCc= Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-6e0a9e9df2dso578766b3a.0 for ; Mon, 12 Feb 2024 05:29:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1707744598; x=1708349398; darn=kvack.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=OKUN4IUfFGbPYG1DZS84NOyg5JfyapaOpinkqXF+JM0=; b=GEnDFYa9pYjb/TBKAhLqKpMkzQPZ0LbEmYGTqP+/4ZbKpmsY+NZOOkPTZqNGewWglh 1QzzKn3qX2mALNyU029uK3KWbnjWqRQ4wwj6bYwVvOaTEn9gmcwF5iNVIV0sUCLY916l dG39iv2giSKHPK2+GOfurICNQaGurjFBp+0r3GsNHJZI5jTxevnqgU+EJTcxfqs7fDy7 HdutL04RzsuVzBT3ZzENO6oMn+2D+Qpz4ycioDJZCm7TiYfaWTKFa7R3JylaY2H+7cO3 jlN7wG1zbbpWKnqWdP39ivrNTb0A1lyAOSPKujUVFpyzW//FslIQhrqVLGnE9Gd6C0Sy OruA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707744598; x=1708349398; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OKUN4IUfFGbPYG1DZS84NOyg5JfyapaOpinkqXF+JM0=; b=la+GqqFpnkmkfZ/+BfL7QEWAUxK3ad8ZWs0rvhL5aP9qCDA556BboVylMUnLzAZxKs zFbw76OqSDhAoqaD5JVeei6eaxGh85R2Ua5IjZNdq/yOQ89ZC6kuJYtTjHQ+ND5Xe7kX UOljZoRYC8d5HFf8RDh7xxuwCcrNznbpkQSh/wzsKSKZjKCV8OXuiBW397vFOG9E/9Nq Xg8ch9mheQvw02jqNUB5+ikhODGQTGij6Kltqs5C4WwchF41xIXNAYZVM2ZXrppqcp6v h104FaKJ3K0Jpfar+vePGUseYmjuCSiKFoWWl/tAVq4EOKSDiQSV9ByjTdgxzpuRz9tI UyCg== X-Forwarded-Encrypted: i=1; AJvYcCVGSIwEiYqqd2aVx75APG5Q/w619a01RR0iKH3h3TZMIkULsHSeN7YKsF6KToTe49WthGuLPxxFrbIwE9E0qXFwFlA= X-Gm-Message-State: AOJu0YyZBEqbbGGUQzPYBc3uotrFWyPzUnMD9wm77KXib6gLmHktzX/7 X7YNIfQdNTl3Bg2oLD6yClrNXNfc24CHttBNqh8BsJSzAktBN5mJaKz27fueds4= X-Google-Smtp-Source: AGHT+IGi6Ui1vWJezQAjjN3CWL31a2SBj0Cftg5aYyGE6A6tBfoTbuVN6Gxdxxdn49WEkfaRgLladQ== X-Received: by 2002:a05:6a00:9097:b0:6e0:6dbb:38f6 with SMTP id jo23-20020a056a00909700b006e06dbb38f6mr7154294pfb.13.1707744598322; Mon, 12 Feb 2024 05:29:58 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWiDAJrOiEqYdoEXPkZdyq/ALpytRtiTRbKZQm/x9Ss+j4ivlFjCCD/j0Iq6oU5L5bM+QC2TrqTJZMj1OZH9cIGGCczCeyuqXRp4km4Hwk5AurshT9Eqoibrk8X/NKR5BCoZmmOOWex8QXXOy5ZOlpEcM8HjeJWnTr1UG4/WLYYnWn11AU2CTxyQlDI Received: from [10.255.183.165] ([139.177.225.230]) by smtp.gmail.com with ESMTPSA id t10-20020a62d14a000000b006dbda1b19f7sm5730969pfl.159.2024.02.12.05.29.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 12 Feb 2024 05:29:57 -0800 (PST) Message-ID: <900cd5da-da96-4107-b5f0-c7d975a8ba97@bytedance.com> Date: Mon, 12 Feb 2024 21:29:53 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 2/2] mm/zswap: change zswap_pool kref to percpu_ref Content-Language: en-US To: Nhat Pham Cc: Andrew Morton , Johannes Weiner , Yosry Ahmed , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20240210-zswap-global-lru-v1-0-853473d7b0da@bytedance.com> <20240210-zswap-global-lru-v1-2-853473d7b0da@bytedance.com> From: Chengming Zhou In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: BBA32180034 X-Rspam-User: X-Stat-Signature: nrk3juas995d6d6qie61a97oca713ccu X-Rspamd-Server: rspam01 X-HE-Tag: 1707744599-709666 X-HE-Meta: U2FsdGVkX19ioU289+NkUp5oOjL7W/qlo3zs4MaxqDp6AmzeN7M6AcBNT1E71IAm+NIBgfkpua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5vA/sXUvhWxrkPqfGscT/hbxIIBPdxreajRA== 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: On 2024/2/12 05:21, Nhat Pham wrote: > On Sun, Feb 11, 2024 at 5:58 AM Chengming Zhou > wrote: >> >> 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)); > > Do we no longer care about this WARN? IIUC, this is to catch someone > still holding a reference to the pool at release time, which sounds > like a bug. I think we can simulate the similar behavior with: Ok, I thought it has already been put to 0 when we're here, so any tryget will fail. But keeping this WARN_ON() is also fine to me, will keep it. Thanks. > > WARN_ON(percpu_ref_tryget(&pool->ref)) > > no? percpu_ref_tryget() should fail when the refcnt goes back down to > 0. Then we can do percpu_ref_exit() as well. > >> + /* 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; >> } >> >> -- >> b4 0.10.1 > > The rest of the code looks solid to me FWIW. Number seems to indicate > this is a good idea as well.