1*bba2c361STejun Heo // SPDX-License-Identifier: GPL-2.0 2*bba2c361STejun Heo /* 3*bba2c361STejun Heo * BPF extensible scheduler class: Documentation/scheduler/sched-ext.rst 4*bba2c361STejun Heo * 5*bba2c361STejun Heo * Built-in idle CPU tracking policy. 6*bba2c361STejun Heo * 7*bba2c361STejun Heo * Copyright (c) 2022 Meta Platforms, Inc. and affiliates. 8*bba2c361STejun Heo * Copyright (c) 2022 Tejun Heo <tj@kernel.org> 9*bba2c361STejun Heo * Copyright (c) 2022 David Vernet <dvernet@meta.com> 10*bba2c361STejun Heo * Copyright (c) 2024 Andrea Righi <arighi@nvidia.com> 11*bba2c361STejun Heo */ 12*bba2c361STejun Heo 13*bba2c361STejun Heo /* Enable/disable built-in idle CPU selection policy */ 14*bba2c361STejun Heo static DEFINE_STATIC_KEY_FALSE(scx_builtin_idle_enabled); 15*bba2c361STejun Heo 16*bba2c361STejun Heo /* Enable/disable per-node idle cpumasks */ 17*bba2c361STejun Heo static DEFINE_STATIC_KEY_FALSE(scx_builtin_idle_per_node); 18*bba2c361STejun Heo 19*bba2c361STejun Heo /* Enable/disable LLC aware optimizations */ 20*bba2c361STejun Heo static DEFINE_STATIC_KEY_FALSE(scx_selcpu_topo_llc); 21*bba2c361STejun Heo 22*bba2c361STejun Heo /* Enable/disable NUMA aware optimizations */ 23*bba2c361STejun Heo static DEFINE_STATIC_KEY_FALSE(scx_selcpu_topo_numa); 24*bba2c361STejun Heo 25*bba2c361STejun Heo /* 26*bba2c361STejun Heo * cpumasks to track idle CPUs within each NUMA node. 27*bba2c361STejun Heo * 28*bba2c361STejun Heo * If SCX_OPS_BUILTIN_IDLE_PER_NODE is not enabled, a single global cpumask 29*bba2c361STejun Heo * from is used to track all the idle CPUs in the system. 30*bba2c361STejun Heo */ 31*bba2c361STejun Heo struct scx_idle_cpus { 32*bba2c361STejun Heo cpumask_var_t cpu; 33*bba2c361STejun Heo cpumask_var_t smt; 34*bba2c361STejun Heo }; 35*bba2c361STejun Heo 36*bba2c361STejun Heo /* 37*bba2c361STejun Heo * Global host-wide idle cpumasks (used when SCX_OPS_BUILTIN_IDLE_PER_NODE 38*bba2c361STejun Heo * is not enabled). 39*bba2c361STejun Heo */ 40*bba2c361STejun Heo static struct scx_idle_cpus scx_idle_global_masks; 41*bba2c361STejun Heo 42*bba2c361STejun Heo /* 43*bba2c361STejun Heo * Per-node idle cpumasks. 44*bba2c361STejun Heo */ 45*bba2c361STejun Heo static struct scx_idle_cpus **scx_idle_node_masks; 46*bba2c361STejun Heo 47*bba2c361STejun Heo /* 48*bba2c361STejun Heo * Local per-CPU cpumasks (used to generate temporary idle cpumasks). 49*bba2c361STejun Heo */ 50*bba2c361STejun Heo static DEFINE_PER_CPU(cpumask_var_t, local_idle_cpumask); 51*bba2c361STejun Heo static DEFINE_PER_CPU(cpumask_var_t, local_llc_idle_cpumask); 52*bba2c361STejun Heo static DEFINE_PER_CPU(cpumask_var_t, local_numa_idle_cpumask); 53*bba2c361STejun Heo 54*bba2c361STejun Heo /* 55*bba2c361STejun Heo * Return the idle masks associated to a target @node. 56*bba2c361STejun Heo * 57*bba2c361STejun Heo * NUMA_NO_NODE identifies the global idle cpumask. 58*bba2c361STejun Heo */ 59*bba2c361STejun Heo static struct scx_idle_cpus *idle_cpumask(int node) 60*bba2c361STejun Heo { 61*bba2c361STejun Heo return node == NUMA_NO_NODE ? &scx_idle_global_masks : scx_idle_node_masks[node]; 62*bba2c361STejun Heo } 63*bba2c361STejun Heo 64*bba2c361STejun Heo /* 65*bba2c361STejun Heo * Returns the NUMA node ID associated with a @cpu, or NUMA_NO_NODE if 66*bba2c361STejun Heo * per-node idle cpumasks are disabled. 67*bba2c361STejun Heo */ 68*bba2c361STejun Heo static int scx_cpu_node_if_enabled(int cpu) 69*bba2c361STejun Heo { 70*bba2c361STejun Heo if (!static_branch_maybe(CONFIG_NUMA, &scx_builtin_idle_per_node)) 71*bba2c361STejun Heo return NUMA_NO_NODE; 72*bba2c361STejun Heo 73*bba2c361STejun Heo return cpu_to_node(cpu); 74*bba2c361STejun Heo } 75*bba2c361STejun Heo 76*bba2c361STejun Heo static bool scx_idle_test_and_clear_cpu(int cpu) 77*bba2c361STejun Heo { 78*bba2c361STejun Heo int node = scx_cpu_node_if_enabled(cpu); 79*bba2c361STejun Heo struct cpumask *idle_cpus = idle_cpumask(node)->cpu; 80*bba2c361STejun Heo 81*bba2c361STejun Heo /* 82*bba2c361STejun Heo * SMT mask should be cleared whether we can claim @cpu or not. The SMT 83*bba2c361STejun Heo * cluster is not wholly idle either way. This also prevents 84*bba2c361STejun Heo * scx_pick_idle_cpu() from getting caught in an infinite loop. 85*bba2c361STejun Heo */ 86*bba2c361STejun Heo if (sched_smt_active()) { 87*bba2c361STejun Heo const struct cpumask *smt = cpu_smt_mask(cpu); 88*bba2c361STejun Heo struct cpumask *idle_smts = idle_cpumask(node)->smt; 89*bba2c361STejun Heo 90*bba2c361STejun Heo /* 91*bba2c361STejun Heo * If offline, @cpu is not its own sibling and 92*bba2c361STejun Heo * scx_pick_idle_cpu() can get caught in an infinite loop as 93*bba2c361STejun Heo * @cpu is never cleared from the idle SMT mask. Ensure that 94*bba2c361STejun Heo * @cpu is eventually cleared. 95*bba2c361STejun Heo * 96*bba2c361STejun Heo * NOTE: Use cpumask_intersects() and cpumask_test_cpu() to 97*bba2c361STejun Heo * reduce memory writes, which may help alleviate cache 98*bba2c361STejun Heo * coherence pressure. 99*bba2c361STejun Heo */ 100*bba2c361STejun Heo if (cpumask_intersects(smt, idle_smts)) 101*bba2c361STejun Heo cpumask_andnot(idle_smts, idle_smts, smt); 102*bba2c361STejun Heo else if (cpumask_test_cpu(cpu, idle_smts)) 103*bba2c361STejun Heo __cpumask_clear_cpu(cpu, idle_smts); 104*bba2c361STejun Heo } 105*bba2c361STejun Heo 106*bba2c361STejun Heo return cpumask_test_and_clear_cpu(cpu, idle_cpus); 107*bba2c361STejun Heo } 108*bba2c361STejun Heo 109*bba2c361STejun Heo /* 110*bba2c361STejun Heo * Pick an idle CPU in a specific NUMA node. 111*bba2c361STejun Heo */ 112*bba2c361STejun Heo static s32 pick_idle_cpu_in_node(const struct cpumask *cpus_allowed, int node, u64 flags) 113*bba2c361STejun Heo { 114*bba2c361STejun Heo int cpu; 115*bba2c361STejun Heo 116*bba2c361STejun Heo retry: 117*bba2c361STejun Heo if (sched_smt_active()) { 118*bba2c361STejun Heo cpu = cpumask_any_and_distribute(idle_cpumask(node)->smt, cpus_allowed); 119*bba2c361STejun Heo if (cpu < nr_cpu_ids) 120*bba2c361STejun Heo goto found; 121*bba2c361STejun Heo 122*bba2c361STejun Heo if (flags & SCX_PICK_IDLE_CORE) 123*bba2c361STejun Heo return -EBUSY; 124*bba2c361STejun Heo } 125*bba2c361STejun Heo 126*bba2c361STejun Heo cpu = cpumask_any_and_distribute(idle_cpumask(node)->cpu, cpus_allowed); 127*bba2c361STejun Heo if (cpu >= nr_cpu_ids) 128*bba2c361STejun Heo return -EBUSY; 129*bba2c361STejun Heo 130*bba2c361STejun Heo found: 131*bba2c361STejun Heo if (scx_idle_test_and_clear_cpu(cpu)) 132*bba2c361STejun Heo return cpu; 133*bba2c361STejun Heo else 134*bba2c361STejun Heo goto retry; 135*bba2c361STejun Heo } 136*bba2c361STejun Heo 137*bba2c361STejun Heo #ifdef CONFIG_NUMA 138*bba2c361STejun Heo /* 139*bba2c361STejun Heo * Tracks nodes that have not yet been visited when searching for an idle 140*bba2c361STejun Heo * CPU across all available nodes. 141*bba2c361STejun Heo */ 142*bba2c361STejun Heo static DEFINE_PER_CPU(nodemask_t, per_cpu_unvisited); 143*bba2c361STejun Heo 144*bba2c361STejun Heo /* 145*bba2c361STejun Heo * Search for an idle CPU across all nodes, excluding @node. 146*bba2c361STejun Heo */ 147*bba2c361STejun Heo static s32 pick_idle_cpu_from_online_nodes(const struct cpumask *cpus_allowed, int node, u64 flags) 148*bba2c361STejun Heo { 149*bba2c361STejun Heo nodemask_t *unvisited; 150*bba2c361STejun Heo s32 cpu = -EBUSY; 151*bba2c361STejun Heo 152*bba2c361STejun Heo preempt_disable(); 153*bba2c361STejun Heo unvisited = this_cpu_ptr(&per_cpu_unvisited); 154*bba2c361STejun Heo 155*bba2c361STejun Heo /* 156*bba2c361STejun Heo * Restrict the search to the online nodes (excluding the current 157*bba2c361STejun Heo * node that has been visited already). 158*bba2c361STejun Heo */ 159*bba2c361STejun Heo nodes_copy(*unvisited, node_states[N_ONLINE]); 160*bba2c361STejun Heo node_clear(node, *unvisited); 161*bba2c361STejun Heo 162*bba2c361STejun Heo /* 163*bba2c361STejun Heo * Traverse all nodes in order of increasing distance, starting 164*bba2c361STejun Heo * from @node. 165*bba2c361STejun Heo * 166*bba2c361STejun Heo * This loop is O(N^2), with N being the amount of NUMA nodes, 167*bba2c361STejun Heo * which might be quite expensive in large NUMA systems. However, 168*bba2c361STejun Heo * this complexity comes into play only when a scheduler enables 169*bba2c361STejun Heo * SCX_OPS_BUILTIN_IDLE_PER_NODE and it's requesting an idle CPU 170*bba2c361STejun Heo * without specifying a target NUMA node, so it shouldn't be a 171*bba2c361STejun Heo * bottleneck is most cases. 172*bba2c361STejun Heo * 173*bba2c361STejun Heo * As a future optimization we may want to cache the list of nodes 174*bba2c361STejun Heo * in a per-node array, instead of actually traversing them every 175*bba2c361STejun Heo * time. 176*bba2c361STejun Heo */ 177*bba2c361STejun Heo for_each_node_numadist(node, *unvisited) { 178*bba2c361STejun Heo cpu = pick_idle_cpu_in_node(cpus_allowed, node, flags); 179*bba2c361STejun Heo if (cpu >= 0) 180*bba2c361STejun Heo break; 181*bba2c361STejun Heo } 182*bba2c361STejun Heo preempt_enable(); 183*bba2c361STejun Heo 184*bba2c361STejun Heo return cpu; 185*bba2c361STejun Heo } 186*bba2c361STejun Heo #else 187*bba2c361STejun Heo static inline s32 188*bba2c361STejun Heo pick_idle_cpu_from_online_nodes(const struct cpumask *cpus_allowed, int node, u64 flags) 189*bba2c361STejun Heo { 190*bba2c361STejun Heo return -EBUSY; 191*bba2c361STejun Heo } 192*bba2c361STejun Heo #endif 193*bba2c361STejun Heo 194*bba2c361STejun Heo /* 195*bba2c361STejun Heo * Find an idle CPU in the system, starting from @node. 196*bba2c361STejun Heo */ 197*bba2c361STejun Heo static s32 scx_pick_idle_cpu(const struct cpumask *cpus_allowed, int node, u64 flags) 198*bba2c361STejun Heo { 199*bba2c361STejun Heo s32 cpu; 200*bba2c361STejun Heo 201*bba2c361STejun Heo /* 202*bba2c361STejun Heo * Always search in the starting node first (this is an 203*bba2c361STejun Heo * optimization that can save some cycles even when the search is 204*bba2c361STejun Heo * not limited to a single node). 205*bba2c361STejun Heo */ 206*bba2c361STejun Heo cpu = pick_idle_cpu_in_node(cpus_allowed, node, flags); 207*bba2c361STejun Heo if (cpu >= 0) 208*bba2c361STejun Heo return cpu; 209*bba2c361STejun Heo 210*bba2c361STejun Heo /* 211*bba2c361STejun Heo * Stop the search if we are using only a single global cpumask 212*bba2c361STejun Heo * (NUMA_NO_NODE) or if the search is restricted to the first node 213*bba2c361STejun Heo * only. 214*bba2c361STejun Heo */ 215*bba2c361STejun Heo if (node == NUMA_NO_NODE || flags & SCX_PICK_IDLE_IN_NODE) 216*bba2c361STejun Heo return -EBUSY; 217*bba2c361STejun Heo 218*bba2c361STejun Heo /* 219*bba2c361STejun Heo * Extend the search to the other online nodes. 220*bba2c361STejun Heo */ 221*bba2c361STejun Heo return pick_idle_cpu_from_online_nodes(cpus_allowed, node, flags); 222*bba2c361STejun Heo } 223*bba2c361STejun Heo 224*bba2c361STejun Heo /* 225*bba2c361STejun Heo * Return the amount of CPUs in the same LLC domain of @cpu (or zero if the LLC 226*bba2c361STejun Heo * domain is not defined). 227*bba2c361STejun Heo */ 228*bba2c361STejun Heo static unsigned int llc_weight(s32 cpu) 229*bba2c361STejun Heo { 230*bba2c361STejun Heo struct sched_domain *sd; 231*bba2c361STejun Heo 232*bba2c361STejun Heo sd = rcu_dereference(per_cpu(sd_llc, cpu)); 233*bba2c361STejun Heo if (!sd) 234*bba2c361STejun Heo return 0; 235*bba2c361STejun Heo 236*bba2c361STejun Heo return sd->span_weight; 237*bba2c361STejun Heo } 238*bba2c361STejun Heo 239*bba2c361STejun Heo /* 240*bba2c361STejun Heo * Return the cpumask representing the LLC domain of @cpu (or NULL if the LLC 241*bba2c361STejun Heo * domain is not defined). 242*bba2c361STejun Heo */ 243*bba2c361STejun Heo static struct cpumask *llc_span(s32 cpu) 244*bba2c361STejun Heo { 245*bba2c361STejun Heo struct sched_domain *sd; 246*bba2c361STejun Heo 247*bba2c361STejun Heo sd = rcu_dereference(per_cpu(sd_llc, cpu)); 248*bba2c361STejun Heo if (!sd) 249*bba2c361STejun Heo return NULL; 250*bba2c361STejun Heo 251*bba2c361STejun Heo return sched_domain_span(sd); 252*bba2c361STejun Heo } 253*bba2c361STejun Heo 254*bba2c361STejun Heo /* 255*bba2c361STejun Heo * Return the amount of CPUs in the same NUMA domain of @cpu (or zero if the 256*bba2c361STejun Heo * NUMA domain is not defined). 257*bba2c361STejun Heo */ 258*bba2c361STejun Heo static unsigned int numa_weight(s32 cpu) 259*bba2c361STejun Heo { 260*bba2c361STejun Heo struct sched_domain *sd; 261*bba2c361STejun Heo struct sched_group *sg; 262*bba2c361STejun Heo 263*bba2c361STejun Heo sd = rcu_dereference(per_cpu(sd_numa, cpu)); 264*bba2c361STejun Heo if (!sd) 265*bba2c361STejun Heo return 0; 266*bba2c361STejun Heo sg = sd->groups; 267*bba2c361STejun Heo if (!sg) 268*bba2c361STejun Heo return 0; 269*bba2c361STejun Heo 270*bba2c361STejun Heo return sg->group_weight; 271*bba2c361STejun Heo } 272*bba2c361STejun Heo 273*bba2c361STejun Heo /* 274*bba2c361STejun Heo * Return the cpumask representing the NUMA domain of @cpu (or NULL if the NUMA 275*bba2c361STejun Heo * domain is not defined). 276*bba2c361STejun Heo */ 277*bba2c361STejun Heo static struct cpumask *numa_span(s32 cpu) 278*bba2c361STejun Heo { 279*bba2c361STejun Heo struct sched_domain *sd; 280*bba2c361STejun Heo struct sched_group *sg; 281*bba2c361STejun Heo 282*bba2c361STejun Heo sd = rcu_dereference(per_cpu(sd_numa, cpu)); 283*bba2c361STejun Heo if (!sd) 284*bba2c361STejun Heo return NULL; 285*bba2c361STejun Heo sg = sd->groups; 286*bba2c361STejun Heo if (!sg) 287*bba2c361STejun Heo return NULL; 288*bba2c361STejun Heo 289*bba2c361STejun Heo return sched_group_span(sg); 290*bba2c361STejun Heo } 291*bba2c361STejun Heo 292*bba2c361STejun Heo /* 293*bba2c361STejun Heo * Return true if the LLC domains do not perfectly overlap with the NUMA 294*bba2c361STejun Heo * domains, false otherwise. 295*bba2c361STejun Heo */ 296*bba2c361STejun Heo static bool llc_numa_mismatch(void) 297*bba2c361STejun Heo { 298*bba2c361STejun Heo int cpu; 299*bba2c361STejun Heo 300*bba2c361STejun Heo /* 301*bba2c361STejun Heo * We need to scan all online CPUs to verify whether their scheduling 302*bba2c361STejun Heo * domains overlap. 303*bba2c361STejun Heo * 304*bba2c361STejun Heo * While it is rare to encounter architectures with asymmetric NUMA 305*bba2c361STejun Heo * topologies, CPU hotplugging or virtualized environments can result 306*bba2c361STejun Heo * in asymmetric configurations. 307*bba2c361STejun Heo * 308*bba2c361STejun Heo * For example: 309*bba2c361STejun Heo * 310*bba2c361STejun Heo * NUMA 0: 311*bba2c361STejun Heo * - LLC 0: cpu0..cpu7 312*bba2c361STejun Heo * - LLC 1: cpu8..cpu15 [offline] 313*bba2c361STejun Heo * 314*bba2c361STejun Heo * NUMA 1: 315*bba2c361STejun Heo * - LLC 0: cpu16..cpu23 316*bba2c361STejun Heo * - LLC 1: cpu24..cpu31 317*bba2c361STejun Heo * 318*bba2c361STejun Heo * In this case, if we only check the first online CPU (cpu0), we might 319*bba2c361STejun Heo * incorrectly assume that the LLC and NUMA domains are fully 320*bba2c361STejun Heo * overlapping, which is incorrect (as NUMA 1 has two distinct LLC 321*bba2c361STejun Heo * domains). 322*bba2c361STejun Heo */ 323*bba2c361STejun Heo for_each_online_cpu(cpu) 324*bba2c361STejun Heo if (llc_weight(cpu) != numa_weight(cpu)) 325*bba2c361STejun Heo return true; 326*bba2c361STejun Heo 327*bba2c361STejun Heo return false; 328*bba2c361STejun Heo } 329*bba2c361STejun Heo 330*bba2c361STejun Heo /* 331*bba2c361STejun Heo * Initialize topology-aware scheduling. 332*bba2c361STejun Heo * 333*bba2c361STejun Heo * Detect if the system has multiple LLC or multiple NUMA domains and enable 334*bba2c361STejun Heo * cache-aware / NUMA-aware scheduling optimizations in the default CPU idle 335*bba2c361STejun Heo * selection policy. 336*bba2c361STejun Heo * 337*bba2c361STejun Heo * Assumption: the kernel's internal topology representation assumes that each 338*bba2c361STejun Heo * CPU belongs to a single LLC domain, and that each LLC domain is entirely 339*bba2c361STejun Heo * contained within a single NUMA node. 340*bba2c361STejun Heo */ 341*bba2c361STejun Heo void scx_idle_update_selcpu_topology(struct sched_ext_ops *ops) 342*bba2c361STejun Heo { 343*bba2c361STejun Heo bool enable_llc = false, enable_numa = false; 344*bba2c361STejun Heo unsigned int nr_cpus; 345*bba2c361STejun Heo s32 cpu = cpumask_first(cpu_online_mask); 346*bba2c361STejun Heo 347*bba2c361STejun Heo /* 348*bba2c361STejun Heo * Enable LLC domain optimization only when there are multiple LLC 349*bba2c361STejun Heo * domains among the online CPUs. If all online CPUs are part of a 350*bba2c361STejun Heo * single LLC domain, the idle CPU selection logic can choose any 351*bba2c361STejun Heo * online CPU without bias. 352*bba2c361STejun Heo * 353*bba2c361STejun Heo * Note that it is sufficient to check the LLC domain of the first 354*bba2c361STejun Heo * online CPU to determine whether a single LLC domain includes all 355*bba2c361STejun Heo * CPUs. 356*bba2c361STejun Heo */ 357*bba2c361STejun Heo rcu_read_lock(); 358*bba2c361STejun Heo nr_cpus = llc_weight(cpu); 359*bba2c361STejun Heo if (nr_cpus > 0) { 360*bba2c361STejun Heo if (nr_cpus < num_online_cpus()) 361*bba2c361STejun Heo enable_llc = true; 362*bba2c361STejun Heo pr_debug("sched_ext: LLC=%*pb weight=%u\n", 363*bba2c361STejun Heo cpumask_pr_args(llc_span(cpu)), llc_weight(cpu)); 364*bba2c361STejun Heo } 365*bba2c361STejun Heo 366*bba2c361STejun Heo /* 367*bba2c361STejun Heo * Enable NUMA optimization only when there are multiple NUMA domains 368*bba2c361STejun Heo * among the online CPUs and the NUMA domains don't perfectly overlap 369*bba2c361STejun Heo * with the LLC domains. 370*bba2c361STejun Heo * 371*bba2c361STejun Heo * If all CPUs belong to the same NUMA node and the same LLC domain, 372*bba2c361STejun Heo * enabling both NUMA and LLC optimizations is unnecessary, as checking 373*bba2c361STejun Heo * for an idle CPU in the same domain twice is redundant. 374*bba2c361STejun Heo * 375*bba2c361STejun Heo * If SCX_OPS_BUILTIN_IDLE_PER_NODE is enabled ignore the NUMA 376*bba2c361STejun Heo * optimization, as we would naturally select idle CPUs within 377*bba2c361STejun Heo * specific NUMA nodes querying the corresponding per-node cpumask. 378*bba2c361STejun Heo */ 379*bba2c361STejun Heo if (!(ops->flags & SCX_OPS_BUILTIN_IDLE_PER_NODE)) { 380*bba2c361STejun Heo nr_cpus = numa_weight(cpu); 381*bba2c361STejun Heo if (nr_cpus > 0) { 382*bba2c361STejun Heo if (nr_cpus < num_online_cpus() && llc_numa_mismatch()) 383*bba2c361STejun Heo enable_numa = true; 384*bba2c361STejun Heo pr_debug("sched_ext: NUMA=%*pb weight=%u\n", 385*bba2c361STejun Heo cpumask_pr_args(numa_span(cpu)), nr_cpus); 386*bba2c361STejun Heo } 387*bba2c361STejun Heo } 388*bba2c361STejun Heo rcu_read_unlock(); 389*bba2c361STejun Heo 390*bba2c361STejun Heo pr_debug("sched_ext: LLC idle selection %s\n", 391*bba2c361STejun Heo str_enabled_disabled(enable_llc)); 392*bba2c361STejun Heo pr_debug("sched_ext: NUMA idle selection %s\n", 393*bba2c361STejun Heo str_enabled_disabled(enable_numa)); 394*bba2c361STejun Heo 395*bba2c361STejun Heo if (enable_llc) 396*bba2c361STejun Heo static_branch_enable_cpuslocked(&scx_selcpu_topo_llc); 397*bba2c361STejun Heo else 398*bba2c361STejun Heo static_branch_disable_cpuslocked(&scx_selcpu_topo_llc); 399*bba2c361STejun Heo if (enable_numa) 400*bba2c361STejun Heo static_branch_enable_cpuslocked(&scx_selcpu_topo_numa); 401*bba2c361STejun Heo else 402*bba2c361STejun Heo static_branch_disable_cpuslocked(&scx_selcpu_topo_numa); 403*bba2c361STejun Heo } 404*bba2c361STejun Heo 405*bba2c361STejun Heo /* 406*bba2c361STejun Heo * Return true if @p can run on all possible CPUs, false otherwise. 407*bba2c361STejun Heo */ 408*bba2c361STejun Heo static inline bool task_affinity_all(const struct task_struct *p) 409*bba2c361STejun Heo { 410*bba2c361STejun Heo return p->nr_cpus_allowed >= num_possible_cpus(); 411*bba2c361STejun Heo } 412*bba2c361STejun Heo 413*bba2c361STejun Heo /* 414*bba2c361STejun Heo * Built-in CPU idle selection policy: 415*bba2c361STejun Heo * 416*bba2c361STejun Heo * 1. Prioritize full-idle cores: 417*bba2c361STejun Heo * - always prioritize CPUs from fully idle cores (both logical CPUs are 418*bba2c361STejun Heo * idle) to avoid interference caused by SMT. 419*bba2c361STejun Heo * 420*bba2c361STejun Heo * 2. Reuse the same CPU: 421*bba2c361STejun Heo * - prefer the last used CPU to take advantage of cached data (L1, L2) and 422*bba2c361STejun Heo * branch prediction optimizations. 423*bba2c361STejun Heo * 424*bba2c361STejun Heo * 3. Prefer @prev_cpu's SMT sibling: 425*bba2c361STejun Heo * - if @prev_cpu is busy and no fully idle core is available, try to 426*bba2c361STejun Heo * place the task on an idle SMT sibling of @prev_cpu; keeping the 427*bba2c361STejun Heo * task on the same core makes migration cheaper, preserves L1 cache 428*bba2c361STejun Heo * locality and reduces wakeup latency. 429*bba2c361STejun Heo * 430*bba2c361STejun Heo * 4. Pick a CPU within the same LLC (Last-Level Cache): 431*bba2c361STejun Heo * - if the above conditions aren't met, pick a CPU that shares the same 432*bba2c361STejun Heo * LLC, if the LLC domain is a subset of @cpus_allowed, to maintain 433*bba2c361STejun Heo * cache locality. 434*bba2c361STejun Heo * 435*bba2c361STejun Heo * 5. Pick a CPU within the same NUMA node, if enabled: 436*bba2c361STejun Heo * - choose a CPU from the same NUMA node, if the node cpumask is a 437*bba2c361STejun Heo * subset of @cpus_allowed, to reduce memory access latency. 438*bba2c361STejun Heo * 439*bba2c361STejun Heo * 6. Pick any idle CPU within the @cpus_allowed domain. 440*bba2c361STejun Heo * 441*bba2c361STejun Heo * Step 4 and 5 are performed only if the system has, respectively, 442*bba2c361STejun Heo * multiple LLCs / multiple NUMA nodes (see scx_selcpu_topo_llc and 443*bba2c361STejun Heo * scx_selcpu_topo_numa) and they don't contain the same subset of CPUs. 444*bba2c361STejun Heo * 445*bba2c361STejun Heo * If %SCX_OPS_BUILTIN_IDLE_PER_NODE is enabled, the search will always 446*bba2c361STejun Heo * begin in @prev_cpu's node and proceed to other nodes in order of 447*bba2c361STejun Heo * increasing distance. 448*bba2c361STejun Heo * 449*bba2c361STejun Heo * Return the picked CPU if idle, or a negative value otherwise. 450*bba2c361STejun Heo * 451*bba2c361STejun Heo * NOTE: tasks that can only run on 1 CPU are excluded by this logic, because 452*bba2c361STejun Heo * we never call ops.select_cpu() for them, see select_task_rq(). 453*bba2c361STejun Heo */ 454*bba2c361STejun Heo s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags, 455*bba2c361STejun Heo const struct cpumask *cpus_allowed, u64 flags) 456*bba2c361STejun Heo { 457*bba2c361STejun Heo const struct cpumask *llc_cpus = NULL, *numa_cpus = NULL; 458*bba2c361STejun Heo const struct cpumask *allowed = cpus_allowed ?: p->cpus_ptr; 459*bba2c361STejun Heo int node = scx_cpu_node_if_enabled(prev_cpu); 460*bba2c361STejun Heo bool is_prev_allowed; 461*bba2c361STejun Heo s32 cpu; 462*bba2c361STejun Heo 463*bba2c361STejun Heo preempt_disable(); 464*bba2c361STejun Heo 465*bba2c361STejun Heo /* 466*bba2c361STejun Heo * Determine the subset of CPUs usable by @p within @cpus_allowed. 467*bba2c361STejun Heo */ 468*bba2c361STejun Heo if (allowed != p->cpus_ptr) { 469*bba2c361STejun Heo struct cpumask *local_cpus = this_cpu_cpumask_var_ptr(local_idle_cpumask); 470*bba2c361STejun Heo 471*bba2c361STejun Heo if (task_affinity_all(p)) { 472*bba2c361STejun Heo allowed = cpus_allowed; 473*bba2c361STejun Heo } else if (cpumask_and(local_cpus, cpus_allowed, p->cpus_ptr)) { 474*bba2c361STejun Heo allowed = local_cpus; 475*bba2c361STejun Heo } else { 476*bba2c361STejun Heo cpu = -EBUSY; 477*bba2c361STejun Heo goto out_enable; 478*bba2c361STejun Heo } 479*bba2c361STejun Heo } 480*bba2c361STejun Heo 481*bba2c361STejun Heo /* 482*bba2c361STejun Heo * Check whether @prev_cpu is still within the allowed set. If not, 483*bba2c361STejun Heo * we can still try selecting a nearby CPU. 484*bba2c361STejun Heo */ 485*bba2c361STejun Heo is_prev_allowed = cpumask_test_cpu(prev_cpu, allowed); 486*bba2c361STejun Heo 487*bba2c361STejun Heo /* 488*bba2c361STejun Heo * This is necessary to protect llc_cpus. 489*bba2c361STejun Heo */ 490*bba2c361STejun Heo rcu_read_lock(); 491*bba2c361STejun Heo 492*bba2c361STejun Heo /* 493*bba2c361STejun Heo * Determine the subset of CPUs that the task can use in its 494*bba2c361STejun Heo * current LLC and node. 495*bba2c361STejun Heo * 496*bba2c361STejun Heo * If the task can run on all CPUs, use the node and LLC cpumasks 497*bba2c361STejun Heo * directly. 498*bba2c361STejun Heo */ 499*bba2c361STejun Heo if (static_branch_maybe(CONFIG_NUMA, &scx_selcpu_topo_numa)) { 500*bba2c361STejun Heo struct cpumask *local_cpus = this_cpu_cpumask_var_ptr(local_numa_idle_cpumask); 501*bba2c361STejun Heo const struct cpumask *cpus = numa_span(prev_cpu); 502*bba2c361STejun Heo 503*bba2c361STejun Heo if (allowed == p->cpus_ptr && task_affinity_all(p)) 504*bba2c361STejun Heo numa_cpus = cpus; 505*bba2c361STejun Heo else if (cpus && cpumask_and(local_cpus, allowed, cpus)) 506*bba2c361STejun Heo numa_cpus = local_cpus; 507*bba2c361STejun Heo } 508*bba2c361STejun Heo 509*bba2c361STejun Heo if (static_branch_maybe(CONFIG_SCHED_MC, &scx_selcpu_topo_llc)) { 510*bba2c361STejun Heo struct cpumask *local_cpus = this_cpu_cpumask_var_ptr(local_llc_idle_cpumask); 511*bba2c361STejun Heo const struct cpumask *cpus = llc_span(prev_cpu); 512*bba2c361STejun Heo 513*bba2c361STejun Heo if (allowed == p->cpus_ptr && task_affinity_all(p)) 514*bba2c361STejun Heo llc_cpus = cpus; 515*bba2c361STejun Heo else if (cpus && cpumask_and(local_cpus, allowed, cpus)) 516*bba2c361STejun Heo llc_cpus = local_cpus; 517*bba2c361STejun Heo } 518*bba2c361STejun Heo 519*bba2c361STejun Heo /* 520*bba2c361STejun Heo * If WAKE_SYNC, try to migrate the wakee to the waker's CPU. 521*bba2c361STejun Heo */ 522*bba2c361STejun Heo if (wake_flags & SCX_WAKE_SYNC) { 523*bba2c361STejun Heo int waker_node; 524*bba2c361STejun Heo 525*bba2c361STejun Heo /* 526*bba2c361STejun Heo * If the waker's CPU is cache affine and prev_cpu is idle, 527*bba2c361STejun Heo * then avoid a migration. 528*bba2c361STejun Heo */ 529*bba2c361STejun Heo cpu = smp_processor_id(); 530*bba2c361STejun Heo if (is_prev_allowed && cpus_share_cache(cpu, prev_cpu) && 531*bba2c361STejun Heo scx_idle_test_and_clear_cpu(prev_cpu)) { 532*bba2c361STejun Heo cpu = prev_cpu; 533*bba2c361STejun Heo goto out_unlock; 534*bba2c361STejun Heo } 535*bba2c361STejun Heo 536*bba2c361STejun Heo /* 537*bba2c361STejun Heo * If the waker's local DSQ is empty, and the system is under 538*bba2c361STejun Heo * utilized, try to wake up @p to the local DSQ of the waker. 539*bba2c361STejun Heo * 540*bba2c361STejun Heo * Checking only for an empty local DSQ is insufficient as it 541*bba2c361STejun Heo * could give the wakee an unfair advantage when the system is 542*bba2c361STejun Heo * oversaturated. 543*bba2c361STejun Heo * 544*bba2c361STejun Heo * Checking only for the presence of idle CPUs is also 545*bba2c361STejun Heo * insufficient as the local DSQ of the waker could have tasks 546*bba2c361STejun Heo * piled up on it even if there is an idle core elsewhere on 547*bba2c361STejun Heo * the system. 548*bba2c361STejun Heo */ 549*bba2c361STejun Heo waker_node = scx_cpu_node_if_enabled(cpu); 550*bba2c361STejun Heo if (!(current->flags & PF_EXITING) && 551*bba2c361STejun Heo cpu_rq(cpu)->scx.local_dsq.nr == 0 && 552*bba2c361STejun Heo (!(flags & SCX_PICK_IDLE_IN_NODE) || (waker_node == node)) && 553*bba2c361STejun Heo !cpumask_empty(idle_cpumask(waker_node)->cpu)) { 554*bba2c361STejun Heo if (cpumask_test_cpu(cpu, allowed)) 555*bba2c361STejun Heo goto out_unlock; 556*bba2c361STejun Heo } 557*bba2c361STejun Heo } 558*bba2c361STejun Heo 559*bba2c361STejun Heo /* 560*bba2c361STejun Heo * If CPU has SMT, any wholly idle CPU is likely a better pick than 561*bba2c361STejun Heo * partially idle @prev_cpu. 562*bba2c361STejun Heo */ 563*bba2c361STejun Heo if (sched_smt_active()) { 564*bba2c361STejun Heo /* 565*bba2c361STejun Heo * Keep using @prev_cpu if it's part of a fully idle core. 566*bba2c361STejun Heo */ 567*bba2c361STejun Heo if (is_prev_allowed && 568*bba2c361STejun Heo cpumask_test_cpu(prev_cpu, idle_cpumask(node)->smt) && 569*bba2c361STejun Heo scx_idle_test_and_clear_cpu(prev_cpu)) { 570*bba2c361STejun Heo cpu = prev_cpu; 571*bba2c361STejun Heo goto out_unlock; 572*bba2c361STejun Heo } 573*bba2c361STejun Heo 574*bba2c361STejun Heo /* 575*bba2c361STejun Heo * Search for any fully idle core in the same LLC domain. 576*bba2c361STejun Heo */ 577*bba2c361STejun Heo if (llc_cpus) { 578*bba2c361STejun Heo cpu = pick_idle_cpu_in_node(llc_cpus, node, SCX_PICK_IDLE_CORE); 579*bba2c361STejun Heo if (cpu >= 0) 580*bba2c361STejun Heo goto out_unlock; 581*bba2c361STejun Heo } 582*bba2c361STejun Heo 583*bba2c361STejun Heo /* 584*bba2c361STejun Heo * Search for any fully idle core in the same NUMA node. 585*bba2c361STejun Heo */ 586*bba2c361STejun Heo if (numa_cpus) { 587*bba2c361STejun Heo cpu = pick_idle_cpu_in_node(numa_cpus, node, SCX_PICK_IDLE_CORE); 588*bba2c361STejun Heo if (cpu >= 0) 589*bba2c361STejun Heo goto out_unlock; 590*bba2c361STejun Heo } 591*bba2c361STejun Heo 592*bba2c361STejun Heo /* 593*bba2c361STejun Heo * Search for any full-idle core usable by the task. 594*bba2c361STejun Heo * 595*bba2c361STejun Heo * If the node-aware idle CPU selection policy is enabled 596*bba2c361STejun Heo * (%SCX_OPS_BUILTIN_IDLE_PER_NODE), the search will always 597*bba2c361STejun Heo * begin in prev_cpu's node and proceed to other nodes in 598*bba2c361STejun Heo * order of increasing distance. 599*bba2c361STejun Heo */ 600*bba2c361STejun Heo cpu = scx_pick_idle_cpu(allowed, node, flags | SCX_PICK_IDLE_CORE); 601*bba2c361STejun Heo if (cpu >= 0) 602*bba2c361STejun Heo goto out_unlock; 603*bba2c361STejun Heo 604*bba2c361STejun Heo /* 605*bba2c361STejun Heo * Give up if we're strictly looking for a full-idle SMT 606*bba2c361STejun Heo * core. 607*bba2c361STejun Heo */ 608*bba2c361STejun Heo if (flags & SCX_PICK_IDLE_CORE) { 609*bba2c361STejun Heo cpu = -EBUSY; 610*bba2c361STejun Heo goto out_unlock; 611*bba2c361STejun Heo } 612*bba2c361STejun Heo } 613*bba2c361STejun Heo 614*bba2c361STejun Heo /* 615*bba2c361STejun Heo * Use @prev_cpu if it's idle. 616*bba2c361STejun Heo */ 617*bba2c361STejun Heo if (is_prev_allowed && scx_idle_test_and_clear_cpu(prev_cpu)) { 618*bba2c361STejun Heo cpu = prev_cpu; 619*bba2c361STejun Heo goto out_unlock; 620*bba2c361STejun Heo } 621*bba2c361STejun Heo 622*bba2c361STejun Heo /* 623*bba2c361STejun Heo * Use @prev_cpu's sibling if it's idle. 624*bba2c361STejun Heo */ 625*bba2c361STejun Heo if (sched_smt_active()) { 626*bba2c361STejun Heo for_each_cpu_and(cpu, cpu_smt_mask(prev_cpu), allowed) { 627*bba2c361STejun Heo if (cpu == prev_cpu) 628*bba2c361STejun Heo continue; 629*bba2c361STejun Heo if (scx_idle_test_and_clear_cpu(cpu)) 630*bba2c361STejun Heo goto out_unlock; 631*bba2c361STejun Heo } 632*bba2c361STejun Heo } 633*bba2c361STejun Heo 634*bba2c361STejun Heo /* 635*bba2c361STejun Heo * Search for any idle CPU in the same LLC domain. 636*bba2c361STejun Heo */ 637*bba2c361STejun Heo if (llc_cpus) { 638*bba2c361STejun Heo cpu = pick_idle_cpu_in_node(llc_cpus, node, 0); 639*bba2c361STejun Heo if (cpu >= 0) 640*bba2c361STejun Heo goto out_unlock; 641*bba2c361STejun Heo } 642*bba2c361STejun Heo 643*bba2c361STejun Heo /* 644*bba2c361STejun Heo * Search for any idle CPU in the same NUMA node. 645*bba2c361STejun Heo */ 646*bba2c361STejun Heo if (numa_cpus) { 647*bba2c361STejun Heo cpu = pick_idle_cpu_in_node(numa_cpus, node, 0); 648*bba2c361STejun Heo if (cpu >= 0) 649*bba2c361STejun Heo goto out_unlock; 650*bba2c361STejun Heo } 651*bba2c361STejun Heo 652*bba2c361STejun Heo /* 653*bba2c361STejun Heo * Search for any idle CPU usable by the task. 654*bba2c361STejun Heo * 655*bba2c361STejun Heo * If the node-aware idle CPU selection policy is enabled 656*bba2c361STejun Heo * (%SCX_OPS_BUILTIN_IDLE_PER_NODE), the search will always begin 657*bba2c361STejun Heo * in prev_cpu's node and proceed to other nodes in order of 658*bba2c361STejun Heo * increasing distance. 659*bba2c361STejun Heo */ 660*bba2c361STejun Heo cpu = scx_pick_idle_cpu(allowed, node, flags); 661*bba2c361STejun Heo 662*bba2c361STejun Heo out_unlock: 663*bba2c361STejun Heo rcu_read_unlock(); 664*bba2c361STejun Heo out_enable: 665*bba2c361STejun Heo preempt_enable(); 666*bba2c361STejun Heo 667*bba2c361STejun Heo return cpu; 668*bba2c361STejun Heo } 669*bba2c361STejun Heo 670*bba2c361STejun Heo /* 671*bba2c361STejun Heo * Initialize global and per-node idle cpumasks. 672*bba2c361STejun Heo */ 673*bba2c361STejun Heo void scx_idle_init_masks(void) 674*bba2c361STejun Heo { 675*bba2c361STejun Heo int i; 676*bba2c361STejun Heo 677*bba2c361STejun Heo /* Allocate global idle cpumasks */ 678*bba2c361STejun Heo BUG_ON(!alloc_cpumask_var(&scx_idle_global_masks.cpu, GFP_KERNEL)); 679*bba2c361STejun Heo BUG_ON(!alloc_cpumask_var(&scx_idle_global_masks.smt, GFP_KERNEL)); 680*bba2c361STejun Heo 681*bba2c361STejun Heo /* Allocate per-node idle cpumasks (use nr_node_ids for non-contiguous NUMA nodes) */ 682*bba2c361STejun Heo scx_idle_node_masks = kzalloc_objs(*scx_idle_node_masks, nr_node_ids); 683*bba2c361STejun Heo BUG_ON(!scx_idle_node_masks); 684*bba2c361STejun Heo 685*bba2c361STejun Heo for_each_node(i) { 686*bba2c361STejun Heo scx_idle_node_masks[i] = kzalloc_node(sizeof(**scx_idle_node_masks), 687*bba2c361STejun Heo GFP_KERNEL, i); 688*bba2c361STejun Heo BUG_ON(!scx_idle_node_masks[i]); 689*bba2c361STejun Heo 690*bba2c361STejun Heo BUG_ON(!alloc_cpumask_var_node(&scx_idle_node_masks[i]->cpu, GFP_KERNEL, i)); 691*bba2c361STejun Heo BUG_ON(!alloc_cpumask_var_node(&scx_idle_node_masks[i]->smt, GFP_KERNEL, i)); 692*bba2c361STejun Heo } 693*bba2c361STejun Heo 694*bba2c361STejun Heo /* Allocate local per-cpu idle cpumasks */ 695*bba2c361STejun Heo for_each_possible_cpu(i) { 696*bba2c361STejun Heo BUG_ON(!alloc_cpumask_var_node(&per_cpu(local_idle_cpumask, i), 697*bba2c361STejun Heo GFP_KERNEL, cpu_to_node(i))); 698*bba2c361STejun Heo BUG_ON(!alloc_cpumask_var_node(&per_cpu(local_llc_idle_cpumask, i), 699*bba2c361STejun Heo GFP_KERNEL, cpu_to_node(i))); 700*bba2c361STejun Heo BUG_ON(!alloc_cpumask_var_node(&per_cpu(local_numa_idle_cpumask, i), 701*bba2c361STejun Heo GFP_KERNEL, cpu_to_node(i))); 702*bba2c361STejun Heo } 703*bba2c361STejun Heo } 704*bba2c361STejun Heo 705*bba2c361STejun Heo static void update_builtin_idle(int cpu, bool idle) 706*bba2c361STejun Heo { 707*bba2c361STejun Heo int node = scx_cpu_node_if_enabled(cpu); 708*bba2c361STejun Heo struct cpumask *idle_cpus = idle_cpumask(node)->cpu; 709*bba2c361STejun Heo 710*bba2c361STejun Heo assign_cpu(cpu, idle_cpus, idle); 711*bba2c361STejun Heo 712*bba2c361STejun Heo if (sched_smt_active()) { 713*bba2c361STejun Heo const struct cpumask *smt = cpu_smt_mask(cpu); 714*bba2c361STejun Heo struct cpumask *idle_smts = idle_cpumask(node)->smt; 715*bba2c361STejun Heo 716*bba2c361STejun Heo if (idle) { 717*bba2c361STejun Heo /* 718*bba2c361STejun Heo * idle_smt handling is racy but that's fine as it's 719*bba2c361STejun Heo * only for optimization and self-correcting. 720*bba2c361STejun Heo */ 721*bba2c361STejun Heo if (!cpumask_subset(smt, idle_cpus)) 722*bba2c361STejun Heo return; 723*bba2c361STejun Heo cpumask_or(idle_smts, idle_smts, smt); 724*bba2c361STejun Heo } else { 725*bba2c361STejun Heo cpumask_andnot(idle_smts, idle_smts, smt); 726*bba2c361STejun Heo } 727*bba2c361STejun Heo } 728*bba2c361STejun Heo } 729*bba2c361STejun Heo 730*bba2c361STejun Heo /* 731*bba2c361STejun Heo * Update the idle state of a CPU to @idle. 732*bba2c361STejun Heo * 733*bba2c361STejun Heo * If @do_notify is true, ops.update_idle() is invoked to notify the scx 734*bba2c361STejun Heo * scheduler of an actual idle state transition (idle to busy or vice 735*bba2c361STejun Heo * versa). If @do_notify is false, only the idle state in the idle masks is 736*bba2c361STejun Heo * refreshed without invoking ops.update_idle(). 737*bba2c361STejun Heo * 738*bba2c361STejun Heo * This distinction is necessary, because an idle CPU can be "reserved" and 739*bba2c361STejun Heo * awakened via scx_bpf_pick_idle_cpu() + scx_bpf_kick_cpu(), marking it as 740*bba2c361STejun Heo * busy even if no tasks are dispatched. In this case, the CPU may return 741*bba2c361STejun Heo * to idle without a true state transition. Refreshing the idle masks 742*bba2c361STejun Heo * without invoking ops.update_idle() ensures accurate idle state tracking 743*bba2c361STejun Heo * while avoiding unnecessary updates and maintaining balanced state 744*bba2c361STejun Heo * transitions. 745*bba2c361STejun Heo */ 746*bba2c361STejun Heo void __scx_update_idle(struct rq *rq, bool idle, bool do_notify) 747*bba2c361STejun Heo { 748*bba2c361STejun Heo struct scx_sched *sch = scx_root; 749*bba2c361STejun Heo int cpu = cpu_of(rq); 750*bba2c361STejun Heo 751*bba2c361STejun Heo lockdep_assert_rq_held(rq); 752*bba2c361STejun Heo 753*bba2c361STejun Heo /* 754*bba2c361STejun Heo * Update the idle masks: 755*bba2c361STejun Heo * - for real idle transitions (do_notify == true) 756*bba2c361STejun Heo * - for idle-to-idle transitions (indicated by the previous task 757*bba2c361STejun Heo * being the idle thread, managed by pick_task_idle()) 758*bba2c361STejun Heo * 759*bba2c361STejun Heo * Skip updating idle masks if the previous task is not the idle 760*bba2c361STejun Heo * thread, since set_next_task_idle() has already handled it when 761*bba2c361STejun Heo * transitioning from a task to the idle thread (calling this 762*bba2c361STejun Heo * function with do_notify == true). 763*bba2c361STejun Heo * 764*bba2c361STejun Heo * In this way we can avoid updating the idle masks twice, 765*bba2c361STejun Heo * unnecessarily. 766*bba2c361STejun Heo */ 767*bba2c361STejun Heo if (static_branch_likely(&scx_builtin_idle_enabled)) 768*bba2c361STejun Heo if (do_notify || is_idle_task(rq->curr)) 769*bba2c361STejun Heo update_builtin_idle(cpu, idle); 770*bba2c361STejun Heo 771*bba2c361STejun Heo /* 772*bba2c361STejun Heo * Trigger ops.update_idle() only when transitioning from a task to 773*bba2c361STejun Heo * the idle thread and vice versa. 774*bba2c361STejun Heo * 775*bba2c361STejun Heo * Idle transitions are indicated by do_notify being set to true, 776*bba2c361STejun Heo * managed by put_prev_task_idle()/set_next_task_idle(). 777*bba2c361STejun Heo * 778*bba2c361STejun Heo * This must come after builtin idle update so that BPF schedulers can 779*bba2c361STejun Heo * create interlocking between ops.update_idle() and ops.enqueue() - 780*bba2c361STejun Heo * either enqueue() sees the idle bit or update_idle() sees the task 781*bba2c361STejun Heo * that enqueue() queued. 782*bba2c361STejun Heo */ 783*bba2c361STejun Heo if (SCX_HAS_OP(sch, update_idle) && do_notify && 784*bba2c361STejun Heo !scx_bypassing(sch, cpu_of(rq))) 785*bba2c361STejun Heo SCX_CALL_OP(sch, update_idle, rq, scx_cpu_arg(cpu_of(rq)), idle); 786*bba2c361STejun Heo } 787*bba2c361STejun Heo 788*bba2c361STejun Heo static void reset_idle_masks(struct sched_ext_ops *ops) 789*bba2c361STejun Heo { 790*bba2c361STejun Heo int node; 791*bba2c361STejun Heo 792*bba2c361STejun Heo /* 793*bba2c361STejun Heo * Consider all online cpus idle. Should converge to the actual state 794*bba2c361STejun Heo * quickly. 795*bba2c361STejun Heo */ 796*bba2c361STejun Heo if (!(ops->flags & SCX_OPS_BUILTIN_IDLE_PER_NODE)) { 797*bba2c361STejun Heo cpumask_copy(idle_cpumask(NUMA_NO_NODE)->cpu, cpu_online_mask); 798*bba2c361STejun Heo cpumask_copy(idle_cpumask(NUMA_NO_NODE)->smt, cpu_online_mask); 799*bba2c361STejun Heo return; 800*bba2c361STejun Heo } 801*bba2c361STejun Heo 802*bba2c361STejun Heo for_each_node(node) { 803*bba2c361STejun Heo const struct cpumask *node_mask = cpumask_of_node(node); 804*bba2c361STejun Heo 805*bba2c361STejun Heo cpumask_and(idle_cpumask(node)->cpu, cpu_online_mask, node_mask); 806*bba2c361STejun Heo cpumask_and(idle_cpumask(node)->smt, cpu_online_mask, node_mask); 807*bba2c361STejun Heo } 808*bba2c361STejun Heo } 809*bba2c361STejun Heo 810*bba2c361STejun Heo void scx_idle_enable(struct sched_ext_ops *ops) 811*bba2c361STejun Heo { 812*bba2c361STejun Heo if (!ops->update_idle || (ops->flags & SCX_OPS_KEEP_BUILTIN_IDLE)) 813*bba2c361STejun Heo static_branch_enable_cpuslocked(&scx_builtin_idle_enabled); 814*bba2c361STejun Heo else 815*bba2c361STejun Heo static_branch_disable_cpuslocked(&scx_builtin_idle_enabled); 816*bba2c361STejun Heo 817*bba2c361STejun Heo if (ops->flags & SCX_OPS_BUILTIN_IDLE_PER_NODE) 818*bba2c361STejun Heo static_branch_enable_cpuslocked(&scx_builtin_idle_per_node); 819*bba2c361STejun Heo else 820*bba2c361STejun Heo static_branch_disable_cpuslocked(&scx_builtin_idle_per_node); 821*bba2c361STejun Heo 822*bba2c361STejun Heo reset_idle_masks(ops); 823*bba2c361STejun Heo } 824*bba2c361STejun Heo 825*bba2c361STejun Heo void scx_idle_disable(void) 826*bba2c361STejun Heo { 827*bba2c361STejun Heo static_branch_disable(&scx_builtin_idle_enabled); 828*bba2c361STejun Heo static_branch_disable(&scx_builtin_idle_per_node); 829*bba2c361STejun Heo } 830*bba2c361STejun Heo 831*bba2c361STejun Heo /******************************************************************************** 832*bba2c361STejun Heo * Helpers that can be called from the BPF scheduler. 833*bba2c361STejun Heo */ 834*bba2c361STejun Heo 835*bba2c361STejun Heo static int validate_node(struct scx_sched *sch, int node) 836*bba2c361STejun Heo { 837*bba2c361STejun Heo if (!static_branch_likely(&scx_builtin_idle_per_node)) { 838*bba2c361STejun Heo scx_error(sch, "per-node idle tracking is disabled"); 839*bba2c361STejun Heo return -EOPNOTSUPP; 840*bba2c361STejun Heo } 841*bba2c361STejun Heo 842*bba2c361STejun Heo /* Return no entry for NUMA_NO_NODE (not a critical scx error) */ 843*bba2c361STejun Heo if (node == NUMA_NO_NODE) 844*bba2c361STejun Heo return -ENOENT; 845*bba2c361STejun Heo 846*bba2c361STejun Heo /* Make sure node is in a valid range */ 847*bba2c361STejun Heo if (node < 0 || node >= nr_node_ids) { 848*bba2c361STejun Heo scx_error(sch, "invalid node %d", node); 849*bba2c361STejun Heo return -EINVAL; 850*bba2c361STejun Heo } 851*bba2c361STejun Heo 852*bba2c361STejun Heo /* Make sure the node is part of the set of possible nodes */ 853*bba2c361STejun Heo if (!node_possible(node)) { 854*bba2c361STejun Heo scx_error(sch, "unavailable node %d", node); 855*bba2c361STejun Heo return -EINVAL; 856*bba2c361STejun Heo } 857*bba2c361STejun Heo 858*bba2c361STejun Heo return node; 859*bba2c361STejun Heo } 860*bba2c361STejun Heo 861*bba2c361STejun Heo __bpf_kfunc_start_defs(); 862*bba2c361STejun Heo 863*bba2c361STejun Heo static bool check_builtin_idle_enabled(struct scx_sched *sch) 864*bba2c361STejun Heo { 865*bba2c361STejun Heo if (static_branch_likely(&scx_builtin_idle_enabled)) 866*bba2c361STejun Heo return true; 867*bba2c361STejun Heo 868*bba2c361STejun Heo scx_error(sch, "built-in idle tracking is disabled"); 869*bba2c361STejun Heo return false; 870*bba2c361STejun Heo } 871*bba2c361STejun Heo 872*bba2c361STejun Heo /* 873*bba2c361STejun Heo * Determine whether @p is a migration-disabled task in the context of BPF 874*bba2c361STejun Heo * code. 875*bba2c361STejun Heo * 876*bba2c361STejun Heo * We can't simply check whether @p->migration_disabled is set in a 877*bba2c361STejun Heo * sched_ext callback, because the BPF prolog (__bpf_prog_enter) may disable 878*bba2c361STejun Heo * migration for the current task while running BPF code. 879*bba2c361STejun Heo * 880*bba2c361STejun Heo * Since the BPF prolog calls migrate_disable() only when CONFIG_PREEMPT_RCU 881*bba2c361STejun Heo * is enabled (via rcu_read_lock_dont_migrate()), migration_disabled == 1 for 882*bba2c361STejun Heo * the current task is ambiguous only in that case: it could be from the BPF 883*bba2c361STejun Heo * prolog rather than a real migrate_disable() call. 884*bba2c361STejun Heo * 885*bba2c361STejun Heo * Without CONFIG_PREEMPT_RCU, the BPF prolog never calls migrate_disable(), 886*bba2c361STejun Heo * so migration_disabled == 1 always means the task is truly 887*bba2c361STejun Heo * migration-disabled. 888*bba2c361STejun Heo * 889*bba2c361STejun Heo * Therefore, when migration_disabled == 1 and CONFIG_PREEMPT_RCU is enabled, 890*bba2c361STejun Heo * check whether @p is the current task or not: if it is, then migration was 891*bba2c361STejun Heo * not disabled before entering the callback, otherwise migration was disabled. 892*bba2c361STejun Heo * 893*bba2c361STejun Heo * Returns true if @p is migration-disabled, false otherwise. 894*bba2c361STejun Heo */ 895*bba2c361STejun Heo static bool is_bpf_migration_disabled(const struct task_struct *p) 896*bba2c361STejun Heo { 897*bba2c361STejun Heo if (p->migration_disabled == 1) { 898*bba2c361STejun Heo if (IS_ENABLED(CONFIG_PREEMPT_RCU)) 899*bba2c361STejun Heo return p != current; 900*bba2c361STejun Heo return true; 901*bba2c361STejun Heo } 902*bba2c361STejun Heo return p->migration_disabled; 903*bba2c361STejun Heo } 904*bba2c361STejun Heo 905*bba2c361STejun Heo static s32 select_cpu_from_kfunc(struct scx_sched *sch, struct task_struct *p, 906*bba2c361STejun Heo s32 prev_cpu, u64 wake_flags, 907*bba2c361STejun Heo const struct cpumask *allowed, u64 flags) 908*bba2c361STejun Heo { 909*bba2c361STejun Heo unsigned long irq_flags; 910*bba2c361STejun Heo bool we_locked = false; 911*bba2c361STejun Heo s32 cpu; 912*bba2c361STejun Heo 913*bba2c361STejun Heo if (!scx_cpu_valid(sch, prev_cpu, NULL)) 914*bba2c361STejun Heo return -EINVAL; 915*bba2c361STejun Heo 916*bba2c361STejun Heo if (!check_builtin_idle_enabled(sch)) 917*bba2c361STejun Heo return -EBUSY; 918*bba2c361STejun Heo 919*bba2c361STejun Heo /* 920*bba2c361STejun Heo * Accessing p->cpus_ptr / p->nr_cpus_allowed needs either @p's rq 921*bba2c361STejun Heo * lock or @p's pi_lock. Three cases: 922*bba2c361STejun Heo * 923*bba2c361STejun Heo * - inside ops.select_cpu(): try_to_wake_up() holds the wake-up 924*bba2c361STejun Heo * task's pi_lock; the wake-up task is recorded in kf_tasks[0] 925*bba2c361STejun Heo * by SCX_CALL_OP_TASK_RET(). 926*bba2c361STejun Heo * - other rq-locked SCX op: scx_locked_rq() points at the held rq. 927*bba2c361STejun Heo * - truly unlocked (UNLOCKED ops, SYSCALL, non-SCX struct_ops): 928*bba2c361STejun Heo * nothing held, take pi_lock ourselves. 929*bba2c361STejun Heo * 930*bba2c361STejun Heo * In the first two cases, BPF schedulers may pass an arbitrary task 931*bba2c361STejun Heo * that the held lock doesn't cover. Refuse those. 932*bba2c361STejun Heo */ 933*bba2c361STejun Heo if (this_rq()->scx.in_select_cpu) { 934*bba2c361STejun Heo if (!scx_kf_arg_task_ok(sch, p)) 935*bba2c361STejun Heo return -EINVAL; 936*bba2c361STejun Heo lockdep_assert_held(&p->pi_lock); 937*bba2c361STejun Heo } else if (scx_locked_rq()) { 938*bba2c361STejun Heo if (task_rq(p) != scx_locked_rq()) 939*bba2c361STejun Heo goto cross_task; 940*bba2c361STejun Heo } else { 941*bba2c361STejun Heo raw_spin_lock_irqsave(&p->pi_lock, irq_flags); 942*bba2c361STejun Heo we_locked = true; 943*bba2c361STejun Heo } 944*bba2c361STejun Heo 945*bba2c361STejun Heo /* 946*bba2c361STejun Heo * This may also be called from ops.enqueue(), so we need to handle 947*bba2c361STejun Heo * per-CPU tasks as well. For these tasks, we can skip all idle CPU 948*bba2c361STejun Heo * selection optimizations and simply check whether the previously 949*bba2c361STejun Heo * used CPU is idle and within the allowed cpumask. 950*bba2c361STejun Heo */ 951*bba2c361STejun Heo if (p->nr_cpus_allowed == 1 || is_bpf_migration_disabled(p)) { 952*bba2c361STejun Heo if (cpumask_test_cpu(prev_cpu, allowed ?: p->cpus_ptr) && 953*bba2c361STejun Heo scx_idle_test_and_clear_cpu(prev_cpu)) 954*bba2c361STejun Heo cpu = prev_cpu; 955*bba2c361STejun Heo else 956*bba2c361STejun Heo cpu = -EBUSY; 957*bba2c361STejun Heo } else { 958*bba2c361STejun Heo cpu = scx_select_cpu_dfl(p, prev_cpu, wake_flags, 959*bba2c361STejun Heo allowed ?: p->cpus_ptr, flags); 960*bba2c361STejun Heo } 961*bba2c361STejun Heo 962*bba2c361STejun Heo if (we_locked) 963*bba2c361STejun Heo raw_spin_unlock_irqrestore(&p->pi_lock, irq_flags); 964*bba2c361STejun Heo 965*bba2c361STejun Heo return cpu; 966*bba2c361STejun Heo 967*bba2c361STejun Heo cross_task: 968*bba2c361STejun Heo scx_error(sch, "select_cpu kfunc called cross-task on %s[%d]", 969*bba2c361STejun Heo p->comm, p->pid); 970*bba2c361STejun Heo return -EINVAL; 971*bba2c361STejun Heo } 972*bba2c361STejun Heo 973*bba2c361STejun Heo /** 974*bba2c361STejun Heo * scx_bpf_cpu_node - Return the NUMA node the given @cpu belongs to, or 975*bba2c361STejun Heo * trigger an error if @cpu is invalid 976*bba2c361STejun Heo * @cpu: target CPU 977*bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 978*bba2c361STejun Heo */ 979*bba2c361STejun Heo __bpf_kfunc s32 scx_bpf_cpu_node(s32 cpu, const struct bpf_prog_aux *aux) 980*bba2c361STejun Heo { 981*bba2c361STejun Heo struct scx_sched *sch; 982*bba2c361STejun Heo 983*bba2c361STejun Heo guard(rcu)(); 984*bba2c361STejun Heo 985*bba2c361STejun Heo sch = scx_prog_sched(aux); 986*bba2c361STejun Heo if (unlikely(!sch) || !scx_cpu_valid(sch, cpu, NULL)) 987*bba2c361STejun Heo return NUMA_NO_NODE; 988*bba2c361STejun Heo return cpu_to_node(cpu); 989*bba2c361STejun Heo } 990*bba2c361STejun Heo 991*bba2c361STejun Heo /** 992*bba2c361STejun Heo * scx_bpf_select_cpu_dfl - The default implementation of ops.select_cpu() 993*bba2c361STejun Heo * @p: task_struct to select a CPU for 994*bba2c361STejun Heo * @prev_cpu: CPU @p was on previously 995*bba2c361STejun Heo * @wake_flags: %SCX_WAKE_* flags 996*bba2c361STejun Heo * @is_idle: out parameter indicating whether the returned CPU is idle 997*bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 998*bba2c361STejun Heo * 999*bba2c361STejun Heo * Can be called from ops.select_cpu(), ops.enqueue(), or from an unlocked 1000*bba2c361STejun Heo * context such as a BPF test_run() call, as long as built-in CPU selection 1001*bba2c361STejun Heo * is enabled: ops.update_idle() is missing or %SCX_OPS_KEEP_BUILTIN_IDLE 1002*bba2c361STejun Heo * is set. 1003*bba2c361STejun Heo * 1004*bba2c361STejun Heo * Returns the picked CPU with *@is_idle indicating whether the picked CPU is 1005*bba2c361STejun Heo * currently idle and thus a good candidate for direct dispatching. 1006*bba2c361STejun Heo */ 1007*bba2c361STejun Heo __bpf_kfunc s32 scx_bpf_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, 1008*bba2c361STejun Heo u64 wake_flags, bool *is_idle, 1009*bba2c361STejun Heo const struct bpf_prog_aux *aux) 1010*bba2c361STejun Heo { 1011*bba2c361STejun Heo struct scx_sched *sch; 1012*bba2c361STejun Heo s32 cpu; 1013*bba2c361STejun Heo 1014*bba2c361STejun Heo guard(rcu)(); 1015*bba2c361STejun Heo 1016*bba2c361STejun Heo sch = scx_prog_sched(aux); 1017*bba2c361STejun Heo if (unlikely(!sch)) 1018*bba2c361STejun Heo return -ENODEV; 1019*bba2c361STejun Heo 1020*bba2c361STejun Heo cpu = select_cpu_from_kfunc(sch, p, prev_cpu, wake_flags, NULL, 0); 1021*bba2c361STejun Heo if (cpu >= 0) { 1022*bba2c361STejun Heo *is_idle = true; 1023*bba2c361STejun Heo return cpu; 1024*bba2c361STejun Heo } 1025*bba2c361STejun Heo *is_idle = false; 1026*bba2c361STejun Heo return prev_cpu; 1027*bba2c361STejun Heo } 1028*bba2c361STejun Heo 1029*bba2c361STejun Heo struct scx_bpf_select_cpu_and_args { 1030*bba2c361STejun Heo /* @p and @cpus_allowed can't be packed together as KF_RCU is not transitive */ 1031*bba2c361STejun Heo s32 prev_cpu; 1032*bba2c361STejun Heo u64 wake_flags; 1033*bba2c361STejun Heo u64 flags; 1034*bba2c361STejun Heo }; 1035*bba2c361STejun Heo 1036*bba2c361STejun Heo /** 1037*bba2c361STejun Heo * __scx_bpf_select_cpu_and - Arg-wrapped CPU selection with cpumask 1038*bba2c361STejun Heo * @p: task_struct to select a CPU for 1039*bba2c361STejun Heo * @cpus_allowed: cpumask of allowed CPUs 1040*bba2c361STejun Heo * @args: struct containing the rest of the arguments 1041*bba2c361STejun Heo * @args->prev_cpu: CPU @p was on previously 1042*bba2c361STejun Heo * @args->wake_flags: %SCX_WAKE_* flags 1043*bba2c361STejun Heo * @args->flags: %SCX_PICK_IDLE* flags 1044*bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 1045*bba2c361STejun Heo * 1046*bba2c361STejun Heo * Wrapper kfunc that takes arguments via struct to work around BPF's 5 argument 1047*bba2c361STejun Heo * limit. BPF programs should use scx_bpf_select_cpu_and() which is provided 1048*bba2c361STejun Heo * as an inline wrapper in common.bpf.h. 1049*bba2c361STejun Heo * 1050*bba2c361STejun Heo * Can be called from ops.select_cpu(), ops.enqueue(), or from an unlocked 1051*bba2c361STejun Heo * context such as a BPF test_run() call, as long as built-in CPU selection 1052*bba2c361STejun Heo * is enabled: ops.update_idle() is missing or %SCX_OPS_KEEP_BUILTIN_IDLE 1053*bba2c361STejun Heo * is set. 1054*bba2c361STejun Heo * 1055*bba2c361STejun Heo * @p, @args->prev_cpu and @args->wake_flags match ops.select_cpu(). 1056*bba2c361STejun Heo * 1057*bba2c361STejun Heo * Returns the selected idle CPU, which will be automatically awakened upon 1058*bba2c361STejun Heo * returning from ops.select_cpu() and can be used for direct dispatch, or 1059*bba2c361STejun Heo * a negative value if no idle CPU is available. 1060*bba2c361STejun Heo */ 1061*bba2c361STejun Heo __bpf_kfunc s32 1062*bba2c361STejun Heo __scx_bpf_select_cpu_and(struct task_struct *p, const struct cpumask *cpus_allowed, 1063*bba2c361STejun Heo struct scx_bpf_select_cpu_and_args *args, 1064*bba2c361STejun Heo const struct bpf_prog_aux *aux) 1065*bba2c361STejun Heo { 1066*bba2c361STejun Heo struct scx_sched *sch; 1067*bba2c361STejun Heo 1068*bba2c361STejun Heo guard(rcu)(); 1069*bba2c361STejun Heo 1070*bba2c361STejun Heo sch = scx_prog_sched(aux); 1071*bba2c361STejun Heo if (unlikely(!sch)) 1072*bba2c361STejun Heo return -ENODEV; 1073*bba2c361STejun Heo 1074*bba2c361STejun Heo return select_cpu_from_kfunc(sch, p, args->prev_cpu, args->wake_flags, 1075*bba2c361STejun Heo cpus_allowed, args->flags); 1076*bba2c361STejun Heo } 1077*bba2c361STejun Heo 1078*bba2c361STejun Heo /* 1079*bba2c361STejun Heo * COMPAT: Will be removed in v6.22. 1080*bba2c361STejun Heo */ 1081*bba2c361STejun Heo __bpf_kfunc s32 scx_bpf_select_cpu_and(struct task_struct *p, s32 prev_cpu, u64 wake_flags, 1082*bba2c361STejun Heo const struct cpumask *cpus_allowed, u64 flags) 1083*bba2c361STejun Heo { 1084*bba2c361STejun Heo struct scx_sched *sch; 1085*bba2c361STejun Heo 1086*bba2c361STejun Heo guard(rcu)(); 1087*bba2c361STejun Heo 1088*bba2c361STejun Heo sch = rcu_dereference(scx_root); 1089*bba2c361STejun Heo if (unlikely(!sch)) 1090*bba2c361STejun Heo return -ENODEV; 1091*bba2c361STejun Heo 1092*bba2c361STejun Heo #ifdef CONFIG_EXT_SUB_SCHED 1093*bba2c361STejun Heo /* 1094*bba2c361STejun Heo * Disallow if any sub-scheds are attached. There is no way to tell 1095*bba2c361STejun Heo * which scheduler called us, just error out @p's scheduler. 1096*bba2c361STejun Heo */ 1097*bba2c361STejun Heo if (unlikely(!list_empty(&sch->children))) { 1098*bba2c361STejun Heo scx_error(scx_task_sched(p), "__scx_bpf_select_cpu_and() must be used"); 1099*bba2c361STejun Heo return -EINVAL; 1100*bba2c361STejun Heo } 1101*bba2c361STejun Heo #endif 1102*bba2c361STejun Heo 1103*bba2c361STejun Heo return select_cpu_from_kfunc(sch, p, prev_cpu, wake_flags, 1104*bba2c361STejun Heo cpus_allowed, flags); 1105*bba2c361STejun Heo } 1106*bba2c361STejun Heo 1107*bba2c361STejun Heo /** 1108*bba2c361STejun Heo * scx_bpf_get_idle_cpumask_node - Get a referenced kptr to the 1109*bba2c361STejun Heo * idle-tracking per-CPU cpumask of a target NUMA node. 1110*bba2c361STejun Heo * @node: target NUMA node 1111*bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 1112*bba2c361STejun Heo * 1113*bba2c361STejun Heo * Returns an empty cpumask if idle tracking is not enabled, if @node is 1114*bba2c361STejun Heo * not valid, or running on a UP kernel. In this case the actual error will 1115*bba2c361STejun Heo * be reported to the BPF scheduler via scx_error(). 1116*bba2c361STejun Heo */ 1117*bba2c361STejun Heo __bpf_kfunc const struct cpumask * 1118*bba2c361STejun Heo scx_bpf_get_idle_cpumask_node(s32 node, const struct bpf_prog_aux *aux) 1119*bba2c361STejun Heo { 1120*bba2c361STejun Heo struct scx_sched *sch; 1121*bba2c361STejun Heo 1122*bba2c361STejun Heo guard(rcu)(); 1123*bba2c361STejun Heo 1124*bba2c361STejun Heo sch = scx_prog_sched(aux); 1125*bba2c361STejun Heo if (unlikely(!sch)) 1126*bba2c361STejun Heo return cpu_none_mask; 1127*bba2c361STejun Heo 1128*bba2c361STejun Heo node = validate_node(sch, node); 1129*bba2c361STejun Heo if (node < 0) 1130*bba2c361STejun Heo return cpu_none_mask; 1131*bba2c361STejun Heo 1132*bba2c361STejun Heo return idle_cpumask(node)->cpu; 1133*bba2c361STejun Heo } 1134*bba2c361STejun Heo 1135*bba2c361STejun Heo /** 1136*bba2c361STejun Heo * scx_bpf_get_idle_cpumask - Get a referenced kptr to the idle-tracking 1137*bba2c361STejun Heo * per-CPU cpumask. 1138*bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 1139*bba2c361STejun Heo * 1140*bba2c361STejun Heo * Returns an empty mask if idle tracking is not enabled, or running on a 1141*bba2c361STejun Heo * UP kernel. 1142*bba2c361STejun Heo */ 1143*bba2c361STejun Heo __bpf_kfunc const struct cpumask *scx_bpf_get_idle_cpumask(const struct bpf_prog_aux *aux) 1144*bba2c361STejun Heo { 1145*bba2c361STejun Heo struct scx_sched *sch; 1146*bba2c361STejun Heo 1147*bba2c361STejun Heo guard(rcu)(); 1148*bba2c361STejun Heo 1149*bba2c361STejun Heo sch = scx_prog_sched(aux); 1150*bba2c361STejun Heo if (unlikely(!sch)) 1151*bba2c361STejun Heo return cpu_none_mask; 1152*bba2c361STejun Heo 1153*bba2c361STejun Heo if (static_branch_unlikely(&scx_builtin_idle_per_node)) { 1154*bba2c361STejun Heo scx_error(sch, "SCX_OPS_BUILTIN_IDLE_PER_NODE enabled"); 1155*bba2c361STejun Heo return cpu_none_mask; 1156*bba2c361STejun Heo } 1157*bba2c361STejun Heo 1158*bba2c361STejun Heo if (!check_builtin_idle_enabled(sch)) 1159*bba2c361STejun Heo return cpu_none_mask; 1160*bba2c361STejun Heo 1161*bba2c361STejun Heo return idle_cpumask(NUMA_NO_NODE)->cpu; 1162*bba2c361STejun Heo } 1163*bba2c361STejun Heo 1164*bba2c361STejun Heo /** 1165*bba2c361STejun Heo * scx_bpf_get_idle_smtmask_node - Get a referenced kptr to the 1166*bba2c361STejun Heo * idle-tracking, per-physical-core cpumask of a target NUMA node. Can be 1167*bba2c361STejun Heo * used to determine if an entire physical core is free. 1168*bba2c361STejun Heo * @node: target NUMA node 1169*bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 1170*bba2c361STejun Heo * 1171*bba2c361STejun Heo * Returns an empty cpumask if idle tracking is not enabled, if @node is 1172*bba2c361STejun Heo * not valid, or running on a UP kernel. In this case the actual error will 1173*bba2c361STejun Heo * be reported to the BPF scheduler via scx_error(). 1174*bba2c361STejun Heo */ 1175*bba2c361STejun Heo __bpf_kfunc const struct cpumask * 1176*bba2c361STejun Heo scx_bpf_get_idle_smtmask_node(s32 node, const struct bpf_prog_aux *aux) 1177*bba2c361STejun Heo { 1178*bba2c361STejun Heo struct scx_sched *sch; 1179*bba2c361STejun Heo 1180*bba2c361STejun Heo guard(rcu)(); 1181*bba2c361STejun Heo 1182*bba2c361STejun Heo sch = scx_prog_sched(aux); 1183*bba2c361STejun Heo if (unlikely(!sch)) 1184*bba2c361STejun Heo return cpu_none_mask; 1185*bba2c361STejun Heo 1186*bba2c361STejun Heo node = validate_node(sch, node); 1187*bba2c361STejun Heo if (node < 0) 1188*bba2c361STejun Heo return cpu_none_mask; 1189*bba2c361STejun Heo 1190*bba2c361STejun Heo if (sched_smt_active()) 1191*bba2c361STejun Heo return idle_cpumask(node)->smt; 1192*bba2c361STejun Heo else 1193*bba2c361STejun Heo return idle_cpumask(node)->cpu; 1194*bba2c361STejun Heo } 1195*bba2c361STejun Heo 1196*bba2c361STejun Heo /** 1197*bba2c361STejun Heo * scx_bpf_get_idle_smtmask - Get a referenced kptr to the idle-tracking, 1198*bba2c361STejun Heo * per-physical-core cpumask. Can be used to determine if an entire physical 1199*bba2c361STejun Heo * core is free. 1200*bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 1201*bba2c361STejun Heo * 1202*bba2c361STejun Heo * Returns an empty mask if idle tracking is not enabled, or running on a 1203*bba2c361STejun Heo * UP kernel. 1204*bba2c361STejun Heo */ 1205*bba2c361STejun Heo __bpf_kfunc const struct cpumask *scx_bpf_get_idle_smtmask(const struct bpf_prog_aux *aux) 1206*bba2c361STejun Heo { 1207*bba2c361STejun Heo struct scx_sched *sch; 1208*bba2c361STejun Heo 1209*bba2c361STejun Heo guard(rcu)(); 1210*bba2c361STejun Heo 1211*bba2c361STejun Heo sch = scx_prog_sched(aux); 1212*bba2c361STejun Heo if (unlikely(!sch)) 1213*bba2c361STejun Heo return cpu_none_mask; 1214*bba2c361STejun Heo 1215*bba2c361STejun Heo if (static_branch_unlikely(&scx_builtin_idle_per_node)) { 1216*bba2c361STejun Heo scx_error(sch, "SCX_OPS_BUILTIN_IDLE_PER_NODE enabled"); 1217*bba2c361STejun Heo return cpu_none_mask; 1218*bba2c361STejun Heo } 1219*bba2c361STejun Heo 1220*bba2c361STejun Heo if (!check_builtin_idle_enabled(sch)) 1221*bba2c361STejun Heo return cpu_none_mask; 1222*bba2c361STejun Heo 1223*bba2c361STejun Heo if (sched_smt_active()) 1224*bba2c361STejun Heo return idle_cpumask(NUMA_NO_NODE)->smt; 1225*bba2c361STejun Heo else 1226*bba2c361STejun Heo return idle_cpumask(NUMA_NO_NODE)->cpu; 1227*bba2c361STejun Heo } 1228*bba2c361STejun Heo 1229*bba2c361STejun Heo /** 1230*bba2c361STejun Heo * scx_bpf_put_idle_cpumask - Release a previously acquired referenced kptr to 1231*bba2c361STejun Heo * either the percpu, or SMT idle-tracking cpumask. 1232*bba2c361STejun Heo * @idle_mask: &cpumask to use 1233*bba2c361STejun Heo */ 1234*bba2c361STejun Heo __bpf_kfunc void scx_bpf_put_idle_cpumask(const struct cpumask *idle_mask) 1235*bba2c361STejun Heo { 1236*bba2c361STejun Heo /* 1237*bba2c361STejun Heo * Empty function body because we aren't actually acquiring or releasing 1238*bba2c361STejun Heo * a reference to a global idle cpumask, which is read-only in the 1239*bba2c361STejun Heo * caller and is never released. The acquire / release semantics here 1240*bba2c361STejun Heo * are just used to make the cpumask a trusted pointer in the caller. 1241*bba2c361STejun Heo */ 1242*bba2c361STejun Heo } 1243*bba2c361STejun Heo 1244*bba2c361STejun Heo /** 1245*bba2c361STejun Heo * scx_bpf_test_and_clear_cpu_idle - Test and clear @cpu's idle state 1246*bba2c361STejun Heo * @cpu: cpu to test and clear idle for 1247*bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 1248*bba2c361STejun Heo * 1249*bba2c361STejun Heo * Returns %true if @cpu was idle and its idle state was successfully cleared. 1250*bba2c361STejun Heo * %false otherwise. 1251*bba2c361STejun Heo * 1252*bba2c361STejun Heo * Unavailable if ops.update_idle() is implemented and 1253*bba2c361STejun Heo * %SCX_OPS_KEEP_BUILTIN_IDLE is not set. 1254*bba2c361STejun Heo */ 1255*bba2c361STejun Heo __bpf_kfunc bool scx_bpf_test_and_clear_cpu_idle(s32 cpu, const struct bpf_prog_aux *aux) 1256*bba2c361STejun Heo { 1257*bba2c361STejun Heo struct scx_sched *sch; 1258*bba2c361STejun Heo 1259*bba2c361STejun Heo guard(rcu)(); 1260*bba2c361STejun Heo 1261*bba2c361STejun Heo sch = scx_prog_sched(aux); 1262*bba2c361STejun Heo if (unlikely(!sch)) 1263*bba2c361STejun Heo return false; 1264*bba2c361STejun Heo 1265*bba2c361STejun Heo if (!check_builtin_idle_enabled(sch)) 1266*bba2c361STejun Heo return false; 1267*bba2c361STejun Heo 1268*bba2c361STejun Heo if (!scx_cpu_valid(sch, cpu, NULL)) 1269*bba2c361STejun Heo return false; 1270*bba2c361STejun Heo 1271*bba2c361STejun Heo return scx_idle_test_and_clear_cpu(cpu); 1272*bba2c361STejun Heo } 1273*bba2c361STejun Heo 1274*bba2c361STejun Heo /** 1275*bba2c361STejun Heo * scx_bpf_pick_idle_cpu_node - Pick and claim an idle cpu from @node 1276*bba2c361STejun Heo * @cpus_allowed: Allowed cpumask 1277*bba2c361STejun Heo * @node: target NUMA node 1278*bba2c361STejun Heo * @flags: %SCX_PICK_IDLE_* flags 1279*bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 1280*bba2c361STejun Heo * 1281*bba2c361STejun Heo * Pick and claim an idle cpu in @cpus_allowed from the NUMA node @node. 1282*bba2c361STejun Heo * 1283*bba2c361STejun Heo * Returns the picked idle cpu number on success, or -%EBUSY if no matching 1284*bba2c361STejun Heo * cpu was found. 1285*bba2c361STejun Heo * 1286*bba2c361STejun Heo * The search starts from @node and proceeds to other online NUMA nodes in 1287*bba2c361STejun Heo * order of increasing distance (unless SCX_PICK_IDLE_IN_NODE is specified, 1288*bba2c361STejun Heo * in which case the search is limited to the target @node). 1289*bba2c361STejun Heo * 1290*bba2c361STejun Heo * Always returns an error if ops.update_idle() is implemented and 1291*bba2c361STejun Heo * %SCX_OPS_KEEP_BUILTIN_IDLE is not set, or if 1292*bba2c361STejun Heo * %SCX_OPS_BUILTIN_IDLE_PER_NODE is not set. 1293*bba2c361STejun Heo */ 1294*bba2c361STejun Heo __bpf_kfunc s32 scx_bpf_pick_idle_cpu_node(const struct cpumask *cpus_allowed, 1295*bba2c361STejun Heo s32 node, u64 flags, 1296*bba2c361STejun Heo const struct bpf_prog_aux *aux) 1297*bba2c361STejun Heo { 1298*bba2c361STejun Heo struct scx_sched *sch; 1299*bba2c361STejun Heo 1300*bba2c361STejun Heo guard(rcu)(); 1301*bba2c361STejun Heo 1302*bba2c361STejun Heo sch = scx_prog_sched(aux); 1303*bba2c361STejun Heo if (unlikely(!sch)) 1304*bba2c361STejun Heo return -ENODEV; 1305*bba2c361STejun Heo 1306*bba2c361STejun Heo node = validate_node(sch, node); 1307*bba2c361STejun Heo if (node < 0) 1308*bba2c361STejun Heo return node; 1309*bba2c361STejun Heo 1310*bba2c361STejun Heo return scx_pick_idle_cpu(cpus_allowed, node, flags); 1311*bba2c361STejun Heo } 1312*bba2c361STejun Heo 1313*bba2c361STejun Heo /** 1314*bba2c361STejun Heo * scx_bpf_pick_idle_cpu - Pick and claim an idle cpu 1315*bba2c361STejun Heo * @cpus_allowed: Allowed cpumask 1316*bba2c361STejun Heo * @flags: %SCX_PICK_IDLE_CPU_* flags 1317*bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 1318*bba2c361STejun Heo * 1319*bba2c361STejun Heo * Pick and claim an idle cpu in @cpus_allowed. Returns the picked idle cpu 1320*bba2c361STejun Heo * number on success. -%EBUSY if no matching cpu was found. 1321*bba2c361STejun Heo * 1322*bba2c361STejun Heo * Idle CPU tracking may race against CPU scheduling state transitions. For 1323*bba2c361STejun Heo * example, this function may return -%EBUSY as CPUs are transitioning into the 1324*bba2c361STejun Heo * idle state. If the caller then assumes that there will be dispatch events on 1325*bba2c361STejun Heo * the CPUs as they were all busy, the scheduler may end up stalling with CPUs 1326*bba2c361STejun Heo * idling while there are pending tasks. Use scx_bpf_pick_any_cpu() and 1327*bba2c361STejun Heo * scx_bpf_kick_cpu() to guarantee that there will be at least one dispatch 1328*bba2c361STejun Heo * event in the near future. 1329*bba2c361STejun Heo * 1330*bba2c361STejun Heo * Unavailable if ops.update_idle() is implemented and 1331*bba2c361STejun Heo * %SCX_OPS_KEEP_BUILTIN_IDLE is not set. 1332*bba2c361STejun Heo * 1333*bba2c361STejun Heo * Always returns an error if %SCX_OPS_BUILTIN_IDLE_PER_NODE is set, use 1334*bba2c361STejun Heo * scx_bpf_pick_idle_cpu_node() instead. 1335*bba2c361STejun Heo */ 1336*bba2c361STejun Heo __bpf_kfunc s32 scx_bpf_pick_idle_cpu(const struct cpumask *cpus_allowed, 1337*bba2c361STejun Heo u64 flags, const struct bpf_prog_aux *aux) 1338*bba2c361STejun Heo { 1339*bba2c361STejun Heo struct scx_sched *sch; 1340*bba2c361STejun Heo 1341*bba2c361STejun Heo guard(rcu)(); 1342*bba2c361STejun Heo 1343*bba2c361STejun Heo sch = scx_prog_sched(aux); 1344*bba2c361STejun Heo if (unlikely(!sch)) 1345*bba2c361STejun Heo return -ENODEV; 1346*bba2c361STejun Heo 1347*bba2c361STejun Heo if (static_branch_maybe(CONFIG_NUMA, &scx_builtin_idle_per_node)) { 1348*bba2c361STejun Heo scx_error(sch, "per-node idle tracking is enabled"); 1349*bba2c361STejun Heo return -EBUSY; 1350*bba2c361STejun Heo } 1351*bba2c361STejun Heo 1352*bba2c361STejun Heo if (!check_builtin_idle_enabled(sch)) 1353*bba2c361STejun Heo return -EBUSY; 1354*bba2c361STejun Heo 1355*bba2c361STejun Heo return scx_pick_idle_cpu(cpus_allowed, NUMA_NO_NODE, flags); 1356*bba2c361STejun Heo } 1357*bba2c361STejun Heo 1358*bba2c361STejun Heo /** 1359*bba2c361STejun Heo * scx_bpf_pick_any_cpu_node - Pick and claim an idle cpu if available 1360*bba2c361STejun Heo * or pick any CPU from @node 1361*bba2c361STejun Heo * @cpus_allowed: Allowed cpumask 1362*bba2c361STejun Heo * @node: target NUMA node 1363*bba2c361STejun Heo * @flags: %SCX_PICK_IDLE_CPU_* flags 1364*bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 1365*bba2c361STejun Heo * 1366*bba2c361STejun Heo * Pick and claim an idle cpu in @cpus_allowed. If none is available, pick any 1367*bba2c361STejun Heo * CPU in @cpus_allowed. Guaranteed to succeed and returns the picked idle cpu 1368*bba2c361STejun Heo * number if @cpus_allowed is not empty. -%EBUSY is returned if @cpus_allowed is 1369*bba2c361STejun Heo * empty. 1370*bba2c361STejun Heo * 1371*bba2c361STejun Heo * The search starts from @node and proceeds to other online NUMA nodes in 1372*bba2c361STejun Heo * order of increasing distance (unless %SCX_PICK_IDLE_IN_NODE is specified, 1373*bba2c361STejun Heo * in which case the search is limited to the target @node, regardless of 1374*bba2c361STejun Heo * the CPU idle state). 1375*bba2c361STejun Heo * 1376*bba2c361STejun Heo * If ops.update_idle() is implemented and %SCX_OPS_KEEP_BUILTIN_IDLE is not 1377*bba2c361STejun Heo * set, this function can't tell which CPUs are idle and will always pick any 1378*bba2c361STejun Heo * CPU. 1379*bba2c361STejun Heo */ 1380*bba2c361STejun Heo __bpf_kfunc s32 scx_bpf_pick_any_cpu_node(const struct cpumask *cpus_allowed, 1381*bba2c361STejun Heo s32 node, u64 flags, 1382*bba2c361STejun Heo const struct bpf_prog_aux *aux) 1383*bba2c361STejun Heo { 1384*bba2c361STejun Heo struct scx_sched *sch; 1385*bba2c361STejun Heo s32 cpu; 1386*bba2c361STejun Heo 1387*bba2c361STejun Heo guard(rcu)(); 1388*bba2c361STejun Heo 1389*bba2c361STejun Heo sch = scx_prog_sched(aux); 1390*bba2c361STejun Heo if (unlikely(!sch)) 1391*bba2c361STejun Heo return -ENODEV; 1392*bba2c361STejun Heo 1393*bba2c361STejun Heo node = validate_node(sch, node); 1394*bba2c361STejun Heo if (node < 0) 1395*bba2c361STejun Heo return node; 1396*bba2c361STejun Heo 1397*bba2c361STejun Heo cpu = scx_pick_idle_cpu(cpus_allowed, node, flags); 1398*bba2c361STejun Heo if (cpu >= 0) 1399*bba2c361STejun Heo return cpu; 1400*bba2c361STejun Heo 1401*bba2c361STejun Heo if (flags & SCX_PICK_IDLE_IN_NODE) 1402*bba2c361STejun Heo cpu = cpumask_any_and_distribute(cpumask_of_node(node), cpus_allowed); 1403*bba2c361STejun Heo else 1404*bba2c361STejun Heo cpu = cpumask_any_distribute(cpus_allowed); 1405*bba2c361STejun Heo if (cpu < nr_cpu_ids) 1406*bba2c361STejun Heo return cpu; 1407*bba2c361STejun Heo else 1408*bba2c361STejun Heo return -EBUSY; 1409*bba2c361STejun Heo } 1410*bba2c361STejun Heo 1411*bba2c361STejun Heo /** 1412*bba2c361STejun Heo * scx_bpf_pick_any_cpu - Pick and claim an idle cpu if available or pick any CPU 1413*bba2c361STejun Heo * @cpus_allowed: Allowed cpumask 1414*bba2c361STejun Heo * @flags: %SCX_PICK_IDLE_CPU_* flags 1415*bba2c361STejun Heo * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs 1416*bba2c361STejun Heo * 1417*bba2c361STejun Heo * Pick and claim an idle cpu in @cpus_allowed. If none is available, pick any 1418*bba2c361STejun Heo * CPU in @cpus_allowed. Guaranteed to succeed and returns the picked idle cpu 1419*bba2c361STejun Heo * number if @cpus_allowed is not empty. -%EBUSY is returned if @cpus_allowed is 1420*bba2c361STejun Heo * empty. 1421*bba2c361STejun Heo * 1422*bba2c361STejun Heo * If ops.update_idle() is implemented and %SCX_OPS_KEEP_BUILTIN_IDLE is not 1423*bba2c361STejun Heo * set, this function can't tell which CPUs are idle and will always pick any 1424*bba2c361STejun Heo * CPU. 1425*bba2c361STejun Heo * 1426*bba2c361STejun Heo * Always returns an error if %SCX_OPS_BUILTIN_IDLE_PER_NODE is set, use 1427*bba2c361STejun Heo * scx_bpf_pick_any_cpu_node() instead. 1428*bba2c361STejun Heo */ 1429*bba2c361STejun Heo __bpf_kfunc s32 scx_bpf_pick_any_cpu(const struct cpumask *cpus_allowed, 1430*bba2c361STejun Heo u64 flags, const struct bpf_prog_aux *aux) 1431*bba2c361STejun Heo { 1432*bba2c361STejun Heo struct scx_sched *sch; 1433*bba2c361STejun Heo s32 cpu; 1434*bba2c361STejun Heo 1435*bba2c361STejun Heo guard(rcu)(); 1436*bba2c361STejun Heo 1437*bba2c361STejun Heo sch = scx_prog_sched(aux); 1438*bba2c361STejun Heo if (unlikely(!sch)) 1439*bba2c361STejun Heo return -ENODEV; 1440*bba2c361STejun Heo 1441*bba2c361STejun Heo if (static_branch_maybe(CONFIG_NUMA, &scx_builtin_idle_per_node)) { 1442*bba2c361STejun Heo scx_error(sch, "per-node idle tracking is enabled"); 1443*bba2c361STejun Heo return -EBUSY; 1444*bba2c361STejun Heo } 1445*bba2c361STejun Heo 1446*bba2c361STejun Heo if (static_branch_likely(&scx_builtin_idle_enabled)) { 1447*bba2c361STejun Heo cpu = scx_pick_idle_cpu(cpus_allowed, NUMA_NO_NODE, flags); 1448*bba2c361STejun Heo if (cpu >= 0) 1449*bba2c361STejun Heo return cpu; 1450*bba2c361STejun Heo } 1451*bba2c361STejun Heo 1452*bba2c361STejun Heo cpu = cpumask_any_distribute(cpus_allowed); 1453*bba2c361STejun Heo if (cpu < nr_cpu_ids) 1454*bba2c361STejun Heo return cpu; 1455*bba2c361STejun Heo else 1456*bba2c361STejun Heo return -EBUSY; 1457*bba2c361STejun Heo } 1458*bba2c361STejun Heo 1459*bba2c361STejun Heo __bpf_kfunc_end_defs(); 1460*bba2c361STejun Heo 1461*bba2c361STejun Heo BTF_KFUNCS_START(scx_kfunc_ids_idle) 1462*bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_cpu_node, KF_IMPLICIT_ARGS) 1463*bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_get_idle_cpumask_node, KF_IMPLICIT_ARGS | KF_ACQUIRE) 1464*bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_get_idle_cpumask, KF_IMPLICIT_ARGS | KF_ACQUIRE) 1465*bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_get_idle_smtmask_node, KF_IMPLICIT_ARGS | KF_ACQUIRE) 1466*bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_get_idle_smtmask, KF_IMPLICIT_ARGS | KF_ACQUIRE) 1467*bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_put_idle_cpumask, KF_RELEASE) 1468*bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_test_and_clear_cpu_idle, KF_IMPLICIT_ARGS) 1469*bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_pick_idle_cpu_node, KF_IMPLICIT_ARGS | KF_RCU) 1470*bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_pick_idle_cpu, KF_IMPLICIT_ARGS | KF_RCU) 1471*bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_pick_any_cpu_node, KF_IMPLICIT_ARGS | KF_RCU) 1472*bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_pick_any_cpu, KF_IMPLICIT_ARGS | KF_RCU) 1473*bba2c361STejun Heo BTF_KFUNCS_END(scx_kfunc_ids_idle) 1474*bba2c361STejun Heo 1475*bba2c361STejun Heo static const struct btf_kfunc_id_set scx_kfunc_set_idle = { 1476*bba2c361STejun Heo .owner = THIS_MODULE, 1477*bba2c361STejun Heo .set = &scx_kfunc_ids_idle, 1478*bba2c361STejun Heo .filter = scx_kfunc_context_filter, 1479*bba2c361STejun Heo }; 1480*bba2c361STejun Heo 1481*bba2c361STejun Heo /* 1482*bba2c361STejun Heo * The select_cpu kfuncs internally call task_rq_lock() when invoked from an 1483*bba2c361STejun Heo * rq-unlocked context, and thus cannot be safely called from arbitrary tracing 1484*bba2c361STejun Heo * contexts where @p's pi_lock state is unknown. Keep them out of 1485*bba2c361STejun Heo * BPF_PROG_TYPE_TRACING by registering them in their own set which is exposed 1486*bba2c361STejun Heo * only to STRUCT_OPS and SYSCALL programs. 1487*bba2c361STejun Heo * 1488*bba2c361STejun Heo * These kfuncs are also members of scx_kfunc_ids_unlocked (see ext.c) because 1489*bba2c361STejun Heo * they're callable from unlocked contexts in addition to ops.select_cpu() and 1490*bba2c361STejun Heo * ops.enqueue(). 1491*bba2c361STejun Heo */ 1492*bba2c361STejun Heo BTF_KFUNCS_START(scx_kfunc_ids_select_cpu) 1493*bba2c361STejun Heo BTF_ID_FLAGS(func, __scx_bpf_select_cpu_and, KF_IMPLICIT_ARGS | KF_RCU) 1494*bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_select_cpu_and, KF_RCU) 1495*bba2c361STejun Heo BTF_ID_FLAGS(func, scx_bpf_select_cpu_dfl, KF_IMPLICIT_ARGS | KF_RCU) 1496*bba2c361STejun Heo BTF_KFUNCS_END(scx_kfunc_ids_select_cpu) 1497*bba2c361STejun Heo 1498*bba2c361STejun Heo static const struct btf_kfunc_id_set scx_kfunc_set_select_cpu = { 1499*bba2c361STejun Heo .owner = THIS_MODULE, 1500*bba2c361STejun Heo .set = &scx_kfunc_ids_select_cpu, 1501*bba2c361STejun Heo .filter = scx_kfunc_context_filter, 1502*bba2c361STejun Heo }; 1503*bba2c361STejun Heo 1504*bba2c361STejun Heo int scx_idle_init(void) 1505*bba2c361STejun Heo { 1506*bba2c361STejun Heo return register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &scx_kfunc_set_idle) ?: 1507*bba2c361STejun Heo register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &scx_kfunc_set_idle) ?: 1508*bba2c361STejun Heo register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &scx_kfunc_set_idle) ?: 1509*bba2c361STejun Heo register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &scx_kfunc_set_select_cpu) ?: 1510*bba2c361STejun Heo register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &scx_kfunc_set_select_cpu); 1511*bba2c361STejun Heo } 1512