From nobody Sat Feb 14 05:36:23 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 42D1915B11D; Wed, 15 May 2024 21:11:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715807470; cv=none; b=qpJ8y9ZBNkhUqy/Js7dDnM9F2CJ44FM7dpiKv7FpyRGd7SN3S/53VRp47NmD6DrAxp8DI2Wz8pDYHEyrMzQ6rHGr0VK1Pa/RJnd+PqqXeQeCL1wkD3K+RZHk4QB4hT7Hu4GQ6x2JPmN2BzfySsP1V57nJvLTfLuZvkBD64r1JNQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715807470; c=relaxed/simple; bh=WyFMRDi/bs5IDpAIMfC56d+PwBTs80Y/L12c4A53rFA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Jh+X+a0ywGgvxZreBgBsVl//9OOeYkzCOeU4Kvx0stT+qz5dNl8Si/LoG4SExJRNhQDsqoTAv3OAQF7QmSp/xSCDDunjwoz8pbGcaaJtlvIiPtTN1n/PDUNDHUNwGTN2K+etWsPYarapRgESeI8mtSazePVZ3lAYJTdMsjJqwRs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id ACD151007; Wed, 15 May 2024 14:11:31 -0700 (PDT) Received: from PF4Q20KV.arm.com (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 6BDE23F7A6; Wed, 15 May 2024 14:11:04 -0700 (PDT) From: Leo Yan To: Arnaldo Carvalho de Melo , Adrian Hunter , James Clark , Peter Zijlstra , Ingo Molnar , Namhyung Kim , Ian Rogers , Mark Rutland , Alexander Shishkin , Jiri Olsa , Athira Rajeev , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Leo Yan Subject: [PATCH v2 1/2] perf maps: Sort kcore maps Date: Wed, 15 May 2024 22:10:44 +0100 Message-Id: <20240515211045.377080-2-leo.yan@arm.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240515211045.377080-1-leo.yan@arm.com> References: <20240515211045.377080-1-leo.yan@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When merging kcore maps into the kernel maps, it has an implicit requirement for the kcore maps ordering, otherwise, some sections delivered by the kcore maps will be ignored. Let's see below example: Ordering 1: Kcore maps | Start address | End address -----------------+--------------------+-------------------- kcore_text | 0xffff800080000000 | 0xffff8000822f0000 vmalloc | 0xffff800080000000 | 0xfffffdffbf800000 Ordering 2: Kcore maps | Start address | End address -----------------+--------------------+-------------------- vmalloc | 0xffff800080000000 | 0xfffffdffbf800000 kcore_text | 0xffff800080000000 | 0xffff8000822f0000 The 'kcore_text' map is a subset of the 'vmalloc' map. When merging these two maps into the kernal maps with the maps__merge_in() function, the ordering 1 inserts the 'kcore_text' map prior to the 'vmalloc' map, thus the 'kcore_text' map will be respected. On the other hand, if maps are inserted with the ordering 2, the 'vmalloc' is inserted ahead, as a result, its subset map will be ignored afterwards. To merge the maps in a reliable way, this commit sorts the kcore maps before merging them. Besides sorting the maps based on the end address, it also gives the priority to a subset map to insert it before its superset map in the list. Signed-off-by: Leo Yan --- tools/perf/util/symbol.c | 50 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 9e5940b5bc59..c1513976ab6e 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -1256,6 +1256,7 @@ static int kcore_mapfn(u64 start, u64 len, u64 pgoff,= void *data) { struct kcore_mapfn_data *md =3D data; struct map_list_node *list_node =3D map_list_node__new(); + struct map_list_node *node; =20 if (!list_node) return -ENOMEM; @@ -1269,8 +1270,55 @@ static int kcore_mapfn(u64 start, u64 len, u64 pgoff= , void *data) map__set_end(list_node->map, map__start(list_node->map) + len); map__set_pgoff(list_node->map, pgoff); =20 - list_add(&list_node->node, &md->maps); + list_for_each_entry(node, &md->maps, node) { + /* + * When the new map (list_node)'s end address is less than + * current node, it can be divided into three cases. + * + * Case 1: the new map does not overlap with the current node, + * as the new map's end address is less than the current node's + * start address. + * [*******node********] + * [***list_node***] `start `end + * `start `end + * + * Case 2: the new map overlaps with the current node. + * + * ,start ,end + * [*******node********] + * [***list_node***] + * `start `end + * + * Case 3: the new map is subset of the current node. + * + * ,start ,end + * [*******node********] + * [***list_node***] + * `start `end + * + * For above three cases, insert the new map node before the + * current node. + */ + if (map__end(node->map) > map__end(list_node->map)) + break; + + /* + * When the new map is subset of the current node and both nodes + * have the same end address, insert the new map node before the + * current node. + * + * ,start ,end + * [*******node********] + * [***list_node***] + * `start `end + */ + if ((map__end(node->map) =3D=3D map__end(list_node->map)) && + (map__start(node->map) <=3D map__start(list_node->map))) + break; + } =20 + /* Insert the new node (list_node) ahead */ + list_add_tail(&list_node->node, &node->node); return 0; } =20 --=20 2.34.1 From nobody Sat Feb 14 05:36:23 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 00B3E15B554; Wed, 15 May 2024 21:11:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715807472; cv=none; b=njDWhgrt5Vr82N0oMARQ15kh0lTRgxFcPgkrF3foIpVVAlU4N4kckkzsKkMOzuGsY3mbyyqF1kbJ0oKHsHWelMF9N7qffQMFmHR/tb/HqdAmXKY0gkUK8CC5YtKqmHqUDUn386m/FACpkfeRR51Ok7gjYBTO5EHsq3ySnTPR0P4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715807472; c=relaxed/simple; bh=6NiV8lMSfEqda6TLVZB7QbPpPh0hi2mYIQL4qTTbKvY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XY7j9ubRNK/wEPijJkv96Kt7pelczo2gUNzD8eQU0zvLPLfpq9WSb9/LajPOF/iy9bwPl9DvETFgvnuuyNE0EpCBFx456doOpGiWQHp1yYtVTHZJjojZpfOEcquvvh2fhsJQdcqyf2x6rMYJ+Wse0hrNk3PEO7BQJcSn8zJqVZc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 970071042; Wed, 15 May 2024 14:11:34 -0700 (PDT) Received: from PF4Q20KV.arm.com (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 970F83F7A6; Wed, 15 May 2024 14:11:07 -0700 (PDT) From: Leo Yan To: Arnaldo Carvalho de Melo , Adrian Hunter , James Clark , Peter Zijlstra , Ingo Molnar , Namhyung Kim , Ian Rogers , Mark Rutland , Alexander Shishkin , Jiri Olsa , Athira Rajeev , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Leo Yan Subject: [PATCH v2 2/2] perf maps: Remove the replacement of kernel map Date: Wed, 15 May 2024 22:10:45 +0100 Message-Id: <20240515211045.377080-3-leo.yan@arm.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240515211045.377080-1-leo.yan@arm.com> References: <20240515211045.377080-1-leo.yan@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now the kcore maps are organized in ordering, the kernel text section will be respected when merging into the kernel maps. As a result, it is no need to replace the kernel text section specially. This commit removes the code for replacement of the kernel text section. With this change, the final maps has a minor difference - it separates the kernel map [_stext.._end] into two sections: [_stext.._edata] and [_edata.._end] As result, it still keeps the intact info for the kernel regions. Signed-off-by: Leo Yan --- tools/perf/util/symbol.c | 67 +++++----------------------------------- 1 file changed, 8 insertions(+), 59 deletions(-) diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index c1513976ab6e..0712d3a6893f 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -1338,12 +1338,10 @@ static int dso__load_kcore(struct dso *dso, struct = map *map, { struct maps *kmaps =3D map__kmaps(map); struct kcore_mapfn_data md; - struct map *map_ref, *replacement_map =3D NULL; struct machine *machine; bool is_64_bit; int err, fd; char kcore_filename[PATH_MAX]; - u64 stext; =20 if (!kmaps) return -EINVAL; @@ -1388,52 +1386,6 @@ static int dso__load_kcore(struct dso *dso, struct m= ap *map, maps__remove_maps(kmaps, remove_old_maps, map); machine->trampolines_mapped =3D false; =20 - /* Find the kernel map using the '_stext' symbol */ - if (!kallsyms__get_function_start(kallsyms_filename, "_stext", &stext)) { - u64 replacement_size =3D 0; - struct map_list_node *new_node; - - list_for_each_entry(new_node, &md.maps, node) { - struct map *new_map =3D new_node->map; - u64 new_size =3D map__size(new_map); - - if (!(stext >=3D 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 =3D new_map; - replacement_size =3D new_size; - } - } - } - - if (!replacement_map) - replacement_map =3D 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 =3D map__get(map); - maps__remove(kmaps, map_ref); - - 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 =3D 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 =3D list_entry(md.maps.next, struct map_l= ist_node, node); @@ -1441,17 +1393,14 @@ static int dso__load_kcore(struct dso *dso, struct = map *map, =20 list_del_init(&new_node->node); =20 - /* 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 =3D -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 =3D -EINVAL; + goto out_err; } free(new_node); } --=20 2.34.1