From patchwork Fri Jan 19 00:13:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 189365 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp703184dyb; Thu, 18 Jan 2024 16:14:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IEULIYY4+GxFB6MEIxzToHsGmgobqH/n0vCy0F8YeSPI9+wk+qCWedA6vBRVPYRfEevTTGZ X-Received: by 2002:ad4:5dcf:0:b0:681:55a5:ec55 with SMTP id m15-20020ad45dcf000000b0068155a5ec55mr1252895qvh.75.1705623288351; Thu, 18 Jan 2024 16:14:48 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705623288; cv=pass; d=google.com; s=arc-20160816; b=RFksu/xyFzLp/klU3m9wKc+JmK5AzHcWtQ+pND+z1HfGGyJ1Is5p/HjAvk0A/zStbr U4YP47Vg99MgIlFoq3nK8dlolaSmzYXI0K4jrWfO/vU7yrtw2imDNC08ILiypb5EXzeb LiXURoVYu9sKGokUg/V6zU1vIJiqzB8yaVYtx1pENOGOr9wG4BkHtr5vGDQNudidZKpm 4oQEHKNVsB+thXNZRpso+b6JG8BPKB5W0k6n+Rx104zLP3AwZnZx/pKQBzin6QaE0H2+ ZARWJP7se3XToD3t+RplOhFDtl0CbelrI1BQV2xYY9SNCVHVO5/Zx2fx+rTdlvfVHDaG JhGA== 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=goCRlABDp65iNRuy7Fdk99c/XuzP9RSm/x9LjyCmqK8=; fh=vyLuMFPX7a5RxtKqZusW0ny9V2SiUBkKFuhqxl/vIvk=; b=yHeZ+CjG9sof6tzNMP3pw0Z7wywLnaHE2mHAe1y/5duC+pcX2WodUhIQSUuiX12K+P PIMMR+LWEbOCUjDYxU5ngHZz3GTxv7s3uj9KgFdaXIuoBuwe2to4s/8V6HvRENcCy1L9 B2zKFI3YKo+fa9KSM6za68h+teuEBs+DDPaMw4Fz+LS7NawWjNsbRSZpgKgRk0h8r+G4 zU++zwPqJzsB8SEE1s5CNVWHeVY6HV6bZa02PYCClLrvExo84/85+TeromFCxw+cBP5r NU6BmzEVtvFgXdvgJhWuip84BA/6pQ/bcXRGFl/NLzrMtDm1BVCgxcQ9TG8n+3ZnzkPG SLqQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=g5vLseTw; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-30650-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-30650-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 g16-20020a05620a219000b007833e8da4fasi14513198qka.599.2024.01.18.16.14.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jan 2024 16:14:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-30650-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=g5vLseTw; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-30650-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-30650-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 1B1851C22A5A for ; Fri, 19 Jan 2024 00:14:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CAB162117; Fri, 19 Jan 2024 00:14:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="g5vLseTw" Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 478D31111 for ; Fri, 19 Jan 2024 00:14:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705623255; cv=none; b=hOeXs8a6JCJGesJy4uADdQu6YOS+WDsquFWcqbY+dgyKsN0dex/gSJCaKa17bZNbaWg1LedwLVmCnuMM8zJkhvjxK+hEe0T/7/eP5+wQrfIMpKrq5HLT3lgMg6ZW8+1EmZGDTY0sxzk4qfq/eub7nkKTFHc9YFzUiQ0F2d16XlA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705623255; c=relaxed/simple; bh=BvdPhHhuuf/fRX3SqKFdqqVaVqqf5FQjd8NlByquZYU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Rwr/uAmv6YncLqVM0SCkBndWAhYIP93pUbRBM/pfei2k/Vb1PdenwuDK7UhUCjAVzhmsGCTDrZsS4Ihn1AHyQWlTyL5jnjC+2F9O0bRYsc2dlSnbnIASR5wcLlvERailhZMlJxURV+VM+Y/rvRDqC0lOOKrVB+bdcGEY7pL03AA= 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=g5vLseTw; arc=none smtp.client-ip=209.85.214.170 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-f170.google.com with SMTP id d9443c01a7336-1d5f1e0e32eso1395905ad.3 for ; Thu, 18 Jan 2024 16:14:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1705623253; x=1706228053; 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=goCRlABDp65iNRuy7Fdk99c/XuzP9RSm/x9LjyCmqK8=; b=g5vLseTwxcL1+e/Y4krZOtr6EtaiOwYUQSZuxbGwOUr8uMWz/nICL/yTIYe+NmfkvY x0lJAgXKVhLVmjFFCntcSbWlM7htz8Hf8jgvRizk46PvmwYOExB+gglUH16J+xor7cWK zMRZVkDvkuTiRFwLXPKaqy/+BDIhqynGoWKpZ3BWwurBeWF8yPMB/XyqcKd7IRFd7SfT ySZYS+yFFx8GtAVLI0/eTK3Q4KP1MeOChv/suiKL+1Y3mH1Q02gLPRbQvQKQVMcPYyZk lz+BPo9Z2og9ZGxLWEERNuffyOYNgJ8NabEbVnG8/VatSP7wWAvrdEoFpd/OlUVYjTPo fFTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705623253; x=1706228053; 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=goCRlABDp65iNRuy7Fdk99c/XuzP9RSm/x9LjyCmqK8=; b=HkYpKVxmjIhrTfAYFD7xxnjJuNc2p+CQ7ed33OaXwM+f88w8jLq7OYce/Tz9kVPBDM foDhZVg1xkK6vsKOr6UJynrBu/Z3M/xNbb3VWpvUtM42YmVv7tbIeX8rcVkgKGuzaQnT EJmrRtQ8UPvVZeLkKrtk985Qqa1Yur3rr8ILxf80Fw8J2bGNOS/1yVHiPYGH3bdCzBFE 7zZulErGIxzGJkRJSS5wSDLgEVM0e4mSlzqZA3i4JPhqJ0DOi0ubFH9BPSP35+ChC3oP +Uf64oJZWNLSmxj6VjDDBTyKWeOI2bKXedgveuqhhFNBvNfeHCZ5AgEgFpvF7SlH9I2+ bNLw== X-Gm-Message-State: AOJu0YzGDRu80alVO3vTmFbCKzkP4um7P8Q0KAQreWvdV3zZd3eS1qyL qLqNaYE0oZ8xOVksI8GOepZq7lv0Nys0oKlvfToPEQmVKM5BDHj/qJURRLA6rQ== X-Received: by 2002:a17:902:dac6:b0:1d4:441e:7377 with SMTP id q6-20020a170902dac600b001d4441e7377mr1612768plx.40.1705623253562; Thu, 18 Jan 2024 16:14:13 -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 mj7-20020a1709032b8700b001d1d1ef8be6sm1921238plb.267.2024.01.18.16.14.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jan 2024 16:14:13 -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 , Song Liu 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 v4 1/4] perf/bpf: Call bpf handler directly, not through overflow machinery Date: Thu, 18 Jan 2024 16:13:48 -0800 Message-Id: <20240119001352.9396-2-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240119001352.9396-1-khuey@kylehuey.com> References: <20240119001352.9396-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: 1788475645445403476 X-GMAIL-MSGID: 1788475645445403476 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 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 Fri Jan 19 00:13:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 189366 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp703345dyb; Thu, 18 Jan 2024 16:15:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IGtCshQURKb4m3Nl9crkShG8TRtJlfKpUjCWfoQCvpVAcELoeJDmjUepJ0F1PO9NzqP8zzL X-Received: by 2002:a05:6a21:4842:b0:19a:ed:9a1f with SMTP id au2-20020a056a21484200b0019a00ed9a1fmr1756646pzc.122.1705623310434; Thu, 18 Jan 2024 16:15:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705623310; cv=pass; d=google.com; s=arc-20160816; b=zH7iofGqhHvZ14MB32PYSl9xaX2t7IPXYiyDgMHnF5S1lWxjjXyUnBWED7SZzDvMIg scvTyNft4aFwFq130kI9bACLApB8WzHw2x+TZKWrOCWx4Wn7OMeoU2aIYdRneGzqDg/0 BhoJAQFfd/B1P+e3hkVQwwJv8yYh38bSTkHwj7P+g5WbVagVo7+7haM2hR2czy4LjeeS AILcu/e/K+sWvnP+0ENulz+hm0rh4uX3/SQxOoyzwzTNJdHEL0Labj+04JlSyy5bYLPv ejEqmrzb/rjk2oCVICjr2XOVXafMKQiOm+rjQPqf6jgMCe/ISjIh+wZMZb82utcg0b1H LZqQ== 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=TZcU1eaajijXdHtIWhayAvoytSB02YbTVp/d9IoCpKI=; fh=mH0n2W+BM2fLLodna5rnDA8H1KU5CuA3WDGj0JNX95E=; b=X4heB7SuRnAda3ilIIMfESAgm+KdsCCvbOHbkgMTx0TFp11TSISZGfzyTTjj+RSn7b lmdSfbzysRKpfevN/kx9w6XFGfXtQGBIRjzs8mkCs546Z4fH89mx34TzbYuXEHBdNDaV ceBqn0cTRkoKi0+zh5T/RoiozHtkL91FRzdH6AvcWwVvn7EZo17M1f7ANuk/D6ydjc9C XTwHTsJMvXt2u1Sj8pN62bSEhVJ7j9lqGXya8yhvHjf2ZVk4dXEpAYWC1+YWV0gNmMf8 bqeVKDPvDdvPputVSOmjArm8hVN6cUqh5r0sAXUfrasvGYdCcW+BUpDzjdAXKpb860vw 9PIQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=k4RCG+1D; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-30651-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-30651-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 w8-20020a63f508000000b005c606792120si2248973pgh.107.2024.01.18.16.15.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jan 2024 16:15:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-30651-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=k4RCG+1D; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-30651-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-30651-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 2C0D128754C for ; Fri, 19 Jan 2024 00:15:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BD3F83FC7; Fri, 19 Jan 2024 00:14:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="k4RCG+1D" 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 334B1186F for ; Fri, 19 Jan 2024 00:14:17 +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=1705623258; cv=none; b=TVLMWDcllIpXJg2M2xfcTSBwT1zPv6lKjY1xL3eipCpI5dAbnqRsSVxoQWZ6td5JKf0EBKEuo4V0/dFSesiQXsuJddKYWBpFxl9X0gKN/a7ZQq/8rD3AU2Nn2KbjdyMyOO9HPGuQ0pksuCIQG/d0xwWHy6G97xAzJPUSLsTzOig= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705623258; c=relaxed/simple; bh=aE2XSvO8OhL2bKh+kya+x2v8NA+iHW4PCGN/ajZfLTA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XplUW4neBiVWWGlkQxN5enaaJ3LO7O8aYLwjY9pO7urnJbMuQX1O1rIua5l84EmJChYtog42f26M/Z/jNd+ZFRA+4l3FAuq+u/YUJK+TIc8ZBt8r58tynaKnOwu9ZNJEBVQEl9+CBv/w7K057JZM3jAYi3b/wXkGOvVkVmgf6Oo= 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=k4RCG+1D; 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-1d54b765414so1547465ad.0 for ; Thu, 18 Jan 2024 16:14:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1705623256; x=1706228056; 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=TZcU1eaajijXdHtIWhayAvoytSB02YbTVp/d9IoCpKI=; b=k4RCG+1DXT7r+NHdYh2FBFIrU7lK/6+cDzG2SmFd1Vrb1s+N0L6ZesFliazSbah2nY A27FnLwf6VuPQDuWchzfkWJJgioV2vZaGMdU5TU8zzmtbW22I+JYpGgadFCOro6vWT5n WDMPPQJIJZBAUqLzVUZGbsaARHUvnVVg2Kd71iXIeu3t9APSxWWoBWdhdWf19JZXo46E u+RBT7nFnr93yRh3p4KD4RsxXRXLbDcYzdWMMQxtt57EpqkJgt1aZDVMpiiOkRtgG97q GQxteT02B6at5I1qxMiH9E52zHjv83QM4nHE8COxNDZ7C/gWE+m9cuicDD4/hLb9COD6 4OBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705623256; x=1706228056; 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=TZcU1eaajijXdHtIWhayAvoytSB02YbTVp/d9IoCpKI=; b=Jg3kcQBWyaVWTq2L2lT63VPTufhX6Yww4Pj/BF9iBrLkJ8DQ9ptemRq06bEOxefWT9 byv24J3VcOY7pyL5A8YwUUMSHeq+CdD+m4sovbbtwcpaN8IoBoEXK8m+DAEKhUh8+Tse gRgXA/r771MEiX1ayRCCEJY7skOsR0fLlGV9fCdxREL78ieX+LyOQ8FSDNfYD4SvIEqe m/vfvAUBGKdLkxFo1eh9ZeUiQseVSTHddh4SW4g3RnaVgzCan2Iy2D0kIekfdq7vwywO WSWh3D/Ea/1dEoRX0PNv/vZHlteXbKyJLJeH+Rz4GyfnnmkvTZ12fCOE1zS7GMvUP7jt v75A== X-Gm-Message-State: AOJu0Yy+WkHs3svBvpvq0rlRcDY11K30Q0dBfWYP1RkIrFoSgnpq1q+S 1CETbKS9dlhJAp+git35ei4xMlKRcorY5/ftCVNsd1ujyL4c+FsblrQkL1O+Pg== X-Received: by 2002:a17:902:d486:b0:1d7:1b54:8969 with SMTP id c6-20020a170902d48600b001d71b548969mr518993plg.11.1705623256601; Thu, 18 Jan 2024 16:14:16 -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 mj7-20020a1709032b8700b001d1d1ef8be6sm1921238plb.267.2024.01.18.16.14.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jan 2024 16:14:16 -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 , Song Liu 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 v4 2/4] perf/bpf: Remove unneeded uses_default_overflow_handler. Date: Thu, 18 Jan 2024 16:13:49 -0800 Message-Id: <20240119001352.9396-3-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240119001352.9396-1-khuey@kylehuey.com> References: <20240119001352.9396-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: 1788475668703929168 X-GMAIL-MSGID: 1788475668703929168 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 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 Fri Jan 19 00:13:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 189368 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp707040dyb; Thu, 18 Jan 2024 16:25:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IERQuL1aJsWCOLhpZh1OqArAU7OgHD5AQEuD6QbRF3J0qnpIjuJtqDTLgnqa+Fm+4TcpEUd X-Received: by 2002:a05:6830:3b0c:b0:6dd:ddd8:51e5 with SMTP id dk12-20020a0568303b0c00b006ddddd851e5mr1633818otb.43.1705623908750; Thu, 18 Jan 2024 16:25:08 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705623908; cv=pass; d=google.com; s=arc-20160816; b=1D4PXYmaeLaLauCVMYQYIDXZStxWbvOMvfst1fSJfjma5GtINb/TEd7Pskq5WxfZF7 BrCTsa8OrJeV/Gux5MdZcRWuTtmWxVECgbRmja4nnLihfHwwEyGyO9D+hzCEdQuFISZe OuAUOMdvPKbK7jEJo4bF96F9Ck9Kt6Yf78Km24paxahebyL5DjlfqyRLspgDGVwOhklD C1GQXdREeqrRX9ynWZ1XPqkPakUjXtwWYiVRh+x5JCK85Z67EPuXeTCJarNQYwASu4le qf+tatjOYE6L209btIzgAa36kkQIdUqfhxs6hpAcPp3hoJrqilnag75nm/lkqwtMt4J2 EVWg== 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=/hEmM3vmtzdKl0VwvFdVPj5xDGeYkLg3iqjbEyKv8xM=; fh=vyLuMFPX7a5RxtKqZusW0ny9V2SiUBkKFuhqxl/vIvk=; b=A/GmaNPG/iwsSdb7woXfQLdlMLI24HU7I7b+VJBoOy9yPC9fRhT5gjGRDIZyaqYYW3 j3yse0sdSwilP6LFWb+qXEdMnFL5swZtXiarKHoYacaoY31vbSih8ehZao52WjwVr0/+ o7jA0MTzOsSYyFVXfdPheZZnkKxH1eITfMcjN+LbTxf9St02nXOQnHS/UjcB4ExT4Tqf A+HZVWL+pDCFeHUxdHPpLaPkqpAw8RTS1aLsq28DMa0NxU56K7XjQ4onkf8oYCfLx6rG bWrPEqg0/38XtNWQU+jF/y5MvpyzO56oG8+xVn8fdisGEWM4oK45QJUEF4YQUlnJHEMT xpzQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=EAHGi4sB; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-30652-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-30652-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id u2-20020a05620a0c4200b0078366089f14si8033237qki.168.2024.01.18.16.25.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jan 2024 16:25:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-30652-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=EAHGi4sB; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-30652-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-30652-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 24BDF1C229E7 for ; Fri, 19 Jan 2024 00:15:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 569824A08; Fri, 19 Jan 2024 00:14:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="EAHGi4sB" Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 703A62F35 for ; Fri, 19 Jan 2024 00:14:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705623260; cv=none; b=gIxK9g7BHGs02Y9aSLOaMrL7Bs9HtF0gvYngsqACt/v7s/eSIfqfKuwEBTJvbXwh1cCYnVPI0KmIab9ctquppRrIbv24I0zd+4dY22qkoUQVa8F2bF8+oLrxVKVGOJGxwNZbFLTH3nbHmma3WFK9yLC6ZF7l70C4VpRK2MXD850= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705623260; c=relaxed/simple; bh=3622UZiYTMAps9fbTXA59oDeFjLFPk6fc41GnG+9ElA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZCcvT150BGDNrhYgk5MF2nIJV8MxLJqFfsnr4sFZpTApVJWCJDzVcNLyFSXpfINhePdNZB9qNyIezOM7B0pqeKY6H70kAaeaXyh6H/IZHd0otLGtLjx0voolTwRiU6JXG+T1G+aeRNslKB6sRAAxMl+5MQSkk+gs8cbNpHJK26E= 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=EAHGi4sB; arc=none smtp.client-ip=209.85.214.182 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-f182.google.com with SMTP id d9443c01a7336-1d70c150632so1737255ad.0 for ; Thu, 18 Jan 2024 16:14:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1705623259; x=1706228059; 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=/hEmM3vmtzdKl0VwvFdVPj5xDGeYkLg3iqjbEyKv8xM=; b=EAHGi4sBFq4E70+oyXfcJgh8JejvoQfg6y70LujDnGNW/nQOK9LEZJMPMZcZYCYo1F 2ncaGimK3K2P/N05NrVZXPV4ips16A8D5lj31KH8UHVEAplH0q4u4wgoO4zN2uZdDxU0 SokB2JECL0+E2SY+ppU5oUWAJU+YvZ/QnM2xulqWen/U98PUR/EiplgxGSafz0xBUWa2 XU3TKx0QjHgl14oL5yg7K56xQUt2WjL8eQVZWBGKuicvbXvqCGET6lHctm35vbiskLTX nQz+dpMWZx2icGdjq5nrRfeDxyj/hDqnrddGgrjvqxxPFEaqnid6C1+SgQDpIj8JGHdJ 3YmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705623259; x=1706228059; 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=/hEmM3vmtzdKl0VwvFdVPj5xDGeYkLg3iqjbEyKv8xM=; b=JsREBLJMkgvZIKTBcAQEycIMgIS1v9gvY8lKwuG/WDDZp4K5uqZtvd+9F0qs3Cn8zb a8QHgwC2HQkYS+IIpm3nFYT4jE2ukhXlG2u8qXR8a0iM73Xjk3DK8lcESdGDMwsBCykI ZX9XzvQ2TWUDMN9o3I4bZg5qKDJFEKINK5RztcINEA6uu1BcdnbFXI3a21I7gYXDNL5H e9vEszjHgb2Qcp4LAtQoCYgzF77/aklGb3iV9/LquSH7vgjDPKVREKpL+ruQI34I6dXa HUZNaUP+CFWgRoSJpDWWsxFmkWCegVPZQJ9V3z3sBjfW3H6efNLOyt9qB5Rj6gqH7s6A wNbg== X-Gm-Message-State: AOJu0YxJcthArW5lQ7+VRyDguRU3Zw4nwrYhCREuCi7aMec9sbGfR273 R16+n/Ku14uOMN5FLZ5KjEP5fcth9Fo2NwJ2dvA1tmBbS5Dy+cKOFW92IVdCGw== X-Received: by 2002:a17:903:26c4:b0:1d4:f29f:e13b with SMTP id jg4-20020a17090326c400b001d4f29fe13bmr1639156plb.40.1705623258923; Thu, 18 Jan 2024 16:14:18 -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 mj7-20020a1709032b8700b001d1d1ef8be6sm1921238plb.267.2024.01.18.16.14.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jan 2024 16:14:18 -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 , Song Liu 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 v4 3/4] perf/bpf: Allow a bpf program to suppress all sample side effects Date: Thu, 18 Jan 2024 16:13:50 -0800 Message-Id: <20240119001352.9396-4-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240119001352.9396-1-khuey@kylehuey.com> References: <20240119001352.9396-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: 1788476295819022698 X-GMAIL-MSGID: 1788476295819022698 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: 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 Fri Jan 19 00:13:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 189367 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp705732dyb; Thu, 18 Jan 2024 16:21:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IHuDg9lm+UItuprdlsKbji8one2bx/A2k5A0a91Sng9WgcrgSYFDZf5FK41Dx/zVQ3OrZ/9 X-Received: by 2002:a50:ee8a:0:b0:55a:4e80:5e1b with SMTP id f10-20020a50ee8a000000b0055a4e805e1bmr176034edr.4.1705623678382; Thu, 18 Jan 2024 16:21:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705623678; cv=pass; d=google.com; s=arc-20160816; b=IhL/PYWqw1nkkNSGehH7+bd5BF8SZwXuZN/NxugyexeCrhz50jZAMqzBxFcxB+gwiP cFU37p9QvaS3wAB4t7aqP85cN220upRjuIAZn00ObeY6/lMle4LTOnMKeAj+++8yALyK eV/niy2rB9fGNPALwwwuqXfjYyFntGJeUZLoW2IRbA2haplPySiT4wz/EVUb+vYFAzzZ 4tsdwttPrO0p+T3Nd8IDPuT+kcQrQl8Qs8FpJL3W+Wz5J9J2yRuFuZT30PudbILTVrVM 3peBHFapsAIWQHmAA7bmWXcIrtIR3U4UCgU9yhLCiJxbrHMN/D/Bk2GO3pOPFC8vGE1R Ju5w== 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=7KAoEfpbtZugctsvQWmIErusprU1vjdWQA1H3EUTUAE=; fh=jXI4JtaIdos0ULKesgj40SZvsVl1UwaaQKZ+rAjtxek=; b=HdqJxmnqQar0k0+r+YyMP89itojIgsVcFwG2FziF262X2vT87Vu+vZ4ltBfQcERQss 6EwIREjH5qcghCDUbaMcWelkYqZEu1BFx71kMIxBUn1cUrQTzQu0Z7ei1LEdjt7mm6L/ 8g7OGeOyt0NhvMctU8nmd3+TecVdWZkCTyzYtUDN0OEBw2e61y3CQ2+o/opKDupsj89M BTWptdqHMQSiZwjZc/RljeOgHdGXOvYwpkAnQeT8sLfy2UepWJ25N8dzzpqLFw8induQ w1t1EmeC2fRPHQWlqVP7U4geev6/PYcyq+th7PSD5mXT72/FMkbbXtt3TYJGNWyqF917 nz3Q== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=XupO7X72; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-30653-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-30653-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id h13-20020a0564020e8d00b0055981e1da71si3404289eda.125.2024.01.18.16.21.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jan 2024 16:21:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-30653-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=XupO7X72; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-30653-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-30653-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 408F11F2375E for ; Fri, 19 Jan 2024 00:15:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8DB581361; Fri, 19 Jan 2024 00:14:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="XupO7X72" Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 D030A182 for ; Fri, 19 Jan 2024 00:14:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705623296; cv=none; b=EzUi/afEx8nQcRyMTui7C17B+Ec51e5ARNeBIhQYQU9q3TAr0fEobarGXfRTh/TWi2Ni+Bkt8pdUAuv8jl/pJ45NeMDW5mTkkqAw3ERZNqT/M3Ry8NkW97ApPNpcQfjRu20I610Mxs6ELgn3+lZ0EBogBVjV/DARnc5+SLGWjDE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705623296; c=relaxed/simple; bh=lgztwFbOxvCSZQBlFKLsXosQinxtlxOmxqJ20zMs7Cw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lsToDT/cgiQBko7aPxyT18nZKftWITUXZ2hUYILDbYl6P1ay4oI8e/iwQ2dzxOPvO+R8HE43Umdtj/Fpe74IC5Yjow1rx7K/q7k4P2Ty/QcwCvfNn/GLCgEDLm3F4PvJASJDE6W2LCKEnWSbqw3ScXHGbluMCOCiEDF0m8RWLz0= 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=XupO7X72; arc=none smtp.client-ip=209.85.214.181 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-f181.google.com with SMTP id d9443c01a7336-1d70c3c2212so1961045ad.1 for ; Thu, 18 Jan 2024 16:14:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1705623294; x=1706228094; 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=7KAoEfpbtZugctsvQWmIErusprU1vjdWQA1H3EUTUAE=; b=XupO7X72OFlt2YjQ4lEe/fBPu5e3h5RALmEQrIq4r/6gO+Lrt8U9eBWcOAbgYe5s8M isEVmgid1Y13IvTCIepzoLnb+rUko+oh37FO8xTAYS71wNgw0oHAFz5VRnin3HQiUKxD FvZLxRL1i80qgd9xZtYuERfyP61gAYAgGrD76wuvL+lcQDk5D6KrJ5n16jK30y/6AABY f5LxrDzhvdUWpM7zy7bGpEJZEha+61YfWnmOmvlAHCTDq5hsFim2IgKjeoSFGQvUQyuR beF5GQYqpk71EJhhbzrNpqVUsu9kq+V3EejWqHMO0nulG6Tad+eg0TR5DKNJOo8ejSc8 B4fA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705623294; x=1706228094; 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=7KAoEfpbtZugctsvQWmIErusprU1vjdWQA1H3EUTUAE=; b=Hu36IWbxCgiFJY5+RppIFBICML4Jg6+apC6EO0aEhREzR3MkYq29Nr2HpkGXHMPm2t gL9E2h/NEwRjBzAWzMOtIBL0Hnq+gh/nWwxgmsUoMvX+IoeBiz9cJcmmoCewzT6yql79 LomwnjWomrL5muyOSeBZBttVvBfR8ses3WkC5/dKNPwJcvHDbViyf2h+94ixD4eETJ5W 6WoZivzKlyLQ3vdO7O37+iwp3/PKA5twDoYQ8SKLbrgGytSawPz/cx6zWcmlXKAbsW2I jNJj2bAQCyptrTsCwR87XwuE4VpHIuG2MPEtSGnScqxt/DQN3fFyC96ZNY7cCNlbz2VI 5APQ== X-Gm-Message-State: AOJu0YxUhpfsZf6TRKVCG9jzJNqTJOg29lZosA4K3BeiN+FUrsXthgep Lyyl2wljPjr0p10FKehi9F3+HyVPrN3HA0uy94gN5HdHZFpujsfPtYFIGoKJwg== X-Received: by 2002:a17:902:7245:b0:1d5:7524:6d21 with SMTP id c5-20020a170902724500b001d575246d21mr181163pll.42.1705623294291; Thu, 18 Jan 2024 16:14: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 mj7-20020a1709032b8700b001d1d1ef8be6sm1921238plb.267.2024.01.18.16.14.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jan 2024 16:14: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 , Song Liu Cc: Robert O'Callahan , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Mykola Lysenko , Shuah Khan , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v4 4/4] selftest/bpf: Test a perf bpf program that suppresses side effects. Date: Thu, 18 Jan 2024 16:13:51 -0800 Message-Id: <20240119001352.9396-5-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240119001352.9396-1-khuey@kylehuey.com> References: <20240119001352.9396-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: 1788476054015284531 X-GMAIL-MSGID: 1788476054015284531 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 --- .../selftests/bpf/prog_tests/perf_skip.c | 136 ++++++++++++++++++ .../selftests/bpf/progs/test_perf_skip.c | 15 ++ 2 files changed, 151 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..c79dae34ae90 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/perf_skip.c @@ -0,0 +1,136 @@ +// 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, "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); + 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); + + 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";