Skip to contentSkip to conversation

bpf: Add BPF CO-RE compatibility for older kernels

PATCH In review Wander Lairson Costa 2 participants 2 messages Nov 4, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Add BPF CO-RE compatibility for older kernels

Introduce a thread_info___legacy struct to provide BPF CO-RE compatibility for older kernels (e.g., RHEL 8.x with 4.18) where the thread_info struct lacks the cpu field. This allows bpf_core_field_exists() checks to correctly determine the availability of this field at runtime, preventing build failures on such systems.

The task_cpu helper function is updated to utilize this new legacy struct when accessing the cpu field from thread_info, ensuring that the BPF program can adapt to different kernel versions.

Cc Derek Barbosa <[email protected]>
Cc John Kacur <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Chunsheng Luo <[email protected]>
---
 bpf/stalld.bpf.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
bpf/stalld.bpf.c +12 -1
index b0a62ac..bf55f5b 100644
@@ -49,6 +49,16 @@ struct {
 #define log_task(p) log_task_prefix("", p)
 #define log_task_error(p) log_task_prefix("error: ", p)
 
+/*
+ * BPF CO-RE compatibility: In older kernels (e.g., RHEL 8.x with 4.18),
+ * thread_info lacks the cpu field. We define it here to enable
+ * bpf_core_field_exists() checks, allowing runtime detection of whether
+ * this field is available on the target kernel.
+ */
+struct thread_info___legacy {
+	int cpu;
+};
+
 /*
  * BPF CO-RE "weak" or "candidate" definition.
  *
@@ -105,10 +115,11 @@ static inline bool task_is_rt(const struct task_struct *p)
 static inline int task_cpu(const struct task_struct *p)
 {
 	const struct task_struct___legacy *lp = (const void *) p;
+	const struct thread_info___legacy *lt = (const void *) &p->thread_info;
 
 	return bpf_core_field_exists(lp->cpu)
 		? BPF_CORE_READ(lp, cpu)
-		: BPF_CORE_READ(p, thread_info.cpu);
+		: BPF_CORE_READ(lt, cpu);
 }
 
 /**
Signature · 1 line
2.51.1
#1 lore raw
Quoted 10 lines · Wander Lairson Costa

Introduce a thread_info___legacy struct to provide BPF CO-RE compatibility for older kernels (e.g., RHEL 8.x with 4.18) where the thread_info struct lacks the cpu field. This allows bpf_core_field_exists() checks to correctly determine the availability of this field at runtime, preventing build failures on such systems.

The task_cpu helper function is updated to utilize this new legacy struct when accessing the cpu field from thread_info, ensuring that the BPF program can adapt to different kernel versions.

Quoted 5 lines
Cc Derek Barbosa <[email protected]>
Cc John Kacur <[email protected]>
Cc Juri Lelli <[email protected]>
Cc Chunsheng Luo <[email protected]>

Applied to my devel branch along with the BPF verbose patch to the Makefile. I'm assembling the v1.25.1 release which will be these two changes and the current test-suite. Hopefully will go out today or tomorrow.

Clark

Signature · 2 lines
The United States Coast Guard
Ruining Natural Selection since 1790