Skip to contentSkip to conversation

ubi: Expose interface for detailed erase counters

PATCH Superseded Rickard Andersson 3 participants 5 messages Nov 25, 2024 – Nov 26, 2024

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 ubi: Expose interface for detailed erase counters

Using the ioctl command 'UBI_IOCECNFO' user space can obtain detailed erase counter information of all blocks of a device.

---
 include/uapi/mtd/ubi-user.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
include/uapi/mtd/ubi-user.h +25 -0
index e1571603175e..667680e44ed4 100644
@@ -175,6 +175,8 @@
 #define UBI_IOCRPEB _IOW(UBI_IOC_MAGIC, 4, __s32)
 /* Force scrubbing on the specified PEB */
 #define UBI_IOCSPEB _IOW(UBI_IOC_MAGIC, 5, __s32)
+/* Read detailed device erase counter information */
+#define UBI_IOCECNFO _IOWR(UBI_IOC_MAGIC, 6, struct ubi_ecinfo_req)
 
 /* ioctl commands of the UBI control character device */
 
@@ -470,4 +472,27 @@ struct ubi_blkcreate_req {
 	__s8  padding[128];
 }  __packed;
 
+/**
+ * struct ubi_ecinfo_req - a data structure used in UBI device erase count
+ * information requests and responses.
+ *
+ * @start_ec_req: first erase counter block in range to read
+ * @end_ec_req: last erase counter block in the half-open range
+ * @end_ec_res: last erase counter block that was actually read
+ * @erase_counters: array of erase counter values
+ * @padding: reserved for future, not used, has to be zeroed
+ *
+ * Erase counters are read in range @start_ec_req to @end_ec_req. Range is half-open
+ * i.e erase counter of end_ec_req is not actually included.
+ * The read erase counters are placed in @erase_counters. If a block is bad or if the
+ * erase counter is unknown the value of the block will be set to -1.
+ */
+struct ubi_ecinfo_req {
+	__s32 start_ec_req;
+	__s32 end_ec_req;
+	__s32 end_ec_res;
+	__s32 erase_counters[1000];
+	__s8  padding[84];
+}  __packed;
+
 #endif /* __UBI_USER_H__ */
Signature · 6 lines
2.30.2


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

PATCH 2/2 ubi: Implement ioctl for detailed erase counters

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 detailed erase counter values give a better understanding of the overall flash wear.
There exists more detailed info in debugfs, but this information is only available for debug builds.

---
 drivers/mtd/ubi/cdev.c | 86 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)
drivers/mtd/ubi/cdev.c +86 -0
index 0d8f04cf03c5..4af99ddbba66 100644
@@ -828,6 +828,86 @@ static int rename_volumes(struct ubi_device *ubi,
 	return err;
 }
 
