From nobody Sun Oct 26 22:32:33 2025 Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) (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 342DBF9EC for ; Fri, 12 Apr 2024 01:50:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886648; cv=none; b=EZX8h5ohTrAaAmYxQ41hWvZMCpSQxDsQHjmQYEQr8wAn1O/bqM6YWIk1yGJx7Fjb1DgSOv/K19vx4x3TJX9Qul0OHxBZHxqYpC+jzjZ6Nm5Wcnzg0AeGBKTxXPVf5S4jH2Sb9ghHWCO95xfz0dfKCwqCMZhmRvtMIj5wqiQapSc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886648; c=relaxed/simple; bh=lLcJXMGYAt18ckXzjBJt3RZcIlon0AJDakZC8+N8M0M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=r5kY52Snju41gC8fmzL8a59Zwd3U0MGd3pawRavC3EOmRijSayx3xdhOLgLaj+SAbC0Op0KprhVq+2Nodj2vGLCzI3s/CWDbl1/WPBxDcDZxKKrJO90ifsqwfyereQBTDOB0nmUB8eg+8iXCU04Wd19MZcygSTFZqAZE/GdhaZU= 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=FQ3BuQZb; arc=none smtp.client-ip=209.85.222.171 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="FQ3BuQZb" Received: by mail-qk1-f171.google.com with SMTP id af79cd13be357-78ebc7e1586so148637485a.1 for ; Thu, 11 Apr 2024 18:50:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1712886646; x=1713491446; 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=2I8ioTIE3St0NCO304/67c5ZZcTg34djE+ltGa2DGgk=; b=FQ3BuQZb8LtOhmt890Bmeq2fPTjqyrHlVC00FaR2K+bSiNS5ffhWMIBCbKqWbCoF9a yTKEojGuNSBLHgeauasteqnUs7ZXlVqVebsjOuOToLwj2QxBsBLCTIrm1YgnQ4ZdptSY Kn0JB14JBHBz64zM4H39qMyt7yft6iJNslfW7EP1u006R86EdrKnRdvwl1GqckKj6Ie2 bmgYnPNGoRRPQnX0i07qpCzGA/iBz/eNkYm2z65qVVDMY6G7ohPS6eRhxyOMeMtXrw90 /p4V0rsU/WxnRWomdyk0/CjfkbhOjQ+wcY/Gtshn6HRziwKWDkEIHi6FpxzvGm7vATrB pbfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712886646; x=1713491446; 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=2I8ioTIE3St0NCO304/67c5ZZcTg34djE+ltGa2DGgk=; b=cNeSKbuAblQVEeQU5lLx/wc6p8SkaDXHRR+Vdmi8FY921CJsOIuUaIEo3tLXS+lM51 mZoDGjqZhpGk5GOVP2FHE7lUfQjoBOjM7gS/pUgC0LIijCXJeB7dqeD/phELqyeFSV8y 3oShzO/kbmOZweUqnJBHF7dAQ2NOlg6pgYuCABMO/7BQAvdaJaZCr2vMg+lfDNrbu4c3 MXKujHKcKMUGWTXq6vGTksMKBlTBAINWVUorFlKxu3Fyls/4dzn72a5D9Qr/JgSXCSod b/OZA2YxVR8u0hO7KSaI1YwlhGVTjmwyCUuXyOakipweMHbLPfrg24VPSrMmwiDWrMVV Qh0g== X-Forwarded-Encrypted: i=1; AJvYcCV/wZgBxSWPqsOpEUdlAXzR20V6+8YP6hGtJRSgELPadvBQdSceuaZTAgRJ5qbrSspWA4ZoTYtwbAPPRRb7LBLRR/cI2wPs+aeffYO9 X-Gm-Message-State: AOJu0YwJd+9Rl11rjo57pR7gTysVV84TXQmCJYLPuihgeYPjycNREiuc uuCSMb1FQxl56dUMDW5tSR6yuReaAw1aUz/oKtecZXNJRwFPoThxGNs4vv5EAg== X-Google-Smtp-Source: AGHT+IEpPEvWFpAFcQSe+VQV8imp3rgz2Yo/hr1w0rVz+GPtikTxqKZX7MSio59IOMcIrmeqvbBuxg== X-Received: by 2002:a05:620a:4589:b0:78e:b996:db93 with SMTP id bp9-20020a05620a458900b0078eb996db93mr3057028qkb.3.1712886646096; Thu, 11 Apr 2024 18:50:46 -0700 (PDT) Received: from ip-172-31-44-15.us-east-2.compute.internal (ec2-52-15-100-147.us-east-2.compute.amazonaws.com. [52.15.100.147]) by smtp.googlemail.com with ESMTPSA id f10-20020a05620a15aa00b0078d76c1178esm1756677qkk.119.2024.04.11.18.50.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 18:50:45 -0700 (PDT) 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 , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo Cc: Robert O'Callahan , bpf@vger.kernel.org, Ingo Molnar , Mark Rutland , Alexander Shishkin , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org Subject: [PATCH v6 1/7] perf/bpf: Reorder bpf_overflow_handler() ahead of __perf_event_overflow() Date: Thu, 11 Apr 2024 18:50:13 -0700 Message-Id: <20240412015019.7060-2-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412015019.7060-1-khuey@kylehuey.com> References: <20240412015019.7060-1-khuey@kylehuey.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This will allow __perf_event_overflow() to call bpf_overflow_handler(). Signed-off-by: Kyle Huey --- kernel/events/core.c | 183 ++++++++++++++++++++++--------------------- 1 file changed, 92 insertions(+), 91 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 724e6d7e128f..ee025125a681 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9544,6 +9544,98 @@ static inline bool sample_is_allowed(struct perf_eve= nt *event, struct pt_regs *r return true; } =20 +#ifdef CONFIG_BPF_SYSCALL +static void 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, + .event =3D event, + }; + struct bpf_prog *prog; + int ret =3D 0; + + ctx.regs =3D perf_arch_bpf_user_pt_regs(regs); + if (unlikely(__this_cpu_inc_return(bpf_prog_active) !=3D 1)) + goto out; + rcu_read_lock(); + prog =3D READ_ONCE(event->prog); + if (prog) { + perf_prepare_sample(data, event, regs); + ret =3D bpf_prog_run(prog, &ctx); + } + rcu_read_unlock(); +out: + __this_cpu_dec(bpf_prog_active); + if (!ret) + return; + + event->orig_overflow_handler(event, data, regs); +} + +static int perf_event_set_bpf_handler(struct perf_event *event, + struct bpf_prog *prog, + u64 bpf_cookie) +{ + if (event->overflow_handler_context) + /* hw breakpoint or kernel counter */ + return -EINVAL; + + if (event->prog) + return -EEXIST; + + if (prog->type !=3D BPF_PROG_TYPE_PERF_EVENT) + return -EINVAL; + + if (event->attr.precise_ip && + prog->call_get_stack && + (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) || + event->attr.exclude_callchain_kernel || + event->attr.exclude_callchain_user)) { + /* + * On perf_event with precise_ip, calling bpf_get_stack() + * may trigger unwinder warnings and occasional crashes. + * bpf_get_[stack|stackid] works around this issue by using + * callchain attached to perf_sample_data. If the + * perf_event does not full (kernel and user) callchain + * attached to perf_sample_data, do not allow attaching BPF + * program that calls bpf_get_[stack|stackid]. + */ + return -EPROTO; + } + + event->prog =3D prog; + event->bpf_cookie =3D bpf_cookie; + event->orig_overflow_handler =3D READ_ONCE(event->overflow_handler); + WRITE_ONCE(event->overflow_handler, bpf_overflow_handler); + return 0; +} + +static void perf_event_free_bpf_handler(struct perf_event *event) +{ + struct bpf_prog *prog =3D event->prog; + + if (!prog) + return; + + WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler); + event->prog =3D NULL; + bpf_prog_put(prog); +} +#else +static int perf_event_set_bpf_handler(struct perf_event *event, + struct bpf_prog *prog, + u64 bpf_cookie) +{ + return -EOPNOTSUPP; +} + +static void perf_event_free_bpf_handler(struct perf_event *event) +{ +} +#endif + /* * Generic event overflow handling, sampling. */ @@ -10422,97 +10514,6 @@ static void perf_event_free_filter(struct perf_eve= nt *event) ftrace_profile_free_filter(event); } =20 -#ifdef CONFIG_BPF_SYSCALL -static void 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, - .event =3D event, - }; - struct bpf_prog *prog; - int ret =3D 0; - - ctx.regs =3D perf_arch_bpf_user_pt_regs(regs); - if (unlikely(__this_cpu_inc_return(bpf_prog_active) !=3D 1)) - goto out; - rcu_read_lock(); - prog =3D READ_ONCE(event->prog); - if (prog) { - perf_prepare_sample(data, event, regs); - ret =3D bpf_prog_run(prog, &ctx); - } - rcu_read_unlock(); -out: - __this_cpu_dec(bpf_prog_active); - if (!ret) - return; - - event->orig_overflow_handler(event, data, regs); -} - -static int perf_event_set_bpf_handler(struct perf_event *event, - struct bpf_prog *prog, - u64 bpf_cookie) -{ - if (event->overflow_handler_context) - /* hw breakpoint or kernel counter */ - return -EINVAL; - - if (event->prog) - return -EEXIST; - - if (prog->type !=3D BPF_PROG_TYPE_PERF_EVENT) - return -EINVAL; - - if (event->attr.precise_ip && - prog->call_get_stack && - (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) || - event->attr.exclude_callchain_kernel || - event->attr.exclude_callchain_user)) { - /* - * On perf_event with precise_ip, calling bpf_get_stack() - * may trigger unwinder warnings and occasional crashes. - * bpf_get_[stack|stackid] works around this issue by using - * callchain attached to perf_sample_data. If the - * perf_event does not full (kernel and user) callchain - * attached to perf_sample_data, do not allow attaching BPF - * program that calls bpf_get_[stack|stackid]. - */ - return -EPROTO; - } - - event->prog =3D prog; - event->bpf_cookie =3D bpf_cookie; - event->orig_overflow_handler =3D READ_ONCE(event->overflow_handler); - WRITE_ONCE(event->overflow_handler, bpf_overflow_handler); - return 0; -} - -static void perf_event_free_bpf_handler(struct perf_event *event) -{ - struct bpf_prog *prog =3D event->prog; - - if (!prog) - return; - - WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler); - event->prog =3D NULL; - bpf_prog_put(prog); -} -#else -static int perf_event_set_bpf_handler(struct perf_event *event, - struct bpf_prog *prog, - u64 bpf_cookie) -{ - return -EOPNOTSUPP; -} -static void perf_event_free_bpf_handler(struct perf_event *event) -{ -} -#endif - /* * returns true if the event is a tracepoint, or a kprobe/upprobe created * with perf_event_open() --=20 2.34.1 From nobody Sun Oct 26 22:32:33 2025 Received: from mail-qk1-f179.google.com (mail-qk1-f179.google.com [209.85.222.179]) (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 30C5EDF55 for ; Fri, 12 Apr 2024 01:50:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886651; cv=none; b=dsXDlaDmAyX0AMFR6zRXU/7qH2KXynimLfRjWJxZC4EKAnatzauyGAlLxgMKkyoZ9d6T5e6USCdA7zy70fBm5pHjkEBEmy1EVpJMFvLmQhIcr23819ZW5+MexCEWRM5gGzS2dbxezPPxSKs9CE6aFOaYAw68VuzZDLvsRoUpNns= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886651; c=relaxed/simple; bh=7YouWyZuxbSYzHxqPUnW+aY1UMlIJgGnhI+PHUoOFhQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=e1BQHX8LOF0d2tTr5KCYHTQJ16I8XtTElR2BzRAWdwPiJ08+kUKe2eS5kgWLHy4VM0hJLt2puD1Brsy+BRUd23tT21keSCQM+/hyML3mZxBFKslEPYSawUvcr+bS+6rMT04bj2UHpWX4MTOCX/EUzzRcmXdBdfYV81RVvCA5caA= 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=aU0bVvpI; arc=none smtp.client-ip=209.85.222.179 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="aU0bVvpI" Received: by mail-qk1-f179.google.com with SMTP id af79cd13be357-78d5f0440d3so24470885a.2 for ; Thu, 11 Apr 2024 18:50:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1712886649; x=1713491449; 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=IR+aRAW+xEUckMtcdjKW/EEXnX6Dbx8rKcxaN3dNqzg=; b=aU0bVvpItdjjGy0Uh4y25anjArKIOtlIy5bEQk0mhmXbgUtnAoxQU+EWL0JLXBUsGE PaFzOpaJzH8GVLLDtvVTKCjsLTFLU3B0AAlawSh4HGgxhag7TfwZH/R+MYRm7QUwbMAG w8FRBHIep8Bt+j5TOyytwhtljABL69vAvnkrXANE7rldNPtRV4s9KqL/BVLLdaS0EEhU S7NYM1+pr3opDH0IQanKJieTjzymm+1VP+TTtawBfPlvipoLVVtG8Lq+0a4+iHZQoxji bYan7dULV/jISLCvZvrhf5LY4IUpVNt3UDYdMtkiPY1qHjhOBcRJAb9+2KRRgreFRl9R P3hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712886649; x=1713491449; 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=IR+aRAW+xEUckMtcdjKW/EEXnX6Dbx8rKcxaN3dNqzg=; b=kfJGFk5o7G6kizE/QsHV7PDktr01yTO130vFDA5t9YIB2SWDQWGbB3NtBkpkNPhtay o0dkAOBKmymr8PHmsOticabzUOm6nqhp3YOP1VLEtOpkHB9rv8nA+JMNd2Lc9n5GJdkK inzbCPYHR4kb6obils4TB7zMjPBnYfb5eAA/Zt2Qq9Owy2sSR0OUB0MPnC7kWgUw+Sdk p1xVjdkJo7xU4vE4Q0WjvUAFsf7NQw6RV6cHu7RidGU6mkAy+Meo+BwtGnLqWs3E5HMm RtA4Dwgy5VsAU7ROVv64Q5yZstuYcMDn/OMr80wRtB12qmAY4LVK6M3g6CyAdrqvOLdl VBMw== X-Forwarded-Encrypted: i=1; AJvYcCVXECdTVbCe1t6khS4WCxneITPxVB85yHKnhX1zRiLE90ruXVGdhMv47iJ9F5XfNKZ8KmJBV8qJ6c44YygOQmZ2bN6m9YgaYoXIVz64 X-Gm-Message-State: AOJu0YwzzseWayX3oJGKNIdgxWEG5qqKn3XrtAkzjCR1QfI10VKcA1Y+ ydNXY350lzuuTZaHALKBA48xKlJz+bpGfaeQ+Z6ei1vcmHo6jBccbjN8ILB5GA== X-Google-Smtp-Source: AGHT+IHD5DOmOr2nUz59Bfple5VcMmCJtdiKzwwn2KQ/kHy7BLf5evZIyHYa8ySlQOOQSWDiJjefKQ== X-Received: by 2002:a05:620a:1983:b0:78d:6339:b943 with SMTP id bm3-20020a05620a198300b0078d6339b943mr1698615qkb.20.1712886649123; Thu, 11 Apr 2024 18:50:49 -0700 (PDT) Received: from ip-172-31-44-15.us-east-2.compute.internal (ec2-52-15-100-147.us-east-2.compute.amazonaws.com. [52.15.100.147]) by smtp.googlemail.com with ESMTPSA id f10-20020a05620a15aa00b0078d76c1178esm1756677qkk.119.2024.04.11.18.50.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 18:50:48 -0700 (PDT) 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 , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo Cc: Robert O'Callahan , bpf@vger.kernel.org, Ingo Molnar , Mark Rutland , Alexander Shishkin , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org Subject: [PATCH v6 2/7] perf/bpf: Create bpf_overflow_handler() stub for !CONFIG_BPF_SYSCALL Date: Thu, 11 Apr 2024 18:50:14 -0700 Message-Id: <20240412015019.7060-3-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412015019.7060-1-khuey@kylehuey.com> References: <20240412015019.7060-1-khuey@kylehuey.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This will allow __perf_event_overflow() (which is independent of CONFIG_BPF_SYSCALL) to call bpf_overflow_handler(). Signed-off-by: Kyle Huey --- kernel/events/core.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kernel/events/core.c b/kernel/events/core.c index ee025125a681..a7c2a739a27c 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9624,6 +9624,12 @@ static void perf_event_free_bpf_handler(struct perf_= event *event) bpf_prog_put(prog); } #else +static void bpf_overflow_handler(struct perf_event *event, + struct perf_sample_data *data, + struct pt_regs *regs) +{ +} + static int perf_event_set_bpf_handler(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie) --=20 2.34.1 From nobody Sun Oct 26 22:32:33 2025 Received: from mail-qk1-f177.google.com (mail-qk1-f177.google.com [209.85.222.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 C22C113FEE for ; Fri, 12 Apr 2024 01:50:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886654; cv=none; b=hWQDzaEm4do4mSlUU4i/W0vmnnICV1kWgF0PBiJ9pj3f9pwXkcJv7py6eHrK8RBg6JNpRCx97dC6IkO1YWaM3a2gBb3XilZrPyuwQdUuJmmOBsttyXEViH+Dw5xWY62B6WDV8Kwi7+Qs/DistRip0wnBqWeBv64iwX5DqfBp9JA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886654; c=relaxed/simple; bh=mqvWJPjJTCHg1USd6XVsQJr18Mdq8CtI+8+xAlWcLkc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gZaiHe78b7xljmwBXAscLmPeefquomEIBqIGLcpwt4GK/9DXMxNac4Mjb2TaDhqtJ0cdabJpr7vjtfAfSSXoeksevJTzysTlZqKlK4z60U6kl8U0U3LlYmD/XGg1DVnvlWjJTM+drNwEUmHfxHO7ItUF7X2UY2XSQVjIp1bEClA= 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=ikZxtLYP; arc=none smtp.client-ip=209.85.222.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="ikZxtLYP" Received: by mail-qk1-f177.google.com with SMTP id af79cd13be357-78d777d7d1fso23518785a.1 for ; Thu, 11 Apr 2024 18:50:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1712886652; x=1713491452; 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=aQsjAVDSJojOvZcYxeh0Pu2aG5e5UAQY3ALs9BYs2YU=; b=ikZxtLYPc50oIz/X4NEG7KZwqzRruUor+I3VOuDCfG1LQyrc5eylNrOuhZSfmSHmAc am3oTb/lwx3Jy6pqHyzqgcZfOwSkwSbeGQex0IwdoOT4Kt6dbEVbigxJTP7RF3khhV0I 17fNqUWNiycdyHmG0bKl0Keqnqw+5Tc7G+k6rsLoJdlBuT0zmfU0DjBrMYAAA5Lrpglz l/cDG6eWN7C2FIUjo5yOr1CznpuXnDd6ImjaEGKYf66XxFnVXfc2oV6BRvzJpCWDMobG Af7OOslT8s9085vEGyCOAduaWwbVuKC+1AGlV3iSK6iA5dt1gegimT+XMjesqDuym9+u MQ+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712886652; x=1713491452; 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=aQsjAVDSJojOvZcYxeh0Pu2aG5e5UAQY3ALs9BYs2YU=; b=RgBX/D/0LQ25Zapck7z2mSJq11qU+BeEADUOxYCw3gu533DO6KSOdRJ5q72fkxsJ6L UUJY5Y8HyucJmJiXIu+qN0XbwiZMHEhU0xn9Hlc4ppwCjNGc2v6U+zRz4RB8NlX4oxsY aSK0cUpqqSxnn7CF12ZK33W2ALNFqu2oDX5dTb10IpvefpJfLG9PH0jvYpb8jcMa5l99 tIfaWbNI9qHWCgGhW+CxDeAngV4ucL8Njt3IZUdP+qWkWz9rLZgCqMmicUoiZx8LGaxh dXJQ5P7Ya+As113zHV9QEhZ9gFcravvCyiZmQqjKmgtdCZLKDE1OWBMXtbjparISjFMl UiGg== X-Forwarded-Encrypted: i=1; AJvYcCWiNE3SpGvhAGHmMLgLt4+VOlbE4i3zfMmoBOQFUh40YZnebfPFPlcOWdSVSkn0fW0tWoDgxY6Pu0DeMorZSi0cL8keFDqy4T6GBPR6 X-Gm-Message-State: AOJu0Yw/C90PV5JzLj+0ti4HGstiMUhJVvRz0Iu/p+rzxNpfVVcxlW+s +IJnWImIj6DJsMkAtoiA5sWPq36+SPsgX7wA3PIoDbEeETyifvmZMs+y2VqS3g== X-Google-Smtp-Source: AGHT+IGKuvAU3YlymoVRT8hC2x9jWoKM60y5jVQlZXrkYD5Z3LF1GHcVUdYxkusrFaNEaXHiGScCmQ== X-Received: by 2002:a05:620a:15ac:b0:78d:3b3f:50cd with SMTP id f12-20020a05620a15ac00b0078d3b3f50cdmr1344767qkk.65.1712886651836; Thu, 11 Apr 2024 18:50:51 -0700 (PDT) Received: from ip-172-31-44-15.us-east-2.compute.internal (ec2-52-15-100-147.us-east-2.compute.amazonaws.com. [52.15.100.147]) by smtp.googlemail.com with ESMTPSA id f10-20020a05620a15aa00b0078d76c1178esm1756677qkk.119.2024.04.11.18.50.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 18:50:51 -0700 (PDT) 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 , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo Cc: Robert O'Callahan , bpf@vger.kernel.org, Ingo Molnar , Mark Rutland , Alexander Shishkin , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org Subject: [PATCH v6 3/7] perf/bpf: Remove #ifdef CONFIG_BPF_SYSCALL from struct perf_event members Date: Thu, 11 Apr 2024 18:50:15 -0700 Message-Id: <20240412015019.7060-4-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412015019.7060-1-khuey@kylehuey.com> References: <20240412015019.7060-1-khuey@kylehuey.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This will allow __perf_event_overflow() (which is independent of CONFIG_BPF_SYSCALL) to use struct perf_event's prog to decide whether to call bpf_overflow_handler(). Suggested-by: Ingo Molnar Signed-off-by: Kyle Huey --- include/linux/perf_event.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index d2a15c0c6f8a..07cd4722dedb 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -809,11 +809,9 @@ struct perf_event { u64 (*clock)(void); 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 =20 #ifdef CONFIG_EVENT_TRACING struct trace_event_call *tp_event; --=20 2.34.1 From nobody Sun Oct 26 22:32:33 2025 Received: from mail-qk1-f172.google.com (mail-qk1-f172.google.com [209.85.222.172]) (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 9E2A615ACB for ; Fri, 12 Apr 2024 01:50:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886657; cv=none; b=gxMlhGcg+r3BojTOVo0tSkqof0VfAYinP7xs8QoWJsVkRAdVPMGxNj4bBO5XmiB5N8DjC0oxv7C9OoXt2tLCt0/8kK21t+3es5BRjsCDZUJvQOf2muBIALQ2eNfNSBUFVG7XzrFbmy2PCucJSULhK+ICOABKDWCF6GmxD4s3mjs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886657; c=relaxed/simple; bh=j+JlZhELkbUyrxfK0CeiZLK3yBU6Q8rGCixqPhBA6EU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ewKna8TSRwMLnqrek5McnmfWsuxqp9KlSpm1SwxiWNBM5QDGLcTmoVcnMIVukfWTDQSufTRz97yy+Ev3huxElpte036lcXMXi8z7eqxTreEvZpvTT+DFW+psLEEqcL9b9j7Qr5QQAQXnXLEmCuiWfSA9IlG4LXShRPz5fNK9pAQ= 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=U1Flfrhd; arc=none smtp.client-ip=209.85.222.172 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="U1Flfrhd" Received: by mail-qk1-f172.google.com with SMTP id af79cd13be357-78d723c0dc5so30841885a.2 for ; Thu, 11 Apr 2024 18:50:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1712886653; x=1713491453; 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=MEkPkaevs/85pKXIFzAxTV+0rXNtmtbXbn0hIUFujbg=; b=U1FlfrhdO9xd3qiKzuDF5u2WHSrUxvKxOWjhZBBNwr8YsurwdNBdKHxx/tiiDYwODe FklQ9x+0lbEybH3z8QFAsoKPOhc+cvO7EHOSzErbl7YbBGlZjGW3xKO0s7sxv2Vyl9Sj nZA7WUxCK4LKCVqGkOL9Kkh55LjRZch68BX8XLMSteGSC2FWHb+x7qvzQ2R3oKFtJOhs 4zkoaZwXlvof4qMBNKBvqgn0OqWcUQJm2WeLURh8jAy1Hq8pxGZwWuly3tfXc4Bqk2U7 +cbLOAMj+O4d/kcfs2rWk/peyd/zfgmNqRNHBupYKo1O99tLKS119oRb3ivEmcbJWq3Y m2Bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712886653; x=1713491453; 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=MEkPkaevs/85pKXIFzAxTV+0rXNtmtbXbn0hIUFujbg=; b=W3mQamW3zW4/9Wr0aHVzTHopERqGhielumFQ09LzsRU4yHflqJPM1D6KZr9M75S+r8 rVd8URgg/Qrg6hErGgOLtF27EOGo6uIJltLIz0LxieO0YOiAenqHfidHZx5+8pov4vzt oDRU6K54lrXGqaXRKGoqBBjwO2NAxNFgHaAqWikC5rjUyZtm4r0A/MySGRM9uazcrBon Sw0Rvim7iU9DqAozAMutL7QNnqyyHu7nHfCSu1ZDBRP9OUMCa9EcFwSMMPHC5QWMw2UW mPjhC1dvuUL49UPbfrn36zlPSuBEwTmZR0mpw1DLPOOsMqx/dHEeUfBeMMXZaMN+Ap1I 8ImQ== X-Forwarded-Encrypted: i=1; AJvYcCWo+9GMrC5DLdmfkoA4cMFEo6Xh0qv6g/y+rhHQXX1pvIw+H1EqvoNLHNiMEVCUBmU5eEzgWcaRscMfGrXws8Z6DhjzihGi4mCR3LLF X-Gm-Message-State: AOJu0Yx3Ka1iSE3sghXN794PfUuO0fE7be5b3lP10NC0xnOCWKoAbdS7 3e8SmhXrFXgsmgH65hERpAEHsg+/YHbQhwWkHF2ZrYj4HWgIE/HIWf3pHqjQTg== X-Google-Smtp-Source: AGHT+IEbFaxDuu1GXCiEZNx7U9M0URvr9l8ow3pyXrdyfGxSiceYpsCWsh1lyCxVKzYndLYworc6Jw== X-Received: by 2002:a05:620a:1a93:b0:78d:5714:6698 with SMTP id bl19-20020a05620a1a9300b0078d57146698mr1675936qkb.4.1712886653450; Thu, 11 Apr 2024 18:50:53 -0700 (PDT) Received: from ip-172-31-44-15.us-east-2.compute.internal (ec2-52-15-100-147.us-east-2.compute.amazonaws.com. [52.15.100.147]) by smtp.googlemail.com with ESMTPSA id f10-20020a05620a15aa00b0078d76c1178esm1756677qkk.119.2024.04.11.18.50.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 18:50:53 -0700 (PDT) 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 , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo Cc: Robert O'Callahan , bpf@vger.kernel.org, Song Liu , Andrii Nakryiko , Ingo Molnar , Mark Rutland , Alexander Shishkin , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org Subject: [PATCH v6 4/7] perf/bpf: Call bpf handler directly, not through overflow machinery Date: Thu, 11 Apr 2024 18:50:16 -0700 Message-Id: <20240412015019.7060-5-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412015019.7060-1-khuey@kylehuey.com> References: <20240412015019.7060-1-khuey@kylehuey.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Suggested-by: Namhyung Kim Signed-off-by: Kyle Huey Acked-by: Song Liu Acked-by: Jiri Olsa Acked-by: Andrii Nakryiko --- include/linux/perf_event.h | 6 +----- kernel/events/core.c | 27 +++++++++++---------------- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 07cd4722dedb..65ad1294218f 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -809,7 +809,6 @@ struct perf_event { u64 (*clock)(void); perf_overflow_handler_t overflow_handler; void *overflow_handler_context; - perf_overflow_handler_t orig_overflow_handler; struct bpf_prog *prog; u64 bpf_cookie; =20 @@ -1355,10 +1354,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 a7c2a739a27c..fd601d509cea 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9545,9 +9545,9 @@ static inline bool sample_is_allowed(struct perf_even= t *event, struct pt_regs *r } =20 #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, @@ -9568,10 +9568,8 @@ static void bpf_overflow_handler(struct perf_event *= event, rcu_read_unlock(); out: __this_cpu_dec(bpf_prog_active); - if (!ret) - return; =20 - event->orig_overflow_handler(event, data, regs); + return ret; } =20 static int perf_event_set_bpf_handler(struct perf_event *event, @@ -9607,8 +9605,6 @@ static int perf_event_set_bpf_handler(struct perf_eve= nt *event, =20 event->prog =3D prog; event->bpf_cookie =3D bpf_cookie; - event->orig_overflow_handler =3D READ_ONCE(event->overflow_handler); - WRITE_ONCE(event->overflow_handler, bpf_overflow_handler); return 0; } =20 @@ -9619,15 +9615,15 @@ static void perf_event_free_bpf_handler(struct perf= _event *event) if (!prog) return; =20 - WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler); event->prog =3D NULL; bpf_prog_put(prog); } #else -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) { + return 1; } =20 static int perf_event_set_bpf_handler(struct perf_event *event, @@ -9711,7 +9707,8 @@ static int __perf_event_overflow(struct perf_event *e= vent, irq_work_queue(&event->pending_irq); } =20 - READ_ONCE(event->overflow_handler)(event, data, regs); + if (!(event->prog && !bpf_overflow_handler(event, data, regs))) + READ_ONCE(event->overflow_handler)(event, data, regs); =20 if (*perf_event_fasync(event) && event->pending_kill) { event->pending_wakeup =3D 1; @@ -11978,13 +11975,11 @@ perf_event_alloc(struct perf_event_attr *attr, in= t 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; =20 bpf_prog_inc(prog); event->prog =3D prog; - event->orig_overflow_handler =3D - parent_event->orig_overflow_handler; } #endif } --=20 2.34.1 From nobody Sun Oct 26 22:32:33 2025 Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com [209.85.222.180]) (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 638B717BA9 for ; Fri, 12 Apr 2024 01:50:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886658; cv=none; b=OV/U85f53ayn23Oi3cnAw8nBq97ce5ulFqID+d5t4fzyr0culpYgTDzwYm/sW5x3dZ/3mK4whp+fmGGlsp0zGCGzzCw/u9RegTScwdu08XGf+AV83qcxWu2Jrad4aZugshoBiaL85eyTEgVwdpUZ96a215X2wdY8m8U/oyXW31o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886658; c=relaxed/simple; bh=NRmEZmEzOcudL2cLJi5SCwBCdAXkrrS/5QKylKnvPRI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NgVYg+3hggVJq2QKmN5YpkHdwnGIsWjSgKLvOYSrva0NYE5tSrfxkBy7R4gmCAr2JTH3fFzZubjR3PPPGdfLt+mCPmnoI1gLEb8kpd6xDx24diKA+q4V8abNhbj2xKPP1nsSOj+oS9CfcsDdKd6TLIwqvSD+EZzWpmc6pOqQcNQ= 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=Xn8JkP7N; arc=none smtp.client-ip=209.85.222.180 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="Xn8JkP7N" Received: by mail-qk1-f180.google.com with SMTP id af79cd13be357-78d57bd577dso33818285a.2 for ; Thu, 11 Apr 2024 18:50:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1712886655; x=1713491455; 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=XmAdJT3ZUCPeWfD1wo8jRuZywHZwwpve3RZWOW9haf0=; b=Xn8JkP7NU04LMLIgfilXIt3KHG4T6B1Vtz1uXM2qEKoiTj8FPRP5gA63Z4JLuoTKPa BUCWI7e4/gN7/ilOtU16JGX26IzFpvJhhRBiifpadbzRHLLNNWEz77cpp/tY5gns7cFx 8f2/jiQ7kCpX4O9YskqqC9JsIQclJSMMziIFEKlzvOq4Dj33AT0moOJSK9wzjhpDcYoV 2CGc8CYJNxGvgbZZifH8HPnX0bJnIyQRxFvt6+mW+opRh3QdiRdXr6+3MlH8+Lc+0ALI OYfgCe3oNsJ1PLp5eqiTM2Ehbs4QEtt4dwhtjWozsOXZ/Ej0ZcrY8tkoNTYEJ9jpDfXF Cpkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712886655; x=1713491455; 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=XmAdJT3ZUCPeWfD1wo8jRuZywHZwwpve3RZWOW9haf0=; b=bPdYgsyoZCZfbskgptmxS+JxP7Wi1U/46l2MHHSAVAiPIcpjDc2oe6KaSmyjXGk6Bw SF7QBEfS27bQKtCn+t2m1EcZ3fPUlkG7wBBY1nc5MOWkU1fEhD73j3at4RIoXMO5Kdy1 d6frLWs3lhVKT4xP2gW926NBffWopOdFvgA1OYqVw5HW/pr8YOnI1UjfJtpzL51aZH5D Q+E2yCuWHuN3N8a227sKEBXcopDH/Q4mL+ZEKSRNpLUC3sZSBhA6OQSyc7Oi5aQcS9f3 Zct9lt3c3kgjM47AtLSf2L6lbXF7u1S5uoHQPDTEvNbGO28rk476BrsgqtjsY4DhvO4O JL1A== X-Forwarded-Encrypted: i=1; AJvYcCV8j1DzC5w4U+oQ0I5jP5Q7ncv4du5Rp+LXOcH1u3aCVshB2p+I3pfyj7kI1RR2tDEuH2phvRg1Dt+e0XtBSdRp1Kx5vUj8Q8YVHOca X-Gm-Message-State: AOJu0Yw4C1alf79cKwanDdm/JA0zdzQyfcMVe0JhGIeHrL+Yuig/RX7s MrOJ9eStTdV7M2D++4tFEi5DQ94PP6oInR0T72lc+Uu97MLqCOe8tMFc91h6kg== X-Google-Smtp-Source: AGHT+IH1CdfzeVz3pZajhCl9CVe8VCWrOdYFKQJn9dqM+zf6JXUpBa7+69QfvqWLEhWCHiBE2XF4sw== X-Received: by 2002:a05:620a:2211:b0:78d:68d6:c1e9 with SMTP id m17-20020a05620a221100b0078d68d6c1e9mr1314228qkh.47.1712886655388; Thu, 11 Apr 2024 18:50:55 -0700 (PDT) Received: from ip-172-31-44-15.us-east-2.compute.internal (ec2-52-15-100-147.us-east-2.compute.amazonaws.com. [52.15.100.147]) by smtp.googlemail.com with ESMTPSA id f10-20020a05620a15aa00b0078d76c1178esm1756677qkk.119.2024.04.11.18.50.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 18:50:55 -0700 (PDT) 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 , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo Cc: Robert O'Callahan , bpf@vger.kernel.org, Will Deacon , Song Liu , Andrii Nakryiko , Mark Rutland , Russell King , Catalin Marinas , Ingo Molnar , Alexander Shishkin , Ian Rogers , Adrian Hunter , linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org Subject: [PATCH v6 5/7] perf/bpf: Remove unneeded uses_default_overflow_handler() Date: Thu, 11 Apr 2024 18:50:17 -0700 Message-Id: <20240412015019.7060-6-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412015019.7060-1-khuey@kylehuey.com> References: <20240412015019.7060-1-khuey@kylehuey.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that struct perf_event's orig_overflow_handler is gone, there's no need for the functions and macros to support looking past overflow_handler to orig_overflow_handler. This patch is solely a refactoring and results in no behavior change. Signed-off-by: Kyle Huey Acked-by: Will Deacon Acked-by: Song Liu Acked-by: Jiri Olsa Acked-by: Andrii Nakryiko --- arch/arm/kernel/hw_breakpoint.c | 8 ++++---- arch/arm64/kernel/hw_breakpoint.c | 4 ++-- include/linux/perf_event.h | 17 +++-------------- 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/arch/arm/kernel/hw_breakpoint.c b/arch/arm/kernel/hw_breakpoin= t.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 &=3D ~alignment_mask; hw->ctrl.len <<=3D offset; =20 - 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, unsi= gned 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, unsi= gned int fsr, info->trigger =3D addr; pr_debug("watchpoint fired: address =3D 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)); } =20 @@ -886,7 +886,7 @@ static void breakpoint_handler(unsigned long unknown, s= truct pt_regs *regs) info->trigger =3D addr; pr_debug("breakpoint fired: address =3D 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_break= point.c index 2f5755192c2b..722ac45f9f7b 100644 --- a/arch/arm64/kernel/hw_breakpoint.c +++ b/arch/arm64/kernel/hw_breakpoint.c @@ -655,7 +655,7 @@ static int breakpoint_handler(unsigned long unused, uns= igned long esr, perf_bp_event(bp, regs); =20 /* Do we need to handle the stepping? */ - if (uses_default_overflow_handler(bp)) + if (is_default_overflow_handler(bp)) step =3D 1; unlock: rcu_read_unlock(); @@ -734,7 +734,7 @@ static u64 get_distance_from_watchpoint(unsigned long a= ddr, u64 val, static int watchpoint_report(struct perf_event *wp, unsigned long addr, struct pt_regs *regs) { - int step =3D uses_default_overflow_handler(wp); + int step =3D is_default_overflow_handler(wp); struct arch_hw_breakpoint *info =3D counter_arch_bp(wp); =20 info->trigger =3D addr; diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 65ad1294218f..13a2b05cc431 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1339,8 +1339,10 @@ extern int perf_event_output(struct perf_event *even= t, struct pt_regs *regs); =20 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 =3D event->overflow_handler; + if (likely(overflow_handler =3D=3D perf_event_output_forward)) return true; if (unlikely(overflow_handler =3D=3D perf_event_output_backward)) @@ -1348,19 +1350,6 @@ __is_default_overflow_handler(perf_overflow_handler_= t overflow_handler) return false; } =20 -#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, --=20 2.34.1 From nobody Sun Oct 26 22:32:33 2025 Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) (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 E77A7168A8 for ; Fri, 12 Apr 2024 01:50:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886659; cv=none; b=iKw/fXQRcNAA7PHUGGK7kno44uBzpFF/h6lWNco8/+l7+hzXdmyCmeV8PSJFejsIO2XzS179ION/j2QqILhflm2rUaMTGkgh3luO5c7zbQdLQ8aAjwAGqqS1xnbr2pkdHBM0rKeWX45Y+9qI7xCT48XzvVv+3zakZH/bLsCJtUo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886659; c=relaxed/simple; bh=AdlygNz0RFZef1mcIWoB8it1jPVZwP5sUo9GIGfOMoo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gmf9q9oqIB0XmYnVnbRlH0pElKfbnwk8nW2BHNmZJsfos5L7mJkiLikM9pKyohVl2kIL+a+5Hg4+SLxID9Wbr3EG79Cvz3QpnXomTvOZ2O17G9/JRPGJlS2fo8eqNI4O5JZduyJDp7s0KpOzmolIunEXrbcvdxTaxO5wPJgt3QU= 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=CCl9p0GX; arc=none smtp.client-ip=209.85.128.169 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="CCl9p0GX" Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-6181b9dc647so4605367b3.1 for ; Thu, 11 Apr 2024 18:50:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1712886657; x=1713491457; 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=6W3XOu1JzCFnI8d0V7+Yp4cwFPdA5VsCmQsQwSODsxU=; b=CCl9p0GXVhmMlklK1/QoFpwlvhnnwH9VJ6mihBgzv9ai+0wV+zdgyYF5DDcwfVvZ0A NpAeMCJ5pzQDBeMCHLV+0RSIyNcdBBLSlWajhj8zmISfuWrCP/6eJbLx0NlvF4o+Vu/5 QJvaBVvfdXhfmvj2FBV7mONS8Pr0sxnKTRr1zdEpruDXSTIGTG+FS9BRCePljkO1+shF JxeBCMBF+8/FBc5e9LoLcpFnwUanMDBr7bFwUvABXtPlt9gt/LzxWUbpy5dhr+GjWMcc KLp0vUJeh4MtKQWg8Yhfa7JeaUt4OmhD+gmfZW6AJsmST2OSMn6sIct5UVZEu+MuGXLl 9hYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712886657; x=1713491457; 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=6W3XOu1JzCFnI8d0V7+Yp4cwFPdA5VsCmQsQwSODsxU=; b=BNpf7nX0muX4PI9QhUh5fy+gr280UFTEO2xaDlQGi8OjMwzP1kezYTVtR+REFTWe8c VTIoJTa9//YfTd4S7hQBD8AChfn/3xw9OL9ywN8gOIoSH5Y7osuP7BEMNdrsz/Ny4AQe QPUs6nmfVb2LpiI0zs1kWzLxxg/45fghI54de0nqK4Dco4I7tV2FiXpqcVcnihpzJXff fp3x9HqHnhvMMtZcO4DvqME/5iqI1pvlTvCMf48+eflnyCuuuyPJR1PStR3kYJjZlFEp HaLGk/hBu7uood1TNdxKAhAoJDPxq3H5RFwva1OkXzPvVHA+OXKggnipHflLmptVI22g h0lg== X-Forwarded-Encrypted: i=1; AJvYcCXtMl7wmSM5oJFPA7IzlwkO9oGCkT9qGqI5p/aFofeUgPnmJIjnaTOwJWiXf8pIFlpAR9X+SEwhq5VNweIpB1QPVGnwXooBQqJMFi/9 X-Gm-Message-State: AOJu0YxvoM2pa8Jv2wn/OIT8jahYEJGW/sg/kVTTKbBhb+L0CTIvelTS DEN1OcJs+cLhoTT/DTZQl4WQiuqcygjbuWPTeCWkms6SzwGtY/45ROpMEJSZfg== X-Google-Smtp-Source: AGHT+IHjXR7BfIecARc6VI1pATfh8TR/3Pi2gT4lAF8cWX7oY3B82GGlWqZk5F7vE4hcCX37K6VYow== X-Received: by 2002:a81:af54:0:b0:611:5ff6:2608 with SMTP id x20-20020a81af54000000b006115ff62608mr1280098ywj.28.1712886656935; Thu, 11 Apr 2024 18:50:56 -0700 (PDT) Received: from ip-172-31-44-15.us-east-2.compute.internal (ec2-52-15-100-147.us-east-2.compute.amazonaws.com. [52.15.100.147]) by smtp.googlemail.com with ESMTPSA id f10-20020a05620a15aa00b0078d76c1178esm1756677qkk.119.2024.04.11.18.50.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 18:50:56 -0700 (PDT) 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 , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo Cc: Robert O'Callahan , bpf@vger.kernel.org, Song Liu , Andrii Nakryiko , Ingo Molnar , Mark Rutland , Alexander Shishkin , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org Subject: [PATCH v6 6/7] perf/bpf: Allow a bpf program to suppress all sample side effects Date: Thu, 11 Apr 2024 18:50:18 -0700 Message-Id: <20240412015019.7060-7-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412015019.7060-1-khuey@kylehuey.com> References: <20240412015019.7060-1-khuey@kylehuey.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Returning zero from a bpf program attached to a perf event already suppresses any data output. Return early from __perf_event_overflow() in this case so it will also suppress event_limit accounting, SIGTRAP generation, and F_ASYNC signalling. Signed-off-by: Kyle Huey Acked-by: Song Liu Acked-by: Jiri Olsa Acked-by: Namhyung Kim Acked-by: Andrii Nakryiko --- kernel/events/core.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index fd601d509cea..cd88d1e89eb8 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9658,6 +9658,9 @@ static int __perf_event_overflow(struct perf_event *e= vent, =20 ret =3D __perf_event_account_interrupt(event, throttle); =20 + if (event->prog && !bpf_overflow_handler(event, data, regs)) + return ret; + /* * XXX event_limit might not quite work as expected on inherited * events @@ -9707,8 +9710,7 @@ static int __perf_event_overflow(struct perf_event *e= vent, irq_work_queue(&event->pending_irq); } =20 - if (!(event->prog && !bpf_overflow_handler(event, data, regs))) - READ_ONCE(event->overflow_handler)(event, data, regs); + READ_ONCE(event->overflow_handler)(event, data, regs); =20 if (*perf_event_fasync(event) && event->pending_kill) { event->pending_wakeup =3D 1; --=20 2.34.1 From nobody Sun Oct 26 22:32:33 2025 Received: from mail-yw1-f181.google.com (mail-yw1-f181.google.com [209.85.128.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 E94FD24B23 for ; Fri, 12 Apr 2024 01:51:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886667; cv=none; b=DXafGeP5tZe+YkaYDrHrHcuCYaKfhgbaK90URU8fjCn70sz/hYoI+hTR95N1K1sHw9HIqLgLvrIXhXhhPiHMyoGWsURU6KP0ywzIgr8v2Uor70LiLtYizROAx7SJvqL/s8nCJq08ApezgLv3MUlcU8eyY6dz4OHRJcWmtDNkqy4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886667; c=relaxed/simple; bh=EfTqzolUvIxbeV77szfL3e0e1IBqxiKk0wQkbdth9Jg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=E/pRaQ5zCni/ujW2Om4c8vIXjukF3tbxQAxVnC95zSBXeUWkMmv4KYhbQgQK0UV8ll/ExvWJg6CkJSAbs6hFwxw9d+aUJNWJmODaqm+jmB8UuBOFg81fDK1DAbfQzEJ8A6/t62FMKQtmjjBBedsmBf9BAIqlJJnWB1rS2/eFKM0= 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=BBumYXRF; arc=none smtp.client-ip=209.85.128.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="BBumYXRF" Received: by mail-yw1-f181.google.com with SMTP id 00721157ae682-617d4797d9bso3587177b3.1 for ; Thu, 11 Apr 2024 18:51:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1712886665; x=1713491465; 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=PLsAtO8E8WEI0YPLnWc4zGD4gPrub60dCUyhat+mc0A=; b=BBumYXRFyY4oAk5wBat/ahkj0vo4ZnGDizw9oe7uiI4of7VQSdzqMqicfgz6Iqva3+ TWB3iYBSoKCwGzfyBadYhTJblNxUidbYGWt29yAxB5uG5AVFqqKSDB9JIUZvJIq3h5un AqcdhEOTYPdqqEB4RwLqMoOsWiVbiB2K6Ecn3dTiLeWaFd5ZlStzsB5463mCQOB7hSHP EamyeE7eoRo/SMtDLC1565o/z8czbepN5SJj7jsJXvH6TKSiN6tBk8KfyjROBuQ25zXo YliCS1PX3q8xNQUWIf+bDWLpFXKq81+s1iRIV0vtGBpRQ4V73mrheJV4nWzRPm6DOH2i aitQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712886665; x=1713491465; 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=PLsAtO8E8WEI0YPLnWc4zGD4gPrub60dCUyhat+mc0A=; b=XnjZvCU9Dpn0APyImLZLDrZlCx+C+2FB8u8H6XslJHqdijkfyEPHFI/Bnn2YFIqgL6 +ClHx6ityJ9jdRp83ImxaEaag6KWAL0dRh8N4WrB/z+RVXA1cmZblJQ5hBpFXSGlMpwj /oemwqyTCReTDqT80XZxcbVOoX2hxbNQOzfBPxw8c895/ecr18tbQujjopGrAL/0iy/O ZFVq1olDgWy+RMDaWl0uuilQm7p8jdUrDUhTjJUHOIcQy6ZXlOPgllhMHSJzG9i8Gr+6 kFfKQMIQOTxfv0VYLLS+8AOQDCdt7imTOpoEIH5LcPCSmAZf/ZoIK/6CmS8cDGb1k9Ie e3zA== X-Forwarded-Encrypted: i=1; AJvYcCWKNC3yQklnYPWXyAGqEaz7sz4u6EfNlrWlHv131z3vOUlX7QRjXJdonMmAvXeIr0oC+fFYfCF72JHBpxfw+FNhycTmXoV2ZU4P12o2 X-Gm-Message-State: AOJu0YwdkCkBL2zTEHz2Qp0vZHYkVFUoIOH1XaGSiRh4sLoaeu/2lz+J qWyJQylDCbVTkYR2GahBE4UeLwvSjpFDIzLvzklD96suiDac1s0vm/92ifIa5w== X-Google-Smtp-Source: AGHT+IF6KWE2O+uOj1W7BUPtfwmixy3UO/O70mlonobqoOzGHLL5MXKtYfA1MJKuTWaSz4KGSxyRhQ== X-Received: by 2002:a81:4ec7:0:b0:615:21e7:6bf6 with SMTP id c190-20020a814ec7000000b0061521e76bf6mr1172665ywb.14.1712886664785; Thu, 11 Apr 2024 18:51:04 -0700 (PDT) Received: from ip-172-31-44-15.us-east-2.compute.internal (ec2-52-15-100-147.us-east-2.compute.amazonaws.com. [52.15.100.147]) by smtp.googlemail.com with ESMTPSA id f10-20020a05620a15aa00b0078d76c1178esm1756677qkk.119.2024.04.11.18.51.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 18:51:04 -0700 (PDT) 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 , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo Cc: Robert O'Callahan , bpf@vger.kernel.org, Song Liu , Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Shuah Khan , linux-kselftest@vger.kernel.org Subject: [PATCH v6 7/7] selftest/bpf: Test a perf bpf program that suppresses side effects. Date: Thu, 11 Apr 2024 18:50:19 -0700 Message-Id: <20240412015019.7060-8-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412015019.7060-1-khuey@kylehuey.com> References: <20240412015019.7060-1-khuey@kylehuey.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The test sets a hardware breakpoint and uses a bpf program to suppress the side effects of a perf event sample, including I/O availability signals, SIGTRAPs, and decrementing the event counter limit, if the ip matches the expected value. Then the function with the breakpoint is executed multiple times to test that all effects behave as expected. Signed-off-by: Kyle Huey Acked-by: Song Liu Acked-by: Jiri Olsa Acked-by: Andrii Nakryiko --- .../selftests/bpf/prog_tests/perf_skip.c | 137 ++++++++++++++++++ .../selftests/bpf/progs/test_perf_skip.c | 15 ++ 2 files changed, 152 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/perf_skip.c create mode 100644 tools/testing/selftests/bpf/progs/test_perf_skip.c diff --git a/tools/testing/selftests/bpf/prog_tests/perf_skip.c b/tools/tes= ting/selftests/bpf/prog_tests/perf_skip.c new file mode 100644 index 000000000000..37d8618800e4 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/perf_skip.c @@ -0,0 +1,137 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE + +#include +#include "test_perf_skip.skel.h" +#include +#include +#include + +#ifndef TRAP_PERF +#define TRAP_PERF 6 +#endif + +int sigio_count, sigtrap_count; + +static void handle_sigio(int sig __always_unused) +{ + ++sigio_count; +} + +static void handle_sigtrap(int signum __always_unused, + siginfo_t *info, + void *ucontext __always_unused) +{ + ASSERT_EQ(info->si_code, TRAP_PERF, "si_code"); + ++sigtrap_count; +} + +static noinline int test_function(void) +{ + asm volatile (""); + return 0; +} + +void serial_test_perf_skip(void) +{ + struct sigaction action =3D {}; + struct sigaction previous_sigtrap; + sighandler_t previous_sigio =3D SIG_ERR; + struct test_perf_skip *skel =3D NULL; + struct perf_event_attr attr =3D {}; + int perf_fd =3D -1; + int err; + struct f_owner_ex owner; + struct bpf_link *prog_link =3D NULL; + + action.sa_flags =3D SA_SIGINFO | SA_NODEFER; + action.sa_sigaction =3D handle_sigtrap; + sigemptyset(&action.sa_mask); + if (!ASSERT_OK(sigaction(SIGTRAP, &action, &previous_sigtrap), "sigaction= ")) + return; + + previous_sigio =3D signal(SIGIO, handle_sigio); + if (!ASSERT_NEQ(previous_sigio, SIG_ERR, "signal")) + goto cleanup; + + skel =3D test_perf_skip__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_load")) + goto cleanup; + + attr.type =3D PERF_TYPE_BREAKPOINT; + attr.size =3D sizeof(attr); + attr.bp_type =3D HW_BREAKPOINT_X; + attr.bp_addr =3D (uintptr_t)test_function; + attr.bp_len =3D sizeof(long); + attr.sample_period =3D 1; + attr.sample_type =3D PERF_SAMPLE_IP; + attr.pinned =3D 1; + attr.exclude_kernel =3D 1; + attr.exclude_hv =3D 1; + attr.precise_ip =3D 3; + attr.sigtrap =3D 1; + attr.remove_on_exec =3D 1; + + perf_fd =3D syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0); + if (perf_fd < 0 && (errno =3D=3D ENOENT || errno =3D=3D 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 =3D fcntl(perf_fd, F_SETFL, O_ASYNC); + if (!ASSERT_OK(err, "fcntl(F_SETFL, O_ASYNC)")) + goto cleanup; + + owner.type =3D F_OWNER_TID; + owner.pid =3D syscall(__NR_gettid); + err =3D 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 =3D ioctl(perf_fd, PERF_EVENT_IOC_REFRESH, 1); + if (!ASSERT_OK(err, "ioctl(PERF_EVENT_IOC_REFRESH)")) + goto cleanup; + + prog_link =3D 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 =3D (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 =3D 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 >=3D 0) + close(perf_fd); + test_perf_skip__destroy(skel); + + if (previous_sigio !=3D SIG_ERR) + signal(SIGIO, previous_sigio); + sigaction(SIGTRAP, &previous_sigtrap, NULL); +} diff --git a/tools/testing/selftests/bpf/progs/test_perf_skip.c b/tools/tes= ting/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 !=3D PT_REGS_IP(&data->regs); +} + +char _license[] SEC("license") =3D "GPL"; --=20 2.34.1 From nobody Sun Oct 26 22:32:33 2025 Received: from mail-ed1-f44.google.com (mail-ed1-f44.google.com [209.85.208.44]) (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 79F4F535DC; Fri, 12 Apr 2024 09:55:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712915707; cv=none; b=eWsJC28D7UQWWfYHoEO5G5uqgRwKoSvB2ckpGj/sVQGWW/odaZgrzevUcuEJtsaXpq5AjIsmSxd7hdHAg7Yuen3o0He7VOYGfJQlXKkC/JVJeUgaEohVbN+wDkBgNKmaZY0wPYovuoTcPNS0pWVvW/WnJ1Jn+L8Z58B6wWx+ae4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712915707; c=relaxed/simple; bh=MxCP72ppniCUwq/Q6jnueZ8iwN0KI8bkGQ/p/fDaVws=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=MpGcBHQi5ktkW4nixlDJZweHMU4fAHWW0Scd/uxi5vrpM1+pnKURbhplyNGVJJnSKS5JokB5BLLPdr8EiuvvvvrB5CMufsJv3uYAthqS/YUCiOfAgn1fAYNWpEKSgL/J2EV8BmnTrJqIkSl2yaMC9twXF3w/VFlm7NqXDRKZM9c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=fwbru7gt; arc=none smtp.client-ip=209.85.208.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fwbru7gt" Received: by mail-ed1-f44.google.com with SMTP id 4fb4d7f45d1cf-56e37503115so576130a12.1; Fri, 12 Apr 2024 02:55:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712915703; x=1713520503; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:sender:from:to:cc:subject:date:message-id :reply-to; bh=V6GO2vbzaTzYvoUcV4Qpl0H9TBLeVNlexEEn9hFraCw=; b=fwbru7gta6k+X/lIIpKhMDd/u6Zv8CZvGwN5LJNPAC06+hptYFIlEFC4L1h+J/UfdZ vhFI6+9KeYaqkzYOHw7wssdjab7soF/YQ7eDbKp4E8E5fGavwF3SQoJyS5GjFm3gWy1Q i3OhcPx80GE50AgVO9eN5KexPyJD9hF6CzISQ9mOuWA5QvzcWtAKGKYMs7WWY+H9T9Nm DZhHJjDzwByG9Mqnw/apngWRUTrHuaXLLjNHXOMTKJ0G0zHb0V7DYbZ6lshU1F37P2DC jVwnPkWna/rrVAkjqAiwD7o9KHjeu7jBbQd1fzPBMv2RK1RYNEDUkoUuomejz/H1od1i f5Jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712915703; x=1713520503; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:sender:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=V6GO2vbzaTzYvoUcV4Qpl0H9TBLeVNlexEEn9hFraCw=; b=NPaVglt3ch5vBMzYIMgpoFr541v1+ZaT9oE0sitIbAMIoMdq2n0ewH+v4hmP0zWI5+ NaOXPAWbWfhgYagnLhNLbPGM9zupYR2oHPgMgkJVyBN/9ZqdUJ3tbCWYXbciugDKaSyG kWxK+81PQhOh0QDl/t6HBriOLpAsl0FUAWIWN0xWYCPT4FVwafOR4MwLZ7Qz/mfQmuRN YGeNeJXahHtr3H/vDMHSOxu/mfzRQNE31wMfjEO0zKkBss/mJBi+L4dXiJtqdUeu8pyM YNHoUqmXQWCNzlaLvv+WaLNGDjzmEToGOYY2oYyGSUyfG4KsAuA/mVGolRGQ5fM+gJul o41g== X-Forwarded-Encrypted: i=1; AJvYcCX5ia0WQ0t/rew5N7BaIoYMvwzEi4wN+9/HtKXzQ15X1TVYLIOp0WSYuDb7tmq5nfLVfQDy3qfFb9dEn/F+21WYKP+YN0gFVXaYvXfkGEGsy+eReRB9H21wMswOHN18YynX X-Gm-Message-State: AOJu0YzJ4ZlZhqte869J0lZL7j11Io2cJ4MzpG/E9DQREcGTdCM6GjyH bLtcKw2fek1HQ+PKGxMN/BQOpp/U/MJHMV6gN9v3Q5D5n9afGD4S X-Google-Smtp-Source: AGHT+IGnk62JGdMQa/B7hDMdZpD/yfiPhQr3f+cJw9cDK3woWaOPl9ouq7KgndTx/SGys6Fc5mFNZw== X-Received: by 2002:a50:aa9c:0:b0:56e:2ff3:bb89 with SMTP id q28-20020a50aa9c000000b0056e2ff3bb89mr1383824edc.28.1712915703327; Fri, 12 Apr 2024 02:55:03 -0700 (PDT) Received: from gmail.com (1F2EF1A5.nat.pool.telekom.hu. [31.46.241.165]) by smtp.gmail.com with ESMTPSA id 14-20020a0564021f4e00b0056e78dbfe13sm44670edz.32.2024.04.12.02.55.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 02:55:02 -0700 (PDT) Sender: Ingo Molnar Date: Fri, 12 Apr 2024 11:55:00 +0200 From: Ingo Molnar To: Kyle Huey Cc: Kyle Huey , linux-kernel@vger.kernel.org, Andrii Nakryiko , Jiri Olsa , Namhyung Kim , Marco Elver , Yonghong Song , Peter Zijlstra , Arnaldo Carvalho de Melo , Robert O'Callahan , bpf@vger.kernel.org Subject: [PATCH 8/7] perf/bpf: Change the !CONFIG_BPF_SYSCALL stubs to static inlines Message-ID: References: <20240412015019.7060-1-khuey@kylehuey.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240412015019.7060-1-khuey@kylehuey.com> Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Otherwise the compiler will be unhappy if they go unused, which they do on allnoconfigs. Cc: Kyle Huey Signed-off-by: Ingo Molnar --- kernel/events/core.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 2212670cbe9b..6708c1121b9f 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9638,21 +9638,21 @@ static void perf_event_free_bpf_handler(struct perf= _event *event) bpf_prog_put(prog); } #else -static int bpf_overflow_handler(struct perf_event *event, - struct perf_sample_data *data, - struct pt_regs *regs) +static inline int bpf_overflow_handler(struct perf_event *event, + struct perf_sample_data *data, + struct pt_regs *regs) { return 1; } =20 -static int perf_event_set_bpf_handler(struct perf_event *event, - struct bpf_prog *prog, - u64 bpf_cookie) +static inline int perf_event_set_bpf_handler(struct perf_event *event, + struct bpf_prog *prog, + u64 bpf_cookie) { return -EOPNOTSUPP; } =20 -static void perf_event_free_bpf_handler(struct perf_event *event) +static inline void perf_event_free_bpf_handler(struct perf_event *event) { } #endif