public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next v3 0/3] bpf, x86: inline bpf_get_current_task() for x86_64
@ 2026-01-09  8:26 Menglong Dong
  2026-01-09  8:26 ` [PATCH bpf-next v3 1/3] " Menglong Dong
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Menglong Dong @ 2026-01-09  8:26 UTC (permalink / raw)
  To: ast, eddyz87
  Cc: daniel, john.fastabend, andrii, martin.lau, song, yonghong.song,
	kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel

Inline bpf_get_current_task() and bpf_get_current_task_btf() for x86_64
to obtain better performance, and add the testcase for it.

Changes since v2:
* implement it in the verifier with BPF_MOV64_PERCPU_REG() instead of in
  x86_64 JIT (Alexei).

Changes since v1:
* add the testcase
* remove the usage of const_current_task

Menglong Dong (3):
  bpf, x86: inline bpf_get_current_task() for x86_64
  selftests/bpf: add TEST_TAG_KCONFIG_CHECK to test_loader
  selftests/bpf: test the jited inline of bpf_get_current_task

 kernel/bpf/verifier.c                         | 25 ++++++++++
 .../selftests/bpf/prog_tests/verifier.c       |  2 +
 tools/testing/selftests/bpf/progs/bpf_misc.h  |  3 ++
 .../selftests/bpf/progs/verifier_jit_inline.c | 36 +++++++++++++++
 tools/testing/selftests/bpf/test_loader.c     | 46 ++++++++++++++++++-
 5 files changed, 111 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_jit_inline.c

-- 
2.52.0


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

* [PATCH bpf-next v3 1/3] bpf, x86: inline bpf_get_current_task() for x86_64
  2026-01-09  8:26 [PATCH bpf-next v3 0/3] bpf, x86: inline bpf_get_current_task() for x86_64 Menglong Dong
@ 2026-01-09  8:26 ` Menglong Dong
  2026-01-09  8:42   ` Menglong Dong
  2026-01-10  3:43   ` Menglong Dong
  2026-01-09  8:26 ` [PATCH bpf-next v3 2/3] selftests/bpf: add TEST_TAG_KCONFIG_CHECK to test_loader Menglong Dong
  2026-01-09  8:26 ` [PATCH bpf-next v3 3/3] selftests/bpf: test the jited inline of bpf_get_current_task Menglong Dong
  2 siblings, 2 replies; 6+ messages in thread
From: Menglong Dong @ 2026-01-09  8:26 UTC (permalink / raw)
  To: ast, eddyz87
  Cc: daniel, john.fastabend, andrii, martin.lau, song, yonghong.song,
	kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel

Inline bpf_get_current_task() and bpf_get_current_task_btf() for x86_64
to obtain better performance.

In !CONFIG_SMP case, the percpu variable is just a normal variable, and
we can read the current_task directly.

Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
---
v3:
- implement it in the verifier with BPF_MOV64_PERCPU_REG() instead of in
  x86_64 JIT.
---
 kernel/bpf/verifier.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 3d44c5d06623..520c413839ee 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -17688,6 +17688,8 @@ static bool verifier_inlines_helper_call(struct bpf_verifier_env *env, s32 imm)
 	switch (imm) {
 #ifdef CONFIG_X86_64
 	case BPF_FUNC_get_smp_processor_id:
+	case BPF_FUNC_get_current_task_btf:
+	case BPF_FUNC_get_current_task:
 		return env->prog->jit_requested && bpf_jit_supports_percpu_insn();
 #endif
 	default:
@@ -23273,6 +23275,24 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
 			insn      = new_prog->insnsi + i + delta;
 			goto next_insn;
 		}
