2021-04-09 11:10:53

by Dan Carpenter

[permalink] [raw]
Subject: [PATCH] HSI: core: fix resource leaks in hsi_add_client_from_dt()

If some of the allocations fail between the dev_set_name() and the
device_register() then the name will not be freed. Fix this by
moving dev_set_name() directly in front of the call to device_register().

Fixes: a2aa24734d9d ("HSI: Add common DT binding for HSI client devices")
Signed-off-by: Dan Carpenter <[email protected]>
---
Jason, this is the most common type of error I see with device_register().
Is there a downside to calling dev_set_name() later? Presumably it's
printed out somewhere, but I feel like just moving the dev_set_name() is
almost always the best and simplest fix.

drivers/hsi/hsi_core.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/hsi/hsi_core.c b/drivers/hsi/hsi_core.c
index c3fb5beb846e..ec90713564e3 100644
--- a/drivers/hsi/hsi_core.c
+++ b/drivers/hsi/hsi_core.c
@@ -210,8 +210,6 @@ static void hsi_add_client_from_dt(struct hsi_port *port,
if (err)
goto err;

- dev_set_name(&cl->device, "%s", name);
-
err = hsi_of_property_parse_mode(client, "hsi-mode", &mode);
if (err) {
err = hsi_of_property_parse_mode(client, "hsi-rx-mode",
@@ -293,6 +291,7 @@ static void hsi_add_client_from_dt(struct hsi_port *port,
cl->device.release = hsi_client_release;
cl->device.of_node = client;

+ dev_set_name(&cl->device, "%s", name);
if (device_register(&cl->device) < 0) {
pr_err("hsi: failed to register client: %s\n", name);
put_device(&cl->device);
--
2.30.2


2021-04-09 17:35:21

by Jason Gunthorpe

[permalink] [raw]
Subject: Re: [PATCH] HSI: core: fix resource leaks in hsi_add_client_from_dt()

On Fri, Apr 09, 2021 at 02:08:17PM +0300, Dan Carpenter wrote:
> If some of the allocations fail between the dev_set_name() and the
> device_register() then the name will not be freed. Fix this by
> moving dev_set_name() directly in front of the call to device_register().
>
> Fixes: a2aa24734d9d ("HSI: Add common DT binding for HSI client devices")
> Signed-off-by: Dan Carpenter <[email protected]>
> ---
> Jason, this is the most common type of error I see with device_register().
> Is there a downside to calling dev_set_name() later? Presumably it's
> printed out somewhere, but I feel like just moving the dev_set_name() is
> almost always the best and simplest fix.

It is hard to tell without detailed analysis.. ie a dev_err()/etc call
will use the name. It is why I don't like this design pattern of
avoiding device_initialize() and using device_registrR()

This movement looks OK though

Reviewed-by: Jason Gunthorpe <[email protected]>

Jason