[PATCH] dlm: validate lockspace under ls_lock in dlm_release_lockspace()

From: Bin Luo (University of Electronic Science and Technology of China)

Date: Thu Nov 26 2026 - 01:08:23 EST


dlm_release_lockspace() currently calls dlm_find_lockspace_local(),
which dereferences the raw lockspace pointer to increment ls_count, and
immediately calls dlm_put_lockspace() before acquiring ls_lock.

When two callers concurrently release the same lockspace (for example
via concurrent DLM_USER_REMOVE_LOCKSPACE requests through
device_remove_lockspace(), which also drops its lookup reference before
calling dlm_release_lockspace()), the first caller can enter
release_lockspace(), remove the lockspace from lslist, and queue
ls_free_work to free ls before the second caller dereferences ls in
dlm_find_lockspace_local() or release_lockspace().

Since lockspaces are only added to and removed from lslist while holding
ls_lock, move the lockspace lookup inside ls_lock and verify that
lockspace is still present on lslist under lslist_lock before calling
release_lockspace(). If it has already been removed, return -EINVAL
without dereferencing the pointer.

Fixes: e7fd41792fc0 ("[DLM] The core of the DLM for GFS2/CLVM")
Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) <luob87709@xxxxxxxxx>
---
Found by code inspection. Compile-tested on x86_64 with W=1 and KASAN
(fs/dlm/lockspace.o); not runtime-tested on a live DLM cluster.

fs/dlm/lockspace.c | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/fs/dlm/lockspace.c b/fs/dlm/lockspace.c
index a9c98b4..37ccb96 100644
--- a/fs/dlm/lockspace.c
+++ b/fs/dlm/lockspace.c
@@ -800,12 +800,18 @@ int dlm_release_lockspace(void *lockspace, unsigned int release_option)
if (release_option > __DLM_RELEASE_MAX)
return -EINVAL;

- ls = dlm_find_lockspace_local(lockspace);
- if (!ls)
- return -EINVAL;
- dlm_put_lockspace(ls);
-
mutex_lock(&ls_lock);
+ spin_lock_bh(&lslist_lock);
+ list_for_each_entry(ls, &lslist, ls_list) {
+ if (ls == lockspace)
+ goto found;
+ }
+ spin_unlock_bh(&lslist_lock);
+ mutex_unlock(&ls_lock);
+ return -EINVAL;
+
+found:
+ spin_unlock_bh(&lslist_lock);
error = release_lockspace(ls, release_option);
if (!error)
ls_count--;
--
2.43.0