user2586388
user2586388

Reputation: 41

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?

Upvotes: 4

Views: 8480

Answers (4)

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?

Upvotes: 5

vinc17
vinc17

Reputation: 3476

As others said, the stack is allocated in user space. But here are more details about this, in particular about its size and its growth.

8 MB is actually not the stack size, but the maximum stack size. A small part is initially allocated and the kernel grows the stack automatically when needed (after a page fault), keeping it below the stack size limit. If you do a memory access above the limit, you'll get a segmentation fault. But even if you do not reach this limit, this means that you might exhaust the physical memory (RAM + swap) just by filling the stack.

Here's a reference I've given in my answer to How does stack allocation work in Linux?: Mel Gorman's paper Understanding The Linux Virtual Memory Manager. See in particular Section 4.6.1 Handling a Page Fault, with the exception "Region not valid but is beside an expandable region like the stack" and the corresponding action "Expand the region and allocate a page". See also D.5.2 Expanding the Stack.

Upvotes: 3

Jeyaram
Jeyaram

Reputation: 9504

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.

Upvotes: 6

Deepu
Deepu

Reputation: 7610

The stack memory required for application software is allocated from the user space.

Upvotes: 0

Related Questions