4

When booting the Linux kernel, it is possible to load an initramfs archive and a DTB file in RAM and specify those physical addresses to the kernel. For example, using U-Boot, you could do:

bootz 0x80008000 0x82000000 0x81000000

which means: boot kernel image located at memory address 0x80008000, and specify to kernel the initramfs archive is at 0x82000000 and the DTB file is at 0x81000000. In this example, it's an ARM system, but my question applies to all systems.

When those three files are loaded into RAM, the RAM might look like this:

[...kkkkk..........iii.............dd............................... ... ..]

where k means kernel, i initramfs, d DTB and . unused space.

The initramfs archive is extracted into a ramfs, which needs to allocate memory pages to exist. The DTB file is used to populate an internal tree, which also allocates pages for its data structure.

How does the kernel avoid overwriting the initramfs and DTB files in memory while allocating pages? Are the physical pages taken by those files mapped and marked as used before eventually being freed when the original data is not needed anymore?

eepp
  • 7,255
  • 1
  • 38
  • 56

1 Answers1

4

When the bootmem allocator is initialized, the available/free ram ranges are passed to it by the kernel. The kernel doesn't consider its own kernel, dtb, and initramfs as part of the available/free ram ranges. Which means that these ranges are marked as used ( already allocated ).

[ During boot-up, the active allocator is bootmem allocator. This is discarded at a later stage of boot-up in favour of other more advanced allocators like buddy allocator ( for page allocation ) and slub/slab/slob. ]

This means that the "kk", "ii", and "dd" areas have always been marked as allocated ( not free ) from the beginning.

For ARM32:

"arm_memblock_init" [1] ( which is called from setup_arch [2] ) is responsible for adding all the memory regions that are available [3] and then reserving the "kernel" [4] , "initrd" [5] , and "device tree" [6] memory ( in addition to other reserved regions ).

KarimRaslan
  • 333
  • 2
  • 6
  • Thank you KarimRaslan, this is helpful. Could you add links to specific files/lines in the current kernel source (using some online LXR) where "the available/free ram ranges are passed to it by the kernel" (which would show the DTB, kernel and initramfs regions are not considered free) happens for ARM? Thanks! – eepp Jul 16 '14 at 06:41
  • eepp, I've just updated the reply for ARM32 case. Let me know if you're interested also in other architectures. – KarimRaslan Jul 16 '14 at 07:20
  • This answers my question perfectly. – eepp Jul 16 '14 at 16:35