是什么vmalloc的和kmalloc的区别? [英] What is the difference between vmalloc and kmalloc?

查看:544
本文介绍了是什么vmalloc的和kmalloc的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我周围的一派,发现大多数人都主张使用的kmalloc ,因为你保证得到连续的内存物理块。然而,它也好像的kmalloc 如果一个连续的物理您想要块不能发现可能会失败。结果
什么是有一个连续的内存块的优势是什么?具体来说,为什么我需要有一个连续的物理的内存块中的系统调用的?是否有任何理由我不能只使用的vmalloc ?结果
最后,如​​果我是一个系统调用的处理过程中分配内存,我应该指定 GFP_ATOMIC ?是一个系统调用在原子上下文中执行?


  

GFP_ATOMIC 结果
  该分配高优先级
  不眠。这是标志
  在中断处理程序,使用底部
  半和其他情况下,您
  无法入睡。


  
  

GFP_KERNEL
  这是一个正常的分配和可能会阻止。这是使用标志
  在进程上下文code,当它是安全的睡眠。



解决方案

您只需要担心使用物理连续的内存如果缓冲区将由DMA设备访问在物理寻址总线(如PCI)。麻烦的是,许多系统调用没有办法知道他们的缓冲区最终将传递给DMA设备:一旦你通过缓冲区到另一个内核子系统,你真的无法知道它会去。即使内核不使用DMA设置缓冲区今天未来的发展可能会这么做。

vmalloc的往往比的kmalloc慢,因为它可以具有重新映射缓冲器空间分成一个几乎连续的范围。 kmalloc的永远重新映射,但如果不与GFP_ATOMIC kmalloc的所谓可以阻止。

的kmalloc被限制在缓冲区的大小可以提供:128千字节 *)。如果你需要一个真正的大缓冲区,你必须使用vmalloc的或一些其他类似的机制在引导保留高端内存。


  

*)这是早期的内核也是如此。在最新的内核(我测试的2.6.33.2),单一的kmalloc的最大尺寸可达4 MB! (我写了一个相当<一个href=\"http://kaiwantech.word$p$pss.com/2011/08/17/kmalloc-and-vmalloc-linux-kernel-memory-allocation-api-limits/\">detailed张贴在此)MDASH。 kaiwan


对于一个系统调用不需要通过GFP_ATOMIC到kmalloc的(),您可以使用GFP_KERNEL。你不是一个中断处理程序:申请code。通过陷阱的方式进入内核的情况下,它不是一个中断

I've googled around and found most people advocating the use of kmalloc, as you're guaranteed to get contiguous physical blocks of memory. However, it also seems as though kmalloc can fail if a contiguous physical block that you want can't be found.
What are the advantages of having a contiguous block of memory? Specifically, why would I need to have a contiguous physical block of memory in a system call? Is there any reason I couldn't just use vmalloc?
Finally, if I were to allocate memory during the handling of a system call, should I specify GFP_ATOMIC? Is a system call executed in an atomic context?

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_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.

解决方案

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.

*) This was true of earlier kernels. On recent kernels (I tested this on 2.6.33.2), max size of a single kmalloc is up to 4 MB! (I wrote a fairly detailed post on this.) — kaiwan

For a system call you don't need to pass GFP_ATOMIC to kmalloc(), you can use GFP_KERNEL. You're not an interrupt handler: the application code enters the kernel context by means of a trap, it is not an interrupt.

这篇关于是什么vmalloc的和kmalloc的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