Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1882065rdb; Thu, 7 Dec 2023 11:13:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IFfdnDPdPJG4j/Pmu3YKgkr5p+rLDo6UOuI0Cp2gHbd0QqO1k227F97V0o90K/zCj5jmVH6 X-Received: by 2002:a05:6a00:328e:b0:6ce:66e6:8c1e with SMTP id ck14-20020a056a00328e00b006ce66e68c1emr3332218pfb.50.1701976394266; Thu, 07 Dec 2023 11:13:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701976394; cv=none; d=google.com; s=arc-20160816; b=W/gI1nxuTGdTFaa/iRFb6hUgRi5zxvJTiZNtkAcjqfKQcG+QqLBUMxZCMOCOUYRpny y5lEXInZWIcBL6RNw4T6hcqz6VQ57dEGRNgPqx8Zw14PBEOJLpiC1pEFkwbVQ5LTLtk5 OP/TSX91CfkzCWBecfIsXQNWu0cX+LK2fuwWt7Wr/10vv3RUB5rOahMeHYUvWIJKg24/ tSt9PculF0rfzkJbJh8cfs5/U3GY3hkYE0YkEitMIwzRX3JVGbDEOccdde46Ycqz0UwN roN+TZV/n/TUYgr+4R5hr5JpC7KjVBuI39XoGnNv67/PE+No0Ngl751rXi0zzazUCyjE jzGA== 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=r7L52bButApvPAe1l+vSFfVbgaHr8LHhwAbqHXZCi2I=; fh=OCS9zMkQCd/4yoOdns5G7OS+Eetb0AH1p6oLbag73Ak=; b=u5xlGvJWNp9BmNHXrT8YfkiTJHL4dsqyzuNV8EWCsgrQ7ONpgsttIUfosRboyCm0D0 xeUmxsBEgJAs4XYE4jWXmf5/E945FvYNIaTJ4UTYzLyNXt1uOA65dFzLAG+dxreb74Z8 uVNCiKWqa8j8N43BUHSGfeZgRWjcbRD2Nc1rQrAbcniiGv2JAEuiC41TN6h0xu9/OV6M +qv7XS9ZGjcH6foqdYrapKJkaV26L1Osdyi/2U6QtpoFszCfRGL0YGCYH3XzrReyfYqb lkLPao/7JWLQVx90941bNtGi8XPDh00R7mBcxLQ1RBHlIkcVa4SPFO/NYJu97GcNoWmg AcBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=VMWAIfXo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id fh31-20020a056a00391f00b006c1222c5eaasi164309pfb.175.2023.12.07.11.13.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 11:13:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=VMWAIfXo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id 151E580FD8A4; Thu, 7 Dec 2023 11:13:10 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231178AbjLGTM4 (ORCPT + 99 others); Thu, 7 Dec 2023 14:12:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229541AbjLGTMz (ORCPT ); Thu, 7 Dec 2023 14:12:55 -0500 Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [IPv6:2a00:1450:4864:20::234]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FC6E10F9; Thu, 7 Dec 2023 11:12:59 -0800 (PST) Received: by mail-lj1-x234.google.com with SMTP id 38308e7fff4ca-2c9eca5bbaeso14529441fa.3; Thu, 07 Dec 2023 11:12:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701976377; x=1702581177; 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=r7L52bButApvPAe1l+vSFfVbgaHr8LHhwAbqHXZCi2I=; b=VMWAIfXoi1i7HFkBSZPZSpjFVb29QjWZFeQdCEiDhBsBM2oyOXFXd6+Jqyg7GrX7B+ pobFwrlbsaFsJ6HtRAn262TSdiriJNEmNFtdgTkAVvrDhO8WQRUechtHxxXvlDoDXjP/ 12ICOAYkqEaWL6Oe6JFmmcqsSTRtru9NI4jrst/9yiQa9xIfU+k4cvDaRxePIZs4lm0D yGOT3J+pVeCr7UIV9QryfVA/94NA2S2UOT+n94m6Znt3hbGOOD1vj4uWy3MO51aahRdh RNHreeTslveH+HJ5mIr+k2XlHOhksItrAoaS+lQ/0C+hJjxvqfwMxwoP/wfs9UghUwwN IXNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701976377; x=1702581177; 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=r7L52bButApvPAe1l+vSFfVbgaHr8LHhwAbqHXZCi2I=; b=UlqIBOCuZEFYwW522C78E+9yC5XMBBNfoLR8YuPhKF9KytbOa0/QrjulrCzy/xeAas n8j62lxYKuYqQQTwMVCopgeOIaeg8QnDO8ykbniOZ9mstU3HJZU9H3hwNxPZF63Qul1B t2+NAyt4KvUTXVF7fxDjKwQeSo+NVKI+LAUi81Ur5lbCR40N3Ffg7dKRIg5s9jbpNles ANFVWXBl/VPvfr/c+emDVxFXQJ5o4w+Ruy7JdIxcKxhBDOZMatVFG2HSXXWXwUw0DfcH 0XTaMJEXus0waq6DVsgppgW86sm2ybwO+bfU8hlMlCMa1KD9GA7U7Duu77QVxEYWOa+V U7gw== X-Gm-Message-State: AOJu0YwPWWielJeZMvPegCLu5TZ49UDZD8rNKno8sd3rwxYUnSkCiwdH R6Bn8bIBVOQgIO214XE1ZlunrtzsoEzy/39hUs0= X-Received: by 2002:a2e:901a:0:b0:2c9:f776:e28f with SMTP id h26-20020a2e901a000000b002c9f776e28fmr928562ljg.120.1701976377304; Thu, 07 Dec 2023 11:12:57 -0800 (PST) MIME-Version: 1.0 References: <20231207163458.5554-1-khuey@kylehuey.com> <20231207163458.5554-4-khuey@kylehuey.com> In-Reply-To: <20231207163458.5554-4-khuey@kylehuey.com> From: Andrii Nakryiko Date: Thu, 7 Dec 2023 11:12:45 -0800 Message-ID: Subject: Re: [PATCH v2 3/3] selftest/bpf: Test a perf bpf program that suppresses side effects. To: Kyle Huey Cc: Kyle Huey , linux-kernel@vger.kernel.org, Jiri Olsa , Namhyung Kim , Marco Elver , Yonghong Song , "Robert O'Callahan" , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Mykola Lysenko , 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=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email 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 (lipwig.vger.email [0.0.0.0]); Thu, 07 Dec 2023 11:13:10 -0800 (PST) On Thu, Dec 7, 2023 at 8:35=E2=80=AFAM Kyle Huey wrote: > > The test sets a hardware breakpoint and uses a bpf program to suppress th= e > 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 multipl= e > times to test that all effects behave as expected. > > Signed-off-by: Kyle Huey > --- > .../selftests/bpf/prog_tests/perf_skip.c | 145 ++++++++++++++++++ > .../selftests/bpf/progs/test_perf_skip.c | 15 ++ > 2 files changed, 160 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..f6fa9bfd9efa > --- /dev/null > +++ b/tools/testing/selftests/bpf/prog_tests/perf_skip.c > @@ -0,0 +1,145 @@ > +// SPDX-License-Identifier: GPL-2.0 > +#define _GNU_SOURCE > + > +/* We need the latest siginfo from the kernel repo. */ > +#include selftests are built with UAPI headers' copies under tools/include, so CI did catch a real issue, I think. Try copying include/uapi/asm-generic/siginfo.h into tools/include/uapi/asm-generic/siginfo.h ? > +#define __have_siginfo_t 1 > +#define __have_sigval_t 1 > +#define __have_sigevent_t 1 > +#define __siginfo_t_defined > +#define __sigval_t_defined > +#define __sigevent_t_defined > +#define _BITS_SIGINFO_CONSTS_H 1 > +#define _BITS_SIGEVENT_CONSTS_H 1 > + > +#include > +#include "test_perf_skip.skel.h" > +#include > +#include > +#include > + > +int signals_unexpected =3D 1; > +int sigio_count, sigtrap_count; > + > +static void handle_sigio(int sig __always_unused) > +{ > + ASSERT_OK(signals_unexpected, "perf event not skipped"); > + ++sigio_count; > +} > + > +static void handle_sigtrap(int signum __always_unused, > + siginfo_t *info, > + void *ucontext __always_unused) > +{ > + ASSERT_OK(signals_unexpected, "perf event not skipped"); > + ASSERT_EQ(info->si_code, TRAP_PERF, "wrong 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; > + 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), "s= igaction")) > + return; > + > + previous_sigio =3D signal(SIGIO, handle_sigio); > + > + 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 EOPNOTSUP= P)) { > + 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. please don't use C++ style comments > + 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; > + signals_unexpected =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, n= ot > + // by bpf) > + test_function(); > + > + ASSERT_EQ(sigio_count, 1, "sigio_count"); > + ASSERT_EQ(sigtrap_count, 1, "sigtrap_count"); > + > +cleanup: > + if (prog_link) nit: no need for a check, bpf_link__destroy() handles NULLs just fine > + bpf_link__destroy(prog_link); > + if (perf_fd >=3D 0) > + close(perf_fd); > + if (skel) > + test_perf_skip__destroy(skel); same, no need for NULL check > + > + signal(SIGIO, previous_sigio); > + sigaction(SIGTRAP, &previous_sigtrap, NULL); > +} > 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..417a9db3b770 > --- /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. C++ comments > + return ip !=3D PT_REGS_IP(&data->regs); > +} > + > +char _license[] SEC("license") =3D "GPL"; > -- > 2.34.1 >