From patchwork Wed Jun 7 14:05:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13270805 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 DC669C77B7A for ; Wed, 7 Jun 2023 14:42:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7E5648E0003; Wed, 7 Jun 2023 10:42:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 76D818E0001; Wed, 7 Jun 2023 10:42:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5E8128E0003; Wed, 7 Jun 2023 10:42:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 4771D8E0001 for ; Wed, 7 Jun 2023 10:42:08 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 008BF1602F6 for ; Wed, 7 Jun 2023 14:42:07 +0000 (UTC) X-FDA: 80876216694.16.B2622BF Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf08.hostedemail.com (Postfix) with ESMTP id 23446160014 for ; Wed, 7 Jun 2023 14:42:03 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="M/t9JxpG"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf08.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686148924; 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=gP0ByWR0Rd7sP557iVrb+4r2WK9sDgokHEHx2x9rWhQhrIXpmnu4Lw845GPRZkAAMOU8ff llFd7ybcN9K83WFu/XMz8qVWue62n1ddRsh9WBUPTooXsc0sH2zm82DqnnjLW9opwR8up+ n79llniYEkpCj+WmVkrR+AJKVZFRF10= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="M/t9JxpG"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf08.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686148924; a=rsa-sha256; cv=none; b=VdfDK2dISnjEfA66nYHFQ06gKLGocAd7UjQ1FEG9riOotYhh/V2onYJBEnZ9HjySCRlRbZ x0etKg8x3gYGX1VXeZKBQBC+SLxrEa0fmyd5pPa9EEODerT2oI1f2KP+4tQgMujfFB5jqi dX3w5djt+IzWXsEj8OBszBFrOyAtdBc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686148923; 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=M/t9JxpGG1lbOnwlNGraRSDI9k3eoqUMm34VSpojC1SuXDkyN1Q6V7yXoxUuV2scLYcIb5 KVaangsk4sJou22PCdyWtVM+WeIKAxTmW9KtQvq7DDjttR44yCnUcfjAgi49Jzc9eFwvxu 3AN1CdREjEuFg0MA6MSIAvMdFvKTfXA= 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-81-yTkbrzWfOVqVewkXPojfRg-1; Wed, 07 Jun 2023 10:06:48 -0400 X-MC-Unique: yTkbrzWfOVqVewkXPojfRg-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 C33823806705; Wed, 7 Jun 2023 14:06:47 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.42.28.182]) by smtp.corp.redhat.com (Postfix) with ESMTP id F23512026D6A; Wed, 7 Jun 2023 14:06:43 +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 v5 10/14] splice, net: Fix SPLICE_F_MORE signalling in splice_direct_to_actor() Date: Wed, 7 Jun 2023 15:05:55 +0100 Message-ID: <20230607140559.2263470-11-dhowells@redhat.com> In-Reply-To: <20230607140559.2263470-1-dhowells@redhat.com> References: <20230607140559.2263470-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 23446160014 X-Stat-Signature: fpx8nffmayt565pgez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YyHDGXmFvAVujhMl6cACGaWGUrkkB2jc4d2NC2u0oLYdZmx+YkrELz+lkCXE2vJACzmB+wZBVfWXwbCsjcQHGFNTLI8vQDHcacVaPNk876ZYnJiYZ4TwG89JwstaTM97+bI5y62NF0hkQlSSlUPeEmTX7d8FYlxrgN4/g7vTENPSDpSRrSZTQXgrSLO045sFFpZCpziqsFCC79zAtMRm7x2AEz31/5tcDWSax65vpWOcnEH1UG5gBu2i/bT/NLja8YkuVvhmYDwSqIENpPTD+pGi0q5sn0qzQqGzkO5fdabFWtbUJS9rqpC2bjAI3BtjG/BpcwZJzMpUXQ30ROi5xqgtd02E0wXBDc4nQVwyCDixqI+CVxyjTbba9OrSGUqiQDsOATyiAuE4c82/eHrRICpYIS2UO42q8mQt9EMgKc2V/zvsboGsNjSe1x8VahlhAuAOhhnAYRveLW8QlSTovRI648jxJnB8htMPG4DethuWi6TVsOJium6IMWHH432TR+6MkST9vQx+XfRYU1/tu9ouTntywOXKc/VuRQl8M0Fj9LG3D7HgOfxK8/oqLvuyBpm//Ephcp2wt2ndHVIljxky4k69mpNR0ujfnkUVnGCEb2XLX3O9lWvCQgGpiDdw+KwZ2MQid65e5bBys4iHCpj+inxwIHi4B8qBWDXnkEhdSHGNdW1Ik1CIK8/jLw6UF/v5jwU8FVuXu62vPK3EczuADBMGbsGV4RWdXaFfET3FZo6arxAiQvtoPWuIOu3L/vgaKrBVAytN/dsOAoJ7Ccd7S8hkjq2k3fLqwRrlHfvvLVmepoea9VSdAavzR7KSn2AUITqWnVU= 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