ubi: Expose the bitrot interface
authorRichard Weinberger <richard@nod.at>
Wed, 7 Nov 2018 22:16:19 +0000 (23:16 +0100)
committerRichard Weinberger <richard@nod.at>
Sun, 24 Feb 2019 10:40:45 +0000 (11:40 +0100)
Using UBI_IOCRPEB and UBI_IOCSPEB userspace can force
reading and scrubbing of PEBs.

In case of bitflips UBI will automatically take action
and move data to a different PEB.
This interface allows a daemon to foster your NAND.

Signed-off-by: Richard Weinberger <richard@nod.at>
drivers/mtd/ubi/cdev.c
drivers/mtd/ubi/ubi.h
drivers/mtd/ubi/wl.c
include/uapi/mtd/ubi-user.h

index 22547d7a84eaaf0108f07ce1ada68d66d58c8d35..947a8adbc79959a3160ee6a287b5cd06dc190592 100644 (file)
@@ -974,6 +974,36 @@ static long ubi_cdev_ioctl(struct file *file, unsigned int cmd,
                break;
        }
 
+       /* Check a specific PEB for bitflips and scrub it if needed */
+       case UBI_IOCRPEB:
+       {
+               int pnum;
+
+               err = get_user(pnum, (__user int32_t *)argp);
+               if (err) {
+                       err = -EFAULT;
+                       break;
+               }
+
+               err = ubi_bitflip_check(ubi, pnum, 0);
+               break;
+       }
+
+       /* Force scrubbing for a specific PEB */
+       case UBI_IOCSPEB:
+       {
+               int pnum;
+
+               err = get_user(pnum, (__user int32_t *)argp);
+               if (err) {
+                       err = -EFAULT;
+                       break;
+               }
+
+               err = ubi_bitflip_check(ubi, pnum, 1);
+               break;
+       }
+
        default:
                err = -ENOTTY;
                break;
index d47b9e436e6730af38e414821c0348f1e5ac7550..a1b9e764d489e4dbf810338fc4361596a9724798 100644 (file)
@@ -929,6 +929,7 @@ int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *used_e,
 int ubi_is_erase_work(struct ubi_work *wrk);
 void ubi_refill_pools(struct ubi_device *ubi);
 int ubi_ensure_anchor_pebs(struct ubi_device *ubi);
+int ubi_bitflip_check(struct ubi_device *ubi, int pnum, int force_scrub);
 
 /* io.c */
 int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
index ca1b31385eb5a6be0d22fe5b76d95f1a05a3e2e2..40f838d54b0fdf4646ade8417e97051378152f7e 100644 (file)
@@ -1440,6 +1440,150 @@ int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
        return err;
 }
 
