2017-06-21 04:46:54

by Viresh Kumar

[permalink] [raw]
Subject: [PATCH V2 0/5] arch_topology: Minor cleanups

Hi Greg,

You weren't included in the first [1] version of this series, as it was
targeting arch/arm*/ directories then.

Here are some cleanups for the arch_topology core.

Tested on ARM64 Hikey board by setting following in cpu nodes in DT:
capacity-dmips-mhz = <1000>;

V1->V2:
- based over linux-next/master (to get Juri's recent changes)
- More cleanups included. V1 only had the first patch.
- Rename of cap_parsing_failed isn't required anymore (as it is
localized to a single routine now).

--
viresh

[1] https://marc.info/?l=linux-arm-kernel&m=149795553024005

Viresh Kumar (5):
arch_topology: Get rid of "cap_parsing_done"
arch_topology: Don't break lines unnecessarily
arch_topology: Covert switch block to if block
arch_topology: Return 0 or -ve errors from
topology_parse_cpu_capacity()
arch_topology: Localize cap_parsing_failed to
topology_parse_cpu_capacity()

arch/arm/kernel/topology.c | 2 +-
drivers/base/arch_topology.c | 78 ++++++++++++++++++++++----------------------
2 files changed, 40 insertions(+), 40 deletions(-)

--
2.13.0.71.gd7076ec9c9cb


2017-06-21 04:47:01

by Viresh Kumar

[permalink] [raw]
Subject: [PATCH V2 2/5] arch_topology: Don't break lines unnecessarily

There is no need of line break at few places, avoid them.

