23.3 Legacy Profiling Subsystem Initialization
The routine profile_init() bootstraps the legacy kernel profiling subsystem. The technical distinction between a profiling tool and a contemporary performance monitoring subsystem (Perf) is that the legacy profiling tool represents an exceptionally lightweight, single-purpose statistical counter matrix designed to isolate hot spots across the kernel's text segment (.text). The profiling tool analyzes hot functions by standardizing execution sampling over the CPU instruction pointer. Although it has been widely superseded by the perf subsystem, this framework is maintained during early system bootstrap to support legacy debugging utilities.
The function is located within the core source tree file git/kernel/profile.c, and its definition is implemented as follows:
int __ref profile_init(void)
{
int buffer_bytes;
if (!prof_on)
return 0;
prof_len = (_etext - _stext) >> prof_shift;
buffer_bytes = prof_len*sizeof(atomic_t);
if (!alloc_cpumask_var(&prof_cpu_mask, GFP_KERNEL))
return -ENOMEM;
cpumask_copy(prof_cpu_mask, cpu_possible_mask);
prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL|__GFP_NOWARN);
if (prof_buffer)
return 0;
prof_buffer = alloc_pages_exact(
buffer_bytes,GFP_KERNEL|__GFP_ZERO|__GFP_NOWARN);
if (prof_buffer)
return 0;
prof_buffer = vzalloc(buffer_bytes);
if (prof_buffer)
return 0;
free_cpumask_var(prof_cpu_mask);
return -ENOMEM;
}
This source sequence outlines the structural memory allocation process for the legacy profiling infrastructure within the Linux kernel. Its algorithmic core calculates the literal footprint of the compiled kernel image binary text to reserve a block of continuous memory configured as an array of atomic counters.
If this specific tracking feature is disabled via the boot properties (!prof_on, the system default state), the function exits immediately and returns 0. The internal execution logic of this routine is processed only when the boot parameters explicitly include the option profile=N (where N dictates the scaling bit-shift constant prof_shift).
If the profiling mechanism is active, the routine first computes the absolute profiling length threshold prof_len. The calculation _etext - _stext isolates the exact byte width spanning the entire kernel text segment. By right-shifting this delta by prof_shift bits, the kernel's instruction address canvas is partitioned into multiple discrete address bins, where each unique bin maps to a dedicated tracking counter.
After resolving this profiling length, the function calls alloc_cpumask_var() to dynamic allocate memory for a CPU mask variable named prof_cpu_mask, and initializes this mask with the properties of cpu_possible_mask. prof_cpu_mask serves as the authorization whitelist enabling the kernel to track precisely which CPU cores participate in legacy profiling passes.
Upon successfully allocating memory for prof_cpu_mask, the function manages the allocation parameters for the primary statistical tracking array prof_buffer. Every individual array index cell within prof_buffer maps to a distinct instruction address bin, and each slot aggregates the cumulative number of times the CPU core was interrupted by a scheduling clock tick exception while executing code within that address range. To guarantee that the system can secure a massive block of memory during this early bootstrap stage, it first attempts to extract a zero-filled allocation straight from the standard SLAB/SLUB allocators via kzalloc(). If this pass fails, it falls back to harvest physically contiguous page frames via alloc_pages_exact(). If this physical allocation pass also fails, the routine dispatches vzalloc() to claim a memory allocation that is virtually contiguous even if its backing physical page frames are fragmented across memory nodes.
If the allocator completely fails to secure memory for prof_buffer, the routine executes a cleanup path via free_cpumask_var() to release the memory previously allocated to hold the prof_cpu_mask structure, returning an out-of-memory error (-ENOMEM).
The data framework initialized by profile_init() represents a rudimentary, flat counter array. Every time a hardware clock event device asserts a scheduling tick interrupt, the exception flow handler dispatches profile_tick(), evaluates the active instruction pointer address being processed by the CPU core, resolves which address bin captures that pointer, and increments the atomic tracking counter mapped to that bin by 1. This legacy approach is strictly constrained to auditing kernel-space execution, remaining entirely blind to user-space threads, and tracks exclusively instruction address execution hot spots, remaining completely unequipped to monitor advanced microarchitectural metrics such as CPU cache misses or branch prediction failures.
Across contemporary Linux development environments, the perf subsystem has comprehensively replaced this legacy architecture. Consequently, profile_init() is maintained primarily to preserve backward compilation compatibility with older verification tooling.