Skip to contentSkip to conversation

PM / OPP: Don't check not plugged in CPUs to avoid error

PATCH Stephen Boyd 3 participants 7 messages Jul 13, 2017 – Jul 20, 2017 1 message not in the archive

lore
Share & tools
lore

Stephen Boyd

author Reviewed-by
#1 lore raw
In reply to a message not in the archive
Quoted 9 lines · Waldemar Rymarkiewicz

On systems where not all possible CPUs are available for Linux call to dev_pm_opp_of_get_sharing_cpus() will fail as cannot find a cpu device for not present (not available) CPUs.

Example (real use case):
2 physical MIPS cores, 4 VPE, cpu0/2 run Linux and cpu1/3 are not available for linux. cpufreq-dt driver + opp v2 fail to register opp_table due to the fact there is no struct device for cpu1 (cpu1 not available for Linux).

Quoted 4 lines

Solve the problem by iterating over cpu_present_mask instead of cpu_possible_mask.

Quoted 9 lines
---
 drivers/base/power/opp/of.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/base/power/opp/of.c +0 -0
index 57eec1c..389c924 100644

drivers/base/power/opp/of.c int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, · line 595

Quoted 8 lines
drivers/base/power/opp/of.c +1 -1
@@ -589,7 +589,7 @@ int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
 	if (!of_property_read_bool(np, "opp-shared"))
 		goto put_cpu_node;
 
