diff mbox series

drivers/firmware/psci: Fix memory leak in alloc_init_cpu_groups()

Message ID 20200130034938.158504-1-gshan@redhat.com (mailing list archive)
State New, archived
Headers show
Series drivers/firmware/psci: Fix memory leak in alloc_init_cpu_groups() | expand

Commit Message

Gavin Shan Jan. 30, 2020, 3:49 a.m. UTC
The CPU mask (@tmp) should be free'd on failing to allocating the element
of @cpu_groups[]. Otherwise, it leads to memory leakage because the CPU
mask variable is allocated with CONFIG_CPUMASK_OFFSTACK.

Signed-off-by: Gavin Shan <gshan@redhat.com>
---
 drivers/firmware/psci/psci_checker.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Gavin Shan Jan. 30, 2020, 4 a.m. UTC | #1
On 1/30/20 2:49 PM, Gavin Shan wrote:
> The CPU mask (@tmp) should be free'd on failing to allocating the element
> of @cpu_groups[]. Otherwise, it leads to memory leakage because the CPU
> mask variable is allocated with CONFIG_CPUMASK_OFFSTACK.
> 
> Signed-off-by: Gavin Shan <gshan@redhat.com>
> ---
>   drivers/firmware/psci/psci_checker.c | 1 +
>   1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/firmware/psci/psci_checker.c b/drivers/firmware/psci/psci_checker.c
> index 6a445397771c..d1d6d1135fed 100644
> --- a/drivers/firmware/psci/psci_checker.c
> +++ b/drivers/firmware/psci/psci_checker.c
> @@ -167,6 +167,7 @@ static int alloc_init_cpu_groups(cpumask_var_t **pcpu_groups)
>   			topology_core_cpumask(cpumask_any(tmp));
>   
>   		if (!alloc_cpumask_var(&cpu_groups[num_groups], GFP_KERNEL)) {
> +			free_cpumask_var(tmp);
>   			free_cpu_groups(num_groups, &cpu_groups);
>   			return -ENOMEM;
>   		}
> 

I think @tmp has to be free'd either when failing to allocate @cpu_groups.
However, I'm holding to post v2 until feedback is received on v1.

diff --git a/drivers/firmware/psci/psci_checker.c b/drivers/firmware/psci/psci_checker.c
index d1d6d1135fed..03eb798ad3ed 100644
--- a/drivers/firmware/psci/psci_checker.c
+++ b/drivers/firmware/psci/psci_checker.c
@@ -157,8 +157,10 @@ static int alloc_init_cpu_groups(cpumask_var_t **pcpu_groups)
  
         cpu_groups = kcalloc(nb_available_cpus, sizeof(cpu_groups),
                              GFP_KERNEL);
-       if (!cpu_groups)
+       if (!cpu_groups) {
+               free_cpumask_var(tmp);
                 return -ENOMEM;
+       }
  
         cpumask_copy(tmp, cpu_online_mask);

