Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the process kernel stack in Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces what the Linux process kernel stack is, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

What is the kernel stack of a process?

In the life cycle of each process, it is bound to fall into the kernel through system calls. After the execution of the system call is trapped in the kernel, the stack used by these kernel code is not the stack in the original user space, but a stack in kernel space, which is called the "kernel stack" of the process.

For example, there is a simple character driver that implements the open method. After this driver is mounted, the application performs open operation on the device node corresponding to that driver. The open of this application actually calls Linux's open system call through the glib library. After the system call is trapped in the kernel, the processor changes to privileged mode (the specific conversion mechanism varies with architecture. For ARM, the SP of normal mode and user mode are different registers). The stack pointer used at this time is the kernel stack pointer, which points to the kernel stack space allocated by the kernel for each process.

The role of kernel stack

My personal understanding is that after falling into the kernel, there are also function calls and automatic variables in system calls, which need stack support. The user space stack is obviously not secure and needs the support of the kernel stack. In addition, the kernel stack is also used to store some application layer information before system calls (such as user space stack pointers, system call parameters).

The relationship between kernel stack and process structure

Each process gets a kernel stack space when it is created, and the correspondence between the kernel stack and the process is accomplished through the pointer members of the two structures:

(1) struct task_struct

In learning the structure that Linux process management must learn, it represents a process in the kernel, in which all the state information of the recorded process is defined in Sched.h (include\ linux).

One of the members: void * stack; is the "bottom of the stack" that points to the following kernel stack structure.

When the system is running, the macro current gets the struct task_struct structure of the current process.

(2) Kernel stack structure union thread_union

Union thread_union {

Struct thread_info thread_info

Unsigned long Stack [thread _ SIZE/sizeof (long)]

}

Struct thread_info is a structure that records part of the process information, including process context information:

/ *

* low level task data that entry.S needs immediate access to.

* _ _ switch_to () assumes cpu_context follows immediately after cpu_domain.

, /

Struct thread_info {

Unsigned long flags; / * low level flags * /

Int preempt_count; / * 0 = > preemptable, bug * /

Mm_segment_t addr_limit; / * address limit * /

Struct task_struct * task; / * main task structure * /

Struct exec_domain * exec_domain; / * execution domain * /

_ _ U32 cpu; / * cpu * /

_ _ U32 cpu_domain; / * cpu domain * /

Struct cpu_context_save cpu_context; / * cpu context * /

_ _ U32 syscall; / * syscall number * /

_ U8 used_cp [16]; / * thread used copro * /

Unsigned long tp_value

Struct crunch_state crunchstate

Union fp_state fpstate _ _ attribute__ ((aligned (8)

Union vfp_state vfpstate

# ifdef CONFIG_ARM_THUMBEE

Unsigned long thumbee_state; / * ThumbEE Handler Base register * /

# endif

Struct restart_block restart_block

}

The key is the task member, which points to the struct task_struct structure of the process created.

And the stack member is the kernel stack. From this we can see that the kernel stack space and thread_info share the same space. If the kernel stack overflows, the thread_info will be destroyed and the system crashes.

The relationship among kernel stack-struct thread_info----struct task_struct is shown in the following figure:

Generation of kernel stack

When the process is created, the system call of the fork family allocates space for the kernel stack and struct task_struct, respectively. The calling procedure is as follows:

System call of fork family-> do_fork--- > copy_process--- > dup_task_struct

In the dup_task_struct function:

Static struct task_struct * dup_task_struct (struct task_struct * orig)

{

Struct task_struct * tsk

Struct thread_info * ti

Unsigned long * stackend

Int err

Prepare_to_copy (orig)

Tsk = alloc_task_struct ()

If (! tsk)

Return NULL

Ti = alloc_thread_info (tsk)

If (! ti) {

Free_task_struct (tsk)

Return NULL

}

Err = arch_dup_task_struct (tsk, orig)

If (err)

Goto out

Tsk- > stack = ti

Err = prop_local_init_single (& tsk- > dirties)

If (err)

Goto out

Setup_thread_stack (tsk, orig)

.

Where alloc_task_struct uses the kernel's slab allocator to allocate struct task_struct space for the process to be created

Alloc_thread_info uses the kernel's partner system to allocate kernel stack (union thread_union) space for the process to be created.

Note:

The following tsk- > stack = ti; statement, which associates the struct task_struct with the kernel stack

In setup_thread_stack (tsk, orig);, the kernel stack is associated with struct task_struct:

Static inline void setup_thread_stack (struct task_struct * p, struct task_struct * org)

{

* task_thread_info (p) = * task_thread_info (org)

Task_thread_info (p)-> task = p

}

The size of the kernel stack

Because each process allocates a kernel stack space, it is not possible to allocate much. This size is architecture-related and is generally measured in pages. In fact, this is the THREAD_SIZE we saw above, and this value is usually 4K or 8K. For the ARM architecture, this is defined in Thread_info.h (arch\ arm\ include\ asm)

# define THREAD_SIZE_ORDER 1

# define THREAD_SIZE 8192

# define THREAD_START_SP (THREAD_SIZE-8)

So the kernel stack of ARM is 8KB.

Problems to pay attention to when programming a driver:

Due to the limitation of stack space, drivers written (especially the underlying functions used by system calls) should be careful to avoid code that consumes a lot of stack space, such as recursive algorithms, the size of local automatic variable definitions, and so on.

Thank you for reading this article carefully. I hope the article "what is the process kernel stack in Linux" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report