Skip to contentSkip to conversation

mtd: physmap: actually prevent unaligned accesses

PATCH In review Jonas Gorski 2 participants 6 messages Jul 21, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover mtd: physmap: actually prevent unaligned accesses

Commit d0788ce4924758249c9552c91cc33024d3434419 ("mtd: physmap_of: Add "no-unaligned-direct-access" DT property") intended to prevent unaligned accesses to the mapped flash by setting map.virt to NO_XIP.

But simple mapping's read_from/write_to still just call memcpy, which will do unaligned reads/writes at least on mips.

This causes problems on e.g. bcm63xx with smp, as both cores share a unaligned load/store unit, and unaligned reads sometimes read garbage.

This patchset aims at preventing unaligned accesses for good by making read_from/write_to check for NO_XIP and do a bytewise copy if one of to or from is unaligned.

Since this is mostly mtd stuff, it should go through the mtd tree.

Jonas Gorski (3):
mtd: simple_map: make NO_XIP actually prevent unaligned accesses
mtd: physmap: allow setting no_unaligned_access for !OF
MIPS: BCM63XX: disable unaligned accesses for SMP

 arch/mips/bcm63xx/dev-flash.c |  1 +
 drivers/mtd/maps/physmap.c    |  3 +++
 include/linux/mtd/map.h       | 26 +++++++++++++++++++++++---
 include/linux/mtd/physmap.h   |  1 +
 4 files changed, 28 insertions(+), 3 deletions(-)
Signature · 1 line
2.0.0
#1 lore raw

PATCH 1/3 mtd: simple_map: make NO_XIP actually prevent unaligned accesses

Some mapping drivers set the map->phys pointer to NO_XIP intending to mean "no unaligned accesses" (see also the DT property of physmap_of). But simple mapping read_from/write_to still just call memcpy, which will do unaligned reads/writes at least on mips.

To work around this, make the inline version check for NO_XIP and do a byte wise copy if "to" or "from" is unaligned.

---
 include/linux/mtd/map.h | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)
include/linux/mtd/map.h +23 -3
index 5f487d7..92a8a0a 100644
@@ -435,15 +435,35 @@ static inline void inline_map_write(struct map_info *map, const map_word datum,
 
 static inline void inline_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
 {
-	if (map->cached)
+	if (map->cached) {
 		memcpy(to, (char *)map->cached + from, len);
-	else
+	} else if (map->phys != NO_XIP ||
+		   (IS_ALIGNED((unsigned long)to, sizeof(long)) &&
+		    IS_ALIGNED((unsigned long)map->virt + from,
+			       sizeof(long)))) {
 		memcpy_fromio(to, map->virt + from, len);
+	} else {
+		void __iomem *src = map->virt + from;
+		char *dst = (char *)to;
+
+		while (len--)
+			*dst++ = readb(src++);
+	}
 }
 
 static inline void inline_map_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
 {
-	memcpy_toio(map->virt + to, from, len);
+	if (map->phys != NO_XIP ||
+	    (IS_ALIGNED((unsigned long)map->virt + to, sizeof(long)) &&
+	     IS_ALIGNED((unsigned long)from, sizeof(long)))) {
+		memcpy_toio(map->virt + to, from, len);
+	} else {
+		char *src = (char *)from;
+		void __iomem *dst = map->virt + to;
+
+		while (len--)
+			writeb(*src++, dst++);
+	}
 }
 
 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
Signature · 1 line
2.0.0
#2 lore raw

PATCH 2/3 mtd: physmap: allow setting no_unaligned_access for !OF

In reply to Jonas Gorski

Similar to physmap_of, allow preventing unaligned accesses to physmap also for non OF enabled platforms.

---
 drivers/mtd/maps/physmap.c  | 3 +++
 include/linux/mtd/physmap.h | 1 +
 2 files changed, 4 insertions(+)
drivers/mtd/maps/physmap.c +3 -0
index f73cd46..410190c 100644
@@ -153,6 +153,9 @@ static int physmap_flash_probe(struct platform_device *dev)
 
 		simple_map_init(&info->map[i]);
 
+		if (physmap_data->no_unaligned_access)
+			info->map[i].phys = NO_XIP;
+
 		probe_type = rom_probe_types;
 		if (physmap_data->probe_type == NULL) {
 			for (; info->mtd[i] == NULL && *probe_type != NULL; probe_type++)
include/linux/mtd/physmap.h +1 -0
index aa6a263..719889d 100644
@@ -31,6 +31,7 @@ struct physmap_flash_data {
 	char                    *probe_type;
 	struct mtd_partition	*parts;
 	const char * const	*part_probe_types;
+	int			no_unaligned_access;
 };
 
 #endif /* __LINUX_MTD_PHYSMAP__ */
Signature · 1 line
2.0.0
#3 lore raw

PATCH 3/3 MIPS: BCM63XX: disable unaligned accesses for SMP

In reply to Jonas Gorski

Unaligned accesses to flash can cause random wrong reads when having both cores enabled. This is easily noticable with a jffs2 filesystem, as jffs2 reads its filenames into an unaligned bytebuffer. This can cause random boot failures when critical files have their read filenames corrupted.

---
 arch/mips/bcm63xx/dev-flash.c | 1 +
 1 file changed, 1 insertion(+)
arch/mips/bcm63xx/dev-flash.c +1 -0
index 172dd83..f6a7116 100644
@@ -36,6 +36,7 @@ static struct physmap_flash_data flash_data = {
 	.width			= 2,
 	.parts			= mtd_partitions,
 	.part_probe_types	= bcm63xx_part_types,
+	.no_unaligned_access	= IS_ENABLED(CONFIG_SMP),
 };
 
 static struct resource mtd_resources[] = {
Signature · 1 line
2.0.0
#4 lore raw
In reply to Jonas Gorski

"linux-mtd" <[email protected]> wrote on 2014/07/21 22:47:25:

Quoted 7 lines

Commit d0788ce4924758249c9552c91cc33024d3434419 ("mtd: physmap_of: Add "no-unaligned-direct-access" DT property") intended to prevent unaligned accesses to the mapped flash by setting map.virt to NO_XIP.

But simple mapping's read_from/write_to still just call memcpy, which will do unaligned reads/writes at least on mips.

They don't just call memcpy, it is memcpy_fromio and I think your(MIPS) memcpy_fromio should be fixed to copy with unaligned accesses instead of here.

Quoted 7 lines

This causes problems on e.g. bcm63xx with smp, as both cores share a unaligned load/store unit, and unaligned reads sometimes read garbage.

This patchset aims at preventing unaligned accesses for good by making read_from/write_to check for NO_XIP and do a bytewise copy if one of to or from is unaligned.

#5 lore raw
Quoted 3 lines

They don't just call memcpy, it is memcpy_fromio and I think your(MIPS) memcpy_fromio should be fixed to copy with unaligned accesses instead of

s/copy/cope/