From patchwork Wed Apr 5 16:53:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13202260 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 E1061C761AF for ; Wed, 5 Apr 2023 16:54:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7BE5C6B007E; Wed, 5 Apr 2023 12:54:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 76E3C6B0080; Wed, 5 Apr 2023 12:54:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 60F016B0081; Wed, 5 Apr 2023 12:54:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 5172A6B007E for ; Wed, 5 Apr 2023 12:54:16 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 2BDB841155 for ; Wed, 5 Apr 2023 16:54:16 +0000 (UTC) X-FDA: 80647935312.09.D26D39F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf28.hostedemail.com (Postfix) with ESMTP id 7A1E1C0017 for ; Wed, 5 Apr 2023 16:54:14 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QQk6nXxk; spf=pass (imf28.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=1680713654; 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=5sjNMjWipBo1Z72oEKiWWK/H6upfHkIbrOvJ4ujx2jY=; b=R61midC2OEHbILCjh/hsSwexAMg5MRDYIIEkmT+RM+sWCZxas8mjWzlVWDFf3GzhVZlRjd dckRzby0cPS1btvlNlV/wOpw+7brPes8wHnqlWO0IGKZag6Vh+l5jBzkCEsNYP2wDx8jJe 3dlfhBQFAz8dDwbo3lLyWhimk1uXW80= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QQk6nXxk; spf=pass (imf28.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=1680713654; a=rsa-sha256; cv=none; b=w3Q+tHlUbW9LA2QQ5VERFz4AvigWS7R+dfIXmyJjyIQmv2Eh8w00WVIB4hYsl7RK4HpskU /Pp1F3rHarOadavQtLAbiBJtiMSAxX/g3XQPvnn0aajVFtdHAgGMPqz+jttA2BzfWPHXa2 WVSQadSp24wG5f6i7LzTdzU/iPB768A= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680713653; 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=QQk6nXxkjrSviRnhzZ9W6cJl85Ma0mRe4lBHpumNt5ihc3J/7UQYRt5qxwEhgkvvvlQbFA MTrCj/0eP6S4Dk966RJFdd4ZywECPyU19/py4cYYDPSxIG5xAXOBCK3M9FyDzbLjI3Lu9o ardr1aYo/LlmoJE+yK3OAN71dmyt4ro= 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 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Stat-Signature: zpodqrgcm6skz8di8ihdeafwmd5stx7o X-Rspam-User: X-Rspamd-Queue-Id: 7A1E1C0017 X-Rspamd-Server: rspam06 X-HE-Tag: 1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V/gdGraXKKEAr/B3FCz2fwcuvPYfW5nrPe28t8OGpBO/pBVDRwLO/RcDSJ7K3sCkokvBrarSw8pJqSzMkybvRQpU48P/g4UF6CFTtjnjA0SLR9+cpUH2Liv96A6ltinEJry5NLNzd2KJ/5J6uVEuR9dQvmHehh7F67HTzk9FvO365qm7Gzhgp1/4EatmgV/7Ql2Bw3dOzTSA4LVIMNrzOI7N4UGnw/09NtMZIKIpDH6+Qjiedj1SxJ4zZG5iqUQG+GQJ/HTuKqxsy6zagmbsg4Jfoz1A4gveDLXf6OZqdDfSqCzJGQw5UuavMBU6HSkiULfo2tjxtd2bDUQf2ka1Umz/7RByMv5eeiDub/J9DjbZJfbjkmjhhXt6szqwX7tPnMlfua7SJk/GgFPG+W+WuThTAFG3pdA9lqugbJbR8VRyeaZwBcMAh 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: 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))