Skip to contentSkip to conversation

Various asan and test fixes

PATCH Applied Ian Rogers 4 participants 16 messages May 28, 2025 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

v2 cover Various asan and test fixes

When testing removing perf_env with asan I noticed a number of test failures either because of asan issues or because of building with NO_LIBELF=1 and NO_LIBBPF=1 to avoid false memory leaks. Address these issues so that real test failures stand out.

v2: Fix wrong elf size use in filename__read_build_id fix. Add
thread__comm_lock fix.

Ian Rogers (7):
perf symbol: Fix use-after-free in filename__read_build_id perf test demangle-java: Don't segv if demangling fails perf symbol: Move demangling code out of symbol-elf.c perf intel-tpebs: Avoid race when evlist is being deleted perf test intel-pt: Skip jitdump test if no libelf perf test trace_summary: Skip --bpf-summary tests if no libbpf perf thread: Avoid recursively taking thread__comm_lock

 tools/perf/tests/demangle-java-test.c   |   5 +
 tools/perf/tests/shell/test_intel_pt.sh |   5 +
 tools/perf/tests/shell/trace_summary.sh |   6 +
 tools/perf/util/demangle-cxx.h          |   2 +
 tools/perf/util/intel-tpebs.c           |  12 +-
 tools/perf/util/symbol-elf.c            |  86 ------------
 tools/perf/util/symbol-minimal.c        | 175 ++++++++++--------------
 tools/perf/util/symbol.c                |  87 ++++++++++++
 tools/perf/util/thread.c                |  17 ++-
 9 files changed, 198 insertions(+), 197 deletions(-)
Signature · 1 line
2.49.0.1238.gf8c92423fb-goog
author reviewer
#1 lore raw

PATCH v2 1/7 perf symbol: Fix use-after-free in filename__read_build_id

The same buf is used for the program headers and reading notes. As the notes memory may be reallocated then this corrupts the memory pointed to by the phdr. Using the same buffer is in any case a logic error. Rather than deal with the duplicated code, introduce an elf32 boolean and a union for either the elf32 or elf64 headers that are in use. Let the program headers have their own memory and grow the buffer for notes as necessary.

Before perf list -j compiled with asan would crash with:

```
==4176189==ERROR: AddressSanitizer: heap-use-after-free on address 0x5160000070b8 at pc 0x555d3b15075b bp 0x7ffebb5a8090 sp 0x7ffebb5a8088
READ of size 8 at 0x5160000070b8 thread T0
    #0 0x555d3b15075a in filename__read_build_id tools/perf/util/symbol-minimal.c:212:25
    #1 0x555d3ae43aff in filename__sprintf_build_id tools/perf/util/build-id.c:110:8
...

0x5160000070b8 is located 312 bytes inside of 560-byte region [0x516000006f80,0x5160000071b0)
freed by thread T0 here:
    #0 0x555d3ab21840 in realloc (perf+0x264840) (BuildId: 12dff2f6629f738e5012abdf0e90055518e70b5e)
    #1 0x555d3b1506e7 in filename__read_build_id tools/perf/util/symbol-minimal.c:206:11
...

previously allocated by thread T0 here:
    #0 0x555d3ab21423 in malloc (perf+0x264423) (BuildId: 12dff2f6629f738e5012abdf0e90055518e70b5e)
    #1 0x555d3b1503a2 in filename__read_build_id tools/perf/util/symbol-minimal.c:182:9
...
```

Note: this bug is long standing and not introduced by the other asan fix in commit fa9c4977fbfb ("perf symbol-minimal: Fix double free in filename__read_build_id").

Fixes b691f64360ecec49 ("perf symbols: Implement poor man's ELF parser")
---
 tools/perf/util/symbol-minimal.c | 168 +++++++++++++------------------
 1 file changed, 70 insertions(+), 98 deletions(-)
