Mem Mgt

Document Sample
Mem Mgt Powered By Docstoc
					                                    Memory Management


A multi-tasking system like Linux makes particular demands of memory management. The
memory belonging to a process and that used by the kernel need to be protected against access
by other processes. This protection is vital to the stability of a multitasking operating system.
Primary memory (RAM) has always been a scarce resource, and still is. As the amount of
working memory regularly available has grown, the memory requirement of application have
grown with it.
The memory management subsystem is one of the most important parts of the operating system.
Since the early days of computing, there has been a need for more memory than exists physically
in a system. Strategies have been developed to overcome this limitation and the most successful
of these is virtual memory. Virtual memory makes the system appear to have more memory than
it actually has by sharing it between competing processes as they need it.
Virtual memory does more than just make your computer's memory go further. The memory
management subsystem provides:

Large Address Spaces: The operating system makes the system appear as if it has a larger
amount of memory than it actually has. The virtual memory can be many times larger than the
physical memory in the system,
Protection: Each process in the system has its own virtual address space. These virtual address
spaces are completely separate from each other and so a process running one application cannot
affect another. Also, the hardware virtual memory mechanisms allow areas of memory to be
protected against writing.
Memory Mapping: Memory mapping is used to map image and data files into a processes
address space. In memory mapping, the contents of a file are linked directly into the virtual
address space of a process.
Fair Physical Memory Allocation: The memory management subsystem allows each running
process in the system a fair share of the physical memory of the system,
Shared Virtual Memory: Although virtual memory allows processes to have separate (virtual)
address spaces, there are times when you need processes to share memory. For example there
could be several processes in the system running the bash command shell. Rather than have
several copies of bash, one in each processes virtual address space, it is better to have only one
copy in physical memory and all of the processes running bash share it. Dynamic libraries are
another common example of executing code shared between several processes.
Shared memory can also be used as an Inter Process Communication (IPC) mechanism, with two
or more processes exchanging information via memory common to all of them.

The architecture-independent memory model in Linux

In a virtual memory system all of these addresses are virtual addresses and not physical
addresses. These virtual addresses are converted into physical addresses by the processor based
on information held in a set of tables maintained by the operating system. The physical memory
is divided into pages. For x86 processor, the size is set to 4K bytes.

                          Abstract model of Virtual to Physical address mapping

Demand Paging
As there is much less physical memory than virtual memory the operating system must be
careful that it does not use the physical memory inefficiently. One way to save physical memory
is to only load virtual pages that are currently being used by the executing program. For example,
a database program may be run to query a database. In this case not all of the database needs to
be loaded into memory, just those data records that are being examined. If the database query is a
search query then it does not make sense to load the code from the database program that deals
with adding new records. This technique of only loading virtual pages into memory as they are
accessed is known as demand paging.
Linux uses demand paging to load executable images into a processes virtual memory. Whenever
a command is executed, the file containing it is opened and its contents are mapped into the
processes virtual memory. This is done by modifying the data structures describing this
processes memory map and is known as memory mapping. However, only the first part of the
image is actually brought into physical memory. The rest of the image is left on disk. As the
image executes, it generates page faults and Linux uses the processes memory map in order to
determine which parts of the image to bring into memory for execution.

If a process needs to bring a virtual page into physical memory and there are no free physical
pages available, the operating system must make room for this page by discarding another page
from physical memory.

Linux uses a Least Recently Used (LRU) page aging technique to fairly choose pages which
might be removed from the system. This scheme involves every page in the system having an
age which changes as the page is accessed. The more that a page is accessed, the younger it is;
the less that it is accessed the older and more stale it becomes. Old pages are good candidates for

Virtual address space
A process is run in a virtual address space. In the abstract memory model, the virtual address
space is structured as a kernel segment plus a user segment. The linear addresses require
conversion to a physical address by either the processor or a separate memory management unit
(MMU). The virtual address of a LINUX process is segmented: a distinction is made between the
kernel segment and the user segment. A process typically has the arrangement shown below:

                                   User context                             Kernel Context

                                     Stack                                Kernel Data


                                Uninitialized data

                                Read-write data

                                Read-only data


The user segment
In user mode, privilege level 3 on x86 processor, a process can access only the user segment. As
the user segment contains the data and code for the process, this segment needs to be different
from those belonging to other processes, and this means in turn that the page directories, or at
least the individual page tables for the different processes, must also be different.

The system call brk
At the start of a process the value of the brk field in the process table entry points to end of the
BSS segment for non-statically initialized data. By modifying this pointer, the process can
allocate and release dynamic memory: this is usually done when the standard C function malloc()
is called. The system call brk() can be used to find the current value of the pointer or to set it to a
new value. The allocated memory is released using standard C function free() .

