1<html><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.78.1"><link rel="home" href="index.html" title="Unreliable Guide To Hacking The Linux Kernel"><link rel="up" href="common-routines.html" title="Chapter 6. Common Routines"><link rel="prev" href="routines-copy.html" title="copy_[to/from]_user() / get_user() / put_user() include/asm/uaccess.h"><link rel="next" href="routines-current.html" title="current include/asm/current.h"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><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> 2 <code class="filename">include/linux/slab.h</code></th></tr><tr><td width="20%" align="left"><a accesskey="p" href="routines-copy.html">Prev</a> </td><th width="60%" align="center">Chapter 6. Common Routines</th><td width="20%" align="right"> <a accesskey="n" href="routines-current.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="routines-kmalloc"></a><code class="function">kmalloc()</code>/<code class="function">kfree()</code> 3 <code class="filename">include/linux/slab.h</code></h2></div></div></div><p> 4 <span class="emphasis"><em>[MAY SLEEP: SEE BELOW]</em></span> 5 </p><p> 6 These routines are used to dynamically request pointer-aligned 7 chunks of memory, like malloc and free do in userspace, but 8 <code class="function">kmalloc()</code> takes an extra flag word. 9 Important values: 10 </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"> 11 <code class="constant"> 12 GFP_KERNEL 13 </code> 14 </span></dt><dd><p> 15 May sleep and swap to free memory. Only allowed in user 16 context, but is the most reliable way to allocate memory. 17 </p></dd><dt><span class="term"> 18 <code class="constant"> 19 GFP_ATOMIC 20 </code> 21 </span></dt><dd><p> 22 Don't sleep. Less reliable than <code class="constant">GFP_KERNEL</code>, 23 but may be called from interrupt context. You should 24 <span class="emphasis"><em>really</em></span> have a good out-of-memory 25 error-handling strategy. 26 </p></dd><dt><span class="term"> 27 <code class="constant"> 28 GFP_DMA 29 </code> 30 </span></dt><dd><p> 31 Allocate ISA DMA lower than 16MB. If you don't know what that 32 is you don't need it. Very unreliable. 33 </p></dd></dl></div><p> 34 If you see a <span class="errorname">sleeping function called from invalid 35 context</span> warning message, then maybe you called a 36 sleeping allocation function from interrupt context without 37 <code class="constant">GFP_ATOMIC</code>. You should really fix that. 38 Run, don't walk. 39 </p><p> 40 If you are allocating at least <code class="constant">PAGE_SIZE</code> 41 (<code class="filename">include/asm/page.h</code>) bytes, 42 consider using <code class="function">__get_free_pages()</code> 43 44 (<code class="filename">include/linux/mm.h</code>). It 45 takes an order argument (0 for page sized, 1 for double page, 2 46 for four pages etc.) and the same memory priority flag word as 47 above. 48 </p><p> 49 If you are allocating more than a page worth of bytes you can use 50 <code class="function">vmalloc()</code>. It'll allocate virtual memory in 51 the kernel map. This block is not contiguous in physical memory, 52 but the <acronym class="acronym">MMU</acronym> makes it look like it is for you 53 (so it'll only look contiguous to the CPUs, not to external device 54 drivers). If you really need large physically contiguous memory 55 for some weird device, you have a problem: it is poorly supported 56 in Linux because after some time memory fragmentation in a running 57 kernel makes it hard. The best way is to allocate the block early 58 in the boot process via the <code class="function">alloc_bootmem()</code> 59 routine. 60 </p><p> 61 Before inventing your own cache of often-used objects consider 62 using a slab cache in 63 <code class="filename">include/linux/slab.h</code> 64 </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="routines-copy.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="common-routines.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="routines-current.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top"> 65 <code class="function">copy_[to/from]_user()</code> 66 / 67 <code class="function">get_user()</code> 68 / 69 <code class="function">put_user()</code> 70 <code class="filename">include/asm/uaccess.h</code> 71  </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> 72 <code class="filename">include/asm/current.h</code></td></tr></table></div></body></html> 73