+static bool scrub_possible(struct ubi_device *ubi, struct ubi_wl_entry *e)
+{
+       if (in_wl_tree(e, &ubi->scrub))
+               return false;
+       else if (in_wl_tree(e, &ubi->erroneous))
+               return false;
+       else if (ubi->move_from == e)
+               return false;
+       else if (ubi->move_to == e)
+               return false;
+
+       return true;
+}
+
+/**
+ * ubi_bitflip_check - Check an eraseblock for bitflips and scrub it if needed.
+ * @ubi: UBI device description object
+ * @pnum: the physical eraseblock to schedule
+ * @force: dont't read the block, assume bitflips happened and take action.
+ *
+ * This function reads the given eraseblock and checks if bitflips occured.
+ * In case of bitflips, the eraseblock is scheduled for scrubbing.
+ * If scrubbing is forced with @force, the eraseblock is not read,
+ * but scheduled for scrubbing right away.
+ *
+ * Returns:
+ * %EINVAL, PEB is out of range
+ * %ENOENT, PEB is no longer used by UBI
+ * %EBUSY, PEB cannot be checked now or a check is currently running on it
+ * %EAGAIN, bit flips happened but scrubbing is currently not possible
+ * %EUCLEAN, bit flips happened and PEB is scheduled for scrubbing
+ * %0, no bit flips detected
+ */
+int ubi_bitflip_check(struct ubi_device *ubi, int pnum, int force)
+{
+       int err;
+       struct ubi_wl_entry *e;
+
+       if (pnum < 0 || pnum >= ubi->peb_count) {
+               err = -EINVAL;
+               goto out;
+       }
+
+       /*
+        * Pause all parallel work, otherwise it can happen that the
+        * erase worker frees a wl entry under us.
+        */
+       down_write(&ubi->work_sem);
+
+       /*
+        * Make sure that the wl entry does not change state while
+        * inspecting it.
+        */
+       spin_lock(&ubi->wl_lock);
+       e = ubi->lookuptbl[pnum];
+       if (!e) {
+               spin_unlock(&ubi->wl_lock);
+               err = -ENOENT;
+               goto out_resume;
+       }
+
+       /*
+        * Does it make sense to check this PEB?
+        */
+       if (!scrub_possible(ubi, e)) {
+               spin_unlock(&ubi->wl_lock);
+               err = -EBUSY;
+               goto out_resume;
+       }
+       spin_unlock(&ubi->wl_lock);
+
+       if (!force) {
+               mutex_lock(&ubi->buf_mutex);
+               err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size);
+               mutex_unlock(&ubi->buf_mutex);
+       }
+
+       if (err == UBI_IO_BITFLIPS || force) {
+               /*
+                * Okay, bit flip happened, let's figure out what we can do.
+                */
+               spin_lock(&ubi->wl_lock);
+
+               /*
+                * Recheck. We released wl_lock, UBI might have killed the
+                * wl entry under us.
+                */
+               e = ubi->lookuptbl[pnum];
+               if (!e) {
+                       spin_unlock(&ubi->wl_lock);
+                       err = -ENOENT;
+                       goto out_resume;
+               }
+
+               /*
+                * Need to re-check state
+                */
+               if (!scrub_possible(ubi, e)) {
+                       spin_unlock(&ubi->wl_lock);
+                       err = -EBUSY;
+                       goto out_resume;
+               }
+
+               if (in_pq(ubi, e)) {
+                       prot_queue_del(ubi, e->pnum);
+                       wl_tree_add(e, &ubi->scrub);
+                       spin_unlock(&ubi->wl_lock);
+
+                       err = ensure_wear_leveling(ubi, 1);
+               } else if (in_wl_tree(e, &ubi->used)) {
+                       rb_erase(&e->u.rb, &ubi->used);
+                       wl_tree_add(e, &ubi->scrub);
+                       spin_unlock(&ubi->wl_lock);
+
+                       err = ensure_wear_leveling(ubi, 1);
+               } else if (in_wl_tree(e, &ubi->free)) {
+                       rb_erase(&e->u.rb, &ubi->free);
+                       ubi->free_count--;
+                       spin_unlock(&ubi->wl_lock);
+
+                       /*
+                        * This PEB is empty we can schedule it for
+                        * erasure right away. No wear leveling needed.
+                        */
+                       err = schedule_erase(ubi, e, UBI_UNKNOWN, UBI_UNKNOWN,
+                                            force ? 0 : 1, true);
+               } else {
+                       spin_unlock(&ubi->wl_lock);
+                       err = -EAGAIN;
+               }
+
+               if (!err && !force)
+                       err = -EUCLEAN;
+       } else {
+               err = 0;
+       }
+
+out_resume:
+       up_write(&ubi->work_sem);
+out:
+
+       return err;
+}
+
 /**
  * tree_destroy - destroy an RB-tree.
  * @ubi: UBI device description object
index aad3b6201fc06c1b5d792edec001bfe0bf96c2ba..b69e9ba6742b96ebf1988069f18c2143acaeb26a 100644 (file)
 /* Re-name volumes */
 #define UBI_IOCRNVOL _IOW(UBI_IOC_MAGIC, 3, struct ubi_rnvol_req)
 
+/* Read the specified PEB and scrub it if there are bitflips */
+#define UBI_IOCRPEB _IOW(UBI_IOC_MAGIC, 4, __s32)
+/* Force scrubbing on the specified PEB */
+#define UBI_IOCSPEB _IOW(UBI_IOC_MAGIC, 5, __s32)
+
 /* ioctl commands of the UBI control character device */
 
 #define UBI_CTRL_IOC_MAGIC 'o'