diff mbox series

counter: Set counter device name

Message ID 20220203073459.90093-1-vilhelm.gray@gmail.com (mailing list archive)
State Handled Elsewhere
Headers show
Series counter: Set counter device name | expand

Commit Message

William Breathitt Gray Feb. 3, 2022, 7:34 a.m. UTC
Naming the counter device provides a convenient way to identify it in
devres_log events and similar situations. This patch names the counter
device by combining the prefix "counter" with the counter device's
unique ID.

Cc: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: William Breathitt Gray <vilhelm.gray@gmail.com>
---
 drivers/counter/counter-core.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)


base-commit: 7b9c8e1a0ca18a62565ee0e28b23baf7b176e96f

Comments

Uwe Kleine-König Feb. 3, 2022, 9 a.m. UTC | #1
On Thu, Feb 03, 2022 at 04:34:59PM +0900, William Breathitt Gray wrote:
> Naming the counter device provides a convenient way to identify it in
> devres_log events and similar situations. This patch names the counter
> device by combining the prefix "counter" with the counter device's
> unique ID.
> 
> Cc: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> Signed-off-by: William Breathitt Gray <vilhelm.gray@gmail.com>
> ---
>  drivers/counter/counter-core.c | 12 +++++++++++-
>  1 file changed, 11 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
> index 7e0957eea094..2ebd9adf0bd2 100644
> --- a/drivers/counter/counter-core.c
> +++ b/drivers/counter/counter-core.c
> @@ -22,6 +22,8 @@
>  #include "counter-chrdev.h"
>  #include "counter-sysfs.h"
>  
> +#define COUNTER_NAME	"counter"
> +
>  /* Provides a unique ID for each counter device */
>  static DEFINE_IDA(counter_ida);
>  
> @@ -104,6 +106,10 @@ struct counter_device *counter_alloc(size_t sizeof_priv)
>  		goto err_ida_alloc;
>  	dev->id = err;
>  
> +	err = dev_set_name(dev, COUNTER_NAME "%d", dev->id);
> +	if (err)
> +		goto err_dev_set_name;
> +
>  	mutex_init(&counter->ops_exist_lock);
>  	dev->type = &counter_device_type;
>  	dev->bus = &counter_bus_type;
> @@ -119,6 +125,9 @@ struct counter_device *counter_alloc(size_t sizeof_priv)
>  
>  err_chrdev_add:
>  
> +	kfree(dev_name(dev));
> +err_dev_set_name:
> +

I think this is wrong as kobject_cleanup frees the name.

Best regards
Uwe
William Breathitt Gray Feb. 4, 2022, 6:41 a.m. UTC | #2
On Thu, Feb 03, 2022 at 10:00:53AM +0100, Uwe Kleine-König wrote:
> On Thu, Feb 03, 2022 at 04:34:59PM +0900, William Breathitt Gray wrote:
> > Naming the counter device provides a convenient way to identify it in
> > devres_log events and similar situations. This patch names the counter
> > device by combining the prefix "counter" with the counter device's
> > unique ID.
> > 
> > Cc: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> > Signed-off-by: William Breathitt Gray <vilhelm.gray@gmail.com>
> > ---
> >  drivers/counter/counter-core.c | 12 +++++++++++-
> >  1 file changed, 11 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
> > index 7e0957eea094..2ebd9adf0bd2 100644
> > --- a/drivers/counter/counter-core.c
> > +++ b/drivers/counter/counter-core.c
> > @@ -22,6 +22,8 @@
> >  #include "counter-chrdev.h"
> >  #include "counter-sysfs.h"
> >  
> > +#define COUNTER_NAME	"counter"
> > +
> >  /* Provides a unique ID for each counter device */
> >  static DEFINE_IDA(counter_ida);
> >  
> > @@ -104,6 +106,10 @@ struct counter_device *counter_alloc(size_t sizeof_priv)
> >  		goto err_ida_alloc;
> >  	dev->id = err;
> >  
> > +	err = dev_set_name(dev, COUNTER_NAME "%d", dev->id);
> > +	if (err)
> > +		goto err_dev_set_name;
> > +
> >  	mutex_init(&counter->ops_exist_lock);
> >  	dev->type = &counter_device_type;
> >  	dev->bus = &counter_bus_type;
> > @@ -119,6 +125,9 @@ struct counter_device *counter_alloc(size_t sizeof_priv)
> >  
> >  err_chrdev_add:
> >  
> > +	kfree(dev_name(dev));
> > +err_dev_set_name:
> > +
> 
> I think this is wrong as kobject_cleanup frees the name.
> 
> Best regards
> Uwe

