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=1796535480; b=NSAn8cQLLgOOBWvkgAVcYshr5N4cF91JEKWhYu8bBaXxBXSCsRYqUwPAEhdBlZHjPpwQ VAHfwTEUmKBfcZ2P19NeESYSJxfrCXLaMnScwd/nKxWMO0Ce6zWcIaGx3eiIZLbeKvB/m GtbAOTD/1jSOBdnY4rMfpbYtZQepogruVykq8TuowuZbdf12YuU0BwOmVVhjYKnDpsDad l6N+SbIOf4cdF+5+AGs3Bsdq5i/DlgaSefROn14HST/7qIGQFrOuBPfWKgnqSbuPK1GN8 v6OZD6SAKM/0KWBtNVBeDP0p6m2vcNj150eB/eRA6051dqTQ8XM5fpaTviw+FhU8wXQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796535480; 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=GH9sAEkOULiwE+WrnccPwRvw1wZumwUkQspcI5ewFO4=; b=tsEp4YTOG9eXpqPixZ93yPrIZcYP4Z/cFRH2PdpNl+nr34zCQjG2KG045zVP3qbhGX+o WNpPQ9RZ+7I/kvTTk8WEE0E4jGGWOYjvelmBZJyWZyBECl1e49XFPfcDvhTuTIicN8xfX 3ROJMfR1NM/Gm/9bEOJC7XAW/AbFV9j6LkJroBAk1Pxt5xu3FvIPTex3bCyIjAMSpzwNX y3WAC/LdyQJ+NwnFpGwrkbi6kYEKXaIJA9jtRBkc0kMRKX0onOw2H7vEqGXTztPW8wHCw nVeuYpjDUSOCcl+tjkd5fI6duYeIg8oyHEJ6M+ejHVbCE2Tod/10wfxJpVa0Ld0KzIg== 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=JE0Vl+mT; 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 A248C606A321 for ; Sun, 6 Dec 2026 05:37:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A248C606A321 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=JE0Vl+mT 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=GH9sAEkOULiwE+WrnccPwRvw1wZumwUkQspcI5ewFO4=; t=1796535479; x=2111895479; b=JE0Vl+mTI4o8BebNDeFNq8qwp0zxZR2VtH36IndbFNEf7W+ MKXcB6z5Xc2X0tFbdR1f2x6dF05OdHIUCNnyNaCGonRA5d2LWMaEbiGZv8MXqofmYBFYFaWp3pLF/ B2oiINghcqhxTWq0g1HEUszF1jwmTjMihp+aYge9ekp/m4wy/SoflCWBagnqB+7zqlE+YdFAgQefS bc8mr5rRCNK+g/DLzLdy52YZCTPiMBfsvtUzBtIB8y6ncFvk3H631cVg69g2wv/dhDkccdDYHz3ub dPaxhyy3oYgtfaTmDtfSfCfT35hS/T/yMCTeuvSlN/edKKvCmgC/KfaT6ltptFCw==; 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 1xa4we-00000002dO6-0jdv; Sun, 06 Dec 2026 06:37:56 +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 v3 4/6] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_priv Date: Sun, 6 Dec 2026 14:37:26 +0900 Message-ID: <20261206053728.2026272-5-changwoo@igalia.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261206053728.2026272-1-changwoo@igalia.com> References: <20261206053728.2026272-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. qmap_exit_task() clears the field before freeing the task_ctx. 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 | 69 ++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/tools/sched_ext/scx_qmap.bpf.c b/tools/sched_ext/scx_qmap.bpf.c index 3951f1f6e3c6..90237a1ce38a 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,49 @@ 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_core_sched_before() and 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. qmap_exit_task() clears the value before freeing the + * task_ctx, as the kernel clears it only after ops.exit_task(). + */ +#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 = scx_bpf_task_priv(p); 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,12 +1100,36 @@ 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) { struct task_ctx_stor_val *v; task_ctx_t *taskc; + /* Don't let lookup_task_ctx() return @p's task_ctx once it's freed */ + scx_bpf_task_set_priv(p, 0); + v = bpf_task_storage_get(&task_ctx_stor, p, NULL, 0); if (!v || !v->taskc) return; @@ -1124,17 +1186,15 @@ void BPF_STRUCT_OPS(qmap_dump_cid, struct scx_dump_ctx *dctx, s32 cid, bool idle void BPF_STRUCT_OPS(qmap_dump_task, struct scx_dump_ctx *dctx, struct task_struct *p) { - struct task_ctx_stor_val *v; task_ctx_t *taskc; QMAP_TOUCH_ARENA(); if (suppress_dump) return; - v = bpf_task_storage_get(&task_ctx_stor, p, NULL, 0); - if (!v || !v->taskc) + taskc = lookup_task_ctx(p); + if (!taskc) return; - taskc = v->taskc; scx_bpf_dump("QMAP: force_local=%d core_sched_seq=%llu", taskc->force_local, taskc->core_sched_seq); @@ -2252,6 +2312,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