[PATCH] uaccess: decouple INLINE_COPY_FROM_USER and CONFIG_RUST

Yury Norov (NVIDIA) posted 1 patch 3 months, 2 weeks ago
There is a newer version of this series
lib/usercopy.c         |  4 ++--
rust/helpers/uaccess.c | 12 ++++++++++++
2 files changed, 14 insertions(+), 2 deletions(-)
[PATCH] uaccess: decouple INLINE_COPY_FROM_USER and CONFIG_RUST
Posted by Yury Norov (NVIDIA) 3 months, 2 weeks ago
Commit 1f9a8286bc0c ("uaccess: always export _copy_[from|to]_user with
CONFIG_RUST") exports _copy_{from,to}_user() unconditionally, if RUST
is enabled. This pollutes exported symbols namespace, and spreads RUST
ifdefery in core files.

It's better to declare a corresponding helper under the rust/helpers,
similarly to how non-underscored copy_{from,to}_user() is handled.

Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
---
 lib/usercopy.c         |  4 ++--
 rust/helpers/uaccess.c | 12 ++++++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/lib/usercopy.c b/lib/usercopy.c
index 7b17b83c8042..b00a3a957de6 100644
--- a/lib/usercopy.c
+++ b/lib/usercopy.c
@@ -12,7 +12,7 @@
 
 /* out-of-line parts */
 
-#if !defined(INLINE_COPY_FROM_USER) || defined(CONFIG_RUST)
+#if !defined(INLINE_COPY_FROM_USER)
 unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n)
 {
 	return _inline_copy_from_user(to, from, n);
@@ -20,7 +20,7 @@ unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n
 EXPORT_SYMBOL(_copy_from_user);
 #endif
 
-#if !defined(INLINE_COPY_TO_USER) || defined(CONFIG_RUST)
+#if !defined(INLINE_COPY_TO_USER)
 unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n)
 {
 	return _inline_copy_to_user(to, from, n);
diff --git a/rust/helpers/uaccess.c b/rust/helpers/uaccess.c
index f49076f813cd..4629b2d15529 100644
--- a/rust/helpers/uaccess.c
+++ b/rust/helpers/uaccess.c
@@ -13,3 +13,15 @@ unsigned long rust_helper_copy_to_user(void __user *to, const void *from,
 {
 	return copy_to_user(to, from, n);
 }
+
+#ifdef INLINE_COPY_FROM_USER
+unsigned long rust_helper__copy_from_user(void *to, const void __user *from, unsigned long n)
+{
+	return _inline_copy_from_user(to, from, n);
+}
+
+unsigned long rust_helper__copy_to_user(void __user *to, const void *from, unsigned long n)
+{
+	return _inline_copy_to_user(to, from, n);
+}
+#endif
-- 
2.43.0
Re: [PATCH] uaccess: decouple INLINE_COPY_FROM_USER and CONFIG_RUST
Posted by Alice Ryhl 3 months, 2 weeks ago
On Thu, Oct 23, 2025 at 01:16:06PM -0400, Yury Norov (NVIDIA) wrote:
> Commit 1f9a8286bc0c ("uaccess: always export _copy_[from|to]_user with
> CONFIG_RUST") exports _copy_{from,to}_user() unconditionally, if RUST
> is enabled. This pollutes exported symbols namespace, and spreads RUST
> ifdefery in core files.
> 
> It's better to declare a corresponding helper under the rust/helpers,
> similarly to how non-underscored copy_{from,to}_user() is handled.
> 
> Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>

Overall LGTM:
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Tested-by: Alice Ryhl <aliceryhl@google.com>


In include/linux/uaccess.h there is the comment:

	Rust code always uses the extern definition.

I think we should reword this comment as part of this change. You can
say that Rust accesses _inline_copy_from_user() through a helper, or
similar.

After all, when using LTO or other mechanism to inline helpers, it is no
longer the case that Rust always uses the a function call.

Alice
Re: [PATCH] uaccess: decouple INLINE_COPY_FROM_USER and CONFIG_RUST
Posted by Yury Norov 3 months, 2 weeks ago
On Fri, Oct 24, 2025 at 09:12:56AM +0000, Alice Ryhl wrote:
> On Thu, Oct 23, 2025 at 01:16:06PM -0400, Yury Norov (NVIDIA) wrote:
> > Commit 1f9a8286bc0c ("uaccess: always export _copy_[from|to]_user with
> > CONFIG_RUST") exports _copy_{from,to}_user() unconditionally, if RUST
> > is enabled. This pollutes exported symbols namespace, and spreads RUST
> > ifdefery in core files.
> > 
> > It's better to declare a corresponding helper under the rust/helpers,
> > similarly to how non-underscored copy_{from,to}_user() is handled.
> > 
> > Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
> 
> Overall LGTM:
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Tested-by: Alice Ryhl <aliceryhl@google.com>
> 
> 
> In include/linux/uaccess.h there is the comment:
> 
> 	Rust code always uses the extern definition.
> 
> I think we should reword this comment as part of this change. You can
> say that Rust accesses _inline_copy_from_user() through a helper, or
> similar.
> 
> After all, when using LTO or other mechanism to inline helpers, it is no
> longer the case that Rust always uses the a function call.

It is actually worth to just drop the rust part of the comment - with
this patch there's nothing special in managing the _copy_from_user().

I'll send v2. Thanks for review!

Thanks,
Yury
Re: [PATCH] uaccess: decouple INLINE_COPY_FROM_USER and CONFIG_RUST
Posted by Alice Ryhl 3 months, 2 weeks ago
On Fri, Oct 24, 2025 at 3:45 PM Yury Norov <yury.norov@gmail.com> wrote:
>
> On Fri, Oct 24, 2025 at 09:12:56AM +0000, Alice Ryhl wrote:
> > On Thu, Oct 23, 2025 at 01:16:06PM -0400, Yury Norov (NVIDIA) wrote:
> > > Commit 1f9a8286bc0c ("uaccess: always export _copy_[from|to]_user with
> > > CONFIG_RUST") exports _copy_{from,to}_user() unconditionally, if RUST
> > > is enabled. This pollutes exported symbols namespace, and spreads RUST
> > > ifdefery in core files.
> > >
> > > It's better to declare a corresponding helper under the rust/helpers,
> > > similarly to how non-underscored copy_{from,to}_user() is handled.
> > >
> > > Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
> >
> > Overall LGTM:
> > Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> > Tested-by: Alice Ryhl <aliceryhl@google.com>
> >
> >
> > In include/linux/uaccess.h there is the comment:
> >
> >       Rust code always uses the extern definition.
> >
> > I think we should reword this comment as part of this change. You can
> > say that Rust accesses _inline_copy_from_user() through a helper, or
> > similar.
> >
> > After all, when using LTO or other mechanism to inline helpers, it is no
> > longer the case that Rust always uses the a function call.
>
> It is actually worth to just drop the rust part of the comment - with
> this patch there's nothing special in managing the _copy_from_user().

Yeah, makes sense.

Alice