Chapter3:MemoryManagement
initialization functions is to keep the data in a specific part of the kernel image that can be completely
removed from memory when booting has finished.The following macros are defined with this
in mind:
<init.h>
#define __init __attribute__ ((__section__ (".init.text"))) __cold
#define __initdata __attribute__ ((__section__ (".init.data")))
__attribute__is a special GNU C keyword to permit the use of attributes. The__section__attribute is
used to instruct the compiler to write the subsequent data or function into the respective.init.dataand
.init.textsections of the binary file (those of you unfamiliar with the structure of ELF files are referred
to Appendix E). The prefix__coldalso instructs the compiler that paths leading to the function will be
unlikely, that is, that the function won’t be called very often, which is usually the case for initialization
functions.
Thereadelftool can be used to display the individual sections of the kernel image.
wolfgang@meitner>readelf — sections vmlinux
There are 53 section headers, starting at offset 0x2c304c8:
Section Headers:
[Nr] Name Type Address Offset
Size EntSize Flags Link Info Align
[ 0] NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] .text PROGBITS ffffffff80200000 00200000
000000000021fc6f 0000000000000000 AX 0 0 4096
[ 2] __ex_table PROGBITS ffffffff8041fc70 0041fc70
0000000000003e50 0000000000000000 A 0 0 8
[ 3] .notes NOTE ffffffff80423ac0 00423ac0
0000000000000024 0000000000000000 AX 0 0 4
...
[28] .init.text PROGBITS ffffffff8067b000 0087b000
000000000002026e 0000000000000000 AX 0 0 1
[29] .init.data PROGBITS ffffffff8069b270 0089b270
000000000000c02e 0000000000000000 WA 0 0 16
...
To release initialization data from memory, it is not necessary for the kernel to know the nature of the
data — which data and functions are held in memory and what purpose they serve is totally irrelevant.
The only information of relevance is the addresses in memory at which the data and functions begin
and end.
Because this information is not available at compilation time, it is patched in when the kernel is linked.
I have already mentioned this technique at other places in this chapter. To support it, the kernel defines
the variable pair__init_beginand__init_end, whose names reveal their meanings.
free_initmemis responsible for freeing the memory area defined for initialization purposes and return-
ing the pages to the buddy system. The function is called right at the end of the boot process immediately
beforeinitstarts the first process in the system. The boot logs include a message indicating how much
memory was freed.