From: David Laight <david.laight.linux@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: David Laight <david.laight.linux@gmail.com>,
Linus Torvalds <torvalds@linux-foundation.org>,
Jens Axboe <axboe@kernel.dk>, David Howells <dhowells@redhat.com>,
Matthew Wilcox <willy@infradead.org>,
Andrew Morton <akpm@linux-foundation.org>,
Alexander Viro <viro@zeniv.linux.org.uk>
Subject: [PATCH next 2/3] iov: Use masked user accesses
Date: Fri, 21 Mar 2025 22:45:56 +0000 [thread overview]
Message-ID: <20250321224557.3847-3-david.laight.linux@gmail.com> (raw)
In-Reply-To: <20250321224557.3847-1-david.laight.linux@gmail.com>
Check can_do_masked_user_access() and use mask_user_address() or
masked_user_access_begin() to optimise user access checks.
Read iov->buf before iov->len to ensure accessing the 'masked'
address fails when kernel addresses are converted to ~0ul.
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
lib/iov_iter.c | 44 ++++++++++++++++++++++++++++----------------
1 file changed, 28 insertions(+), 16 deletions(-)
diff --git a/lib/iov_iter.c b/lib/iov_iter.c
index 623ec43e049a..796ef647bff2 100644
--- a/lib/iov_iter.c
+++ b/lib/iov_iter.c
@@ -19,12 +19,15 @@ size_t copy_to_user_iter(void __user *iter_to, size_t progress,
{
if (should_fail_usercopy())
return len;
- if (access_ok(iter_to, len)) {
- from += progress;
- instrument_copy_to_user(iter_to, from, len);
- len = raw_copy_to_user(iter_to, from, len);
- }
- return len;
+
+ if (can_do_masked_user_access())
+ iter_to = mask_user_address(iter_to);
+ else if (!access_ok(iter_to, len))
+ return len;
+
+ from += progress;
+ instrument_copy_to_user(iter_to, from, len);
+ return raw_copy_to_user(iter_to, from, len);
}
static __always_inline
@@ -49,12 +52,17 @@ size_t copy_from_user_iter(void __user *iter_from, size_t progress,
if (should_fail_usercopy())
return len;
- if (access_ok(iter_from, len)) {
- to += progress;
- instrument_copy_from_user_before(to, iter_from, len);
- res = raw_copy_from_user(to, iter_from, len);
- instrument_copy_from_user_after(to, iter_from, len, res);
- }
+
+ if (can_do_masked_user_access())
+ iter_from = mask_user_address(iter_from);
+ else if (!access_ok(iter_from, len))
+ return len;
+
+ to += progress;
+ instrument_copy_from_user_before(to, iter_from, len);
+ res = raw_copy_from_user(to, iter_from, len);
+ instrument_copy_from_user_after(to, iter_from, len, res);
+
return res;
}
@@ -1326,15 +1334,17 @@ static __noclone int copy_compat_iovec_from_user(struct iovec *iov,
int ret = -EFAULT;
u32 i;
- if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
+ if (can_do_masked_user_access())
+ uiov = masked_user_access_begin(uiov);
+ else if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
return -EFAULT;
for (i = 0; i < nr_segs; i++) {
compat_uptr_t buf;
compat_ssize_t len;
- unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end);
+ unsafe_get_user(len, &uiov[i].iov_len, uaccess_end);
/* check for compat_size_t not fitting in compat_ssize_t .. */
if (len < 0) {
@@ -1356,15 +1366,17 @@ static __noclone int copy_iovec_from_user(struct iovec *iov,
{
int ret = -EFAULT;
- if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
+ if (can_do_masked_user_access())
+ uiov = masked_user_access_begin(uiov);
+ else if (!user_access_begin(uiov, nr_segs * sizeof(*uiov)))
return -EFAULT;
do {
void __user *buf;
ssize_t len;
- unsafe_get_user(len, &uiov->iov_len, uaccess_end);
unsafe_get_user(buf, &uiov->iov_base, uaccess_end);
+ unsafe_get_user(len, &uiov->iov_len, uaccess_end);
/* check for size_t not fitting in ssize_t .. */
if (unlikely(len < 0)) {
--
2.39.5
next prev parent reply other threads:[~2025-03-21 22:46 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-21 22:45 [PATCH next 0/3] iov: Optimise user copies David Laight
2025-03-21 22:45 ` [PATCH next 1/3] iov: Remove access_ok() from import_iovec() David Laight
2025-03-21 22:45 ` David Laight [this message]
2025-03-21 22:45 ` [PATCH next 3/3] iov: Optimise __import_iovec_ubuf() David Laight
2025-03-21 23:35 ` [PATCH next 0/3] iov: Optimise user copies Linus Torvalds
2025-03-22 10:08 ` David Laight
2025-03-22 22:37 ` David Laight
2025-03-29 11:31 ` David Laight
2025-03-22 14:36 ` Jens Axboe
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=20250321224557.3847-3-david.laight.linux@gmail.com \
--to=david.laight.linux@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=axboe@kernel.dk \
--cc=dhowells@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=viro@zeniv.linux.org.uk \
--cc=willy@infradead.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.