From patchwork Thu Apr 6 09:42:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13203053 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 1C035C7618D for ; Thu, 6 Apr 2023 09:43:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC3C26B0082; Thu, 6 Apr 2023 05:43:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A72746B0083; Thu, 6 Apr 2023 05:43:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 914406B0085; Thu, 6 Apr 2023 05:43:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 7EA686B0082 for ; Thu, 6 Apr 2023 05:43:17 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 580CCA0CB7 for ; Thu, 6 Apr 2023 09:43:17 +0000 (UTC) X-FDA: 80650478034.04.3EE4F36 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 827CC4001A for ; Thu, 6 Apr 2023 09:43:15 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=IaVgFzp8; 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=1680774195; 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=srNjm2pDsZ7bySZjGb738UmToW4CeRBJkACWB8iZ9Oo=; b=xAFckvK1h2nfHabl+XYnNajVG7TXBjKQ8ARtJuP1nsMvJuL9s/CtX00fIaQ69Cf9rGrDVu ONZrBZfYNWza1I4dgeowhlJBm4eWv6H1BBaWxRCQgiLp5XHHGI3zdTi6ERTQxXBTDsBzav mlu1bCMkPcbhLAmfp4e8rMjlgQyHqA8= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=IaVgFzp8; 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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680774195; a=rsa-sha256; cv=none; b=hjg2YHF58A2p7Cp1gYS16TM1z9jMilYOWOTNZqZk+2nqVCaosyoj7VNfn7uFIkO515uFEO vgGKIMg27roYmMDJZ8nNNrrLxWlvoOR71pDp74+FQ50yIUhr4ZCPqDe0qVONbKW8aS9gl2 +wKfqwugPPqcNQwlxX40dtz+DpuK63M= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680774194; 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=srNjm2pDsZ7bySZjGb738UmToW4CeRBJkACWB8iZ9Oo=; b=IaVgFzp8oCASI2JULY6NPOutU8DimJ1ScIxFRsj632xKc2RJktkDtWqsEay6XUHSDGToun Zm4vjntvctYnF5Rku1bSYuS4sUasDV4RTRQ69IYMtV1m3qS5rTRNpQJMW9GfGnqueBtSI3 aK+9ngfZPyxs/4IbRgG96evqO1zbXyw= 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-237-DOKvdq12P_Wm-Yg8pakj4Q-1; Thu, 06 Apr 2023 05:43:11 -0400 X-MC-Unique: DOKvdq12P_Wm-Yg8pakj4Q-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B992E38601AC; Thu, 6 Apr 2023 09:43:10 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id A7FC6492C14; Thu, 6 Apr 2023 09:43:08 +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, David Ahern Subject: [PATCH net-next v5 06/19] tcp: Make sendmsg(MSG_SPLICE_PAGES) copy unspliceable data Date: Thu, 6 Apr 2023 10:42:32 +0100 Message-Id: <20230406094245.3633290-7-dhowells@redhat.com> In-Reply-To: <20230406094245.3633290-1-dhowells@redhat.com> References: <20230406094245.3633290-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Stat-Signature: 1cwr6i8xha3h6u9k7pwc1acsfz586amr X-Rspam-User: X-Rspamd-Queue-Id: 827CC4001A X-Rspamd-Server: rspam06 X-HE-Tag: 1680774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Ez6y+X83OS3ciIjT9vKYjRB2dKYgM9qXw9T0SDg4fq/zDByL9tlVDsAiB/f8i+swCbfp54z8ymwFGU5Jc9GN0scSwg9190nrFGtmecrTzkKySZ4wJjY3vW90YBwrNF5EU1hdIw/z4D6F6IEfUnsKfNtveRb+in66Ul200zx4SJZtIqJ5+GdOVLW93g11L3m7MeqchJ56rJfWloktKQAgE64/9lnnCi9eQWmbbxQeurhRXm6B+hslGQWwRVqvlH3J1KE20Y4G7D2y078h9Ql9LdfpVwSvlMInA2O781v3AqA92EiO5biMU0xyHp9g8vdXnFkiROjhMC/G65uaMYK/Jdif+T9VXX6hqThI66IO00yTlpmrqk9G4ZTONhIFMkiQE6iw8tBxtr37lP1maTRvmP9PocJOz2Gur3UeW6I6omnMd99 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: 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: David Ahern 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 msghdr *msg, size_t size) goto do_error; copy = err; } else if (zc == 2) { - /* Splice in data. */ + /* Splice in data if we can; copy if we can't. */ struct page *page = NULL, **pages = &page; size_t off = 0, part; - bool can_coalesce; + bool can_coalesce, put = false; int i = skb_shinfo(skb)->nr_frags; copy = iov_iter_extract_pages(&msg->msg_iter, &pages, @@ -1448,12 +1448,34 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) goto wait_for_space; copy = part; + if (!sendpage_ok(page)) { + const void *p = kmap_local_page(page); + void *q; + + q = 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 = copy ?: -ENOMEM; + goto do_error; + } + page = virt_to_page(q); + off = offset_in_page(q); + put = true; + can_coalesce = 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 = false; skb_fill_page_desc_noacc(skb, i, page, off, copy); } + if (put) + put_page(page); page = NULL; if (!(flags & MSG_NO_SHARED_FRAGS))