NAME
__get_free_pages, get_free_page, __get_free_page,
__get_dma_pages, free_pages, free_page, kmalloc, kfree,
kfree_s, vmalloc, vfree - Allocate and free dynamic kernel
memory
SYNOPSIS
<strong>#include</strong> <strong><linux/malloc.h></strong>
<strong>#include</strong> <strong><linux/mm.h></strong>
<strong>unsigned</strong> <strong>long</strong> <strong>__get_free_pages(int</strong> <strong>priority,</strong> <strong>unsigned</strong> <strong>long</strong> <strong>gfporder);</strong>
<strong>unsigned</strong> <strong>long</strong> <strong>__get_free_page(int</strong> <strong>priority);</strong>
<strong>unsigned</strong> <strong>long</strong> <strong>get_free_page(int</strong> <strong>priority);</strong>
<strong>void</strong> <strong>free_pages(unsigned</strong> <strong>long</strong> <strong>addr,</strong> <strong>unsigned</strong> <strong>long</strong> <strong>order);</strong>
<strong>void</strong> <strong>free_page(addr);</strong>
<strong>void</strong> <strong>*kmalloc</strong> <strong>(size_t</strong> <strong>size,</strong> <strong>int</strong> <strong>priority)</strong>
<strong>void</strong> <strong>kfree_s(void</strong> <strong>*</strong> <strong>obj,</strong> <strong>int</strong> <strong>size);</strong>
<strong>void</strong> <strong>kfree(void</strong> <strong>*obj);</strong>
<strong>void</strong> <strong>*</strong> <strong>vmalloc(unsigned</strong> <strong>long</strong> <strong>size);</strong>
<strong>void</strong> <strong>vfree(void</strong> <strong>*</strong> <strong>addr);</strong>
DESCRIPTION
<strong>__get_free_pages()</strong>
allocates 2^<em>gfporder</em> consecutive pages in kernel
space.
<em>priority</em> is one of <strong>GFP_BUFFER,</strong> <strong>GFP_ATOMIC,</strong> <strong>GFP_KER-</strong>
<strong>NEL,</strong> <strong>GFP_USER,</strong> <strong>GFP_NOBUFFER,</strong> <strong>GFP_NFS</strong> or <strong>GFP_DMA.</strong>
<strong>GFP_BUFFER</strong>
has the lowest priority, and doesn't try to free
other pages if the requested memory isn't avail-
able.
<strong>GFP_ATOMIC</strong>
tries to allocate the memory immediately. The task
will not sleep if the memory isn't available.
There is a number of reserved pages for GFP_ATOMIC.
For allocating memory on interrupt this has to be
used.
<strong>GFP_KERNEL</strong>
is the normal way to allocate memory in the kernel
space. The reserved pages will not be used, and if
the memory is not available immediately,
<em>try</em><strong>_</strong><em>to</em><strong>_</strong><em>free</em><strong>_</strong><em>page()</em> will be called.
<strong>GFP_USER</strong>
is currently the same as GFP_KERNEL.
<strong>GFP_NOBUFFER</strong>
doesn't try to shrink the buffer cache for memory
allocation. This is used in kernel for allocating
pages for the buffer cache.
<strong>GFP_NFS</strong>
is the same as GFP_KERNEL, but the number of the
reserved pages is lower, so this will succeed
faster.
<strong>GFP_DMA</strong>
Has no effect in __get_free_pages(). This flag is
only for use with kmalloc(). For DMA memory kmal-
loc or __get_dma_pages should be used. Description
of effect see __get_dma_pages().
<strong>__get_dma_pages()</strong>
calls repeatedly __get_free_pages() until pages
suitable for dma are found. After success the mis-
takenly allocated pages will be freed. This func-
tion is necessary because PC DMA controlles are
limited to 16MB. DMA Pages may not cross 64k
boundaries. This is guaranteed by the allocation
algorithm of __get_free_pages(). This function
<strong>__get_free_page()</strong>
allocates one page. It's a macro which calls
__get_free_pages() with <em>gfporder</em> 0.
<strong>get_free_page()</strong>
Same as __get_free_page(), except the allocated
memory is set to zero.
<strong>free_pages()</strong>
frees the memory space starting at addr, which
must have been returned by a previous call to
__get_free_pages(). <em>order</em> has to be the same
__get_free_pages() was called with. Note that <em>addr</em>
is unsigned long.
<strong>free_page()</strong>
frees the memory page starting at <em>addr</em>, which must
have been returned by a previous call to
__get_free_page() or get_free_page(). Note that
<em>addr</em> is unsigned long.
<strong>kmalloc()</strong>
allocates <em>size</em> bytes, and returns a pointer to the
allocated memory. On i386 the following bucket
sizes are possible: 24, 56, 120, 244, 500, 1012,
2032, 4072, 8168, 16360, 32744, 65512 and 131048
bytes. ( See linux/mm/kmalloc.c ) If an other size
is kmalloc'ed the the next bigger one is allo-
cated. For <em>priority</em> see <strong>__get_free_pages.</strong>
<strong>kfree_s()</strong>
frees the memory object pointed to by <em>obj</em>. <em>size</em>
should be the size of the memory object or zero.
<em>kfree</em><strong>_</strong><em>s()</em> determines the size of the object from
the block header. If <em>size</em> is not zero it will be
checked for being correct.
<strong>kfree()</strong>
frees the memory object pointed to by <em>obj</em>. It is a
macro which calls kfree_s() with <em>size</em> zero.
<strong>vmalloc()</strong>
allocates <em>size</em> bytes, and returns a pointer to the
allocated memory. size becomes page aligned by
vmalloc(), so the smallest allocated amount is 4kB.
The allocated pages are mapped to the virtual mem-
ory space behind the 1:1 mapped physical memory in
the kernel space. Behind every vmalloc'ed area
there is at least one unmapped page. So writing
behind the end of a vmalloc'ed area will not result
in a system crash, but in a segmentation violation
in the kernel space. Because memory fragmentation
isn't a big problem for vmalloc(), vmalloc() should
be used for huge amounts of memory.
<strong>vfree()</strong>
frees the virtual memory area pointed to by <em>addr</em>,
which must have been allocated by a previous call
to vmalloc().
RETURN VALUES
<strong>__get_free_pages(),</strong> <strong>__get_free_page()</strong> and <strong>get_free_page</strong>
return on success an unsigned long which is the address of
the start of the allocated memory, and should normally be
cast to a pointer. On failure zero is returned.
<strong>kmalloc</strong> and <strong>vmalloc</strong> return a pointer to the allocated mem-
ory on success and <strong>NULL</strong> on failure.
ERRORS
If one of the functions __get_free_pages(),
__get_free_page(), get_free_page(), or kmalloc() is called
from interrupt, and priority is not GFP_ATOMIC, syslog
will give a warning.
SOURCES
<strong>linux/mm/kmalloc.c</strong>
<strong>linux/mm/vmalloc.c</strong>
<strong>linux/mm/swap.c</strong>
<strong>linux/include/linux/mm.h</strong>
<strong>linux/include/linux/malloc.h</strong>
BUGS/LIMITAIONS
Because of memory fragmentation it's still insecure to
allocate areas of many pages with kmalloc. So for areas
bigger than one page vmalloc() should be used. Only for
DMA kmalloc or __get_dma_pages() has to be used.
AUTHOR
Linus Torvalds
转载请注明:在路上 » kmalloc
Post Views: 815