Skip to contentSkip to conversation

uprobe: Do not use UPROBE_SWBP_INSN as static initializer

PATCH In review Jiri Olsa 7 participants 21 messages Jun 18, 2024 – Jul 1, 2024

Subsystems (5)
BPF perf events Tracing LoongArch +1 more subsystems
lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

PATCH 1/1 uprobe: Do not use UPROBE_SWBP_INSN as static initializer

Nathan reported compilation fail for loongarch arch:

kernel/events/uprobes.c: In function 'arch_uprobe_trampoline':
arch/loongarch/include/asm/uprobes.h:12:33: error: initializer element is not constant

     12 | #define UPROBE_SWBP_INSN        larch_insn_gen_break(BRK_UPROBE_BP)
        |                                 ^~~~~~~~~~~~~~~~~~~~

kernel/events/uprobes.c:1479:39: note: in expansion of macro 'UPROBE_SWBP_INSN'
1479 | static uprobe_opcode_t insn = UPROBE_SWBP_INSN;

Loongarch defines UPROBE_SWBP_INSN as function call, so we can't use it to initialize static variable.

Cc Oleg Nesterov <[email protected]>
Fixes ff474a78cef5 ("uprobe: Add uretprobe syscall to speed up return probe")
---
 kernel/events/uprobes.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/events/uprobes.c +2 -1
index 2816e65729ac..6986bd993702 100644
@@ -1476,8 +1476,9 @@ static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)
 
 void * __weak arch_uprobe_trampoline(unsigned long *psize)
 {
-	static uprobe_opcode_t insn = UPROBE_SWBP_INSN;
+	static uprobe_opcode_t insn;
 
+	insn = insn ?: UPROBE_SWBP_INSN;
 	*psize = UPROBE_SWBP_INSN_SIZE;
 	return &insn;
 }
Signature · 1 line
2.45.1
#1 lore raw
Quoted 13 lines · Jiri Olsa

Nathan reported compilation fail for loongarch arch:

kernel/events/uprobes.c: In function 'arch_uprobe_trampoline':
arch/loongarch/include/asm/uprobes.h:12:33: error: initializer element is not constant

     12 | #define UPROBE_SWBP_INSN        larch_insn_gen_break(BRK_UPROBE_BP)
        |                                 ^~~~~~~~~~~~~~~~~~~~

kernel/events/uprobes.c:1479:39: note: in expansion of macro 'UPROBE_SWBP_INSN'
1479 | static uprobe_opcode_t insn = UPROBE_SWBP_INSN;

Loongarch defines UPROBE_SWBP_INSN as function call, so we can't use it to initialize static variable.

Quoted 8 lines
Cc Oleg Nesterov <[email protected]>
Fixes ff474a78cef5 ("uprobe: Add uretprobe syscall to speed up return probe")
---
 kernel/events/uprobes.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Can we instead ask loongarch folks to rewrite it to be a constant? Having this as a function call is both an inconvenience and potential performance problem (a minor one, but still). I would imagine it's not hard to hard-code an instruction as a constant here.

Quoted 18 lines
kernel/events/uprobes.c +2 -2
index 2816e65729ac..6986bd993702 100644
@@ -1476,8 +1476,9 @@ static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)
 
 void * __weak arch_uprobe_trampoline(unsigned long *psize)
 {
-       static uprobe_opcode_t insn = UPROBE_SWBP_INSN;
+       static uprobe_opcode_t insn;
 
+       insn = insn ?: UPROBE_SWBP_INSN;
        *psize = UPROBE_SWBP_INSN_SIZE;
        return &insn;
 }
--

2.45.1

maintainer
#2 lore raw
Quoted 5 lines · Andrii Nakryiko

Can we instead ask loongarch folks to rewrite it to be a constant? Having this as a function call is both an inconvenience and potential performance problem (a minor one, but still). I would imagine it's not hard to hard-code an instruction as a constant here.

I was going to ask the same question when I saw the bug report ;) The same for other users of larch_insn_gen_break().

But I can't understand what does it do, it calls emit_break() and git grep -w emit_break finds nothing.

Oleg.

#3 lore raw
Quoted 8 lines · Oleg Nesterov
Quoted 5 lines · Andrii Nakryiko

Can we instead ask loongarch folks to rewrite it to be a constant? Having this as a function call is both an inconvenience and potential performance problem (a minor one, but still). I would imagine it's not hard to hard-code an instruction as a constant here.

Quoted 6 lines

I was going to ask the same question when I saw the bug report ;) The same for other users of larch_insn_gen_break().

But I can't understand what does it do, it calls emit_break() and git grep -w emit_break finds nothing.

It's DEF_EMIT_REG0I15_FORMAT(break, break_op) in
arch/loongarch/include/asm/inst.h

A bunch of macro magic, but in the end it produces some constant value, of course.

Quoted 2 lines

Oleg.

maintainer
#4 lore raw
Quoted 7 lines · Andrii Nakryiko
Quoted 4 lines · Oleg Nesterov

