Skip to contentSkip to conversation

ubi: Expose mean erase counter in sysfs

PATCH Superseded Rickard Andersson 2 participants 20 messages Oct 11, 2024

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/10 ubi: Expose mean erase counter in sysfs

Currently, only "max_ec" can be read from sysfs, which provides a limited view of the flash device’s wear. In certain cases, such as bugs in the wear-leveling algorithm, specific blocks can be worn down more than others, resulting in uneven wear distribution. Providing a mean erase counter value gives a better understanding of the overall flash wear.
There exists more detailed info in debugfs, but this information is only available for debug builds.

This patch calculates the mean value only when it is requested. This calculation typically takes 2.7 ms for an UBI device with 4091 blocks running on a single core Cortex-A9 at 792 MHz.

---
 drivers/mtd/ubi/build.c | 48 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
drivers/mtd/ubi/build.c +48 -0
index f5140a78a4a8..fa38c434e447 100644
@@ -131,6 +131,8 @@ static struct device_attribute dev_volumes_count =
 	__ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
 static struct device_attribute dev_max_ec =
 	__ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
+static struct device_attribute dev_mean_ec =
+	__ATTR(mean_ec, S_IRUGO, dev_attribute_show, NULL);
 static struct device_attribute dev_reserved_for_bad =
 	__ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
 static struct device_attribute dev_bad_peb_count =
@@ -337,6 +339,49 @@ int ubi_major2num(int major)
 	return ubi_num;
 }
 
+/**
+ * ubi_calc_mean_ec - calculate mean erase counter value.
+ * @ubi: UBI device description object
+ * @start_peb: First PEB in the range
+ * @end_peb: End PEB in the half-open range
+ *
+ * Returns the mean value of all non-bad blocks of the device that are
+ * in the range. Range is half-open i.e end_peb is not actually included.
+ *
+ * Returns zero if it was not possible to calculate the mean value
+ */
+static int ubi_calc_mean_ec(struct ubi_device *ubi, int start_peb, int end_peb)
+{
+	struct ubi_wl_entry *wl;
+	int peb;
+	int ec_count = 0;
+	int mean_ec = 0;
+	uint64_t ec_sum = 0;
+
+	for (peb = start_peb; peb < end_peb; peb++) {
+		int err;
+
+		err = ubi_io_is_bad(ubi, peb);
+		if (err)
+			continue;
+
+		spin_lock(&ubi->wl_lock);
+
+		wl = ubi->lookuptbl[peb];
+		if (wl) {
+			ec_sum += wl->ec;
+			ec_count++;
+		}
+
+		spin_unlock(&ubi->wl_lock);
+	}
+
+	if (ec_count > 0)
+		mean_ec = div_u64(ec_sum, ec_count);
+
+	return mean_ec;
+}
+
 /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
 static ssize_t dev_attribute_show(struct device *dev,
 				  struct device_attribute *attr, char *buf)
@@ -366,6 +411,8 @@ static ssize_t dev_attribute_show(struct device *dev,
 		ret = sprintf(buf, "%d\n", ubi->vol_count - UBI_INT_VOL_COUNT);
 	else if (attr == &dev_max_ec)
 		ret = sprintf(buf, "%d\n", ubi->max_ec);
+	else if (attr == &dev_mean_ec)
+		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, 0, ubi->peb_count));
 	else if (attr == &dev_reserved_for_bad)
 		ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
 	else if (attr == &dev_bad_peb_count)
@@ -392,6 +439,7 @@ static struct attribute *ubi_dev_attrs[] = {
 	&dev_total_eraseblocks.attr,
 	&dev_volumes_count.attr,
 	&dev_max_ec.attr,
+	&dev_mean_ec.attr,
 	&dev_reserved_for_bad.attr,
 	&dev_bad_peb_count.attr,
 	&dev_max_vol_count.attr,
Signature · 6 lines
2.30.2


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

PATCH v2 2/10 ubi: Expose mean erase counter for fastmap in sysfs

Since the fastmap area has its own wear levelling it is valuable to provide a mean value for that area. This value can be used in order to estimate life expectancy of the flash.

---
 drivers/mtd/ubi/build.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/mtd/ubi/build.c +11 -0
index fa38c434e447..3633c743e7d1 100644
@@ -133,6 +133,10 @@ static struct device_attribute dev_max_ec =
 	__ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
 static struct device_attribute dev_mean_ec =
 	__ATTR(mean_ec, S_IRUGO, dev_attribute_show, NULL);
+#ifdef CONFIG_MTD_UBI_FASTMAP
+static struct device_attribute dev_mean_ec_fastmap =
+	__ATTR(mean_ec_fastmap, S_IRUGO, dev_attribute_show, NULL);
+#endif
 static struct device_attribute dev_reserved_for_bad =
 	__ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
 static struct device_attribute dev_bad_peb_count =
@@ -413,6 +417,10 @@ static ssize_t dev_attribute_show(struct device *dev,
 		ret = sprintf(buf, "%d\n", ubi->max_ec);
 	else if (attr == &dev_mean_ec)
 		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, 0, ubi->peb_count));
