Skip to contentSkip to conversation

Fix warnings with recent smatch

PATCH Superseded Ricardo Ribalda 3 participants 10 messages Sep 14 – Sep 24

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Fix warnings with recent smatch

Recent versions of smatch preserve the address space qualifiers while running typeof() [1]. This has triggered tens of new sparse warnings in media-ci.

This series takes care of them following Dan's suggestion [2].

[1] https://github.com/error27/smatch/commit/e53027a4e816a772403baafa83c09e4a94c1cb8f
[2] https://lore.kernel.org/all/CANiDSCuizbMt77XfrzGPMa3Biai5TF1vcs+C5qdhYoLrP=9b3g@mail.gmail.com/

---
Changes in v2:
- Add TYPEOF_NO_ADDRESS_SPACE() in compiler.h
- x86: improve changelog
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Ricardo Ribalda (2):
rcu: Drop the address space qualifier from the dereference macros x86/uaccess: Drop the address space qualifier from put_user()

 arch/x86/include/asm/uaccess.h |  4 ++--
 include/linux/compiler.h       | 17 +++++++++++++++++
 include/linux/rcupdate.h       | 10 +++++-----
 3 files changed, 24 insertions(+), 7 deletions(-)

---
base-commit: 704340f1cd0dcef829eb62f5b48ae95a2ce17bdf
change-id: 20260825-unqual-25d9ee74fd10

Best regards,

Signature · 1 line
Ricardo Ribalda <[email protected]>
#1 lore raw

PATCH v2 1/2 rcu: Drop the address space qualifier from the dereference macros

The RCU dereference macros end with a cast that is meant to hand back a plain kernel pointer from a __rcu pointer.

For that it uses:

	((typeof(*p) __force __kernel *)(local))

The problem is that typeof preserves every qualifier, including the address space qualifiers (__rcu).

Recent versions of smatch[1] care about this and throw tens of warnings
like this one:
./include/trace/events/vb2.h:46:1: warning: incorrect type in assignment (different address spaces)
./include/trace/events/vb2.h:46:1: expected struct tracepoint_func *it_func_ptr
./include/trace/events/vb2.h:46:1: got struct tracepoint_func __rcu *

Use a new macro TYPEOF_NO_ADDRESS_SPACE() for the result type. This new macro strips all the qualifiers when running with sparse (so const and volatile are gone). But keeps all the qualifiers when running with the compiler, caring about const/volatile mismatch.

[1] https://github.com/error27/smatch/commit/e53027a4e816a772403baafa83c09e4a94c1cb8f

---
 include/linux/compiler.h | 17 +++++++++++++++++
 include/linux/rcupdate.h | 10 +++++-----
 2 files changed, 22 insertions(+), 5 deletions(-)
include/linux/compiler.h +17 -0
index cb2f6050bdf7..70cb31d60538 100644
@@ -239,6 +239,23 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 # define TYPEOF_UNQUAL(exp) __typeof__(exp)
 #endif
 
+/*
+ * TYPEOF_NO_ADDRESS_SPACE() - typeof() without the address space qualifiers
+ *
+ * No operator strips only the address space qualifiers: typeof() keeps every
+ * qualifier and TYPEOF_UNQUAL() drops every qualifier, const and volatile
+ * included.
+ *
+ * Approximate one by dropping the qualifiers for sparse only, as it is the
+ * only one that knows about address spaces. The compiler keeps seeing the fully
+ * qualified type, so a missing const or volatile will still throw a warning.
+ */
+#ifdef __CHECKER__
+# define TYPEOF_NO_ADDRESS_SPACE(exp) TYPEOF_UNQUAL(exp)
+#else
+# define TYPEOF_NO_ADDRESS_SPACE(exp) __typeof__(exp)
+#endif
+
 #endif /* __KERNEL__ */
 
 #if defined(CONFIG_CFI) && !defined(__DISABLE_EXPORTS) && !defined(BUILD_VDSO)
