Skip to contentSkip to conversation

drivers: psci: PSCI checker module

PATCH Superseded Kevin Brodsky 6 participants 9 messages Sep 21, 2016 – Oct 24, 2016

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 drivers: psci: PSCI checker module

On arm and arm64, PSCI is one of the possible firmware interfaces used for power management. This includes both turning CPUs on and off, and suspending them (entering idle states).

This patch adds a PSCI checker module that enables basic testing of PSCI operations during startup. There are two main tests: CPU hotplugging and suspending.

In the hotplug tests, the hotplug API is used to turn off and on again all CPUs in the system, and then all CPUs in each cluster, checking the consistency of the return codes.

In the suspend tests, a high-priority thread is created on each core and uses low-level cpuidle functionalities to enter suspend, in all the possible states and multiple times. This should allow a maximum number of CPUs to enter the same sleep state at the same or slightly different time.

In essence, the suspend tests use a principle similar to that of the intel_powerclamp driver (drivers/thermal/intel_powerclamp.c), but the threads are only kept for the duration of the test (they are already gone when userspace is started).

While in theory power management PSCI functions (CPU_{ON,OFF,SUSPEND}) could be directly called, this proved too difficult as it would imply the duplication of all the logic used by the kernel to allow for a clean shutdown/bringup/suspend of the CPU (the deepest sleep states implying potentially the shutdown of the CPU).

Note that this file cannot be compiled as a loadable module, since it uses a number of non-exported identifiers (essentially for PSCI-specific checks and direct use of cpuidle) and relies on the absence of userspace to avoid races when calling hotplug and cpuidle functions.

Cc Thomas Gleixner <[email protected]>
Cc Kevin Hilman <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Sudeep Holla <[email protected]>
Cc Lorenzo Pieralisi <[email protected]>
Cc Mark Rutland <[email protected]>

---
Changelog v1..v2:
* Do not count tick_broadcast_enter() failures as errors, as they may be unavoidable. When it happens, fall back to WFI.
* Do not count unexpected sleep states as errors (currently, the only case is when falling back to WFI). Instead, report the number of times it happens before the suspend thread exits.
* Use usecs_to_jiffies() to compute the suspend timeout. The previous version resulted in a zero timeout if the target residency was shorter than a jiffy.
* Various cleanup.

Thanks to Lorenzo for his help with improving this patch!

Kevin

 drivers/firmware/Kconfig        |   7 +
 drivers/firmware/Makefile       |   1 +
 drivers/firmware/psci_checker.c | 488 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 496 insertions(+)
 create mode 100644 drivers/firmware/psci_checker.c
drivers/firmware/Kconfig +7 -0
index 0e22f241403b..0033c8755104 100644
@@ -206,6 +206,13 @@ config QCOM_SCM_64
 config HAVE_ARM_SMCCC
 	bool
 
+config PSCI_CHECKER
+	bool "PSCI checker"
+	depends on ARM_PSCI_FW && HOTPLUG_CPU
+	help
+	  Run the PSCI checker during startup. This checks that hotplug and
+	  suspend operations work correctly when using PSCI.
+
 source "drivers/firmware/broadcom/Kconfig"
 source "drivers/firmware/google/Kconfig"
 source "drivers/firmware/efi/Kconfig"
drivers/firmware/Makefile +1 -0
index 44a59dcfc398..3efc794906c6 100644
@@ -20,6 +20,7 @@ obj-$(CONFIG_QCOM_SCM)		+= qcom_scm.o
 obj-$(CONFIG_QCOM_SCM_64)	+= qcom_scm-64.o
 obj-$(CONFIG_QCOM_SCM_32)	+= qcom_scm-32.o
 CFLAGS_qcom_scm-32.o :=$(call as-instr,.arch armv7-a\n.arch_extension sec,-DREQUIRES_SEC=1) -march=armv7-a
+obj-$(CONFIG_PSCI_CHECKER)	+= psci_checker.o
 
 obj-y				+= broadcom/
 obj-$(CONFIG_GOOGLE_FIRMWARE)	+= google/
drivers/firmware/psci_checker.c new file +487 -0

487 lines not on this page · raw message

Signature · 1 line
2.9.3
#1 lore raw

PATCH v2 1/1 drivers: psci: PSCI checker module

Quoted 12 lines · Kevin Brodsky

On arm and arm64, PSCI is one of the possible firmware interfaces used for power management. This includes both turning CPUs on and off, and suspending them (entering idle states).

This patch adds a PSCI checker module that enables basic testing of PSCI operations during startup. There are two main tests: CPU hotplugging and suspending.

In the hotplug tests, the hotplug API is used to turn off and on again all CPUs in the system, and then all CPUs in each cluster, checking the consistency of the return codes.

Quoted 5 lines

