From: Luo Gengkun <luogengkun@huaweicloud.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Juri Lelli <juri.lelli@redhat.com>,
Vincent Guittot <vincent.guittot@linaro.org>,
Tim Chen <tim.c.chen@linux.intel.com>,
Chen Yu <yu.c.chen@intel.com>,
K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Dietmar Eggemann <dietmar.eggemann@arm.com>,
Steven Rostedt <rostedt@goodmis.org>,
Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
Valentin Schneider <vschneid@redhat.com>,
linux-kernel@vger.kernel.org
Subject: [RFC PATCH 6/7] sched/cache: Add BPF-facing hint submitters
Date: Thu, 26 Nov 2026 13:48:27 +0000 [thread overview]
Message-ID: <e01c1e6d627ceec97664ee5a0bb2c7a1d76dbe9d.1795691926.git.luogengkun2@huawei.com> (raw)
In-Reply-To: <cover.1795691926.git.luogengkun2@huawei.com>
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@huaweicloud.com>
---
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
next prev parent reply other threads:[~2026-11-26 13:16 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 13:48 [RFC PATCH 0/7] sched/cache: Introduce per-task affinity group via BPF detector Luo Gengkun
2026-11-26 13:48 ` [RFC PATCH 1/7] sched/cache: Move the cache group lifecycle to cache_sched.c Luo Gengkun
2026-11-26 13:48 ` [RFC PATCH 2/7] sched/cache: Add thread count to cache group Luo Gengkun
2026-11-26 13:48 ` [RFC PATCH 3/7] sched/cache: Introduce the task affinity group Luo Gengkun
2026-11-26 13:48 ` [RFC PATCH 4/7] sched/cache: Converge task accounting to the affinity root Luo Gengkun
2026-11-26 13:48 ` [RFC PATCH 5/7] sched/cache: Add the per-CPU hint queue Luo Gengkun
2026-11-26 13:48 ` Luo Gengkun [this message]
2026-11-26 13:48 ` [RFC PATCH 7/7] samples/bpf: Add pipe-based cache hint example Luo Gengkun
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e01c1e6d627ceec97664ee5a0bb2c7a1d76dbe9d.1795691926.git.luogengkun2@huawei.com \
--to=luogengkun@huaweicloud.com \
--cc=bsegall@google.com \
--cc=dietmar.eggemann@arm.com \
--cc=juri.lelli@redhat.com \
--cc=kprateek.nayak@amd.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=tim.c.chen@linux.intel.com \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
--cc=yu.c.chen@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®