All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next v2 0/4] selftests/bpf: Add some tests for LoongArch
@ 2026-06-15 10:05 Tiezhu Yang
  2026-06-15 10:05 ` [PATCH bpf-next v2 1/4] selftests/bpf: Add get_preempt_count() support " Tiezhu Yang
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Tiezhu Yang @ 2026-06-15 10:05 UTC (permalink / raw)
  To: Alexei Starovoitov, Huacai Chen; +Cc: loongarch, bpf

This series is based on the latest master branch of bpf-next tree.
https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/

Dependency Note:
================
This series strictly depends on the following LoongArch core series,
which moves thread_info into task_struct:

"Move thread_info into task_struct for LoongArch"
https://lore.kernel.org/loongarch/20260612011616.27771-1-yangtiezhu@loongson.cn/

Because of this cross-tree dependency, these patches are based on the
bpf-next tree for review, but the actual merge timing and routing are
left to the discretion of the BPF and LoongArch maintainers.

Tiezhu Yang (4):
  selftests/bpf: Add get_preempt_count() support for LoongArch
  selftests/bpf: Add __arch_loongarch to limit test cases for LoongArch
  selftests/bpf: Test jited inline of bpf_get_current_task() for
    LoongArch
  selftests/bpf: Test jited inline of bpf_get_smp_processor_id() for
    LoongArch

 .../testing/selftests/bpf/bpf_experimental.h  | 23 +++++++++++++++++++
 tools/testing/selftests/bpf/progs/bpf_misc.h  |  1 +
 .../selftests/bpf/progs/verifier_jit_inline.c | 13 +++++++++++
 tools/testing/selftests/bpf/test_loader.c     |  5 ++++
 4 files changed, 42 insertions(+)

-- 
2.42.0


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

* [PATCH bpf-next v2 1/4] selftests/bpf: Add get_preempt_count() support for LoongArch
  2026-06-15 10:05 [PATCH bpf-next v2 0/4] selftests/bpf: Add some tests for LoongArch Tiezhu Yang
@ 2026-06-15 10:05 ` Tiezhu Yang
  2026-06-15 10:05 ` [PATCH bpf-next v2 2/4] selftests/bpf: Add __arch_loongarch to limit test cases " Tiezhu Yang
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Tiezhu Yang @ 2026-06-15 10:05 UTC (permalink / raw)
  To: Alexei Starovoitov, Huacai Chen; +Cc: loongarch, bpf

There is no LoongArch support for get_preempt_count() currently and its
fallback path always returns 0, just add it so that bpf_in_interrupt(),
bpf_in_nmi(), bpf_in_hardirq(), bpf_in_serving_softirq(), bpf_in_task()
work for LoongArch as well.

Use bpf_core_field_exists() to provide compatibility for both legacy and
future kernels. Additionally, a struct flavor "task_struct___local" is
introduced to bypass static compiler checks when thread_info is missing
from the base task_struct.

For the newer kernels that select CONFIG_THREAD_INFO_IN_TASK, it reads
preempt_count from the thread_info embedded within task_struct. For the
older kernels without selecting CONFIG_THREAD_INFO_IN_TASK, it retrieves
the thread_info pointer from the bottom of the kernel stack (task->stack)
and then reads the preempt_count. This ensures the BPF selftests remain
functional across various LoongArch kernel versions.

With this patch, "./test_progs -t exe_ctx" passes on LoongArch regardless
of whether CONFIG_THREAD_INFO_IN_TASK is set.

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 .../testing/selftests/bpf/bpf_experimental.h  | 23 +++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h
index d1db355e872b..1d0b29a9acb8 100644
--- a/tools/testing/selftests/bpf/bpf_experimental.h
+++ b/tools/testing/selftests/bpf/bpf_experimental.h
@@ -401,6 +401,12 @@ struct task_struct___preempt_rt {
 extern struct lowcore *bpf_get_lowcore(void) __weak __ksym;
 #endif
 
+#ifdef bpf_target_loongarch
+struct task_struct___local {
+	struct thread_info thread_info;
+} __attribute__((preserve_access_index));
+#endif
+
 static inline int get_preempt_count(void)
 {
 #if defined(bpf_target_x86)
@@ -423,6 +429,18 @@ static inline int get_preempt_count(void)
 	return bpf_get_current_task_btf()->thread_info.preempt_count;
 #elif defined(bpf_target_s390)
 	return bpf_get_lowcore()->preempt_count;
+#elif defined(bpf_target_loongarch)
+	struct task_struct *task = bpf_get_current_task_btf();
+	struct task_struct___local *task_alt = (void *)task;
+
+	if (bpf_core_field_exists(task_alt->thread_info)) {
+		return BPF_CORE_READ(task_alt, thread_info.preempt_count);
+	} else {
+		void *stack = BPF_CORE_READ(task, stack);
+		struct thread_info *ti = (void *)stack;
+
+		return BPF_CORE_READ(ti, preempt_count);
+	}
 #endif
 	return 0;
 }
@@ -433,6 +451,7 @@ static inline int get_preempt_count(void)
  *	* arm64
  *	* powerpc64
  *	* s390x
+ *	* loongarch
  */
 static inline int bpf_in_interrupt(void)
 {
@@ -454,6 +473,7 @@ static inline int bpf_in_interrupt(void)
  *	* arm64
  *	* powerpc64
  *	* s390x
+ *	* loongarch
  */
 static inline int bpf_in_nmi(void)
 {
@@ -466,6 +486,7 @@ static inline int bpf_in_nmi(void)
  *	* arm64
  *	* powerpc64
  *	* s390x
+ *	* loongarch
  */
 static inline int bpf_in_hardirq(void)
 {
@@ -478,6 +499,7 @@ static inline int bpf_in_hardirq(void)
  *	* arm64
  *	* powerpc64
  *	* s390x
+ *	* loongarch
  */
 static inline int bpf_in_serving_softirq(void)
 {
@@ -498,6 +520,7 @@ static inline int bpf_in_serving_softirq(void)
  *	* arm64
  *	* powerpc64
  *	* s390x
+ *	* loongarch
  */
 static inline int bpf_in_task(void)
 {
-- 
2.42.0


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

* [PATCH bpf-next v2 2/4] selftests/bpf: Add __arch_loongarch to limit test cases for LoongArch
  2026-06-15 10:05 [PATCH bpf-next v2 0/4] selftests/bpf: Add some tests for LoongArch Tiezhu Yang
  2026-06-15 10:05 ` [PATCH bpf-next v2 1/4] selftests/bpf: Add get_preempt_count() support " Tiezhu Yang
