Skip to contentSkip to conversation

cpufreq: Allow light-weight tear down on CPU offline

PATCH In review Viresh Kumar 2 participants 5 messages Feb 11, 2019 CPU frequency scaling framework

lore
Share & tools
lore

Viresh Kumar

author maintainer
#0 lore raw

cover cpufreq: Allow light-weight tear down on CPU offline

The cpufreq core doesn't remove the cpufreq policy anymore on CPU offline operation, rather that happens when the CPU device gets unregistered from the kernel. This allows faster recovery when the CPU comes back online. This is also very useful during system wide suspend/resume where we offline all non-boot CPUs during suspend and then bring them back on resume.

This patcset takes the same idea a step ahead to allow drivers to do light weight tear-down during CPU offline operation and updates the cpufreq-dt driver to implement the new helper.

Viresh Kumar (2):
cpufreq: Allow light-weight tear down on CPU offline operation
cpufreq: dt: Implement light_weight_exit() callback

 drivers/cpufreq/cpufreq-dt.c | 19 +++++++++++++++++++
 drivers/cpufreq/cpufreq.c    | 16 +++++++++++-----
 include/linux/cpufreq.h      |  1 +
 3 files changed, 31 insertions(+), 5 deletions(-)
Signature · 1 line
2.20.1.321.g9e740568ce00
author maintainer
#1 lore raw

PATCH 1/2 cpufreq: Allow light-weight tear down on CPU offline operation

The cpufreq core doesn't remove the cpufreq policy anymore on CPU offline operation, rather that happens when the CPU device gets unregistered from the kernel. This allows faster recovery when the CPU comes back online. This is also very useful during system wide suspend/resume where we offline all non-boot CPUs during suspend and then bring them back on resume.

This commit takes the same idea a step ahead to allow drivers to do light weight tear-down during CPU offline operation.

A new callback is introduced, light_weight_exit(), which gets called when all the CPUs of a policy are removed/offlined and the existing exit() callback gets called when the policy gets freed.

---
 drivers/cpufreq/cpufreq.c | 16 +++++++++++-----
 include/linux/cpufreq.h   |  1 +
 2 files changed, 12 insertions(+), 5 deletions(-)
drivers/cpufreq/cpufreq.c +11 -5
index 96a69c67a545..215a8bfc78bb 100644
@@ -1421,11 +1421,12 @@ static int cpufreq_offline(unsigned int cpu)
 		cpufreq_exit_governor(policy);
 
 	/*
-	 * Perform the ->exit() even during light-weight tear-down,
-	 * since this is a core component, and is essential for the
-	 * subsequent light-weight ->init() to succeed.
+	 * Perform the ->light_weight_exit() during light-weight tear-down, as
+	 * that allows fast recovery when the CPU comes back.
 	 */
-	if (cpufreq_driver->exit) {
+	if (cpufreq_driver->light_weight_exit) {
+		cpufreq_driver->light_weight_exit(policy);
+	} else if (cpufreq_driver->exit) {
 		cpufreq_driver->exit(policy);
 		policy->freq_table = NULL;
 	}
@@ -1454,8 +1455,13 @@ static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
 	cpumask_clear_cpu(cpu, policy->real_cpus);
 	remove_cpu_dev_symlink(policy, dev);
 
-	if (cpumask_empty(policy->real_cpus))
+	if (cpumask_empty(policy->real_cpus)) {
+		/* We did light-weight exit earlier, do full tear down now */
+		if (cpufreq_driver->light_weight_exit)
+			cpufreq_driver->exit(policy);
+
 		cpufreq_policy_free(policy);
+	}
 }
 
 /**
include/linux/cpufreq.h +1 -0
index 9db074ecbbd7..36ce31516041 100644
@@ -325,6 +325,7 @@ struct cpufreq_driver {
 	/* optional */
 	int		(*bios_limit)(int cpu, unsigned int *limit);
 
+	int		(*light_weight_exit)(struct cpufreq_policy *policy);
 	int		(*exit)(struct cpufreq_policy *policy);
 	void		(*stop_cpu)(struct cpufreq_policy *policy);
 	int		(*suspend)(struct cpufreq_policy *policy);
Signature · 1 line
2.20.1.321.g9e740568ce00
#2 lore raw

Re: [PATCH 1/2] cpufreq: Allow light-weight tear down on CPU offline operation

Quoted 62 lines · Viresh Kumar

The cpufreq core doesn't remove the cpufreq policy anymore on CPU offline operation, rather that happens when the CPU device gets unregistered from the kernel. This allows faster recovery when the CPU comes back online. This is also very useful during system wide suspend/resume where we offline all non-boot CPUs during suspend and then bring them back on resume.

This commit takes the same idea a step ahead to allow drivers to do light weight tear-down during CPU offline operation.

A new callback is introduced, light_weight_exit(), which gets called when all the CPUs of a policy are removed/offlined and the existing exit() callback gets called when the policy gets freed.

---
 drivers/cpufreq/cpufreq.c | 16 +++++++++++-----
 include/linux/cpufreq.h   |  1 +
 2 files changed, 12 insertions(+), 5 deletions(-)
drivers/cpufreq/cpufreq.c +11 -5
index 96a69c67a545..215a8bfc78bb 100644
@@ -1421,11 +1421,12 @@ static int cpufreq_offline(unsigned int cpu)
                cpufreq_exit_governor(policy);
 
        /*
-        * Perform the ->exit() even during light-weight tear-down,
-        * since this is a core component, and is essential for the
-        * subsequent light-weight ->init() to succeed.
+        * Perform the ->light_weight_exit() during light-weight tear-down, as
+        * that allows fast recovery when the CPU comes back.
         */
