linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 00/17] Clean nandsim error path
@ 2020-05-09 19:14 Miquel Raynal
  2020-05-09 19:14 ` [PATCH 01/17] mtd: rawnand: nandsim: Consistent use of 'ns' instead of 'dev' Miquel Raynal
                   ` (16 more replies)
  0 siblings, 17 replies; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

Hello,

As part of a bigger cleanup I realized the error path of nandsim.c was
horribly wrong. There are a few additional changes, like having a
consistent naming for a given object, but moreover this is an error
path cleanup, driver-wide.

Cheers,
Miquèl

Miquel Raynal (17):
  mtd: rawnand: nandsim: Consistent use of 'ns' instead of 'dev'
  mtd: rawnand: nandsim: Use octal permissions
  mtd: rawnand: nandsim: Use a consistent ns_ prefix for all functions
  mtd: rawnand: nandsim: Clean error handling
  mtd: rawnand: nandsim: Keep track of the created debugfs entries
  mtd: rawnand: nandsim: Remove debugfs entries at unload time
  mtd: rawnand: nandsim: Fix the two ns_alloc_device() error paths
  mtd: rawnand: nandsim: Free partition names on error in ns_init()
  mtd: rawnand: nandsim: Free the allocated device on error in ns_init()
  mtd: rawnand: nandsim: Free the partition names in ns_free()
  mtd: rawnand: nandsim: Stop using nand_release()
  mtd: rawnand: nandsim: Use an additional label when freeing the
    nandsim object
  mtd: rawnand: nandsim: Free erase_block_wear on error
  mtd: rawnand: nandsim: Fix the label pointing on nand_cleanup()
  mtd: rawnand: nandsim: Manage lists on error in ns_init_module()
  mtd: rawnand: nandsim: Rename a label in ns_init_module()
  mtd: rawnand: nandsim: Reorganize ns_cleanup_module()

 drivers/mtd/nand/raw/nandsim.c | 431 +++++++++++++++++++--------------
 1 file changed, 248 insertions(+), 183 deletions(-)

-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 01/17] mtd: rawnand: nandsim: Consistent use of 'ns' instead of 'dev'
  2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
@ 2020-05-09 19:14 ` Miquel Raynal
  2020-05-09 19:14 ` [PATCH 02/17] mtd: rawnand: nandsim: Use octal permissions Miquel Raynal
                   ` (15 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

The nandsim object is called 'ns' almost everywhere, keep it that way
everywhere for consistency.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/raw/nandsim.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index 23cda67a3f53..0062e4fedcc0 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -487,12 +487,12 @@ DEFINE_SHOW_ATTRIBUTE(nandsim);
 
 /**
  * nandsim_debugfs_create - initialize debugfs
- * @dev: nandsim device description object
+ * @ns: nandsim device description object
  *
  * This function creates all debugfs files for UBI device @ubi. Returns zero in
  * case of success and a negative error code in case of failure.
  */
-static int nandsim_debugfs_create(struct nandsim *dev)
+static int nandsim_debugfs_create(struct nandsim *ns)
 {
 	struct dentry *root = nsmtd->dbg.dfs_dir;
 	struct dentry *dent;
@@ -508,8 +508,8 @@ static int nandsim_debugfs_create(struct nandsim *dev)
 		return 0;
 	}
 
-	dent = debugfs_create_file("nandsim_wear_report", S_IRUSR,
-				   root, dev, &nandsim_fops);
+	dent = debugfs_create_file("nandsim_wear_report", S_IRUSR, root, ns,
+				   &nandsim_fops);
 	if (IS_ERR_OR_NULL(dent)) {
 		NS_ERR("cannot create \"nandsim_wear_report\" debugfs entry\n");
 		return -1;
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 02/17] mtd: rawnand: nandsim: Use octal permissions
  2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
  2020-05-09 19:14 ` [PATCH 01/17] mtd: rawnand: nandsim: Consistent use of 'ns' instead of 'dev' Miquel Raynal
