Restartable Sequences (rseq) is a Linux kernel feature (introduced in v4.18) that enables high-performance userspace operations on per-CPU data without requiring atomic instructions or traditional locking. Each thread registers a struct rseq, and the kernel ensures that if a thread is preempted or interrupted while inside a critical section, it is “restarted” by jumping to a predefined abort handler.
Checkpointing and restoring rseq is exceptionally delicate because the kernel’s rseq state is tightly coupled with process execution.
rseq_handle_notify_resume hook may be triggered. This would cause the kernel to “fix up” the rseq state, clearing critical section pointers in userspace memory and losing the very state CRIU needs to capture.CRIU provides robust support for rseq, ensuring that threads interrupted within a critical section correctly restart after restoration.
CRIU captures the rseq configuration without disturbing the thread’s execution state:
struct rseq registered for each thread.PTRACE_PEEKDATA to read the struct rseq and struct rseq_cs (critical section descriptor) directly from the outside while the task is frozen.rseq_cs pointer within the struct rseq, CRIU identifies if a thread was in the middle of a sequence at the time of the snapshot.The restoration process involves two critical rseq-related steps:
cpu_id field in memory that has been unmapped, resulting in an immediate segmentation fault.rseq() system call for each thread. It re-registers the original struct rseq at its original address.rseq_cs pointer is restored as part of the thread’s memory, the kernel will detect the active critical section upon the first resumption and automatically trigger the application’s restart/abort logic, ensuring data integrity.