Skip to contentSkip to conversation

arm64/traps: Avoid unnecessary kernel/user pointer conversion

PATCH Superseded Amit Daniel Kachhap 2 participants 3 messages Sep 14, 2021 – Sep 15, 2021 arm64

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64/traps: Avoid unnecessary kernel/user pointer conversion

Annotating a pointer from kernel to __user and then back again might confuse sparse. In call_undef_hook() it can be avoided by not using the intermediate user pointer variable.

Note: This patch adds no functional changes to code.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
---
 arch/arm64/kernel/traps.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/kernel/traps.c +2 -1
index b03e383d944a..357d10a8bbf5 100644
@@ -404,7 +404,8 @@ static int call_undef_hook(struct pt_regs *regs)
 
 	if (!user_mode(regs)) {
 		__le32 instr_le;
-		if (get_kernel_nofault(instr_le, (__force __le32 *)pc))
+		if (get_kernel_nofault(instr_le,
+				       (__le32 *)instruction_pointer(regs)))
 			goto exit;
 		instr = le32_to_cpu(instr_le);
 	} else if (compat_thumb_mode(regs)) {
Signature · 1 line
2.17.1
reviewer
#1 lore raw
Quoted 3 lines · Amit Daniel Kachhap

Annotating a pointer from kernel to __user and then back again might confuse sparse. In call_undef_hook() it can be avoided by not using the intermediate user pointer variable.

When you say "might confuse sparse", does it complain today? If so, can you include an example of what goes wrong?

Quoted 14 lines

Note: This patch adds no functional changes to code.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
---
 arch/arm64/kernel/traps.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/kernel/traps.c +0 -0
index b03e383d944a..357d10a8bbf5 100644

arch/arm64/kernel/traps.c static int call_undef_hook(struct pt_regs *regs) · line 408

Quoted 6 lines
arch/arm64/kernel/traps.c +2 -1
@@ -404,7 +404,8 @@ static int call_undef_hook(struct pt_regs *regs)
 
 	if (!user_mode(regs)) {
 		__le32 instr_le;
-		if (get_kernel_nofault(instr_le, (__force __le32 *)pc))
+		if (get_kernel_nofault(instr_le,
+				       (__le32 *)instruction_pointer(regs)))

Can we make pc an unsigned long, instead?

It'd be nice to handle all three cases consistently, even if that means adding __force to the two user cases.

Thanks,
Mark.

Quoted 6 lines
arch/arm64/kernel/traps.c +0 -0
 			goto exit;
 		instr = le32_to_cpu(instr_le);
 	} else if (compat_thumb_mode(regs)) {
Signature · 1 line
2.17.1
#2 lore raw

Hi,

Quoted 7 lines · Mark Rutland
Quoted 3 lines · Amit Daniel Kachhap

Annotating a pointer from kernel to __user and then back again might confuse sparse. In call_undef_hook() it can be avoided by not using the intermediate user pointer variable.

When you say "might confuse sparse", does it complain today? If so, can you include an example of what goes wrong?

No it does not give warning. The __force option silences the warning. My idea is to remove the unwanted __force annotations and not mix user and kernel pointers.

Quoted 16 lines
Quoted 16 lines

Note: This patch adds no functional changes to code.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
---
  arch/arm64/kernel/traps.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/kernel/traps.c +0 -0
index b03e383d944a..357d10a8bbf5 100644
@@ -404,7 +404,8 @@ static int call_undef_hook(struct pt_regs *regs)
 
Quoted 7 lines

arch/arm64/kernel/traps.c static int call_undef_hook(struct pt_regs *regs) · line 408

Quoted 5 lines
arch/arm64/kernel/traps.c +2 -1
  	if (!user_mode(regs)) {
  		__le32 instr_le;
-		if (get_kernel_nofault(instr_le, (__force __le32 *)pc))
+		if (get_kernel_nofault(instr_le,
+				       (__le32 *)instruction_pointer(regs)))

Can we make pc an unsigned long, instead?

I think it can be done.

Quoted 3 lines

It'd be nice to handle all three cases consistently, even if that means adding __force to the two user cases.

Agree with your suggestion. Even in the 2 user cases, __force may not be needed as the typecast will be from from unsigned long to user pointer.

BR,
Amit

Quoted 10 lines

Thanks,
Mark.

Quoted 6 lines
arch/arm64/kernel/traps.c +0 -0
  			goto exit;
  		instr = le32_to_cpu(instr_le);
  	} else if (compat_thumb_mode(regs)) {
Signature · 1 line
2.17.1