-       if (cpufreq_driver->exit) {
+       if (cpufreq_driver->light_weight_exit) {
+               cpufreq_driver->light_weight_exit(policy);
+       } else if (cpufreq_driver->exit) {
                cpufreq_driver->exit(policy);
                policy->freq_table = NULL;
        }
@@ -1454,8 +1455,13 @@ static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
        cpumask_clear_cpu(cpu, policy->real_cpus);
        remove_cpu_dev_symlink(policy, dev);
 
-       if (cpumask_empty(policy->real_cpus))
+       if (cpumask_empty(policy->real_cpus)) {
+               /* We did light-weight exit earlier, do full tear down now */
+               if (cpufreq_driver->light_weight_exit)
+                       cpufreq_driver->exit(policy);
+
                cpufreq_policy_free(policy);
+       }
 }
 
 /**
include/linux/cpufreq.h +0 -0
index 9db074ecbbd7..36ce31516041 100644

include/linux/cpufreq.h struct cpufreq_driver { · line 328

Quoted 4 lines
include/linux/cpufreq.h +1 -0
@@ -325,6 +325,7 @@ struct cpufreq_driver {
        /* optional */
        int             (*bios_limit)(int cpu, unsigned int *limit);
 
+       int             (*light_weight_exit)(struct cpufreq_policy *policy);

Can you call it "offline"?

Quoted 4 lines
include/linux/cpufreq.h +0 -1
        int             (*exit)(struct cpufreq_policy *policy);
        void            (*stop_cpu)(struct cpufreq_policy *policy);
        int             (*suspend)(struct cpufreq_policy *policy);
--
#3 lore raw

Re: [PATCH 1/2] cpufreq: Allow light-weight tear down on CPU offline operation

Quoted 19 lines · Rafael J. Wysocki
Quoted 17 lines · Viresh Kumar

The cpufreq core doesn't remove the cpufreq policy anymore on CPU offline operation, rather that happens when the CPU device gets unregistered from the kernel. This allows faster recovery when the CPU comes back online. This is also very useful during system wide suspend/resume where we offline all non-boot CPUs during suspend and then bring them back on resume.

This commit takes the same idea a step ahead to allow drivers to do light weight tear-down during CPU offline operation.

A new callback is introduced, light_weight_exit(), which gets called when all the CPUs of a policy are removed/offlined and the existing exit() callback gets called when the policy gets freed.

---

[cut]

Quoted 8 lines
Quoted 8 lines
include/linux/cpufreq.h +0 -0
index 9db074ecbbd7..36ce31516041 100644
@@ -325,6 +325,7 @@ struct cpufreq_driver {
        /* optional */
        int             (*bios_limit)(int cpu, unsigned int *limit);
 
Quoted 8 lines

include/linux/cpufreq.h struct cpufreq_driver { · line 328

Quoted 1 line
include/linux/cpufreq.h +1 -0
+       int             (*light_weight_exit)(struct cpufreq_policy *policy);

Can you call it "offline"?

Quoted 4 lines
include/linux/cpufreq.h +0 -1
        int             (*exit)(struct cpufreq_policy *policy);
        void            (*stop_cpu)(struct cpufreq_policy *policy);
        int             (*suspend)(struct cpufreq_policy *policy);
--

Also, I would prefer a corresponding "online" callback to be there too for symmetry and I'd prefer the core to decide to call "online" instead of "init" for the light-weight case instead of ->init() itself having to figure out the context.

author maintainer
#4 lore raw

PATCH 2/2 cpufreq: dt: Implement light_weight_exit() callback

In reply to Viresh Kumar

Implement the light-weight tear down helper to reduce the amount of work to do on CPU offline/online operation.

---
 drivers/cpufreq/cpufreq-dt.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/cpufreq/cpufreq-dt.c +19 -0
index 7ba392911cd0..69309a8121f0 100644
@@ -160,6 +160,15 @@ static int cpufreq_init(struct cpufreq_policy *policy)
 	const char *name;
 	int ret;
 
+	/*
+	 * We did light-weight shutdown earlier, nothing else to initialize here
+	 * apart from policy->cpus.
+	 */
+	if (policy->driver_data) {
+		cpumask_copy(policy->cpus, policy->related_cpus);
+		return 0;
+	}
+
 	cpu_dev = get_cpu_device(policy->cpu);
 	if (!cpu_dev) {
 		pr_err("failed to get cpu%d device\n", policy->cpu);
@@ -295,6 +304,15 @@ static int cpufreq_init(struct cpufreq_policy *policy)
 	return ret;
 }
 
+static int cpufreq_light_weight_exit(struct cpufreq_policy *policy)
+{
+	/*
+	 * Preserve policy->driver_data and don't free resources on light-weight
+	 * tear down.
+	 */
+	return 0;
+}
+
 static int cpufreq_exit(struct cpufreq_policy *policy)
 {
 	struct private_data *priv = policy->driver_data;
@@ -319,6 +337,7 @@ static struct cpufreq_driver dt_cpufreq_driver = {
 	.get = cpufreq_generic_get,
 	.init = cpufreq_init,
 	.exit = cpufreq_exit,
+	.light_weight_exit = cpufreq_light_weight_exit,
 	.name = "cpufreq-dt",
 	.attr = cpufreq_dt_attr,
 	.suspend = cpufreq_generic_suspend,
Signature · 1 line
2.20.1.321.g9e740568ce00