Skip to content

Commit 2a36ab7

Browse files
Peter OskolkovPeter Zijlstra
authored andcommitted
rseq/membarrier: Add MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ
This patchset is based on Google-internal RSEQ work done by Paul Turner and Andrew Hunter. When working with per-CPU RSEQ-based memory allocations, it is sometimes important to make sure that a global memory location is no longer accessed from RSEQ critical sections. For example, there can be two per-CPU lists, one is "active" and accessed per-CPU, while another one is inactive and worked on asynchronously "off CPU" (e.g. garbage collection is performed). Then at some point the two lists are swapped, and a fast RCU-like mechanism is required to make sure that the previously active list is no longer accessed. This patch introduces such a mechanism: in short, membarrier() syscall issues an IPI to a CPU, restarting a potentially active RSEQ critical section on the CPU. Signed-off-by: Peter Oskolkov <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Acked-by: Mathieu Desnoyers <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 233e7ac commit 2a36ab7

File tree

4 files changed

+136
-31
lines changed

4 files changed

+136
-31
lines changed

include/linux/sched/mm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,13 @@ enum {
348348
MEMBARRIER_STATE_GLOBAL_EXPEDITED = (1U << 3),
349349
MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY = (1U << 4),
350350
MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE = (1U << 5),
351+
MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ_READY = (1U << 6),
352+
MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ = (1U << 7),
351353
};
352354

353355
enum {
354356
MEMBARRIER_FLAG_SYNC_CORE = (1U << 0),
357+
MEMBARRIER_FLAG_RSEQ = (1U << 1),
355358
};
356359

357360
#ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS

include/linux/syscalls.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ asmlinkage long sys_execveat(int dfd, const char __user *filename,
974974
const char __user *const __user *argv,
975975
const char __user *const __user *envp, int flags);
976976
asmlinkage long sys_userfaultfd(int flags);
977-
asmlinkage long sys_membarrier(int cmd, int flags);
977+
asmlinkage long sys_membarrier(int cmd, unsigned int flags, int cpu_id);
978978
asmlinkage long sys_mlock2(unsigned long start, size_t len, int flags);
979979
asmlinkage long sys_copy_file_range(int fd_in, loff_t __user *off_in,
980980
int fd_out, loff_t __user *off_out,

include/uapi/linux/membarrier.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,26 @@
114114
* If this command is not implemented by an
115115
* architecture, -EINVAL is returned.
116116
* Returns 0 on success.
117+
* @MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ:
118+
* Ensure the caller thread, upon return from
119+
* system call, that all its running thread
120+
* siblings have any currently running rseq
121+
* critical sections restarted if @flags
122+
* parameter is 0; if @flags parameter is
123+
* MEMBARRIER_CMD_FLAG_CPU,
124+
* then this operation is performed only
125+
* on CPU indicated by @cpu_id. If this command is
126+
* not implemented by an architecture, -EINVAL
127+
* is returned. A process needs to register its
128+
* intent to use the private expedited rseq
129+
* command prior to using it, otherwise
130+
* this command returns -EPERM.
131+
* @MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ:
132+
* Register the process intent to use
133+
* MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ.
134+
* If this command is not implemented by an
135+
* architecture, -EINVAL is returned.
136+
* Returns 0 on success.
117137
* @MEMBARRIER_CMD_SHARED:
118138
* Alias to MEMBARRIER_CMD_GLOBAL. Provided for
119139
* header backward compatibility.
@@ -131,9 +151,15 @@ enum membarrier_cmd {
131151
MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED = (1 << 4),
132152
MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE = (1 << 5),
133153
MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE = (1 << 6),
154+
MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ = (1 << 7),
155+
MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ = (1 << 8),
134156

135157
/* Alias for header backward compatibility. */
136158
MEMBARRIER_CMD_SHARED = MEMBARRIER_CMD_GLOBAL,
137159
};
138160

161+
enum membarrier_cmd_flag {
162+
MEMBARRIER_CMD_FLAG_CPU = (1 << 0),
163+
};
164+
139165
#endif /* _UAPI_LINUX_MEMBARRIER_H */

kernel/sched/membarrier.c

Lines changed: 106 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
#define MEMBARRIER_PRIVATE_EXPEDITED_SYNC_CORE_BITMASK 0
1919
#endif
2020

21+
#ifdef CONFIG_RSEQ
22+
#define MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ_BITMASK \
23+
(MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ \
24+
| MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ_BITMASK)
25+
#else
26+
#define MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ_BITMASK 0
27+
#endif
28+
2129
#define MEMBARRIER_CMD_BITMASK \
2230
(MEMBARRIER_CMD_GLOBAL | MEMBARRIER_CMD_GLOBAL_EXPEDITED \
2331
| MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED \
@@ -30,6 +38,11 @@ static void ipi_mb(void *info)
3038
smp_mb(); /* IPIs should be serializing but paranoid. */
3139
}
3240

41+
static void ipi_rseq(void *info)
42+
{
43+
rseq_preempt(current);
44+
}
45+
3346
static void ipi_sync_rq_state(void *info)
3447
{
3548
struct mm_struct *mm = (struct mm_struct *) info;
@@ -129,19 +142,27 @@ static int membarrier_global_expedited(void)
129142
return 0;
130143
}
131144

132-
static int membarrier_private_expedited(int flags)
145+
static int membarrier_private_expedited(int flags, int cpu_id)
133146
{
134-
int cpu;
135147
cpumask_var_t tmpmask;
136148
struct mm_struct *mm = current->mm;
149+
smp_call_func_t ipi_func = ipi_mb;
137150

138-
if (flags & MEMBARRIER_FLAG_SYNC_CORE) {
151+
if (flags == MEMBARRIER_FLAG_SYNC_CORE) {
139152
if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
140153
return -EINVAL;
141154
if (!(atomic_read(&mm->membarrier_state) &
142155
MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY))
143156
return -EPERM;
157+
} else if (flags == MEMBARRIER_FLAG_RSEQ) {
158+
if (!IS_ENABLED(CONFIG_RSEQ))
159+
return -EINVAL;
160+
if (!(atomic_read(&mm->membarrier_state) &
161+
MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ_READY))
162+
return -EPERM;
163+
ipi_func = ipi_rseq;
144164
} else {
165+
WARN_ON_ONCE(flags);
145166
if (!(atomic_read(&mm->membarrier_state) &
146167
MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY))
147168
return -EPERM;
@@ -156,35 +177,59 @@ static int membarrier_private_expedited(int flags)
156177
*/
157178
smp_mb(); /* system call entry is not a mb. */
158179

159-
if (!zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
180+
if (cpu_id < 0 && !zalloc_cpumask_var(&tmpmask, GFP_KERNEL))
160181
return -ENOMEM;
161182

162183
cpus_read_lock();
163-
rcu_read_lock();
164-
for_each_online_cpu(cpu) {
184+
185+
if (cpu_id >= 0) {
165186
struct task_struct *p;
166187

167-
/*
168-
* Skipping the current CPU is OK even through we can be
169-
* migrated at any point. The current CPU, at the point
170-
* where we read raw_smp_processor_id(), is ensured to
171-
* be in program order with respect to the caller
172-
* thread. Therefore, we can skip this CPU from the
173-
* iteration.
174-
*/
175-
if (cpu == raw_smp_processor_id())
176-
continue;
177-
p = rcu_dereference(cpu_rq(cpu)->curr);
178-
if (p && p->mm == mm)
179-
__cpumask_set_cpu(cpu, tmpmask);
188+
if (cpu_id >= nr_cpu_ids || !cpu_online(cpu_id))
189+
goto out;
190+
if (cpu_id == raw_smp_processor_id())
191+
goto out;
192+
rcu_read_lock();
193+
p = rcu_dereference(cpu_rq(cpu_id)->curr);
194+
if (!p || p->mm != mm) {
195+
rcu_read_unlock();
196+
goto out;
197+
}
198+
rcu_read_unlock();
199+
} else {
200+
int cpu;
201+
202+
rcu_read_lock();
203+
for_each_online_cpu(cpu) {
204+
struct task_struct *p;
205+
206+
/*
207+
* Skipping the current CPU is OK even through we can be
208+
* migrated at any point. The current CPU, at the point
209+
* where we read raw_smp_processor_id(), is ensured to
210+
* be in program order with respect to the caller
211+
* thread. Therefore, we can skip this CPU from the
212+
* iteration.
213+
*/
214+
if (cpu == raw_smp_processor_id())
215+
continue;
216+
p = rcu_dereference(cpu_rq(cpu)->curr);
217+
if (p && p->mm == mm)
218+
__cpumask_set_cpu(cpu, tmpmask);
219+
}
220+
rcu_read_unlock();
180221
}
181-
rcu_read_unlock();
182222

183223
preempt_disable();
184-
smp_call_function_many(tmpmask, ipi_mb, NULL, 1);
224+
if (cpu_id >= 0)
225+
smp_call_function_single(cpu_id, ipi_func, NULL, 1);
226+
else
227+
smp_call_function_many(tmpmask, ipi_func, NULL, 1);
185228
preempt_enable();
186229

187-
free_cpumask_var(tmpmask);
230+
out:
231+
if (cpu_id < 0)
232+
free_cpumask_var(tmpmask);
188233
cpus_read_unlock();
189234

190235
/*
@@ -283,11 +328,18 @@ static int membarrier_register_private_expedited(int flags)
283328
set_state = MEMBARRIER_STATE_PRIVATE_EXPEDITED,
284329
ret;
285330

286-
if (flags & MEMBARRIER_FLAG_SYNC_CORE) {
331+
if (flags == MEMBARRIER_FLAG_SYNC_CORE) {
287332
if (!IS_ENABLED(CONFIG_ARCH_HAS_MEMBARRIER_SYNC_CORE))
288333
return -EINVAL;
289334
ready_state =
290335
MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY;
336+
} else if (flags == MEMBARRIER_FLAG_RSEQ) {
337+
if (!IS_ENABLED(CONFIG_RSEQ))
338+
return -EINVAL;
339+
ready_state =
340+
MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ_READY;
341+
} else {
342+
WARN_ON_ONCE(flags);
291343
}
292344

293345
/*
@@ -299,6 +351,8 @@ static int membarrier_register_private_expedited(int flags)
299351
return 0;
300352
if (flags & MEMBARRIER_FLAG_SYNC_CORE)
301353
set_state |= MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE;
354+
if (flags & MEMBARRIER_FLAG_RSEQ)
355+
set_state |= MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ;
302356
atomic_or(set_state, &mm->membarrier_state);
303357
ret = sync_runqueues_membarrier_state(mm);
304358
if (ret)
@@ -310,8 +364,15 @@ static int membarrier_register_private_expedited(int flags)
310364

311365
/**
312366
* sys_membarrier - issue memory barriers on a set of threads
313-
* @cmd: Takes command values defined in enum membarrier_cmd.
314-
* @flags: Currently needs to be 0. For future extensions.
367+
* @cmd: Takes command values defined in enum membarrier_cmd.
368+
* @flags: Currently needs to be 0 for all commands other than
369+
* MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ: in the latter
370+
* case it can be MEMBARRIER_CMD_FLAG_CPU, indicating that @cpu_id
371+
* contains the CPU on which to interrupt (= restart)
372+
* the RSEQ critical section.
373+
* @cpu_id: if @flags == MEMBARRIER_CMD_FLAG_CPU, indicates the cpu on which
374+
* RSEQ CS should be interrupted (@cmd must be
375+
* MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ).
315376
*
316377
* If this system call is not implemented, -ENOSYS is returned. If the
317378
* command specified does not exist, not available on the running
@@ -337,10 +398,21 @@ static int membarrier_register_private_expedited(int flags)
337398
* smp_mb() X O O
338399
* sys_membarrier() O O O
339400
*/
340-
SYSCALL_DEFINE2(membarrier, int, cmd, int, flags)
401+
SYSCALL_DEFINE3(membarrier, int, cmd, unsigned int, flags, int, cpu_id)
341402
{
342-
if (unlikely(flags))
343-
return -EINVAL;
403+
switch (cmd) {
404+
case MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ:
405+
if (unlikely(flags && flags != MEMBARRIER_CMD_FLAG_CPU))
406+
return -EINVAL;
407+
break;
408+
default:
409+
if (unlikely(flags))
410+
return -EINVAL;
411+
}
412+
413+
if (!(flags & MEMBARRIER_CMD_FLAG_CPU))
414+
cpu_id = -1;
415+
344416
switch (cmd) {
345417
case MEMBARRIER_CMD_QUERY:
346418
{
@@ -362,13 +434,17 @@ SYSCALL_DEFINE2(membarrier, int, cmd, int, flags)
362434
case MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED:
363435
return membarrier_register_global_expedited();
364436
case MEMBARRIER_CMD_PRIVATE_EXPEDITED:
365-
return membarrier_private_expedited(0);
437+
return membarrier_private_expedited(0, cpu_id);
366438
case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED:
367439
return membarrier_register_private_expedited(0);
368440
case MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE:
369-
return membarrier_private_expedited(MEMBARRIER_FLAG_SYNC_CORE);
441+
return membarrier_private_expedited(MEMBARRIER_FLAG_SYNC_CORE, cpu_id);
370442
case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE:
371443
return membarrier_register_private_expedited(MEMBARRIER_FLAG_SYNC_CORE);
444+
case MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ:
445+
return membarrier_private_expedited(MEMBARRIER_FLAG_RSEQ, cpu_id);
446+
case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ:
447+
return membarrier_register_private_expedited(MEMBARRIER_FLAG_RSEQ);
372448
default:
373449
return -EINVAL;
374450
}

0 commit comments

Comments
 (0)