没有物理内存分配的虚拟内存分配

发布于 2024-11-18 11:14:32 字数 335 浏览 4 评论 0原文

我正在开发一个 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 技术交流群。

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

发布评论

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

评论(2

疧_╮線 2024-11-25 11:14:32

这在架构上是不可能的。您可以创建具有回写例程的 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.

深陷 2024-11-25 11:14:32

有几种方法可以解决此问题,但大多数方法都要求您首先写入中间存储器。

网络文件系统 (NFS)

最简单的方法就是让服务器打开某种共享文件系统,例如 NFS 并使用 mmap() 进行映射远程文件到一个内存地址。然后,写入该地址将实际写入操作系统的页面缓存,当页面缓存已满或预定义的系统超时后,最终将写入远程文件。

分布式共享内存 (DSM)

另一种方法是使用具有非常小的缓存大小的 DSM

在计算机科学中,分布式共享内存 (DSM) 是一种内存架构形式,其中物理上分离的内存可以作为一个逻辑上共享的地址空间进行寻址。

[...] 软件 DSM 系统可以在操作系统中实现,或者作为编程库实现,并且可以被视为底层虚拟内存架构的扩展。当在操作系统中实现时,此类系统对开发人员是透明的;这意味着底层分布式内存对用户完全隐藏。

这意味着每个虚拟地址都在逻辑上映射到远程计算机上的虚拟地址,并且写入该地址将执行以下操作:(a) 从远程计算机接收页面并获得独占访问权限。 (b) 更新页面数据。 (c) 释放该页面,并在远程计算机再次读取该页面时将其发送回远程计算机。

在典型的 DSM 实现中,(c) 仅当远程计算机再次读取数据时才会发生,但您可以从现有的 DSM 实现开始并更改行为,以便在本地计算机页面缓存已满时发送数据。

输入/输出管理单元

[...]IOMMU 将设备可见的虚拟地址(在本上下文中也称为设备地址或 I/O 地址)映射到物理地址。

这基本上意味着直接写入网络设备缓冲区,这实际上是为该设备实现替代驱动程序。
这种方法似乎是最复杂的,而且我认为这种方法没有任何好处。

这种方法实际上不使用任何中间内存,但绝对不推荐,除非系统对实时性要求很高。

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.

In computer science, distributed shared memory (DSM) is a form of memory architecture where physically separated memories can be addressed as one logically shared address space.

[...] Software DSM systems can be implemented in an operating system, or as a programming library and can be thought of as extensions of the underlying virtual memory architecture. When implemented in the operating system, such systems are transparent to the developer; which means that the underlying distributed memory is completely hidden from the users.

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

[...] the IOMMU maps device-visible virtual addresses (also called device addresses or I/O addresses in this context) to physical addresses.

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.

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