eccee8dd3607c588e54fdaa7a91bba6a0085cfc0
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-6.1 / 950-0238-zswap-Defer-zswap-initialisation.patch
1 From cfe4cda49168a35df7a278e567542c2b7fa096c3 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
5
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.
13
14 See: https://github.com/raspberrypi/linux/pull/3432
15
16 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
17 ---
18 mm/zswap.c | 53 +++++++++++++++++++++++++++++------------------------
19 1 file changed, 29 insertions(+), 24 deletions(-)
20
21 --- a/mm/zswap.c
22 +++ b/mm/zswap.c
23 @@ -663,8 +663,9 @@ error:
24 return NULL;
25 }
26
27 -static __init struct zswap_pool *__zswap_pool_create_fallback(void)
28 +static bool zswap_try_pool_create(void)
29 {
30 + struct zswap_pool *pool;
31 bool has_comp, has_zpool;
32
33 has_comp = crypto_has_acomp(zswap_compressor, 0, 0);
34 @@ -700,9 +701,21 @@ static __init struct zswap_pool *__zswap
35 }
36
37 if (!has_comp || !has_zpool)
38 - return NULL;
39 + return false;
40 +
41 + pool = zswap_pool_create(zswap_zpool_type, zswap_compressor);
42
43 - return zswap_pool_create(zswap_zpool_type, zswap_compressor);
44 + if (pool) {
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;
49 + } else {
50 + pr_err("pool creation failed\n");
51 + zswap_enabled = false;
52 + }
53 +
54 + return zswap_enabled;
55 }
56
57 static void zswap_pool_destroy(struct zswap_pool *pool)
58 @@ -875,16 +888,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)
61 {
62 + int ret;
63 +
64 if (zswap_init_failed) {
65 pr_err("can't enable, initialization failed\n");
66 return -ENODEV;
67 }
68 - if (!zswap_has_pool && zswap_init_started) {
69 - pr_err("can't enable, no pool configured\n");
70 - return -ENODEV;
71 - }
72
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())
77 + ret = -ENODEV;
78 +
79 + return ret;
80 }
81
82 /*********************************
83 @@ -1489,7 +1505,6 @@ static int __init zswap_debugfs_init(voi
84 **********************************/
85 static int __init init_zswap(void)
86 {
87 - struct zswap_pool *pool;
88 int ret;
89
90 zswap_init_started = true;
91 @@ -1513,33 +1528,23 @@ static int __init init_zswap(void)
92 if (ret)
93 goto hp_fail;
94
95 - pool = __zswap_pool_create_fallback();
96 - if (pool) {
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;
101 - } else {
102 - pr_err("pool creation failed\n");
103 - zswap_enabled = false;
104 - }
105 -
106 shrink_wq = create_workqueue("zswap-shrink");
107 if (!shrink_wq)
108 - goto fallback_fail;
109 + goto hp_fail;
110
111 ret = frontswap_register_ops(&zswap_frontswap_ops);
112 if (ret)
113 goto destroy_wq;
114 if (zswap_debugfs_init())
115 pr_warn("debugfs initialization failed\n");
116 +
117 + if (zswap_enabled)
118 + zswap_try_pool_create();
119 +
120 return 0;
121
122 destroy_wq:
123 destroy_workqueue(shrink_wq);
124 -fallback_fail:
125 - if (pool)
126 - zswap_pool_destroy(pool);
127 hp_fail:
128 cpuhp_remove_state(CPUHP_MM_ZSWP_MEM_PREPARE);
129 dstmem_fail: