LoongArch architecture development
 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; 6+ 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] 6+ 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; 6+ 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] 6+ 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; 6+ 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] 6+ 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: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, 0 replies; 6+ 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] 6+ 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 15:45 ` [PATCH bpf-next v2 0/4] selftests/bpf: Add some tests " Alexei Starovoitov
  4 siblings, 0 replies; 6+ 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] 6+ 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; 6+ 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] 6+ messages in thread

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

Thread overview: 6+ 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: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

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