Skip to contentSkip to conversation

arm64: constify arguments to satisfy asm constraints

PATCH Applied Jisheng Zhang 4 participants 11 messages Oct 6, 2022 – Nov 7, 2022

Subsystems (2)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover arm64: constify arguments to satisfy asm constraints

Constify arguments of arch_static_branch(), arch_static_branch_jump() and alternative_has_feature_* to satisfy asm constraints.

Jisheng Zhang (2):
arm64: jump_label: mark arguments as const to satisfy asm constraints
arm64: alternative: constify alternative_has_feature_* argument

 arch/arm64/include/asm/alternative-macros.h | 4 ++--
 arch/arm64/include/asm/jump_label.h         | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)
Signature · 1 line
2.37.2
#1 lore raw

PATCH 1/2 arm64: jump_label: mark arguments as const to satisfy asm constraints

Inspired by x86 commit 864b435514b2("x86/jump_label: Mark arguments as const to satisfy asm constraints"), mark arch_static_branch()'s and arch_static_branch_jump()'s arguments as const to satisfy asm constraints. And Steven in [1] also pointed out that "The "i" constraint needs to be a constant."

Tested with building a simple external kernel module with "O0".

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

---
 arch/arm64/include/asm/jump_label.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/include/asm/jump_label.h +4 -4
index cea441b6aa5d..48ddc0f45d22 100644
@@ -15,8 +15,8 @@
 
 #define JUMP_LABEL_NOP_SIZE		AARCH64_INSN_SIZE
 
