Problem:
I ofent see __meminit
decorator in Linux source code put in front of functions. I am wondering what does the __meminit
decorator mean.
Eg. In arch/x86/mm/init_64.c
:
static void __meminit remove_pagetable(unsigned long start, unsigned long end, bool direct, struct vmem_altmap *altmap)
Attempts:
I tried to look at the definitions of the macros like
/* Used for MEMORY_HOTPLUG */
#define __meminit __section(.meminit.text) __cold notrace \
__latent_entropy
But still cannot understand the usage of it.
Follow-up Question:
Where can Linux learner find descriptions of such macros?
Unfortunately, Linux is not very well documented, and reading the source code and building intuition is still the primary way to learn how it works.
__meminit
is defined in linux/init.h as
#define __meminit __section(".meminit.text") __cold notrace \
__latent_entropy
The comment at the top of this file briefly explains how the __init
macro is used to mark functions that are used only during initialization and that later can be discarded.
__meminit
is a specialized version of __init
, it marks a function that is used during memory initialization.
As the comment /* Used for MEMORY_HOTPLUG */
before the definition of __meminit
implies, it is used with memory hotplug.
Presumably, the kernel won't free the memory initialization functions if memory hotplug is enabled since these can be needed at any time (e.g. when a new DIMM is inserted). But it will still free other initialization functions.
It is generally not safe to call a function marked with __XXXinit
from ordinary code (it could be no longer present) but if you are writing the code that handles the hotplugging of memory, then you know the kernel won't free __meminit
functions and it is safe to call them (in this context).
All the __XXXinit
macros work similarly, they put the function/variable in a section with a specific name so that the kernel can later free up it.
__meminit
expands to:
__section(".meminit.text")
. This places the function in the .meminit.text
section. It is just an ordinary section with a standardized name._cold
expands to the GCC cold attribute. It tells GCC that the function is seldom called and thus it's better to optimize it for size rather than for speed.notrace
is used to mark the function as not ftraceable. The whole machinery is a bit involved to fit this answer.__latent_entropy
is a GCC attribute used (if the necessary plugin is enabled) to make the function mix some (fixed) entropy into the kernel "global entropy state".You see all these attributes make sense for an initialization function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With