Blame man2/membarrier.2

Packit 7cfc04
.\" Copyright 2015-2017 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.TH MEMBARRIER 2 2017-11-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
membarrier \- issue memory barriers on a set of threads
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.B #include <linux/membarrier.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int membarrier(int " cmd ", int " flags ");
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.BR membarrier ()
Packit 7cfc04
system call helps reducing the overhead of the memory barrier
Packit 7cfc04
instructions required to order memory accesses on multi-core systems.
Packit 7cfc04
However, this system call is heavier than a memory barrier, so using it
Packit 7cfc04
effectively is
Packit 7cfc04
.I not
Packit 7cfc04
as simple as replacing memory barriers with this
Packit 7cfc04
system call, but requires understanding of the details below.
Packit 7cfc04
.PP
Packit 7cfc04
Use of memory barriers needs to be done taking into account that a
Packit 7cfc04
memory barrier always needs to be either matched with its memory barrier
Packit 7cfc04
counterparts, or that the architecture's memory model doesn't require the
Packit 7cfc04
matching barriers.
Packit 7cfc04
.PP
Packit 7cfc04
There are cases where one side of the matching barriers (which we will
Packit 7cfc04
refer to as "fast side") is executed much more often than the other
Packit 7cfc04
(which we will refer to as "slow side").
Packit 7cfc04
This is a prime target for the use of
Packit 7cfc04
.BR membarrier ().
Packit 7cfc04
The key idea is to replace, for these matching
Packit 7cfc04
barriers, the fast-side memory barriers by simple compiler barriers,
Packit 7cfc04
for example:
Packit 7cfc04
.PP
Packit 7cfc04
    asm volatile ("" : : : "memory")
Packit 7cfc04
.PP
Packit 7cfc04
and replace the slow-side memory barriers by calls to
Packit 7cfc04
.BR membarrier ().
Packit 7cfc04
.PP
Packit 7cfc04
This will add overhead to the slow side, and remove overhead from the
Packit 7cfc04
fast side, thus resulting in an overall performance increase as long as
Packit 7cfc04
the slow side is infrequent enough that the overhead of the
Packit 7cfc04
.BR membarrier ()
Packit 7cfc04
calls does not outweigh the performance gain on the fast side.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I cmd
Packit 7cfc04
argument is one of the following:
Packit 7cfc04
.TP
Packit 7cfc04
.B MEMBARRIER_CMD_QUERY
Packit 7cfc04
Query the set of supported commands.
Packit 7cfc04
The return value of the call is a bit mask of supported
Packit 7cfc04
commands.
Packit 7cfc04
.BR MEMBARRIER_CMD_QUERY ,
Packit 7cfc04
which has the value 0,
Packit 7cfc04
is not itself included in this bit mask.
Packit 7cfc04
This command is always supported (on kernels where
Packit 7cfc04
.BR membarrier ()
Packit 7cfc04
is provided).
Packit 7cfc04
.TP
Packit 7cfc04
.B MEMBARRIER_CMD_SHARED
Packit 7cfc04
Ensure that all threads from all processes on the system pass through a
Packit 7cfc04
state where all memory accesses to user-space addresses match program
Packit 7cfc04
order between entry to and return from the
Packit 7cfc04
.BR membarrier ()
Packit 7cfc04
system call.
Packit 7cfc04
All threads on the system are targeted by this command.
Packit 7cfc04
.TP
Packit 7cfc04
.BR MEMBARRIER_CMD_PRIVATE_EXPEDITED " (since Linux 4.14)"
Packit 7cfc04
Execute a memory barrier on each running thread belonging to the same
Packit 7cfc04
process as the current thread.
Packit 7cfc04
Upon return from system call, the calling
Packit 7cfc04
thread is assured that all its running threads siblings have passed
Packit 7cfc04
through a state where all memory accesses to user-space addresses match
Packit 7cfc04
program order between entry to and return from the system call
Packit 7cfc04
(non-running threads are de facto in such a state).
Packit 7cfc04
This covers only threads from the same process as the calling thread.
Packit 7cfc04
.IP
Packit 7cfc04
The "expedited" commands complete faster than the non-expedited ones;
Packit 7cfc04
they never block, but have the downside of causing extra overhead.
Packit 7cfc04
A process needs to register its intent to use the private
Packit 7cfc04
expedited command prior to using it.
Packit 7cfc04
.TP
Packit 7cfc04
.BR MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED " (since Linux 4.14)"
Packit 7cfc04
Register the process's intent to use
Packit 7cfc04
.BR MEMBARRIER_CMD_PRIVATE_EXPEDITED .
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I flags
Packit 7cfc04
argument is currently unused and must be specified as 0.
Packit 7cfc04
.PP
Packit 7cfc04
All memory accesses performed in program order from each targeted thread
Packit 7cfc04
are guaranteed to be ordered with respect to
Packit 7cfc04
.BR membarrier ().
Packit 7cfc04
.PP
Packit 7cfc04
If we use the semantic
Packit 7cfc04
.I barrier()
Packit 7cfc04
to represent a compiler barrier forcing memory
Packit 7cfc04
accesses to be performed in program order across the barrier, and
Packit 7cfc04
.I smp_mb()
Packit 7cfc04
to represent explicit memory barriers forcing full memory
Packit 7cfc04
ordering across the barrier, we have the following ordering table for
Packit 7cfc04
each pairing of
Packit 7cfc04
.IR barrier() ,
Packit 7cfc04
.BR membarrier ()
Packit 7cfc04
and
Packit 7cfc04
.IR smp_mb() .
Packit 7cfc04
The pair ordering is detailed as (O: ordered, X: not ordered):
Packit 7cfc04
.PP
Packit 7cfc04
                       barrier()  smp_mb()  membarrier()
