Skip to contentSkip to conversation

UBI: Use the whole NAND device to calculate max bad block number

PATCH In review Richard Genoud 3 participants 45 messages Jul 10, 2012 – Aug 20, 2012

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover UBI: Use the whole NAND device to calculate max bad block number

Hi,

This patch serie applies on top of Shmulik Ladkani's patch serie:
"[PATCH 0/5] ubi: Fix bad PEBs reserve caclulation" (and on mtd-artem git tree)

The goal here is to make UBI reserve a number of PEB for bad block dependant of the whole NAND chipset size, and not of the MTD partition size.

As explained in patch 3/4, the NAND manufacturers only tell how many bad blocks there will be in the worst case on a NAND device, but not that these blocks will be equally disposed on the flash.
I.E. if a NAND flash admits at max 40 bad blocks, and is cutted in two equal MTD partitions, it doesn't mean that there will be at max only 20 bad blocks per partition.
The 40 BEBs could be all on the first partition.

So, for each MTD partition, UBI should reserved the maximum expected number of bad erase blocks.

Patches 1/2/3 are making that happend with a kernel option MTD_UBI_BEB_LIMIT Then, patch 4 replace this option with user-space parameters (kernel parameter and a UBI_IOCATT ioctl)

The patch on mtd-utils follows the serie.

Richard Genoud (4):
mtd_is_partition: struct mtd_info should be const MTD parts: introduce mtd_get_device_size()
UBI: use the whole MTD device size to get bad_peb_limit
UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter

 drivers/mtd/mtdpart.c          |   16 ++++++++-
 drivers/mtd/ubi/Kconfig        |   19 ----------
 drivers/mtd/ubi/build.c        |   73 ++++++++++++++++++++++++++++++---------
 drivers/mtd/ubi/cdev.c         |    9 ++++-
 drivers/mtd/ubi/ubi.h          |    6 +++-
 include/linux/mtd/partitions.h |    3 +-
 include/mtd/ubi-user.h         |   19 ++++++++++-
 7 files changed, 104 insertions(+), 41 deletions(-)
Signature · 1 line
1.7.2.5
#1 lore raw

PATCH 1/4 mtd_is_partition: struct mtd_info should be const

struct mtd_info is not modified by mtd_is_partition so it should be a const.

---
 drivers/mtd/mtdpart.c          |    2 +-
 include/linux/mtd/partitions.h |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/mtd/mtdpart.c +1 -1
index d518e4d..8500584 100644
@@ -744,7 +744,7 @@ int parse_mtd_partitions(struct mtd_info *master, const char **types,
 	return ret;
 }
 
-int mtd_is_partition(struct mtd_info *mtd)
+int mtd_is_partition(const struct mtd_info *mtd)
 {
 	struct mtd_part *part;
 	int ispart = 0;
include/linux/mtd/partitions.h +1 -1
index 2475228..02a5115 100644
@@ -79,7 +79,7 @@ struct mtd_part_parser {
 extern int register_mtd_parser(struct mtd_part_parser *parser);
 extern int deregister_mtd_parser(struct mtd_part_parser *parser);
 
-int mtd_is_partition(struct mtd_info *mtd);
+int mtd_is_partition(const struct mtd_info *mtd);
 int mtd_add_partition(struct mtd_info *master, char *name,
 		      long long offset, long long length);
 int mtd_del_partition(struct mtd_info *master, int partno);
Signature · 1 line
1.7.2.5
#3 lore raw

PATCH 2/4 MTD parts: introduce mtd_get_device_size()

In reply to Richard Genoud

mtd_get_device_size() returns the size of the whole MTD device, that is the mtd_info master size.
This is used by UBI to calculate the maximum number of bad blocks (MBB) on a MTD device.

---
 drivers/mtd/mtdpart.c          |   14 ++++++++++++++
 include/linux/mtd/partitions.h |    1 +
 2 files changed, 15 insertions(+), 0 deletions(-)
drivers/mtd/mtdpart.c +14 -0
index 8500584..d12f583 100644
@@ -760,3 +760,17 @@ int mtd_is_partition(const struct mtd_info *mtd)
 	return ispart;
 }
 EXPORT_SYMBOL_GPL(mtd_is_partition);
+
+/* returns the size of an MTD device */
+uint64_t mtd_get_device_size(const struct mtd_info *mtd)
+{
+	struct mtd_part *part;
+
+	if (!mtd_is_partition(mtd))
+		return mtd->size;
+
+	part = PART(mtd);
+
+	return part->master->size;
+}
+EXPORT_SYMBOL_GPL(mtd_get_device_size);
include/linux/mtd/partitions.h +1 -0
index 02a5115..1f8d24b 100644
@@ -83,5 +83,6 @@ int mtd_is_partition(const struct mtd_info *mtd);
 int mtd_add_partition(struct mtd_info *master, char *name,
 		      long long offset, long long length);
 int mtd_del_partition(struct mtd_info *master, int partno);
+uint64_t mtd_get_device_size(const struct mtd_info *mtd);
 
 #endif
Signature · 1 line
1.7.2.5
#4 lore raw

PATCH 3/4 UBI: use the whole MTD device size to get bad_peb_limit

In reply to Richard Genoud

On NAND flash devices, UBI reserves some physical erase blocks (PEB) for bad block handling.
Today, the number of reserved PEB can only be set as a percentage of the total number of PEB in each MTD partition.
For example, for a NAND flash with 128KiB PEB, 2 MTD partition of 20MiB (mtd0) and 100MiB (mtd1) and 2% reserved PEB:
- the UBI device on mtd0 will have 2 PEB reserved
- the UBI device on mtd1 will have 16 PEB reserved

The problem with this behaviour is that NAND flash manufacturers give a minimum number of valid block (NVB) during the endurance life of the device.
E.G.:
Parameter Symbol Min Max Unit Notes
--------------------------------------------------------------
Valid block number NVB 1004 1024 Blocks 1
Note:
1. Invalid blocks are block that contain one or more bad bits beyond ECC. The device may contain bad blocks upon shipment. Additional bad blocks may develop over time; however, the total number of available blocks will not drop below NVB during the endurance life of the device.

Quoted 1 line

From this number we can deduce the maximum number of bad PEB that a

device will contain during its endurance life :
A 128MiB NAND flash (1024 PEB) will not have less than 20 bad blocks during the flash endurance life.

BUT, the manufacturer doesn't tell where those bad block will appear. He doesn't say either if they will be equally disposed on the whole device (and I'm pretty sure they won't).
So, according to the datasheets, we should reserve the maximum number of bad PEB for each UBI device.
(Worst case scenario: 20 bad blocks appears on the smallest MTD partition.)

So this patch make UBI use the whole MTD device size to calculate the Maximum bad expected eraseblocks.

The Kconfig option is in per1024 blocks, thus it can have a default value of 20 which is *very* common for NAND devices.

---
 drivers/mtd/ubi/Kconfig |   14 ++++++++++----
 drivers/mtd/ubi/build.c |   17 ++++++++++++++---
 2 files changed, 24 insertions(+), 7 deletions(-)
drivers/mtd/ubi/Kconfig +10 -4
index 145cda2..b77ffe1 100644
@@ -28,15 +28,21 @@ config MTD_UBI_WL_THRESHOLD
 	  to 128 or 256, although it does not have to be power of 2).
 
 config MTD_UBI_BEB_LIMIT
-	int "Percentage of maximum expected bad eraseblocks"
-	default 2
-	range 0 25
+	int "Maximum expected bad eraseblocks per 1024 eraseblocks"
+	default 20
+	range 2 256
 	help
 	  If the MTD device admits of bad eraseblocks (e.g. NAND flash), UBI
 	  reserves some amount of physical eraseblocks to handle new bad
 	  eraseblocks.
 	  This option specifies the maximum bad eraseblocks UBI expects on the
-	  ubi device (percents of total number of flash eraseblocks).
+	  ubi device per 1024 eraseblocks.
+	  This value is often given in an other form in the NAND datasheet
+	  (min NVB i.e. minimal number of valid blocks). The maximum expected
+	  bad eraseblocks per 1024 is then:
+	    1024 * (1 - MinNVB / MaxNVB)
+	  Which gives 20 for most NAND devices.
+
 	  This limit is used in order to derive amount of eraseblock UBI
 	  reserves for handling new bad blocks.
 	  If the device has more bad eraseblocks than this limit, UBI does not
drivers/mtd/ubi/build.c +14 -3
index 62cc6ce..87b39c2 100644
@@ -36,6 +36,7 @@
 #include <linux/namei.h>
 #include <linux/stat.h>
 #include <linux/miscdevice.h>
+#include <linux/mtd/partitions.h>
 #include <linux/log2.h>
 #include <linux/kthread.h>
 #include <linux/kernel.h>
@@ -610,12 +611,22 @@ static int io_init(struct ubi_device *ubi)
 	if (mtd_can_have_bb(ubi->mtd)) {
 		ubi->bad_allowed = 1;
 		if (CONFIG_MTD_UBI_BEB_LIMIT > 0) {
-			int percent = CONFIG_MTD_UBI_BEB_LIMIT;
+			int per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
+			int device_peb_count;
+			uint64_t device_size;
 			int beb_limit;
 
-			beb_limit = mult_frac(ubi->peb_count, percent, 100);
+			/* we are using here the whole MTD device size and not
+			 * the MTD partition size because the maximum number of
+			 * bad blocks is a percentage of the whole device and
+			 * the bad blocks are not fairly disposed on a flash
+			 * device
+			 */
+			device_size = mtd_get_device_size(ubi->mtd);
+			device_peb_count = mtd_div_by_eb(device_size, ubi->mtd);
+			beb_limit = mult_frac(device_peb_count, per1024, 1024);
 			/* round it up */
-			if (mult_frac(beb_limit, 100, percent) < ubi->peb_count)
+			if (mult_frac(beb_limit, 1024, per1024) < ubi->peb_count)
 				beb_limit++;
 			ubi->bad_peb_limit = beb_limit;
 		}
Signature · 1 line
1.7.2.5
#18 lore raw

PATCH 1/1 ubiattach: introduce max_beb_per1024 in UBI_IOCATT

In reply to Richard Genoud

The ioctl UBI_IOCATT has been extended with max_beb_per1024 parameter.

This parameter is used for adjusting the "maximum expected number of bad blocks per 1024 blocks" for each mtd device.
The number of physical erase blocks (PEB) that UBI will reserve for bad block handling is now:
whole_flash_chipset__PEB_number * max_beb_per1024 / 1024

This means that for a 4096 PEB NAND device with 3 MTD partitions:
mtd0: 512 PEB
mtd1: 1536 PEB
mtd2: 2048 PEB

the commands:
ubiattach -m 0 -d 0 -b 20 /dev/ubi_ctrl
ubiattach -m 1 -d 1 -b 20 /dev/ubi_ctrl
ubiattach -m 2 -d 2 -b 20 /dev/ubi_ctrl
will attach mtdx to UBIx and reserve:
80 PEB for bad block handling on UBI0
80 PEB for bad block handling on UBI1
80 PEB for bad block handling on UBI2

=> for the whole device, 240 PEB will be reserved for bad block handling.

This may seems a waste of space, but as far as the bad blocks can appear every where on a flash device, in the worst case scenario they can all appear in one MTD partition.
So the maximum number of expected erase blocks given by the NAND manufacturer should be reserve on each MTD partition.

---
 include/mtd/ubi-user.h             |   20 ++++++++++++++++-
 tests/fs-tests/integrity/integck.c |    1 +
 ubi-utils/include/libubi.h         |    2 +
 ubi-utils/libubi.c                 |    2 +
 ubi-utils/ubiattach.c              |   41 +++++++++++++++++++++++++++++-------
 5 files changed, 57 insertions(+), 9 deletions(-)
include/mtd/ubi-user.h +19 -1
index 296efae..5f77906 100644
@@ -235,6 +235,7 @@ enum {
  * @ubi_num: UBI device number to create
  * @mtd_num: MTD device number to attach
  * @vid_hdr_offset: VID header offset (use defaults if %0)
+ * @max_beb_per1024: Maximum expected bad eraseblocks per 1024 eraseblocks
  * @padding: reserved for future, not used, has to be zeroed
  *
  * This data structure is used to specify MTD device UBI has to attach and the
@@ -258,12 +259,29 @@ enum {
  * be 2KiB-64 bytes = 1984. Note, that this position is not even 512-bytes
  * aligned, which is OK, as UBI is clever enough to realize this is 4th
  * sub-page of the first page and add needed padding.
+ *
+ * The @max_beb_per1024 is the maximum bad eraseblocks UBI expects on the ubi
+ * device per 1024 eraseblocks.
+ * This value is often given in an other form in the NAND datasheet (min NVB
+ * i.e. minimal number of valid blocks). The maximum expected bad eraseblocks
+ * per 1024 is then:
+ *   1024 * (1 - MinNVB / MaxNVB)
+ * Which gives 20 for most NAND devices.
+ * This limit is used in order to derive amount of eraseblock UBI reserves for
+ * handling new bad blocks.
+ * If the device has more bad eraseblocks than this limit, UBI does not reserve
+ * any physical eraseblocks for new bad eraseblocks, but attempts to use
+ * available eraseblocks (if any).
+ * The accepted range is 0-255. If 0 is given, the default kernel value
+ * MTD_UBI_DEFAULT_BEB_LIMIT will be used for compatibility with previous
+ * versions.
  */
 struct ubi_attach_req {
 	int32_t ubi_num;
 	int32_t mtd_num;
 	int32_t vid_hdr_offset;
-	int8_t padding[12];
+	int8_t max_beb_per1024;
+	int8_t padding[11];
 };
 
 /**
tests/fs-tests/integrity/integck.c +1 -0
index 30322cd..f12dfac 100644
@@ -3152,6 +3152,7 @@ static int reattach(void)
 	req.mtd_num = args.mtdn;
 	req.vid_hdr_offset = 0;
 	req.mtd_dev_node = NULL;
+	req.max_beb_per1024 = 0;
 
 	err = ubi_attach(libubi, "/dev/ubi_ctrl", &req);
 	if (err)
ubi-utils/include/libubi.h +2 -0
index dc03d02..1eadff8 100644
@@ -50,6 +50,7 @@ typedef void * libubi_t;
  * @mtd_dev_node: path to MTD device node to attach
  * @vid_hdr_offset: VID header offset (%0 means default offset and this is what
  *                  most of the users want)
+ * @max_beb_per1024: Maximum expected bad eraseblocks per 1024 eraseblocks
  */
 struct ubi_attach_request
 {
@@ -57,6 +58,7 @@ struct ubi_attach_request
 	int mtd_num;
 	const char *mtd_dev_node;
 	int vid_hdr_offset;
+	unsigned char max_beb_per1024;
 };
 
 /**
ubi-utils/libubi.c +2 -0
index c898e36..d3c333d 100644
@@ -719,6 +719,7 @@ int ubi_attach_mtd(libubi_t desc, const char *node,
 	r.ubi_num = req->dev_num;
 	r.mtd_num = req->mtd_num;
 	r.vid_hdr_offset = req->vid_hdr_offset;
+	r.max_beb_per1024 = req->max_beb_per1024;
 
 	ret = do_attach(node, &r);
 	if (ret == 0)
@@ -780,6 +781,7 @@ int ubi_attach(libubi_t desc, const char *node, struct ubi_attach_request *req)
 	memset(&r, 0, sizeof(struct ubi_attach_req));
 	r.ubi_num = req->dev_num;
 	r.vid_hdr_offset = req->vid_hdr_offset;
+	r.max_beb_per1024 = req->max_beb_per1024;
 
 	/*
 	 * User has passed path to device node. Lets find out MTD device number
ubi-utils/ubiattach.c +33 -8

89 lines not on this page · raw message

Signature · 1 line
1.7.2.5
#20 lore raw

PATCH 4/4 UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter

In reply to Richard Genoud

This patch provides the possibility to adjust the "maximum expected number of bad blocks per 1024 blocks" (max_beb_per1024) for each mtd device.

The majority of NAND devices have their max_beb_per1024 equal to 20, but sometimes it's more.
Now, we can adjust that via a kernel parameter:
ubi.mtd=<name|num|path>[,<vid_hdr_offs>[,max_beb_per1024]] and via UBI_IOCATT ioctl which is now:

struct ubi_attach_req {
	__s32 ubi_num;
	__s32 mtd_num;
	__s32 vid_hdr_offset;
	__u8 max_beb_per1024;
	__s8 padding[11];
};
---
 drivers/mtd/ubi/Kconfig |   25 --------------
 drivers/mtd/ubi/build.c |   82 +++++++++++++++++++++++++++++++---------------
 drivers/mtd/ubi/cdev.c  |    9 ++++-
 drivers/mtd/ubi/ubi.h   |    6 +++-
 include/mtd/ubi-user.h  |   19 ++++++++++-
 5 files changed, 86 insertions(+), 55 deletions(-)
drivers/mtd/ubi/Kconfig +0 -25
index b77ffe1..7a57cc0 100644
@@ -27,31 +27,6 @@ config MTD_UBI_WL_THRESHOLD
 	  life-cycle less than 10000, the threshold should be lessened (e.g.,
 	  to 128 or 256, although it does not have to be power of 2).
 
-config MTD_UBI_BEB_LIMIT
-	int "Maximum expected bad eraseblocks per 1024 eraseblocks"
-	default 20
-	range 2 256
-	help
-	  If the MTD device admits of bad eraseblocks (e.g. NAND flash), UBI
-	  reserves some amount of physical eraseblocks to handle new bad
-	  eraseblocks.
-	  This option specifies the maximum bad eraseblocks UBI expects on the
-	  ubi device per 1024 eraseblocks.
-	  This value is often given in an other form in the NAND datasheet
-	  (min NVB i.e. minimal number of valid blocks). The maximum expected
-	  bad eraseblocks per 1024 is then:
-	    1024 * (1 - MinNVB / MaxNVB)
-	  Which gives 20 for most NAND devices.
-
-	  This limit is used in order to derive amount of eraseblock UBI
-	  reserves for handling new bad blocks.
-	  If the device has more bad eraseblocks than this limit, UBI does not
-	  reserve any physical eraseblocks for new bad eraseblocks, but
-	  attempts to use available eraseblocks (if any).
-	  If the underlying flash does not admit of bad eraseblocks (e.g. NOR
-	  flash), this value is ignored.
-	  Leave the default value if unsure.
-
 config MTD_UBI_GLUEBI
 	tristate "MTD devices emulation driver (gluebi)"
 	help
drivers/mtd/ubi/build.c +55 -27

172 lines not on this page · raw message

drivers/mtd/ubi/cdev.c +8 -1
index acec85d..ed5dd75 100644
@@ -1005,12 +1005,19 @@ static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd,
 			break;
 		}
 
+		/* for compatibility with older user-space tools,
+		 * a zero value should be treated like a default value
+		 */
+		if (!req.max_beb_per1024)
+			req.max_beb_per1024 = MTD_UBI_DEFAULT_BEB_LIMIT;
+
 		/*
 		 * Note, further request verification is done by
 		 * 'ubi_attach_mtd_dev()'.
 		 */
 		mutex_lock(&ubi_devices_mutex);
-		err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset);
+		err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset,
+					 req.max_beb_per1024);
 		mutex_unlock(&ubi_devices_mutex);
 		if (err < 0)
 			put_mtd_device(mtd);
drivers/mtd/ubi/ubi.h +5 -1
index c94612e..2148f35 100644
@@ -50,6 +50,9 @@
 /* UBI name used for character devices, sysfs, etc */
 #define UBI_NAME_STR "ubi"
 
+/* Default number of maximum expected bad blocks per 1024 eraseblocks */
+#define MTD_UBI_DEFAULT_BEB_LIMIT 20
+
 /* Normal UBI messages */
 #define ubi_msg(fmt, ...) printk(KERN_NOTICE "UBI: " fmt "\n", ##__VA_ARGS__)
 /* UBI warning messages */
@@ -693,7 +696,8 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
 			 struct ubi_vid_hdr *vid_hdr);
 
 /* build.c */
-int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset);
+int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
+		       int vid_hdr_offset, int max_beb_per1024);
 int ubi_detach_mtd_dev(int ubi_num, int anyway);
 struct ubi_device *ubi_get_device(int ubi_num);
 void ubi_put_device(struct ubi_device *ubi);
