Xalanz's 网络日志(Ethan's blog)

阳光风雨中的泥土Mud under the sunlight and rain

Linux Kernel 的kmalloc()

,

[By Xalanz]Linux 内核里面分配内存比用户空间复杂了一些,比如常用的kmalloc()函数,它多了一个参数,呵呵,这个参数可以控制kmalloc()的很多的行为特性.

其实说简单也简单:
void * kmalloc (size_t size, int flags);
中的flags可以传递给kmalloc()以下的3类的信息:
1、分配行为的方式 比如kmalloc()可以不可以睡觉等待资源的可用,可以睡觉,意味着kmalloc()阻塞,出让CPU.
Flag Description
__GFP_COLD The kernel should use cache cold pages.
__GFP_FS The kernel can start filesystem I/O.
__GFP_HIGH The kernel can access emergency pools.
__GFP_IO The kernel can start disk I/O.
__GFP_NOFAIL The kernel can repeat the allocation.
__GFP_NORETRY The kernel does not retry if the allocation fails.
__GFP_NOWARN The kernel does not print failure warnings.
__GFP_REPEAT The kernel repeats the allocation if it fails.
__GFP_WAIT The kernel can sleep.

2、从哪儿分配内存给谁用 比如硬件要使用可以DMA的内存。
Flag Description
__GFP_DMA Allocate only DMA-capable memory.
No flag Allocate from wherever available.
3、分配内存的类型 是1、2的组合以形成特性的分配类型
Flag Description
GFP_ATOMIC The allocation is high-priority and does not sleep. This is the flag to use in interrupt handlers, bottom halves and other situations where you cannot sleep.
GFP_DMA This is an allocation of DMA-capable memory. Device drivers that need DMA-capable memory use this flag.
GFP_KERNEL This is a normal allocation and might block. This is the flag to use in process context code when it is safe to sleep.
GFP_NOFS This allocation might block and might initiate disk I/O, but it does not initiate a filesystem operation. This is the flag to use in filesystem code when you cannot start another filesystem operation.
GFP_NOIO This allocation might block, but it does not initiate block I/O. This is the flag to use in block layer code when you cannot start more block I/O.
GFP_USER This is a normal allocation and might block. This flag is used to allocate memory for user-space processes.
呵呵,看的出来,它们的关系也不是很单纯.
一些等效关系:

Flag Value
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_DMA __GFP_DMA

Linux pipe 管道到底是什么? 跟踪mkfifo到内核Citroen 的超级1.6 16v 引擎

Comments

Unregistered user Sunday, August 9, 2009 7:20:53 PM

匿名 writes: 路过

Write a comment

New comments have been disabled for this post.