Signed-off-by: Viresh Kumar <[email protected]>
---
drivers/base/arch_topology.c | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/drivers/base/arch_topology.c b/drivers/base/arch_topology.c
index 8239a6232808..2f1d9921ee54 100644
--- a/drivers/base/arch_topology.c
+++ b/drivers/base/arch_topology.c
@@ -41,8 +41,7 @@ static ssize_t cpu_capacity_show(struct device *dev,
{
struct cpu *cpu = container_of(dev, struct cpu, dev);

- return sprintf(buf, "%lu\n",
- topology_get_cpu_scale(NULL, cpu->dev.id));
+ return sprintf(buf, "%lu\n", topology_get_cpu_scale(NULL, cpu->dev.id));
}

static ssize_t cpu_capacity_store(struct device *dev,
@@ -128,8 +127,7 @@ int __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
if (cap_parsing_failed)
return !ret;

- ret = of_property_read_u32(cpu_node,
- "capacity-dmips-mhz",
+ ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz",
&cpu_capacity);
if (!ret) {
if (!raw_capacity) {
@@ -180,8 +178,7 @@ init_cpu_capacity_callback(struct notifier_block *nb,
pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
cpumask_pr_args(policy->related_cpus),
cpumask_pr_args(cpus_to_visit));
- cpumask_andnot(cpus_to_visit,
- cpus_to_visit,
+ cpumask_andnot(cpus_to_visit, cpus_to_visit,
policy->related_cpus);
for_each_cpu(cpu, policy->related_cpus) {
raw_capacity[cpu] = topology_get_cpu_scale(NULL, cpu) *
--
2.13.0.71.gd7076ec9c9cb

2017-06-21 04:47:11

by Viresh Kumar

[permalink] [raw]
Subject: [PATCH V2 5/5] arch_topology: Localize cap_parsing_failed to topology_parse_cpu_capacity()

cap_parsing_failed is only required in topology_parse_cpu_capacity() to
know if we have already tried to allocate raw_capacity and failed, or if
at least one of the cpu_node didn't had the required
"capacity-dmips-mhz" property.

All other users can use raw_capacity instead of cap_parsing_failed.

Make sure we set raw_capacity to NULL after we free it.

Signed-off-by: Viresh Kumar <[email protected]>
---
drivers/base/arch_topology.c | 25 +++++++++++++------------
1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/drivers/base/arch_topology.c b/drivers/base/arch_topology.c
index ff8713b83090..d8923f89724f 100644
--- a/drivers/base/arch_topology.c
+++ b/drivers/base/arch_topology.c
@@ -95,14 +95,21 @@ subsys_initcall(register_cpu_capacity_sysctl);

static u32 capacity_scale;
static u32 *raw_capacity;
-static bool cap_parsing_failed;
+
+static int __init free_raw_capacity(void)
+{
+ kfree(raw_capacity);
+ raw_capacity = NULL;
+
+ return 0;
+}

void topology_normalize_cpu_scale(void)
{
u64 capacity;
int cpu;

- if (!raw_capacity || cap_parsing_failed)
+ if (!raw_capacity)
return;

pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale);
@@ -121,6 +128,7 @@ void topology_normalize_cpu_scale(void)

int __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
{
+ static bool cap_parsing_failed;
int ret;
u32 cpu_capacity;

@@ -151,7 +159,7 @@ int __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
}
cap_parsing_failed = true;
- kfree(raw_capacity);
+ free_raw_capacity();
}

return ret;
@@ -170,7 +178,7 @@ init_cpu_capacity_callback(struct notifier_block *nb,
struct cpufreq_policy *policy = data;
int cpu;

- if (cap_parsing_failed)
+ if (!raw_capacity)
return 0;

if (val != CPUFREQ_NOTIFY)
@@ -190,9 +198,8 @@ init_cpu_capacity_callback(struct notifier_block *nb,

if (cpumask_empty(cpus_to_visit)) {
topology_normalize_cpu_scale();
- kfree(raw_capacity);
+ free_raw_capacity();
pr_debug("cpu_capacity: parsing done\n");
- cap_parsing_failed = true;
schedule_work(&parsing_done_work);
}

@@ -232,11 +239,5 @@ static void parsing_done_workfn(struct work_struct *work)
}

#else
-static int __init free_raw_capacity(void)
-{
- kfree(raw_capacity);
-
- return 0;
-}
core_initcall(free_raw_capacity);
#endif
--
2.13.0.71.gd7076ec9c9cb

2017-06-21 04:46:59

by Viresh Kumar

[permalink] [raw]
Subject: [PATCH V2 1/5] arch_topology: Get rid of "cap_parsing_done"

We can reuse "cap_parsing_failed" instead of keeping an additional
variable here.

Signed-off-by: Viresh Kumar <[email protected]>
---
drivers/base/arch_topology.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/base/arch_topology.c b/drivers/base/arch_topology.c
index d1c33a85059e..8239a6232808 100644
--- a/drivers/base/arch_topology.c
+++ b/drivers/base/arch_topology.c
@@ -161,7 +161,6 @@ int __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)

#ifdef CONFIG_CPU_FREQ
static cpumask_var_t cpus_to_visit;
-static bool cap_parsing_done;
static void parsing_done_workfn(struct work_struct *work);
static DECLARE_WORK(parsing_done_work, parsing_done_workfn);

@@ -173,7 +172,7 @@ init_cpu_capacity_callback(struct notifier_block *nb,
struct cpufreq_policy *policy = data;
int cpu;

- if (cap_parsing_failed || cap_parsing_done)
+ if (cap_parsing_failed)
return 0;

switch (val) {
@@ -193,7 +192,7 @@ init_cpu_capacity_callback(struct notifier_block *nb,
topology_normalize_cpu_scale();
kfree(raw_capacity);
pr_debug("cpu_capacity: parsing done\n");
- cap_parsing_done = true;
+ cap_parsing_failed = true;
schedule_work(&parsing_done_work);
}
}
--
2.13.0.71.gd7076ec9c9cb

2017-06-21 04:47:18

by Viresh Kumar

[permalink] [raw]
Subject: [PATCH V2 4/5] arch_topology: Return 0 or -ve errors from topology_parse_cpu_capacity()

Use the standard way of returning errors instead of returning 0(failure)
OR 1(success) and making it hard to read.

Signed-off-by: Viresh Kumar <[email protected]>
---
arch/arm/kernel/topology.c | 2 +-
drivers/base/arch_topology.c | 8 ++++----
2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/arch/arm/kernel/topology.c b/arch/arm/kernel/topology.c
index bf949a763dbe..a7ef4c35855e 100644
--- a/arch/arm/kernel/topology.c
+++ b/arch/arm/kernel/topology.c
@@ -111,7 +111,7 @@ static void __init parse_dt_topology(void)
continue;
}

- if (topology_parse_cpu_capacity(cn, cpu)) {
+ if (!topology_parse_cpu_capacity(cn, cpu)) {
of_node_put(cn);
continue;
}
diff --git a/drivers/base/arch_topology.c b/drivers/base/arch_topology.c
index 07784ba666a7..ff8713b83090 100644
--- a/drivers/base/arch_topology.c
+++ b/drivers/base/arch_topology.c
@@ -121,11 +121,11 @@ void topology_normalize_cpu_scale(void)

int __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
{
- int ret = 1;
+ int ret;
u32 cpu_capacity;

if (cap_parsing_failed)
- return !ret;
+ return -EINVAL;

ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz",
&cpu_capacity);
@@ -137,7 +137,7 @@ int __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
if (!raw_capacity) {
pr_err("cpu_capacity: failed to allocate memory for raw capacities\n");
cap_parsing_failed = true;
- return 0;
+ return -ENOMEM;
}
}
capacity_scale = max(cpu_capacity, capacity_scale);
@@ -154,7 +154,7 @@ int __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
kfree(raw_capacity);
}

- return !ret;
+ return ret;
}

#ifdef CONFIG_CPU_FREQ
--
2.13.0.71.gd7076ec9c9cb

2017-06-21 04:47:05

by Viresh Kumar

[permalink] [raw]
Subject: [PATCH V2 3/5] arch_topology: Covert switch block to if block

We only need to take care of one case here (CPUFREQ_NOTIFY) and there is
no need to add an extra level of indentation to the case specific code
by using a switch block. Use an if block instead.

Also add some blank lines to make the code look better.

Signed-off-by: Viresh Kumar <[email protected]>
---
drivers/base/arch_topology.c | 41 ++++++++++++++++++++++-------------------
1 file changed, 22 insertions(+), 19 deletions(-)

diff --git a/drivers/base/arch_topology.c b/drivers/base/arch_topology.c
index 2f1d9921ee54..07784ba666a7 100644
--- a/drivers/base/arch_topology.c
+++ b/drivers/base/arch_topology.c
@@ -173,26 +173,29 @@ init_cpu_capacity_callback(struct notifier_block *nb,
if (cap_parsing_failed)
return 0;

- switch (val) {
- case CPUFREQ_NOTIFY:
- pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
- cpumask_pr_args(policy->related_cpus),
- cpumask_pr_args(cpus_to_visit));
- cpumask_andnot(cpus_to_visit, cpus_to_visit,
- policy->related_cpus);
- for_each_cpu(cpu, policy->related_cpus) {
- raw_capacity[cpu] = topology_get_cpu_scale(NULL, cpu) *
- policy->cpuinfo.max_freq / 1000UL;
- capacity_scale = max(raw_capacity[cpu], capacity_scale);
- }
- if (cpumask_empty(cpus_to_visit)) {
- topology_normalize_cpu_scale();
- kfree(raw_capacity);
- pr_debug("cpu_capacity: parsing done\n");
- cap_parsing_failed = true;
- schedule_work(&parsing_done_work);
- }
+ if (val != CPUFREQ_NOTIFY)
+ return 0;
+
+ pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
+ cpumask_pr_args(policy->related_cpus),
+ cpumask_pr_args(cpus_to_visit));
+
+ cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus);
+
+ for_each_cpu(cpu, policy->related_cpus) {
+ raw_capacity[cpu] = topology_get_cpu_scale(NULL, cpu) *
+ policy->cpuinfo.max_freq / 1000UL;
+ capacity_scale = max(raw_capacity[cpu], capacity_scale);
}
+
+ if (cpumask_empty(cpus_to_visit)) {
+ topology_normalize_cpu_scale();
+ kfree(raw_capacity);
+ pr_debug("cpu_capacity: parsing done\n");
+ cap_parsing_failed = true;
+ schedule_work(&parsing_done_work);
+ }
+
return 0;
}

--
2.13.0.71.gd7076ec9c9cb

2017-06-22 09:40:03

by Juri Lelli

[permalink] [raw]
Subject: Re: [PATCH V2 4/5] arch_topology: Return 0 or -ve errors from topology_parse_cpu_capacity()

Hi,

On 21/06/17 10:16, Viresh Kumar wrote:
> Use the standard way of returning errors instead of returning 0(failure)
> OR 1(success) and making it hard to read.
>
> Signed-off-by: Viresh Kumar <[email protected]>
> ---
> arch/arm/kernel/topology.c | 2 +-
> drivers/base/arch_topology.c | 8 ++++----
> 2 files changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/arch/arm/kernel/topology.c b/arch/arm/kernel/topology.c
> index bf949a763dbe..a7ef4c35855e 100644
> --- a/arch/arm/kernel/topology.c
> +++ b/arch/arm/kernel/topology.c
> @@ -111,7 +111,7 @@ static void __init parse_dt_topology(void)
> continue;
> }
>
> - if (topology_parse_cpu_capacity(cn, cpu)) {
> + if (!topology_parse_cpu_capacity(cn, cpu)) {

Not sure why you want to change this.

I currently read it as "if cpu_capacity parsing succedeed" continue with
next CPU, otherwise we set cap_from_dt to false and fall back to using
efficiencies.

Thanks,

- Juri

2017-06-22 09:44:14

by Juri Lelli

[permalink] [raw]
Subject: Re: [PATCH V2 1/5] arch_topology: Get rid of "cap_parsing_done"

Hi,

On 21/06/17 10:16, Viresh Kumar wrote:
> We can reuse "cap_parsing_failed" instead of keeping an additional
> variable here.
>
> Signed-off-by: Viresh Kumar <[email protected]>
> ---
> drivers/base/arch_topology.c | 5 ++---
> 1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/base/arch_topology.c b/drivers/base/arch_topology.c
> index d1c33a85059e..8239a6232808 100644
> --- a/drivers/base/arch_topology.c
> +++ b/drivers/base/arch_topology.c
> @@ -161,7 +161,6 @@ int __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
>
> #ifdef CONFIG_CPU_FREQ
> static cpumask_var_t cpus_to_visit;
> -static bool cap_parsing_done;
> static void parsing_done_workfn(struct work_struct *work);
> static DECLARE_WORK(parsing_done_work, parsing_done_workfn);
>
> @@ -173,7 +172,7 @@ init_cpu_capacity_callback(struct notifier_block *nb,
> struct cpufreq_policy *policy = data;
> int cpu;
>
> - if (cap_parsing_failed || cap_parsing_done)
> + if (cap_parsing_failed)
> return 0;
>
> switch (val) {
> @@ -193,7 +192,7 @@ init_cpu_capacity_callback(struct notifier_block *nb,
> topology_normalize_cpu_scale();
> kfree(raw_capacity);
> pr_debug("cpu_capacity: parsing done\n");
> - cap_parsing_done = true;
> + cap_parsing_failed = true;

But we didn't actually failed here, right?

Thanks,

- Juri

2017-06-22 14:28:32

by Viresh Kumar

[permalink] [raw]
Subject: Re: [PATCH V2 4/5] arch_topology: Return 0 or -ve errors from topology_parse_cpu_capacity()

On 22-06-17, 10:39, Juri Lelli wrote:
> Hi,
>
> On 21/06/17 10:16, Viresh Kumar wrote:
> > Use the standard way of returning errors instead of returning 0(failure)
> > OR 1(success) and making it hard to read.
> >
> > Signed-off-by: Viresh Kumar <[email protected]>
> > ---
> > arch/arm/kernel/topology.c | 2 +-
> > drivers/base/arch_topology.c | 8 ++++----
> > 2 files changed, 5 insertions(+), 5 deletions(-)
> >
> > diff --git a/arch/arm/kernel/topology.c b/arch/arm/kernel/topology.c
> > index bf949a763dbe..a7ef4c35855e 100644
> > --- a/arch/arm/kernel/topology.c
> > +++ b/arch/arm/kernel/topology.c
> > @@ -111,7 +111,7 @@ static void __init parse_dt_topology(void)
> > continue;
> > }
> >
> > - if (topology_parse_cpu_capacity(cn, cpu)) {
> > + if (!topology_parse_cpu_capacity(cn, cpu)) {
>
> Not sure why you want to change this.

I just didn't find it straight forward to read.

> I currently read it as "if cpu_capacity parsing succedeed" continue with
> next CPU, otherwise we set cap_from_dt to false and fall back to using
> efficiencies.

Actually, I can just make the return type bool and that should solve
the issues I was seeing and keep the code as it is.

Will that be fine ?

--
viresh

2017-06-22 14:29:31

by Viresh Kumar

[permalink] [raw]
Subject: Re: [PATCH V2 1/5] arch_topology: Get rid of "cap_parsing_done"

On 22-06-17, 10:44, Juri Lelli wrote:
> Hi,
>
> On 21/06/17 10:16, Viresh Kumar wrote:
> > We can reuse "cap_parsing_failed" instead of keeping an additional
> > variable here.
> >
> > Signed-off-by: Viresh Kumar <[email protected]>
> > ---
> > drivers/base/arch_topology.c | 5 ++---
> > 1 file changed, 2 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/base/arch_topology.c b/drivers/base/arch_topology.c
> > index d1c33a85059e..8239a6232808 100644
> > --- a/drivers/base/arch_topology.c
> > +++ b/drivers/base/arch_topology.c
> > @@ -161,7 +161,6 @@ int __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
> >
> > #ifdef CONFIG_CPU_FREQ
> > static cpumask_var_t cpus_to_visit;
> > -static bool cap_parsing_done;
> > static void parsing_done_workfn(struct work_struct *work);
> > static DECLARE_WORK(parsing_done_work, parsing_done_workfn);
> >
> > @@ -173,7 +172,7 @@ init_cpu_capacity_callback(struct notifier_block *nb,
> > struct cpufreq_policy *policy = data;
> > int cpu;
> >
> > - if (cap_parsing_failed || cap_parsing_done)
> > + if (cap_parsing_failed)
> > return 0;
> >
> > switch (val) {
> > @@ -193,7 +192,7 @@ init_cpu_capacity_callback(struct notifier_block *nb,
> > topology_normalize_cpu_scale();
> > kfree(raw_capacity);
> > pr_debug("cpu_capacity: parsing done\n");
> > - cap_parsing_done = true;
> > + cap_parsing_failed = true;
>
> But we didn't actually failed here, right?

Yeah. So I can actually move this patch to the end of the series and
cap_parsing_done can be dropped then as we will end up using
!raw_capacity instead. And the end result will stay the same.

--
viresh

2017-06-22 16:43:50

by Juri Lelli

[permalink] [raw]
Subject: Re: [PATCH V2 4/5] arch_topology: Return 0 or -ve errors from topology_parse_cpu_capacity()

On 22/06/17 19:58, Viresh Kumar wrote:
> On 22-06-17, 10:39, Juri Lelli wrote:
> > Hi,
> >
> > On 21/06/17 10:16, Viresh Kumar wrote:
> > > Use the standard way of returning errors instead of returning 0(failure)
> > > OR 1(success) and making it hard to read.
> > >
> > > Signed-off-by: Viresh Kumar <[email protected]>
> > > ---
> > > arch/arm/kernel/topology.c | 2 +-
> > > drivers/base/arch_topology.c | 8 ++++----
> > > 2 files changed, 5 insertions(+), 5 deletions(-)
> > >
> > > diff --git a/arch/arm/kernel/topology.c b/arch/arm/kernel/topology.c
> > > index bf949a763dbe..a7ef4c35855e 100644
> > > --- a/arch/arm/kernel/topology.c
> > > +++ b/arch/arm/kernel/topology.c
> > > @@ -111,7 +111,7 @@ static void __init parse_dt_topology(void)
> > > continue;
> > > }
> > >
> > > - if (topology_parse_cpu_capacity(cn, cpu)) {
> > > + if (!topology_parse_cpu_capacity(cn, cpu)) {
> >
> > Not sure why you want to change this.
>
> I just didn't find it straight forward to read.
>
> > I currently read it as "if cpu_capacity parsing succedeed" continue with
> > next CPU, otherwise we set cap_from_dt to false and fall back to using
> > efficiencies.
>
> Actually, I can just make the return type bool and that should solve
> the issues I was seeing and keep the code as it is.
>
> Will that be fine ?
>

Think so.

2017-06-22 16:44:16

by Juri Lelli

[permalink] [raw]
Subject: Re: [PATCH V2 1/5] arch_topology: Get rid of "cap_parsing_done"

On 22/06/17 19:59, Viresh Kumar wrote:
> On 22-06-17, 10:44, Juri Lelli wrote:
> > Hi,
> >
> > On 21/06/17 10:16, Viresh Kumar wrote:
> > > We can reuse "cap_parsing_failed" instead of keeping an additional
> > > variable here.
> > >
> > > Signed-off-by: Viresh Kumar <[email protected]>
> > > ---
> > > drivers/base/arch_topology.c | 5 ++---
> > > 1 file changed, 2 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/drivers/base/arch_topology.c b/drivers/base/arch_topology.c
> > > index d1c33a85059e..8239a6232808 100644
> > > --- a/drivers/base/arch_topology.c
> > > +++ b/drivers/base/arch_topology.c
> > > @@ -161,7 +161,6 @@ int __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
> > >
> > > #ifdef CONFIG_CPU_FREQ
> > > static cpumask_var_t cpus_to_visit;
> > > -static bool cap_parsing_done;
> > > static void parsing_done_workfn(struct work_struct *work);
> > > static DECLARE_WORK(parsing_done_work, parsing_done_workfn);
> > >
> > > @@ -173,7 +172,7 @@ init_cpu_capacity_callback(struct notifier_block *nb,
> > > struct cpufreq_policy *policy = data;
> > > int cpu;
> > >
> > > - if (cap_parsing_failed || cap_parsing_done)
> > > + if (cap_parsing_failed)
> > > return 0;
> > >
> > > switch (val) {
> > > @@ -193,7 +192,7 @@ init_cpu_capacity_callback(struct notifier_block *nb,
> > > topology_normalize_cpu_scale();
> > > kfree(raw_capacity);
> > > pr_debug("cpu_capacity: parsing done\n");
> > > - cap_parsing_done = true;
> > > + cap_parsing_failed = true;
> >
> > But we didn't actually failed here, right?
>
> Yeah. So I can actually move this patch to the end of the series and
> cap_parsing_done can be dropped then as we will end up using
> !raw_capacity instead. And the end result will stay the same.
>

OK.