0

People typically mention mfence in the context of lock-free programming with atomics and without system synchronization primitives, which performs request to the kernel. So does it mean that mfence instruction is not needed when I create code for user space that uses kernel synchronization primitives. If so, why?

Also, do int instructions perform implicit mfence?

I will be extremely happy to learn about it, because documentation that I have found in the web regarding int and sysenter specify nothing regarding mfence.

Peter Cordes
  • 286,368
  • 41
  • 520
  • 731
Konstantin Burlachenko
  • 4,925
  • 2
  • 40
  • 38
  • C++ guys are near to metal. So it’s a reason why the tag is here really. But maybe you are right dear @user17732522 actually this question is about x64 assembly. – Konstantin Burlachenko Jan 28 '22 at 22:36
  • Ok, kindly thanks @user17732522 – Konstantin Burlachenko Jan 28 '22 at 22:43
  • 1
    Are you sure you mean `sysenter`? It's only used from 32-bit user-space. The 64-bit system call instruction is `syscall`. – Peter Cordes Jan 29 '22 at 04:34
  • 1
    Anyway, [neither syscall nor sysenter are serializing](https://stackoverflow.com/questions/50323347/how-many-memory-barriers-instructions-does-an-x86-cpu-have), so any memory barriering depends on how the kernel implements whatever system call you invoked. `int` is also not fully serializing, although `iret` is. It is guaranteed like `lfence` to not execute later instructions until all earlier ones are executed: https://www.felixcloutier.com/x86/intn:into:int3:int1 - but not drain the store buffer. – Peter Cordes Jan 29 '22 at 04:35
  • 1
    I would expect that whatever synchronization system call you're using will execute any necessary barriers within its actual kernel code, so that you wouldn't rely on int/syscall/whatever to hand it. If it's some kind of mutex system call, for instance, then the lock had better have an acquire barrier, and the unlock a release barrier (on architectures where that isn't already automatic). If not, hopefully it's very clearly documented that you need your own barriers. One major reason for using such primitives in the first place would be to avoid thinking about memory order at all. – Nate Eldredge Jan 29 '22 at 06:18

0 Answers0