From nobody Fri Sep 25 00:41:23 2026 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0A2F4DD3C0; Fri, 18 Sep 2026 10:21:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789726922; cv=none; b=ZTli5gy82o+cBum/fVyxsvtFSIi6s0/eYJaNcwTWmyUU89xO2FuAt3xKULQoLCqQG6c6RVJa41JOf7wyc3P0jSwg99uqFjktwIJDysyC9/CWKi+2J7i1yu02uPcv1VjHeeQbNg2cd6eNT0dFFagaDF//w/k1oArw8juhwlnLc7U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789726922; c=relaxed/simple; bh=QFXDH9UXsakrxfEHCXhFRjO+WWgVPallibyVnpTnnkY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZHQgylybEYk5kwUDt5UYXUgldqSMEMX237Ozi1/I11tKuBj/UEXEPyKfWLKIOsZVhh2U9SX3LUZEU+NUdozd4/zFKPsfoG+KYoMECKjrI8NaR8gmo1q1iSb3mv4rF81ucXmrE8C1UaNCf49mzcJQjTF2MFx44G1AM4llYSn4png= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=Me6JNl3W; arc=none smtp.client-ip=220.197.31.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="Me6JNl3W" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=+7 teu6LWN+4dSUq6xi9dPDxZX4IejJ2vSM2DIUCr+OU=; b=Me6JNl3W5IZ1PJdOG0 rkWyNJtxSjyVGww8bboc9avoXhFJKnuPKChTQMaYtSDWUKqAniU5SJxf9kRfARri TfOXyHHhjNZcSawSHXAfBdVDtkL8hnASuqkh+UEBwUl0t1VOjzfY8zFZtKE12hI7 b8cNJOHl5DzdcfAQT5L/QTo48= Received: from localhost (unknown []) by gzsmtp2 (Coremail) with SMTP id PSgvCgB3ObOQEK1qLLNJAA--.8472S2; Fri, 18 Sep 2026 18:21:05 +0800 (CST) From: Hui Su To: qmo@kernel.org Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org, emil@etsalapatis.com, ihor.solodrai@linux.dev, davem@davemloft.net, kuba@kernel.org, horms@kernel.org, Hui Su Subject: [PATCH bpf v2 1/2] bpftool: Fix CPU IDs in per-CPU map output Date: Fri, 18 Sep 2026 19:20:51 +0900 Message-ID: <20260918102052.1247819-2-sh_def@163.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260918102052.1247819-1-sh_def@163.com> References: <20260918102052.1247819-1-sh_def@163.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 X-CM-TRANSID: PSgvCgB3ObOQEK1qLLNJAA--.8472S2 X-Coremail-Antispam: 1Uf129KBjvJXoW3GryUZF4UAF4xAry8AF1fZwb_yoWDGF1Upw 45t348Kr48XrW3Ww4Ska98uF4Ygrs7Gw17JasrG3yrZFWfXrn7ZF4UKF10kr15urWFvF4j yFs0vryj9a1xCr7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pi8pndUUUUU= X-CM-SenderInfo: xvkbvvri6rljoofrz/xtbC6hMujWqtEJP8CQAA3r Content-Type: text/plain; charset="utf-8" bpftool uses dense per-CPU value-buffer slots when printing per-CPU map values. It also uses the slot index as the CPU ID, which produces incorrect labels when the possible CPU mask is sparse, such as 0,2-3. Parse the possible CPU mask and use the corresponding logical CPU ID in plain, JSON, and BTF-formatted output. Keep the dense slot index for accessing the per-CPU value buffer, and propagate CPU-ID lookup and map output errors through the shared output path to its callers. Tested: - Built tools/bpf/bpftool successfully on the host. - Booted an arm64 QEMU guest with a patched virt device tree reporting possible=3D0,2-3, present=3D0,2-3, and online=3D0,2-3. - Compared pre-fix and fixed plain and JSON map output in that guest: labels changed from CPU 0,1,2 to CPU 0,2,3. - Loaded a BTF-described per-CPU array and dumped it with the fixed bpftool in plain BTF and JSON formats; both formats reported CPU 0,2,3 while preserving the three dense per-CPU values. Fixes: 71bb428fe2c1 ("tools: bpf: add bpftool") Link: https://lore.kernel.org/bpf/20260813155131.1022745-3-sh_def@163.com/ Signed-off-by: Hui Su Acked-by: Quentin Monnet --- tools/bpf/bpftool/common.c | 40 ++++++++++++++++++++ tools/bpf/bpftool/main.h | 1 + tools/bpf/bpftool/map.c | 76 ++++++++++++++++++++++++++------------ 3 files changed, 93 insertions(+), 24 deletions(-) diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c index ef366ccc9650..ad10bfe8a0fc 100644 --- a/tools/bpf/bpftool/common.c +++ b/tools/bpf/bpftool/common.c @@ -31,6 +31,7 @@ #include #include #include /* libbpf_num_possible_cpus */ +#include #include #include =20 @@ -655,6 +656,45 @@ unsigned int get_possible_cpus(void) return cpus; } =20 +int get_possible_cpu_ids(int **cpu_ids) +{ + const char *possible_cpus_file =3D "/sys/devices/system/cpu/possible"; + bool *mask =3D NULL; + int mask_sz, nr_cpus =3D 0; + int *ids =3D NULL; + int i, res; + + *cpu_ids =3D NULL; + + res =3D parse_cpu_mask_file(possible_cpus_file, &mask, &mask_sz); + if (res) { + p_err("failed to parse possible CPU mask: %s", strerror(-res)); + return res; + } + + for (i =3D 0; i < mask_sz; i++) + nr_cpus +=3D mask[i]; + + ids =3D calloc(nr_cpus, sizeof(*ids)); + if (!ids) { + p_err("failed to allocate possible CPU IDs: %s", strerror(ENOMEM)); + res =3D -ENOMEM; + goto out; + } + + for (i =3D 0, nr_cpus =3D 0; i < mask_sz; i++) { + if (mask[i]) + ids[nr_cpus++] =3D i; + } + *cpu_ids =3D ids; + ids =3D NULL; + res =3D nr_cpus; +out: + free(ids); + free(mask); + return res; +} + static char * ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf) { diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h index 78b6e0ebb85d..6540b632a3ac 100644 --- a/tools/bpf/bpftool/main.h +++ b/tools/bpf/bpftool/main.h @@ -216,6 +216,7 @@ void print_hex_data_json(uint8_t *data, size_t len); =20 unsigned int get_page_size(void); unsigned int get_possible_cpus(void); +int get_possible_cpu_ids(int **cpu_ids); const char * ifindex_to_arch(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, const char **op= t); =20 diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index 684a8fb72414..586c9e076a48 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c @@ -70,7 +70,7 @@ static void *alloc_value(struct bpf_map_info *info) =20 static int do_dump_btf(const struct btf_dumper *d, struct bpf_map_info *map_info, void *key, - void *value) + void *value, const int *cpu_ids) { __u32 value_id; int ret =3D 0; @@ -101,7 +101,7 @@ static int do_dump_btf(const struct btf_dumper *d, step =3D round_up(map_info->value_size, 8); for (i =3D 0; i < n; i++) { jsonw_start_object(d->jw); - jsonw_int_field(d->jw, "cpu", i); + jsonw_int_field(d->jw, "cpu", cpu_ids[i]); jsonw_name(d->jw, "value"); ret =3D btf_dumper_type(d, value_id, value + i * step); jsonw_end_object(d->jw); @@ -130,7 +130,8 @@ static json_writer_t *get_btf_writer(void) } =20 static void print_entry_json(struct bpf_map_info *info, unsigned char *key, - unsigned char *value, struct btf *btf) + unsigned char *value, struct btf *btf, + const int *cpu_ids) { jsonw_start_object(json_wtr); =20 @@ -150,7 +151,7 @@ static void print_entry_json(struct bpf_map_info *info,= unsigned char *key, }; =20 jsonw_name(json_wtr, "formatted"); - do_dump_btf(&d, info, key, value); + do_dump_btf(&d, info, key, value, cpu_ids); } } else { unsigned int i, n, step; @@ -166,7 +167,7 @@ static void print_entry_json(struct bpf_map_info *info,= unsigned char *key, for (i =3D 0; i < n; i++) { jsonw_start_object(json_wtr); =20 - jsonw_int_field(json_wtr, "cpu", i); + jsonw_int_field(json_wtr, "cpu", cpu_ids[i]); =20 jsonw_name(json_wtr, "value"); print_hex_data_json(value + i * step, @@ -183,7 +184,7 @@ static void print_entry_json(struct bpf_map_info *info,= unsigned char *key, }; =20 jsonw_name(json_wtr, "formatted"); - do_dump_btf(&d, info, key, value); + do_dump_btf(&d, info, key, value, cpu_ids); } } =20 @@ -245,7 +246,7 @@ print_entry_error(struct bpf_map_info *map_info, void *= key, int lookup_errno) } =20 static void print_entry_plain(struct bpf_map_info *info, unsigned char *ke= y, - unsigned char *value) + unsigned char *value, const int *cpu_ids) { if (!map_is_per_cpu(info->type)) { bool single_line, break_names; @@ -285,8 +286,8 @@ static void print_entry_plain(struct bpf_map_info *info= , unsigned char *key, } if (info->value_size) { for (i =3D 0; i < n; i++) { - printf("value (CPU %02u):%c", - i, info->value_size > 16 ? '\n' : ' '); + printf("value (CPU %02d):%c", + cpu_ids[i], info->value_size > 16 ? '\n' : ' '); fprint_hex(stdout, value + i * step, info->value_size, " "); printf("\n"); @@ -742,7 +743,7 @@ static int do_show(int argc, char **argv) =20 static int dump_map_elem(int fd, void *key, void *value, struct bpf_map_info *map_info, struct btf *btf, - json_writer_t *btf_wtr) + json_writer_t *btf_wtr, const int *cpu_ids) { if (bpf_map_lookup_elem(fd, key, value)) { print_entry_error(map_info, key, errno); @@ -750,7 +751,7 @@ static int dump_map_elem(int fd, void *key, void *value, } =20 if (json_output) { - print_entry_json(map_info, key, value, btf); + print_entry_json(map_info, key, value, btf, cpu_ids); } else if (btf) { struct btf_dumper d =3D { .btf =3D btf, @@ -758,9 +759,9 @@ static int dump_map_elem(int fd, void *key, void *value, .is_plain_text =3D true, }; =20 - do_dump_btf(&d, map_info, key, value); + do_dump_btf(&d, map_info, key, value, cpu_ids); } else { - print_entry_plain(map_info, key, value); + print_entry_plain(map_info, key, value, cpu_ids); } =20 return 0; @@ -833,6 +834,7 @@ map_dump(int fd, struct bpf_map_info *info, json_writer= _t *wtr, void *key, *value, *prev_key; unsigned int num_elems =3D 0; struct btf *btf =3D NULL; + int *cpu_ids =3D NULL; int err; =20 key =3D malloc(info->key_size); @@ -845,6 +847,16 @@ map_dump(int fd, struct bpf_map_info *info, json_write= r_t *wtr, =20 prev_key =3D NULL; =20 + if (map_is_per_cpu(info->type)) { + int cpu_cnt; + + cpu_cnt =3D get_possible_cpu_ids(&cpu_ids); + if (cpu_cnt < 0) { + err =3D cpu_cnt; + goto exit_free; + } + } + if (wtr) { err =3D get_map_kv_btf(info, &btf); if (err) { @@ -876,7 +888,7 @@ map_dump(int fd, struct bpf_map_info *info, json_writer= _t *wtr, err =3D 0; break; } - if (!dump_map_elem(fd, key, value, info, btf, wtr)) + if (!dump_map_elem(fd, key, value, info, btf, wtr, cpu_ids)) num_elems++; prev_key =3D key; } @@ -893,6 +905,7 @@ map_dump(int fd, struct bpf_map_info *info, json_writer= _t *wtr, exit_free: free(key); free(value); + free(cpu_ids); free_map_kv_btf(btf); =20 return err; @@ -1035,17 +1048,29 @@ static int do_update(int argc, char **argv) return err; } =20 -static void print_key_value(struct bpf_map_info *info, void *key, - void *value) +static int print_key_value(struct bpf_map_info *info, void *key, + void *value) { json_writer_t *btf_wtr; struct btf *btf; + int *cpu_ids =3D NULL; + int err =3D 0; =20 - if (get_map_kv_btf(info, &btf)) - return; + if (map_is_per_cpu(info->type)) { + int cpu_cnt =3D get_possible_cpu_ids(&cpu_ids); + + if (cpu_cnt < 0) { + err =3D cpu_cnt; + goto out; + } + } + + err =3D get_map_kv_btf(info, &btf); + if (err) + goto out; =20 if (json_output) { - print_entry_json(info, key, value, btf); + print_entry_json(info, key, value, btf, cpu_ids); } else if (btf) { /* if here json_wtr wouldn't have been initialised, * so let's create separate writer for btf @@ -1055,7 +1080,7 @@ static void print_key_value(struct bpf_map_info *info= , void *key, p_info("failed to create json writer for btf. falling back to plain out= put"); free_map_kv_btf(btf); btf =3D NULL; - print_entry_plain(info, key, value); + print_entry_plain(info, key, value, cpu_ids); } else { struct btf_dumper d =3D { .btf =3D btf, @@ -1063,13 +1088,16 @@ static void print_key_value(struct bpf_map_info *in= fo, void *key, .is_plain_text =3D true, }; =20 - do_dump_btf(&d, info, key, value); + do_dump_btf(&d, info, key, value, cpu_ids); jsonw_destroy(&btf_wtr); } } else { - print_entry_plain(info, key, value); + print_entry_plain(info, key, value, cpu_ids); } free_map_kv_btf(btf); +out: + free(cpu_ids); + return err; } =20 static int do_lookup(int argc, char **argv) @@ -1114,7 +1142,7 @@ static int do_lookup(int argc, char **argv) } =20 /* here means bpf_map_lookup_elem() succeeded */ - print_key_value(&info, key, value); + err =3D print_key_value(&info, key, value); =20 exit_free: free(key); @@ -1404,7 +1432,7 @@ static int do_pop_dequeue(int argc, char **argv) goto exit_free; } =20 - print_key_value(&info, key, value); + err =3D print_key_value(&info, key, value); =20 exit_free: free(key); --=20 2.55.0 From nobody Fri Sep 25 00:41:23 2026 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 17CFB4DB540; Fri, 18 Sep 2026 10:22:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789726924; cv=none; b=PB6HEvjZzlEp2e8w1hk1dlGuPgQEJfYpG4oD/VVAmayg75GI9HXZZHU+9R42asrfQ93wGrZ8fgkc/EEaA5cc5b0N8MFYnHA62RFq411yzGWQezsv9AKojqXcYFl/Sv9QycZqwvVuY7f1VJKC+A2/G8nEvcj0UiPeuDqi7kqUSeA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789726924; c=relaxed/simple; bh=IpmTmJPbWCCXzMxRFjlr3hZ39oPe1qTH63DD57wZHbs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ioX9G7W7W4gduSa1AxnsRdGNaf0oA998kX7kMa36KSo2tijGsI+8ZSXnKGRaFKAovkoR3Cks8vAVaVh9VWNanhZ3YYCtVNUndL0J6A4iO0fVEZzniKXksKuqXzRBwR9LZ75G0QiUzn8VR/Tw1KxEmR8lBuhTDNjMWW+RVX2C6Ck= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=FUo00DHF; arc=none smtp.client-ip=117.135.210.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="FUo00DHF" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=sM d6B8rglyGSl5JeaOK6WajqLgMQS2WNO6Tr+IYzHCg=; b=FUo00DHFsWV1nOZAJf EH6Qm5WrLGnH0JWoeGOzFEEJZIDzd2AEUFpJFAQO+q0+/4Hwa4bHW+X/+fKhXb9K oXtjdlcNgbaejGD4+Lawe1Su6gHxAUMusxb+nxogwi4IiE42CeGGZy58m3rnbnn0 eUF7luVMpWH6v1CqWDHl3C4Ts= Received: from localhost (unknown []) by gzga-smtp-mtada-g1-4 (Coremail) with SMTP id _____wD3BxCTEK1qCxUrBg--.58514S2; Fri, 18 Sep 2026 18:21:08 +0800 (CST) From: Hui Su To: qmo@kernel.org Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org, emil@etsalapatis.com, ihor.solodrai@linux.dev, davem@davemloft.net, kuba@kernel.org, horms@kernel.org, Hui Su Subject: [PATCH bpf v2 2/2] bpftool: Fix sparse CPU IDs in prog profile Date: Fri, 18 Sep 2026 19:20:52 +0900 Message-ID: <20260918102052.1247819-3-sh_def@163.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260918102052.1247819-1-sh_def@163.com> References: <20260918102052.1247819-1-sh_def@163.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 X-CM-TRANSID: _____wD3BxCTEK1qCxUrBg--.58514S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxKw48Zw1xWw18ZF4UXrW7twb_yoW3Zryfpa n5GF9xKF1kJF9rKw15Ca1DGF43GryxXr4kZ34vg3yrZFWUXr97JF48WFWrKF1FgrWkZFy7 J3WjkrWxAFWUXFUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zi5l1kUUUUU= X-CM-SenderInfo: xvkbvvri6rljoofrz/xtbC6hQvjmqtEJT8LQAA3N Content-Type: text/plain; charset="utf-8" bpftool prog profile currently treats the number of possible CPUs as both the logical CPU ID range and the stride of the perf event array. That misses valid logical CPUs when the possible CPU mask is sparse, such as 0,2-3, and can use incorrect PERF_EVENT_ARRAY keys. Keep the compact possible CPU count for per-CPU result buffers, while using the actual logical CPU IDs for perf events and the maximum logical CPU ID plus one as the metric stride in the PERF_EVENT_ARRAY. Keep the userspace perf file descriptor index separate from the BPF map key. Do not advance profile_perf_event_cnt for CPUs that return ENODEV, since it tracks opened userspace perf file descriptors rather than event-array slots. Tested: - Built tools/bpf/bpftool successfully on the host. - Booted an arm64 QEMU guest with a patched virt device tree reporting possible=3D0,2-3, present=3D0,2-3, and online=3D0,2-3. - Ran both pre-fix and fixed bpftool with cycles and instructions against a BTF-enabled fentry target. The pre-fix binary faulted in perf_event_alloc(), while the fixed binary reached perf-event setup and reported failure to create the instructions event on CPU 0. QEMU did not provide a usable hardware PMU runtime result, so no profile counts are claimed. Fixes: 47c09d6a9f67 ("bpftool: Introduce "prog profile" command") Link: https://lore.kernel.org/bpf/20260813160858.1042834-3-sh_def@163.com/ Signed-off-by: Hui Su Acked-by: Quentin Monnet --- tools/bpf/bpftool/prog.c | 51 +++++++++++++++-------- tools/bpf/bpftool/skeleton/profiler.bpf.c | 8 ++-- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index a9f730d407a9..bb969ffce453 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -2148,6 +2148,7 @@ struct profile_metric { }; =20 static __u64 profile_total_count; +static int profile_cpu_cnt; =20 #define MAX_NUM_PROFILE_METRICS 4 =20 @@ -2184,7 +2185,7 @@ static int profile_parse_metrics(int argc, char **arg= v) =20 static void profile_read_values(struct profiler_bpf *obj) { - __u32 m, cpu, num_cpu =3D obj->rodata->num_cpu; + __u32 m, cpu, num_cpu =3D profile_cpu_cnt; int reading_map_fd, count_map_fd; __u64 counts[num_cpu]; __u32 key =3D 0; @@ -2349,6 +2350,8 @@ static int profile_tgt_fd =3D -1; static char *profile_tgt_name; static int *profile_perf_events; static int profile_perf_event_cnt; +static int *profile_cpu_ids; +static int profile_cpu_id_span; =20 static void profile_close_perf_events(struct profiler_bpf *obj) { @@ -2358,10 +2361,11 @@ static void profile_close_perf_events(struct profil= er_bpf *obj) close(profile_perf_events[i]); =20 free(profile_perf_events); + profile_perf_events =3D NULL; profile_perf_event_cnt =3D 0; } =20 -static int profile_open_perf_event(int mid, int cpu, int map_fd) +static int profile_open_perf_event(int mid, int cpu, int map_key, int map_= fd) { int pmu_fd; =20 @@ -2371,15 +2375,12 @@ static int profile_open_perf_event(int mid, int cpu= , int map_fd) if (errno =3D=3D ENODEV) { p_info("cpu %d may be offline, skip %s profiling.", cpu, metrics[mid].name); - profile_perf_event_cnt++; return 0; } return -1; } =20 - if (bpf_map_update_elem(map_fd, - &profile_perf_event_cnt, - &pmu_fd, BPF_ANY) || + if (bpf_map_update_elem(map_fd, &map_key, &pmu_fd, BPF_ANY) || ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0)) { close(pmu_fd); return -1; @@ -2391,11 +2392,11 @@ static int profile_open_perf_event(int mid, int cpu= , int map_fd) =20 static int profile_open_perf_events(struct profiler_bpf *obj) { - unsigned int cpu, m; + unsigned int cpu, m, metric_idx =3D 0; int map_fd; =20 profile_perf_events =3D calloc( - obj->rodata->num_cpu * obj->rodata->num_metric, sizeof(int)); + profile_cpu_cnt * obj->rodata->num_metric, sizeof(int)); if (!profile_perf_events) { p_err("failed to allocate memory for perf_event array: %s", strerror(errno)); @@ -2410,13 +2411,17 @@ static int profile_open_perf_events(struct profiler= _bpf *obj) for (m =3D 0; m < ARRAY_SIZE(metrics); m++) { if (!metrics[m].selected) continue; - for (cpu =3D 0; cpu < obj->rodata->num_cpu; cpu++) { - if (profile_open_perf_event(m, cpu, map_fd)) { - p_err("failed to create event %s on cpu %u", - metrics[m].name, cpu); + for (cpu =3D 0; cpu < (unsigned int)profile_cpu_cnt; cpu++) { + int cpu_id =3D profile_cpu_ids[cpu]; + int map_key =3D cpu_id + metric_idx * profile_cpu_id_span; + + if (profile_open_perf_event(m, cpu_id, map_key, map_fd)) { + p_err("failed to create event %s on cpu %d", + metrics[m].name, cpu_id); return -1; } } + metric_idx++; } return 0; } @@ -2430,6 +2435,10 @@ static void profile_print_and_cleanup(void) =20 close(profile_tgt_fd); free(profile_tgt_name); + free(profile_cpu_ids); + profile_cpu_ids =3D NULL; + profile_cpu_cnt =3D 0; + profile_cpu_id_span =3D 0; } =20 static void int_exit(int signo) @@ -2440,7 +2449,8 @@ static void int_exit(int signo) =20 static int do_profile(int argc, char **argv) { - int num_metric, num_cpu, err =3D -1; + int num_metric, err =3D -1; + int *cpu_ids =3D NULL; struct bpf_program *prog; unsigned long duration; char *endptr; @@ -2471,11 +2481,13 @@ static int do_profile(int argc, char **argv) if (num_metric <=3D 0) goto out; =20 - num_cpu =3D libbpf_num_possible_cpus(); - if (num_cpu <=3D 0) { + profile_cpu_cnt =3D get_possible_cpu_ids(&cpu_ids); + if (profile_cpu_cnt <=3D 0) { p_err("failed to identify number of CPUs"); goto out; } + profile_cpu_ids =3D cpu_ids; + profile_cpu_id_span =3D cpu_ids[profile_cpu_cnt - 1] + 1; =20 profile_obj =3D profiler_bpf__open(); if (!profile_obj) { @@ -2483,11 +2495,12 @@ static int do_profile(int argc, char **argv) goto out; } =20 - profile_obj->rodata->num_cpu =3D num_cpu; profile_obj->rodata->num_metric =3D num_metric; + profile_obj->rodata->cpu_id_span =3D profile_cpu_id_span; =20 /* adjust map sizes */ - bpf_map__set_max_entries(profile_obj->maps.events, num_metric * num_cpu); + bpf_map__set_max_entries(profile_obj->maps.events, + num_metric * profile_cpu_id_span); bpf_map__set_max_entries(profile_obj->maps.fentry_readings, num_metric); bpf_map__set_max_entries(profile_obj->maps.accum_readings, num_metric); bpf_map__set_max_entries(profile_obj->maps.counts, 1); @@ -2534,6 +2547,10 @@ static int do_profile(int argc, char **argv) profiler_bpf__destroy(profile_obj); close(profile_tgt_fd); free(profile_tgt_name); + free(profile_cpu_ids); + profile_cpu_ids =3D NULL; + profile_cpu_cnt =3D 0; + profile_cpu_id_span =3D 0; return err; } =20 diff --git a/tools/bpf/bpftool/skeleton/profiler.bpf.c b/tools/bpf/bpftool/= skeleton/profiler.bpf.c index f48c783cb9f7..2504c3a65bb9 100644 --- a/tools/bpf/bpftool/skeleton/profiler.bpf.c +++ b/tools/bpf/bpftool/skeleton/profiler.bpf.c @@ -10,7 +10,7 @@ struct bpf_perf_event_value___local { __u64 running; } __attribute__((preserve_access_index)); =20 -/* map of perf event fds, num_cpu * num_metric entries */ +/* map of perf event fds, cpu_id_span * num_metric entries */ struct { __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); __uint(key_size, sizeof(u32)); @@ -38,8 +38,8 @@ struct { __uint(value_size, sizeof(u64)); } counts SEC(".maps"); =20 -const volatile __u32 num_cpu =3D 1; const volatile __u32 num_metric =3D 1; +const volatile __u32 cpu_id_span =3D 1; #define MAX_NUM_METRICS 4 =20 SEC("fentry/XXX") @@ -67,7 +67,7 @@ int BPF_PROG(fentry_XXX) if (err) return 0; *(ptrs[i]) =3D reading; - key +=3D num_cpu; + key +=3D cpu_id_span; } =20 return 0; @@ -107,7 +107,7 @@ int BPF_PROG(fexit_XXX) =20 /* read all events before updating the maps, to reduce error */ for (i =3D 0; i < num_metric && i < MAX_NUM_METRICS; i++) { - err =3D bpf_perf_event_read_value(&events, cpu + i * num_cpu, + err =3D bpf_perf_event_read_value(&events, cpu + i * cpu_id_span, (void *)(readings + i), sizeof(*readings)); if (err) --=20 2.55.0