From nobody Fri Dec 19 16:58:41 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 4BE9F42AA2 for ; Thu, 5 Sep 2024 03:10:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725505829; cv=none; b=kM0Igw9FAztgZxlNDW0wi0G8rUySxEe+KDC1vlWIklwpAvqi5mjUkntliN6eCjJ1q5fRcrUd/TuqcOjeGAP8+INxqzwOYPfORvdaUJwlm6JrTVuwjRLwLM449tpbuRmWEfD9ZCJaEHli+FoQXZZBEqGMuKB4RdIuQhZDCpKX3zA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725505829; c=relaxed/simple; bh=4D5oDBmYspBeextM49eoAfWIHIUc/gszGzj9mXt4t/8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CYAo/sRZAHSHHjnpeovaZj+zWMO2zXwPawBpyaW3whomSGp6XPi3vX457LX9n2elQErvP65iJDPOImpwtTRr/56+yBrL4GvkB7EkXEaMNE/Iqztf72gWppGQtzagbpUAjzErGdckjAzBqzS2rZ5AnGHzkTtc23ik/DJQyw33DBU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=G/SlAUUi; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="G/SlAUUi" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7AECFC4CEC6; Thu, 5 Sep 2024 03:10:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725505828; bh=4D5oDBmYspBeextM49eoAfWIHIUc/gszGzj9mXt4t/8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=G/SlAUUi/TTuMrhX91lGUdFlk+KsTT11rC9G38RpLLchDPLbAh9LlSxDHtmiOX9lt 1kYGNXi+TtfGIewtwUvnSM/Reew2JO1ql9ArUK+KB33zfRdm3nJHhhYvqzbQsLOans KxI2O2Megk4SRsupvHGtWK6qYYv7Z5l62wTrBYHNFCDa3/X3Bg1nOk+BWUihJ2QXii XMyBSmauJ1XOls8E4Kl6p8xKWwvY5jamBvdk/VruXu8TXWfy4X1WH41GO5Li1Bgroe RToNY0w5BM6MYgNjnxt+6iIu5+O9ghOBgfIA5yQGZTGtWr/qfd9bQA9d+YP9E31Rgx LNzaAzI0t4N6g== From: Namhyung Kim To: Peter Zijlstra , Ingo Molnar Cc: Kan Liang , Mark Rutland , Alexander Shishkin , Arnaldo Carvalho de Melo , LKML , Stephane Eranian , Ravi Bangoria Subject: [PATCH 1/5] perf/core: Add PERF_FORMAT_DROPPED Date: Wed, 4 Sep 2024 20:10:23 -0700 Message-ID: <20240905031027.2567913-2-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240905031027.2567913-1-namhyung@kernel.org> References: <20240905031027.2567913-1-namhyung@kernel.org> 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 a perf_event is dropped due to some kind of (SW-based) filter, it won't generate sample data. For example, software events drops samples when it doesn't match to privilege from exclude_{user,kernel}. In order to account such dropped samples, add a new counter in the perf_event, and let users can read(2) the number with the new PERF_FORMAT_DROPPED like the lost sample count. Signed-off-by: Namhyung Kim --- include/linux/perf_event.h | 1 + include/uapi/linux/perf_event.h | 5 ++++- kernel/events/core.c | 12 ++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 701549967c1854e3..955d39543398afb0 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -809,6 +809,7 @@ struct perf_event { u64 id; =20 atomic64_t lost_samples; + atomic64_t dropped_samples; =20 u64 (*clock)(void); perf_overflow_handler_t overflow_handler; diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_even= t.h index 58daf6156fd0ee66..6f19d4f7482389b7 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -347,6 +347,7 @@ enum { * { u64 time_running; } && PERF_FORMAT_TOTAL_TIME_RUNNING * { u64 id; } && PERF_FORMAT_ID * { u64 lost; } && PERF_FORMAT_LOST + * { u64 dropped; } && PERF_FORMAT_DROPPED * } && !PERF_FORMAT_GROUP * * { u64 nr; @@ -355,6 +356,7 @@ enum { * { u64 value; * { u64 id; } && PERF_FORMAT_ID * { u64 lost; } && PERF_FORMAT_LOST + * { u64 dropped; } && PERF_FORMAT_DROPPED * } cntr[nr]; * } && PERF_FORMAT_GROUP * }; @@ -365,8 +367,9 @@ enum perf_event_read_format { PERF_FORMAT_ID =3D 1U << 2, PERF_FORMAT_GROUP =3D 1U << 3, PERF_FORMAT_LOST =3D 1U << 4, + PERF_FORMAT_DROPPED =3D 1U << 5, =20 - PERF_FORMAT_MAX =3D 1U << 5, /* non-ABI */ + PERF_FORMAT_MAX =3D 1U << 6, /* non-ABI */ }; =20 #define PERF_ATTR_SIZE_VER0 64 /* sizeof first published struct */ diff --git a/kernel/events/core.c b/kernel/events/core.c index c6a720f412258079..4d72538628ee62f4 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -5679,6 +5679,8 @@ static int __perf_read_group_add(struct perf_event *l= eader, values[n++] =3D primary_event_id(leader); if (read_format & PERF_FORMAT_LOST) values[n++] =3D atomic64_read(&leader->lost_samples); + if (read_format & PERF_FORMAT_DROPPED) + values[n++] =3D atomic64_read(&leader->dropped_samples); =20 for_each_sibling_event(sub, leader) { values[n++] +=3D perf_event_count(sub, false); @@ -5686,6 +5688,8 @@ static int __perf_read_group_add(struct perf_event *l= eader, values[n++] =3D primary_event_id(sub); if (read_format & PERF_FORMAT_LOST) values[n++] =3D atomic64_read(&sub->lost_samples); + if (read_format & PERF_FORMAT_DROPPED) + values[n++] =3D atomic64_read(&sub->dropped_samples); } =20 unlock: @@ -5751,6 +5755,8 @@ static int perf_read_one(struct perf_event *event, values[n++] =3D primary_event_id(event); if (read_format & PERF_FORMAT_LOST) values[n++] =3D atomic64_read(&event->lost_samples); + if (read_format & PERF_FORMAT_DROPPED) + values[n++] =3D atomic64_read(&event->dropped_samples); =20 if (copy_to_user(buf, values, n * sizeof(u64))) return -EFAULT; @@ -7348,6 +7354,8 @@ static void perf_output_read_one(struct perf_output_h= andle *handle, values[n++] =3D primary_event_id(event); if (read_format & PERF_FORMAT_LOST) values[n++] =3D atomic64_read(&event->lost_samples); + if (read_format & PERF_FORMAT_DROPPED) + values[n++] =3D atomic64_read(&event->dropped_samples); =20 __output_copy(handle, values, n * sizeof(u64)); } @@ -7386,6 +7394,8 @@ static void perf_output_read_group(struct perf_output= _handle *handle, values[n++] =3D primary_event_id(leader); if (read_format & PERF_FORMAT_LOST) values[n++] =3D atomic64_read(&leader->lost_samples); + if (read_format & PERF_FORMAT_DROPPED) + values[n++] =3D atomic64_read(&leader->dropped_samples); =20 __output_copy(handle, values, n * sizeof(u64)); =20 @@ -7401,6 +7411,8 @@ static void perf_output_read_group(struct perf_output= _handle *handle, values[n++] =3D primary_event_id(sub); if (read_format & PERF_FORMAT_LOST) values[n++] =3D atomic64_read(&sub->lost_samples); + if (read_format & PERF_FORMAT_DROPPED) + values[n++] =3D atomic64_read(&sub->dropped_samples); =20 __output_copy(handle, values, n * sizeof(u64)); } --=20 2.46.0.469.g59c65b2a67-goog From nobody Fri Dec 19 16:58:41 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 04937145B26; Thu, 5 Sep 2024 03:10:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725505830; cv=none; b=Jtu+cw49ZJ58aDCeDNw3asGmqep3FpUP5h9Ug1BJFJDnsBY2CanskKbYnhR4i2wfK3IhB30+FXqsLfM2KB1vE63BG8cqT/NSXlGMaEGZyAusWy8xK/RD9fHbNC/mXilTuJ4ZpalpTSk5SRogRsiLD80xU/IE/k47fApM0YbZ/+o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725505830; c=relaxed/simple; bh=hCFzTizAoGoDsVUGq3EAWMcM65X29QRH/OHHwFMG7sA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bQh5UsDsg38x9alMY4FUz+Pu+J22DglTAfrvHOseUTchOCleC9LQ64hkML6JpkDjfiCTChPm9jLh+NVIRaS48tTMJPDjbo1KyfA+ABNoQ8RhV+1n0D8DGDurwDnaC/nRrTt8HmzatYHmZII/6165XaN5tap20TBv08tAH9Xdcw4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=u8ys7Ws8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="u8ys7Ws8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0105AC4CEC2; Thu, 5 Sep 2024 03:10:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725505829; bh=hCFzTizAoGoDsVUGq3EAWMcM65X29QRH/OHHwFMG7sA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=u8ys7Ws8jELO5HmNyglvZvTDDEciClaaVAuGb3ChDbtCt0wlBHxoeNJA0zE8W+BWR g/TDXNMEIqwELQBILJ30xJ7TfCIzeZV3mW7JCj73LGczxQxOcz5L+hdXd7DswIUPp3 dJnQrzHOh/oRvKMamVsERZwbQwG3Sh7Nrz8IRSRmttD151vj1ntjArnCtlijOcGZSt FBKcy0pDCFdTIsEvpeNUE+88AZTzBUVLbvusiqTAieIu7MPUtGFn0N0L7c51mziyD5 UrLRtc8XpcKowRc02PjGIWf110u5F9k1xu3lJpgHcvSqU+dawQlhOrolnLFW3WiFZG 20bWWx97cSMbA== From: Namhyung Kim To: Peter Zijlstra , Ingo Molnar Cc: Kan Liang , Mark Rutland , Alexander Shishkin , Arnaldo Carvalho de Melo , LKML , Stephane Eranian , Ravi Bangoria , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Thomas Richter , linux-s390@vger.kernel.org Subject: [PATCH 2/5] perf/core: Export perf_exclude_event() Date: Wed, 4 Sep 2024 20:10:24 -0700 Message-ID: <20240905031027.2567913-3-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240905031027.2567913-1-namhyung@kernel.org> References: <20240905031027.2567913-1-namhyung@kernel.org> 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" And increase the dropped_sample count when it returns 1. Now it can track how many samples are dropped due to the privilege filters in software events. While at it, rename the same function in s390 cpum_sf PMU and also count the dropped samples. Cc: Heiko Carstens Cc: Vasily Gorbik Cc: Alexander Gordeev Cc: Christian Borntraeger Cc: Sven Schnelle Cc: Thomas Richter Cc: linux-s390@vger.kernel.org Signed-off-by: Namhyung Kim --- arch/s390/kernel/perf_cpum_sf.c | 8 +++++--- include/linux/perf_event.h | 6 ++++++ kernel/events/core.c | 11 +++++++---- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/arch/s390/kernel/perf_cpum_sf.c b/arch/s390/kernel/perf_cpum_s= f.c index 736c1d9632dd554f..f663530ae1f6ba5d 100644 --- a/arch/s390/kernel/perf_cpum_sf.c +++ b/arch/s390/kernel/perf_cpum_sf.c @@ -1074,7 +1074,7 @@ static void cpumsf_pmu_disable(struct pmu *pmu) cpuhw->flags &=3D ~PMU_F_ENABLED; } =20 -/* perf_exclude_event() - Filter event +/* perf_event_exclude() - Filter event * @event: The perf event * @regs: pt_regs structure * @sde_regs: Sample-data-entry (sde) regs structure @@ -1083,7 +1083,7 @@ static void cpumsf_pmu_disable(struct pmu *pmu) * * Return non-zero if the event shall be excluded. */ -static int perf_exclude_event(struct perf_event *event, struct pt_regs *re= gs, +static int perf_event_exclude(struct perf_event *event, struct pt_regs *re= gs, struct perf_sf_sde_regs *sde_regs) { if (event->attr.exclude_user && user_mode(regs)) @@ -1166,8 +1166,10 @@ static int perf_push_sample(struct perf_event *event, data.tid_entry.pid =3D basic->hpp & LPP_PID_MASK; =20 overflow =3D 0; - if (perf_exclude_event(event, ®s, sde_regs)) + if (perf_event_exclude(event, ®s, sde_regs)) { + atomic64_inc(&event->dropped_samples); goto out; + } if (perf_event_overflow(event, &data, ®s)) { overflow =3D 1; event->pmu->stop(event, 0); diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 955d39543398afb0..aaa4bc582d2f1d4e 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1634,6 +1634,8 @@ static inline int perf_allow_tracepoint(struct perf_e= vent_attr *attr) return security_perf_event_open(attr, PERF_SECURITY_TRACEPOINT); } =20 +extern int perf_exclude_event(struct perf_event *event, struct pt_regs *re= gs); + extern void perf_event_init(void); extern void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, struct pt_regs *regs, @@ -1817,6 +1819,10 @@ static inline u64 perf_event_pause(struct perf_event= *event, bool reset) { return 0; } +static inline int perf_exclude_event(struct perf_event *event, struct pt_r= egs *regs) +{ + return 0; +} #endif =20 #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL) diff --git a/kernel/events/core.c b/kernel/events/core.c index 4d72538628ee62f4..8250e76f63358689 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9978,18 +9978,21 @@ static void perf_swevent_event(struct perf_event *e= vent, u64 nr, perf_swevent_overflow(event, 0, data, regs); } =20 -static int perf_exclude_event(struct perf_event *event, - struct pt_regs *regs) +int perf_exclude_event(struct perf_event *event, struct pt_regs *regs) { if (event->hw.state & PERF_HES_STOPPED) return 1; =20 if (regs) { - if (event->attr.exclude_user && user_mode(regs)) + if (event->attr.exclude_user && user_mode(regs)) { + atomic64_inc(&event->dropped_samples); return 1; + } =20 - if (event->attr.exclude_kernel && !user_mode(regs)) + if (event->attr.exclude_kernel && !user_mode(regs)) { + atomic64_inc(&event->dropped_samples); return 1; + } } =20 return 0; --=20 2.46.0.469.g59c65b2a67-goog From nobody Fri Dec 19 16:58:41 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 A7703149E0E; Thu, 5 Sep 2024 03:10:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725505831; cv=none; b=RKkseVY+5nXFmTVnNTp1Yfe+QiJXWCqjR6Hfdn14MbyOEEUEr6g0P01ECwLhJq178zZLRblIAgzuy1WOvnLvvTA5gY0YgQEJD0cDx9hPhqwa02WRNMwMeLzq2ycaKqh0raanEYV5OuB/LIGyGsqSgtmi1ROAjDmxHhOdsCdiDh0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725505831; c=relaxed/simple; bh=uqzF4+T6nDeU2anut/u2vTsVtQcSdsdNORWq/SfW8kU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nxlbQbRyeKFTd/tRKdwtYtiXQMIUWV34H6mhn1QO7PUXA7gPty4LEVY3kGHi9Pir0QzUQKSsd3etmmps8Jxw0Xbf2GMztGIf8s9nXbPjrUcKv2EBnKE+E7XIMudL/Mkr9uRCJ9BVjHBNxmvdCoitN/kcyMxcQbh1lo8vf4TbG48= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hB1IBoEz; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hB1IBoEz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AA106C4CEC9; Thu, 5 Sep 2024 03:10:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725505830; bh=uqzF4+T6nDeU2anut/u2vTsVtQcSdsdNORWq/SfW8kU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hB1IBoEzok+S2QmTY6Q7m3Yukj2kUxViVsaPCsxC4VtET2+3WHhAD3gs13HmjhPJs KU1BjmW8ISgESz0te7/Ec1wjcALuSQBA1dXJTLZwYfODYPjZN2soesDVrwl+aqtGqq 3+5F1o+Lw3/1+xjTnNsz6sGFbEIwkzLIRqCn1OU4FHbuFtZj3+xw/EIUSyB009RVUW 77TiRbznY3/1UcaOrz6zMqu5TLxBr/DkdZL6sKeouO4xEaADwAoWNhqylxbniLAc63 7JUuvL1kg/v4hKT59iyHz75RMNj96bGKDj0i4ojFmXWVCk2pwusfclNjKPUDAMsgWN C0taGYIFltZ1w== From: Namhyung Kim To: Peter Zijlstra , Ingo Molnar Cc: Kan Liang , Mark Rutland , Alexander Shishkin , Arnaldo Carvalho de Melo , LKML , Stephane Eranian , Ravi Bangoria , Alexei Starovoitov , Andrii Nakryiko , Song Liu , Kyle Huey , bpf@vger.kernel.org Subject: [PATCH 3/5] perf/core: Account dropped samples from BPF Date: Wed, 4 Sep 2024 20:10:25 -0700 Message-ID: <20240905031027.2567913-4-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240905031027.2567913-1-namhyung@kernel.org> References: <20240905031027.2567913-1-namhyung@kernel.org> 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" Like in the software events, the BPF overflow handler can drop samples by returning 0. Let's count the dropped samples here too. Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Song Liu Cc: Kyle Huey Cc: bpf@vger.kernel.org Signed-off-by: Namhyung Kim --- kernel/events/core.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 8250e76f63358689..ba1f6b51ea26db5b 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9808,8 +9808,10 @@ static int __perf_event_overflow(struct perf_event *= event, =20 ret =3D __perf_event_account_interrupt(event, throttle); =20 - if (event->prog && !bpf_overflow_handler(event, data, regs)) + if (event->prog && !bpf_overflow_handler(event, data, regs)) { + atomic64_inc(&event->dropped_samples); return ret; + } =20 /* * XXX event_limit might not quite work as expected on inherited --=20 2.46.0.469.g59c65b2a67-goog From nobody Fri Dec 19 16:58:41 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 E8AE014D29B for ; Thu, 5 Sep 2024 03:10:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725505832; cv=none; b=aILGHug+4JhWtTfYHvE3w5FKfKoFx9No9mePMCrJcEyJKP8JvE5ks4PXkwHn6qDXrzTFzvUMen3Sd9ThxmaYA73bScsHXuHCnI/ApYAPQGhbK0/jQxQKfg4ORiioP6BcqzrFdaHMrHssKY6w21d9PGj/AFgQ5kuOXZ44f/1A7R4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725505832; c=relaxed/simple; bh=aziyQ829/wguwAKETpVg+pRPOujyMEAw4EHfDvYu2+A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EEdRiDg4ybPWqy29iZ65txJ3MC7fjO6BA+IQaW0lJEho/jMqADw1dk9RXBBbLuMYsXOOSpwScDTKp5W9vnbf77PmxR8/LBPvwllCzm01zEYt6ppJgqqGik8gCEQZZi7e8lJcQA0mek3Jv5UhPm6w6LNPACUm5rF8fPpffSRfa1M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mD7dO11t; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="mD7dO11t" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5A766C4CEC2; Thu, 5 Sep 2024 03:10:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725505830; bh=aziyQ829/wguwAKETpVg+pRPOujyMEAw4EHfDvYu2+A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mD7dO11tKbOKYa8rLno4b/Crd5G/0t/zA0m4V1HbuuyqSnJjEHaj432FJyQzSl4Th XlJvlSa/eHf7EWGOJ05u9teTfKfNVRjVKrSYlpRmMbDFRqW2LninIBueVH4liyHXNA +afXGYrBbO/gIlL9QoN163JckmbxYZu9pIyNopNKDTFHxPMOqwwy63NkF2G6R+bPvZ 9yl5sM0AtaLm9hIGQQqQSblCri+P2KLA7GPiFxGs6123EpPo8c0wUGuC85PYsJlxtS BLPU76BDrJhhVUakoOoJap2YCvl1Ttm1wYDrg9/A+ltybTgdWssj2Ug4lpXiNlOem6 4n+Yl1JbGTyXA== From: Namhyung Kim To: Peter Zijlstra , Ingo Molnar Cc: Kan Liang , Mark Rutland , Alexander Shishkin , Arnaldo Carvalho de Melo , LKML , Stephane Eranian , Ravi Bangoria , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Naveen N Rao , Kajol Jain , Athira Rajeev , linuxppc-dev@lists.ozlabs.org Subject: [PATCH 4/5] perf/powerpc: Count dropped samples in core-book3s PMU Date: Wed, 4 Sep 2024 20:10:26 -0700 Message-ID: <20240905031027.2567913-5-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240905031027.2567913-1-namhyung@kernel.org> References: <20240905031027.2567913-1-namhyung@kernel.org> 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" Due to the hardware limitation, sometimes it could sample kernel address while attr.exclude_kernel is set. In that case it silently drops the sample. Let's count that case in the new dropped_samples counter. Cc: Michael Ellerman Cc: Nicholas Piggin Cc: Christophe Leroy Cc: Naveen N Rao Cc: Kajol Jain Cc: Athira Rajeev Cc: linuxppc-dev@lists.ozlabs.org Signed-off-by: Namhyung Kim Reviewed-by: Madhavan Srinivasan --- arch/powerpc/perf/core-book3s.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/powerpc/perf/core-book3s.c b/arch/powerpc/perf/core-book3= s.c index 42867469752d73cf..553e288b9f113836 100644 --- a/arch/powerpc/perf/core-book3s.c +++ b/arch/powerpc/perf/core-book3s.c @@ -2287,8 +2287,10 @@ static void record_and_restart(struct perf_event *ev= ent, unsigned long val, */ if (event->attr.exclude_kernel && (event->attr.sample_type & PERF_SAMPLE_IP) && - is_kernel_addr(mfspr(SPRN_SIAR))) + is_kernel_addr(mfspr(SPRN_SIAR))) { + atomic64_inc(&event->dropped_samples); record =3D 0; + } =20 /* * Finally record data if requested. --=20 2.46.0.469.g59c65b2a67-goog From nobody Fri Dec 19 16:58:41 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 E62B714C59A for ; Thu, 5 Sep 2024 03:10:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725505832; cv=none; b=gR3aBdK634QYVdetPHDXBqw8WPPubvmxXH22TpXJwSINSjQdtseSlZj7B+uaXjdlh9c2kLp904pkX37xF9avayszpQL+3xB5x+UmwJnIqHJwdsWr6NxCmUjupbaGfD9sKUCI83feqF9PkRRZ9PMCst9Usf4ojkBZq/GKjCCld4Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725505832; c=relaxed/simple; bh=SUt1t/dGXLRQIHmGDjBK/uiCU8X/cP41NUQ4Kg/f/FM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kuA359NRiHV1oaS2isgkodQqEO0CJ4u4heF6hAtVbnKaFDkypbS5wc24bsHUdaC6uKOxXrsU+h1k8ypLJUHy3zZr1gUeTzgL+XzPgr1OygpK6yaIw11oMftJ03oFSyBIYf9IsedR9DOmL7hmkbjjf7k2HcOuZhvAFo888RCCLYw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=U1037pHC; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="U1037pHC" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 16178C4AF0B; Thu, 5 Sep 2024 03:10:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725505831; bh=SUt1t/dGXLRQIHmGDjBK/uiCU8X/cP41NUQ4Kg/f/FM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=U1037pHCXv+7d2VV/q7DmZGP2cxg/racl9AHhZ1OxiAQhAq81vFMBd+jheXvLFs8A 1eZMJHCfY6cPfTagFKEMhwoh3k+GmmMKhU+cDKMKhFaHnVTWgM0o6YqFGr+1LYJir3 mKx0CUz5CAdnShQESV6sxybqLWJZhQTQAMthNMqgL+pvYwND+2m/3AMG1XnkBo2t+n fbMKARWmo4zczXpphDBksOiPOGgBnNbaqKsW558oR8+B9qzQUEb3cMBvNWujIVpFbZ 3qeSZf5aOsLg8cDbkuXGBeJOhO6jZCv1vthc5G6vEM66L6jaGvVI+eUkF+KVtLUS7U KHAPRGbgKSPmA== From: Namhyung Kim To: Peter Zijlstra , Ingo Molnar Cc: Kan Liang , Mark Rutland , Alexander Shishkin , Arnaldo Carvalho de Melo , LKML , Stephane Eranian , Ravi Bangoria , Ananth Narayan , Sandipan Das Subject: [PATCH 5/5] perf/x86: Relax privilege filter restriction on AMD IBS Date: Wed, 4 Sep 2024 20:10:27 -0700 Message-ID: <20240905031027.2567913-6-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240905031027.2567913-1-namhyung@kernel.org> References: <20240905031027.2567913-1-namhyung@kernel.org> 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" While IBS is available for per-thread profiling, still regular users cannot open an event due to the default paranoid setting (2) which doesn't allow unprivileged users to get kernel samples. That means it needs to set exclude_kernel bit in the attribute but IBS driver would reject it since it has PERF_PMU_CAP_NO_EXCLUDE. This is not what we want and I've been getting requests to fix this issue. This should be done in the hardware, but until we get the HW fix we may allow exclude_{kernel,user} in the attribute and silently drop the samples in the PMU IRQ handler. It won't guarantee the sampling frequency or even it'd miss some with fixed period too. Not ideal, but that'd still be helpful to regular users. To minimize the confusion, let's add 'swfilt' bit to attr.config2 which is exposed in the sysfs format directory so that users can figure out if the kernel support the privilege filters by software. $ perf record -e ibs_op/swfilt=3D1/uh true This uses perf_exclude_event() which checks regs->cs. But it should be fine because set_linear_ip() also updates the CS according to the RIP provided by IBS. Cc: Ravi Bangoria Cc: Ananth Narayan Cc: Sandipan Das Cc: Stephane Eranian Signed-off-by: Namhyung Kim --- arch/x86/events/amd/ibs.c | 50 ++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/arch/x86/events/amd/ibs.c b/arch/x86/events/amd/ibs.c index e91970b01d6243e4..7fea9527971a7aae 100644 --- a/arch/x86/events/amd/ibs.c +++ b/arch/x86/events/amd/ibs.c @@ -31,6 +31,8 @@ static u32 ibs_caps; #define IBS_FETCH_CONFIG_MASK (IBS_FETCH_RAND_EN | IBS_FETCH_MAX_CNT) #define IBS_OP_CONFIG_MASK IBS_OP_MAX_CNT =20 +/* attr.config2 */ +#define IBS_SW_FILTER_MASK 1 =20 /* * IBS states: @@ -290,6 +292,15 @@ static int perf_ibs_init(struct perf_event *event) if (has_branch_stack(event)) return -EOPNOTSUPP; =20 + /* handle exclude_{user,kernel} in the IRQ handler */ + if (event->attr.exclude_hv || event->attr.exclude_idle || + event->attr.exclude_host || event->attr.exclude_guest) + return -EINVAL; + + if (!(event->attr.config2 & IBS_SW_FILTER_MASK) && + (event->attr.exclude_kernel || event->attr.exclude_user)) + return -EINVAL; + ret =3D validate_group(event); if (ret) return ret; @@ -550,24 +561,14 @@ static struct attribute *attrs_empty[] =3D { NULL, }; =20 -static struct attribute_group empty_format_group =3D { - .name =3D "format", - .attrs =3D attrs_empty, -}; - static struct attribute_group empty_caps_group =3D { .name =3D "caps", .attrs =3D attrs_empty, }; =20 -static const struct attribute_group *empty_attr_groups[] =3D { - &empty_format_group, - &empty_caps_group, - NULL, -}; - PMU_FORMAT_ATTR(rand_en, "config:57"); PMU_FORMAT_ATTR(cnt_ctl, "config:19"); +PMU_FORMAT_ATTR(swfilt, "config2:0"); PMU_EVENT_ATTR_STRING(l3missonly, fetch_l3missonly, "config:59"); PMU_EVENT_ATTR_STRING(l3missonly, op_l3missonly, "config:16"); PMU_EVENT_ATTR_STRING(zen4_ibs_extensions, zen4_ibs_extensions, "1"); @@ -583,6 +584,11 @@ static struct attribute *rand_en_attrs[] =3D { NULL, }; =20 +static struct attribute *swfilt_attrs[] =3D { + &format_attr_swfilt.attr, + NULL, +}; + static struct attribute *fetch_l3missonly_attrs[] =3D { &fetch_l3missonly.attr.attr, NULL, @@ -598,6 +604,11 @@ static struct attribute_group group_rand_en =3D { .attrs =3D rand_en_attrs, }; =20 +static struct attribute_group group_swfilt =3D { + .name =3D "format", + .attrs =3D swfilt_attrs, +}; + static struct attribute_group group_fetch_l3missonly =3D { .name =3D "format", .attrs =3D fetch_l3missonly_attrs, @@ -612,6 +623,7 @@ static struct attribute_group group_zen4_ibs_extensions= =3D { =20 static const struct attribute_group *fetch_attr_groups[] =3D { &group_rand_en, + &group_swfilt, &empty_caps_group, NULL, }; @@ -650,6 +662,12 @@ static struct attribute_group group_op_l3missonly =3D { .is_visible =3D zen4_ibs_extensions_is_visible, }; =20 +static const struct attribute_group *op_attr_groups[] =3D { + &group_swfilt, + &empty_caps_group, + NULL, +}; + static const struct attribute_group *op_attr_update[] =3D { &group_cnt_ctl, &group_op_l3missonly, @@ -667,7 +685,6 @@ static struct perf_ibs perf_ibs_fetch =3D { .start =3D perf_ibs_start, .stop =3D perf_ibs_stop, .read =3D perf_ibs_read, - .capabilities =3D PERF_PMU_CAP_NO_EXCLUDE, }, .msr =3D MSR_AMD64_IBSFETCHCTL, .config_mask =3D IBS_FETCH_CONFIG_MASK, @@ -691,7 +708,6 @@ static struct perf_ibs perf_ibs_op =3D { .start =3D perf_ibs_start, .stop =3D perf_ibs_stop, .read =3D perf_ibs_read, - .capabilities =3D PERF_PMU_CAP_NO_EXCLUDE, }, .msr =3D MSR_AMD64_IBSOPCTL, .config_mask =3D IBS_OP_CONFIG_MASK, @@ -1111,6 +1127,12 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf= _ibs, struct pt_regs *iregs) regs.flags |=3D PERF_EFLAGS_EXACT; } =20 + if ((event->attr.config2 & IBS_SW_FILTER_MASK) && + perf_exclude_event(event, ®s)) { + throttle =3D perf_event_account_interrupt(event); + goto out; + } + if (event->attr.sample_type & PERF_SAMPLE_RAW) { raw =3D (struct perf_raw_record){ .frag =3D { @@ -1228,7 +1250,7 @@ static __init int perf_ibs_op_init(void) if (ibs_caps & IBS_CAPS_ZEN4) perf_ibs_op.config_mask |=3D IBS_OP_L3MISSONLY; =20 - perf_ibs_op.pmu.attr_groups =3D empty_attr_groups; + perf_ibs_op.pmu.attr_groups =3D op_attr_groups; perf_ibs_op.pmu.attr_update =3D op_attr_update; =20 return perf_ibs_pmu_init(&perf_ibs_op, "ibs_op"); --=20 2.46.0.469.g59c65b2a67-goog