From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=213.97.179.56 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796273819; b=VAxsriDFBHN6GY5OxTGN1jb3v97u7RXpNJ9uDoPG4lsC0097ylpg9zuB1rovY+etUUlZ NM6r6pg90/UuKJ9lCPW5rB1dfVPC5ObTL9V8jU8TWKCX7R7d93xnlLQlvWo84kvG5NhrU vGp/9NCa6NBx2wnwK5UoSNHRAsowsDLifTCGtP/DsfyEDdrT9Fom5gZ/R3Kkg/xQuLECt 02F+o6AGZR4ICT7elBE+cenawdNyDAum1u9d8zSW7bAIafRDQUhPZCsoEVb3ZdR4ZUfvE 6UgfdTmIfHXN0cIGDbF/N234hPnBl6Ut6qRq9ggxkHqoGXaAuGidM21se7wPyd0T9bg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796273819; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=VSJ66CeHTsVaE7IuaVlbj1oPaz+jlfIIQXYKDQxhV/I=; b=Ka3ddd3yYNa5Pzdu52o0cKsCFUIGtQA4F5l+PEo7wc5/ycrWCYbCyMrV41Vh12DhUfw8 tlAqPBzSBiB7cWFi+5dE9+tooG0dA+zIJzRy0phRRBUHL/Ew/wwwH/vlLgMuN1KppalAy XxXnjX8EDm7qb8WaC0u2v9dgXL1kP1hDr74i3e20gpCwRqaEv9VDHyGv3bnhh+1FR3Rtb MshgVJZknH90OoaDAmfFEVujA1UBsopm75i6gTCuKU38cah3ltiwZzNM25I1O+1zpAlCM rzbdsqpqmYbaGD5ntjPEqOSyllhxGruEBH1Iikahgyjxxn548KswIg3cmqWquongAnw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=kGYIA5Lv; arc=none smtp.remote-ip=213.97.179.56 Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13B47606A322 for ; Thu, 3 Dec 2026 04:56:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 13B47606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=kGYIA5Lv DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject: Cc:To:From:From:Reply-To; bh=VSJ66CeHTsVaE7IuaVlbj1oPaz+jlfIIQXYKDQxhV/I=; t=1796273818; x=2111633818; b=kGYIA5Lvz2UydISirwdAwm9x6EllEc/nB36rTL2iXJx90qE zn32JXDvh/6DVcjEj3WjNqaCpFc9sXDnJ5pY6AV5BHsr+FTMg3JesbphgQK2l6jT1/kBKgL9D2F7g 7CeXOemvhJk8kQUIUTF8hugWLuOX91ujQmh//9EmQc/EjGRgvT+gCPanCWXFv/hYtd6lvmzarvyqr 1H+YOS7jTxYEhPiN9KPoA7jOaHZ8hgaiNDxqtR0leOTX9soBgd6NPtVdovUwqsf1YPv34lWUUcbX1 BbQPWiXUKHmJViCUQMzcckdtLb4NkjTl6w+FdlbvZJOZC6PUXNktoa54Ojl87Zbw==; Received: from [58.29.145.179] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) (envelope-from ) id 1xYysI-00000001Tya-2YTr; Thu, 03 Dec 2026 05:56:55 +0100 From: Changwoo Min To: tj@kernel.org, void@manifault.com, arighi@nvidia.com, changwoo@igalia.com Cc: david.dai@linux.dev, Gavin Guo , kernel-dev@igalia.com, sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v2 6/8] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_priv Date: Thu, 3 Dec 2026 13:56:16 +0900 Message-ID: <20261203045618.982539-7-changwoo@igalia.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261203045618.982539-1-changwoo@igalia.com> References: <20261203045618.982539-1-changwoo@igalia.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Show how a scheduler uses p->scx.bpf_priv. 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 --- 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..8fa6626d6598 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_priv. * * 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_priv 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_priv + * 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_priv, 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 priv = READ_ONCE(p->scx.bpf_priv); QMAP_TOUCH_ARENA(); + if (key && (u32)(priv >> 32) == (u32)key) + return (task_ctx_t *)((key & ~0xffffffffULL) | (u32)priv); + 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_priv(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