What is memory management?
Physical and virtual memory
Allocation
Paging
Just scratching the surface!
We start without any: this is NOMMU mode
Clone system call
Demo: man 2 clone
man 2 clone
Aside: See posix_spawn(3)
posix_spawn(3)
How is fork defined?
It's just clone
kernel/fork.c
clone(CLONE_VM)
CLONE_VM
CLONE_FS
CLONE_FILES
CLONE_SIGHAND
Isolated view of system resources
unshare
sudo unshare --pid --fork --mount-proc bash
--pid: Creates a new PID namespace.
--pid
--fork: Forks a new process to run the specified program (in this case, bash).
--fork
--mount-proc: Mounts a new /proc filesystem for the new PID namespace.
--mount-proc
/proc
Look at ps auxf inside and outside the new shell
ps auxf
Find the external PID X of the new internal PID 1
Look at both of sudo ls -l /proc/{$X,$$}/ns
sudo ls -l /proc/{$X,$$}/ns
Can we achieve address space isolation without the MMU?
No
Demo: NOMMU lack of memory isolation
Enable the MMU (disable RISCV_M_MODE, enable MMU in menuconfig)
Recompile the kernel, init, and run
Successful segfault!
Logical: fixed offset from physical memory
phys_to_virt
Virtual: has entry in page tables
SLOB
* Simple list of blocks * Slowest, oldest, simplest * Uses global `slob_lock`
SLAB
* Newer, but not the best * Still locks * Improved performance via caching and per-cpu lists
SLUB
* fastest, newest * Lockless fastpath for alloc/deallocs * Locks only when crossing CPU boundary
Demo: /proc/slabinfo
/proc/slabinfo
To set allocator: add slab_allocator=sl{u,a,o}b to kernel command line at boot
slab_allocator=sl{u,a,o}b
Collectively, these are the "slab allocators" or "slab layer"
What weighs more: 10G of physical memory of 10G of virtual memory (joke)
Technically: virtual memory requires pagetables so there is some overhead
What's bigger: physical or virtual memory?
Answer: virtual memory. Why?
On 64 bit system each process had maximum theoretical address space of size 2^64!
Most real systems only use 48 bits, some up to 57
Therefore: we need to swap out pages so physical memory doesn't get full
When kernel is under memory pressure:
Toss File-backed memory pages away
If pages are dirty, write back to backing file first
Swap out Anonymous pages
Move memory pages into swap space
free -mh
Can be a swap file, partition, or compressed RAM
zram
lsblk
Uses least recently used (LRU) algorithm
Q: What happens when you run out of memory?
A: The out of memory killer is activated to kill a memory-hogging process
Demo: look at mm/oom_kill.c
mm/oom_kill.c
Pages allocation in power-of-two groups
Use get_free_pages() API
get_free_pages()
You can go around the slab allocators
fork()