+
+		/* Implement bpf_get_current_task() and bpf_get_current_task_btf() inline. */
+		if ((insn->imm == BPF_FUNC_get_current_task || insn->imm == BPF_FUNC_get_current_task_btf) &&
+		    verifier_inlines_helper_call(env, insn->imm)) {
+			insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, (u32)(unsigned long)&current_task);
+			insn_buf[1] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);
+			insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0);
+			cnt = 3;
+
+			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
+			if (!new_prog)
+				return -ENOMEM;
+
+			delta    += cnt - 1;
+			env->prog = prog = new_prog;
+			insn      = new_prog->insnsi + i + delta;
+			goto next_insn;
+		}
 #endif
 		/* Implement bpf_get_func_arg inline. */
 		if (prog_type == BPF_PROG_TYPE_TRACING &&
-- 
2.52.0


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

* [PATCH bpf-next v3 2/3] selftests/bpf: add TEST_TAG_KCONFIG_CHECK to test_loader
  2026-01-09  8:26 [PATCH bpf-next v3 0/3] bpf, x86: inline bpf_get_current_task() for x86_64 Menglong Dong
  2026-01-09  8:26 ` [PATCH bpf-next v3 1/3] " Menglong Dong
@ 2026-01-09  8:26 ` Menglong Dong
  2026-01-09  8:26 ` [PATCH bpf-next v3 3/3] selftests/bpf: test the jited inline of bpf_get_current_task Menglong Dong
  2 siblings, 0 replies; 6+ messages in thread
From: Menglong Dong @ 2026-01-09  8:26 UTC (permalink / raw)
  To: ast, eddyz87
  Cc: daniel, john.fastabend, andrii, martin.lau, song, yonghong.song,
	kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel

Add the __kconfig_check() to specify the kernel config for the test case.
The test case will be skipped if the specified Kconfig option is not
matched.

Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
---
 tools/testing/selftests/bpf/progs/bpf_misc.h |  3 ++
 tools/testing/selftests/bpf/test_loader.c    | 46 +++++++++++++++++++-
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/progs/bpf_misc.h b/tools/testing/selftests/bpf/progs/bpf_misc.h
index c9bfbe1bafc1..e230f135f580 100644
--- a/tools/testing/selftests/bpf/progs/bpf_misc.h
+++ b/tools/testing/selftests/bpf/progs/bpf_misc.h
@@ -129,6 +129,8 @@
  *
  * __linear_size     Specify the size of the linear area of non-linear skbs, or
  *                   0 for linear skbs.
+ *
+ * __kconfig_check   The test case is skipped if the specified Kconfig option is not set.
  */
 #define __msg(msg)		__attribute__((btf_decl_tag("comment:test_expect_msg=" XSTR(__COUNTER__) "=" msg)))
 #define __not_msg(msg)		__attribute__((btf_decl_tag("comment:test_expect_not_msg=" XSTR(__COUNTER__) "=" msg)))
@@ -163,6 +165,7 @@
 #define __stdout(msg)		__attribute__((btf_decl_tag("comment:test_expect_stdout=" XSTR(__COUNTER__) "=" msg)))
 #define __stdout_unpriv(msg)	__attribute__((btf_decl_tag("comment:test_expect_stdout_unpriv=" XSTR(__COUNTER__) "=" msg)))
 #define __linear_size(sz)	__attribute__((btf_decl_tag("comment:test_linear_size=" XSTR(sz))))
+#define __kconfig_check(config)	__attribute__((btf_decl_tag("comment:test_kconfig=" config)))
 
 /* Define common capabilities tested using __caps_unpriv */
 #define CAP_NET_ADMIN		12
diff --git a/tools/testing/selftests/bpf/test_loader.c b/tools/testing/selftests/bpf/test_loader.c
index 338c035c3688..a5fbd70e37d6 100644
--- a/tools/testing/selftests/bpf/test_loader.c
+++ b/tools/testing/selftests/bpf/test_loader.c
@@ -4,6 +4,9 @@
 #include <stdlib.h>
 #include <test_progs.h>
 #include <bpf/btf.h>
+#include <gelf.h>
+#include <zlib.h>
+#include <sys/utsname.h>
 
 #include "autoconf_helper.h"
 #include "disasm_helpers.h"
@@ -44,6 +47,7 @@
 #define TEST_TAG_EXPECT_STDOUT_PFX "comment:test_expect_stdout="
 #define TEST_TAG_EXPECT_STDOUT_PFX_UNPRIV "comment:test_expect_stdout_unpriv="
 #define TEST_TAG_LINEAR_SIZE "comment:test_linear_size="
+#define TEST_TAG_KCONFIG_CHECK "comment:test_kconfig="
 
 /* Warning: duplicated in bpf_misc.h */
 #define POINTER_VALUE	0xbadcafe
@@ -93,6 +97,7 @@ struct test_spec {
 	int linear_sz;
 	bool auxiliary;
 	bool valid;
+	bool skip;
 };
 
 static int tester_init(struct test_loader *tester)
@@ -394,6 +399,41 @@ static int get_current_arch(void)
 	return ARCH_UNKNOWN;
 }
 
+static int kconfig_check(const char *kconfig)
+{
+	int len, err = -ENOENT;
+	char buf[PATH_MAX];
+	struct utsname uts;
+	gzFile file;
+
+	uname(&uts);
+	len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release);
+	if (len < 0)
+		return -EINVAL;
+	else if (len >= PATH_MAX)
+		return -ENAMETOOLONG;
+
+	/* gzopen also accepts uncompressed files. */
+	file = gzopen(buf, "re");
+	if (!file)
+		file = gzopen("/proc/config.gz", "re");
+
+	if (!file) {
+		fprintf(stderr, "failed to open system Kconfig\n");
+		return -ENOENT;
+	}
+
+	while (gzgets(file, buf, sizeof(buf))) {
+		if (strstr(buf, kconfig)) {
+			err = 0;
+			break;
+		}
+	}
+
+	gzclose(file);
+	return err;
+}
+
 /* Uses btf_decl_tag attributes to describe the expected test
  * behavior, see bpf_misc.h for detailed description of each attribute
  * and attribute combinations.
@@ -650,6 +690,10 @@ static int parse_test_spec(struct test_loader *tester,
 				err = -EINVAL;
 				goto cleanup;
 			}
+		} else if (str_has_pfx(s, TEST_TAG_KCONFIG_CHECK)) {
+			val = s + sizeof(TEST_TAG_KCONFIG_CHECK) - 1;
+			if (kconfig_check(val))
+				spec->skip = true;
 		}
 	}
 
@@ -1151,7 +1195,7 @@ void run_subtest(struct test_loader *tester,
 	if (!test__start_subtest(subspec->name))
 		return;
 
-	if ((get_current_arch() & spec->arch_mask) == 0) {
+	if ((get_current_arch() & spec->arch_mask) == 0 || spec->skip) {
 		test__skip();
 		return;
 	}
-- 
2.52.0


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

* [PATCH bpf-next v3 3/3] selftests/bpf: test the jited inline of bpf_get_current_task
  2026-01-09  8:26 [PATCH bpf-next v3 0/3] bpf, x86: inline bpf_get_current_task() for x86_64 Menglong Dong
  2026-01-09  8:26 ` [PATCH bpf-next v3 1/3] " Menglong Dong
  2026-01-09  8:26 ` [PATCH bpf-next v3 2/3] selftests/bpf: add TEST_TAG_KCONFIG_CHECK to test_loader Menglong Dong
@ 2026-01-09  8:26 ` Menglong Dong
  2 siblings, 0 replies; 6+ messages in thread
From: Menglong Dong @ 2026-01-09  8:26 UTC (permalink / raw)
  To: ast, eddyz87
  Cc: daniel, john.fastabend, andrii, martin.lau, song, yonghong.song,
	kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel

Add the testcase for the jited inline of bpf_get_current_task().

Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
---
 .../selftests/bpf/prog_tests/verifier.c       |  2 ++
 .../selftests/bpf/progs/verifier_jit_inline.c | 36 +++++++++++++++++++
 2 files changed, 38 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_jit_inline.c

diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c
index 5829ffd70f8f..47eb78c808c0 100644
--- a/tools/testing/selftests/bpf/prog_tests/verifier.c
+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c
@@ -110,6 +110,7 @@
 #include "verifier_xdp_direct_packet_access.skel.h"
 #include "verifier_bits_iter.skel.h"
 #include "verifier_lsm.skel.h"
+#include "verifier_jit_inline.skel.h"
 #include "irq.skel.h"
 
 #define MAX_ENTRIES 11
@@ -251,6 +252,7 @@ void test_verifier_bits_iter(void) { RUN(verifier_bits_iter); }
 void test_verifier_lsm(void)                  { RUN(verifier_lsm); }
 void test_irq(void)			      { RUN(irq); }
 void test_verifier_mtu(void)		      { RUN(verifier_mtu); }
+void test_verifier_jit_inline(void)               { RUN(verifier_jit_inline); }
 
 static int init_test_val_map(struct bpf_object *obj, char *map_name)
 {
diff --git a/tools/testing/selftests/bpf/progs/verifier_jit_inline.c b/tools/testing/selftests/bpf/progs/verifier_jit_inline.c
new file mode 100644
index 000000000000..ba37c0841f1b
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_jit_inline.c
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+#if defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64)
+
+SEC("fentry/bpf_fentry_test1")
+__description("Jit inline, bpf_get_current_task")
+__success __retval(0)
+__arch_x86_64
+__jited("	addq	%gs:{{.*}}, %rax")
+__arch_arm64
+__jited("	mrs	x7, SP_EL0")
+__kconfig_check("CONFIG_SMP=y")
+int inline_bpf_get_current_task(void)
+{
+	bpf_get_current_task();
+
+	return 0;
+}
+
+#else
+
+SEC("kprobe")
+__description("Jit inline is not supported, use a dummy test")
+__success
+int dummy_test(void)
+{
+	return 0;
+}
+
+#endif
+
+char _license[] SEC("license") = "GPL";
-- 
2.52.0


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

* Re: [PATCH bpf-next v3 1/3] bpf, x86: inline bpf_get_current_task() for x86_64
  2026-01-09  8:26 ` [PATCH bpf-next v3 1/3] " Menglong Dong
@ 2026-01-09  8:42   ` Menglong Dong
  2026-01-10  3:43   ` Menglong Dong
  1 sibling, 0 replies; 6+ messages in thread
From: Menglong Dong @ 2026-01-09  8:42 UTC (permalink / raw)
  To: ast, eddyz87, Menglong Dong
  Cc: daniel, john.fastabend, andrii, martin.lau, song, yonghong.song,
	kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel

On 2026/1/9 16:26 Menglong Dong <menglong8.dong@gmail.com> write:
> Inline bpf_get_current_task() and bpf_get_current_task_btf() for x86_64
> to obtain better performance.
> 
> In !CONFIG_SMP case, the percpu variable is just a normal variable, and
> we can read the current_task directly.

This sentence is redundant, and should be remove :/

> 
> Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
> ---
> v3:
> - implement it in the verifier with BPF_MOV64_PERCPU_REG() instead of in
>   x86_64 JIT.
> ---
>  kernel/bpf/verifier.c | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
> 
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 3d44c5d06623..520c413839ee 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -17688,6 +17688,8 @@ static bool verifier_inlines_helper_call(struct bpf_verifier_env *env, s32 imm)
>  	switch (imm) {
>  #ifdef CONFIG_X86_64
>  	case BPF_FUNC_get_smp_processor_id:
> +	case BPF_FUNC_get_current_task_btf:
> +	case BPF_FUNC_get_current_task:
>  		return env->prog->jit_requested && bpf_jit_supports_percpu_insn();
>  #endif
>  	default:
> @@ -23273,6 +23275,24 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
>  			insn      = new_prog->insnsi + i + delta;
>  			goto next_insn;
>  		}
> +
> +		/* Implement bpf_get_current_task() and bpf_get_current_task_btf() inline. */
> +		if ((insn->imm == BPF_FUNC_get_current_task || insn->imm == BPF_FUNC_get_current_task_btf) &&
> +		    verifier_inlines_helper_call(env, insn->imm)) {
> +			insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, (u32)(unsigned long)&current_task);
> +			insn_buf[1] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);
> +			insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0);
> +			cnt = 3;
> +
> +			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
> +			if (!new_prog)
> +				return -ENOMEM;
> +
> +			delta    += cnt - 1;
> +			env->prog = prog = new_prog;
> +			insn      = new_prog->insnsi + i + delta;
> +			goto next_insn;
> +		}
>  #endif
>  		/* Implement bpf_get_func_arg inline. */
>  		if (prog_type == BPF_PROG_TYPE_TRACING &&
> -- 
> 2.52.0
> 
> 
> 





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

* Re: [PATCH bpf-next v3 1/3] bpf, x86: inline bpf_get_current_task() for x86_64
  2026-01-09  8:26 ` [PATCH bpf-next v3 1/3] " Menglong Dong
  2026-01-09  8:42   ` Menglong Dong
@ 2026-01-10  3:43   ` Menglong Dong
  1 sibling, 0 replies; 6+ messages in thread
From: Menglong Dong @ 2026-01-10  3:43 UTC (permalink / raw)
  To: ast, eddyz87, Menglong Dong
  Cc: daniel, john.fastabend, andrii, martin.lau, song, yonghong.song,
	kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel

On 2026/1/9 16:26, Menglong Dong wrote:
> Inline bpf_get_current_task() and bpf_get_current_task_btf() for x86_64
> to obtain better performance.
> 
> In !CONFIG_SMP case, the percpu variable is just a normal variable, and
> we can read the current_task directly.
> 
> Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
> ---
> v3:
> - implement it in the verifier with BPF_MOV64_PERCPU_REG() instead of in
>   x86_64 JIT.
> ---
>  kernel/bpf/verifier.c | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
> 
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 3d44c5d06623..520c413839ee 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -17688,6 +17688,8 @@ static bool verifier_inlines_helper_call(struct bpf_verifier_env *env, s32 imm)
>  	switch (imm) {
>  #ifdef CONFIG_X86_64
>  	case BPF_FUNC_get_smp_processor_id:
> +	case BPF_FUNC_get_current_task_btf:
> +	case BPF_FUNC_get_current_task:
>  		return env->prog->jit_requested && bpf_jit_supports_percpu_insn();
>  #endif
>  	default:
> @@ -23273,6 +23275,24 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
>  			insn      = new_prog->insnsi + i + delta;
>  			goto next_insn;
>  		}
> +
> +		/* Implement bpf_get_current_task() and bpf_get_current_task_btf() inline. */
> +		if ((insn->imm == BPF_FUNC_get_current_task || insn->imm == BPF_FUNC_get_current_task_btf) &&
> +		    verifier_inlines_helper_call(env, insn->imm)) {
> +			insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, (u32)(unsigned long)&current_task);

The !CONFIG_SMP case is still not handled properly here.
In the CONFIG_SMP case, I think &current_task is always
u32, so we can remove the casting. Therefore, this code
will be suitable for !CONFIG_SMP too.

> +			insn_buf[1] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);

In !CONFIG_SMP case, &current_task is a normal variable
pointer, and the BPF_MOV64_PERCPU_REG() will be ignored,
which makes the whole logic right.

I'll do more testing on it.

> +			insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0);
> +			cnt = 3;
> +
> +			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
> +			if (!new_prog)
> +				return -ENOMEM;
> +
> +			delta    += cnt - 1;
> +			env->prog = prog = new_prog;
> +			insn      = new_prog->insnsi + i + delta;
> +			goto next_insn;
> +		}
>  #endif
>  		/* Implement bpf_get_func_arg inline. */
>  		if (prog_type == BPF_PROG_TYPE_TRACING &&
> 





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

end of thread, other threads:[~2026-01-10  3:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-09  8:26 [PATCH bpf-next v3 0/3] bpf, x86: inline bpf_get_current_task() for x86_64 Menglong Dong
2026-01-09  8:26 ` [PATCH bpf-next v3 1/3] " Menglong Dong
2026-01-09  8:42   ` Menglong Dong
2026-01-10  3:43   ` Menglong Dong
2026-01-09  8:26 ` [PATCH bpf-next v3 2/3] selftests/bpf: add TEST_TAG_KCONFIG_CHECK to test_loader Menglong Dong
2026-01-09  8:26 ` [PATCH bpf-next v3 3/3] selftests/bpf: test the jited inline of bpf_get_current_task Menglong Dong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox