diff mbox

[1/2] cpufreq: powernv: Check for devm_kzalloc() failure

Message ID 1521899375-24335-1-git-send-email-festevam@gmail.com (mailing list archive)
State Superseded, archived
Headers show

Commit Message

Fabio Estevam March 24, 2018, 1:49 p.m. UTC
From: Fabio Estevam <fabio.estevam@nxp.com>

devm_kzalloc() may fail, so we should better check for error and
propagate the error in the case of allocation failure.

This avoids a potential NULL pointer dereference later on.

Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>
---
 drivers/cpufreq/powernv-cpufreq.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Viresh Kumar April 2, 2018, 5:03 a.m. UTC | #1
On 24-03-18, 10:49, Fabio Estevam wrote:
> From: Fabio Estevam <fabio.estevam@nxp.com>
> 
> devm_kzalloc() may fail, so we should better check for error and
> propagate the error in the case of allocation failure.
> 
> This avoids a potential NULL pointer dereference later on.
> 
> Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>
> ---
>  drivers/cpufreq/powernv-cpufreq.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/cpufreq/powernv-cpufreq.c b/drivers/cpufreq/powernv-cpufreq.c
> index 0591874..4ddfec9 100644
> --- a/drivers/cpufreq/powernv-cpufreq.c
> +++ b/drivers/cpufreq/powernv-cpufreq.c
> @@ -329,6 +329,8 @@ static int init_powernv_pstates(void)
>  
>  		revmap_data = (struct pstate_idx_revmap_data *)
>  			      kmalloc(sizeof(*revmap_data), GFP_KERNEL);
> +		if (!revmap_data)
> +			return -ENOMEM;

What about freeing resources on such failures ?
Rafael J. Wysocki April 23, 2018, 8:57 a.m. UTC | #2
On Saturday, March 24, 2018 2:49:34 PM CEST Fabio Estevam wrote:
> From: Fabio Estevam <fabio.estevam@nxp.com>
> 
> devm_kzalloc() may fail, so we should better check for error and
> propagate the error in the case of allocation failure.
> 
> This avoids a potential NULL pointer dereference later on.
> 
> Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>
> ---
>  drivers/cpufreq/powernv-cpufreq.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/cpufreq/powernv-cpufreq.c b/drivers/cpufreq/powernv-cpufreq.c
> index 0591874..4ddfec9 100644
> --- a/drivers/cpufreq/powernv-cpufreq.c
> +++ b/drivers/cpufreq/powernv-cpufreq.c
> @@ -329,6 +329,8 @@ static int init_powernv_pstates(void)
>  
>  		revmap_data = (struct pstate_idx_revmap_data *)
>  			      kmalloc(sizeof(*revmap_data), GFP_KERNEL);
> +		if (!revmap_data)
> +			return -ENOMEM;

As per the response from Viresh, are there any resources to free in this case?

>  
>  		revmap_data->pstate_id = id & 0xFF;
>  		revmap_data->cpufreq_table_idx = i;
> 

Overall, if you make powernv changes, please CC the PowerPC maintainers
as per the LINUX FOR POWERPC entry in MAINTAINERS.

Thanks,
Rafel
Fabio Estevam April 23, 2018, 6:50 p.m. UTC | #3
On Mon, Apr 2, 2018 at 2:03 AM, Viresh Kumar <viresh.kumar@linaro.org> wrote:

>> --- a/drivers/cpufreq/powernv-cpufreq.c
>> +++ b/drivers/cpufreq/powernv-cpufreq.c
>> @@ -329,6 +329,8 @@ static int init_powernv_pstates(void)
>>
>>               revmap_data = (struct pstate_idx_revmap_data *)
>>                             kmalloc(sizeof(*revmap_data), GFP_KERNEL);
>> +             if (!revmap_data)
>> +                     return -ENOMEM;
>
> What about freeing resources on such failures ?

What are the resources that you are talking about?
Viresh Kumar April 24, 2018, 4:14 a.m. UTC | #4
On 23-04-18, 15:50, Fabio Estevam wrote:
> On Mon, Apr 2, 2018 at 2:03 AM, Viresh Kumar <viresh.kumar@linaro.org> wrote:
> 
> >> --- a/drivers/cpufreq/powernv-cpufreq.c
> >> +++ b/drivers/cpufreq/powernv-cpufreq.c
> >> @@ -329,6 +329,8 @@ static int init_powernv_pstates(void)
> >>
> >>               revmap_data = (struct pstate_idx_revmap_data *)
> >>                             kmalloc(sizeof(*revmap_data), GFP_KERNEL);
> >> +             if (!revmap_data)
> >> +                     return -ENOMEM;
> >
> > What about freeing resources on such failures ?
> 
> What are the resources that you are talking about?

You are allocating revmap_data for all states one by one. You need to
undo that for all previous states once this loop fails for any one of
them. Isn't that right ?
diff mbox

Patch

diff --git a/drivers/cpufreq/powernv-cpufreq.c b/drivers/cpufreq/powernv-cpufreq.c
index 0591874..4ddfec9 100644
--- a/drivers/cpufreq/powernv-cpufreq.c
+++ b/drivers/cpufreq/powernv-cpufreq.c
@@ -329,6 +329,8 @@  static int init_powernv_pstates(void)
 
 		revmap_data = (struct pstate_idx_revmap_data *)
 			      kmalloc(sizeof(*revmap_data), GFP_KERNEL);
+		if (!revmap_data)
+			return -ENOMEM;
 
 		revmap_data->pstate_id = id & 0xFF;
 		revmap_data->cpufreq_table_idx = i;