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=1796104147; b=QU4A73sGtvt6CbxGUkhmpOugRJHZqiYhVvNJIZLJ2B2G6o/XNS6GvYIZbInlO5rhdLDJ Gp1QazGNf175ja6o1POdZqtKu1qxksgjgYTL0Q4RzIsiyekIvwTLKnMfwZlok2ZbqIqaI 5MfAjciGc0tnvKVPow6x7XHU+I+Ng/FGSXbA6gpsXvJnmiKu7rpUCzvkYTIftFc6Tst02 UZqG/St72JL81PLPTdy80OU/CuWKiga+iRlLbBeukY5FOlNOtBc9e5pCgytA5bfLUnBa5 taeB3ollyBIKl5kFOX/Q83yLf55VHZB5h6YgnudVfp65WrR3zK427ahyETX6WKb2+Qg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796104147; 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=tJYlHIiD4G1jPBEb3ejcZm96WS3qTbOfOiOTaZJD4IwOpJ9OcPnn+f9nV0oavdZQ2Kv/ 1VUk2ZhqeSPu5kUPZnwlokixJjDs6DN1QYwajgCzVZoQPfsWcfba2wBlxtZ5qu0glvcwi PbcmCmXWuVwFRcvYNSyFee7qbGkz/0jhxjZ88Kca8IcHZfPPOFFT31LntaDT4VDRK3U61 8VF9yfdFMHIjttVxHSdy9At+WaIWxiHlCdhdjpmBGLKF4l6PoQXVzeuWowcGGJEIDRu01 Zk9jT0mvnQf8BGvLTgtSfy/tEWHTA3W+lw+MM5o9fl5xdcVFKUiY88nl6h/ujoqG7WA== 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=b8ueakX5; 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 E7D3F606A31A for ; Tue, 1 Dec 2026 05:49:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E7D3F606A31A 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=b8ueakX5 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=1796104146; x=2111464146; b=b8ueakX5dCMrKKLULkhOKytR155IQxo+1un/BhCOmmbcB9Y nw/scjEHNKRLEkYlyeDyyMv2rRlvkpU+aO96o8ttH/VCcRFxQidCq9m0LZWTRtxmRPEtQS9pEfqYV 6HDWg6u6CMmL0N1jnen0kJlhSBT94ON/nG52h2IRDzBp7dizpkU+RZTpLePB0OlYTPJg1jPEm7jdk bUcU2Z7pIcP8Lfo0sK1oV1uFRx2a0yN4jCERO6Py3qtVVDwG6VyWpU2nHMuKie4tdxr1hBIj+amN2 rLSF8+yq8yR+TE2EpZ8fLPGYVuKimk45W11qrzIO5prUSeBkNqvNTAd/9tNI/zng==; 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: 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 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