What is memory management?
Phsyical and virtual memory
Allocation
Paging
Just scratching the surface!
We start without any: this is NOMMU mode
Clone system call
Demo: man 2 clone
Aside: See posix_spawn(3)
Question: How is fork defined?
A: it's just clone
kernel/fork.c
2887 #ifdef __ARCH_WANT_SYS_FORK
2888 SYSCALL_DEFINE0(fork)
2889 {
2890 #ifdef CONFIG_MMU
2891 struct kernel_clone_args args = {
2892 .exit_signal = SIGCHLD,
2893 };
2894
2895 return kernel_clone(&args);
2896 #else
2897 /* can not support in nommu mode */
2898 return -EINVAL;
2899 #endif
2900 }
2901 #endif
Namespace interlude
clone(CLONE_VM)
CLONE_VM
: The new process shares the same memory space as the parent.CLONE_FS
: Shares file system information.CLONE_FILES
: Shares file descriptors.CLONE_SIGHAND
: Shares signal handlers.Discuss namespaces: isolated view of system resources
Demo: unshare
sudo unshare --pid --fork --mount-proc bash
--pid
: Creates a new PID namespace.
--fork
: Forks a new process to run the specified program (in this case, bash).
--mount-proc
: Mounts a new /proc
filesystem for the new PID namespace.
Look at ps auxf
inside and outside the new shell
Find the external PID X of the new internal PID 1
Look at both of sudo ls -l /proc/{$X,$$}/ns
Q: Can we achieve address space isolation without the MMU?
A: No
Enable the MMU (disable RISCV_M_MODE, enable MMU in menuconfig)
Recompile the kernel, init, and run
Successful segfault!
Types of kernel addresses in MMU mode
Logical: fixed offset from physical memory
phys_to_virt
doesVirtual: 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
To set allocator: add slab_allocator=sl{u,a,o}b
to kernel command line at boot
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 address space of size 2^64!
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
with lsblk
Uses least recently used (LRU) algorithm
Q: What happens when you run out of memory?
Answer: out of memory killer is activated to kill a memory-hogging process
Demo: look at mm/oom_kill.c
Pages allocation in power-of-two groups
Use get_free_pages()
API
You can go around the slab allocators
fork()
is a mirage
Virtual memory is not strictly required
Virtual memory provides important protections
The kernel contains multiple memory allocators
The slab allocators rely on the page allocator on the backend
There are many aspects to the kernel memory management system beyond this intro
We are only scratching the surface
msg = (silence)
whoami = None
singularity v0.6-56-g8e52bc8 https://github.com/underground-software/singularity