Skip to contentSkip to conversation

SUNRPC: Send and receive through rq_bvec in svcsock

PATCH New Chuck Lever 1 participant 9 messages Nov 13

Subsystems (3)
lore
Share & tools
lore

Chuck Lever

author maintainer
#0 lore raw

cover SUNRPC: Send and receive through rq_bvec in svcsock

This series is a first step toward letting NFSD pass folios from a filesystem directly to an RPC transport. That goal requires transports that take their send and receive buffers as bio_vec arrays. This series does not change struct xdr_buf, which still carries one page per entry; that will come later on.

Currently svcsock has two obstacles to a bvec-only transport interface. svc_tcp_sendmsg() and svc_udp_sendto() build the reply's bio_vec array in the per-socket sk_bvec, which NFSD cannot reach. svc_udp_recvfrom() points rq_arg.head into a linear skb instead of copying the datagram into rq_pages.

After this series both socket transports send from the per-thread rq_bvec and receive into rq_pages through a bvec iterator. The new helpers take an svc_rqst and do not reference struct svc_sock, so the calls can move into svc_send() and svc_recv() later without a change to the helpers. This series does not make that move. A later series converts svcrdma to the same helpers.

The TCP record marker stays in a page_frag buffer, now carried in the reserved rq_bvec[0]. Placing the marker in rq_respages[0] ahead of the head would save the allocation on each reply. That placement changes the buffer length setup in svc_process(), the head reassignment in svc_process_bc(), and the head-page assumptions in GSS wrap, which seems to me like a layering violation. The reserved entry serves either placement.

The UDP receive conversion is the only behavior change and comes last (patch 8). Patches 1-7 do not depend on patch 8. A single sock_recvmsg() with a bvec iterator would also take over the checksum and truncation handling from svcsock. Patch 8 keeps the MSG_PEEK receive followed by skb_recv_udp() so that the timestamp write-back and the error exits do not change. The cost of the added copy on NFSv3 NULL and GETATTR rates has not been measured.

---
Chuck Lever (8):
SUNRPC: Add KUnit tests for xdr_buf_to_bvec()
SUNRPC: Reserve a leading rq_bvec slot and add svc_rqst_res_to_bvec()
SUNRPC: Send TCP replies from rq_bvec
SUNRPC: Send UDP replies from rq_bvec
SUNRPC: Remove svc_sock::sk_bvec
SUNRPC: Add svc_rqst_set_arg_len()
SUNRPC: Move the receive bvec iterator setup out of svcsock
SUNRPC: Receive UDP datagrams into rq_pages through a bvec iterator

 include/linux/sunrpc/svc.h     |  13 ++
 include/linux/sunrpc/svcsock.h |   4 +-
 net/sunrpc/Kconfig             |   5 +-
 net/sunrpc/svc.c               |  34 ++--
 net/sunrpc/svc_xprt.c          |  87 +++++++++
 net/sunrpc/svcsock.c           | 217 ++++++----------------
 net/sunrpc/tests/xdr_kunit.c   | 397 +++++++++++++++++++++++++++++++++++++++--
 7 files changed, 563 insertions(+), 194 deletions(-)

---
base-commit: 9d9a3e68a2f8a7d69d597c65cc52026456edbf6c
change-id: 20261111-svcsock-bvecs-7dc1fb9a2947

Best regards,

Signature · 1 line
Chuck Lever <[email protected]>
author maintainer
#1 lore raw

PATCH 1/8 SUNRPC: Add KUnit tests for xdr_buf_to_bvec()

Commit 32af2cb54ed1 ("nfsd: account for page_base when advancing rq_next_page") changed how xdr_buf_to_bvec() walks the page array, and the function has no test. A splice READ at an unaligned offset produces a page_base within the first page. A page_base of a page or more arises only from a direct READ on a device with large logical blocks, and most test rigs do not have one.

Add KUnit tests that cover the page_base cases, head and tail sharing a page, a buffer with head, pages, and tail, an exact fit on the last page, a bio_vec array filled exactly, and overflow at the head, in the pages, and at the tail. The two cases with a page_base of a page or more fail without that commit and pass with it.

Split the page allocation out of xdr_test_setup() so that the new tests share the encoder tests' pages without setting up an xdr_stream.

---
 net/sunrpc/Kconfig           |   5 +-
 net/sunrpc/tests/xdr_kunit.c | 208 +++++++++++++++++++++++++++++++++++++++----
 2 files changed, 193 insertions(+), 20 deletions(-)
net/sunrpc/Kconfig +3 -2
index 5a4e098887d6..8854cf0bb1e1 100644
@@ -62,13 +62,14 @@ config SUNRPC_DEBUG_TRACE
           with sunrpc-based clients or services.
 
 config SUNRPC_KUNIT_TEST
-	tristate "KUnit tests for the SunRPC XDR encoder" if !KUNIT_ALL_TESTS
+	tristate "KUnit tests for SunRPC XDR buffers" if !KUNIT_ALL_TESTS
 	depends on SUNRPC && KUNIT
 	default KUNIT_ALL_TESTS
 	help
 	  This builds KUnit tests for encoding an RPC Reply into pages
 	  that begin at a nonzero offset, as an NFSD direct READ leaves
