From patchwork Mon Dec 11 04:55:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 176469 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp6834769vqy; Sun, 10 Dec 2023 20:56:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IG51fz7r3gIdQcvK2vSeWnpjXfbVOyehmWdg4mGH1PQJVktiuBiZPF4SDka6FYMPOuyY0O0 X-Received: by 2002:a05:6358:d58b:b0:170:30db:1188 with SMTP id ms11-20020a056358d58b00b0017030db1188mr2513094rwb.24.1702270565936; Sun, 10 Dec 2023 20:56:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702270565; cv=none; d=google.com; s=arc-20160816; b=YW7bqyYD2Al+UOSFE/lenxf3Juawk/JxV84U5dpDEEgyF4WxRFX4csYDTcU+BPgn/U GXn0Coy4zktLKMfMhbqxtumHvYJZ2B4OZ64ibpDf79A1irhMrVjFY+oBdy8X9dl/DbAZ Ql0GRlTt9S7IbY0rw3Gf3uvbYHFnGaQn+PpBjGn1VkP+pfVuMhNNNMH9Bpawz12DlMnN x4o4x1htZiMiEFWdguKhtk6yWQLpjpr44bohpsBIpX1Ok36WYrHMAR74hRMSjYiIrysZ oyfrds6R0P1fw6YAK+lirQzuG7eevWNG9xABJrJRVficJTrztLTeY7ZpwksmnEu9sC7T VGYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4LaOcUMKpaO4TscNM+bwybPG9qx56qSeDWtKT8IysKk=; fh=TgNdCu0nkwWF6I0gv1FaAZU9nxNhZAbxhuG8wY4k0B8=; b=GOgsOYT/mme45Hk6wuQIbIkiMIsb6E+z5rVbttOz+rN8sQgOmjEstxbz1CKxf8obG8 WxSx2x0xsEbXcRzXupj3fSSq3z1OeoKuKcice5AsBZRaXxc2v3jvm6S8ZR49VYw3GH3F p/H/JY37DKJdruUobEe/Z57kOfGROlKefkGLZ4JZ/S325KwzyG2qph7YdKPvCk7ZQRnq mIGmQC9dkOgbYKJCgoB48n6ZMeWwqyCIMEKrNYnaemlKEBYAjTPAZJHaLOv04jQSfKam HMUf6wezAG9ImJPHimoA8uK2h7ib7PQx6NPFGOeHxmKjyzLvRMTA8tMfHX+gmTqm3l+j 8u5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=M3R04DG+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id o11-20020a17090ab88b00b002868f5b2c7csi5020722pjr.33.2023.12.10.20.56.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Dec 2023 20:56:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=M3R04DG+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 5DF7780677DA; Sun, 10 Dec 2023 20:56:03 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229570AbjLKEzv (ORCPT + 99 others); Sun, 10 Dec 2023 23:55:51 -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-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 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Sun, 10 Dec 2023 20:56:03 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784960061221884284 X-GMAIL-MSGID: 1784960061221884284 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 --- 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_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. */ @@ -9584,7 +9590,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; @@ -10394,9 +10403,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, @@ -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; - event->orig_overflow_handler(event, data, regs); + return ret; } static int perf_event_set_bpf_handler(struct perf_event *event, @@ -10456,8 +10463,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; } @@ -10468,7 +10473,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); } @@ -11928,13 +11932,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 Dec 11 04:55:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 176470 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp6834779vqy; Sun, 10 Dec 2023 20:56:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IEk+164it//OTNgG9gUEBAAvFBv+2WsP7INlWQ2rDNMcjvAFxPparpDRDHZXZubIqx6Qzem X-Received: by 2002:a05:6e02:1986:b0:35d:6619:c5fc with SMTP id g6-20020a056e02198600b0035d6619c5fcmr3976174ilf.43.1702270569035; Sun, 10 Dec 2023 20:56:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702270568; cv=none; d=google.com; s=arc-20160816; b=Bh0rw+oYzr6/KttMU2V+m94HG34JkaVcQhKqkvnwP4/my280J22kTTRMlVvlN/bxMm nEiOWtwSYW0goNIPTxcWzTC14630NdVH5k0nZ7obj43ayiwQ0SBT71TH+tx4on+yz4uT IMCGjBQgxPa8TTGZX5GqGu1jTh8nmV625C3uK8Cq4DOIgn4MMa9zBIUdZnwp47gHwDbJ BVtcu5GGoVevYPZzb9oD1wvB3yxwfX+geqtCQOyCT5MDhSwQpNYJLitVZGBA/zuDKOzv 8cH71DN+wHdR4n/ivRpdfU16CknM6VAom7gr1BNF2qjkRlD2SjnSR8vmDZ9+5XM/bd4T C9UQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=MD1CGVptJqB75nV04X/vQRYGuHl6l1SpLkm2NuE80OA=; fh=LpdmrgFulbEOokuT4MGGH+WEKJtyPeRtFHIf4z2p7zY=; b=dfVB1Bp77pW8KMciR8LA8fgEgaZYOI/sQpOBkANPi6ioeoZX9XzCKcDAcBoXyiG7cc 62NiRwWe179aRaZliMqENYk+wXhqpljuz8Ylb4iUzYGJEpWpt2tf3cc7k6ndwfujNMXo NGrLPF8YNarW7p27MV8Yi24+jkRD4SUuP8q//0pA0NCowu6mPEF5t2mmj+UK2sD9UsOH DrPqROPcxd/+ehBD2Z1vpGbZNSJIstBEuc5/QhDbOplUztJMSCzyI6B85f9p5WEj6uei Yym5exJVyLIGKZg4hMdc+BdP08Dd02B/RUZ5RZkYmsRYaCMfjxBDh5tOuXBpbmCQTwht sqhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=MTnHAGWt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id i16-20020a170902c95000b001d0b1392c7fsi5592348pla.350.2023.12.10.20.56.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Dec 2023 20:56:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=MTnHAGWt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 204238057471; Sun, 10 Dec 2023 20:56:05 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233036AbjLKEzy (ORCPT + 99 others); Sun, 10 Dec 2023 23:55:54 -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-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 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Sun, 10 Dec 2023 20:56:05 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784960064638482350 X-GMAIL-MSGID: 1784960064638482350 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 --- 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 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); 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)) @@ -1330,19 +1331,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 Dec 11 04:55:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 176471 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp6834869vqy; Sun, 10 Dec 2023 20:56:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IEd7ow91EzRTq4LfgxGB5LJMm9XrROALqVJGOq3E/5lyV/0rAMcRCS+cZCPNPcdNw/rbgHD X-Received: by 2002:a17:90a:e651:b0:286:c5ae:5294 with SMTP id ep17-20020a17090ae65100b00286c5ae5294mr2766722pjb.63.1702270588695; Sun, 10 Dec 2023 20:56:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702270588; cv=none; d=google.com; s=arc-20160816; b=jg8B80hMhPP5M5ejMBuII43kD98+ieGLnIx1tlS+EVGtpXaJ9qyEZWK/APMt0IZOst +F7PIsQw0qCjBuFFLGTOHTaNfU8RWXpfKokN2ZV8+ruuGVSawuIgpR+vdLM1S5Xqw1Vv swkIF9EPy+BP+ZYoTTd3o1UfZ8x96oTF3ajdvGjp+IiiYKF68L9rvo09IKqPs69tz6J0 whBswYOoFlu6xUDtinvxekiht5C/BMD/xYf3eJmVyrzLpG86VeJY3nt0UC9iJyC5yik2 FZ/lrxFDR0EuUoevUsaAjLAoMhAmJSKsvJlDLXJbXscXDHlOdij6gkb3cqlvgnSWfN0p DWqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=BoADJIsaU4bvjYI7gdqH4FQT1qltoRFf88+tkPG8tZk=; fh=TgNdCu0nkwWF6I0gv1FaAZU9nxNhZAbxhuG8wY4k0B8=; b=ojM0WIcnG7LjkbFiXnseJ/ip2TlAQ28uQACvKoQapn14OAaS/2KaBcxxcoZg+XkhX7 EyolC19E1pHDUr9+crTElWMCuHDuLerduK41O8dMSvGmjsc7bCbDIDwujTK+4zDeer0/ u3uJXtsIlSWfTI1gh+ZabQkI0iYrHLXfBLU0ifnv8SXJzsk/pzcKvz9naNQQqxspL5nz k0UYyObtdVO0itzhYkY4BTfthA4GTI5uWxvQ694eLWS7OZnV//ldtbn2OxKaEh5BJSdx ZmsirmzUUVOJcHbB/NHmKT9bwaP9z2ZW4Roi+i7bTLiufEPszg4Ra05/GAJNNQo1f6Fj f5AA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=TKGWwN1B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id mp6-20020a17090b190600b00286d6caba8esi5433986pjb.95.2023.12.10.20.56.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Dec 2023 20:56:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=TKGWwN1B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id C484F809A82E; Sun, 10 Dec 2023 20:56:18 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233080AbjLKEz6 (ORCPT + 99 others); Sun, 10 Dec 2023 23:55:58 -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-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 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Sun, 10 Dec 2023 20:56:19 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784960084594089642 X-GMAIL-MSGID: 1784960084594089642 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 --- 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, 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 @@ -9590,10 +9595,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 Dec 11 04:55:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 176472 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp6835094vqy; Sun, 10 Dec 2023 20:57:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IE3DPPkJNJqebN+CAaeiqrNkMyigk7oFdIJkmr1wg+C9ObhcAZ8kcmFXQLDsl7lahto/J2q X-Received: by 2002:a05:6a00:1303:b0:6ce:7656:5cba with SMTP id j3-20020a056a00130300b006ce76565cbamr4491131pfu.22.1702270638613; Sun, 10 Dec 2023 20:57:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702270638; cv=none; d=google.com; s=arc-20160816; b=lk2rGTgZdNI8xo03gWRufPTGspdczGlBos70n02rRfFpgkR8C6IpEizIe9+twRvZh2 fmIWbWewqu/J+e3GJjYZQCrTV/GTJ/4xK4Nc9ypsXGlqzZ34jZcyITqx2WHCB2thhZen uuGVJmZpCFpLwMuJyUhqO1MG0K98Cd3u4BCB2FYAOObPVy1jfGd5tiiDES40J1tOR1WL ToGnTsa8FE3t4jDvUtqqKQOoeU4WKanXYCGWc+W4Mif0uBu5O+Ixab/Ug0TSDaGZzxsa iJhCI8Azb1Z7YDXknWx8ohypSaDhtW1CrDi6jIXSuMcbh3O6jvG/3pFSLYnrdnUn+hie GOHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=f/uOwN8/AYKX/iTSNcMtLhs7X+zpyhpj6oRruBreRa4=; fh=mQYsxR7xYTE8DykXrbtIRCz296c9MEKm6OXiYrLoXUw=; b=nTSB7fBNPpifT8sn6YZTd9klEbxXQiL1OUdlr3MGdI/8N+Wps9lwjb555lw58FFL7D UwMyT1I6l45zshXgEd7BjIwEheHy4ntm06qqeSrIKgpBZs2VUTpTeyTgPinikuKwykZv i0of9xNgN10kWydMhGmj3popYYJ3BN9rF0cSSoPFwHnVnMqf4fEokWUu9gE+ysBBj7vA poEGbmVRXpsRcCY4+ykb95EmZw9T+NHCN9GcI4fK0yVtWLlXbdTYHX9TNzZAJTaA1nqE 6KVgYVefq8Tg36ojIIUdtdd+4QNLzoW1+N3ge+jzNYWyOA25B396mo3LBfwdysyMQ3oJ P13w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=MKSRvC1O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id cm25-20020a056a00339900b006ce11062b36si5506972pfb.228.2023.12.10.20.57.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Dec 2023 20:57:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=MKSRvC1O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id D604F806311A; Sun, 10 Dec 2023 20:57:06 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233203AbjLKE46 (ORCPT + 99 others); Sun, 10 Dec 2023 23:56:58 -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-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 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Sun, 10 Dec 2023 20:57:07 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784960137196679078 X-GMAIL-MSGID: 1784960137196679078 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 --- .../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/testing/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 = 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 = {}; + struct sigaction previous_sigtrap; + sighandler_t previous_sigio; + 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); + + 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; + signals_unexpected = 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); + + 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";