Linux Kernel Architecture

(Jacob Rumans) #1

Chapter3:MemoryManagement


should be reported by a system error message rather than allowing the data of other kernel parts to be
overwritten unnoticed. Because this separation is made in virtual address space, no valuable real memory
pages are wasted.

Reserving Memory withvmalloc


vmallocis the interface function used by the kernel code to request memory that is not necessarily con-
tiguous in physical memory but is always linear in virtual memory.

<vmalloc.h>
void *vmalloc(unsigned long size);

Just one parameter is required to specify the size of the required memory area — in contrast to the func-
tions discussed earlier, the size unit is not pages but bytes, as is common in userspace programming.

The best-known example ofvmallocuse is in the module implementation of the kernel. Because modules
can be loaded at any time, there is no guarantee — particularly if the system has been up and running for
a long time — that sufficient contiguous memory will be available for the sometimes voluminous module
data. This problem can be circumvented by usingvmallocif sufficient memory can be pieced together
from smaller chunks.

vmallocis also invoked at about 400 other places in the kernel, particularly in device and sound drivers.

Because the memory pages used forvmallocmust in any case be actively mapped in kernel address
space, it is obviously preferable to use pages fromZONE_HIGHMEMfor this purpose. This allows the
kernel to conserve the more valuable lower zones without incurring any added disadvantages. For
this reason,vmalloc(along with the mapping functions discussed in Section 3.5.8) is one of the few
occasions when the kernel is able to use highmem pages for its own purposes (and not for userspace
applications).

Data Structures


When it manages thevmallocarea in virtual memory, the kernel must keep track of which sections
are in use and which are free. To this end, it defines a data structure to hold all used sections in a
linked list.

The kernel uses an important data structure calledvm_area_structto manage the
virtual address space contents of a userspace process. Despite the similarity of name
and purpose, these two structures must not be confused.

<vmalloc.h>
struct vm_struct {
struct vm_struct *next;
void *addr;
unsigned long size;
unsigned long flags;
struct page **pages;
unsigned int nr_pages;
unsigned long phys_addr;
};
Free download pdf