如何通过替换为空白页面来取消映射mmap文件 [英] How to unmap an mmap'd file by replacing with a mapping to empty pages

查看:146
本文介绍了如何通过替换为空白页面来取消映射mmap文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux用户空间中是否有一种方法可以用空白页(从/dev/null映射,或者可能是一个空白页,重复映射)替换映射文件的页(或某个逻辑地址范围内的mmap页).在从文件映射的页面顶部上方)?

Is there a way from Linux userspace to replace the pages of a mapped file (or mmap'd pages within a certain logical address range) with empty pages (mapped from /dev/null, or maybe a single empty page, mapped repeatedly over the top of the pages mapped from the file)?

对于上下文,我想查找此JDK错误的修补程序:

For context, I want to find a fix for this JDK bug:

https://bugs.openjdk.java.net/browse/JDK-4724038

总结一下这个错误:在JVM可以垃圾回收包装mmap文件的MappedByteBuffer之前,当前无法在Java中取消映射文件,因为强行取消映射文件可能会由于种族而引起安全问题.条件(例如,本机代码可能仍试图访问文件映射到的相同地址范围,并且操作系统可能已经将新文件映射到该相同的逻辑地址范围).

To summarize the bug: it is not currently possible to unmap files in Java until the JVM can garbage collect the MappedByteBuffer that wraps an mmap'd file, because forcibly unmapping the file could give rise to security issues due to race conditions (e.g. native code could still be trying to access the same address range that the file was mapped to, and the OS may have already mapped a new file into that same logical address range).

我希望替换逻辑地址范围内的映射页面,然后取消映射文件.有什么办法可以做到这一点?

I'm looking to replace the mapped pages in the logical address range, and then unmap the file. Is there any way to accomplish this?

(如果您也知道在其他操作系统(尤其是Windows和Mac OS X)中执行此操作的方法,则将获得奖励.)

(Bonus points if you know a way of doing this in other operating systems too, particularly Windows and Mac OS X.)

请注意,这不一定是原子操作.主要目标是从关闭文件中分离出内存的取消映射(或将读取的页面内容替换为映射的文件内容)与关闭文件,因为这将解决两个Linux上的一连串问题. Windows和Windows(每个文件在映射时无法删除的事实)的最低限制.

Note that this doesn't have to be an atomic operation. The main goal is to separate the unmapping of the memory (or the replacing of the mapped file contents with zero-on-read pages) from the closing of the file, since that will solve a litany of issues on both Linux (which has a low limit on the number of file descriptors per process) and Windows (the fact you can't delete a file while it is mapped).

更新:另请参见:推荐答案

在Linux上,可以将mmapMAP_FIXED结合使用,以将映射替换为所需的任何映射.如果替换整个映射,则对该文件的引用将被删除.

On Linux you can use mmap with MAP_FIXED to replace the mapping with any mapping you want. If you replace the entire mapping the reference to the file will be removed.

这篇关于如何通过替换为空白页面来取消映射mmap文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