Skip to content

Commit b86a2c5

Browse files
Artem BityutskiyArtem Bityutskiy
authored andcommitted
UBI: do not switch to R/O mode on read errors
This patch improves UBI errors handling. ATM UBI switches to R/O mode when the WL worker fails to read the source PEB. This means that the upper layers (e.g., UBIFS) has no chances to unmap the erroneous PEB and fix the error. This patch changes this behaviour and makes UBI put PEBs like this into a separate RB-tree, thus preventing the WL worker from hitting the same read errors again and again. But there is a 10% limit on a maximum amount of PEBs like this. If there are too much of them, UBI switches to R/O mode. Additionally, this patch teaches UBI not to panic and switch to R/O mode if after a PEB has been copied, the target LEB cannot be read back. Instead, now UBI cancels the operation and schedules the target PEB for torturing. The error paths has been tested by ingecting errors into 'ubi_eba_copy_leb()'. Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
1 parent 87960c0 commit b86a2c5

4 files changed

Lines changed: 74 additions & 15 deletions

File tree

‎drivers/mtd/ubi/build.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,15 @@ static int io_init(struct ubi_device *ubi)
632632
return -EINVAL;
633633
}
634634

635+
/*
636+
* Set maximum amount of physical erroneous eraseblocks to be 10%.
637+
* Erroneous PEB are those which have read errors.
638+
*/
639+
ubi->max_erroneous = ubi->peb_count / 10;
640+
if (ubi->max_erroneous < 16)
641+
ubi->max_erroneous = 16;
642+
dbg_msg("max_erroneous %d", ubi->max_erroneous);
643+
635644
/*
636645
* It may happen that EC and VID headers are situated in one minimal
637646
* I/O unit. In this case we can only accept this UBI image in

‎drivers/mtd/ubi/eba.c‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,9 @@ int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
419419
* not implemented.
420420
*/
421421
if (err == UBI_IO_BAD_VID_HDR) {
422-
ubi_warn("bad VID header at PEB %d, LEB"
423-
"%d:%d", pnum, vol_id, lnum);
422+
ubi_warn("corrupted VID header at PEB "
423+
"%d, LEB %d:%d", pnum, vol_id,
424+
lnum);
424425
err = -EBADMSG;
425426
} else
426427
ubi_ro_mode(ubi);
@@ -1032,6 +1033,8 @@ int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
10321033
if (err && err != UBI_IO_BITFLIPS) {
10331034
ubi_warn("error %d while reading data from PEB %d",
10341035
err, from);
1036+
if (err == -EIO)
1037+
err = MOVE_SOURCE_RD_ERR;
10351038
goto out_unlock_buf;
10361039
}
10371040

@@ -1078,9 +1081,11 @@ int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
10781081
/* Read the VID header back and check if it was written correctly */
10791082
err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
10801083
if (err) {
1081-
if (err != UBI_IO_BITFLIPS)
1084+
if (err != UBI_IO_BITFLIPS) {
10821085
ubi_warn("cannot read VID header back from PEB %d", to);
1083-
else
1086+
if (err == -EIO)
1087+
err = MOVE_TARGET_RD_ERR;
1088+
} else
10841089
err = MOVE_CANCEL_BITFLIPS;
10851090
goto out_unlock_buf;
10861091
}
@@ -1102,10 +1107,12 @@ int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
11021107

11031108
err = ubi_io_read_data(ubi, ubi->peb_buf2, to, 0, aldata_size);
11041109
if (err) {
1105-
if (err != UBI_IO_BITFLIPS)
1110+
if (err != UBI_IO_BITFLIPS) {
11061111
ubi_warn("cannot read data back from PEB %d",
11071112
to);
1108-
else
1113+
if (err == -EIO)
1114+
err = MOVE_TARGET_RD_ERR;
1115+
} else
11091116
err = MOVE_CANCEL_BITFLIPS;
11101117
goto out_unlock_buf;
11111118
}

‎drivers/mtd/ubi/ubi.h‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,19 @@ enum {
105105
*
106106
* MOVE_CANCEL_RACE: canceled because the volume is being deleted, the source
107107
* PEB was put meanwhile, or there is I/O on the source PEB
108+
* MOVE_SOURCE_RD_ERR: canceled because there was a read error from the source
109+
* PEB
110+
* MOVE_TARGET_RD_ERR: canceled because there was a read error from the target
111+
* PEB
108112
* MOVE_TARGET_WR_ERR: canceled because there was a write error to the target
109113
* PEB
110114
* MOVE_CANCEL_BITFLIPS: canceled because a bit-flip was detected in the
111115
* target PEB
112116
*/
113117
enum {
114118
MOVE_CANCEL_RACE = 1,
119+
MOVE_SOURCE_RD_ERR,
120+
MOVE_TARGET_RD_ERR,
115121
MOVE_TARGET_WR_ERR,
116122
MOVE_CANCEL_BITFLIPS,
117123
};
@@ -334,14 +340,15 @@ struct ubi_wl_entry;
334340
* @alc_mutex: serializes "atomic LEB change" operations
335341
*
336342
* @used: RB-tree of used physical eraseblocks
343+
* @erroneous: RB-tree of erroneous used physical eraseblocks
337344
* @free: RB-tree of free physical eraseblocks
338345
* @scrub: RB-tree of physical eraseblocks which need scrubbing
339346
* @pq: protection queue (contain physical eraseblocks which are temporarily
340347
* protected from the wear-leveling worker)
341348
* @pq_head: protection queue head
342349
* @wl_lock: protects the @used, @free, @pq, @pq_head, @lookuptbl, @move_from,
343-
* @move_to, @move_to_put @erase_pending, @wl_scheduled and @works
344-
* fields
350+
* @move_to, @move_to_put @erase_pending, @wl_scheduled, @works and
351+
* @erroneous_peb_count fields
345352
* @move_mutex: serializes eraseblock moves
346353
* @work_sem: synchronizes the WL worker with use tasks
347354
* @wl_scheduled: non-zero if the wear-leveling was scheduled
@@ -361,6 +368,8 @@ struct ubi_wl_entry;
361368
* @peb_size: physical eraseblock size
362369
* @bad_peb_count: count of bad physical eraseblocks
363370
* @good_peb_count: count of good physical eraseblocks
371+
* @erroneous_peb_count: count of erroneous physical eraseblocks in @erroneous
372+
* @max_erroneous: maximum allowed amount of erroneous physical eraseblocks
364373
* @min_io_size: minimal input/output unit size of the underlying MTD device
365374
* @hdrs_min_io_size: minimal I/O unit size used for VID and EC headers
366375
* @ro_mode: if the UBI device is in read-only mode
@@ -418,6 +427,7 @@ struct ubi_device {
418427

419428
/* Wear-leveling sub-system's stuff */
420429
struct rb_root used;
430+
struct rb_root erroneous;
421431
struct rb_root free;
422432
struct rb_root scrub;
423433
struct list_head pq[UBI_PROT_QUEUE_LEN];
@@ -442,6 +452,8 @@ struct ubi_device {
442452
int peb_size;
443453
int bad_peb_count;
444454
int good_peb_count;
455+
int erroneous_peb_count;
456+
int max_erroneous;
445457
int min_io_size;
446458
int hdrs_min_io_size;
447459
int ro_mode;

‎drivers/mtd/ubi/wl.c‎

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
*
5656
* As it was said, for the UBI sub-system all physical eraseblocks are either
5757
* "free" or "used". Free eraseblock are kept in the @wl->free RB-tree, while
58-
* used eraseblocks are kept in @wl->used or @wl->scrub RB-trees, or
59-
* (temporarily) in the @wl->pq queue.
58+
* used eraseblocks are kept in @wl->used, @wl->erroneous, or @wl->scrub
59+
* RB-trees, as well as (temporarily) in the @wl->pq queue.
6060
*
6161
* When the WL sub-system returns a physical eraseblock, the physical
6262
* eraseblock is protected from being moved for some "time". For this reason,
@@ -83,6 +83,8 @@
8383
* used. The former state corresponds to the @wl->free tree. The latter state
8484
* is split up on several sub-states:
8585
* o the WL movement is allowed (@wl->used tree);
86+
* o the WL movement is disallowed (@wl->erroneous) becouse the PEB is
87+
* erroneous - e.g., there was a read error;
8688
* o the WL movement is temporarily prohibited (@wl->pq queue);
8789
* o scrubbing is needed (@wl->scrub tree).
8890
*
@@ -653,7 +655,7 @@ static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
653655
static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
654656
int cancel)
655657
{
656-
int err, scrubbing = 0, torture = 0, protect = 0;
658+
int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
657659
struct ubi_wl_entry *e1, *e2;
658660
struct ubi_vid_hdr *vid_hdr;
659661

@@ -769,13 +771,31 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
769771
goto out_not_moved;
770772
}
771773

772-
if (err == MOVE_CANCEL_BITFLIPS ||
773-
err == MOVE_TARGET_WR_ERR) {
774+
if (err == MOVE_CANCEL_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
775+
err == MOVE_TARGET_RD_ERR) {
774776
/* Target PEB bit-flips or write error, torture it */
775777
torture = 1;
776778
goto out_not_moved;
777779
}
778780

781+
if (err == MOVE_SOURCE_RD_ERR) {
782+
/*
783+
* An error happened while reading the source PEB. Do
784+
* not switch to R/O mode in this case, and give the
785+
* upper layers a possibility to recover from this,
786+
* e.g. by unmapping corresponding LEB. Instead, just
787+
* put thie PEB to the @ubi->erroneus list to prevent
788+
* UBI from trying to move the over and over again.
789+
*/
790+
if (ubi->erroneous_peb_count > ubi->max_erroneous) {
791+
ubi_err("too many erroneous eraseblocks (%d)",
792+
ubi->erroneous_peb_count);
793+
goto out_error;
794+
}
795+
erroneous = 1;
796+
goto out_not_moved;
797+
}
798+
779799
if (err < 0)
780800
goto out_error;
781801

@@ -832,7 +852,10 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
832852
spin_lock(&ubi->wl_lock);
833853
if (protect)
834854
prot_queue_add(ubi, e1);
835-
else if (scrubbing)
855+
else if (erroneous) {
856+
wl_tree_add(e1, &ubi->erroneous);
857+
ubi->erroneous_peb_count += 1;
858+
} else if (scrubbing)
836859
wl_tree_add(e1, &ubi->scrub);
837860
else
838861
wl_tree_add(e1, &ubi->used);
@@ -1116,6 +1139,13 @@ int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture)
11161139
} else if (in_wl_tree(e, &ubi->scrub)) {
11171140
paranoid_check_in_wl_tree(e, &ubi->scrub);
11181141
rb_erase(&e->u.rb, &ubi->scrub);
1142+
} else if (in_wl_tree(e, &ubi->erroneous)) {
1143+
paranoid_check_in_wl_tree(e, &ubi->erroneous);
1144+
rb_erase(&e->u.rb, &ubi->erroneous);
1145+
ubi->erroneous_peb_count -= 1;
1146+
ubi_assert(ubi->erroneous_peb_count >= 0);
1147+
/* Erronious PEBs should be tortured */
1148+
torture = 1;
11191149
} else {
11201150
err = prot_queue_del(ubi, e->pnum);
11211151
if (err) {
@@ -1364,7 +1394,7 @@ int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
13641394
struct ubi_scan_leb *seb, *tmp;
13651395
struct ubi_wl_entry *e;
13661396

1367-
ubi->used = ubi->free = ubi->scrub = RB_ROOT;
1397+
ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
13681398
spin_lock_init(&ubi->wl_lock);
13691399
mutex_init(&ubi->move_mutex);
13701400
init_rwsem(&ubi->work_sem);
@@ -1502,6 +1532,7 @@ void ubi_wl_close(struct ubi_device *ubi)
15021532
cancel_pending(ubi);
15031533
protection_queue_destroy(ubi);
15041534
tree_destroy(&ubi->used);
1535+
tree_destroy(&ubi->erroneous);
15051536
tree_destroy(&ubi->free);
15061537
tree_destroy(&ubi->scrub);
15071538
kfree(ubi->lookuptbl);

0 commit comments

Comments
 (0)