-	  them. The tests report their results in TAP format.
+	  them, and for converting an xdr_buf to a bio_vec array. The
+	  tests report their results in TAP format.
 
 	  For more information on KUnit and unit tests in general, refer
 	  to the KUnit documentation in Documentation/dev-tools/kunit/.
net/sunrpc/tests/xdr_kunit.c +190 -18

245 lines not on this page · raw message

Signature · 1 line
2.56.0
author maintainer
#2 lore raw

PATCH 2/8 SUNRPC: Reserve a leading rq_bvec slot and add svc_rqst_res_to_bvec()

In reply to Chuck Lever

The socket transports convert rq_res into a per-socket sk_bvec inside their xpo_sendto methods. The conversion is meant to move into svc_send() later, so that no transport walks the xdr_buf. svc_send() is transport-independent and cannot reach an array inside struct svc_sock, so the array svc_send() fills has to belong to the svc_rqst.

Each svc_rqst already has an rq_bvec of rq_maxpages entries that only the receive path and NFSD use. A TCP send through rq_bvec needs one more entry for the record marker. An ITER_BVEC iterator covers one flat array, so a single sendmsg() needs the marker and the Reply in adjacent entries.

Allocate rq_bvec with rq_maxpages + RPCSVC_BVEC_HEADROOM entries and add svc_rqst_res_to_bvec() so the transports share one xdr_buf_to_bvec() call and its size arithmetic. Export the helper for svcrdma. No behavior changes.

---
 include/linux/sunrpc/svc.h |  5 +++++
 net/sunrpc/svc.c           | 21 ++++++++++++++++++++-
 2 files changed, 25 insertions(+), 1 deletion(-)
include/linux/sunrpc/svc.h +5 -0
index 042972163b87..497f56cdf7b5 100644
@@ -136,6 +136,9 @@ enum {
 	RPCSVC_MAXPAYLOAD_UDP	= 32 * 1024,
 };
 
+/* rq_bvec entries a transport can fill ahead of the Reply message */
+#define RPCSVC_BVEC_HEADROOM	1
+
 extern u32 svc_max_payload(const struct svc_rqst *rqstp);
 
 /*
@@ -464,6 +467,8 @@ struct svc_serv *svc_create(struct svc_program *, unsigned int,
 bool		   svc_rqst_replace_page(struct svc_rqst *rqstp,
 					 struct page *page);
 void		   svc_rqst_release_pages(struct svc_rqst *rqstp);
+int		   svc_rqst_res_to_bvec(struct svc_rqst *rqstp,
+					unsigned int skip);
 int		   svc_new_thread(struct svc_serv *serv, struct svc_pool *pool);
 void		   svc_exit_thread(struct svc_rqst *);
 struct svc_serv *  svc_create_pooled(struct svc_program *prog,
net/sunrpc/svc.c +20 -1
index 920ae5aeef7e..b61edc49b253 100644
@@ -656,7 +656,7 @@ svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool, int node)
 	if (!svc_init_buffer(rqstp, serv, node))
 		goto out_enomem;
 
-	rqstp->rq_bvec = kcalloc_node(rqstp->rq_maxpages,
+	rqstp->rq_bvec = kcalloc_node(rqstp->rq_maxpages + RPCSVC_BVEC_HEADROOM,
 				      sizeof(struct bio_vec),
 				      GFP_KERNEL, node);
 	if (!rqstp->rq_bvec)
@@ -993,6 +993,25 @@ void svc_rqst_release_pages(struct svc_rqst *rqstp)
 		__folio_batch_release(&rqstp->rq_fbatch);
 }
 
+/**
+ * svc_rqst_res_to_bvec - Convert the Reply buffer into rq_bvec
+ * @rqstp: RPC transaction context
+ * @skip: count of leading rq_bvec entries left for the transport
+ *
+ * The filled entries point into rq_res without holding page references. The
+ * caller leaves rq_res intact while rq_bvec is in use.
+ *
+ * Return: the count of entries filled after @skip, or -ESERVERFAULT
+ * when rq_res does not fit in the remaining entries.
+ */
+int svc_rqst_res_to_bvec(struct svc_rqst *rqstp, unsigned int skip)
+{
+	return xdr_buf_to_bvec(rqstp->rq_bvec + skip,
+			       rqstp->rq_maxpages + RPCSVC_BVEC_HEADROOM - skip,
+			       &rqstp->rq_res);
+}
+EXPORT_SYMBOL_GPL(svc_rqst_res_to_bvec);
+
 /**
  * svc_exit_thread - finalise the termination of a sunrpc server thread
  * @rqstp: the svc_rqst which represents the thread.
Signature · 1 line
2.56.0
author maintainer
#3 lore raw

PATCH 3/8 SUNRPC: Send TCP replies from rq_bvec

In reply to Chuck Lever

svc_tcp_sendmsg() builds the reply's bio_vec array in the per-socket sk_bvec, so every accepted socket carries an array sized for the largest reply although the sending thread already has rq_bvec.

Build the array in rq_bvec, and stop allocating sk_bvec for stream sockets. NFSD finishes its READ and WRITE use of rq_bvec before svc_send() runs, so the two uses do not overlap. rq_bvec is private to the thread, so convert rq_res before taking xpt_mutex.

---
 include/linux/sunrpc/svc.h |  2 ++
 net/sunrpc/svcsock.c       | 40 ++++++++++++++++++----------------------
 2 files changed, 20 insertions(+), 22 deletions(-)
include/linux/sunrpc/svc.h +2 -0
index 497f56cdf7b5..47545866b41d 100644
@@ -232,6 +232,8 @@ struct svc_rqst {
 	struct page *		*rq_page_end;  /* one past the last reply page */
 
 	struct folio_batch	rq_fbatch;