-static __always_inline bool arch_static_branch(struct static_key *key,
-					       bool branch)
+static __always_inline bool arch_static_branch(struct static_key * const key,
+					       const bool branch)
 {
 	asm_volatile_goto(
 		"1:	nop					\n\t"
@@ -32,8 +32,8 @@ static __always_inline bool arch_static_branch(struct static_key *key,
 	return true;
 }
 
-static __always_inline bool arch_static_branch_jump(struct static_key *key,
-						    bool branch)
+static __always_inline bool arch_static_branch_jump(struct static_key * const key,
+						    const bool branch)
 {
 	asm_volatile_goto(
 		"1:	b		%l[l_yes]		\n\t"
Signature · 1 line
2.37.2
#2 lore raw

Re: [PATCH 1/2] arm64: jump_label: mark arguments as const to satisfy asm constraints

Quoted 21 lines · Jisheng Zhang

Inspired by x86 commit 864b435514b2("x86/jump_label: Mark arguments as const to satisfy asm constraints"), mark arch_static_branch()'s and arch_static_branch_jump()'s arguments as const to satisfy asm constraints. And Steven in [1] also pointed out that "The "i" constraint needs to be a constant."

Tested with building a simple external kernel module with "O0".

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

---
 arch/arm64/include/asm/jump_label.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/include/asm/jump_label.h +0 -0
index cea441b6aa5d..48ddc0f45d22 100644

arch/arm64/include/asm/jump_label.h line 22

Quoted 10 lines
arch/arm64/include/asm/jump_label.h +2 -2
@@ -15,8 +15,8 @@
 
 #define JUMP_LABEL_NOP_SIZE            AARCH64_INSN_SIZE
 
-static __always_inline bool arch_static_branch(struct static_key *key,
-                                              bool branch)
+static __always_inline bool arch_static_branch(struct static_key * const key,
+                                              const bool branch)
 {
        asm_volatile_goto(
                "1:     nop                                     \n\t"

Is this still necessary if we specify the constraints in a more reasonable manner:

" .quad %c0 - . + %1 \n\t"
: : "i"(key), "i"(branch) : : l_yes);

instead of the horrid hack with the char* cast and using a bool as an array index?

Quoted 14 lines
arch/arm64/include/asm/jump_label.h +2 -3
@@ -32,8 +32,8 @@ static __always_inline bool arch_static_branch(struct static_key *key,
        return true;
 }
 
-static __always_inline bool arch_static_branch_jump(struct static_key *key,
-                                                   bool branch)
+static __always_inline bool arch_static_branch_jump(struct static_key * const key,
+                                                   const bool branch)
 {
        asm_volatile_goto(
                "1:     b               %l[l_yes]               \n\t"
--

2.37.2

#3 lore raw

Re: [PATCH 1/2] arm64: jump_label: mark arguments as const to satisfy asm constraints

Quoted 36 lines · Ard Biesheuvel
Quoted 21 lines · Jisheng Zhang

Inspired by x86 commit 864b435514b2("x86/jump_label: Mark arguments as const to satisfy asm constraints"), mark arch_static_branch()'s and arch_static_branch_jump()'s arguments as const to satisfy asm constraints. And Steven in [1] also pointed out that "The "i" constraint needs to be a constant."

Tested with building a simple external kernel module with "O0".

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

---
 arch/arm64/include/asm/jump_label.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/include/asm/jump_label.h +0 -0
index cea441b6aa5d..48ddc0f45d22 100644

arch/arm64/include/asm/jump_label.h line 22

Quoted 10 lines
arch/arm64/include/asm/jump_label.h +2 -2
@@ -15,8 +15,8 @@
 
 #define JUMP_LABEL_NOP_SIZE            AARCH64_INSN_SIZE
 
-static __always_inline bool arch_static_branch(struct static_key *key,
-                                              bool branch)
+static __always_inline bool arch_static_branch(struct static_key * const key,
+                                              const bool branch)
 {
        asm_volatile_goto(
                "1:     nop                                     \n\t"

Is this still necessary if we specify the constraints in a more reasonable manner:

Quoted 2 lines

" .quad %c0 - . + %1 \n\t"
: : "i"(key), "i"(branch) : : l_yes);

Just tried this locally with the simple external kernel module, the asm operand 0 probably does not match constraints can still be reproduced with "O0".

Thanks

Quoted 21 lines

instead of the horrid hack with the char* cast and using a bool as an array index?

Quoted 14 lines
arch/arm64/include/asm/jump_label.h +2 -3
@@ -32,8 +32,8 @@ static __always_inline bool arch_static_branch(struct static_key *key,
        return true;
 }
 
-static __always_inline bool arch_static_branch_jump(struct static_key *key,
-                                                   bool branch)
+static __always_inline bool arch_static_branch_jump(struct static_key * const key,
+                                                   const bool branch)
 {
        asm_volatile_goto(
                "1:     b               %l[l_yes]               \n\t"
--

2.37.2

#4 lore raw

Re: [PATCH 1/2] arm64: jump_label: mark arguments as const to satisfy asm constraints

Quoted 37 lines · Jisheng Zhang
Quoted 36 lines · Ard Biesheuvel
Quoted 21 lines · Jisheng Zhang

Inspired by x86 commit 864b435514b2("x86/jump_label: Mark arguments as const to satisfy asm constraints"), mark arch_static_branch()'s and arch_static_branch_jump()'s arguments as const to satisfy asm constraints. And Steven in [1] also pointed out that "The "i" constraint needs to be a constant."

Tested with building a simple external kernel module with "O0".

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

---
 arch/arm64/include/asm/jump_label.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/include/asm/jump_label.h +0 -0
index cea441b6aa5d..48ddc0f45d22 100644

arch/arm64/include/asm/jump_label.h line 22

Quoted 10 lines
arch/arm64/include/asm/jump_label.h +2 -2
@@ -15,8 +15,8 @@
 
 #define JUMP_LABEL_NOP_SIZE            AARCH64_INSN_SIZE
 
-static __always_inline bool arch_static_branch(struct static_key *key,
-                                              bool branch)
+static __always_inline bool arch_static_branch(struct static_key * const key,
+                                              const bool branch)
 {
        asm_volatile_goto(
                "1:     nop                                     \n\t"

Is this still necessary if we specify the constraints in a more reasonable manner:

Quoted 6 lines
Quoted 2 lines

" .quad %c0 - . + %1 \n\t"
: : "i"(key), "i"(branch) : : l_yes);

Just tried this locally with the simple external kernel module, the asm operand 0 probably does not match constraints can still be reproduced with "O0".

I mean the "asm operand 0 probably does not match constraints" warning and related error can still be reproduced with "-O0" w/o the series. While after applying the series, I can't reproduce the build warning any more w/ and w/o the above modifications.

Quoted 24 lines

Thanks

Quoted 21 lines

instead of the horrid hack with the char* cast and using a bool as an array index?

Quoted 14 lines
arch/arm64/include/asm/jump_label.h +2 -3
@@ -32,8 +32,8 @@ static __always_inline bool arch_static_branch(struct static_key *key,
        return true;
 }
 
-static __always_inline bool arch_static_branch_jump(struct static_key *key,
-                                                   bool branch)
+static __always_inline bool arch_static_branch_jump(struct static_key * const key,
+                                                   const bool branch)
 {
        asm_volatile_goto(
                "1:     b               %l[l_yes]               \n\t"
--

2.37.2

#5 lore raw

Re: [PATCH 1/2] arm64: jump_label: mark arguments as const to satisfy asm constraints

In reply to Jisheng Zhang
Quoted 38 lines · Jisheng Zhang
Quoted 36 lines · Ard Biesheuvel
Quoted 21 lines · Jisheng Zhang

Inspired by x86 commit 864b435514b2("x86/jump_label: Mark arguments as const to satisfy asm constraints"), mark arch_static_branch()'s and arch_static_branch_jump()'s arguments as const to satisfy asm constraints. And Steven in [1] also pointed out that "The "i" constraint needs to be a constant."

Tested with building a simple external kernel module with "O0".

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

---
 arch/arm64/include/asm/jump_label.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/include/asm/jump_label.h +0 -0
index cea441b6aa5d..48ddc0f45d22 100644

arch/arm64/include/asm/jump_label.h line 22

Quoted 10 lines
arch/arm64/include/asm/jump_label.h +2 -2
@@ -15,8 +15,8 @@
 
 #define JUMP_LABEL_NOP_SIZE            AARCH64_INSN_SIZE
 
-static __always_inline bool arch_static_branch(struct static_key *key,
-                                              bool branch)
+static __always_inline bool arch_static_branch(struct static_key * const key,
+                                              const bool branch)
 {
        asm_volatile_goto(
                "1:     nop                                     \n\t"

Is this still necessary if we specify the constraints in a more reasonable manner:

Quoted 7 lines
Quoted 2 lines

" .quad %c0 - . + %1 \n\t"
: : "i"(key), "i"(branch) : : l_yes);

Just tried this locally with the simple external kernel module, the asm operand 0 probably does not match constraints can still be reproduced with "O0".

OK so the key pointer needs to be pointer-to-const, which makes sense. How about when using "Si" as the constraint for operand 0?

Quoted 22 lines
Quoted 22 lines

instead of the horrid hack with the char* cast and using a bool as an array index?

Quoted 14 lines
arch/arm64/include/asm/jump_label.h +2 -3
@@ -32,8 +32,8 @@ static __always_inline bool arch_static_branch(struct static_key *key,
        return true;
 }
 
-static __always_inline bool arch_static_branch_jump(struct static_key *key,
-                                                   bool branch)
+static __always_inline bool arch_static_branch_jump(struct static_key * const key,
+                                                   const bool branch)
 {
        asm_volatile_goto(
                "1:     b               %l[l_yes]               \n\t"
--

2.37.2

#6 lore raw

Re: [PATCH 1/2] arm64: jump_label: mark arguments as const to satisfy asm constraints

Quoted 42 lines · Ard Biesheuvel
Quoted 41 lines · Jisheng Zhang
Quoted 38 lines · Ard Biesheuvel
Quoted 21 lines · Jisheng Zhang

Inspired by x86 commit 864b435514b2("x86/jump_label: Mark arguments as const to satisfy asm constraints"), mark arch_static_branch()'s and arch_static_branch_jump()'s arguments as const to satisfy asm constraints. And Steven in [1] also pointed out that "The "i" constraint needs to be a constant."

Tested with building a simple external kernel module with "O0".

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

---
 arch/arm64/include/asm/jump_label.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/include/asm/jump_label.h +0 -0
index cea441b6aa5d..48ddc0f45d22 100644

arch/arm64/include/asm/jump_label.h line 22

Quoted 10 lines
arch/arm64/include/asm/jump_label.h +2 -2
@@ -15,8 +15,8 @@
 
 #define JUMP_LABEL_NOP_SIZE            AARCH64_INSN_SIZE
 
-static __always_inline bool arch_static_branch(struct static_key *key,
-                                              bool branch)
+static __always_inline bool arch_static_branch(struct static_key * const key,
+                                              const bool branch)
 {
        asm_volatile_goto(
                "1:     nop                                     \n\t"

Is this still necessary if we specify the constraints in a more reasonable manner:

" .quad %c0 - . + %1 \n\t"
: : "i"(key), "i"(branch) : : l_yes);

Quoted 7 lines
Quoted 4 lines

Just tried this locally with the simple external kernel module, the asm operand 0 probably does not match constraints can still be reproduced with "O0".

OK so the key pointer needs to be pointer-to-const, which makes sense. How about when using "Si" as the constraint for operand 0?

"Si" constraint doesn't help either.

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines

instead of the horrid hack with the char* cast and using a bool as an array index?

Quoted 14 lines
arch/arm64/include/asm/jump_label.h +2 -3
@@ -32,8 +32,8 @@ static __always_inline bool arch_static_branch(struct static_key *key,
        return true;
 }
 
-static __always_inline bool arch_static_branch_jump(struct static_key *key,
-                                                   bool branch)
+static __always_inline bool arch_static_branch_jump(struct static_key * const key,
+                                                   const bool branch)
 {
        asm_volatile_goto(
                "1:     b               %l[l_yes]               \n\t"
--

2.37.2

reviewer
#7 lore raw

Re: [PATCH 1/2] arm64: jump_label: mark arguments as const to satisfy asm constraints

In reply to Jisheng Zhang
Quoted 5 lines · Jisheng Zhang

Inspired by x86 commit 864b435514b2("x86/jump_label: Mark arguments as const to satisfy asm constraints"), mark arch_static_branch()'s and arch_static_branch_jump()'s arguments as const to satisfy asm constraints. And Steven in [1] also pointed out that "The "i" constraint needs to be a constant."

It needs to be a *compile-time constant*, but const on a function argument only ensures that the function can't modify the argument, not that it's a constant in the caller.

I think this is a quirk of the optimizer rather than anything else.

Quoted 1 line

Tested with building a simple external kernel module with "O0".

Is building with -O0 supported? I thought we required using -O2 or above for a bunch of code that requires constant propagation, etc.

I don't really have a problem with making this const, but I don't particularly want to try to "fix" all the other code that depends on constant propagation to assemble, and I'm worried this is the canary in the coal mine.

Thanks,
Mark.

Quoted 37 lines

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

---
 arch/arm64/include/asm/jump_label.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/include/asm/jump_label.h +4 -4
index cea441b6aa5d..48ddc0f45d22 100644
@@ -15,8 +15,8 @@
 
 #define JUMP_LABEL_NOP_SIZE		AARCH64_INSN_SIZE
 
-static __always_inline bool arch_static_branch(struct static_key *key,
-					       bool branch)
+static __always_inline bool arch_static_branch(struct static_key * const key,
+					       const bool branch)
 {
 	asm_volatile_goto(
 		"1:	nop					\n\t"
@@ -32,8 +32,8 @@ static __always_inline bool arch_static_branch(struct static_key *key,
 	return true;
 }
 
-static __always_inline bool arch_static_branch_jump(struct static_key *key,
-						    bool branch)
+static __always_inline bool arch_static_branch_jump(struct static_key * const key,
+						    const bool branch)
 {
 	asm_volatile_goto(
 		"1:	b		%l[l_yes]		\n\t"
Signature · 1 line
2.37.2
#8 lore raw

Re: [PATCH 1/2] arm64: jump_label: mark arguments as const to satisfy asm constraints

Quoted 9 lines · Mark Rutland
Quoted 5 lines · Jisheng Zhang

Inspired by x86 commit 864b435514b2("x86/jump_label: Mark arguments as const to satisfy asm constraints"), mark arch_static_branch()'s and arch_static_branch_jump()'s arguments as const to satisfy asm constraints. And Steven in [1] also pointed out that "The "i" constraint needs to be a constant."

It needs to be a *compile-time constant*, but const on a function argument only ensures that the function can't modify the argument, not that it's a

compile-time constant is a subset of const.

Quoted 3 lines

constant in the caller.

I think this is a quirk of the optimizer rather than anything else.

I dunno compiler internals, just tried as commit 864b435514b2 suggested the issue did disappear.

PS: I agree with you about this is a quirk or workaround.

Quoted 5 lines
Quoted 2 lines

Tested with building a simple external kernel module with "O0".

Is building with -O0 supported? I thought we required using -O2 or above for a bunch of code that requires constant propagation, etc.

Per the information of Jason's reply in [1]: the reason tring O0/O1 is "to play around with GCC's new static analyzer options".

While the reason I constify the arguments is that: in riscv world, even the "-Os" can also reproduce the warnings and errors[2]. Grepping source found arm64 also shares the same style, so these two patches.

[2]https://lore.kernel.org/linux-riscv/[email protected]/

Quoted 4 lines

I don't really have a problem with making this const, but I don't particularly want to try to "fix" all the other code that depends on constant propagation to assemble, and I'm worried this is the canary in the coal mine.

IMHO, it's a good idea to constify if the arguments can't be modified.

Quoted 41 lines

Thanks,
Mark.

Quoted 37 lines

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

---
 arch/arm64/include/asm/jump_label.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/include/asm/jump_label.h +4 -4
index cea441b6aa5d..48ddc0f45d22 100644
@@ -15,8 +15,8 @@
 
 #define JUMP_LABEL_NOP_SIZE		AARCH64_INSN_SIZE
 
-static __always_inline bool arch_static_branch(struct static_key *key,
-					       bool branch)
+static __always_inline bool arch_static_branch(struct static_key * const key,
+					       const bool branch)
 {
 	asm_volatile_goto(
 		"1:	nop					\n\t"
@@ -32,8 +32,8 @@ static __always_inline bool arch_static_branch(struct static_key *key,
 	return true;
 }
 
-static __always_inline bool arch_static_branch_jump(struct static_key *key,
-						    bool branch)
+static __always_inline bool arch_static_branch_jump(struct static_key * const key,
+						    const bool branch)
 {
 	asm_volatile_goto(
 		"1:	b		%l[l_yes]		\n\t"
Signature · 1 line
2.37.2
#9 lore raw

PATCH 2/2 arm64: alternative: constify alternative_has_feature_* argument

In reply to Jisheng Zhang

Inspired by x86 commit 864b435514b2("x86/jump_label: Mark arguments as const to satisfy asm constraints"), constify alternative_has_feature_* argument to satisfy asm constraints. And Steven in [1] also pointed out that "The "i" constraint needs to be a constant."

Tested with building a simple external kernel module with "O0".

Before the patch, got similar gcc warnings and errors as below:

In file included from <command-line>:
In function ‘alternative_has_feature_likely’,
inlined from ‘system_capabilities_finalized’ at
arch/arm64/include/asm/cpufeature.h:440:9,
inlined from ‘arm64_preempt_schedule_irq’ at
arch/arm64/kernel/entry-common.c:264:6:
include/linux/compiler_types.h:285:33: warning:
‘asm’ operand 0 probably does not match constraints
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
arch/arm64/include/asm/alternative-macros.h:232:9:
note: in expansion of macro ‘asm_volatile_goto’

  232 |         asm_volatile_goto(
      |         ^~~~~~~~~~~~~~~~~

include/linux/compiler_types.h:285:33: error:
impossible constraint in ‘asm’
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
arch/arm64/include/asm/alternative-macros.h:232:9:
note: in expansion of macro ‘asm_volatile_goto’

  232 |         asm_volatile_goto(
      |         ^~~~~~~~~~~~~~~~~

After the patch, the simple external test kernel module is built fine with "-O0".

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

---
 arch/arm64/include/asm/alternative-macros.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/include/asm/alternative-macros.h +2 -2
index 3622e9f4fb44..bdf1f6bcd010 100644
@@ -224,7 +224,7 @@ alternative_endif
 #include <linux/types.h>
 
 static __always_inline bool
-alternative_has_feature_likely(unsigned long feature)
+alternative_has_feature_likely(const unsigned long feature)
 {
 	compiletime_assert(feature < ARM64_NCAPS,
 			   "feature must be < ARM64_NCAPS");
@@ -242,7 +242,7 @@ alternative_has_feature_likely(unsigned long feature)
 }
 
 static __always_inline bool
-alternative_has_feature_unlikely(unsigned long feature)
+alternative_has_feature_unlikely(const unsigned long feature)
 {
 	compiletime_assert(feature < ARM64_NCAPS,
 			   "feature must be < ARM64_NCAPS");
Signature · 1 line
2.37.2
maintainer Applied
#10 lore raw
In reply to Jisheng Zhang
Quoted 8 lines · Jisheng Zhang

Constify arguments of arch_static_branch(), arch_static_branch_jump() and alternative_has_feature_* to satisfy asm constraints.

Jisheng Zhang (2):
arm64: jump_label: mark arguments as const to satisfy asm constraints
arm64: alternative: constify alternative_has_feature_* argument

[...]

Applied to arm64 (for-next/asm-const), thanks!

[1/2] arm64: jump_label: mark arguments as const to satisfy asm constraints

      https://git.kernel.org/arm64/c/efbc95787cbe
[2/2] arm64: alternative: constify alternative_has_feature_* argument
      https://git.kernel.org/arm64/c/b9024f87d63f

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev