From patchwork Mon Dec 11 04:55:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 176472 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp6835094vqy; Sun, 10 Dec 2023 20:57:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IE3DPPkJNJqebN+CAaeiqrNkMyigk7oFdIJkmr1wg+C9ObhcAZ8kcmFXQLDsl7lahto/J2q X-Received: by 2002:a05:6a00:1303:b0:6ce:7656:5cba with SMTP id j3-20020a056a00130300b006ce76565cbamr4491131pfu.22.1702270638613; Sun, 10 Dec 2023 20:57:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702270638; cv=none; d=google.com; s=arc-20160816; b=lk2rGTgZdNI8xo03gWRufPTGspdczGlBos70n02rRfFpgkR8C6IpEizIe9+twRvZh2 fmIWbWewqu/J+e3GJjYZQCrTV/GTJ/4xK4Nc9ypsXGlqzZ34jZcyITqx2WHCB2thhZen uuGVJmZpCFpLwMuJyUhqO1MG0K98Cd3u4BCB2FYAOObPVy1jfGd5tiiDES40J1tOR1WL ToGnTsa8FE3t4jDvUtqqKQOoeU4WKanXYCGWc+W4Mif0uBu5O+Ixab/Ug0TSDaGZzxsa iJhCI8Azb1Z7YDXknWx8ohypSaDhtW1CrDi6jIXSuMcbh3O6jvG/3pFSLYnrdnUn+hie GOHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=f/uOwN8/AYKX/iTSNcMtLhs7X+zpyhpj6oRruBreRa4=; fh=mQYsxR7xYTE8DykXrbtIRCz296c9MEKm6OXiYrLoXUw=; b=nTSB7fBNPpifT8sn6YZTd9klEbxXQiL1OUdlr3MGdI/8N+Wps9lwjb555lw58FFL7D UwMyT1I6l45zshXgEd7BjIwEheHy4ntm06qqeSrIKgpBZs2VUTpTeyTgPinikuKwykZv i0of9xNgN10kWydMhGmj3popYYJ3BN9rF0cSSoPFwHnVnMqf4fEokWUu9gE+ysBBj7vA poEGbmVRXpsRcCY4+ykb95EmZw9T+NHCN9GcI4fK0yVtWLlXbdTYHX9TNzZAJTaA1nqE 6KVgYVefq8Tg36ojIIUdtdd+4QNLzoW1+N3ge+jzNYWyOA25B396mo3LBfwdysyMQ3oJ P13w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=MKSRvC1O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id cm25-20020a056a00339900b006ce11062b36si5506972pfb.228.2023.12.10.20.57.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Dec 2023 20:57:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=MKSRvC1O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id D604F806311A; Sun, 10 Dec 2023 20:57:06 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233203AbjLKE46 (ORCPT + 99 others); Sun, 10 Dec 2023 23:56:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233435AbjLKE4c (ORCPT ); Sun, 10 Dec 2023 23:56:32 -0500 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42A48198 for ; Sun, 10 Dec 2023 20:56:30 -0800 (PST) Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1d053c45897so35744605ad.2 for ; Sun, 10 Dec 2023 20:56:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1702270590; x=1702875390; 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=f/uOwN8/AYKX/iTSNcMtLhs7X+zpyhpj6oRruBreRa4=; b=MKSRvC1OxyjdP7zrUzYvQsbx6mJ74RpWWf1U6sy8Yn3lqxzf5ZqLSo8OeWz9ipEDm6 7W7bubSjoonHVa4aoZuJvUXxlxiUCQkvjbMsCUjiQX98riXNOgWNNrUgoug0opr7JOQQ MUMj8QHvTosq/9WO6urCr8IX49gUENbTMs55EhHuR3IjdOjBAT4DXImbI9Oavf+vvOO2 WknaPCL1RxrYAILMHixVA5da0s7EndwQBJlcoi7Gw6gueeWJvWA8+3JZ4OtW1IkxzQQY p6c5tr6SHhBtT64l0sAoQrMp4q+UTPBIZefdVufgHcLstbOepIhZ6+xMu4UP3/Chwoa3 nL1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702270590; x=1702875390; 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=f/uOwN8/AYKX/iTSNcMtLhs7X+zpyhpj6oRruBreRa4=; b=oBxUafeRQ46tH5+wde/j1hSOzzLgxylo1pBZZIv5JXwoMY4CPo3XHL7gPOQATc1+Ix tKyFy5GUjRBiao9T+1m655DXRo/abXSA0sZ5B2K4lZXCROAazf8JeW3MV5H4Ewcv3p7p rDSmTe8cpzPno2ToRNBylRmWegOtSVlxxNCVdww35xc1ZHGht5aktHUp1nIbMyYGa6sc yEfBO1QBhcIZwlxXduDeACuyv8ISJSPK6Vya+NEt4JjO1P0hb/Y6gkRMxziFn4sfFmT4 g2R73r5f3cyorUCAxxKMEPFWZjLsv5aYO53WIGyptofjH18O/oQt6PxMBQmnDtXw3AJX br7Q== X-Gm-Message-State: AOJu0Ywiz39VDZk80G6fqorXxDsFTurks951Kz7L6QoJfufqVrbdcTly 7VnfPHjakIg72m/4oKMX37bCYD1MqLt63oYnQUl2eg== X-Received: by 2002:a17:902:c3d1:b0:1d0:d168:daae with SMTP id j17-20020a170902c3d100b001d0d168daaemr4338646plj.95.1702270589707; Sun, 10 Dec 2023 20:56:29 -0800 (PST) Received: from zhadum.home.kylehuey.com (c-76-126-33-191.hsd1.ca.comcast.net. [76.126.33.191]) by smtp.gmail.com with ESMTPSA id e11-20020a170902b78b00b001d2ffeac9d3sm3300623pls.186.2023.12.10.20.56.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Dec 2023 20:56:29 -0800 (PST) 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 Cc: Robert O'Callahan , Andrii Nakryiko , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Shuah Khan , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v3 4/4] selftest/bpf: Test a perf bpf program that suppresses side effects. Date: Sun, 10 Dec 2023 20:55:43 -0800 Message-Id: <20231211045543.31741-5-khuey@kylehuey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231211045543.31741-1-khuey@kylehuey.com> References: <20231211045543.31741-1-khuey@kylehuey.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, 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 pete.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 (pete.vger.email [0.0.0.0]); Sun, 10 Dec 2023 20:57:07 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784960137196679078 X-GMAIL-MSGID: 1784960137196679078 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 --- .../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 = 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 = {}; + struct sigaction previous_sigtrap; + sighandler_t previous_sigio; + 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); + + 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; + signals_unexpected = 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); + + 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";