linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH -next v3 0/5] Delay the initializaton of zswap
@ 2022-08-27 10:45 Liu Shixin
  2022-08-27 10:45 ` [PATCH -next v3 1/5] frontswap: skip frontswap_ops init if zswap init failed Liu Shixin
                   ` (5 more replies)
  0 siblings, 6 replies; 14+ messages in thread
From: Liu Shixin @ 2022-08-27 10:45 UTC (permalink / raw)
  To: Seth Jennings, Dan Streetman, Vitaly Wool, Andrew Morton,
	Nathan Chancellor, Christoph Hellwig
  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.                                                             
                                                                              
v2->v3: Fix frontswap_ops NULL reported by Nathan and add init for online
swap device in backend register.
v1->v2: Change init_zswap to zswap_init suggested by Andrew.

Liu Shixin (5):
  frontswap: skip frontswap_ops init if zswap init failed.
  frontswap: invoke ops->init for online swap device in
    frontswap_register_ops
  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

 include/linux/swapfile.h |  2 ++
 mm/frontswap.c           | 50 +++++++++++++++++++++++++-
 mm/swapfile.c            |  4 +--
 mm/zswap.c               | 77 ++++++++++++++++++++++++++++++----------
 4 files changed, 111 insertions(+), 22 deletions(-)

-- 
2.25.1


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

* [PATCH -next v3 1/5] frontswap: skip frontswap_ops init if zswap init failed.
  2022-08-27 10:45 [PATCH -next v3 0/5] Delay the initializaton of zswap Liu Shixin
@ 2022-08-27 10:45 ` Liu Shixin
  2022-08-28 20:44   ` Vitaly Wool
  2022-08-27 10:45 ` [PATCH -next v3 2/5] frontswap: invoke ops->init for online swap device in frontswap_register_ops Liu Shixin
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 14+ messages in thread
From: Liu Shixin @ 2022-08-27 10:45 UTC (permalink / raw)
  To: Seth Jennings, Dan Streetman, Vitaly Wool, Andrew Morton,
	Nathan Chancellor, Christoph Hellwig
  Cc: linux-mm, linux-kernel, Liu Shixin, Kefeng Wang

If zswap initial failed or has not been initial, frontswap_ops will be
NULL. In such situation, swap device would enable failed with following
stack trace:

  Unable to handle kernel access to user memory outside uaccess routines at virtual address 0000000000000000
  Mem abort info:
    ESR = 0x0000000096000004
    EC = 0x25: DABT (current EL), IL = 32 bits
    SET = 0, FnV = 0
    EA = 0, S1PTW = 0
    FSC = 0x04: level 0 translation fault
  Data abort info:
    ISV = 0, ISS = 0x00000004
    CM = 0, WnR = 0
  user pgtable: 4k pages, 48-bit VAs, pgdp=00000020a4fab000
  [0000000000000000] pgd=0000000000000000, p4d=0000000000000000
  Internal error: Oops: 96000004 [#1] SMP
  Modules linked in: zram fsl_dpaa2_eth pcs_lynx phylink ahci_qoriq crct10dif_ce ghash_ce sbsa_gwdt fsl_mc_dpio nvme lm90 nvme_core at803x xhci_plat_hcd rtc_fsl_ftm_alarm xgmac_mdio ahci_platform i2c_imx ip6_tables ip_tables fuse
  Unloaded tainted modules: cppc_cpufreq():1
  CPU: 10 PID: 761 Comm: swapon Not tainted 6.0.0-rc2-00454-g22100432cf14 #1
  Hardware name: SolidRun Ltd. SolidRun CEX7 Platform, BIOS EDK II Jun 21 2022
  pstate: 00400005 (nzcv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
  pc : frontswap_init+0x38/0x60
  lr : __do_sys_swapon+0x8a8/0x9f4
  sp : ffff80000969bcf0
  x29: ffff80000969bcf0 x28: ffff37bee0d8fc00 x27: ffff80000a7f5000
  x26: fffffcdefb971e80 x25: ffffaba797453b90 x24: 0000000000000064
  x23: ffff37c1f209d1a8 x22: ffff37bee880e000 x21: ffffaba797748560
  x20: ffff37bee0d8fce4 x19: ffffaba797748488 x18: 0000000000000014
  x17: 0000000030ec029a x16: ffffaba795a479b0 x15: 0000000000000000
  x14: 0000000000000000 x13: 0000000000000030 x12: 0000000000000001
  x11: ffff37c63c0aba18 x10: 0000000000000000 x9 : ffffaba7956b8c88
  x8 : ffff80000969bcd0 x7 : 0000000000000000 x6 : 0000000000000000
  x5 : 0000000000000001 x4 : 0000000000000000 x3 : ffffaba79730f000
  x2 : ffff37bee0d8fc00 x1 : 0000000000000000 x0 : 0000000000000000
  Call trace:
  frontswap_init+0x38/0x60
  __do_sys_swapon+0x8a8/0x9f4
  __arm64_sys_swapon+0x28/0x3c
  invoke_syscall+0x78/0x100
  el0_svc_common.constprop.0+0xd4/0xf4
  do_el0_svc+0x38/0x4c
  el0_svc+0x34/0x10c
  el0t_64_sync_handler+0x11c/0x150
  el0t_64_sync+0x190/0x194
  Code: d000e283 910003fd f9006c41 f946d461 (f9400021)
  ---[ end trace 0000000000000000 ]---

Reported-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Liu Shixin <liushixin2@huawei.com>
---
 mm/frontswap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mm/frontswap.c b/mm/frontswap.c
index 1a97610308cb..620f95af81dd 100644
--- a/mm/frontswap.c
+++ b/mm/frontswap.c
@@ -125,7 +125,8 @@ void frontswap_init(unsigned type, unsigned long *map)
 	 * p->frontswap set to something valid to work properly.
 	 */
 	frontswap_map_set(sis, map);
-	frontswap_ops->init(type);
+	if (frontswap_ops)
+		frontswap_ops->init(type);
 }
 
 static bool __frontswap_test(struct swap_info_struct *sis,
-- 
2.25.1


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

* [PATCH -next v3 2/5] frontswap: invoke ops->init for online swap device in frontswap_register_ops
  2022-08-27 10:45 [PATCH -next v3 0/5] Delay the initializaton of zswap Liu Shixin
  2022-08-27 10:45 ` [PATCH -next v3 1/5] frontswap: skip frontswap_ops init if zswap init failed Liu Shixin