@ 2026-06-15 10:05 ` Tiezhu Yang
  2026-06-15 10:05 ` [PATCH bpf-next v2 3/4] selftests/bpf: Test jited inline of bpf_get_current_task() " Tiezhu Yang
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Tiezhu Yang @ 2026-06-15 10:05 UTC (permalink / raw)
  To: Alexei Starovoitov, Huacai Chen; +Cc: loongarch, bpf

Make it possible to limit certain tests to loongarch, just like it is
already done for x86_64, arm64, riscv64, and s390x.

This is a follow up patch of:

  commit ee7fe84468b1 ("selftests/bpf: __arch_* macro to limit test cases to specific archs")
  commit 1e4e6b9e260d ("selftests/bpf: Add __arch_s390x macro")

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 tools/testing/selftests/bpf/progs/bpf_misc.h | 1 +
 tools/testing/selftests/bpf/test_loader.c    | 5 +++++
 2 files changed, 6 insertions(+)

diff --git a/tools/testing/selftests/bpf/progs/bpf_misc.h b/tools/testing/selftests/bpf/progs/bpf_misc.h
index 9eeb5b0b63d6..b0c441384f20 100644
--- a/tools/testing/selftests/bpf/progs/bpf_misc.h
+++ b/tools/testing/selftests/bpf/progs/bpf_misc.h
@@ -158,6 +158,7 @@
 #define __arch_arm64		__arch("ARM64")
 #define __arch_riscv64		__arch("RISCV64")
 #define __arch_s390x		__arch("s390x")
+#define __arch_loongarch	__arch("LOONGARCH")
 #define __caps_unpriv(caps)	__test_tag("test_caps_unpriv=" EXPAND_QUOTE(caps))
 #define __load_if_JITed()	__test_tag("load_mode=jited")
 #define __load_if_no_JITed()	__test_tag("load_mode=no_jited")
diff --git a/tools/testing/selftests/bpf/test_loader.c b/tools/testing/selftests/bpf/test_loader.c
index abdb9e6e3713..3ce32d134e2c 100644
--- a/tools/testing/selftests/bpf/test_loader.c
+++ b/tools/testing/selftests/bpf/test_loader.c
@@ -377,6 +377,7 @@ enum arch {
 	ARCH_ARM64	= 0x4,
 	ARCH_RISCV64	= 0x8,
 	ARCH_S390X	= 0x10,
+	ARCH_LOONGARCH	= 0x20,
 };
 
 static int get_current_arch(void)
@@ -389,6 +390,8 @@ static int get_current_arch(void)
 	return ARCH_RISCV64;
 #elif defined(__s390x__)
 	return ARCH_S390X;
+#elif defined(__loongarch__)
+	return ARCH_LOONGARCH;
 #endif
 	return ARCH_UNKNOWN;
 }
@@ -580,6 +583,8 @@ static int parse_test_spec(struct test_loader *tester,
 				arch = ARCH_RISCV64;
 			} else if (strcmp(val, "s390x") == 0) {
 				arch = ARCH_S390X;
+			} else if (strcmp(val, "LOONGARCH") == 0) {
+				arch = ARCH_LOONGARCH;
 			} else {
 				PRINT_FAIL("bad arch spec: '%s'\n", val);
 				err = -EINVAL;
-- 
2.42.0


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

* [PATCH bpf-next v2 3/4] selftests/bpf: Test jited inline of bpf_get_current_task() for LoongArch
  2026-06-15 10:05 [PATCH bpf-next v2 0/4] selftests/bpf: Add some tests for LoongArch Tiezhu Yang
  2026-06-15 10:05 ` [PATCH bpf-next v2 1/4] selftests/bpf: Add get_preempt_count() support " Tiezhu Yang
  2026-06-15 10:05 ` [PATCH bpf-next v2 2/4] selftests/bpf: Add __arch_loongarch to limit test cases " Tiezhu Yang
@ 2026-06-15 10:05 ` Tiezhu Yang
  2026-06-15 10:25   ` sashiko-bot
  2026-06-15 10:05 ` [PATCH bpf-next v2 4/4] selftests/bpf: Test jited inline of bpf_get_smp_processor_id() " Tiezhu Yang
  2026-06-15 15:45 ` [PATCH bpf-next v2 0/4] selftests/bpf: Add some tests " Alexei Starovoitov
  4 siblings, 1 reply; 8+ messages in thread
From: Tiezhu Yang @ 2026-06-15 10:05 UTC (permalink / raw)
  To: Alexei Starovoitov, Huacai Chen; +Cc: loongarch, bpf

Add the JITed inline instruction of bpf_get_current_task() for LoongArch
to pass the test case.

Before:

  $ sudo ./test_progs -t verifier_jit_inline
  #604/1   verifier_jit_inline/inline_bpf_get_current_task:SKIP
  #604     verifier_jit_inline:SKIP
  Summary: 1/0 PASSED, 1 SKIPPED, 0 FAILED

After:

  $ sudo ./test_progs -t verifier_jit_inline
  #604/1   verifier_jit_inline/inline_bpf_get_current_task:OK
  #604     verifier_jit_inline:OK
  Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 tools/testing/selftests/bpf/progs/verifier_jit_inline.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tools/testing/selftests/bpf/progs/verifier_jit_inline.c b/tools/testing/selftests/bpf/progs/verifier_jit_inline.c
index 76d80605ec7f..8560577f2557 100644
--- a/tools/testing/selftests/bpf/progs/verifier_jit_inline.c
+++ b/tools/testing/selftests/bpf/progs/verifier_jit_inline.c
@@ -12,6 +12,8 @@ __arch_arm64
 __jited("	mrs	x8, SP_EL0")
 __arch_riscv64
 __jited("	mv	a5, tp")
+__arch_loongarch
+__jited("	move	$a5, $tp")
 int inline_bpf_get_current_task(void)
 {
 	bpf_get_current_task();
-- 
2.42.0


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

* [PATCH bpf-next v2 4/4] selftests/bpf: Test jited inline of bpf_get_smp_processor_id() for LoongArch
  2026-06-15 10:05 [PATCH bpf-next v2 0/4] selftests/bpf: Add some tests for LoongArch Tiezhu Yang
                   ` (2 preceding siblings ...)
  2026-06-15 10:05 ` [PATCH bpf-next v2 3/4] selftests/bpf: Test jited inline of bpf_get_current_task() " Tiezhu Yang
@ 2026-06-15 10:05 ` Tiezhu Yang
  2026-06-15 10:26   ` sashiko-bot
  2026-06-15 15:45 ` [PATCH bpf-next v2 0/4] selftests/bpf: Add some tests " Alexei Starovoitov
  4 siblings, 1 reply; 8+ messages in thread
From: Tiezhu Yang @ 2026-06-15 10:05 UTC (permalink / raw)
  To: Alexei Starovoitov, Huacai Chen; +Cc: loongarch, bpf

Add the testcase for the jited inline of bpf_get_smp_processor_id(),
only for LoongArch currently.

Here is the test result on LoongArch:

  $ sudo ./test_progs -t verifier_jit_inline
  #604/1   verifier_jit_inline/inline_bpf_get_current_task:OK
  #604/2   verifier_jit_inline/inline_bpf_get_smp_processor_id:OK
  #604     verifier_jit_inline:OK
  Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 .../testing/selftests/bpf/progs/verifier_jit_inline.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/tools/testing/selftests/bpf/progs/verifier_jit_inline.c b/tools/testing/selftests/bpf/progs/verifier_jit_inline.c
index 8560577f2557..02e562f56f9d 100644
--- a/tools/testing/selftests/bpf/progs/verifier_jit_inline.c
+++ b/tools/testing/selftests/bpf/progs/verifier_jit_inline.c
@@ -21,4 +21,15 @@ int inline_bpf_get_current_task(void)
 	return 0;
 }
 
+SEC("fentry/bpf_fentry_test2")
+__success __retval(0)
+__arch_loongarch
+__jited("	ld.wu	$a5, $tp, 16")
+int inline_bpf_get_smp_processor_id(void)
+{
+	bpf_get_smp_processor_id();
+
+	return 0;
+}
+
 char _license[] SEC("license") = "GPL";
-- 
2.42.0


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

* Re: [PATCH bpf-next v2 3/4] selftests/bpf: Test jited inline of bpf_get_current_task() for LoongArch
  2026-06-15 10:05 ` [PATCH bpf-next v2 3/4] selftests/bpf: Test jited inline of bpf_get_current_task() " Tiezhu Yang
@ 2026-06-15 10:25   ` sashiko-bot
  0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-06-15 10:25 UTC (permalink / raw)
  To: Tiezhu Yang; +Cc: bpf

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The selftest expects `bpf_get_current_task()` to be JIT-inlined to `move $a5, $tp` on LoongArch, but the required kernel JIT implementation is missing, causing the test to fail.
--

commit f8173ba138660b46dd1d6b8e58aa91f97cddc7a8
Author: Tiezhu Yang <yangtiezhu@loongson.cn>

selftests/bpf: Test jited inline of bpf_get_current_task() for LoongArch

This commit adds a selftest expectation for the JIT-inlined version of
bpf_get_current_task() on LoongArch.

> diff --git a/tools/testing/selftests/bpf/progs/verifier_jit_inline.c b/tools/testing/selftests/bpf/progs/verifier_jit_inline.c
> index 76d80605ec7fd..8560577f25576 100644
> --- a/tools/testing/selftests/bpf/progs/verifier_jit_inline.c
> +++ b/tools/testing/selftests/bpf/progs/verifier_jit_inline.c
> @@ -12,6 +12,8 @@ __arch_arm64
>  __jited("	mrs	x8, SP_EL0")
>  __arch_riscv64
>  __jited("	mv	a5, tp")
> +__arch_loongarch
> +__jited("	move	$a5, $tp")

[Severity: Medium]
Is the corresponding kernel patch implementing this JIT inline missing from
the tree?

Without the logic in arch/loongarch/net/bpf_jit.c to inline
BPF_FUNC_get_current_task, the BPF JIT emits a standard helper function
call rather than the expected instruction sequence.

Will this cause the selftest to unconditionally fail?

>  int inline_bpf_get_current_task(void)
>  {
>  	bpf_get_current_task();

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260615100506.19558-1-yangtiezhu@loongson.cn?part=3

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

* Re: [PATCH bpf-next v2 4/4] selftests/bpf: Test jited inline of bpf_get_smp_processor_id() for LoongArch
  2026-06-15 10:05 ` [PATCH bpf-next v2 4/4] selftests/bpf: Test jited inline of bpf_get_smp_processor_id() " Tiezhu Yang
@ 2026-06-15 10:26   ` sashiko-bot
  0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-06-15 10:26 UTC (permalink / raw)
  To: Tiezhu Yang; +Cc: bpf

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Broken selftest due to missing kernel JIT implementation and incorrect structural offset
--

commit 8f74b6910905428534e06ac6efaea6a8091a844f
Author: Tiezhu Yang <yangtiezhu@loongson.cn>

selftests/bpf: Test jited inline of bpf_get_smp_processor_id() for LoongArch

This commit adds a BPF selftest for LoongArch to verify that the
bpf_get_smp_processor_id() helper is correctly inlined by the JIT compiler.

> diff --git a/tools/testing/selftests/bpf/progs/verifier_jit_inline.c b/tools/testing/selftests/bpf/progs/verifier_jit_inline.c
> index 8560577f25576..02e562f56f9d3 100644
> --- a/tools/testing/selftests/bpf/progs/verifier_jit_inline.c
> +++ b/tools/testing/selftests/bpf/progs/verifier_jit_inline.c
> @@ -21,4 +21,15 @@ int inline_bpf_get_current_task(void)
>  	return 0;
>  }
>  
> +SEC("fentry/bpf_fentry_test2")
> +__success __retval(0)
> +__arch_loongarch
> +__jited("	ld.wu	$a5, $tp, 16")

[Severity: Medium]
Will this test fail in the current tree? It appears the kernel-side JIT 
implementation to inline this helper on LoongArch is missing. Without it, 
this test will expect an inline load but the JIT will generate a standard 
function call, causing the assertion to fail.

Also, is the offset 16 correct here? In the current tree's LoongArch layout,
struct thread_info contains a task pointer at offset 0, which means the cpu
field is at offset 24, not 16. The test appears to rely on an out-of-tree
configuration or prerequisite patch where the offset is 16.

> +int inline_bpf_get_smp_processor_id(void)
> +{
> +	bpf_get_smp_processor_id();
> +
> +	return 0;
> +}
> +
>  char _license[] SEC("license") = "GPL";

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260615100506.19558-1-yangtiezhu@loongson.cn?part=4

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

* Re: [PATCH bpf-next v2 0/4] selftests/bpf: Add some tests for LoongArch
  2026-06-15 10:05 [PATCH bpf-next v2 0/4] selftests/bpf: Add some tests for LoongArch Tiezhu Yang
                   ` (3 preceding siblings ...)
  2026-06-15 10:05 ` [PATCH bpf-next v2 4/4] selftests/bpf: Test jited inline of bpf_get_smp_processor_id() " Tiezhu Yang
@ 2026-06-15 15:45 ` Alexei Starovoitov
  4 siblings, 0 replies; 8+ messages in thread
From: Alexei Starovoitov @ 2026-06-15 15:45 UTC (permalink / raw)
  To: Tiezhu Yang, Alexei Starovoitov, Huacai Chen; +Cc: loongarch, bpf

On Mon Jun 15, 2026 at 3:05 AM PDT, Tiezhu Yang wrote:
> This series is based on the latest master branch of bpf-next tree.
> https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/
>
> Dependency Note:
> ================
> This series strictly depends on the following LoongArch core series,
> which moves thread_info into task_struct:
>
> "Move thread_info into task_struct for LoongArch"
> https://lore.kernel.org/loongarch/20260612011616.27771-1-yangtiezhu@loongson.cn/

Pls repost when this patch appears in bpf-next.

pw-bot: cr

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

end of thread, other threads:[~2026-06-15 15:45 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-15 10:05 [PATCH bpf-next v2 0/4] selftests/bpf: Add some tests for LoongArch Tiezhu Yang
2026-06-15 10:05 ` [PATCH bpf-next v2 1/4] selftests/bpf: Add get_preempt_count() support " Tiezhu Yang
2026-06-15 10:05 ` [PATCH bpf-next v2 2/4] selftests/bpf: Add __arch_loongarch to limit test cases " Tiezhu Yang
2026-06-15 10:05 ` [PATCH bpf-next v2 3/4] selftests/bpf: Test jited inline of bpf_get_current_task() " Tiezhu Yang
2026-06-15 10:25   ` sashiko-bot
2026-06-15 10:05 ` [PATCH bpf-next v2 4/4] selftests/bpf: Test jited inline of bpf_get_smp_processor_id() " Tiezhu Yang
2026-06-15 10:26   ` sashiko-bot
2026-06-15 15:45 ` [PATCH bpf-next v2 0/4] selftests/bpf: Add some tests " Alexei Starovoitov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.