topology.c 47 KB

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