Several fragments related to the user stack in the kernel code

Source: Internet
Author: User
Today, several pieces of knowledge in my mind are in a large series, which seems to be a spark of thinking. I have been feeling confused about the size limit of the user stack and how the stack is automatically expanded, by comparing some key fragments of the previously viewed kernel code, it seems clear. Take notes before you get confused.

Question 1 What is the default stack size of a user program in Linux? How to view and change the default value (upper-layer commands and underlying implementation )?

Enter "ulimit-s" in Bash to view the default stack size of your program. The result on my machine is (unit: KB)

Whodare @ whodare :~ $ Ulimit-S
8192


It is easy to modify the default stack size.

Whodare @ whodare :~ $ Ulimit-s 16384
Whodare @ whodare :~ $ Ulimit-S
16384


Note that the above modifications only affect the sub-processes generated after the bash and do not have the durability.

If you know the method, let's see how it is embodied in the kernel sometimes. First, you must understand that each process in the system has a set of related resource restrictions, specifying the upper limit of system resources that can be obtained and used, so as to avoid excessive use of system resources.

Such a set of restrictions is implemented in the kernel through an array with the rlimit structure element. Each element of the array corresponds to a resource restriction.

Struct rlimit
{
Unsigned long rlim_curr;
Unsigned long rlim_max;
}

Here, rlim_curr indicates the current resource limit value (Be careful not to misunderstand it as the current resource usage), while rlim_max indicates the maximum resource limit value. The former is a soft limit, which can be modified by the user. As long as it cannot exceed rlim_max; the latter is a hard limit, only the Administrator has the permission to change rlim_max.

The resource limit array of each process is stored in the current-> signal-> rlim field, and the resource limit is automatically inherited by the process.

This makes it easy to understand the previous bash command. The essence is to read and update the value of current-> signal-> rlim [rlimit_stack]. rlim_curr.

2. Stack space Overflow

If a huge array is declared inside the function, the stack overflow error may easily occur, that is, the system prompts "segment error. Core dumped" during the program running ". How does the kernel detect stack overflow errors?

In summary, this is done by handling page missing exceptions. Suppose we define a function as follows:
# Define size 0x800000

Int Foo ()
...{

Char array [size];
Printf ("foo ");
Return 0;
}


The array life is usually translated by the compiler as "sub 0 x 800000% exp", that is, space is allocated for local variables by changing ESP. For the function foo here, because the size of the partial array is the same as that of the default stack (8192kb), the value of ESP becomes invalid. To be exact, after executing the "sub 0 x 800000% exp" Statement, the memory address pointed by ESP is not within the range of any valid memory linear zone of the process. When the program runs to printf () ESP is used for addressing. In this case, a page missing exception occurs, and the exception handler will take different measures based on the different scenarios that cause the exception. For this foo function, the page missing exception processing function identifies an illegal access address. Therefore, it sends a SIGSEGV signal to the process, causing the process to terminate.

3. Automatic stack Scaling

In fact, when a program starts running (fork + Exec), the VMA allocated to its user stack does not correspond to all the stack space, but is only a small part; as function calls overlap, the stack will automatically expand and automatically increase the current size of the stack. This is also completed in the Process of page missing exception handling.
The difference from the previous case is that expand_stack () is called only when the linear address that causes page missing exceptions is not much lower than the current ESP () this function is used to expand the stack space. The function internally calls acct_stack_growth () to check whether the stack space has reached currnent-> signal-> rlim [rlimit_stack]. the upper limit specified by rlim_curr. If not, the stack space will be extended. Otherwise, the SIGSEGV signal will be sent to terminate the process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.