include/mtd/ubi-user.h +18 -1
index 8787349..77cd0d1 100644
@@ -222,6 +222,7 @@ enum {
  * @ubi_num: UBI device number to create
  * @mtd_num: MTD device number to attach
  * @vid_hdr_offset: VID header offset (use defaults if %0)
+ * @max_beb_per1024: Maximum expected bad eraseblocks per 1024 eraseblocks
  * @padding: reserved for future, not used, has to be zeroed
  *
  * This data structure is used to specify MTD device UBI has to attach and the
@@ -245,12 +246,28 @@ enum {
  * be 2KiB-64 bytes = 1984. Note, that this position is not even 512-bytes
  * aligned, which is OK, as UBI is clever enough to realize this is 4th
  * sub-page of the first page and add needed padding.
+ *
+ * The @max_beb_per1024 is the maximum bad eraseblocks UBI expects on the ubi
+ * device per 1024 eraseblocks.
+ * This value is often given in an other form in the NAND datasheet (min NVB
+ * i.e. minimal number of valid blocks). The maximum expected bad eraseblocks
+ * per 1024 is then:
+ *   1024 * (1 - MinNVB / MaxNVB)
+ * Which gives 20 for most NAND devices.
+ * This limit is used in order to derive amount of eraseblock UBI reserves for
+ * handling new bad blocks.
+ * If the device has more bad eraseblocks than this limit, UBI does not reserve
+ * any physical eraseblocks for new bad eraseblocks, but attempts to use
+ * available eraseblocks (if any).
+ * The accepted range is 0-255. If 0 is given, the default value
+ * MTD_UBI_DEFAULT_BEB_LIMIT will be used for compatibility.
  */
 struct ubi_attach_req {
 	__s32 ubi_num;
 	__s32 mtd_num;
 	__s32 vid_hdr_offset;
-	__s8 padding[12];
+	__u8 max_beb_per1024;
+	__s8 padding[11];
 };
 
 /**
Signature · 1 line
1.7.2.5
#5 lore raw

Re: [PATCH 3/4] UBI: use the whole MTD device size to get bad_peb_limit

In reply to Richard Genoud
Quoted 3 lines · Richard Genoud

The Kconfig option is in per1024 blocks, thus it can have a default value of 20 which is *very* common for NAND devices.

Why do you prefer per1024? I'd make it centi-percent instead, wouldn't that be more human-friendly. It is just %*100. If I am a user, it is easy for me to calculate % and multiply that by 100. This per1024 thing would make me scratch my head...

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#6 lore raw

Re: [PATCH 3/4] UBI: use the whole MTD device size to get bad_peb_limit

2012/7/18 Artem Bityutskiy <[email protected]>:

Quoted 9 lines · Richard Genoud
Quoted 3 lines

The Kconfig option is in per1024 blocks, thus it can have a default value of 20 which is *very* common for NAND devices.

Why do you prefer per1024? I'd make it centi-percent instead, wouldn't that be more human-friendly. It is just %*100. If I am a user, it is easy for me to calculate % and multiply that by 100. This per1024 thing would make me scratch my head...

All the NAND devices I've seen are a multiple of 1024 erase blocks, so
I thought it'll be easier for the humans to use per1024...
here are 2 random datasheets :
page 14: http://www.micron.com/~/media/Documents/Products/Data%20Sheet/NAND%20Flash/6691NANDXXX%20A%20128Mb_256Mb_528%20byte_or_264%20word_page_3V_SLC_90nm.pdf
2048 blocks 40BEB max => 20per1024
1024 blocks 20BEB max => 20per1024

page 24: http://www.hynix.com/datasheet/pdf/flash/HY27(U_S)S(08_16)121M(Rev0.6).pdf
4096 blocks 80BEB max => 20per1024

If we express the default exact value of 20 per1024 blocks in percent, that would be 1.953125% (event in per-thousand, we'll have 19.53125). => the value have to be rounded to floor or ceiling and that will make the user unsure about the effective number of bad blocks used for bad block handling.
(even if we say that the default value 20 per-thousand block will be used, as the NAND devices have a number of blocks multiple of 1024, for a 4096 blocks device, we'll have 81.92 reserved blocks, rounded to 82 => we loose 2 blocks for each mtd partition.)
So the per1024 thing was really to stick to the device layout and to be easier for users (IMHO)

Richard.

#7 lore raw

Re: [PATCH 3/4] UBI: use the whole MTD device size to get bad_peb_limit

Quoted 2 lines · Richard Genoud

So the per1024 thing was really to stick to the device layout and to be easier for users (IMHO)

Convinced, thanks!

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#2 lore raw

Re: [PATCH 1/4] mtd_is_partition: struct mtd_info should be const

In reply to Richard Genoud
Quoted 4 lines · Richard Genoud

struct mtd_info is not modified by mtd_is_partition so it should be a const.

I've pushed this and the next MTD patches to linux-ubi, thank you! I hope David does not mind if it goes in via the MTD, or if he could take them to the mtd tree, then I cold merge the mtd tree to the UBI tree. But this is not your pain.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#21 lore raw

Re: [PATCH 4/4] UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter

In reply to Richard Genoud
Quoted 16 lines · Richard Genoud

This patch provides the possibility to adjust the "maximum expected number of bad blocks per 1024 blocks" (max_beb_per1024) for each mtd device.

The majority of NAND devices have their max_beb_per1024 equal to 20, but sometimes it's more.
Now, we can adjust that via a kernel parameter:
ubi.mtd=<name|num|path>[,<vid_hdr_offs>[,max_beb_per1024]] and via UBI_IOCATT ioctl which is now:

struct ubi_attach_req {
	__s32 ubi_num;
	__s32 mtd_num;
	__s32 vid_hdr_offset;
	__u8 max_beb_per1024;
	__s8 padding[11];
};
Quoted 1 line

Would you please split this patch on 2 - first adds the kernel module parameter, the second changes the ioctl.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#8 lore raw

Re: [PATCH 3/4] UBI: use the whole MTD device size to get bad_peb_limit

In reply to Richard Genoud

Review

Quoted 6 lines · Richard Genoud
(quoted diff) +6 -0
+			/* we are using here the whole MTD device size and not
+			 * the MTD partition size because the maximum number of
+			 * bad blocks is a percentage of the whole device and
+			 * the bad blocks are not fairly disposed on a flash
+			 * device
+			 */

Would you please use proper kernel-style comments instead, to be consistent with the rest of the UBI code? I've amended this one, but wanted to note for future.

I've re-based your patch against the latest UBI. I've also tried to improve the Kconfig help message as well. Below is the patch I ended up with.

From cb14c6c5455443cbe960a36e77b3fcd0e5bc7152 Mon Sep 17 00:00:00 2001
From: Richard Genoud <[email protected]>
Date: Tue, 10 Jul 2012 18:23:41 +0200
Subject: [PATCH 2/2] UBI: use the whole MTD device size to get bad_peb_limit

On NAND flash devices, UBI reserves some physical erase blocks (PEB) for bad block handling. Today, the number of reserved PEB can only be set as a percentage of the total number of PEB in each MTD partition. For example, for a NAND flash with 128KiB PEB, 2 MTD partition of 20MiB (mtd0) and 100MiB (mtd1) and 2% reserved PEB:
- the UBI device on mtd0 will have 2 PEB reserved
- the UBI device on mtd1 will have 16 PEB reserved

The problem with this behaviour is that NAND flash manufacturers give a minimum number of valid block (NVB) during the endurance life of the device, e.g.:

Parameter Symbol Min Max Unit Notes
--------------------------------------------------------------
Valid block number NVB 1004 1024 Blocks 1
Note:
1. Invalid blocks are block that contain one or more bad bits beyond ECC. The device may contain bad blocks upon shipment. Additional bad blocks may develop over time; however, the total number of available blocks will not drop below NVB during the endurance life of the device.

From this number we can deduce the maximum number of bad PEB that a device will contain during its endurance life: a 128MiB NAND flash (1024 PEB) will not have less than 20 bad blocks during the flash endurance life.

But the manufacturer doesn't tell where those bad block will appear. He doesn't say either if they will be equally disposed on the whole device (and I'm pretty sure they won't). So, according to the datasheets, we should reserve the maximum number of bad PEB for each UBI device (worst case scenario: 20 bad blocks appears on the smallest MTD partition).

So this patch make UBI use the whole MTD device size to calculate the maximum bad expected eraseblocks.

The Kconfig option is in per1024 blocks, thus it can have a default value of 20 which is *very* common for NAND devices.

---
 drivers/mtd/ubi/Kconfig |   27 +++++++++++++++++++++------
 drivers/mtd/ubi/build.c |   21 ++++++++++++++++++---
 2 files changed, 39 insertions(+), 9 deletions(-)
drivers/mtd/ubi/Kconfig +21 -6
index b2f4f0f..98bda6c 100644
@@ -28,14 +28,29 @@ config MTD_UBI_WL_THRESHOLD
 	  to 128 or 256, although it does not have to be power of 2).
 
 config MTD_UBI_BEB_LIMIT
-	int "Percentage of maximum expected bad eraseblocks"
-	default 2
-	range 0 25
+	int "Maximum expected bad eraseblock count per 1024 eraseblocks"
+	default 20
+	range 2 256
 	help
 	  This option specifies the maximum bad physical eraseblocks UBI
-	  expects on the UBI device (percents of total number of physical
-	  eraseblocks on this MTD partition). If the underlying flash does not
-	  admit of bad eraseblocks (e.g. NOR flash), this value is ignored.
+	  expects on the MTD device (per 1024 eraseblocks). If the underlying
+	  flash does not admit of bad eraseblocks (e.g. NOR flash), this value
+	  is ignored.
+
+	  NAND datasheets often specify the minimum and maximum NVM (Number of
+	  Valid Blocks) for the flashes' endurance lifetime. The maximum
+	  expected bad eraseblocks per 1024 eraseblocks then can be calculated
+	  as "1024 * (1 - MinNVB / MaxNVB)", which gives 20 for most NANDs
+	  (MaxNVB is basically the total count of eraseblocks on the chip).
+
+	  To put it differently, if this value is 20, UBI will try to reserve
+	  about 1.9% of physical eraseblocks for bad blocks handling. And that
+	  will be 1.9% of eraseblocks on the entire NAND chip, not just the MTD
+	  partition UBI attaches. This means that if you have, say,  if a NAND
+	  flash chip admits maximum 40 bad eraseblocks, and it is split on two
+	  MTD partitions of the same size, UBI will reserve 40 eraseblocks when
+	  attaching a partition.
+
 	  Leave the default value if unsure.
 
 config MTD_UBI_GLUEBI
drivers/mtd/ubi/build.c +18 -3
index 7b6b5f9..9fd8d86 100644
@@ -36,6 +36,7 @@
 #include <linux/namei.h>
 #include <linux/stat.h>
 #include <linux/miscdevice.h>
+#include <linux/mtd/partitions.h>
 #include <linux/log2.h>
 #include <linux/kthread.h>
 #include <linux/kernel.h>
@@ -610,11 +611,25 @@ static int io_init(struct ubi_device *ubi)
 	if (mtd_can_have_bb(ubi->mtd)) {
 		ubi->bad_allowed = 1;
 		if (CONFIG_MTD_UBI_BEB_LIMIT > 0) {
-			int percent = CONFIG_MTD_UBI_BEB_LIMIT;
-			int limit = mult_frac(ubi->peb_count, percent, 100);
+			int per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
+			int limit, device_pebs;
+			uint64_t device_size;
+
+			/*
+			 * Here we are using size of the entire flash chip and
+			 * not just the MTD partition size because the maximum
+			 * number of bad eraseblocks is a percentage of the
+			 * whole device and bad eraseblocks are not fairly
+			 * distributed over the flash chip. So the worst case
+			 * is that all the bad eraseblocks of the chip are in
+			 * the MTD partition we are attaching (ubi->mtd).
+			 */
+			device_size = mtd_get_device_size(ubi->mtd);
+			device_pebs = mtd_div_by_eb(device_size, ubi->mtd);
+			limit = mult_frac(device_pebs, per1024, 1024);
 
 			/* Round it up */
-			if (mult_frac(limit, 100, percent) < ubi->peb_count)
+			if (mult_frac(limit, 1024, per1024) < ubi->peb_count)
 				limit += 1;
 			ubi->bad_peb_limit = limit;
 		}
Signature · 5 lines
1.7.10.4

-- 
Best Regards,
Artem Bityutskiy
#9 lore raw

Re: [PATCH 3/4] UBI: use the whole MTD device size to get bad_peb_limit

2012/8/15 Artem Bityutskiy <[email protected]>:

Quoted 11 lines · Richard Genoud

Review

Quoted 6 lines
(quoted diff) +6 -0
+                     /* we are using here the whole MTD device size and not
+                      * the MTD partition size because the maximum number of
+                      * bad blocks is a percentage of the whole device and
+                      * the bad blocks are not fairly disposed on a flash
+                      * device
+                      */

Would you please use proper kernel-style comments instead, to be consistent with the rest of the UBI code? I've amended this one, but wanted to note for future.

ok, sorry for that.

Quoted 78 lines

I've re-based your patch against the latest UBI. I've also tried to improve the Kconfig help message as well. Below is the patch I ended up with.

From cb14c6c5455443cbe960a36e77b3fcd0e5bc7152 Mon Sep 17 00:00:00 2001
From: Richard Genoud <[email protected]>
Date: Tue, 10 Jul 2012 18:23:41 +0200
Subject: [PATCH 2/2] UBI: use the whole MTD device size to get bad_peb_limit

On NAND flash devices, UBI reserves some physical erase blocks (PEB) for bad block handling. Today, the number of reserved PEB can only be set as a percentage of the total number of PEB in each MTD partition. For example, for a NAND flash with 128KiB PEB, 2 MTD partition of 20MiB (mtd0) and 100MiB (mtd1) and 2% reserved PEB:
- the UBI device on mtd0 will have 2 PEB reserved
- the UBI device on mtd1 will have 16 PEB reserved

The problem with this behaviour is that NAND flash manufacturers give a minimum number of valid block (NVB) during the endurance life of the device, e.g.:

Parameter Symbol Min Max Unit Notes
--------------------------------------------------------------
Valid block number NVB 1004 1024 Blocks 1
Note:
1. Invalid blocks are block that contain one or more bad bits beyond ECC. The device may contain bad blocks upon shipment. Additional bad blocks may develop over time; however, the total number of available blocks will not drop below NVB during the endurance life of the device.

From this number we can deduce the maximum number of bad PEB that a device will contain during its endurance life: a 128MiB NAND flash (1024 PEB) will not have less than 20 bad blocks during the flash endurance life.

But the manufacturer doesn't tell where those bad block will appear. He doesn't say either if they will be equally disposed on the whole device (and I'm pretty sure they won't). So, according to the datasheets, we should reserve the maximum number of bad PEB for each UBI device (worst case scenario: 20 bad blocks appears on the smallest MTD partition).

So this patch make UBI use the whole MTD device size to calculate the maximum bad expected eraseblocks.

The Kconfig option is in per1024 blocks, thus it can have a default value of 20 which is *very* common for NAND devices.

---
 drivers/mtd/ubi/Kconfig |   27 +++++++++++++++++++++------
 drivers/mtd/ubi/build.c |   21 ++++++++++++++++++---
 2 files changed, 39 insertions(+), 9 deletions(-)
drivers/mtd/ubi/Kconfig +7 -6
index b2f4f0f..98bda6c 100644
@@ -28,14 +28,29 @@ config MTD_UBI_WL_THRESHOLD
          to 128 or 256, although it does not have to be power of 2).
 
 config MTD_UBI_BEB_LIMIT
-       int "Percentage of maximum expected bad eraseblocks"
-       default 2
-       range 0 25
+       int "Maximum expected bad eraseblock count per 1024 eraseblocks"
+       default 20
+       range 2 256
        help
          This option specifies the maximum bad physical eraseblocks UBI
-         expects on the UBI device (percents of total number of physical
-         eraseblocks on this MTD partition). If the underlying flash does not
-         admit of bad eraseblocks (e.g. NOR flash), this value is ignored.
+         expects on the MTD device (per 1024 eraseblocks). If the underlying
+         flash does not admit of bad eraseblocks (e.g. NOR flash), this value
+         is ignored.
+

drivers/mtd/ubi/Kconfig config MTD_UBI_WL_THRESHOLD · line 49

Quoted 10 lines
drivers/mtd/ubi/Kconfig +10 -0
+         NAND datasheets often specify the minimum and maximum NVM (Number of
+         Valid Blocks) for the flashes' endurance lifetime. The maximum
+         expected bad eraseblocks per 1024 eraseblocks then can be calculated
+         as "1024 * (1 - MinNVB / MaxNVB)", which gives 20 for most NANDs
+         (MaxNVB is basically the total count of eraseblocks on the chip).
+
+         To put it differently, if this value is 20, UBI will try to reserve
+         about 1.9% of physical eraseblocks for bad blocks handling. And that
+         will be 1.9% of eraseblocks on the entire NAND chip, not just the MTD
+         partition UBI attaches. This means that if you have, say,  if a NAND

I don't quite understand this sentence.
Maybe you meant:
This means that if you have, say, a NAND flash chip that admits a maximum of 40 bad eraseblocks [...]
(but I'm not a native english speaker !)

drivers/mtd/ubi/Kconfig config MTD_UBI_WL_THRESHOLD · line 55

Quoted 6 lines
drivers/mtd/ubi/Kconfig +4 -0
+         flash chip admits maximum 40 bad eraseblocks, and it is split on two
+         MTD partitions of the same size, UBI will reserve 40 eraseblocks when
+         attaching a partition.
+
          Leave the default value if unsure.
 

Best regards,
Richard.

Signature · 1 line
for me, ck means con kolivas and not calvin klein... does it mean I'm a geek ?
#11 lore raw

Re: [PATCH 3/4] UBI: use the whole MTD device size to get bad_peb_limit

In reply to Artem Bityutskiy

Hi Artem, Richard,

Quoted 2 lines · On Wed, 15 Aug 2012 18:08:51 +0300 Artem Bityutskiy <[email protected]> wrote:

1. Invalid blocks are block that contain one or more bad bits beyond ECC.

I would remove this one sentence from the log, it is misleading; invalid blocks are not necessarily related to ECC.

Quoted 13 lines
drivers/mtd/ubi/build.c +10 -2
 		if (CONFIG_MTD_UBI_BEB_LIMIT > 0) {
-			int percent = CONFIG_MTD_UBI_BEB_LIMIT;
-			int limit = mult_frac(ubi->peb_count, percent, 100);
+			int per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
+			int limit, device_pebs;
+			uint64_t device_size;
+
+			/*
+			 * Here we are using size of the entire flash chip and
+			 * not just the MTD partition size because the maximum
+			 * number of bad eraseblocks is a percentage of the
+			 * whole device and bad eraseblocks are not fairly
+			 * distributed over the flash chip. So the worst case

drivers/mtd/ubi/build.c static int io_init(struct ubi_device *ubi) · line 632

Quoted 10 lines
drivers/mtd/ubi/build.c +7 -1
+			 * is that all the bad eraseblocks of the chip are in
+			 * the MTD partition we are attaching (ubi->mtd).
+			 */
+			device_size = mtd_get_device_size(ubi->mtd);
+			device_pebs = mtd_div_by_eb(device_size, ubi->mtd);
+			limit = mult_frac(device_pebs, per1024, 1024);
 
 			/* Round it up */
-			if (mult_frac(limit, 100, percent) < ubi->peb_count)
+			if (mult_frac(limit, 1024, per1024) < ubi->peb_count)

Oops... should be:

+ if (mult_frac(limit, 1024, per1024) < device_pebs)

Regards,
Shmulik

#15 lore raw

Re: [PATCH 3/4] UBI: use the whole MTD device size to get bad_peb_limit

In reply to Artem Bityutskiy

Hi,

One more thing...

drivers/mtd/ubi/Kconfig config MTD_UBI_WL_THRESHOLD · line 33

Quoted 7 lines · On Wed, 15 Aug 2012 18:08:51 +0300 Artem Bityutskiy <[email protected]> wrote:

config MTD_UBI_BEB_LIMIT

drivers/mtd/ubi/Kconfig +3 -3
-	int "Percentage of maximum expected bad eraseblocks"
-	default 2
-	range 0 25
+	int "Maximum expected bad eraseblock count per 1024 eraseblocks"
+	default 20
+	range 2 256

Those defconfigs that explicilty set an original LIMIT should be adjusted as well, as the units have changed, no?

Regards,
Shmulik

#28 lore raw

Re: [PATCH 4/4] UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter

In reply to Richard Genoud

Hi Richard,

Sorry for reviewing this late...

drivers/mtd/ubi/Kconfig config MTD_UBI_WL_THRESHOLD · line 33

Quoted 4 lines · On Tue, 10 Jul 2012 18:23:42 +0200 Richard Genoud <[email protected]>…
drivers/mtd/ubi/Kconfig +0 -4
-config MTD_UBI_BEB_LIMIT
-	int "Maximum expected bad eraseblocks per 1024 eraseblocks"
-	default 20
-	range 2 256

I see some benefit keeping the config.

For the simplest systems (those having one ubi device) that need a limit *other* than the default (20 per 1024), they can simply set the config to their chosen value, as they were used to.

With you approach, these system MUST pass the limit parameter via the ioctl / module-parameter.

drivers/mtd/ubi/build.c void ubi_free_internal_volumes(struct ubi_device *ubi) · line 580

Quoted 9 lines
drivers/mtd/ubi/build.c +9 -0
+static int get_bad_peb_limit(const struct ubi_device *ubi, int max_beb_per1024)
+{
+	int device_peb_count;
+	uint64_t device_size;
+	int beb_limit = 0;
+
+	/* this has already been checked in ioctl */
+	if (max_beb_per1024 <= 0)
+		goto out;

Can you explain how 'max_beb_per1024 <= 0' may happen?

It seems that all of your calls to 'ubi_attach_mtd_dev' pass a positive max_beb_per1024 value (the default is always set). See your 'ubi_mtd_param_parse' and 'ctrl_cdev_ioctl'. Am I missing something?

Also, since max_beb_per1024 is always set, how one may specify a zero limit?

Regards,
Shmulik

#29 lore raw

Re: [PATCH 4/4] UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter

2012/8/16 Shmulik Ladkani <[email protected]>:

Quoted 10 lines

Hi Richard,

Sorry for reviewing this late...

drivers/mtd/ubi/Kconfig config MTD_UBI_WL_THRESHOLD · line 33

Quoted 4 lines · On Tue, 10 Jul 2012 18:23:42 +0200 Richard Genoud <[email protected]>…
drivers/mtd/ubi/Kconfig +0 -4
-config MTD_UBI_BEB_LIMIT
-     int "Maximum expected bad eraseblocks per 1024 eraseblocks"
-     default 20
-     range 2 256
Quoted 8 lines

I see some benefit keeping the config.

For the simplest systems (those having one ubi device) that need a limit *other* than the default (20 per 1024), they can simply set the config to their chosen value, as they were used to.

With you approach, these system MUST pass the limit parameter via the ioctl / module-parameter.

That's right.
We can add a kernel config option to change the max_beb_per1024 default value (actually, this is almost the patch I send first). But I see something disturbing with that:
It means that an ubi_attach call from userspace, without specifying max_beb_per1024, won't have the same result, depending of the default config value the kernel has been compiled with.
(Or maybe this behavior is acceptable).

Quoted 12 lines

drivers/mtd/ubi/build.c void ubi_free_internal_volumes(struct ubi_device *ubi) · line 580

Quoted 9 lines
drivers/mtd/ubi/build.c +9 -0
+static int get_bad_peb_limit(const struct ubi_device *ubi, int max_beb_per1024)
+{
+     int device_peb_count;
+     uint64_t device_size;
+     int beb_limit = 0;
+
+     /* this has already been checked in ioctl */
+     if (max_beb_per1024 <= 0)
+             goto out;

Can you explain how 'max_beb_per1024 <= 0' may happen?

Quoted 3 lines

It seems that all of your calls to 'ubi_attach_mtd_dev' pass a positive max_beb_per1024 value (the default is always set). See your 'ubi_mtd_param_parse' and 'ctrl_cdev_ioctl'. Am I missing something?

Actually it can. But it's because I made a mistake:
p->max_beb_per1024 = bytes_str_to_int(tokens[2]);
=> I didn't check the return value of this. It can fail, and if it does the return value is >0.
I'm going to change that.

Quoted 3 lines

Also, since max_beb_per1024 is always set, how one may specify a zero limit?

You can't.
Do you think we need that ?
0 should be kept for "default value", not to break user-space.
But we can use another value for 0, like 1024 or 2048.
(but honestly, I think this will add complexity for an unlikely configuration.)

Quoted 3 lines

Regards,
Shmulik

Regards,
Richard.

Signature · 1 line
for me, ck means con kolivas and not calvin klein... does it mean I'm a geek ?
#12 lore raw

Re: [PATCH 3/4] UBI: use the whole MTD device size to get bad_peb_limit

In reply to Shmulik Ladkani

2012/8/16 Shmulik Ladkani <[email protected]>:

Quoted 8 lines

Hi Artem, Richard,

Quoted 2 lines · On Wed, 15 Aug 2012 18:08:51 +0300 Artem Bityutskiy <[email protected]> wrote:

1. Invalid blocks are block that contain one or more bad bits beyond ECC.

I would remove this one sentence from the log, it is misleading; invalid blocks are not necessarily related to ECC.

I agree (even if this sentence is from a nand datasheet !)

Quoted 25 lines
Quoted 14 lines
              if (CONFIG_MTD_UBI_BEB_LIMIT > 0) {
drivers/mtd/ubi/build.c +10 -2
-                     int percent = CONFIG_MTD_UBI_BEB_LIMIT;
-                     int limit = mult_frac(ubi->peb_count, percent, 100);
+                     int per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
+                     int limit, device_pebs;
+                     uint64_t device_size;
+
+                     /*
+                      * Here we are using size of the entire flash chip and
+                      * not just the MTD partition size because the maximum
+                      * number of bad eraseblocks is a percentage of the
+                      * whole device and bad eraseblocks are not fairly
+                      * distributed over the flash chip. So the worst case

drivers/mtd/ubi/build.c static int io_init(struct ubi_device *ubi) · line 632

Quoted 10 lines
drivers/mtd/ubi/build.c +7 -1
+                      * is that all the bad eraseblocks of the chip are in
+                      * the MTD partition we are attaching (ubi->mtd).
+                      */
+                     device_size = mtd_get_device_size(ubi->mtd);
+                     device_pebs = mtd_div_by_eb(device_size, ubi->mtd);
+                     limit = mult_frac(device_pebs, per1024, 1024);
 
                      /* Round it up */
-                     if (mult_frac(limit, 100, percent) < ubi->peb_count)
+                     if (mult_frac(limit, 1024, per1024) < ubi->peb_count)
Quoted 6 lines

Oops... should be:

+ if (mult_frac(limit, 1024, per1024) < device_pebs)

Regards,
Shmulik

Ok, I'll change that, may be in a separate patch, as it's a bug fix. I'll add your signoff.

thanks !

Signature · 1 line
for me, ck means con kolivas and not calvin klein... does it mean I'm a geek ?
#30 lore raw

Re: [PATCH 4/4] UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter

In reply to Richard Genoud

Hi Richard, Artem,

Quoted 10 lines · On Thu, 16 Aug 2012 12:07:01 +0200 Richard Genoud <[email protected]>…
Quoted 2 lines

With you approach, these system MUST pass the limit parameter via the ioctl / module-parameter.

That's right.
We can add a kernel config option to change the max_beb_per1024 default value (actually, this is almost the patch I send first). But I see something disturbing with that:
It means that an ubi_attach call from userspace, without specifying max_beb_per1024, won't have the same result, depending of the default config value the kernel has been compiled with.
(Or maybe this behavior is acceptable).

Well, that was the previous behavior of MTD_UBI_BEB_RESERVE, long before our patchsets.
I think it is acceptable, given the fact it simplifies the configuration for most simple systems.

Anyway I'm just pointing out the consequences of your change and try to suggest other alternatives.
Artem should decide as he's the maintainer.

Quoted 4 lines
Quoted 2 lines

Also, since max_beb_per1024 is always set, how one may specify a zero limit?

You can't.
Do you think we need that ?

Well again, originally, prior our patchsets, one *could* set a zero MTD_UBI_BEB_RESERVE for his system. So we're introducing a change that affects the possible ways an ubi system can be configured, banning a configuration that was valid in the past.

Does it make sense to set a zero limit? dunno.
For testing purposes, maybe.

Artem, what do you think? prohibit a zero beb limit?

Regards,
Shmulik

#14 lore raw

Re: [PATCH 3/4] UBI: use the whole MTD device size to get bad_peb_limit

In reply to Shmulik Ladkani
Quoted 2 lines · Shmulik Ladkani

I would remove this one sentence from the log, it is misleading; invalid blocks are not necessarily related to ECC.

Done.

Review

Quoted 1 line
(quoted diff) +1 -0
+			if (mult_frac(limit, 1024, per1024) < device_pebs)

Done, thanks!

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#13 lore raw

Re: [PATCH 3/4] UBI: use the whole MTD device size to get bad_peb_limit

In reply to Richard Genoud
Quoted 1 line · Richard Genoud

Ok, I'll change that, may be in a separate patch, as it's a bug fix.

Let me take care of this patch. I'll amend it myself, push to my tree and re-send to the mailing list for your review. Please, provide an updated version of the other patches instead.

P.S. Of course, your authorship will be preserved.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#16 lore raw

Re: [PATCH 3/4] UBI: use the whole MTD device size to get bad_peb_limit

In reply to Shmulik Ladkani
Quoted 8 lines · Shmulik Ladkani

drivers/mtd/ubi/Kconfig config MTD_UBI_WL_THRESHOLD · line 33

Quoted 5 lines
drivers/mtd/ubi/Kconfig +3 -2
-	default 2
-	range 0 25
+	int "Maximum expected bad eraseblock count per 1024 eraseblocks"
+	default 20
+	range 2 256

Those defconfigs that explicilty set an original LIMIT should be adjusted as well, as the units have changed, no?

Yes, I'll do this, thanks!

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#10 lore raw

Re: [PATCH 3/4] UBI: use the whole MTD device size to get bad_peb_limit

In reply to Richard Genoud
Quoted 5 lines · Richard Genoud
Quoted 1 line

+ To put it differently, if this value is 20, UBI will try

to reserve

Quoted 1 line

+ about 1.9% of physical eraseblocks for bad blocks

handling. And that

Quoted 1 line

+ will be 1.9% of eraseblocks on the entire NAND chip, not

Quoted 8 lines

just the MTD

Quoted 1 line

+ partition UBI attaches. This means that if you have, say,

if a NAND
I don't quite understand this sentence.
Maybe you meant:
This means that if you have, say, a NAND flash chip that admits a maximum of 40 bad eraseblocks [...]
(but I'm not a native english speaker !)

Fixed, thanks!

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#17 lore raw

Re: [PATCH 3/4] UBI: use the whole MTD device size to get bad_peb_limit

In reply to Shmulik Ladkani

2012/8/16 Shmulik Ladkani <[email protected]>:

Quoted 13 lines

Hi,

One more thing...

drivers/mtd/ubi/Kconfig config MTD_UBI_WL_THRESHOLD · line 33

Quoted 7 lines · On Wed, 15 Aug 2012 18:08:51 +0300 Artem Bityutskiy <[email protected]> wrote:

config MTD_UBI_BEB_LIMIT

drivers/mtd/ubi/Kconfig +3 -3
-     int "Percentage of maximum expected bad eraseblocks"
-     default 2
-     range 0 25
+     int "Maximum expected bad eraseblock count per 1024 eraseblocks"
+     default 20
+     range 2 256
Quoted 2 lines

Those defconfigs that explicilty set an original LIMIT should be adjusted as well, as the units have changed, no?

you mean that it should be between 0 and 256 ?

#43 lore raw

PATCH 1/2 UBI: use the whole MTD device size to get bad_peb_limit

In reply to Richard Genoud

From: Richard Genoud <[email protected]>

On NAND flash devices, UBI reserves some physical erase blocks (PEB) for bad block handling. Today, the number of reserved PEB can only be set as a percentage of the total number of PEB in each MTD partition. For example, for a NAND flash with 128KiB PEB, 2 MTD partition of 20MiB (mtd0) and 100MiB (mtd1) and 2% reserved PEB:
- the UBI device on mtd0 will have 2 PEB reserved
- the UBI device on mtd1 will have 16 PEB reserved

The problem with this behaviour is that NAND flash manufacturers give a minimum number of valid block (NVB) during the endurance life of the device, e.g.:

Parameter Symbol Min Max Unit Notes
--------------------------------------------------------------
Valid block number NVB 1004 1024 Blocks 1

Quoted 1 line

From this number we can deduce the maximum number of bad PEB that a device will

contain during its endurance life: a 128MiB NAND flash (1024 PEB) will not have less than 20 bad blocks during the flash endurance life.

But the manufacturer doesn't tell where those bad block will appear. He doesn't say either if they will be equally disposed on the whole device (and I'm pretty sure they won't). So, according to the datasheets, we should reserve the maximum number of bad PEB for each UBI device (worst case scenario: 20 bad blocks appears on the smallest MTD partition).

So this patch make UBI use the whole MTD device size to calculate the maximum bad expected eraseblocks.

The Kconfig option is in per1024 blocks, thus it can have a default value of 20 which is *very* common for NAND devices.

---
 drivers/mtd/ubi/Kconfig |   27 +++++++++++++++++++++------
 drivers/mtd/ubi/build.c |   21 ++++++++++++++++++---
 2 files changed, 39 insertions(+), 9 deletions(-)
drivers/mtd/ubi/Kconfig +21 -6
index b2f4f0f..f326877 100644
@@ -28,14 +28,29 @@ config MTD_UBI_WL_THRESHOLD
 	  to 128 or 256, although it does not have to be power of 2).
 
 config MTD_UBI_BEB_LIMIT
-	int "Percentage of maximum expected bad eraseblocks"
-	default 2
-	range 0 25
+	int "Maximum expected bad eraseblock count per 1024 eraseblocks"
+	default 20
+	range 2 256
 	help
 	  This option specifies the maximum bad physical eraseblocks UBI
-	  expects on the UBI device (percents of total number of physical
-	  eraseblocks on this MTD partition). If the underlying flash does not
-	  admit of bad eraseblocks (e.g. NOR flash), this value is ignored.
+	  expects on the MTD device (per 1024 eraseblocks). If the underlying
+	  flash does not admit of bad eraseblocks (e.g. NOR flash), this value
+	  is ignored.
+
+	  NAND datasheets often specify the minimum and maximum NVM (Number of
+	  Valid Blocks) for the flashes' endurance lifetime. The maximum
+	  expected bad eraseblocks per 1024 eraseblocks then can be calculated
+	  as "1024 * (1 - MinNVB / MaxNVB)", which gives 20 for most NANDs
+	  (MaxNVB is basically the total count of eraseblocks on the chip).
+
+	  To put it differently, if this value is 20, UBI will try to reserve
+	  about 1.9% of physical eraseblocks for bad blocks handling. And that
+	  will be 1.9% of eraseblocks on the entire NAND chip, not just the MTD
+	  partition UBI attaches. This means that if you have, say, a NAND
+	  flash chip admits maximum 40 bad eraseblocks, and it is split on two
+	  MTD partitions of the same size, UBI will reserve 40 eraseblocks when
+	  attaching a partition.
+
 	  Leave the default value if unsure.
 
 config MTD_UBI_GLUEBI
drivers/mtd/ubi/build.c +18 -3
index 7b6b5f9..738772c 100644
@@ -36,6 +36,7 @@
 #include <linux/namei.h>
 #include <linux/stat.h>
 #include <linux/miscdevice.h>
+#include <linux/mtd/partitions.h>
 #include <linux/log2.h>
 #include <linux/kthread.h>
 #include <linux/kernel.h>
@@ -610,11 +611,25 @@ static int io_init(struct ubi_device *ubi)
 	if (mtd_can_have_bb(ubi->mtd)) {
 		ubi->bad_allowed = 1;
 		if (CONFIG_MTD_UBI_BEB_LIMIT > 0) {
-			int percent = CONFIG_MTD_UBI_BEB_LIMIT;
-			int limit = mult_frac(ubi->peb_count, percent, 100);
+			int per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
+			int limit, device_pebs;
+			uint64_t device_size;
+
+			/*
+			 * Here we are using size of the entire flash chip and
+			 * not just the MTD partition size because the maximum
+			 * number of bad eraseblocks is a percentage of the
+			 * whole device and bad eraseblocks are not fairly
+			 * distributed over the flash chip. So the worst case
+			 * is that all the bad eraseblocks of the chip are in
+			 * the MTD partition we are attaching (ubi->mtd).
+			 */
+			device_size = mtd_get_device_size(ubi->mtd);
+			device_pebs = mtd_div_by_eb(device_size, ubi->mtd);
+			limit = mult_frac(device_pebs, per1024, 1024);
 
 			/* Round it up */
-			if (mult_frac(limit, 100, percent) < ubi->peb_count)
+			if (mult_frac(limit, 1024, per1024) < device_pebs)
 				limit += 1;
 			ubi->bad_peb_limit = limit;
 		}
Signature · 1 line
1.7.10.4
#44 lore raw

PATCH 2/2 arm: sam9_l9260_defconfig: adjust UBI bad eraseblocks limit

From: Artem Bityutskiy <[email protected]>

UBI has changed the MTD_UBI_BEB_LIMIT semantics. It used to be a percent of total amount of eraseblock in the partition, and now it is the maximum amount of bad eraseblocks on the entire devise per 1024 eraseblocks. So not only the units changed, but also the meaning. But anyway, old 3% roughly correspond to new 30, so change the defconfig correspondingly.

---
 arch/arm/configs/sam9_l9260_defconfig |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/configs/sam9_l9260_defconfig +1 -1
index da276f9..d11fea5 100644
@@ -39,7 +39,7 @@ CONFIG_MTD_NAND=y
 CONFIG_MTD_NAND_ATMEL=y
 CONFIG_MTD_NAND_PLATFORM=y
 CONFIG_MTD_UBI=y
-CONFIG_MTD_UBI_BEB_LIMIT=3
+CONFIG_MTD_UBI_BEB_LIMIT=30
 CONFIG_MTD_UBI_GLUEBI=y
 CONFIG_BLK_DEV_LOOP=y
 CONFIG_BLK_DEV_RAM=y
Signature · 1 line
1.7.10.4
#37 lore raw

Re: [PATCH 4/4] UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter

In reply to Shmulik Ladkani
Quoted 8 lines · Shmulik Ladkani

For the simplest systems (those having one ubi device) that need a limit
*other* than the default (20 per 1024), they can simply set the config to their chosen value, as they were used to.

With you approach, these system MUST pass the limit parameter via the ioctl / module-parameter.

Yeah, when you change the default, you usually need to do an extra step. It does not feel too bad, and I would not keep additional configuration option for a hypothetical user. If someone suffers, we can add an option to change the default. But I'd start without it. So, I think it is OK to remove this.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#31 lore raw

Re: [PATCH 4/4] UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter

In reply to Shmulik Ladkani
Quoted 5 lines · Shmulik Ladkani

Does it make sense to set a zero limit? dunno.
For testing purposes, maybe.

Artem, what do you think? prohibit a zero beb limit?

We do not have that big user-base. No one uses 0 in the tree, most use the default. I never heard anyone using 0. I did not use it also. I think it is OK to have the lower range start from non-zero. But why it is 2 and not 1 - I am not sure :-)

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#38 lore raw

Re: [PATCH 4/4] UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter

In reply to Artem Bityutskiy
Quoted 7 lines · On Thu, 16 Aug 2012 16:28:38 +0300 Artem Bityutskiy <[email protected]> wrote:
Quoted 6 lines · Shmulik Ladkani

For the simplest systems (those having one ubi device) that need a limit
*other* than the default (20 per 1024), they can simply set the config to their chosen value, as they were used to.

Quoted 8 lines
Quoted 2 lines

With you approach, these system MUST pass the limit parameter via the ioctl / module-parameter.

Yeah, when you change the default, you usually need to do an extra step. It does not feel too bad, and I would not keep additional configuration option for a hypothetical user. If someone suffers, we can add an option to change the default. But I'd start without it. So, I think it is OK to remove this.

Yes, but the main drawback I was referring to is those platforms already setting MTD_UBI_BEB_RESERVE other than the default, by means of kernel configuration.
(there's one platform known to do so in its defconfig, that's sam9_l9260_defconfig, which uses 3% instead of the "standard" 2%).

These platforms must now change their usermode code to either pass a module parameter during the insmod or change their attach ioctl of their application.

We force these systems to change their usermode because we changed ubi's default BEB limit to be 20/1024 _hardcoded_ (instead of kernel configurable as previously was).

Is this ok?

Regards,
Shmulik

#39 lore raw

Re: [PATCH 4/4] UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter

2012/8/16 Shmulik Ladkani <[email protected]>:

Quoted 17 lines · On Thu, 16 Aug 2012 16:28:38 +0300 Artem Bityutskiy <[email protected]> wrote:
Quoted 7 lines · Shmulik Ladkani
Quoted 6 lines

For the simplest systems (those having one ubi device) that need a limit
*other* than the default (20 per 1024), they can simply set the config to their chosen value, as they were used to.

Quoted 8 lines
Quoted 2 lines

With you approach, these system MUST pass the limit parameter via the ioctl / module-parameter.

Yeah, when you change the default, you usually need to do an extra step. It does not feel too bad, and I would not keep additional configuration option for a hypothetical user. If someone suffers, we can add an option to change the default. But I'd start without it. So, I think it is OK to remove this.

Quoted 5 lines

Yes, but the main drawback I was referring to is those platforms already setting MTD_UBI_BEB_RESERVE other than the default, by means of kernel configuration.
(there's one platform known to do so in its defconfig, that's sam9_l9260_defconfig, which uses 3% instead of the "standard" 2%).

I found the board:
https://www.olimex.com/dev/sam9-L9260.html
and the nand datasheet:
http://www.rockbox.org/wiki/pub/Main/LyrePrototype/K9xxG08UXM.pdf
page 11, we can see that the max_bad_bebper1024 is 25 (100 for 4096)

Quoted 9 lines

These platforms must now change their usermode code to either pass a module parameter during the insmod or change their attach ioctl of their application.

We force these systems to change their usermode because we changed ubi's default BEB limit to be 20/1024 _hardcoded_ (instead of kernel configurable as previously was).

Is this ok?

well, I don't know, Artem, you're the maintainer :) I made a quick patch on this, if you decide to apply it.

Richard

Signature · 1 line
for me, ck means con kolivas and not calvin klein... does it mean I'm a geek ?
#22 lore raw

cover splitting "UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter"

In reply to Artem Bityutskiy

Artem,
Here are the 2 patches you requested in place of
"[PATCH 4/4] UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter"

I made some changes to correct some things that Shmulik pointed out:

* max_beb_per1024 is now an unsigned.
* ubi_mtd_param_parse can fail on kstrtouint error.

Richard Genoud (2):
UBI: replace MTD_UBI_BEB_LIMIT with module parameter
UBI: add ioctl for max_beb_per1024

 arch/arm/configs/sam9_l9260_defconfig |    1 -
 drivers/mtd/ubi/Kconfig               |   26 ---------
 drivers/mtd/ubi/build.c               |   93 ++++++++++++++++++++++-----------
 drivers/mtd/ubi/cdev.c                |   10 +++-
 drivers/mtd/ubi/ubi.h                 |    6 ++-
 include/mtd/ubi-user.h                |   19 ++++++-
 6 files changed, 95 insertions(+), 60 deletions(-)
Signature · 1 line
1.7.2.5
#23 lore raw

PATCH 1/2 UBI: replace MTD_UBI_BEB_LIMIT with module parameter

In reply to Artem Bityutskiy

This patch provides the possibility to adjust the "maximum expected number of bad blocks per 1024 blocks" (max_beb_per1024) for each mtd device.

The majority of NAND devices have their max_beb_per1024 equal to 20, but sometimes it's more.
Now, we can adjust that via a kernel parameter:
ubi.mtd=<name|num|path>[,<vid_hdr_offs>[,max_beb_per1024]]

---
 arch/arm/configs/sam9_l9260_defconfig |    1 -
 drivers/mtd/ubi/Kconfig               |   26 ---------
 drivers/mtd/ubi/build.c               |   93 ++++++++++++++++++++++-----------
 drivers/mtd/ubi/cdev.c                |    3 +-
 drivers/mtd/ubi/ubi.h                 |    6 ++-
 5 files changed, 70 insertions(+), 59 deletions(-)
arch/arm/configs/sam9_l9260_defconfig +0 -1
index d11fea5..47dd71a 100644
@@ -39,7 +39,6 @@ CONFIG_MTD_NAND=y
 CONFIG_MTD_NAND_ATMEL=y
 CONFIG_MTD_NAND_PLATFORM=y
 CONFIG_MTD_UBI=y
-CONFIG_MTD_UBI_BEB_LIMIT=30
 CONFIG_MTD_UBI_GLUEBI=y
 CONFIG_BLK_DEV_LOOP=y
 CONFIG_BLK_DEV_RAM=y
drivers/mtd/ubi/Kconfig +0 -26
index f326877..7a57cc0 100644
@@ -27,32 +27,6 @@ config MTD_UBI_WL_THRESHOLD
 	  life-cycle less than 10000, the threshold should be lessened (e.g.,
 	  to 128 or 256, although it does not have to be power of 2).
 
-config MTD_UBI_BEB_LIMIT
-	int "Maximum expected bad eraseblock count per 1024 eraseblocks"
-	default 20
-	range 2 256
-	help
-	  This option specifies the maximum bad physical eraseblocks UBI
-	  expects on the MTD device (per 1024 eraseblocks). If the underlying
-	  flash does not admit of bad eraseblocks (e.g. NOR flash), this value
-	  is ignored.
-
-	  NAND datasheets often specify the minimum and maximum NVM (Number of
-	  Valid Blocks) for the flashes' endurance lifetime. The maximum
-	  expected bad eraseblocks per 1024 eraseblocks then can be calculated
-	  as "1024 * (1 - MinNVB / MaxNVB)", which gives 20 for most NANDs
-	  (MaxNVB is basically the total count of eraseblocks on the chip).
-
-	  To put it differently, if this value is 20, UBI will try to reserve
-	  about 1.9% of physical eraseblocks for bad blocks handling. And that
-	  will be 1.9% of eraseblocks on the entire NAND chip, not just the MTD
-	  partition UBI attaches. This means that if you have, say, a NAND
-	  flash chip admits maximum 40 bad eraseblocks, and it is split on two
-	  MTD partitions of the same size, UBI will reserve 40 eraseblocks when
-	  attaching a partition.
-
-	  Leave the default value if unsure.
-
 config MTD_UBI_GLUEBI
 	tristate "MTD devices emulation driver (gluebi)"
 	help
drivers/mtd/ubi/build.c +63 -30

183 lines not on this page · raw message

drivers/mtd/ubi/cdev.c +2 -1
index fb55678..e0027e7 100644
@@ -1010,7 +1010,8 @@ static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd,
 		 * 'ubi_attach_mtd_dev()'.
 		 */
 		mutex_lock(&ubi_devices_mutex);
-		err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset);
+		err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset,
+					 MTD_UBI_DEFAULT_BEB_LIMIT);
 		mutex_unlock(&ubi_devices_mutex);
 		if (err < 0)
 			put_mtd_device(mtd);
drivers/mtd/ubi/ubi.h +5 -1
index c94612e..f926343 100644
@@ -50,6 +50,9 @@
 /* UBI name used for character devices, sysfs, etc */
 #define UBI_NAME_STR "ubi"
 
+/* Default number of maximum expected bad blocks per 1024 eraseblocks */
+#define MTD_UBI_DEFAULT_BEB_LIMIT 20
+
 /* Normal UBI messages */
 #define ubi_msg(fmt, ...) printk(KERN_NOTICE "UBI: " fmt "\n", ##__VA_ARGS__)
 /* UBI warning messages */
@@ -693,7 +696,8 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
 			 struct ubi_vid_hdr *vid_hdr);
 
 /* build.c */
-int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset);
+int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
+		       int vid_hdr_offset, unsigned int max_beb_per1024);
 int ubi_detach_mtd_dev(int ubi_num, int anyway);
 struct ubi_device *ubi_get_device(int ubi_num);
 void ubi_put_device(struct ubi_device *ubi);
