topology.c 52 KB

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