<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <HTML ><HEAD ><TITLE >kmalloc()/kfree() include/linux/slab.h</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Unreliable Guide To Hacking The Linux Kernel" HREF="book1.html"><LINK REL="UP" TITLE="Common Routines" HREF="c141.html"><LINK REL="PREVIOUS" TITLE=" copy_[to/from]_user() / get_user() / put_user() include/asm/uaccess.h " HREF="x159.html"><LINK REL="NEXT" TITLE="current include/asm/current.h" HREF="x221.html"></HEAD ><BODY CLASS="SECT1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Unreliable Guide To Hacking The Linux Kernel</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="x159.html" ACCESSKEY="P" ><<< Previous</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Common Routines</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="x221.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="ROUTINES-KMALLOC" ></A ><TT CLASS="FUNCTION" >kmalloc()</TT >/<TT CLASS="FUNCTION" >kfree()</TT > <TT CLASS="FILENAME" >include/linux/slab.h</TT ></H1 ><P > <I CLASS="EMPHASIS" >[MAY SLEEP: SEE BELOW]</I > </P ><P > These routines are used to dynamically request pointer-aligned chunks of memory, like malloc and free do in userspace, but <TT CLASS="FUNCTION" >kmalloc()</TT > takes an extra flag word. Important values: </P ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="CONSTANT" > GFP_KERNEL </TT ></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 ><TT CLASS="CONSTANT" > GFP_ATOMIC </TT ></DT ><DD ><P > Don't sleep. Less reliable than <TT CLASS="CONSTANT" >GFP_KERNEL</TT >, but may be called from interrupt context. You should <I CLASS="EMPHASIS" >really</I > have a good out-of-memory error-handling strategy. </P ></DD ><DT ><TT CLASS="CONSTANT" > GFP_DMA </TT ></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" >kmem_grow: Called nonatomically from int </SPAN > warning message you called a memory allocation function from interrupt context without <TT CLASS="CONSTANT" >GFP_ATOMIC</TT >. You should really fix that. Run, don't walk. </P ><P > If you are allocating at least <TT CLASS="CONSTANT" >PAGE_SIZE</TT > (<TT CLASS="FILENAME" >include/asm/page.h</TT >) bytes, consider using <TT CLASS="FUNCTION" >__get_free_pages()</TT > (<TT CLASS="FILENAME" >include/linux/mm.h</TT >). 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 <TT CLASS="FUNCTION" >vmalloc()</TT >. It'll allocate virtual memory in the kernel map. This block is not contiguous in physical memory, but the <SPAN CLASS="ACRONYM" >MMU</SPAN > 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 <TT CLASS="FUNCTION" >alloc_bootmem()</TT > routine. </P ><P > Before inventing your own cache of often-used objects consider using a slab cache in <TT CLASS="FILENAME" >include/linux/slab.h</TT > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="x159.html" ACCESSKEY="P" ><<< Previous</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="book1.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="x221.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><TT CLASS="FUNCTION" >copy_[to/from]_user()</TT > / <TT CLASS="FUNCTION" >get_user()</TT > / <TT CLASS="FUNCTION" >put_user()</TT > <TT CLASS="FILENAME" >include/asm/uaccess.h</TT ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="c141.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><TT CLASS="FUNCTION" >current</TT > <TT CLASS="FILENAME" >include/asm/current.h</TT ></TD ></TR ></TABLE ></DIV ></BODY ></HTML >