Signature · 1 line
1.7.2.5
#26 lore raw

PATCH 2/2 UBI: add ioctl for max_beb_per1024

In reply to Artem Bityutskiy

This patch provides the possibility to adjust the "maximum expected number of bad blocks per 1024 blocks" (max_beb_per1024) for each mtd device from UBI_IOCATT ioctl.

The majority of NAND devices have their max_beb_per1024 equal to 20, but sometimes it's more.
We already could adjust that via a kernel parameter, now we can also use UBI_IOCATT ioctl:

struct ubi_attach_req {
	__s32 ubi_num;
	__s32 mtd_num;
	__s32 vid_hdr_offset;
	__u8 max_beb_per1024;
	__s8 padding[11];
};
---
 drivers/mtd/ubi/cdev.c |    9 ++++++++-
 include/mtd/ubi-user.h |   19 ++++++++++++++++++-
 2 files changed, 26 insertions(+), 2 deletions(-)
drivers/mtd/ubi/cdev.c +8 -1
index e0027e7..d268b42 100644
@@ -1006,12 +1006,19 @@ static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd,
 		}
 
 		/*
+		 * For compatibility with older user-space tools,
+		 * a zero value should be treated like a default value
+		 */
+		if (!req.max_beb_per1024)
+			req.max_beb_per1024 = MTD_UBI_DEFAULT_BEB_LIMIT;
+
+		/*
 		 * Note, further request verification is done by
 		 * 'ubi_attach_mtd_dev()'.
 		 */
 		mutex_lock(&ubi_devices_mutex);
 		err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset,
-					 MTD_UBI_DEFAULT_BEB_LIMIT);
+					 req.max_beb_per1024);
 		mutex_unlock(&ubi_devices_mutex);
 		if (err < 0)
 			put_mtd_device(mtd);
include/mtd/ubi-user.h +18 -1
index 8787349..77cd0d1 100644
@@ -222,6 +222,7 @@ enum {
  * @ubi_num: UBI device number to create
  * @mtd_num: MTD device number to attach
  * @vid_hdr_offset: VID header offset (use defaults if %0)
+ * @max_beb_per1024: Maximum expected bad eraseblocks per 1024 eraseblocks
  * @padding: reserved for future, not used, has to be zeroed
  *
  * This data structure is used to specify MTD device UBI has to attach and the
@@ -245,12 +246,28 @@ enum {
  * be 2KiB-64 bytes = 1984. Note, that this position is not even 512-bytes
  * aligned, which is OK, as UBI is clever enough to realize this is 4th
  * sub-page of the first page and add needed padding.
+ *
+ * The @max_beb_per1024 is the maximum bad eraseblocks UBI expects on the ubi
+ * device per 1024 eraseblocks.
+ * This value is often given in an other form in the NAND datasheet (min NVB
+ * i.e. minimal number of valid blocks). The maximum expected bad eraseblocks
+ * per 1024 is then:
+ *   1024 * (1 - MinNVB / MaxNVB)
+ * Which gives 20 for most NAND devices.
+ * This limit is used in order to derive amount of eraseblock UBI reserves for
+ * handling new bad blocks.
+ * If the device has more bad eraseblocks than this limit, UBI does not reserve
+ * any physical eraseblocks for new bad eraseblocks, but attempts to use
+ * available eraseblocks (if any).
+ * The accepted range is 0-255. If 0 is given, the default value
+ * MTD_UBI_DEFAULT_BEB_LIMIT will be used for compatibility.
  */
 struct ubi_attach_req {
 	__s32 ubi_num;
 	__s32 mtd_num;
 	__s32 vid_hdr_offset;
-	__s8 padding[12];
+	__u8 max_beb_per1024;
+	__s8 padding[11];
 };
 
 /**
Signature · 1 line
1.7.2.5
#40 lore raw

PATCH 1/1 UBI: use a config value for default BEB limit

In reply to Richard Genoud

This patch provides the possibility to adjust the default value for "maximum expected number of bad blocks per 1024 blocks" from kernel configuration.

sam9_l9260_defconfig has been adjusted as well. The nand device used by this card has a NVB of 3996/4096 ie a MAX_BEB_LIMIT of 25/1024

---
 arch/arm/configs/sam9_l9260_defconfig |    1 +
 drivers/mtd/ubi/Kconfig               |   29 +++++++++++++++++++++++++++++
 drivers/mtd/ubi/ubi.h                 |    2 +-
 3 files changed, 31 insertions(+), 1 deletions(-)
arch/arm/configs/sam9_l9260_defconfig +1 -0
index 47dd71a..2c20c9e 100644
@@ -39,6 +39,7 @@ CONFIG_MTD_NAND=y
 CONFIG_MTD_NAND_ATMEL=y
 CONFIG_MTD_NAND_PLATFORM=y
 CONFIG_MTD_UBI=y
+CONFIG_MTD_UBI_DEFAULT_BEB_LIMIT=25
 CONFIG_MTD_UBI_GLUEBI=y
 CONFIG_BLK_DEV_LOOP=y
 CONFIG_BLK_DEV_RAM=y
drivers/mtd/ubi/Kconfig +29 -0
index 7a57cc0..30e8370 100644
@@ -27,6 +27,35 @@ config MTD_UBI_WL_THRESHOLD
 	  life-cycle less than 10000, the threshold should be lessened (e.g.,
 	  to 128 or 256, although it does not have to be power of 2).
 
+config MTD_UBI_DEFAULT_BEB_LIMIT
+	int "Maximum expected bad eraseblock count per 1024 eraseblocks"
+	default 20
+	range 0 256
+	help
+	  This option specifies the default value for maximum bad physical
+	  eraseblocks UBI expects on the MTD device (per 1024 eraseblocks).
+	  It will be used when the maximum bad PEB value is null in the
+	  UBI_IOCATT ioctl (e.g. when calling ubiattach) or in the kernel
+	  parameter ubi.mtd.
+	  If the underlying flash does not admit of bad eraseblocks
+	  (e.g. NOR flash), this value is ignored.
+
+	  NAND datasheets often specify the minimum and maximum NVM (Number of
+	  Valid Blocks) for the flashes' endurance lifetime. The maximum
+	  expected bad eraseblocks per 1024 eraseblocks then can be calculated
+	  as "1024 * (1 - MinNVB / MaxNVB)", which gives 20 for most NANDs
+	  (MaxNVB is basically the total count of eraseblocks on the chip).
+
+	  To put it differently, if this value is 20, UBI will try to reserve
+	  about 1.9% of physical eraseblocks for bad blocks handling. And that
+	  will be 1.9% of eraseblocks on the entire NAND chip, not just the MTD
+	  partition UBI attaches. This means that if you have, say,  a NAND
+	  flash chip that admits a maximum 40 bad eraseblocks, and it is split
+	  on two MTD partitions of the same size, UBI will reserve 40
+	  eraseblocks when attaching a partition.
+
+	  Leave the default value if unsure.
+
 config MTD_UBI_GLUEBI
 	tristate "MTD devices emulation driver (gluebi)"
 	help
drivers/mtd/ubi/ubi.h +1 -1
index f926343..39a1e7e 100644
@@ -51,7 +51,7 @@
 #define UBI_NAME_STR "ubi"
 
 /* Default number of maximum expected bad blocks per 1024 eraseblocks */
-#define MTD_UBI_DEFAULT_BEB_LIMIT 20
+#define MTD_UBI_DEFAULT_BEB_LIMIT CONFIG_MTD_UBI_DEFAULT_BEB_LIMIT
 
 /* Normal UBI messages */
 #define ubi_msg(fmt, ...) printk(KERN_NOTICE "UBI: " fmt "\n", ##__VA_ARGS__)
Signature · 1 line
1.7.2.5
#42 lore raw

Re: [PATCH 4/4] UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter

In reply to Shmulik Ladkani
Quoted 5 lines · Shmulik Ladkani

Yes, but the main drawback I was referring to is those platforms already setting MTD_UBI_BEB_RESERVE other than the default, by means of kernel configuration.
(there's one platform known to do so in its defconfig, that's sam9_l9260_defconfig, which uses 3% instead of the "standard" 2%).

Yes, I understand this. I think there are few such systems and many of them are fine to pass the parameter explicitly.

Quoted 3 lines

We force these systems to change their usermode because we changed ubi's default BEB limit to be 20/1024 _hardcoded_ (instead of kernel configurable as previously was).

Not necessarily, they can come to us and ask to add the kernel option.

Quoted 1 line

Is this ok?

It is not ideal, but I am willing to take this risk.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#41 lore raw

Re: [PATCH 4/4] UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter

In reply to Richard Genoud
Quoted 7 lines · Richard Genoud
Quoted 2 lines

(there's one platform known to do so in its defconfig, that's sam9_l9260_defconfig, which uses 3% instead of the "standard" 2%).

I found the board:
https://www.olimex.com/dev/sam9-L9260.html
and the nand datasheet:
http://www.rockbox.org/wiki/pub/Main/LyrePrototype/K9xxG08UXM.pdf
page 11, we can see that the max_bad_bebper1024 is 25 (100 for 4096)

OK, thanks for the research!

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#24 lore raw

Re: [PATCH 1/2] UBI: replace MTD_UBI_BEB_LIMIT with module parameter

In reply to Richard Genoud

Richard, would you please split this series differently:

1. Separate out the calculations to the get_bad_peb_limit() func.
2. Invent
2. Add the module parameter
3. Extends the ioctl
4. Removes the Kconfig option

This will be much easier to review.

See also some comments below.

drivers/mtd/ubi/build.c line 49

Quoted 7 lines
drivers/mtd/ubi/build.c +1 -0
@@ -46,6 +46,8 @@
 /* Maximum length of the 'mtd=' parameter */
 #define MTD_PARAM_LEN_MAX 64
 
+#define MTD_PARAM_NB_MAX 3

Please, make it to be

/* Maximum number of comma-separated items in ht 'mtd=' parameter */ #define MTD_PARAM_MAX_COUNT 3

instead.

drivers/mtd/ubi/build.c line 68

Quoted 11 lines
drivers/mtd/ubi/build.c +2 -0
@@ -57,10 +59,12 @@
  * @name: MTD character device node path, MTD device name, or MTD device number
  *        string
  * @vid_hdr_offs: VID header offset
+ * @max_beb_per1024: maximum expected number of bad blocks per 1024 erase blocks
  */
 struct mtd_dev_param {
 	char name[MTD_PARAM_LEN_MAX];
 	int vid_hdr_offs;
+	unsigned int max_beb_per1024;
 };

Please, make it to be just 'int' here and everywhere, just for consistency with other parameters, which are 'int' (no other stronger reason).

drivers/mtd/ubi/build.c static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp) · line 1407

Quoted 8 lines
drivers/mtd/ubi/build.c +1 -1
 MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: "
-		      "mtd=<name|num|path>[,<vid_hdr_offs>].\n"
+		      "mtd=<name|num|path>[,<vid_hdr_offs>[,max_beb_per1024]].\n"
drivers/mtd/ubi/build.c +0 -0
 		      "Multiple \"mtd\" parameters may be specified.\n"
 		      "MTD devices may be specified by their number, name, or "
 		      "path to the MTD character device node.\n"
 		      "Optional \"vid_hdr_offs\" parameter specifies UBI VID "
 		      "header position to be used by UBI.\n"

This comment needs improvement. Consider a situation when I do not want to specify vid_hdr_offs (want to use the default), but want to specify max_beb_per1024. I think I can put 0 here which means "default". Would you please verify this and add a comment about this in this help output. Also, please, verify that the output looks OK using 'modinfo ubi'.

drivers/mtd/ubi/build.c static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp) · line 1411

