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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4401912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:15:19  来源:igfitidea点击:

Contiguous physical memory from userspace

linuxmemory

提问by xffox

Is there a way to allocate contiguous physical memory from userspace in linux? At least few guaranteed contiguous memory pages. One huge page isn't the answer.

有没有办法在linux中从用户空间分配连续的物理内存?至少有几个保证连续的内存页面。一个巨大的页面不是答案。

采纳答案by Brad

No. There is not. You doneed 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 knowingif 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 (i.e. Kernel) service that needed this requirement. So again, you would have to deal with it at thatlevel.

您需要这样做的唯一原因 - 是如果您正在与需要此要求的硬件或其他一些低级(即内核)服务结合工作。再说一次,你必须在那个级别处理它。

So the answer isn't just "you can't" - but "you should never need to".

所以答案不仅仅是“你不能”——而是“你永远不需要”。

I have written such memory managers that doallow 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. Again, all of this had to be addressed in kernel space.

我已经编写了这样的内存管理器,确实允许我这样做 - 但它总是因为内核级别的一些潜在问题,必须在内核级别解决。通常是因为总线上的其他一些代理(PCI 卡、BIOS 甚至另一台通过 RDMA 接口的计算机)具有物理连续内存要求。同样,所有这些都必须在内核空间中解决。

When you talk about "cache lines" - you don't need to worry. You can be assured that each pageof your user-space memory is contiguous, and each pageis much larger than a cache-line (no matter what architecture you're talking about).

当您谈论“缓存行”时 - 您无需担心。您可以放心,每个页面的用户存储空间的是连续的,并且每一比高速缓存行(无论你在说什么架构)大得多。

回答by Ponkadoodle

Yes, if all you need is a few pages, this may indeed be possible.

是的,如果您只需要几页,这确实是可能的。

The file /proc/[pid]/pagemapnow allows programs to inspect the mapping of their virtual memory to physical memory.

该文件/proc/[pid]/pagemap现在允许程序检查其虚拟内存到物理内存的映射。

While you cannot explicitly modify the mapping, you canjust 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 happento get enough blocks touching eachother to create a large enough contiguous block. Then unlock and free your excess blocks.

虽然您无法显式修改映射,但您可以只分配一个虚拟页面,通过调用 将mlock其锁定到内存中,通过查找记录其物理地址到 中/proc/self/pagemap,然后重复直到碰巧有足够的块相互接触以创建一个大足够的连续块。然后解锁并释放多余的块。

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.

它是骇人听闻的,笨重的并且可能很慢,但值得一试。另一方面,这很可能不是您真正需要的。

回答by mrsmith

DPDK library's memory allocator uses approach @Wallacoloo described. eal_memory.c. The code is BSD licensed.

DPDK 库的内存分配器使用@Wallacoloo 描述的方法。eal_memory.c。该代码是 BSD 许可的。

回答by Ikjn

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.

这是因为物理上连续的约束不是来自用户应用程序,而是来自设备,所以只有设备驱动程序应该关心。