4
votes

I have a hardware buffer that I have allocated in the kernel. This buffer receives DMA data from the PCIe bus. From Userspace I then map the hardware buffer into my virtual address space using mmap /dev/mem. My question is as follows:

1) How does mmap handling caching?

a) From reading about mmap I understand that when writing to the mapped region from userspace I may need to call msync to flush the cached version of memory into the 'file'. Since /dev/mem is implemented differently than an mmaped file, do I still need to msync my writes?

b) Since the memory I am mapping is volatile memory (DMA hardware writes to it), how do I maintain sync of my reads from this memory? If there are changes from the hardware in the physical memory, then I have written changes to my cache from userspace, then I msync my changes to flush my writes, are the physical memory changes lost due to me over writing with the flush?

c) When mmaping /dev/mem should I declare the returned pointer to virtual memory as volatile

I see a lot of documentation on mmap caching as it pertains to mmaping filesystem files (not volatile memory) but when it comes to mmaping /dev/mem I can't seem to get the same information (it could all be the same I guess).

1

1 Answers

0
votes

Open /dev/mem with O_SYNC for uncached access. I am having difficulty finding a authoritative source for that.