没有物理内存分配的虚拟内存分配
我正在开发一个 Linux 内核项目,我需要找到一种在不分配物理内存的情况下分配虚拟内存的方法。例如,如果我使用这个:
char* buffer = my_virtual_mem_malloc(sizeof(char) * 512);
my_virtual_mem_malloc 是我的内核模块实现的新 SYSCALL。写入此缓冲区的所有数据都通过使用套接字存储在文件或其他服务器上(而不是存储在物理内存上)。因此,为了完成这项工作,我需要请求虚拟内存并访问 vm_area_struct 结构以重新定义 vm_ops 结构。
您对此有什么想法吗?
谢谢
I'm working on a Linux kernel project and i need to find a way to allocate Virtual Memory without allocating Physical Memory. For example if I use this :
char* buffer = my_virtual_mem_malloc(sizeof(char) * 512);
my_virtual_mem_malloc is a new SYSCALL implemented by my kernel module. All data written on this buffer is stocked on file or on other server by using socket (not on Physical Memory). So to complete this job, i need to request Virtual Memory and get access to the vm_area_struct structure to redefine vm_ops struct.
Do you have any ideas about this ?
thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这在架构上是不可能的。您可以创建具有回写例程的 vm 区域,该例程将数据复制到某处,但在某种程度上,您必须分配要写入的物理页。
如果你同意的话,你可以简单地编写一个 FUSE 驱动程序,将其安装在某个地方,然后映射一个文件从它。如果不是,那么您只需
write()
,因为 x86 根本不支持在不分配物理页的情况下重定向写入,在至少。This is not architecturally possible. You can create vm areas that have a writeback routine that copies data somewhere, but at some level, you must allocate physical pages to be written to.
If you're okay with that, you can simply write a FUSE driver, mount it somewhere, and mmap a file from it. If you're not, then you'll have to just
write()
, because redirecting writes without allocating a physical page at all is not supported by the x86, at the very least.有几种方法可以解决此问题,但大多数方法都要求您首先写入中间存储器。
网络文件系统 (NFS)
最简单的方法就是让服务器打开某种共享文件系统,例如 NFS 并使用
mmap()
进行映射远程文件到一个内存地址。然后,写入该地址将实际写入操作系统的页面缓存,当页面缓存已满或预定义的系统超时后,最终将写入远程文件。分布式共享内存 (DSM)
另一种方法是使用具有非常小的缓存大小的 DSM。
这意味着每个虚拟地址都在逻辑上映射到远程计算机上的虚拟地址,并且写入该地址将执行以下操作:(a) 从远程计算机接收页面并获得独占访问权限。 (b) 更新页面数据。 (c) 释放该页面,并在远程计算机再次读取该页面时将其发送回远程计算机。
在典型的 DSM 实现中,(c) 仅当远程计算机再次读取数据时才会发生,但您可以从现有的 DSM 实现开始并更改行为,以便在本地计算机页面缓存已满时发送数据。
输入/输出管理单元
这基本上意味着直接写入网络设备缓冲区,这实际上是为该设备实现替代驱动程序。
这种方法似乎是最复杂的,而且我认为这种方法没有任何好处。
这种方法实际上不使用任何中间内存,但绝对不推荐,除非系统对实时性要求很高。
There are a few approaches to this problem, but most of them require you to first write to an intermediate memory.
Network File System (NFS)
The easiest approach is simply to have the server open some sort of a shared file system such as NFS and using
mmap()
to map a remote file to a memory address. Then, writing to that address will actually write the OS's page cache, wich will eventually be written to the remote file when the page cache is full or after predefined system timeout.Distributed Shared Memory (DSM)
An alternative approach is using DSM with a very small cache size.
It means that each virtual address is logically mapped to a virtual address on a remote machine and writing to it will do the following: (a) receive the page from the remote machine and gain exclusive access. (b) update the page data. (c) release the page and send it back to the remote machine when it reads it again.
On typical DSM implementation, (c) will only happen when the remote machine will read the data again, but you might start from existing DSM implementation and change the behavior so that the data is sent once the local machine page cache is full.
I/O MMU
This basically means to write directly to the network device buffer, which is actually implementing an alternative driver for that device.
Such approach seems the most complicated and I don't see any benefit from that approach.
This approach is actually not using any intermediate memory but is definitely not recommended unless the system has a heavy realtime requirement.