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=1796104161; b=2KdaISFj/q9SKrQneCFjwUWRvkKHK0Dk+HHtl7x9GncvdXmkmddWRIIjnjjO1jjzyIOZ 6rJWVpuTLbyzektNIzykCyn1rnwtQZg/GoBxWxyAteBnminejqXdrHlKK2czLRU2/Qfyb gkM6d8IPZhCj1yB7fZyk+g9J4orZA256yxqvtkUoyNpt0EnDge4IZyR+SY8P0U/ZhadO6 hkPTkrtfw09SAFuL7+Tvy1XCNrKTqNX++SJuIXw9E2MpMFKQa3GW5ZrtUGB7f7Z00JAoD KUrsjgJq+ZQWcAOY9/ssv8KEFvA0w/GcuYZfUkf9QOieL8NrE5EshyR+8eQ3sEd0zkg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796104161; 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=u7AJx0slFVscDBhsbO7N3yUgLPy+z12+kGhw9/+b+jH4HCisr/8cGYDT1u73jABZcIV2 QZNwQT4TtRpLljY/MQlKQSCsc61cr83wKhpHCL4nILYP2p0mEMo7zBkLZ+28E9n7dswPu VaPPo0Rmr5Kso7XP8tSKFjVF+humGcDsNZOPGite7Sg4NHDgFmds8l9wIN8W6amXoUjcU I7l8e0c+M36A2GELIZqWOX2ba+beZ2W62An3BswU+vfIInU8yGih53Y+9FrQl2MYrN7Eo IVC9nn5jNvHfwnMPQzzv/lA/gBprWBJZa8mby+rOY2Pa3KgdYNRPixLqGyUqOIP7UTw== 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=USYkZheX; 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 1A4C7606A331 for ; Tue, 1 Dec 2026 05:49:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1A4C7606A331 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=USYkZheX 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=1796104160; x=2111464160; b=USYkZheXyno1Zu46MAr5CiqsDqov8WF4e13twWfEB/ZuzEk BJSQnPycnxUoeq5keK8Ni8PzZ/J1+gVSvd5zdb+HpaBPjVzlAw0/XS7G8e7sOU3y2m8ojBHII5rmK i7A/8R7koTrWnEcg8wcZxPjaySriLuE5bLG7EE4cH+hs5RJVF7aOBhlCVxNlg6IlmfKNU69YTO/Tr CwngSH6t6UfJcPcBE1UE4N8isSm6r+7brG0vFv6CzvaHRHiNqwbgL9LKz+bKoM01Dzb9VCBcsad4F xesbaZNIk9Pnq4jH/mWKw+l3BQniAmFcugljKcgfc/D2zL+TZKrWK3Uyo1KaKLaQ==; 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: sched-ext@lists.linux.dev 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