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=1796535475; b=3lbckZIZcG1N8XRgDFADvq3t85rUpuG7yNLKKUxWy5SZpvtfPD8ybGBqojQOJ4Jh8IhO d3DX/9ZgaCJSWxYrcLedDFb1B33siFzhH0cC6WEnlDQ5UlCl38Sb0DiG/47KXwOJzVQ3+ 2cMAh9EU5zINBhoolfFAicuYeR3f2FtBzlQ7QaDleZO9n1eesIyNyY3M+17xE9uF+mllp jcwBX/QvKCOk5JKMS0nNVcs59RHITELxI+Z8N9EbtsczPEZKnUdiDjYIW5B1OHsAF4Yk3 JHlXSjcWwhHf5wYJ46cFpAd01vzLDR0r5GPUt8no2cANbmNXktgZNkENrmZmeATOnmg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796535475; 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=77ILGiAuIDAMVT1c0BMVq+DlwwW9Dg+TrNY0eVgMKLs=; b=a+PrDaI3ntzSpTNVQGifo6j4FtcHVt/OxKBpOQMKVDG04Bs5/7VVRPatJx0RPH3ZQf8E ecuE/isyUFV+MSiZhFG6L4mDzgpsIPmkz2BN5XwffTcvuZl0igD3Ev4nLVcSJm5yrcGvB e9Uj3wS4O7PWiqyliSaA75YRzoOXqn8TEth32CmyD99WWtXzJ8uJnTbCJCelzsrb9FxBS b0DvjpbzvFx+3086Mm7n/bZ1gtmwyFeajN9jGPtVl8QtYR1rCQWDpB6M1bQafK+cesM3k dj1Jm3hEIMZoWrkU5CzrRMa/keoTPohqZ8T7JHQWsREGgCe8v1N/VGCbn9LWF4fXcCA== 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="dHkV1/CD"; 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 C0455606A321 for ; Sun, 6 Dec 2026 05:37:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C0455606A321 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=dHkV1/CD 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=77ILGiAuIDAMVT1c0BMVq+DlwwW9Dg+TrNY0eVgMKLs=; t=1796535474; x=2111895474; b=dHkV1/CDwbaYptqepKQ7zalxBwkFStYsi0lbVquxNvtYZ9p Ln9uSYx/D4gClzUzyOLFaUOBFl1TJ3UhJ4y/H6AO2qQs6npnllFKwNhqZ7MXdVDb7BNaIzl5E0+6M i1Tlc3XfE7c6y7cKsKOzn5qG6bFKap6fgHCKfSHri4y4zALLvyfI01ksj5yhCUTPgG7tC8U2cQZxp OFv9EHZHvlDHOTyqamGgQPUJECz3Mv9VGKt++h56HySLeJimKgZ2IFJ9GDSkMxm2cjjo3thqDJHDj sihnsCBeUr62/JOqnFBc5Cd5y4LEv0MqvqlPIYD7o/88Pma1dD5XO9qoCOZOd1Xg==; 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 1xa4wZ-00000002dNi-0as9; Sun, 06 Dec 2026 06:37:51 +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 3/6] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Date: Sun, 6 Dec 2026 14:37:25 +0900 Message-ID: <20261206053728.2026272-4-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 BPF schedulers keep their per-task context in task local storage, or in an arena allocation reached through it, and pay a bpf_task_storage_get() call per lookup. Add p->scx.bpf_priv, an opaque u64 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 value belongs to the scheduler that owns @p, from after ops.init_task() until ops.exit_task(). The kernel clears it after ops.exit_task(), so it never reaches the next scheduler. It isn't reset with the other task fields when @p leaves the ext class, as the scheduler still owns @p then. scx_bpf_task_set_priv() checks the ownership under @p's locks, so a write can't race @p's move to another scheduler. Like scx_bpf_cpuperf_set(), it works from an op called for @p, which holds one of those locks, or with no lock held, where it takes them. It fails from any other op, and tracing programs, which may run under any lock, can't call it. For lockless readers, the store is ordered after the stores that set up what the value points to. With sub-schedulers, a task that a scheduler reaches as a waker, from a tracing program or in ops.init_task() may belong to another one. A reader holding the task's locks compares scx_task_sched_id() with its scx_bpf_sched_id(). A lockless reader tags the value with its id instead, since only the owner stores and the kernel clears the value before the task moves. Signed-off-by: Changwoo Min --- include/linux/sched/ext.h | 11 +++ kernel/sched/ext/ext.c | 89 ++++++++++++++++++++++++ tools/sched_ext/include/scx/compat.bpf.h | 24 +++++++ 3 files changed, 124 insertions(+) diff --git a/include/linux/sched/ext.h b/include/linux/sched/ext.h index f3564581c317..a1793ab5f0c3 100644 --- a/include/linux/sched/ext.h +++ b/include/linux/sched/ext.h @@ -277,6 +277,17 @@ 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 on the scheduler, + * from after ops.init_task() until ops.exit_task(), and cleared + * after ops.exit_task(). Any BPF program can read it, so a tracing + * program can reach the context of the current task with a single + * load. + */ + 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 343d82f328ef..4f575a19df76 100644 --- a/kernel/sched/ext/ext.c +++ b/kernel/sched/ext/ext.c @@ -4833,6 +4833,12 @@ void __scx_disable_and_exit_task(struct scx_sched *sch, struct task_struct *p) * ops.exit_task() can still write them while @p is on @sch. */ scx_reset_task_fields(task_rq(p), p); + + /* + * Not in scx_reset_task_fields(), which also runs when @p leaves the + * ext class. The value stays until @p leaves @sch. + */ + WRITE_ONCE(p->scx.bpf_priv, 0); } /* @@ -10912,6 +10918,84 @@ __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 calling scheduler can set it while @p is on it, from after + * ops.init_task() until ops.exit_task(), whatever @p's class. The kernel + * clears it after ops.exit_task(). + * + * @p may be on another scheduler, e.g. as a waker, in a tracing program or + * in ops.init_task(), and the value is then that scheduler's. So check the + * owner before using it. With @p's rq lock or pi_lock held, compare + * scx_task_sched_id() of @p, in compat.bpf.h, with scx_bpf_sched_id(). + * Without them, @p may move in between, so tag the value with + * scx_bpf_sched_id() and check the tag. + * + * Can be called from an op called for @p, or with no rq lock held, e.g. from + * an unlocked op, a BPF timer or a syscall program, in which case @p's locks + * are taken. Fails from any other op, such as one called for another task. + * Tracing programs can't call it. + * + * Return %true if @priv was stored, %false if @p is not on the calling + * scheduler or the caller is an op not called for @p. + */ +__bpf_kfunc bool scx_bpf_task_set_priv(struct task_struct *p, u64 priv, + const struct bpf_prog_aux *aux) +{ + struct scx_sched *sch; + struct rq_flags rf; + struct rq *rq = NULL; + bool locked, ret = false; + u32 state; + + guard(rcu)(); + sch = scx_prog_sched(aux); + if (unlikely(!sch || !scx_kf_allowed_ctx(sch))) + return false; + + /* Fast path, rechecked under @p's locks below */ + if (!scx_task_on_sched(sch, p)) + return false; + + /* + * @p's owner and state change only under both @p's pi_lock and rq lock. + * An op called for @p holds one of them, the rq lock or, for + * ops.select_cpu(), the pi_lock. Any other lock doesn't pin @p, and + * taking @p's locks under it can deadlock, so restrict the call to @p's + * own op when a lock is held. + */ + locked = p == current->scx.kf_tasks[0] || p == current->scx.kf_tasks[1]; + if (!locked && (scx_locked_rq() || current->scx.kf_tasks[0])) + return false; + + /* If no lock is held, pin @p by taking its locks */ + if (!locked) + rq = task_rq_lock(p, &rf); + + /* The sched owns @p from after ops.init_task() until ops.exit_task() */ + state = scx_get_task_state(p); + if (scx_task_on_sched(sch, p) && + (state == SCX_TASK_INIT || state == SCX_TASK_READY || + state == SCX_TASK_ENABLED)) { + /* + * Lockless readers may dereference @priv. Order the stores that + * set up what @priv points to before the store that publishes it. + */ + smp_wmb(); + WRITE_ONCE(p->scx.bpf_priv, priv); + ret = true; + } + + if (!locked) + task_rq_unlock(rq, p, &rf); + return ret; +} + void scx_kick_cpu(struct scx_sched *sch, s32 cpu, u64 flags) { struct scx_sched_pcpu *pcpu; @@ -12267,6 +12351,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) @@ -12330,6 +12415,9 @@ static const struct btf_kfunc_id_set scx_kfunc_set_any = { * when scx_locked_rq() returns NULL. Taking it while the context already holds * an rq lock, the same one or another cpu's, can deadlock. * + * scx_bpf_task_set_priv() takes the task's pi_lock and rq lock outside an op + * called for the task, with the same deadlock. + * * scx_bpf_destroy_dsq() takes a user DSQ lock. So do * bpf_iter_scx_dsq_next() and bpf_iter_scx_dsq_destroy(), the latter only * after a successful next() has linked the cursor. A tracing program can @@ -12347,6 +12435,7 @@ static const struct btf_kfunc_id_set scx_kfunc_set_any = { BTF_KFUNCS_START(scx_kfunc_ids_no_tracing) BTF_ID_FLAGS(func, scx_bpf_cpuperf_set, KF_IMPLICIT_ARGS) BTF_ID_FLAGS(func, scx_bpf_cidperf_set, KF_IMPLICIT_ARGS) +BTF_ID_FLAGS(func, scx_bpf_task_set_priv, KF_IMPLICIT_ARGS | KF_RCU) BTF_ID_FLAGS(func, scx_bpf_sub_grant, KF_IMPLICIT_ARGS) BTF_ID_FLAGS(func, scx_bpf_sub_revoke, KF_IMPLICIT_ARGS) BTF_ID_FLAGS(func, scx_bpf_destroy_dsq, KF_IMPLICIT_ARGS) diff --git a/tools/sched_ext/include/scx/compat.bpf.h b/tools/sched_ext/include/scx/compat.bpf.h index 4d2298bd7ac5..e8cb5f35eed0 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.5: 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.5: Add scx_bpf_sched_id(). Ids start at 1, increase monotonically and are * never reused. 0 means no scheduler, including on kernels without the kfunc. -- 2.56.1