+#ifdef CONFIG_MTD_UBI_FASTMAP
+	else if (attr == &dev_mean_ec_fastmap)
+		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, 0, UBI_FM_MAX_START));
+#endif
 	else if (attr == &dev_reserved_for_bad)
 		ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
 	else if (attr == &dev_bad_peb_count)
@@ -440,6 +448,9 @@ static struct attribute *ubi_dev_attrs[] = {
 	&dev_volumes_count.attr,
 	&dev_max_ec.attr,
 	&dev_mean_ec.attr,
+#ifdef CONFIG_MTD_UBI_FASTMAP
+	&dev_mean_ec_fastmap.attr,
+#endif
 	&dev_reserved_for_bad.attr,
 	&dev_bad_peb_count.attr,
 	&dev_max_vol_count.attr,
Signature · 6 lines
2.30.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
reviewer Reviewed-by on v2 2/10
#2 lore raw

Re: [PATCH v2 02/10] ubi: Expose mean erase counter for fastmap in sysfs

Quoted 9 lines · Rickard Andersson

Since the fastmap area has its own wear levelling it is valuable to provide a mean value for that area. This value can be used in order to estimate life expectancy of the flash.

---
  drivers/mtd/ubi/build.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
Quoted 28 lines
drivers/mtd/ubi/build.c +8 -0
index fa38c434e447..3633c743e7d1 100644
@@ -133,6 +133,10 @@ static struct device_attribute dev_max_ec =
  	__ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
  static struct device_attribute dev_mean_ec =
  	__ATTR(mean_ec, S_IRUGO, dev_attribute_show, NULL);
+#ifdef CONFIG_MTD_UBI_FASTMAP
+static struct device_attribute dev_mean_ec_fastmap =
+	__ATTR(mean_ec_fastmap, S_IRUGO, dev_attribute_show, NULL);
+#endif
  static struct device_attribute dev_reserved_for_bad =
  	__ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
  static struct device_attribute dev_bad_peb_count =
@@ -413,6 +417,10 @@ static ssize_t dev_attribute_show(struct device *dev,
  		ret = sprintf(buf, "%d\n", ubi->max_ec);
  	else if (attr == &dev_mean_ec)
  		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, 0, ubi->peb_count));
+#ifdef CONFIG_MTD_UBI_FASTMAP
+	else if (attr == &dev_mean_ec_fastmap)
+		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, 0, UBI_FM_MAX_START));
+#endif
  	else if (attr == &dev_reserved_for_bad)
  		ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
  	else if (attr == &dev_bad_peb_count)

drivers/mtd/ubi/build.c static struct attribute *ubi_dev_attrs[] = { · line 456

Quoted 9 lines
drivers/mtd/ubi/build.c +3 -0
@@ -440,6 +448,9 @@ static struct attribute *ubi_dev_attrs[] = {
  	&dev_volumes_count.attr,
  	&dev_max_ec.attr,
  	&dev_mean_ec.attr,
+#ifdef CONFIG_MTD_UBI_FASTMAP
+	&dev_mean_ec_fastmap.attr,
+#endif
  	&dev_reserved_for_bad.attr,
  	&dev_bad_peb_count.attr,
  	&dev_max_vol_count.attr,

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

#3 lore raw

PATCH v2 3/10 ubi: Expose max erase counter for fastmap in sysfs

In reply to Rickard Andersson

Since the fastmap area has its own wear levelling it is valuable to provide a max erase counter value specifically for that area.

---
 drivers/mtd/ubi/build.c | 44 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
drivers/mtd/ubi/build.c +44 -0
index 3633c743e7d1..f5a3a3f7d490 100644
@@ -134,6 +134,8 @@ static struct device_attribute dev_max_ec =
 static struct device_attribute dev_mean_ec =
 	__ATTR(mean_ec, S_IRUGO, dev_attribute_show, NULL);
 #ifdef CONFIG_MTD_UBI_FASTMAP
+static struct device_attribute dev_max_ec_fastmap =
+	__ATTR(max_ec_fastmap, S_IRUGO, dev_attribute_show, NULL);
 static struct device_attribute dev_mean_ec_fastmap =
 	__ATTR(mean_ec_fastmap, S_IRUGO, dev_attribute_show, NULL);
 #endif
@@ -386,6 +388,45 @@ static int ubi_calc_mean_ec(struct ubi_device *ubi, int start_peb, int end_peb)
 	return mean_ec;
 }
 
+/**
+ * ubi_calc_max_ec_fastmap - calculate max erase counter value.
+ * @ubi: UBI device description object
+ * @start_peb: First PEB in the range
+ * @end_peb: End PEB in the half-open range
+ *
+ * Returns the max erase counter value that can be found in the range.
+ * Range is half-open i.e end_peb is not actually included.
+ * Returns zero if it was not possible to calculate the max ec value
+ */
+#ifdef CONFIG_MTD_UBI_FASTMAP
+static int ubi_calc_max_ec(struct ubi_device *ubi, int start_peb, int end_peb)
+{
+	struct ubi_wl_entry *wl;
+	int peb;
+	int max_ec = 0;
+
+	for (peb = start_peb; peb < end_peb; peb++) {
+		int err;
+
+		err = ubi_io_is_bad(ubi, peb);
+		if (err)
+			continue;
+
+		spin_lock(&ubi->wl_lock);
+
+		wl = ubi->lookuptbl[peb];
+		if (wl) {
+			if (max_ec < wl->ec)
+				max_ec = wl->ec;
+		}
+
+		spin_unlock(&ubi->wl_lock);
+	}
+
+	return max_ec;
+}
+#endif
+
 /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
 static ssize_t dev_attribute_show(struct device *dev,
 				  struct device_attribute *attr, char *buf)
@@ -418,6 +459,8 @@ static ssize_t dev_attribute_show(struct device *dev,
 	else if (attr == &dev_mean_ec)
 		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, 0, ubi->peb_count));
 #ifdef CONFIG_MTD_UBI_FASTMAP
