Message ID | 20231204201406.341074-3-khuey@kylehuey.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3013204vqy; Mon, 4 Dec 2023 12:14:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IFwR9gFX5ZMUMLkKOsmL7W+Or6VSrQRYaa7cqrY0M/IPkHyxZ01quQHTeNF9INdHX+ho9OR X-Received: by 2002:a05:6a20:914e:b0:18f:97c:9265 with SMTP id x14-20020a056a20914e00b0018f097c9265mr2203476pzc.74.1701720882905; Mon, 04 Dec 2023 12:14:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701720882; cv=none; d=google.com; s=arc-20160816; b=YIBTYQq8yTnUbR0zAHoM73mYStoW+nHsHIBy/YD6iAZlN1XnmhFbqhVQrCqP901YCg jxZnN0moAxE0YZXtKmvtnKk+4v+QxVhGux0UZ2BK5TLO5nRXiMSTn/zu8YZAFxrkdDpG hnbjcf0kjKvKf2DwcPR0+f+iQpsxDb1F+nURSMWJX6llEQyDZ4hhOQ6aHXHDjGLJ3XDc /YKYY0dAo0/uEYWsove5nfOdCgip9g+eRk+Ss11PEzBCSq+xqoTTu3Jxiojma2URpJIQ HwUyjLS145wD5Luf85Y8jw6CxgTjIDDqucq6/mrvL61wi9ka5L9bv/SoyJpOMa1zvOVj Oqcg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=q/gPyzX5houMx56rDPPZ+T9jmCn/l6+TF2FXxv5O3eY=; fh=hbFFxISGzJlcFBKVChpRkjqflH8vGcvS/i23umxk4/Q=; b=CzsOPqllzxXUVXw0MPKrTEo04EuvOUDgovClSFwthyHWHuY9BzyLg/PNSv6vzyGuZ5 O8t4zOGJ3nvtvdRb2BG7a8Nqgs5YkDGomJFaRXaiaPHYcoVr6GkzzXYOkV0LxEwFWoqX eemGGdZxae37NEC2hV6pQoRSRSZSXIS/oCw65i0dYIp8s7Ztxvwhf3v3sONUuqQOtDwe hWdbGryjZjwHPLRRZELyJWQKXgpk/GXakUQdaNR+ScCH3eGaFGY7X91fE4/iKCP+vrBo XDxVrWvzsw15cIWiUgCPxTcBy28SsI5tjAXGpcE58I3yd2P6UtNkcqVsZ36RVfsLceIG 0fag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=ebUgtJn2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id bz17-20020a056a02061100b005c699b49d9bsi1137136pgb.847.2023.12.04.12.14.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 12:14:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=ebUgtJn2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 0AE71804C224; Mon, 4 Dec 2023 12:14:42 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234795AbjLDUO3 (ORCPT <rfc822;chrisfriedt@gmail.com> + 99 others); Mon, 4 Dec 2023 15:14:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234526AbjLDUO1 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 4 Dec 2023 15:14:27 -0500 Received: from mail-oo1-xc2f.google.com (mail-oo1-xc2f.google.com [IPv6:2607:f8b0:4864:20::c2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10D65183 for <linux-kernel@vger.kernel.org>; Mon, 4 Dec 2023 12:14:33 -0800 (PST) Received: by mail-oo1-xc2f.google.com with SMTP id 006d021491bc7-58a7d13b00bso3339040eaf.1 for <linux-kernel@vger.kernel.org>; Mon, 04 Dec 2023 12:14:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1701720872; x=1702325672; 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=q/gPyzX5houMx56rDPPZ+T9jmCn/l6+TF2FXxv5O3eY=; b=ebUgtJn2mw/Y9s+WswbHYIcizhkZA/nFFfxXu7g+x1/eEW3OFqvN5/MPebGMDyivN6 etV0F0c8jcMPMmME2inouU9l/X8+FBHykSMmDvlBA4fhJdlpNHUt7MOMqLwmii2Cwg28 co5HZJ29s8tvqSrp4lGresPxowtehWSV6yXhWWdGXkzrgcWsxsO6xFROBnnWLtr5TvlX 3HbhfbyQhqk6EUbYmexIydEKM2Oi0YlVuqz/7Hslx6PW58wnSDcOdSkGEbBebl/3EhLa ALJc+RHWRbtnRUsquuJW4NfUZ1A7lx2loFZ7lnv7AdjDOCzqX2ZCLer0gDr7pyp7jkXn m2WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701720872; x=1702325672; 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=q/gPyzX5houMx56rDPPZ+T9jmCn/l6+TF2FXxv5O3eY=; b=tl6XIlC4Nu01dEgHGYViMop8H2KGQUhuLfeiF9EJH4AJxhq37kZ9MAsNU+KjBbq98O m4hYPsPuVing8tG50BCty+j7KJGxg21d6E27PiwMMzbsGoI+9kGjmYzgnKHheJjfy0GU Vz2arPnRvHzNBj55QT+/mkMwfZQ8H7o4424e7Zj0AlsETScfxtgsoS0D9nNXK9Q73SZq dV/OEqb1VO10YkS1JW2s7c63Gg2gQFUUSX8cZbP9tRaIS7sUaMCYk19/XKLvB6WM9TNH ZeaBy2KN9e9mQBOwDAjOAhwTIxGOMZxmwhoirNDumNvKLStHHBuucSbX0usErqYGcox9 K0AA== X-Gm-Message-State: AOJu0Yy0Eu4H6utJqR6YKofGyXxH1qTGS58DV8Cy2lD2cfe4oss3XHOb p86nSDrdaeEBRdN7dpK82cC5XQ== X-Received: by 2002:a05:6359:5c2a:b0:170:17eb:377e with SMTP id pu42-20020a0563595c2a00b0017017eb377emr4548494rwb.38.1701720871846; Mon, 04 Dec 2023 12:14:31 -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 n7-20020a63f807000000b005b529d633b7sm7894060pgh.14.2023.12.04.12.14.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 12:14:31 -0800 (PST) From: Kyle Huey <me@kylehuey.com> X-Google-Original-From: Kyle Huey <khuey@kylehuey.com> To: Kyle Huey <khuey@kylehuey.com>, linux-kernel@vger.kernel.org Cc: Robert O'Callahan <robert@ocallahan.org>, Andrii Nakryiko <andrii@kernel.org>, Mykola Lysenko <mykolal@fb.com>, Alexei Starovoitov <ast@kernel.org>, Daniel Borkmann <daniel@iogearbox.net>, Martin KaFai Lau <martin.lau@linux.dev>, Song Liu <song@kernel.org>, Yonghong Song <yonghong.song@linux.dev>, John Fastabend <john.fastabend@gmail.com>, KP Singh <kpsingh@kernel.org>, Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>, Jiri Olsa <jolsa@kernel.org>, Shuah Khan <shuah@kernel.org>, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH 2/2] selftest/bpf: Test returning zero from a perf bpf program suppresses SIGIO. Date: Mon, 4 Dec 2023 12:14:06 -0800 Message-Id: <20231204201406.341074-3-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231204201406.341074-1-khuey@kylehuey.com> References: <20231204201406.341074-1-khuey@kylehuey.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 04 Dec 2023 12:14:42 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784383676544754775 X-GMAIL-MSGID: 1784383676544754775 |
Series |
Combine perf and bpf for fast eval of hw breakpoint conditions
|
|
Commit Message
Kyle Huey
Dec. 4, 2023, 8:14 p.m. UTC
The test sets a hardware breakpoint and uses a bpf program to suppress the
I/O availability signal if the ip matches the expected value.
Signed-off-by: Kyle Huey <khuey@kylehuey.com>
---
.../selftests/bpf/prog_tests/perf_skip.c | 95 +++++++++++++++++++
.../selftests/bpf/progs/test_perf_skip.c | 23 +++++
2 files changed, 118 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
Comments
On Mon, Dec 4, 2023 at 12:14 PM Kyle Huey <me@kylehuey.com> wrote: > > The test sets a hardware breakpoint and uses a bpf program to suppress the > I/O availability signal if the ip matches the expected value. > > Signed-off-by: Kyle Huey <khuey@kylehuey.com> > --- > .../selftests/bpf/prog_tests/perf_skip.c | 95 +++++++++++++++++++ > .../selftests/bpf/progs/test_perf_skip.c | 23 +++++ > 2 files changed, 118 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..b269a31669b7 > --- /dev/null > +++ b/tools/testing/selftests/bpf/prog_tests/perf_skip.c > @@ -0,0 +1,95 @@ > +// SPDX-License-Identifier: GPL-2.0 > +#define _GNU_SOURCE > +#include <test_progs.h> > +#include "test_perf_skip.skel.h" > +#include <linux/hw_breakpoint.h> > +#include <sys/mman.h> > + > +#define BPF_OBJECT "test_perf_skip.bpf.o" leftover? > + > +static void handle_sig(int) > +{ > + ASSERT_OK(1, "perf event not skipped"); > +} > + > +static noinline int test_function(void) > +{ please add asm volatile (""); here to prevent compiler from actually inlining at the call site > + return 0; > +} > + > +void serial_test_perf_skip(void) > +{ > + sighandler_t previous; > + int duration = 0; > + struct test_perf_skip *skel = NULL; > + int map_fd = -1; > + long page_size = sysconf(_SC_PAGE_SIZE); > + uintptr_t *ip = NULL; > + int prog_fd = -1; > + struct perf_event_attr attr = {0}; > + int perf_fd = -1; > + struct f_owner_ex owner; > + int err; > + > + previous = signal(SIGIO, handle_sig); > + > + skel = test_perf_skip__open_and_load(); > + if (!ASSERT_OK_PTR(skel, "skel_load")) > + goto cleanup; > + > + prog_fd = bpf_program__fd(skel->progs.handler); > + if (!ASSERT_OK(prog_fd < 0, "bpf_program__fd")) > + goto cleanup; > + > + map_fd = bpf_map__fd(skel->maps.ip); > + if (!ASSERT_OK(map_fd < 0, "bpf_map__fd")) > + goto cleanup; > + > + ip = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0); > + if (!ASSERT_OK_PTR(ip, "mmap bpf map")) > + goto cleanup; > + > + *ip = (uintptr_t)test_function; > + > + 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; > + > + perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0); > + if (CHECK(perf_fd < 0, "perf_event_open", "err %d\n", perf_fd)) please don't use CHECK() macro, stick to ASSERT_xxx() also, we are going to run all this on different hardware and VMs, see how we skip tests if hardware support is not there. See test__skip usage in prog_tests/perf_branches.c, as one example > + goto cleanup; > + > + err = fcntl(perf_fd, F_SETFL, O_ASYNC); I assume this is what will send SIGIO, right? Can you add a small comment explicitly saying this? > + if (!ASSERT_OK(err, "fcntl(F_SETFL, O_ASYNC)")) > + goto cleanup; > + > + owner.type = F_OWNER_TID; > + owner.pid = gettid(); > + err = fcntl(perf_fd, F_SETOWN_EX, &owner); > + if (!ASSERT_OK(err, "fcntl(F_SETOWN_EX)")) > + goto cleanup; > + > + err = ioctl(perf_fd, PERF_EVENT_IOC_SET_BPF, prog_fd); > + if (!ASSERT_OK(err, "ioctl(PERF_EVENT_IOC_SET_BPF)")) > + goto cleanup; we have a better way to do this, please use bpf_program__attach_perf_event() instead > + > + test_function(); > + > +cleanup: > + if (perf_fd >= 0) > + close(perf_fd); > + if (ip) > + munmap(ip, page_size); > + if (skel) > + test_perf_skip__destroy(skel); > + > + signal(SIGIO, previous); > +} > 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..ef01a9161afe > --- /dev/null > +++ b/tools/testing/selftests/bpf/progs/test_perf_skip.c > @@ -0,0 +1,23 @@ > +// SPDX-License-Identifier: GPL-2.0 > +#include "vmlinux.h" > +#include <bpf/bpf_helpers.h> > +#include <bpf/bpf_tracing.h> > + > +struct { > + __uint(type, BPF_MAP_TYPE_ARRAY); > + __uint(max_entries, 1); > + __uint(map_flags, BPF_F_MMAPABLE); > + __type(key, uint32_t); > + __type(value, uintptr_t); > +} ip SEC(".maps"); please use global variable: __u64 ip; and then access it from user-space side through skeleton skel->bss.ip = &test_function; > + > +SEC("perf_event") > +int handler(struct bpf_perf_event_data *data) > +{ > + const uint32_t index = 0; > + uintptr_t *v = bpf_map_lookup_elem(&ip, &index); > + > + return !(v && *v == PT_REGS_IP(&data->regs)); and so we the above global var suggestion this will be just: return ip == PT_REGS_IP(&data->regs); > +} > + > +char _license[] SEC("license") = "GPL"; > -- > 2.34.1 >
On Mon, Dec 04, 2023 at 12:14:06PM -0800, Kyle Huey wrote: > The test sets a hardware breakpoint and uses a bpf program to suppress the > I/O availability signal if the ip matches the expected value. > > Signed-off-by: Kyle Huey <khuey@kylehuey.com> > --- > .../selftests/bpf/prog_tests/perf_skip.c | 95 +++++++++++++++++++ > .../selftests/bpf/progs/test_perf_skip.c | 23 +++++ > 2 files changed, 118 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..b269a31669b7 > --- /dev/null > +++ b/tools/testing/selftests/bpf/prog_tests/perf_skip.c > @@ -0,0 +1,95 @@ > +// SPDX-License-Identifier: GPL-2.0 > +#define _GNU_SOURCE > +#include <test_progs.h> > +#include "test_perf_skip.skel.h" > +#include <linux/hw_breakpoint.h> > +#include <sys/mman.h> > + > +#define BPF_OBJECT "test_perf_skip.bpf.o" > + > +static void handle_sig(int) > +{ > + ASSERT_OK(1, "perf event not skipped"); > +} > + > +static noinline int test_function(void) > +{ > + return 0; > +} > + > +void serial_test_perf_skip(void) does it need to be serial? jirka
On 12/4/23 3:14 PM, Kyle Huey wrote: > The test sets a hardware breakpoint and uses a bpf program to suppress the > I/O availability signal if the ip matches the expected value. > > Signed-off-by: Kyle Huey <khuey@kylehuey.com> > --- > .../selftests/bpf/prog_tests/perf_skip.c | 95 +++++++++++++++++++ > .../selftests/bpf/progs/test_perf_skip.c | 23 +++++ > 2 files changed, 118 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..b269a31669b7 > --- /dev/null > +++ b/tools/testing/selftests/bpf/prog_tests/perf_skip.c > @@ -0,0 +1,95 @@ > +// SPDX-License-Identifier: GPL-2.0 > +#define _GNU_SOURCE > +#include <test_progs.h> > +#include "test_perf_skip.skel.h" > +#include <linux/hw_breakpoint.h> > +#include <sys/mman.h> > + > +#define BPF_OBJECT "test_perf_skip.bpf.o" > + > +static void handle_sig(int) I hit a warning here: home/yhs/work/bpf-next/tools/testing/selftests/bpf/prog_tests/perf_skip.c:10:27: error: omitting the parameter name in a function definition is a C23 extension [-Werror,-Wc23-extensions] 10 | static void handle_sig(int) | Add a parameter and marked as unused can resolve the issue. #define __always_unused __attribute__((__unused__)) static void handle_sig(int unused __always_unused) { ASSERT_OK(1, "perf event not skipped"); } > +{ > + ASSERT_OK(1, "perf event not skipped"); > +} > + > +static noinline int test_function(void) > +{ > + return 0; > +} > + > +void serial_test_perf_skip(void) > +{ > + sighandler_t previous; > + int duration = 0; > + struct test_perf_skip *skel = NULL; > + int map_fd = -1; > + long page_size = sysconf(_SC_PAGE_SIZE); > + uintptr_t *ip = NULL; > + int prog_fd = -1; > + struct perf_event_attr attr = {0}; > + int perf_fd = -1; > + struct f_owner_ex owner; > + int err; > + > + previous = signal(SIGIO, handle_sig); > + > + skel = test_perf_skip__open_and_load(); > + if (!ASSERT_OK_PTR(skel, "skel_load")) > + goto cleanup; > + > + prog_fd = bpf_program__fd(skel->progs.handler); > + if (!ASSERT_OK(prog_fd < 0, "bpf_program__fd")) > + goto cleanup; > + > + map_fd = bpf_map__fd(skel->maps.ip); > + if (!ASSERT_OK(map_fd < 0, "bpf_map__fd")) > + goto cleanup; > + > + ip = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0); > + if (!ASSERT_OK_PTR(ip, "mmap bpf map")) > + goto cleanup; > + > + *ip = (uintptr_t)test_function; > + > + 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; > + > + perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0); > + if (CHECK(perf_fd < 0, "perf_event_open", "err %d\n", perf_fd)) > + goto cleanup; > + > + 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 = gettid(); I hit a compilation failure here: /home/yhs/work/bpf-next/tools/testing/selftests/bpf/prog_tests/perf_skip.c:75:14: error: call to undeclared function 'gettid'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] 75 | owner.pid = gettid(); | ^ If you looked at some other examples, the common usage is do 'syscall(SYS_gettid)'. So the following patch should fix the compilation error: #include <sys/syscall.h> ... owner.pid = syscall(SYS_gettid); ... > + err = fcntl(perf_fd, F_SETOWN_EX, &owner); > + if (!ASSERT_OK(err, "fcntl(F_SETOWN_EX)")) > + goto cleanup; > + > + err = ioctl(perf_fd, PERF_EVENT_IOC_SET_BPF, prog_fd); > + if (!ASSERT_OK(err, "ioctl(PERF_EVENT_IOC_SET_BPF)")) > + goto cleanup; > + > + test_function(); As Andrii has mentioned in previous comments, we will have issue is RELEASE version of selftest is built RELEASE=1 make ... See https://lore.kernel.org/bpf/20231127050342.1945270-1-yonghong.song@linux.dev > + > +cleanup: > + if (perf_fd >= 0) > + close(perf_fd); > + if (ip) > + munmap(ip, page_size); > + if (skel) > + test_perf_skip__destroy(skel); > + > + signal(SIGIO, previous); > +} > 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..ef01a9161afe > --- /dev/null > +++ b/tools/testing/selftests/bpf/progs/test_perf_skip.c > @@ -0,0 +1,23 @@ > +// SPDX-License-Identifier: GPL-2.0 > +#include "vmlinux.h" > +#include <bpf/bpf_helpers.h> > +#include <bpf/bpf_tracing.h> > + > +struct { > + __uint(type, BPF_MAP_TYPE_ARRAY); > + __uint(max_entries, 1); > + __uint(map_flags, BPF_F_MMAPABLE); > + __type(key, uint32_t); > + __type(value, uintptr_t); > +} ip SEC(".maps"); > + > +SEC("perf_event") > +int handler(struct bpf_perf_event_data *data) > +{ > + const uint32_t index = 0; > + uintptr_t *v = bpf_map_lookup_elem(&ip, &index); > + > + return !(v && *v == PT_REGS_IP(&data->regs)); > +} > + > +char _license[] SEC("license") = "GPL";
On Tue, Dec 5, 2023 at 8:54 AM Yonghong Song <yonghong.song@linux.dev> wrote: > > > On 12/4/23 3:14 PM, Kyle Huey wrote: > > The test sets a hardware breakpoint and uses a bpf program to suppress the > > I/O availability signal if the ip matches the expected value. > > > > Signed-off-by: Kyle Huey <khuey@kylehuey.com> > > --- > > .../selftests/bpf/prog_tests/perf_skip.c | 95 +++++++++++++++++++ > > .../selftests/bpf/progs/test_perf_skip.c | 23 +++++ > > 2 files changed, 118 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..b269a31669b7 > > --- /dev/null > > +++ b/tools/testing/selftests/bpf/prog_tests/perf_skip.c > > @@ -0,0 +1,95 @@ > > +// SPDX-License-Identifier: GPL-2.0 > > +#define _GNU_SOURCE > > +#include <test_progs.h> > > +#include "test_perf_skip.skel.h" > > +#include <linux/hw_breakpoint.h> > > +#include <sys/mman.h> > > + > > +#define BPF_OBJECT "test_perf_skip.bpf.o" > > + > > +static void handle_sig(int) > > I hit a warning here: > home/yhs/work/bpf-next/tools/testing/selftests/bpf/prog_tests/perf_skip.c:10:27: error: omitting the parameter name in a function definition is a C23 extension [-Werror,-Wc23-extensions] Yeah, Meta's kernel-ci bot sent me off-list email about this one. > > 10 | static void handle_sig(int) > | > > Add a parameter and marked as unused can resolve the issue. > > #define __always_unused __attribute__((__unused__)) > > static void handle_sig(int unused __always_unused) > { > ASSERT_OK(1, "perf event not skipped"); > } > > > > +{ > > + ASSERT_OK(1, "perf event not skipped"); > > +} > > + > > +static noinline int test_function(void) > > +{ > > + return 0; > > +} > > + > > +void serial_test_perf_skip(void) > > +{ > > + sighandler_t previous; > > + int duration = 0; > > + struct test_perf_skip *skel = NULL; > > + int map_fd = -1; > > + long page_size = sysconf(_SC_PAGE_SIZE); > > + uintptr_t *ip = NULL; > > + int prog_fd = -1; > > + struct perf_event_attr attr = {0}; > > + int perf_fd = -1; > > + struct f_owner_ex owner; > > + int err; > > + > > + previous = signal(SIGIO, handle_sig); > > + > > + skel = test_perf_skip__open_and_load(); > > + if (!ASSERT_OK_PTR(skel, "skel_load")) > > + goto cleanup; > > + > > + prog_fd = bpf_program__fd(skel->progs.handler); > > + if (!ASSERT_OK(prog_fd < 0, "bpf_program__fd")) > > + goto cleanup; > > + > > + map_fd = bpf_map__fd(skel->maps.ip); > > + if (!ASSERT_OK(map_fd < 0, "bpf_map__fd")) > > + goto cleanup; > > + > > + ip = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0); > > + if (!ASSERT_OK_PTR(ip, "mmap bpf map")) > > + goto cleanup; > > + > > + *ip = (uintptr_t)test_function; > > + > > + 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; > > + > > + perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0); > > + if (CHECK(perf_fd < 0, "perf_event_open", "err %d\n", perf_fd)) > > + goto cleanup; > > + > > + 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 = gettid(); > > I hit a compilation failure here: > > /home/yhs/work/bpf-next/tools/testing/selftests/bpf/prog_tests/perf_skip.c:75:14: error: call to undeclared function 'gettid'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] > 75 | owner.pid = gettid(); > | ^ > > If you looked at some other examples, the common usage is do 'syscall(SYS_gettid)'. Not clear why this works for me but sure I'll change that. > > So the following patch should fix the compilation error: > > #include <sys/syscall.h> > ... > owner.pid = syscall(SYS_gettid); > ... > > > + err = fcntl(perf_fd, F_SETOWN_EX, &owner); > > + if (!ASSERT_OK(err, "fcntl(F_SETOWN_EX)")) > > + goto cleanup; > > + > > + err = ioctl(perf_fd, PERF_EVENT_IOC_SET_BPF, prog_fd); > > + if (!ASSERT_OK(err, "ioctl(PERF_EVENT_IOC_SET_BPF)")) > > + goto cleanup; > > + > > + test_function(); > > As Andrii has mentioned in previous comments, we will have > issue is RELEASE version of selftest is built > RELEASE=1 make ... > > See https://lore.kernel.org/bpf/20231127050342.1945270-1-yonghong.song@linux.dev Not sure I follow this one. Are you saying adding asm volatile ("") in test_function() is *not* sufficient? - Kyle > > > + > > +cleanup: > > + if (perf_fd >= 0) > > + close(perf_fd); > > + if (ip) > > + munmap(ip, page_size); > > + if (skel) > > + test_perf_skip__destroy(skel); > > + > > + signal(SIGIO, previous); > > +} > > 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..ef01a9161afe > > --- /dev/null > > +++ b/tools/testing/selftests/bpf/progs/test_perf_skip.c > > @@ -0,0 +1,23 @@ > > +// SPDX-License-Identifier: GPL-2.0 > > +#include "vmlinux.h" > > +#include <bpf/bpf_helpers.h> > > +#include <bpf/bpf_tracing.h> > > + > > +struct { > > + __uint(type, BPF_MAP_TYPE_ARRAY); > > + __uint(max_entries, 1); > > + __uint(map_flags, BPF_F_MMAPABLE); > > + __type(key, uint32_t); > > + __type(value, uintptr_t); > > +} ip SEC(".maps"); > > + > > +SEC("perf_event") > > +int handler(struct bpf_perf_event_data *data) > > +{ > > + const uint32_t index = 0; > > + uintptr_t *v = bpf_map_lookup_elem(&ip, &index); > > + > > + return !(v && *v == PT_REGS_IP(&data->regs)); > > +} > > + > > +char _license[] SEC("license") = "GPL";
On Mon, Dec 4, 2023 at 2:14 PM Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote: > > On Mon, Dec 4, 2023 at 12:14 PM Kyle Huey <me@kylehuey.com> wrote: > > > > The test sets a hardware breakpoint and uses a bpf program to suppress the > > I/O availability signal if the ip matches the expected value. > > > > Signed-off-by: Kyle Huey <khuey@kylehuey.com> > > --- > > .../selftests/bpf/prog_tests/perf_skip.c | 95 +++++++++++++++++++ > > .../selftests/bpf/progs/test_perf_skip.c | 23 +++++ > > 2 files changed, 118 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..b269a31669b7 > > --- /dev/null > > +++ b/tools/testing/selftests/bpf/prog_tests/perf_skip.c > > @@ -0,0 +1,95 @@ > > +// SPDX-License-Identifier: GPL-2.0 > > +#define _GNU_SOURCE > > +#include <test_progs.h> > > +#include "test_perf_skip.skel.h" > > +#include <linux/hw_breakpoint.h> > > +#include <sys/mman.h> > > + > > +#define BPF_OBJECT "test_perf_skip.bpf.o" > > leftover? Indeed. Fixed. > > + > > +static void handle_sig(int) > > +{ > > + ASSERT_OK(1, "perf event not skipped"); > > +} > > + > > +static noinline int test_function(void) > > +{ > > please add > > asm volatile (""); > > here to prevent compiler from actually inlining at the call site Ok. > > + return 0; > > +} > > + > > +void serial_test_perf_skip(void) > > +{ > > + sighandler_t previous; > > + int duration = 0; > > + struct test_perf_skip *skel = NULL; > > + int map_fd = -1; > > + long page_size = sysconf(_SC_PAGE_SIZE); > > + uintptr_t *ip = NULL; > > + int prog_fd = -1; > > + struct perf_event_attr attr = {0}; > > + int perf_fd = -1; > > + struct f_owner_ex owner; > > + int err; > > + > > + previous = signal(SIGIO, handle_sig); > > + > > + skel = test_perf_skip__open_and_load(); > > + if (!ASSERT_OK_PTR(skel, "skel_load")) > > + goto cleanup; > > + > > + prog_fd = bpf_program__fd(skel->progs.handler); > > + if (!ASSERT_OK(prog_fd < 0, "bpf_program__fd")) > > + goto cleanup; > > + > > + map_fd = bpf_map__fd(skel->maps.ip); > > + if (!ASSERT_OK(map_fd < 0, "bpf_map__fd")) > > + goto cleanup; > > + > > + ip = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0); > > + if (!ASSERT_OK_PTR(ip, "mmap bpf map")) > > + goto cleanup; > > + > > + *ip = (uintptr_t)test_function; > > + > > + 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; > > + > > + perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0); > > + if (CHECK(perf_fd < 0, "perf_event_open", "err %d\n", perf_fd)) > > please don't use CHECK() macro, stick to ASSERT_xxx() Done. > also, we are going to run all this on different hardware and VMs, see > how we skip tests if hardware support is not there. See test__skip > usage in prog_tests/perf_branches.c, as one example Hmm I suppose it should be conditioned on CONFIG_HAVE_HW_BREAKPOINT. > > + goto cleanup; > > + > > + err = fcntl(perf_fd, F_SETFL, O_ASYNC); > > I assume this is what will send SIGIO, right? Can you add a small > comment explicitly saying this? Done. > > + if (!ASSERT_OK(err, "fcntl(F_SETFL, O_ASYNC)")) > > + goto cleanup; > > + > > + owner.type = F_OWNER_TID; > > + owner.pid = gettid(); > > + err = fcntl(perf_fd, F_SETOWN_EX, &owner); > > + if (!ASSERT_OK(err, "fcntl(F_SETOWN_EX)")) > > + goto cleanup; > > + > > + err = ioctl(perf_fd, PERF_EVENT_IOC_SET_BPF, prog_fd); > > + if (!ASSERT_OK(err, "ioctl(PERF_EVENT_IOC_SET_BPF)")) > > + goto cleanup; > > we have a better way to do this, please use > bpf_program__attach_perf_event() instead Done. > > + > > + test_function(); > > + > > +cleanup: > > + if (perf_fd >= 0) > > + close(perf_fd); > > + if (ip) > > + munmap(ip, page_size); > > + if (skel) > > + test_perf_skip__destroy(skel); > > + > > + signal(SIGIO, previous); > > +} > > 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..ef01a9161afe > > --- /dev/null > > +++ b/tools/testing/selftests/bpf/progs/test_perf_skip.c > > @@ -0,0 +1,23 @@ > > +// SPDX-License-Identifier: GPL-2.0 > > +#include "vmlinux.h" > > +#include <bpf/bpf_helpers.h> > > +#include <bpf/bpf_tracing.h> > > + > > +struct { > > + __uint(type, BPF_MAP_TYPE_ARRAY); > > + __uint(max_entries, 1); > > + __uint(map_flags, BPF_F_MMAPABLE); > > + __type(key, uint32_t); > > + __type(value, uintptr_t); > > +} ip SEC(".maps"); > > please use global variable: > > __u64 ip; > > and then access it from user-space side through skeleton > > skel->bss.ip = &test_function; Done. > > + > > +SEC("perf_event") > > +int handler(struct bpf_perf_event_data *data) > > +{ > > + const uint32_t index = 0; > > + uintptr_t *v = bpf_map_lookup_elem(&ip, &index); > > + > > + return !(v && *v == PT_REGS_IP(&data->regs)); > > and so we the above global var suggestion this will be just: > > return ip == PT_REGS_IP(&data->regs); > > > +} > > + > > +char _license[] SEC("license") = "GPL"; > > -- > > 2.34.1 > > - Kyle
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..b269a31669b7 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/perf_skip.c @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include <test_progs.h> +#include "test_perf_skip.skel.h" +#include <linux/hw_breakpoint.h> +#include <sys/mman.h> + +#define BPF_OBJECT "test_perf_skip.bpf.o" + +static void handle_sig(int) +{ + ASSERT_OK(1, "perf event not skipped"); +} + +static noinline int test_function(void) +{ + return 0; +} + +void serial_test_perf_skip(void) +{ + sighandler_t previous; + int duration = 0; + struct test_perf_skip *skel = NULL; + int map_fd = -1; + long page_size = sysconf(_SC_PAGE_SIZE); + uintptr_t *ip = NULL; + int prog_fd = -1; + struct perf_event_attr attr = {0}; + int perf_fd = -1; + struct f_owner_ex owner; + int err; + + previous = signal(SIGIO, handle_sig); + + skel = test_perf_skip__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_load")) + goto cleanup; + + prog_fd = bpf_program__fd(skel->progs.handler); + if (!ASSERT_OK(prog_fd < 0, "bpf_program__fd")) + goto cleanup; + + map_fd = bpf_map__fd(skel->maps.ip); + if (!ASSERT_OK(map_fd < 0, "bpf_map__fd")) + goto cleanup; + + ip = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0); + if (!ASSERT_OK_PTR(ip, "mmap bpf map")) + goto cleanup; + + *ip = (uintptr_t)test_function; + + 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; + + perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0); + if (CHECK(perf_fd < 0, "perf_event_open", "err %d\n", perf_fd)) + goto cleanup; + + 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 = gettid(); + err = fcntl(perf_fd, F_SETOWN_EX, &owner); + if (!ASSERT_OK(err, "fcntl(F_SETOWN_EX)")) + goto cleanup; + + err = ioctl(perf_fd, PERF_EVENT_IOC_SET_BPF, prog_fd); + if (!ASSERT_OK(err, "ioctl(PERF_EVENT_IOC_SET_BPF)")) + goto cleanup; + + test_function(); + +cleanup: + if (perf_fd >= 0) + close(perf_fd); + if (ip) + munmap(ip, page_size); + if (skel) + test_perf_skip__destroy(skel); + + signal(SIGIO, previous); +} 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..ef01a9161afe --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_perf_skip.c @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __uint(map_flags, BPF_F_MMAPABLE); + __type(key, uint32_t); + __type(value, uintptr_t); +} ip SEC(".maps"); + +SEC("perf_event") +int handler(struct bpf_perf_event_data *data) +{ + const uint32_t index = 0; + uintptr_t *v = bpf_map_lookup_elem(&ip, &index); + + return !(v && *v == PT_REGS_IP(&data->regs)); +} + +char _license[] SEC("license") = "GPL";