Skip to contentSkip to conversation

x86/uaccess: fix code generation in put_user()

PATCH Applied Rasmus Villemoes 6 participants 13 messages Oct 23, 2020 – Oct 24, 2020 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/uaccess: fix code generation in put_user()

Quoting
https://gcc.gnu.org/onlinedocs/gcc/Local-Register-Variables.html:

You can define a local register variable and associate it with a specified register...

The only supported use for this feature is to specify registers for input and output operands when calling Extended asm (see Extended Asm). This may be necessary if the constraints for a particular machine don't provide sufficient control to select the desired register.

On 32-bit x86, this is used to ensure that gcc will put an 8-byte value into the %edx:%eax pair, while all other cases will just use the single register %eax (%rax on x86-64). While the _ASM_AX actually just expands to "%eax", note this comment next to get_user() which does something very similar:

* The use of _ASM_DX as the register specifier is a bit of a
* simplification, as gcc only cares about it as the starting point
* and not size: for a 64-bit value it will use %ecx:%edx on 32 bits
* (%ecx being the next register in gcc's x86 register sequence), and
* %rdx on 64 bits.

However, getting this to work requires that there is no code between the assignment to the local register variable and its use as an input to the asm() which can possibly clobber any of the registers involved
- including evaluation of the expressions making up other inputs.

In the current code, the ptr expression used directly as an input may cause such code to be emitted. For example, Sean Christopherson observed that with KASAN enabled and ptr being current->set_child_tid (from chedule_tail()), the load of current->set_child_tid causes a call to __asan_load8() to be emitted immediately prior to the __put_user_4 call, and Naresh Kamboju reports that various mmstress tests fail on KASAN-enabled builds. It's also possible to synthesize a broken case without KASAN if one uses "foo()" as the ptr argument, with foo being some "extern u64
__user *foo(void);" (though I don't know if that appears in real code).

Fix it by making sure ptr gets evaluated before the assignment to __val_pu, and add a comment that __val_pu must be the last thing computed before the asm() is entered.

Cc Sean Christopherson <[email protected]>
Fixes d55564cfc222 ("x86: Make __put_user() generate an out-of-line call")

---

I tried to put together a changelog to earn just a little claim-to-fame, feel free to edit, especially if I got something wrong. Did I miss any appropriate *-by tags?

I'm wondering if one would also need to make __ptr_pu and __ret_pu explicitly "%"_ASM_CX".

 arch/x86/include/asm/uaccess.h | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
arch/x86/include/asm/uaccess.h +9 -1
index f13659523108..c9fa7be3df82 100644
@@ -208,16 +208,24 @@ extern void __put_user_nocheck_2(void);
 extern void __put_user_nocheck_4(void);
 extern void __put_user_nocheck_8(void);
 
+/*
+ * ptr must be evaluated and assigned to the temporary __ptr_pu before
+ * the assignment of x to __val_pu, to avoid any function calls
+ * involved in the ptr expression (possibly implicitly generated due
+ * to KASAN) from clobbering %ax.
+ */
 #define do_put_user_call(fn,x,ptr)					\
 ({									\
 	int __ret_pu;							\
+	void __user *__ptr_pu;						\
 	register __typeof__(*(ptr)) __val_pu asm("%"_ASM_AX);		\
 	__chk_user_ptr(ptr);						\
+	__ptr_pu = (ptr);						\
 	__val_pu = (x);							\
 	asm volatile("call __" #fn "_%P[size]"				\
 		     : "=c" (__ret_pu),					\
 			ASM_CALL_CONSTRAINT				\
-		     : "0" (ptr),					\
+		     : "0" (__ptr_pu),					\
 		       "r" (__val_pu),					\
 		       [size] "i" (sizeof(*(ptr)))			\
 		     :"ebx");						\
Signature · 1 line
2.23.0
#1 lore raw
Quoted 25 lines · Rasmus Villemoes

Quoting
https://gcc.gnu.org/onlinedocs/gcc/Local-Register-Variables.html:

You can define a local register variable and associate it with a specified register...

The only supported use for this feature is to specify registers for input and output operands when calling Extended asm (see Extended Asm). This may be necessary if the constraints for a particular machine don't provide sufficient control to select the desired register.

On 32-bit x86, this is used to ensure that gcc will put an 8-byte value into the %edx:%eax pair, while all other cases will just use the single register %eax (%rax on x86-64). While the _ASM_AX actually just expands to "%eax", note this comment next to get_user() which does something very similar:

* The use of _ASM_DX as the register specifier is a bit of a
* simplification, as gcc only cares about it as the starting point
* and not size: for a 64-bit value it will use %ecx:%edx on 32 bits
* (%ecx being the next register in gcc's x86 register sequence), and
* %rdx on 64 bits.

Quoted 4 lines

However, getting this to work requires that there is no code between the assignment to the local register variable and its use as an input to the asm() which can possibly clobber any of the registers involved
- including evaluation of the expressions making up other inputs.

This looks like the patch is an improvement, but this is still IMO likely to be very fragile. Can we just do the size-dependent "a" vs "A" selection method instead? Sure, it's a little more code, but it will be Obviously Correct. As it stands, I can easily see our code failing on some gcc or clang version and the compiler authors telling us that we're relying on unsupportable behavior.

#2 lore raw
Quoted 7 lines · Andy Lutomirski

This looks like the patch is an improvement, but this is still IMO likely to be very fragile. Can we just do the size-dependent "a" vs "A" selection method instead? Sure, it's a little more code, but it will be Obviously Correct. As it stands, I can easily see our code failing on some gcc or clang version and the compiler authors telling us that we're relying on unsupportable behavior.

Well, the "register asm()" thing actually _is_ documented, and it's something we've used before too (and still use in other places).

We actually have quite a bit of them - just do a

git grep 'register.*asm('

to see literally hundreds of them. So gcc (and clang) actually has a lot of test-cases for it.

In many ways, x86 actually tends to need _fewer_ of these than most other architectures, since on x86, we almost always have specific register naming in ways that other architectures often don't (because x86 has all those specific register rules).

So the 64-bit issue for x86-32 is a bit of a special case for x86, but this is in no way a special case in general, and is quite the common pattern on other architectures.

The fact that KASAN could generate code in between the register asm assignment was something I just overlooked (and embarrassingly similar to the issues we had with objdump checking STAC/CLAC back when we inlined that all).

It's a bit sad that gcc doesn't _warn_ about this kind of issue, since the compiler certainly should see "oh, we just had a register clash". But it is what it is..

Linus

#3 lore raw
In reply to Andy Lutomirski
Quoted 34 lines · Andy Lutomirski
Quoted 7 lines · Rasmus Villemoes

Quoting
https://gcc.gnu.org/onlinedocs/gcc/Local-Register-Variables.html:

You can define a local register variable and associate it with a specified register...

Quoted 8 lines

The only supported use for this feature is to specify registers for input and output operands when calling Extended asm (see Extended Asm). This may be necessary if the constraints for a particular machine don't provide sufficient control to select the desired register.

On 32-bit x86, this is used to ensure that gcc will put an 8-byte value into the %edx:%eax pair, while all other cases will just use

the

Quoted 1 line

single register %eax (%rax on x86-64). While the _ASM_AX actually

just

Quoted 9 lines

expands to "%eax", note this comment next to get_user() which does something very similar:

* The use of _ASM_DX as the register specifier is a bit of a
* simplification, as gcc only cares about it as the starting point
* and not size: for a 64-bit value it will use %ecx:%edx on 32 bits
* (%ecx being the next register in gcc's x86 register sequence), and
* %rdx on 64 bits.

Quoted 4 lines

However, getting this to work requires that there is no code between the assignment to the local register variable and its use as an input to the asm() which can possibly clobber any of the registers involved
- including evaluation of the expressions making up other inputs.

Quoted 6 lines

This looks like the patch is an improvement, but this is still IMO likely to be very fragile. Can we just do the size-dependent "a" vs "A" selection method instead? Sure, it's a little more code, but it will be Obviously Correct. As it stands, I can easily see our code failing on some gcc or clang version and the compiler authors telling us that we're relying on unsupportable behavior.

Yeah, the reason get_user hacks it is because there is no equivalent to "A" for other register pairs, but not using it for dx:ax is just silly.

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
#4 lore raw
In reply to Rasmus Villemoes

Thanks, applied.

Quoted 3 lines · Rasmus Villemoes

I'm wondering if one would also need to make __ptr_pu and __ret_pu explicitly "%"_ASM_CX".

No, the "c"/"0" thing is much better, and makes it properly atomic wrt the actual asm.

As mentioned to Andy, the "register asm()" thing is not uncommon and often useful, but when you can specify the register directly in asm, that's certainly simpler and more straightforward and preferred.

Linus

#5 lore raw
Quoted 8 lines · Linus Torvalds

Thanks, applied.

Quoted 3 lines · Rasmus Villemoes

I'm wondering if one would also need to make __ptr_pu and __ret_pu explicitly "%"_ASM_CX".

Quoted 8 lines

No, the "c"/"0" thing is much better, and makes it properly atomic wrt the actual asm.

As mentioned to Andy, the "register asm()" thing is not uncommon and often useful, but when you can specify the register directly in asm, that's certainly simpler and more straightforward and preferred.

Linus

There is no same reason to mess around with hacks when we are talking about dx:ax, though. We have to do pretty ugly hacks when other register pairs are involved, but "A" is there for a reason. _ASM_AX64 maybe...

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
#6 lore raw
Quoted 2 lines · On Fri, Oct 23, 2020 at 2:00 PM <[email protected]> wrote:

There is no same reason to mess around with hacks when we are talking about dx:ax, though.

Sure there is.

"A" doesn't actually mean %edx:%eax like you seem to think.

It actually means %eax OR %edx, and then if given a 64-bit value, it will use the combination (with %edx being the high bits).

So using "A" unconditionally doesn't work - it gives random behavior for 32-bit (or smaller) types.

Or you'd have to cast the value to always be 64-bit, and have the extra code generation.

IOW, an unconditional "A" is wrong.

And the alternative is to just duplicate things, and go back to the explicit size testing, but honestly, I really think that's much worse than relying on a documented feature of "register asm()" that gcc _documents_ is for this kind of inline asm use.

So the "don't do pointless conditional duplication" is certainly a very sane reason for the code.

Linus

#7 lore raw

From: Linus Torvalds

Quoted 19 lines

Sent: 23 October 2020 22:11

Quoted 2 lines · On Fri, Oct 23, 2020 at 2:00 PM <[email protected]> wrote:

There is no same reason to mess around with hacks when we are talking about dx:ax, though.

Sure there is.

"A" doesn't actually mean %edx:%eax like you seem to think.

It actually means %eax OR %edx, and then if given a 64-bit value, it will use the combination (with %edx being the high bits).

So using "A" unconditionally doesn't work - it gives random behavior for 32-bit (or smaller) types.

Or you'd have to cast the value to always be 64-bit, and have the extra code generation.

Quoted 6 lines

IOW, an unconditional "A" is wrong.

And the alternative is to just duplicate things, and go back to the explicit size testing, but honestly, I really think that's much worse than relying on a documented feature of "register asm()" that gcc _documents_ is for this kind of inline asm use.

Could do_put_user() do an initial check for 64 bit then expand a different #define that contains the actual code passing either "a" or "A" for the constriant.

Apart from another level of indirection nothing is duplicated.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#8 lore raw
Quoted 32 lines · David Laight

From: Linus Torvalds

Quoted 5 lines

Sent: 23 October 2020 22:11

Quoted 2 lines · On Fri, Oct 23, 2020 at 2:00 PM <[email protected]> wrote:

There is no same reason to mess around with hacks when we are

talking about dx:ax, though.

Quoted 14 lines

Sure there is.

"A" doesn't actually mean %edx:%eax like you seem to think.

It actually means %eax OR %edx, and then if given a 64-bit value, it will use the combination (with %edx being the high bits).

So using "A" unconditionally doesn't work - it gives random behavior for 32-bit (or smaller) types.

Or you'd have to cast the value to always be 64-bit, and have the extra code generation.

Quoted 6 lines

IOW, an unconditional "A" is wrong.

And the alternative is to just duplicate things, and go back to the explicit size testing, but honestly, I really think that's much worse than relying on a documented feature of "register asm()" that gcc _documents_ is for this kind of inline asm use.

Could do_put_user() do an initial check for 64 bit then expand a different #define that contains the actual code passing either "a" or "A" for the constriant.

Quoted 8 lines

Apart from another level of indirection nothing is duplicated.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

Maybe #define ASM_AX64 or some such.

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
#9 lore raw
In reply to David Laight

From: David Laight

Quoted 1 line

Sent: 23 October 2020 22:52

...

Quoted 5 lines

Could do_put_user() do an initial check for 64 bit then expand a different #define that contains the actual code passing either "a" or "A" for the constriant.

Apart from another level of indirection nothing is duplicated.

This code seems to compile to something sensible. It does need change the registers that get_user_n() must use - the normal return value is now in %ax (and %dx for 64bit values on 32bit systems, with the error in %cx. (I've not actually tested it.)

#define __inttype_max(x, _max) __typeof__(      \
        __typefits(x,char,                      \
          __typefits(x,short,                   \
            __typefits(x,int,                   \
              __typefits(x,long,_max)))))

#define __inttype(x) __inttype_max(x, 0ULL)

#define get_user_1(x, ptr, type, constraint)                            \
({                                                                      \
        int __ret_gu;                                                   \
        type __val_gu;                                                  \
        asm volatile("call __get_user_%P4"                              \
                     : "=c" (__ret_gu), constraint (__val_gu),          \
                        ASM_CALL_CONSTRAINT                             \
                     : "a" (ptr), "i" (sizeof(*(ptr))));                \
        (x) = (__force __typeof__(*(ptr))) __val_gu;                    \
        __builtin_expect(__ret_gu, 0);                                  \

})

#define get_user(x, ptr)                                                \
({                                                                      \
        __chk_user_ptr(ptr);                                            \
        might_fault();                                                  \
        (sizeof *(ptr) > sizeof(long))                                  \
                ? get_user_1(x, ptr, long long, "=A")                   \
                : get_user_1(x, ptr, __inttype_max(*(ptr),0ul), "=a");  \

})

The __inttype_max() is needed (I think) because clang will try (and fail) to generate the asm for 64bit values on 32bit systems.
So the type needs limiting to 32bits.
Always using 'long' works - but generates extra casts.

The "=A" constraint (%rax or %rdx) is never used on 64bit because the test is always false.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#10 lore raw
In reply to Linus Torvalds
Quoted 25 lines · Linus Torvalds
Quoted 2 lines · On Fri, Oct 23, 2020 at 2:00 PM <[email protected]> wrote:

There is no same reason to mess around with hacks when we are talking

about dx:ax, though.

Sure there is.

"A" doesn't actually mean %edx:%eax like you seem to think.

It actually means %eax OR %edx, and then if given a 64-bit value, it will use the combination (with %edx being the high bits).

So using "A" unconditionally doesn't work - it gives random behavior for 32-bit (or smaller) types.

Or you'd have to cast the value to always be 64-bit, and have the extra code generation.

IOW, an unconditional "A" is wrong.

And the alternative is to just duplicate things, and go back to the explicit size testing, but honestly, I really think that's much worse than relying on a documented feature of "register asm()" that gcc _documents_ is for this kind of inline asm use.

Quoted 4 lines

So the "don't do pointless conditional duplication" is certainly a very sane reason for the code.

Linus

Unconditional "A" is definitely wrong, no argument there.

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
#11 lore raw
In reply to Rasmus Villemoes
I love your patch! Perhaps something to improve:

Hi Rasmus,

I love your patch! Perhaps something to improve:

[auto build test WARNING on tip/master]
[also build test WARNING on next-20201023]
[cannot apply to tip/x86/core linus/master linux/master v5.9] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Rasmus-Villemoes/x86-uaccess-fix-code-generation-in-put_user/20201024-043534
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 95d591cca3b1851eaf66af066149183f18ff5ab3
config: x86_64-randconfig-s031-20201023 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.3-17-g2d3af347-dirty
        # https://github.com/0day-ci/linux/commit/72d916d679c03da185905e3640d7396875085f1e
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Rasmus-Villemoes/x86-uaccess-fix-code-generation-in-put_user/20201024-043534
        git checkout 72d916d679c03da185905e3640d7396875085f1e
        # save the attached .config to linux build tree
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate

"sparse warnings: (new ones prefixed by >>)"
kernel/sys.c:1878:19: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected struct file [noderef] __rcu *__ret @@ got struct file *[assigned] file @@
kernel/sys.c:1878:19: sparse: expected struct file [noderef] __rcu *__ret
kernel/sys.c:1878:19: sparse: got struct file *[assigned] file
kernel/sys.c:1878:17: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct file *old_exe @@ got struct file [noderef] __rcu *[assigned] __ret @@
kernel/sys.c:1878:17: sparse: expected struct file *old_exe
kernel/sys.c:1878:17: sparse: got struct file [noderef] __rcu *[assigned] __ret
kernel/sys.c:2243:16: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const volatile [noderef] __user *ptr @@ got int [noderef] __user **tid_addr @@
kernel/sys.c:2243:16: sparse: expected void const volatile [noderef] __user *ptr
kernel/sys.c:2243:16: sparse: got int [noderef] __user **tid_addr

>> kernel/sys.c:2243:16: sparse: sparse: incorrect type in assignment (different address spaces) @@     expected void [noderef] __user *__ptr_pu @@     got int [noderef] __user **tid_addr @@
>> kernel/sys.c:2243:16: sparse:     expected void [noderef] __user *__ptr_pu

kernel/sys.c:2243:16: sparse: got int [noderef] __user **tid_addr
kernel/sys.c:1049:32: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct task_struct *p1 @@ got struct task_struct [noderef] __rcu *real_parent @@
kernel/sys.c:1049:32: sparse: expected struct task_struct *p1
kernel/sys.c:1049:32: sparse: got struct task_struct [noderef] __rcu *real_parent
kernel/sys.c: note: in included file (through include/linux/rcuwait.h, include/linux/percpu-rwsem.h, include/linux/fs.h, ...):
include/linux/sched/signal.h:695:37: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
include/linux/sched/signal.h:695:37: sparse: expected struct spinlock [usertype] *lock
include/linux/sched/signal.h:695:37: sparse: got struct spinlock [noderef] __rcu *
--
drivers/scsi/megaraid/megaraid_sas_base.c:117:5: sparse: sparse: symbol 'host_tagset_enable' was not declared. Should it be static?
drivers/scsi/megaraid/megaraid_sas_base.c:4561:21: sparse: sparse: incorrect type in assignment (different base types) @@ expected restricted __le16 [usertype] flags @@ got int @@
drivers/scsi/megaraid/megaraid_sas_base.c:4561:21: sparse: expected restricted __le16 [usertype] flags
drivers/scsi/megaraid/megaraid_sas_base.c:4561:21: sparse: got int
drivers/scsi/megaraid/megaraid_sas_base.c:4812:21: sparse: sparse: incorrect type in assignment (different base types) @@ expected restricted __le16 [usertype] flags @@ got int @@
drivers/scsi/megaraid/megaraid_sas_base.c:4812:21: sparse: expected restricted __le16 [usertype] flags
drivers/scsi/megaraid/megaraid_sas_base.c:4812:21: sparse: got int
drivers/scsi/megaraid/megaraid_sas_base.c:6627:21: sparse: sparse: incorrect type in assignment (different base types) @@ expected restricted __le16 [usertype] flags @@ got int @@
drivers/scsi/megaraid/megaraid_sas_base.c:6627:21: sparse: expected restricted __le16 [usertype] flags
drivers/scsi/megaraid/megaraid_sas_base.c:6627:21: sparse: got int
drivers/scsi/megaraid/megaraid_sas_base.c:4468:21: sparse: sparse: incorrect type in assignment (different base types) @@ expected restricted __le16 [usertype] flags @@ got int @@
drivers/scsi/megaraid/megaraid_sas_base.c:4468:21: sparse: expected restricted __le16 [usertype] flags
drivers/scsi/megaraid/megaraid_sas_base.c:4468:21: sparse: got int
drivers/scsi/megaraid/megaraid_sas_base.c:282:31: sparse: sparse: restricted __le16 degrades to integer drivers/scsi/megaraid/megaraid_sas_base.c:288:31: sparse: sparse: cast from restricted __le16 drivers/scsi/megaraid/megaraid_sas_base.c:414:29: sparse: sparse: cast to restricted __le32
drivers/scsi/megaraid/megaraid_sas_base.c:1218:32: sparse: sparse: incorrect type in assignment (different base types) @@ expected unsigned int [usertype] opcode @@ got restricted __le32 [usertype] opcode @@
drivers/scsi/megaraid/megaraid_sas_base.c:1218:32: sparse: expected unsigned int [usertype] opcode
drivers/scsi/megaraid/megaraid_sas_base.c:1218:32: sparse: got restricted __le32 [usertype] opcode
drivers/scsi/megaraid/megaraid_sas_base.c:1996:33: sparse: sparse: cast to restricted __le32 drivers/scsi/megaraid/megaraid_sas_base.c:2037:34: sparse: sparse: cast to restricted __le32
drivers/scsi/megaraid/megaraid_sas_base.c:4692:21: sparse: sparse: incorrect type in assignment (different base types) @@ expected restricted __le16 [usertype] flags @@ got int @@
drivers/scsi/megaraid/megaraid_sas_base.c:4692:21: sparse: expected restricted __le16 [usertype] flags
drivers/scsi/megaraid/megaraid_sas_base.c:4692:21: sparse: got int
drivers/scsi/megaraid/megaraid_sas_base.c:4931:21: sparse: sparse: incorrect type in assignment (different base types) @@ expected restricted __le16 [usertype] flags @@ got int @@
drivers/scsi/megaraid/megaraid_sas_base.c:4931:21: sparse: expected restricted __le16 [usertype] flags
drivers/scsi/megaraid/megaraid_sas_base.c:4931:21: sparse: got int
drivers/scsi/megaraid/megaraid_sas_base.c:5116:21: sparse: sparse: incorrect type in assignment (different base types) @@ expected restricted __le16 [usertype] flags @@ got int @@
drivers/scsi/megaraid/megaraid_sas_base.c:5116:21: sparse: expected restricted __le16 [usertype] flags
drivers/scsi/megaraid/megaraid_sas_base.c:5116:21: sparse: got int
drivers/scsi/megaraid/megaraid_sas_base.c:5198:21: sparse: sparse: incorrect type in assignment (different base types) @@ expected restricted __le16 [usertype] flags @@ got int @@
drivers/scsi/megaraid/megaraid_sas_base.c:5198:21: sparse: expected restricted __le16 [usertype] flags
drivers/scsi/megaraid/megaraid_sas_base.c:5198:21: sparse: got int
drivers/scsi/megaraid/megaraid_sas_base.c:6180:42: sparse: sparse: cast removes address space '__iomem' of expression
drivers/scsi/megaraid/megaraid_sas_base.c:6179:57: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected unsigned int [noderef] [usertype] __iomem * @@ got unsigned int [usertype] * @@
drivers/scsi/megaraid/megaraid_sas_base.c:6179:57: sparse: expected unsigned int [noderef] [usertype] __iomem *
drivers/scsi/megaraid/megaraid_sas_base.c:6179:57: sparse: got unsigned int [usertype] *
drivers/scsi/megaraid/megaraid_sas_base.c:6184:34: sparse: sparse: cast removes address space '__iomem' of expression
drivers/scsi/megaraid/megaraid_sas_base.c:6183:57: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected unsigned int [noderef] [usertype] __iomem * @@ got unsigned int [usertype] * @@
drivers/scsi/megaraid/megaraid_sas_base.c:6183:57: sparse: expected unsigned int [noderef] [usertype] __iomem *
drivers/scsi/megaraid/megaraid_sas_base.c:6183:57: sparse: got unsigned int [usertype] *
drivers/scsi/megaraid/megaraid_sas_base.c:6491:21: sparse: sparse: incorrect type in assignment (different base types) @@ expected restricted __le16 [usertype] flags @@ got int @@
drivers/scsi/megaraid/megaraid_sas_base.c:6491:21: sparse: expected restricted __le16 [usertype] flags
drivers/scsi/megaraid/megaraid_sas_base.c:6491:21: sparse: got int
drivers/scsi/megaraid/megaraid_sas_base.c:6705:21: sparse: sparse: incorrect type in assignment (different base types) @@ expected restricted __le16 [usertype] flags @@ got int @@
drivers/scsi/megaraid/megaraid_sas_base.c:6705:21: sparse: expected restricted __le16 [usertype] flags
drivers/scsi/megaraid/megaraid_sas_base.c:6705:21: sparse: got int
drivers/scsi/megaraid/megaraid_sas_base.c:7461:44: sparse: sparse: restricted __le32 degrades to integer
drivers/scsi/megaraid/megaraid_sas_base.c:8092:23: sparse: sparse: incorrect type in assignment (different base types) @@ expected int crash_support @@ got restricted __le32 @@
drivers/scsi/megaraid/megaraid_sas_base.c:8092:23: sparse: expected int crash_support
drivers/scsi/megaraid/megaraid_sas_base.c:8092:23: sparse: got restricted __le32
drivers/scsi/megaraid/megaraid_sas_base.c:8176:31: sparse: sparse: invalid assignment: &=
drivers/scsi/megaraid/megaraid_sas_base.c:8176:31: sparse: left side has type restricted __le16
drivers/scsi/megaraid/megaraid_sas_base.c:8176:31: sparse: right side has type int
drivers/scsi/megaraid/megaraid_sas_base.c:8270:36: sparse: sparse: incorrect type in assignment (different base types) @@ expected unsigned long @@ got restricted __le64 [usertype] @@
drivers/scsi/megaraid/megaraid_sas_base.c:8270:36: sparse: expected unsigned long
drivers/scsi/megaraid/megaraid_sas_base.c:8270:36: sparse: got restricted __le64 [usertype]
drivers/scsi/megaraid/megaraid_sas_base.c:8272:36: sparse: sparse: incorrect type in assignment (different base types) @@ expected unsigned long @@ got restricted __le32 [usertype] @@
drivers/scsi/megaraid/megaraid_sas_base.c:8272:36: sparse: expected unsigned long
drivers/scsi/megaraid/megaraid_sas_base.c:8272:36: sparse: got restricted __le32 [usertype]
drivers/scsi/megaraid/megaraid_sas_base.c:8523:21: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const volatile [noderef] __user *ptr @@ got unsigned int [usertype] *sense_cioc_ptr @@
drivers/scsi/megaraid/megaraid_sas_base.c:8523:21: sparse: expected void const volatile [noderef] __user *ptr
drivers/scsi/megaraid/megaraid_sas_base.c:8523:21: sparse: got unsigned int [usertype] *sense_cioc_ptr
drivers/scsi/megaraid/megaraid_sas_base.c:8524:21: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const volatile [noderef] __user *ptr @@ got void [noderef] __user **sense_ioc_ptr @@
drivers/scsi/megaraid/megaraid_sas_base.c:8524:21: sparse: expected void const volatile [noderef] __user *ptr
drivers/scsi/megaraid/megaraid_sas_base.c:8524:21: sparse: got void [noderef] __user **sense_ioc_ptr

>> drivers/scsi/megaraid/megaraid_sas_base.c:8524:21: sparse: sparse: incorrect type in assignment (different address spaces) @@     expected void [noderef] __user *__ptr_pu @@     got void [noderef] __user **sense_ioc_ptr @@
>> drivers/scsi/megaraid/megaraid_sas_base.c:8524:21: sparse:     expected void [noderef] __user *__ptr_pu

drivers/scsi/megaraid/megaraid_sas_base.c:8524:21: sparse: got void [noderef] __user **sense_ioc_ptr
drivers/scsi/megaraid/megaraid_sas_base.c:8730:46: sparse: sparse: restricted __le32 degrades to integer
--
drivers/gpu/drm/radeon/radeon_ttm.c:1008:21: sparse: sparse: cast removes address space '__user' of expression drivers/gpu/drm/radeon/radeon_ttm.c:1008:21: sparse: sparse: cast removes address space '__user' of expression
drivers/gpu/drm/radeon/radeon_ttm.c:1008:21: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const volatile [noderef] __user *ptr @@ got unsigned int [usertype] * @@
drivers/gpu/drm/radeon/radeon_ttm.c:1008:21: sparse: expected void const volatile [noderef] __user *ptr
drivers/gpu/drm/radeon/radeon_ttm.c:1008:21: sparse: got unsigned int [usertype] *
drivers/gpu/drm/radeon/radeon_ttm.c:1008:21: sparse: sparse: cast removes address space '__user' of expression

>> drivers/gpu/drm/radeon/radeon_ttm.c:1008:21: sparse: sparse: incorrect type in assignment (different address spaces) @@     expected void [noderef] __user *__ptr_pu @@     got unsigned int [usertype] * @@
>> drivers/gpu/drm/radeon/radeon_ttm.c:1008:21: sparse:     expected void [noderef] __user *__ptr_pu

drivers/gpu/drm/radeon/radeon_ttm.c:1008:21: sparse: got unsigned int [usertype] *
drivers/gpu/drm/radeon/radeon_ttm.c:1008:21: sparse: sparse: cast removes address space '__user' of expression

vim +2243 kernel/sys.c

300f786b2683f8b Cyrill Gorcunov 2012-06-07  2239  
52b3694157e3aa6 Amnon Shiloh    2013-04-30  2240  #ifdef CONFIG_CHECKPOINT_RESTORE
300f786b2683f8b Cyrill Gorcunov 2012-06-07  2241  static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
300f786b2683f8b Cyrill Gorcunov 2012-06-07  2242  {
300f786b2683f8b Cyrill Gorcunov 2012-06-07 @2243  	return put_user(me->clear_child_tid, tid_addr);
300f786b2683f8b Cyrill Gorcunov 2012-06-07  2244  }
52b3694157e3aa6 Amnon Shiloh    2013-04-30  2245  #else
300f786b2683f8b Cyrill Gorcunov 2012-06-07  2246  static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
300f786b2683f8b Cyrill Gorcunov 2012-06-07  2247  {
300f786b2683f8b Cyrill Gorcunov 2012-06-07  2248  	return -EINVAL;
300f786b2683f8b Cyrill Gorcunov 2012-06-07  2249  }
028ee4be34a09a6 Cyrill Gorcunov 2012-01-12  2250  #endif

028ee4be34a09a6 Cyrill Gorcunov 2012-01-12 2251

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#12 lore raw
In reply to Rasmus Villemoes
I love your patch! Perhaps something to improve:

Hi Rasmus,

I love your patch! Perhaps something to improve:

[auto build test WARNING on tip/master]
[also build test WARNING on next-20201023]
[cannot apply to tip/x86/core linus/master linux/master v5.9] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Rasmus-Villemoes/x86-uaccess-fix-code-generation-in-put_user/20201024-043534
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 95d591cca3b1851eaf66af066149183f18ff5ab3
config: i386-randconfig-s031-20201023 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.3-17-g2d3af347-dirty
        # https://github.com/0day-ci/linux/commit/72d916d679c03da185905e3640d7396875085f1e
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Rasmus-Villemoes/x86-uaccess-fix-code-generation-in-put_user/20201024-043534
        git checkout 72d916d679c03da185905e3640d7396875085f1e
        # save the attached .config to linux build tree
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=i386 

If you fix the issue, kindly add following tag as appropriate

"sparse warnings: (new ones prefixed by >>)"
drivers/watchdog/sbc_fitpc2_wdt.c:99:21: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const volatile [noderef] __user *ptr @@ got char const * @@
drivers/watchdog/sbc_fitpc2_wdt.c:99:21: sparse: expected void const volatile [noderef] __user *ptr
drivers/watchdog/sbc_fitpc2_wdt.c:99:21: sparse: got char const *
drivers/watchdog/sbc_fitpc2_wdt.c:128:37: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void [noderef] __user *to @@ got struct watchdog_info * @@
drivers/watchdog/sbc_fitpc2_wdt.c:128:37: sparse: expected void [noderef] __user *to
drivers/watchdog/sbc_fitpc2_wdt.c:128:37: sparse: got struct watchdog_info *
drivers/watchdog/sbc_fitpc2_wdt.c:133:23: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const volatile [noderef] __user *ptr @@ got int * @@
drivers/watchdog/sbc_fitpc2_wdt.c:133:23: sparse: expected void const volatile [noderef] __user *ptr
drivers/watchdog/sbc_fitpc2_wdt.c:133:23: sparse: got int *

>> drivers/watchdog/sbc_fitpc2_wdt.c:133:23: sparse: sparse: incorrect type in assignment (different address spaces) @@     expected void [noderef] __user *__ptr_pu @@     got int * @@
>> drivers/watchdog/sbc_fitpc2_wdt.c:133:23: sparse:     expected void [noderef] __user *__ptr_pu

drivers/watchdog/sbc_fitpc2_wdt.c:133:23: sparse: got int *
drivers/watchdog/sbc_fitpc2_wdt.c:137:23: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const volatile [noderef] __user *ptr @@ got int * @@
drivers/watchdog/sbc_fitpc2_wdt.c:137:23: sparse: expected void const volatile [noderef] __user *ptr
drivers/watchdog/sbc_fitpc2_wdt.c:137:23: sparse: got int *
drivers/watchdog/sbc_fitpc2_wdt.c:137:23: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected void [noderef] __user *__ptr_pu @@ got int * @@
drivers/watchdog/sbc_fitpc2_wdt.c:137:23: sparse: expected void [noderef] __user *__ptr_pu
drivers/watchdog/sbc_fitpc2_wdt.c:137:23: sparse: got int *
drivers/watchdog/sbc_fitpc2_wdt.c:146:23: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const volatile [noderef] __user *ptr @@ got int * @@
drivers/watchdog/sbc_fitpc2_wdt.c:146:23: sparse: expected void const volatile [noderef] __user *ptr
drivers/watchdog/sbc_fitpc2_wdt.c:146:23: sparse: got int *
drivers/watchdog/sbc_fitpc2_wdt.c:160:23: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const volatile [noderef] __user *ptr @@ got int * @@
drivers/watchdog/sbc_fitpc2_wdt.c:160:23: sparse: expected void const volatile [noderef] __user *ptr
drivers/watchdog/sbc_fitpc2_wdt.c:160:23: sparse: got int *
drivers/watchdog/sbc_fitpc2_wdt.c:160:23: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected void [noderef] __user *__ptr_pu @@ got int * @@
drivers/watchdog/sbc_fitpc2_wdt.c:160:23: sparse: expected void [noderef] __user *__ptr_pu
drivers/watchdog/sbc_fitpc2_wdt.c:160:23: sparse: got int *
drivers/watchdog/sbc_fitpc2_wdt.c:187:27: sparse: sparse: incorrect type in initializer (incompatible argument 2 (different address spaces)) @@ expected int ( *write )( ... ) @@ got int ( * )( ... ) @@
drivers/watchdog/sbc_fitpc2_wdt.c:187:27: sparse: expected int ( *write )( ... )
drivers/watchdog/sbc_fitpc2_wdt.c:187:27: sparse: got int ( * )( ... )

vim +133 drivers/watchdog/sbc_fitpc2_wdt.c

3a5f90002e9d08e Denis Turischev     2009-07-21  118  
3a5f90002e9d08e Denis Turischev     2009-07-21  119  
3a5f90002e9d08e Denis Turischev     2009-07-21  120  static long fitpc2_wdt_ioctl(struct file *file, unsigned int cmd,
3a5f90002e9d08e Denis Turischev     2009-07-21  121  							unsigned long arg)
3a5f90002e9d08e Denis Turischev     2009-07-21  122  {
3a5f90002e9d08e Denis Turischev     2009-07-21  123  	int ret = -ENOTTY;
3a5f90002e9d08e Denis Turischev     2009-07-21  124  	int time;
3a5f90002e9d08e Denis Turischev     2009-07-21  125  
3a5f90002e9d08e Denis Turischev     2009-07-21  126  	switch (cmd) {
3a5f90002e9d08e Denis Turischev     2009-07-21  127  	case WDIOC_GETSUPPORT:
3a5f90002e9d08e Denis Turischev     2009-07-21  128  		ret = copy_to_user((struct watchdog_info *)arg, &ident,
3a5f90002e9d08e Denis Turischev     2009-07-21  129  				   sizeof(ident)) ? -EFAULT : 0;
3a5f90002e9d08e Denis Turischev     2009-07-21  130  		break;
3a5f90002e9d08e Denis Turischev     2009-07-21  131  
3a5f90002e9d08e Denis Turischev     2009-07-21  132  	case WDIOC_GETSTATUS:
3a5f90002e9d08e Denis Turischev     2009-07-21 @133  		ret = put_user(0, (int *)arg);
3a5f90002e9d08e Denis Turischev     2009-07-21  134  		break;
3a5f90002e9d08e Denis Turischev     2009-07-21  135  
3a5f90002e9d08e Denis Turischev     2009-07-21  136  	case WDIOC_GETBOOTSTATUS:
3a5f90002e9d08e Denis Turischev     2009-07-21  137  		ret = put_user(0, (int *)arg);
3a5f90002e9d08e Denis Turischev     2009-07-21  138  		break;
3a5f90002e9d08e Denis Turischev     2009-07-21  139  
3a5f90002e9d08e Denis Turischev     2009-07-21  140  	case WDIOC_KEEPALIVE:
3a5f90002e9d08e Denis Turischev     2009-07-21  141  		wdt_enable();
3a5f90002e9d08e Denis Turischev     2009-07-21  142  		ret = 0;
3a5f90002e9d08e Denis Turischev     2009-07-21  143  		break;
3a5f90002e9d08e Denis Turischev     2009-07-21  144  
3a5f90002e9d08e Denis Turischev     2009-07-21  145  	case WDIOC_SETTIMEOUT:
3a5f90002e9d08e Denis Turischev     2009-07-21  146  		ret = get_user(time, (int *)arg);
3a5f90002e9d08e Denis Turischev     2009-07-21  147  		if (ret)
3a5f90002e9d08e Denis Turischev     2009-07-21  148  			break;
3a5f90002e9d08e Denis Turischev     2009-07-21  149  
3a5f90002e9d08e Denis Turischev     2009-07-21  150  		if (time < 31 || time > 255) {
3a5f90002e9d08e Denis Turischev     2009-07-21  151  			ret = -EINVAL;
3a5f90002e9d08e Denis Turischev     2009-07-21  152  			break;
3a5f90002e9d08e Denis Turischev     2009-07-21  153  		}
3a5f90002e9d08e Denis Turischev     2009-07-21  154  
3a5f90002e9d08e Denis Turischev     2009-07-21  155  		margin = time;
3a5f90002e9d08e Denis Turischev     2009-07-21  156  		wdt_enable();
bd490f8222510de Gustavo A. R. Silva 2020-07-07  157  		fallthrough;
3a5f90002e9d08e Denis Turischev     2009-07-21  158  
3a5f90002e9d08e Denis Turischev     2009-07-21  159  	case WDIOC_GETTIMEOUT:
3a5f90002e9d08e Denis Turischev     2009-07-21  160  		ret = put_user(margin, (int *)arg);
3a5f90002e9d08e Denis Turischev     2009-07-21  161  		break;
3a5f90002e9d08e Denis Turischev     2009-07-21  162  	}
3a5f90002e9d08e Denis Turischev     2009-07-21  163  
3a5f90002e9d08e Denis Turischev     2009-07-21  164  	return ret;
3a5f90002e9d08e Denis Turischev     2009-07-21  165  }
3a5f90002e9d08e Denis Turischev     2009-07-21  166  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org