But I can't understand what does it do, it calls emit_break() and git grep -w emit_break finds nothing.

Quoted 5 lines

It's DEF_EMIT_REG0I15_FORMAT(break, break_op) in
arch/loongarch/include/asm/inst.h

A bunch of macro magic, but in the end it produces some constant value, of course.

I see, thanks!

Then perhaps something like below?

Oleg.

arch/loongarch/include/asm/uprobes.h +1 -1
@@ -9,7 +9,7 @@ typedef u32 uprobe_opcode_t;
 #define MAX_UINSN_BYTES		8
 #define UPROBE_XOL_SLOT_BYTES	MAX_UINSN_BYTES
 
-#define UPROBE_SWBP_INSN	larch_insn_gen_break(BRK_UPROBE_BP)
+#define UPROBE_SWBP_INSN	(uprobe_opcode_t)(BRK_UPROBE_BP | (break_op << 15))
 #define UPROBE_SWBP_INSN_SIZE	LOONGARCH_INSN_SIZE
 
 #define UPROBE_XOLBP_INSN	larch_insn_gen_break(BRK_UPROBE_XOLBP)
arch/loongarch/kernel/uprobes.c +7 -0
@@ -7,6 +7,13 @@
 
 #define UPROBE_TRAP_NR	UINT_MAX
 
