[RFC PATCH v2 41/48] sunrpc: Rely on TCP sendmsg + MSG_SPLICE_PAGES to copy unspliceable data

David Howells
2023-03-29T14:22:32Z

Rather than copying data in svc_tcp_sendmsg() into page fragments, just
hand in ITER_KVEC iterators as part of the ITER_ITERLIST and rely on TCP to
copy them if the pages they're residing on are belong to the slab or have a
zero refcount.

Signed-off-by: David Howells <dhowells@redhat.com>
cc: Trond Myklebust <trond.myklebust@hammerspace.com>
cc: Anna Schumaker <anna@kernel.org>
cc: Chuck Lever <chuck.lever@oracle.com>
cc: Jeff Layton <jlayton@kernel.org>
cc: "David S. Miller" <davem@davemloft.net>
cc: Eric Dumazet <edumazet@google.com>
cc: Jakub Kicinski <kuba@kernel.org>
cc: Paolo Abeni <pabeni@redhat.com>
cc: Jens Axboe <axboe@kernel.dk>
cc: Matthew Wilcox <willy@infradead.org>
cc: linux-nfs@vger.kernel.org
cc: netdev@vger.kernel.org
---
net/sunrpc/svcsock.c | 44 ++++++++++++--------------------------------
1 file changed, 12 insertions(+), 32 deletions(-)

diff --git a/net/sunrpc/svcsock.c b/net/sunrpc/svcsock.c
index f1cc53aad6e0..c1421f6fe57a 100644
--- a/net/sunrpc/svcsock.c
+++ b/net/sunrpc/svcsock.c
@@ -1071,47 +1071,27 @@ static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
static int svc_tcp_sendmsg(struct socket *sock, struct xdr_buf *xdr,
rpc_fraghdr marker, unsigned int *sentp)
{
- const struct kvec *head = xdr->head;
- const struct kvec *tail = xdr->tail;
- struct iov_iter iters[3];
- struct bio_vec head_bv, tail_bv;
- struct msghdr msg = {
- .msg_flags = MSG_SPLICE_PAGES,
- };
- void *m, *t;
- int ret, n = 2, size;
+ struct iov_iter iters[4];
+ struct kvec marker_kv;
+ struct msghdr msg = { .msg_flags = MSG_SPLICE_PAGES, };
+ int ret, n = 0, size;

*sentp = 0;
ret = xdr_alloc_bvec(xdr, GFP_KERNEL);
if (ret < 0)
return ret;

- m = page_frag_alloc(NULL, sizeof(marker) + head->iov_len + tail->iov_len,
- GFP_KERNEL);
- if (!m)
- return -ENOMEM;
-
- memcpy(m, &marker, sizeof(marker));
- if (head->iov_len)
- memcpy(m + sizeof(marker), head->iov_base, head->iov_len);
- bvec_set_virt(&head_bv, m, sizeof(marker) + head->iov_len);
- iov_iter_bvec(&iters[0], ITER_SOURCE, &head_bv, 1,
- sizeof(marker) + head->iov_len);
-
- iov_iter_bvec(&iters[1], ITER_SOURCE, xdr->bvec,
+ marker_kv.iov_base = &marker;
+ marker_kv.iov_len = sizeof(marker);
+ iov_iter_kvec(&iters[n++], ITER_SOURCE, &marker_kv, 1, sizeof(marker));
+ iov_iter_kvec(&iters[n++], ITER_SOURCE, xdr->head, 1, xdr->head->iov_len);
+ iov_iter_bvec(&iters[n++], ITER_SOURCE, xdr->bvec,
xdr_buf_pagecount(xdr), xdr->page_len);

- if (tail->iov_len) {
- t = page_frag_alloc(NULL, tail->iov_len, GFP_KERNEL);
- if (!t)
- return -ENOMEM;
- memcpy(t, tail->iov_base, tail->iov_len);
- bvec_set_virt(&tail_bv, t, tail->iov_len);
- iov_iter_bvec(&iters[2], ITER_SOURCE, &tail_bv, 1, tail->iov_len);
- n++;
- }
+ if (xdr->tail->iov_len)
+ iov_iter_kvec(&iters[n++], ITER_SOURCE, xdr->tail, 1, xdr->tail->iov_len);

- size = sizeof(marker) + head->iov_len + xdr->page_len + tail->iov_len;
+ size = sizeof(marker) + xdr->head->iov_len + xdr->page_len + xdr->tail->iov_len;
iov_iter_iterlist(&msg.msg_iter, ITER_SOURCE, iters, n, size);

ret = sock_sendmsg(sock, &msg);