[PATCH] platform/x86: hp-bioscfg: Fix error handling in hp_add_other_attributes()

Harshit Mogalapalli posted 1 patch 2 years, 1 month ago
drivers/platform/x86/hp/hp-bioscfg/bioscfg.c | 14 ++++++--------
1 file changed, 6 insertions(+), 8 deletions(-)
[PATCH] platform/x86: hp-bioscfg: Fix error handling in hp_add_other_attributes()
Posted by Harshit Mogalapalli 2 years, 1 month ago
1. acpi_object *obj is unused in this function, so delete it, also
   delete a unnecessary kfree(obj);
2. Fix a memory leak of 'attr_name_kobj' in the error handling path.
3. When kobject_init_and_add() fails on every subsequent error path call
   kobject_put() to cleanup.

Fixes: a34fc329b189 ("platform/x86: hp-bioscfg: bioscfg")
Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <error27@gmail.com>
Closes: https://lore.kernel.org/r/202309201412.on0VXJGo-lkp@intel.com/
Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
---
This is only compile tested, based on static analysis.
---
 drivers/platform/x86/hp/hp-bioscfg/bioscfg.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c b/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c
index 5798b49ddaba..b28e52b64690 100644
--- a/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c
+++ b/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c
@@ -588,7 +588,6 @@ static void release_attributes_data(void)
 static int hp_add_other_attributes(int attr_type)
 {
 	struct kobject *attr_name_kobj;
-	union acpi_object *obj = NULL;
 	int ret;
 	char *attr_name;
 
@@ -596,8 +595,8 @@ static int hp_add_other_attributes(int attr_type)
 
 	attr_name_kobj = kzalloc(sizeof(*attr_name_kobj), GFP_KERNEL);
 	if (!attr_name_kobj) {
-		ret = -ENOMEM;
-		goto err_other_attr_init;
+		mutex_unlock(&bioscfg_drv.mutex);
+		return -ENOMEM;
 	}
 
 	/* Check if attribute type is supported */
@@ -614,15 +613,15 @@ static int hp_add_other_attributes(int attr_type)
 
 	default:
 		pr_err("Error: Unknown attr_type: %d\n", attr_type);
-		ret = -EINVAL;
-		goto err_other_attr_init;
+		kfree(attr_name_kobj);
+		mutex_unlock(&bioscfg_drv.mutex);
+		return -EINVAL;
 	}
 
 	ret = kobject_init_and_add(attr_name_kobj, &attr_name_ktype,
 				   NULL, "%s", attr_name);
 	if (ret) {
 		pr_err("Error encountered [%d]\n", ret);
-		kobject_put(attr_name_kobj);
 		goto err_other_attr_init;
 	}
 
@@ -647,10 +646,9 @@ static int hp_add_other_attributes(int attr_type)
 
 	mutex_unlock(&bioscfg_drv.mutex);
 	return 0;
-
 err_other_attr_init:
+	kobject_put(attr_name_kobj);
 	mutex_unlock(&bioscfg_drv.mutex);
-	kfree(obj);
 	return ret;
 }
 
-- 
2.39.3
Re: [PATCH] platform/x86: hp-bioscfg: Fix error handling in hp_add_other_attributes()
Posted by Hans de Goede 2 years, 1 month ago
Hi Harshit,

On 11/10/23 10:04, Harshit Mogalapalli wrote:
> 1. acpi_object *obj is unused in this function, so delete it, also
>    delete a unnecessary kfree(obj);
> 2. Fix a memory leak of 'attr_name_kobj' in the error handling path.
> 3. When kobject_init_and_add() fails on every subsequent error path call
>    kobject_put() to cleanup.

If you are fixing 3 different things, please do so in a series
of 3 patches each addressing 1 of the 3 things.

Regards,

Hans