@ 2020-05-09 19:14 ` Miquel Raynal
  2020-05-09 19:14 ` [PATCH 03/17] mtd: rawnand: nandsim: Use a consistent ns_ prefix for all functions Miquel Raynal
                   ` (14 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

Symbolic permissions 'S_IRUSR' are not preferred. Checkpatch.pl
advises to use octal permissions '0400'.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/raw/nandsim.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index 0062e4fedcc0..ea46f7011a0f 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -508,7 +508,7 @@ static int nandsim_debugfs_create(struct nandsim *ns)
 		return 0;
 	}
 
-	dent = debugfs_create_file("nandsim_wear_report", S_IRUSR, root, ns,
+	dent = debugfs_create_file("nandsim_wear_report", 0400, root, ns,
 				   &nandsim_fops);
 	if (IS_ERR_OR_NULL(dent)) {
 		NS_ERR("cannot create \"nandsim_wear_report\" debugfs entry\n");
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 03/17] mtd: rawnand: nandsim: Use a consistent ns_ prefix for all functions
  2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
  2020-05-09 19:14 ` [PATCH 01/17] mtd: rawnand: nandsim: Consistent use of 'ns' instead of 'dev' Miquel Raynal
  2020-05-09 19:14 ` [PATCH 02/17] mtd: rawnand: nandsim: Use octal permissions Miquel Raynal
@ 2020-05-09 19:14 ` Miquel Raynal
  2020-05-09 19:14 ` [PATCH 04/17] mtd: rawnand: nandsim: Clean error handling Miquel Raynal
                   ` (13 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

Some functions are prefixed "nandsim_", others "ns_" and many are
simply not prefixed at all. Make this file consistent by prefixing all
functions by "ns_".

This is a mechanical change. Sometimes the line is a bit reworked as
well to fit the kernel coding style. For instance, there are several
places where displayed strings are cut. When one of this line is
changed because of the naming update, the two parts of the strings
gets concatenated.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/raw/nandsim.c | 248 +++++++++++++++++----------------
 1 file changed, 131 insertions(+), 117 deletions(-)

diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index ea46f7011a0f..2c335cc8bcdf 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -432,7 +432,7 @@ static unsigned long total_wear = 0;
 /* MTD structure for NAND controller */
 static struct mtd_info *nsmtd;
 
-static int nandsim_show(struct seq_file *m, void *private)
+static int ns_show(struct seq_file *m, void *private)
 {
 	unsigned long wmin = -1, wmax = 0, avg;
 	unsigned long deciles[10], decile_max[10], tot = 0;
@@ -483,16 +483,16 @@ static int nandsim_show(struct seq_file *m, void *private)
 
 	return 0;
 }
-DEFINE_SHOW_ATTRIBUTE(nandsim);
+DEFINE_SHOW_ATTRIBUTE(ns);
 
 /**
- * nandsim_debugfs_create - initialize debugfs
+ * ns_debugfs_create - initialize debugfs
  * @ns: nandsim device description object
  *
  * This function creates all debugfs files for UBI device @ubi. Returns zero in
  * case of success and a negative error code in case of failure.
  */
-static int nandsim_debugfs_create(struct nandsim *ns)
+static int ns_debugfs_create(struct nandsim *ns)
 {
 	struct dentry *root = nsmtd->dbg.dfs_dir;
 	struct dentry *dent;
@@ -509,7 +509,7 @@ static int nandsim_debugfs_create(struct nandsim *ns)
 	}
 
 	dent = debugfs_create_file("nandsim_wear_report", 0400, root, ns,
-				   &nandsim_fops);
+				   &ns_fops);
 	if (IS_ERR_OR_NULL(dent)) {
 		NS_ERR("cannot create \"nandsim_wear_report\" debugfs entry\n");
 		return -1;
@@ -524,7 +524,7 @@ static int nandsim_debugfs_create(struct nandsim *ns)
  *
  * RETURNS: 0 if success, -ENOMEM if memory alloc fails.
  */
-static int __init alloc_device(struct nandsim *ns)
+static int __init ns_alloc_device(struct nandsim *ns)
 {
 	struct file *cfile;
 	int i, err;
@@ -588,7 +588,7 @@ static int __init alloc_device(struct nandsim *ns)
 /*
  * Free any allocated pages, and free the array of page pointers.
  */
-static void free_device(struct nandsim *ns)
+static void ns_free_device(struct nandsim *ns)
 {
 	int i;
 
@@ -610,7 +610,7 @@ static void free_device(struct nandsim *ns)
 	}
 }
 
-static char __init *get_partition_name(int i)
+static char __init *ns_get_partition_name(int i)
 {
 	return kasprintf(GFP_KERNEL, "NAND simulator partition %d", i);
 }
@@ -620,7 +620,7 @@ static char __init *get_partition_name(int i)
  *
  * RETURNS: 0 if success, -ERRNO if failure.
  */
-static int __init init_nandsim(struct mtd_info *mtd)
+static int __init ns_init(struct mtd_info *mtd)
 {
 	struct nand_chip *chip = mtd_to_nand(mtd);
 	struct nandsim   *ns   = nand_get_controller_data(chip);
@@ -693,7 +693,7 @@ static int __init init_nandsim(struct mtd_info *mtd)
 			NS_ERR("bad partition size.\n");
 			return -EINVAL;
 		}
-		ns->partitions[i].name   = get_partition_name(i);
+		ns->partitions[i].name = ns_get_partition_name(i);
 		if (!ns->partitions[i].name) {
 			NS_ERR("unable to allocate memory.\n");
 			return -ENOMEM;
@@ -709,7 +709,7 @@ static int __init init_nandsim(struct mtd_info *mtd)
 			NS_ERR("too many partitions.\n");
 			return -EINVAL;
 		}
-		ns->partitions[i].name   = get_partition_name(i);
+		ns->partitions[i].name = ns_get_partition_name(i);
 		if (!ns->partitions[i].name) {
 			NS_ERR("unable to allocate memory.\n");
 			return -ENOMEM;
@@ -739,7 +739,7 @@ static int __init init_nandsim(struct mtd_info *mtd)
 	printk("sector address bytes: %u\n",    ns->geom.secaddrbytes);
 	printk("options: %#x\n",                ns->options);
 
-	if ((ret = alloc_device(ns)) != 0)
+	if ((ret = ns_alloc_device(ns)) != 0)
 		return ret;
 
 	/* Allocate / initialize the internal buffer */
@@ -757,15 +757,15 @@ static int __init init_nandsim(struct mtd_info *mtd)
 /*
  * Free the nandsim structure.
  */
-static void free_nandsim(struct nandsim *ns)
+static void ns_free(struct nandsim *ns)
 {
 	kfree(ns->buf.byte);
-	free_device(ns);
+	ns_free_device(ns);
 
 	return;
 }
 
-static int parse_badblocks(struct nandsim *ns, struct mtd_info *mtd)
+static int ns_parse_badblocks(struct nandsim *ns, struct mtd_info *mtd)
 {
 	char *w;
 	int zero_ok;
@@ -793,7 +793,7 @@ static int parse_badblocks(struct nandsim *ns, struct mtd_info *mtd)
 	return 0;
 }
 
-static int parse_weakblocks(void)
+static int ns_parse_weakblocks(void)
 {
 	char *w;
 	int zero_ok;
@@ -830,7 +830,7 @@ static int parse_weakblocks(void)
 	return 0;
 }
 
-static int erase_error(unsigned int erase_block_no)
+static int ns_erase_error(unsigned int erase_block_no)
 {
 	struct weak_block *wb;
 
@@ -844,7 +844,7 @@ static int erase_error(unsigned int erase_block_no)
 	return 0;
 }
 
-static int parse_weakpages(void)
+static int ns_parse_weakpages(void)
 {
 	char *w;
 	int zero_ok;
@@ -881,7 +881,7 @@ static int parse_weakpages(void)
 	return 0;
 }
 
-static int write_error(unsigned int page_no)
+static int ns_write_error(unsigned int page_no)
 {
 	struct weak_page *wp;
 
@@ -895,7 +895,7 @@ static int write_error(unsigned int page_no)
 	return 0;
 }
 
-static int parse_gravepages(void)
+static int ns_parse_gravepages(void)
 {
 	char *g;
 	int zero_ok;
@@ -932,7 +932,7 @@ static int parse_gravepages(void)
 	return 0;
 }
 
-static int read_error(unsigned int page_no)
+static int ns_read_error(unsigned int page_no)
 {
 	struct grave_page *gp;
 
@@ -946,7 +946,7 @@ static int read_error(unsigned int page_no)
 	return 0;
 }
 
-static void free_lists(void)
+static void ns_free_lists(void)
 {
 	struct list_head *pos, *n;
 	list_for_each_safe(pos, n, &weak_blocks) {
@@ -964,7 +964,7 @@ static void free_lists(void)
 	kfree(erase_block_wear);
 }
 
-static int setup_wear_reporting(struct mtd_info *mtd)
+static int ns_setup_wear_reporting(struct mtd_info *mtd)
 {
 	size_t mem;
 
@@ -982,7 +982,7 @@ static int setup_wear_reporting(struct mtd_info *mtd)
 	return 0;
 }
 
-static void update_wear(unsigned int erase_block_no)
+static void ns_update_wear(unsigned int erase_block_no)
 {
 	if (!erase_block_wear)
 		return;
@@ -1001,7 +1001,7 @@ static void update_wear(unsigned int erase_block_no)
 /*
  * Returns the string representation of 'state' state.
  */
-static char *get_state_name(uint32_t state)
+static char *ns_get_state_name(uint32_t state)
 {
 	switch (NS_STATE(state)) {
 		case STATE_CMD_READ0:
@@ -1061,7 +1061,7 @@ static char *get_state_name(uint32_t state)
  *
  * RETURNS: 1 if wrong command, 0 if right.
  */
-static int check_command(int cmd)
+static int ns_check_command(int cmd)
 {
 	switch (cmd) {
 
@@ -1088,7 +1088,7 @@ static int check_command(int cmd)
 /*
  * Returns state after command is accepted by command number.
  */
-static uint32_t get_state_by_command(unsigned command)
+static uint32_t ns_get_state_by_command(unsigned command)
 {
 	switch (command) {
 		case NAND_CMD_READ0:
@@ -1126,7 +1126,7 @@ static uint32_t get_state_by_command(unsigned command)
 /*
  * Move an address byte to the correspondent internal register.
  */
-static inline void accept_addr_byte(struct nandsim *ns, u_char bt)
+static inline void ns_accept_addr_byte(struct nandsim *ns, u_char bt)
 {
 	uint byte = (uint)bt;
 
@@ -1144,9 +1144,10 @@ static inline void accept_addr_byte(struct nandsim *ns, u_char bt)
 /*
  * Switch to STATE_READY state.
  */
-static inline void switch_to_ready_state(struct nandsim *ns, u_char status)
+static inline void ns_switch_to_ready_state(struct nandsim *ns, u_char status)
 {
-	NS_DBG("switch_to_ready_state: switch to %s state\n", get_state_name(STATE_READY));
+	NS_DBG("switch_to_ready_state: switch to %s state\n",
+	       ns_get_state_name(STATE_READY));
 
 	ns->state       = STATE_READY;
 	ns->nxstate     = STATE_UNKNOWN;
@@ -1203,7 +1204,7 @@ static inline void switch_to_ready_state(struct nandsim *ns, u_char status)
  *          -1 - several matches.
  *           0 - operation is found.
  */
-static int find_operation(struct nandsim *ns, uint32_t flag)
+static int ns_find_operation(struct nandsim *ns, uint32_t flag)
 {
 	int opsfound = 0;
 	int i, j, idx = 0;
@@ -1256,7 +1257,8 @@ static int find_operation(struct nandsim *ns, uint32_t flag)
 		ns->state = ns->op[ns->stateidx];
 		ns->nxstate = ns->op[ns->stateidx + 1];
 		NS_DBG("find_operation: operation found, index: %d, state: %s, nxstate %s\n",
-				idx, get_state_name(ns->state), get_state_name(ns->nxstate));
+		       idx, ns_get_state_name(ns->state),
+		       ns_get_state_name(ns->nxstate));
 		return 0;
 	}
 
@@ -1264,13 +1266,13 @@ static int find_operation(struct nandsim *ns, uint32_t flag)
 		/* Nothing was found. Try to ignore previous commands (if any) and search again */
 		if (ns->npstates != 0) {
 			NS_DBG("find_operation: no operation found, try again with state %s\n",
-					get_state_name(ns->state));
+			       ns_get_state_name(ns->state));
 			ns->npstates = 0;
-			return find_operation(ns, 0);
+			return ns_find_operation(ns, 0);
 
 		}
 		NS_DBG("find_operation: no operations found\n");
-		switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
+		ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
 		return -2;
 	}
 
@@ -1287,7 +1289,7 @@ static int find_operation(struct nandsim *ns, uint32_t flag)
 	return -1;
 }
 
-static void put_pages(struct nandsim *ns)
+static void ns_put_pages(struct nandsim *ns)
 {
 	int i;
 
@@ -1296,7 +1298,8 @@ static void put_pages(struct nandsim *ns)
 }
 
 /* Get page cache pages in advance to provide NOFS memory allocation */
-static int get_pages(struct nandsim *ns, struct file *file, size_t count, loff_t pos)
+static int ns_get_pages(struct nandsim *ns, struct file *file, size_t count,
+			loff_t pos)
 {
 	pgoff_t index, start_index, end_index;
 	struct page *page;
@@ -1316,7 +1319,7 @@ static int get_pages(struct nandsim *ns, struct file *file, size_t count, loff_t
 				page = find_or_create_page(mapping, index, GFP_NOFS);
 			}
 			if (page == NULL) {
-				put_pages(ns);
+				ns_put_pages(ns);
 				return -ENOMEM;
 			}
 			unlock_page(page);
@@ -1326,35 +1329,37 @@ static int get_pages(struct nandsim *ns, struct file *file, size_t count, loff_t
 	return 0;
 }
 
-static ssize_t read_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t pos)
+static ssize_t ns_read_file(struct nandsim *ns, struct file *file, void *buf,
+			    size_t count, loff_t pos)
 {
 	ssize_t tx;
 	int err;
 	unsigned int noreclaim_flag;
 
-	err = get_pages(ns, file, count, pos);
+	err = ns_get_pages(ns, file, count, pos);
 	if (err)
 		return err;
 	noreclaim_flag = memalloc_noreclaim_save();
 	tx = kernel_read(file, buf, count, &pos);
 	memalloc_noreclaim_restore(noreclaim_flag);
-	put_pages(ns);
+	ns_put_pages(ns);
 	return tx;
 }
 
-static ssize_t write_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t pos)
+static ssize_t ns_write_file(struct nandsim *ns, struct file *file, void *buf,
+			     size_t count, loff_t pos)
 {
 	ssize_t tx;
 	int err;
 	unsigned int noreclaim_flag;
 
-	err = get_pages(ns, file, count, pos);
+	err = ns_get_pages(ns, file, count, pos);
 	if (err)
 		return err;
 	noreclaim_flag = memalloc_noreclaim_save();
 	tx = kernel_write(file, buf, count, &pos);
 	memalloc_noreclaim_restore(noreclaim_flag);
-	put_pages(ns);
+	ns_put_pages(ns);
 	return tx;
 }
 
@@ -1374,11 +1379,11 @@ static inline u_char *NS_PAGE_BYTE_OFF(struct nandsim *ns)
 	return NS_GET_PAGE(ns)->byte + ns->regs.column + ns->regs.off;
 }
 
-static int do_read_error(struct nandsim *ns, int num)
+static int ns_do_read_error(struct nandsim *ns, int num)
 {
 	unsigned int page_no = ns->regs.row;
 
-	if (read_error(page_no)) {
+	if (ns_read_error(page_no)) {
 		prandom_bytes(ns->buf.byte, num);
 		NS_WARN("simulating read error in page %u\n", page_no);
 		return 1;
@@ -1386,7 +1391,7 @@ static int do_read_error(struct nandsim *ns, int num)
 	return 0;
 }
 
-static void do_bit_flips(struct nandsim *ns, int num)
+static void ns_do_bit_flips(struct nandsim *ns, int num)
 {
 	if (bitflips && prandom_u32() < (1 << 22)) {
 		int flips = 1;
@@ -1406,7 +1411,7 @@ static void do_bit_flips(struct nandsim *ns, int num)
 /*
  * Fill the NAND buffer with data read from the specified page.
  */
-static void read_page(struct nandsim *ns, int num)
+static void ns_read_page(struct nandsim *ns, int num)
 {
 	union ns_mem *mypage;
 
@@ -1420,15 +1425,16 @@ static void read_page(struct nandsim *ns, int num)
 
 			NS_DBG("read_page: page %d written, reading from %d\n",
 				ns->regs.row, ns->regs.column + ns->regs.off);
-			if (do_read_error(ns, num))
+			if (ns_do_read_error(ns, num))
 				return;
 			pos = (loff_t)NS_RAW_OFFSET(ns) + ns->regs.off;
-			tx = read_file(ns, ns->cfile, ns->buf.byte, num, pos);
+			tx = ns_read_file(ns, ns->cfile, ns->buf.byte, num,
+					  pos);
 			if (tx != num) {
 				NS_ERR("read_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
 				return;
 			}
-			do_bit_flips(ns, num);
+			ns_do_bit_flips(ns, num);
 		}
 		return;
 	}
@@ -1440,17 +1446,17 @@ static void read_page(struct nandsim *ns, int num)
 	} else {
 		NS_DBG("read_page: page %d allocated, reading from %d\n",
 			ns->regs.row, ns->regs.column + ns->regs.off);
-		if (do_read_error(ns, num))
+		if (ns_do_read_error(ns, num))
 			return;
 		memcpy(ns->buf.byte, NS_PAGE_BYTE_OFF(ns), num);
-		do_bit_flips(ns, num);
+		ns_do_bit_flips(ns, num);
 	}
 }
 
 /*
  * Erase all pages in the specified sector.
  */
-static void erase_sector(struct nandsim *ns)
+static void ns_erase_sector(struct nandsim *ns)
 {
 	union ns_mem *mypage;
 	int i;
@@ -1478,7 +1484,7 @@ static void erase_sector(struct nandsim *ns)
 /*
  * Program the specified page with the contents from the NAND buffer.
  */
-static int prog_page(struct nandsim *ns, int num)
+static int ns_prog_page(struct nandsim *ns, int num)
 {
 	int i;
 	union ns_mem *mypage;
@@ -1497,7 +1503,7 @@ static int prog_page(struct nandsim *ns, int num)
 			memset(ns->file_buf, 0xff, ns->geom.pgszoob);
 		} else {
 			all = 0;
-			tx = read_file(ns, ns->cfile, pg_off, num, off);
+			tx = ns_read_file(ns, ns->cfile, pg_off, num, off);
 			if (tx != num) {
 				NS_ERR("prog_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
 				return -1;
@@ -1507,14 +1513,15 @@ static int prog_page(struct nandsim *ns, int num)
 			pg_off[i] &= ns->buf.byte[i];
 		if (all) {
 			loff_t pos = (loff_t)ns->regs.row * ns->geom.pgszoob;
-			tx = write_file(ns, ns->cfile, ns->file_buf, ns->geom.pgszoob, pos);
+			tx = ns_write_file(ns, ns->cfile, ns->file_buf,
+					   ns->geom.pgszoob, pos);
 			if (tx != ns->geom.pgszoob) {
 				NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
 				return -1;
 			}
 			__set_bit(ns->regs.row, ns->pages_written);
 		} else {
-			tx = write_file(ns, ns->cfile, pg_off, num, off);
+			tx = ns_write_file(ns, ns->cfile, pg_off, num, off);
 			if (tx != num) {
 				NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
 				return -1;
@@ -1552,7 +1559,7 @@ static int prog_page(struct nandsim *ns, int num)
  *
  * RETURNS: 0 if success, -1 if error.
  */
-static int do_state_action(struct nandsim *ns, uint32_t action)
+static int ns_do_state_action(struct nandsim *ns, uint32_t action)
 {
 	int num;
 	int busdiv = ns->busw == 8 ? 1 : 2;
@@ -1579,7 +1586,7 @@ static int do_state_action(struct nandsim *ns, uint32_t action)
 			break;
 		}
 		num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
-		read_page(ns, num);
+		ns_read_page(ns, num);
 
 		NS_DBG("do_state_action: (ACTION_CPY:) copy %d bytes to int buf, raw offset %d\n",
 			num, NS_RAW_OFFSET(ns) + ns->regs.off);
@@ -1622,14 +1629,14 @@ static int do_state_action(struct nandsim *ns, uint32_t action)
 				ns->regs.row, NS_RAW_OFFSET(ns));
 		NS_LOG("erase sector %u\n", erase_block_no);
 
-		erase_sector(ns);
+		ns_erase_sector(ns);
 
 		NS_MDELAY(erase_delay);
 
 		if (erase_block_wear)
-			update_wear(erase_block_no);
+			ns_update_wear(erase_block_no);
 
-		if (erase_error(erase_block_no)) {
+		if (ns_erase_error(erase_block_no)) {
 			NS_WARN("simulating erase failure in erase block %u\n", erase_block_no);
 			return -1;
 		}
@@ -1653,7 +1660,7 @@ static int do_state_action(struct nandsim *ns, uint32_t action)
 			return -1;
 		}
 
-		if (prog_page(ns, num) == -1)
+		if (ns_prog_page(ns, num) == -1)
 			return -1;
 
 		page_no = ns->regs.row;
@@ -1665,7 +1672,7 @@ static int do_state_action(struct nandsim *ns, uint32_t action)
 		NS_UDELAY(programm_delay);
 		NS_UDELAY(output_cycle * ns->geom.pgsz / 1000 / busdiv);
 
-		if (write_error(page_no)) {
+		if (ns_write_error(page_no)) {
 			NS_WARN("simulating write failure in page %u\n", page_no);
 			return -1;
 		}
@@ -1702,7 +1709,7 @@ static int do_state_action(struct nandsim *ns, uint32_t action)
 /*
  * Switch simulator's state.
  */
-static void switch_state(struct nandsim *ns)
+static void ns_switch_state(struct nandsim *ns)
 {
 	if (ns->op) {
 		/*
@@ -1716,11 +1723,13 @@ static void switch_state(struct nandsim *ns)
 
 		NS_DBG("switch_state: operation is known, switch to the next state, "
 			"state: %s, nxstate: %s\n",
-			get_state_name(ns->state), get_state_name(ns->nxstate));
+		       ns_get_state_name(ns->state),
+		       ns_get_state_name(ns->nxstate));
 
 		/* See, whether we need to do some action */
-		if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
-			switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
+		if ((ns->state & ACTION_MASK) &&
+		    ns_do_state_action(ns, ns->state) < 0) {
+			ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
 			return;
 		}
 
@@ -1734,15 +1743,16 @@ static void switch_state(struct nandsim *ns)
 		 *  The only event causing the switch_state function to
 		 *  be called with yet unknown operation is new command.
 		 */
-		ns->state = get_state_by_command(ns->regs.command);
+		ns->state = ns_get_state_by_command(ns->regs.command);
 
 		NS_DBG("switch_state: operation is unknown, try to find it\n");
 
-		if (find_operation(ns, 0) != 0)
+		if (ns_find_operation(ns, 0) != 0)
 			return;
 
-		if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
-			switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
+		if ((ns->state & ACTION_MASK) &&
+		    ns_do_state_action(ns, ns->state) < 0) {
+			ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
 			return;
 		}
 	}
@@ -1770,7 +1780,7 @@ static void switch_state(struct nandsim *ns)
 
 		NS_DBG("switch_state: operation complete, switch to STATE_READY state\n");
 
-		switch_to_ready_state(ns, status);
+		ns_switch_to_ready_state(ns, status);
 
 		return;
 	} else if (ns->nxstate & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK)) {
@@ -1784,7 +1794,8 @@ static void switch_state(struct nandsim *ns)
 
 		NS_DBG("switch_state: the next state is data I/O, switch, "
 			"state: %s, nxstate: %s\n",
-			get_state_name(ns->state), get_state_name(ns->nxstate));
+		       ns_get_state_name(ns->state),
+		       ns_get_state_name(ns->nxstate));
 
 		/*
 		 * Set the internal register to the count of bytes which
@@ -1862,8 +1873,8 @@ static u_char ns_nand_read_byte(struct nand_chip *chip)
 		return outb;
 	}
 	if (!(ns->state & STATE_DATAOUT_MASK)) {
-		NS_WARN("read_byte: unexpected data output cycle, state is %s "
-			"return %#x\n", get_state_name(ns->state), (uint)outb);
+		NS_WARN("read_byte: unexpected data output cycle, state is %s return %#x\n",
+			ns_get_state_name(ns->state), (uint)outb);
 		return outb;
 	}
 
@@ -1902,7 +1913,7 @@ static u_char ns_nand_read_byte(struct nand_chip *chip)
 		NS_DBG("read_byte: all bytes were read\n");
 
 		if (NS_STATE(ns->nxstate) == STATE_READY)
-			switch_state(ns);
+			ns_switch_state(ns);
 	}
 
 	return outb;
@@ -1929,12 +1940,12 @@ static void ns_nand_write_byte(struct nand_chip *chip, u_char byte)
 
 		if (byte == NAND_CMD_RESET) {
 			NS_LOG("reset chip\n");
-			switch_to_ready_state(ns, NS_STATUS_OK(ns));
+			ns_switch_to_ready_state(ns, NS_STATUS_OK(ns));
 			return;
 		}
 
 		/* Check that the command byte is correct */
-		if (check_command(byte)) {
+		if (ns_check_command(byte)) {
 			NS_ERR("write_byte: unknown command %#x\n", (uint)byte);
 			return;
 		}
@@ -1943,7 +1954,7 @@ static void ns_nand_write_byte(struct nand_chip *chip, u_char byte)
 			|| NS_STATE(ns->state) == STATE_DATAOUT) {
 			int row = ns->regs.row;
 
-			switch_state(ns);
+			ns_switch_state(ns);
 			if (byte == NAND_CMD_RNDOUT)
 				ns->regs.row = row;
 		}
@@ -1958,16 +1969,17 @@ static void ns_nand_write_byte(struct nand_chip *chip, u_char byte)
 				 * was expected but command was input. In this case ignore
 				 * previous command(s)/state(s) and accept the last one.
 				 */
-				NS_WARN("write_byte: command (%#x) wasn't expected, expected state is %s, "
-					"ignore previous states\n", (uint)byte, get_state_name(ns->nxstate));
+				NS_WARN("write_byte: command (%#x) wasn't expected, expected state is %s, ignore previous states\n",
+					(uint)byte,
+					ns_get_state_name(ns->nxstate));
 			}
-			switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
+			ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
 		}
 
 		NS_DBG("command byte corresponding to %s state accepted\n",
-			get_state_name(get_state_by_command(byte)));
+			ns_get_state_name(ns_get_state_by_command(byte)));
 		ns->regs.command = byte;
-		switch_state(ns);
+		ns_switch_state(ns);
 
 	} else if (ns->lines.ale == 1) {
 		/*
@@ -1978,11 +1990,13 @@ static void ns_nand_write_byte(struct nand_chip *chip, u_char byte)
 
 			NS_DBG("write_byte: operation isn't known yet, identify it\n");
 
-			if (find_operation(ns, 1) < 0)
+			if (ns_find_operation(ns, 1) < 0)
 				return;
 
-			if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
-				switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
+			if ((ns->state & ACTION_MASK) &&
+			    ns_do_state_action(ns, ns->state) < 0) {
+				ns_switch_to_ready_state(ns,
+							 NS_STATUS_FAILED(ns));
 				return;
 			}
 
@@ -2004,20 +2018,20 @@ static void ns_nand_write_byte(struct nand_chip *chip, u_char byte)
 
 		/* Check that chip is expecting address */
 		if (!(ns->nxstate & STATE_ADDR_MASK)) {
-			NS_ERR("write_byte: address (%#x) isn't expected, expected state is %s, "
-				"switch to STATE_READY\n", (uint)byte, get_state_name(ns->nxstate));
-			switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
+			NS_ERR("write_byte: address (%#x) isn't expected, expected state is %s, switch to STATE_READY\n",
+			       (uint)byte, ns_get_state_name(ns->nxstate));
+			ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
 			return;
 		}
 
 		/* Check if this is expected byte */
 		if (ns->regs.count == ns->regs.num) {
 			NS_ERR("write_byte: no more address bytes expected\n");
-			switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
+			ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
 			return;
 		}
 
-		accept_addr_byte(ns, byte);
+		ns_accept_addr_byte(ns, byte);
 
 		ns->regs.count += 1;
 
@@ -2026,7 +2040,7 @@ static void ns_nand_write_byte(struct nand_chip *chip, u_char byte)
 
 		if (ns->regs.count == ns->regs.num) {
 			NS_DBG("address (%#x, %#x) is accepted\n", ns->regs.row, ns->regs.column);
-			switch_state(ns);
+			ns_switch_state(ns);
 		}
 
 	} else {
@@ -2036,10 +2050,10 @@ static void ns_nand_write_byte(struct nand_chip *chip, u_char byte)
 
 		/* Check that chip is expecting data input */
 		if (!(ns->state & STATE_DATAIN_MASK)) {
-			NS_ERR("write_byte: data input (%#x) isn't expected, state is %s, "
-				"switch to %s\n", (uint)byte,
-				get_state_name(ns->state), get_state_name(STATE_READY));
-			switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
+			NS_ERR("write_byte: data input (%#x) isn't expected, state is %s, switch to %s\n",
+			       (uint)byte, ns_get_state_name(ns->state),
+			       ns_get_state_name(STATE_READY));
+			ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
 			return;
 		}
 
@@ -2069,16 +2083,16 @@ static void ns_nand_write_buf(struct nand_chip *chip, const u_char *buf,
 
 	/* Check that chip is expecting data input */
 	if (!(ns->state & STATE_DATAIN_MASK)) {
-		NS_ERR("write_buf: data input isn't expected, state is %s, "
-			"switch to STATE_READY\n", get_state_name(ns->state));
-		switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
+		NS_ERR("write_buf: data input isn't expected, state is %s, switch to STATE_READY\n",
+		       ns_get_state_name(ns->state));
+		ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
 		return;
 	}
 
 	/* Check if these are expected bytes */
 	if (ns->regs.count + len > ns->regs.num) {
 		NS_ERR("write_buf: too many input bytes\n");
-		switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
+		ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
 		return;
 	}
 
@@ -2105,7 +2119,7 @@ static void ns_nand_read_buf(struct nand_chip *chip, u_char *buf, int len)
 	}
 	if (!(ns->state & STATE_DATAOUT_MASK)) {
 		NS_WARN("read_buf: unexpected data output cycle, current state is %s\n",
-			get_state_name(ns->state));
+			ns_get_state_name(ns->state));
 		return;
 	}
 
@@ -2121,7 +2135,7 @@ static void ns_nand_read_buf(struct nand_chip *chip, u_char *buf, int len)
 	/* Check if these are expected bytes */
 	if (ns->regs.count + len > ns->regs.num) {
 		NS_ERR("read_buf: too many bytes to read\n");
-		switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
+		ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
 		return;
 	}
 
@@ -2130,7 +2144,7 @@ static void ns_nand_read_buf(struct nand_chip *chip, u_char *buf, int len)
 
 	if (ns->regs.count == ns->regs.num) {
 		if (NS_STATE(ns->nxstate) == STATE_READY)
-			switch_state(ns);
+			ns_switch_state(ns);
 	}
 
 	return;
@@ -2288,13 +2302,13 @@ static int __init ns_init_module(void)
 
 	nsmtd->owner = THIS_MODULE;
 
-	if ((retval = parse_weakblocks()) != 0)
+	if ((retval = ns_parse_weakblocks()) != 0)
 		goto error;
 
-	if ((retval = parse_weakpages()) != 0)
+	if ((retval = ns_parse_weakpages()) != 0)
 		goto error;
 
-	if ((retval = parse_gravepages()) != 0)
+	if ((retval = ns_parse_gravepages()) != 0)
 		goto error;
 
 	nand_controller_init(&ns->base);
@@ -2328,16 +2342,16 @@ static int __init ns_init_module(void)
 		chip->pagemask = (targetsize >> chip->page_shift) - 1;
 	}
 
-	if ((retval = setup_wear_reporting(nsmtd)) != 0)
+	if ((retval = ns_setup_wear_reporting(nsmtd)) != 0)
 		goto err_exit;
 
-	if ((retval = init_nandsim(nsmtd)) != 0)
+	if ((retval = ns_init(nsmtd)) != 0)
 		goto err_exit;
 
 	if ((retval = nand_create_bbt(chip)) != 0)
 		goto err_exit;
 
-	if ((retval = parse_badblocks(ns, nsmtd)) != 0)
+	if ((retval = ns_parse_badblocks(ns, nsmtd)) != 0)
 		goto err_exit;
 
 	/* Register NAND partitions */
@@ -2346,19 +2360,19 @@ static int __init ns_init_module(void)
 	if (retval != 0)
 		goto err_exit;
 
-	if ((retval = nandsim_debugfs_create(ns)) != 0)
+	if ((retval = ns_debugfs_create(ns)) != 0)
 		goto err_exit;
 
         return 0;
 
 err_exit:
-	free_nandsim(ns);
+	ns_free(ns);
 	nand_release(chip);
 	for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
 		kfree(ns->partitions[i].name);
 error:
 	kfree(ns);
-	free_lists();
+	ns_free_lists();
 
 	return retval;
 }
@@ -2374,12 +2388,12 @@ static void __exit ns_cleanup_module(void)
 	struct nandsim *ns = nand_get_controller_data(chip);
 	int i;
 
-	free_nandsim(ns);    /* Free nandsim private resources */
+	ns_free(ns);    /* Free nandsim private resources */
 	nand_release(chip); /* Unregister driver */
 	for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
 		kfree(ns->partitions[i].name);
 	kfree(ns);        /* Free other structures */
-	free_lists();
+	ns_free_lists();
 }
 
 module_exit(ns_cleanup_module);
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 04/17] mtd: rawnand: nandsim: Clean error handling
  2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
                   ` (2 preceding siblings ...)
  2020-05-09 19:14 ` [PATCH 03/17] mtd: rawnand: nandsim: Use a consistent ns_ prefix for all functions Miquel Raynal
@ 2020-05-09 19:14 ` Miquel Raynal
  2020-05-09 19:14 ` [PATCH 05/17] mtd: rawnand: nandsim: Keep track of the created debugfs entries Miquel Raynal
                   ` (12 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

Many function calls are done this way:

        if ((retval = func()) != 0)
	        return retval;

while we expect in the kernel function calls like:

        retval = func();
	if (retval)
	        return retval;

Apply this change where possible and also use "ret" instead of
"retval" in ns_init_module for consistency, as it is only used in this
function.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/raw/nandsim.c | 46 ++++++++++++++++++++--------------
 1 file changed, 27 insertions(+), 19 deletions(-)

diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index 2c335cc8bcdf..5b427a50bc27 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -739,7 +739,8 @@ static int __init ns_init(struct mtd_info *mtd)
 	printk("sector address bytes: %u\n",    ns->geom.secaddrbytes);
 	printk("options: %#x\n",                ns->options);
 
-	if ((ret = ns_alloc_device(ns)) != 0)
+	ret = ns_alloc_device(ns);
+	if (ret)
 		return ret;
 
 	/* Allocate / initialize the internal buffer */
@@ -1747,7 +1748,7 @@ static void ns_switch_state(struct nandsim *ns)
 
 		NS_DBG("switch_state: operation is unknown, try to find it\n");
 
-		if (ns_find_operation(ns, 0) != 0)
+		if (!ns_find_operation(ns, 0))
 			return;
 
 		if ((ns->state & ACTION_MASK) &&
@@ -2243,7 +2244,7 @@ static int __init ns_init_module(void)
 {
 	struct nand_chip *chip;
 	struct nandsim *ns;
-	int retval = -ENOMEM, i;
+	int ret, i;
 
 	if (bus_width != 8 && bus_width != 16) {
 		NS_ERR("wrong bus width (%d), use only 8 or 16\n", bus_width);
@@ -2276,7 +2277,7 @@ static int __init ns_init_module(void)
 		break;
 	default:
 		NS_ERR("bbt has to be 0..2\n");
-		retval = -EINVAL;
+		ret = -EINVAL;
 		goto error;
 	}
 	/*
@@ -2302,21 +2303,24 @@ static int __init ns_init_module(void)
 
 	nsmtd->owner = THIS_MODULE;
 
-	if ((retval = ns_parse_weakblocks()) != 0)
+	ret = ns_parse_weakblocks();
+	if (ret)
 		goto error;
 
-	if ((retval = ns_parse_weakpages()) != 0)
+	ret = ns_parse_weakpages();
+	if (ret)
 		goto error;
 
-	if ((retval = ns_parse_gravepages()) != 0)
+	ret = ns_parse_gravepages();
+	if (ret)
 		goto error;
 
 	nand_controller_init(&ns->base);
 	ns->base.ops = &ns_controller_ops;
 	chip->controller = &ns->base;
 
-	retval = nand_scan(chip, 1);
-	if (retval) {
+	ret = nand_scan(chip, 1);
+	if (ret) {
 		NS_ERR("Could not scan NAND Simulator device\n");
 		goto error;
 	}
@@ -2330,7 +2334,7 @@ static int __init ns_init_module(void)
 
 		if (new_size >> overridesize != nsmtd->erasesize) {
 			NS_ERR("overridesize is too big\n");
-			retval = -EINVAL;
+			ret = -EINVAL;
 			goto err_exit;
 		}
 
@@ -2342,25 +2346,29 @@ static int __init ns_init_module(void)
 		chip->pagemask = (targetsize >> chip->page_shift) - 1;
 	}
 
-	if ((retval = ns_setup_wear_reporting(nsmtd)) != 0)
+	ret = ns_setup_wear_reporting(nsmtd);
+	if (ret)
 		goto err_exit;
 
-	if ((retval = ns_init(nsmtd)) != 0)
+	ret = ns_init(nsmtd);
+	if (ret)
 		goto err_exit;
 
-	if ((retval = nand_create_bbt(chip)) != 0)
+	ret = nand_create_bbt(chip);
+	if (ret)
 		goto err_exit;
 
-	if ((retval = ns_parse_badblocks(ns, nsmtd)) != 0)
+	ret = ns_parse_badblocks(ns, nsmtd);
+	if (ret)
 		goto err_exit;
 
 	/* Register NAND partitions */
-	retval = mtd_device_register(nsmtd, &ns->partitions[0],
-				     ns->nbparts);
-	if (retval != 0)
+	ret = mtd_device_register(nsmtd, &ns->partitions[0], ns->nbparts);
+	if (ret)
 		goto err_exit;
 
-	if ((retval = ns_debugfs_create(ns)) != 0)
+	ret = ns_debugfs_create(ns);
+	if (ret)
 		goto err_exit;
 
         return 0;
@@ -2374,7 +2382,7 @@ static int __init ns_init_module(void)
 	kfree(ns);
 	ns_free_lists();
 
-	return retval;
+	return ret;
 }
 
 module_init(ns_init_module);
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 05/17] mtd: rawnand: nandsim: Keep track of the created debugfs entries
  2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
                   ` (3 preceding siblings ...)
  2020-05-09 19:14 ` [PATCH 04/17] mtd: rawnand: nandsim: Clean error handling Miquel Raynal
@ 2020-05-09 19:14 ` Miquel Raynal
  2020-05-09 19:14 ` [PATCH 06/17] mtd: rawnand: nandsim: Remove debugfs entries at unload time Miquel Raynal
                   ` (11 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

Debugfs entries should be removed in the error path, so first, keep
track of them.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/raw/nandsim.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index 5b427a50bc27..c8e9c70a6641 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -353,6 +353,9 @@ struct nandsim {
 	void *file_buf;
 	struct page *held_pages[NS_MAX_HELD_PAGES];
 	int held_cnt;
+
+	/* debugfs entry */
+	struct dentry *dent;
 };
 
 /*
@@ -495,7 +498,6 @@ DEFINE_SHOW_ATTRIBUTE(ns);
 static int ns_debugfs_create(struct nandsim *ns)
 {
 	struct dentry *root = nsmtd->dbg.dfs_dir;
-	struct dentry *dent;
 
 	/*
 	 * Just skip debugfs initialization when the debugfs directory is
@@ -508,9 +510,9 @@ static int ns_debugfs_create(struct nandsim *ns)
 		return 0;
 	}
 
-	dent = debugfs_create_file("nandsim_wear_report", 0400, root, ns,
-				   &ns_fops);
-	if (IS_ERR_OR_NULL(dent)) {
+	ns->dent = debugfs_create_file("nandsim_wear_report", 0400, root, ns,
+				       &ns_fops);
+	if (IS_ERR_OR_NULL(ns->dent)) {
 		NS_ERR("cannot create \"nandsim_wear_report\" debugfs entry\n");
 		return -1;
 	}
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 06/17] mtd: rawnand: nandsim: Remove debugfs entries at unload time
  2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
                   ` (4 preceding siblings ...)
  2020-05-09 19:14 ` [PATCH 05/17] mtd: rawnand: nandsim: Keep track of the created debugfs entries Miquel Raynal
@ 2020-05-09 19:14 ` Miquel Raynal
  2020-05-09 20:43   ` Richard Weinberger
  2020-05-24 21:33   ` Richard Weinberger
  2020-05-09 19:14 ` [PATCH 07/17] mtd: rawnand: nandsim: Fix the two ns_alloc_device() error paths Miquel Raynal
                   ` (10 subsequent siblings)
  16 siblings, 2 replies; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

Create a ns_debugfs_remove() helper for that and call it in
ns_cleanup_module().

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/raw/nandsim.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index c8e9c70a6641..7862c65e32ad 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -520,6 +520,11 @@ static int ns_debugfs_create(struct nandsim *ns)
 	return 0;
 }
 
+static void ns_debugfs_remove(struct nandsim *ns)
+{
+	debugfs_remove(ns->dent);
+}
+
 /*
  * Allocate array of page pointers, create slab allocation for an array
  * and initialize the array by NULL pointers.
@@ -2398,6 +2403,7 @@ static void __exit ns_cleanup_module(void)
 	struct nandsim *ns = nand_get_controller_data(chip);
 	int i;
 
+	ns_debugfs_remove(ns);
 	ns_free(ns);    /* Free nandsim private resources */
 	nand_release(chip); /* Unregister driver */
 	for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 07/17] mtd: rawnand: nandsim: Fix the two ns_alloc_device() error paths
  2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
                   ` (5 preceding siblings ...)
  2020-05-09 19:14 ` [PATCH 06/17] mtd: rawnand: nandsim: Remove debugfs entries at unload time Miquel Raynal
@ 2020-05-09 19:14 ` Miquel Raynal
  2020-05-09 19:14 ` [PATCH 08/17] mtd: rawnand: nandsim: Free partition names on error in ns_init() Miquel Raynal
                   ` (9 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

The ns_alloc_device() helper has actually two distinct path. Handle
errors in both of them.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/raw/nandsim.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index 7862c65e32ad..7ffb46b01380 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -543,12 +543,12 @@ static int __init ns_alloc_device(struct nandsim *ns)
 		if (!(cfile->f_mode & FMODE_CAN_READ)) {
 			NS_ERR("alloc_device: cache file not readable\n");
 			err = -EINVAL;
-			goto err_close;
+			goto err_close_filp;
 		}
 		if (!(cfile->f_mode & FMODE_CAN_WRITE)) {
 			NS_ERR("alloc_device: cache file not writeable\n");
 			err = -EINVAL;
-			goto err_close;
+			goto err_close_filp;
 		}
 		ns->pages_written =
 			vzalloc(array_size(sizeof(unsigned long),
@@ -556,16 +556,24 @@ static int __init ns_alloc_device(struct nandsim *ns)
 		if (!ns->pages_written) {
 			NS_ERR("alloc_device: unable to allocate pages written array\n");
 			err = -ENOMEM;
-			goto err_close;
+			goto err_close_filp;
 		}
 		ns->file_buf = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
 		if (!ns->file_buf) {
 			NS_ERR("alloc_device: unable to allocate file buf\n");
 			err = -ENOMEM;
-			goto err_free;
+			goto err_free_pw;
 		}
 		ns->cfile = cfile;
+
 		return 0;
+
+err_free_pw:
+		vfree(ns->pages_written);
+err_close_filp:
+		filp_close(cfile, NULL);
+
+		return err;
 	}
 
 	ns->pages = vmalloc(array_size(sizeof(union ns_mem), ns->geom.pgnum));
@@ -580,15 +588,15 @@ static int __init ns_alloc_device(struct nandsim *ns)
 						ns->geom.pgszoob, 0, 0, NULL);
 	if (!ns->nand_pages_slab) {
 		NS_ERR("cache_create: unable to create kmem_cache\n");
-		return -ENOMEM;
+		err = -ENOMEM;
+		goto err_free_pg;
 	}
 
 	return 0;
 
-err_free:
-	vfree(ns->pages_written);
-err_close:
-	filp_close(cfile, NULL);
+err_free_pg:
+	vfree(ns->pages);
+
 	return err;
 }
 
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 08/17] mtd: rawnand: nandsim: Free partition names on error in ns_init()
  2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
                   ` (6 preceding siblings ...)
  2020-05-09 19:14 ` [PATCH 07/17] mtd: rawnand: nandsim: Fix the two ns_alloc_device() error paths Miquel Raynal
@ 2020-05-09 19:14 ` Miquel Raynal
  2020-05-09 19:14 ` [PATCH 09/17] mtd: rawnand: nandsim: Free the allocated device " Miquel Raynal
                   ` (8 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

The ns_init() function shall free the partition names allocated by
ns_get_partition_name() on error.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/raw/nandsim.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index 7ffb46b01380..a50ffa04cc04 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -722,12 +722,14 @@ static int __init ns_init(struct mtd_info *mtd)
 	if (remains) {
 		if (parts_num + 1 > ARRAY_SIZE(ns->partitions)) {
 			NS_ERR("too many partitions.\n");
-			return -EINVAL;
+			ret = -EINVAL;
+			goto free_partition_names;
 		}
 		ns->partitions[i].name = ns_get_partition_name(i);
 		if (!ns->partitions[i].name) {
 			NS_ERR("unable to allocate memory.\n");
-			return -ENOMEM;
+			ret = -ENOMEM;
+			goto free_partition_names;
 		}
 		ns->partitions[i].offset = next_offset;
 		ns->partitions[i].size   = remains;
@@ -756,18 +758,25 @@ static int __init ns_init(struct mtd_info *mtd)
 
 	ret = ns_alloc_device(ns);
 	if (ret)
-		return ret;
+		goto free_partition_names;
 
 	/* Allocate / initialize the internal buffer */
 	ns->buf.byte = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
 	if (!ns->buf.byte) {
 		NS_ERR("init_nandsim: unable to allocate %u bytes for the internal buffer\n",
 			ns->geom.pgszoob);
-		return -ENOMEM;
+		ret = -ENOMEM;
+		goto free_partition_names;
 	}
 	memset(ns->buf.byte, 0xFF, ns->geom.pgszoob);
 
 	return 0;
+
+free_partition_names:
+	for (i = 0; i < ARRAY_SIZE(ns->partitions); ++i)
+		kfree(ns->partitions[i].name);
+
+	return ret;
 }
 
 /*
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 09/17] mtd: rawnand: nandsim: Free the allocated device on error in ns_init()
  2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
                   ` (7 preceding siblings ...)
  2020-05-09 19:14 ` [PATCH 08/17] mtd: rawnand: nandsim: Free partition names on error in ns_init() Miquel Raynal
@ 2020-05-09 19:14 ` Miquel Raynal
  2020-05-09 19:14 ` [PATCH 10/17] mtd: rawnand: nandsim: Free the partition names in ns_free() Miquel Raynal
                   ` (7 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

The nandsim device is allocated and initialized inside ns_init() by a
call to ns_alloc_device(), free it on error.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/raw/nandsim.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index a50ffa04cc04..c71bbcde154c 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -766,12 +766,14 @@ static int __init ns_init(struct mtd_info *mtd)
 		NS_ERR("init_nandsim: unable to allocate %u bytes for the internal buffer\n",
 			ns->geom.pgszoob);
 		ret = -ENOMEM;
-		goto free_partition_names;
+		goto free_device;
 	}
 	memset(ns->buf.byte, 0xFF, ns->geom.pgszoob);
 
 	return 0;
 
+free_device:
+	ns_free_device(ns);
 free_partition_names:
 	for (i = 0; i < ARRAY_SIZE(ns->partitions); ++i)
 		kfree(ns->partitions[i].name);
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 10/17] mtd: rawnand: nandsim: Free the partition names in ns_free()
  2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
                   ` (8 preceding siblings ...)
  2020-05-09 19:14 ` [PATCH 09/17] mtd: rawnand: nandsim: Free the allocated device " Miquel Raynal
@ 2020-05-09 19:14 ` Miquel Raynal
  2020-05-09 19:14 ` [PATCH 11/17] mtd: rawnand: nandsim: Stop using nand_release() Miquel Raynal
                   ` (6 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

ns_free() is the helper that is called symmetrically to ns_init() and
so should free the same objects, including the partition names.

Now, callers of ns_free() do not need to free this partition names
themselves.

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

diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index c71bbcde154c..0e17d7e0fd68 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -786,6 +786,11 @@ static int __init ns_init(struct mtd_info *mtd)
  */
 static void ns_free(struct nandsim *ns)
 {
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(ns->partitions); ++i)
+		kfree(ns->partitions[i].name);
+
 	kfree(ns->buf.byte);
 	ns_free_device(ns);
 
@@ -2270,7 +2275,7 @@ static int __init ns_init_module(void)
 {
 	struct nand_chip *chip;
 	struct nandsim *ns;
-	int ret, i;
+	int ret;
 
 	if (bus_width != 8 && bus_width != 16) {
 		NS_ERR("wrong bus width (%d), use only 8 or 16\n", bus_width);
@@ -2402,8 +2407,6 @@ static int __init ns_init_module(void)
 err_exit:
 	ns_free(ns);
 	nand_release(chip);
-	for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
-		kfree(ns->partitions[i].name);
 error:
 	kfree(ns);
 	ns_free_lists();
@@ -2420,13 +2423,10 @@ static void __exit ns_cleanup_module(void)
 {
 	struct nand_chip *chip = mtd_to_nand(nsmtd);
 	struct nandsim *ns = nand_get_controller_data(chip);
-	int i;
 
 	ns_debugfs_remove(ns);
 	ns_free(ns);    /* Free nandsim private resources */
 	nand_release(chip); /* Unregister driver */
-	for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
-		kfree(ns->partitions[i].name);
 	kfree(ns);        /* Free other structures */
 	ns_free_lists();
 }
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 11/17] mtd: rawnand: nandsim: Stop using nand_release()
  2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
                   ` (9 preceding siblings ...)
  2020-05-09 19:14 ` [PATCH 10/17] mtd: rawnand: nandsim: Free the partition names in ns_free() Miquel Raynal
@ 2020-05-09 19:14 ` Miquel Raynal
  2020-05-09 19:14 ` [PATCH 12/17] mtd: rawnand: nandsim: Use an additional label when freeing the nandsim object Miquel Raynal
                   ` (5 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

nand_release() basically calls mtd_device_unregister() and
nand_cleanup(). Both helpers should be called after MTD device
registration and NAND scan, respectively. Drop nand_release() and use
the two helpers directly so that they fit the error path and the
labels there.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/raw/nandsim.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index 0e17d7e0fd68..48383c29a10c 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -2400,13 +2400,15 @@ static int __init ns_init_module(void)
 
 	ret = ns_debugfs_create(ns);
 	if (ret)
-		goto err_exit;
+		goto unregister_mtd;
 
         return 0;
 
+unregister_mtd:
+	WARN_ON(mtd_device_unregister(nsmtd));
 err_exit:
 	ns_free(ns);
-	nand_release(chip);
+	nand_cleanup(chip);
 error:
 	kfree(ns);
 	ns_free_lists();
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 12/17] mtd: rawnand: nandsim: Use an additional label when freeing the nandsim object
  2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
                   ` (10 preceding siblings ...)
  2020-05-09 19:14 ` [PATCH 11/17] mtd: rawnand: nandsim: Stop using nand_release() Miquel Raynal
@ 2020-05-09 19:14 ` Miquel Raynal
  2020-05-09 19:14 ` [PATCH 13/17] mtd: rawnand: nandsim: Free erase_block_wear on error Miquel Raynal
                   ` (4 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

Cosmetic change to give a meaning to all labels in this complicated
error path.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/raw/nandsim.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index 48383c29a10c..7f119703f170 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -2387,16 +2387,16 @@ static int __init ns_init_module(void)
 
 	ret = nand_create_bbt(chip);
 	if (ret)
-		goto err_exit;
+		goto free_ns_object;
 
 	ret = ns_parse_badblocks(ns, nsmtd);
 	if (ret)
-		goto err_exit;
+		goto free_ns_object;
 
 	/* Register NAND partitions */
 	ret = mtd_device_register(nsmtd, &ns->partitions[0], ns->nbparts);
 	if (ret)
-		goto err_exit;
+		goto free_ns_object;
 
 	ret = ns_debugfs_create(ns);
 	if (ret)
@@ -2407,6 +2407,7 @@ static int __init ns_init_module(void)
 unregister_mtd:
 	WARN_ON(mtd_device_unregister(nsmtd));
 err_exit:
+free_ns_object:
 	ns_free(ns);
 	nand_cleanup(chip);
 error:
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 13/17] mtd: rawnand: nandsim: Free erase_block_wear on error
  2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
                   ` (11 preceding siblings ...)
  2020-05-09 19:14 ` [PATCH 12/17] mtd: rawnand: nandsim: Use an additional label when freeing the nandsim object Miquel Raynal
@ 2020-05-09 19:14 ` Miquel Raynal
  2020-05-09 19:14 ` [PATCH 14/17] mtd: rawnand: nandsim: Fix the label pointing on nand_cleanup() Miquel Raynal
                   ` (3 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

Free erase_block_wear on error, which is allocated by
ns_setup_wear_reporting().

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/raw/nandsim.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index 7f119703f170..c5ebcf667641 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -2383,7 +2383,7 @@ static int __init ns_init_module(void)
 
 	ret = ns_init(nsmtd);
 	if (ret)
-		goto err_exit;
+		goto free_ebw;
 
 	ret = nand_create_bbt(chip);
 	if (ret)
@@ -2409,6 +2409,8 @@ static int __init ns_init_module(void)
 err_exit:
 free_ns_object:
 	ns_free(ns);
+free_ebw:
+	kfree(erase_block_wear);
 	nand_cleanup(chip);
 error:
 	kfree(ns);
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 14/17] mtd: rawnand: nandsim: Fix the label pointing on nand_cleanup()
  2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
                   ` (12 preceding siblings ...)
  2020-05-09 19:14 ` [PATCH 13/17] mtd: rawnand: nandsim: Free erase_block_wear on error Miquel Raynal
@ 2020-05-09 19:14 ` Miquel Raynal
  2020-05-09 19:14 ` [PATCH 15/17] mtd: rawnand: nandsim: Manage lists on error in ns_init_module() Miquel Raynal
                   ` (2 subsequent siblings)
  16 siblings, 0 replies; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

Drop the generic err_exit.

The remaining operation to do from this goto statement is to cleanup
the NAND allocations, so rename it.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/raw/nandsim.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index c5ebcf667641..e41866e49206 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -2366,7 +2366,7 @@ static int __init ns_init_module(void)
 		if (new_size >> overridesize != nsmtd->erasesize) {
 			NS_ERR("overridesize is too big\n");
 			ret = -EINVAL;
-			goto err_exit;
+			goto cleanup_nand;
 		}
 
 		/* N.B. This relies on nand_scan not doing anything with the size before we change it */
@@ -2379,7 +2379,7 @@ static int __init ns_init_module(void)
 
 	ret = ns_setup_wear_reporting(nsmtd);
 	if (ret)
-		goto err_exit;
+		goto cleanup_nand;
 
 	ret = ns_init(nsmtd);
 	if (ret)
@@ -2406,11 +2406,11 @@ static int __init ns_init_module(void)
 
 unregister_mtd:
 	WARN_ON(mtd_device_unregister(nsmtd));
-err_exit:
 free_ns_object:
 	ns_free(ns);
 free_ebw:
 	kfree(erase_block_wear);
+cleanup_nand:
 	nand_cleanup(chip);
 error:
 	kfree(ns);
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 15/17] mtd: rawnand: nandsim: Manage lists on error in ns_init_module()
  2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
                   ` (13 preceding siblings ...)
  2020-05-09 19:14 ` [PATCH 14/17] mtd: rawnand: nandsim: Fix the label pointing on nand_cleanup() Miquel Raynal
@ 2020-05-09 19:14 ` Miquel Raynal
  2020-05-24 21:39   ` Richard Weinberger
  2020-05-09 19:14 ` [PATCH 16/17] mtd: rawnand: nandsim: Rename a label " Miquel Raynal
  2020-05-09 19:14 ` [PATCH 17/17] mtd: rawnand: nandsim: Reorganize ns_cleanup_module() Miquel Raynal
  16 siblings, 1 reply; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

Lists are filled with calls to ns_parse_weakblocks(),
ns_parse_weakpages() and ns_parse_gravepages(). Handle them in the
error path, all at the same time.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/raw/nandsim.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index e41866e49206..26d23ab5b794 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -2273,6 +2273,7 @@ static const struct nand_controller_ops ns_controller_ops = {
  */
 static int __init ns_init_module(void)
 {
+	struct list_head *pos, *n;
 	struct nand_chip *chip;
 	struct nandsim *ns;
 	int ret;
@@ -2340,11 +2341,11 @@ static int __init ns_init_module(void)
 
 	ret = ns_parse_weakpages();
 	if (ret)
-		goto error;
+		goto free_wb_list;
 
 	ret = ns_parse_gravepages();
 	if (ret)
-		goto error;
+		goto free_wp_list;
 
 	nand_controller_init(&ns->base);
 	ns->base.ops = &ns_controller_ops;
@@ -2353,7 +2354,7 @@ static int __init ns_init_module(void)
 	ret = nand_scan(chip, 1);
 	if (ret) {
 		NS_ERR("Could not scan NAND Simulator device\n");
-		goto error;
+		goto free_gp_list;
 	}
 
 	if (overridesize) {
@@ -2412,9 +2413,23 @@ static int __init ns_init_module(void)
 	kfree(erase_block_wear);
 cleanup_nand:
 	nand_cleanup(chip);
+free_gp_list:
+	list_for_each_safe(pos, n, &grave_pages) {
+		kfree(list_entry(pos, struct grave_page, list));
+		list_del(pos);
+	}
+free_wp_list:
+	list_for_each_safe(pos, n, &weak_pages) {
+		kfree(list_entry(pos, struct weak_page, list));
+		list_del(pos);
+	}
+free_wb_list:
+	list_for_each_safe(pos, n, &weak_blocks) {
+		kfree(list_entry(pos, struct weak_block, list));
+		list_del(pos);
+	}
 error:
 	kfree(ns);
-	ns_free_lists();
 
 	return ret;
 }
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 16/17] mtd: rawnand: nandsim: Rename a label in ns_init_module()
  2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
                   ` (14 preceding siblings ...)
  2020-05-09 19:14 ` [PATCH 15/17] mtd: rawnand: nandsim: Manage lists on error in ns_init_module() Miquel Raynal
@ 2020-05-09 19:14 ` Miquel Raynal
  2020-05-09 19:14 ` [PATCH 17/17] mtd: rawnand: nandsim: Reorganize ns_cleanup_module() Miquel Raynal
  16 siblings, 0 replies; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

Rename the "error" label to gave it a meaning.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/raw/nandsim.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index 26d23ab5b794..fa84f373b4e9 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -2310,7 +2310,7 @@ static int __init ns_init_module(void)
 	default:
 		NS_ERR("bbt has to be 0..2\n");
 		ret = -EINVAL;
-		goto error;
+		goto free_ns_struct;
 	}
 	/*
 	 * Perform minimum nandsim structure initialization to handle
@@ -2337,7 +2337,7 @@ static int __init ns_init_module(void)
 
 	ret = ns_parse_weakblocks();
 	if (ret)
-		goto error;
+		goto free_ns_struct;
 
 	ret = ns_parse_weakpages();
 	if (ret)
@@ -2428,7 +2428,7 @@ static int __init ns_init_module(void)
 		kfree(list_entry(pos, struct weak_block, list));
 		list_del(pos);
 	}
-error:
+free_ns_struct:
 	kfree(ns);
 
 	return ret;
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 17/17] mtd: rawnand: nandsim: Reorganize ns_cleanup_module()
  2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
                   ` (15 preceding siblings ...)
  2020-05-09 19:14 ` [PATCH 16/17] mtd: rawnand: nandsim: Rename a label " Miquel Raynal
@ 2020-05-09 19:14 ` Miquel Raynal
  2020-05-24 21:37   ` Richard Weinberger
  16 siblings, 1 reply; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 19:14 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Tudor Ambarus, linux-mtd
  Cc: dedekind, Boris Brezillon, Miquel Raynal

Reorganize ns_cleanup_module() to fit the reworked exit path of
ns_init_module().

There is no need for a ns_free_lists() function anymore, so drop it.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 drivers/mtd/nand/raw/nandsim.c | 40 +++++++++++++++-------------------
 1 file changed, 18 insertions(+), 22 deletions(-)

diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index fa84f373b4e9..744de767cdc3 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -978,24 +978,6 @@ static int ns_read_error(unsigned int page_no)
 	return 0;
 }
 
-static void ns_free_lists(void)
-{
-	struct list_head *pos, *n;
-	list_for_each_safe(pos, n, &weak_blocks) {
-		list_del(pos);
-		kfree(list_entry(pos, struct weak_block, list));
-	}
-	list_for_each_safe(pos, n, &weak_pages) {
-		list_del(pos);
-		kfree(list_entry(pos, struct weak_page, list));
-	}
-	list_for_each_safe(pos, n, &grave_pages) {
-		list_del(pos);
-		kfree(list_entry(pos, struct grave_page, list));
-	}
-	kfree(erase_block_wear);
-}
-
 static int ns_setup_wear_reporting(struct mtd_info *mtd)
 {
 	size_t mem;
@@ -2443,12 +2425,26 @@ static void __exit ns_cleanup_module(void)
 {
 	struct nand_chip *chip = mtd_to_nand(nsmtd);
 	struct nandsim *ns = nand_get_controller_data(chip);
+	struct list_head *pos, *n;
 
 	ns_debugfs_remove(ns);
-	ns_free(ns);    /* Free nandsim private resources */
-	nand_release(chip); /* Unregister driver */
-	kfree(ns);        /* Free other structures */
-	ns_free_lists();
+	WARN_ON(mtd_device_unregister(nsmtd));
+	ns_free(ns);
+	kfree(erase_block_wear);
+	nand_cleanup(chip);
+	list_for_each_safe(pos, n, &grave_pages) {
+		kfree(list_entry(pos, struct grave_page, list));
+		list_del(pos);
+	}
+	list_for_each_safe(pos, n, &weak_pages) {
+		kfree(list_entry(pos, struct weak_page, list));
+		list_del(pos);
+	}
+	list_for_each_safe(pos, n, &weak_blocks) {
+		kfree(list_entry(pos, struct weak_block, list));
+		list_del(pos);
+	}
+	kfree(ns);
 }
 
 module_exit(ns_cleanup_module);
-- 
2.20.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* Re: [PATCH 06/17] mtd: rawnand: nandsim: Remove debugfs entries at unload time
  2020-05-09 19:14 ` [PATCH 06/17] mtd: rawnand: nandsim: Remove debugfs entries at unload time Miquel Raynal
@ 2020-05-09 20:43   ` Richard Weinberger
  2020-05-09 22:29     ` Miquel Raynal
  2020-05-24 21:33   ` Richard Weinberger
  1 sibling, 1 reply; 31+ messages in thread
From: Richard Weinberger @ 2020-05-09 20:43 UTC (permalink / raw)
  To: Miquel Raynal
  Cc: dedekind, Boris Brezillon, linux-mtd, Vignesh Raghavendra, Tudor Ambarus

----- Ursprüngliche Mail -----
> Von: "Miquel Raynal" <miquel.raynal@bootlin.com>
> An: "richard" <richard@nod.at>, "Vignesh Raghavendra" <vigneshr@ti.com>, "Tudor Ambarus" <Tudor.Ambarus@microchip.com>,
> "linux-mtd" <linux-mtd@lists.infradead.org>
> CC: "Boris Brezillon" <boris.brezillon@collabora.com>, dedekind@infradead.org, "Miquel Raynal"
> <miquel.raynal@bootlin.com>
> Gesendet: Samstag, 9. Mai 2020 21:14:19
> Betreff: [PATCH 06/17] mtd: rawnand: nandsim: Remove debugfs entries at unload time

> Create a ns_debugfs_remove() helper for that and call it in
> ns_cleanup_module().
> 
> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> ---
> drivers/mtd/nand/raw/nandsim.c | 6 ++++++
> 1 file changed, 6 insertions(+)
> 
> diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
> index c8e9c70a6641..7862c65e32ad 100644
> --- a/drivers/mtd/nand/raw/nandsim.c
> +++ b/drivers/mtd/nand/raw/nandsim.c
> @@ -520,6 +520,11 @@ static int ns_debugfs_create(struct nandsim *ns)
> 	return 0;
> }
> 
> +static void ns_debugfs_remove(struct nandsim *ns)
> +{
> +	debugfs_remove(ns->dent);
> +}
> +
> /*
>  * Allocate array of page pointers, create slab allocation for an array
>  * and initialize the array by NULL pointers.
> @@ -2398,6 +2403,7 @@ static void __exit ns_cleanup_module(void)
> 	struct nandsim *ns = nand_get_controller_data(chip);
> 	int i;
> 
> +	ns_debugfs_remove(ns);
> 	ns_free(ns);    /* Free nandsim private resources */

Why is this special and cannot done in ns_free()?

Thanks,
//richard

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 06/17] mtd: rawnand: nandsim: Remove debugfs entries at unload time
  2020-05-09 20:43   ` Richard Weinberger
@ 2020-05-09 22:29     ` Miquel Raynal
  2020-05-24 21:27       ` Richard Weinberger
  0 siblings, 1 reply; 31+ messages in thread
From: Miquel Raynal @ 2020-05-09 22:29 UTC (permalink / raw)
  To: Richard Weinberger
  Cc: dedekind, Boris Brezillon, linux-mtd, Vignesh Raghavendra, Tudor Ambarus

Hi Richard,

Richard Weinberger <richard@nod.at> wrote on Sat, 9 May 2020 22:43:00
+0200 (CEST):

> ----- Ursprüngliche Mail -----
> > Von: "Miquel Raynal" <miquel.raynal@bootlin.com>
> > An: "richard" <richard@nod.at>, "Vignesh Raghavendra" <vigneshr@ti.com>, "Tudor Ambarus" <Tudor.Ambarus@microchip.com>,
> > "linux-mtd" <linux-mtd@lists.infradead.org>
> > CC: "Boris Brezillon" <boris.brezillon@collabora.com>, dedekind@infradead.org, "Miquel Raynal"
> > <miquel.raynal@bootlin.com>
> > Gesendet: Samstag, 9. Mai 2020 21:14:19
> > Betreff: [PATCH 06/17] mtd: rawnand: nandsim: Remove debugfs entries at unload time  
> 
> > Create a ns_debugfs_remove() helper for that and call it in
> > ns_cleanup_module().
> > 
> > Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> > ---
> > drivers/mtd/nand/raw/nandsim.c | 6 ++++++
> > 1 file changed, 6 insertions(+)
> > 
> > diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
> > index c8e9c70a6641..7862c65e32ad 100644
> > --- a/drivers/mtd/nand/raw/nandsim.c
> > +++ b/drivers/mtd/nand/raw/nandsim.c
> > @@ -520,6 +520,11 @@ static int ns_debugfs_create(struct nandsim *ns)
> > 	return 0;
> > }
> > 
> > +static void ns_debugfs_remove(struct nandsim *ns)
> > +{
> > +	debugfs_remove(ns->dent);
> > +}
> > +
> > /*
> >  * Allocate array of page pointers, create slab allocation for an array
> >  * and initialize the array by NULL pointers.
> > @@ -2398,6 +2403,7 @@ static void __exit ns_cleanup_module(void)
> > 	struct nandsim *ns = nand_get_controller_data(chip);
> > 	int i;
> > 
> > +	ns_debugfs_remove(ns);
> > 	ns_free(ns);    /* Free nandsim private resources */  
> 
> Why is this special and cannot done in ns_free()?
> 

ns_debugfs_create() is called in ns_init_module(), so for me it is
natural to call ns_debugfs_remove in ns_cleanup_module(). More than
calling it from ns_free() which is the symmetry of ns_init(). No?


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 06/17] mtd: rawnand: nandsim: Remove debugfs entries at unload time
  2020-05-09 22:29     ` Miquel Raynal
@ 2020-05-24 21:27       ` Richard Weinberger
  0 siblings, 0 replies; 31+ messages in thread
From: Richard Weinberger @ 2020-05-24 21:27 UTC (permalink / raw)
  To: Miquel Raynal
  Cc: Vignesh Raghavendra, Tudor Ambarus, dedekind, Richard Weinberger,
	Boris Brezillon, linux-mtd

On Sun, May 10, 2020 at 12:30 AM Miquel Raynal
<miquel.raynal@bootlin.com> wrote:
>
> Hi Richard,
>
> Richard Weinberger <richard@nod.at> wrote on Sat, 9 May 2020 22:43:00
> +0200 (CEST):
>
> > ----- Ursprüngliche Mail -----
> > > Von: "Miquel Raynal" <miquel.raynal@bootlin.com>
> > > An: "richard" <richard@nod.at>, "Vignesh Raghavendra" <vigneshr@ti.com>, "Tudor Ambarus" <Tudor.Ambarus@microchip.com>,
> > > "linux-mtd" <linux-mtd@lists.infradead.org>
> > > CC: "Boris Brezillon" <boris.brezillon@collabora.com>, dedekind@infradead.org, "Miquel Raynal"
> > > <miquel.raynal@bootlin.com>
> > > Gesendet: Samstag, 9. Mai 2020 21:14:19
> > > Betreff: [PATCH 06/17] mtd: rawnand: nandsim: Remove debugfs entries at unload time
> >
> > > Create a ns_debugfs_remove() helper for that and call it in
> > > ns_cleanup_module().
> > >
> > > Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> > > ---
> > > drivers/mtd/nand/raw/nandsim.c | 6 ++++++
> > > 1 file changed, 6 insertions(+)
> > >
> > > diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
> > > index c8e9c70a6641..7862c65e32ad 100644
> > > --- a/drivers/mtd/nand/raw/nandsim.c
> > > +++ b/drivers/mtd/nand/raw/nandsim.c
> > > @@ -520,6 +520,11 @@ static int ns_debugfs_create(struct nandsim *ns)
> > >     return 0;
> > > }
> > >
> > > +static void ns_debugfs_remove(struct nandsim *ns)
> > > +{
> > > +   debugfs_remove(ns->dent);
> > > +}
> > > +
> > > /*
> > >  * Allocate array of page pointers, create slab allocation for an array
> > >  * and initialize the array by NULL pointers.
> > > @@ -2398,6 +2403,7 @@ static void __exit ns_cleanup_module(void)
> > >     struct nandsim *ns = nand_get_controller_data(chip);
> > >     int i;
> > >
> > > +   ns_debugfs_remove(ns);
> > >     ns_free(ns);    /* Free nandsim private resources */
> >
> > Why is this special and cannot done in ns_free()?
> >
>
> ns_debugfs_create() is called in ns_init_module(), so for me it is
> natural to call ns_debugfs_remove in ns_cleanup_module(). More than
> calling it from ns_free() which is the symmetry of ns_init(). No?

Okay. Makes sense.

-- 
Thanks,
//richard

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 06/17] mtd: rawnand: nandsim: Remove debugfs entries at unload time
  2020-05-09 19:14 ` [PATCH 06/17] mtd: rawnand: nandsim: Remove debugfs entries at unload time Miquel Raynal
  2020-05-09 20:43   ` Richard Weinberger
@ 2020-05-24 21:33   ` Richard Weinberger
  2020-05-24 22:14     ` Miquel Raynal
  1 sibling, 1 reply; 31+ messages in thread
From: Richard Weinberger @ 2020-05-24 21:33 UTC (permalink / raw)
  To: Miquel Raynal
  Cc: Vignesh Raghavendra, Tudor Ambarus, dedekind, Richard Weinberger,
	Boris Brezillon, linux-mtd

On Sat, May 9, 2020 at 9:16 PM Miquel Raynal <miquel.raynal@bootlin.com> wrote:
>
> Create a ns_debugfs_remove() helper for that and call it in
> ns_cleanup_module().
>
> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> ---
>  drivers/mtd/nand/raw/nandsim.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
> index c8e9c70a6641..7862c65e32ad 100644
> --- a/drivers/mtd/nand/raw/nandsim.c
> +++ b/drivers/mtd/nand/raw/nandsim.c
> @@ -520,6 +520,11 @@ static int ns_debugfs_create(struct nandsim *ns)
>         return 0;
>  }
>
> +static void ns_debugfs_remove(struct nandsim *ns)
> +{
> +       debugfs_remove(ns->dent);

BTW: For now it is perfectly fine, but later this can be a
debugfs_remove_recursive().

> +}
> +
>  /*
>   * Allocate array of page pointers, create slab allocation for an array
>   * and initialize the array by NULL pointers.
> @@ -2398,6 +2403,7 @@ static void __exit ns_cleanup_module(void)
>         struct nandsim *ns = nand_get_controller_data(chip);
>         int i;
>
> +       ns_debugfs_remove(ns);
>         ns_free(ns);    /* Free nandsim private resources */
>         nand_release(chip); /* Unregister driver */
>         for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
> --
> 2.20.1
>
>
> ______________________________________________________
> Linux MTD discussion mailing list
> http://lists.infradead.org/mailman/listinfo/linux-mtd/



-- 
Thanks,
//richard

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 17/17] mtd: rawnand: nandsim: Reorganize ns_cleanup_module()
  2020-05-09 19:14 ` [PATCH 17/17] mtd: rawnand: nandsim: Reorganize ns_cleanup_module() Miquel Raynal
@ 2020-05-24 21:37   ` Richard Weinberger
  2020-05-24 22:13     ` Miquel Raynal
  0 siblings, 1 reply; 31+ messages in thread
From: Richard Weinberger @ 2020-05-24 21:37 UTC (permalink / raw)
  To: Miquel Raynal
  Cc: Vignesh Raghavendra, Tudor Ambarus, dedekind, Richard Weinberger,
	Boris Brezillon, linux-mtd

On Sat, May 9, 2020 at 9:19 PM Miquel Raynal <miquel.raynal@bootlin.com> wrote:
>         ns_debugfs_remove(ns);
> -       ns_free(ns);    /* Free nandsim private resources */
> -       nand_release(chip); /* Unregister driver */
> -       kfree(ns);        /* Free other structures */
> -       ns_free_lists();
> +       WARN_ON(mtd_device_unregister(nsmtd));
> +       ns_free(ns);
> +       kfree(erase_block_wear);
> +       nand_cleanup(chip);
> +       list_for_each_safe(pos, n, &grave_pages) {
> +               kfree(list_entry(pos, struct grave_page, list));
> +               list_del(pos);

Are you sure you can use pos after freeing the entry?
Smells like use after free.

-- 
Thanks,
//richard

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 15/17] mtd: rawnand: nandsim: Manage lists on error in ns_init_module()
  2020-05-09 19:14 ` [PATCH 15/17] mtd: rawnand: nandsim: Manage lists on error in ns_init_module() Miquel Raynal
@ 2020-05-24 21:39   ` Richard Weinberger
  0 siblings, 0 replies; 31+ messages in thread
From: Richard Weinberger @ 2020-05-24 21:39 UTC (permalink / raw)
  To: Miquel Raynal
  Cc: Vignesh Raghavendra, Tudor Ambarus, dedekind, Richard Weinberger,
	Boris Brezillon, linux-mtd

On Sat, May 9, 2020 at 9:19 PM Miquel Raynal <miquel.raynal@bootlin.com> wrote:
>
> Lists are filled with calls to ns_parse_weakblocks(),
> ns_parse_weakpages() and ns_parse_gravepages(). Handle them in the
> error path, all at the same time.
>
> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> ---
>  drivers/mtd/nand/raw/nandsim.c | 23 +++++++++++++++++++----
>  1 file changed, 19 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
> index e41866e49206..26d23ab5b794 100644
> --- a/drivers/mtd/nand/raw/nandsim.c
> +++ b/drivers/mtd/nand/raw/nandsim.c
> @@ -2273,6 +2273,7 @@ static const struct nand_controller_ops ns_controller_ops = {
>   */
>  static int __init ns_init_module(void)
>  {
> +       struct list_head *pos, *n;
>         struct nand_chip *chip;
>         struct nandsim *ns;
>         int ret;
> @@ -2340,11 +2341,11 @@ static int __init ns_init_module(void)
>
>         ret = ns_parse_weakpages();
>         if (ret)
> -               goto error;
> +               goto free_wb_list;
>
>         ret = ns_parse_gravepages();
>         if (ret)
> -               goto error;
> +               goto free_wp_list;
>
>         nand_controller_init(&ns->base);
>         ns->base.ops = &ns_controller_ops;
> @@ -2353,7 +2354,7 @@ static int __init ns_init_module(void)
>         ret = nand_scan(chip, 1);
>         if (ret) {
>                 NS_ERR("Could not scan NAND Simulator device\n");
> -               goto error;
> +               goto free_gp_list;
>         }
>
>         if (overridesize) {
> @@ -2412,9 +2413,23 @@ static int __init ns_init_module(void)
>         kfree(erase_block_wear);
>  cleanup_nand:
>         nand_cleanup(chip);
> +free_gp_list:
> +       list_for_each_safe(pos, n, &grave_pages) {
> +               kfree(list_entry(pos, struct grave_page, list));
> +               list_del(pos);

Like said before, this pattern looks odd.

> +       }
> +free_wp_list:
> +       list_for_each_safe(pos, n, &weak_pages) {
> +               kfree(list_entry(pos, struct weak_page, list));
> +               list_del(pos);
> +       }
> +free_wb_list:
> +       list_for_each_safe(pos, n, &weak_blocks) {
> +               kfree(list_entry(pos, struct weak_block, list));
> +               list_del(pos);
> +       }
>  error:
>         kfree(ns);
> -       ns_free_lists();
>
>         return ret;
>  }
> --
> 2.20.1
>
>
> ______________________________________________________
> Linux MTD discussion mailing list
> http://lists.infradead.org/mailman/listinfo/linux-mtd/



-- 
Thanks,
//richard

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 17/17] mtd: rawnand: nandsim: Reorganize ns_cleanup_module()
  2020-05-24 21:37   ` Richard Weinberger
@ 2020-05-24 22:13     ` Miquel Raynal
  2020-05-25  6:46       ` Boris Brezillon
  0 siblings, 1 reply; 31+ messages in thread
From: Miquel Raynal @ 2020-05-24 22:13 UTC (permalink / raw)
  To: Richard Weinberger
  Cc: Vignesh Raghavendra, Tudor Ambarus, dedekind, Richard Weinberger,
	Boris Brezillon, linux-mtd

Hi Richard,

Richard Weinberger <richard.weinberger@gmail.com> wrote on Sun, 24 May
2020 23:37:13 +0200:

> On Sat, May 9, 2020 at 9:19 PM Miquel Raynal <miquel.raynal@bootlin.com> wrote:
> >         ns_debugfs_remove(ns);
> > -       ns_free(ns);    /* Free nandsim private resources */
> > -       nand_release(chip); /* Unregister driver */
> > -       kfree(ns);        /* Free other structures */
> > -       ns_free_lists();
> > +       WARN_ON(mtd_device_unregister(nsmtd));
> > +       ns_free(ns);
> > +       kfree(erase_block_wear);
> > +       nand_cleanup(chip);
> > +       list_for_each_safe(pos, n, &grave_pages) {
> > +               kfree(list_entry(pos, struct grave_page, list));
> > +               list_del(pos);  
> 
> Are you sure you can use pos after freeing the entry?
> Smells like use after free.
> 

Mmmmh, I should probably invert those two lines, first call list_del()
and then call kfree() on list_entry().

Thanks for noticing!
Miquèl

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 06/17] mtd: rawnand: nandsim: Remove debugfs entries at unload time
  2020-05-24 21:33   ` Richard Weinberger
@ 2020-05-24 22:14     ` Miquel Raynal
  0 siblings, 0 replies; 31+ messages in thread
From: Miquel Raynal @ 2020-05-24 22:14 UTC (permalink / raw)
  To: Richard Weinberger
  Cc: Vignesh Raghavendra, Tudor Ambarus, dedekind, Richard Weinberger,
	Boris Brezillon, linux-mtd

Hi Richard,

Richard Weinberger <richard.weinberger@gmail.com> wrote on Sun, 24 May
2020 23:33:06 +0200:

> On Sat, May 9, 2020 at 9:16 PM Miquel Raynal <miquel.raynal@bootlin.com> wrote:
> >
> > Create a ns_debugfs_remove() helper for that and call it in
> > ns_cleanup_module().
> >
> > Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> > ---
> >  drivers/mtd/nand/raw/nandsim.c | 6 ++++++
> >  1 file changed, 6 insertions(+)
> >
> > diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
> > index c8e9c70a6641..7862c65e32ad 100644
> > --- a/drivers/mtd/nand/raw/nandsim.c
> > +++ b/drivers/mtd/nand/raw/nandsim.c
> > @@ -520,6 +520,11 @@ static int ns_debugfs_create(struct nandsim *ns)
> >         return 0;
> >  }
> >
> > +static void ns_debugfs_remove(struct nandsim *ns)
> > +{
> > +       debugfs_remove(ns->dent);  
> 
> BTW: For now it is perfectly fine, but later this can be a
> debugfs_remove_recursive().

Ok, I'll use it in the v2!

> 
> > +}
> > +
> >  /*
> >   * Allocate array of page pointers, create slab allocation for an array
> >   * and initialize the array by NULL pointers.
> > @@ -2398,6 +2403,7 @@ static void __exit ns_cleanup_module(void)
> >         struct nandsim *ns = nand_get_controller_data(chip);
> >         int i;
> >
> > +       ns_debugfs_remove(ns);
> >         ns_free(ns);    /* Free nandsim private resources */
> >         nand_release(chip); /* Unregister driver */
> >         for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
> > --
> > 2.20.1
> >
> >
> > ______________________________________________________
> > Linux MTD discussion mailing list
> > http://lists.infradead.org/mailman/listinfo/linux-mtd/  
> 
> 
> 




Thanks,
Miquèl

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 17/17] mtd: rawnand: nandsim: Reorganize ns_cleanup_module()
  2020-05-24 22:13     ` Miquel Raynal
@ 2020-05-25  6:46       ` Boris Brezillon
  2020-05-25  6:47         ` Boris Brezillon
  0 siblings, 1 reply; 31+ messages in thread
From: Boris Brezillon @ 2020-05-25  6:46 UTC (permalink / raw)
  To: Miquel Raynal
  Cc: Vignesh Raghavendra, Tudor Ambarus, Richard Weinberger,
	Richard Weinberger, dedekind, linux-mtd

On Mon, 25 May 2020 00:13:28 +0200
Miquel Raynal <miquel.raynal@bootlin.com> wrote:

> Hi Richard,
> 
> Richard Weinberger <richard.weinberger@gmail.com> wrote on Sun, 24 May
> 2020 23:37:13 +0200:
> 
> > On Sat, May 9, 2020 at 9:19 PM Miquel Raynal <miquel.raynal@bootlin.com> wrote:  
> > >         ns_debugfs_remove(ns);
> > > -       ns_free(ns);    /* Free nandsim private resources */
> > > -       nand_release(chip); /* Unregister driver */
> > > -       kfree(ns);        /* Free other structures */
> > > -       ns_free_lists();
> > > +       WARN_ON(mtd_device_unregister(nsmtd));
> > > +       ns_free(ns);
> > > +       kfree(erase_block_wear);
> > > +       nand_cleanup(chip);
> > > +       list_for_each_safe(pos, n, &grave_pages) {
> > > +               kfree(list_entry(pos, struct grave_page, list));
> > > +               list_del(pos);    
> > 
> > Are you sure you can use pos after freeing the entry?
> > Smells like use after free.
> >   
> 
> Mmmmh, I should probably invert those two lines, first call list_del()
> and then call kfree() on list_entry().

You can also use  list_for_each_entry_safe():

	struct grave_page *pos, *n;

	...

	list_for_each_safe(pos, n, &grave_pages, list) {
		list_del(&pos->list);
		kfree(pos);
	}

> 
> Thanks for noticing!
> Miquèl


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 17/17] mtd: rawnand: nandsim: Reorganize ns_cleanup_module()
  2020-05-25  6:46       ` Boris Brezillon
@ 2020-05-25  6:47         ` Boris Brezillon
  2020-05-25  7:23           ` Miquel Raynal
  0 siblings, 1 reply; 31+ messages in thread
From: Boris Brezillon @ 2020-05-25  6:47 UTC (permalink / raw)
  To: Miquel Raynal
  Cc: Vignesh Raghavendra, Tudor Ambarus, Richard Weinberger,
	Richard Weinberger, dedekind, linux-mtd

On Mon, 25 May 2020 08:46:37 +0200
Boris Brezillon <boris.brezillon@collabora.com> wrote:

> On Mon, 25 May 2020 00:13:28 +0200
> Miquel Raynal <miquel.raynal@bootlin.com> wrote:
> 
> > Hi Richard,
> > 
> > Richard Weinberger <richard.weinberger@gmail.com> wrote on Sun, 24 May
> > 2020 23:37:13 +0200:
> >   
> > > On Sat, May 9, 2020 at 9:19 PM Miquel Raynal <miquel.raynal@bootlin.com> wrote:    
> > > >         ns_debugfs_remove(ns);
> > > > -       ns_free(ns);    /* Free nandsim private resources */
> > > > -       nand_release(chip); /* Unregister driver */
> > > > -       kfree(ns);        /* Free other structures */
> > > > -       ns_free_lists();
> > > > +       WARN_ON(mtd_device_unregister(nsmtd));
> > > > +       ns_free(ns);
> > > > +       kfree(erase_block_wear);
> > > > +       nand_cleanup(chip);
> > > > +       list_for_each_safe(pos, n, &grave_pages) {
> > > > +               kfree(list_entry(pos, struct grave_page, list));
> > > > +               list_del(pos);      
> > > 
> > > Are you sure you can use pos after freeing the entry?
> > > Smells like use after free.
> > >     
> > 
> > Mmmmh, I should probably invert those two lines, first call list_del()
> > and then call kfree() on list_entry().  
> 
> You can also use  list_for_each_entry_safe():
> 
> 	struct grave_page *pos, *n;
> 
> 	...
> 
> 	list_for_each_safe(pos, n, &grave_pages, list) {

	list_for_each_entry_safe(pos, n, &grave_pages, list) {

> 		list_del(&pos->list);
> 		kfree(pos);
> 	}
> 
> > 
> > Thanks for noticing!
> > Miquèl  
> 


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 17/17] mtd: rawnand: nandsim: Reorganize ns_cleanup_module()
  2020-05-25  6:47         ` Boris Brezillon
@ 2020-05-25  7:23           ` Miquel Raynal
  2020-05-25  8:28             ` Miquel Raynal
  0 siblings, 1 reply; 31+ messages in thread
From: Miquel Raynal @ 2020-05-25  7:23 UTC (permalink / raw)
  To: Boris Brezillon
  Cc: Vignesh Raghavendra, Tudor Ambarus, Richard Weinberger,
	Richard Weinberger, dedekind, linux-mtd

Hi Boris,

Boris Brezillon <boris.brezillon@collabora.com> wrote on Mon, 25 May
2020 08:47:35 +0200:

> On Mon, 25 May 2020 08:46:37 +0200
> Boris Brezillon <boris.brezillon@collabora.com> wrote:
> 
> > On Mon, 25 May 2020 00:13:28 +0200
> > Miquel Raynal <miquel.raynal@bootlin.com> wrote:
> >   
> > > Hi Richard,
> > > 
> > > Richard Weinberger <richard.weinberger@gmail.com> wrote on Sun, 24 May
> > > 2020 23:37:13 +0200:
> > >     
> > > > On Sat, May 9, 2020 at 9:19 PM Miquel Raynal <miquel.raynal@bootlin.com> wrote:      
> > > > >         ns_debugfs_remove(ns);
> > > > > -       ns_free(ns);    /* Free nandsim private resources */
> > > > > -       nand_release(chip); /* Unregister driver */
> > > > > -       kfree(ns);        /* Free other structures */
> > > > > -       ns_free_lists();
> > > > > +       WARN_ON(mtd_device_unregister(nsmtd));
> > > > > +       ns_free(ns);
> > > > > +       kfree(erase_block_wear);
> > > > > +       nand_cleanup(chip);
> > > > > +       list_for_each_safe(pos, n, &grave_pages) {
> > > > > +               kfree(list_entry(pos, struct grave_page, list));
> > > > > +               list_del(pos);        
> > > > 
> > > > Are you sure you can use pos after freeing the entry?
> > > > Smells like use after free.
> > > >       
> > > 
> > > Mmmmh, I should probably invert those two lines, first call list_del()
> > > and then call kfree() on list_entry().    
> > 
> > You can also use  list_for_each_entry_safe():

I usually use this helper, but I guess I copy/pasted the below lines
from somewhere else in this file... I'll use list_for_each_entry_safe().

> > 
> > 	struct grave_page *pos, *n;
> > 
> > 	...
> > 
> > 	list_for_each_safe(pos, n, &grave_pages, list) {  
> 
> 	list_for_each_entry_safe(pos, n, &grave_pages, list) {
> 
> > 		list_del(&pos->list);
> > 		kfree(pos);
> > 	}
> >   
> > > 
> > > Thanks for noticing!
> > > Miquèl    
> >   
> 

Thanks,
Miquèl

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 17/17] mtd: rawnand: nandsim: Reorganize ns_cleanup_module()
  2020-05-25  7:23           ` Miquel Raynal
@ 2020-05-25  8:28             ` Miquel Raynal
  2020-05-25  8:35               ` Boris Brezillon
  0 siblings, 1 reply; 31+ messages in thread
From: Miquel Raynal @ 2020-05-25  8:28 UTC (permalink / raw)
  To: Boris Brezillon
  Cc: Vignesh Raghavendra, Tudor Ambarus, Richard Weinberger,
	Richard Weinberger, dedekind, linux-mtd


Miquel Raynal <miquel.raynal@bootlin.com> wrote on Mon, 25 May 2020
09:23:15 +0200:

> Hi Boris,
> 
> Boris Brezillon <boris.brezillon@collabora.com> wrote on Mon, 25 May
> 2020 08:47:35 +0200:
> 
> > On Mon, 25 May 2020 08:46:37 +0200
> > Boris Brezillon <boris.brezillon@collabora.com> wrote:
> >   
> > > On Mon, 25 May 2020 00:13:28 +0200
> > > Miquel Raynal <miquel.raynal@bootlin.com> wrote:
> > >     
> > > > Hi Richard,
> > > > 
> > > > Richard Weinberger <richard.weinberger@gmail.com> wrote on Sun, 24 May
> > > > 2020 23:37:13 +0200:
> > > >       
> > > > > On Sat, May 9, 2020 at 9:19 PM Miquel Raynal <miquel.raynal@bootlin.com> wrote:        
> > > > > >         ns_debugfs_remove(ns);
> > > > > > -       ns_free(ns);    /* Free nandsim private resources */
> > > > > > -       nand_release(chip); /* Unregister driver */
> > > > > > -       kfree(ns);        /* Free other structures */
> > > > > > -       ns_free_lists();
> > > > > > +       WARN_ON(mtd_device_unregister(nsmtd));
> > > > > > +       ns_free(ns);
> > > > > > +       kfree(erase_block_wear);
> > > > > > +       nand_cleanup(chip);
> > > > > > +       list_for_each_safe(pos, n, &grave_pages) {
> > > > > > +               kfree(list_entry(pos, struct grave_page, list));
> > > > > > +               list_del(pos);          
> > > > > 
> > > > > Are you sure you can use pos after freeing the entry?
> > > > > Smells like use after free.
> > > > >         
> > > > 
> > > > Mmmmh, I should probably invert those two lines, first call list_del()
> > > > and then call kfree() on list_entry().      
> > > 
> > > You can also use  list_for_each_entry_safe():  
> 
> I usually use this helper, but I guess I copy/pasted the below lines
> from somewhere else in this file... I'll use list_for_each_entry_safe().

Actually, grave_pages, weak_pages and weak_blocks are three structures
of different types, that's why they called kfree() directly on
list_entry() -> to avoid having to declare 6 different pointers. I'll
stick to the same presentation than ns_free_lists then.

> 
> > > 
> > > 	struct grave_page *pos, *n;
> > > 
> > > 	...
> > > 
> > > 	list_for_each_safe(pos, n, &grave_pages, list) {    
> > 
> > 	list_for_each_entry_safe(pos, n, &grave_pages, list) {
> >   
> > > 		list_del(&pos->list);
> > > 		kfree(pos);
> > > 	}
> > >     
> > > > 
> > > > Thanks for noticing!
> > > > Miquèl      
> > >     
> >   
> 
> Thanks,
> Miquèl

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: [PATCH 17/17] mtd: rawnand: nandsim: Reorganize ns_cleanup_module()
  2020-05-25  8:28             ` Miquel Raynal
@ 2020-05-25  8:35               ` Boris Brezillon
  0 siblings, 0 replies; 31+ messages in thread
From: Boris Brezillon @ 2020-05-25  8:35 UTC (permalink / raw)
  To: Miquel Raynal
  Cc: Vignesh Raghavendra, Tudor Ambarus, Richard Weinberger,
	Richard Weinberger, dedekind, linux-mtd

On Mon, 25 May 2020 10:28:52 +0200
Miquel Raynal <miquel.raynal@bootlin.com> wrote:

> Miquel Raynal <miquel.raynal@bootlin.com> wrote on Mon, 25 May 2020
> 09:23:15 +0200:
> 
> > Hi Boris,
> > 
> > Boris Brezillon <boris.brezillon@collabora.com> wrote on Mon, 25 May
> > 2020 08:47:35 +0200:
> >   
> > > On Mon, 25 May 2020 08:46:37 +0200
> > > Boris Brezillon <boris.brezillon@collabora.com> wrote:
> > >     
> > > > On Mon, 25 May 2020 00:13:28 +0200
> > > > Miquel Raynal <miquel.raynal@bootlin.com> wrote:
> > > >       
> > > > > Hi Richard,
> > > > > 
> > > > > Richard Weinberger <richard.weinberger@gmail.com> wrote on Sun, 24 May
> > > > > 2020 23:37:13 +0200:
> > > > >         
> > > > > > On Sat, May 9, 2020 at 9:19 PM Miquel Raynal <miquel.raynal@bootlin.com> wrote:          
> > > > > > >         ns_debugfs_remove(ns);
> > > > > > > -       ns_free(ns);    /* Free nandsim private resources */
> > > > > > > -       nand_release(chip); /* Unregister driver */
> > > > > > > -       kfree(ns);        /* Free other structures */
> > > > > > > -       ns_free_lists();
> > > > > > > +       WARN_ON(mtd_device_unregister(nsmtd));
> > > > > > > +       ns_free(ns);
> > > > > > > +       kfree(erase_block_wear);
> > > > > > > +       nand_cleanup(chip);
> > > > > > > +       list_for_each_safe(pos, n, &grave_pages) {
> > > > > > > +               kfree(list_entry(pos, struct grave_page, list));
> > > > > > > +               list_del(pos);            
> > > > > > 
> > > > > > Are you sure you can use pos after freeing the entry?
> > > > > > Smells like use after free.
> > > > > >           
> > > > > 
> > > > > Mmmmh, I should probably invert those two lines, first call list_del()
> > > > > and then call kfree() on list_entry().        
> > > > 
> > > > You can also use  list_for_each_entry_safe():    
> > 
> > I usually use this helper, but I guess I copy/pasted the below lines
> > from somewhere else in this file... I'll use list_for_each_entry_safe().  
> 
> Actually, grave_pages, weak_pages and weak_blocks are three structures
> of different types, that's why they called kfree() directly on
> list_entry() -> to avoid having to declare 6 different pointers. I'll
> stick to the same presentation than ns_free_lists then.

Hm, okay. I guess having the init/cleanup split is sub-functions would
be cleaner, but it's not like we want to invest time in nandsim, so I'm
fine with the list_for_each_safe().

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2020-05-25  8:35 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-09 19:14 [PATCH 00/17] Clean nandsim error path Miquel Raynal
2020-05-09 19:14 ` [PATCH 01/17] mtd: rawnand: nandsim: Consistent use of 'ns' instead of 'dev' Miquel Raynal
2020-05-09 19:14 ` [PATCH 02/17] mtd: rawnand: nandsim: Use octal permissions Miquel Raynal
2020-05-09 19:14 ` [PATCH 03/17] mtd: rawnand: nandsim: Use a consistent ns_ prefix for all functions Miquel Raynal
2020-05-09 19:14 ` [PATCH 04/17] mtd: rawnand: nandsim: Clean error handling Miquel Raynal
2020-05-09 19:14 ` [PATCH 05/17] mtd: rawnand: nandsim: Keep track of the created debugfs entries Miquel Raynal
2020-05-09 19:14 ` [PATCH 06/17] mtd: rawnand: nandsim: Remove debugfs entries at unload time Miquel Raynal
2020-05-09 20:43   ` Richard Weinberger
2020-05-09 22:29     ` Miquel Raynal
2020-05-24 21:27       ` Richard Weinberger
2020-05-24 21:33   ` Richard Weinberger
2020-05-24 22:14     ` Miquel Raynal
2020-05-09 19:14 ` [PATCH 07/17] mtd: rawnand: nandsim: Fix the two ns_alloc_device() error paths Miquel Raynal
2020-05-09 19:14 ` [PATCH 08/17] mtd: rawnand: nandsim: Free partition names on error in ns_init() Miquel Raynal
2020-05-09 19:14 ` [PATCH 09/17] mtd: rawnand: nandsim: Free the allocated device " Miquel Raynal
2020-05-09 19:14 ` [PATCH 10/17] mtd: rawnand: nandsim: Free the partition names in ns_free() Miquel Raynal
2020-05-09 19:14 ` [PATCH 11/17] mtd: rawnand: nandsim: Stop using nand_release() Miquel Raynal
2020-05-09 19:14 ` [PATCH 12/17] mtd: rawnand: nandsim: Use an additional label when freeing the nandsim object Miquel Raynal
2020-05-09 19:14 ` [PATCH 13/17] mtd: rawnand: nandsim: Free erase_block_wear on error Miquel Raynal
2020-05-09 19:14 ` [PATCH 14/17] mtd: rawnand: nandsim: Fix the label pointing on nand_cleanup() Miquel Raynal
2020-05-09 19:14 ` [PATCH 15/17] mtd: rawnand: nandsim: Manage lists on error in ns_init_module() Miquel Raynal
2020-05-24 21:39   ` Richard Weinberger
2020-05-09 19:14 ` [PATCH 16/17] mtd: rawnand: nandsim: Rename a label " Miquel Raynal
2020-05-09 19:14 ` [PATCH 17/17] mtd: rawnand: nandsim: Reorganize ns_cleanup_module() Miquel Raynal
2020-05-24 21:37   ` Richard Weinberger
2020-05-24 22:13     ` Miquel Raynal
2020-05-25  6:46       ` Boris Brezillon
2020-05-25  6:47         ` Boris Brezillon
2020-05-25  7:23           ` Miquel Raynal
2020-05-25  8:28             ` Miquel Raynal
2020-05-25  8:35               ` Boris Brezillon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).