1 From 164ec4ae01c28cd627d085c19f5e1c631b4cccff Mon Sep 17 00:00:00 2001
2 From: Phil Elwell <phil@raspberrypi.com>
3 Date: Tue, 5 May 2020 15:23:32 +0100
4 Subject: [PATCH] zswap: Defer zswap initialisation
6 Enabling zswap support in the kernel configuration costs about 1.5MB
7 of RAM, even when zswap is not enabled at runtime. This cost can be
8 reduced significantly by deferring initialisation (including pool
9 creation) until the "enabled" parameter is set to true. There is a
10 small cost to this in that some initialisation code has to remain in
11 memory after the init phase, just in case they are needed later,
12 but the total size increase is negligible.
14 See: https://github.com/raspberrypi/linux/pull/3432
16 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
18 mm/zswap.c | 53 +++++++++++++++++++++++++++++------------------------
19 1 file changed, 29 insertions(+), 24 deletions(-)
23 @@ -592,8 +592,9 @@ error:
27 -static __init struct zswap_pool *__zswap_pool_create_fallback(void)
28 +static bool zswap_try_pool_create(void)
30 + struct zswap_pool *pool;
31 bool has_comp, has_zpool;
33 has_comp = crypto_has_comp(zswap_compressor, 0, 0);
34 @@ -629,9 +630,21 @@ static __init struct zswap_pool *__zswap
37 if (!has_comp || !has_zpool)
41 + pool = zswap_pool_create(zswap_zpool_type, zswap_compressor);
43 - return zswap_pool_create(zswap_zpool_type, zswap_compressor);
45 + pr_info("loaded using pool %s/%s\n", pool->tfm_name,
46 + zpool_get_type(pool->zpool));
47 + list_add(&pool->list, &zswap_pools);
48 + zswap_has_pool = true;
50 + pr_err("pool creation failed\n");
51 + zswap_enabled = false;
54 + return zswap_enabled;
57 static void zswap_pool_destroy(struct zswap_pool *pool)
58 @@ -804,16 +817,19 @@ static int zswap_zpool_param_set(const c
59 static int zswap_enabled_param_set(const char *val,
60 const struct kernel_param *kp)
64 if (zswap_init_failed) {
65 pr_err("can't enable, initialization failed\n");
68 - if (!zswap_has_pool && zswap_init_started) {
69 - pr_err("can't enable, no pool configured\n");
73 - return param_set_bool(val, kp);
74 + ret = param_set_bool(val, kp);
75 + if (!ret && zswap_enabled && zswap_init_started && !zswap_has_pool)
76 + if (!zswap_try_pool_create())
82 /*********************************
83 @@ -1314,7 +1330,6 @@ static void __exit zswap_debugfs_exit(vo
84 **********************************/
85 static int __init init_zswap(void)
87 - struct zswap_pool *pool;
90 zswap_init_started = true;
91 @@ -1338,29 +1353,19 @@ static int __init init_zswap(void)
95 - pool = __zswap_pool_create_fallback();
97 - pr_info("loaded using pool %s/%s\n", pool->tfm_name,
98 - zpool_get_type(pool->zpool));
99 - list_add(&pool->list, &zswap_pools);
100 - zswap_has_pool = true;
102 - pr_err("pool creation failed\n");
103 - zswap_enabled = false;
106 shrink_wq = create_workqueue("zswap-shrink");
108 - goto fallback_fail;
111 frontswap_register_ops(&zswap_frontswap_ops);
112 if (zswap_debugfs_init())
113 pr_warn("debugfs initialization failed\n");
116 + zswap_try_pool_create();
122 - zswap_pool_destroy(pool);
124 cpuhp_remove_state(CPUHP_MM_ZSWP_MEM_PREPARE);