[PATCH v8 4/8] nvmem: core: Track the registered devices

Miquel Raynal posted 8 patches 2 years, 6 months ago
There is a newer version of this series
[PATCH v8 4/8] nvmem: core: Track the registered devices
Posted by Miquel Raynal 2 years, 6 months ago
Create a list with all the NVMEM devices registered in the
subsystem. This way we can iterate through them when needed (unused for
now).

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/nvmem/core.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 257328887263..4e81e0aaf433 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -23,6 +23,7 @@
 struct nvmem_device {
 	struct module		*owner;
 	struct device		dev;
+	struct list_head	node;
 	int			stride;
 	int			word_size;
 	int			id;
@@ -76,6 +77,9 @@ static LIST_HEAD(nvmem_cell_tables);
 static DEFINE_MUTEX(nvmem_lookup_mutex);
 static LIST_HEAD(nvmem_lookup_list);
 
+static DEFINE_MUTEX(nvmem_devices_mutex);
+static LIST_HEAD(nvmem_devices_list);
+
 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
 
 static DEFINE_SPINLOCK(nvmem_layout_lock);
@@ -1012,6 +1016,10 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	if (rval)
 		goto err_remove_cells;
 
+	mutex_lock(&nvmem_devices_mutex);
+	list_add_tail(&nvmem->node, &nvmem_devices_list);
+	mutex_unlock(&nvmem_devices_mutex);
+
 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
 
 	return nvmem;
@@ -1037,6 +1045,10 @@ static void nvmem_device_release(struct kref *kref)
 
 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
 
+	mutex_lock(&nvmem_devices_mutex);
+	list_del(&nvmem->node);
+	mutex_unlock(&nvmem_devices_mutex);
+
 	if (nvmem->flags & FLAG_COMPAT)
 		device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
 
-- 
2.34.1
Re: [PATCH v8 4/8] nvmem: core: Track the registered devices
Posted by Greg Kroah-Hartman 2 years, 6 months ago
On Mon, Aug 07, 2023 at 10:24:15AM +0200, Miquel Raynal wrote:
> Create a list with all the NVMEM devices registered in the
> subsystem. This way we can iterate through them when needed (unused for
> now).
> 
> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> ---
>  drivers/nvmem/core.c | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
> 
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 257328887263..4e81e0aaf433 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
> @@ -23,6 +23,7 @@
>  struct nvmem_device {
>  	struct module		*owner;
>  	struct device		dev;
> +	struct list_head	node;
>  	int			stride;
>  	int			word_size;
>  	int			id;
> @@ -76,6 +77,9 @@ static LIST_HEAD(nvmem_cell_tables);
>  static DEFINE_MUTEX(nvmem_lookup_mutex);
>  static LIST_HEAD(nvmem_lookup_list);
>  
> +static DEFINE_MUTEX(nvmem_devices_mutex);
> +static LIST_HEAD(nvmem_devices_list);

But this list should already be in the driver core, why create
yet-another-list-and-lock?

Why is "when needed" not sufficient to use the list already present?

And now note, you have the same structure on 2 different lists, watch
out for device lifetime rules :(

thanks,

greg k-h
Re: [PATCH v8 4/8] nvmem: core: Track the registered devices
Posted by Miquel Raynal 2 years, 6 months ago
Hi Greg,

gregkh@linuxfoundation.org wrote on Mon, 7 Aug 2023 11:02:35 +0200:

> On Mon, Aug 07, 2023 at 10:24:15AM +0200, Miquel Raynal wrote:
> > Create a list with all the NVMEM devices registered in the
> > subsystem. This way we can iterate through them when needed (unused for
> > now).
> > 
> > Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> > ---
> >  drivers/nvmem/core.c | 12 ++++++++++++
> >  1 file changed, 12 insertions(+)
> > 
> > diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> > index 257328887263..4e81e0aaf433 100644
> > --- a/drivers/nvmem/core.c
> > +++ b/drivers/nvmem/core.c
> > @@ -23,6 +23,7 @@
> >  struct nvmem_device {
> >  	struct module		*owner;
> >  	struct device		dev;
> > +	struct list_head	node;
> >  	int			stride;
> >  	int			word_size;
> >  	int			id;
> > @@ -76,6 +77,9 @@ static LIST_HEAD(nvmem_cell_tables);
> >  static DEFINE_MUTEX(nvmem_lookup_mutex);
> >  static LIST_HEAD(nvmem_lookup_list);
> >  
> > +static DEFINE_MUTEX(nvmem_devices_mutex);
> > +static LIST_HEAD(nvmem_devices_list);  
> 
> But this list should already be in the driver core, why create
> yet-another-list-and-lock?

I did not think about using it. I believe you mean using
bus_for_each_dev() here? Could definitely make the trick; I'll try.

> 
> Why is "when needed" not sufficient to use the list already present?
> 
> And now note, you have the same structure on 2 different lists, watch
> out for device lifetime rules :(
> 
> thanks,
> 
> greg k-h


Thanks,
Miquèl