From nobody Sun Oct 26 22:20:49 2025 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 98CBD63D8 for ; Mon, 22 Jan 2024 06:25:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705904747; cv=none; b=knJ0ytAAaViLkqoNRz5ypwdbBtP6mUB7ZJcRxvYRc6py7bKiWCPS20pvun410bODPhH4xzD++w54JAJHI8cGEnZ5jdAtPXUiiiCJFM+bvRHl3nUXV8QDEE00cFMSCbGPdVYzuURoC2CNyihPXc9AjPXclidZM2D3fXNRRayWhRg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705904747; c=relaxed/simple; bh=BIEPrj8REzhp6IWPrzPDboiwJzkYVpJOVlkhzZJPWM0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=c06v10vJwszan8zZLYMdr2thH3u3JYTbuTvsySieiJa1jkwZsLiSjMeCgW1uzZjuuvazBFh3odcWE7fhNrZRuHSrawda4UHVa6KVgq8+HPTP/lISy9KZY5CnIWWR4zIiReOJT1tv7ukaNRu23WqJDgPuv07qamNEefrkRbdXIeU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylehuey.com; spf=pass smtp.mailfrom=kylehuey.com; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b=RuPZaiMO; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylehuey.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylehuey.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="RuPZaiMO" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1d75c97ea6aso1453375ad.1 for ; Sun, 21 Jan 2024 22:25:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1705904746; x=1706509546; 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=43jjz2Dnzl3sNSNdkk4MoD6bg1Ygi5eixyO/ZKWqbHM=; b=RuPZaiMO68P3w575sh+zLwFuFvTts15y9QC7sVQoHxZIH5xDd8V56m3T3cKdRRrlM1 zNpWbFItcw9wyI0BqrO+86MAaUUvSXLS3tD2Zyz0r/4U3ExHijXxFxDMbSEMTBljFDan //bKz5cO75A9MozGw48QeGGOAngHszMfjue5GXNZ75FnEmuZtlttID75n7UBjydgrImM 8o7LJlrtBGXGNVok69wyZliPh5IbCGX39xP8LR3Jl7972yTCUFYYeXgqX8jbJFxQAPs0 DEcoe/Wu/n7JjBC5abH9VTtMdH+Ksfpt+DfjPGCWWtKxXfdWWeFMz5PB4dQ8oDiem4JC hFDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705904746; x=1706509546; 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=43jjz2Dnzl3sNSNdkk4MoD6bg1Ygi5eixyO/ZKWqbHM=; b=dt7rGntbct0uMP5iXaeN96HZ18btd9h9aMFX+mIjE1iIkDM7XKP417skrZdb62H2We o7JXzpFsSZvT+IIB6f5hT0eqLVSM/Yx2luR24ibTFY+Od8cUlikir9QsA+kRy4GbGzZC oTFajIomLQpzGeSHFIve+z7oBqjspdVNJS9vC5odB2/SYasUFrSgFr3RMLEQkMKyqLye WN0eSvTnDJ2SuYCgmlM8g9gX9Xirjv+w28oRt/9vnwaN89ObZWTViQbHiZcKXfJeN8/m K4VVA6eI2WqPbB9fiqu+gfA0GkM1pMaX53ZhQImx/wh2Q6V5eaInlzxoF5mBzPDtrkcE rT8Q== X-Gm-Message-State: AOJu0YwhiGSVSMR41edxJMhoE6xvvlqezeL4TPH561eOHn2duJvPDopM jlu/TWhOV2dEsxZHTFnaLQ3NZNpTEuz1cNbdP0sLugz5aWxMYzQr12YfAPlF7g== X-Google-Smtp-Source: AGHT+IEomlRroVz8nzSmtKhcaaGBs692domhzxkZ5V7+Xote4F1HRdHF+uUtdXnAAOCKYpbULE+7SA== X-Received: by 2002:a17:903:2447:b0:1d6:ff29:1007 with SMTP id l7-20020a170903244700b001d6ff291007mr4865690pls.43.1705904746052; Sun, 21 Jan 2024 22:25:46 -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 u5-20020a17090282c500b001d7248fdc26sm4317771plz.69.2024.01.21.22.25.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 22:25:45 -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 , Song Liu , 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 v5 1/4] perf/bpf: Call bpf handler directly, not through overflow machinery Date: Sun, 21 Jan 2024 22:25:32 -0800 Message-Id: <20240122062535.8265-2-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240122062535.8265-1-khuey@kylehuey.com> References: <20240122062535.8265-1-khuey@kylehuey.com> 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" 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: Song Liu Acked-by: Jiri Olsa --- 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 d2a15c0c6f8a..c7f54fd74d89 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 @@ -1357,10 +1356,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 f0f0f71213a1..24a718e7eb98 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9548,6 +9548,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. */ @@ -9617,7 +9623,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; @@ -10427,9 +10436,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, @@ -10450,10 +10459,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, @@ -10489,8 +10496,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 @@ -10501,7 +10506,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); } @@ -11975,13 +11979,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 Sun Oct 26 22:20:49 2025 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E0F88E574 for ; Mon, 22 Jan 2024 06:25:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705904750; cv=none; b=J0eWcxiI2hLTrkiqQk+YeCkDccqrTjHjIPPD5UMHEmDcHwu4DFn6Gs4yNwUgHEUe+ofymBTwXhzieq7IDksOS+1D/WKNN1lQmnHJvehckThpmEAmjSIbQlKo0f0BAPrhfvzlrJ6cdKHbyMLSzu+KHV1R5T57Qp0VDkoqVDvm8ho= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705904750; c=relaxed/simple; bh=zUABpjG2ub173w63GMgMTw18XVCHIXF84Q0edlKhxyE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IUG+FgwEXCK1L1hjxNrqpPEQt8uW/2Rgec3XCkNYA7juDDTuhUkrdey3abcMLRjhkqjeGduzt98JIpEmFa4wQswaucn2kCsnB8o3bx+ZdjzK5aBZuISxqpD3rMnwYc3yvBrWYSSf6ElMWOBSfd/6NnRHwL4PsO9om3HpaWNlx+U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylehuey.com; spf=pass smtp.mailfrom=kylehuey.com; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b=RTy927LL; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylehuey.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylehuey.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="RTy927LL" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-6da9c834646so2781209b3a.3 for ; Sun, 21 Jan 2024 22:25:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1705904748; x=1706509548; 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=/ElBvlPVPDuFmuEeY/T4KuG3RaPIc1GsemQ8p6vqiM4=; b=RTy927LLXVs76d/VyuSlhyqkSoD5DkummxE4BRehN73Ts9ZXlMsFN8BvxI61jk4gtn R3uB27KZ+XFM8KYfcc6sE+lvnK6DysgpQQRypffQx19AWb7h7ybUIYR2Bs6kd1KIoPSw ApnJH+ONrt1QULXAZvH+/7l6h++ae1FDr6QDNJp+Yxb/9oJ3HGYtL0qwACzXif+EG7LU L3TzLlm0PSqAMrkXzOpyct27m46y2UC0VD9OR35uI47+pfhW/FUv0IGCxJkcbDcvHOr/ aUQwFtzZlehkvvlfisVRZgAnHcNvHPgrXOXL3DjAgf/O0sCYxjClS5gKUDpdiru7okrh F9Ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705904748; x=1706509548; 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=/ElBvlPVPDuFmuEeY/T4KuG3RaPIc1GsemQ8p6vqiM4=; b=ljjYkoejCOf5chovBShmEZiCxzfyAsJ/7od7Ztpj8VSXWfC7LmvQQT+wyXd1v1vZ/C RLZNVzgntlB4S6Nlw0cc0qv3xSSn1+kAGvc1WXnGxMCEdH2fWc1iijb/zudpL7vBHE7H gWlfsjimuSFGsU7CcB0oYWNuEE2Ji7mMhEwh/LGYd9OiiN+h3d/uEIVKxNao1zi5x6QS dLh/R2QItItWBz6kSKoq5FEJczmvUn6zEzEFcumtBtPgXAhIvYHJ2gVFLrPdth71Q68Z dpduLdmkSuN9mQFLV2/tRKDgYfUMeuSRs3W0Dm7ALiR0H7QhC1yCuRmIcDt2ERrJWfth FSHg== X-Gm-Message-State: AOJu0Ywk1/+N8NTukeAdCuI4m8cgcAjexbBTz56wqS/KIUoBQB1IvR37 uePnTM+4LLeLKOEPMuhY7Swe2uf6yZLnvh3heN+Opu6XT7iqnmsZ4NLG/OwYvQ== X-Google-Smtp-Source: AGHT+IFNhK4hrreFjeqPVfWLlMWp5Le0zf03LGw1fEZbxchHNJzGczoxBsff0ha/xPdrnL5Hpq48uA== X-Received: by 2002:a05:6a20:4286:b0:199:8d8a:a746 with SMTP id o6-20020a056a20428600b001998d8aa746mr5231644pzj.29.1705904748284; Sun, 21 Jan 2024 22:25:48 -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 u5-20020a17090282c500b001d7248fdc26sm4317771plz.69.2024.01.21.22.25.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 22:25:47 -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 , Song Liu , 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 v5 2/4] perf/bpf: Remove unneeded uses_default_overflow_handler. Date: Sun, 21 Jan 2024 22:25:33 -0800 Message-Id: <20240122062535.8265-3-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240122062535.8265-1-khuey@kylehuey.com> References: <20240122062535.8265-1-khuey@kylehuey.com> 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" 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: Will Deacon Acked-by: Song Liu Acked-by: Jiri Olsa --- 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 c7f54fd74d89..c8bd5bb6610c 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1341,8 +1341,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)) @@ -1350,19 +1351,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 Sun Oct 26 22:20:49 2025 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A503FBF3 for ; Mon, 22 Jan 2024 06:25:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705904752; cv=none; b=YGgUA1lxK3PHY17Az+VL7+NWtZimu2/ZeP/qY0AwAyKvcYfsMGGKWkiKtbN5pngt5wrYR8MDRvYjIfNxSbNUThk1L99V8Z15oZp5xNPs2FiM1I2KBeIWtu4x0VYh5S4Y0blh/G5PFi+lYoHt4oYMAJC1CX8EPR2WqZawWGF8AQY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705904752; c=relaxed/simple; bh=PX2QIRe/Ic85BaGVX4Y1MwT3KJ9EiLQ96IEJSBjyAmE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JMfzCaS5wv3W8Pxk8k/rZfWYYa7p2vITVAPTStjyY4X4H7p2LVgAhcM0LVhSujhUosZssgCh7m9/pLrL9frkqeL1jRwxCDrwkOwejt2zjaDXol4J5wzfozlixJQqGP/YNK+6E7/hgj4ljaA8Nb7HRr5wfCQTnKkNErs7KbDuANo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylehuey.com; spf=pass smtp.mailfrom=kylehuey.com; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b=I6iPxPpg; arc=none smtp.client-ip=209.85.214.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylehuey.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylehuey.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="I6iPxPpg" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1d71c844811so9544675ad.3 for ; Sun, 21 Jan 2024 22:25:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1705904750; x=1706509550; 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=HG1Z9345f9l/NkzG8Y/mlPsfRpv6azyFKs0IWGP/jfI=; b=I6iPxPpgibdZmdGTFHQ3YWOZDl3GyH3s2NYG0Rx4+Cih6gJ2JsArkBciumk59fxrKK nh8SfP5VylN6We9SpQ+vRuAdIvEExLSZq8JjOxZL5cG9QgWqzTkHWgEk4qj/9m6dpv/3 U5yUIOXndWH5zVsn8GIvzjDKwa9Jfbhfp038ORPkmqovMxvEtNdQS5flBVozKyEQU8vj rw7n4OPzlPYYV2heM6g6J5kzvrDC8iw616N/YVefDFcKxmuG35M9UBLbpLMIxtdI7aTK 1Ovhco2IlJeGpvV/vSp3elrt2fdahanZz7Su/Lz0RM/X86wi0Uhop1xFimDz47oh6RsR iClg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705904750; x=1706509550; 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=HG1Z9345f9l/NkzG8Y/mlPsfRpv6azyFKs0IWGP/jfI=; b=sv4zIfdcTXdoKyelQ+LZreSpK5cvBkVdaRyR0zhe/sc7yFJdbBOrQmUQaSqnO6Wa00 36osu2MmXbetGiiLE8LC+WjRjzivaETMq58mnSdbGdiWcxBiP5xwoV8MhlW4CMyhc4I6 YfZ7jOIAOOyrrQJAW5EIzOvUrdDB0enJ+QLP5uy/fYgxiua8LFUou0N9J7srA4t0fG6L vC0m+isFz3varmiBTdvSExDr/4VTyMCTsymlG5DGDp2oi2LyqhJksAyze6kSs5jxxsaw SAe8xaOwlcwM6/VUyr2nUoqG3saqIPRuobcLhVvWhRABKOskUPwfWJJxujKNMVB/D4JT Y4Mw== X-Gm-Message-State: AOJu0YwSyDQAssotIIWwZgm1bAyvWYXB9bCMmVRjou7aciE7HioNtVA6 3SUezXmvXWReI7nArZcjEgk67sQ0G+YiWep4G985r+V+xL37gNeawiRIVYR7WA== X-Google-Smtp-Source: AGHT+IGjwwlnVe73yDG0DhM2YMf89F/r9yfjLGi1/9gc8G0ZQeVTjIN2iKAG0BT0jk+WB4Z4UIUUgA== X-Received: by 2002:a17:902:e5c5:b0:1d7:4060:78b3 with SMTP id u5-20020a170902e5c500b001d7406078b3mr821096plf.39.1705904750581; Sun, 21 Jan 2024 22:25:50 -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 u5-20020a17090282c500b001d7248fdc26sm4317771plz.69.2024.01.21.22.25.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 22:25:50 -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 , Song Liu , 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 v5 3/4] perf/bpf: Allow a bpf program to suppress all sample side effects Date: Sun, 21 Jan 2024 22:25:34 -0800 Message-Id: <20240122062535.8265-4-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240122062535.8265-1-khuey@kylehuey.com> References: <20240122062535.8265-1-khuey@kylehuey.com> 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" 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: Song Liu Acked-by: Jiri Olsa Acked-by: Namhyung Kim --- 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 24a718e7eb98..a329bec42c4d 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9574,6 +9574,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 @@ -9623,10 +9628,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 Sun Oct 26 22:20:49 2025 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D60ADF63 for ; Mon, 22 Jan 2024 06:26:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705904784; cv=none; b=ASuuQeZQCFM9/zljTbgsKd5TcsUeeAA/n09V9kfFYHK/zjOIf/NqYqesyg6EnqOTzu/Kifgi5EffnYNnn24iHgzTPaSqTRqk0m4xbLacjeq6AjX1Ruc/BlcudpTWgwfjVFHpcO0CKG2Fs/jFw+1LZakx79o/miak+wlgUMcQO2M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705904784; c=relaxed/simple; bh=nomzOqG8qtQlSxsBxKy5X1xMNj4S0dr5Ifd+oiSg+rg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=s/xebZ/lA/+y721/Fkp5BIWsx1//0GU9cUZCvvpaPIiXU86Vpt4YddsuRRs3CxVN230tKzSm1H0HLyoJiMZN/T4iybYqUoZTpkajIlwPOLylF6C4EhKPS/4xC1Iei60/4x5qUBIyngRSqLDxoBDrYi1M+Baf3gNNUvacFEe0DdA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylehuey.com; spf=pass smtp.mailfrom=kylehuey.com; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b=azHSEMWX; arc=none smtp.client-ip=209.85.216.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylehuey.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylehuey.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="azHSEMWX" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-2907a17fa34so837691a91.1 for ; Sun, 21 Jan 2024 22:26:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1705904782; x=1706509582; 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=EjezFELGsG5yYdoO8o3jjgWXUKsZRmSJ3eM36KaKRP0=; b=azHSEMWXVXCyiT3KzyyLiDGu21a8ZywlvbfIN+iQ78xfouPHJ2gvFntL1d7k+s0C/w tsjVC0A+OqD9LaqnJIqSc1ptdo2VJY4m+AsbS8UfpIttA+gAX0Zt+7pgK7aDSivAXXY8 v3lXHXYsnFJ5CFYAZyDe7zr+MzdT0rumHMjqNXEEHwbziR+wr+P1PUEgAlzpURfiybVu vmjFLXrr5n9C9TBmR5j9U72KBZ914xzeVZPw4beE7s68BnQ4BRYQCpZ5ETDAyNAaGCl5 S3JPWR4Rhb5MHbRQssWQCoqCFozBTgEgr/65FabKYplFw5TFmNrOv3WYKMDZl+fil49U qeAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705904782; x=1706509582; 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=EjezFELGsG5yYdoO8o3jjgWXUKsZRmSJ3eM36KaKRP0=; b=psCnKwXRd6IHThkYEmXRMyW3GwiVl0+LTsu2V1iNFfwZZ7oYlsaAPvwRH/8cidlLgL ImwFZ0qPIBwic7fgmI1WlbpHYxIbAbsWAz73WGAHhGGYXOtfSjAtLl31Oj0FsF2/UCej VcOrZtxFpxL2IRbpMJEonhm2oilaX/A4t3hDjzofbgWm4AZqNcI9V1ryF/pRk+BBxCDv SLnelHctH67JonvxuyJia5dkRJbUxc8q2yo4fk+L8Q1/pjm1x3f80x02l+zK9P7HJ5CV 9uya9Mu6w95UyfO0zkAbnJKRo78/ArPrhrDiX3V2JL0dWKBR18BcEoAAOMESnAVoJ8BK NkyA== X-Gm-Message-State: AOJu0Yyy64dbvoSLCyOP5PXDzLvu86Ink89gEh37CCSp1wi35WwOWbOp salRdjwgGwPyrHzDVFOSorcu3AxHJZSdHLww/WbRm6ulLyqD3VDD4GCgtAjmqw== X-Google-Smtp-Source: AGHT+IFMW888K8jWC9UqxrFXPqdvN8EA+d82Vk2Kae5kOYRMvM5HyvYhydsHUbRAopyqCX5czH23dg== X-Received: by 2002:a17:90a:d384:b0:28e:8210:ddbc with SMTP id q4-20020a17090ad38400b0028e8210ddbcmr1483228pju.20.1705904781945; Sun, 21 Jan 2024 22:26:21 -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 u5-20020a17090282c500b001d7248fdc26sm4317771plz.69.2024.01.21.22.26.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 22:26:21 -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 , Song Liu , Andrii Nakryiko , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Shuah Khan , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v5 4/4] selftest/bpf: Test a perf bpf program that suppresses side effects. Date: Sun, 21 Jan 2024 22:25:35 -0800 Message-Id: <20240122062535.8265-5-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240122062535.8265-1-khuey@kylehuey.com> References: <20240122062535.8265-1-khuey@kylehuey.com> 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" 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: Song Liu Acked-by: Jiri Olsa --- .../selftests/bpf/prog_tests/perf_skip.c | 137 ++++++++++++++++++ .../selftests/bpf/progs/test_perf_skip.c | 15 ++ 2 files changed, 152 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..37d8618800e4 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/perf_skip.c @@ -0,0 +1,137 @@ +// 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 sigio_count, sigtrap_count; + +static void handle_sigio(int sig __always_unused) +{ + ++sigio_count; +} + +static void handle_sigtrap(int signum __always_unused, + siginfo_t *info, + void *ucontext __always_unused) +{ + ASSERT_EQ(info->si_code, TRAP_PERF, "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 =3D SIG_ERR; + 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); + if (!ASSERT_NEQ(previous_sigio, SIG_ERR, "signal")) + goto cleanup; + + 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; + 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); + + if (previous_sigio !=3D SIG_ERR) + 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