[PATCH v4] lockd: serialize server host->h_rpcclnt across rebind, async submit, and shutdown

From: Ziheng Wei

Date: Wed Dec 02 2026 - 22:34:06 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 in the lockd kthread:
- 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(), so a concurrent shutdown
can free clnt under it.
Also, after the walk, a server host in that net can still bind a new
h_rpcclnt that no later step shuts down.

Add a per-net nlmsvc_shutdown flag to struct lockd_net.
nlm_shutdown_hosts_net() sets it under nlm_host_mutex before the walk;
lockd_up_net() clears it before make_socks() and sets it again if
make_socks() fails. While it is set, nlmsvc_lookup_host() returns NULL
and the new nlm_bind_host_locked() refuses to bind a server host.

The walk now detaches h_rpcclnt under h_mutex and calls
rpc_shutdown_client() after dropping it. For server hosts,
__nlm_async_call() holds h_mutex across nlm_bind_host_locked() and
rpc_run_task(), so either the bind fails or the task is attached to the
client, holding its own rpc_clnt reference, before the walk detaches it
and calls rpc_shutdown_client(). A plain refcount_inc() pins the host
across that section, because rpc_run_task() may release the request and
its host reference before h_mutex is dropped; nlm_get_host() would
reset h_expires and let nlm_gc_hosts() skip the host.

Drop the redundant unlocked nlm_rebind_host() call in
nlmsvc_grant_blocked(), since the callback path proceeds to
nlm_async_call() -> __nlm_async_call() -> nlm_bind_host_locked(), which
now performs the server-side rebind under h_mutex, and assert h_mutex
for server hosts in nlm_rebind_host().

Found by LLM-assisted static analysis; the original rebind/shutdown race
was reproduced in QEMU+KASAN (slab-use-after-free in rpc_force_rebind()).
v4 was compile-tested on x86_64 only.

Fixes: 3b64739fb928 ("Lockd: shutdown NLM hosts in network namespace context")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ziheng Wei <zihengw03@xxxxxxx>
---
v3 -> v4:
- Document the NULL return cases in nlmsvc_lookup_host()'s kernel-doc
(Chuck Lever).
- Add kernel-doc comments on both nlm_bind_host_locked() and
nlm_bind_host() documenting the h_mutex requirement, sleeping in
rpc_create(), and the NULL return once nlm_shutdown_hosts_net() has
started (Chuck Lever).
- Add a Context: note to nlm_rebind_host()'s kernel-doc and assert
lockdep_assert_held(&host->h_mutex) when host->h_server is set
(Chuck Lever).
- Drop the redundant nlm_rebind_host() call in nlmsvc_grant_blocked()
and update its comment to point to nlm_bind_host_locked() (Chuck Lever).
- v3: https://lore.kernel.org/r/20261201030024.1523501-1-zihengw03@xxxxxxx

v2 -> v3:
- Replace the per-host h_shutdown flag and nlm_reopen_hosts_net() with a
per-net nlmsvc_shutdown flag in struct lockd_net; the make_socks()
error path in lockd_up_net() now only sets the flag again instead of
calling nlm_shutdown_hosts_net() (Chuck Lever).
- Have nlmsvc_lookup_host() return NULL while the flag is set, so it can
no longer find or create a server host in that net after the walk
(window reported by Chuck Lever).
- Document nlmsvc_shutdown and update the h_mutex comment (Chuck Lever).
- Pin the host in __nlm_async_call() with refcount_inc() instead of
nlm_get_host(), which reset h_expires and could make the shutdown GC
skip the host (Sashiko AI review).
- v2: https://lore.kernel.org/r/20261130134320.1412254-1-zihengw03@xxxxxxx

v1 -> v2:
- Do not skip shutdown entries in nlmsvc_lookup_host(), add host->h_mutex
serialization across async rpc_run_task(), and add Fixes/Cc: stable.