Thanks,
Gavin
Sudeep Holla Jan. 30, 2020, 11:02 a.m. UTC | #2
On Thu, Jan 30, 2020 at 02:49:38PM +1100, Gavin Shan wrote:
> The CPU mask (@tmp) should be free'd on failing to allocating the element
> of @cpu_groups[]. Otherwise, it leads to memory leakage because the CPU
> mask variable is allocated with CONFIG_CPUMASK_OFFSTACK.
> 
> Signed-off-by: Gavin Shan <gshan@redhat.com>
> ---
>  drivers/firmware/psci/psci_checker.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/firmware/psci/psci_checker.c b/drivers/firmware/psci/psci_checker.c
> index 6a445397771c..d1d6d1135fed 100644
> --- a/drivers/firmware/psci/psci_checker.c
> +++ b/drivers/firmware/psci/psci_checker.c
> @@ -167,6 +167,7 @@ static int alloc_init_cpu_groups(cpumask_var_t **pcpu_groups)
>  			topology_core_cpumask(cpumask_any(tmp));
>  
>  		if (!alloc_cpumask_var(&cpu_groups[num_groups], GFP_KERNEL)) {
> +			free_cpumask_var(tmp);

You may need also below diff, right ?

-->8

diff --git i/drivers/firmware/psci/psci_checker.c w/drivers/firmware/psci/psci_checker.c
index 6a445397771c..c5a5c76a9fda 100644
--- i/drivers/firmware/psci/psci_checker.c
+++ w/drivers/firmware/psci/psci_checker.c
@@ -157,8 +157,10 @@ static int alloc_init_cpu_groups(cpumask_var_t **pcpu_groups)

        cpu_groups = kcalloc(nb_available_cpus, sizeof(cpu_groups),
                             GFP_KERNEL);
-       if (!cpu_groups)
+       if (!cpu_groups) {
+               free_cpumask_var(tmp);
                return -ENOMEM;
+       }

        cpumask_copy(tmp, cpu_online_mask);
Sudeep Holla Jan. 30, 2020, 11:05 a.m. UTC | #3
On Thu, Jan 30, 2020 at 03:00:13PM +1100, Gavin Shan wrote:
> On 1/30/20 2:49 PM, Gavin Shan wrote:
> > The CPU mask (@tmp) should be free'd on failing to allocating the element
> > of @cpu_groups[]. Otherwise, it leads to memory leakage because the CPU
> > mask variable is allocated with CONFIG_CPUMASK_OFFSTACK.
> > 
> > Signed-off-by: Gavin Shan <gshan@redhat.com>
> > ---
> >   drivers/firmware/psci/psci_checker.c | 1 +
> >   1 file changed, 1 insertion(+)
> > 
> > diff --git a/drivers/firmware/psci/psci_checker.c b/drivers/firmware/psci/psci_checker.c
> > index 6a445397771c..d1d6d1135fed 100644
> > --- a/drivers/firmware/psci/psci_checker.c
> > +++ b/drivers/firmware/psci/psci_checker.c
> > @@ -167,6 +167,7 @@ static int alloc_init_cpu_groups(cpumask_var_t **pcpu_groups)
> >   			topology_core_cpumask(cpumask_any(tmp));
> >   		if (!alloc_cpumask_var(&cpu_groups[num_groups], GFP_KERNEL)) {
> > +			free_cpumask_var(tmp);
> >   			free_cpu_groups(num_groups, &cpu_groups);
> >   			return -ENOMEM;
> >   		}
> > 
> 
> I think @tmp has to be free'd either when failing to allocate @cpu_groups.
> However, I'm holding to post v2 until feedback is received on v1.
> 

Ah sorry, problem with sequential mail reading, ignore my reply as you have
already figured this out.

--
Regards,
Sudeep
Gavin Shan Jan. 30, 2020, 9:41 p.m. UTC | #4
On 1/30/20 10:05 PM, Sudeep Holla wrote:
> On Thu, Jan 30, 2020 at 03:00:13PM +1100, Gavin Shan wrote:
>> On 1/30/20 2:49 PM, Gavin Shan wrote:
>>> The CPU mask (@tmp) should be free'd on failing to allocating the element
>>> of @cpu_groups[]. Otherwise, it leads to memory leakage because the CPU
>>> mask variable is allocated with CONFIG_CPUMASK_OFFSTACK.
>>>
>>> Signed-off-by: Gavin Shan <gshan@redhat.com>
>>> ---
>>>    drivers/firmware/psci/psci_checker.c | 1 +
>>>    1 file changed, 1 insertion(+)
>>>
>>> diff --git a/drivers/firmware/psci/psci_checker.c b/drivers/firmware/psci/psci_checker.c
>>> index 6a445397771c..d1d6d1135fed 100644
>>> --- a/drivers/firmware/psci/psci_checker.c
>>> +++ b/drivers/firmware/psci/psci_checker.c
>>> @@ -167,6 +167,7 @@ static int alloc_init_cpu_groups(cpumask_var_t **pcpu_groups)
>>>    			topology_core_cpumask(cpumask_any(tmp));
>>>    		if (!alloc_cpumask_var(&cpu_groups[num_groups], GFP_KERNEL)) {
>>> +			free_cpumask_var(tmp);
>>>    			free_cpu_groups(num_groups, &cpu_groups);
>>>    			return -ENOMEM;
>>>    		}
>>>
>>
>> I think @tmp has to be free'd either when failing to allocate @cpu_groups.
>> However, I'm holding to post v2 until feedback is received on v1.
>>
> 
> Ah sorry, problem with sequential mail reading, ignore my reply as you have
> already figured this out.
> 

Sudeep, no problem. I'll fold the additional changes into v2. Thanks for your
time on this.

Thanks,
Gavin
diff mbox series

Patch

diff --git a/drivers/firmware/psci/psci_checker.c b/drivers/firmware/psci/psci_checker.c
index 6a445397771c..d1d6d1135fed 100644
--- a/drivers/firmware/psci/psci_checker.c
+++ b/drivers/firmware/psci/psci_checker.c
@@ -167,6 +167,7 @@  static int alloc_init_cpu_groups(cpumask_var_t **pcpu_groups)
 			topology_core_cpumask(cpumask_any(tmp));
 
 		if (!alloc_cpumask_var(&cpu_groups[num_groups], GFP_KERNEL)) {
+			free_cpumask_var(tmp);
 			free_cpu_groups(num_groups, &cpu_groups);
 			return -ENOMEM;
 		}