Skip to contentSkip to conversation

perf test: Unwind fixes

PATCH Applied Ian Rogers 2 participants 6 messages May 30, 2020 – Jun 1, 2020 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

cover perf test: Unwind fixes

Fix stack frame count and memory sanitizer issues when running the dwarf unwinding test with the elfutils/libdw unwinder (libunwind disabled).

Ian Rogers (3):
tools compiler.h: Add attribute to disable tail calls perf tests: Don't tail call optimize in unwind test perf test: Initialize memory in dwarf-unwind

 tools/include/linux/compiler-gcc.h       |  8 ++++++++
 tools/include/linux/compiler.h           |  3 +++
 tools/perf/arch/x86/tests/dwarf-unwind.c |  8 ++++++++
 tools/perf/tests/dwarf-unwind.c          | 11 ++++++-----
 4 files changed, 25 insertions(+), 5 deletions(-)
Signature · 1 line
2.27.0.rc2.251.g90737beb825-goog
author reviewer
#1 lore raw

PATCH 1/3 tools compiler.h: Add attribute to disable tail calls

Tail call optimizations can remove stack frames that are used in unwinding tests. Add an attribute that can be used to disable the tail call optimization. Tested on clang and GCC.

---
 tools/include/linux/compiler-gcc.h | 8 ++++++++
 tools/include/linux/compiler.h     | 3 +++
 2 files changed, 11 insertions(+)
tools/include/linux/compiler-gcc.h +8 -0
index 95c072b70d0e..cda555b47d3d 100644
@@ -27,6 +27,14 @@
 #define  __pure		__attribute__((pure))
 #endif
 #define  noinline	__attribute__((noinline))
+#ifdef __has_attribute
+#if __has_attribute(disable_tail_calls)
+#define __no_tail_call	__attribute__((disable_tail_calls))
+#endif
+#endif
+#ifndef __no_tail_call
+#define __no_tail_call	__attribute__((optimize("no-optimize-sibling-calls")))
+#endif
 #ifndef __packed
 #define __packed	__attribute__((packed))
 #endif
tools/include/linux/compiler.h +3 -0
index 180f7714a5f1..9f9002734e19 100644
@@ -47,6 +47,9 @@
 #ifndef noinline
 #define noinline
 #endif
+#ifndef __no_tail_call
+#define __no_tail_call
+#endif
 
 /* Are two types/vars the same type (ignoring qualifiers)? */
 #ifndef __same_type
Signature · 1 line
2.27.0.rc2.251.g90737beb825-goog
#2 lore raw

Re: [PATCH 1/3] tools compiler.h: Add attribute to disable tail calls

Em Sat, May 30, 2020 at 01:20:13AM -0700, Ian Rogers escreveu:

Quoted 16 lines

Tail call optimizations can remove stack frames that are used in unwinding tests. Add an attribute that can be used to disable the tail call optimization. Tested on clang and GCC.

---
 tools/include/linux/compiler-gcc.h | 8 ++++++++
 tools/include/linux/compiler.h     | 3 +++
 2 files changed, 11 insertions(+)
tools/include/linux/compiler-gcc.h +0 -0
index 95c072b70d0e..cda555b47d3d 100644
@@ -27,6 +27,14 @@
 #define  __pure		__attribute__((pure))

tools/include/linux/compiler-gcc.h line 37

Quoted 10 lines
tools/include/linux/compiler-gcc.h +8 -0
 #endif
 #define  noinline	__attribute__((noinline))
+#ifdef __has_attribute
+#if __has_attribute(disable_tail_calls)
+#define __no_tail_call	__attribute__((disable_tail_calls))
+#endif
+#endif
+#ifndef __no_tail_call
+#define __no_tail_call	__attribute__((optimize("no-optimize-sibling-calls")))
+#endif

Added this for some retro-computing old distros when building with old clang versions :-)

- Arnaldo

tools/include/linux/compiler-gcc.h +3 -0
index cda555b47d3d..dea7378ad646 100644
@@ -33,7 +33,10 @@
 #endif
 #endif
 #ifndef __no_tail_call
+#if GCC_VERSION > 40201
 #define __no_tail_call	__attribute__((optimize("no-optimize-sibling-calls")))
+#else
+#define __no_tail_call
 #endif
 #ifndef __packed
 #define __packed	__attribute__((packed))
author reviewer
#3 lore raw

PATCH 2/3 perf tests: Don't tail call optimize in unwind test

In reply to Ian Rogers

The tail call optimization can unexpectedly make the stack smaller and cause the test to fail.

---
 tools/perf/tests/dwarf-unwind.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tools/perf/tests/dwarf-unwind.c +5 -5
