From nobody Fri Dec 19 07:50:31 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C6F48C54EE9 for ; Tue, 13 Sep 2022 14:25:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233683AbiIMOZv (ORCPT ); Tue, 13 Sep 2022 10:25:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233745AbiIMOYK (ORCPT ); Tue, 13 Sep 2022 10:24:10 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17F7166A65; Tue, 13 Sep 2022 07:16:01 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 7D8C7614CB; Tue, 13 Sep 2022 14:14:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6F52AC433D7; Tue, 13 Sep 2022 14:14:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1663078471; bh=sQhboYEXUV6NaHCi0b8UA+XJONRWpkquPv65uDiSKwQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=072L4vkNsd2mLgdq7mux4PqUenz7ETxtd4wahYxgu8IGcMC6AXmsQR04NXDUD1c4h ZeWkj2Yx/8xnQenkBcLFHXOa7OaiBcimxTZRIAYooPAVhUOTujKH/XGUS2N6EGRCQh wTCJ4eXk6G0hmMTKKy71NKJA09Gf1CUgtjFf57u8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, =?UTF-8?q?Tom=C3=A1=C5=A1=20Trnka?= , Adrian Hunter , Jiri Olsa , Ian Rogers , Ingo Molnar , Namhyung Kim , Peter Zijlstra , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH 5.19 152/192] libperf evlist: Fix per-thread mmaps for multi-threaded targets Date: Tue, 13 Sep 2022 16:04:18 +0200 Message-Id: <20220913140417.598141711@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220913140410.043243217@linuxfoundation.org> References: <20220913140410.043243217@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Adrian Hunter [ Upstream commit 7864d8f7c088aad988c44c631f1ceed9179cf2cf ] The offending commit removed mmap_per_thread(), which did not consider the different set-output rules for per-thread mmaps i.e. in the per-thread case set-output is used for file descriptors of the same thread not the same cpu. This was not immediately noticed because it only happens with multi-threaded targets and we do not have a test for that yet. Reinstate mmap_per_thread() expanding it to cover also system-wide per-cpu events i.e. to continue to allow the mixing of per-thread and per-cpu mmaps. Debug messages (with -vv) show the file descriptors that are opened with sys_perf_event_open. New debug messages are added (needs -vvv) that show also which file descriptors are mmapped and which are redirected with set-output. In the per-cpu case (cpu !=3D -1) file descriptors for the same CPU are set-output to the first file descriptor for that CPU. In the per-thread case (cpu =3D=3D -1) file descriptors for the same thread= are set-output to the first file descriptor for that thread. Example (process 17489 has 2 threads): Before (but with new debug prints): $ perf record --no-bpf-event -vvv --per-thread -p 17489 sys_perf_event_open: pid 17489 cpu -1 group_fd -1 flags 0x8 =3D 5 sys_perf_event_open: pid 17490 cpu -1 group_fd -1 flags 0x8 =3D 6 libperf: idx 0: mmapping fd 5 libperf: idx 0: set output fd 6 -> 5 failed to mmap with 22 (Invalid argument) After: $ perf record --no-bpf-event -vvv --per-thread -p 17489 sys_perf_event_open: pid 17489 cpu -1 group_fd -1 flags 0x8 =3D 5 sys_perf_event_open: pid 17490 cpu -1 group_fd -1 flags 0x8 =3D 6 libperf: mmap_per_thread: nr cpu values (may include -1) 1 nr threads 2 libperf: idx 0: mmapping fd 5 libperf: idx 1: mmapping fd 6 [ perf record: Woken up 2 times to write data ] [ perf record: Captured and wrote 0.018 MB perf.data (15 samples) ] Per-cpu example (process 20341 has 2 threads, same as above): $ perf record --no-bpf-event -vvv -p 20341 sys_perf_event_open: pid 20341 cpu 0 group_fd -1 flags 0x8 =3D 5 sys_perf_event_open: pid 20342 cpu 0 group_fd -1 flags 0x8 =3D 6 sys_perf_event_open: pid 20341 cpu 1 group_fd -1 flags 0x8 =3D 7 sys_perf_event_open: pid 20342 cpu 1 group_fd -1 flags 0x8 =3D 8 sys_perf_event_open: pid 20341 cpu 2 group_fd -1 flags 0x8 =3D 9 sys_perf_event_open: pid 20342 cpu 2 group_fd -1 flags 0x8 =3D 10 sys_perf_event_open: pid 20341 cpu 3 group_fd -1 flags 0x8 =3D 11 sys_perf_event_open: pid 20342 cpu 3 group_fd -1 flags 0x8 =3D 12 sys_perf_event_open: pid 20341 cpu 4 group_fd -1 flags 0x8 =3D 13 sys_perf_event_open: pid 20342 cpu 4 group_fd -1 flags 0x8 =3D 14 sys_perf_event_open: pid 20341 cpu 5 group_fd -1 flags 0x8 =3D 15 sys_perf_event_open: pid 20342 cpu 5 group_fd -1 flags 0x8 =3D 16 sys_perf_event_open: pid 20341 cpu 6 group_fd -1 flags 0x8 =3D 17 sys_perf_event_open: pid 20342 cpu 6 group_fd -1 flags 0x8 =3D 18 sys_perf_event_open: pid 20341 cpu 7 group_fd -1 flags 0x8 =3D 19 sys_perf_event_open: pid 20342 cpu 7 group_fd -1 flags 0x8 =3D 20 libperf: mmap_per_cpu: nr cpu values 8 nr threads 2 libperf: idx 0: mmapping fd 5 libperf: idx 0: set output fd 6 -> 5 libperf: idx 1: mmapping fd 7 libperf: idx 1: set output fd 8 -> 7 libperf: idx 2: mmapping fd 9 libperf: idx 2: set output fd 10 -> 9 libperf: idx 3: mmapping fd 11 libperf: idx 3: set output fd 12 -> 11 libperf: idx 4: mmapping fd 13 libperf: idx 4: set output fd 14 -> 13 libperf: idx 5: mmapping fd 15 libperf: idx 5: set output fd 16 -> 15 libperf: idx 6: mmapping fd 17 libperf: idx 6: set output fd 18 -> 17 libperf: idx 7: mmapping fd 19 libperf: idx 7: set output fd 20 -> 19 [ perf record: Woken up 7 times to write data ] [ perf record: Captured and wrote 0.020 MB perf.data (17 samples) ] Fixes: ae4f8ae16a078964 ("libperf evlist: Allow mixing per-thread and per-c= pu mmaps") Reported-by: Tom=C3=A1=C5=A1 Trnka Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=3D216441 Signed-off-by: Adrian Hunter Acked-by: Jiri Olsa Cc: Adrian Hunter Cc: Ian Rogers Cc: Ingo Molnar Cc: Namhyung Kim Cc: Peter Zijlstra Link: https://lore.kernel.org/r/20220905114209.8389-1-adrian.hunter@intel.c= om Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/lib/perf/evlist.c | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tools/lib/perf/evlist.c b/tools/lib/perf/evlist.c index e6c98a6e3908e..6b1bafe267a42 100644 --- a/tools/lib/perf/evlist.c +++ b/tools/lib/perf/evlist.c @@ -486,6 +486,7 @@ mmap_per_evsel(struct perf_evlist *evlist, struct perf_= evlist_mmap_ops *ops, if (ops->idx) ops->idx(evlist, evsel, mp, idx); =20 + pr_debug("idx %d: mmapping fd %d\n", idx, *output); if (ops->mmap(map, mp, *output, evlist_cpu) < 0) return -1; =20 @@ -494,6 +495,7 @@ mmap_per_evsel(struct perf_evlist *evlist, struct perf_= evlist_mmap_ops *ops, if (!idx) perf_evlist__set_mmap_first(evlist, map, overwrite); } else { + pr_debug("idx %d: set output fd %d -> %d\n", idx, fd, *output); if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) !=3D 0) return -1; =20 @@ -519,6 +521,48 @@ mmap_per_evsel(struct perf_evlist *evlist, struct perf= _evlist_mmap_ops *ops, return 0; } =20 +static int +mmap_per_thread(struct perf_evlist *evlist, struct perf_evlist_mmap_ops *o= ps, + struct perf_mmap_param *mp) +{ + int nr_threads =3D perf_thread_map__nr(evlist->threads); + int nr_cpus =3D perf_cpu_map__nr(evlist->all_cpus); + int cpu, thread, idx =3D 0; + int nr_mmaps =3D 0; + + pr_debug("%s: nr cpu values (may include -1) %d nr threads %d\n", + __func__, nr_cpus, nr_threads); + + /* per-thread mmaps */ + for (thread =3D 0; thread < nr_threads; thread++, idx++) { + int output =3D -1; + int output_overwrite =3D -1; + + if (mmap_per_evsel(evlist, ops, idx, mp, 0, thread, &output, + &output_overwrite, &nr_mmaps)) + goto out_unmap; + } + + /* system-wide mmaps i.e. per-cpu */ + for (cpu =3D 1; cpu < nr_cpus; cpu++, idx++) { + int output =3D -1; + int output_overwrite =3D -1; + + if (mmap_per_evsel(evlist, ops, idx, mp, cpu, 0, &output, + &output_overwrite, &nr_mmaps)) + goto out_unmap; + } + + if (nr_mmaps !=3D evlist->nr_mmaps) + pr_err("Miscounted nr_mmaps %d vs %d\n", nr_mmaps, evlist->nr_mmaps); + + return 0; + +out_unmap: + perf_evlist__munmap(evlist); + return -1; +} + static int mmap_per_cpu(struct perf_evlist *evlist, struct perf_evlist_mmap_ops *ops, struct perf_mmap_param *mp) @@ -528,6 +572,8 @@ mmap_per_cpu(struct perf_evlist *evlist, struct perf_ev= list_mmap_ops *ops, int nr_mmaps =3D 0; int cpu, thread; =20 + pr_debug("%s: nr cpu values %d nr threads %d\n", __func__, nr_cpus, nr_th= reads); + for (cpu =3D 0; cpu < nr_cpus; cpu++) { int output =3D -1; int output_overwrite =3D -1; @@ -569,6 +615,7 @@ int perf_evlist__mmap_ops(struct perf_evlist *evlist, struct perf_evlist_mmap_ops *ops, struct perf_mmap_param *mp) { + const struct perf_cpu_map *cpus =3D evlist->all_cpus; struct perf_evsel *evsel; =20 if (!ops || !ops->get || !ops->mmap) @@ -588,6 +635,9 @@ int perf_evlist__mmap_ops(struct perf_evlist *evlist, if (evlist->pollfd.entries =3D=3D NULL && perf_evlist__alloc_pollfd(evlis= t) < 0) return -ENOMEM; =20 + if (perf_cpu_map__empty(cpus)) + return mmap_per_thread(evlist, ops, mp); + return mmap_per_cpu(evlist, ops, mp); } =20 --=20 2.35.1