walt_halt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/cpu.h>
  6. #include <linux/cpumask.h>
  7. #include <linux/sched/isolation.h>
  8. #include <trace/events/power.h>
  9. #include <trace/hooks/sched.h>
  10. #include <walt.h>
  11. #include "trace.h"
  12. #ifdef CONFIG_HOTPLUG_CPU
  13. enum pause_type {
  14. HALT,
  15. PARTIAL_HALT,
  16. MAX_PAUSE_TYPE
  17. };
  18. /* if a cpu is halting */
  19. struct cpumask __cpu_halt_mask;
  20. struct cpumask __cpu_partial_halt_mask;
  21. /* spin lock to allow calling from non-preemptible context */
  22. static DEFINE_RAW_SPINLOCK(halt_lock);
  23. struct halt_cpu_state {
  24. u8 client_vote_mask[MAX_PAUSE_TYPE];
  25. };
  26. static DEFINE_PER_CPU(struct halt_cpu_state, halt_state);
  27. static DEFINE_RAW_SPINLOCK(walt_drain_pending_lock);
  28. /* the amount of time allowed for enqueue operations that happen
  29. * just after a halt operation.
  30. */
  31. #define WALT_HALT_CHECK_THRESHOLD_NS 400000
  32. /*
  33. * Remove a task from the runqueue and pretend that it's migrating. This
  34. * should prevent migrations for the detached task and disallow further
  35. * changes to tsk_cpus_allowed.
  36. */
  37. void
  38. detach_one_task_core(struct task_struct *p, struct rq *rq,
  39. struct list_head *tasks)
  40. {
  41. walt_lockdep_assert_rq(rq, p);
  42. p->on_rq = TASK_ON_RQ_MIGRATING;
  43. deactivate_task(rq, p, 0);
  44. list_add(&p->se.group_node, tasks);
  45. }
  46. void attach_tasks_core(struct list_head *tasks, struct rq *rq)
  47. {
  48. struct task_struct *p;
  49. walt_lockdep_assert_rq(rq, NULL);
  50. while (!list_empty(tasks)) {
  51. p = list_first_entry(tasks, struct task_struct, se.group_node);
  52. list_del_init(&p->se.group_node);
  53. BUG_ON(task_rq(p) != rq);
  54. activate_task(rq, p, 0);
  55. p->on_rq = TASK_ON_RQ_QUEUED;
  56. }
  57. }
  58. /*
  59. * Migrate all tasks from the rq, sleeping tasks will be migrated by
  60. * try_to_wake_up()->select_task_rq().
  61. *
  62. * Called with rq->__lock held even though we'er in stop_machine() and
  63. * there's no concurrency possible, we hold the required locks anyway
  64. * because of lock validation efforts.
  65. *
  66. * The function will skip CPU pinned kthreads.
  67. */
  68. static void migrate_tasks(struct rq *dead_rq, struct rq_flags *rf)
  69. {
  70. struct rq *rq = dead_rq;
  71. struct task_struct *next, *stop = rq->stop;
  72. LIST_HEAD(percpu_kthreads);
  73. unsigned int num_pinned_kthreads = 1;
  74. struct rq_flags orf = *rf;
  75. int dest_cpu;
  76. /*
  77. * Fudge the rq selection such that the below task selection loop
  78. * doesn't get stuck on the currently eligible stop task.
  79. *
  80. * We're currently inside stop_machine() and the rq is either stuck
  81. * in the stop_machine_cpu_stop() loop, or we're executing this code,
  82. * either way we should never end up calling schedule() until we're
  83. * done here.
  84. */
  85. rq->stop = NULL;
  86. /*
  87. * put_prev_task() and pick_next_task() sched
  88. * class method both need to have an up-to-date
  89. * value of rq->clock[_task]
  90. */
  91. update_rq_clock(rq);
  92. #ifdef CONFIG_SCHED_DEBUG
  93. /* note the clock update in orf */
  94. orf.clock_update_flags |= RQCF_UPDATED;
  95. #endif
  96. for (;;) {
  97. /*
  98. * There's this thread running, bail when that's the only
  99. * remaining thread:
  100. */
  101. if (rq->nr_running == 1)
  102. break;
  103. next = pick_migrate_task(rq);
  104. /*
  105. * Argh ... no iterator for tasks, we need to remove the
  106. * kthread from the run-queue to continue.
  107. */
  108. if (is_per_cpu_kthread(next)) {
  109. detach_one_task_core(next, rq, &percpu_kthreads);
  110. num_pinned_kthreads += 1;
  111. continue;
  112. }
  113. /*
  114. * Rules for changing task_struct::cpus_mask are holding
  115. * both pi_lock and rq->__lock, such that holding either
  116. * stabilizes the mask.
  117. *
  118. * Drop rq->__lock is not quite as disastrous as it usually is
  119. * because !cpu_active at this point, which means load-balance
  120. * will not interfere. Also, stop-machine.
  121. */
  122. rq_unlock(rq, rf);
  123. raw_spin_lock(&next->pi_lock);
  124. raw_spin_rq_lock(rq);
  125. rq_repin_lock(rq, rf);
  126. /*
  127. * Since we're inside stop-machine, _nothing_ should have
  128. * changed the task, WARN if weird stuff happened, because in
  129. * that case the above rq->__lock drop is a fail too.
  130. */
  131. if (task_rq(next) != rq || !task_on_rq_queued(next)) {
  132. raw_spin_unlock(&next->pi_lock);
  133. continue;
  134. }
  135. /* Find suitable destination for @next */
  136. dest_cpu = select_fallback_rq(dead_rq->cpu, next);
  137. if (cpu_of(rq) != dest_cpu && !is_migration_disabled(next)) {
  138. /* only perform a required migration */
  139. rq = __migrate_task(rq, rf, next, dest_cpu);
  140. if (rq != dead_rq) {
  141. rq_unlock(rq, rf);
  142. rq = dead_rq;
  143. *rf = orf;
  144. raw_spin_rq_lock(rq);
  145. rq_repin_lock(rq, rf);
  146. }
  147. } else {
  148. detach_one_task_core(next, rq, &percpu_kthreads);
  149. num_pinned_kthreads += 1;
  150. }
  151. raw_spin_unlock(&next->pi_lock);
  152. }
  153. if (num_pinned_kthreads > 1)
  154. attach_tasks_core(&percpu_kthreads, rq);
  155. rq->stop = stop;
  156. }
  157. void __balance_callbacks(struct rq *rq);
  158. static int drain_rq_cpu_stop(void *data)
  159. {
  160. struct rq *rq = this_rq();
  161. struct rq_flags rf;
  162. struct walt_rq *wrq = &per_cpu(walt_rq, cpu_of(rq));
  163. rq_lock_irqsave(rq, &rf);
  164. /* rq lock is pinned */
  165. /* migrate tasks assumes that the lock is pinned, and will unlock/repin */
  166. migrate_tasks(rq, &rf);
  167. /* __balance_callbacks can unlock and relock the rq lock. unpin */
  168. rq_unpin_lock(rq, &rf);
  169. /*
  170. * service any callbacks that were accumulated, prior to unlocking. such that
  171. * any subsequent calls to rq_lock... will see an rq->balance_callback set to
  172. * the default (0 or balance_push_callback);
  173. */
  174. wrq->enqueue_counter = 0;
  175. __balance_callbacks(rq);
  176. if (wrq->enqueue_counter)
  177. WALT_BUG(WALT_BUG_WALT, NULL, "cpu: %d task was re-enqueued", cpu_of(rq));
  178. /* lock is no longer pinned, raw unlock using same flags as locking */
  179. raw_spin_rq_unlock_irqrestore(rq, rf.flags);
  180. return 0;
  181. }
  182. static int cpu_drain_rq(unsigned int cpu)
  183. {
  184. if (!cpu_online(cpu))
  185. return 0;
  186. if (available_idle_cpu(cpu))
  187. return 0;
  188. /* this will schedule, must not be in atomic context */
  189. return stop_one_cpu(cpu, drain_rq_cpu_stop, NULL);
  190. }
  191. struct drain_thread_data {
  192. cpumask_t cpus_to_drain;
  193. };
  194. static struct drain_thread_data drain_data = {
  195. .cpus_to_drain = { CPU_BITS_NONE }
  196. };
  197. static int __ref try_drain_rqs(void *data)
  198. {
  199. cpumask_t *cpus_ptr = &((struct drain_thread_data *)data)->cpus_to_drain;
  200. int cpu;
  201. unsigned long flags;
  202. while (!kthread_should_stop()) {
  203. raw_spin_lock_irqsave(&walt_drain_pending_lock, flags);
  204. if (cpumask_weight(cpus_ptr)) {
  205. cpumask_t local_cpus;
  206. cpumask_copy(&local_cpus, cpus_ptr);
  207. raw_spin_unlock_irqrestore(&walt_drain_pending_lock, flags);
  208. for_each_cpu(cpu, &local_cpus)
  209. cpu_drain_rq(cpu);
  210. raw_spin_lock_irqsave(&walt_drain_pending_lock, flags);
  211. cpumask_andnot(cpus_ptr, cpus_ptr, &local_cpus);
  212. }
  213. raw_spin_unlock_irqrestore(&walt_drain_pending_lock, flags);
  214. set_current_state(TASK_INTERRUPTIBLE);
  215. schedule();
  216. set_current_state(TASK_RUNNING);
  217. }
  218. return 0;
  219. }
  220. void restrict_cpus_and_freq(struct cpumask *cpus)
  221. {
  222. struct cpumask restrict_cpus;
  223. int cpu = 0;
  224. cpumask_copy(&restrict_cpus, cpus);
  225. if (cpumask_intersects(cpus, cpu_partial_halt_mask) &&
  226. !cpumask_intersects(cpus, cpu_halt_mask) &&
  227. is_state1()) {
  228. for_each_cpu(cpu, cpus)
  229. fmax_cap[PARTIAL_HALT_CAP][cpu_cluster(cpu)->id] =
  230. sysctl_max_freq_partial_halt;
  231. } else {
  232. for_each_cpu(cpu, cpus) {
  233. cpumask_or(&restrict_cpus, &restrict_cpus, &(cpu_cluster(cpu)->cpus));
  234. fmax_cap[PARTIAL_HALT_CAP][cpu_cluster(cpu)->id] =
  235. FREQ_QOS_MAX_DEFAULT_VALUE;
  236. }
  237. }
  238. update_fmax_cap_capacities();
  239. }
  240. struct task_struct *walt_drain_thread;
  241. static int halt_cpus(struct cpumask *cpus, enum pause_type type)
  242. {
  243. int cpu;
  244. int ret = 0;
  245. u64 start_time = 0;
  246. struct halt_cpu_state *halt_cpu_state;
  247. unsigned long flags;
  248. if (trace_halt_cpus_enabled())
  249. start_time = sched_clock();
  250. trace_halt_cpus_start(cpus, 1);
  251. /* add the cpus to the halt mask */
  252. for_each_cpu(cpu, cpus) {
  253. if (cpu == cpumask_first(system_32bit_el0_cpumask())) {
  254. ret = -EINVAL;
  255. goto out;
  256. }
  257. halt_cpu_state = per_cpu_ptr(&halt_state, cpu);
  258. if (type == HALT)
  259. cpumask_set_cpu(cpu, cpu_halt_mask);
  260. else
  261. cpumask_set_cpu(cpu, cpu_partial_halt_mask);
  262. /* guarantee mask written at this time */
  263. wmb();
  264. }
  265. restrict_cpus_and_freq(cpus);
  266. /* migrate tasks off the cpu */
  267. if (type == HALT) {
  268. /* signal and wakeup the drain kthread */
  269. raw_spin_lock_irqsave(&walt_drain_pending_lock, flags);
  270. cpumask_or(&drain_data.cpus_to_drain, &drain_data.cpus_to_drain, cpus);
  271. raw_spin_unlock_irqrestore(&walt_drain_pending_lock, flags);
  272. wake_up_process(walt_drain_thread);
  273. }
  274. out:
  275. trace_halt_cpus(cpus, start_time, 1, ret);
  276. return ret;
  277. }
  278. /* start the cpus again, and kick them to balance */
  279. static int start_cpus(struct cpumask *cpus, enum pause_type type)
  280. {
  281. u64 start_time = sched_clock();
  282. struct halt_cpu_state *halt_cpu_state;
  283. int cpu;
  284. trace_halt_cpus_start(cpus, 0);
  285. for_each_cpu(cpu, cpus) {
  286. halt_cpu_state = per_cpu_ptr(&halt_state, cpu);
  287. /* guarantee the halt state is updated */
  288. wmb();
  289. if (type == HALT)
  290. cpumask_clear_cpu(cpu, cpu_halt_mask);
  291. else
  292. cpumask_clear_cpu(cpu, cpu_partial_halt_mask);
  293. /* kick the cpu so it can pull tasks
  294. * after the mask has been cleared.
  295. */
  296. walt_smp_call_newidle_balance(cpu);
  297. }
  298. restrict_cpus_and_freq(cpus);
  299. trace_halt_cpus(cpus, start_time, 0, 0);
  300. trace_clock_set_rate("sync_state", is_state1() ? 0 : 1, raw_smp_processor_id());
  301. return 0;
  302. }
  303. /* update client for cpus in yield/halt mask */
  304. static void update_clients(struct cpumask *cpus, bool halt, enum pause_client client,
  305. enum pause_type type)
  306. {
  307. int cpu;
  308. struct halt_cpu_state *halt_cpu_state;
  309. for_each_cpu(cpu, cpus) {
  310. halt_cpu_state = per_cpu_ptr(&halt_state, cpu);
  311. if (halt)
  312. halt_cpu_state->client_vote_mask[type] |= client;
  313. else
  314. halt_cpu_state->client_vote_mask[type] &= ~client;
  315. }
  316. }
  317. /* remove cpus that are already halted */
  318. static void update_halt_cpus(struct cpumask *cpus, enum pause_type type)
  319. {
  320. int cpu;
  321. struct halt_cpu_state *halt_cpu_state;
  322. for_each_cpu(cpu, cpus) {
  323. halt_cpu_state = per_cpu_ptr(&halt_state, cpu);
  324. if (halt_cpu_state->client_vote_mask[type])
  325. cpumask_clear_cpu(cpu, cpus);
  326. }
  327. }
  328. /* cpus will be modified */
  329. static int walt_halt_cpus(struct cpumask *cpus, enum pause_client client, enum pause_type type)
  330. {
  331. int ret = 0;
  332. cpumask_t requested_cpus;
  333. unsigned long flags;
  334. raw_spin_lock_irqsave(&halt_lock, flags);
  335. cpumask_copy(&requested_cpus, cpus);
  336. /* remove cpus that are already halted */
  337. update_halt_cpus(cpus, type);
  338. if (cpumask_empty(cpus)) {
  339. update_clients(&requested_cpus, true, client, type);
  340. goto unlock;
  341. }
  342. ret = halt_cpus(cpus, type);
  343. if (ret < 0)
  344. pr_debug("halt_cpus failure ret=%d cpus=%*pbl\n", ret,
  345. cpumask_pr_args(&requested_cpus));
  346. else
  347. update_clients(&requested_cpus, true, client, type);
  348. unlock:
  349. raw_spin_unlock_irqrestore(&halt_lock, flags);
  350. trace_clock_set_rate("sync_state", is_state1() ? 0 : 1, raw_smp_processor_id());
  351. return ret;
  352. }
  353. int walt_pause_cpus(struct cpumask *cpus, enum pause_client client)
  354. {
  355. if (walt_disabled)
  356. return -EAGAIN;
  357. return walt_halt_cpus(cpus, client, HALT);
  358. }
  359. EXPORT_SYMBOL_GPL(walt_pause_cpus);
  360. int walt_partial_pause_cpus(struct cpumask *cpus, enum pause_client client)
  361. {
  362. if (walt_disabled)
  363. return -EAGAIN;
  364. return walt_halt_cpus(cpus, client, PARTIAL_HALT);
  365. }
  366. EXPORT_SYMBOL_GPL(walt_partial_pause_cpus);
  367. /* cpus will be modified */
  368. static int walt_start_cpus(struct cpumask *cpus, enum pause_client client, enum pause_type type)
  369. {
  370. int ret = 0;
  371. cpumask_t requested_cpus;
  372. unsigned long flags;
  373. raw_spin_lock_irqsave(&halt_lock, flags);
  374. cpumask_copy(&requested_cpus, cpus);
  375. update_clients(&requested_cpus, false, client, type);
  376. /* remove cpus that should still be halted */
  377. update_halt_cpus(cpus, type);
  378. ret = start_cpus(cpus, type);
  379. if (ret < 0) {
  380. pr_debug("halt_cpus failure ret=%d cpus=%*pbl\n", ret,
  381. cpumask_pr_args(&requested_cpus));
  382. /* restore/increment ref counts in case of error */
  383. update_clients(&requested_cpus, true, client, type);
  384. }
  385. raw_spin_unlock_irqrestore(&halt_lock, flags);
  386. return ret;
  387. }
  388. int walt_resume_cpus(struct cpumask *cpus, enum pause_client client)
  389. {
  390. if (walt_disabled)
  391. return -EAGAIN;
  392. return walt_start_cpus(cpus, client, HALT);
  393. }
  394. EXPORT_SYMBOL_GPL(walt_resume_cpus);
  395. int walt_partial_resume_cpus(struct cpumask *cpus, enum pause_client client)
  396. {
  397. if (walt_disabled)
  398. return -EAGAIN;
  399. return walt_start_cpus(cpus, client, PARTIAL_HALT);
  400. }
  401. EXPORT_SYMBOL_GPL(walt_partial_resume_cpus);
  402. /**
  403. * cpus_halted_by_client: determine if client has halted a cpu
  404. * where all cpus in the mask are halted.
  405. *
  406. * If all cpus in the cluster are halted, and one of them is
  407. * halted for this client, then and only then indicate pass.
  408. *
  409. * Otherwise, if not all cpus are halted, or none of the cpus
  410. * are halted by this particular client, then reject.
  411. *
  412. * return true if conditions are met, false otherwise.
  413. */
  414. bool cpus_halted_by_client(struct cpumask *cpus, enum pause_client client)
  415. {
  416. struct halt_cpu_state *halt_cpu_state;
  417. bool cpu_halted_for_client = false;
  418. int cpu;
  419. for_each_cpu(cpu, cpus) {
  420. halt_cpu_state = per_cpu_ptr(&halt_state, cpu);
  421. if (!halt_cpu_state->client_vote_mask[HALT])
  422. return false;
  423. if (halt_cpu_state->client_vote_mask[HALT] & client)
  424. cpu_halted_for_client = true;
  425. }
  426. if (cpu_halted_for_client)
  427. return true;
  428. return false;
  429. }
  430. static void android_rvh_get_nohz_timer_target(void *unused, int *cpu, bool *done)
  431. {
  432. int i, default_cpu = -1;
  433. struct sched_domain *sd;
  434. cpumask_t active_unhalted;
  435. *done = true;
  436. cpumask_andnot(&active_unhalted, cpu_active_mask, cpu_halt_mask);
  437. if (housekeeping_cpu(*cpu, HK_TYPE_TIMER) && !cpu_halted(*cpu)) {
  438. if (!available_idle_cpu(*cpu))
  439. return;
  440. default_cpu = *cpu;
  441. }
  442. /*
  443. * find first cpu halted by core control and try to avoid
  444. * affecting externally halted cpus.
  445. */
  446. if (!cpumask_weight(&active_unhalted)) {
  447. cpumask_t tmp_pause, tmp_part_pause, tmp_halt, *tmp;
  448. cpumask_and(&tmp_part_pause, cpu_active_mask, &cpus_part_paused_by_us);
  449. cpumask_and(&tmp_pause, cpu_active_mask, &cpus_paused_by_us);
  450. cpumask_and(&tmp_halt, cpu_active_mask, cpu_halt_mask);
  451. tmp = cpumask_weight(&tmp_part_pause) ? &tmp_part_pause :
  452. cpumask_weight(&tmp_pause) ? &tmp_pause : &tmp_halt;
  453. for_each_cpu(i, tmp) {
  454. if ((*cpu == i) && cpumask_weight(tmp) > 1)
  455. continue;
  456. *cpu = i;
  457. return;
  458. }
  459. }
  460. rcu_read_lock();
  461. for_each_domain(*cpu, sd) {
  462. for_each_cpu_and(i, sched_domain_span(sd),
  463. housekeeping_cpumask(HK_TYPE_TIMER)) {
  464. if (*cpu == i)
  465. continue;
  466. if (!available_idle_cpu(i) && !cpu_halted(i)) {
  467. *cpu = i;
  468. goto unlock;
  469. }
  470. }
  471. }
  472. if (default_cpu == -1) {
  473. for_each_cpu_and(i, &active_unhalted,
  474. housekeeping_cpumask(HK_TYPE_TIMER)) {
  475. if (*cpu == i)
  476. continue;
  477. if (!available_idle_cpu(i)) {
  478. *cpu = i;
  479. goto unlock;
  480. }
  481. }
  482. /* choose any active unhalted cpu */
  483. default_cpu = cpumask_any(&active_unhalted);
  484. if (unlikely(default_cpu >= nr_cpu_ids))
  485. goto unlock;
  486. }
  487. *cpu = default_cpu;
  488. unlock:
  489. rcu_read_unlock();
  490. }
  491. /**
  492. * android_rvh_set_cpus_allowed_by_task: disallow cpus that are halted
  493. *
  494. * NOTES: may be called if migration is disabled for the task
  495. * if per-cpu-kthread, must not deliberately return an invalid cpu
  496. * if !per-cpu-kthread, may return an invalid cpu (reject dest_cpu)
  497. * must not change cpu in in_exec 32bit task case
  498. */
  499. static void android_rvh_set_cpus_allowed_by_task(void *unused,
  500. const struct cpumask *cpu_valid_mask,
  501. const struct cpumask *new_mask,
  502. struct task_struct *p,
  503. unsigned int *dest_cpu)
  504. {
  505. if (unlikely(walt_disabled))
  506. return;
  507. /* allow kthreads to change affinity regardless of halt status of dest_cpu */
  508. if (p->flags & PF_KTHREAD)
  509. return;
  510. if (cpu_halted(*dest_cpu) && !p->migration_disabled) {
  511. cpumask_t allowed_cpus;
  512. if (unlikely(is_compat_thread(task_thread_info(p)) && p->in_execve))
  513. return;
  514. /* remove halted cpus from the valid mask, and store locally */
  515. cpumask_andnot(&allowed_cpus, cpu_valid_mask, cpu_halt_mask);
  516. cpumask_and(&allowed_cpus, &allowed_cpus, new_mask);
  517. /* do not modify dest_cpu if there are no cpus to choose from */
  518. if (!cpumask_empty(&allowed_cpus))
  519. *dest_cpu = cpumask_any_and_distribute(&allowed_cpus, new_mask);
  520. }
  521. }
  522. /**
  523. * android_rvh_rto_next-cpu: disallow halted cpus for irq workfunctions
  524. */
  525. static void android_rvh_rto_next_cpu(void *unused, int rto_cpu, struct cpumask *rto_mask, int *cpu)
  526. {
  527. cpumask_t allowed_cpus;
  528. if (unlikely(walt_disabled))
  529. return;
  530. if (cpu_halted(*cpu)) {
  531. /* remove halted cpus from the valid mask, and store locally */
  532. cpumask_andnot(&allowed_cpus, rto_mask, cpu_halt_mask);
  533. *cpu = cpumask_next(rto_cpu, &allowed_cpus);
  534. }
  535. }
  536. /**
  537. * android_rvh_is_cpu_allowed: disallow cpus that are halted
  538. *
  539. * NOTE: this function will not be called if migration is disabled for the task.
  540. */
  541. static void android_rvh_is_cpu_allowed(void *unused, struct task_struct *p, int cpu, bool *allowed)
  542. {
  543. if (unlikely(walt_disabled))
  544. return;
  545. if (cpumask_test_cpu(cpu, cpu_halt_mask)) {
  546. cpumask_t cpus_allowed;
  547. /* default reject for any halted cpu */
  548. *allowed = false;
  549. if (unlikely(is_compat_thread(task_thread_info(p)) && p->in_execve)) {
  550. /* 32bit task in execve. allow this cpu. */
  551. *allowed = true;
  552. return;
  553. }
  554. /*
  555. * for cfs threads, active cpus in the affinity are allowed
  556. * but halted cpus are not allowed
  557. */
  558. cpumask_and(&cpus_allowed, cpu_active_mask, p->cpus_ptr);
  559. cpumask_andnot(&cpus_allowed, &cpus_allowed, cpu_halt_mask);
  560. if (!(p->flags & PF_KTHREAD)) {
  561. if (cpumask_empty(&cpus_allowed)) {
  562. /*
  563. * All affined cpus are inactive or halted.
  564. * Allow this cpu for user threads
  565. */
  566. *allowed = true;
  567. }
  568. return;
  569. }
  570. /* for kthreads, dying cpus are not allowed */
  571. cpumask_andnot(&cpus_allowed, &cpus_allowed, cpu_dying_mask);
  572. if (cpumask_empty(&cpus_allowed)) {
  573. /*
  574. * All affined cpus inactive or halted or dying.
  575. * Allow this cpu for kthreads
  576. */
  577. *allowed = true;
  578. }
  579. }
  580. }
  581. void walt_halt_init(void)
  582. {
  583. struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
  584. walt_drain_thread = kthread_run(try_drain_rqs, &drain_data, "halt_drain_rqs");
  585. if (IS_ERR(walt_drain_thread)) {
  586. pr_err("Error creating walt drain thread\n");
  587. return;
  588. }
  589. sched_setscheduler_nocheck(walt_drain_thread, SCHED_FIFO, &param);
  590. register_trace_android_rvh_get_nohz_timer_target(android_rvh_get_nohz_timer_target, NULL);
  591. register_trace_android_rvh_set_cpus_allowed_by_task(
  592. android_rvh_set_cpus_allowed_by_task, NULL);
  593. register_trace_android_rvh_rto_next_cpu(android_rvh_rto_next_cpu, NULL);
  594. register_trace_android_rvh_is_cpu_allowed(android_rvh_is_cpu_allowed, NULL);
  595. }
  596. #endif /* CONFIG_HOTPLUG_CPU */