Chinaunix首页 | 论坛 | 博客
  • 博客访问: 87573
  • 博文数量: 15
  • 博客积分: 581
  • 博客等级: 中士
  • 技术积分: 185
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-23 15:29
文章分类
文章存档

2009年(10)

2008年(5)

我的朋友

分类: LINUX

2009-02-01 10:10:24

kmalloc returns physically contiguous memory, malloc does
not guarantee anything about the physical memory mapping.

The other main difference is that kmalloc'ed memory is
reserved and locked, it cannot be swapped. malloc does not
actually allocate physical memory. Physical memory gets
mapped later, during use.

Memory is subject to fragmentation. You may have many megabytes
of free memory left, but a 8K kmalloc (on a 4k page machine)
can still fail. Things like the buddy allocator help but
can't always guarantee large unfragmented blocks.

If you don't need contiguous mapping in kernel space, you
can use vmalloc to avoid the fragmentation problem, at the
cost of some MMU overhead.

Userspace memory (via malloc) is virtual - immediately after
the call, the same non-writable zero page is mapped into every
4K block of your memory region. Only when you write to a page,
a physical memory page is mapped for you at that location.

If swap is enabled, that page may be 'borrowed' from some other
process, after the contents are deposited in the swap file.


You only need to worry about using physically contiguous memory if the buffer will be accessed by a DMA device on a physically addressed bus (like PCI). The trouble is that many system calls have no way to know whether their buffer will eventually be passed to a DMA device: once you pass the buffer to another kernel subsystem, you really cannot know where it is going to go. Even if the kernel does not use the buffer for DMA today, a future development might do so.

vmalloc is often slower than kmalloc, because it may have to remap the buffer space into a virtually contiguous range. kmalloc never remaps, though if not called with GFP_ATOMIC kmalloc can block.

kmalloc is limited in the size of buffer it can provide: 128 KBytes. If you need a really big buffer, you have to use vmalloc or some other mechanism like reserving high memory at boot.

vmalloc() is very rarely used, because the kernel rarely uses virtual memory. kmalloc() is what is typically used, but you have to know what the consequences of the different flags are and you need a strategy for dealing with what happens when it fails - particularly if you're in an interrupt handler


kmalloc allocates physically contiguous memory, memory which
pages are laid consecutively in physical RAM. vmalloc allocates
memory which is contiguous in kernel virtual memory space (that means
pages allocated that way are not contiguous in RAM, but the kernel
sees them as one block).

kmalloc is the preffered way, as long as you don't need very big
areas. The trouble is, if you want to do DMA from/to some hardware
device, you'll need to use kmalloc, and you'll probably need bigger
chunk. The solution is to allocate memory as soon as possible, before
memory gets fragmented.


总结:
  1. kmalloc和vmalloc是分配的是内核的内存,malloc分配的是用户的内存
  2. kmalloc保证分配的内存在物理上是连续的,vmalloc保证的是在虚拟地址空间上的连续,malloc不保证任何东西(这点是自己猜测的,不一定正确)
  3. kmalloc能分配的大小有限,vmalloc和malloc能分配的大小相对较大
  4. 内存只有在要被DMA访问的时候才需要物理上连续
  5. vmalloc比kmalloc要慢


阅读(3124) | 评论(1) | 转发(0) |
0

上一篇:用repeater有什么限制?

下一篇:没有了

给主人留下些什么吧!~~

时间看来2012-03-18 23:18:38