* [PATCH v9 01/11] perf symbol: Drop num_by_name and sorted_by_name from struct symbols
2026-11-24 3:28 [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking Ian Rogers
@ 2026-11-24 3:28 ` Ian Rogers
2026-11-24 3:28 ` [PATCH v9 02/11] perf symbol: Replace sorted boolean with sorted_cnt prefix length Ian Rogers
` (10 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2026-11-24 3:28 UTC (permalink / raw)
To: Namhyung Kim, Arnaldo Carvalho de Melo, Alireza Haghdoost,
Ian Rogers
Cc: Peter Zijlstra, Ingo Molnar, Jiri Olsa, Adrian Hunter,
James Clark, linux-perf-users, linux-kernel
Every mutation to symbols->symbols frees symbols->symbols_by_name, and
symbols__sort_by_name_locked() copies all symbols->cnt pointers verbatim
before sorting by name. As a result:
- symbols->num_by_name always equals symbols->cnt whenever
symbols->symbols_by_name is non-NULL (and 0 otherwise).
- symbols->sorted_by_name is equivalent to
(symbols->symbols_by_name != NULL || symbols->cnt == 0). Treating
cnt == 0 as trivially sorted by name also avoids an unnecessary
read-to-write lock upgrade on empty DSOs in
symbols__sort_by_name_read_lock() and dso__sort_by_name().
Remove num_by_name and sorted_by_name from struct symbols and clarify
the @symbols_by_name kernel-doc.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/dso.c | 2 +-
tools/perf/util/symbol.c | 44 ++++++++--------------------------------
tools/perf/util/symbol.h | 16 +++++----------
3 files changed, 14 insertions(+), 48 deletions(-)
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index 287f6812139c..ab0c87d0595e 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -1809,7 +1809,7 @@ bool dso__sorted_by_name(const struct dso *dso)
bool res;
down_read(&symbols->lock);
- res = symbols->sorted_by_name;
+ res = symbols->symbols_by_name != NULL || symbols->cnt == 0;
up_read(&symbols->lock);
return res;
}
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 35b08c05a2fd..03c4314935a5 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -221,9 +221,7 @@ void symbols__init(struct symbols *symbols)
symbols->symbols_by_name = NULL;
symbols->cnt = 0;
symbols->allocated = 0;
- symbols->num_by_name = 0;
symbols->sorted = true;
- symbols->sorted_by_name = false;
}
static void symbols__delete_locked(struct symbols *symbols)
@@ -236,9 +234,7 @@ static void symbols__delete_locked(struct symbols *symbols)
zfree(&symbols->symbols_by_name);
symbols->cnt = 0;
symbols->allocated = 0;
- symbols->num_by_name = 0;
symbols->sorted = true;
- symbols->sorted_by_name = false;
}
void symbols__delete(struct symbols *symbols)
@@ -510,8 +506,6 @@ void symbols__fixup_duplicate(struct symbols *symbols)
}
symbols->cnt = write_idx + 1;
zfree(&symbols->symbols_by_name);
- symbols->num_by_name = 0;
- symbols->sorted_by_name = false;
up_write(&symbols->lock);
}
@@ -691,8 +685,6 @@ static int __symbols__insert(struct symbols *symbols, struct symbol *sym)
symbols->symbols[symbols->cnt++] = sym;
zfree(&symbols->symbols_by_name);
- symbols->num_by_name = 0;
- symbols->sorted_by_name = false;
return 0;
}
@@ -795,17 +787,9 @@ static int symbols__sort_by_name_locked(struct symbols *symbols)
struct symbol **result;
unsigned int size = symbols->cnt;
- if (symbols->sorted_by_name)
+ if (symbols->symbols_by_name || size == 0)
return 0;
- zfree(&symbols->symbols_by_name);
- symbols->num_by_name = 0;
-
- if (size == 0) {
- symbols->sorted_by_name = true;
- return 0;
- }
-
result = malloc(sizeof(*result) * size);
if (!result)
return -ENOMEM;
@@ -813,8 +797,6 @@ static int symbols__sort_by_name_locked(struct symbols *symbols)
memcpy(result, symbols->symbols, sizeof(*result) * size);
qsort(result, size, sizeof(*result), symbols__sort_name_cmp);
symbols->symbols_by_name = result;
- symbols->num_by_name = size;
- symbols->sorted_by_name = true;
return 0;
}
@@ -824,7 +806,7 @@ static int symbols__sort_by_name_read_lock(struct symbols *symbols)
int err = 0;
down_read(&symbols->lock);
- while (!symbols->sorted_by_name) {
+ while (!symbols->symbols_by_name && symbols->cnt > 0) {
up_read(&symbols->lock);
down_write(&symbols->lock);
err = symbols__sort_by_name_locked(symbols);
@@ -833,7 +815,7 @@ static int symbols__sort_by_name_read_lock(struct symbols *symbols)
if (err)
break;
}
- return symbols->sorted_by_name ? 0 : err;
+ return (symbols->symbols_by_name || symbols->cnt == 0) ? 0 : err;
}
int symbol__match_symbol_name(const char *name, const char *str,
@@ -918,8 +900,6 @@ void dso__delete_symbol(struct dso *dso, struct symbol *sym)
(symbols->cnt - i - 1) * sizeof(struct symbol *));
symbols->cnt--;
zfree(&symbols->symbols_by_name);
- symbols->num_by_name = 0;
- symbols->sorted_by_name = false;
symbol__delete(sym);
break;
}
@@ -947,7 +927,7 @@ struct symbol *dso__next_symbol_by_name(struct dso *dso, size_t *idx)
symbols = dso__symbols(dso);
if (!symbols__sort_by_name_read_lock(symbols) &&
- *idx + 1 < symbols->num_by_name) {
+ *idx + 1 < symbols->cnt) {
++*idx;
res = symbols->symbols_by_name[*idx];
}
@@ -971,10 +951,10 @@ struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name, size_
symbols = dso__symbols(dso);
if (!symbols__sort_by_name_read_lock(symbols)) {
s = symbols__find_by_name(symbols->symbols_by_name,
- symbols->num_by_name,
+ symbols->cnt,
name, SYMBOL_TAG_INCLUDE__NONE, idx);
if (!s) {
- s = symbols__find_by_name(symbols->symbols_by_name, symbols->num_by_name,
+ s = symbols__find_by_name(symbols->symbols_by_name, symbols->cnt,
name, SYMBOL_TAG_INCLUDE__DEFAULT_ONLY, idx);
}
}
@@ -987,7 +967,7 @@ void dso__sort_by_name(struct dso *dso)
struct symbols *symbols = dso__symbols(dso);
down_read(&symbols->lock);
- if (!symbols->sorted_by_name) {
+ if (!symbols->symbols_by_name && symbols->cnt > 0) {
up_read(&symbols->lock);
down_write(&symbols->lock);
symbols__sort_by_name_locked(symbols);
@@ -1004,7 +984,7 @@ int symbols__visit_by_name(struct symbols *symbols,
int err = symbols__sort_by_name_read_lock(symbols);
if (!err) {
- for (size_t i = 0; i < symbols->num_by_name; i++) {
+ for (size_t i = 0; i < symbols->cnt; i++) {
err = cb(symbols->symbols_by_name[i], data);
if (err)
break;
@@ -1266,8 +1246,6 @@ static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso)
root->allocated = 0;
root->sorted = true;
zfree(&root->symbols_by_name);
- root->num_by_name = 0;
- root->sorted_by_name = false;
up_write(&root->lock);
for (unsigned int i = 0; i < old_cnt; i++) {
@@ -1340,8 +1318,6 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
root->allocated = 0;
root->sorted = true;
zfree(&root->symbols_by_name);
- root->num_by_name = 0;
- root->sorted_by_name = false;
up_write(&root->lock);
for (i = 0; i < old_cnt; i++) {
@@ -1495,8 +1471,6 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
__symbols__insert(root, old_symbols[j]);
}
zfree(&root->symbols_by_name);
- root->num_by_name = 0;
- root->sorted_by_name = false;
up_write(&root->lock);
free(old_symbols);
@@ -1525,8 +1499,6 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
root->sorted = false;
}
zfree(&root->symbols_by_name);
- root->num_by_name = 0;
- root->sorted_by_name = false;
up_write(&root->lock);
free(old_symbols);
return -1;
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 8b8ab7e1e148..629f6b2fcd0a 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -102,26 +102,20 @@ struct symbols {
/** @lock: Read/write semaphore synchronizing access to the arrays. */
struct rw_semaphore lock;
/**
- * @symbols: Dynamically allocated array of symbol pointers, sorted by
- * address range [start, end) when @sorted is true.
+ * @symbols: Dynamically allocated array of @allocated symbol pointers,
+ * sorted by start address when @sorted is true.
*/
struct symbol **symbols __guarded_by(&lock);
/**
- * @symbols_by_name: Optional dynamically allocated array of symbol
- * pointers sorted lexicographically by sym->name.
+ * @symbols_by_name: Optional lazily built array of @cnt symbol pointers
+ * sorted lexicographically by sym->name; NULL when unbuilt, when @cnt
+ * is 0, or when invalidated by a symbol-table mutation.
*/
struct symbol **symbols_by_name __guarded_by(&lock);
/** @cnt: Number of valid entries in @symbols. */
unsigned int cnt __guarded_by(&lock);
/** @allocated: Capacity (in entries) of the @symbols allocation. */
unsigned int allocated __guarded_by(&lock);
- /** @num_by_name: Number of valid entries in @symbols_by_name. */
- unsigned int num_by_name __guarded_by(&lock);
- /**
- * @sorted_by_name: True when @symbols_by_name is populated and sorted
- * by name.
- */
- bool sorted_by_name __guarded_by(&lock);
/** @sorted: True when @symbols is sorted by address range. */
bool sorted __guarded_by(&lock);
};
base-commit: 964e8b1c9c795982886a6778cb0cd73c901ca4ca
--
2.56.1.809.ga0b61035b5-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v9 02/11] perf symbol: Replace sorted boolean with sorted_cnt prefix length
2026-11-24 3:28 [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking Ian Rogers
2026-11-24 3:28 ` [PATCH v9 01/11] perf symbol: Drop num_by_name and sorted_by_name from struct symbols Ian Rogers
@ 2026-11-24 3:28 ` Ian Rogers
2026-11-24 3:28 ` [PATCH v9 03/11] perf symbol: Add secondary sorted run for dso__insert_symbol() Ian Rogers
` (9 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2026-11-24 3:28 UTC (permalink / raw)
To: Namhyung Kim, Arnaldo Carvalho de Melo, Alireza Haghdoost,
Ian Rogers
Cc: Peter Zijlstra, Ingo Molnar, Jiri Olsa, Adrian Hunter,
James Clark, linux-perf-users, linux-kernel
When symbols are appended after an initial sort, re-running full-array
merge sort across the entire symbol table rescans the already-sorted
prefix and allocates a scratch buffer even when only a few symbols were
appended.
Replace the @sorted boolean in struct symbols with @sorted_cnt, the
length of the leading start-address-sorted prefix (so @sorted_cnt ==
@cnt indicates the entire array is sorted):
- Pass first_unsorted to symbols__insertion_sort_range() so
already-sorted prefixes are not rescanned.
- In symbols__sort_locked(), binary-insert in place without allocating
when <= 8 symbols have been appended (or cnt <= 64), and otherwise
sort only the newly appended tail [sorted_cnt .. cnt) before merging
with [0 .. sorted_cnt).
Using 'perf bench internals symbols':
Before:
Average append + lookup: 1551.533 usec (+- 3.379 usec)
After (-90.3%):
Average append + lookup: 150.333 usec (+- 0.273 usec)
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/symbol.c | 75 +++++++++++++++++++++++++++++-----------
tools/perf/util/symbol.h | 21 +++++++----
2 files changed, 68 insertions(+), 28 deletions(-)
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 03c4314935a5..b9ddebdcd900 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -220,8 +220,8 @@ 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->sorted = true;
}
static void symbols__delete_locked(struct symbols *symbols)
@@ -233,8 +233,8 @@ 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->sorted = true;
}
void symbols__delete(struct symbols *symbols)
@@ -279,9 +279,10 @@ static unsigned int symbols__upper_bound(struct symbol **arr, unsigned int len,
static void symbols__insertion_sort_range(struct symbol **arr,
unsigned int start,
+ unsigned int first_unsorted,
unsigned int end)
{
- for (unsigned int i = start + 1; i < end; i++) {
+ for (unsigned int i = max(first_unsorted, start + 1); i < end; i++) {
struct symbol *sym = arr[i];
if (!symbol__less_or_equal(arr[i - 1], sym)) {
@@ -355,7 +356,7 @@ static void symbols__sort_range_with_tmp(struct symbol **arr, unsigned int n,
unsigned int i = 0;
if (n <= 32) {
- symbols__insertion_sort_range(arr, 0, n);
+ symbols__insertion_sort_range(arr, 0, 1, n);
return;
}
@@ -379,8 +380,10 @@ static void symbols__sort_range_with_tmp(struct symbol **arr, unsigned int n,
}
if (i - start < 32 && i < n) {
+ unsigned int run_end = i;
+
i = min(start + 32U, n);
- symbols__insertion_sort_range(arr, start, i);
+ symbols__insertion_sort_range(arr, start, run_end, i);
}
runs[nr_runs++] = (struct symbol_run){
@@ -431,22 +434,48 @@ static void symbols__sort_range_with_tmp(struct symbol **arr, unsigned int n,
static void symbols__sort_locked(struct symbols *symbols)
__must_hold(&symbols->lock)
{
- if (symbols->sorted)
+ unsigned int m = symbols->sorted_cnt;
+ unsigned int k;
+
+ if (m >= symbols->cnt) {
+ symbols->sorted_cnt = symbols->cnt;
return;
+ }
+ k = symbols->cnt - m;
- if (symbols->cnt <= 64) {
- symbols__insertion_sort_range(symbols->symbols, 0, symbols->cnt);
- } else {
+ /*
+ * 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, m, symbols->cnt);
+ symbols->sorted_cnt = symbols->cnt;
+ return;
+ }
+
+ 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);
+ symbols__insertion_sort_range(symbols->symbols, 0, m, 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, m, symbols->cnt);
}
}
- symbols->sorted = true;
+
+ symbols->sorted_cnt = symbols->cnt;
}
/*
@@ -456,7 +485,7 @@ static void symbols__sort_locked(struct symbols *symbols)
void symbols__sort_read_lock(struct symbols *symbols)
{
down_read(&symbols->lock);
- while (!symbols->sorted) {
+ while (symbols->sorted_cnt < symbols->cnt) {
up_read(&symbols->lock);
down_write(&symbols->lock);
symbols__sort_locked(symbols);
@@ -505,6 +534,7 @@ void symbols__fixup_duplicate(struct symbols *symbols)
}
}
symbols->cnt = write_idx + 1;
+ symbols->sorted_cnt = symbols->cnt;
zfree(&symbols->symbols_by_name);
up_write(&symbols->lock);
}
@@ -679,9 +709,10 @@ 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))
- symbols->sorted = false;
+ if (symbols->sorted_cnt == symbols->cnt &&
+ (symbols->cnt == 0 ||
+ symbol__less_or_equal(symbols->symbols[symbols->cnt - 1], sym)))
+ symbols->sorted_cnt = symbols->cnt + 1;
symbols->symbols[symbols->cnt++] = sym;
zfree(&symbols->symbols_by_name);
@@ -894,11 +925,13 @@ 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--;
zfree(&symbols->symbols_by_name);
symbol__delete(sym);
break;
@@ -1243,8 +1276,8 @@ 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);
up_write(&root->lock);
@@ -1315,8 +1348,8 @@ 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);
up_write(&root->lock);
@@ -1463,8 +1496,8 @@ 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;
} else {
for (unsigned int j = 0; j < write_idx; j++)
@@ -1490,13 +1523,13 @@ 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 : min(write_idx, 1U);
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 = false;
+ root->sorted_cnt = min(root->cnt, 1U);
}
zfree(&root->symbols_by_name);
up_write(&root->lock);
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 629f6b2fcd0a..2b20bfb22fd1 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -97,14 +97,18 @@ struct symbol {
/**
* struct symbols - A locked collection of symbols stored in a sorted array.
+ *
+ * The @symbols array of @cnt pointers (within an allocation of @allocated
+ * slots) keeps a start-address-sorted prefix @symbols[0 .. @sorted_cnt)
+ * followed by an optional unsorted tail @symbols[@sorted_cnt .. @cnt) from
+ * bulk symbols__insert() appends. When @sorted_cnt == @cnt, the entire array
+ * is sorted; otherwise the unsorted tail is lazily sorted and merged into the
+ * prefix on lookup or iteration.
*/
struct symbols {
/** @lock: Read/write semaphore synchronizing access to the arrays. */
struct rw_semaphore lock;
- /**
- * @symbols: Dynamically allocated array of @allocated symbol pointers,
- * sorted by start address when @sorted is true.
- */
+ /** @symbols: Dynamically allocated array of @allocated symbol pointers. */
struct symbol **symbols __guarded_by(&lock);
/**
* @symbols_by_name: Optional lazily built array of @cnt symbol pointers
@@ -112,12 +116,15 @@ struct symbols {
* is 0, or when invalidated by a symbol-table mutation.
*/
struct symbol **symbols_by_name __guarded_by(&lock);
- /** @cnt: Number of valid entries in @symbols. */
+ /** @cnt: Total number of valid symbol pointers in @symbols. */
unsigned int cnt __guarded_by(&lock);
+ /**
+ * @sorted_cnt: Length of the leading start-address-sorted prefix in
+ * @symbols. The entire @symbols array is sorted iff @sorted_cnt == @cnt.
+ */
+ unsigned int sorted_cnt __guarded_by(&lock);
/** @allocated: Capacity (in entries) of the @symbols allocation. */
unsigned int allocated __guarded_by(&lock);
- /** @sorted: True when @symbols is sorted by address range. */
- bool sorted __guarded_by(&lock);
};
void symbol__delete(struct symbol *sym);
--
2.56.1.809.ga0b61035b5-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v9 03/11] perf symbol: Add secondary sorted run for dso__insert_symbol()
2026-11-24 3:28 [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking Ian Rogers
2026-11-24 3:28 ` [PATCH v9 01/11] perf symbol: Drop num_by_name and sorted_by_name from struct symbols Ian Rogers
2026-11-24 3:28 ` [PATCH v9 02/11] perf symbol: Replace sorted boolean with sorted_cnt prefix length Ian Rogers
@ 2026-11-24 3:28 ` Ian Rogers
2026-11-24 3:28 ` [PATCH v9 04/11] perf test symbols: Add sorted insertion and equal-start alias tests Ian Rogers
` (8 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2026-11-24 3:28 UTC (permalink / raw)
To: Namhyung Kim, Arnaldo Carvalho de Melo, Alireza Haghdoost,
Ian Rogers
Cc: Peter Zijlstra, Ingo Molnar, Jiri Olsa, Adrian Hunter,
James Clark, linux-perf-users, linux-kernel
Dynamic symbol resolution via dso__insert_symbol() can insert symbols
out of order (with sym->start < symbols[sorted_cnt - 1]->start) into an
already-sorted DSO symbol table. Binary-inserting directly into the
primary sorted run memmoves O(N) pointers on every insertion.
Add @sorted_tail_cnt to struct symbols tracking an optional secondary
start-address-sorted run at
symbols[sorted_cnt .. sorted_cnt + sorted_tail_cnt) (keeping
sizeof(struct symbols) unchanged at 88 bytes on 64-bit) and expand the
struct symbols kernel-doc to document the three array regions:
- Append ascending symbols in O(1) (advancing @sorted_cnt) and
binary-insert in place for small tables (cnt < 64).
- For larger out-of-order dynamic insertions when no unsorted bulk
entries are pending, insert into the secondary sorted run with an
amortized sqrt(N) merge threshold (sorted_tail_cnt >= 256 and
sorted_tail_cnt^2 >= sorted_cnt).
- Search both sorted runs under a read lock in symbols__find() while
preserving single-array highest start <= ip lookup semantics, and
merge into a single sorted run in symbols__sort_read_lock().
Using 'perf bench internals symbols':
Default (10,000 symbols, 200 appends):
Before:
Average append + lookup: 150.333 usec (+- 0.273 usec)
After (-71.4%):
Average append + lookup: 43.000 usec (+- 0.311 usec)
Large DSO (-n 100000 -a 500):
Before:
Average append + lookup: 3315.200 usec (+- 12.532 usec)
After (-91.1%):
Average append + lookup: 294.000 usec (+- 11.574 usec)
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/symbol.c | 151 ++++++++++++++++++++++++++++++++++++---
tools/perf/util/symbol.h | 38 +++++++---
2 files changed, 172 insertions(+), 17 deletions(-)
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index b9ddebdcd900..f3a3889f1062 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -221,6 +221,7 @@ void symbols__init(struct symbols *symbols)
symbols->symbols_by_name = NULL;
symbols->cnt = 0;
symbols->sorted_cnt = 0;
+ symbols->sorted_tail_cnt = 0;
symbols->allocated = 0;
}
@@ -234,6 +235,7 @@ static void symbols__delete_locked(struct symbols *symbols)
zfree(&symbols->symbols_by_name);
symbols->cnt = 0;
symbols->sorted_cnt = 0;
+ symbols->sorted_tail_cnt = 0;
symbols->allocated = 0;
}
@@ -435,12 +437,16 @@ static void symbols__sort_locked(struct symbols *symbols)
__must_hold(&symbols->lock)
{
unsigned int m = symbols->sorted_cnt;
+ unsigned int t = symbols->sorted_tail_cnt;
unsigned int k;
if (m >= symbols->cnt) {
symbols->sorted_cnt = symbols->cnt;
+ symbols->sorted_tail_cnt = 0;
return;
}
+ if (t > symbols->cnt - m)
+ t = 0;
k = symbols->cnt - m;
/*
@@ -451,10 +457,24 @@ static void symbols__sort_locked(struct symbols *symbols)
if (k <= 8 || symbols->cnt <= 64) {
symbols__insertion_sort_range(symbols->symbols, 0, m, symbols->cnt);
symbols->sorted_cnt = symbols->cnt;
+ symbols->sorted_tail_cnt = 0;
return;
}
- if (m <= 1) {
+ if (m + t == symbols->cnt) {
+ /*
+ * Both [0 .. m) and the secondary run [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, m, symbols->cnt);
+ }
+ } else if (m <= 1) {
struct symbol **tmp = malloc(((symbols->cnt + 1) / 2) * sizeof(struct symbol *));
if (tmp) {
@@ -476,11 +496,12 @@ static void symbols__sort_locked(struct symbols *symbols)
}
symbols->sorted_cnt = symbols->cnt;
+ symbols->sorted_tail_cnt = 0;
}
/*
* 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)
{
@@ -711,14 +732,81 @@ static int __symbols__insert(struct symbols *symbols, struct symbol *sym)
if (symbols->sorted_cnt == symbols->cnt &&
(symbols->cnt == 0 ||
- symbol__less_or_equal(symbols->symbols[symbols->cnt - 1], sym)))
+ symbol__less_or_equal(symbols->symbols[symbols->cnt - 1], sym))) {
symbols->sorted_cnt = symbols->cnt + 1;
+ } else if (symbols->sorted_tail_cnt > 0 &&
+ symbols->sorted_cnt + symbols->sorted_tail_cnt == symbols->cnt &&
+ symbol__less_or_equal(symbols->symbols[symbols->cnt - 1], sym)) {
+ symbols->sorted_tail_cnt++;
+ }
symbols->symbols[symbols->cnt++] = sym;
zfree(&symbols->symbols_by_name);
return 0;
}
+static int __symbols__insert_sorted(struct symbols *symbols, struct symbol *sym)
+ __must_hold(&symbols->lock)
+{
+ unsigned int idx;
+
+ if (symbols->sorted_cnt + symbols->sorted_tail_cnt < symbols->cnt)
+ 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_tail_cnt == 0) {
+ 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 secondary sorted run at symbols[sorted_cnt]
+ * so out-of-order insertions do not memmove the entire
+ * primary sorted run on every call.
+ */
+ symbols->symbols[symbols->cnt++] = sym;
+ symbols->sorted_tail_cnt = 1;
+ }
+ } else {
+ idx = symbols->sorted_cnt +
+ symbols__upper_bound(&symbols->symbols[symbols->sorted_cnt],
+ symbols->sorted_tail_cnt, sym);
+ memmove(&symbols->symbols[idx + 1], &symbols->symbols[idx],
+ (symbols->cnt - idx) * sizeof(struct symbol *));
+ symbols->symbols[idx] = sym;
+ symbols->cnt++;
+ symbols->sorted_tail_cnt++;
+
+ if (symbols->sorted_tail_cnt >= 256 &&
+ (size_t)symbols->sorted_tail_cnt * symbols->sorted_tail_cnt >=
+ symbols->sorted_cnt)
+ symbols__sort_locked(symbols);
+ }
+
+ zfree(&symbols->symbols_by_name);
+ return 0;
+}
+
int symbols__insert(struct symbols *symbols, struct symbol *sym)
{
int ret;
@@ -737,7 +825,9 @@ static inline bool symbol__covers(const struct symbol *s, u64 ip)
return ip >= start && (ip < end || (ip == end && ip == start));
}
-static struct symbol *symbols__find_range(struct symbol **arr, unsigned int cnt, u64 ip)
+static struct symbol *symbols__find_range(struct symbol **arr, unsigned int cnt,
+ u64 ip, bool *has_candidate,
+ u64 *candidate_start)
{
unsigned int low = 0, high = cnt, idx;
u64 start;
@@ -750,8 +840,10 @@ static struct symbol *symbols__find_range(struct symbol **arr, unsigned int cnt,
else
high = mid;
}
- if (low == 0)
+ if (low == 0) {
+ *has_candidate = false;
return NULL;
+ }
/*
* Rewind across any equal-start group so a trailing zero-length or
@@ -760,6 +852,8 @@ static struct symbol *symbols__find_range(struct symbol **arr, unsigned int cnt,
*/
idx = low - 1;
start = arr[idx]->start;
+ *has_candidate = true;
+ *candidate_start = start;
while (idx > 0 && arr[idx - 1]->start == start)
idx--;
for (; idx < low; idx++) {
@@ -771,17 +865,43 @@ static struct symbol *symbols__find_range(struct symbol **arr, unsigned int cnt,
/*
* Binary search @symbols->symbols for a symbol covering @ip. Lazily sorts
- * @symbols under @symbols->lock if unsorted.
+ * @symbols under @symbols->lock if unsorted entries are present.
*/
static struct symbol *symbols__find(struct symbols *symbols, u64 ip)
{
struct symbol *res;
+ bool has_prefix;
+ u64 prefix_start = 0;
if (symbols == NULL)
return NULL;
- symbols__sort_read_lock(symbols);
- res = symbols__find_range(symbols->symbols, symbols->cnt, ip);
+ down_read(&symbols->lock);
+ while (symbols->sorted_cnt + symbols->sorted_tail_cnt < symbols->cnt) {
+ up_read(&symbols->lock);
+ down_write(&symbols->lock);
+ symbols__sort_locked(symbols);
+ up_write(&symbols->lock);
+ down_read(&symbols->lock);
+ }
+
+ res = symbols__find_range(symbols->symbols, symbols->sorted_cnt, ip,
+ &has_prefix, &prefix_start);
+ if (symbols->sorted_tail_cnt > 0) {
+ struct symbol *tail_res;
+ bool has_tail;
+ u64 tail_start = 0;
+
+ tail_res = symbols__find_range(&symbols->symbols[symbols->sorted_cnt],
+ symbols->sorted_tail_cnt, ip,
+ &has_tail, &tail_start);
+ if (has_tail) {
+ if (!has_prefix || tail_start > prefix_start)
+ res = tail_res;
+ else if (tail_start == prefix_start && !res)
+ res = tail_res;
+ }
+ }
up_read(&symbols->lock);
return res;
@@ -917,7 +1037,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)
@@ -932,6 +1058,8 @@ void dso__delete_symbol(struct dso *dso, struct symbol *sym)
symbols->cnt--;
if (i < symbols->sorted_cnt)
symbols->sorted_cnt--;
+ else if (i < symbols->sorted_cnt + symbols->sorted_tail_cnt)
+ symbols->sorted_tail_cnt--;
zfree(&symbols->symbols_by_name);
symbol__delete(sym);
break;
@@ -1277,6 +1405,7 @@ static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso)
root->symbols = NULL;
root->cnt = 0;
root->sorted_cnt = 0;
+ root->sorted_tail_cnt = 0;
root->allocated = 0;
zfree(&root->symbols_by_name);
up_write(&root->lock);
@@ -1349,6 +1478,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
root->symbols = NULL;
root->cnt = 0;
root->sorted_cnt = 0;
+ root->sorted_tail_cnt = 0;
root->allocated = 0;
zfree(&root->symbols_by_name);
up_write(&root->lock);
@@ -1497,6 +1627,7 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
root->symbols = old_symbols;
root->cnt = write_idx;
root->sorted_cnt = write_idx;
+ root->sorted_tail_cnt = 0;
root->allocated = old_allocated;
old_symbols = NULL;
} else {
@@ -1524,12 +1655,14 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
root->symbols = old_symbols;
root->cnt = write_idx;
root->sorted_cnt = (delta == 0) ? write_idx : min(write_idx, 1U);
+ root->sorted_tail_cnt = 0;
root->allocated = old_allocated;
old_symbols = NULL;
} else {
for (unsigned int j = 0; j < write_idx; j++)
__symbols__insert(root, old_symbols[j]);
root->sorted_cnt = min(root->cnt, 1U);
+ root->sorted_tail_cnt = 0;
}
zfree(&root->symbols_by_name);
up_write(&root->lock);
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 2b20bfb22fd1..1aa54c1f1680 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -99,11 +99,31 @@ struct symbol {
* struct symbols - A locked collection of symbols stored in a sorted array.
*
* The @symbols array of @cnt pointers (within an allocation of @allocated
- * slots) keeps a start-address-sorted prefix @symbols[0 .. @sorted_cnt)
- * followed by an optional unsorted tail @symbols[@sorted_cnt .. @cnt) from
- * bulk symbols__insert() appends. When @sorted_cnt == @cnt, the entire array
- * is sorted; otherwise the unsorted tail is lazily sorted and merged into the
- * prefix on lookup or iteration.
+ * slots) is partitioned into up to three contiguous regions:
+ *
+ * [0 .. @sorted_cnt)
+ * Primary run, sorted by ascending symbol start address.
+ * [@sorted_cnt .. @sorted_cnt + @sorted_tail_cnt)
+ * Optional secondary sorted run holding out-of-order dynamic insertions
+ * from dso__insert_symbol() whose start addresses precede
+ * @symbols[@sorted_cnt - 1]->start (so the two runs overlap in address
+ * range rather than forming one ascending sequence). Keeping @sorted_cnt
+ * at the boundary between the two runs avoids memmoving the primary run
+ * on every insertion while allowing O(log N) binary search across both
+ * runs under a read lock; once the secondary run reaches
+ * O(sqrt(@sorted_cnt)) entries (or a single contiguous run is required),
+ * the two runs are merged and @sorted_cnt advances to @cnt.
+ * [@sorted_cnt + @sorted_tail_cnt .. @cnt)
+ * Optional unsorted tail from bulk symbols__insert() appends, lazily
+ * sorted and merged into the primary run on lookup or iteration.
+ *
+ * Invariants:
+ * - @sorted_cnt == @cnt: all entries form a single sorted run
+ * (@sorted_tail_cnt is 0).
+ * - @sorted_cnt + @sorted_tail_cnt == @cnt: all entries reside in the two
+ * sorted runs and can be binary-searched without sorting.
+ * - @sorted_cnt + @sorted_tail_cnt < @cnt: unsorted entries are present and
+ * must be sorted under @lock for writing before searching.
*/
struct symbols {
/** @lock: Read/write semaphore synchronizing access to the arrays. */
@@ -118,11 +138,13 @@ struct symbols {
struct symbol **symbols_by_name __guarded_by(&lock);
/** @cnt: Total number of valid symbol pointers in @symbols. */
unsigned int cnt __guarded_by(&lock);
+ /** @sorted_cnt: Length of the primary start-address-sorted prefix. */
+ unsigned int sorted_cnt __guarded_by(&lock);
/**
- * @sorted_cnt: Length of the leading start-address-sorted prefix in
- * @symbols. The entire @symbols array is sorted iff @sorted_cnt == @cnt.
+ * @sorted_tail_cnt: Length of the secondary start-address-sorted run
+ * immediately following @sorted_cnt (0 when absent).
*/
- unsigned int sorted_cnt __guarded_by(&lock);
+ unsigned int sorted_tail_cnt __guarded_by(&lock);
/** @allocated: Capacity (in entries) of the @symbols allocation. */
unsigned int allocated __guarded_by(&lock);
};
--
2.56.1.809.ga0b61035b5-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v9 04/11] perf test symbols: Add sorted insertion and equal-start alias tests
2026-11-24 3:28 [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking Ian Rogers
` (2 preceding siblings ...)
2026-11-24 3:28 ` [PATCH v9 03/11] perf symbol: Add secondary sorted run for dso__insert_symbol() Ian Rogers
@ 2026-11-24 3:28 ` Ian Rogers
2026-11-24 3:39 ` sashiko-bot
2026-11-24 3:28 ` [PATCH v9 05/11] perf dso: Synchronize dso_cache access and make dso->loaded atomic Ian Rogers
` (7 subsequent siblings)
11 siblings, 1 reply; 14+ messages in thread
From: Ian Rogers @ 2026-11-24 3:28 UTC (permalink / raw)
To: Namhyung Kim, Arnaldo Carvalho de Melo, Alireza Haghdoost,
Ian Rogers
Cc: Peter Zijlstra, Ingo Molnar, Jiri Olsa, Adrian Hunter,
James Clark, linux-perf-users, linux-kernel
Add unit tests to tools/perf/tests/symbols.c covering the struct symbols
sorted-array implementation:
- Ordered bulk insertion via symbols__insert(), out-of-order dynamic
insertion via dso__insert_symbol() into the secondary sorted run,
unsorted append followed by sort-on-lookup, and unmerged-vs-merged
two-run overlap and equal-start alias lookups.
- Equal-start symbol aliases: rewinding past a trailing zero-length alias
during lookup, preserving non-zero size in symbols__fixup_end(), alias
deduplication in symbols__fixup_duplicate(), and scanning equal-start
groups in map__fixup_end().
- Same-address STB_WEAK / STB_GLOBAL alias resolution in
test__kallsyms_fixup_end().
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/tests/symbols.c | 268 +++++++++++++++++++++++++++++++++++--
1 file changed, 257 insertions(+), 11 deletions(-)
diff --git a/tools/perf/tests/symbols.c b/tools/perf/tests/symbols.c
index b73da581f895..4e4f8eabec5b 100644
--- a/tools/perf/tests/symbols.c
+++ b/tools/perf/tests/symbols.c
@@ -219,12 +219,248 @@ static int test_file(struct test_info *ti, char *filename)
return ret;
}
+static int test_symbols_sorted_insert(void)
+ __context_unsafe(/* single-threaded white-box test */)
+{
+ 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 they land in the secondary sorted run.
+ */
+ 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_cnt != 100 || symbols->cnt != 100) {
+ pr_debug("Expected sorted_cnt=100, 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_cnt + symbols->sorted_tail_cnt != symbols->cnt) {
+ pr_debug("dso__insert_symbol did not preserve sorted runs!\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_cnt + symbols->sorted_tail_cnt >= symbols->cnt ||
+ symbols->cnt != 200) {
+ pr_debug("Unexpected sorted_cnt=%u sorted_tail_cnt=%u cnt=%u before lookup\n",
+ symbols->sorted_cnt, symbols->sorted_tail_cnt, 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 || sym->start != (u64)i * 0x100) {
+ pr_debug("Lookup failed at idx %u (addr=%#" PRIx64 ")\n", i, addr);
+ goto out;
+ }
+ }
+
+ /*
+ * Insert dynamic symbols into the unmerged secondary sorted run and
+ * verify lookups match merged-array semantics in all prefix/tail
+ * overlap and equal-start cases:
+ * - [0x0, 0x40) in prefix + [0x20, 0x30) in tail: 0x24 -> inner,
+ * 0x34 -> NULL (closer tail start shadows outer prefix symbol).
+ * - [0x100, 0x140) in prefix + [0x80, 0x180) in tail: 0x150 -> NULL
+ * (closer prefix start shadows outer tail symbol).
+ * - [0x200, 0x240) in prefix + [0x200, 0x260) in tail: 0x250 -> alias
+ * (equal-start group spans prefix and tail).
+ */
+ {
+ struct symbol *inner = symbol__new(0x20, 0x10, STB_GLOBAL, STT_FUNC,
+ "inner_tail_sym");
+ struct symbol *outer = symbol__new(0x80, 0x100, STB_GLOBAL, STT_FUNC,
+ "outer_tail_sym");
+ struct symbol *alias = symbol__new(0x200, 0x60, STB_GLOBAL, STT_FUNC,
+ "longer_tail_alias");
+
+ if (!inner || dso__insert_symbol(dso, inner)) {
+ symbol__delete(outer);
+ symbol__delete(alias);
+ goto out;
+ }
+ if (!outer || dso__insert_symbol(dso, outer)) {
+ symbol__delete(alias);
+ goto out;
+ }
+ if (!alias || dso__insert_symbol(dso, alias))
+ goto out;
+ if (symbols->sorted_tail_cnt == 0 || symbols->sorted_cnt >= symbols->cnt) {
+ pr_debug("Expected unmerged secondary sorted run!\n");
+ goto out;
+ }
+ if (dso__find_symbol(dso, 0x24) != inner ||
+ dso__find_symbol(dso, 0x34) != NULL ||
+ dso__find_symbol(dso, 0x150) != NULL ||
+ dso__find_symbol(dso, 0x250) != alias) {
+ pr_debug("Unmerged tail lookup mismatch!\n");
+ goto out;
+ }
+ symbols__sort_read_lock(symbols);
+ up_read(&symbols->lock);
+ if (dso__find_symbol(dso, 0x24) != inner ||
+ dso__find_symbol(dso, 0x34) != NULL ||
+ dso__find_symbol(dso, 0x150) != NULL ||
+ dso__find_symbol(dso, 0x250) != alias) {
+ pr_debug("Merged lookup mismatch!\n");
+ goto out;
+ }
+ }
+
+ ret = TEST_OK;
+out:
+ dso__put(dso);
+ return ret;
+}
+
+static int test_symbols_equal_start_aliases(void)
+{
+ struct dso *dso = dso__new("/tmp/perf_test_equal_start_aliases");
+ struct map *map = NULL;
+ struct symbols *symbols;
+ struct symbol *sized_sym, *zero_alias, *last_sized, *last_short, *found;
+ int ret = TEST_FAIL;
+
+ if (!dso)
+ return TEST_FAIL;
+
+ map = map__new2(0, dso);
+ if (!map)
+ goto out;
+
+ symbols = dso__symbols(dso);
+ sized_sym = symbol__new(0x1000, 0x40, STB_GLOBAL, STT_FUNC, "sized_sym");
+ zero_alias = symbol__new(0x1000, 0, STB_GLOBAL, STT_FUNC, "zero_alias");
+ last_sized = symbol__new(0x2000, 0x80, STB_GLOBAL, STT_FUNC, "last_sized");
+ if (!sized_sym || !zero_alias || !last_sized) {
+ symbol__delete(sized_sym);
+ symbol__delete(zero_alias);
+ symbol__delete(last_sized);
+ goto out;
+ }
+
+ if (symbols__insert(symbols, sized_sym)) {
+ symbol__delete(zero_alias);
+ symbol__delete(last_sized);
+ goto out;
+ }
+ if (symbols__insert(symbols, zero_alias)) {
+ symbol__delete(last_sized);
+ goto out;
+ }
+ if (symbols__insert(symbols, last_sized))
+ goto out;
+
+ /*
+ * With 3 entries [0x1000..0x1040, 0x1000..0x1000, 0x2000..0x2080], a
+ * lookup at 0x1020 must rewind past the trailing zero-length alias at
+ * 0x1000 and find sized_sym.
+ */
+ found = dso__find_symbol(dso, 0x1020);
+ if (found != sized_sym) {
+ pr_debug("Expected sized_sym at 0x1020, got %s\n",
+ found ? found->name : "NULL");
+ goto out;
+ }
+
+ /*
+ * symbols__fixup_end() must not inflate zero_alias to 0x2000 when
+ * sized_sym already provides a non-zero size at 0x1000.
+ */
+ symbols__fixup_end(symbols, /*is_kallsyms=*/false);
+ if (zero_alias->end != 0x1000) {
+ pr_debug("zero_alias inflated to %#" PRIx64 "\n", zero_alias->end);
+ goto out;
+ }
+ symbols__fixup_duplicate(symbols);
+ found = dso__find_symbol(dso, 0x1020);
+ if (found != sized_sym || found->end != 0x1040) {
+ pr_debug("After dedup expected sized_sym [0x1000, 0x1040), got %s\n",
+ found ? found->name : "NULL");
+ goto out;
+ }
+
+ /*
+ * Append a shorter alias at the final start address 0x2000 (as when
+ * symbol_conf.allow_aliases is set) and verify map__fixup_end() uses
+ * the maximum end across the equal-start group.
+ */
+ last_short = symbol__new(0x2000, 0x10, STB_WEAK, STT_FUNC, "last_short");
+ if (!last_short || symbols__insert(symbols, last_short))
+ goto out;
+
+ map__fixup_end(map);
+ if (map__end(map) != 0x2080) {
+ pr_debug("Expected map end 0x2080, got %#" PRIx64 "\n", map__end(map));
+ goto out;
+ }
+
+ ret = TEST_OK;
+out:
+ map__put(map);
+ 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_equal_start_aliases();
+ if (ret != TEST_OK)
+ return ret;
+
ret = init_test_info(&ti);
if (ret != TEST_OK)
return ret;
@@ -240,24 +476,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 +527,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 +537,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))
--
2.56.1.809.ga0b61035b5-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread* Re: [PATCH v9 04/11] perf test symbols: Add sorted insertion and equal-start alias tests
2026-11-24 3:28 ` [PATCH v9 04/11] perf test symbols: Add sorted insertion and equal-start alias tests Ian Rogers
@ 2026-11-24 3:39 ` sashiko-bot
0 siblings, 0 replies; 14+ messages in thread
From: sashiko-bot @ 2026-11-24 3:39 UTC (permalink / raw)
To: Ian Rogers; +Cc: linux-perf-users
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] perf test symbols: STB_WEAK alias test in
test__kallsyms_fixup_end() bypassed
--
commit 728aaf364c23458b2251cf89f0396157b988f69d
Author: Ian Rogers <irogers@google.com>
perf test symbols: Add sorted insertion and equal-start alias tests
This commit adds unit tests for sorted symbol insertion, equal-start
symbol aliases, and STB_WEAK / STB_GLOBAL alias resolution in kallsyms.
> diff --git a/tools/perf/tests/symbols.c b/tools/perf/tests/symbols.c
> index b73da581f8956..4e4f8eabec5b8 100644
> --- a/tools/perf/tests/symbols.c
> +++ b/tools/perf/tests/symbols.c
[ ... ]
> @@ -240,24 +476,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 +527,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 +537,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;
[Severity: Medium]
Does test__kallsyms_fixup_end() actually exercise the STB_WEAK vs
STB_GLOBAL check in choose_best_symbol()?
Because symbols__fixup_end() skips equal-start entries with
curr->start == prev->start, only the last symbol at 0xffffffff81000500
(global_target_symbol) has its end updated to 0xffffffff81000ff0, while
weak_alias_symbol remains zero-sized. When symbols__fixup_duplicate()
then calls choose_best_symbol(), it picks global_target_symbol via the
earlier non-zero length check without reaching the binding comparison:
tools/perf/util/symbol.c:choose_best_symbol() {
...
/* Prefer a symbol with non zero length */
a = syma->end - syma->start;
b = symb->end - symb->start;
if ((b == 0) && (a > 0))
return SYMBOL_A;
else if ((a == 0) && (b > 0))
return SYMBOL_B;
...
}
As a result, the test passes solely because global_target_symbol is
listed second in kallsyms_syms[]; if global_target_symbol precedes
weak_alias_symbol at the same address, symbols__fixup_end() inflates only
weak_alias_symbol and symbols__fixup_duplicate() drops the global symbol.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124032902.3859564-1-irogers@google.com?part=4
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v9 05/11] perf dso: Synchronize dso_cache access and make dso->loaded atomic
2026-11-24 3:28 [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking Ian Rogers
` (3 preceding siblings ...)
2026-11-24 3:28 ` [PATCH v9 04/11] perf test symbols: Add sorted insertion and equal-start alias tests Ian Rogers
@ 2026-11-24 3:28 ` Ian Rogers
2026-11-24 3:28 ` [PATCH v9 06/11] perf symbol: Add symbols__shrink() and symbols__try_delete_unannotated() Ian Rogers
` (6 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2026-11-24 3:28 UTC (permalink / raw)
To: Namhyung Kim, Arnaldo Carvalho de Melo, Alireza Haghdoost,
Ian Rogers
Cc: Peter Zijlstra, Ingo Molnar, Jiri Olsa, Adrian Hunter,
James Clark, linux-perf-users, linux-kernel
Prepare struct dso for concurrent cache and symbol shrinking:
- Split __dso_cache__free() out of dso_cache__free() with
__must_hold(dso__lock(dso)) so rb_first() is read under dso__lock(dso),
and hold dso__lock(dso) across __dso_cache__find() and
dso_cache__memcpy() in dso_cache_io(), re-looking up after
dso_cache__populate() reacquires the lock.
- Make dso->loaded an _Atomic bool accessed with acquire/release ordering,
and avoid redundant dso__set_loaded() writes when dso__load() finds the
DSO already loaded under dso__lock(dso).
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/dso.c | 48 +++++++++++++++++++++++++---------------
tools/perf/util/dso.h | 5 +++--
tools/perf/util/symbol.c | 5 ++++-
3 files changed, 37 insertions(+), 21 deletions(-)
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index ab0c87d0595e..83611d7ccaff 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -3,6 +3,7 @@
#include <assert.h>
#include <errno.h>
+#include <stdatomic.h>
#include <stdlib.h>
#include <asm/bug.h>
@@ -1053,12 +1054,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)
+ __must_hold(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;
@@ -1067,10 +1068,18 @@ 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));
}
static struct dso_cache *__dso_cache__find(struct dso *dso, u64 offset)
+ __must_hold(dso__lock(dso))
{
const struct rb_root *root = &dso__data(dso)->cache;
struct rb_node * const *p = &root->rb_node;
@@ -1221,27 +1230,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;
}
/*
@@ -1800,7 +1812,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)
@@ -1838,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;
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index 63c1b7d6f6f8..70a121fd7ecd 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -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>
@@ -343,7 +344,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[];
};
@@ -599,7 +600,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)
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index f3a3889f1062..f1abe3333ac0 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include <dirent.h>
#include <errno.h>
+#include <stdatomic.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -2362,8 +2363,9 @@ int dso__load(struct dso *dso, struct map *map)
/* check again under the dso->lock */
if (dso__loaded(dso)) {
ret = 1;
- goto out;
+ goto out_unlock;
}
+ atomic_store_explicit(&RC_CHK_ACCESS(dso)->loaded, false, memory_order_release);
kmod = dso__is_kmod(dso);
@@ -2521,6 +2523,7 @@ int dso__load(struct dso *dso, struct map *map)
ret = 0;
out:
dso__set_loaded(dso);
+out_unlock:
mutex_unlock(dso__lock(dso));
nsinfo__mountns_exit(&nsc);
--
2.56.1.809.ga0b61035b5-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v9 06/11] perf symbol: Add symbols__shrink() and symbols__try_delete_unannotated()
2026-11-24 3:28 [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking Ian Rogers
` (4 preceding siblings ...)
2026-11-24 3:28 ` [PATCH v9 05/11] perf dso: Synchronize dso_cache access and make dso->loaded atomic Ian Rogers
@ 2026-11-24 3:28 ` Ian Rogers
2026-11-24 3:28 ` [PATCH v9 07/11] perf dso: Add dso__shrink() and dso__try_evict() Ian Rogers
` (5 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2026-11-24 3:28 UTC (permalink / raw)
To: Namhyung Kim, Arnaldo Carvalho de Melo, Alireza Haghdoost,
Ian Rogers
Cc: Peter Zijlstra, Ingo Molnar, Jiri Olsa, Adrian Hunter,
James Clark, linux-perf-users, linux-kernel
Add symbol-container memory shrinking helpers to struct symbols:
- symbols__shrink(): Reclaims the secondary name-sorted index
(symbols_by_name, rebuilt on demand by dso__sort_by_name()) and trims
unused over-allocated tail capacity in symbols->symbols while keeping
all live struct symbol entries intact.
- symbols__try_delete_unannotated(): Deletes all symbols under
symbols->lock when none have entries in the annotations hashmap, running
an optional pre_delete callback before freeing the symbols.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/symbol.c | 61 ++++++++++++++++++++++++++++++++++++++++
tools/perf/util/symbol.h | 3 ++
2 files changed, 64 insertions(+)
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index f1abe3333ac0..1bcc36e011fc 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -253,6 +253,67 @@ void symbols__exit(struct symbols *symbols)
exit_rwsem(&symbols->lock);
}
+bool symbols__try_delete_unannotated(struct symbols *symbols,
+ void (*pre_delete)(void *ctx), void *ctx)
+{
+ down_write(&symbols->lock);
+ for (unsigned int i = 0; i < symbols->cnt; i++) {
+ if (symbol__annotated(symbols->symbols[i])) {
+ up_write(&symbols->lock);
+ return false;
+ }
+ }
+
+ if (pre_delete)
+ pre_delete(ctx);
+
+ symbols__delete_locked(symbols);
+ up_write(&symbols->lock);
+ return true;
+}
+
+/*
+ * Reclaim auxiliary memory from @symbols without freeing live symbol
+ * entries (whose lifetimes are owned by the containing struct dso):
+ * - Free the secondary name-sorted index (@symbols_by_name), which is
+ * rebuilt on demand by dso__sort_by_name().
+ * - Trim unused over-allocated tail capacity in @symbols->symbols.
+ *
+ * Returns the number of buffers reclaimed/shrunk.
+ */
+size_t symbols__shrink(struct symbols *symbols)
+{
+ size_t freed = 0;
+
+ down_write(&symbols->lock);
+
+ if (symbols->symbols_by_name) {
+ zfree(&symbols->symbols_by_name);
+ freed++;
+ }
+
+ if (symbols->cnt == 0) {
+ if (symbols->allocated > 0) {
+ zfree(&symbols->symbols);
+ symbols->sorted_cnt = 0;
+ symbols->sorted_tail_cnt = 0;
+ symbols->allocated = 0;
+ freed++;
+ }
+ } else if (symbols->cnt < symbols->allocated) {
+ struct symbol **shrunk = realloc(symbols->symbols,
+ symbols->cnt * sizeof(struct symbol *));
+ if (shrunk) {
+ symbols->symbols = shrunk;
+ symbols->allocated = symbols->cnt;
+ freed++;
+ }
+ }
+
+ up_write(&symbols->lock);
+ return freed;
+}
+
/*
* Order symbols by start address only, preserving insertion order among
* symbols sharing the same start address (matching the previous rbtree
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 1aa54c1f1680..dd9be561618d 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -153,6 +153,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) __acquires_shared(&symbols->lock);
static inline u8 symbol__type(const struct symbol *sym)
--
2.56.1.809.ga0b61035b5-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v9 07/11] perf dso: Add dso__shrink() and dso__try_evict()
2026-11-24 3:28 [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking Ian Rogers
` (5 preceding siblings ...)
2026-11-24 3:28 ` [PATCH v9 06/11] perf symbol: Add symbols__shrink() and symbols__try_delete_unannotated() Ian Rogers
@ 2026-11-24 3:28 ` Ian Rogers
2026-11-24 3:28 ` [PATCH v9 08/11] perf session: Add dsos, machine, and perf_session shrinking Ian Rogers
` (4 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2026-11-24 3:28 UTC (permalink / raw)
To: Namhyung Kim, Arnaldo Carvalho de Melo, Alireza Haghdoost,
Ian Rogers
Cc: Peter Zijlstra, Ingo Molnar, Jiri Olsa, Adrian Hunter,
James Clark, linux-perf-users, linux-kernel
Add per-DSO memory shrinking and unreferenced DSO eviction helpers:
- dso__shrink(): Without blocking on contended locks, closes cached DSO
data file descriptors, frees cached 4KB data pages, addr2line handles,
and libdw DWARF contexts. When a reloadable user DSO in a dsos
collection has no external references (refcnt == 1) and no annotated
symbols or inlines, evicts its symbol table via
symbols__try_delete_unannotated() and marks dso->loaded false so it
reloads on demand; otherwise calls symbols__shrink().
- dso__try_evict(): Evicts an unreferenced user DSO (refcnt == 1, no
symbols, not kernel/vDSO/hit/header_build_id, and not on
dso__data_open) without blocking on locks.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/dso.c | 215 +++++++++++++++++++++++++++++++++++++++++-
tools/perf/util/dso.h | 9 ++
2 files changed, 222 insertions(+), 2 deletions(-)
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index 83611d7ccaff..b6217c49dd14 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -17,6 +17,7 @@
#include <sys/types.h>
#include <unistd.h>
+#include "annotate.h"
#include "annotate-data.h"
#include "auxtrace.h"
#include "compress.h"
@@ -1874,7 +1875,7 @@ struct dso *dso__new(const char *name)
return dso__new_id(name, NULL);
}
-void dso__delete(struct dso *dso)
+static void __dso__delete(struct dso *dso, bool close_data)
{
if (dso__dsos(dso))
pr_err("DSO %s is still in rbtree when being deleted!\n", dso__long_name(dso));
@@ -1896,7 +1897,8 @@ void dso__delete(struct dso *dso)
RC_CHK_ACCESS(dso)->long_name_allocated = false;
}
- dso__data_close(dso);
+ if (close_data)
+ dso__data_close(dso);
auxtrace_cache__free(RC_CHK_ACCESS(dso)->auxtrace_cache);
dso_cache__free(dso);
dso__free_a2l(dso);
@@ -1908,6 +1910,215 @@ void dso__delete(struct dso *dso)
RC_CHK_FREE(dso);
}
+void dso__delete(struct dso *dso)
+{
+ __dso__delete(dso, /*close_data=*/true);
+}
+
+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 bool dso__has_annotated_inlines(struct dso *dso)
+{
+ struct rb_root_cached *tree = dso__inlined_nodes(dso);
+ struct rb_node *nd;
+
+ for (nd = rb_first_cached(tree); nd; nd = rb_next(nd)) {
+ struct inline_node *node = rb_entry(nd, struct inline_node, rb_node);
+ struct inline_list *ilist;
+
+ list_for_each_entry(ilist, &node->val, list) {
+ if (ilist->symbol && symbol__annotated(ilist->symbol))
+ return true;
+ }
+ }
+ return false;
+}
+
+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)
+{
+ 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) &&
+ !dso__has_annotated_inlines(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;
+}
+
+/*
+ * Try to evict an unreferenced @dso from its owning struct dsos (whose write
+ * lock must be held by the caller) without blocking on any lock.
+ */
+bool dso__try_evict(struct dso *dso)
+{
+ bool can_evict = false;
+
+ if (!dso ||
+ dso__has_symbols(dso) ||
+ dso__header_build_id(dso) ||
+ dso__hit(dso) ||
+ dso__kernel(dso) ||
+ dso__is_vdso(dso) ||
+ refcount_read(dso__refcnt(dso)) != 1)
+ return false;
+
+ if (!mutex_trylock(&dso__data_open_lock))
+ return false;
+
+ if (dso__data(dso)->fd < 0 &&
+ list_empty(&dso__data(dso)->open_entry) &&
+ refcount_read(dso__refcnt(dso)) == 1 &&
+ mutex_trylock(dso__lock(dso))) {
+ can_evict = !dso__has_symbols(dso) && !dso__has_annotated_inlines(dso);
+ mutex_unlock(dso__lock(dso));
+ }
+ mutex_unlock(&dso__data_open_lock);
+ if (!can_evict)
+ return false;
+
+ dso__set_dsos(dso, NULL);
+ if (refcount_dec_and_test(&RC_CHK_ACCESS(dso)->refcnt))
+ __dso__delete(dso, /*close_data=*/false);
+ else
+ RC_CHK_PUT(dso);
+ return true;
+}
+
struct dso *dso__get(struct dso *dso)
{
struct dso *result;
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index 70a121fd7ecd..b0ee45a53f40 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -789,6 +789,15 @@ static inline bool dso__has_symbols(const struct dso *dso)
return res;
}
+static inline refcount_t *dso__refcnt(struct dso *dso)
+{
+ return &RC_CHK_ACCESS(dso)->refcnt;
+}
+
+bool dso__can_shrink_symbols(const struct dso *dso);
+bool dso__try_evict(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);
--
2.56.1.809.ga0b61035b5-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v9 08/11] perf session: Add dsos, machine, and perf_session shrinking
2026-11-24 3:28 [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking Ian Rogers
` (6 preceding siblings ...)
2026-11-24 3:28 ` [PATCH v9 07/11] perf dso: Add dso__shrink() and dso__try_evict() Ian Rogers
@ 2026-11-24 3:28 ` Ian Rogers
2026-11-24 3:29 ` [PATCH v9 09/11] perf test symbols: Add symbol and DSO shrinking tests Ian Rogers
` (3 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2026-11-24 3:28 UTC (permalink / raw)
To: Namhyung Kim, Arnaldo Carvalho de Melo, Alireza Haghdoost,
Ian Rogers
Cc: Peter Zijlstra, Ingo Molnar, Jiri Olsa, Adrian Hunter,
James Clark, linux-perf-users, linux-kernel
Add container-level shrinking helpers walking DSOs across a session's host
and guest machines:
- dsos__shrink(): Calls dso__shrink() and dso__try_evict() across all DSOs
under dsos->lock, compacts the array when unreferenced DSOs are evicted,
and calls malloc_trim(0) on glibc when memory was reclaimed.
- machine__shrink(), machines__shrink(), and perf_session__shrink():
Shrink host and guest machine DSO collections.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/dsos.c | 50 +++++++++++++++++++++++++++++++++++++++
tools/perf/util/dsos.h | 2 ++
tools/perf/util/machine.c | 29 +++++++++++++++++++++++
tools/perf/util/machine.h | 3 +++
tools/perf/util/session.c | 8 +++++++
tools/perf/util/session.h | 2 ++
6 files changed, 94 insertions(+)
diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c
index 7a56577f4ec8..77f668d7557c 100644
--- a/tools/perf/util/dsos.c
+++ b/tools/perf/util/dsos.c
@@ -6,6 +6,9 @@
#include "vdso.h"
#include "namespaces.h"
#include <errno.h>
+#ifdef __GLIBC__
+#include <malloc.h>
+#endif
#include <stdlib.h>
#include <string.h>
#include <symbol.h> // filename__read_build_id
@@ -517,3 +520,50 @@ int dsos__for_each_dso(struct dsos *dsos, int (*cb)(struct dso *dso, void *data)
up_read(&dsos->lock);
return err;
}
+
+size_t dsos__shrink(struct dsos *dsos)
+{
+ unsigned int write_idx = 0;
+ size_t freed = 0;
+ bool any_removed = false;
+
+ down_write(&dsos->lock);
+
+ for (unsigned int read_idx = 0; read_idx < dsos->cnt; read_idx++) {
+ struct dso *dso = dsos->dsos[read_idx];
+
+ freed += dso__shrink(dso);
+
+ if (dso__try_evict(dso)) {
+ any_removed = true;
+ freed++;
+ continue;
+ }
+ dsos->dsos[write_idx++] = dso;
+ }
+
+ dsos->cnt = write_idx;
+ if (any_removed) {
+ if (write_idx == 0) {
+ zfree(&dsos->dsos);
+ dsos->allocated = 0;
+ dsos->sorted = true;
+ } else if (write_idx < dsos->allocated / 2) {
+ struct dso **shrunk = realloc(dsos->dsos,
+ write_idx * sizeof(struct dso *));
+ if (shrunk) {
+ dsos->dsos = shrunk;
+ dsos->allocated = write_idx;
+ }
+ }
+ }
+
+ up_write(&dsos->lock);
+
+#ifdef __GLIBC__
+ if (freed > 0)
+ malloc_trim(0);
+#endif
+
+ return freed;
+}
diff --git a/tools/perf/util/dsos.h b/tools/perf/util/dsos.h
index cc7e3a42f9cb..de10a10de777 100644
--- a/tools/perf/util/dsos.h
+++ b/tools/perf/util/dsos.h
@@ -51,4 +51,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 */
diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index 3c206c26bd62..f4108cc725fc 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -3438,3 +3438,32 @@ 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)
+{
+ struct rb_node *nd;
+ size_t freed;
+
+ if (!machines)
+ return 0;
+
+ freed = machine__shrink(&machines->host);
+
+ 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);
+
+ freed += machine__shrink(pos);
+ }
+ up_read(&machines->lock);
+
+ return freed;
+}
diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h
index 67d517742fb6..b42c363723c9 100644
--- a/tools/perf/util/machine.h
+++ b/tools/perf/util/machine.h
@@ -339,4 +339,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 */
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 4e11eabbbe6f..9f16bff1b72f 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -4270,3 +4270,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);
+}
diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
index 05ea2c1ee8c9..cbea6792a008 100644
--- a/tools/perf/util/session.h
+++ b/tools/perf/util/session.h
@@ -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 */
--
2.56.1.809.ga0b61035b5-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v9 09/11] perf test symbols: Add symbol and DSO shrinking tests
2026-11-24 3:28 [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking Ian Rogers
` (7 preceding siblings ...)
2026-11-24 3:28 ` [PATCH v9 08/11] perf session: Add dsos, machine, and perf_session shrinking Ian Rogers
@ 2026-11-24 3:29 ` Ian Rogers
2026-11-24 3:29 ` [PATCH v9 10/11] perf session: Add dso__session() and session-scoped allocation wrappers Ian Rogers
` (2 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2026-11-24 3:29 UTC (permalink / raw)
To: Namhyung Kim, Arnaldo Carvalho de Melo, Alireza Haghdoost,
Ian Rogers
Cc: Peter Zijlstra, Ingo Molnar, Jiri Olsa, Adrian Hunter,
James Clark, linux-perf-users, linux-kernel
Add unit tests to tools/perf/tests/symbols.c covering symbol and DSO
memory shrinking:
- test_symbols_shrink(): Verify symbols__shrink() reclaims symbols_by_name
and trims excess capacity while keeping live symbols intact, and verify
symbols__try_delete_unannotated() respects symbol annotations.
- test_dsos_shrink(): Verify dsos__shrink() evicts unreferenced user DSOs
while retaining referenced DSOs and vDSOs.
- test_dso_shrink_and_reload(): Verify dso__shrink() on a referenced ELF
DSO preserves symbol pointers and rebuilds symbols_by_name on demand,
dso_symbol__copy() pins DSO symbols across dsos__shrink() until
dso_symbol__exit(), inline/srcline trees are freed safely before
symbols, and evicted hit DSOs reload transparently on map__find_symbol().
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/tests/symbols.c | 361 +++++++++++++++++++++++++++++++++++++
1 file changed, 361 insertions(+)
diff --git a/tools/perf/tests/symbols.c b/tools/perf/tests/symbols.c
index 4e4f8eabec5b..1fd6bfdc4147 100644
--- a/tools/perf/tests/symbols.c
+++ b/tools/perf/tests/symbols.c
@@ -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 "annotate.h"
#include "debug.h"
#include "dso.h"
#include "env.h"
#include "machine.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,341 @@ 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)
+ __context_unsafe(/* single-threaded white-box test */)
+{
+ struct symbols symbols;
+ struct symbol *sym1, *sym2, *sym3;
+ 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__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;
+ }
+
+ /*
+ * 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;
+ }
+
+ /*
+ * Verify symbols__try_delete_unannotated() skips deletion when any
+ * symbol has an entry in the annotations hashmap and deletes all
+ * symbols once unannotated.
+ */
+ if (!symbol__annotation(sym1) || !symbol__annotated(sym1)) {
+ pr_debug("symbol__annotation(sym1) failed!\n");
+ goto out;
+ }
+ if (symbols__try_delete_unannotated(&symbols, NULL, NULL) || symbols.cnt != 3) {
+ pr_debug("symbols__try_delete_unannotated deleted annotated table!\n");
+ goto out;
+ }
+ symbol__annotation_delete(sym1);
+ if (symbol__annotated(sym1) ||
+ !symbols__try_delete_unannotated(&symbols, NULL, NULL) ||
+ symbols.cnt != 0) {
+ pr_debug("symbols__try_delete_unannotated failed after clearing annotation!\n");
+ goto out;
+ }
+
+ ret = TEST_OK;
+out:
+ symbols__exit(&symbols);
+ return ret;
+}
+
+static int test_dsos_shrink(void)
+ __context_unsafe(/* single-threaded white-box test */)
+{
+ 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;
+}
+
+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 = sym->start;
+ args->max_end = sym->end;
+ return 0;
+ }
+ if (!args->second_name && sym->start >= args->max_end) {
+ args->second_start = sym->start;
+ args->second_name = strdup(sym->name);
+ if (!args->second_name)
+ return -ENOMEM;
+ }
+ if (sym->end > args->max_end)
+ args->max_end = sym->end;
+ return 0;
+}
+
+static int test_dso_shrink_and_reload(struct test_info *ti, char *filename, struct map **map_p)
+ __context_unsafe(/* single-threaded white-box test */)
+{
+ 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->symbols_by_name || syms->cnt < 2) {
+ free(args.second_name);
+ return TEST_FAIL;
+ }
+ {
+ size_t idx = 0;
+ struct symbol *expected_next = syms->symbols_by_name[1];
+ u8 hdr_byte = 0;
+
+ /* Populate dso data fd and cache so dso__shrink() reclaims them too. */
+ (void)dso__data_read_offset(dso, ti->machine, 0, &hdr_byte, 1);
+
+ if (dso__shrink(dso) == 0 || syms->cnt != initial_cnt ||
+ syms->symbols_by_name != NULL || dso__data(dso)->fd >= 0) {
+ 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(reloaded->name, 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_cnt != syms->cnt || !dso__loaded(dso)) {
+ pr_debug("Reloaded symbol table state mismatch (cnt=%u vs %u, sorted_cnt=%u, loaded=%d)!\n",
+ syms->cnt, initial_cnt, syms->sorted_cnt, dso__loaded(dso));
+ free(args.second_name);
+ return TEST_FAIL;
+ }
+ for (unsigned int i = 1; i < syms->cnt; i++) {
+ if (syms->symbols[i - 1]->start > syms->symbols[i]->start) {
+ 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,6 +559,13 @@ 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);
@@ -461,6 +814,14 @@ static int test__symbols(struct test_suite *test __maybe_unused, int subtest __m
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 = init_test_info(&ti);
if (ret != TEST_OK)
return ret;
--
2.56.1.809.ga0b61035b5-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v9 10/11] perf session: Add dso__session() and session-scoped allocation wrappers
2026-11-24 3:28 [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking Ian Rogers
` (8 preceding siblings ...)
2026-11-24 3:29 ` [PATCH v9 09/11] perf test symbols: Add symbol and DSO shrinking tests Ian Rogers
@ 2026-11-24 3:29 ` Ian Rogers
2026-11-24 3:29 ` [PATCH v9 11/11] perf test symbols: Add session allocation wrapper and lock contention tests Ian Rogers
2026-11-24 3:49 ` [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking sashiko-bot
11 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2026-11-24 3:29 UTC (permalink / raw)
To: Namhyung Kim, Arnaldo Carvalho de Melo, Alireza Haghdoost,
Ian Rogers
Cc: Peter Zijlstra, Ingo Molnar, Jiri Olsa, Adrian Hunter,
James Clark, linux-perf-users, linux-kernel
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.
Signed-off-by: Ian Rogers <irogers@google.com>
---
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(+)
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index b6217c49dd14..43b1ad44d165 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -1915,6 +1915,13 @@ void dso__delete(struct dso *dso)
__dso__delete(dso, /*close_data=*/true);
}
+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;
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index b0ee45a53f40..eb56bf9fb4d1 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -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]"
@@ -797,6 +798,7 @@ static inline refcount_t *dso__refcnt(struct dso *dso)
bool dso__can_shrink_symbols(const struct dso *dso);
bool dso__try_evict(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);
diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c
index 77f668d7557c..43605daedba6 100644
--- a/tools/perf/util/dsos.c
+++ b/tools/perf/util/dsos.c
@@ -19,6 +19,7 @@ void dsos__init(struct dsos *dsos)
{
init_rwsem(&dsos->lock);
+ dsos->machine = NULL;
dsos->cnt = 0;
dsos->allocated = 0;
dsos->dsos = NULL;
diff --git a/tools/perf/util/dsos.h b/tools/perf/util/dsos.h
index de10a10de777..87002b25a3bb 100644
--- a/tools/perf/util/dsos.h
+++ b/tools/perf/util/dsos.h
@@ -19,6 +19,7 @@ struct machine;
*/
struct dsos {
struct rw_semaphore lock;
+ struct machine *machine;
struct dso **dsos __pt_guarded_by(&lock) __guarded_by(&lock);
unsigned int cnt __guarded_by(&lock);
unsigned int allocated __guarded_by(&lock);
diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index f4108cc725fc..20215c12819e 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -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);
diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h
index b42c363723c9..80ef16384aa5 100644
--- a/tools/perf/util/machine.h
+++ b/tools/perf/util/machine.h
@@ -165,6 +165,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);
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 9f16bff1b72f..610bbc769ef7 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -4278,3 +4278,73 @@ size_t perf_session__shrink(struct perf_session *session)
return machines__shrink(&session->machines);
}
+
+static __thread bool perf_session__in_shrink;
+
+static bool perf_session__try_shrink(struct perf_session *session)
+{
+ size_t freed;
+
+ if (!session || perf_session__in_shrink)
+ return false;
+
+ perf_session__in_shrink = true;
+ freed = perf_session__shrink(session);
+ perf_session__in_shrink = false;
+
+ return freed > 0;
+}
+
+void *perf_session__malloc(struct perf_session *session, size_t size)
+{
+ void *ptr = malloc(size);
+
+ if (unlikely(!ptr && size != 0) && perf_session__try_shrink(session))
+ ptr = malloc(size);
+
+ return ptr;
+}
+
+void *perf_session__zalloc(struct perf_session *session, size_t size)
+{
+ void *ptr = zalloc(size);
+
+ if (unlikely(!ptr && size != 0) && perf_session__try_shrink(session))
+ ptr = zalloc(size);
+
+ return ptr;
+}
+
+void *perf_session__calloc(struct perf_session *session, size_t nmemb, size_t size)
+{
+ void *ptr = calloc(nmemb, size);
+
+ if (unlikely(!ptr && nmemb != 0 && size != 0) && perf_session__try_shrink(session))
+ ptr = calloc(nmemb, size);
+
+ return ptr;
+}
+
+void *perf_session__realloc(struct perf_session *session, void *ptr, size_t size)
+{
+ void *new_ptr = realloc(ptr, size);
+
+ if (unlikely(!new_ptr && size != 0) && perf_session__try_shrink(session))
+ new_ptr = realloc(ptr, size);
+
+ return new_ptr;
+}
+
+char *perf_session__strdup(struct perf_session *session, const char *s)
+{
+ char *dup;
+
+ if (!s)
+ return NULL;
+
+ dup = strdup(s);
+ if (unlikely(!dup) && perf_session__try_shrink(session))
+ dup = strdup(s);
+
+ return dup;
+}
diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
index cbea6792a008..c6157eddcc43 100644
--- a/tools/perf/util/session.h
+++ b/tools/perf/util/session.h
@@ -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 */
--
2.56.1.809.ga0b61035b5-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread* [PATCH v9 11/11] perf test symbols: Add session allocation wrapper and lock contention tests
2026-11-24 3:28 [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking Ian Rogers
` (9 preceding siblings ...)
2026-11-24 3:29 ` [PATCH v9 10/11] perf session: Add dso__session() and session-scoped allocation wrappers Ian Rogers
@ 2026-11-24 3:29 ` Ian Rogers
2026-11-24 3:49 ` [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking sashiko-bot
11 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2026-11-24 3:29 UTC (permalink / raw)
To: Namhyung Kim, Arnaldo Carvalho de Melo, Alireza Haghdoost,
Ian Rogers
Cc: Peter Zijlstra, Ingo Molnar, Jiri Olsa, Adrian Hunter,
James Clark, linux-perf-users, linux-kernel
Add test_session_alloc_shrink() to tools/perf/tests/symbols.c verifying:
- machine__session() and dso__session() back-pointer resolution.
- perf_session__malloc(), zalloc(), calloc(), realloc(), and strdup()
allocation wrappers and perf_session__shrink() eviction.
- Multithreaded contention between dso__find_binary_type()
(dso__data_open_lock -> dso__lock(dso)) and perf_session__shrink() under
dso__lock(dso), plus perf_session__e_machine() shard iteration.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/tests/symbols.c | 162 +++++++++++++++++++++++++++++++++++++
1 file changed, 162 insertions(+)
diff --git a/tools/perf/tests/symbols.c b/tools/perf/tests/symbols.c
index 1fd6bfdc4147..c6375bd7e3d9 100644
--- a/tools/perf/tests/symbols.c
+++ b/tools/perf/tests/symbols.c
@@ -8,6 +8,8 @@
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
+#include <pthread.h>
+#include <stdatomic.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
@@ -19,6 +21,7 @@
#include "dso.h"
#include "env.h"
#include "machine.h"
+#include "session.h"
#include "srcline.h"
#include "thread.h"
#include "symbol.h"
@@ -320,6 +323,161 @@ static int test_dsos_shrink(void)
return ret;
}
+struct shrink_lock_race_args {
+ struct dso *dso;
+ atomic_bool stop;
+};
+
+static void *shrink_lock_race_worker(void *arg)
+{
+ struct shrink_lock_race_args *args = arg;
+
+ while (!atomic_load_explicit(&args->stop, memory_order_relaxed))
+ dso__find_binary_type(args->dso);
+
+ return NULL;
+}
+
+static int test_session_alloc_shrink(void)
+{
+ struct perf_session *session = zalloc(sizeof(*session));
+ struct dso *dso;
+ struct thread *th;
+ pid_t pid = getpid();
+ char *p1 = NULL, *p2 = NULL, *p3 = NULL, *s = NULL, *tmp;
+ int ret = TEST_FAIL;
+
+ if (!session)
+ return TEST_FAIL;
+
+ perf_env__init(&session->header.env);
+ if (machines__init(&session->machines, session))
+ goto out;
+ session->machines.host.env = &session->header.env;
+ 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) {
+ pr_debug("perf_session__shrink failed to evict unreferenced DSO!\n");
+ goto out;
+ }
+ down_read(&session->machines.host.dsos.lock);
+ if (session->machines.host.dsos.cnt != 0) {
+ up_read(&session->machines.host.dsos.lock);
+ pr_debug("perf_session__shrink failed to evict unreferenced DSO!\n");
+ goto out;
+ }
+ up_read(&session->machines.host.dsos.lock);
+
+ /*
+ * Regression test for AB-BA lock inversion between dso__lock(dso_A)
+ * (held by dso__load() when calling perf_session__shrink() to evict
+ * unreferenced dso_B) and dso__data_open_lock (acquired before
+ * dso__lock(dso_A) by dso__find_binary_type()).
+ */
+ dso = machine__findnew_dso(&session->machines.host, "/tmp/perf_test_contended_dso");
+ if (!dso)
+ goto out;
+ {
+ bool was_singlethreaded = perf_singlethreaded;
+ struct shrink_lock_race_args race_args = {
+ .dso = dso,
+ .stop = false,
+ };
+ pthread_t worker;
+
+ perf_set_multithreaded();
+ if (pthread_create(&worker, NULL, shrink_lock_race_worker, &race_args) != 0) {
+ if (was_singlethreaded)
+ perf_set_singlethreaded();
+ dso__put(dso);
+ goto out;
+ }
+ for (int i = 0; i < 200; i++) {
+ struct dso *evict = machine__findnew_dso(&session->machines.host,
+ "/tmp/perf_test_evict_dso");
+
+ dso__put(evict);
+ mutex_lock(dso__lock(dso));
+ RC_CHK_ACCESS(dso)->bintype_searched = 0;
+ perf_session__shrink(session);
+ mutex_unlock(dso__lock(dso));
+ }
+ atomic_store_explicit(&race_args.stop, true, memory_order_relaxed);
+ pthread_join(worker, NULL);
+ if (was_singlethreaded)
+ perf_set_singlethreaded();
+ }
+ dso__put(dso);
+ perf_session__shrink(session);
+
+ /*
+ * Verify perf_session__e_machine() -> threads__for_each_thread() does
+ * not self-deadlock on table->lock when a non-leader thread looks up or
+ * creates its leader thread in the same shard.
+ */
+ {
+ bool was_singlethreaded = perf_singlethreaded;
+ uint16_t e_machine;
+
+ perf_set_multithreaded();
+ th = machine__findnew_thread(&session->machines.host, pid,
+ pid + THREADS__TABLE_SIZE);
+ if (!th) {
+ if (was_singlethreaded)
+ perf_set_singlethreaded();
+ goto out;
+ }
+ thread__put(th);
+ e_machine = perf_session__e_machine(session, NULL);
+ if (was_singlethreaded)
+ perf_set_singlethreaded();
+ if (e_machine == EM_NONE) {
+ pr_debug("perf_session__e_machine failed!\n");
+ goto out;
+ }
+ }
+
+ ret = TEST_OK;
+out:
+ free(p1);
+ free(p2);
+ free(p3);
+ free(s);
+ machines__exit(&session->machines);
+ perf_env__exit(&session->header.env);
+ free(session);
+ return ret;
+}
+
struct shrink_reload_find_args {
struct symbol *kept;
u64 first_start;
@@ -822,6 +980,10 @@ static int test__symbols(struct test_suite *test __maybe_unused, int subtest __m
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;
--
2.56.1.809.ga0b61035b5-goog
^ permalink raw reply related [flat|nested] 14+ messages in thread* Re: [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking
2026-11-24 3:28 [PATCH v9 00/11] perf symbol: Sorted array optimizations and memory shrinking Ian Rogers
` (10 preceding siblings ...)
2026-11-24 3:29 ` [PATCH v9 11/11] perf test symbols: Add session allocation wrapper and lock contention tests Ian Rogers
@ 2026-11-24 3:49 ` sashiko-bot
11 siblings, 0 replies; 14+ messages in thread
From: sashiko-bot @ 2026-11-24 3:49 UTC (permalink / raw)
To: Ian Rogers; +Cc: linux-perf-users
Sashiko has finished reviewing this 11-patch series:
- Patches 1-3 and 5-11: no issues found.
- Patch 4 ("perf test symbols: Add sorted insertion and equal-st..."): 1
potential issue (1 Medium)
A detailed review has been sent as a separate reply to patch 4.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124032902.3859564-1-irogers@google.com
^ permalink raw reply [flat|nested] 14+ messages in thread