+
+static int ubi_get_ec_info(struct ubi_device *ubi, void __user *argp)
+{
+	struct ubi_ecinfo_req *req;
+	struct ubi_wl_entry *wl;
+	int peb;
+	int end_peb;
+	int i;
+	int err = 0;
+
+	req = kzalloc(sizeof(struct ubi_ecinfo_req), GFP_KERNEL);
+	if (!req)
+		return -ENOMEM;
+
+	/* Copy just the input arguments */
+	err = copy_from_user(req, argp, sizeof(struct ubi_ecinfo_req) -
+			     offsetof(struct ubi_ecinfo_req, end_ec_res));
+	if (err) {
+		err = -EFAULT;
+		goto out_free;
+	}
+
+	/* Make sure that start value is less or equal to end value */
+	if (req->start_ec_req > req->end_ec_req) {
+		err = -EINVAL;
+		goto out_free;
+	}
+
+	/* Check input argument start value */
+	if ((req->start_ec_req < 0 || req->start_ec_req >= ubi->peb_count)) {
+		err = -EINVAL;
+		goto out_free;
+	}
+
+	/* Check if end value is less than 0 */
+	if (req->end_ec_req < 0) {
+		err = -EINVAL;
+		goto out_free;
+	}
+
+	end_peb = req->end_ec_req;
+
+	/* Control end value */
+	if (end_peb > ubi->peb_count)
+		end_peb = ubi->peb_count;
+
+	/* Check that we do not overwrite our working memory */
+	if (end_peb > (ARRAY_SIZE(req->erase_counters) + req->start_ec_req))
+		end_peb = ARRAY_SIZE(req->erase_counters) + req->start_ec_req;
+
+	i = 0;
+	for (peb = req->start_ec_req; peb < end_peb; i++, peb++) {
+
+		if (ubi_io_is_bad(ubi, peb)) {
+			req->erase_counters[i] = UBI_UNKNOWN;
+			continue;
+		}
+
+		spin_lock(&ubi->wl_lock);
+
+		wl = ubi->lookuptbl[peb];
+		if (wl)
+			req->erase_counters[i] = wl->ec;
+		else
+			req->erase_counters[i] = UBI_UNKNOWN;
+
+		spin_unlock(&ubi->wl_lock);
+	}
+
+	/* Return last read peb */
+	req->end_ec_res = end_peb - 1;
+
+	if (copy_to_user(argp, req, sizeof(struct ubi_ecinfo_req)))
+		err = -EFAULT;
+
+out_free:
+	kfree(req);
+	return err;
+}
+
 static long ubi_cdev_ioctl(struct file *file, unsigned int cmd,
 			   unsigned long arg)
 {
@@ -991,6 +1071,12 @@ static long ubi_cdev_ioctl(struct file *file, unsigned int cmd,
 		break;
 	}
 
+	case UBI_IOCECNFO:
+	{
+		err = ubi_get_ec_info(ubi, argp);
+		break;
+	}
+
 	default:
 		err = -ENOTTY;
 		break;
Signature · 6 lines
2.30.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
#2 lore raw
In reply to Rickard Andersson

----- Ursprüngliche Mail -----

Quoted 18 lines

Von: "Rickard X Andersson" <[email protected]>
+/**
+ * struct ubi_ecinfo_req - a data structure used in UBI device erase count
+ * information requests and responses.
+ *
+ * @start_ec_req: first erase counter block in range to read
+ * @end_ec_req: last erase counter block in the half-open range
+ * @end_ec_res: last erase counter block that was actually read
+ * @erase_counters: array of erase counter values
+ * @padding: reserved for future, not used, has to be zeroed
+ *
+ * Erase counters are read in range @start_ec_req to @end_ec_req. Range is half-open
+ * i.e erase counter of end_ec_req is not actually included.
+ * The read erase counters are placed in @erase_counters. If a block is bad or if the
+ * erase counter is unknown the value of the block will be set to -1.
+ */

include/uapi/mtd/ubi-user.h struct ubi_blkcreate_req { · line 494

Quoted 5 lines
include/uapi/mtd/ubi-user.h +5 -0
+struct ubi_ecinfo_req {
+	__s32 start_ec_req;
+	__s32 end_ec_req;
+	__s32 end_ec_res;
+	__s32 erase_counters[1000];

There is no need to have a limited number. Just use a flexible array. If userspace asks for more entries than the provided buffer can hold, __put_user() will fail anyways.

Have a look at fibmap, I think you can the model the API the same way.

Thanks,
//richard

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

#3 lore raw

----- Ursprüngliche Mail -----

Quoted 37 lines

Von: "richard" <[email protected]>
An: "Rickard X Andersson" <[email protected]>
CC: "chengzhihao1" <[email protected]>, "linux-mtd" <[email protected]>, "rickard314 andersson" <[email protected]>, "kernel" <[email protected]>
Gesendet: Montag, 25. November 2024 16:44:54
Betreff: Re: [PATCH 1/2] ubi: Expose interface for detailed erase counters

----- Ursprüngliche Mail -----

Quoted 18 lines

Von: "Rickard X Andersson" <[email protected]>
+/**
+ * struct ubi_ecinfo_req - a data structure used in UBI device erase count
+ * information requests and responses.
+ *
+ * @start_ec_req: first erase counter block in range to read
+ * @end_ec_req: last erase counter block in the half-open range
+ * @end_ec_res: last erase counter block that was actually read
+ * @erase_counters: array of erase counter values
+ * @padding: reserved for future, not used, has to be zeroed
+ *
+ * Erase counters are read in range @start_ec_req to @end_ec_req. Range is half-open
+ * i.e erase counter of end_ec_req is not actually included.
+ * The read erase counters are placed in @erase_counters. If a block is bad or if the
+ * erase counter is unknown the value of the block will be set to -1.
+ */

include/uapi/mtd/ubi-user.h struct ubi_blkcreate_req { · line 494

Quoted 5 lines
include/uapi/mtd/ubi-user.h +5 -0
+struct ubi_ecinfo_req {
+	__s32 start_ec_req;
+	__s32 end_ec_req;
+	__s32 end_ec_res;
+	__s32 erase_counters[1000];

There is no need to have a limited number. Just use a flexible array. If userspace asks for more entries than the provided buffer can hold, __put_user()
will fail anyways.

Quoted 1 line

Have a look at fibmap, I think you can the model the API the same way.

*fiemap

Thanks,
//richard

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

reviewer
#4 lore raw
In reply to Rickard Andersson
Quoted 38 lines · Rickard Andersson

Using the ioctl command 'UBI_IOCECNFO' user space can obtain detailed erase counter information of all blocks of a device.

---
  include/uapi/mtd/ubi-user.h | 25 +++++++++++++++++++++++++
  1 file changed, 25 insertions(+)
include/uapi/mtd/ubi-user.h +15 -0
index e1571603175e..667680e44ed4 100644
@@ -175,6 +175,8 @@
  #define UBI_IOCRPEB _IOW(UBI_IOC_MAGIC, 4, __s32)
  /* Force scrubbing on the specified PEB */
  #define UBI_IOCSPEB _IOW(UBI_IOC_MAGIC, 5, __s32)
+/* Read detailed device erase counter information */
+#define UBI_IOCECNFO _IOWR(UBI_IOC_MAGIC, 6, struct ubi_ecinfo_req)
 
  /* ioctl commands of the UBI control character device */
 
@@ -470,4 +472,27 @@ struct ubi_blkcreate_req {
  	__s8  padding[128];
  }  __packed;
 
+/**
+ * struct ubi_ecinfo_req - a data structure used in UBI device erase count
+ * information requests and responses.
+ *
+ * @start_ec_req: first erase counter block in range to read
+ * @end_ec_req: last erase counter block in the half-open range
+ * @end_ec_res: last erase counter block that was actually read
+ * @erase_counters: array of erase counter values
+ * @padding: reserved for future, not used, has to be zeroed
+ *
+ * Erase counters are read in range @start_ec_req to @end_ec_req. Range is half-open
+ * i.e erase counter of end_ec_req is not actually included.
+ * The read erase counters are placed in @erase_counters. If a block is bad or if the

include/uapi/mtd/ubi-user.h struct ubi_blkcreate_req { · line 497

Quoted 10 lines
include/uapi/mtd/ubi-user.h +10 -0
+ * erase counter is unknown the value of the block will be set to -1.
+ */
+struct ubi_ecinfo_req {
+	__s32 start_ec_req;
+	__s32 end_ec_req;
+	__s32 end_ec_res;
+	__s32 erase_counters[1000];
+	__s8  padding[84];
+}  __packed;
+

Hi Rickard, please move the definition of 'ubi_ecinfo_req' between 'ubi_rnvol_req' and 'ubi_leb_change_req'. The last part in 'include/uapi/mtd/ubi-user.h' defines all structures for the ubi volume ioctl.

include/uapi/mtd/ubi-user.h struct ubi_blkcreate_req { · line 498

Quoted 2 lines
include/uapi/mtd/ubi-user.h +0 -0
  #endif /* __UBI_USER_H__ */

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