Where is the stack memory allocated for the Linux process?

We know that when creating a process, one stack is allocated for this process. The stack size is usually 8 MB on linux. My question is from where, where does this stack stand out? From user space or from system space?

+3
source share
4 answers

I hope you know the concept that the entire user process will only be stored in user space. It uses system calls to do some kernel work.

The stack memory will be part of the process context area in memory. I am user space .

Assuming your process is running, set the PID using ps -ax . let's say 1234 is your PID.

cat /proc/1234/maps will give you a mapping of this particular process.

In this map file, you can check the stack mapping for the stack.

+5
source

As others have said, a stack is allocated in user space. But here in more detail about this, in particular about its size and its growth.

8 MB is actually not the stack size, but the maximum stack size. First, a small part is allocated, and the kernel automatically grows the stack if necessary (after a page error), keeping it below the stack size limit. If you use memory access above the limit, you will get a segmentation error. But even if you do not reach this limit, it means that you can run out of physical memory (RAM + swap) by simply populating the stack.

Here is the link I gave in response to How is stack distribution performed on Linux? : Mel Gorman, paper Understanding Linux Virtual Memory Manager . See, In particular, section 4.6.1, โ€œHandling a page error,โ€ with the exception of โ€œThe area is invalid, but is located next to an expandable area, such as a stack,โ€ and the corresponding action โ€œExpand region and select pageโ€. See Also D.5.2. Stack expansion.

+3
source

First you need to understand what paging and page errors are: How does x86 swap work?

Process core and memory

The Linux kernel reserves two zones of virtual memory:

  • one for kernel memory
  • one for programs

The exact split is set to CONFIG_VMSPLIT_... Default:

  • on a 32-bit version:

    • lower 3/4 is the program space: 00000000 to BFFFFFFF
    • upper 1/4 is the kernel memory: C0000000 to FFFFFFFF

    Like this:

     ------------------ FFFFFFFF Kernel ------------------ C0000000 ------------------ BFFFFFFF Process ------------------ 00000000 
  • in 64-bit mode: currently only 48 bits are actually used, divided into two equally spaced spaces. The Linux kernel simply assigns:

    • bottom handles 00000000 00000000 to 008FFFFF FFFFFFFF
    • upper core: FFFF8000 00000000 to FFFFFFFF FFFFFFFF

    Like this:

     ------------------ FFFFFFFF FFFFFFFF Kernel ------------------ FFFF8000 00000000 (not addressable) ------------------ 008FFFFF FFFFFFFF Process ------------------ 00000000 00000000 

Process address space

Simplified software virtual process memory:

 ------------------ <--- Top of the process address space Stack (grows down) vvvvvvvvv ------------------ (unmapped) ------------------ <--- Maximum stack size. (unmapped) ------------------- mmap ------------------- (unmapped) ------------------- ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ brk (grows up) ------------------- BSS ------------------- Data ------------------- Text ------------------- ------------------- <--- Bottom or process address space. 

Stack placement

The kernel maintains a list of pages related to each process and synchronizes them with paging.

If the program accesses memory that does not belong to it, the kernel processes the page error and decides what to do:

  • if it exceeds the maximum stack size, select these pages for the process
  • otherwise, send SIGSEGV to a process that usually kills it.

Additional information: https://unix.stackexchange.com/questions/145557/how-does-stack-allocation-work-in-linux/239323#239323

brk and mmap

These system calls allow processes to explicitly request chunks of memory for the kernel instead of just popping off the stack and segfault.

Here is a practical brk example: What does the brk () system call do?

This answer explains the advantage of using the stack when possible: What is the function of the push / pop instructions used for registers in the x86 assembly?

Physical memory

There is no clear separation between the kernel and memory in user space: Is there an blurring of experimentation between user space and kernel in physical memory on Linux x86-64?

+3
source

Stack memory required for application software is allocated from user space.

0
source

Source: https://habr.com/ru/post/906266/


All Articles