[PATCH v3] mtd: phram: Use I/O memory accessors when in uncached mappings
Edward Adam Davis
eadavis at sina.com
Mon Nov 23 00:57:58 PST 2026
When registering a phram device via module parameter or sysfs invokes
register_device() with pdev = NULL, which causes new->cached to be false.
As a result, phram_map() maps the physical memory region using ioremap()
(mapped as PROT_DEVICE_nGnRE on arm64) and forces the pointer into mtd->
priv, yet phram_read(), phram_write(), and phram_erase() access it using
standard memcpy() and memset() instead of I/O memory accessors (like
memcpy_fromio()). When scanning partition headers (e.g., ftl_add_mtd()
reading a 68-byte header via mtd_read()), arm64's memcpy implementation
issues pair-load (ldp) instructions at unaligned offsets, which triggers
an alignment fault (FSC = 0x21) on Device memory and crashes the kernel.
phram_point() exposes a uncached pointer that obtained via ioremap() in
mtd->priv, which leads callers to perform standard memcpy() on it.
In phram_read/write/erase(), standard memory accessors are used when in
a cached; otherwise, I/O memory accessors are used. Only register _point
and __unpoint when cached mapping.
Fixes: f3d45ac21e46 ("mtd: phram: Module parameters add writable permissions")
Fixes: 285133040e6c ("arm64: Import latest memcpy()/memmove() implementation")
Reported-by: syzbot+ee09ee2112d03c35f1f7 at syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=ee09ee2112d03c35f1f7
Signed-off-by: Edward Adam Davis <eadavis at sina.com>
---
v1 -> v2: use phram_mtd_list cached, add void __iomem* and write use toio
v2 -> v3: update subject, conditionally register _point and _unpoint
drivers/mtd/devices/phram.c | 28 +++++++++++++++++++++++-----
1 file changed, 23 insertions(+), 5 deletions(-)
diff --git a/drivers/mtd/devices/phram.c b/drivers/mtd/devices/phram.c
index b42cadcd76b1..d7a074a69e62 100644
--- a/drivers/mtd/devices/phram.c
+++ b/drivers/mtd/devices/phram.c
@@ -43,8 +43,14 @@ static LIST_HEAD(phram_list);
static int phram_erase(struct mtd_info *mtd, struct erase_info *instr)
{
u_char *start = mtd->priv;
+ struct phram_mtd_list *phram =
+ container_of(mtd, struct phram_mtd_list, mtd);
- memset(start + instr->addr, 0xff, instr->len);
+ if (phram->cached)
+ memset(start + instr->addr, 0xff, instr->len);
+ else
+ memset_io((void __iomem *)start + instr->addr, 0xff,
+ instr->len);
return 0;
}
@@ -66,8 +72,13 @@ static int phram_read(struct mtd_info *mtd, loff_t from, size_t len,
size_t *retlen, u_char *buf)
{
u_char *start = mtd->priv;
+ struct phram_mtd_list *phram =
+ container_of(mtd, struct phram_mtd_list, mtd);
- memcpy(buf, start + from, len);
+ if (phram->cached)
+ memcpy(buf, start + from, len);
+ else
+ memcpy_fromio(buf, (void __iomem *)start + from, len);
*retlen = len;
return 0;
}
@@ -76,8 +87,13 @@ static int phram_write(struct mtd_info *mtd, loff_t to, size_t len,
size_t *retlen, const u_char *buf)
{
u_char *start = mtd->priv;
+ struct phram_mtd_list *phram =
+ container_of(mtd, struct phram_mtd_list, mtd);
- memcpy(start + to, buf, len);
+ if (phram->cached)
+ memcpy(start + to, buf, len);
+ else
+ memcpy_toio((void __iomem *)start + to, buf, len);
*retlen = len;
return 0;
}
@@ -147,8 +163,10 @@ static int register_device(struct platform_device *pdev, const char *name,
new->mtd.size = len;
new->mtd.flags = MTD_CAP_RAM;
new->mtd._erase = phram_erase;
- new->mtd._point = phram_point;
- new->mtd._unpoint = phram_unpoint;
+ if (new->cached) {
+ new->mtd._point = phram_point;
+ new->mtd._unpoint = phram_unpoint;
+ }
new->mtd._read = phram_read;
new->mtd._write = phram_write;
new->mtd.owner = THIS_MODULE;
--
2.43.0
More information about the linux-mtd
mailing list