Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp477433rdb; Tue, 5 Dec 2023 10:22:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IF6YyLc/qw/qAxD93yrqlpY166BsvOmnuH1LiOSu1AjwMwDS//1nX1A9C8jYw6H2UfSG71n X-Received: by 2002:a17:902:ab45:b0:1cf:f353:6e47 with SMTP id ij5-20020a170902ab4500b001cff3536e47mr5796500plb.20.1701800531693; Tue, 05 Dec 2023 10:22:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701800531; cv=none; d=google.com; s=arc-20160816; b=mIXmlMzO+j+jUUZJdWQMWxOqdtzeqh+EGV4mB7/lctRddlAPk+Y29FYAR5ph01xGD5 4m7BZP4g2K9VH4E/49OhvAKlCexHRBGVJi7XkhHWcpfFFSEkAbyU8KUF5XQzoNv/k9Q/ dwM1ifgOHRex3yiBKizg+etFHg1b02RWuf35thualMTS0malX1YXhdUgOSUs18UnSkzE dXLOttYI6XB3QlBQREJhXd+VmC5cWfgSLIOlHoqJgBGtEuoYcPsCF02T/GbG0WfbrhND 4swDYX3amtvYar0ShfxZFEeWYqJhGbS879hKcuUAqu8u2fpjKuOGwmoyecs2Z46d6wF4 6jDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=T+nkDPugKKNNkRBSOn2CVgjn9FjQf79ux7IfHoEDnOg=; fh=mMjUN+JcJSXT+4s9bhhk/TgCwVBRkrlo8UyAXm6QT6k=; b=MKC7niiBPmcBgg2GIQFSRMoyLu8YrJ/RunHoxSMCSD8sgVPJLRNDwAszVGQSHxU/qb qMEVnZhQ8Kdzd48asFYEIslKLt0MXy+nojh7tW9RqW9dfX0pR7eGH1luWCY1tFK/e1Kn UjoJDvx5dpkv4JxdWaDshCdQkaTP6NZEGj135H+Z3LnORWlSIsRDO1goz97EaurVEY8o nKqu0Doiv9g/lAhI+PThfyD6alu7mLKqjy3TZoPuf/sLXsuI+gHJQ3L+jpaxwkf90va2 6hc1QW9a2TVjzcDHbiiOKEwzQknaXWzZeItWW18uE2Y2b6MPsGo4qhYwozNcPlk8sSOA y5dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=W32wTH4h; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id a14-20020a170902710e00b001c07bac13d0si3989662pll.383.2023.12.05.10.22.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 10:22:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=W32wTH4h; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 57EB28030B51; Tue, 5 Dec 2023 10:22:06 -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 S1345930AbjLESVu (ORCPT + 99 others); Tue, 5 Dec 2023 13:21:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346698AbjLESVd (ORCPT ); Tue, 5 Dec 2023 13:21:33 -0500 Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF6C0137 for ; Tue, 5 Dec 2023 10:21:38 -0800 (PST) Received: by mail-ej1-x629.google.com with SMTP id a640c23a62f3a-a186e5d1056so762594566b.0 for ; Tue, 05 Dec 2023 10:21:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1701800497; x=1702405297; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=T+nkDPugKKNNkRBSOn2CVgjn9FjQf79ux7IfHoEDnOg=; b=W32wTH4h5PuyCsKJT+O5qeimqf9gUO1Bi9ihc/ahqaX6CWx/Um5+clPktn1WLKCSKd GEkmI8TkBaVSIerOmjdFO15b+QoGblnZelT6abp218oD7+W63lXYZnsjFmksee8Ob3sR XDpO3G+ttuAFW0Oq0IjW/BsePkoQZYVRPJw/0RTykBaLtzDrSnOLhtbWW+eOJDyeMNaM ic7f4tpPurSLzGUvvHHwE5m1w3irhGiuVjSOY1FQpwuAvaV6r+vbzI8mRSTov1EJ1kw/ x16swUkx+HSiU2Z83/vF3CKKeJCJg4Cl/WofC9T3EeZspHJ6qBbm6JM0/Ot8H0wY/CLI 5N5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701800497; x=1702405297; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=T+nkDPugKKNNkRBSOn2CVgjn9FjQf79ux7IfHoEDnOg=; b=WPcNMT/DfZK3nLZYJLD864Yw4zYZD+cY9FSE5egE+9tn1FUmPxrQVXeH1b2vqh4UCK T5RoVXF9Wfcl1uy8dYgJeYq2/wwy0/QaIzA/Ru0FRQGL3/WzpT37pHCdG9QbsOJ3qii4 VTxgPH5aLULgP+tARFRikUmnEKY9UE8nqjm9rrWUQNKzUytms0Xnd2u+TATTC60rvSvn LLTGFis+Yp9H7dXVqiLLijU6wmpjmVvhvGF56jph1W70Sek+tIibwRX04fPR4aXZzN43 c+FjzPaFcJ+1DtQ3nI+jUAI514WqaPOiX6zkBwsgPPRp/AIOV+Wd4YLfyARzJoqOX/D6 y1hw== X-Gm-Message-State: AOJu0Yx73Ngo3zBTGcmDCnCHhXHvTkNO/9hVZrljohH96+BLQuJAwXQx luQjUFpg6mmoamS5GVWjymjjUkK5f3gQn0TLToiE5g== X-Received: by 2002:a17:906:2241:b0:a19:a19b:5605 with SMTP id 1-20020a170906224100b00a19a19b5605mr892974ejr.149.1701800497041; Tue, 05 Dec 2023 10:21:37 -0800 (PST) MIME-Version: 1.0 References: <20231204201406.341074-1-khuey@kylehuey.com> <20231204201406.341074-3-khuey@kylehuey.com> In-Reply-To: From: Kyle Huey Date: Tue, 5 Dec 2023 10:21:25 -0800 Message-ID: Subject: Re: [PATCH 2/2] selftest/bpf: Test returning zero from a perf bpf program suppresses SIGIO. To: Andrii Nakryiko Cc: Kyle Huey , linux-kernel@vger.kernel.org, "Robert O'Callahan" , Andrii Nakryiko , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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_NONE, 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: 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]); Tue, 05 Dec 2023 10:22:06 -0800 (PST) On Mon, Dec 4, 2023 at 2:14=E2=80=AFPM Andrii Nakryiko wrote: > > On Mon, Dec 4, 2023 at 12:14=E2=80=AFPM 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 > > --- > > .../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 > > +#include "test_perf_skip.skel.h" > > +#include > > +#include > > + > > +#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 =3D 0; > > + struct test_perf_skip *skel =3D NULL; > > + int map_fd =3D -1; > > + long page_size =3D sysconf(_SC_PAGE_SIZE); > > + uintptr_t *ip =3D NULL; > > + int prog_fd =3D -1; > > + struct perf_event_attr attr =3D {0}; > > + int perf_fd =3D -1; > > + struct f_owner_ex owner; > > + int err; > > + > > + previous =3D signal(SIGIO, handle_sig); > > + > > + skel =3D test_perf_skip__open_and_load(); > > + if (!ASSERT_OK_PTR(skel, "skel_load")) > > + goto cleanup; > > + > > + prog_fd =3D bpf_program__fd(skel->progs.handler); > > + if (!ASSERT_OK(prog_fd < 0, "bpf_program__fd")) > > + goto cleanup; > > + > > + map_fd =3D bpf_map__fd(skel->maps.ip); > > + if (!ASSERT_OK(map_fd < 0, "bpf_map__fd")) > > + goto cleanup; > > + > > + ip =3D mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED= , map_fd, 0); > > + if (!ASSERT_OK_PTR(ip, "mmap bpf map")) > > + goto cleanup; > > + > > + *ip =3D (uintptr_t)test_function; > > + > > + 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; > > + > > + perf_fd =3D 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 =3D 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 =3D F_OWNER_TID; > > + owner.pid =3D gettid(); > > + err =3D fcntl(perf_fd, F_SETOWN_EX, &owner); > > + if (!ASSERT_OK(err, "fcntl(F_SETOWN_EX)")) > > + goto cleanup; > > + > > + err =3D 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 >=3D 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 > > +#include > > + > > +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 =3D &test_function; Done. > > + > > +SEC("perf_event") > > +int handler(struct bpf_perf_event_data *data) > > +{ > > + const uint32_t index =3D 0; > > + uintptr_t *v =3D bpf_map_lookup_elem(&ip, &index); > > + > > + return !(v && *v =3D=3D PT_REGS_IP(&data->regs)); > > and so we the above global var suggestion this will be just: > > return ip =3D=3D PT_REGS_IP(&data->regs); > > > +} > > + > > +char _license[] SEC("license") =3D "GPL"; > > -- > > 2.34.1 > > - Kyle