Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the stack memory allocated from for a Linux process?

We know that when a process is created,one stack is allocated for this process.The size of the stack is typically 8 Mb in linux.My question is that,from where this stack is allocated??From user space or from system space?

like image 997
user2586388 Avatar asked Dec 11 '22 13:12

user2586388


2 Answers

I hope you know the concept that all user process will be kept in user space only. It uses system calls to get some work done by kernel.

The stack memory will be part of process context area in memory. i.e user space.

Suppose your process is running, get the PID by ps -ax. say 1234 is your PID.

cat /proc/1234/maps will give you the mapping of that particular process.

In thats maps file, you can check the stack for stack mapping.

like image 129
Jeyaram Avatar answered Feb 09 '23 00:02

Jeyaram


First you must understand what paging and page faults are: How does x86 paging work?

Kernel vs process memory

The Linux Kernel reserves two zones of virtual memory:

  • one for kernel memory
  • one for programs

The exact split is configured by CONFIG_VMSPLIT_.... By default:

  • on 32-bit:

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

    Like this:

    ------------------ FFFFFFFF
    Kernel
    ------------------ C0000000
    ------------------ BFFFFFFF
    
    
    Process
    
    
    ------------------ 00000000
    
  • on 64-bit: currently only 48-bits are actually used, split into two equally sized disjoint spaces. The Linux kernel just assigns:

    • the bottom part to processes 00000000 00000000 to 008FFFFF FFFFFFFF
    • the top part to the kernel: FFFF8000 00000000 to FFFFFFFF FFFFFFFF

    Like this:

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

Process address space

Simplified program virtual memory of a process:

------------------ <--- Top of the process address space
Stack (grows down)
v v v v v v v v v
------------------

(unmapped)

------------------ <--- Maximum stack size.


(unmapped)


-------------------
mmap
-------------------


(unmapped)


-------------------
^ ^ ^ ^ ^ ^ ^ ^ ^ ^
brk (grows up)
-------------------
BSS
-------------------
Data
-------------------
Text
-------------------

------------------- <--- Bottom or process address space.

Stack allocation

The kernel maintains a list of pages that belong to each process, and synchronizes that with the paging.

If the program accesses memory that does not belong to it, the kernel handles a page-fault, and decides what to do:

  • if it is above the maximum stack size, allocate those pages to the process
  • otherwise, send a SIGSEGV to the process, which usually kills it

More info at: https://unix.stackexchange.com/questions/145557/how-does-stack-allocation-work-in-linux/239323#239323

brk and mmap

Those system calls allow processes to explicitly request chunks of memory to the kernel instead of just going down the stack and segfaulting.

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

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

Physical memory

There is no clear split between kernel and userspace memory: Is there an explict split between userspace and kernel in physical memory on Linux x86-64?