[PATCH mm-unstable v2] mm/zswap: fix zswap_pools_lock usages after changing to percpu_ref

From: Chengming Zhou
Date: Wed Feb 28 2024 - 10:50:23 EST


Now the release of zswap pool is controlled by percpu_ref, its release
callback (__zswap_pool_empty()) will be called when percpu_ref hit 0.
But this release callback may potentially be called from RCU callback
context by percpu_ref_kill(), which maybe from the softirq context.

So we need to use spin_lock/unlock_bh() to avoid potential deadlock.

This problem is introduced by the commit f3da427e82c4 ("mm/zswap: change
zswap_pool kref to percpu_ref"), which is in mm-unstable branch now.
It can be reproduced by testing kernel build in tmpfs with zswap and
CONFIG_LOCKDEP enabled, meanwhile changing the zswap compressor setting
dynamically.

Signed-off-by: Chengming Zhou <chengming.zhou@xxxxxxxxx>
---
v2:
- Change to use spin_lock/unlock_bh(), per Matthew.
---
mm/zswap.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/mm/zswap.c b/mm/zswap.c
index 011e068eb355..da90933c6d20 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -459,7 +459,7 @@ static void __zswap_pool_empty(struct percpu_ref *ref)

pool = container_of(ref, typeof(*pool), ref);

- spin_lock(&zswap_pools_lock);
+ spin_lock_bh(&zswap_pools_lock);

WARN_ON(pool == zswap_pool_current());

@@ -468,7 +468,7 @@ static void __zswap_pool_empty(struct percpu_ref *ref)
INIT_WORK(&pool->release_work, __zswap_pool_release);
schedule_work(&pool->release_work);

- spin_unlock(&zswap_pools_lock);
+ spin_unlock_bh(&zswap_pools_lock);
}

static int __must_check zswap_pool_get(struct zswap_pool *pool)
@@ -598,7 +598,7 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp,
return -EINVAL;
}

- spin_lock(&zswap_pools_lock);
+ spin_lock_bh(&zswap_pools_lock);

pool = zswap_pool_find_get(type, compressor);
if (pool) {
@@ -607,7 +607,7 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp,
list_del_rcu(&pool->list);
}

- spin_unlock(&zswap_pools_lock);
+ spin_unlock_bh(&zswap_pools_lock);

if (!pool)
pool = zswap_pool_create(type, compressor);
@@ -628,7 +628,7 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp,
else
ret = -EINVAL;

- spin_lock(&zswap_pools_lock);
+ spin_lock_bh(&zswap_pools_lock);

if (!ret) {
put_pool = zswap_pool_current();
@@ -643,7 +643,7 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp,
put_pool = pool;
}

- spin_unlock(&zswap_pools_lock);
+ spin_unlock_bh(&zswap_pools_lock);

if (!zswap_has_pool && !pool) {
/* if initial pool creation failed, and this pool creation also
--
2.40.1