7.1 Setting the Initialization Task Stack-End Magic Number
The Linux operating system manages all transactional activities via an abstraction model known as tasks, with each individual task represented by a struct task_struct descriptor block. Linux drives these tasks forward through threads, assigning exactly one thread to track every active task instance. The primitive boot-stage task is globally defined as init_task, which is instantiated as a static global variable of type struct task_struct.
During compilation, kernel architects toggle the preprocessor build flag CONFIG_THREAD_INFO_IN_TASK to determine whether the low-level thread metadata structure (struct thread_info) is stored directly inside the main task_struct container block or allocated independently. If the system is compiled to house the thread metadata inside the task structure itself, the stack-end validation token is written directly to the memory boundary pointed to by the task's internal stack pointer field (stack). Conversely, if the thread_info block is allocated externally, this validation token is injected into the absolute first memory word immediately following the task's corresponding thread metadata structure. The Linux kernel implements this dedicated validation word as a robust, lightweight safety mechanism to detect stack overflows.
The programmatic task of writing this stack-end signature is managed by the following routine:
void set_task_stack_end_magic(struct task_struct *tsk)
{
unsigned long *stackend;
stackend = end_of_stack(tsk);
*stackend = STACK_END_MAGIC; /* for overflow detection */
}
This function is defined within the core subsystem source module kernel/fork.c. When thread_info is nested inside the task block, the macro helper end_of_stack() returns the absolute address value cached within the task's stack parameter field. When thread_info is configured externally, the macro instead resolves to the physical memory coordinates immediately succeeding the task's companion thread metadata block (as illustrated in Figure 7-1).
Every task structure encapsulates an internal stack pointer defined as an unsigned long word that tracks its execution frame. For the boot-stage init_task instance, this stack pointer targets the statically allocated array init_stack, whose absolute physical boundaries and memory limits are pre-determined at compile time by the linker script.
The execution of set_task_stack_end_magic() permanently seals this memory boundary by writing the specialized 32-bit validation word STACK_END_MAGIC (0x57AC6E9D) directly to the base of the init_stack array, providing the kernel with a deterministic guard value to trap stack-smashing events.
Figure 7-1 Structural Mapping of the Initialization Thread and Task Descriptors