mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Changwoo Min <changwoo@igalia.com>
To: tj@kernel.org, void@manifault.com, arighi@nvidia.com,
	changwoo@igalia.com
Cc: david.dai@linux.dev, Gavin Guo <gavinguo@igalia.com>,
	kernel-dev@igalia.com, sched-ext@lists.linux.dev,
	linux-kernel@vger.kernel.org
Subject: [PATCH 1/5] sched_ext: Add scx_bpf_sched_id() and scx_bpf_task_sched_id()
Date: Tue,  1 Dec 2026 14:48:46 +0900	[thread overview]
Message-ID: <20261201054850.780664-2-changwoo@igalia.com> (raw)
In-Reply-To: <20261201054850.780664-1-changwoo@igalia.com>

A BPF scheduler can't tell which scheduler instance it is or which one
owns a task. With sub-schedulers, a task it reaches as a waker, from a
tracing program or by iteration may belong to another scheduler, and a
value that scheduler stored in the task means nothing to it.

Every scx_sched already has an id that starts at 1, increases
monotonically and is never reused. Add scx_bpf_sched_id(), the calling
scheduler's id, and scx_bpf_task_sched_id(), the id of the scheduler
that owns a task, both 0 for none. Any context may call them.

Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
 kernel/sched/ext/ext.c                   | 35 ++++++++++++++++++++++++
 tools/sched_ext/include/scx/compat.bpf.h | 22 +++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index a3cf580b0333..cdceeffbcc65 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -11933,6 +11933,39 @@ __bpf_kfunc s32 scx_bpf_task_cpu(const struct task_struct *p)
 	return task_cpu(p);
 }
 
+/**
+ * scx_bpf_sched_id - Id of the calling scheduler
+ * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
+ *
+ * Return the calling scheduler's instance id, 0 if there is none. Ids start at
+ * 1, increase monotonically and are never reused.
+ */
+__bpf_kfunc u64 scx_bpf_sched_id(const struct bpf_prog_aux *aux)
+{
+	struct scx_sched *sch;
+
+	guard(rcu)();
+	sch = scx_prog_sched(aux);
+	return sch ? sch->id : 0;
+}
+
+/**
+ * scx_bpf_task_sched_id - Id of the scheduler that owns a task
+ * @p: task of interest
+ *
+ * Return the instance id of the scheduler @p is on, see scx_bpf_sched_id(), 0
+ * if @p is not on any. Without the rq lock of @p, @p may move to another
+ * scheduler right after.
+ */
+__bpf_kfunc u64 scx_bpf_task_sched_id(struct task_struct *p)
+{
+	struct scx_sched *sch;
+
+	guard(rcu)();
+	sch = scx_task_sched_rcu(p);
+	return sch ? sch->id : 0;
+}
+
 /**
  * scx_bpf_task_cid - cid a task is currently associated with
  * @p: task of interest
@@ -12255,6 +12288,8 @@ BTF_ID_FLAGS(func, scx_bpf_get_online_cpumask, KF_ACQUIRE)
 BTF_ID_FLAGS(func, scx_bpf_put_cpumask, KF_RELEASE)
 BTF_ID_FLAGS(func, scx_bpf_task_running, KF_RCU)
 BTF_ID_FLAGS(func, scx_bpf_task_cpu, KF_RCU)
+BTF_ID_FLAGS(func, scx_bpf_sched_id, KF_IMPLICIT_ARGS)
+BTF_ID_FLAGS(func, scx_bpf_task_sched_id, KF_RCU)
 BTF_ID_FLAGS(func, scx_bpf_task_cid, KF_RCU)
 BTF_ID_FLAGS(func, scx_bpf_locked_rq, KF_IMPLICIT_ARGS | KF_RET_NULL)
 BTF_ID_FLAGS(func, scx_bpf_cpu_curr, KF_IMPLICIT_ARGS | KF_RET_NULL | KF_RCU_PROTECTED)
diff --git a/tools/sched_ext/include/scx/compat.bpf.h b/tools/sched_ext/include/scx/compat.bpf.h
index 3520a749f8ce..41882c6497ac 100644
--- a/tools/sched_ext/include/scx/compat.bpf.h
+++ b/tools/sched_ext/include/scx/compat.bpf.h
@@ -487,6 +487,28 @@ static inline void scx_bpf_task_set_lazy_resched(struct task_struct *p, bool laz
 		scx_bpf_task_set_lazy_resched___new(p, lazy);
 }
 
+/*
+ * v7.x: Add scx_bpf_sched_id() and scx_bpf_task_sched_id(). Ids start at 1,
+ * increase monotonically and are never reused. 0 means no scheduler, including
+ * on kernels without the kfuncs.
+ */
+u64 scx_bpf_sched_id___new(void) __ksym __weak;
+u64 scx_bpf_task_sched_id___new(struct task_struct *p) __ksym __weak;
+
+static inline u64 scx_bpf_sched_id(void)
+{
+	if (bpf_ksym_exists(scx_bpf_sched_id___new))
+		return scx_bpf_sched_id___new();
+	return 0;
+}
+
+static inline u64 scx_bpf_task_sched_id(struct task_struct *p)
+{
+	if (bpf_ksym_exists(scx_bpf_task_sched_id___new))
+		return scx_bpf_task_sched_id___new(p);
+	return 0;
+}
+
 /*
  * v7.1: New scx_bpf_dsq_reenq() that allows re-enqueues on more DSQs. This
  * will eventually deprecate scx_bpf_reenqueue_local().
-- 
2.56.1


  reply	other threads:[~2026-12-01  5:49 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  5:48 [PATCH 0/5] sched_ext: Add p->scx.bpf_data for O(1) task context lookup Changwoo Min
2026-12-01  5:48 ` Changwoo Min [this message]
2026-12-01  5:48 ` [PATCH 2/5] selftests/sched_ext: Test scx_bpf_sched_id() and scx_bpf_task_sched_id() Changwoo Min
2026-12-01  5:48 ` [PATCH 3/5] sched_ext: Add p->scx.bpf_data for O(1) task context lookup Changwoo Min
2026-12-01  7:48   ` Andrea Righi
2026-12-01 11:04     ` Changwoo Min
2026-12-01  5:48 ` [PATCH 4/5] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_data Changwoo Min
2026-12-01  5:48 ` [PATCH 5/5] selftests/sched_ext: Test p->scx.bpf_data across class changes Changwoo Min

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=20261201054850.780664-2-changwoo@igalia.com \
    --to=changwoo@igalia.com \
    --cc=arighi@nvidia.com \
    --cc=david.dai@linux.dev \
    --cc=gavinguo@igalia.com \
    --cc=kernel-dev@igalia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=tj@kernel.org \
    --cc=void@manifault.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®