* [PATCH v5 1/4] iov_iter: Convert copy_from_user_iter() to masked user access
2025-11-17 16:43 [PATCH v5 0/4] uaccess: Prepare for masked user access on powerpc Christophe Leroy
@ 2025-11-17 16:43 ` Christophe Leroy
2025-11-17 16:43 ` [PATCH v5 2/4] iov_iter: Add missing speculation barrier to copy_from_user_iter() Christophe Leroy
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Christophe Leroy @ 2025-11-17 16:43 UTC (permalink / raw)
To: Thomas Gleixner, Peter Zijlstra
Cc: Christophe Leroy, Alexander Viro, Christian Brauner, Jan Kara,
Ingo Molnar, Darren Hart, Davidlohr Bueso, Andre Almeida,
Andrew Morton, Eric Dumazet, Kuniyuki Iwashima, Paolo Abeni,
Willem de Bruijn, David S. Miller, Jakub Kicinski, Simon Horman,
Daniel Borkmann, Dave Hansen, Madhavan Srinivasan,
Michael Ellerman, Nichlas Piggin, linux-block, linux-fsdevel,
linux-kernel, netdev, linuxppc-dev
Following patch will add missing barrier_nospec() to
copy_from_user_iter(). On some architecture like x86 it might
degrade performance, which would be unfortunate as
copy_from_user_iter() is a critical function.
Convert copy_from_user_iter() to using masked user access on
architecture that support it.
This is similar to what was done for copy_from_user() by
commit 0fc810ae3ae1 ("x86/uaccess: Avoid barrier_nospec()
in 64-bit copy_from_user()")
Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
---
v5: Changed commit message
v2: New in v2
---
lib/iov_iter.c | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/lib/iov_iter.c b/lib/iov_iter.c
index 2fe66a6b8789..a589935bf302 100644
--- a/lib/iov_iter.c
+++ b/lib/iov_iter.c
@@ -49,12 +49,16 @@ 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 res;
+
+ 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;
}
--
2.49.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v5 2/4] iov_iter: Add missing speculation barrier to copy_from_user_iter()
2025-11-17 16:43 [PATCH v5 0/4] uaccess: Prepare for masked user access on powerpc Christophe Leroy
2025-11-17 16:43 ` [PATCH v5 1/4] iov_iter: Convert copy_from_user_iter() to masked user access Christophe Leroy
@ 2025-11-17 16:43 ` Christophe Leroy
2025-11-17 16:43 ` [PATCH v5 3/4] scm: Convert put_cmsg() to scoped user access Christophe Leroy
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Christophe Leroy @ 2025-11-17 16:43 UTC (permalink / raw)
To: Thomas Gleixner, Peter Zijlstra
Cc: Christophe Leroy, Alexander Viro, Christian Brauner, Jan Kara,
Ingo Molnar, Darren Hart, Davidlohr Bueso, Andre Almeida,
Andrew Morton, Eric Dumazet, Kuniyuki Iwashima, Paolo Abeni,
Willem de Bruijn, David S. Miller, Jakub Kicinski, Simon Horman,
Daniel Borkmann, Dave Hansen, Madhavan Srinivasan,
Michael Ellerman, Nichlas Piggin, linux-block, linux-fsdevel,
linux-kernel, netdev, linuxppc-dev
The results of "access_ok()" can be mis-speculated. The result is that
you can end speculatively:
if (access_ok(from, size))
// Right here
For the same reason as done in copy_from_user() by
commit 74e19ef0ff80 ("uaccess: Add speculation barrier to
copy_from_user()"), add a speculation barrier to copy_from_user_iter().
Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
---
lib/iov_iter.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/lib/iov_iter.c b/lib/iov_iter.c
index a589935bf302..896760bad455 100644
--- a/lib/iov_iter.c
+++ b/lib/iov_iter.c
@@ -49,11 +49,19 @@ size_t copy_from_user_iter(void __user *iter_from, size_t progress,
if (should_fail_usercopy())
return len;
- if (can_do_masked_user_access())
+ if (can_do_masked_user_access()) {
iter_from = mask_user_address(iter_from);
- else if (!access_ok(iter_from, len))
- return res;
+ } else {
+ if (!access_ok(iter_from, len))
+ return res;
+ /*
+ * Ensure that bad access_ok() speculation will not
+ * lead to nasty side effects *after* the copy is
+ * finished:
+ */
+ barrier_nospec();
+ }
to += progress;
instrument_copy_from_user_before(to, iter_from, len);
res = raw_copy_from_user(to, iter_from, len);
--
2.49.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v5 3/4] scm: Convert put_cmsg() to scoped user access
2025-11-17 16:43 [PATCH v5 0/4] uaccess: Prepare for masked user access on powerpc Christophe Leroy
2025-11-17 16:43 ` [PATCH v5 1/4] iov_iter: Convert copy_from_user_iter() to masked user access Christophe Leroy
2025-11-17 16:43 ` [PATCH v5 2/4] iov_iter: Add missing speculation barrier to copy_from_user_iter() Christophe Leroy
@ 2025-11-17 16:43 ` Christophe Leroy
2025-11-17 16:43 ` [PATCH v5 4/4] lib/strn*,uaccess: Use masked_user_{read/write}_access_begin when required Christophe Leroy
2025-11-18 14:29 ` [PATCH v5 0/4] uaccess: Prepare for masked user access on powerpc Thomas Gleixner
4 siblings, 0 replies; 6+ messages in thread
From: Christophe Leroy @ 2025-11-17 16:43 UTC (permalink / raw)
To: Thomas Gleixner, Peter Zijlstra
Cc: Christophe Leroy, Alexander Viro, Christian Brauner, Jan Kara,
Ingo Molnar, Darren Hart, Davidlohr Bueso, Andre Almeida,
Andrew Morton, Eric Dumazet, Kuniyuki Iwashima, Paolo Abeni,
Willem de Bruijn, David S. Miller, Jakub Kicinski, Simon Horman,
Daniel Borkmann, Dave Hansen, Madhavan Srinivasan,
Michael Ellerman, Nichlas Piggin, linux-block, linux-fsdevel,
linux-kernel, netdev, linuxppc-dev
Replace the open coded implementation with the scoped user access
guards.
No functional change intended.
Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
---
v5: New
---
net/core/scm.c | 20 +++++++-------------
1 file changed, 7 insertions(+), 13 deletions(-)
diff --git a/net/core/scm.c b/net/core/scm.c
index 66eaee783e8b..cd87f66671aa 100644
--- a/net/core/scm.c
+++ b/net/core/scm.c
@@ -273,17 +273,13 @@ int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
check_object_size(data, cmlen - sizeof(*cm), true);
- if (can_do_masked_user_access())
- cm = masked_user_access_begin(cm);
- else if (!user_write_access_begin(cm, cmlen))
- goto efault;
-
- unsafe_put_user(cmlen, &cm->cmsg_len, efault_end);
- unsafe_put_user(level, &cm->cmsg_level, efault_end);
- unsafe_put_user(type, &cm->cmsg_type, efault_end);
- unsafe_copy_to_user(CMSG_USER_DATA(cm), data,
- cmlen - sizeof(*cm), efault_end);
- user_write_access_end();
+ scoped_user_write_access_size(cm, cmlen, efault) {
+ unsafe_put_user(cmlen, &cm->cmsg_len, efault);
+ unsafe_put_user(level, &cm->cmsg_level, efault);
+ unsafe_put_user(type, &cm->cmsg_type, efault);
+ unsafe_copy_to_user(CMSG_USER_DATA(cm), data,
+ cmlen - sizeof(*cm), efault);
+ }
} else {
struct cmsghdr *cm = msg->msg_control;
@@ -301,8 +297,6 @@ int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
msg->msg_controllen -= cmlen;
return 0;
-efault_end:
- user_write_access_end();
efault:
return -EFAULT;
}
--
2.49.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v5 4/4] lib/strn*,uaccess: Use masked_user_{read/write}_access_begin when required
2025-11-17 16:43 [PATCH v5 0/4] uaccess: Prepare for masked user access on powerpc Christophe Leroy
` (2 preceding siblings ...)
2025-11-17 16:43 ` [PATCH v5 3/4] scm: Convert put_cmsg() to scoped user access Christophe Leroy
@ 2025-11-17 16:43 ` Christophe Leroy
2025-11-18 14:29 ` [PATCH v5 0/4] uaccess: Prepare for masked user access on powerpc Thomas Gleixner
4 siblings, 0 replies; 6+ messages in thread
From: Christophe Leroy @ 2025-11-17 16:43 UTC (permalink / raw)
To: Thomas Gleixner, Peter Zijlstra
Cc: Christophe Leroy, Alexander Viro, Christian Brauner, Jan Kara,
Ingo Molnar, Darren Hart, Davidlohr Bueso, Andre Almeida,
Andrew Morton, Eric Dumazet, Kuniyuki Iwashima, Paolo Abeni,
Willem de Bruijn, David S. Miller, Jakub Kicinski, Simon Horman,
Daniel Borkmann, Dave Hansen, Madhavan Srinivasan,
Michael Ellerman, Nichlas Piggin, linux-block, linux-fsdevel,
linux-kernel, netdev, linuxppc-dev
Properly use masked_user_read_access_begin() and
masked_user_write_access_begin() instead of masked_user_access_begin()
in order to match user_read_access_end() and user_write_access_end().
This is important for architectures like powerpc that enable
separately user reads and user writes.
That means masked_user_read_access_begin() is used when user memory is
exclusively read during the window and masked_user_write_access_begin()
is used when user memory is exclusively writen during the window.
masked_user_access_begin() remains and is used when both reads and
writes are performed during the open window. Each of them is expected
to be terminated by the matching user_read_access_end(),
user_write_access_end() and user_access_end().
Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
---
v5:
- Removed net/core/scm.c which is converted to scope user access by previous patch
- Renamed the patch as it now only handles lib/strncpy_from_user.c and lib/strnlen_user.c
v4: Rebased on top of core-scoped-uaccess tag
v3: Rebased on top of v6.18-rc1 ==> change in net/core/scm.c
v2: Added more explanations in the commit message following comments received.
---
lib/strncpy_from_user.c | 2 +-
lib/strnlen_user.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/lib/strncpy_from_user.c b/lib/strncpy_from_user.c
index 6dc234913dd5..5bb752ff7c61 100644
--- a/lib/strncpy_from_user.c
+++ b/lib/strncpy_from_user.c
@@ -126,7 +126,7 @@ long strncpy_from_user(char *dst, const char __user *src, long count)
if (can_do_masked_user_access()) {
long retval;
- src = masked_user_access_begin(src);
+ src = masked_user_read_access_begin(src);
retval = do_strncpy_from_user(dst, src, count, count);
user_read_access_end();
return retval;
diff --git a/lib/strnlen_user.c b/lib/strnlen_user.c
index 6e489f9e90f1..4a6574b67f82 100644
--- a/lib/strnlen_user.c
+++ b/lib/strnlen_user.c
@@ -99,7 +99,7 @@ long strnlen_user(const char __user *str, long count)
if (can_do_masked_user_access()) {
long retval;
- str = masked_user_access_begin(str);
+ str = masked_user_read_access_begin(str);
retval = do_strnlen_user(str, count, count);
user_read_access_end();
return retval;
--
2.49.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH v5 0/4] uaccess: Prepare for masked user access on powerpc
2025-11-17 16:43 [PATCH v5 0/4] uaccess: Prepare for masked user access on powerpc Christophe Leroy
` (3 preceding siblings ...)
2025-11-17 16:43 ` [PATCH v5 4/4] lib/strn*,uaccess: Use masked_user_{read/write}_access_begin when required Christophe Leroy
@ 2025-11-18 14:29 ` Thomas Gleixner
4 siblings, 0 replies; 6+ messages in thread
From: Thomas Gleixner @ 2025-11-18 14:29 UTC (permalink / raw)
To: Christophe Leroy, Peter Zijlstra
Cc: Christophe Leroy, Alexander Viro, Christian Brauner, Jan Kara,
Ingo Molnar, Darren Hart, Davidlohr Bueso, Andre Almeida,
Andrew Morton, Eric Dumazet, Kuniyuki Iwashima, Paolo Abeni,
Willem de Bruijn, David S. Miller, Jakub Kicinski, Simon Horman,
Daniel Borkmann, Dave Hansen, Madhavan Srinivasan,
Michael Ellerman, Nichlas Piggin, linux-block, linux-fsdevel,
linux-kernel, netdev, linuxppc-dev
On Mon, Nov 17 2025 at 17:43, Christophe Leroy wrote:
> This is v5 of the series "powerpc: Implement masked user access". This
> version only includes the preparatory patches to enable merging of
> powerpc architecture patches that depend on them on next cycle.
>
> It applies on top of commit 6ec821f050e2 (tag: core-scoped-uaccess)
> from tip tree.
>
> Thomas, Peter, could you please take those preparatory patches
> in tip tree for v6.19, then Maddy will take powerpc patches
> into powerpc-next for v6.20.
I've applied them to tip core/uaccess, which contains only the uaccess
related bits. That branch is immutable and could be consumed by PPC if
required.
Thanks,
tglx
^ permalink raw reply [flat|nested] 6+ messages in thread