tools/perf/util/symbol-minimal.c +70 -98
index d8da3da01fe6..36c1d3090689 100644
@@ -90,11 +90,23 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
 {
 	FILE *fp;
 	int ret = -1;
-	bool need_swap = false;
+	bool need_swap = false, elf32;
 	u8 e_ident[EI_NIDENT];
-	size_t buf_size;
-	void *buf;
 	int i;
+	union {
+		struct {
+			Elf32_Ehdr ehdr32;
+			Elf32_Phdr *phdr32;
+		};
+		struct {
+			Elf64_Ehdr ehdr64;
+			Elf64_Phdr *phdr64;
+		};
+	} hdrs;
+	void *phdr;
+	size_t phdr_size;
+	void *buf = NULL;
+	size_t buf_size = 0;
 
 	fp = fopen(filename, "r");
 	if (fp == NULL)
@@ -108,119 +120,79 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
 		goto out;
 
 	need_swap = check_need_swap(e_ident[EI_DATA]);
+	elf32 = e_ident[EI_CLASS] == ELFCLASS32;
 
-	/* for simplicity */
-	fseek(fp, 0, SEEK_SET);
-
-	if (e_ident[EI_CLASS] == ELFCLASS32) {
-		Elf32_Ehdr ehdr;
-		Elf32_Phdr *phdr;
-
-		if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
-			goto out;
+	if (fread(elf32 ? (void *)&hdrs.ehdr32 : (void *)&hdrs.ehdr64,
+		  elf32 ? sizeof(hdrs.ehdr32) : sizeof(hdrs.ehdr64),
+		  1, fp) != 1)
+		goto out;
 
-		if (need_swap) {
-			ehdr.e_phoff = bswap_32(ehdr.e_phoff);
-			ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
-			ehdr.e_phnum = bswap_16(ehdr.e_phnum);
+	if (need_swap) {
+		if (elf32) {
+			hdrs.ehdr32.e_phoff = bswap_32(hdrs.ehdr32.e_phoff);
+			hdrs.ehdr32.e_phentsize = bswap_16(hdrs.ehdr32.e_phentsize);
+			hdrs.ehdr32.e_phnum = bswap_16(hdrs.ehdr32.e_phnum);
+		} else {
+			hdrs.ehdr64.e_phoff = bswap_64(hdrs.ehdr64.e_phoff);
+			hdrs.ehdr64.e_phentsize = bswap_16(hdrs.ehdr64.e_phentsize);
+			hdrs.ehdr64.e_phnum = bswap_16(hdrs.ehdr64.e_phnum);
 		}
+	}
+	phdr_size = elf32 ? hdrs.ehdr32.e_phentsize * hdrs.ehdr32.e_phnum
+			  : hdrs.ehdr64.e_phentsize * hdrs.ehdr64.e_phnum;
+	phdr = malloc(phdr_size);
+	if (phdr == NULL)
+		goto out;
 
-		buf_size = ehdr.e_phentsize * ehdr.e_phnum;
-		buf = malloc(buf_size);
-		if (buf == NULL)
-			goto out;
-
-		fseek(fp, ehdr.e_phoff, SEEK_SET);
-		if (fread(buf, buf_size, 1, fp) != 1)
-			goto out_free;
-
-		for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
-			void *tmp;
-			long offset;
-
-			if (need_swap) {
-				phdr->p_type = bswap_32(phdr->p_type);
-				phdr->p_offset = bswap_32(phdr->p_offset);
-				phdr->p_filesz = bswap_32(phdr->p_filesz);
-			}
-
-			if (phdr->p_type != PT_NOTE)
-				continue;
-
-			offset = phdr->p_offset;
-			if (phdr->p_filesz > buf_size) {
-				buf_size = phdr->p_filesz;
-				tmp = realloc(buf, buf_size);
-				if (tmp == NULL)
-					goto out_free;
-				buf = tmp;
-			}
-			fseek(fp, offset, SEEK_SET);
-			if (fread(buf, phdr->p_filesz, 1, fp) != 1)
-				goto out_free;
+	fseek(fp, elf32 ? hdrs.ehdr32.e_phoff : hdrs.ehdr64.e_phoff, SEEK_SET);
+	if (fread(phdr, phdr_size, 1, fp) != 1)
+		goto out_free;
 
-			ret = read_build_id(buf, phdr->p_filesz, bid, need_swap);
-			if (ret == 0) {
-				ret = bid->size;
-				break;
-			}
-		}
-	} else {
-		Elf64_Ehdr ehdr;
-		Elf64_Phdr *phdr;
+	if (elf32)
+		hdrs.phdr32 = phdr;
+	else
+		hdrs.phdr64 = phdr;
 
-		if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
-			goto out;
+	for (i = 0; i < elf32 ? hdrs.ehdr32.e_phnum : hdrs.ehdr64.e_phnum; i++) {
+		size_t p_filesz;
 
 		if (need_swap) {
-			ehdr.e_phoff = bswap_64(ehdr.e_phoff);
-			ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
-			ehdr.e_phnum = bswap_16(ehdr.e_phnum);
+			if (elf32) {
+				hdrs.phdr32[i].p_type = bswap_32(hdrs.phdr32[i].p_type);
+				hdrs.phdr32[i].p_offset = bswap_32(hdrs.phdr32[i].p_offset);
+				hdrs.phdr32[i].p_filesz = bswap_32(hdrs.phdr32[i].p_offset);
+			} else {
+				hdrs.phdr64[i].p_type = bswap_32(hdrs.phdr64[i].p_type);
+				hdrs.phdr64[i].p_offset = bswap_64(hdrs.phdr64[i].p_offset);
+				hdrs.phdr64[i].p_filesz = bswap_64(hdrs.phdr64[i].p_filesz);
+			}
 		}
+		if ((elf32 ? hdrs.phdr32[i].p_type : hdrs.phdr64[i].p_type) != PT_NOTE)
+			continue;
 
-		buf_size = ehdr.e_phentsize * ehdr.e_phnum;
-		buf = malloc(buf_size);
-		if (buf == NULL)
-			goto out;
-
-		fseek(fp, ehdr.e_phoff, SEEK_SET);
-		if (fread(buf, buf_size, 1, fp) != 1)
-			goto out_free;
-
-		for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
+		p_filesz = elf32 ? hdrs.phdr32[i].p_filesz : hdrs.phdr64[i].p_filesz;
+		if (p_filesz > buf_size) {
 			void *tmp;
-			long offset;
-
-			if (need_swap) {
-				phdr->p_type = bswap_32(phdr->p_type);
-				phdr->p_offset = bswap_64(phdr->p_offset);
-				phdr->p_filesz = bswap_64(phdr->p_filesz);
-			}
-
-			if (phdr->p_type != PT_NOTE)
-				continue;
 
-			offset = phdr->p_offset;
-			if (phdr->p_filesz > buf_size) {
-				buf_size = phdr->p_filesz;
-				tmp = realloc(buf, buf_size);
-				if (tmp == NULL)
-					goto out_free;
-				buf = tmp;
-			}
-			fseek(fp, offset, SEEK_SET);
-			if (fread(buf, phdr->p_filesz, 1, fp) != 1)
+			buf_size = p_filesz;
+			tmp = realloc(buf, buf_size);
+			if (tmp == NULL)
 				goto out_free;
+			buf = tmp;
+		}
+		fseek(fp, elf32 ? hdrs.phdr32[i].p_offset : hdrs.phdr64[i].p_offset, SEEK_SET);
+		if (fread(buf, p_filesz, 1, fp) != 1)
+			goto out_free;
 
-			ret = read_build_id(buf, phdr->p_filesz, bid, need_swap);
-			if (ret == 0) {
-				ret = bid->size;
-				break;
-			}
+		ret = read_build_id(buf, p_filesz, bid, need_swap);
+		if (ret == 0) {
+			ret = bid->size;
+			break;
 		}
 	}
 out_free:
 	free(buf);
+	free(phdr);
 out:
 	fclose(fp);
 	return ret;
Signature · 1 line
2.49.0.1238.gf8c92423fb-goog
author reviewer
#2 lore raw

PATCH v2 2/7 perf test demangle-java: Don't segv if demangling fails

In reply to Ian Rogers

The buffer returned by dso__demangle_sym may be NULL, don't segv in strcmp if this happens. Currently this happens for NO_LIBELF=1 builds.

---
 tools/perf/tests/demangle-java-test.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/perf/tests/demangle-java-test.c +5 -0
index ebaf60cdfa99..0fb3e5a4a0ed 100644
@@ -30,6 +30,11 @@ static int test__demangle_java(struct test_suite *test __maybe_unused, int subte
 
 	for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
 		buf = dso__demangle_sym(/*dso=*/NULL, /*kmodule=*/0, test_cases[i].mangled);
+		if (!buf) {
+			pr_debug("FAILED to demangle: \"%s\"\n \"%s\"\n", test_cases[i].mangled,
+				 test_cases[i].demangled);
+			continue;
+		}
 		if (strcmp(buf, test_cases[i].demangled)) {
 			pr_debug("FAILED: %s: %s != %s\n", test_cases[i].mangled,
 				 buf, test_cases[i].demangled);
Signature · 1 line
2.49.0.1238.gf8c92423fb-goog
author reviewer
#3 lore raw

PATCH v2 3/7 perf symbol: Move demangling code out of symbol-elf.c

In reply to Ian Rogers

symbol-elf.c is used when building with libelf, symbol-minimal is used otherwise. There is no reason the demangling code with no dependencies on libelf is part of symbol-elf.c so move to symbol.c. This allows demangling tests to pass with NO_LIBELF=1.

---
 tools/perf/util/demangle-cxx.h   |  2 +
 tools/perf/util/symbol-elf.c     | 86 -------------------------------
 tools/perf/util/symbol-minimal.c |  7 ---
 tools/perf/util/symbol.c         | 87 ++++++++++++++++++++++++++++++++
 4 files changed, 89 insertions(+), 93 deletions(-)
tools/perf/util/demangle-cxx.h +2 -0
index 26b5b66c0b4e..9359937a881a 100644
@@ -2,6 +2,8 @@
 #ifndef __PERF_DEMANGLE_CXX
 #define __PERF_DEMANGLE_CXX 1
 
+#include <stdbool.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
tools/perf/util/symbol-elf.c +0 -86

110 lines not on this page · raw message

tools/perf/util/symbol-minimal.c +0 -7
index 36c1d3090689..c73fe2e09fe9 100644
@@ -355,13 +355,6 @@ void symbol__elf_init(void)
 {
 }
 
-char *dso__demangle_sym(struct dso *dso __maybe_unused,
-			int kmodule __maybe_unused,
-			const char *elf_name __maybe_unused)
-{
-	return NULL;
-}
-
 bool filename__has_section(const char *filename __maybe_unused, const char *sec __maybe_unused)
 {
 	return false;
tools/perf/util/symbol.c +87 -0

102 lines not on this page · raw message

Signature · 1 line
2.49.0.1238.gf8c92423fb-goog
author reviewer
#4 lore raw

PATCH v2 4/7 perf intel-tpebs: Avoid race when evlist is being deleted

In reply to Ian Rogers

Reading through the evsel->evlist may seg fault if a sample arrives when the evlist is being deleted. Detect this case and ignore samples arriving when the evlist is being deleted.

Fixes bcfab08db7fb ("perf intel-tpebs: Filter non-workload samples")
---
 tools/perf/util/intel-tpebs.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
tools/perf/util/intel-tpebs.c +10 -2
index 4ad4bc118ea5..3b92ebf5c112 100644
@@ -162,9 +162,17 @@ static bool is_child_pid(pid_t parent, pid_t child)
 
 static bool should_ignore_sample(const struct perf_sample *sample, const struct tpebs_retire_lat *t)
 {
-	pid_t workload_pid = t->evsel->evlist->workload.pid;
-	pid_t sample_pid = sample->pid;
+	pid_t workload_pid, sample_pid = sample->pid;
 
+	/*
+	 * During evlist__purge the evlist will be removed prior to the
+	 * evsel__exit calling evsel__tpebs_close and taking the
+	 * tpebs_mtx. Avoid a segfault by ignoring samples in this case.
+	 */
+	if (t->evsel->evlist == NULL)
+		return true;
+
+	workload_pid = t->evsel->evlist->workload.pid;
 	if (workload_pid < 0 || workload_pid == sample_pid)
 		return false;
 
Signature · 1 line
2.49.0.1238.gf8c92423fb-goog
maintainer
#5 lore raw

Re: [PATCH v2 4/7] perf intel-tpebs: Avoid race when evlist is being deleted

Hi Ian,

Quoted 21 lines · Ian Rogers

Reading through the evsel->evlist may seg fault if a sample arrives when the evlist is being deleted. Detect this case and ignore samples arriving when the evlist is being deleted.

Fixes bcfab08db7fb ("perf intel-tpebs: Filter non-workload samples")
---
 tools/perf/util/intel-tpebs.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
tools/perf/util/intel-tpebs.c +1 -2
index 4ad4bc118ea5..3b92ebf5c112 100644
@@ -162,9 +162,17 @@ static bool is_child_pid(pid_t parent, pid_t child)
 
 static bool should_ignore_sample(const struct perf_sample *sample, const struct tpebs_retire_lat *t)
 {
-	pid_t workload_pid = t->evsel->evlist->workload.pid;
-	pid_t sample_pid = sample->pid;
+	pid_t workload_pid, sample_pid = sample->pid;

tools/perf/util/intel-tpebs.c static bool is_child_pid(pid_t parent, pid_t child) · line 175

Quoted 10 lines
tools/perf/util/intel-tpebs.c +9 -0
 
+	/*
+	 * During evlist__purge the evlist will be removed prior to the
+	 * evsel__exit calling evsel__tpebs_close and taking the
+	 * tpebs_mtx. Avoid a segfault by ignoring samples in this case.
+	 */
+	if (t->evsel->evlist == NULL)
+		return true;
+
+	workload_pid = t->evsel->evlist->workload.pid;

I'm curious if there's a chance of TOCTOU race. It'd certainly help the segfault but would this code prevent it completely?

Thanks,
Namhyung

Quoted 6 lines
tools/perf/util/intel-tpebs.c +0 -0
 	if (workload_pid < 0 || workload_pid == sample_pid)
 		return false;
 
Signature · 1 line
2.49.0.1238.gf8c92423fb-goog
author reviewer
#6 lore raw

Re: [PATCH v2 4/7] perf intel-tpebs: Avoid race when evlist is being deleted

Quoted 26 lines · Namhyung Kim

Hi Ian,

Quoted 22 lines · Ian Rogers

Reading through the evsel->evlist may seg fault if a sample arrives when the evlist is being deleted. Detect this case and ignore samples arriving when the evlist is being deleted.

Fixes bcfab08db7fb ("perf intel-tpebs: Filter non-workload samples")
---
 tools/perf/util/intel-tpebs.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
tools/perf/util/intel-tpebs.c +1 -2
index 4ad4bc118ea5..3b92ebf5c112 100644
@@ -162,9 +162,17 @@ static bool is_child_pid(pid_t parent, pid_t child)
 
 static bool should_ignore_sample(const struct perf_sample *sample, const struct tpebs_retire_lat *t)
 {
-     pid_t workload_pid = t->evsel->evlist->workload.pid;
-     pid_t sample_pid = sample->pid;
+     pid_t workload_pid, sample_pid = sample->pid;
 
Quoted 12 lines

tools/perf/util/intel-tpebs.c static bool is_child_pid(pid_t parent, pid_t child) · line 175

Quoted 9 lines
tools/perf/util/intel-tpebs.c +9 -0
+     /*
+      * During evlist__purge the evlist will be removed prior to the
+      * evsel__exit calling evsel__tpebs_close and taking the
+      * tpebs_mtx. Avoid a segfault by ignoring samples in this case.
+      */
+     if (t->evsel->evlist == NULL)
+             return true;
+
+     workload_pid = t->evsel->evlist->workload.pid;

I'm curious if there's a chance of TOCTOU race. It'd certainly help the segfault but would this code prevent it completely?

Good point. I think the race is already small as it doesn't happen without sanitizers for me.
Thinking about the evlist problem. When a destructor (evlist__delete) it is generally assumed the code is being single threaded and in C++ clang's -Wthread-safety will ignore destructors for this reason (annoying imo as it hides bugs). I don't see a good way to solve that for the evlist and evsel for the TPEBS case without using reference counting. Adding reference counts to evlist and evsel would be do-able as we could use reference count checking, but it would be a large and invasive change. Wdyt?

Thanks,
Ian

Quoted 10 lines

Thanks,
Namhyung

Quoted 6 lines
tools/perf/util/intel-tpebs.c +0 -1
      if (workload_pid < 0 || workload_pid == sample_pid)
              return false;
 
--

2.49.0.1238.gf8c92423fb-goog

maintainer
#7 lore raw

Re: [PATCH v2 4/7] perf intel-tpebs: Avoid race when evlist is being deleted

Quoted 40 lines · Ian Rogers
Quoted 26 lines · Namhyung Kim

Hi Ian,

Quoted 22 lines · Ian Rogers

Reading through the evsel->evlist may seg fault if a sample arrives when the evlist is being deleted. Detect this case and ignore samples arriving when the evlist is being deleted.

Fixes bcfab08db7fb ("perf intel-tpebs: Filter non-workload samples")
---
 tools/perf/util/intel-tpebs.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
tools/perf/util/intel-tpebs.c +1 -2
index 4ad4bc118ea5..3b92ebf5c112 100644
@@ -162,9 +162,17 @@ static bool is_child_pid(pid_t parent, pid_t child)
 
 static bool should_ignore_sample(const struct perf_sample *sample, const struct tpebs_retire_lat *t)
 {
-     pid_t workload_pid = t->evsel->evlist->workload.pid;
-     pid_t sample_pid = sample->pid;
+     pid_t workload_pid, sample_pid = sample->pid;
 
Quoted 12 lines

tools/perf/util/intel-tpebs.c static bool is_child_pid(pid_t parent, pid_t child) · line 175

Quoted 9 lines
tools/perf/util/intel-tpebs.c +9 -0
+     /*
+      * During evlist__purge the evlist will be removed prior to the
+      * evsel__exit calling evsel__tpebs_close and taking the
+      * tpebs_mtx. Avoid a segfault by ignoring samples in this case.
+      */
+     if (t->evsel->evlist == NULL)
+             return true;
+
+     workload_pid = t->evsel->evlist->workload.pid;

I'm curious if there's a chance of TOCTOU race. It'd certainly help the segfault but would this code prevent it completely?

Quoted 10 lines

Good point. I think the race is already small as it doesn't happen without sanitizers for me.
Thinking about the evlist problem. When a destructor (evlist__delete) it is generally assumed the code is being single threaded and in C++ clang's -Wthread-safety will ignore destructors for this reason (annoying imo as it hides bugs). I don't see a good way to solve that for the evlist and evsel for the TPEBS case without using reference counting. Adding reference counts to evlist and evsel would be do-able as we could use reference count checking, but it would be a large and invasive change. Wdyt?

Would it be possible to kill the TPEBS thread before deleting evlist?

Thanks,
Namhyung

author reviewer
#8 lore raw

Re: [PATCH v2 4/7] perf intel-tpebs: Avoid race when evlist is being deleted

Quoted 53 lines · Namhyung Kim
Quoted 40 lines · Ian Rogers
Quoted 26 lines · Namhyung Kim

Hi Ian,

Quoted 22 lines · Ian Rogers

Reading through the evsel->evlist may seg fault if a sample arrives when the evlist is being deleted. Detect this case and ignore samples arriving when the evlist is being deleted.

Fixes bcfab08db7fb ("perf intel-tpebs: Filter non-workload samples")
---
 tools/perf/util/intel-tpebs.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
tools/perf/util/intel-tpebs.c +1 -2
index 4ad4bc118ea5..3b92ebf5c112 100644
@@ -162,9 +162,17 @@ static bool is_child_pid(pid_t parent, pid_t child)
 
 static bool should_ignore_sample(const struct perf_sample *sample, const struct tpebs_retire_lat *t)
 {
-     pid_t workload_pid = t->evsel->evlist->workload.pid;
-     pid_t sample_pid = sample->pid;
+     pid_t workload_pid, sample_pid = sample->pid;
 
Quoted 12 lines

tools/perf/util/intel-tpebs.c static bool is_child_pid(pid_t parent, pid_t child) · line 175

Quoted 9 lines
tools/perf/util/intel-tpebs.c +9 -0
+     /*
+      * During evlist__purge the evlist will be removed prior to the
+      * evsel__exit calling evsel__tpebs_close and taking the
+      * tpebs_mtx. Avoid a segfault by ignoring samples in this case.
+      */
+     if (t->evsel->evlist == NULL)
+             return true;
+
+     workload_pid = t->evsel->evlist->workload.pid;

I'm curious if there's a chance of TOCTOU race. It'd certainly help the segfault but would this code prevent it completely?

Quoted 10 lines

Good point. I think the race is already small as it doesn't happen without sanitizers for me.
Thinking about the evlist problem. When a destructor (evlist__delete) it is generally assumed the code is being single threaded and in C++ clang's -Wthread-safety will ignore destructors for this reason (annoying imo as it hides bugs). I don't see a good way to solve that for the evlist and evsel for the TPEBS case without using reference counting. Adding reference counts to evlist and evsel would be do-able as we could use reference count checking, but it would be a large and invasive change. Wdyt?

Quoted 1 line

Would it be possible to kill the TPEBS thread before deleting evlist?

The TPEBS thread and other data structures are global and not tied to the evlist, so there can and are multiple evlists at play. When using TPEBS there is the evlist for perf stat, there is also the evlist for the samples. There's sense in having the evlist own the TPEBS data structures, there's also sense in things being global. I think if I'd done it I'd have gone with TPEBS within the evlist, but I suspect in the original changes there was a worry about adding cost on non-x86 builds.

Thanks,
Ian

Quoted 3 lines

Thanks,
Namhyung

maintainer
#9 lore raw

Re: [PATCH v2 4/7] perf intel-tpebs: Avoid race when evlist is being deleted

Quoted 56 lines · Ian Rogers
Quoted 53 lines · Namhyung Kim
Quoted 40 lines · Ian Rogers
Quoted 26 lines · Namhyung Kim

Hi Ian,

Quoted 22 lines · Ian Rogers

Reading through the evsel->evlist may seg fault if a sample arrives when the evlist is being deleted. Detect this case and ignore samples arriving when the evlist is being deleted.

Fixes bcfab08db7fb ("perf intel-tpebs: Filter non-workload samples")
---
 tools/perf/util/intel-tpebs.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
tools/perf/util/intel-tpebs.c +1 -2
index 4ad4bc118ea5..3b92ebf5c112 100644
@@ -162,9 +162,17 @@ static bool is_child_pid(pid_t parent, pid_t child)
 
 static bool should_ignore_sample(const struct perf_sample *sample, const struct tpebs_retire_lat *t)
 {
-     pid_t workload_pid = t->evsel->evlist->workload.pid;
-     pid_t sample_pid = sample->pid;
+     pid_t workload_pid, sample_pid = sample->pid;
 
Quoted 12 lines

tools/perf/util/intel-tpebs.c static bool is_child_pid(pid_t parent, pid_t child) · line 175

Quoted 9 lines
tools/perf/util/intel-tpebs.c +9 -0
+     /*
+      * During evlist__purge the evlist will be removed prior to the
+      * evsel__exit calling evsel__tpebs_close and taking the
+      * tpebs_mtx. Avoid a segfault by ignoring samples in this case.
+      */
+     if (t->evsel->evlist == NULL)
+             return true;
+
+     workload_pid = t->evsel->evlist->workload.pid;

I'm curious if there's a chance of TOCTOU race. It'd certainly help the segfault but would this code prevent it completely?

Quoted 10 lines

Good point. I think the race is already small as it doesn't happen without sanitizers for me.
Thinking about the evlist problem. When a destructor (evlist__delete) it is generally assumed the code is being single threaded and in C++ clang's -Wthread-safety will ignore destructors for this reason (annoying imo as it hides bugs). I don't see a good way to solve that for the evlist and evsel for the TPEBS case without using reference counting. Adding reference counts to evlist and evsel would be do-able as we could use reference count checking, but it would be a large and invasive change. Wdyt?

Quoted 1 line

Would it be possible to kill the TPEBS thread before deleting evlist?

Quoted 8 lines

The TPEBS thread and other data structures are global and not tied to the evlist, so there can and are multiple evlists at play. When using TPEBS there is the evlist for perf stat, there is also the evlist for the samples. There's sense in having the evlist own the TPEBS data structures, there's also sense in things being global. I think if I'd done it I'd have gone with TPEBS within the evlist, but I suspect in the original changes there was a worry about adding cost on non-x86 builds.

Ok, I thought deleting evlist is quite late in the execution and it may be easy to make the change. If not, let's see how the current fix will work before going further. :)

Thanks,
Namhyung

author reviewer
#10 lore raw

PATCH v2 5/7 perf test intel-pt: Skip jitdump test if no libelf

In reply to Ian Rogers

jitdump support is only present if building with libelf. Skip the intel-pt jitdump test if perf isn't compiled with libelf support.

---
 tools/perf/tests/shell/test_intel_pt.sh | 5 +++++
 1 file changed, 5 insertions(+)
tools/perf/tests/shell/test_intel_pt.sh +5 -0
index f3a9a040bacc..32a9b8dcb200 100755
@@ -288,6 +288,11 @@ test_jitdump()
 	jitdump_incl_dir="${script_dir}/../../util"
 	jitdump_h="${jitdump_incl_dir}/jitdump.h"
 
+        if ! perf check feature -q libelf ; then
+		echo "SKIP: libelf is needed for jitdump"
+		return 2
+	fi
+
 	if [ ! -e "${jitdump_h}" ] ; then
 		echo "SKIP: Include file jitdump.h not found"
 		return 2
Signature · 1 line
2.49.0.1238.gf8c92423fb-goog
author reviewer
#11 lore raw

PATCH v2 6/7 perf test trace_summary: Skip --bpf-summary tests if no libbpf

In reply to Ian Rogers

If perf is built without libbpf (e.g. NO_LIBBPF=1) then the --bpf-summary perf trace tests will fail. Skip the tests as this is expected behavior.

---
 tools/perf/tests/shell/trace_summary.sh | 6 ++++++
 1 file changed, 6 insertions(+)
tools/perf/tests/shell/trace_summary.sh +6 -0
index 49766524dc21..f9bb7f9388be 100755
@@ -53,6 +53,12 @@ test_perf_trace "-as --summary-mode=thread --no-bpf-summary"
 # summary only for system wide - total summary mode
 test_perf_trace "-as --summary-mode=total --no-bpf-summary"
 
+if ! perf check feature -q bpf; then
+    echo "Skip --bpf-summary tests as perf built without libbpf"
+    rm -f ${OUTPUT}
+    exit 2
+fi
+
 # summary only for system wide - per-thread summary with BPF
 test_perf_trace "-as --summary-mode=thread --bpf-summary"
 
Signature · 1 line
2.49.0.1238.gf8c92423fb-goog
Acked-by on v2 6/7
#12 lore raw

Re: [PATCH v2 6/7] perf test trace_summary: Skip --bpf-summary tests if no libbpf

Hello Ian,

Quoted 6 lines · Ian Rogers

If perf is built without libbpf (e.g. NO_LIBBPF=1) then the --bpf-summary perf trace tests will fail. Skip the tests as this is expected behavior.

Thanks,
Howard

maintainer Applied
#13 lore raw

Re: [PATCH v2 6/7] perf test trace_summary: Skip --bpf-summary tests if no libbpf

Quoted 11 lines · Howard Chu

Hello Ian,

Quoted 6 lines · Ian Rogers

If perf is built without libbpf (e.g. NO_LIBBPF=1) then the --bpf-summary perf trace tests will fail. Skip the tests as this is expected behavior.

Thanks, applied to perf-tools-next,

BTW, my answers were for v1 but b4 gets v2, for instance, the one that is failing:

⬢ [acme@toolbx perf-tools-next]$ b4 am -P3 -ctsl --cc-trailers [email protected] Grabbing thread from lore.kernel.org/all/[email protected]/t.mbox.gz Breaking thread to remove parents of [email protected]
Checking for newer revisions
Grabbing search results from lore.kernel.org
Added from v2: 8 patches
Analyzing 21 messages in the thread
Analyzing 0 code-review messages
Will use the latest revision: v2
You can pick other revisions using the -vN flag
Unknown range value specified: 3
Checking attestation on all messages, may take a moment...
---
✓ [PATCH v2 3/7] perf symbol: Move demangling code out of symbol-elf.c
+ Link: https://lore.kernel.org/r/[email protected]
+ Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
✓ Signed: DKIM/google.com
---
Total patches: 1 (cherrypicked: 3)
---
Cover: ./v2_20250527_irogers_various_asan_and_test_fixes.cover

Base: not specified

       git am ./v2_20250527_irogers_various_asan_and_test_fixes.mbx

⬢ [acme@toolbx perf-tools-next]$

⬢ [acme@toolbx perf-tools-next]$ git am ./v2_20250527_irogers_various_asan_and_test_fixes.mbx
Applying: perf symbol: Move demangling code out of symbol-elf.c ⬢ [acme@toolbx perf-tools-next]$

⬢ [acme@toolbx perf-tools-next]$ m
rm: cannot remove '/home/acme/libexec/perf-core/scripts/python/Perf-Trace-Util/lib/Perf/Trace/__pycache__/Core.cpython-313.pyc': Permission denied
make: Entering directory '/home/acme/git/perf-tools-next/tools/perf'
BUILD: Doing 'make -j32' parallel build
Warning: Kernel ABI header differences:
diff -u tools/arch/arm64/include/asm/cputype.h arch/arm64/include/asm/cputype.h

Auto-detecting system features:

...                                   libdw: [ on  ]
...                                   glibc: [ on  ]
...                                  libelf: [ on  ]
...                                 libnuma: [ on  ]
...                  numa_num_possible_cpus: [ on  ]
...                                 libperl: [ on  ]
...                               libpython: [ on  ]
...                               libcrypto: [ on  ]
...                             libcapstone: [ on  ]
...                               llvm-perf: [ on  ]
...                                    zlib: [ on  ]
...                                    lzma: [ on  ]
...                               get_cpuid: [ on  ]
...                                     bpf: [ on  ]
...                                  libaio: [ on  ]
...                                 libzstd: [ on  ]

INSTALL libsubcmd_headers
INSTALL libperf_headers
INSTALL libapi_headers
INSTALL libsymbol_headers
PERF_VERSION = 6.15.rc7.g50ec05a72bc1
LINK /tmp/build/perf-tools-next/libperf-jvmti.so
INSTALL libbpf_headers

  GEN     perf-archive
  GEN     perf-iostat
  CC      /tmp/build/perf-tools-next/util/symbol.o
  CC      /tmp/build/perf-tools-next/util/header.o
  CC      /tmp/build/perf-tools-next/util/symbol-elf.o
  CXX     /tmp/build/perf-tools-next/util/demangle-cxx.o
util/symbol-elf.c: In function ‘get_plt_got_name’:
util/symbol-elf.c:563:21: error: implicit declaration of function ‘demangle_sym’; did you mean ‘dso__demangle_sym’? [-Wimplicit-function-declaration]
  563 |         demangled = demangle_sym(di->dso, 0, sym_name);
      |                     ^~~~~~~~~~~~
      |                     dso__demangle_sym

util/symbol-elf.c:563:19: error: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
563 | demangled = demangle_sym(di->dso, 0, sym_name);
| ^
util/symbol-elf.c: In function ‘dso__synthesize_plt_symbols’:
util/symbol-elf.c:761:27: error: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
761 | demangled = demangle_sym(dso, 0, elf_name);
| ^
util/symbol-elf.c: In function ‘dso__load_sym_internal’:
util/symbol-elf.c:1778:27: error: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
1778 | demangled = demangle_sym(dso, kmodule, elf_name);
| ^
make[4]: *** [/home/acme/git/perf-tools-next/tools/build/Makefile.build:85: /tmp/build/perf-tools-next/util/symbol-elf.o] Error 1 make[4]: *** Waiting for unfinished jobs....
make[3]: *** [/home/acme/git/perf-tools-next/tools/build/Makefile.build:142: util] Error 2
make[2]: *** [Makefile.perf:798: /tmp/build/perf-tools-next/perf-util-in.o] Error 2
make[1]: *** [Makefile.perf:290: sub-make] Error 2
make: *** [Makefile:119: install-bin] Error 2
make: Leaving directory '/home/acme/git/perf-tools-next/tools/perf'
⬢ [acme@toolbx perf-tools-next]$

I can try fixing those up, but I'm concentrated now in getting as much low hanging fruits as possible for this merge, and the patches in this series were mostly super small and fixed things.

- Arnaldo

author reviewer
#14 lore raw

PATCH v2 7/7 perf thread: Avoid recursively taking thread__comm_lock

In reply to Ian Rogers

Fix cases where functions that take thread_comm_lcok were called holding thread__comm_lock. This created some buggy behavior in perf top when built with sanitizers. Ensure -Wthread-safety warnings for clang work.

Fixes 8f454c95817d ("perf thread: Ensure comm_lock held for comm_list")
---
 tools/perf/util/thread.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
tools/perf/util/thread.c +13 -4
index c202b98b36c2..f4ad15e1e314 100644
@@ -201,13 +201,21 @@ int thread__set_namespaces(struct thread *thread, u64 timestamp,
 	return ret;
 }
 
+static struct comm *__thread__comm(struct thread *thread)
+	SHARED_LOCKS_REQUIRED(thread__comm_lock(thread))
+{
+	if (list_empty(thread__comm_list(thread)))
+		return NULL;
+
+	return list_first_entry(thread__comm_list(thread), struct comm, list);
+}
+
 struct comm *thread__comm(struct thread *thread)
 {
 	struct comm *res = NULL;
 
 	down_read(thread__comm_lock(thread));
-	if (!list_empty(thread__comm_list(thread)))
-		res = list_first_entry(thread__comm_list(thread), struct comm, list);
+	res = __thread__comm(thread);
 	up_read(thread__comm_lock(thread));
 	return res;
 }
@@ -243,7 +251,7 @@ static int ____thread__set_comm(struct thread *thread, const char *str,
 				u64 timestamp, bool exec)
 	EXCLUSIVE_LOCKS_REQUIRED(thread__comm_lock(thread))
 {
-	struct comm *new, *curr = thread__comm(thread);
+	struct comm *new, *curr = __thread__comm(thread);
 
 	/* Override the default :tid entry */
 	if (!thread__comm_set(thread)) {
@@ -294,8 +302,9 @@ int thread__set_comm_from_proc(struct thread *thread)
 }
 
 static const char *__thread__comm_str(struct thread *thread)
+	SHARED_LOCKS_REQUIRED(thread__comm_lock(thread))
 {
-	const struct comm *comm = thread__comm(thread);
+	const struct comm *comm = __thread__comm(thread);
 
 	if (!comm)
 		return NULL;
Signature · 1 line
2.49.0.1238.gf8c92423fb-goog
maintainer Acked-by on the v2 series
#15 lore raw
In reply to Ian Rogers
Quoted 8 lines · Ian Rogers

When testing removing perf_env with asan I noticed a number of test failures either because of asan issues or because of building with NO_LIBELF=1 and NO_LIBBPF=1 to avoid false memory leaks. Address these issues so that real test failures stand out.

v2: Fix wrong elf size use in filename__read_build_id fix. Add
thread__comm_lock fix.

Quoted 8 lines

Ian Rogers (7):
perf symbol: Fix use-after-free in filename__read_build_id perf test demangle-java: Don't segv if demangling fails perf symbol: Move demangling code out of symbol-elf.c perf intel-tpebs: Avoid race when evlist is being deleted perf test intel-pt: Skip jitdump test if no libelf perf test trace_summary: Skip --bpf-summary tests if no libbpf perf thread: Avoid recursively taking thread__comm_lock

Thanks,
Namhyung

Quoted 15 lines
 tools/perf/tests/demangle-java-test.c   |   5 +
 tools/perf/tests/shell/test_intel_pt.sh |   5 +
 tools/perf/tests/shell/trace_summary.sh |   6 +
 tools/perf/util/demangle-cxx.h          |   2 +
 tools/perf/util/intel-tpebs.c           |  12 +-
 tools/perf/util/symbol-elf.c            |  86 ------------
 tools/perf/util/symbol-minimal.c        | 175 ++++++++++--------------
 tools/perf/util/symbol.c                |  87 ++++++++++++
 tools/perf/util/thread.c                |  17 ++-
 9 files changed, 198 insertions(+), 197 deletions(-)
Signature · 1 line
2.49.0.1238.gf8c92423fb-goog