* [PATCH v11 1/5] perf symbol: Fix symbols__fixup_end() for equal-start zero-sized aliases
2026-11-30 22:04 [PATCH v11 0/5] perf symbol: Fix alias end fixup and optimize sorted array Ian Rogers
@ 2026-11-30 22:04 ` Ian Rogers
2026-11-30 22:04 ` [PATCH v11 2/5] perf symbol: Drop num_by_name and sorted_by_name from struct symbols Ian Rogers
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Ian Rogers @ 2026-11-30 22:04 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 multiple zero-sized symbols share the same start address (such as
STB_GLOBAL and STB_WEAK aliases in /proc/kallsyms), symbols__fixup_end()
previously skipped earlier entries in the equal-start group and only
updated end on the final entry before the next distinct address.
Because __dso__load_kallsyms() and ELF loaders call symbols__fixup_end()
before symbols__fixup_duplicate(), inflating only the last entry gave it
a non-zero length while leaving earlier aliases at the same address with
zero length. In choose_best_symbol(), the initial "Prefer a symbol with
non zero length" check then unconditionally selected whichever alias
happened to appear last, bypassing the STT_NOTYPE, STB_WEAK, STB_GLOBAL,
underscore-count, and arch__choose_best_symbol() heuristics (for example
dropping a global symbol when followed by a weak alias at the same
address).
Track group_start across each equal-start group in symbols__fixup_end():
when no symbol at that start address already has a non-zero size
(!addr_has_sized), synthesize end for every symbol in the group so
subsequent deduplication compares equal-length aliases using binding and
name heuristics (and so allow_aliases callers do not retain zero-length
earlier aliases). When any symbol at that address already has an
explicit non-zero size (addr_has_sized), leave zero-sized aliases at
zero as before so choose_best_symbol() continues to prefer explicitly
sized symbols.
Fixes: 432746f8e0b6 ("perf symbols: Fixup symbol sizes before picking best ones")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/symbol.c | 74 +++++++++++++++++++++++-----------------
1 file changed, 42 insertions(+), 32 deletions(-)
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index d6a841dadcad..a6f667e9dedc 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -540,7 +540,7 @@ void symbols__fixup_duplicate(struct symbols *symbols)
/* Update zero-sized symbols using the address of the next symbol */
void symbols__fixup_end(struct symbols *symbols, bool is_kallsyms)
{
- struct symbol *curr, *prev;
+ unsigned int group_start = 0;
bool addr_has_sized;
down_write(&symbols->lock);
@@ -551,14 +551,12 @@ void symbols__fixup_end(struct symbols *symbols, bool is_kallsyms)
return;
}
- curr = symbols->symbols[0];
- addr_has_sized = curr->end > curr->start;
+ addr_has_sized = symbols->symbols[0]->end > symbols->symbols[0]->start;
for (unsigned int i = 1; i < symbols->cnt; i++) {
- prev = curr;
- curr = symbols->symbols[i];
+ struct symbol *curr = symbols->symbols[i];
- if (curr->start == prev->start) {
+ if (curr->start == symbols->symbols[group_start]->start) {
if (curr->end > curr->start)
addr_has_sized = true;
continue;
@@ -580,40 +578,52 @@ void symbols__fixup_end(struct symbols *symbols, bool is_kallsyms)
* like in:
* ffffffffc1937000 T hdmi_driver_init [snd_hda_codec_hdmi]
*/
- if (!addr_has_sized && prev->end == prev->start) {
- const char *prev_mod;
- const char *curr_mod;
+ if (!addr_has_sized) {
+ const char *curr_mod = is_kallsyms ? strchr(curr->name, '[') : NULL;
- if (!is_kallsyms) {
- prev->end = curr->start;
- } else {
- prev_mod = strchr(prev->name, '[');
- curr_mod = strchr(curr->name, '[');
-
- /* Last kernel/module symbol mapped to end of page */
- if (!prev_mod != !curr_mod) {
- prev->end = roundup(prev->end + 4096, 4096);
- /* Last symbol in the previous module */
- } else if (prev_mod && strcmp(prev_mod, curr_mod)) {
- prev->end = roundup(prev->end + 4096, 4096);
- } else {
- prev->end = curr->start;
- }
+ for (unsigned int j = group_start; j < i; j++) {
+ struct symbol *prev = symbols->symbols[j];
- /* The next module can start within that page */
- if (prev->end > curr->start)
- prev->end = curr->start;
+ if (prev->end != prev->start)
+ continue;
- pr_debug4("%s sym:%s end:%#" PRIx64 "\n",
- __func__, prev->name, prev->end);
+ if (!is_kallsyms) {
+ prev->end = curr->start;
+ } else {
+ const char *prev_mod = strchr(prev->name, '[');
+
+ /* Last kernel/module symbol mapped to end of page */
+ if (!prev_mod != !curr_mod) {
+ prev->end = roundup(prev->end + 4096, 4096);
+ /* Last symbol in the previous module */
+ } else if (prev_mod && strcmp(prev_mod, curr_mod)) {
+ prev->end = roundup(prev->end + 4096, 4096);
+ } else {
+ prev->end = curr->start;
+ }
+
+ /* The next module can start within that page */
+ if (prev->end > curr->start)
+ prev->end = curr->start;
+
+ pr_debug4("%s sym:%s end:%#" PRIx64 "\n",
+ __func__, prev->name, prev->end);
+ }
}
}
+ group_start = i;
addr_has_sized = curr->end > curr->start;
}
- /* Last entry */
- if (!addr_has_sized && curr->end == curr->start)
- curr->end = roundup(curr->start, 4096) + 4096;
+ /* Last equal-start group */
+ if (!addr_has_sized) {
+ for (unsigned int j = group_start; j < symbols->cnt; j++) {
+ struct symbol *curr = symbols->symbols[j];
+
+ if (curr->end == curr->start)
+ curr->end = roundup(curr->start, 4096) + 4096;
+ }
+ }
up_write(&symbols->lock);
}
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v11 2/5] perf symbol: Drop num_by_name and sorted_by_name from struct symbols
2026-11-30 22:04 [PATCH v11 0/5] perf symbol: Fix alias end fixup and optimize sorted array Ian Rogers
2026-11-30 22:04 ` [PATCH v11 1/5] perf symbol: Fix symbols__fixup_end() for equal-start zero-sized aliases Ian Rogers
@ 2026-11-30 22:04 ` Ian Rogers
2026-11-30 22:04 ` [PATCH v11 3/5] perf symbol: Replace sorted boolean with sorted_cnt prefix length Ian Rogers
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Ian Rogers @ 2026-11-30 22:04 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 a6f667e9dedc..aa9fb69bf6e5 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -243,9 +243,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)
@@ -258,9 +256,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)
@@ -532,8 +528,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);
}
@@ -767,8 +761,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;
}
@@ -871,17 +863,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;
@@ -889,8 +873,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;
}
@@ -900,7 +882,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);
@@ -909,7 +891,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,
@@ -994,8 +976,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;
}
@@ -1037,7 +1017,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];
}
@@ -1061,10 +1041,10 @@ struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name, size_
symbols = dso__named_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);
}
}
@@ -1110,7 +1090,7 @@ void dso__sort_by_name(struct dso *dso)
struct symbols *symbols = dso__named_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);
@@ -1127,7 +1107,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;
@@ -1389,8 +1369,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++) {
@@ -1463,8 +1441,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++) {
@@ -1618,8 +1594,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);
@@ -1648,8 +1622,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 64fdf402d219..e6e511920926 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -114,26 +114,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);
};
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v11 3/5] perf symbol: Replace sorted boolean with sorted_cnt prefix length
2026-11-30 22:04 [PATCH v11 0/5] perf symbol: Fix alias end fixup and optimize sorted array Ian Rogers
2026-11-30 22:04 ` [PATCH v11 1/5] perf symbol: Fix symbols__fixup_end() for equal-start zero-sized aliases Ian Rogers
2026-11-30 22:04 ` [PATCH v11 2/5] perf symbol: Drop num_by_name and sorted_by_name from struct symbols Ian Rogers
@ 2026-11-30 22:04 ` Ian Rogers
2026-11-30 22:04 ` [PATCH v11 4/5] perf symbol: Add secondary sorted run for dso__insert_symbol() Ian Rogers
2026-11-30 22:04 ` [PATCH v11 5/5] perf test symbols: Add sorted insertion and equal-start alias tests Ian Rogers
4 siblings, 0 replies; 6+ messages in thread
From: Ian Rogers @ 2026-11-30 22:04 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 aa9fb69bf6e5..d86b64a4776e 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -242,8 +242,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)
@@ -255,8 +255,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)
@@ -301,9 +301,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)) {
@@ -377,7 +378,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;
}
@@ -401,8 +402,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){
@@ -453,22 +456,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;
}
/*
@@ -478,7 +507,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);
@@ -527,6 +556,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);
}
@@ -755,9 +785,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);
@@ -970,11 +1001,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;
@@ -1366,8 +1399,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);
@@ -1438,8 +1471,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);
@@ -1586,8 +1619,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++)
@@ -1613,13 +1646,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 e6e511920926..41bc09d24e1e 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -109,14 +109,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
@@ -124,12 +128,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.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v11 4/5] perf symbol: Add secondary sorted run for dso__insert_symbol()
2026-11-30 22:04 [PATCH v11 0/5] perf symbol: Fix alias end fixup and optimize sorted array Ian Rogers
` (2 preceding siblings ...)
2026-11-30 22:04 ` [PATCH v11 3/5] perf symbol: Replace sorted boolean with sorted_cnt prefix length Ian Rogers
@ 2026-11-30 22:04 ` Ian Rogers
2026-11-30 22:04 ` [PATCH v11 5/5] perf test symbols: Add sorted insertion and equal-start alias tests Ian Rogers
4 siblings, 0 replies; 6+ messages in thread
From: Ian Rogers @ 2026-11-30 22:04 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 d86b64a4776e..74545a7ba9a0 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -243,6 +243,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;
}
@@ -256,6 +257,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;
}
@@ -457,12 +459,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;
/*
@@ -473,10 +479,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) {
@@ -498,11 +518,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)
{
@@ -787,14 +808,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;
@@ -813,7 +901,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;
@@ -826,8 +916,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
@@ -836,6 +928,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++) {
@@ -847,17 +941,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.
*/
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;
@@ -993,7 +1113,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)
@@ -1008,6 +1134,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;
@@ -1400,6 +1528,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);
@@ -1472,6 +1601,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);
@@ -1620,6 +1750,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 {
@@ -1647,12 +1778,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 41bc09d24e1e..fc42a1e21790 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -111,11 +111,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. */
@@ -130,11 +150,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.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v11 5/5] perf test symbols: Add sorted insertion and equal-start alias tests
2026-11-30 22:04 [PATCH v11 0/5] perf symbol: Fix alias end fixup and optimize sorted array Ian Rogers
` (3 preceding siblings ...)
2026-11-30 22:04 ` [PATCH v11 4/5] perf symbol: Add secondary sorted run for dso__insert_symbol() Ian Rogers
@ 2026-11-30 22:04 ` Ian Rogers
4 siblings, 0 replies; 6+ messages in thread
From: Ian Rogers @ 2026-11-30 22:04 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 both symbol
orderings (weak-then-global and global-then-weak) in
test__kallsyms_fixup_end().
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/tests/symbols.c | 273 +++++++++++++++++++++++++++++++++++--
1 file changed, 262 insertions(+), 11 deletions(-)
diff --git a/tools/perf/tests/symbols.c b/tools/perf/tests/symbols.c
index b73da581f895..dfda71fc8a70 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,30 @@ 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, weak/global alias pairs at the same address in both orderings, 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" },
+ { 0xffffffff81000800, STB_GLOBAL, "global_first_symbol" },
+ { 0xffffffff81000800, STB_WEAK, "weak_second_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 +529,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 +539,16 @@ 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 pairs resolve to the global symbol in both orders. */
+ if (check_symbol(dso, 0xffffffff81000510, "global_target_symbol",
+ 0xffffffff81000800))
+ ret = TEST_FAIL;
+ if (check_symbol(dso, 0xffffffff81000810, "global_first_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.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 6+ messages in thread