diff mbox series

[2/2] ubi: Implement ioctl for detailed erase counters

Message ID 20241125134820.560648-2-rickard.andersson@axis.com
State Superseded
Headers show
Series [1/2] ubi: Expose interface for detailed erase counters | expand

Commit Message

Rickard Andersson Nov. 25, 2024, 1:48 p.m. UTC
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.

Signed-off-by: Rickard Andersson <rickard.andersson@axis.com>
---
 drivers/mtd/ubi/cdev.c | 86 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)
diff mbox series

Patch

diff --git a/drivers/mtd/ubi/cdev.c b/drivers/mtd/ubi/cdev.c
index 0d8f04cf03c5..4af99ddbba66 100644
--- a/drivers/mtd/ubi/cdev.c
+++ b/drivers/mtd/ubi/cdev.c
@@ -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;