Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp6822050rdb; Tue, 2 Jan 2024 15:05:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IFe+k0EJivjtASwz3jC2lR98sP+7CI3BNV/i2UH3YCg/Ow1pvPxUdfZmnknHuBkb0vgjnsi X-Received: by 2002:a05:6602:1492:b0:7b7:fcc0:8ff5 with SMTP id a18-20020a056602149200b007b7fcc08ff5mr30603293iow.32.1704236758092; Tue, 02 Jan 2024 15:05:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704236758; cv=none; d=google.com; s=arc-20160816; b=WkVzVtiG/TTO/102eaMu9Iwif1qVYE1YzEcRVEtRXXAtM6QhYZFNLOPE7jZYkPT1/c 3c+t7ZYEmHmK269BnVA4Tc/Vfnsf9tQh3ARIv/WkDm2kiZJzmZMD9P/xglvjpotnVc+f YCc2X/WX1bchnwa7+tPY6SyhX1Bj5Oj+4xF/cGBabJS8zWydGO8H0mXbvZp44Z5mXEbN MRLstZZE6UrNmEuCyFGWRGFP+/rRPWkDGYvFEaeFc/HxGkCVxQr/e7qKmtUp1p9Y9z7W C7MBxz6eRdCgu+unIhclkYOOYyNlRvyWkSPGpT0BeM21f9+jDFKN2buNfUto+snKsGGQ aGZg== ARC-Message-Signature: i=1; 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=kf5EVxYHoGE4uYEsopUyp55/6IdLmNJGoMMTSRtzrFo=; fh=nyEdS10mxrsMOkkMNreKeRijF5sNyQpzU0pkmR/jRFE=; b=xR3Xz50zZd0OqxJlZHQC1T2prylkUUZwPDt/BFb6hnyaRsWub+fGZ5d+ejdFcDTDar hEe6z4HR2EEcjVbJoipohhs9CdOdw7wL2sALQFT/9ktnT3pGtP3WRSBbCcxgUiBmjh6z hDykQjYE/lb3M7iw+VebrpW1kdPXiNXE1g+xgoaKC4pigDIhurQ1d8Sps8FO3i4NFQDt QjCe+zYwgWlwKX9lsLRlpWCRQ3lodBP2WOwAgqV1qnfmoclSemepGKx21HmmCaSPvOg3 l4c9WTEYK26K6yPh2Zgao47xJ8psSyyDgUpxbXw0ZS60M6xkk73XZQAMuv8oTEoJbQPN JgrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=XtCoocYz; spf=pass (google.com: domain of linux-kernel+bounces-14963-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14963-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id z64-20020a636543000000b005cdaa0544cfsi20090610pgb.670.2024.01.02.15.05.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 15:05:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14963-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=XtCoocYz; spf=pass (google.com: domain of linux-kernel+bounces-14963-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14963-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 60863B2249C for ; Tue, 2 Jan 2024 23:05:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 04AD71774A; Tue, 2 Jan 2024 23:05:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="XtCoocYz" X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 255FA1772C; Tue, 2 Jan 2024 23:05:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A43A7C433C7; Tue, 2 Jan 2024 23:05:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1704236742; bh=T6cXZag/Xy65XNlKPdc4AWtqQPyk9JHRqhKUCxsZsBY=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=XtCoocYzHvo//7+EA3mfYWOHX9EIE0xClVk2Hh8KjUCHbOLySkNh6Uli8DsDZbysb EYpN0fOqylv/K4teNG//cP1FVP4ZEozs8Zj7yP16QW+2uI7CvZix9wK0q3R/63nWSd NEoEss9ZMalFTjKSc+FsPEYoj/+STjn7LQqgw3+tCVlJuMGLGKnqw7cMgUI6yAepE1 OcWo/a/qwYxQdmKR4icMTeHUCFY4Wgs9MalwiErq8eDF4OBEWxEz9KO/1LQjdXDFBE KcWg/z5bcH7ni5qjF+OT6/QNKrEjIm1W8PxpLojC4XsUa47Zhf+xzFbmenLnGUyfPc bgZIM7QBjH+xA== Received: by mail-lf1-f44.google.com with SMTP id 2adb3069b0e04-50e9e5c97e1so1450898e87.0; Tue, 02 Jan 2024 15:05:42 -0800 (PST) X-Gm-Message-State: AOJu0YyVQaBkLUBenTsZJq95GpUeYbAz65nNzwVui+0R3hZxt/uzoRX1 XDunK48UHcQDhKJLJm/yibKW3uilSGJxEIznBKc= X-Received: by 2002:a05:6512:3ba5:b0:50e:7d6b:b5b2 with SMTP id g37-20020a0565123ba500b0050e7d6bb5b2mr6487931lfv.6.1704236740865; Tue, 02 Jan 2024 15:05:40 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20231211045543.31741-1-khuey@kylehuey.com> <20231211045543.31741-2-khuey@kylehuey.com> In-Reply-To: <20231211045543.31741-2-khuey@kylehuey.com> From: Song Liu Date: Tue, 2 Jan 2024 15:05:29 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3 1/4] perf/bpf: Call bpf handler directly, not through overflow machinery To: Kyle Huey Cc: Kyle Huey , linux-kernel@vger.kernel.org, Andrii Nakryiko , Jiri Olsa , Namhyung Kim , Marco Elver , Yonghong Song , "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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sun, Dec 10, 2023 at 8:55=E2=80=AFPM 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 > --- > 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_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) \ > 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_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. > */ > @@ -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))) This condition is hard to follow. Please consider simplifying it. Thanks, Song > +#endif > + READ_ONCE(event->overflow_handler)(event, data, regs); > > if (*perf_event_fasync(event) && event->pending_kill) { > event->pending_wakeup =3D 1; > @@ -10394,9 +10403,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, > @@ -10417,10 +10426,8 @@ static void bpf_overflow_handler(struct perf_eve= nt *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 =3D prog; > event->bpf_cookie =3D bpf_cookie; > - event->orig_overflow_handler =3D READ_ONCE(event->overflow_handle= r); > - WRITE_ONCE(event->overflow_handler, bpf_overflow_handler); > return 0; > } > > @@ -10468,7 +10473,6 @@ static void perf_event_free_bpf_handler(struct pe= rf_event *event) > if (!prog) > return; > > - WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler)= ; > event->prog =3D NULL; > bpf_prog_put(prog); > } > @@ -11928,13 +11932,11 @@ perf_event_alloc(struct perf_event_attr *attr, = int cpu, > overflow_handler =3D parent_event->overflow_handler; > context =3D parent_event->overflow_handler_context; > #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING) > - if (overflow_handler =3D=3D bpf_overflow_handler) { > + if (parent_event->prog) { > struct bpf_prog *prog =3D parent_event->prog; > > bpf_prog_inc(prog); > event->prog =3D prog; > - event->orig_overflow_handler =3D > - parent_event->orig_overflow_handler; > } > #endif > } > -- > 2.34.1 > >