include/linux/rcupdate.h +5 -5
index 44c07a66edff..3f74ae6d6e1f 100644
@@ -488,7 +488,7 @@ static __always_inline bool lockdep_assert_rcu_helper(bool c, const struct __ctx
 context_unsafe(								\
 	typeof(*p) *local = (typeof(*p) *__force)(p);			\
 	rcu_check_sparse(p, __rcu);					\
-	((typeof(*p) __force __kernel *)(local))			\
+	((TYPEOF_NO_ADDRESS_SPACE(*p) __force __kernel *)(local))	\
 )
 /**
  * unrcu_pointer - mark a pointer as not being RCU protected
@@ -503,7 +503,7 @@ context_unsafe(								\
 ({ \
 	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
 	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((TYPEOF_NO_ADDRESS_SPACE(*p) __force __kernel *)(local)); \
 }) )
 #define __rcu_dereference_check(p, local, c, space) \
 ({ \
@@ -511,19 +511,19 @@ context_unsafe(								\
 	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \
 	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((TYPEOF_NO_ADDRESS_SPACE(*p) __force __kernel *)(local)); \
 })
 #define __rcu_dereference_protected(p, local, c, space) \
 ({ \
 	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \
 	rcu_check_sparse(p, space); \
-	((typeof(*p) __force __kernel *)(p)); \
+	((TYPEOF_NO_ADDRESS_SPACE(*p) __force __kernel *)(p)); \
 })
 #define __rcu_dereference_raw(p, local) \
 ({ \
 	/* Dependency order vs. p above. */ \
 	typeof(p) local = READ_ONCE(p); \
-	((typeof(*p) __force __kernel *)(local)); \
+	((TYPEOF_NO_ADDRESS_SPACE(*p) __force __kernel *)(local)); \
 })
 #define rcu_dereference_raw(p) __rcu_dereference_raw(p, __UNIQUE_ID(rcu))
 
Signature · 1 line
2.55.0.1007.g17ff1f9808-goog
#2 lore raw

PATCH v2 2/2 x86/uaccess: Drop the address space qualifier from put_user()

In reply to Ricardo Ribalda

__typeof__() preserves every qualifier, the address space included.

When we do __typeof(*__user *int), we are declaring a __user integer, which is pretty much meaningless. A value in a register or in the stack does not live in the user address space.

This annotation did not trigger any error until a recent version of smatch[1] started caring. And as a result of that now we have tens of warnings like:

drivers/media/usb/uvc/uvc_v4l2.c:1112:13: warning: incorrect type in argument 2 (different address spaces)
drivers/media/usb/uvc/uvc_v4l2.c:1112:13: expected void const *from
drivers/media/usb/uvc/uvc_v4l2.c:1112:13: got unsigned int __user *

Instead of __typeof__() use TYPEOF_UNQUAL(). It maps to __typeof_unqual__() in recent compilers, so the address space annotation is gone.

[1] https://github.com/error27/smatch/commit/e53027a4e816a772403baafa83c09e4a94c1cb8f

---
 arch/x86/include/asm/uaccess.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/uaccess.h +2 -2
index 3a0dd3c2b233..4e576c0b9131 100644
@@ -172,7 +172,7 @@ extern void __put_user_nocheck_8(void);
 	int __ret_pu;							\
 	void __user *__ptr_pu;						\
 	register __typeof__(*(ptr)) __val_pu asm("%"_ASM_AX);		\
-	__typeof__(*(ptr)) __x = (x); /* eval x once */			\
+	TYPEOF_UNQUAL(*(ptr)) __x = (x); /* eval x once */		\
 	__typeof__(ptr) __ptr = (ptr); /* eval ptr once */		\
 	__chk_user_ptr(__ptr);						\
 	__ptr_pu = __ptr;						\
