Skip to contentSkip to conversation

LoongArch: BPF: Support 64-bit pointers to kfuncs

PATCH Superseded Hengqi Chen 2 participants 3 messages Dec 15, 2023 – Dec 22, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Hengqi Chen

author reviewer
#0 lore raw

PATCH 1/1 LoongArch: BPF: Support 64-bit pointers to kfuncs

Like commit 1cf3bfc60f98 ("bpf: Support 64-bit pointers to kfuncs") for s390x, add support for 64-bit pointers to kfuncs for LoongArch. Since the infrastructure is already implemented in BPF core, the only thing need to be done is to override bpf_jit_supports_far_kfunc_call().

Before this change, several test_verifier tests failed:

  # ./test_verifier | grep # | grep FAIL
  #119/p calls: invalid kfunc call: ptr_to_mem to struct with non-scalar FAIL
  #120/p calls: invalid kfunc call: ptr_to_mem to struct with nesting depth > 4 FAIL
  #121/p calls: invalid kfunc call: ptr_to_mem to struct with FAM FAIL
  #122/p calls: invalid kfunc call: reg->type != PTR_TO_CTX FAIL
  #123/p calls: invalid kfunc call: void * not allowed in func proto without mem size arg FAIL
  #124/p calls: trigger reg2btf_ids[reg->type] for reg->type > __BPF_REG_TYPE_MAX FAIL
  #125/p calls: invalid kfunc call: reg->off must be zero when passed to release kfunc FAIL
  #126/p calls: invalid kfunc call: don't match first member type when passed to release kfunc FAIL
  #127/p calls: invalid kfunc call: PTR_TO_BTF_ID with negative offset FAIL
  #128/p calls: invalid kfunc call: PTR_TO_BTF_ID with variable offset FAIL
  #129/p calls: invalid kfunc call: referenced arg needs refcounted PTR_TO_BTF_ID FAIL
  #130/p calls: valid kfunc call: referenced arg needs refcounted PTR_TO_BTF_ID FAIL
  #486/p map_kptr: ref: reference state created and released on xchg FAIL

This is because the kfuncs in the loaded module are far away from __bpf_call_base:

ffff800002009440 t bpf_kfunc_call_test_fail1 [bpf_testmod]
9000000002e128d8 T __bpf_call_base

The offset relative to __bpf_call_base does NOT fit in s32, which breaks the assumption in BPF core. Enable bpf_jit_supports_far_kfunc_call() lifts this limit.

With this change, the test_verifier tests now all passed:

  # ./test_verifier
  ...
  Summary: 777 PASSED, 0 SKIPPED, 0 FAILED
---
 arch/loongarch/net/bpf_jit.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/loongarch/net/bpf_jit.c +5 -0
index 4fcd6cd6da23..2f154c60ee00 100644
@@ -201,6 +201,11 @@ bool bpf_jit_supports_kfunc_call(void)
 	return true;
 }
 
+bool bpf_jit_supports_far_kfunc_call(void)
+{
+	return true;
+}
+
 /* initialized on the first pass of build_body() */
 static int out_offset = -1;
 static int emit_bpf_tail_call(struct jit_ctx *ctx)
Signature · 1 line
2.27.0
maintainer Tested-by on v1 1/1
#1 lore raw

Hi Hengqi,

Thanks for the patch, sorry for the late reply.

Quoted 8 lines · Hengqi Chen

Like commit 1cf3bfc60f98 ("bpf: Support 64-bit pointers to kfuncs") for s390x, add support for 64-bit pointers to kfuncs for LoongArch. Since the infrastructure is already implemented in BPF core, the only thing need to be done is to override bpf_jit_supports_far_kfunc_call().

Before this change, several test_verifier tests failed:

  # ./test_verifier | grep # | grep FAIL

[root@linux bpf]# ./test_verifier | grep # | grep FAIL
Usage: grep [OPTION]... PATTERNS [FILE]... Try 'grep --help' for more information.

It should be:
[root@linux bpf]# ./test_verifier | grep "#" | grep FAIL

Quoted 24 lines

#119/p calls: invalid kfunc call: ptr_to_mem to struct with non-scalar FAIL #120/p calls: invalid kfunc call: ptr_to_mem to struct with nesting depth > 4 FAIL #121/p calls: invalid kfunc call: ptr_to_mem to struct with FAM FAIL
#122/p calls: invalid kfunc call: reg->type != PTR_TO_CTX FAIL
#123/p calls: invalid kfunc call: void * not allowed in func proto without mem size arg FAIL #124/p calls: trigger reg2btf_ids[reg->type] for reg->type > __BPF_REG_TYPE_MAX FAIL #125/p calls: invalid kfunc call: reg->off must be zero when passed to release kfunc FAIL #126/p calls: invalid kfunc call: don't match first member type when passed to release kfunc FAIL #127/p calls: invalid kfunc call: PTR_TO_BTF_ID with negative offset FAIL
#128/p calls: invalid kfunc call: PTR_TO_BTF_ID with variable offset FAIL
#129/p calls: invalid kfunc call: referenced arg needs refcounted PTR_TO_BTF_ID FAIL #130/p calls: valid kfunc call: referenced arg needs refcounted PTR_TO_BTF_ID FAIL #486/p map_kptr: ref: reference state created and released on xchg FAIL