+	else if (attr == &dev_max_ec_fastmap)
+		ret = sprintf(buf, "%d\n", ubi_calc_max_ec(ubi, 0, UBI_FM_MAX_START));
 	else if (attr == &dev_mean_ec_fastmap)
 		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, 0, UBI_FM_MAX_START));
 #endif
@@ -449,6 +492,7 @@ static struct attribute *ubi_dev_attrs[] = {
 	&dev_max_ec.attr,
 	&dev_mean_ec.attr,
 #ifdef CONFIG_MTD_UBI_FASTMAP
+	&dev_max_ec_fastmap.attr,
 	&dev_mean_ec_fastmap.attr,
 #endif
 	&dev_reserved_for_bad.attr,
Signature · 6 lines
2.30.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
reviewer
#4 lore raw

Re: [PATCH v2 03/10] ubi: Expose max erase counter for fastmap in sysfs

Quoted 25 lines · Rickard Andersson

Since the fastmap area has its own wear levelling it is valuable to provide a max erase counter value specifically for that area.

---
  drivers/mtd/ubi/build.c | 44 +++++++++++++++++++++++++++++++++++++++++
  1 file changed, 44 insertions(+)
drivers/mtd/ubi/build.c +2 -0
index 3633c743e7d1..f5a3a3f7d490 100644
@@ -134,6 +134,8 @@ static struct device_attribute dev_max_ec =
  static struct device_attribute dev_mean_ec =
  	__ATTR(mean_ec, S_IRUGO, dev_attribute_show, NULL);
  #ifdef CONFIG_MTD_UBI_FASTMAP
+static struct device_attribute dev_max_ec_fastmap =
+	__ATTR(max_ec_fastmap, S_IRUGO, dev_attribute_show, NULL);
  static struct device_attribute dev_mean_ec_fastmap =
  	__ATTR(mean_ec_fastmap, S_IRUGO, dev_attribute_show, NULL);
  #endif
@@ -386,6 +388,45 @@ static int ubi_calc_mean_ec(struct ubi_device *ubi, int start_peb, int end_peb)
  	return mean_ec;
  }
 

drivers/mtd/ubi/build.c static int ubi_calc_mean_ec(struct ubi_device *ubi, int start_peb, int end_peb) · line 400

Quoted 10 lines
drivers/mtd/ubi/build.c +9 -0
+/**
+ * ubi_calc_max_ec_fastmap - calculate max erase counter value.
+ * @ubi: UBI device description object
+ * @start_peb: First PEB in the range
+ * @end_peb: End PEB in the half-open range
+ *
+ * Returns the max erase counter value that can be found in the range.
+ * Range is half-open i.e end_peb is not actually included.
 
+ * Returns zero if it was not possible to calculate the max ec value

Delete this line from the comments, since zero is a normal value if all PEBs' erase counter is initialized as '0' by ubiformat tool.

Quoted 44 lines
drivers/mtd/ubi/build.c +30 -0
+ */
+#ifdef CONFIG_MTD_UBI_FASTMAP
+static int ubi_calc_max_ec(struct ubi_device *ubi, int start_peb, int end_peb)
+{
+	struct ubi_wl_entry *wl;
+	int peb;
+	int max_ec = 0;
+
+	for (peb = start_peb; peb < end_peb; peb++) {
+		int err;
+
+		err = ubi_io_is_bad(ubi, peb);
+		if (err)
+			continue;
+
+		spin_lock(&ubi->wl_lock);
+
+		wl = ubi->lookuptbl[peb];
+		if (wl) {
+			if (max_ec < wl->ec)
+				max_ec = wl->ec;
+		}
+
+		spin_unlock(&ubi->wl_lock);
+	}
+
+	return max_ec;
+}
+#endif
+
  /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
  static ssize_t dev_attribute_show(struct device *dev,

struct device_attribute *attr, char *buf)

drivers/mtd/ubi/build.c +2 -0
@@ -418,6 +459,8 @@ static ssize_t dev_attribute_show(struct device *dev,
  	else if (attr == &dev_mean_ec)
  		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, 0, ubi->peb_count));
  #ifdef CONFIG_MTD_UBI_FASTMAP
+	else if (attr == &dev_max_ec_fastmap)
+		ret = sprintf(buf, "%d\n", ubi_calc_max_ec(ubi, 0, UBI_FM_MAX_START));
  	else if (attr == &dev_mean_ec_fastmap)
  		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, 0, UBI_FM_MAX_START));
  #endif

drivers/mtd/ubi/build.c static struct attribute *ubi_dev_attrs[] = { · line 498

Quoted 7 lines
drivers/mtd/ubi/build.c +1 -0
@@ -449,6 +492,7 @@ static struct attribute *ubi_dev_attrs[] = {
  	&dev_max_ec.attr,
  	&dev_mean_ec.attr,
  #ifdef CONFIG_MTD_UBI_FASTMAP
+	&dev_max_ec_fastmap.attr,
  	&dev_mean_ec_fastmap.attr,
  #endif
  	&dev_reserved_for_bad.attr,

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

#5 lore raw

PATCH v2 4/10 ubi: Expose mean erase counter for data in sysfs

In reply to Rickard Andersson

Expose mean erase counter for data area in sysfs. Data area includes all blocks except for the fastmap area.

This value can be used in order to estimate life expectancy of the flash.

---
 drivers/mtd/ubi/build.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/mtd/ubi/build.c +5 -0
index f5a3a3f7d490..b253dcf849c7 100644
@@ -138,6 +138,8 @@ static struct device_attribute dev_max_ec_fastmap =
 	__ATTR(max_ec_fastmap, S_IRUGO, dev_attribute_show, NULL);
 static struct device_attribute dev_mean_ec_fastmap =
 	__ATTR(mean_ec_fastmap, S_IRUGO, dev_attribute_show, NULL);
+static struct device_attribute dev_mean_ec_data =
+	__ATTR(mean_ec_data, S_IRUGO, dev_attribute_show, NULL);
 #endif
 static struct device_attribute dev_reserved_for_bad =
 	__ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
@@ -463,6 +465,8 @@ static ssize_t dev_attribute_show(struct device *dev,
 		ret = sprintf(buf, "%d\n", ubi_calc_max_ec(ubi, 0, UBI_FM_MAX_START));
 	else if (attr == &dev_mean_ec_fastmap)
 		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, 0, UBI_FM_MAX_START));
+	else if (attr == &dev_mean_ec_data)
+		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, UBI_FM_MAX_START, ubi->peb_count));
 #endif
 	else if (attr == &dev_reserved_for_bad)
 		ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
@@ -494,6 +498,7 @@ static struct attribute *ubi_dev_attrs[] = {
 #ifdef CONFIG_MTD_UBI_FASTMAP
 	&dev_max_ec_fastmap.attr,
 	&dev_mean_ec_fastmap.attr,
+	&dev_mean_ec_data.attr,
 #endif
 	&dev_reserved_for_bad.attr,
 	&dev_bad_peb_count.attr,
Signature · 6 lines
2.30.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
reviewer Reviewed-by on v2 4/10
#6 lore raw

Re: [PATCH v2 04/10] ubi: Expose mean erase counter for data in sysfs

Quoted 10 lines · Rickard Andersson

Expose mean erase counter for data area in sysfs. Data area includes all blocks except for the fastmap area.

This value can be used in order to estimate life expectancy of the flash.

---
  drivers/mtd/ubi/build.c | 5 +++++
  1 file changed, 5 insertions(+)
Quoted 25 lines
drivers/mtd/ubi/build.c +4 -0
index f5a3a3f7d490..b253dcf849c7 100644
@@ -138,6 +138,8 @@ static struct device_attribute dev_max_ec_fastmap =
  	__ATTR(max_ec_fastmap, S_IRUGO, dev_attribute_show, NULL);
  static struct device_attribute dev_mean_ec_fastmap =
  	__ATTR(mean_ec_fastmap, S_IRUGO, dev_attribute_show, NULL);
+static struct device_attribute dev_mean_ec_data =
+	__ATTR(mean_ec_data, S_IRUGO, dev_attribute_show, NULL);
  #endif
  static struct device_attribute dev_reserved_for_bad =
  	__ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
@@ -463,6 +465,8 @@ static ssize_t dev_attribute_show(struct device *dev,
  		ret = sprintf(buf, "%d\n", ubi_calc_max_ec(ubi, 0, UBI_FM_MAX_START));
  	else if (attr == &dev_mean_ec_fastmap)
  		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, 0, UBI_FM_MAX_START));
+	else if (attr == &dev_mean_ec_data)
+		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, UBI_FM_MAX_START, ubi->peb_count));
  #endif
  	else if (attr == &dev_reserved_for_bad)
  		ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);