Quoted 4 lines
drivers/mtd/ubi/build.c +4 -0
+		      "Optional \"max_beb_per1024\" parameter specifies the "
+		      "maximum expected bad eraseblock per 1024 eraseblocks."
+		      "(default " __stringify(MTD_UBI_DEFAULT_BEB_LIMIT)
+		      " if 0 or not set)\n"

Yeah, something like "if 0 or not set" is needed for 'vid_hdr_offs' as well.

drivers/mtd/ubi/build.c static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp) · line 1414

Quoted 3 lines
drivers/mtd/ubi/build.c +0 -0
 		      "Example 1: mtd=/dev/mtd0 - attach MTD device "
 		      "/dev/mtd0.\n"
 		      "Example 2: mtd=content,1984 mtd=4 - attach MTD device "

Could you also modify one of the examples and add "max_beb_per1024" there?

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#27 lore raw

Re: [PATCH 2/2] UBI: add ioctl for max_beb_per1024

In reply to Richard Genoud

drivers/mtd/ubi/cdev.c static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd, · line 1021

Quoted 4 lines · Richard Genoud
drivers/mtd/ubi/cdev.c +1 -1
 		mutex_lock(&ubi_devices_mutex);
 		err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset,
-					 MTD_UBI_DEFAULT_BEB_LIMIT);
+					 req.max_beb_per1024);

