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 4/5] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_data
Date: Tue,  1 Dec 2026 14:48:49 +0900	[thread overview]
Message-ID: <20261201054850.780664-5-changwoo@igalia.com> (raw)
In-Reply-To: <20261201054850.780664-1-changwoo@igalia.com>

Show how a scheduler uses p->scx.bpf_data. lookup_task_ctx() runs in most
qmap callbacks and costs a task storage lookup each time. qmap_enable()
now caches the task_ctx arena pointer in the field, and lookup_task_ctx()
takes it with a single load, falling back to task storage otherwise.

The field holds whatever the sched owning the task stored, and a qmap
sched also sees the tasks of its sub-scheds, e.g. when dumping. So the
cached value is tagged with scx_bpf_sched_id(): the upper 32 bits hold
the id with bit 31 set, the lower 32 bits the pointer's. Ids are never
reused, so values of other scheds, including unloaded ones, never match.
The pointer is rebuilt from the upper 32 bits all of the arena's pointers
share.

An arena pointer is a user space address, whose most significant bit is
always zero. Bit 31 of the tag is the most significant bit of the cached
value, and setting it to 1 makes the value an invalid user space address,
for foolproofing.

Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
 tools/sched_ext/scx_qmap.bpf.c | 58 ++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/tools/sched_ext/scx_qmap.bpf.c b/tools/sched_ext/scx_qmap.bpf.c
index 3951f1f6e3c6..079082711f69 100644
--- a/tools/sched_ext/scx_qmap.bpf.c
+++ b/tools/sched_ext/scx_qmap.bpf.c
@@ -9,6 +9,7 @@
  * - BPF arena for scheduler state.
  * - Core-sched support.
  * - Hierarchical sub-scheduling: delegating cpus to child cgroup schedulers.
+ * - Caching the per-task context in p->scx.bpf_data.
  *
  * scx_qmap is often used to exercise sched_ext features as they land. It is
  * coupled with the kernel it ships with and uses its sched_ext interfaces
@@ -256,12 +257,47 @@ static s32 pick_direct_dispatch_cid(struct task_struct *p, s32 prev_cid,
  */
 #define QMAP_TOUCH_ARENA() do { asm volatile("" :: "r"(&arena)); } while (0)
 
+/*
+ * p->scx.bpf_data caches @p's task_ctx so that lookup_task_ctx() takes a load
+ * instead of a task storage lookup. The field holds whatever the sched owning
+ * @p stored, and a qmap sched also sees tasks of its sub-scheds, e.g. in
+ * qmap_dump_task(). So the cached value carries this sched's id as a tag:
+ *
+ *   task_ctx_key
+ *     63-32  upper 32 bits of the arena's pointers
+ *     31     1, TASK_CTX_TAG_BIT
+ *     30-0   id from scx_bpf_sched_id(), 1 <= id < 2^31
+ *
+ *   p->scx.bpf_data
+ *     63     1, TASK_CTX_TAG_BIT
+ *     62-32  id
+ *     31-0   lower 32 bits of @p's task_ctx pointer
+ *
+ * The arena never crosses a 4G boundary, so all its pointers share the upper
+ * 32 bits. Ids are never reused, so a value left by another sched, including
+ * one that has since unloaded, never matches. An arena pointer is a user space
+ * address, whose most significant bit is always zero. Bit 31 of the tag is the
+ * most significant bit of p->scx.bpf_data, and setting it to 1 makes the
+ * cached value an invalid user space address, for foolproofing.
+ *
+ * qmap_enable() sets the value. The first call also sets task_ctx_key, which
+ * then never changes. The kernel clears the value when @p leaves this sched.
+ */
+#define TASK_CTX_TAG_BIT	(1U << 31)
+
+static u64 task_ctx_key;
+
 static task_ctx_t *lookup_task_ctx(struct task_struct *p)
 {
 	struct task_ctx_stor_val *v;
+	u64 key = READ_ONCE(task_ctx_key);
+	u64 data = READ_ONCE(p->scx.bpf_data);
 
 	QMAP_TOUCH_ARENA();
 
+	if (key && (u32)(data >> 32) == (u32)key)
+		return (task_ctx_t *)((key & ~0xffffffffULL) | (u32)data);
+
 	v = bpf_task_storage_get(&task_ctx_stor, p, 0, 0);
 	if (!v || !v->taskc)
 		return NULL;
@@ -1062,6 +1098,27 @@ s32 BPF_STRUCT_OPS_SLEEPABLE(qmap_init_task, struct task_struct *p,
 	return 0;
 }
 
+void BPF_STRUCT_OPS(qmap_enable, struct task_struct *p,
+		    struct scx_enable_args *args)
+{
+	task_ctx_t *taskc = lookup_task_ctx(p);
+	u64 key = READ_ONCE(task_ctx_key);
+	u64 id;
+
+	if (!taskc)
+		return;
+
+	if (!key) {
+		id = scx_bpf_sched_id();
+		if (!id || id >= TASK_CTX_TAG_BIT)
+			return;
+		key = ((u64)taskc & ~0xffffffffULL) | TASK_CTX_TAG_BIT | (u32)id;
+		WRITE_ONCE(task_ctx_key, key);
+	}
+
+	scx_bpf_task_set_data(p, (u64)(u32)key << 32 | (u32)(u64)taskc);
+}
+
 void BPF_STRUCT_OPS(qmap_exit_task, struct task_struct *p,
 		    struct scx_exit_task_args *args)
 {
@@ -2252,6 +2309,7 @@ SCX_OPS_CID_DEFINE(qmap_ops,
 	       .set_cmask		= (void *)qmap_set_cmask,
 	       .update_idle		= (void *)qmap_update_idle,
 	       .init_task		= (void *)qmap_init_task,
+	       .enable			= (void *)qmap_enable,
 	       .exit_task		= (void *)qmap_exit_task,
 	       .dump			= (void *)qmap_dump,
 	       .dump_cid		= (void *)qmap_dump_cid,
-- 
2.56.1


  parent 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 ` [PATCH 1/5] sched_ext: Add scx_bpf_sched_id() and scx_bpf_task_sched_id() Changwoo Min
2026-12-01  5:48 ` [PATCH 2/5] selftests/sched_ext: Test " 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 ` Changwoo Min [this message]
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-5-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®