UBI: misc patches
Subsystems (2)
cover UBI: misc patches
Hi,
here are various misc patches which I created few months ago but then forgot about them.
Artem.
PATCH 2/5 UBI: use raw mtd read function in debugging code
From: Artem Bityutskiy <[email protected]>
This change affects only the debugging code. Namely, use mtd->read() function instead of ubi_io_read() to avoid bit-flips injection (ubi_dbg_is_bitflip()) which we do not want on the debugging path.
--- drivers/mtd/ubi/io.c | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-)
drivers/mtd/ubi/io.c +4 -2
@@ -1325,10 +1325,12 @@ int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum, int offset, int len) { int err, i; +size_t read; +loff_t addr = (loff_t )pnum * ubi->peb_size + offset;mutex_lock (&ubi->dbg_buf_mutex); - err =ubi_io_read (ubi, ubi->dbg_peb_buf, pnum, offset, len); -if (err) + err = ubi->mtd->read (ubi->mtd, addr, len, &read, ubi->dbg_peb_buf); +if (err && err != -EUCLEAN ) goto out_unlock;for (i =0 ; i < len; i++) {
Signature · 1 line
1.7.2.3
PATCH 5/5 UBI: always re-read in case of read failures
In reply to Artem BityutskiyFrom: Artem Bityutskiy <[email protected]>
When the read operation fails, UBI tries to re-read several times in a hope that one of the subsequent reads may succeed. However, currently UBI re-reads only if MTD failed to read all data, but does not re-reads if all the data were read, but with an integrity error (-EBADMSB). This patch makes UBI to always re-try reading.
This should be useful for reading NAND pages with unstable bits - re-reading may help to get correct data.
--- drivers/mtd/ubi/io.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/mtd/ubi/io.c +1 -1
@@ -188,7 +188,7 @@ retry:return UBI_IO_BITFLIPS ; } -if (read != len && retries++ <UBI_IO_RETRIES ) { +if (retries++ <UBI_IO_RETRIES ) {dbg_io ("error %d%s while reading %d bytes from PEB %d:%d," " read only %zd bytes, retry" , err, errstr, len, pnum, offset, read);
Signature · 1 line
1.7.2.3
PATCH 1/5 UBI: try to reveal buggy MTD drivers
In reply to Artem BityutskiyFrom: Artem Bityutskiy <[email protected]>
When reading data from the flash, corrupt the buffer we are about to read to. The idea is to fix the following possible situation:
1. The buffer contains data from previous operation, e.g., read from another PEB previously. The data looks like expected, e.g., if we just do not read anything and return - the caller would not notice this. E.g., if we are reading a VID header, the buffer may contain a valid VID header from another PEB.
2. The driver is buggy and returns use success or -EBADMSG or -EUCLEAN, but it does not actually put any data to the buffer.
This may confuse UBI or upper layers - they may think the buffer contains valid data while in fact it is just old data.
Thus, try to reveal such buggy MTD drivers with simple debugging code which fills the read buffer with 0x12 constant.
--- drivers/mtd/ubi/io.c | 22 ++++++++++++++++++++++ 1 files changed, 22 insertions(+), 0 deletions(-)
drivers/mtd/ubi/io.c +22 -0
@@ -146,6 +146,28 @@ int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,if (err)return err; +/* +* Deliberately corrupt the buffer to improve robustness. Indeed, if we +* do not do this, the following may happen: +* 1. The buffer contains data from previous operation, e.g., read from +* another PEB previously. The data looks like expected, e.g., if we +* just do not read anything and return - the caller would not +* notice this. E.g., if we are reading a VID header, the buffer may +* contain a valid VID header from another PEB. +* 2. The driver is buggy and returns us success or -EBADMSG or +* -EUCLEAN, but it does not actually put any data to the buffer. +* +* This may confuse UBI or upper layers - they may think the buffer +* contains valid data while in fact it is just old data. This is +* especially possible because UBI (and UBIFS) relies on CRC, and +* treats data as correct even in case of ECC errors if the CRC is +* correct. +* +* Try to prevent this situation by changing the first byte of the +* buffer. +*/ + *((uint8_t *)buf) ^=0xFF ; + addr = (loff_t )pnum * ubi->peb_size + offset; retry: err = ubi->mtd->read (ubi->mtd, addr, len, &read, buf);
Signature · 1 line
1.7.2.3
PATCH 3/5 UBI: add slab cache for ubi_scan_leb objects
In reply to Artem BityutskiyFrom: Artem Bityutskiy <[email protected]>
During scanning UBI allocates one struct ubi_scan_leb object for each PEB, so it can end up allocating thousands of them. Use slab cache to reduce memory consumption for these 48-byte objects, because currently used 'kmalloc()' ends up allocating 64 bytes per object, instead of 48.
--- drivers/mtd/ubi/build.c | 2 ++ drivers/mtd/ubi/scan.c | 32 +++++++++++++++++++++----------- drivers/mtd/ubi/scan.h | 2 ++ 3 files changed, 25 insertions(+), 11 deletions(-)
drivers/mtd/ubi/build.c +2 -0
@@ -923,6 +923,8 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)spin_lock_init (&ubi->volumes_lock);ubi_msg ("attaching mtd%d to ubi%d" , mtd->index, ubi_num); +dbg_msg ("sizeof(struct ubi_scan_leb) %zu" ,sizeof (struct ubi_scan_leb )); +dbg_msg ("sizeof(struct ubi_wl_entry) %zu" ,sizeof (struct ubi_wl_entry )); err =io_init (ubi);if (err)
drivers/mtd/ubi/scan.c +21 -11
@@ -115,7 +115,7 @@ static int add_to_list(struct ubi_scan_info *si, int pnum, int ec, int to_head, }else BUG (); - seb =kmalloc (sizeof (struct ubi_scan_leb ),GFP_KERNEL ); + seb =kmem_cache_alloc (si->scan_leb_slab,GFP_KERNEL );if (!seb)return -ENOMEM ;
@@ -144,7 +144,7 @@ static int add_corrupted(struct ubi_scan_info *si, int pnum, int ec)dbg_bld ("add to corrupted: PEB %d, EC %d" , pnum, ec); - seb =kmalloc (sizeof (struct ubi_scan_leb ),GFP_KERNEL ); + seb =kmem_cache_alloc (si->scan_leb_slab,GFP_KERNEL );if (!seb)return -ENOMEM ;
@@ -553,7 +553,7 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,if (err)return err; - seb =kmalloc (sizeof (struct ubi_scan_leb ),GFP_KERNEL ); + seb =kmem_cache_alloc (si->scan_leb_slab,GFP_KERNEL );if (!seb)return -ENOMEM ;
@@ -1152,9 +1152,15 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi) si->volumes =RB_ROOT ; err = -ENOMEM ; + si->scan_leb_slab =kmem_cache_create ("ubi_scan_leb_slab" , +sizeof (struct ubi_scan_leb ), +0 ,0 ,NULL ); +if (!si->scan_leb_slab) + goto out_si; + ech =kzalloc (ubi->ec_hdr_alsize,GFP_KERNEL );if (!ech) - goto out_si; + goto out_slab; vidh =ubi_zalloc_vid_hdr (ubi,GFP_KERNEL );if (!vidh)
@@ -1215,6 +1221,8 @@ out_vidh:ubi_free_vid_hdr (ubi, vidh); out_ech:kfree (ech); +out_slab: +kmem_cache_destroy (si->scan_leb_slab); out_si:ubi_scan_destroy_si (si);return ERR_PTR (err);
@@ -1223,11 +1231,12 @@ out_si:/** * destroy_sv - free the scanning volume information * @sv: scanning volume information +* @si: scanning information * * This function destroys the volume RB-tree (@sv->root) and the scanning * volume information. */ -static void destroy_sv (struct ubi_scan_volume *sv) +static void destroy_sv (struct ubi_scan_info *si,struct ubi_scan_volume *sv) {struct ubi_scan_leb *seb;struct rb_node *this = sv->root.rb_node;
@@ -1247,7 +1256,7 @@ static void destroy_sv(struct ubi_scan_volume *sv) this->rb_right =NULL ; } -kfree (seb); +kmem_cache_free (si->scan_leb_slab, seb); } }kfree (sv);
@@ -1265,19 +1274,19 @@ void ubi_scan_destroy_si(struct ubi_scan_info *si)list_for_each_entry_safe (seb, seb_tmp, &si->alien, u.list) {list_del (&seb->u.list); -kfree (seb); +kmem_cache_free (si->scan_leb_slab, seb); }list_for_each_entry_safe (seb, seb_tmp, &si->erase, u.list) {list_del (&seb->u.list); -kfree (seb); +kmem_cache_free (si->scan_leb_slab, seb); }list_for_each_entry_safe (seb, seb_tmp, &si->corr, u.list) {list_del (&seb->u.list); -kfree (seb); +kmem_cache_free (si->scan_leb_slab, seb); }list_for_each_entry_safe (seb, seb_tmp, &si->free, u.list) {list_del (&seb->u.list); -kfree (seb); +kmem_cache_free (si->scan_leb_slab, seb); }/* Destroy the volume RB-tree */
@@ -1298,10 +1307,11 @@ void ubi_scan_destroy_si(struct ubi_scan_info *si) rb->rb_right =NULL ; } -destroy_sv (sv); +destroy_sv (si, sv); } } +kmem_cache_destroy (si->scan_leb_slab);kfree (si); }
drivers/mtd/ubi/scan.h +2 -0
@@ -109,6 +109,7 @@ struct ubi_scan_volume {* @mean_ec: mean erase counter value * @ec_sum: a temporary variable used when calculating @mean_ec * @ec_count: a temporary variable used when calculating @mean_ec +* @scan_leb_slab: slab cache for &struct ubi_scan_leb objects * * This data structure contains the result of scanning and may be used by other * UBI sub-systems to build final UBI data structures, further error-recovery
@@ -134,6 +135,7 @@ struct ubi_scan_info {int mean_ec;uint64_t ec_sum;int ec_count; +struct kmem_cache *scan_leb_slab; };struct ubi_device ;
Signature · 1 line
1.7.2.3
PATCH 4/5 UBI: cleanup comments about corrupted PEBs
In reply to Artem BityutskiyFrom: Artem Bityutskiy <[email protected]>
Just make them a bit more readable and explanatory.
--- drivers/mtd/ubi/scan.c | 54 ++++++++++++++++++++++++++++++----------------- 1 files changed, 34 insertions(+), 20 deletions(-)
drivers/mtd/ubi/scan.c +34 -20
@@ -39,32 +39,46 @@* eraseblocks are put to the @free list and the physical eraseblock to be * erased are put to the @erase list. * +* About corruptions +* ~~~~~~~~~~~~~~~~~ +* +* UBI protects EC and VID headers with CRC-32 checksums, so it can detect +* whether the headers are corrupted or not. Sometimes UBI also protects the +* data with CRC-32, e.g., when it executes the atomic LEB change operation, or +* when it moves the contents of a PEB for wear-leveling purposes. +* * UBI tries to distinguish between 2 types of corruptions. -* 1. Corruptions caused by power cuts. These are harmless and expected -* corruptions and UBI tries to handle them gracefully, without printing too -* many warnings and error messages. The idea is that we do not lose -* important data in these case - we may lose only the data which was being -* written to the media just before the power cut happened, and the upper -* layers (e.g., UBIFS) are supposed to handle these situations. UBI puts -* these PEBs to the head of the @erase list and they are scheduled for -* erasure. +* +* 1. Corruptions caused by power cuts. These are expected corruptions and UBI +* tries to handle them gracefully, without printing too many warnings and +* error messages. The idea is that we do not lose important data in these case +* - we may lose only the data which was being written to the media just before +* the power cut happened, and the upper layers (e.g., UBIFS) are supposed to +* handle such data losses (e.g., by using the FS journal). +* +* When UBI detects a corruption (CRC-32 mismatch) in a PEB, and it looks like +* the reason is a power cut, UBI puts this PEB to the @erase list, and all +* PEBs in the @erase list are scheduled for erasure later. * * 2. Unexpected corruptions which are not caused by power cuts. During -* scanning, such PEBs are put to the @corr list and UBI preserves them. -* Obviously, this lessens the amount of available PEBs, and if at some -* point UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly -* informs about such PEBs every time the MTD device is attached. +* scanning, such PEBs are put to the @corr list and UBI preserves them. +* Obviously, this lessens the amount of available PEBs, and if at some point +* UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly informs +* about such PEBs every time the MTD device is attached. * * However, it is difficult to reliably distinguish between these types of -* corruptions and UBI's strategy is as follows. UBI assumes (2.) if the VID -* header is corrupted and the data area does not contain all 0xFFs, and there -* were not bit-flips or integrity errors while reading the data area. Otherwise -* UBI assumes (1.). The assumptions are: -* o if the data area contains only 0xFFs, there is no data, and it is safe -* to just erase this PEB. -* o if the data area has bit-flips and data integrity errors (ECC errors on +* corruptions and UBI's strategy is as follows. UBI assumes corruption type 2 +* if the VID header is corrupted and the data area does not contain all 0xFFs, +* and there were no bit-flips or integrity errors while reading the data area. +* Otherwise UBI assumes corruption type 1. So the decision criteria are as +* follows. +* o If the data area contains only 0xFFs, there is no data, and it is safe +* to just erase this PEB - this is corruption type 1. +* o If the data area has bit-flips or data integrity errors (ECC errors on * NAND), it is probably a PEB which was being erased when power cut -* happened. +* happened, so this is corruption type 1. However, this is just a guess, +* which might be wrong. +* o Otherwise this it corruption type 2. */ #include <linux/err.h>
Signature · 1 line
1.7.2.3