Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf tools: Fixup module symbol end address properly

I got a strange error on ARM to fail on processing FINISHED_ROUND
record. It turned out that it was failing in symbol__alloc_hist()
because the symbol size is too big.

When a sample is captured on a specific BPF program, it failed. I've
added a debug code and found the end address of the symbol is from
the next module which is placed far way.

ffff800008795778-ffff80000879d6d8: bpf_prog_1bac53b8aac4bc58_netcg_sock [bpf]
ffff80000879d6d8-ffff80000ad656b4: bpf_prog_76867454b5944e15_netcg_getsockopt [bpf]
ffff80000ad656b4-ffffd69b7af74048: bpf_prog_1d50286d2eb1be85_hn_egress [bpf] <---------- here
ffffd69b7af74048-ffffd69b7af74048: $x.5 [sha3_generic]
ffffd69b7af74048-ffffd69b7af740b8: crypto_sha3_init [sha3_generic]
ffffd69b7af740b8-ffffd69b7af741e0: crypto_sha3_update [sha3_generic]

The logic in symbols__fixup_end() just uses curr->start to update the
prev->end. But in this case, it won't work as it's too different.

I think ARM has a different kernel memory layout for modules and BPF
than on x86. Actually there's a logic to handle kernel and module
boundary. Let's do the same for symbols between different modules.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Reviewed-by: Leo Yan <leo.yan@linux.dev>
Cc: Will Deacon <will@kernel.org>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: John Garry <john.g.garry@oracle.com>
Link: https://lore.kernel.org/r/20240212233322.1855161-1-namhyung@kernel.org

+19 -2
+19 -2
tools/perf/util/symbol.c
··· 248 248 * segment is very big. Therefore do not fill this gap and do 249 249 * not assign it to the kernel dso map (kallsyms). 250 250 * 251 + * Also BPF code can be allocated separately from text segments 252 + * and modules. So the last entry in a module should not fill 253 + * the gap too. 254 + * 251 255 * In kallsyms, it determines module symbols using '[' character 252 256 * like in: 253 257 * ffffffffc1937000 T hdmi_driver_init [snd_hda_codec_hdmi] 254 258 */ 255 259 if (prev->end == prev->start) { 260 + const char *prev_mod; 261 + const char *curr_mod; 262 + 263 + if (!is_kallsyms) { 264 + prev->end = curr->start; 265 + continue; 266 + } 267 + 268 + prev_mod = strchr(prev->name, '['); 269 + curr_mod = strchr(curr->name, '['); 270 + 256 271 /* Last kernel/module symbol mapped to end of page */ 257 - if (is_kallsyms && (!strchr(prev->name, '[') != 258 - !strchr(curr->name, '['))) 272 + if (!prev_mod != !curr_mod) 273 + prev->end = roundup(prev->end + 4096, 4096); 274 + /* Last symbol in the previous module */ 275 + else if (prev_mod && strcmp(prev_mod, curr_mod)) 259 276 prev->end = roundup(prev->end + 4096, 4096); 260 277 else 261 278 prev->end = curr->start;