From nobody Sun Feb 8 16:32:14 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2478EEB64D9 for ; Thu, 15 Jun 2023 13:37:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232615AbjFONhL (ORCPT ); Thu, 15 Jun 2023 09:37:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344389AbjFONgX (ORCPT ); Thu, 15 Jun 2023 09:36:23 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E0092D76 for ; Thu, 15 Jun 2023 06:35:13 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 1BF6D63C87 for ; Thu, 15 Jun 2023 13:34:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8888AC43391; Thu, 15 Jun 2023 13:34:18 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.96) (envelope-from ) id 1q9n7B-000Tsb-2D; Thu, 15 Jun 2023 09:34:17 -0400 Message-ID: <20230615133417.503900831@goodmis.org> User-Agent: quilt/0.66 Date: Thu, 15 Jun 2023 09:05:45 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Andrew Morton , Beau Belgrave Subject: [for-linus][PATCH 14/15] selftests/user_events: Adapt dyn_test to non-persist events References: <20230615130531.200384328@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Beau Belgrave Now that user_events does not honor persist events the dynamic_events file cannot be easily used to test parsing and matching cases. Update dyn_test to use the direct ABI file instead of dynamic_events so that we still have testing coverage until persist events and dynamic_events file integration has been decided. Link: https://lkml.kernel.org/r/20230614163336.5797-6-beaub@linux.microsoft= .com Signed-off-by: Beau Belgrave Signed-off-by: Steven Rostedt (Google) --- .../testing/selftests/user_events/dyn_test.c | 177 ++++++++++++++---- 1 file changed, 136 insertions(+), 41 deletions(-) diff --git a/tools/testing/selftests/user_events/dyn_test.c b/tools/testing= /selftests/user_events/dyn_test.c index 8879a7b04c6a..d6979a48478f 100644 --- a/tools/testing/selftests/user_events/dyn_test.c +++ b/tools/testing/selftests/user_events/dyn_test.c @@ -16,42 +16,140 @@ =20 #include "../kselftest_harness.h" =20 -const char *dyn_file =3D "/sys/kernel/tracing/dynamic_events"; -const char *clear =3D "!u:__test_event"; +const char *abi_file =3D "/sys/kernel/tracing/user_events_data"; +const char *enable_file =3D "/sys/kernel/tracing/events/user_events/__test= _event/enable"; =20 -static int Append(const char *value) +static bool wait_for_delete(void) { - int fd =3D open(dyn_file, O_RDWR | O_APPEND); - int ret =3D write(fd, value, strlen(value)); + int i; + + for (i =3D 0; i < 1000; ++i) { + int fd =3D open(enable_file, O_RDONLY); + + if (fd =3D=3D -1) + return true; + + close(fd); + usleep(1000); + } + + return false; +} + +static int reg_event(int fd, int *check, int bit, const char *value) +{ + struct user_reg reg =3D {0}; + + reg.size =3D sizeof(reg); + reg.name_args =3D (__u64)value; + reg.enable_bit =3D bit; + reg.enable_addr =3D (__u64)check; + reg.enable_size =3D sizeof(*check); + + if (ioctl(fd, DIAG_IOCSREG, ®) =3D=3D -1) + return -1; + + return 0; +} + +static int unreg_event(int fd, int *check, int bit) +{ + struct user_unreg unreg =3D {0}; + + unreg.size =3D sizeof(unreg); + unreg.disable_bit =3D bit; + unreg.disable_addr =3D (__u64)check; + + return ioctl(fd, DIAG_IOCSUNREG, &unreg); +} + +static int parse(int *check, const char *value) +{ + int fd =3D open(abi_file, O_RDWR); + int ret; + + if (fd =3D=3D -1) + return -1; + + /* Until we have persist flags via dynamic events, use the base name */ + if (value[0] !=3D 'u' || value[1] !=3D ':') { + close(fd); + return -1; + } + + ret =3D reg_event(fd, check, 31, value + 2); + + if (ret !=3D -1) { + if (unreg_event(fd, check, 31) =3D=3D -1) + printf("WARN: Couldn't unreg event\n"); + } =20 close(fd); + return ret; } =20 -#define CLEAR() \ +static int check_match(int *check, const char *first, const char *second, = bool *match) +{ + int fd =3D open(abi_file, O_RDWR); + int ret =3D -1; + + if (fd =3D=3D -1) + return -1; + + if (reg_event(fd, check, 31, first) =3D=3D -1) + goto cleanup; + + if (reg_event(fd, check, 30, second) =3D=3D -1) { + if (errno =3D=3D EADDRINUSE) { + /* Name is in use, with different fields */ + *match =3D false; + ret =3D 0; + } + + goto cleanup; + } + + *match =3D true; + ret =3D 0; +cleanup: + unreg_event(fd, check, 31); + unreg_event(fd, check, 30); + + close(fd); + + wait_for_delete(); + + return ret; +} + +#define TEST_MATCH(x, y) \ do { \ - int ret =3D Append(clear); \ - if (ret =3D=3D -1) \ - ASSERT_EQ(ENOENT, errno); \ + bool match; \ + ASSERT_NE(-1, check_match(&self->check, x, y, &match)); \ + ASSERT_EQ(true, match); \ } while (0) =20 -#define TEST_PARSE(x) \ +#define TEST_NMATCH(x, y) \ do { \ - ASSERT_NE(-1, Append(x)); \ - CLEAR(); \ + bool match; \ + ASSERT_NE(-1, check_match(&self->check, x, y, &match)); \ + ASSERT_EQ(false, match); \ } while (0) =20 -#define TEST_NPARSE(x) ASSERT_EQ(-1, Append(x)) +#define TEST_PARSE(x) ASSERT_NE(-1, parse(&self->check, x)) + +#define TEST_NPARSE(x) ASSERT_EQ(-1, parse(&self->check, x)) =20 FIXTURE(user) { + int check; }; =20 FIXTURE_SETUP(user) { - CLEAR(); } =20 FIXTURE_TEARDOWN(user) { - CLEAR(); + wait_for_delete(); } =20 TEST_F(user, basic_types) { @@ -95,33 +193,30 @@ TEST_F(user, size_types) { TEST_NPARSE("u:__test_event char a 20"); } =20 -TEST_F(user, flags) { - /* Should work */ - TEST_PARSE("u:__test_event:BPF_ITER u32 a"); - /* Forward compat */ - TEST_PARSE("u:__test_event:BPF_ITER,FLAG_FUTURE u32 a"); -} - TEST_F(user, matching) { - /* Register */ - ASSERT_NE(-1, Append("u:__test_event struct custom a 20")); - /* Should not match */ - TEST_NPARSE("!u:__test_event struct custom b"); - /* Should match */ - TEST_PARSE("!u:__test_event struct custom a"); - /* Multi field reg */ - ASSERT_NE(-1, Append("u:__test_event u32 a; u32 b")); - /* Non matching cases */ - TEST_NPARSE("!u:__test_event u32 a"); - TEST_NPARSE("!u:__test_event u32 b"); - TEST_NPARSE("!u:__test_event u32 a; u32 "); - TEST_NPARSE("!u:__test_event u32 a; u32 a"); - /* Matching case */ - TEST_PARSE("!u:__test_event u32 a; u32 b"); - /* Register */ - ASSERT_NE(-1, Append("u:__test_event u32 a; u32 b")); - /* Ensure trailing semi-colon case */ - TEST_PARSE("!u:__test_event u32 a; u32 b;"); + /* Single name matches */ + TEST_MATCH("__test_event u32 a", + "__test_event u32 a"); + + /* Multiple names match */ + TEST_MATCH("__test_event u32 a; u32 b", + "__test_event u32 a; u32 b"); + + /* Multiple names match with dangling ; */ + TEST_MATCH("__test_event u32 a; u32 b", + "__test_event u32 a; u32 b;"); + + /* Single name doesn't match */ + TEST_NMATCH("__test_event u32 a", + "__test_event u32 b"); + + /* Multiple names don't match */ + TEST_NMATCH("__test_event u32 a; u32 b", + "__test_event u32 b; u32 a"); + + /* Types don't match */ + TEST_NMATCH("__test_event u64 a; u64 b", + "__test_event u32 a; u32 b"); } =20 int main(int argc, char **argv) --=20 2.39.2