@ 2022-08-27 10:45 ` Liu Shixin
  2022-08-28 20:47   ` Vitaly Wool
  2022-08-27 10:45 ` [PATCH -next v3 3/5] mm/zswap: replace zswap_init_{started/failed} with zswap_init_state Liu Shixin
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 14+ messages in thread
From: Liu Shixin @ 2022-08-27 10:45 UTC (permalink / raw)
  To: Seth Jennings, Dan Streetman, Vitaly Wool, Andrew Morton,
	Nathan Chancellor, Christoph Hellwig
  Cc: linux-mm, linux-kernel, Liu Shixin, Kefeng Wang

Since we are supported to delay zswap initializaton, we need to invoke
ops->init for the swap device which is already online when register
backend.

This patch is a revert of f328c1d16e4c ("frontswap: simplify frontswap_register_ops")
and 633423a09cb5 ("mm: mark swap_lock and swap_active_head static")

Signed-off-by: Liu Shixin <liushixin2@huawei.com>
---
 include/linux/swapfile.h |  2 ++
 mm/frontswap.c           | 47 ++++++++++++++++++++++++++++++++++++++++
 mm/swapfile.c            |  4 ++--
 3 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/include/linux/swapfile.h b/include/linux/swapfile.h
index 2fbcc9afd814..75fc069594a5 100644
--- a/include/linux/swapfile.h
+++ b/include/linux/swapfile.h
@@ -6,6 +6,8 @@
  * these were static in swapfile.c but frontswap.c needs them and we don't
  * want to expose them to the dozens of source files that include swap.h
  */
+extern spinlock_t swap_lock;
+extern struct plist_head swap_active_head;
 extern struct swap_info_struct *swap_info[];
 extern unsigned long generic_max_swapfile_size(void);
 /* Maximum swapfile size supported for the arch (not inclusive). */
diff --git a/mm/frontswap.c b/mm/frontswap.c
index 620f95af81dd..449e6f499b88 100644
--- a/mm/frontswap.c
+++ b/mm/frontswap.c
@@ -96,11 +96,58 @@ static inline void inc_frontswap_invalidates(void) { }
  */
 int frontswap_register_ops(const struct frontswap_ops *ops)
 {
+	DECLARE_BITMAP(a, MAX_SWAPFILES);
+	DECLARE_BITMAP(b, MAX_SWAPFILES);
+	struct swap_info_struct *si;
+	unsigned int i;
+
 	if (frontswap_ops)
 		return -EINVAL;
 
+	bitmap_zero(a, MAX_SWAPFILES);
+	bitmap_zero(b, MAX_SWAPFILES);
+
+	spin_lock(&swap_lock);
+	plist_for_each_entry(si, &swap_active_head, list) {
+		if (!WARN_ON(!si->frontswap_map))
+			__set_bit(si->type, a);
+	}
+	spin_unlock(&swap_lock);
+
+	/* the new ops needs to know the currently active swap devices */
+	for_each_set_bit(i, a, MAX_SWAPFILES) {
+		pr_err("init frontswap_ops\n");
+		ops->init(i);
+	}
+
 	frontswap_ops = ops;
 	static_branch_inc(&frontswap_enabled_key);
+
+	spin_lock(&swap_lock);
+	plist_for_each_entry(si, &swap_active_head, list) {
+		if (si->frontswap_map)
+			__set_bit(si->type, b);
+	}
+	spin_unlock(&swap_lock);
+
+	/*
+	 * On the very unlikely chance that a swap device was added or
+	 * removed between setting the "a" list bits and the ops init
+	 * calls, we re-check and do init or invalidate for any changed
+	 * bits.
+	 */
+	if (unlikely(!bitmap_equal(a, b, MAX_SWAPFILES))) {
+		for (i = 0; i < MAX_SWAPFILES; i++) {
+			if (!test_bit(i, a) && test_bit(i, b)) {
+				pr_err("init frontswap_ops re\n");
+				ops->init(i);
+			} else if (test_bit(i, a) && !test_bit(i, b)) {
+				pr_err("inval frontswap_ops re\n");
+				ops->invalidate_area(i);
+			}
+		}
+	}
+
 	return 0;
 }
 
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 469d9af86be2..d383b282f269 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -51,7 +51,7 @@ static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
 				 unsigned char);
 static void free_swap_count_continuations(struct swap_info_struct *);
 
-static DEFINE_SPINLOCK(swap_lock);
+DEFINE_SPINLOCK(swap_lock);
 static unsigned int nr_swapfiles;
 atomic_long_t nr_swap_pages;
 /*
@@ -77,7 +77,7 @@ static const char Unused_offset[] = "Unused swap offset entry ";
  * all active swap_info_structs
  * protected with swap_lock, and ordered by priority.
  */
-static PLIST_HEAD(swap_active_head);
+PLIST_HEAD(swap_active_head);
 
 /*
  * all available (active, not full) swap_info_structs
-- 
2.25.1


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

* [PATCH -next v3 3/5] mm/zswap: replace zswap_init_{started/failed} with zswap_init_state
  2022-08-27 10:45 [PATCH -next v3 0/5] Delay the initializaton of zswap Liu Shixin
  2022-08-27 10:45 ` [PATCH -next v3 1/5] frontswap: skip frontswap_ops init if zswap init failed Liu Shixin
  2022-08-27 10:45 ` [PATCH -next v3 2/5] frontswap: invoke ops->init for online swap device in frontswap_register_ops Liu Shixin
@ 2022-08-27 10:45 ` Liu Shixin
  2022-08-28 20:46   ` Vitaly Wool
  2022-08-27 10:45 ` [PATCH -next v3 4/5] mm/zswap: delay the initializaton of zswap until the first enablement Liu Shixin
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 14+ messages in thread
From: Liu Shixin @ 2022-08-27 10:45 UTC (permalink / raw)
  To: Seth Jennings, Dan Streetman, Vitaly Wool, Andrew Morton,
	Nathan Chancellor, Christoph Hellwig
  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] 14+ messages in thread

