1 .\" Copyright 2015-2017 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH MEMBARRIER 2 2021-03-22 "Linux" "Linux Programmer's Manual"
27 membarrier \- issue memory barriers on a set of threads
31 .BR "#include <linux/membarrier.h>" \
32 " /* Definition of " MEMBARRIER_* " constants */"
33 .BR "#include <sys/syscall.h>" " /* Definition of " SYS_* " constants */"
34 .B #include <unistd.h>
36 .BI "int syscall(SYS_membarrier, int " cmd ", unsigned int " flags \
41 glibc provides no wrapper for
43 necessitating the use of
48 system call helps reducing the overhead of the memory barrier
49 instructions required to order memory accesses on multi-core systems.
50 However, this system call is heavier than a memory barrier, so using it
53 as simple as replacing memory barriers with this
54 system call, but requires understanding of the details below.
56 Use of memory barriers needs to be done taking into account that a
57 memory barrier always needs to be either matched with its memory barrier
58 counterparts, or that the architecture's memory model doesn't require the
61 There are cases where one side of the matching barriers (which we will
62 refer to as "fast side") is executed much more often than the other
63 (which we will refer to as "slow side").
64 This is a prime target for the use of
66 The key idea is to replace, for these matching
67 barriers, the fast-side memory barriers by simple compiler barriers,
72 asm volatile ("" : : : "memory")
76 and replace the slow-side memory barriers by calls to
79 This will add overhead to the slow side, and remove overhead from the
80 fast side, thus resulting in an overall performance increase as long as
81 the slow side is infrequent enough that the overhead of the
83 calls does not outweigh the performance gain on the fast side.
87 argument is one of the following:
89 .BR MEMBARRIER_CMD_QUERY " (since Linux 4.3)"
90 Query the set of supported commands.
91 The return value of the call is a bit mask of supported
93 .BR MEMBARRIER_CMD_QUERY ,
94 which has the value 0,
95 is not itself included in this bit mask.
96 This command is always supported (on kernels where
100 .BR MEMBARRIER_CMD_GLOBAL " (since Linux 4.16)"
101 Ensure that all threads from all processes on the system pass through a
102 state where all memory accesses to user-space addresses match program
103 order between entry to and return from the
106 All threads on the system are targeted by this command.
108 .BR MEMBARRIER_CMD_GLOBAL_EXPEDITED " (since Linux 4.16)"
109 Execute a memory barrier on all running threads of all processes that
110 previously registered with
111 .BR MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED .
113 Upon return from the system call, the calling thread has a guarantee that all
114 running threads have passed through a state where all memory accesses to
115 user-space addresses match program order between entry to and return
116 from the system call (non-running threads are de facto in such a state).
117 This guarantee is provided only for the threads of processes that
118 previously registered with
119 .BR MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED .
121 Given that registration is about the intent to receive the barriers, it
123 .BR MEMBARRIER_CMD_GLOBAL_EXPEDITED
124 from a process that has not employed
125 .BR MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED .
127 The "expedited" commands complete faster than the non-expedited ones;
128 they never block, but have the downside of causing extra overhead.
130 .BR MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED " (since Linux 4.16)"
131 Register the process's intent to receive
132 .BR MEMBARRIER_CMD_GLOBAL_EXPEDITED
135 .BR MEMBARRIER_CMD_PRIVATE_EXPEDITED " (since Linux 4.14)"
136 Execute a memory barrier on each running thread belonging to the same
137 process as the calling thread.
139 Upon return from the system call, the calling
140 thread has a guarantee that all its running thread siblings have passed
141 through a state where all memory accesses to user-space addresses match
142 program order between entry to and return from the system call
143 (non-running threads are de facto in such a state).
144 This guarantee is provided only for threads in
145 the same process as the calling thread.
147 The "expedited" commands complete faster than the non-expedited ones;
148 they never block, but have the downside of causing extra overhead.
150 A process must register its intent to use the private
151 expedited command prior to using it.
153 .BR MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED " (since Linux 4.14)"
154 Register the process's intent to use
155 .BR MEMBARRIER_CMD_PRIVATE_EXPEDITED .
157 .BR MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE " (since Linux 4.16)"
158 In addition to providing the memory ordering guarantees described in
159 .BR MEMBARRIER_CMD_PRIVATE_EXPEDITED ,
160 upon return from system call the calling thread has a guarantee that all its
161 running thread siblings have executed a core serializing instruction.
162 This guarantee is provided only for threads in
163 the same process as the calling thread.
165 The "expedited" commands complete faster than the non-expedited ones,
166 they never block, but have the downside of causing extra overhead.
168 A process must register its intent to use the private expedited sync
169 core command prior to using it.
171 .BR MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE " (since Linux 4.16)"
172 Register the process's intent to use
173 .BR MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE .
175 .BR MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ " (since Linux 5.10)"
176 Ensure the caller thread, upon return from system call, that all its
177 running thread siblings have any currently running rseq critical sections
183 .BR MEMBARRIER_CMD_FLAG_CPU ,
184 then this operation is performed only on CPU indicated by
186 This guarantee is provided only for threads in
187 the same process as the calling thread.
189 RSEQ membarrier is only available in the "private expedited" form.
191 A process must register its intent to use the private expedited rseq
192 command prior to using it.
194 .BR MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ " (since Linux 5.10)"
195 Register the process's intent to use
196 .BR MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ .
198 .BR MEMBARRIER_CMD_SHARED " (since Linux 4.3)"
200 .BR MEMBARRIER_CMD_GLOBAL
201 that exists for header backward compatibility.
205 argument must be specified as 0 unless the command is
206 .BR MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ ,
210 .BR MEMBARRIER_CMD_FLAG_CPU .
214 argument is ignored unless
217 .BR MEMBARRIER_CMD_FLAG_CPU ,
218 in which case it must specify the CPU targeted by this membarrier
221 All memory accesses performed in program order from each targeted thread
222 are guaranteed to be ordered with respect to
225 If we use the semantic
227 to represent a compiler barrier forcing memory
228 accesses to be performed in program order across the barrier, and
230 to represent explicit memory barriers forcing full memory
231 ordering across the barrier, we have the following ordering table for
237 The pair ordering is detailed as (O: ordered, X: not ordered):
239 barrier() smp_mb() membarrier()
245 .B MEMBARRIER_CMD_QUERY
246 operation returns a bit mask of supported commands, and the
247 .BR MEMBARRIER_CMD_GLOBAL ,
248 .BR MEMBARRIER_CMD_GLOBAL_EXPEDITED ,
249 .BR MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED ,
250 .BR MEMBARRIER_CMD_PRIVATE_EXPEDITED ,
251 .BR MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED ,
252 .BR MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE ,
254 .B MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE
255 operations return zero.
256 On error, \-1 is returned,
259 is set to indicate the error.
261 For a given command, with
263 set to 0, this system call is
264 guaranteed to always return the same value until reboot.
265 Further calls with the same arguments will lead to the same result.
268 set to 0, error handling is required only for the first call to
277 .BR MEMBARRIER_CMD_GLOBAL
278 command is disabled because the
280 CPU parameter has been set, or the
281 .BR MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE
283 .BR MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE
284 commands are not implemented by the architecture.
289 system call is not implemented by this kernel.
292 The current process was not registered prior to using private expedited
297 system call was added in Linux 4.3.
299 Before Linux 5.10, the prototype for
305 .BI "int membarrier(int " cmd ", int " flags );
312 .\" FIXME See if the following syscalls make it into Linux 4.15 or later
316 A memory barrier instruction is part of the instruction set of
317 architectures with weakly ordered memory models.
319 accesses prior to the barrier and after the barrier with respect to
320 matching barriers on other cores.
321 For instance, a load fence can order
322 loads prior to and following that fence with respect to stores ordered
325 Program order is the order in which instructions are ordered in the
326 program assembly code.
330 can be useful include implementations
331 of Read-Copy-Update libraries and garbage collectors.
333 Assuming a multithreaded application where "fast_path()" is executed
334 very frequently, and where "slow_path()" is executed infrequently, the
335 following code (x86) can be transformed using
342 static volatile int a, b;
345 fast_path(int *read_b)
348 asm volatile ("mfence" : : : "memory");
353 slow_path(int *read_a)
356 asm volatile ("mfence" : : : "memory");
361 main(int argc, char *argv[])
366 * Real applications would call fast_path() and slow_path()
367 * from different threads. Call those from main() to keep
368 * this example short.
375 * read_b == 0 implies read_a == 1 and
376 * read_a == 0 implies read_b == 1.
379 if (read_b == 0 && read_a == 0)
387 The code above transformed to use
397 #include <sys/syscall.h>
398 #include <linux/membarrier.h>
400 static volatile int a, b;
403 membarrier(int cmd, unsigned int flags, int cpu_id)
405 return syscall(__NR_membarrier, cmd, flags, cpu_id);
409 init_membarrier(void)
413 /* Check that membarrier() is supported. */
415 ret = membarrier(MEMBARRIER_CMD_QUERY, 0, 0);
417 perror("membarrier");
421 if (!(ret & MEMBARRIER_CMD_GLOBAL)) {
423 "membarrier does not support MEMBARRIER_CMD_GLOBAL\en");
431 fast_path(int *read_b)
434 asm volatile ("" : : : "memory");
439 slow_path(int *read_a)
442 membarrier(MEMBARRIER_CMD_GLOBAL, 0, 0);
447 main(int argc, char *argv[])
451 if (init_membarrier())
455 * Real applications would call fast_path() and slow_path()
456 * from different threads. Call those from main() to keep
457 * this example short.
464 * read_b == 0 implies read_a == 1 and
465 * read_a == 0 implies read_b == 1.
468 if (read_b == 0 && read_a == 0)