drivers/mtd/ubi/build.c static struct attribute *ubi_dev_attrs[] = { · line 504

Quoted 7 lines
drivers/mtd/ubi/build.c +1 -0
@@ -494,6 +498,7 @@ static struct attribute *ubi_dev_attrs[] = {
  #ifdef CONFIG_MTD_UBI_FASTMAP
  	&dev_max_ec_fastmap.attr,
  	&dev_mean_ec_fastmap.attr,
+	&dev_mean_ec_data.attr,
  #endif
  	&dev_reserved_for_bad.attr,
  	&dev_bad_peb_count.attr,

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

#7 lore raw

PATCH v2 5/10 ubi: Expose max erase counter for data in sysfs

In reply to Rickard Andersson

Expose max erase counter for data area in sysfs. Data area includes all blocks except for the fastmap area.

This value can be used in order to estimate life expectancy of the flash.

---
 drivers/mtd/ubi/build.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/mtd/ubi/build.c +5 -0
index b253dcf849c7..f3649afbc777 100644
@@ -138,6 +138,8 @@ static struct device_attribute dev_max_ec_fastmap =
 	__ATTR(max_ec_fastmap, S_IRUGO, dev_attribute_show, NULL);
 static struct device_attribute dev_mean_ec_fastmap =
 	__ATTR(mean_ec_fastmap, S_IRUGO, dev_attribute_show, NULL);
+static struct device_attribute dev_max_ec_data =
+	__ATTR(max_ec_data, S_IRUGO, dev_attribute_show, NULL);
 static struct device_attribute dev_mean_ec_data =
 	__ATTR(mean_ec_data, S_IRUGO, dev_attribute_show, NULL);
 #endif
@@ -465,6 +467,8 @@ static ssize_t dev_attribute_show(struct device *dev,
 		ret = sprintf(buf, "%d\n", ubi_calc_max_ec(ubi, 0, UBI_FM_MAX_START));
 	else if (attr == &dev_mean_ec_fastmap)
 		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, 0, UBI_FM_MAX_START));
+	else if (attr == &dev_max_ec_data)
+		ret = sprintf(buf, "%d\n", ubi_calc_max_ec(ubi, UBI_FM_MAX_START, ubi->peb_count));
 	else if (attr == &dev_mean_ec_data)
 		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, UBI_FM_MAX_START, ubi->peb_count));
 #endif
@@ -498,6 +502,7 @@ static struct attribute *ubi_dev_attrs[] = {
 #ifdef CONFIG_MTD_UBI_FASTMAP
 	&dev_max_ec_fastmap.attr,
 	&dev_mean_ec_fastmap.attr,
+	&dev_max_ec_data.attr,
 	&dev_mean_ec_data.attr,
 #endif
 	&dev_reserved_for_bad.attr,
Signature · 6 lines
2.30.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
reviewer Reviewed-by on v2 5/10
#8 lore raw

Re: [PATCH v2 05/10] ubi: Expose max erase counter for data in sysfs

Quoted 10 lines · Rickard Andersson

Expose max erase counter for data area in sysfs. Data area includes all blocks except for the fastmap area.

This value can be used in order to estimate life expectancy of the flash.

---
  drivers/mtd/ubi/build.c | 5 +++++
  1 file changed, 5 insertions(+)
Quoted 25 lines
drivers/mtd/ubi/build.c +4 -0
index b253dcf849c7..f3649afbc777 100644
@@ -138,6 +138,8 @@ static struct device_attribute dev_max_ec_fastmap =
  	__ATTR(max_ec_fastmap, S_IRUGO, dev_attribute_show, NULL);
  static struct device_attribute dev_mean_ec_fastmap =
  	__ATTR(mean_ec_fastmap, S_IRUGO, dev_attribute_show, NULL);
+static struct device_attribute dev_max_ec_data =
+	__ATTR(max_ec_data, S_IRUGO, dev_attribute_show, NULL);
  static struct device_attribute dev_mean_ec_data =
  	__ATTR(mean_ec_data, S_IRUGO, dev_attribute_show, NULL);
  #endif
@@ -465,6 +467,8 @@ static ssize_t dev_attribute_show(struct device *dev,
  		ret = sprintf(buf, "%d\n", ubi_calc_max_ec(ubi, 0, UBI_FM_MAX_START));
  	else if (attr == &dev_mean_ec_fastmap)
  		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, 0, UBI_FM_MAX_START));
+	else if (attr == &dev_max_ec_data)
+		ret = sprintf(buf, "%d\n", ubi_calc_max_ec(ubi, UBI_FM_MAX_START, ubi->peb_count));
  	else if (attr == &dev_mean_ec_data)
  		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, UBI_FM_MAX_START, ubi->peb_count));
  #endif

