<?xml version="1.0" encoding="ANSI_X3.4-1968" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968" /><title>kmalloc()/kfree() include/linux/slab.h</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><link rel="home" href="index.html" title="Unreliable Guide To Hacking The Linux Kernel" /><link rel="up" href="ch06.html" title="Chapter 6. Common Routines" /><link rel="prev" href="ch06s02.html" title="copy_[to/from]_user() / get_user() / put_user() include/asm/uaccess.h" /><link rel="next" href="ch06s04.html" title="current include/asm/current.h" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center"><code class="function">kmalloc()</code>/<code class="function">kfree()</code> <code class="filename">include/linux/slab.h</code></th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch06s02.html">Prev</a> </td><th width="60%" align="center">Chapter 6. Common Routines</th><td width="20%" align="right"> <a accesskey="n" href="ch06s04.html">Next</a></td></tr></table><hr /></div><div class="sect1" title="kmalloc()/kfree() include/linux/slab.h"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="routines-kmalloc"></a><code class="function">kmalloc()</code>/<code class="function">kfree()</code> <code class="filename">include/linux/slab.h</code></h2></div></div></div><p> <span class="emphasis"><em>[MAY SLEEP: SEE BELOW]</em></span> </p><p> These routines are used to dynamically request pointer-aligned chunks of memory, like malloc and free do in userspace, but <code class="function">kmalloc()</code> takes an extra flag word. Important values: </p><div class="variablelist"><dl><dt><span class="term"> <code class="constant"> GFP_KERNEL </code> </span></dt><dd><p> May sleep and swap to free memory. Only allowed in user context, but is the most reliable way to allocate memory. </p></dd><dt><span class="term"> <code class="constant"> GFP_ATOMIC </code> </span></dt><dd><p> Don't sleep. Less reliable than <code class="constant">GFP_KERNEL</code>, but may be called from interrupt context. You should <span class="emphasis"><em>really</em></span> have a good out-of-memory error-handling strategy. </p></dd><dt><span class="term"> <code class="constant"> GFP_DMA </code> </span></dt><dd><p> Allocate ISA DMA lower than 16MB. If you don't know what that is you don't need it. Very unreliable. </p></dd></dl></div><p> If you see a <span class="errorname">sleeping function called from invalid context</span> warning message, then maybe you called a sleeping allocation function from interrupt context without <code class="constant">GFP_ATOMIC</code>. You should really fix that. Run, don't walk. </p><p> If you are allocating at least <code class="constant">PAGE_SIZE</code> (<code class="filename">include/asm/page.h</code>) bytes, consider using <code class="function">__get_free_pages()</code> (<code class="filename">include/linux/mm.h</code>). It takes an order argument (0 for page sized, 1 for double page, 2 for four pages etc.) and the same memory priority flag word as above. </p><p> If you are allocating more than a page worth of bytes you can use <code class="function">vmalloc()</code>. It'll allocate virtual memory in the kernel map. This block is not contiguous in physical memory, but the <acronym class="acronym">MMU</acronym> makes it look like it is for you (so it'll only look contiguous to the CPUs, not to external device drivers). If you really need large physically contiguous memory for some weird device, you have a problem: it is poorly supported in Linux because after some time memory fragmentation in a running kernel makes it hard. The best way is to allocate the block early in the boot process via the <code class="function">alloc_bootmem()</code> routine. </p><p> Before inventing your own cache of often-used objects consider using a slab cache in <code class="filename">include/linux/slab.h</code> </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch06s02.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch06.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch06s04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top"> <code class="function">copy_[to/from]_user()</code> / <code class="function">get_user()</code> / <code class="function">put_user()</code> <code class="filename">include/asm/uaccess.h</code>  </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> <code class="function">current</code> <code class="filename">include/asm/current.h</code></td></tr></table></div></body></html>