Linux Kernel Architecture

(Jacob Rumans) #1

Chapter3:MemoryManagement


if (addr > end - size)
goto out;
}
...

The kernel finds a suitable position ifsize+addris less than the start address of the area just examined
(held intmp->addr). The new list element is then initialized with the appropriate values and is added to
thevmlistlinked list.

mm/vmalloc.c
found:
area->next = *p;
*p = area;

area->flags = flags;
area->addr = (void *)addr;
area->size = size;
area->pages = NULL;
area->nr_pages = 0;
area->phys_addr = 0;

return area;
...
}

A null pointer is returned to indicate failure if no suitable memory area is found.

Theremove_vm_areafunction removes an existing area from thevmallocaddress space.

<vmalloc.h>
struct vm_struct *remove_vm_area(void *addr);

The function expects as a parameter the virtual start address of the area to be removed. To find the area,
the kernel must successively scan the list elements ofvmlistuntil it finds a match. The corresponding
vm_areainstance can then be removed from the list.

Allocating a Memory Area


Allocation of a non-continuous memory area is initiated byvmalloc. This is simply a front-end function
to supply__vmallocwith suitable parameters and to directly invoke__vmalloc_node. The associated
code flow diagram is shown in Figure 3-39.

Implementation is divided into three parts. First,get_vm_areafinds a suitable area in thevmalloc
address space. Then individual pages are allocated from physical memory, and finally, these pages are
mapped contiguously into thevmallocarea — and VM allocation is done.

The full code need not be reproduced here because it is riddled with boring safety checks.^23 What is
interesting is the allocation of the physical memory area (ignore the possibility that there may not be
enough physical pages available).

(^23) This, however, does not mean that you should avoid safety checks in your own code!

Free download pdf