From patchwork Mon Feb 13 13:46: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: 56302 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2360384wrn; Mon, 13 Feb 2023 06:01:50 -0800 (PST) X-Google-Smtp-Source: AK7set+NrMqOuFH8s0ii3WyLJzIqo4bX4Qxaw03P74E0e7tgRR9uv/0AQDc8+MAgDyy1m/2cdDCY X-Received: by 2002:a50:c38a:0:b0:4ac:c9d9:94c with SMTP id h10-20020a50c38a000000b004acc9d9094cmr2617446edf.7.1676296910326; Mon, 13 Feb 2023 06:01:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676296910; cv=none; d=google.com; s=arc-20160816; b=PuffEtkNkfBPoYVpfylJ2bOZ3L/yi/S0ftVyj379zkjnMCuHc5292Z9kQ7qXsZhLG3 WFftJ35iyqNw7KLbz3bbwm3SDlDzqDEsYxtndKzgYhDQ+rhDx1FmyFLVqTaN2qv4ekp9 4Ogvx6vyBRER9XASP3wiowP/Z9ij3SMOqAaGCuE6WdilaF7eeQeC2yA3nXT1MpFfnbgm S4M/y3ygumPQo1IdCmS5eNnNqhtOuR8SBhsq9GwWx01PtSO+mptJ20oRYCTj9vPYtn68 sznTD4kuET9n0gWWmmPjE7OlptYe4rKvGvnQPjtm1zvcCdpWAIH6FNE6lWmpfTJZCxiH YQjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=cowZ1cmLrnZZxcephacOQa4mjhuuVERSSt8ZL/UsOKw=; b=JMCsxWrL9dKABF1QmEac15XFTcdsuCv5h/jGvqtUfozKd6Lp7nfrPQvQzzCw7NSSOs 2YIMHFfS1+C7lmV7B6ulEEdlt/TYOLb1qaLwxhLTyJ+ZGZHXzDDCY+cA6AuMyeCCKUqG X7HXruTr8KQp7l4nIv1BjhSbk5X40r1RAWWetJ4XYzbJWcigWarqXlWROtbH7thQyPXn iRjyaOXEwlz6RRgxwphkcTDCMr+Jiea8whCb3ullZpuznuPKf5+Fspxc1IQhgTfOzwNB fvNeVTEWdM5iEUs7q5dXEsB6ffZt7LzYfqfJYp2tbcTZNTeQRVFK91S7eyaPaTT8hIoi oRSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Q+P+dyif; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j7-20020aa7c407000000b004acc68a5878si3995695edq.274.2023.02.13.06.01.25; Mon, 13 Feb 2023 06:01:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Q+P+dyif; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229521AbjBMNrN (ORCPT + 99 others); Mon, 13 Feb 2023 08:47:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49280 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229554AbjBMNrM (ORCPT ); Mon, 13 Feb 2023 08:47:12 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D798B1ABD5 for ; Mon, 13 Feb 2023 05:46:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676295989; 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=cowZ1cmLrnZZxcephacOQa4mjhuuVERSSt8ZL/UsOKw=; b=Q+P+dyiflwfVrbjJKVHlGuTN+JG/dWe3sP2i5ngTCKlZ8IP2CnYBtJkzzIR8LQg/JbSQy8 JgLsF7dVBa1Z1jGULzdEXmi6eWWVnX3czkj7wFhWoAkZwWKe0QO7aYvL+8GKaRaGuEbgpZ RVSvtt8y0ebybJ9sCn76rU7SH6d/jh4= 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-167-u6s-Mzk-MTWOuQY7_HuoaQ-1; Mon, 13 Feb 2023 08:46:27 -0500 X-MC-Unique: u6s-Mzk-MTWOuQY7_HuoaQ-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 EF5EE971086; Mon, 13 Feb 2023 13:46:25 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0AE40492C3C; Mon, 13 Feb 2023 13:46:23 +0000 (UTC) From: David Howells To: Jens Axboe , Al Viro , Christoph Hellwig Cc: David Howells , Matthew Wilcox , Jan Kara , Jeff Layton , David Hildenbrand , Jason Gunthorpe , Logan Gunthorpe , Hillf Danton , linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Christoph Hellwig , John Hubbard Subject: [PATCH 1/4] splice: Rename new splice functions Date: Mon, 13 Feb 2023 13:46:16 +0000 Message-Id: <20230213134619.2198965-2-dhowells@redhat.com> In-Reply-To: <20230213134619.2198965-1-dhowells@redhat.com> References: <20230213134619.2198965-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757724708969303950?= X-GMAIL-MSGID: =?utf-8?q?1757724708969303950?= Rename generic_file_buffered_splice_read() to filemap_splice_read(). Rename generic_file_direct_splice_read() to direct_splice_read(). Requested-by: Christoph Hellwig Signed-off-by: David Howells cc: Christoph Hellwig cc: Jens Axboe cc: Al Viro cc: John Hubbard cc: David Hildenbrand cc: Matthew Wilcox cc: linux-block@vger.kernel.org cc: linux-fsdevel@vger.kernel.org cc: linux-mm@kvack.org Reviewed-by: Christoph Hellwig --- fs/splice.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/fs/splice.c b/fs/splice.c index 2717078949a2..91b9e2cb9e03 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -287,9 +287,9 @@ void splice_shrink_spd(struct splice_pipe_desc *spd) * Splice data from an O_DIRECT file into pages and then add them to the output * pipe. */ -static ssize_t generic_file_direct_splice_read(struct file *in, loff_t *ppos, - struct pipe_inode_info *pipe, - size_t len, unsigned int flags) +static ssize_t direct_splice_read(struct file *in, loff_t *ppos, + struct pipe_inode_info *pipe, + size_t len, unsigned int flags) { struct iov_iter to; struct bio_vec *bv; @@ -417,10 +417,9 @@ static size_t splice_folio_into_pipe(struct pipe_inode_info *pipe, * Splice folios from the pagecache of a buffered (ie. non-O_DIRECT) file into * a pipe. */ -static ssize_t generic_file_buffered_splice_read(struct file *in, loff_t *ppos, - struct pipe_inode_info *pipe, - size_t len, - unsigned int flags) +static ssize_t filemap_splice_read(struct file *in, loff_t *ppos, + struct pipe_inode_info *pipe, + size_t len, unsigned int flags) { struct folio_batch fbatch; size_t total_spliced = 0, used, npages; @@ -529,8 +528,8 @@ ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, if (unlikely(!len)) return 0; if (in->f_flags & O_DIRECT) - return generic_file_direct_splice_read(in, ppos, pipe, len, flags); - return generic_file_buffered_splice_read(in, ppos, pipe, len, flags); + return direct_splice_read(in, ppos, pipe, len, flags); + return filemap_splice_read(in, ppos, pipe, len, flags); } EXPORT_SYMBOL(generic_file_splice_read); From patchwork Mon Feb 13 13:46:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 56304 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2372313wrn; Mon, 13 Feb 2023 06:22:29 -0800 (PST) X-Google-Smtp-Source: AK7set/1PQJnX67aT9wAAQqFEQaK9G5gL3rm8S7ZGum9E5e2A3lhpC6zlUxz3VXDtgIh+kLT9XvV X-Received: by 2002:a50:d085:0:b0:4ab:1681:b99e with SMTP id v5-20020a50d085000000b004ab1681b99emr7122451edd.4.1676298149671; Mon, 13 Feb 2023 06:22:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676298149; cv=none; d=google.com; s=arc-20160816; b=hBrkd9nj23pKekN9pBbsHxjZZkYsR2rFXZBERQVXe9nZwHdMCkmeQlH0ic3Uv8GoGj 1M+zGot2O05ZExtjfXrSa+hgCjbIYUZ5ePWTSHVc92zE1icz9vPNc6qnfuynqM/U5uCm DyA/7Kbne8UXIJijNxMlcsZEDbmTvUPuLnV/vsspo49MgpSsigOabxJDPo7U1apdg4dG JqQPEBgqF88mmn8Gef8qL4VC1Ly1ZMd4QT/0NLgHfM4hY6mUNHCArKiNJwAHO3lgLxtg b8PFyAScWsH7s5mpBQPvoWbGCFbZ7vH783/9veNgcUfqB091gB+vRd/Fp1pFsyf6Kav2 vh5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Nvl8+FROX68UViRruMCdGgXgWsaTBkOPXdZwOet0pQY=; b=KNFvuKshCGdvm/GVuzkFr4qkSl1MNYSod76Pu/ICI8Dj6P2epNbtNaji/rUzbrpwrH wQ600uH1DSvFzWdXOjyFCkNZgwwssapC9zE/zCFTAxpsZ00bjASrx5KlhdDBVjzPKYHG HEuem4Y0sw6eF25EYcRh+Ix0UfPGQxxKPV647M1xyPWSlvaPtd04s7ZRi2G5cfhTxUwK joH01IvtekKOckQXrxmEYVma4C1QNY6nh4Z3xhuvWR05IUsE64PDFIPZdI7cEg0vYQ4a xjnosQLR8/PExkBCdBEtR/LdiyJDRiQiJPenM31ZQ1rZ+sbTsnLj5JkQZyMEpP4Gw6Xz AHHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aCgPqUc8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w4-20020a056402128400b004acc613462asi4283381edv.78.2023.02.13.06.22.06; Mon, 13 Feb 2023 06:22:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aCgPqUc8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230374AbjBMNsK (ORCPT + 99 others); Mon, 13 Feb 2023 08:48:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230285AbjBMNsB (ORCPT ); Mon, 13 Feb 2023 08:48:01 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50B051ADE7 for ; Mon, 13 Feb 2023 05:46:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676295992; 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=Nvl8+FROX68UViRruMCdGgXgWsaTBkOPXdZwOet0pQY=; b=aCgPqUc8cO2iel1mn9sznB0epdXZxv6FMa98KT7yVPkmqEDDe2aBIuUQnKbVww6BhDjI2Y mx7vO4MBdyUnveiI6XoMTPjtgLlr/rywivoYFNiGGfF4tlZ6IJJ1IAGk1mAkqPSOyW5LaQ jGOzT98gJbXwuqZ5s8BMpad4MsFeoEc= 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-447-Qzx4kRCYOYyoMJ2DNcD_ow-1; Mon, 13 Feb 2023 08:46:29 -0500 X-MC-Unique: Qzx4kRCYOYyoMJ2DNcD_ow-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 A0EAF857AA4; Mon, 13 Feb 2023 13:46:28 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id A5926C16022; Mon, 13 Feb 2023 13:46:26 +0000 (UTC) From: David Howells To: Jens Axboe , Al Viro , Christoph Hellwig Cc: David Howells , Matthew Wilcox , Jan Kara , Jeff Layton , David Hildenbrand , Jason Gunthorpe , Logan Gunthorpe , Hillf Danton , linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Christoph Hellwig , John Hubbard Subject: [PATCH 2/4] splice: Provide pipe_head_buf() helper Date: Mon, 13 Feb 2023 13:46:17 +0000 Message-Id: <20230213134619.2198965-3-dhowells@redhat.com> In-Reply-To: <20230213134619.2198965-1-dhowells@redhat.com> References: <20230213134619.2198965-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757726008763793317?= X-GMAIL-MSGID: =?utf-8?q?1757726008763793317?= Provide a helper, pipe_head_buf(), to get the current head buffer from a pipe. Implement this as a wrapper around a more general function, pipe_buf(), that gets a specified buffer. Requested-by: Christoph Hellwig Signed-off-by: David Howells cc: Christoph Hellwig cc: Jens Axboe cc: Al Viro cc: John Hubbard cc: David Hildenbrand cc: Matthew Wilcox cc: linux-block@vger.kernel.org cc: linux-fsdevel@vger.kernel.org cc: linux-mm@kvack.org Reviewed-by: Christoph Hellwig --- fs/splice.c | 9 +++------ include/linux/pipe_fs_i.h | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/fs/splice.c b/fs/splice.c index 91b9e2cb9e03..7c0ff187f87a 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -295,7 +295,6 @@ static ssize_t direct_splice_read(struct file *in, loff_t *ppos, struct bio_vec *bv; struct kiocb kiocb; struct page **pages; - unsigned int head; ssize_t ret; size_t used, npages, chunk, remain, reclaim; int i; @@ -358,9 +357,8 @@ static ssize_t direct_splice_read(struct file *in, loff_t *ppos, } /* Push the remaining pages into the pipe. */ - head = pipe->head; for (i = 0; i < npages; i++) { - struct pipe_buffer *buf = &pipe->bufs[head & (pipe->ring_size - 1)]; + struct pipe_buffer *buf = pipe_head_buf(pipe); chunk = min_t(size_t, remain, PAGE_SIZE); *buf = (struct pipe_buffer) { @@ -369,10 +367,9 @@ static ssize_t direct_splice_read(struct file *in, loff_t *ppos, .offset = 0, .len = chunk, }; - head++; + pipe->head++; remain -= chunk; } - pipe->head = head; kfree(bv); return ret; @@ -394,7 +391,7 @@ static size_t splice_folio_into_pipe(struct pipe_inode_info *pipe, while (spliced < size && !pipe_full(pipe->head, pipe->tail, pipe->max_usage)) { - struct pipe_buffer *buf = &pipe->bufs[pipe->head & (pipe->ring_size - 1)]; + struct pipe_buffer *buf = pipe_head_buf(pipe); size_t part = min_t(size_t, PAGE_SIZE - offset, size - spliced); *buf = (struct pipe_buffer) { diff --git a/include/linux/pipe_fs_i.h b/include/linux/pipe_fs_i.h index 6cb65df3e3ba..d2c3f16cf6b1 100644 --- a/include/linux/pipe_fs_i.h +++ b/include/linux/pipe_fs_i.h @@ -156,6 +156,26 @@ static inline bool pipe_full(unsigned int head, unsigned int tail, return pipe_occupancy(head, tail) >= limit; } +/** + * pipe_buf - Return the pipe buffer for the specified slot in the pipe ring + * @pipe: The pipe to access + * @slot: The slot of interest + */ +static inline struct pipe_buffer *pipe_buf(const struct pipe_inode_info *pipe, + unsigned int slot) +{ + return &pipe->bufs[slot & (pipe->ring_size - 1)]; +} + +/** + * pipe_head_buf - Return the pipe buffer at the head of the pipe ring + * @pipe: The pipe to access + */ +static inline struct pipe_buffer *pipe_head_buf(const struct pipe_inode_info *pipe) +{ + return pipe_buf(pipe, pipe->head); +} + /** * pipe_buf_get - get a reference to a pipe_buffer * @pipe: the pipe that the buffer belongs to From patchwork Mon Feb 13 13:46:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 56305 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2375586wrn; Mon, 13 Feb 2023 06:29:10 -0800 (PST) X-Google-Smtp-Source: AK7set8TR/MqWv8sg/zr7zO/EEDjhiJN1Oav6VL/zHZnNlZ0y5tUDhpi2+58Jlt10VxsFsrv3xav X-Received: by 2002:a50:d7da:0:b0:4ac:bd73:3b0e with SMTP id m26-20020a50d7da000000b004acbd733b0emr6594370edj.32.1676298549996; Mon, 13 Feb 2023 06:29:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676298549; cv=none; d=google.com; s=arc-20160816; b=HsuiO8phm5xN74rdNxt2dn1ERDIfz0PV/HhrzM4fBvl/29WQPebpgOKECY+Sc+2wmG iOhfl/TfD8tf3lyUjhIj8D00F+DgLLjPYlbu04sVzojlV3TTfkzcPiMdNt6F9g+s61ed tczJCPdI+mFxAc3WSgpyj6qTgugViXazCcHm5NSbaqzDl8YvmSHZSe1UN2GsE1Ml2JRf ugOz0xJfmZchQsP4OkLVaojCsxo101eqeJzFn9PDQgxck5qxEDmfR9soPi1pmchPoPxT DScD1/6yRqmyhCyRn40Oq/wiSvG4pjqccYAJ2Sl1TH5t2z4ynvI/+xCluIuSpJaSo6Ds 2E9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4Gd+AM0ivl8H3lv5f0bcfNcViU9oHee8wwcgLJRIkXo=; b=bujaGRW7xTLEEan9xX7pVzZ05ffL2nw8CS5p92sioVw1tFU4khD/EEk8tIzGeUfu4t VfrrUyCDbZL4lEuAMvA0iqydBaTDyXcvVvblQ5y0ZvSvlSE3CwPxIHyZkrneCJk56bks dY9pPD+cJzweLPwYW36/TSJ8Ytj5sM1X+L9rR+eOq70YFWPOPydtQA+s033qKe0lLBhw SQc57xplcioPifAjHx0wgreb5DXMKTpMsNyec+VrUT/TlnAWX11M/KrO/mK50mB/KY2n gMv4U3vnXYRBO29cspIM0MU64wUcNmqNtXrU4d+KnnOs4Ic+hcCACL5joqeOtt1aCgZy rUlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=BlOUek+N; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n19-20020a05640206d300b004acb305fe3bsi8268469edy.605.2023.02.13.06.28.47; Mon, 13 Feb 2023 06:29:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=BlOUek+N; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230424AbjBMNs0 (ORCPT + 99 others); Mon, 13 Feb 2023 08:48:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230416AbjBMNsN (ORCPT ); Mon, 13 Feb 2023 08:48:13 -0500 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 76C711B578 for ; Mon, 13 Feb 2023 05:46:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676296013; 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=4Gd+AM0ivl8H3lv5f0bcfNcViU9oHee8wwcgLJRIkXo=; b=BlOUek+Nk/cL7kv43+7d4YUm1Wbn+bJpaEX4XvJSU/OUvivwGm3fOmVu2xtPbxSMbZ7QaW K6ic+/ONXwpXwgFh2MUaoJ6rhxzdJ1apyg3tWI7X6ppda6OylbEvpO7BPn0o6PHBhui0ru SomSdxvBJ67QfMllgDQFmBsPbClYtuY= 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-58-5GjRqJH1Nv2E9X1dNEvJAQ-1; Mon, 13 Feb 2023 08:46:47 -0500 X-MC-Unique: 5GjRqJH1Nv2E9X1dNEvJAQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 367EF3C16E82; Mon, 13 Feb 2023 13:46:31 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 41D452166B26; Mon, 13 Feb 2023 13:46:29 +0000 (UTC) From: David Howells To: Jens Axboe , Al Viro , Christoph Hellwig Cc: David Howells , Matthew Wilcox , Jan Kara , Jeff Layton , David Hildenbrand , Jason Gunthorpe , Logan Gunthorpe , Hillf Danton , linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Christoph Hellwig , John Hubbard Subject: [PATCH 3/4] splice: Use init_sync_kiocb() in filemap_splice_read() Date: Mon, 13 Feb 2023 13:46:18 +0000 Message-Id: <20230213134619.2198965-4-dhowells@redhat.com> In-Reply-To: <20230213134619.2198965-1-dhowells@redhat.com> References: <20230213134619.2198965-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757726428481762088?= X-GMAIL-MSGID: =?utf-8?q?1757726428481762088?= Use init_sync_kiocb() in filemap_splice_read() rather than open coding it and filter out IOCB_NOWAIT since the splice is synchronous. Requested-by: Christoph Hellwig Signed-off-by: David Howells cc: Christoph Hellwig cc: Jens Axboe cc: Al Viro cc: John Hubbard cc: David Hildenbrand cc: Matthew Wilcox cc: linux-block@vger.kernel.org cc: linux-fsdevel@vger.kernel.org cc: linux-mm@kvack.org --- fs/splice.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fs/splice.c b/fs/splice.c index 7c0ff187f87a..8b2a9d963bc4 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -419,15 +419,15 @@ static ssize_t filemap_splice_read(struct file *in, loff_t *ppos, size_t len, unsigned int flags) { struct folio_batch fbatch; + struct kiocb iocb; size_t total_spliced = 0, used, npages; loff_t isize, end_offset; bool writably_mapped; int i, error = 0; - struct kiocb iocb = { - .ki_filp = in, - .ki_pos = *ppos, - }; + init_sync_kiocb(&iocb, in); + iocb.ki_pos = *ppos; + iocb.ki_flags &= IOCB_NOWAIT; /* Work out how much data we can actually add into the pipe */ used = pipe_occupancy(pipe->head, pipe->tail); From patchwork Mon Feb 13 13:46:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 56303 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2371468wrn; Mon, 13 Feb 2023 06:21:11 -0800 (PST) X-Google-Smtp-Source: AK7set/cqiEKlvJE5Cl2tfJILLkFYAVJvgA0CaSC0YLLixJWwBzCPBZAnEQjLaRlH/FnzgeHB0RG X-Received: by 2002:a50:bace:0:b0:4ac:cc7a:24c1 with SMTP id x72-20020a50bace000000b004accc7a24c1mr2399485ede.37.1676298071164; Mon, 13 Feb 2023 06:21:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676298071; cv=none; d=google.com; s=arc-20160816; b=uiSlJDaJShbnEcycCbBMEpyCRxyfQIg7EuLvkL8jwPCQyCLavsEVaVKPesec3lajJM eGJngGtwHaTcgk783z/WQCzt7smf6JYqj57jggsPmhX2YzLd06gQYrSZzDOlM6q4Uf0A 1nI177lZiaRd6pc8rV297Zd69WPNyOnD1sdiwr7b4rhUT398QYGqv9s/MNcYRAG4M4A3 dFNA0CEdFcykJqLte936Cr7fOr4T8snoeAo4l3kLYevyP1CxFcqGzpIutkwlE2PjsMZ2 G+NCPL6lj4kSRNFdc8IuZH6Q417wVtqy+G619C0+QigjcX+rNVpjZe5p9RuTnlVQN88W PELQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=v3dWdulFMkC02AM2jYIVI6FYFE9HkcrX92HlV1c4tO4=; b=1ESCgPmR5dFzh1dSjxRnXy3JzRBEWQjos7+FjJB88iPq3+SkTzjOqY7wrEH1sj6k4h zYbRbJKnHwBorp/9tvH4GjmkAbNgRY6vSiVOj/V87SrMHFM8m1Ft32bn3Uusqb/Ude7z x6RCRIXgEe/WhOqf6ifXYOFuKzEmOAOG2dz+5t0g38XAaEJSwMDLYfOmxWxklQApDu+8 6opR8R2rrXgqsrSXGK8D7cLZGJkJoDj1uRaPUAFOFzDSAW1wTv9vhijdgPSQ1Zzh0YvD KMjsFfhWjKCP//7Rnt4Y6MkYPfIAcg6Jmh/lxMB8dkhy6St4ePPloPuYKWo+FbW65Wfy CN0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="E/7QJ64H"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w4-20020a056402128400b004acc613462asi4283381edv.78.2023.02.13.06.20.46; Mon, 13 Feb 2023 06:21:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="E/7QJ64H"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230344AbjBMNsG (ORCPT + 99 others); Mon, 13 Feb 2023 08:48:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230331AbjBMNsC (ORCPT ); Mon, 13 Feb 2023 08:48:02 -0500 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 75D411B555 for ; Mon, 13 Feb 2023 05:46:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676295999; 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=v3dWdulFMkC02AM2jYIVI6FYFE9HkcrX92HlV1c4tO4=; b=E/7QJ64Ha2EtM7b92+/IqfQKUWnrb5Ftf8V4vWDOY9bprSN7PSG38UCA21eFN42QMfPUDM 1SgcOiDI9mTRQhq95e8FMw+sI6tUfkTsYa2e/QYQArFenFZYsz41CzTyuPEKchXmWNxYNT 4U0+QO2wh3t+rUDptV4hozPwLoY4rfY= 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-510-H46sO8NjN5-GuV9-iHBZng-1; Mon, 13 Feb 2023 08:46:34 -0500 X-MC-Unique: H46sO8NjN5-GuV9-iHBZng-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 D32F1885620; Mon, 13 Feb 2023 13:46:33 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id E0CB7C16022; Mon, 13 Feb 2023 13:46:31 +0000 (UTC) From: David Howells To: Jens Axboe , Al Viro , Christoph Hellwig Cc: David Howells , Matthew Wilcox , Jan Kara , Jeff Layton , David Hildenbrand , Jason Gunthorpe , Logan Gunthorpe , Hillf Danton , linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Christoph Hellwig , John Hubbard Subject: [PATCH 4/4] splice: Move filemap_read_splice() to mm/filemap.c Date: Mon, 13 Feb 2023 13:46:19 +0000 Message-Id: <20230213134619.2198965-5-dhowells@redhat.com> In-Reply-To: <20230213134619.2198965-1-dhowells@redhat.com> References: <20230213134619.2198965-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757725926176397806?= X-GMAIL-MSGID: =?utf-8?q?1757725926176397806?= Move filemap_read_splice() to mm/filemap.c and make filemap_get_pages() static again. Requested-by: Christoph Hellwig Signed-off-by: David Howells cc: Christoph Hellwig cc: Jens Axboe cc: Al Viro cc: John Hubbard cc: David Hildenbrand cc: Matthew Wilcox cc: linux-block@vger.kernel.org cc: linux-fsdevel@vger.kernel.org cc: linux-mm@kvack.org Reviewed-by: Christoph Hellwig --- fs/splice.c | 128 ------------------------------------- include/linux/pagemap.h | 2 - include/linux/splice.h | 4 ++ mm/filemap.c | 138 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 136 insertions(+), 136 deletions(-) diff --git a/fs/splice.c b/fs/splice.c index 8b2a9d963bc4..341cd8fb47a8 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -375,134 +375,6 @@ static ssize_t direct_splice_read(struct file *in, loff_t *ppos, return ret; } -/* - * Splice subpages from a folio into a pipe. - */ -static size_t splice_folio_into_pipe(struct pipe_inode_info *pipe, - struct folio *folio, - loff_t fpos, size_t size) -{ - struct page *page; - size_t spliced = 0, offset = offset_in_folio(folio, fpos); - - page = folio_page(folio, offset / PAGE_SIZE); - size = min(size, folio_size(folio) - offset); - offset %= PAGE_SIZE; - - while (spliced < size && - !pipe_full(pipe->head, pipe->tail, pipe->max_usage)) { - struct pipe_buffer *buf = pipe_head_buf(pipe); - size_t part = min_t(size_t, PAGE_SIZE - offset, size - spliced); - - *buf = (struct pipe_buffer) { - .ops = &page_cache_pipe_buf_ops, - .page = page, - .offset = offset, - .len = part, - }; - folio_get(folio); - pipe->head++; - page++; - spliced += part; - offset = 0; - } - - return spliced; -} - -/* - * Splice folios from the pagecache of a buffered (ie. non-O_DIRECT) file into - * a pipe. - */ -static ssize_t filemap_splice_read(struct file *in, loff_t *ppos, - struct pipe_inode_info *pipe, - size_t len, unsigned int flags) -{ - struct folio_batch fbatch; - struct kiocb iocb; - size_t total_spliced = 0, used, npages; - loff_t isize, end_offset; - bool writably_mapped; - int i, error = 0; - - init_sync_kiocb(&iocb, in); - iocb.ki_pos = *ppos; - iocb.ki_flags &= IOCB_NOWAIT; - - /* Work out how much data we can actually add into the pipe */ - used = pipe_occupancy(pipe->head, pipe->tail); - npages = max_t(ssize_t, pipe->max_usage - used, 0); - len = min_t(size_t, len, npages * PAGE_SIZE); - - folio_batch_init(&fbatch); - - do { - cond_resched(); - - if (*ppos >= i_size_read(file_inode(in))) - break; - - iocb.ki_pos = *ppos; - error = filemap_get_pages(&iocb, len, &fbatch, true); - if (error < 0) - break; - - /* - * i_size must be checked after we know the pages are Uptodate. - * - * Checking i_size after the check allows us to calculate - * the correct value for "nr", which means the zero-filled - * part of the page is not copied back to userspace (unless - * another truncate extends the file - this is desired though). - */ - isize = i_size_read(file_inode(in)); - if (unlikely(*ppos >= isize)) - break; - end_offset = min_t(loff_t, isize, *ppos + len); - - /* - * Once we start copying data, we don't want to be touching any - * cachelines that might be contended: - */ - writably_mapped = mapping_writably_mapped(in->f_mapping); - - for (i = 0; i < folio_batch_count(&fbatch); i++) { - struct folio *folio = fbatch.folios[i]; - size_t n; - - if (folio_pos(folio) >= end_offset) - goto out; - folio_mark_accessed(folio); - - /* - * If users can be writing to this folio using arbitrary - * virtual addresses, take care of potential aliasing - * before reading the folio on the kernel side. - */ - if (writably_mapped) - flush_dcache_folio(folio); - - n = splice_folio_into_pipe(pipe, folio, *ppos, len); - if (!n) - goto out; - len -= n; - total_spliced += n; - *ppos += n; - in->f_ra.prev_pos = *ppos; - if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) - goto out; - } - - folio_batch_release(&fbatch); - } while (len); - -out: - folio_batch_release(&fbatch); - file_accessed(in); - - return total_spliced ? total_spliced : error; -} - /** * generic_file_splice_read - splice data from file to a pipe * @in: file to splice from diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 3a7bdb35acff..29e1f9e76eb6 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -748,8 +748,6 @@ struct page *read_cache_page(struct address_space *, pgoff_t index, filler_t *filler, struct file *file); extern struct page * read_cache_page_gfp(struct address_space *mapping, pgoff_t index, gfp_t gfp_mask); -int filemap_get_pages(struct kiocb *iocb, size_t count, - struct folio_batch *fbatch, bool need_uptodate); static inline struct page *read_mapping_page(struct address_space *mapping, pgoff_t index, struct file *file) diff --git a/include/linux/splice.h b/include/linux/splice.h index a55179fd60fc..691c44ef5c0b 100644 --- a/include/linux/splice.h +++ b/include/linux/splice.h @@ -67,6 +67,10 @@ typedef int (splice_actor)(struct pipe_inode_info *, struct pipe_buffer *, typedef int (splice_direct_actor)(struct pipe_inode_info *, struct splice_desc *); +ssize_t filemap_splice_read(struct file *in, loff_t *ppos, + struct pipe_inode_info *pipe, + size_t len, unsigned int flags); + extern ssize_t splice_from_pipe(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int, splice_actor *); diff --git a/mm/filemap.c b/mm/filemap.c index 6970be64a3e0..963602671fee 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -42,6 +42,8 @@ #include #include #include +#include +#include #include #include #include "internal.h" @@ -2576,12 +2578,8 @@ static int filemap_readahead(struct kiocb *iocb, struct file *file, return 0; } -/* - * Extract some folios from the pagecache of a file, reading those pages from - * the backing store if necessary and waiting for them. - */ -int filemap_get_pages(struct kiocb *iocb, size_t count, - struct folio_batch *fbatch, bool need_uptodate) +static int filemap_get_pages(struct kiocb *iocb, size_t count, + struct folio_batch *fbatch, bool need_uptodate) { struct file *filp = iocb->ki_filp; struct address_space *mapping = filp->f_mapping; @@ -2845,6 +2843,134 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) } EXPORT_SYMBOL(generic_file_read_iter); +/* + * Splice subpages from a folio into a pipe. + */ +static size_t splice_folio_into_pipe(struct pipe_inode_info *pipe, + struct folio *folio, + loff_t fpos, size_t size) +{ + struct page *page; + size_t spliced = 0, offset = offset_in_folio(folio, fpos); + + page = folio_page(folio, offset / PAGE_SIZE); + size = min(size, folio_size(folio) - offset); + offset %= PAGE_SIZE; + + while (spliced < size && + !pipe_full(pipe->head, pipe->tail, pipe->max_usage)) { + struct pipe_buffer *buf = pipe_head_buf(pipe); + size_t part = min_t(size_t, PAGE_SIZE - offset, size - spliced); + + *buf = (struct pipe_buffer) { + .ops = &page_cache_pipe_buf_ops, + .page = page, + .offset = offset, + .len = part, + }; + folio_get(folio); + pipe->head++; + page++; + spliced += part; + offset = 0; + } + + return spliced; +} + +/* + * Splice folios from the pagecache of a buffered (ie. non-O_DIRECT) file into + * a pipe. + */ +ssize_t filemap_splice_read(struct file *in, loff_t *ppos, + struct pipe_inode_info *pipe, + size_t len, unsigned int flags) +{ + struct folio_batch fbatch; + struct kiocb iocb; + size_t total_spliced = 0, used, npages; + loff_t isize, end_offset; + bool writably_mapped; + int i, error = 0; + + init_sync_kiocb(&iocb, in); + iocb.ki_pos = *ppos; + iocb.ki_flags &= IOCB_NOWAIT; + + /* Work out how much data we can actually add into the pipe */ + used = pipe_occupancy(pipe->head, pipe->tail); + npages = max_t(ssize_t, pipe->max_usage - used, 0); + len = min_t(size_t, len, npages * PAGE_SIZE); + + folio_batch_init(&fbatch); + + do { + cond_resched(); + + if (*ppos >= i_size_read(file_inode(in))) + break; + + iocb.ki_pos = *ppos; + error = filemap_get_pages(&iocb, len, &fbatch, true); + if (error < 0) + break; + + /* + * i_size must be checked after we know the pages are Uptodate. + * + * Checking i_size after the check allows us to calculate + * the correct value for "nr", which means the zero-filled + * part of the page is not copied back to userspace (unless + * another truncate extends the file - this is desired though). + */ + isize = i_size_read(file_inode(in)); + if (unlikely(*ppos >= isize)) + break; + end_offset = min_t(loff_t, isize, *ppos + len); + + /* + * Once we start copying data, we don't want to be touching any + * cachelines that might be contended: + */ + writably_mapped = mapping_writably_mapped(in->f_mapping); + + for (i = 0; i < folio_batch_count(&fbatch); i++) { + struct folio *folio = fbatch.folios[i]; + size_t n; + + if (folio_pos(folio) >= end_offset) + goto out; + folio_mark_accessed(folio); + + /* + * If users can be writing to this folio using arbitrary + * virtual addresses, take care of potential aliasing + * before reading the folio on the kernel side. + */ + if (writably_mapped) + flush_dcache_folio(folio); + + n = splice_folio_into_pipe(pipe, folio, *ppos, len); + if (!n) + goto out; + len -= n; + total_spliced += n; + *ppos += n; + in->f_ra.prev_pos = *ppos; + if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) + goto out; + } + + folio_batch_release(&fbatch); + } while (len); + +out: + folio_batch_release(&fbatch); + file_accessed(in); + + return total_spliced ? total_spliced : error; +} + static inline loff_t folio_seek_hole_data(struct xa_state *xas, struct address_space *mapping, struct folio *folio, loff_t start, loff_t end, bool seek_data)