psci_checker.c 13 KB

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