1 .\" Copyright 2015 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 2015-12-28 "Linux" "Linux Programmer's Manual"
27 membarrier \- issue memory barriers on a set of threads
29 .B #include <linux/membarrier.h>
31 .BI "int membarrier(int " cmd ", int " flags ");
35 system call helps reducing the overhead of the memory barrier
36 instructions required to order memory accesses on multi-core systems.
37 However, this system call is heavier than a memory barrier, so using it
40 as simple as replacing memory barriers with this
41 system call, but requires understanding of the details below.
43 Use of memory barriers needs to be done taking into account that a
44 memory barrier always needs to be either matched with its memory barrier
45 counterparts, or that the architecture's memory model doesn't require the
48 There are cases where one side of the matching barriers (which we will
49 refer to as "fast side") is executed much more often than the other
50 (which we will refer to as "slow side").
51 This is a prime target for the use of
53 The key idea is to replace, for these matching
54 barriers, the fast-side memory barriers by simple compiler barriers,
57 asm volatile ("" : : : "memory")
59 and replace the slow-side memory barriers by calls to
62 This will add overhead to the slow side, and remove overhead from the
63 fast side, thus resulting in an overall performance increase as long as
64 the slow side is infrequent enough that the overhead of the
66 calls does not outweigh the performance gain on the fast side.
70 argument is one of the following:
72 .B MEMBARRIER_CMD_QUERY
73 Query the set of supported commands.
74 The return value of the call is a bit mask of supported
76 .BR MEMBARRIER_CMD_QUERY ,
77 which has the value 0,
78 is not itself included in this bit mask.
79 This command is always supported (on kernels where
83 .B MEMBARRIER_CMD_SHARED
84 Ensure that all threads from all processes on the system pass through a
85 state where all memory accesses to user-space addresses match program
86 order between entry to and return from the
89 All threads on the system are targeted by this command.
93 argument is currently unused and must be specified as 0.
95 All memory accesses performed in program order from each targeted thread
96 are guaranteed to be ordered with respect to
99 If we use the semantic
101 to represent a compiler barrier forcing memory
102 accesses to be performed in program order across the barrier, and
104 to represent explicit memory barriers forcing full memory
105 ordering across the barrier, we have the following ordering table for
111 The pair ordering is detailed as (O: ordered, X: not ordered):
113 barrier() smp_mb() membarrier()
119 .B MEMBARRIER_CMD_QUERY
120 operation returns a bit mask of supported commands and the
121 .B MEMBARRIER_CMD_SHARED
122 operation returns zero.
123 On error, \-1 is returned,
126 is set appropriately.
128 For a given command, with
130 set to 0, this system call is
131 guaranteed to always return the same value until reboot.
132 Further calls with the same arguments will lead to the same result.
135 set to 0, error handling is required only for the first call to
148 system call is not implemented by this kernel.
150 .BR ENOSYS " (since Linux 4.11)"
151 .\" 907565337ebf998a68cb5c5b2174ce5e5da065eb
154 system call is disabled because the
156 CPU parameter has been set.
160 system call was added in Linux 4.3.
166 A memory barrier instruction is part of the instruction set of
167 architectures with weakly-ordered memory models.
169 accesses prior to the barrier and after the barrier with respect to
170 matching barriers on other cores.
171 For instance, a load fence can order
172 loads prior to and following that fence with respect to stores ordered
175 Program order is the order in which instructions are ordered in the
176 program assembly code.
180 can be useful include implementations
181 of Read-Copy-Update libraries and garbage collectors.
183 Assuming a multithreaded application where "fast_path()" is executed
184 very frequently, and where "slow_path()" is executed infrequently, the
185 following code (x86) can be transformed using
192 static volatile int a, b;
200 asm volatile ("mfence" : : : "memory");
203 /* read_b == 1 implies read_a == 1. */
205 if (read_b == 1 && read_a == 0)
213 asm volatile ("mfence" : : : "memory");
218 main(int argc, char **argv)
221 * Real applications would call fast_path() and slow_path()
222 * from different threads. Call those from main() to keep
223 * this example short.
234 The code above transformed to use
244 #include <sys/syscall.h>
245 #include <linux/membarrier.h>
247 static volatile int a, b;
250 membarrier(int cmd, int flags)
252 return syscall(__NR_membarrier, cmd, flags);
256 init_membarrier(void)
260 /* Check that membarrier() is supported. */
262 ret = membarrier(MEMBARRIER_CMD_QUERY, 0);
264 perror("membarrier");
268 if (!(ret & MEMBARRIER_CMD_SHARED)) {
270 "membarrier does not support MEMBARRIER_CMD_SHARED\\n");
283 asm volatile ("" : : : "memory");
286 /* read_b == 1 implies read_a == 1. */
288 if (read_b == 1 && read_a == 0)
296 membarrier(MEMBARRIER_CMD_SHARED, 0);
301 main(int argc, char **argv)
303 if (init_membarrier())
307 * Real applications would call fast_path() and slow_path()
308 * from different threads. Call those from main() to keep
309 * this example short.