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

From: Chuck Lever

Date: Mon Nov 30 2026 - 13:14:46 EST


> lockd: serialize server host->h_rpcclnt across rebind, async submit, and shutdown
>
> 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")
> Signed-off-by: Ziheng Wei <zihengw03@xxxxxxx>

> diff --git a/fs/lockd/host.c b/fs/lockd/host.c
> index d572cb27533f..06a3734ad2ac 100644
> --- a/fs/lockd/host.c
> +++ b/fs/lockd/host.c
> @@ -146,6 +146,7 @@ static struct nlm_host *nlm_alloc_host(struct nlm_lookup_host_info *ni,
> host->h_server = ni->server;
> host->h_noresvport = ni->noresvport;
> host->h_inuse = 0;
> + host->h_shutdown = false;

Can a server host created after nlm_shutdown_hosts_net() returns still
bind a new client? lockd_down_net() shuts down the hosts before it
closes the net's transports:

fs/lockd/svc.c:lockd_down_net() {
...
if (--ln->nlmsvc_users == 0) {
nlm_shutdown_hosts_net(net);
cancel_delayed_work_sync(&ln->grace_period_end);
locks_end_grace(&ln->lockd_manager);
svc_xprt_destroy_all(serv, net, true);
}
...
}

nlm_shutdown_hosts_net() drops nlm_host_mutex before
svc_xprt_destroy_all() runs. A request that arrives on a still-open
transport in that window reaches nlmsvc_lookup_host(), which can
allocate a new host here with h_shutdown false. That host can then
create an h_rpcclnt and queue async replies after the shutdown walk has
finished. No later step in lockd_down_net() shuts that rpc_clnt down.

The window predates this patch. Still, the commit message promises that
rpc_shutdown_client() drains every submitted task, and that promise
holds only for hosts that exist when the walk runs.

Might a flag in struct lockd_net work better than a per-host flag?
nlm_shutdown_hosts_net() could set it before the walk, and
nlm_bind_host_locked() could check it under h_mutex. Hosts created
after the walk would then be covered too, and lockd_up_net() would
clear one flag instead of walking nlm_server_hosts. Alternatively,
could lockd_down_net() call svc_xprt_destroy_all() before
nlm_shutdown_hosts_net()?


> @@ -638,6 +651,23 @@ static void nlm_complain_hosts(struct net *net)
> }
> }
>
> +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;

The only caller, lockd_up_net(), always passes a non-NULL net. Maybe
the "net &&" test isn't needed here?


> diff --git a/fs/lockd/lockd.h b/fs/lockd/lockd.h
> index 55958e490ee4..68d10d0aa48f 100644
> --- a/fs/lockd/lockd.h
> +++ b/fs/lockd/lockd.h
> @@ -93,6 +93,7 @@ struct nlm_host {
> h_server : 1, /* server side, not client side */
> h_noresvport : 1,
> h_inuse : 1;
> + bool h_shutdown;


Nit: the neighboring fields carry a short comment and h_shutdown does
not. A few lines further down, the h_mutex comment still reads "mutex
for pmap binding". After this patch, h_mutex also protects h_rpcclnt
and h_shutdown for server hosts. Could h_shutdown get a comment, and
could the h_mutex comment be updated to match? If h_shutdown stays in
struct nlm_host, could it also sit next to h_mutex, which protects it?


> diff --git a/fs/lockd/svc.c b/fs/lockd/svc.c
> index 8e1b1735acaf..c467b61de03b 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;
>
> + 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:

nlm_shutdown_hosts_net() does much more than undo
nlm_reopen_hosts_net(). It expires every server host in the net, frees
their lock resources, runs a GC pass, and calls nlm_complain_hosts(),
which logs "couldn't shutdown host module" if any host is still
referenced. Is that full teardown wanted on a make_socks() failure?

With a flag in struct lockd_net, this error path would only need to
set the flag again.


--
Chuck Lever