topology.c 71 KB

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