Do not forget to explicitely validate 'req.max_beb_per1024' valuse in 'ubi_attach_mtd_dev()', as we do for other parameters.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#19 lore raw

Re: [PATCH] ubiattach: introduce max_beb_per1024 in UBI_IOCATT

In reply to Richard Genoud
Quoted 7 lines · Richard Genoud

The ioctl UBI_IOCATT has been extended with max_beb_per1024 parameter.

This parameter is used for adjusting the "maximum expected number of bad blocks per 1024 blocks" for each mtd device.
The number of physical erase blocks (PEB) that UBI will reserve for bad block handling is now:
whole_flash_chipset__PEB_number * max_beb_per1024 / 1024

Could you please split this on 2 patches:

1. Update teh ubi-user.h file by syncing with the kernel. Do make headers_install and just copy it to mtd-utils.
2. Do the change.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#25 lore raw

Re: [PATCH 1/2] UBI: replace MTD_UBI_BEB_LIMIT with module parameter

In reply to Artem Bityutskiy

2012/8/17 Artem Bityutskiy <[email protected]>:

Quoted 9 lines

Richard, would you please split this series differently:

1. Separate out the calculations to the get_bad_peb_limit() func.
2. Invent
2. Add the module parameter
3. Extends the ioctl
4. Removes the Kconfig option