fs/lockd/clntproc.c | 25 +++++++++++++++
fs/lockd/host.c | 76 +++++++++++++++++++++++++++++++++++++++------
fs/lockd/lockd.h | 3 +-
fs/lockd/netns.h | 1 +
fs/lockd/svc.c | 6 +++-
fs/lockd/svclock.c | 9 +++---
6 files changed, 104 insertions(+), 16 deletions(-)

diff --git a/fs/lockd/clntproc.c b/fs/lockd/clntproc.c
index f8018bfe9..6485b547d 100644
--- a/fs/lockd/clntproc.c
+++ b/fs/lockd/clntproc.c
@@ -354,6 +354,31 @@ static struct rpc_task *__nlm_async_call(struct nlm_rqst *req, u32 proc, struct
dprintk("lockd: call procedure %d on %s (async)\n",
(int)proc, host->h_name);

+ if (host->h_server) {
+ struct rpc_task *task;
+
+ /* Serialize with the walk in nlm_shutdown_hosts_net() */
+ mutex_lock(&host->h_mutex);
+ clnt = nlm_bind_host_locked(host);
+ if (!clnt) {
+ mutex_unlock(&host->h_mutex);
+ goto out_err;
+ }
+ msg->rpc_proc = &clnt->cl_procinfo[proc];
+ task_setup_data.rpc_client = clnt;
+
+ /*
+ * rpc_run_task() may release @req, and its host reference,
+ * before h_mutex is dropped. Pin the host without touching
+ * h_expires, which nlm_get_host() would push into the future.
+ */
+ refcount_inc(&host->h_count);
+ 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/host.c b/fs/lockd/host.c
index d572cb275..9bc521f7a 100644
--- a/fs/lockd/host.c
+++ b/fs/lockd/host.c
@@ -346,6 +346,9 @@ EXPORT_SYMBOL_GPL(nlmclnt_shutdown_rpc_clnt);
* NLM request. If one doesn't already exist in the host cache, a
* new handle is created and returned.
*
+ * Returns NULL if lockd is shutting down in this network namespace,
+ * or if a new nlm_host cannot be allocated.
+ *
* Before possibly creating a new nlm_host, construct a sockaddr
* for a specific source address in case the local system has
* multiple network addresses. The family of the address in
@@ -381,6 +384,9 @@ struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp,

mutex_lock(&nlm_host_mutex);

+ if (READ_ONCE(ln->nlmsvc_shutdown))
+ goto out;
+
if (time_after_eq(jiffies, ln->next_gc))
nlm_gc_hosts(net);

@@ -449,19 +455,32 @@ void nlmsvc_release_host(struct nlm_host *host)
refcount_dec(&host->h_count);
}

-/*
- * Create the NLM RPC client for an NLM peer
+/**
+ * nlm_bind_host_locked - Create or rebind the NLM RPC client for an NLM peer
+ * @host: NLM host handle for peer
+ *
+ * Context: Caller must hold @host->h_mutex. May sleep in rpc_create().
+ *
+ * Return: Pointer to the bound rpc_clnt, or %NULL if @host is a server
+ * host and nlm_shutdown_hosts_net() has started on its network namespace,
+ * or if rpc_create() fails.
*/
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_server) {
+ struct lockd_net *ln = net_generic(host->net, lockd_net_id);
+
+ if (READ_ONCE(ln->nlmsvc_shutdown))
+ return NULL;
+ }

