bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next v2 0/2] bpf, arm64: enable kfunc call
@ 2022-01-27  7:15 Hou Tao
  2022-01-27  7:15 ` [PATCH bpf-next v2 1/2] " Hou Tao
  2022-01-27  7:15 ` [PATCH bpf-next v2 2/2] selftests/bpf: check whether s32 is sufficient for kfunc offset Hou Tao
  0 siblings, 2 replies; 4+ messages in thread
From: Hou Tao @ 2022-01-27  7:15 UTC (permalink / raw)
  To: Daniel Borkmann
  Cc: Alexei Starovoitov, Martin KaFai Lau, Yonghong Song,
	Andrii Nakryiko, David S . Miller, Jakub Kicinski,
	John Fastabend, netdev, bpf, houtao1, Zi Shen Lim,
	Catalin Marinas, Will Deacon, Ard Biesheuvel, linux-arm-kernel

Hi,

The simple patchset tries to enable kfunc call for arm64. Patch #1 just
overrides bpf_jit_supports_kfunc_call() to enable kfunc call and patch #2
add a test to ensure s32 is sufficient for kfunc offset.

Change Log:
v2: 
  * add a test to check whether imm will be overflowed for kfunc call

v1: https://lore.kernel.org/bpf/20220119144942.305568-1-houtao1@huawei.com

Hou Tao (2):
  bpf, arm64: enable kfunc call
  selftests/bpf: check whether s32 is sufficient for kfunc offset

 arch/arm64/net/bpf_jit_comp.c                 |  5 ++
 .../selftests/bpf/prog_tests/ksyms_module.c   | 72 +++++++++++++++++++
 2 files changed, 77 insertions(+)

-- 
2.27.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH bpf-next v2 1/2] bpf, arm64: enable kfunc call
  2022-01-27  7:15 [PATCH bpf-next v2 0/2] bpf, arm64: enable kfunc call Hou Tao
@ 2022-01-27  7:15 ` Hou Tao
  2022-01-27  7:15 ` [PATCH bpf-next v2 2/2] selftests/bpf: check whether s32 is sufficient for kfunc offset Hou Tao
  1 sibling, 0 replies; 4+ messages in thread
From: Hou Tao @ 2022-01-27  7:15 UTC (permalink / raw)
  To: Daniel Borkmann
  Cc: Alexei Starovoitov, Martin KaFai Lau, Yonghong Song,
	Andrii Nakryiko, David S . Miller, Jakub Kicinski,
	John Fastabend, netdev, bpf, houtao1, Zi Shen Lim,
	Catalin Marinas, Will Deacon, Ard Biesheuvel, linux-arm-kernel

Since commit b2eed9b58811 ("arm64/kernel: kaslr: reduce module
randomization range to 2 GB"), for arm64 whether KASLR is enabled
or not, the module is placed within 2GB of the kernel region, so
s32 in bpf_kfunc_desc is sufficient to represente the offset of
module function relative to __bpf_call_base. The only thing needed
is to override bpf_jit_supports_kfunc_call().

Signed-off-by: Hou Tao <houtao1@huawei.com>
---
 arch/arm64/net/bpf_jit_comp.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
index 6d92f363028c..e60c464004c2 100644
--- a/arch/arm64/net/bpf_jit_comp.c
+++ b/arch/arm64/net/bpf_jit_comp.c
@@ -1284,6 +1284,11 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
 	return prog;
 }
 
