From nobody Fri Dec 19 07:19:01 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 C1DC2189B86 for ; Fri, 30 Aug 2024 23:29:12 +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=1725060552; cv=none; b=mrwvCKpV7v/0lr04Ineu+unfphsW84l0uFm6yVIkagnGIJblmHu3O5KxcAq8jJistZOyPvimVg35yjPqHkwpTQojGUGxmHc9Mo99qsmXYRubGgVjprZ/VkCO4n4XpuEUBYF7R+e03i5ZJbvVkwfSIOgZLhQIzU/eubSkIN1ktrM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725060552; c=relaxed/simple; bh=Z35wGoOvlGoZEMj7VNvwpNtCUeWsIs1LrPL4WtnqMGU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=b9Wn8wuy/yQ6l5wyrJhsMjECeveXo52Tu9qhUrwQ7j+t52HjBB7qR1TG/uajgrXt4owSqxUYNjO/q7oIty6AskazLrgWf82BZlnan4jX2GkEvTc7M05TVbB2nkrf4bW5PTmRPKAIAMs3hxty9al7X6fAweJzRsOoo/YOi3yB8nM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Tc3vUnY4; 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="Tc3vUnY4" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E7526C4CECB; Fri, 30 Aug 2024 23:29:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725060552; bh=Z35wGoOvlGoZEMj7VNvwpNtCUeWsIs1LrPL4WtnqMGU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Tc3vUnY4nXFpgu6SoQwIbgTReaJHCi3sccMjUw/J68E2WxXbxOEDXT9j9kpbalEar +LocJVAtB/9F1spKDMU6IunQmkl6PKIGf8unNbPuFQ06+A+dJPoyj43sh7dN2qjsSX ZtG/vGCliYVDdlhznYDTy3zeMIX+r4Pj1XSESotmKJS1IPJNHRNWDlaroFdhJNsEaP 5GpMOAjjxdawhhXJR5L7dARlIvHounzgO+wMG/0evtL07q/FwH+X7ml45XTLdu/tsg 6H7yTxEWq/i7eHsAq7cvqfc+8qoF5KwPj6LzMK9RvMw2bcOHi2O+xxO0P6Qyvz8qh7 EI3RsvboYKpNA== 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: [RFC/PATCH 1/4] perf/core: Add PERF_FORMAT_DROPPED Date: Fri, 30 Aug 2024 16:29:07 -0700 Message-ID: <20240830232910.1839548-2-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240830232910.1839548-1-namhyung@kernel.org> References: <20240830232910.1839548-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 701549967c18..955d39543398 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 58daf6156fd0..6f19d4f74823 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 c6a720f41225..4d72538628ee 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 07:19:01 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 3FFDD18E371 for ; Fri, 30 Aug 2024 23:29:12 +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=1725060553; cv=none; b=UkWyPMdVjRwdHX7GUjaSRtfSgNEe16oUWKZBra62RnTcAE9cWt3H7AUOfnHhgnXxn/Er0rV2mfq6CCo5oA/AtMFZxqGbO0Acb+mK4TAXA+7P3GyXOcEqSulGwo6dMXOsKVXzL/4O3BG9SdhifWqTWNpgyKcQNwgjLM1tNFONN7I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725060553; c=relaxed/simple; bh=4Ii+dUnPcg3T2XmEEMSdM+nVDagN9epzB1SA1hZllB4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SA4e3hlvzn/yeR7foiEU50TRs0p5LqM0753yZm44SQllL3u7iTB4xOr5VMhfucOGoWdeqUdWruRiPjAFq/Ait5AzjfCv5H4iBSzrsK9kZGxyj7FGpYP/Y3EkW4cTF0TdjqNbyvrzbkPJYVB4hkxgXFkB5XoMit0T7IgWDa3NvA4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gHdxcIQS; 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="gHdxcIQS" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 68147C4CED0; Fri, 30 Aug 2024 23:29:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725060552; bh=4Ii+dUnPcg3T2XmEEMSdM+nVDagN9epzB1SA1hZllB4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gHdxcIQSyj8sDVPy0zvrPbuqOj1SIuXRvBmMo9uKwkNYTAzoqTMuJxqcFPmmJFMIY /S3L6onv2WToAAQyjjmSnzTc0LsPtjfyuJ6bxg5Vhh5jQiiJyTkFy5H/ciJgsRwU2N zYVZWNohBgIdbuvd1ywt96hjxtCB5ec1k+ABi04otZ0WNoKcmDquA+sm4oEnaC5VXx EDSDN7auI2K2OBpPn9fP3Iwoaqhkm+Z7EpOGMQfykMWoUcV6rctdm6zVL0AI+nwjmA iVq/a046G9DD/Uk5WUzatN9S0KKFEBfidDFcWQiGWxE9Ky+6rIFQsyg6Ui8ks/f51S z+oJz2DIOADtw== 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: [RFC/PATCH 2/4] perf/core: Export perf_exclude_event() Date: Fri, 30 Aug 2024 16:29:08 -0700 Message-ID: <20240830232910.1839548-3-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240830232910.1839548-1-namhyung@kernel.org> References: <20240830232910.1839548-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. Signed-off-by: Namhyung Kim --- include/linux/perf_event.h | 2 ++ kernel/events/core.c | 11 +++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 955d39543398..c93673a8d66a 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, diff --git a/kernel/events/core.c b/kernel/events/core.c index 4d72538628ee..8250e76f6335 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 07:19:01 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 A55EF18FDB9 for ; Fri, 30 Aug 2024 23:29:13 +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=1725060553; cv=none; b=IOePOgWRM1z8GFy66t7Y8IRvSU/yFCp/35Te8i+mFFm0i6OVKSk0IpNaC3KBN7q1ARaBq4acH2A1Xp8Ybed/C26AzHZylxtRhXPijDKYTFHfmKp4YQi0yqK85DsN7KrPTeSmi+Kx828wlgWIT/NjBiZDmxlcX4DvArzenCFXS4A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725060553; c=relaxed/simple; bh=prH0/SeaSKC9p22Ln6Tcjz4jujhD5pDE9JLweDDsE+Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZFrW0e1NGEVqAMn7pFUuWdXd3LT7CekOxoq60ZLFjcrCHN+TbqjiJoVx3f78gF4UnsDqqc02TlR6BVVlEwnigEf71b5oRk70GsNIK2KFClsT1QhcSPus2P8gkCDEVU6yafz3uyw6PPNjkQctmdqYS0tHxurQkXiLX631cUrs8nA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lk4VnZKU; 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="lk4VnZKU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DC94BC4CECA; Fri, 30 Aug 2024 23:29:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725060553; bh=prH0/SeaSKC9p22Ln6Tcjz4jujhD5pDE9JLweDDsE+Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lk4VnZKUIeNyGC2mJNoDVRkWUWyNRvRpH0MCIBhg57cokFqXiZAXf9dG6VeOYG+Sj si/jD+B/YWvSqot4QSVjfHapkyXX2xTGyqNStpyM70wKWGI25tI3BGs43kSXmufTD2 BAOlINdpc/26xP2/+I4+YnD/pdqTI3zKqQfBZtnYa472/5gphyUj+DT0wwH25nHZ4W 1WdkGZmokHDaA2yHS5PP+NbPrKMF4LbtuP8E2qsWB9lXEXHj5ltD0vJjQWLdIlStpi /4/C21ywBUZHp+pe4mtuallsEorKhFg4BSDM9qFwK/D0qwIkD8elpHydrjKw88ECUa aTKGF+EsT6iUA== 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: [RFC/PATCH 3/4] perf/core: Account dropped samples from BPF Date: Fri, 30 Aug 2024 16:29:09 -0700 Message-ID: <20240830232910.1839548-4-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240830232910.1839548-1-namhyung@kernel.org> References: <20240830232910.1839548-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. 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 8250e76f6335..ba1f6b51ea26 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 07:19:01 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 CC5F018FDDA for ; Fri, 30 Aug 2024 23:29:13 +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=1725060553; cv=none; b=JmGFXcGiCrOwcxaJQiBve70+CWJe1EvpQ2+AciiNYDqm+K1xukAWNUy7Qyv/PRodd0OGTYo57Gfzqo0rWF+040OqAIHN42JvSItDxhMYjsPG8QfXKgdjqOUVs9db6JPcJZd37kzjeuk50vov/U+U8NGQKCRUuJWGt1pAMW+3iBs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725060553; c=relaxed/simple; bh=FhSuLhCZLujOsL7ZD0FWBtHpkqfk0F0sycpI8h9gs4w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lf5WnkgOfOgDYu/SPQoWap2ngwlK8FAAklglBAlFtt6qZIn0F52y3ZRt8+kZSVDdWKzPt1X6IrYVg4TZhwwKi1ZbF2l52sb4iwUja61+6ZBiCLLds6BkiPYbbMadZqiaBQMQa/1GVRz15/jzXXi5rKlro5462eWBPbnjL4aRiIw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZM1U2eyX; 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="ZM1U2eyX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 59F3FC4CEC6; Fri, 30 Aug 2024 23:29:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725060553; bh=FhSuLhCZLujOsL7ZD0FWBtHpkqfk0F0sycpI8h9gs4w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZM1U2eyXBDhtfsvZiIjrBCu/u6XVvteZUBf8gyCpR55Im+KzkVUTg+yFv7s6sbPkj /M3VbAxS0iV2COvXY+ZJBSICmyzDdDk+xir1sZyxZexDgUTU9Lpp0oLOvYK6l5bVO7 F4+iJOo1OKnwt/mdO2doHAg+gBITxygSHN6edtGRYx781nOEKQkQYInRLh6jiOQLKp iVQEJGeMEcgsz0r6v8zsgdQW0M2iQIYSeL8lN0KxZBeVYYYjZm8DQLAwMsALSycRCK JW3x0w0nPiM4E549qldThfskOri4HcB6Le0InPFz6K4edX6vamPHK4SNsco2C64Zuu GgSH5x3lMIuaQ== 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: [RFC/PATCH 4/4] perf/x86: Relax privilege filter restriction on AMD IBS Date: Fri, 30 Aug 2024 16:29:10 -0700 Message-ID: <20240830232910.1839548-5-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240830232910.1839548-1-namhyung@kernel.org> References: <20240830232910.1839548-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. 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: Stephane Eranian Signed-off-by: Namhyung Kim --- arch/x86/events/amd/ibs.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/arch/x86/events/amd/ibs.c b/arch/x86/events/amd/ibs.c index e91970b01d62..e40e2255239a 100644 --- a/arch/x86/events/amd/ibs.c +++ b/arch/x86/events/amd/ibs.c @@ -290,6 +290,11 @@ 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; + ret =3D validate_group(event); if (ret) return ret; @@ -667,7 +672,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 +695,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 +1114,12 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf= _ibs, struct pt_regs *iregs) regs.flags |=3D PERF_EFLAGS_EXACT; } =20 + if (perf_exclude_event(event, ®s)) { + throttle =3D perf_event_account_interrupt(event); + atomic64_inc(&event->dropped_samples); + goto out; + } + if (event->attr.sample_type & PERF_SAMPLE_RAW) { raw =3D (struct perf_raw_record){ .frag =3D { --=20 2.46.0.469.g59c65b2a67-goog