From mboxrd@z Thu Jan 1 00:00:00 1970 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 CFAFEC77B7A for ; Thu, 1 Jun 2023 17:15:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232594AbjFARPj (ORCPT ); Thu, 1 Jun 2023 13:15:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232659AbjFARP2 (ORCPT ); Thu, 1 Jun 2023 13:15: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 C9198E2 for ; Thu, 1 Jun 2023 10:14:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1685639686; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=M7UAbNml4oAWGP++9AqxwAaw9U/MQwWhNaY462tBqx4=; b=igR2KjXgb3/16k3aIHz3+9LB/T4aTtlMbYh/s124YsfjrfakAP0mbmpXaPidbNuhIjlQic D0krDZJWtcWpx5Yj7HLJe+Qg5t+CssTJD+Omk5XzfE8BP+9gKlml1v5dlDcNYzojl9P+qN 9bzMpul5TOVhhJiCnt/2PBn+rT/NsZQ= 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-324-Dzf09lmbM7i9aB3uYBIKvQ-1; Thu, 01 Jun 2023 13:14:44 -0400 X-MC-Unique: Dzf09lmbM7i9aB3uYBIKvQ-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 6338385A5A8; Thu, 1 Jun 2023 17:14:43 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.42.28.182]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3AD31C154D7; Thu, 1 Jun 2023 17:14:41 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 From: David Howells In-Reply-To: References: <20230524153311.3625329-1-dhowells@redhat.com> <20230524153311.3625329-10-dhowells@redhat.com> <20230526180844.73745d78@kernel.org> <499791.1685485603@warthog.procyon.org.uk> <832277.1685630048@warthog.procyon.org.uk> To: Linus Torvalds Cc: dhowells@redhat.com, Jakub Kicinski , netdev@vger.kernel.org, "David S. Miller" , Eric Dumazet , Paolo Abeni , Willem de Bruijn , David Ahern , Matthew Wilcox , Jens Axboe , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Chuck Lever , Boris Pismenny , John Fastabend , Christoph Hellwig Subject: Re: Bug in short splice to socket? MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <909594.1685639680.1@warthog.procyon.org.uk> Content-Transfer-Encoding: quoted-printable Date: Thu, 01 Jun 2023 18:14:40 +0100 Message-ID: <909595.1685639680@warthog.procyon.org.uk> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Linus Torvalds wrote: > > However, this might well cause a malfunction in UDP, for example. > > MSG_MORE corks the current packet, so if I ask sendfile() say shove 32= K > > into a packet, if, say, 16K is read from the source and entirely > > transcribed into the packet, > = > If you use splice() for UDP, I don't think you would normally expect > to get all that well-defined packet boundaries. Actually, it will. Attempting to overfill a UDP packet with splice will g= et you -EMSGSIZE. It won't turn a splice into more than one UDP packet. I wonder if the right solution actually is to declare that the problem is userspace's. If you ask it to splice Z amount of data and it can't manage that because the source dries up prematurely, then make it so that you ass= ume it always passed MSG_MORE and returns a short splice to userspace. Usersp= ace can retry the splice/sendfile or do an empty sendmsg() to cap the message = (or cancel it). Perhaps flushing a short message is actually a *bad* idea. The answer then might be to make TLS handle a zero-length send() and fix t= he test cases. Would the attached changes then work for you? David --- diff --git a/fs/splice.c b/fs/splice.c index 3e06611d19ae..237688b0700b 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -956,13 +956,17 @@ ssize_t splice_direct_to_actor(struct file *in, stru= ct splice_desc *sd, */ bytes =3D 0; len =3D sd->total_len; + + /* Don't block on output, we have to drain the direct pipe. */ flags =3D sd->flags; + sd->flags &=3D ~SPLICE_F_NONBLOCK; = /* - * Don't block on output, we have to drain the direct pipe. + * We signal MORE until we've read sufficient data to fulfill the + * request and we keep signalling it if the caller set it. */ - sd->flags &=3D ~SPLICE_F_NONBLOCK; more =3D sd->flags & SPLICE_F_MORE; + sd->flags |=3D SPLICE_F_MORE; = WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail)); = @@ -978,14 +982,12 @@ ssize_t splice_direct_to_actor(struct file *in, stru= ct splice_desc *sd, sd->total_len =3D read_len; = /* - * If more data is pending, set SPLICE_F_MORE - * If this is the last data and SPLICE_F_MORE was not set - * initially, clears it. + * If we now have sufficient data to fulfill the request then + * we clear SPLICE_F_MORE if it was not set initially. */ - if (read_len < len) - sd->flags |=3D SPLICE_F_MORE; - else if (!more) + if (read_len >=3D len && !more) sd->flags &=3D ~SPLICE_F_MORE; + /* * NOTE: nonblocking mode only applies to the input. We * must not do the output in nonblocking mode as then we diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c index f63e4405cf34..5d48391da16c 100644 --- a/net/tls/tls_sw.c +++ b/net/tls/tls_sw.c @@ -995,6 +995,9 @@ static int tls_sw_sendmsg_locked(struct sock *sk, stru= ct msghdr *msg, } } = + if (!msg_data_left(msg) && eor) + goto copied; + while (msg_data_left(msg)) { if (sk->sk_err) { ret =3D -sk->sk_err; diff --git a/tools/testing/selftests/net/tls.c b/tools/testing/selftests/n= et/tls.c index e699548d4247..7df31583f2a4 100644 --- a/tools/testing/selftests/net/tls.c +++ b/tools/testing/selftests/net/tls.c @@ -377,7 +377,7 @@ static void chunked_sendfile(struct __test_metadata *_= metadata, char buf[TLS_PAYLOAD_MAX_LEN]; uint16_t test_payload_size; int size =3D 0; - int ret; + int ret =3D 0; char filename[] =3D "/tmp/mytemp.XXXXXX"; int fd =3D mkstemp(filename); off_t offset =3D 0; @@ -398,6 +398,9 @@ static void chunked_sendfile(struct __test_metadata *_= metadata, size -=3D ret; } = + if (ret < chunk_size) + EXPECT_EQ(send(self->fd, "", 0, 0), 0); + EXPECT_EQ(recv(self->cfd, buf, test_payload_size, MSG_WAITALL), test_payload_size); =