* [PATCH -next v3 4/5] mm/zswap: delay the initializaton of zswap until the first enablement
  2022-08-27 10:45 [PATCH -next v3 0/5] Delay the initializaton of zswap Liu Shixin
                   ` (2 preceding siblings ...)
  2022-08-27 10:45 ` [PATCH -next v3 3/5] mm/zswap: replace zswap_init_{started/failed} with zswap_init_state Liu Shixin
@ 2022-08-27 10:45 ` Liu Shixin
  2022-08-28 20:45   ` Vitaly Wool
  2022-08-27 10:46 ` [PATCH -next v3 5/5] mm/zswap: skip confusing print info Liu Shixin
  2022-08-27 22:13 ` [PATCH -next v3 0/5] Delay the initializaton of zswap Andrew Morton
  5 siblings, 1 reply; 14+ messages in thread
From: Liu Shixin @ 2022-08-27 10:45 UTC (permalink / raw)
  To: Seth Jennings, Dan Streetman, Vitaly Wool, Andrew Morton,
	Nathan Chancellor, Christoph Hellwig
  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] 14+ messages in thread

* [PATCH -next v3 5/5] mm/zswap: skip confusing print info
  2022-08-27 10:45 [PATCH -next v3 0/5] Delay the initializaton of zswap Liu Shixin
                   ` (3 preceding siblings ...)
  2022-08-27 10:45 ` [PATCH -next v3 4/5] mm/zswap: delay the initializaton of zswap until the first enablement Liu Shixin
@ 2022-08-27 10:46 ` Liu Shixin
  2022-08-27 22:13 ` [PATCH -next v3 0/5] Delay the initializaton of zswap Andrew Morton
  5 siblings, 0 replies; 14+ messages in thread
From: Liu Shixin @ 2022-08-27 10:46 UTC (permalink / raw)
  To: Seth Jennings, Dan Streetman, Vitaly Wool, Andrew Morton,
	Nathan Chancellor, Christoph Hellwig
  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] 14+ messages in thread

* Re: [PATCH -next v3 0/5] Delay the initializaton of zswap
  2022-08-27 10:45 [PATCH -next v3 0/5] Delay the initializaton of zswap Liu Shixin
                   ` (4 preceding siblings ...)
  2022-08-27 10:46 ` [PATCH -next v3 5/5] mm/zswap: skip confusing print info Liu Shixin
@ 2022-08-27 22:13 ` Andrew Morton
  5 siblings, 0 replies; 14+ messages in thread
From: Andrew Morton @ 2022-08-27 22:13 UTC (permalink / raw)
  To: Liu Shixin
  Cc: Seth Jennings, Dan Streetman, Vitaly Wool, Nathan Chancellor,
	Christoph Hellwig, linux-mm, linux-kernel, Kefeng Wang,
	Konrad Rzeszutek Wilk

On Sat, 27 Aug 2022 18:45:55 +0800 Liu Shixin <liushixin2@huawei.com> wrote:

> 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.                                                             
>                                                                               
> v2->v3: Fix frontswap_ops NULL reported by Nathan and add init for online
> swap device in backend register.
> v1->v2: Change init_zswap to zswap_init suggested by Andrew.

Thanks.  Konrad, could you please take a look for the frontswap
changes?


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

* Re: [PATCH -next v3 1/5] frontswap: skip frontswap_ops init if zswap init failed.
  2022-08-27 10:45 ` [PATCH -next v3 1/5] frontswap: skip frontswap_ops init if zswap init failed Liu Shixin
@ 2022-08-28 20:44   ` Vitaly Wool
  2022-08-29  2:10     ` Liu Shixin
  0 siblings, 1 reply; 14+ messages in thread
From: Vitaly Wool @ 2022-08-28 20:44 UTC (permalink / raw)
  To: Liu Shixin
  Cc: Seth Jennings, Dan Streetman, Andrew Morton, Nathan Chancellor,
	Christoph Hellwig, linux-mm, linux-kernel, Kefeng Wang

