简体   繁体   English

来自用户空间的连续物理内存

[英]Contiguous physical memory from userspace

Is there a way to allocate contiguous physical memory from userspace in linux?有没有办法在linux中从用户空间分配连续的物理内存? At least few guaranteed contiguous memory pages.至少有几个保证连续的内存页面。 One huge page isn't the answer.一个巨大的页面不是答案。

No. There is not.不,那里没有。 You do need to do this from Kernel space.确实需要从内核空间执行此操作。

If you say "we need to do this from User Space" - without anything going on in kernel-space it makes little sense - because a user space program has no way of controlling or even knowing if the underlying memory is contiguous or not.如果你说“我们需要从用户空间做这件事”——在内核空间没有任何事情发生,那就没什么意义了——因为用户空间程序无法控制甚至不知道底层内存是否是连续的。

The only reason where you would need to do this - is if you were working in-conjunction with a piece of hardware, or some other low-level (ie Kernel) service that needed this requirement.您需要这样做的唯一原因 - 是如果您正在与需要此要求的硬件或其他一些低级(即内核)服务结合工作。 So again, you would have to deal with it at that level.再说一次,你必须在那个级别处理它。

So the answer isn't just "you can't" - but "you should never need to".所以答案不仅仅是“你不能”——而是“你永远不需要”。

I have written such memory managers that do allow me to do this - but it was always because of some underlying issue at the kernel level, which had to be addressed at the kernel level.我已经编写了这样的内存管理器,确实允许我这样做 - 但它总是因为内核级别的一些潜在问题,必须在内核级别解决。 Generally because some other agent on the bus (PCI card, BIOS or even another computer over RDMA interface) had the physical contiguous memory requirement.通常是因为总线上的其他一些代理(PCI 卡、BIOS 甚至另一台通过 RDMA 接口的计算机)具有物理连续内存要求。 Again, all of this had to be addressed in kernel space.同样,所有这些都必须在内核空间中解决。

When you talk about "cache lines" - you don't need to worry.当您谈论“缓存行”时 - 您无需担心。 You can be assured that each page of your user-space memory is contiguous, and each page is much larger than a cache-line (no matter what architecture you're talking about).您可以放心,您的用户存储空间的每一是连续的,并且每一比高速缓存行(无论你在说什么架构)大得多。

Yes, if all you need is a few pages, this may indeed be possible.是的,如果您只需要几页,这确实是可能的。

The file /proc/[pid]/pagemap now allows programs to inspect the mapping of their virtual memory to physical memory.文件/proc/[pid]/pagemap现在允许程序检查其虚拟内存到物理内存的映射。

While you cannot explicitly modify the mapping, you can just allocate a virtual page, lock it into memory via a call to mlock , record its physical address via a lookup into /proc/self/pagemap , and repeat until you just happen to get enough blocks touching eachother to create a large enough contiguous block.虽然您无法显式修改映射,但您可以只分配一个虚拟页面,通过调用mlock将其锁定到内存中,通过查找/proc/self/pagemap记录其物理地址,然后重复直到恰好获得足够的块相互接触以创建足够大的连续块。 Then unlock and free your excess blocks.然后解锁并释放多余的块。

It's hackish, clunky and potentially slow, but it's worth a try.它是骇人听闻的,笨重的并且可能很慢,但值得一试。 On the other hand, there's a decently large chance that this isn't actually what you really need.另一方面,这很可能不是您真正需要的。

DPDK library's memory allocator uses approach @Wallacoloo described. DPDK 库的内存分配器使用@Wallacoloo 描述的方法。 eal_memory.c . eal_memory.c The code is BSD licensed.该代码是 BSD 许可的。

if specific device driver exports dma buffer which is physical contiguous, user space can access through dma buf apis so user task can access but not allocate directly如果特定设备驱动程序导出物理连续的 dma 缓冲区,则用户空间可以通过 dma buf apis 访问,因此用户任务可以访问但不能直接分配

that is because physically contiguous constraints are not from user aplications but only from device so only device drivers should care.这是因为物理上连续的约束不是来自用户应用程序,而是来自设备,所以只有设备驱动程序应该关心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM