Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp456452rdb; Tue, 5 Dec 2023 09:52:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IHQOlqGE3iu2oYgmCDjfsQZR5vsqTrw6RKxwojX0AGR8aR6hpSOlLNbhpvW/vSsIs9Qcue4 X-Received: by 2002:a05:6a21:6da7:b0:18f:221b:52a8 with SMTP id wl39-20020a056a216da700b0018f221b52a8mr3933268pzb.59.1701798750370; Tue, 05 Dec 2023 09:52:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701798750; cv=none; d=google.com; s=arc-20160816; b=olAxrUCKh2p2DeLisDQ4qOfJyVTciE1eePQImFed90QWO4hJGYqtuOsqJ/diSjWdIB Q9p4stj4kB6AJv0WSgK/k+JmKFDDK/OkTRGO+8VDfcbmxSD7YlT9gocNQTknu12Dqa6f dPATOS8hPSBVrnAHcOQMVhfXTb45cndXDIjM1n2SsAlwt07CGTpf+innezxJ8F5VkWv4 qnPooGA5VddA1ZKfP/j0iKQ+5SHHUOE7s8TTHVF+KcQXZayZSVLv/Zw/p6fUgyq6T6qC OL4PjgpqZfnDopwvYxe7zVXtZAUlL4oqeU2MnA2ILJfSNVRhzdIGhl5jgWI7OacVwYWi 2Gwg== 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=0rB5zy5FXc2Q0muVk5ePz1qdh/FmI9i/LZX5yd22+Lg=; fh=xHpaPA5ZJc5T7EPVqeKiF2P2YDMGoSaxO08sVU5vISA=; b=aso2n0IBo1odoYdJG9AYm20bM7K2ibbSz8SUf1QXCxoIwitbskNXrN53FdOH4h2ZDj Tc6tG8BcZYF9f1RkTvULT39O5yORU1dga9FUNw4XA7ZXITDahbedC1KvGx20+DzKDN2I 7sPHonIBIQJPFSTtpvmyb4vngDy0yQj205+I8S4Cz+JHYuh202jfBy88dRF2IVqvMteQ /H8lCBq2VVWE56IZDOBV+Ye6GlR71jyQR6zrWQLja3ho0W5KLWeU0J48TNFxuhbw8jEn a//c1UCyLwVz5wQHeXQgsQFE1ynjMQl06gcZ7FmgbsIJAWgAQ6kRI0aCUJRAZ6lYjY8w yDBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=KoXk7Z+f; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id x30-20020a63485e000000b005c21d7d1126si9828775pgk.843.2023.12.05.09.52.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 09:52:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=KoXk7Z+f; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (Postfix) with ESMTP id 9F5D2805194B; Tue, 5 Dec 2023 09:52:27 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232117AbjLERwJ (ORCPT + 99 others); Tue, 5 Dec 2023 12:52:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231933AbjLERwI (ORCPT ); Tue, 5 Dec 2023 12:52:08 -0500 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0AA6196 for ; Tue, 5 Dec 2023 09:52:13 -0800 (PST) Received: by mail-ed1-x529.google.com with SMTP id 4fb4d7f45d1cf-54c5d041c23so5702275a12.2 for ; Tue, 05 Dec 2023 09:52:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; t=1701798732; x=1702403532; 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=0rB5zy5FXc2Q0muVk5ePz1qdh/FmI9i/LZX5yd22+Lg=; b=KoXk7Z+fzqGYfn0O/Ypf7YmO1vLrTZpWIHq3YD4wIGsCsD54TaQAtavyZdLC6g7pbD zs7SBUJ+gBnZXER5o4oOHS+0MbFkf1z3hbFq2FwK7UglGcN2v7MfQ1xC5NM72ThXdeZG hWXBWI9Ydkrg0zkzQsOdgq8kTVU1+UltqnmvKZqAad5Q0VZyhJ9VswratbNswP/+BgZw 0KlZf3MznVvdOhjbGXvsAamQCO31R3R/0kbwE1hKGgDLt6KRb+jRjxU2RQNIKvk2WJpD 40YDijUpsApDKJrbdjEwsE31oDKv0qwlk5f2ndaPWexzfYjgEm5tmh+tkG3KA6rYlPDT otyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701798732; x=1702403532; 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=0rB5zy5FXc2Q0muVk5ePz1qdh/FmI9i/LZX5yd22+Lg=; b=rZpxlfaF8MLhBAgqptc+WXpYK/9WoJ/9URL2fWDo8gN1EKt/rEvsT2dM5LSa66igqT zVTgusBp6GI5OSgSxyw8di3Eh+6ge1URqMGnw5FmtkM5+wcpKG/aVF5zhKTxZaa2pkd9 O/YWoQ44sOvLyCu5tphZenMESj/uaL2foWZuMal1FYu6B4KivsFwa2BNSlvC6/wEkzTf vm2+kQBLBGm7eipFbW+zw6sSiX1G32orHBhXhp2HvMdZmBP1NhhPqU0ujpP48LJ8g3Rg 6U/WpTTZmuSepyRyx6B5XIuEcW989hwgI/LlZEaFUcJq6/oeOSEAdse76GlteABNiAKT Cv5A== X-Gm-Message-State: AOJu0YzEiSatyJ061ni2iZkC+4T/eQeMLCFoJSjYA0Upv9KylsixIDZD FFU35vzYpVigIXyc20OgKHrN0qsB3CZA8QfzWYgLeQ== X-Received: by 2002:a17:907:7632:b0:a18:7e19:5347 with SMTP id jy18-20020a170907763200b00a187e195347mr2274927ejc.47.1701798732114; Tue, 05 Dec 2023 09:52:12 -0800 (PST) MIME-Version: 1.0 References: <20231204201406.341074-1-khuey@kylehuey.com> <20231204201406.341074-3-khuey@kylehuey.com> In-Reply-To: From: Kyle Huey Date: Tue, 5 Dec 2023 09:52:00 -0800 Message-ID: Subject: Re: [PATCH 2/2] selftest/bpf: Test returning zero from a perf bpf program suppresses SIGIO. To: Yonghong Song Cc: Kyle Huey , linux-kernel@vger.kernel.org, "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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 morse.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 (morse.vger.email [0.0.0.0]); Tue, 05 Dec 2023 09:52:27 -0800 (PST) On Tue, Dec 5, 2023 at 8:54=E2=80=AFAM Yonghong Song wrote: > > > 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 C2= 3 extension [-Werror,-Wc23-extensions] Yeah, Meta's kernel-ci bot sent me off-list email about this one. > > 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 =3D 0; > > + struct test_perf_skip *skel =3D NULL; > > + int map_fd =3D -1; > > + long page_size =3D sysconf(_SC_PAGE_SIZE); > > + uintptr_t *ip =3D NULL; > > + int prog_fd =3D -1; > > + struct perf_event_attr attr =3D {0}; > > + int perf_fd =3D -1; > > + struct f_owner_ex owner; > > + int err; > > + > > + previous =3D signal(SIGIO, handle_sig); > > + > > + skel =3D test_perf_skip__open_and_load(); > > + if (!ASSERT_OK_PTR(skel, "skel_load")) > > + goto cleanup; > > + > > + prog_fd =3D bpf_program__fd(skel->progs.handler); > > + if (!ASSERT_OK(prog_fd < 0, "bpf_program__fd")) > > + goto cleanup; > > + > > + map_fd =3D bpf_map__fd(skel->maps.ip); > > + if (!ASSERT_OK(map_fd < 0, "bpf_map__fd")) > > + goto cleanup; > > + > > + ip =3D mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, = map_fd, 0); > > + if (!ASSERT_OK_PTR(ip, "mmap bpf map")) > > + goto cleanup; > > + > > + *ip =3D (uintptr_t)test_function; > > + > > + 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; > > + > > + perf_fd =3D 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 =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 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 =3D gettid(); > | ^ > > If you looked at some other examples, the common usage is do 'syscall(SYS= _gettid)'. Not clear why this works for me but sure I'll change that. > > So the following patch should fix the compilation error: > > #include > ... > owner.pid =3D syscall(SYS_gettid); > ... > > > + err =3D fcntl(perf_fd, F_SETOWN_EX, &owner); > > + if (!ASSERT_OK(err, "fcntl(F_SETOWN_EX)")) > > + goto cleanup; > > + > > + err =3D 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=3D1 make ... > > See https://lore.kernel.org/bpf/20231127050342.1945270-1-yonghong.song@li= nux.dev Not sure I follow this one. Are you saying adding asm volatile ("") in test_function() is *not* sufficient? - Kyle > > > + > > +cleanup: > > + if (perf_fd >=3D 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 =3D 0; > > + uintptr_t *v =3D bpf_map_lookup_elem(&ip, &index); > > + > > + return !(v && *v =3D=3D PT_REGS_IP(&data->regs)); > > +} > > + > > +char _license[] SEC("license") =3D "GPL";