+static __init int __ck_insn(void)
+{
+	BUG_ON(UPROBE_SWBP_INSN != larch_insn_gen_break(BRK_UPROBE_BP));
+	return 0;
+}
+late_initcall(__ck_insn);
+
 int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe,
 			     struct mm_struct *mm, unsigned long addr)
 {
#5 lore raw
Quoted 11 lines · Oleg Nesterov
Quoted 10 lines · Andrii Nakryiko
Quoted 4 lines · Oleg Nesterov

But I can't understand what does it do, it calls emit_break() and git grep -w emit_break finds nothing.

It's DEF_EMIT_REG0I15_FORMAT(break, break_op) in
arch/loongarch/include/asm/inst.h

Quoted 6 lines
Quoted 2 lines

A bunch of macro magic, but in the end it produces some constant value, of course.

I see, thanks!

Then perhaps something like below?

lgtm, added loong arch list/folks

for context:
https://lore.kernel.org/bpf/20240614174822.GA1185149@thelio-3990X/

thanks,
jirka

Quoted 32 lines

Oleg.

arch/loongarch/include/asm/uprobes.h +1 -1
@@ -9,7 +9,7 @@ typedef u32 uprobe_opcode_t;
 #define MAX_UINSN_BYTES		8
 #define UPROBE_XOL_SLOT_BYTES	MAX_UINSN_BYTES
 
-#define UPROBE_SWBP_INSN	larch_insn_gen_break(BRK_UPROBE_BP)
+#define UPROBE_SWBP_INSN	(uprobe_opcode_t)(BRK_UPROBE_BP | (break_op << 15))
 #define UPROBE_SWBP_INSN_SIZE	LOONGARCH_INSN_SIZE
 
 #define UPROBE_XOLBP_INSN	larch_insn_gen_break(BRK_UPROBE_XOLBP)
arch/loongarch/kernel/uprobes.c +7 -0
@@ -7,6 +7,13 @@
 
 #define UPROBE_TRAP_NR	UINT_MAX
 
+static __init int __ck_insn(void)
+{
+	BUG_ON(UPROBE_SWBP_INSN != larch_insn_gen_break(BRK_UPROBE_BP));
+	return 0;
+}
+late_initcall(__ck_insn);
+
 int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe,
 			     struct mm_struct *mm, unsigned long addr)
 {
#6 lore raw
Quoted 12 lines · Jiri Olsa
Quoted 11 lines · Oleg Nesterov
Quoted 10 lines · Andrii Nakryiko
Quoted 4 lines · Oleg Nesterov

But I can't understand what does it do, it calls emit_break() and git grep -w emit_break finds nothing.

It's DEF_EMIT_REG0I15_FORMAT(break, break_op) in
arch/loongarch/include/asm/inst.h

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

A bunch of macro magic, but in the end it produces some constant value, of course.

I see, thanks!

Then perhaps something like below?

lgtm, added loong arch list/folks

ping

Oleg, do you want to send formal patch?

thanks,
jirka

Quoted 39 lines

for context:
https://lore.kernel.org/bpf/20240614174822.GA1185149@thelio-3990X/

thanks,
jirka

Quoted 32 lines

Oleg.

arch/loongarch/include/asm/uprobes.h +1 -1
@@ -9,7 +9,7 @@ typedef u32 uprobe_opcode_t;
 #define MAX_UINSN_BYTES		8
 #define UPROBE_XOL_SLOT_BYTES	MAX_UINSN_BYTES
 
-#define UPROBE_SWBP_INSN	larch_insn_gen_break(BRK_UPROBE_BP)
+#define UPROBE_SWBP_INSN	(uprobe_opcode_t)(BRK_UPROBE_BP | (break_op << 15))
 #define UPROBE_SWBP_INSN_SIZE	LOONGARCH_INSN_SIZE
 
 #define UPROBE_XOLBP_INSN	larch_insn_gen_break(BRK_UPROBE_XOLBP)
arch/loongarch/kernel/uprobes.c +7 -0
@@ -7,6 +7,13 @@
 
 #define UPROBE_TRAP_NR	UINT_MAX
 
+static __init int __ck_insn(void)
+{
+	BUG_ON(UPROBE_SWBP_INSN != larch_insn_gen_break(BRK_UPROBE_BP));
+	return 0;
+}
+late_initcall(__ck_insn);
+
 int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe,
 			     struct mm_struct *mm, unsigned long addr)
 {
#7 lore raw
Quoted 20 lines · On Thu, 27 Jun 2024 15:44:16 +0200 Jiri Olsa <[email protected]> wrote:
Quoted 19 lines · Jiri Olsa
Quoted 17 lines · Oleg Nesterov
Quoted 7 lines · Andrii Nakryiko
Quoted 4 lines · Oleg Nesterov

But I can't understand what does it do, it calls emit_break() and git grep -w emit_break finds nothing.

Quoted 5 lines

It's DEF_EMIT_REG0I15_FORMAT(break, break_op) in
arch/loongarch/include/asm/inst.h

A bunch of macro magic, but in the end it produces some constant value, of course.

I see, thanks!

Then perhaps something like below?

Quoted 8 lines
Quoted 1 line

lgtm, added loong arch list/folks

ping

Oleg, do you want to send formal patch?

thanks,
jirka

Yes, can you send v2 patch?

Thank you,

Quoted 40 lines
Quoted 40 lines

for context:
https://lore.kernel.org/bpf/20240614174822.GA1185149@thelio-3990X/

thanks,
jirka

Quoted 32 lines

Oleg.

arch/loongarch/include/asm/uprobes.h +1 -1
@@ -9,7 +9,7 @@ typedef u32 uprobe_opcode_t;
 #define MAX_UINSN_BYTES		8
 #define UPROBE_XOL_SLOT_BYTES	MAX_UINSN_BYTES
 
-#define UPROBE_SWBP_INSN	larch_insn_gen_break(BRK_UPROBE_BP)
+#define UPROBE_SWBP_INSN	(uprobe_opcode_t)(BRK_UPROBE_BP | (break_op << 15))
 #define UPROBE_SWBP_INSN_SIZE	LOONGARCH_INSN_SIZE
 
 #define UPROBE_XOLBP_INSN	larch_insn_gen_break(BRK_UPROBE_XOLBP)
arch/loongarch/kernel/uprobes.c +7 -0
@@ -7,6 +7,13 @@
 
 #define UPROBE_TRAP_NR	UINT_MAX
 
+static __init int __ck_insn(void)
+{
+	BUG_ON(UPROBE_SWBP_INSN != larch_insn_gen_break(BRK_UPROBE_BP));
+	return 0;
+}
+late_initcall(__ck_insn);
+
 int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe,
 			     struct mm_struct *mm, unsigned long addr)
 {
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
maintainer
#8 lore raw
Quoted 10 lines · Masami Hiramatsu
Quoted 4 lines · On Thu, 27 Jun 2024 15:44:16 +0200 Jiri Olsa <[email protected]> wrote:

Oleg, do you want to send formal patch?

thanks,
jirka

Yes, can you send v2 patch?

I was waiting for the comments from loongarch maintainers...

OK, will do today, but the patch won't be even compile tested.

Oleg.

maintainer
#9 lore raw

PATCH 1/1 LoongArch: uprobes: make UPROBE_SWBP_INSN/UPROBE_XOLBP_INSN constant

In reply to Jiri Olsa

LoongArch defines UPROBE_SWBP_INSN as a function call and this breaks arch_uprobe_trampoline() which uses it to initialize a static variable.

Fixes ff474a78cef5 ("uprobe: Add uretprobe syscall to speed up return probe")
---
 arch/loongarch/include/asm/uprobes.h | 6 ++++--
 arch/loongarch/kernel/uprobes.c      | 8 ++++++++
 2 files changed, 12 insertions(+), 2 deletions(-)
arch/loongarch/include/asm/uprobes.h +4 -2
index c8f59983f702..18221eb9a8b0 100644
@@ -6,13 +6,15 @@
 
 typedef u32 uprobe_opcode_t;
 
+#define __emit_break(imm)	(uprobe_opcode_t)((imm) | (break_op << 15))
+
 #define MAX_UINSN_BYTES		8
 #define UPROBE_XOL_SLOT_BYTES	MAX_UINSN_BYTES
 
-#define UPROBE_SWBP_INSN	larch_insn_gen_break(BRK_UPROBE_BP)
+#define UPROBE_SWBP_INSN	__emit_break(BRK_UPROBE_BP)
 #define UPROBE_SWBP_INSN_SIZE	LOONGARCH_INSN_SIZE
 
-#define UPROBE_XOLBP_INSN	larch_insn_gen_break(BRK_UPROBE_XOLBP)
+#define UPROBE_XOLBP_INSN	__emit_break(BRK_UPROBE_XOLBP)
 
 struct arch_uprobe {
 	unsigned long	resume_era;
arch/loongarch/kernel/uprobes.c +8 -0
index 87abc7137b73..90462d94c28f 100644
@@ -7,6 +7,14 @@
 
 #define UPROBE_TRAP_NR	UINT_MAX
 
+static __init int check_emit_break(void)
+{
+	BUG_ON(UPROBE_SWBP_INSN  != larch_insn_gen_break(BRK_UPROBE_BP));
+	BUG_ON(UPROBE_XOLBP_INSN != larch_insn_gen_break(BRK_UPROBE_XOLBP));
+	return 0;
+}
+arch_initcall(check_emit_break);
+
 int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe,
 			     struct mm_struct *mm, unsigned long addr)
 {
Signature · 1 line
2.25.1.362.g51ebf55
Acked-by on v1 1/1
#10 lore raw

Re: [PATCH] LoongArch: uprobes: make UPROBE_SWBP_INSN/UPROBE_XOLBP_INSN constant

Quoted 9 lines · Oleg Nesterov

LoongArch defines UPROBE_SWBP_INSN as a function call and this breaks arch_uprobe_trampoline() which uses it to initialize a static variable.

Fixes ff474a78cef5 ("uprobe: Add uretprobe syscall to speed up return probe")
Quoted 5 lines
---
 arch/loongarch/include/asm/uprobes.h | 6 ++++--
 arch/loongarch/kernel/uprobes.c      | 8 ++++++++
 2 files changed, 12 insertions(+), 2 deletions(-)

LGTM.

Quoted 10 lines
arch/loongarch/include/asm/uprobes.h +2 -0
index c8f59983f702..18221eb9a8b0 100644
@@ -6,13 +6,15 @@
 
 typedef u32 uprobe_opcode_t;
 
+#define __emit_break(imm)      (uprobe_opcode_t)((imm) | (break_op << 15))
+

arch/loongarch/include/asm/uprobes.h line 18

Quoted 10 lines
arch/loongarch/include/asm/uprobes.h +2 -2
 #define MAX_UINSN_BYTES                8
 #define UPROBE_XOL_SLOT_BYTES  MAX_UINSN_BYTES
 
-#define UPROBE_SWBP_INSN       larch_insn_gen_break(BRK_UPROBE_BP)
+#define UPROBE_SWBP_INSN       __emit_break(BRK_UPROBE_BP)
 #define UPROBE_SWBP_INSN_SIZE  LOONGARCH_INSN_SIZE
 
-#define UPROBE_XOLBP_INSN      larch_insn_gen_break(BRK_UPROBE_XOLBP)
+#define UPROBE_XOLBP_INSN      __emit_break(BRK_UPROBE_XOLBP)
 

this looks correct (but based on pure code inspection)

Quoted 8 lines
arch/loongarch/include/asm/uprobes.h +0 -0
 struct arch_uprobe {
        unsigned long   resume_era;
arch/loongarch/kernel/uprobes.c +0 -0
index 87abc7137b73..90462d94c28f 100644
@@ -7,6 +7,14 @@
 

arch/loongarch/kernel/uprobes.c line 17

Quoted 10 lines
arch/loongarch/kernel/uprobes.c +8 -0
 #define UPROBE_TRAP_NR UINT_MAX
 
+static __init int check_emit_break(void)
+{
+       BUG_ON(UPROBE_SWBP_INSN  != larch_insn_gen_break(BRK_UPROBE_BP));
+       BUG_ON(UPROBE_XOLBP_INSN != larch_insn_gen_break(BRK_UPROBE_XOLBP));
+       return 0;
+}
+arch_initcall(check_emit_break);
+

I wouldn't even bother with this, but whatever.

Quoted 7 lines
arch/loongarch/kernel/uprobes.c +0 -1
 int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe,
                             struct mm_struct *mm, unsigned long addr)
 {
--

2.25.1.362.g51ebf55

maintainer
#11 lore raw

Re: [PATCH] LoongArch: uprobes: make UPROBE_SWBP_INSN/UPROBE_XOLBP_INSN constant

Quoted 2 lines · Andrii Nakryiko

Thanks!

Quoted 6 lines
Quoted 6 lines
arch/loongarch/kernel/uprobes.c +0 -0
@@ -7,6 +7,14 @@
 
 #define UPROBE_TRAP_NR UINT_MAX
 
Quoted 10 lines

arch/loongarch/kernel/uprobes.c line 17

Quoted 8 lines
arch/loongarch/kernel/uprobes.c +8 -0
+static __init int check_emit_break(void)
+{
+       BUG_ON(UPROBE_SWBP_INSN  != larch_insn_gen_break(BRK_UPROBE_BP));
+       BUG_ON(UPROBE_XOLBP_INSN != larch_insn_gen_break(BRK_UPROBE_XOLBP));
+       return 0;
+}
+arch_initcall(check_emit_break);
+

I wouldn't even bother with this, but whatever.

Agreed, this looks a bit ugly. I did this only because I can not test this (hopefully trivial) patch and the maintainers didn't reply.

If LoongArch boots at least once with this change, this run-time check can be removed.

And just in case... I didn't dare to make a more "generic" change, but perhaps KPROBE_BP_INSN and KPROBE_SSTEPBP_INSN should be redefined the same way for micro-optimization. In this case __emit_break() should be probably moved into arch/loongarch/include/asm/inst.h.

Oleg.

#12 lore raw

Re: [PATCH] LoongArch: uprobes: make UPROBE_SWBP_INSN/UPROBE_XOLBP_INSN constant

Quoted 26 lines · On Thu, 27 Jun 2024 19:38:06 +0200 Oleg Nesterov <[email protected]> wrote:
Quoted 2 lines · Andrii Nakryiko

Thanks!

Quoted 6 lines
Quoted 6 lines
arch/loongarch/kernel/uprobes.c +0 -0
@@ -7,6 +7,14 @@
 
 #define UPROBE_TRAP_NR UINT_MAX
 
Quoted 10 lines

arch/loongarch/kernel/uprobes.c line 17

Quoted 8 lines
arch/loongarch/kernel/uprobes.c +8 -0
+static __init int check_emit_break(void)
+{
+       BUG_ON(UPROBE_SWBP_INSN  != larch_insn_gen_break(BRK_UPROBE_BP));
+       BUG_ON(UPROBE_XOLBP_INSN != larch_insn_gen_break(BRK_UPROBE_XOLBP));
+       return 0;
+}
+arch_initcall(check_emit_break);
+

I wouldn't even bother with this, but whatever.

Agreed, this looks a bit ugly. I did this only because I can not test this (hopefully trivial) patch and the maintainers didn't reply.

Quoted 7 lines

If LoongArch boots at least once with this change, this run-time check can be removed.

And just in case... I didn't dare to make a more "generic" change, but perhaps KPROBE_BP_INSN and KPROBE_SSTEPBP_INSN should be redefined the same way for micro-optimization. In this case __emit_break() should be probably moved into arch/loongarch/include/asm/inst.h.

That idea sounds good to me too. If it is good to loongarch maintainers, (e.g. breakpoint instruction is stable), it is better to define in asm/insn.h.

Thank you,

Quoted 4 lines

Oleg.

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#13 lore raw

Re: [PATCH] LoongArch: uprobes: make UPROBE_SWBP_INSN/UPROBE_XOLBP_INSN constant

In reply to Oleg Nesterov

On Thu, 27 Jun 2024 19:38:06 +0200
Oleg Nesterov <[email protected]> wrote:

...

Quoted 7 lines
Quoted 4 lines

arch/loongarch/kernel/uprobes.c line 17

Quoted 2 lines
arch/loongarch/kernel/uprobes.c +2 -0
+arch_initcall(check_emit_break);
+

I wouldn't even bother with this, but whatever.

Agreed, this looks a bit ugly. I did this only because I can not test this (hopefully trivial) patch and the maintainers didn't reply.

The LoongArch maintainer Huacai told me offline to reply this thread today.

Quoted 2 lines

If LoongArch boots at least once with this change, this run-time check can be removed.

I will test it next Monday.

Quoted 4 lines

And just in case... I didn't dare to make a more "generic" change, but perhaps KPROBE_BP_INSN and KPROBE_SSTEPBP_INSN should be redefined the same way for micro-optimization. In this case __emit_break() should be probably moved into arch/loongarch/include/asm/inst.h.

Yeah. I think so too.

Thanks,
Tiezhu

maintainer
#14 lore raw

Re: [PATCH] LoongArch: uprobes: make UPROBE_SWBP_INSN/UPROBE_XOLBP_INSN constant

Quoted 14 lines · Tiezhu Yang

On Thu, 27 Jun 2024 19:38:06 +0200
Oleg Nesterov <[email protected]> wrote:

...

Quoted 7 lines
Quoted 4 lines

arch/loongarch/kernel/uprobes.c line 17

Quoted 2 lines
arch/loongarch/kernel/uprobes.c +2 -0
+arch_initcall(check_emit_break);
+

I wouldn't even bother with this, but whatever.

Agreed, this looks a bit ugly. I did this only because I can not test this (hopefully trivial) patch and the maintainers didn't reply.

Quoted 6 lines

The LoongArch maintainer Huacai told me offline to reply this thread today.

Quoted 2 lines

If LoongArch boots at least once with this change, this run-time check can be removed.

I will test it next Monday.

Thanks!

Quoted 6 lines
Quoted 4 lines

And just in case... I didn't dare to make a more "generic" change, but perhaps KPROBE_BP_INSN and KPROBE_SSTEPBP_INSN should be redefined the same way for micro-optimization. In this case __emit_break() should be probably moved into arch/loongarch/include/asm/inst.h.

Yeah. I think so too.

OK... should I send v2? Or another change which does this on top of this patch? Or will you do it yourself?

Quoted 4 lines

Thanks,
Tiezhu

maintainer
#15 lore raw

Re: [PATCH] LoongArch: uprobes: make UPROBE_SWBP_INSN/UPROBE_XOLBP_INSN constant

Quoted 30 lines · Oleg Nesterov
Quoted 14 lines · Tiezhu Yang

On Thu, 27 Jun 2024 19:38:06 +0200
Oleg Nesterov <[email protected]> wrote:

...

Quoted 7 lines
Quoted 4 lines

arch/loongarch/kernel/uprobes.c line 17

Quoted 2 lines
arch/loongarch/kernel/uprobes.c +2 -0
+arch_initcall(check_emit_break);
+

I wouldn't even bother with this, but whatever.

Agreed, this looks a bit ugly. I did this only because I can not test this (hopefully trivial) patch and the maintainers didn't reply.

Quoted 6 lines

The LoongArch maintainer Huacai told me offline to reply this thread today.

Quoted 2 lines

If LoongArch boots at least once with this change, this run-time check can be removed.

I will test it next Monday.

Thanks!

Quoted 5 lines
Quoted 4 lines

And just in case... I didn't dare to make a more "generic" change, but perhaps KPROBE_BP_INSN and KPROBE_SSTEPBP_INSN should be redefined the same way for micro-optimization. In this case __emit_break() should be probably moved into arch/loongarch/include/asm/inst.h.

Quoted 4 lines
Quoted 1 line

Yeah. I think so too.

OK... should I send v2? Or another change which does this on top of this patch? Or will you do it yourself?

I prefer V2.

Huacai

Quoted 6 lines
Quoted 5 lines

Thanks,
Tiezhu

maintainer
#16 lore raw

PATCH 1/1 LoongArch: make the users of larch_insn_gen_break() constant

LoongArch defines UPROBE_SWBP_INSN as a function call and this breaks arch_uprobe_trampoline() which uses it to initialize a static variable.

Add the new "__builtin_constant_p" helper, __emit_break(), and redefine the current users of larch_insn_gen_break() to use it.

The patch adds check_emit_break() into kprobes.c and uprobes.c to test this change. They can be removed if LoongArch boots at least once, but otoh these 2 __init functions will be discarded by free_initmem().

Fixes ff474a78cef5 ("uprobe: Add uretprobe syscall to speed up return probe")
---
 arch/loongarch/include/asm/inst.h    |  3 +++
 arch/loongarch/include/asm/uprobes.h |  4 ++--
 arch/loongarch/kernel/kprobes.c      | 12 ++++++++++--
 arch/loongarch/kernel/uprobes.c      |  8 ++++++++
 4 files changed, 23 insertions(+), 4 deletions(-)
arch/loongarch/include/asm/inst.h +3 -0
index c3993fd88aba..944482063f14 100644
@@ -532,6 +532,9 @@ static inline void emit_##NAME(union loongarch_instruction *insn,	\
 
 DEF_EMIT_REG0I15_FORMAT(break, break_op)
 
+/* like emit_break(imm) but returns a constant expression */
+#define __emit_break(imm)	((u32)((imm) | (break_op << 15)))
+
 #define DEF_EMIT_REG0I26_FORMAT(NAME, OP)				\
 static inline void emit_##NAME(union loongarch_instruction *insn,	\
 			       int offset)				\
arch/loongarch/include/asm/uprobes.h +2 -2
index c8f59983f702..99a0d198927f 100644
@@ -9,10 +9,10 @@ typedef u32 uprobe_opcode_t;
 #define MAX_UINSN_BYTES		8
 #define UPROBE_XOL_SLOT_BYTES	MAX_UINSN_BYTES
 
-#define UPROBE_SWBP_INSN	larch_insn_gen_break(BRK_UPROBE_BP)
+#define UPROBE_SWBP_INSN	__emit_break(BRK_UPROBE_BP)
 #define UPROBE_SWBP_INSN_SIZE	LOONGARCH_INSN_SIZE
 
-#define UPROBE_XOLBP_INSN	larch_insn_gen_break(BRK_UPROBE_XOLBP)
+#define UPROBE_XOLBP_INSN	__emit_break(BRK_UPROBE_XOLBP)
 
 struct arch_uprobe {
 	unsigned long	resume_era;
arch/loongarch/kernel/kprobes.c +10 -2
index 17b040bd6067..78cfaac52748 100644
@@ -4,8 +4,16 @@
 #include <linux/preempt.h>
 #include <asm/break.h>
 
-#define KPROBE_BP_INSN		larch_insn_gen_break(BRK_KPROBE_BP)
-#define KPROBE_SSTEPBP_INSN	larch_insn_gen_break(BRK_KPROBE_SSTEPBP)
+#define KPROBE_BP_INSN		__emit_break(BRK_KPROBE_BP)
+#define KPROBE_SSTEPBP_INSN	__emit_break(BRK_KPROBE_SSTEPBP)
+
+static __init int check_emit_break(void)
+{
+	BUG_ON(KPROBE_BP_INSN      != larch_insn_gen_break(BRK_KPROBE_BP));
+	BUG_ON(KPROBE_SSTEPBP_INSN != larch_insn_gen_break(BRK_KPROBE_SSTEPBP));
+	return 0;
+}
+arch_initcall(check_emit_break);
 
 DEFINE_PER_CPU(struct kprobe *, current_kprobe);
 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
arch/loongarch/kernel/uprobes.c +8 -0
index 87abc7137b73..90462d94c28f 100644
@@ -7,6 +7,14 @@
 
 #define UPROBE_TRAP_NR	UINT_MAX
 
+static __init int check_emit_break(void)
+{
+	BUG_ON(UPROBE_SWBP_INSN  != larch_insn_gen_break(BRK_UPROBE_BP));
+	BUG_ON(UPROBE_XOLBP_INSN != larch_insn_gen_break(BRK_UPROBE_XOLBP));
+	return 0;
+}
+arch_initcall(check_emit_break);
+
 int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe,
 			     struct mm_struct *mm, unsigned long addr)
 {
Signature · 1 line
2.25.1.362.g51ebf55
maintainer
#17 lore raw

Re: [PATCH] LoongArch: make the users of larch_insn_gen_break() constant

Hi, Oleg,

Quoted 82 lines · Oleg Nesterov

LoongArch defines UPROBE_SWBP_INSN as a function call and this breaks arch_uprobe_trampoline() which uses it to initialize a static variable.

Add the new "__builtin_constant_p" helper, __emit_break(), and redefine the current users of larch_insn_gen_break() to use it.

The patch adds check_emit_break() into kprobes.c and uprobes.c to test this change. They can be removed if LoongArch boots at least once, but otoh these 2 __init functions will be discarded by free_initmem().

Fixes ff474a78cef5 ("uprobe: Add uretprobe syscall to speed up return probe")
---
 arch/loongarch/include/asm/inst.h    |  3 +++
 arch/loongarch/include/asm/uprobes.h |  4 ++--
 arch/loongarch/kernel/kprobes.c      | 12 ++++++++++--
 arch/loongarch/kernel/uprobes.c      |  8 ++++++++
 4 files changed, 23 insertions(+), 4 deletions(-)
arch/loongarch/include/asm/inst.h +3 -0
index c3993fd88aba..944482063f14 100644
@@ -532,6 +532,9 @@ static inline void emit_##NAME(union loongarch_instruction *insn,   \
 
 DEF_EMIT_REG0I15_FORMAT(break, break_op)
 
+/* like emit_break(imm) but returns a constant expression */
+#define __emit_break(imm)      ((u32)((imm) | (break_op << 15)))
+
 #define DEF_EMIT_REG0I26_FORMAT(NAME, OP)                              \
 static inline void emit_##NAME(union loongarch_instruction *insn,      \
                               int offset)                              \
arch/loongarch/include/asm/uprobes.h +2 -2
index c8f59983f702..99a0d198927f 100644
@@ -9,10 +9,10 @@ typedef u32 uprobe_opcode_t;
 #define MAX_UINSN_BYTES                8
 #define UPROBE_XOL_SLOT_BYTES  MAX_UINSN_BYTES
 
-#define UPROBE_SWBP_INSN       larch_insn_gen_break(BRK_UPROBE_BP)
+#define UPROBE_SWBP_INSN       __emit_break(BRK_UPROBE_BP)
 #define UPROBE_SWBP_INSN_SIZE  LOONGARCH_INSN_SIZE
 
-#define UPROBE_XOLBP_INSN      larch_insn_gen_break(BRK_UPROBE_XOLBP)
+#define UPROBE_XOLBP_INSN      __emit_break(BRK_UPROBE_XOLBP)
 
 struct arch_uprobe {
        unsigned long   resume_era;
arch/loongarch/kernel/kprobes.c +10 -2
index 17b040bd6067..78cfaac52748 100644
@@ -4,8 +4,16 @@
 #include <linux/preempt.h>
 #include <asm/break.h>
 
-#define KPROBE_BP_INSN         larch_insn_gen_break(BRK_KPROBE_BP)
-#define KPROBE_SSTEPBP_INSN    larch_insn_gen_break(BRK_KPROBE_SSTEPBP)
+#define KPROBE_BP_INSN         __emit_break(BRK_KPROBE_BP)
+#define KPROBE_SSTEPBP_INSN    __emit_break(BRK_KPROBE_SSTEPBP)
+
+static __init int check_emit_break(void)
+{
+       BUG_ON(KPROBE_BP_INSN      != larch_insn_gen_break(BRK_KPROBE_BP));
+       BUG_ON(KPROBE_SSTEPBP_INSN != larch_insn_gen_break(BRK_KPROBE_SSTEPBP));
+       return 0;
+}
+arch_initcall(check_emit_break);
 
 DEFINE_PER_CPU(struct kprobe *, current_kprobe);
 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
arch/loongarch/kernel/uprobes.c +0 -0
index 87abc7137b73..90462d94c28f 100644

arch/loongarch/kernel/uprobes.c line 16

Quoted 10 lines
arch/loongarch/kernel/uprobes.c +7 -0
@@ -7,6 +7,14 @@
 
 #define UPROBE_TRAP_NR UINT_MAX
 
+static __init int check_emit_break(void)
+{
+       BUG_ON(UPROBE_SWBP_INSN  != larch_insn_gen_break(BRK_UPROBE_BP));
+       BUG_ON(UPROBE_XOLBP_INSN != larch_insn_gen_break(BRK_UPROBE_XOLBP));
+       return 0;
+}
+arch_initcall(check_emit_break);

Do you mind if I remove the runtime checking after Tiezhu tests the correctness?

Huacai

Quoted 8 lines
arch/loongarch/kernel/uprobes.c +1 -1
+
 int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe,
                             struct mm_struct *mm, unsigned long addr)
 {
--

2.25.1.362.g51ebf55

maintainer
#18 lore raw

Re: [PATCH] LoongArch: make the users of larch_insn_gen_break() constant

Quoted 9 lines · Huacai Chen

arch/loongarch/kernel/uprobes.c line 16

Quoted 8 lines
arch/loongarch/kernel/uprobes.c +7 -0
+static __init int check_emit_break(void)
+{
+       BUG_ON(UPROBE_SWBP_INSN  != larch_insn_gen_break(BRK_UPROBE_BP));
+       BUG_ON(UPROBE_XOLBP_INSN != larch_insn_gen_break(BRK_UPROBE_XOLBP));
+       return 0;
+}
+arch_initcall(check_emit_break);

Do you mind if I remove the runtime checking after Tiezhu tests the correctness?

Sure, please remove, thanks.

Oleg.

Tested-by on v1 1/1
#19 lore raw

Re: [PATCH] LoongArch: make the users of larch_insn_gen_break() constant

In reply to Oleg Nesterov
Quoted 10 lines · Oleg Nesterov

LoongArch defines UPROBE_SWBP_INSN as a function call and this breaks arch_uprobe_trampoline() which uses it to initialize a static variable.

Add the new "__builtin_constant_p" helper, __emit_break(), and redefine the current users of larch_insn_gen_break() to use it.

The patch adds check_emit_break() into kprobes.c and uprobes.c to test this change. They can be removed if LoongArch boots at least once, but otoh these 2 __init functions will be discarded by free_initmem().

Quoted 5 lines
Fixes ff474a78cef5 ("uprobe: Add uretprobe syscall to speed up return probe")

Tested on LoongArch machine with Loongson-3A5000 and Loongson-3A6000 CPU, based on 6.10-rc3,

KPROBE_BP_INSN == larch_insn_gen_break(BRK_KPROBE_BP) KPROBE_SSTEPBP_INSN == larch_insn_gen_break(BRK_KPROBE_SSTEPBP) UPROBE_SWBP_INSN == larch_insn_gen_break(BRK_UPROBE_BP) UPROBE_XOLBP_INSN == larch_insn_gen_break(BRK_UPROBE_XOLBP)

The two functions check_emit_break() can be removed in arch/loongarch/kernel/kprobes.c and arch/loongarch/kernel/uprobes.c

Thanks,
Tiezhu

maintainer Applied
#20 lore raw

Re: [PATCH] LoongArch: make the users of larch_insn_gen_break() constant

Quoted 26 lines · Tiezhu Yang
Quoted 10 lines · Oleg Nesterov

LoongArch defines UPROBE_SWBP_INSN as a function call and this breaks arch_uprobe_trampoline() which uses it to initialize a static variable.

Add the new "__builtin_constant_p" helper, __emit_break(), and redefine the current users of larch_insn_gen_break() to use it.

The patch adds check_emit_break() into kprobes.c and uprobes.c to test this change. They can be removed if LoongArch boots at least once, but otoh these 2 __init functions will be discarded by free_initmem().

Quoted 5 lines
Fixes ff474a78cef5 ("uprobe: Add uretprobe syscall to speed up return probe")

Tested on LoongArch machine with Loongson-3A5000 and Loongson-3A6000 CPU, based on 6.10-rc3,

KPROBE_BP_INSN == larch_insn_gen_break(BRK_KPROBE_BP) KPROBE_SSTEPBP_INSN == larch_insn_gen_break(BRK_KPROBE_SSTEPBP) UPROBE_SWBP_INSN == larch_insn_gen_break(BRK_UPROBE_BP) UPROBE_XOLBP_INSN == larch_insn_gen_break(BRK_UPROBE_XOLBP)

Quoted 4 lines

The two functions check_emit_break() can be removed in arch/loongarch/kernel/kprobes.c and arch/loongarch/kernel/uprobes.c

Queued, thanks.

Huacai

Quoted 5 lines

Thanks,
Tiezhu