linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH -next v2 0/3] Delay the initializaton of zswap
@ 2022-08-26  7:34 Liu Shixin
  2022-08-26  7:34 ` [PATCH -next v2 1/3] mm/zswap: replace zswap_init_{started/failed} with zswap_init_state Liu Shixin
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Liu Shixin @ 2022-08-26  7:34 UTC (permalink / raw)
  To: Seth Jennings, Dan Streetman, Vitaly Wool, Andrew Morton
  Cc: linux-mm, linux-kernel, Liu Shixin, Kefeng Wang

In the initialization of zswap, about 18MB memory will be allocated for       
zswap_pool. 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.

v1->v2: Change init_zswap to zswap_init suggested by Andrew.

Liu Shixin (3):
  mm/zswap: replace zswap_init_{started/failed} with zswap_init_state
  mm/zswap: delay the initializaton of zswap until the first enablement
  mm/zswap: skip confusing print info

 mm/zswap.c | 77 ++++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 58 insertions(+), 19 deletions(-)

-- 
2.25.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH -next v2 1/3] mm/zswap: replace zswap_init_{started/failed} with zswap_init_state
  2022-08-26  7:34 [PATCH -next v2 0/3] Delay the initializaton of zswap Liu Shixin
@ 2022-08-26  7:34 ` Liu Shixin
  2022-08-26  7:34 ` [PATCH -next v2 2/3] mm/zswap: delay the initializaton of zswap until the first enablement Liu Shixin
  2022-08-26  7:34 ` [PATCH -next v2 3/3] mm/zswap: skip confusing print info Liu Shixin
  2 siblings, 0 replies; 4+ messages in thread
From: Liu Shixin @ 2022-08-26  7:34 UTC (permalink / raw)
  To: Seth Jennings, Dan Streetman, Vitaly Wool, Andrew Morton
  Cc: linux-mm, linux-kernel, Liu Shixin, Kefeng Wang

zswap_init_started indicates that the initialization is started. And
zswap_init_failed indicates that the initialization is failed. As we will
support to init zswap after system startup, it's necessary to add a state
to indicate the initialization is complete and succeed to avoid
concurrency issues. Since we don't care about the difference between
init started with init completion. We only need three states:
uninitialized, initial failed, initial succeed.

Signed-off-by: Liu Shixin <liushixin2@huawei.com>
---
 mm/zswap.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/mm/zswap.c b/mm/zswap.c
index 2d48fd59cc7a..84e38300f571 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -214,11 +214,12 @@ static DEFINE_SPINLOCK(zswap_pools_lock);
 /* pool counter to provide unique names to zpool */
 static atomic_t zswap_pools_count = ATOMIC_INIT(0);
 
-/* used by param callback function */
-static bool zswap_init_started;
+#define ZSWAP_UNINIT		0
+#define ZSWAP_INIT_SUCCEED	1
+#define ZSWAP_INIT_FAILED	2
 
-/* fatal error during init */
-static bool zswap_init_failed;
+/* init state */
+static int zswap_init_state;
 
 /* init completed, but couldn't create the initial pool */
 static bool zswap_has_pool;
@@ -772,7 +773,7 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp,
 	char *s = strstrip((char *)val);
 	int ret;
 