In the suspend tests, a high-priority thread is created on each core and uses low-level cpuidle functionalities to enter suspend, in all the possible states and multiple times. This should allow a maximum number of CPUs to enter the same sleep state at the same or slightly different time.

Since you're doing it multiple times, it might be useful to track the actual time it takes to suspend and wakeup, and possibly even warn if the time is longer than the target_residency. This could catch poorly configured idle states.

Kevin

#2 lore raw

PATCH v2 1/1 drivers: psci: PSCI checker module

Quoted 20 lines · Kevin Hilman
Quoted 12 lines · Kevin Brodsky

On arm and arm64, PSCI is one of the possible firmware interfaces used for power management. This includes both turning CPUs on and off, and suspending them (entering idle states).

This patch adds a PSCI checker module that enables basic testing of PSCI operations during startup. There are two main tests: CPU hotplugging and suspending.

In the hotplug tests, the hotplug API is used to turn off and on again all CPUs in the system, and then all CPUs in each cluster, checking the consistency of the return codes.

Quoted 5 lines

In the suspend tests, a high-priority thread is created on each core and uses low-level cpuidle functionalities to enter suspend, in all the possible states and multiple times. This should allow a maximum number of CPUs to enter the same sleep state at the same or slightly different time.

Quoted 4 lines

Since you're doing it multiple times, it might be useful to track the actual time it takes to suspend and wakeup, and possibly even warn if the time is longer than the target_residency. This could catch poorly configured idle states.

That's a good point, we can add a couple of checks for latencies on top of the existing code, we will definitely do, at the moment we are concerned with the overall design in particular in relation to usage of high priority threads and tick broadcast to test the PSCI suspend interface.

Thank you for having a look !

Lorenzo

#3 lore raw

PATCH v2 1/1 drivers: psci: PSCI checker module

In reply to Kevin Brodsky

Hi Kevin,

Quoted 58 lines · Kevin Brodsky

On arm and arm64, PSCI is one of the possible firmware interfaces used for power management. This includes both turning CPUs on and off, and suspending them (entering idle states).

This patch adds a PSCI checker module that enables basic testing of PSCI operations during startup. There are two main tests: CPU hotplugging and suspending.

In the hotplug tests, the hotplug API is used to turn off and on again all CPUs in the system, and then all CPUs in each cluster, checking the consistency of the return codes.

In the suspend tests, a high-priority thread is created on each core and uses low-level cpuidle functionalities to enter suspend, in all the possible states and multiple times. This should allow a maximum number of CPUs to enter the same sleep state at the same or slightly different time.

In essence, the suspend tests use a principle similar to that of the intel_powerclamp driver (drivers/thermal/intel_powerclamp.c), but the threads are only kept for the duration of the test (they are already gone when userspace is started).

While in theory power management PSCI functions (CPU_{ON,OFF,SUSPEND}) could be directly called, this proved too difficult as it would imply the duplication of all the logic used by the kernel to allow for a clean shutdown/bringup/suspend of the CPU (the deepest sleep states implying potentially the shutdown of the CPU).

Note that this file cannot be compiled as a loadable module, since it uses a number of non-exported identifiers (essentially for PSCI-specific checks and direct use of cpuidle) and relies on the absence of userspace to avoid races when calling hotplug and cpuidle functions.

Cc Thomas Gleixner <[email protected]>
Cc Kevin Hilman <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Sudeep Holla <[email protected]>
Cc Lorenzo Pieralisi <[email protected]>
Cc Mark Rutland <[email protected]>

---
Changelog v1..v2:
* Do not count tick_broadcast_enter() failures as errors, as they may be unavoidable. When it happens, fall back to WFI.
* Do not count unexpected sleep states as errors (currently, the only case is when falling back to WFI). Instead, report the number of times it happens before the suspend thread exits.
* Use usecs_to_jiffies() to compute the suspend timeout. The previous version resulted in a zero timeout if the target residency was shorter than a jiffy.
* Various cleanup.

Thanks to Lorenzo for his help with improving this patch!

Kevin

[...]

