[RFC PATCH 6/7] sched/cache: Add BPF-facing hint submitters
From: Luo Gengkun
Date: Thu Nov 26 2026 - 08:16:50 EST
Expose the hint queue to BPF programs as kfuncs:
- scg_bpf_submit_hint(tid1, tid2) submits by vpid.
- scg_bpf_submit_hint_tsk(t1, t2) submits by task pointer.
Both pin the two tasks before submitting: the queue owns the references
from there, and the worker drops what it consumes. Register them for
the tracing, kprobe, tracepoint and syscall BPF program types. The
queue machinery itself is in the previous commit.
Signed-off-by: Luo Gengkun <luogengkun@xxxxxxxxxxxxxxx>
---
kernel/sched/build_utility.c | 3 ++
kernel/sched/cache_sched.c | 95 ++++++++++++++++++++++++++++++++++++
2 files changed, 98 insertions(+)
diff --git a/kernel/sched/build_utility.c b/kernel/sched/build_utility.c
index 5f63dbd43714..b4a72fd2311a 100644
--- a/kernel/sched/build_utility.c
+++ b/kernel/sched/build_utility.c
@@ -19,6 +19,9 @@
#include <linux/sched/rseq_api.h>
#include <linux/sched/task_stack.h>
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/btf_ids.h>
#include <linux/cpufreq.h>
#include <linux/cpumask_api.h>
#include <linux/cpuset.h>
diff --git a/kernel/sched/cache_sched.c b/kernel/sched/cache_sched.c
index fafb04bcdfa9..045c21e90505 100644
--- a/kernel/sched/cache_sched.c
+++ b/kernel/sched/cache_sched.c
@@ -753,6 +753,77 @@ static inline s32 scg_submit_hint_pinned(struct task_struct *t1,
return ret;
}
+__bpf_kfunc_start_defs();
+
+/*
+ * Submit a hint: two tasks, by vpid, are worth keeping in one cache group.
+ * The vpid is resolved in the caller's pid namespace here, because the
+ * worker consumes task pointers and the namespace context cannot shift
+ * between submit and flush.
+ */
+__bpf_kfunc s32 scg_bpf_submit_hint(u32 tid1, u32 tid2)
+{
+ struct task_struct *t1, *t2;
+
+ if (!sched_cache_enabled())
+ return -EOPNOTSUPP;
+
+ if (!tid1 || !tid2 || tid1 == tid2)
+ return 0;
+
+ t1 = find_get_task_by_vpid(tid1);
+ if (!t1)
+ return -ESRCH;
+
+ t2 = find_get_task_by_vpid(tid2);
+ if (!t2) {
+ put_task_struct(t1);
+ return -ESRCH;
+ }
+
+ return scg_submit_hint_pinned(t1, t2);
+}
+
+/*
+ * Submit a hint using two task pointers. The caller passes pointers that
+ * are valid in its context (an fentry/fexit probe argument, or
+ * bpf_get_current_task_btf()); they are pinned with tryget_task_struct()
+ * here, so the asynchronous worker can use them safely.
+ */
+__bpf_kfunc s32 scg_bpf_submit_hint_tsk(struct task_struct *t1,
+ struct task_struct *t2)
+{
+ if (!sched_cache_enabled())
+ return -EOPNOTSUPP;
+
+ if (!t1 || !t2 || t1 == t2)
+ return 0;
+
+ t1 = tryget_task_struct(t1);
+ if (!t1)
+ return -ESRCH;
+
+ t2 = tryget_task_struct(t2);
+ if (!t2) {
+ put_task_struct(t1);
+ return -ESRCH;
+ }
+
+ return scg_submit_hint_pinned(t1, t2);
+}
+
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(scg_kfunc_ids_hint)
+BTF_ID_FLAGS(func, scg_bpf_submit_hint)
+BTF_ID_FLAGS(func, scg_bpf_submit_hint_tsk)
+BTF_KFUNCS_END(scg_kfunc_ids_hint)
+
+static const struct btf_kfunc_id_set scg_kfunc_set_hint = {
+ .owner = THIS_MODULE,
+ .set = &scg_kfunc_ids_hint,
+};
+
static int scg_hint_should_run(unsigned int cpu)
{
return !llist_empty(&per_cpu_ptr(scg_hint_queues, cpu)->hints);
@@ -878,6 +949,30 @@ static int __init scg_hint_init(void)
return ret;
}
+ ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
+ &scg_kfunc_set_hint);
+ if (ret) {
+ /*
+ * A registered kfunc set cannot be unregistered, so there is
+ * nothing to unwind here either.
+ */
+ pr_err("sched_cache: Failed to register the hint kfuncs (%d)\n",
+ ret);
+ return ret;
+ }
+
+ /*
+ * The remaining types are best-effort: keep the working queues and
+ * carry on without the types that fail to register.
+ */
+ if (register_btf_kfunc_id_set(BPF_PROG_TYPE_KPROBE,
+ &scg_kfunc_set_hint) ||
+ register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACEPOINT,
+ &scg_kfunc_set_hint) ||
+ register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL,
+ &scg_kfunc_set_hint))
+ pr_err("sched_cache: some hint kfunc registrations failed\n");
+
return 0;
}
late_initcall(scg_hint_init);
--
2.34.1