cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
ngerber
Visitor
Visitor
2,804 Views
Registered: ‎04-28-2016

Cache coherency when using mmap with different drivers on Linux

Hi,

 

I have a bit more general question concerning how to maintain cache coherence in a Linux userspace application when using mmap'ed memory regions and different Linux device drivers. Can you confirm the following statements? (Please read "volatile pointer" as "pointer pointing to volatile data" and all the mapping is done with the MAP_SHARED flag.)

  • UIO device driver: As the driver maps the memory without processor cache, there is no need to cast the pointer returned by mmap to a volatile pointer. Is it be good practice to cast it nevertheless? Would there be any differences between using a volatile pointer and just a pointer?
  • /dev/mem:
    - Read: As the caches are enabled, any read must occur on a volatile pointer, otherwise outdated data might be read.
    - Write: There are two options; either the data is written to volatile pointers to force the write through or the data is written using a normal pointer but after finishing writing, msync with the MS_SYNC flag has to be called to force the device to flush the cached data to the memory. Is one of the two ways better than the other?
  • PCI device: Same behaviour as /dev/mem

Best regards,

 

Nicola

0 Kudos
1 Reply
divcesar
Visitor
Visitor
2,704 Views
Registered: ‎07-11-2017

Hi,

 

what I can tell is that "volatile variables" aren't directly related to caching. It is related to compiler optimizations. More specifically, when you have a volatile variable or pointer, you are telling the compiler to not hold that value in a register because there might be other actors in the system trying to access (i.e., change) that variable concurrently. Keeping the variable in a register would prevent actors from seeing changes of each other.