Search code examples
linuxarmembeddeduart

Is msync always really needed when writing to /dev/mem?


I am using mmap to open /dev/mem for read/write into UART registers. It works well but my question is : After a write, is the msync system call with MS_SYNC flag really needed ?

From my understanding, /dev/mem is a virtual device than provide access to physical memory zones (UART registers in my case ) by translating virtual memory address and so give access to some physical memory from user space.

This is not a common file and i guess that modifications of registers are not buffered/cached. I actually would like to avoid this system call for performance reasons.

Thanks


Solution

  • My understanding is that msync() is needed to update the data in a normal file that is modified through a mapping created with mmap().

    But when you use mmap on /dev/mem you are not mapping a normal file on disk, you are just mapping the desired hardware memory range directly into your process virtual address space, so msync() is off topic, it will do nothing.

    The only thing that lies between your writing into your mmapped virtual space and the hardware device is the CPU cache. To force that you can force a cache flush (__clear_cache() maybe?), but that is usually unnecessary because the kernel identifies the memory mapped device register and disables the cache for that range. In X86 CPUs that is usually done with MTRR, but with ARM I don't know the details...