This is true once the device is initialized by device_initialize() but
that won't happen until after counter_chrdev_add() is called; if
counter_chrdev_add() fails, we need to free this memory lest it leak
when we return the error code.

I did notice kobject_cleanup() calls kfree_const() on the name rather
than kfree(). Would it make sense to change this patch to call
kfree_const() instead?

William Breathitt Gray
Uwe Kleine-König Feb. 4, 2022, 7:43 a.m. UTC | #3
On Fri, Feb 04, 2022 at 03:41:33PM +0900, William Breathitt Gray wrote:
> On Thu, Feb 03, 2022 at 10:00:53AM +0100, Uwe Kleine-König wrote:
> > On Thu, Feb 03, 2022 at 04:34:59PM +0900, William Breathitt Gray wrote:
> > > Naming the counter device provides a convenient way to identify it in
> > > devres_log events and similar situations. This patch names the counter
> > > device by combining the prefix "counter" with the counter device's
> > > unique ID.
> > > 
> > > Cc: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> > > Signed-off-by: William Breathitt Gray <vilhelm.gray@gmail.com>
> > > ---
> > >  drivers/counter/counter-core.c | 12 +++++++++++-
> > >  1 file changed, 11 insertions(+), 1 deletion(-)
> > > 
> > > diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
> > > index 7e0957eea094..2ebd9adf0bd2 100644
> > > --- a/drivers/counter/counter-core.c
> > > +++ b/drivers/counter/counter-core.c
> > > @@ -22,6 +22,8 @@
> > >  #include "counter-chrdev.h"
> > >  #include "counter-sysfs.h"
> > >  
> > > +#define COUNTER_NAME	"counter"
> > > +
> > >  /* Provides a unique ID for each counter device */
> > >  static DEFINE_IDA(counter_ida);
> > >  
> > > @@ -104,6 +106,10 @@ struct counter_device *counter_alloc(size_t sizeof_priv)
> > >  		goto err_ida_alloc;
> > >  	dev->id = err;
> > >  
> > > +	err = dev_set_name(dev, COUNTER_NAME "%d", dev->id);
> > > +	if (err)
> > > +		goto err_dev_set_name;
> > > +
> > >  	mutex_init(&counter->ops_exist_lock);
> > >  	dev->type = &counter_device_type;
> > >  	dev->bus = &counter_bus_type;
> > > @@ -119,6 +125,9 @@ struct counter_device *counter_alloc(size_t sizeof_priv)
> > >  
> > >  err_chrdev_add:
> > >  
> > > +	kfree(dev_name(dev));
> > > +err_dev_set_name:
> > > +
> > 
> > I think this is wrong as kobject_cleanup frees the name.
> 
> This is true once the device is initialized by device_initialize() but
> that won't happen until after counter_chrdev_add() is called; if
> counter_chrdev_add() fails, we need to free this memory lest it leak
> when we return the error code.

Ah I see. Still I would consider it a layer violation to have to know
this has to be freed (also about kfree vs. kfree_const). I suggest to
move setting the name after the call to device_initialize(). Then the
device core cares for that.

Best regards
Uwe
diff mbox series

Patch

diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
index 7e0957eea094..2ebd9adf0bd2 100644
--- a/drivers/counter/counter-core.c
+++ b/drivers/counter/counter-core.c
@@ -22,6 +22,8 @@ 
 #include "counter-chrdev.h"
 #include "counter-sysfs.h"
 
+#define COUNTER_NAME	"counter"
+
 /* Provides a unique ID for each counter device */
 static DEFINE_IDA(counter_ida);
 
@@ -104,6 +106,10 @@  struct counter_device *counter_alloc(size_t sizeof_priv)
 		goto err_ida_alloc;
 	dev->id = err;
 
+	err = dev_set_name(dev, COUNTER_NAME "%d", dev->id);
+	if (err)
+		goto err_dev_set_name;
+
 	mutex_init(&counter->ops_exist_lock);
 	dev->type = &counter_device_type;
 	dev->bus = &counter_bus_type;
@@ -119,6 +125,9 @@  struct counter_device *counter_alloc(size_t sizeof_priv)
 
 err_chrdev_add:
 
+	kfree(dev_name(dev));
+err_dev_set_name:
+
 	ida_free(&counter_ida, dev->id);
 err_ida_alloc:
 
@@ -250,7 +259,8 @@  static int __init counter_init(void)
 	if (err < 0)
 		return err;
 
-	err = alloc_chrdev_region(&counter_devt, 0, COUNTER_DEV_MAX, "counter");
+	err = alloc_chrdev_region(&counter_devt, 0, COUNTER_DEV_MAX,
+				  COUNTER_NAME);
 	if (err < 0)
 		goto err_unregister_bus;