From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0483C76188 for ; Wed, 5 Apr 2023 16:55:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233536AbjDEQzW (ORCPT ); Wed, 5 Apr 2023 12:55:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229642AbjDEQzU (ORCPT ); Wed, 5 Apr 2023 12:55:20 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA428359D for ; Wed, 5 Apr 2023 09:53:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713632; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TEbJfA2XN3YS2bVTA2W3okmrsaJKbACj3JMzRKlRxcU=; b=T569Mfc+Z8nUc7pSaIN/Il3eDgjWbgv8XGTxdLgaj1fRmFRZF9t9pWtVVNRP/uYHYbjvu2 xqacVqcIqc0x96eRdejCyQb8YJl9+gkRPpZexVydgs+ghvfle6ZCIEvo9oDYNglLDJfGDI u63NmPavyZCH73GhW2nevnVgk50iRSI= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-587-Ma9Js4ofNX2CEUj3eZmXIg-1; Wed, 05 Apr 2023 12:53:48 -0400 X-MC-Unique: Ma9Js4ofNX2CEUj3eZmXIg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id ADC8B3823A04; Wed, 5 Apr 2023 16:53:47 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2CAB2202701F; Wed, 5 Apr 2023 16:53:45 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Boris Pismenny , John Fastabend , Herbert Xu Subject: [PATCH net-next v4 01/20] net: Add samples for network I/O and splicing Date: Wed, 5 Apr 2023 17:53:20 +0100 Message-Id: <20230405165339.3468808-2-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add some small sample programs for doing network I/O including splicing. There are three IPv4/IPv6 servers: tcp-sink, tls-sink and udp-sink. They can be given a port number by passing "-p " and will listen on an IPv6 socket unless given a "-4" flag, in which case they'll listen for IPv4 only. There are three IPv4/IPv6 clients: tcp-send, tls-send and udp-send. They are given a file to get data from (or "-" for stdin) and the name of a server to talk to. They can also be given a port number by passing "-p ", "-4" or "-6" to force the use of IPv4 or IPv6, "-s" to indicate they should use splice/sendfile to transfer the data and "-z" to specify how much data to copy. If "-s" is given, the input will be spliced if it's a pipe and sendfiled otherwise. A driver program, splice-out, is provided to splice data from a file/stdin to stdout and can be used to pipe into the aforementioned clients for testing splice. This takes the name of the file to splice from (or "-" for stdin). It can also be given "-w " to indicate the maximum size of each splice, "-k " if a chunk of the input should be skipped between splices to prevent coalescence and "-s" if sendfile should be used instead of splice. Additionally, there is an AF_UNIX client and server. These are similar to the IPv[46] programs, except both take a socket path and there is no option to change the port number. And then there are two AF_ALG clients (there is no server). These are similar to the other clients, except no destination is specified. One exercised skcipher encryption and the other hashing. Examples include: ./splice-out -w0x400 /foo/16K 4K | ./alg-encrypt -s - ./splice-out -w0x400 /foo/1M | ./unix-send -s - /tmp/foo ./splice-out -w0x400 /foo/16K 16K -w1 | ./tls-send -s6 -z16K - servbox ./tcp-send /bin/ls 192.168.6.1 ./udp-send -4 -p5555 /foo/4K localhost where, for example, /foo/16K is a 16KiB file. Signed-off-by: David Howells cc: Willem de Bruijn cc: Boris Pismenny cc: John Fastabend cc: Herbert Xu cc: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: netdev@vger.kernel.org --- samples/Kconfig | 6 ++ samples/Makefile | 1 + samples/net/Makefile | 13 +++ samples/net/alg-encrypt.c | 201 ++++++++++++++++++++++++++++++++++++++ samples/net/alg-hash.c | 143 +++++++++++++++++++++++++++ samples/net/splice-out.c | 142 +++++++++++++++++++++++++++ samples/net/tcp-send.c | 154 +++++++++++++++++++++++++++++ samples/net/tcp-sink.c | 76 ++++++++++++++ samples/net/tls-send.c | 176 +++++++++++++++++++++++++++++++++ samples/net/tls-sink.c | 98 +++++++++++++++++++ samples/net/udp-send.c | 151 ++++++++++++++++++++++++++++ samples/net/udp-sink.c | 82 ++++++++++++++++ samples/net/unix-send.c | 147 ++++++++++++++++++++++++++++ samples/net/unix-sink.c | 51 ++++++++++ 14 files changed, 1441 insertions(+) create mode 100644 samples/net/Makefile create mode 100644 samples/net/alg-encrypt.c create mode 100644 samples/net/alg-hash.c create mode 100644 samples/net/splice-out.c create mode 100644 samples/net/tcp-send.c create mode 100644 samples/net/tcp-sink.c create mode 100644 samples/net/tls-send.c create mode 100644 samples/net/tls-sink.c create mode 100644 samples/net/udp-send.c create mode 100644 samples/net/udp-sink.c create mode 100644 samples/net/unix-send.c create mode 100644 samples/net/unix-sink.c diff --git a/samples/Kconfig b/samples/Kconfig index 30ef8bd48ba3..14051e9f7532 100644 --- a/samples/Kconfig +++ b/samples/Kconfig @@ -273,6 +273,12 @@ config SAMPLE_CORESIGHT_SYSCFG This demonstrates how a user may create their own CoreSight configurations and easily load them into the system at runtime. =20 +config SAMPLE_NET + bool "Build example programs that drive network protocols" + depends on NET + help + Build example userspace programs that drive network protocols. + source "samples/rust/Kconfig" =20 endif # SAMPLES diff --git a/samples/Makefile b/samples/Makefile index 7cb632ef88ee..22c1d6244eaf 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -37,3 +37,4 @@ obj-$(CONFIG_DEBUG_KMEMLEAK_TEST) +=3D kmemleak/ obj-$(CONFIG_SAMPLE_CORESIGHT_SYSCFG) +=3D coresight/ obj-$(CONFIG_SAMPLE_FPROBE) +=3D fprobe/ obj-$(CONFIG_SAMPLES_RUST) +=3D rust/ +obj-$(CONFIG_SAMPLE_NET) +=3D net/ diff --git a/samples/net/Makefile b/samples/net/Makefile new file mode 100644 index 000000000000..0ccd68a36edf --- /dev/null +++ b/samples/net/Makefile @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: GPL-2.0-only +userprogs-always-y +=3D \ + alg-hash \ + alg-encrypt \ + splice-out \ + tcp-send \ + tcp-sink \ + tls-send \ + tls-sink \ + udp-send \ + udp-sink \ + unix-send \ + unix-sink diff --git a/samples/net/alg-encrypt.c b/samples/net/alg-encrypt.c new file mode 100644 index 000000000000..34a62a9c480a --- /dev/null +++ b/samples/net/alg-encrypt.c @@ -0,0 +1,201 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* AF_ALG hash test + * + * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define OSERROR(X, Y) do { if ((long)(X) =3D=3D -1) { perror(Y); exit(1); = } } while(0) +#define min(x, y) ((x) < (y) ? (x) : (y)) + +static unsigned char buffer[4096 * 32] __attribute__((aligned(4096))); +static unsigned char iv[16]; +static unsigned char key[16]; + +static const struct sockaddr_alg sa =3D { + .salg_family =3D AF_ALG, + .salg_type =3D "skcipher", + .salg_name =3D "cbc(aes)", +}; + +static __attribute__((noreturn)) +void format(void) +{ + fprintf(stderr, "alg-send [-s] [-z] |-\n"); + exit(2); +} + +static void algif_add_set_op(struct msghdr *msg, unsigned int op) +{ + struct cmsghdr *__cmsg; + + __cmsg =3D msg->msg_control + msg->msg_controllen; + __cmsg->cmsg_len =3D CMSG_LEN(sizeof(unsigned int)); + __cmsg->cmsg_level =3D SOL_ALG; + __cmsg->cmsg_type =3D ALG_SET_OP; + *(unsigned int *)CMSG_DATA(__cmsg) =3D op; + msg->msg_controllen +=3D CMSG_ALIGN(__cmsg->cmsg_len); +} + +static void algif_add_set_iv(struct msghdr *msg, const void *iv, size_t iv= len) +{ + struct af_alg_iv *ivbuf; + struct cmsghdr *__cmsg; + + printf("%zx\n", msg->msg_controllen); + __cmsg =3D msg->msg_control + msg->msg_controllen; + __cmsg->cmsg_len =3D CMSG_LEN(sizeof(*ivbuf) + ivlen); + __cmsg->cmsg_level =3D SOL_ALG; + __cmsg->cmsg_type =3D ALG_SET_IV; + ivbuf =3D (struct af_alg_iv *)CMSG_DATA(__cmsg); + ivbuf->ivlen =3D ivlen; + memcpy(ivbuf->iv, iv, ivlen); + msg->msg_controllen +=3D CMSG_ALIGN(__cmsg->cmsg_len); +} + +int main(int argc, char *argv[]) +{ + struct msghdr msg; + struct stat st; + const char *filename; + unsigned char ctrl[4096]; + ssize_t r, w, o, ret; + size_t size =3D LONG_MAX, total =3D 0, i, out =3D 160; + char *end; + bool use_sendfile =3D false, all =3D true; + int opt, alg, sock, fd =3D 0; + + while ((opt =3D getopt(argc, argv, "sz:")) !=3D EOF) { + switch (opt) { + case 's': + use_sendfile =3D true; + break; + case 'z': + size =3D strtoul(optarg, &end, 0); + switch (*end) { + case 'K': + case 'k': + size *=3D 1024; + break; + case 'M': + case 'm': + size *=3D 1024 * 1024; + break; + } + all =3D false; + break; + default: + format(); + } + } + + argc -=3D optind; + argv +=3D optind; + if (argc !=3D 1) + format(); + filename =3D argv[0]; + + alg =3D socket(AF_ALG, SOCK_SEQPACKET, 0); + OSERROR(alg, "AF_ALG"); + OSERROR(bind(alg, (struct sockaddr *)&sa, sizeof(sa)), "bind"); + OSERROR(setsockopt(alg, SOL_ALG, ALG_SET_KEY, key, sizeof(key)), "ALG_SET= _KEY"); + sock =3D accept(alg, NULL, 0); + OSERROR(sock, "accept"); + + if (strcmp(filename, "-") !=3D 0) { + fd =3D open(filename, O_RDONLY); + OSERROR(fd, filename); + OSERROR(fstat(fd, &st), filename); + size =3D st.st_size; + } else { + OSERROR(fstat(fd, &st), argv[2]); + } + + memset(&msg, 0, sizeof(msg)); + msg.msg_control =3D ctrl; + algif_add_set_op(&msg, ALG_OP_ENCRYPT); + algif_add_set_iv(&msg, iv, sizeof(iv)); + + OSERROR(sendmsg(sock, &msg, MSG_MORE), "sock/sendmsg"); + + if (!use_sendfile) { + bool more =3D false; + + while (size) { + r =3D read(fd, buffer, sizeof(buffer)); + OSERROR(r, filename); + if (r =3D=3D 0) + break; + size -=3D r; + + o =3D 0; + do { + more =3D size > 0; + w =3D send(sock, buffer + o, r - o, + more ? MSG_MORE : 0); + OSERROR(w, "sock/send"); + total +=3D w; + o +=3D w; + } while (o < r); + } + + if (more) + send(sock, NULL, 0, 0); + } else if (S_ISFIFO(st.st_mode)) { + do { + r =3D splice(fd, NULL, sock, NULL, size, + size > 0 ? SPLICE_F_MORE : 0); + OSERROR(r, "sock/splice"); + size -=3D r; + total +=3D r; + } while (r > 0 && size > 0); + if (size && !all) { + fprintf(stderr, "Short splice\n"); + exit(1); + } + } else { + r =3D sendfile(sock, fd, NULL, size); + OSERROR(r, "sock/sendfile"); + if (r !=3D size) { + fprintf(stderr, "Short sendfile\n"); + exit(1); + } + total =3D r; + } + + while (total > 0) { + ret =3D read(sock, buffer, min(sizeof(buffer), total)); + OSERROR(ret, "sock/read"); + if (ret =3D=3D 0) + break; + total -=3D ret; + + if (out > 0) { + ret =3D min(out, ret); + out -=3D ret; + for (i =3D 0; i < ret; i++) + printf("%02x", (unsigned char)buffer[i]); + } + printf("...\n"); + } + + OSERROR(close(sock), "sock/close"); + OSERROR(close(alg), "alg/close"); + OSERROR(close(fd), "close"); + return 0; +} diff --git a/samples/net/alg-hash.c b/samples/net/alg-hash.c new file mode 100644 index 000000000000..842a8016acb3 --- /dev/null +++ b/samples/net/alg-hash.c @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* AF_ALG hash test + * + * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define OSERROR(X, Y) do { if ((long)(X) =3D=3D -1) { perror(Y); exit(1); = } } while(0) + +static unsigned char buffer[4096 * 32] __attribute__((aligned(4096))); + +static const struct sockaddr_alg sa =3D { + .salg_family =3D AF_ALG, + .salg_type =3D "hash", + .salg_name =3D "sha1", +}; + +static __attribute__((noreturn)) +void format(void) +{ + fprintf(stderr, "alg-send [-s] [-z] |-\n"); + exit(2); +} + +int main(int argc, char *argv[]) +{ + struct stat st; + const char *filename; + ssize_t r, w, o, ret; + size_t size =3D LONG_MAX, i; + char *end; + int use_sendfile =3D 0; + int opt, alg, sock, fd =3D 0; + + while ((opt =3D getopt(argc, argv, "sz:")) !=3D EOF) { + switch (opt) { + case 's': + use_sendfile =3D true; + break; + case 'z': + size =3D strtoul(optarg, &end, 0); + switch (*end) { + case 'K': + case 'k': + size *=3D 1024; + break; + case 'M': + case 'm': + size *=3D 1024 * 1024; + break; + } + break; + default: + format(); + } + } + + argc -=3D optind; + argv +=3D optind; + if (argc !=3D 1) + format(); + filename =3D argv[0]; + + alg =3D socket(AF_ALG, SOCK_SEQPACKET, 0); + OSERROR(alg, "AF_ALG"); + OSERROR(bind(alg, (struct sockaddr *)&sa, sizeof(sa)), "bind"); + sock =3D accept(alg, NULL, 0); + OSERROR(sock, "accept"); + + if (strcmp(filename, "-") !=3D 0) { + fd =3D open(filename, O_RDONLY); + OSERROR(fd, filename); + OSERROR(fstat(fd, &st), filename); + size =3D st.st_size; + } else { + OSERROR(fstat(fd, &st), argv[2]); + } + + if (!use_sendfile) { + bool more =3D false; + + while (size) { + r =3D read(fd, buffer, sizeof(buffer)); + OSERROR(r, filename); + if (r =3D=3D 0) + break; + size -=3D r; + + o =3D 0; + do { + more =3D size > 0; + w =3D send(sock, buffer + o, r - o, + more ? MSG_MORE : 0); + OSERROR(w, "sock/send"); + o +=3D w; + } while (o < r); + } + + if (more) + send(sock, NULL, 0, 0); + } else if (S_ISFIFO(st.st_mode)) { + r =3D splice(fd, NULL, sock, NULL, size, 0); + OSERROR(r, "sock/splice"); + if (r !=3D size) { + fprintf(stderr, "Short splice\n"); + exit(1); + } + } else { + r =3D sendfile(sock, fd, NULL, size); + OSERROR(r, "sock/sendfile"); + if (r !=3D size) { + fprintf(stderr, "Short sendfile\n"); + exit(1); + } + } + + ret =3D read(sock, buffer, sizeof(buffer)); + OSERROR(ret, "sock/read"); + + for (i =3D 0; i < ret; i++) + printf("%02x", (unsigned char)buffer[i]); + printf("\n"); + + OSERROR(close(sock), "sock/close"); + OSERROR(close(alg), "alg/close"); + OSERROR(close(fd), "close"); + return 0; +} diff --git a/samples/net/splice-out.c b/samples/net/splice-out.c new file mode 100644 index 000000000000..07bc0d774779 --- /dev/null +++ b/samples/net/splice-out.c @@ -0,0 +1,142 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* Splice or sendfile from the given file/stdin to stdout. + * + * Format: splice-out [-s] |- [] + * + * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define OSERROR(X, Y) do { if ((long)(X) =3D=3D -1) { perror(Y); exit(1); = } } while(0) +#define min(x, y) ((x) < (y) ? (x) : (y)) + +static unsigned char buffer[4096] __attribute__((aligned(4096))); + +static __attribute__((noreturn)) +void format(void) +{ + fprintf(stderr, "splice-out [-kN][-s][-wN] |- []\n"); + exit(2); +} + +int main(int argc, char *argv[]) +{ + const char *filename; + struct stat st; + ssize_t r; + size_t size =3D 1024 * 1024, skip =3D 0, unit =3D 0, part; + char *end; + bool use_sendfile =3D false, all =3D true; + int opt, fd =3D 0; + + while ((opt =3D getopt(argc, argv, "k:sw:")), + opt !=3D -1) { + switch (opt) { + case 'k': + /* Skip size - prevent coalescence. */ + skip =3D strtoul(optarg, &end, 0); + if (skip < 1 || skip >=3D 4096) { + fprintf(stderr, "-kN must be 00\n"); + exit(2); + } + switch (*end) { + case 'K': + case 'k': + unit *=3D 1024; + break; + case 'M': + case 'm': + unit *=3D 1024 * 1024; + break; + } + break; + default: + format(); + } + } + + argc -=3D optind; + argv +=3D optind; + + if (argc !=3D 1 && argc !=3D 2) + format(); + + filename =3D argv[0]; + if (argc =3D=3D 2) { + size =3D strtoul(argv[1], &end, 0); + switch (*end) { + case 'K': + case 'k': + size *=3D 1024; + break; + case 'M': + case 'm': + size *=3D 1024 * 1024; + break; + } + all =3D false; + } + + OSERROR(fstat(1, &st), "stdout"); + if (!S_ISFIFO(st.st_mode)) { + fprintf(stderr, "stdout must be a pipe\n"); + exit(3); + } + + if (strcmp(filename, "-") !=3D 0) { + fd =3D open(filename, O_RDONLY); + OSERROR(fd, filename); + OSERROR(fstat(fd, &st), filename); + if (!all && size > st.st_size) { + fprintf(stderr, "%s: Specified size larger than file\n", filename); + exit(3); + } + } + + do { + if (skip) { + part =3D skip; + do { + r =3D read(fd, buffer, skip); + OSERROR(r, filename); + part -=3D r; + } while (part > 0 && r > 0); + } + + part =3D unit ? min(size, unit) : size; + if (use_sendfile) { + r =3D sendfile(1, fd, NULL, part); + OSERROR(r, "sendfile"); + } else { + r =3D splice(fd, NULL, 1, NULL, part, 0); + OSERROR(r, "splice"); + } + if (!all) + size -=3D r; + } while (r > 0 && size > 0); + + OSERROR(close(fd), "close"); + return 0; +} diff --git a/samples/net/tcp-send.c b/samples/net/tcp-send.c new file mode 100644 index 000000000000..153105f6a30a --- /dev/null +++ b/samples/net/tcp-send.c @@ -0,0 +1,154 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * TCP send client. Pass -s to splice. + * + * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define OSERROR(X, Y) do { if ((long)(X) =3D=3D -1) { perror(Y); exit(1); = } } while(0) + +static unsigned char buffer[4096] __attribute__((aligned(4096))); + +static __attribute__((noreturn)) +void format(void) +{ + fprintf(stderr, "tcp-send [-46s][-p][-z] |- \n"= ); + exit(2); +} + +int main(int argc, char *argv[]) +{ + struct addrinfo *addrs =3D NULL, hints =3D {}; + struct stat st; + const char *filename, *sockname, *service =3D "5555"; + ssize_t r, w, o; + size_t size =3D LONG_MAX; + char *end; + bool use_sendfile =3D false; + int opt, sock, fd =3D 0, gai; + + hints.ai_family =3D AF_UNSPEC; + hints.ai_socktype =3D SOCK_STREAM; + + while ((opt =3D getopt(argc, argv, "46p:sz:")) !=3D EOF) { + switch (opt) { + case '4': + hints.ai_family =3D AF_INET; + break; + case '6': + hints.ai_family =3D AF_INET6; + break; + case 'p': + service =3D optarg; + break; + case 's': + use_sendfile =3D true; + break; + case 'z': + size =3D strtoul(optarg, &end, 0); + switch (*end) { + case 'K': + case 'k': + size *=3D 1024; + break; + case 'M': + case 'm': + size *=3D 1024 * 1024; + break; + } + break; + default: + format(); + } + } + + argc -=3D optind; + argv +=3D optind; + if (argc !=3D 2) + format(); + filename =3D argv[0]; + sockname =3D argv[1]; + + gai =3D getaddrinfo(sockname, service, &hints, &addrs); + if (gai) { + fprintf(stderr, "%s: %s\n", sockname, gai_strerror(gai)); + exit(3); + } + + if (!addrs) { + fprintf(stderr, "%s: No addresses\n", sockname); + exit(3); + } + + sockname =3D addrs->ai_canonname; + sock =3D socket(addrs->ai_family, addrs->ai_socktype, addrs->ai_protocol); + OSERROR(sock, "socket"); + OSERROR(connect(sock, addrs->ai_addr, addrs->ai_addrlen), "connect"); + + if (strcmp(filename, "-") !=3D 0) { + fd =3D open(filename, O_RDONLY); + OSERROR(fd, filename); + OSERROR(fstat(fd, &st), filename); + if (size > st.st_size) + size =3D st.st_size; + } else { + OSERROR(fstat(fd, &st), filename); + } + + if (!use_sendfile) { + bool more =3D false; + + while (size) { + r =3D read(fd, buffer, sizeof(buffer)); + OSERROR(r, filename); + if (r =3D=3D 0) + break; + size -=3D r; + + o =3D 0; + do { + more =3D size > 0; + w =3D send(sock, buffer + o, r - o, + more ? MSG_MORE : 0); + OSERROR(w, "sock/send"); + o +=3D w; + } while (o < r); + } + + if (more) + send(sock, NULL, 0, 0); + } else if (S_ISFIFO(st.st_mode)) { + r =3D splice(fd, NULL, sock, NULL, size, 0); + OSERROR(r, "sock/splice"); + if (r !=3D size) { + fprintf(stderr, "Short splice\n"); + exit(1); + } + } else { + r =3D sendfile(sock, fd, NULL, size); + OSERROR(r, "sock/sendfile"); + if (r !=3D size) { + fprintf(stderr, "Short sendfile\n"); + exit(1); + } + } + + OSERROR(close(sock), "sock/close"); + OSERROR(close(fd), "close"); + return 0; +} diff --git a/samples/net/tcp-sink.c b/samples/net/tcp-sink.c new file mode 100644 index 000000000000..33d949d0e9aa --- /dev/null +++ b/samples/net/tcp-sink.c @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * TCP sink server + * + * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define OSERROR(X, Y) do { if ((long)(X) =3D=3D -1) { perror(Y); exit(1); = } } while(0) + +static unsigned char buffer[512 * 1024] __attribute__((aligned(4096))); + +static __attribute__((noreturn)) +void format(void) +{ + fprintf(stderr, "tcp-sink [-4][-p]\n"); + exit(2); +} + +int main(int argc, char *argv[]) +{ + unsigned int port =3D 5555; + bool ipv6 =3D true; + int opt, server_sock, sock; + + + while ((opt =3D getopt(argc, argv, "4p:")) !=3D EOF) { + switch (opt) { + case '4': + ipv6 =3D false; + break; + case 'p': + port =3D atoi(optarg); + break; + default: + format(); + } + } + + if (!ipv6) { + struct sockaddr_in sin =3D { + .sin_family =3D AF_INET, + .sin_port =3D htons(port), + }; + server_sock =3D socket(AF_INET, SOCK_STREAM, 0); + OSERROR(server_sock, "socket"); + OSERROR(bind(server_sock, (struct sockaddr *)&sin, sizeof(sin)), "bind"); + OSERROR(listen(server_sock, 1), "listen"); + } else { + struct sockaddr_in6 sin6 =3D { + .sin6_family =3D AF_INET6, + .sin6_port =3D htons(port), + }; + server_sock =3D socket(AF_INET6, SOCK_STREAM, 0); + OSERROR(server_sock, "socket"); + OSERROR(bind(server_sock, (struct sockaddr *)&sin6, sizeof(sin6)), "bind= "); + OSERROR(listen(server_sock, 1), "listen"); + } + + for (;;) { + sock =3D accept(server_sock, NULL, NULL); + if (sock !=3D -1) { + while (read(sock, buffer, sizeof(buffer)) > 0) {} + close(sock); + } + } +} diff --git a/samples/net/tls-send.c b/samples/net/tls-send.c new file mode 100644 index 000000000000..b3b8a0a3b41f --- /dev/null +++ b/samples/net/tls-send.c @@ -0,0 +1,176 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * TLS-over-TCP send client. Pass -s to splice. + * + * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define OSERROR(X, Y) do { if ((long)(X) =3D=3D -1) { perror(Y); exit(1); = } } while(0) + +static unsigned char buffer[4096] __attribute__((aligned(4096))); + +static __attribute__((noreturn)) +void format(void) +{ + fprintf(stderr, "tls-send [-46s][-p][-z] |- \n"= ); + exit(2); +} + +static void set_tls(int sock) +{ + struct tls12_crypto_info_aes_gcm_128 crypto_info; + + crypto_info.info.version =3D TLS_1_2_VERSION; + crypto_info.info.cipher_type =3D TLS_CIPHER_AES_GCM_128; + memset(crypto_info.iv, 0, TLS_CIPHER_AES_GCM_128_IV_SIZE); + memset(crypto_info.rec_seq, 0, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); + memset(crypto_info.key, 0, TLS_CIPHER_AES_GCM_128_KEY_SIZE); + memset(crypto_info.salt, 0, TLS_CIPHER_AES_GCM_128_SALT_SIZE); + + OSERROR(setsockopt(sock, SOL_TCP, TCP_ULP, "tls", sizeof("tls")), + "TCP_ULP"); + OSERROR(setsockopt(sock, SOL_TLS, TLS_TX, &crypto_info, sizeof(crypto_inf= o)), + "TLS_TX"); + OSERROR(setsockopt(sock, SOL_TLS, TLS_RX, &crypto_info, sizeof(crypto_inf= o)), + "TLS_RX"); +} + +int main(int argc, char *argv[]) +{ + struct addrinfo *addrs =3D NULL, hints =3D {}; + struct stat st; + const char *filename, *sockname, *service =3D "5556"; + ssize_t r, w, o; + size_t size =3D LONG_MAX; + char *end; + bool use_sendfile =3D false; + int opt, sock, fd =3D 0, gai; + + hints.ai_family =3D AF_UNSPEC; + hints.ai_socktype =3D SOCK_STREAM; + + while ((opt =3D getopt(argc, argv, "46p:sz:")) !=3D EOF) { + switch (opt) { + case '4': + hints.ai_family =3D AF_INET; + break; + case '6': + hints.ai_family =3D AF_INET6; + break; + case 'p': + service =3D optarg; + break; + case 's': + use_sendfile =3D true; + break; + case 'z': + size =3D strtoul(optarg, &end, 0); + switch (*end) { + case 'K': + case 'k': + size *=3D 1024; + break; + case 'M': + case 'm': + size *=3D 1024 * 1024; + break; + } + break; + default: + format(); + } + } + + argc -=3D optind; + argv +=3D optind; + if (argc !=3D 2) + format(); + filename =3D argv[0]; + sockname =3D argv[1]; + + gai =3D getaddrinfo(sockname, service, &hints, &addrs); + if (gai) { + fprintf(stderr, "%s: %s\n", sockname, gai_strerror(gai)); + exit(3); + } + + if (!addrs) { + fprintf(stderr, "%s: No addresses\n", sockname); + exit(3); + } + + sockname =3D addrs->ai_canonname; + sock =3D socket(addrs->ai_family, addrs->ai_socktype, addrs->ai_protocol); + OSERROR(sock, "socket"); + OSERROR(connect(sock, addrs->ai_addr, addrs->ai_addrlen), "connect"); + set_tls(sock); + + if (strcmp(filename, "-") !=3D 0) { + fd =3D open(filename, O_RDONLY); + OSERROR(fd, filename); + OSERROR(fstat(fd, &st), filename); + if (size > st.st_size) + size =3D st.st_size; + } else { + OSERROR(fstat(fd, &st), filename); + } + + if (!use_sendfile) { + bool more =3D false; + + while (size) { + r =3D read(fd, buffer, sizeof(buffer)); + OSERROR(r, filename); + if (r =3D=3D 0) + break; + size -=3D r; + + o =3D 0; + do { + more =3D size > 0; + w =3D send(sock, buffer + o, r - o, + more ? MSG_MORE : 0); + OSERROR(w, "sock/send"); + o +=3D w; + } while (o < r); + } + + if (more) + send(sock, NULL, 0, 0); + } else if (S_ISFIFO(st.st_mode)) { + r =3D splice(fd, NULL, sock, NULL, size, 0); + OSERROR(r, "sock/splice"); + if (r !=3D size) { + fprintf(stderr, "Short splice\n"); + exit(1); + } + } else { + r =3D sendfile(sock, fd, NULL, size); + OSERROR(r, "sock/sendfile"); + if (r !=3D size) { + fprintf(stderr, "Short sendfile\n"); + exit(1); + } + } + + OSERROR(close(sock), "sock/close"); + OSERROR(close(fd), "close"); + return 0; +} diff --git a/samples/net/tls-sink.c b/samples/net/tls-sink.c new file mode 100644 index 000000000000..1d6d4ed07101 --- /dev/null +++ b/samples/net/tls-sink.c @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * TLS-over-TCP sink server + * + * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define OSERROR(X, Y) do { if ((long)(X) =3D=3D -1) { perror(Y); exit(1); = } } while(0) + +static unsigned char buffer[512 * 1024] __attribute__((aligned(4096))); + +static __attribute__((noreturn)) +void format(void) +{ + fprintf(stderr, "tls-sink [-4][-p]\n"); + exit(2); +} + +static void set_tls(int sock) +{ + struct tls12_crypto_info_aes_gcm_128 crypto_info; + + crypto_info.info.version =3D TLS_1_2_VERSION; + crypto_info.info.cipher_type =3D TLS_CIPHER_AES_GCM_128; + memset(crypto_info.iv, 0, TLS_CIPHER_AES_GCM_128_IV_SIZE); + memset(crypto_info.rec_seq, 0, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); + memset(crypto_info.key, 0, TLS_CIPHER_AES_GCM_128_KEY_SIZE); + memset(crypto_info.salt, 0, TLS_CIPHER_AES_GCM_128_SALT_SIZE); + + OSERROR(setsockopt(sock, SOL_TCP, TCP_ULP, "tls", sizeof("tls")), + "TCP_ULP"); + OSERROR(setsockopt(sock, SOL_TLS, TLS_TX, &crypto_info, sizeof(crypto_inf= o)), + "TLS_TX"); + OSERROR(setsockopt(sock, SOL_TLS, TLS_RX, &crypto_info, sizeof(crypto_inf= o)), + "TLS_RX"); +} + +int main(int argc, char *argv[]) +{ + unsigned int port =3D 5556; + bool ipv6 =3D true; + int opt, server_sock, sock; + + + while ((opt =3D getopt(argc, argv, "4p:")) !=3D EOF) { + switch (opt) { + case '4': + ipv6 =3D false; + break; + case 'p': + port =3D atoi(optarg); + break; + default: + format(); + } + } + + if (!ipv6) { + struct sockaddr_in sin =3D { + .sin_family =3D AF_INET, + .sin_port =3D htons(port), + }; + server_sock =3D socket(AF_INET, SOCK_STREAM, 0); + OSERROR(server_sock, "socket"); + OSERROR(bind(server_sock, (struct sockaddr *)&sin, sizeof(sin)), "bind"); + OSERROR(listen(server_sock, 1), "listen"); + } else { + struct sockaddr_in6 sin6 =3D { + .sin6_family =3D AF_INET6, + .sin6_port =3D htons(port), + }; + server_sock =3D socket(AF_INET6, SOCK_STREAM, 0); + OSERROR(server_sock, "socket"); + OSERROR(bind(server_sock, (struct sockaddr *)&sin6, sizeof(sin6)), "bind= "); + OSERROR(listen(server_sock, 1), "listen"); + } + + for (;;) { + sock =3D accept(server_sock, NULL, NULL); + if (sock !=3D -1) { + set_tls(sock); + while (read(sock, buffer, sizeof(buffer)) > 0) {} + close(sock); + } + } +} diff --git a/samples/net/udp-send.c b/samples/net/udp-send.c new file mode 100644 index 000000000000..31abd6b2d9fd --- /dev/null +++ b/samples/net/udp-send.c @@ -0,0 +1,151 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * UDP send client. Pass -s to splice. + * + * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define OSERROR(X, Y) do { if ((long)(X) =3D=3D -1) { perror(Y); exit(1); = } } while(0) +#define min(x, y) ((x) < (y) ? (x) : (y)) + +static unsigned char buffer[65536] __attribute__((aligned(4096))); + +static __attribute__((noreturn)) +void format(void) +{ + fprintf(stderr, "udp-send [-46s][-p][-z] |- \n"= ); + exit(2); +} + +int main(int argc, char *argv[]) +{ + struct addrinfo *addrs =3D NULL, hints =3D {}; + struct stat st; + const char *filename, *sockname, *service =3D "5555"; + unsigned int len; + ssize_t r, o, size =3D 65535; + char *end; + bool use_sendfile =3D false; + int opt, sock, fd =3D 0, gai; + + hints.ai_family =3D AF_UNSPEC; + hints.ai_socktype =3D SOCK_DGRAM; + + while ((opt =3D getopt(argc, argv, "46p:sz:")) !=3D EOF) { + switch (opt) { + case '4': + hints.ai_family =3D AF_INET; + break; + case '6': + hints.ai_family =3D AF_INET6; + break; + case 'p': + service =3D optarg; + break; + case 's': + use_sendfile =3D true; + break; + case 'z': + size =3D strtoul(optarg, &end, 0); + switch (*end) { + case 'K': + case 'k': + size *=3D 1024; + break; + } + if (size > 65535) { + fprintf(stderr, "Too much data for UDP packet\n"); + exit(2); + } + break; + default: + format(); + } + } + + argc -=3D optind; + argv +=3D optind; + if (argc !=3D 2) + format(); + filename =3D argv[0]; + sockname =3D argv[1]; + + gai =3D getaddrinfo(sockname, service, &hints, &addrs); + if (gai) { + fprintf(stderr, "%s: %s\n", sockname, gai_strerror(gai)); + exit(3); + } + + if (!addrs) { + fprintf(stderr, "%s: No addresses\n", sockname); + exit(3); + } + + sockname =3D addrs->ai_canonname; + sock =3D socket(addrs->ai_family, addrs->ai_socktype, addrs->ai_protocol); + OSERROR(sock, "socket"); + OSERROR(connect(sock, addrs->ai_addr, addrs->ai_addrlen), "connect"); + + if (strcmp(filename, "-") !=3D 0) { + fd =3D open(filename, O_RDONLY); + OSERROR(fd, filename); + OSERROR(fstat(fd, &st), filename); + if (size > st.st_size) + size =3D st.st_size; + } else { + OSERROR(fstat(fd, &st), filename); + } + + len =3D htonl(size); + OSERROR(send(sock, &len, 4, MSG_MORE), "sock/send"); + + if (!use_sendfile) { + while (size) { + r =3D read(fd, buffer, sizeof(buffer)); + OSERROR(r, filename); + if (r =3D=3D 0) + break; + size -=3D r; + + o =3D 0; + do { + ssize_t w =3D send(sock, buffer + o, r - o, + size > 0 ? MSG_MORE : 0); + OSERROR(w, "sock/send"); + o +=3D w; + } while (o < r); + } + } else if (S_ISFIFO(st.st_mode)) { + r =3D splice(fd, NULL, sock, NULL, size, 0); + OSERROR(r, "sock/splice"); + if (r !=3D size) { + fprintf(stderr, "Short splice\n"); + exit(1); + } + } else { + r =3D sendfile(sock, fd, NULL, size); + OSERROR(r, "sock/sendfile"); + if (r !=3D size) { + fprintf(stderr, "Short sendfile\n"); + exit(1); + } + } + + OSERROR(close(sock), "sock/close"); + OSERROR(close(fd), "close"); + return 0; +} diff --git a/samples/net/udp-sink.c b/samples/net/udp-sink.c new file mode 100644 index 000000000000..b98f45b64296 --- /dev/null +++ b/samples/net/udp-sink.c @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * UDP sink server + * + * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define OSERROR(X, Y) do { if ((long)(X) =3D=3D -1) { perror(Y); exit(1); = } } while(0) + +static unsigned char buffer[512 * 1024] __attribute__((aligned(4096))); + +static __attribute__((noreturn)) +void format(void) +{ + fprintf(stderr, "udp-sink [-4][-p]\n"); + exit(2); +} + +int main(int argc, char *argv[]) +{ + struct iovec iov[1] =3D { + [0] =3D { + .iov_base =3D buffer, + .iov_len =3D sizeof(buffer), + }, + }; + struct msghdr msg =3D { + .msg_iov =3D iov, + .msg_iovlen =3D 1, + }; + unsigned int port =3D 5555; + bool ipv6 =3D true; + int opt, sock; + + while ((opt =3D getopt(argc, argv, "4p:")) !=3D EOF) { + switch (opt) { + case '4': + ipv6 =3D false; + break; + case 'p': + port =3D atoi(optarg); + break; + default: + format(); + } + } + + if (!ipv6) { + struct sockaddr_in sin =3D { + .sin_family =3D AF_INET, + .sin_port =3D htons(port), + }; + sock =3D socket(AF_INET, SOCK_DGRAM, 0); + OSERROR(sock, "socket"); + OSERROR(bind(sock, (struct sockaddr *)&sin, sizeof(sin)), "bind"); + } else { + struct sockaddr_in6 sin6 =3D { + .sin6_family =3D AF_INET6, + .sin6_port =3D htons(port), + }; + sock =3D socket(AF_INET6, SOCK_DGRAM, 0); + OSERROR(sock, "socket"); + OSERROR(bind(sock, (struct sockaddr *)&sin6, sizeof(sin6)), "bind"); + } + + for (;;) { + ssize_t r; + + r =3D recvmsg(sock, &msg, 0); + printf("rx %zd\n", r); + } +} diff --git a/samples/net/unix-send.c b/samples/net/unix-send.c new file mode 100644 index 000000000000..88fae776985c --- /dev/null +++ b/samples/net/unix-send.c @@ -0,0 +1,147 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * AF_UNIX stream send client. Pass -s to splice. + * + * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define OSERROR(X, Y) do { if ((long)(X) =3D=3D -1) { perror(Y); exit(1); = } } while(0) +#define min(x, y) ((x) < (y) ? (x) : (y)) + +static unsigned char buffer[4096] __attribute__((aligned(4096))); + +static __attribute__((noreturn)) +void format(void) +{ + fprintf(stderr, "unix-send [-s] [-z] |- \n"); + exit(2); +} + +int main(int argc, char *argv[]) +{ + struct sockaddr_un sun =3D { .sun_family =3D AF_UNIX, }; + struct stat st; + const char *filename, *sockname; + ssize_t r, w, o, size =3D LONG_MAX; + size_t plen, total =3D 0; + char *end; + bool use_sendfile =3D false, all =3D true; + int opt, sock, fd =3D 0; + + while ((opt =3D getopt(argc, argv, "sz:")) !=3D EOF) { + switch (opt) { + case 's': + use_sendfile =3D true; + break; + case 'z': + size =3D strtoul(optarg, &end, 0); + switch (*end) { + case 'K': + case 'k': + size *=3D 1024; + break; + case 'M': + case 'm': + size *=3D 1024 * 1024; + break; + } + all =3D false; + break; + default: + format(); + } + } + + argc -=3D optind; + argv +=3D optind; + if (argc !=3D 2) + format(); + filename =3D argv[0]; + sockname =3D argv[1]; + + plen =3D strlen(sockname); + if (plen =3D=3D 0 || plen > sizeof(sun.sun_path) - 1) { + fprintf(stderr, "socket filename too short or too long\n"); + exit(2); + } + memcpy(sun.sun_path, sockname, plen + 1); + + sock =3D socket(AF_UNIX, SOCK_STREAM, 0); + OSERROR(sock, "socket"); + OSERROR(connect(sock, (struct sockaddr *)&sun, sizeof(sun)), "connect"); + + if (strcmp(filename, "-") !=3D 0) { + fd =3D open(filename, O_RDONLY); + OSERROR(fd, filename); + OSERROR(fstat(fd, &st), filename); + if (size > st.st_size) + size =3D st.st_size; + } else { + OSERROR(fstat(fd, &st), argv[2]); + } + + if (!use_sendfile) { + bool more =3D false; + + while (size) { + r =3D read(fd, buffer, min(sizeof(buffer), size)); + OSERROR(r, filename); + if (r =3D=3D 0) + break; + size -=3D r; + + o =3D 0; + do { + more =3D size > 0; + w =3D send(sock, buffer + o, r - o, + more ? MSG_MORE : 0); + OSERROR(w, "sock/send"); + o +=3D w; + total +=3D w; + } while (o < r); + } + + if (more) + send(sock, NULL, 0, 0); + } else if (S_ISFIFO(st.st_mode)) { + do { + r =3D splice(fd, NULL, sock, NULL, size, + size > 0 ? SPLICE_F_MORE : 0); + OSERROR(r, "sock/splice"); + size -=3D r; + total +=3D r; + } while (r > 0 && size > 0); + if (size && !all) { + fprintf(stderr, "Short splice\n"); + exit(1); + } + } else { + r =3D sendfile(sock, fd, NULL, size); + OSERROR(r, "sock/sendfile"); + if (r !=3D size) { + fprintf(stderr, "Short sendfile\n"); + exit(1); + } + total +=3D r; + } + + printf("Sent %zu bytes\n", total); + OSERROR(close(sock), "sock/close"); + OSERROR(close(fd), "close"); + return 0; +} diff --git a/samples/net/unix-sink.c b/samples/net/unix-sink.c new file mode 100644 index 000000000000..3c75979dc52a --- /dev/null +++ b/samples/net/unix-sink.c @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * UNIX stream sink server + * + * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + */ + +#include +#include +#include +#include +#include +#include +#include + +#define OSERROR(X, Y) do { if ((long)(X) =3D=3D -1) { perror(Y); exit(1); = } } while(0) + +static unsigned char buffer[512 * 1024] __attribute__((aligned(4096))); + +int main(int argc, char *argv[]) +{ + struct sockaddr_un sun =3D { .sun_family =3D AF_UNIX, }; + size_t plen; + int server_sock, sock; + + if (argc !=3D 2) { + fprintf(stderr, "unix-sink \n"); + exit(2); + } + + plen =3D strlen(argv[1]); + if (plen =3D=3D 0 || plen > sizeof(sun.sun_path) - 1) { + fprintf(stderr, "socket filename too short or too long\n"); + exit(2); + } + memcpy(sun.sun_path, argv[1], plen + 1); + + server_sock =3D socket(AF_UNIX, SOCK_STREAM, 0); + OSERROR(server_sock, "socket"); + OSERROR(bind(server_sock, (struct sockaddr *)&sun, sizeof(sun)), "bind"); + OSERROR(listen(server_sock, 1), "listen"); + + for (;;) { + sock =3D accept(server_sock, NULL, NULL); + if (sock !=3D -1) { + while (read(sock, buffer, sizeof(buffer)) > 0) {} + close(sock); + } + } +} From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45508C76188 for ; Wed, 5 Apr 2023 16:55:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233651AbjDEQza (ORCPT ); Wed, 5 Apr 2023 12:55:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229631AbjDEQzY (ORCPT ); Wed, 5 Apr 2023 12:55:24 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2BBA54480 for ; Wed, 5 Apr 2023 09:53:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713635; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QAACn2mr7ooi1DdUlemHLPNq4itk2LGg3Qvic6fgj7U=; b=ROSJirJnfIu+QIEYocTaKCoTGBeWXChXsL3rMLjzJBlQQb46+LnkAVhrYI9SCM0JGq5fiD gfB3KuOelYavI8ebZesI9/pbd2hGewnZZOzik+ea91Km1ljG1H+CETBQ3W3/Q9Bueiiep4 hv2x7Yjv/G5+W9n9R1tJh8hNj5IMmOU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-86-CL3O1Pd0MuC9cMWkPeSHyw-1; Wed, 05 Apr 2023 12:53:51 -0400 X-MC-Unique: CL3O1Pd0MuC9cMWkPeSHyw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 82003185A791; Wed, 5 Apr 2023 16:53:50 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6A251400F57; Wed, 5 Apr 2023 16:53:48 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Willem de Bruijn Subject: [PATCH net-next v4 02/20] net: Declare MSG_SPLICE_PAGES internal sendmsg() flag Date: Wed, 5 Apr 2023 17:53:21 +0100 Message-Id: <20230405165339.3468808-3-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Declare MSG_SPLICE_PAGES, an internal sendmsg() flag, that hints to a network protocol that it should splice pages from the source iterator rather than copying the data if it can. This flag is added to a list that is cleared by sendmsg syscalls on entry. This is intended as a replacement for the ->sendpage() op, allowing a way to splice in several multipage folios in one go. Signed-off-by: David Howells Reviewed-by: Willem de Bruijn cc: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- include/linux/socket.h | 3 +++ net/socket.c | 2 ++ 2 files changed, 5 insertions(+) diff --git a/include/linux/socket.h b/include/linux/socket.h index 13c3a237b9c9..bd1cc3238851 100644 --- a/include/linux/socket.h +++ b/include/linux/socket.h @@ -327,6 +327,7 @@ struct ucred { */ =20 #define MSG_ZEROCOPY 0x4000000 /* Use user data in kernel path */ +#define MSG_SPLICE_PAGES 0x8000000 /* Splice the pages from the iterator i= n sendmsg() */ #define MSG_FASTOPEN 0x20000000 /* Send data in TCP SYN */ #define MSG_CMSG_CLOEXEC 0x40000000 /* Set close_on_exec for file descriptor received through @@ -337,6 +338,8 @@ struct ucred { #define MSG_CMSG_COMPAT 0 /* We never have 32 bit fixups */ #endif =20 +/* Flags to be cleared on entry by sendmsg and sendmmsg syscalls */ +#define MSG_INTERNAL_SENDMSG_FLAGS (MSG_SPLICE_PAGES) =20 /* Setsockoptions(2) level. Thanks to BSD these must match IPPROTO_xxx */ #define SOL_IP 0 diff --git a/net/socket.c b/net/socket.c index 73e493da4589..b3fd3f7f7e03 100644 --- a/net/socket.c +++ b/net/socket.c @@ -2136,6 +2136,7 @@ int __sys_sendto(int fd, void __user *buff, size_t le= n, unsigned int flags, msg.msg_name =3D (struct sockaddr *)&address; msg.msg_namelen =3D addr_len; } + flags &=3D ~MSG_INTERNAL_SENDMSG_FLAGS; if (sock->file->f_flags & O_NONBLOCK) flags |=3D MSG_DONTWAIT; msg.msg_flags =3D flags; @@ -2483,6 +2484,7 @@ static int ____sys_sendmsg(struct socket *sock, struc= t msghdr *msg_sys, } msg_sys->msg_flags =3D flags; =20 + flags &=3D ~MSG_INTERNAL_SENDMSG_FLAGS; if (sock->file->f_flags & O_NONBLOCK) msg_sys->msg_flags |=3D MSG_DONTWAIT; /* From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D7D4CC76188 for ; Wed, 5 Apr 2023 16:55:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233656AbjDEQzk (ORCPT ); Wed, 5 Apr 2023 12:55:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233638AbjDEQz2 (ORCPT ); Wed, 5 Apr 2023 12:55:28 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 378F55276 for ; Wed, 5 Apr 2023 09:54:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713641; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6CZTaviJSB+HI6SS+UVazISaMVPKFDisRAz1iEkYVHU=; b=Dla8QCvM8hpGX73m10zK5/Eaxfv81ugGF38q8WQW9UZ8vUB9XdluHhy0EIqqjOeE9jy2Y/ AbCuZCFsk7ZhBM91R95Zxjtxg60ufunfeB6hDKEcGum1ukrfVatFPRe3iSeZuAussB/DZr ZNbsJD9FZNHIDfpOQLF93PxcY0FzOB4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-347-uZn3wgunNsa9-TdfFJYYyQ-1; Wed, 05 Apr 2023 12:53:54 -0400 X-MC-Unique: uZn3wgunNsa9-TdfFJYYyQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9442A8996E6; Wed, 5 Apr 2023 16:53:53 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 23187140EBF4; Wed, 5 Apr 2023 16:53:51 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Bernard Metzler , Tom Talpey , linux-rdma@vger.kernel.org Subject: [PATCH net-next v4 03/20] mm: Move the page fragment allocator from page_alloc.c into its own file Date: Wed, 5 Apr 2023 17:53:22 +0100 Message-Id: <20230405165339.3468808-4-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Move the page fragment allocator from page_alloc.c into its own file preparatory to changing it. Signed-off-by: David Howells cc: Bernard Metzler cc: Tom Talpey cc: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: linux-rdma@vger.kernel.org cc: netdev@vger.kernel.org --- mm/Makefile | 2 +- mm/page_alloc.c | 126 ----------------------------------------- mm/page_frag_alloc.c | 131 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 127 deletions(-) create mode 100644 mm/page_frag_alloc.c diff --git a/mm/Makefile b/mm/Makefile index 8e105e5b3e29..4e6dc12b4cbd 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -52,7 +52,7 @@ obj-y :=3D filemap.o mempool.o oom_kill.o fadvise.o \ readahead.o swap.o truncate.o vmscan.o shmem.o \ util.o mmzone.o vmstat.o backing-dev.o \ mm_init.o percpu.o slab_common.o \ - compaction.o \ + compaction.o page_frag_alloc.o \ interval_tree.o list_lru.o workingset.o \ debug.o gup.o mmap_lock.o $(mmu-y) =20 diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 7136c36c5d01..d751e750c14b 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -5695,132 +5695,6 @@ void free_pages(unsigned long addr, unsigned int or= der) =20 EXPORT_SYMBOL(free_pages); =20 -/* - * Page Fragment: - * An arbitrary-length arbitrary-offset area of memory which resides - * within a 0 or higher order page. Multiple fragments within that page - * are individually refcounted, in the page's reference counter. - * - * The page_frag functions below provide a simple allocation framework for - * page fragments. This is used by the network stack and network device - * drivers to provide a backing region of memory for use as either an - * sk_buff->head, or to be used in the "frags" portion of skb_shared_info. - */ -static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, - gfp_t gfp_mask) -{ - struct page *page =3D NULL; - gfp_t gfp =3D gfp_mask; - -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - gfp_mask |=3D __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY | - __GFP_NOMEMALLOC; - page =3D alloc_pages_node(NUMA_NO_NODE, gfp_mask, - PAGE_FRAG_CACHE_MAX_ORDER); - nc->size =3D page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; -#endif - if (unlikely(!page)) - page =3D alloc_pages_node(NUMA_NO_NODE, gfp, 0); - - nc->va =3D page ? page_address(page) : NULL; - - return page; -} - -void __page_frag_cache_drain(struct page *page, unsigned int count) -{ - VM_BUG_ON_PAGE(page_ref_count(page) =3D=3D 0, page); - - if (page_ref_sub_and_test(page, count)) - free_the_page(page, compound_order(page)); -} -EXPORT_SYMBOL(__page_frag_cache_drain); - -void *page_frag_alloc_align(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask, - unsigned int align_mask) -{ - unsigned int size =3D PAGE_SIZE; - struct page *page; - int offset; - - if (unlikely(!nc->va)) { -refill: - page =3D __page_frag_cache_refill(nc, gfp_mask); - if (!page) - return NULL; - -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size =3D nc->size; -#endif - /* Even if we own the page, we do not use atomic_set(). - * This would break get_page_unless_zero() users. - */ - page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); - - /* reset page count bias and offset to start of new frag */ - nc->pfmemalloc =3D page_is_pfmemalloc(page); - nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset =3D size; - } - - offset =3D nc->offset - fragsz; - if (unlikely(offset < 0)) { - page =3D virt_to_page(nc->va); - - if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) - goto refill; - - if (unlikely(nc->pfmemalloc)) { - free_the_page(page, compound_order(page)); - goto refill; - } - -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size =3D nc->size; -#endif - /* OK, page count is 0, we can safely set it */ - set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); - - /* reset page count bias and offset to start of new frag */ - nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; - offset =3D size - fragsz; - if (unlikely(offset < 0)) { - /* - * The caller is trying to allocate a fragment - * with fragsz > PAGE_SIZE but the cache isn't big - * enough to satisfy the request, this may - * happen in low memory conditions. - * We don't release the cache page because - * it could make memory pressure worse - * so we simply return NULL here. - */ - return NULL; - } - } - - nc->pagecnt_bias--; - offset &=3D align_mask; - nc->offset =3D offset; - - return nc->va + offset; -} -EXPORT_SYMBOL(page_frag_alloc_align); - -/* - * Frees a page fragment allocated out of either a compound or order 0 pag= e. - */ -void page_frag_free(void *addr) -{ - struct page *page =3D virt_to_head_page(addr); - - if (unlikely(put_page_testzero(page))) - free_the_page(page, compound_order(page)); -} -EXPORT_SYMBOL(page_frag_free); - static void *make_alloc_exact(unsigned long addr, unsigned int order, size_t size) { diff --git a/mm/page_frag_alloc.c b/mm/page_frag_alloc.c new file mode 100644 index 000000000000..bee95824ef8f --- /dev/null +++ b/mm/page_frag_alloc.c @@ -0,0 +1,131 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Page fragment allocator + * + * Page Fragment: + * An arbitrary-length arbitrary-offset area of memory which resides with= in a + * 0 or higher order page. Multiple fragments within that page are + * individually refcounted, in the page's reference counter. + * + * The page_frag functions provide a simple allocation framework for page + * fragments. This is used by the network stack and network device driver= s to + * provide a backing region of memory for use as either an sk_buff->head, = or to + * be used in the "frags" portion of skb_shared_info. + */ + +#include +#include +#include + +static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, + gfp_t gfp_mask) +{ + struct page *page =3D NULL; + gfp_t gfp =3D gfp_mask; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + gfp_mask |=3D __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY | + __GFP_NOMEMALLOC; + page =3D alloc_pages_node(NUMA_NO_NODE, gfp_mask, + PAGE_FRAG_CACHE_MAX_ORDER); + nc->size =3D page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; +#endif + if (unlikely(!page)) + page =3D alloc_pages_node(NUMA_NO_NODE, gfp, 0); + + nc->va =3D page ? page_address(page) : NULL; + + return page; +} + +void __page_frag_cache_drain(struct page *page, unsigned int count) +{ + VM_BUG_ON_PAGE(page_ref_count(page) =3D=3D 0, page); + + if (page_ref_sub_and_test(page, count - 1)) + __free_pages(page, compound_order(page)); +} +EXPORT_SYMBOL(__page_frag_cache_drain); + +void *page_frag_alloc_align(struct page_frag_cache *nc, + unsigned int fragsz, gfp_t gfp_mask, + unsigned int align_mask) +{ + unsigned int size =3D PAGE_SIZE; + struct page *page; + int offset; + + if (unlikely(!nc->va)) { +refill: + page =3D __page_frag_cache_refill(nc, gfp_mask); + if (!page) + return NULL; + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + /* if size can vary use size else just use PAGE_SIZE */ + size =3D nc->size; +#endif + /* Even if we own the page, we do not use atomic_set(). + * This would break get_page_unless_zero() users. + */ + page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); + + /* reset page count bias and offset to start of new frag */ + nc->pfmemalloc =3D page_is_pfmemalloc(page); + nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; + nc->offset =3D size; + } + + offset =3D nc->offset - fragsz; + if (unlikely(offset < 0)) { + page =3D virt_to_page(nc->va); + + if (page_ref_count(page) !=3D nc->pagecnt_bias) + goto refill; + if (unlikely(nc->pfmemalloc)) { + page_ref_sub(page, nc->pagecnt_bias - 1); + __free_pages(page, compound_order(page)); + goto refill; + } + +#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) + /* if size can vary use size else just use PAGE_SIZE */ + size =3D nc->size; +#endif + /* OK, page count is 0, we can safely set it */ + set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); + + /* reset page count bias and offset to start of new frag */ + nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; + offset =3D size - fragsz; + if (unlikely(offset < 0)) { + /* + * The caller is trying to allocate a fragment + * with fragsz > PAGE_SIZE but the cache isn't big + * enough to satisfy the request, this may + * happen in low memory conditions. + * We don't release the cache page because + * it could make memory pressure worse + * so we simply return NULL here. + */ + return NULL; + } + } + + nc->pagecnt_bias--; + offset &=3D align_mask; + nc->offset =3D offset; + + return nc->va + offset; +} +EXPORT_SYMBOL(page_frag_alloc_align); + +/* + * Frees a page fragment allocated out of either a compound or order 0 pag= e. + */ +void page_frag_free(void *addr) +{ + struct page *page =3D virt_to_head_page(addr); + + __free_pages(page, compound_order(page)); +} +EXPORT_SYMBOL(page_frag_free); From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8E7CDC7619A for ; Wed, 5 Apr 2023 16:55:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233257AbjDEQz0 (ORCPT ); Wed, 5 Apr 2023 12:55:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230144AbjDEQzU (ORCPT ); Wed, 5 Apr 2023 12:55:20 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB49B4ED0 for ; Wed, 5 Apr 2023 09:53:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713638; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5HFcSli7GWFgeyoEx21ojBPq2vhhM7tieZtPm930p+c=; b=GylYef7p+jpVN9uzCLTEglx/uEpamy8qDTyXoyTn82CbHHJmScl6mKpw1FZoWdzJjwuYXQ 6lMXBL35vcRay3oPl3G1MG9H89jj5icKXlM7licYOZw+QwGwDLqh7EB7g7tlecgVTdyB7f NHAE6ZUKwEAODZLDZzCI2OXJglYAaFw= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-163-JZMa1yBiN6mNkIPxSZoyZQ-1; Wed, 05 Apr 2023 12:53:57 -0400 X-MC-Unique: JZMa1yBiN6mNkIPxSZoyZQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 53FD1185A794; Wed, 5 Apr 2023 16:53:56 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4A7E8C1602A; Wed, 5 Apr 2023 16:53:54 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH net-next v4 04/20] mm: Make the page_frag_cache allocator use multipage folios Date: Wed, 5 Apr 2023 17:53:23 +0100 Message-Id: <20230405165339.3468808-5-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Change the page_frag_cache allocator to use multipage folios rather than groups of pages. This reduces page_frag_free to just a folio_put() or put_page(). Signed-off-by: David Howells cc: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- drivers/net/ethernet/mediatek/mtk_wed_wo.c | 15 +-- drivers/nvme/host/tcp.c | 7 +- drivers/nvme/target/tcp.c | 5 +- include/linux/gfp.h | 1 + include/linux/mm_types.h | 13 +-- mm/page_frag_alloc.c | 101 +++++++++++---------- 6 files changed, 63 insertions(+), 79 deletions(-) diff --git a/drivers/net/ethernet/mediatek/mtk_wed_wo.c b/drivers/net/ether= net/mediatek/mtk_wed_wo.c index 69fba29055e9..6ce532217777 100644 --- a/drivers/net/ethernet/mediatek/mtk_wed_wo.c +++ b/drivers/net/ethernet/mediatek/mtk_wed_wo.c @@ -286,7 +286,6 @@ mtk_wed_wo_queue_free(struct mtk_wed_wo *wo, struct mtk= _wed_wo_queue *q) static void mtk_wed_wo_queue_tx_clean(struct mtk_wed_wo *wo, struct mtk_wed_wo_queue *= q) { - struct page *page; int i; =20 for (i =3D 0; i < q->n_desc; i++) { @@ -298,12 +297,7 @@ mtk_wed_wo_queue_tx_clean(struct mtk_wed_wo *wo, struc= t mtk_wed_wo_queue *q) entry->buf =3D NULL; } =20 - if (!q->cache.va) - return; - - page =3D virt_to_page(q->cache.va); - __page_frag_cache_drain(page, q->cache.pagecnt_bias); - memset(&q->cache, 0, sizeof(q->cache)); + page_frag_cache_clear(&q->cache); } =20 static void @@ -320,12 +314,7 @@ mtk_wed_wo_queue_rx_clean(struct mtk_wed_wo *wo, struc= t mtk_wed_wo_queue *q) skb_free_frag(buf); } =20 - if (!q->cache.va) - return; - - page =3D virt_to_page(q->cache.va); - __page_frag_cache_drain(page, q->cache.pagecnt_bias); - memset(&q->cache, 0, sizeof(q->cache)); + page_frag_cache_clear(&q->cache); } =20 static void diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c index 42c0598c31f2..76f12ac714b0 100644 --- a/drivers/nvme/host/tcp.c +++ b/drivers/nvme/host/tcp.c @@ -1323,12 +1323,7 @@ static void nvme_tcp_free_queue(struct nvme_ctrl *nc= trl, int qid) if (queue->hdr_digest || queue->data_digest) nvme_tcp_free_crypto(queue); =20 - if (queue->pf_cache.va) { - page =3D virt_to_head_page(queue->pf_cache.va); - __page_frag_cache_drain(page, queue->pf_cache.pagecnt_bias); - queue->pf_cache.va =3D NULL; - } - + page_frag_cache_clear(&queue->pf_cache); noreclaim_flag =3D memalloc_noreclaim_save(); sock_release(queue->sock); memalloc_noreclaim_restore(noreclaim_flag); diff --git a/drivers/nvme/target/tcp.c b/drivers/nvme/target/tcp.c index 66e8f9fd0ca7..ae871c31cf00 100644 --- a/drivers/nvme/target/tcp.c +++ b/drivers/nvme/target/tcp.c @@ -1438,7 +1438,6 @@ static void nvmet_tcp_free_cmd_data_in_buffers(struct= nvmet_tcp_queue *queue) =20 static void nvmet_tcp_release_queue_work(struct work_struct *w) { - struct page *page; struct nvmet_tcp_queue *queue =3D container_of(w, struct nvmet_tcp_queue, release_work); =20 @@ -1460,9 +1459,7 @@ static void nvmet_tcp_release_queue_work(struct work_= struct *w) if (queue->hdr_digest || queue->data_digest) nvmet_tcp_free_crypto(queue); ida_free(&nvmet_tcp_queue_ida, queue->idx); - - page =3D virt_to_head_page(queue->pf_cache.va); - __page_frag_cache_drain(page, queue->pf_cache.pagecnt_bias); + page_frag_cache_clear(&queue->pf_cache); kfree(queue); } =20 diff --git a/include/linux/gfp.h b/include/linux/gfp.h index 65a78773dcca..5e15384798eb 100644 --- a/include/linux/gfp.h +++ b/include/linux/gfp.h @@ -313,6 +313,7 @@ static inline void *page_frag_alloc(struct page_frag_ca= che *nc, { return page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u); } +void page_frag_cache_clear(struct page_frag_cache *nc); =20 extern void page_frag_free(void *addr); =20 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 0722859c3647..49a70b3f44a9 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -420,18 +420,13 @@ static inline void *folio_get_private(struct folio *f= olio) } =20 struct page_frag_cache { - void * va; -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - __u16 offset; - __u16 size; -#else - __u32 offset; -#endif + struct folio *folio; + unsigned int offset; /* we maintain a pagecount bias, so that we dont dirty cache line * containing page->_refcount every time we allocate a fragment. */ - unsigned int pagecnt_bias; - bool pfmemalloc; + unsigned int pagecnt_bias; + bool pfmemalloc; }; =20 typedef unsigned long vm_flags_t; diff --git a/mm/page_frag_alloc.c b/mm/page_frag_alloc.c index bee95824ef8f..9b138cb0e3a4 100644 --- a/mm/page_frag_alloc.c +++ b/mm/page_frag_alloc.c @@ -16,88 +16,95 @@ #include #include =20 -static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, - gfp_t gfp_mask) +/* + * Allocate a new folio for the frag cache. + */ +static struct folio *page_frag_cache_refill(struct page_frag_cache *nc, + gfp_t gfp_mask) { - struct page *page =3D NULL; + struct folio *folio =3D NULL; gfp_t gfp =3D gfp_mask; =20 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - gfp_mask |=3D __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY | - __GFP_NOMEMALLOC; - page =3D alloc_pages_node(NUMA_NO_NODE, gfp_mask, - PAGE_FRAG_CACHE_MAX_ORDER); - nc->size =3D page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; + gfp_mask |=3D __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC; + folio =3D folio_alloc(gfp_mask, PAGE_FRAG_CACHE_MAX_ORDER); #endif - if (unlikely(!page)) - page =3D alloc_pages_node(NUMA_NO_NODE, gfp, 0); - - nc->va =3D page ? page_address(page) : NULL; + if (unlikely(!folio)) + folio =3D folio_alloc(gfp, 0); =20 - return page; + if (folio) + nc->folio =3D folio; + return folio; } =20 void __page_frag_cache_drain(struct page *page, unsigned int count) { - VM_BUG_ON_PAGE(page_ref_count(page) =3D=3D 0, page); + struct folio *folio =3D page_folio(page); =20 - if (page_ref_sub_and_test(page, count - 1)) - __free_pages(page, compound_order(page)); + VM_BUG_ON_FOLIO(folio_ref_count(folio) =3D=3D 0, folio); + + folio_put_refs(folio, count); } EXPORT_SYMBOL(__page_frag_cache_drain); =20 +void page_frag_cache_clear(struct page_frag_cache *nc) +{ + struct folio *folio =3D nc->folio; + + if (folio) { + VM_BUG_ON_FOLIO(folio_ref_count(folio) =3D=3D 0, folio); + folio_put_refs(folio, nc->pagecnt_bias); + nc->folio =3D NULL; + } + +} +EXPORT_SYMBOL(page_frag_cache_clear); + void *page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz, gfp_t gfp_mask, unsigned int align_mask) { - unsigned int size =3D PAGE_SIZE; - struct page *page; - int offset; + struct folio *folio =3D nc->folio; + size_t offset; =20 - if (unlikely(!nc->va)) { + if (unlikely(!folio)) { refill: - page =3D __page_frag_cache_refill(nc, gfp_mask); - if (!page) + folio =3D page_frag_cache_refill(nc, gfp_mask); + if (!folio) return NULL; =20 -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size =3D nc->size; -#endif /* Even if we own the page, we do not use atomic_set(). * This would break get_page_unless_zero() users. */ - page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); + folio_ref_add(folio, PAGE_FRAG_CACHE_MAX_SIZE); =20 /* reset page count bias and offset to start of new frag */ - nc->pfmemalloc =3D page_is_pfmemalloc(page); + nc->pfmemalloc =3D folio_is_pfmemalloc(folio); nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset =3D size; + nc->offset =3D folio_size(folio); } =20 - offset =3D nc->offset - fragsz; - if (unlikely(offset < 0)) { - page =3D virt_to_page(nc->va); - - if (page_ref_count(page) !=3D nc->pagecnt_bias) + offset =3D nc->offset; + if (unlikely(fragsz > offset)) { + /* Reuse the folio if everyone we gave it to has finished with it. */ + if (!folio_ref_sub_and_test(folio, nc->pagecnt_bias)) { + nc->folio =3D NULL; goto refill; + } + if (unlikely(nc->pfmemalloc)) { - page_ref_sub(page, nc->pagecnt_bias - 1); - __free_pages(page, compound_order(page)); + __folio_put(folio); + nc->folio =3D NULL; goto refill; } =20 -#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - /* if size can vary use size else just use PAGE_SIZE */ - size =3D nc->size; -#endif /* OK, page count is 0, we can safely set it */ - set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); + folio_set_count(folio, PAGE_FRAG_CACHE_MAX_SIZE + 1); =20 /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; - offset =3D size - fragsz; - if (unlikely(offset < 0)) { + offset =3D folio_size(folio); + if (unlikely(fragsz > offset)) { /* * The caller is trying to allocate a fragment * with fragsz > PAGE_SIZE but the cache isn't big @@ -107,15 +114,17 @@ void *page_frag_alloc_align(struct page_frag_cache *n= c, * it could make memory pressure worse * so we simply return NULL here. */ + nc->offset =3D offset; return NULL; } } =20 nc->pagecnt_bias--; + offset -=3D fragsz; offset &=3D align_mask; nc->offset =3D offset; =20 - return nc->va + offset; + return folio_address(folio) + offset; } EXPORT_SYMBOL(page_frag_alloc_align); =20 @@ -124,8 +133,6 @@ EXPORT_SYMBOL(page_frag_alloc_align); */ void page_frag_free(void *addr) { - struct page *page =3D virt_to_head_page(addr); - - __free_pages(page, compound_order(page)); + folio_put(virt_to_folio(addr)); } EXPORT_SYMBOL(page_frag_free); From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1B06CC76188 for ; Wed, 5 Apr 2023 16:55:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229542AbjDEQzn (ORCPT ); Wed, 5 Apr 2023 12:55:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232442AbjDEQz3 (ORCPT ); Wed, 5 Apr 2023 12:55:29 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6DA845587 for ; Wed, 5 Apr 2023 09:54:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713644; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IxFOI8BlLlR7NCyVkzu+oUkui6zaCPq4znyAUScBVv0=; b=No/fYYeobsKVuvgs1Ng071i/uMTSGhtuBwQ9RnMSpNoCoNC017VmFLxm+W6DGEnGg77Uk+ E+WzrYgTKwFbR/jVaUMlacEMpf+U3XdKl8HOcIwCflSx/a7eLDo76fO7gn34VQY809QWYT VYMtuhW5RX5OYgMDs5axlFWMZHUQIaE= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-499-j5Q8MndgOw2Vu35Xrvwpvg-1; Wed, 05 Apr 2023 12:54:02 -0400 X-MC-Unique: j5Q8MndgOw2Vu35Xrvwpvg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 97712858F09; Wed, 5 Apr 2023 16:54:00 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id E7A24140EBF4; Wed, 5 Apr 2023 16:53:56 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Lorenzo Bianconi , Felix Fietkau , John Crispin , Sean Wang , Mark Lee , Keith Busch , Christoph Hellwig , Sagi Grimberg , Chaitanya Kulkarni , linux-nvme@lists.infradead.org, linux-mediatek@lists.infradead.org Subject: [PATCH net-next v4 05/20] mm: Make the page_frag_cache allocator use per-cpu Date: Wed, 5 Apr 2023 17:53:24 +0100 Message-Id: <20230405165339.3468808-6-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Make the page_frag_cache allocator have a separate allocation bucket for each cpu to avoid racing. This means that no lock is required, other than preempt disablement, to allocate from it, though if a softirq wants to access it, then softirq disablement will need to be added. Make the NVMe and mediatek drivers pass in NULL to page_frag_cache() and use the default allocation buckets rather than defining their own. Signed-off-by: David Howells cc: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Lorenzo Bianconi cc: Felix Fietkau cc: John Crispin cc: Sean Wang cc: Mark Lee cc: Keith Busch cc: Christoph Hellwig cc: Sagi Grimberg cc: Chaitanya Kulkarni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org cc: linux-nvme@lists.infradead.org cc: linux-mediatek@lists.infradead.org --- drivers/net/ethernet/mediatek/mtk_wed_wo.c | 8 +- drivers/net/ethernet/mediatek/mtk_wed_wo.h | 2 - drivers/nvme/host/tcp.c | 14 +- drivers/nvme/target/tcp.c | 19 +- include/linux/gfp.h | 18 +- mm/page_frag_alloc.c | 195 ++++++++++++++------- net/core/skbuff.c | 32 ++-- 7 files changed, 164 insertions(+), 124 deletions(-) diff --git a/drivers/net/ethernet/mediatek/mtk_wed_wo.c b/drivers/net/ether= net/mediatek/mtk_wed_wo.c index 6ce532217777..859f34447f2f 100644 --- a/drivers/net/ethernet/mediatek/mtk_wed_wo.c +++ b/drivers/net/ethernet/mediatek/mtk_wed_wo.c @@ -143,7 +143,7 @@ mtk_wed_wo_queue_refill(struct mtk_wed_wo *wo, struct m= tk_wed_wo_queue *q, dma_addr_t addr; void *buf; =20 - buf =3D page_frag_alloc(&q->cache, q->buf_size, GFP_ATOMIC); + buf =3D page_frag_alloc(NULL, q->buf_size, GFP_ATOMIC); if (!buf) break; =20 @@ -296,15 +296,11 @@ mtk_wed_wo_queue_tx_clean(struct mtk_wed_wo *wo, stru= ct mtk_wed_wo_queue *q) skb_free_frag(entry->buf); entry->buf =3D NULL; } - - page_frag_cache_clear(&q->cache); } =20 static void mtk_wed_wo_queue_rx_clean(struct mtk_wed_wo *wo, struct mtk_wed_wo_queue *= q) { - struct page *page; - for (;;) { void *buf =3D mtk_wed_wo_dequeue(wo, q, NULL, true); =20 @@ -313,8 +309,6 @@ mtk_wed_wo_queue_rx_clean(struct mtk_wed_wo *wo, struct= mtk_wed_wo_queue *q) =20 skb_free_frag(buf); } - - page_frag_cache_clear(&q->cache); } =20 static void diff --git a/drivers/net/ethernet/mediatek/mtk_wed_wo.h b/drivers/net/ether= net/mediatek/mtk_wed_wo.h index dbcf42ce9173..6f940db67fb8 100644 --- a/drivers/net/ethernet/mediatek/mtk_wed_wo.h +++ b/drivers/net/ethernet/mediatek/mtk_wed_wo.h @@ -210,8 +210,6 @@ struct mtk_wed_wo_queue_entry { struct mtk_wed_wo_queue { struct mtk_wed_wo_queue_regs regs; =20 - struct page_frag_cache cache; - struct mtk_wed_wo_queue_desc *desc; dma_addr_t desc_dma; =20 diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c index 76f12ac714b0..5a92236db92a 100644 --- a/drivers/nvme/host/tcp.c +++ b/drivers/nvme/host/tcp.c @@ -147,8 +147,6 @@ struct nvme_tcp_queue { __le32 exp_ddgst; __le32 recv_ddgst; =20 - struct page_frag_cache pf_cache; - void (*state_change)(struct sock *); void (*data_ready)(struct sock *); void (*write_space)(struct sock *); @@ -482,9 +480,8 @@ static int nvme_tcp_init_request(struct blk_mq_tag_set = *set, struct nvme_tcp_queue *queue =3D &ctrl->queues[queue_idx]; u8 hdgst =3D nvme_tcp_hdgst_len(queue); =20 - req->pdu =3D page_frag_alloc(&queue->pf_cache, - sizeof(struct nvme_tcp_cmd_pdu) + hdgst, - GFP_KERNEL | __GFP_ZERO); + req->pdu =3D page_frag_alloc(NULL, sizeof(struct nvme_tcp_cmd_pdu) + hdgs= t, + GFP_KERNEL | __GFP_ZERO); if (!req->pdu) return -ENOMEM; =20 @@ -1300,9 +1297,8 @@ static int nvme_tcp_alloc_async_req(struct nvme_tcp_c= trl *ctrl) struct nvme_tcp_request *async =3D &ctrl->async_req; u8 hdgst =3D nvme_tcp_hdgst_len(queue); =20 - async->pdu =3D page_frag_alloc(&queue->pf_cache, - sizeof(struct nvme_tcp_cmd_pdu) + hdgst, - GFP_KERNEL | __GFP_ZERO); + async->pdu =3D page_frag_alloc(NULL, sizeof(struct nvme_tcp_cmd_pdu) + hd= gst, + GFP_KERNEL | __GFP_ZERO); if (!async->pdu) return -ENOMEM; =20 @@ -1312,7 +1308,6 @@ static int nvme_tcp_alloc_async_req(struct nvme_tcp_c= trl *ctrl) =20 static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid) { - struct page *page; struct nvme_tcp_ctrl *ctrl =3D to_tcp_ctrl(nctrl); struct nvme_tcp_queue *queue =3D &ctrl->queues[qid]; unsigned int noreclaim_flag; @@ -1323,7 +1318,6 @@ static void nvme_tcp_free_queue(struct nvme_ctrl *nct= rl, int qid) if (queue->hdr_digest || queue->data_digest) nvme_tcp_free_crypto(queue); =20 - page_frag_cache_clear(&queue->pf_cache); noreclaim_flag =3D memalloc_noreclaim_save(); sock_release(queue->sock); memalloc_noreclaim_restore(noreclaim_flag); diff --git a/drivers/nvme/target/tcp.c b/drivers/nvme/target/tcp.c index ae871c31cf00..d6cc557cc539 100644 --- a/drivers/nvme/target/tcp.c +++ b/drivers/nvme/target/tcp.c @@ -143,8 +143,6 @@ struct nvmet_tcp_queue { =20 struct nvmet_tcp_cmd connect; =20 - struct page_frag_cache pf_cache; - void (*data_ready)(struct sock *); void (*state_change)(struct sock *); void (*write_space)(struct sock *); @@ -1312,25 +1310,25 @@ static int nvmet_tcp_alloc_cmd(struct nvmet_tcp_que= ue *queue, c->queue =3D queue; c->req.port =3D queue->port->nport; =20 - c->cmd_pdu =3D page_frag_alloc(&queue->pf_cache, - sizeof(*c->cmd_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); + c->cmd_pdu =3D page_frag_alloc(NULL, sizeof(*c->cmd_pdu) + hdgst, + GFP_KERNEL | __GFP_ZERO); if (!c->cmd_pdu) return -ENOMEM; c->req.cmd =3D &c->cmd_pdu->cmd; =20 - c->rsp_pdu =3D page_frag_alloc(&queue->pf_cache, - sizeof(*c->rsp_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); + c->rsp_pdu =3D page_frag_alloc(NULL, sizeof(*c->rsp_pdu) + hdgst, + GFP_KERNEL | __GFP_ZERO); if (!c->rsp_pdu) goto out_free_cmd; c->req.cqe =3D &c->rsp_pdu->cqe; =20 - c->data_pdu =3D page_frag_alloc(&queue->pf_cache, - sizeof(*c->data_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); + c->data_pdu =3D page_frag_alloc(NULL, sizeof(*c->data_pdu) + hdgst, + GFP_KERNEL | __GFP_ZERO); if (!c->data_pdu) goto out_free_rsp; =20 - c->r2t_pdu =3D page_frag_alloc(&queue->pf_cache, - sizeof(*c->r2t_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO); + c->r2t_pdu =3D page_frag_alloc(NULL, sizeof(*c->r2t_pdu) + hdgst, + GFP_KERNEL | __GFP_ZERO); if (!c->r2t_pdu) goto out_free_data; =20 @@ -1459,7 +1457,6 @@ static void nvmet_tcp_release_queue_work(struct work_= struct *w) if (queue->hdr_digest || queue->data_digest) nvmet_tcp_free_crypto(queue); ida_free(&nvmet_tcp_queue_ida, queue->idx); - page_frag_cache_clear(&queue->pf_cache); kfree(queue); } =20 diff --git a/include/linux/gfp.h b/include/linux/gfp.h index 5e15384798eb..b208ca315882 100644 --- a/include/linux/gfp.h +++ b/include/linux/gfp.h @@ -304,16 +304,18 @@ extern void free_pages(unsigned long addr, unsigned i= nt order); =20 struct page_frag_cache; extern void __page_frag_cache_drain(struct page *page, unsigned int count); -extern void *page_frag_alloc_align(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask, - unsigned int align_mask); - -static inline void *page_frag_alloc(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask) +extern void *page_frag_alloc_align(struct page_frag_cache __percpu *frag_c= ache, + size_t fragsz, gfp_t gfp, + unsigned long align_mask); +extern void *page_frag_memdup(struct page_frag_cache __percpu *frag_cache, + const void *p, size_t fragsz, gfp_t gfp, + unsigned long align_mask); + +static inline void *page_frag_alloc(struct page_frag_cache __percpu *frag_= cache, + size_t fragsz, gfp_t gfp) { - return page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u); + return page_frag_alloc_align(frag_cache, fragsz, gfp, ULONG_MAX); } -void page_frag_cache_clear(struct page_frag_cache *nc); =20 extern void page_frag_free(void *addr); =20 diff --git a/mm/page_frag_alloc.c b/mm/page_frag_alloc.c index 9b138cb0e3a4..7844398afe26 100644 --- a/mm/page_frag_alloc.c +++ b/mm/page_frag_alloc.c @@ -16,25 +16,23 @@ #include #include =20 +static DEFINE_PER_CPU(struct page_frag_cache, page_frag_default_allocator); + /* * Allocate a new folio for the frag cache. */ -static struct folio *page_frag_cache_refill(struct page_frag_cache *nc, - gfp_t gfp_mask) +static struct folio *page_frag_cache_refill(gfp_t gfp) { - struct folio *folio =3D NULL; - gfp_t gfp =3D gfp_mask; + struct folio *folio; =20 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) - gfp_mask |=3D __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC; - folio =3D folio_alloc(gfp_mask, PAGE_FRAG_CACHE_MAX_ORDER); + folio =3D folio_alloc(gfp | __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALL= OC, + PAGE_FRAG_CACHE_MAX_ORDER); + if (folio) + return folio; #endif - if (unlikely(!folio)) - folio =3D folio_alloc(gfp, 0); =20 - if (folio) - nc->folio =3D folio; - return folio; + return folio_alloc(gfp, 0); } =20 void __page_frag_cache_drain(struct page *page, unsigned int count) @@ -47,54 +45,68 @@ void __page_frag_cache_drain(struct page *page, unsigne= d int count) } EXPORT_SYMBOL(__page_frag_cache_drain); =20 -void page_frag_cache_clear(struct page_frag_cache *nc) -{ - struct folio *folio =3D nc->folio; - - if (folio) { - VM_BUG_ON_FOLIO(folio_ref_count(folio) =3D=3D 0, folio); - folio_put_refs(folio, nc->pagecnt_bias); - nc->folio =3D NULL; - } - -} -EXPORT_SYMBOL(page_frag_cache_clear); - -void *page_frag_alloc_align(struct page_frag_cache *nc, - unsigned int fragsz, gfp_t gfp_mask, - unsigned int align_mask) +/** + * page_frag_alloc_align - Allocate some memory for use in zerocopy + * @frag_cache: The frag cache to use (or NULL for the default) + * @fragsz: The size of the fragment desired + * @gfp: Allocation flags under which to make an allocation + * @align_mask: The required alignment + * + * Allocate some memory for use with zerocopy where protocol bits have to = be + * mixed in with spliced/zerocopied data. Unlike memory allocated from the + * slab, this memory's lifetime is purely dependent on the folio's refcoun= t. + * + * The way it works is that a folio is allocated and fragments are broken = off + * sequentially and returned to the caller with a ref until the folio no l= onger + * has enough spare space - at which point the allocator's ref is dropped = and a + * new folio is allocated. The folio remains in existence until the last = ref + * held by, say, an sk_buff is discarded and then the page is returned to = the + * page allocator. + * + * Returns a pointer to the memory on success and -ENOMEM on allocation + * failure. + * + * The allocated memory should be disposed of with folio_put(). + */ +void *page_frag_alloc_align(struct page_frag_cache __percpu *frag_cache, + size_t fragsz, gfp_t gfp, unsigned long align_mask) { - struct folio *folio =3D nc->folio; + struct page_frag_cache *nc; + struct folio *folio, *spare =3D NULL; size_t offset; + void *p; =20 - if (unlikely(!folio)) { -refill: - folio =3D page_frag_cache_refill(nc, gfp_mask); - if (!folio) - return NULL; + if (!frag_cache) + frag_cache =3D &page_frag_default_allocator; + if (WARN_ON_ONCE(fragsz =3D=3D 0)) + fragsz =3D 1; + align_mask &=3D ~3UL; =20 - /* Even if we own the page, we do not use atomic_set(). - * This would break get_page_unless_zero() users. - */ - folio_ref_add(folio, PAGE_FRAG_CACHE_MAX_SIZE); - - /* reset page count bias and offset to start of new frag */ - nc->pfmemalloc =3D folio_is_pfmemalloc(folio); - nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; - nc->offset =3D folio_size(folio); + nc =3D get_cpu_ptr(frag_cache); +reload: + folio =3D nc->folio; + offset =3D nc->offset; +try_again: + + /* Make the allocation if there's sufficient space. */ + if (fragsz <=3D offset) { + nc->pagecnt_bias--; + offset =3D (offset - fragsz) & align_mask; + nc->offset =3D offset; + p =3D folio_address(folio) + offset; + put_cpu_ptr(frag_cache); + if (spare) + folio_put(spare); + return p; } =20 - offset =3D nc->offset; - if (unlikely(fragsz > offset)) { - /* Reuse the folio if everyone we gave it to has finished with it. */ - if (!folio_ref_sub_and_test(folio, nc->pagecnt_bias)) { - nc->folio =3D NULL; + /* Insufficient space - see if we can refurbish the current folio. */ + if (folio) { + if (!folio_ref_sub_and_test(folio, nc->pagecnt_bias)) goto refill; - } =20 if (unlikely(nc->pfmemalloc)) { __folio_put(folio); - nc->folio =3D NULL; goto refill; } =20 @@ -104,27 +116,56 @@ void *page_frag_alloc_align(struct page_frag_cache *n= c, /* reset page count bias and offset to start of new frag */ nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; offset =3D folio_size(folio); - if (unlikely(fragsz > offset)) { - /* - * The caller is trying to allocate a fragment - * with fragsz > PAGE_SIZE but the cache isn't big - * enough to satisfy the request, this may - * happen in low memory conditions. - * We don't release the cache page because - * it could make memory pressure worse - * so we simply return NULL here. - */ - nc->offset =3D offset; + if (unlikely(fragsz > offset)) + goto frag_too_big; + goto try_again; + } + +refill: + if (!spare) { + nc->folio =3D NULL; + put_cpu_ptr(frag_cache); + + spare =3D page_frag_cache_refill(gfp); + if (!spare) return NULL; - } + + nc =3D get_cpu_ptr(frag_cache); + /* We may now be on a different cpu and/or someone else may + * have refilled it + */ + nc->pfmemalloc =3D folio_is_pfmemalloc(spare); + if (nc->folio) + goto reload; } =20 - nc->pagecnt_bias--; - offset -=3D fragsz; - offset &=3D align_mask; + nc->folio =3D spare; + folio =3D spare; + spare =3D NULL; + + /* Even if we own the page, we do not use atomic_set(). This would + * break get_page_unless_zero() users. + */ + folio_ref_add(folio, PAGE_FRAG_CACHE_MAX_SIZE); + + /* Reset page count bias and offset to start of new frag */ + nc->pagecnt_bias =3D PAGE_FRAG_CACHE_MAX_SIZE + 1; + offset =3D folio_size(folio); + goto try_again; + +frag_too_big: + /* + * The caller is trying to allocate a fragment with fragsz > PAGE_SIZE + * but the cache isn't big enough to satisfy the request, this may + * happen in low memory conditions. We don't release the cache page + * because it could make memory pressure worse so we simply return NULL + * here. + */ nc->offset =3D offset; - - return folio_address(folio) + offset; + put_cpu_ptr(frag_cache); + if (spare) + folio_put(spare); + return NULL; } EXPORT_SYMBOL(page_frag_alloc_align); =20 @@ -136,3 +177,25 @@ void page_frag_free(void *addr) folio_put(virt_to_folio(addr)); } EXPORT_SYMBOL(page_frag_free); + +/** + * page_frag_memdup - Allocate a page fragment and duplicate some data int= o it + * @frag_cache: The frag cache to use (or NULL for the default) + * @fragsz: The amount of memory to copy (maximum 1/2 page). + * @p: The source data to copy + * @gfp: Allocation flags under which to make an allocation + * @align_mask: The required alignment + */ +void *page_frag_memdup(struct page_frag_cache __percpu *frag_cache, + const void *p, size_t fragsz, gfp_t gfp, + unsigned long align_mask) +{ + void *q; + + q =3D page_frag_alloc_align(frag_cache, fragsz, gfp, align_mask); + if (!q) + return q; + + return memcpy(q, p, fragsz); +} +EXPORT_SYMBOL(page_frag_memdup); diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 050a875d09c5..3d05ed64b606 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -222,13 +222,13 @@ static void *page_frag_alloc_1k(struct page_frag_1k *= nc, gfp_t gfp_mask) #endif =20 struct napi_alloc_cache { - struct page_frag_cache page; struct page_frag_1k page_small; unsigned int skb_count; void *skb_cache[NAPI_SKB_CACHE_SIZE]; }; =20 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); +static DEFINE_PER_CPU(struct page_frag_cache, napi_frag_cache); static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); =20 /* Double check that napi_get_frags() allocates skbs with @@ -250,11 +250,9 @@ void napi_get_frags_check(struct napi_struct *napi) =20 void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) { - struct napi_alloc_cache *nc =3D this_cpu_ptr(&napi_alloc_cache); - fragsz =3D SKB_DATA_ALIGN(fragsz); =20 - return page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); + return page_frag_alloc_align(&napi_frag_cache, fragsz, GFP_ATOMIC, align_= mask); } EXPORT_SYMBOL(__napi_alloc_frag_align); =20 @@ -264,15 +262,12 @@ void *__netdev_alloc_frag_align(unsigned int fragsz, = unsigned int align_mask) =20 fragsz =3D SKB_DATA_ALIGN(fragsz); if (in_hardirq() || irqs_disabled()) { - struct page_frag_cache *nc =3D this_cpu_ptr(&netdev_alloc_cache); - - data =3D page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask); + data =3D page_frag_alloc_align(&netdev_alloc_cache, + fragsz, GFP_ATOMIC, align_mask); } else { - struct napi_alloc_cache *nc; - local_bh_disable(); - nc =3D this_cpu_ptr(&napi_alloc_cache); - data =3D page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask= ); + data =3D page_frag_alloc_align(&napi_frag_cache, + fragsz, GFP_ATOMIC, align_mask); local_bh_enable(); } return data; @@ -652,7 +647,6 @@ EXPORT_SYMBOL(__alloc_skb); struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int le= n, gfp_t gfp_mask) { - struct page_frag_cache *nc; struct sk_buff *skb; bool pfmemalloc; void *data; @@ -677,14 +671,12 @@ struct sk_buff *__netdev_alloc_skb(struct net_device = *dev, unsigned int len, gfp_mask |=3D __GFP_MEMALLOC; =20 if (in_hardirq() || irqs_disabled()) { - nc =3D this_cpu_ptr(&netdev_alloc_cache); - data =3D page_frag_alloc(nc, len, gfp_mask); - pfmemalloc =3D nc->pfmemalloc; + data =3D page_frag_alloc(&netdev_alloc_cache, len, gfp_mask); + pfmemalloc =3D folio_is_pfmemalloc(virt_to_folio(data)); } else { local_bh_disable(); - nc =3D this_cpu_ptr(&napi_alloc_cache.page); - data =3D page_frag_alloc(nc, len, gfp_mask); - pfmemalloc =3D nc->pfmemalloc; + data =3D page_frag_alloc(&napi_frag_cache, len, gfp_mask); + pfmemalloc =3D folio_is_pfmemalloc(virt_to_folio(data)); local_bh_enable(); } =20 @@ -772,8 +764,8 @@ struct sk_buff *__napi_alloc_skb(struct napi_struct *na= pi, unsigned int len, } else { len =3D SKB_HEAD_ALIGN(len); =20 - data =3D page_frag_alloc(&nc->page, len, gfp_mask); - pfmemalloc =3D nc->page.pfmemalloc; + data =3D page_frag_alloc(&napi_frag_cache, len, gfp_mask); + pfmemalloc =3D folio_is_pfmemalloc(virt_to_folio(data)); } =20 if (unlikely(!data)) From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DCC97C76188 for ; Wed, 5 Apr 2023 16:55:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233169AbjDEQzh (ORCPT ); Wed, 5 Apr 2023 12:55:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233625AbjDEQz2 (ORCPT ); Wed, 5 Apr 2023 12:55:28 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F1E559DD for ; Wed, 5 Apr 2023 09:54:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713645; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gNgTI4ct3B9VKtnPZ5NdE6sXYdaZCQGxwVn8DWUxWC8=; b=Onnh4voqwJ0DikizEdOJZjJKYk6emUMyE2PDfGwWjLGPD9h3zJ45IdPevrEo8GUoAkNCkz fIb0FMOSLcNGKLrRMfu6/DNubinUqAgH7XXQC7EAU9zqrHasSb+z839eO2rU/QZpLonTQ8 HH5UMPC2sdNMTMXWjXpB2dFOO2iAaSg= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-652-HgV9LYxTO12prleccTyfZw-1; Wed, 05 Apr 2023 12:54:04 -0400 X-MC-Unique: HgV9LYxTO12prleccTyfZw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5CFB588904B; Wed, 5 Apr 2023 16:54:03 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 53E15C1602A; Wed, 5 Apr 2023 16:54:01 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH net-next v4 06/20] tcp: Support MSG_SPLICE_PAGES Date: Wed, 5 Apr 2023 17:53:25 +0100 Message-Id: <20230405165339.3468808-7-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Make TCP's sendmsg() support MSG_SPLICE_PAGES. This causes pages to be spliced from the source iterator. This allows ->sendpage() to be replaced by something that can handle multiple multipage folios in a single transaction. Signed-off-by: David Howells cc: Eric Dumazet cc: "David S. Miller" cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- net/ipv4/tcp.c | 67 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 7 deletions(-) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index fd68d49490f2..510bacc7ce7b 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -1221,7 +1221,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr= *msg, size_t size) int flags, err, copied =3D 0; int mss_now =3D 0, size_goal, copied_syn =3D 0; int process_backlog =3D 0; - bool zc =3D false; + int zc =3D 0; long timeo; =20 flags =3D msg->msg_flags; @@ -1232,17 +1232,22 @@ int tcp_sendmsg_locked(struct sock *sk, struct msgh= dr *msg, size_t size) if (msg->msg_ubuf) { uarg =3D msg->msg_ubuf; net_zcopy_get(uarg); - zc =3D sk->sk_route_caps & NETIF_F_SG; + if (sk->sk_route_caps & NETIF_F_SG) + zc =3D 1; } else if (sock_flag(sk, SOCK_ZEROCOPY)) { uarg =3D msg_zerocopy_realloc(sk, size, skb_zcopy(skb)); if (!uarg) { err =3D -ENOBUFS; goto out_err; } - zc =3D sk->sk_route_caps & NETIF_F_SG; - if (!zc) + if (sk->sk_route_caps & NETIF_F_SG) + zc =3D 1; + else uarg_to_msgzc(uarg)->zerocopy =3D 0; } + } else if (unlikely(msg->msg_flags & MSG_SPLICE_PAGES) && size) { + if (sk->sk_route_caps & NETIF_F_SG) + zc =3D 2; } =20 if (unlikely(flags & MSG_FASTOPEN || inet_sk(sk)->defer_connect) && @@ -1305,7 +1310,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr= *msg, size_t size) goto do_error; =20 while (msg_data_left(msg)) { - int copy =3D 0; + ssize_t copy =3D 0; =20 skb =3D tcp_write_queue_tail(sk); if (skb) @@ -1346,7 +1351,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr= *msg, size_t size) if (copy > msg_data_left(msg)) copy =3D msg_data_left(msg); =20 - if (!zc) { + if (zc =3D=3D 0) { bool merge =3D true; int i =3D skb_shinfo(skb)->nr_frags; struct page_frag *pfrag =3D sk_page_frag(sk); @@ -1391,7 +1396,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr= *msg, size_t size) page_ref_inc(pfrag->page); } pfrag->offset +=3D copy; - } else { + } else if (zc =3D=3D 1) { /* First append to a fragless skb builds initial * pure zerocopy skb */ @@ -1412,6 +1417,54 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghd= r *msg, size_t size) if (err < 0) goto do_error; copy =3D err; + } else if (zc =3D=3D 2) { + /* Splice in data. */ + struct page *page =3D NULL, **pages =3D &page; + size_t off =3D 0, part; + bool can_coalesce; + int i =3D skb_shinfo(skb)->nr_frags; + + copy =3D iov_iter_extract_pages(&msg->msg_iter, &pages, + copy, 1, 0, &off); + if (copy <=3D 0) { + err =3D copy ?: -EIO; + goto do_error; + } + + can_coalesce =3D skb_can_coalesce(skb, i, page, off); + if (!can_coalesce && i >=3D READ_ONCE(sysctl_max_skb_frags)) { + tcp_mark_push(tp, skb); + iov_iter_revert(&msg->msg_iter, copy); + goto new_segment; + } + if (tcp_downgrade_zcopy_pure(sk, skb)) { + iov_iter_revert(&msg->msg_iter, copy); + goto wait_for_space; + } + + part =3D tcp_wmem_schedule(sk, copy); + iov_iter_revert(&msg->msg_iter, copy - part); + if (!part) + goto wait_for_space; + copy =3D part; + + if (can_coalesce) { + skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); + } else { + get_page(page); + skb_fill_page_desc_noacc(skb, i, page, off, copy); + } + page =3D NULL; + + if (!(flags & MSG_NO_SHARED_FRAGS)) + skb_shinfo(skb)->flags |=3D SKBFL_SHARED_FRAG; + + skb->len +=3D copy; + skb->data_len +=3D copy; + skb->truesize +=3D copy; + sk_wmem_queued_add(sk, copy); + sk_mem_charge(sk, copy); + } =20 if (!copied) From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F24B9C7619A for ; Wed, 5 Apr 2023 16:56:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233276AbjDEQ4f (ORCPT ); Wed, 5 Apr 2023 12:56:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233522AbjDEQ4K (ORCPT ); Wed, 5 Apr 2023 12:56:10 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A192D5FD4 for ; Wed, 5 Apr 2023 09:54:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713650; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5sjNMjWipBo1Z72oEKiWWK/H6upfHkIbrOvJ4ujx2jY=; b=XPMs70IoLNtx6GDjmlFrz+KoljN1ACaEjd+DtviiJ8XPnl3frWkzaOvXu/mPW4ez00BdVS vUm7iI4jl0+wRDqyLBV7v2ShissRTl93sICSzkjfKTezcPZ/NClLlPHUyOjAM8zcDQZTlv agEBWAHJ6kcwxezr/lS60d17K/eoPMc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-511-Ya2LXt7-OzuHeZYmbpex1g-1; Wed, 05 Apr 2023 12:54:07 -0400 X-MC-Unique: Ya2LXt7-OzuHeZYmbpex1g-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 00159885622; Wed, 5 Apr 2023 16:54:05 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id F2A0AC1602A; Wed, 5 Apr 2023 16:54:03 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH net-next v4 07/20] tcp: Make sendmsg(MSG_SPLICE_PAGES) copy unspliceable data Date: Wed, 5 Apr 2023 17:53:26 +0100 Message-Id: <20230405165339.3468808-8-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" If sendmsg() with MSG_SPLICE_PAGES encounters a page that shouldn't be spliced - a slab page, for instance, or one with a zero count - make tcp_sendmsg() copy it. Signed-off-by: David Howells cc: Eric Dumazet cc: "David S. Miller" cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- net/ipv4/tcp.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 510bacc7ce7b..238a8ad6527c 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -1418,10 +1418,10 @@ int tcp_sendmsg_locked(struct sock *sk, struct msgh= dr *msg, size_t size) goto do_error; copy =3D err; } else if (zc =3D=3D 2) { - /* Splice in data. */ + /* Splice in data if we can; copy if we can't. */ struct page *page =3D NULL, **pages =3D &page; size_t off =3D 0, part; - bool can_coalesce; + bool can_coalesce, put =3D false; int i =3D skb_shinfo(skb)->nr_frags; =20 copy =3D iov_iter_extract_pages(&msg->msg_iter, &pages, @@ -1448,12 +1448,34 @@ int tcp_sendmsg_locked(struct sock *sk, struct msgh= dr *msg, size_t size) goto wait_for_space; copy =3D part; =20 + if (!sendpage_ok(page)) { + const void *p =3D kmap_local_page(page); + void *q; + + q =3D page_frag_memdup(NULL, p + off, copy, + sk->sk_allocation, ULONG_MAX); + kunmap_local(p); + if (!q) { + iov_iter_revert(&msg->msg_iter, copy); + err =3D copy ?: -ENOMEM; + goto do_error; + } + page =3D virt_to_page(q); + off =3D offset_in_page(q); + put =3D true; + can_coalesce =3D false; + } + if (can_coalesce) { skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); } else { - get_page(page); + if (!put) + get_page(page); + put =3D false; skb_fill_page_desc_noacc(skb, i, page, off, copy); } + if (put) + put_page(page); page =3D NULL; =20 if (!(flags & MSG_NO_SHARED_FRAGS)) From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 36811C77B62 for ; Wed, 5 Apr 2023 16:56:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233876AbjDEQ4b (ORCPT ); Wed, 5 Apr 2023 12:56:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233276AbjDEQ4I (ORCPT ); Wed, 5 Apr 2023 12:56:08 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA6436182 for ; Wed, 5 Apr 2023 09:54:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713654; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uC0BSMRrW9srePF7/s9CMXAAGjZJUiQVDnNxN1lT1H0=; b=EURJ10eGT+25NcQAqT6NzU5B3iEPF7rYyiczJvh52PbGj6JdaAUMUMgnBgTzLECyID25Ql ZSU47Cdik9KJTTWWBVSk41xxHOqV0KkP5DeUrYm15lSG6lzWia8nrJ9UflZYXCejUkMHpP nmrL4p+XC9ma6P7nTjakgb7Jr5Asc8E= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-350-u4AwxYTMNSm_M09jQKer0w-1; Wed, 05 Apr 2023 12:54:09 -0400 X-MC-Unique: u4AwxYTMNSm_M09jQKer0w-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AB6A7101A531; Wed, 5 Apr 2023 16:54:08 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id AB8111400E6D; Wed, 5 Apr 2023 16:54:06 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH net-next v4 08/20] tcp: Convert do_tcp_sendpages() to use MSG_SPLICE_PAGES Date: Wed, 5 Apr 2023 17:53:27 +0100 Message-Id: <20230405165339.3468808-9-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Convert do_tcp_sendpages() to use sendmsg() with MSG_SPLICE_PAGES rather than directly splicing in the pages itself. do_tcp_sendpages() can then be inlined in subsequent patches into its callers. This allows ->sendpage() to be replaced by something that can handle multiple multipage folios in a single transaction. Signed-off-by: David Howells cc: Eric Dumazet cc: "David S. Miller" cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- net/ipv4/tcp.c | 158 +++---------------------------------------------- 1 file changed, 7 insertions(+), 151 deletions(-) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 238a8ad6527c..a8a4ace8b3da 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -972,163 +972,19 @@ static int tcp_wmem_schedule(struct sock *sk, int co= py) return min(copy, sk->sk_forward_alloc); } =20 -static struct sk_buff *tcp_build_frag(struct sock *sk, int size_goal, int = flags, - struct page *page, int offset, size_t *size) -{ - struct sk_buff *skb =3D tcp_write_queue_tail(sk); - struct tcp_sock *tp =3D tcp_sk(sk); - bool can_coalesce; - int copy, i; - - if (!skb || (copy =3D size_goal - skb->len) <=3D 0 || - !tcp_skb_can_collapse_to(skb)) { -new_segment: - if (!sk_stream_memory_free(sk)) - return NULL; - - skb =3D tcp_stream_alloc_skb(sk, 0, sk->sk_allocation, - tcp_rtx_and_write_queues_empty(sk)); - if (!skb) - return NULL; - -#ifdef CONFIG_TLS_DEVICE - skb->decrypted =3D !!(flags & MSG_SENDPAGE_DECRYPTED); -#endif - tcp_skb_entail(sk, skb); - copy =3D size_goal; - } - - if (copy > *size) - copy =3D *size; - - i =3D skb_shinfo(skb)->nr_frags; - can_coalesce =3D skb_can_coalesce(skb, i, page, offset); - if (!can_coalesce && i >=3D READ_ONCE(sysctl_max_skb_frags)) { - tcp_mark_push(tp, skb); - goto new_segment; - } - if (tcp_downgrade_zcopy_pure(sk, skb)) - return NULL; - - copy =3D tcp_wmem_schedule(sk, copy); - if (!copy) - return NULL; - - if (can_coalesce) { - skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); - } else { - get_page(page); - skb_fill_page_desc_noacc(skb, i, page, offset, copy); - } - - if (!(flags & MSG_NO_SHARED_FRAGS)) - skb_shinfo(skb)->flags |=3D SKBFL_SHARED_FRAG; - - skb->len +=3D copy; - skb->data_len +=3D copy; - skb->truesize +=3D copy; - sk_wmem_queued_add(sk, copy); - sk_mem_charge(sk, copy); - WRITE_ONCE(tp->write_seq, tp->write_seq + copy); - TCP_SKB_CB(skb)->end_seq +=3D copy; - tcp_skb_pcount_set(skb, 0); - - *size =3D copy; - return skb; -} - ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset, size_t size, int flags) { - struct tcp_sock *tp =3D tcp_sk(sk); - int mss_now, size_goal; - int err; - ssize_t copied; - long timeo =3D sock_sndtimeo(sk, flags & MSG_DONTWAIT); - - if (IS_ENABLED(CONFIG_DEBUG_VM) && - WARN_ONCE(!sendpage_ok(page), - "page must not be a Slab one and have page_count > 0")) - return -EINVAL; - - /* Wait for a connection to finish. One exception is TCP Fast Open - * (passive side) where data is allowed to be sent before a connection - * is fully established. - */ - if (((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) && - !tcp_passive_fastopen(sk)) { - err =3D sk_stream_wait_connect(sk, &timeo); - if (err !=3D 0) - goto out_err; - } + struct bio_vec bvec; + struct msghdr msg =3D { .msg_flags =3D flags | MSG_SPLICE_PAGES, }; =20 - sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); + bvec_set_page(&bvec, page, size, offset); + iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, size); =20 - mss_now =3D tcp_send_mss(sk, &size_goal, flags); - copied =3D 0; + if (flags & MSG_SENDPAGE_NOTLAST) + msg.msg_flags |=3D MSG_MORE; =20 - err =3D -EPIPE; - if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) - goto out_err; - - while (size > 0) { - struct sk_buff *skb; - size_t copy =3D size; - - skb =3D tcp_build_frag(sk, size_goal, flags, page, offset, ©); - if (!skb) - goto wait_for_space; - - if (!copied) - TCP_SKB_CB(skb)->tcp_flags &=3D ~TCPHDR_PSH; - - copied +=3D copy; - offset +=3D copy; - size -=3D copy; - if (!size) - goto out; - - if (skb->len < size_goal || (flags & MSG_OOB)) - continue; - - if (forced_push(tp)) { - tcp_mark_push(tp, skb); - __tcp_push_pending_frames(sk, mss_now, TCP_NAGLE_PUSH); - } else if (skb =3D=3D tcp_send_head(sk)) - tcp_push_one(sk, mss_now); - continue; - -wait_for_space: - set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); - tcp_push(sk, flags & ~MSG_MORE, mss_now, - TCP_NAGLE_PUSH, size_goal); - - err =3D sk_stream_wait_memory(sk, &timeo); - if (err !=3D 0) - goto do_error; - - mss_now =3D tcp_send_mss(sk, &size_goal, flags); - } - -out: - if (copied) { - tcp_tx_timestamp(sk, sk->sk_tsflags); - if (!(flags & MSG_SENDPAGE_NOTLAST)) - tcp_push(sk, flags, mss_now, tp->nonagle, size_goal); - } - return copied; - -do_error: - tcp_remove_empty_skb(sk); - if (copied) - goto out; -out_err: - /* make sure we wake any epoll edge trigger waiter */ - if (unlikely(tcp_rtx_and_write_queues_empty(sk) && err =3D=3D -EAGAIN)) { - sk->sk_write_space(sk); - tcp_chrono_stop(sk, TCP_CHRONO_SNDBUF_LIMITED); - } - return sk_stream_error(sk, flags, err); + return tcp_sendmsg_locked(sk, &msg, size); } EXPORT_SYMBOL_GPL(do_tcp_sendpages); From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CCA49C76188 for ; Wed, 5 Apr 2023 16:56:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233552AbjDEQ41 (ORCPT ); Wed, 5 Apr 2023 12:56:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32852 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233299AbjDEQ4I (ORCPT ); Wed, 5 Apr 2023 12:56:08 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8794E6191 for ; Wed, 5 Apr 2023 09:54:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713657; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LU+EuI1w95foBpvL+MnfTRdJBoeqcZi7esoiDYFHUqg=; b=QejeTNge9D8ptm7U98n1WuZoVuxTK8PO5PUU6W145NXM2guh41Ab3geQ/cpEEQ8ESr2kb3 gdd2vkFxUEd3BmA7riwk3VhzLcWiAgTZAfrdqt3j23uvLsx9U6mjIxNVcsIdg1YNGAxYrm PBNwhz0bwtPCMBjaKmGdhQb/XoWuS7M= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-256-Ory9MB64PridaGhduckyKQ-1; Wed, 05 Apr 2023 12:54:12 -0400 X-MC-Unique: Ory9MB64PridaGhduckyKQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 95935885620; Wed, 5 Apr 2023 16:54:11 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4A2CDC1602A; Wed, 5 Apr 2023 16:54:09 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, John Fastabend , Jakub Sitnicki , bpf@vger.kernel.org Subject: [PATCH net-next v4 09/20] tcp_bpf: Inline do_tcp_sendpages as it's now a wrapper around tcp_sendmsg Date: Wed, 5 Apr 2023 17:53:28 +0100 Message-Id: <20230405165339.3468808-10-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" do_tcp_sendpages() is now just a small wrapper around tcp_sendmsg_locked(), so inline it. This is part of replacing ->sendpage() with a call to sendmsg() with MSG_SPLICE_PAGES set. Signed-off-by: David Howells cc: John Fastabend cc: Jakub Sitnicki cc: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org cc: bpf@vger.kernel.org --- net/ipv4/tcp_bpf.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c index ebf917511937..24bfb885777e 100644 --- a/net/ipv4/tcp_bpf.c +++ b/net/ipv4/tcp_bpf.c @@ -72,11 +72,13 @@ static int tcp_bpf_push(struct sock *sk, struct sk_msg = *msg, u32 apply_bytes, { bool apply =3D apply_bytes; struct scatterlist *sge; + struct msghdr msghdr =3D { .msg_flags =3D flags | MSG_SPLICE_PAGES, }; struct page *page; int size, ret =3D 0; u32 off; =20 while (1) { + struct bio_vec bvec; bool has_tx_ulp; =20 sge =3D sk_msg_elem(msg, msg->sg.start); @@ -88,16 +90,18 @@ static int tcp_bpf_push(struct sock *sk, struct sk_msg = *msg, u32 apply_bytes, tcp_rate_check_app_limited(sk); retry: has_tx_ulp =3D tls_sw_has_ctx_tx(sk); - if (has_tx_ulp) { - flags |=3D MSG_SENDPAGE_NOPOLICY; - ret =3D kernel_sendpage_locked(sk, - page, off, size, flags); - } else { - ret =3D do_tcp_sendpages(sk, page, off, size, flags); - } + if (has_tx_ulp) + msghdr.msg_flags |=3D MSG_SENDPAGE_NOPOLICY; =20 + if (flags & MSG_SENDPAGE_NOTLAST) + msghdr.msg_flags |=3D MSG_MORE; + + bvec_set_page(&bvec, page, size, off); + iov_iter_bvec(&msghdr.msg_iter, ITER_SOURCE, &bvec, 1, size); + ret =3D tcp_sendmsg_locked(sk, &msghdr, size); if (ret <=3D 0) return ret; + if (apply) apply_bytes -=3D ret; msg->sg.size -=3D ret; @@ -404,7 +408,7 @@ static int tcp_bpf_sendmsg(struct sock *sk, struct msgh= dr *msg, size_t size) long timeo; int flags; =20 - /* Don't let internal do_tcp_sendpages() flags through */ + /* Don't let internal sendpage flags through */ flags =3D (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED); flags |=3D MSG_NO_SHARED_FRAGS; From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D538FC7619A for ; Wed, 5 Apr 2023 16:56:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231894AbjDEQ4n (ORCPT ); Wed, 5 Apr 2023 12:56:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233658AbjDEQ4P (ORCPT ); Wed, 5 Apr 2023 12:56:15 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B43165B7 for ; Wed, 5 Apr 2023 09:54:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713665; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DjALLcCTAkJ9GFGzQm0mxszTSA2UBcCYteYf8nsJBJs=; b=ajuQZvkL0V2yuCvVzAZrd7sryW1j1Ve8tkThDdH6wV8p228RRL+n236n5QaetNRs5QGJbS oxC4BiNm+AnXn0v+rmnVpl6s0RRkBhtd5MoL6YTGzaQA31d2+YPR3oa2Lg31aj4yA/uYfX wq6mrXIusVYfqc1G10zI9YxdmQtdemE= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-561-cGYh42d-MymHRdF7Qm9tVg-1; Wed, 05 Apr 2023 12:54:23 -0400 X-MC-Unique: cGYh42d-MymHRdF7Qm9tVg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9C29F88904E; Wed, 5 Apr 2023 16:54:14 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4C0AA18EC7; Wed, 5 Apr 2023 16:54:12 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Steffen Klassert , Herbert Xu Subject: [PATCH net-next v4 10/20] espintcp: Inline do_tcp_sendpages() Date: Wed, 5 Apr 2023 17:53:29 +0100 Message-Id: <20230405165339.3468808-11-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" do_tcp_sendpages() is now just a small wrapper around tcp_sendmsg_locked(), so inline it, allowing do_tcp_sendpages() to be removed. This is part of replacing ->sendpage() with a call to sendmsg() with MSG_SPLICE_PAGES set. Signed-off-by: David Howells cc: Steffen Klassert cc: Herbert Xu cc: Eric Dumazet cc: "David S. Miller" cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- net/xfrm/espintcp.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/net/xfrm/espintcp.c b/net/xfrm/espintcp.c index 872b80188e83..3504925babdb 100644 --- a/net/xfrm/espintcp.c +++ b/net/xfrm/espintcp.c @@ -205,14 +205,16 @@ static int espintcp_sendskb_locked(struct sock *sk, s= truct espintcp_msg *emsg, static int espintcp_sendskmsg_locked(struct sock *sk, struct espintcp_msg *emsg, int flags) { + struct msghdr msghdr =3D { .msg_flags =3D flags | MSG_SPLICE_PAGES, }; struct sk_msg *skmsg =3D &emsg->skmsg; struct scatterlist *sg; int done =3D 0; int ret; =20 - flags |=3D MSG_SENDPAGE_NOTLAST; + msghdr.msg_flags |=3D MSG_SENDPAGE_NOTLAST; sg =3D &skmsg->sg.data[skmsg->sg.start]; do { + struct bio_vec bvec; size_t size =3D sg->length - emsg->offset; int offset =3D sg->offset + emsg->offset; struct page *p; @@ -220,11 +222,13 @@ static int espintcp_sendskmsg_locked(struct sock *sk, emsg->offset =3D 0; =20 if (sg_is_last(sg)) - flags &=3D ~MSG_SENDPAGE_NOTLAST; + msghdr.msg_flags &=3D ~MSG_SENDPAGE_NOTLAST; =20 p =3D sg_page(sg); retry: - ret =3D do_tcp_sendpages(sk, p, offset, size, flags); + bvec_set_page(&bvec, p, size, offset); + iov_iter_bvec(&msghdr.msg_iter, ITER_SOURCE, &bvec, 1, size); + ret =3D tcp_sendmsg_locked(sk, &msghdr, size); if (ret < 0) { emsg->offset =3D offset - sg->offset; skmsg->sg.start +=3D done; From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 838A1C761AF for ; Wed, 5 Apr 2023 16:56:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229960AbjDEQ4j (ORCPT ); Wed, 5 Apr 2023 12:56:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233638AbjDEQ4P (ORCPT ); Wed, 5 Apr 2023 12:56:15 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C760A658C for ; Wed, 5 Apr 2023 09:54:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713662; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3FA5Yjp/rAglxaSde3E7QeP89aqCLnwKrUfyvWNxLew=; b=DupbQtgLUb+e2dU+bgtj3EHeaTzt2puAes7ybIGPpoOi3qG3oFGJYeXKhjpB0Vq0v0qYwb bLl9G7mZyAo3qv3SK6Vkmw8KbIIy3zv7NcutFfcpzBKMB/eZU+jUl7umEQbWiQmQTw7Mpw ZKx4i6eExEK8DybORJLLUYx9YR3yWdk= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-452-U_gW8eScNw2-IFPm1Urp3A-1; Wed, 05 Apr 2023 12:54:18 -0400 X-MC-Unique: U_gW8eScNw2-IFPm1Urp3A-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6D39D2807D69; Wed, 5 Apr 2023 16:54:17 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3A90D40C6EC4; Wed, 5 Apr 2023 16:54:15 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Boris Pismenny , John Fastabend Subject: [PATCH net-next v4 11/20] tls: Inline do_tcp_sendpages() Date: Wed, 5 Apr 2023 17:53:30 +0100 Message-Id: <20230405165339.3468808-12-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" do_tcp_sendpages() is now just a small wrapper around tcp_sendmsg_locked(), so inline it, allowing do_tcp_sendpages() to be removed. This is part of replacing ->sendpage() with a call to sendmsg() with MSG_SPLICE_PAGES set. Signed-off-by: David Howells cc: Boris Pismenny cc: John Fastabend cc: Jakub Kicinski cc: "David S. Miller" cc: Eric Dumazet cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- include/net/tls.h | 2 +- net/tls/tls_main.c | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/include/net/tls.h b/include/net/tls.h index 154949c7b0c8..d31521c36a84 100644 --- a/include/net/tls.h +++ b/include/net/tls.h @@ -256,7 +256,7 @@ struct tls_context { struct scatterlist *partially_sent_record; u16 partially_sent_offset; =20 - bool in_tcp_sendpages; + bool splicing_pages; bool pending_open_record_frags; =20 struct mutex tx_lock; /* protects partially_sent_* fields and diff --git a/net/tls/tls_main.c b/net/tls/tls_main.c index b32c112984dd..1d0e318d7977 100644 --- a/net/tls/tls_main.c +++ b/net/tls/tls_main.c @@ -124,7 +124,10 @@ int tls_push_sg(struct sock *sk, u16 first_offset, int flags) { - int sendpage_flags =3D flags | MSG_SENDPAGE_NOTLAST; + struct bio_vec bvec; + struct msghdr msg =3D { + .msg_flags =3D MSG_SENDPAGE_NOTLAST | MSG_SPLICE_PAGES | flags, + }; int ret =3D 0; struct page *p; size_t size; @@ -133,16 +136,19 @@ int tls_push_sg(struct sock *sk, size =3D sg->length - offset; offset +=3D sg->offset; =20 - ctx->in_tcp_sendpages =3D true; + ctx->splicing_pages =3D true; while (1) { if (sg_is_last(sg)) - sendpage_flags =3D flags; + msg.msg_flags =3D flags; =20 /* is sending application-limited? */ tcp_rate_check_app_limited(sk); p =3D sg_page(sg); retry: - ret =3D do_tcp_sendpages(sk, p, offset, size, sendpage_flags); + bvec_set_page(&bvec, p, size, offset); + iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, size); + + ret =3D tcp_sendmsg_locked(sk, &msg, size); =20 if (ret !=3D size) { if (ret > 0) { @@ -154,7 +160,7 @@ int tls_push_sg(struct sock *sk, offset -=3D sg->offset; ctx->partially_sent_offset =3D offset; ctx->partially_sent_record =3D (void *)sg; - ctx->in_tcp_sendpages =3D false; + ctx->splicing_pages =3D false; return ret; } =20 @@ -168,7 +174,7 @@ int tls_push_sg(struct sock *sk, size =3D sg->length; } =20 - ctx->in_tcp_sendpages =3D false; + ctx->splicing_pages =3D false; =20 return 0; } @@ -246,11 +252,11 @@ static void tls_write_space(struct sock *sk) { struct tls_context *ctx =3D tls_get_ctx(sk); =20 - /* If in_tcp_sendpages call lower protocol write space handler + /* If splicing_pages call lower protocol write space handler * to ensure we wake up any waiting operations there. For example - * if do_tcp_sendpages where to call sk_wait_event. + * if splicing pages where to call sk_wait_event. */ - if (ctx->in_tcp_sendpages) { + if (ctx->splicing_pages) { ctx->sk_write_space(sk); return; } From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 51B57C7619A for ; Wed, 5 Apr 2023 16:56:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231344AbjDEQ4r (ORCPT ); Wed, 5 Apr 2023 12:56:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233664AbjDEQ4R (ORCPT ); Wed, 5 Apr 2023 12:56:17 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB80665A9 for ; Wed, 5 Apr 2023 09:54:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713663; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pP5V7G7GRqo/82ZWWApO+0CTJP1pdqvbg0H8r7V6l6c=; b=ZtNlTLNlGewb4ms1loGn2bF5tlIDkjzgu1mfOyEtyKIE9GoSkWkaoqOvmKyMPFSt7EA/94 J3VZxUCYb6zWfX61+pfu83G1vqPx3CLzMRojqmm3zSn12OeOGGpqoQ6tXsLmKxnyzFx++7 hs3LXdLdPyXyTGRODZQ+6eN5E7Dvc04= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-523-jkYT3UqJN2e1IEz58PiLDA-1; Wed, 05 Apr 2023 12:54:21 -0400 X-MC-Unique: jkYT3UqJN2e1IEz58PiLDA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6F2AA3823A07; Wed, 5 Apr 2023 16:54:20 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 227C72027061; Wed, 5 Apr 2023 16:54:18 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Bernard Metzler , Tom Talpey , linux-rdma@vger.kernel.org Subject: [PATCH net-next v4 12/20] siw: Inline do_tcp_sendpages() Date: Wed, 5 Apr 2023 17:53:31 +0100 Message-Id: <20230405165339.3468808-13-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" do_tcp_sendpages() is now just a small wrapper around tcp_sendmsg_locked(), so inline it, allowing do_tcp_sendpages() to be removed. This is part of replacing ->sendpage() with a call to sendmsg() with MSG_SPLICE_PAGES set. Signed-off-by: David Howells cc: Bernard Metzler cc: Tom Talpey cc: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: linux-rdma@vger.kernel.org cc: netdev@vger.kernel.org --- drivers/infiniband/sw/siw/siw_qp_tx.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/drivers/infiniband/sw/siw/siw_qp_tx.c b/drivers/infiniband/sw/= siw/siw_qp_tx.c index 05052b49107f..fa5de40d85d5 100644 --- a/drivers/infiniband/sw/siw/siw_qp_tx.c +++ b/drivers/infiniband/sw/siw/siw_qp_tx.c @@ -313,7 +313,7 @@ static int siw_tx_ctrl(struct siw_iwarp_tx *c_tx, struc= t socket *s, } =20 /* - * 0copy TCP transmit interface: Use do_tcp_sendpages. + * 0copy TCP transmit interface: Use MSG_SPLICE_PAGES. * * Using sendpage to push page by page appears to be less efficient * than using sendmsg, even if data are copied. @@ -324,20 +324,27 @@ static int siw_tx_ctrl(struct siw_iwarp_tx *c_tx, str= uct socket *s, static int siw_tcp_sendpages(struct socket *s, struct page **page, int off= set, size_t size) { + struct bio_vec bvec; + struct msghdr msg =3D { + .msg_flags =3D (MSG_MORE | MSG_DONTWAIT | MSG_SENDPAGE_NOTLAST | + MSG_SPLICE_PAGES), + }; struct sock *sk =3D s->sk; - int i =3D 0, rv =3D 0, sent =3D 0, - flags =3D MSG_MORE | MSG_DONTWAIT | MSG_SENDPAGE_NOTLAST; + int i =3D 0, rv =3D 0, sent =3D 0; =20 while (size) { size_t bytes =3D min_t(size_t, PAGE_SIZE - offset, size); =20 if (size + offset <=3D PAGE_SIZE) - flags =3D MSG_MORE | MSG_DONTWAIT; + msg.msg_flags =3D MSG_MORE | MSG_DONTWAIT; =20 tcp_rate_check_app_limited(sk); + bvec_set_page(&bvec, page[i], bytes, offset); + iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, size); + try_page_again: lock_sock(sk); - rv =3D do_tcp_sendpages(sk, page[i], offset, bytes, flags); + rv =3D tcp_sendmsg_locked(sk, &msg, size); release_sock(sk); =20 if (rv > 0) { From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 15093C7619A for ; Wed, 5 Apr 2023 16:57:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233422AbjDEQ5h (ORCPT ); Wed, 5 Apr 2023 12:57:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233789AbjDEQ4X (ORCPT ); Wed, 5 Apr 2023 12:56:23 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3AC16A41 for ; Wed, 5 Apr 2023 09:54:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713668; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mV2Euz/SvGe1s9i2UzZ379KdNOAT2pMlJalr2LTSkGw=; b=gHyXispebH5cJ9tXzBhmB+K533mWbmOYDY5vBuh+vMkXJF5529Xi80SVuFttrA4JE+0Hy2 aTKfAzWf1SpvNoDGeooj7nQ0qXNhBwN5BGHt7Rz1b3ni5IkIqrXq8H2QHnTwMGrpnS/br3 Ib4ZCnJFvh6Pu8mTd8PkEZl0PIWyv5o= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-191-AWcuO68OMJ-A2c7oOB3JYw-1; Wed, 05 Apr 2023 12:54:24 -0400 X-MC-Unique: AWcuO68OMJ-A2c7oOB3JYw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 159FC858F09; Wed, 5 Apr 2023 16:54:23 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0D2D1C1602A; Wed, 5 Apr 2023 16:54:20 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH net-next v4 13/20] tcp: Fold do_tcp_sendpages() into tcp_sendpage_locked() Date: Wed, 5 Apr 2023 17:53:32 +0100 Message-Id: <20230405165339.3468808-14-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Fold do_tcp_sendpages() into its last remaining caller, tcp_sendpage_locked(). Signed-off-by: David Howells cc: Eric Dumazet cc: "David S. Miller" cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- include/net/tcp.h | 2 -- net/ipv4/tcp.c | 21 +++++++-------------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index a0a91a988272..11c62d37f3d5 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -333,8 +333,6 @@ int tcp_sendpage(struct sock *sk, struct page *page, in= t offset, size_t size, int flags); int tcp_sendpage_locked(struct sock *sk, struct page *page, int offset, size_t size, int flags); -ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset, - size_t size, int flags); int tcp_send_mss(struct sock *sk, int *size_goal, int flags); void tcp_push(struct sock *sk, int flags, int mss_now, int nonagle, int size_goal); diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index a8a4ace8b3da..c7240beedfed 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -972,12 +972,17 @@ static int tcp_wmem_schedule(struct sock *sk, int cop= y) return min(copy, sk->sk_forward_alloc); } =20 -ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset, - size_t size, int flags) +int tcp_sendpage_locked(struct sock *sk, struct page *page, int offset, + size_t size, int flags) { struct bio_vec bvec; struct msghdr msg =3D { .msg_flags =3D flags | MSG_SPLICE_PAGES, }; =20 + if (!(sk->sk_route_caps & NETIF_F_SG)) + return sock_no_sendpage_locked(sk, page, offset, size, flags); + + tcp_rate_check_app_limited(sk); /* is sending application-limited? */ + bvec_set_page(&bvec, page, size, offset); iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, size); =20 @@ -986,18 +991,6 @@ ssize_t do_tcp_sendpages(struct sock *sk, struct page = *page, int offset, =20 return tcp_sendmsg_locked(sk, &msg, size); } -EXPORT_SYMBOL_GPL(do_tcp_sendpages); - -int tcp_sendpage_locked(struct sock *sk, struct page *page, int offset, - size_t size, int flags) -{ - if (!(sk->sk_route_caps & NETIF_F_SG)) - return sock_no_sendpage_locked(sk, page, offset, size, flags); - - tcp_rate_check_app_limited(sk); /* is sending application-limited? */ - - return do_tcp_sendpages(sk, page, offset, size, flags); -} EXPORT_SYMBOL_GPL(tcp_sendpage_locked); =20 int tcp_sendpage(struct sock *sk, struct page *page, int offset, From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 22F7BC761AF for ; Wed, 5 Apr 2023 16:56:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232321AbjDEQ4y (ORCPT ); Wed, 5 Apr 2023 12:56:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60412 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233685AbjDEQ4U (ORCPT ); Wed, 5 Apr 2023 12:56:20 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEF6C6A44 for ; Wed, 5 Apr 2023 09:54:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713669; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rczukz9qV63hFuelx2DCwDUOgKVDzUk5rK7clppjA7k=; b=fOhKh8EplE1fdXdVQAY2hjc68li3FdxKqarE8L/GAjEojBduickNqnIo/rxgrB6JPIlfMM nAqCHkQu3FUAs4foBPPxrgTHyNxUkI8mrSSC6TSCD9LpW/bRDe4rPMwLoEJRtKmo0YTljL JonTdYzwalpALT3T9A8bWo/z/J43Eoc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-224-KG6uRlhrMeKauli9V7lJyg-1; Wed, 05 Apr 2023 12:54:26 -0400 X-MC-Unique: KG6uRlhrMeKauli9V7lJyg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CC15D858F09; Wed, 5 Apr 2023 16:54:25 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id C7AD1400F4F; Wed, 5 Apr 2023 16:54:23 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH net-next v4 14/20] udp: Convert udp_sendpage() to use MSG_SPLICE_PAGES Date: Wed, 5 Apr 2023 17:53:33 +0100 Message-Id: <20230405165339.3468808-15-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Convert udp_sendpage() to use sendmsg() with MSG_SPLICE_PAGES rather than directly splicing in the pages itself. This allows ->sendpage() to be replaced by something that can handle multiple multipage folios in a single transaction. Signed-off-by: David Howells cc: Willem de Bruijn cc: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- net/ipv4/udp.c | 50 +++++++++----------------------------------------- 1 file changed, 9 insertions(+), 41 deletions(-) diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index aa32afd871ee..0d3e78a65f51 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -1332,52 +1332,20 @@ EXPORT_SYMBOL(udp_sendmsg); int udp_sendpage(struct sock *sk, struct page *page, int offset, size_t size, int flags) { - struct inet_sock *inet =3D inet_sk(sk); - struct udp_sock *up =3D udp_sk(sk); + struct bio_vec bvec; + struct msghdr msg =3D { + .msg_flags =3D flags | MSG_SPLICE_PAGES | MSG_MORE + }; int ret; =20 - if (flags & MSG_SENDPAGE_NOTLAST) - flags |=3D MSG_MORE; + bvec_set_page(&bvec, page, size, offset); + iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, size); =20 - if (!up->pending) { - struct msghdr msg =3D { .msg_flags =3D flags|MSG_MORE }; - - /* Call udp_sendmsg to specify destination address which - * sendpage interface can't pass. - * This will succeed only when the socket is connected. - */ - ret =3D udp_sendmsg(sk, &msg, 0); - if (ret < 0) - return ret; - } + if (flags & MSG_SENDPAGE_NOTLAST) + msg.msg_flags |=3D MSG_MORE; =20 lock_sock(sk); - - if (unlikely(!up->pending)) { - release_sock(sk); - - net_dbg_ratelimited("cork failed\n"); - return -EINVAL; - } - - ret =3D ip_append_page(sk, &inet->cork.fl.u.ip4, - page, offset, size, flags); - if (ret =3D=3D -EOPNOTSUPP) { - release_sock(sk); - return sock_no_sendpage(sk->sk_socket, page, offset, - size, flags); - } - if (ret < 0) { - udp_flush_pending_frames(sk); - goto out; - } - - up->len +=3D size; - if (!(READ_ONCE(up->corkflag) || (flags&MSG_MORE))) - ret =3D udp_push_pending_frames(sk); - if (!ret) - ret =3D size; -out: + ret =3D udp_sendmsg(sk, &msg, size); release_sock(sk); return ret; } From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 337C1C761AF for ; Wed, 5 Apr 2023 16:57:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233803AbjDEQ5y (ORCPT ); Wed, 5 Apr 2023 12:57:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233850AbjDEQ4Y (ORCPT ); Wed, 5 Apr 2023 12:56:24 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 733C86A5E for ; Wed, 5 Apr 2023 09:54:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713672; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vSIQMN2HZrWiQ4eH8GOTjkisrj+ZE6cW7zZ4IaXdbXs=; b=Uz35m6idyZ++BUOJw9H605Lp984B0ZDH57yYrU5Aj5YnLhnEbDSRNZTY05jkAWhv4ZisVL zEBG/fhPZaEkwpbnhxu0nOZNs8VmnZXgtBMWNKEumg6PKWCev4T3lPyacWWJMHnbFm4L5c t18t6PKubWNyLtXQSH9YkoTG6Ah5P6Q= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-612-UDoecsx0PrK-YaToklNIkQ-1; Wed, 05 Apr 2023 12:54:29 -0400 X-MC-Unique: UDoecsx0PrK-YaToklNIkQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AA0EC3823A07; Wed, 5 Apr 2023 16:54:28 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id A5B03140EBF4; Wed, 5 Apr 2023 16:54:26 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH net-next v4 15/20] ip: Remove ip_append_page() Date: Wed, 5 Apr 2023 17:53:34 +0100 Message-Id: <20230405165339.3468808-16-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" ip_append_page() is no longer used with the removal of udp_sendpage(), so remove it. Signed-off-by: David Howells cc: Willem de Bruijn cc: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- include/net/ip.h | 2 - net/ipv4/ip_output.c | 136 ++----------------------------------------- 2 files changed, 4 insertions(+), 134 deletions(-) diff --git a/include/net/ip.h b/include/net/ip.h index c3fffaa92d6e..7627a4df893b 100644 --- a/include/net/ip.h +++ b/include/net/ip.h @@ -220,8 +220,6 @@ int ip_append_data(struct sock *sk, struct flowi4 *fl4, unsigned int flags); int ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb); -ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *p= age, - int offset, size_t size, int flags); struct sk_buff *__ip_make_skb(struct sock *sk, struct flowi4 *fl4, struct sk_buff_head *queue, struct inet_cork *cork); diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c index 22a90a9392eb..2dacee1a1ed4 100644 --- a/net/ipv4/ip_output.c +++ b/net/ipv4/ip_output.c @@ -1310,10 +1310,10 @@ static int ip_setup_cork(struct sock *sk, struct in= et_cork *cork, } =20 /* - * ip_append_data() and ip_append_page() can make one large IP datagram - * from many pieces of data. Each pieces will be holded on the socket - * until ip_push_pending_frames() is called. Each piece can be a page - * or non-page data. + * ip_append_data() can make one large IP datagram from many pieces of + * data. Each piece will be held on the socket until + * ip_push_pending_frames() is called. Each piece can be a page or + * non-page data. * * Not only UDP, other transport protocols - e.g. raw sockets - can use * this interface potentially. @@ -1346,134 +1346,6 @@ int ip_append_data(struct sock *sk, struct flowi4 *= fl4, from, length, transhdrlen, flags); } =20 -ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *p= age, - int offset, size_t size, int flags) -{ - struct inet_sock *inet =3D inet_sk(sk); - struct sk_buff *skb; - struct rtable *rt; - struct ip_options *opt =3D NULL; - struct inet_cork *cork; - int hh_len; - int mtu; - int len; - int err; - unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize; - - if (inet->hdrincl) - return -EPERM; - - if (flags&MSG_PROBE) - return 0; - - if (skb_queue_empty(&sk->sk_write_queue)) - return -EINVAL; - - cork =3D &inet->cork.base; - rt =3D (struct rtable *)cork->dst; - if (cork->flags & IPCORK_OPT) - opt =3D cork->opt; - - if (!(rt->dst.dev->features & NETIF_F_SG)) - return -EOPNOTSUPP; - - hh_len =3D LL_RESERVED_SPACE(rt->dst.dev); - mtu =3D cork->gso_size ? IP_MAX_MTU : cork->fragsize; - - fragheaderlen =3D sizeof(struct iphdr) + (opt ? opt->optlen : 0); - maxfraglen =3D ((mtu - fragheaderlen) & ~7) + fragheaderlen; - maxnonfragsize =3D ip_sk_ignore_df(sk) ? 0xFFFF : mtu; - - if (cork->length + size > maxnonfragsize - fragheaderlen) { - ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, - mtu - (opt ? opt->optlen : 0)); - return -EMSGSIZE; - } - - skb =3D skb_peek_tail(&sk->sk_write_queue); - if (!skb) - return -EINVAL; - - cork->length +=3D size; - - while (size > 0) { - /* Check if the remaining data fits into current packet. */ - len =3D mtu - skb->len; - if (len < size) - len =3D maxfraglen - skb->len; - - if (len <=3D 0) { - struct sk_buff *skb_prev; - int alloclen; - - skb_prev =3D skb; - fraggap =3D skb_prev->len - maxfraglen; - - alloclen =3D fragheaderlen + hh_len + fraggap + 15; - skb =3D sock_wmalloc(sk, alloclen, 1, sk->sk_allocation); - if (unlikely(!skb)) { - err =3D -ENOBUFS; - goto error; - } - - /* - * Fill in the control structures - */ - skb->ip_summed =3D CHECKSUM_NONE; - skb->csum =3D 0; - skb_reserve(skb, hh_len); - - /* - * Find where to start putting bytes. - */ - skb_put(skb, fragheaderlen + fraggap); - skb_reset_network_header(skb); - skb->transport_header =3D (skb->network_header + - fragheaderlen); - if (fraggap) { - skb->csum =3D skb_copy_and_csum_bits(skb_prev, - maxfraglen, - skb_transport_header(skb), - fraggap); - skb_prev->csum =3D csum_sub(skb_prev->csum, - skb->csum); - pskb_trim_unique(skb_prev, maxfraglen); - } - - /* - * Put the packet on the pending queue. - */ - __skb_queue_tail(&sk->sk_write_queue, skb); - continue; - } - - if (len > size) - len =3D size; - - if (skb_append_pagefrags(skb, page, offset, len)) { - err =3D -EMSGSIZE; - goto error; - } - - if (skb->ip_summed =3D=3D CHECKSUM_NONE) { - __wsum csum; - csum =3D csum_page(page, offset, len); - skb->csum =3D csum_block_add(skb->csum, csum, skb->len); - } - - skb_len_add(skb, len); - refcount_add(len, &sk->sk_wmem_alloc); - offset +=3D len; - size -=3D len; - } - return 0; - -error: - cork->length -=3D size; - IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS); - return err; -} - static void ip_cork_release(struct inet_cork *cork) { cork->flags &=3D ~IPCORK_OPT; From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D00BEC76188 for ; Wed, 5 Apr 2023 16:57:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231967AbjDEQ5b (ORCPT ); Wed, 5 Apr 2023 12:57:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232717AbjDEQ4X (ORCPT ); Wed, 5 Apr 2023 12:56:23 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D90D6A6E for ; Wed, 5 Apr 2023 09:54:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713675; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4sRhyVsK/d5pgFDi65Wlb3WXN0jzDUhNbeQVzVbMamI=; b=GMfgh1Ye574s0GmlOm9Mebz2iBLMuOS9/82ApOqTK6971wst5aeXov2YYhhjhSDjn20pqK oTN0t+AwgqpsnQj3bdoavK8j7Kvs3P/h6g/jh8kjdiVqN9COpu1ID4eq/6bXlzfcI1pWZu Hl5Yc8CMtFtmM53wUpMuDN+3jcGKuHQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-68-CiPL6DpHNz2kv6EGD8VAcQ-1; Wed, 05 Apr 2023 12:54:32 -0400 X-MC-Unique: CiPL6DpHNz2kv6EGD8VAcQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 610BD185A78B; Wed, 5 Apr 2023 16:54:31 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5F30A1121314; Wed, 5 Apr 2023 16:54:29 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH net-next v4 16/20] ip, udp: Support MSG_SPLICE_PAGES Date: Wed, 5 Apr 2023 17:53:35 +0100 Message-Id: <20230405165339.3468808-17-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Make IP/UDP sendmsg() support MSG_SPLICE_PAGES. This causes pages to be spliced from the source iterator. This allows ->sendpage() to be replaced by something that can handle multiple multipage folios in a single transaction. Signed-off-by: David Howells cc: Willem de Bruijn cc: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- net/ipv4/ip_output.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c index 2dacee1a1ed4..13d19867ffd3 100644 --- a/net/ipv4/ip_output.c +++ b/net/ipv4/ip_output.c @@ -957,6 +957,41 @@ csum_page(struct page *page, int offset, int copy) return csum; } =20 +/* + * Add (or copy) data pages for MSG_SPLICE_PAGES. + */ +static int __ip_splice_pages(struct sock *sk, struct sk_buff *skb, + void *from, int *pcopy) +{ + struct msghdr *msg =3D from; + struct page *page =3D NULL, **pages =3D &page; + ssize_t copy =3D *pcopy; + size_t off; + int err; + + copy =3D iov_iter_extract_pages(&msg->msg_iter, &pages, copy, 1, 0, &off); + if (copy <=3D 0) + return copy ?: -EIO; + + err =3D skb_append_pagefrags(skb, page, off, copy); + if (err < 0) { + iov_iter_revert(&msg->msg_iter, copy); + return err; + } + + if (skb->ip_summed =3D=3D CHECKSUM_NONE) { + __wsum csum; + + csum =3D csum_page(page, off, copy); + skb->csum =3D csum_block_add(skb->csum, csum, skb->len); + } + + skb_len_add(skb, copy); + refcount_add(copy, &sk->sk_wmem_alloc); + *pcopy =3D copy; + return 0; +} + static int __ip_append_data(struct sock *sk, struct flowi4 *fl4, struct sk_buff_head *queue, @@ -1048,6 +1083,14 @@ static int __ip_append_data(struct sock *sk, skb_zcopy_set(skb, uarg, &extra_uref); } } + } else if ((flags & MSG_SPLICE_PAGES) && length) { + if (inet->hdrincl) + return -EPERM; + if (rt->dst.dev->features & NETIF_F_SG) + /* We need an empty buffer to attach stuff to */ + paged =3D true; + else + flags &=3D ~MSG_SPLICE_PAGES; } =20 cork->length +=3D length; @@ -1207,6 +1250,10 @@ static int __ip_append_data(struct sock *sk, err =3D -EFAULT; goto error; } + } else if (flags & MSG_SPLICE_PAGES) { + err =3D __ip_splice_pages(sk, skb, from, ©); + if (err < 0) + goto error; } else if (!zc) { int i =3D skb_shinfo(skb)->nr_frags; From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2481FC76188 for ; Wed, 5 Apr 2023 16:58:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232745AbjDEQ6R (ORCPT ); Wed, 5 Apr 2023 12:58:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232112AbjDEQ4o (ORCPT ); Wed, 5 Apr 2023 12:56:44 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1BE56EAE for ; Wed, 5 Apr 2023 09:54:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713679; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OMwy0qt4L2slgfHXAMvoFhpKJLhShXlB6/4lgaiCeU4=; b=fsPcXU9nLDFTuwDCwSmk3GuOvKKwxW65+3X8nj3/Q7ptinpTttZ2puwlrCLtRBA0tYqvKl esBZYOx2u8+cn9b7bl672y8Pr+Sm4NC8khEwfknq+lI+wfbO7NrFmbR7TE8HTB28Gl7+88 CVAJAtQ1e76S4/GjZzXQhxEernt839w= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-160-4RoSPamyNOOyKJ-8OPZdCA-1; Wed, 05 Apr 2023 12:54:36 -0400 X-MC-Unique: 4RoSPamyNOOyKJ-8OPZdCA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 044E23823A0B; Wed, 5 Apr 2023 16:54:34 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id F2DAE2166B26; Wed, 5 Apr 2023 16:54:31 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH net-next v4 17/20] ip, udp: Make sendmsg(MSG_SPLICE_PAGES) copy unspliceable data Date: Wed, 5 Apr 2023 17:53:36 +0100 Message-Id: <20230405165339.3468808-18-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" If sendmsg() with MSG_SPLICE_PAGES encounters a page that shouldn't be spliced - a slab page, for instance, or one with a zero count - make __ip_append_data() copy it. Signed-off-by: David Howells cc: Willem de Bruijn cc: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- net/ipv4/ip_output.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c index 13d19867ffd3..e34c86b1b59a 100644 --- a/net/ipv4/ip_output.c +++ b/net/ipv4/ip_output.c @@ -967,13 +967,32 @@ static int __ip_splice_pages(struct sock *sk, struct = sk_buff *skb, struct page *page =3D NULL, **pages =3D &page; ssize_t copy =3D *pcopy; size_t off; + bool put =3D false; int err; =20 copy =3D iov_iter_extract_pages(&msg->msg_iter, &pages, copy, 1, 0, &off); if (copy <=3D 0) return copy ?: -EIO; =20 + if (!sendpage_ok(page)) { + const void *p =3D kmap_local_page(page); + void *q; + + q =3D page_frag_memdup(NULL, p + off, copy, + sk->sk_allocation, ULONG_MAX); + kunmap_local(p); + if (!q) { + iov_iter_revert(&msg->msg_iter, copy); + return -ENOMEM; + } + page =3D virt_to_page(q); + off =3D offset_in_page(q); + put =3D true; + } + err =3D skb_append_pagefrags(skb, page, off, copy); + if (put) + put_page(page); if (err < 0) { iov_iter_revert(&msg->msg_iter, copy); return err; From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70160C77B62 for ; Wed, 5 Apr 2023 16:58:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233908AbjDEQ6N (ORCPT ); Wed, 5 Apr 2023 12:58:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230108AbjDEQ4l (ORCPT ); Wed, 5 Apr 2023 12:56:41 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F34026EA2 for ; Wed, 5 Apr 2023 09:54:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713679; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/JFI/23Tc+s/ig3NV2/20J93ojirddDNW00qNvViyfU=; b=ReqOQhGXwcfWg1FfIgVmuPPkkV+IiWeOeLaVuDlhFN0p7Fdafkj3e6ohqH8AQSlOLkxhSp vIvcDCu0fRiEQh3JHfzXucwvmp043i5zEPAMaM1pp6cxPIGxtmpeoISyhKC47SiQqRXxB4 I4AoHBTx1eB1hy26Y9ZWXDFFudLCnOs= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-456-mGoavVuyPK63hF6pFvfXqA-1; Wed, 05 Apr 2023 12:54:37 -0400 X-MC-Unique: mGoavVuyPK63hF6pFvfXqA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B5FED101A531; Wed, 5 Apr 2023 16:54:36 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id B22C6C1602B; Wed, 5 Apr 2023 16:54:34 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH net-next v4 18/20] ip6, udp6: Support MSG_SPLICE_PAGES Date: Wed, 5 Apr 2023 17:53:37 +0100 Message-Id: <20230405165339.3468808-19-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Make IP6/UDP6 sendmsg() support MSG_SPLICE_PAGES. This causes pages to be spliced from the source iterator if possible, copying the data if not. This allows ->sendpage() to be replaced by something that can handle multiple multipage folios in a single transaction. Signed-off-by: David Howells cc: Willem de Bruijn cc: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- include/net/ip.h | 1 + net/ipv4/ip_output.c | 4 ++-- net/ipv6/ip6_output.c | 12 ++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/include/net/ip.h b/include/net/ip.h index 7627a4df893b..8a50341007bf 100644 --- a/include/net/ip.h +++ b/include/net/ip.h @@ -211,6 +211,7 @@ int ip_local_out(struct net *net, struct sock *sk, stru= ct sk_buff *skb); int __ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl, __u8 tos); void ip_init(void); +int __ip_splice_pages(struct sock *sk, struct sk_buff *skb, void *from, in= t *pcopy); int ip_append_data(struct sock *sk, struct flowi4 *fl4, int getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb), diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c index e34c86b1b59a..241a78d82766 100644 --- a/net/ipv4/ip_output.c +++ b/net/ipv4/ip_output.c @@ -960,8 +960,7 @@ csum_page(struct page *page, int offset, int copy) /* * Add (or copy) data pages for MSG_SPLICE_PAGES. */ -static int __ip_splice_pages(struct sock *sk, struct sk_buff *skb, - void *from, int *pcopy) +int __ip_splice_pages(struct sock *sk, struct sk_buff *skb, void *from, in= t *pcopy) { struct msghdr *msg =3D from; struct page *page =3D NULL, **pages =3D &page; @@ -1010,6 +1009,7 @@ static int __ip_splice_pages(struct sock *sk, struct = sk_buff *skb, *pcopy =3D copy; return 0; } +EXPORT_SYMBOL_GPL(__ip_splice_pages); =20 static int __ip_append_data(struct sock *sk, struct flowi4 *fl4, diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c index 0b6140f0179d..82846d18cf22 100644 --- a/net/ipv6/ip6_output.c +++ b/net/ipv6/ip6_output.c @@ -1589,6 +1589,14 @@ static int __ip6_append_data(struct sock *sk, skb_zcopy_set(skb, uarg, &extra_uref); } } + } else if ((flags & MSG_SPLICE_PAGES) && length) { + if (inet_sk(sk)->hdrincl) + return -EPERM; + if (rt->dst.dev->features & NETIF_F_SG) + /* We need an empty buffer to attach stuff to */ + paged =3D true; + else + flags &=3D ~MSG_SPLICE_PAGES; } =20 /* @@ -1778,6 +1786,10 @@ static int __ip6_append_data(struct sock *sk, err =3D -EFAULT; goto error; } + } else if (flags & MSG_SPLICE_PAGES) { + err =3D __ip_splice_pages(sk, skb, from, ©); + if (err < 0) + goto error; } else if (!zc) { int i =3D skb_shinfo(skb)->nr_frags; From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 69176C761AF for ; Wed, 5 Apr 2023 16:58:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233279AbjDEQ6V (ORCPT ); Wed, 5 Apr 2023 12:58:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233664AbjDEQ4w (ORCPT ); Wed, 5 Apr 2023 12:56:52 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17F237281 for ; Wed, 5 Apr 2023 09:54:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713683; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4DKKh1jysgamxWGpEyeqRE19EsYVOUgugB+w/Q3Uqtg=; b=goka7qCHd27EjsRG2dIG9oPALnsAN810wybe4spYYhHETi74W2KMqAbI0TTA1/N5SfVCQc JdsKKac2CxzO9I2Gyecf4GQIgCn85UPdXRKZBVPdii4OHWjsO06+ybM4Ey3K5VSKHyN2UD D2dGJHDaNH/0eJG3A8hJcz2I524CtSU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-547-cweTzd3XPYSIW2fcQ6DILQ-1; Wed, 05 Apr 2023 12:54:40 -0400 X-MC-Unique: cweTzd3XPYSIW2fcQ6DILQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5B889885624; Wed, 5 Apr 2023 16:54:39 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 54D9CC1602A; Wed, 5 Apr 2023 16:54:37 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH net-next v4 19/20] af_unix: Support MSG_SPLICE_PAGES Date: Wed, 5 Apr 2023 17:53:38 +0100 Message-Id: <20230405165339.3468808-20-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Make AF_UNIX sendmsg() support MSG_SPLICE_PAGES, splicing in pages from the source iterator if possible and copying the data in otherwise. This allows ->sendpage() to be replaced by something that can handle multiple multipage folios in a single transaction. Signed-off-by: David Howells cc: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- net/unix/af_unix.c | 93 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 77 insertions(+), 16 deletions(-) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index fb31e8a4409e..fee431a089d3 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -2157,6 +2157,53 @@ static int queue_oob(struct socket *sock, struct msg= hdr *msg, struct sock *other } #endif =20 +/* + * Extract pages from an iterator and add them to the socket buffer. + */ +static ssize_t unix_extract_bvec_to_skb(struct sk_buff *skb, + struct iov_iter *iter, ssize_t maxsize) +{ + struct page *pages[8], **ppages =3D pages; + unsigned int i, nr; + ssize_t ret =3D 0; + + while (iter->count > 0) { + size_t off, len; + + nr =3D min_t(size_t, MAX_SKB_FRAGS - skb_shinfo(skb)->nr_frags, + ARRAY_SIZE(pages)); + if (nr =3D=3D 0) + break; + + len =3D iov_iter_extract_pages(iter, &ppages, maxsize, nr, 0, &off); + if (len <=3D 0) { + if (!ret) + ret =3D len ?: -EIO; + break; + } + + i =3D 0; + do { + size_t part =3D min_t(size_t, PAGE_SIZE - off, len); + + if (skb_append_pagefrags(skb, pages[i++], off, part) < 0) { + if (!ret) + ret =3D -EMSGSIZE; + goto out; + } + off =3D 0; + ret +=3D part; + maxsize -=3D part; + len -=3D part; + } while (len > 0); + if (maxsize <=3D 0) + break; + } + +out: + return ret; +} + static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) { @@ -2200,19 +2247,25 @@ static int unix_stream_sendmsg(struct socket *sock,= struct msghdr *msg, while (sent < len) { size =3D len - sent; =20 - /* Keep two messages in the pipe so it schedules better */ - size =3D min_t(int, size, (sk->sk_sndbuf >> 1) - 64); + if (unlikely(msg->msg_flags & MSG_SPLICE_PAGES)) { + skb =3D sock_alloc_send_pskb(sk, 0, 0, + msg->msg_flags & MSG_DONTWAIT, + &err, 0); + } else { + /* Keep two messages in the pipe so it schedules better */ + size =3D min_t(int, size, (sk->sk_sndbuf >> 1) - 64); =20 - /* allow fallback to order-0 allocations */ - size =3D min_t(int, size, SKB_MAX_HEAD(0) + UNIX_SKB_FRAGS_SZ); + /* allow fallback to order-0 allocations */ + size =3D min_t(int, size, SKB_MAX_HEAD(0) + UNIX_SKB_FRAGS_SZ); =20 - data_len =3D max_t(int, 0, size - SKB_MAX_HEAD(0)); + data_len =3D max_t(int, 0, size - SKB_MAX_HEAD(0)); =20 - data_len =3D min_t(size_t, size, PAGE_ALIGN(data_len)); + data_len =3D min_t(size_t, size, PAGE_ALIGN(data_len)); =20 - skb =3D sock_alloc_send_pskb(sk, size - data_len, data_len, - msg->msg_flags & MSG_DONTWAIT, &err, - get_order(UNIX_SKB_FRAGS_SZ)); + skb =3D sock_alloc_send_pskb(sk, size - data_len, data_len, + msg->msg_flags & MSG_DONTWAIT, &err, + get_order(UNIX_SKB_FRAGS_SZ)); + } if (!skb) goto out_err; =20 @@ -2224,13 +2277,21 @@ static int unix_stream_sendmsg(struct socket *sock,= struct msghdr *msg, } fds_sent =3D true; =20 - skb_put(skb, size - data_len); - skb->data_len =3D data_len; - skb->len =3D size; - err =3D skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size); - if (err) { - kfree_skb(skb); - goto out_err; + if (unlikely(msg->msg_flags & MSG_SPLICE_PAGES)) { + size =3D unix_extract_bvec_to_skb(skb, &msg->msg_iter, size); + skb->data_len +=3D size; + skb->len +=3D size; + skb->truesize +=3D size; + refcount_add(size, &sk->sk_wmem_alloc); + } else { + skb_put(skb, size - data_len); + skb->data_len =3D data_len; + skb->len =3D size; + err =3D skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size); + if (err) { + kfree_skb(skb); + goto out_err; + } } =20 unix_state_lock(other); From nobody Wed Feb 11 06:53:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9B935C7619A for ; Wed, 5 Apr 2023 16:58:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233664AbjDEQ6Y (ORCPT ); Wed, 5 Apr 2023 12:58:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229872AbjDEQ5K (ORCPT ); Wed, 5 Apr 2023 12:57:10 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF2336EB4 for ; Wed, 5 Apr 2023 09:54:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713690; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QZshbeXwoidsAPUStOu/ElI81f+RaVvzRteHlRNDwSs=; b=NJv6rvybgrz8U4JezGSdflvTwATv1CviLUd4rAIOtiKUJBBwSPwW9BdvYBesKBEr5icIix lHyfOnpkip6UzHwrEJfueQPEgga7qxenHnkDsmhAi6kD+xIKDATijraq9ohHRXQNdPazik 6tSJoabrlRDBGR0OXW7WQe08SHS/Kj0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-659-7ntbeM6RPUWZ_fvl6pOJLw-1; Wed, 05 Apr 2023 12:54:43 -0400 X-MC-Unique: 7ntbeM6RPUWZ_fvl6pOJLw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1C59F8996E4; Wed, 5 Apr 2023 16:54:42 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 19E08140EBF4; Wed, 5 Apr 2023 16:54:40 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH net-next v4 20/20] af_unix: Make sendmsg(MSG_SPLICE_PAGES) copy unspliceable data Date: Wed, 5 Apr 2023 17:53:39 +0100 Message-Id: <20230405165339.3468808-21-dhowells@redhat.com> In-Reply-To: <20230405165339.3468808-1-dhowells@redhat.com> References: <20230405165339.3468808-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" If sendmsg() with MSG_SPLICE_PAGES encounters a page that shouldn't be spliced - a slab page, for instance, or one with a zero count - make unix_extract_bvec_to_skb() copy it. Signed-off-by: David Howells cc: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- net/unix/af_unix.c | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index fee431a089d3..6941be8dae7e 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -2160,12 +2160,12 @@ static int queue_oob(struct socket *sock, struct ms= ghdr *msg, struct sock *other /* * Extract pages from an iterator and add them to the socket buffer. */ -static ssize_t unix_extract_bvec_to_skb(struct sk_buff *skb, - struct iov_iter *iter, ssize_t maxsize) +static ssize_t unix_extract_bvec_to_skb(struct sk_buff *skb, struct iov_it= er *iter, + ssize_t maxsize, gfp_t gfp) { struct page *pages[8], **ppages =3D pages; unsigned int i, nr; - ssize_t ret =3D 0; + ssize_t spliced =3D 0, ret =3D 0; =20 while (iter->count > 0) { size_t off, len; @@ -2177,31 +2177,52 @@ static ssize_t unix_extract_bvec_to_skb(struct sk_b= uff *skb, =20 len =3D iov_iter_extract_pages(iter, &ppages, maxsize, nr, 0, &off); if (len <=3D 0) { - if (!ret) - ret =3D len ?: -EIO; + ret =3D len ?: -EIO; break; } =20 i =3D 0; do { + struct page *page =3D pages[i++]; size_t part =3D min_t(size_t, PAGE_SIZE - off, len); + bool put =3D false; + + if (!sendpage_ok(page)) { + const void *p =3D kmap_local_page(page); + void *q; + + q =3D page_frag_memdup(NULL, p + off, part, gfp, + ULONG_MAX); + kunmap_local(p); + if (!q) { + iov_iter_revert(iter, len); + ret =3D -ENOMEM; + goto out; + } + page =3D virt_to_page(q); + off =3D offset_in_page(q); + put =3D true; + } =20 - if (skb_append_pagefrags(skb, pages[i++], off, part) < 0) { - if (!ret) - ret =3D -EMSGSIZE; + ret =3D skb_append_pagefrags(skb, page, off, part); + if (put) + put_page(page); + if (ret < 0) { + iov_iter_revert(iter, len); goto out; } off =3D 0; - ret +=3D part; + spliced +=3D part; maxsize -=3D part; len -=3D part; } while (len > 0); + if (maxsize <=3D 0) break; } =20 out: - return ret; + return spliced ?: ret; } =20 static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, @@ -2278,7 +2299,8 @@ static int unix_stream_sendmsg(struct socket *sock, s= truct msghdr *msg, fds_sent =3D true; =20 if (unlikely(msg->msg_flags & MSG_SPLICE_PAGES)) { - size =3D unix_extract_bvec_to_skb(skb, &msg->msg_iter, size); + size =3D unix_extract_bvec_to_skb(skb, &msg->msg_iter, size, + sk->sk_allocation); skb->data_len +=3D size; skb->len +=3D size; skb->truesize +=3D size;