[PATCH] drivers core: bus: Remove unnecessary NULL assignments in the bus_register() function

Yaxiong Tian posted 1 patch 1 year, 2 months ago
drivers/base/bus.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] drivers core: bus: Remove unnecessary NULL assignments in the bus_register() function
Posted by Yaxiong Tian 1 year, 2 months ago
In bus_register(), Priv is a local pointer variable, so its assignment
can be ignored and the function can be returned directly. This can reduce
the NULL checks in the subsequent kfree function.

Signed-off-by: Yaxiong Tian <tianyaxiong@kylinos.cn>
---
 drivers/base/bus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index 657c93c38b0d..c1fd2860e397 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -923,7 +923,7 @@ int bus_register(const struct bus_type *bus)
 bus_uevent_fail:
 	kset_unregister(&priv->subsys);
 	/* Above kset_unregister() will kfree @priv */
-	priv = NULL;
+	return retval;
 out:
 	kfree(priv);
 	return retval;
-- 
2.25.1
Re: [PATCH] drivers core: bus: Remove unnecessary NULL assignments in the bus_register() function
Posted by Greg KH 1 year ago
On Fri, Nov 29, 2024 at 11:01:30AM +0800, Yaxiong Tian wrote:
> In bus_register(), Priv is a local pointer variable, so its assignment
> can be ignored and the function can be returned directly. This can reduce
> the NULL checks in the subsequent kfree function.
> 
> Signed-off-by: Yaxiong Tian <tianyaxiong@kylinos.cn>
> ---
>  drivers/base/bus.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/base/bus.c b/drivers/base/bus.c
> index 657c93c38b0d..c1fd2860e397 100644
> --- a/drivers/base/bus.c
> +++ b/drivers/base/bus.c
> @@ -923,7 +923,7 @@ int bus_register(const struct bus_type *bus)
>  bus_uevent_fail:
>  	kset_unregister(&priv->subsys);
>  	/* Above kset_unregister() will kfree @priv */
> -	priv = NULL;
> +	return retval;
>  out:
>  	kfree(priv);
>  	return retval;

The goal here was to fall down through the out: block, which is why priv
was set to NULL.  So the code is correct as-is, no need to change
anything here as that would just be a preference change from what the
original author wanted with no functional change at all.

thanks,

greg k-h