Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp793688rdb; Thu, 15 Feb 2024 16:12:06 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXpWmOfJGXHWk4w4AzZIRLalZWWWNw4uhAehU8z9IipIVrPOh+Fs/e+i/5v0rBUnK8vX0yY7HMIz57SkEKXZvDpVoFdn++wq16hCTfoVw== X-Google-Smtp-Source: AGHT+IHgZ1pwoqaezIHbHFkvgEBE5xUChQpoIhYhxGb0DbLzfHiG8cty0y+KqyWX0hUZ5i2DsV+m X-Received: by 2002:a05:6214:c22:b0:68f:1aed:88cd with SMTP id a2-20020a0562140c2200b0068f1aed88cdmr3923571qvd.11.1708042326558; Thu, 15 Feb 2024 16:12:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708042326; cv=pass; d=google.com; s=arc-20160816; b=X15MhXqvpsRrdwv1F7E6ejzueoCcUNevmu+DuCqpOOQheK6kZZS2Yxu274113oRav9 /SoE8jfTxoU0XynFRAL6PX3w6F+ZbYZrhKiMa25lV507eS83f/7YW1qpjEoaJ1MJyi4a SYYlQjkCeWZjeLvM2tAhVfOOJfPGTG+O0MQKDfheZ4vCMhsIt8di9fzJTnM3z5OW8IUS KV0c7d06DzluvbMaL5jEBQDuJH5g7HlzhZK0agr4F/RVvrabCdnqXVF5/npaeqhWL3uV S1W7sxX5DWsZL4xoHSZ55+JX2+jjIvRWwq5jyimGOUkTC1JBidqaIB/OtjYTTSVfBeEe qyvQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=u8P+Esqb3vOTsAokX1aWIvQbUbsCyNuNLfr6EeZzaz8=; fh=Hk5DQnizRCmyEU8J61CS/rgI0qHvWEX6LvjOSS6Uq98=; b=t0kSUQM4mmamUp7IL/Fqmpn9l3MY/VVDLGWTKesNvhAB5T78LDQJDqfd6r/2/650Lu d53JF+2BrTg+F3KKXS0z0kHjYIPrG8NBtUzALUxssiFmwHHbLGZyFDwefss4mr3sujil Krkb4pFjOxGj6ymcEKI4vvBmXnwla/66+k+jgFAACF2WmwD3b9rKNduj2/J7PthXWiTc alQMwdlCZwITSosSuCX/k2s+xZqToRHGb3oL2tE/hskfRZ1iHYLj6q7boJSKBdVxS2Xs rLd5S9QSh7GGYpaC6WbccORPTR1P/MmOTZ8bfNM5HtyDGBklhVTV+L+29kOMJpOkvjfL BsNQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=jkkyzQAI; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-67882-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67882-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d8-20020a05621421c800b0068d0503a3a8si2991836qvh.439.2024.02.15.16.12.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 16:12:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67882-linux.lists.archive=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=@gmail.com header.s=20230601 header.b=jkkyzQAI; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-67882-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67882-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 44C8E1C21457 for ; Fri, 16 Feb 2024 00:12:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6C618320F; Fri, 16 Feb 2024 00:11:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jkkyzQAI" Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (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 05FE1170; Fri, 16 Feb 2024 00:11:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708042316; cv=none; b=jS9ewHOK7aYRjOglm9O33mvaFM9Vlsj8lNgbtBKDhJ+iapwMarYudDd4Py1OVdqIplXI65trEOY0LPCet70iAFYy+b6V5J8my9+FoFQbhUbuMjTd3FzdUGnwkc0j/GuYlWsLZXfjYtQOZoHIYMVEQDemEUqdYTI0QO5imHvHEGg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708042316; c=relaxed/simple; bh=F+B9CWHQoXM/3CVWDT8Du9ciplR2a1Pk+BKGIJYiqus=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=YI7YX9Re/sN1mYalo5zkR3FsVtjJvPZVaMz1c5wuDKSRyLVvvts3B0vH2rXKjMPThoQkvbIIsIXjcHkoFmV/kk7YiNxNEsxKvxOA4EFdhXlGj5wwI9r/xPLoQESJEEDQ1COgCTblWkoMH+QJckdnI9AmUp9thyn/CEdBwNDifzM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jkkyzQAI; arc=none smtp.client-ip=209.85.216.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-296d26db6aaso1065048a91.1; Thu, 15 Feb 2024 16:11:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708042314; x=1708647114; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=u8P+Esqb3vOTsAokX1aWIvQbUbsCyNuNLfr6EeZzaz8=; b=jkkyzQAIEf1k0JnT+6cWxDV9pKVo4SqR1csPbYLKs6Vnd5Sq83yq1y2BjHu4TRc85+ C59XUcQvFUcrwUumbclixXCvJ78+hXO51iK4d4FDqJObmFFojPONSi54K0KWo51uxJfK El+b2bAmActhjy6Q1fe1Jff6SHKDfWvDWSh2gRgC3Ns6Cm4Y3H3ttrd4fwCz09IzbYzX IfL4o16ortOTtGa4UrRMAs59L8c+zSrFRJ7KnfgxbRYrGGxVrjGu+zihf5snswwsBrDQ X+c1jOReCGelnrbbUFW561cESKKlip1q1EAvlagRrz+FMeIOfFTxwmd+6PhXyHD70Nli YRVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708042314; x=1708647114; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=u8P+Esqb3vOTsAokX1aWIvQbUbsCyNuNLfr6EeZzaz8=; b=EHNDj73rV0FodBq5Fv6wPYBIopH3EeMu/FAkJW4moMRXOGlgoos4iVPXxvOp3hw+Sp gi4tACgSq1kYW1vK+wtfYwJVo4SD1Q3jP1qoTi+9yYvqE4HYjuNNniY/u/VOrpTFIYBP T0Ef/EjH7DihReDaEnar3EgHCERLWehvzaTpIJsOHbhL9C1aNQtw2niDlSxkvpSGwj2w Xv7pOKIGXF8NWPc6oeL9w/1E0U8WYjWssjftaPJoVbkjjYcYCaZ0VN45IsE3Lt4sJEES QdUgh/aw1eT97E7fU7mm3IQG9bWdgFsJAlsu9e5QPxVB83c4TJmGkZz4DFDEJqpBEcK7 Rnkw== X-Forwarded-Encrypted: i=1; AJvYcCVDaKjXNexl2nMoRHKx3PwBfVnoq6hX8N1GxaO1AfpuuKc5zjPAA6z/UuyQzsFIJZAHeJZipRF87oy1QrJka4GBLrr3Q+NJVCo9VcOH3AC8QTi+xFYk0RtBpvl2s7KrUSNaTljPbzsVQoZBVV++RidndYbDnid/loQzYKmsOy+1ljCZ6Q== X-Gm-Message-State: AOJu0YwG1vOoV+iaxwsSvGqAuzJLtFycro6kMW34NH6gKYYxHfNq00HY eMrXM/rRObC2KUHfx7Y4BT7qXvXjcmlQ+MMg33toac5oCV730ZTwCtaNL1anhBInmedXosE+DeK X/bHMp1ERxMzkz6cBg6L3o4zpk1g= X-Received: by 2002:a17:90a:db17:b0:298:e89f:72f7 with SMTP id g23-20020a17090adb1700b00298e89f72f7mr3236729pjv.8.1708042314198; Thu, 15 Feb 2024 16:11:54 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240214173950.18570-1-khuey@kylehuey.com> <20240214173950.18570-2-khuey@kylehuey.com> In-Reply-To: <20240214173950.18570-2-khuey@kylehuey.com> From: Andrii Nakryiko Date: Thu, 15 Feb 2024 16:11:42 -0800 Message-ID: Subject: Re: [RESEND PATCH v5 1/4] perf/bpf: Call bpf handler directly, not through overflow machinery To: Kyle Huey Cc: Kyle Huey , linux-kernel@vger.kernel.org, Jiri Olsa , Namhyung Kim , Marco Elver , Yonghong Song , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , "Robert O'Callahan" , Song Liu , Mark Rutland , Alexander Shishkin , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Feb 14, 2024 at 9:40=E2=80=AFAM Kyle Huey wrote: > > To ultimately allow bpf programs attached to perf events to completely > suppress all of the effects of a perf event overflow (rather than just th= e > 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_handle= r_t overflow_handler) > #ifdef CONFIG_BPF_SYSCALL > static inline bool uses_default_overflow_handler(struct perf_event *even= t) > { > - 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) \ and so in both cases uses_default_overflow_handler() is now just is_default_overflow_handler(), right? So we can clean all this up quite a bit? > 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_e= vent *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); This is quite hard to follow... And that CONFIG_BPF_SYSCALL check breaking apart that if statement is not great. Maybe something like: bool skip_def_handler =3D false; #ifdef CONFIG_BPF_SYSCALL if (event->prog) skip =3D bpf_overflow_handler(event, data, regs) =3D=3D 0; #endif if (!skip_def_handler) READ_ONCE(event->overflow_handler)(event, data, regs); we can of course invert "skip" to be "run" and invert conditions, if that's easier to follow > > if (*perf_event_fasync(event) && event->pending_kill) { > event->pending_wakeup =3D 1; > @@ -10427,9 +10436,9 @@ static void perf_event_free_filter(struct perf_ev= ent *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 =3D { > .data =3D data, [...]