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=1796273815; b=nSBOgkMrIBqxNX02bkcyPDXTnJO3A7YulRIWuB9N3cYuBwRbN5VvbD3ewN+0Ga7omp5t 6ioVAGPxW2eL+RnXfZ7/yoy5XPbvU9WeU7V7tvPYjS77OwEBb2f3NyX57ZeZEaL/POIKD ne6A00EPRtxVFdwua081CKl7uUrPGZLqS8JOMNBbOX64SjjL/wlDtgGe1jB+ePVQMLVAI dssTHLbP6Mt0XntO3qGzLKSvHTNs+o0+2LFq8qaA6opeTuTomMBLUMHuq83tCZPAq7aM9 J5sQqpHFcrjjKDcUB/y2Lf4BWicoU2eijOAVUnhgKIC+eqmWGrXW7j03YevwVEqy/FQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796273815; 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=KH3x3F8RqYQep/lPSaRCabPClHh4iFW7iUgPVf1lUB0=; b=SktLn0oDoMU7KX4nePvZb9CmSwFIHQPQPNqlCiX5YUIEFB6AbsDdxnMMJFzenlCGKVTU VneUo2fuYS3E8AJcS9tBx8urHu2QfCf2/LSM69mIZw4xWksz98NpPCFYMZwlnRtv1iKRZ qXWJJz8NrxFHsvkiH4sSJnrbZ3/9rRCyxIhUmdYC9C6hsPblbg1y7my+W2yXjM7Q3aK9H 4Zweh4/M78TXCZffRi2VjrNGXKPZ5llH8U7c3dBcQ2VX+fkZDdLdRIMeZg4vEc6AoI8mS /5Eyx+7rbOOaZXEf7H++MhDECmqaK83Xqb9htD87BLIEj8pamclM5iwZvUmU7RqJ3jA== 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="TIaQW/d7"; 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 CD325606A333 for ; Thu, 3 Dec 2026 04:56:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CD325606A333 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=TIaQW/d7 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=KH3x3F8RqYQep/lPSaRCabPClHh4iFW7iUgPVf1lUB0=; t=1796273814; x=2111633814; b=TIaQW/d7PXa+isQVkHM9e5swHsgUtNIfCw6tyqYp0hIDwwr vvm8YfKyWJC5lGWIa6aGwkoan44gg0aM1ZuOzHelJkw2ltBZxVjZIxP1zqJQzwNGDwPWavmj0KlUt C9AWJJ4U92tM3ieKypeL1MEm38KU5sIBrDYMClkBKl6hX/EILoqWZAgHD5s89mQ1wsftz2m36/S3g 2yqIWTdeP5oaQ7f0bXqNUt3uVP1GSDGQYU2VSVXT5NS3Ckto/E8G++aoiP+5DxFqqpMLlQgE6XZAL lb+jZ76zWr+GP5SBOh8J3tGyVQCcvQkk5Ci4i98tNDA/GWb3/ToBLTlKMDkRLCsA==; 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 1xYysD-00000001TyI-26zV; Thu, 03 Dec 2026 05:56:50 +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 5/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Date: Thu, 3 Dec 2026 13:56:15 +0900 Message-ID: <20261203045618.982539-6-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 BPF schedulers keep their per-task context in task local storage, or in an arena allocation reached through it. Each lookup costs a bpf_task_storage_get() call. Add an opaque u64, p->scx.bpf_priv, that the task's scheduler sets with scx_bpf_task_set_priv(), typically to an arena pointer to its context. Any BPF program reads it with a plain load. The kernel never dereferences it. The value is valid while @p is enabled on the scheduler that set it. scx_reset_task_fields() clears it when @p leaves the scheduler's control, after the last op the scheduler runs for @p. That covers @p leaving the ext class, moving to another sub-scheduler and exiting. ops.enable() runs again whenever @p comes back. Like scx_bpf_task_set_dsq_vtime(), scx_bpf_task_set_priv() accepts any caller and refuses a task that is not on the calling scheduler. Unlike it, scx_bpf_task_set_priv() also refuses a task that is not enabled. The value is cleared whenever @p leaves the ext class, and only ops.enable() runs again when @p comes back. A value set earlier, e.g. from ops.init_task(), would be lost the first time @p leaves the ext class. A write from outside @p's own ops can race @p leaving and outlive the clear. As with dsq_vtime, synchronizing such writes is left to the scheduler. The value means something only to the scheduler that set it. With sub-schedulers, a task a scheduler reaches as a waker or from a tracing program may belong to another one. A scheduler can tag the value with its scx_bpf_sched_id() to recognize its own. Signed-off-by: Changwoo Min --- include/linux/sched/ext.h | 13 ++++++ kernel/sched/ext/ext.c | 54 ++++++++++++++++++++++-- tools/sched_ext/include/scx/compat.bpf.h | 24 +++++++++++ 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/include/linux/sched/ext.h b/include/linux/sched/ext.h index f3564581c317..e63bd789c7d8 100644 --- a/include/linux/sched/ext.h +++ b/include/linux/sched/ext.h @@ -277,6 +277,19 @@ struct sched_ext_entity { */ atomic64_t slice_oob; + /* + * Opaque value owned by the task's scheduler, typically an arena + * pointer to its per-task context. The kernel never dereferences it. + * Set with scx_bpf_task_set_priv() while @p is enabled on the + * scheduler, from ops.enable() on, and cleared when @p leaves the + * scheduler's control, after the last op the scheduler runs for @p. + * Any BPF program can read it, so a tracing program can reach the + * context of the current task with a single load. A write from + * outside @p's own ops can outlive the clear, see + * scx_bpf_task_set_priv(). + */ + u64 bpf_priv; + /* * Sub-sched cap rejected reenq context, valid only while * %SCX_TASK_REENQ_CAP is set. @reenq_reason_caps is the SCX_CAP_* bits diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c index 2ebc67916757..0877341440ad 100644 --- a/kernel/sched/ext/ext.c +++ b/kernel/sched/ext/ext.c @@ -4753,16 +4753,17 @@ void scx_enable_task(struct scx_sched *sch, struct task_struct *p) * @rq: @p's rq, locked * @p: task leaving its sched's control * - * The vtime, the slice and what rides on it, the reenqueue count and the - * watchdog wait belong to the sched while it controls @p. The control ends when - * @p is taken off the sched or leaves the ext class while staying on the sched. - * Reset them so that @p starts clean wherever it goes. + * The vtime, the slice and what rides on it, the reenqueue count, the BPF + * private value and the watchdog wait belong to the sched while it controls @p. + * The control ends when @p is taken off the sched or leaves the ext class while + * staying on the sched. Reset them so that @p starts clean wherever it goes. */ static void scx_reset_task_fields(struct rq *rq, struct task_struct *p) { lockdep_assert_rq_held(rq); p->scx.dsq_vtime = 0; + p->scx.bpf_priv = 0; scx_task_slice_ended(rq, p); scx_set_task_slice(p, 0); p->scx.reenq_cnt = 0; @@ -10893,6 +10894,50 @@ __bpf_kfunc bool scx_bpf_task_set_lazy_resched(struct task_struct *p, bool lazy, return true; } +/** + * scx_bpf_task_set_priv - Set the BPF scheduler's private value of a task + * @p: task of interest + * @priv: opaque value to store in @p->scx.bpf_priv + * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs + * + * Store @priv in @p->scx.bpf_priv, where any BPF program can read it with a + * plain load. The value is valid while @p is enabled on the calling scheduler. + * The kernel clears it when @p leaves the scheduler's control, after the last + * op the scheduler runs for @p, and ops.enable() runs again when @p comes back. + * + * From @p's own ops, ops.enable() included, @p can't leave the scheduler + * concurrently. Elsewhere, e.g. from a timer or a tracing program, the write + * can race @p leaving and outlive the clear, as with + * scx_bpf_task_set_dsq_vtime(). Synchronizing such writes is the BPF + * scheduler's responsibility. + * + * Return %true if @priv was stored, %false if @p is not enabled on the calling + * scheduler. + */ +__bpf_kfunc bool scx_bpf_task_set_priv(struct task_struct *p, u64 priv, + const struct bpf_prog_aux *aux) +{ + struct scx_sched *sch; + + guard(rcu)(); + sch = scx_prog_sched(aux); + if (unlikely(!sch || !scx_task_on_sched(sch, p))) + return false; + + /* + * Accept only %ENABLED. The value is cleared whenever @p leaves the ext + * class, and only ops.enable() runs again when @p comes back. A value + * set before ops.enable() would be lost the first time @p leaves the ext + * class. This refuses ops.init_task(), ops.exit_task() and @p on a + * non-ext class. + */ + if (scx_get_task_state(p) != SCX_TASK_ENABLED) + return false; + + p->scx.bpf_priv = priv; + return true; +} + void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags) { struct scx_sched_pcpu *pcpu; @@ -12265,6 +12310,7 @@ BTF_KFUNCS_START(scx_kfunc_ids_any) BTF_ID_FLAGS(func, scx_bpf_task_set_slice, KF_IMPLICIT_ARGS | KF_RCU); BTF_ID_FLAGS(func, scx_bpf_task_set_dsq_vtime, KF_IMPLICIT_ARGS | KF_RCU); BTF_ID_FLAGS(func, scx_bpf_task_set_lazy_resched, KF_IMPLICIT_ARGS | KF_RCU); +BTF_ID_FLAGS(func, scx_bpf_task_set_priv, KF_IMPLICIT_ARGS | KF_RCU); BTF_ID_FLAGS(func, scx_bpf_kick_cpu, KF_IMPLICIT_ARGS) BTF_ID_FLAGS(func, scx_bpf_kick_cid, KF_IMPLICIT_ARGS) BTF_ID_FLAGS(func, scx_bpf_dsq_nr_queued, KF_IMPLICIT_ARGS) diff --git a/tools/sched_ext/include/scx/compat.bpf.h b/tools/sched_ext/include/scx/compat.bpf.h index 41882c6497ac..2445f1affbec 100644 --- a/tools/sched_ext/include/scx/compat.bpf.h +++ b/tools/sched_ext/include/scx/compat.bpf.h @@ -487,6 +487,30 @@ static inline void scx_bpf_task_set_lazy_resched(struct task_struct *p, bool laz scx_bpf_task_set_lazy_resched___new(p, lazy); } +/* v7.x: Add p->scx.bpf_priv and scx_bpf_task_set_priv(). */ +bool scx_bpf_task_set_priv___new(struct task_struct *p, u64 priv) __ksym __weak; + +static inline bool scx_bpf_task_set_priv(struct task_struct *p, u64 priv) +{ + if (bpf_ksym_exists(scx_bpf_task_set_priv___new)) + return scx_bpf_task_set_priv___new(p, priv); + return false; +} + +struct sched_ext_entity___bpf_priv { + u64 bpf_priv; +} __attribute__((preserve_access_index)); + +/* p->scx.bpf_priv, or 0 on kernels without it */ +static __always_inline u64 scx_bpf_task_priv(struct task_struct *p) +{ + struct sched_ext_entity___bpf_priv *scx = (void *)&p->scx; + + if (bpf_core_field_exists(scx->bpf_priv)) + return READ_ONCE(scx->bpf_priv); + return 0; +} + /* * v7.x: Add scx_bpf_sched_id() and scx_bpf_task_sched_id(). Ids start at 1, * increase monotonically and are never reused. 0 means no scheduler, including -- 2.56.1