Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp420349rdb; Tue, 5 Dec 2023 08:57:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IGsbl7taUBqPs9+A8DrGvGCBN6DvMtiewm9m1tSFTezg7qFvtHkqdSK804NptRqqgxGyzJ7 X-Received: by 2002:a05:6a21:1a5:b0:18f:bce9:594a with SMTP id le37-20020a056a2101a500b0018fbce9594amr325785pzb.38.1701795434872; Tue, 05 Dec 2023 08:57:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701795434; cv=none; d=google.com; s=arc-20160816; b=df4XaySHn//3us6Vu4fPtBjZmZ5XzeAOuSd3u2t/5T0ThaxRMYWeSWhEHZgUB1C1c3 j4KmePEGwHLIgSqdYS+MuYOhPu8P/uFW7Hd8Nrex822tC7figkt6/qPiPeSorS9jhseE ObCfINfo0bsLhRyLvBUdR/bCg8zkK9+aWvPmO5118Q6v8IKcRciKuhDSGeXnPHEw9Suy GmSzOixOiznP0bTAyWhLk9WWBrz/coIRIljLBdg60AOnYy8u2pGLhcpgY0QV0BaDCuWy yAchjyFLRYC/bavAhwDxEJX/EDx0E6tVsEd1I9AUFAlUC5RHNNJj0R2ya9wTBIpqJ6r7 Nqzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:mime-version:date :dkim-signature:message-id; bh=ZF3HKdpkUYHivvRpAHFn3rEF2x4jY5PYbwjx9JOVu3E=; fh=feqlMTEQwBjaiK1EDjzMkoavCejh7/APowOT7FVHa3k=; b=cOdPuv6YXaW6zoPxKx180/nQbIh0Z1Iuasj/wKFdg0G3mpb0Mv5dhiwR01BepgVgpd aF81iRRi4xXaE9VW+agUaf2TyJrG/UBbQqNvwISkORDQwL3dFGuIJ03txdosGkBq9cXU HvtX0mgZ/pMvypVqX4nHWOM5MR9g7NX3rCzmnocY1CqZTVsM/J8dKvEX1YxSsacno2CD ox8mP+XLcnU4ckTpbFn4EiDGz4qZ0/sZWxMtsbJcxTs4hioh10RWVTcYLKcNWfKGZzNc 0A3m9tT4sV5kDp2A+X83bJB74V8UEgtYrLNMWFThtKlzEoN5LDD+ASJfA58W/dCKuS04 W0xw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=EhGU4wmC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id g22-20020a1709029f9600b001cf5572d972si7373032plq.210.2023.12.05.08.57.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 08:57:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=EhGU4wmC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 173A080689EE; Tue, 5 Dec 2023 08:55:41 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229462AbjLEQzY (ORCPT + 99 others); Tue, 5 Dec 2023 11:55:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235216AbjLEQzJ (ORCPT ); Tue, 5 Dec 2023 11:55:09 -0500 Received: from out-186.mta1.migadu.com (out-186.mta1.migadu.com [IPv6:2001:41d0:203:375::ba]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93DF6198D for ; Tue, 5 Dec 2023 08:54:12 -0800 (PST) Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1701795250; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZF3HKdpkUYHivvRpAHFn3rEF2x4jY5PYbwjx9JOVu3E=; b=EhGU4wmCYyp07Ts+1DE5grTY3Sa7dC6p4b+M8EA50s/cqLKx/tNEewXo2w34DMYWAjXRiG dC4LRQhfb7+SuHDbRr+yB8fzOzg6gRYClNgAsob/ctKQNZJbRn9c+Od1gtaJGP3SQEYgVr WvgAQYh4Ky+LRbsixpHMKTFloYpDy7Q= Date: Tue, 5 Dec 2023 08:54:02 -0800 MIME-Version: 1.0 Subject: Re: [PATCH 2/2] selftest/bpf: Test returning zero from a perf bpf program suppresses SIGIO. Content-Language: en-GB To: Kyle Huey , Kyle Huey , linux-kernel@vger.kernel.org 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 , Jiri Olsa , Shuah Khan , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org References: <20231204201406.341074-1-khuey@kylehuey.com> <20231204201406.341074-3-khuey@kylehuey.com> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Yonghong Song In-Reply-To: <20231204201406.341074-3-khuey@kylehuey.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT 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=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.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 (groat.vger.email [0.0.0.0]); Tue, 05 Dec 2023 08:55:41 -0800 (PST) On 12/4/23 3:14 PM, Kyle Huey wrote: > The test sets a hardware breakpoint and uses a bpf program to suppress the > 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/testing/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" > + > +static void handle_sig(int) I hit a warning here: home/yhs/work/bpf-next/tools/testing/selftests/bpf/prog_tests/perf_skip.c:10:27: error: omitting the parameter name in a function definition is a C23 extension [-Werror,-Wc23-extensions] 10 | static void handle_sig(int) | Add a parameter and marked as unused can resolve the issue. #define __always_unused __attribute__((__unused__)) static void handle_sig(int unused __always_unused) { ASSERT_OK(1, "perf event not skipped"); } > +{ > + ASSERT_OK(1, "perf event not skipped"); > +} > + > +static noinline int test_function(void) > +{ > + return 0; > +} > + > +void serial_test_perf_skip(void) > +{ > + sighandler_t previous; > + int duration = 0; > + struct test_perf_skip *skel = NULL; > + int map_fd = -1; > + long page_size = sysconf(_SC_PAGE_SIZE); > + uintptr_t *ip = NULL; > + int prog_fd = -1; > + struct perf_event_attr attr = {0}; > + int perf_fd = -1; > + struct f_owner_ex owner; > + int err; > + > + previous = signal(SIGIO, handle_sig); > + > + skel = test_perf_skip__open_and_load(); > + if (!ASSERT_OK_PTR(skel, "skel_load")) > + goto cleanup; > + > + prog_fd = bpf_program__fd(skel->progs.handler); > + if (!ASSERT_OK(prog_fd < 0, "bpf_program__fd")) > + goto cleanup; > + > + map_fd = bpf_map__fd(skel->maps.ip); > + if (!ASSERT_OK(map_fd < 0, "bpf_map__fd")) > + goto cleanup; > + > + ip = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0); > + if (!ASSERT_OK_PTR(ip, "mmap bpf map")) > + goto cleanup; > + > + *ip = (uintptr_t)test_function; > + > + 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; > + > + perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0); > + if (CHECK(perf_fd < 0, "perf_event_open", "err %d\n", perf_fd)) > + goto cleanup; > + > + 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 = gettid(); I hit a compilation failure here: /home/yhs/work/bpf-next/tools/testing/selftests/bpf/prog_tests/perf_skip.c:75:14: error: call to undeclared function 'gettid'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]    75 |         owner.pid = gettid();       |                     ^ If you looked at some other examples, the common usage is do 'syscall(SYS_gettid)'. So the following patch should fix the compilation error: #include ... owner.pid = syscall(SYS_gettid); ... > + err = fcntl(perf_fd, F_SETOWN_EX, &owner); > + if (!ASSERT_OK(err, "fcntl(F_SETOWN_EX)")) > + goto cleanup; > + > + err = ioctl(perf_fd, PERF_EVENT_IOC_SET_BPF, prog_fd); > + if (!ASSERT_OK(err, "ioctl(PERF_EVENT_IOC_SET_BPF)")) > + goto cleanup; > + > + test_function(); As Andrii has mentioned in previous comments, we will have issue is RELEASE version of selftest is built RELEASE=1 make ... See https://lore.kernel.org/bpf/20231127050342.1945270-1-yonghong.song@linux.dev > + > +cleanup: > + if (perf_fd >= 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/testing/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"); > + > +SEC("perf_event") > +int handler(struct bpf_perf_event_data *data) > +{ > + const uint32_t index = 0; > + uintptr_t *v = bpf_map_lookup_elem(&ip, &index); > + > + return !(v && *v == PT_REGS_IP(&data->regs)); > +} > + > +char _license[] SEC("license") = "GPL";