在内核空间中创建连续区域

发布于 2025-01-01 23:47:14 字数 185 浏览 1 评论 0原文

我通过 alloc_page(GFP_USER) 分配内存,并且分配了不止一页。 我需要将其视为连续区域(我需要从它的缓冲区中创建),所以我想撤销它*(vm_beg + off)

我知道这可以通过用户空间中的 mmap 实现,但是我如何在内核空间中做到这一点?

I'm allocating memory by alloc_page(GFP_USER) and I have allocated more than one page.
I need to make that I will see it as contiguous region (I need to make from it buffer), so I want to revoke to it *(vm_beg + off).

I know that is possible by mmap in user space but how I can do that in kernel space?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有你我更好 2025-01-08 23:47:14

如果您需要虚拟分配连续内存,则可以使用vmalloc()来执行此操作(更多信息此处)。不过,实际的物理内存可能是分散的,因此,如果您计划将该缓冲区与 CPU MMU 外部的某些硬件块(例如 DMA、PCI 总线)一起使用,那么 vmalloc() 可能不适合您最好的选择。

对于大多数用途,您可能应该使用 kmalloc() (更多信息此处 )。不同之处在于,vmalloc() 将始终修改页表以获取新内存(有点像用户态中的 mmap())。另一个区别是 kmalloc() 为您提供了物理上连续的内存,并且通常比必须始终修改页表的 vmalloc() 更快。

关于 Linux 内存的一个非常好的信息来源是 Linux Device Drivers 3 (特别是您的情况下的第 15 章) ,您可能会在其中找到大多数问题的答案。

If you need to allocate virtually contiguous memory, then you can use vmalloc() to do so (more info here). The actual physical memory might be scattered though, so if you are planning on using that buffer with some hardware block (e.g DMA, PCI bus) outside of your CPU MMU, then vmalloc() might not be your best bet.

You probably should use kmalloc() for most purposes (more info here). The difference is that vmalloc() will always modify the page tables to get you new memory (a bit like mmap() in userland). Another difference is that kmalloc() gives you phisically contiguous memory and is usually faster than vmalloc() which has to always modify the page tables.

A very good source of information on Linux memory is Linux Device Drivers 3 (specifically Chapter 15 in your case), you will probably find the answer to most of your questions inside.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文