hmp.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. /*
  2. * Copyright (C) 2016 MediaTek Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See http://www.gnu.org/licenses/gpl-2.0.html for more details.
  12. */
  13. #include <linux/sched.h>
  14. #include <linux/stat.h>
  15. #include <linux/math64.h>
  16. #include <linux/kobject.h>
  17. #include <linux/sysfs.h>
  18. #include <trace/events/sched.h>
  19. #include <linux/stop_machine.h>
  20. #include <linux/cpumask.h>
  21. #include <linux/list_sort.h>
  22. /*
  23. * Heterogenous multiprocessor (HMP) optimizations
  24. *
  25. * The cpu types are distinguished using a list of hmp_domains
  26. * which each represent one cpu type using a cpumask.
  27. * The list is assumed ordered by compute capacity with the
  28. * fastest domain first.
  29. */
  30. DEFINE_PER_CPU(struct hmp_domain *, hmp_cpu_domain);
  31. /* Setup hmp_domains */
  32. void hmp_cpu_mask_setup(void)
  33. {
  34. struct hmp_domain *domain;
  35. struct list_head *pos;
  36. int cpu;
  37. pr_info("Initializing HMP scheduler:\n");
  38. /* Initialize hmp_domains using platform code */
  39. if (list_empty(&hmp_domains)) {
  40. pr_info("HMP domain list is empty!\n");
  41. return;
  42. }
  43. /* Print hmp_domains */
  44. list_for_each(pos, &hmp_domains) {
  45. domain = list_entry(pos, struct hmp_domain, hmp_domains);
  46. for_each_cpu(cpu, &domain->possible_cpus)
  47. per_cpu(hmp_cpu_domain, cpu) = domain;
  48. }
  49. pr_info("Initializing HMP scheduler done\n");
  50. }
  51. /*
  52. * Heterogenous CPU capacity compare function
  53. * Only inspect lowest id of cpus in same domain.
  54. * Assume CPUs in same domain has same capacity.
  55. */
  56. struct cluster_info {
  57. struct hmp_domain *hmpd;
  58. unsigned long cpu_perf;
  59. int cpu;
  60. };
  61. static inline void fillin_cluster(struct cluster_info *cinfo,
  62. struct hmp_domain *hmpd)
  63. {
  64. int cpu;
  65. unsigned long cpu_perf;
  66. cinfo->hmpd = hmpd;
  67. cinfo->cpu = cpumask_any(&cinfo->hmpd->possible_cpus);
  68. for_each_cpu(cpu, &hmpd->possible_cpus) {
  69. cpu_perf = arch_scale_cpu_capacity(NULL, cpu);
  70. if (cpu_perf > 0)
  71. break;
  72. }
  73. cinfo->cpu_perf = cpu_perf;
  74. if (cpu_perf == 0)
  75. pr_info("Uninitialized CPU performance (CPU mask: %lx)",
  76. cpumask_bits(&hmpd->possible_cpus)[0]);
  77. }
  78. /*
  79. * Negative, if @a should sort before @b
  80. * Positive, if @a should sort after @b.
  81. * Return 0, if ordering is to be preserved
  82. */
  83. int hmp_compare(void *priv, struct list_head *a, struct list_head *b)
  84. {
  85. struct cluster_info ca;
  86. struct cluster_info cb;
  87. fillin_cluster(&ca, list_entry(a, struct hmp_domain, hmp_domains));
  88. fillin_cluster(&cb, list_entry(b, struct hmp_domain, hmp_domains));
  89. return (ca.cpu_perf > cb.cpu_perf) ? -1 : 1;
  90. }
  91. void init_hmp_domains(void)
  92. {
  93. struct hmp_domain *domain;
  94. struct cpumask cpu_mask;
  95. int id, maxid;
  96. cpumask_clear(&cpu_mask);
  97. maxid = arch_get_nr_clusters();
  98. /*
  99. * Initialize hmp_domains
  100. * Must be ordered with respect to compute capacity.
  101. * Fastest domain at head of list.
  102. */
  103. for (id = 0; id < maxid; id++) {
  104. arch_get_cluster_cpus(&cpu_mask, id);
  105. domain = (struct hmp_domain *)
  106. kmalloc(sizeof(struct hmp_domain), GFP_KERNEL);
  107. if (domain) {
  108. cpumask_copy(&domain->possible_cpus, &cpu_mask);
  109. cpumask_and(&domain->cpus, cpu_online_mask,
  110. &domain->possible_cpus);
  111. list_add(&domain->hmp_domains, &hmp_domains);
  112. }
  113. }
  114. /*
  115. * Sorting HMP domain by CPU capacity
  116. */
  117. list_sort(NULL, &hmp_domains, &hmp_compare);
  118. pr_info("Sort hmp_domains from little to big:\n");
  119. for_each_hmp_domain_L_first(domain) {
  120. pr_info(" cpumask: 0x%02lx\n",
  121. *cpumask_bits(&domain->possible_cpus));
  122. }
  123. hmp_cpu_mask_setup();
  124. }
  125. #ifdef CONFIG_SCHED_HMP
  126. static int is_heavy_task(struct task_struct *p)
  127. {
  128. return p->se.avg.loadwop_avg >= 650 ? 1 : 0;
  129. }
  130. struct clb_env {
  131. struct clb_stats bstats;
  132. struct clb_stats lstats;
  133. int btarget, ltarget;
  134. struct cpumask bcpus;
  135. struct cpumask lcpus;
  136. unsigned int flags;
  137. struct mcheck {
  138. /* Details of this migration check */
  139. int status;
  140. /* Indicate whether we should perform this task migration */
  141. int result;
  142. } mcheck;
  143. };
  144. static void collect_cluster_stats(struct clb_stats *clbs,
  145. struct cpumask *cluster_cpus, int target)
  146. {
  147. /* Update cluster informatics */
  148. int cpu;
  149. int loadwop;
  150. for_each_cpu(cpu, cluster_cpus) {
  151. if (cpu_online(cpu)) {
  152. clbs->ncpu++;
  153. clbs->ntask += cpu_rq(cpu)->cfs.h_nr_running;
  154. clbs->load_avg += cpu_rq(cpu)->cfs.avg.loadwop_avg;
  155. #ifdef CONFIG_SCHED_HMP_PRIO_FILTER
  156. clbs->nr_normal_prio_task += cfs_nr_normal_prio(cpu);
  157. clbs->nr_dequeuing_low_prio +=
  158. cfs_nr_dequeuing_low_prio(cpu);
  159. #endif
  160. }
  161. }
  162. if (!clbs->ncpu || target >= num_possible_cpus() ||
  163. !cpumask_test_cpu(target, cluster_cpus))
  164. return;
  165. /*
  166. * Calculate available CPU capacity
  167. * Calculate available task space
  168. *
  169. * Why load ratio should be multiplied by the number of task ?
  170. * The task is the entity of scheduling unit so that we should consider
  171. * it in scheduler. Only considering task load is not enough.
  172. * Thus, multiplying the number of tasks can adjust load ratio to a more
  173. * reasonable value.
  174. */
  175. loadwop = cpu_rq(target)->cfs.avg.loadwop_avg;
  176. clbs->load_avg /= clbs->ncpu;
  177. clbs->acap = (clbs->cpu_capacity > loadwop) ?
  178. (clbs->cpu_capacity - loadwop) : 0;
  179. clbs->scaled_atask = (clbs->cpu_capacity > loadwop) ?
  180. (clbs->cpu_capacity - loadwop) : 0;
  181. trace_sched_cluster_stats(target,
  182. cpu_rq(target)->cfs.avg.loadwop_avg,
  183. cpu_rq(target)->cfs.h_nr_running,
  184. *cpumask_bits(cluster_cpus),
  185. clbs->ntask, clbs->load_avg,
  186. clbs->cpu_capacity, clbs->acap,
  187. clbs->scaled_atask, clbs->threshold);
  188. }
  189. /*
  190. * Task Dynamic Migration Threshold Adjustment.
  191. *
  192. * If the workload between clusters is not balanced, adjust migration
  193. * threshold in an attempt to move task precisely.
  194. *
  195. * Diff. = Max Threshold - Min Threshold
  196. *
  197. * Dynamic UP-Threshold =
  198. * B_nacap B_natask
  199. * Max Threshold - Diff. x ----------------- x -------------------
  200. * B_nacap + L_nacap B_natask + L_natask
  201. *
  202. *
  203. * Dynamic Down-Threshold =
  204. * L_nacap L_natask
  205. * Min Threshold + Diff. x ----------------- x -------------------
  206. * B_nacap + L_nacap B_natask + L_natask
  207. */
  208. static void adj_threshold(struct clb_env *clbenv)
  209. {
  210. #define HMP_RESOLUTION_SCALING (4)
  211. #define hmp_scale_down(w) ((w) >> HMP_RESOLUTION_SCALING)
  212. unsigned long b_cap = 0, l_cap = 0;
  213. int b_nacap, l_nacap;
  214. const int hmp_max_weight = scale_load_down(HMP_MAX_LOAD);
  215. b_cap = clbenv->bstats.cpu_power;
  216. l_cap = clbenv->lstats.cpu_power;
  217. b_nacap = clbenv->bstats.acap;
  218. l_nacap = clbenv->lstats.acap * l_cap / (b_cap+1);
  219. b_nacap = hmp_scale_down(b_nacap);
  220. l_nacap = hmp_scale_down(l_nacap);
  221. if ((b_nacap + l_nacap) == 0) {
  222. clbenv->bstats.threshold = hmp_max_weight;
  223. clbenv->lstats.threshold = 0;
  224. } else {
  225. clbenv->bstats.threshold = hmp_max_weight -
  226. (hmp_max_weight * b_nacap * b_nacap) /
  227. ((b_nacap + l_nacap) * (b_nacap + l_nacap));
  228. clbenv->lstats.threshold = hmp_max_weight * l_nacap * l_nacap /
  229. ((b_nacap + l_nacap) * (b_nacap + l_nacap));
  230. }
  231. trace_sched_adj_threshold(clbenv->bstats.threshold,
  232. clbenv->lstats.threshold, clbenv->ltarget,
  233. l_cap, clbenv->btarget, b_cap);
  234. }
  235. static void sched_update_clbstats(struct clb_env *clbenv)
  236. {
  237. /* init cpu power and capacity */
  238. clbenv->bstats.cpu_power =
  239. (int) arch_scale_cpu_capacity(NULL, clbenv->btarget);
  240. clbenv->lstats.cpu_power =
  241. (int) arch_scale_cpu_capacity(NULL, clbenv->ltarget);
  242. clbenv->lstats.cpu_capacity = SCHED_CAPACITY_SCALE *
  243. clbenv->lstats.cpu_power / (clbenv->bstats.cpu_power+1);
  244. clbenv->bstats.cpu_capacity = SCHED_CAPACITY_SCALE;
  245. collect_cluster_stats(&clbenv->bstats, &clbenv->bcpus, clbenv->btarget);
  246. collect_cluster_stats(&clbenv->lstats, &clbenv->lcpus, clbenv->ltarget);
  247. adj_threshold(clbenv);
  248. }
  249. static struct hmp_domain *hmp_get_hmp_domain_for_cpu(int cpu)
  250. {
  251. struct hmp_domain *domain;
  252. struct list_head *pos;
  253. list_for_each(pos, &hmp_domains) {
  254. domain = list_entry(pos, struct hmp_domain, hmp_domains);
  255. if (cpumask_test_cpu(cpu, &domain->possible_cpus))
  256. return domain;
  257. }
  258. return NULL;
  259. }
  260. static void hmp_online_cpu(int cpu)
  261. {
  262. struct hmp_domain *domain = hmp_get_hmp_domain_for_cpu(cpu);
  263. if (domain)
  264. cpumask_set_cpu(cpu, &domain->cpus);
  265. }
  266. static void hmp_offline_cpu(int cpu)
  267. {
  268. struct hmp_domain *domain = hmp_get_hmp_domain_for_cpu(cpu);
  269. if (domain)
  270. cpumask_clear_cpu(cpu, &domain->cpus);
  271. }
  272. unsigned int hmp_next_up_threshold = 4096;
  273. unsigned int hmp_next_down_threshold = 4096;
  274. #define hmp_last_up_migration(cpu) \
  275. cpu_rq(cpu)->cfs.avg.hmp_last_up_migration
  276. #define hmp_last_down_migration(cpu) \
  277. cpu_rq(cpu)->cfs.avg.hmp_last_down_migration
  278. static inline unsigned int hmp_domain_min_load(struct hmp_domain *hmpd,
  279. int *min_cpu);
  280. /* Check if cpu is in fastest hmp_domain */
  281. inline unsigned int hmp_cpu_is_fastest(int cpu)
  282. {
  283. struct list_head *pos;
  284. pos = &hmp_cpu_domain(cpu)->hmp_domains;
  285. return pos == hmp_domains.next;
  286. }
  287. /* Check if cpu is in slowest hmp_domain */
  288. inline unsigned int hmp_cpu_is_slowest(int cpu)
  289. {
  290. struct list_head *pos;
  291. pos = &hmp_cpu_domain(cpu)->hmp_domains;
  292. return list_is_last(pos, &hmp_domains);
  293. }
  294. /* Next (slower) hmp_domain relative to cpu */
  295. static inline struct hmp_domain *hmp_slower_domain(int cpu)
  296. {
  297. struct list_head *pos;
  298. pos = &hmp_cpu_domain(cpu)->hmp_domains;
  299. if (list_is_last(pos, &hmp_domains))
  300. return list_entry(pos, struct hmp_domain, hmp_domains);
  301. return list_entry(pos->next, struct hmp_domain, hmp_domains);
  302. }
  303. /* Previous (faster) hmp_domain relative to cpu */
  304. static inline struct hmp_domain *hmp_faster_domain(int cpu)
  305. {
  306. struct list_head *pos;
  307. pos = &hmp_cpu_domain(cpu)->hmp_domains;
  308. if (pos->prev == &hmp_domains)
  309. return list_entry(pos, struct hmp_domain, hmp_domains);
  310. return list_entry(pos->prev, struct hmp_domain, hmp_domains);
  311. }
  312. /*
  313. * Selects a cpu in previous (faster) hmp_domain
  314. * Note that cpumask_any_and() returns the first cpu in the cpumask
  315. */
  316. static inline unsigned int hmp_select_faster_cpu(struct task_struct *tsk,
  317. int cpu)
  318. {
  319. int lowest_cpu = num_possible_cpus();
  320. __always_unused int lowest_ratio =
  321. hmp_domain_min_load(hmp_faster_domain(cpu), &lowest_cpu);
  322. /*
  323. * If the lowest-loaded CPU in the domain is allowed by
  324. * the task affinity.
  325. * Select that one, otherwise select one which is allowed
  326. */
  327. if (lowest_cpu < nr_cpu_ids &&
  328. cpumask_test_cpu(lowest_cpu, &tsk->cpus_allowed))
  329. return lowest_cpu;
  330. else
  331. return cpumask_any_and(&hmp_faster_domain(cpu)->cpus,
  332. &tsk->cpus_allowed);
  333. }
  334. static inline void hmp_next_up_delay(struct sched_entity *se, int cpu)
  335. {
  336. hmp_last_up_migration(cpu) = sched_clock();
  337. hmp_last_down_migration(cpu) = 0;
  338. }
  339. static inline void hmp_next_down_delay(struct sched_entity *se, int cpu)
  340. {
  341. hmp_last_down_migration(cpu) = sched_clock();
  342. hmp_last_up_migration(cpu) = 0;
  343. }
  344. static inline unsigned int hmp_domain_min_load(struct hmp_domain *hmpd,
  345. int *min_cpu)
  346. {
  347. int cpu;
  348. int min_cpu_runnable_temp = num_possible_cpus();
  349. unsigned long min_runnable_load = INT_MAX;
  350. unsigned long contrib;
  351. for_each_cpu(cpu, &hmpd->cpus) {
  352. struct cfs_rq *cfs_rq = &cpu_rq(cpu)->cfs;
  353. /* don't use the divisor in the loop, just at the end */
  354. contrib = cfs_rq->runnable_load_avg * scale_load_down(1024);
  355. if (contrib < min_runnable_load) {
  356. min_runnable_load = contrib;
  357. min_cpu_runnable_temp = cpu;
  358. }
  359. }
  360. if (min_cpu)
  361. *min_cpu = min_cpu_runnable_temp;
  362. /* domain will often have at least one empty CPU */
  363. return min_runnable_load ? min_runnable_load / (__LOAD_AVG_MAX + 1) : 0;
  364. }
  365. /* Function Declaration */
  366. static int hmp_up_stable(int cpu);
  367. static int hmp_down_stable(int cpu);
  368. static unsigned int hmp_up_migration(int cpu,
  369. int *target_cpu, struct sched_entity *se,
  370. struct clb_env *clbenv);
  371. static unsigned int hmp_down_migration(int cpu,
  372. int *target_cpu, struct sched_entity *se,
  373. struct clb_env *clbenv);
  374. #ifdef CONFIG_SCHED_HMP_PLUS
  375. static struct sched_entity *hmp_get_heaviest_task(
  376. struct sched_entity *se, int target_cpu);
  377. static struct sched_entity *hmp_get_lightest_task(
  378. struct sched_entity *se, int migrate_down);
  379. #endif
  380. #define hmp_caller_is_gb(caller) ((caller == HMP_GB)?1:0)
  381. #define hmp_cpu_stable(cpu, up) (up ? \
  382. hmp_up_stable(cpu) : hmp_down_stable(cpu))
  383. #define hmp_inc(v) ((v) + 1)
  384. #define task_created(f) ((SD_BALANCE_EXEC == f || SD_BALANCE_FORK == f)?1:0)
  385. /*
  386. * Heterogenous Multi-Processor (HMP) - Utility Function
  387. */
  388. /*
  389. * These functions add next up/down migration delay that prevents the task from
  390. * doing another migration in the same direction until the delay has expired.
  391. */
  392. static int hmp_up_stable(int cpu)
  393. {
  394. u64 now = sched_clock();
  395. if (((now - hmp_last_up_migration(cpu)) >> 10) < hmp_next_up_threshold)
  396. return 0;
  397. return 1;
  398. }
  399. static int hmp_down_stable(int cpu)
  400. {
  401. u64 now = sched_clock();
  402. u64 duration = now - hmp_last_down_migration(cpu);
  403. if ((duration >> 10) < hmp_next_down_threshold)
  404. return 0;
  405. return 1;
  406. }
  407. /* Select the most appropriate CPU from hmp cluster */
  408. static unsigned int hmp_select_cpu(unsigned int caller, struct task_struct *p,
  409. struct cpumask *mask, int prev, int up)
  410. {
  411. int curr = 0;
  412. int target = num_possible_cpus();
  413. unsigned long curr_wload = 0;
  414. unsigned long target_wload = 0;
  415. struct cpumask srcp;
  416. struct cpumask *tsk_cpus_allow = &p->cpus_allowed;
  417. cpumask_andnot(&srcp, cpu_online_mask, cpu_isolated_mask);
  418. cpumask_and(&srcp, &srcp, mask);
  419. target = cpumask_any_and(&srcp, tsk_cpus_allow);
  420. if (target >= num_possible_cpus())
  421. goto out;
  422. /*
  423. * RT class is taken into account because CPU load is multiplied
  424. * by the total number of CPU runnable tasks that includes RT tasks.
  425. */
  426. target_wload = hmp_inc(cfs_load(target));
  427. target_wload *= rq_length(target);
  428. for_each_cpu(curr, mask) {
  429. /* Check CPU status and task affinity */
  430. if (!cpu_online(curr) ||
  431. !cpumask_test_cpu(curr, tsk_cpus_allow) ||
  432. cpu_isolated(curr))
  433. continue;
  434. /* For global load balancing, unstable CPU will be bypassed */
  435. if (hmp_caller_is_gb(caller) && !hmp_cpu_stable(curr, up))
  436. continue;
  437. curr_wload = hmp_inc(cfs_load(curr));
  438. curr_wload *= rq_length(curr);
  439. if (curr_wload < target_wload) {
  440. target_wload = curr_wload;
  441. target = curr;
  442. } else if (curr_wload == target_wload && curr == prev) {
  443. target = curr;
  444. }
  445. }
  446. out:
  447. return target;
  448. }
  449. static int hmp_select_task_migration(int sd_flag,
  450. struct task_struct *p, int prev_cpu, int new_cpu,
  451. struct cpumask *fast_cpu_mask, struct cpumask *slow_cpu_mask)
  452. {
  453. int step = 0;
  454. struct sched_entity *se = &p->se;
  455. int B_target = num_possible_cpus();
  456. int L_target = num_possible_cpus();
  457. struct clb_env clbenv;
  458. B_target = hmp_select_cpu(HMP_SELECT_RQ, p, fast_cpu_mask, prev_cpu, 0);
  459. L_target = hmp_select_cpu(HMP_SELECT_RQ, p, slow_cpu_mask, prev_cpu, 1);
  460. /*
  461. * Only one cluster exists or only one cluster is allowed for this task
  462. * Case 1: return the runqueue whose load is minimum
  463. * Case 2: return original CFS runqueue selection result
  464. */
  465. if (B_target >= num_possible_cpus() && L_target >= num_possible_cpus())
  466. goto out;
  467. if (B_target >= num_possible_cpus())
  468. goto select_slow;
  469. if (L_target >= num_possible_cpus())
  470. goto select_fast;
  471. /*
  472. * Two clusters exist and both clusters are allowed for this task
  473. * Step 1: Move newly created task to the cpu where no tasks are running
  474. * Step 2: Migrate heavy-load task to big
  475. * Step 3: Migrate light-load task to LITTLE
  476. * Step 4: Make sure the task stays in its previous hmp domain
  477. */
  478. step = 1;
  479. if (task_created(sd_flag) && !task_low_priority(p->prio)) {
  480. if (!rq_length(B_target))
  481. goto select_fast;
  482. if (!rq_length(L_target))
  483. goto select_slow;
  484. }
  485. memset(&clbenv, 0, sizeof(clbenv));
  486. clbenv.flags |= HMP_SELECT_RQ;
  487. cpumask_copy(&clbenv.lcpus, slow_cpu_mask);
  488. cpumask_copy(&clbenv.bcpus, fast_cpu_mask);
  489. clbenv.ltarget = L_target;
  490. clbenv.btarget = B_target;
  491. step = 2;
  492. sched_update_clbstats(&clbenv);
  493. if (hmp_up_migration(L_target, &B_target, se, &clbenv))
  494. goto select_fast;
  495. step = 3;
  496. if (hmp_down_migration(B_target, &L_target, se, &clbenv))
  497. goto select_slow;
  498. step = 4;
  499. if (hmp_cpu_is_slowest(prev_cpu))
  500. goto select_slow;
  501. goto select_fast;
  502. select_fast:
  503. new_cpu = B_target;
  504. cpumask_clear(slow_cpu_mask);
  505. goto out;
  506. select_slow:
  507. new_cpu = L_target;
  508. cpumask_copy(fast_cpu_mask, slow_cpu_mask);
  509. cpumask_clear(slow_cpu_mask);
  510. goto out;
  511. out:
  512. /*
  513. * Value of clbenb..load_avg only ready after step 2.
  514. * Dump value after this step to avoid invalid stack value
  515. */
  516. if (step > 1)
  517. trace_sched_hmp_load(step,
  518. clbenv.bstats.load_avg, clbenv.lstats.load_avg);
  519. return new_cpu;
  520. }
  521. /*
  522. * Heterogenous Multi-Processor (HMP) - Task Runqueue Selection
  523. */
  524. /* This function enhances the original task selection function */
  525. static int hmp_select_task_rq_fair(int sd_flag, struct task_struct *p,
  526. int prev_cpu, int new_cpu)
  527. {
  528. struct list_head *pos;
  529. struct cpumask fast_cpu_mask, slow_cpu_mask;
  530. if (idle_cpu(new_cpu) && hmp_cpu_is_fastest(new_cpu))
  531. return new_cpu;
  532. /* error handling */
  533. if (prev_cpu >= num_possible_cpus())
  534. return new_cpu;
  535. /*
  536. * Skip all the checks if only one CPU is online.
  537. * Otherwise, select the most appropriate CPU from cluster.
  538. */
  539. if (num_online_cpus() == 1)
  540. goto out;
  541. cpumask_clear(&fast_cpu_mask);
  542. cpumask_clear(&slow_cpu_mask);
  543. /* order: fast to slow hmp domain */
  544. list_for_each(pos, &hmp_domains) {
  545. struct hmp_domain *domain;
  546. domain = list_entry(pos, struct hmp_domain, hmp_domains);
  547. if (cpumask_empty(&domain->cpus))
  548. continue;
  549. if (cpumask_empty(&fast_cpu_mask)) {
  550. cpumask_copy(&fast_cpu_mask, &domain->possible_cpus);
  551. } else {
  552. cpumask_copy(&slow_cpu_mask, &domain->possible_cpus);
  553. new_cpu = hmp_select_task_migration(sd_flag, p,
  554. prev_cpu, new_cpu, &fast_cpu_mask,
  555. &slow_cpu_mask);
  556. }
  557. }
  558. out:
  559. /* it happens when num_online_cpus=1 */
  560. if (new_cpu >= nr_cpu_ids) {
  561. /* BUG_ON(1); */
  562. new_cpu = prev_cpu;
  563. }
  564. return new_cpu;
  565. }
  566. #define hmp_fast_cpu_has_spare_cycles(B, cpu_load) \
  567. (cpu_load * capacity_margin < (SCHED_CAPACITY_SCALE * 1024))
  568. #define hmp_task_fast_cpu_afford(B, se, cpu) \
  569. (B->acap > 0 && hmp_fast_cpu_has_spare_cycles(B, \
  570. se_load(se) + cfs_load(cpu)))
  571. #define hmp_fast_cpu_oversubscribed(caller, B, se, cpu) \
  572. (hmp_caller_is_gb(caller) ? \
  573. !hmp_fast_cpu_has_spare_cycles(B, cfs_load(cpu)) : \
  574. !hmp_task_fast_cpu_afford(B, se, cpu))
  575. #define hmp_task_slow_cpu_afford(L, se) \
  576. (L->acap > 0 && L->acap >= se_load(se))
  577. /* Macro used by low-priority task filter */
  578. #define hmp_low_prio_task_up_rejected(p, B, L) \
  579. (task_low_priority(p->prio) && \
  580. (B->ntask >= B->ncpu || 0 != L->nr_normal_prio_task) && \
  581. (p->se.avg.loadwop_avg < 800))
  582. #define hmp_low_prio_task_down_allowed(p, B, L) \
  583. (task_low_priority(p->prio) && !B->nr_dequeuing_low_prio && \
  584. B->ntask >= B->ncpu && 0 != L->nr_normal_prio_task && \
  585. (p->se.avg.loadwop_avg < 800))
  586. /* Migration check result */
  587. #define HMP_BIG_NOT_OVERSUBSCRIBED (0x01)
  588. #define HMP_BIG_CAPACITY_INSUFFICIENT (0x02)
  589. #define HMP_LITTLE_CAPACITY_INSUFFICIENT (0x04)
  590. #define HMP_LOW_PRIORITY_FILTER (0x08)
  591. #define HMP_BIG_BUSY_LITTLE_IDLE (0x10)
  592. #define HMP_BIG_IDLE (0x20)
  593. #define HMP_MIGRATION_APPROVED (0x100)
  594. #define HMP_TASK_UP_MIGRATION (0x200)
  595. #define HMP_TASK_DOWN_MIGRATION (0x400)
  596. /* Migration statistics */
  597. struct hmp_statisic hmp_stats;
  598. /*
  599. * Check whether this task should be migrated to big
  600. * Briefly summarize the flow as below;
  601. * 1) Migration stabilizing
  602. * 2) Filter low-priority task
  603. * 2.5) Keep all cpu busy
  604. * 3) Check CPU capacity
  605. * 4) Check dynamic migration threshold
  606. */
  607. static unsigned int hmp_up_migration(int cpu,
  608. int *target_cpu, struct sched_entity *se,
  609. struct clb_env *clbenv)
  610. {
  611. struct task_struct *p = task_of(se);
  612. struct clb_stats *L, *B;
  613. struct mcheck *check;
  614. int curr_cpu = cpu;
  615. unsigned int caller = clbenv->flags;
  616. cpumask_t act_mask;
  617. L = &clbenv->lstats;
  618. B = &clbenv->bstats;
  619. check = &clbenv->mcheck;
  620. check->status = clbenv->flags;
  621. check->status |= HMP_TASK_UP_MIGRATION;
  622. check->result = 0;
  623. cpumask_andnot(&act_mask, cpu_active_mask, cpu_isolated_mask);
  624. /*
  625. * No migration is needed if
  626. * 1) There is only one cluster
  627. * 2) Task is already in big cluster
  628. * 3) It violates task affinity
  629. */
  630. if (!L->ncpu || !B->ncpu
  631. || cpumask_test_cpu(curr_cpu, &clbenv->bcpus)
  632. || !cpumask_intersects(&clbenv->bcpus,
  633. &p->cpus_allowed)
  634. || !cpumask_intersects(&clbenv->bcpus, &act_mask))
  635. goto out;
  636. /*
  637. * [1] Migration stabilizing
  638. * Let the task load settle before doing another up migration.
  639. * It can prevent a bunch of tasks from migrating to a unstable CPU.
  640. */
  641. if (!hmp_up_stable(*target_cpu))
  642. goto out;
  643. /* [2] Filter low-priority task */
  644. #ifdef CONFIG_SCHED_HMP_PRIO_FILTER
  645. if (hmp_low_prio_task_up_rejected(p, B, L)) {
  646. check->status |= HMP_LOW_PRIORITY_FILTER;
  647. goto trace;
  648. }
  649. #endif
  650. /* [2.5]if big is idle, just go to big */
  651. if (rq_length(*target_cpu) == 0) {
  652. check->status |= HMP_BIG_IDLE;
  653. check->status |= HMP_MIGRATION_APPROVED;
  654. check->result = 1;
  655. goto trace;
  656. }
  657. /*
  658. * [3] Check CPU capacity
  659. * Forbid up-migration if big CPU can't handle this task
  660. */
  661. if (!hmp_task_fast_cpu_afford(B, se, *target_cpu)) {
  662. check->status |= HMP_BIG_CAPACITY_INSUFFICIENT;
  663. goto trace;
  664. }
  665. /*
  666. * [4] Check dynamic migration threshold
  667. * Migrate task from LITTLE to big if load is greater than up-threshold
  668. */
  669. if (se_load(se) >= B->threshold) {
  670. check->status |= HMP_MIGRATION_APPROVED;
  671. check->result = 1;
  672. }
  673. trace:
  674. if (check->result && hmp_caller_is_gb(caller))
  675. hmp_stats.nr_force_up++;
  676. trace_sched_hmp_stats(&hmp_stats);
  677. trace_sched_dynamic_threshold(task_of(se), B->threshold, check->status,
  678. curr_cpu, *target_cpu, se_load(se), B, L);
  679. trace_sched_dynamic_threshold_draw(B->threshold, L->threshold);
  680. out:
  681. return check->result;
  682. }
  683. /*
  684. * Check whether this task should be migrated to LITTLE
  685. * Briefly summarize the flow as below;
  686. * 1) Migration stabilizing
  687. * 1.5) Keep all cpu busy
  688. * 2) Filter low-priority task
  689. * 3) Check CPU capacity
  690. * 4) Check dynamic migration threshold
  691. */
  692. static unsigned int hmp_down_migration(int cpu,
  693. int *target_cpu, struct sched_entity *se,
  694. struct clb_env *clbenv)
  695. {
  696. struct task_struct *p = task_of(se);
  697. struct clb_stats *L, *B;
  698. struct mcheck *check;
  699. int curr_cpu = cpu;
  700. unsigned int caller = clbenv->flags;
  701. cpumask_t act_mask;
  702. L = &clbenv->lstats;
  703. B = &clbenv->bstats;
  704. check = &clbenv->mcheck;
  705. check->status = caller;
  706. check->status |= HMP_TASK_DOWN_MIGRATION;
  707. check->result = 0;
  708. cpumask_andnot(&act_mask, cpu_active_mask, cpu_isolated_mask);
  709. /*
  710. * No migration is needed if
  711. * 1) There is only one cluster
  712. * 2) Task is already in LITTLE cluster
  713. * 3) It violates task affinity
  714. */
  715. if (!L->ncpu || !B->ncpu
  716. || cpumask_test_cpu(curr_cpu, &clbenv->lcpus)
  717. || !cpumask_intersects(&clbenv->lcpus,
  718. &p->cpus_allowed)
  719. || !cpumask_intersects(&clbenv->lcpus, &act_mask))
  720. goto out;
  721. /*
  722. * [1] Migration stabilizing
  723. * Let the task load settle before doing another down migration.
  724. * It can prevent a bunch of tasks from migrating to a unstable CPU.
  725. */
  726. if (!hmp_down_stable(*target_cpu))
  727. goto out;
  728. /* [1.5]if big is busy and little is idle, just go to little */
  729. if (rq_length(*target_cpu) == 0 && caller == HMP_SELECT_RQ
  730. && rq_length(curr_cpu) > 0) {
  731. struct rq *curr_rq = cpu_rq(curr_cpu);
  732. /*
  733. * If current big core is not heavy task,
  734. * and wake up task is heavy task.
  735. *
  736. * Dont go to little.
  737. */
  738. if (!(!is_heavy_task(curr_rq->curr) && is_heavy_task(p))) {
  739. check->status |= HMP_BIG_BUSY_LITTLE_IDLE;
  740. check->status |= HMP_MIGRATION_APPROVED;
  741. check->result = 1;
  742. goto trace;
  743. }
  744. }
  745. /* [2] Filter low-priority task */
  746. #ifdef CONFIG_SCHED_HMP_PRIO_FILTER
  747. if (hmp_low_prio_task_down_allowed(p, B, L)) {
  748. cfs_nr_dequeuing_low_prio(curr_cpu)++;
  749. check->status |= HMP_LOW_PRIORITY_FILTER;
  750. check->status |= HMP_MIGRATION_APPROVED;
  751. check->result = 1;
  752. goto trace;
  753. }
  754. #endif
  755. /*
  756. * [3] Check CPU capacity
  757. * Forbid down-migration if either of the following conditions is true
  758. * 1) big cpu is not oversubscribed (if big CPU seems to have spare
  759. * cycles, do not force this task to run on LITTLE CPU, but
  760. * keep it staying in its previous cluster instead)
  761. * 2) LITTLE cpu doesn't have available capacity for this new task
  762. */
  763. if (cpu_rq(curr_cpu)->cfs.h_nr_running > 1 &&
  764. !hmp_fast_cpu_oversubscribed(caller, B, se, curr_cpu)) {
  765. check->status |= HMP_BIG_NOT_OVERSUBSCRIBED;
  766. goto trace;
  767. }
  768. /*
  769. * [4] Check dynamic migration threshold
  770. * Migrate task from big to LITTLE if load ratio is less than
  771. * or equal to down-threshold
  772. */
  773. if (L->threshold >= se_load(se)) {
  774. check->status |= HMP_MIGRATION_APPROVED;
  775. check->result = 1;
  776. }
  777. trace:
  778. if (check->result && hmp_caller_is_gb(caller))
  779. hmp_stats.nr_force_down++;
  780. trace_sched_hmp_stats(&hmp_stats);
  781. trace_sched_dynamic_threshold(task_of(se), L->threshold, check->status,
  782. curr_cpu, *target_cpu, se_load(se), B, L);
  783. trace_sched_dynamic_threshold_draw(B->threshold, L->threshold);
  784. out:
  785. return check->result;
  786. }
  787. static int hmp_active_load_balance_cpu_stop(void *data)
  788. {
  789. int ret;
  790. struct task_struct *p = ((struct rq *)data)->migrate_task;
  791. ret = active_load_balance_cpu_stop(data);
  792. put_task_struct(p);
  793. return ret;
  794. }
  795. /*
  796. * According to Linaro's comment, we should only check the currently running
  797. * tasks because selecting other tasks for migration will require extensive
  798. * book keeping.
  799. */
  800. static void hmp_force_down_migration(int this_cpu)
  801. {
  802. int target_cpu;
  803. struct sched_entity *se;
  804. struct rq *target;
  805. unsigned long flags;
  806. unsigned int force = 0;
  807. struct task_struct *p;
  808. struct clb_env clbenv;
  809. #ifdef CONFIG_SCHED_HMP_PLUS
  810. struct sched_entity *orig;
  811. int B_cpu;
  812. #endif
  813. struct hmp_domain *hmp_domain = NULL;
  814. struct cpumask fast_cpu_mask, slow_cpu_mask;
  815. cpumask_clear(&fast_cpu_mask);
  816. cpumask_clear(&slow_cpu_mask);
  817. /* Migrate light task from big to LITTLE */
  818. if (!hmp_cpu_is_slowest(this_cpu)) {
  819. hmp_domain = hmp_cpu_domain(this_cpu);
  820. cpumask_copy(&fast_cpu_mask, &hmp_domain->possible_cpus);
  821. while (!list_is_last(&hmp_domain->hmp_domains, &hmp_domains)) {
  822. struct list_head *pos = &hmp_domain->hmp_domains;
  823. hmp_domain = list_entry(pos->next,
  824. struct hmp_domain, hmp_domains);
  825. if (!cpumask_empty(&hmp_domain->cpus)) {
  826. cpumask_copy(&slow_cpu_mask,
  827. &hmp_domain->possible_cpus);
  828. break;
  829. }
  830. }
  831. }
  832. if (!hmp_domain || hmp_domain == hmp_cpu_domain(this_cpu))
  833. return;
  834. if (cpumask_empty(&fast_cpu_mask) || cpumask_empty(&slow_cpu_mask))
  835. return;
  836. force = 0;
  837. target = cpu_rq(this_cpu);
  838. raw_spin_lock_irqsave(&target->lock, flags);
  839. se = target->cfs.curr;
  840. if (!se) {
  841. raw_spin_unlock_irqrestore(&target->lock, flags);
  842. return;
  843. }
  844. /* Find task entity */
  845. if (!entity_is_task(se)) {
  846. struct cfs_rq *cfs_rq;
  847. cfs_rq = group_cfs_rq(se);
  848. while (cfs_rq) {
  849. se = cfs_rq->curr;
  850. cfs_rq = group_cfs_rq(se);
  851. }
  852. }
  853. #ifdef CONFIG_SCHED_HMP_PLUS
  854. orig = se;
  855. se = hmp_get_lightest_task(orig, 1);
  856. if (!se) {
  857. raw_spin_unlock_irqrestore(&target->lock, flags);
  858. return;
  859. }
  860. if (!entity_is_task(se))
  861. p = task_of(orig);
  862. else
  863. #endif
  864. p = task_of(se);
  865. #ifdef CONFIG_SCHED_HMP_PLUS
  866. /*
  867. * Don't offload to little if there is one idle big,
  868. * let load balance to do it's work.
  869. * Also, to prevent idle_balance from leading to potential ping-pong
  870. */
  871. B_cpu = hmp_select_cpu(HMP_GB, p, &fast_cpu_mask, this_cpu, 0);
  872. if (B_cpu < nr_cpu_ids && !rq_length(B_cpu)) {
  873. raw_spin_unlock_irqrestore(&target->lock, flags);
  874. return;
  875. }
  876. #endif
  877. target_cpu = hmp_select_cpu(HMP_GB, p, &slow_cpu_mask, -1, 1);
  878. if (target_cpu >= num_possible_cpus()) {
  879. raw_spin_unlock_irqrestore(&target->lock, flags);
  880. return;
  881. }
  882. /* Collect cluster information */
  883. memset(&clbenv, 0, sizeof(clbenv));
  884. clbenv.flags |= HMP_GB;
  885. clbenv.btarget = this_cpu;
  886. clbenv.ltarget = target_cpu;
  887. cpumask_copy(&clbenv.lcpus, &slow_cpu_mask);
  888. cpumask_copy(&clbenv.bcpus, &fast_cpu_mask);
  889. sched_update_clbstats(&clbenv);
  890. #ifdef CONFIG_SCHED_HMP_PLUS
  891. if (cpu_rq(this_cpu)->cfs.h_nr_running < 2) {
  892. raw_spin_unlock_irqrestore(&target->lock, flags);
  893. return;
  894. }
  895. #endif
  896. /* Check migration threshold */
  897. if (!target->active_balance &&
  898. hmp_down_migration(this_cpu,
  899. &target_cpu, se, &clbenv) &&
  900. !cpu_park(cpu_of(target))) {
  901. if (p->state != TASK_DEAD) {
  902. get_task_struct(p);
  903. target->active_balance = MIGR_DOWN_MIGRATE;
  904. target->push_cpu = target_cpu;
  905. target->migrate_task = p;
  906. force = 1;
  907. trace_sched_hmp_migrate(p, target->push_cpu,
  908. MIGR_DOWN_MIGRATE);
  909. hmp_next_down_delay(&p->se, target->push_cpu);
  910. }
  911. }
  912. raw_spin_unlock_irqrestore(&target->lock, flags);
  913. if (force) {
  914. if (!stop_one_cpu_nowait(cpu_of(target),
  915. hmp_active_load_balance_cpu_stop,
  916. target, &target->active_balance_work)) {
  917. put_task_struct(p); /* out of rq->lock */
  918. raw_spin_lock_irqsave(&target->lock, flags);
  919. target->active_balance = 0;
  920. target->migrate_task = NULL;
  921. force = 0;
  922. raw_spin_unlock_irqrestore(&target->lock, flags);
  923. }
  924. }
  925. }
  926. /*
  927. * hmp_force_up_migration checks runqueues for tasks that need to
  928. * be actively migrated to a faster cpu.
  929. */
  930. static void hmp_force_up_migration(int this_cpu)
  931. {
  932. int curr_cpu, target_cpu;
  933. struct sched_entity *se;
  934. struct rq *target;
  935. unsigned long flags;
  936. unsigned int force = 0;
  937. struct task_struct *p;
  938. struct clb_env clbenv;
  939. #ifdef CONFIG_SCHED_HMP_PLUS
  940. struct sched_entity *orig;
  941. #endif
  942. if (!spin_trylock(&hmp_force_migration))
  943. return;
  944. /* Migrate heavy task from LITTLE to big */
  945. for_each_online_cpu(curr_cpu) {
  946. struct hmp_domain *hmp_domain = NULL;
  947. struct cpumask fast_cpu_mask, slow_cpu_mask;
  948. cpumask_clear(&fast_cpu_mask);
  949. cpumask_clear(&slow_cpu_mask);
  950. if (!hmp_cpu_is_fastest(curr_cpu)) {
  951. /* current cpu is slow_cpu_mask*/
  952. hmp_domain = hmp_cpu_domain(curr_cpu);
  953. cpumask_copy(&slow_cpu_mask,
  954. &hmp_domain->possible_cpus);
  955. while (&hmp_domain->hmp_domains != hmp_domains.next) {
  956. struct list_head *pos;
  957. pos = &hmp_domain->hmp_domains;
  958. hmp_domain = list_entry(pos->prev,
  959. struct hmp_domain, hmp_domains);
  960. if (cpumask_empty(&hmp_domain->cpus))
  961. continue;
  962. cpumask_copy(&fast_cpu_mask,
  963. &hmp_domain->possible_cpus);
  964. break;
  965. }
  966. } else {
  967. hmp_force_down_migration(curr_cpu);
  968. continue;
  969. }
  970. if (!hmp_domain || hmp_domain == hmp_cpu_domain(curr_cpu))
  971. continue;
  972. if (cpumask_empty(&fast_cpu_mask) ||
  973. cpumask_empty(&slow_cpu_mask))
  974. continue;
  975. force = 0;
  976. target = cpu_rq(curr_cpu);
  977. raw_spin_lock_irqsave(&target->lock, flags);
  978. se = target->cfs.curr;
  979. if (!se) {
  980. raw_spin_unlock_irqrestore(&target->lock, flags);
  981. continue;
  982. }
  983. /* Find task entity */
  984. if (!entity_is_task(se)) {
  985. struct cfs_rq *cfs_rq;
  986. cfs_rq = group_cfs_rq(se);
  987. while (cfs_rq) {
  988. se = cfs_rq->curr;
  989. cfs_rq = group_cfs_rq(se);
  990. }
  991. }
  992. #ifdef CONFIG_SCHED_HMP_PLUS
  993. orig = se;
  994. se = hmp_get_heaviest_task(se, -1);
  995. if (!se) {
  996. raw_spin_unlock_irqrestore(&target->lock, flags);
  997. continue;
  998. }
  999. if (!entity_is_task(se))
  1000. p = task_of(orig);
  1001. else
  1002. #endif
  1003. p = task_of(se);
  1004. target_cpu = hmp_select_cpu(HMP_GB, p, &fast_cpu_mask, -1, 0);
  1005. if (target_cpu >= num_possible_cpus()) {
  1006. raw_spin_unlock_irqrestore(&target->lock, flags);
  1007. continue;
  1008. }
  1009. /* Collect cluster information */
  1010. memset(&clbenv, 0, sizeof(clbenv));
  1011. clbenv.flags |= HMP_GB;
  1012. clbenv.ltarget = curr_cpu;
  1013. clbenv.btarget = target_cpu;
  1014. cpumask_copy(&clbenv.lcpus, &slow_cpu_mask);
  1015. cpumask_copy(&clbenv.bcpus, &fast_cpu_mask);
  1016. sched_update_clbstats(&clbenv);
  1017. /* Check migration threshold */
  1018. if (!target->active_balance &&
  1019. hmp_up_migration(curr_cpu,
  1020. &target_cpu, se, &clbenv) &&
  1021. !cpu_park(cpu_of(target))) {
  1022. if (p->state != TASK_DEAD) {
  1023. get_task_struct(p);
  1024. target->active_balance = MIGR_UP_MIGRATE;
  1025. target->push_cpu = target_cpu;
  1026. target->migrate_task = p;
  1027. force = 1;
  1028. trace_sched_hmp_migrate(p, target->push_cpu,
  1029. MIGR_UP_MIGRATE);
  1030. hmp_next_up_delay(&p->se, target->push_cpu);
  1031. }
  1032. }
  1033. raw_spin_unlock_irqrestore(&target->lock, flags);
  1034. if (force) {
  1035. if (!stop_one_cpu_nowait(cpu_of(target),
  1036. hmp_active_load_balance_cpu_stop,
  1037. target, &target->active_balance_work)) {
  1038. put_task_struct(p); /* out of rq->lock */
  1039. raw_spin_lock_irqsave(&target->lock, flags);
  1040. target->active_balance = 0;
  1041. target->migrate_task = NULL;
  1042. force = 0;
  1043. raw_spin_unlock_irqrestore(
  1044. &target->lock, flags);
  1045. }
  1046. } else
  1047. hmp_force_down_migration(this_cpu);
  1048. }
  1049. trace_sched_hmp_load(100,
  1050. clbenv.bstats.load_avg, clbenv.lstats.load_avg);
  1051. spin_unlock(&hmp_force_migration);
  1052. }
  1053. static inline void
  1054. hmp_enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
  1055. {
  1056. int cpu = cfs_rq->rq->cpu;
  1057. cfs_rq->avg.loadwop_avg += se->avg.loadwop_avg;
  1058. cfs_rq->avg.loadwop_sum += se->avg.loadwop_sum;
  1059. #ifdef CONFIG_SCHED_HMP_PRIO_FILTER
  1060. if (!task_low_priority(task_of(se)->prio))
  1061. cfs_nr_normal_prio(cpu)++;
  1062. #endif
  1063. trace_sched_cfs_enqueue_task(task_of(se), se_load(se), cpu);
  1064. }
  1065. static inline void
  1066. hmp_dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
  1067. {
  1068. int cpu = cfs_rq->rq->cpu;
  1069. cfs_reset_nr_dequeuing_low_prio(cpu);
  1070. if (!task_low_priority(task_of(se)->prio))
  1071. cfs_nr_normal_prio(cpu)--;
  1072. cfs_rq->avg.loadwop_avg = max_t(long,
  1073. cfs_rq->avg.loadwop_avg - se->avg.loadwop_avg, 0);
  1074. cfs_rq->avg.loadwop_sum = max_t(s64,
  1075. cfs_rq->avg.loadwop_sum - se->avg.loadwop_sum, 0);
  1076. trace_sched_cfs_dequeue_task(task_of(se), se_load(se), cfs_rq->rq->cpu);
  1077. }
  1078. /*
  1079. * hmp_idle_pull looks at little domain runqueues to see
  1080. * if a task should be pulled.
  1081. *
  1082. * Reuses hmp_force_migration spinlock.
  1083. *
  1084. */
  1085. static unsigned int hmp_idle_pull(int this_cpu)
  1086. {
  1087. int cpu;
  1088. struct sched_entity *curr, *orig;
  1089. struct hmp_domain *hmp_domain = NULL;
  1090. struct rq *target = NULL, *rq;
  1091. unsigned long flags, ratio = 0;
  1092. unsigned int moved = 0;
  1093. struct task_struct *p = NULL;
  1094. struct clb_env clbenv;
  1095. struct task_struct *prev_selected = NULL;
  1096. int selected = 0;
  1097. if (!hmp_cpu_is_slowest(this_cpu))
  1098. hmp_domain = hmp_slower_domain(this_cpu);
  1099. if (!hmp_domain)
  1100. return 0;
  1101. if (!spin_trylock(&hmp_force_migration))
  1102. return 0;
  1103. memset(&clbenv, 0, sizeof(clbenv));
  1104. clbenv.flags |= HMP_GB;
  1105. clbenv.btarget = this_cpu;
  1106. cpumask_copy(&clbenv.lcpus, &hmp_domain->possible_cpus);
  1107. cpumask_copy(&clbenv.bcpus, &hmp_cpu_domain(this_cpu)->possible_cpus);
  1108. /* first select a task */
  1109. for_each_cpu(cpu, &hmp_domain->cpus) {
  1110. rq = cpu_rq(cpu);
  1111. raw_spin_lock_irqsave(&rq->lock, flags);
  1112. curr = rq->cfs.curr;
  1113. if (!curr) {
  1114. raw_spin_unlock_irqrestore(&rq->lock, flags);
  1115. continue;
  1116. }
  1117. if (!entity_is_task(curr)) {
  1118. struct cfs_rq *cfs_rq;
  1119. cfs_rq = group_cfs_rq(curr);
  1120. while (cfs_rq) {
  1121. curr = cfs_rq->curr;
  1122. if (!entity_is_task(curr))
  1123. cfs_rq = group_cfs_rq(curr);
  1124. else
  1125. cfs_rq = NULL;
  1126. }
  1127. }
  1128. orig = curr;
  1129. curr = hmp_get_heaviest_task(curr, this_cpu);
  1130. /* check if heaviest eligible task on this
  1131. * CPU is heavier than previous task
  1132. */
  1133. clbenv.ltarget = cpu;
  1134. sched_update_clbstats(&clbenv);
  1135. if (curr && entity_is_task(curr) &&
  1136. (se_load(curr) > clbenv.bstats.threshold) &&
  1137. (se_load(curr) > ratio) &&
  1138. cpumask_test_cpu(this_cpu,
  1139. &task_of(curr)->cpus_allowed)) {
  1140. selected = 1;
  1141. /* get task and selection inside rq lock */
  1142. p = task_of(curr);
  1143. get_task_struct(p);
  1144. target = rq;
  1145. ratio = curr->avg.loadwop_avg;
  1146. }
  1147. raw_spin_unlock_irqrestore(&rq->lock, flags);
  1148. if (selected) {
  1149. if (prev_selected) /* To put task out of rq lock */
  1150. put_task_struct(prev_selected);
  1151. prev_selected = p;
  1152. selected = 0;
  1153. }
  1154. }
  1155. if (!p)
  1156. goto done;
  1157. moved = migrate_running_task(this_cpu, p, target);
  1158. done:
  1159. spin_unlock(&hmp_force_migration);
  1160. if (p)
  1161. put_task_struct(p);
  1162. return moved;
  1163. }
  1164. /* must hold runqueue lock for queue se is currently on */
  1165. static const int hmp_max_tasks = 5;
  1166. static struct sched_entity *hmp_get_heaviest_task(
  1167. struct sched_entity *se, int target_cpu)
  1168. {
  1169. int num_tasks = hmp_max_tasks;
  1170. struct sched_entity *max_se = se;
  1171. long int max_ratio = se->avg.loadwop_avg;
  1172. const struct cpumask *hmp_target_mask = NULL;
  1173. struct hmp_domain *hmp;
  1174. if (hmp_cpu_is_fastest(cpu_of(se->cfs_rq->rq)))
  1175. return max_se;
  1176. if (!task_prefer_little(task_of(se))) {
  1177. max_se = se;
  1178. max_ratio = se->avg.loadwop_avg;
  1179. }
  1180. hmp = hmp_faster_domain(cpu_of(se->cfs_rq->rq));
  1181. hmp_target_mask = &hmp->cpus;
  1182. if (target_cpu >= 0) {
  1183. /* idle_balance gets run on a CPU while
  1184. * it is in the middle of being hotplugged
  1185. * out. Bail early in that case.
  1186. */
  1187. if (!cpumask_test_cpu(target_cpu, hmp_target_mask))
  1188. return NULL;
  1189. hmp_target_mask = cpumask_of(target_cpu);
  1190. }
  1191. /* The currently running task is not on the runqueue */
  1192. se = __pick_first_entity(cfs_rq_of(se));
  1193. while (num_tasks && se) {
  1194. if (entity_is_task(se) && se->avg.loadwop_avg > max_ratio &&
  1195. cpumask_intersects(hmp_target_mask,
  1196. &task_of(se)->cpus_allowed)) {
  1197. max_se = se;
  1198. max_ratio = se->avg.loadwop_avg;
  1199. }
  1200. se = __pick_next_entity(se);
  1201. num_tasks--;
  1202. }
  1203. return max_se;
  1204. }
  1205. static struct sched_entity *hmp_get_lightest_task(
  1206. struct sched_entity *se, int migrate_down)
  1207. {
  1208. int num_tasks = hmp_max_tasks;
  1209. struct sched_entity *min_se = 0;
  1210. unsigned long int min_ratio = INT_MAX;
  1211. const struct cpumask *hmp_target_mask = NULL;
  1212. if (migrate_down) {
  1213. struct hmp_domain *hmp;
  1214. if (hmp_cpu_is_slowest(cpu_of(se->cfs_rq->rq)))
  1215. return min_se;
  1216. hmp = hmp_slower_domain(cpu_of(se->cfs_rq->rq));
  1217. hmp_target_mask = &hmp->cpus;
  1218. }
  1219. if (!task_prefer_big(task_of(se))) {
  1220. min_se = se;
  1221. min_ratio = se->avg.loadwop_avg;
  1222. }
  1223. /* The currently running task is not on the runqueue */
  1224. se = __pick_first_entity(cfs_rq_of(se));
  1225. while (num_tasks && se) {
  1226. if (entity_is_task(se) &&
  1227. (se->avg.loadwop_avg < min_ratio
  1228. && hmp_target_mask &&
  1229. cpumask_intersects(hmp_target_mask,
  1230. &task_of(se)->cpus_allowed))) {
  1231. min_se = se;
  1232. min_ratio = se->avg.loadwop_avg;
  1233. }
  1234. se = __pick_next_entity(se);
  1235. num_tasks--;
  1236. }
  1237. return min_se;
  1238. }
  1239. inline int hmp_fork_balance(struct task_struct *p, int prev_cpu)
  1240. {
  1241. int new_cpu = prev_cpu;
  1242. int cpu = smp_processor_id();
  1243. if (hmp_cpu_is_fastest(prev_cpu)) {
  1244. /* prev_cpu is fastest domain */
  1245. struct hmp_domain *hmpdom;
  1246. __always_unused int lowest_ratio;
  1247. hmpdom = list_entry(
  1248. &hmp_cpu_domain(prev_cpu)->hmp_domains,
  1249. struct hmp_domain, hmp_domains);
  1250. lowest_ratio = hmp_domain_min_load(hmpdom, &new_cpu);
  1251. if (new_cpu < nr_cpu_ids &&
  1252. cpumask_test_cpu(new_cpu, &p->cpus_allowed)
  1253. && !cpu_isolated(new_cpu))
  1254. return new_cpu;
  1255. new_cpu = cpumask_any_and(&hmp_faster_domain(cpu)->cpus,
  1256. &p->cpus_allowed);
  1257. if (new_cpu < nr_cpu_ids)
  1258. return new_cpu;
  1259. } else {
  1260. /* prev_cpu is not fastest domain */
  1261. new_cpu = hmp_select_faster_cpu(p, prev_cpu);
  1262. if (new_cpu < nr_cpu_ids)
  1263. return new_cpu;
  1264. }
  1265. return new_cpu;
  1266. }
  1267. #endif
  1268. #ifdef CONFIG_HMP_FREQUENCY_INVARIANT_SCALE
  1269. #include <linux/cpufreq.h>
  1270. static int cpufreq_callback(struct notifier_block *nb,
  1271. unsigned long val, void *data)
  1272. {
  1273. struct cpufreq_freqs *freq = data;
  1274. int cpu = freq->cpu;
  1275. struct cpumask cls_cpus;
  1276. int id;
  1277. if (freq->flags & CPUFREQ_CONST_LOOPS)
  1278. return NOTIFY_OK;
  1279. if (val == CPUFREQ_PRECHANGE) {
  1280. arch_get_cluster_cpus(&cls_cpus, arch_get_cluster_id(cpu));
  1281. for_each_cpu(id, &cls_cpus)
  1282. arch_scale_set_curr_freq(id, freq->new);
  1283. }
  1284. return NOTIFY_OK;
  1285. }
  1286. static struct notifier_block cpufreq_notifier = {
  1287. .notifier_call = cpufreq_callback,
  1288. };
  1289. static int cpufreq_policy_callback(struct notifier_block *nb,
  1290. unsigned long val, void *data)
  1291. {
  1292. struct cpufreq_policy *policy = data;
  1293. int i;
  1294. if (val != CPUFREQ_NOTIFY)
  1295. return NOTIFY_OK;
  1296. for_each_cpu(i, policy->cpus) {
  1297. arch_scale_set_curr_freq(i, policy->cur);
  1298. arch_scale_set_max_freq(i, policy->max);
  1299. arch_scale_set_min_freq(i, policy->min);
  1300. }
  1301. return NOTIFY_OK;
  1302. }
  1303. static struct notifier_block cpufreq_policy_notifier = {
  1304. .notifier_call = cpufreq_policy_callback,
  1305. };
  1306. static int __init register_cpufreq_notifier(void)
  1307. {
  1308. int ret;
  1309. ret = cpufreq_register_notifier(&cpufreq_notifier,
  1310. CPUFREQ_TRANSITION_NOTIFIER);
  1311. if (ret)
  1312. return ret;
  1313. return cpufreq_register_notifier(&cpufreq_policy_notifier,
  1314. CPUFREQ_POLICY_NOTIFIER);
  1315. }
  1316. core_initcall(register_cpufreq_notifier);
  1317. #endif /* CONFIG_HMP_FREQUENCY_INVARIANT_SCALE */