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=1796104145; b=eXmEhVHPWnERYixI9CFUVYY0JsocmVKm6JE52iNj9qdGZY+FYJESnPMcJ/NLUwAw5CKo QtkS8M3GcEaMrMUjipurrCaXA2FpsgwftH7g+n8F2N/h8G0X9wQyMiEWy4rVSyQdFe7DA 5qag1lpMSlckCmjmdq91ir84q6SASIZm8w/VnQdtkAniWo5tD130WH8XD2J/SZvvPlM/e CAlfNgrauOm0buMdoNW9D+vnNgHLUXr0d4Lxyy1ReOzCBn8phgEfl9ZjdkNBnzmXbXvpS OTPEE9tcazuOf+ivWsRx+wNe/be/6MLtt6+18WlALGsv12GrOebolol+0gQd/SocFIQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796104145; 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=Q00LNXVzpknhNM5DzWHMsnf/RnqsP6bLzjeDo6T8Egk=; b=RA6yQ5H5uPmbZ27C9dRP4uG6d4Y0D9mMQSmL33LXX4AQhxlxTT+kjaLFC21zetCqawTU KQRmRm0aWiM4H0XKcn+/GuyVSxXBfrZr6bxcLFdBDinxBM3vH0O3AeWcwPq6Uoml1c1hj S46bM/ISZKNcNMA5S7wpV48dpIdrwYZZo3E+HckI6x4v/XTQ0LyRipi8xiPMEU8A/7GAg sWTAFDbo7D8WwxHoqHBOZaoyuwGBD7siQVI/kPTBxHwmzVeYCbDrSzrgc9YICmEsFFSx0 Ur4/WxcLEsZz7GSf+cvzIGhHL1gj3vy6XG8E/aIqb/o2XeiueeLn73+09yYdfwZ1LSg== 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=ks8bCjoT; 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 58A1B606A334 for ; Tue, 1 Dec 2026 05:49:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 58A1B606A334 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=ks8bCjoT 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=Q00LNXVzpknhNM5DzWHMsnf/RnqsP6bLzjeDo6T8Egk=; t=1796104145; x=2111464145; b=ks8bCjoTfnYOJdWwTImKXOWmkgkugUg7IKfvHE7U5vVcrPW y47swl8miWq9GNR1Sbo7++MhvittYaphbZmOBBZtsfpVgghQ5d5+jYhor7pE1IPnw5k0huDaVV+23 T0HUc4N0dVFK1UdgaeEVlaKQuJmNPglEoVjY6i2HHSUqB1apkSaE2fkz19h6TpUfE2FBN+GtiONfK aXZfSy1uXMdgsTdJ9XITL4DnX+uj9wxg6y/OkOkT6RZ1yOLBNcPsAHVOLUZdjodW7d6Qch4NoSc/s rMykaHfFjxq/0cqfbTBjxaxqpTklv7LOC2aPnrDai1eXC0+whg8XH8HSDbDjtE/A==; 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 1xYGje-00000000Ufe-1Gy3; Tue, 01 Dec 2026 06:49:02 +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 1/5] sched_ext: Add scx_bpf_sched_id() and scx_bpf_task_sched_id() Date: Tue, 1 Dec 2026 14:48:46 +0900 Message-ID: <20261201054850.780664-2-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 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 scx_bpf_sched_id(), the calling scheduler's id, and scx_bpf_task_sched_id(), the id of the scheduler that owns a task, both 0 for none. Any context may call them. Signed-off-by: Changwoo Min --- kernel/sched/ext/ext.c | 35 ++++++++++++++++++++++++ tools/sched_ext/include/scx/compat.bpf.h | 22 +++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c index a3cf580b0333..cdceeffbcc65 100644 --- a/kernel/sched/ext/ext.c +++ b/kernel/sched/ext/ext.c @@ -11933,6 +11933,39 @@ __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_sched_id - Id of the scheduler that owns a task + * @p: task of interest + * + * Return the instance id of the scheduler @p is on, see scx_bpf_sched_id(), 0 + * if @p is not on any. Without the rq lock of @p, @p may move to another + * scheduler right after. + */ +__bpf_kfunc u64 scx_bpf_task_sched_id(struct task_struct *p) +{ + struct scx_sched *sch; + + guard(rcu)(); + sch = scx_task_sched_rcu(p); + return sch ? sch->id : 0; +} + /** * scx_bpf_task_cid - cid a task is currently associated with * @p: task of interest @@ -12255,6 +12288,8 @@ 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_sched_id, KF_RCU) 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..41882c6497ac 100644 --- a/tools/sched_ext/include/scx/compat.bpf.h +++ b/tools/sched_ext/include/scx/compat.bpf.h @@ -487,6 +487,28 @@ 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 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 + * on kernels without the kfuncs. + */ +u64 scx_bpf_sched_id___new(void) __ksym __weak; +u64 scx_bpf_task_sched_id___new(struct task_struct *p) __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; +} + +static inline u64 scx_bpf_task_sched_id(struct task_struct *p) +{ + if (bpf_ksym_exists(scx_bpf_task_sched_id___new)) + return scx_bpf_task_sched_id___new(p); + return 0; +} + /* * 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