[PATCH v2] lockd: serialize server host->h_rpcclnt across rebind, async submit, and shutdown
From: Ziheng Wei
Date: Mon Nov 30 2026 - 08:44:21 EST
lockd_down() -> lockd_down_net() calls nlm_shutdown_hosts_net(net) while
the lockd kthread is still running. nlm_shutdown_hosts_net() shuts down
and clears host->h_rpcclnt without holding host->h_mutex, racing with
server-side callers:
- nlmsvc_grant_blocked() calls nlm_rebind_host() without h_mutex, passing
a freed or cleared h_rpcclnt to rpc_force_rebind().
- __nlm_async_call() drops h_mutex inside nlm_bind_host() before reading
clnt->cl_procinfo and calling rpc_run_task(), allowing a concurrent
shutdown to free clnt or allowing a new async task to be queued after
rpc_shutdown_client() has already drained cl_tasks.
Add bool h_shutdown to struct nlm_host (initialized in nlm_alloc_host(),
set under h_mutex in nlm_shutdown_hosts_net() while detaching h_rpcclnt
before rpc_shutdown_client(), and cleared by nlm_reopen_hosts_net() in
lockd_up_net() before opening new sockets so any surviving cached host
can bind again after a service restart in the same netns). Factor out
nlm_bind_host_locked() so __nlm_async_call() pins the server host and
holds host->h_mutex across nlm_bind_host_locked() and the non-blocking
async rpc_run_task() call, guaranteeing rpc_shutdown_client() sees and
drains every submitted task. Also serialize nlm_rebind_host() in
nlmsvc_grant_blocked() with h_mutex.
Found by LLM-assisted static analysis; the original rebind/shutdown race
was reproduced in QEMU+KASAN (slab-use-after-free in rpc_force_rebind())
and v2 was compile-tested on x86_64.
Fixes: 3b64739fb928 ("Lockd: shutdown NLM hosts in network namespace context")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ziheng Wei <zihengw03@xxxxxxx>
---
v1 -> v2 (addressing Sashiko AI review findings):
- Do not skip host->h_shutdown entries in nlmsvc_lookup_host(), avoiding
allocating duplicate leaked nlm_host/nsm_handle entries for in-flight
requests arriving before svc_xprt_destroy_all() completes.
- Add nlm_reopen_hosts_net() called from lockd_up_net() under nlmsvc_mutex
before make_socks() so any surviving cached host has h_shutdown cleared
when lockd restarts in the same netns.
- Hold host->h_mutex (with host pinned via nlm_get_host/nlmsvc_release_host)
across nlm_bind_host_locked() and the non-blocking async rpc_run_task()
for server hosts so rpc_shutdown_client() cannot return before a newly
bound async task is registered on clnt->cl_tasks, avoiding exporting
rpc_hold_client().
- Add Fixes: 3b64739fb928 and Cc: stable@xxxxxxxxxxxxxxx.
diff --git a/fs/lockd/lockd.h b/fs/lockd/lockd.h
--- a/fs/lockd/lockd.h
+++ b/fs/lockd/lockd.h
@@ -93,6 +93,7 @@
h_server : 1, /* server side, not client side */
h_noresvport : 1,
h_inuse : 1;
+ bool h_shutdown;
wait_queue_head_t h_gracewait; /* wait while reclaiming */
struct rw_semaphore h_rwsem; /* Reboot recovery lock */
u32 h_state; /* pseudo-state counter */
@@ -287,10 +288,12 @@
const char *hostname,
const size_t hostname_len);
void nlmsvc_release_host(struct nlm_host *);
+struct rpc_clnt *nlm_bind_host_locked(struct nlm_host *host);
struct rpc_clnt * nlm_bind_host(struct nlm_host *);
void nlm_rebind_host(struct nlm_host *);
struct nlm_host * nlm_get_host(struct nlm_host *);
void nlm_shutdown_hosts(void);
+void nlm_reopen_hosts_net(struct net *net);
void nlm_shutdown_hosts_net(struct net *net);
void nlm_host_rebooted(const struct net *net,
const struct lockd_reboot *);
diff --git a/fs/lockd/host.c b/fs/lockd/host.c
--- a/fs/lockd/host.c
+++ b/fs/lockd/host.c
@@ -146,6 +146,7 @@
host->h_server = ni->server;
host->h_noresvport = ni->noresvport;
host->h_inuse = 0;
+ host->h_shutdown = false;
init_waitqueue_head(&host->h_gracewait);
init_rwsem(&host->h_rwsem);
host->h_state = 0;
@@ -453,15 +454,17 @@
* Create the NLM RPC client for an NLM peer
*/
struct rpc_clnt *
-nlm_bind_host(struct nlm_host *host)
+nlm_bind_host_locked(struct nlm_host *host)
{
struct rpc_clnt *clnt;
+ lockdep_assert_held(&host->h_mutex);
+
dprintk("lockd: nlm_bind_host %s (%s)\n",
host->h_name, host->h_addrbuf);
- /* Lock host handle */
- mutex_lock(&host->h_mutex);
+ if (host->h_shutdown)
+ return NULL;
/* If we've already created an RPC client, check whether
* RPC rebind is required
@@ -512,7 +515,17 @@
clnt = NULL;
}
}
+
+ return clnt;
+}
+
+struct rpc_clnt *
+nlm_bind_host(struct nlm_host *host)
+{
+ struct rpc_clnt *clnt;
+ mutex_lock(&host->h_mutex);
+ clnt = nlm_bind_host_locked(host);
mutex_unlock(&host->h_mutex);
return clnt;
}
@@ -639,6 +652,23 @@
}
void
+nlm_reopen_hosts_net(struct net *net)
+{
+ struct hlist_head *chain;
+ struct nlm_host *host;
+
+ mutex_lock(&nlm_host_mutex);
+ for_each_host(host, chain, nlm_server_hosts) {
+ if (net && host->net != net)
+ continue;
+ mutex_lock(&host->h_mutex);
+ host->h_shutdown = false;
+ mutex_unlock(&host->h_mutex);
+ }
+ mutex_unlock(&nlm_host_mutex);
+}
+
+void
nlm_shutdown_hosts_net(struct net *net)
{
struct hlist_head *chain;
@@ -650,13 +680,18 @@
dprintk("lockd: nuking all hosts in net %x...\n",
net ? net->ns.inum : 0);
for_each_host(host, chain, nlm_server_hosts) {
+ struct rpc_clnt *clnt;
+
if (net && host->net != net)
continue;
host->h_expires = jiffies - 1;
- if (host->h_rpcclnt) {
- rpc_shutdown_client(host->h_rpcclnt);
- host->h_rpcclnt = NULL;
- }
+ mutex_lock(&host->h_mutex);
+ host->h_shutdown = true;
+ clnt = host->h_rpcclnt;
+ host->h_rpcclnt = NULL;
+ mutex_unlock(&host->h_mutex);
+ if (clnt)
+ rpc_shutdown_client(clnt);
nlmsvc_free_host_resources(host);
}
diff --git a/fs/lockd/svc.c b/fs/lockd/svc.c
--- a/fs/lockd/svc.c
+++ b/fs/lockd/svc.c
@@ -231,13 +231,17 @@
if (error)
goto err_bind;
+ nlm_reopen_hosts_net(net);
+
error = make_socks(serv, net, cred);
if (error < 0)
- goto err_bind;
+ goto err_socks;
set_grace_period(net);
dprintk("%s: per-net data created; net=%x\n", __func__, net->ns.inum);
return 0;
+err_socks:
+ nlm_shutdown_hosts_net(net);
err_bind:
ln->nlmsvc_users--;
return error;
diff --git a/fs/lockd/clntproc.c b/fs/lockd/clntproc.c
--- a/fs/lockd/clntproc.c
+++ b/fs/lockd/clntproc.c
@@ -354,6 +354,27 @@
dprintk("lockd: call procedure %d on %s (async)\n",
(int)proc, host->h_name);
+ if (host->h_server) {
+ struct rpc_task *task;
+
+ nlm_get_host(host);
+ mutex_lock(&host->h_mutex);
+ clnt = nlm_bind_host_locked(host);
+ if (!clnt) {
+ mutex_unlock(&host->h_mutex);
+ nlmsvc_release_host(host);
+ goto out_err;
+ }
+ msg->rpc_proc = &clnt->cl_procinfo[proc];
+ task_setup_data.rpc_client = clnt;
+
+ /* bootstrap and kick off the async RPC call */
+ task = rpc_run_task(&task_setup_data);
+ mutex_unlock(&host->h_mutex);
+ nlmsvc_release_host(host);
+ return task;
+ }
+
/* If we have no RPC client yet, create one. */
clnt = nlm_bind_host(host);
if (clnt == NULL)
diff --git a/fs/lockd/svclock.c b/fs/lockd/svclock.c
--- a/fs/lockd/svclock.c
+++ b/fs/lockd/svclock.c
@@ -835,7 +835,9 @@
* Just retry the grant callback, possibly refreshing the RPC
* binding */
if (block->b_granted) {
+ mutex_lock(&block->b_host->h_mutex);
nlm_rebind_host(block->b_host);
+ mutex_unlock(&block->b_host->h_mutex);
goto callback;
}
base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa