Fixups for kernel maps insertion
Arnaldo Carvalho de Melo
maintainerv2 cover Fixups for kernel maps insertion
From: Arnaldo Carvalho de Melo <[email protected]>
Hi Namhyung,
Here are the patches from the recent session on fixing the build
when DEBUG=1 is used, please take a look.
I think that since there are no reported problems and ended up
being a sizeable series, we better apply it to perf-tools-next,
Best Regards,
- Arnaldo
v2: Use map__set_kmap_maps() as suggested by Ian.
Arnaldo Carvalho de Melo (3):
perf maps: Introduce map__set_kmap_maps() for kernel maps perf maps: Set the kmaps for newly created/added kernel maps perf maps: Add missing map__set_kmap_maps() when replacing a kernel map
Namhyung Kim (3):
perf machine: Fixup kernel maps ends after adding extra maps perf maps: Fixup maps_by_name when modifying maps_by_address perf machine: Fix insertion of PERF_RECORD_KSYMBOL related kernel maps
tools/perf/util/machine.c | 6 ++-- tools/perf/util/maps.c | 58 +++++++++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 12 deletions(-)
Signature · 1 line
2.48.1
Arnaldo Carvalho de Melo
maintainerPATCH v2 1/6 perf maps: Introduce map__set_kmap_maps() for kernel maps
From: Arnaldo Carvalho de Melo <[email protected]>
We need to set it in other places than __maps__insert(), so that we can have access to the 'struct maps' from a kernel 'struct map'.
When building perf with 'DEBUG=1' we can notice it failing a consistency check done in the check_invariants() function:
root@number:~# perf record -- perf test -w offcpu [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.040 MB perf.data (23 samples) ] perf: util/maps.c:95: check_invariants: Assertion `map__end(prev) <= map__end(map)' failed. Aborted (core dumped) root@number:~#
The investigation on that was happening bisected to 876e80cf83d10585 ("perf tools: Fixup end address of modules"), and the following patches will plug the problems found, this patch is just legwork on that direction.
Use the map__set_kmap_maps() name as per a review comment from Ian Rogers, later there are further suggestions from him on getting rid of the kmaps variable, see the thread referenced in the Link below.
--- tools/perf/util/maps.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-)
tools/perf/util/maps.c +21 -8
@@ -428,11 +428,29 @@ static unsigned int maps__by_name_index(const struct maps *maps, const struct mareturn -1 ; } +static void map__set_kmap_maps (struct map *map,struct maps *maps) +{ +struct dso *dso; + +if (map ==NULL ) +return ; + + dso =map__dso (map); + +if (dso &&dso__kernel (dso)) { +struct kmap *kmap =map__kmap (map); + +if (kmap) + kmap->kmaps = maps; +else +pr_err ("Internal error: kernel dso with non kernel map\n" ); + } +} +static int __maps__insert (struct maps *maps,struct map *new) {struct map **maps_by_address =maps__maps_by_address (maps);struct map **maps_by_name =maps__maps_by_name (maps); -const struct dso *dso =map__dso (new);unsigned int nr_maps =maps__nr_maps (maps);unsigned int nr_allocate =RC_CHK_ACCESS (maps)->nr_maps_allocated;
@@ -483,14 +501,9 @@ static int __maps__insert(struct maps *maps, struct map *new) }if (map__end (new) <map__start (new))RC_CHK_ACCESS (maps)->ends_broken = true; -if (dso &&dso__kernel (dso)) { -struct kmap *kmap =map__kmap (new); -if (kmap) - kmap->kmaps = maps; -else -pr_err ("Internal error: kernel dso with non kernel map\n" ); - } +map__set_kmap_maps (new, maps); +return 0 ; }
Signature · 1 line
2.48.1
Arnaldo Carvalho de Melo
maintainerPATCH v2 2/6 perf maps: Set the kmaps for newly created/added kernel maps
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
When using __maps__insert_sorted() the map kmaps field needs to be initialized, as we need kernel maps to work with map__kmap().
Fix it by using the newly introduced map__set_kmap() method.
--- tools/perf/util/maps.c | 3 +++ 1 file changed, 3 insertions(+)
tools/perf/util/maps.c +3 -0
@@ -798,6 +798,9 @@ static int __maps__insert_sorted(struct maps *maps, unsigned int first_after_ind }RC_CHK_ACCESS (maps)->nr_maps = nr_maps + to_add;maps__set_maps_by_name_sorted (maps, false); +map__set_kmap_maps (new1, maps); +map__set_kmap_maps (new2, maps); +check_invariants (maps);return 0 ; }
Signature · 1 line
2.48.1
Arnaldo Carvalho de Melo
maintainerPATCH v2 3/6 perf machine: Fixup kernel maps ends after adding extra maps
In reply to Arnaldo Carvalho de MeloFrom: Namhyung Kim <[email protected]>
I just noticed it would add extra kernel maps after modules. I think it should fixup end address of the kernel maps after adding all maps first.
--- tools/perf/util/machine.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/util/machine.c +2 -2
@@ -1462,8 +1462,6 @@ static int machine__create_modules(struct machine *machine)if (modules__parse (modules, machine, machine__create_module))return -1 ; -maps__fixup_end (machine__kernel_maps (machine)); -if (!machine__set_modules_path (machine))return 0 ;
@@ -1557,6 +1555,8 @@ int machine__create_kernel_maps(struct machine *machine) } } +maps__fixup_end (machine__kernel_maps (machine)); + out_put:dso__put (kernel);return ret;
Signature · 1 line
2.48.1
Arnaldo Carvalho de Melo
maintainerPATCH v2 4/6 perf maps: Fixup maps_by_name when modifying maps_by_address
In reply to Arnaldo Carvalho de MeloFrom: Namhyung Kim <[email protected]>
We can't just replacing the map in the maps_by_address and not touching on the maps_by_name, that would leave the refcount as 1 and thus trip another consistency check, this one:
perf: util/maps.c:110: check_invariants:
Assertion `refcount_read(map__refcnt(map)) > 1' failed.
106 /*
107 * Maps by name maps should be in maps_by_address, so 108 * the reference count should be higher. 109 */
110 assert(refcount_read(map__refcnt(map)) > 1);
Committer notice:
Initialize the newly added 'ni' variable, that really can't be accessed unitialized trips some gcc versions, like:
12 20.00 archlinux:base : FAIL gcc version 13.2.1 20230801 (GCC)
util/maps.c: In function ‘__maps__fixup_overlap_and_insert’:
util/maps.c:896:54: error: ‘ni’ may be used uninitialized [-Werror=maybe-uninitialized]
896 | map__put(maps_by_name[ni]);
| ^
util/maps.c:816:25: note: ‘ni’ was declared here
816 | unsigned int i, ni;
| ^~
cc1: all warnings being treated as errors
make[3]: *** [/git/perf-6.14.0-rc1/tools/build/Makefile.build:138: util] Error 2
--- tools/perf/util/maps.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-)
tools/perf/util/maps.c +23 -1
@@ -813,7 +813,7 @@ static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map *new) {int err =0 ;FILE *fp =debug_file (); -unsigned int i; +unsigned int i, ni =INT_MAX ;// Some gcc complain, but depends on maps_by_name... if (!maps__maps_by_address_sorted (maps))__maps__sort_by_address (maps);
@@ -824,6 +824,7 @@ static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map *new)*/ for (i =first_ending_after (maps, new); i <maps__nr_maps (maps); ) {struct map **maps_by_address =maps__maps_by_address (maps); +struct map **maps_by_name =maps__maps_by_name (maps);struct map *pos = maps_by_address[i];struct map *before =NULL , *after =NULL ;
@@ -843,6 +844,9 @@ static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map *new)map__fprintf (pos, fp); } +if (maps_by_name) + ni =maps__by_name_index (maps, pos); +/* * Now check if we need to create new maps for areas not * overlapped by the new map:
@@ -887,6 +891,12 @@ static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map *new)if (before) {map__put (maps_by_address[i]); maps_by_address[i] = before; + +if (maps_by_name) { +map__put (maps_by_name[ni]); + maps_by_name[ni] =map__get (before); + } +/* Maps are still ordered, go to next one. */ i++;if (after) {
@@ -908,6 +918,12 @@ static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map *new)*/ map__put (maps_by_address[i]); maps_by_address[i] =map__get (new); + +if (maps_by_name) { +map__put (maps_by_name[ni]); + maps_by_name[ni] =map__get (new); + } + err =__maps__insert_sorted (maps, i +1 , after,NULL );map__put (after);check_invariants (maps);
@@ -926,6 +942,12 @@ static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map *new)*/ map__put (maps_by_address[i]); maps_by_address[i] =map__get (new); + +if (maps_by_name) { +map__put (maps_by_name[ni]); + maps_by_name[ni] =map__get (new); + } +check_invariants (maps);return err; }
Signature · 1 line
2.48.1
Arnaldo Carvalho de Melo
maintainerPATCH v2 5/6 perf maps: Add missing map__set_kmap_maps() when replacing a kernel map
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
Since in this case __maps__insert_sorted() is not called and thus doesn't have the opportunity to do the needed map__set_kmap_maps() calls on the new map.
--- tools/perf/util/maps.c | 2 ++ 1 file changed, 2 insertions(+)
tools/perf/util/maps.c +2 -0
@@ -948,6 +948,8 @@ static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map *new) maps_by_name[ni] =map__get (new); } +map__set_kmap_maps (new, maps); +check_invariants (maps);return err; }
Signature · 1 line
2.48.1
Arnaldo Carvalho de Melo
maintainerPATCH v2 6/6 perf machine: Fix insertion of PERF_RECORD_KSYMBOL related kernel maps
In reply to Arnaldo Carvalho de MeloFrom: Namhyung Kim <[email protected]>
This was detected at the end of a 'perf record' session when build-id collection was enabled and thus the BPF programs put in place while the session was running, some even put in place by perf itself were processed and inserted, with some overlaps related to BPF trampolines and programs took place.
Using maps__fixup_overlap_and_insert() instead of maps__insert() "fixes" the problem, in the sense that overlaps will be dealt with and then the consistency will be kept, but it would be interesting to fully understand why such overlaps take place and how to deal with them when doing symbol resolution.
--- tools/perf/util/machine.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/machine.c +1 -1
@@ -718,7 +718,7 @@ static int machine__process_ksymbol_register(struct machine *machine,map__set_start (map, event->ksymbol.addr);map__set_end (map,map__start (map) + event->ksymbol.len); - err =maps__insert (machine__kernel_maps (machine), map); + err =maps__fixup_overlap_and_insert (machine__kernel_maps (machine), map);if (err) { err = -ENOMEM ; goto out;
Signature · 1 line
2.48.1
Namhyung Kim
maintainerQuoted 6 lines · Arnaldo Carvalho de Melo
From: Arnaldo Carvalho de Melo <[email protected]>
Hi Namhyung,
Here are the patches from the recent session on fixing the build
when DEBUG=1 is used, please take a look.
Looks good. It's more fine-grained than I expected. :)
Quoted 3 lines
I think that since there are no reported problems and ended up
being a sizeable series, we better apply it to perf-tools-next,
Sure, will add there.
Thanks,
Namhyung
Quoted 24 lines
Best Regards,
- Arnaldo
v2: Use
map__set_kmap_maps()as suggested by Ian.Arnaldo Carvalho de Melo (3):
perf maps: Introducemap__set_kmap_maps()for kernel maps perf maps: Set the kmaps for newly created/added kernel maps perf maps: Add missingmap__set_kmap_maps()when replacing a kernel mapNamhyung Kim (3):
perf machine: Fixup kernel maps ends after adding extra maps perf maps: Fixupmaps_by_namewhen modifyingmaps_by_addressperf machine: Fix insertion ofPERF_RECORD_KSYMBOLrelated kernel mapstools/perf/util/machine.c | 6 ++-- tools/perf/util/maps.c | 58 +++++++++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 12 deletions(-)Signature · 1 line
2.48.1
Namhyung Kim
maintainerQuoted 8 lines · Namhyung Kim
Quoted 6 lines · Arnaldo Carvalho de Melo
From: Arnaldo Carvalho de Melo <[email protected]>
Hi Namhyung,
Here are the patches from the recent session on fixing the build
when DEBUG=1 is used, please take a look.
Quoted 7 lines
Looks good. It's more fine-grained than I expected. :)
Quoted 3 lines
I think that since there are no reported problems and ended up
being a sizeable series, we better apply it to perf-tools-next,Sure, will add there.
Ian, are you ok with this now?
Thanks,
Namhyung
Quoted 13 lines · Namhyung Kim
Quoted 11 lines · Namhyung Kim
Quoted 6 lines · Arnaldo Carvalho de Melo
From: Arnaldo Carvalho de Melo <[email protected]>
Hi Namhyung,
Here are the patches from the recent session on fixing the build
when DEBUG=1 is used, please take a look.Looks good. It's more fine-grained than I expected. :)
Quoted 1 line
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
I think that since there are no reported problems and ended up
being a sizeable series, we better apply it to perf-tools-next,Sure, will add there.
Ian, are you ok with this now?
Yep, sorry for the delay.
Thanks,
Ian
Quoted 3 lines
Thanks,
Namhyung
Quoted 9 lines · Arnaldo Carvalho de Melo
Here are the patches from the recent session on fixing the build
when DEBUG=1 is used, please take a look.I think that since there are no reported problems and ended up
being a sizeable series, we better apply it to perf-tools-next,Best Regards,
[...]
Applied to perf-tools-next, thanks!
Best regards,
Namhyung