perf report: fix module symbol adjustment for s390x
PATCH v2 1/2 perf report: fix module symbol adjustment for s390x
The perf report tool does not display the addresses of kernel module symbols correctly.
For example symbol qeth_send_ipa_cmd in kernel module qeth.ko has this relative address for function qeth_send_ipa_cmd() [root@s8360047 linux]# nm -g drivers/s390/net/qeth.ko | fgrep send_ipa_cmd 0000000000013088 T qeth_send_ipa_cmd
The module is loaded at address
[root@s8360047 linux]# cat /sys/module/qeth/sections/.text 0x000003ff80296d20
[root@s8360047 linux]#
This should result in a start address of0x13088 + 0x3ff80296d20 = 0x3ff802a9da8
Using crash to verify the address on a live system:
[root@s8360046 linux]# crash vmlinux
crash 7.1.9++
Copyright (C) 2002-2016 Red Hat, Inc. Copyright (C) 2004, 2005, 2006, 2010 IBM Corporation
[...]
crash> mod -s qeth drivers/s390/net/qeth.ko
MODULE NAME SIZE OBJECT FILE
3ff8028d700 qeth 151552 drivers/s390/net/qeth.kocrash> sym qeth_send_ipa_cmd3ff802a9da8 (T) qeth_send_ipa_cmd [qeth] /root/linux/drivers/s390/net/qeth_core_main.c: 2944 crash>
Now perf report displays the address of symbol qeth_send_ipa_cmd:symbol__new: qeth_send_ipa_cmd 0x130f0-0x132ce
There is a difference of 0x68 between the entry in the symbol table (see nm command above) and perf. The difference is from the offset the .text segment of qeth.ko:
[root@s8360047 perf]# readelf -a drivers/s390/net/qeth.ko Section Headers:
[Nr] Name Type Address Offset
Size EntSize Flags Link Info Align
[ 0] NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] .note.gnu.build-i NOTE 0000000000000000 00000040
0000000000000024 0000000000000000 A 0 0 4
[ 2] .text PROGBITS 0000000000000000 00000068
000000000001c8a0 0000000000000000 AX 0 0 8As seen the .text segment has an offset of 0x68 with start address 0x0. Therefore 0x68 is added to the address of qeth_send_ipa_cmd and thus 0x13088 + 0x68 = 0x130f0 is displayed.
This is wrong, perf report needs to display the start address of symbol qeth_send_ipa_cmd at 0x13088 + qeth.ko.text section start address.
The qeth.ko module .text start address is available in the qeth.ko DSO map. Just identify the kernel module symbols and correct the addresses.
With the fix I see this correct address for symbol:symbol__new: qeth_send_ipa_cmd 0x3ff802a9da8-0x3ff802a9f86
--- tools/perf/arch/s390/util/sym-handling.c | 7 +++++++ tools/perf/util/symbol-elf.c | 8 +++++++- tools/perf/util/symbol.h | 3 +++ 3 files changed, 17 insertions(+), 1 deletion(-)
tools/perf/arch/s390/util/sym-handling.c +7 -0
@@ -19,4 +19,11 @@ bool elf__needs_adjust_symbols(GElf_Ehdr ehdr) return ehdr.e_type == ET_REL || ehdr.e_type == ET_DYN; } +void arch__adjust_sym_map_offset (GElf_Sym *sym, +GElf_Shdr *shdr__maybe_unused , +struct map *map) +{ +if (map->type ==MAP__FUNCTION ) + sym->st_value += map->start; +}#endif
tools/perf/util/symbol-elf.c +7 -1
@@ -793,6 +793,12 @@ static u64 ref_reloc(struct kmap *kmap)void __weak arch__sym_update (struct symbol *s__maybe_unused ,GElf_Sym *sym__maybe_unused ) { } +void __weak arch__adjust_sym_map_offset (GElf_Sym *sym,GElf_Shdr *shdr, +struct map *map__maybe_unused ) +{ + sym->st_value -= shdr->sh_addr - shdr->sh_offset; +} +int dso__load_sym (struct dso *dso,struct map *map,struct symsrc *syms_ss,struct symsrc *runtime_ss,int kmodule) {
@@ -973,7 +979,7 @@ int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,/* Adjust symbol to map to file offset */ if (adjust_kernel_syms) - sym.st_value -= shdr.sh_addr - shdr.sh_offset; +arch__adjust_sym_map_offset (&sym, &shdr, map); if (strcmp(section_name, (curr_dso->short_name +
tools/perf/util/symbol.h +3 -0
@@ -343,6 +343,9 @@ int setup_intlist(struct intlist **list, const char *list_str,#ifdef HAVE_LIBELF_SUPPORT bool elf__needs_adjust_symbols (GElf_Ehdr ehdr);void arch__sym_update (struct symbol *s,GElf_Sym *sym); +void arch__adjust_sym_map_offset (GElf_Sym *sym, +GElf_Shdr *shdr__maybe_unused , +struct map *map__maybe_unused );#endif #define SYMBOL_A 0
Signature · 1 line
2.9.3
PATCH v2 2/2 perf record: wrong size in perf_record_mmap for last kernel module
During work on perf report for s390 I ran into the following issue:
0 0x318 [0x78]: PERF_RECORD_MMAP -1/0:
[0x3ff804d6990(0xfffffc007fb2966f) @ 0]:
x /lib/modules/4.12.0perf1+/kernel/drivers/s390/net/qeth_l2.koThis is a PERF_RECORD_MMAP entry of the perf.data file with an invalid module size for qeth_l2.ko (the s390 ethernet device driver). Even a mainframe does not have 0xfffffc007fb2966f bytes of main memory.
It turned out that this wrong size is created by the perf record command. What happens is this function call sequence from __cmd_record():
perf_session__new():perf_session__create_kernel_maps():machine__create_kernel_maps():
machine__create_modules(): Creates map for all loaded kernel modules. modules__parse(): Reads /proc/modules and extracts module name and
load address (1st and last column)machine__create_module(): Called for every module found in /proc/modules.
Creates a new map for every module found and enters module name and start address into the map. Since the module end address is unknown it is set to zero.
This ends up with a kernel module map list sorted by module start addresses. All module end addresses are zero.
Last machine__create_kernel_maps() calls function map_groups__fixup_end(). This function iterates through the maps and assigns each map entry's end address the successor map entry start address. The last entry of the map group has no successor, so ~0 is used as end to consume the remaining memory.
Later __cmd_record calls function record__synthesize() which in turn calls perf_event__synthesize_kernel_mmap() and perf_event__synthesize_modules() to create PERF_REPORT_MMAP entries into the perf.data file.
On s390 this results in the last module qeth_l2.ko (which has highest start address, see module table:
[root@s8360047 perf]# cat /proc/modules
qeth_l2 86016 1 - Live 0x000003ff804d6000
qeth 266240 1 qeth_l2, Live 0x000003ff80296000
ccwgroup 24576 1 qeth, Live 0x000003ff80218000
vmur 36864 0 - Live 0x000003ff80182000
qdio 143360 2 qeth_l2,qeth, Live 0x000003ff80002000
[root@s8360047 perf]# )to be the last entry and its map has an end address of ~0.
When the PERF_RECORD_MMAP entry is created for kernel module qeth_l2.ko its start address and length is written. The length is calculated in line:
event->mmap.len = pos->end - pos->start;
and results in 0xffffffffffffffff - 0x3ff804d6990(*) = 0xfffffc007fb2966f
(*) On s390 the module start address is actually determined by a __weak function named arch__fix_module_text_start() in machine__create_module().
I think this improvable. We can use the module size (2nd column of /proc/modules) to get each loaded kernel module size and calculate its end address. Only for map entries which do not have a valid end address (end is still zero) we can use the heuristic we have now, that is use successor start address or ~0.
--- tools/perf/util/machine.c | 4 +++- tools/perf/util/symbol-elf.c | 2 +- tools/perf/util/symbol.c | 21 ++++++++++++++------- tools/perf/util/symbol.h | 2 +- 4 files changed, 19 insertions(+), 10 deletions(-)
tools/perf/util/machine.c +3 -1
@@ -1137,7 +1137,8 @@ int __weak arch__fix_module_text_start(u64 *start __maybe_unused,return 0 ; } -static int machine__create_module (void *arg,const char *name,u64 start) +static int machine__create_module (void *arg,const char *name,u64 start, +u64 size) {struct machine *machine = arg;struct map *map;
@@ -1148,6 +1149,7 @@ static int machine__create_module(void *arg, const char *name, u64 start) map =machine__findnew_module_map (machine, start, name);if (map ==NULL )return -1 ; + map->end =roundup (start + size, page_size);dso__kernel_module_get_build_id (map->dso, machine->root_dir);
tools/perf/util/symbol-elf.c +1 -1
@@ -1448,7 +1448,7 @@ static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,static int kcore_copy__process_modules (void *arg,const char *name__maybe_unused , -u64 start) +u64 start,u64 size__maybe_unused ) {struct kcore_copy_info *kci = arg;
tools/perf/util/symbol.c +14 -7
@@ -231,7 +231,8 @@ void __map_groups__fixup_end(struct map_groups *mg, enum map_type type) goto out_unlock;for (next =map__next (curr); next; next =map__next (curr)) { - curr->end = next->start; +if (!curr->end) + curr->end = next->start; curr = next; }
@@ -239,7 +240,8 @@ void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)* We still haven't the actual symbols, so guess the * last map final address. */ - curr->end = ~0ULL ; +if (!curr->end) + curr->end = ~0ULL ; out_unlock:pthread_rwlock_unlock (&maps->lock);
@@ -550,7 +552,7 @@ void dso__sort_by_name(struct dso *dso, enum map_type type)int modules__parse (const char *filename,void *arg,int (*process_module)(void *arg,const char *name, -u64 start)) +u64 start,u64 size)) {char *line =NULL ;size_t n;
@@ -563,8 +565,8 @@ int modules__parse(const char *filename, void *arg,while (1 ) {char name[PATH_MAX ]; -u64 start; -char *sep; +u64 start, size; +char *sep, *endptr;ssize_t line_len; line_len =getline (&line, &n, file);
@@ -596,7 +598,11 @@ int modules__parse(const char *filename, void *arg,scnprintf (name,sizeof (name),"[%s]" , line); - err =process_module (arg, name, start); + size =strtoul (sep +1 , &endptr,0 ); +if (*endptr !=' ' && *endptr !='\t' ) +continue ; + + err =process_module (arg, name, start, size);if (err)break ; }
@@ -943,7 +949,8 @@ static struct module_info *find_module(const char *name,return NULL ; } -static int __read_proc_modules (void *arg,const char *name,u64 start) +static int __read_proc_modules (void *arg,const char *name,u64 start, +u64 size__maybe_unused ) {struct rb_root *modules = arg;struct module_info *mi;
tools/perf/util/symbol.h +1 -1
@@ -273,7 +273,7 @@ int filename__read_build_id(const char *filename, void *bf, size_t size);int sysfs__read_build_id (const char *filename,void *bf,size_t size);int modules__parse (const char *filename,void *arg,int (*process_module)(void *arg,const char *name, -u64 start)); +u64 start,u64 size));int filename__read_debuglink (const char *filename,char *debuglink,size_t size);
Signature · 1 line
2.9.3