drivers/mtd/ubi/build.c static struct attribute *ubi_dev_attrs[] = { · line 508

Quoted 7 lines
drivers/mtd/ubi/build.c +1 -0
@@ -498,6 +502,7 @@ static struct attribute *ubi_dev_attrs[] = {
  #ifdef CONFIG_MTD_UBI_FASTMAP
  	&dev_max_ec_fastmap.attr,
  	&dev_mean_ec_fastmap.attr,
+	&dev_max_ec_data.attr,
  	&dev_mean_ec_data.attr,
  #endif
  	&dev_reserved_for_bad.attr,

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

#9 lore raw

PATCH v2 6/10 ubi: Add mean erase counter sysfs attribute

In reply to Rickard Andersson

A mean erase counter gives a better understanding of the flash wear.

---
 Documentation/ABI/stable/sysfs-class-ubi | 7 +++++++
 1 file changed, 7 insertions(+)
Documentation/ABI/stable/sysfs-class-ubi +7 -0
index a6b324014692..535a3e6dab12 100644
@@ -219,3 +219,10 @@ Description:
 		Logical eraseblock size of this volume. Equivalent to logical
 		eraseblock size of the device aligned on the volume alignment
 		value.
+
+What:		/sys/class/ubi/ubiX/mean_ec
+Date:		October 2024
+KernelVersion:	6.X
+Contact:	[email protected]
+Description:
+		Mean physical eraseblock erase counter value.
Signature · 6 lines
2.30.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
reviewer Reviewed-by on v2 6/10
#10 lore raw

Re: [PATCH v2 06/10] ubi: Add mean erase counter sysfs attribute

Quoted 8 lines · Rickard Andersson

A mean erase counter gives a better understanding of the flash wear.

---
  Documentation/ABI/stable/sysfs-class-ubi | 7 +++++++
  1 file changed, 7 insertions(+)
Quoted 6 lines
Documentation/ABI/stable/sysfs-class-ubi +0 -0
index a6b324014692..535a3e6dab12 100644

Documentation/ABI/stable/sysfs-class-ubi Description: · line 228

Quoted 10 lines
Documentation/ABI/stable/sysfs-class-ubi +7 -0
@@ -219,3 +219,10 @@ Description:
  		Logical eraseblock size of this volume. Equivalent to logical
  		eraseblock size of the device aligned on the volume alignment
  		value.
+
+What:		/sys/class/ubi/ubiX/mean_ec
+Date:		October 2024
+KernelVersion:	6.X
+Contact:	[email protected]
+Description:
+		Mean physical eraseblock erase counter value.

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

#11 lore raw

PATCH v2 7/10 ubi: Add mean erase counter sysfs attribute for fastmap area

In reply to Rickard Andersson

A mean erase counter gives a better understanding of the flash wear of the fastmap area.

---
 Documentation/ABI/stable/sysfs-class-ubi | 8 ++++++++
 1 file changed, 8 insertions(+)
Documentation/ABI/stable/sysfs-class-ubi +8 -0
index 535a3e6dab12..05a701b03a9a 100644
@@ -226,3 +226,11 @@ KernelVersion:	6.X
 Contact:	[email protected]
 Description:
 		Mean physical eraseblock erase counter value.
+
+What:		/sys/class/ubi/ubiX/mean_ec_fastmap
+Date:		October 2024
+KernelVersion:	6.X
+Contact:	[email protected]
+Description:
+		Mean physical eraseblock erase counter value for the fastmap
+		area.
Signature · 6 lines
2.30.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
reviewer Reviewed-by on v2 7/10
#12 lore raw

Re: [PATCH v2 07/10] ubi: Add mean erase counter sysfs attribute for fastmap area

Quoted 7 lines · Rickard Andersson

A mean erase counter gives a better understanding of the flash wear of the fastmap area.

---
  Documentation/ABI/stable/sysfs-class-ubi | 8 ++++++++
  1 file changed, 8 insertions(+)
Quoted 8 lines
Documentation/ABI/stable/sysfs-class-ubi +0 -0
index 535a3e6dab12..05a701b03a9a 100644
@@ -226,3 +226,11 @@ KernelVersion:	6.X
  Contact:	[email protected]

Documentation/ABI/stable/sysfs-class-ubi KernelVersion: 6.X · line 236

Quoted 10 lines
Documentation/ABI/stable/sysfs-class-ubi +8 -0
  Description:
  		Mean physical eraseblock erase counter value.
+
+What:		/sys/class/ubi/ubiX/mean_ec_fastmap
+Date:		October 2024
+KernelVersion:	6.X
+Contact:	[email protected]
+Description:
+		Mean physical eraseblock erase counter value for the fastmap
+		area.

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

#13 lore raw

PATCH v2 8/10 ubi: Add max erase counter sysfs attribute for fastmap area

In reply to Rickard Andersson

A max erase counter specifically for the fastmap area gives a better understanding of the flash wear.

---
 Documentation/ABI/stable/sysfs-class-ubi | 8 ++++++++
 1 file changed, 8 insertions(+)
Documentation/ABI/stable/sysfs-class-ubi +8 -0
index 05a701b03a9a..62c93fda0bc4 100644
@@ -78,6 +78,14 @@ Contact:	Artem Bityutskiy <[email protected]>
 Description:
 		Maximum physical eraseblock erase counter value.
 
+What:		/sys/class/ubi/ubiX/max_ec_fastmap
+Date:		October 2024
+KernelVersion:	6.X
+Contact:	[email protected]
+Description:
+		Maximum physical eraseblock erase counter value for a block
+		in the fastmap area.
+
 What:		/sys/class/ubi/ubiX/max_vol_count
 Date:		July 2006
 KernelVersion:	2.6.22
Signature · 6 lines
2.30.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
reviewer Reviewed-by on v2 8/10
#14 lore raw

Re: [PATCH v2 08/10] ubi: Add max erase counter sysfs attribute for fastmap area

Quoted 7 lines · Rickard Andersson

A max erase counter specifically for the fastmap area gives a better understanding of the flash wear.

---
  Documentation/ABI/stable/sysfs-class-ubi | 8 ++++++++
  1 file changed, 8 insertions(+)
Quoted 11 lines
Documentation/ABI/stable/sysfs-class-ubi +1 -0
index 05a701b03a9a..62c93fda0bc4 100644
@@ -78,6 +78,14 @@ Contact:	Artem Bityutskiy <[email protected]>
  Description:
  		Maximum physical eraseblock erase counter value.
 
+What:		/sys/class/ubi/ubiX/max_ec_fastmap

Documentation/ABI/stable/sysfs-class-ubi Contact: Artem Bityutskiy <[email protected]> · line 91

Quoted 10 lines
Documentation/ABI/stable/sysfs-class-ubi +7 -0
+Date:		October 2024
+KernelVersion:	6.X
+Contact:	[email protected]
+Description:
+		Maximum physical eraseblock erase counter value for a block
+		in the fastmap area.
+
  What:		/sys/class/ubi/ubiX/max_vol_count
  Date:		July 2006
  KernelVersion:	2.6.22

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

#15 lore raw

PATCH v2 9/10 ubi: Add mean erase counter sysfs attribute for data area

In reply to Rickard Andersson

Data area includes all blocks except for the fastmap area.

---
 Documentation/ABI/stable/sysfs-class-ubi | 8 ++++++++
 1 file changed, 8 insertions(+)
Documentation/ABI/stable/sysfs-class-ubi +8 -0
index 62c93fda0bc4..100e1c0698a3 100644
@@ -242,3 +242,11 @@ Contact:	[email protected]
 Description:
 		Mean physical eraseblock erase counter value for the fastmap
 		area.
+
+What:		/sys/class/ubi/ubiX/mean_ec_data
+Date:		October 2024
+KernelVersion:	6.X
+Contact:	[email protected]
+Description:
+		Mean physical eraseblock erase counter value for data area.
+		Data area includes all blocks except for the fastmap area.
Signature · 6 lines
2.30.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
reviewer Reviewed-by on v2 9/10
#16 lore raw

Re: [PATCH v2 09/10] ubi: Add mean erase counter sysfs attribute for data area

Quoted 6 lines · Rickard Andersson

Data area includes all blocks except for the fastmap area.

---
  Documentation/ABI/stable/sysfs-class-ubi | 8 ++++++++
  1 file changed, 8 insertions(+)
Quoted 8 lines
Documentation/ABI/stable/sysfs-class-ubi +0 -0
index 62c93fda0bc4..100e1c0698a3 100644
@@ -242,3 +242,11 @@ Contact:	[email protected]
  Description:

Documentation/ABI/stable/sysfs-class-ubi Contact: [email protected] · line 252

Quoted 10 lines
Documentation/ABI/stable/sysfs-class-ubi +8 -0
  		Mean physical eraseblock erase counter value for the fastmap
  		area.
+
+What:		/sys/class/ubi/ubiX/mean_ec_data
+Date:		October 2024
+KernelVersion:	6.X
+Contact:	[email protected]
+Description:
+		Mean physical eraseblock erase counter value for data area.
+		Data area includes all blocks except for the fastmap area.

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

#17 lore raw

PATCH v2 10/10 ubi: Add max erase counter sysfs attribute for data area

In reply to Rickard Andersson

Data area includes all blocks except for the fastmap area.

---
 Documentation/ABI/stable/sysfs-class-ubi | 8 ++++++++
 1 file changed, 8 insertions(+)
Documentation/ABI/stable/sysfs-class-ubi +8 -0
index 100e1c0698a3..ff453557b0db 100644
@@ -86,6 +86,14 @@ Description:
 		Maximum physical eraseblock erase counter value for a block
 		in the fastmap area.
 
+What:		/sys/class/ubi/ubiX/max_ec_data
+Date:		October 2024
+KernelVersion:	6.X
+Contact:	[email protected]
+Description:
+		Max physical eraseblock erase counter value for data area.
+		Data area includes all blocks except for the fastmap area.
+
 What:		/sys/class/ubi/ubiX/max_vol_count
 Date:		July 2006
 KernelVersion:	2.6.22
Signature · 6 lines
2.30.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
reviewer Reviewed-by on v2 10/10
#18 lore raw

Re: [PATCH v2 10/10] ubi: Add max erase counter sysfs attribute for data area

Quoted 6 lines · Rickard Andersson

Data area includes all blocks except for the fastmap area.

---
  Documentation/ABI/stable/sysfs-class-ubi | 8 ++++++++
  1 file changed, 8 insertions(+)
Quoted 11 lines
Documentation/ABI/stable/sysfs-class-ubi +1 -0
index 100e1c0698a3..ff453557b0db 100644
@@ -86,6 +86,14 @@ Description:
  		Maximum physical eraseblock erase counter value for a block
  		in the fastmap area.
 
+What:		/sys/class/ubi/ubiX/max_ec_data

Documentation/ABI/stable/sysfs-class-ubi Description: · line 99

Quoted 10 lines
Documentation/ABI/stable/sysfs-class-ubi +7 -0
+Date:		October 2024
+KernelVersion:	6.X
+Contact:	[email protected]
+Description:
+		Max physical eraseblock erase counter value for data area.
+		Data area includes all blocks except for the fastmap area.
+
  What:		/sys/class/ubi/ubiX/max_vol_count
  Date:		July 2006
  KernelVersion:	2.6.22

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

reviewer
#19 lore raw
In reply to Rickard Andersson
Quoted 36 lines · Rickard Andersson

Currently, only "max_ec" can be read from sysfs, which provides a limited view of the flash device’s wear. In certain cases, such as bugs in the wear-leveling algorithm, specific blocks can be worn down more than others, resulting in uneven wear distribution. Providing a mean erase counter value gives a better understanding of the overall flash wear.
There exists more detailed info in debugfs, but this information is only available for debug builds.

This patch calculates the mean value only when it is requested. This calculation typically takes 2.7 ms for an UBI device with 4091 blocks running on a single core Cortex-A9 at 792 MHz.

---
  drivers/mtd/ubi/build.c | 48 +++++++++++++++++++++++++++++++++++++++++
  1 file changed, 48 insertions(+)
drivers/mtd/ubi/build.c +3 -0
index f5140a78a4a8..fa38c434e447 100644
@@ -131,6 +131,8 @@ static struct device_attribute dev_volumes_count =
  	__ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
  static struct device_attribute dev_max_ec =
  	__ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
+static struct device_attribute dev_mean_ec =
+	__ATTR(mean_ec, S_IRUGO, dev_attribute_show, NULL);
  static struct device_attribute dev_reserved_for_bad =
  	__ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
  static struct device_attribute dev_bad_peb_count =
@@ -337,6 +339,49 @@ int ubi_major2num(int major)
  	return ubi_num;
  }
 
+/**

drivers/mtd/ubi/build.c int ubi_major2num(int major) · line 352

Quoted 10 lines
drivers/mtd/ubi/build.c +9 -0
+ * ubi_calc_mean_ec - calculate mean erase counter value.
+ * @ubi: UBI device description object
+ * @start_peb: First PEB in the range
+ * @end_peb: End PEB in the half-open range
+ *
+ * Returns the mean value of all non-bad blocks of the device that are
+ * in the range. Range is half-open i.e end_peb is not actually included.
+ *
 
+ * Returns zero if it was not possible to calculate the mean value

Delete this line from the comments, since zero is a normal value if all PEBs' erase counter is initialized as '0' by ubiformat tool.

Quoted 47 lines
drivers/mtd/ubi/build.c +33 -0
+ */
+static int ubi_calc_mean_ec(struct ubi_device *ubi, int start_peb, int end_peb)
+{
+	struct ubi_wl_entry *wl;
+	int peb;
+	int ec_count = 0;
+	int mean_ec = 0;
+	uint64_t ec_sum = 0;
+
+	for (peb = start_peb; peb < end_peb; peb++) {
+		int err;
+
+		err = ubi_io_is_bad(ubi, peb);
+		if (err)
+			continue;
+
+		spin_lock(&ubi->wl_lock);
+
+		wl = ubi->lookuptbl[peb];
+		if (wl) {
+			ec_sum += wl->ec;
+			ec_count++;
+		}
+
+		spin_unlock(&ubi->wl_lock);
+	}
+
+	if (ec_count > 0)
+		mean_ec = div_u64(ec_sum, ec_count);
+
+	return mean_ec;
+}
+
  /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
  static ssize_t dev_attribute_show(struct device *dev,

struct device_attribute *attr, char *buf)

drivers/mtd/ubi/build.c +2 -0
@@ -366,6 +411,8 @@ static ssize_t dev_attribute_show(struct device *dev,
  		ret = sprintf(buf, "%d\n", ubi->vol_count - UBI_INT_VOL_COUNT);
  	else if (attr == &dev_max_ec)
  		ret = sprintf(buf, "%d\n", ubi->max_ec);
+	else if (attr == &dev_mean_ec)
+		ret = sprintf(buf, "%d\n", ubi_calc_mean_ec(ubi, 0, ubi->peb_count));
  	else if (attr == &dev_reserved_for_bad)
  		ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
  	else if (attr == &dev_bad_peb_count)

drivers/mtd/ubi/build.c static struct attribute *ubi_dev_attrs[] = { · line 445

Quoted 7 lines
drivers/mtd/ubi/build.c +1 -0
@@ -392,6 +439,7 @@ static struct attribute *ubi_dev_attrs[] = {
  	&dev_total_eraseblocks.attr,
  	&dev_volumes_count.attr,
  	&dev_max_ec.attr,
+	&dev_mean_ec.attr,
  	&dev_reserved_for_bad.attr,
  	&dev_bad_peb_count.attr,
  	&dev_max_vol_count.attr,

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