Where is task_struct stored?
kernel, linux, process, task
Solution
The Linux kernel allocates a task_struct via the kmem_cache facility. For instance in fork.c there is a piece of code responsible for allocating a task struct:
#define alloc_task_struct_node(node) \
kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node)
static struct kmem_cache *task_struct_cachep;
The place where the pointer to the current thread is stored is architecture-dependent. For instance, this is how it works for x86 (arch/x86/include/asm/current.h):
static __always_inline struct task_struct *get_current(void)
{
return percpu_read_stable(current_task);
}
and in PowerPC (arch/powerpc/include/asm/current.h):
static inline struct task_struct *get_current(void)
{
struct task_struct *task;
__asm__ __volatile__("ld %0,%1(13)"
: "=r" (task)
: "i" (offsetof(struct paca_struct, __current)));
return task;
}
You can use the Elixir Cross Reference in order to easily explore the kernel source.
Problem
Task_struct is used for keeping necessary information about a process by kernel. Thanks to that structure kernel can suspend a process and after a while proceed with its implementation. But my question is: where is this task_struct stored in memory (I've read about kernel stack, is that one which is in kernel space of virtual address space?)? where does kernel keep a pointer to that structure and that structure after suspending process? I would appreciate if you give some references to resources where it's described. PS. I forgot to say the question is about Linux kernel.