[v3,06/18] nvmem: core: drop the removal of the cells in nvmem_add_cells()

Message ID 20221118185118.1190044-7-michael@walle.cc
State New
Headers
Series nvmem: core: introduce NVMEM layouts |

Commit Message

Michael Walle Nov. 18, 2022, 6:51 p.m. UTC
  If nvmem_add_cells() fails, the whole nvmem_register() will fail
and the cells will then be removed anyway. This is a prepartion
to introduce a nvmem_add_one_cell() which can then be used by
nvmem_add_cells().

This is then the same to what nvmem_add_cells_from_table() and
nvmem_add_cells_from_of() do.

Signed-off-by: Michael Walle <michael@walle.cc>
---
changes since v2:
 - none

changes since v1:
 - none

 drivers/nvmem/core.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
  

Comments

Sascha Hauer Nov. 21, 2022, 7:56 a.m. UTC | #1
On Fri, Nov 18, 2022 at 07:51:06PM +0100, Michael Walle wrote:
> If nvmem_add_cells() fails, the whole nvmem_register() will fail
> and the cells will then be removed anyway. This is a prepartion

s/prepartion/preparation/

Sascha
  

Patch

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 24573e63e5a9..7c76e0e0072e 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -515,7 +515,7 @@  static int nvmem_add_cells(struct nvmem_device *nvmem,
 		    int ncells)
 {
 	struct nvmem_cell_entry **cells;
-	int i, rval;
+	int i, rval = 0;
 
 	cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
 	if (!cells)
@@ -525,28 +525,22 @@  static int nvmem_add_cells(struct nvmem_device *nvmem,
 		cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
 		if (!cells[i]) {
 			rval = -ENOMEM;
-			goto err;
+			goto out;
 		}
 
 		rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, &info[i], cells[i]);
 		if (rval) {
 			kfree(cells[i]);
-			goto err;
+			goto out;
 		}
 
 		nvmem_cell_entry_add(cells[i]);
 	}
 
+out:
 	/* remove tmp array */
 	kfree(cells);
 
-	return 0;
-err:
-	while (i--)
-		nvmem_cell_entry_drop(cells[i]);
-
-	kfree(cells);
-
 	return rval;
 }