Search code examples
clinux-kernelmemory-barriersmemory-modelstdatomic

Pairing acquire / release operations between user and kernel space


I am trying to ensure proper synchronization over a piece of memory shared between a user thread and another thread running in kernel mode on Linux.

Does it make sense to pair a C11's atomic_store_explicit(memory_order_release) from user space with an smp_load_acquire() from within the kernel and respectively an atomic_load_explicit(memory_order_acquire) with an smp_store_release()?


Solution

  • Yes, it definitely has a sense to pair C11 atomic memory_order_release and memory_order_acquire in user space with corresponded smp_load_acquire and smp_store_release in the kernel space when access the memory shared between kernel and user (e.g. via mmap()).

    Acquire/release semantic provides similar guarantees both in C11 and Linux kernel and it would be very strange to find their implementation in machine instructions incompatible.

    While C11 allows atomic instructions for "non-atomic" (too large) types, which could be implemented via locks, the Linux kernel bans usage of smp_load_acquire and smp_load_release for "non-atomic" types. So there is no risk to have C11 atomics implemented via user-space locks, which, of course, doesn't affect on the kernel.