From patchwork Mon Jan 22 06:25:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 189925 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp2402244dyb; Sun, 21 Jan 2024 22:52:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IFjHUvyNnyqdk9ZD8r/7LTIMhIu8qwQ6p8Lix8yEBHZMTa0Bhzown8rMRK6VboB1WQqkVSy X-Received: by 2002:a17:90a:c387:b0:28f:22fc:e853 with SMTP id h7-20020a17090ac38700b0028f22fce853mr4376704pjt.46.1705906369312; Sun, 21 Jan 2024 22:52:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705906369; cv=pass; d=google.com; s=arc-20160816; b=0Liw8DqpteS6W93h8KQCO/3vUUQ6wUQfYUcbY0DzUOuTDZ1wTqT94O+Ru4mMGm1rGD Ukylw6LT4/ENB8oONFw5hvNl70NNbgVcxB1hSKyAGZac3zW/8C57HTevwUjxef6vRvMQ Gtu0ueViG+T3EkwHA5Al0KQkXXB+PPfNfmaerv9JWj7rlwdeaFjMJtfdMjuup4HMSqSo xBBPOXC6CeV0lCkymK6F2ADM2M7BKAHoNGKrKGEFCl95vF47ItW0R7l8QvCZhEPRFhNZ ifBlJWFBEmX8HLtI9n76jML5oi51c4e+Lf4dnKpKSof3lMDEL3AeLsdGpeK4UZtBS0F3 Urew== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=43jjz2Dnzl3sNSNdkk4MoD6bg1Ygi5eixyO/ZKWqbHM=; fh=K5yXAxTssbJLQcrUsVgOEJ74eFXZrEcbr8gMwVXs9zI=; b=NJet+RC/p3dI8ddNf4vv9NqWyutOJoC1OLZAhfMujzKGScwvmx/o2sLqpJGem+hJAN jbKM+B3mfR8ih7gcFW2Ja5w60qh0ky2TNXQi6e6vUOx7ri8B46tdzp3XziM+Zi0K2ZpP POFjyMbWQWe77ojFftbLXUfo2C0HEfmUs/FAT4RgWpNeGPLzLYUggHHxQvYqjZtD/R6A eJZk2+oy8DufCIDKD8v0wxWL6BVMiunfyijvwCwTQt/Mq7Rq5FrVDtUTZDWTY30UtqHQ seDjHdEdLfpq0/XJdvmvWXeKrF81aGZJ8Ihl1ZdR8N6J89HO08cpn3qdmh4D52+rj0Gu 9njg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=RuPZaiMO; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-32383-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32383-ouuuleilei=gmail.com@vger.kernel.org" Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id h22-20020a17090acf1600b0029045eb77a0si5801741pju.150.2024.01.21.22.52.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 22:52:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-32383-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=RuPZaiMO; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-32383-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32383-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 0C5D9B256DF for ; Mon, 22 Jan 2024 06:26:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 38A28FBF6; Mon, 22 Jan 2024 06:25:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="RuPZaiMO" 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 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-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788772477340589614 X-GMAIL-MSGID: 1788772477340589614 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_event *event, struct pt_regs *r return true; } +#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); } - 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); if (*perf_event_fasync(event) && event->pending_kill) { event->pending_wakeup = 1; @@ -10427,9 +10436,9 @@ static void perf_event_free_filter(struct perf_event *event) } #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 = { .data = 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; - event->orig_overflow_handler(event, data, regs); + return ret; } static int perf_event_set_bpf_handler(struct perf_event *event, @@ -10489,8 +10496,6 @@ static int perf_event_set_bpf_handler(struct perf_event *event, event->prog = prog; event->bpf_cookie = bpf_cookie; - event->orig_overflow_handler = READ_ONCE(event->overflow_handler); - WRITE_ONCE(event->overflow_handler, bpf_overflow_handler); return 0; } @@ -10501,7 +10506,6 @@ static void perf_event_free_bpf_handler(struct perf_event *event) if (!prog) return; - WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler); event->prog = NULL; bpf_prog_put(prog); } @@ -11975,13 +11979,11 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu, overflow_handler = parent_event->overflow_handler; context = parent_event->overflow_handler_context; #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING) - if (overflow_handler == bpf_overflow_handler) { + if (parent_event->prog) { struct bpf_prog *prog = parent_event->prog; bpf_prog_inc(prog); event->prog = prog; - event->orig_overflow_handler = - parent_event->orig_overflow_handler; } #endif } From patchwork Mon Jan 22 06:25:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 189916 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp2395045dyb; Sun, 21 Jan 2024 22:26:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IGDCs7EJbR6XbzppKvJ36pe0MryLlVSmtUlKE23NjY+U+D8ypegEsiGsD7dUmhOd7oeUgDf X-Received: by 2002:a05:620a:2207:b0:781:428e:bc0 with SMTP id m7-20020a05620a220700b00781428e0bc0mr4359944qkh.51.1705904814984; Sun, 21 Jan 2024 22:26:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705904814; cv=pass; d=google.com; s=arc-20160816; b=Lnf2189Q8TjOTutAwbKBah0nzBeLfbSUhmSqNZXOechDbItT6BqsPzFnkBbFjduYIO pITWg7nRwr0fAsINFghiMQDqbyMsHaxvWAoVUY0zDS5ctwb/xqGkTHmWvunRpAn7sijN e0++e0G7NhW9Mh4my87AFZqFlxbNh7+gh3PIudMKWfhqqLfVo0NWi6b/8uQcLfj/Ex6y 4Yz5Hf2fsBV3TeVZLM2b2qO9oWKZgLDgQV/c1HJHFLrmT7rng+1kJPV7+Djlsh+BKYwZ aPBW6yMqKq2Q1ZhtL02imVpotl5lRFx7Lh7BNjY7SI78MUvlaJ0BLUgGGtrlFnzc/Nps Z/lA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=/ElBvlPVPDuFmuEeY/T4KuG3RaPIc1GsemQ8p6vqiM4=; fh=vjw2Rz66Gf38vxlMdm/RFQlQyUlkZNx8/VDXhUgsJoI=; b=LVKk8cag6mlB323+U1/zwsnYpNGWPTEBlQ/Pux9wVce01V1aGaKH4+yx9flor2p/Sw rmQuE7Xxhv6fLrBAoJGjSjulQLA/6vI51os50P9jdtR5kbSXG0gPfk4bAtUStLpIptcZ iTrLzgJKPlSOQVTkOlSBld8L3MxaxGmtDnyp+J1fqqKgLj5eIGMyFsZK9fEAGKa4Wlsb M8f4NRQF2wPwwpKyiLBnq+gD8wZlMjkTQvgkru2pvR2STn9/I2Tv5Dl2t5IK6ifQhxl4 MaiQ8wQvK4kd2nggIj1AiYKXy+n5vEwZVkoLHdge9RgzKieESXHDe84HraesWCvv/VzE GfHQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=RTy927LL; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-32384-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32384-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d5-20020a05620a166500b00781457d5f5dsi4942392qko.548.2024.01.21.22.26.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 22:26:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-32384-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=RTy927LL; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-32384-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32384-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id B593B1C20C6D for ; Mon, 22 Jan 2024 06:26:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7AA5D125CB; Mon, 22 Jan 2024 06:25:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="RTy927LL" 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 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-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788770847143906199 X-GMAIL-MSGID: 1788770847143906199 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_breakpoint.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 &= ~alignment_mask; hw->ctrl.len <<= offset; - 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, unsigned 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, unsigned int fsr, info->trigger = addr; pr_debug("watchpoint fired: address = 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)); } @@ -886,7 +886,7 @@ static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs) info->trigger = addr; pr_debug("breakpoint fired: address = 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_breakpoint.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, unsigned long esr, perf_bp_event(bp, regs); /* Do we need to handle the stepping? */ - if (uses_default_overflow_handler(bp)) + if (is_default_overflow_handler(bp)) step = 1; unlock: rcu_read_unlock(); @@ -733,7 +733,7 @@ static u64 get_distance_from_watchpoint(unsigned long addr, u64 val, static int watchpoint_report(struct perf_event *wp, unsigned long addr, struct pt_regs *regs) { - int step = uses_default_overflow_handler(wp); + int step = is_default_overflow_handler(wp); struct arch_hw_breakpoint *info = counter_arch_bp(wp); info->trigger = 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); 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 = event->overflow_handler; if (likely(overflow_handler == perf_event_output_forward)) return true; if (unlikely(overflow_handler == perf_event_output_backward)) @@ -1350,19 +1351,6 @@ __is_default_overflow_handler(perf_overflow_handler_t overflow_handler) return false; } -#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, From patchwork Mon Jan 22 06:25:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 189919 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp2395990dyb; Sun, 21 Jan 2024 22:30:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IEG8rmxphECLSncVS6Jiqqw2o9ipa9Ggc5jtInQc2kk91+0MAwNbBKXeK0jHAAKk9SpjcS2 X-Received: by 2002:a17:90a:31c7:b0:28c:a722:c587 with SMTP id j7-20020a17090a31c700b0028ca722c587mr912614pjf.42.1705905003519; Sun, 21 Jan 2024 22:30:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705905003; cv=pass; d=google.com; s=arc-20160816; b=VbEq6ookxkUVyVsVDfIceMVLF96xNpzhsLDAcdnDUmgujchaBwq5EUKjGwEEQgDW+7 uExhDQQgb0llicR445jALZTiY1yv39NpXYjOcEjiWNumy7EiFYqeDfpJpWfoNF3Bilro RfsVPhBb7O9JH64Fky3CCS5O56s2yAj5SZjEzIzHBhEtcba8Yi/5Ojp3ENO1edqkJJVw ZKeulHtNrvtph3FkNLAq/6kYRWP8QsM/oMcOWt3f9qPNasTehmXjdmlU6TK/MJKqMA3L QitwwIJmf5wLZnUD1Z2X9QU6TlUh7aMvN89wG9VXmTdMNgKW4k5nzQB0D7S0yb7Ka9gK m5SA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=HG1Z9345f9l/NkzG8Y/mlPsfRpv6azyFKs0IWGP/jfI=; fh=K5yXAxTssbJLQcrUsVgOEJ74eFXZrEcbr8gMwVXs9zI=; b=wjF2p8HsXoSyDO83tk0hbRmcLmOXiXzV4+G24aybyznaB3P+LMxQXOsDiH6M1NEGdT 8pN8B8X9NScTnAJ+RMThs9mU6p8mSCkem2iqeHJpNJNOyobmwFoSaFmSuzp0FrzXtU/z mwbXLb3nMlDBle35gwYRGbLrhdvGN7DWOSZ2N8J3mFgkmTgc5mazYJEkZi2H9aBYo9Qa ObmuEO1g6ALg/uWrDQG7QuA2JsZZ5eRk2HmgcfCCixjCyynhabMR34o0++gNfIB0L1O3 jiVRWevpE+r4npM7MIndojt1ktI3B/+cSIdn6YF2hbVKRj8eN+97O+o3D8LwnPt6mOE/ pyYw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=I6iPxPpg; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-32385-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32385-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id x19-20020a17090ab01300b002901f506806si7389030pjq.130.2024.01.21.22.30.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 22:30:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-32385-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=I6iPxPpg; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-32385-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32385-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 078622825D5 for ; Mon, 22 Jan 2024 06:27:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 07B7214A88; Mon, 22 Jan 2024 06:25:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="I6iPxPpg" 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 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-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788771044794252640 X-GMAIL-MSGID: 1788771044794252640 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, ret = __perf_event_account_interrupt(event, throttle); +#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); } -#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); if (*perf_event_fasync(event) && event->pending_kill) { event->pending_wakeup = 1; From patchwork Mon Jan 22 06:25:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 189917 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp2395350dyb; Sun, 21 Jan 2024 22:27:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IEyW/zzee16P2tD666vxR6BRSebecRD2SfXvXxx9sjKX27XBsWWDttMnH2pUl9JBm3i6/fK X-Received: by 2002:a17:906:4a83:b0:a30:84a7:ae30 with SMTP id x3-20020a1709064a8300b00a3084a7ae30mr22344eju.97.1705904872001; Sun, 21 Jan 2024 22:27:52 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705904871; cv=pass; d=google.com; s=arc-20160816; b=A4I+LR3fPpgQfVDeFw4lQ8hyaUKmAvnANtffj4qczRA77W/GB+lI3EPWzaSyN5XxgC 8htILGMl26dUK4gb8Q+tKSv09uZFTWGWPOgkBuwqAPMNq2J40RncMr5QeEfnW8jTmmaj 4Lh/YWitJd8JEQolkiIIC8DuqH+5s6OAsJtKfgAY8SY8Q7chYR2EA9AGke2coVVC6Wrq qaCNqkldG6UZWOEtGD6fLJVsZhpeNGDoMy7S9+1gTxTWaHAIMYD+FOwZpQO3V5WNGBUC 7ntmD+PB3qDEp1CZRaaAaetFZ0kg1xbmWNc1cYDzOptpWGFQkR0+3Q9qWhsd1vBUi+IP wiBg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=EjezFELGsG5yYdoO8o3jjgWXUKsZRmSJ3eM36KaKRP0=; fh=QqAG7y11SrDZ5ggNxSzkSsm0mHGtP7ak63ick3on3hY=; b=WGmMJISmjETzx0NVWlwFd6YGcoCwvHKTL0TCJlq4bFt3cTwD9d9PPBoTKcclwCZexE 7X1OUBrvfHhDVr2UOva94qpbEAmXJrkJfwkoFgtYHp9Nt1Xxc1EkYM8qJQSkG+0xU1zZ 5ruD2ge5qEsfKA7jx0hL0DmmFSZzUioYpdFxDXR05ebz9Gu4XOcg9silQ4DBqCfNLCw+ WtCVi6Qhhin+8wxfCBgR0mYW8BeXOKOWP+sDJryJ1F91QzFu0933jLMTnsgXxMTfTxBW 8Qp2WG0uZXvQpzIU1ezs15gJgroJIlq1DBUqjNCrfSFF8YIw8Oyw42T8hBgx3LYsiaa/ 9Klw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=azHSEMWX; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-32387-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32387-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id ot26-20020a170906ccda00b00a2accc5f96esi10149726ejb.581.2024.01.21.22.27.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 22:27:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-32387-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=azHSEMWX; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-32387-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32387-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 3AE6C1F21406 for ; Mon, 22 Jan 2024 06:27:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 575FC1549F; Mon, 22 Jan 2024 06:26:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="azHSEMWX" 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 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-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788770907234814951 X-GMAIL-MSGID: 1788770907234814951 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/testing/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 = {}; + struct sigaction previous_sigtrap; + sighandler_t previous_sigio = SIG_ERR; + struct test_perf_skip *skel = NULL; + struct perf_event_attr attr = {}; + int perf_fd = -1; + int err; + struct f_owner_ex owner; + struct bpf_link *prog_link = NULL; + + action.sa_flags = SA_SIGINFO | SA_NODEFER; + action.sa_sigaction = handle_sigtrap; + sigemptyset(&action.sa_mask); + if (!ASSERT_OK(sigaction(SIGTRAP, &action, &previous_sigtrap), "sigaction")) + return; + + previous_sigio = signal(SIGIO, handle_sigio); + if (!ASSERT_NEQ(previous_sigio, SIG_ERR, "signal")) + goto cleanup; + + skel = test_perf_skip__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_load")) + goto cleanup; + + attr.type = PERF_TYPE_BREAKPOINT; + attr.size = sizeof(attr); + attr.bp_type = HW_BREAKPOINT_X; + attr.bp_addr = (uintptr_t)test_function; + attr.bp_len = sizeof(long); + attr.sample_period = 1; + attr.sample_type = PERF_SAMPLE_IP; + attr.pinned = 1; + attr.exclude_kernel = 1; + attr.exclude_hv = 1; + attr.precise_ip = 3; + attr.sigtrap = 1; + attr.remove_on_exec = 1; + + perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0); + if (perf_fd < 0 && (errno == ENOENT || errno == 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 = fcntl(perf_fd, F_SETFL, O_ASYNC); + if (!ASSERT_OK(err, "fcntl(F_SETFL, O_ASYNC)")) + goto cleanup; + + owner.type = F_OWNER_TID; + owner.pid = syscall(__NR_gettid); + err = 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 = ioctl(perf_fd, PERF_EVENT_IOC_REFRESH, 1); + if (!ASSERT_OK(err, "ioctl(PERF_EVENT_IOC_REFRESH)")) + goto cleanup; + + prog_link = 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 = (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 = 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 >= 0) + close(perf_fd); + test_perf_skip__destroy(skel); + + if (previous_sigio != 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/testing/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 != PT_REGS_IP(&data->regs); +} + +char _license[] SEC("license") = "GPL";