selftests/bpf: Fix build of task_pt_regs test for arm64
Subsystems (3)
PATCH v2 1/1 selftests/bpf: Fix build of task_pt_regs test for arm64
struct pt_regs is not exported to userspace on all archs. arm64 and s390 export "user_pt_regs" instead, which causes build failure at the moment:
progs/test_task_pt_regs.c:8:16: error: variable has incomplete type 'struct pt_regs' struct pt_regs current_regs = {};
Instead of using pt_regs from ptrace.h, use the larger kernel struct from vmlinux.h directly. Since the test runner task_pt_regs.c does not have access to the kernel struct definition, copy it into a char array.
---
v2: Work on struct pt_regs from vmlinux.h
v1: https://lore.kernel.org/bpf/[email protected]/
--- .../selftests/bpf/prog_tests/task_pt_regs.c | 1 - .../selftests/bpf/progs/test_task_pt_regs.c | 19 +++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/prog_tests/task_pt_regs.c +0 -1
@@ -1,7 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #define _GNU_SOURCE#include <test_progs.h> -#include <linux/ptrace.h> #include "test_task_pt_regs.skel.h" void test_task_pt_regs (void )
tools/testing/selftests/bpf/progs/test_task_pt_regs.c +13 -6
@@ -1,12 +1,17 @@// SPDX-License-Identifier: GPL-2.0 -#include <linux/ptrace.h> -#include <linux/bpf.h> +#include "vmlinux.h" #include <bpf/bpf_helpers.h> #include <bpf/bpf_tracing.h> -struct pt_regs current_regs = {}; -struct pt_regs ctx_regs = {}; +#define PT_REGS_SIZE sizeof(struct pt_regs) + +/* +* The kernel struct pt_regs isn't exported in its entirety to userspace. +* Pass it as an array to task_pt_regs.c +*/ +char current_regs[PT_REGS_SIZE ] = {}; +char ctx_regs[PT_REGS_SIZE ] = {};int uprobe_res =0 ;SEC ("uprobe/trigger_func" )
@@ -17,8 +22,10 @@ int handle_uprobe(struct pt_regs *ctx) current =bpf_get_current_task_btf (); regs = (struct pt_regs *)bpf_task_pt_regs (current); -__builtin_memcpy (¤t_regs, regs,sizeof (*regs)); -__builtin_memcpy (&ctx_regs, ctx,sizeof (*ctx)); +if (bpf_probe_read_kernel (current_regs,PT_REGS_SIZE , regs)) +return 0 ; +if (bpf_probe_read_kernel (ctx_regs,PT_REGS_SIZE , ctx)) +return 0 ;/* Prove that uprobe was run */ uprobe_res =1 ;
Signature · 1 line
2.33.0
Quoted 71 lines · Jean-Philippe Brucker
struct
pt_regsis not exported to userspace on all archs. arm64 and s390
export "user_pt_regs" instead, which causes build failure at the moment:
progs/test_task_pt_regs.c:8:16: error: variable has incomplete type 'structpt_regs'
structpt_regscurrent_regs= {};Instead of using
pt_regsfromptrace.h, use the larger kernel struct fromvmlinux.hdirectly. Since the test runnertask_pt_regs.cdoes not have access to the kernel struct definition, copy it into a char array.Fixes 576d47bb1a92 ("bpf: selftests: Add bpf_task_pt_regs() selftest")Suggested-by Andrii Nakryiko <[email protected]>Signed-off-by Jean-Philippe Brucker <[email protected]>---
v2: Work on structpt_regsfromvmlinux.h
v1:
https://lore.kernel.org/bpf/[email protected]/
---
.../selftests/bpf/prog_tests/task_pt_regs.c | 1 - .../selftests/bpf/progs/test_task_pt_regs.c | 19 +++++++++++++------2 files changed, 13 insertions(+), 7 deletions(-)tools/testing/selftests/bpf/prog_tests/task_pt_regs.c +0
-1@@ -1,7 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #define _GNU_SOURCE #include <test_progs.h> -#include <linux/ptrace.h> #include "test_task_pt_regs.skel.h"
voidtest_task_pt_regs(void)tools/testing/selftests/bpf/progs/test_task_pt_regs.c +0
-0@@ -1,12 +1,17 @@ // SPDX-License-Identifier: GPL-2.0tools/testing/selftests/bpf/progs/test_task_pt_regs.c +1
-2-#include <linux/ptrace.h> -#include <linux/bpf.h> +#include "vmlinux.h" #include <
bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>tools/testing/selftests/bpf/progs/test_task_pt_regs.c +4
-2-struct pt_regs current_regs = {}; -struct pt_regs ctx_regs = {}; +#define PT_REGS_SIZE sizeof(struct pt_regs) + +/* +* The kernel struct pt_regs isn't exported in its entirety to userspace.
+ * Pass it as an array totask_pt_regs.c
+ */tools/testing/selftests/bpf/progs/test_task_pt_regs.c +2
-0+char current_regs[PT_REGS_SIZE ] = {}; +char ctx_regs[PT_REGS_SIZE ] = {};int
uprobe_res= 0;
SEC("uprobe/trigger_func")tools/testing/selftests/bpf/progs/test_task_pt_regs.c +0
-0@@ -17,8 +22,10 @@ int handle_uprobe(struct pt_regs *ctx)
current =bpf_get_current_task_btf();
regs = (structpt_regs*)bpf_task_pt_regs(current); - __builtin_memcpy(¤t_regs, regs,sizeof(*regs));
Quoted 8 lines
- __builtin_memcpy(&ctx_regs, ctx,
sizeof(*ctx));
+ if (bpf_probe_read_kernel(current_regs, PT_REGS_SIZE, regs)) + return 0;
+ if (bpf_probe_read_kernel(ctx_regs, PT_REGS_SIZE, ctx)) + return 0;
/* Prove that uprobe was run */
uprobe_res = 1;
I've tested this patch on s390 and it does indeed fix the build issue. Thanks!
This patch was applied to bpf/bpf.git (refs/heads/master):
Hello:
This patch was applied to bpf/bpf.git (refs/heads/master):
Quoted 11 lines · On Mon, 6 Sep 2021 17:36:38 +0100 you wrote:
struct
pt_regsis not exported to userspace on all archs. arm64 and s390 export "user_pt_regs" instead, which causes build failure at the moment:
progs/test_task_pt_regs.c:8:16: error: variable has incomplete type 'structpt_regs' structpt_regscurrent_regs= {};Instead of using
pt_regsfromptrace.h, use the larger kernel struct fromvmlinux.hdirectly. Since the test runnertask_pt_regs.cdoes not have access to the kernel struct definition, copy it into a char array.[...]
Here is the summary with links:
- [bpf-next,v2] selftests/bpf: Fix build of task_pt_regs test for arm64
https://git.kernel.org/bpf/bpf/c/3a029e1f3d6e
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html