Re: [PATCH v2] lockd: serialize server host->h_rpcclnt across rebind, async submit, and shutdown
From: Ziheng Wei
Date: Mon Nov 30 2026 - 22:01:11 EST
On Mon, Nov 30, 2026, Chuck Lever wrote:
> 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.
You're right. The v2 commit message claimed more than the code did.
> 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()?
v3 uses a per-net flag, ln->nlmsvc_shutdown. nlm_shutdown_hosts_net()
sets it under nlm_host_mutex before the walk, and nlm_bind_host_locked()
checks it under h_mutex for server hosts. v3 also has
nlmsvc_lookup_host() return NULL while the flag is set. After the walk,
a request can then neither find nor create a host in that net through
nlmsvc_lookup_host(), and that path no longer calls nlm_get_host() on a
walked host.
I also looked at the reordering. By itself it does not close the race:
- svc_xprt_destroy_all() does not wait for a request that the lockd
thread has already dequeued. svc_udp_recvfrom() and svc_tcp_recvfrom()
call svc_xprt_received() before the request is processed. The
transport can therefore be closed and deleted while that request is
still on its way to nlmsvc_lookup_host() or nlm_async_reply().
- nlmsvc_retry_blocked() does not depend on the transports. Suppose it
is in nlmsvc_grant_blocked(), holding file->f_mutex, when the walk
clears h_rpcclnt. __nlm_async_call() would then create a new rpc_clnt
before nlmsvc_free_host_resources() can take f_mutex and remove the
block.
The bind check covers both cases, so v3 leaves the order in
lockd_down_net() as it is.
One case v3 does not change: work already running in the lockd thread
can hold a host reference while the GC pass runs, for example between
nlm_alloc_call() and nlm_async_reply() in nlmsvc_callback(). The GC pass
skips that host. It stays in nlm_server_hosts until lockd is started
again in that net and a later GC collects it, or until lockd() exits and
calls nlm_shutdown_hosts(). If the netns exits first, lockd_exit_net()
warns about the leftover nsm_handle. The short reference that
__nlm_async_call() now holds across rpc_run_task() is the same kind of
reference. This predates the patch. With the flag set, such a host can
no longer bind an rpc_clnt.
> The only caller, lockd_up_net(), always passes a non-NULL net. Maybe
> the "net &&" test isn't needed here?
v3 removes nlm_reopen_hosts_net(). lockd_up_net() now just clears
ln->nlmsvc_shutdown before make_socks().
> 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?
h_shutdown is gone. nlmsvc_shutdown in struct lockd_net has a comment,
and the h_mutex comment now reads "RPC binding, server h_rpcclnt".
> 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.
No, this fix doesn't need it. In v3 the err_socks path only sets
ln->nlmsvc_shutdown again. The full teardown would have collected, for
example, hosts created by requests on a listener that make_socks()
opened before it failed. That window predates this patch. Also, if this
was the last lockd user, lockd_up() calls lockd_put(), and lockd() then
runs nlm_shutdown_hosts() as it exits.
Separately, Sashiko's review of v2 pointed out that the nlm_get_host()
I added in __nlm_async_call() resets h_expires. If that races with the
walk, nlm_gc_hosts() skips the host, and the host and its nsm_handle
outlive the netns. v3 pins the host with refcount_inc() instead.
I'll post v3 shortly.
Thanks,
Ziheng