index 779ce280a0e9..2a0dac81f44c 100644
@@ -94,7 +94,7 @@ static int unwind_entry(struct unwind_entry *entry, void *arg)
 	return strcmp((const char *) symbol, funcs[idx]);
 }
 
-noinline int test_dwarf_unwind__thread(struct thread *thread)
+__no_tail_call noinline int test_dwarf_unwind__thread(struct thread *thread)
 {
 	struct perf_sample sample;
 	unsigned long cnt = 0;
@@ -125,7 +125,7 @@ noinline int test_dwarf_unwind__thread(struct thread *thread)
 
 static int global_unwind_retval = -INT_MAX;
 
-noinline int test_dwarf_unwind__compare(void *p1, void *p2)
+__no_tail_call noinline int test_dwarf_unwind__compare(void *p1, void *p2)
 {
 	/* Any possible value should be 'thread' */
 	struct thread *thread = *(struct thread **)p1;
@@ -144,7 +144,7 @@ noinline int test_dwarf_unwind__compare(void *p1, void *p2)
 	return p1 - p2;
 }
 
-noinline int test_dwarf_unwind__krava_3(struct thread *thread)
+__no_tail_call noinline int test_dwarf_unwind__krava_3(struct thread *thread)
 {
 	struct thread *array[2] = {thread, thread};
 	void *fp = &bsearch;
@@ -163,12 +163,12 @@ noinline int test_dwarf_unwind__krava_3(struct thread *thread)
 	return global_unwind_retval;
 }
 
-noinline int test_dwarf_unwind__krava_2(struct thread *thread)
+__no_tail_call noinline int test_dwarf_unwind__krava_2(struct thread *thread)
 {
 	return test_dwarf_unwind__krava_3(thread);
 }
 
-noinline int test_dwarf_unwind__krava_1(struct thread *thread)
+__no_tail_call noinline int test_dwarf_unwind__krava_1(struct thread *thread)
 {
 	return test_dwarf_unwind__krava_2(thread);
 }
Signature · 1 line
2.27.0.rc2.251.g90737beb825-goog
author reviewer
#4 lore raw

PATCH 3/3 perf test: Initialize memory in dwarf-unwind

In reply to Ian Rogers

Avoid a false positive caused by assembly code in arch/x86. In tests, zero the perf_event to avoid uninitialized memory uses. Warnings were caught using clang with -fsanitize=memory.

---
 tools/perf/arch/x86/tests/dwarf-unwind.c | 8 ++++++++
 tools/perf/tests/dwarf-unwind.c          | 1 +
 2 files changed, 9 insertions(+)
tools/perf/arch/x86/tests/dwarf-unwind.c +8 -0
index ef43be9b6ec2..4e40402a4f81 100644
@@ -55,6 +55,14 @@ int test__arch_unwind_sample(struct perf_sample *sample,
 		return -1;
 	}
 
+#ifdef MEMORY_SANITIZER
+	/*
+	 * Assignments to buf in the assembly function perf_regs_load aren't
+	 * seen by memory sanitizer. Zero the memory to convince memory
+	 * sanitizer the memory is initialized.
+	 */
+	memset(buf, 0, sizeof(u64) * PERF_REGS_MAX);
+#endif
 	perf_regs_load(buf);
 	regs->abi  = PERF_SAMPLE_REGS_ABI;
 	regs->regs = buf;
tools/perf/tests/dwarf-unwind.c +1 -0
index 2a0dac81f44c..2491d167bf76 100644
@@ -37,6 +37,7 @@ static int init_live_machine(struct machine *machine)
 	union perf_event event;
 	pid_t pid = getpid();
 
+	memset(&event, 0, sizeof(event));
 	return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
 						  mmap_handler, machine, true);
 }
Signature · 1 line
2.27.0.rc2.251.g90737beb825-goog
maintainer Applied
#5 lore raw
In reply to Ian Rogers

Em Sat, May 30, 2020 at 01:20:12AM -0700, Ian Rogers escreveu:

Quoted 3 lines

Fix stack frame count and memory sanitizer issues when running the dwarf unwinding test with the elfutils/libdw unwinder (libunwind disabled).

Thanks, applied.

Quoted 14 lines

Ian Rogers (3):
tools compiler.h: Add attribute to disable tail calls perf tests: Don't tail call optimize in unwind test perf test: Initialize memory in dwarf-unwind

 tools/include/linux/compiler-gcc.h       |  8 ++++++++
 tools/include/linux/compiler.h           |  3 +++
 tools/perf/arch/x86/tests/dwarf-unwind.c |  8 ++++++++
 tools/perf/tests/dwarf-unwind.c          | 11 ++++++-----
 4 files changed, 25 insertions(+), 5 deletions(-)
Signature · 1 line
2.27.0.rc2.251.g90737beb825-goog
Signature · 1 line
- Arnaldo