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=1796535465; b=arwjT1x3OUwCArFTeXMgERuUSQ7aH/NPMjSn4zxwRF924nB3LHedMV1nrApqS7oMcOO0 yzfSvyFXWvTZD7dU/PctHyv3zrGdJTWVBi92AG0h6/s7pdq1usBEdizqsx0jIVt4Oo20L obiWLBoc0rfSuD1qpSycC0Y+r5B59y5haJRKD7RoMzoRU7njEZg0ipl4H5Qtmh46ggCTC fQLJ1XckpIGiGS1cigh+F4Z7NvmTnteAIY+dv86wgiCuHk81kkifYGdE+ZwMJ0STdA9qw bvzvljuEcPPxooQye0ROeyBeG5CjjRb6pIir73S0XXni47StYHfWbA6ZX11B41Yobdg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796535465; 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=f8hoAK7n83ixMaXqFQC+E9hQpyme7YxC9UktBJQVsrw=; b=E9KJYGIkQDSv8XrnM5rR9fZbpXXJlhXo/boCCgs1PB293uH7+S9MYaU1v5YBjIDnzOtG pWIw3ixeaSyjQ24rte3QOA+9k1eE6ejB3RTi3p+vkX9V/w1wztitWvSQsZ1eq9nECQHQD gVnH/KY25AAVjJWNxnCSdFHXI+vsLqJclwUnCDMu72gzA+u1y23mtjHHJ6draAb5ijcvn f0KruLs8TdSyGCBQ5lgos2zouCbDsx8LS6QYO/hC+shHT+LhkaiIzqqMu5zebFu+wh1z4 abc+Ju1YCfDLLNN268YaCGlZY++YgXjBS1iKKNy91CHlnlnCvUnT1XINUTuTYVMaMFA== 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=rXFrPrw0; 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 13329606A31F for ; Sun, 6 Dec 2026 05:37:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 13329606A31F 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=rXFrPrw0 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=f8hoAK7n83ixMaXqFQC+E9hQpyme7YxC9UktBJQVsrw=; t=1796535464; x=2111895464; b=rXFrPrw0kCpuQVrufG3xMs0HyUWkFZU3RFJOAhTeUXTFb0x RK+M/ipOSU4etvuOQ8/aw8GzhMoTIcIh71F6XRKBeXnxZ75kqMM1fuagSwoW+/jd807Jd5Pc+75di zkGVFRHPz4NzFxWT1GXUL4RlcGW70PlI+lhl6uV63Z6tuTe6RNQcRZScTBM5pNDqaLYuhVNmfO3LK l8iqqyYUo47Ezn73ShVuiTHeMJrOETx0kkrvguOJJAv1DW5dYHYPZ4WR5/YdC3ngUmKxe8u0A3vfe UP/eTIjwVV9bl4VR9yPiAf8O2aq2A6sXIQpBurRoeFZwks0KDJGzimvl1iUV+raw==; 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 1xa4wO-00000002dMy-0mDr; Sun, 06 Dec 2026 06:37:40 +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 1/6] sched_ext: Add scx_bpf_sched_id() and scx_task_sched_id() Date: Sun, 6 Dec 2026 14:37:23 +0900 Message-ID: <20261206053728.2026272-2-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 A BPF scheduler can't tell which scheduler instance it is or which one owns a task. With sub-schedulers, a task it reaches as a waker, from a tracing program or by iteration may belong to another scheduler, and a value that scheduler stored in the task means nothing to it. Every scx_sched already has an id that starts at 1, increases monotonically and is never reused. Add the scx_bpf_sched_id() kfunc, the calling scheduler's id, 0 for none. Only the kernel knows which scheduler a program belongs to, so it has to be a kfunc. Add scx_task_sched_id() to compat.bpf.h, the id of the scheduler that owns a task, 0 for none. It reads p->scx.sched with CO-RE, but only in INIT, READY or ENABLED, as a scheduler owns a task only after ops.init_task(). Without CONFIG_EXT_SUB_SCHED, the kernel doesn't set p->scx.sched, so the helper returns scx_bpf_sched_id(), the root's id. scx_set_task_state() now publishes the state bits in one store, as the helper reads them without a lock. Suggested-by: Andrea Righi Signed-off-by: Changwoo Min --- kernel/sched/ext/ext.c | 22 ++++++++- tools/sched_ext/include/scx/compat.bpf.h | 61 ++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c index 6e1ce8681352..343d82f328ef 100644 --- a/kernel/sched/ext/ext.c +++ b/kernel/sched/ext/ext.c @@ -784,8 +784,9 @@ void scx_set_task_state(struct task_struct *p, u32 state) WARN_ONCE(warn, "sched_ext: Invalid task state transition 0x%x -> 0x%x for %s[%d]", prev_state, state, p->comm, p->pid); - p->scx.flags &= ~SCX_TASK_STATE_MASK; - p->scx.flags |= state; + /* One store, as BPF's scx_task_sched_id() reads it locklessly */ + WRITE_ONCE(p->scx.flags, + (p->scx.flags & ~SCX_TASK_STATE_MASK) | state); } /** @@ -11958,6 +11959,22 @@ __bpf_kfunc s32 scx_bpf_task_cpu(const struct task_struct *p) return task_cpu(p); } +/** + * scx_bpf_sched_id - Id of the calling scheduler + * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs + * + * Return the calling scheduler's instance id, 0 if there is none. Ids start at + * 1, increase monotonically and are never reused. + */ +__bpf_kfunc u64 scx_bpf_sched_id(const struct bpf_prog_aux *aux) +{ + struct scx_sched *sch; + + guard(rcu)(); + sch = scx_prog_sched(aux); + return sch ? sch->id : 0; +} + /** * scx_bpf_task_cid - cid a task is currently associated with * @p: task of interest @@ -12280,6 +12297,7 @@ BTF_ID_FLAGS(func, scx_bpf_get_online_cpumask, KF_ACQUIRE) BTF_ID_FLAGS(func, scx_bpf_put_cpumask, KF_RELEASE) BTF_ID_FLAGS(func, scx_bpf_task_running, KF_RCU) BTF_ID_FLAGS(func, scx_bpf_task_cpu, KF_RCU) +BTF_ID_FLAGS(func, scx_bpf_sched_id, KF_IMPLICIT_ARGS) BTF_ID_FLAGS(func, scx_bpf_task_cid, KF_RCU) BTF_ID_FLAGS(func, scx_bpf_locked_rq, KF_IMPLICIT_ARGS | KF_RET_NULL) BTF_ID_FLAGS(func, scx_bpf_cpu_curr, KF_IMPLICIT_ARGS | KF_RET_NULL | KF_RCU_PROTECTED) diff --git a/tools/sched_ext/include/scx/compat.bpf.h b/tools/sched_ext/include/scx/compat.bpf.h index 3520a749f8ce..4d2298bd7ac5 100644 --- a/tools/sched_ext/include/scx/compat.bpf.h +++ b/tools/sched_ext/include/scx/compat.bpf.h @@ -487,6 +487,67 @@ 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 scx_bpf_sched_id(). Ids start at 1, increase monotonically and are + * never reused. 0 means no scheduler, including on kernels without the kfunc. + */ +u64 scx_bpf_sched_id___new(void) __ksym __weak; + +static inline u64 scx_bpf_sched_id(void) +{ + if (bpf_ksym_exists(scx_bpf_sched_id___new)) + return scx_bpf_sched_id___new(); + return 0; +} + +struct scx_sched___task_id { + u64 id; + struct cgroup *cgrp; +} __attribute__((preserve_access_index)); + +struct sched_ext_entity___task_id { + struct scx_sched___task_id *sched; + u32 flags; +} __attribute__((preserve_access_index)); + +/* + * Id of the scheduler @p is on, see scx_bpf_sched_id(), 0 if @p is not on any. + * Unless @p's rq lock or pi_lock is held, @p may move to another scheduler + * right after. + */ +static __always_inline u64 scx_task_sched_id(const struct task_struct *p) +{ + struct sched_ext_entity___task_id *scx = (void *)&p->scx; + struct scx_sched___task_id *sch; + u32 state; + u64 id; + + if (!bpf_ksym_exists(scx_bpf_sched_id___new)) + return 0; + + /* A sched owns @p only after ops.init_task() */ + state = READ_ONCE(scx->flags) & + bpf_core_enum_value(enum scx_ent_flags, SCX_TASK_STATE_MASK); + if (state == bpf_core_enum_value(enum scx_ent_flags, SCX_TASK_NONE) || + state == bpf_core_enum_value(enum scx_ent_flags, SCX_TASK_INIT_BEGIN) || + state == bpf_core_enum_value(enum scx_ent_flags, SCX_TASK_DEAD)) + return 0; + + /* + * Without CONFIG_EXT_SUB_SCHED, which @cgrp comes with, the kernel + * doesn't set p->scx.sched, and the root is the only scheduler. + */ + if (!bpf_core_field_exists(struct scx_sched___task_id, cgrp)) + return scx_bpf_sched_id(); + + /* Sleepable programs aren't in an RCU read section */ + bpf_rcu_read_lock(); + sch = scx->sched; + id = sch ? sch->id : 0; + bpf_rcu_read_unlock(); + return id; +} + /* * v7.1: New scx_bpf_dsq_reenq() that allows re-enqueues on more DSQs. This * will eventually deprecate scx_bpf_reenqueue_local(). -- 2.56.1