Received: by 2002:ab2:7407:0:b0:1f4:b336:87c4 with SMTP id e7csp171355lqn; Thu, 11 Apr 2024 18:52:48 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXcOvlCLaDIkOBdKN9bcqHL/4x2G0XxOtgijiMraVaQ/UWgKqPGUayB+TKm/4IhRhrVb3Zt3P1VtHJ7tL0g+gvthA3sXHTm9ZEQpw0hjg== X-Google-Smtp-Source: AGHT+IGKvIHcpZ8xye5w/MJmtiVEpFkD2yFa/o014Q06Coy6gKhv8mfMN6IfCWbGUufck+IML4ar X-Received: by 2002:a0c:c38e:0:b0:69b:14cb:234e with SMTP id o14-20020a0cc38e000000b0069b14cb234emr1557700qvi.39.1712886767785; Thu, 11 Apr 2024 18:52:47 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712886767; cv=pass; d=google.com; s=arc-20160816; b=0RkjZTx+CuGXlK7y0lrkYhbQNIFKfh/htMCKMdMhVJpI9LcgFofCUDau2uO3uLEoW3 LfAcR68lL44I5NlPQoTch0lOuEm5StvJpSRn1Zx28spVMtLsAK9pWTA4uz3HQ/2DBIsE pk/+USF9ntryW/n1SfuETjteilu5Qyo0/TnKU4f4M0ZxLPav9wGawMcIQIWB/E2CFCtg eErOVEH+XSkrIDZsxfhnZxGeyNGhD8o62I9nbCexxGcQnVO/aJ796t3aZQXQQfUI8GFR RZu0ZUSVlEYH3/oXgC3BrBne30iW0QBKniOWsPOjL8iASzxbwiwWlhQiypUPtayMGc4b BlIA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=PLsAtO8E8WEI0YPLnWc4zGD4gPrub60dCUyhat+mc0A=; fh=ORwsJAxSEl6lLGcPOeuS/mGVtYvkHKGxFBBNnwkLdCk=; b=gaTV3kzUzvxsy4Qu+106wXM8JxPZiGwu1Njwos1DcWNR8f6Uiw+M3lo3L4pO77cMnQ gILw4ScQABET6itXWA49yyOgVUF5Gf/6yq8YVHdo97T4jttr8wLdxOIEVlph1yYTMhYt PIjxVllyjn1Ay6NWTIC28pwELszvuGluxbD/GzTWomXFi+VOuoms8cC4vK+kcwU3wLUV 4F80Y9iH9GLvkWYQG/r6C/PPUaociBTtNGy72c3SBmZn4srC5/RqICMgjXdrHYhsb/u8 FsHUkS0iw8j7pnFG1tnnrpIKCnEKc8atB3T945XFuDeUvWsRm/T7yk+X8eD3yH8nwb1j +g8Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=BBumYXRF; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-141788-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-141788-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id q4-20020a05621419e400b0069b55bbf6e2si865927qvc.499.2024.04.11.18.52.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 18:52:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-141788-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=BBumYXRF; arc=pass (i=1 spf=pass spfdomain=kylehuey.com dkim=pass dkdomain=kylehuey.com); spf=pass (google.com: domain of linux-kernel+bounces-141788-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-141788-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id AA38C1C229C1 for ; Fri, 12 Apr 2024 01:52:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6324FFC12; Fri, 12 Apr 2024 01:51:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kylehuey.com header.i=@kylehuey.com header.b="BBumYXRF" Received: from mail-yw1-f181.google.com (mail-yw1-f181.google.com [209.85.128.181]) (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 E94FD24B23 for ; Fri, 12 Apr 2024 01:51:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886667; cv=none; b=DXafGeP5tZe+YkaYDrHrHcuCYaKfhgbaK90URU8fjCn70sz/hYoI+hTR95N1K1sHw9HIqLgLvrIXhXhhPiHMyoGWsURU6KP0ywzIgr8v2Uor70LiLtYizROAx7SJvqL/s8nCJq08ApezgLv3MUlcU8eyY6dz4OHRJcWmtDNkqy4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712886667; c=relaxed/simple; bh=EfTqzolUvIxbeV77szfL3e0e1IBqxiKk0wQkbdth9Jg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=E/pRaQ5zCni/ujW2Om4c8vIXjukF3tbxQAxVnC95zSBXeUWkMmv4KYhbQgQK0UV8ll/ExvWJg6CkJSAbs6hFwxw9d+aUJNWJmODaqm+jmB8UuBOFg81fDK1DAbfQzEJ8A6/t62FMKQtmjjBBedsmBf9BAIqlJJnWB1rS2/eFKM0= 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=BBumYXRF; arc=none smtp.client-ip=209.85.128.181 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-yw1-f181.google.com with SMTP id 00721157ae682-617d4797d9bso3587177b3.1 for ; Thu, 11 Apr 2024 18:51:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1712886665; x=1713491465; 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=PLsAtO8E8WEI0YPLnWc4zGD4gPrub60dCUyhat+mc0A=; b=BBumYXRFyY4oAk5wBat/ahkj0vo4ZnGDizw9oe7uiI4of7VQSdzqMqicfgz6Iqva3+ TWB3iYBSoKCwGzfyBadYhTJblNxUidbYGWt29yAxB5uG5AVFqqKSDB9JIUZvJIq3h5un AqcdhEOTYPdqqEB4RwLqMoOsWiVbiB2K6Ecn3dTiLeWaFd5ZlStzsB5463mCQOB7hSHP EamyeE7eoRo/SMtDLC1565o/z8czbepN5SJj7jsJXvH6TKSiN6tBk8KfyjROBuQ25zXo YliCS1PX3q8xNQUWIf+bDWLpFXKq81+s1iRIV0vtGBpRQ4V73mrheJV4nWzRPm6DOH2i aitQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712886665; x=1713491465; 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=PLsAtO8E8WEI0YPLnWc4zGD4gPrub60dCUyhat+mc0A=; b=XnjZvCU9Dpn0APyImLZLDrZlCx+C+2FB8u8H6XslJHqdijkfyEPHFI/Bnn2YFIqgL6 +ClHx6ityJ9jdRp83ImxaEaag6KWAL0dRh8N4WrB/z+RVXA1cmZblJQ5hBpFXSGlMpwj /oemwqyTCReTDqT80XZxcbVOoX2hxbNQOzfBPxw8c895/ecr18tbQujjopGrAL/0iy/O ZFVq1olDgWy+RMDaWl0uuilQm7p8jdUrDUhTjJUHOIcQy6ZXlOPgllhMHSJzG9i8Gr+6 kFfKQMIQOTxfv0VYLLS+8AOQDCdt7imTOpoEIH5LcPCSmAZf/ZoIK/6CmS8cDGb1k9Ie e3zA== X-Forwarded-Encrypted: i=1; AJvYcCWKNC3yQklnYPWXyAGqEaz7sz4u6EfNlrWlHv131z3vOUlX7QRjXJdonMmAvXeIr0oC+fFYfCF72JHBpxfw+FNhycTmXoV2ZU4P12o2 X-Gm-Message-State: AOJu0YwdkCkBL2zTEHz2Qp0vZHYkVFUoIOH1XaGSiRh4sLoaeu/2lz+J qWyJQylDCbVTkYR2GahBE4UeLwvSjpFDIzLvzklD96suiDac1s0vm/92ifIa5w== X-Received: by 2002:a81:4ec7:0:b0:615:21e7:6bf6 with SMTP id c190-20020a814ec7000000b0061521e76bf6mr1172665ywb.14.1712886664785; Thu, 11 Apr 2024 18:51:04 -0700 (PDT) Received: from ip-172-31-44-15.us-east-2.compute.internal (ec2-52-15-100-147.us-east-2.compute.amazonaws.com. [52.15.100.147]) by smtp.googlemail.com with ESMTPSA id f10-20020a05620a15aa00b0078d76c1178esm1756677qkk.119.2024.04.11.18.51.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 18:51:04 -0700 (PDT) From: Kyle Huey X-Google-Original-From: Kyle Huey To: Kyle Huey , linux-kernel@vger.kernel.org, Andrii Nakryiko , Jiri Olsa , Namhyung Kim , Marco Elver , Yonghong Song , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo Cc: Robert O'Callahan , bpf@vger.kernel.org, Song Liu , Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Shuah Khan , linux-kselftest@vger.kernel.org Subject: [PATCH v6 7/7] selftest/bpf: Test a perf bpf program that suppresses side effects. Date: Thu, 11 Apr 2024 18:50:19 -0700 Message-Id: <20240412015019.7060-8-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240412015019.7060-1-khuey@kylehuey.com> References: <20240412015019.7060-1-khuey@kylehuey.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 the expected value. Then the function with the breakpoint is executed multiple times to test that all effects behave as expected. Signed-off-by: Kyle Huey Acked-by: Song Liu Acked-by: Jiri Olsa Acked-by: Andrii Nakryiko --- .../selftests/bpf/prog_tests/perf_skip.c | 137 ++++++++++++++++++ .../selftests/bpf/progs/test_perf_skip.c | 15 ++ 2 files changed, 152 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..37d8618800e4 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/perf_skip.c @@ -0,0 +1,137 @@ +// 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 sigio_count, sigtrap_count; + +static void handle_sigio(int sig __always_unused) +{ + ++sigio_count; +} + +static void handle_sigtrap(int signum __always_unused, + siginfo_t *info, + void *ucontext __always_unused) +{ + ASSERT_EQ(info->si_code, TRAP_PERF, "si_code"); + ++sigtrap_count; +} + +static noinline int test_function(void) +{ + asm volatile (""); + return 0; +} + +void serial_test_perf_skip(void) +{ + struct sigaction action = {}; + struct sigaction previous_sigtrap; + sighandler_t previous_sigio = SIG_ERR; + struct test_perf_skip *skel = NULL; + struct perf_event_attr attr = {}; + int perf_fd = -1; + int err; + struct f_owner_ex owner; + struct bpf_link *prog_link = NULL; + + action.sa_flags = SA_SIGINFO | SA_NODEFER; + action.sa_sigaction = handle_sigtrap; + sigemptyset(&action.sa_mask); + if (!ASSERT_OK(sigaction(SIGTRAP, &action, &previous_sigtrap), "sigaction")) + return; + + previous_sigio = signal(SIGIO, handle_sigio); + if (!ASSERT_NEQ(previous_sigio, SIG_ERR, "signal")) + goto cleanup; + + skel = test_perf_skip__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_load")) + goto cleanup; + + 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; + attr.sigtrap = 1; + attr.remove_on_exec = 1; + + perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0); + if (perf_fd < 0 && (errno == ENOENT || errno == EOPNOTSUPP)) { + 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 = 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 = syscall(__NR_gettid); + err = 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 = ioctl(perf_fd, PERF_EVENT_IOC_REFRESH, 1); + if (!ASSERT_OK(err, "ioctl(PERF_EVENT_IOC_REFRESH)")) + goto cleanup; + + prog_link = 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 = (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 = 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) + */ + 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 >= 0) + close(perf_fd); + test_perf_skip__destroy(skel); + + if (previous_sigio != SIG_ERR) + 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 != PT_REGS_IP(&data->regs); +} + +char _license[] SEC("license") = "GPL"; -- 2.34.1