Skip to contentSkip to conversation

Perf: support to unwind userspace application stacks generated with thumb.

PATCH In review Jiping Ma 2 participants 4 messages Apr 16, 2020 – Apr 22, 2020

Subsystems (3)
lore
Share & tools
lore

Jiping Ma

author
#0 lore raw

PATCH 1/1 Perf: support to unwind userspace application stacks generated with thumb.

Only push sp and lr in the stack for thumb mode. it will go through the stack find sp and lr.

Change this to the more detailed description of the patch

---
 arch/arm64/kernel/perf_callchain.c | 36 +++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)
arch/arm64/kernel/perf_callchain.c +33 -3
index bcafd7dcfe8b..97dde271c121 100644
@@ -104,6 +104,30 @@ compat_user_backtrace(struct compat_frame_tail __user *tail,
 
 	return (struct compat_frame_tail __user *)compat_ptr(buftail.fp) - 1;
 }
+
+void
+user_backtrace_thumb(struct perf_callchain_entry_ctx *entry,
+		     struct pt_regs *regs)
+{
+	u32 sp;
+	u32 *sp_t;
+	/*
+	 * Only push sp, lr to stack.
+	 */
+	for (sp = regs->compat_sp; (sp < current->mm->start_stack) &&
+		(entry->nr < entry->max_stack); sp += 4) {
+		sp_t = (u32 *)(unsigned long)sp;
+		if ((*sp_t > regs->compat_sp) &&
+			(*sp_t < current->mm->start_stack)) {
+			if (*(sp_t + 1) < current->mm->end_code &&
+				*(sp_t + 1) > current->mm->start_code) {
+				perf_callchain_store(entry,  *(sp_t + 1)-1);
+				sp += 4;
+			}
+		}
+	}
+}
+
 #endif /* CONFIG_COMPAT */
 
 void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
@@ -132,9 +156,15 @@ void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
 
 		tail = (struct compat_frame_tail __user *)regs->compat_fp - 1;
 
-		while ((entry->nr < entry->max_stack) &&
-			tail && !((unsigned long)tail & 0x3))
-			tail = compat_user_backtrace(tail, entry);
+		if (((unsigned long)tail > current->mm->start_stack) ||
+			((unsigned long)tail < regs->compat_sp) ||
+			regs->compat_sp == regs->compat_usr(7)) {
+			user_backtrace_thumb(entry, regs);
+		} else {
+			while ((entry->nr < entry->max_stack) &&
+				tail && !((unsigned long)tail & 0x3))
+				tail = compat_user_backtrace(tail, entry);
+		}
 #endif
 	}
 }
Signature · 1 line
2.18.1
maintainer
#1 lore raw
Quoted 31 lines · Jiping Ma

Only push sp and lr in the stack for thumb mode. it will go through the stack find sp and lr.

Change this to the more detailed description of the patch

---
 arch/arm64/kernel/perf_callchain.c | 36 +++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)
arch/arm64/kernel/perf_callchain.c +13 -0
index bcafd7dcfe8b..97dde271c121 100644
@@ -104,6 +104,30 @@ compat_user_backtrace(struct compat_frame_tail __user *tail,
 
 	return (struct compat_frame_tail __user *)compat_ptr(buftail.fp) - 1;
 }