-	if (zswap_init_failed) {
+	if (zswap_init_state == ZSWAP_INIT_FAILED) {
 		pr_err("can't set param, initialization failed\n");
 		return -ENODEV;
 	}
@@ -784,7 +785,7 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp,
 	/* if this is load-time (pre-init) param setting,
 	 * don't create a pool; that's done during init.
 	 */
-	if (!zswap_init_started)
+	if (zswap_init_state == ZSWAP_UNINIT)
 		return param_set_charp(s, kp);
 
 	if (!type) {
@@ -875,11 +876,11 @@ static int zswap_zpool_param_set(const char *val,
 static int zswap_enabled_param_set(const char *val,
 				   const struct kernel_param *kp)
 {
-	if (zswap_init_failed) {
+	if (zswap_init_state == ZSWAP_INIT_FAILED) {
 		pr_err("can't enable, initialization failed\n");
 		return -ENODEV;
 	}
-	if (!zswap_has_pool && zswap_init_started) {
+	if (!zswap_has_pool && zswap_init_state == ZSWAP_INIT_SUCCEED) {
 		pr_err("can't enable, no pool configured\n");
 		return -ENODEV;
 	}
@@ -1476,8 +1477,6 @@ static int __init init_zswap(void)
 	struct zswap_pool *pool;
 	int ret;
 
-	zswap_init_started = true;
-
 	if (zswap_entry_cache_create()) {
 		pr_err("entry cache creation failed\n");
 		goto cache_fail;
@@ -1517,6 +1516,7 @@ static int __init init_zswap(void)
 		goto destroy_wq;
 	if (zswap_debugfs_init())
 		pr_warn("debugfs initialization failed\n");
+	zswap_init_state = ZSWAP_INIT_SUCCEED;
 	return 0;
 
 destroy_wq:
@@ -1530,7 +1530,7 @@ static int __init init_zswap(void)
 	zswap_entry_cache_destroy();
 cache_fail:
 	/* if built-in, we aren't unloaded on failure; don't allow use */
-	zswap_init_failed = true;
+	zswap_init_state = ZSWAP_INIT_FAILED;
 	zswap_enabled = false;
 	return -ENOMEM;
 }
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH -next v2 2/3] mm/zswap: delay the initializaton of zswap until the first enablement
  2022-08-26  7:34 [PATCH -next v2 0/3] Delay the initializaton of zswap Liu Shixin
  2022-08-26  7:34 ` [PATCH -next v2 1/3] mm/zswap: replace zswap_init_{started/failed} with zswap_init_state Liu Shixin
@ 2022-08-26  7:34 ` Liu Shixin
  2022-08-26  7:34 ` [PATCH -next v2 3/3] mm/zswap: skip confusing print info Liu Shixin
  2 siblings, 0 replies; 4+ messages in thread
From: Liu Shixin @ 2022-08-26  7:34 UTC (permalink / raw)
  To: Seth Jennings, Dan Streetman, Vitaly Wool, Andrew Morton
  Cc: linux-mm, linux-kernel, Liu Shixin, Kefeng Wang

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 <liushixin2@huawei.com>
---
 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 <sjennings@variantweb.net>");
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH -next v2 3/3] mm/zswap: skip confusing print info
  2022-08-26  7:34 [PATCH -next v2 0/3] Delay the initializaton of zswap Liu Shixin
  2022-08-26  7:34 ` [PATCH -next v2 1/3] mm/zswap: replace zswap_init_{started/failed} with zswap_init_state Liu Shixin
  2022-08-26  7:34 ` [PATCH -next v2 2/3] mm/zswap: delay the initializaton of zswap until the first enablement Liu Shixin
@ 2022-08-26  7:34 ` Liu Shixin
  2 siblings, 0 replies; 4+ messages in thread
From: Liu Shixin @ 2022-08-26  7:34 UTC (permalink / raw)
  To: Seth Jennings, Dan Streetman, Vitaly Wool, Andrew Morton
  Cc: linux-mm, linux-kernel, Liu Shixin, Kefeng Wang

It's confusing when we disable zswap while zswap is init failed or has no
pool. If no change required, just return directly.

Signed-off-by: Liu Shixin <liushixin2@huawei.com>
---
 mm/zswap.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/mm/zswap.c b/mm/zswap.c
index 4c476c463035..ef7463550e49 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -886,6 +886,15 @@ static int zswap_zpool_param_set(const char *val,
 static int zswap_enabled_param_set(const char *val,
 				   const struct kernel_param *kp)
 {
+	bool res;
+
+	if (kstrtobool(val, &res))
+		return -EINVAL;
+
+	/* no change required */
+	if (res == *(bool *)kp->arg)
+		return 0;
+
 	if (system_state == SYSTEM_RUNNING) {
 		mutex_lock(&zswap_init_lock);
 		if (zswap_setup()) {
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-08-26  7:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-26  7:34 [PATCH -next v2 0/3] Delay the initializaton of zswap Liu Shixin
2022-08-26  7:34 ` [PATCH -next v2 1/3] mm/zswap: replace zswap_init_{started/failed} with zswap_init_state Liu Shixin
2022-08-26  7:34 ` [PATCH -next v2 2/3] mm/zswap: delay the initializaton of zswap until the first enablement Liu Shixin
2022-08-26  7:34 ` [PATCH -next v2 3/3] mm/zswap: skip confusing print info Liu Shixin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).