blob: 678577267a9ac5228ed9327b6d9e283ac37a8530 [file] [log] [blame]
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -04001/*
2 * Copyright (C) 2010-2017 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * membarrier system call
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/syscalls.h>
18#include <linux/membarrier.h>
19#include <linux/tick.h>
20#include <linux/cpumask.h>
Mathieu Desnoyersa961e402017-10-19 13:30:15 -040021#include <linux/atomic.h>
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -040022
23#include "sched.h" /* for cpu_rq(). */
24
25/*
26 * Bitmask made from a "or" of all commands within enum membarrier_cmd,
27 * except MEMBARRIER_CMD_QUERY.
28 */
29#define MEMBARRIER_CMD_BITMASK \
Mathieu Desnoyersa961e402017-10-19 13:30:15 -040030 (MEMBARRIER_CMD_SHARED | MEMBARRIER_CMD_PRIVATE_EXPEDITED \
31 | MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED)
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -040032
33static void ipi_mb(void *info)
34{
35 smp_mb(); /* IPIs should be serializing but paranoid. */
36}
37
Mathieu Desnoyersa961e402017-10-19 13:30:15 -040038static int membarrier_private_expedited(void)
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -040039{
40 int cpu;
41 bool fallback = false;
42 cpumask_var_t tmpmask;
43
Mathieu Desnoyersa961e402017-10-19 13:30:15 -040044 if (!(atomic_read(&current->mm->membarrier_state)
45 & MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY))
46 return -EPERM;
47
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -040048 if (num_online_cpus() == 1)
Mathieu Desnoyersa961e402017-10-19 13:30:15 -040049 return 0;
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -040050
51 /*
52 * Matches memory barriers around rq->curr modification in
53 * scheduler.
54 */
55 smp_mb(); /* system call entry is not a mb. */
56
57 /*
58 * Expedited membarrier commands guarantee that they won't
59 * block, hence the GFP_NOWAIT allocation flag and fallback
60 * implementation.
61 */
62 if (!zalloc_cpumask_var(&tmpmask, GFP_NOWAIT)) {
63 /* Fallback for OOM. */
64 fallback = true;
65 }
66
67 cpus_read_lock();
68 for_each_online_cpu(cpu) {
69 struct task_struct *p;
70
71 /*
72 * Skipping the current CPU is OK even through we can be
73 * migrated at any point. The current CPU, at the point
74 * where we read raw_smp_processor_id(), is ensured to
75 * be in program order with respect to the caller
76 * thread. Therefore, we can skip this CPU from the
77 * iteration.
78 */
79 if (cpu == raw_smp_processor_id())
80 continue;
81 rcu_read_lock();
82 p = task_rcu_dereference(&cpu_rq(cpu)->curr);
83 if (p && p->mm == current->mm) {
84 if (!fallback)
85 __cpumask_set_cpu(cpu, tmpmask);
86 else
87 smp_call_function_single(cpu, ipi_mb, NULL, 1);
88 }
89 rcu_read_unlock();
90 }
91 if (!fallback) {
Mathieu Desnoyers54167602017-12-15 14:23:10 -050092 preempt_disable();
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -040093 smp_call_function_many(tmpmask, ipi_mb, NULL, 1);
Mathieu Desnoyers54167602017-12-15 14:23:10 -050094 preempt_enable();
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -040095 free_cpumask_var(tmpmask);
96 }
97 cpus_read_unlock();
98
99 /*
100 * Memory barrier on the caller thread _after_ we finished
101 * waiting for the last IPI. Matches memory barriers around
102 * rq->curr modification in scheduler.
103 */
104 smp_mb(); /* exit from system call is not a mb */
Mathieu Desnoyersa961e402017-10-19 13:30:15 -0400105 return 0;
106}
107
108static void membarrier_register_private_expedited(void)
109{
110 struct task_struct *p = current;
111 struct mm_struct *mm = p->mm;
112
113 /*
114 * We need to consider threads belonging to different thread
115 * groups, which use the same mm. (CLONE_VM but not
116 * CLONE_THREAD).
117 */
118 if (atomic_read(&mm->membarrier_state)
119 & MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY)
120 return;
Mathieu Desnoyers3ccfebe2018-01-29 15:20:11 -0500121 atomic_or(MEMBARRIER_STATE_PRIVATE_EXPEDITED, &mm->membarrier_state);
122 if (!(atomic_read(&mm->mm_users) == 1 && get_nr_threads(p) == 1)) {
123 /*
124 * Ensure all future scheduler executions will observe the
125 * new thread flag state for this process.
126 */
127 synchronize_sched();
128 }
Mathieu Desnoyersa961e402017-10-19 13:30:15 -0400129 atomic_or(MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY,
130 &mm->membarrier_state);
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400131}
132
133/**
134 * sys_membarrier - issue memory barriers on a set of threads
135 * @cmd: Takes command values defined in enum membarrier_cmd.
136 * @flags: Currently needs to be 0. For future extensions.
137 *
138 * If this system call is not implemented, -ENOSYS is returned. If the
139 * command specified does not exist, not available on the running
140 * kernel, or if the command argument is invalid, this system call
141 * returns -EINVAL. For a given command, with flags argument set to 0,
142 * this system call is guaranteed to always return the same value until
143 * reboot.
144 *
145 * All memory accesses performed in program order from each targeted thread
146 * is guaranteed to be ordered with respect to sys_membarrier(). If we use
147 * the semantic "barrier()" to represent a compiler barrier forcing memory
148 * accesses to be performed in program order across the barrier, and
149 * smp_mb() to represent explicit memory barriers forcing full memory
150 * ordering across the barrier, we have the following ordering table for
151 * each pair of barrier(), sys_membarrier() and smp_mb():
152 *
153 * The pair ordering is detailed as (O: ordered, X: not ordered):
154 *
155 * barrier() smp_mb() sys_membarrier()
156 * barrier() X X O
157 * smp_mb() X O O
158 * sys_membarrier() O O O
159 */
160SYSCALL_DEFINE2(membarrier, int, cmd, int, flags)
161{
162 if (unlikely(flags))
163 return -EINVAL;
164 switch (cmd) {
165 case MEMBARRIER_CMD_QUERY:
166 {
167 int cmd_mask = MEMBARRIER_CMD_BITMASK;
168
169 if (tick_nohz_full_enabled())
170 cmd_mask &= ~MEMBARRIER_CMD_SHARED;
171 return cmd_mask;
172 }
173 case MEMBARRIER_CMD_SHARED:
174 /* MEMBARRIER_CMD_SHARED is not compatible with nohz_full. */
175 if (tick_nohz_full_enabled())
176 return -EINVAL;
177 if (num_online_cpus() > 1)
178 synchronize_sched();
179 return 0;
180 case MEMBARRIER_CMD_PRIVATE_EXPEDITED:
Mathieu Desnoyersa961e402017-10-19 13:30:15 -0400181 return membarrier_private_expedited();
182 case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED:
183 membarrier_register_private_expedited();
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -0400184 return 0;
185 default:
186 return -EINVAL;
187 }
188}