From nobody Tue Dec 16 16:41:32 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 F11ABC10DC3 for ; Mon, 11 Dec 2023 04:56:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233025AbjLKEzw (ORCPT ); Sun, 10 Dec 2023 23:55:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232982AbjLKEzs (ORCPT ); Sun, 10 Dec 2023 23:55:48 -0500 Received: from mail-yw1-x1135.google.com (mail-yw1-x1135.google.com [IPv6:2607:f8b0:4864:20::1135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00C65ED for ; Sun, 10 Dec 2023 20:55:54 -0800 (PST) Received: by mail-yw1-x1135.google.com with SMTP id 00721157ae682-5d33574f64eso39483907b3.3 for ; Sun, 10 Dec 2023 20:55:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1702270554; x=1702875354; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4LaOcUMKpaO4TscNM+bwybPG9qx56qSeDWtKT8IysKk=; b=M3R04DG+k5gvzaElC3f8cfdbgyW2SN0/jlAMkC9xioLyoYthHoXeq0o/70HRyD/hC9 mMCNPvjyAeIofw0hVH+aEMqMNDjLManpbBClzlJrbP8KwIlvVyFLGWn7HMfeIWYd79mQ Ir6P+rdfe1ruTQHr1XrqVjcTEq199D/RXum+K+DYNL95xxEGM9sj+bBdtFI5VeUI1gJa obiMRMwdcYuAFLc34LMY7KE4HQqOQdZEcwbiqtOnJGLpfL1/9n8ESaiZ7dIlxKNgFzZD tDvxBLU/cssGK6CxDnS64XwHRGS0J4yn4K3aSxVJ8a8o1+S0jZ0oFvVV/wBZ6I9r8zOH Pviw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702270554; x=1702875354; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4LaOcUMKpaO4TscNM+bwybPG9qx56qSeDWtKT8IysKk=; b=WHgpd5eNUs7KL1BbCbzfi5lU1p+QSqpqKed0G/bbRf888lFjn555ERyXqR4MDRzIGo IRTn+obJY/MeDGRW6p4lZmw0RubCxYGc2JrGRr09itlbJ12AK3WWby4pBH5pdtWI2UL+ L8aAGQAuHfxc0b23jEfk5hNWNbRaVTJ/6kTOxEbokN7AGCH0W7sY0LrfiweKkAUOHkzX vf3iy7iqaC7j/9qgZe81ljh441GigkxStTuublPpvfBJ80Y8wllWcjmrHSYnamLXQomt QD4WFzPm3C7gLWkm8D2O1kDYwbQxxk9JNX7kwcfmwvZ+fmEUBQwoWi5C+9/qpk4rWtI9 t/uQ== X-Gm-Message-State: AOJu0Yxt2vXt+RbcW6QGSB2ZCLQgvtDAA5QuZ7471XRqmNG6eQlGoW2W x5U6kbBuBBF57g8fNlV3V4ADkQ== X-Google-Smtp-Source: AGHT+IHAF2s56Q3cvUDcAQsGiDW8PGeDXKlIoMtqxBQkR5BpVaFZyDHs+/DYtQpzmLR2hm7B1UnyWw== X-Received: by 2002:a0d:d5cd:0:b0:5d7:1940:b36c with SMTP id x196-20020a0dd5cd000000b005d71940b36cmr3212062ywd.56.1702270554159; Sun, 10 Dec 2023 20:55:54 -0800 (PST) Received: from zhadum.home.kylehuey.com (c-76-126-33-191.hsd1.ca.comcast.net. [76.126.33.191]) by smtp.gmail.com with ESMTPSA id e11-20020a170902b78b00b001d2ffeac9d3sm3300623pls.186.2023.12.10.20.55.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Dec 2023 20:55:53 -0800 (PST) From: Kyle Huey X-Google-Original-From: Kyle Huey To: Kyle Huey , linux-kernel@vger.kernel.org, Andrii Nakryiko , Jiri Olsa , Namhyung Kim , Marco Elver , Yonghong Song Cc: Robert O'Callahan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH v3 1/4] perf/bpf: Call bpf handler directly, not through overflow machinery Date: Sun, 10 Dec 2023 20:55:40 -0800 Message-Id: <20231211045543.31741-2-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231211045543.31741-1-khuey@kylehuey.com> References: <20231211045543.31741-1-khuey@kylehuey.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" To ultimately allow bpf programs attached to perf events to completely suppress all of the effects of a perf event overflow (rather than just the sample output, as they do today), call bpf_overflow_handler() from __perf_event_overflow() directly rather than modifying struct perf_event's overflow_handler. Return the bpf program's return value from bpf_overflow_handler() so that __perf_event_overflow() knows how to proceed. Remove the now unnecessary orig_overflow_handler from struct perf_event. This patch is solely a refactoring and results in no behavior change. Signed-off-by: Kyle Huey Suggested-by: Namhyung Kim Acked-by: Namhyung Kim Acked-by: Song Liu --- include/linux/perf_event.h | 6 +----- kernel/events/core.c | 28 +++++++++++++++------------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 5547ba68e6e4..312b9f31442c 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -810,7 +810,6 @@ struct perf_event { perf_overflow_handler_t overflow_handler; void *overflow_handler_context; #ifdef CONFIG_BPF_SYSCALL - perf_overflow_handler_t orig_overflow_handler; struct bpf_prog *prog; u64 bpf_cookie; #endif @@ -1337,10 +1336,7 @@ __is_default_overflow_handler(perf_overflow_handler_= t overflow_handler) #ifdef CONFIG_BPF_SYSCALL static inline bool uses_default_overflow_handler(struct perf_event *event) { - if (likely(is_default_overflow_handler(event))) - return true; - - return __is_default_overflow_handler(event->orig_overflow_handler); + return is_default_overflow_handler(event); } #else #define uses_default_overflow_handler(event) \ diff --git a/kernel/events/core.c b/kernel/events/core.c index b704d83a28b2..54f6372d2634 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9515,6 +9515,12 @@ static inline bool sample_is_allowed(struct perf_eve= nt *event, struct pt_regs *r return true; } =20 +#ifdef CONFIG_BPF_SYSCALL +static int bpf_overflow_handler(struct perf_event *event, + struct perf_sample_data *data, + struct pt_regs *regs); +#endif + /* * Generic event overflow handling, sampling. */ @@ -9584,7 +9590,10 @@ static int __perf_event_overflow(struct perf_event *= event, irq_work_queue(&event->pending_irq); } =20 - READ_ONCE(event->overflow_handler)(event, data, regs); +#ifdef CONFIG_BPF_SYSCALL + if (!(event->prog && !bpf_overflow_handler(event, data, regs))) +#endif + READ_ONCE(event->overflow_handler)(event, data, regs); =20 if (*perf_event_fasync(event) && event->pending_kill) { event->pending_wakeup =3D 1; @@ -10394,9 +10403,9 @@ static void perf_event_free_filter(struct perf_even= t *event) } =20 #ifdef CONFIG_BPF_SYSCALL -static void bpf_overflow_handler(struct perf_event *event, - struct perf_sample_data *data, - struct pt_regs *regs) +static int bpf_overflow_handler(struct perf_event *event, + struct perf_sample_data *data, + struct pt_regs *regs) { struct bpf_perf_event_data_kern ctx =3D { .data =3D data, @@ -10417,10 +10426,8 @@ static void bpf_overflow_handler(struct perf_event= *event, rcu_read_unlock(); out: __this_cpu_dec(bpf_prog_active); - if (!ret) - return; =20 - event->orig_overflow_handler(event, data, regs); + return ret; } =20 static int perf_event_set_bpf_handler(struct perf_event *event, @@ -10456,8 +10463,6 @@ static int perf_event_set_bpf_handler(struct perf_e= vent *event, =20 event->prog =3D prog; event->bpf_cookie =3D bpf_cookie; - event->orig_overflow_handler =3D READ_ONCE(event->overflow_handler); - WRITE_ONCE(event->overflow_handler, bpf_overflow_handler); return 0; } =20 @@ -10468,7 +10473,6 @@ static void perf_event_free_bpf_handler(struct perf= _event *event) if (!prog) return; =20 - WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler); event->prog =3D NULL; bpf_prog_put(prog); } @@ -11928,13 +11932,11 @@ perf_event_alloc(struct perf_event_attr *attr, in= t cpu, overflow_handler =3D parent_event->overflow_handler; context =3D parent_event->overflow_handler_context; #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING) - if (overflow_handler =3D=3D bpf_overflow_handler) { + if (parent_event->prog) { struct bpf_prog *prog =3D parent_event->prog; =20 bpf_prog_inc(prog); event->prog =3D prog; - event->orig_overflow_handler =3D - parent_event->orig_overflow_handler; } #endif } --=20 2.34.1 From nobody Tue Dec 16 16:41:32 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 37541C4167B for ; Mon, 11 Dec 2023 04:56:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233064AbjLKEz5 (ORCPT ); Sun, 10 Dec 2023 23:55:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232984AbjLKEzu (ORCPT ); Sun, 10 Dec 2023 23:55:50 -0500 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E521FED for ; Sun, 10 Dec 2023 20:55:56 -0800 (PST) Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-2866af9d73bso2792198a91.3 for ; Sun, 10 Dec 2023 20:55:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1702270556; x=1702875356; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=MD1CGVptJqB75nV04X/vQRYGuHl6l1SpLkm2NuE80OA=; b=MTnHAGWt+qRvHekeTQ34DLhn3CKxjyXAQxPHsdHOZCFMr1OeQBEcgCZAi+dte3F9e4 Tv0s7kcJDYTpMmHnWyqNzMxUxd31ROM/qPQjsrbcLJd6U0IW+99p29Xh8JrWDOAXh9sm fS8YNpXfvpllHfkuBg4tKTSslrTIk1xSjSWghPb7WnH3fipst/Hb3SbWpx2tHPJftNE9 1yve8XSVYvSYpPbP9c8Nob1B6LR7vR/nQihHjBizydeA/O+MLG21kPtqlHpatqEMr503 JOKYWJjCWZcfcXqdIk/ozkCVwI6tEaKawHO7yMpfJ1RUVFD6TyriSnrhIWUKH+ppH6dD St5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702270556; x=1702875356; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MD1CGVptJqB75nV04X/vQRYGuHl6l1SpLkm2NuE80OA=; b=lOt6P4lLz+EHsGtCiiBegjRIVXXhOX+hktt4JWFe6Xu1r7tqGcqVXVIgvkveihcnfZ HNjGHkqJEQWV4UhDkLsgTKRe6bAn9Ef1r6UPwyOcFIad2fUKqURu53CH1kt+k61IwT7u AmofzqvI6gVOT5WONKww8L8znVctpYmBvB5iCUAsGTNdchr8PGVoCUJsOLxdqase4y57 c5MsnvdMsuHPu/b5vMFYOLM7z8xmFhd3Tx/ZzuqlKJFVDBUoohs5lk3yB31hsh7V3cNs tEikWzztb5isV5f3Iu3EKEJV0obMipEwlKsm8a5hZoBcBmETLxtCCh4KIZk4kMPnoPbY DxHQ== X-Gm-Message-State: AOJu0YxvgwKF4+wkgAi/GsXVN4xRiM/w9SWtSSwPWCJJtqQT9JEEkLpJ vD7HmUS60Vm3fAzyDMofUGH8qA== X-Google-Smtp-Source: AGHT+IFk4Rw49zHj089ze8JCHt9tGAvA1DX6jp40Y61tz9veBn8RRGtYiMdt7+m5TnCHgq0Zz4xZqQ== X-Received: by 2002:a17:902:da89:b0:1d0:6ffe:1e63 with SMTP id j9-20020a170902da8900b001d06ffe1e63mr2161568plx.70.1702270556361; Sun, 10 Dec 2023 20:55:56 -0800 (PST) Received: from zhadum.home.kylehuey.com (c-76-126-33-191.hsd1.ca.comcast.net. [76.126.33.191]) by smtp.gmail.com with ESMTPSA id e11-20020a170902b78b00b001d2ffeac9d3sm3300623pls.186.2023.12.10.20.55.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Dec 2023 20:55:55 -0800 (PST) From: Kyle Huey X-Google-Original-From: Kyle Huey To: Kyle Huey , linux-kernel@vger.kernel.org, Andrii Nakryiko , Jiri Olsa , Namhyung Kim , Marco Elver , Yonghong Song Cc: Robert O'Callahan , Will Deacon , Mark Rutland , Russell King , Catalin Marinas , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Alexander Shishkin , Ian Rogers , Adrian Hunter , linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH v3 2/4] perf/bpf: Remove unneeded uses_default_overflow_handler. Date: Sun, 10 Dec 2023 20:55:41 -0800 Message-Id: <20231211045543.31741-3-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231211045543.31741-1-khuey@kylehuey.com> References: <20231211045543.31741-1-khuey@kylehuey.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Now that struct perf_event's orig_overflow_handler is gone, there's no need for the functions and macros to support looking past overflow_handler to orig_overflow_handler. This patch is solely a refactoring and results in no behavior change. Signed-off-by: Kyle Huey Acked-by: Namhyung Kim Acked-by: Song Liu Acked-by: Will Deacon --- arch/arm/kernel/hw_breakpoint.c | 8 ++++---- arch/arm64/kernel/hw_breakpoint.c | 4 ++-- include/linux/perf_event.h | 16 ++-------------- 3 files changed, 8 insertions(+), 20 deletions(-) diff --git a/arch/arm/kernel/hw_breakpoint.c b/arch/arm/kernel/hw_breakpoin= t.c index dc0fb7a81371..054e9199f30d 100644 --- a/arch/arm/kernel/hw_breakpoint.c +++ b/arch/arm/kernel/hw_breakpoint.c @@ -626,7 +626,7 @@ int hw_breakpoint_arch_parse(struct perf_event *bp, hw->address &=3D ~alignment_mask; hw->ctrl.len <<=3D offset; =20 - if (uses_default_overflow_handler(bp)) { + if (is_default_overflow_handler(bp)) { /* * Mismatch breakpoints are required for single-stepping * breakpoints. @@ -798,7 +798,7 @@ static void watchpoint_handler(unsigned long addr, unsi= gned int fsr, * Otherwise, insert a temporary mismatch breakpoint so that * we can single-step over the watchpoint trigger. */ - if (!uses_default_overflow_handler(wp)) + if (!is_default_overflow_handler(wp)) continue; step: enable_single_step(wp, instruction_pointer(regs)); @@ -811,7 +811,7 @@ static void watchpoint_handler(unsigned long addr, unsi= gned int fsr, info->trigger =3D addr; pr_debug("watchpoint fired: address =3D 0x%x\n", info->trigger); perf_bp_event(wp, regs); - if (uses_default_overflow_handler(wp)) + if (is_default_overflow_handler(wp)) enable_single_step(wp, instruction_pointer(regs)); } =20 @@ -886,7 +886,7 @@ static void breakpoint_handler(unsigned long unknown, s= truct pt_regs *regs) info->trigger =3D addr; pr_debug("breakpoint fired: address =3D 0x%x\n", addr); perf_bp_event(bp, regs); - if (uses_default_overflow_handler(bp)) + if (is_default_overflow_handler(bp)) enable_single_step(bp, addr); goto unlock; } diff --git a/arch/arm64/kernel/hw_breakpoint.c b/arch/arm64/kernel/hw_break= point.c index 35225632d70a..db2a1861bb97 100644 --- a/arch/arm64/kernel/hw_breakpoint.c +++ b/arch/arm64/kernel/hw_breakpoint.c @@ -654,7 +654,7 @@ static int breakpoint_handler(unsigned long unused, uns= igned long esr, perf_bp_event(bp, regs); =20 /* Do we need to handle the stepping? */ - if (uses_default_overflow_handler(bp)) + if (is_default_overflow_handler(bp)) step =3D 1; unlock: rcu_read_unlock(); @@ -733,7 +733,7 @@ static u64 get_distance_from_watchpoint(unsigned long a= ddr, u64 val, static int watchpoint_report(struct perf_event *wp, unsigned long addr, struct pt_regs *regs) { - int step =3D uses_default_overflow_handler(wp); + int step =3D is_default_overflow_handler(wp); struct arch_hw_breakpoint *info =3D counter_arch_bp(wp); =20 info->trigger =3D addr; diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 312b9f31442c..7fef6299151b 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1321,8 +1321,9 @@ extern int perf_event_output(struct perf_event *event, struct pt_regs *regs); =20 static inline bool -__is_default_overflow_handler(perf_overflow_handler_t overflow_handler) +is_default_overflow_handler(struct perf_event *event) { + perf_overflow_handler_t overflow_handler =3D event->overflow_handler; if (likely(overflow_handler =3D=3D perf_event_output_forward)) return true; if (unlikely(overflow_handler =3D=3D perf_event_output_backward)) @@ -1330,19 +1331,6 @@ __is_default_overflow_handler(perf_overflow_handler_= t overflow_handler) return false; } =20 -#define is_default_overflow_handler(event) \ - __is_default_overflow_handler((event)->overflow_handler) - -#ifdef CONFIG_BPF_SYSCALL -static inline bool uses_default_overflow_handler(struct perf_event *event) -{ - return is_default_overflow_handler(event); -} -#else -#define uses_default_overflow_handler(event) \ - is_default_overflow_handler(event) -#endif - extern void perf_event_header__init_id(struct perf_event_header *header, struct perf_sample_data *data, --=20 2.34.1 From nobody Tue Dec 16 16:41:32 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 C2888C4167B for ; Mon, 11 Dec 2023 04:56:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233155AbjLKE4A (ORCPT ); Sun, 10 Dec 2023 23:56:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232996AbjLKEzw (ORCPT ); Sun, 10 Dec 2023 23:55:52 -0500 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A6D82F3 for ; Sun, 10 Dec 2023 20:55:58 -0800 (PST) Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-1cfb30ce241so35598025ad.0 for ; Sun, 10 Dec 2023 20:55:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1702270558; x=1702875358; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BoADJIsaU4bvjYI7gdqH4FQT1qltoRFf88+tkPG8tZk=; b=TKGWwN1BMSfkcarzFN58mcceccPtTKo8f+lsaSmGESfSjcWXVzeVturjsETQBsbmFx KoFzR0qSE93M3dMBH8rwryOSXnsHsy/ktAVjRiH8ZWThdUmvvFdSo6m+o39XW1tLokql TZZCzt2UKeWBkgUg15enpKP+v+8vlN4oR/uaWp52O7LdaqWQJXf99XOf2xc90YSpbfX/ aPRFQdKIKvUP+O25NwGvrJ6cywElGOKbvQQ81ttlMgK29w2zSm3KQ3o7B7Xo9Vlw2xSC 9INEfPcO6VTPC4wEaYbnmjqIO922djMFHBBlTQ/kUX5QYXtleLSNqrGZzCy3toqswhN1 an9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702270558; x=1702875358; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BoADJIsaU4bvjYI7gdqH4FQT1qltoRFf88+tkPG8tZk=; b=Xam5YCf9yzFyc1bsPZwqv895/VrZOOC/xUqFVJBZ3MRMiZugydjTPZFkQwfrIRp2Nm QLauyPQXMxVowvxNFDX9q/OIYOV8dYFByhNiDTWRImggzjp+jiuGDL7mZJTrZLeqh91C GqAmHqZ901Ml5QiTdE3OsHNrdqxKmIZbp6i0dHJ0tq+peDNLaBYTcPZY1TBY+woEODNh hMQLxx22LPXyKetQs8W6/CQwNBJ1WDBcor2kM8tAfLI0ccQVp1gujd2DipkNUEuckxbO gbHg3xWo08jGCCxwrCCsryk5U3M8PR9H5T6KhfGgPZjzCYXwYOHyr9s0lfyM1Ejja+Q4 B/AQ== X-Gm-Message-State: AOJu0YzJmpBkOxZIfkB07cyYaMqU+JV0yk+464800a8hqrykQ0lye/wv ldYgxqaL6YKtTY6LGOpZ+TbvbQ== X-Google-Smtp-Source: AGHT+IHW/xB8+qfj2EAC1FWaRV9BV7vEy4h+GdJx19JjFNP+gJ4a3gVBh6F+eeS7FTFAgLsaSJA/jg== X-Received: by 2002:a17:902:bc84:b0:1d2:e976:bd21 with SMTP id bb4-20020a170902bc8400b001d2e976bd21mr3501188plb.94.1702270558222; Sun, 10 Dec 2023 20:55:58 -0800 (PST) Received: from zhadum.home.kylehuey.com (c-76-126-33-191.hsd1.ca.comcast.net. [76.126.33.191]) by smtp.gmail.com with ESMTPSA id e11-20020a170902b78b00b001d2ffeac9d3sm3300623pls.186.2023.12.10.20.55.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Dec 2023 20:55:57 -0800 (PST) From: Kyle Huey X-Google-Original-From: Kyle Huey To: Kyle Huey , linux-kernel@vger.kernel.org, Andrii Nakryiko , Jiri Olsa , Namhyung Kim , Marco Elver , Yonghong Song Cc: Robert O'Callahan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH v3 3/4] perf/bpf: Allow a bpf program to suppress all sample side effects Date: Sun, 10 Dec 2023 20:55:42 -0800 Message-Id: <20231211045543.31741-4-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231211045543.31741-1-khuey@kylehuey.com> References: <20231211045543.31741-1-khuey@kylehuey.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Returning zero from a bpf program attached to a perf event already suppresses any data output. Return early from __perf_event_overflow() in this case so it will also suppress event_limit accounting, SIGTRAP generation, and F_ASYNC signalling. Signed-off-by: Kyle Huey Acked-by: Namhyung Kim Acked-by: Song Liu --- kernel/events/core.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 54f6372d2634..d6093fe893c8 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9541,6 +9541,11 @@ static int __perf_event_overflow(struct perf_event *= event, =20 ret =3D __perf_event_account_interrupt(event, throttle); =20 +#ifdef CONFIG_BPF_SYSCALL + if (event->prog && !bpf_overflow_handler(event, data, regs)) + return ret; +#endif + /* * XXX event_limit might not quite work as expected on inherited * events @@ -9590,10 +9595,7 @@ static int __perf_event_overflow(struct perf_event *= event, irq_work_queue(&event->pending_irq); } =20 -#ifdef CONFIG_BPF_SYSCALL - if (!(event->prog && !bpf_overflow_handler(event, data, regs))) -#endif - READ_ONCE(event->overflow_handler)(event, data, regs); + READ_ONCE(event->overflow_handler)(event, data, regs); =20 if (*perf_event_fasync(event) && event->pending_kill) { event->pending_wakeup =3D 1; --=20 2.34.1 From nobody Tue Dec 16 16:41:32 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 B0597C4167B for ; Mon, 11 Dec 2023 04:57:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233216AbjLKE47 (ORCPT ); Sun, 10 Dec 2023 23:56:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233435AbjLKE4c (ORCPT ); Sun, 10 Dec 2023 23:56:32 -0500 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42A48198 for ; Sun, 10 Dec 2023 20:56:30 -0800 (PST) Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1d053c45897so35744605ad.2 for ; Sun, 10 Dec 2023 20:56:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1702270590; x=1702875390; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=f/uOwN8/AYKX/iTSNcMtLhs7X+zpyhpj6oRruBreRa4=; b=MKSRvC1OxyjdP7zrUzYvQsbx6mJ74RpWWf1U6sy8Yn3lqxzf5ZqLSo8OeWz9ipEDm6 7W7bubSjoonHVa4aoZuJvUXxlxiUCQkvjbMsCUjiQX98riXNOgWNNrUgoug0opr7JOQQ MUMj8QHvTosq/9WO6urCr8IX49gUENbTMs55EhHuR3IjdOjBAT4DXImbI9Oavf+vvOO2 WknaPCL1RxrYAILMHixVA5da0s7EndwQBJlcoi7Gw6gueeWJvWA8+3JZ4OtW1IkxzQQY p6c5tr6SHhBtT64l0sAoQrMp4q+UTPBIZefdVufgHcLstbOepIhZ6+xMu4UP3/Chwoa3 nL1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702270590; x=1702875390; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=f/uOwN8/AYKX/iTSNcMtLhs7X+zpyhpj6oRruBreRa4=; b=oBxUafeRQ46tH5+wde/j1hSOzzLgxylo1pBZZIv5JXwoMY4CPo3XHL7gPOQATc1+Ix tKyFy5GUjRBiao9T+1m655DXRo/abXSA0sZ5B2K4lZXCROAazf8JeW3MV5H4Ewcv3p7p rDSmTe8cpzPno2ToRNBylRmWegOtSVlxxNCVdww35xc1ZHGht5aktHUp1nIbMyYGa6sc yEfBO1QBhcIZwlxXduDeACuyv8ISJSPK6Vya+NEt4JjO1P0hb/Y6gkRMxziFn4sfFmT4 g2R73r5f3cyorUCAxxKMEPFWZjLsv5aYO53WIGyptofjH18O/oQt6PxMBQmnDtXw3AJX br7Q== X-Gm-Message-State: AOJu0Ywiz39VDZk80G6fqorXxDsFTurks951Kz7L6QoJfufqVrbdcTly 7VnfPHjakIg72m/4oKMX37bCYD1MqLt63oYnQUl2eg== X-Google-Smtp-Source: AGHT+IHgP24C9H6TC1qkhK0WnVUgjdIKQD/L+UmDGrOm9ocb3UIOQduZF38TYB04xwZtzp9Bl64vMA== X-Received: by 2002:a17:902:c3d1:b0:1d0:d168:daae with SMTP id j17-20020a170902c3d100b001d0d168daaemr4338646plj.95.1702270589707; Sun, 10 Dec 2023 20:56:29 -0800 (PST) Received: from zhadum.home.kylehuey.com (c-76-126-33-191.hsd1.ca.comcast.net. [76.126.33.191]) by smtp.gmail.com with ESMTPSA id e11-20020a170902b78b00b001d2ffeac9d3sm3300623pls.186.2023.12.10.20.56.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Dec 2023 20:56:29 -0800 (PST) From: Kyle Huey X-Google-Original-From: Kyle Huey To: Kyle Huey , linux-kernel@vger.kernel.org, Andrii Nakryiko , Jiri Olsa , Namhyung Kim , Marco Elver , Yonghong Song Cc: Robert O'Callahan , Andrii Nakryiko , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Shuah Khan , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v3 4/4] selftest/bpf: Test a perf bpf program that suppresses side effects. Date: Sun, 10 Dec 2023 20:55:43 -0800 Message-Id: <20231211045543.31741-5-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231211045543.31741-1-khuey@kylehuey.com> References: <20231211045543.31741-1-khuey@kylehuey.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The test sets a hardware breakpoint and uses a bpf program to suppress the side effects of a perf event sample, including I/O availability signals, SIGTRAPs, and decrementing the event counter limit, if the ip matches the expected value. Then the function with the breakpoint is executed multiple times to test that all effects behave as expected. Signed-off-by: Kyle Huey Acked-by: Namhyung Kim --- .../selftests/bpf/prog_tests/perf_skip.c | 140 ++++++++++++++++++ .../selftests/bpf/progs/test_perf_skip.c | 15 ++ 2 files changed, 155 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/perf_skip.c create mode 100644 tools/testing/selftests/bpf/progs/test_perf_skip.c diff --git a/tools/testing/selftests/bpf/prog_tests/perf_skip.c b/tools/tes= ting/selftests/bpf/prog_tests/perf_skip.c new file mode 100644 index 000000000000..0200736a8baf --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/perf_skip.c @@ -0,0 +1,140 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE + +#include +#include "test_perf_skip.skel.h" +#include +#include +#include + +#ifndef TRAP_PERF +#define TRAP_PERF 6 +#endif + +int signals_unexpected =3D 1; +int sigio_count, sigtrap_count; + +static void handle_sigio(int sig __always_unused) +{ + ASSERT_OK(signals_unexpected, "perf event not skipped"); + ++sigio_count; +} + +static void handle_sigtrap(int signum __always_unused, + siginfo_t *info, + void *ucontext __always_unused) +{ + ASSERT_OK(signals_unexpected, "perf event not skipped"); + ASSERT_EQ(info->si_code, TRAP_PERF, "wrong si_code"); + ++sigtrap_count; +} + +static noinline int test_function(void) +{ + asm volatile (""); + return 0; +} + +void serial_test_perf_skip(void) +{ + struct sigaction action =3D {}; + struct sigaction previous_sigtrap; + sighandler_t previous_sigio; + struct test_perf_skip *skel =3D NULL; + struct perf_event_attr attr =3D {}; + int perf_fd =3D -1; + int err; + struct f_owner_ex owner; + struct bpf_link *prog_link =3D NULL; + + action.sa_flags =3D SA_SIGINFO | SA_NODEFER; + action.sa_sigaction =3D handle_sigtrap; + sigemptyset(&action.sa_mask); + if (!ASSERT_OK(sigaction(SIGTRAP, &action, &previous_sigtrap), "sigaction= ")) + return; + + previous_sigio =3D signal(SIGIO, handle_sigio); + + skel =3D test_perf_skip__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_load")) + goto cleanup; + + attr.type =3D PERF_TYPE_BREAKPOINT; + attr.size =3D sizeof(attr); + attr.bp_type =3D HW_BREAKPOINT_X; + attr.bp_addr =3D (uintptr_t)test_function; + attr.bp_len =3D sizeof(long); + attr.sample_period =3D 1; + attr.sample_type =3D PERF_SAMPLE_IP; + attr.pinned =3D 1; + attr.exclude_kernel =3D 1; + attr.exclude_hv =3D 1; + attr.precise_ip =3D 3; + attr.sigtrap =3D 1; + attr.remove_on_exec =3D 1; + + perf_fd =3D syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0); + if (perf_fd < 0 && (errno =3D=3D ENOENT || errno =3D=3D EOPNOTSUPP)) { + printf("SKIP:no PERF_TYPE_BREAKPOINT/HW_BREAKPOINT_X\n"); + test__skip(); + goto cleanup; + } + if (!ASSERT_OK(perf_fd < 0, "perf_event_open")) + goto cleanup; + + /* Configure the perf event to signal on sample. */ + err =3D fcntl(perf_fd, F_SETFL, O_ASYNC); + if (!ASSERT_OK(err, "fcntl(F_SETFL, O_ASYNC)")) + goto cleanup; + + owner.type =3D F_OWNER_TID; + owner.pid =3D syscall(__NR_gettid); + err =3D fcntl(perf_fd, F_SETOWN_EX, &owner); + if (!ASSERT_OK(err, "fcntl(F_SETOWN_EX)")) + goto cleanup; + + /* + * Allow at most one sample. A sample rejected by bpf should + * not count against this. + */ + err =3D ioctl(perf_fd, PERF_EVENT_IOC_REFRESH, 1); + if (!ASSERT_OK(err, "ioctl(PERF_EVENT_IOC_REFRESH)")) + goto cleanup; + + prog_link =3D bpf_program__attach_perf_event(skel->progs.handler, perf_fd= ); + if (!ASSERT_OK_PTR(prog_link, "bpf_program__attach_perf_event")) + goto cleanup; + + /* Configure the bpf program to suppress the sample. */ + skel->bss->ip =3D (uintptr_t)test_function; + test_function(); + + ASSERT_EQ(sigio_count, 0, "sigio_count"); + ASSERT_EQ(sigtrap_count, 0, "sigtrap_count"); + + /* Configure the bpf program to allow the sample. */ + skel->bss->ip =3D 0; + signals_unexpected =3D 0; + test_function(); + + ASSERT_EQ(sigio_count, 1, "sigio_count"); + ASSERT_EQ(sigtrap_count, 1, "sigtrap_count"); + + /* + * Test that the sample above is the only one allowed (by perf, not + * by bpf) + */ + test_function(); + + ASSERT_EQ(sigio_count, 1, "sigio_count"); + ASSERT_EQ(sigtrap_count, 1, "sigtrap_count"); + +cleanup: + bpf_link__destroy(prog_link); + if (perf_fd >=3D 0) + close(perf_fd); + test_perf_skip__destroy(skel); + + signal(SIGIO, previous_sigio); + sigaction(SIGTRAP, &previous_sigtrap, NULL); +} diff --git a/tools/testing/selftests/bpf/progs/test_perf_skip.c b/tools/tes= ting/selftests/bpf/progs/test_perf_skip.c new file mode 100644 index 000000000000..7eb8b6de7a57 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_perf_skip.c @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "vmlinux.h" +#include +#include + +uintptr_t ip; + +SEC("perf_event") +int handler(struct bpf_perf_event_data *data) +{ + /* Skip events that have the correct ip. */ + return ip !=3D PT_REGS_IP(&data->regs); +} + +char _license[] SEC("license") =3D "GPL"; --=20 2.34.1