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

问题描述

我搜索了一下,发现大多数人都提倡使用 kmalloc,因为您可以保证获得连续的物理内存块.但是,如果找不到您想要的连续 物理 块,kmalloc 似乎也会失败.
连续的内存块有什么好处?具体来说,为什么我需要在系统调用中有一个连续的物理内存块?有什么理由我不能只使用 vmalloc?
最后,如果我要在处理系统调用期间分配内存,我应该指定 GFP_ATOMIC 吗?系统调用是否在原子上下文中执行?

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
分配是高优先级的不睡觉.这是旗帜在中断处理程序中使用,底部一半和其他情况,你睡不着.

GFP_KERNEL这是一个正常的分配,可能会阻塞.这是要使用的标志在可以安全休眠时的进程上下文代码中.

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.

推荐答案

如果缓冲区将由物理寻址总线(如 PCI)上的 DMA 设备访问,您只需担心使用物理连续内存.问题在于,许多系统调用无法知道它们的缓冲区最终是否会传递给 DMA 设备:一旦将缓冲区传递给另一个内核子系统,您就真的不知道它会去哪里.即使内核今天不使用 DMA 缓冲区,未来的开发可能会这样做.

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 通常比 kmalloc 慢,因为它可能必须将缓冲区空间重新映射到几乎连续的范围.kmalloc 从不重新映射,但如果不使用 GFP_ATOMIC 调用 kmalloc 可以阻塞.

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 受限于它可以提供的缓冲区大小:128 KB.如果您需要一个非常大的缓冲区,则必须使用 vmalloc 或其他一些机制,例如在启动时保留高内存.

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.

早期内核确实如此.在最近的内核上(我在 2.6.33.2 上测试过),单个 kmalloc 的最大大小高达 4 MB!(我写了一个相当有关此内容的详细帖子.)—开湾

对于系统调用,您不需要将 GFP_ATOMIC 传递给 kmalloc(),您可以使用 GFP_KERNEL.您不是中断处理程序:应用程序代码通过陷阱进入内核上下文,它不是中断.

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 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 15:57