@@ -231,7 +231,7 @@ extern void __put_user_nocheck_8(void);
 
 #define __put_user_size(x, ptr, size, label)				\
 do {									\
-	__typeof__(*(ptr)) __x = (x); /* eval x once */			\
+	TYPEOF_UNQUAL(*(ptr)) __x = (x); /* eval x once */		\
 	__typeof__(ptr) __ptr = (ptr); /* eval ptr once */		\
 	__chk_user_ptr(__ptr);						\
 	switch (size) {							\
Signature · 1 line
2.55.0.1007.g17ff1f9808-goog
maintainer
#3 lore raw

Re: [PATCH v2 2/2] x86/uaccess: Drop the address space qualifier from put_user()

Quoted 18 lines · Ricardo Ribalda

__typeof__() preserves every qualifier, the address space included.

When we do __typeof(*__user *int), we are declaring a __user integer, which is pretty much meaningless. A value in a register or in the stack does not live in the user address space.

This annotation did not trigger any error until a recent version of smatch[1] started caring. And as a result of that now we have tens of warnings like:

drivers/media/usb/uvc/uvc_v4l2.c:1112:13: warning: incorrect type in argument 2 (different address spaces)
drivers/media/usb/uvc/uvc_v4l2.c:1112:13: expected void const *from
drivers/media/usb/uvc/uvc_v4l2.c:1112:13: got unsigned int __user *

Instead of __typeof__() use TYPEOF_UNQUAL(). It maps to __typeof_unqual__() in recent compilers, so the address space annotation is gone.

Quoted 5 lines

[1] https://github.com/error27/smatch/commit/e53027a4e816a772403baafa83c09e4a94c1cb8f

Queued for testing and review, thank you both!

Thanx, Paul

Quoted 30 lines
---
 arch/x86/include/asm/uaccess.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/uaccess.h +2 -2
index 3a0dd3c2b233..4e576c0b9131 100644
@@ -172,7 +172,7 @@ extern void __put_user_nocheck_8(void);
 	int __ret_pu;							\
 	void __user *__ptr_pu;						\
 	register __typeof__(*(ptr)) __val_pu asm("%"_ASM_AX);		\
-	__typeof__(*(ptr)) __x = (x); /* eval x once */			\
+	TYPEOF_UNQUAL(*(ptr)) __x = (x); /* eval x once */		\
 	__typeof__(ptr) __ptr = (ptr); /* eval ptr once */		\
 	__chk_user_ptr(__ptr);						\
 	__ptr_pu = __ptr;						\
@@ -231,7 +231,7 @@ extern void __put_user_nocheck_8(void);
 
 #define __put_user_size(x, ptr, size, label)				\
 do {									\
-	__typeof__(*(ptr)) __x = (x); /* eval x once */			\
+	TYPEOF_UNQUAL(*(ptr)) __x = (x); /* eval x once */		\
 	__typeof__(ptr) __ptr = (ptr); /* eval ptr once */		\
 	__chk_user_ptr(__ptr);						\
 	switch (size) {							\
Signature · 2 lines
-- 
2.55.0.1007.g17ff1f9808-goog
maintainer
#4 lore raw

Re: [PATCH v2 2/2] x86/uaccess: Drop the address space qualifier from put_user()

Quoted 19 lines · Paul E. McKenney
Quoted 18 lines · Ricardo Ribalda

__typeof__() preserves every qualifier, the address space included.

When we do __typeof(*__user *int), we are declaring a __user integer, which is pretty much meaningless. A value in a register or in the stack does not live in the user address space.

This annotation did not trigger any error until a recent version of smatch[1] started caring. And as a result of that now we have tens of warnings like:

drivers/media/usb/uvc/uvc_v4l2.c:1112:13: warning: incorrect type in argument 2 (different address spaces)
drivers/media/usb/uvc/uvc_v4l2.c:1112:13: expected void const *from
drivers/media/usb/uvc/uvc_v4l2.c:1112:13: got unsigned int __user *

Instead of __typeof__() use TYPEOF_UNQUAL(). It maps to __typeof_unqual__() in recent compilers, so the address space annotation is gone.

Quoted 7 lines
Quoted 5 lines

[1] https://github.com/error27/smatch/commit/e53027a4e816a772403baafa83c09e4a94c1cb8f

Queued for testing and review, thank you both!

GAH!!! I actually queued the rcu_ref one and replied to the wrong email, apologies for my confusion. Or was it a hallucination? You be the judge! ;-)

0a45e252da65 ("rcu: Drop the address space qualifier from the dereference macros")

Thanx, Paul

Quoted 31 lines
Quoted 30 lines
---
 arch/x86/include/asm/uaccess.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/uaccess.h +2 -2
index 3a0dd3c2b233..4e576c0b9131 100644
@@ -172,7 +172,7 @@ extern void __put_user_nocheck_8(void);
 	int __ret_pu;							\
 	void __user *__ptr_pu;						\
 	register __typeof__(*(ptr)) __val_pu asm("%"_ASM_AX);		\
-	__typeof__(*(ptr)) __x = (x); /* eval x once */			\
+	TYPEOF_UNQUAL(*(ptr)) __x = (x); /* eval x once */		\
 	__typeof__(ptr) __ptr = (ptr); /* eval ptr once */		\
 	__chk_user_ptr(__ptr);						\
 	__ptr_pu = __ptr;						\
@@ -231,7 +231,7 @@ extern void __put_user_nocheck_8(void);
 
 #define __put_user_size(x, ptr, size, label)				\
 do {									\
-	__typeof__(*(ptr)) __x = (x); /* eval x once */			\
+	TYPEOF_UNQUAL(*(ptr)) __x = (x); /* eval x once */		\
 	__typeof__(ptr) __ptr = (ptr); /* eval ptr once */		\
 	__chk_user_ptr(__ptr);						\
 	switch (size) {							\
Signature · 2 lines
-- 
2.55.0.1007.g17ff1f9808-goog
#5 lore raw

Re: [PATCH v2 2/2] x86/uaccess: Drop the address space qualifier from put_user()

In reply to Ricardo Ribalda

Friendly ping?

Quoted 54 lines · Ricardo Ribalda

__typeof__() preserves every qualifier, the address space included.

When we do __typeof(*__user *int), we are declaring a __user integer, which is pretty much meaningless. A value in a register or in the stack does not live in the user address space.

This annotation did not trigger any error until a recent version of smatch[1] started caring. And as a result of that now we have tens of warnings like:

drivers/media/usb/uvc/uvc_v4l2.c:1112:13: warning: incorrect type in argument 2 (different address spaces)
drivers/media/usb/uvc/uvc_v4l2.c:1112:13: expected void const *from
drivers/media/usb/uvc/uvc_v4l2.c:1112:13: got unsigned int __user *

Instead of __typeof__() use TYPEOF_UNQUAL(). It maps to __typeof_unqual__() in recent compilers, so the address space annotation is gone.

[1] https://github.com/error27/smatch/commit/e53027a4e816a772403baafa83c09e4a94c1cb8f

---
 arch/x86/include/asm/uaccess.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/uaccess.h +2 -3
index 3a0dd3c2b233..4e576c0b9131 100644
@@ -172,7 +172,7 @@ extern void __put_user_nocheck_8(void);
        int __ret_pu;                                                   \
        void __user *__ptr_pu;                                          \
        register __typeof__(*(ptr)) __val_pu asm("%"_ASM_AX);           \
-       __typeof__(*(ptr)) __x = (x); /* eval x once */                 \
+       TYPEOF_UNQUAL(*(ptr)) __x = (x); /* eval x once */              \
        __typeof__(ptr) __ptr = (ptr); /* eval ptr once */              \
        __chk_user_ptr(__ptr);                                          \
        __ptr_pu = __ptr;                                               \
@@ -231,7 +231,7 @@ extern void __put_user_nocheck_8(void);
 
 #define __put_user_size(x, ptr, size, label)                           \
 do {                                                                   \
-       __typeof__(*(ptr)) __x = (x); /* eval x once */                 \
+       TYPEOF_UNQUAL(*(ptr)) __x = (x); /* eval x once */              \
        __typeof__(ptr) __ptr = (ptr); /* eval ptr once */              \
        __chk_user_ptr(__ptr);                                          \
        switch (size) {                                                 \
 
--

2.55.0.1007.g17ff1f9808-goog

Signature · 1 line
Ricardo Ribalda
#6 lore raw

Re: [PATCH v2 2/2] x86/uaccess: Drop the address space qualifier from put_user()

Quoted 9 lines · On Wed, 23 Sep 2026 13:08:20 +0200 Ricardo Ribalda <[email protected]> wrote:

Friendly ping?

Quoted 6 lines · Ricardo Ribalda

__typeof__() preserves every qualifier, the address space included.

When we do __typeof(*__user *int), we are declaring a __user integer, which is pretty much meaningless. A value in a register or in the stack does not live in the user address space.

Doesn't that mean it is a smatch bug - so should be fixed there.

Somewhere in the middle of this there ought to checks that the value is the correct type (eg pointer v integer) and that, for put_user(), the destination pointer isn't const.
Using:

	auto __x = 0 ? *ptr : x;

can be more succinct than other versions - might not help here.

David

Quoted 50 lines
Quoted 48 lines

This annotation did not trigger any error until a recent version of smatch[1] started caring. And as a result of that now we have tens of warnings like:

drivers/media/usb/uvc/uvc_v4l2.c:1112:13: warning: incorrect type in argument 2 (different address spaces)
drivers/media/usb/uvc/uvc_v4l2.c:1112:13: expected void const *from
drivers/media/usb/uvc/uvc_v4l2.c:1112:13: got unsigned int __user *

Instead of __typeof__() use TYPEOF_UNQUAL(). It maps to __typeof_unqual__() in recent compilers, so the address space annotation is gone.

[1] https://github.com/error27/smatch/commit/e53027a4e816a772403baafa83c09e4a94c1cb8f

---
 arch/x86/include/asm/uaccess.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/uaccess.h +2 -3
index 3a0dd3c2b233..4e576c0b9131 100644
@@ -172,7 +172,7 @@ extern void __put_user_nocheck_8(void);
        int __ret_pu;                                                   \
        void __user *__ptr_pu;                                          \
        register __typeof__(*(ptr)) __val_pu asm("%"_ASM_AX);           \
-       __typeof__(*(ptr)) __x = (x); /* eval x once */                 \
+       TYPEOF_UNQUAL(*(ptr)) __x = (x); /* eval x once */              \
        __typeof__(ptr) __ptr = (ptr); /* eval ptr once */              \
        __chk_user_ptr(__ptr);                                          \
        __ptr_pu = __ptr;                                               \
@@ -231,7 +231,7 @@ extern void __put_user_nocheck_8(void);
 
 #define __put_user_size(x, ptr, size, label)                           \
 do {                                                                   \
-       __typeof__(*(ptr)) __x = (x); /* eval x once */                 \
+       TYPEOF_UNQUAL(*(ptr)) __x = (x); /* eval x once */              \
        __typeof__(ptr) __ptr = (ptr); /* eval ptr once */              \
        __chk_user_ptr(__ptr);                                          \
        switch (size) {                                                 \
 
--

2.55.0.1007.g17ff1f9808-goog

#7 lore raw

Re: [PATCH v2 2/2] x86/uaccess: Drop the address space qualifier from put_user()

Hi David

Quoted 8 lines · David Laight
Quoted 4 lines · On Wed, 23 Sep 2026 13:08:20 +0200 Ricardo Ribalda <[email protected]> wrote:

Friendly ping?

Quoted 1 line · Ricardo Ribalda
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

__typeof__() preserves every qualifier, the address space included.

When we do __typeof(*__user *int), we are declaring a __user integer, which is pretty much meaningless. A value in a register or in the stack does not live in the user address space.

Doesn't that mean it is a smatch bug - so should be fixed there.

I don't think it is a Smatch bug: __typeof__() preserves all qualifiers including address spaces. Smatch is just enforcing that more strictly now.

I'd prefer that developers explicitly say when they are crossing the address space boundaries, and TYPEOF_UNQUAL() seems to be the right macro, which also Dan recommended.

Quoted 61 lines

Somewhere in the middle of this there ought to checks that the value is the correct type (eg pointer v integer) and that, for put_user(), the destination pointer isn't const.
Using:

        auto __x = 0 ? *ptr : x;

can be more succinct than other versions - might not help here.

David

Quoted 50 lines
Quoted 48 lines

This annotation did not trigger any error until a recent version of smatch[1] started caring. And as a result of that now we have tens of warnings like:

drivers/media/usb/uvc/uvc_v4l2.c:1112:13: warning: incorrect type in argument 2 (different address spaces)
drivers/media/usb/uvc/uvc_v4l2.c:1112:13: expected void const *from
drivers/media/usb/uvc/uvc_v4l2.c:1112:13: got unsigned int __user *

Instead of __typeof__() use TYPEOF_UNQUAL(). It maps to __typeof_unqual__() in recent compilers, so the address space annotation is gone.

[1] https://github.com/error27/smatch/commit/e53027a4e816a772403baafa83c09e4a94c1cb8f

---
 arch/x86/include/asm/uaccess.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/uaccess.h +2 -3
index 3a0dd3c2b233..4e576c0b9131 100644
@@ -172,7 +172,7 @@ extern void __put_user_nocheck_8(void);
        int __ret_pu;                                                   \
        void __user *__ptr_pu;                                          \
        register __typeof__(*(ptr)) __val_pu asm("%"_ASM_AX);           \
-       __typeof__(*(ptr)) __x = (x); /* eval x once */                 \
+       TYPEOF_UNQUAL(*(ptr)) __x = (x); /* eval x once */              \
        __typeof__(ptr) __ptr = (ptr); /* eval ptr once */              \
        __chk_user_ptr(__ptr);                                          \
        __ptr_pu = __ptr;                                               \
@@ -231,7 +231,7 @@ extern void __put_user_nocheck_8(void);
 
 #define __put_user_size(x, ptr, size, label)                           \
 do {                                                                   \
-       __typeof__(*(ptr)) __x = (x); /* eval x once */                 \
+       TYPEOF_UNQUAL(*(ptr)) __x = (x); /* eval x once */              \
        __typeof__(ptr) __ptr = (ptr); /* eval ptr once */              \
        __chk_user_ptr(__ptr);                                          \
        switch (size) {                                                 \
 
--

2.55.0.1007.g17ff1f9808-goog

Signature · 1 line
Ricardo Ribalda
#8 lore raw

Re: [PATCH v2 2/2] x86/uaccess: Drop the address space qualifier from put_user()

Quoted 20 lines · On Thu, 24 Sep 2026 09:19:16 +0200 Ricardo Ribalda <[email protected]> wrote:

Hi David

Quoted 8 lines · David Laight
Quoted 4 lines · On Wed, 23 Sep 2026 13:08:20 +0200 Ricardo Ribalda <[email protected]> wrote:

Friendly ping?

Quoted 1 line · Ricardo Ribalda
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

__typeof__() preserves every qualifier, the address space included.

When we do __typeof(*__user *int), we are declaring a __user integer, which is pretty much meaningless. A value in a register or in the stack does not live in the user address space.

Doesn't that mean it is a smatch bug - so should be fixed there.

Quoted 7 lines

I don't think it is a Smatch bug: __typeof__() preserves all qualifiers including address spaces. Smatch is just enforcing that more strictly now.

I'd prefer that developers explicitly say when they are crossing the address space boundaries, and TYPEOF_UNQUAL() seems to be the right macro, which also Dan recommended.

It depends where you are removing it.
TYPEOF_UNQUAL() also removes const - which you don't want to remove. It is also entirely horrid on older versions of gcc.

David

Quoted 64 lines
Quoted 62 lines

Somewhere in the middle of this there ought to checks that the value is the correct type (eg pointer v integer) and that, for put_user(), the destination pointer isn't const.
Using:

        auto __x = 0 ? *ptr : x;

can be more succinct than other versions - might not help here.

David

Quoted 50 lines
Quoted 48 lines

This annotation did not trigger any error until a recent version of smatch[1] started caring. And as a result of that now we have tens of warnings like:

drivers/media/usb/uvc/uvc_v4l2.c:1112:13: warning: incorrect type in argument 2 (different address spaces)
drivers/media/usb/uvc/uvc_v4l2.c:1112:13: expected void const *from
drivers/media/usb/uvc/uvc_v4l2.c:1112:13: got unsigned int __user *

Instead of __typeof__() use TYPEOF_UNQUAL(). It maps to __typeof_unqual__() in recent compilers, so the address space annotation is gone.

[1] https://github.com/error27/smatch/commit/e53027a4e816a772403baafa83c09e4a94c1cb8f

---
 arch/x86/include/asm/uaccess.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/uaccess.h +2 -3
index 3a0dd3c2b233..4e576c0b9131 100644
@@ -172,7 +172,7 @@ extern void __put_user_nocheck_8(void);
        int __ret_pu;                                                   \
        void __user *__ptr_pu;                                          \
        register __typeof__(*(ptr)) __val_pu asm("%"_ASM_AX);           \
-       __typeof__(*(ptr)) __x = (x); /* eval x once */                 \
+       TYPEOF_UNQUAL(*(ptr)) __x = (x); /* eval x once */              \
        __typeof__(ptr) __ptr = (ptr); /* eval ptr once */              \
        __chk_user_ptr(__ptr);                                          \
        __ptr_pu = __ptr;                                               \
@@ -231,7 +231,7 @@ extern void __put_user_nocheck_8(void);
 
 #define __put_user_size(x, ptr, size, label)                           \
 do {                                                                   \
-       __typeof__(*(ptr)) __x = (x); /* eval x once */                 \
+       TYPEOF_UNQUAL(*(ptr)) __x = (x); /* eval x once */              \
        __typeof__(ptr) __ptr = (ptr); /* eval ptr once */              \
        __chk_user_ptr(__ptr);                                          \
        switch (size) {                                                 \
 
--

2.55.0.1007.g17ff1f9808-goog

#9 lore raw

Re: [PATCH v2 2/2] x86/uaccess: Drop the address space qualifier from put_user()

Quoted 28 lines · David Laight
Quoted 24 lines · On Thu, 24 Sep 2026 09:19:16 +0200 Ricardo Ribalda <[email protected]> wrote:

Hi David

Quoted 8 lines · David Laight
Quoted 4 lines · On Wed, 23 Sep 2026 13:08:20 +0200 Ricardo Ribalda <[email protected]> wrote:

Friendly ping?

Quoted 1 line · Ricardo Ribalda
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

__typeof__() preserves every qualifier, the address space included.

When we do __typeof(*__user *int), we are declaring a __user integer, which is pretty much meaningless. A value in a register or in the stack does not live in the user address space.

Doesn't that mean it is a smatch bug - so should be fixed there.

I don't think it is a Smatch bug: __typeof__() preserves all qualifiers including address spaces. Smatch is just enforcing that more strictly now.

Quoted 7 lines
Quoted 3 lines

I'd prefer that developers explicitly say when they are crossing the address space boundaries, and TYPEOF_UNQUAL() seems to be the right macro, which also Dan recommended.

It depends where you are removing it.
TYPEOF_UNQUAL() also removes const - which you don't want to remove. It is also entirely horrid on older versions of gcc.

What about using TYPEOF_NO_ADDRESS_SPACE() defined in 1/2

https://lore.kernel.org/all/[email protected]/

Would that work for you?

It uses TYPEOF_UNQUAL for smatch, but keep typeof for normal builds

Quoted 68 lines

David

Quoted 64 lines
Quoted 62 lines

Somewhere in the middle of this there ought to checks that the value is the correct type (eg pointer v integer) and that, for put_user(), the destination pointer isn't const.
Using:

        auto __x = 0 ? *ptr : x;

can be more succinct than other versions - might not help here.

David

Quoted 50 lines
Quoted 48 lines

This annotation did not trigger any error until a recent version of smatch[1] started caring. And as a result of that now we have tens of warnings like:

drivers/media/usb/uvc/uvc_v4l2.c:1112:13: warning: incorrect type in argument 2 (different address spaces)
drivers/media/usb/uvc/uvc_v4l2.c:1112:13: expected void const *from
drivers/media/usb/uvc/uvc_v4l2.c:1112:13: got unsigned int __user *

Instead of __typeof__() use TYPEOF_UNQUAL(). It maps to __typeof_unqual__() in recent compilers, so the address space annotation is gone.

[1] https://github.com/error27/smatch/commit/e53027a4e816a772403baafa83c09e4a94c1cb8f

---
 arch/x86/include/asm/uaccess.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/uaccess.h +2 -3
index 3a0dd3c2b233..4e576c0b9131 100644
@@ -172,7 +172,7 @@ extern void __put_user_nocheck_8(void);
        int __ret_pu;                                                   \
        void __user *__ptr_pu;                                          \
        register __typeof__(*(ptr)) __val_pu asm("%"_ASM_AX);           \
-       __typeof__(*(ptr)) __x = (x); /* eval x once */                 \
+       TYPEOF_UNQUAL(*(ptr)) __x = (x); /* eval x once */              \
        __typeof__(ptr) __ptr = (ptr); /* eval ptr once */              \
        __chk_user_ptr(__ptr);                                          \
        __ptr_pu = __ptr;                                               \
@@ -231,7 +231,7 @@ extern void __put_user_nocheck_8(void);
 
 #define __put_user_size(x, ptr, size, label)                           \
 do {                                                                   \
-       __typeof__(*(ptr)) __x = (x); /* eval x once */                 \
+       TYPEOF_UNQUAL(*(ptr)) __x = (x); /* eval x once */              \
        __typeof__(ptr) __ptr = (ptr); /* eval ptr once */              \
        __chk_user_ptr(__ptr);                                          \
        switch (size) {                                                 \
 
--

2.55.0.1007.g17ff1f9808-goog

Signature · 1 line
Ricardo Ribalda