Re: [tip:x86/mm 5/16] sound/core/hwdep.c:243:24: sparse: sparse: incorrect type in assignment (different address spaces)

From: Dave Hansen
Date: Mon Nov 14 2022 - 17:55:53 EST


On 11/14/22 13:41, kernel test robot wrote:
> sound/core/hwdep.c:243:24: sparse: expected int [noderef] __user *__ptr_clean
> sound/core/hwdep.c:243:24: sparse: got int *
> sound/core/hwdep.c:273:29: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected int [noderef] __user *__ptr_clean @@ got int * @@
> sound/core/hwdep.c:273:29: sparse: expected int [noderef] __user *__ptr_clean
> sound/core/hwdep.c:273:29: sparse: got int *
> sound/core/hwdep.c:292:29: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected int [noderef] __user *__ptr_clean @@ got int * @@
> sound/core/hwdep.c:292:29: sparse: expected int [noderef] __user *__ptr_clean
> sound/core/hwdep.c:292:29: sparse: got int *

I think the sparse ends up throwing away all of its annotations once it
dereferences a pointer. So, '*(int __user *)' boils down to a plain
'int'. Confusingly, a '*(int __user *) *' boils down to an 'int *'.

That's what happened here. A __user-annotated point got dereferenced
down to an 'int' and then turned into a pointer again.

I think the trick in this case is to avoid dereferencing the pointer too
early by just moving the dereference outside of the casting, like the
attached patch. But, it also feels kinda wrong. I'd love a second
opinion on this one.diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
index 0db6f5451854..b8947b623c72 100644
--- a/arch/x86/include/asm/uaccess.h
+++ b/arch/x86/include/asm/uaccess.h
@@ -39,7 +39,7 @@ static inline bool pagefault_disabled(void);
#define untagged_ptr(mm, ptr) ({ \
u64 __ptrval = (__force u64)(ptr); \
__ptrval = untagged_addr(mm, __ptrval); \
- (__force __typeof__(*(ptr)) *)__ptrval; \
+ *(__force __typeof__((ptr)) *)__ptrval; \
})
#else
#define untagged_addr(mm, addr) (addr)