Linux Kernel Architecture

(Jacob Rumans) #1

Chapter3:MemoryManagement


but it is ensured that the resulting address__START_KERNELis aligned with__KERNEL_ALIGN.Theregion
reserved for the kernel binary isKERNEL_TEXT_SIZE, currently defined to 40 MiB.

include/asm-x86/page_64.h
#define __PHYSICAL_START CONFIG_PHYSICAL_START
#define __KERNEL_ALIGN 0x200000

#define __START_KERNEL (__START_KERNEL_map + __PHYSICAL_START)
#define __START_KERNEL_map _AC(0xffffffff80000000, UL)
#define KERNEL_TEXT_SIZE (40*1024*1024)
#define KERNEL_TEXT_START _AC(0xffffffff80000000, UL)

Finally, some space to map modules into must be provided, and this is in the region fromMODULES_VADDR
toMODULES_END:

include/asm-x86/pgtable_64.h
#define MODULES_VADDR _AC(0xffffffff88000000, UL)
#define MODULES_END _AC(0xfffffffffff00000, UL)
#define MODULES_LEN (MODULES_END - MODULES_VADDR)

The available amount of memory is computed inMODULES_LEN; currently, this amounts to approximately
1,920 MiB.

3.4.3 Memory Management during the Boot Process


Although memory management is not yet initialized, the kernel needs to reserve memory during the
boot process so that it can create various data structures. Abootmem allocatorthat assigns memory in the
early boot phase is used to do this.

Obviously, what is required is a system that focuses on simplicity rather than on performance and univer-
sality. Kernel developers therefore decided to implement afirst-fitallocator as the simplest conceivable
way of managing memory in the boot phase.

A bitmap with (at least) as many bits as there are physical pages present in the system is used to manage
pages. Bit value 1 indicates a used page and 0 a free page.

When memory needs to be reserved, the allocator scans the bitmap bit by bit until it finds a posi-
tion big enough to hold a sufficient number of contiguous pages, literally the first-best or first-fit
position.

This procedure is not very efficient because the bit chain must be scanned right from the start for each
allocation. It cannot therefore be used for memory management once the kernel has been fully initialized.
The buddy system (used in conjunction with the slab, slub, or slob allocator) is a far better alternative, as
discussed in Section 3.5.5.

Data Structures


Even the first-fit allocator has to manage some data. The kernel provides an instance of thebootmem_data
structure (for each node in the system) for this purpose. Of course, the memory needed for the struc-
turecannotbe reserved dynamically but must already be allocated to the kernel at compilation time.
Free download pdf