I was thinking about how the Linux kernel implements system calls and I was wondering if someone could give me a high level view of how sbrk/brk work?
I’ve reviewed the kernel code, but there is just so much of it and I don’t understand it. I was hoping for a summary from someone?
In a very high level view, the Linux kernel tracks the memory visible to a process as several “memory areas” (
struct vm_area_struct). There is also a structure which represents (again in a very high level view) a process’ whole address space (struct mm_struct). Each process (except some kernel threads) has exactly onestruct mm_struct, which in turn points to all thestruct vm_area_structfor the memory it can accesss.The
sys_brksystem call (found inmm/mmap.c) simply adjusts some of these memory areas. (sbrkis a glibc wrapper aroundbrk). It does so by comparing the old value of thebrkaddress (found insidestruct mm_struct) and the requested value.It would be simpler to look at the
mmapfamily of functions first, sincebrkis a special case of it.