kernel: fix mtd partition erase < parent_erasesize writes
authorJohn Thomson <git@johnthomson.fastmail.com.au>
Wed, 5 Aug 2020 21:13:54 +0000 (07:13 +1000)
committerAdrian Schmutzler <freifunk@adrianschmutzler.de>
Sat, 19 Sep 2020 10:41:29 +0000 (12:41 +0200)
This bug applied where mtd partition end address,
or erase start address, was not cleanly divisible by parent mtd erasesize.

This error would cause the bits following the end of the partition
to the next erasesize block boundary to be erased,
and this partition-overflow data to be written to the partition erase
address (missing additional partition offset address)
of the mtd (top) parent device.

Fixes: FS#2428
Signed-off-by: John Thomson <git@johnthomson.fastmail.com.au>
Tested-by: Tomasz Maciej Nowak <tomek_n@o2.pl>
[shorten commit title, add Fixes, fix kernel 4.19 as well]
Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
target/linux/generic/pending-4.19/411-mtd-partial_eraseblock_write.patch
target/linux/generic/pending-4.19/412-mtd-partial_eraseblock_unlock.patch
target/linux/generic/pending-5.4/411-mtd-partial_eraseblock_write.patch
target/linux/generic/pending-5.4/412-mtd-partial_eraseblock_unlock.patch

index f3a314ae02e16def6ffaace8593ef89390ec703f..f945122149229b11363fa3ba2da536a8d5e881f6 100644 (file)
@@ -19,7 +19,7 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
  /* Our partition linked list */
  static LIST_HEAD(mtd_partitions);
  static DEFINE_MUTEX(mtd_partitions_mutex);
-@@ -220,6 +222,53 @@ static int part_erase(struct mtd_info *m
+@@ -220,11 +222,77 @@ static int part_erase(struct mtd_info *m
  {
        struct mtd_part *part = mtd_to_part(mtd);
        int ret;
@@ -73,10 +73,9 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
  
        instr->addr += part->offset;
        ret = part->parent->_erase(part->parent, instr);
-@@ -227,6 +276,24 @@ static int part_erase(struct mtd_info *m
+       if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
                instr->fail_addr -= part->offset;
-       instr->addr -= part->offset;
++
 +      if (mtd->flags & MTD_ERASE_PARTIAL) {
 +              if (partial_start) {
 +                      part->parent->_write(part->parent,
@@ -95,10 +94,10 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
 +              kfree(erase_buf);
 +      }
 +
-       return ret;
- }
+       instr->addr -= part->offset;
  
-@@ -539,19 +606,22 @@ static struct mtd_part *allocate_partiti
+       return ret;
+@@ -539,19 +607,22 @@ static struct mtd_part *allocate_partiti
        remainder = do_div(tmp, wr_alignment);
        if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
                /* Doesn't start on a boundary of major erase size */
index a54603a0f893800b32ccac5c1e37a6ad8af551c4..e038c4e60c0fb0c73dd8a7f28d19d186d237ad5b 100644 (file)
@@ -20,7 +20,7 @@ Signed-off-by: Tim Harvey <tharvey@gateworks.com>
 
 --- a/drivers/mtd/mtdpart.c
 +++ b/drivers/mtd/mtdpart.c
-@@ -306,7 +306,16 @@ static int part_lock(struct mtd_info *mt
+@@ -307,7 +307,16 @@ static int part_lock(struct mtd_info *mt
  static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  {
        struct mtd_part *part = mtd_to_part(mtd);
index b46c3f5ed42e1ff342c5778ab77ea4da8ed000f0..c48a144d3de66437b8c6f0f03bfd9d7bf98a5666 100644 (file)
@@ -19,7 +19,7 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
  /* Our partition linked list */
  static LIST_HEAD(mtd_partitions);
  static DEFINE_MUTEX(mtd_partitions_mutex);
-@@ -206,6 +208,53 @@ static int part_erase(struct mtd_info *m
+@@ -206,11 +208,77 @@ static int part_erase(struct mtd_info *m
  {
        struct mtd_part *part = mtd_to_part(mtd);
        int ret;
@@ -73,10 +73,9 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
  
        instr->addr += part->offset;
        ret = part->parent->_erase(part->parent, instr);
-@@ -213,6 +262,24 @@ static int part_erase(struct mtd_info *m
+       if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
                instr->fail_addr -= part->offset;
-       instr->addr -= part->offset;
++
 +      if (mtd->flags & MTD_ERASE_PARTIAL) {
 +              if (partial_start) {
 +                      part->parent->_write(part->parent,
@@ -95,10 +94,10 @@ Signed-off-by: Felix Fietkau <nbd@nbd.name>
 +              kfree(erase_buf);
 +      }
 +
-       return ret;
- }
+       instr->addr -= part->offset;
  
-@@ -525,19 +592,22 @@ static struct mtd_part *allocate_partiti
+       return ret;
+@@ -525,19 +593,22 @@ static struct mtd_part *allocate_partiti
        remainder = do_div(tmp, wr_alignment);
        if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
                /* Doesn't start on a boundary of major erase size */
index 348fb9a842ceccf2b3fd24a3f0fdc303d9953f51..b23bc1b20161aa283348a0dba9042447fc0f02df 100644 (file)
@@ -20,7 +20,7 @@ Signed-off-by: Tim Harvey <tharvey@gateworks.com>
 
 --- a/drivers/mtd/mtdpart.c
 +++ b/drivers/mtd/mtdpart.c
-@@ -292,7 +292,16 @@ static int part_lock(struct mtd_info *mt
+@@ -293,7 +293,16 @@ static int part_lock(struct mtd_info *mt
  static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  {
        struct mtd_part *part = mtd_to_part(mtd);