+
+	/* Scratch array: ->xpo_recvfrom and ->xpo_sendto overwrite it */
 	struct bio_vec		*rq_bvec;
 
 	__be32			rq_xid;		/* transmission id */
net/sunrpc/svcsock.c +18 -22
index fd2f62652b93..a88ca9b9afd6 100644
@@ -1342,42 +1342,36 @@ static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
 	return 0;	/* record not complete */
 }
 
+#define SVC_TCP_MARKER_BVECS	1
+
 /*
  * MSG_SPLICE_PAGES is used exclusively to reduce the number of
  * copy operations in this path. Therefore the caller must ensure
- * that the pages backing @xdr are unchanging.
+ * that the pages backing rq_res are unchanging.
  */
 static int svc_tcp_sendmsg(struct svc_sock *svsk, struct svc_rqst *rqstp,
-			   rpc_fraghdr marker)
+			   rpc_fraghdr marker, unsigned int count)
 {
 	struct msghdr msg = {
 		.msg_flags	= MSG_SPLICE_PAGES,
 	};
-	int count;
 	void *buf;
 	int ret;
 
 	/* The stream record marker is copied into a temporary page
-	 * fragment buffer so that it can be included in sk_bvec.
+	 * fragment buffer so that it can be included in rq_bvec.
 	 */
 	buf = page_frag_alloc(&svsk->sk_frag_cache, sizeof(marker),
 			      GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 	memcpy(buf, &marker, sizeof(marker));
-	bvec_set_virt(svsk->sk_bvec, buf, sizeof(marker));
+	bvec_set_virt(rqstp->rq_bvec, buf, sizeof(marker));
 
-	count = xdr_buf_to_bvec(svsk->sk_bvec + 1, rqstp->rq_maxpages,
-				&rqstp->rq_res);
-	if (count < 0) {
-		ret = count;
-		goto out;
-	}
-
-	iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, svsk->sk_bvec,
-		      1 + count, sizeof(marker) + rqstp->rq_res.len);
+	iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, rqstp->rq_bvec,
+		      SVC_TCP_MARKER_BVECS + count,
+		      sizeof(marker) + rqstp->rq_res.len);
 	ret = sock_sendmsg(svsk->sk_sock, &msg);
-out:
 	page_frag_free(buf);
 	return ret;
 }
@@ -1398,15 +1392,20 @@ static int svc_tcp_sendto(struct svc_rqst *rqstp)
 	struct xdr_buf *xdr = &rqstp->rq_res;
 	rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
 					 (u32)xdr->len);
-	int sent;
+	int count, sent;
 
 	svc_tcp_release_ctxt(xprt, rqstp->rq_xprt_ctxt);
 	rqstp->rq_xprt_ctxt = NULL;
 
+	BUILD_BUG_ON(SVC_TCP_MARKER_BVECS > RPCSVC_BVEC_HEADROOM);
+	count = svc_rqst_res_to_bvec(rqstp, SVC_TCP_MARKER_BVECS);
+
 	mutex_lock(&xprt->xpt_mutex);
 	if (svc_xprt_is_dead(xprt))
 		goto out_notconn;
-	sent = svc_tcp_sendmsg(svsk, rqstp, marker);
+	sent = count;
+	if (count >= 0)
+		sent = svc_tcp_sendmsg(svsk, rqstp, marker, count);
 	trace_svcsock_tcp_send(xprt, sent);
 	if (sent < 0 || sent != (xdr->len + sizeof(marker)))
 		goto out_close;
@@ -1520,13 +1519,10 @@ void svc_sock_update_bufs(struct svc_serv *serv)
 	spin_unlock_bh(&serv->sv_lock);
 }
 
-static int svc_sock_sendpages(struct svc_serv *serv, struct socket *sock, int flags)
+static int svc_sock_sendpages(struct socket *sock)
 {
 	switch (sock->type) {
 	case SOCK_STREAM:
-		/* +1 for TCP record marker */
-		if (flags & SVC_SOCK_TEMPORARY)
-			return svc_serv_maxpages(serv) + 1;
 		return 0;
 	case SOCK_DGRAM:
 		return SUNRPC_MAX_UDP_SENDPAGES;
@@ -1547,7 +1543,7 @@ static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
 	int		sendpages;
 	unsigned long	pages;
 
-	sendpages = svc_sock_sendpages(serv, sock, flags);
+	sendpages = svc_sock_sendpages(sock);
 	if (sendpages < 0)
 		return ERR_PTR(sendpages);
 
Signature · 1 line
2.56.0
author maintainer
#4 lore raw

PATCH 4/8 SUNRPC: Send UDP replies from rq_bvec

In reply to Chuck Lever

svc_udp_sendto() is the last user of the per-socket sk_bvec array. Once UDP replies are sent from rq_bvec, sk_bvec can be removed.

Convert rq_res into rq_bvec before taking xpt_mutex, as svc_tcp_sendto() does. A UDP reply is bounded by RPCSVC_MAXPAYLOAD_UDP, so rq_bvec has room for the reply.

A datagram goes out in one sendmsg, so drop the kernel-doc sentence about xpt_mutex.

---
 net/sunrpc/svcsock.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
net/sunrpc/svcsock.c +5 -8
index a88ca9b9afd6..77a57bb74235 100644
@@ -681,16 +681,12 @@ static int svc_udp_recvfrom(struct svc_rqst *rqstp)
  * svc_udp_sendto - Send out a reply on a UDP socket
  * @rqstp: completed svc_rqst
  *
- * xpt_mutex ensures @rqstp's whole message is written to the socket
- * without interruption.
- *
- * Returns the number of bytes sent, or a negative errno.
+ * Return: the number of bytes sent, or a negative errno.
  */
 static int svc_udp_sendto(struct svc_rqst *rqstp)
 {
 	struct svc_xprt *xprt = rqstp->rq_xprt;
 	struct svc_sock	*svsk = container_of(xprt, struct svc_sock, sk_xprt);
-	struct xdr_buf *xdr = &rqstp->rq_res;
 	union {
 		struct cmsghdr	hdr;
 		long		all[SVC_PKTINFO_SPACE / sizeof(long)];
@@ -711,23 +707,24 @@ static int svc_udp_sendto(struct svc_rqst *rqstp)
 
 	svc_set_cmsg_data(rqstp, cmh);
 
+	count = svc_rqst_res_to_bvec(rqstp, 0);
+
 	mutex_lock(&xprt->xpt_mutex);
 
 	if (svc_xprt_is_dead(xprt))
 		goto out_notconn;
 
-	count = xdr_buf_to_bvec(svsk->sk_bvec, SUNRPC_MAX_UDP_SENDPAGES, xdr);
 	if (count < 0) {
 		err = count;
 		goto out_trace;
 	}
 
-	iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, svsk->sk_bvec,
+	iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, rqstp->rq_bvec,
 		      count, rqstp->rq_res.len);
 	err = sock_sendmsg(svsk->sk_sock, &msg);
 	if (err == -ECONNREFUSED) {
 		/* ICMP error on earlier request. */
-		iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, svsk->sk_bvec,
+		iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, rqstp->rq_bvec,
 			      count, rqstp->rq_res.len);
 		err = sock_sendmsg(svsk->sk_sock, &msg);
 	}
Signature · 1 line
2.56.0
author maintainer
#5 lore raw

PATCH 5/8 SUNRPC: Remove svc_sock::sk_bvec

In reply to Chuck Lever

Both socket transports send from the per-thread rq_bvec, so sk_bvec, its sizing in svc_sock_sendpages(), and SUNRPC_MAX_UDP_SENDPAGES have no remaining readers.

Remove them. The default case of svc_sock_sendpages() was the only check of sock->type on the svc_addsock() path. Without that check a SOCK_RAW socket with IPPROTO_TCP or IPPROTO_UDP reaches svc_tcp_init(). Check the type in svc_addsock() instead, after the -EISCONN and -EBUSY tests so those errors keep precedence over -EINVAL. The other callers of svc_setup_socket() pass only SOCK_STREAM or SOCK_DGRAM sockets.

Move the "For sends" comment to sk_frag_cache, which svc_tcp_sendmsg() allocates from under xpt_mutex.

---
 include/linux/sunrpc/svcsock.h |  4 +---
 net/sunrpc/svcsock.c           | 40 +++-------------------------------------
 2 files changed, 4 insertions(+), 40 deletions(-)
include/linux/sunrpc/svcsock.h +1 -3
index 372a00882ca6..7bb7781d038a 100644
@@ -26,9 +26,6 @@ struct svc_sock {
 	void			(*sk_odata)(struct sock *);
 	void			(*sk_owspace)(struct sock *);
 
-	/* For sends (protected by xpt_mutex) */
-	struct bio_vec		*sk_bvec;
-
 	/* private TCP part */
 	/* On-the-wire fragment header: */
 	__be32			sk_marker;
@@ -39,6 +36,7 @@ struct svc_sock {
 	 * received so far in the fragments making up this rpc: */
 	u32			sk_datalen;
 
+	/* For sends (protected by xpt_mutex) */
 	struct page_frag_cache  sk_frag_cache;
 
 	struct completion	sk_handshake_done;
net/sunrpc/svcsock.c +3 -37
index 77a57bb74235..8dc7921a5b91 100644
@@ -59,17 +59,6 @@
 
 #define RPCDBG_FACILITY	RPCDBG_SVCXPRT
 
-/*
- * For UDP:
- * 1 for header page
- * enough pages for RPCSVC_MAXPAYLOAD_UDP
- * 1 in case payload is not aligned
- * 1 for tail page
- */
-enum {
-	SUNRPC_MAX_UDP_SENDPAGES = 1 + RPCSVC_MAXPAYLOAD_UDP / PAGE_SIZE + 1 + 1
-};
-
 /* To-do: to avoid tying up an nfsd thread while waiting for a
  * handshake request, the request could instead be deferred.
  */
@@ -1516,17 +1505,6 @@ void svc_sock_update_bufs(struct svc_serv *serv)
 	spin_unlock_bh(&serv->sv_lock);
 }
 
-static int svc_sock_sendpages(struct socket *sock)
-{
-	switch (sock->type) {
-	case SOCK_STREAM:
-		return 0;
-	case SOCK_DGRAM:
-		return SUNRPC_MAX_UDP_SENDPAGES;
-	}
-	return -EINVAL;
-}
-
 /*
  * Initialize socket for RPC use and create svc_sock struct
  */
@@ -1537,26 +1515,13 @@ static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
 	struct svc_sock	*svsk;
 	struct sock	*inet;
 	int		pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
-	int		sendpages;
 	unsigned long	pages;
 
-	sendpages = svc_sock_sendpages(sock);
-	if (sendpages < 0)
-		return ERR_PTR(sendpages);
-
 	pages = svc_serv_maxpages(serv);
 	svsk = kzalloc_flex(*svsk, sk_pages, pages);
 	if (!svsk)
 		return ERR_PTR(-ENOMEM);
 
-	if (sendpages) {
-		svsk->sk_bvec = kzalloc_objs(*svsk->sk_bvec, sendpages);
-		if (!svsk->sk_bvec) {
-			kfree(svsk);
-			return ERR_PTR(-ENOMEM);
-		}
-	}
-
 	svsk->sk_maxpages = pages;
 
 	inet = sock->sk;
@@ -1568,7 +1533,6 @@ static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
 				     inet->sk_protocol,
 				     ntohs(inet_sk(inet)->inet_sport));
 		if (err < 0) {
-			kfree(svsk->sk_bvec);
 			kfree(svsk);
 			return ERR_PTR(err);
 		}
@@ -1638,6 +1602,9 @@ int svc_addsock(struct svc_serv *serv, struct net *net, const int fd,
 	err = -EBUSY;
 	if (so->sk->sk_user_data)
 		goto out;
+	err = -EINVAL;
+	if (so->type != SOCK_STREAM && so->type != SOCK_DGRAM)
+		goto out;
 	err = -ENOENT;
 	if (!try_module_get(THIS_MODULE))
 		goto out;
@@ -1789,6 +1756,5 @@ static void svc_sock_free(struct svc_xprt *xprt)
 		sock_release(sock);
 
 	page_frag_cache_drain(&svsk->sk_frag_cache);
-	kfree(svsk->sk_bvec);
 	kfree(svsk);
 }
Signature · 1 line
2.56.0
author maintainer
#6 lore raw

PATCH 6/8 SUNRPC: Add svc_rqst_set_arg_len()

In reply to Chuck Lever

The trim that fits rq_arg to a received length is open-coded in svc_tcp_recvfrom(), svc_udp_recvfrom(), and svc_process_bc(), which also clamps an oversized length. A receive path hoisted out of the transports needs the trim in one place, and a transport that can receive more than rq_arg holds needs to learn that the clamp fired.

Factor the three into svc_rqst_set_arg_len(), which returns false when it clamps. svc_tcp_recv_actor() rejects a record larger than sv_max_mesg, and rq_arg holds more than sv_max_mesg. A clamp in svc_tcp_recvfrom() means the receive accounting is broken. Warn once and close the connection.

The helper zeroes page_base, which the backchannel site did not, but no backchannel transport sets page_base in rq_rcv_buf. Add KUnit cases for the clamp, which no runtime test reaches.

---
 include/linux/sunrpc/svc.h   |  2 +
 net/sunrpc/svc.c             | 13 +------
 net/sunrpc/svc_xprt.c        | 32 ++++++++++++++++
 net/sunrpc/svcsock.c         | 20 +++-------
 net/sunrpc/tests/xdr_kunit.c | 88 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 128 insertions(+), 27 deletions(-)
include/linux/sunrpc/svc.h +2 -0
index 47545866b41d..9f2df8d02169 100644
@@ -471,6 +471,8 @@ bool		   svc_rqst_replace_page(struct svc_rqst *rqstp,
 void		   svc_rqst_release_pages(struct svc_rqst *rqstp);
 int		   svc_rqst_res_to_bvec(struct svc_rqst *rqstp,
 					unsigned int skip);
+bool		   svc_rqst_set_arg_len(struct svc_rqst *rqstp,
+					unsigned int len);
 int		   svc_new_thread(struct svc_serv *serv, struct svc_pool *pool);
 void		   svc_exit_thread(struct svc_rqst *);
 struct svc_serv *  svc_create_pooled(struct svc_program *prog,
net/sunrpc/svc.c +1 -12
index b61edc49b253..6be2020a4907 100644
@@ -1809,18 +1809,7 @@ void svc_process_bc(struct rpc_rqst *req, struct svc_rqst *rqstp)
 	memcpy(&rqstp->rq_arg, &req->rq_rcv_buf, sizeof(rqstp->rq_arg));
 	memcpy(&rqstp->rq_res, &req->rq_snd_buf, sizeof(rqstp->rq_res));
 
-	/* Adjust the argument buffer length */
-	rqstp->rq_arg.len = req->rq_private_buf.len;
-	if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
-		rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
-		rqstp->rq_arg.page_len = 0;
-	} else if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len +
-			rqstp->rq_arg.page_len)
-		rqstp->rq_arg.page_len = rqstp->rq_arg.len -
-			rqstp->rq_arg.head[0].iov_len;
-	else
-		rqstp->rq_arg.len = rqstp->rq_arg.head[0].iov_len +
-			rqstp->rq_arg.page_len;
+	svc_rqst_set_arg_len(rqstp, req->rq_private_buf.len);
 
 	/* Reset the response buffer */
 	rqstp->rq_res.head[0].iov_len = 0;
net/sunrpc/svc_xprt.c +32 -0
index bcc867592f2d..99b2e0aa80aa 100644
@@ -765,6 +765,38 @@ static bool svc_alloc_arg(struct svc_rqst *rqstp)
 	return true;
 }
 
+/**
+ * svc_rqst_set_arg_len - Trim the Call buffer to a received length
+ * @rqstp: RPC transaction context
+ * @len: count of octets received into rq_arg
+ *
+ * On entry head[0].iov_len and page_len give the capacity of rq_arg. On return
+ * page_base is zero. The head and page buffers are not read or written, so the
+ * helper works whatever memory backs rq_arg.
+ *
+ * Return: %true when @len fits, %false when it exceeds the capacity
+ * and rq_arg was clamped to the capacity instead.
+ */
+bool svc_rqst_set_arg_len(struct svc_rqst *rqstp, unsigned int len)
+{
+	struct xdr_buf *arg = &rqstp->rq_arg;
+	unsigned int capacity = arg->head[0].iov_len + arg->page_len;
+	bool fits = len <= capacity;
+
+	if (!fits)
+		len = capacity;
+	arg->len = len;
+	arg->page_base = 0;
+	if (len <= arg->head[0].iov_len) {
+		arg->head[0].iov_len = len;
+		arg->page_len = 0;
+	} else {
+		arg->page_len = len - arg->head[0].iov_len;
+	}
+	return fits;
+}
+EXPORT_SYMBOL_GPL(svc_rqst_set_arg_len);
+
 static bool
 svc_thread_should_sleep(struct svc_rqst *rqstp)
 {
net/sunrpc/svcsock.c +5 -15
index 8dc7921a5b91..d95ab14dea89 100644
@@ -606,7 +606,6 @@ static int svc_udp_recvfrom(struct svc_rqst *rqstp)
 	set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
 
 	len = skb->len;
-	rqstp->rq_arg.len = len;
 	trace_svcsock_udp_recv(&svsk->sk_xprt, len);
 
 	rqstp->rq_prot = IPPROTO_UDP;
@@ -631,13 +630,7 @@ static int svc_udp_recvfrom(struct svc_rqst *rqstp)
 		rqstp->rq_xprt_ctxt = skb;
 	}
 
-	rqstp->rq_arg.page_base = 0;
-	if (len <= rqstp->rq_arg.head[0].iov_len) {
-		rqstp->rq_arg.head[0].iov_len = len;
-		rqstp->rq_arg.page_len = 0;
-	} else {
-		rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
-	}
+	svc_rqst_set_arg_len(rqstp, len);
 
 	if (serv->sv_stats)
 		serv->sv_stats->netudpcnt++;
@@ -1263,13 +1256,10 @@ static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
 
 	svc_tcp_flush_pages(svsk, rqstp);
 
-	rqstp->rq_arg.len = svsk->sk_datalen;
-	rqstp->rq_arg.page_base = 0;
-	if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
-		rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
-		rqstp->rq_arg.page_len = 0;
-	} else
-		rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
+	if (WARN_ON_ONCE(!svc_rqst_set_arg_len(rqstp, svsk->sk_datalen))) {
+		len = -EMSGSIZE;
+		goto err_nuts;
+	}
 
 	rqstp->rq_xprt_ctxt   = NULL;
 	rqstp->rq_prot	      = IPPROTO_TCP;
