1 /* 2 * include/linux/topology.h 3 * 4 * Written by: Matthew Dobson, IBM Corporation 5 * 6 * Copyright (C) 2002, IBM Corp. 7 * 8 * All rights reserved. 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, but 16 * WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or 18 * NON INFRINGEMENT. See the GNU General Public License for more 19 * details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with this program; if not, write to the Free Software 23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 24 * 25 * Send feedback to <[email protected]> 26 */ 27 #ifndef _LINUX_TOPOLOGY_H 28 #define _LINUX_TOPOLOGY_H 29 30 #include <linux/arch_topology.h> 31 #include <linux/cpumask.h> 32 #include <linux/bitops.h> 33 #include <linux/mmzone.h> 34 #include <linux/smp.h> 35 #include <linux/percpu.h> 36 #include <asm/topology.h> 37 38 #ifndef nr_cpus_node 39 #define nr_cpus_node(node) cpumask_weight(cpumask_of_node(node)) 40 #endif 41 42 #define for_each_node_with_cpus(node) \ 43 for_each_online_node(node) \ 44 if (nr_cpus_node(node)) 45 46 int arch_update_cpu_topology(void); 47 48 /* Conform to ACPI 2.0 SLIT distance definitions */ 49 #define LOCAL_DISTANCE 10 50 #define REMOTE_DISTANCE 20 51 #define DISTANCE_BITS 8 52 #ifndef node_distance 53 #define node_distance(from,to) ((from) == (to) ? LOCAL_DISTANCE : REMOTE_DISTANCE) 54 #endif 55 #ifndef RECLAIM_DISTANCE 56 /* 57 * If the distance between nodes in a system is larger than RECLAIM_DISTANCE 58 * (in whatever arch specific measurement units returned by node_distance()) 59 * and node_reclaim_mode is enabled then the VM will only call node_reclaim() 60 * on nodes within this distance. 61 */ 62 #define RECLAIM_DISTANCE 30 63 #endif 64 65 /* 66 * The following tunable allows platforms to override the default node 67 * reclaim distance (RECLAIM_DISTANCE) if remote memory accesses are 68 * sufficiently fast that the default value actually hurts 69 * performance. 70 * 71 * AMD EPYC machines use this because even though the 2-hop distance 72 * is 32 (3.2x slower than a local memory access) performance actually 73 * *improves* if allowed to reclaim memory and load balance tasks 74 * between NUMA nodes 2-hops apart. 75 */ 76 extern int __read_mostly node_reclaim_distance; 77 78 #ifndef PENALTY_FOR_NODE_WITH_CPUS 79 #define PENALTY_FOR_NODE_WITH_CPUS (1) 80 #endif 81 82 #ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID 83 DECLARE_PER_CPU(int, numa_node); 84 85 #ifndef numa_node_id 86 /* Returns the number of the current Node. */ 87 static inline int numa_node_id(void) 88 { 89 return raw_cpu_read(numa_node); 90 } 91 #endif 92 93 #ifndef cpu_to_node 94 static inline int cpu_to_node(int cpu) 95 { 96 return per_cpu(numa_node, cpu); 97 } 98 #endif 99 100 #ifndef set_numa_node 101 static inline void set_numa_node(int node) 102 { 103 this_cpu_write(numa_node, node); 104 } 105 #endif 106 107 #ifndef set_cpu_numa_node 108 static inline void set_cpu_numa_node(int cpu, int node) 109 { 110 per_cpu(numa_node, cpu) = node; 111 } 112 #endif 113 114 #else /* !CONFIG_USE_PERCPU_NUMA_NODE_ID */ 115 116 /* Returns the number of the current Node. */ 117 #ifndef numa_node_id 118 static inline int numa_node_id(void) 119 { 120 return cpu_to_node(raw_smp_processor_id()); 121 } 122 #endif 123 124 #endif /* [!]CONFIG_USE_PERCPU_NUMA_NODE_ID */ 125 126 #ifdef CONFIG_HAVE_MEMORYLESS_NODES 127 128 /* 129 * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly. 130 * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined. 131 * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem(). 132 */ 133 DECLARE_PER_CPU(int, _numa_mem_); 134 135 #ifndef set_numa_mem 136 static inline void set_numa_mem(int node) 137 { 138 this_cpu_write(_numa_mem_, node); 139 } 140 #endif 141 142 #ifndef numa_mem_id 143 /* Returns the number of the nearest Node with memory */ 144 static inline int numa_mem_id(void) 145 { 146 return raw_cpu_read(_numa_mem_); 147 } 148 #endif 149 150 #ifndef cpu_to_mem 151 static inline int cpu_to_mem(int cpu) 152 { 153 return per_cpu(_numa_mem_, cpu); 154 } 155 #endif 156 157 #ifndef set_cpu_numa_mem 158 static inline void set_cpu_numa_mem(int cpu, int node) 159 { 160 per_cpu(_numa_mem_, cpu) = node; 161 } 162 #endif 163 164 #else /* !CONFIG_HAVE_MEMORYLESS_NODES */ 165 166 #ifndef numa_mem_id 167 /* Returns the number of the nearest Node with memory */ 168 static inline int numa_mem_id(void) 169 { 170 return numa_node_id(); 171 } 172 #endif 173 174 #ifndef cpu_to_mem 175 static inline int cpu_to_mem(int cpu) 176 { 177 return cpu_to_node(cpu); 178 } 179 #endif 180 181 #endif /* [!]CONFIG_HAVE_MEMORYLESS_NODES */ 182 183 #if defined(topology_die_id) && defined(topology_die_cpumask) 184 #define TOPOLOGY_DIE_SYSFS 185 #endif 186 #if defined(topology_cluster_id) && defined(topology_cluster_cpumask) 187 #define TOPOLOGY_CLUSTER_SYSFS 188 #endif 189 #if defined(topology_book_id) && defined(topology_book_cpumask) 190 #define TOPOLOGY_BOOK_SYSFS 191 #endif 192 #if defined(topology_drawer_id) && defined(topology_drawer_cpumask) 193 #define TOPOLOGY_DRAWER_SYSFS 194 #endif 195 196 #ifndef topology_physical_package_id 197 #define topology_physical_package_id(cpu) ((void)(cpu), -1) 198 #endif 199 #ifndef topology_die_id 200 #define topology_die_id(cpu) ((void)(cpu), -1) 201 #endif 202 #ifndef topology_cluster_id 203 #define topology_cluster_id(cpu) ((void)(cpu), -1) 204 #endif 205 #ifndef topology_core_id 206 #define topology_core_id(cpu) ((void)(cpu), 0) 207 #endif 208 #ifndef topology_book_id 209 #define topology_book_id(cpu) ((void)(cpu), -1) 210 #endif 211 #ifndef topology_drawer_id 212 #define topology_drawer_id(cpu) ((void)(cpu), -1) 213 #endif 214 #ifndef topology_ppin 215 #define topology_ppin(cpu) ((void)(cpu), 0ull) 216 #endif 217 #ifndef topology_sibling_cpumask 218 #define topology_sibling_cpumask(cpu) cpumask_of(cpu) 219 #endif 220 #ifndef topology_core_cpumask 221 #define topology_core_cpumask(cpu) cpumask_of(cpu) 222 #endif 223 #ifndef topology_cluster_cpumask 224 #define topology_cluster_cpumask(cpu) cpumask_of(cpu) 225 #endif 226 #ifndef topology_die_cpumask 227 #define topology_die_cpumask(cpu) cpumask_of(cpu) 228 #endif 229 #ifndef topology_book_cpumask 230 #define topology_book_cpumask(cpu) cpumask_of(cpu) 231 #endif 232 #ifndef topology_drawer_cpumask 233 #define topology_drawer_cpumask(cpu) cpumask_of(cpu) 234 #endif 235 236 #if defined(CONFIG_SCHED_SMT) && !defined(cpu_smt_mask) 237 static inline const struct cpumask *cpu_smt_mask(int cpu) 238 { 239 return topology_sibling_cpumask(cpu); 240 } 241 #endif 242 243 #ifndef topology_is_primary_thread 244 245 static inline bool topology_is_primary_thread(unsigned int cpu) 246 { 247 /* 248 * When disabling SMT, the primary thread of the SMT will remain 249 * enabled/active. Architectures that have a special primary thread 250 * (e.g. x86) need to override this function. Otherwise the first 251 * thread in the SMT can be made the primary thread. 252 * 253 * The sibling cpumask of an offline CPU always contains the CPU 254 * itself on architectures using the implementation of 255 * CONFIG_GENERIC_ARCH_TOPOLOGY for building their topology. 256 * Other architectures not using CONFIG_GENERIC_ARCH_TOPOLOGY for 257 * building their topology have to check whether to use this default 258 * implementation or to override it. 259 */ 260 return cpu == cpumask_first(topology_sibling_cpumask(cpu)); 261 } 262 #define topology_is_primary_thread topology_is_primary_thread 263 264 #endif 265 266 static inline const struct cpumask *cpu_cpu_mask(int cpu) 267 { 268 return cpumask_of_node(cpu_to_node(cpu)); 269 } 270 271 #ifdef CONFIG_NUMA 272 int sched_numa_find_nth_cpu(const struct cpumask *cpus, int cpu, int node); 273 extern const struct cpumask *sched_numa_hop_mask(unsigned int node, unsigned int hops); 274 #else 275 static __always_inline int sched_numa_find_nth_cpu(const struct cpumask *cpus, int cpu, int node) 276 { 277 return cpumask_nth_and(cpu, cpus, cpu_online_mask); 278 } 279 280 static inline const struct cpumask * 281 sched_numa_hop_mask(unsigned int node, unsigned int hops) 282 { 283 return ERR_PTR(-EOPNOTSUPP); 284 } 285 #endif /* CONFIG_NUMA */ 286 287 /** 288 * for_each_node_numadist() - iterate over nodes in increasing distance 289 * order, starting from a given node 290 * @node: the iteration variable and the starting node. 291 * @unvisited: a nodemask to keep track of the unvisited nodes. 292 * 293 * This macro iterates over NUMA node IDs in increasing distance from the 294 * starting @node and yields MAX_NUMNODES when all the nodes have been 295 * visited. 296 * 297 * Note that by the time the loop completes, the @unvisited nodemask will 298 * be fully cleared, unless the loop exits early. 299 * 300 * The difference between for_each_node() and for_each_node_numadist() is 301 * that the former allows to iterate over nodes in numerical order, whereas 302 * the latter iterates over nodes in increasing order of distance. 303 * 304 * This complexity of this iterator is O(N^2), where N represents the 305 * number of nodes, as each iteration involves scanning all nodes to 306 * find the one with the shortest distance. 307 * 308 * Requires rcu_lock to be held. 309 */ 310 #define for_each_node_numadist(node, unvisited) \ 311 for (int __start = (node), \ 312 (node) = nearest_node_nodemask((__start), &(unvisited)); \ 313 (node) < MAX_NUMNODES; \ 314 node_clear((node), (unvisited)), \ 315 (node) = nearest_node_nodemask((__start), &(unvisited))) 316 317 /** 318 * for_each_numa_hop_mask - iterate over cpumasks of increasing NUMA distance 319 * from a given node. 320 * @mask: the iteration variable. 321 * @node: the NUMA node to start the search from. 322 * 323 * Requires rcu_lock to be held. 324 * 325 * Yields cpu_online_mask for @node == NUMA_NO_NODE. 326 */ 327 #define for_each_numa_hop_mask(mask, node) \ 328 for (unsigned int __hops = 0; \ 329 mask = (node != NUMA_NO_NODE || __hops) ? \ 330 sched_numa_hop_mask(node, __hops) : \ 331 cpu_online_mask, \ 332 !IS_ERR_OR_NULL(mask); \ 333 __hops++) 334 335 #endif /* _LINUX_TOPOLOGY_H */ 336