> 
> Fixes: a34fc329b189 ("platform/x86: hp-bioscfg: bioscfg")
> Reported-by: kernel test robot <lkp@intel.com>
> Reported-by: Dan Carpenter <error27@gmail.com>
> Closes: https://lore.kernel.org/r/202309201412.on0VXJGo-lkp@intel.com/
> Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
> ---
> This is only compile tested, based on static analysis.
> ---
>  drivers/platform/x86/hp/hp-bioscfg/bioscfg.c | 14 ++++++--------
>  1 file changed, 6 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c b/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c
> index 5798b49ddaba..b28e52b64690 100644
> --- a/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c
> +++ b/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c
> @@ -588,7 +588,6 @@ static void release_attributes_data(void)
>  static int hp_add_other_attributes(int attr_type)
>  {
>  	struct kobject *attr_name_kobj;
> -	union acpi_object *obj = NULL;
>  	int ret;
>  	char *attr_name;
>  
> @@ -596,8 +595,8 @@ static int hp_add_other_attributes(int attr_type)
>  
>  	attr_name_kobj = kzalloc(sizeof(*attr_name_kobj), GFP_KERNEL);
>  	if (!attr_name_kobj) {
> -		ret = -ENOMEM;
> -		goto err_other_attr_init;
> +		mutex_unlock(&bioscfg_drv.mutex);
> +		return -ENOMEM;
>  	}
>  
>  	/* Check if attribute type is supported */
> @@ -614,15 +613,15 @@ static int hp_add_other_attributes(int attr_type)
>  
>  	default:
>  		pr_err("Error: Unknown attr_type: %d\n", attr_type);
> -		ret = -EINVAL;
> -		goto err_other_attr_init;
> +		kfree(attr_name_kobj);
> +		mutex_unlock(&bioscfg_drv.mutex);
> +		return -EINVAL;
>  	}
>  
>  	ret = kobject_init_and_add(attr_name_kobj, &attr_name_ktype,
>  				   NULL, "%s", attr_name);
>  	if (ret) {
>  		pr_err("Error encountered [%d]\n", ret);
> -		kobject_put(attr_name_kobj);
>  		goto err_other_attr_init;
>  	}
>  
> @@ -647,10 +646,9 @@ static int hp_add_other_attributes(int attr_type)
>  
>  	mutex_unlock(&bioscfg_drv.mutex);
>  	return 0;
> -
>  err_other_attr_init:
> +	kobject_put(attr_name_kobj);
>  	mutex_unlock(&bioscfg_drv.mutex);
> -	kfree(obj);
>  	return ret;
>  }
>
Re: [PATCH] platform/x86: hp-bioscfg: Fix error handling in hp_add_other_attributes()
Posted by Harshit Mogalapalli 2 years, 1 month ago
Hi Hans,
On 10/11/23 4:50 pm, Hans de Goede wrote:
> Hi Harshit,
> 
> On 11/10/23 10:04, Harshit Mogalapalli wrote:
>> 1. acpi_object *obj is unused in this function, so delete it, also
>>     delete a unnecessary kfree(obj);
>> 2. Fix a memory leak of 'attr_name_kobj' in the error handling path.
>> 3. When kobject_init_and_add() fails on every subsequent error path call
>>     kobject_put() to cleanup.
> 
> If you are fixing 3 different things, please do so in a series
> of 3 patches each addressing 1 of the 3 things.
> 

Issue 1 is solved by cleaning up obj, so that can be in patch 1.
Issue 2 & 3 are solved by using a kobj_put(which also takes care of 
kfree()) correctly before returning on an error path, so issue 2,3 will 
have a same fix. This can be done in patch 2.

I will send a V2 with splitting the patches.

Thanks,
Harshit

