Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp3016612rdb; Mon, 4 Dec 2023 14:15:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IG4DYxFcZ6fsCwdiA+MufbSDd6oNdvJi2X+iqG/M9gVJFsjVbgR4lux0hsdaxih8aKg2Xj8 X-Received: by 2002:a05:6a20:7f9c:b0:18f:5f6f:fc69 with SMTP id d28-20020a056a207f9c00b0018f5f6ffc69mr1875503pzj.97.1701728106795; Mon, 04 Dec 2023 14:15:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701728106; cv=none; d=google.com; s=arc-20160816; b=Lvb2zTCP2vSkBcZ/4qUt+0nWFm9zgXMRkKK1AWKgZJG7ZRwSLuKrbZgtdpKidn4fyU Em57+VS+U/iDeVZDekqWk6rBZlHwVpUwubyVoTdDapGktedVjoN40zLIQaG9+K+goaiK i+uEFF/5D4u/dWl+6yrWWK2upAZ5xTauImW+bEDroZQjueXHpd+XSIxhIK6p3kSZcWAF ZpLIuTqi57kTyG1fQsBrqOVvoudBM3eVs3Er+SBZWx+XvuBnErqY+Xh0N0QiUGklZ5PK +JNfyY1VXnFpp36d5MAKnDpO1wmzAeNgyJLTe5r+rqiyTYbj11BElQfF1w+EEkzmio+J w0ig== 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=cT5ING78Aw6QvaBr7RPKXeEm1alnh4pQOnp24KNcG6U=; fh=9hQ1PGr1N0f3GCF95Zutn93tX4weFMq5W8x/NcEylno=; b=QAn1udynyscWcfHWIBuSqTmYGBQWpiCZiM9qM0xlMz/ZHKf03es70OQpR3BStHICSO 9sERorqt20ueBU/ti/OgZavkwR6HZsNVYrmoKuMKWQ0zt0zmdSNSkO62FxzO9KXB54In tQjCAAaxGvvYQmlIt2Rjgb+MHl4FhcmzmDwtKOTZ5miEI5dxqQU8PUzDedg5WBGJ81qK sJ7Uvv0TIeCqyJa1q0XySmMx6BwR0k7sjpi60tuXCRhPs7pCO4tDmCappJWtcq658Whw Rs5B4cf1F/7eyeSrerpgw/S5ml0T2rOjYidWJ9oD324vg4KUgbeY+VHVUJDFj469PxNJ M/Fg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=LBcrlt+M; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id x28-20020a634a1c000000b00585a5e9a965si8336562pga.161.2023.12.04.14.15.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 14:15:06 -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=@gmail.com header.s=20230601 header.b=LBcrlt+M; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 4F745804ACFD; Mon, 4 Dec 2023 14:15:05 -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 S234013AbjLDWOz (ORCPT + 99 others); Mon, 4 Dec 2023 17:14:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229847AbjLDWOy (ORCPT ); Mon, 4 Dec 2023 17:14:54 -0500 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D560FF; Mon, 4 Dec 2023 14:15:00 -0800 (PST) Received: by mail-ej1-x632.google.com with SMTP id a640c23a62f3a-a1a5772b8a5so363454966b.1; Mon, 04 Dec 2023 14:14:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701728098; x=1702332898; 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=cT5ING78Aw6QvaBr7RPKXeEm1alnh4pQOnp24KNcG6U=; b=LBcrlt+MKjEO3c31uU2GKLPLSDABi1fW5KFcLhihgKg3/4NIz0K0I0sn0nT/sIiwe9 7KhY/zMJWyRWpoCxmZF6Sm84vz6Ie0tWoHIOSR1NVoWHB+8pTnhM+P4f1fkU0lt3qW6v B/nMBm3PptrsYM3tlMp+JoK3BBC6d3VH9AP8visDYkeyFtFc0dUoaFpSQmGnOwYpgNmi zm05yPVl2uyF1VcRUrUOj3u+Z7zXEO/0N4ZS/XzjfmO8yBIobBKlPV+3DDQs/EhxxqmF c+aN7EkQarbEYUkvYqzRnjhlLi0K5I3JMlX720yy4wrh+R7/FrXzj7lFI5Yeeaqhcv5D BULg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701728098; x=1702332898; 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=cT5ING78Aw6QvaBr7RPKXeEm1alnh4pQOnp24KNcG6U=; b=w/NrVxx3J3XPJaWwZmCON9oTi9CWh/jeaUiHtQmZY1T1GnvJ891gTD8IcY+ciCgLNh cf+9srdRa5Rh5SVc1Bqrz8/SqTNaXL7Cql51jhT1BNUEiV6CtgwEW+P9efkOiirPXwbw 45klZb6z+H6IaAsWcsOO/0tOdEj8ehE65kDyCz+J7KvXDCi079LGueQ3VRDat5GoZjEO 6ElP++dhI8XBlBkrYhsvB5ThN5X3508SqREFs7hh0WN10VGlQHqaekjphT+LyxpURGET jiwEPk1JOMyY9mGUxc642be2Dn3HYSZYhePap/4YONMwJKuFiDGxJTL0ICV5deHetAaW dsZg== X-Gm-Message-State: AOJu0YyQXHWH+sBdOahJXXt2bYEgIkXH+n2Bj9zqG4kEidFejvjOUk22 F9eJvKylFq5m6FJDajGQTay3kDjf9mfmpi8EkGJWZ8YWyzk= X-Received: by 2002:a17:906:74c2:b0:a19:a19b:78ac with SMTP id z2-20020a17090674c200b00a19a19b78acmr2117622ejl.111.1701728098257; Mon, 04 Dec 2023 14:14:58 -0800 (PST) MIME-Version: 1.0 References: <20231204201406.341074-1-khuey@kylehuey.com> <20231204201406.341074-3-khuey@kylehuey.com> In-Reply-To: <20231204201406.341074-3-khuey@kylehuey.com> From: Andrii Nakryiko Date: Mon, 4 Dec 2023 14:14:46 -0800 Message-ID: Subject: Re: [PATCH 2/2] selftest/bpf: Test returning zero from a perf bpf program suppresses SIGIO. To: Kyle Huey 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,FREEMAIL_FROM, 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]); Mon, 04 Dec 2023 14:15:05 -0800 (PST) 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 th= e > 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/t= esting/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? > + > +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 =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() 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 =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? > + 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 > + > + 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/t= esting/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; > + > +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 >