[PATCH v2 2/3] SUNRPC: read a new listener's port before publishing its transport

From: Piyus Kedia

Date: Sun Nov 29 2026 - 12:51:04 EST


_svc_xprt_create() has the same ordering as svc_addsock() had:

svc_add_new_perm_xprt(serv, newxprt);
newport = svc_xprt_local_port(newxprt);

Once XPT_BUSY is clear the creating thread has dropped its only
reference, so newxprt may be gone before the port is read.
ed2849d3ecfa ("sunrpc: prevent use-after-free on clearing XPT_BUSY")
read the port first for exactly this reason; 39b553013719 moved the read
back after publication when it factored this code out of
svc_create_xprt().

I have no reproducer for this one - a freshly created listener does not
get XPT_CLOSE set the way svc_addsock()'s unconnected socket does - but
the rule holds either way, so swap the two statements back.

Fixes: 39b553013719 ("svcrpc: share some setup of listening sockets")
Suggested-by: Chuck Lever <cel@xxxxxxxxxx>
Signed-off-by: Kapil Vaswani <kapil@xxxxxxxxxxxxxxx>
Signed-off-by: Piyus Kedia <piyus@xxxxxxxxxxx>
---
net/sunrpc/svc_xprt.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/net/sunrpc/svc_xprt.c b/net/sunrpc/svc_xprt.c
index 08cfe8a31189..74b50326d67c 100644
--- a/net/sunrpc/svc_xprt.c
+++ b/net/sunrpc/svc_xprt.c
@@ -290,8 +290,9 @@ static int _svc_xprt_create(struct svc_serv *serv, const char *xprt_name,
return PTR_ERR(newxprt);
}
newxprt->xpt_cred = get_cred(cred);
- svc_add_new_perm_xprt(serv, newxprt);
+ /* svc_add_new_perm_xprt() drops this thread's only reference */
newport = svc_xprt_local_port(newxprt);
+ svc_add_new_perm_xprt(serv, newxprt);
return newport;
}
err: