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=1796273804; b=MHEu+MHbez0YlxUw+UmP+AjN0G5V2JAR4vJhDpHwmaZL2NrB8G2L8kov6CZWtEOXQvxg 0BQ6edsfPfb2CGd3QRboGLVag6CeiH1BruvC9VCFVZNHsCUJo8RZsjmFu58Bi24U+xde2 H0rKdmBT0orXszyd78SjpfbNZnMaPzREXTvuF4FfQx5t5A5VgF38u3dHLudzxhsOqhg0s r7GJHcZ3a4O5XKNtb/eNBVOPy0koqft+qlOJE1/AV5/XTJ8Ta4b91Q6HkQN9SqO8Mm0BT CMLOjaWiI4ndconSxpREWAs+j7U1t974QJUMh2hD4sMf939Ku51Jv+4x5M3LAlJj3vw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796273804; 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=U29a84NianqqK1d5EA36PEeUqXX9bg1JgmohjxMxKvk=; b=sHI8MGBxB1vEbEWQrMu6nzSZAd6hOIe4ni9tIAsVLkFsWjY+00W+ne48HGWFzDUYKpKC y3UAEg1iuyABffFCMpBU+4LXSkkPGLjfHwBoh2nmrIxwp6WPbTn/coGE1CzfmQZgWg+7b USkiM81jHxBQvyIKH+Pyyl7KAhlUf+BS8ejvW6jp/DXoO2aLzPNY8nr/MbAMLSYZHUuCx wHe6zGCfBrqMkiizmtKcqyALEIWHrSSGmeidxBNj4LgGG71PlEHKX+Pm8WVACG6sPTypD 028v14aAo78CnyAbvqBwNLzGElxB/DJ2LirqusaTAu7YzhpYcazeqs+AmkJlzSyUG4Q== 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=IF5PuOVM; 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 8C0DD606A322 for ; Thu, 3 Dec 2026 04:56:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8C0DD606A322 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=IF5PuOVM 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=U29a84NianqqK1d5EA36PEeUqXX9bg1JgmohjxMxKvk=; t=1796273804; x=2111633804; b=IF5PuOVMXR04RRS0TUacdZfHHnBf1xi8z2bZRdMCUisXDkE qVWfiI7P2XKWN8on3nT6jTXI8+rZ7ln2FBam56hajmswJh0Jlz89oBt1Qj1zA9dBX7uV1fpJAQovV EkX1A8ZJGmqHKwHRcikb2R9t+pJ/fC2bbHosPxtl+icpefu+2GI9niOQc0O0J+cj3JWsbWQCTwcws E/9P9M20fOXuphoOOV8YsCyO6nf/o0caPDutZHDR7iNOJxA0eDTqyzis76ch3xLiU+T+ZOtt2HVoV 7cnASc41YxinFoL9wnrnKkP1fSDqpmXomyOix7Grim2X1oy66w7hJS6vGv9Gqo5w==; 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 1xYys3-00000001Txb-2lwT; Thu, 03 Dec 2026 05:56: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 v2 3/8] sched_ext: Add scx_bpf_sched_id() and scx_bpf_task_sched_id() Date: Thu, 3 Dec 2026 13:56:13 +0900 Message-ID: <20261203045618.982539-4-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 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 7e5f58f1d844..2ebc67916757 100644 --- a/kernel/sched/ext/ext.c +++ b/kernel/sched/ext/ext.c @@ -11940,6 +11940,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 @@ -12262,6 +12295,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