topology.c 47 KB

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