Skip to contentSkip to conversation

perf symbol: Reference counting, flat array storage, and LRU shrinking

PATCH In review Ian Rogers 5 participants 146 messages Sep 28 – Nov 16 perf events Page 2 of 3

lore
Share & tools
lore
author reviewer
#35 lore raw

PATCH v2 11/18 perf symbol-elf: Cache ELF program/section headers and defer symbol name lookup

In reply to Ian Rogers

Profiling dso__load_sym_internal() on large binaries showed significant time spent in repeated libelf section and program header lookups (elf_section_by_idx() and elf_read_program_header()) for each symbol, resolving elf_sym__name() before filtering out non-symbol/non-allocated entries, and running symbols__fixup_end() and symbols__fixup_duplicate() twice when both .symtab and .dynsym are loaded.

Speed up ELF symbol table loading in dso__load_sym_internal() and dso__load_sym():

- Pre-load PT_LOAD program headers into struct elf_phdr_cache and cache per-section metadata in a 64-entry direct-mapped table in dso__load_sym_internal() to avoid repeated libelf section and program header lookups during symbol iteration.
- Defer elf_sym__name() lookup until after non-allocated and non-symbol entries have been filtered out, and run symbols__fixup_end() and symbols__fixup_duplicate() once in dso__load_sym() after loading both .symtab and .dynsym.

Using 'perf bench internals symbols':

Before (perf binary):

    Average DSO load + sort: 5.808 msec (+- 0.047 msec)
  After (perf binary, -8.3%):
    Average DSO load + sort: 5.324 msec (+- 0.045 msec)

Before (perf binary, --no-demangle):

    Average DSO load + sort: 3.948 msec (+- 0.064 msec)
  After (perf binary, --no-demangle, -13.7%):
    Average DSO load + sort: 3.408 msec (+- 0.034 msec)

Before (libLLVM.so.21.1, --no-demangle):

    Average DSO load + sort: 22.433 msec (+- 0.260 msec)
  After (libLLVM.so.21.1, --no-demangle, -14.8%):
    Average DSO load + sort: 19.122 msec (+- 0.226 msec)
---
 tools/perf/util/symbol-elf.c | 275 +++++++++++++++++++++++++++--------
 1 file changed, 217 insertions(+), 58 deletions(-)
tools/perf/util/symbol-elf.c +217 -58
index 4023174ed6db..6082136c9662 100644
@@ -265,6 +265,118 @@ static int elf_read_program_header(Elf *elf, u64 vaddr, GElf_Phdr *phdr)
 	return -1;
 }
 
+struct elf_phdr_range {
+	u64 vaddr;
+	u64 end;
+	u64 adjust;
+	GElf_Phdr phdr;
+};
+
+struct elf_phdr_cache {
+	struct elf_phdr_range inline_ranges[16];
+	struct elf_phdr_range *ranges;
+	size_t nr_ranges;
+	size_t last_idx;
+};
+
+static void elf_phdr_cache__init(struct elf_phdr_cache *cache, Elf *elf)
+{
+	size_t i, phdrnum = 0, cnt = 0;
+
+	cache->ranges = cache->inline_ranges;
+	cache->nr_ranges = 0;
+	cache->last_idx = 0;
+
+	if (!elf || elf_getphdrnum(elf, &phdrnum) || phdrnum == 0)
+		return;
+
+	if (phdrnum > ARRAY_SIZE(cache->inline_ranges)) {
+		cache->ranges = calloc(phdrnum, sizeof(*cache->ranges));
+		if (!cache->ranges) {
+			cache->ranges = cache->inline_ranges;
+			phdrnum = ARRAY_SIZE(cache->inline_ranges);
+		}
+	}
+
+	for (i = 0; i < phdrnum; i++) {
+		GElf_Phdr phdr;
+		u64 sz;
+
+		if (gelf_getphdr(elf, i, &phdr) == NULL)
+			break;
+		if (phdr.p_type != PT_LOAD)
+			continue;
+		sz = max(phdr.p_memsz, phdr.p_filesz);
+		if (!sz)
+			continue;
+		cache->ranges[cnt].vaddr = phdr.p_vaddr;
+		cache->ranges[cnt].end = phdr.p_vaddr + sz;
+		cache->ranges[cnt].adjust = phdr.p_vaddr - phdr.p_offset;
+		cache->ranges[cnt].phdr = phdr;
+		cnt++;
+	}
+	cache->nr_ranges = cnt;
+}
+
+static void elf_phdr_cache__exit(struct elf_phdr_cache *cache)
+{
+	if (cache->ranges && cache->ranges != cache->inline_ranges)
+		free(cache->ranges);
+}
+
+static const struct elf_phdr_range *
+elf_phdr_cache__find(struct elf_phdr_cache *cache, u64 vaddr)
+{
+	if (cache->nr_ranges == 0)
+		return NULL;
+
+	if (cache->last_idx < cache->nr_ranges) {
+		const struct elf_phdr_range *r = &cache->ranges[cache->last_idx];
+
+		if (vaddr >= r->vaddr && vaddr < r->end)
+			return r;
+	}
+	for (size_t i = 0; i < cache->nr_ranges; i++) {
+		const struct elf_phdr_range *r = &cache->ranges[i];
+
+		if (vaddr >= r->vaddr && vaddr < r->end) {
+			cache->last_idx = i;
+			return r;
+		}
+	}
+	return NULL;
+}
+
+#define ELF_SEC_CACHE_SIZE 64
+
+struct elf_sec_cache_entry {
+	uint32_t shndx;
+	bool valid;
+	bool is_alloc;
+	bool is_label_sec;
+	bool has_phdr;
+	u64 phdr_vaddr;
+	u64 phdr_end;
+	const char *section_name;
+	GElf_Shdr shdr;
+	GElf_Phdr phdr;
+};
+
+static inline const GElf_Phdr *
+elf_sec_cache__phdr(const struct elf_sec_cache_entry *sce,
+		    struct elf_phdr_cache *phdr_cache,
+		    u64 vaddr)
+{
+	const struct elf_phdr_range *pr;
+
+	if (sce->has_phdr && vaddr >= sce->phdr_vaddr && vaddr < sce->phdr_end)
+		return &sce->phdr;
+	pr = elf_phdr_cache__find(phdr_cache, vaddr);
+	if (!pr)
+		return NULL;
+	return &pr->phdr;
+}
+
 struct rel_info {
 	u32		nr_entries;
 	u32		*sorted;
@@ -1543,6 +1655,8 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 	int nr = 0;
 	bool remap_kernel = false, adjust_kernel_syms = false;
 	u64 max_text_sh_offset = 0;
+	struct elf_phdr_cache phdr_cache = {};
+	struct elf_sec_cache_entry sec_cache[ELF_SEC_CACHE_SIZE];
 
 	if (kmap && !kmaps)
 		return -1;
@@ -1634,14 +1748,18 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 	if (kmodule && adjust_kernel_syms)
 		max_text_sh_offset = max_text_section(runtime_ss->elf, &runtime_ss->ehdr);
 
+	elf_phdr_cache__init(&phdr_cache, runtime_ss->elf);
+	memset(sec_cache, 0, sizeof(sec_cache));
+
 	curr_dso = dso__get(dso);
 	elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
 		struct symbol *f;
-		const char *elf_name = elf_sym__name(&sym, symstrs);
+		const char *elf_name = NULL;
 		char *demangled = NULL;
 		int is_label = elf_sym__is_label(&sym);
 		const char *section_name;
 		bool used_opd = false;
+		struct elf_sec_cache_entry *sce;
 
 		if (!is_label && !elf_sym__filter(&sym))
 			continue;
@@ -1652,6 +1770,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		 * output:
 		 */
 		if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
+			elf_name = elf_sym__name(&sym, symstrs);
 			if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
 			    && (elf_name[2] == '\0' || elf_name[2] == '.'))
 				continue;
@@ -1659,13 +1778,19 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 
 		/* Reject RISCV ELF "mapping symbols" */
 		if (ehdr.e_machine == EM_RISCV) {
+			if (!elf_name)
+				elf_name = elf_sym__name(&sym, symstrs);
 			if (elf_name[0] == '$' && strchr("dx", elf_name[1]))
 				continue;
 		}
 
 		/* Reject kernel mapping symbols for kernel DSOs only */
-		if (dso__kernel(dso) && is_ignored_kernel_symbol(elf_name))
-			continue;
+		if (dso__kernel(dso)) {
+			if (!elf_name)
+				elf_name = elf_sym__name(&sym, symstrs);
+			if (is_ignored_kernel_symbol(elf_name))
+				continue;
+		}
 
 		if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
 			u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
@@ -1688,16 +1813,56 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		if (sym.st_shndx == SHN_ABS)
 			continue;
 
-		sec = elf_getscn(syms_ss->elf, sym.st_shndx);
-		if (!sec) {
-			if (dynsym && ehdr.e_shnum &&
-			    sym.st_shndx < SHN_LORESERVE &&
-			    sym.st_shndx >= ehdr.e_shnum)
-				continue;
-			goto out_elf_end;
-		}
+		sce = &sec_cache[sym.st_shndx & (ELF_SEC_CACHE_SIZE - 1)];
+		if (!sce->valid || sce->shndx != sym.st_shndx) {
+			const struct elf_phdr_range *pr;
 
-		gelf_getshdr(sec, &shdr);
+			sec = elf_getscn(syms_ss->elf, sym.st_shndx);
+			if (!sec) {
+				if (dynsym && ehdr.e_shnum &&
+				    sym.st_shndx < SHN_LORESERVE &&
+				    sym.st_shndx >= ehdr.e_shnum)
+					continue;
+				goto out_elf_end;
+			}
+
+			gelf_getshdr(sec, &shdr);
+			secstrs = secstrs_sym;
+
+			/*
+			 * We have to fallback to runtime when syms' section
+			 * header has NOBITS set. NOBITS results in file offset
+			 * (sh_offset) not being incremented. So sh_offset used
+			 * below has different values for syms (invalid) and
+			 * runtime (valid).
+			 */
+			if (shdr.sh_type == SHT_NOBITS && (shdr.sh_flags & SHF_ALLOC)) {
+				sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
+				if (!sec)
+					goto out_elf_end;
+
+				gelf_getshdr(sec, &shdr);
+				secstrs = secstrs_run;
+			}
+
+			sce->shndx = sym.st_shndx;
+			sce->valid = true;
+			sce->is_alloc = (shdr.sh_flags & SHF_ALLOC) != 0;
+			if (sce->is_alloc) {
+				sce->shdr = shdr;
+				sce->section_name = elf_sec__name(&shdr, secstrs);
+				sce->is_label_sec = elf_sec__filter(&shdr, secstrs) != 0;
+				pr = elf_phdr_cache__find(&phdr_cache, shdr.sh_addr);
+				if (pr) {
+					sce->has_phdr = true;
+					sce->phdr_vaddr = pr->vaddr;
+					sce->phdr_end = pr->end;
+					sce->phdr = pr->phdr;
+				} else {
+					sce->has_phdr = false;
+				}
+			}
+		}
 
 		/*
 		 * If the attribute bit SHF_ALLOC is not set, the section
@@ -1707,30 +1872,13 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		 * the section aren't loaded to memory during process execution,
 		 * so skip them.
 		 */
-		if (!(shdr.sh_flags & SHF_ALLOC))
+		if (!sce->is_alloc)
 			continue;
 
-		secstrs = secstrs_sym;
-
-		/*
-		 * We have to fallback to runtime when syms' section header has
-		 * NOBITS set. NOBITS results in file offset (sh_offset) not
-		 * being incremented. So sh_offset used below has different
-		 * values for syms (invalid) and runtime (valid).
-		 */
-		if (shdr.sh_type == SHT_NOBITS) {
-			sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
-			if (!sec)
-				goto out_elf_end;
-
-			gelf_getshdr(sec, &shdr);
-			secstrs = secstrs_run;
-		}
-
-		if (is_label && !elf_sec__filter(&shdr, secstrs))
+		if (is_label && !sce->is_label_sec)
 			continue;
 
-		section_name = elf_sec__name(&shdr, secstrs);
+		section_name = sce->section_name;
 
 		/* On ARM, symbols for thumb functions have 1 added to
 		 * the symbol address as a flag - remove it */
@@ -1740,7 +1888,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 			--sym.st_value;
 
 		if (dso__kernel(dso)) {
-			if (dso__process_kernel_symbol(dso, map, &sym, &shdr,
+			if (dso__process_kernel_symbol(dso, map, &sym, &sce->shdr,
 						       kmaps, kmap, &curr_dso,
 						       section_name,
 						       adjust_kernel_syms,
@@ -1750,17 +1898,20 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 				goto out_elf_end;
 		} else if ((used_opd && runtime_ss->adjust_symbols) ||
 			   (!used_opd && syms_ss->adjust_symbols)) {
-			GElf_Phdr phdr;
+			const GElf_Phdr *phdr;
 
-			if (elf_read_program_header(runtime_ss->elf,
-						    (u64)sym.st_value, &phdr)) {
+			phdr = elf_sec_cache__phdr(sce, &phdr_cache,
+						   (u64)sym.st_value);
+			if (!phdr) {
 				pr_debug4("%s: failed to find program header for "
 					   "symbol: %s st_value: %#" PRIx64 "\n",
-					   __func__, elf_name, (u64)sym.st_value);
+					   __func__, elf_sym__name(&sym, symstrs),
+					   (u64)sym.st_value);
 				pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
 					"sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n",
-					__func__, (u64)sym.st_value, (u64)shdr.sh_addr,
-					(u64)shdr.sh_offset);
+					__func__, (u64)sym.st_value,
+					(u64)sce->shdr.sh_addr,
+					(u64)sce->shdr.sh_offset);
 				/*
 				 * Fail to find program header, let's rollback
 				 * to use shdr.sh_addr and shdr.sh_offset to
@@ -1769,16 +1920,18 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 				 * still need to handle java JIT symbols in this
 				 * case.
 				 */
-				sym.st_value -= shdr.sh_addr - shdr.sh_offset;
+				sym.st_value -= sce->shdr.sh_addr - sce->shdr.sh_offset;
 			} else {
 				pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
 					"p_vaddr: %#" PRIx64 " p_offset: %#" PRIx64 "\n",
-					__func__, (u64)sym.st_value, (u64)phdr.p_vaddr,
-					(u64)phdr.p_offset);
-				sym.st_value -= phdr.p_vaddr - phdr.p_offset;
+					__func__, (u64)sym.st_value, (u64)phdr->p_vaddr,
+					(u64)phdr->p_offset);
+				sym.st_value -= phdr->p_vaddr - phdr->p_offset;
 			}
 		}
 
+		if (!elf_name)
+			elf_name = elf_sym__name(&sym, symstrs);
 		demangled = dso__demangle_sym(dso, kmodule, elf_name);
 		if (demangled != NULL)
 			elf_name = demangled;
@@ -1796,25 +1949,13 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 			goto out_elf_end;
 		nr++;
 	}
-	dso__put(curr_dso);
 
-	/*
-	 * For misannotated, zeroed, ASM function sizes.
-	 */
-	if (nr > 0) {
-		symbols__fixup_end(dso__symbols(dso), false);
-		symbols__fixup_duplicate(dso__symbols(dso));
-		if (kmap) {
-			/*
-			 * We need to fixup this here too because we create new
-			 * maps here, for things like vsyscall sections.
-			 */
-			maps__fixup_end(kmaps);
-		}
-	}
+	dso__put(curr_dso);
+	elf_phdr_cache__exit(&phdr_cache);
 	return nr;
 out_elf_end:
 	dso__put(curr_dso);
+	elf_phdr_cache__exit(&phdr_cache);
 	return -1;
 }
 
@@ -1872,6 +2013,24 @@ int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		nr += err;
 	}
 
+	/*
+	 * For misannotated, zeroed, ASM function sizes.
+	 */
+	if (nr > 0) {
+		struct kmap *kmap = dso__kernel(dso) ? map__kmap(map) : NULL;
+		struct maps *kmaps = kmap ? map__kmaps(map) : NULL;
+
+		symbols__fixup_end(dso__symbols(dso), false);
+		symbols__fixup_duplicate(dso__symbols(dso));
+		if (kmap && kmaps) {
+			/*
+			 * We need to fixup this here too because we create new
+			 * maps here, for things like vsyscall sections.
+			 */
+			maps__fixup_end(kmaps);
+		}
+	}
+
 	return nr;
 }
 
Signature · 1 line
2.56.0.313.g2479322e68-goog
#36 lore raw

Re: [PATCH v2 11/18] perf symbol-elf: Cache ELF program/section headers and defer symbol name lookup

Quoted 9 lines · Ian Rogers

+ /*
+ * For misannotated, zeroed, ASM function sizes.
+ */
+ if (nr > 0) {
+ struct kmap *kmap = dso__kernel(dso) ? map__kmap(map) : NULL;
+ struct maps *kmaps = kmap ? map__kmaps(map) : NULL; +
+ symbols__fixup_end(dso__symbols(dso), false);
+ symbols__fixup_duplicate(dso__symbols(dso));

Could the change to run these fixups once per load be split into its own patch, with an explanation and a test? It changes symbol resolution, not only the amount of work.

Before this change, a zero-size .symtab symbol ended at the next .symtab symbol, so it could cover a function that only exists in .dynsym. Running the fixups after both tables have loaded instead ends it at the next symbol from either table.

I reproduced this with a small library where zfun has no .size. The next function in address order is efun, which I removed from .symtab with objcopy --strip-symbol=efun but left exported in .dynsym. The same sample is then attributed differently:

before: 99.97% libz.so [.] zfun
after: 99.97% libz.so [.] efun

The new answer looks right to me. Minidebuginfo, where exports may exist only in .dynsym, should hit the same pattern and would be useful test coverage for the separate patch.

Thanks,
Alireza

author reviewer
#37 lore raw

PATCH v2 12/18 perf rwsem: Add down_read_trylock() and down_write_trylock()

In reply to Ian Rogers

On-demand memory shrinking (perf_session__shrink(), dsos__shrink(), symbols__shrink()) can be triggered from OOM recovery inside allocation wrappers while the current thread already holds a rw_semaphore (such as symbols->lock, dsos->lock, or machines->lock). Non-blocking trylock primitives are needed so shrinking paths can safely skip any locked container without deadlocking.

Add non-blocking down_read_trylock() and down_write_trylock() wrappers around pthread_rwlock_tryrdlock() / pthread_rwlock_trywrlock() and pthread_mutex_trylock() along with SHARED_TRYLOCK_FUNCTION and EXCLUSIVE_TRYLOCK_FUNCTION thread-safety annotations.

---
 tools/perf/util/mutex.h |  3 +++
 tools/perf/util/rwsem.c | 20 ++++++++++++++++++++
 tools/perf/util/rwsem.h |  2 ++
 3 files changed, 25 insertions(+)
tools/perf/util/mutex.h +3 -0
index 5de1065ea2b9..de7dd6d8326c 100644
@@ -57,6 +57,8 @@
 /* Documents functions that try to acquire a lock, and return success or failure. */
 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) \
 	__attribute__((exclusive_trylock_function(__VA_ARGS__)))
+#define SHARED_TRYLOCK_FUNCTION(...) \
+	__attribute__((shared_trylock_function(__VA_ARGS__)))
 
 /* Documents a function that expects a mutex to be held prior to entry. */
 #define EXCLUSIVE_LOCKS_REQUIRED(...) __attribute__((exclusive_locks_required(__VA_ARGS__)))
@@ -78,6 +80,7 @@
 #define SHARED_LOCK_FUNCTION(...)
 #define UNLOCK_FUNCTION(...)
 #define EXCLUSIVE_TRYLOCK_FUNCTION(...)
+#define SHARED_TRYLOCK_FUNCTION(...)
 #define EXCLUSIVE_LOCKS_REQUIRED(...)
 #define SHARED_LOCKS_REQUIRED(...)
 #define NO_THREAD_SAFETY_ANALYSIS
tools/perf/util/rwsem.c +20 -0
index 9d26832398db..37fa08bae5e9 100644
@@ -37,6 +37,16 @@ int down_read(struct rw_semaphore *sem)
 #endif
 }
 
+bool down_read_trylock(struct rw_semaphore *sem)
+	NO_THREAD_SAFETY_ANALYSIS
+{
+#if RWS_ERRORCHECK
+	return mutex_trylock(&sem->mtx);
+#else
+	return perf_singlethreaded ? true : pthread_rwlock_tryrdlock(&sem->lock) == 0;
+#endif
+}
+
 int up_read(struct rw_semaphore *sem)
 	NO_THREAD_SAFETY_ANALYSIS
 {
@@ -59,6 +69,16 @@ int down_write(struct rw_semaphore *sem)
 #endif
 }
 
+bool down_write_trylock(struct rw_semaphore *sem)
+	NO_THREAD_SAFETY_ANALYSIS
+{
+#if RWS_ERRORCHECK
+	return mutex_trylock(&sem->mtx);
+#else
+	return perf_singlethreaded ? true : pthread_rwlock_trywrlock(&sem->lock) == 0;
+#endif
+}
+
 int up_write(struct rw_semaphore *sem)
 	NO_THREAD_SAFETY_ANALYSIS
 {
tools/perf/util/rwsem.h +2 -0
index b102d8143181..782016c3e01f 100644
@@ -22,9 +22,11 @@ int init_rwsem(struct rw_semaphore *sem);
 int exit_rwsem(struct rw_semaphore *sem);
 
 int down_read(struct rw_semaphore *sem) SHARED_LOCK_FUNCTION(sem);
+bool down_read_trylock(struct rw_semaphore *sem) SHARED_TRYLOCK_FUNCTION(true, sem);
 int up_read(struct rw_semaphore *sem) UNLOCK_FUNCTION(sem);
 
 int down_write(struct rw_semaphore *sem) EXCLUSIVE_LOCK_FUNCTION(sem);
+bool down_write_trylock(struct rw_semaphore *sem) EXCLUSIVE_TRYLOCK_FUNCTION(true, sem);
 int up_write(struct rw_semaphore *sem) UNLOCK_FUNCTION(sem);
 
 #endif /* _PERF_RWSEM_H */
Signature · 1 line
2.56.0.313.g2479322e68-goog
#38 lore raw

Re: [PATCH v2 12/18] perf rwsem: Add down_read_trylock() and down_write_trylock()

Quoted 3 lines · Ian Rogers

symbols->lock, dsos->lock, or machines->lock). Non-blocking trylock primitives are needed so shrinking paths can safely skip any locked container without deadlocking.

[...]

Quoted 1 line

+ return perf_singlethreaded ? true : pthread_rwlock_trywrlock(&sem->lock) == 0;

Could the single-threaded trylock path detect when the current thread already holds the lock, rather than unconditionally succeeding? One option would be to track lock ownership or nesting depth. Otherwise, the shrink path needs to avoid any container that is currently being changed. In the full series, the unconditional success can make shrinking free an array that its caller is about to pass back to realloc().

perf_singlethreaded remains true in perf report, perf script, and most commands. perf top, multi-threaded synthesis in perf record, and perf bench synthesize explicitly switch it. In report and script these trylocks therefore always succeed, even on a lock the current thread already holds. The shrink paths can't skip a locked container there.

The case I hit: __symbols__insert_sorted(), reached through dso__insert_symbol() outside dso__load() (for example from db_ids_from_al()), holds symbols->lock for writing and calls

perf_session__realloc(session, symbols->symbols, ...)

This path combines 12/18 with the shrink and allocator plumbing in 15-17/18.

If that realloc fails, perf_session__shrink() reaches symbols__shrink() on the same container. The write trylock succeeds and, with every symbol at refcount 1, it frees symbols->symbols (or reallocs it smaller). perf_session__realloc() then retries realloc() with the old pointer. With an LD_PRELOAD shim that fails one realloc, ASan reports realloc() on freed memory from perf_session__realloc() <- __symbols__insert_sorted() <- dso__insert_symbol(). After perf_set_multithreaded() the trylock fails and the insert returns -ENOMEM, as intended.

Thanks,
Alireza

author reviewer
#39 lore raw

PATCH v2 13/18 perf vdso: Constify dso parameter in dso__is_vdso()

In reply to Ian Rogers

dso__can_shrink_symbols(const struct dso *dso) needs to query dso__is_vdso(dso) on a const struct dso pointer to exclude vDSO images from symbol shrinking.

Change the parameter of dso__is_vdso() from 'struct dso *' to 'const struct dso *'.

---
 tools/perf/util/vdso.c | 2 +-
 tools/perf/util/vdso.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
tools/perf/util/vdso.c +1 -1
index c12f5d8c4bf6..7f2dde70f3e4 100644
@@ -361,7 +361,7 @@ struct dso *machine__findnew_vdso(struct machine *machine,
 	return __machine__addnew_vdso(machine, DSO__NAME_VDSO, file);
 }
 
-bool dso__is_vdso(struct dso *dso)
+bool dso__is_vdso(const struct dso *dso)
 {
 	return !strcmp(dso__short_name(dso), DSO__NAME_VDSO) ||
 	       !strcmp(dso__short_name(dso), DSO__NAME_VDSO32) ||
tools/perf/util/vdso.h +1 -1
index bc74ace603ae..b6e05bc2d9f3 100644
@@ -19,7 +19,7 @@ static inline bool is_vdso_map(const char *filename)
 
 struct dso;
 
-bool dso__is_vdso(struct dso *dso);
+bool dso__is_vdso(const struct dso *dso);
 
 struct machine;
 struct thread;
Signature · 1 line
2.56.0.313.g2479322e68-goog
author reviewer
#40 lore raw

PATCH v2 14/18 perf machine: Protect machines->guests rbtree with a rw_semaphore

In reply to Ian Rogers

Unlike struct dsos, struct maps, and struct threads, struct machines had no lock protecting its machines->guests rbtree. Synchronizing access to machines->guests is needed both for concurrent guest machine lookups and so machines__shrink() can safely iterate guest machines using down_read_trylock(&machines->lock).

Add a rw_semaphore lock to struct machines to synchronize access to the machines->guests rbtree across machines__add(), machines__find(), machines__set_comm_exec(), machines__process_guests(), machines__set_id_hdr_size(), machines__fprintf_dsos(), machines__fprintf_dsos_buildid(), machines__destroy_kernel_maps(), and machines__for_each_thread(), and check for an existing guest machine PID under write lock in machines__add().

---
 tools/perf/util/machine.c | 42 +++++++++++++++++++++++++++++++++------
 tools/perf/util/machine.h |  1 +
 2 files changed, 37 insertions(+), 6 deletions(-)
tools/perf/util/machine.c +36 -6
index cb9e34220036..fb83da8ef09b 100644
@@ -237,6 +237,7 @@ int machines__init(struct machines *machines, struct perf_session *session)
 {
 	int err = machine__init(&machines->host, "", HOST_KERNEL_ID);
 
+	init_rwsem(&machines->lock);
 	machines->session = session;
 	machines->host.machines = machines;
 	machines->guests = RB_ROOT_CACHED;
@@ -247,12 +248,13 @@ void machines__exit(struct machines *machines)
 {
 	machine__exit(&machines->host);
 	/* XXX exit guest */
+	exit_rwsem(&machines->lock);
 }
 
 struct machine *machines__add(struct machines *machines, pid_t pid,
 			      const char *root_dir)
 {
-	struct rb_node **p = &machines->guests.rb_root.rb_node;
+	struct rb_node **p;
 	struct rb_node *parent = NULL;
 	struct machine *pos, *machine = malloc(sizeof(*machine));
 	bool leftmost = true;
@@ -265,9 +267,17 @@ struct machine *machines__add(struct machines *machines, pid_t pid,
 		return NULL;
 	}
 
+	down_write(&machines->lock);
+	p = &machines->guests.rb_root.rb_node;
 	while (*p != NULL) {
 		parent = *p;
 		pos = rb_entry(parent, struct machine, rb_node);
+		if (pid == pos->pid) {
+			up_write(&machines->lock);
+			machine__exit(machine);
+			free(machine);
+			return pos;
+		}
 		if (pid < pos->pid)
 			p = &(*p)->rb_left;
 		else {
@@ -278,8 +288,8 @@ struct machine *machines__add(struct machines *machines, pid_t pid,
 
 	rb_link_node(&machine->rb_node, parent, p);
 	rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
-
 	machine->machines = machines;
+	up_write(&machines->lock);
 
 	return machine;
 }
@@ -290,16 +300,18 @@ void machines__set_comm_exec(struct machines *machines, bool comm_exec)
 
 	machines->host.comm_exec = comm_exec;
 
+	down_read(&machines->lock);
 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
 		struct machine *machine = rb_entry(nd, struct machine, rb_node);
 
 		machine->comm_exec = comm_exec;
 	}
+	up_read(&machines->lock);
 }
 
 struct machine *machines__find(struct machines *machines, pid_t pid)
 {
-	struct rb_node **p = &machines->guests.rb_root.rb_node;
+	struct rb_node **p;
 	struct rb_node *parent = NULL;
 	struct machine *machine;
 	struct machine *default_machine = NULL;
@@ -307,6 +319,8 @@ struct machine *machines__find(struct machines *machines, pid_t pid)
 	if (pid == HOST_KERNEL_ID)
 		return &machines->host;
 
+	down_read(&machines->lock);
+	p = &machines->guests.rb_root.rb_node;
 	while (*p != NULL) {
 		parent = *p;
 		machine = rb_entry(parent, struct machine, rb_node);
@@ -314,11 +328,14 @@ struct machine *machines__find(struct machines *machines, pid_t pid)
 			p = &(*p)->rb_left;
 		else if (pid > machine->pid)
 			p = &(*p)->rb_right;
-		else
+		else {
+			up_read(&machines->lock);
 			return machine;
+		}
 		if (!machine->pid)
 			default_machine = machine;
 	}
+	up_read(&machines->lock);
 
 	return default_machine;
 }
@@ -464,10 +481,12 @@ void machines__process_guests(struct machines *machines,
 {
 	struct rb_node *nd;
 
+	down_read(&machines->lock);
 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
 		struct machine *pos = rb_entry(nd, struct machine, rb_node);
 		process(pos, data);
 	}
+	up_read(&machines->lock);
 }
 
 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
@@ -477,11 +496,13 @@ void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
 
 	machines->host.id_hdr_size = id_hdr_size;
 
+	down_read(&machines->lock);
 	for (node = rb_first_cached(&machines->guests); node;
 	     node = rb_next(node)) {
 		machine = rb_entry(node, struct machine, rb_node);
 		machine->id_hdr_size = id_hdr_size;
 	}
+	up_read(&machines->lock);
 
 	return;
 }
@@ -935,10 +956,12 @@ size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
 	struct rb_node *nd;
 	size_t ret = dsos__fprintf(&machines->host.dsos, fp);
 
+	down_read(&machines->lock);
 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
 		struct machine *pos = rb_entry(nd, struct machine, rb_node);
 		ret += dsos__fprintf(&pos->dsos, fp);
 	}
+	up_read(&machines->lock);
 
 	return ret;
 }
@@ -955,10 +978,12 @@ size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
 	struct rb_node *nd;
 	size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
 
+	down_read(&machines->lock);
 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
 		struct machine *pos = rb_entry(nd, struct machine, rb_node);
 		ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
 	}
+	up_read(&machines->lock);
 	return ret;
 }
 
@@ -1303,10 +1328,12 @@ int machines__create_guest_kernel_maps(struct machines *machines)
 
 void machines__destroy_kernel_maps(struct machines *machines)
 {
-	struct rb_node *next = rb_first_cached(&machines->guests);
+	struct rb_node *next;
 
 	machine__destroy_kernel_maps(&machines->host);
 
+	down_write(&machines->lock);
+	next = rb_first_cached(&machines->guests);
 	while (next) {
 		struct machine *pos = rb_entry(next, struct machine, rb_node);
 
@@ -1314,6 +1341,7 @@ void machines__destroy_kernel_maps(struct machines *machines)
 		rb_erase_cached(&pos->rb_node, &machines->guests);
 		machine__delete(pos);
 	}
+	up_write(&machines->lock);
 }
 
 int machines__create_kernel_maps(struct machines *machines, pid_t pid)
@@ -3134,13 +3162,15 @@ int machines__for_each_thread(struct machines *machines,
 	if (rc != 0)
 		return rc;
 
+	down_read(&machines->lock);
 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
 		struct machine *machine = rb_entry(nd, struct machine, rb_node);
 
 		rc = machine__for_each_thread(machine, fn, priv);
 		if (rc != 0)
-			return rc;
+			break;
 	}
+	up_read(&machines->lock);
 	return rc;
 }
 
tools/perf/util/machine.h +1 -0
index 973a59f7c26a..147ce343e370 100644
@@ -151,6 +151,7 @@ typedef void (*machine__process_t)(struct machine *machine, void *data);
 struct machines {
 	/** @session: back link to owning session if there is one. */
 	struct perf_session *session;
+	struct rw_semaphore lock;
 	struct machine host;
 	struct rb_root_cached guests;
 };
Signature · 1 line
2.56.0.313.g2479322e68-goog
author reviewer
#41 lore raw

PATCH v2 15/18 perf symbol: Add memory shrinking for symbols, DSOs, and machines

In reply to Ian Rogers

Long-running 'perf top' and large 'perf report' sessions can load full symbol tables, cached 4KB data pages, addr2line subprocesses, and libdw contexts across thousands of DSOs even when only a small fraction of symbols are actively referenced by live hist_entry or callchain nodes.

Add on-demand memory shrinking functions to reclaim cached symbol, DSO, and debuginfo state under memory pressure:

- symbols__shrink(): Evict symbols whose only reference is the struct symbols container (refcnt == 1 and not annotated) while retaining symbols actively referenced by callers (e.g. hist_entry, map_symbol, callchain nodes, or annotations). Mark the symbol table as shrunk and clear dso->loaded when symbols are evicted so subsequent map__load() calls reload the DSO under dso__lock(dso) and merge retained symbol instances back in place.
- dso__shrink(): Close open file descriptors, free cached 4KB data pages (on file-backed user DSOs), addr2line handles, and libdw Dwfl contexts, and shrink reloadable user DSO symbol tables.
- dsos__shrink(), machine__shrink(), machines__shrink(), and perf_session__shrink(): Shrink all DSOs across host and guest machines, evict unreferenced user DSOs with no remaining symbols, and return free heap pages via malloc_trim(0) on glibc.

All shrinking paths use non-blocking trylocks and perform zero heap allocations so they can be safely invoked from OOM recovery paths.

---
 tools/perf/util/dso.c        | 192 ++++++++++++++++++--
 tools/perf/util/dso.h        |  24 ++-
 tools/perf/util/dsos.c       |  68 +++++++
 tools/perf/util/dsos.h       |   2 +
 tools/perf/util/machine.c    |  32 ++++
 tools/perf/util/machine.h    |   3 +
 tools/perf/util/session.c    |   8 +
 tools/perf/util/session.h    |   2 +
 tools/perf/util/symbol-elf.c |   2 +-
 tools/perf/util/symbol.c     | 332 ++++++++++++++++++++++++++++++-----
 tools/perf/util/symbol.h     |   6 +
 11 files changed, 609 insertions(+), 62 deletions(-)
tools/perf/util/dso.c +173 -19
index 35a5c65030af..be257c77ab0a 100644
@@ -1075,12 +1075,12 @@ static int bpf_size(struct dso *dso)
 #endif // HAVE_LIBBPF_SUPPORT
 
 static void
-dso_cache__free(struct dso *dso)
+__dso_cache__free(struct dso *dso)
+	EXCLUSIVE_LOCKS_REQUIRED(dso__lock(dso))
 {
 	struct rb_root *root = &dso__data(dso)->cache;
 	struct rb_node *next = rb_first(root);
 
-	mutex_lock(dso__lock(dso));
 	while (next) {
 		struct dso_cache *cache;
 
@@ -1089,6 +1089,13 @@ dso_cache__free(struct dso *dso)
 		rb_erase(&cache->rb_node, root);
 		free(cache);
 	}
+}
+
+static void
+dso_cache__free(struct dso *dso)
+{
+	mutex_lock(dso__lock(dso));
+	__dso_cache__free(dso);
 	mutex_unlock(dso__lock(dso));
 }
 
@@ -1243,27 +1250,34 @@ static struct dso_cache *dso_cache__populate(struct dso *dso,
 	return cache;
 }
 
-static struct dso_cache *dso_cache__find(struct dso *dso,
-					 struct machine *machine,
-					 u64 offset,
-					 ssize_t *ret)
-{
-	struct dso_cache *cache = __dso_cache__find(dso, offset);
-
-	return cache ? cache : dso_cache__populate(dso, machine, offset, ret);
-}
-
 static ssize_t dso_cache_io(struct dso *dso, struct machine *machine,
 			    u64 offset, u8 *data, ssize_t size, bool out)
 {
 	struct dso_cache *cache;
 	ssize_t ret = 0;
 
-	cache = dso_cache__find(dso, machine, offset, &ret);
-	if (!cache)
-		return ret;
+	mutex_lock(dso__lock(dso));
+	cache = __dso_cache__find(dso, offset);
+	if (!cache) {
+		mutex_unlock(dso__lock(dso));
+		cache = dso_cache__populate(dso, machine, offset, &ret);
+		if (!cache)
+			return ret;
+		mutex_lock(dso__lock(dso));
+		/*
+		 * Re-lookup under dso__lock(dso) in case dso__shrink() freed
+		 * the cache while the lock was dropped.
+		 */
+		cache = __dso_cache__find(dso, offset);
+		if (!cache) {
+			mutex_unlock(dso__lock(dso));
+			return -EAGAIN;
+		}
+	}
 
-	return dso_cache__memcpy(cache, offset, data, size, out);
+	ret = dso_cache__memcpy(cache, offset, data, size, out);
+	mutex_unlock(dso__lock(dso));
+	return ret;
 }
 
 /*
@@ -1803,7 +1817,8 @@ int dso__name_len(const struct dso *dso)
 
 bool dso__loaded(const struct dso *dso)
 {
-	return RC_CHK_ACCESS(dso)->loaded;
+	return atomic_load_explicit(&RC_CHK_ACCESS(dso)->loaded, memory_order_acquire) &&
+	       !dso__symbols_shrunk(dso);
 }
 
 bool dso__sorted_by_name(const struct dso *dso)
@@ -1835,7 +1850,7 @@ struct dso *dso__new_id(const char *name, const struct dso_id *id)
 		dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
 		dso->dbginfo_type = DSO_BINARY_TYPE__NOT_FOUND;
 		dso->is_64_bit = (sizeof(void *) == 8);
-		dso->loaded = 0;
+		atomic_init(&dso->loaded, false);
 		dso->rel = 0;
 		dso->has_srcline = 1;
 		dso->a2l_fails = 1;
@@ -1896,6 +1911,144 @@ void dso__delete(struct dso *dso)
 	RC_CHK_FREE(dso);
 }
 
+bool dso__can_shrink_symbols(const struct dso *dso)
+{
+	const char *name;
+
+	if (!dso || (!dso__loaded(dso) && !dso__symbols_shrunk(dso)))
+		return false;
+
+	if (dso__kernel(dso) || dso__is_kmod(dso))
+		return false;
+
+	if (dso__is_vdso(dso))
+		return false;
+
+	switch (dso__binary_type(dso)) {
+	case DSO_BINARY_TYPE__KALLSYMS:
+	case DSO_BINARY_TYPE__GUEST_KALLSYMS:
+	case DSO_BINARY_TYPE__VMLINUX:
+	case DSO_BINARY_TYPE__GUEST_VMLINUX:
+	case DSO_BINARY_TYPE__KCORE:
+	case DSO_BINARY_TYPE__GUEST_KCORE:
+	case DSO_BINARY_TYPE__BPF_PROG_INFO:
+	case DSO_BINARY_TYPE__BPF_IMAGE:
+	case DSO_BINARY_TYPE__OOL:
+	case DSO_BINARY_TYPE__JAVA_JIT:
+	case DSO_BINARY_TYPE__GUEST_KMODULE:
+	case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
+	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
+	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
+		return false;
+	case DSO_BINARY_TYPE__DEBUGLINK:
+	case DSO_BINARY_TYPE__BUILD_ID_CACHE:
+	case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
+	case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
+	case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
+	case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
+	case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
+	case DSO_BINARY_TYPE__GNU_DEBUGDATA:
+	case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
+	case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
+	case DSO_BINARY_TYPE__NOT_FOUND:
+	default:
+		break;
+	}
+
+	switch (dso__symtab_type(dso)) {
+	case DSO_BINARY_TYPE__KALLSYMS:
+	case DSO_BINARY_TYPE__GUEST_KALLSYMS:
+	case DSO_BINARY_TYPE__VMLINUX:
+	case DSO_BINARY_TYPE__GUEST_VMLINUX:
+	case DSO_BINARY_TYPE__KCORE:
+	case DSO_BINARY_TYPE__GUEST_KCORE:
+	case DSO_BINARY_TYPE__BPF_PROG_INFO:
+	case DSO_BINARY_TYPE__BPF_IMAGE:
+	case DSO_BINARY_TYPE__OOL:
+	case DSO_BINARY_TYPE__JAVA_JIT:
+	case DSO_BINARY_TYPE__GUEST_KMODULE:
+	case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
+	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
+	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
+	case DSO_BINARY_TYPE__NOT_FOUND:
+		return false;
+	case DSO_BINARY_TYPE__DEBUGLINK:
+	case DSO_BINARY_TYPE__BUILD_ID_CACHE:
+	case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
+	case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
+	case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
+	case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
+	case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
+	case DSO_BINARY_TYPE__GNU_DEBUGDATA:
+	case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
+	case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
+	default:
+		break;
+	}
+
+	name = dso__long_name(dso);
+	if (!name || name[0] == '[' || is_perf_pid_map_name(name) ||
+	    strstr(name, " (deleted)") != NULL)
+		return false;
+
+	return true;
+}
+
+size_t dso__shrink(struct dso *dso)
+	NO_THREAD_SAFETY_ANALYSIS
+{
+	size_t freed = 0;
+
+	if (!dso)
+		return 0;
+
+	if (mutex_trylock(dso__data_open_lock())) {
+		if (dso__data(dso)->fd >= 0) {
+			close_dso(dso);
+			freed++;
+		}
+		mutex_unlock(dso__data_open_lock());
+	}
+
+	if (!mutex_trylock(dso__lock(dso)))
+		return freed;
+
+	if (!dso__kernel(dso) && dso__binary_type(dso) != DSO_BINARY_TYPE__OOL &&
+	    !RB_EMPTY_ROOT(&dso__data(dso)->cache)) {
+		__dso_cache__free(dso);
+		freed++;
+	}
+	if (dso__a2l(dso)) {
+		dso__free_a2l(dso);
+		freed++;
+	}
+#ifdef HAVE_LIBBFD_SUPPORT
+	if (dso__a2l_libbfd(dso)) {
+		dso__free_a2l_libbfd(dso);
+		freed++;
+	}
+#endif
+#ifdef HAVE_LIBDW_SUPPORT
+	if (dso__libdw(dso)) {
+		dso__free_libdw(dso);
+		freed++;
+	}
+#endif
+
+	if (dso__can_shrink_symbols(dso)) {
+		size_t sym_freed = symbols__shrink(dso__symbols(dso));
+
+		if (sym_freed > 0) {
+			atomic_store_explicit(&RC_CHK_ACCESS(dso)->loaded, false,
+					      memory_order_release);
+			freed += sym_freed;
+		}
+	}
+
+	mutex_unlock(dso__lock(dso));
+	return freed;
+}
+
 struct dso *dso__get(struct dso *dso)
 {
 	struct dso *result;
@@ -1909,7 +2062,8 @@ struct dso *dso__get(struct dso *dso)
 void dso__put(struct dso *dso)
 {
 #ifdef REFCNT_CHECKING
-	if (dso && dso__data(dso) && refcount_read(&RC_CHK_ACCESS(dso)->refcnt) == 2)
+	if (dso && dso__data(dso) && dso__data(dso)->fd >= 0 &&
+	    refcount_read(&RC_CHK_ACCESS(dso)->refcnt) == 2)
 		dso__data_close(dso);
 #endif
 	if (dso && refcount_dec_and_test(&RC_CHK_ACCESS(dso)->refcnt))
tools/perf/util/dso.h +22 -2
index f23cf9957aec..64a118ea2524 100644
@@ -346,7 +346,7 @@ DECLARE_RC_STRUCT(dso) {
 	u8		 is_64_bit:1;
 	u8		 bintype_searched:1;
 	u8		 debuginfo_searched:1;
-	bool		 loaded;
+	_Atomic bool	 loaded;
 	u8		 rel;
 	char		 name[];
 };
@@ -602,7 +602,7 @@ static inline enum dso_load_errno *dso__load_errno(struct dso *dso)
 
 static inline void dso__set_loaded(struct dso *dso)
 {
-	RC_CHK_ACCESS(dso)->loaded = true;
+	atomic_store_explicit(&RC_CHK_ACCESS(dso)->loaded, true, memory_order_release);
 }
 
 static inline struct mutex *dso__lock(struct dso *dso)
@@ -783,6 +783,26 @@ static inline bool dso__has_symbols(const struct dso *dso)
 	return RC_CHK_ACCESS(dso)->symbols.cnt > 0;
 }
 
+static inline bool dso__symbols_shrunk(const struct dso *dso)
+{
+	return atomic_load_explicit(&RC_CHK_ACCESS(dso)->symbols.shrunk,
+				    memory_order_acquire);
+}
+
+static inline void dso__set_symbols_shrunk(struct dso *dso, bool val)
+{
+	atomic_store_explicit(&RC_CHK_ACCESS(dso)->symbols.shrunk, val,
+			      memory_order_release);
+}
+
+static inline refcount_t *dso__refcnt(struct dso *dso)
+{
+	return &RC_CHK_ACCESS(dso)->refcnt;
+}
+
+bool dso__can_shrink_symbols(const struct dso *dso);
+size_t dso__shrink(struct dso *dso);
+
 char *dso__filename_with_chroot(const struct dso *dso, const char *filename);
 
 bool dso__sorted_by_name(const struct dso *dso);
tools/perf/util/dsos.c +68 -0

77 lines not on this page · raw message

tools/perf/util/dsos.h +2 -0
index a26774950866..151efdba2aa4 100644
@@ -49,4 +49,6 @@ struct dso *dsos__find_kernel_dso(struct dsos *dsos);
 
 int dsos__for_each_dso(struct dsos *dsos, int (*cb)(struct dso *dso, void *data), void *data);
 
+size_t dsos__shrink(struct dsos *dsos);
+
 #endif /* __PERF_DSOS */
tools/perf/util/machine.c +32 -0
index fb83da8ef09b..8d0c545ee544 100644
@@ -3446,3 +3446,35 @@ int machine__hit_all_dsos(struct machine *machine)
 {
 	return dsos__hit_all(&machine->dsos);
 }
+
+size_t machine__shrink(struct machine *machine)
+{
+	if (!machine)
+		return 0;
+
+	return dsos__shrink(&machine->dsos);
+}
+
+size_t machines__shrink(struct machines *machines)
+	NO_THREAD_SAFETY_ANALYSIS
+{
+	struct rb_node *nd;
+	size_t freed;
+
+	if (!machines)
+		return 0;
+
+	freed = machine__shrink(&machines->host);
+
+	if (!down_read_trylock(&machines->lock))
+		return freed;
+
+	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
+		struct machine *pos = rb_entry(nd, struct machine, rb_node);
+
+		freed += machine__shrink(pos);
+	}
+	up_read(&machines->lock);
+
+	return freed;
+}
tools/perf/util/machine.h +3 -0
index 147ce343e370..1d1f46a01a38 100644
@@ -337,4 +337,7 @@ int machine__resolve(struct machine *machine, struct addr_location *al,
 
 int machine__hit_all_dsos(struct machine *machine);
 
+size_t machine__shrink(struct machine *machine);
+size_t machines__shrink(struct machines *machines);
+
 #endif /* __PERF_MACHINE_H */
tools/perf/util/session.c +8 -0
index 56a5e57ec512..3f35c7affc29 100644
@@ -4266,3 +4266,11 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flags
 	 */
 	return perf_env__e_machine_nocache(env, e_flags);
 }
+
+size_t perf_session__shrink(struct perf_session *session)
+{
+	if (!session)
+		return 0;
+
+	return machines__shrink(&session->machines);
+}
tools/perf/util/session.h +2 -0
index ac5803d5fb4e..fdec65fcfee6 100644
@@ -217,4 +217,6 @@ int perf_event__process_finished_round(const struct perf_tool *tool,
 struct perf_env *perf_session__env(struct perf_session *session);
 uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flags);
 
+size_t perf_session__shrink(struct perf_session *session);
+
 #endif /* __PERF_SESSION_H */
tools/perf/util/symbol-elf.c +1 -1
index 6082136c9662..946a2426933c 100644
@@ -1973,7 +1973,7 @@ int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 	 * Modules may already have symbols from kallsyms, but those symbols
 	 * have the wrong values for the dso maps, so remove them.
 	 */
-	if (kmodule && syms_ss->symtab)
+	if (kmodule && syms_ss->symtab && !dso__symbols_shrunk(dso))
 		symbols__delete(dso__symbols(dso));
 
 	if (!syms_ss->symtab) {
tools/perf/util/symbol.c +292 -40

441 lines not on this page · raw message

tools/perf/util/symbol.h +6 -0
index 066697d8da8f..7d6469d6a976 100644
@@ -135,12 +135,18 @@ struct symbols {
 	 * by name.
 	 */
 	bool sorted_by_name;
+	/**
+	 * @shrunk: True when symbols have been evicted by symbols__shrink()
+	 * and may need to be reloaded on demand.
+	 */
+	_Atomic bool shrunk;
 };
 
 void symbol__delete(struct symbol *sym);
 void symbols__init(struct symbols *symbols);
 void symbols__exit(struct symbols *symbols);
 void symbols__delete(struct symbols *symbols);
+size_t symbols__shrink(struct symbols *symbols);
 void symbols__sort_read_lock(struct symbols *symbols) SHARED_LOCK_FUNCTION(symbols->lock);
 
 static inline refcount_t *symbol__refcnt(struct symbol *sym)
Signature · 1 line
2.56.0.313.g2479322e68-goog
#42 lore raw

Re: [PATCH v2 15/18] perf symbol: Add memory shrinking for symbols, DSOs, and machines

Quoted 4 lines · Ian Rogers

+ if (was_shrunk) {
+ if (ret > 0) {
+ symbols__merge_retained(dso__symbols(dso), retained, retained_cnt);
+ } else {

Quoted 8 lines

+ dso__set_adjust_symbols(dso, saved_adjust_symbols);
+ symbols__restore_retained(dso__symbols(dso), retained,
+ retained_cnt, retained_allocated);
+ if (retained_cnt > 0)
+ ret = retained_cnt;
+ }
+ dso__set_symbols_shrunk(dso, false);
+ dso__set_loaded(dso);

Could the failed-reload path leave the DSO in a state where a later lookup can retry, rather than clearing symbols_shrunk and marking it loaded?

If the file is gone or no longer matches when the reload runs, this restores only the retained symbols and still marks the DSO loaded and not shrunk. The evicted symbols never come back and nothing retries. For a DSO without a build-id whose file was replaced, the reload would also merge the new file's symbols with the old retained ones.

Could the reload also build a local struct symbols and swap it in under the write lock, instead of exposing the table while it is being rebuilt?

dso__load() currently detaches the retained symbols for the whole reload and merges them back at the end. map__find_symbol() waits for the dso lock, but a direct dso__find_symbol() from another thread can miss the retained symbols or get the fresh copy meanwhile. In a test with one lookup thread and one shrink plus map__load() thread, 115,691 lookups missed a retained symbol, and most of the 13.2M lookups returned the fresh copy instead of the retained object. That breaks the pointer identity annotations rely on. Swapping a completed table under the write lock would close that window.

Smaller things:

- symbols__merge_retained() is O(R^2) in retained symbols because of the already_retained scan. Both arrays are sorted, so a linear merge works.

- symbols__fixup_duplicate() no longer returns early for allow_aliases, and dso__load() now runs a second dedupe after PLT synthesis on every load, not only after a shrink.

- choose_best_symbol() now prefers refcount > 1, so which alias wins depends on references other threads hold at that moment.

- If shrink removes a newly populated cache entry, dso_cache_io() returns -EAGAIN. cached_io() does not retry, so this transient race becomes a hard DSO read failure.

Thanks,
Alireza

author reviewer
#43 lore raw

PATCH v2 16/18 perf session: Add dso__session() and session-scoped allocation wrappers

In reply to Ian Rogers

Add explicit session-scoped memory allocation wrappers:
- perf_session__malloc(session, size)
- perf_session__zalloc(session, size)
- perf_session__calloc(session, nmemb, size)
- perf_session__realloc(session, ptr, size)
- perf_session__strdup(session, s)

When an allocation fails and returns NULL, if a non-NULL struct perf_session is provided, the wrapper invokes perf_session__shrink() to evict unreferenced symbols, DSOs, and cached debuginfo across the session's machines and then retries the allocation once. This avoids the thrashing overhead of periodic heuristic shrinking while allowing perf to recover from memory exhaustion.

Also plumb a back-pointer from struct dsos to its owning struct machine so machine__session(machine) and dso__session(dso) can resolve the active perf_session.

---
 tools/perf/util/dso.c     |  7 ++++
 tools/perf/util/dso.h     |  2 ++
 tools/perf/util/dsos.c    |  1 +
 tools/perf/util/dsos.h    |  1 +
 tools/perf/util/machine.c |  1 +
 tools/perf/util/machine.h |  5 +++
 tools/perf/util/session.c | 70 +++++++++++++++++++++++++++++++++++++++
 tools/perf/util/session.h |  6 ++++
 8 files changed, 93 insertions(+)
tools/perf/util/dso.c +7 -0
index be257c77ab0a..c19de9ab4b24 100644
@@ -1911,6 +1911,13 @@ void dso__delete(struct dso *dso)
 	RC_CHK_FREE(dso);
 }
 
+struct perf_session *dso__session(const struct dso *dso)
+{
+	struct dsos *dsos = dso ? RC_CHK_ACCESS(dso)->dsos : NULL;
+
+	return dsos ? machine__session(dsos->machine) : NULL;
+}
+
 bool dso__can_shrink_symbols(const struct dso *dso)
 {
 	const char *name;
tools/perf/util/dso.h +2 -0
index 64a118ea2524..5fa059ba6cf1 100644
@@ -19,6 +19,7 @@
 struct machine;
 struct map;
 struct perf_env;
+struct perf_session;
 
 #define DSO__NAME_KALLSYMS	"[kernel.kallsyms]"
 #define DSO__NAME_KCORE		"[kernel.kcore]"
@@ -802,6 +803,7 @@ static inline refcount_t *dso__refcnt(struct dso *dso)
 
 bool dso__can_shrink_symbols(const struct dso *dso);
 size_t dso__shrink(struct dso *dso);
+struct perf_session *dso__session(const struct dso *dso);
 
 char *dso__filename_with_chroot(const struct dso *dso, const char *filename);
 
tools/perf/util/dsos.c +1 -0
index 30ecc9a19244..8f3e3f3c6ce3 100644
@@ -18,6 +18,7 @@ void dsos__init(struct dsos *dsos)
 {
 	init_rwsem(&dsos->lock);
 
+	dsos->machine = NULL;
 	dsos->cnt = 0;
 	dsos->allocated = 0;
 	dsos->dsos = NULL;
tools/perf/util/dsos.h +1 -0
index 151efdba2aa4..d636e4c5cbaf 100644
@@ -19,6 +19,7 @@ struct machine;
  */
 struct dsos {
 	struct rw_semaphore lock;
+	struct machine *machine;
 	struct dso **dsos;
 	unsigned int cnt;
 	unsigned int allocated;
tools/perf/util/machine.c +1 -0
index 8d0c545ee544..3e0d68fd5efe 100644
@@ -81,6 +81,7 @@ int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
 	memset(machine, 0, sizeof(*machine));
 	RB_CLEAR_NODE(&machine->rb_node);
 	dsos__init(&machine->dsos);
+	machine->dsos.machine = machine;
 	threads__init(&machine->threads);
 
 	machine->kmaps = maps__new(machine);
tools/perf/util/machine.h +5 -0
index 1d1f46a01a38..ab67cef67c80 100644
@@ -164,6 +164,11 @@ static inline struct perf_session *machines__session(struct machines *machines)
 	return machines->session;
 }
 
+static inline struct perf_session *machine__session(const struct machine *machine)
+{
+	return machine && machine->machines ? machine->machines->session : NULL;
+}
+
 void machines__process_guests(struct machines *machines,
 			      machine__process_t process, void *data);
 
tools/perf/util/session.c +70 -0

73 lines not on this page · raw message

tools/perf/util/session.h +6 -0
index fdec65fcfee6..90ae16a631b0 100644
@@ -219,4 +219,10 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flags
 
 size_t perf_session__shrink(struct perf_session *session);
 
+void *perf_session__malloc(struct perf_session *session, size_t size);
+void *perf_session__zalloc(struct perf_session *session, size_t size);
+void *perf_session__calloc(struct perf_session *session, size_t nmemb, size_t size);
+void *perf_session__realloc(struct perf_session *session, void *ptr, size_t size);
+char *perf_session__strdup(struct perf_session *session, const char *s);
+
 #endif /* __PERF_SESSION_H */
Signature · 1 line
2.56.0.313.g2479322e68-goog
author reviewer
#44 lore raw

PATCH v2 17/18 perf symbol: Use session allocation wrappers in symbol, DSO, thread, and hist paths

In reply to Ian Rogers

Connect the session-scoped allocation wrappers (perf_session__malloc, perf_session__zalloc, perf_session__calloc, perf_session__realloc, perf_session__strdup) to the primary heap allocation paths during sample and symbol processing: symbol__new(), symbols->symbols array growth, dso__new_id(), thread__new(), and hist_entry allocation.

In normal operation when memory is available, behavior is unchanged. When an allocation fails with NULL, these paths now invoke perf_session__shrink() on the owning perf_session to reclaim unreferenced symbols and DSOs and retry the allocation once before failing.

---
 tools/perf/util/dso.c        |  4 ++-
 tools/perf/util/dsos.c       |  7 +++++
 tools/perf/util/hist.c       | 41 +++++++++++++++++++++---
 tools/perf/util/machine.c    |  8 ++++-
 tools/perf/util/symbol-elf.c | 14 ++++++---
 tools/perf/util/symbol.c     | 60 +++++++++++++++++++++++-------------
 tools/perf/util/symbol.h     |  3 ++
 7 files changed, 104 insertions(+), 33 deletions(-)
tools/perf/util/dso.c +3 -1
index c19de9ab4b24..de694e119a6e 100644
@@ -27,6 +27,7 @@
 #include "map.h"
 #include "namespaces.h"
 #include "path.h"
+#include "session.h"
 #include "srcline.h"
 #include "string2.h"
 #include "symbol.h"
@@ -1214,10 +1215,11 @@ static struct dso_cache *dso_cache__populate(struct dso *dso,
 					     u64 offset, ssize_t *ret)
 {
 	u64 cache_offset = offset & DSO__DATA_CACHE_MASK;
+	struct perf_session *session = machine ? machine__session(machine) : dso__session(dso);
 	struct dso_cache *cache;
 	struct dso_cache *old;
 
-	cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
+	cache = perf_session__zalloc(session, sizeof(*cache) + DSO__DATA_CACHE_SIZE);
 	if (!cache) {
 		*ret = -ENOMEM;
 		return NULL;
tools/perf/util/dsos.c +7 -0
index 8f3e3f3c6ce3..374e1c5d2c32 100644
@@ -2,6 +2,8 @@
 #include "debug.h"
 #include "dsos.h"
 #include "dso.h"
+#include "machine.h"
+#include "session.h"
 #include "util.h"
 #include "vdso.h"
 #include "namespaces.h"
@@ -350,6 +352,11 @@ struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, const struct d
 	down_write(&dsos->lock);
 	dso = __dsos__findnew_id(dsos, name, id);
 	up_write(&dsos->lock);
+	if (!dso && dsos->machine && perf_session__shrink(machine__session(dsos->machine)) > 0) {
+		down_write(&dsos->lock);
+		dso = __dsos__findnew_id(dsos, name, id);
+		up_write(&dsos->lock);
+	}
 	return dso;
 }
 
tools/perf/util/hist.c +36 -5

79 lines not on this page · raw message

tools/perf/util/machine.c +7 -1
index 3e0d68fd5efe..f45befc35a7f 100644
@@ -20,6 +20,7 @@
 #include "mem-events.h"
 #include "mem-info.h"
 #include "path.h"
+#include "session.h"
 #include "srcline.h"
 #include "symbol.h"
 #include "synthetic-events.h"
@@ -598,7 +599,12 @@ static struct thread *__machine__findnew_thread(struct machine *machine,
 
 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
 {
-	return __machine__findnew_thread(machine, pid, tid, /*create=*/true);
+	struct thread *th = __machine__findnew_thread(machine, pid, tid, /*create=*/true);
+
+	if (!th && perf_session__shrink(machine__session(machine)) > 0)
+		th = __machine__findnew_thread(machine, pid, tid, /*create=*/true);
+
+	return th;
 }
 
 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
tools/perf/util/symbol-elf.c +10 -4
index 946a2426933c..75139fab0748 100644
@@ -15,6 +15,7 @@
 #include "symbol.h"
 #include "symsrc.h"
 #include "machine.h"
+#include "session.h"
 #include "vdso.h"
 #include "debug.h"
 #include "util/copyfile.h"
@@ -701,7 +702,7 @@ static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf,
 		sym = symbol__new(shdr.sh_offset + i, shdr.sh_entsize, STB_GLOBAL, STT_FUNC, buf);
 		if (!sym)
 			goto out;
-		if (symbols__insert(dso__symbols(dso), sym))
+		if (symbols__insert_session(dso__symbols(dso), sym, dso__session(dso)))
 			goto out;
 	}
 	err = 0;
@@ -757,7 +758,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
 	plt_sym = symbol__new(shdr_plt.sh_offset, plt_header_size, STB_GLOBAL, STT_FUNC, ".plt");
 	if (!plt_sym)
 		goto out_elf_end;
-	if (symbols__insert(dso__symbols(dso), plt_sym))
+	if (symbols__insert_session(dso__symbols(dso), plt_sym, dso__session(dso)))
 		goto out_elf_end;
 
 	/* Only x86 has .plt.got */
@@ -880,7 +881,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
 			goto out_elf_end;
 
 		plt_offset += plt_entry_size;
-		if (symbols__insert(dso__symbols(dso), f))
+		if (symbols__insert_session(dso__symbols(dso), f, dso__session(dso)))
 			goto out_elf_end;
 		++nr;
 	}
@@ -1939,13 +1940,18 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		f = symbol__new(sym.st_value, sym.st_size,
 				GELF_ST_BIND(sym.st_info),
 				GELF_ST_TYPE(sym.st_info), elf_name);
+		if (!f && perf_session__shrink(dso__session(curr_dso)) > 0) {
+			f = symbol__new(sym.st_value, sym.st_size,
+					GELF_ST_BIND(sym.st_info),
+					GELF_ST_TYPE(sym.st_info), elf_name);
+		}
 		free(demangled);
 		if (!f)
 			goto out_elf_end;
 
 		arch__sym_update(f, &sym);
 
-		if (symbols__insert(dso__symbols(curr_dso), f))
+		if (symbols__insert_session(dso__symbols(curr_dso), f, dso__session(curr_dso)))
 			goto out_elf_end;
 		nr++;
 	}
tools/perf/util/symbol.c +38 -22

174 lines not on this page · raw message

tools/perf/util/symbol.h +3 -0
index 7d6469d6a976..2db5eb84b268 100644
@@ -33,6 +33,7 @@ struct maps;
 struct option;
 struct build_id;
 struct perf_env;
+struct perf_session;
 
 /*
  * Ignore kernel mapping symbols, matching kernel is_mapping_symbol() logic.
@@ -376,6 +377,8 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);
 char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name);
 
 int symbols__insert(struct symbols *symbols, struct symbol *sym);
+int symbols__insert_session(struct symbols *symbols, struct symbol *sym,
+			    struct perf_session *session);
 void symbols__fixup_duplicate(struct symbols *symbols);
 void symbols__fixup_end(struct symbols *symbols, bool is_kallsyms);
 
Signature · 1 line
2.56.0.313.g2479322e68-goog
author reviewer
#45 lore raw

PATCH v2 18/18 perf test symbols: Add tests for symbol shrinking and session alloc wrappers

In reply to Ian Rogers

Ensure the new symbol sorting, reference-counted symbol and DSO shrinking, transparent DSO reload with retained-symbol pointer preservation, and session-scoped allocation wrappers are verified by 'perf test symbols' (including under REFCNT_CHECKING=1).

Add unit tests to 'perf test symbols' covering:
- test_symbols_sorted_insert(): Verify ascending symbols__insert(), out-of-order dso__insert_symbol() maintaining sorted order, and unsorted symbols__insert() followed by sort-on-lookup in dso__find_symbol().
- test_symbols_shrink(): Verify symbols__shrink() evicts unreferenced symbols (refcnt == 1), retains symbols with active external references (refcnt > 1), and sets symbols.shrunk.
- test_dsos_shrink(): Verify dsos__shrink() evicts unreferenced user DSOs with no remaining symbols while retaining DSOs with active external references.
- test_session_alloc_shrink(): Verify machine__session(), dso__session(), perf_session__malloc/zalloc/calloc/realloc/strdup(), and perf_session__shrink().
- test_dso_shrink_and_reload(): Verify dso__shrink() on a loaded ELF binary followed by map__find_symbol() transparently reloads evicted symbols while preserving pointer identity for retained symbols.

---
 tools/perf/tests/symbols.c | 372 +++++++++++++++++++++++++++++++++++++
 1 file changed, 372 insertions(+)
tools/perf/tests/symbols.c +372 -0
index d70adbad3978..1a773c535732 100644
@@ -1,13 +1,21 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <linux/compiler.h>
 #include <linux/string.h>
+#include <linux/zalloc.h>
 #include <sys/mman.h>
+#include <errno.h>
 #include <inttypes.h>
 #include <limits.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include "debug.h"
 #include "dso.h"
 #include "env.h"
 #include "machine.h"
+#include "session.h"
 #include "thread.h"
 #include "symbol.h"
 #include "map.h"
@@ -179,6 +187,264 @@ static int process_subdivided_dso(struct machine *machine, struct dso *dso)
 	return ret < 0 ? TEST_FAIL : TEST_OK;
 }
 
+static int test_symbols_shrink(void)
+{
+	struct symbols symbols;
+	struct symbol *sym1, *sym2, *sym3, *held;
+	int ret = TEST_FAIL;
+
+	symbols__init(&symbols);
+
+	sym1 = symbol__new(0x100, 0x50, STB_GLOBAL, STT_FUNC, "sym1");
+	sym2 = symbol__new(0x200, 0x50, STB_GLOBAL, STT_FUNC, "sym2");
+	sym3 = symbol__new(0x300, 0x50, STB_GLOBAL, STT_FUNC, "sym3");
+	if (!sym1 || !sym2 || !sym3) {
+		symbol__put(sym1);
+		symbol__put(sym2);
+		symbol__put(sym3);
+		goto out;
+	}
+
+	/* Hold an active reference on sym1; sym2 and sym3 only have the table ref. */
+	held = symbol__get(sym1);
+
+	symbols__insert(&symbols, sym1);
+	symbols__insert(&symbols, sym2);
+	symbols__insert(&symbols, sym3);
+
+	/*
+	 * First shrink: sym2 and sym3 (refcnt == 1) are evicted; sym1 has
+	 * refcnt > 1 and is retained.
+	 */
+	if (symbols__shrink(&symbols) != 2 || symbols.cnt != 1 || !symbols.shrunk) {
+		pr_debug("First symbols__shrink failed (cnt=%u)!\n", symbols.cnt);
+		symbol__put(held);
+		goto out;
+	}
+
+	/*
+	 * Drop caller reference on sym1 and shrink again: sym1 is now evicted.
+	 */
+	symbol__put(held);
+	if (symbols__shrink(&symbols) != 1 || symbols.cnt != 0) {
+		pr_debug("Second symbols__shrink failed (cnt=%u)!\n", symbols.cnt);
+		goto out;
+	}
+
+	ret = TEST_OK;
+out:
+	symbols__exit(&symbols);
+	return ret;
+}
+
+static int test_dsos_shrink(void)
+{
+	struct dsos dsos;
+	struct dso *dso1, *dso2;
+	int ret = TEST_FAIL;
+
+	dsos__init(&dsos);
+
+	dso1 = dsos__findnew_id(&dsos, "/tmp/perf_test_dso1", NULL);
+	dso2 = dsos__findnew_id(&dsos, "/tmp/perf_test_dso2", NULL);
+	if (!dso1 || !dso2) {
+		dso__put(dso1);
+		dso__put(dso2);
+		goto out;
+	}
+
+	/* Keep the caller's active reference on dso1; drop caller reference on dso2. */
+	dso__put(dso2);
+
+	/*
+	 * First shrink: dso2 has refcnt == 1 and no symbols, so it is evicted;
+	 * dso1 has refcnt == 2, so it is retained.
+	 */
+	if (dsos__shrink(&dsos) != 1 || dsos.cnt != 1) {
+		pr_debug("First dsos__shrink failed (cnt=%u)!\n", dsos.cnt);
+		dso__put(dso1);
+		goto out;
+	}
+
+	dso__put(dso1);
+	if (dsos__shrink(&dsos) != 1 || dsos.cnt != 0) {
+		pr_debug("Second dsos__shrink failed (cnt=%u)!\n", dsos.cnt);
+		goto out;
+	}
+
+	ret = TEST_OK;
+out:
+	dsos__exit(&dsos);
+	return ret;
+}
+
+static int test_session_alloc_shrink(void)
+{
+	struct perf_session *session = zalloc(sizeof(*session));
+	struct dso *dso;
+	char *p1 = NULL, *p2 = NULL, *p3 = NULL, *s = NULL;
+	int ret = TEST_FAIL;
+
+	if (!session)
+		return TEST_FAIL;
+
+	if (machines__init(&session->machines, session))
+		goto out;
+	if (machine__session(&session->machines.host) != session) {
+		pr_debug("machine__session did not return owning session!\n");
+		goto out;
+	}
+
+	dso = machine__findnew_dso(&session->machines.host, "/tmp/perf_test_session_dso");
+	if (!dso)
+		goto out;
+	if (dso__session(dso) != session) {
+		pr_debug("dso__session did not return owning session!\n");
+		dso__put(dso);
+		goto out;
+	}
+	dso__put(dso);
+
+	p1 = perf_session__malloc(session, 64);
+	p2 = perf_session__zalloc(session, 64);
+	p3 = perf_session__calloc(session, 4, 16);
+	s = perf_session__strdup(session, "perf_session_alloc_test");
+	if (!p1 || !p2 || !p3 || !s || strcmp(s, "perf_session_alloc_test") != 0) {
+		pr_debug("perf_session allocation wrappers failed!\n");
+		goto out;
+	}
+
+	p1 = perf_session__realloc(session, p1, 128);
+	if (!p1) {
+		pr_debug("perf_session__realloc failed!\n");
+		goto out;
+	}
+
+	if (perf_session__shrink(session) == 0 || session->machines.host.dsos.cnt != 0) {
+		pr_debug("perf_session__shrink failed to evict unreferenced DSO!\n");
+		goto out;
+	}
+
+	ret = TEST_OK;
+out:
+	free(p1);
+	free(p2);
+	free(p3);
+	free(s);
+	machines__exit(&session->machines);
+	free(session);
+	return ret;
+}
+
+struct shrink_reload_find_args {
+	struct symbol *kept;
+	u64 first_start;
+	u64 second_start;
+	char *second_name;
+	u64 max_end;
+};
+
+static int shrink_reload_find_cb(struct symbol *sym, void *data)
+{
+	struct shrink_reload_find_args *args = data;
+
+	if (!args->kept) {
+		args->kept = symbol__get(sym);
+		args->first_start = symbol__start(sym);
+		args->max_end = symbol__end(sym);
+		return 0;
+	}
+	if (!args->second_name && symbol__start(sym) >= args->max_end) {
+		args->second_start = symbol__start(sym);
+		args->second_name = strdup(symbol__name(sym));
+		if (!args->second_name)
+			return -ENOMEM;
+	}
+	if (symbol__end(sym) > args->max_end)
+		args->max_end = symbol__end(sym);
+	return 0;
+}
+
+static int test_dso_shrink_and_reload(struct dso *dso, struct map *map)
+{
+	struct shrink_reload_find_args args = {
+		.kept = NULL,
+		.first_start = 0,
+		.second_start = 0,
+		.second_name = NULL,
+		.max_end = 0,
+	};
+	struct symbol *sym, *reloaded;
+	size_t removed;
+
+	if (!dso__can_shrink_symbols(dso) || dso__kernel(dso))
+		return TEST_OK;
+	if (dso__for_each_symbol(dso, shrink_reload_find_cb, &args) < 0) {
+		symbol__put(args.kept);
+		return TEST_FAIL;
+	}
+	if (!args.second_name) {
+		symbol__put(args.kept);
+		return TEST_OK;
+	}
+
+	/* Unreferenced symbols have refcnt == 1 and are evicted immediately. */
+	removed = dso__shrink(dso);
+	if (removed == 0 || !dso__symbols_shrunk(dso)) {
+		pr_debug("dso__shrink did not evict any symbols!\n");
+		free(args.second_name);
+		symbol__put(args.kept);
+		return TEST_FAIL;
+	}
+
+	/* Kept symbol is still found without reloading. */
+	sym = dso__find_symbol(dso, args.first_start);
+	if (!RC_CHK_EQUAL(sym, args.kept)) {
+		pr_debug("Kept symbol not found after dso__shrink!\n");
+		symbol__put(sym);
+		free(args.second_name);
+		symbol__put(args.kept);
+		return TEST_FAIL;
+	}
+	symbol__put(sym);
+
+	/* Evicted symbol is no longer in dso->symbols before reload. */
+	sym = dso__find_symbol(dso, args.second_start);
+	if (sym != NULL) {
+		pr_debug("Evicted symbol still found after dso__shrink!\n");
+		symbol__put(sym);
+		free(args.second_name);
+		symbol__put(args.kept);
+		return TEST_FAIL;
+	}
+
+	/* Lookup via map__find_symbol transparently reloads the DSO. */
+	reloaded = map__find_symbol(map, args.second_start);
+	if (!reloaded || strcmp(symbol__name(reloaded), args.second_name) != 0) {
+		pr_debug("Failed to reload evicted symbol %s!\n", args.second_name);
+		symbol__put(reloaded);
+		free(args.second_name);
+		symbol__put(args.kept);
+		return TEST_FAIL;
+	}
+	symbol__put(reloaded);
+
+	/* Kept symbol pointer was preserved across reload deduplication. */
+	sym = dso__find_symbol(dso, args.first_start);
+	if (!RC_CHK_EQUAL(sym, args.kept)) {
+		pr_debug("Kept symbol pointer was not preserved across reload!\n");
+		symbol__put(sym);
+		free(args.second_name);
+		symbol__put(args.kept);
+		return TEST_FAIL;
+	}
+	symbol__put(sym);
+	symbol__zput(args.kept);
+
+	free(args.second_name);
+	return TEST_OK;
+}
+
 static int test_file(struct test_info *ti, char *filename)
 {
 	struct map *map = NULL;
@@ -210,18 +476,124 @@ static int test_file(struct test_info *ti, char *filename)
 	/* Module dso is split into many dsos by section */
 	if (ret == TEST_OK && dso__kernel(dso) != DSO_SPACE__USER)
 		ret = process_subdivided_dso(ti->machine, dso);
+
+	if (ret == TEST_OK)
+		ret = test_dso_shrink_and_reload(dso, map);
 out_put:
 	map__put(map);
 
 	return ret;
 }
 
+static int test_symbols_sorted_insert(void)
+{
+	struct dso *dso = dso__new("/tmp/perf_test_sorted_insert");
+	struct symbols *symbols;
+	int ret = TEST_FAIL;
+
+	if (!dso)
+		return TEST_FAIL;
+
+	symbols = dso__symbols(dso);
+
+	/*
+	 * Insert 100 odd-indexed symbols in ascending order via
+	 * symbols__insert(), then insert 5 even-indexed symbols out of order via
+	 * dso__insert_symbol() and verify symbols->sorted remains true.
+	 */
+	for (unsigned int i = 0; i < 100; i++) {
+		char name[32];
+		u64 addr = (u64)(i * 2 + 1) * 0x100;
+		struct symbol *sym;
+
+		snprintf(name, sizeof(name), "sym_%u", i * 2 + 1);
+		sym = symbol__new(addr, 0x40, STB_GLOBAL, STT_FUNC, name);
+		if (!sym || symbols__insert(symbols, sym))
+			goto out;
+	}
+	if (!symbols->sorted || symbols->cnt != 100) {
+		pr_debug("Expected sorted=true, cnt=100 after ordered inserts\n");
+		goto out;
+	}
+
+	for (unsigned int i = 0; i < 5; i++) {
+		char name[32];
+		u64 addr = (u64)(i * 20) * 0x100;
+		struct symbol *sym;
+
+		snprintf(name, sizeof(name), "ins_%u", i * 20);
+		sym = symbol__new(addr, 0x40, STB_GLOBAL, STT_FUNC, name);
+		if (!sym || dso__insert_symbol(dso, sym))
+			goto out;
+		if (!symbols->sorted) {
+			pr_debug("dso__insert_symbol did not preserve sorted invariant!\n");
+			goto out;
+		}
+	}
+
+	/*
+	 * Now append 95 remaining even-indexed symbols in descending order via
+	 * symbols__insert() to exercise the unsorted append + sort-on-lookup path.
+	 */
+	for (unsigned int i = 99; i > 0; i--) {
+		char name[32];
+		u64 addr;
+		struct symbol *sym;
+
+		if ((i * 2) % 20 == 0 && (i * 2) < 100)
+			continue;
+		addr = (u64)(i * 2) * 0x100;
+		snprintf(name, sizeof(name), "ins_%u", i * 2);
+		sym = symbol__new(addr, 0x40, STB_GLOBAL, STT_FUNC, name);
+		if (!sym || symbols__insert(symbols, sym))
+			goto out;
+	}
+	if (symbols->sorted || symbols->cnt != 200) {
+		pr_debug("Unexpected sorted=%d cnt=%u before lookup\n",
+			 symbols->sorted, symbols->cnt);
+		goto out;
+	}
+
+	for (unsigned int i = 0; i < 200; i++) {
+		u64 addr = (u64)i * 0x100 + 0x10;
+		struct symbol *sym = dso__find_symbol(dso, addr);
+
+		if (!sym || symbol__start(sym) != (u64)i * 0x100) {
+			pr_debug("Lookup failed at idx %u (addr=%#" PRIx64 ")\n", i, addr);
+			symbol__put(sym);
+			goto out;
+		}
+		symbol__put(sym);
+	}
+
+	ret = TEST_OK;
+out:
+	dso__put(dso);
+	return ret;
+}
+
 static int test__symbols(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
 {
 	char filename[PATH_MAX];
 	struct test_info ti;
 	int ret;
 
+	ret = test_symbols_sorted_insert();
+	if (ret != TEST_OK)
+		return ret;
+
+	ret = test_symbols_shrink();
+	if (ret != TEST_OK)
+		return ret;
+
+	ret = test_dsos_shrink();
+	if (ret != TEST_OK)
+		return ret;
+
+	ret = test_session_alloc_shrink();
+	if (ret != TEST_OK)
+		return ret;
+
 	ret = init_test_info(&ti);
 	if (ret != TEST_OK)
 		return ret;
Signature · 1 line
2.56.0.313.g2479322e68-goog
#46 lore raw

Re: [PATCH v2 18/18] perf test symbols: Add tests for symbol shrinking and session alloc wrappers

Quoted 2 lines · Ian Rogers

+ if (!dso__can_shrink_symbols(dso) || dso__kernel(dso))
+ return TEST_OK;

This passes without testing anything when the DSO can't be shrunk, for example a perf binary without .symtab. TEST_SKIP would make that visible.

No test makes an allocation fail, so the shrink-and-retry path in the session wrappers, where 12/18 combines with the 15-17/18 plumbing, never runs. Other gaps:

- a reload after the file is gone (15/18)
- a lookup from another thread during a reload
- kallsyms alias groups (09/18)
- the merge sort with many ties
- after a reload, only one symbol's identity is checked, not the count or the order

Thanks,
Alireza

author reviewer
#47 lore raw

Re: [PATCH v2 00/18] perf symbol: Reference counting, sorted arrays, and memory shrinking

In reply to Ian Rogers
Quoted 137 lines · Ian Rogers

When profiling large binaries or long-running workloads with many DSOs and threads, symbol and DSO tables frequently dominate perf's memory footprint and startup latency. Currently struct symbol has several structural limitations:

1. Every struct symbol embeds a 24-byte struct rb_node for the dso->symbols address rbtree, plus an optional negative-offset allocation prefix (symbol_conf.priv_size) for TUI browser indices, db-export IDs, or struct annotation state even when only a tiny fraction of symbols are ever annotated or exported.
2. Unlike struct map, struct dso, and struct thread, struct symbol is not reference-counted: once loaded into a struct dso, symbols can never be safely evicted while hist_entry, callchain_cursor_node, or map_symbol references exist, and dso__symbols() has no internal synchronization against concurrent readers/writers.
3. ELF symbol table loading (dso__load_sym_internal()) repeatedly invokes libelf section/program header lookups per symbol, calls elf_sym__name() before filtering out non-function/non-allocated symbols, runs Rust/Java demanglers on plain C symbols without fast-path prefix checks, and resorts dso->symbols with qsort() even when ELF symbol tables are already largely ordered in ascending address runs.

This series modernizes struct symbol to match the rest of tools/perf/util (DECLARE_RC_STRUCT, struct rw_semaphore, sorted pointer arrays) and introduces symbol/DSO memory shrinking and faster ELF symbol loading:

- Patch 1 adds perf bench internals symbols and perf bench internals maps to benchmark ELF symbol loading, bulk symbol insertion/sorting/lookup, incremental symbol appends, ascending map insertion, and incremental map appends.
- Patch 2 adds fast-path prefix checks in dso__demangle_sym() (before calling rust_demangle_demangle(), keeping demangle-rust-v0.c unmodified relative to upstream rustc-demangle) and in java_demangle_sym().
- Patches 3-6 encapsulate struct symbol field access behind accessor functions (annotated with __attribute__((nonnull))) and replace the negative-offset symbol_conf.priv_size allocation prefix with on-demand browser index calculation and per-DSO/per-exporter hashmaps for db-export and struct annotation.
- Patch 7 replaces dso->symbols (rb_root_cached) and dso->symbol_names with a locked struct symbols container backed by sorted pointer arrays (symbols and symbols_by_name) protected by a struct rw_semaphore, removing struct rb_node from struct symbol.
- Patch 8 wraps struct symbol in DECLARE_RC_STRUCT(symbol) with refcount_t (symbol__get() / symbol__put() / symbol__zput()) for sanitizer-checked reference counting across map_symbol, addr_location, hist_entry, callchain, thread_stack, and db-export.
- Patch 9 replaces qsort() in struct symbols with a natural-run merge sort and galloping merge (taking advantage of pre-ordered runs in .symtab/.dynsym and kallsyms) plus an in-place binary insertion fast path for small incremental appends.
- Patch 10 applies the same incremental binary-insertion sort and an O(1) tail-insertion fast path to struct maps for /proc/<pid>/maps synthesis and runtime PERF_RECORD_MMAP2 / JIT map insertions.
- Patch 11 caches ELF program and section headers during dso__load_sym_internal(), defers elf_sym__name() string table lookups until after symbol filtering, and runs symbols__fixup_end() / symbols__fixup_duplicate() once per DSO load.
- Patches 12-17 add down_read_trylock() / down_write_trylock(), protect machines->guests with a rw_semaphore, implement symbols__shrink(), dso__shrink(), dsos__shrink(), and perf_session__shrink() (with transparent on-demand reload in dso__load() that preserves pointer identity for retained symbols), and provide perf_session__malloc() / zalloc() / calloc() / realloc() / strdup() wrappers that automatically shrink unreferenced symbols and DSOs on allocation failure.
- Patch 18 adds unit tests in perf test symbols covering sorted symbol insertion, symbol/DSO shrinking, transparent DSO reload after shrink, and session allocation wrappers.

End-to-end benchmark results across the series:

perf bench internals symbols (perf binary):

    Average DSO load + sort:      7.348 msec ->   5.324 msec (-27.5%)
    Average DSO memory usage:        1401 KB ->      1308 KB ( -6.6%)
    Average bulk insert + lookup: 1453.95 us -> 1161.00 us   (-20.1%)
    Average append + lookup (200 interior inserts into 10,000 symbols):
      rbtree: 42.85 us -> array+qsort: 40429.75 us -> array+binary-insert: 152.70 us

perf bench internals symbols -b libLLVM.so.21.1:

    Average DSO load + sort:     81.196 msec ->  62.582 msec (-22.9%)
    Average DSO memory usage:        9530 KB ->      9164 KB ( -3.8%)

perf bench internals symbols -b libLLVM.so.21.1 --no-demangle:

    Average DSO load + sort:     32.675 msec ->  19.122 msec (-41.5%)
    Average DSO memory usage:        7589 KB ->      7229 KB ( -4.7%)

perf bench internals maps:

    Average ascending insert + lookup:   63.37 us -> 58.97 us ( -6.9%)
    Average append + lookup:           7477.45 us -> 96.67 us (-98.7%, 77.3x faster)

Net performance and memory trade-offs:
- Replacing the per-symbol rbtree (struct rb_node) and negative-offset symbol_conf.priv_size allocation prefix with a contiguous sorted pointer array reduces per-symbol heap overhead by 16 to 24+ bytes (saving 3.8% to 6.6% of total DSO symbol table heap memory unconditionally, and allowing unreferenced symbols and DSOs to be shrunk on demand under memory pressure).
- For the dominant symbol workloads—loading ELF binaries (-22.9% to -41.5%) and bulk symbol insertion, sorting, and lookup (-20.1%)—the contiguous array, natural-run merge sort, ELF header caching, and demangler fast paths yield substantial speedups over the rbtree baseline.
- For synthetic single-symbol interior insertions interleaved with immediate lookups into an already-large 10,000-symbol DSO (dso__insert_symbol()), an rbtree rotation takes ~0.21 us/op (42.85 us for 200 ops) whereas shifting the contiguous pointer array tail with memmove() after binary search takes ~0.76 us/op (152.70 us for 200 ops, down from 40429.75 us with naive qsort()). In real workloads, symbols are either loaded in bulk (dso__load()) or inserted into small per-prog BPF/JIT DSOs where the array fits in a few cache lines, making this sub-microsecond difference negligible in practice.
- Overall, the series reduces memory consumption across all workloads while significantly improving—or remaining neutral on—runtime performance.

v2:
- Rebased onto perf-tools-next (dropping "perf symbols: Fix broken ELF_C_READ_MMAP fallback guard", which was merged upstream, and splitting "perf libdw: Optimize fallback logic for missing DWARF" into a separate fixes series).
- Added 'perf bench internals symbols' and 'perf bench internals maps' benchmarks (Patch 1) and included before/after numbers in the optimization patches.
- Added __attribute__((nonnull)) annotations to struct symbol accessor functions (Patch 3).
- Split the symbol_conf.priv_size removal patch into three separate patches for map_browser (Patch 4), db-export (Patch 5), and annotate (Patch 6).
- Added dedicated optimization patches for Rust/Java demangler prefix checks (Patch 2), natural-run merge sort and binary insertion in struct symbols (Patch 9), tail-insertion and incremental binary-insertion sort in struct maps (Patch 10), and ELF program/section header caching and deferred symbol name lookup (Patch 11).
- Replaced periodic heuristic LRU shrinking and windowed ELF symbol loading with on-demand shrinking (symbols__shrink(), dso__shrink(), dsos__shrink(), perf_session__shrink()) triggered on allocation failure via session-scoped allocation wrappers, plus transparent DSO reload preserving retained symbol pointers (Patches 12-18).

Quoted 4 lines

v1: https://lore.kernel.org/lkml/[email protected]/

Assisted-by Antigravity:gemini-3.1-pro

So Sashiko failed to apply the patches (local Sashiko runs were clean) because I had these four patches applied locally:
- Alireza's "perf symbols: Fix broken ELF_C_READ_MMAP fallback guard"
https://lore.kernel.org/linux-perf-users/[email protected]/
- My fixes in "perf: Fix libdw addr2line fallback, pr_debugN
overhead, and cumulative hist decay"
https://lore.kernel.org/linux-perf-users/[email protected]/

Hopefully the patches above won't take too long to land, so I'll hold off sending v3 until then.

Thanks,
Ian

Quoted 138 lines

Ian Rogers (18):
perf bench internals: Add symbols and maps benchmarks perf demangle: Add fast-path prefix checks for Rust and Java
demanglers
perf symbol: Add accessor functions for struct symbol fields perf map_browser: Compute symbol browser index on demand perf db-export: Store symbol database IDs in a hashmap perf annotate: Move struct annotation to a hashmap and remove
symbol_conf.priv_size
perf symbol: Switch backing storage from rbtree to struct symbols
array
perf symbol: Add reference counting and DECLARE_RC_STRUCT(symbol) perf symbol: Optimize symbol sorting with natural-run merge sort and
binary insertion
perf maps: Optimize tail insertion and small-append sorting perf symbol-elf: Cache ELF program/section headers and defer symbol
name lookup
perf rwsem: Add down_read_trylock() and down_write_trylock() perf vdso: Constify dso parameter in dso__is_vdso()
perf machine: Protect machines->guests rbtree with a rw_semaphore perf symbol: Add memory shrinking for symbols, DSOs, and machines perf session: Add dso__session() and session-scoped allocation
wrappers
perf symbol: Use session allocation wrappers in symbol, DSO, thread,
and hist paths
perf test symbols: Add tests for symbol shrinking and session alloc
wrappers

 tools/perf/Documentation/perf-bench.txt       |    6 +
 .../arch/powerpc/util/skip-callchain-idx.c    |    2 +-
 tools/perf/arch/powerpc/util/sym-handling.c   |   40 +-
 tools/perf/bench/Build                        |    2 +
 tools/perf/bench/bench.h                      |    2 +
 tools/perf/bench/maps.c                       |  186 +++
 tools/perf/bench/symbols.c                    |  239 +++
 tools/perf/builtin-annotate.c                 |   22 +-
 tools/perf/builtin-bench.c                    |    2 +
 tools/perf/builtin-c2c.c                      |   14 -
 tools/perf/builtin-diff.c                     |    4 +-
 tools/perf/builtin-kallsyms.c                 |    9 +-
 tools/perf/builtin-kmem.c                     |   82 +-
 tools/perf/builtin-kwork.c                    |    4 +-
 tools/perf/builtin-lock.c                     |   25 +-
 tools/perf/builtin-mem.c                      |    2 +-
 tools/perf/builtin-report.c                   |   14 -
 tools/perf/builtin-sched.c                    |    6 +-
 tools/perf/builtin-script.c                   |   18 +-
 tools/perf/builtin-timechart.c                |    2 +-
 tools/perf/builtin-top.c                      |   24 +-
 tools/perf/builtin-trace.c                    |   23 +-
 tools/perf/tests/builtin-test.c               |    1 -
 tools/perf/tests/dlfilter-test.c              |   12 +-
 tools/perf/tests/dwarf-unwind.c               |    2 +-
 tools/perf/tests/hists_common.c               |    9 +-
 tools/perf/tests/hists_cumulate.c             |    8 +-
 tools/perf/tests/hists_filter.c               |   10 +-
 tools/perf/tests/hists_link.c                 |   16 +-
 tools/perf/tests/hists_output.c               |    9 +-
 tools/perf/tests/kallsyms-split.c             |    5 +-
 tools/perf/tests/maps.c                       |   58 +
 tools/perf/tests/pe-file-parsing.c            |    1 +
 tools/perf/tests/symbols.c                    |  454 +++++-
 tools/perf/tests/vmlinux-kallsyms.c           |  187 ++-
 tools/perf/ui/browser.c                       |    3 +
 tools/perf/ui/browsers/annotate.c             |   10 +-
 tools/perf/ui/browsers/hists.c                |   35 +-
 tools/perf/ui/browsers/map.c                  |   88 +-
 tools/perf/ui/gtk/annotate.c                  |    6 +-
 tools/perf/ui/stdio/hist.c                    |    6 +-
 tools/perf/util/addr2line.c                   |    8 +-
 tools/perf/util/addr_location.c               |    4 +
 .../perf/util/annotate-arch/annotate-alpha.c  |    5 +-
 .../perf/util/annotate-arch/annotate-arm64.c  |   12 +-
 .../util/annotate-arch/annotate-loongarch.c   |   15 +-
 tools/perf/util/annotate-arch/annotate-s390.c |    6 +-
 tools/perf/util/annotate-arch/annotate-x86.c  |   14 +-
 tools/perf/util/annotate-data.c               |   12 +-
 tools/perf/util/annotate.c                    |  211 ++-
 tools/perf/util/annotate.h                    |    6 +-
 tools/perf/util/auxtrace.c                    |  111 +-
 tools/perf/util/block-info.c                  |   12 +-
 tools/perf/util/bpf_lock_contention.c         |   35 +-
 tools/perf/util/c2c-function.c                |   32 +-
 tools/perf/util/call-path.c                   |   15 +-
 tools/perf/util/callchain.c                   |   23 +-
 tools/perf/util/capstone.c                    |   22 +-
 tools/perf/util/data-convert-json.c           |    4 +-
 tools/perf/util/db-export.c                   |   99 +-
 tools/perf/util/db-export.h                   |    8 +
 tools/perf/util/debug.c                       |    3 +-
 tools/perf/util/demangle-java.c               |   23 +-
 tools/perf/util/disasm.c                      |   69 +-
 tools/perf/util/dlfilter.c                    |   12 +-
 tools/perf/util/dso.c                         |  257 ++-
 tools/perf/util/dso.h                         |   94 +-
 tools/perf/util/dsos.c                        |   76 +
 tools/perf/util/dsos.h                        |    3 +
 tools/perf/util/event.c                       |   12 +-
 tools/perf/util/evsel_fprintf.c               |    2 +-
 tools/perf/util/hist.c                        |   70 +-
 tools/perf/util/intel-pt.c                    |   74 +-
 tools/perf/util/libbfd.c                      |   14 +-
 tools/perf/util/libdw.c                       |    5 +-
 tools/perf/util/llvm.c                        |    8 +-
 tools/perf/util/lock-contention.c             |    8 +-
 tools/perf/util/machine.c                     |  189 ++-
 tools/perf/util/machine.h                     |    9 +
 tools/perf/util/map.c                         |   27 +-
 tools/perf/util/map.h                         |   18 +-
 tools/perf/util/map_symbol.c                  |    4 +-
 tools/perf/util/maps.c                        |  284 +++-
 tools/perf/util/mutex.h                       |    3 +
 tools/perf/util/probe-event.c                 |  148 +-
 tools/perf/util/python.c                      |   36 +-
 tools/perf/util/rwsem.c                       |   20 +
 tools/perf/util/rwsem.h                       |    2 +
 tools/perf/util/session.c                     |   78 +
 tools/perf/util/session.h                     |    8 +
 tools/perf/util/sort.c                        |   47 +-
 tools/perf/util/srcline.c                     |   22 +-
 tools/perf/util/symbol-elf.c                  |  311 +++-
 tools/perf/util/symbol.c                      | 1438 +++++++++++++----
 tools/perf/util/symbol.h                      |  187 ++-
 tools/perf/util/symbol_conf.h                 |    1 -
 tools/perf/util/symbol_fprintf.c              |   37 +-
 tools/perf/util/thread-stack.c                |   18 +-
 tools/perf/util/threads.c                     |   17 +-
 tools/perf/util/unwind-libdw.c                |    4 +-
 tools/perf/util/unwind-libunwind.c            |    2 +-
 tools/perf/util/vdso.c                        |    2 +-
 tools/perf/util/vdso.h                        |    2 +-
 103 files changed, 4557 insertions(+), 1389 deletions(-)
 create mode 100644 tools/perf/bench/maps.c
 create mode 100644 tools/perf/bench/symbols.c

--
2.56.0.313.g2479322e68-goog

#48 lore raw

Re: [PATCH v2 00/18] perf symbol: Reference counting, sorted arrays, and memory shrinking

Hi Ian,

I applied v2 on perf-tools-next e74880764bf9 with the ELF_C_READ_MMAP fix and your three fixes, and compared it with the same tree without the 18 patches. I posted comments are on the individual patches; this is the summary.

On a profile of a large C++ service, about 1M symbols, perf script and perf r eport output is byte-identical and roughly 22-32% faster:

Execution time and peak RSS before and after:
perf script 3.3s 334 MiB vs 2.25s 332 MiB
perf report --sort dso,sym 3.0s 389 MiB vs 2.25s 386 MiB
perf report --children -g caller 4.6s 706 MiB vs 3.60s 703 MiB

Peak RSS barely moves. sizeof(struct symbol) goes from 48 to 32 bytes, but the address array adds a pointer per symbol back, so the net is about 8 bytes per symbol before malloc rounding. That is under 1% here.

Thanks
Alireza

Quoted 296 lines · Ian Rogers
Quoted 137 lines · Ian Rogers

When profiling large binaries or long-running workloads with many DSOs and threads, symbol and DSO tables frequently dominate perf's memory footprint and startup latency. Currently struct symbol has several structural limitations:

1. Every struct symbol embeds a 24-byte struct rb_node for the dso->symbols address rbtree, plus an optional negative-offset allocation prefix (symbol_conf.priv_size) for TUI browser indices, db-export IDs, or struct annotation state even when only a tiny fraction of symbols are ever annotated or exported.
2. Unlike struct map, struct dso, and struct thread, struct symbol is not reference-counted: once loaded into a struct dso, symbols can never be safely evicted while hist_entry, callchain_cursor_node, or map_symbol references exist, and dso__symbols() has no internal synchronization against concurrent readers/writers.
3. ELF symbol table loading (dso__load_sym_internal()) repeatedly invokes libelf section/program header lookups per symbol, calls elf_sym__name() before filtering out non-function/non-allocated symbols, runs Rust/Java demanglers on plain C symbols without fast-path prefix checks, and resorts dso->symbols with qsort() even when ELF symbol tables are already largely ordered in ascending address runs.

This series modernizes struct symbol to match the rest of tools/perf/util (DECLARE_RC_STRUCT, struct rw_semaphore, sorted pointer arrays) and introduces symbol/DSO memory shrinking and faster ELF symbol loading:

- Patch 1 adds perf bench internals symbols and perf bench internals maps to benchmark ELF symbol loading, bulk symbol insertion/sorting/lookup, incremental symbol appends, ascending map insertion, and incremental map appends.
- Patch 2 adds fast-path prefix checks in dso__demangle_sym() (before calling rust_demangle_demangle(), keeping demangle-rust-v0.c unmodified relative to upstream rustc-demangle) and in java_demangle_sym().
- Patches 3-6 encapsulate struct symbol field access behind accessor functions (annotated with __attribute__((nonnull))) and replace the negative-offset symbol_conf.priv_size allocation prefix with on-demand browser index calculation and per-DSO/per-exporter hashmaps for db-export and struct annotation.
- Patch 7 replaces dso->symbols (rb_root_cached) and dso->symbol_names with a locked struct symbols container backed by sorted pointer arrays (symbols and symbols_by_name) protected by a struct rw_semaphore, removing struct rb_node from struct symbol.
- Patch 8 wraps struct symbol in DECLARE_RC_STRUCT(symbol) with refcount_t (symbol__get() / symbol__put() / symbol__zput()) for sanitizer-checked reference counting across map_symbol, addr_location, hist_entry, callchain, thread_stack, and db-export.
- Patch 9 replaces qsort() in struct symbols with a natural-run merge sort and galloping merge (taking advantage of pre-ordered runs in .symtab/.dynsym and kallsyms) plus an in-place binary insertion fast path for small incremental appends.
- Patch 10 applies the same incremental binary-insertion sort and an O(1) tail-insertion fast path to struct maps for /proc/<pid>/maps synthesis and runtime PERF_RECORD_MMAP2 / JIT map insertions.
- Patch 11 caches ELF program and section headers during dso__load_sym_internal(), defers elf_sym__name() string table lookups until after symbol filtering, and runs symbols__fixup_end() / symbols__fixup_duplicate() once per DSO load.
- Patches 12-17 add down_read_trylock() / down_write_trylock(), protect machines->guests with a rw_semaphore, implement symbols__shrink(), dso__shrink(), dsos__shrink(), and perf_session__shrink() (with transparent on-demand reload in dso__load() that preserves pointer identity for retained symbols), and provide perf_session__malloc() / zalloc() / calloc() / realloc() / strdup() wrappers that automatically shrink unreferenced symbols and DSOs on allocation failure.
- Patch 18 adds unit tests in perf test symbols covering sorted symbol insertion, symbol/DSO shrinking, transparent DSO reload after shrink, and session allocation wrappers.

End-to-end benchmark results across the series:

perf bench internals symbols (perf binary):

    Average DSO load + sort:      7.348 msec ->   5.324 msec (-27.5%)
    Average DSO memory usage:        1401 KB ->      1308 KB ( -6.6%)
    Average bulk insert + lookup: 1453.95 us -> 1161.00 us   (-20.1%)
    Average append + lookup (200 interior inserts into 10,000 symbols):
      rbtree: 42.85 us -> array+qsort: 40429.75 us -> array+binary-insert: 152.70 us

perf bench internals symbols -b libLLVM.so.21.1:

    Average DSO load + sort:     81.196 msec ->  62.582 msec (-22.9%)
    Average DSO memory usage:        9530 KB ->      9164 KB ( -3.8%)

perf bench internals symbols -b libLLVM.so.21.1 --no-demangle:

    Average DSO load + sort:     32.675 msec ->  19.122 msec (-41.5%)
    Average DSO memory usage:        7589 KB ->      7229 KB ( -4.7%)

perf bench internals maps:

    Average ascending insert + lookup:   63.37 us -> 58.97 us ( -6.9%)
    Average append + lookup:           7477.45 us -> 96.67 us (-98.7%, 77.3x faster)

Net performance and memory trade-offs:
- Replacing the per-symbol rbtree (struct rb_node) and negative-offset symbol_conf.priv_size allocation prefix with a contiguous sorted pointer array reduces per-symbol heap overhead by 16 to 24+ bytes (saving 3.8% to 6.6% of total DSO symbol table heap memory unconditionally, and allowing unreferenced symbols and DSOs to be shrunk on demand under memory pressure).
- For the dominant symbol workloads—loading ELF binaries (-22.9% to -41.5%) and bulk symbol insertion, sorting, and lookup (-20.1%)—the contiguous array, natural-run merge sort, ELF header caching, and demangler fast paths yield substantial speedups over the rbtree baseline.
- For synthetic single-symbol interior insertions interleaved with immediate lookups into an already-large 10,000-symbol DSO (dso__insert_symbol()), an rbtree rotation takes ~0.21 us/op (42.85 us for 200 ops) whereas shifting the contiguous pointer array tail with memmove() after binary search takes ~0.76 us/op (152.70 us for 200 ops, down from 40429.75 us with naive qsort()). In real workloads, symbols are either loaded in bulk (dso__load()) or inserted into small per-prog BPF/JIT DSOs where the array fits in a few cache lines, making this sub-microsecond difference negligible in practice.
- Overall, the series reduces memory consumption across all workloads while significantly improving—or remaining neutral on—runtime performance.

v2:
- Rebased onto perf-tools-next (dropping "perf symbols: Fix broken ELF_C_READ_MMAP fallback guard", which was merged upstream, and splitting "perf libdw: Optimize fallback logic for missing DWARF" into a separate fixes series).
- Added 'perf bench internals symbols' and 'perf bench internals maps' benchmarks (Patch 1) and included before/after numbers in the optimization patches.
- Added __attribute__((nonnull)) annotations to struct symbol accessor functions (Patch 3).
- Split the symbol_conf.priv_size removal patch into three separate patches for map_browser (Patch 4), db-export (Patch 5), and annotate (Patch 6).
- Added dedicated optimization patches for Rust/Java demangler prefix checks (Patch 2), natural-run merge sort and binary insertion in struct symbols (Patch 9), tail-insertion and incremental binary-insertion sort in struct maps (Patch 10), and ELF program/section header caching and deferred symbol name lookup (Patch 11).
- Replaced periodic heuristic LRU shrinking and windowed ELF symbol loading with on-demand shrinking (symbols__shrink(), dso__shrink(), dsos__shrink(), perf_session__shrink()) triggered on allocation failure via session-scoped allocation wrappers, plus transparent DSO reload preserving retained symbol pointers (Patches 12-18).

Quoted 4 lines

v1: https://lore.kernel.org/lkml/[email protected]/

Assisted-by Antigravity:gemini-3.1-pro

So Sashiko failed to apply the patches (local Sashiko runs were clean) because I had these four patches applied locally:
- Alireza's "perf symbols: Fix broken ELF_C_READ_MMAP fallback guard"
https://lore.kernel.org/linux-perf-users/[email protected]/
- My fixes in "perf: Fix libdw addr2line fallback, pr_debugN
overhead, and cumulative hist decay"
https://lore.kernel.org/linux-perf-users/[email protected]/

Hopefully the patches above won't take too long to land, so I'll hold off sending v3 until then.

Thanks,
Ian

Quoted 138 lines

Ian Rogers (18):
perf bench internals: Add symbols and maps benchmarks perf demangle: Add fast-path prefix checks for Rust and Java
demanglers
perf symbol: Add accessor functions for struct symbol fields perf map_browser: Compute symbol browser index on demand perf db-export: Store symbol database IDs in a hashmap perf annotate: Move struct annotation to a hashmap and remove
symbol_conf.priv_size
perf symbol: Switch backing storage from rbtree to struct symbols
array
perf symbol: Add reference counting and DECLARE_RC_STRUCT(symbol) perf symbol: Optimize symbol sorting with natural-run merge sort and
binary insertion
perf maps: Optimize tail insertion and small-append sorting perf symbol-elf: Cache ELF program/section headers and defer symbol
name lookup
perf rwsem: Add down_read_trylock() and down_write_trylock() perf vdso: Constify dso parameter in dso__is_vdso()
perf machine: Protect machines->guests rbtree with a rw_semaphore perf symbol: Add memory shrinking for symbols, DSOs, and machines perf session: Add dso__session() and session-scoped allocation
wrappers
perf symbol: Use session allocation wrappers in symbol, DSO, thread,
and hist paths
perf test symbols: Add tests for symbol shrinking and session alloc
wrappers

 tools/perf/Documentation/perf-bench.txt       |    6 +
 .../arch/powerpc/util/skip-callchain-idx.c    |    2 +-
 tools/perf/arch/powerpc/util/sym-handling.c   |   40 +-
 tools/perf/bench/Build                        |    2 +
 tools/perf/bench/bench.h                      |    2 +
 tools/perf/bench/maps.c                       |  186 +++
 tools/perf/bench/symbols.c                    |  239 +++
 tools/perf/builtin-annotate.c                 |   22 +-
 tools/perf/builtin-bench.c                    |    2 +
 tools/perf/builtin-c2c.c                      |   14 -
 tools/perf/builtin-diff.c                     |    4 +-
 tools/perf/builtin-kallsyms.c                 |    9 +-
 tools/perf/builtin-kmem.c                     |   82 +-
 tools/perf/builtin-kwork.c                    |    4 +-
 tools/perf/builtin-lock.c                     |   25 +-
 tools/perf/builtin-mem.c                      |    2 +-
 tools/perf/builtin-report.c                   |   14 -
 tools/perf/builtin-sched.c                    |    6 +-
 tools/perf/builtin-script.c                   |   18 +-
 tools/perf/builtin-timechart.c                |    2 +-
 tools/perf/builtin-top.c                      |   24 +-
 tools/perf/builtin-trace.c                    |   23 +-
 tools/perf/tests/builtin-test.c               |    1 -
 tools/perf/tests/dlfilter-test.c              |   12 +-
 tools/perf/tests/dwarf-unwind.c               |    2 +-
 tools/perf/tests/hists_common.c               |    9 +-
 tools/perf/tests/hists_cumulate.c             |    8 +-
 tools/perf/tests/hists_filter.c               |   10 +-
 tools/perf/tests/hists_link.c                 |   16 +-
 tools/perf/tests/hists_output.c               |    9 +-
 tools/perf/tests/kallsyms-split.c             |    5 +-
 tools/perf/tests/maps.c                       |   58 +
 tools/perf/tests/pe-file-parsing.c            |    1 +
 tools/perf/tests/symbols.c                    |  454 +++++-
 tools/perf/tests/vmlinux-kallsyms.c           |  187 ++-
 tools/perf/ui/browser.c                       |    3 +
 tools/perf/ui/browsers/annotate.c             |   10 +-
 tools/perf/ui/browsers/hists.c                |   35 +-
 tools/perf/ui/browsers/map.c                  |   88 +-
 tools/perf/ui/gtk/annotate.c                  |    6 +-
 tools/perf/ui/stdio/hist.c                    |    6 +-
 tools/perf/util/addr2line.c                   |    8 +-
 tools/perf/util/addr_location.c               |    4 +
 .../perf/util/annotate-arch/annotate-alpha.c  |    5 +-
 .../perf/util/annotate-arch/annotate-arm64.c  |   12 +-
 .../util/annotate-arch/annotate-loongarch.c   |   15 +-
 tools/perf/util/annotate-arch/annotate-s390.c |    6 +-
 tools/perf/util/annotate-arch/annotate-x86.c  |   14 +-
 tools/perf/util/annotate-data.c               |   12 +-
 tools/perf/util/annotate.c                    |  211 ++-
 tools/perf/util/annotate.h                    |    6 +-
 tools/perf/util/auxtrace.c                    |  111 +-
 tools/perf/util/block-info.c                  |   12 +-
 tools/perf/util/bpf_lock_contention.c         |   35 +-
 tools/perf/util/c2c-function.c                |   32 +-
 tools/perf/util/call-path.c                   |   15 +-
 tools/perf/util/callchain.c                   |   23 +-
 tools/perf/util/capstone.c                    |   22 +-
 tools/perf/util/data-convert-json.c           |    4 +-
 tools/perf/util/db-export.c                   |   99 +-
 tools/perf/util/db-export.h                   |    8 +
 tools/perf/util/debug.c                       |    3 +-
 tools/perf/util/demangle-java.c               |   23 +-
 tools/perf/util/disasm.c                      |   69 +-
 tools/perf/util/dlfilter.c                    |   12 +-
 tools/perf/util/dso.c                         |  257 ++-
 tools/perf/util/dso.h                         |   94 +-
 tools/perf/util/dsos.c                        |   76 +
 tools/perf/util/dsos.h                        |    3 +
 tools/perf/util/event.c                       |   12 +-
 tools/perf/util/evsel_fprintf.c               |    2 +-
 tools/perf/util/hist.c                        |   70 +-
 tools/perf/util/intel-pt.c                    |   74 +-
 tools/perf/util/libbfd.c                      |   14 +-
 tools/perf/util/libdw.c                       |    5 +-
 tools/perf/util/llvm.c                        |    8 +-
 tools/perf/util/lock-contention.c             |    8 +-
 tools/perf/util/machine.c                     |  189 ++-
 tools/perf/util/machine.h                     |    9 +
 tools/perf/util/map.c                         |   27 +-
 tools/perf/util/map.h                         |   18 +-
 tools/perf/util/map_symbol.c                  |    4 +-
 tools/perf/util/maps.c                        |  284 +++-
 tools/perf/util/mutex.h                       |    3 +
 tools/perf/util/probe-event.c                 |  148 +-
 tools/perf/util/python.c                      |   36 +-
 tools/perf/util/rwsem.c                       |   20 +
 tools/perf/util/rwsem.h                       |    2 +
 tools/perf/util/session.c                     |   78 +
 tools/perf/util/session.h                     |    8 +
 tools/perf/util/sort.c                        |   47 +-
 tools/perf/util/srcline.c                     |   22 +-
 tools/perf/util/symbol-elf.c                  |  311 +++-
 tools/perf/util/symbol.c                      | 1438 +++++++++++++----
 tools/perf/util/symbol.h                      |  187 ++-
 tools/perf/util/symbol_conf.h                 |    1 -
 tools/perf/util/symbol_fprintf.c              |   37 +-
 tools/perf/util/thread-stack.c                |   18 +-
 tools/perf/util/threads.c                     |   17 +-
 tools/perf/util/unwind-libdw.c                |    4 +-
 tools/perf/util/unwind-libunwind.c            |    2 +-
 tools/perf/util/vdso.c                        |    2 +-
 tools/perf/util/vdso.h                        |    2 +-
 103 files changed, 4557 insertions(+), 1389 deletions(-)
 create mode 100644 tools/perf/bench/maps.c
 create mode 100644 tools/perf/bench/symbols.c

--
2.56.0.313.g2479322e68-goog

author reviewer
#49 lore raw

Re: [PATCH v2 00/18] perf symbol: Reference counting, sorted arrays, and memory shrinking

Quoted 16 lines · Alireza Haghdoost

Hi Ian,

I applied v2 on perf-tools-next e74880764bf9 with the ELF_C_READ_MMAP fix and your three fixes, and compared it with the same tree without the 18 patches. I posted comments are on the individual patches; this is the summary.

On a profile of a large C++ service, about 1M symbols, perf script and perf r eport output is byte-identical and roughly 22-32% faster:

Execution time and peak RSS before and after:
perf script 3.3s 334 MiB vs 2.25s 332 MiB
perf report --sort dso,sym 3.0s 389 MiB vs 2.25s 386 MiB
perf report --children -g caller 4.6s 706 MiB vs 3.60s 703 MiB

Quoted 4 lines

Peak RSS barely moves. sizeof(struct symbol) goes from 48 to 32 bytes, but the address array adds a pointer per symbol back, so the net is about 8 bytes per symbol before malloc rounding. That is under 1% here.

Thanks for your work and running the numbers! I think we should aim to land your lazy loading patches ahead of these patches. The merge conflicts don't look like they'll be too terrible, wdyt?

Since the merge window for Linux v7.4 is currently open, I don't see anything landing before November 1st. So I can do a v3 incorporating your suggestions and rebase it on top of your patches when they land. I'll work on pulling out the fixes (you highlighted some you thought were particularly worthwhile) to mail for v7.4 for now.

Thanks,
Ian

Quoted 300 lines

Thanks
Alireza

Quoted 296 lines · Ian Rogers
Quoted 137 lines · Ian Rogers

When profiling large binaries or long-running workloads with many DSOs and threads, symbol and DSO tables frequently dominate perf's memory footprint and startup latency. Currently struct symbol has several structural limitations:

1. Every struct symbol embeds a 24-byte struct rb_node for the dso->symbols address rbtree, plus an optional negative-offset allocation prefix (symbol_conf.priv_size) for TUI browser indices, db-export IDs, or struct annotation state even when only a tiny fraction of symbols are ever annotated or exported.
2. Unlike struct map, struct dso, and struct thread, struct symbol is not reference-counted: once loaded into a struct dso, symbols can never be safely evicted while hist_entry, callchain_cursor_node, or map_symbol references exist, and dso__symbols() has no internal synchronization against concurrent readers/writers.
3. ELF symbol table loading (dso__load_sym_internal()) repeatedly invokes libelf section/program header lookups per symbol, calls elf_sym__name() before filtering out non-function/non-allocated symbols, runs Rust/Java demanglers on plain C symbols without fast-path prefix checks, and resorts dso->symbols with qsort() even when ELF symbol tables are already largely ordered in ascending address runs.

This series modernizes struct symbol to match the rest of tools/perf/util (DECLARE_RC_STRUCT, struct rw_semaphore, sorted pointer arrays) and introduces symbol/DSO memory shrinking and faster ELF symbol loading:

- Patch 1 adds perf bench internals symbols and perf bench internals maps to benchmark ELF symbol loading, bulk symbol insertion/sorting/lookup, incremental symbol appends, ascending map insertion, and incremental map appends.
- Patch 2 adds fast-path prefix checks in dso__demangle_sym() (before calling rust_demangle_demangle(), keeping demangle-rust-v0.c unmodified relative to upstream rustc-demangle) and in java_demangle_sym().
- Patches 3-6 encapsulate struct symbol field access behind accessor functions (annotated with __attribute__((nonnull))) and replace the negative-offset symbol_conf.priv_size allocation prefix with on-demand browser index calculation and per-DSO/per-exporter hashmaps for db-export and struct annotation.
- Patch 7 replaces dso->symbols (rb_root_cached) and dso->symbol_names with a locked struct symbols container backed by sorted pointer arrays (symbols and symbols_by_name) protected by a struct rw_semaphore, removing struct rb_node from struct symbol.
- Patch 8 wraps struct symbol in DECLARE_RC_STRUCT(symbol) with refcount_t (symbol__get() / symbol__put() / symbol__zput()) for sanitizer-checked reference counting across map_symbol, addr_location, hist_entry, callchain, thread_stack, and db-export.
- Patch 9 replaces qsort() in struct symbols with a natural-run merge sort and galloping merge (taking advantage of pre-ordered runs in .symtab/.dynsym and kallsyms) plus an in-place binary insertion fast path for small incremental appends.
- Patch 10 applies the same incremental binary-insertion sort and an O(1) tail-insertion fast path to struct maps for /proc/<pid>/maps synthesis and runtime PERF_RECORD_MMAP2 / JIT map insertions.
- Patch 11 caches ELF program and section headers during dso__load_sym_internal(), defers elf_sym__name() string table lookups until after symbol filtering, and runs symbols__fixup_end() / symbols__fixup_duplicate() once per DSO load.
- Patches 12-17 add down_read_trylock() / down_write_trylock(), protect machines->guests with a rw_semaphore, implement symbols__shrink(), dso__shrink(), dsos__shrink(), and perf_session__shrink() (with transparent on-demand reload in dso__load() that preserves pointer identity for retained symbols), and provide perf_session__malloc() / zalloc() / calloc() / realloc() / strdup() wrappers that automatically shrink unreferenced symbols and DSOs on allocation failure.
- Patch 18 adds unit tests in perf test symbols covering sorted symbol insertion, symbol/DSO shrinking, transparent DSO reload after shrink, and session allocation wrappers.

End-to-end benchmark results across the series:

perf bench internals symbols (perf binary):

    Average DSO load + sort:      7.348 msec ->   5.324 msec (-27.5%)
    Average DSO memory usage:        1401 KB ->      1308 KB ( -6.6%)
    Average bulk insert + lookup: 1453.95 us -> 1161.00 us   (-20.1%)
    Average append + lookup (200 interior inserts into 10,000 symbols):
      rbtree: 42.85 us -> array+qsort: 40429.75 us -> array+binary-insert: 152.70 us

perf bench internals symbols -b libLLVM.so.21.1:

    Average DSO load + sort:     81.196 msec ->  62.582 msec (-22.9%)
    Average DSO memory usage:        9530 KB ->      9164 KB ( -3.8%)

perf bench internals symbols -b libLLVM.so.21.1 --no-demangle:

    Average DSO load + sort:     32.675 msec ->  19.122 msec (-41.5%)
    Average DSO memory usage:        7589 KB ->      7229 KB ( -4.7%)

perf bench internals maps:

    Average ascending insert + lookup:   63.37 us -> 58.97 us ( -6.9%)
    Average append + lookup:           7477.45 us -> 96.67 us (-98.7%, 77.3x faster)

Net performance and memory trade-offs:
- Replacing the per-symbol rbtree (struct rb_node) and negative-offset symbol_conf.priv_size allocation prefix with a contiguous sorted pointer array reduces per-symbol heap overhead by 16 to 24+ bytes (saving 3.8% to 6.6% of total DSO symbol table heap memory unconditionally, and allowing unreferenced symbols and DSOs to be shrunk on demand under memory pressure).
- For the dominant symbol workloads—loading ELF binaries (-22.9% to -41.5%) and bulk symbol insertion, sorting, and lookup (-20.1%)—the contiguous array, natural-run merge sort, ELF header caching, and demangler fast paths yield substantial speedups over the rbtree baseline.
- For synthetic single-symbol interior insertions interleaved with immediate lookups into an already-large 10,000-symbol DSO (dso__insert_symbol()), an rbtree rotation takes ~0.21 us/op (42.85 us for 200 ops) whereas shifting the contiguous pointer array tail with memmove() after binary search takes ~0.76 us/op (152.70 us for 200 ops, down from 40429.75 us with naive qsort()). In real workloads, symbols are either loaded in bulk (dso__load()) or inserted into small per-prog BPF/JIT DSOs where the array fits in a few cache lines, making this sub-microsecond difference negligible in practice.
- Overall, the series reduces memory consumption across all workloads while significantly improving—or remaining neutral on—runtime performance.

v2:
- Rebased onto perf-tools-next (dropping "perf symbols: Fix broken ELF_C_READ_MMAP fallback guard", which was merged upstream, and splitting "perf libdw: Optimize fallback logic for missing DWARF" into a separate fixes series).
- Added 'perf bench internals symbols' and 'perf bench internals maps' benchmarks (Patch 1) and included before/after numbers in the optimization patches.
- Added __attribute__((nonnull)) annotations to struct symbol accessor functions (Patch 3).
- Split the symbol_conf.priv_size removal patch into three separate patches for map_browser (Patch 4), db-export (Patch 5), and annotate (Patch 6).
- Added dedicated optimization patches for Rust/Java demangler prefix checks (Patch 2), natural-run merge sort and binary insertion in struct symbols (Patch 9), tail-insertion and incremental binary-insertion sort in struct maps (Patch 10), and ELF program/section header caching and deferred symbol name lookup (Patch 11).
- Replaced periodic heuristic LRU shrinking and windowed ELF symbol loading with on-demand shrinking (symbols__shrink(), dso__shrink(), dsos__shrink(), perf_session__shrink()) triggered on allocation failure via session-scoped allocation wrappers, plus transparent DSO reload preserving retained symbol pointers (Patches 12-18).

Quoted 4 lines

v1: https://lore.kernel.org/lkml/[email protected]/

Assisted-by Antigravity:gemini-3.1-pro

So Sashiko failed to apply the patches (local Sashiko runs were clean) because I had these four patches applied locally:
- Alireza's "perf symbols: Fix broken ELF_C_READ_MMAP fallback guard"
https://lore.kernel.org/linux-perf-users/[email protected]/
- My fixes in "perf: Fix libdw addr2line fallback, pr_debugN
overhead, and cumulative hist decay"
https://lore.kernel.org/linux-perf-users/[email protected]/

Hopefully the patches above won't take too long to land, so I'll hold off sending v3 until then.

Thanks,
Ian

Quoted 138 lines

Ian Rogers (18):
perf bench internals: Add symbols and maps benchmarks perf demangle: Add fast-path prefix checks for Rust and Java
demanglers
perf symbol: Add accessor functions for struct symbol fields perf map_browser: Compute symbol browser index on demand perf db-export: Store symbol database IDs in a hashmap perf annotate: Move struct annotation to a hashmap and remove
symbol_conf.priv_size
perf symbol: Switch backing storage from rbtree to struct symbols
array
perf symbol: Add reference counting and DECLARE_RC_STRUCT(symbol) perf symbol: Optimize symbol sorting with natural-run merge sort and
binary insertion
perf maps: Optimize tail insertion and small-append sorting perf symbol-elf: Cache ELF program/section headers and defer symbol
name lookup
perf rwsem: Add down_read_trylock() and down_write_trylock() perf vdso: Constify dso parameter in dso__is_vdso()
perf machine: Protect machines->guests rbtree with a rw_semaphore perf symbol: Add memory shrinking for symbols, DSOs, and machines perf session: Add dso__session() and session-scoped allocation
wrappers
perf symbol: Use session allocation wrappers in symbol, DSO, thread,
and hist paths
perf test symbols: Add tests for symbol shrinking and session alloc
wrappers

 tools/perf/Documentation/perf-bench.txt       |    6 +
 .../arch/powerpc/util/skip-callchain-idx.c    |    2 +-
 tools/perf/arch/powerpc/util/sym-handling.c   |   40 +-
 tools/perf/bench/Build                        |    2 +
 tools/perf/bench/bench.h                      |    2 +
 tools/perf/bench/maps.c                       |  186 +++
 tools/perf/bench/symbols.c                    |  239 +++
 tools/perf/builtin-annotate.c                 |   22 +-
 tools/perf/builtin-bench.c                    |    2 +
 tools/perf/builtin-c2c.c                      |   14 -
 tools/perf/builtin-diff.c                     |    4 +-
 tools/perf/builtin-kallsyms.c                 |    9 +-
 tools/perf/builtin-kmem.c                     |   82 +-
 tools/perf/builtin-kwork.c                    |    4 +-
 tools/perf/builtin-lock.c                     |   25 +-
 tools/perf/builtin-mem.c                      |    2 +-
 tools/perf/builtin-report.c                   |   14 -
 tools/perf/builtin-sched.c                    |    6 +-
 tools/perf/builtin-script.c                   |   18 +-
 tools/perf/builtin-timechart.c                |    2 +-
 tools/perf/builtin-top.c                      |   24 +-
 tools/perf/builtin-trace.c                    |   23 +-
 tools/perf/tests/builtin-test.c               |    1 -
 tools/perf/tests/dlfilter-test.c              |   12 +-
 tools/perf/tests/dwarf-unwind.c               |    2 +-
 tools/perf/tests/hists_common.c               |    9 +-
 tools/perf/tests/hists_cumulate.c             |    8 +-
 tools/perf/tests/hists_filter.c               |   10 +-
 tools/perf/tests/hists_link.c                 |   16 +-
 tools/perf/tests/hists_output.c               |    9 +-
 tools/perf/tests/kallsyms-split.c             |    5 +-
 tools/perf/tests/maps.c                       |   58 +
 tools/perf/tests/pe-file-parsing.c            |    1 +
 tools/perf/tests/symbols.c                    |  454 +++++-
 tools/perf/tests/vmlinux-kallsyms.c           |  187 ++-
 tools/perf/ui/browser.c                       |    3 +
 tools/perf/ui/browsers/annotate.c             |   10 +-
 tools/perf/ui/browsers/hists.c                |   35 +-
 tools/perf/ui/browsers/map.c                  |   88 +-
 tools/perf/ui/gtk/annotate.c                  |    6 +-
 tools/perf/ui/stdio/hist.c                    |    6 +-
 tools/perf/util/addr2line.c                   |    8 +-
 tools/perf/util/addr_location.c               |    4 +
 .../perf/util/annotate-arch/annotate-alpha.c  |    5 +-
 .../perf/util/annotate-arch/annotate-arm64.c  |   12 +-
 .../util/annotate-arch/annotate-loongarch.c   |   15 +-
 tools/perf/util/annotate-arch/annotate-s390.c |    6 +-
 tools/perf/util/annotate-arch/annotate-x86.c  |   14 +-
 tools/perf/util/annotate-data.c               |   12 +-
 tools/perf/util/annotate.c                    |  211 ++-
 tools/perf/util/annotate.h                    |    6 +-
 tools/perf/util/auxtrace.c                    |  111 +-
 tools/perf/util/block-info.c                  |   12 +-
 tools/perf/util/bpf_lock_contention.c         |   35 +-
 tools/perf/util/c2c-function.c                |   32 +-
 tools/perf/util/call-path.c                   |   15 +-
 tools/perf/util/callchain.c                   |   23 +-
 tools/perf/util/capstone.c                    |   22 +-
 tools/perf/util/data-convert-json.c           |    4 +-
 tools/perf/util/db-export.c                   |   99 +-
 tools/perf/util/db-export.h                   |    8 +
 tools/perf/util/debug.c                       |    3 +-
 tools/perf/util/demangle-java.c               |   23 +-
 tools/perf/util/disasm.c                      |   69 +-
 tools/perf/util/dlfilter.c                    |   12 +-
 tools/perf/util/dso.c                         |  257 ++-
 tools/perf/util/dso.h                         |   94 +-
 tools/perf/util/dsos.c                        |   76 +
 tools/perf/util/dsos.h                        |    3 +
 tools/perf/util/event.c                       |   12 +-
 tools/perf/util/evsel_fprintf.c               |    2 +-
 tools/perf/util/hist.c                        |   70 +-
 tools/perf/util/intel-pt.c                    |   74 +-
 tools/perf/util/libbfd.c                      |   14 +-
 tools/perf/util/libdw.c                       |    5 +-
 tools/perf/util/llvm.c                        |    8 +-
 tools/perf/util/lock-contention.c             |    8 +-
 tools/perf/util/machine.c                     |  189 ++-
 tools/perf/util/machine.h                     |    9 +
 tools/perf/util/map.c                         |   27 +-
 tools/perf/util/map.h                         |   18 +-
 tools/perf/util/map_symbol.c                  |    4 +-
 tools/perf/util/maps.c                        |  284 +++-
 tools/perf/util/mutex.h                       |    3 +
 tools/perf/util/probe-event.c                 |  148 +-
 tools/perf/util/python.c                      |   36 +-
 tools/perf/util/rwsem.c                       |   20 +
 tools/perf/util/rwsem.h                       |    2 +
 tools/perf/util/session.c                     |   78 +
 tools/perf/util/session.h                     |    8 +
 tools/perf/util/sort.c                        |   47 +-
 tools/perf/util/srcline.c                     |   22 +-
 tools/perf/util/symbol-elf.c                  |  311 +++-
 tools/perf/util/symbol.c                      | 1438 +++++++++++++----
 tools/perf/util/symbol.h                      |  187 ++-
 tools/perf/util/symbol_conf.h                 |    1 -
 tools/perf/util/symbol_fprintf.c              |   37 +-
 tools/perf/util/thread-stack.c                |   18 +-
 tools/perf/util/threads.c                     |   17 +-
 tools/perf/util/unwind-libdw.c                |    4 +-
 tools/perf/util/unwind-libunwind.c            |    2 +-
 tools/perf/util/vdso.c                        |    2 +-
 tools/perf/util/vdso.h                        |    2 +-
 103 files changed, 4557 insertions(+), 1389 deletions(-)
 create mode 100644 tools/perf/bench/maps.c
 create mode 100644 tools/perf/bench/symbols.c

--
2.56.0.313.g2479322e68-goog

author reviewer
#50 lore raw

Re: [PATCH v2 00/18] perf symbol: Reference counting, sorted arrays, and memory shrinking

Quoted 27 lines · Ian Rogers
Quoted 16 lines · Alireza Haghdoost

Hi Ian,

I applied v2 on perf-tools-next e74880764bf9 with the ELF_C_READ_MMAP fix and your three fixes, and compared it with the same tree without the 18 patches. I posted comments are on the individual patches; this is the summary.

On a profile of a large C++ service, about 1M symbols, perf script and perf r eport output is byte-identical and roughly 22-32% faster:

Execution time and peak RSS before and after:
perf script 3.3s 334 MiB vs 2.25s 332 MiB
perf report --sort dso,sym 3.0s 389 MiB vs 2.25s 386 MiB
perf report --children -g caller 4.6s 706 MiB vs 3.60s 703 MiB

Quoted 4 lines

Peak RSS barely moves. sizeof(struct symbol) goes from 48 to 32 bytes, but the address array adds a pointer per symbol back, so the net is about 8 bytes per symbol before malloc rounding. That is under 1% here.

Thanks for your work and running the numbers! I think we should aim to land your lazy loading patches ahead of these patches. The merge conflicts don't look like they'll be too terrible, wdyt?

Quoted 5 lines

Since the merge window for Linux v7.4 is currently open, I don't see anything landing before November 1st. So I can do a v3 incorporating your suggestions and rebase it on top of your patches when they land. I'll work on pulling out the fixes (you highlighted some you thought were particularly worthwhile) to mail for v7.4 for now.

Chatting with Namhyung he mentions having the arrays in the DSOs be a struct symbol[] rather than a struct symbol *[], because the number of symbols could be known for a DSO when we load the symbols. Currently this isn't possible because of things like the annotated_symbol that this series turns into a side table. I'm not sure it is possible in general to use an array of symbols, as JIT code may need the symbols array to be realloc-ed and the realloc would potentially invalidate any symbol * that had been given out. Maybe we can treat that as a special case. In this scenario, the symbol's lifetime would tie to the DSO's lifetime, allowing us to use the DSO reference count instead of needing one per symbol. It would still be nice to add functions like session__malloc so that we can reduce the memory footprint of a session when memory allocations fail. I still think this is work to land on top of your series.

Thanks,
Ian

Quoted 303 lines

Thanks,
Ian

Quoted 300 lines

Thanks
Alireza

Quoted 296 lines · Ian Rogers
Quoted 137 lines · Ian Rogers

When profiling large binaries or long-running workloads with many DSOs and threads, symbol and DSO tables frequently dominate perf's memory footprint and startup latency. Currently struct symbol has several structural limitations:

1. Every struct symbol embeds a 24-byte struct rb_node for the dso->symbols address rbtree, plus an optional negative-offset allocation prefix (symbol_conf.priv_size) for TUI browser indices, db-export IDs, or struct annotation state even when only a tiny fraction of symbols are ever annotated or exported.
2. Unlike struct map, struct dso, and struct thread, struct symbol is not reference-counted: once loaded into a struct dso, symbols can never be safely evicted while hist_entry, callchain_cursor_node, or map_symbol references exist, and dso__symbols() has no internal synchronization against concurrent readers/writers.
3. ELF symbol table loading (dso__load_sym_internal()) repeatedly invokes libelf section/program header lookups per symbol, calls elf_sym__name() before filtering out non-function/non-allocated symbols, runs Rust/Java demanglers on plain C symbols without fast-path prefix checks, and resorts dso->symbols with qsort() even when ELF symbol tables are already largely ordered in ascending address runs.

This series modernizes struct symbol to match the rest of tools/perf/util (DECLARE_RC_STRUCT, struct rw_semaphore, sorted pointer arrays) and introduces symbol/DSO memory shrinking and faster ELF symbol loading:

- Patch 1 adds perf bench internals symbols and perf bench internals maps to benchmark ELF symbol loading, bulk symbol insertion/sorting/lookup, incremental symbol appends, ascending map insertion, and incremental map appends.
- Patch 2 adds fast-path prefix checks in dso__demangle_sym() (before calling rust_demangle_demangle(), keeping demangle-rust-v0.c unmodified relative to upstream rustc-demangle) and in java_demangle_sym().
- Patches 3-6 encapsulate struct symbol field access behind accessor functions (annotated with __attribute__((nonnull))) and replace the negative-offset symbol_conf.priv_size allocation prefix with on-demand browser index calculation and per-DSO/per-exporter hashmaps for db-export and struct annotation.
- Patch 7 replaces dso->symbols (rb_root_cached) and dso->symbol_names with a locked struct symbols container backed by sorted pointer arrays (symbols and symbols_by_name) protected by a struct rw_semaphore, removing struct rb_node from struct symbol.
- Patch 8 wraps struct symbol in DECLARE_RC_STRUCT(symbol) with refcount_t (symbol__get() / symbol__put() / symbol__zput()) for sanitizer-checked reference counting across map_symbol, addr_location, hist_entry, callchain, thread_stack, and db-export.
- Patch 9 replaces qsort() in struct symbols with a natural-run merge sort and galloping merge (taking advantage of pre-ordered runs in .symtab/.dynsym and kallsyms) plus an in-place binary insertion fast path for small incremental appends.
- Patch 10 applies the same incremental binary-insertion sort and an O(1) tail-insertion fast path to struct maps for /proc/<pid>/maps synthesis and runtime PERF_RECORD_MMAP2 / JIT map insertions.
- Patch 11 caches ELF program and section headers during dso__load_sym_internal(), defers elf_sym__name() string table lookups until after symbol filtering, and runs symbols__fixup_end() / symbols__fixup_duplicate() once per DSO load.
- Patches 12-17 add down_read_trylock() / down_write_trylock(), protect machines->guests with a rw_semaphore, implement symbols__shrink(), dso__shrink(), dsos__shrink(), and perf_session__shrink() (with transparent on-demand reload in dso__load() that preserves pointer identity for retained symbols), and provide perf_session__malloc() / zalloc() / calloc() / realloc() / strdup() wrappers that automatically shrink unreferenced symbols and DSOs on allocation failure.
- Patch 18 adds unit tests in perf test symbols covering sorted symbol insertion, symbol/DSO shrinking, transparent DSO reload after shrink, and session allocation wrappers.

End-to-end benchmark results across the series:

perf bench internals symbols (perf binary):

    Average DSO load + sort:      7.348 msec ->   5.324 msec (-27.5%)
    Average DSO memory usage:        1401 KB ->      1308 KB ( -6.6%)
    Average bulk insert + lookup: 1453.95 us -> 1161.00 us   (-20.1%)
    Average append + lookup (200 interior inserts into 10,000 symbols):
      rbtree: 42.85 us -> array+qsort: 40429.75 us -> array+binary-insert: 152.70 us

perf bench internals symbols -b libLLVM.so.21.1:

    Average DSO load + sort:     81.196 msec ->  62.582 msec (-22.9%)
    Average DSO memory usage:        9530 KB ->      9164 KB ( -3.8%)

perf bench internals symbols -b libLLVM.so.21.1 --no-demangle:

    Average DSO load + sort:     32.675 msec ->  19.122 msec (-41.5%)
    Average DSO memory usage:        7589 KB ->      7229 KB ( -4.7%)

perf bench internals maps:

    Average ascending insert + lookup:   63.37 us -> 58.97 us ( -6.9%)
    Average append + lookup:           7477.45 us -> 96.67 us (-98.7%, 77.3x faster)

Net performance and memory trade-offs:
- Replacing the per-symbol rbtree (struct rb_node) and negative-offset symbol_conf.priv_size allocation prefix with a contiguous sorted pointer array reduces per-symbol heap overhead by 16 to 24+ bytes (saving 3.8% to 6.6% of total DSO symbol table heap memory unconditionally, and allowing unreferenced symbols and DSOs to be shrunk on demand under memory pressure).
- For the dominant symbol workloads—loading ELF binaries (-22.9% to -41.5%) and bulk symbol insertion, sorting, and lookup (-20.1%)—the contiguous array, natural-run merge sort, ELF header caching, and demangler fast paths yield substantial speedups over the rbtree baseline.
- For synthetic single-symbol interior insertions interleaved with immediate lookups into an already-large 10,000-symbol DSO (dso__insert_symbol()), an rbtree rotation takes ~0.21 us/op (42.85 us for 200 ops) whereas shifting the contiguous pointer array tail with memmove() after binary search takes ~0.76 us/op (152.70 us for 200 ops, down from 40429.75 us with naive qsort()). In real workloads, symbols are either loaded in bulk (dso__load()) or inserted into small per-prog BPF/JIT DSOs where the array fits in a few cache lines, making this sub-microsecond difference negligible in practice.
- Overall, the series reduces memory consumption across all workloads while significantly improving—or remaining neutral on—runtime performance.

v2:
- Rebased onto perf-tools-next (dropping "perf symbols: Fix broken ELF_C_READ_MMAP fallback guard", which was merged upstream, and splitting "perf libdw: Optimize fallback logic for missing DWARF" into a separate fixes series).
- Added 'perf bench internals symbols' and 'perf bench internals maps' benchmarks (Patch 1) and included before/after numbers in the optimization patches.
- Added __attribute__((nonnull)) annotations to struct symbol accessor functions (Patch 3).
- Split the symbol_conf.priv_size removal patch into three separate patches for map_browser (Patch 4), db-export (Patch 5), and annotate (Patch 6).
- Added dedicated optimization patches for Rust/Java demangler prefix checks (Patch 2), natural-run merge sort and binary insertion in struct symbols (Patch 9), tail-insertion and incremental binary-insertion sort in struct maps (Patch 10), and ELF program/section header caching and deferred symbol name lookup (Patch 11).
- Replaced periodic heuristic LRU shrinking and windowed ELF symbol loading with on-demand shrinking (symbols__shrink(), dso__shrink(), dsos__shrink(), perf_session__shrink()) triggered on allocation failure via session-scoped allocation wrappers, plus transparent DSO reload preserving retained symbol pointers (Patches 12-18).

Quoted 4 lines

v1: https://lore.kernel.org/lkml/[email protected]/

Assisted-by Antigravity:gemini-3.1-pro

So Sashiko failed to apply the patches (local Sashiko runs were clean) because I had these four patches applied locally:
- Alireza's "perf symbols: Fix broken ELF_C_READ_MMAP fallback guard"
https://lore.kernel.org/linux-perf-users/[email protected]/
- My fixes in "perf: Fix libdw addr2line fallback, pr_debugN
overhead, and cumulative hist decay"
https://lore.kernel.org/linux-perf-users/[email protected]/

Hopefully the patches above won't take too long to land, so I'll hold off sending v3 until then.

Thanks,
Ian

Quoted 138 lines

Ian Rogers (18):
perf bench internals: Add symbols and maps benchmarks perf demangle: Add fast-path prefix checks for Rust and Java
demanglers
perf symbol: Add accessor functions for struct symbol fields perf map_browser: Compute symbol browser index on demand perf db-export: Store symbol database IDs in a hashmap perf annotate: Move struct annotation to a hashmap and remove
symbol_conf.priv_size
perf symbol: Switch backing storage from rbtree to struct symbols
array
perf symbol: Add reference counting and DECLARE_RC_STRUCT(symbol) perf symbol: Optimize symbol sorting with natural-run merge sort and
binary insertion
perf maps: Optimize tail insertion and small-append sorting perf symbol-elf: Cache ELF program/section headers and defer symbol
name lookup
perf rwsem: Add down_read_trylock() and down_write_trylock() perf vdso: Constify dso parameter in dso__is_vdso()
perf machine: Protect machines->guests rbtree with a rw_semaphore perf symbol: Add memory shrinking for symbols, DSOs, and machines perf session: Add dso__session() and session-scoped allocation
wrappers
perf symbol: Use session allocation wrappers in symbol, DSO, thread,
and hist paths
perf test symbols: Add tests for symbol shrinking and session alloc
wrappers

 tools/perf/Documentation/perf-bench.txt       |    6 +
 .../arch/powerpc/util/skip-callchain-idx.c    |    2 +-
 tools/perf/arch/powerpc/util/sym-handling.c   |   40 +-
 tools/perf/bench/Build                        |    2 +
 tools/perf/bench/bench.h                      |    2 +
 tools/perf/bench/maps.c                       |  186 +++
 tools/perf/bench/symbols.c                    |  239 +++
 tools/perf/builtin-annotate.c                 |   22 +-
 tools/perf/builtin-bench.c                    |    2 +
 tools/perf/builtin-c2c.c                      |   14 -
 tools/perf/builtin-diff.c                     |    4 +-
 tools/perf/builtin-kallsyms.c                 |    9 +-
 tools/perf/builtin-kmem.c                     |   82 +-
 tools/perf/builtin-kwork.c                    |    4 +-
 tools/perf/builtin-lock.c                     |   25 +-
 tools/perf/builtin-mem.c                      |    2 +-
 tools/perf/builtin-report.c                   |   14 -
 tools/perf/builtin-sched.c                    |    6 +-
 tools/perf/builtin-script.c                   |   18 +-
 tools/perf/builtin-timechart.c                |    2 +-
 tools/perf/builtin-top.c                      |   24 +-
 tools/perf/builtin-trace.c                    |   23 +-
 tools/perf/tests/builtin-test.c               |    1 -
 tools/perf/tests/dlfilter-test.c              |   12 +-
 tools/perf/tests/dwarf-unwind.c               |    2 +-
 tools/perf/tests/hists_common.c               |    9 +-
 tools/perf/tests/hists_cumulate.c             |    8 +-
 tools/perf/tests/hists_filter.c               |   10 +-
 tools/perf/tests/hists_link.c                 |   16 +-
 tools/perf/tests/hists_output.c               |    9 +-
 tools/perf/tests/kallsyms-split.c             |    5 +-
 tools/perf/tests/maps.c                       |   58 +
 tools/perf/tests/pe-file-parsing.c            |    1 +
 tools/perf/tests/symbols.c                    |  454 +++++-
 tools/perf/tests/vmlinux-kallsyms.c           |  187 ++-
 tools/perf/ui/browser.c                       |    3 +
 tools/perf/ui/browsers/annotate.c             |   10 +-
 tools/perf/ui/browsers/hists.c                |   35 +-
 tools/perf/ui/browsers/map.c                  |   88 +-
 tools/perf/ui/gtk/annotate.c                  |    6 +-
 tools/perf/ui/stdio/hist.c                    |    6 +-
 tools/perf/util/addr2line.c                   |    8 +-
 tools/perf/util/addr_location.c               |    4 +
 .../perf/util/annotate-arch/annotate-alpha.c  |    5 +-
 .../perf/util/annotate-arch/annotate-arm64.c  |   12 +-
 .../util/annotate-arch/annotate-loongarch.c   |   15 +-
 tools/perf/util/annotate-arch/annotate-s390.c |    6 +-
 tools/perf/util/annotate-arch/annotate-x86.c  |   14 +-
 tools/perf/util/annotate-data.c               |   12 +-
 tools/perf/util/annotate.c                    |  211 ++-
 tools/perf/util/annotate.h                    |    6 +-
 tools/perf/util/auxtrace.c                    |  111 +-
 tools/perf/util/block-info.c                  |   12 +-
 tools/perf/util/bpf_lock_contention.c         |   35 +-
 tools/perf/util/c2c-function.c                |   32 +-
 tools/perf/util/call-path.c                   |   15 +-
 tools/perf/util/callchain.c                   |   23 +-
 tools/perf/util/capstone.c                    |   22 +-
 tools/perf/util/data-convert-json.c           |    4 +-
 tools/perf/util/db-export.c                   |   99 +-
 tools/perf/util/db-export.h                   |    8 +
 tools/perf/util/debug.c                       |    3 +-
 tools/perf/util/demangle-java.c               |   23 +-
 tools/perf/util/disasm.c                      |   69 +-
 tools/perf/util/dlfilter.c                    |   12 +-
 tools/perf/util/dso.c                         |  257 ++-
 tools/perf/util/dso.h                         |   94 +-
 tools/perf/util/dsos.c                        |   76 +
 tools/perf/util/dsos.h                        |    3 +
 tools/perf/util/event.c                       |   12 +-
 tools/perf/util/evsel_fprintf.c               |    2 +-
 tools/perf/util/hist.c                        |   70 +-
 tools/perf/util/intel-pt.c                    |   74 +-
 tools/perf/util/libbfd.c                      |   14 +-
 tools/perf/util/libdw.c                       |    5 +-
 tools/perf/util/llvm.c                        |    8 +-
 tools/perf/util/lock-contention.c             |    8 +-
 tools/perf/util/machine.c                     |  189 ++-
 tools/perf/util/machine.h                     |    9 +
 tools/perf/util/map.c                         |   27 +-
 tools/perf/util/map.h                         |   18 +-
 tools/perf/util/map_symbol.c                  |    4 +-
 tools/perf/util/maps.c                        |  284 +++-
 tools/perf/util/mutex.h                       |    3 +
 tools/perf/util/probe-event.c                 |  148 +-
 tools/perf/util/python.c                      |   36 +-
 tools/perf/util/rwsem.c                       |   20 +
 tools/perf/util/rwsem.h                       |    2 +
 tools/perf/util/session.c                     |   78 +
 tools/perf/util/session.h                     |    8 +
 tools/perf/util/sort.c                        |   47 +-
 tools/perf/util/srcline.c                     |   22 +-
 tools/perf/util/symbol-elf.c                  |  311 +++-
 tools/perf/util/symbol.c                      | 1438 +++++++++++++----
 tools/perf/util/symbol.h                      |  187 ++-
 tools/perf/util/symbol_conf.h                 |    1 -
 tools/perf/util/symbol_fprintf.c              |   37 +-
 tools/perf/util/thread-stack.c                |   18 +-
 tools/perf/util/threads.c                     |   17 +-
 tools/perf/util/unwind-libdw.c                |    4 +-
 tools/perf/util/unwind-libunwind.c            |    2 +-
 tools/perf/util/vdso.c                        |    2 +-
 tools/perf/util/vdso.h                        |    2 +-
 103 files changed, 4557 insertions(+), 1389 deletions(-)
 create mode 100644 tools/perf/bench/maps.c
 create mode 100644 tools/perf/bench/symbols.c

--
2.56.0.313.g2479322e68-goog

#51 lore raw

Re: [PATCH v2 00/18] perf symbol: Reference counting, sorted arrays, and memory shrinking

Quoted 39 lines · On Fri, 30 Oct 2026 10:57:27 -0700 Ian Rogers <[email protected]> wrote:

To: Alireza Haghdoost <[email protected]>
Cc: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
Subject: Re: [PATCH v2 00/18] perf symbol: Reference counting, sorted arrays, and memory shrinking
Date: Fri, 30 Oct 2026 10:57:27 -0700

Quoted 27 lines · Ian Rogers
Quoted 16 lines · Alireza Haghdoost

Hi Ian,

I applied v2 on perf-tools-next e74880764bf9 with the ELF_C_READ_MMAP fix and your three fixes, and compared it with the same tree without the 18 patches. I posted comments are on the individual patches; this is the summary.

On a profile of a large C++ service, about 1M symbols, perf script and perf r eport output is byte-identical and roughly 22-32% faster:

Execution time and peak RSS before and after:
perf script 3.3s 334 MiB vs 2.25s 332 MiB
perf report --sort dso,sym 3.0s 389 MiB vs 2.25s 386 MiB
perf report --children -g caller 4.6s 706 MiB vs 3.60s 703 MiB

Quoted 4 lines

Peak RSS barely moves. sizeof(struct symbol) goes from 48 to 32 bytes, but the address array adds a pointer per symbol back, so the net is about 8 bytes per symbol before malloc rounding. That is under 1% here.

Thanks for your work and running the numbers! I think we should aim to land your lazy loading patches ahead of these patches. The merge conflicts don't look like they'll be too terrible, wdyt?

Quoted 5 lines

Since the merge window for Linux v7.4 is currently open, I don't see anything landing before November 1st. So I can do a v3 incorporating your suggestions and rebase it on top of your patches when they land. I'll work on pulling out the fixes (you highlighted some you thought were particularly worthwhile) to mail for v7.4 for now.

Quoted 14 lines

Chatting with Namhyung he mentions having the arrays in the DSOs be a struct symbol[] rather than a struct symbol *[], because the number of symbols could be known for a DSO when we load the symbols. Currently this isn't possible because of things like the annotated_symbol that this series turns into a side table. I'm not sure it is possible in general to use an array of symbols, as JIT code may need the symbols array to be realloc-ed and the realloc would potentially invalidate any symbol * that had been given out. Maybe we can treat that as a special case. In this scenario, the symbol's lifetime would tie to the DSO's lifetime, allowing us to use the DSO reference count instead of needing one per symbol. It would still be nice to add functions like session__malloc so that we can reduce the memory footprint of a session when memory allocations fail. I still think this is work to land on top of your series.

Could you have two tables, one 'struct symbol []' containing the fixed symbols and a second smaller 'struct symbol *[]' containing any extra symbols?
Lookup would require checking both tables - but won't cost much more.

David

Quoted 3 lines

Thanks,
Ian

author reviewer
#52 lore raw

v3 cover perf symbol: Sorted arrays, DSO-owned lifetimes, and memory shrinking

In reply to Ian Rogers

When profiling large binaries or long-running workloads with many DSOs and threads, symbol and DSO tables frequently dominate perf's memory footprint and startup latency. Currently struct symbol has several structural limitations:

1. Every struct symbol embeds a 24-byte struct rb_node for the dso->symbols address rbtree, plus an optional negative-offset allocation prefix (symbol_conf.priv_size) for TUI browser indices, db-export IDs, or struct annotation state even when only a tiny fraction of symbols are ever annotated or exported.
2. dso__symbols() has no internal synchronization against concurrent readers/writers, and while most symbol holders (map_symbol, addr_location) already pin the owning struct dso via map__get(), a handful of long-lived symbol holders (hist_entry->parent, call_path, and db-export) retain raw struct symbol * pointers without retaining a reference on the owning struct dso, preventing unreferenced DSOs and their symbol tables from being safely reclaimed under memory pressure.
3. ELF symbol table loading (dso__load_sym_internal()) repeatedly invokes libelf section/program header lookups per symbol, calls elf_sym__name() before filtering out non-function/non-allocated symbols, runs Rust/Java demanglers on plain C symbols without fast-path prefix checks, and resorts dso->symbols with qsort() even when ELF symbol tables are already largely ordered in ascending address runs.

This series modernizes struct symbol storage, ties symbol lifetimes explicitly to their owning struct dso (rather than adding per-symbol reference counting), removes symbol_conf.priv_size, speeds up ELF symbol and map loading/sorting, and adds on-demand session memory shrinking:

- **Fixes and preparatory cleanups (Patches 1-4)**:
- Patch 1 fixes a heap buffer corruption in the TUI map browser when symbol_conf.priv_size was not initialized with sizeof(u32) by computing symbol browser indices on demand.
- Patch 2 protects the machines->guests rbtree with a rw_semaphore.
- Patch 3 constifies dso in dso__is_vdso().
- Patch 4 adds down_read_trylock() and down_write_trylock() to

    `util/rwsem.[ch]`.

- **DSO-owned symbol lifetimes and priv_size removal (Patches 5-8)**:
- Patches 5-6 introduce struct dso_symbol ({ struct dso *dso; struct symbol *sym; }) so callers that borrow a symbol's lifetime directly from its owning struct dso without a struct map (hist_entry parent symbols, call_path nodes, and db-export) retain a dso reference for the lifetime of the symbol pointer. (Consolidating map_symbol and dso_symbol back into symbol.h as explicit DSO/map-borrowed symbol handle types is left for follow-up cleanup.)
- Patches 7-8 move db-export symbol IDs and struct annotation into on-demand hashmaps and remove symbol_conf.priv_size and symbol__priv().
- **Benchmarks and loading/sorting optimizations (Patches 9-13)**:
- Patches 9-10 add standalone perf bench internals symbols and perf bench internals maps subcommands.
- Patch 11 adds fast-path prefix checks for the Rust and Java demanglers.
- Patch 12 caches ELF program and section headers in dso__load_sym_internal() and defers elf_sym__name() lookups until after non-symbol/non-allocated entries are filtered out.
- Patch 13 adds an O(1) tail-insertion fast path and incremental binary insertion sort to struct maps.
- **Sorted symbol arrays and natural-run merge sort (Patches 14-16)**:
- Patch 14 encapsulates struct symbol field accesses behind inline accessor functions in preparation for lazy symbol/string loading.
- Patch 15 replaces dso->symbols (rb_root_cached) and dso->symbol_names with a rw_semaphore-protected struct symbols container backed by sorted pointer arrays, removing struct rb_node from struct symbol (shrinking sizeof(struct symbol) from 48 to 24 bytes, or 16 bytes net per symbol after accounting for the array pointer).
- Patch 16 replaces qsort() on struct symbols with a stable natural-run merge sort, galloping merge, and a sorted tail buffer with amortized sqrt(N) merge for out-of-order insertions.
- **On-demand memory shrinking and tests (Patches 17-20)**:
- Patch 17 adds symbols__shrink(), dso__shrink(), dsos__shrink(),

    `machine__shrink()`, `machines__shrink()`, and `perf_session__shrink()`
    to reclaim `symbols_by_name`, excess array capacity, cached DSO file
    descriptors/data/DWARF state, and unreferenced user DSOs (`refcnt == 1`
    with no annotated symbols), with transparent on-demand reload via
    `dso__load()`.

- Patches 18-19 add dso__session() and perf_session__malloc() / zalloc() / calloc() / realloc() / strdup() wrappers that trigger perf_session__shrink() and retry once on allocation failure.
- Patch 20 adds unit tests in perf test symbols covering sorted symbol insertion, symbol/DSO shrinking, dso_symbol lifetime pinning, inline frame/srcline cleanup ordering, symbols_by_name on-demand rebuild, transparent DSO reload, and session allocation wrappers.

Changes in v3:
- Drop per-symbol DECLARE_RC_STRUCT(symbol) reference counting and partial symbol-table retention (symbols__merge_retained()) in favor of DSO-owned symbol lifetimes via struct dso_symbol (Patches 5-6), evicting a DSO's symbol table only as a whole when dso has refcnt == 1 and no annotated symbols (Namhyung Kim, Alireza Haghdoost).
- Reorder the series so bug fixes and small preparatory refactorings come first ahead of the larger struct symbol accessor and container patches.
- Split the perf bench internals patch into two smaller standalone patches (symbols and maps), separate dso__load() timing and Max RSS from dso__sort_by_name() so address-only/lazy symbol loading is measured independently of symbol name sorting, replace mallinfo2() (and its build feature test) with getrusage() ru_maxrss, skip stripped binaries (nr == 0) cleanly, call symbol__exit(), and include example output in the commit messages (Namhyung Kim, Alireza Haghdoost).
- Split out and land upstream the independent fixes uncovered during v2
review: c4baf52c7901 ("perf callchain: Fix loss of symbol in merge_chain_branch()"), 854dd5e7d099 ("perf annotate: Fix block_range symbol loss on split and leaks on exit"), and baef30dd8308 ("perf
symbol-elf: Fix zero-size symbol end calculation across .symtab and .dynsym") (Alireza Haghdoost).
- Remove the hash_bits(..., 16) 64K-bucket cap in annotate and db-export hashmaps, set symbol__set_annotated() on the existing-entry write path, and share the empty-hashmap cleanup path on zalloc() failure in symbol__annotation() (Alireza Haghdoost, Sashiko).
- Preserve insertion order on equal-start symbols (symbol__start(a) <= symbol__start(b)), use stable insertion sort instead of qsort() as the allocation-failure fallback, keep kallsyms "last alias wins" behavior in symbols__fixup_end(), and add a sorted tail buffer with amortized sqrt(N) merge in __symbols__insert_sorted() to avoid O(n^2) memmove() on random insertions (Alireza Haghdoost).
- Retry dso_cache_io() under dso__lock(dso) if dso__shrink() evicted a newly populated cache entry, free dso->inlined_nodes and dso->srclines before symbols__delete_locked() in dso__shrink(), and rebuild symbols_by_name on demand via symbols__sort_by_name_read_lock() in dso__next_symbol_by_name(), dso__find_symbol_by_name(), and symbols__visit_by_name() (Alireza Haghdoost, Sashiko).
- Expand perf test symbols (Patch 20) to return TEST_SKIP on unshrinkable DSOs, verify full symbol count and sorted order after reload, and test dso_symbol pinning, inline/srcline eviction ordering, and dso__next_symbol_by_name() index rebuild after dso__shrink() (Alireza Haghdoost, Sashiko).
- Rebase onto perf-tools-next (cc905a18a647).

v2: https://lore.kernel.org/linux-perf-users/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/

AI Assistance Disclosure (per Documentation/process/coding-assistants.rst and Documentation/process/generated-content.rst):
- Tools used: Antigravity coding assistant and Sashiko automated patch review.
- Summary of prompts and assistance: Interactive pair-programming session to replace per-symbol refcounting with struct dso_symbol DSO-borrowed lifetimes, reorder fixes and preparatory patches ahead of the struct symbol array conversion, split and refine the symbols and maps benchmarks, and address LKML reviewer and Sashiko feedback across annotate, dso, symbol, and tests/symbols.c.
- Testing performed: Built with Clang (-Wthread-safety), AddressSanitizer, LeakSanitizer, and DEBUG=1 (REFCNT_CHECKING=1); ran perf test (including Symbols, Maps, and hists suites), perf bench internals symbols and maps, perf report -s parent,symbol, export-to-sqlite.py, and scripts/checkpatch.pl.

Ian Rogers (20):
perf map_browser: Fix browser index heap corruption by computing on
demand
perf machine: Protect machines->guests rbtree with a rw_semaphore perf vdso: Constify dso parameter in dso__is_vdso()
perf rwsem: Add down_read_trylock() and down_write_trylock() perf map_symbol: Add struct dso_symbol and use for parent symbols perf call-path: Use struct dso_symbol to retain symbol DSO lifetimes perf db-export: Store symbol database IDs in a hashmap perf annotate: Move struct annotation to a hashmap and remove
symbol_conf.priv_size
perf bench internals: Add symbols benchmark
perf bench internals: Add maps benchmark
perf demangle: Add fast-path prefix checks for Rust and Java
demanglers
perf symbol-elf: Cache ELF program/section headers and defer symbol
name lookup
perf maps: Optimize tail insertion and small-append sorting perf symbol: Add accessor functions for struct symbol fields perf symbol: Switch backing storage from rbtree to struct symbols
array
perf symbol: Optimize symbol sorting with natural-run merge sort and
binary insertion
perf symbol: Add memory shrinking for symbols, DSOs, and machines perf session: Add dso__session() and session-scoped allocation
wrappers
perf symbol: Use session allocation wrappers in symbol, DSO, thread,
and hist paths
perf test symbols: Add tests for symbol shrinking and session alloc
wrappers

 tools/perf/Documentation/perf-bench.txt       |    6 +
 .../arch/powerpc/util/skip-callchain-idx.c    |    2 +-
 tools/perf/arch/powerpc/util/sym-handling.c   |   40 +-
 tools/perf/bench/Build                        |    2 +
 tools/perf/bench/bench.h                      |    2 +
 tools/perf/bench/maps.c                       |  202 +++
 tools/perf/bench/symbols.c                    |  273 ++++
 tools/perf/builtin-annotate.c                 |   18 +-
 tools/perf/builtin-bench.c                    |    2 +
 tools/perf/builtin-c2c.c                      |   14 -
 tools/perf/builtin-diff.c                     |    4 +-
 tools/perf/builtin-kallsyms.c                 |    7 +-
 tools/perf/builtin-kmem.c                     |   70 +-
 tools/perf/builtin-kwork.c                    |    4 +-
 tools/perf/builtin-lock.c                     |   10 +-
 tools/perf/builtin-mem.c                      |    2 +-
 tools/perf/builtin-report.c                   |   14 -
 tools/perf/builtin-sched.c                    |    6 +-
 tools/perf/builtin-script.c                   |   14 +-
 tools/perf/builtin-timechart.c                |    2 +-
 tools/perf/builtin-top.c                      |   22 +-
 tools/perf/builtin-trace.c                    |   22 +-
 tools/perf/tests/builtin-test.c               |    1 -
 tools/perf/tests/dlfilter-test.c              |    4 +-
 tools/perf/tests/dwarf-unwind.c               |    2 +-
 tools/perf/tests/hists_common.c               |    9 +-
 tools/perf/tests/hists_cumulate.c             |    4 +-
 tools/perf/tests/hists_output.c               |    2 +-
 tools/perf/tests/maps.c                       |   67 +
 tools/perf/tests/symbols.c                    |  611 ++++++++-
 tools/perf/tests/vmlinux-kallsyms.c           |  181 +--
 tools/perf/ui/browser.c                       |    3 +
 tools/perf/ui/browsers/annotate.c             |   10 +-
 tools/perf/ui/browsers/hists.c                |   26 +-
 tools/perf/ui/browsers/map.c                  |   85 +-
 tools/perf/ui/gtk/annotate.c                  |    6 +-
 tools/perf/ui/stdio/hist.c                    |   12 +-
 .../perf/util/annotate-arch/annotate-arm64.c  |   12 +-
 .../util/annotate-arch/annotate-loongarch.c   |    4 +-
 tools/perf/util/annotate-arch/annotate-x86.c  |   14 +-
 tools/perf/util/annotate-data.c               |   12 +-
 tools/perf/util/annotate.c                    |  205 ++-
 tools/perf/util/annotate.h                    |    6 +-
 tools/perf/util/auxtrace.c                    |  111 +-
 tools/perf/util/block-info.c                  |    8 +-
 tools/perf/util/block-range.c                 |    3 +-
 tools/perf/util/bpf_lock_contention.c         |   16 +-
 tools/perf/util/c2c-function.c                |   19 +-
 tools/perf/util/call-path.c                   |   42 +-
 tools/perf/util/call-path.h                   |   10 +-
 tools/perf/util/callchain.c                   |   22 +-
 tools/perf/util/callchain.h                   |    3 +-
 tools/perf/util/capstone.c                    |   20 +-
 tools/perf/util/data-convert-json.c           |    4 +-
 tools/perf/util/db-export.c                   |  144 +-
 tools/perf/util/db-export.h                   |   16 +
 tools/perf/util/debug.c                       |    3 +-
 tools/perf/util/demangle-java.c               |   23 +-
 tools/perf/util/disasm.c                      |   49 +-
 tools/perf/util/dlfilter.c                    |   12 +-
 tools/perf/util/dso.c                         |  263 +++-
 tools/perf/util/dso.h                         |   83 +-
 tools/perf/util/dsos.c                        |   77 ++
 tools/perf/util/dsos.h                        |    3 +
 tools/perf/util/event.c                       |    4 +-
 tools/perf/util/evsel_fprintf.c               |    2 +-
 tools/perf/util/hist.c                        |   86 +-
 tools/perf/util/hist.h                        |    8 +-
 tools/perf/util/intel-pt.c                    |   74 +-
 tools/perf/util/libbfd.c                      |    7 +-
 tools/perf/util/llvm.c                        |    6 +-
 tools/perf/util/lock-contention.c             |    2 +-
 tools/perf/util/machine.c                     |  171 ++-
 tools/perf/util/machine.h                     |   14 +-
 tools/perf/util/map.c                         |   27 +-
 tools/perf/util/map.h                         |   10 +-
 tools/perf/util/map_symbol.c                  |   30 +
 tools/perf/util/map_symbol.h                  |   11 +
 tools/perf/util/maps.c                        |  281 +++-
 tools/perf/util/mutex.h                       |    3 +
 tools/perf/util/probe-event.c                 |  133 +-
 tools/perf/util/python.c                      |   65 +-
 tools/perf/util/rwsem.c                       |   20 +
 tools/perf/util/rwsem.h                       |    2 +
 tools/perf/util/session.c                     |   78 ++
 tools/perf/util/session.h                     |   14 +-
 tools/perf/util/sort.c                        |   49 +-
 tools/perf/util/srcline.c                     |   16 +-
 tools/perf/util/symbol-elf.c                  |  269 +++-
 tools/perf/util/symbol.c                      | 1185 +++++++++++++----
 tools/perf/util/symbol.h                      |  146 +-
 tools/perf/util/symbol_conf.h                 |    1 -
 tools/perf/util/symbol_fprintf.c              |   37 +-
 tools/perf/util/thread-stack.c                |   48 +-
 tools/perf/util/unwind-libdw.c                |    2 +-
 tools/perf/util/vdso.c                        |    2 +-
 tools/perf/util/vdso.h                        |    2 +-
 97 files changed, 4430 insertions(+), 1320 deletions(-)
 create mode 100644 tools/perf/bench/maps.c
 create mode 100644 tools/perf/bench/symbols.c
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
author reviewer
#53 lore raw

PATCH v3 1/20 perf map_browser: Fix browser index heap corruption by computing on demand

Commit 259dce914e93 ("perf symbol: Remove symbol_name_rb_node") removed the 24-byte symbol_name_rb_node reservation from symbol_conf.priv_size, but left symbol__browser_index() in ui/browsers/map.c subtracting sizeof(struct rb_node) + sizeof(u32) (28 bytes on 64-bit) ahead of struct symbol. In 'perf report -v' and 'perf c2c report -v', symbol_conf.priv_size is sizeof(struct annotation) + sizeof(u32) (20 bytes on 64-bit), so opening the TUI map browser writes a u32 index 8 bytes before each symbol's calloc() buffer, corrupting heap metadata.

Remove symbol__browser_index() in ui/browsers/map.c and compute the TUI map browser index on demand in map_browser__search() by walking the map browser's symbol entries when the user searches with '/'.

While this changes finding a symbol's display row index in map_browser__search() from O(1) to O(N) in the number of symbols in that single map, map_browser__search() is only invoked interactively when a user opens the TUI "Browse map details" view for a map, presses '/', types a query, and hits ENTER. Even for a large DSO such as vmlinux (~100k symbols), scanning the map's symbols once per interactive search takes well under a millisecond.

Previously, '/' search in the TUI map browser was gated behind 'verbose > 0' (-v) in builtin-c2c.c, builtin-report.c, and ui/browsers/map.c solely to avoid adding sizeof(u32) to symbol_conf.priv_size for every struct symbol across all DSOs in normal runs. With on-demand index computation there is no per-symbol memory overhead, so remove the verbose > 0 restriction and allow '/' search in the map browser unconditionally.

Fixes 259dce914e93 ("perf symbol: Remove symbol_name_rb_node")
---
 tools/perf/builtin-c2c.c     | 14 --------------
 tools/perf/builtin-report.c  | 14 --------------
 tools/perf/ui/browsers/map.c | 37 ++++++++++++++++++------------------
 3 files changed, 18 insertions(+), 47 deletions(-)
tools/perf/builtin-c2c.c +0 -14
index f51fa23f3bf1..719f47dac2dc 100644
@@ -3412,20 +3412,6 @@ static int perf_c2c__report(int argc, const char **argv)
 
 		if (ret < 0)
 			goto out_mem2node;
-		/*
-		 * For searching by name on the "Browse map details".
-		 * providing it only in verbose mode not to bloat too
-		 * much struct symbol.
-		 */
-		if (verbose > 0) {
-			/*
-			 * XXX: Need to provide a less kludgy way to ask for
-			 * more space per symbol, the u32 is for the index on
-			 * the ui browser.
-			 * See symbol__browser_index.
-			 */
-			symbol_conf.priv_size += sizeof(u32);
-		}
 		annotation_config__init();
 	}
 
tools/perf/builtin-report.c +0 -14
index 29085fdc91e5..6672a4d16529 100644
@@ -1885,20 +1885,6 @@ int cmd_report(int argc, const char **argv)
 		ret = symbol__annotation_init();
 		if (ret < 0)
 			goto error;
-		/*
- 		 * For searching by name on the "Browse map details".
- 		 * providing it only in verbose mode not to bloat too
- 		 * much struct symbol.
- 		 */
-		if (verbose > 0) {
-			/*
-			 * XXX: Need to provide a less kludgy way to ask for
-			 * more space per symbol, the u32 is for the index on
-			 * the ui browser.
-			 * See symbol__browser_index.
-			 */
-			symbol_conf.priv_size += sizeof(u32);
-		}
 		annotation_config__init();
 	}
 
tools/perf/ui/browsers/map.c +18 -19
index 075a575cdc5d..be7390156edd 100644
@@ -39,12 +39,6 @@ static void map_browser__write(struct ui_browser *browser, void *nd, int row)
 		ui_browser__write_nstring(browser, sym->name, width);
 }
 
-/* FIXME uber-kludgy, see comment on cmd_report... */
-static u32 *symbol__browser_index(struct symbol *browser)
-{
-	return ((void *)browser) - sizeof(struct rb_node) - sizeof(u32);
-}
-
 static int map_browser__search(struct map_browser *browser)
 {
 	char target[512];
@@ -62,10 +56,21 @@ static int map_browser__search(struct map_browser *browser)
 		sym = map__find_symbol_by_name(browser->map, target);
 
 	if (sym != NULL) {
-		u32 *idx = symbol__browser_index(sym);
-
-		browser->b.top = &sym->rb_node;
-		browser->b.index = browser->b.top_idx = *idx;
+		struct rb_node *nd;
+		u32 idx = 0;
+
+		/*
+		 * Walk the map browser's symbol entries to find the matching
+		 * symbol node and its display row index, then position the
+		 * browser cursor at that entry.
+		 */
+		for (nd = rb_first(browser->b.entries); nd; nd = rb_next(nd), ++idx) {
+			if (&sym->rb_node == nd) {
+				browser->b.top = nd;
+				browser->b.index = browser->b.top_idx = idx;
+				break;
+			}
+		}
 	} else
 		ui_helpline__fpush("%s not found!", target);
 
@@ -77,8 +82,7 @@ static int map_browser__run(struct map_browser *browser)
 	int key;
 
 	if (ui_browser__show(&browser->b, dso__long_name(map__dso(browser->map)),
-			     "Press ESC to exit, %s / to search",
-			     verbose > 0 ? "" : "restart with -v to use") < 0)
+			     "Press ESC to exit, / to search") < 0)
 		return -1;
 
 	while (1) {
@@ -86,9 +90,8 @@ static int map_browser__run(struct map_browser *browser)
 
 		switch (key) {
 		case '/':
-			if (verbose > 0)
-				map_browser__search(browser);
-			/* fall thru */
+			map_browser__search(browser);
+			continue;
 		default:
 			ui_browser__warn_unhandled_hotkey(&browser->b, key, 0, NULL);
 			continue;
@@ -124,10 +127,6 @@ int map__browse(struct map *map)
 
 		if (maxaddr < pos->end)
 			maxaddr = pos->end;
-		if (verbose > 0) {
-			u32 *idx = symbol__browser_index(pos);
-			*idx = mb.b.nr_entries;
-		}
 		++mb.b.nr_entries;
 	}
 
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#54 lore raw

Re: [PATCH v3 01/20] perf map_browser: Fix browser index heap corruption by computing on demand

Sashiko has reviewed this patch and found no issues.
Quoted 13 lines

Commit 259dce914e93 ("perf symbol: Remove symbol_name_rb_node") removed the 24-byte symbol_name_rb_node reservation from symbol_conf.priv_size, but left symbol__browser_index() in ui/browsers/map.c subtracting sizeof(struct rb_node) + sizeof(u32) (28 bytes on 64-bit) ahead of struct symbol. In 'perf report -v' and 'perf c2c report -v', symbol_conf.priv_size is sizeof(struct annotation) + sizeof(u32) (20 bytes on 64-bit), so opening the TUI map browser writes a u32 index 8 bytes before each symbol's calloc() buffer, corrupting heap metadata.

Remove symbol__browser_index() in ui/browsers/map.c and compute the TUI map browser index on demand in map_browser__search() by walking the map browser's symbol entries when the user searches with '/'.

Quoted 8 lines

While this changes finding a symbol's display row index in map_browser__search() from O(1) to O(N) in the number of symbols in that [ ... ]
overhead, so remove the verbose > 0 restriction and allow '/' search in the map browser unconditionally.

Fixes 259dce914e93 ("perf symbol: Remove symbol_name_rb_node")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#55 lore raw

PATCH v3 2/20 perf machine: Protect machines->guests rbtree with a rw_semaphore

In reply to Ian Rogers

Unlike struct dsos, struct maps, and struct threads, struct machines had no lock protecting its machines->guests rbtree. Synchronizing access to machines->guests is needed both for concurrent guest machine lookups and so machines__shrink() can safely iterate guest machines using down_read_trylock(&machines->lock).

Add a rw_semaphore lock to struct machines to synchronize access to the machines->guests rbtree across machines__add(), machines__find(), machines__set_comm_exec(), machines__process_guests(), machines__set_id_hdr_size(), machines__fprintf_dsos(), machines__fprintf_dsos_buildid(), machines__destroy_kernel_maps(), and machines__for_each_thread(), and check for an existing guest machine PID under write lock in machines__add().

---
 tools/perf/util/machine.c | 42 +++++++++++++++++++++++++++++++++------
 tools/perf/util/machine.h |  1 +
 2 files changed, 37 insertions(+), 6 deletions(-)
tools/perf/util/machine.c +36 -6
index 5eb5efa4eb90..f6975d9c4568 100644
@@ -237,6 +237,7 @@ int machines__init(struct machines *machines, struct perf_session *session)
 {
 	int err = machine__init(&machines->host, "", HOST_KERNEL_ID);
 
+	init_rwsem(&machines->lock);
 	machines->session = session;
 	machines->host.machines = machines;
 	machines->guests = RB_ROOT_CACHED;
@@ -247,12 +248,13 @@ void machines__exit(struct machines *machines)
 {
 	machine__exit(&machines->host);
 	/* XXX exit guest */
+	exit_rwsem(&machines->lock);
 }
 
 struct machine *machines__add(struct machines *machines, pid_t pid,
 			      const char *root_dir)
 {
-	struct rb_node **p = &machines->guests.rb_root.rb_node;
+	struct rb_node **p;
 	struct rb_node *parent = NULL;
 	struct machine *pos, *machine = malloc(sizeof(*machine));
 	bool leftmost = true;
@@ -265,9 +267,17 @@ struct machine *machines__add(struct machines *machines, pid_t pid,
 		return NULL;
 	}
 
+	down_write(&machines->lock);
+	p = &machines->guests.rb_root.rb_node;
 	while (*p != NULL) {
 		parent = *p;
 		pos = rb_entry(parent, struct machine, rb_node);
+		if (pid == pos->pid) {
+			up_write(&machines->lock);
+			machine__exit(machine);
+			free(machine);
+			return pos;
+		}
 		if (pid < pos->pid)
 			p = &(*p)->rb_left;
 		else {
@@ -278,8 +288,8 @@ struct machine *machines__add(struct machines *machines, pid_t pid,
 
 	rb_link_node(&machine->rb_node, parent, p);
 	rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
-
 	machine->machines = machines;
+	up_write(&machines->lock);
 
 	return machine;
 }
@@ -290,16 +300,18 @@ void machines__set_comm_exec(struct machines *machines, bool comm_exec)
 
 	machines->host.comm_exec = comm_exec;
 
+	down_read(&machines->lock);
 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
 		struct machine *machine = rb_entry(nd, struct machine, rb_node);
 
 		machine->comm_exec = comm_exec;
 	}
+	up_read(&machines->lock);
 }
 
 struct machine *machines__find(struct machines *machines, pid_t pid)
 {
-	struct rb_node **p = &machines->guests.rb_root.rb_node;
+	struct rb_node **p;
 	struct rb_node *parent = NULL;
 	struct machine *machine;
 	struct machine *default_machine = NULL;
@@ -307,6 +319,8 @@ struct machine *machines__find(struct machines *machines, pid_t pid)
 	if (pid == HOST_KERNEL_ID)
 		return &machines->host;
 
+	down_read(&machines->lock);
+	p = &machines->guests.rb_root.rb_node;
 	while (*p != NULL) {
 		parent = *p;
 		machine = rb_entry(parent, struct machine, rb_node);
@@ -314,11 +328,14 @@ struct machine *machines__find(struct machines *machines, pid_t pid)
 			p = &(*p)->rb_left;
 		else if (pid > machine->pid)
 			p = &(*p)->rb_right;
-		else
+		else {
+			up_read(&machines->lock);
 			return machine;
+		}
 		if (!machine->pid)
 			default_machine = machine;
 	}
+	up_read(&machines->lock);
 
 	return default_machine;
 }
@@ -464,10 +481,12 @@ void machines__process_guests(struct machines *machines,
 {
 	struct rb_node *nd;
 
+	down_read(&machines->lock);
 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
 		struct machine *pos = rb_entry(nd, struct machine, rb_node);
 		process(pos, data);
 	}
+	up_read(&machines->lock);
 }
 
 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
@@ -477,11 +496,13 @@ void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
 
 	machines->host.id_hdr_size = id_hdr_size;
 
+	down_read(&machines->lock);
 	for (node = rb_first_cached(&machines->guests); node;
 	     node = rb_next(node)) {
 		machine = rb_entry(node, struct machine, rb_node);
 		machine->id_hdr_size = id_hdr_size;
 	}
+	up_read(&machines->lock);
 
 	return;
 }
@@ -933,10 +954,12 @@ size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
 	struct rb_node *nd;
 	size_t ret = dsos__fprintf(&machines->host.dsos, fp);
 
+	down_read(&machines->lock);
 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
 		struct machine *pos = rb_entry(nd, struct machine, rb_node);
 		ret += dsos__fprintf(&pos->dsos, fp);
 	}
+	up_read(&machines->lock);
 
 	return ret;
 }
@@ -953,10 +976,12 @@ size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
 	struct rb_node *nd;
 	size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
 
+	down_read(&machines->lock);
 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
 		struct machine *pos = rb_entry(nd, struct machine, rb_node);
 		ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
 	}
+	up_read(&machines->lock);
 	return ret;
 }
 
@@ -1295,10 +1320,12 @@ int machines__create_guest_kernel_maps(struct machines *machines)
 
 void machines__destroy_kernel_maps(struct machines *machines)
 {
-	struct rb_node *next = rb_first_cached(&machines->guests);
+	struct rb_node *next;
 
 	machine__destroy_kernel_maps(&machines->host);
 
+	down_write(&machines->lock);
+	next = rb_first_cached(&machines->guests);
 	while (next) {
 		struct machine *pos = rb_entry(next, struct machine, rb_node);
 
@@ -1306,6 +1333,7 @@ void machines__destroy_kernel_maps(struct machines *machines)
 		rb_erase_cached(&pos->rb_node, &machines->guests);
 		machine__delete(pos);
 	}
+	up_write(&machines->lock);
 }
 
 int machines__create_kernel_maps(struct machines *machines, pid_t pid)
@@ -3128,13 +3156,15 @@ int machines__for_each_thread(struct machines *machines,
 	if (rc != 0)
 		return rc;
 
+	down_read(&machines->lock);
 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
 		struct machine *machine = rb_entry(nd, struct machine, rb_node);
 
 		rc = machine__for_each_thread(machine, fn, priv);
 		if (rc != 0)
-			return rc;
+			break;
 	}
+	up_read(&machines->lock);
 	return rc;
 }
 
tools/perf/util/machine.h +1 -0
index 973a59f7c26a..147ce343e370 100644
@@ -151,6 +151,7 @@ typedef void (*machine__process_t)(struct machine *machine, void *data);
 struct machines {
 	/** @session: back link to owning session if there is one. */
 	struct perf_session *session;
+	struct rw_semaphore lock;
 	struct machine host;
 	struct rb_root_cached guests;
 };
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#56 lore raw

Re: [PATCH v3 02/20] perf machine: Protect machines->guests rbtree with a rw_semaphore

Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

Unlike struct dsos, struct maps, and struct threads, struct machines had no lock protecting its machines->guests rbtree. Synchronizing access to machines->guests is needed both for concurrent guest machine lookups and so machines__shrink() can safely iterate guest machines using down_read_trylock(&machines->lock).

Add a rw_semaphore lock to struct machines to synchronize access to the machines->guests rbtree across machines__add(), machines__find(), machines__set_comm_exec(), machines__process_guests(), machines__set_id_hdr_size(), machines__fprintf_dsos(), machines__fprintf_dsos_buildid(), machines__destroy_kernel_maps(), and machines__for_each_thread(), and check for an existing guest machine PID under write lock in machines__add().

Quoted 1 line

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author reviewer
#57 lore raw

PATCH v3 3/20 perf vdso: Constify dso parameter in dso__is_vdso()

In reply to Ian Rogers

dso__can_shrink_symbols(const struct dso *dso) needs to query dso__is_vdso(dso) on a const struct dso pointer to exclude vDSO images from symbol shrinking.

Change the parameter of dso__is_vdso() from 'struct dso *' to 'const struct dso *'.

---
 tools/perf/util/vdso.c | 2 +-
 tools/perf/util/vdso.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
tools/perf/util/vdso.c +1 -1
index c12f5d8c4bf6..7f2dde70f3e4 100644
@@ -361,7 +361,7 @@ struct dso *machine__findnew_vdso(struct machine *machine,
 	return __machine__addnew_vdso(machine, DSO__NAME_VDSO, file);
 }
 
-bool dso__is_vdso(struct dso *dso)
+bool dso__is_vdso(const struct dso *dso)
 {
 	return !strcmp(dso__short_name(dso), DSO__NAME_VDSO) ||
 	       !strcmp(dso__short_name(dso), DSO__NAME_VDSO32) ||
tools/perf/util/vdso.h +1 -1
index bc74ace603ae..b6e05bc2d9f3 100644
@@ -19,7 +19,7 @@ static inline bool is_vdso_map(const char *filename)
 
 struct dso;
 
-bool dso__is_vdso(struct dso *dso);
+bool dso__is_vdso(const struct dso *dso);
 
 struct machine;
 struct thread;
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#58 lore raw

Re: [PATCH v3 03/20] perf vdso: Constify dso parameter in dso__is_vdso()

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

dso__can_shrink_symbols(const struct dso *dso) needs to query dso__is_vdso(dso) on a const struct dso pointer to exclude vDSO images from symbol shrinking.

Change the parameter of dso__is_vdso() from 'struct dso *' to 'const struct dso *'.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author reviewer
#59 lore raw

PATCH v3 4/20 perf rwsem: Add down_read_trylock() and down_write_trylock()

In reply to Ian Rogers

On-demand memory shrinking (perf_session__shrink(), dsos__shrink(), symbols__shrink()) can be triggered from OOM recovery inside allocation wrappers while the current thread already holds a rw_semaphore (such as symbols->lock, dsos->lock, or machines->lock). Non-blocking trylock primitives are needed so shrinking paths can safely skip any locked container without deadlocking.

Add non-blocking down_read_trylock() and down_write_trylock() wrappers around pthread_rwlock_tryrdlock() / pthread_rwlock_trywrlock() and pthread_mutex_trylock() along with SHARED_TRYLOCK_FUNCTION and EXCLUSIVE_TRYLOCK_FUNCTION thread-safety annotations.

---
 tools/perf/util/mutex.h |  3 +++
 tools/perf/util/rwsem.c | 20 ++++++++++++++++++++
 tools/perf/util/rwsem.h |  2 ++
 3 files changed, 25 insertions(+)
tools/perf/util/mutex.h +3 -0
index 5de1065ea2b9..de7dd6d8326c 100644
@@ -57,6 +57,8 @@
 /* Documents functions that try to acquire a lock, and return success or failure. */
 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) \
 	__attribute__((exclusive_trylock_function(__VA_ARGS__)))
+#define SHARED_TRYLOCK_FUNCTION(...) \
+	__attribute__((shared_trylock_function(__VA_ARGS__)))
 
 /* Documents a function that expects a mutex to be held prior to entry. */
 #define EXCLUSIVE_LOCKS_REQUIRED(...) __attribute__((exclusive_locks_required(__VA_ARGS__)))
@@ -78,6 +80,7 @@
 #define SHARED_LOCK_FUNCTION(...)
 #define UNLOCK_FUNCTION(...)
 #define EXCLUSIVE_TRYLOCK_FUNCTION(...)
+#define SHARED_TRYLOCK_FUNCTION(...)
 #define EXCLUSIVE_LOCKS_REQUIRED(...)
 #define SHARED_LOCKS_REQUIRED(...)
 #define NO_THREAD_SAFETY_ANALYSIS
tools/perf/util/rwsem.c +20 -0
index 9d26832398db..37fa08bae5e9 100644
@@ -37,6 +37,16 @@ int down_read(struct rw_semaphore *sem)
 #endif
 }
 
+bool down_read_trylock(struct rw_semaphore *sem)
+	NO_THREAD_SAFETY_ANALYSIS
+{
+#if RWS_ERRORCHECK
+	return mutex_trylock(&sem->mtx);
+#else
+	return perf_singlethreaded ? true : pthread_rwlock_tryrdlock(&sem->lock) == 0;
+#endif
+}
+
 int up_read(struct rw_semaphore *sem)
 	NO_THREAD_SAFETY_ANALYSIS
 {
@@ -59,6 +69,16 @@ int down_write(struct rw_semaphore *sem)
 #endif
 }
 
+bool down_write_trylock(struct rw_semaphore *sem)
+	NO_THREAD_SAFETY_ANALYSIS
+{
+#if RWS_ERRORCHECK
+	return mutex_trylock(&sem->mtx);
+#else
+	return perf_singlethreaded ? true : pthread_rwlock_trywrlock(&sem->lock) == 0;
+#endif
+}
+
 int up_write(struct rw_semaphore *sem)
 	NO_THREAD_SAFETY_ANALYSIS
 {
tools/perf/util/rwsem.h +2 -0
index b102d8143181..782016c3e01f 100644
@@ -22,9 +22,11 @@ int init_rwsem(struct rw_semaphore *sem);
 int exit_rwsem(struct rw_semaphore *sem);
 
 int down_read(struct rw_semaphore *sem) SHARED_LOCK_FUNCTION(sem);
+bool down_read_trylock(struct rw_semaphore *sem) SHARED_TRYLOCK_FUNCTION(true, sem);
 int up_read(struct rw_semaphore *sem) UNLOCK_FUNCTION(sem);
 
 int down_write(struct rw_semaphore *sem) EXCLUSIVE_LOCK_FUNCTION(sem);
+bool down_write_trylock(struct rw_semaphore *sem) EXCLUSIVE_TRYLOCK_FUNCTION(true, sem);
 int up_write(struct rw_semaphore *sem) UNLOCK_FUNCTION(sem);
 
 #endif /* _PERF_RWSEM_H */
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#60 lore raw

Re: [PATCH v3 04/20] perf rwsem: Add down_read_trylock() and down_write_trylock()

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

On-demand memory shrinking (perf_session__shrink(), dsos__shrink(), symbols__shrink()) can be triggered from OOM recovery inside allocation wrappers while the current thread already holds a rw_semaphore (such as symbols->lock, dsos->lock, or machines->lock). Non-blocking trylock primitives are needed so shrinking paths can safely skip any locked container without deadlocking.

Quoted 6 lines

Add non-blocking down_read_trylock() and down_write_trylock() wrappers around pthread_rwlock_tryrdlock() / pthread_rwlock_trywrlock() and pthread_mutex_trylock() along with SHARED_TRYLOCK_FUNCTION and EXCLUSIVE_TRYLOCK_FUNCTION thread-safety annotations.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author reviewer
#61 lore raw

PATCH v3 5/20 perf map_symbol: Add struct dso_symbol and use for parent symbols

In reply to Ian Rogers

A struct symbol's lifetime is owned by the struct dso containing its symbol table rather than by an individual struct map. When retaining a symbol pointer beyond the scope of a temporary addr_location or map reference without holding the map itself, the owning struct dso must be refcounted alongside the symbol pointer.

Introduce struct dso_symbol pairing a refcounted struct dso * with a struct symbol *, along with dso_symbol__copy(), dso_symbol__exit(), and dso_symbol__from_al() lifecycle helpers in map_symbol.[ch].

Convert parent symbol tracking across callchain resolution, hist_entry_iter, and hist_entry from bare struct symbol * pointers to struct dso_symbol so that parent symbols pin their owning DSOs until the iterator or histogram entry is destroyed. Also drop the unused perf_session__resolve_callchain() prototype from session.h.

---
 tools/perf/ui/stdio/hist.c   |  4 ++--
 tools/perf/util/callchain.c  |  3 ++-
 tools/perf/util/callchain.h  |  3 ++-
 tools/perf/util/hist.c       | 31 +++++++++++++++++++------------
 tools/perf/util/hist.h       |  8 ++++----
 tools/perf/util/machine.c    | 36 +++++++++++++++++++++++-------------
 tools/perf/util/machine.h    |  5 +++--
 tools/perf/util/map_symbol.c | 30 ++++++++++++++++++++++++++++++
 tools/perf/util/map_symbol.h | 11 +++++++++++
 tools/perf/util/session.h    |  6 ------
 tools/perf/util/sort.c       |  6 +++---
 11 files changed, 99 insertions(+), 44 deletions(-)
tools/perf/ui/stdio/hist.c +2 -2
index 8c4c8925df2c..3240c39bd025 100644
@@ -424,7 +424,7 @@ int __hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp,
 	int ret;
 	bool first = true;
 
-	if (symbol_conf.exclude_other && !he->parent)
+	if (symbol_conf.exclude_other && !he->parent.sym)
 		return 0;
 
 	perf_hpp_list__for_each_format(hpp_list, fmt) {
@@ -471,7 +471,7 @@ static int hist_entry__hierarchy_fprintf(struct hist_entry *he,
 	int ret, printed = 0;
 	bool first = true;
 
-	if (symbol_conf.exclude_other && !he->parent)
+	if (symbol_conf.exclude_other && !he->parent.sym)
 		return 0;
 
 	ret = scnprintf(hpp->buf, hpp->size, "%*s", he->depth * HIERARCHY_INDENT, "");
tools/perf/util/callchain.c +2 -1
index 6d5212be466d..4e4696cfa6d4 100644
@@ -1170,7 +1170,8 @@ int callchain_cursor_append(struct callchain_cursor *cursor,
 }
 
 int sample__resolve_callchain(struct perf_sample *sample,
-			      struct callchain_cursor *cursor, struct symbol **parent,
+			      struct callchain_cursor *cursor,
+			      struct dso_symbol *parent,
 			      struct addr_location *al,
 			      int max_stack)
 {
tools/perf/util/callchain.h +2 -1
index 8b1e405d1cdf..23f59b389799 100644
@@ -242,7 +242,8 @@ int record_opts__parse_callchain(struct record_opts *record,
 				 const char *arg, bool unset);
 
 int sample__resolve_callchain(struct perf_sample *sample,
-			      struct callchain_cursor *cursor, struct symbol **parent,
+			      struct callchain_cursor *cursor,
+			      struct dso_symbol *parent,
 			      struct addr_location *al,
 			      int max_stack);
 int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample);
tools/perf/util/hist.c +19 -12
index 364754e9d12a..d516ce469c7e 100644
@@ -119,8 +119,8 @@ void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
 		hists__new_col_len(hists, HISTC_DSO, len);
 	}
 
-	if (h->parent)
-		hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
+	if (h->parent.sym)
+		hists__new_col_len(hists, HISTC_PARENT, h->parent.sym->namelen);
 
 	if (h->branch_info) {
 		if (h->branch_info->from.ms.sym) {
@@ -543,6 +543,7 @@ static int hist_entry__init(struct hist_entry *he,
 
 	he->ms.thread = thread__get(he->ms.thread);
 	he->ms.map = map__get(he->ms.map);
+	dso_symbol__copy(&he->parent, &template->parent);
 
 	if (he->branch_info) {
 		/*
@@ -616,6 +617,7 @@ static int hist_entry__init(struct hist_entry *he,
 	if (he->mem_info)
 		mem_info__zput(he->mem_info);
 err:
+	dso_symbol__exit(&he->parent);
 	map_symbol__exit(&he->ms);
 	zfree(&he->stat_acc);
 	return -ENOMEM;
@@ -796,7 +798,7 @@ static void hists__res_sample(struct hist_entry *he, struct perf_sample *sample)
 static struct hist_entry*
 __hists__add_entry(struct hists *hists,
 		   struct addr_location *al,
-		   struct symbol *sym_parent,
+		   const struct dso_symbol *sym_parent,
 		   struct branch_info *bi,
 		   struct mem_info *mi,
 		   struct kvm_info *ki,
@@ -835,8 +837,9 @@ __hists__add_entry(struct hists *hists,
 			.weight3 = sample->weight3,
 			.latency = al->latency,
 		},
-		.parent = sym_parent,
-		.filtered = symbol__parent_filter(sym_parent) | al->filtered,
+		.parent = sym_parent ? *sym_parent : (struct dso_symbol){},
+		.filtered = symbol__parent_filter(sym_parent ? sym_parent->sym : NULL) |
+			    al->filtered,
 		.hists	= hists,
 		.branch_info = bi,
 		.mem_info = mi,
@@ -862,7 +865,7 @@ __hists__add_entry(struct hists *hists,
 
 struct hist_entry *hists__add_entry(struct hists *hists,
 				    struct addr_location *al,
-				    struct symbol *sym_parent,
+				    const struct dso_symbol *sym_parent,
 				    struct branch_info *bi,
 				    struct mem_info *mi,
 				    struct kvm_info *ki,
@@ -876,7 +879,7 @@ struct hist_entry *hists__add_entry(struct hists *hists,
 struct hist_entry *hists__add_entry_ops(struct hists *hists,
 					struct hist_entry_ops *ops,
 					struct addr_location *al,
-					struct symbol *sym_parent,
+					const struct dso_symbol *sym_parent,
 					struct branch_info *bi,
 					struct mem_info *mi,
 					struct kvm_info *ki,
@@ -957,7 +960,7 @@ iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al
 	 */
 	sample->period = cost;
 
-	he = hists__add_entry(hists, al, iter->parent, NULL, mi, NULL,
+	he = hists__add_entry(hists, al, &iter->parent, NULL, mi, NULL,
 			      sample, true);
 	if (!he)
 		return -ENOMEM;
@@ -1057,7 +1060,7 @@ iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *a
 	sample->period = 1;
 	sample->weight = bi[i].flags.cycles ? bi[i].flags.cycles : 1;
 
-	he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL, NULL,
+	he = hists__add_entry(hists, al, &iter->parent, &bi[i], NULL, NULL,
 			      sample, true);
 	if (he == NULL)
 		return -ENOMEM;
@@ -1109,7 +1112,7 @@ iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location
 	struct evsel *evsel = sample->evsel;
 	struct hist_entry *he;
 
-	he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
+	he = hists__add_entry(evsel__hists(evsel), al, &iter->parent, NULL, NULL,
 			      NULL, sample, true);
 	if (he == NULL)
 		return -ENOMEM;
@@ -1174,7 +1177,7 @@ iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
 	struct hist_entry *he;
 	int err = 0;
 
-	he = hists__add_entry(hists, al, iter->parent, NULL, NULL, NULL,
+	he = hists__add_entry(hists, al, &iter->parent, NULL, NULL, NULL,
 			      sample, true);
 	if (he == NULL)
 		return -ENOMEM;
@@ -1277,7 +1280,7 @@ iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
 		}
 	}
 
-	he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
+	he = hists__add_entry(evsel__hists(evsel), al, &iter->parent, NULL, NULL,
 			      NULL, sample, false);
 	if (he == NULL)
 		return -ENOMEM;
@@ -1340,12 +1343,14 @@ int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
 	int err, err2;
 	struct map *alm = NULL;
 
+	dso_symbol__exit(&iter->parent);
 	if (al)
 		alm = map__get(al->map);
 
 	err = sample__resolve_callchain(iter->sample, get_tls_callchain_cursor(), &iter->parent,
 					al, max_stack_depth);
 	if (err) {
+		dso_symbol__exit(&iter->parent);
 		map__put(alm);
 		return err;
 	}
@@ -1381,6 +1386,7 @@ int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
 	if (!err)
 		err = err2;
 
+	dso_symbol__exit(&iter->parent);
 	map__put(alm);
 
 	return err;
@@ -1468,6 +1474,7 @@ void hist_entry__delete(struct hist_entry *he)
 
 	thread__zput(he->thread);
 	map_symbol__exit(&he->ms);
+	dso_symbol__exit(&he->parent);
 
 	if (he->branch_info) {
 		branch_info__exit(he->branch_info);
tools/perf/util/hist.h +4 -4
index 0d08011aebce..4553365b3d8c 100644
@@ -159,7 +159,7 @@ struct hist_entry_iter {
 
 	struct perf_sample *sample;
 	struct hist_entry *he;
-	struct symbol *parent;
+	struct dso_symbol parent;
 
 	struct mem_info *mi;
 	struct branch_info *bi;
@@ -292,7 +292,7 @@ struct hist_entry {
 	};
 	char			*srcline;
 	char			*srcfile;
-	struct symbol		*parent;
+	struct dso_symbol	parent;
 	struct branch_info	*branch_info;
 	long			time;
 	struct hists		*hists;
@@ -350,7 +350,7 @@ static inline void hist_entry__add_pair(struct hist_entry *pair,
 
 struct hist_entry *hists__add_entry(struct hists *hists,
 				    struct addr_location *al,
-				    struct symbol *parent,
+				    const struct dso_symbol *parent,
 				    struct branch_info *bi,
 				    struct mem_info *mi,
 				    struct kvm_info *ki,
@@ -360,7 +360,7 @@ struct hist_entry *hists__add_entry(struct hists *hists,
 struct hist_entry *hists__add_entry_ops(struct hists *hists,
 					struct hist_entry_ops *ops,
 					struct addr_location *al,
-					struct symbol *sym_parent,
+					const struct dso_symbol *sym_parent,
 					struct branch_info *bi,
 					struct mem_info *mi,
 					struct kvm_info *ki,
tools/perf/util/machine.c +23 -13
index f6975d9c4568..905d275ca364 100644
@@ -2235,7 +2235,7 @@ static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms
 
 static int add_callchain_ip(struct thread *thread,
 			    struct callchain_cursor *cursor,
-			    struct symbol **parent,
+			    struct dso_symbol *parent,
 			    struct addr_location *root_al,
 			    u8 *cpumode,
 			    u64 ip,
@@ -2278,6 +2278,8 @@ static int add_callchain_ip(struct thread *thread,
 				 * Discard all.
 				 */
 				callchain_cursor_reset(cursor);
+				if (parent)
+					dso_symbol__exit(parent);
 				err = 1;
 				goto out;
 			}
@@ -2290,10 +2292,12 @@ static int add_callchain_ip(struct thread *thread,
 	}
 
 	if (al.sym != NULL) {
-		if (perf_hpp_list.parent && !*parent &&
-		    symbol__match_regex(al.sym, &parent_regex))
-			*parent = al.sym;
-		else if (have_ignore_callees && root_al &&
+		if (perf_hpp_list.parent && parent && !parent->sym &&
+		    symbol__match_regex(al.sym, &parent_regex)) {
+			struct dso_symbol ds = dso_symbol__from_al(&al);
+
+			dso_symbol__copy(parent, &ds);
+		} else if (have_ignore_callees && root_al &&
 		  symbol__match_regex(al.sym, &ignore_callees_regex)) {
 			/* Treat this symbol as the root,
 			   forgetting its callees. */
@@ -2417,7 +2421,7 @@ static int remove_loops(struct branch_entry *l, int nr,
 static int lbr_callchain_add_kernel_ip(struct thread *thread,
 				       struct callchain_cursor *cursor,
 				       struct perf_sample *sample,
-				       struct symbol **parent,
+				       struct dso_symbol *parent,
 				       struct addr_location *root_al,
 				       u64 branch_from,
 				       bool callee, int end,
@@ -2483,7 +2487,7 @@ static void save_lbr_cursor_node(struct thread *thread,
 static int lbr_callchain_add_lbr_ip(struct thread *thread,
 				    struct callchain_cursor *cursor,
 				    struct perf_sample *sample,
-				    struct symbol **parent,
+				    struct dso_symbol *parent,
 				    struct addr_location *root_al,
 				    u64 *branch_from,
 				    bool callee,
@@ -2751,7 +2755,7 @@ static bool alloc_lbr_stitch(struct thread *thread, unsigned int max_lbr)
 static int resolve_lbr_callchain_sample(struct thread *thread,
 					struct callchain_cursor *cursor,
 					struct perf_sample *sample,
-					struct symbol **parent,
+					struct dso_symbol *parent,
 					struct addr_location *root_al,
 					int max_stack,
 					unsigned int max_lbr,
@@ -2838,7 +2842,7 @@ static int resolve_lbr_callchain_sample(struct thread *thread,
 
 static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
 			     struct callchain_cursor *cursor,
-			     struct symbol **parent,
+			     struct dso_symbol *parent,
 			     struct addr_location *root_al,
 			     u8 *cpumode, int ent, bool symbols)
 {
@@ -2869,7 +2873,7 @@ static u64 get_leaf_frame_caller(struct perf_sample *sample,
 static int thread__resolve_callchain_sample(struct thread *thread,
 					    struct callchain_cursor *cursor,
 					    struct perf_sample *sample,
-					    struct symbol **parent,
+					    struct dso_symbol *parent,
 					    struct addr_location *root_al,
 					    int max_stack,
 					    bool symbols)
@@ -3101,13 +3105,16 @@ static int thread__resolve_callchain_unwind(struct thread *thread,
 int __thread__resolve_callchain(struct thread *thread,
 				struct callchain_cursor *cursor,
 				struct perf_sample *sample,
-				struct symbol **parent,
+				struct dso_symbol *parent,
 				struct addr_location *root_al,
 				int max_stack,
 				bool symbols)
 {
 	int ret = 0;
 
+	if (parent)
+		dso_symbol__exit(parent);
+
 	if (cursor == NULL)
 		return -ENOMEM;
 
@@ -3119,7 +3126,7 @@ int __thread__resolve_callchain(struct thread *thread,
 						       parent, root_al,
 						       max_stack, symbols);
 		if (ret)
-			return ret;
+			goto out;
 		ret = thread__resolve_callchain_unwind(thread, cursor,
 						       sample,
 						       max_stack, symbols);
@@ -3128,13 +3135,16 @@ int __thread__resolve_callchain(struct thread *thread,
 						       sample,
 						       max_stack, symbols);
 		if (ret)
-			return ret;
+			goto out;
 		ret = thread__resolve_callchain_sample(thread, cursor,
 						       sample,
 						       parent, root_al,
 						       max_stack, symbols);
 	}
 
+out:
+	if (ret && parent)
+		dso_symbol__exit(parent);
 	return ret;
 }
 
tools/perf/util/machine.h +3 -2
index 147ce343e370..80191545ee8a 100644
@@ -193,11 +193,12 @@ struct mem_info *sample__resolve_mem(struct perf_sample *sample,
 				     struct addr_location *al);
 
 struct callchain_cursor;
+struct dso_symbol;
 
 int __thread__resolve_callchain(struct thread *thread,
 				struct callchain_cursor *cursor,
 				struct perf_sample *sample,
-				struct symbol **parent,
+				struct dso_symbol *parent,
 				struct addr_location *root_al,
 				int max_stack,
 				bool symbols);
@@ -205,7 +206,7 @@ int __thread__resolve_callchain(struct thread *thread,
 static inline int thread__resolve_callchain(struct thread *thread,
 					    struct callchain_cursor *cursor,
 					    struct perf_sample *sample,
-					    struct symbol **parent,
+					    struct dso_symbol *parent,
 					    struct addr_location *root_al,
 					    int max_stack)
 {
tools/perf/util/map_symbol.c +30 -0
index 11bc0a7f704c..595ec8f0541d 100644
@@ -1,9 +1,39 @@
 // SPDX-License-Identifier: GPL-2.0
 #include "map_symbol.h"
+#include "addr_location.h"
+#include "dso.h"
 #include "maps.h"
 #include "map.h"
 #include "thread.h"
 
+void dso_symbol__exit(struct dso_symbol *ds)
+{
+	dso__zput(ds->dso);
+	ds->sym = NULL;
+}
+
+/*
+ * Initialize @dst from @src, acquiring a reference on @src->dso (without
+ * releasing any prior @dst->dso, matching map_symbol__copy()).
+ */
+void dso_symbol__copy(struct dso_symbol *dst, const struct dso_symbol *src)
+{
+	dst->dso = src ? dso__get(src->dso) : NULL;
+	dst->sym = src ? src->sym : NULL;
+}
+
+/*
+ * Construct a borrowed (non-owning) dso_symbol view from @al while @al is
+ * live. Callers that persist the pair must retain it via dso_symbol__copy().
+ */
+struct dso_symbol dso_symbol__from_al(const struct addr_location *al)
+{
+	return (struct dso_symbol){
+		.dso = (al && al->sym && al->map) ? map__dso(al->map) : NULL,
+		.sym = al ? al->sym : NULL,
+	};
+}
+
 void map_symbol__exit(struct map_symbol *ms)
 {
 	thread__zput(ms->thread);
tools/perf/util/map_symbol.h +11 -0
index 7437e319f4a3..1781af7bd965 100644
@@ -4,11 +4,18 @@
 
 #include <linux/types.h>
 
+struct addr_location;
+struct dso;
 struct thread;
 struct maps;
 struct map;
 struct symbol;
 
+struct dso_symbol {
+	struct dso    *dso;
+	struct symbol *sym;
+};
+
 struct map_symbol {
 	struct thread *thread;
 	struct map    *map;
@@ -24,6 +31,10 @@ struct addr_map_symbol {
 	u64	      data_page_size;
 };
 
+void dso_symbol__exit(struct dso_symbol *ds);
+void dso_symbol__copy(struct dso_symbol *dst, const struct dso_symbol *src);
+struct dso_symbol dso_symbol__from_al(const struct addr_location *al);
+
 void map_symbol__exit(struct map_symbol *ms);
 void addr_map_symbol__exit(struct addr_map_symbol *ams);
 
tools/perf/util/session.h +0 -6
index ac5803d5fb4e..05ea2c1ee8c9 100644
@@ -139,12 +139,6 @@ int perf_session__process_events(struct perf_session *session);
 int perf_session__queue_event(struct perf_session *s, union perf_event *event,
 			      u64 timestamp, u64 file_offset, const char *file_path);
 
-int perf_session__resolve_callchain(struct perf_session *session,
-				    struct evsel *evsel,
-				    struct thread *thread,
-				    struct ip_callchain *chain,
-				    struct symbol **parent);
-
 bool perf_session__has_traces(struct perf_session *session, const char *msg);
 bool perf_session__has_switch_events(struct perf_session *session);
 
tools/perf/util/sort.c +3 -3
index 58638ec9ae0e..f4c50f191141 100644
@@ -1010,8 +1010,8 @@ static struct sort_entry sort_srcfile = {
 static int64_t
 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
 {
-	struct symbol *sym_l = left->parent;
-	struct symbol *sym_r = right->parent;
+	struct symbol *sym_l = left->parent.sym;
+	struct symbol *sym_r = right->parent.sym;
 
 	if (!sym_l || !sym_r)
 		return cmp_null(sym_l, sym_r);
@@ -1023,7 +1023,7 @@ static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
 				       size_t size, unsigned int width)
 {
 	return repsep_snprintf(bf, size, "%-*.*s", width, width,
-			      he->parent ? he->parent->name : "[other]");
+			      he->parent.sym ? he->parent.sym->name : "[other]");
 }
 
 struct sort_entry sort_parent = {
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#62 lore raw

Re: [PATCH v3 05/20] perf map_symbol: Add struct dso_symbol and use for parent symbols

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

A struct symbol's lifetime is owned by the struct dso containing its symbol table rather than by an individual struct map. When retaining a symbol pointer beyond the scope of a temporary addr_location or map reference without holding the map itself, the owning struct dso must be refcounted alongside the symbol pointer.

Introduce struct dso_symbol pairing a refcounted struct dso * with a struct symbol *, along with dso_symbol__copy(), dso_symbol__exit(), and dso_symbol__from_al() lifecycle helpers in map_symbol.[ch].

Quoted 7 lines

Convert parent symbol tracking across callchain resolution, hist_entry_iter, and hist_entry from bare struct symbol * pointers to struct dso_symbol so that parent symbols pin their owning DSOs until the iterator or histogram entry is destroyed. Also drop the unused perf_session__resolve_callchain() prototype from session.h.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author reviewer
#63 lore raw

PATCH v3 6/20 perf call-path: Use struct dso_symbol to retain symbol DSO lifetimes

In reply to Ian Rogers

struct call_path stores resolved function symbols for the lifetime of a call_path_root (in thread-stack call/return processing and db-export) without holding a map reference. Similarly, Python callchain frames and nodes store resolved symbols across Python object lifetimes.

Replace bare struct symbol * fields in struct call_path, struct pyrf_callchain_frame, and struct pyrf_callchain_node with struct dso_symbol so that each retained symbol pins its owning struct dso until call_path_root__free() or Python deallocation. In python.c, this also avoids duplicating symbol names for call_path frames and allows pyrf_callchain_node__get_dso() to report the DSO of resolved call_path symbols whose ip was zeroed.

---
 tools/perf/util/call-path.c    | 42 ++++++++++++++++++++-------
 tools/perf/util/call-path.h    | 10 ++++---
 tools/perf/util/db-export.c    | 12 ++++++--
 tools/perf/util/python.c       | 53 +++++++++++++++-------------------
 tools/perf/util/thread-stack.c | 40 +++++++++++++------------
 5 files changed, 93 insertions(+), 64 deletions(-)
tools/perf/util/call-path.c +32 -10
index 5c60b8be1cf6..eac52a902d6b 100644
@@ -10,13 +10,21 @@
 #include <stdlib.h>
 
 #include "call-path.h"
+#include "dso.h"
 
 static void call_path__init(struct call_path *cp, struct call_path *parent,
-			    struct symbol *sym, u64 ip, bool in_kernel)
+			    const struct dso_symbol *ds, u64 ip, bool in_kernel)
 {
+	struct symbol *sym = ds ? ds->sym : NULL;
+
 	cp->parent = parent;
-	cp->sym = sym;
-	cp->ip = sym ? 0 : ip;
+	if (sym) {
+		dso_symbol__copy(&cp->ds, ds);
+		cp->ip = 0;
+	} else {
+		cp->ds = (struct dso_symbol){};
+		cp->ip = ip;
+	}
 	cp->db_id = 0;
 	cp->in_kernel = in_kernel;
 	RB_CLEAR_NODE(&cp->rb_node);
@@ -39,7 +47,13 @@ void call_path_root__free(struct call_path_root *cpr)
 {
 	struct call_path_block *pos, *n;
 
+	if (!cpr)
+		return;
+
+	dso_symbol__exit(&cpr->call_path.ds);
 	list_for_each_entry_safe(pos, n, &cpr->blocks, node) {
+		for (size_t i = 0; i < CALL_PATH_BLOCK_SIZE; i++)
+			dso_symbol__exit(&pos->cp[i].ds);
 		list_del_init(&pos->node);
 		free(pos);
 	}
@@ -48,7 +62,7 @@ void call_path_root__free(struct call_path_root *cpr)
 
 static struct call_path *call_path__new(struct call_path_root *cpr,
 					struct call_path *parent,
-					struct symbol *sym, u64 ip,
+					const struct dso_symbol *ds, u64 ip,
 					bool in_kernel)
 {
 	struct call_path_block *cpb;
@@ -69,17 +83,20 @@ static struct call_path *call_path__new(struct call_path_root *cpr,
 	n = cpr->next++ & CALL_PATH_BLOCK_MASK;
 	cp = &cpb->cp[n];
 
-	call_path__init(cp, parent, sym, ip, in_kernel);
+	call_path__init(cp, parent, ds, ip, in_kernel);
 
 	return cp;
 }
 
 struct call_path *call_path__findnew(struct call_path_root *cpr,
 				     struct call_path *parent,
-				     struct symbol *sym, u64 ip, u64 ks)
+				     const struct dso_symbol *ds,
+				     u64 ip, u64 ks)
 {
 	struct rb_node **p;
 	struct rb_node *node_parent = NULL;
+	struct symbol *sym = ds ? ds->sym : NULL;
+	const void *dso_ptr = (sym && ds->dso) ? RC_CHK_ACCESS(ds->dso) : NULL;
 	struct call_path *cp;
 	bool in_kernel = ip >= ks;
 
@@ -87,23 +104,28 @@ struct call_path *call_path__findnew(struct call_path_root *cpr,
 		ip = 0;
 
 	if (!parent)
-		return call_path__new(cpr, parent, sym, ip, in_kernel);
+		return call_path__new(cpr, parent, ds, ip, in_kernel);
 
 	p = &parent->children.rb_node;
 	while (*p != NULL) {
+		const void *cp_dso_ptr;
+
 		node_parent = *p;
 		cp = rb_entry(node_parent, struct call_path, rb_node);
+		cp_dso_ptr = cp->ds.dso ? RC_CHK_ACCESS(cp->ds.dso) : NULL;
 
-		if (cp->sym == sym && cp->ip == ip)
+		if (cp->ds.sym == sym && cp_dso_ptr == dso_ptr && cp->ip == ip)
 			return cp;
 
-		if (sym < cp->sym || (sym == cp->sym && ip < cp->ip))
+		if (sym < cp->ds.sym ||
+		    (sym == cp->ds.sym && dso_ptr < cp_dso_ptr) ||
+		    (sym == cp->ds.sym && dso_ptr == cp_dso_ptr && ip < cp->ip))
 			p = &(*p)->rb_left;
 		else
 			p = &(*p)->rb_right;
 	}
 
-	cp = call_path__new(cpr, parent, sym, ip, in_kernel);
+	cp = call_path__new(cpr, parent, ds, ip, in_kernel);
 	if (!cp)
 		return NULL;
 
tools/perf/util/call-path.h +6 -4
index 5875cfc8106e..e43aa7513633 100644
@@ -11,12 +11,13 @@
 
 #include <linux/types.h>
 #include <linux/rbtree.h>
+#include "map_symbol.h"
 
 /**
  * struct call_path - node in list of calls leading to a function call.
  * @parent: call path to the parent function call
- * @sym: symbol of function called
- * @ip: only if sym is null, the ip of the function
+ * @ds: DSO and symbol of function called
+ * @ip: only if ds.sym is null, the ip of the function
  * @db_id: id used for db-export
  * @in_kernel: whether function is a in the kernel
  * @rb_node: node in parent's tree of called functions
@@ -27,7 +28,7 @@
  */
 struct call_path {
 	struct call_path *parent;
-	struct symbol *sym;
+	struct dso_symbol ds;
 	u64 ip;
 	u64 db_id;
 	bool in_kernel;
@@ -63,6 +64,7 @@ void call_path_root__free(struct call_path_root *cpr);
 
 struct call_path *call_path__findnew(struct call_path_root *cpr,
 				     struct call_path *parent,
-				     struct symbol *sym, u64 ip, u64 ks);
+				     const struct dso_symbol *ds,
+				     u64 ip, u64 ks);
 
 #endif
tools/perf/util/db-export.c +9 -3
index ba54b1119ee6..6a5db62cd58b 100644
@@ -261,12 +261,18 @@ static struct call_path *call_path_from_sample(struct db_export *dbe,
 		db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
 
 		/* add node to the call path tree if it doesn't exist */
-		current = call_path__findnew(dbe->cpr, current,
-					     al.sym, node->ip,
-					     kernel_start);
+		{
+			struct dso_symbol ds = dso_symbol__from_al(&al);
+
+			current = call_path__findnew(dbe->cpr, current,
+						     &ds, node->ip,
+						     kernel_start);
+		}
 
 		callchain_cursor_advance(cursor);
 		addr_location__exit(&al);
+		if (!current)
+			break;
 	}
 
 	/* Reset the callchain order to its prior value. */
tools/perf/util/python.c +24 -29

127 lines not on this page · raw message

tools/perf/util/thread-stack.c +22 -18
index d452d1a7eabb..aa05acb0467c 100644
@@ -11,6 +11,7 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
+#include "addr_location.h"
 #include "thread.h"
 #include "event.h"
 #include "machine.h"
@@ -823,7 +824,7 @@ static int thread_stack__pop_cp(struct thread *thread, struct thread_stack *ts,
 	if (ts->cnt == 1) {
 		struct thread_stack_entry *tse = &ts->stack[0];
 
-		if (tse->cp->sym == sym)
+		if (tse->cp->ds.sym == sym)
 			return thread_stack__call_return(thread, ts, --ts->cnt,
 							 timestamp, ref, false);
 	}
@@ -862,21 +863,21 @@ static int thread_stack__bottom(struct thread_stack *ts,
 				struct addr_location *to_al, u64 ref)
 {
 	struct call_path_root *cpr = ts->crp->cpr;
+	struct dso_symbol ds;
 	struct call_path *cp;
-	struct symbol *sym;
 	u64 ip;
 
 	if (sample->ip) {
 		ip = sample->ip;
-		sym = from_al->sym;
+		ds = dso_symbol__from_al(from_al);
 	} else if (sample->addr) {
 		ip = sample->addr;
-		sym = to_al->sym;
+		ds = dso_symbol__from_al(to_al);
 	} else {
 		return 0;
 	}
 
-	cp = call_path__findnew(cpr, &cpr->call_path, sym, ip,
+	cp = call_path__findnew(cpr, &cpr->call_path, &ds, ip,
 				ts->kernel_start);
 
 	return thread_stack__push_cp(ts, ip, sample->time, ref, cp,
@@ -908,8 +909,8 @@ static int thread_stack__no_call_return(struct thread *thread,
 {
 	struct call_path_root *cpr = ts->crp->cpr;
 	struct call_path *root = &cpr->call_path;
-	struct symbol *fsym = from_al->sym;
-	struct symbol *tsym = to_al->sym;
+	struct dso_symbol fds = dso_symbol__from_al(from_al);
+	struct dso_symbol tds = dso_symbol__from_al(to_al);
 	struct call_path *cp, *parent;
 	u64 ks = ts->kernel_start;
 	u64 addr = sample->addr;
@@ -925,7 +926,7 @@ static int thread_stack__no_call_return(struct thread *thread,
 
 		/* If the stack is empty, push the userspace address */
 		if (!ts->cnt) {
-			cp = call_path__findnew(cpr, root, tsym, addr, ks);
+			cp = call_path__findnew(cpr, root, &tds, addr, ks);
 			return thread_stack__push_cp(ts, 0, tm, ref, cp, true,
 						     false);
 		}
@@ -941,7 +942,7 @@ static int thread_stack__no_call_return(struct thread *thread,
 	else
 		parent = root;
 
-	if (parent->sym == from_al->sym) {
+	if (parent->ds.sym == from_al->sym) {
 		/*
 		 * At the bottom of the stack, assume the missing 'call' was
 		 * before the trace started. So, pop the current symbol and push
@@ -955,7 +956,7 @@ static int thread_stack__no_call_return(struct thread *thread,
 		}
 
 		if (!ts->cnt) {
-			cp = call_path__findnew(cpr, root, tsym, addr, ks);
+			cp = call_path__findnew(cpr, root, &tds, addr, ks);
 
 			return thread_stack__push_cp(ts, addr, tm, ref, cp,
 						     true, false);
@@ -965,7 +966,7 @@ static int thread_stack__no_call_return(struct thread *thread,
 		 * Otherwise assume the 'return' is being used as a jump (e.g.
 		 * retpoline) and just push the 'to' symbol.
 		 */
-		cp = call_path__findnew(cpr, parent, tsym, addr, ks);
+		cp = call_path__findnew(cpr, parent, &tds, addr, ks);
 
 		err = thread_stack__push_cp(ts, 0, tm, ref, cp, true, false);
 		if (!err)
@@ -979,13 +980,13 @@ static int thread_stack__no_call_return(struct thread *thread,
 	 * pop 'from'.
 	 */
 
-	cp = call_path__findnew(cpr, parent, tsym, addr, ks);
+	cp = call_path__findnew(cpr, parent, &tds, addr, ks);
 
 	err = thread_stack__push_cp(ts, addr, tm, ref, cp, true, false);
 	if (err)
 		return err;
 
-	cp = call_path__findnew(cpr, cp, fsym, ip, ks);
+	cp = call_path__findnew(cpr, cp, &fds, ip, ks);
 
 	err = thread_stack__push_cp(ts, ip, tm, ref, cp, true, false);
 	if (err)
@@ -1052,7 +1053,8 @@ static int thread_stack__x86_retpoline(struct thread_stack *ts,
 {
 	struct thread_stack_entry *tse = &ts->stack[ts->cnt - 1];
 	struct call_path_root *cpr = ts->crp->cpr;
-	struct symbol *sym = tse->cp->sym;
+	struct symbol *sym = tse->cp->ds.sym;
+	struct dso_symbol tds = dso_symbol__from_al(to_al);
 	struct symbol *tsym = to_al->sym;
 	struct call_path *cp;
 
@@ -1068,7 +1070,7 @@ static int thread_stack__x86_retpoline(struct thread_stack *ts,
 		 * graph, since all the original branches are left unchanged.
 		 */
 		ts->cnt -= 1;
-		sym = ts->stack[ts->cnt - 2].cp->sym;
+		sym = ts->stack[ts->cnt - 2].cp->ds.sym;
 		if (sym && sym == tsym && to_al->addr != tsym->start) {
 			/*
 			 * Target is back to the middle of the symbol we came
@@ -1087,7 +1089,7 @@ static int thread_stack__x86_retpoline(struct thread_stack *ts,
 		return 0;
 	}
 
-	cp = call_path__findnew(cpr, ts->stack[ts->cnt - 2].cp, tsym,
+	cp = call_path__findnew(cpr, ts->stack[ts->cnt - 2].cp, &tds,
 				sample->addr, ts->kernel_start);
 	if (!cp)
 		return -ENOMEM;
@@ -1147,6 +1149,7 @@ int thread_stack__process(struct thread *thread, struct comm *comm,
 
 	if (sample->flags & PERF_IP_FLAG_CALL) {
 		bool trace_end = sample->flags & PERF_IP_FLAG_TRACE_END;
+		struct dso_symbol tds = dso_symbol__from_al(to_al);
 		struct call_path_root *cpr = ts->crp->cpr;
 		struct call_path *cp;
 		u64 ret_addr;
@@ -1160,7 +1163,7 @@ int thread_stack__process(struct thread *thread, struct comm *comm,
 			return 0; /* Zero-length calls are excluded */
 
 		cp = call_path__findnew(cpr, ts->stack[ts->cnt - 1].cp,
-					to_al->sym, sample->addr,
+					&tds, sample->addr,
 					ts->kernel_start);
 		err = thread_stack__push_cp(ts, ret_addr, sample->time, ref,
 					    cp, false, trace_end);
@@ -1209,6 +1212,7 @@ int thread_stack__process(struct thread *thread, struct comm *comm,
 	} else if (sample->flags & PERF_IP_FLAG_BRANCH &&
 		   from_al->sym != to_al->sym && to_al->sym &&
 		   to_al->addr == to_al->sym->start) {
+		struct dso_symbol tds = dso_symbol__from_al(to_al);
 		struct call_path_root *cpr = ts->crp->cpr;
 		struct call_path *cp;
 
@@ -1219,7 +1223,7 @@ int thread_stack__process(struct thread *thread, struct comm *comm,
 		 * when a ret pops the stack, all jmps must be popped off first.
 		 */
 		cp = call_path__findnew(cpr, ts->stack[ts->cnt - 1].cp,
-					to_al->sym, sample->addr,
+					&tds, sample->addr,
 					ts->kernel_start);
 		err = thread_stack__push_cp(ts, 0, sample->time, ref, cp, false,
 					    false);
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#64 lore raw

Re: [PATCH v3 06/20] perf call-path: Use struct dso_symbol to retain symbol DSO lifetimes

Sashiko has reviewed this patch and found no issues.
Quoted 13 lines

struct call_path stores resolved function symbols for the lifetime of a call_path_root (in thread-stack call/return processing and db-export) without holding a map reference. Similarly, Python callchain frames and nodes store resolved symbols across Python object lifetimes.

Replace bare struct symbol * fields in struct call_path, struct pyrf_callchain_frame, and struct pyrf_callchain_node with struct dso_symbol so that each retained symbol pins its owning struct dso until call_path_root__free() or Python deallocation. In python.c, this also avoids duplicating symbol names for call_path frames and allows pyrf_callchain_node__get_dso() to report the DSO of resolved call_path symbols whose ip was zeroed.

Quoted 1 line

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
author reviewer
#65 lore raw

PATCH v3 7/20 perf db-export: Store symbol database IDs in a hashmap

In reply to Ian Rogers

Currently, db-export reserves sizeof(u64) bytes ahead of every allocated struct symbol via symbol_conf.priv_size and accesses the database ID at a negative offset via symbol__priv(). This adds 8 bytes to every loaded symbol across all DSOs even when only a subset of symbols are exported, and relies on negative-offset pointer arithmetic ahead of struct symbol.

Store exported symbol database IDs in an on-demand sym_db_ids hashmap in struct db_export keyed by struct dso_symbol * instead of using symbol__priv(), and retain exported struct dso references in db_export until db_export__exit() so exported DSO and symbol database IDs remain valid across the export session.

---
 tools/perf/util/db-export.c | 126 ++++++++++++++++++++++++++++++++----
 tools/perf/util/db-export.h |  16 +++++
 2 files changed, 129 insertions(+), 13 deletions(-)
tools/perf/util/db-export.c +113 -13
index 6a5db62cd58b..e7b807e968cf 100644
@@ -4,21 +4,46 @@
  * Copyright (c) 2014, Intel Corporation.
  */
 
+#include "db-export.h"
+
 #include <errno.h>
+#include <stdbool.h>
+#include <stddef.h>
 #include <stdlib.h>
+#include <string.h>
+
+#include <linux/err.h>
+#include <linux/zalloc.h>
 
+#include "call-path.h"
+#include "callchain.h"
+#include "comm.h"
 #include "dso.h"
+#include "event.h"
 #include "evsel.h"
+#include "hashmap.h"
 #include "machine.h"
-#include "thread.h"
-#include "comm.h"
-#include "symbol.h"
 #include "map.h"
-#include "event.h"
+#include "map_symbol.h"
+#include "symbol.h"
 #include "thread-stack.h"
-#include "callchain.h"
-#include "call-path.h"
-#include "db-export.h"
+#include "thread.h"
+
+static size_t dso_sym_hash(long key, void *ctx __maybe_unused)
+{
+	const struct dso_symbol *ds = (const struct dso_symbol *)key;
+	const void *dso_ptr = ds->dso ? RC_CHK_ACCESS(ds->dso) : NULL;
+
+	return (size_t)dso_ptr ^ (size_t)ds->sym;
+}
+
+static bool dso_sym_equal(long key1, long key2, void *ctx __maybe_unused)
+{
+	const struct dso_symbol *ds1 = (const struct dso_symbol *)key1;
+	const struct dso_symbol *ds2 = (const struct dso_symbol *)key2;
+
+	return ds1->sym == ds2->sym && RC_CHK_EQUAL(ds1->dso, ds2->dso);
+}
 
 int db_export__init(struct db_export *dbe)
 {
@@ -28,6 +53,26 @@ int db_export__init(struct db_export *dbe)
 
 void db_export__exit(struct db_export *dbe)
 {
+	if (dbe->sym_db_ids) {
+		struct hashmap_entry *entry;
+		size_t bkt;
+
+		hashmap__for_each_entry(dbe->sym_db_ids, entry, bkt) {
+			struct dso_symbol *key = (void *)entry->pkey;
+
+			dso_symbol__exit(key);
+			free(key);
+		}
+		hashmap__free(dbe->sym_db_ids);
+		dbe->sym_db_ids = NULL;
+	}
+	for (size_t i = 0; i < dbe->dsos_cnt; i++) {
+		dso__set_db_id(dbe->dsos[i], 0);
+		dso__put(dbe->dsos[i]);
+	}
+	zfree(&dbe->dsos);
+	dbe->dsos_cnt = 0;
+	dbe->dsos_alloc = 0;
 	call_return_processor__free(dbe->crp);
 	dbe->crp = NULL;
 }
@@ -148,6 +193,18 @@ int db_export__dso(struct db_export *dbe, struct dso *dso,
 	if (dso__db_id(dso))
 		return 0;
 
+	if (dbe->dsos_cnt == dbe->dsos_alloc) {
+		size_t new_alloc = dbe->dsos_alloc ? dbe->dsos_alloc * 2 : 16;
+		struct dso **new_dsos = realloc(dbe->dsos,
+						new_alloc * sizeof(*new_dsos));
+
+		if (!new_dsos)
+			return -ENOMEM;
+		dbe->dsos = new_dsos;
+		dbe->dsos_alloc = new_alloc;
+	}
+
+	dbe->dsos[dbe->dsos_cnt++] = dso__get(dso);
 	dso__set_db_id(dso, ++dbe->dso_last_db_id);
 
 	if (dbe->export_dso)
@@ -156,15 +213,60 @@ int db_export__dso(struct db_export *dbe, struct dso *dso,
 	return 0;
 }
 
+/*
+ * Return the database export ID assigned to (@dso, @sym) in @dbe->sym_db_ids,
+ * or 0 if @sym has not been exported yet.
+ */
+u64 db_export__symbol_db_id(struct db_export *dbe, struct dso *dso,
+			    struct symbol *sym)
+{
+	struct dso_symbol key = {
+		.dso = dso,
+		.sym = sym,
+	};
+	long db_id = 0;
+
+	if (sym && dbe->sym_db_ids)
+		hashmap__find(dbe->sym_db_ids, &key, &db_id);
+	return (u64)db_id;
+}
+
 int db_export__symbol(struct db_export *dbe, struct symbol *sym,
 		      struct dso *dso)
 {
-	u64 *sym_db_id = symbol__priv(sym);
+	struct dso_symbol src = {
+		.dso = dso,
+		.sym = sym,
+	};
+	struct dso_symbol *key;
+	long db_id;
 
-	if (*sym_db_id)
+	if (db_export__symbol_db_id(dbe, dso, sym))
 		return 0;
 
-	*sym_db_id = ++dbe->symbol_last_db_id;
+	if (!dbe->sym_db_ids) {
+		dbe->sym_db_ids =
+			hashmap__new(dso_sym_hash, dso_sym_equal, NULL);
+		if (IS_ERR(dbe->sym_db_ids)) {
+			int err = PTR_ERR(dbe->sym_db_ids);
+
+			dbe->sym_db_ids = NULL;
+			return err;
+		}
+	}
+
+	key = malloc(sizeof(*key));
+	if (!key)
+		return -ENOMEM;
+
+	dso_symbol__copy(key, &src);
+	db_id = dbe->symbol_last_db_id + 1;
+	if (hashmap__add(dbe->sym_db_ids, key, db_id)) {
+		dso_symbol__exit(key);
+		free(key);
+		return -ENOMEM;
+	}
+	dbe->symbol_last_db_id = db_id;
 
 	if (dbe->export_symbol)
 		return dbe->export_symbol(dbe, sym, dso);
@@ -192,12 +294,10 @@ static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
 		}
 
 		if (al->sym) {
-			u64 *db_id = symbol__priv(al->sym);
-
 			err = db_export__symbol(dbe, al->sym, dso);
 			if (err)
 				return err;
-			*sym_db_id = *db_id;
+			*sym_db_id = db_export__symbol_db_id(dbe, dso, al->sym);
 			*offset = al->addr - al->sym->start;
 		}
 	}
tools/perf/util/db-export.h +16 -0
index 1abbfd398e3a..c56037644d62 100644
@@ -21,6 +21,8 @@ struct call_return_processor;
 struct call_path_root;
 struct call_path;
 struct call_return;
+struct hashmap;
+struct symbol;
 
 struct export_sample {
 	union perf_event	*event;
@@ -63,6 +65,18 @@ struct db_export {
 				     u64 th_in_id, u64 comm_in_id, int flags);
 	struct call_return_processor *crp;
 	struct call_path_root *cpr;
+	/**
+	 * @sym_db_ids: Hashmap mapping heap-allocated struct dso_symbol * keys
+	 * to their exported 64-bit database IDs, pinning each symbol's DSO.
+	 */
+	struct hashmap *sym_db_ids;
+	/**
+	 * @dsos: Exported DSOs retained so dso__db_id(dso) remains valid until
+	 * db_export__exit().
+	 */
+	struct dso **dsos;
+	size_t dsos_cnt;
+	size_t dsos_alloc;
 	u64 evsel_last_db_id;
 	u64 machine_last_db_id;
 	u64 thread_last_db_id;
@@ -90,6 +104,8 @@ int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
 			   struct thread *thread);
 int db_export__dso(struct db_export *dbe, struct dso *dso,
 		   struct machine *machine);
+u64 db_export__symbol_db_id(struct db_export *dbe, struct dso *dso,
+			    struct symbol *sym);
 int db_export__symbol(struct db_export *dbe, struct symbol *sym,
 		      struct dso *dso);
 int db_export__branch_type(struct db_export *dbe, u32 branch_type,
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#66 lore raw

Re: [PATCH v3 07/20] perf db-export: Store symbol database IDs in a hashmap

Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

Currently, db-export reserves sizeof(u64) bytes ahead of every allocated struct symbol via symbol_conf.priv_size and accesses the database ID at a negative offset via symbol__priv(). This adds 8 bytes to every loaded symbol across all DSOs even when only a subset of symbols are exported, and relies on negative-offset pointer arithmetic ahead of struct symbol.

Quoted 7 lines

Store exported symbol database IDs in an on-demand sym_db_ids hashmap in struct db_export keyed by struct dso_symbol * instead of using symbol__priv(), and retain exported struct dso references in db_export until db_export__exit() so exported DSO and symbol database IDs remain valid across the export session.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
author reviewer
#67 lore raw

PATCH v3 8/20 perf annotate: Move struct annotation to a hashmap and remove symbol_conf.priv_size

In reply to Ian Rogers

Previously, enabling annotation added sizeof(struct annotation) to symbol_conf.priv_size, bloating every loaded struct symbol in every DSO even though only a small fraction of symbols are ever annotated.

Move struct annotation storage to an on-demand hashmap in util/annotate.c keyed by struct symbol pointer, guarded by annotations_lock and tracked via a new SYMBOL_FLAG_ANNOTATED flag in sym->flags so unannotated symbols pay zero lookup or teardown overhead. If allocation fails in symbol__annotation(), return a static empty_annotation fallback so callers that do not check for NULL remain safe while mutating callers check for &empty_annotation.

With annotation, TUI map browser indices, and db-export symbol IDs no longer using negative-offset space ahead of struct symbol, remove symbol_conf.priv_size and symbol__priv().

---
 tools/perf/builtin-annotate.c   |   3 +
 tools/perf/tests/builtin-test.c |   1 -
 tools/perf/ui/browsers/hists.c  |  13 ++-
 tools/perf/util/annotate.c      | 136 +++++++++++++++++++++++++++++++-
 tools/perf/util/annotate.h      |   6 +-
 tools/perf/util/block-info.c    |   2 +-
 tools/perf/util/block-range.c   |   3 +-
 tools/perf/util/sort.c          |   5 +-
 tools/perf/util/symbol.c        |  36 ++++-----
 tools/perf/util/symbol.h        |  16 ++--
 tools/perf/util/symbol_conf.h   |   1 -
 11 files changed, 179 insertions(+), 43 deletions(-)
tools/perf/builtin-annotate.c +3 -0
index 331adbdbd635..d1bbee421ea6 100644
@@ -441,6 +441,9 @@ static void hists__find_annotations(struct hists *hists,
 				goto find_next;
 		}
 
+		if (!symbol__annotated(he->ms.sym))
+			goto find_next;
+
 		notes = symbol__annotation(he->ms.sym);
 		if (notes->src == NULL) {
 find_next:
tools/perf/tests/builtin-test.c +0 -1
index 86698b86657f..8dcd05c3e8ed 100644
@@ -2053,7 +2053,6 @@ int cmd_test(int argc, const char **argv)
 	if (dont_fork)
 		sequential = true;
 
-	symbol_conf.priv_size = sizeof(int);
 	symbol_conf.try_vmlinux_path = true;
 
 
tools/perf/ui/browsers/hists.c +10 -3
index 593e1fd5759d..a50e9b1dfe37 100644
@@ -796,7 +796,9 @@ static void hist_browser__show_callchain_entry(struct hist_browser *browser,
 {
 	int color, width;
 	char folded_sign = callchain_list__folded(chain);
-	bool show_annotated = browser->show_dso && chain->ms.sym && symbol__annotation(chain->ms.sym)->src;
+	bool show_annotated = browser->show_dso && chain->ms.sym &&
+			      symbol__annotated(chain->ms.sym) &&
+			      symbol__annotation(chain->ms.sym)->src;
 
 	color = HE_COLORSET_NORMAL;
 	width = browser->b.width - (offset + 2);
@@ -2486,6 +2488,9 @@ do_annotate(struct hist_browser *browser, struct popup_action *act)
 	    perf_env__lookup_objdump(browser->env, &annotate_opts.objdump_path))
 		return 0;
 
+	if (!symbol__annotated(act->ms.sym))
+		return 0;
+
 	notes = symbol__annotation(act->ms.sym);
 	if (!notes->src)
 		return 0;
@@ -2545,7 +2550,8 @@ add_annotate_opt(struct popup_action *act, char **optstr,
 	if (!ms->sym)
 		ms->sym = symbol__new_unresolved(addr, ms->map);
 
-	if (ms->sym == NULL || symbol__annotation(ms->sym)->src == NULL)
+	if (ms->sym == NULL || !symbol__annotated(ms->sym) ||
+	    symbol__annotation(ms->sym)->src == NULL)
 		return 0;
 
 	if (asprintf(optstr, "Annotate %s", ms->sym->name) < 0)
@@ -3204,7 +3210,8 @@ static int evsel__hists_browse(struct evsel *evsel, int nr_events, const char *h
 				do_annotate(browser, &hotkey_act);
 				map_symbol__exit(&hotkey_act.ms);
 			} else {
-				if (symbol__annotation(browser->selection->sym)->src == NULL) {
+				if (!symbol__annotated(browser->selection->sym) ||
+				    symbol__annotation(browser->selection->sym)->src == NULL) {
 					ui_browser__warning(&browser->b, delay_secs * 2,
 						"No samples for the \"%s\" symbol.\n\n"
 						"Probably appeared just in a callchain",
tools/perf/util/annotate.c +134 -2

180 lines not on this page · raw message

tools/perf/util/annotate.h +2 -4
index 1b47c1156b9a..d87de0f11a60 100644
@@ -449,10 +449,8 @@ static inline struct sym_hist *annotation__histogram(struct annotation *notes,
 struct sym_hist_entry *
 annotated_source__hist_entry(struct annotated_source *src, const struct evsel *evsel, u64 offset);
 
-static inline struct annotation *symbol__annotation(struct symbol *sym)
-{
-	return (void *)sym - symbol_conf.priv_size;
-}
+struct annotation *symbol__annotation(struct symbol *sym);
+void symbol__annotation_delete(struct symbol *sym);
 
 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample);
 
tools/perf/util/block-info.c +1 -1
index 1135e54f4c7f..572cfb569849 100644
@@ -134,7 +134,7 @@ int block_info__process_sym(struct hist_entry *he, struct block_hist *bh,
 	static struct addr_location al;
 	u64 cycles = 0;
 
-	if (!he->ms.map || !he->ms.sym)
+	if (!he->ms.map || !he->ms.sym || !symbol__annotated(he->ms.sym))
 		return 0;
 
 	memset(&al, 0, sizeof(al));
tools/perf/util/block-range.c +2 -1
index 280d695c6c58..2f2c641b01c3 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 #include "block-range.h"
 #include "annotate.h"
+#include "symbol.h"
 #include <assert.h>
 #include <stdlib.h>
 
@@ -323,7 +324,7 @@ double block_range__coverage(struct block_range *br)
 	}
 
 	sym = br->sym;
-	if (!sym)
+	if (!sym || !symbol__annotated(sym))
 		return -1;
 
 	branch = symbol__annotation(sym)->branch;
tools/perf/util/sort.c +3 -2
index f4c50f191141..8b455081442a 100644
@@ -793,14 +793,15 @@ static int hist_entry__sym_ipc_snprintf(struct hist_entry *he, char *bf,
 {
 
 	struct symbol *sym = he->ms.sym;
-	struct annotated_branch *branch;
+	struct annotated_branch *branch = NULL;
 	double ipc = 0.0, coverage = 0.0;
 	char tmp[64];
 
 	if (!sym)
 		return repsep_snprintf(bf, size, "%-*s", width, "-");
 
-	branch = symbol__annotation(sym)->branch;
+	if (symbol__annotated(sym))
+		branch = symbol__annotation(sym)->branch;
 
 	if (branch && branch->hit_cycles)
 		ipc = branch->hit_insn / ((double)branch->hit_cycles);
tools/perf/util/symbol.c +15 -21
index b61a574ce9a3..0deda26181e0 100644
@@ -319,19 +319,11 @@ void symbols__fixup_end(struct rb_root_cached *symbols, bool is_kallsyms)
 struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name)
 {
 	size_t namelen = strlen(name) + 1;
-	struct symbol *sym = calloc(1, (symbol_conf.priv_size +
-					sizeof(*sym) + namelen));
+	struct symbol *sym = calloc(1, sizeof(*sym) + namelen);
+
 	if (sym == NULL)
 		return NULL;
 
-	if (symbol_conf.priv_size) {
-		if (symbol_conf.init_annotation) {
-			struct annotation *notes = (void *)sym;
-			annotation__init(notes);
-		}
-		sym = ((void *)sym) + symbol_conf.priv_size;
-	}
-
 	sym->start   = start;
 	sym->end     = len ? start + len : start;
 	atomic_init(&sym->flags, (type << SYMBOL_FLAG_TYPE_SHIFT) |
@@ -347,14 +339,11 @@ struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *
 
 void symbol__delete(struct symbol *sym)
 {
-	if (symbol_conf.priv_size) {
-		if (symbol_conf.init_annotation) {
-			struct annotation *notes = symbol__annotation(sym);
-
-			annotation__exit(notes);
-		}
-	}
-	free(((void *)sym) - symbol_conf.priv_size);
+	if (!sym)
+		return;
+	if (symbol__annotated(sym))
+		symbol__annotation_delete(sym);
+	free(sym);
 }
 
 void symbol__set_ignore(struct symbol *sym, bool ignore)
@@ -389,6 +378,14 @@ void symbol__set_ifunc_alias(struct symbol *sym, bool ifunc_alias)
 		atomic_fetch_and(&sym->flags, ~SYMBOL_FLAG_IFUNC_ALIAS);
 }
 
+void symbol__set_annotated(struct symbol *sym, bool annotated)
+{
+	if (annotated)
+		atomic_fetch_or(&sym->flags, SYMBOL_FLAG_ANNOTATED);
+	else
+		atomic_fetch_and(&sym->flags, ~SYMBOL_FLAG_ANNOTATED);
+}
+
 static void symbol__set_idle(struct symbol *sym, bool idle)
 {
 	uint16_t old_flags = atomic_load_explicit(&sym->flags, memory_order_relaxed);
@@ -2479,7 +2476,6 @@ int symbol__annotation_init(void)
 		return -1;
 	}
 
-	symbol_conf.priv_size += sizeof(struct annotation);
 	symbol_conf.init_annotation = true;
 	return 0;
 }
@@ -2522,8 +2518,6 @@ int symbol__init(struct perf_env *env)
 	if (symbol_conf.initialized)
 		return 0;
 
-	symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
-
 	symbol__elf_init();
 
 	if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
tools/perf/util/symbol.h +9 -7
index 6347bd218ad5..a0ad62e452be 100644
@@ -78,9 +78,9 @@ enum symbol_idle_kind {
 #define SYMBOL_FLAG_INLINED         (1 << 11)
 #define SYMBOL_FLAG_ANNOTATE2       (1 << 12)
 #define SYMBOL_FLAG_IFUNC_ALIAS     (1 << 13)
+#define SYMBOL_FLAG_ANNOTATED       (1 << 14)
 /**
- * A symtab entry. When allocated this may be preceded by an annotation (see
- * symbol__annotation) and/or a browser_index (see symbol__browser_index).
+ * A symtab entry.
  */
 struct symbol {
 	struct rb_node	rb_node;
@@ -135,12 +135,19 @@ static inline bool symbol__ifunc_alias(const struct symbol *sym)
 		SYMBOL_FLAG_IFUNC_ALIAS) != 0;
 }
 
+static inline bool symbol__annotated(const struct symbol *sym)
+{
+	return (atomic_load_explicit(&sym->flags, memory_order_relaxed) &
+		SYMBOL_FLAG_ANNOTATED) != 0;
+}
+
 bool symbol__is_idle(struct symbol *sym, const struct dso *dso, struct perf_env *env);
 
 void symbol__set_ignore(struct symbol *sym, bool ignore);
 void symbol__set_annotate2(struct symbol *sym, bool annotate2);
 void symbol__set_inlined(struct symbol *sym, bool inlined);
 void symbol__set_ifunc_alias(struct symbol *sym, bool ifunc_alias);
+void symbol__set_annotated(struct symbol *sym, bool annotated);
 
 /* symbols__for_each_entry - iterate over symbols (rb_root)
  *
@@ -174,11 +181,6 @@ static inline int __symbol__join_symfs(char *bf, size_t size, const char *path)
 extern int vmlinux_path__nr_entries;
 extern char **vmlinux_path;
 
-static inline void *symbol__priv(struct symbol *sym)
-{
-	return ((void *)sym) - symbol_conf.priv_size;
-}
-
 struct ref_reloc_sym {
 	const char	*name;
 	u64		addr;
tools/perf/util/symbol_conf.h +0 -1
index 37d35f42dcc1..aaa7148c3ac1 100644
@@ -42,7 +42,6 @@ struct symbol_conf {
 	/* Was hybrid_merge given on the command line rather than in a config file? */
 	bool		hybrid_merge_set;
 	bool		nanosecs;
-	unsigned short	priv_size;
 	bool		try_vmlinux_path,
 			init_annotation,
 			force,
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#68 lore raw

Re: [PATCH v3 08/20] perf annotate: Move struct annotation to a hashmap and remove symbol_conf.priv_size

Sashiko has reviewed this patch and found no issues.
Quoted 12 lines

Previously, enabling annotation added sizeof(struct annotation) to symbol_conf.priv_size, bloating every loaded struct symbol in every DSO even though only a small fraction of symbols are ever annotated.

Move struct annotation storage to an on-demand hashmap in util/annotate.c keyed by struct symbol pointer, guarded by annotations_lock and tracked via a new SYMBOL_FLAG_ANNOTATED flag in sym->flags so unannotated symbols pay zero lookup or teardown overhead. If allocation fails in symbol__annotation(), return a static empty_annotation fallback so callers that do not check for NULL remain safe while mutating callers check for &empty_annotation.

Quoted 5 lines

With annotation, TUI map browser indices, and db-export symbol IDs no longer using negative-offset space ahead of struct symbol, remove symbol_conf.priv_size and symbol__priv().

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
author reviewer
#69 lore raw

PATCH v3 9/20 perf bench internals: Add symbols benchmark

In reply to Ian Rogers

Add a 'perf bench internals symbols' subcommand to measure symbol loading, sorting, and lookup operations directly:

1. Loads symbols from an ELF binary (defaulting to the running perf executable, or specified via -b/--binary) without sorting by name, reporting average load runtime and maximum RSS via getrusage().
2. Sorts the loaded DSO symbols by name (dso__sort_by_name()), reporting average sort runtime and maximum RSS separately so address-only/lazy symbol loading can be evaluated independently of symbol name sorting.
3. Inserts synthetic symbols in two ascending runs (simulating .symtab and .dynsym), fixes up symbol ends and duplicates, and queries each symbol by address.
4. Incrementally appends symbols at interior addresses and immediately queries each appended symbol by address.

Example output:

  $ perf bench internals symbols
  # Running 'internals/symbols' benchmark:
    Average DSO load: 9.174 msec (+- 0.220 msec)
    Max RSS (DSO load): 75640 KB
    Average DSO sort by name: 2.787 msec (+- 0.019 msec)
    Max RSS (with sort by name): 75640 KB
    Average bulk insert + lookup: 2030.050 usec (+- 13.376 usec)
    Average append + lookup: 54.650 usec (+- 1.466 usec)
---
 tools/perf/Documentation/perf-bench.txt |   3 +
 tools/perf/bench/Build                  |   1 +
 tools/perf/bench/bench.h                |   1 +
 tools/perf/bench/symbols.c              | 273 ++++++++++++++++++++++++
 tools/perf/builtin-bench.c              |   1 +
 5 files changed, 279 insertions(+)
 create mode 100644 tools/perf/bench/symbols.c
tools/perf/Documentation/perf-bench.txt +3 -0
index 7f820f69740f..54eb809540c4 100644
@@ -351,6 +351,9 @@ SUITES FOR 'internals'
 *synthesize*::
 Suite for evaluating perf's event synthesis performance.
 
+*symbols*::
+Suite for evaluating ELF symbol loading, sorting, and lookup performance.
+
 SEE ALSO
 --------
 linkperf:perf[1]
tools/perf/bench/Build +1 -0
index c64c52468d3d..c3c0319696ae 100644
@@ -20,6 +20,7 @@ perf-bench-y += evlist-open-close.o
 perf-bench-y += breakpoint.o
 perf-bench-y += pmu-scan.o
 perf-bench-y += uprobe.o
+perf-bench-y += symbols.o
 
 perf-bench-$(CONFIG_X86_64) += mem-memcpy-x86-64-asm.o
 perf-bench-$(CONFIG_X86_64) += mem-memset-x86-64-asm.o
tools/perf/bench/bench.h +1 -0
index 85e3ac471875..e0b35d45559a 100644
@@ -52,6 +52,7 @@ int bench_uprobe_trace_printk(int argc, const char **argv);
 int bench_uprobe_empty_ret(int argc, const char **argv);
 int bench_uprobe_trace_printk_ret(int argc, const char **argv);
 int bench_pmu_scan(int argc, const char **argv);
+int bench_symbols(int argc, const char **argv);
 
 #define BENCH_FORMAT_DEFAULT_STR	"default"
 #define BENCH_FORMAT_DEFAULT		0
tools/perf/bench/symbols.c new file +273 -0

273 lines not on this page · raw message

tools/perf/builtin-bench.c +1 -0
index 62bebdedd3b8..f937c08f3900 100644
@@ -97,6 +97,7 @@ static const struct bench internals_benchmarks[] = {
 	{ "inject-build-id", "Benchmark build-id injection",	bench_inject_build_id	},
 	{ "evlist-open-close", "Benchmark evlist open and close",	bench_evlist_open_close	},
 	{ "pmu-scan", "Benchmark sysfs PMU info scanning",	bench_pmu_scan		},
+	{ "symbols", "Benchmark symbol loading, sorting and lookup",	bench_symbols	},
 	{ NULL,		NULL,					NULL			}
 };
 
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#70 lore raw

Re: [PATCH v3 09/20] perf bench internals: Add symbols benchmark

Sashiko has reviewed this patch and found no issues.
Quoted 27 lines

Add a 'perf bench internals symbols' subcommand to measure symbol loading, sorting, and lookup operations directly:

1. Loads symbols from an ELF binary (defaulting to the running perf executable, or specified via -b/--binary) without sorting by name, reporting average load runtime and maximum RSS via getrusage().
2. Sorts the loaded DSO symbols by name (dso__sort_by_name()), reporting average sort runtime and maximum RSS separately so address-only/lazy symbol loading can be evaluated independently of symbol name sorting.
3. Inserts synthetic symbols in two ascending runs (simulating .symtab and .dynsym), fixes up symbol ends and duplicates, and queries each symbol by address.
4. Incrementally appends symbols at interior addresses and immediately queries each appended symbol by address.

Example output:

  $ perf bench internals symbols
  # Running 'internals/symbols' benchmark:
    Average DSO load: 9.174 msec (+- 0.220 msec)
    Max RSS (DSO load): 75640 KB
    Average DSO sort by name: 2.787 msec (+- 0.019 msec)
    Max RSS (with sort by name): 75640 KB
    Average bulk insert + lookup: 2030.050 usec (+- 13.376 usec)
    Average append + lookup: 54.650 usec (+- 1.466 usec)
Quoted 1 line

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
author reviewer
#71 lore raw

PATCH v3 10/20 perf bench internals: Add maps benchmark

In reply to Ian Rogers

Add a 'perf bench internals maps' subcommand to measure map insertion, sorting, and lookup operations directly:

1. Inserts non-overlapping maps in ascending address order (simulating /proc/<pid>/maps synthesis) and queries each map by address.
2. Incrementally appends maps at interior addresses (simulating runtime PERF_RECORD_MMAP2 / JIT events) and immediately queries each map by address and by name.

Example output:

  $ perf bench internals maps
  # Running 'internals/maps' benchmark:
    Average ascending insert + lookup: 145.870 usec (+- 14.628 usec)
    Average append + lookup: 17405.810 usec (+- 1773.195 usec)
---
 tools/perf/Documentation/perf-bench.txt |   3 +
 tools/perf/bench/Build                  |   1 +
 tools/perf/bench/bench.h                |   1 +
 tools/perf/bench/maps.c                 | 202 ++++++++++++++++++++++++
 tools/perf/builtin-bench.c              |   1 +
 5 files changed, 208 insertions(+)
 create mode 100644 tools/perf/bench/maps.c
tools/perf/Documentation/perf-bench.txt +3 -0
index 54eb809540c4..c65dd8f4131f 100644
@@ -354,6 +354,9 @@ Suite for evaluating perf's event synthesis performance.
 *symbols*::
 Suite for evaluating ELF symbol loading, sorting, and lookup performance.
 
+*maps*::
+Suite for evaluating maps insertion, sorting, and lookup performance.
+
 SEE ALSO
 --------
 linkperf:perf[1]
tools/perf/bench/Build +1 -0
index c3c0319696ae..d51e52d3742a 100644
@@ -21,6 +21,7 @@ perf-bench-y += breakpoint.o
 perf-bench-y += pmu-scan.o
 perf-bench-y += uprobe.o
 perf-bench-y += symbols.o
+perf-bench-y += maps.o
 
 perf-bench-$(CONFIG_X86_64) += mem-memcpy-x86-64-asm.o
 perf-bench-$(CONFIG_X86_64) += mem-memset-x86-64-asm.o
tools/perf/bench/bench.h +1 -0
index e0b35d45559a..c988d236e0f0 100644
@@ -53,6 +53,7 @@ int bench_uprobe_empty_ret(int argc, const char **argv);
 int bench_uprobe_trace_printk_ret(int argc, const char **argv);
 int bench_pmu_scan(int argc, const char **argv);
 int bench_symbols(int argc, const char **argv);
+int bench_maps(int argc, const char **argv);
 
 #define BENCH_FORMAT_DEFAULT_STR	"default"
 #define BENCH_FORMAT_DEFAULT		0
tools/perf/bench/maps.c new file +202 -0

202 lines not on this page · raw message

tools/perf/builtin-bench.c +1 -0
index f937c08f3900..57a4199a058c 100644
@@ -98,6 +98,7 @@ static const struct bench internals_benchmarks[] = {
 	{ "evlist-open-close", "Benchmark evlist open and close",	bench_evlist_open_close	},
 	{ "pmu-scan", "Benchmark sysfs PMU info scanning",	bench_pmu_scan		},
 	{ "symbols", "Benchmark symbol loading, sorting and lookup",	bench_symbols	},
+	{ "maps", "Benchmark maps insertion, sorting and lookup",	bench_maps	},
 	{ NULL,		NULL,					NULL			}
 };
 
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#72 lore raw

Re: [PATCH v3 10/20] perf bench internals: Add maps benchmark

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

Add a 'perf bench internals maps' subcommand to measure map insertion, sorting, and lookup operations directly:

1. Inserts non-overlapping maps in ascending address order (simulating /proc/<pid>/maps synthesis) and queries each map by address.
2. Incrementally appends maps at interior addresses (simulating runtime PERF_RECORD_MMAP2 / JIT events) and immediately queries each map by address and by name.

Quoted 8 lines

Example output:

  $ perf bench internals maps
  # Running 'internals/maps' benchmark:
    Average ascending insert + lookup: 145.870 usec (+- 14.628 usec)
    Average append + lookup: 17405.810 usec (+- 1773.195 usec)

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
author reviewer
#73 lore raw

PATCH v3 11/20 perf demangle: Add fast-path prefix checks for Rust and Java demanglers

In reply to Ian Rogers

During DSO symbol loading in dso__load_sym_internal(), dso__demangle_sym() is called for every ELF symbol. Both rust_demangle_demangle() and java_demangle_sym() previously performed string searches or buffer allocations before checking whether the symbol name could possibly be a mangled Rust or Java symbol, adding unnecessary overhead on plain C symbols.

Add fast-path prefix checks in dso__demangle_sym() before calling rust_demangle_demangle() (using strstarts() for '_ZN', 'ZN', '__ZN', '_R', 'R', or '__R', keeping demangle-rust-v0.c unmodified relative to upstream rustc-demangle) and in java_demangle_sym() (requiring 'L', ';', '(', and ')') to reject non-Rust and non-Java symbol names immediately.

Using 'perf bench internals symbols':

Before (perf binary):

    Average DSO load + sort: 7.348 msec (+- 0.069 msec)
  After (perf binary, -4.2%):
    Average DSO load + sort: 7.041 msec (+- 0.051 msec)

Before (libLLVM.so.21.1):

    Average DSO load + sort: 81.196 msec (+- 0.780 msec)
  After (libLLVM.so.21.1, -3.2%):
    Average DSO load + sort: 78.599 msec (+- 0.857 msec)
---
 tools/perf/util/demangle-java.c | 23 +++++++++++++++++++++--
 tools/perf/util/symbol.c        | 15 ++++++++++++++-
 2 files changed, 35 insertions(+), 3 deletions(-)
tools/perf/util/demangle-java.c +21 -2
index c3cb327ed562..5625c4de39eb 100644
@@ -158,14 +158,33 @@ char *
 java_demangle_sym(const char *str, int flags)
 {
 	char *buf, *ptr;
-	const char *p;
+	const char *l, *semi, *open, *p;
 	size_t len, l1 = 0;
 
 	if (!str)
 		return NULL;
 
+	/*
+	 * OpenJDK JIT symbols have the form:
+	 *   [prefix]L<class>;<method>(<args>)<return_type>
+	 * where the return type (such as 'I' for int or 'V' for void) is at the
+	 * end after ')'. In __demangle_java_sym(), the parser starts in
+	 * MODE_PREFIX and only reaches MODE_TYPE (required when ')' is reached)
+	 * via 'L' (MODE_CLASS) -> ';' (MODE_FUNC) -> '(' (MODE_TYPE). Verify
+	 * those delimiters appear in order before allocating the output buffer.
+	 */
+	l = strchr(str, 'L');
+	if (!l)
+		return NULL;
+	semi = strchr(l + 1, ';');
+	if (!semi)
+		return NULL;
+	open = strchr(semi + 1, '(');
+	if (!open)
+		return NULL;
+
 	/* find start of return type */
-	p = strrchr(str, ')');
+	p = strrchr(open + 1, ')');
 	if (!p)
 		return NULL;
 
tools/perf/util/symbol.c +14 -1
index 0deda26181e0..cea8b9db8cd3 100644
@@ -2732,7 +2732,20 @@ char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
 	if (!want_demangle((dso && dso__kernel(dso)) || kmodule))
 		return demangled;
 
-	rust_demangle_demangle(elf_name, &rust_demangle);
+	/*
+	 * Rust mangled symbols start with one of the prefixes checked by
+	 * rust_demangle_legacy_demangle() ("_ZN", Windows dbghelp "ZN", or
+	 * macOS "__ZN") or rust_demangle_v0_demangle() ("_R", Windows dbghelp
+	 * "R", or macOS "__R") in demangle-rust-v0.c. Check the prefix here so
+	 * we avoid calling rust_demangle_demangle() (which scans the entire
+	 * symbol name with strstr() and strlen() for ThinLTO ".llvm." suffixes)
+	 * on non-Rust symbols, while keeping demangle-rust-v0.c unmodified
+	 * relative to upstream rustc-demangle.
+	 */
+	if (strstarts(elf_name, "_ZN") || strstarts(elf_name, "ZN") ||
+	    strstarts(elf_name, "__ZN") || strstarts(elf_name, "_R") ||
+	    strstarts(elf_name, "R") || strstarts(elf_name, "__R"))
+		rust_demangle_demangle(elf_name, &rust_demangle);
 	if (rust_demangle_is_known(&rust_demangle)) {
 		/* A rust mangled name. */
 		if (rust_demangle.mangled_len == 0)
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#74 lore raw

Re: [PATCH v3 11/20] perf demangle: Add fast-path prefix checks for Rust and Java demanglers

Sashiko has reviewed this patch and found no issues.
Quoted 20 lines

During DSO symbol loading in dso__load_sym_internal(), dso__demangle_sym() is called for every ELF symbol. Both rust_demangle_demangle() and java_demangle_sym() previously performed string searches or buffer allocations before checking whether the symbol name could possibly be a mangled Rust or Java symbol, adding unnecessary overhead on plain C symbols.

Add fast-path prefix checks in dso__demangle_sym() before calling rust_demangle_demangle() (using strstarts() for '_ZN', 'ZN', '__ZN', '_R', 'R', or '__R', keeping demangle-rust-v0.c unmodified relative to upstream rustc-demangle) and in java_demangle_sym() (requiring 'L', ';', '(', and ')') to reject non-Rust and non-Java symbol names immediately.

Using 'perf bench internals symbols':

Before (perf binary):

    Average DSO load + sort: 7.348 msec (+- 0.069 msec)
  After (perf binary, -4.2%):
    Average DSO load + sort: 7.041 msec (+- 0.051 msec)
Quoted 6 lines

Before (libLLVM.so.21.1):

    Average DSO load + sort: 81.196 msec (+- 0.780 msec)
  After (libLLVM.so.21.1, -3.2%):
    Average DSO load + sort: 78.599 msec (+- 0.857 msec)

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
author reviewer
#75 lore raw

PATCH v3 12/20 perf symbol-elf: Cache ELF program/section headers and defer symbol name lookup

In reply to Ian Rogers

Profiling dso__load_sym_internal() on large binaries showed significant time spent in repeated libelf section and program header lookups (elf_section_by_idx() and elf_read_program_header()) for each symbol and in resolving elf_sym__name() before filtering out non-symbol and non-allocated entries.

Speed up ELF symbol table loading in dso__load_sym_internal():

- Pre-load PT_LOAD program headers into struct elf_phdr_cache and cache per-section metadata in a 64-entry direct-mapped table in dso__load_sym_internal() to avoid repeated libelf section and program header lookups during symbol iteration.
- Defer elf_sym__name() lookup until after non-allocated and non-symbol entries have been filtered out.

Using 'perf bench internals symbols':

Before (perf binary):

    Average DSO load + sort: 5.808 msec (+- 0.047 msec)
  After (perf binary, -8.3%):
    Average DSO load + sort: 5.324 msec (+- 0.045 msec)

Before (perf binary, --no-demangle):

    Average DSO load + sort: 3.948 msec (+- 0.064 msec)
  After (perf binary, --no-demangle, -13.7%):
    Average DSO load + sort: 3.408 msec (+- 0.034 msec)

Before (libLLVM.so.21.1, --no-demangle):

    Average DSO load + sort: 22.433 msec (+- 0.260 msec)
  After (libLLVM.so.21.1, --no-demangle, -14.8%):
    Average DSO load + sort: 19.122 msec (+- 0.226 msec)
---
 tools/perf/util/symbol-elf.c | 241 ++++++++++++++++++++++++++++-------
 1 file changed, 198 insertions(+), 43 deletions(-)
tools/perf/util/symbol-elf.c +198 -43
index 98b6d7c73d23..39d69c4a137f 100644
@@ -267,6 +267,118 @@ static int elf_read_program_header(Elf *elf, u64 vaddr, GElf_Phdr *phdr)
 	return -1;
 }
 
+struct elf_phdr_range {
+	u64 vaddr;
+	u64 end;
+	u64 adjust;
+	GElf_Phdr phdr;
+};
+
+struct elf_phdr_cache {
+	struct elf_phdr_range inline_ranges[16];
+	struct elf_phdr_range *ranges;
+	size_t nr_ranges;
+	size_t last_idx;
+};
+
+static void elf_phdr_cache__init(struct elf_phdr_cache *cache, Elf *elf)
+{
+	size_t i, phdrnum = 0, cnt = 0;
+
+	cache->ranges = cache->inline_ranges;
+	cache->nr_ranges = 0;
+	cache->last_idx = 0;
+
+	if (!elf || elf_getphdrnum(elf, &phdrnum) || phdrnum == 0)
+		return;
+
+	if (phdrnum > ARRAY_SIZE(cache->inline_ranges)) {
+		cache->ranges = calloc(phdrnum, sizeof(*cache->ranges));
+		if (!cache->ranges) {
+			cache->ranges = cache->inline_ranges;
+			phdrnum = ARRAY_SIZE(cache->inline_ranges);
+		}
+	}
+
+	for (i = 0; i < phdrnum; i++) {
+		GElf_Phdr phdr;
+		u64 sz;
+
+		if (gelf_getphdr(elf, i, &phdr) == NULL)
+			break;
+		if (phdr.p_type != PT_LOAD)
+			continue;
+		sz = max(phdr.p_memsz, phdr.p_filesz);
+		if (!sz)
+			continue;
+		cache->ranges[cnt].vaddr = phdr.p_vaddr;
+		cache->ranges[cnt].end = phdr.p_vaddr + sz;
+		cache->ranges[cnt].adjust = phdr.p_vaddr - phdr.p_offset;
+		cache->ranges[cnt].phdr = phdr;
+		cnt++;
+	}
+	cache->nr_ranges = cnt;
+}
+
+static void elf_phdr_cache__exit(struct elf_phdr_cache *cache)
+{
+	if (cache->ranges && cache->ranges != cache->inline_ranges)
+		free(cache->ranges);
+}
+
+static const struct elf_phdr_range *
+elf_phdr_cache__find(struct elf_phdr_cache *cache, u64 vaddr)
+{
+	if (cache->nr_ranges == 0)
+		return NULL;
+
+	if (cache->last_idx < cache->nr_ranges) {
+		const struct elf_phdr_range *r = &cache->ranges[cache->last_idx];
+
+		if (vaddr >= r->vaddr && vaddr < r->end)
+			return r;
+	}
+	for (size_t i = 0; i < cache->nr_ranges; i++) {
+		const struct elf_phdr_range *r = &cache->ranges[i];
+
+		if (vaddr >= r->vaddr && vaddr < r->end) {
+			cache->last_idx = i;
+			return r;
+		}
+	}
+	return NULL;
+}
+
+#define ELF_SEC_CACHE_SIZE 64
+
+struct elf_sec_cache_entry {
+	uint32_t shndx;
+	bool valid;
+	bool is_alloc;
+	bool is_label_sec;
+	bool has_phdr;
+	u64 phdr_vaddr;
+	u64 phdr_end;
+	const char *section_name;
+	GElf_Shdr shdr;
+	GElf_Phdr phdr;
+};
+
+static inline const GElf_Phdr *
+elf_sec_cache__phdr(const struct elf_sec_cache_entry *sce,
+		    struct elf_phdr_cache *phdr_cache,
+		    u64 vaddr)
+{
+	const struct elf_phdr_range *pr;
+
+	if (sce->has_phdr && vaddr >= sce->phdr_vaddr && vaddr < sce->phdr_end)
+		return &sce->phdr;
+	pr = elf_phdr_cache__find(phdr_cache, vaddr);
+	if (!pr)
+		return NULL;
+	return &pr->phdr;
+}
+
 struct rel_info {
 	u32		nr_entries;
 	u32		*sorted;
@@ -1538,6 +1650,8 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 	int nr = 0;
 	bool remap_kernel = false, adjust_kernel_syms = false;
 	u64 max_text_sh_offset = 0;
+	struct elf_phdr_cache phdr_cache = {};
+	struct elf_sec_cache_entry sec_cache[ELF_SEC_CACHE_SIZE];
 
 	if (kmap && !kmaps)
 		return -1;
@@ -1629,14 +1743,18 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 	if (kmodule && adjust_kernel_syms)
 		max_text_sh_offset = max_text_section(runtime_ss->elf, &runtime_ss->ehdr);
 
+	elf_phdr_cache__init(&phdr_cache, runtime_ss->elf);
+	memset(sec_cache, 0, sizeof(sec_cache));
+
 	curr_dso = dso__get(dso);
 	elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
 		struct symbol *f;
-		const char *elf_name = elf_sym__name(&sym, symstrs);
+		const char *elf_name = NULL;
 		char *demangled = NULL;
 		int is_label = elf_sym__is_label(&sym);
 		const char *section_name;
 		bool used_opd = false;
+		struct elf_sec_cache_entry *sce;
 
 		if (!is_label && !elf_sym__filter(&sym))
 			continue;
@@ -1647,6 +1765,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		 * output:
 		 */
 		if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
+			elf_name = elf_sym__name(&sym, symstrs);
 			if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
 			    && (elf_name[2] == '\0' || elf_name[2] == '.'))
 				continue;
@@ -1654,13 +1773,19 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 
 		/* Reject RISCV ELF "mapping symbols" */
 		if (ehdr.e_machine == EM_RISCV) {
+			if (!elf_name)
+				elf_name = elf_sym__name(&sym, symstrs);
 			if (elf_name[0] == '$' && strchr("dx", elf_name[1]))
 				continue;
 		}
 
 		/* Reject kernel mapping symbols for kernel DSOs only */
-		if (dso__kernel(dso) && is_ignored_kernel_symbol(elf_name))
-			continue;
+		if (dso__kernel(dso)) {
+			if (!elf_name)
+				elf_name = elf_sym__name(&sym, symstrs);
+			if (is_ignored_kernel_symbol(elf_name))
+				continue;
+		}
 
 		if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
 			u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
@@ -1683,16 +1808,56 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		if (sym.st_shndx == SHN_ABS)
 			continue;
 
-		sec = elf_getscn(syms_ss->elf, sym.st_shndx);
-		if (!sec) {
-			if (dynsym && ehdr.e_shnum &&
-			    sym.st_shndx < SHN_LORESERVE &&
-			    sym.st_shndx >= ehdr.e_shnum)
-				continue;
-			goto out_elf_end;
-		}
+		sce = &sec_cache[sym.st_shndx & (ELF_SEC_CACHE_SIZE - 1)];
+		if (!sce->valid || sce->shndx != sym.st_shndx) {
+			const struct elf_phdr_range *pr;
 
-		gelf_getshdr(sec, &shdr);
+			sec = elf_getscn(syms_ss->elf, sym.st_shndx);
+			if (!sec) {
+				if (dynsym && ehdr.e_shnum &&
+				    sym.st_shndx < SHN_LORESERVE &&
+				    sym.st_shndx >= ehdr.e_shnum)
+					continue;
+				goto out_elf_end;
+			}
+
+			gelf_getshdr(sec, &shdr);
+			secstrs = secstrs_sym;
+
+			/*
+			 * We have to fallback to runtime when syms' section
+			 * header has NOBITS set. NOBITS results in file offset
+			 * (sh_offset) not being incremented. So sh_offset used
+			 * below has different values for syms (invalid) and
+			 * runtime (valid).
+			 */
+			if (shdr.sh_type == SHT_NOBITS && (shdr.sh_flags & SHF_ALLOC)) {
+				sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
+				if (!sec)
+					goto out_elf_end;
+
+				gelf_getshdr(sec, &shdr);
+				secstrs = secstrs_run;
+			}
+
+			sce->shndx = sym.st_shndx;
+			sce->valid = true;
+			sce->is_alloc = (shdr.sh_flags & SHF_ALLOC) != 0;
+			if (sce->is_alloc) {
+				sce->shdr = shdr;
+				sce->section_name = elf_sec__name(&shdr, secstrs);
+				sce->is_label_sec = elf_sec__filter(&shdr, secstrs) != 0;
+				pr = elf_phdr_cache__find(&phdr_cache, shdr.sh_addr);
+				if (pr) {
+					sce->has_phdr = true;
+					sce->phdr_vaddr = pr->vaddr;
+					sce->phdr_end = pr->end;
+					sce->phdr = pr->phdr;
+				} else {
+					sce->has_phdr = false;
+				}
+			}
+		}
 
 		/*
 		 * If the attribute bit SHF_ALLOC is not set, the section
@@ -1702,30 +1867,13 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		 * the section aren't loaded to memory during process execution,
 		 * so skip them.
 		 */
-		if (!(shdr.sh_flags & SHF_ALLOC))
+		if (!sce->is_alloc)
 			continue;
 
-		secstrs = secstrs_sym;
-
-		/*
-		 * We have to fallback to runtime when syms' section header has
-		 * NOBITS set. NOBITS results in file offset (sh_offset) not
-		 * being incremented. So sh_offset used below has different
-		 * values for syms (invalid) and runtime (valid).
-		 */
-		if (shdr.sh_type == SHT_NOBITS) {
-			sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
-			if (!sec)
-				goto out_elf_end;
-
-			gelf_getshdr(sec, &shdr);
-			secstrs = secstrs_run;
-		}
-
-		if (is_label && !elf_sec__filter(&shdr, secstrs))
+		if (is_label && !sce->is_label_sec)
 			continue;
 
-		section_name = elf_sec__name(&shdr, secstrs);
+		section_name = sce->section_name;
 
 		/* On ARM, symbols for thumb functions have 1 added to
 		 * the symbol address as a flag - remove it */
@@ -1735,7 +1883,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 			--sym.st_value;
 
 		if (dso__kernel(dso)) {
-			if (dso__process_kernel_symbol(dso, map, &sym, &shdr,
+			if (dso__process_kernel_symbol(dso, map, &sym, &sce->shdr,
 						       kmaps, kmap, &curr_dso,
 						       section_name,
 						       adjust_kernel_syms,
@@ -1745,17 +1893,20 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 				goto out_elf_end;
 		} else if ((used_opd && runtime_ss->adjust_symbols) ||
 			   (!used_opd && syms_ss->adjust_symbols)) {
-			GElf_Phdr phdr;
+			const GElf_Phdr *phdr;
 
-			if (elf_read_program_header(runtime_ss->elf,
-						    (u64)sym.st_value, &phdr)) {
+			phdr = elf_sec_cache__phdr(sce, &phdr_cache,
+						   (u64)sym.st_value);
+			if (!phdr) {
 				pr_debug4("%s: failed to find program header for "
 					   "symbol: %s st_value: %#" PRIx64 "\n",
-					   __func__, elf_name, (u64)sym.st_value);
+					   __func__, elf_sym__name(&sym, symstrs),
+					   (u64)sym.st_value);
 				pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
 					"sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n",
-					__func__, (u64)sym.st_value, (u64)shdr.sh_addr,
-					(u64)shdr.sh_offset);
+					__func__, (u64)sym.st_value,
+					(u64)sce->shdr.sh_addr,
+					(u64)sce->shdr.sh_offset);
 				/*
 				 * Fail to find program header, let's rollback
 				 * to use shdr.sh_addr and shdr.sh_offset to
@@ -1764,16 +1915,18 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 				 * still need to handle java JIT symbols in this
 				 * case.
 				 */
-				sym.st_value -= shdr.sh_addr - shdr.sh_offset;
+				sym.st_value -= sce->shdr.sh_addr - sce->shdr.sh_offset;
 			} else {
 				pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
 					"p_vaddr: %#" PRIx64 " p_offset: %#" PRIx64 "\n",
-					__func__, (u64)sym.st_value, (u64)phdr.p_vaddr,
-					(u64)phdr.p_offset);
-				sym.st_value -= phdr.p_vaddr - phdr.p_offset;
+					__func__, (u64)sym.st_value, (u64)phdr->p_vaddr,
+					(u64)phdr->p_offset);
+				sym.st_value -= phdr->p_vaddr - phdr->p_offset;
 			}
 		}
 
+		if (!elf_name)
+			elf_name = elf_sym__name(&sym, symstrs);
 		demangled = dso__demangle_sym(dso, kmodule, elf_name);
 		if (demangled != NULL)
 			elf_name = demangled;
@@ -1791,9 +1944,11 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		nr++;
 	}
 	dso__put(curr_dso);
+	elf_phdr_cache__exit(&phdr_cache);
 	return nr;
 out_elf_end:
 	dso__put(curr_dso);
+	elf_phdr_cache__exit(&phdr_cache);
 	return -1;
 }
 
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#76 lore raw

Re: [PATCH v3 12/20] perf symbol-elf: Cache ELF program/section headers and defer symbol name lookup

Sashiko has reviewed this patch and found no issues.
Quoted 15 lines

Profiling dso__load_sym_internal() on large binaries showed significant time spent in repeated libelf section and program header lookups (elf_section_by_idx() and elf_read_program_header()) for each symbol and in resolving elf_sym__name() before filtering out non-symbol and non-allocated entries.

Speed up ELF symbol table loading in dso__load_sym_internal():

- Pre-load PT_LOAD program headers into struct elf_phdr_cache and cache per-section metadata in a 64-entry direct-mapped table in dso__load_sym_internal() to avoid repeated libelf section and program header lookups during symbol iteration.
- Defer elf_sym__name() lookup until after non-allocated and non-symbol entries have been filtered out.

Quoted 6 lines

[ ... ]

    Average DSO load + sort: 22.433 msec (+- 0.260 msec)
  After (libLLVM.so.21.1, --no-demangle, -14.8%):
    Average DSO load + sort: 19.122 msec (+- 0.226 msec)

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
author reviewer
#77 lore raw

PATCH v3 13/20 perf maps: Optimize tail insertion and small-append sorting

In reply to Ian Rogers

User-space map insertions (from /proc/pid/maps synthesis, runtime PERF_RECORD_MMAP2, and jitdump) and BPF ksymbol registrations all flow through maps__fixup_overlap_and_insert() -> first_ending_after(). Currently first_ending_after() has a fast-path check for the first map (maps_by_address[0]), but no fast-path check for the last map (maps_by_address[high]). Because /proc/pid/maps entries are synthesized in ascending address order, every synthesized mapping starts at or after maps_by_address[high]'s end, yet currently executes the full O(log N) binary search loop. Add a tail check in first_ending_after() so non-overlapping ascending insertions complete in O(1).

Additionally, track the sorted prefix lengths (nr_maps_by_address_sorted and nr_maps_by_name_sorted) in struct maps:
- In __maps__sort_by_address() and maps__sort_by_name(), when the unsorted tail is small (k <= 8) or the total map count is small (n <= 64), binary-insert the newly appended maps into the sorted prefix instead of re-running qsort() over the entire array. This avoids O(N^2 log N) re-sorting when maps__find_by_name() is called repeatedly between single-map maps__insert() calls in dso__process_kernel_symbol() and dso__split_kallsyms().
- In maps__by_address_index() and maps__by_name_index(), binary-search the known-sorted prefix before scanning only the unsorted tail.
- Add a unit test in tools/perf/tests/maps.c covering the tail fast-path and incremental binary-insertion sort.

Using 'perf bench internals maps':

Before:

    Average ascending insert + lookup: 63.370 usec (+- 0.476 usec)
    Average append + lookup: 7477.450 usec (+- 20.829 usec)
  After (-6.9% ascending, -98.7% append):
    Average ascending insert + lookup: 58.970 usec (+- 0.609 usec)
    Average append + lookup: 96.670 usec (+- 1.353 usec)
---
 tools/perf/tests/maps.c |  67 ++++++++++
 tools/perf/util/maps.c  | 281 +++++++++++++++++++++++++++++++++-------
 2 files changed, 298 insertions(+), 50 deletions(-)
tools/perf/tests/maps.c +67 -0
index ff6d25f9c043..04ab425f4a87 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <inttypes.h>
 #include <unistd.h>
+#include <internal/rc_check.h>
 #include <linux/compiler.h>
 #include <linux/kernel.h>
 #include "debug.h"
@@ -253,9 +254,75 @@ static int test__maps__fixup_overlap_and_insert(struct test_suite *t __maybe_unu
 	return TEST_OK;
 }
 
+static int test__maps__incremental_sort(struct test_suite *t __maybe_unused,
+					int subtest __maybe_unused)
+{
+	struct map_def ascending_maps[] = {
+		{ "map_c", 1000, 2000 },
+		{ "map_a", 2000, 3000 },
+		{ "map_d", 4000, 5000 },
+	};
+	struct map_def out_of_order = { "map_b", 3000, 4000 };
+	struct map *map, *found;
+	struct perf_env host_env;
+	struct machine *machine;
+	struct maps *maps;
+
+	perf_env__init(&host_env);
+	machine = machine__new_live(&host_env, /*kernel_maps=*/false, getpid());
+	maps = maps__new(machine);
+
+	TEST_ASSERT_VAL("failed to create machine", machine);
+	TEST_ASSERT_VAL("failed to create maps", maps);
+
+	/* Exercise tail fast-path in maps__fixup_overlap_and_insert. */
+	for (unsigned int i = 0; i < ARRAY_SIZE(ascending_maps); i++) {
+		map = dso__new_map(ascending_maps[i].name);
+		TEST_ASSERT_VAL("failed to create map", map);
+		map__set_start(map, ascending_maps[i].start);
+		map__set_end(map, ascending_maps[i].end);
+		TEST_ASSERT_VAL("failed to insert sorted map",
+				maps__fixup_overlap_and_insert(maps, map) == 0);
+		map__put(map);
+	}
+
+	/* Populate maps_by_name and verify lookup. */
+	found = maps__find_by_name(maps, "map_a");
+	TEST_ASSERT_VAL("failed to find map_a by name", found && map__start(found) == 2000);
+	map__zput(found);
+
+	/* Append out-of-order map via maps__insert and verify binary-insertion sort. */
+	map = dso__new_map(out_of_order.name);
+	TEST_ASSERT_VAL("failed to create map_b", map);
+	map__set_start(map, out_of_order.start);
+	map__set_end(map, out_of_order.end);
+	TEST_ASSERT_VAL("failed to insert map_b", maps__insert(maps, map) == 0);
+
+	found = maps__find(maps, 3500);
+	TEST_ASSERT_VAL("failed to find map_b by address", RC_CHK_EQUAL(found, map));
+	map__zput(found);
+
+	found = maps__find_by_name(maps, "map_b");
+	TEST_ASSERT_VAL("failed to find map_b by name", RC_CHK_EQUAL(found, map));
+	map__zput(found);
+
+	maps__remove(maps, map);
+	map__zput(map);
+
+	found = maps__find_by_name(maps, "map_d");
+	TEST_ASSERT_VAL("failed to find map_d after removal", found && map__start(found) == 4000);
+	map__zput(found);
+
+	maps__zput(maps);
+	machine__delete(machine);
+	perf_env__exit(&host_env);
+	return TEST_OK;
+}
+
 static struct test_case tests__maps[] = {
 	TEST_CASE("Test merge_in interface", maps__merge_in),
 	TEST_CASE("Test fix up overlap interface", maps__fixup_overlap_and_insert),
+	TEST_CASE("Test incremental sort and tail insert", maps__incremental_sort),
 	{	.name = NULL, }
 };
 
tools/perf/util/maps.c +231 -50

475 lines not on this page · raw message

Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#78 lore raw

Re: [PATCH v3 13/20] perf maps: Optimize tail insertion and small-append sorting

Sashiko has reviewed this patch and found no issues.
Quoted 17 lines

User-space map insertions (from /proc/pid/maps synthesis, runtime PERF_RECORD_MMAP2, and jitdump) and BPF ksymbol registrations all flow through maps__fixup_overlap_and_insert() -> first_ending_after(). Currently first_ending_after() has a fast-path check for the first map (maps_by_address[0]), but no fast-path check for the last map (maps_by_address[high]). Because /proc/pid/maps entries are synthesized in ascending address order, every synthesized mapping starts at or after maps_by_address[high]'s end, yet currently executes the full O(log N) binary search loop. Add a tail check in first_ending_after() so non-overlapping ascending insertions complete in O(1).

Additionally, track the sorted prefix lengths (nr_maps_by_address_sorted and nr_maps_by_name_sorted) in struct maps:
- In __maps__sort_by_address() and maps__sort_by_name(), when the unsorted tail is small (k <= 8) or the total map count is small
[ ... ]
After (-6.9% ascending, -98.7% append):

Quoted 4 lines
    Average ascending insert + lookup: 58.970 usec (+- 0.609 usec)
    Average append + lookup: 96.670 usec (+- 1.353 usec)

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
author reviewer
#79 lore raw

PATCH v3 14/20 perf symbol: Add accessor functions for struct symbol fields

In reply to Ian Rogers

Encapsulating struct symbol fields behind inline accessor functions decouples callers across tools/perf from struct symbol's internal layout as symbol storage and lazy loading evolve.

Add inline accessor functions for struct symbol fields (symbol__start, symbol__set_start, symbol__end, symbol__set_end, symbol__name, symbol__namelen, symbol__arch_sym, symbol__set_arch_sym) and convert direct member accesses across tools/perf.

---
 .../arch/powerpc/util/skip-callchain-idx.c    |   2 +-
 tools/perf/arch/powerpc/util/sym-handling.c   |   8 +-
 tools/perf/builtin-annotate.c                 |   4 +-
 tools/perf/builtin-diff.c                     |   4 +-
 tools/perf/builtin-kallsyms.c                 |   7 +-
 tools/perf/builtin-kmem.c                     |  29 ++---
 tools/perf/builtin-kwork.c                    |   4 +-
 tools/perf/builtin-lock.c                     |  10 +-
 tools/perf/builtin-mem.c                      |   2 +-
 tools/perf/builtin-sched.c                    |   6 +-
 tools/perf/builtin-script.c                   |  14 +--
 tools/perf/builtin-timechart.c                |   2 +-
 tools/perf/builtin-top.c                      |  22 ++--
 tools/perf/builtin-trace.c                    |  22 ++--
 tools/perf/tests/dlfilter-test.c              |   4 +-
 tools/perf/tests/dwarf-unwind.c               |   2 +-
 tools/perf/tests/hists_common.c               |   4 +-
 tools/perf/tests/hists_cumulate.c             |   4 +-
 tools/perf/tests/hists_output.c               |   2 +-
 tools/perf/tests/symbols.c                    |  12 +-
 tools/perf/tests/vmlinux-kallsyms.c           |  34 +++---
 tools/perf/ui/browsers/annotate.c             |  10 +-
 tools/perf/ui/browsers/hists.c                |  10 +-
 tools/perf/ui/browsers/map.c                  |   8 +-
 tools/perf/ui/gtk/annotate.c                  |   6 +-
 tools/perf/ui/stdio/hist.c                    |   8 +-
 .../perf/util/annotate-arch/annotate-arm64.c  |  12 +-
 .../util/annotate-arch/annotate-loongarch.c   |   4 +-
 tools/perf/util/annotate-arch/annotate-x86.c  |  14 +--
 tools/perf/util/annotate-data.c               |  12 +-
 tools/perf/util/annotate.c                    |  69 ++++++------
 tools/perf/util/auxtrace.c                    |  14 +--
 tools/perf/util/block-info.c                  |   6 +-
 tools/perf/util/bpf_lock_contention.c         |  16 +--
 tools/perf/util/c2c-function.c                |  19 ++--
 tools/perf/util/callchain.c                   |  19 ++--
 tools/perf/util/capstone.c                    |  20 ++--
 tools/perf/util/data-convert-json.c           |   4 +-
 tools/perf/util/db-export.c                   |   2 +-
 tools/perf/util/debug.c                       |   3 +-
 tools/perf/util/disasm.c                      |  49 +++++----
 tools/perf/util/dlfilter.c                    |  12 +-
 tools/perf/util/dso.c                         |   4 +-
 tools/perf/util/event.c                       |   4 +-
 tools/perf/util/evsel_fprintf.c               |   2 +-
 tools/perf/util/hist.c                        |  16 +--
 tools/perf/util/intel-pt.c                    |   8 +-
 tools/perf/util/libbfd.c                      |   4 +-
 tools/perf/util/llvm.c                        |   6 +-
 tools/perf/util/lock-contention.c             |   2 +-
 tools/perf/util/machine.c                     |  26 ++---
 tools/perf/util/map.c                         |   6 +-
 tools/perf/util/map.h                         |   2 +-
 tools/perf/util/probe-event.c                 |  41 +++----
 tools/perf/util/python.c                      |  16 +--
 tools/perf/util/sort.c                        |  40 +++----
 tools/perf/util/srcline.c                     |  16 +--
 tools/perf/util/symbol-elf.c                  |  10 +-
 tools/perf/util/symbol.c                      | 103 +++++++++---------
 tools/perf/util/symbol.h                      |  43 +++++++-
 tools/perf/util/symbol_fprintf.c              |  14 +--
 tools/perf/util/thread-stack.c                |   8 +-
 tools/perf/util/unwind-libdw.c                |   2 +-
 63 files changed, 481 insertions(+), 407 deletions(-)
tools/perf/arch/powerpc/util/skip-callchain-idx.c +1 -1
index 472714cfad38..94854f9fa440 100644
@@ -239,7 +239,7 @@ int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain)
 	rc = check_return_addr(dso, map__map_ip(al.map, ip));
 
 	pr_debug("[DSO %s, sym %s, ip 0x%" PRIx64 "] rc %d\n",
-		dso__long_name(dso), al.sym->name, ip, rc);
+		dso__long_name(dso), al.sym ? symbol__name(al.sym) : "unknown", ip, rc);
 
 	if (rc == 0) {
 		/*
tools/perf/arch/powerpc/util/sym-handling.c +4 -4
index 947bfad7aa59..d73bfdcdbec1 100644
@@ -13,7 +13,7 @@
 int arch__choose_best_symbol(struct symbol *syma,
 			     struct symbol *symb __maybe_unused)
 {
-	char *sym = syma->name;
+	const char *sym = symbol__name(syma);
 
 #if !defined(_CALL_ELF) || _CALL_ELF != 2
 	/* Skip over any initial dot */
@@ -69,7 +69,7 @@ const char *arch__normalize_symbol_name(const char *name)
 #ifdef HAVE_LIBELF_SUPPORT
 void arch__sym_update(struct symbol *s, GElf_Sym *sym)
 {
-	s->arch_sym = sym->st_other;
+	symbol__set_arch_sym(s, sym->st_other);
 }
 #endif
 
@@ -102,7 +102,7 @@ void arch__fix_tev_from_maps(struct perf_probe_event *pev,
 			return;
 	}
 
-	lep_offset = PPC64_LOCAL_ENTRY_OFFSET(sym->arch_sym);
+	lep_offset = PPC64_LOCAL_ENTRY_OFFSET(symbol__arch_sym(sym));
 
 	if (map__dso(map)->symtab_type == DSO_BINARY_TYPE__KALLSYMS)
 		tev->point.offset += PPC64LE_LEP_OFFSET;
@@ -131,7 +131,7 @@ void arch__post_process_probe_trace_events(struct perf_probe_event *pev,
 	for (i = 0; i < ntevs; i++) {
 		tev = &pev->tevs[i];
 		map__for_each_symbol(map, sym, tmp) {
-			if (map__unmap_ip(map, sym->start) == tev->point.address) {
+			if (map__unmap_ip(map, symbol__start(sym)) == tev->point.address) {
 				arch__fix_tev_from_maps(pev, tev, map, sym);
 				break;
 			}
tools/perf/builtin-annotate.c +2 -2
index d1bbee421ea6..89e4e8058890 100644
@@ -256,7 +256,7 @@ static int add_sample(struct perf_sample *sample,
 	if ((!ann->has_br_stack || !has_annotation(ann)) &&
 	    ann->sym_hist_filter != NULL &&
 	    (al->sym == NULL ||
-	     strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
+	     strcmp(ann->sym_hist_filter, symbol__name(al->sym)) != 0)) {
 		/* We're only interested in a symbol named sym_hist_filter */
 		/*
 		 * FIXME: why isn't this done in the symbol_filter when loading
@@ -427,7 +427,7 @@ static void hists__find_annotations(struct hists *hists,
 			goto find_next;
 
 		if (ann->sym_hist_filter &&
-		    (strcmp(he->ms.sym->name, ann->sym_hist_filter) != 0))
+		    (strcmp(symbol__name(he->ms.sym), ann->sym_hist_filter) != 0))
 			goto find_next;
 
 		if (ann->min_percent) {
tools/perf/builtin-diff.c +2 -2
index 9fa8e900637b..c263b848153b 100644
@@ -1358,10 +1358,10 @@ static int cycles_printf(struct hist_entry *he, struct hist_entry *pair,
 
 	bi = block_he->block_info;
 
-	start_line = map__srcline(he->ms.map, bi->sym->start + bi->start,
+	start_line = map__srcline(he->ms.map, symbol__start(bi->sym) + bi->start,
 				  he->ms.sym);
 
-	end_line = map__srcline(he->ms.map, bi->sym->start + bi->end,
+	end_line = map__srcline(he->ms.map, symbol__start(bi->sym) + bi->end,
 				he->ms.sym);
 
 	if (start_line != SRCLINE_UNKNOWN &&
tools/perf/builtin-kallsyms.c +4 -3
index 3c4339982b16..e99f61c3653e 100644
@@ -48,9 +48,10 @@ static int __cmd_kallsyms(int argc, const char **argv)
 
 		dso = map__dso(map);
 		printf("%s: %s %s %#" PRIx64 "-%#" PRIx64 " (%#" PRIx64 "-%#" PRIx64")\n",
-			symbol->name, dso__short_name(dso), dso__long_name(dso),
-			map__unmap_ip(map, symbol->start), map__unmap_ip(map, symbol->end),
-			symbol->start, symbol->end);
+			symbol__name(symbol), dso__short_name(dso), dso__long_name(dso),
+			map__unmap_ip(map, symbol__start(symbol)),
+			map__unmap_ip(map, symbol__end(symbol)),
+			symbol__start(symbol), symbol__end(symbol));
 	}
 out:
 	machine__delete(machine);
tools/perf/builtin-kmem.c +15 -14
index 8693c6b135ca..7af3fd9868a1 100644
@@ -310,7 +310,7 @@ static struct rb_root page_caller_sorted;
 struct alloc_func {
 	u64 start;
 	u64 end;
-	char *name;
+	const char *name;
 };
 
 static int nr_alloc_funcs;
@@ -368,7 +368,7 @@ static int build_alloc_func_list(void)
 	}
 
 	map__for_each_symbol(kernel_map, sym, node) {
-		if (regexec(&alloc_func_regex, sym->name, 0, NULL, 0))
+		if (regexec(&alloc_func_regex, symbol__name(sym), 0, NULL, 0))
 			continue;
 
 		func = realloc(alloc_func_list,
@@ -376,10 +376,10 @@ static int build_alloc_func_list(void)
 		if (func == NULL)
 			return -ENOMEM;
 
-		pr_debug("alloc func: %s\n", sym->name);
-		func[nr_alloc_funcs].start = sym->start;
-		func[nr_alloc_funcs].end   = sym->end;
-		func[nr_alloc_funcs].name  = sym->name;
+		pr_debug("alloc func: %s\n", symbol__name(sym));
+		func[nr_alloc_funcs].start = symbol__start(sym);
+		func[nr_alloc_funcs].end   = symbol__end(sym);
+		func[nr_alloc_funcs].name  = symbol__name(sym);
 
 		alloc_func_list = func;
 		nr_alloc_funcs++;
@@ -1048,11 +1048,12 @@ static void __print_slab_result(struct rb_root *root,
 		} else
 			addr = data->ptr;
 
-		if (sym != NULL)
-			snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name,
-				 addr - map__unmap_ip(map, sym->start));
-		else
+		if (sym != NULL) {
+			snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", symbol__name(sym),
+				 addr - map__unmap_ip(map, symbol__start(sym)));
+		} else {
 			snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
+		}
 		printf(" %-34s |", buf);
 
 		printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %9lu | %6.3f%%\n",
@@ -1105,12 +1106,12 @@ static void __print_page_alloc_result(struct perf_session *session, int n_lines)
 		struct symbol *sym;
 		struct map *map;
 		char buf[32];
-		char *caller = buf;
+		const char *caller = buf;
 
 		data = rb_entry(next, struct page_stat, node);
 		sym = machine__find_kernel_symbol(machine, data->callsite, &map);
 		if (sym)
-			caller = sym->name;
+			caller = symbol__name(sym);
 		else
 			scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
 
@@ -1147,12 +1148,12 @@ static void __print_page_caller_result(struct perf_session *session, int n_lines
 		struct symbol *sym;
 		struct map *map;
 		char buf[32];
-		char *caller = buf;
+		const char *caller = buf;
 
 		data = rb_entry(next, struct page_stat, node);
 		sym = machine__find_kernel_symbol(machine, data->callsite, &map);
 		if (sym)
-			caller = sym->name;
+			caller = symbol__name(sym);
 		else
 			scnprintf(buf, sizeof(buf), "%"PRIx64, data->callsite);
 
tools/perf/builtin-kwork.c +2 -2
index fce588441e49..98714f1ed949 100644
@@ -768,8 +768,8 @@ static void timehist_save_callchain(struct perf_kwork *kwork,
 
 		sym = node->ms.sym;
 		if (sym) {
-			if (!strcmp(sym->name, "__softirqentry_text_start") ||
-			    !strcmp(sym->name, "__do_softirq"))
+			if (!strcmp(symbol__name(sym), "__softirqentry_text_start") ||
+			    !strcmp(symbol__name(sym), "__do_softirq"))
 				symbol__set_ignore(sym, true);
 		}
 
tools/perf/builtin-lock.c +5 -5
index 778923c92f42..e52e3785b657 100644
@@ -826,12 +826,12 @@ static int get_symbol_name_offset(struct map *map, struct symbol *sym, u64 ip,
 		return 0;
 	}
 
-	offset = map__map_ip(map, ip) - sym->start;
+	offset = map__map_ip(map, ip) - symbol__start(sym);
 
 	if (offset)
-		return scnprintf(buf, size, "%s+%#lx", sym->name, offset);
+		return scnprintf(buf, size, "%s+%#lx", symbol__name(sym), offset);
 	else
-		return strlcpy(buf, sym->name, size);
+		return strlcpy(buf, symbol__name(sym), size);
 }
 static int lock_contention_caller(struct perf_sample *sample,
 				  char *buf, int size)
@@ -1006,7 +1006,7 @@ static int report_lock_contention_begin_event(struct perf_sample *sample)
 				return -ENOMEM;
 			}
 
-			addrs[filters.nr_addrs++] = map__unmap_ip(kmap, sym->start);
+			addrs[filters.nr_addrs++] = map__unmap_ip(kmap, symbol__start(sym));
 			filters.addrs = addrs;
 		}
 	}
@@ -1059,7 +1059,7 @@ static int report_lock_contention_begin_event(struct perf_sample *sample)
 		case LOCK_AGGR_ADDR:
 			sym = machine__find_kernel_symbol(machine, key, &kmap);
 			if (sym)
-				name = sym->name;
+				name = symbol__name(sym);
 			break;
 		case LOCK_AGGR_CALLER:
 			name = buf;
tools/perf/builtin-mem.c +1 -1
index ade53688ebcd..5a19be73348d 100644
@@ -249,7 +249,7 @@ dump_raw_samples(const struct perf_tool *tool,
 		sample->data_src,
 		symbol_conf.field_sep,
 		dso ? dso__long_name(dso) : "???",
-		al.sym ? al.sym->name : "???");
+		al.sym ? symbol__name(al.sym) : "???");
 out_put:
 	addr_location__exit(&al);
 	return 0;
tools/perf/builtin-sched.c +3 -3
index 39571f03eda2..fba0b9194eee 100644
@@ -2629,9 +2629,9 @@ static void save_task_callchain(struct perf_sched *sched,
 
 		sym = node->ms.sym;
 		if (sym) {
-			if (!strcmp(sym->name, "schedule") ||
-			    !strcmp(sym->name, "__schedule") ||
-			    !strcmp(sym->name, "preempt_schedule"))
+			if (!strcmp(symbol__name(sym), "schedule") ||
+			    !strcmp(symbol__name(sym), "__schedule") ||
+			    !strcmp(symbol__name(sym), "preempt_schedule"))
 				symbol__set_ignore(sym, true);
 		}
 
tools/perf/builtin-script.c +7 -7
index b6691ebb1b4e..7559d4d230f2 100644
@@ -1408,7 +1408,7 @@ static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
 	addr_location__init(&al);
 	thread__find_map(thread, cpumode, addr, &al);
 
-	if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
+	if ((*lastsym) && al.addr >= symbol__start((*lastsym)) && al.addr < symbol__end((*lastsym)))
 		goto out;
 
 	al.cpu = cpu;
@@ -1419,11 +1419,11 @@ static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
 	if (!al.sym)
 		goto out;
 
-	if (al.addr < al.sym->end)
-		off = al.addr - al.sym->start;
+	if (al.addr < symbol__end(al.sym))
+		off = al.addr - symbol__start(al.sym);
 	else
-		off = al.addr - map__start(al.map) - al.sym->start;
-	printed += fprintf(fp, "\t%s", al.sym->name);
+		off = al.addr - map__start(al.map) - symbol__start(al.sym);
+	printed += fprintf(fp, "\t%s", symbol__name(al.sym));
 	if (off)
 		printed += fprintf(fp, "%+d", off);
 	printed += fprintf(fp, ":");
@@ -1642,7 +1642,7 @@ static const char *resolve_branch_sym(struct perf_sample *sample,
 			if (!addr_al->thread)
 				thread__resolve(thread, addr_al, sample);
 			if (addr_al->sym)
-				name = addr_al->sym->name;
+				name = symbol__name(addr_al->sym);
 			else
 				*ip = sample->addr;
 		} else {
@@ -1650,7 +1650,7 @@ static const char *resolve_branch_sym(struct perf_sample *sample,
 		}
 	} else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
 		if (al->sym)
-			name = al->sym->name;
+			name = symbol__name(al->sym);
 		else
 			*ip = sample->ip;
 	}
tools/perf/builtin-timechart.c +1 -1
index 9eb335d9db16..52db3522aa77 100644
@@ -563,7 +563,7 @@ static char *cat_backtrace(struct perf_sample *sample,
 		addr_location__init(&tal);
 		tal.filtered = 0;
 		if (thread__find_symbol(al.thread, cpumode, ip, &tal))
-			fprintf(f, "..... %016" PRIx64 " %s\n", ip, tal.sym->name);
+			fprintf(f, "..... %016" PRIx64 " %s\n", ip, symbol__name(tal.sym));
 		else
 			fprintf(f, "..... %016" PRIx64 "\n", ip);
 
tools/perf/builtin-top.c +11 -11
index a3442d2d6edf..d85a49d546eb 100644
@@ -133,8 +133,8 @@ static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)
 	 * We can't annotate with just /proc/kallsyms
 	 */
 	if (dso__symtab_type(dso) == DSO_BINARY_TYPE__KALLSYMS && !dso__is_kcore(dso)) {
-		pr_err("Can't annotate %s: No vmlinux file was found in the "
-		       "path\n", sym->name);
+		pr_err("Can't annotate %s: No vmlinux file was found in the path\n",
+		       symbol__name(sym));
 		sleep(1);
 		return -1;
 	}
@@ -145,7 +145,7 @@ static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)
 	if (!symbol__hists(sym, evlist__nr_entries(top->evlist))) {
 		annotation__unlock(notes);
 		pr_err("Not enough memory for annotating '%s' symbol!\n",
-		       sym->name);
+		       symbol__name(sym));
 		sleep(1);
 		return err;
 	}
@@ -156,7 +156,7 @@ static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)
 	} else {
 		char msg[BUFSIZ];
 		symbol__strerror_disassemble(&he->ms, err, msg, sizeof(msg));
-		pr_err("Couldn't annotate %s: %s\n", sym->name, msg);
+		pr_err("Couldn't annotate %s: %s\n", symbol__name(sym), msg);
 	}
 
 	annotation__unlock(notes);
@@ -186,9 +186,9 @@ static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip)
 		    "Not all samples will be on the annotation output.\n\n"
 		    "Please report to [email protected]\n",
 		    ip, dso__long_name(dso), dso__symtab_origin(dso),
-		    map__start(map), map__end(map), sym->start, sym->end,
+		    map__start(map), map__end(map), symbol__start(sym), symbol__end(sym),
 		    symbol__binding(sym) == STB_GLOBAL ? 'g' :
-		    symbol__binding(sym) == STB_LOCAL  ? 'l' : 'w', sym->name,
+		    symbol__binding(sym) == STB_LOCAL  ? 'l' : 'w', symbol__name(sym),
 		    err ? "[unknown]" : uts.machine,
 		    err ? "[unknown]" : uts.release, perf_version_string);
 	if (use_browser <= 0)
@@ -232,7 +232,7 @@ static void perf_top__record_precise_ip(struct perf_top *top,
 			ui__warn_map_erange(he->ms.map, sym, ip);
 		else if (err == -ENOMEM) {
 			pr_err("Not enough memory for annotating '%s' symbol!\n",
-			       sym->name);
+			       symbol__name(sym));
 			sleep(1);
 		}
 
@@ -263,7 +263,7 @@ static void perf_top__show_details(struct perf_top *top)
 	if (notes->src == NULL)
 		goto out_unlock;
 
-	printf("Showing %s for %s\n", evsel__name(top->sym_evsel), symbol->name);
+	printf("Showing %s for %s\n", evsel__name(top->sym_evsel), symbol__name(symbol));
 	printf("  Events  Pcnt (>=%d%%)\n", annotate_opts.min_pcnt);
 
 	more = hist_entry__annotate_printf(he, top->sym_evsel);
@@ -418,7 +418,7 @@ static void perf_top__prompt_symbol(struct perf_top *top, const char *msg)
 	next = rb_first_cached(&hists->entries);
 	while (next) {
 		n = rb_entry(next, struct hist_entry, rb_node);
-		if (n->ms.sym && !strcmp(buf, n->ms.sym->name)) {
+		if (n->ms.sym && !strcmp(buf, symbol__name(n->ms.sym))) {
 			found = n;
 			break;
 		}
@@ -437,11 +437,11 @@ static void perf_top__prompt_symbol(struct perf_top *top, const char *msg)
 
 static void perf_top__print_mapped_keys(struct perf_top *top)
 {
-	char *name = NULL;
+	const char *name = NULL;
 
 	if (top->sym_filter_entry) {
 		struct symbol *sym = top->sym_filter_entry->ms.sym;
-		name = sym->name;
+		name = symbol__name(sym);
 	}
 
 	fprintf(stdout, "\nMapped keys:\n");
tools/perf/builtin-trace.c +12 -10
index 4094141461f9..7683b362783a 100644
@@ -789,15 +789,16 @@ size_t syscall_arg__scnprintf_ksym(char *bf, size_t size, struct syscall_arg *ar
 								 arg->val, &map);
 
 		if (sym) {
-			u64 start = map__unmap_ip(map, sym->start);
+			u64 start = map__unmap_ip(map, symbol__start(sym));
 			u64 offset = arg->val - start;
 			size_t printed;
 
-			if (offset == 0)
-				printed = scnprintf(bf, size, "%s", sym->name);
-			else
+			if (offset == 0) {
+				printed = scnprintf(bf, size, "%s", symbol__name(sym));
+			} else {
 				printed = scnprintf(bf, size, "%s+0x%" PRIx64,
-						    sym->name, offset);
+						    symbol__name(sym), offset);
+			}
 			map__put(map);
 			return printed;
 		}
@@ -3669,13 +3670,14 @@ static void print_location(FILE *f, struct perf_sample *sample,
 	if ((verbose > 0 || print_dso) && al->map)
 		fprintf(f, "%s@", dso__long_name(map__dso(al->map)));
 
-	if ((verbose > 0 || print_sym) && al->sym)
-		fprintf(f, "%s+0x%" PRIx64, al->sym->name,
-			al->addr - al->sym->start);
-	else if (al->map)
+	if ((verbose > 0 || print_sym) && al->sym) {
+		fprintf(f, "%s+0x%" PRIx64, symbol__name(al->sym),
+			al->addr - symbol__start(al->sym));
+	} else if (al->map) {
 		fprintf(f, "0x%" PRIx64, al->addr);
-	else
+	} else {
 		fprintf(f, "0x%" PRIx64, sample->addr);
+	}
 }
 
 static int trace__pgfault(struct trace *trace,
tools/perf/tests/dlfilter-test.c +2 -2
index 204663571943..b4649c79c309 100644
@@ -274,11 +274,11 @@ static int get_ip_addr(struct test_data *td)
 
 	sym = map__find_symbol_by_name(map, "foo");
 	if (sym)
-		td->foo = sym->start;
+		td->foo = symbol__start(sym);
 
 	sym = map__find_symbol_by_name(map, "bar");
 	if (sym)
-		td->bar = sym->start;
+		td->bar = symbol__start(sym);
 
 	map__put(map);
 
tools/perf/tests/dwarf-unwind.c +1 -1
index 9ed78d00fb87..b8b0c430a6b3 100644
@@ -56,7 +56,7 @@ int test__dwarf_unwind(struct test_suite *test, int subtest);
 static int unwind_entry(struct unwind_entry *entry, void *arg)
 {
 	unsigned long *cnt = (unsigned long *) arg;
-	char *symbol = entry->ms.sym ? entry->ms.sym->name : NULL;
+	const char *symbol = entry->ms.sym ? symbol__name(entry->ms.sym) : NULL;
 	static const char *funcs[MAX_STACK] = {
 		"test__arch_unwind_sample",
 		"test_dwarf_unwind__thread",
tools/perf/tests/hists_common.c +2 -2
index 187f12f5bc21..0db3ea9cf62e 100644
@@ -184,7 +184,7 @@ void print_hists_in(struct hists *hists)
 			pr_info("%2d: entry: %-8s [%-8s] %20s: period = %"PRIu64"\n",
 				i, thread__comm_str(he->thread),
 				dso__short_name(dso),
-				he->ms.sym->name, he->stat.period);
+				symbol__name(he->ms.sym), he->stat.period);
 		}
 
 		i++;
@@ -213,7 +213,7 @@ void print_hists_out(struct hists *hists)
 			pr_info("%2d: entry: %8s:%5d [%-8s] %20s: period = %"PRIu64"/%"PRIu64"\n",
 				i, thread__comm_str(he->thread), thread__tid(he->thread),
 				dso__short_name(dso),
-				he->ms.sym->name, he->stat.period,
+				symbol__name(he->ms.sym), he->stat.period,
 				he->stat_acc ? he->stat_acc->period : 0);
 		}
 
tools/perf/tests/hists_cumulate.c +2 -2
index c4a11a3255bc..95b9a629e59e 100644
@@ -164,11 +164,11 @@ typedef int (*test_fn_t)(struct evsel *, struct machine *);
 
 #define COMM(he)  (thread__comm_str(he->thread))
 #define DSO(he)   (dso__short_name(map__dso(he->ms.map)))
-#define SYM(he)   (he->ms.sym->name)
+#define SYM(he)   (symbol__name(he->ms.sym))
 #define CPU(he)   (he->cpu)
 #define DEPTH(he) (he->callchain->max_depth)
 #define CDSO(cl)  (dso__short_name(map__dso(cl->ms.map)))
-#define CSYM(cl)  (cl->ms.sym->name)
+#define CSYM(cl)  (symbol__name(cl->ms.sym))
 
 struct result {
 	u64 children;
tools/perf/tests/hists_output.c +1 -1
index 35d230a83e43..77fb3a187139 100644
@@ -129,7 +129,7 @@ typedef int (*test_fn_t)(struct evsel *, struct machine *);
 
 #define COMM(he)  (thread__comm_str(he->thread))
 #define DSO(he)   (dso__short_name(map__dso(he->ms.map)))
-#define SYM(he)   (he->ms.sym->name)
+#define SYM(he)   (symbol__name(he->ms.sym))
 #define CPU(he)   (he->cpu)
 #define PID(he)   (thread__tid(he->thread))
 
tools/perf/tests/symbols.c +6 -6
index d3180db45ee6..f6c0f7bdef43 100644
@@ -133,14 +133,14 @@ static int test_dso(struct dso *dso)
 			continue;
 
 		/* Check for overlapping function symbols */
-		if (last_sym && sym->start < last_sym->end) {
+		if (last_sym && symbol__start(sym) < symbol__end(last_sym)) {
 			pr_debug("Overlapping symbols:\n");
 			symbol__fprintf(last_sym, stderr);
 			symbol__fprintf(sym, stderr);
 			ret = TEST_FAIL;
 		}
 		/* Check for zero-length function symbol */
-		if (sym->start == sym->end) {
+		if (symbol__start(sym) == symbol__end(sym)) {
 			pr_debug("Zero-length symbol:\n");
 			symbol__fprintf(sym, stderr);
 			ret = TEST_FAIL;
@@ -253,14 +253,14 @@ static int check_symbol(struct dso *dso, u64 addr, const char *name, u64 end)
 {
 	struct symbol *sym = dso__find_symbol_nocache(dso, addr);
 
-	if (!sym || strcmp(sym->name, name)) {
+	if (!sym || strcmp(symbol__name(sym), name)) {
 		pr_debug("%#" PRIx64 ": expected %s, got %s\n", addr, name,
-			 sym ? sym->name : "no symbol");
+			 sym ? symbol__name(sym) : "no symbol");
 		return TEST_FAIL;
 	}
-	if (sym->end != end) {
+	if (symbol__end(sym) != end) {
 		pr_debug("%s: expected end %#" PRIx64 ", got %#" PRIx64 "\n",
-			 name, end, sym->end);
+			 name, end, symbol__end(sym));
 		return TEST_FAIL;
 	}
 	return TEST_OK;
tools/perf/tests/vmlinux-kallsyms.c +19 -15
index 9396c8a77c86..9b6f2d836d8f 100644
@@ -292,18 +292,19 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused
 
 		sym  = rb_entry(nd, struct symbol, rb_node);
 
-		if (sym->start == sym->end)
+		if (symbol__start(sym) == symbol__end(sym))
 			continue;
 
-		mem_start = map__unmap_ip(args.vmlinux_map, sym->start);
-		mem_end = map__unmap_ip(args.vmlinux_map, sym->end);
+		mem_start = map__unmap_ip(args.vmlinux_map, symbol__start(sym));
+		mem_end = map__unmap_ip(args.vmlinux_map, symbol__end(sym));
 
 		first_pair = machine__find_kernel_symbol(&args.kallsyms, mem_start, NULL);
 		pair = first_pair;
 
-		if (pair && UM(pair->start) == mem_start) {
+		if (pair && UM(symbol__start(pair)) == mem_start) {
 next_pair:
-			if (arch__compare_symbol_names(sym->name, pair->name) == 0) {
+			if (arch__compare_symbol_names(symbol__name(sym),
+						       symbol__name(pair)) == 0) {
 				/*
 				 * kallsyms don't have the symbol end, so we
 				 * set that by using the next symbol start - 1,
@@ -313,11 +314,13 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused
 				 * off the real size. More than that and we
 				 * _really_ have a problem.
 				 */
-				s64 skew = mem_end - UM(pair->end);
-				if (llabs(skew) >= page_size)
+				s64 skew = mem_end - UM(symbol__end(pair));
+
+				if (llabs(skew) >= page_size) {
 					pr_debug("WARN: %#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n",
-						 mem_start, sym->name, mem_end,
-						 UM(pair->end));
+						 mem_start, symbol__name(sym), mem_end,
+						 UM(symbol__end(pair)));
+				}
 
 				/*
 				 * Do not count this as a failure, because we
@@ -328,16 +331,17 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused
 				continue;
 			} else {
 				pair = machine__find_kernel_symbol_by_name(&args.kallsyms,
-									   sym->name, NULL);
+									   symbol__name(sym), NULL);
 				if (pair) {
-					if (UM(pair->start) == mem_start)
+					if (UM(symbol__start(pair)) == mem_start)
 						goto next_pair;
 
 					pr_debug("WARN: %#" PRIx64 ": diff name v: %s k: %s\n",
-						 mem_start, sym->name, pair->name);
+						 mem_start, symbol__name(sym), symbol__name(pair));
 				} else {
 					pr_debug("WARN: %#" PRIx64 ": diff name v: %s k: %s\n",
-						 mem_start, sym->name, first_pair->name);
+						 mem_start, symbol__name(sym),
+						 symbol__name(first_pair));
 				}
 
 				continue;
@@ -348,14 +352,14 @@ static int test__vmlinux_matches_kallsyms(struct test_suite *test __maybe_unused
 			 * such as __indirect_thunk_end.
 			 */
 			continue;
-		} else if (is_ignored_symbol(sym->name, symbol__type(sym))) {
+		} else if (is_ignored_symbol(symbol__name(sym), symbol__type(sym))) {
 			/*
 			 * Ignore hidden symbols, see scripts/kallsyms.c for the details
 			 */
 			continue;
 		} else {
 			pr_debug("ERR : %#" PRIx64 ": %s not on kallsyms\n",
-				 mem_start, sym->name);
+				 mem_start, symbol__name(sym));
 		}
 
 		err = -1;
tools/perf/ui/browsers/annotate.c +5 -5
index cf36b5347f72..66b7b42ea197 100644
@@ -196,7 +196,7 @@ static void annotate_browser__draw_current_jump(struct ui_browser *browser)
 	int diff = 0;
 
 	/* PLT symbols contain external offsets */
-	if (strstr(sym->name, "@plt"))
+	if (strstr(symbol__name(sym), "@plt"))
 		return;
 
 	if (!disasm_line__is_valid_local_jump(cursor, sym))
@@ -554,7 +554,7 @@ static void ui_browser__init_asm_mode(struct ui_browser *browser)
 static int sym_title(struct symbol *sym, struct map *map, char *title,
 		     size_t sz, int percent_type)
 {
-	return snprintf(title, sz, "%s  %s [Percent: %s] %s", sym->name,
+	return snprintf(title, sz, "%s  %s [Percent: %s] %s", symbol__name(sym),
 			dso__long_name(map__dso(map)),
 			percent_type_str(percent_type),
 			annotate_opts.code_with_type ? "[Type]" : "");
@@ -602,7 +602,7 @@ static bool annotate_browser__callq(struct annotate_browser *browser,
 	if (!symbol__hists(dl->ops.target.sym, evlist__nr_entries(evsel->evlist))) {
 		annotation__unlock(notes);
 		ui__warning("Not enough memory for annotating '%s' symbol!\n",
-			    dl->ops.target.sym->name);
+			    symbol__name(dl->ops.target.sym));
 		return true;
 	}
 
@@ -876,7 +876,7 @@ static void annotate_browser__symbol_annotate_error(struct annotate_browser *bro
 
 	dso__set_annotate_warned(dso);
 	symbol__strerror_disassemble(ms, err, msg, sizeof(msg));
-	ui__error("Couldn't annotate %s:\n%s", sym->name, msg);
+	ui__error("Couldn't annotate %s:\n%s", symbol__name(sym), msg);
 }
 
 static int annotate_browser__run(struct annotate_browser *browser,
@@ -1255,7 +1255,7 @@ int __hist_entry__tui_annotate(struct hist_entry *he, struct map_symbol *ms,
 	 */
 	if (al_addr != NO_ADDR) {
 		struct annotation_line *al = annotated_source__get_line(notes->src,
-			al_addr - sym->start);
+			al_addr - symbol__start(sym));
 
 		browser.selection = al;
 	}
tools/perf/ui/browsers/hists.c +5 -5
index a50e9b1dfe37..1f1a877f667e 100644
@@ -2554,7 +2554,7 @@ add_annotate_opt(struct popup_action *act, char **optstr,
 	    symbol__annotation(ms->sym)->src == NULL)
 		return 0;
 
-	if (asprintf(optstr, "Annotate %s", ms->sym->name) < 0)
+	if (asprintf(optstr, "Annotate %s", symbol__name(ms->sym)) < 0)
 		return 0;
 
 	map_symbol__copy(&act->ms, ms);
@@ -2754,7 +2754,7 @@ do_run_script(struct hist_browser *browser,
 	if (act->ms.thread)
 		len += strlen(thread__comm_str(act->ms.thread));
 	else if (act->ms.sym)
-		len += strlen(act->ms.sym->name);
+		len += strlen(symbol__name(act->ms.sym));
 	script_opt = malloc(len);
 	if (!script_opt)
 		return -1;
@@ -2765,7 +2765,7 @@ do_run_script(struct hist_browser *browser,
 			  thread__comm_str(act->ms.thread));
 	} else if (act->ms.sym) {
 		n = scnprintf(script_opt, len, " -S %s ",
-			  act->ms.sym->name);
+			  symbol__name(act->ms.sym));
 	}
 
 	if (act->time) {
@@ -2810,7 +2810,7 @@ add_script_opt_2(struct popup_action *act, char **optstr,
 			return 0;
 	} else if (sym) {
 		if (asprintf(optstr, "Run scripts for samples of symbol [%s]%s",
-			     sym->name, tstr) < 0)
+			     symbol__name(sym), tstr) < 0)
 			return 0;
 	} else {
 		if (asprintf(optstr, "Run scripts for all samples%s", tstr) < 0)
@@ -3215,7 +3215,7 @@ static int evsel__hists_browse(struct evsel *evsel, int nr_events, const char *h
 					ui_browser__warning(&browser->b, delay_secs * 2,
 						"No samples for the \"%s\" symbol.\n\n"
 						"Probably appeared just in a callchain",
-						browser->selection->sym->name);
+						symbol__name(browser->selection->sym));
 					continue;
 				}
 
tools/perf/ui/browsers/map.c +4 -4
index be7390156edd..2f070a53b5cb 100644
@@ -31,12 +31,12 @@ static void map_browser__write(struct ui_browser *browser, void *nd, int row)
 
 	ui_browser__set_percent_color(browser, 0, current_entry);
 	ui_browser__printf(browser, "%*" PRIx64 " %*" PRIx64 " %c ",
-			   mb->addrlen, sym->start, mb->addrlen, sym->end,
+			   mb->addrlen, symbol__start(sym), mb->addrlen, symbol__end(sym),
 			   symbol__binding(sym) == STB_GLOBAL ? 'g' :
 				symbol__binding(sym) == STB_LOCAL  ? 'l' : 'w');
 	width = browser->width - ((mb->addrlen * 2) + 4);
 	if (width > 0)
-		ui_browser__write_nstring(browser, sym->name, width);
+		ui_browser__write_nstring(browser, symbol__name(sym), width);
 }
 
 static int map_browser__search(struct map_browser *browser)
@@ -125,8 +125,8 @@ int map__browse(struct map *map)
 	for (nd = rb_first(mb.b.entries); nd; nd = rb_next(nd)) {
 		struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
 
-		if (maxaddr < pos->end)
-			maxaddr = pos->end;
+		if (maxaddr < symbol__end(pos))
+			maxaddr = symbol__end(pos);
 		++mb.b.nr_entries;
 	}
 
tools/perf/ui/gtk/annotate.c +3 -3
index 094dfa045b77..2307a7f609f8 100644
@@ -67,7 +67,7 @@ static int perf_gtk__get_percent(char *buf, size_t size, struct symbol *sym,
 static int perf_gtk__get_offset(char *buf, size_t size, struct map_symbol *ms,
 				struct disasm_line *dl)
 {
-	u64 start = map__rip_2objdump(ms->map, ms->sym->start);
+	u64 start = map__rip_2objdump(ms->map, symbol__start(ms->sym));
 
 	strcpy(buf, "");
 
@@ -192,7 +192,7 @@ static int symbol__gtk_annotate(struct map_symbol *ms, struct evsel *evsel,
 
 		dso__set_annotate_warned(dso);
 		symbol__strerror_disassemble(ms, err, msg, sizeof(msg));
-		ui__error("Couldn't annotate %s: %s\n", sym->name, msg);
+		ui__error("Couldn't annotate %s: %s\n", symbol__name(sym), msg);
 		return -1;
 	}
 
@@ -234,7 +234,7 @@ static int symbol__gtk_annotate(struct map_symbol *ms, struct evsel *evsel,
 	}
 
 	scrolled_window = gtk_scrolled_window_new();
-	tab_label = gtk_label_new(sym->name);
+	tab_label = gtk_label_new(symbol__name(sym));
 
 	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
 				       GTK_POLICY_AUTOMATIC,
tools/perf/ui/stdio/hist.c +5 -3
index 3240c39bd025..20439ac6b6ad 100644
@@ -100,13 +100,12 @@ static struct callchain_list rem_hits;
 
 static void init_rem_hits(void)
 {
-	rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
+	rem_sq_bracket = symbol__new(0, 0, 0, 0, "[...]");
 	if (!rem_sq_bracket) {
 		fprintf(stderr, "Not enough memory to display remaining hits\n");
 		return;
 	}
 
-	strcpy(rem_sq_bracket->name, "[...]");
 	rem_hits.ms.sym = rem_sq_bracket;
 }
 
@@ -909,7 +908,10 @@ size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
 
 	free(line);
 out:
-	zfree(&rem_sq_bracket);
+	if (rem_sq_bracket) {
+		symbol__delete(rem_sq_bracket);
+		rem_sq_bracket = NULL;
+	}
 
 	return ret;
 }
tools/perf/util/annotate-arch/annotate-arm64.c +6 -6
index 81369ae47290..5ad03c519a5d 100644
@@ -477,12 +477,12 @@ static void update_call_insn_state(struct type_state *state,
 	Dwarf_Die type_die;
 	u32 insn_offset = dl->al.offset;
 
-	call_name = func ? func->name : dl->ops.target.name;
+	call_name = func ? symbol__name(func) : dl->ops.target.name;
 	pr_debug_dtp("call [%x] %s\n", insn_offset, call_name ?: "<unknown>");
 
 	/* Invalidate caller-saved registers after call */
 	call_addr = map__rip_2objdump(dloc->ms->map,
-				      dloc->ms->sym->start + dl->al.offset);
+				      symbol__start(dloc->ms->sym) + dl->al.offset);
 	for (unsigned int i = 0; i < ARRAY_SIZE(state->regs); i++) {
 		struct type_state_reg *reg = &state->regs[i];
 
@@ -640,7 +640,7 @@ static int propagate_load_reg_state(struct type_state *state,
 	tsr->lifetime_end = 0;
 
 	if (dloc->fb_cfa) {
-		u64 ip = dloc->ms->sym->start + dl->al.offset;
+		u64 ip = symbol__start(dloc->ms->sym) + dl->al.offset;
 		u64 pc = map__rip_2objdump(dloc->ms->map, ip);
 
 		if (die_get_cfa(dloc->di->dbg, pc, &fbreg, &fboff) < 0)
@@ -727,7 +727,7 @@ static int propagate_load_reg_state(struct type_state *state,
 
 	/* Or check if it's a global variable */
 	if (src_tsr->kind == TSR_KIND_GLOBAL_ADDR) {
-		u64 ip = dloc->ms->sym->start + dl->al.offset;
+		u64 ip = symbol__start(dloc->ms->sym) + dl->al.offset;
 		u64 addr = src_tsr->imm_value + reg_offset;
 		int offset;
 		u8 kind;
@@ -875,7 +875,7 @@ static void propagate_store_reg_state(struct type_state *state,
 	reg_offset += mem_spacing;
 
 	if (dloc->fb_cfa) {
-		u64 ip = dloc->ms->sym->start + dl->al.offset;
+		u64 ip = symbol__start(dloc->ms->sym) + dl->al.offset;
 		u64 pc = map__rip_2objdump(dloc->ms->map, ip);
 
 		if (die_get_cfa(dloc->di->dbg, pc, &fbreg, &fboff) < 0)
@@ -1144,7 +1144,7 @@ static void update_mrs_insn_state(struct type_state *state,
 		return;
 	}
 
-	ip = dloc->ms->sym->start + dl->al.offset;
+	ip = symbol__start(dloc->ms->sym) + dl->al.offset;
 	pc = map__rip_2objdump(dloc->ms->map, ip);
 
 	/*
tools/perf/util/annotate-arch/annotate-loongarch.c +2 -2
index a949b4392dd4..d275730080b7 100644
@@ -94,8 +94,8 @@ static int loongarch_jump__parse(const struct arch *arch, struct ins_operands *o
 		ops->target.addr = strtoull(ops->raw, NULL, 16);
 
 	target.addr = map__objdump_2mem(map, ops->target.addr);
-	start = map__unmap_ip(map, sym->start);
-	end = map__unmap_ip(map, sym->end);
+	start = map__unmap_ip(map, symbol__start(sym));
+	end = map__unmap_ip(map, symbol__end(sym));
 
 	ops->target.outside = target.addr < start || target.addr >= end;
 
tools/perf/util/annotate-arch/annotate-x86.c +7 -7
index e38a4e7a54d5..47e9d2cf337d 100644
@@ -254,7 +254,7 @@ static void update_insn_state_x86(struct type_state *state,
 
 		/* Try to resolve the call target name */
 		if (func)
-			call_name = func->name;
+			call_name = symbol__name(func);
 		else
 			call_name = dl->ops.target.name;
 
@@ -289,7 +289,7 @@ static void update_insn_state_x86(struct type_state *state,
 
 		/* Invalidate caller-saved registers after call */
 		call_addr = map__rip_2objdump(dloc->ms->map,
-					      dloc->ms->sym->start + dl->al.offset);
+					      symbol__start(dloc->ms->sym) + dl->al.offset);
 		for (unsigned i = 0; i < ARRAY_SIZE(state->regs); i++) {
 			struct type_state_reg *reg = &state->regs[i];
 
@@ -324,7 +324,7 @@ static void update_insn_state_x86(struct type_state *state,
 		int offset;
 		const char *var_name = NULL;
 		struct map_symbol *ms = dloc->ms;
-		u64 ip = ms->sym->start + dl->al.offset;
+		u64 ip = symbol__start(ms->sym) + dl->al.offset;
 
 		if (!has_reg_type(state, dst->reg1))
 			return;
@@ -525,7 +525,7 @@ static void update_insn_state_x86(struct type_state *state,
 		return;
 
 	if (dloc->fb_cfa) {
-		u64 ip = dloc->ms->sym->start + dl->al.offset;
+		u64 ip = symbol__start(dloc->ms->sym) + dl->al.offset;
 		u64 pc = map__rip_2objdump(dloc->ms->map, ip);
 
 		if (die_get_cfa(dloc->di->dbg, pc, &fbreg, &fboff) < 0)
@@ -542,7 +542,7 @@ static void update_insn_state_x86(struct type_state *state,
 
 		if (dso__kernel(map__dso(dloc->ms->map)) &&
 		    src->segment == INSN_SEG_X86_GS && src->imm) {
-			u64 ip = dloc->ms->sym->start + dl->al.offset;
+			u64 ip = symbol__start(dloc->ms->sym) + dl->al.offset;
 			u64 var_addr;
 			int offset;
 
@@ -719,7 +719,7 @@ static void update_insn_state_x86(struct type_state *state,
 		/* Or check if it's a global variable */
 		else if (sreg == DWARF_REG_PC) {
 			struct map_symbol *ms = dloc->ms;
-			u64 ip = ms->sym->start + dl->al.offset;
+			u64 ip = symbol__start(ms->sym) + dl->al.offset;
 			u64 addr;
 			int offset;
 
@@ -744,7 +744,7 @@ static void update_insn_state_x86(struct type_state *state,
 		/* And check percpu access with base register */
 		else if (has_reg_type(state, sreg) &&
 			 state->regs[sreg].kind == TSR_KIND_PERCPU_BASE) {
-			u64 ip = dloc->ms->sym->start + dl->al.offset;
+			u64 ip = symbol__start(dloc->ms->sym) + dl->al.offset;
 			u64 var_addr = src->offset;
 			int offset;
 
tools/perf/util/annotate-data.c +6 -6
index dd9c1d7b2da8..ec9286646b70 100644
@@ -910,9 +910,9 @@ bool get_global_var_info(struct data_loc_info *dloc, u64 addr,
 	sym = thread__find_symbol_fb(dloc->thread, dloc->cpumode,
 				     mem_addr, &al);
 	if (sym) {
-		*var_name = sym->name;
+		*var_name = symbol__name(sym);
 		/* Calculate type offset from the start of variable */
-		*var_offset = mem_addr - map__unmap_ip(al.map, sym->start);
+		*var_offset = mem_addr - map__unmap_ip(al.map, symbol__start(sym));
 	} else {
 		*var_name = NULL;
 	}
@@ -1680,7 +1680,7 @@ static enum type_match_result find_data_type_insn(struct data_loc_info *dloc,
 			     bb->begin->al.offset, bb->end->al.offset);
 
 		list_for_each_entry_from(dl, &notes->src->source, al.node) {
-			u64 this_ip = sym->start + dl->al.offset;
+			u64 this_ip = symbol__start(sym) + dl->al.offset;
 			u64 addr = map__rip_2objdump(dloc->ms->map, this_ip);
 
 			/* Skip comment or debug info lines */
@@ -1760,8 +1760,8 @@ static enum type_match_result find_data_type_block(struct data_loc_info *dloc,
 			}
 
 			pr_debug_dtp("cannot find a basic block from %"PRIx64" to %"PRIx64"\n",
-				     src_ip - dloc->ms->sym->start,
-				     dst_ip - dloc->ms->sym->start);
+				     src_ip - symbol__start(dloc->ms->sym),
+				     dst_ip - symbol__start(dloc->ms->sym));
 			continue;
 		}
 		prepend_basic_blocks(&this_blocks, &basic_blocks);
@@ -1840,7 +1840,7 @@ static int find_data_type_die(struct data_loc_info *dloc, Dwarf_Die *type_die)
 	pr_debug_dtp("-----------------------------------------------------------\n");
 	pr_debug_dtp("find data type for %s%#x(%s) at %s+%#"PRIx64"\n",
 		     offset_sign, offset, buf,
-		     dloc->ms->sym->name, dloc->ip - dloc->ms->sym->start);
+		     symbol__name(dloc->ms->sym), dloc->ip - symbol__start(dloc->ms->sym));
 
 	/*
 	 * IP is a relative instruction address from the start of the map, as
tools/perf/util/annotate.c +37 -32

213 lines not on this page · raw message

tools/perf/util/auxtrace.c +7 -7
index 8bb491d389a2..b3cc7b17aca4 100644
@@ -2707,7 +2707,7 @@ static bool dso_sym_match(struct symbol *sym, const char *name, int *cnt,
 			  int idx)
 {
 	/* Same name, and global or the n'th found or any */
-	return !arch__compare_symbol_names(name, sym->name) &&
+	return !arch__compare_symbol_names(name, symbol__name(sym)) &&
 	       ((!idx && symbol__binding(sym) == STB_GLOBAL) ||
 		(idx > 0 && ++*cnt == idx) ||
 		idx < 0);
@@ -2725,14 +2725,14 @@ static void print_duplicate_syms(struct dso *dso, const char *sym_name)
 	while (sym) {
 		if (dso_sym_match(sym, sym_name, &cnt, -1)) {
 			pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
-			       ++cnt, sym->start,
+			       ++cnt, symbol__start(sym),
 			       symbol__binding(sym) == STB_GLOBAL ? 'g' :
 			       symbol__binding(sym) == STB_LOCAL  ? 'l' : 'w',
-			       sym->name);
+			       symbol__name(sym));
 			near = true;
 		} else if (near) {
 			near = false;
-			pr_err("\t\twhich is near\t\t%s\n", sym->name);
+			pr_err("\t\twhich is near\t\t%s\n", symbol__name(sym));
 		}
 		sym = dso__next_symbol(sym);
 	}
@@ -2755,7 +2755,7 @@ static int find_dso_sym(struct dso *dso, const char *sym_name, u64 *start,
 	while (sym) {
 		if (*start) {
 			if (!*size)
-				*size = sym->start - *start;
+				*size = symbol__start(sym) - *start;
 			if (idx > 0) {
 				if (*size)
 					return 0;
@@ -2764,8 +2764,8 @@ static int find_dso_sym(struct dso *dso, const char *sym_name, u64 *start,
 				return -EINVAL;
 			}
 		} else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
-			*start = sym->start;
-			*size = sym->end - sym->start;
+			*start = symbol__start(sym);
+			*size = symbol__end(sym) - symbol__start(sym);
 		}
 		sym = dso__next_symbol(sym);
 	}
tools/perf/util/block-info.c +3 -3
index 572cfb569849..8b65afab1017 100644
@@ -84,7 +84,7 @@ int64_t __block_info__cmp(struct hist_entry *left, struct hist_entry *right)
 			return 1;
 	}
 
-	cmp = strcmp(bi_l->sym->name, bi_r->sym->name);
+	cmp = strcmp(symbol__name(bi_l->sym), symbol__name(bi_r->sym));
 	if (cmp)
 		return cmp;
 
@@ -305,10 +305,10 @@ static int block_range_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
 
 	symbol_conf.addr2line_disable_warn = true;
 
-	start_line = map__srcline(he->ms.map, bi->sym->start + bi->start,
+	start_line = map__srcline(he->ms.map, symbol__start(bi->sym) + bi->start,
 				  he->ms.sym);
 
-	end_line = map__srcline(he->ms.map, bi->sym->start + bi->end,
+	end_line = map__srcline(he->ms.map, symbol__start(bi->sym) + bi->end,
 				he->ms.sym);
 
 	if (start_line != SRCLINE_UNKNOWN &&
tools/perf/util/bpf_lock_contention.c +8 -8
index c20bd075664e..2b799bf7a9a2 100644
@@ -143,7 +143,7 @@ static void init_numa_data(struct lock_contention *con)
 						  "contig_page_data",
 						  &kmap);
 	if (sym) {
-		skel->rodata->contig_page_data_addr = map__unmap_ip(kmap, sym->start);
+		skel->rodata->contig_page_data_addr = map__unmap_ip(kmap, symbol__start(sym));
 		map__put(kmap);
 		return;
 	}
@@ -159,7 +159,7 @@ static void init_numa_data(struct lock_contention *con)
 	if (sym == NULL)
 		return;
 
-	skel->rodata->node_data_addr = map__unmap_ip(kmap, sym->start);
+	skel->rodata->node_data_addr = map__unmap_ip(kmap, symbol__start(sym));
 	map__put(kmap);
 
 	/* get the number of online nodes using the last node number + 1 */
@@ -266,7 +266,7 @@ int lock_contention_prepare(struct lock_contention *con)
 				continue;
 			}
 
-			addrs[con->filters->nr_addrs++] = map__unmap_ip(kmap, sym->start);
+			addrs[con->filters->nr_addrs++] = map__unmap_ip(kmap, symbol__start(sym));
 			con->filters->addrs = addrs;
 		}
 		naddrs = con->filters->nr_addrs ?: has_mmap_lock;
@@ -288,7 +288,7 @@ int lock_contention_prepare(struct lock_contention *con)
 				continue;
 			}
 
-			con->delays[i].addr = map__unmap_ip(kmap, sym->start);
+			con->delays[i].addr = map__unmap_ip(kmap, symbol__start(sym));
 		}
 		skel->rodata->lock_delay = 1;
 		bpf_map__set_max_entries(skel->maps.lock_delays, con->nr_delays);
@@ -608,7 +608,7 @@ static const char *lock_contention_get_name(struct lock_contention *con,
 		/* global locks with symbols */
 		sym = machine__find_kernel_symbol(machine, key->lock_addr_or_cgroup, &kmap);
 		if (sym)
-			return sym->name;
+			return symbol__name(sym);
 
 		/* try semi-global locks collected separately */
 		if (!bpf_map_lookup_elem(lock_fd, &key->lock_addr_or_cgroup, &flags)) {
@@ -652,12 +652,12 @@ static const char *lock_contention_get_name(struct lock_contention *con,
 	if (sym) {
 		unsigned long offset;
 
-		offset = map__map_ip(kmap, addr) - sym->start;
+		offset = map__map_ip(kmap, addr) - symbol__start(sym);
 
 		if (offset == 0)
-			return sym->name;
+			return symbol__name(sym);
 
-		snprintf(name_buf, sizeof(name_buf), "%s+%#lx", sym->name, offset);
+		snprintf(name_buf, sizeof(name_buf), "%s+%#lx", symbol__name(sym), offset);
 	} else {
 		snprintf(name_buf, sizeof(name_buf), "%#lx", (unsigned long)addr);
 	}
tools/perf/util/c2c-function.c +10 -9
index d410196d0429..b548c76e3016 100644
@@ -966,7 +966,7 @@ c2c_function_hists__level1_entry(struct symbol *sym,
 	 * true "function view" rather than a per-code-address view.
 	 */
 	u64 sym_start = (sym && detail_he->ms.map) ?
-			map__unmap_ip(detail_he->ms.map, sym->start) : detail_he->ip;
+			map__unmap_ip(detail_he->ms.map, symbol__start(sym)) : detail_he->ip;
 
 	mi = mem_info__new();
 	if (!mi)
@@ -1058,8 +1058,8 @@ c2c_function_hists__level2_entry(struct c2c_hist_entry *level1_c2c,
 	/* Key by the function symbol start so all code addresses collapse. */
 	level2_c2c = c2c_child_entry__alloc(level1_he, detail_he, 1,
 					    (sym && detail_he->ms.map) ?
-						  map__unmap_ip(detail_he->ms.map, sym->start) :
-						  hist_entry__iaddr(detail_he));
+					    map__unmap_ip(detail_he->ms.map, symbol__start(sym)) :
+					    hist_entry__iaddr(detail_he));
 	if (!level2_c2c)
 		return NULL;
 
@@ -1178,13 +1178,14 @@ static void c2c_he__resort_by_stores(struct hist_entry *parent_he)
 				/* Stable tie-break: instruction address, name, then cacheline. */
 				u64 a = hist_entry__iaddr(he), b = hist_entry__iaddr(iter);
 
-				if (a != b)
+				if (a != b) {
 					cmp = a < b ? -1 : 1;
-				else if (he->ms.sym && iter->ms.sym)
-					cmp = strcmp(he->ms.sym->name,
-						     iter->ms.sym->name);
-				else
+				} else if (he->ms.sym && iter->ms.sym) {
+					cmp = strcmp(symbol__name(he->ms.sym),
+						     symbol__name(iter->ms.sym));
+				} else {
 					cmp = (iter->ms.sym ? 1 : 0) - (he->ms.sym ? 1 : 0);
+				}
 
 				if (!cmp) {
 					struct c2c_hist_entry *he_c2c;
@@ -1407,7 +1408,7 @@ static int c2c_function__ident_len(struct hist_entry *he)
 	 * Match symbol_view_entry(): sort_sym adds the cpumode prefix and, in
 	 * verbose mode, the address and DSO origin before the symbol name.
 	 */
-	size = strlen(he->ms.sym->name) + 64;
+	size = strlen(symbol__name(he->ms.sym)) + 64;
 	symbuf = malloc(size);
 	if (!symbuf)
 		return size - 1;
tools/perf/util/callchain.c +11 -8
index 4e4696cfa6d4..489f203994df 100644
@@ -802,13 +802,15 @@ static enum match_result match_chain(struct callchain_cursor_node *node,
 			 * on the symbol start address.
 			 */
 			if (symbol__inlined(cnode->ms.sym) || symbol__inlined(node->ms.sym)) {
-				match = match_chain_strings(cnode->ms.sym->name,
-							    node->ms.sym->name);
+				match = match_chain_strings(symbol__name(cnode->ms.sym),
+							    symbol__name(node->ms.sym));
 				if (match != MATCH_ERROR)
 					break;
 			} else {
-				match = match_chain_dso_addresses(cnode->ms.map, cnode->ms.sym->start,
-								  node->ms.map, node->ms.sym->start);
+				match = match_chain_dso_addresses(cnode->ms.map,
+								  symbol__start(cnode->ms.sym),
+								  node->ms.map,
+								  symbol__start(node->ms.sym));
 				break;
 			}
 		}
@@ -1249,13 +1251,14 @@ char *callchain_list__sym_name(struct callchain_list *cl,
 	if (cl->ms.sym) {
 		const char *inlined = symbol__inlined(cl->ms.sym) ? " (inlined)" : "";
 
-		if (show_srcline && cl->srcline)
+		if (show_srcline && cl->srcline) {
 			printed = scnprintf(bf, bfsize, "%s %s%s",
-					    cl->ms.sym->name, cl->srcline,
+					    symbol__name(cl->ms.sym), cl->srcline,
 					    inlined);
-		else
+		} else {
 			printed = scnprintf(bf, bfsize, "%s%s",
-					    cl->ms.sym->name, inlined);
+					    symbol__name(cl->ms.sym), inlined);
+		}
 	} else
 		printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
 
tools/perf/util/capstone.c +10 -10
index 95881f15324a..422a48385b78 100644
@@ -333,7 +333,7 @@ static struct symbol *find_symbol_from_addr(struct map *map, u64 addr,
 		return NULL;
 	}
 
-	*sym_offset = al_addr - sym->start;
+	*sym_offset = al_addr - symbol__start(sym);
 	return sym;
 }
 
@@ -368,10 +368,10 @@ static void print_capstone_detail(struct cs_insn *insn, char *buf, size_t len,
 
 		if (sym_offset == 0) {
 			scnprintf(buf, len, "\t# %"PRIx64" <%s>",
-				  orig_addr, sym->name);
+				  orig_addr, symbol__name(sym));
 		} else {
 			scnprintf(buf, len, "\t# %"PRIx64" <%s+%#"PRIx64">",
-				  orig_addr, sym->name, sym_offset);
+				  orig_addr, symbol__name(sym), sym_offset);
 		}
 		map__put(found_map);
 		break;
@@ -454,10 +454,10 @@ static void format_capstone_insn_arm64(struct cs_insn *insn, char *buf,
 		len = len - (last_imm - buf);
 		if (sym_offset == 0) {
 			scnprintf(last_imm, len, "%"PRIx64" <%s>",
-				  addr, sym->name);
+				  addr, symbol__name(sym));
 		} else {
 			scnprintf(last_imm, len, "%"PRIx64" <%s+%#"PRIx64">",
-				  addr, sym->name, sym_offset);
+				  addr, symbol__name(sym), sym_offset);
 		}
 		map__put(found_map);
 	}
@@ -498,7 +498,7 @@ int symbol__disassemble_capstone(const char *filename, struct symbol *sym,
 	struct annotation *notes = symbol__annotation(sym);
 	struct map *map = args->ms->map;
 	struct dso *dso = map__dso(map);
-	u64 start = map__rip_2objdump(map, sym->start);
+	u64 start = map__rip_2objdump(map, symbol__start(sym));
 	u64 offset;
 	int i, count, free_count;
 	bool is_64bit = false;
@@ -526,7 +526,7 @@ int symbol__disassemble_capstone(const char *filename, struct symbol *sym,
 
 	/* add the function address and name */
 	scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
-		  start, sym->name);
+		  start, symbol__name(sym));
 
 	args->offset = -1;
 	args->line = disasm_buf;
@@ -631,8 +631,8 @@ int symbol__disassemble_capstone_powerpc(const char *filename __maybe_unused,
 	struct map *map = args->ms->map;
 	struct dso *dso = map__dso(map);
 	struct nscookie nsc;
-	u64 start = map__rip_2objdump(map, sym->start);
-	u64 end = map__rip_2objdump(map, sym->end);
+	u64 start = map__rip_2objdump(map, symbol__start(sym));
+	u64 end = map__rip_2objdump(map, symbol__end(sym));
 	u64 len = end - start;
 	u64 offset;
 	int i, fd, count;
@@ -703,7 +703,7 @@ int symbol__disassemble_capstone_powerpc(const char *filename __maybe_unused,
 
 	/* add the function address and name */
 	scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
-		  start, sym->name);
+		  start, symbol__name(sym));
 
 	args->offset = -1;
 	args->line = disasm_buf;
tools/perf/util/data-convert-json.c +2 -2
index 40888b7c4467..c09bd77f5665 100644
@@ -138,11 +138,11 @@ static void output_sample_callchain_entry(const struct perf_tool *tool,
 	output_json_format(out, false, 4, "{");
 	output_json_key_format(out, false, 5, "ip", "\"0x%" PRIx64 "\"", ip);
 
-	if (al && al->sym && al->sym->namelen) {
+	if (al && al->sym && symbol__namelen(al->sym)) {
 		struct dso *dso = al->map ? map__dso(al->map) : NULL;
 
 		fputc(',', out);
-		output_json_key_string(out, false, 5, "symbol", al->sym->name);
+		output_json_key_string(out, false, 5, "symbol", symbol__name(al->sym));
 
 		if (dso) {
 			const char *dso_name = dso__short_name(dso);
tools/perf/util/db-export.c +1 -1
index e7b807e968cf..3c46280ba273 100644
@@ -298,7 +298,7 @@ static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
 			if (err)
 				return err;
 			*sym_db_id = db_export__symbol_db_id(dbe, dso, al->sym);
-			*offset = al->addr - al->sym->start;
+			*offset = al->addr - symbol__start(al->sym);
 		}
 	}
 
tools/perf/util/debug.c +2 -1
index b7095519419e..eec3fc14df98 100644
@@ -342,7 +342,8 @@ void __dump_stack(FILE *file, void **stackdump, size_t stackdump_size)
 		if (thread && thread__find_map(thread, PERF_RECORD_MISC_USER, addr, &al)) {
 			al.sym = map__find_symbol(al.map, al.addr);
 			if (al.sym) {
-				fprintf(file, "    #%zd %p in %s ", i, stackdump[i], al.sym->name);
+				fprintf(file, "    #%zd %p in %s ", i, stackdump[i],
+					symbol__name(al.sym));
 				printed = true;
 			}
 		}
tools/perf/util/disasm.c +27 -22

119 lines not on this page · raw message

tools/perf/util/dlfilter.c +6 -6
index 7e179d1d84d7..f66b8c130623 100644
@@ -47,13 +47,13 @@ static void al_to_d_al(struct addr_location *al, struct perf_dlfilter_al *d_al)
 		d_al->buildid = NULL;
 	}
 	if (sym) {
-		d_al->sym = sym->name;
-		d_al->sym_start = sym->start;
-		d_al->sym_end = sym->end;
-		if (al->addr < sym->end)
-			d_al->symoff = al->addr - sym->start;
+		d_al->sym = symbol__name(sym);
+		d_al->sym_start = symbol__start(sym);
+		d_al->sym_end = symbol__end(sym);
+		if (al->addr < symbol__end(sym))
+			d_al->symoff = al->addr - symbol__start(sym);
 		else if (al->map)
-			d_al->symoff = al->addr - map__start(al->map) - sym->start;
+			d_al->symoff = al->addr - map__start(al->map) - symbol__start(sym);
 		else
 			d_al->symoff = 0;
 		d_al->sym_binding = symbol__binding(sym);
tools/perf/util/dso.c +2 -2
index ce98613868e7..39a1eb9cb73f 100644
@@ -2147,8 +2147,8 @@ const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename,
 			   const struct map *map, const struct symbol *sym,
 			   u8 **out_buf, u64 *out_buf_len, bool *is_64bit)
 {
-	u64 start = map__rip_2objdump(map, sym->start);
-	u64 end = map__rip_2objdump(map, sym->end);
+	u64 start = map__rip_2objdump(map, symbol__start(sym));
+	u64 end = map__rip_2objdump(map, symbol__end(sym));
 	size_t len = end - start;
 
 	*out_buf = NULL;
tools/perf/util/event.c +2 -2
index 2f80e364659f..3c78891be062 100644
@@ -881,11 +881,11 @@ int machine__resolve(struct machine *machine, struct addr_location *al,
 
 		if (al->sym) {
 			ret = strlist__has_entry(symbol_conf.sym_list,
-						al->sym->name);
+						symbol__name(al->sym));
 		}
 		if (!ret && al->sym) {
 			snprintf(al_addr_str, sz, "0x%"PRIx64,
-				 map__unmap_ip(al->map, al->sym->start));
+				 map__unmap_ip(al->map, symbol__start(al->sym)));
 			ret = strlist__has_entry(symbol_conf.sym_list,
 						al_addr_str);
 		}
tools/perf/util/evsel_fprintf.c +1 -1
index 0f7a25500a44..76a0c69e6f45 100644
@@ -200,7 +200,7 @@ int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment,
 
 			/* Add srccode here too? */
 			if (bt_stop_list && sym &&
-			    strlist__has_entry(bt_stop_list, sym->name)) {
+			    strlist__has_entry(bt_stop_list, symbol__name(sym))) {
 				break;
 			}
 
tools/perf/util/hist.c +8 -8
index d516ce469c7e..566ecf4035fb 100644
@@ -97,7 +97,7 @@ void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
 	 * +3 accounts for ' y ' symtab origin info
 	 */
 	if (h->ms.sym) {
-		symlen = h->ms.sym->namelen + 4;
+		symlen = symbol__namelen(h->ms.sym) + 4;
 		if (verbose > 0)
 			symlen += BITS_PER_LONG / 4 + 2 + 3;
 		hists__new_col_len(hists, HISTC_SYMBOL, symlen);
@@ -120,11 +120,11 @@ void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
 	}
 
 	if (h->parent.sym)
-		hists__new_col_len(hists, HISTC_PARENT, h->parent.sym->namelen);
+		hists__new_col_len(hists, HISTC_PARENT, symbol__namelen(h->parent.sym));
 
 	if (h->branch_info) {
 		if (h->branch_info->from.ms.sym) {
-			symlen = (int)h->branch_info->from.ms.sym->namelen + 4;
+			symlen = (int) symbol__namelen(h->branch_info->from.ms.sym) + 4;
 			if (verbose > 0)
 				symlen += BITS_PER_LONG / 4 + 2 + 3;
 			hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
@@ -139,7 +139,7 @@ void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
 		}
 
 		if (h->branch_info->to.ms.sym) {
-			symlen = (int)h->branch_info->to.ms.sym->namelen + 4;
+			symlen = (int) symbol__namelen(h->branch_info->to.ms.sym) + 4;
 			if (verbose > 0)
 				symlen += BITS_PER_LONG / 4 + 2 + 3;
 			hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
@@ -163,7 +163,7 @@ void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
 
 	if (h->mem_info) {
 		if (mem_info__daddr(h->mem_info)->ms.sym) {
-			symlen = (int)mem_info__daddr(h->mem_info)->ms.sym->namelen + 4
+			symlen = (int) symbol__namelen(mem_info__daddr(h->mem_info)->ms.sym) + 4
 			       + unresolved_col_width + 2;
 			hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
 					   symlen);
@@ -178,7 +178,7 @@ void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
 		}
 
 		if (mem_info__iaddr(h->mem_info)->ms.sym) {
-			symlen = (int)mem_info__iaddr(h->mem_info)->ms.sym->namelen + 4
+			symlen = (int) symbol__namelen(mem_info__iaddr(h->mem_info)->ms.sym) + 4
 			       + unresolved_col_width + 2;
 			hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
 					   symlen);
@@ -729,7 +729,7 @@ static struct hist_entry *hists__findnew_entry(struct hists *hists,
 			 */
 			if (hists__has(hists, sym) && he->ms.map != entry->ms.map) {
 				if (he->ms.sym) {
-					u64 addr = he->ms.sym->start;
+					u64 addr = symbol__start(he->ms.sym);
 					he->ms.sym = map__find_symbol(entry->ms.map, addr);
 				}
 
@@ -2307,7 +2307,7 @@ static bool hists__filter_entry_by_symbol(struct hists *hists,
 					  struct hist_entry *he)
 {
 	if (hists->symbol_filter_str != NULL &&
-	    (!he->ms.sym || strstr(he->ms.sym->name,
+	    (!he->ms.sym || strstr(symbol__name(he->ms.sym),
 				   hists->symbol_filter_str) == NULL)) {
 		he->filtered |= (1 << HIST_FILTER__SYMBOL);
 		return true;
tools/perf/util/intel-pt.c +4 -4
index e8b9c8a67b0a..a6120f35a9ff 100644
@@ -3010,8 +3010,8 @@ static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)
 
 	for (sym = start; sym; sym = dso__next_symbol(sym)) {
 		if (symbol__binding(sym) == STB_GLOBAL &&
-		    !strcmp(sym->name, "__switch_to")) {
-			ip = map__unmap_ip(map, sym->start);
+		    !strcmp(symbol__name(sym), "__switch_to")) {
+			ip = map__unmap_ip(map, symbol__start(sym));
 			if (ip >= map__start(map) && ip < map__end(map)) {
 				switch_ip = ip;
 				break;
@@ -3028,8 +3028,8 @@ static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)
 		ptss = "__perf_event_task_sched_out";
 
 	for (sym = start; sym; sym = dso__next_symbol(sym)) {
-		if (!strcmp(sym->name, ptss)) {
-			ip = map__unmap_ip(map, sym->start);
+		if (!strcmp(symbol__name(sym), ptss)) {
+			ip = map__unmap_ip(map, symbol__start(sym));
 			if (ip >= map__start(map) && ip < map__end(map)) {
 				*ptss_ip = ip;
 				break;
tools/perf/util/libbfd.c +2 -2
index 58cadc93e8b5..a5e2fbf774e9 100644
@@ -553,7 +553,7 @@ int symbol__disassemble_bpf_libbfd(struct symbol *sym __maybe_unused,
 	struct annotation *notes = symbol__annotation(sym);
 	struct bpf_prog_linfo *prog_linfo = NULL;
 	struct bpf_prog_info_node *info_node;
-	int len = sym->end - sym->start;
+	int len = symbol__end(sym) - symbol__start(sym);
 	disassembler_ftype disassemble;
 	struct map *map = args->ms->map;
 	struct perf_bpil *info_linear;
@@ -573,7 +573,7 @@ int symbol__disassemble_bpf_libbfd(struct symbol *sym __maybe_unused,
 		return SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE;
 
 	pr_debug("%s: handling sym %s addr %" PRIx64 " len %" PRIx64 "\n", __func__,
-		  sym->name, sym->start, sym->end - sym->start);
+		  symbol__name(sym), symbol__start(sym), symbol__end(sym) - symbol__start(sym));
 
 	memset(tpath, 0, sizeof(tpath));
 	perf_exe(tpath, sizeof(tpath));
tools/perf/util/llvm.c +3 -3
index 0ecb1a53359e..7429e8ef6cce 100644
@@ -138,7 +138,7 @@ int symbol__disassemble_llvm(const char *filename, struct symbol *sym,
 	struct annotation *notes = symbol__annotation(sym);
 	struct map *map = args->ms->map;
 	struct dso *dso = map__dso(map);
-	u64 start = map__rip_2objdump(map, sym->start);
+	u64 start = map__rip_2objdump(map, symbol__start(sym));
 	/* Malloc-ed buffer containing instructions read from disk. */
 	u8 *code_buf = NULL;
 	/* Pointer to code to be disassembled. */
@@ -202,7 +202,7 @@ int symbol__disassemble_llvm(const char *filename, struct symbol *sym,
 
 	/* add the function address and name */
 	scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
-		  start, sym->name);
+		  start, symbol__name(sym));
 
 	args->offset = -1;
 	args->line = disasm_buf;
@@ -323,7 +323,7 @@ int symbol__disassemble_llvm(const char *filename, struct symbol *sym,
 	return ret;
 #else // HAVE_LIBLLVM_SUPPORT
 	pr_debug("The LLVM disassembler isn't linked in for %s in %s\n",
-		 sym->name, filename);
+		 symbol__name(sym), filename);
 	return -1;
 #endif
 }
tools/perf/util/lock-contention.c +1 -1
index 119a7206f3cd..8ee1b19a8292 100644
@@ -135,7 +135,7 @@ bool match_callstack_filter(struct machine *machine, u64 *callstack, int max_sta
 			continue;
 
 		list_for_each_entry(filter, &callstack_filters, list) {
-			if (strstr(sym->name, filter->name))
+			if (strstr(symbol__name(sym), filter->name))
 				return true;
 		}
 	}
tools/perf/util/machine.c +13 -13
index 905d275ca364..5c8bf5779c47 100644
@@ -1135,8 +1135,8 @@ static u64 find_entry_trampoline(struct dso *dso)
 		if (symbol__binding(sym) != STB_GLOBAL)
 			continue;
 		for (i = 0; i < ARRAY_SIZE(syms); i++) {
-			if (!strcmp(sym->name, syms[i]))
-				return sym->start;
+			if (!strcmp(symbol__name(sym), syms[i]))
+				return symbol__start(sym);
 		}
 	}
 
@@ -2084,7 +2084,7 @@ int machine__process_event(struct machine *machine, union perf_event *event,
 
 static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
 {
-	return regexec(regex, sym->name, 0, NULL, 0) == 0;
+	return regexec(regex, symbol__name(sym), 0, NULL, 0) == 0;
 }
 
 static void ip__resolve_ams(struct thread *thread,
@@ -3323,8 +3323,8 @@ char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, ch
 		return NULL;
 
 	*modp = __map__is_kmodule(map) ? (char *)dso__short_name(map__dso(map)) : NULL;
-	*addrp = map__unmap_ip(map, sym->start);
-	return sym->name;
+	*addrp = map__unmap_ip(map, symbol__start(sym));
+	return (char *)symbol__name(sym);
 }
 
 struct machine__for_each_dso_cb_args {
@@ -3367,33 +3367,33 @@ bool machine__is_lock_function(struct machine *machine, u64 addr)
 		if (!sym)
 			goto fail;
 
-		machine->sched.text_start = map__unmap_ip(kmap, sym->start);
+		machine->sched.text_start = map__unmap_ip(kmap, symbol__start(sym));
 
 		/* should not fail from here */
 		sym = machine__find_kernel_symbol_by_name(machine, "__sched_text_end", &kmap);
 		if (!sym)
 			goto fail;
-		machine->sched.text_end = map__unmap_ip(kmap, sym->start);
+		machine->sched.text_end = map__unmap_ip(kmap, symbol__start(sym));
 
 		sym = machine__find_kernel_symbol_by_name(machine, "__lock_text_start", &kmap);
 		if (!sym)
 			goto fail;
-		machine->lock.text_start = map__unmap_ip(kmap, sym->start);
+		machine->lock.text_start = map__unmap_ip(kmap, symbol__start(sym));
 
 		sym = machine__find_kernel_symbol_by_name(machine, "__lock_text_end", &kmap);
 		if (!sym)
 			goto fail;
-		machine->lock.text_end = map__unmap_ip(kmap, sym->start);
+		machine->lock.text_end = map__unmap_ip(kmap, symbol__start(sym));
 
 		sym = machine__find_kernel_symbol_by_name(machine, "__traceiter_contention_begin", &kmap);
 		if (sym) {
-			machine->traceiter.text_start = map__unmap_ip(kmap, sym->start);
-			machine->traceiter.text_end = map__unmap_ip(kmap, sym->end);
+			machine->traceiter.text_start = map__unmap_ip(kmap, symbol__start(sym));
+			machine->traceiter.text_end = map__unmap_ip(kmap, symbol__end(sym));
 		}
 		sym = machine__find_kernel_symbol_by_name(machine, "trace_contention_begin", &kmap);
 		if (sym) {
-			machine->trace.text_start = map__unmap_ip(kmap, sym->start);
-			machine->trace.text_end = map__unmap_ip(kmap, sym->end);
+			machine->trace.text_start = map__unmap_ip(kmap, symbol__start(sym));
+			machine->trace.text_end = map__unmap_ip(kmap, symbol__end(sym));
 		}
 	}
 
tools/perf/util/map.c +3 -3
index d5319ffa88a7..8be5606c940e 100644
@@ -321,7 +321,7 @@ void map__fixup_start(struct map *map)
 	if (nd != NULL) {
 		struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
 
-		map__set_start(map, sym->start);
+		map__set_start(map, symbol__start(sym));
 	}
 }
 
@@ -333,7 +333,7 @@ void map__fixup_end(struct map *map)
 
 	if (nd != NULL) {
 		struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
-		map__set_end(map, sym->end);
+		map__set_end(map, symbol__end(sym));
 	}
 }
 
@@ -603,7 +603,7 @@ u64 map__objdump_2rip(const struct map *map, u64 ip)
 
 bool map__contains_symbol(const struct map *map, const struct symbol *sym)
 {
-	u64 ip = map__unmap_ip(map, sym->start);
+	u64 ip = map__unmap_ip(map, symbol__start(sym));
 
 	return ip >= map__start(map) && ip < map__end(map);
 }
tools/perf/util/map.h +1 -1
index c2e31be232c8..0d1b8a1cd84c 100644
@@ -164,7 +164,7 @@ struct thread;
 #define __map__for_each_symbol_by_name(map, sym_name, pos, idx)		\
 	for (pos = map__find_symbol_by_name_idx(map, sym_name, &idx);	\
 	     pos &&						\
-	     !symbol__match_symbol_name(pos->name, sym_name,	\
+	     !symbol__match_symbol_name(symbol__name(pos), sym_name,	\
 					SYMBOL_TAG_INCLUDE__DEFAULT_ONLY); \
 	     pos = dso__next_symbol_by_name(map__dso(map), &idx))
 
tools/perf/util/probe-event.c +21 -20

121 lines not on this page · raw message

tools/perf/util/python.c +8 -8
index 2fcbab8e69ec..4ab5c19050e0 100644
@@ -812,7 +812,7 @@ static PyObject *pyrf_sample_event__get_symbol(struct pyrf_event *pevent,
 	if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.sym)
 		Py_RETURN_NONE;
 
-	return PyUnicode_FromString(pevent->al.sym->name);
+	return PyUnicode_FromString(symbol__name(pevent->al.sym));
 }
 
 static PyObject *pyrf_sample_event__get_sym_start(struct pyrf_event *pevent,
@@ -821,7 +821,7 @@ static PyObject *pyrf_sample_event__get_sym_start(struct pyrf_event *pevent,
 	if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.sym)
 		Py_RETURN_NONE;
 
-	return PyLong_FromUnsignedLongLong(pevent->al.sym->start);
+	return PyLong_FromUnsignedLongLong(symbol__start(pevent->al.sym));
 }
 
 static PyObject *pyrf_sample_event__get_sym_offset(struct pyrf_event *pevent,
@@ -830,7 +830,7 @@ static PyObject *pyrf_sample_event__get_sym_offset(struct pyrf_event *pevent,
 	if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.sym)
 		Py_RETURN_NONE;
 
-	return PyLong_FromUnsignedLongLong(pevent->al.addr - pevent->al.sym->start);
+	return PyLong_FromUnsignedLongLong(pevent->al.addr - symbol__start(pevent->al.sym));
 }
 
 static PyObject *pyrf_sample_event__get_sym_end(struct pyrf_event *pevent,
@@ -839,7 +839,7 @@ static PyObject *pyrf_sample_event__get_sym_end(struct pyrf_event *pevent,
 	if (pyrf_sample_event__resolve_al(pevent) < 0 || !pevent->al.sym)
 		Py_RETURN_NONE;
 
-	return PyLong_FromUnsignedLongLong(pevent->al.sym->end);
+	return PyLong_FromUnsignedLongLong(symbol__end(pevent->al.sym));
 }
 
 static PyObject *pyrf_sample_event__get_raw_buf(struct pyrf_event *pevent,
@@ -957,7 +957,7 @@ static PyObject *pyrf_callchain_node__get_symbol(struct pyrf_callchain_node *pno
 						 void *closure __maybe_unused)
 {
 	if (pnode->ds.sym)
-		return PyUnicode_FromString(pnode->ds.sym->name);
+		return PyUnicode_FromString(symbol__name(pnode->ds.sym));
 	return PyUnicode_FromString("[unknown]");
 }
 
@@ -985,7 +985,7 @@ static PyObject *pyrf_callchain_node__get_sym_offset(struct pyrf_callchain_node
 		Py_RETURN_NONE;
 
 	addr = pnode->map ? map__map_ip(pnode->map, pnode->ip) : pnode->ip;
-	return PyLong_FromUnsignedLongLong(addr - pnode->ds.sym->start);
+	return PyLong_FromUnsignedLongLong(addr - symbol__start(pnode->ds.sym));
 }
 
 static PyGetSetDef pyrf_callchain_node__getset[] = {
@@ -1328,7 +1328,7 @@ static PyObject *pyrf_sample_event__get_addr_symbol(struct pyrf_event *pevent,
 	PyObject *ret = Py_None;
 
 	if (pyrf_sample_event__resolve_addr_al(pevent, &addr_al) == 0 && addr_al.sym)
-		ret = PyUnicode_FromString(addr_al.sym->name);
+		ret = PyUnicode_FromString(symbol__name(addr_al.sym));
 	else
 		Py_INCREF(Py_None);
 
@@ -1343,7 +1343,7 @@ static PyObject *pyrf_sample_event__get_addr_sym_offset(struct pyrf_event *peven
 	PyObject *ret = Py_None;
 
 	if (pyrf_sample_event__resolve_addr_al(pevent, &addr_al) == 0 && addr_al.sym)
-		ret = PyLong_FromUnsignedLongLong(addr_al.addr - addr_al.sym->start);
+		ret = PyLong_FromUnsignedLongLong(addr_al.addr - symbol__start(addr_al.sym));
 	else
 		Py_INCREF(Py_None);
 
tools/perf/util/sort.c +21 -19
index 8b455081442a..722bccc2554e 100644
@@ -470,18 +470,19 @@ int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
 		return 0;
 
 	if (symbol__inlined(sym_l) || symbol__inlined(sym_r)) {
-		int ret = strcmp(sym_l->name, sym_r->name);
+		int ret = strcmp(symbol__name(sym_l), symbol__name(sym_r));
 
 		if (ret)
 			return ret;
-		if ((sym_l->start <= sym_r->end) && (sym_l->end >= sym_r->start))
+		if ((symbol__start(sym_l) <= symbol__end(sym_r)) &&
+		    (symbol__end(sym_l) >= symbol__start(sym_r)))
 			return 0;
 	}
 
-	if (sym_l->start != sym_r->start)
-		return (int64_t)(sym_r->start - sym_l->start);
+	if (symbol__start(sym_l) != symbol__start(sym_r))
+		return (int64_t)(symbol__start(sym_r) - symbol__start(sym_l));
 
-	return (int64_t)(sym_r->end - sym_l->end);
+	return (int64_t)(symbol__end(sym_r) - symbol__end(sym_l));
 }
 
 static int64_t
@@ -511,7 +512,7 @@ sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
 	if (!left->ms.sym || !right->ms.sym)
 		return cmp_null(left->ms.sym, right->ms.sym);
 
-	return strcmp(right->ms.sym->name, left->ms.sym->name);
+	return strcmp(symbol__name(right->ms.sym), symbol__name(left->ms.sym));
 }
 
 static int _hist_entry__sym_snprintf(struct map_symbol *ms,
@@ -537,13 +538,13 @@ static int _hist_entry__sym_snprintf(struct map_symbol *ms,
 	ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
 	if (sym && map) {
 		if (symbol__type(sym) == STT_OBJECT) {
-			ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
+			ret += repsep_snprintf(bf + ret, size - ret, "%s", symbol__name(sym));
 			ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
-					ip - map__unmap_ip(map, sym->start));
+					ip - map__unmap_ip(map, symbol__start(sym)));
 		} else {
 			ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
 					       width - ret,
-					       sym->name);
+					       symbol__name(sym));
 			if (symbol__inlined(sym))
 				ret += repsep_snprintf(bf + ret, size - ret,
 						       " (inlined)");
@@ -570,7 +571,7 @@ static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *a
 	if (type != HIST_FILTER__SYMBOL)
 		return -1;
 
-	return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym));
+	return sym && (!he->ms.sym || !strstr(symbol__name(he->ms.sym), sym));
 }
 
 struct sort_entry sort_sym = {
@@ -616,7 +617,8 @@ hist_entry__symoff_snprintf(struct hist_entry *he, char *bf, size_t size, unsign
 	if (sym == NULL)
 		return repsep_snprintf(bf, size, "[%c] %-#.*llx", he->level, width - 4, he->ip);
 
-	return repsep_snprintf(bf, size, "[%c] %s+0x%llx", he->level, sym->name, he->ip - sym->start);
+	return repsep_snprintf(bf, size, "[%c] %s+0x%llx", he->level,
+			       symbol__name(sym), he->ip - symbol__start(sym));
 }
 
 static struct sort_entry sort_sym_offset = {
@@ -1017,14 +1019,14 @@ sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
 	if (!sym_l || !sym_r)
 		return cmp_null(sym_l, sym_r);
 
-	return strcmp(sym_r->name, sym_l->name);
+	return strcmp(symbol__name(sym_r), symbol__name(sym_l));
 }
 
 static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
 				       size_t size, unsigned int width)
 {
 	return repsep_snprintf(bf, size, "%-*.*s", width, width,
-			      he->parent.sym ? he->parent.sym->name : "[other]");
+			      he->parent.sym ? symbol__name(he->parent.sym) : "[other]");
 }
 
 struct sort_entry sort_parent = {
@@ -1427,7 +1429,7 @@ static int hist_entry__sym_from_filter(struct hist_entry *he, int type,
 		return -1;
 
 	return sym && !(he->branch_info && he->branch_info->from.ms.sym &&
-			strstr(he->branch_info->from.ms.sym->name, sym));
+			strstr(symbol__name(he->branch_info->from.ms.sym), sym));
 }
 
 static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
@@ -1439,7 +1441,7 @@ static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
 		return -1;
 
 	return sym && !(he->branch_info && he->branch_info->to.ms.sym &&
-		        strstr(he->branch_info->to.ms.sym->name, sym));
+			strstr(symbol__name(he->branch_info->to.ms.sym), sym));
 }
 
 struct sort_entry sort_dso_from = {
@@ -1485,14 +1487,14 @@ static int _hist_entry__addr_snprintf(struct map_symbol *ms,
 	ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
 	if (sym && map) {
 		if (symbol__type(sym) == STT_OBJECT) {
-			ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
+			ret += repsep_snprintf(bf + ret, size - ret, "%s", symbol__name(sym));
 			ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
-					ip - map__unmap_ip(map, sym->start));
+					ip - map__unmap_ip(map, symbol__start(sym)));
 		} else {
 			ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
 					       width - ret,
-					       sym->name);
-			offs = ip - sym->start;
+					       symbol__name(sym));
+			offs = ip - symbol__start(sym);
 			if (offs)
 				ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx", offs);
 		}
tools/perf/util/srcline.c +9 -7
index 23951c35fb66..14f2b6dfd086 100644
@@ -116,7 +116,7 @@ struct symbol *new_inline_sym(struct dso *dso,
 			funcname = demangled;
 	}
 
-	if (base_sym && strcmp(funcname, base_sym->name) == 0) {
+	if (base_sym && strcmp(funcname, symbol__name(base_sym)) == 0) {
 		/* reuse the real, existing symbol */
 		inline_sym = base_sym;
 		/* ensure that we don't alias an inlined symbol, which could
@@ -125,8 +125,9 @@ struct symbol *new_inline_sym(struct dso *dso,
 		assert(!symbol__inlined(base_sym));
 	} else {
 		/* create a fake symbol for the inline frame */
-		inline_sym = symbol__new(base_sym ? base_sym->start : 0,
-					 base_sym ? (base_sym->end - base_sym->start) : 0,
+		inline_sym = symbol__new(base_sym ? symbol__start(base_sym) : 0,
+					 base_sym ?
+					 (symbol__end(base_sym) - symbol__start(base_sym)) : 0,
 					 base_sym ? symbol__binding(base_sym) : 0,
 					 base_sym ? symbol__type(base_sym) : 0,
 					 funcname);
@@ -310,13 +311,14 @@ char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
 	}
 	mutex_unlock(dso__lock(dso));
 out:
-	if (!show_addr)
+	if (!show_addr) {
 		return (show_sym && sym) ?
-			    strndup(sym->name, sym->namelen) : SRCLINE_UNKNOWN;
+			    strndup(symbol__name(sym), symbol__namelen(sym)) : SRCLINE_UNKNOWN;
+	}
 
 	if (sym) {
-		if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
-					ip - sym->start) < 0)
+		if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? symbol__name(sym) : "",
+					ip - symbol__start(sym)) < 0)
 			return SRCLINE_UNKNOWN;
 	} else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso__short_name(dso), addr) < 0)
 		return SRCLINE_UNKNOWN;
tools/perf/util/symbol-elf.c +5 -5
index 39d69c4a137f..96e21b9b41e2 100644
@@ -465,11 +465,11 @@ static bool get_ifunc_name(Elf *elf, struct dso *dso, GElf_Ehdr *ehdr,
 	sym = dso__find_symbol_nocache(dso, addr);
 
 	/* Expecting the address to be an IFUNC or IFUNC alias */
-	if (!sym || sym->start != addr ||
+	if (!sym || symbol__start(sym) != addr ||
 	    (symbol__type(sym) != STT_GNU_IFUNC && !symbol__ifunc_alias(sym)))
 		return false;
 
-	snprintf(buf, buf_sz, "%s@plt", sym->name);
+	snprintf(buf, buf_sz, "%s@plt", symbol__name(sym));
 
 	return true;
 }
@@ -744,8 +744,8 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
 	 * a symbol for .plt header.
 	 */
 	f = dso__find_symbol_nocache(dso, shdr_plt.sh_offset);
-	if (f && f->start < shdr_plt.sh_offset && f->end > shdr_plt.sh_offset)
-		f->end = shdr_plt.sh_offset;
+	if (f && symbol__start(f) < shdr_plt.sh_offset && symbol__end(f) > shdr_plt.sh_offset)
+		symbol__set_end(f, shdr_plt.sh_offset);
 
 	if (!get_plt_sizes(dso, &ehdr, &shdr_plt, &plt_header_size, &plt_entry_size))
 		return 0;
@@ -767,7 +767,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
 		if (!get_plt_sizes(dso, &ehdr, &plt_sec_shdr, &plt_header_size, &plt_entry_size))
 			return 0;
 		/* Extend .plt symbol to entire .plt */
-		plt_sym->end = plt_sym->start + shdr_plt.sh_size;
+		symbol__set_end(plt_sym, symbol__start(plt_sym) + shdr_plt.sh_size);
 		/* Use .plt.sec offset */
 		plt_offset = plt_sec_shdr.sh_offset;
 		lazy_plt = false;
tools/perf/util/symbol.c +53 -50

257 lines not on this page · raw message

tools/perf/util/symbol.h +42 -1

55 lines not on this page · raw message

tools/perf/util/symbol_fprintf.c +7 -7
index 4dc8d5761f52..8c131325e989 100644
@@ -10,10 +10,10 @@
 size_t symbol__fprintf(struct symbol *sym, FILE *fp)
 {
 	return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
-		       sym->start, sym->end,
+		       symbol__start(sym), symbol__end(sym),
 		       symbol__binding(sym) == STB_GLOBAL ? 'g' :
 		       symbol__binding(sym) == STB_LOCAL  ? 'l' : 'w',
-		       sym->name);
+		       symbol__name(sym));
 }
 
 size_t __symbol__fprintf_symname_offs(const struct symbol *sym,
@@ -25,12 +25,12 @@ size_t __symbol__fprintf_symname_offs(const struct symbol *sym,
 	size_t length;
 
 	if (sym) {
-		length = fprintf(fp, "%s", sym->name);
+		length = fprintf(fp, "%s", symbol__name(sym));
 		if (al && print_offsets) {
-			if (al->addr < sym->end)
-				offset = al->addr - sym->start;
+			if (al->addr < symbol__end(sym))
+				offset = al->addr - symbol__start(sym);
 			else
-				offset = al->addr - map__start(al->map) - sym->start;
+				offset = al->addr - map__start(al->map) - symbol__start(sym);
 			length += fprintf(fp, "+0x%lx", offset);
 		}
 		return length;
@@ -67,7 +67,7 @@ size_t dso__fprintf_symbols_by_name(struct dso *dso,
 	for (size_t i = 0; i < dso__symbol_names_len(dso); i++) {
 		struct symbol *pos = dso__symbol_names(dso)[i];
 
-		ret += fprintf(fp, "%s\n", pos->name);
+		ret += fprintf(fp, "%s\n", symbol__name(pos));
 	}
 	return ret;
 }
tools/perf/util/thread-stack.c +4 -4
index aa05acb0467c..4045fe4e9e11 100644
@@ -1058,7 +1058,7 @@ static int thread_stack__x86_retpoline(struct thread_stack *ts,
 	struct symbol *tsym = to_al->sym;
 	struct call_path *cp;
 
-	if (sym && is_x86_retpoline(sym->name)) {
+	if (sym && is_x86_retpoline(symbol__name(sym))) {
 		/*
 		 * This is a x86 retpoline fn. It pollutes the call graph by
 		 * showing up everywhere there is an indirect branch, but does
@@ -1071,7 +1071,7 @@ static int thread_stack__x86_retpoline(struct thread_stack *ts,
 		 */
 		ts->cnt -= 1;
 		sym = ts->stack[ts->cnt - 2].cp->ds.sym;
-		if (sym && sym == tsym && to_al->addr != tsym->start) {
+		if (sym && sym == tsym && to_al->addr != symbol__start(tsym)) {
 			/*
 			 * Target is back to the middle of the symbol we came
 			 * from so assume it is an indirect jmp and forget it
@@ -1174,7 +1174,7 @@ int thread_stack__process(struct thread *thread, struct comm *comm,
 		 */
 		if (!err && rstate == X86_RETPOLINE_POSSIBLE && to_al->sym &&
 		    from_al->sym == to_al->sym &&
-		    to_al->addr != to_al->sym->start)
+		    to_al->addr != symbol__start(to_al->sym))
 			ts->rstate = X86_RETPOLINE_DETECTED;
 
 	} else if (sample->flags & PERF_IP_FLAG_RETURN) {
@@ -1211,7 +1211,7 @@ int thread_stack__process(struct thread *thread, struct comm *comm,
 		err = thread_stack__trace_end(ts, sample, ref);
 	} else if (sample->flags & PERF_IP_FLAG_BRANCH &&
 		   from_al->sym != to_al->sym && to_al->sym &&
-		   to_al->addr == to_al->sym->start) {
+		   to_al->addr == symbol__start(to_al->sym)) {
 		struct dso_symbol tds = dso_symbol__from_al(to_al);
 		struct call_path_root *cpr = ts->crp->cpr;
 		struct call_path *cp;
tools/perf/util/unwind-libdw.c +1 -1
index d49901b1571e..87ade098b5c6 100644
@@ -172,7 +172,7 @@ static int entry(u64 ip, struct unwind_info *ui)
 	e->ms.sym = al.sym;
 
 	pr_debug("unwind: %s:ip = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
-		 al.sym ? al.sym->name : "''",
+		 al.sym ? symbol__name(al.sym) : "''",
 		 ip,
 		 al.map ? map__map_ip(al.map, ip) : (u64) 0);
 	addr_location__exit(&al);
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#80 lore raw

Re: [PATCH v3 14/20] perf symbol: Add accessor functions for struct symbol fields

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

Encapsulating struct symbol fields behind inline accessor functions decouples callers across tools/perf from struct symbol's internal layout as symbol storage and lazy loading evolve.

Add inline accessor functions for struct symbol fields (symbol__start, symbol__set_start, symbol__end, symbol__set_end, symbol__name, symbol__namelen, symbol__arch_sym, symbol__set_arch_sym) and convert direct member accesses across tools/perf.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
author reviewer
#81 lore raw

PATCH v3 15/20 perf symbol: Switch backing storage from rbtree to struct symbols array

In reply to Ian Rogers

Previously, DSO symbols were stored in an unprotected rb_root_cached with a 24-byte struct rb_node embedded inside every struct symbol. That design had three drawbacks:
1. Embedding struct rb_node (3 pointers, 24 bytes on 64-bit) accounted for nearly 40% of struct symbol's fixed header size and scattered symbol lookups across non-contiguous heap nodes.
2. Unlike struct dsos and struct maps, the symbol rbtree had no lock protecting concurrent lookups, sorting, and insertions.
3. Deduplicating symbols, splitting kallsyms, and trimming excess symbol container capacity under memory pressure benefit from operating on a compact array under a lock.

Replace struct rb_node in struct symbol with a locked, lazily-sorted pointer array in struct symbols, matching the design of struct dsos and struct maps:

- Introduce struct symbols containing a rw_semaphore lock, primary address-sorted symbols array, secondary name-sorted symbols_by_name array, counts, capacity, and sorted state flags.
- Lazily sort symbols by [start, end) using qsort (symbols__sort_locked) and use binary search in symbols__find().
- Update symbols__fixup_duplicate(), symbols__fixup_end(), maps__split_kallsyms_for_kcore(), and maps__split_kallsyms() to operate on the sorted array and compact entries in place.
- Convert symbols__for_each_entry(), dso__for_each_symbol(), and map__for_each_symbol() to callback-based iteration under symbols->lock, matching dsos__for_each_dso() and maps__for_each_map().
- Remove the unsynchronized single-entry dso->last_find_result cache and dso__reset_find_symbol_cache() since binary search over a contiguous array is cache-friendly.
- Remove the direct rb_erase_cached() workaround in builtin-annotate.c's add_sample().
- Update the TUI map browser (ui/browsers/map.c) to snapshot the sorted symbol array into an entries array and use ui_browser__argv_refresh and ui_browser__argv_seek instead of ui_browser__rb_tree_refresh.

Using 'perf bench internals symbols':

Before (perf binary):

    Average DSO load + sort: 7.041 msec (+- 0.051 msec)
    Average DSO memory usage: 1401 KB (+- 4 KB)
    Average bulk insert + lookup: 1453.950 usec (+- 10.895 usec)
  After (perf binary, -13.5% load, -14.6% memory, -7.7% bulk):
    Average DSO load + sort: 6.089 msec (+- 0.039 msec)
    Average DSO memory usage: 1197 KB (+- 3 KB)
    Average bulk insert + lookup: 1341.500 usec (+- 21.564 usec)

Before (libLLVM.so.21.1):
Average DSO load + sort: 78.599 msec (+- 0.857 msec) Average DSO memory usage: 9530 KB (+- 5 KB)
After (libLLVM.so.21.1, -22.3% load, -8.1% memory):
Average DSO load + sort: 61.074 msec (+- 0.236 msec) Average DSO memory usage: 8755 KB (+- 1 KB)

---
 tools/perf/arch/powerpc/util/sym-handling.c |  34 +-
 tools/perf/builtin-annotate.c               |  11 -
 tools/perf/builtin-kmem.c                   |  51 +-
 tools/perf/tests/hists_common.c             |   5 +-
 tools/perf/tests/symbols.c                  |  62 ++-
 tools/perf/tests/vmlinux-kallsyms.c         | 185 +++----
 tools/perf/ui/browser.c                     |   3 +
 tools/perf/ui/browsers/hists.c              |   3 +-
 tools/perf/ui/browsers/map.c                |  62 ++-
 tools/perf/util/auxtrace.c                  | 109 ++--
 tools/perf/util/db-export.c                 |   4 +-
 tools/perf/util/dso.c                       |  50 +-
 tools/perf/util/dso.h                       |  68 +--
 tools/perf/util/intel-pt.c                  |  74 ++-
 tools/perf/util/libbfd.c                    |   3 +-
 tools/perf/util/machine.c                   |  30 +-
 tools/perf/util/map.c                       |  25 +-
 tools/perf/util/map.h                       |   8 +-
 tools/perf/util/probe-event.c               |  98 ++--
 tools/perf/util/symbol-elf.c                |  12 +-
 tools/perf/util/symbol.c                    | 576 +++++++++++++-------
 tools/perf/util/symbol.h                    |  72 ++-
 tools/perf/util/symbol_fprintf.c            |  25 +-
 23 files changed, 947 insertions(+), 623 deletions(-)
tools/perf/arch/powerpc/util/sym-handling.c +24 -10
index d73bfdcdbec1..0ec327930b0a 100644
@@ -115,13 +115,27 @@ void arch__fix_tev_from_maps(struct perf_probe_event *pev,
 }
 
 #ifdef HAVE_LIBELF_SUPPORT
+struct post_process_probe_trace_events_args {
+	struct perf_probe_event *pev;
+	struct probe_trace_event *tev;
+	struct map *map;
+};
+
+static int post_process_probe_trace_events_cb(struct symbol *sym, void *data)
+{
+	struct post_process_probe_trace_events_args *args = data;
+
+	if (map__unmap_ip(args->map, symbol__start(sym)) == args->tev->point.address) {
+		arch__fix_tev_from_maps(args->pev, args->tev, args->map, sym);
+		return 1;
+	}
+	return 0;
+}
+
 void arch__post_process_probe_trace_events(struct perf_probe_event *pev,
 					   int ntevs)
 {
-	struct probe_trace_event *tev;
 	struct map *map;
-	struct symbol *sym = NULL;
-	struct rb_node *tmp;
 	int i = 0;
 
 	map = get_target_map(pev->target, pev->nsi, pev->uprobes);
@@ -129,13 +143,13 @@ void arch__post_process_probe_trace_events(struct perf_probe_event *pev,
 		return;
 
 	for (i = 0; i < ntevs; i++) {
-		tev = &pev->tevs[i];
-		map__for_each_symbol(map, sym, tmp) {
-			if (map__unmap_ip(map, symbol__start(sym)) == tev->point.address) {
-				arch__fix_tev_from_maps(pev, tev, map, sym);
-				break;
-			}
-		}
+		struct post_process_probe_trace_events_args args = {
+			.pev = pev,
+			.tev = &pev->tevs[i],
+			.map = map,
+		};
+
+		map__for_each_symbol(map, post_process_probe_trace_events_cb, &args);
 	}
 }
 #endif /* HAVE_LIBELF_SUPPORT */
tools/perf/builtin-annotate.c +0 -11
index 89e4e8058890..7797a36598e5 100644
@@ -258,17 +258,6 @@ static int add_sample(struct perf_sample *sample,
 	    (al->sym == NULL ||
 	     strcmp(ann->sym_hist_filter, symbol__name(al->sym)) != 0)) {
 		/* We're only interested in a symbol named sym_hist_filter */
-		/*
-		 * FIXME: why isn't this done in the symbol_filter when loading
-		 * the DSO?
-		 */
-		if (al->sym != NULL) {
-			struct dso *dso = map__dso(al->map);
-
-			rb_erase_cached(&al->sym->rb_node, dso__symbols(dso));
-			symbol__delete(al->sym);
-			dso__reset_find_symbol_cache(dso);
-		}
 		return 0;
 	}
 
tools/perf/builtin-kmem.c +29 -22

72 lines not on this page · raw message

tools/perf/tests/hists_common.c +4 -1
index 0db3ea9cf62e..beb147e2a654 100644
@@ -146,7 +146,10 @@ struct machine *setup_fake_machine(struct machines *machines)
 				goto out;
 			}
 
-			symbols__insert(dso__symbols(dso), sym);
+			if (symbols__insert(dso__symbols(dso), sym)) {
+				dso__put(dso);
+				goto out;
+			}
 		}
 
 		dso__put(dso);
tools/perf/tests/symbols.c +37 -25

79 lines not on this page · raw message

tools/perf/tests/vmlinux-kallsyms.c +93 -92

221 lines not on this page · raw message

tools/perf/ui/browser.c +3 -0
index 321187b204d3..c7451f640d1d 100644
@@ -622,6 +622,9 @@ static int ui_browser__color_config(const char *var, const char *value,
 
 void ui_browser__argv_seek(struct ui_browser *browser, off_t offset, int whence)
 {
+	if (browser->nr_entries == 0)
+		return;
+
 	switch (whence) {
 	case SEEK_SET:
 		browser->top = browser->entries;
tools/perf/ui/browsers/hists.c +2 -1
index 1f1a877f667e..5147b7dca256 100644
@@ -2531,7 +2531,8 @@ static struct symbol *symbol__new_unresolved(u64 addr, struct map *map)
 			return NULL;
 		}
 
-		dso__insert_symbol(map__dso(map), sym);
+		if (dso__insert_symbol(map__dso(map), sym))
+			return NULL;
 	}
 
 	return sym;
tools/perf/ui/browsers/map.c +41 -21

92 lines not on this page · raw message

tools/perf/util/auxtrace.c +72 -37

133 lines not on this page · raw message

tools/perf/util/db-export.c +2 -2
index 3c46280ba273..f25c7a06aa4f 100644
@@ -289,8 +289,8 @@ static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
 
 		if (!al->sym) {
 			al->sym = symbol__new(al->addr, 0, 0, 0, "unknown");
-			if (al->sym)
-				dso__insert_symbol(dso, al->sym);
+			if (al->sym && dso__insert_symbol(dso, al->sym))
+				al->sym = NULL;
 		}
 
 		if (al->sym) {
tools/perf/util/dso.c +26 -24

85 lines not on this page · raw message

tools/perf/util/dso.h +11 -57

131 lines not on this page · raw message

tools/perf/util/intel-pt.c +45 -29

94 lines not on this page · raw message

tools/perf/util/libbfd.c +2 -1
index a5e2fbf774e9..5ed221855132 100644
@@ -453,7 +453,8 @@ int dso__load_bfd_symbols(struct dso *dso, const char *debugfile)
 		if (!symbol)
 			goto out_free;
 
-		symbols__insert(dso__symbols(dso), symbol);
+		if (symbols__insert(dso__symbols(dso), symbol))
+			goto out_free;
 	}
 #ifdef bfd_get_section
 #undef bfd_asymbol_section
tools/perf/util/machine.c +18 -12
index 5c8bf5779c47..7c83321eea76 100644
@@ -817,7 +817,7 @@ static int machine__process_ksymbol_register(struct machine *machine,
 		err = -ENOMEM;
 		goto out;
 	}
-	dso__insert_symbol(dso, sym);
+	err = dso__insert_symbol(dso, sym);
 out:
 	map__put(map);
 	dso__put(dso);
@@ -1120,29 +1120,35 @@ int machine__create_extra_kernel_map(struct machine *machine,
 	return err;
 }
 
-static u64 find_entry_trampoline(struct dso *dso)
+static int find_entry_trampoline_cb(struct symbol *sym, void *data)
 {
 	/* Duplicates are removed so lookup all aliases */
-	const char *syms[] = {
+	static const char * const syms[] = {
 		"_entry_trampoline",
 		"__entry_trampoline_start",
 		"entry_SYSCALL_64_trampoline",
 	};
-	struct symbol *sym = dso__first_symbol(dso);
-	unsigned int i;
+	u64 *addr = data;
 
-	for (; sym; sym = dso__next_symbol(sym)) {
-		if (symbol__binding(sym) != STB_GLOBAL)
-			continue;
-		for (i = 0; i < ARRAY_SIZE(syms); i++) {
-			if (!strcmp(symbol__name(sym), syms[i]))
-				return symbol__start(sym);
+	if (symbol__binding(sym) != STB_GLOBAL)
+		return 0;
+	for (unsigned int i = 0; i < ARRAY_SIZE(syms); i++) {
+		if (!strcmp(symbol__name(sym), syms[i])) {
+			*addr = symbol__start(sym);
+			return 1;
 		}
 	}
-
 	return 0;
 }
 
+static u64 find_entry_trampoline(struct dso *dso)
+{
+	u64 addr = 0;
+
+	dso__for_each_symbol(dso, find_entry_trampoline_cb, &addr);
+	return addr;
+}
+
 /*
  * These values can be used for kernels that do not have symbols for the entry
  * trampolines in kallsyms.
tools/perf/util/map.c +10 -15
index 8be5606c940e..4697f79b667e 100644
@@ -314,27 +314,22 @@ void map__put(struct map *map)
 
 void map__fixup_start(struct map *map)
 {
-	struct dso *dso = map__dso(map);
-	struct rb_root_cached *symbols = dso__symbols(dso);
-	struct rb_node *nd = rb_first_cached(symbols);
-
-	if (nd != NULL) {
-		struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
+	struct symbols *symbols = dso__symbols(map__dso(map));
 
-		map__set_start(map, symbol__start(sym));
-	}
+	symbols__sort_read_lock(symbols);
+	if (symbols->cnt > 0)
+		map__set_start(map, symbol__start(symbols->symbols[0]));
+	up_read(&symbols->lock);
 }
 
 void map__fixup_end(struct map *map)
 {
-	struct dso *dso = map__dso(map);
-	struct rb_root_cached *symbols = dso__symbols(dso);
-	struct rb_node *nd = rb_last(&symbols->rb_root);
+	struct symbols *symbols = dso__symbols(map__dso(map));
 
-	if (nd != NULL) {
-		struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
-		map__set_end(map, symbol__end(sym));
-	}
+	symbols__sort_read_lock(symbols);
+	if (symbols->cnt > 0)
+		map__set_end(map, symbol__end(symbols->symbols[symbols->cnt - 1]));
+	up_read(&symbols->lock);
 }
 
 #define DSO__DELETED "(deleted)"
tools/perf/util/map.h +4 -4
index 0d1b8a1cd84c..805a9a0d9365 100644
@@ -146,12 +146,12 @@ struct thread;
 /* map__for_each_symbol - iterate over the symbols in the given map
  *
  * @map: the 'struct map *' in which symbols are iterated
- * @pos: the 'struct symbol *' to use as a loop cursor
- * @n: the 'struct rb_node *' to use as a temporary storage
+ * @cb: callback function invoked for each symbol
+ * @data: opaque data pointer passed to @cb
  * Note: caller must ensure map->dso is not NULL (map is loaded).
  */
-#define map__for_each_symbol(map, pos, n)	\
-	dso__for_each_symbol(map__dso(map), pos, n)
+#define map__for_each_symbol(map, cb, data)	\
+	dso__for_each_symbol(map__dso(map), cb, data)
 
 /* map__for_each_symbol_with_name - iterate over the symbols in the given map
  *                                  that have the given name
tools/perf/util/probe-event.c +58 -40

125 lines not on this page · raw message

tools/perf/util/symbol-elf.c +8 -4
index 96e21b9b41e2..2c8088184568 100644
@@ -700,7 +700,8 @@ static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf,
 		sym = symbol__new(shdr.sh_offset + i, shdr.sh_entsize, STB_GLOBAL, STT_FUNC, buf);
 		if (!sym)
 			goto out;
-		symbols__insert(dso__symbols(dso), sym);
+		if (symbols__insert(dso__symbols(dso), sym))
+			goto out;
 	}
 	err = 0;
 out:
@@ -754,7 +755,8 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
 	plt_sym = symbol__new(shdr_plt.sh_offset, plt_header_size, STB_GLOBAL, STT_FUNC, ".plt");
 	if (!plt_sym)
 		goto out_elf_end;
-	symbols__insert(dso__symbols(dso), plt_sym);
+	if (symbols__insert(dso__symbols(dso), plt_sym))
+		goto out_elf_end;
 
 	/* Only x86 has .plt.got */
 	if (machine_is_x86(ehdr.e_machine) &&
@@ -876,7 +878,8 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
 			goto out_elf_end;
 
 		plt_offset += plt_entry_size;
-		symbols__insert(dso__symbols(dso), f);
+		if (symbols__insert(dso__symbols(dso), f))
+			goto out_elf_end;
 		++nr;
 	}
 
@@ -1940,7 +1943,8 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 
 		arch__sym_update(f, &sym);
 
-		__symbols__insert(dso__symbols(curr_dso), f);
+		if (symbols__insert(dso__symbols(curr_dso), f))
+			goto out_elf_end;
 		nr++;
 	}
 	dso__put(curr_dso);
tools/perf/util/symbol.c +388 -188

794 lines not on this page · raw message

tools/perf/util/symbol.h +52 -20

117 lines not on this page · raw message

tools/perf/util/symbol_fprintf.c +18 -7
index 8c131325e989..8453a3554cd4 100644
@@ -59,15 +59,26 @@ size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
 	return __symbol__fprintf_symname_offs(sym, NULL, false, false, fp);
 }
 
+struct fprintf_symbols_by_name_args {
+	FILE *fp;
+	size_t ret;
+};
+
+static int fprintf_symbols_by_name_cb(struct symbol *pos, void *data)
+{
+	struct fprintf_symbols_by_name_args *args = data;
+
+	args->ret += fprintf(args->fp, "%s\n", symbol__name(pos));
+	return 0;
+}
+
 size_t dso__fprintf_symbols_by_name(struct dso *dso,
 				    FILE *fp)
 {
-	size_t ret = 0;
-
-	for (size_t i = 0; i < dso__symbol_names_len(dso); i++) {
-		struct symbol *pos = dso__symbol_names(dso)[i];
+	struct fprintf_symbols_by_name_args args = {
+		.fp = fp,
+	};
 
-		ret += fprintf(fp, "%s\n", symbol__name(pos));
-	}
-	return ret;
+	dso__for_each_symbol_by_name(dso, fprintf_symbols_by_name_cb, &args);
+	return args.ret;
 }
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#82 lore raw

Re: [PATCH v3 15/20] perf symbol: Switch backing storage from rbtree to struct symbols array

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] perf symbol: missing end address tie-breaker in symbols__sort_cmp() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] perf symbol: missing end address tie-breaker in symbols__sort_cmp()
--

commit 0573604acc8bf2d5351a71f4fe63cacfd8d508e5
Author: Ian Rogers <[email protected]>

perf symbol: Switch backing storage from rbtree to struct symbols array

This commit replaces the embedded rbtree node in struct symbol with a locked, lazily-sorted pointer array in struct symbols and updates symbol iteration and lookup callers to use it.

Quoted 5 lines
tools/perf/util/symbol.c +0 -0
index ec2a78873b903..66c5bd0386362 100644
@@ -213,57 +213,153 @@ static int choose_best_symbol(struct symbol *syma, struct symbol *symb)

[ ... ]

Quoted 14 lines
tools/perf/util/symbol.c +14 -0
+static inline bool symbol__less_or_equal(const struct symbol *a, const struct symbol *b)
+{
+	return symbol__start(a) <= symbol__start(b);
+}
+
+static int symbols__sort_cmp(const void *vlhs, const void *vrhs)
+{
+	const struct symbol *lhs = *((const struct symbol **)vlhs);
+	const struct symbol *rhs = *((const struct symbol **)vrhs);
+
+	if (symbol__start(lhs) != symbol__start(rhs))
+		return symbol__start(lhs) < symbol__start(rhs) ? -1 : 1;
+	return 0;
+}

[ ... ]

Quoted 1 line
tools/perf/util/symbol.c +0 -0
@@ -397,97 +495,94 @@ static void symbol__set_idle(struct symbol *sym, bool idle)

[ ... ]

Quoted 37 lines
tools/perf/util/symbol.c +19 -11
-static struct symbol *symbols__find(struct rb_root_cached *symbols, u64 ip)
+/*
+ * Binary search @symbols->symbols for a symbol covering @ip. Lazily sorts
+ * @symbols under @symbols->lock if unsorted.
+ */
+static struct symbol *symbols__find(struct symbols *symbols, u64 ip)
 {
-	struct rb_node *n;
+	int low, high;
+	struct symbol *res = NULL;
 
 	if (symbols == NULL)
 		return NULL;
 
-	n = symbols->rb_root.rb_node;
+	symbols__sort_read_lock(symbols);
 
-	while (n) {
-		struct symbol *s = rb_entry(n, struct symbol, rb_node);
+	low = 0;
+	high = (int)symbols->cnt - 1;
+	while (low <= high) {
+		int mid = low + (high - low) / 2;
+		struct symbol *s = symbols->symbols[mid];
+		u64 start = symbol__start(s);
+		u64 end = symbol__end(s);
 
-		if (ip < symbol__start(s))
-			n = n->rb_left;
-		else if (ip > symbol__end(s) || (ip == symbol__end(s) && ip != symbol__start(s)))
-			n = n->rb_right;
-		else
-			return s;
+		if (ip < start)
+			high = mid - 1;
+		else if (ip > end || (ip == end && ip != start))
+			low = mid + 1;

tools/perf/util/symbol.c static void symbol__set_idle(struct symbol *sym, bool idle) · line 569

Quoted 10 lines
tools/perf/util/symbol.c +6 -1
+		else {
+			res = s;
+			break;
+		}
 	}
+	up_read(&symbols->lock);
 
-	return NULL;
+	return res;
 }

[Severity: Medium]
Should symbol__less_or_equal() and symbols__sort_cmp() also compare symbol__end() when two symbols share the same start address?

Because they only compare symbol__start(), a sized symbol [X, X + L) can precede a zero-length alias [X, X) at the same start address (for example when symbol_conf.allow_aliases is set or when both .symtab and .dynsym define symbols at X).

If symbols__find() looks up an ip in (X, X + L) and mid lands on the later [X, X) entry, ip > end advances low = mid + 1, skipping the covering [X, X + L) entry to its left and returning NULL.

In addition, during ELF loading symbols__fixup_end() runs before symbols__fixup_duplicate() and extends a trailing [X, X) entry up to the next distinct symbol address Y, inflating its size before choose_best_symbol() compares lengths, and map__fixup_end() can pick the shorter symbol at symbols->symbols[symbols->cnt - 1] as the map end.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
author reviewer
#83 lore raw

PATCH v3 16/20 perf symbol: Optimize symbol sorting with natural-run merge sort and binary insertion

In reply to Ian Rogers

ELF .symtab and .dynsym sections are typically already mostly ordered by address in ascending runs, and dynamic symbol resolution (dso__insert_symbol()) frequently inserts one or a few symbols into an already-sorted DSO symbol table. Invoking full-array qsort() on every sort after incremental insertions or across pre-ordered ELF runs spends avoidable CPU time in symbol comparisons.

Speed up symbol array sorting and out-of-order symbol insertions:

- Track the sorted prefix length (@sorted_cnt) in struct symbols and replace full-array qsort() with a natural-run merge sort (symbols__sort_range_with_tmp()) and galloping binary-search merge (symbols__merge_runs()), plus an O(N) in-place binary insertion fast path when <= 8 symbols are appended.
- Insert directly into sorted position in dso__insert_symbol() when symbols->sorted is true so dynamic single-symbol insertions maintain the sorted invariant without triggering full re-sorts.

Using 'perf bench internals symbols':

Before:

    Average DSO load + sort: 6.242 msec (+- 0.057 msec)
    Average bulk insert + lookup: 1404.150 usec (+- 18.752 usec)
    Average append + lookup: 40429.750 usec (+- 507.578 usec)
  After (-7.0% load, -13.9% bulk, -99.6% append):
    Average DSO load + sort: 5.808 msec (+- 0.047 msec)
    Average bulk insert + lookup: 1209.250 usec (+- 18.446 usec)
    Average append + lookup: 152.900 usec (+- 0.736 usec)
---
 tools/perf/util/symbol.c | 486 ++++++++++++++++++++++++++++++++++-----
 tools/perf/util/symbol.h |  11 +-
 2 files changed, 442 insertions(+), 55 deletions(-)
tools/perf/util/symbol.c +432 -54
index 66c5bd038636..9d211a745049 100644
@@ -219,6 +219,7 @@ void symbols__init(struct symbols *symbols)
 	symbols->symbols = NULL;
 	symbols->symbols_by_name = NULL;
 	symbols->cnt = 0;
+	symbols->sorted_cnt = 0;
 	symbols->allocated = 0;
 	symbols->num_by_name = 0;
 	symbols->sorted = true;
@@ -233,6 +234,7 @@ static void symbols__delete_locked(struct symbols *symbols)
 	zfree(&symbols->symbols);
 	zfree(&symbols->symbols_by_name);
 	symbols->cnt = 0;
+	symbols->sorted_cnt = 0;
 	symbols->allocated = 0;
 	symbols->num_by_name = 0;
 	symbols->sorted = true;
@@ -257,37 +259,311 @@ static inline bool symbol__less_or_equal(const struct symbol *a, const struct sy
 	return symbol__start(a) <= symbol__start(b);
 }
 
-static int symbols__sort_cmp(const void *vlhs, const void *vrhs)
+static unsigned int symbols__upper_bound(struct symbol **arr, unsigned int len,
+					 const struct symbol *sym)
 {
-	const struct symbol *lhs = *((const struct symbol **)vlhs);
-	const struct symbol *rhs = *((const struct symbol **)vrhs);
+	unsigned int low = 0, high = len;
 
-	if (symbol__start(lhs) != symbol__start(rhs))
-		return symbol__start(lhs) < symbol__start(rhs) ? -1 : 1;
-	return 0;
+	while (low < high) {
+		unsigned int mid = low + (high - low) / 2;
+
+		if (symbol__less_or_equal(arr[mid], sym))
+			low = mid + 1;
+		else
+			high = mid;
+	}
+	return low;
+}
+
+static unsigned int symbols__lower_bound(struct symbol **arr, unsigned int len,
+					 const struct symbol *sym)
+{
+	unsigned int low = 0, high = len;
+
+	while (low < high) {
+		unsigned int mid = low + (high - low) / 2;
+
+		if (!symbol__less_or_equal(sym, arr[mid]))
+			low = mid + 1;
+		else
+			high = mid;
+	}
+	return low;
+}
+
+static void symbols__insertion_sort_range(struct symbol **arr,
+					  unsigned int start,
+					  unsigned int end)
+{
+	for (unsigned int i = start + 1; i < end; i++) {
+		struct symbol *sym = arr[i];
+
+		if (!symbol__less_or_equal(arr[i - 1], sym)) {
+			unsigned int pos = start +
+				symbols__upper_bound(&arr[start], i - start, sym);
+
+			memmove(&arr[pos + 1], &arr[pos],
+				(i - pos) * sizeof(struct symbol *));
+			arr[pos] = sym;
+		}
+	}
+}
+
+static void symbols__merge_runs(struct symbol **arr,
+				unsigned int left,
+				unsigned int mid,
+				unsigned int right,
+				struct symbol **tmp)
+{
+	unsigned int first_left, last_left, left_len, right_len;
+	unsigned int i, j, dst;
+
+	if (left >= mid || mid >= right)
+		return;
+	if (symbol__less_or_equal(arr[mid - 1], arr[mid]))
+		return;
+
+	first_left = left + symbols__upper_bound(&arr[left], mid - left, arr[mid]);
+	right_len = right - mid;
+
+	if (mid - first_left <= right_len) {
+		left_len = mid - first_left;
+		memcpy(tmp, &arr[first_left], left_len * sizeof(struct symbol *));
+		i = 0;
+		j = mid;
+		dst = first_left;
+		while (i < left_len && j < right) {
+			if (symbol__less_or_equal(tmp[i], arr[j])) {
+				arr[dst++] = tmp[i++];
+			} else {
+				arr[dst++] = arr[j++];
+				if (right - j >= 16 &&
+				    (right - j) > 8 * (left_len - i) &&
+				    !symbol__less_or_equal(tmp[i], arr[j])) {
+					unsigned int step =
+						symbols__lower_bound(&arr[j], right - j, tmp[i]);
+
+					if (step > 0) {
+						memmove(&arr[dst], &arr[j],
+							step * sizeof(struct symbol *));
+						dst += step;
+						j += step;
+					}
+				}
+			}
+		}
+		if (i < left_len)
+			memcpy(&arr[dst], &tmp[i], (left_len - i) * sizeof(struct symbol *));
+		return;
+	}
+
+	last_left = first_left +
+		    symbols__upper_bound(&arr[first_left], mid - first_left, arr[right - 1]);
+	left_len = last_left - first_left;
+
+	memcpy(tmp, &arr[mid], right_len * sizeof(struct symbol *));
+	if (last_left < mid) {
+		memmove(&arr[last_left + right_len], &arr[last_left],
+			(mid - last_left) * sizeof(struct symbol *));
+	}
+
+	if (first_left == last_left) {
+		memcpy(&arr[first_left], tmp, right_len * sizeof(struct symbol *));
+		return;
+	}
+
+	i = last_left;
+	j = right_len;
+	dst = last_left + right_len;
+	while (i > first_left && j > 0) {
+		if (symbol__less_or_equal(arr[i - 1], tmp[j - 1])) {
+			arr[--dst] = tmp[--j];
+		} else {
+			arr[--dst] = arr[--i];
+			if (i - first_left >= 16 &&
+			    (i - first_left) > 8 * j &&
+			    !symbol__less_or_equal(arr[i - 1], tmp[j - 1])) {
+				unsigned int pos = first_left +
+					symbols__upper_bound(&arr[first_left],
+							     i - first_left,
+							     tmp[j - 1]);
+				unsigned int step = i - pos;
+
+				if (step > 0) {
+					dst -= step;
+					i = pos;
+					memmove(&arr[dst], &arr[pos],
+						step * sizeof(struct symbol *));
+				}
+			}
+		}
+	}
+	if (j > 0)
+		memcpy(&arr[first_left], tmp, j * sizeof(struct symbol *));
+}
+
+struct symbol_run {
+	unsigned int start;
+	unsigned int len;
+};
+
+static void symbols__sort_range_with_tmp(struct symbol **arr, unsigned int n,
+					 struct symbol **tmp)
+{
+	struct symbol_run runs[64];
+	unsigned int nr_runs = 0;
+	unsigned int i = 0;
+
+	if (n <= 32) {
+		symbols__insertion_sort_range(arr, 0, n);
+		return;
+	}
+
+	while (i < n) {
+		unsigned int start = i++;
+
+		if (i < n) {
+			if (symbol__less_or_equal(arr[i - 1], arr[i])) {
+				while (i < n && symbol__less_or_equal(arr[i - 1], arr[i]))
+					i++;
+			} else {
+				while (i < n && !symbol__less_or_equal(arr[i - 1], arr[i]))
+					i++;
+				for (unsigned int lo = start, hi = i - 1; lo < hi; lo++, hi--) {
+					struct symbol *swap = arr[lo];
+
+					arr[lo] = arr[hi];
+					arr[hi] = swap;
+				}
+			}
+		}
+
+		if (i - start < 32 && i < n) {
+			i = min(start + 32U, n);
+			symbols__insertion_sort_range(arr, start, i);
+		}
+
+		runs[nr_runs++] = (struct symbol_run){
+			.start = start,
+			.len = i - start,
+		};
+
+		while (nr_runs > 1) {
+			unsigned int r = nr_runs - 2;
+
+			if (nr_runs >= 3 &&
+			    runs[nr_runs - 3].len <=
+			    runs[nr_runs - 2].len + runs[nr_runs - 1].len) {
+				if (runs[nr_runs - 3].len < runs[nr_runs - 1].len)
+					r = nr_runs - 3;
+			} else if (runs[nr_runs - 2].len > runs[nr_runs - 1].len &&
+				   nr_runs < ARRAY_SIZE(runs)) {
+				break;
+			}
+
+			symbols__merge_runs(arr, runs[r].start,
+					    runs[r + 1].start,
+					    runs[r + 1].start + runs[r + 1].len,
+					    tmp);
+			runs[r].len += runs[r + 1].len;
+			if (r + 2 < nr_runs)
+				runs[r + 1] = runs[r + 2];
+			nr_runs--;
+		}
+	}
+
+	while (nr_runs > 1) {
+		unsigned int r = nr_runs - 2;
+
+		if (nr_runs >= 3 && runs[nr_runs - 3].len < runs[nr_runs - 1].len)
+			r = nr_runs - 3;
+		symbols__merge_runs(arr, runs[r].start,
+				    runs[r + 1].start,
+				    runs[r + 1].start + runs[r + 1].len,
+				    tmp);
+		runs[r].len += runs[r + 1].len;
+		if (r + 2 < nr_runs)
+			runs[r + 1] = runs[r + 2];
+		nr_runs--;
+	}
 }
 
 static void symbols__sort_locked(struct symbols *symbols)
 {
-	if (symbols->sorted)
+	unsigned int m = symbols->sorted_cnt;
+	unsigned int k;
+
+	if (symbols->sorted && symbols->sorted_cnt == symbols->cnt)
+		return;
+
+	if (m > symbols->cnt)
+		m = 0;
+	k = symbols->cnt - m;
+
+	if (k == 0) {
+		symbols->sorted = true;
 		return;
+	}
 
-	if (symbols->cnt > 1) {
-		qsort(symbols->symbols, symbols->cnt, sizeof(struct symbol *),
-		      symbols__sort_cmp);
+	/*
+	 * When only a few symbols have been appended since the last sort
+	 * (including single-symbol insertions), insert them directly at
+	 * their binary-searched sorted positions in O(N) without allocating.
+	 */
+	if (k <= 8 || symbols->cnt <= 64) {
+		symbols__insertion_sort_range(symbols->symbols, 0, symbols->cnt);
+		symbols->sorted_cnt = symbols->cnt;
+		symbols->sorted = true;
+		return;
 	}
+
+	if (symbols->sorted) {
+		/*
+		 * Both [0 .. m) and the sorted tail [m .. cnt) are already
+		 * sorted; merge them into a single sorted run.
+		 */
+		struct symbol **tmp = malloc(min(m, k) * sizeof(struct symbol *));
+
+		if (tmp) {
+			symbols__merge_runs(symbols->symbols, 0, m, symbols->cnt, tmp);
+			free(tmp);
+		} else {
+			symbols__insertion_sort_range(symbols->symbols, 0, symbols->cnt);
+		}
+	} else if (m <= 1) {
+		struct symbol **tmp = malloc(((symbols->cnt + 1) / 2) * sizeof(struct symbol *));
+
+		if (tmp) {
+			symbols__sort_range_with_tmp(symbols->symbols, symbols->cnt, tmp);
+			free(tmp);
+		} else {
+			symbols__insertion_sort_range(symbols->symbols, 0, symbols->cnt);
+		}
+	} else {
+		struct symbol **tmp = malloc(k * sizeof(struct symbol *));
+
+		if (tmp) {
+			symbols__sort_range_with_tmp(&symbols->symbols[m], k, tmp);
+			symbols__merge_runs(symbols->symbols, 0, m, symbols->cnt, tmp);
+			free(tmp);
+		} else {
+			symbols__insertion_sort_range(symbols->symbols, 0, symbols->cnt);
+		}
+	}
+
+	symbols->sorted_cnt = symbols->cnt;
 	symbols->sorted = true;
 }
 
 /*
  * Acquire @symbols->lock for reading after ensuring @symbols->symbols is
- * sorted.
+ * fully sorted into a single contiguous run.
  */
 void symbols__sort_read_lock(struct symbols *symbols)
 	NO_THREAD_SAFETY_ANALYSIS
 {
 	down_read(&symbols->lock);
-	while (!symbols->sorted) {
+	while (!symbols->sorted || symbols->sorted_cnt < symbols->cnt) {
 		up_read(&symbols->lock);
 		down_write(&symbols->lock);
 		symbols__sort_locked(symbols);
@@ -336,6 +612,7 @@ void symbols__fixup_duplicate(struct symbols *symbols)
 		}
 	}
 	symbols->cnt = write_idx + 1;
+	symbols->sorted_cnt = symbols->cnt;
 	zfree(&symbols->symbols_by_name);
 	symbols->num_by_name = 0;
 	symbols->sorted_by_name = false;
@@ -377,33 +654,36 @@ void symbols__fixup_end(struct symbols *symbols, bool is_kallsyms)
 		 * like in:
 		 *   ffffffffc1937000 T hdmi_driver_init  [snd_hda_codec_hdmi]
 		 */
-		if (symbol__end(prev) == symbol__start(prev)) {
+		if (symbol__end(prev) == symbol__start(prev) &&
+		    symbol__start(prev) != symbol__start(curr)) {
 			const char *prev_mod;
 			const char *curr_mod;
 
 			if (!is_kallsyms) {
 				symbol__set_end(prev, symbol__start(curr));
-				continue;
-			}
-
-			prev_mod = strchr(symbol__name(prev), '[');
-			curr_mod = strchr(symbol__name(curr), '[');
+			} else {
+				prev_mod = strchr(symbol__name(prev), '[');
+				curr_mod = strchr(symbol__name(curr), '[');
+
+				/* Last kernel/module symbol mapped to end of page */
+				if (!prev_mod != !curr_mod) {
+					symbol__set_end(prev,
+							roundup(symbol__end(prev) + 4096, 4096));
+				/* Last symbol in the previous module */
+				} else if (prev_mod && strcmp(prev_mod, curr_mod)) {
+					symbol__set_end(prev,
+							roundup(symbol__end(prev) + 4096, 4096));
+				} else {
+					symbol__set_end(prev, symbol__start(curr));
+				}
 
-			/* Last kernel/module symbol mapped to end of page */
-			if (!prev_mod != !curr_mod)
-				symbol__set_end(prev, roundup(symbol__end(prev) + 4096, 4096));
-			/* Last symbol in the previous module */
-			else if (prev_mod && strcmp(prev_mod, curr_mod))
-				symbol__set_end(prev, roundup(symbol__end(prev) + 4096, 4096));
-			else
-				symbol__set_end(prev, symbol__start(curr));
+				/* The next module can start within that page */
+				if (symbol__end(prev) > symbol__start(curr))
+					symbol__set_end(prev, symbol__start(curr));
 
-			/* The next module can start within that page */
-			if (symbol__end(prev) > symbol__start(curr))
-				symbol__set_end(prev, symbol__start(curr));
-
-			pr_debug4("%s sym:%s end:%#" PRIx64 "\n",
-				  __func__, symbol__name(prev), symbol__end(prev));
+				pr_debug4("%s sym:%s end:%#" PRIx64 "\n",
+					  __func__, symbol__name(prev), symbol__end(prev));
+			}
 		}
 	}
 
@@ -511,9 +791,15 @@ static int __symbols__insert(struct symbols *symbols, struct symbol *sym)
 		symbols->allocated = to_allocate;
 	}
 
-	if (symbols->sorted && symbols->cnt > 0 &&
-	    !symbol__less_or_equal(symbols->symbols[symbols->cnt - 1], sym))
+	if (symbols->sorted && symbols->sorted_cnt == symbols->cnt) {
+		if (symbols->cnt == 0 ||
+		    symbol__less_or_equal(symbols->symbols[symbols->cnt - 1], sym))
+			symbols->sorted_cnt = symbols->cnt + 1;
+		else
+			symbols->sorted = false;
+	} else {
 		symbols->sorted = false;
+	}
 
 	symbols->symbols[symbols->cnt++] = sym;
 	zfree(&symbols->symbols_by_name);
@@ -522,6 +808,69 @@ static int __symbols__insert(struct symbols *symbols, struct symbol *sym)
 	return 0;
 }
 
+static int __symbols__insert_sorted(struct symbols *symbols, struct symbol *sym)
+{
+	unsigned int idx;
+
+	if (!symbols->sorted)
+		return __symbols__insert(symbols, sym);
+
+	if (symbols->cnt == symbols->allocated) {
+		unsigned int to_allocate = symbols->allocated ? symbols->allocated * 2 : 32;
+		struct symbol **temp = realloc(symbols->symbols,
+					       sizeof(struct symbol *) * to_allocate);
+
+		if (!temp) {
+			symbol__delete(sym);
+			return -ENOMEM;
+		}
+		symbols->symbols = temp;
+		symbols->allocated = to_allocate;
+	}
+
+	if (symbols->sorted_cnt == symbols->cnt) {
+		if (symbols->cnt == 0 ||
+		    symbol__less_or_equal(symbols->symbols[symbols->cnt - 1], sym)) {
+			symbols->symbols[symbols->cnt++] = sym;
+			symbols->sorted_cnt = symbols->cnt;
+		} else if (symbols->cnt < 64) {
+			idx = symbols__upper_bound(symbols->symbols, symbols->cnt, sym);
+			memmove(&symbols->symbols[idx + 1], &symbols->symbols[idx],
+				(symbols->cnt - idx) * sizeof(struct symbol *));
+			symbols->symbols[idx] = sym;
+			symbols->cnt++;
+			symbols->sorted_cnt = symbols->cnt;
+		} else {
+			/*
+			 * Start a sorted tail buffer at symbols[sorted_cnt .. cnt)
+			 * so out-of-order insertions do not memmove the entire
+			 * primary sorted prefix on every call.
+			 */
+			symbols->symbols[symbols->cnt++] = sym;
+		}
+	} else {
+		unsigned int tail_cnt = symbols->cnt - symbols->sorted_cnt;
+
+		idx = symbols->sorted_cnt +
+		      symbols__upper_bound(&symbols->symbols[symbols->sorted_cnt],
+					   tail_cnt, sym);
+		memmove(&symbols->symbols[idx + 1], &symbols->symbols[idx],
+			(symbols->cnt - idx) * sizeof(struct symbol *));
+		symbols->symbols[idx] = sym;
+		symbols->cnt++;
+		tail_cnt++;
+
+		if (tail_cnt >= 256 &&
+		    (size_t)tail_cnt * tail_cnt >= symbols->sorted_cnt)
+			symbols__sort_locked(symbols);
+	}
+
+	zfree(&symbols->symbols_by_name);
+	symbols->num_by_name = 0;
+	symbols->sorted_by_name = false;
+	return 0;
+}
+
 int symbols__insert(struct symbols *symbols, struct symbol *sym)
 {
 	int ret;
@@ -532,36 +881,50 @@ int symbols__insert(struct symbols *symbols, struct symbol *sym)
 	return ret;
 }
 
+static struct symbol *symbols__find_range(struct symbol **arr, unsigned int cnt, u64 ip)
+{
+	int low = 0, high = (int)cnt - 1;
+
+	while (low <= high) {
+		int mid = low + (high - low) / 2;
+		struct symbol *s = arr[mid];
+		u64 start = symbol__start(s);
+		u64 end = symbol__end(s);
+
+		if (ip < start)
+			high = mid - 1;
+		else if (ip > end || (ip == end && ip != start))
+			low = mid + 1;
+		else
+			return s;
+	}
+	return NULL;
+}
+
 /*
  * Binary search @symbols->symbols for a symbol covering @ip. Lazily sorts
  * @symbols under @symbols->lock if unsorted.
  */
 static struct symbol *symbols__find(struct symbols *symbols, u64 ip)
 {
-	int low, high;
 	struct symbol *res = NULL;
 
 	if (symbols == NULL)
 		return NULL;
 
-	symbols__sort_read_lock(symbols);
-
-	low = 0;
-	high = (int)symbols->cnt - 1;
-	while (low <= high) {
-		int mid = low + (high - low) / 2;
-		struct symbol *s = symbols->symbols[mid];
-		u64 start = symbol__start(s);
-		u64 end = symbol__end(s);
+	down_read(&symbols->lock);
+	while (!symbols->sorted) {
+		up_read(&symbols->lock);
+		down_write(&symbols->lock);
+		symbols__sort_locked(symbols);
+		up_write(&symbols->lock);
+		down_read(&symbols->lock);
+	}
 
-		if (ip < start)
-			high = mid - 1;
-		else if (ip > end || (ip == end && ip != start))
-			low = mid + 1;
-		else {
-			res = s;
-			break;
-		}
+	res = symbols__find_range(symbols->symbols, symbols->sorted_cnt, ip);
+	if (!res && symbols->sorted_cnt < symbols->cnt) {
+		res = symbols__find_range(&symbols->symbols[symbols->sorted_cnt],
+					  symbols->cnt - symbols->sorted_cnt, ip);
 	}
 	up_read(&symbols->lock);
 
@@ -707,7 +1070,13 @@ static struct symbol *symbols__find_by_name(struct symbol *symbols[],
 
 int dso__insert_symbol(struct dso *dso, struct symbol *sym)
 {
-	return symbols__insert(dso__symbols(dso), sym);
+	struct symbols *symbols = dso__symbols(dso);
+	int ret;
+
+	down_write(&symbols->lock);
+	ret = __symbols__insert_sorted(symbols, sym);
+	up_write(&symbols->lock);
+	return ret;
 }
 
 void dso__delete_symbol(struct dso *dso, struct symbol *sym)
@@ -715,11 +1084,15 @@ void dso__delete_symbol(struct dso *dso, struct symbol *sym)
 	struct symbols *symbols = dso__symbols(dso);
 
 	down_write(&symbols->lock);
-	for (unsigned int i = 0; i < symbols->cnt; i++) {
+	for (unsigned int i = symbols->cnt; i-- > 0; ) {
 		if (symbols->symbols[i] == sym) {
 			memmove(&symbols->symbols[i], &symbols->symbols[i + 1],
 				(symbols->cnt - i - 1) * sizeof(struct symbol *));
 			symbols->cnt--;
+			if (i < symbols->sorted_cnt)
+				symbols->sorted_cnt--;
+			if (symbols->sorted_cnt == symbols->cnt)
+				symbols->sorted = true;
 			zfree(&symbols->symbols_by_name);
 			symbols->num_by_name = 0;
 			symbols->sorted_by_name = false;
@@ -1066,6 +1439,7 @@ static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso)
 	old_cnt = root->cnt;
 	root->symbols = NULL;
 	root->cnt = 0;
+	root->sorted_cnt = 0;
 	root->allocated = 0;
 	root->sorted = true;
 	zfree(&root->symbols_by_name);
@@ -1143,6 +1517,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
 	old_allocated = root->allocated;
 	root->symbols = NULL;
 	root->cnt = 0;
+	root->sorted_cnt = 0;
 	root->allocated = 0;
 	root->sorted = true;
 	zfree(&root->symbols_by_name);
@@ -1289,6 +1664,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
 	if (root->cnt == 0) {
 		root->symbols = old_symbols;
 		root->cnt = write_idx;
+		root->sorted_cnt = write_idx;
 		root->allocated = old_allocated;
 		root->sorted = true;
 		old_symbols = NULL;
@@ -1318,12 +1694,14 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
 	if (root->cnt == 0) {
 		root->symbols = old_symbols;
 		root->cnt = write_idx;
+		root->sorted_cnt = (delta == 0) ? write_idx : 0;
 		root->allocated = old_allocated;
 		root->sorted = (delta == 0);
 		old_symbols = NULL;
 	} else {
 		for (unsigned int j = 0; j < write_idx; j++)
 			__symbols__insert(root, old_symbols[j]);
+		root->sorted_cnt = 0;
 		root->sorted = false;
 	}
 	zfree(&root->symbols_by_name);
tools/perf/util/symbol.h +10 -1
index 51cb5634341e..97cdc2e962c6 100644
@@ -114,11 +114,20 @@ struct symbols {
 	struct symbol **symbols_by_name;
 	/** @cnt: Number of valid entries in @symbols. */
 	unsigned int cnt;
+	/**
+	 * @sorted_cnt: Number of leading entries in @symbols that form the
+	 * primary sorted prefix. When @sorted is true and @sorted_cnt < @cnt,
+	 * the remaining tail @symbols[@sorted_cnt .. @cnt - 1] is also sorted.
+	 */
+	unsigned int sorted_cnt;
 	/** @allocated: Capacity (in entries) of the @symbols allocation. */
 	unsigned int allocated;
 	/** @num_by_name: Number of valid entries in @symbols_by_name. */
 	unsigned int num_by_name;
-	/** @sorted: True when @symbols is sorted by address range. */
+	/**
+	 * @sorted: True when @symbols[0 .. @sorted_cnt - 1] and any tail
+	 * @symbols[@sorted_cnt .. @cnt - 1] are sorted by start address.
+	 */
 	bool sorted;
 	/**
 	 * @sorted_by_name: True when @symbols_by_name is populated and sorted
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#84 lore raw

Re: [PATCH v3 16/20] perf symbol: Optimize symbol sorting with natural-run merge sort and binary insertion

Sashiko has reviewed this patch and found no issues.
Quoted 29 lines

ELF .symtab and .dynsym sections are typically already mostly ordered by address in ascending runs, and dynamic symbol resolution (dso__insert_symbol()) frequently inserts one or a few symbols into an already-sorted DSO symbol table. Invoking full-array qsort() on every sort after incremental insertions or across pre-ordered ELF runs spends avoidable CPU time in symbol comparisons.

Speed up symbol array sorting and out-of-order symbol insertions:

- Track the sorted prefix length (@sorted_cnt) in struct symbols and replace full-array qsort() with a natural-run merge sort (symbols__sort_range_with_tmp()) and galloping binary-search merge (symbols__merge_runs()), plus an O(N) in-place binary insertion fast path when <= 8 symbols are appended.
- Insert directly into sorted position in dso__insert_symbol() when symbols->sorted is true so dynamic single-symbol insertions maintain the sorted invariant without triggering full re-sorts.

Using 'perf bench internals symbols':

Before:

    Average DSO load + sort: 6.242 msec (+- 0.057 msec)
    Average bulk insert + lookup: 1404.150 usec (+- 18.752 usec)
    Average append + lookup: 40429.750 usec (+- 507.578 usec)
  After (-7.0% load, -13.9% bulk, -99.6% append):
    Average DSO load + sort: 5.808 msec (+- 0.047 msec)
    Average bulk insert + lookup: 1209.250 usec (+- 18.446 usec)
    Average append + lookup: 152.900 usec (+- 0.736 usec)
Quoted 1 line

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
author reviewer
#85 lore raw

PATCH v3 17/20 perf symbol: Add memory shrinking for symbols, DSOs, and machines

In reply to Ian Rogers

When profiling large workloads or systems with thousands of processes and shared libraries, perf can accumulate significant memory in loaded DSO symbol tables, secondary name-sorted symbol indices, cached DSO file data, and DWARF/addr2line state.

Because the lifetime of struct symbol is controlled by its owning struct dso, symbols in a referenced DSO (refcnt > 1) must remain valid for the lifetime of the DSO reference. However, auxiliary per-DSO allocations and unreferenced DSOs can still be reclaimed under memory pressure:

1. symbols__shrink(): Reclaims the secondary name-sorted index (symbols_by_name, which is rebuilt on demand by dso__sort_by_name()) and trims unused over-allocated tail capacity in the symbols array, using down_write_trylock() so shrinking never deadlocks if invoked while a symbol table lock is already held on the current stack.

2. dso__shrink(): Releases cached DSO resources (open file descriptor, cached 4KB data pages, addr2line handles, and libdw DWARF contexts). When a DSO in a dsos collection has no external references (refcnt == 1), dso__shrink() also evicts its unannotated symbol table via symbols__try_delete_unannotated() and marks the DSO unloaded so it can be reloaded on demand if mapped again; otherwise it calls symbols__shrink() to reclaim auxiliary symbol arrays.

3. dsos__shrink(), machine__shrink(), machines__shrink(), and perf_session__shrink(): Walk the session's host and guest machines to shrink DSOs and evict unreferenced user DSOs (refcnt == 1, not kernel, not vDSO, not hit, and not carrying header build-ids), followed by malloc_trim(0) on glibc to return freed heap pages to the OS.

---
 tools/perf/util/dso.c     | 198 ++++++++++++++++++++++++++++++++++----
 tools/perf/util/dso.h     |  13 ++-
 tools/perf/util/dsos.c    |  69 +++++++++++++
 tools/perf/util/dsos.h    |   2 +
 tools/perf/util/machine.c |  32 ++++++
 tools/perf/util/machine.h |   3 +
 tools/perf/util/session.c |   8 ++
 tools/perf/util/session.h |   2 +
 tools/perf/util/symbol.c  |  73 +++++++++++++-
 tools/perf/util/symbol.h  |   3 +
 10 files changed, 381 insertions(+), 22 deletions(-)
tools/perf/util/dso.c +179 -19
index 35a5c65030af..184d49935bfd 100644
@@ -2,6 +2,7 @@
 #include "dso.h"
 
 #include <errno.h>
+#include <stdatomic.h>
 #include <stdlib.h>
 
 #include <asm/bug.h>
@@ -1075,12 +1076,12 @@ static int bpf_size(struct dso *dso)
 #endif // HAVE_LIBBPF_SUPPORT
 
 static void
-dso_cache__free(struct dso *dso)
+__dso_cache__free(struct dso *dso)
+	EXCLUSIVE_LOCKS_REQUIRED(dso__lock(dso))
 {
 	struct rb_root *root = &dso__data(dso)->cache;
 	struct rb_node *next = rb_first(root);
 
-	mutex_lock(dso__lock(dso));
 	while (next) {
 		struct dso_cache *cache;
 
@@ -1089,6 +1090,13 @@ dso_cache__free(struct dso *dso)
 		rb_erase(&cache->rb_node, root);
 		free(cache);
 	}
+}
+
+static void
+dso_cache__free(struct dso *dso)
+{
+	mutex_lock(dso__lock(dso));
+	__dso_cache__free(dso);
 	mutex_unlock(dso__lock(dso));
 }
 
@@ -1243,27 +1251,30 @@ static struct dso_cache *dso_cache__populate(struct dso *dso,
 	return cache;
 }
 
-static struct dso_cache *dso_cache__find(struct dso *dso,
-					 struct machine *machine,
-					 u64 offset,
-					 ssize_t *ret)
-{
-	struct dso_cache *cache = __dso_cache__find(dso, offset);
-
-	return cache ? cache : dso_cache__populate(dso, machine, offset, ret);
-}
-
 static ssize_t dso_cache_io(struct dso *dso, struct machine *machine,
 			    u64 offset, u8 *data, ssize_t size, bool out)
 {
 	struct dso_cache *cache;
 	ssize_t ret = 0;
 
-	cache = dso_cache__find(dso, machine, offset, &ret);
-	if (!cache)
-		return ret;
+	mutex_lock(dso__lock(dso));
+	cache = __dso_cache__find(dso, offset);
+	while (!cache) {
+		mutex_unlock(dso__lock(dso));
+		cache = dso_cache__populate(dso, machine, offset, &ret);
+		if (!cache)
+			return ret;
+		mutex_lock(dso__lock(dso));
+		/*
+		 * Re-lookup under dso__lock(dso) in case dso__shrink() freed
+		 * the cache while the lock was dropped.
+		 */
+		cache = __dso_cache__find(dso, offset);
+	}
 
-	return dso_cache__memcpy(cache, offset, data, size, out);
+	ret = dso_cache__memcpy(cache, offset, data, size, out);
+	mutex_unlock(dso__lock(dso));
+	return ret;
 }
 
 /*
@@ -1803,7 +1814,7 @@ int dso__name_len(const struct dso *dso)
 
 bool dso__loaded(const struct dso *dso)
 {
-	return RC_CHK_ACCESS(dso)->loaded;
+	return atomic_load_explicit(&RC_CHK_ACCESS(dso)->loaded, memory_order_acquire);
 }
 
 bool dso__sorted_by_name(const struct dso *dso)
@@ -1835,7 +1846,7 @@ struct dso *dso__new_id(const char *name, const struct dso_id *id)
 		dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
 		dso->dbginfo_type = DSO_BINARY_TYPE__NOT_FOUND;
 		dso->is_64_bit = (sizeof(void *) == 8);
-		dso->loaded = 0;
+		atomic_init(&dso->loaded, false);
 		dso->rel = 0;
 		dso->has_srcline = 1;
 		dso->a2l_fails = 1;
@@ -1896,6 +1907,154 @@ void dso__delete(struct dso *dso)
 	RC_CHK_FREE(dso);
 }
 
+bool dso__can_shrink_symbols(const struct dso *dso)
+{
+	const char *name;
+
+	if (!dso || !dso__loaded(dso))
+		return false;
+
+	if (dso__kernel(dso) || dso__is_kmod(dso))
+		return false;
+
+	if (dso__is_vdso(dso))
+		return false;
+
+	switch (dso__binary_type(dso)) {
+	case DSO_BINARY_TYPE__KALLSYMS:
+	case DSO_BINARY_TYPE__GUEST_KALLSYMS:
+	case DSO_BINARY_TYPE__VMLINUX:
+	case DSO_BINARY_TYPE__GUEST_VMLINUX:
+	case DSO_BINARY_TYPE__KCORE:
+	case DSO_BINARY_TYPE__GUEST_KCORE:
+	case DSO_BINARY_TYPE__BPF_PROG_INFO:
+	case DSO_BINARY_TYPE__BPF_IMAGE:
+	case DSO_BINARY_TYPE__OOL:
+	case DSO_BINARY_TYPE__JAVA_JIT:
+	case DSO_BINARY_TYPE__GUEST_KMODULE:
+	case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
+	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
+	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
+		return false;
+	case DSO_BINARY_TYPE__DEBUGLINK:
+	case DSO_BINARY_TYPE__BUILD_ID_CACHE:
+	case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
+	case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
+	case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
+	case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
+	case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
+	case DSO_BINARY_TYPE__GNU_DEBUGDATA:
+	case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
+	case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
+	case DSO_BINARY_TYPE__NOT_FOUND:
+	default:
+		break;
+	}
+
+	switch (dso__symtab_type(dso)) {
+	case DSO_BINARY_TYPE__KALLSYMS:
+	case DSO_BINARY_TYPE__GUEST_KALLSYMS:
+	case DSO_BINARY_TYPE__VMLINUX:
+	case DSO_BINARY_TYPE__GUEST_VMLINUX:
+	case DSO_BINARY_TYPE__KCORE:
+	case DSO_BINARY_TYPE__GUEST_KCORE:
+	case DSO_BINARY_TYPE__BPF_PROG_INFO:
+	case DSO_BINARY_TYPE__BPF_IMAGE:
+	case DSO_BINARY_TYPE__OOL:
+	case DSO_BINARY_TYPE__JAVA_JIT:
+	case DSO_BINARY_TYPE__GUEST_KMODULE:
+	case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
+	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
+	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
+	case DSO_BINARY_TYPE__NOT_FOUND:
+		return false;
+	case DSO_BINARY_TYPE__DEBUGLINK:
+	case DSO_BINARY_TYPE__BUILD_ID_CACHE:
+	case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
+	case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
+	case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
+	case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
+	case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
+	case DSO_BINARY_TYPE__GNU_DEBUGDATA:
+	case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
+	case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
+	default:
+		break;
+	}
+
+	name = dso__long_name(dso);
+	if (!name || name[0] == '[' || is_perf_pid_map_name(name) ||
+	    strstr(name, " (deleted)") != NULL)
+		return false;
+
+	return true;
+}
+
+static void dso__delete_inlines_and_srclines(void *ctx)
+{
+	struct dso *dso = ctx;
+
+	/* Free inlines before symbols, as inline frames reference symbols. */
+	inlines__tree_delete(dso__inlined_nodes(dso));
+	srcline__tree_delete(dso__srclines(dso));
+}
+
+size_t dso__shrink(struct dso *dso)
+	NO_THREAD_SAFETY_ANALYSIS
+{
+	size_t freed = 0;
+
+	if (!dso)
+		return 0;
+
+	if (mutex_trylock(dso__data_open_lock())) {
+		if (dso__data(dso)->fd >= 0) {
+			close_dso(dso);
+			freed++;
+		}
+		mutex_unlock(dso__data_open_lock());
+	}
+
+	if (!mutex_trylock(dso__lock(dso)))
+		return freed;
+
+	if (!dso__kernel(dso) && dso__binary_type(dso) != DSO_BINARY_TYPE__OOL &&
+	    !RB_EMPTY_ROOT(&dso__data(dso)->cache)) {
+		__dso_cache__free(dso);
+		freed++;
+	}
+	if (dso__a2l(dso)) {
+		dso__free_a2l(dso);
+		freed++;
+	}
+#ifdef HAVE_LIBBFD_SUPPORT
+	if (dso__a2l_libbfd(dso)) {
+		dso__free_a2l_libbfd(dso);
+		freed++;
+	}
+#endif
+#ifdef HAVE_LIBDW_SUPPORT
+	if (dso__libdw(dso)) {
+		dso__free_libdw(dso);
+		freed++;
+	}
+#endif
+
+	if (dso__dsos(dso) && refcount_read(dso__refcnt(dso)) == 1 &&
+	    dso__can_shrink_symbols(dso) && dso__has_symbols(dso) &&
+	    symbols__try_delete_unannotated(dso__symbols(dso),
+					    dso__delete_inlines_and_srclines, dso)) {
+		atomic_store_explicit(&RC_CHK_ACCESS(dso)->loaded, false,
+				      memory_order_release);
+		freed++;
+	} else {
+		freed += symbols__shrink(dso__symbols(dso));
+	}
+
+	mutex_unlock(dso__lock(dso));
+	return freed;
+}
+
 struct dso *dso__get(struct dso *dso)
 {
 	struct dso *result;
@@ -1909,7 +2068,8 @@ struct dso *dso__get(struct dso *dso)
 void dso__put(struct dso *dso)
 {
 #ifdef REFCNT_CHECKING
-	if (dso && dso__data(dso) && refcount_read(&RC_CHK_ACCESS(dso)->refcnt) == 2)
+	if (dso && dso__data(dso) && dso__data(dso)->fd >= 0 &&
+	    refcount_read(&RC_CHK_ACCESS(dso)->refcnt) == 2)
 		dso__data_close(dso);
 #endif
 	if (dso && refcount_dec_and_test(&RC_CHK_ACCESS(dso)->refcnt))
tools/perf/util/dso.h +11 -2
index f23cf9957aec..7f217815ec07 100644
@@ -6,6 +6,7 @@
 #include <linux/types.h>
 #include <linux/rbtree.h>
 #include <sys/types.h>
+#include <stdatomic.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <linux/bitops.h>
@@ -346,7 +347,7 @@ DECLARE_RC_STRUCT(dso) {
 	u8		 is_64_bit:1;
 	u8		 bintype_searched:1;
 	u8		 debuginfo_searched:1;
-	bool		 loaded;
+	_Atomic bool	 loaded;
 	u8		 rel;
 	char		 name[];
 };
@@ -602,7 +603,7 @@ static inline enum dso_load_errno *dso__load_errno(struct dso *dso)
 
 static inline void dso__set_loaded(struct dso *dso)
 {
-	RC_CHK_ACCESS(dso)->loaded = true;
+	atomic_store_explicit(&RC_CHK_ACCESS(dso)->loaded, true, memory_order_release);
 }
 
 static inline struct mutex *dso__lock(struct dso *dso)
@@ -783,6 +784,14 @@ static inline bool dso__has_symbols(const struct dso *dso)
 	return RC_CHK_ACCESS(dso)->symbols.cnt > 0;
 }
 
+static inline refcount_t *dso__refcnt(struct dso *dso)
+{
+	return &RC_CHK_ACCESS(dso)->refcnt;
+}
+
+bool dso__can_shrink_symbols(const struct dso *dso);
+size_t dso__shrink(struct dso *dso);
+
 char *dso__filename_with_chroot(const struct dso *dso, const char *filename);
 
 bool dso__sorted_by_name(const struct dso *dso);
tools/perf/util/dsos.c +69 -0

78 lines not on this page · raw message

tools/perf/util/dsos.h +2 -0
index a26774950866..151efdba2aa4 100644
@@ -49,4 +49,6 @@ struct dso *dsos__find_kernel_dso(struct dsos *dsos);
 
 int dsos__for_each_dso(struct dsos *dsos, int (*cb)(struct dso *dso, void *data), void *data);
 
+size_t dsos__shrink(struct dsos *dsos);
+
 #endif /* __PERF_DSOS */
tools/perf/util/machine.c +32 -0
index 7c83321eea76..dad06eb865e6 100644
@@ -3440,3 +3440,35 @@ int machine__hit_all_dsos(struct machine *machine)
 {
 	return dsos__hit_all(&machine->dsos);
 }
+
+size_t machine__shrink(struct machine *machine)
+{
+	if (!machine)
+		return 0;
+
+	return dsos__shrink(&machine->dsos);
+}
+
+size_t machines__shrink(struct machines *machines)
+	NO_THREAD_SAFETY_ANALYSIS
+{
+	struct rb_node *nd;
+	size_t freed;
+
+	if (!machines)
+		return 0;
+
+	freed = machine__shrink(&machines->host);
+
+	if (!down_read_trylock(&machines->lock))
+		return freed;
+
+	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
+		struct machine *pos = rb_entry(nd, struct machine, rb_node);
+
+		freed += machine__shrink(pos);
+	}
+	up_read(&machines->lock);
+
+	return freed;
+}
tools/perf/util/machine.h +3 -0
index 80191545ee8a..0f4c0ae5b56d 100644
@@ -338,4 +338,7 @@ int machine__resolve(struct machine *machine, struct addr_location *al,
 
 int machine__hit_all_dsos(struct machine *machine);
 
+size_t machine__shrink(struct machine *machine);
+size_t machines__shrink(struct machines *machines);
+
 #endif /* __PERF_MACHINE_H */
tools/perf/util/session.c +8 -0
index 56a5e57ec512..3f35c7affc29 100644
@@ -4266,3 +4266,11 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flags
 	 */
 	return perf_env__e_machine_nocache(env, e_flags);
 }
+
+size_t perf_session__shrink(struct perf_session *session)
+{
+	if (!session)
+		return 0;
+
+	return machines__shrink(&session->machines);
+}
tools/perf/util/session.h +2 -0
index 05ea2c1ee8c9..cbea6792a008 100644
@@ -211,4 +211,6 @@ int perf_event__process_finished_round(const struct perf_tool *tool,
 struct perf_env *perf_session__env(struct perf_session *session);
 uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flags);
 
+size_t perf_session__shrink(struct perf_session *session);
+
 #endif /* __PERF_SESSION_H */
tools/perf/util/symbol.c +72 -1

98 lines not on this page · raw message

tools/perf/util/symbol.h +3 -0
index 97cdc2e962c6..e68248be38c5 100644
@@ -140,6 +140,9 @@ void symbol__delete(struct symbol *sym);
 void symbols__init(struct symbols *symbols);
 void symbols__exit(struct symbols *symbols);
 void symbols__delete(struct symbols *symbols);
+bool symbols__try_delete_unannotated(struct symbols *symbols,
+				     void (*pre_delete)(void *ctx), void *ctx);
+size_t symbols__shrink(struct symbols *symbols);
 void symbols__sort_read_lock(struct symbols *symbols) SHARED_LOCK_FUNCTION(symbols->lock);
 
 static inline __attribute__((nonnull)) u64 symbol__start(const struct symbol *sym)
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#86 lore raw

Re: [PATCH v3 17/20] perf symbol: Add memory shrinking for symbols, DSOs, and machines

Sashiko has reviewed this patch and found no issues.
Quoted 20 lines

When profiling large workloads or systems with thousands of processes and shared libraries, perf can accumulate significant memory in loaded DSO symbol tables, secondary name-sorted symbol indices, cached DSO file data, and DWARF/addr2line state.

Because the lifetime of struct symbol is controlled by its owning struct dso, symbols in a referenced DSO (refcnt > 1) must remain valid for the lifetime of the DSO reference. However, auxiliary per-DSO allocations and unreferenced DSOs can still be reclaimed under memory pressure:

1. symbols__shrink(): Reclaims the secondary name-sorted index (symbols_by_name, which is rebuilt on demand by dso__sort_by_name()) and trims unused over-allocated tail capacity in the symbols array, using down_write_trylock() so shrinking never deadlocks if invoked while a symbol table lock is already held on the current stack.
[ ... ]
shrink DSOs and evict unreferenced user DSOs (refcnt == 1, not kernel, not vDSO, not hit, and not carrying header build-ids), followed by malloc_trim(0) on glibc to return freed heap pages to the OS.

Quoted 1 line

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
author reviewer
#87 lore raw

PATCH v3 18/20 perf session: Add dso__session() and session-scoped allocation wrappers

In reply to Ian Rogers

Add explicit session-scoped memory allocation wrappers:
- perf_session__malloc(session, size)
- perf_session__zalloc(session, size)
- perf_session__calloc(session, nmemb, size)
- perf_session__realloc(session, ptr, size)
- perf_session__strdup(session, s)

When an allocation fails and returns NULL, if a non-NULL struct perf_session is provided, the wrapper invokes perf_session__shrink() to evict unreferenced symbols, DSOs, and cached debuginfo across the session's machines and then retries the allocation once. This avoids the thrashing overhead of periodic heuristic shrinking while allowing perf to recover from memory exhaustion.

Also plumb a back-pointer from struct dsos to its owning struct machine so machine__session(machine) and dso__session(dso) can resolve the active perf_session.

---
 tools/perf/util/dso.c     |  7 ++++
 tools/perf/util/dso.h     |  2 ++
 tools/perf/util/dsos.c    |  1 +
 tools/perf/util/dsos.h    |  1 +
 tools/perf/util/machine.c |  1 +
 tools/perf/util/machine.h |  5 +++
 tools/perf/util/session.c | 70 +++++++++++++++++++++++++++++++++++++++
 tools/perf/util/session.h |  6 ++++
 8 files changed, 93 insertions(+)
tools/perf/util/dso.c +7 -0
index 184d49935bfd..7b71fd5e48a9 100644
@@ -1907,6 +1907,13 @@ void dso__delete(struct dso *dso)
 	RC_CHK_FREE(dso);
 }
 
+struct perf_session *dso__session(const struct dso *dso)
+{
+	struct dsos *dsos = dso ? RC_CHK_ACCESS(dso)->dsos : NULL;
+
+	return dsos ? machine__session(dsos->machine) : NULL;
+}
+
 bool dso__can_shrink_symbols(const struct dso *dso)
 {
 	const char *name;
tools/perf/util/dso.h +2 -0
index 7f217815ec07..9b01a86526c1 100644
@@ -20,6 +20,7 @@
 struct machine;
 struct map;
 struct perf_env;
+struct perf_session;
 
 #define DSO__NAME_KALLSYMS	"[kernel.kallsyms]"
 #define DSO__NAME_KCORE		"[kernel.kcore]"
@@ -791,6 +792,7 @@ static inline refcount_t *dso__refcnt(struct dso *dso)
 
 bool dso__can_shrink_symbols(const struct dso *dso);
 size_t dso__shrink(struct dso *dso);
+struct perf_session *dso__session(const struct dso *dso);
 
 char *dso__filename_with_chroot(const struct dso *dso, const char *filename);
 
tools/perf/util/dsos.c +1 -0
index f6868ffcc3e1..d53a8ab217e6 100644
@@ -18,6 +18,7 @@ void dsos__init(struct dsos *dsos)
 {
 	init_rwsem(&dsos->lock);
 
+	dsos->machine = NULL;
 	dsos->cnt = 0;
 	dsos->allocated = 0;
 	dsos->dsos = NULL;
tools/perf/util/dsos.h +1 -0
index 151efdba2aa4..d636e4c5cbaf 100644
@@ -19,6 +19,7 @@ struct machine;
  */
 struct dsos {
 	struct rw_semaphore lock;
+	struct machine *machine;
 	struct dso **dsos;
 	unsigned int cnt;
 	unsigned int allocated;
tools/perf/util/machine.c +1 -0
index dad06eb865e6..c89f47ba16f2 100644
@@ -81,6 +81,7 @@ int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
 	memset(machine, 0, sizeof(*machine));
 	RB_CLEAR_NODE(&machine->rb_node);
 	dsos__init(&machine->dsos);
+	machine->dsos.machine = machine;
 	threads__init(&machine->threads);
 
 	machine->kmaps = maps__new(machine);
tools/perf/util/machine.h +5 -0
index 0f4c0ae5b56d..6192bd3e33bb 100644
@@ -164,6 +164,11 @@ static inline struct perf_session *machines__session(struct machines *machines)
 	return machines->session;
 }
 
+static inline struct perf_session *machine__session(const struct machine *machine)
+{
+	return machine && machine->machines ? machine->machines->session : NULL;
+}
+
 void machines__process_guests(struct machines *machines,
 			      machine__process_t process, void *data);
 
tools/perf/util/session.c +70 -0

73 lines not on this page · raw message

tools/perf/util/session.h +6 -0
index cbea6792a008..c6157eddcc43 100644
@@ -213,4 +213,10 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flags
 
 size_t perf_session__shrink(struct perf_session *session);
 
+void *perf_session__malloc(struct perf_session *session, size_t size);
+void *perf_session__zalloc(struct perf_session *session, size_t size);
+void *perf_session__calloc(struct perf_session *session, size_t nmemb, size_t size);
+void *perf_session__realloc(struct perf_session *session, void *ptr, size_t size);
+char *perf_session__strdup(struct perf_session *session, const char *s);
+
 #endif /* __PERF_SESSION_H */
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#88 lore raw

Re: [PATCH v3 18/20] perf session: Add dso__session() and session-scoped allocation wrappers

Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

Add explicit session-scoped memory allocation wrappers:
- perf_session__malloc(session, size)
- perf_session__zalloc(session, size)
- perf_session__calloc(session, nmemb, size)
- perf_session__realloc(session, ptr, size)
- perf_session__strdup(session, s)

When an allocation fails and returns NULL, if a non-NULL struct perf_session is provided, the wrapper invokes perf_session__shrink() to evict unreferenced symbols, DSOs, and cached debuginfo across the session's machines and then retries the allocation once. This avoids the thrashing overhead of periodic heuristic shrinking while allowing perf to recover from memory exhaustion.

Quoted 5 lines

Also plumb a back-pointer from struct dsos to its owning struct machine so machine__session(machine) and dso__session(dso) can resolve the active perf_session.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
author reviewer
#89 lore raw

PATCH v3 19/20 perf symbol: Use session allocation wrappers in symbol, DSO, thread, and hist paths

In reply to Ian Rogers

Connect the session-scoped allocation wrappers (perf_session__malloc, perf_session__zalloc, perf_session__calloc, perf_session__realloc, perf_session__strdup) to the primary heap allocation paths during sample and symbol processing: symbol__new(), symbols->symbols array growth, dso__new_id(), thread__new(), and hist_entry allocation.

In normal operation when memory is available, behavior is unchanged. When an allocation fails with NULL, these paths now invoke perf_session__shrink() on the owning perf_session to reclaim unreferenced DSOs and auxiliary symbol/DSO buffers and retry the allocation once before failing.

---
 tools/perf/util/dso.c        |  4 ++-
 tools/perf/util/dsos.c       |  7 +++++
 tools/perf/util/hist.c       | 41 ++++++++++++++++++++++++----
 tools/perf/util/machine.c    |  8 +++++-
 tools/perf/util/symbol-elf.c | 14 +++++++---
 tools/perf/util/symbol.c     | 52 +++++++++++++++++++++++-------------
 tools/perf/util/symbol.h     |  3 +++
 7 files changed, 99 insertions(+), 30 deletions(-)
tools/perf/util/dso.c +3 -1
index 7b71fd5e48a9..cfeed7cc3e9f 100644
@@ -28,6 +28,7 @@
 #include "map.h"
 #include "namespaces.h"
 #include "path.h"
+#include "session.h"
 #include "srcline.h"
 #include "string2.h"
 #include "symbol.h"
@@ -1215,10 +1216,11 @@ static struct dso_cache *dso_cache__populate(struct dso *dso,
 					     u64 offset, ssize_t *ret)
 {
 	u64 cache_offset = offset & DSO__DATA_CACHE_MASK;
+	struct perf_session *session = machine ? machine__session(machine) : dso__session(dso);
 	struct dso_cache *cache;
 	struct dso_cache *old;
 
-	cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
+	cache = perf_session__zalloc(session, sizeof(*cache) + DSO__DATA_CACHE_SIZE);
 	if (!cache) {
 		*ret = -ENOMEM;
 		return NULL;
tools/perf/util/dsos.c +7 -0
index d53a8ab217e6..94f93fbf02e4 100644
@@ -2,6 +2,8 @@
 #include "debug.h"
 #include "dsos.h"
 #include "dso.h"
+#include "machine.h"
+#include "session.h"
 #include "util.h"
 #include "vdso.h"
 #include "namespaces.h"
@@ -350,6 +352,11 @@ struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, const struct d
 	down_write(&dsos->lock);
 	dso = __dsos__findnew_id(dsos, name, id);
 	up_write(&dsos->lock);
+	if (!dso && dsos->machine && perf_session__shrink(machine__session(dsos->machine)) > 0) {
+		down_write(&dsos->lock);
+		dso = __dsos__findnew_id(dsos, name, id);
+		up_write(&dsos->lock);
+	}
 	return dso;
 }
 
tools/perf/util/hist.c +36 -5

79 lines not on this page · raw message

tools/perf/util/machine.c +7 -1
index c89f47ba16f2..489395b53c2e 100644
@@ -20,6 +20,7 @@
 #include "mem-events.h"
 #include "mem-info.h"
 #include "path.h"
+#include "session.h"
 #include "srcline.h"
 #include "symbol.h"
 #include "synthetic-events.h"
@@ -598,7 +599,12 @@ static struct thread *__machine__findnew_thread(struct machine *machine,
 
 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
 {
-	return __machine__findnew_thread(machine, pid, tid, /*create=*/true);
+	struct thread *th = __machine__findnew_thread(machine, pid, tid, /*create=*/true);
+
+	if (!th && perf_session__shrink(machine__session(machine)) > 0)
+		th = __machine__findnew_thread(machine, pid, tid, /*create=*/true);
+
+	return th;
 }
 
 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
tools/perf/util/symbol-elf.c +10 -4
index 2c8088184568..f2a071508d2c 100644
@@ -15,6 +15,7 @@
 #include "symbol.h"
 #include "symsrc.h"
 #include "machine.h"
+#include "session.h"
 #include "vdso.h"
 #include "debug.h"
 #include "util/copyfile.h"
@@ -700,7 +701,7 @@ static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf,
 		sym = symbol__new(shdr.sh_offset + i, shdr.sh_entsize, STB_GLOBAL, STT_FUNC, buf);
 		if (!sym)
 			goto out;
-		if (symbols__insert(dso__symbols(dso), sym))
+		if (symbols__insert_session(dso__symbols(dso), sym, dso__session(dso)))
 			goto out;
 	}
 	err = 0;
@@ -755,7 +756,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
 	plt_sym = symbol__new(shdr_plt.sh_offset, plt_header_size, STB_GLOBAL, STT_FUNC, ".plt");
 	if (!plt_sym)
 		goto out_elf_end;
-	if (symbols__insert(dso__symbols(dso), plt_sym))
+	if (symbols__insert_session(dso__symbols(dso), plt_sym, dso__session(dso)))
 		goto out_elf_end;
 
 	/* Only x86 has .plt.got */
@@ -878,7 +879,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
 			goto out_elf_end;
 
 		plt_offset += plt_entry_size;
-		if (symbols__insert(dso__symbols(dso), f))
+		if (symbols__insert_session(dso__symbols(dso), f, dso__session(dso)))
 			goto out_elf_end;
 		++nr;
 	}
@@ -1937,13 +1938,18 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		f = symbol__new(sym.st_value, sym.st_size,
 				GELF_ST_BIND(sym.st_info),
 				GELF_ST_TYPE(sym.st_info), elf_name);
+		if (!f && perf_session__shrink(dso__session(curr_dso)) > 0) {
+			f = symbol__new(sym.st_value, sym.st_size,
+					GELF_ST_BIND(sym.st_info),
+					GELF_ST_TYPE(sym.st_info), elf_name);
+		}
 		free(demangled);
 		if (!f)
 			goto out_elf_end;
 
 		arch__sym_update(f, &sym);
 
-		if (symbols__insert(dso__symbols(curr_dso), f))
+		if (symbols__insert_session(dso__symbols(curr_dso), f, dso__session(curr_dso)))
 			goto out_elf_end;
 		nr++;
 	}
tools/perf/util/symbol.c +33 -19

148 lines not on this page · raw message

tools/perf/util/symbol.h +3 -0
index e68248be38c5..003b9cc57e5e 100644
@@ -32,6 +32,7 @@ struct maps;
 struct option;
 struct build_id;
 struct perf_env;
+struct perf_session;
 
 /*
  * Ignore local labels and mapping symbols, matching kernel is_ignored_kernel_symbol() logic.
@@ -347,6 +348,8 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);
 char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name);
 
 int symbols__insert(struct symbols *symbols, struct symbol *sym);
+int symbols__insert_session(struct symbols *symbols, struct symbol *sym,
+			    struct perf_session *session);
 void symbols__fixup_duplicate(struct symbols *symbols);
 void symbols__fixup_end(struct symbols *symbols, bool is_kallsyms);
 
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#90 lore raw

Re: [PATCH v3 19/20] perf symbol: Use session allocation wrappers in symbol, DSO, thread, and hist paths

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] perf machine: NULL deref in __machine__findnew_thread() on thread OOM - [Low] perf symbol: NULL…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] perf machine: NULL deref in __machine__findnew_thread() on thread OOM
- [Low] perf symbol: NULL session for split kernel DSOs in maps__split_kallsyms()
--

commit ae77bbd4b11902101ad3db4f72bc23f1b4972f1f
Author: Ian Rogers <[email protected]>

perf symbol: Use session allocation wrappers in symbol, DSO, thread, and hist paths

This commit wires session-scoped allocation wrappers and shrink-and-retry handling into symbol, DSO, thread, and hist_entry allocation paths.

Quoted 4 lines
tools/perf/util/machine.c +0 -0
index c89f47ba16f2c..489395b53c2e3 100644

[ ... ]

tools/perf/util/machine.c static struct thread *__machine__findnew_thread(struct machine *machine, · line 608

Quoted 12 lines
tools/perf/util/machine.c +6 -1
@@ -598,7 +599,12 @@ static struct thread *__machine__findnew_thread(struct machine *machine,
 
 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
 {
-	return __machine__findnew_thread(machine, pid, tid, /*create=*/true);
+	struct thread *th = __machine__findnew_thread(machine, pid, tid, /*create=*/true);
+
+	if (!th && perf_session__shrink(machine__session(machine)) > 0)
+		th = __machine__findnew_thread(machine, pid, tid, /*create=*/true);
+
+	return th;
 }

[Severity: Medium]
Can __machine__findnew_thread() dereference a NULL th before returning to this retry path when thread allocation fails?

When threads__findnew() fails to allocate a thread, it returns NULL with created == false:

tools/perf/util/machine.c:__machine__findnew_thread() {
    ...
	th = threads__findnew(&machine->threads, pid, tid, &created);
	if (created) {
		if (thread__init_maps(th, machine)) {
			pr_err("Thread init failed thread %d\n", pid);
			threads__remove(&machine->threads, th);
			thread__put(th);
			return NULL;
		}
	} else
		machine__update_thread_pid(machine, th, pid);
    ...
}

The else branch then calls machine__update_thread_pid(machine, NULL, pid), which immediately dereferences th in thread__pid(th) instead of returning NULL so machine__findnew_thread() can shrink the session and retry.

Quoted 4 lines
tools/perf/util/symbol.c +0 -0
index 9d7c87bcbbea8..21f012252dcd5 100644

[ ... ]

tools/perf/util/symbol.c static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, · line 1734

Quoted 10 lines
tools/perf/util/symbol.c +2 -1
@@ -1717,7 +1727,8 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
 		if (!RC_CHK_EQUAL(curr_map, initial_map)) {
 			struct dso *curr_map_dso = map__dso(curr_map);
 
-			if (symbols__insert(dso__symbols(curr_map_dso), pos) == 0)
+			if (symbols__insert_session(dso__symbols(curr_map_dso), pos,
+						    dso__session(curr_map_dso)) == 0)
 				++moved;
 		} else {
 			old_symbols[write_idx++] = pos;

[Severity: Low]
Does dso__session(curr_map_dso) return NULL for split kernel range DSOs created earlier in maps__split_kallsyms()?

When a non-module kernel symbol falls outside initial_map, ndso is created with dso__new(dso_name) and inserted into kmaps via map__new2(), but it is never added to machine->dsos. Because ndso->dsos remains NULL, dso__session(curr_map_dso) evaluates to NULL and symbol array growth for those maps bypasses perf_session__shrink(); passing dso__session(dso) would preserve the session.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
author reviewer
#91 lore raw

PATCH v3 20/20 perf test symbols: Add tests for symbol shrinking and session alloc wrappers

In reply to Ian Rogers

Ensure the new symbol sorting, DSO-lifetime symbol and DSO shrinking, dso_symbol reference pinning, on-demand DSO reload after unreferenced eviction, and session-scoped allocation wrappers are verified by 'perf test symbols' (including under REFCNT_CHECKING=1).

Add unit tests to 'perf test symbols' covering:
- test_symbols_sorted_insert(): Verify ascending symbols__insert(), out-of-order dso__insert_symbol() maintaining sorted order, and unsorted symbols__insert() followed by sort-on-lookup in dso__find_symbol().
- test_symbols_shrink(): Verify symbols__shrink() and symbols__try_delete_unannotated() respect held locks without deadlocking, reclaim the secondary symbols_by_name index, and trim unused capacity in symbols->symbols while keeping live symbols intact.
- test_dsos_shrink(): Verify dsos__shrink() evicts unreferenced user DSOs while retaining DSOs with active external references and vDSOs.
- test_session_alloc_shrink(): Verify machine__session(), dso__session(), perf_session__malloc/zalloc/calloc/realloc/strdup(), and perf_session__shrink().
- test_dso_shrink_and_reload(): Verify dso__shrink() on a referenced ELF DSO reclaims auxiliary buffers while preserving symbol pointers, dso_symbol__copy() pins DSO symbols across dsos__shrink() until dso_symbol__exit(), and dsos__shrink() on an unreferenced hit DSO evicts its symbol table and transparently reloads it on a subsequent map__find_symbol().

---
 tools/perf/tests/symbols.c | 541 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 530 insertions(+), 11 deletions(-)
tools/perf/tests/symbols.c +530 -11
index 1185ab0731e0..1fdc2428938f 100644
@@ -1,20 +1,31 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <linux/compiler.h>
+#include <linux/list.h>
 #include <linux/string.h>
+#include <linux/zalloc.h>
 #include <sys/mman.h>
 #include <elf.h>
+#include <errno.h>
 #include <inttypes.h>
 #include <limits.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include "debug.h"
 #include "dso.h"
 #include "env.h"
 #include "machine.h"
+#include "session.h"
+#include "srcline.h"
 #include "thread.h"
 #include "symbol.h"
 #include "map.h"
+#include "map_symbol.h"
 #include "util.h"
+#include "vdso.h"
 #include "tests.h"
 
 struct test_info {
@@ -182,6 +193,396 @@ static int process_subdivided_dso(struct machine *machine, struct dso *dso)
 	return ret < 0 ? TEST_FAIL : TEST_OK;
 }
 
+static int noop_visit_cb(struct symbol *sym __maybe_unused, void *data __maybe_unused)
+{
+	return 0;
+}
+
+static int test_symbols_shrink(void)
+{
+	struct symbols symbols;
+	struct symbol *sym1, *sym2, *sym3;
+	bool was_singlethreaded = perf_singlethreaded;
+	int ret = TEST_FAIL;
+
+	perf_set_multithreaded();
+	symbols__init(&symbols);
+
+	sym1 = symbol__new(0x100, 0x50, STB_GLOBAL, STT_FUNC, "sym1");
+	sym2 = symbol__new(0x200, 0x50, STB_GLOBAL, STT_FUNC, "sym2");
+	sym3 = symbol__new(0x300, 0x50, STB_GLOBAL, STT_FUNC, "sym3");
+	if (!sym1 || !sym2 || !sym3) {
+		symbol__delete(sym1);
+		symbol__delete(sym2);
+		symbol__delete(sym3);
+		goto out;
+	}
+
+	symbols__insert(&symbols, sym1);
+	symbols__insert(&symbols, sym2);
+	symbols__insert(&symbols, sym3);
+	symbols__visit_by_name(&symbols, noop_visit_cb, NULL);
+	if (!symbols.symbols_by_name || symbols.allocated <= symbols.cnt) {
+		pr_debug("Expected populated symbols_by_name and excess capacity!\n");
+		goto out;
+	}
+
+	/*
+	 * Verify symbols__shrink() (via down_write_trylock()) does not
+	 * re-enter when symbols->lock is already held on the current thread.
+	 */
+	down_write(&symbols.lock);
+	if (symbols__shrink(&symbols) != 0 || symbols.cnt != 3) {
+		up_write(&symbols.lock);
+		pr_debug("symbols__shrink re-entered held write lock!\n");
+		goto out;
+	}
+	up_write(&symbols.lock);
+
+	down_read(&symbols.lock);
+	if (symbols__try_delete_unannotated(&symbols, NULL, NULL) || symbols.cnt != 3) {
+		up_read(&symbols.lock);
+		pr_debug("symbols__try_delete_unannotated re-entered held read lock!\n");
+		goto out;
+	}
+	up_read(&symbols.lock);
+
+	/*
+	 * First shrink: reclaims symbols_by_name and trims excess capacity in
+	 * symbols.symbols while keeping all 3 symbols intact.
+	 */
+	if (symbols__shrink(&symbols) != 2 || symbols.cnt != 3 ||
+	    symbols.allocated != 3 || symbols.symbols_by_name != NULL) {
+		pr_debug("First symbols__shrink failed (cnt=%u, allocated=%u)!\n",
+			 symbols.cnt, symbols.allocated);
+		goto out;
+	}
+
+	/*
+	 * Second shrink with no symbols_by_name and no excess capacity is a no-op.
+	 */
+	if (symbols__shrink(&symbols) != 0 || symbols.cnt != 3) {
+		pr_debug("Second symbols__shrink failed (cnt=%u)!\n", symbols.cnt);
+		goto out;
+	}
+
+	ret = TEST_OK;
+out:
+	symbols__exit(&symbols);
+	if (was_singlethreaded)
+		perf_set_singlethreaded();
+	return ret;
+}
+
+static int test_dsos_shrink(void)
+{
+	struct dsos dsos;
+	struct dso *dso1, *dso2, *vdso;
+	int ret = TEST_FAIL;
+
+	dsos__init(&dsos);
+
+	dso1 = dsos__findnew_id(&dsos, "/tmp/perf_test_dso1", NULL);
+	dso2 = dsos__findnew_id(&dsos, "/tmp/perf_test_dso2", NULL);
+	vdso = dsos__findnew_id(&dsos, DSO__NAME_VDSO, NULL);
+	if (!dso1 || !dso2 || !vdso) {
+		dso__put(dso1);
+		dso__put(dso2);
+		dso__put(vdso);
+		goto out;
+	}
+
+	/*
+	 * Keep the caller's active reference on dso1; drop caller references on
+	 * dso2 and vdso.
+	 */
+	dso__put(dso2);
+	dso__put(vdso);
+
+	/*
+	 * First shrink: dso2 has refcnt == 1 and no symbols, so it is evicted;
+	 * dso1 (refcnt == 2) and vdso (dso__is_vdso() == true) are retained.
+	 */
+	if (dsos__shrink(&dsos) != 1 || dsos.cnt != 2) {
+		pr_debug("First dsos__shrink failed (cnt=%u)!\n", dsos.cnt);
+		dso__put(dso1);
+		goto out;
+	}
+
+	dso__put(dso1);
+	if (dsos__shrink(&dsos) != 1 || dsos.cnt != 1 || !dso__is_vdso(dsos.dsos[0])) {
+		pr_debug("Second dsos__shrink failed (cnt=%u)!\n", dsos.cnt);
+		goto out;
+	}
+
+	ret = TEST_OK;
+out:
+	dsos__exit(&dsos);
+	return ret;
+}
+
+static int test_session_alloc_shrink(void)
+{
+	struct perf_session *session = zalloc(sizeof(*session));
+	struct dso *dso;
+	char *p1 = NULL, *p2 = NULL, *p3 = NULL, *s = NULL, *tmp;
+	int ret = TEST_FAIL;
+
+	if (!session)
+		return TEST_FAIL;
+
+	if (machines__init(&session->machines, session))
+		goto out;
+	if (machine__session(&session->machines.host) != session) {
+		pr_debug("machine__session did not return owning session!\n");
+		goto out;
+	}
+
+	dso = machine__findnew_dso(&session->machines.host, "/tmp/perf_test_session_dso");
+	if (!dso)
+		goto out;
+	if (dso__session(dso) != session) {
+		pr_debug("dso__session did not return owning session!\n");
+		dso__put(dso);
+		goto out;
+	}
+	dso__put(dso);
+
+	p1 = perf_session__malloc(session, 64);
+	p2 = perf_session__zalloc(session, 64);
+	p3 = perf_session__calloc(session, 4, 16);
+	s = perf_session__strdup(session, "perf_session_alloc_test");
+	if (!p1 || !p2 || !p3 || !s || strcmp(s, "perf_session_alloc_test") != 0) {
+		pr_debug("perf_session allocation wrappers failed!\n");
+		goto out;
+	}
+
+	tmp = perf_session__realloc(session, p1, 128);
+	if (!tmp) {
+		pr_debug("perf_session__realloc failed!\n");
+		goto out;
+	}
+	p1 = tmp;
+
+	if (perf_session__shrink(session) == 0 || session->machines.host.dsos.cnt != 0) {
+		pr_debug("perf_session__shrink failed to evict unreferenced DSO!\n");
+		goto out;
+	}
+
+	ret = TEST_OK;
+out:
+	free(p1);
+	free(p2);
+	free(p3);
+	free(s);
+	machines__exit(&session->machines);
+	free(session);
+	return ret;
+}
+
+struct shrink_reload_find_args {
+	struct symbol *kept;
+	u64 first_start;
+	u64 second_start;
+	char *second_name;
+	u64 max_end;
+};
+
+static int shrink_reload_find_cb(struct symbol *sym, void *data)
+{
+	struct shrink_reload_find_args *args = data;
+
+	if (!args->kept) {
+		args->kept = sym;
+		args->first_start = symbol__start(sym);
+		args->max_end = symbol__end(sym);
+		return 0;
+	}
+	if (!args->second_name && symbol__start(sym) >= args->max_end) {
+		args->second_start = symbol__start(sym);
+		args->second_name = strdup(symbol__name(sym));
+		if (!args->second_name)
+			return -ENOMEM;
+	}
+	if (symbol__end(sym) > args->max_end)
+		args->max_end = symbol__end(sym);
+	return 0;
+}
+
+static int test_dso_shrink_and_reload(struct test_info *ti, char *filename, struct map **map_p)
+{
+	struct shrink_reload_find_args args = {
+		.kept = NULL,
+		.first_start = 0,
+		.second_start = 0,
+		.second_name = NULL,
+		.max_end = 0,
+	};
+	struct map *map = *map_p;
+	struct dso *dso = map__dso(map);
+	struct symbols *syms = dso__symbols(dso);
+	struct symbol *sym, *reloaded;
+	unsigned int initial_cnt;
+
+	if (!dso__can_shrink_symbols(dso) || dso__kernel(dso))
+		return TEST_SKIP;
+	initial_cnt = syms->cnt;
+	if (dso__for_each_symbol(dso, shrink_reload_find_cb, &args) < 0)
+		return TEST_FAIL;
+	if (!args.second_name)
+		return TEST_SKIP;
+
+	/*
+	 * While dso is referenced by map (refcnt > 1), dso__shrink() reclaims
+	 * auxiliary buffers (such as symbols_by_name) while keeping all symbols
+	 * and their pointers valid.
+	 */
+	dso__sort_by_name(dso);
+	if (syms->num_by_name < 2) {
+		free(args.second_name);
+		return TEST_FAIL;
+	}
+	{
+		size_t idx = 0;
+		struct symbol *expected_next = syms->symbols_by_name[1];
+
+		if (dso__shrink(dso) == 0 || syms->cnt != initial_cnt ||
+		    syms->symbols_by_name != NULL) {
+			pr_debug("dso__shrink on referenced DSO failed!\n");
+			free(args.second_name);
+			return TEST_FAIL;
+		}
+		if (dso__next_symbol_by_name(dso, &idx) != expected_next || idx != 1) {
+			pr_debug("dso__next_symbol_by_name failed to rebuild symbols_by_name!\n");
+			free(args.second_name);
+			return TEST_FAIL;
+		}
+		dso__shrink(dso);
+	}
+
+	sym = dso__find_symbol(dso, args.first_start);
+	if (sym != args.kept) {
+		pr_debug("Symbol pointer changed after dso__shrink on referenced DSO!\n");
+		free(args.second_name);
+		return TEST_FAIL;
+	}
+
+	/*
+	 * Verify struct dso_symbol retains a reference on its owning DSO even
+	 * after the map reference is dropped, preventing dsos__shrink() from
+	 * evicting the DSO's symbol table until dso_symbol__exit() is called.
+	 */
+	dso__set_hit(dso);
+	{
+		struct dso_symbol src = {
+			.dso = dso,
+			.sym = args.kept,
+		};
+		struct dso_symbol pinned;
+		struct inline_node *inode;
+		struct symbol *inlined_sym;
+		char *srcline;
+
+		dso_symbol__copy(&pinned, &src);
+		map__zput(*map_p);
+		dsos__shrink(&ti->machine->dsos);
+		if (!dso__loaded(pinned.dso) ||
+		    dso__symbols(pinned.dso)->cnt != initial_cnt ||
+		    dso__find_symbol(pinned.dso, args.first_start) != pinned.sym) {
+			pr_debug("dso_symbol failed to pin DSO symbols across dsos__shrink!\n");
+			dso_symbol__exit(&pinned);
+			free(args.second_name);
+			return TEST_FAIL;
+		}
+
+		/*
+		 * Populate dso->inlined_nodes (referencing both a base symbol
+		 * from dso->symbols and an owned inlined symbol) and
+		 * dso->srclines so dsos__shrink() exercises freeing inlines
+		 * before freeing the underlying symbol table.
+		 */
+		inode = zalloc(sizeof(*inode));
+		inlined_sym = new_inline_sym(pinned.dso, pinned.sym, "inlined_helper");
+		srcline = strdup("test.c:1");
+		if (!inode || !inlined_sym || !srcline) {
+			free(inode);
+			if (inlined_sym && symbol__inlined(inlined_sym))
+				symbol__delete(inlined_sym);
+			free(srcline);
+			dso_symbol__exit(&pinned);
+			free(args.second_name);
+			return TEST_FAIL;
+		}
+		inode->addr = args.first_start;
+		INIT_LIST_HEAD(&inode->val);
+		if (inline_list__append(inlined_sym, SRCLINE_UNKNOWN, inode) < 0) {
+			if (symbol__inlined(inlined_sym))
+				symbol__delete(inlined_sym);
+			inline_node__delete(inode);
+			free(srcline);
+			dso_symbol__exit(&pinned);
+			free(args.second_name);
+			return TEST_FAIL;
+		}
+		if (inline_list__append(pinned.sym, SRCLINE_UNKNOWN, inode) < 0) {
+			inline_node__delete(inode);
+			free(srcline);
+			dso_symbol__exit(&pinned);
+			free(args.second_name);
+			return TEST_FAIL;
+		}
+		inlines__tree_insert(dso__inlined_nodes(pinned.dso), inode);
+		srcline__tree_insert(dso__srclines(pinned.dso), args.first_start, srcline);
+		dso_symbol__exit(&pinned);
+	}
+
+	if (dsos__shrink(&ti->machine->dsos) == 0) {
+		pr_debug("dsos__shrink did not reclaim unreferenced DSO symbols!\n");
+		free(args.second_name);
+		return TEST_FAIL;
+	}
+
+	if (create_map(ti, filename, map_p) != TEST_OK) {
+		free(args.second_name);
+		return TEST_FAIL;
+	}
+	map = *map_p;
+	dso = map__dso(map);
+	syms = dso__symbols(dso);
+	if (!dso__hit(dso) || syms->cnt != 0 || dso__loaded(dso) ||
+	    inlines__tree_find(dso__inlined_nodes(dso), args.first_start) != NULL ||
+	    srcline__tree_find(dso__srclines(dso), args.first_start) != NULL) {
+		pr_debug("Unreferenced hit DSO state mismatch after dsos__shrink!\n");
+		free(args.second_name);
+		return TEST_FAIL;
+	}
+
+	/* Lookup via map__find_symbol transparently reloads the DSO. */
+	reloaded = map__find_symbol(map, args.second_start);
+	if (!reloaded || strcmp(symbol__name(reloaded), args.second_name) != 0) {
+		pr_debug("Failed to reload evicted symbol %s!\n", args.second_name);
+		free(args.second_name);
+		return TEST_FAIL;
+	}
+
+	if (syms->cnt != initial_cnt || !syms->sorted || !dso__loaded(dso)) {
+		pr_debug("Reloaded symbol table state mismatch (cnt=%u vs %u, sorted=%d, loaded=%d)!\n",
+			 syms->cnt, initial_cnt, syms->sorted, dso__loaded(dso));
+		free(args.second_name);
+		return TEST_FAIL;
+	}
+	for (unsigned int i = 1; i < syms->cnt; i++) {
+		if (symbol__start(syms->symbols[i - 1]) > symbol__start(syms->symbols[i])) {
+			pr_debug("Reloaded symbols out of order at %u!\n", i);
+			free(args.second_name);
+			return TEST_FAIL;
+		}
+	}
+
+	free(args.second_name);
+	return TEST_OK;
+}
+
 static int test_file(struct test_info *ti, char *filename)
 {
 	struct map *map = NULL;
@@ -213,18 +614,126 @@ static int test_file(struct test_info *ti, char *filename)
 	/* Module dso is split into many dsos by section */
 	if (ret == TEST_OK && dso__kernel(dso) != DSO_SPACE__USER)
 		ret = process_subdivided_dso(ti->machine, dso);
+
+	if (ret == TEST_OK) {
+		int shrink_ret = test_dso_shrink_and_reload(ti, filename, &map);
+
+		if (shrink_ret != TEST_SKIP)
+			ret = shrink_ret;
+	}
 out_put:
 	map__put(map);
 
 	return ret;
 }
 
+static int test_symbols_sorted_insert(void)
+{
+	struct dso *dso = dso__new("/tmp/perf_test_sorted_insert");
+	struct symbols *symbols;
+	int ret = TEST_FAIL;
+
+	if (!dso)
+		return TEST_FAIL;
+
+	symbols = dso__symbols(dso);
+
+	/*
+	 * Insert 100 odd-indexed symbols in ascending order via
+	 * symbols__insert(), then insert 5 even-indexed symbols out of order via
+	 * dso__insert_symbol() and verify symbols->sorted remains true.
+	 */
+	for (unsigned int i = 0; i < 100; i++) {
+		char name[32];
+		u64 addr = (u64)(i * 2 + 1) * 0x100;
+		struct symbol *sym;
+
+		snprintf(name, sizeof(name), "sym_%u", i * 2 + 1);
+		sym = symbol__new(addr, 0x40, STB_GLOBAL, STT_FUNC, name);
+		if (!sym || symbols__insert(symbols, sym))
+			goto out;
+	}
+	if (!symbols->sorted || symbols->cnt != 100) {
+		pr_debug("Expected sorted=true, cnt=100 after ordered inserts\n");
+		goto out;
+	}
+
+	for (unsigned int i = 0; i < 5; i++) {
+		char name[32];
+		u64 addr = (u64)(i * 20) * 0x100;
+		struct symbol *sym;
+
+		snprintf(name, sizeof(name), "ins_%u", i * 20);
+		sym = symbol__new(addr, 0x40, STB_GLOBAL, STT_FUNC, name);
+		if (!sym || dso__insert_symbol(dso, sym))
+			goto out;
+		if (!symbols->sorted) {
+			pr_debug("dso__insert_symbol did not preserve sorted invariant!\n");
+			goto out;
+		}
+	}
+
+	/*
+	 * Now append 95 remaining even-indexed symbols in descending order via
+	 * symbols__insert() to exercise the unsorted append + sort-on-lookup path.
+	 */
+	for (unsigned int i = 99; i > 0; i--) {
+		char name[32];
+		u64 addr;
+		struct symbol *sym;
+
+		if ((i * 2) % 20 == 0 && (i * 2) < 100)
+			continue;
+		addr = (u64)(i * 2) * 0x100;
+		snprintf(name, sizeof(name), "ins_%u", i * 2);
+		sym = symbol__new(addr, 0x40, STB_GLOBAL, STT_FUNC, name);
+		if (!sym || symbols__insert(symbols, sym))
+			goto out;
+	}
+	if (symbols->sorted || symbols->cnt != 200) {
+		pr_debug("Unexpected sorted=%d cnt=%u before lookup\n",
+			 symbols->sorted, symbols->cnt);
+		goto out;
+	}
+
+	for (unsigned int i = 0; i < 200; i++) {
+		u64 addr = (u64)i * 0x100 + 0x10;
+		struct symbol *sym = dso__find_symbol(dso, addr);
+
+		if (!sym || symbol__start(sym) != (u64)i * 0x100) {
+			pr_debug("Lookup failed at idx %u (addr=%#" PRIx64 ")\n", i, addr);
+			goto out;
+		}
+	}
+
+	ret = TEST_OK;
+out:
+	dso__put(dso);
+	return ret;
+}
+
 static int test__symbols(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
 {
 	char filename[PATH_MAX];
 	struct test_info ti;
 	int ret;
 
+	ret = test_symbols_sorted_insert();
+	if (ret != TEST_OK)
+		return ret;
+
+	ret = test_symbols_shrink();
+	if (ret != TEST_OK)
+		return ret;
+
+	ret = test_dsos_shrink();
+	if (ret != TEST_OK)
+		return ret;
+
+	ret = test_session_alloc_shrink();
+	if (ret != TEST_OK)
+		return ret;
+
 	ret = init_test_info(&ti);
 	if (ret != TEST_OK)
 		return ret;
@@ -240,24 +749,28 @@ static int test__symbols(struct test_suite *test __maybe_unused, int subtest __m
 
 struct kallsyms_sym {
 	u64 start;
+	u8 binding;
 	const char *name;
 };
 
 /*
  * kallsyms from an x86 host where nf_tables was loaded in the page right
  * after the last symbol of dca, plus a kernel symbol followed closely by a
- * module and a module far away from the others.
+ * module, a weak/global alias pair at the same address, and a module far
+ * away from the others.
  */
 static const struct kallsyms_sym kallsyms_syms[] = {
-	{ 0xffffffff81000000, "_stext" },
-	{ 0xffffffff81000ff0, "last_kernel_symbol" },
-	{ 0xffffffff81001000, "near_module_symbol\t[near]" },
-	{ 0xffffffffc0c4aa40, "dca_exit\t[dca]" },
-	{ 0xffffffffc0c4aa50, "dca_sysfs_exit\t[dca]" },
-	{ 0xffffffffc0c4b000, "__nft_trace_packet\t[nf_tables]" },
-	{ 0xffffffffc0c4b0b0, "nft_do_chain\t[nf_tables]" },
-	{ 0xffffffffc0c4b4e0, "nf_tables_core_module_exit\t[nf_tables]" },
-	{ 0xffffffffc2000000, "far_module_symbol\t[far]" },
+	{ 0xffffffff81000000, STB_GLOBAL, "_stext" },
+	{ 0xffffffff81000500, STB_WEAK, "weak_alias_symbol" },
+	{ 0xffffffff81000500, STB_GLOBAL, "global_target_symbol" },
+	{ 0xffffffff81000ff0, STB_GLOBAL, "last_kernel_symbol" },
+	{ 0xffffffff81001000, STB_GLOBAL, "near_module_symbol\t[near]" },
+	{ 0xffffffffc0c4aa40, STB_GLOBAL, "dca_exit\t[dca]" },
+	{ 0xffffffffc0c4aa50, STB_GLOBAL, "dca_sysfs_exit\t[dca]" },
+	{ 0xffffffffc0c4b000, STB_GLOBAL, "__nft_trace_packet\t[nf_tables]" },
+	{ 0xffffffffc0c4b0b0, STB_GLOBAL, "nft_do_chain\t[nf_tables]" },
+	{ 0xffffffffc0c4b4e0, STB_GLOBAL, "nf_tables_core_module_exit\t[nf_tables]" },
+	{ 0xffffffffc2000000, STB_GLOBAL, "far_module_symbol\t[far]" },
 };
 
 static int check_symbol(struct dso *dso, u64 addr, const char *name, u64 end)
@@ -287,7 +800,8 @@ static int test__kallsyms_fixup_end(struct test_suite *test __maybe_unused,
 		return TEST_FAIL;
 
 	for (unsigned int i = 0; i < ARRAY_SIZE(kallsyms_syms); i++) {
-		struct symbol *sym = symbol__new(kallsyms_syms[i].start, 0, 0, 0,
+		struct symbol *sym = symbol__new(kallsyms_syms[i].start, 0,
+						 kallsyms_syms[i].binding, STT_FUNC,
 						 kallsyms_syms[i].name);
 
 		if (!sym)
@@ -296,8 +810,13 @@ static int test__kallsyms_fixup_end(struct test_suite *test __maybe_unused,
 	}
 
 	symbols__fixup_end(dso__symbols(dso), true);
+	symbols__fixup_duplicate(dso__symbols(dso));
 
 	ret = TEST_OK;
+	/* Same-address weak/global alias pair resolves to global_target_symbol. */
+	if (check_symbol(dso, 0xffffffff81000510, "global_target_symbol",
+			 0xffffffff81000ff0))
+		ret = TEST_FAIL;
 	/* The next symbol is too close for the end of the page. */
 	if (check_symbol(dso, 0xffffffff81000ff8, "last_kernel_symbol",
 			 0xffffffff81001000))
Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#92 lore raw

Re: [PATCH v3 20/20] perf test symbols: Add tests for symbol shrinking and session alloc wrappers

Sashiko has reviewed this patch and found no issues.
Quoted 26 lines

Ensure the new symbol sorting, DSO-lifetime symbol and DSO shrinking, dso_symbol reference pinning, on-demand DSO reload after unreferenced eviction, and session-scoped allocation wrappers are verified by 'perf test symbols' (including under REFCNT_CHECKING=1).

Add unit tests to 'perf test symbols' covering:
- test_symbols_sorted_insert(): Verify ascending symbols__insert(), out-of-order dso__insert_symbol() maintaining sorted order, and unsorted symbols__insert() followed by sort-on-lookup in dso__find_symbol().
- test_symbols_shrink(): Verify symbols__shrink() and symbols__try_delete_unannotated() respect held locks without deadlocking, reclaim the secondary symbols_by_name index, and trim unused capacity in symbols->symbols while keeping live symbols intact.
- test_dsos_shrink(): Verify dsos__shrink() evicts unreferenced user DSOs while retaining DSOs with active external references and vDSOs.
- test_session_alloc_shrink(): Verify machine__session(), dso__session(), perf_session__malloc/zalloc/calloc/realloc/strdup(), and perf_session__shrink().
- test_dso_shrink_and_reload(): Verify dso__shrink() on a referenced ELF DSO reclaims auxiliary buffers while preserving symbol pointers, dso_symbol__copy() pins DSO symbols across dsos__shrink() until dso_symbol__exit(), and dsos__shrink() on an unreferenced hit DSO evicts its symbol table and transparently reloads it on a subsequent map__find_symbol().

Quoted 1 line

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20
author reviewer
#93 lore raw

v4 cover perf symbol: Sorted arrays, DSO-owned lifetimes, and memory shrinking

In reply to Ian Rogers

When profiling large binaries or long-running workloads with many DSOs and threads, symbol and DSO tables frequently dominate perf's memory footprint and startup latency. Currently struct symbol has several structural limitations:

1. Every struct symbol embeds a 24-byte struct rb_node for the dso->symbols address rbtree, plus an optional negative-offset allocation prefix (symbol_conf.priv_size) for TUI browser indices, db-export IDs, or struct annotation state even when only a tiny fraction of symbols are ever annotated or exported.
2. dso__symbols() has no internal synchronization against concurrent readers/writers, and while most symbol holders (map_symbol, addr_location) already pin the owning struct dso via map__get(), a handful of long-lived symbol holders (hist_entry->parent, call_path, and db-export) retain raw struct symbol * pointers without retaining a reference on the owning struct dso, preventing unreferenced DSOs and their symbol tables from being safely reclaimed under memory pressure.
3. ELF symbol table loading (dso__load_sym_internal()) repeatedly invokes libelf section/program header lookups per symbol, calls elf_sym__name() before filtering out non-function/non-allocated symbols, runs Rust/Java demanglers on plain C symbols without fast-path prefix checks, and resorts dso->symbols with qsort() even when ELF symbol tables are already largely ordered in ascending address runs.

This series modernizes struct symbol storage, ties symbol lifetimes explicitly to their owning struct dso (rather than adding per-symbol reference counting), removes symbol_conf.priv_size and perf_singlethreaded, speeds up ELF symbol and map loading/sorting, and adds on-demand session memory shrinking:

- **Fixes and preparatory locking cleanups (Patches 1-7)**:
- Patch 1 fixes a NULL pointer dereference in __machine__findnew_thread() when threads__findnew() fails to allocate a thread.
- Patch 2 snapshots shard threads and drops table->lock before invoking callbacks in threads__for_each_thread() to avoid read-to-write self-deadlock when a callback looks up or creates a leader thread in the same shard.
- Patch 3 removes perf_singlethreaded so struct rw_semaphore always uses pthread_rwlock_t (allowing non-blocking trylock operations to reliably detect locks already held on the current call stack during OOM shrinking) and guards di_cache in util/annotate.c with a mutex.
- Patch 4 fixes a heap buffer corruption in the TUI map browser when symbol_conf.priv_size was not initialized with sizeof(u32) by computing symbol browser indices on demand.
- Patch 5 protects the machines->guests rbtree with a rw_semaphore.
- Patch 6 constifies dso in dso__is_vdso().
- Patch 7 adds down_read_trylock() and down_write_trylock() to

    `util/rwsem.[ch]`.

- **DSO-owned symbol lifetimes and priv_size removal (Patches 8-11)**:
- Patches 8-9 introduce struct dso_symbol ({ struct dso *dso; struct symbol *sym; }) so callers that borrow a symbol's lifetime directly from its owning struct dso without a struct map (hist_entry parent symbols, call_path nodes, and db-export) retain a dso reference for the lifetime of the symbol pointer. (Consolidating map_symbol and dso_symbol back into symbol.h as explicit DSO/map-borrowed symbol handle types is left for follow-up cleanup.)
- Patches 10-11 move db-export symbol IDs and struct annotation into on-demand hashmaps and remove symbol_conf.priv_size and symbol__priv().
- **Benchmarks and loading/sorting optimizations (Patches 12-16)**:
- Patches 12-13 add standalone perf bench internals symbols and perf bench internals maps subcommands.
- Patch 14 adds fast-path prefix checks for the Rust and Java demanglers.
- Patch 15 caches ELF program and section headers in dso__load_sym_internal() and defers elf_sym__name() lookups until after non-symbol/non-allocated entries are filtered out.
- Patch 16 adds an O(1) tail-insertion fast path and incremental binary insertion sort to struct maps.
- **Sorted symbol arrays and natural-run merge sort (Patches 17-19)**:
- Patch 17 encapsulates struct symbol field accesses behind inline accessor functions in preparation for lazy symbol/string loading.
- Patch 18 replaces dso->symbols (rb_root_cached) and dso->symbol_names with a rw_semaphore-protected struct symbols container backed by sorted pointer arrays, removing struct rb_node from struct symbol (shrinking sizeof(struct symbol) from 48 to 24 bytes, or 16 bytes net per symbol after accounting for the array pointer).
- Patch 19 replaces qsort() on struct symbols with a stable natural-run merge sort, galloping merge, and a sorted tail buffer with amortized sqrt(N) merge for out-of-order insertions.
- **On-demand memory shrinking and tests (Patches 20-23)**:
- Patch 20 adds symbols__shrink(), dso__shrink(), dsos__shrink(),

    `machine__shrink()`, `machines__shrink()`, and `perf_session__shrink()`
    to reclaim `symbols_by_name`, excess array capacity, cached DSO file
    descriptors/data/DWARF state, and unreferenced user DSOs (`refcnt == 1`
    with no annotated symbols), with transparent on-demand reload via
    `dso__load()`.

- Patches 21-22 add dso__session() and perf_session__malloc() / zalloc() / calloc() / realloc() / strdup() wrappers that trigger perf_session__shrink() and retry once on allocation failure.
- Patch 23 adds unit tests in perf test symbols covering sorted symbol insertion, equal-start aliases, symbol/DSO shrinking, dso_symbol lifetime pinning, inline frame/srcline cleanup ordering, symbols_by_name on-demand rebuild, transparent DSO reload, and session allocation wrappers.

Changes in v4:
- Add standalone fix for NULL th dereference on allocation failure in __machine__findnew_thread() (Patch 1) so machine__findnew_thread() can shrink the session and retry cleanly (Sashiko).
- Fix table->lock read-to-write self-deadlock in threads__for_each_thread() (Patch 2) and remove perf_singlethreaded (Patch 3) so rw_semaphore trylock operations always detect locks held on the current call stack during OOM shrinking (Sashiko).
- Handle equal-start symbol groups where a sized symbol precedes a zero-length or shorter alias at the same start address (Patches 18-19):
use upper_bound plus equal-start rewind in symbols__find_range(), avoid inflating trailing zero-length aliases in symbols__fixup_end() when a sized symbol already exists at that address, scan trailing equal-start groups for maximum symbol__end() in map__fixup_end(), and always check the unmerged sorted tail in symbols__find() when sorted_cnt < cnt (Sashiko).
- Preserve owning perf_session across split kallsyms/kcore and ELF kernel DSOs (ndso->dsos == NULL) in maps__split_kallsyms(), maps__split_kallsyms_for_kcore(), and dso__load_sym_internal() (Patch 22), use down_write_trylock() in machines__shrink() (Patch 20), and add unit tests for equal-start aliases, unmerged tail lookups, and same-shard perf_session__e_machine() (Patch 23) (Sashiko).
- Rebase onto perf-tools-next (4af65363b11f).

Changes in v3:
- Drop per-symbol DECLARE_RC_STRUCT(symbol) reference counting and partial symbol-table retention (symbols__merge_retained()) in favor of DSO-owned symbol lifetimes via struct dso_symbol (Patches 8-9), evicting a DSO's symbol table only as a whole when dso has refcnt == 1 and no annotated symbols (Namhyung Kim, Alireza Haghdoost).
- Reorder the series so bug fixes and small preparatory refactorings come first ahead of the larger struct symbol accessor and container patches.
- Split the perf bench internals patch into two smaller standalone patches (symbols and maps), separate dso__load() timing and Max RSS from dso__sort_by_name() so address-only/lazy symbol loading is measured independently of symbol name sorting, replace mallinfo2() (and its build feature test) with getrusage() ru_maxrss, skip stripped binaries (nr == 0) cleanly, call symbol__exit(), and include example output in the commit messages (Namhyung Kim, Alireza Haghdoost).
- Split out and land upstream the independent fixes uncovered during v2
review: c4baf52c7901 ("perf callchain: Fix loss of symbol in merge_chain_branch()"), 854dd5e7d099 ("perf annotate: Fix block_range symbol loss on split and leaks on exit"), and baef30dd8308 ("perf
symbol-elf: Fix zero-size symbol end calculation across .symtab and .dynsym") (Alireza Haghdoost).
- Remove the hash_bits(..., 16) 64K-bucket cap in annotate and db-export hashmaps, set symbol__set_annotated() on the existing-entry write path, and share the empty-hashmap cleanup path on zalloc() failure in symbol__annotation() (Alireza Haghdoost, Sashiko).
- Preserve insertion order on equal-start symbols (symbol__start(a) <= symbol__start(b)), use stable insertion sort instead of qsort() as the allocation-failure fallback, keep kallsyms "last alias wins" behavior in symbols__fixup_end(), and add a sorted tail buffer with amortized sqrt(N) merge in __symbols__insert_sorted() to avoid O(n^2) memmove() on random insertions (Alireza Haghdoost).
- Retry dso_cache_io() under dso__lock(dso) if dso__shrink() evicted a newly populated cache entry, free dso->inlined_nodes and dso->srclines before symbols__delete_locked() in dso__shrink(), and rebuild symbols_by_name on demand via symbols__sort_by_name_read_lock() in dso__next_symbol_by_name(), dso__find_symbol_by_name(), and symbols__visit_by_name() (Alireza Haghdoost, Sashiko).
- Expand perf test symbols (Patch 23) to return TEST_SKIP on unshrinkable DSOs, verify full symbol count and sorted order after reload, and test dso_symbol pinning, inline/srcline eviction ordering, and dso__next_symbol_by_name() index rebuild after dso__shrink() (Alireza Haghdoost, Sashiko).

v3: https://lore.kernel.org/linux-perf-users/[email protected]/
v2: https://lore.kernel.org/linux-perf-users/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/

AI Assistance Disclosure (per Documentation/process/coding-assistants.rst and Documentation/process/generated-content.rst):
- Tools used: Google Antigravity / Jetski coding assistant and Sashiko automated patch review (sashiko.dev and internal Sashiko).
- Summary of prompts and assistance: Interactive pair-programming session to replace per-symbol refcounting with struct dso_symbol DSO-borrowed lifetimes, reorder fixes and preparatory patches ahead of the struct symbol array conversion, remove perf_singlethreaded, split and refine the symbols and maps benchmarks, and address LKML reviewer and Sashiko feedback across machine, threads, annotate, dso, map, symbol, and tests/symbols.c.
- Testing performed: Built with Clang (-Wthread-safety), AddressSanitizer, LeakSanitizer, and DEBUG=1 (REFCNT_CHECKING=1); ran perf test (including Symbols, Maps, DSO data, hists, thread, annotate, report, and script suites), perf bench internals (synthesize, symbols, and maps), and scripts/checkpatch.pl.

Ian Rogers (23):
perf machine: Fix NULL deref on allocation failure in

    __machine__findnew_thread()

perf threads: Avoid table->lock self-deadlock in
threads__for_each_thread()
perf tools: Remove perf_singlethreaded
perf map_browser: Fix browser index heap corruption by computing on
demand
perf machine: Protect machines->guests rbtree with a rw_semaphore perf vdso: Constify dso parameter in dso__is_vdso()
perf rwsem: Add down_read_trylock() and down_write_trylock() perf map_symbol: Add struct dso_symbol and use for parent symbols perf call-path: Use struct dso_symbol to retain symbol DSO lifetimes perf db-export: Store symbol database IDs in a hashmap perf annotate: Move struct annotation to a hashmap and remove
symbol_conf.priv_size
perf bench internals: Add symbols benchmark
perf bench internals: Add maps benchmark
perf demangle: Add fast-path prefix checks for Rust and Java
demanglers
perf symbol-elf: Cache ELF program/section headers and defer symbol
name lookup
perf maps: Optimize tail insertion and small-append sorting perf symbol: Add accessor functions for struct symbol fields perf symbol: Switch backing storage from rbtree to struct symbols
array
perf symbol: Optimize symbol sorting with natural-run merge sort and
binary insertion
perf symbol: Add memory shrinking for symbols, DSOs, and machines perf session: Add dso__session() and session-scoped allocation
wrappers
perf symbol: Use session allocation wrappers in symbol, DSO, thread,
and hist paths
perf test symbols: Add tests for symbol shrinking and session alloc
wrappers

 tools/perf/Documentation/perf-bench.txt       |    6 +
 .../arch/powerpc/util/skip-callchain-idx.c    |    2 +-
 tools/perf/arch/powerpc/util/sym-handling.c   |   40 +-
 tools/perf/bench/Build                        |    2 +
 tools/perf/bench/bench.h                      |    2 +
 tools/perf/bench/maps.c                       |  202 +++
 tools/perf/bench/symbols.c                    |  273 ++++
 tools/perf/bench/synthesize.c                 |    7 -
 tools/perf/builtin-annotate.c                 |   18 +-
 tools/perf/builtin-bench.c                    |    2 +
 tools/perf/builtin-c2c.c                      |   14 -
 tools/perf/builtin-diff.c                     |    4 +-
 tools/perf/builtin-kallsyms.c                 |    7 +-
 tools/perf/builtin-kmem.c                     |   70 +-
 tools/perf/builtin-kwork.c                    |    4 +-
 tools/perf/builtin-lock.c                     |   10 +-
 tools/perf/builtin-mem.c                      |    2 +-
 tools/perf/builtin-record.c                   |    5 +-
 tools/perf/builtin-report.c                   |   14 -
 tools/perf/builtin-sched.c                    |    6 +-
 tools/perf/builtin-script.c                   |   16 +-
 tools/perf/builtin-timechart.c                |    2 +-
 tools/perf/builtin-top.c                      |   28 +-
 tools/perf/builtin-trace.c                    |   22 +-
 tools/perf/tests/builtin-test.c               |    1 -
 tools/perf/tests/dlfilter-test.c              |    4 +-
 tools/perf/tests/dwarf-unwind.c               |    2 +-
 tools/perf/tests/hists_common.c               |    9 +-
 tools/perf/tests/hists_cumulate.c             |    4 +-
 tools/perf/tests/hists_output.c               |    2 +-
 tools/perf/tests/maps.c                       |   67 +
 tools/perf/tests/symbols.c                    |  741 +++++++++-
 tools/perf/tests/vmlinux-kallsyms.c           |  181 +--
 tools/perf/ui/browser.c                       |    3 +
 tools/perf/ui/browsers/annotate.c             |   10 +-
 tools/perf/ui/browsers/hists.c                |   26 +-
 tools/perf/ui/browsers/map.c                  |   85 +-
 tools/perf/ui/gtk/annotate.c                  |    6 +-
 tools/perf/ui/stdio/hist.c                    |   12 +-
 .../perf/util/annotate-arch/annotate-arm64.c  |   12 +-
 .../util/annotate-arch/annotate-loongarch.c   |    4 +-
 tools/perf/util/annotate-arch/annotate-x86.c  |   14 +-
 tools/perf/util/annotate-data.c               |   12 +-
 tools/perf/util/annotate.c                    |  255 +++-
 tools/perf/util/annotate.h                    |    6 +-
 tools/perf/util/auxtrace.c                    |  111 +-
 tools/perf/util/block-info.c                  |    8 +-
 tools/perf/util/block-range.c                 |    3 +-
 tools/perf/util/bpf_lock_contention.c         |   16 +-
 tools/perf/util/c2c-function.c                |   19 +-
 tools/perf/util/call-path.c                   |   42 +-
 tools/perf/util/call-path.h                   |   10 +-
 tools/perf/util/callchain.c                   |   22 +-
 tools/perf/util/callchain.h                   |    3 +-
 tools/perf/util/capstone.c                    |   20 +-
 tools/perf/util/data-convert-json.c           |    4 +-
 tools/perf/util/db-export.c                   |  144 +-
 tools/perf/util/db-export.h                   |   16 +
 tools/perf/util/debug.c                       |    3 +-
 tools/perf/util/demangle-java.c               |   23 +-
 tools/perf/util/disasm.c                      |   49 +-
 tools/perf/util/dlfilter.c                    |   12 +-
 tools/perf/util/dso.c                         |  263 +++-
 tools/perf/util/dso.h                         |   83 +-
 tools/perf/util/dsos.c                        |   77 ++
 tools/perf/util/dsos.h                        |    3 +
 tools/perf/util/event.c                       |    4 +-
 tools/perf/util/evsel_fprintf.c               |    2 +-
 tools/perf/util/hist.c                        |   86 +-
 tools/perf/util/hist.h                        |    8 +-
 tools/perf/util/intel-pt.c                    |   74 +-
 tools/perf/util/libbfd.c                      |    7 +-
 tools/perf/util/llvm.c                        |    6 +-
 tools/perf/util/lock-contention.c             |    2 +-
 tools/perf/util/machine.c                     |  174 ++-
 tools/perf/util/machine.h                     |   14 +-
 tools/perf/util/map.c                         |   38 +-
 tools/perf/util/map.h                         |   10 +-
 tools/perf/util/map_symbol.c                  |   30 +
 tools/perf/util/map_symbol.h                  |   11 +
 tools/perf/util/maps.c                        |  281 +++-
 tools/perf/util/mutex.h                       |    3 +
 tools/perf/util/probe-event.c                 |  133 +-
 tools/perf/util/python.c                      |   65 +-
 tools/perf/util/rwsem.c                       |   29 +-
 tools/perf/util/rwsem.h                       |   11 +
 tools/perf/util/session.c                     |   78 ++
 tools/perf/util/session.h                     |   14 +-
 tools/perf/util/sort.c                        |   49 +-
 tools/perf/util/srcline.c                     |   16 +-
 tools/perf/util/symbol-elf.c                  |  274 +++-
 tools/perf/util/symbol.c                      | 1228 +++++++++++++----
 tools/perf/util/symbol.h                      |  146 +-
 tools/perf/util/symbol_conf.h                 |    1 -
 tools/perf/util/symbol_fprintf.c              |   37 +-
 tools/perf/util/thread-stack.c                |   48 +-
 tools/perf/util/threads.c                     |   35 +-
 tools/perf/util/unwind-libdw.c                |    2 +-
 tools/perf/util/util.c                        |   12 -
 tools/perf/util/util.h                        |    5 -
 tools/perf/util/vdso.c                        |    2 +-
 tools/perf/util/vdso.h                        |    2 +-
 102 files changed, 4695 insertions(+), 1387 deletions(-)
 create mode 100644 tools/perf/bench/maps.c
 create mode 100644 tools/perf/bench/symbols.c
Signature · 1 line
2.56.1.703.gf848ed807c-goog
author reviewer
#94 lore raw

PATCH v4 1/23 perf machine: Fix NULL deref on allocation failure in __machine__findnew_thread()

When threads__findnew() fails to allocate a struct thread (or fails to insert it into the shard hashmap and no concurrent thread inserted the tid), it returns NULL with *created == false.

In __machine__findnew_thread(), the !created branch unconditionally passed th to machine__update_thread_pid(), which immediately dereferenced NULL via thread__pid(th) instead of propagating NULL to the caller.

Guard the machine__update_thread_pid() call with 'else if (th)' so allocation failure returns NULL cleanly.

Fixes d436f90a64f3 ("perf machine: Move machine's threads into its own abstraction")
---
 tools/perf/util/machine.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/machine.c +2 -1
index 5eb5efa4eb90..dc0aaac992f9 100644
@@ -568,8 +568,9 @@ static struct thread *__machine__findnew_thread(struct machine *machine,
 			thread__put(th);
 			return NULL;
 		}
-	} else
+	} else if (th) {
 		machine__update_thread_pid(machine, th, pid);
+	}
 
 	return th;
 }
Signature · 1 line
2.56.1.703.gf848ed807c-goog
#95 lore raw

Re: [PATCH v4 01/23] perf machine: Fix NULL deref on allocation failure in __machine__findnew_thread()

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

When threads__findnew() fails to allocate a struct thread (or fails to insert it into the shard hashmap and no concurrent thread inserted the tid), it returns NULL with *created == false.

In __machine__findnew_thread(), the !created branch unconditionally passed th to machine__update_thread_pid(), which immediately dereferenced NULL via thread__pid(th) instead of propagating NULL to the caller.

Quoted 5 lines

Guard the machine__update_thread_pid() call with 'else if (th)' so allocation failure returns NULL cleanly.

Fixes d436f90a64f3 ("perf machine: Move machine's threads into its own abstraction")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#96 lore raw

PATCH v4 2/23 perf threads: Avoid table->lock self-deadlock in threads__for_each_thread()

In reply to Ian Rogers

threads__for_each_thread() holds down_read(&table->lock) while invoking its callback fn(). Callbacks such as perf_session__e_machine_cb() call thread__e_machine_endian(), which calls machine__findnew_thread(machine, pid, pid) when pid != tid. Both threads__find() (via threads_table_entry__set_last_match()) and threads__findnew() acquire down_write(&table->lock) on pid's shard, triggering a read-to-write self-deadlock whenever pid and tid hash to the same shard.

Snapshot each shard's threads with thread__get() references (using a stack buffer with a heap fallback for large shards) and release table->lock before invoking fn(), matching maps__load_maps().

Fixes d436f90a64f3 ("perf machine: Move machine's threads into its own abstraction")
---
 tools/perf/util/threads.c | 35 ++++++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 7 deletions(-)
tools/perf/util/threads.c +28 -7
index 6ca0b178fb6c..5db81b9debb5 100644
@@ -1,4 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
+#include <errno.h>
+#include <stdlib.h>
+#include <linux/kernel.h>
 #include "threads.h"
 #include "machine.h"
 #include "thread.h"
@@ -171,20 +174,38 @@ int threads__for_each_thread(struct threads *threads,
 {
 	for (int i = 0; i < THREADS__TABLE_SIZE; i++) {
 		struct threads_table_entry *table = &threads->table[i];
+		struct thread *stack_threads[32];
+		struct thread **shard_threads = stack_threads;
 		struct hashmap_entry *cur;
-		size_t bkt;
+		size_t bkt, cnt, idx = 0;
+		int rc = 0;
 
 		down_read(&table->lock);
-		hashmap__for_each_entry(&table->shard, cur, bkt) {
-			int rc = fn((struct thread *)cur->pvalue, data);
-
-			if (rc != 0) {
+		cnt = hashmap__size(&table->shard);
+		if (cnt == 0) {
+			up_read(&table->lock);
+			continue;
+		}
+		if (cnt > ARRAY_SIZE(stack_threads)) {
+			shard_threads = malloc(cnt * sizeof(*shard_threads));
+			if (!shard_threads) {
 				up_read(&table->lock);
-				return rc;
+				return -ENOMEM;
 			}
 		}
+		hashmap__for_each_entry(&table->shard, cur, bkt)
+			shard_threads[idx++] = thread__get((struct thread *)cur->pvalue);
 		up_read(&table->lock);
+
+		for (size_t j = 0; j < idx; j++) {
+			if (rc == 0)
+				rc = fn(shard_threads[j], data);
+			thread__put(shard_threads[j]);
+		}
+		if (shard_threads != stack_threads)
+			free(shard_threads);
+		if (rc != 0)
+			return rc;
 	}
 	return 0;
-
 }
Signature · 1 line
2.56.1.703.gf848ed807c-goog
#97 lore raw

Re: [PATCH v4 02/23] perf threads: Avoid table->lock self-deadlock in threads__for_each_thread()

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

threads__for_each_thread() holds down_read(&table->lock) while invoking its callback fn(). Callbacks such as perf_session__e_machine_cb() call thread__e_machine_endian(), which calls machine__findnew_thread(machine, pid, pid) when pid != tid. Both threads__find() (via threads_table_entry__set_last_match()) and threads__findnew() acquire down_write(&table->lock) on pid's shard, triggering a read-to-write self-deadlock whenever pid and tid hash to the same shard.

Quoted 6 lines

Snapshot each shard's threads with thread__get() references (using a stack buffer with a heap fallback for large shards) and release table->lock before invoking fn(), matching maps__load_maps().

Fixes d436f90a64f3 ("perf machine: Move machine's threads into its own abstraction")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author reviewer
#98 lore raw

PATCH v4 3/23 perf tools: Remove perf_singlethreaded

In reply to Ian Rogers

perf_singlethreaded bypassed pthread_rwlock operations in struct rw_semaphore by default unless callers manually toggled perf_set_multithreaded(). Because uncontended pthread_rwlock operations in userspace are cheap, keeping a global single-threaded bypass flag is unnecessary and prevents rw_semaphore trylock operations from detecting locks already held on the current call stack during OOM shrinking.

Remove perf_singlethreaded, perf_set_singlethreaded(), and perf_set_multithreaded() so rw_semaphore always uses pthread_rwlock_t (or struct mutex when RWS_ERRORCHECK is enabled), and guard di_cache in util/annotate.c with a mutex instead of asserting perf_singlethreaded.

---
 tools/perf/bench/synthesize.c |  7 -----
 tools/perf/builtin-record.c   |  5 +---
 tools/perf/builtin-script.c   |  2 --
 tools/perf/builtin-top.c      |  6 -----
 tools/perf/util/annotate.c    | 50 ++++++++++++++++++++---------------
 tools/perf/util/rwsem.c       |  9 +++----
 tools/perf/util/util.c        | 12 ---------
 tools/perf/util/util.h        |  5 ----
 8 files changed, 34 insertions(+), 62 deletions(-)
tools/perf/bench/synthesize.c +0 -7
index 265d49a913d9..31bdf07ebb50 100644
@@ -118,7 +118,6 @@ static int run_single_threaded(void)
 	struct perf_env host_env;
 	int err;
 
-	perf_set_singlethreaded();
 	perf_env__init(&host_env);
 	session = __perf_session__new(/*data=*/NULL, /*tool=*/NULL,
 				      /*trace_event_repipe=*/false, &host_env);
@@ -231,11 +230,6 @@ static int run_multi_threaded(void)
 	for (nr_threads_synthesize = min_threads;
 	     nr_threads_synthesize <= max_threads;
 	     nr_threads_synthesize++) {
-		if (nr_threads_synthesize == 1)
-			perf_set_singlethreaded();
-		else
-			perf_set_multithreaded();
-
 		printf("  Number of synthesis threads: %u\n",
 			nr_threads_synthesize);
 
@@ -243,7 +237,6 @@ static int run_multi_threaded(void)
 		if (err)
 			return err;
 	}
-	perf_set_singlethreaded();
 	return 0;
 }
 
tools/perf/builtin-record.c +1 -4
index ad81458989b5..55a14dbe458b 100644
@@ -2210,7 +2210,6 @@ static int record__synthesize(struct record *rec, bool tail)
 
 	if (rec->opts.nr_threads_synthesize > 1) {
 		mutex_init(&synth_lock);
-		perf_set_multithreaded();
 		f = process_locked_synthesized_event;
 	}
 
@@ -2223,10 +2222,8 @@ static int record__synthesize(struct record *rec, bool tail)
 						    rec->opts.nr_threads_synthesize);
 	}
 
-	if (rec->opts.nr_threads_synthesize > 1) {
-		perf_set_singlethreaded();
+	if (rec->opts.nr_threads_synthesize > 1)
 		mutex_destroy(&synth_lock);
-	}
 
 out:
 	return err;
tools/perf/builtin-script.c +0 -2
index b6691ebb1b4e..393c8d45d917 100644
@@ -4361,8 +4361,6 @@ int cmd_script(int argc, const char **argv)
 	};
 	struct perf_env *env;
 
-	perf_set_singlethreaded();
-
 	argc = parse_options_subcommand(argc, argv, options, NULL, script_usage,
 			     PARSE_OPT_STOP_AT_NON_OPTION);
 
tools/perf/builtin-top.c +0 -6
index a3442d2d6edf..ff2f928fe515 100644
@@ -1269,9 +1269,6 @@ static int __cmd_top(struct perf_top *top)
 	if (perf_session__register_idle_thread(top->session) < 0)
 		return ret;
 
-	if (top->nr_threads_synthesize > 1)
-		perf_set_multithreaded();
-
 	init_process_thread(top);
 
 	if (opts->record_namespaces)
@@ -1300,8 +1297,6 @@ static int __cmd_top(struct perf_top *top)
 				    evlist__core(top->evlist)->threads, true, false,
 				    top->nr_threads_synthesize);
 
-	perf_set_multithreaded();
-
 	if (perf_hpp_list.socket) {
 		ret = perf_env__read_cpu_topology_map(perf_session__env(top->session));
 		if (ret < 0) {
@@ -1400,7 +1395,6 @@ static int __cmd_top(struct perf_top *top)
 out_join_thread:
 	cond_signal(&top->qe.cond);
 	pthread_join(thread_process, NULL);
-	perf_set_singlethreaded();
 	exit_process_thread(top);
 	return ret;
 }
tools/perf/util/annotate.c +29 -21

117 lines not on this page · raw message

tools/perf/util/rwsem.c +4 -5
index 9d26832398db..1d15c0e3732b 100644
@@ -1,5 +1,4 @@
 // SPDX-License-Identifier: GPL-2.0
-#include "util.h"
 #include "rwsem.h"
 
 #if RWS_ERRORCHECK
@@ -33,7 +32,7 @@ int down_read(struct rw_semaphore *sem)
 	mutex_lock(&sem->mtx);
 	return 0;
 #else
-	return perf_singlethreaded ? 0 : pthread_rwlock_rdlock(&sem->lock);
+	return pthread_rwlock_rdlock(&sem->lock);
 #endif
 }
 
@@ -44,7 +43,7 @@ int up_read(struct rw_semaphore *sem)
 	mutex_unlock(&sem->mtx);
 	return 0;
 #else
-	return perf_singlethreaded ? 0 : pthread_rwlock_unlock(&sem->lock);
+	return pthread_rwlock_unlock(&sem->lock);
 #endif
 }
 
@@ -55,7 +54,7 @@ int down_write(struct rw_semaphore *sem)
 	mutex_lock(&sem->mtx);
 	return 0;
 #else
-	return perf_singlethreaded ? 0 : pthread_rwlock_wrlock(&sem->lock);
+	return pthread_rwlock_wrlock(&sem->lock);
 #endif
 }
 
@@ -66,6 +65,6 @@ int up_write(struct rw_semaphore *sem)
 	mutex_unlock(&sem->mtx);
 	return 0;
 #else
-	return perf_singlethreaded ? 0 : pthread_rwlock_unlock(&sem->lock);
+	return pthread_rwlock_unlock(&sem->lock);
 #endif
 }
tools/perf/util/util.c +0 -12
index 8f7cd32f524d..2f2674d17887 100644
@@ -31,18 +31,6 @@
 
 const char *input_name;
 
-bool perf_singlethreaded = true;
-
-void perf_set_singlethreaded(void)
-{
-	perf_singlethreaded = true;
-}
-
-void perf_set_multithreaded(void)
-{
-	perf_singlethreaded = false;
-}
-
 int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
 int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
 
tools/perf/util/util.h +0 -5
index f8f1ff603c72..2321abfae2a4 100644
@@ -59,11 +59,6 @@ int scandirat(int dirfd, const char *dirp,
 	      int (*compar)(const struct dirent **, const struct dirent **));
 #endif
 
-extern bool perf_singlethreaded;
-
-void perf_set_singlethreaded(void);
-void perf_set_multithreaded(void);
-
 char *perf_exe(char *buf, int len);
 
 #ifndef O_CLOEXEC
Signature · 1 line
2.56.1.703.gf848ed807c-goog
#99 lore raw

Re: [PATCH v4 03/23] perf tools: Remove perf_singlethreaded

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

perf_singlethreaded bypassed pthread_rwlock operations in struct rw_semaphore by default unless callers manually toggled perf_set_multithreaded(). Because uncontended pthread_rwlock operations in userspace are cheap, keeping a global single-threaded bypass flag is unnecessary and prevents rw_semaphore trylock operations from detecting locks already held on the current call stack during OOM shrinking.

Quoted 6 lines

Remove perf_singlethreaded, perf_set_singlethreaded(), and perf_set_multithreaded() so rw_semaphore always uses pthread_rwlock_t (or struct mutex when RWS_ERRORCHECK is enabled), and guard di_cache in util/annotate.c with a mutex instead of asserting perf_singlethreaded.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author reviewer
#100 lore raw

PATCH v4 4/23 perf map_browser: Fix browser index heap corruption by computing on demand

In reply to Ian Rogers

Commit 259dce914e93 ("perf symbol: Remove symbol_name_rb_node") removed the 24-byte symbol_name_rb_node reservation from symbol_conf.priv_size, but left symbol__browser_index() in ui/browsers/map.c subtracting sizeof(struct rb_node) + sizeof(u32) (28 bytes on 64-bit) ahead of struct symbol. In 'perf report -v' and 'perf c2c report -v', symbol_conf.priv_size is sizeof(struct annotation) + sizeof(u32) (20 bytes on 64-bit), so opening the TUI map browser writes a u32 index 8 bytes before each symbol's calloc() buffer, corrupting heap metadata.

Remove symbol__browser_index() in ui/browsers/map.c and compute the TUI map browser index on demand in map_browser__search() by walking the map browser's symbol entries when the user searches with '/'.

While this changes finding a symbol's display row index in map_browser__search() from O(1) to O(N) in the number of symbols in that single map, map_browser__search() is only invoked interactively when a user opens the TUI "Browse map details" view for a map, presses '/', types a query, and hits ENTER. Even for a large DSO such as vmlinux (~100k symbols), scanning the map's symbols once per interactive search takes well under a millisecond.

Previously, '/' search in the TUI map browser was gated behind 'verbose > 0' (-v) in builtin-c2c.c, builtin-report.c, and ui/browsers/map.c solely to avoid adding sizeof(u32) to symbol_conf.priv_size for every struct symbol across all DSOs in normal runs. With on-demand index computation there is no per-symbol memory overhead, so remove the verbose > 0 restriction and allow '/' search in the map browser unconditionally.

Fixes 259dce914e93 ("perf symbol: Remove symbol_name_rb_node")
---
 tools/perf/builtin-c2c.c     | 14 --------------
 tools/perf/builtin-report.c  | 14 --------------
 tools/perf/ui/browsers/map.c | 37 ++++++++++++++++++------------------
 3 files changed, 18 insertions(+), 47 deletions(-)
tools/perf/builtin-c2c.c +0 -14
index f51fa23f3bf1..719f47dac2dc 100644
@@ -3412,20 +3412,6 @@ static int perf_c2c__report(int argc, const char **argv)
 
 		if (ret < 0)
 			goto out_mem2node;
-		/*
-		 * For searching by name on the "Browse map details".
-		 * providing it only in verbose mode not to bloat too
-		 * much struct symbol.
-		 */
-		if (verbose > 0) {
-			/*
-			 * XXX: Need to provide a less kludgy way to ask for
-			 * more space per symbol, the u32 is for the index on
-			 * the ui browser.
-			 * See symbol__browser_index.
-			 */
-			symbol_conf.priv_size += sizeof(u32);
-		}
 		annotation_config__init();
 	}
 
tools/perf/builtin-report.c +0 -14
index 29085fdc91e5..6672a4d16529 100644
@@ -1885,20 +1885,6 @@ int cmd_report(int argc, const char **argv)
 		ret = symbol__annotation_init();
 		if (ret < 0)
 			goto error;
-		/*
- 		 * For searching by name on the "Browse map details".
- 		 * providing it only in verbose mode not to bloat too
- 		 * much struct symbol.
- 		 */
-		if (verbose > 0) {
-			/*
-			 * XXX: Need to provide a less kludgy way to ask for
-			 * more space per symbol, the u32 is for the index on
-			 * the ui browser.
-			 * See symbol__browser_index.
-			 */
-			symbol_conf.priv_size += sizeof(u32);
-		}
 		annotation_config__init();
 	}
 
tools/perf/ui/browsers/map.c +18 -19
index 075a575cdc5d..be7390156edd 100644
@@ -39,12 +39,6 @@ static void map_browser__write(struct ui_browser *browser, void *nd, int row)
 		ui_browser__write_nstring(browser, sym->name, width);
 }
 
-/* FIXME uber-kludgy, see comment on cmd_report... */
-static u32 *symbol__browser_index(struct symbol *browser)
-{
-	return ((void *)browser) - sizeof(struct rb_node) - sizeof(u32);
-}
-
 static int map_browser__search(struct map_browser *browser)
 {
 	char target[512];
@@ -62,10 +56,21 @@ static int map_browser__search(struct map_browser *browser)
 		sym = map__find_symbol_by_name(browser->map, target);
 
 	if (sym != NULL) {
-		u32 *idx = symbol__browser_index(sym);
-
-		browser->b.top = &sym->rb_node;
-		browser->b.index = browser->b.top_idx = *idx;
+		struct rb_node *nd;
+		u32 idx = 0;
+
+		/*
+		 * Walk the map browser's symbol entries to find the matching
+		 * symbol node and its display row index, then position the
+		 * browser cursor at that entry.
+		 */
+		for (nd = rb_first(browser->b.entries); nd; nd = rb_next(nd), ++idx) {
+			if (&sym->rb_node == nd) {
+				browser->b.top = nd;
+				browser->b.index = browser->b.top_idx = idx;
+				break;
+			}
+		}
 	} else
 		ui_helpline__fpush("%s not found!", target);
 
@@ -77,8 +82,7 @@ static int map_browser__run(struct map_browser *browser)
 	int key;
 
 	if (ui_browser__show(&browser->b, dso__long_name(map__dso(browser->map)),
-			     "Press ESC to exit, %s / to search",
-			     verbose > 0 ? "" : "restart with -v to use") < 0)
+			     "Press ESC to exit, / to search") < 0)
 		return -1;
 
 	while (1) {
@@ -86,9 +90,8 @@ static int map_browser__run(struct map_browser *browser)
 
 		switch (key) {
 		case '/':
-			if (verbose > 0)
-				map_browser__search(browser);
-			/* fall thru */
+			map_browser__search(browser);
+			continue;
 		default:
 			ui_browser__warn_unhandled_hotkey(&browser->b, key, 0, NULL);
 			continue;
@@ -124,10 +127,6 @@ int map__browse(struct map *map)
 
 		if (maxaddr < pos->end)
 			maxaddr = pos->end;
-		if (verbose > 0) {
-			u32 *idx = symbol__browser_index(pos);
-			*idx = mb.b.nr_entries;
-		}
 		++mb.b.nr_entries;
 	}
 
Signature · 1 line
2.56.1.703.gf848ed807c-goog
#101 lore raw

Re: [PATCH v4 04/23] perf map_browser: Fix browser index heap corruption by computing on demand

Sashiko has reviewed this patch and found no issues.
Quoted 13 lines

Commit 259dce914e93 ("perf symbol: Remove symbol_name_rb_node") removed the 24-byte symbol_name_rb_node reservation from symbol_conf.priv_size, but left symbol__browser_index() in ui/browsers/map.c subtracting sizeof(struct rb_node) + sizeof(u32) (28 bytes on 64-bit) ahead of struct symbol. In 'perf report -v' and 'perf c2c report -v', symbol_conf.priv_size is sizeof(struct annotation) + sizeof(u32) (20 bytes on 64-bit), so opening the TUI map browser writes a u32 index 8 bytes before each symbol's calloc() buffer, corrupting heap metadata.

Remove symbol__browser_index() in ui/browsers/map.c and compute the TUI map browser index on demand in map_browser__search() by walking the map browser's symbol entries when the user searches with '/'.

Quoted 8 lines

While this changes finding a symbol's display row index in map_browser__search() from O(1) to O(N) in the number of symbols in that [ ... ]
overhead, so remove the verbose > 0 restriction and allow '/' search in the map browser unconditionally.

Fixes 259dce914e93 ("perf symbol: Remove symbol_name_rb_node")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author reviewer
#102 lore raw

PATCH v4 5/23 perf machine: Protect machines->guests rbtree with a rw_semaphore

In reply to Ian Rogers

Unlike struct dsos, struct maps, and struct threads, struct machines had no lock protecting its machines->guests rbtree. Synchronizing access to machines->guests is needed both for concurrent guest machine lookups and so machines__shrink() can safely iterate guest machines using down_read_trylock(&machines->lock).

Add a rw_semaphore lock to struct machines to synchronize access to the machines->guests rbtree across machines__add(), machines__find(), machines__set_comm_exec(), machines__process_guests(), machines__set_id_hdr_size(), machines__fprintf_dsos(), machines__fprintf_dsos_buildid(), machines__destroy_kernel_maps(), and machines__for_each_thread(), and check for an existing guest machine PID under write lock in machines__add().

---
 tools/perf/util/machine.c | 42 +++++++++++++++++++++++++++++++++------
 tools/perf/util/machine.h |  1 +
 2 files changed, 37 insertions(+), 6 deletions(-)
tools/perf/util/machine.c +36 -6
index dc0aaac992f9..a300f3c1f5a4 100644
@@ -237,6 +237,7 @@ int machines__init(struct machines *machines, struct perf_session *session)
 {
 	int err = machine__init(&machines->host, "", HOST_KERNEL_ID);
 
+	init_rwsem(&machines->lock);
 	machines->session = session;
 	machines->host.machines = machines;
 	machines->guests = RB_ROOT_CACHED;
@@ -247,12 +248,13 @@ void machines__exit(struct machines *machines)
 {
 	machine__exit(&machines->host);
 	/* XXX exit guest */
+	exit_rwsem(&machines->lock);
 }
 
 struct machine *machines__add(struct machines *machines, pid_t pid,
 			      const char *root_dir)
 {
-	struct rb_node **p = &machines->guests.rb_root.rb_node;
+	struct rb_node **p;
 	struct rb_node *parent = NULL;
 	struct machine *pos, *machine = malloc(sizeof(*machine));
 	bool leftmost = true;
@@ -265,9 +267,17 @@ struct machine *machines__add(struct machines *machines, pid_t pid,
 		return NULL;
 	}
 
+	down_write(&machines->lock);
+	p = &machines->guests.rb_root.rb_node;
 	while (*p != NULL) {
 		parent = *p;
 		pos = rb_entry(parent, struct machine, rb_node);
+		if (pid == pos->pid) {
+			up_write(&machines->lock);
+			machine__exit(machine);
+			free(machine);
+			return pos;
+		}
 		if (pid < pos->pid)
 			p = &(*p)->rb_left;
 		else {
@@ -278,8 +288,8 @@ struct machine *machines__add(struct machines *machines, pid_t pid,
 
 	rb_link_node(&machine->rb_node, parent, p);
 	rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
-
 	machine->machines = machines;
+	up_write(&machines->lock);
 
 	return machine;
 }
@@ -290,16 +300,18 @@ void machines__set_comm_exec(struct machines *machines, bool comm_exec)
 
 	machines->host.comm_exec = comm_exec;
 
+	down_read(&machines->lock);
 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
 		struct machine *machine = rb_entry(nd, struct machine, rb_node);
 
 		machine->comm_exec = comm_exec;
 	}
+	up_read(&machines->lock);
 }
 
 struct machine *machines__find(struct machines *machines, pid_t pid)
 {
-	struct rb_node **p = &machines->guests.rb_root.rb_node;
+	struct rb_node **p;
 	struct rb_node *parent = NULL;
 	struct machine *machine;
 	struct machine *default_machine = NULL;
@@ -307,6 +319,8 @@ struct machine *machines__find(struct machines *machines, pid_t pid)
 	if (pid == HOST_KERNEL_ID)
 		return &machines->host;
 
+	down_read(&machines->lock);
+	p = &machines->guests.rb_root.rb_node;
 	while (*p != NULL) {
 		parent = *p;
 		machine = rb_entry(parent, struct machine, rb_node);
@@ -314,11 +328,14 @@ struct machine *machines__find(struct machines *machines, pid_t pid)
 			p = &(*p)->rb_left;
 		else if (pid > machine->pid)
 			p = &(*p)->rb_right;
-		else
+		else {
+			up_read(&machines->lock);
 			return machine;
+		}
 		if (!machine->pid)
 			default_machine = machine;
 	}
+	up_read(&machines->lock);
 
 	return default_machine;
 }
@@ -464,10 +481,12 @@ void machines__process_guests(struct machines *machines,
 {
 	struct rb_node *nd;
 
+	down_read(&machines->lock);
 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
 		struct machine *pos = rb_entry(nd, struct machine, rb_node);
 		process(pos, data);
 	}
+	up_read(&machines->lock);
 }
 
 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
@@ -477,11 +496,13 @@ void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
 
 	machines->host.id_hdr_size = id_hdr_size;
 
+	down_read(&machines->lock);
 	for (node = rb_first_cached(&machines->guests); node;
 	     node = rb_next(node)) {
 		machine = rb_entry(node, struct machine, rb_node);
 		machine->id_hdr_size = id_hdr_size;
 	}
+	up_read(&machines->lock);
 
 	return;
 }
@@ -934,10 +955,12 @@ size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
 	struct rb_node *nd;
 	size_t ret = dsos__fprintf(&machines->host.dsos, fp);
 
+	down_read(&machines->lock);
 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
 		struct machine *pos = rb_entry(nd, struct machine, rb_node);
 		ret += dsos__fprintf(&pos->dsos, fp);
 	}
+	up_read(&machines->lock);
 
 	return ret;
 }
@@ -954,10 +977,12 @@ size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
 	struct rb_node *nd;
 	size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
 
+	down_read(&machines->lock);
 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
 		struct machine *pos = rb_entry(nd, struct machine, rb_node);
 		ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
 	}
+	up_read(&machines->lock);
 	return ret;
 }
 
@@ -1296,10 +1321,12 @@ int machines__create_guest_kernel_maps(struct machines *machines)
 
 void machines__destroy_kernel_maps(struct machines *machines)
 {
-	struct rb_node *next = rb_first_cached(&machines->guests);
+	struct rb_node *next;
 
 	machine__destroy_kernel_maps(&machines->host);
 
+	down_write(&machines->lock);
+	next = rb_first_cached(&machines->guests);
 	while (next) {
 		struct machine *pos = rb_entry(next, struct machine, rb_node);
 
@@ -1307,6 +1334,7 @@ void machines__destroy_kernel_maps(struct machines *machines)
 		rb_erase_cached(&pos->rb_node, &machines->guests);
 		machine__delete(pos);
 	}
+	up_write(&machines->lock);
 }
 
 int machines__create_kernel_maps(struct machines *machines, pid_t pid)
@@ -3129,13 +3157,15 @@ int machines__for_each_thread(struct machines *machines,
 	if (rc != 0)
 		return rc;
 
+	down_read(&machines->lock);
 	for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
 		struct machine *machine = rb_entry(nd, struct machine, rb_node);
 
 		rc = machine__for_each_thread(machine, fn, priv);
 		if (rc != 0)
-			return rc;
+			break;
 	}
+	up_read(&machines->lock);
 	return rc;
 }
 
tools/perf/util/machine.h +1 -0
index 973a59f7c26a..147ce343e370 100644
@@ -151,6 +151,7 @@ typedef void (*machine__process_t)(struct machine *machine, void *data);
 struct machines {
 	/** @session: back link to owning session if there is one. */
 	struct perf_session *session;
+	struct rw_semaphore lock;
 	struct machine host;
 	struct rb_root_cached guests;
 };
Signature · 1 line
2.56.1.703.gf848ed807c-goog
#103 lore raw

Re: [PATCH v4 05/23] perf machine: Protect machines->guests rbtree with a rw_semaphore

Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

Unlike struct dsos, struct maps, and struct threads, struct machines had no lock protecting its machines->guests rbtree. Synchronizing access to machines->guests is needed both for concurrent guest machine lookups and so machines__shrink() can safely iterate guest machines using down_read_trylock(&machines->lock).

Add a rw_semaphore lock to struct machines to synchronize access to the machines->guests rbtree across machines__add(), machines__find(), machines__set_comm_exec(), machines__process_guests(), machines__set_id_hdr_size(), machines__fprintf_dsos(), machines__fprintf_dsos_buildid(), machines__destroy_kernel_maps(), and machines__for_each_thread(), and check for an existing guest machine PID under write lock in machines__add().

Quoted 1 line

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author reviewer
#104 lore raw

PATCH v4 6/23 perf vdso: Constify dso parameter in dso__is_vdso()

In reply to Ian Rogers

dso__can_shrink_symbols(const struct dso *dso) needs to query dso__is_vdso(dso) on a const struct dso pointer to exclude vDSO images from symbol shrinking.

Change the parameter of dso__is_vdso() from 'struct dso *' to 'const struct dso *'.

---
 tools/perf/util/vdso.c | 2 +-
 tools/perf/util/vdso.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
tools/perf/util/vdso.c +1 -1
index c12f5d8c4bf6..7f2dde70f3e4 100644
@@ -361,7 +361,7 @@ struct dso *machine__findnew_vdso(struct machine *machine,
 	return __machine__addnew_vdso(machine, DSO__NAME_VDSO, file);
 }
 
-bool dso__is_vdso(struct dso *dso)
+bool dso__is_vdso(const struct dso *dso)
 {
 	return !strcmp(dso__short_name(dso), DSO__NAME_VDSO) ||
 	       !strcmp(dso__short_name(dso), DSO__NAME_VDSO32) ||
tools/perf/util/vdso.h +1 -1
index bc74ace603ae..b6e05bc2d9f3 100644
@@ -19,7 +19,7 @@ static inline bool is_vdso_map(const char *filename)
 
 struct dso;
 
-bool dso__is_vdso(struct dso *dso);
+bool dso__is_vdso(const struct dso *dso);
 
 struct machine;
 struct thread;
Signature · 1 line
2.56.1.703.gf848ed807c-goog
#105 lore raw

Re: [PATCH v4 06/23] perf vdso: Constify dso parameter in dso__is_vdso()

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

dso__can_shrink_symbols(const struct dso *dso) needs to query dso__is_vdso(dso) on a const struct dso pointer to exclude vDSO images from symbol shrinking.

Change the parameter of dso__is_vdso() from 'struct dso *' to 'const struct dso *'.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
author reviewer
#106 lore raw

PATCH v4 7/23 perf rwsem: Add down_read_trylock() and down_write_trylock()

In reply to Ian Rogers

On-demand memory shrinking (perf_session__shrink(), dsos__shrink(), symbols__shrink()) can be triggered from OOM recovery inside allocation wrappers while the current thread already holds a rw_semaphore (such as symbols->lock, dsos->lock, or machines->lock). Non-blocking trylock primitives are needed so shrinking paths can safely skip any locked container without deadlocking.

Add non-blocking down_read_trylock() and down_write_trylock() wrappers around pthread_rwlock_tryrdlock() / pthread_rwlock_trywrlock() and pthread_mutex_trylock() along with SHARED_TRYLOCK_FUNCTION and EXCLUSIVE_TRYLOCK_FUNCTION thread-safety annotations.

---
 tools/perf/util/mutex.h |  3 +++
 tools/perf/util/rwsem.c | 20 ++++++++++++++++++++
 tools/perf/util/rwsem.h | 11 +++++++++++
 3 files changed, 34 insertions(+)
tools/perf/util/mutex.h +3 -0
index 5de1065ea2b9..de7dd6d8326c 100644
@@ -57,6 +57,8 @@
 /* Documents functions that try to acquire a lock, and return success or failure. */
 #define EXCLUSIVE_TRYLOCK_FUNCTION(...) \
 	__attribute__((exclusive_trylock_function(__VA_ARGS__)))
+#define SHARED_TRYLOCK_FUNCTION(...) \
+	__attribute__((shared_trylock_function(__VA_ARGS__)))
 
 /* Documents a function that expects a mutex to be held prior to entry. */
 #define EXCLUSIVE_LOCKS_REQUIRED(...) __attribute__((exclusive_locks_required(__VA_ARGS__)))
@@ -78,6 +80,7 @@
 #define SHARED_LOCK_FUNCTION(...)
 #define UNLOCK_FUNCTION(...)
 #define EXCLUSIVE_TRYLOCK_FUNCTION(...)
+#define SHARED_TRYLOCK_FUNCTION(...)
 #define EXCLUSIVE_LOCKS_REQUIRED(...)
 #define SHARED_LOCKS_REQUIRED(...)
 #define NO_THREAD_SAFETY_ANALYSIS
tools/perf/util/rwsem.c +20 -0
index 1d15c0e3732b..9548a3b44f16 100644
@@ -36,6 +36,16 @@ int down_read(struct rw_semaphore *sem)
 #endif
 }
 
+bool down_read_trylock(struct rw_semaphore *sem)
+	NO_THREAD_SAFETY_ANALYSIS
+{
+#if RWS_ERRORCHECK
+	return mutex_trylock(&sem->mtx);
+#else
+	return pthread_rwlock_tryrdlock(&sem->lock) == 0;
+#endif
+}
+
 int up_read(struct rw_semaphore *sem)
 	NO_THREAD_SAFETY_ANALYSIS
 {
@@ -58,6 +68,16 @@ int down_write(struct rw_semaphore *sem)
 #endif
 }
 
+bool down_write_trylock(struct rw_semaphore *sem)
+	NO_THREAD_SAFETY_ANALYSIS
+{
+#if RWS_ERRORCHECK
+	return mutex_trylock(&sem->mtx);
+#else
+	return pthread_rwlock_trywrlock(&sem->lock) == 0;
+#endif
+}
+
 int up_write(struct rw_semaphore *sem)
 	NO_THREAD_SAFETY_ANALYSIS
 {
tools/perf/util/rwsem.h +11 -0
index b102d8143181..53c45990d9c9 100644
@@ -22,9 +22,20 @@ int init_rwsem(struct rw_semaphore *sem);
 int exit_rwsem(struct rw_semaphore *sem);
 
 int down_read(struct rw_semaphore *sem) SHARED_LOCK_FUNCTION(sem);
+/*
+ * Tries to acquire a shared read lock without blocking. Per POSIX
+ * pthread_rwlock_tryrdlock(), succeeds when no writer holds or blocks on @sem
+ * (including when the calling thread already holds a read lock on @sem).
+ */
+bool down_read_trylock(struct rw_semaphore *sem) SHARED_TRYLOCK_FUNCTION(true, sem);
 int up_read(struct rw_semaphore *sem) UNLOCK_FUNCTION(sem);
 
 int down_write(struct rw_semaphore *sem) EXCLUSIVE_LOCK_FUNCTION(sem);
+/*
+ * Tries to acquire an exclusive write lock without blocking. Returns false if
+ * any thread (including the calling thread) holds a read or write lock on @sem.
+ */
+bool down_write_trylock(struct rw_semaphore *sem) EXCLUSIVE_TRYLOCK_FUNCTION(true, sem);
 int up_write(struct rw_semaphore *sem) UNLOCK_FUNCTION(sem);
 
 #endif /* _PERF_RWSEM_H */
Signature · 1 line
2.56.1.703.gf848ed807c-goog
#107 lore raw

Re: [PATCH v4 07/23] perf rwsem: Add down_read_trylock() and down_write_trylock()

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

On-demand memory shrinking (perf_session__shrink(), dsos__shrink(), symbols__shrink()) can be triggered from OOM recovery inside allocation wrappers while the current thread already holds a rw_semaphore (such as symbols->lock, dsos->lock, or machines->lock). Non-blocking trylock primitives are needed so shrinking paths can safely skip any locked container without deadlocking.

Quoted 6 lines

Add non-blocking down_read_trylock() and down_write_trylock() wrappers around pthread_rwlock_tryrdlock() / pthread_rwlock_trywrlock() and pthread_mutex_trylock() along with SHARED_TRYLOCK_FUNCTION and EXCLUSIVE_TRYLOCK_FUNCTION thread-safety annotations.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
author reviewer
#108 lore raw

PATCH v4 8/23 perf map_symbol: Add struct dso_symbol and use for parent symbols

In reply to Ian Rogers

A struct symbol's lifetime is owned by the struct dso containing its symbol table rather than by an individual struct map. When retaining a symbol pointer beyond the scope of a temporary addr_location or map reference without holding the map itself, the owning struct dso must be refcounted alongside the symbol pointer.

Introduce struct dso_symbol pairing a refcounted struct dso * with a struct symbol *, along with dso_symbol__copy(), dso_symbol__exit(), and dso_symbol__from_al() lifecycle helpers in map_symbol.[ch].

Convert parent symbol tracking across callchain resolution, hist_entry_iter, and hist_entry from bare struct symbol * pointers to struct dso_symbol so that parent symbols pin their owning DSOs until the iterator or histogram entry is destroyed. Also drop the unused perf_session__resolve_callchain() prototype from session.h.

---
 tools/perf/ui/stdio/hist.c   |  4 ++--
 tools/perf/util/callchain.c  |  3 ++-
 tools/perf/util/callchain.h  |  3 ++-
 tools/perf/util/hist.c       | 31 +++++++++++++++++++------------
 tools/perf/util/hist.h       |  8 ++++----
 tools/perf/util/machine.c    | 36 +++++++++++++++++++++++-------------
 tools/perf/util/machine.h    |  5 +++--
 tools/perf/util/map_symbol.c | 30 ++++++++++++++++++++++++++++++
 tools/perf/util/map_symbol.h | 11 +++++++++++
 tools/perf/util/session.h    |  6 ------
 tools/perf/util/sort.c       |  6 +++---
 11 files changed, 99 insertions(+), 44 deletions(-)
tools/perf/ui/stdio/hist.c +2 -2
index 8c4c8925df2c..3240c39bd025 100644
@@ -424,7 +424,7 @@ int __hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp,
 	int ret;
 	bool first = true;
 
-	if (symbol_conf.exclude_other && !he->parent)
+	if (symbol_conf.exclude_other && !he->parent.sym)
 		return 0;
 
 	perf_hpp_list__for_each_format(hpp_list, fmt) {
@@ -471,7 +471,7 @@ static int hist_entry__hierarchy_fprintf(struct hist_entry *he,
 	int ret, printed = 0;
 	bool first = true;
 
-	if (symbol_conf.exclude_other && !he->parent)
+	if (symbol_conf.exclude_other && !he->parent.sym)
 		return 0;
 
 	ret = scnprintf(hpp->buf, hpp->size, "%*s", he->depth * HIERARCHY_INDENT, "");
tools/perf/util/callchain.c +2 -1
index 6d5212be466d..4e4696cfa6d4 100644
@@ -1170,7 +1170,8 @@ int callchain_cursor_append(struct callchain_cursor *cursor,
 }
 
 int sample__resolve_callchain(struct perf_sample *sample,
-			      struct callchain_cursor *cursor, struct symbol **parent,
+			      struct callchain_cursor *cursor,
+			      struct dso_symbol *parent,
 			      struct addr_location *al,
 			      int max_stack)
 {
tools/perf/util/callchain.h +2 -1
index 8b1e405d1cdf..23f59b389799 100644
@@ -242,7 +242,8 @@ int record_opts__parse_callchain(struct record_opts *record,
 				 const char *arg, bool unset);
 
 int sample__resolve_callchain(struct perf_sample *sample,
-			      struct callchain_cursor *cursor, struct symbol **parent,
+			      struct callchain_cursor *cursor,
+			      struct dso_symbol *parent,
 			      struct addr_location *al,
 			      int max_stack);
 int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample);
tools/perf/util/hist.c +19 -12
index 364754e9d12a..d516ce469c7e 100644
@@ -119,8 +119,8 @@ void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
 		hists__new_col_len(hists, HISTC_DSO, len);
 	}
 
-	if (h->parent)
-		hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
+	if (h->parent.sym)
+		hists__new_col_len(hists, HISTC_PARENT, h->parent.sym->namelen);
 
 	if (h->branch_info) {
 		if (h->branch_info->from.ms.sym) {
@@ -543,6 +543,7 @@ static int hist_entry__init(struct hist_entry *he,
 
 	he->ms.thread = thread__get(he->ms.thread);
 	he->ms.map = map__get(he->ms.map);
+	dso_symbol__copy(&he->parent, &template->parent);
 
 	if (he->branch_info) {
 		/*
@@ -616,6 +617,7 @@ static int hist_entry__init(struct hist_entry *he,
 	if (he->mem_info)
 		mem_info__zput(he->mem_info);
 err:
+	dso_symbol__exit(&he->parent);
 	map_symbol__exit(&he->ms);
 	zfree(&he->stat_acc);
 	return -ENOMEM;
@@ -796,7 +798,7 @@ static void hists__res_sample(struct hist_entry *he, struct perf_sample *sample)
 static struct hist_entry*
 __hists__add_entry(struct hists *hists,
 		   struct addr_location *al,
-		   struct symbol *sym_parent,
+		   const struct dso_symbol *sym_parent,
 		   struct branch_info *bi,
 		   struct mem_info *mi,
 		   struct kvm_info *ki,
@@ -835,8 +837,9 @@ __hists__add_entry(struct hists *hists,
 			.weight3 = sample->weight3,
 			.latency = al->latency,
 		},
-		.parent = sym_parent,
-		.filtered = symbol__parent_filter(sym_parent) | al->filtered,
+		.parent = sym_parent ? *sym_parent : (struct dso_symbol){},
+		.filtered = symbol__parent_filter(sym_parent ? sym_parent->sym : NULL) |
+			    al->filtered,
 		.hists	= hists,
 		.branch_info = bi,
 		.mem_info = mi,
@@ -862,7 +865,7 @@ __hists__add_entry(struct hists *hists,
 
 struct hist_entry *hists__add_entry(struct hists *hists,
 				    struct addr_location *al,
-				    struct symbol *sym_parent,
+				    const struct dso_symbol *sym_parent,
 				    struct branch_info *bi,
 				    struct mem_info *mi,
 				    struct kvm_info *ki,
@@ -876,7 +879,7 @@ struct hist_entry *hists__add_entry(struct hists *hists,
 struct hist_entry *hists__add_entry_ops(struct hists *hists,
 					struct hist_entry_ops *ops,
 					struct addr_location *al,
-					struct symbol *sym_parent,
+					const struct dso_symbol *sym_parent,
 					struct branch_info *bi,
 					struct mem_info *mi,
 					struct kvm_info *ki,
@@ -957,7 +960,7 @@ iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al
 	 */
 	sample->period = cost;
 
-	he = hists__add_entry(hists, al, iter->parent, NULL, mi, NULL,
+	he = hists__add_entry(hists, al, &iter->parent, NULL, mi, NULL,
 			      sample, true);
 	if (!he)
 		return -ENOMEM;
@@ -1057,7 +1060,7 @@ iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *a
 	sample->period = 1;
 	sample->weight = bi[i].flags.cycles ? bi[i].flags.cycles : 1;
 
-	he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL, NULL,
+	he = hists__add_entry(hists, al, &iter->parent, &bi[i], NULL, NULL,
 			      sample, true);
 	if (he == NULL)
 		return -ENOMEM;
@@ -1109,7 +1112,7 @@ iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location
 	struct evsel *evsel = sample->evsel;
 	struct hist_entry *he;
 
-	he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
+	he = hists__add_entry(evsel__hists(evsel), al, &iter->parent, NULL, NULL,
 			      NULL, sample, true);
 	if (he == NULL)
 		return -ENOMEM;
@@ -1174,7 +1177,7 @@ iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
 	struct hist_entry *he;
 	int err = 0;
 
-	he = hists__add_entry(hists, al, iter->parent, NULL, NULL, NULL,
+	he = hists__add_entry(hists, al, &iter->parent, NULL, NULL, NULL,
 			      sample, true);
 	if (he == NULL)
 		return -ENOMEM;
@@ -1277,7 +1280,7 @@ iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
 		}
 	}
 
-	he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
+	he = hists__add_entry(evsel__hists(evsel), al, &iter->parent, NULL, NULL,
 			      NULL, sample, false);
 	if (he == NULL)
 		return -ENOMEM;
@@ -1340,12 +1343,14 @@ int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
 	int err, err2;
 	struct map *alm = NULL;
 
+	dso_symbol__exit(&iter->parent);
 	if (al)
 		alm = map__get(al->map);
 
 	err = sample__resolve_callchain(iter->sample, get_tls_callchain_cursor(), &iter->parent,
 					al, max_stack_depth);
 	if (err) {
+		dso_symbol__exit(&iter->parent);
 		map__put(alm);
 		return err;
 	}
@@ -1381,6 +1386,7 @@ int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
 	if (!err)
 		err = err2;
 
+	dso_symbol__exit(&iter->parent);
 	map__put(alm);
 
 	return err;
@@ -1468,6 +1474,7 @@ void hist_entry__delete(struct hist_entry *he)
 
 	thread__zput(he->thread);
 	map_symbol__exit(&he->ms);
+	dso_symbol__exit(&he->parent);
 
 	if (he->branch_info) {
 		branch_info__exit(he->branch_info);
tools/perf/util/hist.h +4 -4
index 0d08011aebce..4553365b3d8c 100644
@@ -159,7 +159,7 @@ struct hist_entry_iter {
 
 	struct perf_sample *sample;
 	struct hist_entry *he;
-	struct symbol *parent;
+	struct dso_symbol parent;
 
 	struct mem_info *mi;
 	struct branch_info *bi;
@@ -292,7 +292,7 @@ struct hist_entry {
 	};
 	char			*srcline;
 	char			*srcfile;
-	struct symbol		*parent;
+	struct dso_symbol	parent;
 	struct branch_info	*branch_info;
 	long			time;
 	struct hists		*hists;
@@ -350,7 +350,7 @@ static inline void hist_entry__add_pair(struct hist_entry *pair,
 
 struct hist_entry *hists__add_entry(struct hists *hists,
 				    struct addr_location *al,
-				    struct symbol *parent,
+				    const struct dso_symbol *parent,
 				    struct branch_info *bi,
 				    struct mem_info *mi,
 				    struct kvm_info *ki,
@@ -360,7 +360,7 @@ struct hist_entry *hists__add_entry(struct hists *hists,
 struct hist_entry *hists__add_entry_ops(struct hists *hists,
 					struct hist_entry_ops *ops,
 					struct addr_location *al,
-					struct symbol *sym_parent,
+					const struct dso_symbol *sym_parent,
 					struct branch_info *bi,
 					struct mem_info *mi,
 					struct kvm_info *ki,
tools/perf/util/machine.c +23 -13
index a300f3c1f5a4..5b4550e97172 100644
@@ -2236,7 +2236,7 @@ static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms
 
 static int add_callchain_ip(struct thread *thread,
 			    struct callchain_cursor *cursor,
-			    struct symbol **parent,
+			    struct dso_symbol *parent,
 			    struct addr_location *root_al,
 			    u8 *cpumode,
 			    u64 ip,
@@ -2279,6 +2279,8 @@ static int add_callchain_ip(struct thread *thread,
 				 * Discard all.
 				 */
 				callchain_cursor_reset(cursor);
+				if (parent)
+					dso_symbol__exit(parent);
 				err = 1;
 				goto out;
 			}
@@ -2291,10 +2293,12 @@ static int add_callchain_ip(struct thread *thread,
 	}
 
 	if (al.sym != NULL) {
-		if (perf_hpp_list.parent && !*parent &&
-		    symbol__match_regex(al.sym, &parent_regex))
-			*parent = al.sym;
-		else if (have_ignore_callees && root_al &&
+		if (perf_hpp_list.parent && parent && !parent->sym &&
+		    symbol__match_regex(al.sym, &parent_regex)) {
+			struct dso_symbol ds = dso_symbol__from_al(&al);
+
+			dso_symbol__copy(parent, &ds);
+		} else if (have_ignore_callees && root_al &&
 		  symbol__match_regex(al.sym, &ignore_callees_regex)) {
 			/* Treat this symbol as the root,
 			   forgetting its callees. */
@@ -2418,7 +2422,7 @@ static int remove_loops(struct branch_entry *l, int nr,
 static int lbr_callchain_add_kernel_ip(struct thread *thread,
 				       struct callchain_cursor *cursor,
 				       struct perf_sample *sample,
-				       struct symbol **parent,
+				       struct dso_symbol *parent,
 				       struct addr_location *root_al,
 				       u64 branch_from,
 				       bool callee, int end,
@@ -2484,7 +2488,7 @@ static void save_lbr_cursor_node(struct thread *thread,
 static int lbr_callchain_add_lbr_ip(struct thread *thread,
 				    struct callchain_cursor *cursor,
 				    struct perf_sample *sample,
-				    struct symbol **parent,
+				    struct dso_symbol *parent,
 				    struct addr_location *root_al,
 				    u64 *branch_from,
 				    bool callee,
@@ -2752,7 +2756,7 @@ static bool alloc_lbr_stitch(struct thread *thread, unsigned int max_lbr)
 static int resolve_lbr_callchain_sample(struct thread *thread,
 					struct callchain_cursor *cursor,
 					struct perf_sample *sample,
-					struct symbol **parent,
+					struct dso_symbol *parent,
 					struct addr_location *root_al,
 					int max_stack,
 					unsigned int max_lbr,
@@ -2839,7 +2843,7 @@ static int resolve_lbr_callchain_sample(struct thread *thread,
 
 static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
 			     struct callchain_cursor *cursor,
-			     struct symbol **parent,
+			     struct dso_symbol *parent,
 			     struct addr_location *root_al,
 			     u8 *cpumode, int ent, bool symbols)
 {
@@ -2870,7 +2874,7 @@ static u64 get_leaf_frame_caller(struct perf_sample *sample,
 static int thread__resolve_callchain_sample(struct thread *thread,
 					    struct callchain_cursor *cursor,
 					    struct perf_sample *sample,
-					    struct symbol **parent,
+					    struct dso_symbol *parent,
 					    struct addr_location *root_al,
 					    int max_stack,
 					    bool symbols)
@@ -3102,13 +3106,16 @@ static int thread__resolve_callchain_unwind(struct thread *thread,
 int __thread__resolve_callchain(struct thread *thread,
 				struct callchain_cursor *cursor,
 				struct perf_sample *sample,
-				struct symbol **parent,
+				struct dso_symbol *parent,
 				struct addr_location *root_al,
 				int max_stack,
 				bool symbols)
 {
 	int ret = 0;
 
+	if (parent)
+		dso_symbol__exit(parent);
+
 	if (cursor == NULL)
 		return -ENOMEM;
 
@@ -3120,7 +3127,7 @@ int __thread__resolve_callchain(struct thread *thread,
 						       parent, root_al,
 						       max_stack, symbols);
 		if (ret)
-			return ret;
+			goto out;
 		ret = thread__resolve_callchain_unwind(thread, cursor,
 						       sample,
 						       max_stack, symbols);
@@ -3129,13 +3136,16 @@ int __thread__resolve_callchain(struct thread *thread,
 						       sample,
 						       max_stack, symbols);
 		if (ret)
-			return ret;
+			goto out;
 		ret = thread__resolve_callchain_sample(thread, cursor,
 						       sample,
 						       parent, root_al,
 						       max_stack, symbols);
 	}
 
+out:
+	if (ret && parent)
+		dso_symbol__exit(parent);
 	return ret;
 }
 
tools/perf/util/machine.h +3 -2
index 147ce343e370..80191545ee8a 100644
@@ -193,11 +193,12 @@ struct mem_info *sample__resolve_mem(struct perf_sample *sample,
 				     struct addr_location *al);
 
 struct callchain_cursor;
+struct dso_symbol;
 
 int __thread__resolve_callchain(struct thread *thread,
 				struct callchain_cursor *cursor,
 				struct perf_sample *sample,
-				struct symbol **parent,
+				struct dso_symbol *parent,
 				struct addr_location *root_al,
 				int max_stack,
 				bool symbols);
@@ -205,7 +206,7 @@ int __thread__resolve_callchain(struct thread *thread,
 static inline int thread__resolve_callchain(struct thread *thread,
 					    struct callchain_cursor *cursor,
 					    struct perf_sample *sample,
-					    struct symbol **parent,
+					    struct dso_symbol *parent,
 					    struct addr_location *root_al,
 					    int max_stack)
 {
tools/perf/util/map_symbol.c +30 -0
index 11bc0a7f704c..595ec8f0541d 100644
@@ -1,9 +1,39 @@
 // SPDX-License-Identifier: GPL-2.0
 #include "map_symbol.h"
+#include "addr_location.h"
+#include "dso.h"
 #include "maps.h"
 #include "map.h"
 #include "thread.h"
 
+void dso_symbol__exit(struct dso_symbol *ds)
+{
+	dso__zput(ds->dso);
+	ds->sym = NULL;
+}
+
+/*
+ * Initialize @dst from @src, acquiring a reference on @src->dso (without
+ * releasing any prior @dst->dso, matching map_symbol__copy()).
+ */
+void dso_symbol__copy(struct dso_symbol *dst, const struct dso_symbol *src)
+{
+	dst->dso = src ? dso__get(src->dso) : NULL;
+	dst->sym = src ? src->sym : NULL;
+}
+
+/*
+ * Construct a borrowed (non-owning) dso_symbol view from @al while @al is
+ * live. Callers that persist the pair must retain it via dso_symbol__copy().
+ */
+struct dso_symbol dso_symbol__from_al(const struct addr_location *al)
+{
+	return (struct dso_symbol){
+		.dso = (al && al->sym && al->map) ? map__dso(al->map) : NULL,
+		.sym = al ? al->sym : NULL,
+	};
+}
+
 void map_symbol__exit(struct map_symbol *ms)
 {
 	thread__zput(ms->thread);
tools/perf/util/map_symbol.h +11 -0
index 7437e319f4a3..1781af7bd965 100644
@@ -4,11 +4,18 @@
 
 #include <linux/types.h>
 
+struct addr_location;
+struct dso;
 struct thread;
 struct maps;
 struct map;
 struct symbol;
 
+struct dso_symbol {
+	struct dso    *dso;
+	struct symbol *sym;
+};
+
 struct map_symbol {
 	struct thread *thread;
 	struct map    *map;
@@ -24,6 +31,10 @@ struct addr_map_symbol {
 	u64	      data_page_size;
 };
 
+void dso_symbol__exit(struct dso_symbol *ds);
+void dso_symbol__copy(struct dso_symbol *dst, const struct dso_symbol *src);
+struct dso_symbol dso_symbol__from_al(const struct addr_location *al);
+
 void map_symbol__exit(struct map_symbol *ms);
 void addr_map_symbol__exit(struct addr_map_symbol *ams);
 
tools/perf/util/session.h +0 -6
index ac5803d5fb4e..05ea2c1ee8c9 100644
@@ -139,12 +139,6 @@ int perf_session__process_events(struct perf_session *session);
 int perf_session__queue_event(struct perf_session *s, union perf_event *event,
 			      u64 timestamp, u64 file_offset, const char *file_path);
 
-int perf_session__resolve_callchain(struct perf_session *session,
-				    struct evsel *evsel,
-				    struct thread *thread,
-				    struct ip_callchain *chain,
-				    struct symbol **parent);
-
 bool perf_session__has_traces(struct perf_session *session, const char *msg);
 bool perf_session__has_switch_events(struct perf_session *session);
 
tools/perf/util/sort.c +3 -3
index 58638ec9ae0e..f4c50f191141 100644
@@ -1010,8 +1010,8 @@ static struct sort_entry sort_srcfile = {
 static int64_t
 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
 {
-	struct symbol *sym_l = left->parent;
-	struct symbol *sym_r = right->parent;
+	struct symbol *sym_l = left->parent.sym;
+	struct symbol *sym_r = right->parent.sym;
 
 	if (!sym_l || !sym_r)
 		return cmp_null(sym_l, sym_r);
@@ -1023,7 +1023,7 @@ static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
 				       size_t size, unsigned int width)
 {
 	return repsep_snprintf(bf, size, "%-*.*s", width, width,
-			      he->parent ? he->parent->name : "[other]");
+			      he->parent.sym ? he->parent.sym->name : "[other]");
 }
 
 struct sort_entry sort_parent = {
Signature · 1 line
2.56.1.703.gf848ed807c-goog