From nobody Mon Oct 6 17:01:57 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 329F51FBE83; Sun, 20 Jul 2025 11:24:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753010670; cv=none; b=WmyzJFaF5rSzFXj7RlrvSAoCc9utJQs4slVTnzS2Ld70tvo7cQIzF1VPHaERrrPTAx2xNIiVkH9zeQDFRw92cJeR2eUaRZjpwnlK1ay6Bm3oxRU4V3o3L2TnctCWvpkRVe/VQ04QsdBK3yOA6SODenTqjI6rjPYKWCEn11mNGxE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753010670; c=relaxed/simple; bh=OvxjrrxQqzYo+k0s2QFU6ncZdHwpCTqeCjsz6CbBKLk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QyXkzyNY+YUA45maxS139q5N5Uva3oYspESHGUTOWSzwwHUUbCRRpjoWHbcaxZScHh1EWnIB+p6Dcc+eH0NL8XZeU7kU2lJP1dal0T/t+3dLb4f2qYoKgM636lUiE5YTzznkbs9mz8nwqgxC/qemAhumOAxFFf7/bs2rgfYxcQ0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=n3BONuLt; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="n3BONuLt" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 435DEC4CEE7; Sun, 20 Jul 2025 11:24:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1753010670; bh=OvxjrrxQqzYo+k0s2QFU6ncZdHwpCTqeCjsz6CbBKLk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n3BONuLthYm6gD1+Ht/nNsPrGJ+aCwmvCKy2bEjyZxMExcV5RO3sV3mR+pFCNwnUk zvgBauAW+ph75dA2cCdkgAyedwL+QRRL0i7fL5VdkaOb3ad8S3aZyMe1vPLguHiBoR SZz+XBlvd/4Q9/57RldnZas+Y5vyoiwZAolvUA/9EqFin+90XPKmXbH7NRk2yBXNXv TefN2A9IZoLmrjPWlBpVu6HefM1kiPMbXRoDI66tArvlbPUrvqgeSGtx3vdV02A+t7 pfBcZg0dNUroi9oT4JSZMmI07XFrQje8zCHOPxXkNvI97mnT0+LFxi+mPT2BvP61UQ RTcSKQgXLKX5A== From: Jiri Olsa To: Oleg Nesterov , Peter Zijlstra , Andrii Nakryiko Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, x86@kernel.org, Song Liu , Yonghong Song , John Fastabend , Hao Luo , Steven Rostedt , Masami Hiramatsu , Alan Maguire , David Laight , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Ingo Molnar Subject: [PATCHv6 perf/core 15/22] selftests/bpf: Add hit/attach/detach race optimized uprobe test Date: Sun, 20 Jul 2025 13:21:25 +0200 Message-ID: <20250720112133.244369-16-jolsa@kernel.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250720112133.244369-1-jolsa@kernel.org> References: <20250720112133.244369-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Adding test that makes sure parallel execution of the uprobe and attach/detach of optimized uprobe on it works properly. By default the test runs for 500ms, which is adjustable by using BPF_SELFTESTS_UPROBE_SYSCALL_RACE_MSEC env variable. Acked-by: Andrii Nakryiko Signed-off-by: Jiri Olsa --- .../selftests/bpf/prog_tests/uprobe_syscall.c | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c b/tool= s/testing/selftests/bpf/prog_tests/uprobe_syscall.c index b91135abcf8a..3d27c8bc019e 100644 --- a/tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c @@ -15,6 +15,7 @@ #include #include "uprobe_syscall.skel.h" #include "uprobe_syscall_executed.skel.h" +#include "bpf/libbpf_internal.h" =20 #define USDT_NOP .byte 0x0f, 0x1f, 0x44, 0x00, 0x00 #include "usdt.h" @@ -629,6 +630,111 @@ static void test_uretprobe_shadow_stack(void) ARCH_PRCTL(ARCH_SHSTK_DISABLE, ARCH_SHSTK_SHSTK); } =20 +static volatile bool race_stop; + +static USDT_DEFINE_SEMA(race); + +static void *worker_trigger(void *arg) +{ + unsigned long rounds =3D 0; + + while (!race_stop) { + uprobe_test(); + rounds++; + } + + printf("tid %d trigger rounds: %lu\n", gettid(), rounds); + return NULL; +} + +static void *worker_attach(void *arg) +{ + LIBBPF_OPTS(bpf_uprobe_opts, opts); + struct uprobe_syscall_executed *skel; + unsigned long rounds =3D 0, offset; + const char *sema[2] =3D { + __stringify(USDT_SEMA(race)), + NULL, + }; + unsigned long *ref; + int err; + + offset =3D get_uprobe_offset(&uprobe_test); + if (!ASSERT_GE(offset, 0, "get_uprobe_offset")) + return NULL; + + err =3D elf_resolve_syms_offsets("/proc/self/exe", 1, (const char **) &se= ma, &ref, STT_OBJECT); + if (!ASSERT_OK(err, "elf_resolve_syms_offsets_sema")) + return NULL; + + opts.ref_ctr_offset =3D *ref; + + skel =3D uprobe_syscall_executed__open_and_load(); + if (!ASSERT_OK_PTR(skel, "uprobe_syscall_executed__open_and_load")) + return NULL; + + skel->bss->pid =3D getpid(); + + while (!race_stop) { + skel->links.test_uprobe =3D bpf_program__attach_uprobe_opts(skel->progs.= test_uprobe, + 0, "/proc/self/exe", offset, &opts); + if (!ASSERT_OK_PTR(skel->links.test_uprobe, "bpf_program__attach_uprobe_= opts")) + break; + + bpf_link__destroy(skel->links.test_uprobe); + skel->links.test_uprobe =3D NULL; + rounds++; + } + + printf("tid %d attach rounds: %lu hits: %d\n", gettid(), rounds, skel->bs= s->executed); + uprobe_syscall_executed__destroy(skel); + free(ref); + return NULL; +} + +static useconds_t race_msec(void) +{ + char *env; + + env =3D getenv("BPF_SELFTESTS_UPROBE_SYSCALL_RACE_MSEC"); + if (env) + return atoi(env); + + /* default duration is 500ms */ + return 500; +} + +static void test_uprobe_race(void) +{ + int err, i, nr_threads; + pthread_t *threads; + + nr_threads =3D libbpf_num_possible_cpus(); + if (!ASSERT_GT(nr_threads, 0, "libbpf_num_possible_cpus")) + return; + nr_threads =3D max(2, nr_threads); + + threads =3D alloca(sizeof(*threads) * nr_threads); + if (!ASSERT_OK_PTR(threads, "malloc")) + return; + + for (i =3D 0; i < nr_threads; i++) { + err =3D pthread_create(&threads[i], NULL, i % 2 ? worker_trigger : worke= r_attach, + NULL); + if (!ASSERT_OK(err, "pthread_create")) + goto cleanup; + } + + usleep(race_msec() * 1000); + +cleanup: + race_stop =3D true; + for (nr_threads =3D i, i =3D 0; i < nr_threads; i++) + pthread_join(threads[i], NULL); + + ASSERT_FALSE(USDT_SEMA_IS_ACTIVE(race), "race_semaphore"); +} + static void __test_uprobe_syscall(void) { if (test__start_subtest("uretprobe_regs_equal")) @@ -647,6 +753,8 @@ static void __test_uprobe_syscall(void) test_uprobe_session(); if (test__start_subtest("uprobe_usdt")) test_uprobe_usdt(); + if (test__start_subtest("uprobe_race")) + test_uprobe_race(); } #else static void __test_uprobe_syscall(void) --=20 2.50.1