public inbox for linux-fsdevel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] uaccess: Fix build of scoped user access with const pointer
@ 2026-03-01 19:33 Christophe Leroy (CS GROUP)
  2026-03-01 20:01 ` Linus Torvalds
  2026-03-01 21:52 ` David Laight
  0 siblings, 2 replies; 5+ messages in thread
From: Christophe Leroy (CS GROUP) @ 2026-03-01 19:33 UTC (permalink / raw)
  To: Thomas Gleixner, LKML
  Cc: Christophe Leroy (CS GROUP), Christophe Leroy, Mathieu Desnoyers,
	Andrew Cooper, Linus Torvalds, David Laight, kernel test robot,
	Russell King, linux-arm-kernel, x86, Madhavan Srinivasan,
	Michael Ellerman, Nicholas Piggin, linuxppc-dev, Paul Walmsley,
	Palmer Dabbelt, linux-riscv, Heiko Carstens,
	Christian Borntraeger, Sven Schnelle, linux-s390, Julia Lawall,
	Nicolas Palix, Peter Zijlstra, Darren Hart, Davidlohr Bueso,
	Andre Almeida, Alexander Viro, Christian Brauner, Jan Kara,
	linux-fsdevel

After converting powerpc checksum wrappers to scoped user access,
following build failure happens:

	  CC      arch/powerpc/lib/checksum_wrappers.o
	In file included from arch/powerpc/lib/checksum_wrappers.c:12:
	arch/powerpc/lib/checksum_wrappers.c: In function 'csum_and_copy_from_user':
	./include/linux/uaccess.h:691:1: error: initialization discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]
	  691 | ({                                                                      \
	      | ^
	./include/linux/uaccess.h:755:37: note: in expansion of macro '__scoped_user_access_begin'
	  755 |         for (void __user *_tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl); \
	      |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~
	./include/linux/uaccess.h:770:9: note: in expansion of macro '__scoped_user_access'
	  770 |         __scoped_user_access(read, usrc, size, elbl)
	      |         ^~~~~~~~~~~~~~~~~~~~
	arch/powerpc/lib/checksum_wrappers.c:17:9: note: in expansion of macro 'scoped_user_read_access_size'
	   17 |         scoped_user_read_access_size(src, len, efault)
	      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

Cast __scoped_user_access_begin() to (void __user *) to fix it.

Fixes: e497310b4ffb ("uaccess: Provide scoped user access regions")
Signed-off-by: Christophe Leroy (CS GROUP) <chleroy@kernel.org>
---
Thomas, I encountered this problem while preparing some patches to start using
scope user access widely on powerpc in order to benefit more from masked user
access. Can you make this patch go into 7.0 as a fix in order avoid dependency
on this change when we start using scoped user access ?

 include/linux/uaccess.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
index 1f3804245c06..5d9f6d45d301 100644
--- a/include/linux/uaccess.h
+++ b/include/linux/uaccess.h
@@ -752,7 +752,8 @@ USER_ACCESS_GUARD(rw)
  */
 #define __scoped_user_access(mode, uptr, size, elbl)					\
 for (bool done = false; !done; done = true)						\
-	for (void __user *_tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl); \
+	for (void __user *_tmpptr = (void __user *)					\
+				    __scoped_user_access_begin(mode, uptr, size, elbl); \
 	     !done; done = true)							\
 		for (CLASS(user_##mode##_access, scope)(_tmpptr); !done; done = true)	\
 			/* Force modified pointer usage within the scope */		\
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] uaccess: Fix build of scoped user access with const pointer
  2026-03-01 19:33 [PATCH] uaccess: Fix build of scoped user access with const pointer Christophe Leroy (CS GROUP)
@ 2026-03-01 20:01 ` Linus Torvalds
  2026-03-01 21:59   ` David Laight
  2026-03-01 22:16   ` David Laight
  2026-03-01 21:52 ` David Laight
  1 sibling, 2 replies; 5+ messages in thread
From: Linus Torvalds @ 2026-03-01 20:01 UTC (permalink / raw)
  To: Christophe Leroy (CS GROUP)
  Cc: Thomas Gleixner, LKML, Christophe Leroy, Mathieu Desnoyers,
	Andrew Cooper, David Laight, kernel test robot, Russell King,
	linux-arm-kernel, x86, Madhavan Srinivasan, Michael Ellerman,
	Nicholas Piggin, linuxppc-dev, Paul Walmsley, Palmer Dabbelt,
	linux-riscv, Heiko Carstens, Christian Borntraeger, Sven Schnelle,
	linux-s390, Julia Lawall, Nicolas Palix, Peter Zijlstra,
	Darren Hart, Davidlohr Bueso, Andre Almeida, Alexander Viro,
	Christian Brauner, Jan Kara, linux-fsdevel

On Sun, 1 Mar 2026 at 11:34, Christophe Leroy (CS GROUP)
<chleroy@kernel.org> wrote:
>
> -       for (void __user *_tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl); \
> +       for (void __user *_tmpptr = (void __user *)                                     \
> +                                   __scoped_user_access_begin(mode, uptr, size, elbl); \

Why are you casting this return value? Wouldn't it be a lot better to
just make the types be the CORRECT ones?

I didn't test this, so maybe I'm missing something, but why isn't that
just doing

        for (auto _tmpptr = __scoped_user_access_begin(mode, uptr,
size, elbl);         \

instead? No cast, just a "use the right type automatically".

That macro actually does something similar just a few lines later, in
that the innermost loop uses

         for (const typeof(uptr) uptr = _tmpptr; !done; done = true)

which picks up the type automatically from the argument (and then it
uses the argument both for the type and name, which is horrendously
confusing, but that's a separate thing).

Does that simple "auto" approach break something else?

                   Linus

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] uaccess: Fix build of scoped user access with const pointer
  2026-03-01 19:33 [PATCH] uaccess: Fix build of scoped user access with const pointer Christophe Leroy (CS GROUP)
  2026-03-01 20:01 ` Linus Torvalds
@ 2026-03-01 21:52 ` David Laight
  1 sibling, 0 replies; 5+ messages in thread
From: David Laight @ 2026-03-01 21:52 UTC (permalink / raw)
  To: Christophe Leroy (CS GROUP)
  Cc: Thomas Gleixner, LKML, Christophe Leroy, Mathieu Desnoyers,
	Andrew Cooper, Linus Torvalds, kernel test robot, Russell King,
	linux-arm-kernel, x86, Madhavan Srinivasan, Michael Ellerman,
	Nicholas Piggin, linuxppc-dev, Paul Walmsley, Palmer Dabbelt,
	linux-riscv, Heiko Carstens, Christian Borntraeger, Sven Schnelle,
	linux-s390, Julia Lawall, Nicolas Palix, Peter Zijlstra,
	Darren Hart, Davidlohr Bueso, Andre Almeida, Alexander Viro,
	Christian Brauner, Jan Kara, linux-fsdevel

On Sun,  1 Mar 2026 20:33:58 +0100
"Christophe Leroy (CS GROUP)" <chleroy@kernel.org> wrote:

> After converting powerpc checksum wrappers to scoped user access,
> following build failure happens:
> 
> 	  CC      arch/powerpc/lib/checksum_wrappers.o
> 	In file included from arch/powerpc/lib/checksum_wrappers.c:12:
> 	arch/powerpc/lib/checksum_wrappers.c: In function 'csum_and_copy_from_user':
> 	./include/linux/uaccess.h:691:1: error: initialization discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]
> 	  691 | ({                                                                      \
> 	      | ^
> 	./include/linux/uaccess.h:755:37: note: in expansion of macro '__scoped_user_access_begin'
> 	  755 |         for (void __user *_tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl); \
> 	      |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~
> 	./include/linux/uaccess.h:770:9: note: in expansion of macro '__scoped_user_access'
> 	  770 |         __scoped_user_access(read, usrc, size, elbl)
> 	      |         ^~~~~~~~~~~~~~~~~~~~
> 	arch/powerpc/lib/checksum_wrappers.c:17:9: note: in expansion of macro 'scoped_user_read_access_size'
> 	   17 |         scoped_user_read_access_size(src, len, efault)
> 	      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> Cast __scoped_user_access_begin() to (void __user *) to fix it.

I posted a patch to fix this in december, I'll find it and resend it.

	David

> 
> Fixes: e497310b4ffb ("uaccess: Provide scoped user access regions")
> Signed-off-by: Christophe Leroy (CS GROUP) <chleroy@kernel.org>
> ---
> Thomas, I encountered this problem while preparing some patches to start using
> scope user access widely on powerpc in order to benefit more from masked user
> access. Can you make this patch go into 7.0 as a fix in order avoid dependency
> on this change when we start using scoped user access ?
> 
>  include/linux/uaccess.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
> index 1f3804245c06..5d9f6d45d301 100644
> --- a/include/linux/uaccess.h
> +++ b/include/linux/uaccess.h
> @@ -752,7 +752,8 @@ USER_ACCESS_GUARD(rw)
>   */
>  #define __scoped_user_access(mode, uptr, size, elbl)					\
>  for (bool done = false; !done; done = true)						\
> -	for (void __user *_tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl); \
> +	for (void __user *_tmpptr = (void __user *)					\
> +				    __scoped_user_access_begin(mode, uptr, size, elbl); \
>  	     !done; done = true)							\
>  		for (CLASS(user_##mode##_access, scope)(_tmpptr); !done; done = true)	\
>  			/* Force modified pointer usage within the scope */		\


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] uaccess: Fix build of scoped user access with const pointer
  2026-03-01 20:01 ` Linus Torvalds
@ 2026-03-01 21:59   ` David Laight
  2026-03-01 22:16   ` David Laight
  1 sibling, 0 replies; 5+ messages in thread
From: David Laight @ 2026-03-01 21:59 UTC (permalink / raw)
  To: Linus Torvalds
  Cc: Christophe Leroy (CS GROUP), Thomas Gleixner, LKML,
	Christophe Leroy, Mathieu Desnoyers, Andrew Cooper,
	kernel test robot, Russell King, linux-arm-kernel, x86,
	Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin,
	linuxppc-dev, Paul Walmsley, Palmer Dabbelt, linux-riscv,
	Heiko Carstens, Christian Borntraeger, Sven Schnelle, linux-s390,
	Julia Lawall, Nicolas Palix, Peter Zijlstra, Darren Hart,
	Davidlohr Bueso, Andre Almeida, Alexander Viro, Christian Brauner,
	Jan Kara, linux-fsdevel

On Sun, 1 Mar 2026 12:01:08 -0800
Linus Torvalds <torvalds@linux-foundation.org> wrote:

> On Sun, 1 Mar 2026 at 11:34, Christophe Leroy (CS GROUP)
> <chleroy@kernel.org> wrote:
> >
> > -       for (void __user *_tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl); \
> > +       for (void __user *_tmpptr = (void __user *)                                     \
> > +                                   __scoped_user_access_begin(mode, uptr, size, elbl); \  
> 
> Why are you casting this return value? Wouldn't it be a lot better to
> just make the types be the CORRECT ones?
> 
> I didn't test this, so maybe I'm missing something, but why isn't that
> just doing
> 
>         for (auto _tmpptr = __scoped_user_access_begin(mode, uptr,
> size, elbl);         \
> 
> instead? No cast, just a "use the right type automatically".
> 
> That macro actually does something similar just a few lines later, in
> that the innermost loop uses
> 
>          for (const typeof(uptr) uptr = _tmpptr; !done; done = true)
> 
> which picks up the type automatically from the argument (and then it
> uses the argument both for the type and name, which is horrendously
> confusing, but that's a separate thing).
> 
> Does that simple "auto" approach break something else?

This is what I needed to do:
(Note that is pre-dates 'auto', but it should work.)
Send at 21:56 on dec 20.

If a 'const struct foo __user *ptr' is used for the address passed
to scoped_user_read_access() then you get a warning/error
uaccess.h:691:1: error: initialization discards 'const' qualifier
    from pointer target type [-Werror=discarded-qualifiers]
for the
    void __user *_tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl)
assignment.

Fix by using typeof(uptr) in that assignment and changing the 'read' functions
to use 'const void __user *ptr' rather than 'void __user *ptr'.

Fixes: e497310b4ffb "(uaccess: Provide scoped user access regions)"
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
 include/linux/uaccess.h | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
index 1f3804245c06..c5d5f2d395bc 100644
--- a/include/linux/uaccess.h
+++ b/include/linux/uaccess.h
@@ -650,32 +650,32 @@ static inline void user_access_restore(unsigned long flags) { }
 #define user_rw_access_end()		user_access_end()
 
 /* Scoped user access */
-#define USER_ACCESS_GUARD(_mode)				\
-static __always_inline void __user *				\
-class_user_##_mode##_begin(void __user *ptr)			\
+#define USER_ACCESS_GUARD(_mode, type)				\
+static __always_inline type __user *				\
+class_user_##_mode##_begin(type __user *ptr)			\
 {								\
 	return ptr;						\
 }								\
 								\
 static __always_inline void					\
-class_user_##_mode##_end(void __user *ptr)			\
+class_user_##_mode##_end(type __user *ptr)			\
 {								\
 	user_##_mode##_access_end();				\
 }								\
 								\
-DEFINE_CLASS(user_ ##_mode## _access, void __user *,		\
+DEFINE_CLASS(user_ ##_mode## _access, type __user *,		\
 	     class_user_##_mode##_end(_T),			\
-	     class_user_##_mode##_begin(ptr), void __user *ptr)	\
+	     class_user_##_mode##_begin(ptr), type __user *ptr)	\
 								\
 static __always_inline class_user_##_mode##_access_t		\
-class_user_##_mode##_access_ptr(void __user *scope)		\
+class_user_##_mode##_access_ptr(type __user *scope)		\
 {								\
 	return scope;						\
 }
 
-USER_ACCESS_GUARD(read)
-USER_ACCESS_GUARD(write)
-USER_ACCESS_GUARD(rw)
+USER_ACCESS_GUARD(read, const void)
+USER_ACCESS_GUARD(write, void)
+USER_ACCESS_GUARD(rw, void)
 #undef USER_ACCESS_GUARD
 
 /**
@@ -752,7 +752,7 @@ USER_ACCESS_GUARD(rw)
  */
 #define __scoped_user_access(mode, uptr, size, elbl)					\
 for (bool done = false; !done; done = true)						\
-	for (void __user *_tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl); \
+	for (typeof(uptr) _tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl); \
 	     !done; done = true)							\
 		for (CLASS(user_##mode##_access, scope)(_tmpptr); !done; done = true)	\
 			/* Force modified pointer usage within the scope */		\
-- 
2.39.5

	David

> 
>                    Linus


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] uaccess: Fix build of scoped user access with const pointer
  2026-03-01 20:01 ` Linus Torvalds
  2026-03-01 21:59   ` David Laight
@ 2026-03-01 22:16   ` David Laight
  1 sibling, 0 replies; 5+ messages in thread
From: David Laight @ 2026-03-01 22:16 UTC (permalink / raw)
  To: Linus Torvalds
  Cc: Christophe Leroy (CS GROUP), Thomas Gleixner, LKML,
	Christophe Leroy, Mathieu Desnoyers, Andrew Cooper,
	kernel test robot, Russell King, linux-arm-kernel, x86,
	Madhavan Srinivasan, Michael Ellerman, Nicholas Piggin,
	linuxppc-dev, Paul Walmsley, Palmer Dabbelt, linux-riscv,
	Heiko Carstens, Christian Borntraeger, Sven Schnelle, linux-s390,
	Julia Lawall, Nicolas Palix, Peter Zijlstra, Darren Hart,
	Davidlohr Bueso, Andre Almeida, Alexander Viro, Christian Brauner,
	Jan Kara, linux-fsdevel

On Sun, 1 Mar 2026 12:01:08 -0800
Linus Torvalds <torvalds@linux-foundation.org> wrote:

I also added to compiler.h :

+/*
+ * Sometimes a #define needs to declare a variable that is scoped
+ * to the statement that follows without having mismatched {}.
+ *	with (int x = expression) {
+ *		statements
+ *	}
+ * is the same as:
+ *	{
+ *		int x = expression;
+ *		statements
+ *	}
+ * but lets it all be hidden from the call site, eg:
+ *	frobnicate(args) {
+ *		statements
+ *	} 
+ * Only a single variable can be defined, and_with() allows extra ones
+ * without adding an additional outer loop.
+ *
+ * The controlled scope can be terminated using break, continue or goto.
+ */
+#define with(declaration) \
+	for (bool _with_done = false; !_with_done; _with_done = true)	\
+		and_with (declaration)
+#define and_with(declaration) \
+	for (declaration; !_with_done; _with_done = true)
+

So that you get:
#define __scoped_user_access(mode, uptr, size, elbl)					\
	with (auto _tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl)) \
		and_with (CLASS(user_##mode##_access, scope)(_tmpptr))			\
		/* Force modified pointer usage within the scope */			\
		and_with (const auto uptr = _tmpptr)

The next patch did:
-		and_with (const typeof(uptr) uptr = _tmpptr)
+		__diag_push() __diag_ignore_all("-Wshadow", "uptr is readonly copy")	\
+		and_with (const typeof(uptr) uptr = _tmpptr)				\
+		__diag_pop()

I'll update (to use auto as above) and resend.

	David

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-03-01 22:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-01 19:33 [PATCH] uaccess: Fix build of scoped user access with const pointer Christophe Leroy (CS GROUP)
2026-03-01 20:01 ` Linus Torvalds
2026-03-01 21:59   ` David Laight
2026-03-01 22:16   ` David Laight
2026-03-01 21:52 ` David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox