kmemcheck
Introduction to the kmemcheck in the Linux kernel
This is the third part of the chapter which describes memory management in the Linux kernel and in the previous part of this chapter we met two memory management related concepts:
Fix-Mapped Addresses
;ioremap
.
The first concept represents special area in virtual memory, whose corresponding physical mapping is calculated in compile-time. The second concept provides ability to map input/output related memory to virtual memory.
For example if you will look at the output of the /proc/iomem
:
you will see map of the system's memory for each physical device. Here the first column displays the memory registers used by each of the different types of memory. The second column lists the kind of memory located within those registers. Or for example:
can show us lists of currently registered port regions used for input or output communication with a device. All memory-mapped I/O addresses are not used by the kernel directly. So, before the Linux kernel can use such memory, it must map it to the virtual memory space which is the main purpose of the ioremap
mechanism. Note that we saw only early ioremap
in the previous part. Soon we will look at the implementation of the non-early ioremap
function. But before this we must learn other things, like different types of memory allocators and etc., because otherwise it will be very difficult to understand it.
So, before we will move on to the non-early memory management of the Linux kernel, we will see some mechanisms which provide special abilities for debugging, check of memory leaks, memory control and etc. It will be easier to understand how memory management arranged in the Linux kernel after learning of all of these things.
As you already may guess from the title of this part, we will start to consider memory mechanisms from the kmemcheck. As we always did in other chapters, we will start to consider from theoretical side and will learn what is kmemcheck
mechanism in general and only after this, we will see how it is implemented in the Linux kernel.
So let's start. What is it kmemcheck
in the Linux kernel? As you may guess from the name of this mechanism, the kmemcheck
checks memory. That's true. Main point of the kmemcheck
mechanism is to check that some kernel code accesses uninitialized memory
. Let's take following simple C program:
Here we allocate memory for the A
structure and tries to print value of the a
field. If we will compile this program without additional options:
The compiler will not show us warning that a
field is not uninitialized. But if we will run this program with valgrind tool, we will see the following output:
Actually the kmemcheck
mechanism does the same for the kernel, what the valgrind
does for userspace programs. It check uninitialized memory.
To enable this mechanism in the Linux kernel, you need to enable the CONFIG_KMEMCHECK
kernel configuration option in the:
menu of the Linux kernel configuration:
We may not only enable support of the kmemcheck
mechanism in the Linux kernel, but it also provides some configuration options for us. We will see all of these options in the next paragraph of this part. Last note before we will consider how does the kmemcheck
check memory. Now this mechanism is implemented only for the x86_64 architecture. You can be sure if you will look in the arch/x86/Kconfig x86
related kernel configuration file, you will see following lines:
So, there isn't anything which is specific for other architectures.
Ok, so we know that kmemcheck
provides mechanism to check usage of uninitialized memory
in the Linux kernel and how to enable it. How it does these checks? When the Linux kernel tries to allocate some memory i.e. something is called like this:
or in other words somebody wants to access a page, a page fault exception is generated. This is achieved by the fact that the kmemcheck
marks memory pages as non-present
(more about this you can read in the special part which is devoted to Paging). If a page fault
exception occurred, the exception handler knows about it and in a case when the kmemcheck
is enabled it transfers control to it. After the kmemcheck
will finish its checks, the page will be marked as present
and the interrupted code will be able to continue execution. There is little subtlety in this chain. When the first instruction of interrupted code will be executed, the kmemcheck
will mark the page as non-present
again. In this way next access to memory will be caught again.
We just considered the kmemcheck
mechanism from theoretical side. Now let's consider how it is implemented in the Linux kernel.
Implementation of the kmemcheck
mechanism in the Linux kernel
kmemcheck
mechanism in the Linux kernelSo, now we know what is it kmemcheck
and what it does in the Linux kernel. Time to see at its implementation in the Linux kernel. Implementation of the kmemcheck
is split in two parts. The first is generic part is located in the mm/kmemcheck.c source code file and the second x86_64 architecture-specific part is located in the arch/x86/mm/kmemcheck directory.
Let's start from the initialization of this mechanism. We already know that to enable the kmemcheck
mechanism in the Linux kernel, we must enable the CONFIG_KMEMCHECK
kernel configuration option. But besides this, we need to pass one of following parameters:
kmemcheck=0 (disabled)
kmemcheck=1 (enabled)
kmemcheck=2 (one-shot mode)
to the Linux kernel command line. The first two are clear, but the last needs a little explanation. This option switches the kmemcheck
in a special mode when it will be turned off after detecting the first use of uninitialized memory. Actually this mode is enabled by default in the Linux kernel:
We know from the seventh part of the chapter which describes initialization of the Linux kernel that the kernel command line is parsed during initialization of the Linux kernel in do_initcall_level
, do_early_param
functions. Actually the kmemcheck
subsystem consists from two stages. The first stage is early. If we will look at the mm/kmemcheck.c source code file, we will see the param_kmemcheck
function which is will be called during early command line parsing:
As we already saw, the param_kmemcheck
may have one of the following values: 0
(enabled), 1
(disabled) or 2
(one-shot). The implementation of the param_kmemcheck
is pretty simple. We just convert string value of the kmemcheck
command line option to integer representation and set it to the kmemcheck_enabled
variable.
The second stage will be executed during initialization of the Linux kernel, rather during initialization of early initcalls. The second stage is represented by the kmemcheck_init
:
Main goal of the kmemcheck_init
function is to call the kmemcheck_selftest
function and check its result:
and return with the EINVAL
if this check is failed. The kmemcheck_selftest
function checks sizes of different memory access related opcodes like rep movsb
, movzwq
and etc. If sizes of opcodes are equal to expected sizes, the kmemcheck_selftest
will return true
and false
otherwise.
So when somebody calls:
through a series of different function calls the kmem_getpages
function will be called. This function is defined in the mm/slab.c source code file and main goal of this function tries to allocate pages with the given flags. In the end of this function we can see following code:
So, here we check that the if kmemcheck
is enabled and the SLAB_NOTRACK
bit is not set in flags we set non-present
bit for the just allocated page. The SLAB_NOTRACK
bit tell us to not track uninitialized memory. Additionally we check if a cache object has constructor (details will be considered in next parts) we mark allocated page as uninitialized or unallocated otherwise. The kmemcheck_alloc_shadow
function is defined in the mm/kmemcheck.c source code file and does following things:
First of all it allocates memory space for the shadow bits. If this bit is set in a page, this means that this page is tracked by the kmemcheck
. After we allocated space for the shadow bit, we fill all allocated pages with this bit. In the end we just call the kmemcheck_hide_pages
function with the pointer to the allocated page and number of these pages. The kmemcheck_hide_pages
is architecture-specific function, so its implementation is located in the arch/x86/mm/kmemcheck/kmemcheck.c source code file. The main goal of this function is to set non-present
bit in given pages. Let's look at the implementation of this function:
Here we go through all pages and try to get page table entry
for each page. If this operation was successful, we unset present bit and set hidden bit in each page. In the end we flush translation lookaside buffer, because some pages was changed. From this point allocated pages are tracked by the kmemcheck
. Now, as present
bit is unset, the page fault execution will be occurred right after the kmalloc
will return pointer to allocated space and a code will try to access this memory.
As you may remember from the second part of the Linux kernel initialization chapter, the page fault
handler is located in the arch/x86/mm/fault.c source code file and represented by the do_page_fault
function. We can see following check from the beginning of the do_page_fault
function:
The kmemcheck_active
gets kmemcheck_context
per-cpu structure and returns the result of comparison of the balance
field of this structure with zero:
The kmemcheck_context
is structure which describes current state of the kmemcheck
mechanism. It stored uninitialized addresses, number of such addresses and etc. The balance
field of this structure represents current state of the kmemcheck
or in other words it can tell us did kmemcheck
already hid pages or not yet. If the data->balance
is greater than zero, the kmemcheck_hide
function will be called. This means than kmemecheck
already set present
bit for given pages and now we need to hide pages again to cause next step to page fault. This function will hide addresses of pages again by unsetting of present
bit. This means that one session of kmemcheck
already finished and new page fault occurred. At the first step the kmemcheck_active
will return false as the data->balance
is zero for the start and the kmemcheck_hide
will not be called. Next, we may see following line of code in the do_page_fault
:
First of all the kmemcheck_fault
function checks that the fault occurred by the correct reason. At first we check the flags register and check that we are in normal kernel mode:
If these checks weren't successful we return from the kmemcheck_fault
function as it was not kmemcheck
related page fault. After this we try to lookup a page table entry
related to the faulted address and if we can't find it we return:
Last two steps of the kmemcheck_fault
function is to call the kmemcheck_access
function which check access to the given page and show addresses again by setting present bit in the given page. The kmemcheck_access
function does all main job. It checks current instruction which caused a page fault. If it finds an error, the context of this error will be saved by kmemcheck
to the ring queue:
The kmemcheck
mechanism declares special tasklet:
which runs the do_wakeup
function from the arch/x86/mm/kmemcheck/error.c source code file when it is scheduled to run.
The do_wakeup
function will call the kmemcheck_error_recall
function which will print errors collected by kmemcheck
. As we already saw the:
function will be called in the end of the kmemcheck_fault
function. This function will set present bit for the given pages again:
Where the kmemcheck_show_all
function calls the kmemcheck_show_addr
for each address:
by the call of the kmemcheck_show_addr
:
In the end of the kmemcheck_show
function we set the TF flag if it wasn't set:
We need to do it because we need to hide pages again after first executed instruction after a page fault will be handled. In a case when the TF
flag, so the processor will switch into single-step mode after the first instruction will be executed. In this case debug
exception will occurred. From this moment pages will be hidden again and execution will be continued. As pages hidden from this moment, page fault exception will occur again and kmemcheck
continue to check/collect errors again and print them from time to time.
That's all.
Conclusion
This is the end of the third part about Linux kernel memory management. If you have questions or suggestions, ping me on twitter 0xAX, drop me an email or just create an issue. In the next part we will see yet another memory debugging related tool - kmemleak
.
Please note that English is not my first language and I am really sorry for any inconvenience. If you found any mistakes please send me a PR to linux-insides.
Links
Last updated