> Regards,
> 
> Hans
> 
> 
> 
>>
>> Fixes: a34fc329b189 ("platform/x86: hp-bioscfg: bioscfg")
>> Reported-by: kernel test robot <lkp@intel.com>
>> Reported-by: Dan Carpenter <error27@gmail.com>
>> Closes: https://lore.kernel.org/r/202309201412.on0VXJGo-lkp@intel.com/
>> Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
>> ---
>> This is only compile tested, based on static analysis.
>> ---
>>   drivers/platform/x86/hp/hp-bioscfg/bioscfg.c | 14 ++++++--------
>>   1 file changed, 6 insertions(+), 8 deletions(-)
>>
>> diff --git a/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c b/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c
>> index 5798b49ddaba..b28e52b64690 100644
>> --- a/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c
>> +++ b/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c
>> @@ -588,7 +588,6 @@ static void release_attributes_data(void)
>>   static int hp_add_other_attributes(int attr_type)
>>   {
>>   	struct kobject *attr_name_kobj;
>> -	union acpi_object *obj = NULL;
>>   	int ret;
>>   	char *attr_name;
>>   
>> @@ -596,8 +595,8 @@ static int hp_add_other_attributes(int attr_type)
>>   
>>   	attr_name_kobj = kzalloc(sizeof(*attr_name_kobj), GFP_KERNEL);
>>   	if (!attr_name_kobj) {
>> -		ret = -ENOMEM;
>> -		goto err_other_attr_init;
>> +		mutex_unlock(&bioscfg_drv.mutex);
>> +		return -ENOMEM;
>>   	}
>>   
>>   	/* Check if attribute type is supported */
>> @@ -614,15 +613,15 @@ static int hp_add_other_attributes(int attr_type)
>>   
>>   	default:
>>   		pr_err("Error: Unknown attr_type: %d\n", attr_type);
>> -		ret = -EINVAL;
>> -		goto err_other_attr_init;
>> +		kfree(attr_name_kobj);
>> +		mutex_unlock(&bioscfg_drv.mutex);
>> +		return -EINVAL;
>>   	}
>>   
>>   	ret = kobject_init_and_add(attr_name_kobj, &attr_name_ktype,
>>   				   NULL, "%s", attr_name);
>>   	if (ret) {
>>   		pr_err("Error encountered [%d]\n", ret);
>> -		kobject_put(attr_name_kobj);
>>   		goto err_other_attr_init;
>>   	}
>>   
>> @@ -647,10 +646,9 @@ static int hp_add_other_attributes(int attr_type)
>>   
>>   	mutex_unlock(&bioscfg_drv.mutex);
>>   	return 0;
>> -
>>   err_other_attr_init:
>> +	kobject_put(attr_name_kobj);
>>   	mutex_unlock(&bioscfg_drv.mutex);
>> -	kfree(obj);
>>   	return ret;
>>   }
>>   
> 
>
Re: [PATCH] platform/x86: hp-bioscfg: Fix error handling in hp_add_other_attributes()
Posted by Ilpo Järvinen 2 years, 1 month ago
On Fri, 10 Nov 2023, Harshit Mogalapalli wrote:

> 1. acpi_object *obj is unused in this function, so delete it, also
>    delete a unnecessary kfree(obj);
> 2. Fix a memory leak of 'attr_name_kobj' in the error handling path.
> 3. When kobject_init_and_add() fails on every subsequent error path call
>    kobject_put() to cleanup.
> 
> Fixes: a34fc329b189 ("platform/x86: hp-bioscfg: bioscfg")
> Reported-by: kernel test robot <lkp@intel.com>
> Reported-by: Dan Carpenter <error27@gmail.com>
> Closes: https://lore.kernel.org/r/202309201412.on0VXJGo-lkp@intel.com/
> Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
> ---
> This is only compile tested, based on static analysis.
> ---
>  drivers/platform/x86/hp/hp-bioscfg/bioscfg.c | 14 ++++++--------
>  1 file changed, 6 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c b/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c
> index 5798b49ddaba..b28e52b64690 100644
> --- a/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c
> +++ b/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c
> @@ -588,7 +588,6 @@ static void release_attributes_data(void)
>  static int hp_add_other_attributes(int attr_type)
>  {
>  	struct kobject *attr_name_kobj;
> -	union acpi_object *obj = NULL;
>  	int ret;
>  	char *attr_name;
>  
> @@ -596,8 +595,8 @@ static int hp_add_other_attributes(int attr_type)
>  
>  	attr_name_kobj = kzalloc(sizeof(*attr_name_kobj), GFP_KERNEL);
>  	if (!attr_name_kobj) {
> -		ret = -ENOMEM;
> -		goto err_other_attr_init;
> +		mutex_unlock(&bioscfg_drv.mutex);

I don't understand why this has to be inside the mutex at all, cannot 
you just move it outside of the mutex and then there's no need to unlock?

> +		return -ENOMEM;
>  	}
>  
>  	/* Check if attribute type is supported */
> @@ -614,15 +613,15 @@ static int hp_add_other_attributes(int attr_type)
>  
>  	default:
>  		pr_err("Error: Unknown attr_type: %d\n", attr_type);
> -		ret = -EINVAL;
> -		goto err_other_attr_init;
> +		kfree(attr_name_kobj);
> +		mutex_unlock(&bioscfg_drv.mutex);
> +		return -EINVAL;

Add a new label for unlock and goto to it instead.

>  	}
>  
>  	ret = kobject_init_and_add(attr_name_kobj, &attr_name_ktype,
>  				   NULL, "%s", attr_name);
>  	if (ret) {
>  		pr_err("Error encountered [%d]\n", ret);
> -		kobject_put(attr_name_kobj);
>  		goto err_other_attr_init;
>  	}
>  
> @@ -647,10 +646,9 @@ static int hp_add_other_attributes(int attr_type)
>  
>  	mutex_unlock(&bioscfg_drv.mutex);
>  	return 0;
> -
>  err_other_attr_init:
> +	kobject_put(attr_name_kobj);