Quoted 44 lines
drivers/firmware/psci_checker.c new file +44 -0
+
+static int suspend_tests(void)
+{
+	int i, cpu, err = 0;
+	struct task_struct **threads;
+	int nb_threads = 0;
+
+	threads = kmalloc_array(nb_available_cpus, sizeof(*threads),
+				GFP_KERNEL);
+	if (!threads)
+		return -ENOMEM;
+
+	for_each_online_cpu(cpu) {
+		struct task_struct *thread;
+		/* Check that cpuidle is available on that CPU. */
+		struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
+		struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
+
+		if (cpuidle_not_available(drv, dev)) {
+			pr_warn("cpuidle not available on CPU %d, ignoring\n",
+				cpu);
+			continue;
+		}
+
+		thread = kthread_create_on_cpu(suspend_stress_thread,
+					       (void *)(long)cpu, cpu,
+					       "psci_suspend_stress");
+		if (IS_ERR(thread))
+			pr_err("Failed to create kthread on CPU %d\n", cpu);
+		else
+			threads[nb_threads++] = thread;
+	}
+	if (nb_threads < 1) {
+		kfree(threads);
+		return -ENODEV;
+	}
+
+	atomic_set(&nb_active_threads, nb_threads);
+
+	/*
+	 * Stop cpuidle to prevent the idle tasks from entering a deep sleep
+	 * mode, as it might interfere with the suspend threads on other CPUs.
+	 * This does not prevent the suspend threads from using cpuidle (only
+	 * the idle tasks check this status).

drivers/firmware/psci_checker.c line 419

Quoted 10 lines
drivers/firmware/psci_checker.c new file +10 -0
+	 */
+	cpuidle_pause();
+
+	/*
+	 * Unpark the suspend threads. To avoid the main thread being preempted
+	 * before all the threads have been unparked, the suspend threads will
+	 * wait for the completion of suspend_threads_started.
+	 */
+	for (i = 0; i < nb_threads; ++i)
+		kthread_unpark(threads[i]);

Just a heads up: this doesn't work anymore, since a65d4096 (kthread/smpboot: do not park in kthread_create_on_cpu()), in 4.9-rc1. I think that the unpark call could be replaced by wake_up_process. The comment of kthread_create_on_cpu is now misleading.

Thanks,
Jean-Philippe

#4 lore raw

PATCH v2 1/1 drivers: psci: PSCI checker module

Hi Jean-Philippe,

Quoted 121 lines · Jean-Philippe Brucker

Hi Kevin,

Quoted 58 lines · Kevin Brodsky

On arm and arm64, PSCI is one of the possible firmware interfaces used for power management. This includes both turning CPUs on and off, and suspending them (entering idle states).

This patch adds a PSCI checker module that enables basic testing of PSCI operations during startup. There are two main tests: CPU hotplugging and suspending.

In the hotplug tests, the hotplug API is used to turn off and on again all CPUs in the system, and then all CPUs in each cluster, checking the consistency of the return codes.

In the suspend tests, a high-priority thread is created on each core and uses low-level cpuidle functionalities to enter suspend, in all the possible states and multiple times. This should allow a maximum number of CPUs to enter the same sleep state at the same or slightly different time.

In essence, the suspend tests use a principle similar to that of the intel_powerclamp driver (drivers/thermal/intel_powerclamp.c), but the threads are only kept for the duration of the test (they are already gone when userspace is started).

While in theory power management PSCI functions (CPU_{ON,OFF,SUSPEND}) could be directly called, this proved too difficult as it would imply the duplication of all the logic used by the kernel to allow for a clean shutdown/bringup/suspend of the CPU (the deepest sleep states implying potentially the shutdown of the CPU).

Note that this file cannot be compiled as a loadable module, since it uses a number of non-exported identifiers (essentially for PSCI-specific checks and direct use of cpuidle) and relies on the absence of userspace to avoid races when calling hotplug and cpuidle functions.

Cc Thomas Gleixner <[email protected]>
Cc Kevin Hilman <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Sudeep Holla <[email protected]>
Cc Lorenzo Pieralisi <[email protected]>
Cc Mark Rutland <[email protected]>

---
Changelog v1..v2:
* Do not count tick_broadcast_enter() failures as errors, as they may be unavoidable. When it happens, fall back to WFI.
* Do not count unexpected sleep states as errors (currently, the only case is when falling back to WFI). Instead, report the number of times it happens before the suspend thread exits.
* Use usecs_to_jiffies() to compute the suspend timeout. The previous version resulted in a zero timeout if the target residency was shorter than a jiffy.
* Various cleanup.

Thanks to Lorenzo for his help with improving this patch!

Kevin

[...]

Quoted 44 lines
drivers/firmware/psci_checker.c new file +44 -0
+
+static int suspend_tests(void)
+{
+    int i, cpu, err = 0;
+    struct task_struct **threads;
+    int nb_threads = 0;
+
+    threads = kmalloc_array(nb_available_cpus, sizeof(*threads),
+                            GFP_KERNEL);
+    if (!threads)
+            return -ENOMEM;
+
+    for_each_online_cpu(cpu) {
+            struct task_struct *thread;
+            /* Check that cpuidle is available on that CPU. */
+            struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
+            struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
+
+            if (cpuidle_not_available(drv, dev)) {
+                    pr_warn("cpuidle not available on CPU %d, ignoring\n",
+                            cpu);
+                    continue;
+            }
+
+            thread = kthread_create_on_cpu(suspend_stress_thread,
+                                           (void *)(long)cpu, cpu,
+                                           "psci_suspend_stress");
+            if (IS_ERR(thread))
+                    pr_err("Failed to create kthread on CPU %d\n", cpu);
+            else
+                    threads[nb_threads++] = thread;
+    }
+    if (nb_threads < 1) {
+            kfree(threads);
+            return -ENODEV;
+    }
+
+    atomic_set(&nb_active_threads, nb_threads);
+
+    /*
+     * Stop cpuidle to prevent the idle tasks from entering a deep sleep
+     * mode, as it might interfere with the suspend threads on other CPUs.
+     * This does not prevent the suspend threads from using cpuidle (only
+     * the idle tasks check this status).

drivers/firmware/psci_checker.c line 419

Quoted 10 lines
drivers/firmware/psci_checker.c new file +10 -0
+     */
+    cpuidle_pause();
+
+    /*
+     * Unpark the suspend threads. To avoid the main thread being preempted
+     * before all the threads have been unparked, the suspend threads will
+     * wait for the completion of suspend_threads_started.
+     */
+    for (i = 0; i < nb_threads; ++i)
+            kthread_unpark(threads[i]);

Just a heads up: this doesn't work anymore, since a65d4096 (kthread/smpboot: do not park in kthread_create_on_cpu()), in 4.9-rc1. I think that the unpark call could be replaced by wake_up_process. The comment of kthread_create_on_cpu is now misleading.

Quoted 3 lines

Thanks,
Jean-Philippe

Thanks for the heads-up! I'll rebase on 4.9-rc1 and see what needs to be done.

Thanks,
Kevin

IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

#5 lore raw

PATCH v2 1/1 drivers: psci: PSCI checker module

On 20/10/16 14:38, Kevin Brodsky wrote:

[...]

Quoted 4 lines

Thanks for the heads-up! I'll rebase on 4.9-rc1 and see what needs to be done.

Just be aware that v4.9-rc1 doesn't have commit 9cfb38a7ba5a ("sched/fair: Fix sched domains NULL dereference in select_idle_sibling()") which fixes the cpuhotplug issue you would observe.

Signature · 2 lines
Regards,
Sudeep
#6 lore raw

PATCH v2 1/1 drivers: psci: PSCI checker module

Quoted 5 lines · Sudeep Holla

On 20/10/16 14:38, Kevin Brodsky wrote:

[...]

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

Thanks for the heads-up! I'll rebase on 4.9-rc1 and see what needs to be done.

Just be aware that v4.9-rc1 doesn't have commit 9cfb38a7ba5a ("sched/fair: Fix sched domains NULL dereference in select_idle_sibling()") which fixes the cpuhotplug issue you would observe.

Good to know. I saw that issue during resume from s2ram on r8a7795/Salvator-X once, but couldn't reproduce it.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert at linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#7 lore raw

PATCH v2 1/1 drivers: psci: PSCI checker module

Quoted 10 lines · Geert Uytterhoeven
Quoted 9 lines · Sudeep Holla

On 20/10/16 14:38, Kevin Brodsky wrote:

[...]

Quoted 4 lines

Thanks for the heads-up! I'll rebase on 4.9-rc1 and see what needs to be done.

Quoted 8 lines
Quoted 4 lines

Just be aware that v4.9-rc1 doesn't have commit 9cfb38a7ba5a ("sched/fair: Fix sched domains NULL dereference in select_idle_sibling()") which fixes the cpuhotplug issue you would observe.

Good to know. I saw that issue during resume from s2ram on r8a7795/Salvator-X once, but couldn't reproduce it.

Did you try v4.9-rc1 itself ? The above commit is present post -rc1 and must be in -rc2. I can consistently see the crash without the commit.

Signature · 2 lines
Regards,
Sudeep
#8 lore raw

PATCH v2 1/1 drivers: psci: PSCI checker module

Hi Sudeep,

Quoted 18 lines · Sudeep Holla
Quoted 17 lines · Geert Uytterhoeven
Quoted 13 lines · Sudeep Holla

On 20/10/16 14:38, Kevin Brodsky wrote:

[...]

Quoted 4 lines

Thanks for the heads-up! I'll rebase on 4.9-rc1 and see what needs to be done.

Just be aware that v4.9-rc1 doesn't have commit 9cfb38a7ba5a ("sched/fair: Fix sched domains NULL dereference in select_idle_sibling()") which fixes the cpuhotplug issue you would observe.

Quoted 7 lines
Quoted 4 lines

Good to know. I saw that issue during resume from s2ram on r8a7795/Salvator-X
once, but couldn't reproduce it.

Did you try v4.9-rc1 itself ? The above commit is present post -rc1 and must be in -rc2. I can consistently see the crash without the commit.

I used a tree based on v4.9-rc1, not including commit 9cfb38a7ba5a. Will try v4.9-rc2 shortly...

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert at linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds