kernel/rseq.c
Source file repositories/reference/linux-study-clean/kernel/rseq.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/rseq.c- Extension
.c- Size
- 25775 bytes
- Lines
- 890
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- Inferred role
- Core OS: syscall or user/kernel boundary
- Status
- core implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Defines or participates in a user/kernel boundary; inspect argument validation, copy_from_user/copy_to_user, credentials, and dispatch target.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/debugfs.hlinux/hrtimer.hlinux/percpu.hlinux/prctl.hlinux/ratelimit.hlinux/rseq_entry.hlinux/sched.hlinux/syscalls.hlinux/uaccess.hlinux/types.hlinux/rseq.hasm/ptrace.htrace/events/rseq.h
Detected Declarations
syscall rseqsyscall rseq_slice_yieldstruct slice_timerfunction rseq_control_debugfunction rseq_setup_debugfunction __rseq_trace_updatefunction __rseq_trace_ip_fixupfunction rseq_stats_showfunction for_each_possible_cpufunction rseq_stats_openfunction rseq_stats_initfunction rseq_stats_initfunction rseq_debug_writefunction rseq_debug_openfunction rseq_debugfs_initfunction rseq_handle_csfunction rseq_slowpath_update_usrfunction __rseq_handle_slowpathfunction __rseq_signal_deliverfunction __rseq_debug_syscall_returnfunction rseq_syscallfunction rseq_reset_idsfunction rseq_registerfunction scoped_user_write_accessfunction rseq_unregisterfunction rseq_reregisterfunction rseq_length_validfunction rseq_slice_expiredfunction __rseq_arm_slice_extension_timerfunction rseq_cancel_slice_extension_timerfunction rseq_slice_set_need_reschedfunction rseq_slice_validate_ctrlfunction rseq_slice_yieldfunction set_tsk_need_reschedfunction rseq_slice_extension_prctlfunction rseq_slice_ext_showfunction rseq_slice_ext_writefunction rseq_slice_ext_openfunction rseq_slice_ext_initfunction rseq_slice_cmdlinefunction rseq_slice_initfunction for_each_possible_cpufunction rseq_slice_ext_initmodule init rseq_slice_init
Annotated Snippet
SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32, rseq_len, int, flags, u32, sig)
{
if (flags & RSEQ_FLAG_UNREGISTER)
return rseq_unregister(rseq, rseq_len, flags, sig);
if (unlikely(flags & ~RSEQ_FLAGS_SUPPORTED))
return -EINVAL;
if (current->rseq.usrptr)
return rseq_reregister(rseq, rseq_len, sig);
if (!rseq_length_valid(rseq, rseq_len))
return -EINVAL;
return rseq_register(rseq, rseq_len, flags, sig);
}
#ifdef CONFIG_RSEQ_SLICE_EXTENSION
struct slice_timer {
struct hrtimer timer;
void *cookie;
};
static const unsigned int rseq_slice_ext_nsecs_min = 5 * NSEC_PER_USEC;
static const unsigned int rseq_slice_ext_nsecs_max = 50 * NSEC_PER_USEC;
unsigned int rseq_slice_ext_nsecs __read_mostly = rseq_slice_ext_nsecs_min;
static DEFINE_PER_CPU(struct slice_timer, slice_timer);
DEFINE_STATIC_KEY_TRUE(rseq_slice_extension_key);
/*
* When the timer expires and the task is still in user space, the return
* from interrupt will revoke the grant and schedule. If the task already
* entered the kernel via a syscall and the timer fires before the syscall
* work was able to cancel it, then depending on the preemption model this
* will either reschedule on return from interrupt or in the syscall work
* below.
*/
static enum hrtimer_restart rseq_slice_expired(struct hrtimer *tmr)
{
struct slice_timer *st = container_of(tmr, struct slice_timer, timer);
/*
* Validate that the task which armed the timer is still on the
* CPU. It could have been scheduled out without canceling the
* timer.
*/
if (st->cookie == current && current->rseq.slice.state.granted) {
rseq_stat_inc(rseq_stats.s_expired);
set_need_resched_current();
}
return HRTIMER_NORESTART;
}
bool __rseq_arm_slice_extension_timer(void)
{
struct slice_timer *st = this_cpu_ptr(&slice_timer);
struct task_struct *curr = current;
lockdep_assert_irqs_disabled();
/*
* This check prevents a task, which got a time slice extension
* granted, from exceeding the maximum scheduling latency when the
* grant expired before going out to user space. Don't bother to
* clear the grant here, it will be cleaned up automatically before
* going out to user space after being scheduled back in.
*/
if ((unlikely(curr->rseq.slice.expires < ktime_get_mono_fast_ns()))) {
set_need_resched_current();
return true;
}
/*
* Store the task pointer as a cookie for comparison in the timer
* function. This is safe as the timer is CPU local and cannot be
* in the expiry function at this point.
*/
st->cookie = curr;
hrtimer_start(&st->timer, curr->rseq.slice.expires, HRTIMER_MODE_ABS_PINNED_HARD);
/* Arm the syscall entry work */
set_task_syscall_work(curr, SYSCALL_RSEQ_SLICE);
return false;
}
static void rseq_cancel_slice_extension_timer(void)
{
struct slice_timer *st = this_cpu_ptr(&slice_timer);
/*
* st->cookie can be safely read as preemption is disabled and the
Annotation
- Immediate include surface: `linux/debugfs.h`, `linux/hrtimer.h`, `linux/percpu.h`, `linux/prctl.h`, `linux/ratelimit.h`, `linux/rseq_entry.h`, `linux/sched.h`, `linux/syscalls.h`.
- Detected declarations: `syscall rseq`, `syscall rseq_slice_yield`, `struct slice_timer`, `function rseq_control_debug`, `function rseq_setup_debug`, `function __rseq_trace_update`, `function __rseq_trace_ip_fixup`, `function rseq_stats_show`, `function for_each_possible_cpu`, `function rseq_stats_open`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- Implementation status: core implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.