From patchwork Tue Apr 11 09:36:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Shixin X-Patchwork-Id: 13207215 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 8EA9CC77B72 for ; Tue, 11 Apr 2023 08:46:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 006376B0115; Tue, 11 Apr 2023 04:46:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F07066B0114; Tue, 11 Apr 2023 04:46:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DBEFC6B0116; Tue, 11 Apr 2023 04:46:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id CD85A6B0114 for ; Tue, 11 Apr 2023 04:46:38 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 9B268A0A84 for ; Tue, 11 Apr 2023 08:46:38 +0000 (UTC) X-FDA: 80668479276.26.11E6BBD Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf06.hostedemail.com (Postfix) with ESMTP id 17B83180009 for ; Tue, 11 Apr 2023 08:46:34 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=none; spf=pass (imf06.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=liushixin2@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681202795; a=rsa-sha256; cv=none; b=t/vuY7rjBhwkL/e7p4s/bGqFzpQgWzXPuy9P+v3CfWjUtIBF24q8ipH3imPXE9FWAET0L8 CQTzHP9huqORJlNsmZtNl9vBPPq2d8YYzhGZHMgpNAfj5lOrtkTnyBGmQQuUa7VwAl1/mG 0kIzc/wYsMMUqnJS1aD0Ysd9Kso12Vw= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=none; spf=pass (imf06.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=liushixin2@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681202795; 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=Eohi5X/qwmbm1UVCekfJ1YLnYtTqNIOSNTla4RtlBGQ=; b=WO/K/n4U5Q4CfY5gbVNKQgsRMz04QzCcrm9THciUB8cHtN/Mv/z/FB/Ztp3Zg0ZvzONywn RwJvGm+Rf7i2PvT1waTMqUyYoWA7YaTlXbDieQvDP4AJWIl8gp6JhEBbDsO7zZAOAorPN8 L2a0mvTjQYA9ub1DR/FVJf0XAZkUp9s= Received: from dggpemm100009.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4PwfYL17MqznVXb; Tue, 11 Apr 2023 16:45:10 +0800 (CST) Received: from huawei.com (10.175.113.32) by dggpemm100009.china.huawei.com (7.185.36.113) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Tue, 11 Apr 2023 16:46:31 +0800 From: Liu Shixin To: Seth Jennings , Dan Streetman , Vitaly Wool , Andrew Morton , Nathan Chancellor , Christoph Hellwig CC: , , Liu Shixin Subject: [PATCH -next v9 3/3] mm/zswap: delay the initialization of zswap Date: Tue, 11 Apr 2023 17:36:32 +0800 Message-ID: <20230411093632.822290-4-liushixin2@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230411093632.822290-1-liushixin2@huawei.com> References: <20230411093632.822290-1-liushixin2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpemm100009.china.huawei.com (7.185.36.113) X-CFilter-Loop: Reflected X-Rspam-User: X-Rspamd-Queue-Id: 17B83180009 X-Rspamd-Server: rspam01 X-Stat-Signature: 15wzomimsrr7f8ebw4k6ia8w1qn45a6f X-HE-Tag: 1681202794-220183 X-HE-Meta: U2FsdGVkX19IEtJWjdx5UGJQrkw03CcQAhtwfDJuGTz29yDvtOsdm6Hd8RRwKxb24ylux3DtaPilLivUex0oYuzHCk/AvHBuTg/N/IXjUNIi3ELSuoFkrhcH77rhxBqfycsDKAZj47pzcnUlZJbaM6cncJhePUz/p+kx9tqmM04QmLnttNzfkymV7tmwll+fkqsIksICxeNsOavzobAhrH5ehDQisyYRZtc8QEBbGqzsK5C3Zm3vkTFK8AkU0g33fTfM/7IGMltSpzyT9iZkEpmv8yKmnUGtfgadjGvlekw+rfSphcMvRP9t+7fBTPYJglHrga1q0EynXIInAptRgArDQruKkHN7K0mjzWGGlQuyRiv9fxRm7UpQ2Pq2doHCY7+26CzSLJEo5I3puvt9V1dQZvyRh2Nm1B8AcVOIXA57z29VBk6zDw9Px60p79LdqdrZcUHwt8CXTIFA4MN2WlOfEdCnAxoQzOlZfmpp1wry0+TS0Z5KAWVtdHxgbq/4kFkJI6G20ynIyGNRwjCw/CGoItal/EgthRuFtKEqnjVb/U8UnP5AV2aBSMp1nRziarprX4ATZcFw9XFjOScD5JoUoztPKOv0JAKWtdio5nP2XyxoRok4aAedfZl1AjJ2CkUBUpE/bfq9J4IfXLSooyDTJKm42MCapjCA7H226P5s2R8T+DncmsIsN090BFYUcRngncOCut7YKqkUzkW9Y4mq2YOH0ZSA3Wa2ikhjpgRCW0A+BBiMR7h5Cjzap+hq/y6NCHvNhFIzh7cjNN3wSgp2Jef+F+Ri73r151HoU6ZceJdpG75XWyTwKAIlxsDgDw8hFb5rCyFiCv+YQIWo0wvw/88PSvjVnbVnSXfxqoUC9WY3LxiSA0NaEgmQdX8LjUXNM+ljPP5sGSVIzStgsceSf8vgW99l8/AGmxnZe+xK0cSbWP0u+sm5YNCZ+EFv8wAER7haEx4Hh+dj6Jf KUQmtYW1 dzhOebLRbuQKktQ9Z7AQ8p30oqMo5VYQ2KgUv5nsBZZblEp63+R0ZnYgC8wtm/AtwOkBFT2dPSBJnsn47z8v8q4B2cseOmibiBXzg3rweOQ6KtZmTQ+NC+TNgiaQ3tbgAixMXaSSl1VzEkbjYlBzUItFvZx/gxClOeUjHQ4M4dN4rs5rEkY3vdDMQSuYvjb2PzFTBD9kHaHqlkxTQTSbT5zDfgXJYoqgqm9mo 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: Since some users may not use zswap, the zswap_pool is wasted. Save memory by delaying the initialization of zswap until enabled. Signed-off-by: Liu Shixin Reviewed-by: Christoph Hellwig --- mm/zswap.c | 71 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 17 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 9169c2baee87..af97e8f9d678 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -81,6 +81,8 @@ static bool zswap_pool_reached_full; #define ZSWAP_PARAM_UNSET "" +static int zswap_setup(void); + /* Enable/disable zswap */ static bool zswap_enabled = IS_ENABLED(CONFIG_ZSWAP_DEFAULT_ON); static int zswap_enabled_param_set(const char *, @@ -222,6 +224,9 @@ enum zswap_init_type { static enum zswap_init_type zswap_init_state; +/* used to ensure the integrity of initialization */ +static DEFINE_MUTEX(zswap_init_lock); + /* init completed, but couldn't create the initial pool */ static bool zswap_has_pool; @@ -654,7 +659,7 @@ static struct zswap_pool *zswap_pool_create(char *type, char *compressor) return NULL; } -static __init struct zswap_pool *__zswap_pool_create_fallback(void) +static struct zswap_pool *__zswap_pool_create_fallback(void) { bool has_comp, has_zpool; @@ -755,29 +760,43 @@ static void zswap_pool_put(struct zswap_pool *pool) * param callbacks **********************************/ +static bool zswap_pool_changed(const char *s, const struct kernel_param *kp) +{ + /* no change required */ + if (!strcmp(s, *(char **)kp->arg) && zswap_has_pool) + return false; + return true; +} + /* val must be a null-terminated string */ static int __zswap_param_set(const char *val, const struct kernel_param *kp, char *type, char *compressor) { struct zswap_pool *pool, *put_pool = NULL; char *s = strstrip((char *)val); - int ret; + int ret = 0; + bool new_pool = false; + mutex_lock(&zswap_init_lock); switch (zswap_init_state) { case ZSWAP_UNINIT: /* if this is load-time (pre-init) param setting, * don't create a pool; that's done during init. */ - return param_set_charp(s, kp); + ret = param_set_charp(s, kp); + break; case ZSWAP_INIT_SUCCEED: - /* no change required */ - if (!strcmp(s, *(char **)kp->arg) && zswap_has_pool) - return 0; + new_pool = zswap_pool_changed(s, kp); break; case ZSWAP_INIT_FAILED: pr_err("can't set param, initialization failed\n"); - return -ENODEV; + ret = -ENODEV; } + mutex_unlock(&zswap_init_lock); + + /* no need to create a new pool, return directly */ + if (!new_pool) + return ret; if (!type) { if (!zpool_has_pool(s)) { @@ -867,19 +886,30 @@ static int zswap_zpool_param_set(const char *val, static int zswap_enabled_param_set(const char *val, const struct kernel_param *kp) { + int ret = -ENODEV; + + /* if this is load-time (pre-init) param setting, only set param. */ + if (system_state != SYSTEM_RUNNING) + return param_set_bool(val, kp); + + mutex_lock(&zswap_init_lock); switch (zswap_init_state) { case ZSWAP_UNINIT: - return param_set_bool(val, kp); + if (zswap_setup()) + break; + fallthrough; case ZSWAP_INIT_SUCCEED: - if (!zswap_has_pool) { + if (!zswap_has_pool) pr_err("can't enable, no pool configured\n"); - return -ENODEV; - } else - return param_set_bool(val, kp); + else + ret = param_set_bool(val, kp); + break; case ZSWAP_INIT_FAILED: pr_err("can't enable, initialization failed\n"); - return -ENODEV; } + mutex_unlock(&zswap_init_lock); + + return ret; } /********************************* @@ -1437,7 +1467,7 @@ static const struct frontswap_ops zswap_frontswap_ops = { static struct dentry *zswap_debugfs_root; -static int __init zswap_debugfs_init(void) +static int zswap_debugfs_init(void) { if (!debugfs_initialized()) return -ENODEV; @@ -1468,7 +1498,7 @@ static int __init zswap_debugfs_init(void) return 0; } #else -static int __init zswap_debugfs_init(void) +static int zswap_debugfs_init(void) { return 0; } @@ -1477,7 +1507,7 @@ static int __init zswap_debugfs_init(void) /********************************* * module init and exit **********************************/ -static int __init init_zswap(void) +static int zswap_setup(void) { struct zswap_pool *pool; int ret; @@ -1540,8 +1570,15 @@ static int __init init_zswap(void) zswap_enabled = false; return -ENOMEM; } + +static int __init zswap_init(void) +{ + if (!zswap_enabled) + return 0; + return zswap_setup(); +} /* must be late so crypto has time to come up */ -late_initcall(init_zswap); +late_initcall(zswap_init); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Seth Jennings ");