[RFC PATCH v1 9/9] uaccess: Convert small fixed size copy_{to/from}_user() to scoped user access

Christophe Leroy (CS GROUP)
2026-04-27T17:19:29Z

copy_{to/from}_user() is a heavy function optimised for copy of large
blocs of memory between user and kernel space.

When the number of bytes to be copied is known at build time and small,
using scoped user access removes the burden of that optimisation.

Signed-off-by: Christophe Leroy (CS GROUP) <chleroy@kernel.org>
---
include/linux/uaccess.h | 47 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 47 insertions(+)

diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
index 33b7d0f5f808..3ac544527af2 100644
--- a/include/linux/uaccess.h
+++ b/include/linux/uaccess.h
@@ -50,6 +50,8 @@
#define mask_user_address(src) (src)
#endif

+#define SMALL_COPY_USER 64
+
/*
* Architectures should provide two primitives (raw_copy_{to,from}_user())
* and get rid of their private instances of copy_{to,from}_user() and
@@ -191,6 +193,9 @@ _inline_copy_from_user(void *to, const void __user *from, unsigned long n)
return res;
}

+static __always_inline __must_check unsigned long
+_small_copy_from_user(void *to, const void __user *from, unsigned long n);
+
extern __must_check unsigned long
_copy_from_user(void *, const void __user *, unsigned long);

@@ -207,6 +212,9 @@ _inline_copy_to_user(void __user *to, const void *from, unsigned long n)
return n;
}

+static __always_inline __must_check unsigned long
+_small_copy_to_user(void __user *to, const void *from, unsigned long n);
+
extern __must_check unsigned long
_copy_to_user(void __user *, const void *, unsigned long);

@@ -215,6 +223,8 @@ copy_from_user_common(void *to, const void __user *from, unsigned long n, bool p
{
if (!check_copy_size(to, n, false))
return n;
+ if (!partial && __builtin_constant_p(n) && n <= SMALL_COPY_USER)
+ return _small_copy_from_user(to, from, n);
if (IS_ENABLED(ARCH_WANTS_NOINLINE_COPY_USER))
return _copy_from_user(to, from, n);
else
@@ -239,6 +249,8 @@ copy_to_user_common(void __user *to, const void *from, unsigned long n, bool par
if (!check_copy_size(from, n, true))
return n;

+ if (!partial && __builtin_constant_p(n) && n <= SMALL_COPY_USER)
+ return _small_copy_to_user(to, from, n);
if (IS_ENABLED(ARCH_WANTS_NOINLINE_COPY_USER))
return _copy_to_user(to, from, n);
else
@@ -838,6 +850,41 @@ for (bool done = false; !done; done = true) \
#define scoped_user_rw_access(uptr, elbl) \
scoped_user_rw_access_size(uptr, sizeof(*(uptr)), elbl)

+static __always_inline __must_check unsigned long
+_small_copy_from_user(void *to, const void __user *from, unsigned long n)
+{
+ might_fault();
+ instrument_copy_from_user_before(to, from, n);
+ scoped_user_read_access_size(from, n, failed) {
+ /*
+ * Ensure that bad access_ok() speculation will not lead
+ * to nasty side effects *after* the copy is finished:
+ */
+ if (!can_do_masked_user_access())
+ barrier_nospec();
+ unsafe_copy_from_user(to, from, n, failed);
+ }
+ instrument_copy_from_user_after(to, from, n, 0);
+ return 0;
+failed:
+ instrument_copy_from_user_after(to, from, n, n);
+ return n;
+}
+
+static __always_inline __must_check unsigned long
+_small_copy_to_user(void __user *to, const void *from, unsigned long n)
+{
+ might_fault();
+ if (should_fail_usercopy())
+ return n;
+ instrument_copy_to_user(to, from, n);
+ scoped_user_write_access_size(to, n, failed)
+ unsafe_copy_to_user(to, from, n, failed);
+ return 0;
+failed:
+ return n;
+}
+
/**
* get_user_inline - Read user data inlined
* @val: The variable to store the value read from user memory
--
2.49.0