unlock:

>  	mutex_unlock(&bioscfg_drv.mutex);
> -	kfree(obj);
>  	return ret;
>  }
>  
> 


-- 
 i.
Re: [PATCH] platform/x86: hp-bioscfg: Fix error handling in hp_add_other_attributes()
Posted by Harshit Mogalapalli 2 years, 1 month ago
Hi Ilpo,

On 10/11/23 6:45 pm, Ilpo Järvinen wrote:
> On Fri, 10 Nov 2023, Harshit Mogalapalli wrote:
> 
>> 1. acpi_object *obj is unused in this function, so delete it, also
>>     delete a unnecessary kfree(obj);
>> 2. Fix a memory leak of 'attr_name_kobj' in the error handling path.
>> 3. When kobject_init_and_add() fails on every subsequent error path call
>>     kobject_put() to cleanup.
>>
>> Fixes: a34fc329b189 ("platform/x86: hp-bioscfg: bioscfg")
>> Reported-by: kernel test robot <lkp@intel.com>
>> Reported-by: Dan Carpenter <error27@gmail.com>
>> Closes: https://lore.kernel.org/r/202309201412.on0VXJGo-lkp@intel.com/
>> Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
>> ---
>> This is only compile tested, based on static analysis.
>> ---
>>   drivers/platform/x86/hp/hp-bioscfg/bioscfg.c | 14 ++++++--------
>>   1 file changed, 6 insertions(+), 8 deletions(-)
>>
>> diff --git a/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c b/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c
>> index 5798b49ddaba..b28e52b64690 100644
>> --- a/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c
>> +++ b/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c
>> @@ -588,7 +588,6 @@ static void release_attributes_data(void)
>>   static int hp_add_other_attributes(int attr_type)
>>   {
>>   	struct kobject *attr_name_kobj;
>> -	union acpi_object *obj = NULL;
>>   	int ret;
>>   	char *attr_name;
>>   
>> @@ -596,8 +595,8 @@ static int hp_add_other_attributes(int attr_type)
>>   
>>   	attr_name_kobj = kzalloc(sizeof(*attr_name_kobj), GFP_KERNEL);
>>   	if (!attr_name_kobj) {
>> -		ret = -ENOMEM;
>> -		goto err_other_attr_init;
>> +		mutex_unlock(&bioscfg_drv.mutex);
> 
> I don't understand why this has to be inside the mutex at all, cannot
> you just move it outside of the mutex and then there's no need to unlock?
> 

Oh right, I think this allocation can be done without a lock.

Will move this down in my v2.

Thanks for the suggestion.

Regards,
Harshit

>> +		return -ENOMEM;
>>   	}
>>   
>>   	/* Check if attribute type is supported */
>> @@ -614,15 +613,15 @@ static int hp_add_other_attributes(int attr_type)
>>   
>>   	default:
>>   		pr_err("Error: Unknown attr_type: %d\n", attr_type);
>> -		ret = -EINVAL;
>> -		goto err_other_attr_init;
>> +		kfree(attr_name_kobj);
>> +		mutex_unlock(&bioscfg_drv.mutex);
>> +		return -EINVAL;
> 
> Add a new label for unlock and goto to it instead.
> 
>>   	}
>>   
>>   	ret = kobject_init_and_add(attr_name_kobj, &attr_name_ktype,
>>   				   NULL, "%s", attr_name);
>>   	if (ret) {
>>   		pr_err("Error encountered [%d]\n", ret);
>> -		kobject_put(attr_name_kobj);
>>   		goto err_other_attr_init;
>>   	}
>>   
>> @@ -647,10 +646,9 @@ static int hp_add_other_attributes(int attr_type)
>>   
>>   	mutex_unlock(&bioscfg_drv.mutex);
>>   	return 0;
>> -
>>   err_other_attr_init:
>> +	kobject_put(attr_name_kobj);
> 
> unlock:
> 
>>   	mutex_unlock(&bioscfg_drv.mutex);
>> -	kfree(obj);
>>   	return ret;
>>   }
>>   
>>
> 
>