-	for_each_possible_cpu(cpu) {
+	for_each_present_cpu(cpu) {
 		if (cpu == cpu_dev->id)
 			continue;
 

Is there some way to iterate through all cpu nodes in DT without using devices? I suppose some sort of loop on top of of_get_cpu_node().

I worry that we may have some problem where we want to know about all the CPUs that are sharing a particular OPP table but they haven't been physically hotplugged yet. This also raises the question about what the DT looks like before and after a CPU is inserted into a system. If the non-present CPU nodes are in DT all the time, then it may make sense for cpufreq-dt to intersect the OPP sharing mask with the cpu_present_mask.

Probably nobody cares about knowing this difference though, because we pretty much forward the result to cpufreq, so moving to the present mask is good enough.

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
Acked-by
#2 lore raw
Quoted 10 lines · Stephen Boyd
Quoted 9 lines · Waldemar Rymarkiewicz

On systems where not all possible CPUs are available for Linux call to dev_pm_opp_of_get_sharing_cpus() will fail as cannot find a cpu device for not present (not available) CPUs.

Example (real use case):
2 physical MIPS cores, 4 VPE, cpu0/2 run Linux and cpu1/3 are not available for linux. cpufreq-dt driver + opp v2 fail to register opp_table due to the fact there is no struct device for cpu1 (cpu1 not available for Linux).

Quoted 6 lines
Quoted 4 lines

Solve the problem by iterating over cpu_present_mask instead of cpu_possible_mask.

Quoted 12 lines
Quoted 12 lines
---
 drivers/base/power/opp/of.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/base/power/opp/of.c +0 -0
index 57eec1c..389c924 100644
@@ -589,7 +589,7 @@ int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
 	if (!of_property_read_bool(np, "opp-shared"))
 		goto put_cpu_node;
 
Quoted 9 lines

drivers/base/power/opp/of.c int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, · line 595

Quoted 5 lines
drivers/base/power/opp/of.c +1 -1
-	for_each_possible_cpu(cpu) {
+	for_each_present_cpu(cpu) {
 		if (cpu == cpu_dev->id)
 			continue;
 

Is there some way to iterate through all cpu nodes in DT without using devices? I suppose some sort of loop on top of of_get_cpu_node().

I am looking for the same thing :)

Quoted 3 lines

I worry that we may have some problem where we want to know about all the CPUs that are sharing a particular OPP table but they haven't been physically hotplugged yet.

We will have a problem if we are going to use OPP framework on platforms where CPUs are hotpluggable. The cpumask returned by this function is used by the cpufreq drivers to get the related_cpus mask and that will be used when a cpu is inserted in later on.

Do we have such users of OPP framework right now ? ARM doesn't have such platforms AFAIK. Does MIPS have it ?

Quoted 5 lines

This also raises the
question about what the DT looks like before and after a CPU is inserted into a system. If the non-present CPU nodes are in DT all the time, then it may make sense for cpufreq-dt to intersect the OPP sharing mask with the cpu_present_mask.

We need the related-cpus mask in cpufreq to include non-present CPUs as well.

Signature · 1 line
viresh
#3 lore raw
Quoted 12 lines · Viresh Kumar
Quoted 5 lines · Stephen Boyd

On 07/13, Waldemar Rymarkiewicz wrote:

Is there some way to iterate through all cpu nodes in DT without using devices? I suppose some sort of loop on top of of_get_cpu_node().

I am looking for the same thing :)

Quoted 3 lines

I worry that we may have some problem where we want to know about all the CPUs that are sharing a particular OPP table but they haven't been physically hotplugged yet.

Isn't that cpufreq registers only online CPUs at boot up and will add hot-added CPUs later on calling cpufreq_online() ~> cpufreq_driver->init()?

See cpufreq_online()

http://elixir.free-electrons.com/linux/v4.13-rc1/source/drivers/cpufreq/cpufreq.c#L1143

this calls cpufreq_driver->init() which check for sharing CPUs. If that's called for online_cpus only so I guess it's OK to check only within online cpus for shared freq_table. If a cpu is plugged in later
this is called again, so we can eventually add this cpu to the mask.
Is this right?

Quoted 4 lines

We will have a problem if we are going to use OPP framework on platforms where CPUs are hotpluggable. The cpumask returned by this function is used by the cpufreq drivers to get the related_cpus mask and that will be used when a cpu is inserted in later on.

related_cpus bitmask is set in cpufreq_online() too,

http://elixir.free-electrons.com/linux/v4.13-rc1/source/drivers/cpufreq/cpufreq.c#L1188

Policy->cpus is for online CPUs, so where are the offline CPUs added to related_cpus mask?

Quoted 2 lines

Do we have such users of OPP framework right now ? ARM doesn't have such platforms AFAIK. Does MIPS have it ?

I don't think so there is an up streamed code that uses opp framework in such use case. I use MIPS platform which starts with maxcpu=4 but it puts 2 cpus offline at boot up for some reason. Will try to check how cpufreq behave if I hot-add a CPU in runtime.

Quoted 8 lines
Quoted 5 lines

This also raises the
question about what the DT looks like before and after a CPU is inserted into a system. If the non-present CPU nodes are in DT all the time, then it may make sense for cpufreq-dt to intersect the OPP sharing mask with the cpu_present_mask.

We need the related-cpus mask in cpufreq to include non-present CPUs as well.

Why? can you refer to the code? I'd like to see it and understand better.

/Waldek

Quoted 3 lines

--
viresh

#4 lore raw
Quoted 2 lines · Waldemar Rymarkiewicz

Isn't that cpufreq registers only online CPUs at boot up and will add hot-added CPUs later on calling cpufreq_online() ~>

Yes, but its a bit complicated than that.

Quoted 1 line

cpufreq_driver->init()?

init() is called only once for a cpufreq policy and it should fill policy->cpus with all the *possible* CPUs that share their clock/voltage rails. i.e. it includes the CPUs which aren't hotplugged in yet.

Later on when the CPU gets hotplugged in, we check it in existing policies ->related_cpus field. If its their, then very minimal stuff is done, else a new policy is created.

Quoted 10 lines

this calls cpufreq_driver->init() which check for sharing CPUs. If that's called for online_cpus only so I guess it's OK to check only within online cpus for shared freq_table. If a cpu is plugged in later
this is called again, so we can eventually add this cpu to the mask.
Is this right?

Quoted 4 lines

We will have a problem if we are going to use OPP framework on platforms where CPUs are hotpluggable. The cpumask returned by this function is used by the cpufreq drivers to get the related_cpus mask and that will be used when a cpu is inserted in later on.

And that's why I said this.

Quoted 6 lines

related_cpus bitmask is set in cpufreq_online() too,

   http://elixir.free-electrons.com/linux/v4.13-rc1/source/drivers/cpufreq/cpufreq.c#L1188

Policy->cpus is for online CPUs, so where are the offline CPUs added to related_cpus mask?

So policy->init() sets policy->cpus to all online+offline CPUs. That is copied to related_cpus by core and then policy->cpus is capped to only have online CPUs.

Quoted 4 lines

I don't think so there is an up streamed code that uses opp framework in such use case. I use MIPS platform which starts with maxcpu=4 but it puts 2 cpus offline at boot up for some reason. Will try to check how cpufreq behave if I hot-add a CPU in runtime.

Ok.

Signature · 1 line
viresh
#5 lore raw
Quoted 8 lines · Viresh Kumar

On 17-07-17, 15:03, Waldemar Rymarkiewicz wrote:
init() is called only once for a cpufreq policy and it should fill policy->cpus with all the *possible* CPUs that share their clock/voltage rails. i.e. it includes the CPUs which aren't hotplugged in yet.
Later on when the CPU gets hotplugged in, we check it in existing policies ->related_cpus field. If its their, then very minimal stuff is done, else a new policy is created.

I see. That is not obvious from the code what policy->cpus should initially include. Now I understand.

policy->related_cpu contains online+offline cpus that share this policy policy->cpus initially includes online+offline, but end the end it keeps online cpus mask only that share this policy.

Quoted 10 lines

So policy->init() sets policy->cpus to all online+offline CPUs. That is copied to related_cpus by core and then policy->cpus is capped to only have online CPUs.

Quoted 4 lines

I don't think so there is an up streamed code that uses opp framework in such use case. I use MIPS platform which starts with maxcpu=4 but it puts 2 cpus offline at boot up for some reason. Will try to check how cpufreq behave if I hot-add a CPU in runtime.

Ok.

Unfortunately, I failed to test this on my platform, seems like hotplug is not fully supported yet.

Anyway, below a proposal how we can solve the problem of getting shared CPUs when hotplug is used.

@@ -555,8 +576,7 @@ EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
struct cpumask *cpumask)
{

-       struct device_node *np, *tmp_np;
-       struct device *tcpu_dev;
+       struct device_node *np, *tmp_np, *cpu_np;
        int cpu, ret = 0;

        /* Get OPP descriptor node */
@@ -572,22 +592,16 @@ int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
        if (!of_property_read_bool(np, "opp-shared"))
                goto put_cpu_node;

- for_each_possible_cpu(cpu) {
+ for_each_node_by_type(cpu_np, "cpu") {
+ cpu = of_device_node_get_cpu(cpu_np);
+

                if (cpu == cpu_dev->id)
                        continue;

- tcpu_dev = get_cpu_device(cpu);
- if (!tcpu_dev) {
- dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
- __func__, cpu);
- ret = -ENODEV;
- goto put_cpu_node;
- }
-

                /* Get OPP descriptor node */
-               tmp_np = _of_get_opp_desc_node(tcpu_dev);
+               tmp_np = of_parse_phandle(cpu_np, "operating-points-v2", 0);
                if (!tmp_np) {
-                       dev_err(tcpu_dev, "%s: Couldn't find tcpu_dev node.\n",
+                       dev_err(cpu_dev, "%s: Couldn't find tcpu_dev node.\n",
                                __func__);
                        ret = -ENOENT;
                        goto put_cpu_node;

the helper function
of_device_node_get_cpu()
comes from ML https://patchwork.kernel.org/patch/9830203/

What are your thoughts?

/Waldek

#6 lore raw
Quoted 15 lines · Waldemar Rymarkiewicz

Anyway, below a proposal how we can solve the problem of getting shared CPUs when hotplug is used.

(quoted diff) +1 -2
@@ -555,8 +576,7 @@ EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table);
 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
                                   struct cpumask *cpumask)
 {
-       struct device_node *np, *tmp_np;
-       struct device *tcpu_dev;
+       struct device_node *np, *tmp_np, *cpu_np;
        int cpu, ret = 0;
 
        /* Get OPP descriptor node */

Review int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, · line 596

Quoted 6 lines
(quoted diff) +2 -1
@@ -572,22 +592,16 @@ int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
        if (!of_property_read_bool(np, "opp-shared"))
                goto put_cpu_node;
 
-       for_each_possible_cpu(cpu) {
+       for_each_node_by_type(cpu_np, "cpu") {
+               cpu = of_device_node_get_cpu(cpu_np);

Actually you can keep the original loop as it is and use of_get_cpu_node() here. We better open code of_device_node_get_cpu() here.

Quoted 4 lines
(quoted diff) +1 -0
+
                if (cpu == cpu_dev->id)
                        continue;
 

Review int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, · line 588

Quoted 10 lines
(quoted diff) +0 -9
-               tcpu_dev = get_cpu_device(cpu);
-               if (!tcpu_dev) {
-                       dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
-                               __func__, cpu);
-                       ret = -ENODEV;
-                       goto put_cpu_node;
-               }
-
                /* Get OPP descriptor node */
-               tmp_np = _of_get_opp_desc_node(tcpu_dev);

I see dev_pm_opp_of_get_opp_desc_node() here, not sure how you got above change..

Review int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, · line 602

Quoted 1 line
(quoted diff) +1 -0
+               tmp_np = of_parse_phandle(cpu_np, "operating-points-v2", 0);

I would create _opp_of_get_opp_desc_node(np) and use it everywhere, i.e. from dev_pm_opp_of_get_opp_desc_node() as well.

Review int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, · line 607

Quoted 6 lines
(quoted diff) +1 -1
                if (!tmp_np) {
-                       dev_err(tcpu_dev, "%s: Couldn't find tcpu_dev node.\n",
+                       dev_err(cpu_dev, "%s: Couldn't find tcpu_dev node.\n",
                                __func__);
                        ret = -ENOENT;
                        goto put_cpu_node;

Thanks.

Signature · 1 line
viresh
#7 lore raw
Quoted 8 lines · Viresh Kumar
Quoted 7 lines · Waldemar Rymarkiewicz

Anyway, below a proposal how we can solve the problem of getting shared CPUs w

        /* Get OPP descriptor node */
(quoted diff) +0 -0
@@ -572,22 +592,16 @@ int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev,
        if (!of_property_read_bool(np, "opp-shared"))
                goto put_cpu_node;
 
Quoted 7 lines

Review int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, · line 596

Quoted 3 lines
(quoted diff) +2 -1
-       for_each_possible_cpu(cpu) {
+       for_each_node_by_type(cpu_np, "cpu") {
+               cpu = of_device_node_get_cpu(cpu_np);

Actually you can keep the original loop as it is and use of_get_cpu_node() here. We better open code of_device_node_get_cpu() here.

Indeed, I can do so.

Quoted 4 lines
Quoted 4 lines
(quoted diff) +1 -0
+
                if (cpu == cpu_dev->id)
                        continue;
 
Quoted 13 lines

Review int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, · line 588

Quoted 10 lines
(quoted diff) +0 -9
-               tcpu_dev = get_cpu_device(cpu);
-               if (!tcpu_dev) {
-                       dev_err(cpu_dev, "%s: failed to get cpu%d device\n",
-                               __func__, cpu);
-                       ret = -ENODEV;
-                       goto put_cpu_node;
-               }
-
                /* Get OPP descriptor node */
-               tmp_np = _of_get_opp_desc_node(tcpu_dev);

I see dev_pm_opp_of_get_opp_desc_node() here, not sure how you got above change..

Yeah, I test on 4.9 kernel. I will include your comments and build patch on top of linux-next/master anyway.

Quoted 4 lines

Review int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, · line 602

Quoted 1 line
(quoted diff) +1 -0
+               tmp_np = of_parse_phandle(cpu_np, "operating-points-v2", 0);

I would create _opp_of_get_opp_desc_node(np) and use it everywhere, i.e. from dev_pm_opp_of_get_opp_desc_node() as well.

Makes sense.

/Waldek