+bool bpf_jit_supports_kfunc_call(void)
+{
+	return true;
+}
+
 u64 bpf_jit_alloc_exec_limit(void)
 {
 	return VMALLOC_END - VMALLOC_START;
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH bpf-next v2 2/2] selftests/bpf: check whether s32 is sufficient for kfunc offset
  2022-01-27  7:15 [PATCH bpf-next v2 0/2] bpf, arm64: enable kfunc call Hou Tao
  2022-01-27  7:15 ` [PATCH bpf-next v2 1/2] " Hou Tao
@ 2022-01-27  7:15 ` Hou Tao
  2022-01-28 14:42   ` Daniel Borkmann
  1 sibling, 1 reply; 4+ messages in thread
From: Hou Tao @ 2022-01-27  7:15 UTC (permalink / raw)
  To: Daniel Borkmann
  Cc: Alexei Starovoitov, Martin KaFai Lau, Yonghong Song,
	Andrii Nakryiko, David S . Miller, Jakub Kicinski,
	John Fastabend, netdev, bpf, houtao1, Zi Shen Lim,
	Catalin Marinas, Will Deacon, Ard Biesheuvel, linux-arm-kernel

In add_kfunc_call(), bpf_kfunc_desc->imm with type s32 is used to
represent the offset of called kfunc from __bpf_call_base, so
add a test to ensure that the offset will not be overflowed.

Signed-off-by: Hou Tao <houtao1@huawei.com>
---
 .../selftests/bpf/prog_tests/ksyms_module.c   | 72 +++++++++++++++++++
 1 file changed, 72 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/ksyms_module.c b/tools/testing/selftests/bpf/prog_tests/ksyms_module.c
index d490ad80eccb..ce0cd3446931 100644
--- a/tools/testing/selftests/bpf/prog_tests/ksyms_module.c
+++ b/tools/testing/selftests/bpf/prog_tests/ksyms_module.c
@@ -6,6 +6,76 @@
 #include "test_ksyms_module.lskel.h"
 #include "test_ksyms_module.skel.h"
 
+/* Most logic comes from bpf_object__read_kallsyms_file() */
+static int test_find_func_in_kallsyms(const char *func, unsigned long *addr)
+{
+	/* Same as KSYM_NAME_LEN */
+	char sym_name[128];
+	char sym_type;
+	unsigned long sym_addr;
+	int ret, err;
+	FILE *f;
+
+	f = fopen("/proc/kallsyms", "r");
+	if (!f)
+		return -errno;
+
+	err = -ENOENT;
+	while (true) {
+		ret = fscanf(f, "%lx %c %127s%*[^\n]\n",
+			     &sym_addr, &sym_type, sym_name);
+		if (ret == EOF && feof(f))
+			break;
+
+		if (ret != 3) {
+			err = -EINVAL;
+			break;
+		}
+
+		if ((sym_type == 't' || sym_type == 'T') &&
+		    !strcmp(sym_name, func)) {
+			*addr = sym_addr;
+			err = 0;
+			break;
+		}
+	}
+
+	fclose(f);
+	return err;
+}
+
+/*
+ * Check whether or not s32 in bpf_kfunc_desc is sufficient
+ * to represent the offset between bpf_testmod_test_mod_kfunc
+ * and __bpf_call_base.
+ */
+void test_ksyms_module_valid_offset(void)
+{
+	unsigned long kfunc_addr;
+	unsigned long base_addr;
+	int used_offset;
+	long actual_offset;
+	int err;
+
+	if (!env.has_testmod) {
+		test__skip();
+		return;
+	}
+
+	err = test_find_func_in_kallsyms("bpf_testmod_test_mod_kfunc",
+					 &kfunc_addr);
+	if (!ASSERT_OK(err, "find kfunc addr"))
+		return;
+
+	err = test_find_func_in_kallsyms("__bpf_call_base", &base_addr);
+	if (!ASSERT_OK(err, "find base addr"))
+		return;
+
+	used_offset = kfunc_addr - base_addr;
+	actual_offset = kfunc_addr - base_addr;
+	ASSERT_EQ((long)used_offset, actual_offset, "kfunc offset overflowed");
+}
+
 void test_ksyms_module_lskel(void)
 {
 	struct test_ksyms_module_lskel *skel;
@@ -55,6 +125,8 @@ void test_ksyms_module_libbpf(void)
 
 void test_ksyms_module(void)
 {
+	if (test__start_subtest("valid_offset"))
+		test_ksyms_module_valid_offset();
 	if (test__start_subtest("lskel"))
 		test_ksyms_module_lskel();
 	if (test__start_subtest("libbpf"))
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next v2 2/2] selftests/bpf: check whether s32 is sufficient for kfunc offset
  2022-01-27  7:15 ` [PATCH bpf-next v2 2/2] selftests/bpf: check whether s32 is sufficient for kfunc offset Hou Tao
