1 .\" Copyright 2015-2020 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH RSEQ 2 2020-06-05 "Linux" "Linux Programmer's Manual"
27 rseq \- Restartable sequences and cpu number cache
30 .B #include <linux/rseq.h>
32 .BI "int rseq(struct rseq * " rseq ", uint32_t " rseq_len ", int " flags ", uint32_t " sig ");
37 ABI accelerates user-space operations on per-cpu data by defining a
38 shared data structure ABI between each user-space thread and the kernel.
40 It allows user-space to perform update operations on per-cpu data
41 without requiring heavy-weight atomic operations.
43 The term CPU used in this documentation refers to a hardware execution
46 Restartable sequences are atomic with respect to preemption (making it
47 atomic with respect to other threads running on the same CPU), as well
48 as signal delivery (user-space execution contexts nested over the same
49 thread). They either complete atomically with respect to preemption on
50 the current CPU and signal delivery, or they are aborted.
52 It is suited for update operations on per-cpu data.
54 It can be used on data structures shared between threads within a
55 process, and on data structures shared between threads across different
59 Some examples of operations that can be accelerated or improved
62 Memory allocator per-cpu free-lists,
64 Querying the current CPU number,
66 Incrementing per-CPU counters,
68 Modifying data protected by per-CPU spinlocks,
70 Inserting/removing elements in per-CPU linked-lists,
72 Writing/reading per-CPU ring buffers content.
74 Accurately reading performance monitoring unit counters
75 with respect to thread migration.
78 Restartable sequences must not perform system calls. Doing so may result
79 in termination of the process by a segmentation fault.
84 argument is a pointer to the thread-local rseq structure to be shared
85 between kernel and user-space.
92 .B Structure alignment
93 This structure is aligned on 32-byte boundary.
96 This structure is extensible. Its size is passed as parameter to the
104 Optimistic cache of the CPU number on which the current thread is
105 running. Its value is guaranteed to always be a possible CPU number,
106 even when rseq is not initialized. The value it contains should always
107 be confirmed by reading the cpu_id field.
109 This field is an optimistic cache in the sense that it is always
110 guaranteed to hold a valid CPU number in the range [ 0 ..
111 nr_possible_cpus - 1 ]. It can therefore be loaded by user-space and
112 used as an offset in per-cpu data structures without having to
113 check whether its value is within the valid bounds compared to the
114 number of possible CPUs in the system.
116 For user-space applications executed on a kernel without rseq support,
117 the cpu_id_start field stays initialized at 0, which is indeed a valid
118 CPU number. It is therefore valid to use it as an offset in per-cpu data
119 structures, and only validate whether it's actually the current CPU
120 number by comparing it with the cpu_id field within the rseq critical
121 section. If the kernel does not provide rseq support, that cpu_id field
122 stays initialized at -1, so the comparison always fails, as intended.
124 It is then up to user-space to use a fall-back mechanism, considering
125 that rseq is not available.
130 Cache of the CPU number on which the current thread is running.
136 The rseq_cs field is a pointer to a struct rseq_cs. Is is NULL when no
137 rseq assembly block critical section is active for the current thread.
138 Setting it to point to a critical section descriptor (struct rseq_cs)
139 marks the beginning of the critical section.
144 Flags indicating the restart behavior for the current thread. This is
145 mainly used for debugging purposes. Can be either:
147 RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT
149 RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL
151 RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE
157 version 0 is as follows:
159 .B Structure alignment
160 This structure is aligned on 32-byte boundary.
163 This structure has a fixed size of 32 bytes.
170 Version of this structure.
175 Flags indicating the restart behavior of this structure. Can be
178 RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT
180 RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL
182 RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE
186 Instruction pointer address of the first instruction of the sequence of
187 consecutive assembly instructions.
191 .I post_commit_offset
192 Offset (from start_ip address) of the address after the last instruction
193 of the sequence of consecutive assembly instructions.
198 Instruction pointer address where to move the execution flow in case of
199 abort of the sequence of consecutive assembly instructions.
205 argument is the size of the
212 argument is 0 for registration, and
213 .IR RSEQ_FLAG_UNREGISTER
219 argument is the 32-bit signature to be expected before the abort
223 A single library per process should keep the rseq structure in a
224 thread-local storage variable.
227 field should be initialized to -1, and the
229 field should be initialized to a possible CPU value (typically 0).
232 Each thread is responsible for registering and unregistering its rseq
233 structure. No more than one rseq structure address can be registered
234 per thread at a given time.
237 Memory of a registered rseq object must not be freed before the thread
238 exits. Reclaim of rseq object's memory must only be done after either an
239 explicit rseq unregistration is performed or after the thread exits. Keep
240 in mind that the implementation of the Thread-Local Storage (C language
241 __thread) lifetime does not guarantee existence of the TLS area up until
245 In a typical usage scenario, the thread registering the rseq
246 structure will be performing loads and stores from/to that structure. It
247 is however also allowed to read that structure from other threads.
248 The rseq field updates performed by the kernel provide relaxed atomicity
249 semantics, which guarantee that other threads performing relaxed atomic
250 reads of the cpu number cache will always observe a consistent value.
253 A return value of 0 indicates success. On error, \-1 is returned, and
255 is set appropriately.
262 contains an invalid value, or
264 contains an address which is not appropriately aligned, or
266 contains a size that does not match the size received on registration.
271 system call is not implemented by this kernel.
275 is an invalid address.
278 Restartable sequence is already registered for this thread.
283 argument on unregistration does not match the signature received
289 system call was added in Linux 4.18.
297 .BR sched_getcpu (3) ,