kernel/context_tracking.c
Source file repositories/reference/linux-study-clean/kernel/context_tracking.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/context_tracking.c- Extension
.c- Size
- 22891 bytes
- Lines
- 712
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- Inferred role
- Core OS: exported/initcall integration point
- Status
- integration 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.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/context_tracking.hlinux/rcupdate.hlinux/sched.hlinux/hardirq.hlinux/export.hlinux/kprobes.htrace/events/rcu.htrace/events/context_tracking.h
Detected Declarations
function rcu_task_exitfunction rcu_task_enterfunction ct_kernel_exit_statefunction ct_kernel_enter_statefunction ct_kernel_exitfunction ct_kernel_enterfunction ct_nmi_exitfunction ct_nmi_enterfunction periodfunction irq_enterfunction ct_idle_exitfunction irq_enterfunction irq_enterfunction ct_irq_enterfunction ct_irq_exitfunction ct_kernel_exitfunction context_tracking_recursion_enterfunction context_tracking_recursion_exitfunction __ct_user_enterfunction exception_exitfunction local_irq_restorefunction user_enter_callablefunction __ct_user_exitfunction local_irq_savefunction user_exit_callablefunction ct_cpu_track_userfunction context_tracking_initexport context_trackingexport ct_idle_enterexport ct_idle_exitexport context_tracking_keyexport __ct_user_enterexport ct_user_enterexport __ct_user_exitexport ct_user_exit
Annotated Snippet
static __always_inline void ct_kernel_exit(bool user, int offset) { }
static __always_inline void ct_kernel_enter(bool user, int offset) { }
#endif /* #ifdef CONFIG_CONTEXT_TRACKING_IDLE */
#ifdef CONFIG_CONTEXT_TRACKING_USER
#define CREATE_TRACE_POINTS
#include <trace/events/context_tracking.h>
DEFINE_STATIC_KEY_FALSE_RO(context_tracking_key);
EXPORT_SYMBOL_GPL(context_tracking_key);
static noinstr bool context_tracking_recursion_enter(void)
{
int recursion;
recursion = __this_cpu_inc_return(context_tracking.recursion);
if (recursion == 1)
return true;
WARN_ONCE((recursion < 1), "Invalid context tracking recursion value %d\n", recursion);
__this_cpu_dec(context_tracking.recursion);
return false;
}
static __always_inline void context_tracking_recursion_exit(void)
{
__this_cpu_dec(context_tracking.recursion);
}
/**
* __ct_user_enter - Inform the context tracking that the CPU is going
* to enter user or guest space mode.
*
* @state: userspace context-tracking state to enter.
*
* This function must be called right before we switch from the kernel
* to user or guest space, when it's guaranteed the remaining kernel
* instructions to execute won't use any RCU read side critical section
* because this function sets RCU in extended quiescent state.
*/
void noinstr __ct_user_enter(enum ctx_state state)
{
struct context_tracking *ct = this_cpu_ptr(&context_tracking);
lockdep_assert_irqs_disabled();
/* Kernel threads aren't supposed to go to userspace */
WARN_ON_ONCE(!current->mm);
if (!context_tracking_recursion_enter())
return;
if (__ct_state() != state) {
if (ct->active) {
/*
* At this stage, only low level arch entry code remains and
* then we'll run in userspace. We can assume there won't be
* any RCU read-side critical section until the next call to
* user_exit() or ct_irq_enter(). Let's remove RCU's dependency
* on the tick.
*/
if (state == CT_STATE_USER) {
instrumentation_begin();
trace_user_enter(0);
vtime_user_enter(current);
instrumentation_end();
}
/*
* Other than generic entry implementation, we may be past the last
* rescheduling opportunity in the entry code. Trigger a self IPI
* that will fire and reschedule once we resume in user/guest mode.
*/
rcu_irq_work_resched();
/*
* Enter RCU idle mode right before resuming userspace. No use of RCU
* is permitted between this call and rcu_eqs_exit(). This way the
* CPU doesn't need to maintain the tick for RCU maintenance purposes
* when the CPU runs in userspace.
*/
ct_kernel_exit(true, CT_RCU_WATCHING + state);
/*
* Special case if we only track user <-> kernel transitions for tickless
* cputime accounting but we don't support RCU extended quiescent state.
* In this we case we don't care about any concurrency/ordering.
*/
if (!IS_ENABLED(CONFIG_CONTEXT_TRACKING_IDLE))
raw_atomic_set(&ct->state, state);
Annotation
- Immediate include surface: `linux/context_tracking.h`, `linux/rcupdate.h`, `linux/sched.h`, `linux/hardirq.h`, `linux/export.h`, `linux/kprobes.h`, `trace/events/rcu.h`, `trace/events/context_tracking.h`.
- Detected declarations: `function rcu_task_exit`, `function rcu_task_enter`, `function ct_kernel_exit_state`, `function ct_kernel_enter_state`, `function ct_kernel_exit`, `function ct_kernel_enter`, `function ct_nmi_exit`, `function ct_nmi_enter`, `function period`, `function irq_enter`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- Implementation status: integration implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.