1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Topological CPU IDs (cids) 4 * -------------------------- 5 * 6 * Raw cpu numbers are clumsy for sharding work and communication across 7 * topology units, especially from BPF: the space can be sparse, numerical 8 * closeness doesn't imply topological closeness (x86 hyperthreading often puts 9 * SMT siblings far apart), and a range of cpu ids doesn't mean anything. 10 * Sub-scheds make this acute - cpu allocation, revocation and other state are 11 * constantly communicated across sub-scheds, and passing whole cpumasks scales 12 * poorly with cpu count. cpumasks are also awkward in BPF: a variable-length 13 * kernel type sized for the maximum NR_CPUS (4k), with verbose helper sequences 14 * for every op. 15 * 16 * cids give every cpu a dense, topology-ordered id. CPUs in each core, 17 * cluster, LLC or NUMA node get contiguous cid ranges, so a topology unit 18 * becomes a (start, length) slice of cid space. A core without a wider cluster 19 * level forms a cluster of its own. Communication can pass a slice 20 * instead of a cpumask, and BPF code can process, for example, a u64 word's 21 * worth of cids at a time. 22 * 23 * The mapping is built once at root scheduler enable time by walking the 24 * topology of online cpus only. Going by online cpus is out of necessity: 25 * depending on the arch, topology info isn't reliably available for offline 26 * cpus. The expected usage model is restarting the scheduler on hotplug events 27 * so the mapping is rebuilt against the new online set. A scheduler that wants 28 * to handle hotplug without a restart can provide its own cid and shard mapping 29 * through the override interface. 30 * 31 * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. 32 * Copyright (c) 2026 Tejun Heo <tj@kernel.org> 33 */ 34 #ifndef _KERNEL_SCHED_EXT_CID_H 35 #define _KERNEL_SCHED_EXT_CID_H 36 37 #include "internal.h" 38 39 struct scx_sched; 40 41 /* 42 * Cid space (total is always num_possible_cpus()) is laid out with 43 * topology-annotated cids first, then no-topo cids at the tail. The 44 * topology-annotated block covers the cpus that were online when scx_cid_init() 45 * ran and remains valid even after those cpus go offline. The tail block covers 46 * possible-but-not-online cpus and carries all-(-1) topo info (see 47 * scx_cid_topo); callers detect it via the -1 sentinels. 48 * 49 * See the comment above the table definitions in cid.c for the 50 * memory-ordering and visibility contract. 51 */ 52 struct scx_cid_tables { 53 u32 nr_shards; 54 s16 *cid_to_cpu; /* [num_possible_cpus()] */ 55 s16 *cpu_to_cid; /* [nr_cpu_ids] */ 56 s32 *cid_to_shard; /* [num_possible_cpus()] */ 57 s32 *shard_node; /* [num_possible_cpus()] */ 58 struct scx_cid_shard *shard_ranges; /* [num_possible_cpus()] */ 59 struct scx_cid_topo *topo; /* [num_possible_cpus()] */ 60 struct rcu_head rcu; 61 }; 62 63 extern u32 scx_nr_cid_shards; 64 extern s16 __rcu *scx_cid_to_cpu_tbl; 65 extern s16 __rcu *scx_cpu_to_cid_tbl; 66 extern s32 __rcu *scx_cid_to_shard; 67 extern s32 __rcu *scx_shard_node; 68 extern struct scx_cid_shard __rcu *scx_cid_shard_ranges; 69 extern struct scx_cid_topo __rcu *scx_cid_topo; 70 extern struct btf_id_set8 scx_kfunc_ids_init_cids; 71 extern struct btf_id_set8 scx_kfunc_ids_cid; 72 73 void scx_cmask_clear(struct scx_cmask *m); 74 void scx_cmask_fill(struct scx_cmask *m); 75 void scx_cmask_and(struct scx_cmask *dst, const struct scx_cmask *src); 76 void scx_cmask_or(struct scx_cmask *dst, const struct scx_cmask *src); 77 void scx_cmask_copy(struct scx_cmask *dst, const struct scx_cmask *src); 78 void scx_cmask_andnot(struct scx_cmask *dst, const struct scx_cmask *src); 79 bool scx_cmask_subset(const struct scx_cmask *sub, const struct scx_cmask *super); 80 bool scx_cmask_intersects(const struct scx_cmask *a, const struct scx_cmask *b); 81 bool scx_cmask_empty(const struct scx_cmask *m); 82 s32 scx_cid_init(struct scx_sched *sch); 83 void scx_cid_publish_tables(void); 84 void scx_cid_retire_tables(void); 85 int scx_cid_kfunc_init(void); 86 87 /** 88 * cid_valid - Verify a cid value, to be used on ops input args 89 * @sch: scx_sched to abort on error 90 * @cid: cid which came from a BPF ops 91 * 92 * Return true if @cid is in [0, num_possible_cpus()). On failure, trigger 93 * scx_error() and return false. 94 */ 95 static inline bool cid_valid(struct scx_sched *sch, s32 cid) 96 { 97 if (likely(cid >= 0 && cid < num_possible_cpus())) 98 return true; 99 scx_error(sch, "invalid cid %d", cid); 100 return false; 101 } 102 103 /** 104 * __scx_cid_to_cpu - Unchecked cid->cpu table lookup 105 * @cid: cid to look up. Must be in [0, num_possible_cpus()). 106 * 107 * Intended for callsites that have already validated @cid and that run on a 108 * live scheduler, which guarantees the tables are published and stable. 109 */ 110 static inline s32 __scx_cid_to_cpu(s32 cid) 111 { 112 return rcu_dereference_all(scx_cid_to_cpu_tbl)[cid]; 113 } 114 115 /** 116 * __scx_cpu_to_cid - Unchecked cpu->cid table lookup 117 * @cpu: cpu to look up. Must be a valid possible cpu id. 118 * 119 * Same usage constraints as __scx_cid_to_cpu(). 120 */ 121 static inline s32 __scx_cpu_to_cid(s32 cpu) 122 { 123 return rcu_dereference_all(scx_cpu_to_cid_tbl)[cpu]; 124 } 125 126 /** 127 * scx_cid_to_cpu - Translate @cid to its cpu 128 * @sch: scx_sched for error reporting 129 * @cid: cid to look up 130 * 131 * Return the cpu for @cid or a negative errno on failure. Invalid cid triggers 132 * scx_error() on @sch. The mapping is stable while the scheduler is live. 133 * 134 * Return -EINVAL without triggering scx_error() if no tables have been 135 * published yet, which a prog-facing kfunc can observe while racing the root 136 * scheduler enable. 137 */ 138 static inline s32 scx_cid_to_cpu(struct scx_sched *sch, s32 cid) 139 { 140 s16 *tbl = rcu_dereference_all(scx_cid_to_cpu_tbl); 141 142 if (!cid_valid(sch, cid) || unlikely(!tbl)) 143 return -EINVAL; 144 return tbl[cid]; 145 } 146 147 /** 148 * scx_cpu_to_cid - Translate @cpu to its cid 149 * @sch: scx_sched for error reporting 150 * @cpu: cpu to look up 151 * 152 * Return the cid for @cpu or a negative errno on failure. Invalid cpu triggers 153 * scx_error() on @sch. Same usage rules as scx_cid_to_cpu(). 154 */ 155 static inline s32 scx_cpu_to_cid(struct scx_sched *sch, s32 cpu) 156 { 157 s16 *tbl = rcu_dereference_all(scx_cpu_to_cid_tbl); 158 159 if (!scx_cpu_valid(sch, cpu, NULL) || unlikely(!tbl)) 160 return -EINVAL; 161 return tbl[cpu]; 162 } 163 164 /** 165 * scx_is_cid_type - Test whether the active scheduler hierarchy is cid-form 166 */ 167 static inline bool scx_is_cid_type(void) 168 { 169 return static_branch_unlikely(&__scx_is_cid_type); 170 } 171 172 static inline bool __scx_cmask_contains(u32 cid, const struct scx_cmask *m) 173 { 174 return likely(cid >= m->base && cid < m->base + m->nr_cids); 175 } 176 177 /* Word in bits[] covering @cid. @cid must satisfy __scx_cmask_contains(). */ 178 static inline u64 *__scx_cmask_word(u32 cid, const struct scx_cmask *m) 179 { 180 return (u64 *)&m->bits[cid / 64 - m->base / 64]; 181 } 182 183 /** 184 * __scx_cmask_init - Initialize @m with explicit storage capacity 185 * @m: cmask to initialize 186 * @base: first cid of the active range 187 * @nr_cids: number of cids in the active range 188 * @alloc_cids: storage capacity in cids, at least @nr_cids 189 * 190 * Use when storage is sized larger than the initial active range. All of 191 * bits[] is zeroed. 192 */ 193 static inline void __scx_cmask_init(struct scx_cmask *m, u32 base, u32 nr_cids, 194 u32 alloc_cids) 195 { 196 if (WARN_ON_ONCE(alloc_cids < nr_cids)) 197 nr_cids = alloc_cids; 198 199 m->base = base; 200 m->nr_cids = nr_cids; 201 m->alloc_words = SCX_CMASK_NR_WORDS(alloc_cids); 202 memset(m->bits, 0, m->alloc_words * sizeof(u64)); 203 } 204 205 /** 206 * scx_cmask_init - Initialize @m on tight storage 207 * @m: cmask to initialize 208 * @base: first cid of the active range 209 * @nr_cids: number of cids in the active range 210 * 211 * All of bits[] is zeroed. 212 */ 213 static inline void scx_cmask_init(struct scx_cmask *m, u32 base, u32 nr_cids) 214 { 215 __scx_cmask_init(m, base, nr_cids, nr_cids); 216 } 217 218 /** 219 * scx_cmask_reframe - Reshape @m's active range without resizing storage 220 * @m: cmask to reframe 221 * @base: new active range base 222 * @nr_cids: new active range length, must fit within @m->alloc_words 223 * 224 * Body bits within the new range become garbage - only the head and tail 225 * words are zeroed to keep the padding invariant. 226 */ 227 static inline void scx_cmask_reframe(struct scx_cmask *m, u32 base, u32 nr_cids) 228 { 229 if (WARN_ON_ONCE(SCX_CMASK_NR_WORDS(nr_cids) > m->alloc_words)) 230 return; 231 232 if (nr_cids) { 233 u32 last_word = ((base & 63) + nr_cids - 1) / 64; 234 235 m->bits[0] = 0; 236 m->bits[last_word] = 0; 237 } 238 239 m->base = base; 240 m->nr_cids = nr_cids; 241 } 242 243 static inline void __scx_cmask_set(u32 cid, struct scx_cmask *m) 244 { 245 if (!__scx_cmask_contains(cid, m)) 246 return; 247 *__scx_cmask_word(cid, m) |= BIT_U64(cid & 63); 248 } 249 250 /** 251 * scx_cmask_test - test whether @cid is set in @m 252 * @cid: cid to test 253 * @m: cmask to test 254 * 255 * Return %false if @cid is outside @m's active range. Otherwise return the 256 * bit's value. Read via READ_ONCE so callers can race set/clear writers. 257 */ 258 static inline bool scx_cmask_test(u32 cid, const struct scx_cmask *m) 259 { 260 if (!__scx_cmask_contains(cid, m)) 261 return false; 262 return READ_ONCE(*__scx_cmask_word(cid, m)) & BIT_U64(cid & 63); 263 } 264 265 /* 266 * Words of bits[] the active range spans, 0 if empty. Tighter than the storage 267 * SCX_CMASK_NR_WORDS() sizes for the worst-case base alignment. 268 */ 269 static inline u32 scx_cmask_nr_used_words(const struct scx_cmask *m) 270 { 271 if (!m->nr_cids) 272 return 0; 273 return ((m->base & 63) + m->nr_cids - 1) / 64 + 1; 274 } 275 276 /** 277 * scx_cmask_for_each_cid - iterate set cids in @m 278 * @cid: s32 loop var that receives each set cid in turn 279 * @m: cmask to iterate 280 * 281 * Visits set bits within @m's active range in ascending order. Scans only the 282 * words the active range spans, where head and tail padding is kept zero, so 283 * no per-cid range check is needed. 284 */ 285 #define scx_cmask_for_each_cid(cid, m) \ 286 for (u64 __bs = (m)->base & ~63u, __wi = 0, \ 287 __nw = scx_cmask_nr_used_words(m); \ 288 __wi < __nw; __wi++) \ 289 for (u64 __w = READ_ONCE((m)->bits[__wi]); \ 290 __w && ((cid) = __bs + __wi * 64 + __ffs64(__w), true); \ 291 __w &= __w - 1) 292 293 /* 294 * scx_cpu_arg() wraps a cpu arg being handed to an SCX op. For cid-form 295 * schedulers it resolves to the matching cid; for cpu-form it passes @cpu 296 * through. scx_cpu_ret() is the inverse for a cpu/cid returned from an op 297 * (currently only ops.select_cpu); it validates the BPF-supplied cid and 298 * triggers scx_error() on @sch if invalid. 299 */ 300 static inline s32 scx_cpu_arg(s32 cpu) 301 { 302 if (scx_is_cid_type()) 303 return __scx_cpu_to_cid(cpu); 304 return cpu; 305 } 306 307 static inline s32 scx_cpu_ret(struct scx_sched *sch, s32 cpu_or_cid) 308 { 309 if (cpu_or_cid < 0 || !scx_is_cid_type()) 310 return cpu_or_cid; 311 return scx_cid_to_cpu(sch, cpu_or_cid); 312 } 313 314 int scx_cmask_ref_init(struct scx_sched *sch, const struct scx_cmask *src, 315 struct scx_cmask_ref *ref); 316 void scx_cmask_ref_init_kern(struct scx_sched *sch, struct scx_cmask *m, 317 u32 base, u32 nr_cids, struct scx_cmask_ref *ref); 318 void scx_cmask_ref_shard(const struct scx_cmask_ref *ref, s32 shard_idx, 319 struct scx_cmask *out); 320 void scx_cmask_ref_from_cpumask(const struct scx_cmask_ref *ref, 321 const struct cpumask *cpumask); 322 void scx_cmask_ref_or(const struct scx_cmask_ref *ref, const struct scx_cmask *src); 323 void scx_cmask_ref_copy(const struct scx_cmask_ref *ref, const struct scx_cmask *src); 324 325 #endif /* _KERNEL_SCHED_EXT_CID_H */ 326