2015-08-20 16:14:10

by Vaishali Thakkar

[permalink] [raw]
Subject: [PATCH v2] thermal: cpu_cooling: Remove usage of devm functions

In the function cpufreq_get_requested_power, the memory allocated
for load_cpu is live within the function only. And after the
allocation it is immediately freed with devm_kfree. There is no
need to allocate memory for load_cpu with devm function so replace
devm_kcalloc with kcalloc and devm_kfree with kfree.

Signed-off-by: Vaishali Thakkar <[email protected]>
---
Changes since v1:
- Introduce new label based on Viresh Kumar's suggestion
---
drivers/thermal/cpu_cooling.c | 20 +++++++++-----------
1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c
index 620dcd4..7027923 100644
--- a/drivers/thermal/cpu_cooling.c
+++ b/drivers/thermal/cpu_cooling.c
@@ -584,8 +584,7 @@ static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
if (trace_thermal_power_cpu_get_power_enabled()) {
u32 ncpus = cpumask_weight(&cpufreq_device->allowed_cpus);

- load_cpu = devm_kcalloc(&cdev->device, ncpus, sizeof(*load_cpu),
- GFP_KERNEL);
+ load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
}

for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
@@ -607,22 +606,21 @@ static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,

dynamic_power = get_dynamic_power(cpufreq_device, freq);
ret = get_static_power(cpufreq_device, tz, freq, &static_power);
- if (ret) {
- if (load_cpu)
- devm_kfree(&cdev->device, load_cpu);
- return ret;
- }
+ if (ret)
+ goto free;

- if (load_cpu) {
+ if (load_cpu)
trace_thermal_power_cpu_get_power(
&cpufreq_device->allowed_cpus,
freq, load_cpu, i, dynamic_power, static_power);

- devm_kfree(&cdev->device, load_cpu);
- }
-
*power = static_power + dynamic_power;
return 0;
+
+free:
+ kfree(load_cpu);
+
+ return ret;
}

/**
--
1.9.1


2015-08-20 16:17:11

by Viresh Kumar

[permalink] [raw]
Subject: Re: [PATCH v2] thermal: cpu_cooling: Remove usage of devm functions

On 20-08-15, 21:44, Vaishali Thakkar wrote:
> In the function cpufreq_get_requested_power, the memory allocated
> for load_cpu is live within the function only. And after the
> allocation it is immediately freed with devm_kfree. There is no
> need to allocate memory for load_cpu with devm function so replace
> devm_kcalloc with kcalloc and devm_kfree with kfree.
>
> Signed-off-by: Vaishali Thakkar <[email protected]>
> ---
> Changes since v1:
> - Introduce new label based on Viresh Kumar's suggestion
> ---
> drivers/thermal/cpu_cooling.c | 20 +++++++++-----------
> 1 file changed, 9 insertions(+), 11 deletions(-)

Acked-by: Viresh Kumar <[email protected]>

--
viresh

2015-08-26 12:47:46

by Javi Merino

[permalink] [raw]
Subject: Re: [PATCH v2] thermal: cpu_cooling: Remove usage of devm functions

I missed this because I wasn't CCed :( Thankfully, I'll be in
MAINTAINERS for this soon.

On Thu, Aug 20, 2015 at 05:14:02PM +0100, Vaishali Thakkar wrote:
> In the function cpufreq_get_requested_power, the memory allocated
> for load_cpu is live within the function only. And after the
> allocation it is immediately freed with devm_kfree. There is no
> need to allocate memory for load_cpu with devm function so replace
> devm_kcalloc with kcalloc and devm_kfree with kfree.
>
> Signed-off-by: Vaishali Thakkar <[email protected]>
> ---
> Changes since v1:
> - Introduce new label based on Viresh Kumar's suggestion
> ---
> drivers/thermal/cpu_cooling.c | 20 +++++++++-----------
> 1 file changed, 9 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c
> index 620dcd4..7027923 100644
> --- a/drivers/thermal/cpu_cooling.c
> +++ b/drivers/thermal/cpu_cooling.c
> @@ -584,8 +584,7 @@ static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
> if (trace_thermal_power_cpu_get_power_enabled()) {
> u32 ncpus = cpumask_weight(&cpufreq_device->allowed_cpus);
>
> - load_cpu = devm_kcalloc(&cdev->device, ncpus, sizeof(*load_cpu),
> - GFP_KERNEL);
> + load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
> }
>
> for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
> @@ -607,22 +606,21 @@ static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
>
> dynamic_power = get_dynamic_power(cpufreq_device, freq);
> ret = get_static_power(cpufreq_device, tz, freq, &static_power);
> - if (ret) {
> - if (load_cpu)
> - devm_kfree(&cdev->device, load_cpu);
> - return ret;
> - }
> + if (ret)
> + goto free;
>
> - if (load_cpu) {
> + if (load_cpu)
> trace_thermal_power_cpu_get_power(
> &cpufreq_device->allowed_cpus,
> freq, load_cpu, i, dynamic_power, static_power);
>
> - devm_kfree(&cdev->device, load_cpu);

This introduces a memory leak. Keep the kfree() here, you can't drop
it. Cheers,
Javi

> - }
> -
> *power = static_power + dynamic_power;
> return 0;
> +
> +free:
> + kfree(load_cpu);
> +
> + return ret;
> }
>
> /**

2015-08-26 12:52:07

by Viresh Kumar

[permalink] [raw]
Subject: Re: [PATCH v2] thermal: cpu_cooling: Remove usage of devm functions

On 26-08-15, 13:47, Javi Merino wrote:
> I missed this because I wasn't CCed :( Thankfully, I'll be in
> MAINTAINERS for this soon.

Yeah, I need to resend that patch soon :)

> > - devm_kfree(&cdev->device, load_cpu);
>
> This introduces a memory leak. Keep the kfree() here, you can't drop
> it. Cheers,
> Javi
>
> > - }
> > -
> > *power = static_power + dynamic_power;
> > return 0;
> > +
> > +free:
> > + kfree(load_cpu);

Wouldn't this make that work ?

--
viresh

2015-08-26 13:09:32

by Javi Merino

[permalink] [raw]
Subject: Re: [PATCH v2] thermal: cpu_cooling: Remove usage of devm functions

On Wed, Aug 26, 2015 at 01:51:58PM +0100, Viresh Kumar wrote:
> On 26-08-15, 13:47, Javi Merino wrote:
> > I missed this because I wasn't CCed :( Thankfully, I'll be in
> > MAINTAINERS for this soon.
>
> Yeah, I need to resend that patch soon :)
>
> > > - devm_kfree(&cdev->device, load_cpu);
> >
> > This introduces a memory leak. Keep the kfree() here, you can't drop
> > it. Cheers,
> > Javi
> >
> > > - }
> > > -
> > > *power = static_power + dynamic_power;
> > > return 0;
> > > +
> > > +free:
> > > + kfree(load_cpu);
>
> Wouldn't this make that work ?

Nope, you're not reaching that code path from there. Removing the
"return 0" would work, but I don't like it, since we would be calling
kfree() all the time, even when the trace is not enabled. I'd rather
leave the kfree() where it is.

Cheers,
Javi

2015-08-26 13:14:09

by Viresh Kumar

[permalink] [raw]
Subject: Re: [PATCH v2] thermal: cpu_cooling: Remove usage of devm functions

On 26-08-15, 14:09, Javi Merino wrote:
> On Wed, Aug 26, 2015 at 01:51:58PM +0100, Viresh Kumar wrote:
> > On 26-08-15, 13:47, Javi Merino wrote:
> > > I missed this because I wasn't CCed :( Thankfully, I'll be in
> > > MAINTAINERS for this soon.
> >
> > Yeah, I need to resend that patch soon :)
> >
> > > > - devm_kfree(&cdev->device, load_cpu);
> > >
> > > This introduces a memory leak. Keep the kfree() here, you can't drop
> > > it. Cheers,
> > > Javi
> > >
> > > > - }
> > > > -
> > > > *power = static_power + dynamic_power;
> > > > return 0;

So, the change I suggested on V1 removed this as well :) and Vaishali
missed it completely.

> > > > +
> > > > +free:
> > > > + kfree(load_cpu);
> >
> > Wouldn't this make that work ?
>
> Nope, you're not reaching that code path from there. Removing the
> "return 0" would work, but I don't like it, since we would be calling
> kfree() all the time, even when the trace is not enabled. I'd rather
> leave the kfree() where it is.

Hmm..

--
viresh

2015-08-27 01:31:28

by Vaishali Thakkar

[permalink] [raw]
Subject: Re: [PATCH v2] thermal: cpu_cooling: Remove usage of devm functions

On Wed, Aug 26, 2015 at 6:44 PM, Viresh Kumar <[email protected]> wrote:
> On 26-08-15, 14:09, Javi Merino wrote:
>> On Wed, Aug 26, 2015 at 01:51:58PM +0100, Viresh Kumar wrote:
>> > On 26-08-15, 13:47, Javi Merino wrote:
>> > > I missed this because I wasn't CCed :( Thankfully, I'll be in
>> > > MAINTAINERS for this soon.
>> >
>> > Yeah, I need to resend that patch soon :)
>> >
>> > > > - devm_kfree(&cdev->device, load_cpu);
>> > >
>> > > This introduces a memory leak. Keep the kfree() here, you can't drop
>> > > it. Cheers,
>> > > Javi
>> > >
>> > > > - }
>> > > > -
>> > > > *power = static_power + dynamic_power;
>> > > > return 0;
>
> So, the change I suggested on V1 removed this as well :) and Vaishali
> missed it completely.

Yes. I missed the point that kfree was called at 2 places previously.
Would you like me to send v3 with changes having just new label with
'goto' at both of these places or you would like to apply v1 of the patch?

>> > > > +
>> > > > +free:
>> > > > + kfree(load_cpu);
>> >
>> > Wouldn't this make that work ?
>>
>> Nope, you're not reaching that code path from there. Removing the
>> "return 0" would work, but I don't like it, since we would be calling
>> kfree() all the time, even when the trace is not enabled. I'd rather
>> leave the kfree() where it is.
>
> Hmm..
>
> --
> viresh



--
Vaishali

2015-08-27 09:00:26

by Javi Merino

[permalink] [raw]
Subject: Re: [PATCH v2] thermal: cpu_cooling: Remove usage of devm functions

On Thu, Aug 27, 2015 at 02:31:26AM +0100, Vaishali Thakkar wrote:
> On Wed, Aug 26, 2015 at 6:44 PM, Viresh Kumar <[email protected]> wrote:
> > On 26-08-15, 14:09, Javi Merino wrote:
> >> On Wed, Aug 26, 2015 at 01:51:58PM +0100, Viresh Kumar wrote:
> >> > On 26-08-15, 13:47, Javi Merino wrote:
> >> > > I missed this because I wasn't CCed :( Thankfully, I'll be in
> >> > > MAINTAINERS for this soon.
> >> >
> >> > Yeah, I need to resend that patch soon :)
> >> >
> >> > > > - devm_kfree(&cdev->device, load_cpu);
> >> > >
> >> > > This introduces a memory leak. Keep the kfree() here, you can't drop
> >> > > it. Cheers,
> >> > > Javi
> >> > >
> >> > > > - }
> >> > > > -
> >> > > > *power = static_power + dynamic_power;
> >> > > > return 0;
> >
> > So, the change I suggested on V1 removed this as well :) and Vaishali
> > missed it completely.
>
> Yes. I missed the point that kfree was called at 2 places previously.
> Would you like me to send v3 with changes having just new label with
> 'goto' at both of these places or you would like to apply v1 of the patch?

I vote for v1. I've acked it.

Cheers,
Javi