On Sat, Aug 27, 2022 at 12:12 PM Liu Shixin <liushixin2@huawei.com> wrote:
>
> If zswap initial failed or has not been initial, frontswap_ops will be
> NULL. In such situation, swap device would enable failed with following
> stack trace:
>
>   Unable to handle kernel access to user memory outside uaccess routines at virtual address 0000000000000000
>   Mem abort info:
>     ESR = 0x0000000096000004
>     EC = 0x25: DABT (current EL), IL = 32 bits
>     SET = 0, FnV = 0
>     EA = 0, S1PTW = 0
>     FSC = 0x04: level 0 translation fault
>   Data abort info:
>     ISV = 0, ISS = 0x00000004
>     CM = 0, WnR = 0
>   user pgtable: 4k pages, 48-bit VAs, pgdp=00000020a4fab000
>   [0000000000000000] pgd=0000000000000000, p4d=0000000000000000
>   Internal error: Oops: 96000004 [#1] SMP
>   Modules linked in: zram fsl_dpaa2_eth pcs_lynx phylink ahci_qoriq crct10dif_ce ghash_ce sbsa_gwdt fsl_mc_dpio nvme lm90 nvme_core at803x xhci_plat_hcd rtc_fsl_ftm_alarm xgmac_mdio ahci_platform i2c_imx ip6_tables ip_tables fuse
>   Unloaded tainted modules: cppc_cpufreq():1
>   CPU: 10 PID: 761 Comm: swapon Not tainted 6.0.0-rc2-00454-g22100432cf14 #1
>   Hardware name: SolidRun Ltd. SolidRun CEX7 Platform, BIOS EDK II Jun 21 2022
>   pstate: 00400005 (nzcv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
>   pc : frontswap_init+0x38/0x60
>   lr : __do_sys_swapon+0x8a8/0x9f4
>   sp : ffff80000969bcf0
>   x29: ffff80000969bcf0 x28: ffff37bee0d8fc00 x27: ffff80000a7f5000
>   x26: fffffcdefb971e80 x25: ffffaba797453b90 x24: 0000000000000064
>   x23: ffff37c1f209d1a8 x22: ffff37bee880e000 x21: ffffaba797748560
>   x20: ffff37bee0d8fce4 x19: ffffaba797748488 x18: 0000000000000014
>   x17: 0000000030ec029a x16: ffffaba795a479b0 x15: 0000000000000000
>   x14: 0000000000000000 x13: 0000000000000030 x12: 0000000000000001
>   x11: ffff37c63c0aba18 x10: 0000000000000000 x9 : ffffaba7956b8c88
>   x8 : ffff80000969bcd0 x7 : 0000000000000000 x6 : 0000000000000000
>   x5 : 0000000000000001 x4 : 0000000000000000 x3 : ffffaba79730f000
>   x2 : ffff37bee0d8fc00 x1 : 0000000000000000 x0 : 0000000000000000
>   Call trace:
>   frontswap_init+0x38/0x60
>   __do_sys_swapon+0x8a8/0x9f4
>   __arm64_sys_swapon+0x28/0x3c
>   invoke_syscall+0x78/0x100
>   el0_svc_common.constprop.0+0xd4/0xf4
>   do_el0_svc+0x38/0x4c
>   el0_svc+0x34/0x10c
>   el0t_64_sync_handler+0x11c/0x150
>   el0t_64_sync+0x190/0x194
>   Code: d000e283 910003fd f9006c41 f946d461 (f9400021)
>   ---[ end trace 0000000000000000 ]---
>

Well, this issue you are seeing is in fact introduced by the following patch:

author Christoph Hellwig <hch@lst.de> 2022-01-21 22:15:10 -0800
frontswap: remove support for multiple ops

So I would rather see that one reverted and fixed.

Thanks,
Vitaly

> Reported-by: Nathan Chancellor <nathan@kernel.org>
> Signed-off-by: Liu Shixin <liushixin2@huawei.com>
> ---
>  mm/frontswap.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/mm/frontswap.c b/mm/frontswap.c
> index 1a97610308cb..620f95af81dd 100644
> --- a/mm/frontswap.c
> +++ b/mm/frontswap.c
> @@ -125,7 +125,8 @@ void frontswap_init(unsigned type, unsigned long *map)
>          * p->frontswap set to something valid to work properly.
>          */
>         frontswap_map_set(sis, map);
> -       frontswap_ops->init(type);
> +       if (frontswap_ops)
> +               frontswap_ops->init(type);
>  }
>
>  static bool __frontswap_test(struct swap_info_struct *sis,
> --
> 2.25.1
>

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

* Re: [PATCH -next v3 4/5] mm/zswap: delay the initializaton of zswap until the first enablement
  2022-08-27 10:45 ` [PATCH -next v3 4/5] mm/zswap: delay the initializaton of zswap until the first enablement Liu Shixin
@ 2022-08-28 20:45   ` Vitaly Wool
  0 siblings, 0 replies; 14+ messages in thread
From: Vitaly Wool @ 2022-08-28 20:45 UTC (permalink / raw)
  To: Liu Shixin
  Cc: Seth Jennings, Dan Streetman, Andrew Morton, Nathan Chancellor,
	Christoph Hellwig, linux-mm, linux-kernel, Kefeng Wang

On Sat, Aug 27, 2022 at 12:12 PM Liu Shixin <liushixin2@huawei.com> wrote:
>
> 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>

Reviewed-by: Vitaly Wool <vitaly.wool@konsulko.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	[flat|nested] 14+ messages in thread

* Re: [PATCH -next v3 3/5] mm/zswap: replace zswap_init_{started/failed} with zswap_init_state
  2022-08-27 10:45 ` [PATCH -next v3 3/5] mm/zswap: replace zswap_init_{started/failed} with zswap_init_state Liu Shixin
@ 2022-08-28 20:46   ` Vitaly Wool
  0 siblings, 0 replies; 14+ messages in thread
From: Vitaly Wool @ 2022-08-28 20:46 UTC (permalink / raw)
  To: Liu Shixin
  Cc: Seth Jennings, Dan Streetman, Andrew Morton, Nathan Chancellor,
	Christoph Hellwig, linux-mm, linux-kernel, Kefeng Wang

On Sat, Aug 27, 2022 at 12:12 PM Liu Shixin <liushixin2@huawei.com> wrote:
>
> 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>

Reviewed-by: Vitaly Wool <vitaly.wool@konulko.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	[flat|nested] 14+ messages in thread

* Re: [PATCH -next v3 2/5] frontswap: invoke ops->init for online swap device in frontswap_register_ops
  2022-08-27 10:45 ` [PATCH -next v3 2/5] frontswap: invoke ops->init for online swap device in frontswap_register_ops Liu Shixin
@ 2022-08-28 20:47   ` Vitaly Wool
  2022-08-29  2:12     ` Liu Shixin
  0 siblings, 1 reply; 14+ messages in thread
From: Vitaly Wool @ 2022-08-28 20:47 UTC (permalink / raw)
  To: Liu Shixin
  Cc: Seth Jennings, Dan Streetman, Andrew Morton, Nathan Chancellor,
	Christoph Hellwig, linux-mm, linux-kernel, Kefeng Wang

On Sat, Aug 27, 2022 at 12:12 PM Liu Shixin <liushixin2@huawei.com> wrote:
>
> Since we are supported to delay zswap initializaton, we need to invoke
> ops->init for the swap device which is already online when register
> backend.
>
> This patch is a revert of f328c1d16e4c ("frontswap: simplify frontswap_register_ops")
> and 633423a09cb5 ("mm: mark swap_lock and swap_active_head static")
>
> Signed-off-by: Liu Shixin <liushixin2@huawei.com>

Sorry, is this the revert of 2 patches at the same time? I would
rather not do it like that.

Thanks,
Vitaly

> ---
>  include/linux/swapfile.h |  2 ++
>  mm/frontswap.c           | 47 ++++++++++++++++++++++++++++++++++++++++
>  mm/swapfile.c            |  4 ++--
>  3 files changed, 51 insertions(+), 2 deletions(-)
>
> diff --git a/include/linux/swapfile.h b/include/linux/swapfile.h
> index 2fbcc9afd814..75fc069594a5 100644
> --- a/include/linux/swapfile.h
> +++ b/include/linux/swapfile.h
> @@ -6,6 +6,8 @@
>   * these were static in swapfile.c but frontswap.c needs them and we don't
>   * want to expose them to the dozens of source files that include swap.h
>   */
> +extern spinlock_t swap_lock;
> +extern struct plist_head swap_active_head;
>  extern struct swap_info_struct *swap_info[];
>  extern unsigned long generic_max_swapfile_size(void);
>  /* Maximum swapfile size supported for the arch (not inclusive). */
> diff --git a/mm/frontswap.c b/mm/frontswap.c
> index 620f95af81dd..449e6f499b88 100644
> --- a/mm/frontswap.c
> +++ b/mm/frontswap.c
> @@ -96,11 +96,58 @@ static inline void inc_frontswap_invalidates(void) { }
>   */
>  int frontswap_register_ops(const struct frontswap_ops *ops)
>  {
> +       DECLARE_BITMAP(a, MAX_SWAPFILES);
> +       DECLARE_BITMAP(b, MAX_SWAPFILES);
> +       struct swap_info_struct *si;
> +       unsigned int i;
> +
>         if (frontswap_ops)
>                 return -EINVAL;
>
> +       bitmap_zero(a, MAX_SWAPFILES);
> +       bitmap_zero(b, MAX_SWAPFILES);
> +
> +       spin_lock(&swap_lock);
> +       plist_for_each_entry(si, &swap_active_head, list) {
> +               if (!WARN_ON(!si->frontswap_map))
> +                       __set_bit(si->type, a);
> +       }
> +       spin_unlock(&swap_lock);
> +
> +       /* the new ops needs to know the currently active swap devices */
> +       for_each_set_bit(i, a, MAX_SWAPFILES) {
> +               pr_err("init frontswap_ops\n");
> +               ops->init(i);
> +       }
> +
>         frontswap_ops = ops;
>         static_branch_inc(&frontswap_enabled_key);
> +
> +       spin_lock(&swap_lock);
> +       plist_for_each_entry(si, &swap_active_head, list) {
> +               if (si->frontswap_map)
> +                       __set_bit(si->type, b);
> +       }
> +       spin_unlock(&swap_lock);
> +
> +       /*
> +        * On the very unlikely chance that a swap device was added or
> +        * removed between setting the "a" list bits and the ops init
> +        * calls, we re-check and do init or invalidate for any changed
> +        * bits.
> +        */
> +       if (unlikely(!bitmap_equal(a, b, MAX_SWAPFILES))) {
> +               for (i = 0; i < MAX_SWAPFILES; i++) {
> +                       if (!test_bit(i, a) && test_bit(i, b)) {
> +                               pr_err("init frontswap_ops re\n");
> +                               ops->init(i);
> +                       } else if (test_bit(i, a) && !test_bit(i, b)) {
> +                               pr_err("inval frontswap_ops re\n");
> +                               ops->invalidate_area(i);
> +                       }
> +               }
> +       }
> +
>         return 0;
>  }
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 469d9af86be2..d383b282f269 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -51,7 +51,7 @@ static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
>                                  unsigned char);
>  static void free_swap_count_continuations(struct swap_info_struct *);
>
> -static DEFINE_SPINLOCK(swap_lock);
> +DEFINE_SPINLOCK(swap_lock);
>  static unsigned int nr_swapfiles;
>  atomic_long_t nr_swap_pages;
>  /*
> @@ -77,7 +77,7 @@ static const char Unused_offset[] = "Unused swap offset entry ";
>   * all active swap_info_structs
>   * protected with swap_lock, and ordered by priority.
>   */
> -static PLIST_HEAD(swap_active_head);
> +PLIST_HEAD(swap_active_head);
>
>  /*
>   * all available (active, not full) swap_info_structs
> --
> 2.25.1
>

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

* Re: [PATCH -next v3 1/5] frontswap: skip frontswap_ops init if zswap init failed.
  2022-08-28 20:44   ` Vitaly Wool
@ 2022-08-29  2:10     ` Liu Shixin
  2022-08-29  6:11       ` Vitaly Wool
  0 siblings, 1 reply; 14+ messages in thread
From: Liu Shixin @ 2022-08-29  2:10 UTC (permalink / raw)
  To: Vitaly Wool
  Cc: Seth Jennings, Dan Streetman, Andrew Morton, Nathan Chancellor,
	Christoph Hellwig, linux-mm, linux-kernel, Kefeng Wang

On 2022/8/29 4:44, Vitaly Wool wrote:
> On Sat, Aug 27, 2022 at 12:12 PM Liu Shixin <liushixin2@huawei.com> wrote:
>> If zswap initial failed or has not been initial, frontswap_ops will be
>> NULL. In such situation, swap device would enable failed with following
>> stack trace:
>>
>>   Unable to handle kernel access to user memory outside uaccess routines at virtual address 0000000000000000
>>   Mem abort info:
>>     ESR = 0x0000000096000004
>>     EC = 0x25: DABT (current EL), IL = 32 bits
>>     SET = 0, FnV = 0
>>     EA = 0, S1PTW = 0
>>     FSC = 0x04: level 0 translation fault
>>   Data abort info:
>>     ISV = 0, ISS = 0x00000004
>>     CM = 0, WnR = 0
>>   user pgtable: 4k pages, 48-bit VAs, pgdp=00000020a4fab000
>>   [0000000000000000] pgd=0000000000000000, p4d=0000000000000000
>>   Internal error: Oops: 96000004 [#1] SMP
>>   Modules linked in: zram fsl_dpaa2_eth pcs_lynx phylink ahci_qoriq crct10dif_ce ghash_ce sbsa_gwdt fsl_mc_dpio nvme lm90 nvme_core at803x xhci_plat_hcd rtc_fsl_ftm_alarm xgmac_mdio ahci_platform i2c_imx ip6_tables ip_tables fuse
>>   Unloaded tainted modules: cppc_cpufreq():1
>>   CPU: 10 PID: 761 Comm: swapon Not tainted 6.0.0-rc2-00454-g22100432cf14 #1
>>   Hardware name: SolidRun Ltd. SolidRun CEX7 Platform, BIOS EDK II Jun 21 2022
>>   pstate: 00400005 (nzcv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
>>   pc : frontswap_init+0x38/0x60
>>   lr : __do_sys_swapon+0x8a8/0x9f4
>>   sp : ffff80000969bcf0
>>   x29: ffff80000969bcf0 x28: ffff37bee0d8fc00 x27: ffff80000a7f5000
>>   x26: fffffcdefb971e80 x25: ffffaba797453b90 x24: 0000000000000064
>>   x23: ffff37c1f209d1a8 x22: ffff37bee880e000 x21: ffffaba797748560
>>   x20: ffff37bee0d8fce4 x19: ffffaba797748488 x18: 0000000000000014
>>   x17: 0000000030ec029a x16: ffffaba795a479b0 x15: 0000000000000000
>>   x14: 0000000000000000 x13: 0000000000000030 x12: 0000000000000001
>>   x11: ffff37c63c0aba18 x10: 0000000000000000 x9 : ffffaba7956b8c88
>>   x8 : ffff80000969bcd0 x7 : 0000000000000000 x6 : 0000000000000000
>>   x5 : 0000000000000001 x4 : 0000000000000000 x3 : ffffaba79730f000
>>   x2 : ffff37bee0d8fc00 x1 : 0000000000000000 x0 : 0000000000000000
>>   Call trace:
>>   frontswap_init+0x38/0x60
>>   __do_sys_swapon+0x8a8/0x9f4
>>   __arm64_sys_swapon+0x28/0x3c
>>   invoke_syscall+0x78/0x100
>>   el0_svc_common.constprop.0+0xd4/0xf4
>>   do_el0_svc+0x38/0x4c
>>   el0_svc+0x34/0x10c
>>   el0t_64_sync_handler+0x11c/0x150
>>   el0t_64_sync+0x190/0x194
>>   Code: d000e283 910003fd f9006c41 f946d461 (f9400021)
>>   ---[ end trace 0000000000000000 ]---
>>
> Well, this issue you are seeing is in fact introduced by the following patch:
>
> author Christoph Hellwig <hch@lst.de> 2022-01-21 22:15:10 -0800
> frontswap: remove support for multiple ops
>
> So I would rather see that one reverted and fixed.
>
> Thanks,
> Vitaly
It is surely introduced by the previous patch ,but is it need to revert that patch? Do we have
any plans to add new backend in the future?

Thanks,
>
>> Reported-by: Nathan Chancellor <nathan@kernel.org>
>> Signed-off-by: Liu Shixin <liushixin2@huawei.com>
>> ---
>>  mm/frontswap.c | 3 ++-
>>  1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/mm/frontswap.c b/mm/frontswap.c
>> index 1a97610308cb..620f95af81dd 100644
>> --- a/mm/frontswap.c
>> +++ b/mm/frontswap.c
>> @@ -125,7 +125,8 @@ void frontswap_init(unsigned type, unsigned long *map)
>>          * p->frontswap set to something valid to work properly.
>>          */
>>         frontswap_map_set(sis, map);
>> -       frontswap_ops->init(type);
>> +       if (frontswap_ops)
>> +               frontswap_ops->init(type);
>>  }
>>
>>  static bool __frontswap_test(struct swap_info_struct *sis,
>> --
>> 2.25.1
>>
> .
>


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

* Re: [PATCH -next v3 2/5] frontswap: invoke ops->init for online swap device in frontswap_register_ops
  2022-08-28 20:47   ` Vitaly Wool
@ 2022-08-29  2:12     ` Liu Shixin
  0 siblings, 0 replies; 14+ messages in thread
From: Liu Shixin @ 2022-08-29  2:12 UTC (permalink / raw)
  To: Vitaly Wool
  Cc: Seth Jennings, Dan Streetman, Andrew Morton, Nathan Chancellor,
	Christoph Hellwig, linux-mm, linux-kernel, Kefeng Wang



On 2022/8/29 4:47, Vitaly Wool wrote:
> On Sat, Aug 27, 2022 at 12:12 PM Liu Shixin <liushixin2@huawei.com> wrote:
>> Since we are supported to delay zswap initializaton, we need to invoke
>> ops->init for the swap device which is already online when register
>> backend.
>>
>> This patch is a revert of f328c1d16e4c ("frontswap: simplify frontswap_register_ops")
>> and 633423a09cb5 ("mm: mark swap_lock and swap_active_head static")
>>
>> Signed-off-by: Liu Shixin <liushixin2@huawei.com>
> Sorry, is this the revert of 2 patches at the same time? I would
> rather not do it like that.
>
> Thanks,
> Vitaly
Thanks for your advice, I'll split it to two patches and resend it.
>> ---
>>  include/linux/swapfile.h |  2 ++
>>  mm/frontswap.c           | 47 ++++++++++++++++++++++++++++++++++++++++
>>  mm/swapfile.c            |  4 ++--
>>  3 files changed, 51 insertions(+), 2 deletions(-)
>>
>> diff --git a/include/linux/swapfile.h b/include/linux/swapfile.h
>> index 2fbcc9afd814..75fc069594a5 100644
>> --- a/include/linux/swapfile.h
>> +++ b/include/linux/swapfile.h
>> @@ -6,6 +6,8 @@
>>   * these were static in swapfile.c but frontswap.c needs them and we don't
>>   * want to expose them to the dozens of source files that include swap.h
>>   */
>> +extern spinlock_t swap_lock;
>> +extern struct plist_head swap_active_head;
>>  extern struct swap_info_struct *swap_info[];
>>  extern unsigned long generic_max_swapfile_size(void);
>>  /* Maximum swapfile size supported for the arch (not inclusive). */
>> diff --git a/mm/frontswap.c b/mm/frontswap.c
>> index 620f95af81dd..449e6f499b88 100644
>> --- a/mm/frontswap.c
>> +++ b/mm/frontswap.c
>> @@ -96,11 +96,58 @@ static inline void inc_frontswap_invalidates(void) { }
>>   */
>>  int frontswap_register_ops(const struct frontswap_ops *ops)
>>  {
>> +       DECLARE_BITMAP(a, MAX_SWAPFILES);
>> +       DECLARE_BITMAP(b, MAX_SWAPFILES);
>> +       struct swap_info_struct *si;
>> +       unsigned int i;
>> +
>>         if (frontswap_ops)
>>                 return -EINVAL;
>>
>> +       bitmap_zero(a, MAX_SWAPFILES);
>> +       bitmap_zero(b, MAX_SWAPFILES);
>> +
>> +       spin_lock(&swap_lock);
>> +       plist_for_each_entry(si, &swap_active_head, list) {
>> +               if (!WARN_ON(!si->frontswap_map))
>> +                       __set_bit(si->type, a);
>> +       }
>> +       spin_unlock(&swap_lock);
>> +
>> +       /* the new ops needs to know the currently active swap devices */
>> +       for_each_set_bit(i, a, MAX_SWAPFILES) {
>> +               pr_err("init frontswap_ops\n");
>> +               ops->init(i);
>> +       }
>> +
>>         frontswap_ops = ops;
>>         static_branch_inc(&frontswap_enabled_key);
>> +
>> +       spin_lock(&swap_lock);
>> +       plist_for_each_entry(si, &swap_active_head, list) {
>> +               if (si->frontswap_map)
>> +                       __set_bit(si->type, b);
>> +       }
>> +       spin_unlock(&swap_lock);
>> +
>> +       /*
>> +        * On the very unlikely chance that a swap device was added or
>> +        * removed between setting the "a" list bits and the ops init
>> +        * calls, we re-check and do init or invalidate for any changed
>> +        * bits.
>> +        */
>> +       if (unlikely(!bitmap_equal(a, b, MAX_SWAPFILES))) {
>> +               for (i = 0; i < MAX_SWAPFILES; i++) {
>> +                       if (!test_bit(i, a) && test_bit(i, b)) {
>> +                               pr_err("init frontswap_ops re\n");
>> +                               ops->init(i);
>> +                       } else if (test_bit(i, a) && !test_bit(i, b)) {
>> +                               pr_err("inval frontswap_ops re\n");
>> +                               ops->invalidate_area(i);
>> +                       }
>> +               }
>> +       }
>> +
>>         return 0;
>>  }
>>
>> diff --git a/mm/swapfile.c b/mm/swapfile.c
>> index 469d9af86be2..d383b282f269 100644
>> --- a/mm/swapfile.c
>> +++ b/mm/swapfile.c
>> @@ -51,7 +51,7 @@ static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
>>                                  unsigned char);
>>  static void free_swap_count_continuations(struct swap_info_struct *);
>>
>> -static DEFINE_SPINLOCK(swap_lock);
>> +DEFINE_SPINLOCK(swap_lock);
>>  static unsigned int nr_swapfiles;
>>  atomic_long_t nr_swap_pages;
>>  /*
>> @@ -77,7 +77,7 @@ static const char Unused_offset[] = "Unused swap offset entry ";
>>   * all active swap_info_structs
>>   * protected with swap_lock, and ordered by priority.
>>   */
>> -static PLIST_HEAD(swap_active_head);
>> +PLIST_HEAD(swap_active_head);
>>
>>  /*
>>   * all available (active, not full) swap_info_structs
>> --
>> 2.25.1
>>
> .
>


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

* Re: [PATCH -next v3 1/5] frontswap: skip frontswap_ops init if zswap init failed.
  2022-08-29  2:10     ` Liu Shixin
@ 2022-08-29  6:11       ` Vitaly Wool
  0 siblings, 0 replies; 14+ messages in thread
From: Vitaly Wool @ 2022-08-29  6:11 UTC (permalink / raw)
  To: Liu Shixin
  Cc: Seth Jennings, Dan Streetman, Andrew Morton, Nathan Chancellor,
	Christoph Hellwig, linux-mm, linux-kernel, Kefeng Wang

On Mon, Aug 29, 2022 at 4:11 AM Liu Shixin <liushixin2@huawei.com> wrote:
>
> On 2022/8/29 4:44, Vitaly Wool wrote:
> > On Sat, Aug 27, 2022 at 12:12 PM Liu Shixin <liushixin2@huawei.com> wrote:
> >> If zswap initial failed or has not been initial, frontswap_ops will be
> >> NULL. In such situation, swap device would enable failed with following
> >> stack trace:
> >>
> >>   Unable to handle kernel access to user memory outside uaccess routines at virtual address 0000000000000000
> >>   Mem abort info:
> >>     ESR = 0x0000000096000004
> >>     EC = 0x25: DABT (current EL), IL = 32 bits
> >>     SET = 0, FnV = 0
> >>     EA = 0, S1PTW = 0
> >>     FSC = 0x04: level 0 translation fault
> >>   Data abort info:
> >>     ISV = 0, ISS = 0x00000004
> >>     CM = 0, WnR = 0
> >>   user pgtable: 4k pages, 48-bit VAs, pgdp=00000020a4fab000
> >>   [0000000000000000] pgd=0000000000000000, p4d=0000000000000000
> >>   Internal error: Oops: 96000004 [#1] SMP
> >>   Modules linked in: zram fsl_dpaa2_eth pcs_lynx phylink ahci_qoriq crct10dif_ce ghash_ce sbsa_gwdt fsl_mc_dpio nvme lm90 nvme_core at803x xhci_plat_hcd rtc_fsl_ftm_alarm xgmac_mdio ahci_platform i2c_imx ip6_tables ip_tables fuse
> >>   Unloaded tainted modules: cppc_cpufreq():1
> >>   CPU: 10 PID: 761 Comm: swapon Not tainted 6.0.0-rc2-00454-g22100432cf14 #1
> >>   Hardware name: SolidRun Ltd. SolidRun CEX7 Platform, BIOS EDK II Jun 21 2022
> >>   pstate: 00400005 (nzcv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
> >>   pc : frontswap_init+0x38/0x60
> >>   lr : __do_sys_swapon+0x8a8/0x9f4
> >>   sp : ffff80000969bcf0
> >>   x29: ffff80000969bcf0 x28: ffff37bee0d8fc00 x27: ffff80000a7f5000
> >>   x26: fffffcdefb971e80 x25: ffffaba797453b90 x24: 0000000000000064
> >>   x23: ffff37c1f209d1a8 x22: ffff37bee880e000 x21: ffffaba797748560
> >>   x20: ffff37bee0d8fce4 x19: ffffaba797748488 x18: 0000000000000014
> >>   x17: 0000000030ec029a x16: ffffaba795a479b0 x15: 0000000000000000
> >>   x14: 0000000000000000 x13: 0000000000000030 x12: 0000000000000001
> >>   x11: ffff37c63c0aba18 x10: 0000000000000000 x9 : ffffaba7956b8c88
> >>   x8 : ffff80000969bcd0 x7 : 0000000000000000 x6 : 0000000000000000
> >>   x5 : 0000000000000001 x4 : 0000000000000000 x3 : ffffaba79730f000
> >>   x2 : ffff37bee0d8fc00 x1 : 0000000000000000 x0 : 0000000000000000
> >>   Call trace:
> >>   frontswap_init+0x38/0x60
> >>   __do_sys_swapon+0x8a8/0x9f4
> >>   __arm64_sys_swapon+0x28/0x3c
> >>   invoke_syscall+0x78/0x100
> >>   el0_svc_common.constprop.0+0xd4/0xf4
> >>   do_el0_svc+0x38/0x4c
> >>   el0_svc+0x34/0x10c
> >>   el0t_64_sync_handler+0x11c/0x150
> >>   el0t_64_sync+0x190/0x194
> >>   Code: d000e283 910003fd f9006c41 f946d461 (f9400021)
> >>   ---[ end trace 0000000000000000 ]---
> >>
> > Well, this issue you are seeing is in fact introduced by the following patch:
> >
> > author Christoph Hellwig <hch@lst.de> 2022-01-21 22:15:10 -0800
> > frontswap: remove support for multiple ops
> >
> > So I would rather see that one reverted and fixed.
> >
> > Thanks,
> > Vitaly
> It is surely introduced by the previous patch ,but is it need to revert that patch? Do we have
> any plans to add new backend in the future?

I believe we do. Besides, this patch introduces the bug you have hit,
before this patch frontswap just wouldn't go doing anything on an
empty list.
It's my bad I didn't NAK that patch then, but we have an opportunity
to do it the right way now.

Thanks,
Vitaly

> Thanks,
> >
> >> Reported-by: Nathan Chancellor <nathan@kernel.org>
> >> Signed-off-by: Liu Shixin <liushixin2@huawei.com>
> >> ---
> >>  mm/frontswap.c | 3 ++-
> >>  1 file changed, 2 insertions(+), 1 deletion(-)
> >>
> >> diff --git a/mm/frontswap.c b/mm/frontswap.c
> >> index 1a97610308cb..620f95af81dd 100644
> >> --- a/mm/frontswap.c
> >> +++ b/mm/frontswap.c
> >> @@ -125,7 +125,8 @@ void frontswap_init(unsigned type, unsigned long *map)
> >>          * p->frontswap set to something valid to work properly.
> >>          */
> >>         frontswap_map_set(sis, map);
> >> -       frontswap_ops->init(type);
> >> +       if (frontswap_ops)
> >> +               frontswap_ops->init(type);
> >>  }
> >>
> >>  static bool __frontswap_test(struct swap_info_struct *sis,
> >> --
> >> 2.25.1
> >>
> > .
> >
>

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

end of thread, other threads:[~2022-08-29  6:11 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-27 10:45 [PATCH -next v3 0/5] Delay the initializaton of zswap Liu Shixin
2022-08-27 10:45 ` [PATCH -next v3 1/5] frontswap: skip frontswap_ops init if zswap init failed Liu Shixin
2022-08-28 20:44   ` Vitaly Wool
2022-08-29  2:10     ` Liu Shixin
2022-08-29  6:11       ` Vitaly Wool
2022-08-27 10:45 ` [PATCH -next v3 2/5] frontswap: invoke ops->init for online swap device in frontswap_register_ops Liu Shixin
2022-08-28 20:47   ` Vitaly Wool
2022-08-29  2:12     ` Liu Shixin
2022-08-27 10:45 ` [PATCH -next v3 3/5] mm/zswap: replace zswap_init_{started/failed} with zswap_init_state Liu Shixin
2022-08-28 20:46   ` Vitaly Wool
2022-08-27 10:45 ` [PATCH -next v3 4/5] mm/zswap: delay the initializaton of zswap until the first enablement Liu Shixin
2022-08-28 20:45   ` Vitaly Wool
2022-08-27 10:46 ` [PATCH -next v3 5/5] mm/zswap: skip confusing print info Liu Shixin
2022-08-27 22:13 ` [PATCH -next v3 0/5] Delay the initializaton of zswap Andrew Morton

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).