Back to home page

LXR

 
 

    


0001 /*
0002  * Copyright (C) 2010, 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
0003  *
0004  * membarrier system call
0005  *
0006  * This program is free software; you can redistribute it and/or modify
0007  * it under the terms of the GNU General Public License as published by
0008  * the Free Software Foundation; either version 2 of the License, or
0009  * (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  */
0016 
0017 #include <linux/syscalls.h>
0018 #include <linux/membarrier.h>
0019 
0020 /*
0021  * Bitmask made from a "or" of all commands within enum membarrier_cmd,
0022  * except MEMBARRIER_CMD_QUERY.
0023  */
0024 #define MEMBARRIER_CMD_BITMASK  (MEMBARRIER_CMD_SHARED)
0025 
0026 /**
0027  * sys_membarrier - issue memory barriers on a set of threads
0028  * @cmd:   Takes command values defined in enum membarrier_cmd.
0029  * @flags: Currently needs to be 0. For future extensions.
0030  *
0031  * If this system call is not implemented, -ENOSYS is returned. If the
0032  * command specified does not exist, or if the command argument is invalid,
0033  * this system call returns -EINVAL. For a given command, with flags argument
0034  * set to 0, this system call is guaranteed to always return the same value
0035  * until reboot.
0036  *
0037  * All memory accesses performed in program order from each targeted thread
0038  * is guaranteed to be ordered with respect to sys_membarrier(). If we use
0039  * the semantic "barrier()" to represent a compiler barrier forcing memory
0040  * accesses to be performed in program order across the barrier, and
0041  * smp_mb() to represent explicit memory barriers forcing full memory
0042  * ordering across the barrier, we have the following ordering table for
0043  * each pair of barrier(), sys_membarrier() and smp_mb():
0044  *
0045  * The pair ordering is detailed as (O: ordered, X: not ordered):
0046  *
0047  *                        barrier()   smp_mb() sys_membarrier()
0048  *        barrier()          X           X            O
0049  *        smp_mb()           X           O            O
0050  *        sys_membarrier()   O           O            O
0051  */
0052 SYSCALL_DEFINE2(membarrier, int, cmd, int, flags)
0053 {
0054     if (unlikely(flags))
0055         return -EINVAL;
0056     switch (cmd) {
0057     case MEMBARRIER_CMD_QUERY:
0058         return MEMBARRIER_CMD_BITMASK;
0059     case MEMBARRIER_CMD_SHARED:
0060         if (num_online_cpus() > 1)
0061             synchronize_sched();
0062         return 0;
0063     default:
0064         return -EINVAL;
0065     }
0066 }