Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Scheduler topology setup/handling methods |
| 3 | */ |
| 4 | #include <linux/sched.h> |
| 5 | #include <linux/mutex.h> |
| 6 | |
| 7 | #include "sched.h" |
| 8 | |
| 9 | DEFINE_MUTEX(sched_domains_mutex); |
| 10 | |
| 11 | /* Protected by sched_domains_mutex: */ |
| 12 | cpumask_var_t sched_domains_tmpmask; |
Peter Zijlstra | 1676330 | 2017-04-25 14:31:11 +0200 | [diff] [blame] | 13 | cpumask_var_t sched_domains_tmpmask2; |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 14 | |
| 15 | #ifdef CONFIG_SCHED_DEBUG |
| 16 | |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 17 | static int __init sched_debug_setup(char *str) |
| 18 | { |
Peter Zijlstra | 9469eb0 | 2017-09-07 17:03:53 +0200 | [diff] [blame] | 19 | sched_debug_enabled = true; |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 20 | |
| 21 | return 0; |
| 22 | } |
| 23 | early_param("sched_debug", sched_debug_setup); |
| 24 | |
| 25 | static inline bool sched_debug(void) |
| 26 | { |
| 27 | return sched_debug_enabled; |
| 28 | } |
| 29 | |
| 30 | static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level, |
| 31 | struct cpumask *groupmask) |
| 32 | { |
| 33 | struct sched_group *group = sd->groups; |
| 34 | |
| 35 | cpumask_clear(groupmask); |
| 36 | |
Peter Zijlstra | 005f874 | 2017-04-26 17:35:35 +0200 | [diff] [blame] | 37 | printk(KERN_DEBUG "%*s domain-%d: ", level, "", level); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 38 | |
| 39 | if (!(sd->flags & SD_LOAD_BALANCE)) { |
| 40 | printk("does not load-balance\n"); |
| 41 | if (sd->parent) |
| 42 | printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain" |
| 43 | " has parent"); |
| 44 | return -1; |
| 45 | } |
| 46 | |
Peter Zijlstra | 005f874 | 2017-04-26 17:35:35 +0200 | [diff] [blame] | 47 | printk(KERN_CONT "span=%*pbl level=%s\n", |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 48 | cpumask_pr_args(sched_domain_span(sd)), sd->name); |
| 49 | |
| 50 | if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) { |
| 51 | printk(KERN_ERR "ERROR: domain->span does not contain " |
| 52 | "CPU%d\n", cpu); |
| 53 | } |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 54 | if (!cpumask_test_cpu(cpu, sched_group_span(group))) { |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 55 | printk(KERN_ERR "ERROR: domain->groups does not contain" |
| 56 | " CPU%d\n", cpu); |
| 57 | } |
| 58 | |
| 59 | printk(KERN_DEBUG "%*s groups:", level + 1, ""); |
| 60 | do { |
| 61 | if (!group) { |
| 62 | printk("\n"); |
| 63 | printk(KERN_ERR "ERROR: group is NULL\n"); |
| 64 | break; |
| 65 | } |
| 66 | |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 67 | if (!cpumask_weight(sched_group_span(group))) { |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 68 | printk(KERN_CONT "\n"); |
| 69 | printk(KERN_ERR "ERROR: empty group\n"); |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | if (!(sd->flags & SD_OVERLAP) && |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 74 | cpumask_intersects(groupmask, sched_group_span(group))) { |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 75 | printk(KERN_CONT "\n"); |
| 76 | printk(KERN_ERR "ERROR: repeated CPUs\n"); |
| 77 | break; |
| 78 | } |
| 79 | |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 80 | cpumask_or(groupmask, groupmask, sched_group_span(group)); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 81 | |
Peter Zijlstra | 005f874 | 2017-04-26 17:35:35 +0200 | [diff] [blame] | 82 | printk(KERN_CONT " %d:{ span=%*pbl", |
| 83 | group->sgc->id, |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 84 | cpumask_pr_args(sched_group_span(group))); |
Peter Zijlstra | b0151c2 | 2017-04-14 17:29:16 +0200 | [diff] [blame] | 85 | |
Peter Zijlstra | af21812 | 2017-05-01 08:51:05 +0200 | [diff] [blame] | 86 | if ((sd->flags & SD_OVERLAP) && |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 87 | !cpumask_equal(group_balance_mask(group), sched_group_span(group))) { |
Peter Zijlstra | 005f874 | 2017-04-26 17:35:35 +0200 | [diff] [blame] | 88 | printk(KERN_CONT " mask=%*pbl", |
Peter Zijlstra | e5c14b1 | 2017-05-01 10:47:02 +0200 | [diff] [blame] | 89 | cpumask_pr_args(group_balance_mask(group))); |
Peter Zijlstra | b0151c2 | 2017-04-14 17:29:16 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Peter Zijlstra | 005f874 | 2017-04-26 17:35:35 +0200 | [diff] [blame] | 92 | if (group->sgc->capacity != SCHED_CAPACITY_SCALE) |
| 93 | printk(KERN_CONT " cap=%lu", group->sgc->capacity); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 94 | |
Peter Zijlstra | a420b06 | 2017-04-14 18:20:48 +0200 | [diff] [blame] | 95 | if (group == sd->groups && sd->child && |
| 96 | !cpumask_equal(sched_domain_span(sd->child), |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 97 | sched_group_span(group))) { |
Peter Zijlstra | a420b06 | 2017-04-14 18:20:48 +0200 | [diff] [blame] | 98 | printk(KERN_ERR "ERROR: domain->groups does not match domain->child\n"); |
| 99 | } |
| 100 | |
Peter Zijlstra | 005f874 | 2017-04-26 17:35:35 +0200 | [diff] [blame] | 101 | printk(KERN_CONT " }"); |
| 102 | |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 103 | group = group->next; |
Peter Zijlstra | b0151c2 | 2017-04-14 17:29:16 +0200 | [diff] [blame] | 104 | |
| 105 | if (group != sd->groups) |
| 106 | printk(KERN_CONT ","); |
| 107 | |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 108 | } while (group != sd->groups); |
| 109 | printk(KERN_CONT "\n"); |
| 110 | |
| 111 | if (!cpumask_equal(sched_domain_span(sd), groupmask)) |
| 112 | printk(KERN_ERR "ERROR: groups don't span domain->span\n"); |
| 113 | |
| 114 | if (sd->parent && |
| 115 | !cpumask_subset(groupmask, sched_domain_span(sd->parent))) |
| 116 | printk(KERN_ERR "ERROR: parent span is not a superset " |
| 117 | "of domain->span\n"); |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static void sched_domain_debug(struct sched_domain *sd, int cpu) |
| 122 | { |
| 123 | int level = 0; |
| 124 | |
| 125 | if (!sched_debug_enabled) |
| 126 | return; |
| 127 | |
| 128 | if (!sd) { |
| 129 | printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); |
| 130 | return; |
| 131 | } |
| 132 | |
Peter Zijlstra | 005f874 | 2017-04-26 17:35:35 +0200 | [diff] [blame] | 133 | printk(KERN_DEBUG "CPU%d attaching sched-domain(s):\n", cpu); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 134 | |
| 135 | for (;;) { |
| 136 | if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask)) |
| 137 | break; |
| 138 | level++; |
| 139 | sd = sd->parent; |
| 140 | if (!sd) |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | #else /* !CONFIG_SCHED_DEBUG */ |
| 145 | |
| 146 | # define sched_debug_enabled 0 |
| 147 | # define sched_domain_debug(sd, cpu) do { } while (0) |
| 148 | static inline bool sched_debug(void) |
| 149 | { |
| 150 | return false; |
| 151 | } |
| 152 | #endif /* CONFIG_SCHED_DEBUG */ |
| 153 | |
| 154 | static int sd_degenerate(struct sched_domain *sd) |
| 155 | { |
| 156 | if (cpumask_weight(sched_domain_span(sd)) == 1) |
| 157 | return 1; |
| 158 | |
| 159 | /* Following flags need at least 2 groups */ |
| 160 | if (sd->flags & (SD_LOAD_BALANCE | |
| 161 | SD_BALANCE_NEWIDLE | |
| 162 | SD_BALANCE_FORK | |
| 163 | SD_BALANCE_EXEC | |
| 164 | SD_SHARE_CPUCAPACITY | |
| 165 | SD_ASYM_CPUCAPACITY | |
| 166 | SD_SHARE_PKG_RESOURCES | |
| 167 | SD_SHARE_POWERDOMAIN)) { |
| 168 | if (sd->groups != sd->groups->next) |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | /* Following flags don't use groups */ |
| 173 | if (sd->flags & (SD_WAKE_AFFINE)) |
| 174 | return 0; |
| 175 | |
| 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | static int |
| 180 | sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) |
| 181 | { |
| 182 | unsigned long cflags = sd->flags, pflags = parent->flags; |
| 183 | |
| 184 | if (sd_degenerate(parent)) |
| 185 | return 1; |
| 186 | |
| 187 | if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent))) |
| 188 | return 0; |
| 189 | |
| 190 | /* Flags needing groups don't count if only 1 group in parent */ |
| 191 | if (parent->groups == parent->groups->next) { |
| 192 | pflags &= ~(SD_LOAD_BALANCE | |
| 193 | SD_BALANCE_NEWIDLE | |
| 194 | SD_BALANCE_FORK | |
| 195 | SD_BALANCE_EXEC | |
| 196 | SD_ASYM_CPUCAPACITY | |
| 197 | SD_SHARE_CPUCAPACITY | |
| 198 | SD_SHARE_PKG_RESOURCES | |
| 199 | SD_PREFER_SIBLING | |
| 200 | SD_SHARE_POWERDOMAIN); |
| 201 | if (nr_node_ids == 1) |
| 202 | pflags &= ~SD_SERIALIZE; |
| 203 | } |
| 204 | if (~cflags & pflags) |
| 205 | return 0; |
| 206 | |
| 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | static void free_rootdomain(struct rcu_head *rcu) |
| 211 | { |
| 212 | struct root_domain *rd = container_of(rcu, struct root_domain, rcu); |
| 213 | |
| 214 | cpupri_cleanup(&rd->cpupri); |
| 215 | cpudl_cleanup(&rd->cpudl); |
| 216 | free_cpumask_var(rd->dlo_mask); |
| 217 | free_cpumask_var(rd->rto_mask); |
| 218 | free_cpumask_var(rd->online); |
| 219 | free_cpumask_var(rd->span); |
| 220 | kfree(rd); |
| 221 | } |
| 222 | |
| 223 | void rq_attach_root(struct rq *rq, struct root_domain *rd) |
| 224 | { |
| 225 | struct root_domain *old_rd = NULL; |
| 226 | unsigned long flags; |
| 227 | |
| 228 | raw_spin_lock_irqsave(&rq->lock, flags); |
| 229 | |
| 230 | if (rq->rd) { |
| 231 | old_rd = rq->rd; |
| 232 | |
| 233 | if (cpumask_test_cpu(rq->cpu, old_rd->online)) |
| 234 | set_rq_offline(rq); |
| 235 | |
| 236 | cpumask_clear_cpu(rq->cpu, old_rd->span); |
| 237 | |
| 238 | /* |
| 239 | * If we dont want to free the old_rd yet then |
| 240 | * set old_rd to NULL to skip the freeing later |
| 241 | * in this function: |
| 242 | */ |
| 243 | if (!atomic_dec_and_test(&old_rd->refcount)) |
| 244 | old_rd = NULL; |
| 245 | } |
| 246 | |
| 247 | atomic_inc(&rd->refcount); |
| 248 | rq->rd = rd; |
| 249 | |
| 250 | cpumask_set_cpu(rq->cpu, rd->span); |
| 251 | if (cpumask_test_cpu(rq->cpu, cpu_active_mask)) |
| 252 | set_rq_online(rq); |
| 253 | |
| 254 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
| 255 | |
| 256 | if (old_rd) |
| 257 | call_rcu_sched(&old_rd->rcu, free_rootdomain); |
| 258 | } |
| 259 | |
| 260 | static int init_rootdomain(struct root_domain *rd) |
| 261 | { |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 262 | if (!zalloc_cpumask_var(&rd->span, GFP_KERNEL)) |
| 263 | goto out; |
| 264 | if (!zalloc_cpumask_var(&rd->online, GFP_KERNEL)) |
| 265 | goto free_span; |
| 266 | if (!zalloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL)) |
| 267 | goto free_online; |
| 268 | if (!zalloc_cpumask_var(&rd->rto_mask, GFP_KERNEL)) |
| 269 | goto free_dlo_mask; |
| 270 | |
Steven Rostedt (Red Hat) | 4bdced5 | 2017-10-06 14:05:04 -0400 | [diff] [blame^] | 271 | #ifdef HAVE_RT_PUSH_IPI |
| 272 | rd->rto_cpu = -1; |
| 273 | raw_spin_lock_init(&rd->rto_lock); |
| 274 | init_irq_work(&rd->rto_push_work, rto_push_irq_work_func); |
| 275 | #endif |
| 276 | |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 277 | init_dl_bw(&rd->dl_bw); |
| 278 | if (cpudl_init(&rd->cpudl) != 0) |
| 279 | goto free_rto_mask; |
| 280 | |
| 281 | if (cpupri_init(&rd->cpupri) != 0) |
| 282 | goto free_cpudl; |
| 283 | return 0; |
| 284 | |
| 285 | free_cpudl: |
| 286 | cpudl_cleanup(&rd->cpudl); |
| 287 | free_rto_mask: |
| 288 | free_cpumask_var(rd->rto_mask); |
| 289 | free_dlo_mask: |
| 290 | free_cpumask_var(rd->dlo_mask); |
| 291 | free_online: |
| 292 | free_cpumask_var(rd->online); |
| 293 | free_span: |
| 294 | free_cpumask_var(rd->span); |
| 295 | out: |
| 296 | return -ENOMEM; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * By default the system creates a single root-domain with all CPUs as |
| 301 | * members (mimicking the global state we have today). |
| 302 | */ |
| 303 | struct root_domain def_root_domain; |
| 304 | |
| 305 | void init_defrootdomain(void) |
| 306 | { |
| 307 | init_rootdomain(&def_root_domain); |
| 308 | |
| 309 | atomic_set(&def_root_domain.refcount, 1); |
| 310 | } |
| 311 | |
| 312 | static struct root_domain *alloc_rootdomain(void) |
| 313 | { |
| 314 | struct root_domain *rd; |
| 315 | |
Viresh Kumar | 4d13a06 | 2017-04-13 14:45:48 +0530 | [diff] [blame] | 316 | rd = kzalloc(sizeof(*rd), GFP_KERNEL); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 317 | if (!rd) |
| 318 | return NULL; |
| 319 | |
| 320 | if (init_rootdomain(rd) != 0) { |
| 321 | kfree(rd); |
| 322 | return NULL; |
| 323 | } |
| 324 | |
| 325 | return rd; |
| 326 | } |
| 327 | |
| 328 | static void free_sched_groups(struct sched_group *sg, int free_sgc) |
| 329 | { |
| 330 | struct sched_group *tmp, *first; |
| 331 | |
| 332 | if (!sg) |
| 333 | return; |
| 334 | |
| 335 | first = sg; |
| 336 | do { |
| 337 | tmp = sg->next; |
| 338 | |
| 339 | if (free_sgc && atomic_dec_and_test(&sg->sgc->ref)) |
| 340 | kfree(sg->sgc); |
| 341 | |
Shu Wang | 213c5a4 | 2017-08-10 15:52:16 +0800 | [diff] [blame] | 342 | if (atomic_dec_and_test(&sg->ref)) |
| 343 | kfree(sg); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 344 | sg = tmp; |
| 345 | } while (sg != first); |
| 346 | } |
| 347 | |
| 348 | static void destroy_sched_domain(struct sched_domain *sd) |
| 349 | { |
| 350 | /* |
Peter Zijlstra | a090c4f | 2017-08-21 15:42:52 +0200 | [diff] [blame] | 351 | * A normal sched domain may have multiple group references, an |
| 352 | * overlapping domain, having private groups, only one. Iterate, |
| 353 | * dropping group/capacity references, freeing where none remain. |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 354 | */ |
Shu Wang | 213c5a4 | 2017-08-10 15:52:16 +0800 | [diff] [blame] | 355 | free_sched_groups(sd->groups, 1); |
| 356 | |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 357 | if (sd->shared && atomic_dec_and_test(&sd->shared->ref)) |
| 358 | kfree(sd->shared); |
| 359 | kfree(sd); |
| 360 | } |
| 361 | |
| 362 | static void destroy_sched_domains_rcu(struct rcu_head *rcu) |
| 363 | { |
| 364 | struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu); |
| 365 | |
| 366 | while (sd) { |
| 367 | struct sched_domain *parent = sd->parent; |
| 368 | destroy_sched_domain(sd); |
| 369 | sd = parent; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | static void destroy_sched_domains(struct sched_domain *sd) |
| 374 | { |
| 375 | if (sd) |
| 376 | call_rcu(&sd->rcu, destroy_sched_domains_rcu); |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * Keep a special pointer to the highest sched_domain that has |
| 381 | * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this |
| 382 | * allows us to avoid some pointer chasing select_idle_sibling(). |
| 383 | * |
| 384 | * Also keep a unique ID per domain (we use the first CPU number in |
| 385 | * the cpumask of the domain), this allows us to quickly tell if |
| 386 | * two CPUs are in the same cache domain, see cpus_share_cache(). |
| 387 | */ |
| 388 | DEFINE_PER_CPU(struct sched_domain *, sd_llc); |
| 389 | DEFINE_PER_CPU(int, sd_llc_size); |
| 390 | DEFINE_PER_CPU(int, sd_llc_id); |
| 391 | DEFINE_PER_CPU(struct sched_domain_shared *, sd_llc_shared); |
| 392 | DEFINE_PER_CPU(struct sched_domain *, sd_numa); |
| 393 | DEFINE_PER_CPU(struct sched_domain *, sd_asym); |
| 394 | |
| 395 | static void update_top_cache_domain(int cpu) |
| 396 | { |
| 397 | struct sched_domain_shared *sds = NULL; |
| 398 | struct sched_domain *sd; |
| 399 | int id = cpu; |
| 400 | int size = 1; |
| 401 | |
| 402 | sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES); |
| 403 | if (sd) { |
| 404 | id = cpumask_first(sched_domain_span(sd)); |
| 405 | size = cpumask_weight(sched_domain_span(sd)); |
| 406 | sds = sd->shared; |
| 407 | } |
| 408 | |
| 409 | rcu_assign_pointer(per_cpu(sd_llc, cpu), sd); |
| 410 | per_cpu(sd_llc_size, cpu) = size; |
| 411 | per_cpu(sd_llc_id, cpu) = id; |
| 412 | rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds); |
| 413 | |
| 414 | sd = lowest_flag_domain(cpu, SD_NUMA); |
| 415 | rcu_assign_pointer(per_cpu(sd_numa, cpu), sd); |
| 416 | |
| 417 | sd = highest_flag_domain(cpu, SD_ASYM_PACKING); |
| 418 | rcu_assign_pointer(per_cpu(sd_asym, cpu), sd); |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Attach the domain 'sd' to 'cpu' as its base domain. Callers must |
| 423 | * hold the hotplug lock. |
| 424 | */ |
| 425 | static void |
| 426 | cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu) |
| 427 | { |
| 428 | struct rq *rq = cpu_rq(cpu); |
| 429 | struct sched_domain *tmp; |
| 430 | |
| 431 | /* Remove the sched domains which do not contribute to scheduling. */ |
| 432 | for (tmp = sd; tmp; ) { |
| 433 | struct sched_domain *parent = tmp->parent; |
| 434 | if (!parent) |
| 435 | break; |
| 436 | |
| 437 | if (sd_parent_degenerate(tmp, parent)) { |
| 438 | tmp->parent = parent->parent; |
| 439 | if (parent->parent) |
| 440 | parent->parent->child = tmp; |
| 441 | /* |
| 442 | * Transfer SD_PREFER_SIBLING down in case of a |
| 443 | * degenerate parent; the spans match for this |
| 444 | * so the property transfers. |
| 445 | */ |
| 446 | if (parent->flags & SD_PREFER_SIBLING) |
| 447 | tmp->flags |= SD_PREFER_SIBLING; |
| 448 | destroy_sched_domain(parent); |
| 449 | } else |
| 450 | tmp = tmp->parent; |
| 451 | } |
| 452 | |
| 453 | if (sd && sd_degenerate(sd)) { |
| 454 | tmp = sd; |
| 455 | sd = sd->parent; |
| 456 | destroy_sched_domain(tmp); |
| 457 | if (sd) |
| 458 | sd->child = NULL; |
| 459 | } |
| 460 | |
| 461 | sched_domain_debug(sd, cpu); |
| 462 | |
| 463 | rq_attach_root(rq, rd); |
| 464 | tmp = rq->sd; |
| 465 | rcu_assign_pointer(rq->sd, sd); |
Peter Zijlstra | bbdacdf | 2017-08-10 17:10:26 +0200 | [diff] [blame] | 466 | dirty_sched_domain_sysctl(cpu); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 467 | destroy_sched_domains(tmp); |
| 468 | |
| 469 | update_top_cache_domain(cpu); |
| 470 | } |
| 471 | |
| 472 | /* Setup the mask of CPUs configured for isolated domains */ |
| 473 | static int __init isolated_cpu_setup(char *str) |
| 474 | { |
| 475 | int ret; |
| 476 | |
| 477 | alloc_bootmem_cpumask_var(&cpu_isolated_map); |
| 478 | ret = cpulist_parse(str, cpu_isolated_map); |
| 479 | if (ret) { |
Alexey Dobriyan | 9b130ad | 2017-09-08 16:14:18 -0700 | [diff] [blame] | 480 | pr_err("sched: Error, all isolcpus= values must be between 0 and %u\n", nr_cpu_ids); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 481 | return 0; |
| 482 | } |
| 483 | return 1; |
| 484 | } |
| 485 | __setup("isolcpus=", isolated_cpu_setup); |
| 486 | |
| 487 | struct s_data { |
| 488 | struct sched_domain ** __percpu sd; |
| 489 | struct root_domain *rd; |
| 490 | }; |
| 491 | |
| 492 | enum s_alloc { |
| 493 | sa_rootdomain, |
| 494 | sa_sd, |
| 495 | sa_sd_storage, |
| 496 | sa_none, |
| 497 | }; |
| 498 | |
| 499 | /* |
Peter Zijlstra | 35a566e | 2017-04-28 10:54:26 +0200 | [diff] [blame] | 500 | * Return the canonical balance CPU for this group, this is the first CPU |
Peter Zijlstra | e5c14b1 | 2017-05-01 10:47:02 +0200 | [diff] [blame] | 501 | * of this group that's also in the balance mask. |
Peter Zijlstra | 35a566e | 2017-04-28 10:54:26 +0200 | [diff] [blame] | 502 | * |
Peter Zijlstra | e5c14b1 | 2017-05-01 10:47:02 +0200 | [diff] [blame] | 503 | * The balance mask are all those CPUs that could actually end up at this |
| 504 | * group. See build_balance_mask(). |
Peter Zijlstra | 35a566e | 2017-04-28 10:54:26 +0200 | [diff] [blame] | 505 | * |
| 506 | * Also see should_we_balance(). |
| 507 | */ |
| 508 | int group_balance_cpu(struct sched_group *sg) |
| 509 | { |
Peter Zijlstra | e5c14b1 | 2017-05-01 10:47:02 +0200 | [diff] [blame] | 510 | return cpumask_first(group_balance_mask(sg)); |
Peter Zijlstra | 35a566e | 2017-04-28 10:54:26 +0200 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | |
| 514 | /* |
| 515 | * NUMA topology (first read the regular topology blurb below) |
| 516 | * |
| 517 | * Given a node-distance table, for example: |
| 518 | * |
| 519 | * node 0 1 2 3 |
| 520 | * 0: 10 20 30 20 |
| 521 | * 1: 20 10 20 30 |
| 522 | * 2: 30 20 10 20 |
| 523 | * 3: 20 30 20 10 |
| 524 | * |
| 525 | * which represents a 4 node ring topology like: |
| 526 | * |
| 527 | * 0 ----- 1 |
| 528 | * | | |
| 529 | * | | |
| 530 | * | | |
| 531 | * 3 ----- 2 |
| 532 | * |
| 533 | * We want to construct domains and groups to represent this. The way we go |
| 534 | * about doing this is to build the domains on 'hops'. For each NUMA level we |
| 535 | * construct the mask of all nodes reachable in @level hops. |
| 536 | * |
| 537 | * For the above NUMA topology that gives 3 levels: |
| 538 | * |
| 539 | * NUMA-2 0-3 0-3 0-3 0-3 |
| 540 | * groups: {0-1,3},{1-3} {0-2},{0,2-3} {1-3},{0-1,3} {0,2-3},{0-2} |
| 541 | * |
| 542 | * NUMA-1 0-1,3 0-2 1-3 0,2-3 |
| 543 | * groups: {0},{1},{3} {0},{1},{2} {1},{2},{3} {0},{2},{3} |
| 544 | * |
| 545 | * NUMA-0 0 1 2 3 |
| 546 | * |
| 547 | * |
| 548 | * As can be seen; things don't nicely line up as with the regular topology. |
| 549 | * When we iterate a domain in child domain chunks some nodes can be |
| 550 | * represented multiple times -- hence the "overlap" naming for this part of |
| 551 | * the topology. |
| 552 | * |
| 553 | * In order to minimize this overlap, we only build enough groups to cover the |
| 554 | * domain. For instance Node-0 NUMA-2 would only get groups: 0-1,3 and 1-3. |
| 555 | * |
| 556 | * Because: |
| 557 | * |
| 558 | * - the first group of each domain is its child domain; this |
| 559 | * gets us the first 0-1,3 |
| 560 | * - the only uncovered node is 2, who's child domain is 1-3. |
| 561 | * |
| 562 | * However, because of the overlap, computing a unique CPU for each group is |
| 563 | * more complicated. Consider for instance the groups of NODE-1 NUMA-2, both |
| 564 | * groups include the CPUs of Node-0, while those CPUs would not in fact ever |
| 565 | * end up at those groups (they would end up in group: 0-1,3). |
| 566 | * |
Peter Zijlstra | e5c14b1 | 2017-05-01 10:47:02 +0200 | [diff] [blame] | 567 | * To correct this we have to introduce the group balance mask. This mask |
Peter Zijlstra | 35a566e | 2017-04-28 10:54:26 +0200 | [diff] [blame] | 568 | * will contain those CPUs in the group that can reach this group given the |
| 569 | * (child) domain tree. |
| 570 | * |
| 571 | * With this we can once again compute balance_cpu and sched_group_capacity |
| 572 | * relations. |
| 573 | * |
| 574 | * XXX include words on how balance_cpu is unique and therefore can be |
| 575 | * used for sched_group_capacity links. |
| 576 | * |
| 577 | * |
| 578 | * Another 'interesting' topology is: |
| 579 | * |
| 580 | * node 0 1 2 3 |
| 581 | * 0: 10 20 20 30 |
| 582 | * 1: 20 10 20 20 |
| 583 | * 2: 20 20 10 20 |
| 584 | * 3: 30 20 20 10 |
| 585 | * |
| 586 | * Which looks a little like: |
| 587 | * |
| 588 | * 0 ----- 1 |
| 589 | * | / | |
| 590 | * | / | |
| 591 | * | / | |
| 592 | * 2 ----- 3 |
| 593 | * |
| 594 | * This topology is asymmetric, nodes 1,2 are fully connected, but nodes 0,3 |
| 595 | * are not. |
| 596 | * |
| 597 | * This leads to a few particularly weird cases where the sched_domain's are |
| 598 | * not of the same number for each cpu. Consider: |
| 599 | * |
| 600 | * NUMA-2 0-3 0-3 |
| 601 | * groups: {0-2},{1-3} {1-3},{0-2} |
| 602 | * |
| 603 | * NUMA-1 0-2 0-3 0-3 1-3 |
| 604 | * |
| 605 | * NUMA-0 0 1 2 3 |
| 606 | * |
| 607 | */ |
| 608 | |
| 609 | |
| 610 | /* |
Peter Zijlstra | e5c14b1 | 2017-05-01 10:47:02 +0200 | [diff] [blame] | 611 | * Build the balance mask; it contains only those CPUs that can arrive at this |
| 612 | * group and should be considered to continue balancing. |
Peter Zijlstra | 35a566e | 2017-04-28 10:54:26 +0200 | [diff] [blame] | 613 | * |
| 614 | * We do this during the group creation pass, therefore the group information |
| 615 | * isn't complete yet, however since each group represents a (child) domain we |
| 616 | * can fully construct this using the sched_domain bits (which are already |
| 617 | * complete). |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 618 | */ |
Peter Zijlstra | 1676330 | 2017-04-25 14:31:11 +0200 | [diff] [blame] | 619 | static void |
Peter Zijlstra | e5c14b1 | 2017-05-01 10:47:02 +0200 | [diff] [blame] | 620 | build_balance_mask(struct sched_domain *sd, struct sched_group *sg, struct cpumask *mask) |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 621 | { |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 622 | const struct cpumask *sg_span = sched_group_span(sg); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 623 | struct sd_data *sdd = sd->private; |
| 624 | struct sched_domain *sibling; |
| 625 | int i; |
| 626 | |
Peter Zijlstra | 1676330 | 2017-04-25 14:31:11 +0200 | [diff] [blame] | 627 | cpumask_clear(mask); |
| 628 | |
Lauro Ramos Venancio | f32d782 | 2017-04-20 16:51:40 -0300 | [diff] [blame] | 629 | for_each_cpu(i, sg_span) { |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 630 | sibling = *per_cpu_ptr(sdd->sd, i); |
Peter Zijlstra | 73bb059 | 2017-04-25 14:00:49 +0200 | [diff] [blame] | 631 | |
| 632 | /* |
| 633 | * Can happen in the asymmetric case, where these siblings are |
| 634 | * unused. The mask will not be empty because those CPUs that |
| 635 | * do have the top domain _should_ span the domain. |
| 636 | */ |
| 637 | if (!sibling->child) |
| 638 | continue; |
| 639 | |
| 640 | /* If we would not end up here, we can't continue from here */ |
| 641 | if (!cpumask_equal(sg_span, sched_domain_span(sibling->child))) |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 642 | continue; |
| 643 | |
Peter Zijlstra | 1676330 | 2017-04-25 14:31:11 +0200 | [diff] [blame] | 644 | cpumask_set_cpu(i, mask); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 645 | } |
Peter Zijlstra | 73bb059 | 2017-04-25 14:00:49 +0200 | [diff] [blame] | 646 | |
| 647 | /* We must not have empty masks here */ |
Peter Zijlstra | 1676330 | 2017-04-25 14:31:11 +0200 | [diff] [blame] | 648 | WARN_ON_ONCE(cpumask_empty(mask)); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /* |
Peter Zijlstra | 35a566e | 2017-04-28 10:54:26 +0200 | [diff] [blame] | 652 | * XXX: This creates per-node group entries; since the load-balancer will |
| 653 | * immediately access remote memory to construct this group's load-balance |
| 654 | * statistics having the groups node local is of dubious benefit. |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 655 | */ |
Lauro Ramos Venancio | 8c03346 | 2017-04-13 10:56:07 -0300 | [diff] [blame] | 656 | static struct sched_group * |
| 657 | build_group_from_child_sched_domain(struct sched_domain *sd, int cpu) |
| 658 | { |
| 659 | struct sched_group *sg; |
| 660 | struct cpumask *sg_span; |
| 661 | |
| 662 | sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(), |
| 663 | GFP_KERNEL, cpu_to_node(cpu)); |
| 664 | |
| 665 | if (!sg) |
| 666 | return NULL; |
| 667 | |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 668 | sg_span = sched_group_span(sg); |
Lauro Ramos Venancio | 8c03346 | 2017-04-13 10:56:07 -0300 | [diff] [blame] | 669 | if (sd->child) |
| 670 | cpumask_copy(sg_span, sched_domain_span(sd->child)); |
| 671 | else |
| 672 | cpumask_copy(sg_span, sched_domain_span(sd)); |
| 673 | |
Shu Wang | 213c5a4 | 2017-08-10 15:52:16 +0800 | [diff] [blame] | 674 | atomic_inc(&sg->ref); |
Lauro Ramos Venancio | 8c03346 | 2017-04-13 10:56:07 -0300 | [diff] [blame] | 675 | return sg; |
| 676 | } |
| 677 | |
| 678 | static void init_overlap_sched_group(struct sched_domain *sd, |
Peter Zijlstra | 1676330 | 2017-04-25 14:31:11 +0200 | [diff] [blame] | 679 | struct sched_group *sg) |
Lauro Ramos Venancio | 8c03346 | 2017-04-13 10:56:07 -0300 | [diff] [blame] | 680 | { |
Peter Zijlstra | 1676330 | 2017-04-25 14:31:11 +0200 | [diff] [blame] | 681 | struct cpumask *mask = sched_domains_tmpmask2; |
Lauro Ramos Venancio | 8c03346 | 2017-04-13 10:56:07 -0300 | [diff] [blame] | 682 | struct sd_data *sdd = sd->private; |
| 683 | struct cpumask *sg_span; |
Peter Zijlstra | 1676330 | 2017-04-25 14:31:11 +0200 | [diff] [blame] | 684 | int cpu; |
| 685 | |
Peter Zijlstra | e5c14b1 | 2017-05-01 10:47:02 +0200 | [diff] [blame] | 686 | build_balance_mask(sd, sg, mask); |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 687 | cpu = cpumask_first_and(sched_group_span(sg), mask); |
Lauro Ramos Venancio | 8c03346 | 2017-04-13 10:56:07 -0300 | [diff] [blame] | 688 | |
| 689 | sg->sgc = *per_cpu_ptr(sdd->sgc, cpu); |
| 690 | if (atomic_inc_return(&sg->sgc->ref) == 1) |
Peter Zijlstra | e5c14b1 | 2017-05-01 10:47:02 +0200 | [diff] [blame] | 691 | cpumask_copy(group_balance_mask(sg), mask); |
Peter Zijlstra | 35a566e | 2017-04-28 10:54:26 +0200 | [diff] [blame] | 692 | else |
Peter Zijlstra | e5c14b1 | 2017-05-01 10:47:02 +0200 | [diff] [blame] | 693 | WARN_ON_ONCE(!cpumask_equal(group_balance_mask(sg), mask)); |
Lauro Ramos Venancio | 8c03346 | 2017-04-13 10:56:07 -0300 | [diff] [blame] | 694 | |
| 695 | /* |
| 696 | * Initialize sgc->capacity such that even if we mess up the |
| 697 | * domains and no possible iteration will get us here, we won't |
| 698 | * die on a /0 trap. |
| 699 | */ |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 700 | sg_span = sched_group_span(sg); |
Lauro Ramos Venancio | 8c03346 | 2017-04-13 10:56:07 -0300 | [diff] [blame] | 701 | sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span); |
| 702 | sg->sgc->min_capacity = SCHED_CAPACITY_SCALE; |
| 703 | } |
| 704 | |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 705 | static int |
| 706 | build_overlap_sched_groups(struct sched_domain *sd, int cpu) |
| 707 | { |
Peter Zijlstra | 91eaed0 | 2017-04-14 17:32:07 +0200 | [diff] [blame] | 708 | struct sched_group *first = NULL, *last = NULL, *sg; |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 709 | const struct cpumask *span = sched_domain_span(sd); |
| 710 | struct cpumask *covered = sched_domains_tmpmask; |
| 711 | struct sd_data *sdd = sd->private; |
| 712 | struct sched_domain *sibling; |
| 713 | int i; |
| 714 | |
| 715 | cpumask_clear(covered); |
| 716 | |
Peter Zijlstra | 0372dd2 | 2017-04-14 17:24:02 +0200 | [diff] [blame] | 717 | for_each_cpu_wrap(i, span, cpu) { |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 718 | struct cpumask *sg_span; |
| 719 | |
| 720 | if (cpumask_test_cpu(i, covered)) |
| 721 | continue; |
| 722 | |
| 723 | sibling = *per_cpu_ptr(sdd->sd, i); |
| 724 | |
Lauro Ramos Venancio | c20e1ea | 2017-04-20 16:51:42 -0300 | [diff] [blame] | 725 | /* |
| 726 | * Asymmetric node setups can result in situations where the |
| 727 | * domain tree is of unequal depth, make sure to skip domains |
| 728 | * that already cover the entire range. |
| 729 | * |
| 730 | * In that case build_sched_domains() will have terminated the |
| 731 | * iteration early and our sibling sd spans will be empty. |
| 732 | * Domains should always include the CPU they're built on, so |
| 733 | * check that. |
| 734 | */ |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 735 | if (!cpumask_test_cpu(i, sched_domain_span(sibling))) |
| 736 | continue; |
| 737 | |
Lauro Ramos Venancio | 8c03346 | 2017-04-13 10:56:07 -0300 | [diff] [blame] | 738 | sg = build_group_from_child_sched_domain(sibling, cpu); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 739 | if (!sg) |
| 740 | goto fail; |
| 741 | |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 742 | sg_span = sched_group_span(sg); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 743 | cpumask_or(covered, covered, sg_span); |
| 744 | |
Peter Zijlstra | 1676330 | 2017-04-25 14:31:11 +0200 | [diff] [blame] | 745 | init_overlap_sched_group(sd, sg); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 746 | |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 747 | if (!first) |
| 748 | first = sg; |
| 749 | if (last) |
| 750 | last->next = sg; |
| 751 | last = sg; |
| 752 | last->next = first; |
| 753 | } |
Peter Zijlstra | 91eaed0 | 2017-04-14 17:32:07 +0200 | [diff] [blame] | 754 | sd->groups = first; |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 755 | |
| 756 | return 0; |
| 757 | |
| 758 | fail: |
| 759 | free_sched_groups(first, 0); |
| 760 | |
| 761 | return -ENOMEM; |
| 762 | } |
| 763 | |
Peter Zijlstra | 35a566e | 2017-04-28 10:54:26 +0200 | [diff] [blame] | 764 | |
| 765 | /* |
| 766 | * Package topology (also see the load-balance blurb in fair.c) |
| 767 | * |
| 768 | * The scheduler builds a tree structure to represent a number of important |
| 769 | * topology features. By default (default_topology[]) these include: |
| 770 | * |
| 771 | * - Simultaneous multithreading (SMT) |
| 772 | * - Multi-Core Cache (MC) |
| 773 | * - Package (DIE) |
| 774 | * |
| 775 | * Where the last one more or less denotes everything up to a NUMA node. |
| 776 | * |
| 777 | * The tree consists of 3 primary data structures: |
| 778 | * |
| 779 | * sched_domain -> sched_group -> sched_group_capacity |
| 780 | * ^ ^ ^ ^ |
| 781 | * `-' `-' |
| 782 | * |
| 783 | * The sched_domains are per-cpu and have a two way link (parent & child) and |
| 784 | * denote the ever growing mask of CPUs belonging to that level of topology. |
| 785 | * |
| 786 | * Each sched_domain has a circular (double) linked list of sched_group's, each |
| 787 | * denoting the domains of the level below (or individual CPUs in case of the |
| 788 | * first domain level). The sched_group linked by a sched_domain includes the |
| 789 | * CPU of that sched_domain [*]. |
| 790 | * |
| 791 | * Take for instance a 2 threaded, 2 core, 2 cache cluster part: |
| 792 | * |
| 793 | * CPU 0 1 2 3 4 5 6 7 |
| 794 | * |
| 795 | * DIE [ ] |
| 796 | * MC [ ] [ ] |
| 797 | * SMT [ ] [ ] [ ] [ ] |
| 798 | * |
| 799 | * - or - |
| 800 | * |
| 801 | * DIE 0-7 0-7 0-7 0-7 0-7 0-7 0-7 0-7 |
| 802 | * MC 0-3 0-3 0-3 0-3 4-7 4-7 4-7 4-7 |
| 803 | * SMT 0-1 0-1 2-3 2-3 4-5 4-5 6-7 6-7 |
| 804 | * |
| 805 | * CPU 0 1 2 3 4 5 6 7 |
| 806 | * |
| 807 | * One way to think about it is: sched_domain moves you up and down among these |
| 808 | * topology levels, while sched_group moves you sideways through it, at child |
| 809 | * domain granularity. |
| 810 | * |
| 811 | * sched_group_capacity ensures each unique sched_group has shared storage. |
| 812 | * |
| 813 | * There are two related construction problems, both require a CPU that |
| 814 | * uniquely identify each group (for a given domain): |
| 815 | * |
| 816 | * - The first is the balance_cpu (see should_we_balance() and the |
| 817 | * load-balance blub in fair.c); for each group we only want 1 CPU to |
| 818 | * continue balancing at a higher domain. |
| 819 | * |
| 820 | * - The second is the sched_group_capacity; we want all identical groups |
| 821 | * to share a single sched_group_capacity. |
| 822 | * |
| 823 | * Since these topologies are exclusive by construction. That is, its |
| 824 | * impossible for an SMT thread to belong to multiple cores, and cores to |
| 825 | * be part of multiple caches. There is a very clear and unique location |
| 826 | * for each CPU in the hierarchy. |
| 827 | * |
| 828 | * Therefore computing a unique CPU for each group is trivial (the iteration |
| 829 | * mask is redundant and set all 1s; all CPUs in a group will end up at _that_ |
| 830 | * group), we can simply pick the first CPU in each group. |
| 831 | * |
| 832 | * |
| 833 | * [*] in other words, the first group of each domain is its child domain. |
| 834 | */ |
| 835 | |
Peter Zijlstra | 0c0e776 | 2017-05-03 14:18:06 +0200 | [diff] [blame] | 836 | static struct sched_group *get_group(int cpu, struct sd_data *sdd) |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 837 | { |
| 838 | struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu); |
| 839 | struct sched_domain *child = sd->child; |
Peter Zijlstra | 0c0e776 | 2017-05-03 14:18:06 +0200 | [diff] [blame] | 840 | struct sched_group *sg; |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 841 | |
| 842 | if (child) |
| 843 | cpu = cpumask_first(sched_domain_span(child)); |
| 844 | |
Peter Zijlstra | 0c0e776 | 2017-05-03 14:18:06 +0200 | [diff] [blame] | 845 | sg = *per_cpu_ptr(sdd->sg, cpu); |
| 846 | sg->sgc = *per_cpu_ptr(sdd->sgc, cpu); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 847 | |
Peter Zijlstra | 0c0e776 | 2017-05-03 14:18:06 +0200 | [diff] [blame] | 848 | /* For claim_allocations: */ |
| 849 | atomic_inc(&sg->ref); |
| 850 | atomic_inc(&sg->sgc->ref); |
| 851 | |
| 852 | if (child) { |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 853 | cpumask_copy(sched_group_span(sg), sched_domain_span(child)); |
| 854 | cpumask_copy(group_balance_mask(sg), sched_group_span(sg)); |
Peter Zijlstra | 0c0e776 | 2017-05-03 14:18:06 +0200 | [diff] [blame] | 855 | } else { |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 856 | cpumask_set_cpu(cpu, sched_group_span(sg)); |
Peter Zijlstra | e5c14b1 | 2017-05-01 10:47:02 +0200 | [diff] [blame] | 857 | cpumask_set_cpu(cpu, group_balance_mask(sg)); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 858 | } |
| 859 | |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 860 | sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sched_group_span(sg)); |
Peter Zijlstra | 0c0e776 | 2017-05-03 14:18:06 +0200 | [diff] [blame] | 861 | sg->sgc->min_capacity = SCHED_CAPACITY_SCALE; |
| 862 | |
| 863 | return sg; |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /* |
| 867 | * build_sched_groups will build a circular linked list of the groups |
| 868 | * covered by the given span, and will set each group's ->cpumask correctly, |
| 869 | * and ->cpu_capacity to 0. |
| 870 | * |
| 871 | * Assumes the sched_domain tree is fully constructed |
| 872 | */ |
| 873 | static int |
| 874 | build_sched_groups(struct sched_domain *sd, int cpu) |
| 875 | { |
| 876 | struct sched_group *first = NULL, *last = NULL; |
| 877 | struct sd_data *sdd = sd->private; |
| 878 | const struct cpumask *span = sched_domain_span(sd); |
| 879 | struct cpumask *covered; |
| 880 | int i; |
| 881 | |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 882 | lockdep_assert_held(&sched_domains_mutex); |
| 883 | covered = sched_domains_tmpmask; |
| 884 | |
| 885 | cpumask_clear(covered); |
| 886 | |
Peter Zijlstra | 0c0e776 | 2017-05-03 14:18:06 +0200 | [diff] [blame] | 887 | for_each_cpu_wrap(i, span, cpu) { |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 888 | struct sched_group *sg; |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 889 | |
| 890 | if (cpumask_test_cpu(i, covered)) |
| 891 | continue; |
| 892 | |
Peter Zijlstra | 0c0e776 | 2017-05-03 14:18:06 +0200 | [diff] [blame] | 893 | sg = get_group(i, sdd); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 894 | |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 895 | cpumask_or(covered, covered, sched_group_span(sg)); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 896 | |
| 897 | if (!first) |
| 898 | first = sg; |
| 899 | if (last) |
| 900 | last->next = sg; |
| 901 | last = sg; |
| 902 | } |
| 903 | last->next = first; |
Peter Zijlstra | 0c0e776 | 2017-05-03 14:18:06 +0200 | [diff] [blame] | 904 | sd->groups = first; |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 905 | |
| 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | /* |
| 910 | * Initialize sched groups cpu_capacity. |
| 911 | * |
| 912 | * cpu_capacity indicates the capacity of sched group, which is used while |
| 913 | * distributing the load between different sched groups in a sched domain. |
| 914 | * Typically cpu_capacity for all the groups in a sched domain will be same |
| 915 | * unless there are asymmetries in the topology. If there are asymmetries, |
| 916 | * group having more cpu_capacity will pickup more load compared to the |
| 917 | * group having less cpu_capacity. |
| 918 | */ |
| 919 | static void init_sched_groups_capacity(int cpu, struct sched_domain *sd) |
| 920 | { |
| 921 | struct sched_group *sg = sd->groups; |
| 922 | |
| 923 | WARN_ON(!sg); |
| 924 | |
| 925 | do { |
| 926 | int cpu, max_cpu = -1; |
| 927 | |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 928 | sg->group_weight = cpumask_weight(sched_group_span(sg)); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 929 | |
| 930 | if (!(sd->flags & SD_ASYM_PACKING)) |
| 931 | goto next; |
| 932 | |
Peter Zijlstra | ae4df9d | 2017-05-01 11:03:12 +0200 | [diff] [blame] | 933 | for_each_cpu(cpu, sched_group_span(sg)) { |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 934 | if (max_cpu < 0) |
| 935 | max_cpu = cpu; |
| 936 | else if (sched_asym_prefer(cpu, max_cpu)) |
| 937 | max_cpu = cpu; |
| 938 | } |
| 939 | sg->asym_prefer_cpu = max_cpu; |
| 940 | |
| 941 | next: |
| 942 | sg = sg->next; |
| 943 | } while (sg != sd->groups); |
| 944 | |
| 945 | if (cpu != group_balance_cpu(sg)) |
| 946 | return; |
| 947 | |
| 948 | update_group_capacity(sd, cpu); |
| 949 | } |
| 950 | |
| 951 | /* |
| 952 | * Initializers for schedule domains |
| 953 | * Non-inlined to reduce accumulated stack pressure in build_sched_domains() |
| 954 | */ |
| 955 | |
| 956 | static int default_relax_domain_level = -1; |
| 957 | int sched_domain_level_max; |
| 958 | |
| 959 | static int __init setup_relax_domain_level(char *str) |
| 960 | { |
| 961 | if (kstrtoint(str, 0, &default_relax_domain_level)) |
| 962 | pr_warn("Unable to set relax_domain_level\n"); |
| 963 | |
| 964 | return 1; |
| 965 | } |
| 966 | __setup("relax_domain_level=", setup_relax_domain_level); |
| 967 | |
| 968 | static void set_domain_attribute(struct sched_domain *sd, |
| 969 | struct sched_domain_attr *attr) |
| 970 | { |
| 971 | int request; |
| 972 | |
| 973 | if (!attr || attr->relax_domain_level < 0) { |
| 974 | if (default_relax_domain_level < 0) |
| 975 | return; |
| 976 | else |
| 977 | request = default_relax_domain_level; |
| 978 | } else |
| 979 | request = attr->relax_domain_level; |
| 980 | if (request < sd->level) { |
| 981 | /* Turn off idle balance on this domain: */ |
| 982 | sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE); |
| 983 | } else { |
| 984 | /* Turn on idle balance on this domain: */ |
| 985 | sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE); |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | static void __sdt_free(const struct cpumask *cpu_map); |
| 990 | static int __sdt_alloc(const struct cpumask *cpu_map); |
| 991 | |
| 992 | static void __free_domain_allocs(struct s_data *d, enum s_alloc what, |
| 993 | const struct cpumask *cpu_map) |
| 994 | { |
| 995 | switch (what) { |
| 996 | case sa_rootdomain: |
| 997 | if (!atomic_read(&d->rd->refcount)) |
| 998 | free_rootdomain(&d->rd->rcu); |
| 999 | /* Fall through */ |
| 1000 | case sa_sd: |
| 1001 | free_percpu(d->sd); |
| 1002 | /* Fall through */ |
| 1003 | case sa_sd_storage: |
| 1004 | __sdt_free(cpu_map); |
| 1005 | /* Fall through */ |
| 1006 | case sa_none: |
| 1007 | break; |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | static enum s_alloc |
| 1012 | __visit_domain_allocation_hell(struct s_data *d, const struct cpumask *cpu_map) |
| 1013 | { |
| 1014 | memset(d, 0, sizeof(*d)); |
| 1015 | |
| 1016 | if (__sdt_alloc(cpu_map)) |
| 1017 | return sa_sd_storage; |
| 1018 | d->sd = alloc_percpu(struct sched_domain *); |
| 1019 | if (!d->sd) |
| 1020 | return sa_sd_storage; |
| 1021 | d->rd = alloc_rootdomain(); |
| 1022 | if (!d->rd) |
| 1023 | return sa_sd; |
| 1024 | return sa_rootdomain; |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * NULL the sd_data elements we've used to build the sched_domain and |
| 1029 | * sched_group structure so that the subsequent __free_domain_allocs() |
| 1030 | * will not free the data we're using. |
| 1031 | */ |
| 1032 | static void claim_allocations(int cpu, struct sched_domain *sd) |
| 1033 | { |
| 1034 | struct sd_data *sdd = sd->private; |
| 1035 | |
| 1036 | WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd); |
| 1037 | *per_cpu_ptr(sdd->sd, cpu) = NULL; |
| 1038 | |
| 1039 | if (atomic_read(&(*per_cpu_ptr(sdd->sds, cpu))->ref)) |
| 1040 | *per_cpu_ptr(sdd->sds, cpu) = NULL; |
| 1041 | |
| 1042 | if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref)) |
| 1043 | *per_cpu_ptr(sdd->sg, cpu) = NULL; |
| 1044 | |
| 1045 | if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref)) |
| 1046 | *per_cpu_ptr(sdd->sgc, cpu) = NULL; |
| 1047 | } |
| 1048 | |
| 1049 | #ifdef CONFIG_NUMA |
| 1050 | static int sched_domains_numa_levels; |
| 1051 | enum numa_topology_type sched_numa_topology_type; |
| 1052 | static int *sched_domains_numa_distance; |
| 1053 | int sched_max_numa_distance; |
| 1054 | static struct cpumask ***sched_domains_numa_masks; |
| 1055 | static int sched_domains_curr_level; |
| 1056 | #endif |
| 1057 | |
| 1058 | /* |
| 1059 | * SD_flags allowed in topology descriptions. |
| 1060 | * |
| 1061 | * These flags are purely descriptive of the topology and do not prescribe |
| 1062 | * behaviour. Behaviour is artificial and mapped in the below sd_init() |
| 1063 | * function: |
| 1064 | * |
| 1065 | * SD_SHARE_CPUCAPACITY - describes SMT topologies |
| 1066 | * SD_SHARE_PKG_RESOURCES - describes shared caches |
| 1067 | * SD_NUMA - describes NUMA topologies |
| 1068 | * SD_SHARE_POWERDOMAIN - describes shared power domain |
| 1069 | * SD_ASYM_CPUCAPACITY - describes mixed capacity topologies |
| 1070 | * |
| 1071 | * Odd one out, which beside describing the topology has a quirk also |
| 1072 | * prescribes the desired behaviour that goes along with it: |
| 1073 | * |
| 1074 | * SD_ASYM_PACKING - describes SMT quirks |
| 1075 | */ |
| 1076 | #define TOPOLOGY_SD_FLAGS \ |
| 1077 | (SD_SHARE_CPUCAPACITY | \ |
| 1078 | SD_SHARE_PKG_RESOURCES | \ |
| 1079 | SD_NUMA | \ |
| 1080 | SD_ASYM_PACKING | \ |
| 1081 | SD_ASYM_CPUCAPACITY | \ |
| 1082 | SD_SHARE_POWERDOMAIN) |
| 1083 | |
| 1084 | static struct sched_domain * |
| 1085 | sd_init(struct sched_domain_topology_level *tl, |
| 1086 | const struct cpumask *cpu_map, |
| 1087 | struct sched_domain *child, int cpu) |
| 1088 | { |
| 1089 | struct sd_data *sdd = &tl->data; |
| 1090 | struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu); |
| 1091 | int sd_id, sd_weight, sd_flags = 0; |
| 1092 | |
| 1093 | #ifdef CONFIG_NUMA |
| 1094 | /* |
| 1095 | * Ugly hack to pass state to sd_numa_mask()... |
| 1096 | */ |
| 1097 | sched_domains_curr_level = tl->numa_level; |
| 1098 | #endif |
| 1099 | |
| 1100 | sd_weight = cpumask_weight(tl->mask(cpu)); |
| 1101 | |
| 1102 | if (tl->sd_flags) |
| 1103 | sd_flags = (*tl->sd_flags)(); |
| 1104 | if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS, |
| 1105 | "wrong sd_flags in topology description\n")) |
| 1106 | sd_flags &= ~TOPOLOGY_SD_FLAGS; |
| 1107 | |
| 1108 | *sd = (struct sched_domain){ |
| 1109 | .min_interval = sd_weight, |
| 1110 | .max_interval = 2*sd_weight, |
| 1111 | .busy_factor = 32, |
| 1112 | .imbalance_pct = 125, |
| 1113 | |
| 1114 | .cache_nice_tries = 0, |
| 1115 | .busy_idx = 0, |
| 1116 | .idle_idx = 0, |
| 1117 | .newidle_idx = 0, |
| 1118 | .wake_idx = 0, |
| 1119 | .forkexec_idx = 0, |
| 1120 | |
| 1121 | .flags = 1*SD_LOAD_BALANCE |
| 1122 | | 1*SD_BALANCE_NEWIDLE |
| 1123 | | 1*SD_BALANCE_EXEC |
| 1124 | | 1*SD_BALANCE_FORK |
| 1125 | | 0*SD_BALANCE_WAKE |
| 1126 | | 1*SD_WAKE_AFFINE |
| 1127 | | 0*SD_SHARE_CPUCAPACITY |
| 1128 | | 0*SD_SHARE_PKG_RESOURCES |
| 1129 | | 0*SD_SERIALIZE |
| 1130 | | 0*SD_PREFER_SIBLING |
| 1131 | | 0*SD_NUMA |
| 1132 | | sd_flags |
| 1133 | , |
| 1134 | |
| 1135 | .last_balance = jiffies, |
| 1136 | .balance_interval = sd_weight, |
| 1137 | .smt_gain = 0, |
| 1138 | .max_newidle_lb_cost = 0, |
| 1139 | .next_decay_max_lb_cost = jiffies, |
| 1140 | .child = child, |
| 1141 | #ifdef CONFIG_SCHED_DEBUG |
| 1142 | .name = tl->name, |
| 1143 | #endif |
| 1144 | }; |
| 1145 | |
| 1146 | cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu)); |
| 1147 | sd_id = cpumask_first(sched_domain_span(sd)); |
| 1148 | |
| 1149 | /* |
| 1150 | * Convert topological properties into behaviour. |
| 1151 | */ |
| 1152 | |
| 1153 | if (sd->flags & SD_ASYM_CPUCAPACITY) { |
| 1154 | struct sched_domain *t = sd; |
| 1155 | |
| 1156 | for_each_lower_domain(t) |
| 1157 | t->flags |= SD_BALANCE_WAKE; |
| 1158 | } |
| 1159 | |
| 1160 | if (sd->flags & SD_SHARE_CPUCAPACITY) { |
| 1161 | sd->flags |= SD_PREFER_SIBLING; |
| 1162 | sd->imbalance_pct = 110; |
| 1163 | sd->smt_gain = 1178; /* ~15% */ |
| 1164 | |
| 1165 | } else if (sd->flags & SD_SHARE_PKG_RESOURCES) { |
Peter Zijlstra | ed4ad1c | 2017-10-02 14:50:33 +0200 | [diff] [blame] | 1166 | sd->flags |= SD_PREFER_SIBLING; |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 1167 | sd->imbalance_pct = 117; |
| 1168 | sd->cache_nice_tries = 1; |
| 1169 | sd->busy_idx = 2; |
| 1170 | |
| 1171 | #ifdef CONFIG_NUMA |
| 1172 | } else if (sd->flags & SD_NUMA) { |
| 1173 | sd->cache_nice_tries = 2; |
| 1174 | sd->busy_idx = 3; |
| 1175 | sd->idle_idx = 2; |
| 1176 | |
| 1177 | sd->flags |= SD_SERIALIZE; |
| 1178 | if (sched_domains_numa_distance[tl->numa_level] > RECLAIM_DISTANCE) { |
| 1179 | sd->flags &= ~(SD_BALANCE_EXEC | |
| 1180 | SD_BALANCE_FORK | |
| 1181 | SD_WAKE_AFFINE); |
| 1182 | } |
| 1183 | |
| 1184 | #endif |
| 1185 | } else { |
| 1186 | sd->flags |= SD_PREFER_SIBLING; |
| 1187 | sd->cache_nice_tries = 1; |
| 1188 | sd->busy_idx = 2; |
| 1189 | sd->idle_idx = 1; |
| 1190 | } |
| 1191 | |
| 1192 | /* |
| 1193 | * For all levels sharing cache; connect a sched_domain_shared |
| 1194 | * instance. |
| 1195 | */ |
| 1196 | if (sd->flags & SD_SHARE_PKG_RESOURCES) { |
| 1197 | sd->shared = *per_cpu_ptr(sdd->sds, sd_id); |
| 1198 | atomic_inc(&sd->shared->ref); |
| 1199 | atomic_set(&sd->shared->nr_busy_cpus, sd_weight); |
| 1200 | } |
| 1201 | |
| 1202 | sd->private = sdd; |
| 1203 | |
| 1204 | return sd; |
| 1205 | } |
| 1206 | |
| 1207 | /* |
| 1208 | * Topology list, bottom-up. |
| 1209 | */ |
| 1210 | static struct sched_domain_topology_level default_topology[] = { |
| 1211 | #ifdef CONFIG_SCHED_SMT |
| 1212 | { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) }, |
| 1213 | #endif |
| 1214 | #ifdef CONFIG_SCHED_MC |
| 1215 | { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) }, |
| 1216 | #endif |
| 1217 | { cpu_cpu_mask, SD_INIT_NAME(DIE) }, |
| 1218 | { NULL, }, |
| 1219 | }; |
| 1220 | |
| 1221 | static struct sched_domain_topology_level *sched_domain_topology = |
| 1222 | default_topology; |
| 1223 | |
| 1224 | #define for_each_sd_topology(tl) \ |
| 1225 | for (tl = sched_domain_topology; tl->mask; tl++) |
| 1226 | |
| 1227 | void set_sched_topology(struct sched_domain_topology_level *tl) |
| 1228 | { |
| 1229 | if (WARN_ON_ONCE(sched_smp_initialized)) |
| 1230 | return; |
| 1231 | |
| 1232 | sched_domain_topology = tl; |
| 1233 | } |
| 1234 | |
| 1235 | #ifdef CONFIG_NUMA |
| 1236 | |
| 1237 | static const struct cpumask *sd_numa_mask(int cpu) |
| 1238 | { |
| 1239 | return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)]; |
| 1240 | } |
| 1241 | |
| 1242 | static void sched_numa_warn(const char *str) |
| 1243 | { |
| 1244 | static int done = false; |
| 1245 | int i,j; |
| 1246 | |
| 1247 | if (done) |
| 1248 | return; |
| 1249 | |
| 1250 | done = true; |
| 1251 | |
| 1252 | printk(KERN_WARNING "ERROR: %s\n\n", str); |
| 1253 | |
| 1254 | for (i = 0; i < nr_node_ids; i++) { |
| 1255 | printk(KERN_WARNING " "); |
| 1256 | for (j = 0; j < nr_node_ids; j++) |
| 1257 | printk(KERN_CONT "%02d ", node_distance(i,j)); |
| 1258 | printk(KERN_CONT "\n"); |
| 1259 | } |
| 1260 | printk(KERN_WARNING "\n"); |
| 1261 | } |
| 1262 | |
| 1263 | bool find_numa_distance(int distance) |
| 1264 | { |
| 1265 | int i; |
| 1266 | |
| 1267 | if (distance == node_distance(0, 0)) |
| 1268 | return true; |
| 1269 | |
| 1270 | for (i = 0; i < sched_domains_numa_levels; i++) { |
| 1271 | if (sched_domains_numa_distance[i] == distance) |
| 1272 | return true; |
| 1273 | } |
| 1274 | |
| 1275 | return false; |
| 1276 | } |
| 1277 | |
| 1278 | /* |
| 1279 | * A system can have three types of NUMA topology: |
| 1280 | * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system |
| 1281 | * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes |
| 1282 | * NUMA_BACKPLANE: nodes can reach other nodes through a backplane |
| 1283 | * |
| 1284 | * The difference between a glueless mesh topology and a backplane |
| 1285 | * topology lies in whether communication between not directly |
| 1286 | * connected nodes goes through intermediary nodes (where programs |
| 1287 | * could run), or through backplane controllers. This affects |
| 1288 | * placement of programs. |
| 1289 | * |
| 1290 | * The type of topology can be discerned with the following tests: |
| 1291 | * - If the maximum distance between any nodes is 1 hop, the system |
| 1292 | * is directly connected. |
| 1293 | * - If for two nodes A and B, located N > 1 hops away from each other, |
| 1294 | * there is an intermediary node C, which is < N hops away from both |
| 1295 | * nodes A and B, the system is a glueless mesh. |
| 1296 | */ |
| 1297 | static void init_numa_topology_type(void) |
| 1298 | { |
| 1299 | int a, b, c, n; |
| 1300 | |
| 1301 | n = sched_max_numa_distance; |
| 1302 | |
| 1303 | if (sched_domains_numa_levels <= 1) { |
| 1304 | sched_numa_topology_type = NUMA_DIRECT; |
| 1305 | return; |
| 1306 | } |
| 1307 | |
| 1308 | for_each_online_node(a) { |
| 1309 | for_each_online_node(b) { |
| 1310 | /* Find two nodes furthest removed from each other. */ |
| 1311 | if (node_distance(a, b) < n) |
| 1312 | continue; |
| 1313 | |
| 1314 | /* Is there an intermediary node between a and b? */ |
| 1315 | for_each_online_node(c) { |
| 1316 | if (node_distance(a, c) < n && |
| 1317 | node_distance(b, c) < n) { |
| 1318 | sched_numa_topology_type = |
| 1319 | NUMA_GLUELESS_MESH; |
| 1320 | return; |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | sched_numa_topology_type = NUMA_BACKPLANE; |
| 1325 | return; |
| 1326 | } |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | void sched_init_numa(void) |
| 1331 | { |
| 1332 | int next_distance, curr_distance = node_distance(0, 0); |
| 1333 | struct sched_domain_topology_level *tl; |
| 1334 | int level = 0; |
| 1335 | int i, j, k; |
| 1336 | |
| 1337 | sched_domains_numa_distance = kzalloc(sizeof(int) * nr_node_ids, GFP_KERNEL); |
| 1338 | if (!sched_domains_numa_distance) |
| 1339 | return; |
| 1340 | |
Suravee Suthikulpanit | 051f3ca | 2017-09-07 02:20:05 -0500 | [diff] [blame] | 1341 | /* Includes NUMA identity node at level 0. */ |
| 1342 | sched_domains_numa_distance[level++] = curr_distance; |
| 1343 | sched_domains_numa_levels = level; |
| 1344 | |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 1345 | /* |
| 1346 | * O(nr_nodes^2) deduplicating selection sort -- in order to find the |
| 1347 | * unique distances in the node_distance() table. |
| 1348 | * |
| 1349 | * Assumes node_distance(0,j) includes all distances in |
| 1350 | * node_distance(i,j) in order to avoid cubic time. |
| 1351 | */ |
| 1352 | next_distance = curr_distance; |
| 1353 | for (i = 0; i < nr_node_ids; i++) { |
| 1354 | for (j = 0; j < nr_node_ids; j++) { |
| 1355 | for (k = 0; k < nr_node_ids; k++) { |
| 1356 | int distance = node_distance(i, k); |
| 1357 | |
| 1358 | if (distance > curr_distance && |
| 1359 | (distance < next_distance || |
| 1360 | next_distance == curr_distance)) |
| 1361 | next_distance = distance; |
| 1362 | |
| 1363 | /* |
| 1364 | * While not a strong assumption it would be nice to know |
| 1365 | * about cases where if node A is connected to B, B is not |
| 1366 | * equally connected to A. |
| 1367 | */ |
| 1368 | if (sched_debug() && node_distance(k, i) != distance) |
| 1369 | sched_numa_warn("Node-distance not symmetric"); |
| 1370 | |
| 1371 | if (sched_debug() && i && !find_numa_distance(distance)) |
| 1372 | sched_numa_warn("Node-0 not representative"); |
| 1373 | } |
| 1374 | if (next_distance != curr_distance) { |
| 1375 | sched_domains_numa_distance[level++] = next_distance; |
| 1376 | sched_domains_numa_levels = level; |
| 1377 | curr_distance = next_distance; |
| 1378 | } else break; |
| 1379 | } |
| 1380 | |
| 1381 | /* |
| 1382 | * In case of sched_debug() we verify the above assumption. |
| 1383 | */ |
| 1384 | if (!sched_debug()) |
| 1385 | break; |
| 1386 | } |
| 1387 | |
| 1388 | if (!level) |
| 1389 | return; |
| 1390 | |
| 1391 | /* |
Suravee Suthikulpanit | 051f3ca | 2017-09-07 02:20:05 -0500 | [diff] [blame] | 1392 | * 'level' contains the number of unique distances |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 1393 | * |
| 1394 | * The sched_domains_numa_distance[] array includes the actual distance |
| 1395 | * numbers. |
| 1396 | */ |
| 1397 | |
| 1398 | /* |
| 1399 | * Here, we should temporarily reset sched_domains_numa_levels to 0. |
| 1400 | * If it fails to allocate memory for array sched_domains_numa_masks[][], |
| 1401 | * the array will contain less then 'level' members. This could be |
| 1402 | * dangerous when we use it to iterate array sched_domains_numa_masks[][] |
| 1403 | * in other functions. |
| 1404 | * |
| 1405 | * We reset it to 'level' at the end of this function. |
| 1406 | */ |
| 1407 | sched_domains_numa_levels = 0; |
| 1408 | |
| 1409 | sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL); |
| 1410 | if (!sched_domains_numa_masks) |
| 1411 | return; |
| 1412 | |
| 1413 | /* |
| 1414 | * Now for each level, construct a mask per node which contains all |
| 1415 | * CPUs of nodes that are that many hops away from us. |
| 1416 | */ |
| 1417 | for (i = 0; i < level; i++) { |
| 1418 | sched_domains_numa_masks[i] = |
| 1419 | kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL); |
| 1420 | if (!sched_domains_numa_masks[i]) |
| 1421 | return; |
| 1422 | |
| 1423 | for (j = 0; j < nr_node_ids; j++) { |
| 1424 | struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL); |
| 1425 | if (!mask) |
| 1426 | return; |
| 1427 | |
| 1428 | sched_domains_numa_masks[i][j] = mask; |
| 1429 | |
| 1430 | for_each_node(k) { |
| 1431 | if (node_distance(j, k) > sched_domains_numa_distance[i]) |
| 1432 | continue; |
| 1433 | |
| 1434 | cpumask_or(mask, mask, cpumask_of_node(k)); |
| 1435 | } |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | /* Compute default topology size */ |
| 1440 | for (i = 0; sched_domain_topology[i].mask; i++); |
| 1441 | |
| 1442 | tl = kzalloc((i + level + 1) * |
| 1443 | sizeof(struct sched_domain_topology_level), GFP_KERNEL); |
| 1444 | if (!tl) |
| 1445 | return; |
| 1446 | |
| 1447 | /* |
| 1448 | * Copy the default topology bits.. |
| 1449 | */ |
| 1450 | for (i = 0; sched_domain_topology[i].mask; i++) |
| 1451 | tl[i] = sched_domain_topology[i]; |
| 1452 | |
| 1453 | /* |
Suravee Suthikulpanit | 051f3ca | 2017-09-07 02:20:05 -0500 | [diff] [blame] | 1454 | * Add the NUMA identity distance, aka single NODE. |
| 1455 | */ |
| 1456 | tl[i++] = (struct sched_domain_topology_level){ |
| 1457 | .mask = sd_numa_mask, |
| 1458 | .numa_level = 0, |
| 1459 | SD_INIT_NAME(NODE) |
| 1460 | }; |
| 1461 | |
| 1462 | /* |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 1463 | * .. and append 'j' levels of NUMA goodness. |
| 1464 | */ |
Suravee Suthikulpanit | 051f3ca | 2017-09-07 02:20:05 -0500 | [diff] [blame] | 1465 | for (j = 1; j < level; i++, j++) { |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 1466 | tl[i] = (struct sched_domain_topology_level){ |
| 1467 | .mask = sd_numa_mask, |
| 1468 | .sd_flags = cpu_numa_flags, |
| 1469 | .flags = SDTL_OVERLAP, |
| 1470 | .numa_level = j, |
| 1471 | SD_INIT_NAME(NUMA) |
| 1472 | }; |
| 1473 | } |
| 1474 | |
| 1475 | sched_domain_topology = tl; |
| 1476 | |
| 1477 | sched_domains_numa_levels = level; |
| 1478 | sched_max_numa_distance = sched_domains_numa_distance[level - 1]; |
| 1479 | |
| 1480 | init_numa_topology_type(); |
| 1481 | } |
| 1482 | |
| 1483 | void sched_domains_numa_masks_set(unsigned int cpu) |
| 1484 | { |
| 1485 | int node = cpu_to_node(cpu); |
| 1486 | int i, j; |
| 1487 | |
| 1488 | for (i = 0; i < sched_domains_numa_levels; i++) { |
| 1489 | for (j = 0; j < nr_node_ids; j++) { |
| 1490 | if (node_distance(j, node) <= sched_domains_numa_distance[i]) |
| 1491 | cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]); |
| 1492 | } |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | void sched_domains_numa_masks_clear(unsigned int cpu) |
| 1497 | { |
| 1498 | int i, j; |
| 1499 | |
| 1500 | for (i = 0; i < sched_domains_numa_levels; i++) { |
| 1501 | for (j = 0; j < nr_node_ids; j++) |
| 1502 | cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]); |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | #endif /* CONFIG_NUMA */ |
| 1507 | |
| 1508 | static int __sdt_alloc(const struct cpumask *cpu_map) |
| 1509 | { |
| 1510 | struct sched_domain_topology_level *tl; |
| 1511 | int j; |
| 1512 | |
| 1513 | for_each_sd_topology(tl) { |
| 1514 | struct sd_data *sdd = &tl->data; |
| 1515 | |
| 1516 | sdd->sd = alloc_percpu(struct sched_domain *); |
| 1517 | if (!sdd->sd) |
| 1518 | return -ENOMEM; |
| 1519 | |
| 1520 | sdd->sds = alloc_percpu(struct sched_domain_shared *); |
| 1521 | if (!sdd->sds) |
| 1522 | return -ENOMEM; |
| 1523 | |
| 1524 | sdd->sg = alloc_percpu(struct sched_group *); |
| 1525 | if (!sdd->sg) |
| 1526 | return -ENOMEM; |
| 1527 | |
| 1528 | sdd->sgc = alloc_percpu(struct sched_group_capacity *); |
| 1529 | if (!sdd->sgc) |
| 1530 | return -ENOMEM; |
| 1531 | |
| 1532 | for_each_cpu(j, cpu_map) { |
| 1533 | struct sched_domain *sd; |
| 1534 | struct sched_domain_shared *sds; |
| 1535 | struct sched_group *sg; |
| 1536 | struct sched_group_capacity *sgc; |
| 1537 | |
| 1538 | sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(), |
| 1539 | GFP_KERNEL, cpu_to_node(j)); |
| 1540 | if (!sd) |
| 1541 | return -ENOMEM; |
| 1542 | |
| 1543 | *per_cpu_ptr(sdd->sd, j) = sd; |
| 1544 | |
| 1545 | sds = kzalloc_node(sizeof(struct sched_domain_shared), |
| 1546 | GFP_KERNEL, cpu_to_node(j)); |
| 1547 | if (!sds) |
| 1548 | return -ENOMEM; |
| 1549 | |
| 1550 | *per_cpu_ptr(sdd->sds, j) = sds; |
| 1551 | |
| 1552 | sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(), |
| 1553 | GFP_KERNEL, cpu_to_node(j)); |
| 1554 | if (!sg) |
| 1555 | return -ENOMEM; |
| 1556 | |
| 1557 | sg->next = sg; |
| 1558 | |
| 1559 | *per_cpu_ptr(sdd->sg, j) = sg; |
| 1560 | |
| 1561 | sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(), |
| 1562 | GFP_KERNEL, cpu_to_node(j)); |
| 1563 | if (!sgc) |
| 1564 | return -ENOMEM; |
| 1565 | |
Peter Zijlstra | 005f874 | 2017-04-26 17:35:35 +0200 | [diff] [blame] | 1566 | #ifdef CONFIG_SCHED_DEBUG |
| 1567 | sgc->id = j; |
| 1568 | #endif |
| 1569 | |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 1570 | *per_cpu_ptr(sdd->sgc, j) = sgc; |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | return 0; |
| 1575 | } |
| 1576 | |
| 1577 | static void __sdt_free(const struct cpumask *cpu_map) |
| 1578 | { |
| 1579 | struct sched_domain_topology_level *tl; |
| 1580 | int j; |
| 1581 | |
| 1582 | for_each_sd_topology(tl) { |
| 1583 | struct sd_data *sdd = &tl->data; |
| 1584 | |
| 1585 | for_each_cpu(j, cpu_map) { |
| 1586 | struct sched_domain *sd; |
| 1587 | |
| 1588 | if (sdd->sd) { |
| 1589 | sd = *per_cpu_ptr(sdd->sd, j); |
| 1590 | if (sd && (sd->flags & SD_OVERLAP)) |
| 1591 | free_sched_groups(sd->groups, 0); |
| 1592 | kfree(*per_cpu_ptr(sdd->sd, j)); |
| 1593 | } |
| 1594 | |
| 1595 | if (sdd->sds) |
| 1596 | kfree(*per_cpu_ptr(sdd->sds, j)); |
| 1597 | if (sdd->sg) |
| 1598 | kfree(*per_cpu_ptr(sdd->sg, j)); |
| 1599 | if (sdd->sgc) |
| 1600 | kfree(*per_cpu_ptr(sdd->sgc, j)); |
| 1601 | } |
| 1602 | free_percpu(sdd->sd); |
| 1603 | sdd->sd = NULL; |
| 1604 | free_percpu(sdd->sds); |
| 1605 | sdd->sds = NULL; |
| 1606 | free_percpu(sdd->sg); |
| 1607 | sdd->sg = NULL; |
| 1608 | free_percpu(sdd->sgc); |
| 1609 | sdd->sgc = NULL; |
| 1610 | } |
| 1611 | } |
| 1612 | |
Viresh Kumar | 181a80d1 | 2017-04-27 13:58:59 +0530 | [diff] [blame] | 1613 | static struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl, |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 1614 | const struct cpumask *cpu_map, struct sched_domain_attr *attr, |
| 1615 | struct sched_domain *child, int cpu) |
| 1616 | { |
| 1617 | struct sched_domain *sd = sd_init(tl, cpu_map, child, cpu); |
| 1618 | |
| 1619 | if (child) { |
| 1620 | sd->level = child->level + 1; |
| 1621 | sched_domain_level_max = max(sched_domain_level_max, sd->level); |
| 1622 | child->parent = sd; |
| 1623 | |
| 1624 | if (!cpumask_subset(sched_domain_span(child), |
| 1625 | sched_domain_span(sd))) { |
| 1626 | pr_err("BUG: arch topology borken\n"); |
| 1627 | #ifdef CONFIG_SCHED_DEBUG |
| 1628 | pr_err(" the %s domain not a subset of the %s domain\n", |
| 1629 | child->name, sd->name); |
| 1630 | #endif |
| 1631 | /* Fixup, ensure @sd has at least @child cpus. */ |
| 1632 | cpumask_or(sched_domain_span(sd), |
| 1633 | sched_domain_span(sd), |
| 1634 | sched_domain_span(child)); |
| 1635 | } |
| 1636 | |
| 1637 | } |
| 1638 | set_domain_attribute(sd, attr); |
| 1639 | |
| 1640 | return sd; |
| 1641 | } |
| 1642 | |
| 1643 | /* |
| 1644 | * Build sched domains for a given set of CPUs and attach the sched domains |
| 1645 | * to the individual CPUs |
| 1646 | */ |
| 1647 | static int |
| 1648 | build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *attr) |
| 1649 | { |
| 1650 | enum s_alloc alloc_state; |
| 1651 | struct sched_domain *sd; |
| 1652 | struct s_data d; |
| 1653 | struct rq *rq = NULL; |
| 1654 | int i, ret = -ENOMEM; |
| 1655 | |
| 1656 | alloc_state = __visit_domain_allocation_hell(&d, cpu_map); |
| 1657 | if (alloc_state != sa_rootdomain) |
| 1658 | goto error; |
| 1659 | |
| 1660 | /* Set up domains for CPUs specified by the cpu_map: */ |
| 1661 | for_each_cpu(i, cpu_map) { |
| 1662 | struct sched_domain_topology_level *tl; |
| 1663 | |
| 1664 | sd = NULL; |
| 1665 | for_each_sd_topology(tl) { |
| 1666 | sd = build_sched_domain(tl, cpu_map, attr, sd, i); |
| 1667 | if (tl == sched_domain_topology) |
| 1668 | *per_cpu_ptr(d.sd, i) = sd; |
Peter Zijlstra | af85596 | 2017-04-26 17:36:41 +0200 | [diff] [blame] | 1669 | if (tl->flags & SDTL_OVERLAP) |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 1670 | sd->flags |= SD_OVERLAP; |
| 1671 | if (cpumask_equal(cpu_map, sched_domain_span(sd))) |
| 1672 | break; |
| 1673 | } |
| 1674 | } |
| 1675 | |
| 1676 | /* Build the groups for the domains */ |
| 1677 | for_each_cpu(i, cpu_map) { |
| 1678 | for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) { |
| 1679 | sd->span_weight = cpumask_weight(sched_domain_span(sd)); |
| 1680 | if (sd->flags & SD_OVERLAP) { |
| 1681 | if (build_overlap_sched_groups(sd, i)) |
| 1682 | goto error; |
| 1683 | } else { |
| 1684 | if (build_sched_groups(sd, i)) |
| 1685 | goto error; |
| 1686 | } |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | /* Calculate CPU capacity for physical packages and nodes */ |
| 1691 | for (i = nr_cpumask_bits-1; i >= 0; i--) { |
| 1692 | if (!cpumask_test_cpu(i, cpu_map)) |
| 1693 | continue; |
| 1694 | |
| 1695 | for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) { |
| 1696 | claim_allocations(i, sd); |
| 1697 | init_sched_groups_capacity(i, sd); |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | /* Attach the domains */ |
| 1702 | rcu_read_lock(); |
| 1703 | for_each_cpu(i, cpu_map) { |
| 1704 | rq = cpu_rq(i); |
| 1705 | sd = *per_cpu_ptr(d.sd, i); |
| 1706 | |
| 1707 | /* Use READ_ONCE()/WRITE_ONCE() to avoid load/store tearing: */ |
| 1708 | if (rq->cpu_capacity_orig > READ_ONCE(d.rd->max_cpu_capacity)) |
| 1709 | WRITE_ONCE(d.rd->max_cpu_capacity, rq->cpu_capacity_orig); |
| 1710 | |
| 1711 | cpu_attach_domain(sd, d.rd, i); |
| 1712 | } |
| 1713 | rcu_read_unlock(); |
| 1714 | |
| 1715 | if (rq && sched_debug_enabled) { |
| 1716 | pr_info("span: %*pbl (max cpu_capacity = %lu)\n", |
| 1717 | cpumask_pr_args(cpu_map), rq->rd->max_cpu_capacity); |
| 1718 | } |
| 1719 | |
| 1720 | ret = 0; |
| 1721 | error: |
| 1722 | __free_domain_allocs(&d, alloc_state, cpu_map); |
| 1723 | return ret; |
| 1724 | } |
| 1725 | |
| 1726 | /* Current sched domains: */ |
| 1727 | static cpumask_var_t *doms_cur; |
| 1728 | |
| 1729 | /* Number of sched domains in 'doms_cur': */ |
| 1730 | static int ndoms_cur; |
| 1731 | |
| 1732 | /* Attribues of custom domains in 'doms_cur' */ |
| 1733 | static struct sched_domain_attr *dattr_cur; |
| 1734 | |
| 1735 | /* |
| 1736 | * Special case: If a kmalloc() of a doms_cur partition (array of |
| 1737 | * cpumask) fails, then fallback to a single sched domain, |
| 1738 | * as determined by the single cpumask fallback_doms. |
| 1739 | */ |
Peter Zijlstra | 8d5dc51 | 2017-04-25 15:29:40 +0200 | [diff] [blame] | 1740 | static cpumask_var_t fallback_doms; |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 1741 | |
| 1742 | /* |
| 1743 | * arch_update_cpu_topology lets virtualized architectures update the |
| 1744 | * CPU core maps. It is supposed to return 1 if the topology changed |
| 1745 | * or 0 if it stayed the same. |
| 1746 | */ |
| 1747 | int __weak arch_update_cpu_topology(void) |
| 1748 | { |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
| 1752 | cpumask_var_t *alloc_sched_domains(unsigned int ndoms) |
| 1753 | { |
| 1754 | int i; |
| 1755 | cpumask_var_t *doms; |
| 1756 | |
| 1757 | doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL); |
| 1758 | if (!doms) |
| 1759 | return NULL; |
| 1760 | for (i = 0; i < ndoms; i++) { |
| 1761 | if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) { |
| 1762 | free_sched_domains(doms, i); |
| 1763 | return NULL; |
| 1764 | } |
| 1765 | } |
| 1766 | return doms; |
| 1767 | } |
| 1768 | |
| 1769 | void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms) |
| 1770 | { |
| 1771 | unsigned int i; |
| 1772 | for (i = 0; i < ndoms; i++) |
| 1773 | free_cpumask_var(doms[i]); |
| 1774 | kfree(doms); |
| 1775 | } |
| 1776 | |
| 1777 | /* |
| 1778 | * Set up scheduler domains and groups. Callers must hold the hotplug lock. |
| 1779 | * For now this just excludes isolated CPUs, but could be used to |
| 1780 | * exclude other special cases in the future. |
| 1781 | */ |
Peter Zijlstra | 8d5dc51 | 2017-04-25 15:29:40 +0200 | [diff] [blame] | 1782 | int sched_init_domains(const struct cpumask *cpu_map) |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 1783 | { |
| 1784 | int err; |
| 1785 | |
Peter Zijlstra | 8d5dc51 | 2017-04-25 15:29:40 +0200 | [diff] [blame] | 1786 | zalloc_cpumask_var(&sched_domains_tmpmask, GFP_KERNEL); |
Peter Zijlstra | 1676330 | 2017-04-25 14:31:11 +0200 | [diff] [blame] | 1787 | zalloc_cpumask_var(&sched_domains_tmpmask2, GFP_KERNEL); |
Peter Zijlstra | 8d5dc51 | 2017-04-25 15:29:40 +0200 | [diff] [blame] | 1788 | zalloc_cpumask_var(&fallback_doms, GFP_KERNEL); |
| 1789 | |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 1790 | arch_update_cpu_topology(); |
| 1791 | ndoms_cur = 1; |
| 1792 | doms_cur = alloc_sched_domains(ndoms_cur); |
| 1793 | if (!doms_cur) |
| 1794 | doms_cur = &fallback_doms; |
| 1795 | cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map); |
| 1796 | err = build_sched_domains(doms_cur[0], NULL); |
| 1797 | register_sched_domain_sysctl(); |
| 1798 | |
| 1799 | return err; |
| 1800 | } |
| 1801 | |
| 1802 | /* |
| 1803 | * Detach sched domains from a group of CPUs specified in cpu_map |
| 1804 | * These CPUs will now be attached to the NULL domain |
| 1805 | */ |
| 1806 | static void detach_destroy_domains(const struct cpumask *cpu_map) |
| 1807 | { |
| 1808 | int i; |
| 1809 | |
| 1810 | rcu_read_lock(); |
| 1811 | for_each_cpu(i, cpu_map) |
| 1812 | cpu_attach_domain(NULL, &def_root_domain, i); |
| 1813 | rcu_read_unlock(); |
| 1814 | } |
| 1815 | |
| 1816 | /* handle null as "default" */ |
| 1817 | static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur, |
| 1818 | struct sched_domain_attr *new, int idx_new) |
| 1819 | { |
| 1820 | struct sched_domain_attr tmp; |
| 1821 | |
| 1822 | /* Fast path: */ |
| 1823 | if (!new && !cur) |
| 1824 | return 1; |
| 1825 | |
| 1826 | tmp = SD_ATTR_INIT; |
| 1827 | return !memcmp(cur ? (cur + idx_cur) : &tmp, |
| 1828 | new ? (new + idx_new) : &tmp, |
| 1829 | sizeof(struct sched_domain_attr)); |
| 1830 | } |
| 1831 | |
| 1832 | /* |
| 1833 | * Partition sched domains as specified by the 'ndoms_new' |
| 1834 | * cpumasks in the array doms_new[] of cpumasks. This compares |
| 1835 | * doms_new[] to the current sched domain partitioning, doms_cur[]. |
| 1836 | * It destroys each deleted domain and builds each new domain. |
| 1837 | * |
| 1838 | * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'. |
| 1839 | * The masks don't intersect (don't overlap.) We should setup one |
| 1840 | * sched domain for each mask. CPUs not in any of the cpumasks will |
| 1841 | * not be load balanced. If the same cpumask appears both in the |
| 1842 | * current 'doms_cur' domains and in the new 'doms_new', we can leave |
| 1843 | * it as it is. |
| 1844 | * |
| 1845 | * The passed in 'doms_new' should be allocated using |
| 1846 | * alloc_sched_domains. This routine takes ownership of it and will |
| 1847 | * free_sched_domains it when done with it. If the caller failed the |
| 1848 | * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1, |
| 1849 | * and partition_sched_domains() will fallback to the single partition |
| 1850 | * 'fallback_doms', it also forces the domains to be rebuilt. |
| 1851 | * |
| 1852 | * If doms_new == NULL it will be replaced with cpu_online_mask. |
| 1853 | * ndoms_new == 0 is a special case for destroying existing domains, |
| 1854 | * and it will not create the default domain. |
| 1855 | * |
| 1856 | * Call with hotplug lock held |
| 1857 | */ |
| 1858 | void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[], |
| 1859 | struct sched_domain_attr *dattr_new) |
| 1860 | { |
| 1861 | int i, j, n; |
| 1862 | int new_topology; |
| 1863 | |
| 1864 | mutex_lock(&sched_domains_mutex); |
| 1865 | |
| 1866 | /* Always unregister in case we don't destroy any domains: */ |
| 1867 | unregister_sched_domain_sysctl(); |
| 1868 | |
| 1869 | /* Let the architecture update CPU core mappings: */ |
| 1870 | new_topology = arch_update_cpu_topology(); |
| 1871 | |
Peter Zijlstra | 09e0dd8 | 2017-08-08 12:16:24 +0200 | [diff] [blame] | 1872 | if (!doms_new) { |
| 1873 | WARN_ON_ONCE(dattr_new); |
| 1874 | n = 0; |
| 1875 | doms_new = alloc_sched_domains(1); |
| 1876 | if (doms_new) { |
| 1877 | n = 1; |
| 1878 | cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map); |
| 1879 | } |
| 1880 | } else { |
| 1881 | n = ndoms_new; |
| 1882 | } |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 1883 | |
| 1884 | /* Destroy deleted domains: */ |
| 1885 | for (i = 0; i < ndoms_cur; i++) { |
| 1886 | for (j = 0; j < n && !new_topology; j++) { |
| 1887 | if (cpumask_equal(doms_cur[i], doms_new[j]) |
| 1888 | && dattrs_equal(dattr_cur, i, dattr_new, j)) |
| 1889 | goto match1; |
| 1890 | } |
| 1891 | /* No match - a current sched domain not in new doms_new[] */ |
| 1892 | detach_destroy_domains(doms_cur[i]); |
| 1893 | match1: |
| 1894 | ; |
| 1895 | } |
| 1896 | |
| 1897 | n = ndoms_cur; |
Peter Zijlstra | 09e0dd8 | 2017-08-08 12:16:24 +0200 | [diff] [blame] | 1898 | if (!doms_new) { |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 1899 | n = 0; |
| 1900 | doms_new = &fallback_doms; |
| 1901 | cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map); |
Ingo Molnar | f2cb136 | 2017-02-01 13:10:18 +0100 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | /* Build new domains: */ |
| 1905 | for (i = 0; i < ndoms_new; i++) { |
| 1906 | for (j = 0; j < n && !new_topology; j++) { |
| 1907 | if (cpumask_equal(doms_new[i], doms_cur[j]) |
| 1908 | && dattrs_equal(dattr_new, i, dattr_cur, j)) |
| 1909 | goto match2; |
| 1910 | } |
| 1911 | /* No match - add a new doms_new */ |
| 1912 | build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL); |
| 1913 | match2: |
| 1914 | ; |
| 1915 | } |
| 1916 | |
| 1917 | /* Remember the new sched domains: */ |
| 1918 | if (doms_cur != &fallback_doms) |
| 1919 | free_sched_domains(doms_cur, ndoms_cur); |
| 1920 | |
| 1921 | kfree(dattr_cur); |
| 1922 | doms_cur = doms_new; |
| 1923 | dattr_cur = dattr_new; |
| 1924 | ndoms_cur = ndoms_new; |
| 1925 | |
| 1926 | register_sched_domain_sysctl(); |
| 1927 | |
| 1928 | mutex_unlock(&sched_domains_mutex); |
| 1929 | } |
| 1930 | |