Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp1224860rwe; Sat, 27 Aug 2022 03:40:11 -0700 (PDT) X-Google-Smtp-Source: AA6agR6r9+gUE2wxtkHxtz/TlE/6OVm/DoNMY/wOftolMgyNSDiNtebACGfXL2oDmt/XsbbM68fT X-Received: by 2002:a17:903:1109:b0:170:a092:c881 with SMTP id n9-20020a170903110900b00170a092c881mr7450509plh.68.1661596811090; Sat, 27 Aug 2022 03:40:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661596811; cv=none; d=google.com; s=arc-20160816; b=I3ZPP+hmRDTYIY/tObZyaOnSs19QU0W81zm36uMSlqmOzfhn37VtRMClsOOSk2mTi4 9uhfV+UTt57RAguflL/E1PZCi14R7T6rxZYD4QpswJZBthiTAxTGICabqg2ItEcM58OX 9ZHd9lRXCXgGXmYJgjTt7niq1dgVqJvC+Dx4bn16XSgRlYnpfuI6gmZ6adA/OcoV3TRx 0sptc0xFeWJcgZJE8RRzlcaY+vOYMTFTq911P86FtKhmT8q8N77YCYFs0L524MNvK0Bj S4Jy8fKMDPYzABA89qZLZPV5Yu6YuSrQE8zKfnQUM+5glgyO+rQJcvBhsdPwU8rdr1eh DBdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=ygudW5h3+fGZn8qgaKuqS8DlGNtvOI0q05JNF5mAobg=; b=Xxk/zxxhAEihj67r5GHYk+QAQ9fvnNn8/ywRYuL8pLkg7OsWyvdSHA32fTRSnafe+Q 6ply0FmRi/oNOJ2c/0iRlspgLjimlv39zApgJStC1zV0A4yOPlgNYcCZg8hOss7N1elZ 5p/WTqWYUPEajC/xy+8Mav9aK5W80a1w01XgfIggvNiSN9dOpItoTE0ghVM/Cl0JkUTC dQw7rNTqkJr+cRB5iu/wePVwqAGgq6kH6c43+l9NXhgrEkzzjYEdJE4Hhx/nwR9VfG/u xeDLqm4RquIhOReHJiYjwFCtD1RwplaV2PHia7XQEq9/lFF/H/O11u/xXPXs8nKIcRov CFVg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 27-20020a17090a005b00b001f307a7871fsi3983612pjb.45.2022.08.27.03.39.55; Sat, 27 Aug 2022 03:40:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234499AbiH0KMJ (ORCPT + 99 others); Sat, 27 Aug 2022 06:12:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233533AbiH0KLs (ORCPT ); Sat, 27 Aug 2022 06:11:48 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93BA624F19 for ; Sat, 27 Aug 2022 03:11:44 -0700 (PDT) Received: from dggpemm500021.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MFC7r4rZFzkWKd; Sat, 27 Aug 2022 18:08:08 +0800 (CST) Received: from dggpemm100009.china.huawei.com (7.185.36.113) by dggpemm500021.china.huawei.com (7.185.36.109) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Sat, 27 Aug 2022 18:11:42 +0800 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.2375.24; Sat, 27 Aug 2022 18:11:42 +0800 From: Liu Shixin To: Seth Jennings , Dan Streetman , Vitaly Wool , Andrew Morton , Nathan Chancellor , "Christoph Hellwig" CC: , , Liu Shixin , Kefeng Wang Subject: [PATCH -next v3 4/5] mm/zswap: delay the initializaton of zswap until the first enablement Date: Sat, 27 Aug 2022 18:45:59 +0800 Message-ID: <20220827104600.1813214-5-liushixin2@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220827104600.1813214-1-liushixin2@huawei.com> References: <20220827104600.1813214-1-liushixin2@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm100009.china.huawei.com (7.185.36.113) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In the initialization of zswap, about 18MB memory will be allocated for zswap_pool in my machine. Since not all users use zswap, the memory may be wasted. Save the memory for these users by delaying the initialization of zswap to first enablement. Signed-off-by: Liu Shixin --- mm/zswap.c | 50 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 84e38300f571..4c476c463035 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 *, @@ -220,6 +222,8 @@ static atomic_t zswap_pools_count = ATOMIC_INIT(0); /* init state */ static int 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; @@ -273,13 +277,13 @@ static void zswap_update_total_size(void) **********************************/ static struct kmem_cache *zswap_entry_cache; -static int __init zswap_entry_cache_create(void) +static int zswap_entry_cache_create(void) { zswap_entry_cache = KMEM_CACHE(zswap_entry, 0); return zswap_entry_cache == NULL; } -static void __init zswap_entry_cache_destroy(void) +static void zswap_entry_cache_destroy(void) { kmem_cache_destroy(zswap_entry_cache); } @@ -664,7 +668,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; @@ -782,11 +786,17 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp, if (!strcmp(s, *(char **)kp->arg) && zswap_has_pool) return 0; - /* if this is load-time (pre-init) param setting, + /* + * if zswap has not been initialized, * don't create a pool; that's done during init. */ - if (zswap_init_state == ZSWAP_UNINIT) - return param_set_charp(s, kp); + mutex_lock(&zswap_init_lock); + if (zswap_init_state == ZSWAP_UNINIT) { + ret = param_set_charp(s, kp); + mutex_unlock(&zswap_init_lock); + return ret; + } + mutex_unlock(&zswap_init_lock); if (!type) { if (!zpool_has_pool(s)) { @@ -876,6 +886,14 @@ static int zswap_zpool_param_set(const char *val, static int zswap_enabled_param_set(const char *val, const struct kernel_param *kp) { + if (system_state == SYSTEM_RUNNING) { + mutex_lock(&zswap_init_lock); + if (zswap_setup()) { + mutex_unlock(&zswap_init_lock); + return -ENODEV; + } + mutex_unlock(&zswap_init_lock); + } if (zswap_init_state == ZSWAP_INIT_FAILED) { pr_err("can't enable, initialization failed\n"); return -ENODEV; @@ -1432,7 +1450,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; @@ -1463,7 +1481,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; } @@ -1472,11 +1490,14 @@ 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; + if (zswap_init_state != ZSWAP_UNINIT) + return 0; + if (zswap_entry_cache_create()) { pr_err("entry cache creation failed\n"); goto cache_fail; @@ -1534,8 +1555,17 @@ static int __init init_zswap(void) zswap_enabled = false; return -ENOMEM; } + +static int __init zswap_init(void) +{ + /* skip init if zswap is disabled when system startup */ + 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 "); -- 2.25.1