+
+void
+user_backtrace_thumb(struct perf_callchain_entry_ctx *entry,
+		     struct pt_regs *regs)
+{
+	u32 sp;
+	u32 *sp_t;
+	/*
+	 * Only push sp, lr to stack.
+	 */
+	for (sp = regs->compat_sp; (sp < current->mm->start_stack) &&
+		(entry->nr < entry->max_stack); sp += 4) {
+		sp_t = (u32 *)(unsigned long)sp;

arch/arm64/kernel/perf_callchain.c compat_user_backtrace(struct compat_frame_tail __user *tail, · line 129

Quoted 10 lines
arch/arm64/kernel/perf_callchain.c +10 -0
+		if ((*sp_t > regs->compat_sp) &&
+			(*sp_t < current->mm->start_stack)) {
+			if (*(sp_t + 1) < current->mm->end_code &&
+				*(sp_t + 1) > current->mm->start_code) {
+				perf_callchain_store(entry,  *(sp_t + 1)-1);
+				sp += 4;
+			}
+		}
+	}
+}

This looks like a pile of fragile heuristics to me. Why don't you just use libunwind in userspace, the same way you'd have to if you compiled without framepointers?

Will

author
#2 lore raw
Quoted 37 lines · Will Deacon
Quoted 36 lines · Jiping Ma

Only push sp and lr in the stack for thumb mode. it will go through the stack find sp and lr.

Change this to the more detailed description of the patch

---
  arch/arm64/kernel/perf_callchain.c | 36 +++++++++++++++++++++++++++---
  1 file changed, 33 insertions(+), 3 deletions(-)
arch/arm64/kernel/perf_callchain.c +18 -0
index bcafd7dcfe8b..97dde271c121 100644
@@ -104,6 +104,30 @@ compat_user_backtrace(struct compat_frame_tail __user *tail,
 
  	return (struct compat_frame_tail __user *)compat_ptr(buftail.fp) - 1;
  }
+
+void
+user_backtrace_thumb(struct perf_callchain_entry_ctx *entry,
+		     struct pt_regs *regs)
+{
+	u32 sp;
+	u32 *sp_t;
+	/*
+	 * Only push sp, lr to stack.
+	 */
+	for (sp = regs->compat_sp; (sp < current->mm->start_stack) &&
+		(entry->nr < entry->max_stack); sp += 4) {
+		sp_t = (u32 *)(unsigned long)sp;
+		if ((*sp_t > regs->compat_sp) &&
+			(*sp_t < current->mm->start_stack)) {
+			if (*(sp_t + 1) < current->mm->end_code &&
+				*(sp_t + 1) > current->mm->start_code) {
+				perf_callchain_store(entry,  *(sp_t + 1)-1);
Quoted 8 lines

arch/arm64/kernel/perf_callchain.c compat_user_backtrace(struct compat_frame_tail __user *tail, · line 129

Quoted 5 lines
arch/arm64/kernel/perf_callchain.c +5 -0
+				sp += 4;
+			}
+		}
+	}
+}

This looks like a pile of fragile heuristics to me. Why don't you just use libunwind in userspace, the same way you'd have to if you compiled without framepointers?

Hi, Will

Could you share more details for libunwind in userspace? Following is our build command for test app.
bt_perf.thumb: arm-wrs-linux-gnueabi-gcc -march=armv7ve -mthumb -mfpu=neon -mfloat-abi=softfp -mcpu=cortex-a15 --sysroot=sysroots/cortexa15t2-neon-wrs-linux-gnueabi -O0 -g -ggdb -Wall -funwind-tables -fno-omit-frame-pointer -frecord-gcc-switches -mapcs-frame bt_perf.c -o bt_perf.thumb

Thanks,
Jiping

Quoted 3 lines

Will

maintainer
#3 lore raw
Quoted 34 lines · Jiping Ma
Quoted 18 lines · Will Deacon
Quoted 17 lines · Jiping Ma
arch/arm64/kernel/perf_callchain.c +17 -0
+void
+user_backtrace_thumb(struct perf_callchain_entry_ctx *entry,
+		     struct pt_regs *regs)
+{
+	u32 sp;
+	u32 *sp_t;
+	/*
+	 * Only push sp, lr to stack.
+	 */
+	for (sp = regs->compat_sp; (sp < current->mm->start_stack) &&
+		(entry->nr < entry->max_stack); sp += 4) {
+		sp_t = (u32 *)(unsigned long)sp;
+		if ((*sp_t > regs->compat_sp) &&
+			(*sp_t < current->mm->start_stack)) {
+			if (*(sp_t + 1) < current->mm->end_code &&
+				*(sp_t + 1) > current->mm->start_code) {
+				perf_callchain_store(entry,  *(sp_t + 1)-1);
Quoted 8 lines

arch/arm64/kernel/perf_callchain.c compat_user_backtrace(struct compat_frame_tail __user *tail, · line 129

Quoted 5 lines
arch/arm64/kernel/perf_callchain.c +5 -0
+				sp += 4;
+			}
+		}
+	}
+}

This looks like a pile of fragile heuristics to me. Why don't you just use libunwind in userspace, the same way you'd have to if you compiled without framepointers?

Could you share more details for libunwind in userspace? Following is our build command for test app.
bt_perf.thumb: arm-wrs-linux-gnueabi-gcc -march=armv7ve -mthumb -mfpu=neon

(quoted diff) +0 -3
-mfloat-abi=softfp -mcpu=cortex-a15
--sysroot=sysroots/cortexa15t2-neon-wrs-linux-gnueabi -O0 -g -ggdb -Wall
-funwind-tables -fno-omit-frame-pointer -frecord-gcc-switches -mapcs-frame
Quoted 1 line

bt_perf.c -o bt_perf.thumb

You can build the perf tool with support for libunwind, and then it should handle the unwinding in userspace. I haven't used it for ages, but you may need to pass '--call-graph dwarf'. Have a play (and you can also read the code in tools/perf/).

Will