net/sunrpc/tests/xdr_kunit.c +88 -0

100 lines not on this page · raw message

Signature · 1 line
2.56.0
author maintainer
#7 lore raw

PATCH 7/8 SUNRPC: Move the receive bvec iterator setup out of svcsock

In reply to Chuck Lever

svc_tcp_recv_iter_init() and svc_tcp_flush_pages() map a range of rq_pages into rq_bvec and flush the received pages. Neither function depends on TCP, and svc_tcp_flush_pages() uses struct svc_sock only to read sk_datalen. A UDP receive through a bvec iterator needs the same two steps.

Move the two functions to svc_xprt.c as svc_rqst_arg_iter_init() and svc_rqst_arg_flush(). TCP bounds the record at sv_max_mesg, but a transport outside svcsock does not, so have the iterator setup reject a range that does not fit in rq_pages.

The TCP actor reports the rejection through desc->error with desc->count zeroed, as for a copy fault. The errno is -EMSGSIZE, as for an oversized fragment, so that trace_svcsock_tcp_recv_err names the cause.

---
 include/linux/sunrpc/svc.h   |   4 ++
 net/sunrpc/svc_xprt.c        |  55 +++++++++++++++++++++++
 net/sunrpc/svcsock.c         |  51 ++++++----------------
 net/sunrpc/tests/xdr_kunit.c | 101 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 173 insertions(+), 38 deletions(-)
