[v9,1/7] nvmem: core: Create all cells before adding the nvmem device

Message ID 20230808062932.150588-2-miquel.raynal@bootlin.com
State New
Headers
Series NVMEM cells in sysfs |

Commit Message

Miquel Raynal Aug. 8, 2023, 6:29 a.m. UTC
  Let's pack all the cells creation in one place, so they are all created
before we add the nvmem device.

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

Comments

Srinivas Kandagatla Aug. 11, 2023, 11:11 a.m. UTC | #1
On 08/08/2023 08:24, Miquel Raynal wrote:
> Hi Srinivas,
> 
> srinivas.kandagatla@linaro.org wrote on Tue, 8 Aug 2023 07:56:47 +0100:
> 
>> On 08/08/2023 07:29, Miquel Raynal wrote:
>>> Let's pack all the cells creation in one place, so they are all created
>>> before we add the nvmem device.
>>>
>>> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
>>> ---
>>>    drivers/nvmem/core.c | 12 ++++++------
>>>    1 file changed, 6 insertions(+), 6 deletions(-)
>>>
>>> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
>>> index 3f8c7718412b..48659106a1e2 100644
>>> --- a/drivers/nvmem/core.c
>>> +++ b/drivers/nvmem/core.c
>>> @@ -998,12 +998,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
>>>    	if (rval)
>>>    		goto err_remove_cells;
>>>    > -	dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
>>> -
>>> -	rval = device_add(&nvmem->dev);
>>> -	if (rval)
>>> -		goto err_remove_cells;
>>> -
>>>    	rval = nvmem_add_cells_from_fixed_layout(nvmem);
>>>    	if (rval)
>>>    		goto err_remove_cells;
>>> @@ -1012,6 +1006,12 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
>>>    	if (rval)
>>>    		goto err_remove_cells;
>>>    > +	dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
>>> +
>>> +	rval = device_add(&nvmem->dev);
>>> +	if (rval)
>>> +		goto err_remove_cells;
>>
>> All the error handling paths are now messed up with this patch, put_device() in error path will be called incorrectly from multiple places.
> 
> I'm not sure what this means. Perhaps I should additionally call
> device_del() after device_add was successful to mimic the
> device_unregister() call from the remove path. Is that what you mean?


This looks perfectly fine, no change required. This also fixes a bug of 
missing device_del() in err path.

pl, Ignore my old comments.

> 
> I also see the layout_np below should be freed before jumping in the
> error section.

you mean missing of_node_put()?

--srini
  
Miquel Raynal Aug. 11, 2023, 12:11 p.m. UTC | #2
Hi Srinivas,

srinivas.kandagatla@linaro.org wrote on Fri, 11 Aug 2023 12:11:19 +0100:

> On 08/08/2023 08:24, Miquel Raynal wrote:
> > Hi Srinivas,
> > 
> > srinivas.kandagatla@linaro.org wrote on Tue, 8 Aug 2023 07:56:47 +0100:
> >   
> >> On 08/08/2023 07:29, Miquel Raynal wrote:  
> >>> Let's pack all the cells creation in one place, so they are all created
> >>> before we add the nvmem device.
> >>>
> >>> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> >>> ---
> >>>    drivers/nvmem/core.c | 12 ++++++------
> >>>    1 file changed, 6 insertions(+), 6 deletions(-)
> >>>
> >>> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> >>> index 3f8c7718412b..48659106a1e2 100644
> >>> --- a/drivers/nvmem/core.c
> >>> +++ b/drivers/nvmem/core.c
> >>> @@ -998,12 +998,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
> >>>    	if (rval)
> >>>    		goto err_remove_cells;  
> >>>    > -	dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);  
> >>> -
> >>> -	rval = device_add(&nvmem->dev);
> >>> -	if (rval)
> >>> -		goto err_remove_cells;
> >>> -
> >>>    	rval = nvmem_add_cells_from_fixed_layout(nvmem);
> >>>    	if (rval)
> >>>    		goto err_remove_cells;
> >>> @@ -1012,6 +1006,12 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
> >>>    	if (rval)
> >>>    		goto err_remove_cells;  
> >>>    > +	dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);  
> >>> +
> >>> +	rval = device_add(&nvmem->dev);
> >>> +	if (rval)
> >>> +		goto err_remove_cells;  
> >>
> >> All the error handling paths are now messed up with this patch, put_device() in error path will be called incorrectly from multiple places.  
> > 
> > I'm not sure what this means. Perhaps I should additionally call
> > device_del() after device_add was successful to mimic the
> > device_unregister() call from the remove path. Is that what you mean?  
> 
> 
> This looks perfectly fine, no change required. This also fixes a bug of missing device_del() in err path.
> 
> pl, Ignore my old comments.

nvmem_register() calls device_initialize() and later device_add(),
which is exactly the content of device_register(). Upon error
after device_add(), we currently call device_put(), whereas
device_unregister would call both device_del() and device_put().

I would expect device_del() to be first called upon error before
device_put() *after* device_add() has succeded, no?

> > I also see the layout_np below should be freed before jumping in the
> > error section.  
> 
> you mean missing of_node_put()?

Yes, I need to call of_node_put() before jumping into the error path.

Thanks,
Miquèl
  
Srinivas Kandagatla Aug. 11, 2023, 12:26 p.m. UTC | #3
On 11/08/2023 13:11, Miquel Raynal wrote:

>>
>>
> 
> nvmem_register() calls device_initialize() and later device_add(),
> which is exactly the content of device_register(). Upon error
> after device_add(), we currently call device_put(), whereas
> device_unregister would call both device_del() and device_put().
> 
> I would expect device_del() to be first called upon error before
> device_put() *after* device_add() has succeded, no?

That is correct afaiu, if device_add is succeed we need to call 
device_del(). As the patch now moved the device_add to end of function 
we really do not need device_del() in err path.

> 
>>> I also see the layout_np below should be freed before jumping in the
>>> error section.
>>
>> you mean missing of_node_put()?
> 
> Yes, I need to call of_node_put() before jumping into the error path.

Are we not already doing it in nvmem_layout_get() and 
nvmem_add_cells_from_fixed_layout() ?


> 
> Thanks,
> Miquèl
  
Miquel Raynal Aug. 11, 2023, 12:38 p.m. UTC | #4
Hi Srinivas,

srinivas.kandagatla@linaro.org wrote on Fri, 11 Aug 2023 13:26:24 +0100:

> On 11/08/2023 13:11, Miquel Raynal wrote:
> 
> >>
> >>  
> > 
> > nvmem_register() calls device_initialize() and later device_add(),
> > which is exactly the content of device_register(). Upon error
> > after device_add(), we currently call device_put(), whereas
> > device_unregister would call both device_del() and device_put().
> > 
> > I would expect device_del() to be first called upon error before
> > device_put() *after* device_add() has succeded, no?  
> 
> That is correct afaiu, if device_add is succeed we need to call device_del(). As the patch now moved the device_add to end of function we really do not need device_del() in err path.

Right, I'm looking at the end of the series where I need to add
device_del() in the error path because something gets added after
device_add(). So we are aligned, thanks for the feedback.

> >>> I also see the layout_np below should be freed before jumping in the
> >>> error section.  
> >>
> >> you mean missing of_node_put()?  
> > 
> > Yes, I need to call of_node_put() before jumping into the error path.  
> 
> Are we not already doing it in nvmem_layout_get() and nvmem_add_cells_from_fixed_layout() ?

We perform the layout_get for two reasons:
- knowing if there is a layout
- using the layout
Here we are in the first case, and we don't want to retain a reference
from here. Only in the second case.

Thanks,
Miquèl
  

Patch

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 3f8c7718412b..48659106a1e2 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -998,12 +998,6 @@  struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	if (rval)
 		goto err_remove_cells;
 
-	dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
-
-	rval = device_add(&nvmem->dev);
-	if (rval)
-		goto err_remove_cells;
-
 	rval = nvmem_add_cells_from_fixed_layout(nvmem);
 	if (rval)
 		goto err_remove_cells;
@@ -1012,6 +1006,12 @@  struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	if (rval)
 		goto err_remove_cells;
 
+	dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
+
+	rval = device_add(&nvmem->dev);
+	if (rval)
+		goto err_remove_cells;
+
 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
 
 	return nvmem;