Received: by 2002:a05:7412:ba23:b0:fa:4c10:6cad with SMTP id jp35csp683557rdb; Thu, 18 Jan 2024 16:08:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IF2Kkpba3TH+Q+Xcb2xIbyeR7NfZXyBqfXpzQJqLt3GXdZZpFOjTQN5C0mnRJRqWQp0+wbV X-Received: by 2002:a17:90a:f0d2:b0:28d:70b5:fafb with SMTP id fa18-20020a17090af0d200b0028d70b5fafbmr1492050pjb.32.1705622939128; Thu, 18 Jan 2024 16:08:59 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705622939; cv=pass; d=google.com; s=arc-20160816; b=pvE+mqejUVf9SShpHv0cAZG9B0y2g8Suy1ctg2YGi7pW/xJASeXFu3An42/eoBgHi6 Y+qyROx7PcTG/jcUKcQVOFB6qG2eyIpwQuLC2sRwKNHrkednRRZ4qA2GjzzYGlFxbeON BdaNu+Zpln1wt32Hb+tWp1Kk1TuhfyWoDfT+ATIcYJxQN2f9a9zEsVE2XPglBYY9eTW2 DzHvJ77nTgMH29Zpj5JprgKHu2BG6vuVPVzbNriiG42nJsoN5jQwlx3ZbJWrEER5IEiK eQEsNc9s4n/jpcdq3wP9qYU5ofpGlHhC1tsZDY8jrANwDYJheRhkDPgBPTCmMyOLjiON hsDg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=nkPMMIL6cG9Vww2U8ysDt1UGfqwAexusqourdf/9zpY=; fh=Tj+1w9uwS3DlbjzTxg6izrELQNTEebmp10i0jXtt3aw=; b=Ci5R04PCkhNovp9DwTSjerBvIpitDQ0Ydlfj3js0igMIKjD2g5YeNwbaOeHdyO5SD5 2/LLclfUGoe9mQ1W8FBffpmKAdVo7B6AH/J4um/IlPRrNYKKX5zotx9QT3BFq/DtFySA 48JCVXi9qCW3BbWm/29sAcrmHLRsjXveTtMW35EL5ymkjUt7wCL7dMoDYJ/bF9xKPONr kmHD1f9JP0976+LN+DwWLmpncZcswQnUjZfbDkOdZ07wWeHMW1LJsEn3KUVaIUwPJeVs XUkjER7YuKt2vmUaxcAh17Ph7F0QmThklteWLf5iIzq0GT/5X9yScYTSEArEVJ1L9Wj5 G69Q== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=bps6f9wA; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-30648-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-30648-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id l7-20020a17090a850700b002904a9cfc34si286680pjn.165.2024.01.18.16.08.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jan 2024 16:08:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-30648-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=bps6f9wA; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-30648-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-30648-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id EA42FB22EC7 for ; Fri, 19 Jan 2024 00:08:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 79D6C81B; Fri, 19 Jan 2024 00:08:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="bps6f9wA" Received: from mail-lj1-f170.google.com (mail-lj1-f170.google.com [209.85.208.170]) (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 C094863D for ; Fri, 19 Jan 2024 00:08:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705622927; cv=none; b=MGm93JFrV5DcFr79sGLYGvBZvTeaj3QcU0EkAy+XMr3H9hGHs4CyAipS55HFbEajnJtGKCS+viyAA4vtjWRmjBLuLDw01i7m3175PMQ4Ot/7SzP5hefIXVD03EsLS8gExX4PwKTqNpobNQXGF5IBKX7FZnXIB6c4PT36bF+Qvy8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705622927; c=relaxed/simple; bh=Nl+g4tuaqCxOgaP3Iln3PLlQaui5SCReIL+qHRQfNC8=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=r97yyuMPjXyEtM+ENhyHdzZaZzSZmk/x3rtHyZQ9HgNZ7JdnKFNvy4waWGIiYbgp09CbhIPwa8U261JIM3zfuZwo1pOGhm5Vfm3ItvzX2/jZU21CU2vrmcj2ln3lwP6mJkuWGK/yBRPVFWqyTDKLG5t4dguhyEV+xn4mWIDxr20= 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=bps6f9wA; arc=none smtp.client-ip=209.85.208.170 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 Received: by mail-lj1-f170.google.com with SMTP id 38308e7fff4ca-2ccae380df2so2793101fa.1 for ; Thu, 18 Jan 2024 16:08:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1705622924; x=1706227724; 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=nkPMMIL6cG9Vww2U8ysDt1UGfqwAexusqourdf/9zpY=; b=bps6f9wAA0AU+KQiJVFMFH2DL2v0ct7AR3MMpIcZgczl8cr27GvuWteHqbOaE6k9Vm J9Vl6CfqIK7PPOB2wlkMTzzxcUbKSCoX1GJf72LsPgVMObKa2a0rhbIkTnYBHLusxEcT JvljttiDLwFuUN+tLjfMQP81YZIQD8+ki8rcy/kHlO74stkbYaJ39iF1F/OOFj4goRST vYzFM1/vFCmTKb82GmwdFAqg0NgCSc6y7B5XfhBOkWM0w4Hyj95vlTMwSwXPpBadZKbR SvNmXLTGBpplvnuPmFFnc85LMSMocnwcnDMmteVmunkSuvHzh1SL8XS9T4NjQZT7two7 bu+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705622924; x=1706227724; 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=nkPMMIL6cG9Vww2U8ysDt1UGfqwAexusqourdf/9zpY=; b=BmmUSt879dCaxTVpj2CBX+MK6sxl0Cg0bs6u4DFTajt2OzMwoy2yP0KE3w7qrIBT6B qwSXxQSU1RnlNDlkugYEKRGKFa1gHwWZ/lYFr0io12gO0BlsgnK2xOkd7NYTq49joRXy slHNQr8vvvPlZ0+7YE6/z+Q2s5BnbILVO4Bz6QMI0mMIJ2EZWLtYxVtyoPlQT6d2YFwi Q/q3HaW5hYCBpm2DOznKx+s7jfx25IBSB/HRAZyUgLtQtYAq22B628v7f0lk8ERqrpGQ UdjOQq3BCwQdGfZ5s/rJegSaHNU9MrlbP1NngVzlG7jOmh6Z91Eolmrz28/WFjS5sq5L RaVg== X-Gm-Message-State: AOJu0YySIQ6d0S/NY0cXfIvXrRTB7ONqhbebVxZACk5wY3wuFP31VZ7c FptGaSWeWlHGy6Q+gEI1GhIYGTycTNxRHIMTcJ4Sy3u8S3ZAeh6tmxX9jv/DWM6OWS2tr5B7cy3 bQ++X06iYApodkglMmatS5F+qA2aRdu/Xt5n+ X-Received: by 2002:a2e:2e0b:0:b0:2cc:7db2:acb5 with SMTP id u11-20020a2e2e0b000000b002cc7db2acb5mr877140lju.93.1705622923687; Thu, 18 Jan 2024 16:08:43 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20231211045543.31741-1-khuey@kylehuey.com> <20231211045543.31741-5-khuey@kylehuey.com> In-Reply-To: From: Kyle Huey Date: Thu, 18 Jan 2024 16:08:32 -0800 Message-ID: Subject: Re: [PATCH v3 4/4] selftest/bpf: Test a perf bpf program that suppresses side effects. To: Song Liu Cc: Kyle Huey , linux-kernel@vger.kernel.org, Andrii Nakryiko , Jiri Olsa , Namhyung Kim , Marco Elver , Yonghong Song , "Robert O'Callahan" , Andrii Nakryiko , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Shuah Khan , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Jan 2, 2024 at 2:49=E2=80=AFPM Song Liu wrote: > > On Sun, Dec 10, 2023 at 8:56=E2=80=AFPM Kyle Huey wrote= : > > > > 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 t= he > > expected value. Then the function with the breakpoint is executed multi= ple > > times to test that all effects behave as expected. > > > > Signed-off-by: Kyle Huey > > --- > > .../selftests/bpf/prog_tests/perf_skip.c | 140 ++++++++++++++++++ > > .../selftests/bpf/progs/test_perf_skip.c | 15 ++ > > 2 files changed, 155 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..0200736a8baf > > --- /dev/null > > +++ b/tools/testing/selftests/bpf/prog_tests/perf_skip.c > > @@ -0,0 +1,140 @@ > > +// 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 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"); > > ASSERT_OK is a little confusing. Maybe do something like: > > static int signals_expected; > static void handle_sigio(int sig __always_unused) > { > ASSERT_EQ(signals_expected, 1, "expected sig_io"); > } > serial_test_perf_skip() > { > ... > signals_expected =3D 1; > } > I'll just drop signals_expected. Now that I'm counting the exact number of signals it's redundant. > > + ++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"); > ditto > > > + 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), = "sigaction")) > > + return; > > + > > + previous_sigio =3D signal(SIGIO, handle_sigio); > > handle signal() errors here? Addressed in v4. > > + > > + 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 EOPNOTS= UPP)) { > > + 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. > > + */ > > Multi-line comment style should be like Addressed in v4. > /* 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.handle= r, 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,= not > > + * by bpf) > > + */ > > ditto. > > > + 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); > > + > > + signal(SIGIO, previous_sigio); > > + sigaction(SIGTRAP, &previous_sigtrap, NULL); > > +} > > 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..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"; > > -- > > 2.34.1 > > - Kyle