walt_lb.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <trace/hooks/sched.h>
  7. #include "walt.h"
  8. #include "trace.h"
  9. static inline unsigned long walt_lb_cpu_util(int cpu)
  10. {
  11. struct walt_rq *wrq = &per_cpu(walt_rq, cpu);
  12. return wrq->walt_stats.cumulative_runnable_avg_scaled;
  13. }
  14. static void walt_detach_task(struct task_struct *p, struct rq *src_rq,
  15. struct rq *dst_rq)
  16. {
  17. //TODO can we just replace with detach_task in fair.c??
  18. deactivate_task(src_rq, p, 0);
  19. set_task_cpu(p, dst_rq->cpu);
  20. }
  21. static void walt_attach_task(struct task_struct *p, struct rq *rq)
  22. {
  23. activate_task(rq, p, 0);
  24. check_preempt_curr(rq, p, 0);
  25. }
  26. static int stop_walt_lb_active_migration(void *data)
  27. {
  28. struct rq *busiest_rq = data;
  29. int busiest_cpu = cpu_of(busiest_rq);
  30. int target_cpu = busiest_rq->push_cpu;
  31. struct rq *target_rq = cpu_rq(target_cpu);
  32. struct walt_rq *wrq = &per_cpu(walt_rq, busiest_cpu);
  33. struct task_struct *push_task;
  34. int push_task_detached = 0;
  35. raw_spin_lock_irq(&busiest_rq->__lock);
  36. push_task = wrq->push_task;
  37. /* sanity checks before initiating the pull */
  38. if (!cpu_active(busiest_cpu) || !cpu_active(target_cpu) || !push_task)
  39. goto out_unlock;
  40. if (unlikely(busiest_cpu != raw_smp_processor_id() ||
  41. !busiest_rq->active_balance))
  42. goto out_unlock;
  43. if (busiest_rq->nr_running <= 1)
  44. goto out_unlock;
  45. BUG_ON(busiest_rq == target_rq);
  46. if (task_on_rq_queued(push_task) &&
  47. READ_ONCE(push_task->__state) == TASK_RUNNING &&
  48. task_cpu(push_task) == busiest_cpu &&
  49. cpu_active(target_cpu) &&
  50. cpumask_test_cpu(target_cpu, push_task->cpus_ptr)) {
  51. walt_detach_task(push_task, busiest_rq, target_rq);
  52. push_task_detached = 1;
  53. }
  54. out_unlock: /* called with busiest_rq lock */
  55. busiest_rq->active_balance = 0;
  56. target_cpu = busiest_rq->push_cpu;
  57. clear_reserved(target_cpu);
  58. wrq->push_task = NULL;
  59. raw_spin_unlock(&busiest_rq->__lock);
  60. if (push_task_detached) {
  61. raw_spin_lock(&target_rq->__lock);
  62. walt_attach_task(push_task, target_rq);
  63. raw_spin_unlock(&target_rq->__lock);
  64. }
  65. if (push_task)
  66. put_task_struct(push_task);
  67. local_irq_enable();
  68. return 0;
  69. }
  70. struct walt_lb_rotate_work {
  71. struct work_struct w;
  72. struct task_struct *src_task;
  73. struct task_struct *dst_task;
  74. int src_cpu;
  75. int dst_cpu;
  76. };
  77. DEFINE_PER_CPU(struct walt_lb_rotate_work, walt_lb_rotate_works);
  78. static void walt_lb_rotate_work_func(struct work_struct *work)
  79. {
  80. struct walt_lb_rotate_work *wr = container_of(work,
  81. struct walt_lb_rotate_work, w);
  82. struct rq *src_rq = cpu_rq(wr->src_cpu), *dst_rq = cpu_rq(wr->dst_cpu);
  83. unsigned long flags;
  84. migrate_swap(wr->src_task, wr->dst_task, wr->dst_cpu, wr->src_cpu);
  85. put_task_struct(wr->src_task);
  86. put_task_struct(wr->dst_task);
  87. local_irq_save(flags);
  88. double_rq_lock(src_rq, dst_rq);
  89. dst_rq->active_balance = 0;
  90. src_rq->active_balance = 0;
  91. double_rq_unlock(src_rq, dst_rq);
  92. local_irq_restore(flags);
  93. clear_reserved(wr->src_cpu);
  94. clear_reserved(wr->dst_cpu);
  95. }
  96. static void walt_lb_rotate_work_init(void)
  97. {
  98. int i;
  99. for_each_possible_cpu(i) {
  100. struct walt_lb_rotate_work *wr = &per_cpu(walt_lb_rotate_works, i);
  101. INIT_WORK(&wr->w, walt_lb_rotate_work_func);
  102. }
  103. }
  104. #define WALT_ROTATION_THRESHOLD_NS 16000000
  105. static void walt_lb_check_for_rotation(struct rq *src_rq)
  106. {
  107. u64 wc, wait, max_wait = 0, run, max_run = 0;
  108. int deserved_cpu = nr_cpu_ids, dst_cpu = nr_cpu_ids;
  109. int i, src_cpu = cpu_of(src_rq);
  110. struct rq *dst_rq;
  111. struct walt_lb_rotate_work *wr = NULL;
  112. struct walt_task_struct *wts;
  113. if (!is_min_possible_cluster_cpu(src_cpu))
  114. return;
  115. /*
  116. * Use src_rq->clock directly instead of rq_clock() since
  117. * we do not have the rq lock and
  118. * src_rq->clock was updated in the tick callpath.
  119. */
  120. wc = src_rq->clock;
  121. for_each_possible_cpu(i) {
  122. struct rq *rq = cpu_rq(i);
  123. if (!is_min_possible_cluster_cpu(i))
  124. break;
  125. if (is_reserved(i))
  126. continue;
  127. if (!rq->misfit_task_load || !walt_fair_task(rq->curr))
  128. continue;
  129. wts = (struct walt_task_struct *) rq->curr->android_vendor_data1;
  130. wait = wc - wts->last_enqueued_ts;
  131. if (wait > max_wait) {
  132. max_wait = wait;
  133. deserved_cpu = i;
  134. }
  135. }
  136. if (deserved_cpu != src_cpu)
  137. return;
  138. for_each_possible_cpu(i) {
  139. struct rq *rq = cpu_rq(i);
  140. if (is_min_possible_cluster_cpu(i))
  141. continue;
  142. if (is_reserved(i))
  143. continue;
  144. if (!walt_fair_task(rq->curr))
  145. continue;
  146. if (rq->nr_running > 1)
  147. continue;
  148. wts = (struct walt_task_struct *) rq->curr->android_vendor_data1;
  149. run = wc - wts->last_enqueued_ts;
  150. if (run < WALT_ROTATION_THRESHOLD_NS)
  151. continue;
  152. if (run > max_run) {
  153. max_run = run;
  154. dst_cpu = i;
  155. }
  156. }
  157. if (dst_cpu == nr_cpu_ids)
  158. return;
  159. dst_rq = cpu_rq(dst_cpu);
  160. double_rq_lock(src_rq, dst_rq);
  161. if (walt_fair_task(dst_rq->curr) &&
  162. !src_rq->active_balance && !dst_rq->active_balance &&
  163. cpumask_test_cpu(dst_cpu, src_rq->curr->cpus_ptr) &&
  164. cpumask_test_cpu(src_cpu, dst_rq->curr->cpus_ptr)) {
  165. get_task_struct(src_rq->curr);
  166. get_task_struct(dst_rq->curr);
  167. mark_reserved(src_cpu);
  168. mark_reserved(dst_cpu);
  169. wr = &per_cpu(walt_lb_rotate_works, src_cpu);
  170. wr->src_task = src_rq->curr;
  171. wr->dst_task = dst_rq->curr;
  172. wr->src_cpu = src_cpu;
  173. wr->dst_cpu = dst_cpu;
  174. dst_rq->active_balance = 1;
  175. src_rq->active_balance = 1;
  176. }
  177. double_rq_unlock(src_rq, dst_rq);
  178. if (wr)
  179. queue_work_on(src_cpu, system_highpri_wq, &wr->w);
  180. }
  181. static inline bool _walt_can_migrate_task(struct task_struct *p, int dst_cpu,
  182. bool to_lower, bool to_higher, bool force)
  183. {
  184. struct walt_rq *wrq = &per_cpu(walt_rq, task_cpu(p));
  185. struct walt_task_struct *wts = (struct walt_task_struct *) p->android_vendor_data1;
  186. /* Don't detach task if it is under active migration */
  187. if (wrq->push_task == p)
  188. return false;
  189. if (to_lower) {
  190. if (wts->iowaited)
  191. return false;
  192. if (per_task_boost(p) == TASK_BOOST_STRICT_MAX &&
  193. task_in_related_thread_group(p))
  194. return false;
  195. if (walt_pipeline_low_latency_task(p))
  196. return false;
  197. if (!force && walt_get_rtg_status(p))
  198. return false;
  199. if (!force && !task_fits_max(p, dst_cpu))
  200. return false;
  201. } else if (!to_higher) {
  202. if (!task_fits_max(p, dst_cpu) &&
  203. wrq->walt_stats.nr_big_tasks < 2)
  204. return false;
  205. }
  206. /* Don't detach task if dest cpu is halted */
  207. if (cpu_halted(dst_cpu))
  208. return false;
  209. return true;
  210. }
  211. static inline bool need_active_lb(struct task_struct *p, int dst_cpu,
  212. int src_cpu)
  213. {
  214. struct walt_task_struct *wts = (struct walt_task_struct *) p->android_vendor_data1;
  215. if (cpu_rq(src_cpu)->active_balance)
  216. return false;
  217. if (!check_for_higher_capacity(dst_cpu, src_cpu))
  218. return false;
  219. if (!wts->misfit)
  220. return false;
  221. if (!is_min_possible_cluster_cpu(src_cpu) && !task_fits_max(p, dst_cpu))
  222. return false;
  223. if (task_reject_partialhalt_cpu(p, dst_cpu))
  224. return false;
  225. /*
  226. * If the sleeping task on the dst_cpu and the task for which we are
  227. * doing active load balance, are pipeline tasks then don't do active
  228. * load balance. If we allow this, the sleeping task might wakeup
  229. * again on dst_cpu before the migration of actively pulled task. This
  230. * will result in two pipeline tasks on the same cpu
  231. */
  232. if (walt_pipeline_low_latency_task(p) &&
  233. walt_pipeline_low_latency_task(cpu_rq(dst_cpu)->curr))
  234. return false;
  235. return true;
  236. }
  237. static int walt_lb_pull_tasks(int dst_cpu, int src_cpu, struct task_struct **pulled_task_struct)
  238. {
  239. struct rq *dst_rq = cpu_rq(dst_cpu);
  240. struct rq *src_rq = cpu_rq(src_cpu);
  241. unsigned long flags;
  242. struct task_struct *p;
  243. bool active_balance = false, to_lower, to_higher;
  244. struct walt_rq *src_wrq = &per_cpu(walt_rq, src_cpu);
  245. struct walt_task_struct *wts;
  246. struct task_struct *pull_me;
  247. int task_visited;
  248. BUG_ON(src_cpu == dst_cpu);
  249. to_lower = check_for_higher_capacity(src_cpu, dst_cpu);
  250. to_higher = check_for_higher_capacity(dst_cpu, src_cpu);
  251. raw_spin_lock_irqsave(&src_rq->__lock, flags);
  252. pull_me = NULL;
  253. task_visited = 0;
  254. list_for_each_entry_reverse(p, &src_rq->cfs_tasks, se.group_node) {
  255. if (!cpumask_test_cpu(dst_cpu, p->cpus_ptr))
  256. continue;
  257. if (task_on_cpu(src_rq, p))
  258. continue;
  259. if (!_walt_can_migrate_task(p, dst_cpu, to_lower, to_higher,
  260. false))
  261. continue;
  262. if (pull_me == NULL) {
  263. pull_me = p;
  264. } else {
  265. if (to_lower) {
  266. if (task_util(p) < task_util(pull_me))
  267. pull_me = p;
  268. } else if (task_util(p) > task_util(pull_me)) {
  269. pull_me = p;
  270. }
  271. }
  272. task_visited++;
  273. if (task_visited > 5)
  274. break;
  275. }
  276. if (pull_me) {
  277. walt_detach_task(pull_me, src_rq, dst_rq);
  278. goto unlock;
  279. }
  280. pull_me = NULL;
  281. task_visited = 0;
  282. list_for_each_entry_reverse(p, &src_rq->cfs_tasks, se.group_node) {
  283. if (!cpumask_test_cpu(dst_cpu, p->cpus_ptr))
  284. continue;
  285. if (task_on_cpu(src_rq, p))
  286. continue;
  287. if (!_walt_can_migrate_task(p, dst_cpu, to_lower, to_higher,
  288. true))
  289. continue;
  290. if (pull_me == NULL) {
  291. pull_me = p;
  292. } else {
  293. if (to_lower) {
  294. if (task_util(p) < task_util(pull_me))
  295. pull_me = p;
  296. } else if (task_util(p) > task_util(pull_me)) {
  297. pull_me = p;
  298. }
  299. }
  300. task_visited++;
  301. if (task_visited > 5)
  302. break;
  303. }
  304. if (pull_me) {
  305. walt_detach_task(pull_me, src_rq, dst_rq);
  306. goto unlock;
  307. }
  308. list_for_each_entry_reverse(p, &src_rq->cfs_tasks, se.group_node) {
  309. if (task_on_cpu(src_rq, p)) {
  310. if (cpumask_test_cpu(dst_cpu, p->cpus_ptr)
  311. && need_active_lb(p, dst_cpu, src_cpu)) {
  312. bool success;
  313. active_balance = true;
  314. src_rq->active_balance = 1;
  315. src_rq->push_cpu = dst_cpu;
  316. get_task_struct(p);
  317. src_wrq->push_task = p;
  318. mark_reserved(dst_cpu);
  319. /* lock must be dropped before waking the stopper */
  320. raw_spin_unlock_irqrestore(&src_rq->__lock, flags);
  321. /*
  322. * Using our custom active load balance callback so that
  323. * the push_task is really pulled onto this CPU.
  324. */
  325. wts = (struct walt_task_struct *) p->android_vendor_data1;
  326. trace_walt_active_load_balance(p, src_cpu, dst_cpu, wts);
  327. success = stop_one_cpu_nowait(src_cpu,
  328. stop_walt_lb_active_migration,
  329. src_rq, &src_rq->active_balance_work);
  330. if (!success)
  331. clear_reserved(dst_cpu);
  332. return 0; /* we did not pull any task here */
  333. }
  334. goto unlock;
  335. }
  336. }
  337. unlock:
  338. /* lock must be dropped before waking the stopper */
  339. raw_spin_unlock_irqrestore(&src_rq->__lock, flags);
  340. if (!pull_me)
  341. return 0;
  342. raw_spin_lock_irqsave(&dst_rq->__lock, flags);
  343. walt_attach_task(pull_me, dst_rq);
  344. raw_spin_unlock_irqrestore(&dst_rq->__lock, flags);
  345. *pulled_task_struct = pull_me;
  346. return 1; /* we pulled 1 task */
  347. }
  348. #define SMALL_TASK_THRESHOLD 102
  349. /*
  350. * find_first_idle_if_others_are_busy
  351. *
  352. * Get an idle cpu in the middle clusters
  353. *
  354. * Returns -1 if there are no idle cpus in the mask, or if there
  355. * is a CPU that is about to be come newly idle. Returns <cpu>
  356. * if there is an idle cpu in a cluster that's not about to do
  357. * newly idle load balancing.
  358. * Also returns -1 if called on a single or dual cluster system
  359. */
  360. static int find_first_idle_if_others_are_busy(void)
  361. {
  362. int i, first_idle = -1;
  363. struct cpumask src_mask;
  364. cpumask_clear(&src_mask);
  365. for (i = 0; i < num_sched_clusters; i++) {
  366. if (i == 0 || i == num_sched_clusters - 1)
  367. continue;
  368. else
  369. cpumask_or(&src_mask, &src_mask, &cpu_array[0][i]);
  370. }
  371. for_each_cpu(i, &src_mask) {
  372. if (!cpu_active(i))
  373. continue;
  374. if (cpu_halted(i))
  375. continue;
  376. if (available_idle_cpu(i))
  377. first_idle = i;
  378. if (cpu_util(i) < SMALL_TASK_THRESHOLD && cpu_rq(i)->nr_running == 1) {
  379. /*
  380. * there was one CPU in the mask that was almost idle, but not
  381. * quite. when it becomes idle, it will do newidle load-balance,
  382. * and start off with it's own cluster. So no reason to kick
  383. * anything in this cluster. i.e. don't perform a wasted kick.
  384. */
  385. first_idle = -1;
  386. /* return -1, a cpu in this cluster is about to do
  387. * newly idle load balancing
  388. */
  389. break;
  390. }
  391. }
  392. return first_idle;
  393. }
  394. static int walt_lb_find_busiest_similar_cap_cpu(int dst_cpu, const cpumask_t *src_mask,
  395. int *has_misfit, bool is_newidle)
  396. {
  397. int i;
  398. int busiest_cpu = -1;
  399. unsigned long util, busiest_util = 0;
  400. struct walt_rq *wrq;
  401. for_each_cpu(i, src_mask) {
  402. wrq = &per_cpu(walt_rq, i);
  403. trace_walt_lb_cpu_util(i, wrq);
  404. if (cpu_rq(i)->nr_running < 2 || !cpu_rq(i)->cfs.h_nr_running)
  405. continue;
  406. util = walt_lb_cpu_util(i);
  407. if (util < busiest_util)
  408. continue;
  409. busiest_util = util;
  410. busiest_cpu = i;
  411. }
  412. return busiest_cpu;
  413. }
  414. static int walt_lb_find_busiest_from_higher_cap_cpu(int dst_cpu, const cpumask_t *src_mask,
  415. int *has_misfit, bool is_newidle)
  416. {
  417. int i;
  418. int busiest_cpu = -1;
  419. unsigned long util, busiest_util = 0;
  420. unsigned long total_capacity = 0, total_util = 0, total_nr = 0;
  421. int total_cpus = 0;
  422. struct walt_rq *wrq;
  423. bool asymcap_boost = ASYMCAP_BOOST(dst_cpu);
  424. if (cpu_partial_halted(dst_cpu))
  425. return -1;
  426. for_each_cpu(i, src_mask) {
  427. if (!cpu_active(i))
  428. continue;
  429. wrq = &per_cpu(walt_rq, i);
  430. trace_walt_lb_cpu_util(i, wrq);
  431. util = walt_lb_cpu_util(i);
  432. total_cpus += 1;
  433. total_util += util;
  434. total_capacity += capacity_orig_of(i);
  435. total_nr += cpu_rq(i)->cfs.h_nr_running;
  436. if (cpu_rq(i)->cfs.h_nr_running < 2)
  437. continue;
  438. if (cpu_rq(i)->cfs.h_nr_running == 2 &&
  439. task_util(cpu_rq(i)->curr) < SMALL_TASK_THRESHOLD)
  440. continue;
  441. /*
  442. * During rotation, two silver fmax tasks gets
  443. * placed on gold/prime and the CPU may not be
  444. * overutilized but for rotation, we have to
  445. * spread out.
  446. */
  447. if (!walt_rotation_enabled && !cpu_overutilized(i) &&
  448. !asymcap_boost)
  449. continue;
  450. if (util < busiest_util)
  451. continue;
  452. busiest_util = util;
  453. busiest_cpu = i;
  454. }
  455. /*
  456. * Don't allow migrating to lower cluster unless this high
  457. * capacity cluster is sufficiently loaded.
  458. */
  459. if (!walt_rotation_enabled && !asymcap_boost) {
  460. if (total_nr <= total_cpus || total_util * 1280 < total_capacity * 1024)
  461. busiest_cpu = -1;
  462. }
  463. return busiest_cpu;
  464. }
  465. static int walt_lb_find_busiest_from_lower_cap_cpu(int dst_cpu, const cpumask_t *src_mask,
  466. int *has_misfit, bool is_newidle)
  467. {
  468. int i;
  469. int busiest_cpu = -1;
  470. unsigned long util, busiest_util = 0;
  471. unsigned long total_capacity = 0, total_util = 0, total_nr = 0;
  472. int total_cpus = 0;
  473. int busy_nr_big_tasks = 0;
  474. struct walt_rq *wrq;
  475. bool treat_dst_idle = is_newidle || available_idle_cpu(dst_cpu);
  476. /*
  477. * A higher capacity CPU is looking at a lower capacity
  478. * cluster. active balance and big tasks are in play.
  479. * other than that, it is very much same as above. we
  480. * really don't need this as a separate block. will
  481. * refactor this after final testing is done.
  482. */
  483. for_each_cpu(i, src_mask) {
  484. wrq = &per_cpu(walt_rq, i);
  485. if (!cpu_active(i))
  486. continue;
  487. trace_walt_lb_cpu_util(i, wrq);
  488. util = walt_lb_cpu_util(i);
  489. total_cpus += 1;
  490. total_util += util;
  491. total_capacity += capacity_orig_of(i);
  492. total_nr += cpu_rq(i)->cfs.h_nr_running;
  493. /*
  494. * no point in selecting this CPU as busy, as
  495. * active balance is in progress.
  496. */
  497. if (cpu_rq(i)->active_balance)
  498. continue;
  499. /* active migration is allowed only to idle cpu */
  500. if (cpu_rq(i)->cfs.h_nr_running < 2 &&
  501. (!wrq->walt_stats.nr_big_tasks || !treat_dst_idle))
  502. continue;
  503. if (!walt_rotation_enabled && !cpu_overutilized(i) &&
  504. !ASYMCAP_BOOST(i))
  505. continue;
  506. if (util < busiest_util)
  507. continue;
  508. busiest_util = util;
  509. busiest_cpu = i;
  510. busy_nr_big_tasks = wrq->walt_stats.nr_big_tasks;
  511. }
  512. if (!walt_rotation_enabled && !busy_nr_big_tasks &&
  513. !(busiest_cpu != -1 && ASYMCAP_BOOST(busiest_cpu))) {
  514. if (total_nr <= total_cpus || total_util * 1280 < total_capacity * 1024)
  515. busiest_cpu = -1;
  516. }
  517. if (busy_nr_big_tasks && busiest_cpu != -1)
  518. *has_misfit = true;
  519. return busiest_cpu;
  520. }
  521. static int walt_lb_find_busiest_cpu(int dst_cpu, const cpumask_t *src_mask, int *has_misfit,
  522. bool is_newidle)
  523. {
  524. int fsrc_cpu = cpumask_first(src_mask);
  525. int busiest_cpu;
  526. if (check_for_higher_capacity(dst_cpu, fsrc_cpu))
  527. busiest_cpu = walt_lb_find_busiest_from_lower_cap_cpu(dst_cpu,
  528. src_mask, has_misfit, is_newidle);
  529. else if (check_for_higher_capacity(fsrc_cpu, dst_cpu))
  530. busiest_cpu = walt_lb_find_busiest_from_higher_cap_cpu(dst_cpu,
  531. src_mask, has_misfit, is_newidle);
  532. else
  533. busiest_cpu = walt_lb_find_busiest_similar_cap_cpu(dst_cpu,
  534. src_mask, has_misfit, is_newidle);
  535. return busiest_cpu;
  536. }
  537. static DEFINE_RAW_SPINLOCK(walt_lb_migration_lock);
  538. void walt_lb_tick(struct rq *rq)
  539. {
  540. int prev_cpu = rq->cpu, new_cpu, ret;
  541. struct task_struct *p = rq->curr;
  542. unsigned long flags;
  543. struct walt_rq *prev_wrq = &per_cpu(walt_rq, cpu_of(rq));
  544. struct walt_task_struct *wts = (struct walt_task_struct *) p->android_vendor_data1;
  545. raw_spin_lock(&rq->__lock);
  546. if (available_idle_cpu(prev_cpu) && is_reserved(prev_cpu) && !rq->active_balance)
  547. clear_reserved(prev_cpu);
  548. raw_spin_unlock(&rq->__lock);
  549. if (!walt_fair_task(p))
  550. return;
  551. walt_cfs_tick(rq);
  552. if (!rq->misfit_task_load)
  553. return;
  554. if (READ_ONCE(p->__state) != TASK_RUNNING || p->nr_cpus_allowed == 1)
  555. return;
  556. raw_spin_lock_irqsave(&walt_lb_migration_lock, flags);
  557. if (walt_rotation_enabled) {
  558. walt_lb_check_for_rotation(rq);
  559. goto out_unlock;
  560. }
  561. rcu_read_lock();
  562. new_cpu = walt_find_energy_efficient_cpu(p, prev_cpu, 0, 1);
  563. rcu_read_unlock();
  564. if (new_cpu < 0)
  565. goto out_unlock;
  566. /* prevent active task migration to busy or same/lower capacity CPU */
  567. if (!available_idle_cpu(new_cpu) || !check_for_higher_capacity(new_cpu, prev_cpu))
  568. goto out_unlock;
  569. if (!is_min_possible_cluster_cpu(prev_cpu) && !task_fits_max(p, new_cpu))
  570. goto out_unlock;
  571. raw_spin_lock(&rq->__lock);
  572. if (rq->active_balance) {
  573. raw_spin_unlock(&rq->__lock);
  574. goto out_unlock;
  575. }
  576. rq->active_balance = 1;
  577. rq->push_cpu = new_cpu;
  578. get_task_struct(p);
  579. prev_wrq->push_task = p;
  580. raw_spin_unlock(&rq->__lock);
  581. mark_reserved(new_cpu);
  582. raw_spin_unlock_irqrestore(&walt_lb_migration_lock, flags);
  583. trace_walt_active_load_balance(p, prev_cpu, new_cpu, wts);
  584. ret = stop_one_cpu_nowait(prev_cpu,
  585. stop_walt_lb_active_migration, rq,
  586. &rq->active_balance_work);
  587. if (!ret)
  588. clear_reserved(new_cpu);
  589. else
  590. wake_up_if_idle(new_cpu);
  591. return;
  592. out_unlock:
  593. raw_spin_unlock_irqrestore(&walt_lb_migration_lock, flags);
  594. }
  595. static inline int has_pushable_tasks(struct rq *rq)
  596. {
  597. return !plist_head_empty(&rq->rt.pushable_tasks);
  598. }
  599. #define WALT_RT_PULL_THRESHOLD_NS 250000
  600. static bool walt_balance_rt(struct rq *this_rq)
  601. {
  602. int i, this_cpu = this_rq->cpu, src_cpu = this_cpu;
  603. struct rq *src_rq;
  604. struct task_struct *p;
  605. struct walt_task_struct *wts;
  606. bool pulled = false;
  607. u64 wallclock;
  608. /* can't help if this has a runnable RT */
  609. if (sched_rt_runnable(this_rq))
  610. return false;
  611. /* check if any CPU has a pushable RT task */
  612. for_each_possible_cpu(i) {
  613. struct rq *rq = cpu_rq(i);
  614. if (!has_pushable_tasks(rq))
  615. continue;
  616. src_cpu = i;
  617. break;
  618. }
  619. if (src_cpu == this_cpu)
  620. return false;
  621. src_rq = cpu_rq(src_cpu);
  622. double_lock_balance(this_rq, src_rq);
  623. /* lock is dropped, so check again */
  624. if (sched_rt_runnable(this_rq))
  625. goto unlock;
  626. p = pick_highest_pushable_task(src_rq, this_cpu);
  627. if (!p)
  628. goto unlock;
  629. if (!cpumask_test_cpu(this_cpu, p->cpus_ptr))
  630. goto unlock;
  631. wts = (struct walt_task_struct *) p->android_vendor_data1;
  632. /*
  633. * Use rq->clock directly instead of rq_clock() since
  634. * rq->clock was updated recently in the __schedule() -> pick_next_task() callpath.
  635. * Time lost in grabbing rq locks will likely be corrected via max.
  636. */
  637. wallclock = max(this_rq->clock, src_rq->clock);
  638. if (wallclock > wts->last_wake_ts &&
  639. wallclock - wts->last_wake_ts < WALT_RT_PULL_THRESHOLD_NS)
  640. goto unlock;
  641. pulled = true;
  642. deactivate_task(src_rq, p, 0);
  643. set_task_cpu(p, this_cpu);
  644. activate_task(this_rq, p, 0);
  645. unlock:
  646. double_unlock_balance(this_rq, src_rq);
  647. return pulled;
  648. }
  649. __read_mostly unsigned int sysctl_sched_force_lb_enable = 1;
  650. static bool should_help_min_cap(int this_cpu)
  651. {
  652. int cpu;
  653. if (!sysctl_sched_force_lb_enable || is_min_possible_cluster_cpu(this_cpu))
  654. return false;
  655. for_each_cpu(cpu, &cpu_array[0][0]) {
  656. struct walt_rq *wrq = &per_cpu(walt_rq, cpu);
  657. if (wrq->walt_stats.nr_big_tasks)
  658. return true;
  659. }
  660. return false;
  661. }
  662. /* similar to sysctl_sched_migration_cost */
  663. #define NEWIDLE_BALANCE_THRESHOLD 500000
  664. static void walt_newidle_balance(struct rq *this_rq,
  665. struct rq_flags *rf, int *pulled_task,
  666. int *done, int force_overload)
  667. {
  668. int this_cpu = this_rq->cpu;
  669. struct walt_rq *wrq = &per_cpu(walt_rq, this_cpu);
  670. int order_index;
  671. int busy_cpu = -1;
  672. bool enough_idle = (this_rq->avg_idle > NEWIDLE_BALANCE_THRESHOLD);
  673. bool help_min_cap = false;
  674. int first_idle;
  675. int has_misfit = 0;
  676. int i;
  677. struct task_struct *pulled_task_struct = NULL;
  678. struct walt_sched_cluster *cluster;
  679. if (unlikely(walt_disabled))
  680. return;
  681. for_each_sched_cluster(cluster) {
  682. if (cluster != cpu_cluster(this_cpu))
  683. update_freq_relation(cluster);
  684. }
  685. /*
  686. * newly idle load balance is completely handled here, so
  687. * set done to skip the load balance by the caller.
  688. */
  689. *done = 1;
  690. *pulled_task = 0;
  691. /*
  692. * This CPU is about to enter idle, so clear the
  693. * misfit_task_load and mark the idle stamp.
  694. */
  695. this_rq->misfit_task_load = 0;
  696. this_rq->idle_stamp = rq_clock(this_rq);
  697. if (!cpu_active(this_cpu))
  698. return;
  699. if (cpu_halted(this_cpu))
  700. return;
  701. if (is_reserved(this_cpu))
  702. return;
  703. /*Cluster isn't initialized until after WALT is enabled*/
  704. order_index = wrq->cluster->id;
  705. rq_unpin_lock(this_rq, rf);
  706. /*
  707. * Since we drop rq lock while doing RT balance,
  708. * check if any tasks are queued on this and bail out
  709. * early.
  710. */
  711. if (walt_balance_rt(this_rq) || this_rq->nr_running)
  712. goto rt_pulled;
  713. if (!force_overload && !READ_ONCE(this_rq->rd->overload))
  714. goto repin;
  715. if (atomic_read(&this_rq->nr_iowait) && !enough_idle)
  716. goto repin;
  717. help_min_cap = should_help_min_cap(this_cpu);
  718. raw_spin_unlock(&this_rq->__lock);
  719. /*
  720. * careful, we dropped the lock, and has to be acquired
  721. * before returning. Since rq lock is dropped, tasks
  722. * can be queued remotely, so keep a check on nr_running
  723. * and bail out.
  724. */
  725. order_index = wrq->cluster->id;
  726. for (i = 0; i < num_sched_clusters; i++) {
  727. int first_cpu = cpumask_first(&cpu_array[order_index][i]);
  728. struct walt_rq *src_wrq = &per_cpu(walt_rq, first_cpu);
  729. int src_cluster_id = src_wrq->cluster->id;
  730. busy_cpu = walt_lb_find_busiest_cpu(this_cpu, &cpu_array[order_index][i],
  731. &has_misfit, true);
  732. if (busy_cpu == -1)
  733. continue;
  734. /* when not enough idle
  735. * Small should not help big.
  736. * Big should help small ONLY is mifit is present.
  737. * Same capacity cpus should help each other
  738. */
  739. if (!enough_idle &&
  740. (capacity_orig_of(this_cpu) < capacity_orig_of(busy_cpu) ||
  741. (capacity_orig_of(this_cpu) > capacity_orig_of(busy_cpu) && !has_misfit)))
  742. continue;
  743. /* if helping farthest cluster, kick a middle */
  744. if (num_sched_clusters > 2 &&
  745. ((wrq->cluster->id == 0 && src_cluster_id == num_sched_clusters - 1) ||
  746. (wrq->cluster->id == num_sched_clusters - 1 && src_cluster_id == 0))) {
  747. first_idle = find_first_idle_if_others_are_busy();
  748. if (first_idle != -1) {
  749. walt_kick_cpu(first_idle);
  750. } else {
  751. if (walt_rotation_enabled &&
  752. capacity_orig_of(this_cpu) >
  753. capacity_orig_of(busy_cpu)) {
  754. /*
  755. * When BTR active help
  756. * smallest immediately
  757. */
  758. goto found_busy_cpu;
  759. }
  760. }
  761. } else {
  762. goto found_busy_cpu;
  763. }
  764. }
  765. goto unlock;
  766. found_busy_cpu:
  767. /* sanity checks before attempting the pull */
  768. if (this_rq->nr_running > 0 || (busy_cpu == this_cpu))
  769. goto unlock;
  770. *pulled_task = walt_lb_pull_tasks(this_cpu, busy_cpu, &pulled_task_struct);
  771. unlock:
  772. raw_spin_lock(&this_rq->__lock);
  773. rt_pulled:
  774. if (this_rq->cfs.h_nr_running && !*pulled_task)
  775. *pulled_task = 1;
  776. /* Is there a task of a high priority class? */
  777. if (this_rq->nr_running != this_rq->cfs.h_nr_running)
  778. *pulled_task = -1;
  779. /* reset the idle time stamp if we pulled any task */
  780. if (*pulled_task)
  781. this_rq->idle_stamp = 0;
  782. repin:
  783. rq_repin_lock(this_rq, rf);
  784. trace_walt_newidle_balance(this_cpu, busy_cpu, *pulled_task,
  785. help_min_cap, enough_idle, pulled_task_struct);
  786. }
  787. /* run newidle balance as a result of an unhalt operation */
  788. void walt_smp_newidle_balance(void *ignored)
  789. {
  790. int cpu = raw_smp_processor_id();
  791. struct rq *rq = cpu_rq(cpu);
  792. struct rq_flags rf;
  793. int pulled_task;
  794. int done = 0;
  795. rq_lock(rq, &rf);
  796. update_rq_clock(rq);
  797. walt_newidle_balance(rq, &rf, &pulled_task, &done, true);
  798. resched_curr(rq);
  799. rq_unlock(rq, &rf);
  800. }
  801. static DEFINE_PER_CPU(call_single_data_t, nib_csd);
  802. void walt_smp_call_newidle_balance(int cpu)
  803. {
  804. call_single_data_t *csd = &per_cpu(nib_csd, cpu);
  805. if (unlikely(walt_disabled))
  806. return;
  807. smp_call_function_single_async(cpu, csd);
  808. }
  809. static void walt_find_busiest_queue(void *unused, int dst_cpu,
  810. struct sched_group *group,
  811. struct cpumask *env_cpus,
  812. struct rq **busiest, int *done)
  813. {
  814. int fsrc_cpu = cpumask_first(sched_group_span(group));
  815. int busiest_cpu = -1;
  816. struct cpumask src_mask;
  817. int has_misfit;
  818. if (unlikely(walt_disabled))
  819. return;
  820. *done = 1;
  821. *busiest = NULL;
  822. /*
  823. * same cluster means, there will only be 1
  824. * CPU in the busy group, so just select it.
  825. */
  826. if (same_cluster(dst_cpu, fsrc_cpu)) {
  827. busiest_cpu = fsrc_cpu;
  828. goto done;
  829. }
  830. /*
  831. * We will allow inter cluster migrations
  832. * only if the source group is sufficiently
  833. * loaded. The upstream load balancer is a
  834. * bit more generous.
  835. *
  836. * re-using the same code that we use it
  837. * for newly idle load balance. The policies
  838. * remain same.
  839. */
  840. cpumask_and(&src_mask, sched_group_span(group), env_cpus);
  841. busiest_cpu = walt_lb_find_busiest_cpu(dst_cpu, &src_mask, &has_misfit, false);
  842. done:
  843. if (busiest_cpu != -1)
  844. *busiest = cpu_rq(busiest_cpu);
  845. trace_walt_find_busiest_queue(dst_cpu, busiest_cpu, src_mask.bits[0]);
  846. }
  847. /*
  848. * we only decide if nohz balance kick is needed or not. the
  849. * first CPU in the nohz.idle will come out of idle and do
  850. * load balance on behalf of every CPU. adding another hook
  851. * to decide which cpu to kick is useless. most of the time,
  852. * it is impossible to decide which CPU has to come out because
  853. * we get to kick only once.
  854. */
  855. static void walt_nohz_balancer_kick(void *unused, struct rq *rq,
  856. unsigned int *flags, int *done)
  857. {
  858. if (unlikely(walt_disabled))
  859. return;
  860. *done = 1;
  861. /*
  862. * tick path migration takes care of misfit task.
  863. * so we have to check for nr_running >= 2 here.
  864. */
  865. if (rq->nr_running >= 2 && cpu_overutilized(rq->cpu)) {
  866. *flags = NOHZ_KICK_MASK;
  867. trace_walt_nohz_balance_kick(rq);
  868. }
  869. }
  870. static void walt_can_migrate_task(void *unused, struct task_struct *p,
  871. int dst_cpu, int *can_migrate)
  872. {
  873. bool to_lower, to_higher;
  874. if (unlikely(walt_disabled))
  875. return;
  876. to_lower = check_for_higher_capacity(task_cpu(p), dst_cpu);
  877. to_higher = check_for_higher_capacity(dst_cpu, task_cpu(p));
  878. if (_walt_can_migrate_task(p, dst_cpu, to_lower,
  879. to_higher, true))
  880. return;
  881. *can_migrate = 0;
  882. }
  883. static void walt_sched_newidle_balance(void *unused, struct rq *this_rq,
  884. struct rq_flags *rf, int *pulled_task,
  885. int *done)
  886. {
  887. walt_newidle_balance(this_rq, rf, pulled_task, done, false);
  888. }
  889. void walt_lb_init(void)
  890. {
  891. int cpu;
  892. walt_lb_rotate_work_init();
  893. register_trace_android_rvh_sched_nohz_balancer_kick(walt_nohz_balancer_kick, NULL);
  894. register_trace_android_rvh_can_migrate_task(walt_can_migrate_task, NULL);
  895. register_trace_android_rvh_find_busiest_queue(walt_find_busiest_queue, NULL);
  896. register_trace_android_rvh_sched_newidle_balance(walt_sched_newidle_balance, NULL);
  897. for_each_cpu(cpu, cpu_possible_mask) {
  898. call_single_data_t *csd;
  899. csd = &per_cpu(nib_csd, cpu);
  900. INIT_CSD(csd, walt_smp_newidle_balance, (void *)(unsigned long)cpu);
  901. }
  902. }