// SPDX-License-Identifier: GPL-2.0 /* * Scheduler topology setup/handling methods */ #include <linux/bsearch.h> DEFINE_MUTEX(…) …; /* Protected by sched_domains_mutex: */ static cpumask_var_t sched_domains_tmpmask; static cpumask_var_t sched_domains_tmpmask2; #ifdef CONFIG_SCHED_DEBUG static int __init sched_debug_setup(char *str) { … } early_param(…); static inline bool sched_debug(void) { … } #define SD_FLAG … const struct sd_flag_debug sd_flag_debug[] = …; #undef SD_FLAG static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level, struct cpumask *groupmask) { … } static void sched_domain_debug(struct sched_domain *sd, int cpu) { … } #else /* !CONFIG_SCHED_DEBUG */ #define sched_debug_verbose … #define sched_domain_debug … static inline bool sched_debug(void) { return false; } #endif /* CONFIG_SCHED_DEBUG */ /* Generate a mask of SD flags with the SDF_NEEDS_GROUPS metaflag */ #define SD_FLAG … static const unsigned int SD_DEGENERATE_GROUPS_MASK = …; #undef SD_FLAG static int sd_degenerate(struct sched_domain *sd) { … } static int sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) { … } #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL) DEFINE_STATIC_KEY_FALSE(sched_energy_present); static unsigned int sysctl_sched_energy_aware = …; static DEFINE_MUTEX(sched_energy_mutex); static bool sched_energy_update; static bool sched_is_eas_possible(const struct cpumask *cpu_mask) { … } void rebuild_sched_domains_energy(void) { … } #ifdef CONFIG_PROC_SYSCTL static int sched_energy_aware_handler(const struct ctl_table *table, int write, void *buffer, size_t *lenp, loff_t *ppos) { … } static struct ctl_table sched_energy_aware_sysctls[] = …; static int __init sched_energy_aware_sysctl_init(void) { … } late_initcall(sched_energy_aware_sysctl_init); #endif static void free_pd(struct perf_domain *pd) { … } static struct perf_domain *find_pd(struct perf_domain *pd, int cpu) { … } static struct perf_domain *pd_init(int cpu) { … } static void perf_domain_debug(const struct cpumask *cpu_map, struct perf_domain *pd) { … } static void destroy_perf_domain_rcu(struct rcu_head *rp) { … } static void sched_energy_set(bool has_eas) { … } /* * EAS can be used on a root domain if it meets all the following conditions: * 1. an Energy Model (EM) is available; * 2. the SD_ASYM_CPUCAPACITY flag is set in the sched_domain hierarchy. * 3. no SMT is detected. * 4. schedutil is driving the frequency of all CPUs of the rd; * 5. frequency invariance support is present; */ static bool build_perf_domains(const struct cpumask *cpu_map) { … } #else static void free_pd(struct perf_domain *pd) { } #endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL*/ static void free_rootdomain(struct rcu_head *rcu) { … } void rq_attach_root(struct rq *rq, struct root_domain *rd) { … } void sched_get_rd(struct root_domain *rd) { … } void sched_put_rd(struct root_domain *rd) { … } static int init_rootdomain(struct root_domain *rd) { … } /* * By default the system creates a single root-domain with all CPUs as * members (mimicking the global state we have today). */ struct root_domain def_root_domain; void __init init_defrootdomain(void) { … } static struct root_domain *alloc_rootdomain(void) { … } static void free_sched_groups(struct sched_group *sg, int free_sgc) { … } static void destroy_sched_domain(struct sched_domain *sd) { … } static void destroy_sched_domains_rcu(struct rcu_head *rcu) { … } static void destroy_sched_domains(struct sched_domain *sd) { … } /* * Keep a special pointer to the highest sched_domain that has SD_SHARE_LLC set * (Last Level Cache Domain) for this allows us to avoid some pointer chasing * select_idle_sibling(). * * Also keep a unique ID per domain (we use the first CPU number in the cpumask * of the domain), this allows us to quickly tell if two CPUs are in the same * cache domain, see cpus_share_cache(). */ DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc); DEFINE_PER_CPU(int, sd_llc_size); DEFINE_PER_CPU(int, sd_llc_id); DEFINE_PER_CPU(int, sd_share_id); DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared); DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa); DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing); DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity); DEFINE_STATIC_KEY_FALSE(sched_asym_cpucapacity); DEFINE_STATIC_KEY_FALSE(sched_cluster_active); static void update_top_cache_domain(int cpu) { … } /* * Attach the domain 'sd' to 'cpu' as its base domain. Callers must * hold the hotplug lock. */ static void cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu) { … } struct s_data { … }; enum s_alloc { … }; /* * Return the canonical balance CPU for this group, this is the first CPU * of this group that's also in the balance mask. * * The balance mask are all those CPUs that could actually end up at this * group. See build_balance_mask(). * * Also see should_we_balance(). */ int group_balance_cpu(struct sched_group *sg) { … } /* * NUMA topology (first read the regular topology blurb below) * * Given a node-distance table, for example: * * node 0 1 2 3 * 0: 10 20 30 20 * 1: 20 10 20 30 * 2: 30 20 10 20 * 3: 20 30 20 10 * * which represents a 4 node ring topology like: * * 0 ----- 1 * | | * | | * | | * 3 ----- 2 * * We want to construct domains and groups to represent this. The way we go * about doing this is to build the domains on 'hops'. For each NUMA level we * construct the mask of all nodes reachable in @level hops. * * For the above NUMA topology that gives 3 levels: * * NUMA-2 0-3 0-3 0-3 0-3 * groups: {0-1,3},{1-3} {0-2},{0,2-3} {1-3},{0-1,3} {0,2-3},{0-2} * * NUMA-1 0-1,3 0-2 1-3 0,2-3 * groups: {0},{1},{3} {0},{1},{2} {1},{2},{3} {0},{2},{3} * * NUMA-0 0 1 2 3 * * * As can be seen; things don't nicely line up as with the regular topology. * When we iterate a domain in child domain chunks some nodes can be * represented multiple times -- hence the "overlap" naming for this part of * the topology. * * In order to minimize this overlap, we only build enough groups to cover the * domain. For instance Node-0 NUMA-2 would only get groups: 0-1,3 and 1-3. * * Because: * * - the first group of each domain is its child domain; this * gets us the first 0-1,3 * - the only uncovered node is 2, who's child domain is 1-3. * * However, because of the overlap, computing a unique CPU for each group is * more complicated. Consider for instance the groups of NODE-1 NUMA-2, both * groups include the CPUs of Node-0, while those CPUs would not in fact ever * end up at those groups (they would end up in group: 0-1,3). * * To correct this we have to introduce the group balance mask. This mask * will contain those CPUs in the group that can reach this group given the * (child) domain tree. * * With this we can once again compute balance_cpu and sched_group_capacity * relations. * * XXX include words on how balance_cpu is unique and therefore can be * used for sched_group_capacity links. * * * Another 'interesting' topology is: * * node 0 1 2 3 * 0: 10 20 20 30 * 1: 20 10 20 20 * 2: 20 20 10 20 * 3: 30 20 20 10 * * Which looks a little like: * * 0 ----- 1 * | / | * | / | * | / | * 2 ----- 3 * * This topology is asymmetric, nodes 1,2 are fully connected, but nodes 0,3 * are not. * * This leads to a few particularly weird cases where the sched_domain's are * not of the same number for each CPU. Consider: * * NUMA-2 0-3 0-3 * groups: {0-2},{1-3} {1-3},{0-2} * * NUMA-1 0-2 0-3 0-3 1-3 * * NUMA-0 0 1 2 3 * */ /* * Build the balance mask; it contains only those CPUs that can arrive at this * group and should be considered to continue balancing. * * We do this during the group creation pass, therefore the group information * isn't complete yet, however since each group represents a (child) domain we * can fully construct this using the sched_domain bits (which are already * complete). */ static void build_balance_mask(struct sched_domain *sd, struct sched_group *sg, struct cpumask *mask) { … } /* * XXX: This creates per-node group entries; since the load-balancer will * immediately access remote memory to construct this group's load-balance * statistics having the groups node local is of dubious benefit. */ static struct sched_group * build_group_from_child_sched_domain(struct sched_domain *sd, int cpu) { … } static void init_overlap_sched_group(struct sched_domain *sd, struct sched_group *sg) { … } static struct sched_domain * find_descended_sibling(struct sched_domain *sd, struct sched_domain *sibling) { … } static int build_overlap_sched_groups(struct sched_domain *sd, int cpu) { … } /* * Package topology (also see the load-balance blurb in fair.c) * * The scheduler builds a tree structure to represent a number of important * topology features. By default (default_topology[]) these include: * * - Simultaneous multithreading (SMT) * - Multi-Core Cache (MC) * - Package (PKG) * * Where the last one more or less denotes everything up to a NUMA node. * * The tree consists of 3 primary data structures: * * sched_domain -> sched_group -> sched_group_capacity * ^ ^ ^ ^ * `-' `-' * * The sched_domains are per-CPU and have a two way link (parent & child) and * denote the ever growing mask of CPUs belonging to that level of topology. * * Each sched_domain has a circular (double) linked list of sched_group's, each * denoting the domains of the level below (or individual CPUs in case of the * first domain level). The sched_group linked by a sched_domain includes the * CPU of that sched_domain [*]. * * Take for instance a 2 threaded, 2 core, 2 cache cluster part: * * CPU 0 1 2 3 4 5 6 7 * * PKG [ ] * MC [ ] [ ] * SMT [ ] [ ] [ ] [ ] * * - or - * * PKG 0-7 0-7 0-7 0-7 0-7 0-7 0-7 0-7 * MC 0-3 0-3 0-3 0-3 4-7 4-7 4-7 4-7 * SMT 0-1 0-1 2-3 2-3 4-5 4-5 6-7 6-7 * * CPU 0 1 2 3 4 5 6 7 * * One way to think about it is: sched_domain moves you up and down among these * topology levels, while sched_group moves you sideways through it, at child * domain granularity. * * sched_group_capacity ensures each unique sched_group has shared storage. * * There are two related construction problems, both require a CPU that * uniquely identify each group (for a given domain): * * - The first is the balance_cpu (see should_we_balance() and the * load-balance blurb in fair.c); for each group we only want 1 CPU to * continue balancing at a higher domain. * * - The second is the sched_group_capacity; we want all identical groups * to share a single sched_group_capacity. * * Since these topologies are exclusive by construction. That is, its * impossible for an SMT thread to belong to multiple cores, and cores to * be part of multiple caches. There is a very clear and unique location * for each CPU in the hierarchy. * * Therefore computing a unique CPU for each group is trivial (the iteration * mask is redundant and set all 1s; all CPUs in a group will end up at _that_ * group), we can simply pick the first CPU in each group. * * * [*] in other words, the first group of each domain is its child domain. */ static struct sched_group *get_group(int cpu, struct sd_data *sdd) { … } /* * build_sched_groups will build a circular linked list of the groups * covered by the given span, will set each group's ->cpumask correctly, * and will initialize their ->sgc. * * Assumes the sched_domain tree is fully constructed */ static int build_sched_groups(struct sched_domain *sd, int cpu) { … } /* * Initialize sched groups cpu_capacity. * * cpu_capacity indicates the capacity of sched group, which is used while * distributing the load between different sched groups in a sched domain. * Typically cpu_capacity for all the groups in a sched domain will be same * unless there are asymmetries in the topology. If there are asymmetries, * group having more cpu_capacity will pickup more load compared to the * group having less cpu_capacity. */ static void init_sched_groups_capacity(int cpu, struct sched_domain *sd) { … } /* * Set of available CPUs grouped by their corresponding capacities * Each list entry contains a CPU mask reflecting CPUs that share the same * capacity. * The lifespan of data is unlimited. */ LIST_HEAD(…); /* * Verify whether there is any CPU capacity asymmetry in a given sched domain. * Provides sd_flags reflecting the asymmetry scope. */ static inline int asym_cpu_capacity_classify(const struct cpumask *sd_span, const struct cpumask *cpu_map) { … } static void free_asym_cap_entry(struct rcu_head *head) { … } static inline void asym_cpu_capacity_update_data(int cpu) { … } /* * Build-up/update list of CPUs grouped by their capacities * An update requires explicit request to rebuild sched domains * with state indicating CPU topology changes. */ static void asym_cpu_capacity_scan(void) { … } /* * Initializers for schedule domains * Non-inlined to reduce accumulated stack pressure in build_sched_domains() */ static int default_relax_domain_level = …; int sched_domain_level_max; static int __init setup_relax_domain_level(char *str) { … } __setup(…); static void set_domain_attribute(struct sched_domain *sd, struct sched_domain_attr *attr) { … } static void __sdt_free(const struct cpumask *cpu_map); static int __sdt_alloc(const struct cpumask *cpu_map); static void __free_domain_allocs(struct s_data *d, enum s_alloc what, const struct cpumask *cpu_map) { … } static enum s_alloc __visit_domain_allocation_hell(struct s_data *d, const struct cpumask *cpu_map) { … } /* * NULL the sd_data elements we've used to build the sched_domain and * sched_group structure so that the subsequent __free_domain_allocs() * will not free the data we're using. */ static void claim_allocations(int cpu, struct sched_domain *sd) { … } #ifdef CONFIG_NUMA enum numa_topology_type sched_numa_topology_type; static int sched_domains_numa_levels; static int sched_domains_curr_level; int sched_max_numa_distance; static int *sched_domains_numa_distance; static struct cpumask ***sched_domains_numa_masks; #endif /* * SD_flags allowed in topology descriptions. * * These flags are purely descriptive of the topology and do not prescribe * behaviour. Behaviour is artificial and mapped in the below sd_init() * function. For details, see include/linux/sched/sd_flags.h. * * SD_SHARE_CPUCAPACITY * SD_SHARE_LLC * SD_CLUSTER * SD_NUMA * * Odd one out, which beside describing the topology has a quirk also * prescribes the desired behaviour that goes along with it: * * SD_ASYM_PACKING - describes SMT quirks */ #define TOPOLOGY_SD_FLAGS … static struct sched_domain * sd_init(struct sched_domain_topology_level *tl, const struct cpumask *cpu_map, struct sched_domain *child, int cpu) { … } /* * Topology list, bottom-up. */ static struct sched_domain_topology_level default_topology[] = …; static struct sched_domain_topology_level *sched_domain_topology = …; static struct sched_domain_topology_level *sched_domain_topology_saved; #define for_each_sd_topology(tl) … void __init set_sched_topology(struct sched_domain_topology_level *tl) { … } #ifdef CONFIG_NUMA static const struct cpumask *sd_numa_mask(int cpu) { … } static void sched_numa_warn(const char *str) { … } bool find_numa_distance(int distance) { … } #define for_each_cpu_node_but(n, nbut) … /* * A system can have three types of NUMA topology: * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes * NUMA_BACKPLANE: nodes can reach other nodes through a backplane * * The difference between a glueless mesh topology and a backplane * topology lies in whether communication between not directly * connected nodes goes through intermediary nodes (where programs * could run), or through backplane controllers. This affects * placement of programs. * * The type of topology can be discerned with the following tests: * - If the maximum distance between any nodes is 1 hop, the system * is directly connected. * - If for two nodes A and B, located N > 1 hops away from each other, * there is an intermediary node C, which is < N hops away from both * nodes A and B, the system is a glueless mesh. */ static void init_numa_topology_type(int offline_node) { … } #define NR_DISTANCE_VALUES … void sched_init_numa(int offline_node) { … } static void sched_reset_numa(void) { … } /* * Call with hotplug lock held */ void sched_update_numa(int cpu, bool online) { … } void sched_domains_numa_masks_set(unsigned int cpu) { … } void sched_domains_numa_masks_clear(unsigned int cpu) { … } /* * sched_numa_find_closest() - given the NUMA topology, find the cpu * closest to @cpu from @cpumask. * cpumask: cpumask to find a cpu from * cpu: cpu to be close to * * returns: cpu, or nr_cpu_ids when nothing found. */ int sched_numa_find_closest(const struct cpumask *cpus, int cpu) { … } struct __cmp_key { … }; static int hop_cmp(const void *a, const void *b) { … } /** * sched_numa_find_nth_cpu() - given the NUMA topology, find the Nth closest CPU * from @cpus to @cpu, taking into account distance * from a given @node. * @cpus: cpumask to find a cpu from * @cpu: CPU to start searching * @node: NUMA node to order CPUs by distance * * Return: cpu, or nr_cpu_ids when nothing found. */ int sched_numa_find_nth_cpu(const struct cpumask *cpus, int cpu, int node) { … } EXPORT_SYMBOL_GPL(…); /** * sched_numa_hop_mask() - Get the cpumask of CPUs at most @hops hops away from * @node * @node: The node to count hops from. * @hops: Include CPUs up to that many hops away. 0 means local node. * * Return: On success, a pointer to a cpumask of CPUs at most @hops away from * @node, an error value otherwise. * * Requires rcu_lock to be held. Returned cpumask is only valid within that * read-side section, copy it if required beyond that. * * Note that not all hops are equal in distance; see sched_init_numa() for how * distances and masks are handled. * Also note that this is a reflection of sched_domains_numa_masks, which may change * during the lifetime of the system (offline nodes are taken out of the masks). */ const struct cpumask *sched_numa_hop_mask(unsigned int node, unsigned int hops) { … } EXPORT_SYMBOL_GPL(…); #endif /* CONFIG_NUMA */ static int __sdt_alloc(const struct cpumask *cpu_map) { … } static void __sdt_free(const struct cpumask *cpu_map) { … } static struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl, const struct cpumask *cpu_map, struct sched_domain_attr *attr, struct sched_domain *child, int cpu) { … } /* * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for * any two given CPUs at this (non-NUMA) topology level. */ static bool topology_span_sane(struct sched_domain_topology_level *tl, const struct cpumask *cpu_map, int cpu) { … } /* * Build sched domains for a given set of CPUs and attach the sched domains * to the individual CPUs */ static int build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *attr) { … } /* Current sched domains: */ static cpumask_var_t *doms_cur; /* Number of sched domains in 'doms_cur': */ static int ndoms_cur; /* Attributes of custom domains in 'doms_cur' */ static struct sched_domain_attr *dattr_cur; /* * Special case: If a kmalloc() of a doms_cur partition (array of * cpumask) fails, then fallback to a single sched domain, * as determined by the single cpumask fallback_doms. */ static cpumask_var_t fallback_doms; /* * arch_update_cpu_topology lets virtualized architectures update the * CPU core maps. It is supposed to return 1 if the topology changed * or 0 if it stayed the same. */ int __weak arch_update_cpu_topology(void) { … } cpumask_var_t *alloc_sched_domains(unsigned int ndoms) { … } void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms) { … } /* * Set up scheduler domains and groups. For now this just excludes isolated * CPUs, but could be used to exclude other special cases in the future. */ int __init sched_init_domains(const struct cpumask *cpu_map) { … } /* * Detach sched domains from a group of CPUs specified in cpu_map * These CPUs will now be attached to the NULL domain */ static void detach_destroy_domains(const struct cpumask *cpu_map) { … } /* handle null as "default" */ static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur, struct sched_domain_attr *new, int idx_new) { … } /* * Partition sched domains as specified by the 'ndoms_new' * cpumasks in the array doms_new[] of cpumasks. This compares * doms_new[] to the current sched domain partitioning, doms_cur[]. * It destroys each deleted domain and builds each new domain. * * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'. * The masks don't intersect (don't overlap.) We should setup one * sched domain for each mask. CPUs not in any of the cpumasks will * not be load balanced. If the same cpumask appears both in the * current 'doms_cur' domains and in the new 'doms_new', we can leave * it as it is. * * The passed in 'doms_new' should be allocated using * alloc_sched_domains. This routine takes ownership of it and will * free_sched_domains it when done with it. If the caller failed the * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1, * and partition_sched_domains() will fallback to the single partition * 'fallback_doms', it also forces the domains to be rebuilt. * * If doms_new == NULL it will be replaced with cpu_online_mask. * ndoms_new == 0 is a special case for destroying existing domains, * and it will not create the default domain. * * Call with hotplug lock and sched_domains_mutex held */ void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new[], struct sched_domain_attr *dattr_new) { … } /* * Call with hotplug lock held */ void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[], struct sched_domain_attr *dattr_new) { … }