On Fri, Apr 09, 2021 at 02:01:57PM +0300, Dan Carpenter wrote:
> We can't use kfree() to free device managed resources so the kfree(dev)
> is against the rules.
> 
> It's easier to write this code if we open code the device_register() as
> a device_initialize() and device_add().  That way if dev_set_name() set
> name fails we can call put_device() and it will clean up correctly.
> 
> Fixes: acc02a109b04 ("node: Add memory-side caching attributes")
> Signed-off-by: Dan Carpenter <dan.carpen...@oracle.com>
> ---
>  drivers/base/node.c | 26 ++++++++++++--------------
>  1 file changed, 12 insertions(+), 14 deletions(-)

Wow, yikes, that "kfree_const(dev->kobj.name);" is really creative

Reviewed-by: Jason Gunthorpe <j...@nvidia.com>

Though I dislike ignoring the error code from dev_set_name(), I think
Greg would prefer this:

diff --git a/drivers/base/node.c b/drivers/base/node.c
index f449dbb2c74666..80079e440add12 100644
--- a/drivers/base/node.c
+++ b/drivers/base/node.c
@@ -140,20 +140,16 @@ static struct node_access_nodes 
*node_init_node_access(struct node *node,
        dev->parent = &node->dev;
        dev->release = node_access_release;
        dev->groups = node_access_node_groups;
-       if (dev_set_name(dev, "access%u", access))
-               goto free;
 
-       if (device_register(dev))
-               goto free_name;
+       dev_set_name(dev, "access%u", access);
+       if (device_register(dev)) {
+               put_device(dev);
+               return NULL;
+       }
 
        pm_runtime_no_callbacks(dev);
        list_add_tail(&access_node->list_node, &node->access_list);
        return access_node;
-free_name:
-       kfree_const(dev->kobj.name);
-free:
-       kfree(access_node);
-       return NULL;
 }
 

(arguably a device_register_name() would be even better, if you are
handy with coccinelle there could quickly be alot of users)

Jason

Reply via email to