Packit 7cfc04
       barrier()          X          X          O
Packit 7cfc04
       smp_mb()           X          O          O
Packit 7cfc04
       membarrier()       O          O          O
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success, the
Packit 7cfc04
.B MEMBARRIER_CMD_QUERY
Packit 7cfc04
operation returns a bit mask of supported commands, and the
Packit 7cfc04
.B MEMBARRIER_CMD_SHARED ,
Packit 7cfc04
.B MEMBARRIER_CMD_PRIVATE_EXPEDITED ,
Packit 7cfc04
and
Packit 7cfc04
.B MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED ,
Packit 7cfc04
operations return zero.
Packit 7cfc04
On error, \-1 is returned,
Packit 7cfc04
and
Packit 7cfc04
.I errno
Packit 7cfc04
is set appropriately.
Packit 7cfc04
.PP
Packit 7cfc04
For a given command, with
Packit 7cfc04
.I flags
Packit 7cfc04
set to 0, this system call is
Packit 7cfc04
guaranteed to always return the same value until reboot.
Packit 7cfc04
Further calls with the same arguments will lead to the same result.
Packit 7cfc04
Therefore, with
Packit 7cfc04
.I flags
Packit 7cfc04
set to 0, error handling is required only for the first call to
Packit 7cfc04
.BR membarrier ().
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.I cmd
Packit 7cfc04
is invalid, or
Packit 7cfc04
.I flags
Packit 7cfc04
is nonzero, or the
Packit 7cfc04
.BR MEMBARRIER_CMD_SHARED
Packit 7cfc04
command is disabled because the
Packit 7cfc04
.I nohz_full
Packit 7cfc04
CPU parameter has been set.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOSYS
Packit 7cfc04
The
Packit 7cfc04
.BR membarrier ()
Packit 7cfc04
system call is not implemented by this kernel.
Packit 7cfc04
.TP
Packit 7cfc04
.B EPERM
Packit 7cfc04
The current process was not registered prior to using private expedited
Packit 7cfc04
commands.
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
The
Packit 7cfc04
.BR membarrier ()
Packit 7cfc04
system call was added in Linux 4.3.
Packit 7cfc04
.\"
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
.BR membarrier ()
Packit 7cfc04
is Linux-specific.
Packit 7cfc04
.\" .SH SEE ALSO
Packit 7cfc04
.\" FIXME See if the following syscalls make it into Linux 4.15 or later
Packit 7cfc04
.\" .BR cpu_opv (2),
Packit 7cfc04
.\" .BR rseq (2)
Packit 7cfc04
.SH NOTES
Packit 7cfc04
A memory barrier instruction is part of the instruction set of
Packit 7cfc04
architectures with weakly-ordered memory models.
Packit 7cfc04
It orders memory
Packit 7cfc04
accesses prior to the barrier and after the barrier with respect to
Packit 7cfc04
matching barriers on other cores.
Packit 7cfc04
For instance, a load fence can order
Packit 7cfc04
loads prior to and following that fence with respect to stores ordered
Packit 7cfc04
by store fences.
Packit 7cfc04
.PP
Packit 7cfc04
Program order is the order in which instructions are ordered in the
Packit 7cfc04
program assembly code.
Packit 7cfc04
.PP
Packit 7cfc04
Examples where
Packit 7cfc04
.BR membarrier ()
Packit 7cfc04
can be useful include implementations
Packit 7cfc04
of Read-Copy-Update libraries and garbage collectors.
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
Assuming a multithreaded application where "fast_path()" is executed
Packit 7cfc04
very frequently, and where "slow_path()" is executed infrequently, the
Packit 7cfc04
following code (x86) can be transformed using
Packit 7cfc04
.BR membarrier ():
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
Packit 7cfc04
static volatile int a, b;
Packit 7cfc04
Packit 7cfc04
static void
Packit 7cfc04
fast_path(int *read_b)
Packit 7cfc04
{
Packit 7cfc04
    a = 1;
Packit 7cfc04
    asm volatile ("mfence" : : : "memory");
Packit 7cfc04
    *read_b = b;
Packit 7cfc04
}
Packit 7cfc04
Packit 7cfc04
static void
Packit 7cfc04
slow_path(int *read_a)
Packit 7cfc04
{
Packit 7cfc04
    b = 1;
Packit 7cfc04
    asm volatile ("mfence" : : : "memory");
Packit 7cfc04
    *read_a = a;
Packit 7cfc04
}
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char **argv)
Packit 7cfc04
{
Packit 7cfc04
    int read_a, read_b;
Packit 7cfc04
Packit 7cfc04
    /*
Packit 7cfc04
     * Real applications would call fast_path() and slow_path()
Packit 7cfc04
     * from different threads. Call those from main() to keep
Packit 7cfc04
     * this example short.
Packit 7cfc04
     */
Packit 7cfc04
Packit 7cfc04
    slow_path(&read_a);
Packit 7cfc04
    fast_path(&read_b);
Packit 7cfc04
Packit 7cfc04
    /*
Packit 7cfc04
     * read_b == 0 implies read_a == 1 and
Packit 7cfc04
     * read_a == 0 implies read_b == 1.
Packit 7cfc04
     */
Packit 7cfc04
Packit 7cfc04
    if (read_b == 0 && read_a == 0)
Packit 7cfc04
        abort();
Packit 7cfc04
Packit 7cfc04
    exit(EXIT_SUCCESS);
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
The code above transformed to use
Packit 7cfc04
.BR membarrier ()
Packit 7cfc04
becomes:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
#define _GNU_SOURCE
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
#include <unistd.h>
Packit 7cfc04
#include <sys/syscall.h>
Packit 7cfc04
#include <linux/membarrier.h>
Packit 7cfc04
Packit 7cfc04
static volatile int a, b;
Packit 7cfc04
Packit 7cfc04
static int
Packit 7cfc04
membarrier(int cmd, int flags)
Packit 7cfc04
{
Packit 7cfc04
    return syscall(__NR_membarrier, cmd, flags);
Packit 7cfc04
}
Packit 7cfc04
Packit 7cfc04
static int
Packit 7cfc04
init_membarrier(void)
Packit 7cfc04
{
Packit 7cfc04
    int ret;
Packit 7cfc04
Packit 7cfc04
    /* Check that membarrier() is supported. */
Packit 7cfc04
Packit 7cfc04
    ret = membarrier(MEMBARRIER_CMD_QUERY, 0);
Packit 7cfc04
    if (ret < 0) {
Packit 7cfc04
        perror("membarrier");
Packit 7cfc04
        return \-1;
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    if (!(ret & MEMBARRIER_CMD_SHARED)) {
Packit 7cfc04
        fprintf(stderr,
Packit 7cfc04
            "membarrier does not support MEMBARRIER_CMD_SHARED\\n");
Packit 7cfc04
        return \-1;
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    return 0;
Packit 7cfc04
}
Packit 7cfc04
Packit 7cfc04
static void
Packit 7cfc04
fast_path(int *read_b)
Packit 7cfc04
{
Packit 7cfc04
    a = 1;
Packit 7cfc04
    asm volatile ("" : : : "memory");
Packit 7cfc04
    *read_b = b;
Packit 7cfc04
}
Packit 7cfc04
Packit 7cfc04
static void
Packit 7cfc04
slow_path(int *read_a)
Packit 7cfc04
{
Packit 7cfc04
    b = 1;
Packit 7cfc04
    membarrier(MEMBARRIER_CMD_SHARED, 0);
Packit 7cfc04
    *read_a = a;
Packit 7cfc04
}
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char **argv)
Packit 7cfc04
{
Packit 7cfc04
    int read_a, read_b;
Packit 7cfc04
Packit 7cfc04
    if (init_membarrier())
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
Packit 7cfc04
    /*
Packit 7cfc04
     * Real applications would call fast_path() and slow_path()
Packit 7cfc04
     * from different threads. Call those from main() to keep
Packit 7cfc04
     * this example short.
Packit 7cfc04
     */
Packit 7cfc04
Packit 7cfc04
    slow_path(&read_a);
Packit 7cfc04
    fast_path(&read_b);
Packit 7cfc04
Packit 7cfc04
    /*
Packit 7cfc04
     * read_b == 0 implies read_a == 1 and
Packit 7cfc04
     * read_a == 0 implies read_b == 1.
Packit 7cfc04
     */
Packit 7cfc04
Packit 7cfc04
    if (read_b == 0 && read_a == 0)
Packit 7cfc04
        abort();
Packit 7cfc04
Packit 7cfc04
    exit(EXIT_SUCCESS);
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.