From nobody Tue Nov 26 00:23:05 2024 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 9BD09211C for ; Wed, 23 Oct 2024 00:09: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=1729642170; cv=none; b=FTV9w/kscy11BYeaellXL1xUZAyP3pp1GUnoG/fVs/dv1EhJqpp0muJFlKFCtv0Gke8aXyKj0uLqcgq+WU0dZfxT4TtAq/hU1m6PZ8dpX18850sbzoMk3k+RPBqpChUmmCp4FpFfS6dEYUyKvIIlE+lQraTJ61f0bj7AlSgU3l0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729642170; c=relaxed/simple; bh=+9ImHzQM83ukdUpgROB2C6IE07UJCS2V4nHqORAMoyM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=llwnzK4t9r4Ts/GCTDVMiEaQvDgoGduvCtudDVJVwBwiNPFfwnvpO5lFsBUm0MY1jKQ2is63bs2NeSCNstXkzM2Oo0ls+UR0dhBa7Th2PudGLA+daEhrJ1FgBlaKbJpmH2x+Cl+3e20CJJs7t5Jm4DRulKgU0J2AEPsAy70Mna4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YxgRb8Fc; 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="YxgRb8Fc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D9B49C4AF09; Wed, 23 Oct 2024 00:09:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1729642170; bh=+9ImHzQM83ukdUpgROB2C6IE07UJCS2V4nHqORAMoyM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YxgRb8Fc57SM6gbHeQn38FMQnWyRYUGrwsBBypsOG4xdy/cPWzH9p7UmwUNBeFNI5 20hWSSpwU5NFRDMCucrnTYycOHxpXfFouULHYg2yHtpDpp1r7lnd4aNiUmP2nbrH0l SED0/FI16jpnStg33H7gPXuMh5ynvHyfBQBw8/Y+KH02yarlNLJKYMsIo2IawSl3kH ZRarKj6K/SH7t53Sy6H+ohu8o1Yvc8Eq1DfxC9klBT3iRlEQXGjgew5bEnZOt+o0Nh jyw5fKzYcF4pHMzthDOzG4eMPNf7HN2B/W1N+h3JJagz2EisAs5brdk8iO2fciw661 G5D9Xh+b1CiQA== From: Namhyung Kim To: Peter Zijlstra , Ingo Molnar Cc: Kan Liang , Mark Rutland , Alexander Shishkin , Arnaldo Carvalho de Melo , LKML , Stephane Eranian , Ravi Bangoria , Sandipan Das Subject: [PATCH v4 1/5] perf/core: Add PERF_FORMAT_DROPPED Date: Tue, 22 Oct 2024 17:09:24 -0700 Message-ID: <20241023000928.957077-2-namhyung@kernel.org> X-Mailer: git-send-email 2.47.0.105.g07ac214952-goog In-Reply-To: <20241023000928.957077-1-namhyung@kernel.org> References: <20241023000928.957077-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 fb908843f209288d..c1e6340e561c400e 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -830,6 +830,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 4842c36fdf801996..7813e05218657713 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 e3589c4287cb458c..7e15fe0a8dee4ee7 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -5697,6 +5697,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); @@ -5704,6 +5706,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: @@ -5769,6 +5773,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; @@ -7370,6 +7376,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)); } @@ -7408,6 +7416,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 @@ -7423,6 +7433,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.47.0.105.g07ac214952-goog From nobody Tue Nov 26 00:23:05 2024 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 4E8155672; Wed, 23 Oct 2024 00:09: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=1729642171; cv=none; b=pccekiRCIsKG0D0bEDqluTuyIoU8EtEPpmZSr5eocKFF9AN2O5E5wm8ew2PK8ra+/cvf+hF+QWdiI3MRGOAf3yHH06kNcDrre0gIxRdiEgv5yPaiOgrrF6Zz+ZA7yGzK90BeG56heNzzUpa+WO9PGwqB176VRigoe1asDJZZlVI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729642171; c=relaxed/simple; bh=A4QwPJQlw9zD0sZg2Q9Dv/Ayd2j8ALkZKSmR7YsG0Hw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bMdCHckxg2PY41MsA9MXHe/5rMelqZGqsGwP/fw//syMSt6+8WOIZpxAqUv412xNEcnnj/Bv3j/hX36JFFvlTlEO9jajH+5+mPtrUvaFfyMpT+8kWXSGsrl6ZDdn14huaVAMQg2cyF6Y93DlJ8QiHeq9YY4zY2ff/tNphxiLzT8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=exojCRsB; 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="exojCRsB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 53889C4CEC3; Wed, 23 Oct 2024 00:09:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1729642170; bh=A4QwPJQlw9zD0sZg2Q9Dv/Ayd2j8ALkZKSmR7YsG0Hw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=exojCRsB+rVAk4Fj9T2SZgXmyFivZay4GulED0veFq/Md4DUyyHzd3UTkSZQnuxbX X4OSqlZ4wglU9AbmYuA2VuDiwihXT+UgjZELujmRtRnpBwTND4zDoXzj/8IRrD2WsZ wi/9V8X7zOAHZpmVAjIEvs4Qx5mfUXcCcc3Gz1kZWT4iiqNudCwS0w1cPw5ozdF3mW lHrW+0/RInp+uzMxwCb7zw07C/JfTysoQoIXu//w7os/uCe9BGGAyKxHAGW/kwrbD4 HHOTR6w3Iyg5WGQe3o5+3jk/P78h5ZRQjxmUUFzyPJhs+oo/AbUNGGWiJWkZbZpp8X cKa0VBnKFBnBw== From: Namhyung Kim To: Peter Zijlstra , Ingo Molnar Cc: Kan Liang , Mark Rutland , Alexander Shishkin , Arnaldo Carvalho de Melo , LKML , Stephane Eranian , Ravi Bangoria , Sandipan Das , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Thomas Richter , linux-s390@vger.kernel.org Subject: [PATCH v4 2/5] perf/core: Export perf_exclude_event() Date: Tue, 22 Oct 2024 17:09:25 -0700 Message-ID: <20241023000928.957077-3-namhyung@kernel.org> X-Mailer: git-send-email 2.47.0.105.g07ac214952-goog In-Reply-To: <20241023000928.957077-1-namhyung@kernel.org> References: <20241023000928.957077-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 Acked-by: Thomas Richter --- 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 5b765e3ccf0cadc8..ff9e694f2be45c6b 100644 --- a/arch/s390/kernel/perf_cpum_sf.c +++ b/arch/s390/kernel/perf_cpum_sf.c @@ -996,7 +996,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 @@ -1005,7 +1005,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)) @@ -1088,8 +1088,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 c1e6340e561c400e..6b31958a2b1db8db 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1649,6 +1649,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, @@ -1832,6 +1834,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 7e15fe0a8dee4ee7..5d24597180dec167 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -10001,18 +10001,21 @@ static void perf_swevent_event(struct perf_event = *event, 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.47.0.105.g07ac214952-goog From nobody Tue Nov 26 00:23:05 2024 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 DBE65D27E; Wed, 23 Oct 2024 00:09: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=1729642172; cv=none; b=DDGoaw05lQ7DonV/v6tdoDVcC5vuEvNfmG8gSScJBXK5u3dml+BlGIG0x4nGqzhl+bzS7o2U/OF3xyGXLwtZbs5vuIBNqL+d7cO0wMWqLN5cdgjme+ZW7v5tGvGvfeZANPMAgSepgF8ZGBIps3k/AGg3GvDqYkrOAzqM20nIeSM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729642172; c=relaxed/simple; bh=PKH4Jgsz//Q/of6iS1YGe153y5OqAJsRm7bX4nqyHko=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ucgrau9lyuyPU+eBSPbcI8DP5Op1v1+qZEZEFRTjr75O2B71c2RvtfDGcZJ+VArnHi6uw8WvLLS33fzzZYBTpCAiCEAMbUZNbwKLrZTqx57uW16rXfVFQwQDtaXtT25NehBJCU9s+Njr+yd31zd4RwrvLHP3S3NOe4ouDa4p3XA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rh0fM182; 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="rh0fM182" Received: by smtp.kernel.org (Postfix) with ESMTPSA id F349DC4CECD; Wed, 23 Oct 2024 00:09:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1729642171; bh=PKH4Jgsz//Q/of6iS1YGe153y5OqAJsRm7bX4nqyHko=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rh0fM182/aJDP8EyWTODmgWd748o++4ysFpRNbTKxu1I70JLyjtyCBbX/jyshjnXm 4fc9mWye5Jn+ADUKcSY9ESrTA+2Nk2wDAwnUaJdOm+3oXOlIWFKColC1hCfmQ2ZICb Xt0iiHC7Zak2fBw7VfG2zhq2trYS1BR4XI5oVj19XbWhHMhx1McEIRNPCWg/FlGni9 rPAkY24w6kmD2MdFTz+9yn3a/2OT459ORjlu5FORfPEuFaI05GpIah3MfiXodJe54o qoMqEDkXIi0j9ciIoKgpDkcntwPJV83AcXjrJ2zt+RROsODATUX44G8+MBVZTSSPbM QnF59rcV2l1Vg== From: Namhyung Kim To: Peter Zijlstra , Ingo Molnar Cc: Kan Liang , Mark Rutland , Alexander Shishkin , Arnaldo Carvalho de Melo , LKML , Stephane Eranian , Ravi Bangoria , Sandipan Das , Kyle Huey , Alexei Starovoitov , Andrii Nakryiko , Song Liu , bpf@vger.kernel.org Subject: [PATCH v4 3/5] perf/core: Account dropped samples from BPF Date: Tue, 22 Oct 2024 17:09:26 -0700 Message-ID: <20241023000928.957077-4-namhyung@kernel.org> X-Mailer: git-send-email 2.47.0.105.g07ac214952-goog In-Reply-To: <20241023000928.957077-1-namhyung@kernel.org> References: <20241023000928.957077-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. Acked-by: Kyle Huey Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Song Liu 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 5d24597180dec167..b41c17a0bc19f7c2 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9831,8 +9831,10 @@ static int __perf_event_overflow(struct perf_event *= event, ret =3D __perf_event_account_interrupt(event, throttle); =20 if (event->prog && event->prog->type =3D=3D BPF_PROG_TYPE_PERF_EVENT && - !bpf_overflow_handler(event, data, regs)) + !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.47.0.105.g07ac214952-goog From nobody Tue Nov 26 00:23:05 2024 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 5799F171BB for ; Wed, 23 Oct 2024 00:09:32 +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=1729642172; cv=none; b=g+YW6uU1sx7BHa+wxqOywST0x3CdvC+lEHofqnKRZ9SOPuAF9r7xkxdpXq7TGIQylFtsov7Yr+PXSFHeVy8qo80UPDtMRMjZfbO71amHrkWUEqEy9iP+JWWbmH7Zqa217u5x6cXcVtLWIf/I/mpJEpuj4C366RArf8oGc015pPE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729642172; c=relaxed/simple; bh=19tEuIYfqiZ31nw60wQoKj/BrMPUXOXCbGkABPMtDio=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pUTMjKLE8ib74Sistoia0pAc+T2uy966BH+cY78f9qU87FS2uRIiUOoY3KG+Nw5UWAm07FaujlOjwDcoPXbRliko5/N6XIR5R0rMBwuYX2DER6wui8RbP+B200VP8E9jENRovRWSFQNu/yhAqQWC+bAnvs2C3Qlm1a7AvsxM55c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=d8NpyZs+; 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="d8NpyZs+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A3034C4CEE7; Wed, 23 Oct 2024 00:09:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1729642172; bh=19tEuIYfqiZ31nw60wQoKj/BrMPUXOXCbGkABPMtDio=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=d8NpyZs+nIvTORI8b9ezVmcZhx5k/19f3THU1Sz6tPdDk4bJpoc3q9iqoCQ+DczyZ AuVWeHu8DrHMUK+x3jRTWXnXfXxj9L+7D0z7NC8UdLDs7/It8qwoLcFx4RHJ6cR4F1 s0Fl8VcOfNnfSTO58/4NWU6keFxoTjgES1UISVCQYkPZgckQhPa2uKs6Mu4WAzZrqY w3VvD8Xubfjk3QZH0iZdFillQA+aFfgA53Sxs/s5QpDSR9hmsTXnSG1SZ4RmhhAxCc bxDBQ5Lu5U47eKZw3oE8jvvZ65p92T0keU+oXZlHxAHzQ0tip9XComAdE/mm3LBl9P weMgJOwI2r1zA== From: Namhyung Kim To: Peter Zijlstra , Ingo Molnar Cc: Kan Liang , Mark Rutland , Alexander Shishkin , Arnaldo Carvalho de Melo , LKML , Stephane Eranian , Ravi Bangoria , Sandipan Das , Madhavan Srinivasan , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Naveen N Rao , Kajol Jain , Athira Rajeev , linuxppc-dev@lists.ozlabs.org Subject: [PATCH v4 4/5] perf/powerpc: Count dropped samples in core-book3s PMU Date: Tue, 22 Oct 2024 17:09:27 -0700 Message-ID: <20241023000928.957077-5-namhyung@kernel.org> X-Mailer: git-send-email 2.47.0.105.g07ac214952-goog In-Reply-To: <20241023000928.957077-1-namhyung@kernel.org> References: <20241023000928.957077-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. Reviewed-by: Madhavan Srinivasan 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 --- 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.47.0.105.g07ac214952-goog From nobody Tue Nov 26 00:23:05 2024 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 CBF581A29A for ; Wed, 23 Oct 2024 00:09:32 +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=1729642172; cv=none; b=ZKwJ191dtPMzwzaMbMceKvISzI5M+4BypBCNg2gK2Puy5iI3AQJz1sacRtK0ihUEgwpq+3YooXqFOa2UQibqlK1e8lInfwJzZNzhxR5juRMj9s60OWJnWnEjNuL+kqQPNqETxBD52VqqeWyTQ5Ajp7hBOXoDlSGKVSEpHuqrwFw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729642172; c=relaxed/simple; bh=bMrbyixiaTcC6Gds/aupKMLHu7/KWuPTgmG7iuWeJzA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hMtBS+z/ng/jb3p0pFfuiicYw+gsymy/U8Vx/Vr5LC+eD5IS0AC8HdhIGolL+5LylAvk0LK/Hj8lf6btPc40MMWjkC893GfS9gJr84XFbZD/HksP4MuerELFbIdAXJX6b4zt+moyW5SffsN7crGFSvjMVaytEpjuxl9Xi2aglLY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qI+Ib8ad; 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="qI+Ib8ad" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 59707C4CEEB; Wed, 23 Oct 2024 00:09:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1729642172; bh=bMrbyixiaTcC6Gds/aupKMLHu7/KWuPTgmG7iuWeJzA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qI+Ib8adVXoFyAH5MTW9Fle/MgxaJ0Oy29gZwuUR2jUQKZ6x85b11WxETOHSofKID aPstWvIw5gA4Xl9cid5AOqXUBj6CFWNqIsGyth5xq+9TRdFDQWLKKpjubYxNdcxy9c 72TiYtJJUvIR5rEP8DNjZ8BVaSIW59gMqJz8vr8ZRvrgwBa1ZgGhZfbgq1T8xwuDUt CRiMA72ai1fOOgmZmhEop4PXCURcmnaBa1/FCwMtDQZGjENT8Eb95UVWR3AEecxI0e PbjLf0xOos2g7hKPZaARYrzh4HGPdv7n+90WqrU6DvrHuMjQznZlX+7dtx+YQY4AyL n4oF707CdmaEg== From: Namhyung Kim To: Peter Zijlstra , Ingo Molnar Cc: Kan Liang , Mark Rutland , Alexander Shishkin , Arnaldo Carvalho de Melo , LKML , Stephane Eranian , Ravi Bangoria , Sandipan Das , Ananth Narayan Subject: [PATCH v4 5/5] perf/x86: Relax privilege filter restriction on AMD IBS Date: Tue, 22 Oct 2024 17:09:28 -0700 Message-ID: <20241023000928.957077-6-namhyung@kernel.org> X-Mailer: git-send-email 2.47.0.105.g07ac214952-goog In-Reply-To: <20241023000928.957077-1-namhyung@kernel.org> References: <20241023000928.957077-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,hv} 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/u 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 | 59 +++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/arch/x86/events/amd/ibs.c b/arch/x86/events/amd/ibs.c index e91970b01d6243e4..d89622880a9fbbb9 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,16 @@ 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_host || event->attr.exclude_guest || + event->attr.exclude_idle) + return -EINVAL; + + if (!(event->attr.config2 & IBS_SW_FILTER_MASK) && + (event->attr.exclude_kernel || event->attr.exclude_user || + event->attr.exclude_hv)) + return -EINVAL; + ret =3D validate_group(event); if (ret) return ret; @@ -550,24 +562,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"); @@ -578,8 +580,9 @@ zen4_ibs_extensions_is_visible(struct kobject *kobj, st= ruct attribute *attr, int return ibs_caps & IBS_CAPS_ZEN4 ? attr->mode : 0; } =20 -static struct attribute *rand_en_attrs[] =3D { +static struct attribute *fetch_attrs[] =3D { &format_attr_rand_en.attr, + &format_attr_swfilt.attr, NULL, }; =20 @@ -593,9 +596,9 @@ static struct attribute *zen4_ibs_extensions_attrs[] = =3D { NULL, }; =20 -static struct attribute_group group_rand_en =3D { +static struct attribute_group group_fetch_formats =3D { .name =3D "format", - .attrs =3D rand_en_attrs, + .attrs =3D fetch_attrs, }; =20 static struct attribute_group group_fetch_l3missonly =3D { @@ -611,7 +614,7 @@ static struct attribute_group group_zen4_ibs_extensions= =3D { }; =20 static const struct attribute_group *fetch_attr_groups[] =3D { - &group_rand_en, + &group_fetch_formats, &empty_caps_group, NULL, }; @@ -628,6 +631,11 @@ cnt_ctl_is_visible(struct kobject *kobj, struct attrib= ute *attr, int i) return ibs_caps & IBS_CAPS_OPCNT ? attr->mode : 0; } =20 +static struct attribute *op_attrs[] =3D { + &format_attr_swfilt.attr, + NULL, +}; + static struct attribute *cnt_ctl_attrs[] =3D { &format_attr_cnt_ctl.attr, NULL, @@ -638,6 +646,11 @@ static struct attribute *op_l3missonly_attrs[] =3D { NULL, }; =20 +static struct attribute_group group_op_formats =3D { + .name =3D "format", + .attrs =3D op_attrs, +}; + static struct attribute_group group_cnt_ctl =3D { .name =3D "format", .attrs =3D cnt_ctl_attrs, @@ -650,6 +663,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_op_formats, + &empty_caps_group, + NULL, +}; + static const struct attribute_group *op_attr_update[] =3D { &group_cnt_ctl, &group_op_l3missonly, @@ -667,7 +686,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 +709,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 +1128,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 +1251,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.47.0.105.g07ac214952-goog