malloc in kernel
Asked Answered
U

7

14

When I try to use malloc in a kernel module I get an error message from the compiler. My code:

res=(ListNode*)malloc(sizeof(ListNode));

The compilers error message is:

/root/ex3/ex3mod.c:491: error: implicit declaration of function ‘malloc’

What should I do?

Unbalance answered 22/5, 2010 at 14:22 Comment(1)
in other words as we all know #include <stdlib.h> doesnt exist in kernel what include should i had ?Unbalance
A
24

Note about the difference between the two allocation methods - kmalloc and kmem_cache, or vmalloc:

kmalloc: Best used for fast allocations that are smaller than a page (PAGE_SIZE, 0x1000 on most architectures). It doesn't involve mapping memory, so you get the memory straight from the kernel's 1:1 physical memory mapping. You get physically contingent memory. Note that if you want to allocate more than one page (i.e. order > 0), you risk bumping into external fragmentation issues - i.e. the call might fail even if there is enough free. Higher order - higher chance for allocation failure, and up-time plays a factor here too.

If you want to achieve maximal allocation efficiency then using your own kmem_cache for each type of struct is the way to go (the other benefits for this strategy are being able to monitor the state of your allocations from /proc and catching memory leaks more easily).

vmalloc: Allocations of more than one page. You get mapped-memory in kernel space. Behind the scenes it is similar to what userspace gets - the kernel allocates a bunch of pages and maps them in a virtual address space. This allocation is slower than kmalloc's, and memory accesses might incur a bit more overhead.

Annabelleannabergite answered 2/11, 2011 at 22:40 Comment(0)
L
21

Use kmalloc or vmalloc instead (see also this)

Lauds answered 22/5, 2010 at 14:31 Comment(2)
Or vmalloc(). Memory allocation works differently in the kernel.Nicolas
these links are dead; perfect example of why link-only answers are a bad idea. this answer is at the top of a google search for "can i use malloc in a kernel module?".Antimacassar
M
7

You can't use libraries in the kernel. None whatsoever.

This means that ANY function you're calling in the kernel needs to be defined in the kernel. Linux does not define a malloc, hence you can't use it.

There is a memory allocator and a family of memory allocation functions. Read the kernel docs on the memory allocator for more information.

Incidentially, there are a few functions the kernel defines which are in the standard C library as well; this is for convenience.

It does, for instance, defined snprintf

Mendes answered 22/5, 2010 at 22:29 Comment(0)
L
2

use kmalloc/kfree. You may grep malloc in kernel source code.

Lavona answered 16/8, 2011 at 16:31 Comment(0)
J
1

You can imagine it like a three step process:

  1. User space library - malloc()

  2. System call - brk()

  3. Inside Kernel - kmalloc/vmalloc/gfp etc routines of memory manager

So If you are already at step 3, writing kernel module, then going back wouldn't make sense. So you need to use the kernel routines only for memory allocation.

Juggler answered 23/4, 2014 at 5:19 Comment(0)
C
0

Indeed, you should use kmalloc and friends, check the linux kernel for examples. e.g.: http://lxr.linux.no/linux+v3.0/drivers/infiniband/hw/mthca/mthca_provider.c#L1033

Castro answered 28/12, 2012 at 1:48 Comment(0)
C
0

None of the answers really answer the question. Compiler thinks you're re-declaring the function malloc, just don't redeclare it.

Coracle answered 1/6, 2024 at 21:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.