Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

19.6 Context Tracking Initialization

The initialization of the context tracking infrastructure is explicitly enabled via the compilation configuration switch CONFIG_CONTEXT_TRACKING_FORCE. If context tracking capabilities are omitted from the build options, the function context_tracking_init() resolves to an empty function block. When context tracking is active, context_tracking_init() bootstraps the kernel's core context tracking mechanism, which is engineered to dynamically detect and differentiate whether a CPU core currently executes within kernel space, user space, or an idle state, thereby providing foundational structural support for advanced systems including RCU, NOHZ, and tickless states. This routine is triggered during the early stages of kernel boot to guarantee that the context tracking infrastructure is fully operational before any user-space processes are authorized to execute.

The function context_tracking_init() is declared within the source file git/kernel/context_tracking.c, and its definition is implemented as follows:

void __init context_tracking_init(void)
{
	int cpu;
	for_each_possible_cpu(cpu)
		context_tracking_cpu_set(cpu);
}

The underlying routine that physically activates context tracking is context_tracking_cpu_set()性能, which is implemented as:

void __init context_tracking_cpu_set(int cpu)
{
	static __initdata bool initialized = false;
	if (!per_cpu(context_tracking.active, cpu)) {
		per_cpu(context_tracking.active, cpu) = true;
		static_branch_inc(&context_tracking_key);
	}
	if (initialized)
		return;
#ifdef CONFIG_HAVE_TIF_NOHZ
	set_tsk_thread_flag(&init_task, TIF_NOHZ);
#endif
	WARN_ON_ONCE(!tasklist_empty());
	initialized = true;
}

This snippet details how the Linux kernel activates the context tracking machinery. It first utilizes the per_cpu() macro helper to access an independent private variable named context_tracking.active bound to a specific CPU core, marking that target core's status as "context tracking active." Following this state modification, it leverages static jump label mechanics via static_branch_inc() to directly rewrite the machine code instructions in memory, substituting a standard NOP placeholder with a concrete hardware branch instruction to energize context tracking. This represents a critical microarchitectural performance optimization path powered by the static keys infrastructure. As long as at least one CPU core triggers a tracking session, this global switch increments by 1.

Only when this reference counter evaluates to a value greater than 0 does the kernel hot-patch its low-level assembly entry vectors (such as system call exception handlers) via runtime code modification to execute the actual tracking logic loops. If the reference count rests at 0, the engine completely steps over these logic passes, eliminating runtime performance overhead. Because the system still resides in its early boot phase at this checkpoint, only the master init task (PID 0, the idle thread context) is active in memory. By invoking set_tsk_thread_flag(&init_task, TIF_NOHZ), the routine applies the TIF_NOHZ flag to init_task; consequently, all subsequent child processes spawned via future fork() cycles automatically inherit this thread attribute. This setup guarantees that whenever a process transitions across the kernel space boundary via an entry or exit path, the system audits its configuration to determine whether a context-switching notification must be raised.

Copyright © 2026 Guangzhou eSmart Information Technology Co., Ltd. All Rights Reserved.
All content in this book is copyrighted by the author. For personal learning and exchange only. Commercial use without authorization is strictly prohibited. Email:

✉ 3348194517@qq.com | hai616878@gmail.com