topology.c 47 KB

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