/* If we've already created an RPC client, check whether
* RPC rebind is required
@@ -513,6 +532,29 @@ nlm_bind_host(struct nlm_host *host)
}
}

+ return clnt;
+}
+
+/**
+ * nlm_bind_host - Create or rebind the NLM RPC client for an NLM peer
+ * @host: NLM host handle for peer
+ *
+ * Acquires @host->h_mutex and calls nlm_bind_host_locked().
+ *
+ * Context: Process context. Acquires @host->h_mutex and may sleep in
+ * rpc_create().
+ *
+ * Return: Pointer to the bound rpc_clnt, or %NULL if @host is a server
+ * host and nlm_shutdown_hosts_net() has started on its network namespace,
+ * or if rpc_create() fails.
+ */
+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;
}
@@ -524,10 +566,16 @@ nlm_bind_host(struct nlm_host *host)
* This is not needed when using a connection-oriented protocol, such as TCP.
* The existing autobind mechanism is sufficient to force a rebind when
* required, e.g. on connection state transitions.
+ *
+ * Context: For a server host (@host->h_server is set), caller must hold
+ * @host->h_mutex. Client-side callers invoke this without @host->h_mutex.
*/
void
nlm_rebind_host(struct nlm_host *host)
{
+ if (host->h_server)
+ lockdep_assert_held(&host->h_mutex);
+
if (host->h_proto != IPPROTO_UDP)
return;

@@ -646,17 +694,27 @@ nlm_shutdown_hosts_net(struct net *net)

mutex_lock(&nlm_host_mutex);

+ if (net) {
+ struct lockd_net *ln = net_generic(net, lockd_net_id);
+
+ WRITE_ONCE(ln->nlmsvc_shutdown, true);
+ }
+
/* First, make all hosts eligible for gc */
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);
+ 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/lockd.h b/fs/lockd/lockd.h
index 14cc952fe..f6e1c664a 100644
--- a/fs/lockd/lockd.h
+++ b/fs/lockd/lockd.h
@@ -99,7 +99,7 @@ struct nlm_host {
u32 h_nsmstate; /* true remote NSM state */
u32 h_pidcount; /* Pseudopids */
refcount_t h_count; /* reference count */
- struct mutex h_mutex; /* mutex for pmap binding */
+ struct mutex h_mutex; /* RPC binding, server h_rpcclnt */
unsigned long h_nextrebind; /* next portmap call */
unsigned long h_expires; /* eligible for GC */
struct list_head h_lockowners; /* Lockowners for the client */
@@ -287,6 +287,7 @@ struct nlm_host *nlmsvc_lookup_host(const struct svc_rqst *rqstp,
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 *);
diff --git a/fs/lockd/netns.h b/fs/lockd/netns.h
index 88e8e2a97..4dd1dfc73 100644
--- a/fs/lockd/netns.h
+++ b/fs/lockd/netns.h
@@ -8,6 +8,7 @@

struct lockd_net {
unsigned int nlmsvc_users;
+ bool nlmsvc_shutdown; /* refuse server host lookup and bind */
unsigned long next_gc;
unsigned long nrhosts;
u32 gracetime;
diff --git a/fs/lockd/svc.c b/fs/lockd/svc.c
index f0e1a58c9..616ef9d25 100644
--- a/fs/lockd/svc.c
+++ b/fs/lockd/svc.c
@@ -231,13 +231,17 @@ static int lockd_up_net(struct svc_serv *serv, struct net *net,
if (error)
goto err_bind;

+ WRITE_ONCE(ln->nlmsvc_shutdown, false);
+
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:
+ WRITE_ONCE(ln->nlmsvc_shutdown, true);
err_bind:
ln->nlmsvc_users--;
return error;
diff --git a/fs/lockd/svclock.c b/fs/lockd/svclock.c
index 495eacb32..37216bc9c 100644
--- a/fs/lockd/svclock.c
+++ b/fs/lockd/svclock.c
@@ -832,12 +832,11 @@ nlmsvc_grant_blocked(struct nlm_block *block)
nlmsvc_unlink_block(block);

/* If b_granted is true this means we've been here before.
- * Just retry the grant callback, possibly refreshing the RPC
- * binding */
- if (block->b_granted) {
- nlm_rebind_host(block->b_host);
+ * Just retry the grant callback; nlm_bind_host_locked() will
+ * refresh the RPC binding if needed.
+ */
+ if (block->b_granted)
goto callback;
- }

/* Try the lock operation again */
/* vfs_lock_file() can mangle fl_start and fl_end, but we need

base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa
--
2.43.0