include/linux/sunrpc/svc.h +4 -0
index 9f2df8d02169..51ed435c32ce 100644
@@ -471,6 +471,10 @@ bool		   svc_rqst_replace_page(struct svc_rqst *rqstp,
 void		   svc_rqst_release_pages(struct svc_rqst *rqstp);
 int		   svc_rqst_res_to_bvec(struct svc_rqst *rqstp,
 					unsigned int skip);
+int		   svc_rqst_arg_iter_init(struct svc_rqst *rqstp,
+					  struct iov_iter *iter,
+					  size_t offset, size_t len);
+void		   svc_rqst_arg_flush(struct svc_rqst *rqstp, size_t len);
 bool		   svc_rqst_set_arg_len(struct svc_rqst *rqstp,
 					unsigned int len);
 int		   svc_new_thread(struct svc_serv *serv, struct svc_pool *pool);
net/sunrpc/svc_xprt.c +55 -0

67 lines not on this page · raw message

net/sunrpc/svcsock.c +13 -38

81 lines not on this page · raw message

net/sunrpc/tests/xdr_kunit.c +101 -0

119 lines not on this page · raw message

Signature · 1 line
2.56.0
author maintainer
#8 lore raw

PATCH 8/8 SUNRPC: Receive UDP datagrams into rq_pages through a bvec iterator

In reply to Chuck Lever

svc_udp_recvfrom() copies a nonlinear skb into rq_arg through csum_partial_copy_to_xdr() but aliases a linear one: rq_arg.head points into the skb, and the skb rides in rq_xprt_ctxt until the reply is sent or the deferred request is revisited. The alias is the one case that keeps svcsock from handing every receive buffer around as a bvec array, and the last UDP code that cannot share the receive helpers with TCP.

Copy every datagram into rq_pages through an ITER_DEST bvec iterator. A datagram larger than the capacity svc_alloc_arg() left in rq_arg is dropped before the copy and reported through the svcsock_udp_recv_err tracepoint with -EMSGSIZE. Lockd's buffer is near 16 KB and no conformant NLM Call exceeds 5 KB. With rq_xprt_ctxt unused, both transports share one empty svc_sock_release_ctxt().

skb_copy_datagram_iter() and skb_copy_and_csum_datagram_msg() replace the two branches of csum_partial_copy_to_xdr(), except that a checksum fault is reported without the receiving device's name. The local_bh_disable() around the copy goes, because udp_recvmsg() calls the same helpers in process context.

The alias set head[0].iov_len to the datagram length, so svc_defer() accepted any linear datagram. After this patch the head is capped at PAGE_SIZE, and a request over one page whose upcall outlasts cache_defer_req()'s inline wait is dropped for the client to retransmit, as a nonlinear datagram already is. In an NFSv3 run over an igb NIC with a 1500-byte MTU, svc_udp_recvfrom() received 4721 datagrams and none was both linear and over one page. All 2224 nonlinear datagrams were reassembled from IP fragments. At that MTU a datagram over one page is always fragmented, so the patch does not change which requests svc_defer() drops.

The copy costs at most one MTU of data per small request, and a copying checksum replaces skb_checksum_complete() on linear skbs. The effect on NFSv3 NULL and GETATTR rates has not been measured.

---
 net/sunrpc/svcsock.c | 57 ++++++++++++++++------------------------------------
 1 file changed, 17 insertions(+), 40 deletions(-)
net/sunrpc/svcsock.c +17 -40
index 4c33a1d95594..1586c8f3c78f 100644
@@ -53,7 +53,6 @@
 #include <trace/events/sock.h>
 #include <trace/events/sunrpc.h>
 
-#include "socklib.h"
 #include "sunrpc.h"
 
 #define RPCDBG_FACILITY	RPCDBG_SVCXPRT
@@ -113,29 +112,15 @@ static void svc_reclassify_socket(struct socket *sock)
 #endif
 
 /**
- * svc_tcp_release_ctxt - Release transport-related resources
+ * svc_sock_release_ctxt - Release transport-related resources
  * @xprt: the transport which owned the context
  * @ctxt: the context from rqstp->rq_xprt_ctxt or dr->xprt_ctxt
  *
  */
-static void svc_tcp_release_ctxt(struct svc_xprt *xprt, void *ctxt)
+static void svc_sock_release_ctxt(struct svc_xprt *xprt, void *ctxt)
 {
 }
 
-/**
- * svc_udp_release_ctxt - Release transport-related resources
- * @xprt: the transport which owned the context
- * @ctxt: the context from rqstp->rq_xprt_ctxt or dr->xprt_ctxt
- *
- */
-static void svc_udp_release_ctxt(struct svc_xprt *xprt, void *ctxt)
-{
-	struct sk_buff *skb = ctxt;
-
-	if (skb)
-		consume_skb(skb);
-}
-
 union svc_pktinfo_u {
 	struct in_pktinfo pkti;
 	struct in6_pktinfo pkti6;
@@ -613,22 +598,20 @@ static int svc_udp_recvfrom(struct svc_rqst *rqstp)
 		goto out_cmsg_err;
 	rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
 
-	if (skb_is_nonlinear(skb)) {
-		/* we have to copy */
-		local_bh_disable();
-		if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb))
-			goto out_bh_enable;
-		local_bh_enable();
-		consume_skb(skb);
-	} else {
-		/* we can use it in-place */
-		rqstp->rq_arg.head[0].iov_base = skb->data;
-		rqstp->rq_arg.head[0].iov_len = len;
-		if (skb_checksum_complete(skb))
-			goto out_free;
-		rqstp->rq_xprt_ctxt = skb;
+	if (len > rqstp->rq_arg.head[0].iov_len + rqstp->rq_arg.page_len ||
+	    svc_rqst_arg_iter_init(rqstp, &msg.msg_iter, 0, len)) {
+		trace_svcsock_udp_recv_err(&svsk->sk_xprt, -EMSGSIZE);
+		goto out_free;
 	}
+	if (skb_csum_unnecessary(skb))
+		err = skb_copy_datagram_iter(skb, 0, &msg.msg_iter, len);
+	else
+		err = skb_copy_and_csum_datagram_msg(skb, 0, &msg);
+	if (err)
+		goto out_free;
+	consume_skb(skb);
 
+	svc_rqst_arg_flush(rqstp, len);
 	svc_rqst_set_arg_len(rqstp, len);
 
 	if (serv->sv_stats)
@@ -648,9 +631,6 @@ static int svc_udp_recvfrom(struct svc_rqst *rqstp)
 out_cmsg_err:
 	net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
 			     cmh->cmsg_level, cmh->cmsg_type);
