diff mbox series

[v2,06/15] leds: trigger: blkdev: Add function to get gendisk by name

Message ID 20210909222513.2184795-7-arequipeno@gmail.com
State New
Headers show
Series Introduce block device LED trigger | expand

Commit Message

Ian Pilcher Sept. 9, 2021, 10:25 p.m. UTC
Add ledtrig_blkdev_get_disk() to find block device by name and increment
its reference count.  (Caller must call put_disk().)  Must be built-in to
access block_class and disk_type symbols.

Signed-off-by: Ian Pilcher <arequipeno@gmail.com>
---
 drivers/leds/trigger/ledtrig-blkdev-core.c | 20 ++++++++++++++++++++
 drivers/leds/trigger/ledtrig-blkdev.h      |  3 +++
 2 files changed, 23 insertions(+)

Comments

Greg Kroah-Hartman Sept. 10, 2021, 6:45 a.m. UTC | #1
On Thu, Sep 09, 2021 at 05:25:04PM -0500, Ian Pilcher wrote:
> Add ledtrig_blkdev_get_disk() to find block device by name and increment

> its reference count.  (Caller must call put_disk().)  Must be built-in to

> access block_class and disk_type symbols.

> 

> Signed-off-by: Ian Pilcher <arequipeno@gmail.com>

> ---

>  drivers/leds/trigger/ledtrig-blkdev-core.c | 20 ++++++++++++++++++++

>  drivers/leds/trigger/ledtrig-blkdev.h      |  3 +++

>  2 files changed, 23 insertions(+)

> 

> diff --git a/drivers/leds/trigger/ledtrig-blkdev-core.c b/drivers/leds/trigger/ledtrig-blkdev-core.c

> index d7b02e760b06..5fd741aa14a6 100644

> --- a/drivers/leds/trigger/ledtrig-blkdev-core.c

> +++ b/drivers/leds/trigger/ledtrig-blkdev-core.c

> @@ -33,3 +33,23 @@ void ledtrig_blkdev_disk_cleanup(struct gendisk *const gd)

>  

>  	mutex_unlock(&ledtrig_blkdev_mutex);

>  }

> +

> +/* class_find_device() callback.  Must be built-in to access disk_type. */

> +static int blkdev_match_name(struct device *const dev, const void *const name)

> +{

> +	return dev->type == &disk_type

> +			&& sysfs_streq(dev_to_disk(dev)->disk_name, name);

> +}

> +

> +/* Must be built-in to access block_class */

> +struct gendisk *ledtrig_blkdev_get_disk(const char *const name)

> +{

> +	struct device *dev;

> +

> +	dev = class_find_device(&block_class, NULL, name, blkdev_match_name);

> +	if (dev == NULL)

> +		return NULL;


You now have bumped the reference count on this structure.  Where do you
decrement it when you are finished with it?

thanks,

greg k-h
Ian Pilcher Sept. 10, 2021, 3:17 p.m. UTC | #2
On 9/10/21 1:45 AM, Greg KH wrote:
> You now have bumped the reference count on this structure.  Where do you

> decrement it when you are finished with it?


That happens when I "unlink" the block device from the LED in
blkdev_disk_unlink_locked() at ledtrig-blkdev.c:410.

(And also in the error path of link_device_store() at
ledtrig-blkdev.c:372.)

-- 
========================================================================
                  In Soviet Russia, Google searches you!
========================================================================
Greg Kroah-Hartman Sept. 10, 2021, 3:23 p.m. UTC | #3
On Fri, Sep 10, 2021 at 10:17:03AM -0500, Ian Pilcher wrote:
> On 9/10/21 1:45 AM, Greg KH wrote:

> > You now have bumped the reference count on this structure.  Where do you

> > decrement it when you are finished with it?

> 

> That happens when I "unlink" the block device from the LED in

> blkdev_disk_unlink_locked() at ledtrig-blkdev.c:410.


I have no context here anymore, so I have no idea if this is really true
:(
Ian Pilcher Sept. 10, 2021, 4:28 p.m. UTC | #4
On 9/10/21 1:45 AM, Greg KH wrote:
> On Thu, Sep 09, 2021 at 05:25:04PM -0500, Ian Pilcher wrote:

>> +/* Must be built-in to access block_class */

>> +struct gendisk *ledtrig_blkdev_get_disk(const char *const name)

>> +{

>> +	struct device *dev;

>> +

>> +	dev = class_find_device(&block_class, NULL, name, blkdev_match_name);

>> +	if (dev == NULL)

>> +		return NULL;

> 

> You now have bumped the reference count on this structure.  Where do you

> decrement it when you are finished with it?


With context this time.  Sorry about that.


That happens when I "unlink" the block device from the LED in
blkdev_disk_unlink_locked() at ledtrig-blkdev.c:410.

(And also in the error path of link_device_store() at
ledtrig-blkdev.c:372.)

-- 
========================================================================
                  In Soviet Russia, Google searches you!
========================================================================
diff mbox series

Patch

diff --git a/drivers/leds/trigger/ledtrig-blkdev-core.c b/drivers/leds/trigger/ledtrig-blkdev-core.c
index d7b02e760b06..5fd741aa14a6 100644
--- a/drivers/leds/trigger/ledtrig-blkdev-core.c
+++ b/drivers/leds/trigger/ledtrig-blkdev-core.c
@@ -33,3 +33,23 @@  void ledtrig_blkdev_disk_cleanup(struct gendisk *const gd)
 
 	mutex_unlock(&ledtrig_blkdev_mutex);
 }
+
+/* class_find_device() callback.  Must be built-in to access disk_type. */
+static int blkdev_match_name(struct device *const dev, const void *const name)
+{
+	return dev->type == &disk_type
+			&& sysfs_streq(dev_to_disk(dev)->disk_name, name);
+}
+
+/* Must be built-in to access block_class */
+struct gendisk *ledtrig_blkdev_get_disk(const char *const name)
+{
+	struct device *dev;
+
+	dev = class_find_device(&block_class, NULL, name, blkdev_match_name);
+	if (dev == NULL)
+		return NULL;
+
+	return dev_to_disk(dev);
+}
+EXPORT_SYMBOL_NS_GPL(ledtrig_blkdev_get_disk, LEDTRIG_BLKDEV);
diff --git a/drivers/leds/trigger/ledtrig-blkdev.h b/drivers/leds/trigger/ledtrig-blkdev.h
index b1294da17ba3..a6ff24fad0c2 100644
--- a/drivers/leds/trigger/ledtrig-blkdev.h
+++ b/drivers/leds/trigger/ledtrig-blkdev.h
@@ -12,4 +12,7 @@ 
 extern struct mutex ledtrig_blkdev_mutex;
 extern void (*__ledtrig_blkdev_disk_cleanup)(struct gendisk *gd);
 
+/* Caller must call put_disk() */
+struct gendisk *ledtrig_blkdev_get_disk(const char *const name);
+
 #endif	/* __LEDTRIG_BLKDEV_H */