mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Breno Leitao <leitao@debian.org>
To: Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	 Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	 x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	 Alexander Viro <viro@zeniv.linux.org.uk>,
	 Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
	 Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	 linux-block@vger.kernel.org, Breno Leitao <leitao@debian.org>,
	 kernel-team@meta.com
Subject: [PATCH RFC 2/3] iov_iter: add copy_bvecs_to_iter_full()
Date: Fri, 13 Nov 2026 03:51:45 -0800	[thread overview]
Message-ID: <20261113-pipe-pool-regression-smap-v1-2-b0dc69558ab3@debian.org> (raw)
In-Reply-To: <20261113-pipe-pool-regression-smap-v1-0-b0dc69558ab3@debian.org>

copy_page_to_iter() opens and closes a user access section for every
page it copies to a user buffer. On x86 that is a STAC/CLAC pair per
page, even when the caller holds a batch of pages, as anon_pipe_read()
does, that one section could cover.

Add copy_bvecs_to_iter_full(), which copies an array of pages to an
iov_iter like a copy_page_to_iter() loop, but all or nothing: after a
short copy the iterator is reverted, as copy_from_iter_full() does.

For a plain user buffer (ITER_UBUF) on architectures that provide
unsafe_copy_user_generic(), all the copies run inside one scoped user
write access. It keeps the usercopy fault injection and the
instrumentation that copy_to_user_iter() does for every copy. Every
other destination gets the copy_page_to_iter() loop.

Signed-off-by: Breno Leitao <leitao@debian.org>
---
 include/linux/uio.h |  2 ++
 lib/iov_iter.c      | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+)

diff --git a/include/linux/uio.h b/include/linux/uio.h
index 76e4103b9ec423..7ab25080b0edea 100644
--- a/include/linux/uio.h
+++ b/include/linux/uio.h
@@ -197,6 +197,8 @@ size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t bytes);
 size_t iov_iter_single_seg_count(const struct iov_iter *i);
 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
 			 struct iov_iter *i);
+bool copy_bvecs_to_iter_full(const struct bio_vec *bv, unsigned int nr,
+			     struct iov_iter *i);
 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
 			 struct iov_iter *i);
 size_t copy_folio_from_iter_atomic(struct folio *folio, size_t offset,
diff --git a/lib/iov_iter.c b/lib/iov_iter.c
index de7a48205e0117..2df0539228be84 100644
--- a/lib/iov_iter.c
+++ b/lib/iov_iter.c
@@ -387,6 +387,67 @@ size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
 }
 EXPORT_SYMBOL(copy_page_to_iter);
 
+#ifdef unsafe_copy_user_generic
+static bool copy_bvecs_to_ubuf_full(const struct bio_vec *bv, unsigned int nr,
+				    struct iov_iter *i)
+{
+	char __user *dst = i->ubuf + i->iov_offset;
+	size_t len = 0, off = 0;
+	unsigned int k;
+
+	for (k = 0; k < nr; k++)
+		len += bv[k].bv_len;
+	if (len > iov_iter_count(i) || should_fail_usercopy())
+		return false;
+
+	might_fault();
+	scoped_user_write_access_size(dst, len, efault) {
+		for (k = 0; k < nr; off += bv[k].bv_len, k++) {
+			void *src = page_address(bv[k].bv_page) +
+				    bv[k].bv_offset;
+
+			instrument_copy_to_user(dst + off, src, bv[k].bv_len);
+			if (unsafe_copy_user_generic((__force void *)dst + off,
+						     src, bv[k].bv_len))
+				goto efault;
+		}
+	}
+	iov_iter_advance(i, len);
+	return true;
+
+efault:
+	return false;
+}
+#endif
+
+/*
+ * Copy the pages in @bv to @i one after the other, like copy_page_to_iter(),
+ * but all or nothing: after a short copy @i is reverted. For a plain user
+ * buffer the copies share one user access section where the architecture
+ * allows it.
+ */
+bool copy_bvecs_to_iter_full(const struct bio_vec *bv, unsigned int nr,
+			     struct iov_iter *i)
+{
+	size_t copied = 0, n;
+	unsigned int k;
+
+#ifdef unsafe_copy_user_generic
+	if (iter_is_ubuf(i))
+		return copy_bvecs_to_ubuf_full(bv, nr, i);
+#endif
+	for (k = 0; k < nr; k++) {
+		n = copy_page_to_iter(bv[k].bv_page, bv[k].bv_offset,
+				      bv[k].bv_len, i);
+		copied += n;
+		if (n != bv[k].bv_len) {
+			iov_iter_revert(i, copied);
+			return false;
+		}
+	}
+	return true;
+}
+
 size_t copy_page_to_iter_nofault(struct page *page, unsigned offset, size_t bytes,
 				 struct iov_iter *i)
 {

-- 
2.53.1-Meta


  parent reply	other threads:[~2026-11-13 11:54 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13 11:51 [PATCH RFC 0/3] fs/pipe: copy a run of pipe buffers in one user access section Breno Leitao
2026-11-13 11:51 ` [PATCH RFC 1/3] x86/uaccess: Split the STAC/CLAC out of copy_user_generic() Breno Leitao
2026-11-13 11:51 ` Breno Leitao [this message]
2026-11-13 11:51 ` [PATCH RFC 3/3] fs/pipe: copy a run of anonymous buffers in one user access section Breno Leitao
2026-11-17  9:04   ` Chris Down
2026-11-17  9:04 ` [PATCH RFC 0/3] fs/pipe: copy a run of pipe " Chris Down

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261113-pipe-pool-regression-smap-v1-2-b0dc69558ab3@debian.org \
    --to=leitao@debian.org \
    --cc=akpm@linux-foundation.org \
    --cc=bp@alien8.de \
    --cc=brauner@kernel.org \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=jack@suse.cz \
    --cc=kernel-team@meta.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®