- void * kmalloc (size_t size, int flags) : 在kernel配置記憶體位置
- INCLUDE :include/linux/slab.h
- FLAG:
flags:
Action Modifiers( 通常使用後面提到的type modifier)
__GFP_WAIT
|
The allocator can sleep.
|
__GFP_HIGH
|
The allocator can access emergency pools.
|
__GFP_IO
|
The allocator can start physical I/O.
|
__GFP_FS
|
The allocator can start filesystem I/O.
|
__GFP_COLD
|
The allocator should use cache cold pages.
|
__GFP_NOWARN
|
The allocator will not print failure warnings.
|
__GFP_REPEAT
|
The allocator will repeat the allocation if it fails, but the allocation can potentially fail. This depends upon the particular VM implementation.
|
__GFP_NOFAIL
|
The allocator will indefinitely repeat the allocation. The allocation cannot fail.
|
__GFP_NORETRY
|
The allocator will never retry if the allocation fails.
|
__GFP_COMP
|
Add compound page metadata. Used internally by thehugetlb code.
|
__GFP_ZERO
|
Add compound page metadata. Used internally by thehugetlb code.
|
__GFP_NOMEMALLOC
|
Don't use emergency reserves.
|
__GFP_HARDWALL
|
Enforce hardwall cpuset memory allocs.
|
__GFP_THISNODE
|
No fallback, no policies.
|
__GFP_RECLAIMABLE
|
Page is reclaimable.
|
__GFP_NOTRACK
|
Don't track with kmemcheck.
|
__GFP_NO_KSWAPD
| |
__GFP_OTHER_NODE
|
On behalf of other node.
|
Example:
ptr = kmalloc(size, __GFP_WAIT | __GFP_IO | __GFP_FS);
Zone Modifiers ( 選擇要從哪分配空間 預設是從ZONE_NORMAL)
__GFP_DMA
|
Allocate only from ZONE_DMA
|
__GFP_HIGHMEM
|
Allocate from ZONE_HIGHMEM or ZONE_NORMAL
|
__GFP_DMA32
| |
__GFP_MOVABLE
|
Type Flags( 定義了action 及 zone modifier)
GFP_ATOMIC
| |
GFP_NOIO
| |
GFP_NOFS
| |
GFP_KERNEL
| |
GFP_USER
| |
GFP_HIGHUSER
| |
GFP_DMA
|
This is an allocation from ZONE_DMA. Device drivers that need DMA-able memory use this flag, usually in combination with one of the above.
|
以下列出各Type modifier的組合:
GFP_ATOMIC
|
__GFP_HIGH
|
GFP_NOIO
|
__GFP_WAIT
|
GFP_NOFS
|
(__GFP_WAIT | __GFP_IO)
|
GFP_KERNEL
|
(__GFP_WAIT | __GFP_IO | __GFP_FS)
|
GFP_USER
|
(__GFP_WAIT | __GFP_IO | __GFP_FS)
|
GFP_HIGHUSER
|
(__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HIGHMEM)
|
GFP_DMA
|
__GFP_DMA
|
建議的用法:
Situation
|
Solution
|
---|---|
Process context, can sleep
|
Use GFP_KERNEL
|
Process context, cannot sleep
|
Use GFP_ATOMIC, or perform your allocations with GFP_KERNEL at an earlier or later point when you can sleep
|
Interrupt handler
|
Use GFP_ATOMIC
|
Softirq
|
Use GFP_ATOMIC
|
Tasklet
|
Use GFP_ATOMIC
|
Need DMA-able memory, can sleep
|
Use (GFP_DMA | GFP_KERNEL)
|
Need DMA-able memory, cannot sleep
|
Use (GFP_DMA | GFP_ATOMIC), or perform your allocation at an earlier point when you can sleep
|
Example:
char *reloc_lp0; reloc_lp0 = kmalloc(size,GFP_KERNEL); WARN_ON(!reloc_lp0); if (!reloc_lp0) { pr_err("%s: Failed to allocate reloc_lp0\n",__func__); goto out; } ... kfree(reloc_lp0);
Reference:
include/linux/gfp.h
http://www.makelinux.net/books/lkd2/ch11lev1sec4
http://www.google.com.tw/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&ved=0CF8QFjAE&url=http%3A%2F%2Fant.comm.ccu.edu.tw%2Fcourse%2F96_Driver%2F0_Lectures%2FChap7-Getting%2520Hold%2520of%2520Memory_milk.ppt&ei=TMFqT_ysHqWfmQW5obyWBg&usg=AFQjCNGvVEk9-Io481khM_SqoFd-WsvwqQ&sig2=qvOIAsidZzvw15vpq0JlrQ
沒有留言:
張貼留言