-	goto out_free;
-out_bh_enable:
-	local_bh_enable();
 out_free:
 	kfree_skb(skb);
 out_clear_busy:
@@ -683,9 +663,6 @@ static int svc_udp_sendto(struct svc_rqst *rqstp)
 	int count;
 	int err;
 
-	svc_udp_release_ctxt(xprt, rqstp->rq_xprt_ctxt);
-	rqstp->rq_xprt_ctxt = NULL;
-
 	svc_set_cmsg_data(rqstp, cmh);
 
 	count = svc_rqst_res_to_bvec(rqstp, 0);
@@ -762,7 +739,7 @@ static const struct svc_xprt_ops svc_udp_ops = {
 	.xpo_recvfrom = svc_udp_recvfrom,
 	.xpo_sendto = svc_udp_sendto,
 	.xpo_result_payload = svc_sock_result_payload,
-	.xpo_release_ctxt = svc_udp_release_ctxt,
+	.xpo_release_ctxt = svc_sock_release_ctxt,
 	.xpo_detach = svc_sock_detach,
 	.xpo_free = svc_sock_free,
 	.xpo_has_wspace = svc_udp_has_wspace,
@@ -1345,7 +1322,7 @@ static int svc_tcp_sendto(struct svc_rqst *rqstp)
 					 (u32)xdr->len);
 	int count, sent;
 
-	svc_tcp_release_ctxt(xprt, rqstp->rq_xprt_ctxt);
+	svc_sock_release_ctxt(xprt, rqstp->rq_xprt_ctxt);
 	rqstp->rq_xprt_ctxt = NULL;
 
 	BUILD_BUG_ON(SVC_TCP_MARKER_BVECS > RPCSVC_BVEC_HEADROOM);
@@ -1389,7 +1366,7 @@ static const struct svc_xprt_ops svc_tcp_ops = {
 	.xpo_recvfrom = svc_tcp_recvfrom,
 	.xpo_sendto = svc_tcp_sendto,
 	.xpo_result_payload = svc_sock_result_payload,
-	.xpo_release_ctxt = svc_tcp_release_ctxt,
+	.xpo_release_ctxt = svc_sock_release_ctxt,
 	.xpo_detach = svc_tcp_sock_detach,
 	.xpo_free = svc_sock_free,
 	.xpo_has_wspace = svc_tcp_has_wspace,
Signature · 1 line
2.56.0