Skip to contentSkip to conversation

Restartable sequences and CPU op vector system calls

RFC PATCH Superseded Mathieu Desnoyers 13 participants 65 messages Oct 12, 2017 – Oct 23, 2017

Subsystems (12)
Arm (32-bit) x86 VFS PowerPC +8 more subsystems

v9 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
author maintainer
#1 lore raw

RFC PATCH v9 1/14 Restartable sequences system call

In reply to Mathieu Desnoyers

Expose a new system call allowing each thread to register one userspace memory area to be used as an ABI between kernel and user-space for two
purposes: user-space restartable sequences and quick access to read the current CPU number value from user-space.

* Restartable sequences (per-cpu atomics)

Restartables sequences allow user-space to perform update operations on per-cpu data without requiring heavy-weight atomic operations.

The restartable critical sections (percpu atomics) work has been started by Paul Turner and Andrew Hunter. It lets the kernel handle restart of critical sections. [1] [2] The re-implementation proposed here brings a few simplifications to the ABI which facilitates porting to other architectures and speeds up the user-space fast path. A locking-based fall-back, purely implemented in user-space, is proposed here to deal with debugger single-stepping. This fallback interacts with rseq_start() and rseq_finish(), which force retries in response to concurrent lock-based activity.

Here are benchmarks of counter increment in various scenarios compared to restartable sequences. Those benchmarks were taken on v8 of the patchset.

ARMv7 Processor rev 4 (v7l)
Machine model: Cubietruck

Counter increment speed (ns/increment)
1 thread 2 threads
global increment (baseline) 6 N/A

percpu rseq increment           50            52
percpu rseq spinlock            94            94
global atomic increment         48            74 (__sync_add_and_fetch_4)
global atomic CAS               50           172 (__sync_val_compare_and_swap_4)

global pthread mutex 148 862

ARMv7 Processor rev 10 (v7l)
Machine model: Wandboard

Counter increment speed (ns/increment)
1 thread 4 threads
global increment (baseline) 7 N/A

percpu rseq increment           50            50
percpu rseq spinlock            82            84
global atomic increment         44           262 (__sync_add_and_fetch_4)
global atomic CAS               46           316 (__sync_val_compare_and_swap_4)

global pthread mutex 146 1400

x86-64 Intel(R) Xeon(R) CPU E5-2630 v3 @ 2.40GHz:

Counter increment speed (ns/increment)
1 thread 8 threads

global increment (baseline)      3.0                N/A
percpu rseq increment            3.6                3.8
percpu rseq spinlock             5.6                6.2
global LOCK; inc                 8.0              166.4

global LOCK; cmpxchg 13.4 435.2
global pthread mutex 25.2 1363.6

* Reading the current CPU number

Speeding up reading the current CPU number on which the caller thread is running is done by keeping the current CPU number up do date within the cpu_id field of the memory area registered by the thread. This is done by making scheduler preemption set the TIF_NOTIFY_RESUME flag on the current thread. Upon return to user-space, a notify-resume handler updates the current CPU value within the registered user-space memory area. User-space can then read the current CPU number directly from memory.

Keeping the current cpu id in a memory area shared between kernel and user-space is an improvement over current mechanisms available to read the current CPU number, which has the following benefits over alternative approaches:

- 35x speedup on ARM vs system call through glibc
- 20x speedup on x86 compared to calling glibc, which calls vdso executing a "lsl" instruction,
- 14x speedup on x86 compared to inlined "lsl" instruction,
- Unlike vdso approaches, this cpu_id value can be read from an inline assembly, which makes it a useful building block for restartable sequences.
- The approach of reading the cpu id through memory mapping shared between kernel and user-space is portable (e.g. ARM), which is not the case for the lsl-based x86 vdso.

On x86, yet another possible approach would be to use the gs segment selector to point to user-space per-cpu data. This approach performs similarly to the cpu id cache, but it has two disadvantages: it is not portable, and it is incompatible with existing applications already using the gs segment selector for other purposes.

Benchmarking various approaches for reading the current CPU number:

ARMv7 Processor rev 4 (v7l)
Machine model: Cubietruck
- Baseline (empty loop): 8.4 ns
- Read CPU from rseq cpu_id: 16.7 ns
- Read CPU from rseq cpu_id (lazy register): 19.8 ns
- glibc 2.19-0ubuntu6.6 getcpu: 301.8 ns
- getcpu system call: 234.9 ns

x86-64 Intel(R) Xeon(R) CPU E5-2630 v3 @ 2.40GHz:

- Baseline (empty loop):                                    0.8 ns
- Read CPU from rseq cpu_id:                                0.8 ns
- Read CPU from rseq cpu_id (lazy register):                0.8 ns
- Read using gs segment selector:                           0.8 ns
- "lsl" inline assembly:                                   13.0 ns
- glibc 2.19-0ubuntu6 getcpu:                              16.6 ns
- getcpu system call:                                      53.9 ns

- Speed

Running 10 runs of hackbench -l 100000 seems to indicate, contrary to expectations, that enabling CONFIG_RSEQ slightly accelerates the scheduler:

Configuration: 2 sockets * 8-core Intel(R) Xeon(R) CPU E5-2630 v3 @ 2.40GHz (directly on hardware, hyperthreading disabled in BIOS, energy saving disabled in BIOS, turboboost disabled in BIOS, cpuidle.off=1 kernel parameter), with a Linux v4.6 defconfig+localyesconfig, restartable sequences series applied.

* CONFIG_RSEQ=n

avg.:      41.37 s
std.dev.:   0.36 s

* CONFIG_RSEQ=y

avg.:      40.46 s
std.dev.:   0.33 s

- Size

On x86-64, between CONFIG_RSEQ=n/y, the text size increase of vmlinux is 2855 bytes, and the data size increase of vmlinux is 1024 bytes.

* CONFIG_RSEQ=n

text data bss dec hex filename
9964559 4256280 962560 15183399 e7ae27 vmlinux.norseq

* CONFIG_RSEQ=y

text data bss dec hex filename
9967414 4257304 962560 15187278 e7bd4e vmlinux.rseq

[1] https://lwn.net/Articles/650333/
[2] http://www.linuxplumbersconf.org/2013/ocw/system/presentations/1695/original/LPC%20-%20PerCpu%20Atomics.pdf

CC Thomas Gleixner <[email protected]>
CC Paul Turner <[email protected]>
CC Andrew Hunter <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Andi Kleen <[email protected]>
CC Dave Watson <[email protected]>
CC Chris Lameter <[email protected]>
CC Ingo Molnar <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Ben Maurer <[email protected]>
CC Steven Rostedt <[email protected]>
CC "Paul E. McKenney" <[email protected]>
CC Josh Triplett <[email protected]>
CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Russell King <[email protected]>
CC Catalin Marinas <[email protected]>
CC Will Deacon <[email protected]>
CC Michael Kerrisk <[email protected]>
CC Boqun Feng <[email protected]>
CC Alexander Viro <[email protected]>

---

Changes since v1:
- Return -1, errno=EINVAL if cpu_cache pointer is not aligned on sizeof(int32_t).
- Update man page to describe the pointer alignement requirements and update atomicity guarantees.
- Add MAINTAINERS file GETCPU_CACHE entry.
- Remove dynamic memory allocation: go back to having a single getcpu_cache entry per thread. Update documentation accordingly.
- Rebased on Linux 4.4.

Changes since v2:
- Introduce a "cmd" argument, along with an enum with GETCPU_CACHE_GET and GETCPU_CACHE_SET. Introduce a uapi header linux/getcpu_cache.h defining this enumeration.
- Split resume notifier architecture implementation from the system call wire up in the following arch-specific patches.
- Man pages updates.
- Handle 32-bit compat pointers.
- Simplify handling of getcpu_cache GETCPU_CACHE_SET compiler barrier:
set the current cpu cache pointer before doing the cache update, and set it back to NULL if the update fails. Setting it back to NULL on error ensures that no resume notifier will trigger a SIGSEGV if a migration happened concurrently.

Changes since v3:
- Fix __user annotations in compat code,
- Update memory ordering comments.
- Rebased on kernel v4.5-rc5.

Changes since v4:
- Inline getcpu_cache_fork, getcpu_cache_execve, and getcpu_cache_exit.
- Add new line between if() and switch() to improve readability.
- Added sched switch benchmarks (hackbench) and size overhead comparison to change log.

Changes since v5:
- Rename "getcpu_cache" to "thread_local_abi", allowing to extend this system call to cover future features such as restartable critical sections. Generalizing this system call ensures that we can add features similar to the cpu_id field within the same cache-line without having to track one pointer per feature within the task struct.
- Add a tlabi_nr parameter to the system call, thus allowing to extend the ABI beyond the initial 64-byte structure by registering structures with tlabi_nr greater than 0. The initial ABI structure is associated with tlabi_nr 0.
- Rebased on kernel v4.5.

Changes since v6:
- Integrate "restartable sequences" v2 patchset from Paul Turner.
- Add handling of single-stepping purely in user-space, with a fallback to locking after 2 rseq failures to ensure progress, and by exposing a __rseq_table section to debuggers so they know where to put breakpoints when dealing with rseq assembly blocks which can be aborted at any point.
- make the code and ABI generic: porting the kernel implementation simply requires to wire up the signal handler and return to user-space hooks, and allocate the syscall number.
- extend testing with a fully configurable test program. See param_spinlock_test -h for details.
- handling of rseq ENOSYS in user-space, also with a fallback to locking.
- modify Paul Turner's rseq ABI to only require a single TLS store on the user-space fast-path, removing the need to populate two additional registers. This is made possible by introducing struct rseq_cs into the ABI to describe a critical section start_ip, post_commit_ip, and abort_ip.
- Rebased on kernel v4.7-rc7.

Changes since v7:
- Documentation updates.
- Integrated powerpc architecture support.
- Compare rseq critical section start_ip, allows shriking the user-space fast-path code size.
- Added Peter Zijlstra, Paul E. McKenney and Boqun Feng as co-maintainers.
- Added do_rseq2 and do_rseq_memcpy to test program helper library.
- Code cleanup based on review from Peter Zijlstra, Andy Lutomirski and Boqun Feng.
- Rebase on kernel v4.8-rc2.

Changes since v8:
- clear rseq_cs even if non-nested. Speeds up user-space fast path by removing the final "rseq_cs=NULL" assignment.
- add enum rseq_flags: critical sections and threads can set migration, preemption and signal "disable" flags to inhibit rseq behavior.
- rseq_event_counter needs to be updated with a pre-increment: Otherwise misses an increment after exec (when TLS and in-kernel states are initially 0).

Man page associated:

RSEQ(2) Linux Programmer's Manual RSEQ(2)

NAME
rseq - Restartable sequences and cpu number cache

SYNOPSIS

       #include <linux/rseq.h>

       int rseq(struct rseq * rseq, int flags);

DESCRIPTION
The rseq() ABI accelerates user-space operations on per-cpu data by defining a shared data structure ABI between each user-space thread and the kernel.

It allows user-space to perform update operations on per-cpu data without requiring heavy-weight atomic operations.

Restartable sequences are atomic with respect to preemption (mak‐ ing it atomic with respect to other threads running on the same CPU), as well as signal delivery (user-space execution contexts nested over the same thread).

It is suited for update operations on per-cpu data.

It can be used on data structures shared between threads within a process, and on data structures shared between threads across dif‐ ferent processes.

Some examples of operations that can be accelerated by this ABI:

· Querying the current CPU number,

· Incrementing per-CPU counters,

· Modifying data protected by per-CPU spinlocks,

· Inserting/removing elements in per-CPU linked-lists,

· Writing/reading per-CPU ring buffers content.

The rseq argument is a pointer to the thread-local rseq structure to be shared between kernel and user-space. A NULL rseq value unregisters the current thread rseq structure.

The layout of struct rseq is as follows:

Structure alignment
This structure is aligned on multiples of 128 bytes.

Structure size
This structure has a fixed size of 128 bytes.

Fields

cpu_id
Cache of the CPU number on which the current thread is run‐ ning.

event_counter
Counter guaranteed to be incremented when the current thread is preempted or when a signal is delivered to the current thread.

rseq_cs
The rseq_cs field is a pointer to a struct rseq_cs. Is is NULL when no rseq assembly block critical section is active for the current thread. Setting it to point to a critical section descriptor (struct rseq_cs) marks the beginning of the critical section. It is cleared after the end of the critical section.

The layout of struct rseq_cs is as follows:

Structure alignment
This structure is aligned on multiples of 256 bytes.

Structure size
This structure has a fixed size of 256 bytes.

Fields

start_ip
Instruction pointer address of the first instruction of the sequence of consecutive assembly instructions.

post_commit_ip
Instruction pointer address after the last instruction of the sequence of consecutive assembly instructions.

abort_ip
Instruction pointer address where to move the execution flow in case of abort of the sequence of consecutive assem‐ bly instructions.

Upon registration, the flags argument is currently unused and must be specified as 0. Upon unregistration, the flags argument can be either specified as 0, or as RSEQ_FORCE_UNREGISTER, which will force unregistration of the current rseq address rather than requiring each registration to be matched by an unregistration.

Libraries and applications should keep the rseq structure in a thread-local storage variable. Since only one rseq address can be registered per thread, applications and libraries should define their struct rseq as a volatile thread-local storage variable with the weak symbol __rseq_abi. This allows using rseq from an appli‐ cation executable and from multiple shared libraries linked to the same executable. The cpu_id field should be initialized to -1.

Each thread is responsible for registering and unregistering its rseq structure. No more than one rseq structure address can be registered per thread at a given time. The same address can be registered more than once for a thread, and each registration needs to have a matching unregistration before the address is effectively unregistered. After the rseq address is effectively unregistered for a thread, a new address can be registered. Unreg‐ istration of associated rseq structure is implicitly performed when a thread or process exits.

In a typical usage scenario, the thread registering the rseq structure will be performing loads and stores from/to that struc‐ ture. It is however also allowed to read that structure from other threads. The rseq field updates performed by the kernel provide relaxed atomicity semantics, which guarantee that other threads performing relaxed atomic reads of the cpu number cache will always observe a consistent value.

RETURN VALUE
A return value of 0 indicates success. On error, -1 is returned, and errno is set appropriately.

ERRORS
EINVAL Either flags contains an invalid value, or rseq contains an
address which is not appropriately aligned.

ENOSYS The rseq() system call is not implemented by this kernel.

EFAULT rseq is an invalid address.

EBUSY The rseq argument contains a non-NULL address which differs
from the memory location already registered for this thread.

EOVERFLOW
Registering the rseq address is not allowed because it would cause a reference counter overflow.

ENOENT The rseq argument is NULL, but no memory location is cur‐
rently registered for this thread.

VERSIONS
The rseq() system call was added in Linux 4.X (TODO).

CONFORMING TO
rseq() is Linux-specific.

ALGORITHM
The restartable sequences mechanism is the overlap of two distinct restart mechanisms: a sequence counter tracking preemption and signal delivery for high-level code, and an ip-fixup-based mecha‐ nism for the final assembly instruction sequence.

A high-level summary of the algorithm to use rseq from user-space is as follows:

The high-level code between rseq_start() and rseq_finish() loads the current value of the sequence counter in rseq_start(), and then it gets compared with the new current value within the
rseq_finish() restartable instruction sequence. Between
rseq_start() and rseq_finish(), the high-level code can perform operations that do not have side-effects, such as getting the cur‐ rent CPU number, and loading from variables.

Stores are performed at the very end of the restartable sequence assembly block. Each assembly block defines a struct rseq_cs
structure which describes the start_ip and post_commit_ip
addresses, as well as the abort_ip address where the kernel should move the thread instruction pointer if a rseq critical section assembly block is preempted or if a signal is delivered on top of a rseq critical section assembly block.

Detailed algorithm of rseq use:

rseq_start()

0. Userspace loads the current event counter value from the event_counter field of the registered struct rseq TLS area,

rseq_finish()

Steps [1]-[3] (inclusive) need to be a sequence of instruc‐ tions in userspace that can handle being moved to the abort_ip between any of those instructions.

The abort_ip address needs to be less than start_ip, or
greater-or-equal the post_commit_ip. Step [4] and the
failure code step [F1] need to be at addresses lesser than start_ip, or greater-or-equal the post_commit_ip.

[ start_ip ]

1. Userspace stores the address of the struct rseq_cs assembly block descriptor into the rseq_cs field of the registered struct rseq TLS area.

2. Userspace tests to see whether the current event_counter value match the value loaded at [0]. Manually jumping to [F1] in case of a mismatch.

Note that if we are preempted or interrupted by a signal after [1] and before post_commit_ip, then the kernel also performs the comparison performed in [2], and conditionally clears the rseq_cs field of struct rseq, then jumps us to abort_ip.

3. Userspace critical section final instruction before
post_commit_ip is the commit. The critical section is self- terminating.

[ post_commit_ip ]

4. Userspace clears the rseq_cs field of the struct rseq TLS area.

5. Return true.

On failure at [2]:

F1.
Userspace clears the rseq_cs field of the struct rseq TLS area. Followed by step [F2].

[ abort_ip ]

F2.
Return false.

EXAMPLE

       The following code uses the rseq() system call to keep a thread-local
       storage variable up to date with the current CPU number, with a fall‐
       back on sched_getcpu(3) if the cache is not  available.  For  example
       simplicity,  it  is  done in main(), but multithreaded programs would
       need to invoke rseq() from each program thread.

           #define _GNU_SOURCE
           #include <stdlib.h>
           #include <stdio.h>
           #include <unistd.h>
           #include <stdint.h>
           #include <sched.h>
           #include <stddef.h>
           #include <errno.h>
           #include <string.h>
           #include <stdbool.h>
           #include <sys/syscall.h>
           #include <linux/rseq.h>

           __attribute__((weak)) __thread volatile struct rseq __rseq_abi = {
               .u.e.cpu_id = -1,
           };

           static int
           sys_rseq(volatile struct rseq *rseq_abi, int flags)
           {
               return syscall(__NR_rseq, rseq_abi, flags);
           }

           static int32_t
           rseq_current_cpu_raw(void)
           {
               return __rseq_abi.u.e.cpu_id;
           }

           static int32_t
           rseq_current_cpu(void)
           {
               int32_t cpu;

               cpu = rseq_current_cpu_raw();
               if (cpu < 0)
                   cpu = sched_getcpu();
               return cpu;
           }

           static int
           rseq_register_current_thread(void)
           {
               int rc;

               rc = sys_rseq(&__rseq_abi, 0);
               if (rc) {
                   fprintf(stderr,
                       "Error: sys_rseq(...) register failed(%d): %s\n",
                       errno, strerror(errno));
                   return -1;
               }
               return 0;
           }

           static int
           rseq_unregister_current_thread(void)
           {
               int rc;

               rc = sys_rseq(NULL, 0);
               if (rc) {
                   fprintf(stderr,
                       "Error: sys_rseq(...) unregister failed(%d): %s\n",
                       errno, strerror(errno));
                   return -1;
               }
               return 0;
           }

           int
           main(int argc, char **argv)
           {
               bool rseq_registered = false;

               if (!rseq_register_current_thread()) {
                   rseq_registered = true;
               } else {
                   fprintf(stderr,
                       "Unable to register restartable sequences.\n");
                   fprintf(stderr, "Using sched_getcpu() as fallback.\n");
               }

               printf("Current CPU number: %d\n", rseq_current_cpu());

               if (rseq_registered && rseq_unregister_current_thread()) {
                   exit(EXIT_FAILURE);
               }
               exit(EXIT_SUCCESS);
           }

SEE ALSO

       sched_getcpu(3)

Linux 2016-08-19 RSEQ(2)

---
 MAINTAINERS               |  10 ++
 arch/Kconfig              |   7 +
 fs/exec.c                 |   1 +
 include/linux/sched.h     |  89 ++++++++++++
 include/uapi/linux/rseq.h | 131 +++++++++++++++++
 init/Kconfig              |  13 ++
 kernel/Makefile           |   1 +
 kernel/fork.c             |   2 +
 kernel/rseq.c             | 347 ++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sched/core.c       |   4 +
 kernel/sys_ni.c           |   3 +
 11 files changed, 608 insertions(+)
 create mode 100644 include/uapi/linux/rseq.h
 create mode 100644 kernel/rseq.c
MAINTAINERS +10 -0
index 1c3feffb1c1c..f05c526fe1e8 100644
@@ -11224,6 +11224,16 @@ F:	include/dt-bindings/reset/
 F:	include/linux/reset.h
 F:	include/linux/reset-controller.h
 
+RESTARTABLE SEQUENCES SUPPORT
+M:	Mathieu Desnoyers <[email protected]>
+M:	Peter Zijlstra <[email protected]>
+M:	"Paul E. McKenney" <[email protected]>
+M:	Boqun Feng <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	kernel/rseq.c
+F:	include/uapi/linux/rseq.h
+
 RFKILL
 M:	Johannes Berg <[email protected]>
 L:	[email protected]
arch/Kconfig +7 -0
index 21d0089117fe..6f1203612403 100644
@@ -257,6 +257,13 @@ config HAVE_REGS_AND_STACK_ACCESS_API
 	  declared in asm/ptrace.h
 	  For example the kprobes-based event tracer needs this API.
 
+config HAVE_RSEQ
+	bool
+	depends on HAVE_REGS_AND_STACK_ACCESS_API
+	help
+	  This symbol should be selected by an architecture if it
+	  supports an implementation of restartable sequences.
+
 config HAVE_CLK
 	bool
 	help
fs/exec.c +1 -0
index 62175cbcc801..75fcbaeb0206 100644
@@ -1794,6 +1794,7 @@ static int do_execveat_common(int fd, struct filename *filename,
 	/* execve succeeded */
 	current->fs->in_exec = 0;
 	current->in_execve = 0;
+	rseq_execve(current);
 	acct_update_integrals(current);
 	task_numa_free(current);
 	free_bprm(bprm);
include/linux/sched.h +89 -0

105 lines not on this page · raw message

include/uapi/linux/rseq.h new file +131 -0

131 lines not on this page · raw message

init/Kconfig +13 -0
index 8514b25db21c..b8aa41bd4f4f 100644
@@ -1395,6 +1395,19 @@ config MEMBARRIER
 
 	  If unsure, say Y.
 
+config RSEQ
+	bool "Enable rseq() system call" if EXPERT
+	default y
+	depends on HAVE_RSEQ
+	help
+	  Enable the restartable sequences system call. It provides a
+	  user-space cache for the current CPU number value, which
+	  speeds up getting the current CPU number from user-space,
+	  as well as an ABI to speed up user-space operations on
+	  per-CPU data.
+
+	  If unsure, say Y.
+
 config EMBEDDED
 	bool "Embedded system"
 	option allnoconfig_y
kernel/Makefile +1 -0
index 4cb8e8b23c6e..5c09592b3b9f 100644
@@ -111,6 +111,7 @@ obj-$(CONFIG_TORTURE_TEST) += torture.o
 obj-$(CONFIG_MEMBARRIER) += membarrier.o
 
 obj-$(CONFIG_HAS_IOMEM) += memremap.o
+obj-$(CONFIG_RSEQ) += rseq.o
 
 $(obj)/configs.o: $(obj)/config_data.h
 
kernel/fork.c +2 -0
index b7e9e57b71ea..f311a99fb1d1 100644
@@ -1849,6 +1849,8 @@ static __latent_entropy struct task_struct *copy_process(
 	 */
 	copy_seccomp(p);
 
+	rseq_fork(p, clone_flags);
+
 	/*
 	 * Process group and session signals need to be delivered to just the
 	 * parent before the fork or both the parent and the child after the
kernel/rseq.c new file +347 -0

347 lines not on this page · raw message

kernel/sched/core.c +4 -0
index 0869b20fba81..12da0f771d73 100644
@@ -1170,6 +1170,8 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
 #endif
 #endif
 
+	rseq_migrate(p);
+
 	trace_sched_migrate_task(p, new_cpu);
 
 	if (task_cpu(p) != new_cpu) {
@@ -2572,6 +2574,7 @@ prepare_task_switch(struct rq *rq, struct task_struct *prev,
 {
 	sched_info_switch(rq, prev, next);
 	perf_event_task_sched_out(prev, next);
+	rseq_sched_out(prev);
 	fire_sched_out_preempt_notifiers(prev, next);
 	prepare_lock_switch(rq, next);
 	prepare_arch_switch(next);
@@ -3322,6 +3325,7 @@ static void __sched notrace __schedule(bool preempt)
 	clear_preempt_need_resched();
 
 	if (likely(prev != next)) {
+		rseq_preempt(prev);
 		rq->nr_switches++;
 		rq->curr = next;
 		++*switch_count;
kernel/sys_ni.c +3 -0
index 8acef8576ce9..c7b366ccf39c 100644
@@ -258,3 +258,6 @@ cond_syscall(sys_membarrier);
 cond_syscall(sys_pkey_mprotect);
 cond_syscall(sys_pkey_alloc);
 cond_syscall(sys_pkey_free);
+
+/* restartable sequence */
+cond_syscall(sys_rseq);
Signature · 1 line
2.11.0
author maintainer
#35 lore raw

RFC PATCH v9 2/14 tracing: instrument restartable sequences

In reply to Mathieu Desnoyers
CC Thomas Gleixner <[email protected]>
CC Paul Turner <[email protected]>
CC Andrew Hunter <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Andi Kleen <[email protected]>
CC Dave Watson <[email protected]>
CC Chris Lameter <[email protected]>
CC Ingo Molnar <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Ben Maurer <[email protected]>
CC Steven Rostedt <[email protected]>
CC "Paul E. McKenney" <[email protected]>
CC Josh Triplett <[email protected]>
CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Russell King <[email protected]>
CC Catalin Marinas <[email protected]>
CC Will Deacon <[email protected]>
CC Michael Kerrisk <[email protected]>
CC Boqun Feng <[email protected]>
---
 MAINTAINERS                 |  1 +
 include/trace/events/rseq.h | 64 +++++++++++++++++++++++++++++++++++++++++++++
 kernel/rseq.c               |  7 +++++
 3 files changed, 72 insertions(+)
 create mode 100644 include/trace/events/rseq.h
MAINTAINERS +1 -0
index f05c526fe1e8..9d6a830a8c32 100644
@@ -11233,6 +11233,7 @@ L:	[email protected]
 S:	Supported
 F:	kernel/rseq.c
 F:	include/uapi/linux/rseq.h
+F:	include/trace/events/rseq.h
 
 RFKILL
 M:	Johannes Berg <[email protected]>
include/trace/events/rseq.h new file +64 -0

64 lines not on this page · raw message

kernel/rseq.c +7 -0
index 706a83bd885c..31582e5ff3be 100644
@@ -32,6 +32,9 @@
 #include <linux/types.h>
 #include <asm/ptrace.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/rseq.h>
+
 /*
  * The restartable sequences mechanism is the overlap of two distinct
  * restart mechanisms: a sequence counter tracking preemption and signal
@@ -139,6 +142,7 @@ static bool rseq_update_cpu_id_event_counter(struct task_struct *t,
 			t->rseq_event_counter;
 	if (__put_user(u.v, &t->rseq->u.v))
 		return false;
+	trace_rseq_update(t);
 	return true;
 }
 
@@ -230,6 +234,9 @@ static int rseq_ip_fixup(struct pt_regs *regs)
 
 	ret = rseq_get_rseq_cs(t, &start_ip, &post_commit_ip, &abort_ip,
 			&cs_flags);
+	trace_rseq_ip_fixup((void __user *)instruction_pointer(regs),
+		start_ip, post_commit_ip, abort_ip, t->rseq_event_counter,
+		ret);
 	if (!ret)
 		return -EFAULT;
 
Signature · 1 line
2.11.0
author maintainer
#36 lore raw

RFC PATCH v9 3/14 Restartable sequences: ARM 32 architecture support

In reply to Mathieu Desnoyers

Call the rseq_handle_notify_resume() function on return to userspace if TIF_NOTIFY_RESUME thread flag is set.

Increment the event counter and perform fixup on the pre-signal frame when a signal is delivered on top of a restartable sequence critical section.

CC Russell King <[email protected]>
CC Catalin Marinas <[email protected]>
CC Will Deacon <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Paul Turner <[email protected]>
CC Andrew Hunter <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Andi Kleen <[email protected]>
CC Dave Watson <[email protected]>
CC Chris Lameter <[email protected]>
CC Ingo Molnar <[email protected]>
CC Ben Maurer <[email protected]>
CC Steven Rostedt <[email protected]>
CC "Paul E. McKenney" <[email protected]>
CC Josh Triplett <[email protected]>
CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Boqun Feng <[email protected]>
---
 arch/arm/Kconfig         | 1 +
 arch/arm/kernel/signal.c | 7 +++++++
 2 files changed, 8 insertions(+)
arch/arm/Kconfig +1 -0
index 61a0cb15067e..85bc5d8de3eb 100644
@@ -86,6 +86,7 @@ config ARM
 	select HAVE_PERF_USER_STACK_DUMP
 	select HAVE_RCU_TABLE_FREE if (SMP && ARM_LPAE)
 	select HAVE_REGS_AND_STACK_ACCESS_API
+	select HAVE_RSEQ
 	select HAVE_SYSCALL_TRACEPOINTS
 	select HAVE_UID16
 	select HAVE_VIRT_CPU_ACCOUNTING_GEN
arch/arm/kernel/signal.c +7 -0
index 5814298ef0b7..7de5df4ba6ec 100644
@@ -517,6 +517,12 @@ static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
 	int ret;
 
 	/*
+	 * Increment event counter and perform fixup for the pre-signal
+	 * frame.
+	 */
+	rseq_signal_deliver(regs);
+
+	/*
 	 * Set up the stack frame
 	 */
 	if (ksig->ka.sa.sa_flags & SA_SIGINFO)
@@ -636,6 +642,7 @@ do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
 			} else {
 				clear_thread_flag(TIF_NOTIFY_RESUME);
 				tracehook_notify_resume(regs);
+				rseq_handle_notify_resume(regs);
 			}
 		}
 		local_irq_disable();
Signature · 1 line
2.11.0
author maintainer
#37 lore raw

RFC PATCH v9 4/14 Restartable sequences: wire up ARM 32 system call

In reply to Mathieu Desnoyers

Wire up the rseq system call on 32-bit ARM.

This provides an ABI improving the speed of a user-space getcpu operation on ARM by skipping the getcpu system call on the fast path, as well as improving the speed of user-space operations on per-cpu data compared to using load-linked/store-conditional.

CC Russell King <[email protected]>
CC Catalin Marinas <[email protected]>
CC Will Deacon <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Paul Turner <[email protected]>
CC Andrew Hunter <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Andi Kleen <[email protected]>
CC Dave Watson <[email protected]>
CC Chris Lameter <[email protected]>
CC Ingo Molnar <[email protected]>
CC Ben Maurer <[email protected]>
CC Steven Rostedt <[email protected]>
CC "Paul E. McKenney" <[email protected]>
CC Josh Triplett <[email protected]>
CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Boqun Feng <[email protected]>
---
 arch/arm/tools/syscall.tbl | 1 +
 1 file changed, 1 insertion(+)
arch/arm/tools/syscall.tbl +1 -0
index 0bb0e9c6376c..fbc74b5fa3ed 100644
@@ -412,3 +412,4 @@
 395	common	pkey_alloc		sys_pkey_alloc
 396	common	pkey_free		sys_pkey_free
 397	common	statx			sys_statx
+398	common	rseq			sys_rseq
Signature · 1 line
2.11.0
author maintainer
#38 lore raw

RFC PATCH v9 5/14 Restartable sequences: x86 32/64 architecture support

In reply to Mathieu Desnoyers

Call the rseq_handle_notify_resume() function on return to userspace if TIF_NOTIFY_RESUME thread flag is set.

Increment the event counter and perform fixup on the pre-signal frame when a signal is delivered on top of a restartable sequence critical section.

CC Russell King <[email protected]>
CC Catalin Marinas <[email protected]>
CC Will Deacon <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Paul Turner <[email protected]>
CC Andrew Hunter <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Andi Kleen <[email protected]>
CC Dave Watson <[email protected]>
CC Chris Lameter <[email protected]>
CC Ingo Molnar <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Ben Maurer <[email protected]>
CC Steven Rostedt <[email protected]>
CC "Paul E. McKenney" <[email protected]>
CC Josh Triplett <[email protected]>
CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Boqun Feng <[email protected]>
---
 arch/x86/Kconfig         | 1 +
 arch/x86/entry/common.c  | 1 +
 arch/x86/kernel/signal.c | 6 ++++++
 3 files changed, 8 insertions(+)
arch/x86/Kconfig +1 -0
index 323cb065be5e..b2ce9970bc9c 100644
@@ -170,6 +170,7 @@ config X86
 	select HAVE_REGS_AND_STACK_ACCESS_API
 	select HAVE_RELIABLE_STACKTRACE		if X86_64 && FRAME_POINTER && STACK_VALIDATION
 	select HAVE_STACK_VALIDATION		if X86_64
+	select HAVE_RSEQ
 	select HAVE_SYSCALL_TRACEPOINTS
 	select HAVE_UNSTABLE_SCHED_CLOCK
 	select HAVE_USER_RETURN_NOTIFIER
arch/x86/entry/common.c +1 -0
index cdefcfdd9e63..2077086ecb94 100644
@@ -159,6 +159,7 @@ static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
 		if (cached_flags & _TIF_NOTIFY_RESUME) {
 			clear_thread_flag(TIF_NOTIFY_RESUME);
 			tracehook_notify_resume(regs);
+			rseq_handle_notify_resume(regs);
 		}
 
 		if (cached_flags & _TIF_USER_RETURN_NOTIFY)
arch/x86/kernel/signal.c +6 -0
index cc30a74e4adb..61adc59cde3f 100644
@@ -686,6 +686,12 @@ setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
 	sigset_t *set = sigmask_to_save();
 	compat_sigset_t *cset = (compat_sigset_t *) set;
 
+	/*
+	 * Increment event counter and perform fixup for the pre-signal
+	 * frame.
+	 */
+	rseq_signal_deliver(regs);
+
 	/* Set up the stack frame */
 	if (is_ia32_frame(ksig)) {
 		if (ksig->ka.sa.sa_flags & SA_SIGINFO)
Signature · 1 line
2.11.0
author maintainer
#39 lore raw

RFC PATCH v9 6/14 Restartable sequences: wire up x86 32/64 system call

In reply to Mathieu Desnoyers

Wire up the rseq system call on x86 32/64.

This provides an ABI improving the speed of a user-space getcpu operation on x86 by removing the need to perform a function call, "lsl" instruction, or system call on the fast path, as well as improving the speed of user-space operations on per-cpu data.

CC Russell King <[email protected]>
CC Catalin Marinas <[email protected]>
CC Will Deacon <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Paul Turner <[email protected]>
CC Andrew Hunter <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Andi Kleen <[email protected]>
CC Dave Watson <[email protected]>
CC Chris Lameter <[email protected]>
CC Ingo Molnar <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Ben Maurer <[email protected]>
CC Steven Rostedt <[email protected]>
CC "Paul E. McKenney" <[email protected]>
CC Josh Triplett <[email protected]>
CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Boqun Feng <[email protected]>
---
 arch/x86/entry/syscalls/syscall_32.tbl | 1 +
 arch/x86/entry/syscalls/syscall_64.tbl | 1 +
 2 files changed, 2 insertions(+)
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
index 448ac2161112..ba43ee75e425 100644
@@ -391,3 +391,4 @@
 382	i386	pkey_free		sys_pkey_free
 383	i386	statx			sys_statx
 384	i386	arch_prctl		sys_arch_prctl			compat_sys_arch_prctl
+385	i386	rseq			sys_rseq
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index 5aef183e2f85..3ad03495bbb9 100644
@@ -339,6 +339,7 @@
 330	common	pkey_alloc		sys_pkey_alloc
 331	common	pkey_free		sys_pkey_free
 332	common	statx			sys_statx
+333	common	rseq			sys_rseq
 
 #
 # x32-specific system call numbers start at 512 to avoid cache impact
Signature · 1 line
2.11.0
author maintainer
#42 lore raw

RFC PATCH v9 9/14 Provide cpu_opv system call

In reply to Mathieu Desnoyers

This new cpu_opv system call executes a vector of operations on behalf of user-space on a specific CPU with preemption disabled. It is inspired from readv() and writev() system calls which take a "struct iovec" array as argument.

The operations available are: comparison, memcpy, add, or, and, xor, left shift, and right shift. The system call receives a CPU number from user-space as argument, which is the CPU on which those operations need to be performed. All preparation steps such as loading pointers, and applying offsets to arrays, need to be performed by user-space before invoking the system call. The "comparison" operation can be used to check that the data used in the preparation step did not change between preparation of system call inputs and operation execution within the preempt-off critical section.

The reason why we require all pointer offsets to be calculated by user-space beforehand is because we need to use get_user_pages_fast() to first pin all pages touched by each operation. This takes care of faulting-in the pages. Then, preemption is disabled, and the operations are performed atomically with respect to other thread execution on that CPU, without generating any page fault.

A maximum limit of 16 operations per cpu_opv syscall invocation is enforced, so user-space cannot generate a too long preempt-off critical section. Each operation is also limited a length of PAGE_SIZE bytes, meaning that an operation can touch a maximum of 4 pages (memcpy: 2 pages for source, 2 pages for destination if addresses are not aligned on page boundaries).

If the thread is not running on the requested CPU, a new push_task_to_cpu() is invoked to migrate the task to the requested CPU. If the requested CPU is not part of the cpus allowed mask of the thread, the system call fails with EINVAL. After the migration has been performed, preemption is disabled, and the current CPU number is checked again and compared to the requested CPU number. If it still differs, it means the scheduler migrated us away from that CPU. Return EAGAIN to user-space in that case, and let user-space retry (either requesting the same CPU number, or a different one, depending on the user-space algorithm constraints).

CC "Paul E. McKenney" <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Paul Turner <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Andrew Hunter <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Andi Kleen <[email protected]>
CC Dave Watson <[email protected]>
CC Chris Lameter <[email protected]>
CC Ingo Molnar <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Ben Maurer <[email protected]>
CC Steven Rostedt <[email protected]>
CC Josh Triplett <[email protected]>
CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Russell King <[email protected]>
CC Catalin Marinas <[email protected]>
CC Will Deacon <[email protected]>
CC Michael Kerrisk <[email protected]>
CC Boqun Feng <[email protected]>
---
 MAINTAINERS                  |    7 +
 include/uapi/linux/cpu_opv.h |   93 ++++
 init/Kconfig                 |   14 +
 kernel/Makefile              |    1 +
 kernel/cpu_opv.c             | 1000 ++++++++++++++++++++++++++++++++++++++++++
 kernel/sched/core.c          |   37 ++
 kernel/sched/sched.h         |    2 +
 kernel/sys_ni.c              |    1 +
 8 files changed, 1155 insertions(+)
 create mode 100644 include/uapi/linux/cpu_opv.h
 create mode 100644 kernel/cpu_opv.c
MAINTAINERS +7 -0
index 9d6a830a8c32..6a5f3afb2ea4 100644
@@ -3611,6 +3611,13 @@ B:	https://bugzilla.kernel.org
 F:	drivers/cpuidle/*
 F:	include/linux/cpuidle.h
 
+CPU NON-PREEMPTIBLE OPERATION VECTOR SUPPORT
+M:	Mathieu Desnoyers <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	kernel/cpu_opv.c
+F:	include/uapi/linux/cpu_opv.h
+
 CRAMFS FILESYSTEM
 W:	http://sourceforge.net/projects/cramfs/
 S:	Orphan / Obsolete
include/uapi/linux/cpu_opv.h new file +93 -0

93 lines not on this page · raw message

init/Kconfig +14 -0
index b8aa41bd4f4f..98b79eb9020e 100644
@@ -1399,6 +1399,7 @@ config RSEQ
 	bool "Enable rseq() system call" if EXPERT
 	default y
 	depends on HAVE_RSEQ
+	select CPU_OPV
 	help
 	  Enable the restartable sequences system call. It provides a
 	  user-space cache for the current CPU number value, which
@@ -1408,6 +1409,19 @@ config RSEQ
 
 	  If unsure, say Y.
 
+config CPU_OPV
+	bool "Enable cpu_opv() system call" if EXPERT
+	default y
+	help
+	  Enable the CPU preempt-off operation vector system call.
+	  It allows user-space to perform a sequence of operations on
+	  per-cpu data with preemption disabled. Useful as
+	  single-stepping fall-back for restartable sequences, and for
+	  performing more complex operations on per-cpu data that would
+	  not be otherwise possible to do with restartable sequences.
+
+	  If unsure, say Y.
+
 config EMBEDDED
 	bool "Embedded system"
 	option allnoconfig_y
kernel/Makefile +1 -0
index 5c09592b3b9f..8301e454c2a8 100644
@@ -112,6 +112,7 @@ obj-$(CONFIG_MEMBARRIER) += membarrier.o
 
 obj-$(CONFIG_HAS_IOMEM) += memremap.o
 obj-$(CONFIG_RSEQ) += rseq.o
+obj-$(CONFIG_CPU_OPV) += cpu_opv.o
 
 $(obj)/configs.o: $(obj)/config_data.h
 
kernel/cpu_opv.c new file +1000 -0

1,000 lines not on this page · raw message

kernel/sched/core.c +37 -0
index 12da0f771d73..db50984f7535 100644
@@ -1047,6 +1047,43 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
 		set_curr_task(rq, p);
 }
 
+int push_task_to_cpu(struct task_struct *p, unsigned int dest_cpu)
+{
+	struct rq_flags rf;
+	struct rq *rq;
+	int ret = 0;
+
+	rq = task_rq_lock(p, &rf);
+	update_rq_clock(rq);
+
+	if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	if (task_cpu(p) == dest_cpu)
+		goto out;
+
+	if (task_running(rq, p) || p->state == TASK_WAKING) {
+		struct migration_arg arg = { p, dest_cpu };
+		/* Need help from migration thread: drop lock and wait. */
+		task_rq_unlock(rq, p, &rf);
+		stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
+		tlb_migrate_finish(p->mm);
+		return 0;
+	} else if (task_on_rq_queued(p)) {
+		/*
+		 * OK, since we're going to drop the lock immediately
+		 * afterwards anyway.
+		 */
+		rq = move_queued_task(rq, &rf, p, dest_cpu);
+	}
+out:
+	task_rq_unlock(rq, p, &rf);
+
+	return ret;
+}
+
 /*
  * Change a given task's CPU affinity. Migrate the thread to a
  * proper CPU and schedule it away if the CPU it's executing on
kernel/sched/sched.h +2 -0
index eeef1a3086d1..a1c0e60006f8 100644
@@ -1207,6 +1207,8 @@ static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
 #endif
 }
 
+int push_task_to_cpu(struct task_struct *p, unsigned int dest_cpu);
+
 /*
  * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
  */
kernel/sys_ni.c +1 -0
index c7b366ccf39c..044808ac8197 100644
@@ -261,3 +261,4 @@ cond_syscall(sys_pkey_free);
 
 /* restartable sequence */
 cond_syscall(sys_rseq);
+cond_syscall(sys_cpu_opv);
Signature · 1 line
2.11.0
author maintainer
#49 lore raw

RFC PATCH v9 10/14 cpu_opv: Wire up x86 32/64 system call

In reply to Mathieu Desnoyers
CC "Paul E. McKenney" <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Paul Turner <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Andrew Hunter <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Andi Kleen <[email protected]>
CC Dave Watson <[email protected]>
CC Chris Lameter <[email protected]>
CC Ingo Molnar <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Ben Maurer <[email protected]>
CC Steven Rostedt <[email protected]>
CC Josh Triplett <[email protected]>
CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Russell King <[email protected]>
CC Catalin Marinas <[email protected]>
CC Will Deacon <[email protected]>
CC Michael Kerrisk <[email protected]>
CC Boqun Feng <[email protected]>
---
 arch/x86/entry/syscalls/syscall_32.tbl | 1 +
 arch/x86/entry/syscalls/syscall_64.tbl | 1 +
 2 files changed, 2 insertions(+)
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
index ba43ee75e425..afc6988fb2c8 100644
@@ -392,3 +392,4 @@
 383	i386	statx			sys_statx
 384	i386	arch_prctl		sys_arch_prctl			compat_sys_arch_prctl
 385	i386	rseq			sys_rseq
+386	i386	cpu_opv			sys_cpu_opv
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index 3ad03495bbb9..ab5d1f9f9396 100644
@@ -340,6 +340,7 @@
 331	common	pkey_free		sys_pkey_free
 332	common	statx			sys_statx
 333	common	rseq			sys_rseq
+334	common	cpu_opv			sys_cpu_opv
 
 #
 # x32-specific system call numbers start at 512 to avoid cache impact
Signature · 1 line
2.11.0
author maintainer
#51 lore raw

RFC PATCH v9 12/14 cpu_opv: Wire up ARM32 system call

In reply to Mathieu Desnoyers
CC Russell King <[email protected]>
CC Catalin Marinas <[email protected]>
CC Will Deacon <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Paul Turner <[email protected]>
CC Andrew Hunter <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Andi Kleen <[email protected]>
CC Dave Watson <[email protected]>
CC Chris Lameter <[email protected]>
CC Ingo Molnar <[email protected]>
CC Ben Maurer <[email protected]>
CC Steven Rostedt <[email protected]>
CC "Paul E. McKenney" <[email protected]>
CC Josh Triplett <[email protected]>
CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Boqun Feng <[email protected]>
---
 arch/arm/tools/syscall.tbl | 1 +
 1 file changed, 1 insertion(+)
arch/arm/tools/syscall.tbl +1 -0
index fbc74b5fa3ed..213ccfc2c437 100644
@@ -413,3 +413,4 @@
 396	common	pkey_free		sys_pkey_free
 397	common	statx			sys_statx
 398	common	rseq			sys_rseq
+399	common	cpu_opv			sys_cpu_opv
Signature · 1 line
2.11.0
author maintainer
#52 lore raw

RFC PATCH v9 13/14 cpu_opv: Implement selftests

In reply to Mathieu Desnoyers
CC Russell King <[email protected]>
CC Catalin Marinas <[email protected]>
CC Will Deacon <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Paul Turner <[email protected]>
CC Andrew Hunter <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Andi Kleen <[email protected]>
CC Dave Watson <[email protected]>
CC Chris Lameter <[email protected]>
CC Ingo Molnar <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Ben Maurer <[email protected]>
CC Steven Rostedt <[email protected]>
CC "Paul E. McKenney" <[email protected]>
CC Josh Triplett <[email protected]>
CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Boqun Feng <[email protected]>
CC Shuah Khan <[email protected]>
---
 MAINTAINERS                                        |   1 +
 tools/testing/selftests/cpu-opv/.gitignore         |   1 +
 tools/testing/selftests/cpu-opv/Makefile           |  13 +
 .../testing/selftests/cpu-opv/basic_cpu_opv_test.c | 828 +++++++++++++++++++++
 tools/testing/selftests/cpu-opv/cpu-op.c           | 189 +++++
 tools/testing/selftests/cpu-opv/cpu-op.h           |  53 ++
 6 files changed, 1085 insertions(+)
 create mode 100644 tools/testing/selftests/cpu-opv/.gitignore
 create mode 100644 tools/testing/selftests/cpu-opv/Makefile
 create mode 100644 tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c
 create mode 100644 tools/testing/selftests/cpu-opv/cpu-op.c
 create mode 100644 tools/testing/selftests/cpu-opv/cpu-op.h
MAINTAINERS +1 -0
index 6a5f3afb2ea4..9134a3234737 100644
@@ -3617,6 +3617,7 @@ L:	[email protected]
 S:	Supported
 F:	kernel/cpu_opv.c
 F:	include/uapi/linux/cpu_opv.h
+F:	tools/testing/selftests/cpu-opv/
 
 CRAMFS FILESYSTEM
 W:	http://sourceforge.net/projects/cramfs/
tools/testing/selftests/cpu-opv/.gitignore new file +1 -0
new file mode 100644
index 000000000000..c7186eb95cf5
@@ -0,0 +1 @@
+basic_cpu_opv_test
tools/testing/selftests/cpu-opv/Makefile new file +13 -0
new file mode 100644
index 000000000000..81d0596824ee
@@ -0,0 +1,13 @@
+CFLAGS += -O2 -Wall -g -I./ -I../../../../usr/include/
+LDFLAGS += -lpthread
+
+TESTS = basic_cpu_opv_test
+
+all: $(TESTS)
+%: %.c cpu-op.c cpu-op.h
+	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
+
+include ../lib.mk
+
+clean:
+	$(RM) $(TESTS)
tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c new file +828 -0

828 lines not on this page · raw message

tools/testing/selftests/cpu-opv/cpu-op.c new file +189 -0

189 lines not on this page · raw message

tools/testing/selftests/cpu-opv/cpu-op.h new file +53 -0

53 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#53 lore raw

RFC PATCH v9 14/14 Restartable sequences: Provide self-tests

In reply to Mathieu Desnoyers

Implements two basic tests of RSEQ functionality, and one more exhaustive parameterizable test.

The first, "basic_test" only asserts that RSEQ works moderately correctly.
E.g. that:
- The CPUID pointer works
- Code infinitely looping within a critical section will eventually be interrupted.
- Critical sections are interrupted by signals.

"basic_percpu_ops_test" is a slightly more "realistic" variant, implementing a few simple per-cpu operations and testing their correctness.

"param_test" is a parametrizable restartable sequences test. See the "--help" output for usage.

As part of those tests, a helper library "rseq" implements a user-space API around restartable sequences. It uses the cpu_opv system call as fallback when single-stepped by a debugger. It exposes the instruction pointer addresses where the rseq assembly blocks begin and end, as well as the associated abort instruction pointer, in the __rseq_table section. This section allows debuggers may know where to place breakpoints when single-stepping through assembly blocks which may be aborted at any point by the kernel.

The following rseq APIs are implemented in this helper library:
- rseq_register_current_thread()/rseq_unregister_current_thread():
register/unregister current thread's use of rseq,
- rseq_current_cpu_raw():
current CPU number,
- rseq_start():
beginning of a restartable sequence,
- rseq_cpu_at_start():
CPU number at start of restartable sequence,
- rseq_finish():
End of restartable sequence made of zero or more loads, completed by a word-sized store,
- rseq_finish2():
End of restartable sequence made of zero or more loads, one speculative word-sized store, completed by a word-sized store,
- rseq_finish2_release():
End of restartable sequence made of zero or more loads, one speculative word-sized store, completed by a word-sized store with release semantic,
- rseq_finish_memcpy():
End of restartable sequence made of zero or more loads, a speculative copy of a variable length memory region, completed by a word-sized store.
- rseq_finish_memcpy_release():
End of restartable sequence made of zero or more loads, a speculative copy of a variable length memory region, completed by a word-sized store with release semantic.

PowerPC tests have been implemented by Boqun Feng.

CC Russell King <[email protected]>
CC Catalin Marinas <[email protected]>
CC Will Deacon <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Paul Turner <[email protected]>
CC Andrew Hunter <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Andi Kleen <[email protected]>
CC Dave Watson <[email protected]>
CC Chris Lameter <[email protected]>
CC Ingo Molnar <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Ben Maurer <[email protected]>
CC Steven Rostedt <[email protected]>
CC "Paul E. McKenney" <[email protected]>
CC Josh Triplett <[email protected]>
CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Boqun Feng <[email protected]>
CC Shuah Khan <[email protected]>
---
 MAINTAINERS                                        |    1 +
 tools/testing/selftests/rseq/.gitignore            |    4 +
 tools/testing/selftests/rseq/Makefile              |   13 +
 .../testing/selftests/rseq/basic_percpu_ops_test.c |  319 +++++
 tools/testing/selftests/rseq/basic_test.c          |   97 ++
 tools/testing/selftests/rseq/param_test.c          | 1246 ++++++++++++++++++++
 tools/testing/selftests/rseq/rseq-arm.h            |  159 +++
 tools/testing/selftests/rseq/rseq-ppc.h            |  266 +++++
 tools/testing/selftests/rseq/rseq-x86.h            |  304 +++++
 tools/testing/selftests/rseq/rseq.c                |   78 ++
 tools/testing/selftests/rseq/rseq.h                |  298 +++++
 11 files changed, 2785 insertions(+)
 create mode 100644 tools/testing/selftests/rseq/.gitignore
 create mode 100644 tools/testing/selftests/rseq/Makefile
 create mode 100644 tools/testing/selftests/rseq/basic_percpu_ops_test.c
 create mode 100644 tools/testing/selftests/rseq/basic_test.c
 create mode 100644 tools/testing/selftests/rseq/param_test.c
 create mode 100644 tools/testing/selftests/rseq/rseq-arm.h
 create mode 100644 tools/testing/selftests/rseq/rseq-ppc.h
 create mode 100644 tools/testing/selftests/rseq/rseq-x86.h
 create mode 100644 tools/testing/selftests/rseq/rseq.c
 create mode 100644 tools/testing/selftests/rseq/rseq.h
MAINTAINERS +1 -0
index 9134a3234737..a79b0b473e7f 100644
@@ -11242,6 +11242,7 @@ S:	Supported
 F:	kernel/rseq.c
 F:	include/uapi/linux/rseq.h
 F:	include/trace/events/rseq.h
+F:	tools/testing/selftests/rseq/
 
 RFKILL
 M:	Johannes Berg <[email protected]>
tools/testing/selftests/rseq/.gitignore new file +4 -0
new file mode 100644
index 000000000000..9409c3db99b2
@@ -0,0 +1,4 @@
+basic_percpu_ops_test
+basic_test
+basic_rseq_op_test
+param_test
tools/testing/selftests/rseq/Makefile new file +13 -0
new file mode 100644
index 000000000000..7f0153556b80
@@ -0,0 +1,13 @@
+CFLAGS += -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/
+LDFLAGS += -lpthread
+
+TESTS = basic_test basic_percpu_ops_test param_test
+
+all: $(TESTS)
+%: %.c rseq.h rseq-*.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h
+	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
+
+include ../lib.mk
+
+clean:
+	$(RM) $(TESTS)
tools/testing/selftests/rseq/basic_percpu_ops_test.c new file +319 -0

319 lines not on this page · raw message

tools/testing/selftests/rseq/basic_test.c new file +97 -0

97 lines not on this page · raw message

tools/testing/selftests/rseq/param_test.c new file +1246 -0

1,246 lines not on this page · raw message

tools/testing/selftests/rseq/rseq-arm.h new file +159 -0

159 lines not on this page · raw message

tools/testing/selftests/rseq/rseq-ppc.h new file +266 -0

266 lines not on this page · raw message

tools/testing/selftests/rseq/rseq-x86.h new file +304 -0

304 lines not on this page · raw message

tools/testing/selftests/rseq/rseq.c new file +78 -0

78 lines not on this page · raw message

tools/testing/selftests/rseq/rseq.h new file +298 -0

298 lines not on this page · raw message

Signature · 1 line
2.11.0

Mathieu Desnoyers

author maintainer
#0 lore raw

v9 cover Restartable sequences and CPU op vector system calls

Hi,

This v9 series addresses the debugger single-stepping problem for restartable sequences by providing a new cpu_opv "cpu operation vector" system call, and let userspace defer execution of the critical sections to the kernel in case a restartable sequence gets restarted.

This cpu_opv system call is inspired from readv/writev system calls. Instead of a struct io_vec array, it receives a struct cpu_op array as argument.

This is provided as RFC, feedback is welcome!

Thanks,

Mathieu

Boqun Feng (2):
Restartable sequences: powerpc architecture support Restartable sequences: Wire up powerpc system call

Mathieu Desnoyers (12):
Restartable sequences system call (v9)
tracing: instrument restartable sequences Restartable sequences: ARM 32 architecture support Restartable sequences: wire up ARM 32 system call Restartable sequences: x86 32/64 architecture support Restartable sequences: wire up x86 32/64 system call Provide cpu_opv system call
cpu_opv: Wire up x86 32/64 system call
cpu_opv: Wire up powerpc system call
cpu_opv: Wire up ARM32 system call
cpu_opv: Implement selftests
Restartable sequences: Provide self-tests

 MAINTAINERS                                        |   20 +
 arch/Kconfig                                       |    7 +
 arch/arm/Kconfig                                   |    1 +
 arch/arm/kernel/signal.c                           |    7 +
 arch/arm/tools/syscall.tbl                         |    2 +
 arch/powerpc/Kconfig                               |    1 +
 arch/powerpc/include/asm/systbl.h                  |    2 +
 arch/powerpc/include/asm/unistd.h                  |    2 +-
 arch/powerpc/include/uapi/asm/unistd.h             |    2 +
 arch/powerpc/kernel/signal.c                       |    3 +
 arch/x86/Kconfig                                   |    1 +
 arch/x86/entry/common.c                            |    1 +
 arch/x86/entry/syscalls/syscall_32.tbl             |    2 +
 arch/x86/entry/syscalls/syscall_64.tbl             |    2 +
 arch/x86/kernel/signal.c                           |    6 +
 fs/exec.c                                          |    1 +
 include/linux/sched.h                              |   89 ++
 include/trace/events/rseq.h                        |   64 +
 include/uapi/linux/cpu_opv.h                       |   93 ++
 include/uapi/linux/rseq.h                          |  131 ++
 init/Kconfig                                       |   27 +
 kernel/Makefile                                    |    2 +
 kernel/cpu_opv.c                                   | 1000 ++++++++++++++++
 kernel/fork.c                                      |    2 +
 kernel/rseq.c                                      |  354 ++++++
 kernel/sched/core.c                                |   41 +
 kernel/sched/sched.h                               |    2 +
 kernel/sys_ni.c                                    |    4 +
 tools/testing/selftests/cpu-opv/.gitignore         |    1 +
 tools/testing/selftests/cpu-opv/Makefile           |   13 +
 .../testing/selftests/cpu-opv/basic_cpu_opv_test.c |  828 +++++++++++++
 tools/testing/selftests/cpu-opv/cpu-op.c           |  189 +++
 tools/testing/selftests/cpu-opv/cpu-op.h           |   53 +
 tools/testing/selftests/rseq/.gitignore            |    4 +
 tools/testing/selftests/rseq/Makefile              |   13 +
 .../testing/selftests/rseq/basic_percpu_ops_test.c |  319 +++++
 tools/testing/selftests/rseq/basic_test.c          |   97 ++
 tools/testing/selftests/rseq/param_test.c          | 1246 ++++++++++++++++++++
 tools/testing/selftests/rseq/rseq-arm.h            |  159 +++
 tools/testing/selftests/rseq/rseq-ppc.h            |  266 +++++
 tools/testing/selftests/rseq/rseq-x86.h            |  304 +++++
 tools/testing/selftests/rseq/rseq.c                |   78 ++
 tools/testing/selftests/rseq/rseq.h                |  298 +++++
 43 files changed, 5736 insertions(+), 1 deletion(-)
 create mode 100644 include/trace/events/rseq.h
 create mode 100644 include/uapi/linux/cpu_opv.h
 create mode 100644 include/uapi/linux/rseq.h
 create mode 100644 kernel/cpu_opv.c
 create mode 100644 kernel/rseq.c
 create mode 100644 tools/testing/selftests/cpu-opv/.gitignore
 create mode 100644 tools/testing/selftests/cpu-opv/Makefile
 create mode 100644 tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c
 create mode 100644 tools/testing/selftests/cpu-opv/cpu-op.c
 create mode 100644 tools/testing/selftests/cpu-opv/cpu-op.h
 create mode 100644 tools/testing/selftests/rseq/.gitignore
 create mode 100644 tools/testing/selftests/rseq/Makefile
 create mode 100644 tools/testing/selftests/rseq/basic_percpu_ops_test.c
 create mode 100644 tools/testing/selftests/rseq/basic_test.c
 create mode 100644 tools/testing/selftests/rseq/param_test.c
 create mode 100644 tools/testing/selftests/rseq/rseq-arm.h
 create mode 100644 tools/testing/selftests/rseq/rseq-ppc.h
 create mode 100644 tools/testing/selftests/rseq/rseq-x86.h
 create mode 100644 tools/testing/selftests/rseq/rseq.c
 create mode 100644 tools/testing/selftests/rseq/rseq.h
Signature · 1 line
2.11.0
author maintainer
#40 lore raw

RFC PATCH v9 7/14 Restartable sequences: powerpc architecture support

From: Boqun Feng <[email protected]>

Call the rseq_handle_notify_resume() function on return to userspace if TIF_NOTIFY_RESUME thread flag is set.

Increment the event counter and perform fixup on the pre-signal when a signal is delivered on top of a restartable sequence critical section.

CC Benjamin Herrenschmidt <[email protected]>
CC Paul Mackerras <[email protected]>
CC Michael Ellerman <[email protected]>
CC Peter Zijlstra <[email protected]>
CC "Paul E. McKenney" <[email protected]>
---
 arch/powerpc/Kconfig         | 1 +
 arch/powerpc/kernel/signal.c | 3 +++
 2 files changed, 4 insertions(+)
arch/powerpc/Kconfig +1 -0
index 81b0031f909f..816590f4a2eb 100644
@@ -207,6 +207,7 @@ config PPC
 	select HAVE_SYSCALL_TRACEPOINTS
 	select HAVE_VIRT_CPU_ACCOUNTING
 	select HAVE_IRQ_TIME_ACCOUNTING
+	select HAVE_RSEQ
 	select IRQ_DOMAIN
 	select IRQ_FORCED_THREADING
 	select MODULES_USE_ELF_RELA
arch/powerpc/kernel/signal.c +3 -0
index e9436c5e1e09..17a994b801b1 100644
@@ -133,6 +133,8 @@ static void do_signal(struct task_struct *tsk)
 	/* Re-enable the breakpoints for the signal stack */
 	thread_change_pc(tsk, tsk->thread.regs);
 
+	rseq_signal_deliver(tsk->thread.regs);
+
 	if (is32) {
         	if (ksig.ka.sa.sa_flags & SA_SIGINFO)
 			ret = handle_rt_signal32(&ksig, oldset, tsk);
@@ -161,6 +163,7 @@ void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
 	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
 		clear_thread_flag(TIF_NOTIFY_RESUME);
 		tracehook_notify_resume(regs);
+		rseq_handle_notify_resume(regs);
 	}
 
 	if (thread_info_flags & _TIF_PATCH_PENDING)
Signature · 1 line
2.11.0
author maintainer
#50 lore raw

RFC PATCH v9 11/14 cpu_opv: Wire up powerpc system call

In reply to Mathieu Desnoyers
CC Benjamin Herrenschmidt <[email protected]>
CC Paul Mackerras <[email protected]>
CC Michael Ellerman <[email protected]>
CC Boqun Feng <[email protected]>
CC Peter Zijlstra <[email protected]>
CC "Paul E. McKenney" <[email protected]>
---
 arch/powerpc/include/asm/systbl.h      | 1 +
 arch/powerpc/include/asm/unistd.h      | 2 +-
 arch/powerpc/include/uapi/asm/unistd.h | 1 +
 3 files changed, 3 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/systbl.h +1 -0
index a805a4d10d1c..2846dfded7fe 100644
@@ -389,3 +389,4 @@ COMPAT_SYS_SPU(pwritev2)
 SYSCALL(kexec_file_load)
 SYSCALL(statx)
 SYSCALL(rseq)
+SYSCALL(cpu_opv)
arch/powerpc/include/asm/unistd.h +1 -1
index e76bd5601ea4..48f80f452e31 100644
@@ -12,7 +12,7 @@
 #include <uapi/asm/unistd.h>
 
 
-#define NR_syscalls		385
+#define NR_syscalls		386
 
 #define __NR__exit __NR_exit
 
arch/powerpc/include/uapi/asm/unistd.h +1 -0
index 854a8db4144e..c739299b5279 100644
@@ -395,5 +395,6 @@
 #define __NR_kexec_file_load	382
 #define __NR_statx		383
 #define __NR_rseq		384
+#define __NR_cpu_opv		385
 
 #endif /* _UAPI_ASM_POWERPC_UNISTD_H_ */
Signature · 1 line
2.11.0
author maintainer
#41 lore raw

RFC PATCH v9 8/14 Restartable sequences: Wire up powerpc system call

In reply to Mathieu Desnoyers

From: Boqun Feng <[email protected]>

Wire up the rseq system call on powerpc.

This provides an ABI improving the speed of a user-space getcpu operation on powerpc by skipping the getcpu system call on the fast path, as well as improving the speed of user-space operations on per-cpu data compared to using load-reservation/store-conditional atomics.

CC Benjamin Herrenschmidt <[email protected]>
CC Paul Mackerras <[email protected]>
CC Michael Ellerman <[email protected]>
CC Peter Zijlstra <[email protected]>
CC "Paul E. McKenney" <[email protected]>
---
 arch/powerpc/include/asm/systbl.h      | 1 +
 arch/powerpc/include/asm/unistd.h      | 2 +-
 arch/powerpc/include/uapi/asm/unistd.h | 1 +
 3 files changed, 3 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/systbl.h +1 -0
index 1c9470881c4a..a805a4d10d1c 100644
@@ -388,3 +388,4 @@ COMPAT_SYS_SPU(preadv2)
 COMPAT_SYS_SPU(pwritev2)
 SYSCALL(kexec_file_load)
 SYSCALL(statx)
+SYSCALL(rseq)
arch/powerpc/include/asm/unistd.h +1 -1
index 9ba11dbcaca9..e76bd5601ea4 100644
@@ -12,7 +12,7 @@
 #include <uapi/asm/unistd.h>
 
 
-#define NR_syscalls		384
+#define NR_syscalls		385
 
 #define __NR__exit __NR_exit
 
arch/powerpc/include/uapi/asm/unistd.h +1 -0
index b85f14228857..854a8db4144e 100644
@@ -394,5 +394,6 @@
 #define __NR_pwritev2		381
 #define __NR_kexec_file_load	382
 #define __NR_statx		383
+#define __NR_rseq		384
 
 #endif /* _UAPI_ASM_POWERPC_UNISTD_H_ */
Signature · 1 line
2.11.0
#2 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

In reply to Mathieu Desnoyers

I do not hate this series, and I'd be happy to apply it, but I will repeat what I've asked for EVERY SINGLE TIME this series has come up:

Quoted 4 lines · Mathieu Desnoyers

Here are benchmarks of counter increment in various scenarios compared to restartable sequences. Those benchmarks were taken on v8 of the patchset.

I want to see real numbers from real issues.

A "increment percpu value" simply isn't relevant.

When I asked last time, people pointed me to potential uses, including malloc libraries that could get per-thread performance with just per-cpu (not per thread) data structure overhead. I see that you once more point to the slides from 2013 that again talks about it.

But people didn't post code, people didn't post numbers, and people didn't point to actual real uses, just "this could happen".

I really really want more than hand-waving. I want more than pointless "this is how quickly you can increment a per-thread counter". I want to hear about _real_ uses, and real numbers.

This has been going on for long enough, that if there *still* are no actual real users, then I'm *still* not interested in having this merged.

Because without real-world uses, it's not obvious that there won't be somebody who goes "oh, this isn't quite enough for us, the semantics are subtly incompatible with our real-world use case".

So I want real numbers from a real implementation of malloc/free. And if it's not malloc/free, then what is it? I want something *real*, not some micro-benchmark that benchmarks a totally pointless load.

Because until there are that kind of "yes, this is more than theory", I'm not really willing to have this merged.

Linus

#3 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Hey,

I'm really excited to hear that you're open to this patch set and totally understand the desire for some more numbers. I have a few thoughts and questions -- hopefully ones that could help better understand where you'd like to see more data (potentially from myself and other Facebook folks)

Quoted 1 line

A "increment percpu value" simply isn't relevant.

While I understand it seems trivial, my experience has been that this type of operation can actually be important in many server workloads. In applications with 1000s of threads, keeping a counter like this can pose a challenge. One can use a per-thread variable, but the size overhead here can be very large (8 bytes per counter per thread adds up very quickly). And atomic instructions can cause contention quickly. Server programs tend to have many statistical counters, being able to implement them efficiently without size bloat is a real world win.

This type of per-cpu counter can also very quickly be used to implement other abstractions in common use -- eg an asymmetric reader-writer lock or a reference counted object that is changed infrequently. While thread local storage can also be used in these cases this can be a substantial size overhead and can also require cooperation between the application and the library to manage thread lifecycle.

At least from what I've seen of our usage of these types of abstractions within Facebook, if rseq met these use cases and did absolutely nothing else it would still be a feature that our applications would benefit from. Hopefully we can find evidence that it can do even more than this, but I think that this "trivial" use case is actually addressing a real world problem.

Quoted 7 lines

When I asked last time, people pointed me to potential uses, including malloc libraries that could get per-thread performance with just per-cpu (not per thread) data structure overhead. I see that you once more point to the slides from 2013 that again talks about it.

But people didn't post code, people didn't post numbers, and people didn't point to actual real uses, just "this could happen".

At Facebook we did some work to experiment with rseq and jemalloc Qi and David (cc'd) may be able to provide more context on the current state.

Quoted 3 lines

Because without real-world uses, it's not obvious that there won't be somebody who goes "oh, this isn't quite enough for us, the semantics are subtly incompatible with our real-world use case".

Is your concern mainly this question (is this patchset a good way to bring per-cpu algorithms to userspace)? I'm hoping that given the variety of ways that per-cpu data structures are used in the kernel the concerns around this patch set are mainly around what approach we should take rather than if per-cpu algorithms are a good idea at all. If this is your main concern perhaps our focus should be around demonstrating that a number of useful per-cpu algorithms can be implemented using restartable sequences.

Ultimately I'm worried there's a chicken and egg problem here. It's hard to get people to commit to investing in rseq without a clear path to the patchset seeing the light of day. It's also easy to understand why you'd be reluctant to merge such a substantial and unfamiliar API without extensive data. If we're still not able to get compelling data, I'm wondering if there are other approaches that could get us unstuck, eg

(1) Could we merge enough of this patchset (eg things like the faster getcpu() operation, which seems like a good improvement over the current approach). If we make the remaining patches small enough it may be easier for sophisticated users to apply the remaining patches, maintain them, and provide real-world operational experience with this abstraction.

(2) Could we implement restartable sequences in the kernel but only allow the vdso to make use of them? We could have the vdso export a number of high-level operations (like the ones suggested in Paul Turner's original presentation -- per-cpu spin lock, per-cpu atomic increment/decrement, per-cpu list push/pop). This would allow us to get real-world data about how these primitives are used without committing to a complex ABI -- only committing to support the specific operations. If the whole idea flops we could eventually create a slow/naive implementation of the vdso functions and kill restartable sequences entirely.

-b

#21 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

In reply to Mathieu Desnoyers
Quoted 10 lines · Mathieu Desnoyers

Expose a new system call allowing each thread to register one userspace memory area to be used as an ABI between kernel and user-space for two
purposes: user-space restartable sequences and quick access to read the current CPU number value from user-space.

* Restartable sequences (per-cpu atomics)

Restartables sequences allow user-space to perform update operations on per-cpu data without requiring heavy-weight atomic operations.

Quoted 9 lines

The restartable critical sections (percpu atomics) work has been started by Paul Turner and Andrew Hunter. It lets the kernel handle restart of critical sections. [1] [2] The re-implementation proposed here brings a few simplifications to the ABI which facilitates porting to other architectures and speeds up the user-space fast path. A locking-based fall-back, purely implemented in user-space, is proposed here to deal with debugger single-stepping. This fallback interacts with rseq_start() and rseq_finish(), which force retries in response to concurrent lock-based activity.

This functionality essentially relies on writable function pointers (or pointers to data containing function pointers), right? Is there a way to make this a less attractive target for exploit writers?

Thanks,
Florian

author maintainer
#22 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Quoted 8 lines · ----- On Oct 13, 2017, at 8:50 AM, Florian Weimer [email protected] wrote:
Quoted 7 lines · Mathieu Desnoyers

Expose a new system call allowing each thread to register one userspace memory area to be used as an ABI between kernel and user-space for two
purposes: user-space restartable sequences and quick access to read the current CPU number value from user-space.

* Restartable sequences (per-cpu atomics)

Quoted 8 lines
Quoted 8 lines

Restartables sequences allow user-space to perform update operations on per-cpu data without requiring heavy-weight atomic operations.

The restartable critical sections (percpu atomics) work has been started by Paul Turner and Andrew Hunter. It lets the kernel handle restart of critical sections. [1] [2] The re-implementation proposed here brings a few simplifications to the ABI which facilitates porting to other architectures and speeds up the user-space fast path.

This part:

Quoted 5 lines
Quoted 5 lines

A locking-based
fall-back, purely implemented in user-space, is proposed here to deal with debugger single-stepping. This fallback interacts with rseq_start() and rseq_finish(), which force retries in response to concurrent lock-based activity.

should have been updated in this series to:

A second system call, cpu_opv(), is proposed as fallback to deal with debugger single-stepping. cpu_opv() executes a sequence of operations on behalf of user-space with preemption disabled.

Quoted 3 lines

This functionality essentially relies on writable function pointers (or pointers to data containing function pointers), right? Is there a way to make this a less attractive target for exploit writers?

The proposed ABI does not require to store any function pointer. For a given rseq_finish() critical section, pointers to specific instructions (within a function) are emitted at link-time into a struct rseq_cs:

struct rseq_cs {
        RSEQ_FIELD_u32_u64(start_ip);
        RSEQ_FIELD_u32_u64(post_commit_ip);
        RSEQ_FIELD_u32_u64(abort_ip);
        uint32_t flags;
} __attribute__((aligned(4 * sizeof(uint64_t))));

Then, at runtime, the fast-path stores the address of that struct rseq_cs into the TLS struct rseq "rseq_cs" field.

So all we store at runtime is a pointer to data, not a pointer to functions.

But you seem to hint that having a pointer to data containing pointers to code may still be making it easier for exploit writers. Can you elaborate on the scenario ?

Thanks,

Mathieu

Quoted 3 lines

Thanks,
Florian

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#23 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Quoted 11 lines · Mathieu Desnoyers

The proposed ABI does not require to store any function pointer. For a given rseq_finish() critical section, pointers to specific instructions (within a function) are emitted at link-time into a struct rseq_cs:

struct rseq_cs {
         RSEQ_FIELD_u32_u64(start_ip);
         RSEQ_FIELD_u32_u64(post_commit_ip);
         RSEQ_FIELD_u32_u64(abort_ip);
         uint32_t flags;
} __attribute__((aligned(4 * sizeof(uint64_t))));
Quoted 8 lines

Then, at runtime, the fast-path stores the address of that struct rseq_cs into the TLS struct rseq "rseq_cs" field.

So all we store at runtime is a pointer to data, not a pointer to functions.

But you seem to hint that having a pointer to data containing pointers to code may still be making it easier for exploit writers. Can you elaborate on the scenario ?

I'm concerned that the exploit writer writes a totally made up struct rseq_cs object into writable memory, along with function pointers, and puts the address of that in to the rseq_cs field.

This would be comparable to how C++ vtable pointers are targeted (including those in the glibc libio implementation of stdio streams).

Does this answer your questions?

Thanks,
Florian

#43 lore raw

Re: [RFC PATCH for 4.15 09/14] Provide cpu_opv system call

In reply to Mathieu Desnoyers
Quoted 3 lines

A maximum limit of 16 operations per cpu_opv syscall invocation is enforced, so user-space cannot generate a too long preempt-off critical section.

Except that all the operations could be going to mmapped I/O space and if I pick the right targets could take quite a long time to complete. It's still only 16 operations - But 160ms is a lot worse than 10ms. In fact with compare_iter I could make it much much worse still as I get 2 x TMP_BUFLEN x 16 x worst case latency in my attack. That's enough to screw up plenty of things.

So it seems to me at minimum it needs to be restricted to genuine RAM user pages, and in fact would be far far simpler code as well if it were limited to a single page for a given invocation or if like futexes you had to specifically create a per_cpu_opv mapping.

Alan

author maintainer
#24 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

In reply to Florian Weimer
Quoted 21 lines · ----- On Oct 13, 2017, at 9:56 AM, Florian Weimer [email protected] wrote:
Quoted 11 lines · Mathieu Desnoyers

The proposed ABI does not require to store any function pointer. For a given rseq_finish() critical section, pointers to specific instructions (within a function) are emitted at link-time into a struct rseq_cs:

struct rseq_cs {
         RSEQ_FIELD_u32_u64(start_ip);
         RSEQ_FIELD_u32_u64(post_commit_ip);
         RSEQ_FIELD_u32_u64(abort_ip);
         uint32_t flags;
} __attribute__((aligned(4 * sizeof(uint64_t))));
Quoted 8 lines

Then, at runtime, the fast-path stores the address of that struct rseq_cs into the TLS struct rseq "rseq_cs" field.

So all we store at runtime is a pointer to data, not a pointer to functions.

But you seem to hint that having a pointer to data containing pointers to code may still be making it easier for exploit writers. Can you elaborate on the scenario ?

Quoted 8 lines

I'm concerned that the exploit writer writes a totally made up struct rseq_cs object into writable memory, along with function pointers, and puts the address of that in to the rseq_cs field.

This would be comparable to how C++ vtable pointers are targeted (including those in the glibc libio implementation of stdio streams).

Does this answer your questions?

Yes, it does. How about we add a "canary" field to the TLS struct rseq, e.g.:

struct rseq {
        union rseq_cpu_event u;
        RSEQ_FIELD_u32_u64(rseq_cs);  -> pointer to struct rseq_cs
        uint32_t flags;
        uint32_t canary;   -> 32 low bits of rseq_cs ^ canary_mask
};

We could then add a "uint32_t canary_mask" argument to sys_rseq, e.g.:

SYSCALL_DEFINE3(rseq, struct rseq __user *, rseq, uint32_t, canary_mask, int, flags);

So a thread which does not care about hardening would simply register its struct rseq TLS with a canary mask of "0". Nothing changes on the fast-path.

A thread belonging to a process that cares about hardening could use a random value as canary, and pass it as canary_mask argument to the syscall. The fast-path could then set the struct rseq "canary" value to (32-low-bits of rseq_cs) ^ canary_mask just surrounding the critical section, and set it back to 0 afterward.

In the kernel, whenever the rseq_cs pointer would be loaded, its 32 low bits would be checked to match (canary ^ canary_mask). If it differs, then the kernel kills the process with SIGSEGV.

Would that take care of your concern ?

Thanks,

Mathieu

Quoted 3 lines

Thanks,
Florian

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
author maintainer
#44 lore raw

Re: [RFC PATCH for 4.15 09/14] Provide cpu_opv system call

In reply to Alan Cox
Quoted 6 lines · ----- On Oct 13, 2017, at 9:57 AM, One Thousand Gnomes…
Quoted 3 lines

A maximum limit of 16 operations per cpu_opv syscall invocation is enforced, so user-space cannot generate a too long preempt-off critical section.

Except that all the operations could be going to mmapped I/O space and if I pick the right targets could take quite a long time to complete.

We could check whether a struct page belongs to mmapped I/O space, and return EINVAL in that case.

Quoted 5 lines

It's
still only 16 operations - But 160ms is a lot worse than 10ms. In fact with compare_iter I could make it much much worse still as I get 2 x TMP_BUFLEN x 16 x worst case latency in my attack. That's enough to screw up plenty of things.

Would a check that ensures the page is not mmapped I/O space be sufficient to take care of this ? If happen to know which API I need to look for, it would be welcome.

Quoted 5 lines

So it seems to me at minimum it needs to be restricted to genuine RAM user pages, and in fact would be far far simpler code as well if it were limited to a single page for a given invocation or if like futexes you had to specifically create a per_cpu_opv mapping.

I've had requests to implement per-cpu ring buffers with memcpy + offset pointer update restartable sequences. Having a memcpy operation which does not require page-alignment allows cpu_opv() to be used as a single-stepping fallback for those use-cases.

I'm open to consider simplifying the other operations such as compare, add, bitwise ops, and shift ops by requiring that they target aligned content, which would therefore fit within a single page. However, given that we already want to support the unaligned memcpy operation, it does not add much extra complexity to support unaligned accesses for the other cases. We could also limit the "compare" operation to 1, 2, 4, 8 aligned bytes rather than being an up-to-PAGE_SIZE compare, but it would limit its usefulness in case of structure content comparison.

Thanks,

Mathieu

Quoted 2 lines

Alan

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#46 lore raw

Re: [RFC PATCH for 4.15 09/14] Provide cpu_opv system call

In reply to Mathieu Desnoyers
Quoted 29 lines · Mathieu Desnoyers

This new cpu_opv system call executes a vector of operations on behalf of user-space on a specific CPU with preemption disabled. It is inspired from readv() and writev() system calls which take a "struct iovec" array as argument.

The operations available are: comparison, memcpy, add, or, and, xor, left shift, and right shift. The system call receives a CPU number from user-space as argument, which is the CPU on which those operations need to be performed. All preparation steps such as loading pointers, and applying offsets to arrays, need to be performed by user-space before invoking the system call. The "comparison" operation can be used to check that the data used in the preparation step did not change between preparation of system call inputs and operation execution within the preempt-off critical section.

The reason why we require all pointer offsets to be calculated by user-space beforehand is because we need to use get_user_pages_fast() to first pin all pages touched by each operation. This takes care of faulting-in the pages. Then, preemption is disabled, and the operations are performed atomically with respect to other thread execution on that CPU, without generating any page fault.

A maximum limit of 16 operations per cpu_opv syscall invocation is enforced, so user-space cannot generate a too long preempt-off critical section. Each operation is also limited a length of PAGE_SIZE bytes, meaning that an operation can touch a maximum of 4 pages (memcpy: 2 pages for source, 2 pages for destination if addresses are not aligned on page boundaries).

Quoted 10 lines

If the thread is not running on the requested CPU, a new push_task_to_cpu() is invoked to migrate the task to the requested CPU. If the requested CPU is not part of the cpus allowed mask of the thread, the system call fails with EINVAL. After the migration has been performed, preemption is disabled, and the current CPU number is checked again and compared to the requested CPU number. If it still differs, it means the scheduler migrated us away from that CPU. Return EAGAIN to user-space in that case, and let user-space retry (either requesting the same CPU number, or a different one, depending on the user-space algorithm constraints).

This series seems to get more complicated every time, and it's been so long that I've mostly forgetten all the details. I would have sworn we had a solution that got single-stepping right without any complicated work like this in the kernel and had at most a minor performance hit relative to the absolutely fastest solution. I'll try to dig it up.

#25 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

In reply to Mathieu Desnoyers
Quoted 54 lines · Mathieu Desnoyers
Quoted 21 lines · ----- On Oct 13, 2017, at 9:56 AM, Florian Weimer [email protected] wrote:
Quoted 11 lines · Mathieu Desnoyers

The proposed ABI does not require to store any function pointer. For a given rseq_finish() critical section, pointers to specific instructions (within a function) are emitted at link-time into a struct rseq_cs:

struct rseq_cs {
         RSEQ_FIELD_u32_u64(start_ip);
         RSEQ_FIELD_u32_u64(post_commit_ip);
         RSEQ_FIELD_u32_u64(abort_ip);
         uint32_t flags;
} __attribute__((aligned(4 * sizeof(uint64_t))));
Quoted 8 lines

Then, at runtime, the fast-path stores the address of that struct rseq_cs into the TLS struct rseq "rseq_cs" field.

So all we store at runtime is a pointer to data, not a pointer to functions.

But you seem to hint that having a pointer to data containing pointers to code may still be making it easier for exploit writers. Can you elaborate on the scenario ?

Quoted 8 lines

I'm concerned that the exploit writer writes a totally made up struct rseq_cs object into writable memory, along with function pointers, and puts the address of that in to the rseq_cs field.

This would be comparable to how C++ vtable pointers are targeted (including those in the glibc libio implementation of stdio streams).

Does this answer your questions?

Yes, it does. How about we add a "canary" field to the TLS struct rseq, e.g.:

struct rseq {
        union rseq_cpu_event u;
        RSEQ_FIELD_u32_u64(rseq_cs);  -> pointer to struct rseq_cs
        uint32_t flags;
        uint32_t canary;   -> 32 low bits of rseq_cs ^ canary_mask
};

We could then add a "uint32_t canary_mask" argument to sys_rseq, e.g.:

SYSCALL_DEFINE3(rseq, struct rseq __user *, rseq, uint32_t, canary_mask, int, flags);

So a thread which does not care about hardening would simply register its struct rseq TLS with a canary mask of "0". Nothing changes on the fast-path.

A thread belonging to a process that cares about hardening could use a random value as canary, and pass it as canary_mask argument to the syscall. The fast-path could then set the struct rseq "canary" value to (32-low-bits of rseq_cs) ^ canary_mask just surrounding the critical section, and set it back to 0 afterward.

Quoted 6 lines

In the kernel, whenever the rseq_cs pointer would be loaded, its 32 low bits would be checked to match (canary ^ canary_mask). If it differs, then the kernel kills the process with SIGSEGV.

Would that take care of your concern ?

I would propose a slightly different solution: have the kernel verify that it jumps to a code sequence that occurs just after some highly-unlikely magic bytes in the text *and* that those bytes have some signature that matches a signature in the struct rseq that's passed in.

#26 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Quoted 60 lines · Andy Lutomirski
Quoted 58 lines · Mathieu Desnoyers
Quoted 21 lines · ----- On Oct 13, 2017, at 9:56 AM, Florian Weimer…
Quoted 11 lines · Mathieu Desnoyers

The proposed ABI does not require to store any function pointer. For a given rseq_finish() critical section, pointers to specific instructions (within a function) are emitted at link-time into a struct rseq_cs:

struct rseq_cs {
          RSEQ_FIELD_u32_u64(start_ip);
          RSEQ_FIELD_u32_u64(post_commit_ip);
          RSEQ_FIELD_u32_u64(abort_ip);
          uint32_t flags;
} __attribute__((aligned(4 * sizeof(uint64_t))));
Quoted 8 lines

Then, at runtime, the fast-path stores the address of that struct rseq_cs into the TLS struct rseq "rseq_cs" field.

So all we store at runtime is a pointer to data, not a pointer to functions.

But you seem to hint that having a pointer to data containing pointers to code may still be making it easier for exploit writers. Can you elaborate on the scenario ?

Quoted 8 lines

I'm concerned that the exploit writer writes a totally made up struct rseq_cs object into writable memory, along with function pointers, and puts the address of that in to the rseq_cs field.

This would be comparable to how C++ vtable pointers are targeted (including those in the glibc libio implementation of stdio streams).

Does this answer your questions?

Yes, it does. How about we add a "canary" field to the TLS struct rseq, e.g.:

struct rseq {
         union rseq_cpu_event u;
         RSEQ_FIELD_u32_u64(rseq_cs);  -> pointer to struct rseq_cs
         uint32_t flags;
         uint32_t canary;   -> 32 low bits of rseq_cs ^ canary_mask
};

We could then add a "uint32_t canary_mask" argument to sys_rseq, e.g.:

SYSCALL_DEFINE3(rseq, struct rseq __user *, rseq, uint32_t, canary_mask, int, flags);

So a thread which does not care about hardening would simply register its struct rseq TLS with a canary mask of "0". Nothing changes on the fast-path.

A thread belonging to a process that cares about hardening could use a random value as canary, and pass it as canary_mask argument to the syscall. The fast-path could then set the struct rseq "canary" value to (32-low-bits of rseq_cs) ^ canary_mask just surrounding the critical section, and set it back to 0 afterward.

In the kernel, whenever the rseq_cs pointer would be loaded, its 32 low bits would be checked to match (canary ^ canary_mask). If it differs, then the kernel kills the process with SIGSEGV.

Quoted 8 lines
Quoted 2 lines

Would that take care of your concern ?

I would propose a slightly different solution: have the kernel verify that it jumps to a code sequence that occurs just after some highly-unlikely magic bytes in the text *and* that those bytes have some signature that matches a signature in the struct rseq that's passed in.

And the signature is fixed at the time of the rseq syscall?

Yes, that would be far more reliable.

Thanks,
Florian

#27 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Quoted 82 lines · Florian Weimer
Quoted 81 lines · Andy Lutomirski
Quoted 77 lines · Mathieu Desnoyers

----- On Oct 13, 2017, at 9:56 AM, Florian Weimer [email protected]

Quoted 29 lines · wrote:
Quoted 18 lines · Mathieu Desnoyers

The proposed ABI does not require to store any function pointer. For a given
rseq_finish() critical section, pointers to specific instructions (within a
function) are emitted at link-time into a struct rseq_cs:

struct rseq_cs {
          RSEQ_FIELD_u32_u64(start_ip);
          RSEQ_FIELD_u32_u64(post_commit_ip);
          RSEQ_FIELD_u32_u64(abort_ip);
          uint32_t flags;
} __attribute__((aligned(4 * sizeof(uint64_t))));

Then, at runtime, the fast-path stores the address of that struct rseq_cs
into the TLS struct rseq "rseq_cs" field.

Quoted 8 lines

So all we store at runtime is a pointer to data, not a pointer to functions.

But you seem to hint that having a pointer to data containing pointers to code
may still be making it easier for exploit writers. Can you elaborate on the
scenario ?

Quoted 8 lines

I'm concerned that the exploit writer writes a totally made up struct rseq_cs object into writable memory, along with function pointers, and puts the address of that in to the rseq_cs field.

This would be comparable to how C++ vtable pointers are targeted (including those in the glibc libio implementation of stdio streams).

Does this answer your questions?

Yes, it does. How about we add a "canary" field to the TLS struct rseq, e.g.:

struct rseq {
         union rseq_cpu_event u;
         RSEQ_FIELD_u32_u64(rseq_cs);  -> pointer to struct rseq_cs
         uint32_t flags;
         uint32_t canary;   -> 32 low bits of rseq_cs ^ canary_mask
};

We could then add a "uint32_t canary_mask" argument to sys_rseq, e.g.:

SYSCALL_DEFINE3(rseq, struct rseq __user *, rseq, uint32_t, canary_mask, int, flags);

So a thread which does not care about hardening would simply register its struct rseq TLS with a canary mask of "0". Nothing changes on the fast-path.

A thread belonging to a process that cares about hardening could use a random
value as canary, and pass it as canary_mask argument to the syscall. The fast-path could then set the struct rseq "canary" value to (32-low-bits of rseq_cs) ^ canary_mask just surrounding the critical section,
and set it back to 0 afterward.

In the kernel, whenever the rseq_cs pointer would be loaded, its 32 low bits
would be checked to match (canary ^ canary_mask). If it differs, then the kernel kills the process with SIGSEGV.

Would that take care of your concern ?

Quoted 8 lines
Quoted 5 lines

I would propose a slightly different solution: have the kernel verify that it jumps to a code sequence that occurs just after some highly-unlikely magic bytes in the text *and* that those bytes have some signature that matches a signature in the struct rseq that's passed in.

And the signature is fixed at the time of the rseq syscall?

The point of the signature is to prevent an rseq landing pad from being used out of context. Actually getting the details right might be tricky.

Quoted 5 lines

Yes, that would be far more reliable.

Thanks,
Florian

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
#4 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

In reply to Ben Maurer
Quoted 2 lines · Ben Maurer

I'm really excited to hear that you're open to this patch set and totally understand the desire for some more numbers.

So the patch-set actually looks very reasonable today. I looked through it (ok, I wasn't cc'd on the ppc-only patches so I didn't look at those, but I don't think they are likely objectionable either), and everything looked fine from a patch standpoint.

But it's not _just_ numbers for real loads I'm looking for, it's actually an _existence proof_ for a real load too. I'd like to know that the suggested interface _really_ works in practice too for all the expected users.

In particular, it's easy to make test-cases to show basic functionality, but that does not necessarily show that the interface then works in "real life".

For example, if this is supposed to work for a malloc library, it's important that people show that yes, this can really work in a *LIBRARY*.

That sounds so obvious and stupid that you might go "What do you mean?", but for things to work for libraries, they have to work together with *other* users, and with *independent* users.

For example, say that you're some runtime that wants to use the percpu thing for percpu counters - because you want to avoid cache ping-pong, and you want to avoid per-thread allocation overhead (or per-thread scaling for just summing up the counters) when you have potentially tens of thousands of threads.

Now, how does this runtime work *together* with

- CPU hotplug adding new cpu's while you are running (and after you
allocated your percpu areas)

- libraries and system admins that limit - or extend - you to a
certain set of CPUs

- another library (like the malloc library) that wants to use the
same interface for its percpu allocation queues.

maybe all of this "just works", but I really want to see an existence proof. Not just a "dedicated use of the interface for one benchmark".

So yes, I want to see numbers, but I really want to see something much more fundamental. I want to feel like there is a good reason to believe that the interface really is sufficient and that it really does work, even when a single thread may have multiple *different* uses for this. Statistics, memory allocation queues, RCU, per-cpu locking, yadda yadda. All these things may want to use this, but they want to use it *together*, and without you having to write special code where every user needs to know about every other user statically.

Can you load two different *dynamic* libraries that each independently uses this thing for their own use, without having to be built together for each other?

Quoted 3 lines
Quoted 1 line

A "increment percpu value" simply isn't relevant.

While I understand it seems trivial, my experience has been that this type of operation can actually be important in many server workloads.

Oh, I'm not saying that it's not relevant to have high-performance statistics gathering using percpu data structures. Of _course_ that is important, we do that very much in the kernel itself.

But a benchmark that does nothing else really isn't relevant. If the *only* thing somebody uses this for is statistics, it's simply not good enough.

Quoted 4 lines
Quoted 3 lines

Because without real-world uses, it's not obvious that there won't be somebody who goes "oh, this isn't quite enough for us, the semantics are subtly incompatible with our real-world use case".

Quoted 8 lines

Is your concern mainly this question (is this patchset a good way to bring per-cpu algorithms to userspace)? I'm hoping that given the variety of ways that per-cpu data structures are used in the kernel the concerns around this patch set are mainly around what approach we should take rather than if per-cpu algorithms are a good idea at all. If this is your main concern perhaps our focus should be around demonstrating that a number of useful per-cpu algorithms can be implemented using restartable sequences.

The important thing for me is that it should demonstrate that you can have users co-exists, and that the interface is sufficient for that.

So I do want to see "just numbers" in the sense that I would want to see that people have actually written code that takes advantage of the percpu nature to do real things (like an allocator). But more than that, I want to see *use*.

Quoted 1 line

Ultimately I'm worried there's a chicken and egg problem here.

This patch-set has been around for *years* in some form. It's improved over the years, but the basic approaches are not new.

Honestly, if people still don't have any actual user-level code that really _uses_ this, I'm not interested in merging it.

There's no chicken-and-egg here. Anybody who wants to push this patch-set needs to write the user level code to validate that the patch-set makes sense. That's not chicken-and-egg, that's just "without the user-space code, the kernel code has never been tested, validated or used".

And if nobody can be bothered to write the user-level code and test this patch-series, then obviously it's not important enough for the kernel to merge it.

Linus

#5 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Quoted 107 lines · Linus Torvalds
Quoted 2 lines · Ben Maurer

I'm really excited to hear that you're open to this patch set and totally understand the desire for some more numbers.

So the patch-set actually looks very reasonable today. I looked through it (ok, I wasn't cc'd on the ppc-only patches so I didn't look at those, but I don't think they are likely objectionable either), and everything looked fine from a patch standpoint.

But it's not _just_ numbers for real loads I'm looking for, it's actually an _existence proof_ for a real load too. I'd like to know that the suggested interface _really_ works in practice too for all the expected users.

In particular, it's easy to make test-cases to show basic functionality, but that does not necessarily show that the interface then works in "real life".

For example, if this is supposed to work for a malloc library, it's important that people show that yes, this can really work in a *LIBRARY*.

That sounds so obvious and stupid that you might go "What do you mean?", but for things to work for libraries, they have to work together with *other* users, and with *independent* users.

For example, say that you're some runtime that wants to use the percpu thing for percpu counters - because you want to avoid cache ping-pong, and you want to avoid per-thread allocation overhead (or per-thread scaling for just summing up the counters) when you have potentially tens of thousands of threads.

Now, how does this runtime work *together* with

- CPU hotplug adding new cpu's while you are running (and after you
allocated your percpu areas)

- libraries and system admins that limit - or extend - you to a
certain set of CPUs

- another library (like the malloc library) that wants to use the
same interface for its percpu allocation queues.

maybe all of this "just works", but I really want to see an existence proof. Not just a "dedicated use of the interface for one benchmark".

So yes, I want to see numbers, but I really want to see something much more fundamental. I want to feel like there is a good reason to believe that the interface really is sufficient and that it really does work, even when a single thread may have multiple *different* uses for this. Statistics, memory allocation queues, RCU, per-cpu locking, yadda yadda. All these things may want to use this, but they want to use it *together*, and without you having to write special code where every user needs to know about every other user statically.

Can you load two different *dynamic* libraries that each independently uses this thing for their own use, without having to be built together for each other?

Quoted 3 lines
Quoted 1 line

A "increment percpu value" simply isn't relevant.

While I understand it seems trivial, my experience has been that this type of operation can actually be important in many server workloads.

Oh, I'm not saying that it's not relevant to have high-performance statistics gathering using percpu data structures. Of _course_ that is important, we do that very much in the kernel itself.

But a benchmark that does nothing else really isn't relevant. If the *only* thing somebody uses this for is statistics, it's simply not good enough.

Quoted 4 lines
Quoted 3 lines

Because without real-world uses, it's not obvious that there won't be somebody who goes "oh, this isn't quite enough for us, the semantics are subtly incompatible with our real-world use case".

Quoted 8 lines

Is your concern mainly this question (is this patchset a good way to bring per-cpu algorithms to userspace)? I'm hoping that given the variety of ways that per-cpu data structures are used in the kernel the concerns around this patch set are mainly around what approach we should take rather than if per-cpu algorithms are a good idea at all. If this is your main concern perhaps our focus should be around demonstrating that a number of useful per-cpu algorithms can be implemented using restartable sequences.

The important thing for me is that it should demonstrate that you can have users co-exists, and that the interface is sufficient for that.

So I do want to see "just numbers" in the sense that I would want to see that people have actually written code that takes advantage of the percpu nature to do real things (like an allocator). But more than that, I want to see *use*.

Quoted 1 line

Ultimately I'm worried there's a chicken and egg problem here.

This patch-set has been around for *years* in some form. It's improved over the years, but the basic approaches are not new.

Honestly, if people still don't have any actual user-level code that really _uses_ this, I'm not interested in merging it.

There's no chicken-and-egg here. Anybody who wants to push this patch-set needs to write the user level code to validate that the patch-set makes sense. That's not chicken-and-egg, that's just "without the user-space code, the kernel code has never been tested, validated or used".

Quoted 3 lines

And if nobody can be bothered to write the user-level code and test this patch-series, then obviously it's not important enough for the kernel to merge it.

My guess is that it will take some time, probably measured in months, to carry out this level of integration and testing to. But agreed, it is needed -- as you know, I recently removed some code from RCU that was requested but then never used. Not fun. Even worse would be a case where the requested code was half-used in a inefficient way, but precluding improvements. And we actually had that problem some years back with the userspace-accessible ring-buffer code.

So if it would help the people doing the testing, I would be happy to maintain a out-of-tree repository for this series. That way, if the testing showed that kernel-code changes were required, these changes could be easily made without worrying about backwards compatibility (you don't get the backwards-compatibility guarantee until the code hits mainline). This repository would be similar in some ways to the -rt tree, however, given the small size of this patchset, I cannot justify a separate git tree. My thought is to provide (for example) v4.14-rc4-rseq tags within my -rcu tree.

If there are problems with this approach, or if someone has a better idea, please let me know.

Thanx, Paul

#6 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Quoted 7 lines · Paul E. McKenney
Quoted 4 lines

And if nobody can be bothered to write the user-level code and test this patch-series, then obviously it's not important enough for the kernel to merge it.

My guess is that it will take some time, probably measured in months, to carry out this level of integration and testing to.

That would be an argument if this was a new patch series. "Wait a few months".

But that just isn't the case here.

The fact is, these patches have been floating around in one form or another not for a couple of months, but for years. There's a LWN article about it from 2015, and it wasn't new back then either (slides from 2013).

I wouldn't be surprised if there had been academic _papers_ written about the notion.

So if there *still* is no actual real code around this, then that just strengthens my point - no way should we merge something where people haven't actually bothered to write the user-mode component for years and years.

It really boils down to: "if nobody can be bothered to write the user mode parts after several years, why should it be merged into the kernel"?

I don't think that's too much to ask for.

Linus

#7 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Quoted 27 lines · Linus Torvalds
Quoted 7 lines · Paul E. McKenney
Quoted 4 lines

And if nobody can be bothered to write the user-level code and test this patch-series, then obviously it's not important enough for the kernel to merge it.

My guess is that it will take some time, probably measured in months, to carry out this level of integration and testing to.

That would be an argument if this was a new patch series. "Wait a few months".

But that just isn't the case here.

The fact is, these patches have been floating around in one form or another not for a couple of months, but for years. There's a LWN article about it from 2015, and it wasn't new back then either (slides from 2013).

I wouldn't be surprised if there had been academic _papers_ written about the notion.

So if there *still* is no actual real code around this, then that just strengthens my point - no way should we merge something where people haven't actually bothered to write the user-mode component for years and years.

Quoted 5 lines

It really boils down to: "if nobody can be bothered to write the user mode parts after several years, why should it be merged into the kernel"?

I don't think that's too much to ask for.

Completely and totally agreed. This doesn't go into mainline without being integrated into the various relevant usermode parts, and even then not without some significant advantages having been clearly demonstrated.

The same as has been the case for the -rt patches.

Thanx, Paul

author maintainer
#8 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

In reply to Linus Torvalds
Quoted 27 lines · ----- On Oct 13, 2017, at 5:05 PM, Linus Torvalds…
Quoted 7 lines · Paul E. McKenney
Quoted 4 lines

And if nobody can be bothered to write the user-level code and test this patch-series, then obviously it's not important enough for the kernel to merge it.

My guess is that it will take some time, probably measured in months, to carry out this level of integration and testing to.

That would be an argument if this was a new patch series. "Wait a few months".

But that just isn't the case here.

The fact is, these patches have been floating around in one form or another not for a couple of months, but for years. There's a LWN article about it from 2015, and it wasn't new back then either (slides from 2013).

I wouldn't be surprised if there had been academic _papers_ written about the notion.

So if there *still* is no actual real code around this, then that just strengthens my point - no way should we merge something where people haven't actually bothered to write the user-mode component for years and years.

Quoted 5 lines

It really boils down to: "if nobody can be bothered to write the user mode parts after several years, why should it be merged into the kernel"?

I don't think that's too much to ask for.

I remember hearing that Google have been running their own version of this patch on their servers. My understanding is that they did not really care about things like single-stepping on server workloads, because they never single-step. One issue there is that getting rseq to work for specifically tuned systems (e.g. no cpu hotplug, no single-stepping, and so on) is fairly straightforward. The tricky part is to make it work in all situations, and I don't think Google had incentive to complete those tricky bits, because they don't need them.

Facebook seem to try to follow upstream more closely. My understanding is that they can do specific prototypes to prove the value of the approach, as they did with their jemalloc integration from last year.

I have myself integrated the LTTng-UST tracer to rseq as a prototype branch, and created a Userspace RCU prototype branch that works similarly to SRCU in the kernel, using per-cpu counters. Those are staying prototypes because I won't release an open source tool or library based on non-mainline system call numbers, this just cannot end well. Once/if rseq gets in, my next step will be to implement a multi-process userspace RCU flavor based on per-cpu counters (with rseq). Doing this with atomic operations is not worth it, because it just leads to really poor performance for read-side.

I also spoke to Carlos O'Donell from glibc about it, and he was very excited about the possible use of rseq for malloc speedup/memory usage improvement. But again, I don't see a project like glibc starting to use a system call for which the number will have to be bumped every now and then.

I would *not* want this merged before we gather significant user feedback. The question is: how can we best gather that feedback ?

Perhaps one approach could be to reserve system call numbers for sys_rseq and sys_cpu_opv, but leave them unimplemented for now (ENOSYS). This would lessen the amount of pain user-space would have to go through to adapt to system call number changes, and we could provide the implementation of those system calls in a -rseq tree, which I'd be happy to maintain in order to gather feedback. If it ends up that it's not the right approach after all, all we would have lost is two unwired system call numbers per architecture.

Thoughts ?

Thanks,

Mathieu

Quoted 2 lines

Linus

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#47 lore raw

Re: [RFC PATCH for 4.15 09/14] Provide cpu_opv system call

In reply to Mathieu Desnoyers

kernel/cpu_opv.c line 461

Quoted 9 lines
kernel/cpu_opv.c new file +9 -0
+	pagefault_disable();
+	switch (len) {
+	case 1:
+		if (__get_user(tmp._u8, (uint8_t __user *)p))
+			goto end;
+		tmp._u8 += (uint8_t)count;
+		if (__put_user(tmp._u8, (uint8_t __user *)p))
+			goto end;
+		break;

It seems the code size could be dramatically shrunk by using a function pointer callback for the actual operation, and then avoiding all the copies. Since this is only a fallback this shouldn't be a problem (and modern branch predictors are fairly good in such situations anyways)

If you really want to keep the code it would be better if you used some macros -- i bet there's a typo in here somewhere in this forest.

-Andi

#18 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

In reply to Linus Torvalds
Quoted 3 lines

That sounds so obvious and stupid that you might go "What do you mean?", but for things to work for libraries, they have to work together with *other* users, and with *independent* users.

As far as I can see the current model fundamentally only works for one user per process (because there is only a single range and abort IP)

So once malloc started using it noone else could.

Since malloc is the primary user it would be pointless to ever try it on something else.

It seems fixing that would complicate everything quite a bit -- all ranges would need to be (unlimited?) arrays.

-Andi

#19 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Quoted 3 lines · Andi Kleen

As far as I can see the current model fundamentally only works for one user per process (because there is only a single range and abort IP)

No, it should work for libraries, you just need to always initialize the proper start/commit/abort IP's for every transaction. Then everybody should be fine.

So I _think_ it's all good. But I really would want to see that actually being the case.

Linus

author maintainer
#20 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Quoted 8 lines · ----- On Oct 14, 2017, at 12:05 AM, Linus Torvalds…
Quoted 3 lines · Andi Kleen

As far as I can see the current model fundamentally only works for one user per process (because there is only a single range and abort IP)

No, it should work for libraries, you just need to always initialize the proper start/commit/abort IP's for every transaction. Then everybody should be fine.

Yes, it does work for libraries. I have used it in my lttng-ust and liburcu prototypes, which are libraries. LTTng-UST requires at least two distinct critical sections (reserve and commit). For use in both executable and multiple libraries, we need each to declare the struct rseq TLS as a weak symbol, so only one gets picked throughout the process.

One clarification about your statement above: the user-space fast-path does not need to initialize much at runtime: one "rseq_cs descriptor" is created by each rseq_finish assembly section. Each of those is initialized by the dynamic loader with the proper addresses.

All the user-space fast-path really needs to do is to store the address to that descriptor into the TLS "rseq_cs" field. It does not even have to clear it after the critical section: the kernel can do it lazily.

Quoted 3 lines

So I _think_ it's all good. But I really would want to see that actually being the case.

There is one other use-case I've been made aware of in the past months:
Will Deacon want to use rseq on aarch64 to read PMU counters on big.LITTLE to prevent migration and use of an unsupported PMC on a LITTLE core, which could trigger a fault.

You had a really good point about cpu hotplug by the way. I recently realize that algorithms that have multiple non-atomic steps may _require_ to execute a series of steps on the same CPU. One example is lttng-ust ring buffer: it works on per-cpu buffers, and does a series of operations: reserve, [write to buffer], commit. Both reserve and commit can benefit from rseq, but we really need the commit to happen on the right CPU. Currently, in order to handle CPU hotplug, lttng-ust allocates CPU buffers for all possible cpus. If a CPU is hotunplugged between the reserve and commit though, we would run into a scenario where the "commit" could never be completed on the right CPU. I've actually prepared a follow-up patch [1] yesterday that fixes this in the cpu_opv() system call: it detects situations where the target CPU is possible but not online, prevents cpu hotplug, grabs a mutex, and performs the requested operation from whichever CPU it happens to run on.

Those are the kind of use-cases I want to gather more feedback on before we integrate those system calls for good.

Thanks,

Mathieu

[1] https://github.com/compudj/linux-percpu-dev/commit/b602821e446f7bd8a0a2de44c598f257cf4120f5

Quoted 2 lines

Linus

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
author maintainer
#28 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

In reply to Andy Lutomirski
Quoted 90 lines · ----- On Oct 13, 2017, at 2:17 PM, Andy Lutomirski [email protected] wrote:
Quoted 89 lines · Florian Weimer
Quoted 86 lines · Andy Lutomirski
Quoted 70 lines · Mathieu Desnoyers

----- On Oct 13, 2017, at 9:56 AM, Florian Weimer [email protected]

Quoted 29 lines · wrote:
Quoted 18 lines · Mathieu Desnoyers

The proposed ABI does not require to store any function pointer. For a given
rseq_finish() critical section, pointers to specific instructions (within a
function) are emitted at link-time into a struct rseq_cs:

struct rseq_cs {
          RSEQ_FIELD_u32_u64(start_ip);
          RSEQ_FIELD_u32_u64(post_commit_ip);
          RSEQ_FIELD_u32_u64(abort_ip);
          uint32_t flags;
} __attribute__((aligned(4 * sizeof(uint64_t))));

Then, at runtime, the fast-path stores the address of that struct rseq_cs
into the TLS struct rseq "rseq_cs" field.

Quoted 8 lines

So all we store at runtime is a pointer to data, not a pointer to functions.

But you seem to hint that having a pointer to data containing pointers to code
may still be making it easier for exploit writers. Can you elaborate on the
scenario ?

Quoted 8 lines

I'm concerned that the exploit writer writes a totally made up struct rseq_cs object into writable memory, along with function pointers, and puts the address of that in to the rseq_cs field.

This would be comparable to how C++ vtable pointers are targeted (including those in the glibc libio implementation of stdio streams).

Does this answer your questions?

Yes, it does. How about we add a "canary" field to the TLS struct rseq, e.g.:

struct rseq {
         union rseq_cpu_event u;
         RSEQ_FIELD_u32_u64(rseq_cs);  -> pointer to struct rseq_cs
         uint32_t flags;
         uint32_t canary;   -> 32 low bits of rseq_cs ^ canary_mask
};

We could then add a "uint32_t canary_mask" argument to sys_rseq, e.g.:

SYSCALL_DEFINE3(rseq, struct rseq __user *, rseq, uint32_t, canary_mask, int, flags);

So a thread which does not care about hardening would simply register its struct rseq TLS with a canary mask of "0". Nothing changes on the fast-path.

A thread belonging to a process that cares about hardening could use a random
value as canary, and pass it as canary_mask argument to the syscall. The fast-path could then set the struct rseq "canary" value to (32-low-bits of rseq_cs) ^ canary_mask just surrounding the critical section,
and set it back to 0 afterward.

Quoted 7 lines

In the kernel, whenever the rseq_cs pointer would be loaded, its 32 low bits
would be checked to match (canary ^ canary_mask). If it differs, then the kernel kills the process with SIGSEGV.

Would that take care of your concern ?

I would propose a slightly different solution: have the kernel verify that it jumps to a code sequence that occurs just after some highly-unlikely magic bytes in the text *and* that those bytes have some signature that matches a signature in the struct rseq that's passed in.

Quoted 5 lines
Quoted 1 line

And the signature is fixed at the time of the rseq syscall?

The point of the signature is to prevent an rseq landing pad from being used out of context. Actually getting the details right might be tricky.

So my understanding is that we want to prevent an attacker that controls the stack to easily use rseq to trick the kernel into branching into an arbitrary pre-existing executable address in the process.

I like the idea of putting a signature just before the abort_ip landing address and having it checked by the kernel. We could start by using a fixed hardcoded signature for now, and pass the signature value to the kernel when registering rseq. This would eventually allow a process to use a randomized signature if we figure out it's needed in the future.

I don't see how placing this signature in struct rseq TLS area is a good idea: an attacker could then just overwrite that value so it matches whatever code is before the branch target it wishes to branch to.

I also don't get how having the signature in the struct rseq_cs (restartable sequence descriptor) alongside with start/end/abort ip can be useful. Typically, an attacker would put its fake structure either on the stack, in data, or in rw memory, and make sure it uses the right signature in there. In the end, we don't really care whether the user ends up controlling the content of a struct rseq_cs, what we really care about is that it does not make the kernel branch to a pre-existing executable code address of its choosing.

So having the kernel validate a signature placed just before the abort_ip should be enough for hardening purposes.

Thoughts ?

Thanks,

Mathieu

Quoted 12 lines
Quoted 6 lines

Yes, that would be far more reliable.

Thanks,
Florian

--
Andy Lutomirski
AMA Capital Management, LLC

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
author maintainer
#48 lore raw

Re: [RFC PATCH for 4.15 09/14] Provide cpu_opv system call

In reply to Andi Kleen
Quoted 15 lines · ----- On Oct 13, 2017, at 10:50 PM, Andi Kleen [email protected] wrote:

kernel/cpu_opv.c line 461

Quoted 9 lines
kernel/cpu_opv.c new file +9 -0
+	pagefault_disable();
+	switch (len) {
+	case 1:
+		if (__get_user(tmp._u8, (uint8_t __user *)p))
+			goto end;
+		tmp._u8 += (uint8_t)count;
+		if (__put_user(tmp._u8, (uint8_t __user *)p))
+			goto end;
+		break;

It seems the code size could be dramatically shrunk by using a function pointer callback for the actual operation, and then avoiding all the copies. Since this is only a fallback this shouldn't be a problem (and modern branch predictors are fairly good in such situations anyways)

Quoted 3 lines

If you really want to keep the code it would be better if you used some macros -- i bet there's a typo in here somewhere in this forest.

Good point!

I'll add this in the next round. Meanwhile, here is the resulting
commit: https://github.com/compudj/linux-percpu-dev/commit/864b444f64f4c227ddc587d12631ff0d3440796c

Thanks,

Mathieu

Quoted 2 lines

-Andi

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
author maintainer
#45 lore raw

Re: [RFC PATCH for 4.15 09/14] Provide cpu_opv system call

In reply to Mathieu Desnoyers
Quoted 19 lines · ----- On Oct 13, 2017, at 10:50 AM, Mathieu Desnoyers…

----- On Oct 13, 2017, at 9:57 AM, One Thousand Gnomes

Quoted 6 lines · [email protected] wrote:
Quoted 3 lines

A maximum limit of 16 operations per cpu_opv syscall invocation is enforced, so user-space cannot generate a too long preempt-off critical section.

Except that all the operations could be going to mmapped I/O space and if I pick the right targets could take quite a long time to complete.

We could check whether a struct page belongs to mmapped I/O space, and return EINVAL in that case.

Quoted 5 lines

It's
still only 16 operations - But 160ms is a lot worse than 10ms. In fact with compare_iter I could make it much much worse still as I get 2 x TMP_BUFLEN x 16 x worst case latency in my attack. That's enough to screw up plenty of things.

Quoted 3 lines

Would a check that ensures the page is not mmapped I/O space be sufficient to take care of this ? If happen to know which API I need to look for, it would be welcome.

I think is_zone_device_page() is what I was looking for.

Let me know if I missed something,

Thanks,

Mathieu

Quoted 12 lines

Thanks,

Mathieu

Quoted 2 lines

Alan

--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#54 lore raw

Re: [RFC PATCH for 4.15 14/14] Restartable sequences: Provide self-tests

In reply to Mathieu Desnoyers
Quoted 11 lines · Mathieu Desnoyers

Implements two basic tests of RSEQ functionality, and one more exhaustive parameterizable test.

The first, "basic_test" only asserts that RSEQ works moderately correctly.
E.g. that:
- The CPUID pointer works
- Code infinitely looping within a critical section will eventually be interrupted.
- Critical sections are interrupted by signals.

Quoted 6 lines

"basic_percpu_ops_test" is a slightly more "realistic" variant, implementing a few simple per-cpu operations and testing their correctness.

"param_test" is a parametrizable restartable sequences test. See the "--help" output for usage.

Thanks for providing selftests :)

The Makefiles could use a little clean up:
- cpu-opv doesn't need libpthread
- you don't need to define your own rule just for building
- use TEST_GEN_PROGS to hook into the right parts of lib.mk
- .. which means you can use the clean rule in lib.mk

I notice you didn't add rseq or cpu-opv to the list of TARGETS in tools/testing/selftests/Makefile, was that deliberate?

Feel free to squash this patch in if you're happy to.

This still works with:

  $ make -C tools/testing/selftests TARGETS=rseq

and:

  $ cd tools/testing/selftests/rseq; make

cheers

tools/testing/selftests/cpu-opv/Makefile +4 -8
index 81d0596824ee..d41670ad5c43 100644
@@ -1,13 +1,9 @@
 CFLAGS += -O2 -Wall -g -I./ -I../../../../usr/include/
-LDFLAGS += -lpthread
 
-TESTS = basic_cpu_opv_test
+TEST_GEN_PROGS = basic_cpu_opv_test
 
-all: $(TESTS)
-%: %.c cpu-op.c cpu-op.h
-	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
+all: $(TEST_GEN_PROGS)
 
-include ../lib.mk
+$(TEST_GEN_PROGS): cpu-op.c cpu-op.h
 
-clean:
-	$(RM) $(TESTS)
+include ../lib.mk
tools/testing/selftests/rseq/Makefile +5 -8
index 7f0153556b80..9f8257b4ce14 100644
@@ -1,13 +1,10 @@
 CFLAGS += -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/
-LDFLAGS += -lpthread
+LDLIBS += -lpthread
 
-TESTS = basic_test basic_percpu_ops_test param_test
+TEST_GEN_PROGS = basic_test basic_percpu_ops_test param_test
 
-all: $(TESTS)
-%: %.c rseq.h rseq-*.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h
-	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
+all: $(TEST_GEN_PROGS)
 
-include ../lib.mk
+$(TEST_GEN_PROGS): rseq.h rseq-*.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h
 
-clean:
-	$(RM) $(TESTS)
+include ../lib.mk
#62 lore raw

Re: [RFC PATCH for 4.15 14/14] Restartable sequences: Provide self-tests

In reply to Mathieu Desnoyers
Quoted 55 lines · Mathieu Desnoyers

Implements two basic tests of RSEQ functionality, and one more exhaustive parameterizable test.

The first, "basic_test" only asserts that RSEQ works moderately correctly.
E.g. that:
- The CPUID pointer works
- Code infinitely looping within a critical section will eventually be interrupted.
- Critical sections are interrupted by signals.

"basic_percpu_ops_test" is a slightly more "realistic" variant, implementing a few simple per-cpu operations and testing their correctness.

"param_test" is a parametrizable restartable sequences test. See the "--help" output for usage.

As part of those tests, a helper library "rseq" implements a user-space API around restartable sequences. It uses the cpu_opv system call as fallback when single-stepped by a debugger. It exposes the instruction pointer addresses where the rseq assembly blocks begin and end, as well as the associated abort instruction pointer, in the __rseq_table section. This section allows debuggers may know where to place breakpoints when single-stepping through assembly blocks which may be aborted at any point by the kernel.

The following rseq APIs are implemented in this helper library:
- rseq_register_current_thread()/rseq_unregister_current_thread():
register/unregister current thread's use of rseq,
- rseq_current_cpu_raw():
current CPU number,
- rseq_start():
beginning of a restartable sequence,
- rseq_cpu_at_start():
CPU number at start of restartable sequence,
- rseq_finish():
End of restartable sequence made of zero or more loads, completed by a word-sized store,
- rseq_finish2():
End of restartable sequence made of zero or more loads, one speculative word-sized store, completed by a word-sized store,
- rseq_finish2_release():
End of restartable sequence made of zero or more loads, one speculative word-sized store, completed by a word-sized store with release semantic,
- rseq_finish_memcpy():
End of restartable sequence made of zero or more loads, a speculative copy of a variable length memory region, completed by a word-sized store.
- rseq_finish_memcpy_release():
End of restartable sequence made of zero or more loads, a speculative copy of a variable length memory region, completed by a word-sized store with release semantic.

Quoted 1 line

PowerPC tests have been implemented by Boqun Feng.

Hi Boqun,

I'm having trouble testing these, I get:

~/linus/tools/testing/selftests/cpu-opv$ ./basic_cpu_opv_test Testing test_compare_eq same
Testing test_compare_eq different
Testing test_compare_ne same
Testing test_compare_ne different
Testing test_2compare_eq index
Testing test_2compare_ne index
Testing test_memcpy
Testing test_memcpy_u32
Testing test_add
Testing test_two_add
Testing test_or
Testing test_and
Testing test_xor
Testing test_lshift
Testing test_rshift
Testing test_cmpxchg success
Testing test_cmpxchg fail

~/linus/tools/testing/selftests/rseq$ ./basic_test testing current cpu
testing critical section
testing critical section is interrupted by signal

~/linus/tools/testing/selftests/rseq$ ./basic_percpu_ops_test
./basic_percpu_ops_test: error while loading shared libraries: R_PPC64_ADDR16_HI re10d8f10a0 for symbol `' out of range ~/linus/tools/testing/selftests/rseq$ ./param_test
./param_test: error while loading shared libraries: R_PPC64_ADDR16_HI re136251b48 for symbol `' out of range

Any idea what's going on with the last two? I assume you don't see that in your test setup :)

cheers

#63 lore raw

Re: [RFC PATCH for 4.15 14/14] Restartable sequences: Provide self-tests

Quoted 57 lines · Michael Ellerman
Quoted 55 lines · Mathieu Desnoyers

Implements two basic tests of RSEQ functionality, and one more exhaustive parameterizable test.

The first, "basic_test" only asserts that RSEQ works moderately correctly.
E.g. that:
- The CPUID pointer works
- Code infinitely looping within a critical section will eventually be interrupted.
- Critical sections are interrupted by signals.

"basic_percpu_ops_test" is a slightly more "realistic" variant, implementing a few simple per-cpu operations and testing their correctness.

"param_test" is a parametrizable restartable sequences test. See the "--help" output for usage.

As part of those tests, a helper library "rseq" implements a user-space API around restartable sequences. It uses the cpu_opv system call as fallback when single-stepped by a debugger. It exposes the instruction pointer addresses where the rseq assembly blocks begin and end, as well as the associated abort instruction pointer, in the __rseq_table section. This section allows debuggers may know where to place breakpoints when single-stepping through assembly blocks which may be aborted at any point by the kernel.

The following rseq APIs are implemented in this helper library:
- rseq_register_current_thread()/rseq_unregister_current_thread():
register/unregister current thread's use of rseq,
- rseq_current_cpu_raw():
current CPU number,
- rseq_start():
beginning of a restartable sequence,
- rseq_cpu_at_start():
CPU number at start of restartable sequence,
- rseq_finish():
End of restartable sequence made of zero or more loads, completed by a word-sized store,
- rseq_finish2():
End of restartable sequence made of zero or more loads, one speculative word-sized store, completed by a word-sized store,
- rseq_finish2_release():
End of restartable sequence made of zero or more loads, one speculative word-sized store, completed by a word-sized store with release semantic,
- rseq_finish_memcpy():
End of restartable sequence made of zero or more loads, a speculative copy of a variable length memory region, completed by a word-sized store.
- rseq_finish_memcpy_release():
End of restartable sequence made of zero or more loads, a speculative copy of a variable length memory region, completed by a word-sized store with release semantic.

Quoted 4 lines
Quoted 1 line

PowerPC tests have been implemented by Boqun Feng.

Hi Boqun,

Hello Michael,

Quoted 26 lines

I'm having trouble testing these, I get:

~/linus/tools/testing/selftests/cpu-opv$ ./basic_cpu_opv_test Testing test_compare_eq same
Testing test_compare_eq different
Testing test_compare_ne same
Testing test_compare_ne different
Testing test_2compare_eq index
Testing test_2compare_ne index
Testing test_memcpy
Testing test_memcpy_u32
Testing test_add
Testing test_two_add
Testing test_or
Testing test_and
Testing test_xor
Testing test_lshift
Testing test_rshift
Testing test_cmpxchg success
Testing test_cmpxchg fail

~/linus/tools/testing/selftests/rseq$ ./basic_test testing current cpu
testing critical section
testing critical section is interrupted by signal

Quoted 5 lines

~/linus/tools/testing/selftests/rseq$ ./basic_percpu_ops_test
./basic_percpu_ops_test: error while loading shared libraries: R_PPC64_ADDR16_HI re10d8f10a0 for symbol `' out of range ~/linus/tools/testing/selftests/rseq$ ./param_test
./param_test: error while loading shared libraries: R_PPC64_ADDR16_HI re136251b48 for symbol `' out of range

I think this one is due to the same reason as:

	7998eb3dc700 ("powerpc: Fix 64 bit builds with binutils 2.24")

I have made the fix before, but seems forgot to send it to Mathieu...

so would this help?

tools/testing/selftests/rseq/rseq-ppc.h +1 -1
index bc78b4fd72b1..39cbabe89b0e 100644
@@ -74,7 +74,7 @@ do {									\
 		"lis %%r17, (3b)@highest\n\t" \
 		"ori %%r17, %%r17, (3b)@higher\n\t" \
 		"rldicr %%r17, %%r17, 32, 31\n\t" \
-		"oris %%r17, %%r17, (3b)@h\n\t" \
+		"oris %%r17, %%r17, (3b)@high\n\t" \
 		"ori %%r17, %%r17, (3b)@l\n\t" \
 		"std %%r17, 0(%[rseq_cs])\n\t" \
 		RSEQ_INJECT_ASM(2) \

Regards,
Boqun

Quoted 5 lines

Any idea what's going on with the last two? I assume you don't see that in your test setup :)

cheers

#64 lore raw

Re: [RFC PATCH for 4.15 14/14] Restartable sequences: Provide self-tests

Quoted 3 lines · Boqun Feng
Quoted 1 line · Michael Ellerman

I'm having trouble testing these, I get:

...

Quoted 20 lines
Quoted 6 lines

~/linus/tools/testing/selftests/rseq$ ./basic_percpu_ops_test
./basic_percpu_ops_test: error while loading shared libraries: R_PPC64_ADDR16_HI re10d8f10a0 for symbol `' out of range ~/linus/tools/testing/selftests/rseq$ ./param_test
./param_test: error while loading shared libraries: R_PPC64_ADDR16_HI re136251b48 for symbol `' out of range

I think this one is due to the same reason as:

	7998eb3dc700 ("powerpc: Fix 64 bit builds with binutils 2.24")

I have made the fix before, but seems forgot to send it to Mathieu...

so would this help?

tools/testing/selftests/rseq/rseq-ppc.h +0 -0
index bc78b4fd72b1..39cbabe89b0e 100644

tools/testing/selftests/rseq/rseq-ppc.h do { \ · line 80

Quoted 8 lines
tools/testing/selftests/rseq/rseq-ppc.h +1 -1
@@ -74,7 +74,7 @@ do {									\
 		"lis %%r17, (3b)@highest\n\t" \
 		"ori %%r17, %%r17, (3b)@higher\n\t" \
 		"rldicr %%r17, %%r17, 32, 31\n\t" \
-		"oris %%r17, %%r17, (3b)@h\n\t" \
+		"oris %%r17, %%r17, (3b)@high\n\t" \
 		"ori %%r17, %%r17, (3b)@l\n\t" \
 		"std %%r17, 0(%[rseq_cs])\n\t" \
 		RSEQ_INJECT_ASM(2) \

Yes, that fixes it thanks!

cheers

author maintainer
#55 lore raw

Re: [RFC PATCH for 4.15 14/14] Restartable sequences: Provide self-tests

In reply to Michael Ellerman
Quoted 29 lines · ----- On Oct 15, 2017, at 10:51 PM, Michael Ellerman [email protected] wrote:
Quoted 11 lines · Mathieu Desnoyers

Implements two basic tests of RSEQ functionality, and one more exhaustive parameterizable test.

The first, "basic_test" only asserts that RSEQ works moderately correctly.
E.g. that:
- The CPUID pointer works
- Code infinitely looping within a critical section will eventually be interrupted.
- Critical sections are interrupted by signals.

Quoted 6 lines

"basic_percpu_ops_test" is a slightly more "realistic" variant, implementing a few simple per-cpu operations and testing their correctness.

"param_test" is a parametrizable restartable sequences test. See the "--help" output for usage.

Thanks for providing selftests :)

The Makefiles could use a little clean up:
- cpu-opv doesn't need libpthread
- you don't need to define your own rule just for building
- use TEST_GEN_PROGS to hook into the right parts of lib.mk
- .. which means you can use the clean rule in lib.mk

Quoted 2 lines

I notice you didn't add rseq or cpu-opv to the list of TARGETS in tools/testing/selftests/Makefile, was that deliberate?

No, I think I just copied some other selftest which perhaps did not have those rules back then.

Quoted 8 lines

Feel free to squash this patch in if you're happy to.

This still works with:

 $ make -C tools/testing/selftests TARGETS=rseq

and:

 $ cd tools/testing/selftests/rseq; make

Great ! I'll fold this patch in.

Thanks!

Mathieu

Quoted 51 lines

cheers

tools/testing/selftests/cpu-opv/Makefile +0 -0
index 81d0596824ee..d41670ad5c43 100644
@@ -1,13 +1,9 @@

CFLAGS += -O2 -Wall -g -I./ -I../../../../usr/include/

tools/testing/selftests/cpu-opv/Makefile +4 -8
-LDFLAGS += -lpthread
 
-TESTS = basic_cpu_opv_test
+TEST_GEN_PROGS = basic_cpu_opv_test
 
-all: $(TESTS)
-%: %.c cpu-op.c cpu-op.h
-	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
+all: $(TEST_GEN_PROGS)
 
-include ../lib.mk
+$(TEST_GEN_PROGS): cpu-op.c cpu-op.h
 
-clean:
-	$(RM) $(TESTS)
+include ../lib.mk
tools/testing/selftests/rseq/Makefile +0 -0
index 7f0153556b80..9f8257b4ce14 100644
@@ -1,13 +1,10 @@

CFLAGS += -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/

tools/testing/selftests/rseq/Makefile +4 -6
-LDFLAGS += -lpthread
+LDLIBS += -lpthread
 
-TESTS = basic_test basic_percpu_ops_test param_test
+TEST_GEN_PROGS = basic_test basic_percpu_ops_test param_test
 
-all: $(TESTS)
-%: %.c rseq.h rseq-*.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h
-	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
+all: $(TEST_GEN_PROGS)
 
-include ../lib.mk
+$(TEST_GEN_PROGS): rseq.h rseq-*.h rseq.c ../cpu-opv/cpu-op.c

../cpu-opv/cpu-op.h

tools/testing/selftests/rseq/Makefile +1 -2
-clean:
-	$(RM) $(TESTS)
+include ../lib.mk
Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#9 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

In reply to Mathieu Desnoyers
Quoted 18 lines · Mathieu Desnoyers

I also spoke to Carlos O'Donell from glibc about it, and he was very excited about the possible use of rseq for malloc speedup/memory usage improvement. But again, I don't see a project like glibc starting to use a system call for which the number will have to be bumped every now and then.

I would *not* want this merged before we gather significant user feedback. The question is: how can we best gather that feedback ?

Perhaps one approach could be to reserve system call numbers for sys_rseq and sys_cpu_opv, but leave them unimplemented for now (ENOSYS). This would lessen the amount of pain user-space would have to go through to adapt to system call number changes, and we could provide the implementation of those system calls in a -rseq tree, which I'd be happy to maintain in order to gather feedback. If it ends up that it's not the right approach after all, all we would have lost is two unwired system call numbers per architecture.

Quoted 1 line

Thoughts ?

We have similar problems in glibc with API/ABI issues, and there isn't really any way around this except to present a reviewer with an overwhelming amount of evidence that use cases exist and work.

How you collect, summarize, and analyze that overwhelming evidence is up to you, specific to each change, and difficult to do accurately and with any large measure of statistical confidence. The reviewer has to basically trust you to some degree :-)

We should probably be working together to present the case to Linus that glibc is immediately ready to use restartable sequences and provide the use cases we have in mind with a public branch showing the work and the results. This would at least convince people that if we turned this on, every application would get benefit from a GNU system running glibc (which is less than the number of people running Linux on phones these days so YMMV).

As always, glibc can use any new kernel features immediately, and only needs to detect presence at startup.

My only concrete suggestion would be to add a level of indirection, some way to fetch the new syscalls dynamically at program startup, then I could construct a way to call them, mark it RO, and use that e.g. a userspace syscall table populated dynamically for experimental syscalls (semantic changes would require changes in the name used for lookup). It's just an expansion of the number of bits used to identify the syscall. Obviously such a patch is only for downstream testing in order to gather consensus for upstream patches.

Signature · 2 lines
Cheers,
Carlos.
#10 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Quoted 4 lines

How you collect, summarize, and analyze that overwhelming evidence is up to you, specific to each change, and difficult to do accurately and with any large measure of statistical confidence. The reviewer has to basically trust you to some degree :-)

I think Linus' just asked for some working "real world, not micro" code that demonstrates use.

A prototype type implementation of the glibc malloc cache using this may be good enough.

Even if the API still changes slightly later in review I would assume the basic concepts will stay the same, so it would be likely not too difficult to convert that prototype to the later final API.

-Andi

author maintainer
#58 lore raw

Re: [RFC PATCH for 4.15 14/14] Restartable sequences: Provide self-tests

In reply to Michael Ellerman

Hi Michael,

With your changes integrated, both rseq and cpu-opv selftests fail to build if I pass e.g. -j32 to make.

cd tools/testing/selftests/cpu-opv

efficios@compudjdev:~/git/linux-percpu-dev/tools/testing/selftests/cpu-opv$ make clean; make
rm -f -r /home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test   
gcc -O2 -Wall -g -I./ -I../../../../usr/include/    basic_cpu_opv_test.c cpu-op.c cpu-op.h   -o basic_cpu_opv_test
efficios@compudjdev:~/git/linux-percpu-dev/tools/testing/selftests/cpu-opv$ make clean; make -j32
rm -f -r /home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test   
gcc -O2 -Wall -g -I./ -I../../../../usr/include/    basic_cpu_opv_test.c cpu-op.c cpu-op.h   -o basic_cpu_opv_test
gcc -O2 -Wall -g -I./ -I../../../../usr/include/    basic_cpu_opv_test.c  -o /home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test
/tmp/ccDthnqM.o: In function `test_memcpy_op':
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:364: undefined reference to `cpu_op_get_current_cpu'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:365: undefined reference to `cpu_opv'
/tmp/ccDthnqM.o: In function `test_2compare_ne_op':
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:279: undefined reference to `cpu_op_get_current_cpu'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:280: undefined reference to `cpu_opv'
/tmp/ccDthnqM.o: In function `test_2compare_eq_op':
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:189: undefined reference to `cpu_op_get_current_cpu'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:190: undefined reference to `cpu_opv'
/tmp/ccDthnqM.o: In function `test_compare_ne_op':
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:108: undefined reference to `cpu_op_get_current_cpu'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:109: undefined reference to `cpu_opv'
/tmp/ccDthnqM.o: In function `test_compare_eq_op':
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:34: undefined reference to `cpu_op_get_current_cpu'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:35: undefined reference to `cpu_opv'
/tmp/ccDthnqM.o: In function `test_add_op':
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:430: undefined reference to `cpu_op_get_current_cpu'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:431: undefined reference to `cpu_op_add'
/tmp/ccDthnqM.o: In function `test_two_add_op':
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:479: undefined reference to `cpu_op_get_current_cpu'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:480: undefined reference to `cpu_opv'
/tmp/ccDthnqM.o: In function `test_or_op':
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:522: undefined reference to `cpu_op_get_current_cpu'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:523: undefined reference to `cpu_opv'
/tmp/ccDthnqM.o: In function `test_and_op':
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:565: undefined reference to `cpu_op_get_current_cpu'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:566: undefined reference to `cpu_opv'
/tmp/ccDthnqM.o: In function `test_xor_op':
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:608: undefined reference to `cpu_op_get_current_cpu'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:609: undefined reference to `cpu_opv'
/tmp/ccDthnqM.o: In function `test_lshift_op':
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:651: undefined reference to `cpu_op_get_current_cpu'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:652: undefined reference to `cpu_opv'
/tmp/ccDthnqM.o: In function `test_rshift_op':
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:695: undefined reference to `cpu_op_get_current_cpu'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:696: undefined reference to `cpu_opv'
/tmp/ccDthnqM.o: In function `test_cmpxchg_op':
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:731: undefined reference to `cpu_op_get_current_cpu'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:732: undefined reference to `cpu_op_cmpxchg'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:731: undefined reference to `cpu_op_get_current_cpu'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:732: undefined reference to `cpu_op_cmpxchg'
collect2: error: ld returned 1 exit status
make: *** [/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test] Error 1
make: *** Waiting for unfinished jobs....

Any idea what is going on here ?

Thanks,

Mathieu

Quoted 90 lines · ----- On Oct 15, 2017, at 10:51 PM, Michael Ellerman [email protected] wrote:
Quoted 11 lines · Mathieu Desnoyers

Implements two basic tests of RSEQ functionality, and one more exhaustive parameterizable test.

The first, "basic_test" only asserts that RSEQ works moderately correctly.
E.g. that:
- The CPUID pointer works
- Code infinitely looping within a critical section will eventually be interrupted.
- Critical sections are interrupted by signals.

Quoted 6 lines

"basic_percpu_ops_test" is a slightly more "realistic" variant, implementing a few simple per-cpu operations and testing their correctness.

"param_test" is a parametrizable restartable sequences test. See the "--help" output for usage.

Thanks for providing selftests :)

The Makefiles could use a little clean up:
- cpu-opv doesn't need libpthread
- you don't need to define your own rule just for building
- use TEST_GEN_PROGS to hook into the right parts of lib.mk
- .. which means you can use the clean rule in lib.mk

I notice you didn't add rseq or cpu-opv to the list of TARGETS in tools/testing/selftests/Makefile, was that deliberate?

Feel free to squash this patch in if you're happy to.

This still works with:

 $ make -C tools/testing/selftests TARGETS=rseq

and:

 $ cd tools/testing/selftests/rseq; make

cheers

tools/testing/selftests/cpu-opv/Makefile +0 -0
index 81d0596824ee..d41670ad5c43 100644
@@ -1,13 +1,9 @@

CFLAGS += -O2 -Wall -g -I./ -I../../../../usr/include/

tools/testing/selftests/cpu-opv/Makefile +4 -8
-LDFLAGS += -lpthread
 
-TESTS = basic_cpu_opv_test
+TEST_GEN_PROGS = basic_cpu_opv_test
 
-all: $(TESTS)
-%: %.c cpu-op.c cpu-op.h
-	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
+all: $(TEST_GEN_PROGS)
 
-include ../lib.mk
+$(TEST_GEN_PROGS): cpu-op.c cpu-op.h
 
-clean:
-	$(RM) $(TESTS)
+include ../lib.mk
tools/testing/selftests/rseq/Makefile +0 -0
index 7f0153556b80..9f8257b4ce14 100644
@@ -1,13 +1,10 @@

CFLAGS += -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/

tools/testing/selftests/rseq/Makefile +4 -6
-LDFLAGS += -lpthread
+LDLIBS += -lpthread
 
-TESTS = basic_test basic_percpu_ops_test param_test
+TEST_GEN_PROGS = basic_test basic_percpu_ops_test param_test
 
-all: $(TESTS)
-%: %.c rseq.h rseq-*.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h
-	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
+all: $(TEST_GEN_PROGS)
 
-include ../lib.mk
+$(TEST_GEN_PROGS): rseq.h rseq-*.h rseq.c ../cpu-opv/cpu-op.c

../cpu-opv/cpu-op.h

tools/testing/selftests/rseq/Makefile +1 -2
-clean:
-	$(RM) $(TESTS)
+include ../lib.mk
Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
author maintainer
#11 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

In reply to Andi Kleen
Quoted 8 lines · ----- On Oct 16, 2017, at 12:46 PM, Andi Kleen [email protected] wrote:
Quoted 4 lines

How you collect, summarize, and analyze that overwhelming evidence is up to you, specific to each change, and difficult to do accurately and with any large measure of statistical confidence. The reviewer has to basically trust you to some degree :-)

I think Linus' just asked for some working "real world, not micro" code that demonstrates use.

Quoted 6 lines

A prototype type implementation of the glibc malloc cache using this may be good enough.

Even if the API still changes slightly later in review I would assume the basic concepts will stay the same, so it would be likely not too difficult to convert that prototype to the later final API.

In that respect, I have working prototypes of two non-trivial library projects using rseq within the same process.

Those can be considered as being "early adopters" of rseq, before it becomes available in glibc.

- liburcu per-cpu flavor prototype [1]
Interesting bits at
https://github.com/compudj/userspace-rcu-dev/blob/urcu-percpu/include/urcu/static/urcu-percpu.h
https://github.com/compudj/userspace-rcu-dev/blob/urcu-percpu/src/urcu-percpu.c
(it also has its own copy of rseq and cpu-opv helper libraries)

- lttng-ust tracer rseq prototype [2, 3]
Interesting bits at
https://github.com/compudj/lttng-ust-dev/blob/rseq-integration-oct-2017/libringbuffer/getcpu.h#L85
https://github.com/compudj/lttng-ust-dev/blob/rseq-integration-oct-2017/libringbuffer/vatomic.h#L60
(it also has its own copy of rseq and cpu-opv helper libraries)

They use a slightly updated version of the rseq patchset, which I plan to push into a new "rseq" tree on kernel.org soon. It takes care of the comments I received in the past few days.

They end up sharing the "__rseq_abi" TLS weak symbol (initial state of cpu_id = -1). They lazy-detect whether rseq needs to be registered for the current thread by checking if the cpu_id read from the rseq TLS is < 0. If rseq registration fails, they set its value to -2 and won't try to register again (will use their fallback). When they successfully register, they setup a pthread_key so rseq is unregistered when the thread exits.

So far the restrictions I see for libraries using this symbol are:
- They should never be unloaded,
- They should never be loaded with dlopen RTLD_LOCAL flag.

If those are considered acceptable limitations, then we can stick to the "single rseq TLS per thread" rule, and we don't have to implement a linked-list of rseq TLS per thread.

When glibc eventually adds support for rseq, I expect it to deal with rseq TLS registration and unregistration at thread creation/exit. Therefore, the checks for negative cpu_id performed by lttng-ust and liburcu will figure out that rseq is already registered, and skip registration altogether when it's already performed by glibc.

Thoughts ?

Thanks,

Mathieu

[1] https://github.com/compudj/userspace-rcu-dev/tree/urcu-percpu
[2] https://github.com/compudj/lttng-ust-dev/tree/rseq-integration-oct-2017
[3] https://github.com/compudj/lttng-tools-dev/tree/urcu-percpu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#59 lore raw

Re: [RFC PATCH for 4.15 14/14] Restartable sequences: Provide self-tests

In reply to Mathieu Desnoyers
Quoted 18 lines · Mathieu Desnoyers

Hi Michael,

With your changes integrated, both rseq and cpu-opv selftests fail to build if I pass e.g. -j32 to make.

cd tools/testing/selftests/cpu-opv

efficios@compudjdev:~/git/linux-percpu-dev/tools/testing/selftests/cpu-opv$ make clean; make
rm -f -r /home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test   
gcc -O2 -Wall -g -I./ -I../../../../usr/include/    basic_cpu_opv_test.c cpu-op.c cpu-op.h   -o basic_cpu_opv_test
efficios@compudjdev:~/git/linux-percpu-dev/tools/testing/selftests/cpu-opv$ make clean; make -j32
rm -f -r /home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test   
gcc -O2 -Wall -g -I./ -I../../../../usr/include/    basic_cpu_opv_test.c cpu-op.c cpu-op.h   -o basic_cpu_opv_test
gcc -O2 -Wall -g -I./ -I../../../../usr/include/    basic_cpu_opv_test.c  -o /home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test
/tmp/ccDthnqM.o: In function `test_memcpy_op':
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:364: undefined reference to `cpu_op_get_current_cpu'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:365: undefined reference to `cpu_opv'

...

Quoted 4 lines

make: *** [/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test] Error 1
make: *** Waiting for unfinished jobs....

Any idea what is going on here ?

Ugh sorry, yes.

New patch below should fix it. Tested with -j:

~/linux/tools/testing/selftests$ make TARGETS="rseq cpu-opv" -j make[1]: Entering directory '/home/michael/linux/tools/testing/selftests/rseq'

  gcc -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/    basic_test.c rseq.h rseq-ppc.h rseq-x86.h rseq-arm.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h -lpthread -o /home/michael/linux/tools/testing/selftests/rseq/basic_test
  gcc -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/    basic_percpu_ops_test.c rseq.h rseq-ppc.h rseq-x86.h rseq-arm.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h -lpthread -o /home/michael/linux/tools/testing/selftests/rseq/basic_percpu_ops_test
  gcc -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/    param_test.c rseq.h rseq-ppc.h rseq-x86.h rseq-arm.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h -lpthread -o /home/michael/linux/tools/testing/selftests/rseq/param_test
  make[1]: Leaving directory '/home/michael/linux/tools/testing/selftests/rseq'
  make[1]: Entering directory '/home/michael/linux/tools/testing/selftests/cpu-opv'
  gcc -O2 -Wall -g -I./ -I../../../../usr/include/    basic_cpu_opv_test.c cpu-op.c cpu-op.h  -o /home/michael/linux/tools/testing/selftests/cpu-opv/basic_cpu_opv_test
  make[1]: Leaving directory '/home/michael/linux/tools/testing/selftests/cpu-opv'
  ~/linux/tools/testing/selftests$ make TARGETS="rseq cpu-opv" clean
  make[1]: Entering directory '/home/michael/linux/tools/testing/selftests/rseq'
  rm -f -r /home/michael/linux/tools/testing/selftests/rseq/basic_test /home/michael/linux/tools/testing/selftests/rseq/basic_percpu_ops_test /home/michael/linux/tools/testing/selftests/rseq/param_test   
  make[1]: Leaving directory '/home/michael/linux/tools/testing/selftests/rseq'
  make[1]: Entering directory '/home/michael/linux/tools/testing/selftests/cpu-opv'
  rm -f -r /home/michael/linux/tools/testing/selftests/cpu-opv/basic_cpu_opv_test   
  make[1]: Leaving directory '/home/michael/linux/tools/testing/selftests/cpu-opv'
  ~/linux/tools/testing/selftests$ cd cpu-opv/
  ~/linux/tools/testing/selftests/cpu-opv$ make -j
  gcc -O2 -Wall -g -I./ -I../../../../usr/include/    basic_cpu_opv_test.c cpu-op.c cpu-op.h  -o /home/michael/linux/tools/testing/selftests/cpu-opv/basic_cpu_opv_test
  ~/linux/tools/testing/selftests/cpu-opv$ make clean
  rm -f -r /home/michael/linux/tools/testing/selftests/cpu-opv/basic_cpu_opv_test   
  ~/linux/tools/testing/selftests/cpu-opv$ cd ../rseq/
  ~/linux/tools/testing/selftests/rseq$ make -j
  gcc -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/    basic_test.c rseq.h rseq-ppc.h rseq-x86.h rseq-arm.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h -lpthread -o /home/michael/linux/tools/testing/selftests/rseq/basic_test
  gcc -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/    basic_percpu_ops_test.c rseq.h rseq-ppc.h rseq-x86.h rseq-arm.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h -lpthread -o /home/michael/linux/tools/testing/selftests/rseq/basic_percpu_ops_test
  gcc -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/    param_test.c rseq.h rseq-ppc.h rseq-x86.h rseq-arm.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h -lpthread -o /home/michael/linux/tools/testing/selftests/rseq/param_test

~/linux/tools/testing/selftests/rseq$ make clean
rm -f -r /home/michael/linux/tools/testing/selftests/rseq/basic_test /home/michael/linux/tools/testing/selftests/rseq/basic_percpu_ops_test /home/michael/linux/tools/testing/selftests/rseq/param_test

cheers

tools/testing/selftests/cpu-opv/Makefile +2 -8
index 81d0596824ee..d27bd0f74deb 100644
@@ -1,13 +1,7 @@
 CFLAGS += -O2 -Wall -g -I./ -I../../../../usr/include/
-LDFLAGS += -lpthread
 
-TESTS = basic_cpu_opv_test
-
-all: $(TESTS)
-%: %.c cpu-op.c cpu-op.h
-       $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
+TEST_GEN_PROGS = basic_cpu_opv_test
 
 include ../lib.mk
 
-clean:
-       $(RM) $(TESTS)
+$(TEST_GEN_PROGS): cpu-op.c cpu-op.h
tools/testing/selftests/rseq/Makefile +3 -8
index 7f0153556b80..7f625147b7fe 100644
@@ -1,13 +1,8 @@
 CFLAGS += -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/
-LDFLAGS += -lpthread
+LDLIBS += -lpthread
 
-TESTS = basic_test basic_percpu_ops_test param_test
-
-all: $(TESTS)
-%: %.c rseq.h rseq-*.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h
-       $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
+TEST_GEN_PROGS = basic_test basic_percpu_ops_test param_test
 
 include ../lib.mk
 
-clean:
-       $(RM) $(TESTS)
+$(TEST_GEN_PROGS): rseq.h rseq-*.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h
#56 lore raw

Re: [RFC PATCH for 4.15 14/14] Restartable sequences: Provide self-tests

In reply to Mathieu Desnoyers
Quoted 31 lines · Mathieu Desnoyers
Quoted 29 lines · ----- On Oct 15, 2017, at 10:51 PM, Michael Ellerman…
Quoted 11 lines · Mathieu Desnoyers

Implements two basic tests of RSEQ functionality, and one more exhaustive parameterizable test.

The first, "basic_test" only asserts that RSEQ works moderately correctly.
E.g. that:
- The CPUID pointer works
- Code infinitely looping within a critical section will eventually be interrupted.
- Critical sections are interrupted by signals.

Quoted 6 lines

"basic_percpu_ops_test" is a slightly more "realistic" variant, implementing a few simple per-cpu operations and testing their correctness.

"param_test" is a parametrizable restartable sequences test. See the "--help" output for usage.

Thanks for providing selftests :)

The Makefiles could use a little clean up:
- cpu-opv doesn't need libpthread
- you don't need to define your own rule just for building
- use TEST_GEN_PROGS to hook into the right parts of lib.mk
- .. which means you can use the clean rule in lib.mk

Quoted 5 lines
Quoted 2 lines

I notice you didn't add rseq or cpu-opv to the list of TARGETS in tools/testing/selftests/Makefile, was that deliberate?

No, I think I just copied some other selftest which perhaps did not have those rules back then.

OK. I don't see why these tests shouldn't be part of the default test run, so here's a patch to add them.

cheers

tools/testing/selftests/Makefile +2 -0
index ff805643b5f7..b79ef5915c74 100644
@@ -3,6 +3,7 @@ TARGETS += breakpoints
 TARGETS += capabilities
 TARGETS += cpufreq
 TARGETS += cpu-hotplug
+TARGETS += cpu-opv
 TARGETS += efivarfs
 TARGETS += exec
 TARGETS += firmware
@@ -23,6 +24,7 @@ TARGETS += nsfs
 TARGETS += powerpc
 TARGETS += pstore
 TARGETS += ptrace
+TARGETS += rseq
 TARGETS += seccomp
 TARGETS += sigaltstack
 TARGETS += size
author maintainer
#60 lore raw

Re: [RFC PATCH for 4.15 14/14] Restartable sequences: Provide self-tests

In reply to Michael Ellerman
Quoted 35 lines · ----- On Oct 17, 2017, at 6:36 AM, Michael Ellerman [email protected] wrote:
Quoted 27 lines · Mathieu Desnoyers

Hi Michael,

With your changes integrated, both rseq and cpu-opv selftests fail to build if I pass e.g. -j32 to make.

cd tools/testing/selftests/cpu-opv

efficios@compudjdev:~/git/linux-percpu-dev/tools/testing/selftests/cpu-opv$ make
clean; make
rm -f -r
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test
gcc -O2 -Wall -g -I./ -I../../../../usr/include/    basic_cpu_opv_test.c
cpu-op.c cpu-op.h   -o basic_cpu_opv_test
efficios@compudjdev:~/git/linux-percpu-dev/tools/testing/selftests/cpu-opv$ make
clean; make -j32
rm -f -r
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test
gcc -O2 -Wall -g -I./ -I../../../../usr/include/    basic_cpu_opv_test.c
cpu-op.c cpu-op.h   -o basic_cpu_opv_test
gcc -O2 -Wall -g -I./ -I../../../../usr/include/    basic_cpu_opv_test.c  -o
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test
/tmp/ccDthnqM.o: In function `test_memcpy_op':
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:364:
undefined reference to `cpu_op_get_current_cpu'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:365:
undefined reference to `cpu_opv'

...

Quoted 5 lines

make: *** [/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test] Error 1
make: *** Waiting for unfinished jobs....

Quoted 5 lines
Quoted 1 line

Any idea what is going on here ?

Ugh sorry, yes.

New patch below should fix it. Tested with -j:

Perfect, folding it into my series.

I see that the "all" target was redundant here.

Thanks,

Mathieu

Quoted 106 lines

~/linux/tools/testing/selftests$ make TARGETS="rseq cpu-opv" -j make[1]: Entering directory '/home/michael/linux/tools/testing/selftests/rseq'
gcc -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/ basic_test.c
rseq.h rseq-ppc.h rseq-x86.h rseq-arm.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h -lpthread -o /home/michael/linux/tools/testing/selftests/rseq/basic_test
gcc -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/ basic_percpu_ops_test.c rseq.h rseq-ppc.h rseq-x86.h rseq-arm.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h -lpthread -o /home/michael/linux/tools/testing/selftests/rseq/basic_percpu_ops_test
gcc -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/ param_test.c
rseq.h rseq-ppc.h rseq-x86.h rseq-arm.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h -lpthread -o /home/michael/linux/tools/testing/selftests/rseq/param_test make[1]: Leaving directory '/home/michael/linux/tools/testing/selftests/rseq' make[1]: Entering directory '/home/michael/linux/tools/testing/selftests/cpu-opv'
gcc -O2 -Wall -g -I./ -I../../../../usr/include/ basic_cpu_opv_test.c
cpu-op.c cpu-op.h -o /home/michael/linux/tools/testing/selftests/cpu-opv/basic_cpu_opv_test make[1]: Leaving directory '/home/michael/linux/tools/testing/selftests/cpu-opv' ~/linux/tools/testing/selftests$ make TARGETS="rseq cpu-opv" clean make[1]: Entering directory '/home/michael/linux/tools/testing/selftests/rseq' rm -f -r /home/michael/linux/tools/testing/selftests/rseq/basic_test /home/michael/linux/tools/testing/selftests/rseq/basic_percpu_ops_test /home/michael/linux/tools/testing/selftests/rseq/param_test make[1]: Leaving directory '/home/michael/linux/tools/testing/selftests/rseq' make[1]: Entering directory '/home/michael/linux/tools/testing/selftests/cpu-opv'
rm -f -r /home/michael/linux/tools/testing/selftests/cpu-opv/basic_cpu_opv_test make[1]: Leaving directory '/home/michael/linux/tools/testing/selftests/cpu-opv' ~/linux/tools/testing/selftests$ cd cpu-opv/ ~/linux/tools/testing/selftests/cpu-opv$ make -j
gcc -O2 -Wall -g -I./ -I../../../../usr/include/ basic_cpu_opv_test.c
cpu-op.c cpu-op.h -o /home/michael/linux/tools/testing/selftests/cpu-opv/basic_cpu_opv_test ~/linux/tools/testing/selftests/cpu-opv$ make clean
rm -f -r /home/michael/linux/tools/testing/selftests/cpu-opv/basic_cpu_opv_test ~/linux/tools/testing/selftests/cpu-opv$ cd ../rseq/ ~/linux/tools/testing/selftests/rseq$ make -j
gcc -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/ basic_test.c
rseq.h rseq-ppc.h rseq-x86.h rseq-arm.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h -lpthread -o /home/michael/linux/tools/testing/selftests/rseq/basic_test
gcc -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/ basic_percpu_ops_test.c rseq.h rseq-ppc.h rseq-x86.h rseq-arm.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h -lpthread -o /home/michael/linux/tools/testing/selftests/rseq/basic_percpu_ops_test
gcc -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/ param_test.c
rseq.h rseq-ppc.h rseq-x86.h rseq-arm.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h -lpthread -o /home/michael/linux/tools/testing/selftests/rseq/param_test ~/linux/tools/testing/selftests/rseq$ make clean
rm -f -r /home/michael/linux/tools/testing/selftests/rseq/basic_test /home/michael/linux/tools/testing/selftests/rseq/basic_percpu_ops_test /home/michael/linux/tools/testing/selftests/rseq/param_test

cheers

tools/testing/selftests/cpu-opv/Makefile +0 -0
index 81d0596824ee..d27bd0f74deb 100644
@@ -1,13 +1,7 @@

CFLAGS += -O2 -Wall -g -I./ -I../../../../usr/include/

tools/testing/selftests/cpu-opv/Makefile +1 -6
-LDFLAGS += -lpthread
 
-TESTS = basic_cpu_opv_test
-
-all: $(TESTS)
-%: %.c cpu-op.c cpu-op.h
-       $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
+TEST_GEN_PROGS = basic_cpu_opv_test

include ../lib.mk

tools/testing/selftests/cpu-opv/Makefile +1 -2
-clean:
-       $(RM) $(TESTS)
+$(TEST_GEN_PROGS): cpu-op.c cpu-op.h
tools/testing/selftests/rseq/Makefile +0 -0
index 7f0153556b80..7f625147b7fe 100644
@@ -1,13 +1,8 @@

CFLAGS += -O2 -Wall -g -I./ -I../cpu-opv/ -I../../../../usr/include/

tools/testing/selftests/rseq/Makefile +2 -6
-LDFLAGS += -lpthread
+LDLIBS += -lpthread
 
-TESTS = basic_test basic_percpu_ops_test param_test
-
-all: $(TESTS)
-%: %.c rseq.h rseq-*.h rseq.c ../cpu-opv/cpu-op.c ../cpu-opv/cpu-op.h
-       $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
+TEST_GEN_PROGS = basic_test basic_percpu_ops_test param_test

include ../lib.mk

tools/testing/selftests/rseq/Makefile +1 -2
-clean:
-       $(RM) $(TESTS)
+$(TEST_GEN_PROGS): rseq.h rseq-*.h rseq.c ../cpu-opv/cpu-op.c

../cpu-opv/cpu-op.h

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
author maintainer
#57 lore raw

Re: [RFC PATCH for 4.15 14/14] Restartable sequences: Provide self-tests

In reply to Michael Ellerman
Quoted 33 lines · ----- On Oct 17, 2017, at 6:38 AM, Michael Ellerman [email protected] wrote:
Quoted 31 lines · Mathieu Desnoyers
Quoted 29 lines · ----- On Oct 15, 2017, at 10:51 PM, Michael Ellerman [email protected] wrote:
Quoted 11 lines · Mathieu Desnoyers

Implements two basic tests of RSEQ functionality, and one more exhaustive parameterizable test.

The first, "basic_test" only asserts that RSEQ works moderately correctly.
E.g. that:
- The CPUID pointer works
- Code infinitely looping within a critical section will eventually be interrupted.
- Critical sections are interrupted by signals.

Quoted 6 lines

"basic_percpu_ops_test" is a slightly more "realistic" variant, implementing a few simple per-cpu operations and testing their correctness.

"param_test" is a parametrizable restartable sequences test. See the "--help" output for usage.

Thanks for providing selftests :)

The Makefiles could use a little clean up:
- cpu-opv doesn't need libpthread
- you don't need to define your own rule just for building
- use TEST_GEN_PROGS to hook into the right parts of lib.mk
- .. which means you can use the clean rule in lib.mk

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I notice you didn't add rseq or cpu-opv to the list of TARGETS in tools/testing/selftests/Makefile, was that deliberate?

No, I think I just copied some other selftest which perhaps did not have those rules back then.

OK. I don't see why these tests shouldn't be part of the default test run, so here's a patch to add them.

Perfect, folded that change into my series as well.

Thanks!

Mathieu

Quoted 24 lines

cheers

tools/testing/selftests/Makefile +0 -0
index ff805643b5f7..b79ef5915c74 100644
@@ -3,6 +3,7 @@ TARGETS += breakpoints

TARGETS += capabilities
TARGETS += cpufreq
TARGETS += cpu-hotplug
+TARGETS += cpu-opv
TARGETS += efivarfs
TARGETS += exec
TARGETS += firmware

tools/testing/selftests/Makefile +0 -0
@@ -23,6 +24,7 @@ TARGETS += nsfs

TARGETS += powerpc
TARGETS += pstore
TARGETS += ptrace
+TARGETS += rseq
TARGETS += seccomp
TARGETS += sigaltstack
TARGETS += size

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#12 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

In reply to Mathieu Desnoyers

Hey,

Quoted 3 lines

So far the restrictions I see for libraries using this symbol are:
- They should never be unloaded,
- They should never be loaded with dlopen RTLD_LOCAL flag.

We talked a bit about this off-list but I wanted to state publicly that I think this model works well for our use case. Specifically,

(1) It reduces complexity by focusing on the common case -- long term we expect glibc to manage the process of using this feature and registering/deregistering threads for rseq. Unloading isn't a challenge in these situations, so why add the complexity for it?

(2) This still allows for early adopters to use rseq before there is glibc support. I believe the vast majority of real world applications meet these two criteria you've listed. If not, they can create a thin shared library that has the sole purpose of providing the weak symbol and that never gets unloaded

(3) This allows for applications to provide the __rseq_abi so that they can ensure it uses the initial_exec tls model and optimize in-application assembly code for it. This is a good optimization for server applications that tend to statically link.

If others agree with this, would it make sense to remove the concept of reference counting in the system call that defines and redefines the per-thread area? Seems like it would remove complexity.

-b

author maintainer
#13 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Quoted 19 lines · ----- On Oct 17, 2017, at 12:19 PM, Ben Maurer [email protected] wrote:

Hey,

Quoted 3 lines

So far the restrictions I see for libraries using this symbol are:
- They should never be unloaded,
- They should never be loaded with dlopen RTLD_LOCAL flag.

We talked a bit about this off-list but I wanted to state publicly that I think this model works well for our use case. Specifically,

(1) It reduces complexity by focusing on the common case -- long term we expect glibc to manage the process of using this feature and registering/deregistering threads for rseq. Unloading isn't a challenge in these situations, so why add the complexity for it?

(2) This still allows for early adopters to use rseq before there is glibc support. I believe the vast majority of real world applications meet these two criteria you've listed. If not, they can create a thin shared library that has the sole purpose of providing the weak symbol and that never gets unloaded

Quoted 4 lines

(3) This allows for applications to provide the __rseq_abi so that they can ensure it uses the initial_exec tls model and optimize in-application assembly code for it. This is a good optimization for server applications that tend to statically link.

Agreed with all the above,

Quoted 4 lines

If others agree with this, would it make sense to remove the concept of reference counting in the system call that defines and redefines the per-thread area? Seems like it would remove complexity.

I have a use-case for keeping the reference counting in place though. It's use of rseq in signal handlers.

If we have two early-adopter libraries trying to lazy-register rseq, and one of those libraries can be called within a signal handler (e.g. lttng-ust), we run into a situation where signal handler could nest on top of the first library lazy-register (test, branch, register), and race against it. So having reference counting in place allows the kernel to deal with those multi-lib use-cases atomically wrt signal handlers from a thread perspective.

And I don't want to require every early-adopter library to disable signals just in case some _other_ library would be invoked in a signal handler.

Thoughts ?

Thanks,

Mathieu

Quoted 2 lines

-b

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#14 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Quoted 2 lines

 I have a use-case for keeping the reference counting in place though. It's use of rseq in signal handlers.

Would this be solved by saying that the rseq api will return an error if you register and there's already a block registered. In this case the signal handler would register the rseq abi area just as the non-signal code is trying to do the same. The non-signal code would see this error code and realize that its job had been done for it and then go on it's way.

It would be unsafe for signal handler code to *unregister* the area, but I don't think that's necessary.

Basically we'd support a refcount of either 0 or 1, but nothing else.

If a signal handler registers the ABI area, how will it ensure the ABI is cleaned up at thread exit? I can't imagine pthread_key is signal safe.

author maintainer
#15 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Quoted 8 lines · ----- On Oct 17, 2017, at 12:41 PM, Ben Maurer [email protected] wrote:
Quoted 2 lines

 I have a use-case for keeping the reference counting in place though. It's use of rseq in signal handlers.

Would this be solved by saying that the rseq api will return an error if you register and there's already a block registered. In this case the signal handler would register the rseq abi area just as the non-signal code is trying to do the same. The non-signal code would see this error code and realize that its job had been done for it and then go on it's way.

Yes, that should work, as long as we return a specific error code, e.g. -EBUSY, to tell the caller that rseq has actually been registered.

Quoted 3 lines

It would be unsafe for signal handler code to *unregister* the area, but I don't think that's necessary.

Right.

Quoted 2 lines

Basically we'd support a refcount of either 0 or 1, but nothing else.

Yep, I'll try this out.

Quoted 3 lines

If a signal handler registers the ABI area, how will it ensure the ABI is cleaned up at thread exit? I can't imagine pthread_key is signal safe.

You have a very good point there. This highlights a signal-safety issue I have in liburcu-bp when used by lttng-ust. pthread_setspecific is indeed not listed as being signal-safe: it can perform on-demand memory allocation when a second level array is needed.

I'll have to scratch my head a bit to fix this one.

Thanks!

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#61 lore raw

Re: [RFC PATCH for 4.15 14/14] Restartable sequences: Provide self-tests

In reply to Mathieu Desnoyers
Quoted 37 lines · Mathieu Desnoyers
Quoted 35 lines · ----- On Oct 17, 2017, at 6:36 AM, Michael Ellerman…
Quoted 27 lines · Mathieu Desnoyers

Hi Michael,

With your changes integrated, both rseq and cpu-opv selftests fail to build if I pass e.g. -j32 to make.

cd tools/testing/selftests/cpu-opv

efficios@compudjdev:~/git/linux-percpu-dev/tools/testing/selftests/cpu-opv$ make
clean; make
rm -f -r
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test
gcc -O2 -Wall -g -I./ -I../../../../usr/include/    basic_cpu_opv_test.c
cpu-op.c cpu-op.h   -o basic_cpu_opv_test
efficios@compudjdev:~/git/linux-percpu-dev/tools/testing/selftests/cpu-opv$ make
clean; make -j32
rm -f -r
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test
gcc -O2 -Wall -g -I./ -I../../../../usr/include/    basic_cpu_opv_test.c
cpu-op.c cpu-op.h   -o basic_cpu_opv_test
gcc -O2 -Wall -g -I./ -I../../../../usr/include/    basic_cpu_opv_test.c  -o
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test
/tmp/ccDthnqM.o: In function `test_memcpy_op':
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:364:
undefined reference to `cpu_op_get_current_cpu'
/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test.c:365:
undefined reference to `cpu_opv'

...

Quoted 5 lines

make: *** [/home/efficios/git/linux-percpu-dev/tools/testing/selftests/cpu-opv/basic_cpu_opv_test] Error 1
make: *** Waiting for unfinished jobs....

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Any idea what is going on here ?

Ugh sorry, yes.

New patch below should fix it. Tested with -j:

Perfect, folding it into my series.

Thanks.

Quoted 1 line

I see that the "all" target was redundant here.

Yeah.

Unfortunately lib.mk has grown a bit more complicated than I would like, including defining the all target.

It does make for nice short Makefiles for individual test directories, but it's not all that easy to use if you're not familiar with it [or even if you are, as I demostrated here :)]

cheers

#16 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

In reply to Ben Maurer
Quoted 6 lines · Ben Maurer

Hey,

Quoted 3 lines

So far the restrictions I see for libraries using this symbol are:
- They should never be unloaded,
- They should never be loaded with dlopen RTLD_LOCAL flag.

Quoted 7 lines

We talked a bit about this off-list but I wanted to state publicly that I think this model works well for our use case. Specifically,

(1) It reduces complexity by focusing on the common case -- long term we expect glibc to manage the process of using this feature and registering/deregistering threads for rseq. Unloading isn't a challenge in these situations, so why add the complexity for it?

You never install a new version of glibc on a running system, and expect everything to keep running successfully? Breaking that would not be good...

thanks,

greg k-h

author maintainer
#17 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Quoted 10 lines · ----- On Oct 18, 2017, at 2:22 AM, Greg Kroah-Hartman…
Quoted 9 lines · Ben Maurer

Hey,

Quoted 3 lines

So far the restrictions I see for libraries using this symbol are:
- They should never be unloaded,
- They should never be loaded with dlopen RTLD_LOCAL flag.

We talked a bit about this off-list but I wanted to state publicly that I think this model works well for our use case. Specifically,

Quoted 8 lines
Quoted 4 lines

(1) It reduces complexity by focusing on the common case -- long term we expect glibc to manage the process of using this feature and registering/deregistering threads for rseq. Unloading isn't a challenge in these situations, so why add the complexity for it?

You never install a new version of glibc on a running system, and expect everything to keep running successfully? Breaking that would not be good...

If we share the __rseq_abi TLS weak symbol between glibc, applications, and early-adopter libraries, we just need those early adopters to check whether the TLS is already registered (cpu_id field >= 0), and don't bother doing their lazy registration if it's already been done for them (either by glibc or by the application).

If either the application or a lazy-registering library gets a EBUSY errno from rseq registration, it can consider that another library already performed the registration for them (probably glibc).

As long as early adopter libraries don't expect to be sole users of __rseq_abi, upgrading to newer glibc should work fine.

But this means we need to get all early adopters to get it right from the get-go, or things could break when you compose them.

Thoughts ?

Thanks,

Mathieu

Quoted 4 lines

thanks,

greg k-h

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#29 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

In reply to Mathieu Desnoyers
Quoted 6 lines

The layout of struct rseq_cs is as follows:

start_ip
Instruction pointer address of the first instruction of the sequence of consecutive assembly instructions.

Quoted 8 lines

post_commit_ip
Instruction pointer address after the last  instruction  of the sequence of consecutive assembly instructions.

  abort_ip
Instruction  pointer  address  where  to move the execution flow in case of abort of the sequence of consecutive assem‐ bly instructions.

Really minor performance performance thought here.

1) In the kernel at context switch time you'd need code like:

if (ip >= start_ip && ip <= post_commit_ip)

This branch would be hard to predict because most instruction pointers would be either before or after. If post_commit_ip were relative to start_ip you could do this:

if (ip - start_ip <= post_commit_offset)

which is a single branch that would be more predictable.

2) In a shared library a rseq_cs structure would have to be relocated at runtime because at compilation time the final address of the library wouldn't be known. I'm not sure if this is important enough to address, but it could be solved by making the pointers relative to the address of rseq_cs. But this would make for an uglier API.

author maintainer
#30 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Quoted 21 lines · ----- On Oct 18, 2017, at 12:41 PM, Ben Maurer [email protected] wrote:
Quoted 6 lines

The layout of struct rseq_cs is as follows:

start_ip
Instruction pointer address of the first instruction of the sequence of consecutive assembly instructions.

Quoted 8 lines

post_commit_ip
Instruction pointer address after the last  instruction  of the sequence of consecutive assembly instructions.

  abort_ip
Instruction  pointer  address  where  to move the execution flow in case of abort of the sequence of consecutive assem‐ bly instructions.

Really minor performance performance thought here.

1) In the kernel at context switch time you'd need code like:

if (ip >= start_ip && ip <= post_commit_ip)

Quoted 7 lines

This branch would be hard to predict because most instruction pointers would be either before or after. If post_commit_ip were relative to start_ip you could do this:

if (ip - start_ip <= post_commit_offset)

which is a single branch that would be more predictable.

The actual context switch code only sets the "t->rseq_preempt" flags and TIF_NOTIFY_RESUME. The user-accesses happen when returning to user-space with TIF_NOTIFY_RESUME set.

Indeed, we can expect most context switch out of a registered rseq thread to trigger one __rseq_handle_notify_resume on return to user-space for that thread.

As you point out, the "common" case is *not* nested over a critical section. This means t->rseq->rseq_cs is NULL. This effectively means post_commit_ip and start_ip are NULL in rseq_ip_fixup when compared to the current ip.

The check is currently implemented like this:

        /* Handle potentially not being within a critical section. */
        if ((void __user *)instruction_pointer(regs) >= post_commit_ip ||
                        (void __user *)instruction_pointer(regs) < start_ip)
                return 1;

So if non-nested over c.s., the first branch is ip >= NULL, which turns out to be true, and we therefore return 1 from rseq_ip_fixup.

I suspect that we'd need to cast those pointers to (unsigned long) to be strictly C standard compliant.

If we instead use "post_commit_offset" relative to start_ip, a non-nested common case would have start_ip = NULL, post_commit_offset = 0. The check you propose for not being nested over a c.s. would look like:

if (!((long)ip - (long)start_ip <= (long)post_commit_offset))
return 1;

This introduces an issue here: if "ip" is lower than "start_ip", we can incorrectly think we are in a critical section, when we are in fact not.

With the previous approach proposed by Paul Turner, this was not an issue, because he was setting the equivalent of the rseq_cs pointer back to NULL at the end of the assembly fast-path. However, I have a fast-path optimization that only sets the rseq_cs pointer at the beginning of the fast-path, without clearing it afterward. It's up to the following critical section to overwrite the rseq_cs pointer, or to the kernel to set it back to NULL if it finds out that it is preempting/delivering a signal over an instruction pointer outside of the current rseq_cs start_ip/post_commit_ip range (lazy clear).

Moreover, this modification would add a subtraction on the common case (ip - start_ip), and makes the ABI slightly uglier.

Quoted 6 lines

2) In a shared library a rseq_cs structure would have to be relocated at runtime because at compilation time the final address of the library wouldn't be known. I'm not sure if this is important enough to address, but it could be solved by making the pointers relative to the address of rseq_cs. But this would make for an uglier API.

If I understand well, you are proposing to speed up .so load time by means of removing relocations of pointers within rseq_cs, done by making those relative to the rseq_cs address.

So the downside here is extra arithmetic operations on resume to userspace (__rseq_handle_notify_resume): the kernel would have to calculate the offset of start_ip and post_commit_ip from the address of rseq_cs. Sure, we're only talking about two additions there, but I don't think marginally speeding up library load justifies extra work in that kernel path, nor the uglier ABI.

Thoughts ?

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
author maintainer
#31 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Speaking of optimization, I think the rseq.c helper library (and eventually glibc) should define the __rseq_abi TLS variable with __attribute__((tls_model("initial-exec"))). It provides faster, and signal-safe, accesses to the TLS variable from libraries.

The idea you were suggesting where the application could override the glibc symbol with its own just ends up enforcing an initial-exec model, but it looks like we can do this directly from the library.

Thoughts ?

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#32 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

* Mathieu Desnoyers:

Quoted 10 lines

Speaking of optimization, I think the rseq.c helper library (and eventually glibc) should define the __rseq_abi TLS variable with __attribute__((tls_model("initial-exec"))). It provides faster, and signal-safe, accesses to the TLS variable from libraries.

The idea you were suggesting where the application could override the glibc symbol with its own just ends up enforcing an initial-exec model, but it looks like we can do this directly from the library.

This really depends on how the programming model turns out once multiple libraries are involved. I think it's premature to discuss such details.

#33 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

In reply to Mathieu Desnoyers
Quoted 6 lines

if (!((long)ip - (long)start_ip <= (long)post_commit_offset)) return 1;

This introduces an issue here: if "ip" is lower than "start_ip", we can incorrectly think we are in a critical section, when we are in fact not.

This shouldn't be an issue if we used unsigned numbers. Eg if start_ip is X and post_commit_offset is L, then (ip - X <= L) means that if ip is less than X ip - X will be signed, which will become a large unsigned value.

Quoted 3 lines

or to the kernel to set it back to NULL if it finds out that it is preempting/delivering a signal over an instruction pointer outside of the current rseq_cs start_ip/post_commit_ip range (lazy clear).

I see, lazy clear makes sense. Still, if during most execution periods the user code enters some rseq section (likely if rseq is used for something like malloc) on every context switch this code will have to be run.

Quoted 2 lines

Moreover, this modification would add a subtraction on the common case (ip - start_ip), and makes the ABI slightly uglier.

We could benchmark it but the subtraction should be similar in cost to the extra comparison but reducing the number of branches seems like it will help as well. FWIW GCC attempts to translate this kind of sequence to a subtract and compare: https://godbolt.org/g/5DGLvo.

I agree the ABI is uglier, but since we're mucking with every context switch I thought I'd point it out.

Quoted 3 lines

If I understand well, you are proposing to speed up .so load time by means of removing relocations of pointers within rseq_cs, done by making those relative to the rseq_cs address.

Yeah, I think this may be overkill as optimization.

author maintainer
#34 lore raw

Re: [RFC PATCH v9 for 4.15 01/14] Restartable sequences system call

Quoted 22 lines · ----- On Oct 23, 2017, at 7:30 PM, Ben Maurer [email protected] wrote:
Quoted 6 lines

if (!((long)ip - (long)start_ip <= (long)post_commit_offset)) return 1;

This introduces an issue here: if "ip" is lower than "start_ip", we can incorrectly think we are in a critical section, when we are in fact not.

This shouldn't be an issue if we used unsigned numbers. Eg if start_ip is X and post_commit_offset is L, then (ip - X <= L) means that if ip is less than X ip
- X will be signed, which will become a large unsigned value.

Quoted 3 lines

or to the kernel to set it back to NULL if it finds out that it is preempting/delivering a signal over an instruction pointer outside of the current rseq_cs start_ip/post_commit_ip range (lazy clear).

I see, lazy clear makes sense. Still, if during most execution periods the user code enters some rseq section (likely if rseq is used for something like malloc) on every context switch this code will have to be run.

Quoted 2 lines

Moreover, this modification would add a subtraction on the common case (ip - start_ip), and makes the ABI slightly uglier.

Quoted 7 lines

We could benchmark it but the subtraction should be similar in cost to the extra comparison but reducing the number of branches seems like it will help as well. FWIW GCC attempts to translate this kind of sequence to a subtract and compare:
https://godbolt.org/g/5DGLvo.

I agree the ABI is uglier, but since we're mucking with every context switch I thought I'd point it out.

Thanks for following up on this. I did not initially realize the importance of doing the unsigned comparison. I've pushed a commit in my private dev branch implementing your suggestion.

https://github.com/compudj/linux-percpu-dev/commit/4cf8e9104636b51741c0118f2c88519e3acab7aa

Thanks!

Mathieu

Quoted 6 lines
Quoted 4 lines

If I understand well, you are proposing to speed up .so load time by means of removing relocations of pointers within rseq_cs, done by making those relative to the rseq_cs address.

Yeah, I think this may be overkill as optimization.

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com