next up previous contents
Next: Creating a Process Up: Processes Previous: Files

Virtual Memory

    A processes virtual memory contains executable code and data from many sources. First there is the program image that is loaded; for example a command like ls . This command, like all executable images, is composed of both executable code and data. The image file contains all of the information neccessary to load the executable code and associated program data into the virtual memory of the process. Secondly, processses can allocate (virtual) memory to use during their processing, say to hold the contents of files that it is reading. This newly allocated, virtual, memory needs to be linked into the processes existing virtual memory so that it can be used. Thirdly, Linux processes use libraries of commonly useful code, for example file handling routines. It does not make sense that each process has its own copy of the library, Linux uses shared libraries that can be used by several running processes at the same time. The code and the data from these shared libraries must be linked into this processes virtual address space and also into the virtual address space of the other processes sharing the library.

In any given time period a process will not have used all of the code and data contained within its virtual memory. It could contain code that is only used during certain situations, such as during initialization or to process a particular event. It may only have used some of the routines from its shared libraries. It would be wasteful to load all of this code and data into physical memory where it would lie unused. Multiply this wastage by the number of processes in the system and the system would run very inefficiently. Instead, Linux uses a technique called demand paging where the virtual memory of a process is brought into physical memory only when a process attempts to use it. So, instead of loading the code and data into physical memory straight away, the Linux kernel alters the processes page table, marking the virtual areas as existing but not in memory. When the process attempts to acccess the code or data the system hardware will generate a page fault and hand control to the Linux kernel to fix things up. Therefore, for every area of virtual memory in the processes address space Linux needs to know where that virtual memory comes from and how to get it into memory so that it can fix up these page faults.

   figure2761
Figure: A Processes Virtual Memory

The Linux kernel needs to manage all of these areas of virtual memory and the contents of each processes virtual memory is described by a mm_struct  data structure pointed at from its task_struct . The processes mm_struct    data structure also contains information about the loaded executable image and a pointer to the processes page tables. It contains pointers to a list of vm_area_struct  data structures, each   representing an area of virtual memory within this process.

This linked list is in ascending virtual memory order, figure gif shows the layout in virtual memory of a simple process together with the kernel data structures managing it. As those areas of virtual memory are from several sources, Linux abstracts the interface by having the vm_area_struct  point to a set of virtual memory handling routines (via vm_ops ). This way all of the processes virtual memory can be handled in a consistant way no matter how the underlying services managing that memory differ. For example there is a routine that will be called when the process attempts to access the memory and it does not exist, this is how page faults are handled.

The processes set of vm_area_struct  data structures is accessed repeatedly by the Linux kernel as it creates new areas of virtual memory for the process and as it fixes up references to virtual memory not in the system's physical memory. This makes the time that it takes to find the correct vm_area_struct  critical to the performance of the system. To speed up this access, Linux also arranges the vm_area_struct  data structures into an AVL (Adelson-Velskii and Landis) tree. This tree is arranged so that each vm_area_struct  (or node) has a left and a right pointer to its neighbouring vm_area_struct  structure. The left pointer points to node with a lower starting virtual address and the right pointer points to a node with a higher starting virtual address. To find the correct node, Linux goes to the root of the tree and follows each node's left and right pointers until it finds the right vm_area_struct . Of course, nothing is for free and inserting a new vm_area_struct  into this tree takes additional processing time.

When a process allocates virtual memory, Linux does not actually reserve physical memory for the process. Instead, it describes the virtual memory by creating a new vm_area_struct  data structure. This is linked into the processes list of virtual memory. When the process attempts to write to a virtual address within that new virtual memory region then the system will page fault. The processor will attempt to decode the virtual address, but as there are no Page Table Entries for any of this memory, it will give up and raise a page fault exception, leaving the Linux kernel to fix things up. Linux looks to see if the virtual address referenced is in the current processes virtual address space. If it is, Linux creates the appropriate PTEs and allocates a physical page of memory for this process. The code or data may need to be brought into that physical page from the filesystem or from the swap disk. The process can then be restarted at the instruction that caused the page fault and, this time as the memory physically exists, it may continue.


next up previous contents
Next: Creating a Process Up: Processes Previous: Files

David A. Rusling
david.rusling@reo.mts.dec.com