Mapping functions
The C library provides three functions in the header file sys/mman.h. The mmap() is used to map
the file in user segment and munmap() function to remove memory areas mapped to user
segment. These functions are useful to map the file into memory for fast access.

The kernel segment
As the page tables for kernel segment are identical for all processes, this ensures that any process
in the system mode will encounter the same kernel segment. In the kernel segment, physical
addresses and virtual addresses are the same except for the virtual memory areas mapped by

Kernal Memory Management Functions
In the kernel, it is often necessary to allocate dynamic memory, or example for temporary
buffers. The kernel offers a unified memory management to interface to the drivers.

Memory Allocation with kmalloc and kfree
The functions used for this are kmalloc() and kfree(). With kmalloc() one can reserve
     void *kmalloc(size_t size, int priority);
     void kfree(void *obj);
Some of important priorities are:
    GFP_BUFFER Used in managing the buffer cache, allows the allocator to sleep.
    GFP_ATOMIC Used to allocate memory from interrupt handler and other code outside of
                      a process context. Never sleeps.
    GFP_USER          Used to allocate memory on behalf of the user. It may sleep.
    GFP_KERNEL Normal allocation of kernel memory. May sleep.

Page-oriented memory Allocation
If module needs big chunks of memory , it is usually better to use a page-oriented technique.
To allocate big chunks of memory (approx 2 MB):
       Returns pointer to a new page and fills the page with zeros.
       Similar to get_zeroed_page, but does not clear the page.
       Allocates and returns a pointer of a memory area that is several pages long.
The prototypes of functions are:
       unsigned long __get_zeroed_page(int flags);
       void __get_free_pages(int flags);
       unsigned long __get_free_pages(int flags, unsigned long order);
The flags argument is same as with kmalloc().

When a program is done with pages the pages, it can free them with one of the following
       void free_page(unsigned long addr);
       void free_pages(unsigned long addr, unsigned long order);

vmalloc allocates a contiguous memory region in the virtual address space. Although pages are
not necessarily consecutive in physical memory, the kernel sees them as a contiguous range of
Using the functions vmalloc() and vmfree(), the memory can be allocated in multiples of a page
of memory. It is limited only by the size of free physical memory.
     void *vmalloc(unsigned long size);
     void *vmfree(void *addr);

Boot-Time Allocation
If you need a huge buffer of physically contiguous memory, you need to allocate it by requesting
memory at boot time. This technique is inelegant an inflexible, but it is also the least prone to
failure. A module can’t allocate memory at boot time, only drivers directly liked to the kernel
can do that.
Allocation at boot time is the only way to retrieve consecutive memory pages while bypassing
the limits imposed by get_free_pages on the buffer size, both in terms of maximum allowed size
and limited choice of sizes. Allocating memory at boot time bypasses all memory management
policies by reserving a private memory pool.
The boot-time allocation is only available for code linked in the kernel image, a device driver
using this kind of allocation can only be installed or replaced by rebuilding the kernel and
rebooting the computer.

Acquiring a Dedicated Buffer at Boot Time
When the kernel is booted, it gains access to all the physical memory available in the system. It
then initializes each of its subsystems by calling that subsystem’s initialization function,
allowing initialization code to allocate a memory buffer for private use by reducing the amount
of RAM left for normal system operation.
This kind of allocation is performed by calling one of these functions:
        #include <linux/bootmem.h>
        void *alloc_bootmem(unsigned long size);
The functions allocate either whole pages or non-page-aligned memory areas.

This way of allocating memory has several disadvantages, not the least being the inability to ever
free the buffer. After a driver has taken some memory, it has no way of returning it to the pool of
free pages. On the other hand, the advantage of this technique is that it makes available an area
of consecutive physical memory that is suitable for DMA. This is currently the only safe way in
the standard kernel to allocate a buffer of more than 32 consecutive pages. If, however you need
many pages and they don’t have to be physically contiguous, vmalloc is by far the best function
to use.

Reserving High RAM Addresses
Another option for allocating contiguous memory areas, is reserving a memory area at the end of
physical memory. You need to pass a command-line option to the kernel to limit the amount of
memory being managed. For example, use mem=31M to reserve 1 megabytes in a system that
actually has 32 megabytes of RAM. Later at runtime, this memory can be allocated and used by
device drivers using function:
        Bufptr = ioremap( 0x1f0000, /* 31MB /, 0x100000) /* 1M */);


Shared By:
Description: Interview questions and carreir developmwnt concept study materials,some tutorials
sreekanth reddy sreekanth reddy Software Engineer
About i am peaceful and kind person