This is because the kfuncs in the loaded module are far away from __bpf_call_base:

ffff800002009440 t bpf_kfunc_call_test_fail1 [bpf_testmod]
9000000002e128d8 T __bpf_call_base

The offset relative to __bpf_call_base does NOT fit in s32, which breaks the assumption in BPF core. Enable bpf_jit_supports_far_kfunc_call() lifts this limit.

Quoted 5 lines

With this change, the test_verifier tests now all passed:

  # ./test_verifier
  ...
  Summary: 777 PASSED, 0 SKIPPED, 0 FAILED

I met build errors and failed/skipped tests at the beginning, so I spent some time to analysis the problems. In order to achieve success, it is necessary to do the following steps:

(1) build with loongson3_defconfig and also set
CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y
CONFIG_DEBUG_INFO_BTF=y
CONFIG_FTRACE_SYSCALLS=y
CONFIG_BPF_LSM=y

(2) enable bpf jit and enable unprivileged bpf before test echo 1 > /proc/sys/net/core/bpf_jit_enable
echo 0 > /proc/sys/kernel/unprivileged_bpf_disabled

Otherwise there exist build errors when execute

  make headers && cd tools/testing/selftests/bpf && make -j4

Furthermore, there exist failed test "#507/p sleepable lsm accept FAIL" and many skipped tests when execute "./test_verifier".

I think it would be best to add the above info in the commit message so that the users know how to test and how to solve or avoid the failures.

Quoted 2 lines

Feel free to add a

Thanks,
Tiezhu

author reviewer
#2 lore raw
Quoted 77 lines · Tiezhu Yang

Hi Hengqi,

Thanks for the patch, sorry for the late reply.

Quoted 8 lines · Hengqi Chen

Like commit 1cf3bfc60f98 ("bpf: Support 64-bit pointers to kfuncs") for s390x, add support for 64-bit pointers to kfuncs for LoongArch. Since the infrastructure is already implemented in BPF core, the only thing need to be done is to override bpf_jit_supports_far_kfunc_call().

Before this change, several test_verifier tests failed:

  # ./test_verifier | grep # | grep FAIL

[root@linux bpf]# ./test_verifier | grep # | grep FAIL
Usage: grep [OPTION]... PATTERNS [FILE]... Try 'grep --help' for more information.

It should be:
[root@linux bpf]# ./test_verifier | grep "#" | grep FAIL

Quoted 24 lines

#119/p calls: invalid kfunc call: ptr_to_mem to struct with non-scalar FAIL #120/p calls: invalid kfunc call: ptr_to_mem to struct with nesting depth > 4 FAIL #121/p calls: invalid kfunc call: ptr_to_mem to struct with FAM FAIL
#122/p calls: invalid kfunc call: reg->type != PTR_TO_CTX FAIL
#123/p calls: invalid kfunc call: void * not allowed in func proto without mem size arg FAIL #124/p calls: trigger reg2btf_ids[reg->type] for reg->type > __BPF_REG_TYPE_MAX FAIL #125/p calls: invalid kfunc call: reg->off must be zero when passed to release kfunc FAIL #126/p calls: invalid kfunc call: don't match first member type when passed to release kfunc FAIL #127/p calls: invalid kfunc call: PTR_TO_BTF_ID with negative offset FAIL
#128/p calls: invalid kfunc call: PTR_TO_BTF_ID with variable offset FAIL
#129/p calls: invalid kfunc call: referenced arg needs refcounted PTR_TO_BTF_ID FAIL #130/p calls: valid kfunc call: referenced arg needs refcounted PTR_TO_BTF_ID FAIL #486/p map_kptr: ref: reference state created and released on xchg FAIL

This is because the kfuncs in the loaded module are far away from __bpf_call_base:

ffff800002009440 t bpf_kfunc_call_test_fail1 [bpf_testmod]
9000000002e128d8 T __bpf_call_base

The offset relative to __bpf_call_base does NOT fit in s32, which breaks the assumption in BPF core. Enable bpf_jit_supports_far_kfunc_call() lifts this limit.

Quoted 5 lines

With this change, the test_verifier tests now all passed:

  # ./test_verifier
  ...
  Summary: 777 PASSED, 0 SKIPPED, 0 FAILED

I met build errors and failed/skipped tests at the beginning, so I spent some time to analysis the problems. In order to achieve success, it is necessary to do the following steps:

(1) build with loongson3_defconfig and also set
CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y
CONFIG_DEBUG_INFO_BTF=y
CONFIG_FTRACE_SYSCALLS=y
CONFIG_BPF_LSM=y

(2) enable bpf jit and enable unprivileged bpf before test echo 1 > /proc/sys/net/core/bpf_jit_enable
echo 0 > /proc/sys/kernel/unprivileged_bpf_disabled

Otherwise there exist build errors when execute

   make headers && cd tools/testing/selftests/bpf && make -j4

Furthermore, there exist failed test "#507/p sleepable lsm accept FAIL" and many skipped tests when execute "./test_verifier".

I think it would be best to add the above info in the commit message so that the users know how to test and how to solve or avoid the failures.

Quoted 1 line
Quoted 9 lines
Quoted 1 line

Feel free to add a

Thanks,
Tiezhu

Thanks, will send a v2 soon.