public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Leo Yan <leo.yan@arm.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>,
	Ian Rogers <irogers@google.com>,
	Namhyung Kim <namhyung@kernel.org>,
	James Clark <james.clark@arm.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Athira Rajeev <atrajeev@linux.vnet.ibm.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Leo Yan <leo.yan@arm.com>
Subject: [PATCH v3 3/3] perf maps: Remove the replacement of kernel map
Date: Mon, 20 May 2024 10:06:47 +0100	[thread overview]
Message-ID: <20240520090647.949371-4-leo.yan@arm.com> (raw)
In-Reply-To: <20240520090647.949371-1-leo.yan@arm.com>

The kernel text map has been removed from the kernel maps by
maps__remove_maps(), and the kcore maps are organized in order, allowing
us to achieve neat kernel maps.

As a result, it is not necessary to replace the old kernel text map.
Instead, the commit finds the latest text map, assigns it to
'machine->vmlinux_map', and releases the old map.

One concern is if a platform fails to find a kernel text map after
updating maps list with kcore, in this case, it should not happen and
reports the failure.

Signed-off-by: Leo Yan <leo.yan@arm.com>
---
 tools/perf/util/symbol.c | 77 ++++++++++------------------------------
 1 file changed, 19 insertions(+), 58 deletions(-)

diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 915435d55498..a4b65d868fc9 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -1335,13 +1335,12 @@ static int dso__load_kcore(struct dso *dso, struct map *map,
 			   const char *kallsyms_filename)
 {
 	struct maps *kmaps = map__kmaps(map);
+	struct map *vmlinux_map;
 	struct kcore_mapfn_data md;
-	struct map *map_ref, *replacement_map = NULL;
 	struct machine *machine;
 	bool is_64_bit;
 	int err, fd;
 	char kcore_filename[PATH_MAX];
-	u64 stext;
 
 	if (!kmaps)
 		return -EINVAL;
@@ -1386,51 +1385,6 @@ static int dso__load_kcore(struct dso *dso, struct map *map,
 	maps__remove_maps(kmaps, remove_old_maps);
 	machine->trampolines_mapped = false;
 
-	/* Find the kernel map using the '_stext' symbol */
-	if (!kallsyms__get_function_start(kallsyms_filename, "_stext", &stext)) {
-		u64 replacement_size = 0;
-		struct map_list_node *new_node;
-
-		list_for_each_entry(new_node, &md.maps, node) {
-			struct map *new_map = new_node->map;
-			u64 new_size = map__size(new_map);
-
-			if (!(stext >= map__start(new_map) && stext < map__end(new_map)))
-				continue;
-
-			/*
-			 * On some architectures, ARM64 for example, the kernel
-			 * text can get allocated inside of the vmalloc segment.
-			 * Select the smallest matching segment, in case stext
-			 * falls within more than one in the list.
-			 */
-			if (!replacement_map || new_size < replacement_size) {
-				replacement_map = new_map;
-				replacement_size = new_size;
-			}
-		}
-	}
-
-	if (!replacement_map)
-		replacement_map = list_entry(md.maps.next, struct map_list_node, node)->map;
-
-	/*
-	 * Update addresses of vmlinux map. Re-insert it to ensure maps are
-	 * correctly ordered. Do this before using maps__merge_in() for the
-	 * remaining maps so vmlinux gets split if necessary.
-	 */
-	map_ref = map__get(map);
-
-	map__set_start(map_ref, map__start(replacement_map));
-	map__set_end(map_ref, map__end(replacement_map));
-	map__set_pgoff(map_ref, map__pgoff(replacement_map));
-	map__set_mapping_type(map_ref, map__mapping_type(replacement_map));
-
-	err = maps__insert(kmaps, map_ref);
-	map__put(map_ref);
-	if (err)
-		goto out_err;
-
 	/* Add new maps */
 	while (!list_empty(&md.maps)) {
 		struct map_list_node *new_node = list_entry(md.maps.next, struct map_list_node, node);
@@ -1438,21 +1392,28 @@ static int dso__load_kcore(struct dso *dso, struct map *map,
 
 		list_del_init(&new_node->node);
 
-		/* skip if replacement_map, already inserted above */
-		if (!RC_CHK_EQUAL(new_map, replacement_map)) {
-			/*
-			 * Merge kcore map into existing maps,
-			 * and ensure that current maps (eBPF)
-			 * stay intact.
-			 */
-			if (maps__merge_in(kmaps, new_map)) {
-				err = -EINVAL;
-				goto out_err;
-			}
+		/*
+		 * Merge kcore map into existing maps,
+		 * and ensure that current maps (eBPF)
+		 * stay intact.
+		 */
+		if (maps__merge_in(kmaps, new_map)) {
+			err = -EINVAL;
+			goto out_err;
 		}
 		free(new_node);
 	}
 
+	/* Update vmlinux_map */
+	vmlinux_map = maps__find(kmaps, map__start(map));
+	if (vmlinux_map) {
+		free(machine->vmlinux_map);
+		machine->vmlinux_map = vmlinux_map;
+	} else {
+		pr_err("Failed to find vmlinux map from kcore\n");
+		goto out_err;
+	}
+
 	if (machine__is(machine, "x86_64")) {
 		u64 addr;
 
-- 
2.34.1


  parent reply	other threads:[~2024-05-20  9:07 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-20  9:06 [PATCH v3 0/3] perf maps: Improve the kcore maps merging Leo Yan
2024-05-20  9:06 ` [PATCH v3 1/3] perf maps: Sort kcore maps Leo Yan
2024-05-22 10:31   ` Adrian Hunter
2024-05-25  9:29     ` Leo Yan
2024-05-28 15:00       ` James Clark
2024-05-29  8:45         ` Adrian Hunter
2024-05-20  9:06 ` [PATCH v3 2/3] perf maps: Remove the kernel text map with maps__remove_maps() Leo Yan
2024-05-22 11:27   ` Adrian Hunter
2024-05-20  9:06 ` Leo Yan [this message]
2024-05-22  8:57   ` [PATCH v3 3/3] perf maps: Remove the replacement of kernel map Adrian Hunter
2024-05-28 15:33   ` James Clark

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240520090647.949371-4-leo.yan@arm.com \
    --to=leo.yan@arm.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=atrajeev@linux.vnet.ibm.com \
    --cc=irogers@google.com \
    --cc=james.clark@arm.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=namhyung@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox