From patchwork Wed Jun 7 18:19:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13271095 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2D669C7EE23 for ; Wed, 7 Jun 2023 18:20:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C505F900008; Wed, 7 Jun 2023 14:20:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BD834900004; Wed, 7 Jun 2023 14:20:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A520F900008; Wed, 7 Jun 2023 14:20:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 915E5900004 for ; Wed, 7 Jun 2023 14:20:23 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 57960A03C4 for ; Wed, 7 Jun 2023 18:20:23 +0000 (UTC) X-FDA: 80876766726.07.EF96011 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf04.hostedemail.com (Postfix) with ESMTP id 7D70D4001A for ; Wed, 7 Jun 2023 18:20:21 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XmzZmhrH; spf=pass (imf04.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686162021; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=VfP0KSvbwDpdDK3r99Lo/bGf0PzU9/ETfdNk/1wpqNM=; b=CQ6msQEJRJ3AUb6aW5sIGKgbEUaX8gd/6ICjibAO4w503LYmHmMWxEcL2KQ1FV0c6RjmAq R6DY3o+5z8Nx+a6VoI4ifS2aDmvsjyvQOiv3Ebbmxy2c8dc8LO9VbHjWgE0Q6X041Orru9 Y48ho5DoU4wGHMfVUryIqAMqoVw6iww= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686162021; a=rsa-sha256; cv=none; b=Ad5UNuXfksqd44GtfCLCdb7J0z/xuzb+IKUYAwhf5Yt/0mR5DGM5bddDVQ6AsfgvGiqJBK 93eD/O7wkw2TDulQ3M2Hz+0QTiAg5M7jTIURnJNd/yGQoRJkQko2OcyHcjrlEjsdC8pJqA Cx4LuSIaNAxY48JgCdiokPslfWXJls8= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XmzZmhrH; spf=pass (imf04.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686162020; 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=VfP0KSvbwDpdDK3r99Lo/bGf0PzU9/ETfdNk/1wpqNM=; b=XmzZmhrHBvC7TbWUbxI4pImqdBI0VWl8HktIUzdmKIgK9wjD1g5Odb0J1CdK3PY+K7PgJT pUkzaD9EsaLUYjq/5nyejZ8Yie5Dy+TslI2sH6qX9JypI50JQJcBT8PXHU3wzEOL//0KSG jVAQnIiZwIkcpauj6y2G+FY0raBk3ys= 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-643-JvJ2kpHPNDC1LBuwFYWsFg-1; Wed, 07 Jun 2023 14:20:17 -0400 X-MC-Unique: JvJ2kpHPNDC1LBuwFYWsFg-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 665F285A5BA; Wed, 7 Jun 2023 18:20:16 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.42.28.182]) by smtp.corp.redhat.com (Postfix) with ESMTP id 90EEE2026D49; Wed, 7 Jun 2023 18:20:13 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org, Linus Torvalds Cc: David Howells , Chuck Lever , Boris Pismenny , John Fastabend , Jakub Kicinski , "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, Christoph Hellwig , Al Viro , Jan Kara , Jeff Layton , David Hildenbrand , Christian Brauner , linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH net-next v6 10/14] splice, net: Fix SPLICE_F_MORE signalling in splice_direct_to_actor() Date: Wed, 7 Jun 2023 19:19:16 +0100 Message-ID: <20230607181920.2294972-11-dhowells@redhat.com> In-Reply-To: <20230607181920.2294972-1-dhowells@redhat.com> References: <20230607181920.2294972-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Stat-Signature: qenj8noa5oemtirb7jmqcaxh6srd8ywi X-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HJ0pb2lA502UGQrzmgkq0iVgwq7JubEydJAzrKeLBQMIFjC2JX7YYbUPh4E+a40vyUW/ERA+uJDQ9EjqZIfFJ6tR6REiblXFtqXna5gm5MXJTLb+QLoMv0a5qx+Bzjey0r9AYNl+hMMNOj1rTB48Utc2ye2XCKrSpfokwjiymoOiX6VxyqescsOdjTlSnCGoUxkhUdcVnDPig7oiBlNADdDM1rdvBfWRVo0knCol6mG072Jj7MSQT47NQEN1kiCD3vfvddafF8tXP91+bulyGa6FQJuQRwwl293v6cEdAze+oUmSdY326eE1EVqRnDJ+T28Cl0HbwSgZGm6Nu1SoAp4wXjpwRvejXjJDa4qP4+QYOnH/XRKOfPrJChGFu3mIngNIV3HToIWIauvSjlwJ612nyYnhu5VR9lno/JUdJnSy8PwV5asrYHMqGZkZgBiya5V1+jaisZV1GO/PMTRdaWhtRw32QyUH0P2eLTURXMwAO31pP29gmleR2u8VGK1+/KbKIEcn/VAU5/pEnevm5NVPTagOZehIyd1cGqnhltW9ib/DQDEPVm/59C69FGPjoHVYu4ixAAFTlREWRFALm27K5/biZ+2UTrNxNXx6XEpQxQFIJ67+gYmMfD9fyKAyAxVSJfmMlpD919Az1NK4l1Oeud1FmciEh6167FO23grfgzVUWsxSTG62/YcJM1zcFVqmBEksaY/Q/n0qko4B5PS7K9sh3shz2u9wqBZ3qDROr33m8aMF+L8WPGHsekqKS/MTIeTTgTmQz81gENQsJYU86KTRVj+1BAYDdxhIxqo9tKUVBeTa3DCT5CbLiuOk4owUEdYTbshzjL4xieoPxmlFYmKIS4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: splice_direct_to_actor() doesn't manage SPLICE_F_MORE correctly[1] - and, as a result, it incorrectly signals/fails to signal MSG_MORE when splicing to a socket. The problem I'm seeing happens when a short splice occurs because we got a short read due to hitting the EOF on a file: as the length read (read_len) is less than the remaining size to be spliced (len), SPLICE_F_MORE (and thus MSG_MORE) is set. The issue is that, for the moment, we have no way to know *why* the short read occurred and so can't make a good decision on whether we *should* keep MSG_MORE set. MSG_SENDPAGE_NOTLAST was added to work around this, but that is also set incorrectly under some circumstances - for example if a short read fills a single pipe_buffer, but the next read would return more (seqfile can do this). This was observed with the multi_chunk_sendfile tests in the tls kselftest program. Some of those tests would hang and time out when the last chunk of file was less than the sendfile request size: build/kselftest/net/tls -r tls.12_aes_gcm.multi_chunk_sendfile This has been observed before[2] and worked around in AF_TLS[3]. Fix this by making splice_direct_to_actor() always signal SPLICE_F_MORE if we haven't yet hit the requested operation size. SPLICE_F_MORE remains signalled if the user passed it in to splice() but otherwise gets cleared when we've read sufficient data to fulfill the request. If, however, we get a premature EOF from ->splice_read(), have sent at least one byte and SPLICE_F_MORE was not set by the caller, ->splice_eof() will be invoked. Signed-off-by: David Howells cc: Linus Torvalds cc: Jakub Kicinski cc: Jens Axboe cc: Christoph Hellwig cc: Al Viro cc: Matthew Wilcox cc: Jan Kara cc: Jeff Layton cc: David Hildenbrand cc: Christian Brauner cc: Chuck Lever cc: Boris Pismenny cc: John Fastabend cc: Eric Dumazet cc: "David S. Miller" cc: Paolo Abeni cc: linux-fsdevel@vger.kernel.org cc: linux-block@vger.kernel.org cc: linux-mm@kvack.org cc: netdev@vger.kernel.org Link: https://lore.kernel.org/r/499791.1685485603@warthog.procyon.org.uk/ [1] Link: https://lore.kernel.org/r/1591392508-14592-1-git-send-email-pooja.trivedi@stackpath.com/ [2] Link: https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git/commit/?id=d452d48b9f8b1a7f8152d33ef52cfd7fe1735b0a [3] --- Notes: ver #4) - Use ->splice_eof() to signal a premature EOF to the splice output. fs/splice.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/fs/splice.c b/fs/splice.c index 67dbd85db207..67ddaac1f5c5 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -1063,13 +1063,17 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, */ bytes = 0; len = sd->total_len; + + /* Don't block on output, we have to drain the direct pipe. */ flags = sd->flags; + sd->flags &= ~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 &= ~SPLICE_F_NONBLOCK; more = sd->flags & SPLICE_F_MORE; + sd->flags |= SPLICE_F_MORE; WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail)); @@ -1085,14 +1089,12 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, sd->total_len = 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 |= SPLICE_F_MORE; - else if (!more) + if (read_len >= len && !more) sd->flags &= ~SPLICE_F_MORE; + /* * NOTE: nonblocking mode only applies to the input. We * must not do the output in nonblocking mode as then we