Commit 699bb9e8 authored by Liu Shixin's avatar Liu Shixin Committed by Ma Wupeng
Browse files

mm/zswap: delay the initializaton of zswap until the first enablement

hulk inclusion
category: performance
bugzilla: 187468, https://gitee.com/openeuler/kernel/issues/I61HSG



--------------------------------

The zswap initialization consumes some memory. If we don't use zswap,
there's no need to initial it. So delay the initializaton of zswap until
the first enablement.

Signed-off-by: default avatarLiu Shixin <liushixin2@huawei.com>
parent 3286455e
Loading
Loading
Loading
Loading
+37 −7
Original line number Diff line number Diff line
@@ -79,6 +79,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 *,
@@ -209,6 +211,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;
@@ -767,11 +771,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)) {
@@ -861,6 +871,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;
@@ -1435,14 +1453,14 @@ static int __init zswap_debugfs_init(void)
static void __exit zswap_debugfs_exit(void) { }
#endif

/*********************************
* 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;
@@ -1496,6 +1514,18 @@ static int __init init_zswap(void)
	zswap_enabled = false;
	return -ENOMEM;
}

/*********************************
* module init and exit
**********************************/
static int __init init_zswap(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);