topology.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Scheduler topology setup/handling methods
  4. */
  5. #include <trace/hooks/sched.h>
  6. DEFINE_MUTEX(sched_domains_mutex);
  7. #ifdef CONFIG_LOCKDEP
  8. EXPORT_SYMBOL_GPL(sched_domains_mutex);
  9. #endif
  10. /* Protected by sched_domains_mutex: */
  11. static cpumask_var_t sched_domains_tmpmask;
  12. static cpumask_var_t sched_domains_tmpmask2;
  13. #ifdef CONFIG_SCHED_DEBUG
  14. static int __init sched_debug_setup(char *str)
  15. {
  16. sched_debug_verbose = true;
  17. return 0;
  18. }
  19. early_param("sched_verbose", sched_debug_setup);
  20. static inline bool sched_debug(void)
  21. {
  22. return sched_debug_verbose;
  23. }
  24. #define SD_FLAG(_name, mflags) [__##_name] = { .meta_flags = mflags, .name = #_name },
  25. const struct sd_flag_debug sd_flag_debug[] = {
  26. #include <linux/sched/sd_flags.h>
  27. };
  28. #undef SD_FLAG
  29. static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
  30. struct cpumask *groupmask)
  31. {
  32. struct sched_group *group = sd->groups;
  33. unsigned long flags = sd->flags;
  34. unsigned int idx;
  35. cpumask_clear(groupmask);
  36. printk(KERN_DEBUG "%*s domain-%d: ", level, "", level);
  37. printk(KERN_CONT "span=%*pbl level=%s\n",
  38. cpumask_pr_args(sched_domain_span(sd)), sd->name);
  39. if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
  40. printk(KERN_ERR "ERROR: domain->span does not contain CPU%d\n", cpu);
  41. }
  42. if (group && !cpumask_test_cpu(cpu, sched_group_span(group))) {
  43. printk(KERN_ERR "ERROR: domain->groups does not contain CPU%d\n", cpu);
  44. }
  45. for_each_set_bit(idx, &flags, __SD_FLAG_CNT) {
  46. unsigned int flag = BIT(idx);
  47. unsigned int meta_flags = sd_flag_debug[idx].meta_flags;
  48. if ((meta_flags & SDF_SHARED_CHILD) && sd->child &&
  49. !(sd->child->flags & flag))
  50. printk(KERN_ERR "ERROR: flag %s set here but not in child\n",
  51. sd_flag_debug[idx].name);
  52. if ((meta_flags & SDF_SHARED_PARENT) && sd->parent &&
  53. !(sd->parent->flags & flag))
  54. printk(KERN_ERR "ERROR: flag %s set here but not in parent\n",
  55. sd_flag_debug[idx].name);
  56. }
  57. printk(KERN_DEBUG "%*s groups:", level + 1, "");
  58. do {
  59. if (!group) {
  60. printk("\n");
  61. printk(KERN_ERR "ERROR: group is NULL\n");
  62. break;
  63. }
  64. if (cpumask_empty(sched_group_span(group))) {
  65. printk(KERN_CONT "\n");
  66. printk(KERN_ERR "ERROR: empty group\n");
  67. break;
  68. }
  69. if (!(sd->flags & SD_OVERLAP) &&
  70. cpumask_intersects(groupmask, sched_group_span(group))) {
  71. printk(KERN_CONT "\n");
  72. printk(KERN_ERR "ERROR: repeated CPUs\n");
  73. break;
  74. }
  75. cpumask_or(groupmask, groupmask, sched_group_span(group));
  76. printk(KERN_CONT " %d:{ span=%*pbl",
  77. group->sgc->id,
  78. cpumask_pr_args(sched_group_span(group)));
  79. if ((sd->flags & SD_OVERLAP) &&
  80. !cpumask_equal(group_balance_mask(group), sched_group_span(group))) {
  81. printk(KERN_CONT " mask=%*pbl",
  82. cpumask_pr_args(group_balance_mask(group)));
  83. }
  84. if (group->sgc->capacity != SCHED_CAPACITY_SCALE)
  85. printk(KERN_CONT " cap=%lu", group->sgc->capacity);
  86. if (group == sd->groups && sd->child &&
  87. !cpumask_equal(sched_domain_span(sd->child),
  88. sched_group_span(group))) {
  89. printk(KERN_ERR "ERROR: domain->groups does not match domain->child\n");
  90. }
  91. printk(KERN_CONT " }");
  92. group = group->next;
  93. if (group != sd->groups)
  94. printk(KERN_CONT ",");
  95. } while (group != sd->groups);
  96. printk(KERN_CONT "\n");
  97. if (!cpumask_equal(sched_domain_span(sd), groupmask))
  98. printk(KERN_ERR "ERROR: groups don't span domain->span\n");
  99. if (sd->parent &&
  100. !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
  101. printk(KERN_ERR "ERROR: parent span is not a superset of domain->span\n");
  102. return 0;
  103. }
  104. static void sched_domain_debug(struct sched_domain *sd, int cpu)
  105. {
  106. int level = 0;
  107. if (!sched_debug_verbose)
  108. return;
  109. if (!sd) {
  110. printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
  111. return;
  112. }
  113. printk(KERN_DEBUG "CPU%d attaching sched-domain(s):\n", cpu);
  114. for (;;) {
  115. if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
  116. break;
  117. level++;
  118. sd = sd->parent;
  119. if (!sd)
  120. break;
  121. }
  122. }
  123. #else /* !CONFIG_SCHED_DEBUG */
  124. # define sched_debug_verbose 0
  125. # define sched_domain_debug(sd, cpu) do { } while (0)
  126. static inline bool sched_debug(void)
  127. {
  128. return false;
  129. }
  130. #endif /* CONFIG_SCHED_DEBUG */
  131. /* Generate a mask of SD flags with the SDF_NEEDS_GROUPS metaflag */
  132. #define SD_FLAG(name, mflags) (name * !!((mflags) & SDF_NEEDS_GROUPS)) |
  133. static const unsigned int SD_DEGENERATE_GROUPS_MASK =
  134. #include <linux/sched/sd_flags.h>
  135. 0;
  136. #undef SD_FLAG
  137. static int sd_degenerate(struct sched_domain *sd)
  138. {
  139. if (cpumask_weight(sched_domain_span(sd)) == 1)
  140. return 1;
  141. /* Following flags need at least 2 groups */
  142. if ((sd->flags & SD_DEGENERATE_GROUPS_MASK) &&
  143. (sd->groups != sd->groups->next))
  144. return 0;
  145. /* Following flags don't use groups */
  146. if (sd->flags & (SD_WAKE_AFFINE))
  147. return 0;
  148. return 1;
  149. }
  150. static int
  151. sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
  152. {
  153. unsigned long cflags = sd->flags, pflags = parent->flags;
  154. if (sd_degenerate(parent))
  155. return 1;
  156. if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
  157. return 0;
  158. /* Flags needing groups don't count if only 1 group in parent */
  159. if (parent->groups == parent->groups->next)
  160. pflags &= ~SD_DEGENERATE_GROUPS_MASK;
  161. if (~cflags & pflags)
  162. return 0;
  163. return 1;
  164. }
  165. #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
  166. DEFINE_STATIC_KEY_FALSE(sched_energy_present);
  167. static unsigned int sysctl_sched_energy_aware = 1;
  168. DEFINE_MUTEX(sched_energy_mutex);
  169. bool sched_energy_update;
  170. void rebuild_sched_domains_energy(void)
  171. {
  172. mutex_lock(&sched_energy_mutex);
  173. sched_energy_update = true;
  174. rebuild_sched_domains();
  175. sched_energy_update = false;
  176. mutex_unlock(&sched_energy_mutex);
  177. }
  178. #ifdef CONFIG_PROC_SYSCTL
  179. static int sched_energy_aware_handler(struct ctl_table *table, int write,
  180. void *buffer, size_t *lenp, loff_t *ppos)
  181. {
  182. int ret, state;
  183. if (write && !capable(CAP_SYS_ADMIN))
  184. return -EPERM;
  185. ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  186. if (!ret && write) {
  187. state = static_branch_unlikely(&sched_energy_present);
  188. if (state != sysctl_sched_energy_aware)
  189. rebuild_sched_domains_energy();
  190. }
  191. return ret;
  192. }
  193. static struct ctl_table sched_energy_aware_sysctls[] = {
  194. {
  195. .procname = "sched_energy_aware",
  196. .data = &sysctl_sched_energy_aware,
  197. .maxlen = sizeof(unsigned int),
  198. .mode = 0644,
  199. .proc_handler = sched_energy_aware_handler,
  200. .extra1 = SYSCTL_ZERO,
  201. .extra2 = SYSCTL_ONE,
  202. },
  203. {}
  204. };
  205. static int __init sched_energy_aware_sysctl_init(void)
  206. {
  207. register_sysctl_init("kernel", sched_energy_aware_sysctls);
  208. return 0;
  209. }
  210. late_initcall(sched_energy_aware_sysctl_init);
  211. #endif
  212. static void free_pd(struct perf_domain *pd)
  213. {
  214. struct perf_domain *tmp;
  215. while (pd) {
  216. tmp = pd->next;
  217. kfree(pd);
  218. pd = tmp;
  219. }
  220. }
  221. static struct perf_domain *find_pd(struct perf_domain *pd, int cpu)
  222. {
  223. while (pd) {
  224. if (cpumask_test_cpu(cpu, perf_domain_span(pd)))
  225. return pd;
  226. pd = pd->next;
  227. }
  228. return NULL;
  229. }
  230. static struct perf_domain *pd_init(int cpu)
  231. {
  232. struct em_perf_domain *obj = em_cpu_get(cpu);
  233. struct perf_domain *pd;
  234. if (!obj) {
  235. if (sched_debug())
  236. pr_info("%s: no EM found for CPU%d\n", __func__, cpu);
  237. return NULL;
  238. }
  239. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  240. if (!pd)
  241. return NULL;
  242. pd->em_pd = obj;
  243. return pd;
  244. }
  245. static void perf_domain_debug(const struct cpumask *cpu_map,
  246. struct perf_domain *pd)
  247. {
  248. if (!sched_debug() || !pd)
  249. return;
  250. printk(KERN_DEBUG "root_domain %*pbl:", cpumask_pr_args(cpu_map));
  251. while (pd) {
  252. printk(KERN_CONT " pd%d:{ cpus=%*pbl nr_pstate=%d }",
  253. cpumask_first(perf_domain_span(pd)),
  254. cpumask_pr_args(perf_domain_span(pd)),
  255. em_pd_nr_perf_states(pd->em_pd));
  256. pd = pd->next;
  257. }
  258. printk(KERN_CONT "\n");
  259. }
  260. static void destroy_perf_domain_rcu(struct rcu_head *rp)
  261. {
  262. struct perf_domain *pd;
  263. pd = container_of(rp, struct perf_domain, rcu);
  264. free_pd(pd);
  265. }
  266. static void sched_energy_set(bool has_eas)
  267. {
  268. if (!has_eas && static_branch_unlikely(&sched_energy_present)) {
  269. if (sched_debug())
  270. pr_info("%s: stopping EAS\n", __func__);
  271. static_branch_disable_cpuslocked(&sched_energy_present);
  272. } else if (has_eas && !static_branch_unlikely(&sched_energy_present)) {
  273. if (sched_debug())
  274. pr_info("%s: starting EAS\n", __func__);
  275. static_branch_enable_cpuslocked(&sched_energy_present);
  276. }
  277. }
  278. /*
  279. * EAS can be used on a root domain if it meets all the following conditions:
  280. * 1. an Energy Model (EM) is available;
  281. * 2. the SD_ASYM_CPUCAPACITY flag is set in the sched_domain hierarchy.
  282. * 3. no SMT is detected.
  283. * 4. the EM complexity is low enough to keep scheduling overheads low;
  284. * 5. frequency invariance support is present;
  285. *
  286. * The complexity of the Energy Model is defined as:
  287. *
  288. * C = nr_pd * (nr_cpus + nr_ps)
  289. *
  290. * with parameters defined as:
  291. * - nr_pd: the number of performance domains
  292. * - nr_cpus: the number of CPUs
  293. * - nr_ps: the sum of the number of performance states of all performance
  294. * domains (for example, on a system with 2 performance domains,
  295. * with 10 performance states each, nr_ps = 2 * 10 = 20).
  296. *
  297. * It is generally not a good idea to use such a model in the wake-up path on
  298. * very complex platforms because of the associated scheduling overheads. The
  299. * arbitrary constraint below prevents that. It makes EAS usable up to 16 CPUs
  300. * with per-CPU DVFS and less than 8 performance states each, for example.
  301. */
  302. #define EM_MAX_COMPLEXITY 2048
  303. static bool build_perf_domains(const struct cpumask *cpu_map)
  304. {
  305. int i, nr_pd = 0, nr_ps = 0, nr_cpus = cpumask_weight(cpu_map);
  306. struct perf_domain *pd = NULL, *tmp;
  307. int cpu = cpumask_first(cpu_map);
  308. struct root_domain *rd = cpu_rq(cpu)->rd;
  309. bool eas_check = false;
  310. if (!sysctl_sched_energy_aware)
  311. goto free;
  312. /*
  313. * EAS is enabled for asymmetric CPU capacity topologies.
  314. * Allow vendor to override if desired.
  315. */
  316. trace_android_rvh_build_perf_domains(&eas_check);
  317. if (!per_cpu(sd_asym_cpucapacity, cpu) && !eas_check) {
  318. if (sched_debug()) {
  319. pr_info("rd %*pbl: CPUs do not have asymmetric capacities\n",
  320. cpumask_pr_args(cpu_map));
  321. }
  322. goto free;
  323. }
  324. /* EAS definitely does *not* handle SMT */
  325. if (sched_smt_active()) {
  326. pr_warn("rd %*pbl: Disabling EAS, SMT is not supported\n",
  327. cpumask_pr_args(cpu_map));
  328. goto free;
  329. }
  330. if (!arch_scale_freq_invariant()) {
  331. if (sched_debug()) {
  332. pr_warn("rd %*pbl: Disabling EAS: frequency-invariant load tracking not yet supported",
  333. cpumask_pr_args(cpu_map));
  334. }
  335. goto free;
  336. }
  337. for_each_cpu(i, cpu_map) {
  338. /* Skip already covered CPUs. */
  339. if (find_pd(pd, i))
  340. continue;
  341. /* Create the new pd and add it to the local list. */
  342. tmp = pd_init(i);
  343. if (!tmp)
  344. goto free;
  345. tmp->next = pd;
  346. pd = tmp;
  347. /*
  348. * Count performance domains and performance states for the
  349. * complexity check.
  350. */
  351. nr_pd++;
  352. nr_ps += em_pd_nr_perf_states(pd->em_pd);
  353. }
  354. /* Bail out if the Energy Model complexity is too high. */
  355. if (nr_pd * (nr_ps + nr_cpus) > EM_MAX_COMPLEXITY) {
  356. WARN(1, "rd %*pbl: Failed to start EAS, EM complexity is too high\n",
  357. cpumask_pr_args(cpu_map));
  358. goto free;
  359. }
  360. perf_domain_debug(cpu_map, pd);
  361. /* Attach the new list of performance domains to the root domain. */
  362. tmp = rd->pd;
  363. rcu_assign_pointer(rd->pd, pd);
  364. if (tmp)
  365. call_rcu(&tmp->rcu, destroy_perf_domain_rcu);
  366. return !!pd;
  367. free:
  368. free_pd(pd);
  369. tmp = rd->pd;
  370. rcu_assign_pointer(rd->pd, NULL);
  371. if (tmp)
  372. call_rcu(&tmp->rcu, destroy_perf_domain_rcu);
  373. return false;
  374. }
  375. #else
  376. static void free_pd(struct perf_domain *pd) { }
  377. #endif /* CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL*/
  378. static void free_rootdomain(struct rcu_head *rcu)
  379. {
  380. struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
  381. cpupri_cleanup(&rd->cpupri);
  382. cpudl_cleanup(&rd->cpudl);
  383. free_cpumask_var(rd->dlo_mask);
  384. free_cpumask_var(rd->rto_mask);
  385. free_cpumask_var(rd->online);
  386. free_cpumask_var(rd->span);
  387. free_pd(rd->pd);
  388. kfree(rd);
  389. }
  390. void rq_attach_root(struct rq *rq, struct root_domain *rd)
  391. {
  392. struct root_domain *old_rd = NULL;
  393. unsigned long flags;
  394. raw_spin_rq_lock_irqsave(rq, flags);
  395. if (rq->rd) {
  396. old_rd = rq->rd;
  397. if (cpumask_test_cpu(rq->cpu, old_rd->online))
  398. set_rq_offline(rq);
  399. cpumask_clear_cpu(rq->cpu, old_rd->span);
  400. /*
  401. * If we dont want to free the old_rd yet then
  402. * set old_rd to NULL to skip the freeing later
  403. * in this function:
  404. */
  405. if (!atomic_dec_and_test(&old_rd->refcount))
  406. old_rd = NULL;
  407. }
  408. atomic_inc(&rd->refcount);
  409. rq->rd = rd;
  410. cpumask_set_cpu(rq->cpu, rd->span);
  411. if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
  412. set_rq_online(rq);
  413. raw_spin_rq_unlock_irqrestore(rq, flags);
  414. if (old_rd)
  415. call_rcu(&old_rd->rcu, free_rootdomain);
  416. }
  417. void sched_get_rd(struct root_domain *rd)
  418. {
  419. atomic_inc(&rd->refcount);
  420. }
  421. void sched_put_rd(struct root_domain *rd)
  422. {
  423. if (!atomic_dec_and_test(&rd->refcount))
  424. return;
  425. call_rcu(&rd->rcu, free_rootdomain);
  426. }
  427. static int init_rootdomain(struct root_domain *rd)
  428. {
  429. if (!zalloc_cpumask_var(&rd->span, GFP_KERNEL))
  430. goto out;
  431. if (!zalloc_cpumask_var(&rd->online, GFP_KERNEL))
  432. goto free_span;
  433. if (!zalloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL))
  434. goto free_online;
  435. if (!zalloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
  436. goto free_dlo_mask;
  437. #ifdef HAVE_RT_PUSH_IPI
  438. rd->rto_cpu = -1;
  439. raw_spin_lock_init(&rd->rto_lock);
  440. rd->rto_push_work = IRQ_WORK_INIT_HARD(rto_push_irq_work_func);
  441. #endif
  442. rd->visit_gen = 0;
  443. init_dl_bw(&rd->dl_bw);
  444. if (cpudl_init(&rd->cpudl) != 0)
  445. goto free_rto_mask;
  446. if (cpupri_init(&rd->cpupri) != 0)
  447. goto free_cpudl;
  448. return 0;
  449. free_cpudl:
  450. cpudl_cleanup(&rd->cpudl);
  451. free_rto_mask:
  452. free_cpumask_var(rd->rto_mask);
  453. free_dlo_mask:
  454. free_cpumask_var(rd->dlo_mask);
  455. free_online:
  456. free_cpumask_var(rd->online);
  457. free_span:
  458. free_cpumask_var(rd->span);
  459. out:
  460. return -ENOMEM;
  461. }
  462. /*
  463. * By default the system creates a single root-domain with all CPUs as
  464. * members (mimicking the global state we have today).
  465. */
  466. struct root_domain def_root_domain;
  467. void init_defrootdomain(void)
  468. {
  469. init_rootdomain(&def_root_domain);
  470. atomic_set(&def_root_domain.refcount, 1);
  471. }
  472. static struct root_domain *alloc_rootdomain(void)
  473. {
  474. struct root_domain *rd;
  475. rd = kzalloc(sizeof(*rd), GFP_KERNEL);
  476. if (!rd)
  477. return NULL;
  478. if (init_rootdomain(rd) != 0) {
  479. kfree(rd);
  480. return NULL;
  481. }
  482. return rd;
  483. }
  484. static void free_sched_groups(struct sched_group *sg, int free_sgc)
  485. {
  486. struct sched_group *tmp, *first;
  487. if (!sg)
  488. return;
  489. first = sg;
  490. do {
  491. tmp = sg->next;
  492. if (free_sgc && atomic_dec_and_test(&sg->sgc->ref))
  493. kfree(sg->sgc);
  494. if (atomic_dec_and_test(&sg->ref))
  495. kfree(sg);
  496. sg = tmp;
  497. } while (sg != first);
  498. }
  499. static void destroy_sched_domain(struct sched_domain *sd)
  500. {
  501. /*
  502. * A normal sched domain may have multiple group references, an
  503. * overlapping domain, having private groups, only one. Iterate,
  504. * dropping group/capacity references, freeing where none remain.
  505. */
  506. free_sched_groups(sd->groups, 1);
  507. if (sd->shared && atomic_dec_and_test(&sd->shared->ref))
  508. kfree(sd->shared);
  509. kfree(sd);
  510. }
  511. static void destroy_sched_domains_rcu(struct rcu_head *rcu)
  512. {
  513. struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
  514. while (sd) {
  515. struct sched_domain *parent = sd->parent;
  516. destroy_sched_domain(sd);
  517. sd = parent;
  518. }
  519. }
  520. static void destroy_sched_domains(struct sched_domain *sd)
  521. {
  522. if (sd)
  523. call_rcu(&sd->rcu, destroy_sched_domains_rcu);
  524. }
  525. /*
  526. * Keep a special pointer to the highest sched_domain that has
  527. * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
  528. * allows us to avoid some pointer chasing select_idle_sibling().
  529. *
  530. * Also keep a unique ID per domain (we use the first CPU number in
  531. * the cpumask of the domain), this allows us to quickly tell if
  532. * two CPUs are in the same cache domain, see cpus_share_cache().
  533. */
  534. DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc);
  535. DEFINE_PER_CPU(int, sd_llc_size);
  536. DEFINE_PER_CPU(int, sd_llc_id);
  537. DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
  538. DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa);
  539. DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
  540. DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
  541. DEFINE_STATIC_KEY_FALSE(sched_asym_cpucapacity);
  542. static void update_top_cache_domain(int cpu)
  543. {
  544. struct sched_domain_shared *sds = NULL;
  545. struct sched_domain *sd;
  546. int id = cpu;
  547. int size = 1;
  548. sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
  549. if (sd) {
  550. id = cpumask_first(sched_domain_span(sd));
  551. size = cpumask_weight(sched_domain_span(sd));
  552. sds = sd->shared;
  553. }
  554. rcu_assign_pointer(per_cpu(sd_llc, cpu), sd);
  555. per_cpu(sd_llc_size, cpu) = size;
  556. per_cpu(sd_llc_id, cpu) = id;
  557. rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
  558. sd = lowest_flag_domain(cpu, SD_NUMA);
  559. rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
  560. sd = highest_flag_domain(cpu, SD_ASYM_PACKING);
  561. rcu_assign_pointer(per_cpu(sd_asym_packing, cpu), sd);
  562. sd = lowest_flag_domain(cpu, SD_ASYM_CPUCAPACITY_FULL);
  563. rcu_assign_pointer(per_cpu(sd_asym_cpucapacity, cpu), sd);
  564. }
  565. /*
  566. * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
  567. * hold the hotplug lock.
  568. */
  569. static void
  570. cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
  571. {
  572. struct rq *rq = cpu_rq(cpu);
  573. struct sched_domain *tmp;
  574. /* Remove the sched domains which do not contribute to scheduling. */
  575. for (tmp = sd; tmp; ) {
  576. struct sched_domain *parent = tmp->parent;
  577. if (!parent)
  578. break;
  579. if (sd_parent_degenerate(tmp, parent)) {
  580. tmp->parent = parent->parent;
  581. if (parent->parent)
  582. parent->parent->child = tmp;
  583. /*
  584. * Transfer SD_PREFER_SIBLING down in case of a
  585. * degenerate parent; the spans match for this
  586. * so the property transfers.
  587. */
  588. if (parent->flags & SD_PREFER_SIBLING)
  589. tmp->flags |= SD_PREFER_SIBLING;
  590. destroy_sched_domain(parent);
  591. } else
  592. tmp = tmp->parent;
  593. }
  594. if (sd && sd_degenerate(sd)) {
  595. tmp = sd;
  596. sd = sd->parent;
  597. destroy_sched_domain(tmp);
  598. if (sd) {
  599. struct sched_group *sg = sd->groups;
  600. /*
  601. * sched groups hold the flags of the child sched
  602. * domain for convenience. Clear such flags since
  603. * the child is being destroyed.
  604. */
  605. do {
  606. sg->flags = 0;
  607. } while (sg != sd->groups);
  608. sd->child = NULL;
  609. }
  610. }
  611. sched_domain_debug(sd, cpu);
  612. rq_attach_root(rq, rd);
  613. tmp = rq->sd;
  614. rcu_assign_pointer(rq->sd, sd);
  615. dirty_sched_domain_sysctl(cpu);
  616. destroy_sched_domains(tmp);
  617. update_top_cache_domain(cpu);
  618. }
  619. struct s_data {
  620. struct sched_domain * __percpu *sd;
  621. struct root_domain *rd;
  622. };
  623. enum s_alloc {
  624. sa_rootdomain,
  625. sa_sd,
  626. sa_sd_storage,
  627. sa_none,
  628. };
  629. /*
  630. * Return the canonical balance CPU for this group, this is the first CPU
  631. * of this group that's also in the balance mask.
  632. *
  633. * The balance mask are all those CPUs that could actually end up at this
  634. * group. See build_balance_mask().
  635. *
  636. * Also see should_we_balance().
  637. */
  638. int group_balance_cpu(struct sched_group *sg)
  639. {
  640. return cpumask_first(group_balance_mask(sg));
  641. }
  642. /*
  643. * NUMA topology (first read the regular topology blurb below)
  644. *
  645. * Given a node-distance table, for example:
  646. *
  647. * node 0 1 2 3
  648. * 0: 10 20 30 20
  649. * 1: 20 10 20 30
  650. * 2: 30 20 10 20
  651. * 3: 20 30 20 10
  652. *
  653. * which represents a 4 node ring topology like:
  654. *
  655. * 0 ----- 1
  656. * | |
  657. * | |
  658. * | |
  659. * 3 ----- 2
  660. *
  661. * We want to construct domains and groups to represent this. The way we go
  662. * about doing this is to build the domains on 'hops'. For each NUMA level we
  663. * construct the mask of all nodes reachable in @level hops.
  664. *
  665. * For the above NUMA topology that gives 3 levels:
  666. *
  667. * NUMA-2 0-3 0-3 0-3 0-3
  668. * groups: {0-1,3},{1-3} {0-2},{0,2-3} {1-3},{0-1,3} {0,2-3},{0-2}
  669. *
  670. * NUMA-1 0-1,3 0-2 1-3 0,2-3
  671. * groups: {0},{1},{3} {0},{1},{2} {1},{2},{3} {0},{2},{3}
  672. *
  673. * NUMA-0 0 1 2 3
  674. *
  675. *
  676. * As can be seen; things don't nicely line up as with the regular topology.
  677. * When we iterate a domain in child domain chunks some nodes can be
  678. * represented multiple times -- hence the "overlap" naming for this part of
  679. * the topology.
  680. *
  681. * In order to minimize this overlap, we only build enough groups to cover the
  682. * domain. For instance Node-0 NUMA-2 would only get groups: 0-1,3 and 1-3.
  683. *
  684. * Because:
  685. *
  686. * - the first group of each domain is its child domain; this
  687. * gets us the first 0-1,3
  688. * - the only uncovered node is 2, who's child domain is 1-3.
  689. *
  690. * However, because of the overlap, computing a unique CPU for each group is
  691. * more complicated. Consider for instance the groups of NODE-1 NUMA-2, both
  692. * groups include the CPUs of Node-0, while those CPUs would not in fact ever
  693. * end up at those groups (they would end up in group: 0-1,3).
  694. *
  695. * To correct this we have to introduce the group balance mask. This mask
  696. * will contain those CPUs in the group that can reach this group given the
  697. * (child) domain tree.
  698. *
  699. * With this we can once again compute balance_cpu and sched_group_capacity
  700. * relations.
  701. *
  702. * XXX include words on how balance_cpu is unique and therefore can be
  703. * used for sched_group_capacity links.
  704. *
  705. *
  706. * Another 'interesting' topology is:
  707. *
  708. * node 0 1 2 3
  709. * 0: 10 20 20 30
  710. * 1: 20 10 20 20
  711. * 2: 20 20 10 20
  712. * 3: 30 20 20 10
  713. *
  714. * Which looks a little like:
  715. *
  716. * 0 ----- 1
  717. * | / |
  718. * | / |
  719. * | / |
  720. * 2 ----- 3
  721. *
  722. * This topology is asymmetric, nodes 1,2 are fully connected, but nodes 0,3
  723. * are not.
  724. *
  725. * This leads to a few particularly weird cases where the sched_domain's are
  726. * not of the same number for each CPU. Consider:
  727. *
  728. * NUMA-2 0-3 0-3
  729. * groups: {0-2},{1-3} {1-3},{0-2}
  730. *
  731. * NUMA-1 0-2 0-3 0-3 1-3
  732. *
  733. * NUMA-0 0 1 2 3
  734. *
  735. */
  736. /*
  737. * Build the balance mask; it contains only those CPUs that can arrive at this
  738. * group and should be considered to continue balancing.
  739. *
  740. * We do this during the group creation pass, therefore the group information
  741. * isn't complete yet, however since each group represents a (child) domain we
  742. * can fully construct this using the sched_domain bits (which are already
  743. * complete).
  744. */
  745. static void
  746. build_balance_mask(struct sched_domain *sd, struct sched_group *sg, struct cpumask *mask)
  747. {
  748. const struct cpumask *sg_span = sched_group_span(sg);
  749. struct sd_data *sdd = sd->private;
  750. struct sched_domain *sibling;
  751. int i;
  752. cpumask_clear(mask);
  753. for_each_cpu(i, sg_span) {
  754. sibling = *per_cpu_ptr(sdd->sd, i);
  755. /*
  756. * Can happen in the asymmetric case, where these siblings are
  757. * unused. The mask will not be empty because those CPUs that
  758. * do have the top domain _should_ span the domain.
  759. */
  760. if (!sibling->child)
  761. continue;
  762. /* If we would not end up here, we can't continue from here */
  763. if (!cpumask_equal(sg_span, sched_domain_span(sibling->child)))
  764. continue;
  765. cpumask_set_cpu(i, mask);
  766. }
  767. /* We must not have empty masks here */
  768. WARN_ON_ONCE(cpumask_empty(mask));
  769. }
  770. /*
  771. * XXX: This creates per-node group entries; since the load-balancer will
  772. * immediately access remote memory to construct this group's load-balance
  773. * statistics having the groups node local is of dubious benefit.
  774. */
  775. static struct sched_group *
  776. build_group_from_child_sched_domain(struct sched_domain *sd, int cpu)
  777. {
  778. struct sched_group *sg;
  779. struct cpumask *sg_span;
  780. sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
  781. GFP_KERNEL, cpu_to_node(cpu));
  782. if (!sg)
  783. return NULL;
  784. sg_span = sched_group_span(sg);
  785. if (sd->child) {
  786. cpumask_copy(sg_span, sched_domain_span(sd->child));
  787. sg->flags = sd->child->flags;
  788. } else {
  789. cpumask_copy(sg_span, sched_domain_span(sd));
  790. }
  791. atomic_inc(&sg->ref);
  792. return sg;
  793. }
  794. static void init_overlap_sched_group(struct sched_domain *sd,
  795. struct sched_group *sg)
  796. {
  797. struct cpumask *mask = sched_domains_tmpmask2;
  798. struct sd_data *sdd = sd->private;
  799. struct cpumask *sg_span;
  800. int cpu;
  801. build_balance_mask(sd, sg, mask);
  802. cpu = cpumask_first(mask);
  803. sg->sgc = *per_cpu_ptr(sdd->sgc, cpu);
  804. if (atomic_inc_return(&sg->sgc->ref) == 1)
  805. cpumask_copy(group_balance_mask(sg), mask);
  806. else
  807. WARN_ON_ONCE(!cpumask_equal(group_balance_mask(sg), mask));
  808. /*
  809. * Initialize sgc->capacity such that even if we mess up the
  810. * domains and no possible iteration will get us here, we won't
  811. * die on a /0 trap.
  812. */
  813. sg_span = sched_group_span(sg);
  814. sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
  815. sg->sgc->min_capacity = SCHED_CAPACITY_SCALE;
  816. sg->sgc->max_capacity = SCHED_CAPACITY_SCALE;
  817. }
  818. static struct sched_domain *
  819. find_descended_sibling(struct sched_domain *sd, struct sched_domain *sibling)
  820. {
  821. /*
  822. * The proper descendant would be the one whose child won't span out
  823. * of sd
  824. */
  825. while (sibling->child &&
  826. !cpumask_subset(sched_domain_span(sibling->child),
  827. sched_domain_span(sd)))
  828. sibling = sibling->child;
  829. /*
  830. * As we are referencing sgc across different topology level, we need
  831. * to go down to skip those sched_domains which don't contribute to
  832. * scheduling because they will be degenerated in cpu_attach_domain
  833. */
  834. while (sibling->child &&
  835. cpumask_equal(sched_domain_span(sibling->child),
  836. sched_domain_span(sibling)))
  837. sibling = sibling->child;
  838. return sibling;
  839. }
  840. static int
  841. build_overlap_sched_groups(struct sched_domain *sd, int cpu)
  842. {
  843. struct sched_group *first = NULL, *last = NULL, *sg;
  844. const struct cpumask *span = sched_domain_span(sd);
  845. struct cpumask *covered = sched_domains_tmpmask;
  846. struct sd_data *sdd = sd->private;
  847. struct sched_domain *sibling;
  848. int i;
  849. cpumask_clear(covered);
  850. for_each_cpu_wrap(i, span, cpu) {
  851. struct cpumask *sg_span;
  852. if (cpumask_test_cpu(i, covered))
  853. continue;
  854. sibling = *per_cpu_ptr(sdd->sd, i);
  855. /*
  856. * Asymmetric node setups can result in situations where the
  857. * domain tree is of unequal depth, make sure to skip domains
  858. * that already cover the entire range.
  859. *
  860. * In that case build_sched_domains() will have terminated the
  861. * iteration early and our sibling sd spans will be empty.
  862. * Domains should always include the CPU they're built on, so
  863. * check that.
  864. */
  865. if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
  866. continue;
  867. /*
  868. * Usually we build sched_group by sibling's child sched_domain
  869. * But for machines whose NUMA diameter are 3 or above, we move
  870. * to build sched_group by sibling's proper descendant's child
  871. * domain because sibling's child sched_domain will span out of
  872. * the sched_domain being built as below.
  873. *
  874. * Smallest diameter=3 topology is:
  875. *
  876. * node 0 1 2 3
  877. * 0: 10 20 30 40
  878. * 1: 20 10 20 30
  879. * 2: 30 20 10 20
  880. * 3: 40 30 20 10
  881. *
  882. * 0 --- 1 --- 2 --- 3
  883. *
  884. * NUMA-3 0-3 N/A N/A 0-3
  885. * groups: {0-2},{1-3} {1-3},{0-2}
  886. *
  887. * NUMA-2 0-2 0-3 0-3 1-3
  888. * groups: {0-1},{1-3} {0-2},{2-3} {1-3},{0-1} {2-3},{0-2}
  889. *
  890. * NUMA-1 0-1 0-2 1-3 2-3
  891. * groups: {0},{1} {1},{2},{0} {2},{3},{1} {3},{2}
  892. *
  893. * NUMA-0 0 1 2 3
  894. *
  895. * The NUMA-2 groups for nodes 0 and 3 are obviously buggered, as the
  896. * group span isn't a subset of the domain span.
  897. */
  898. if (sibling->child &&
  899. !cpumask_subset(sched_domain_span(sibling->child), span))
  900. sibling = find_descended_sibling(sd, sibling);
  901. sg = build_group_from_child_sched_domain(sibling, cpu);
  902. if (!sg)
  903. goto fail;
  904. sg_span = sched_group_span(sg);
  905. cpumask_or(covered, covered, sg_span);
  906. init_overlap_sched_group(sibling, sg);
  907. if (!first)
  908. first = sg;
  909. if (last)
  910. last->next = sg;
  911. last = sg;
  912. last->next = first;
  913. }
  914. sd->groups = first;
  915. return 0;
  916. fail:
  917. free_sched_groups(first, 0);
  918. return -ENOMEM;
  919. }
  920. /*
  921. * Package topology (also see the load-balance blurb in fair.c)
  922. *
  923. * The scheduler builds a tree structure to represent a number of important
  924. * topology features. By default (default_topology[]) these include:
  925. *
  926. * - Simultaneous multithreading (SMT)
  927. * - Multi-Core Cache (MC)
  928. * - Package (DIE)
  929. *
  930. * Where the last one more or less denotes everything up to a NUMA node.
  931. *
  932. * The tree consists of 3 primary data structures:
  933. *
  934. * sched_domain -> sched_group -> sched_group_capacity
  935. * ^ ^ ^ ^
  936. * `-' `-'
  937. *
  938. * The sched_domains are per-CPU and have a two way link (parent & child) and
  939. * denote the ever growing mask of CPUs belonging to that level of topology.
  940. *
  941. * Each sched_domain has a circular (double) linked list of sched_group's, each
  942. * denoting the domains of the level below (or individual CPUs in case of the
  943. * first domain level). The sched_group linked by a sched_domain includes the
  944. * CPU of that sched_domain [*].
  945. *
  946. * Take for instance a 2 threaded, 2 core, 2 cache cluster part:
  947. *
  948. * CPU 0 1 2 3 4 5 6 7
  949. *
  950. * DIE [ ]
  951. * MC [ ] [ ]
  952. * SMT [ ] [ ] [ ] [ ]
  953. *
  954. * - or -
  955. *
  956. * DIE 0-7 0-7 0-7 0-7 0-7 0-7 0-7 0-7
  957. * MC 0-3 0-3 0-3 0-3 4-7 4-7 4-7 4-7
  958. * SMT 0-1 0-1 2-3 2-3 4-5 4-5 6-7 6-7
  959. *
  960. * CPU 0 1 2 3 4 5 6 7
  961. *
  962. * One way to think about it is: sched_domain moves you up and down among these
  963. * topology levels, while sched_group moves you sideways through it, at child
  964. * domain granularity.
  965. *
  966. * sched_group_capacity ensures each unique sched_group has shared storage.
  967. *
  968. * There are two related construction problems, both require a CPU that
  969. * uniquely identify each group (for a given domain):
  970. *
  971. * - The first is the balance_cpu (see should_we_balance() and the
  972. * load-balance blub in fair.c); for each group we only want 1 CPU to
  973. * continue balancing at a higher domain.
  974. *
  975. * - The second is the sched_group_capacity; we want all identical groups
  976. * to share a single sched_group_capacity.
  977. *
  978. * Since these topologies are exclusive by construction. That is, its
  979. * impossible for an SMT thread to belong to multiple cores, and cores to
  980. * be part of multiple caches. There is a very clear and unique location
  981. * for each CPU in the hierarchy.
  982. *
  983. * Therefore computing a unique CPU for each group is trivial (the iteration
  984. * mask is redundant and set all 1s; all CPUs in a group will end up at _that_
  985. * group), we can simply pick the first CPU in each group.
  986. *
  987. *
  988. * [*] in other words, the first group of each domain is its child domain.
  989. */
  990. static struct sched_group *get_group(int cpu, struct sd_data *sdd)
  991. {
  992. struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
  993. struct sched_domain *child = sd->child;
  994. struct sched_group *sg;
  995. bool already_visited;
  996. if (child)
  997. cpu = cpumask_first(sched_domain_span(child));
  998. sg = *per_cpu_ptr(sdd->sg, cpu);
  999. sg->sgc = *per_cpu_ptr(sdd->sgc, cpu);
  1000. /* Increase refcounts for claim_allocations: */
  1001. already_visited = atomic_inc_return(&sg->ref) > 1;
  1002. /* sgc visits should follow a similar trend as sg */
  1003. WARN_ON(already_visited != (atomic_inc_return(&sg->sgc->ref) > 1));
  1004. /* If we have already visited that group, it's already initialized. */
  1005. if (already_visited)
  1006. return sg;
  1007. if (child) {
  1008. cpumask_copy(sched_group_span(sg), sched_domain_span(child));
  1009. cpumask_copy(group_balance_mask(sg), sched_group_span(sg));
  1010. sg->flags = child->flags;
  1011. } else {
  1012. cpumask_set_cpu(cpu, sched_group_span(sg));
  1013. cpumask_set_cpu(cpu, group_balance_mask(sg));
  1014. }
  1015. sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sched_group_span(sg));
  1016. sg->sgc->min_capacity = SCHED_CAPACITY_SCALE;
  1017. sg->sgc->max_capacity = SCHED_CAPACITY_SCALE;
  1018. return sg;
  1019. }
  1020. /*
  1021. * build_sched_groups will build a circular linked list of the groups
  1022. * covered by the given span, will set each group's ->cpumask correctly,
  1023. * and will initialize their ->sgc.
  1024. *
  1025. * Assumes the sched_domain tree is fully constructed
  1026. */
  1027. static int
  1028. build_sched_groups(struct sched_domain *sd, int cpu)
  1029. {
  1030. struct sched_group *first = NULL, *last = NULL;
  1031. struct sd_data *sdd = sd->private;
  1032. const struct cpumask *span = sched_domain_span(sd);
  1033. struct cpumask *covered;
  1034. int i;
  1035. lockdep_assert_held(&sched_domains_mutex);
  1036. covered = sched_domains_tmpmask;
  1037. cpumask_clear(covered);
  1038. for_each_cpu_wrap(i, span, cpu) {
  1039. struct sched_group *sg;
  1040. if (cpumask_test_cpu(i, covered))
  1041. continue;
  1042. sg = get_group(i, sdd);
  1043. cpumask_or(covered, covered, sched_group_span(sg));
  1044. if (!first)
  1045. first = sg;
  1046. if (last)
  1047. last->next = sg;
  1048. last = sg;
  1049. }
  1050. last->next = first;
  1051. sd->groups = first;
  1052. return 0;
  1053. }
  1054. /*
  1055. * Initialize sched groups cpu_capacity.
  1056. *
  1057. * cpu_capacity indicates the capacity of sched group, which is used while
  1058. * distributing the load between different sched groups in a sched domain.
  1059. * Typically cpu_capacity for all the groups in a sched domain will be same
  1060. * unless there are asymmetries in the topology. If there are asymmetries,
  1061. * group having more cpu_capacity will pickup more load compared to the
  1062. * group having less cpu_capacity.
  1063. */
  1064. static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
  1065. {
  1066. struct sched_group *sg = sd->groups;
  1067. WARN_ON(!sg);
  1068. do {
  1069. int cpu, max_cpu = -1;
  1070. sg->group_weight = cpumask_weight(sched_group_span(sg));
  1071. if (!(sd->flags & SD_ASYM_PACKING))
  1072. goto next;
  1073. for_each_cpu(cpu, sched_group_span(sg)) {
  1074. if (max_cpu < 0)
  1075. max_cpu = cpu;
  1076. else if (sched_asym_prefer(cpu, max_cpu))
  1077. max_cpu = cpu;
  1078. }
  1079. sg->asym_prefer_cpu = max_cpu;
  1080. next:
  1081. sg = sg->next;
  1082. } while (sg != sd->groups);
  1083. if (cpu != group_balance_cpu(sg))
  1084. return;
  1085. update_group_capacity(sd, cpu);
  1086. }
  1087. /*
  1088. * Asymmetric CPU capacity bits
  1089. */
  1090. struct asym_cap_data {
  1091. struct list_head link;
  1092. unsigned long capacity;
  1093. unsigned long cpus[];
  1094. };
  1095. /*
  1096. * Set of available CPUs grouped by their corresponding capacities
  1097. * Each list entry contains a CPU mask reflecting CPUs that share the same
  1098. * capacity.
  1099. * The lifespan of data is unlimited.
  1100. */
  1101. static LIST_HEAD(asym_cap_list);
  1102. #define cpu_capacity_span(asym_data) to_cpumask((asym_data)->cpus)
  1103. /*
  1104. * Verify whether there is any CPU capacity asymmetry in a given sched domain.
  1105. * Provides sd_flags reflecting the asymmetry scope.
  1106. */
  1107. static inline int
  1108. asym_cpu_capacity_classify(const struct cpumask *sd_span,
  1109. const struct cpumask *cpu_map)
  1110. {
  1111. struct asym_cap_data *entry;
  1112. int count = 0, miss = 0;
  1113. /*
  1114. * Count how many unique CPU capacities this domain spans across
  1115. * (compare sched_domain CPUs mask with ones representing available
  1116. * CPUs capacities). Take into account CPUs that might be offline:
  1117. * skip those.
  1118. */
  1119. list_for_each_entry(entry, &asym_cap_list, link) {
  1120. if (cpumask_intersects(sd_span, cpu_capacity_span(entry)))
  1121. ++count;
  1122. else if (cpumask_intersects(cpu_map, cpu_capacity_span(entry)))
  1123. ++miss;
  1124. }
  1125. WARN_ON_ONCE(!count && !list_empty(&asym_cap_list));
  1126. /* No asymmetry detected */
  1127. if (count < 2)
  1128. return 0;
  1129. /* Some of the available CPU capacity values have not been detected */
  1130. if (miss)
  1131. return SD_ASYM_CPUCAPACITY;
  1132. /* Full asymmetry */
  1133. return SD_ASYM_CPUCAPACITY | SD_ASYM_CPUCAPACITY_FULL;
  1134. }
  1135. static inline void asym_cpu_capacity_update_data(int cpu)
  1136. {
  1137. unsigned long capacity = arch_scale_cpu_capacity(cpu);
  1138. struct asym_cap_data *entry = NULL;
  1139. list_for_each_entry(entry, &asym_cap_list, link) {
  1140. if (capacity == entry->capacity)
  1141. goto done;
  1142. }
  1143. entry = kzalloc(sizeof(*entry) + cpumask_size(), GFP_KERNEL);
  1144. if (WARN_ONCE(!entry, "Failed to allocate memory for asymmetry data\n"))
  1145. return;
  1146. entry->capacity = capacity;
  1147. list_add(&entry->link, &asym_cap_list);
  1148. done:
  1149. __cpumask_set_cpu(cpu, cpu_capacity_span(entry));
  1150. }
  1151. /*
  1152. * Build-up/update list of CPUs grouped by their capacities
  1153. * An update requires explicit request to rebuild sched domains
  1154. * with state indicating CPU topology changes.
  1155. */
  1156. static void asym_cpu_capacity_scan(void)
  1157. {
  1158. struct asym_cap_data *entry, *next;
  1159. int cpu;
  1160. list_for_each_entry(entry, &asym_cap_list, link)
  1161. cpumask_clear(cpu_capacity_span(entry));
  1162. for_each_cpu_and(cpu, cpu_possible_mask, housekeeping_cpumask(HK_TYPE_DOMAIN))
  1163. asym_cpu_capacity_update_data(cpu);
  1164. list_for_each_entry_safe(entry, next, &asym_cap_list, link) {
  1165. if (cpumask_empty(cpu_capacity_span(entry))) {
  1166. list_del(&entry->link);
  1167. kfree(entry);
  1168. }
  1169. }
  1170. /*
  1171. * Only one capacity value has been detected i.e. this system is symmetric.
  1172. * No need to keep this data around.
  1173. */
  1174. if (list_is_singular(&asym_cap_list)) {
  1175. entry = list_first_entry(&asym_cap_list, typeof(*entry), link);
  1176. list_del(&entry->link);
  1177. kfree(entry);
  1178. }
  1179. }
  1180. /*
  1181. * Initializers for schedule domains
  1182. * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
  1183. */
  1184. static int default_relax_domain_level = -1;
  1185. int sched_domain_level_max;
  1186. static int __init setup_relax_domain_level(char *str)
  1187. {
  1188. if (kstrtoint(str, 0, &default_relax_domain_level))
  1189. pr_warn("Unable to set relax_domain_level\n");
  1190. return 1;
  1191. }
  1192. __setup("relax_domain_level=", setup_relax_domain_level);
  1193. static void set_domain_attribute(struct sched_domain *sd,
  1194. struct sched_domain_attr *attr)
  1195. {
  1196. int request;
  1197. if (!attr || attr->relax_domain_level < 0) {
  1198. if (default_relax_domain_level < 0)
  1199. return;
  1200. request = default_relax_domain_level;
  1201. } else
  1202. request = attr->relax_domain_level;
  1203. if (sd->level > request) {
  1204. /* Turn off idle balance on this domain: */
  1205. sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
  1206. }
  1207. }
  1208. static void __sdt_free(const struct cpumask *cpu_map);
  1209. static int __sdt_alloc(const struct cpumask *cpu_map);
  1210. static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
  1211. const struct cpumask *cpu_map)
  1212. {
  1213. switch (what) {
  1214. case sa_rootdomain:
  1215. if (!atomic_read(&d->rd->refcount))
  1216. free_rootdomain(&d->rd->rcu);
  1217. fallthrough;
  1218. case sa_sd:
  1219. free_percpu(d->sd);
  1220. fallthrough;
  1221. case sa_sd_storage:
  1222. __sdt_free(cpu_map);
  1223. fallthrough;
  1224. case sa_none:
  1225. break;
  1226. }
  1227. }
  1228. static enum s_alloc
  1229. __visit_domain_allocation_hell(struct s_data *d, const struct cpumask *cpu_map)
  1230. {
  1231. memset(d, 0, sizeof(*d));
  1232. if (__sdt_alloc(cpu_map))
  1233. return sa_sd_storage;
  1234. d->sd = alloc_percpu(struct sched_domain *);
  1235. if (!d->sd)
  1236. return sa_sd_storage;
  1237. d->rd = alloc_rootdomain();
  1238. if (!d->rd)
  1239. return sa_sd;
  1240. return sa_rootdomain;
  1241. }
  1242. /*
  1243. * NULL the sd_data elements we've used to build the sched_domain and
  1244. * sched_group structure so that the subsequent __free_domain_allocs()
  1245. * will not free the data we're using.
  1246. */
  1247. static void claim_allocations(int cpu, struct sched_domain *sd)
  1248. {
  1249. struct sd_data *sdd = sd->private;
  1250. WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
  1251. *per_cpu_ptr(sdd->sd, cpu) = NULL;
  1252. if (atomic_read(&(*per_cpu_ptr(sdd->sds, cpu))->ref))
  1253. *per_cpu_ptr(sdd->sds, cpu) = NULL;
  1254. if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
  1255. *per_cpu_ptr(sdd->sg, cpu) = NULL;
  1256. if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
  1257. *per_cpu_ptr(sdd->sgc, cpu) = NULL;
  1258. }
  1259. #ifdef CONFIG_NUMA
  1260. enum numa_topology_type sched_numa_topology_type;
  1261. static int sched_domains_numa_levels;
  1262. static int sched_domains_curr_level;
  1263. int sched_max_numa_distance;
  1264. static int *sched_domains_numa_distance;
  1265. static struct cpumask ***sched_domains_numa_masks;
  1266. #endif
  1267. /*
  1268. * SD_flags allowed in topology descriptions.
  1269. *
  1270. * These flags are purely descriptive of the topology and do not prescribe
  1271. * behaviour. Behaviour is artificial and mapped in the below sd_init()
  1272. * function:
  1273. *
  1274. * SD_SHARE_CPUCAPACITY - describes SMT topologies
  1275. * SD_SHARE_PKG_RESOURCES - describes shared caches
  1276. * SD_NUMA - describes NUMA topologies
  1277. *
  1278. * Odd one out, which beside describing the topology has a quirk also
  1279. * prescribes the desired behaviour that goes along with it:
  1280. *
  1281. * SD_ASYM_PACKING - describes SMT quirks
  1282. */
  1283. #define TOPOLOGY_SD_FLAGS \
  1284. (SD_SHARE_CPUCAPACITY | \
  1285. SD_SHARE_PKG_RESOURCES | \
  1286. SD_NUMA | \
  1287. SD_ASYM_PACKING)
  1288. static struct sched_domain *
  1289. sd_init(struct sched_domain_topology_level *tl,
  1290. const struct cpumask *cpu_map,
  1291. struct sched_domain *child, int cpu)
  1292. {
  1293. struct sd_data *sdd = &tl->data;
  1294. struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
  1295. int sd_id, sd_weight, sd_flags = 0;
  1296. struct cpumask *sd_span;
  1297. #ifdef CONFIG_NUMA
  1298. /*
  1299. * Ugly hack to pass state to sd_numa_mask()...
  1300. */
  1301. sched_domains_curr_level = tl->numa_level;
  1302. #endif
  1303. sd_weight = cpumask_weight(tl->mask(cpu));
  1304. if (tl->sd_flags)
  1305. sd_flags = (*tl->sd_flags)();
  1306. if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS,
  1307. "wrong sd_flags in topology description\n"))
  1308. sd_flags &= TOPOLOGY_SD_FLAGS;
  1309. *sd = (struct sched_domain){
  1310. .min_interval = sd_weight,
  1311. .max_interval = 2*sd_weight,
  1312. .busy_factor = 16,
  1313. .imbalance_pct = 117,
  1314. .cache_nice_tries = 0,
  1315. .flags = 1*SD_BALANCE_NEWIDLE
  1316. | 1*SD_BALANCE_EXEC
  1317. | 1*SD_BALANCE_FORK
  1318. | 0*SD_BALANCE_WAKE
  1319. | 1*SD_WAKE_AFFINE
  1320. | 0*SD_SHARE_CPUCAPACITY
  1321. | 0*SD_SHARE_PKG_RESOURCES
  1322. | 0*SD_SERIALIZE
  1323. | 1*SD_PREFER_SIBLING
  1324. | 0*SD_NUMA
  1325. | sd_flags
  1326. ,
  1327. .last_balance = jiffies,
  1328. .balance_interval = sd_weight,
  1329. .max_newidle_lb_cost = 0,
  1330. .last_decay_max_lb_cost = jiffies,
  1331. .child = child,
  1332. #ifdef CONFIG_SCHED_DEBUG
  1333. .name = tl->name,
  1334. #endif
  1335. };
  1336. sd_span = sched_domain_span(sd);
  1337. cpumask_and(sd_span, cpu_map, tl->mask(cpu));
  1338. sd_id = cpumask_first(sd_span);
  1339. sd->flags |= asym_cpu_capacity_classify(sd_span, cpu_map);
  1340. WARN_ONCE((sd->flags & (SD_SHARE_CPUCAPACITY | SD_ASYM_CPUCAPACITY)) ==
  1341. (SD_SHARE_CPUCAPACITY | SD_ASYM_CPUCAPACITY),
  1342. "CPU capacity asymmetry not supported on SMT\n");
  1343. /*
  1344. * Convert topological properties into behaviour.
  1345. */
  1346. /* Don't attempt to spread across CPUs of different capacities. */
  1347. if ((sd->flags & SD_ASYM_CPUCAPACITY) && sd->child)
  1348. sd->child->flags &= ~SD_PREFER_SIBLING;
  1349. if (sd->flags & SD_SHARE_CPUCAPACITY) {
  1350. sd->imbalance_pct = 110;
  1351. } else if (sd->flags & SD_SHARE_PKG_RESOURCES) {
  1352. sd->imbalance_pct = 117;
  1353. sd->cache_nice_tries = 1;
  1354. #ifdef CONFIG_NUMA
  1355. } else if (sd->flags & SD_NUMA) {
  1356. sd->cache_nice_tries = 2;
  1357. sd->flags &= ~SD_PREFER_SIBLING;
  1358. sd->flags |= SD_SERIALIZE;
  1359. if (sched_domains_numa_distance[tl->numa_level] > node_reclaim_distance) {
  1360. sd->flags &= ~(SD_BALANCE_EXEC |
  1361. SD_BALANCE_FORK |
  1362. SD_WAKE_AFFINE);
  1363. }
  1364. #endif
  1365. } else {
  1366. sd->cache_nice_tries = 1;
  1367. }
  1368. /*
  1369. * For all levels sharing cache; connect a sched_domain_shared
  1370. * instance.
  1371. */
  1372. if (sd->flags & SD_SHARE_PKG_RESOURCES) {
  1373. sd->shared = *per_cpu_ptr(sdd->sds, sd_id);
  1374. atomic_inc(&sd->shared->ref);
  1375. atomic_set(&sd->shared->nr_busy_cpus, sd_weight);
  1376. }
  1377. sd->private = sdd;
  1378. return sd;
  1379. }
  1380. /*
  1381. * Topology list, bottom-up.
  1382. */
  1383. static struct sched_domain_topology_level default_topology[] = {
  1384. #ifdef CONFIG_SCHED_SMT
  1385. { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
  1386. #endif
  1387. #ifdef CONFIG_SCHED_CLUSTER
  1388. { cpu_clustergroup_mask, cpu_cluster_flags, SD_INIT_NAME(CLS) },
  1389. #endif
  1390. #ifdef CONFIG_SCHED_MC
  1391. { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
  1392. #endif
  1393. { cpu_cpu_mask, SD_INIT_NAME(DIE) },
  1394. { NULL, },
  1395. };
  1396. static struct sched_domain_topology_level *sched_domain_topology =
  1397. default_topology;
  1398. static struct sched_domain_topology_level *sched_domain_topology_saved;
  1399. #define for_each_sd_topology(tl) \
  1400. for (tl = sched_domain_topology; tl->mask; tl++)
  1401. void set_sched_topology(struct sched_domain_topology_level *tl)
  1402. {
  1403. if (WARN_ON_ONCE(sched_smp_initialized))
  1404. return;
  1405. sched_domain_topology = tl;
  1406. sched_domain_topology_saved = NULL;
  1407. }
  1408. #ifdef CONFIG_NUMA
  1409. static const struct cpumask *sd_numa_mask(int cpu)
  1410. {
  1411. return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
  1412. }
  1413. static void sched_numa_warn(const char *str)
  1414. {
  1415. static int done = false;
  1416. int i,j;
  1417. if (done)
  1418. return;
  1419. done = true;
  1420. printk(KERN_WARNING "ERROR: %s\n\n", str);
  1421. for (i = 0; i < nr_node_ids; i++) {
  1422. printk(KERN_WARNING " ");
  1423. for (j = 0; j < nr_node_ids; j++) {
  1424. if (!node_state(i, N_CPU) || !node_state(j, N_CPU))
  1425. printk(KERN_CONT "(%02d) ", node_distance(i,j));
  1426. else
  1427. printk(KERN_CONT " %02d ", node_distance(i,j));
  1428. }
  1429. printk(KERN_CONT "\n");
  1430. }
  1431. printk(KERN_WARNING "\n");
  1432. }
  1433. bool find_numa_distance(int distance)
  1434. {
  1435. bool found = false;
  1436. int i, *distances;
  1437. if (distance == node_distance(0, 0))
  1438. return true;
  1439. rcu_read_lock();
  1440. distances = rcu_dereference(sched_domains_numa_distance);
  1441. if (!distances)
  1442. goto unlock;
  1443. for (i = 0; i < sched_domains_numa_levels; i++) {
  1444. if (distances[i] == distance) {
  1445. found = true;
  1446. break;
  1447. }
  1448. }
  1449. unlock:
  1450. rcu_read_unlock();
  1451. return found;
  1452. }
  1453. #define for_each_cpu_node_but(n, nbut) \
  1454. for_each_node_state(n, N_CPU) \
  1455. if (n == nbut) \
  1456. continue; \
  1457. else
  1458. /*
  1459. * A system can have three types of NUMA topology:
  1460. * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system
  1461. * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes
  1462. * NUMA_BACKPLANE: nodes can reach other nodes through a backplane
  1463. *
  1464. * The difference between a glueless mesh topology and a backplane
  1465. * topology lies in whether communication between not directly
  1466. * connected nodes goes through intermediary nodes (where programs
  1467. * could run), or through backplane controllers. This affects
  1468. * placement of programs.
  1469. *
  1470. * The type of topology can be discerned with the following tests:
  1471. * - If the maximum distance between any nodes is 1 hop, the system
  1472. * is directly connected.
  1473. * - If for two nodes A and B, located N > 1 hops away from each other,
  1474. * there is an intermediary node C, which is < N hops away from both
  1475. * nodes A and B, the system is a glueless mesh.
  1476. */
  1477. static void init_numa_topology_type(int offline_node)
  1478. {
  1479. int a, b, c, n;
  1480. n = sched_max_numa_distance;
  1481. if (sched_domains_numa_levels <= 2) {
  1482. sched_numa_topology_type = NUMA_DIRECT;
  1483. return;
  1484. }
  1485. for_each_cpu_node_but(a, offline_node) {
  1486. for_each_cpu_node_but(b, offline_node) {
  1487. /* Find two nodes furthest removed from each other. */
  1488. if (node_distance(a, b) < n)
  1489. continue;
  1490. /* Is there an intermediary node between a and b? */
  1491. for_each_cpu_node_but(c, offline_node) {
  1492. if (node_distance(a, c) < n &&
  1493. node_distance(b, c) < n) {
  1494. sched_numa_topology_type =
  1495. NUMA_GLUELESS_MESH;
  1496. return;
  1497. }
  1498. }
  1499. sched_numa_topology_type = NUMA_BACKPLANE;
  1500. return;
  1501. }
  1502. }
  1503. pr_err("Failed to find a NUMA topology type, defaulting to DIRECT\n");
  1504. sched_numa_topology_type = NUMA_DIRECT;
  1505. }
  1506. #define NR_DISTANCE_VALUES (1 << DISTANCE_BITS)
  1507. void sched_init_numa(int offline_node)
  1508. {
  1509. struct sched_domain_topology_level *tl;
  1510. unsigned long *distance_map;
  1511. int nr_levels = 0;
  1512. int i, j;
  1513. int *distances;
  1514. struct cpumask ***masks;
  1515. /*
  1516. * O(nr_nodes^2) deduplicating selection sort -- in order to find the
  1517. * unique distances in the node_distance() table.
  1518. */
  1519. distance_map = bitmap_alloc(NR_DISTANCE_VALUES, GFP_KERNEL);
  1520. if (!distance_map)
  1521. return;
  1522. bitmap_zero(distance_map, NR_DISTANCE_VALUES);
  1523. for_each_cpu_node_but(i, offline_node) {
  1524. for_each_cpu_node_but(j, offline_node) {
  1525. int distance = node_distance(i, j);
  1526. if (distance < LOCAL_DISTANCE || distance >= NR_DISTANCE_VALUES) {
  1527. sched_numa_warn("Invalid distance value range");
  1528. bitmap_free(distance_map);
  1529. return;
  1530. }
  1531. bitmap_set(distance_map, distance, 1);
  1532. }
  1533. }
  1534. /*
  1535. * We can now figure out how many unique distance values there are and
  1536. * allocate memory accordingly.
  1537. */
  1538. nr_levels = bitmap_weight(distance_map, NR_DISTANCE_VALUES);
  1539. distances = kcalloc(nr_levels, sizeof(int), GFP_KERNEL);
  1540. if (!distances) {
  1541. bitmap_free(distance_map);
  1542. return;
  1543. }
  1544. for (i = 0, j = 0; i < nr_levels; i++, j++) {
  1545. j = find_next_bit(distance_map, NR_DISTANCE_VALUES, j);
  1546. distances[i] = j;
  1547. }
  1548. rcu_assign_pointer(sched_domains_numa_distance, distances);
  1549. bitmap_free(distance_map);
  1550. /*
  1551. * 'nr_levels' contains the number of unique distances
  1552. *
  1553. * The sched_domains_numa_distance[] array includes the actual distance
  1554. * numbers.
  1555. */
  1556. /*
  1557. * Here, we should temporarily reset sched_domains_numa_levels to 0.
  1558. * If it fails to allocate memory for array sched_domains_numa_masks[][],
  1559. * the array will contain less then 'nr_levels' members. This could be
  1560. * dangerous when we use it to iterate array sched_domains_numa_masks[][]
  1561. * in other functions.
  1562. *
  1563. * We reset it to 'nr_levels' at the end of this function.
  1564. */
  1565. sched_domains_numa_levels = 0;
  1566. masks = kzalloc(sizeof(void *) * nr_levels, GFP_KERNEL);
  1567. if (!masks)
  1568. return;
  1569. /*
  1570. * Now for each level, construct a mask per node which contains all
  1571. * CPUs of nodes that are that many hops away from us.
  1572. */
  1573. for (i = 0; i < nr_levels; i++) {
  1574. masks[i] = kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
  1575. if (!masks[i])
  1576. return;
  1577. for_each_cpu_node_but(j, offline_node) {
  1578. struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
  1579. int k;
  1580. if (!mask)
  1581. return;
  1582. masks[i][j] = mask;
  1583. for_each_cpu_node_but(k, offline_node) {
  1584. if (sched_debug() && (node_distance(j, k) != node_distance(k, j)))
  1585. sched_numa_warn("Node-distance not symmetric");
  1586. if (node_distance(j, k) > sched_domains_numa_distance[i])
  1587. continue;
  1588. cpumask_or(mask, mask, cpumask_of_node(k));
  1589. }
  1590. }
  1591. }
  1592. rcu_assign_pointer(sched_domains_numa_masks, masks);
  1593. /* Compute default topology size */
  1594. for (i = 0; sched_domain_topology[i].mask; i++);
  1595. tl = kzalloc((i + nr_levels + 1) *
  1596. sizeof(struct sched_domain_topology_level), GFP_KERNEL);
  1597. if (!tl)
  1598. return;
  1599. /*
  1600. * Copy the default topology bits..
  1601. */
  1602. for (i = 0; sched_domain_topology[i].mask; i++)
  1603. tl[i] = sched_domain_topology[i];
  1604. /*
  1605. * Add the NUMA identity distance, aka single NODE.
  1606. */
  1607. tl[i++] = (struct sched_domain_topology_level){
  1608. .mask = sd_numa_mask,
  1609. .numa_level = 0,
  1610. SD_INIT_NAME(NODE)
  1611. };
  1612. /*
  1613. * .. and append 'j' levels of NUMA goodness.
  1614. */
  1615. for (j = 1; j < nr_levels; i++, j++) {
  1616. tl[i] = (struct sched_domain_topology_level){
  1617. .mask = sd_numa_mask,
  1618. .sd_flags = cpu_numa_flags,
  1619. .flags = SDTL_OVERLAP,
  1620. .numa_level = j,
  1621. SD_INIT_NAME(NUMA)
  1622. };
  1623. }
  1624. sched_domain_topology_saved = sched_domain_topology;
  1625. sched_domain_topology = tl;
  1626. sched_domains_numa_levels = nr_levels;
  1627. WRITE_ONCE(sched_max_numa_distance, sched_domains_numa_distance[nr_levels - 1]);
  1628. init_numa_topology_type(offline_node);
  1629. }
  1630. static void sched_reset_numa(void)
  1631. {
  1632. int nr_levels, *distances;
  1633. struct cpumask ***masks;
  1634. nr_levels = sched_domains_numa_levels;
  1635. sched_domains_numa_levels = 0;
  1636. sched_max_numa_distance = 0;
  1637. sched_numa_topology_type = NUMA_DIRECT;
  1638. distances = sched_domains_numa_distance;
  1639. rcu_assign_pointer(sched_domains_numa_distance, NULL);
  1640. masks = sched_domains_numa_masks;
  1641. rcu_assign_pointer(sched_domains_numa_masks, NULL);
  1642. if (distances || masks) {
  1643. int i, j;
  1644. synchronize_rcu();
  1645. kfree(distances);
  1646. for (i = 0; i < nr_levels && masks; i++) {
  1647. if (!masks[i])
  1648. continue;
  1649. for_each_node(j)
  1650. kfree(masks[i][j]);
  1651. kfree(masks[i]);
  1652. }
  1653. kfree(masks);
  1654. }
  1655. if (sched_domain_topology_saved) {
  1656. kfree(sched_domain_topology);
  1657. sched_domain_topology = sched_domain_topology_saved;
  1658. sched_domain_topology_saved = NULL;
  1659. }
  1660. }
  1661. /*
  1662. * Call with hotplug lock held
  1663. */
  1664. void sched_update_numa(int cpu, bool online)
  1665. {
  1666. int node;
  1667. node = cpu_to_node(cpu);
  1668. /*
  1669. * Scheduler NUMA topology is updated when the first CPU of a
  1670. * node is onlined or the last CPU of a node is offlined.
  1671. */
  1672. if (cpumask_weight(cpumask_of_node(node)) != 1)
  1673. return;
  1674. sched_reset_numa();
  1675. sched_init_numa(online ? NUMA_NO_NODE : node);
  1676. }
  1677. void sched_domains_numa_masks_set(unsigned int cpu)
  1678. {
  1679. int node = cpu_to_node(cpu);
  1680. int i, j;
  1681. for (i = 0; i < sched_domains_numa_levels; i++) {
  1682. for (j = 0; j < nr_node_ids; j++) {
  1683. if (!node_state(j, N_CPU))
  1684. continue;
  1685. /* Set ourselves in the remote node's masks */
  1686. if (node_distance(j, node) <= sched_domains_numa_distance[i])
  1687. cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
  1688. }
  1689. }
  1690. }
  1691. void sched_domains_numa_masks_clear(unsigned int cpu)
  1692. {
  1693. int i, j;
  1694. for (i = 0; i < sched_domains_numa_levels; i++) {
  1695. for (j = 0; j < nr_node_ids; j++) {
  1696. if (sched_domains_numa_masks[i][j])
  1697. cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
  1698. }
  1699. }
  1700. }
  1701. /*
  1702. * sched_numa_find_closest() - given the NUMA topology, find the cpu
  1703. * closest to @cpu from @cpumask.
  1704. * cpumask: cpumask to find a cpu from
  1705. * cpu: cpu to be close to
  1706. *
  1707. * returns: cpu, or nr_cpu_ids when nothing found.
  1708. */
  1709. int sched_numa_find_closest(const struct cpumask *cpus, int cpu)
  1710. {
  1711. int i, j = cpu_to_node(cpu), found = nr_cpu_ids;
  1712. struct cpumask ***masks;
  1713. rcu_read_lock();
  1714. masks = rcu_dereference(sched_domains_numa_masks);
  1715. if (!masks)
  1716. goto unlock;
  1717. for (i = 0; i < sched_domains_numa_levels; i++) {
  1718. if (!masks[i][j])
  1719. break;
  1720. cpu = cpumask_any_and(cpus, masks[i][j]);
  1721. if (cpu < nr_cpu_ids) {
  1722. found = cpu;
  1723. break;
  1724. }
  1725. }
  1726. unlock:
  1727. rcu_read_unlock();
  1728. return found;
  1729. }
  1730. #endif /* CONFIG_NUMA */
  1731. static int __sdt_alloc(const struct cpumask *cpu_map)
  1732. {
  1733. struct sched_domain_topology_level *tl;
  1734. int j;
  1735. for_each_sd_topology(tl) {
  1736. struct sd_data *sdd = &tl->data;
  1737. sdd->sd = alloc_percpu(struct sched_domain *);
  1738. if (!sdd->sd)
  1739. return -ENOMEM;
  1740. sdd->sds = alloc_percpu(struct sched_domain_shared *);
  1741. if (!sdd->sds)
  1742. return -ENOMEM;
  1743. sdd->sg = alloc_percpu(struct sched_group *);
  1744. if (!sdd->sg)
  1745. return -ENOMEM;
  1746. sdd->sgc = alloc_percpu(struct sched_group_capacity *);
  1747. if (!sdd->sgc)
  1748. return -ENOMEM;
  1749. for_each_cpu(j, cpu_map) {
  1750. struct sched_domain *sd;
  1751. struct sched_domain_shared *sds;
  1752. struct sched_group *sg;
  1753. struct sched_group_capacity *sgc;
  1754. sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
  1755. GFP_KERNEL, cpu_to_node(j));
  1756. if (!sd)
  1757. return -ENOMEM;
  1758. *per_cpu_ptr(sdd->sd, j) = sd;
  1759. sds = kzalloc_node(sizeof(struct sched_domain_shared),
  1760. GFP_KERNEL, cpu_to_node(j));
  1761. if (!sds)
  1762. return -ENOMEM;
  1763. *per_cpu_ptr(sdd->sds, j) = sds;
  1764. sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
  1765. GFP_KERNEL, cpu_to_node(j));
  1766. if (!sg)
  1767. return -ENOMEM;
  1768. sg->next = sg;
  1769. *per_cpu_ptr(sdd->sg, j) = sg;
  1770. sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
  1771. GFP_KERNEL, cpu_to_node(j));
  1772. if (!sgc)
  1773. return -ENOMEM;
  1774. #ifdef CONFIG_SCHED_DEBUG
  1775. sgc->id = j;
  1776. #endif
  1777. *per_cpu_ptr(sdd->sgc, j) = sgc;
  1778. }
  1779. }
  1780. return 0;
  1781. }
  1782. static void __sdt_free(const struct cpumask *cpu_map)
  1783. {
  1784. struct sched_domain_topology_level *tl;
  1785. int j;
  1786. for_each_sd_topology(tl) {
  1787. struct sd_data *sdd = &tl->data;
  1788. for_each_cpu(j, cpu_map) {
  1789. struct sched_domain *sd;
  1790. if (sdd->sd) {
  1791. sd = *per_cpu_ptr(sdd->sd, j);
  1792. if (sd && (sd->flags & SD_OVERLAP))
  1793. free_sched_groups(sd->groups, 0);
  1794. kfree(*per_cpu_ptr(sdd->sd, j));
  1795. }
  1796. if (sdd->sds)
  1797. kfree(*per_cpu_ptr(sdd->sds, j));
  1798. if (sdd->sg)
  1799. kfree(*per_cpu_ptr(sdd->sg, j));
  1800. if (sdd->sgc)
  1801. kfree(*per_cpu_ptr(sdd->sgc, j));
  1802. }
  1803. free_percpu(sdd->sd);
  1804. sdd->sd = NULL;
  1805. free_percpu(sdd->sds);
  1806. sdd->sds = NULL;
  1807. free_percpu(sdd->sg);
  1808. sdd->sg = NULL;
  1809. free_percpu(sdd->sgc);
  1810. sdd->sgc = NULL;
  1811. }
  1812. }
  1813. static struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
  1814. const struct cpumask *cpu_map, struct sched_domain_attr *attr,
  1815. struct sched_domain *child, int cpu)
  1816. {
  1817. struct sched_domain *sd = sd_init(tl, cpu_map, child, cpu);
  1818. if (child) {
  1819. sd->level = child->level + 1;
  1820. sched_domain_level_max = max(sched_domain_level_max, sd->level);
  1821. child->parent = sd;
  1822. if (!cpumask_subset(sched_domain_span(child),
  1823. sched_domain_span(sd))) {
  1824. pr_err("BUG: arch topology borken\n");
  1825. #ifdef CONFIG_SCHED_DEBUG
  1826. pr_err(" the %s domain not a subset of the %s domain\n",
  1827. child->name, sd->name);
  1828. #endif
  1829. /* Fixup, ensure @sd has at least @child CPUs. */
  1830. cpumask_or(sched_domain_span(sd),
  1831. sched_domain_span(sd),
  1832. sched_domain_span(child));
  1833. }
  1834. }
  1835. set_domain_attribute(sd, attr);
  1836. return sd;
  1837. }
  1838. /*
  1839. * Ensure topology masks are sane, i.e. there are no conflicts (overlaps) for
  1840. * any two given CPUs at this (non-NUMA) topology level.
  1841. */
  1842. static bool topology_span_sane(struct sched_domain_topology_level *tl,
  1843. const struct cpumask *cpu_map, int cpu)
  1844. {
  1845. int i;
  1846. /* NUMA levels are allowed to overlap */
  1847. if (tl->flags & SDTL_OVERLAP)
  1848. return true;
  1849. /*
  1850. * Non-NUMA levels cannot partially overlap - they must be either
  1851. * completely equal or completely disjoint. Otherwise we can end up
  1852. * breaking the sched_group lists - i.e. a later get_group() pass
  1853. * breaks the linking done for an earlier span.
  1854. */
  1855. for_each_cpu(i, cpu_map) {
  1856. if (i == cpu)
  1857. continue;
  1858. /*
  1859. * We should 'and' all those masks with 'cpu_map' to exactly
  1860. * match the topology we're about to build, but that can only
  1861. * remove CPUs, which only lessens our ability to detect
  1862. * overlaps
  1863. */
  1864. if (!cpumask_equal(tl->mask(cpu), tl->mask(i)) &&
  1865. cpumask_intersects(tl->mask(cpu), tl->mask(i)))
  1866. return false;
  1867. }
  1868. return true;
  1869. }
  1870. /*
  1871. * Build sched domains for a given set of CPUs and attach the sched domains
  1872. * to the individual CPUs
  1873. */
  1874. static int
  1875. build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *attr)
  1876. {
  1877. enum s_alloc alloc_state = sa_none;
  1878. struct sched_domain *sd;
  1879. struct s_data d;
  1880. struct rq *rq = NULL;
  1881. int i, ret = -ENOMEM;
  1882. bool has_asym = false;
  1883. if (WARN_ON(cpumask_empty(cpu_map)))
  1884. goto error;
  1885. alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
  1886. if (alloc_state != sa_rootdomain)
  1887. goto error;
  1888. /* Set up domains for CPUs specified by the cpu_map: */
  1889. for_each_cpu(i, cpu_map) {
  1890. struct sched_domain_topology_level *tl;
  1891. sd = NULL;
  1892. for_each_sd_topology(tl) {
  1893. if (WARN_ON(!topology_span_sane(tl, cpu_map, i)))
  1894. goto error;
  1895. sd = build_sched_domain(tl, cpu_map, attr, sd, i);
  1896. has_asym |= sd->flags & SD_ASYM_CPUCAPACITY;
  1897. if (tl == sched_domain_topology)
  1898. *per_cpu_ptr(d.sd, i) = sd;
  1899. if (tl->flags & SDTL_OVERLAP)
  1900. sd->flags |= SD_OVERLAP;
  1901. if (cpumask_equal(cpu_map, sched_domain_span(sd)))
  1902. break;
  1903. }
  1904. }
  1905. /* Build the groups for the domains */
  1906. for_each_cpu(i, cpu_map) {
  1907. for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
  1908. sd->span_weight = cpumask_weight(sched_domain_span(sd));
  1909. if (sd->flags & SD_OVERLAP) {
  1910. if (build_overlap_sched_groups(sd, i))
  1911. goto error;
  1912. } else {
  1913. if (build_sched_groups(sd, i))
  1914. goto error;
  1915. }
  1916. }
  1917. }
  1918. /*
  1919. * Calculate an allowed NUMA imbalance such that LLCs do not get
  1920. * imbalanced.
  1921. */
  1922. for_each_cpu(i, cpu_map) {
  1923. unsigned int imb = 0;
  1924. unsigned int imb_span = 1;
  1925. for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
  1926. struct sched_domain *child = sd->child;
  1927. if (!(sd->flags & SD_SHARE_PKG_RESOURCES) && child &&
  1928. (child->flags & SD_SHARE_PKG_RESOURCES)) {
  1929. struct sched_domain __rcu *top_p;
  1930. unsigned int nr_llcs;
  1931. /*
  1932. * For a single LLC per node, allow an
  1933. * imbalance up to 12.5% of the node. This is
  1934. * arbitrary cutoff based two factors -- SMT and
  1935. * memory channels. For SMT-2, the intent is to
  1936. * avoid premature sharing of HT resources but
  1937. * SMT-4 or SMT-8 *may* benefit from a different
  1938. * cutoff. For memory channels, this is a very
  1939. * rough estimate of how many channels may be
  1940. * active and is based on recent CPUs with
  1941. * many cores.
  1942. *
  1943. * For multiple LLCs, allow an imbalance
  1944. * until multiple tasks would share an LLC
  1945. * on one node while LLCs on another node
  1946. * remain idle. This assumes that there are
  1947. * enough logical CPUs per LLC to avoid SMT
  1948. * factors and that there is a correlation
  1949. * between LLCs and memory channels.
  1950. */
  1951. nr_llcs = sd->span_weight / child->span_weight;
  1952. if (nr_llcs == 1)
  1953. imb = sd->span_weight >> 3;
  1954. else
  1955. imb = nr_llcs;
  1956. imb = max(1U, imb);
  1957. sd->imb_numa_nr = imb;
  1958. /* Set span based on the first NUMA domain. */
  1959. top_p = sd->parent;
  1960. while (top_p && !(top_p->flags & SD_NUMA)) {
  1961. top_p = top_p->parent;
  1962. }
  1963. imb_span = top_p ? top_p->span_weight : sd->span_weight;
  1964. } else {
  1965. int factor = max(1U, (sd->span_weight / imb_span));
  1966. sd->imb_numa_nr = imb * factor;
  1967. }
  1968. }
  1969. }
  1970. /* Calculate CPU capacity for physical packages and nodes */
  1971. for (i = nr_cpumask_bits-1; i >= 0; i--) {
  1972. if (!cpumask_test_cpu(i, cpu_map))
  1973. continue;
  1974. for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
  1975. claim_allocations(i, sd);
  1976. init_sched_groups_capacity(i, sd);
  1977. }
  1978. }
  1979. /* Attach the domains */
  1980. rcu_read_lock();
  1981. for_each_cpu(i, cpu_map) {
  1982. rq = cpu_rq(i);
  1983. sd = *per_cpu_ptr(d.sd, i);
  1984. /* Use READ_ONCE()/WRITE_ONCE() to avoid load/store tearing: */
  1985. if (rq->cpu_capacity_orig > READ_ONCE(d.rd->max_cpu_capacity))
  1986. WRITE_ONCE(d.rd->max_cpu_capacity, rq->cpu_capacity_orig);
  1987. cpu_attach_domain(sd, d.rd, i);
  1988. }
  1989. rcu_read_unlock();
  1990. if (has_asym)
  1991. static_branch_inc_cpuslocked(&sched_asym_cpucapacity);
  1992. if (rq && sched_debug_verbose) {
  1993. pr_info("root domain span: %*pbl (max cpu_capacity = %lu)\n",
  1994. cpumask_pr_args(cpu_map), rq->rd->max_cpu_capacity);
  1995. }
  1996. trace_android_vh_build_sched_domains(has_asym);
  1997. ret = 0;
  1998. error:
  1999. __free_domain_allocs(&d, alloc_state, cpu_map);
  2000. return ret;
  2001. }
  2002. /* Current sched domains: */
  2003. static cpumask_var_t *doms_cur;
  2004. /* Number of sched domains in 'doms_cur': */
  2005. static int ndoms_cur;
  2006. /* Attributes of custom domains in 'doms_cur' */
  2007. static struct sched_domain_attr *dattr_cur;
  2008. /*
  2009. * Special case: If a kmalloc() of a doms_cur partition (array of
  2010. * cpumask) fails, then fallback to a single sched domain,
  2011. * as determined by the single cpumask fallback_doms.
  2012. */
  2013. static cpumask_var_t fallback_doms;
  2014. /*
  2015. * arch_update_cpu_topology lets virtualized architectures update the
  2016. * CPU core maps. It is supposed to return 1 if the topology changed
  2017. * or 0 if it stayed the same.
  2018. */
  2019. int __weak arch_update_cpu_topology(void)
  2020. {
  2021. return 0;
  2022. }
  2023. cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
  2024. {
  2025. int i;
  2026. cpumask_var_t *doms;
  2027. doms = kmalloc_array(ndoms, sizeof(*doms), GFP_KERNEL);
  2028. if (!doms)
  2029. return NULL;
  2030. for (i = 0; i < ndoms; i++) {
  2031. if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
  2032. free_sched_domains(doms, i);
  2033. return NULL;
  2034. }
  2035. }
  2036. return doms;
  2037. }
  2038. void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
  2039. {
  2040. unsigned int i;
  2041. for (i = 0; i < ndoms; i++)
  2042. free_cpumask_var(doms[i]);
  2043. kfree(doms);
  2044. }
  2045. /*
  2046. * Set up scheduler domains and groups. For now this just excludes isolated
  2047. * CPUs, but could be used to exclude other special cases in the future.
  2048. */
  2049. int sched_init_domains(const struct cpumask *cpu_map)
  2050. {
  2051. int err;
  2052. zalloc_cpumask_var(&sched_domains_tmpmask, GFP_KERNEL);
  2053. zalloc_cpumask_var(&sched_domains_tmpmask2, GFP_KERNEL);
  2054. zalloc_cpumask_var(&fallback_doms, GFP_KERNEL);
  2055. arch_update_cpu_topology();
  2056. asym_cpu_capacity_scan();
  2057. ndoms_cur = 1;
  2058. doms_cur = alloc_sched_domains(ndoms_cur);
  2059. if (!doms_cur)
  2060. doms_cur = &fallback_doms;
  2061. cpumask_and(doms_cur[0], cpu_map, housekeeping_cpumask(HK_TYPE_DOMAIN));
  2062. err = build_sched_domains(doms_cur[0], NULL);
  2063. return err;
  2064. }
  2065. /*
  2066. * Detach sched domains from a group of CPUs specified in cpu_map
  2067. * These CPUs will now be attached to the NULL domain
  2068. */
  2069. static void detach_destroy_domains(const struct cpumask *cpu_map)
  2070. {
  2071. unsigned int cpu = cpumask_any(cpu_map);
  2072. int i;
  2073. if (rcu_access_pointer(per_cpu(sd_asym_cpucapacity, cpu)))
  2074. static_branch_dec_cpuslocked(&sched_asym_cpucapacity);
  2075. rcu_read_lock();
  2076. for_each_cpu(i, cpu_map)
  2077. cpu_attach_domain(NULL, &def_root_domain, i);
  2078. rcu_read_unlock();
  2079. }
  2080. /* handle null as "default" */
  2081. static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
  2082. struct sched_domain_attr *new, int idx_new)
  2083. {
  2084. struct sched_domain_attr tmp;
  2085. /* Fast path: */
  2086. if (!new && !cur)
  2087. return 1;
  2088. tmp = SD_ATTR_INIT;
  2089. return !memcmp(cur ? (cur + idx_cur) : &tmp,
  2090. new ? (new + idx_new) : &tmp,
  2091. sizeof(struct sched_domain_attr));
  2092. }
  2093. /*
  2094. * Partition sched domains as specified by the 'ndoms_new'
  2095. * cpumasks in the array doms_new[] of cpumasks. This compares
  2096. * doms_new[] to the current sched domain partitioning, doms_cur[].
  2097. * It destroys each deleted domain and builds each new domain.
  2098. *
  2099. * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
  2100. * The masks don't intersect (don't overlap.) We should setup one
  2101. * sched domain for each mask. CPUs not in any of the cpumasks will
  2102. * not be load balanced. If the same cpumask appears both in the
  2103. * current 'doms_cur' domains and in the new 'doms_new', we can leave
  2104. * it as it is.
  2105. *
  2106. * The passed in 'doms_new' should be allocated using
  2107. * alloc_sched_domains. This routine takes ownership of it and will
  2108. * free_sched_domains it when done with it. If the caller failed the
  2109. * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
  2110. * and partition_sched_domains() will fallback to the single partition
  2111. * 'fallback_doms', it also forces the domains to be rebuilt.
  2112. *
  2113. * If doms_new == NULL it will be replaced with cpu_online_mask.
  2114. * ndoms_new == 0 is a special case for destroying existing domains,
  2115. * and it will not create the default domain.
  2116. *
  2117. * Call with hotplug lock and sched_domains_mutex held
  2118. */
  2119. void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new[],
  2120. struct sched_domain_attr *dattr_new)
  2121. {
  2122. bool __maybe_unused has_eas = false;
  2123. int i, j, n;
  2124. int new_topology;
  2125. lockdep_assert_held(&sched_domains_mutex);
  2126. /* Let the architecture update CPU core mappings: */
  2127. new_topology = arch_update_cpu_topology();
  2128. /* Trigger rebuilding CPU capacity asymmetry data */
  2129. if (new_topology)
  2130. asym_cpu_capacity_scan();
  2131. if (!doms_new) {
  2132. WARN_ON_ONCE(dattr_new);
  2133. n = 0;
  2134. doms_new = alloc_sched_domains(1);
  2135. if (doms_new) {
  2136. n = 1;
  2137. cpumask_and(doms_new[0], cpu_active_mask,
  2138. housekeeping_cpumask(HK_TYPE_DOMAIN));
  2139. }
  2140. } else {
  2141. n = ndoms_new;
  2142. }
  2143. /* Destroy deleted domains: */
  2144. for (i = 0; i < ndoms_cur; i++) {
  2145. for (j = 0; j < n && !new_topology; j++) {
  2146. if (cpumask_equal(doms_cur[i], doms_new[j]) &&
  2147. dattrs_equal(dattr_cur, i, dattr_new, j)) {
  2148. struct root_domain *rd;
  2149. /*
  2150. * This domain won't be destroyed and as such
  2151. * its dl_bw->total_bw needs to be cleared. It
  2152. * will be recomputed in function
  2153. * update_tasks_root_domain().
  2154. */
  2155. rd = cpu_rq(cpumask_any(doms_cur[i]))->rd;
  2156. dl_clear_root_domain(rd);
  2157. goto match1;
  2158. }
  2159. }
  2160. /* No match - a current sched domain not in new doms_new[] */
  2161. detach_destroy_domains(doms_cur[i]);
  2162. match1:
  2163. ;
  2164. }
  2165. n = ndoms_cur;
  2166. if (!doms_new) {
  2167. n = 0;
  2168. doms_new = &fallback_doms;
  2169. cpumask_and(doms_new[0], cpu_active_mask,
  2170. housekeeping_cpumask(HK_TYPE_DOMAIN));
  2171. }
  2172. /* Build new domains: */
  2173. for (i = 0; i < ndoms_new; i++) {
  2174. for (j = 0; j < n && !new_topology; j++) {
  2175. if (cpumask_equal(doms_new[i], doms_cur[j]) &&
  2176. dattrs_equal(dattr_new, i, dattr_cur, j))
  2177. goto match2;
  2178. }
  2179. /* No match - add a new doms_new */
  2180. build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
  2181. match2:
  2182. ;
  2183. }
  2184. #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
  2185. /* Build perf. domains: */
  2186. for (i = 0; i < ndoms_new; i++) {
  2187. for (j = 0; j < n && !sched_energy_update; j++) {
  2188. if (cpumask_equal(doms_new[i], doms_cur[j]) &&
  2189. cpu_rq(cpumask_first(doms_cur[j]))->rd->pd) {
  2190. has_eas = true;
  2191. goto match3;
  2192. }
  2193. }
  2194. /* No match - add perf. domains for a new rd */
  2195. has_eas |= build_perf_domains(doms_new[i]);
  2196. match3:
  2197. ;
  2198. }
  2199. sched_energy_set(has_eas);
  2200. #endif
  2201. /* Remember the new sched domains: */
  2202. if (doms_cur != &fallback_doms)
  2203. free_sched_domains(doms_cur, ndoms_cur);
  2204. kfree(dattr_cur);
  2205. doms_cur = doms_new;
  2206. dattr_cur = dattr_new;
  2207. ndoms_cur = ndoms_new;
  2208. update_sched_domain_debugfs();
  2209. }
  2210. /*
  2211. * Call with hotplug lock held
  2212. */
  2213. void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
  2214. struct sched_domain_attr *dattr_new)
  2215. {
  2216. mutex_lock(&sched_domains_mutex);
  2217. partition_sched_domains_locked(ndoms_new, doms_new, dattr_new);
  2218. mutex_unlock(&sched_domains_mutex);
  2219. }