@ 2022-01-28 14:42   ` Daniel Borkmann
  0 siblings, 0 replies; 4+ messages in thread
From: Daniel Borkmann @ 2022-01-28 14:42 UTC (permalink / raw)
  To: Hou Tao
  Cc: Alexei Starovoitov, Martin KaFai Lau, Yonghong Song,
	Andrii Nakryiko, David S . Miller, Jakub Kicinski,
	John Fastabend, netdev, bpf, Zi Shen Lim, Catalin Marinas,
	Will Deacon, Ard Biesheuvel, linux-arm-kernel

On 1/27/22 8:15 AM, Hou Tao wrote:
> In add_kfunc_call(), bpf_kfunc_desc->imm with type s32 is used to
> represent the offset of called kfunc from __bpf_call_base, so
> add a test to ensure that the offset will not be overflowed.
> 
> Signed-off-by: Hou Tao <houtao1@huawei.com>

Thanks for looking into this!

> ---
>   .../selftests/bpf/prog_tests/ksyms_module.c   | 72 +++++++++++++++++++
>   1 file changed, 72 insertions(+)
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/ksyms_module.c b/tools/testing/selftests/bpf/prog_tests/ksyms_module.c
> index d490ad80eccb..ce0cd3446931 100644
> --- a/tools/testing/selftests/bpf/prog_tests/ksyms_module.c
> +++ b/tools/testing/selftests/bpf/prog_tests/ksyms_module.c
> @@ -6,6 +6,76 @@
>   #include "test_ksyms_module.lskel.h"
>   #include "test_ksyms_module.skel.h"
>   
> +/* Most logic comes from bpf_object__read_kallsyms_file() */
> +static int test_find_func_in_kallsyms(const char *func, unsigned long *addr)
> +{
> +	/* Same as KSYM_NAME_LEN */
> +	char sym_name[128];
> +	char sym_type;
> +	unsigned long sym_addr;
> +	int ret, err;
> +	FILE *f;
> +
> +	f = fopen("/proc/kallsyms", "r");
> +	if (!f)
> +		return -errno;
> +
> +	err = -ENOENT;
> +	while (true) {
> +		ret = fscanf(f, "%lx %c %127s%*[^\n]\n",
> +			     &sym_addr, &sym_type, sym_name);
> +		if (ret == EOF && feof(f))
> +			break;
> +
> +		if (ret != 3) {
> +			err = -EINVAL;
> +			break;
> +		}
> +
> +		if ((sym_type == 't' || sym_type == 'T') &&
> +		    !strcmp(sym_name, func)) {
> +			*addr = sym_addr;
> +			err = 0;
> +			break;
> +		}
> +	}
> +
> +	fclose(f);
> +	return err;
> +}

Could we just reuse kallsyms_find() from trace_helpers.c which is also used
in couple of other prog_tests already?

> +
> +/*
> + * Check whether or not s32 in bpf_kfunc_desc is sufficient
> + * to represent the offset between bpf_testmod_test_mod_kfunc
> + * and __bpf_call_base.
> + */
> +void test_ksyms_module_valid_offset(void)
> +{
> +	unsigned long kfunc_addr;
> +	unsigned long base_addr;
> +	int used_offset;
> +	long actual_offset;
> +	int err;
> +
> +	if (!env.has_testmod) {
> +		test__skip();
> +		return;
> +	}
> +
> +	err = test_find_func_in_kallsyms("bpf_testmod_test_mod_kfunc",
> +					 &kfunc_addr);
> +	if (!ASSERT_OK(err, "find kfunc addr"))
> +		return;
> +
> +	err = test_find_func_in_kallsyms("__bpf_call_base", &base_addr);
> +	if (!ASSERT_OK(err, "find base addr"))
> +		return;
> +
> +	used_offset = kfunc_addr - base_addr;
> +	actual_offset = kfunc_addr - base_addr;
> +	ASSERT_EQ((long)used_offset, actual_offset, "kfunc offset overflowed");

Is the above also executed in case bpf_jit_supports_kfunc_call() falls back to
the default __weak callback, returning false? If yes, then the ASSERT_EQ() may
fail on archs like s390, ppc, etc where the offset may not be enough.

> +}
> +
>   void test_ksyms_module_lskel(void)
>   {
>   	struct test_ksyms_module_lskel *skel;
> @@ -55,6 +125,8 @@ void test_ksyms_module_libbpf(void)
>   
>   void test_ksyms_module(void)
>   {
> +	if (test__start_subtest("valid_offset"))
> +		test_ksyms_module_valid_offset();
>   	if (test__start_subtest("lskel"))
>   		test_ksyms_module_lskel();
>   	if (test__start_subtest("libbpf"))
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-01-28 14:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-27  7:15 [PATCH bpf-next v2 0/2] bpf, arm64: enable kfunc call Hou Tao
2022-01-27  7:15 ` [PATCH bpf-next v2 1/2] " Hou Tao
2022-01-27  7:15 ` [PATCH bpf-next v2 2/2] selftests/bpf: check whether s32 is sufficient for kfunc offset Hou Tao
2022-01-28 14:42   ` Daniel Borkmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).