classification
Title: MMAP duplicate reads and writes on ARM64 platform
Type: Stage:
Components: Library (Lib) Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: psswirhun
Priority: normal Keywords:

Created on 2021-02-04 22:45 by psswirhun, last changed 2021-02-04 23:02 by psswirhun.

Messages (2)
msg386501 - (view) Author: Paul Swirhun (psswirhun) Date: 2021-02-04 22:45
mmap (example, as used in python-periphery) duplicates writes, compared to devmem and a c-periphery on an ARM64 platform (Zynq/Petalinux/Yocto). There are various other reports of duplicated writes using mmap suggesting it is a bug. Example:

fd = os.open("/dev/mem", os.O_RDWR | os.O_SYNC)
mapping = mmap.mmap(fd, 4096, flags=mmap.MAP_SHARED, prot=(mmap.PROT_READ | mmap.PROT_WRITE), offset=0x80000000)
mapping.write(bytearray([0x0,]* 4))  # results in 2 writes, should be 1
mapping.write(bytearray([0x0,]* 8))  # results in 4 writes, should be 1-2 depending on bus width (32b vs 64b)
# OR:
mapping[0:4] = struct.pack("=L", 0x0)  # results in 2 writes, should be 1
msg386502 - (view) Author: Paul Swirhun (psswirhun) Date: 2021-02-04 23:02
A workaround is using memoryview (as reported here: https://stackoverflow.com/questions/53492716/python-writing-to-memory-in-a-single-operation). This results in only 1 transaction on the bus for some reason.

import os, mmap
fd = os.open("/dev/mem", os.O_RDWR | os.O_SYNC)
mapping = mmap.mmap(fd, 4096, flags=mmap.MAP_SHARED, prot=(mmap.PROT_READ | mmap.PROT_WRITE), offset=0x80000000)
mv = memoryview(mapping)
mv_as_int32 = mv.cast('I')  # Note "I" works as intended, "L" still results in duplicate writes; "LL" is not an option
mv_as_int32[0x0 // 4] = 0xDEADBEEF  # this works; results in 1 write issued on the AXI bus
History
Date User Action Args
2021-02-04 23:02:15psswirhunsetmessages: + msg386502
2021-02-04 22:45:15psswirhuncreate