[Devel] [PATCH] tcache/tswap: don't shrink tcache/tswap until they are initialized

Konstantin Khorenko khorenko at virtuozzo.com
Tue Dec 11 17:21:35 MSK 2018


kswapd could run before tcache_init()/tswap_init() finished,
thus before tcache_nodeinfo/tswap_lru_node are allocated,
thus lead to a kernel panic in tcache_shrink_count()/tswap_shrink_count().

https://pmc.acronis.com/browse/VSTOR-18694

Found-by: Andrey Ryabinin <aryabinin at virtuozzo.com>
Signed-off-by: Konstantin Khorenko <khorenko at virtuozzo.com>
---
 mm/internal.h | 8 ++++----
 mm/tcache.c   | 3 +++
 mm/tswap.c    | 3 +++
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/mm/internal.h b/mm/internal.h
index b34b03dc37f2..10d790bb6bda 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -393,9 +393,9 @@ unsigned long tswap_shrink_count(struct shrinker *shrink,
 static inline unsigned long tswap_shrink(struct shrink_control *sc)
 {
 	unsigned long ret;
-	extern bool tswap_enabled;
+	extern struct static_key tswap_inited;
 
-	if (!READ_ONCE(tswap_enabled))
+	if (static_key_false(&tswap_inited))
 		return 0;
 
 	ret = tswap_shrink_count(NULL, sc);
@@ -421,9 +421,9 @@ unsigned long tcache_shrink_count(struct shrinker *shrink,
 static inline unsigned long tcache_shrink(struct shrink_control *sc)
 {
 	unsigned long ret;
-	extern bool tcache_enabled;
+	extern struct static_key tcache_inited;
 
-	if (!READ_ONCE(tcache_enabled))
+	if (static_key_false(&tcache_inited))
 		return 0;
 
 	ret = tcache_shrink_count(NULL, sc);
diff --git a/mm/tcache.c b/mm/tcache.c
index a6afb0c510aa..9a254ac2cb82 100644
--- a/mm/tcache.c
+++ b/mm/tcache.c
@@ -179,6 +179,8 @@ static struct tcache_nodeinfo *tcache_nodeinfo;
 /* Enable/disable tcache backend (set at boot time) */
 bool tcache_enabled __read_mostly = true;
 module_param_named(enabled, tcache_enabled, bool, 0444);
+/* To avoid shrink attempt before tcache is inited */
+struct static_key tcache_inited = STATIC_KEY_INIT_FALSE;
 
 /* Enable/disable populating the cache */
 static bool tcache_active __read_mostly = true;
@@ -1460,6 +1462,7 @@ static int __init tcache_init(void)
 	if (err)
 		goto out_unregister_shrinker;
 
+	static_key_slow_inc(&tcache_inited);
 	pr_info("tcache loaded\n");
 	return 0;
 
diff --git a/mm/tswap.c b/mm/tswap.c
index bf7644d5f033..e3c8cf4a51f7 100644
--- a/mm/tswap.c
+++ b/mm/tswap.c
@@ -37,6 +37,8 @@ static struct tswap_lru *tswap_lru_node;
 /* Enable/disable tswap backend (set at boot time) */
 bool tswap_enabled __read_mostly = true;
 module_param_named(enabled, tswap_enabled, bool, 0444);
+/* To avoid shrink attempt before tswap is inited */
+struct static_key tswap_inited = STATIC_KEY_INIT_FALSE;
 
 /* Enable/disable populating the cache */
 static bool tswap_active __read_mostly = true;
@@ -433,6 +435,7 @@ static int __init tswap_init(void)
 	frontswap_tmem_exclusive_gets(true);
 
 	old_ops = frontswap_register_ops(&tswap_frontswap_ops);
+	static_key_slow_inc(&tswap_inited);
 	pr_info("tswap loaded\n");
 	if (old_ops)
 		pr_warn("tswap: frontswap_ops %p overridden\n", old_ops);
-- 
2.15.1



More information about the Devel mailing list