This will be much easier to review.

ok, will do that.

Quoted 9 lines

See also some comments below.

Quoted 6 lines
drivers/mtd/ubi/build.c +0 -0
@@ -46,6 +46,8 @@
 /* Maximum length of the 'mtd=' parameter */
 #define MTD_PARAM_LEN_MAX 64
 
Quoted 8 lines

drivers/mtd/ubi/build.c line 49

Quoted 1 line
drivers/mtd/ubi/build.c +1 -0
+#define MTD_PARAM_NB_MAX 3

Please, make it to be

/* Maximum number of comma-separated items in ht 'mtd=' parameter */ #define MTD_PARAM_MAX_COUNT 3

instead.

done.

Quoted 16 lines

drivers/mtd/ubi/build.c line 68

Quoted 12 lines
drivers/mtd/ubi/build.c +2 -0
@@ -57,10 +59,12 @@
  * @name: MTD character device node path, MTD device name, or MTD device number
  *        string
  * @vid_hdr_offs: VID header offset
+ * @max_beb_per1024: maximum expected number of bad blocks per 1024 erase blocks
  */
 struct mtd_dev_param {
      char name[MTD_PARAM_LEN_MAX];
      int vid_hdr_offs;
+     unsigned int max_beb_per1024;
 };

Please, make it to be just 'int' here and everywhere, just for consistency with other parameters, which are 'int' (no other stronger reason).

ok, I put it back to an int.

Quoted 9 lines
Quoted 8 lines
drivers/mtd/ubi/build.c +1 -1
 MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: "
-                   "mtd=<name|num|path>[,<vid_hdr_offs>].\n"
+                   "mtd=<name|num|path>[,<vid_hdr_offs>[,max_beb_per1024]].\n"

"Multiple \"mtd\" parameters may be specified.\n" "MTD devices may be specified by their number, name, or " "path to the MTD character device node.\n" "Optional \"vid_hdr_offs\" parameter specifies UBI VID " "header position to be used by UBI.\n"

Quoted 4 lines

This comment needs improvement. Consider a situation when I do not want to specify vid_hdr_offs (want to use the default), but want to specify max_beb_per1024. I think I can put 0 here which means "default". Would you please verify this and add a comment about this in this help output.

you're right, if vid_hdr_offs is 0, the default value is taken. I'll improve the comments accordingly.

Quoted 1 line

Also, please, verify that the output looks OK using 'modinfo ubi'.

ok.

Quoted 8 lines

drivers/mtd/ubi/build.c static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp) · line 1411

Quoted 4 lines
drivers/mtd/ubi/build.c +4 -0
+                   "Optional \"max_beb_per1024\" parameter specifies the "
+                   "maximum expected bad eraseblock per 1024 eraseblocks."
+                   "(default " __stringify(MTD_UBI_DEFAULT_BEB_LIMIT)
+                   " if 0 or not set)\n"

Yeah, something like "if 0 or not set" is needed for 'vid_hdr_offs' as well.

Quoted 6 lines
Quoted 3 lines

"Example 1: mtd=/dev/mtd0 - attach MTD device " "/dev/mtd0.\n"
"Example 2: mtd=content,1984 mtd=4 - attach MTD device "

Could you also modify one of the examples and add "max_beb_per1024" there?

yes, of course.

Quoted 3 lines

--
Best Regards,
Artem Bityutskiy

Signature · 1 line
for me, ck means con kolivas and not calvin klein... does it mean I'm a geek ?
#32 lore raw

Re: [PATCH 4/4] UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter

In reply to Artem Bityutskiy
Quoted 4 lines · On Thu, 16 Aug 2012 16:33:32 +0300 Artem Bityutskiy <[email protected]> wrote:

We do not have that big user-base. No one uses 0 in the tree, most use the default. I never heard anyone using 0. I did not use it also. I think it is OK to have the lower range start from non-zero. But why it is 2 and not 1 - I am not sure :-)

Artem,

Note that originally range was 0..25 (percentage).

It was changed to be 2..256 (per 1024) by 07ad322 in linux-ubi.

Seems arbitrary change.
So if you'd like, we can fix to 1..256 (or 0..256) - as you prefer.

Regards,
Shmulik

#33 lore raw

Re: [PATCH 4/4] UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter

Quoted 6 lines · Shmulik Ladkani
Quoted 4 lines · On Thu, 16 Aug 2012 16:33:32 +0300 Artem Bityutskiy <[email protected]> wrote:

We do not have that big user-base. No one uses 0 in the tree, most use the default. I never heard anyone using 0. I did not use it also. I think it is OK to have the lower range start from non-zero. But why it is 2 and not 1 - I am not sure :-)

Quoted 8 lines

Artem,

Note that originally range was 0..25 (percentage).

It was changed to be 2..256 (per 1024) by 07ad322 in linux-ubi.

Seems arbitrary change.
So if you'd like, we can fix to 1..256 (or 0..256) - as you prefer.

Yeah, I wanted to make it 1..256 but forgot, will do now. 0..256 would need some more work to avoid division by 0.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#34 lore raw

Re: [PATCH 4/4] UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter

Hi Artem,
2012/8/19 Artem Bityutskiy <[email protected]>:

Quoted 2 lines

Yeah, I wanted to make it 1..256 but forgot, will do now. 0..256 would need some more work to avoid division by 0.

Division by 0 is handled in the get_bad_peb_limit() function, I don't see another dangerous place.
So, I think that we can change back the range to 0..256. (and if we want to be coherent with user-space, it should be 0..255, as the range is coded with an u8)

Richard.

Signature · 1 line
for me, ck means con kolivas and not calvin klein... does it mean I'm a geek ?
#35 lore raw

Re: [PATCH 4/4] UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter

Quoted 6 lines · Richard Genoud

Hi Artem,
2012/8/19 Artem Bityutskiy <[email protected]>:

Quoted 2 lines

Yeah, I wanted to make it 1..256 but forgot, will do now. 0..256 would need some more work to avoid division by 0.

Division by 0 is handled in the get_bad_peb_limit() function, I don't see another dangerous place.

        if (mult_frac(limit, 1024, max_beb_per1024) < device_pebs)

will divide by 0 if max_beb_per1024 is 0.

Quoted 2 lines

(and if we want to be coherent with user-space, it should be 0..255, as the range is coded with an u8)

I think it should be uint16_t instead, because we are defining ABI here and we should not assume no one will ever nee values higher than 255.

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#36 lore raw

Re: [PATCH 4/4] UBI: replace MTD_UBI_BEB_LIMIT with user-space parameter

2012/8/20 Artem Bityutskiy <[email protected]>:

Quoted 12 lines · Richard Genoud
Quoted 6 lines

Hi Artem,
2012/8/19 Artem Bityutskiy <[email protected]>:

Quoted 2 lines

Yeah, I wanted to make it 1..256 but forgot, will do now. 0..256 would need some more work to avoid division by 0.

Division by 0 is handled in the get_bad_peb_limit() function, I don't see another dangerous place.

        if (mult_frac(limit, 1024, max_beb_per1024) < device_pebs)

will divide by 0 if max_beb_per1024 is 0.

just a few lines before, you've got:

        /*
         * We don't want a division by 0, and having max_beb_per1024 == 0 is ok
         */
        if (!max_beb_per1024)
                return 0;

from commit abae1f1
(or I'm not looking at the right line ?)

Quoted 5 lines
Quoted 2 lines

(and if we want to be coherent with user-space, it should be 0..255, as the range is coded with an u8)

I think it should be uint16_t instead, because we are defining ABI here and we should not assume no one will ever nee values higher than 255.

I agree with you, even if 25% of reserved space for bad block seems insane, we never know...
I'll update that.

Signature · 1 line
for me, ck means con kolivas and not calvin klein... does it mean I'm a geek ?