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=1796104163; b=scffuh6se8g8b2DHDXmgtFV5iyDOPUXdInPU/dJAFqULEYngeXag0dhe6EeKUt8dvz7p Es6h0NZSDRX2EzC9nUDEnv8TtFYKIoGyyBxlJ1Rsy4Ybg+i1DLZcSugna83T2jSw4qC9u sDNaXrB8r1/1AybGuXBK/3GiUWWsxXzUS0etcraKGHNrBY1m/10AaFKdkuJo7LjftP+T2 o/Mc3nplD28/rs2J6jRpJIwU56DR2ooPRv4jZBIgS+27SftGrO624+rd8VP4nU4JYvZhM iQiDHl+fZ3FuOyDoqZ/ZT0TCK03j43WogjbX1aUtLOSC6ejrOun0gXxr3agt4/WxNkQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796104163; 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=NMp/v1H5g8VqOgHhPOiPgR4PX2E+I3JwOkDVk/WKqsk=; b=HyramYi9rmhxNFDaOP04UJ/UCRtl+AguRC4Wme1GqLve9tDpUytr285aSSder4EQf2zA cv2lo8C9mAy/k1vjR7cfFwQKu67kk4RjXKVpLcQYB3gSzCyIV+dQxPpPG/QwWnyYyk4Iw ZGefNQieu5ki5/kwGNpOnm8WQhVgfilEZA6X7lM/XwDmnJ/rttEjLLx9QIhVRXW7MuhlR FDdXtmUF+kDFUakfR7w9lfamSQGF8iHx/aA0AzQFu45CKLmdzqylegY+9TbaUSPKQQBds zyOPvoS/8P8bvHMsiuqS4ubI38xK/a7VnSOWpIlgMWkPPMoRl3w7Pu1+eDwzw5uAl+g== 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="A/9rtJhK"; 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 6FA70606A31A for ; Tue, 1 Dec 2026 05:49:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6FA70606A31A 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=A/9rtJhK 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=NMp/v1H5g8VqOgHhPOiPgR4PX2E+I3JwOkDVk/WKqsk=; t=1796104163; x=2111464163; b=A/9rtJhKWwroxb//DCYZtDWid4QBSfB9S5KDOSpPfs/uW8R 3wCyYxy7znTZk21pSB8Srx+Sa4Auu1ze9f5wB52St86/Ohuy3wKhsGke+B9rj4bwYcqNr3/urcVwe uGut4xJTyT/wfHzp/O3ERab1eRTCDJmDRzwA/vRfkymL0I37OM0dEjSGtgYioZ7LKzxoVO9KWNjyu rrAC6DmJyp/sZpEEfVx2k6tAu25hUPbO/2KRIT+pyUFJTp+138usnajQSryf10BdTZtno2fSHyWMs 6bGL+FnItofcxO4IToJyl325s1PGBbxA08DWQE1gqTGz0DRX+57oS8yN0a8Ae1iQ==; 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) id 1xYGju-00000000Ugc-0EYR; Tue, 01 Dec 2026 06:49:18 +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 4/5] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_data Date: Tue, 1 Dec 2026 14:48:49 +0900 Message-ID: <20261201054850.780664-5-changwoo@igalia.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261201054850.780664-1-changwoo@igalia.com> References: <20261201054850.780664-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_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 --- 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