psci_checker.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2016 ARM Limited
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/atomic.h>
  8. #include <linux/completion.h>
  9. #include <linux/cpu.h>
  10. #include <linux/cpuidle.h>
  11. #include <linux/cpu_pm.h>
  12. #include <linux/kernel.h>
  13. #include <linux/kthread.h>
  14. #include <uapi/linux/sched/types.h>
  15. #include <linux/module.h>
  16. #include <linux/preempt.h>
  17. #include <linux/psci.h>
  18. #include <linux/slab.h>
  19. #include <linux/tick.h>
  20. #include <linux/topology.h>
  21. #include <asm/cpuidle.h>
  22. #include <uapi/linux/psci.h>
  23. #define NUM_SUSPEND_CYCLE (10)
  24. static unsigned int nb_available_cpus;
  25. static int tos_resident_cpu = -1;
  26. static atomic_t nb_active_threads;
  27. static struct completion suspend_threads_started =
  28. COMPLETION_INITIALIZER(suspend_threads_started);
  29. static struct completion suspend_threads_done =
  30. COMPLETION_INITIALIZER(suspend_threads_done);
  31. /*
  32. * We assume that PSCI operations are used if they are available. This is not
  33. * necessarily true on arm64, since the decision is based on the
  34. * "enable-method" property of each CPU in the DT, but given that there is no
  35. * arch-specific way to check this, we assume that the DT is sensible.
  36. */
  37. static int psci_ops_check(void)
  38. {
  39. int migrate_type = -1;
  40. int cpu;
  41. if (!(psci_ops.cpu_off && psci_ops.cpu_on && psci_ops.cpu_suspend)) {
  42. pr_warn("Missing PSCI operations, aborting tests\n");
  43. return -EOPNOTSUPP;
  44. }
  45. if (psci_ops.migrate_info_type)
  46. migrate_type = psci_ops.migrate_info_type();
  47. if (migrate_type == PSCI_0_2_TOS_UP_MIGRATE ||
  48. migrate_type == PSCI_0_2_TOS_UP_NO_MIGRATE) {
  49. /* There is a UP Trusted OS, find on which core it resides. */
  50. for_each_online_cpu(cpu)
  51. if (psci_tos_resident_on(cpu)) {
  52. tos_resident_cpu = cpu;
  53. break;
  54. }
  55. if (tos_resident_cpu == -1)
  56. pr_warn("UP Trusted OS resides on no online CPU\n");
  57. }
  58. return 0;
  59. }
  60. /*
  61. * offlined_cpus is a temporary array but passing it as an argument avoids
  62. * multiple allocations.
  63. */
  64. static unsigned int down_and_up_cpus(const struct cpumask *cpus,
  65. struct cpumask *offlined_cpus)
  66. {
  67. int cpu;
  68. int err = 0;
  69. cpumask_clear(offlined_cpus);
  70. /* Try to power down all CPUs in the mask. */
  71. for_each_cpu(cpu, cpus) {
  72. int ret = remove_cpu(cpu);
  73. /*
  74. * cpu_down() checks the number of online CPUs before the TOS
  75. * resident CPU.
  76. */
  77. if (cpumask_weight(offlined_cpus) + 1 == nb_available_cpus) {
  78. if (ret != -EBUSY) {
  79. pr_err("Unexpected return code %d while trying "
  80. "to power down last online CPU %d\n",
  81. ret, cpu);
  82. ++err;
  83. }
  84. } else if (cpu == tos_resident_cpu) {
  85. if (ret != -EPERM) {
  86. pr_err("Unexpected return code %d while trying "
  87. "to power down TOS resident CPU %d\n",
  88. ret, cpu);
  89. ++err;
  90. }
  91. } else if (ret != 0) {
  92. pr_err("Error occurred (%d) while trying "
  93. "to power down CPU %d\n", ret, cpu);
  94. ++err;
  95. }
  96. if (ret == 0)
  97. cpumask_set_cpu(cpu, offlined_cpus);
  98. }
  99. /* Try to power up all the CPUs that have been offlined. */
  100. for_each_cpu(cpu, offlined_cpus) {
  101. int ret = add_cpu(cpu);
  102. if (ret != 0) {
  103. pr_err("Error occurred (%d) while trying "
  104. "to power up CPU %d\n", ret, cpu);
  105. ++err;
  106. } else {
  107. cpumask_clear_cpu(cpu, offlined_cpus);
  108. }
  109. }
  110. /*
  111. * Something went bad at some point and some CPUs could not be turned
  112. * back on.
  113. */
  114. WARN_ON(!cpumask_empty(offlined_cpus) ||
  115. num_online_cpus() != nb_available_cpus);
  116. return err;
  117. }
  118. static void free_cpu_groups(int num, cpumask_var_t **pcpu_groups)
  119. {
  120. int i;
  121. cpumask_var_t *cpu_groups = *pcpu_groups;
  122. for (i = 0; i < num; ++i)
  123. free_cpumask_var(cpu_groups[i]);
  124. kfree(cpu_groups);
  125. }
  126. static int alloc_init_cpu_groups(cpumask_var_t **pcpu_groups)
  127. {
  128. int num_groups = 0;
  129. cpumask_var_t tmp, *cpu_groups;
  130. if (!alloc_cpumask_var(&tmp, GFP_KERNEL))
  131. return -ENOMEM;
  132. cpu_groups = kzalloc_objs(*cpu_groups, nb_available_cpus);
  133. if (!cpu_groups) {
  134. free_cpumask_var(tmp);
  135. return -ENOMEM;
  136. }
  137. cpumask_copy(tmp, cpu_online_mask);
  138. while (!cpumask_empty(tmp)) {
  139. const struct cpumask *cpu_group =
  140. topology_core_cpumask(cpumask_any(tmp));
  141. if (!alloc_cpumask_var(&cpu_groups[num_groups], GFP_KERNEL)) {
  142. free_cpumask_var(tmp);
  143. free_cpu_groups(num_groups, &cpu_groups);
  144. return -ENOMEM;
  145. }
  146. cpumask_copy(cpu_groups[num_groups++], cpu_group);
  147. cpumask_andnot(tmp, tmp, cpu_group);
  148. }
  149. free_cpumask_var(tmp);
  150. *pcpu_groups = cpu_groups;
  151. return num_groups;
  152. }
  153. static int hotplug_tests(void)
  154. {
  155. int i, nb_cpu_group, err = -ENOMEM;
  156. cpumask_var_t offlined_cpus, *cpu_groups;
  157. char *page_buf;
  158. if (!alloc_cpumask_var(&offlined_cpus, GFP_KERNEL))
  159. return err;
  160. nb_cpu_group = alloc_init_cpu_groups(&cpu_groups);
  161. if (nb_cpu_group < 0)
  162. goto out_free_cpus;
  163. page_buf = (char *)__get_free_page(GFP_KERNEL);
  164. if (!page_buf)
  165. goto out_free_cpu_groups;
  166. /*
  167. * Of course the last CPU cannot be powered down and cpu_down() should
  168. * refuse doing that.
  169. */
  170. pr_info("Trying to turn off and on again all CPUs\n");
  171. err = down_and_up_cpus(cpu_online_mask, offlined_cpus);
  172. /*
  173. * Take down CPUs by cpu group this time. When the last CPU is turned
  174. * off, the cpu group itself should shut down.
  175. */
  176. for (i = 0; i < nb_cpu_group; ++i) {
  177. ssize_t len = cpumap_print_to_pagebuf(true, page_buf,
  178. cpu_groups[i]);
  179. /* Remove trailing newline. */
  180. page_buf[len - 1] = '\0';
  181. pr_info("Trying to turn off and on again group %d (CPUs %s)\n",
  182. i, page_buf);
  183. err += down_and_up_cpus(cpu_groups[i], offlined_cpus);
  184. }
  185. free_page((unsigned long)page_buf);
  186. out_free_cpu_groups:
  187. free_cpu_groups(nb_cpu_group, &cpu_groups);
  188. out_free_cpus:
  189. free_cpumask_var(offlined_cpus);
  190. return err;
  191. }
  192. static void dummy_callback(struct timer_list *unused) {}
  193. static int suspend_cpu(struct cpuidle_device *dev,
  194. struct cpuidle_driver *drv, int index)
  195. {
  196. struct cpuidle_state *state = &drv->states[index];
  197. bool broadcast = state->flags & CPUIDLE_FLAG_TIMER_STOP;
  198. int ret;
  199. arch_cpu_idle_enter();
  200. if (broadcast) {
  201. /*
  202. * The local timer will be shut down, we need to enter tick
  203. * broadcast.
  204. */
  205. ret = tick_broadcast_enter();
  206. if (ret) {
  207. /*
  208. * In the absence of hardware broadcast mechanism,
  209. * this CPU might be used to broadcast wakeups, which
  210. * may be why entering tick broadcast has failed.
  211. * There is little the kernel can do to work around
  212. * that, so enter WFI instead (idle state 0).
  213. */
  214. cpu_do_idle();
  215. ret = 0;
  216. goto out_arch_exit;
  217. }
  218. }
  219. ret = state->enter(dev, drv, index);
  220. if (broadcast)
  221. tick_broadcast_exit();
  222. out_arch_exit:
  223. arch_cpu_idle_exit();
  224. return ret;
  225. }
  226. static int suspend_test_thread(void *arg)
  227. {
  228. int cpu = (long)arg;
  229. int i, nb_suspend = 0, nb_shallow_sleep = 0, nb_err = 0;
  230. struct cpuidle_device *dev;
  231. struct cpuidle_driver *drv;
  232. /* No need for an actual callback, we just want to wake up the CPU. */
  233. struct timer_list wakeup_timer;
  234. /* Wait for the main thread to give the start signal. */
  235. wait_for_completion(&suspend_threads_started);
  236. /* Set maximum priority to preempt all other threads on this CPU. */
  237. sched_set_fifo(current);
  238. dev = this_cpu_read(cpuidle_devices);
  239. drv = cpuidle_get_cpu_driver(dev);
  240. pr_info("CPU %d entering suspend cycles, states 1 through %d\n",
  241. cpu, drv->state_count - 1);
  242. timer_setup_on_stack(&wakeup_timer, dummy_callback, 0);
  243. for (i = 0; i < NUM_SUSPEND_CYCLE; ++i) {
  244. int index;
  245. /*
  246. * Test all possible states, except 0 (which is usually WFI and
  247. * doesn't use PSCI).
  248. */
  249. for (index = 1; index < drv->state_count; ++index) {
  250. int ret;
  251. struct cpuidle_state *state = &drv->states[index];
  252. /*
  253. * Set the timer to wake this CPU up in some time (which
  254. * should be largely sufficient for entering suspend).
  255. * If the local tick is disabled when entering suspend,
  256. * suspend_cpu() takes care of switching to a broadcast
  257. * tick, so the timer will still wake us up.
  258. */
  259. mod_timer(&wakeup_timer, jiffies +
  260. usecs_to_jiffies(state->target_residency));
  261. /* IRQs must be disabled during suspend operations. */
  262. local_irq_disable();
  263. ret = suspend_cpu(dev, drv, index);
  264. /*
  265. * We have woken up. Re-enable IRQs to handle any
  266. * pending interrupt, do not wait until the end of the
  267. * loop.
  268. */
  269. local_irq_enable();
  270. if (ret == index) {
  271. ++nb_suspend;
  272. } else if (ret >= 0) {
  273. /* We did not enter the expected state. */
  274. ++nb_shallow_sleep;
  275. } else {
  276. pr_err("Failed to suspend CPU %d: error %d "
  277. "(requested state %d, cycle %d)\n",
  278. cpu, ret, index, i);
  279. ++nb_err;
  280. }
  281. }
  282. }
  283. /*
  284. * Disable the timer to make sure that the timer will not trigger
  285. * later.
  286. */
  287. timer_delete(&wakeup_timer);
  288. timer_destroy_on_stack(&wakeup_timer);
  289. if (atomic_dec_return_relaxed(&nb_active_threads) == 0)
  290. complete(&suspend_threads_done);
  291. for (;;) {
  292. /* Needs to be set first to avoid missing a wakeup. */
  293. set_current_state(TASK_INTERRUPTIBLE);
  294. if (kthread_should_park())
  295. break;
  296. schedule();
  297. }
  298. pr_info("CPU %d suspend test results: success %d, shallow states %d, errors %d\n",
  299. cpu, nb_suspend, nb_shallow_sleep, nb_err);
  300. kthread_parkme();
  301. return nb_err;
  302. }
  303. static int suspend_tests(void)
  304. {
  305. int i, cpu, err = 0;
  306. struct task_struct **threads;
  307. int nb_threads = 0;
  308. threads = kmalloc_objs(*threads, nb_available_cpus);
  309. if (!threads)
  310. return -ENOMEM;
  311. /*
  312. * Stop cpuidle to prevent the idle tasks from entering a deep sleep
  313. * mode, as it might interfere with the suspend threads on other CPUs.
  314. * This does not prevent the suspend threads from using cpuidle (only
  315. * the idle tasks check this status). Take the idle lock so that
  316. * the cpuidle driver and device look-up can be carried out safely.
  317. */
  318. cpuidle_pause_and_lock();
  319. for_each_online_cpu(cpu) {
  320. struct task_struct *thread;
  321. /* Check that cpuidle is available on that CPU. */
  322. struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
  323. struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
  324. if (!dev || !drv) {
  325. pr_warn("cpuidle not available on CPU %d, ignoring\n",
  326. cpu);
  327. continue;
  328. }
  329. thread = kthread_create_on_cpu(suspend_test_thread,
  330. (void *)(long)cpu, cpu,
  331. "psci_suspend_test");
  332. if (IS_ERR(thread))
  333. pr_err("Failed to create kthread on CPU %d\n", cpu);
  334. else
  335. threads[nb_threads++] = thread;
  336. }
  337. if (nb_threads < 1) {
  338. err = -ENODEV;
  339. goto out;
  340. }
  341. atomic_set(&nb_active_threads, nb_threads);
  342. /*
  343. * Wake up the suspend threads. To avoid the main thread being preempted
  344. * before all the threads have been unparked, the suspend threads will
  345. * wait for the completion of suspend_threads_started.
  346. */
  347. for (i = 0; i < nb_threads; ++i)
  348. wake_up_process(threads[i]);
  349. complete_all(&suspend_threads_started);
  350. wait_for_completion(&suspend_threads_done);
  351. /* Stop and destroy all threads, get return status. */
  352. for (i = 0; i < nb_threads; ++i) {
  353. err += kthread_park(threads[i]);
  354. err += kthread_stop(threads[i]);
  355. }
  356. out:
  357. cpuidle_resume_and_unlock();
  358. kfree(threads);
  359. return err;
  360. }
  361. static int __init psci_checker(void)
  362. {
  363. int ret;
  364. /*
  365. * Since we're in an initcall, we assume that all the CPUs that all
  366. * CPUs that can be onlined have been onlined.
  367. *
  368. * The tests assume that hotplug is enabled but nobody else is using it,
  369. * otherwise the results will be unpredictable. However, since there
  370. * is no userspace yet in initcalls, that should be fine, as long as
  371. * no torture test is running at the same time (see Kconfig).
  372. */
  373. nb_available_cpus = num_online_cpus();
  374. /* Check PSCI operations are set up and working. */
  375. ret = psci_ops_check();
  376. if (ret)
  377. return ret;
  378. pr_info("PSCI checker started using %u CPUs\n", nb_available_cpus);
  379. pr_info("Starting hotplug tests\n");
  380. ret = hotplug_tests();
  381. if (ret == 0)
  382. pr_info("Hotplug tests passed OK\n");
  383. else if (ret > 0)
  384. pr_err("%d error(s) encountered in hotplug tests\n", ret);
  385. else {
  386. pr_err("Out of memory\n");
  387. return ret;
  388. }
  389. pr_info("Starting suspend tests (%d cycles per state)\n",
  390. NUM_SUSPEND_CYCLE);
  391. ret = suspend_tests();
  392. if (ret == 0)
  393. pr_info("Suspend tests passed OK\n");
  394. else if (ret > 0)
  395. pr_err("%d error(s) encountered in suspend tests\n", ret);
  396. else {
  397. switch (ret) {
  398. case -ENOMEM:
  399. pr_err("Out of memory\n");
  400. break;
  401. case -ENODEV:
  402. pr_warn("Could not start suspend tests on any CPU\n");
  403. break;
  404. }
  405. }
  406. pr_info("PSCI checker completed\n");
  407. return ret < 0 ? ret : 0;
  408. }
  409. late_initcall(psci_checker);