简体   繁体   English

mmap是否分配页面或页面的一部分?

[英]Does mmap allocate a page or part of a page?

I'm confused, does mmap allocate an entire page of memory (regardless of size specified), or does it just allocate the size you request? 我很困惑, mmap分配整个内存页面(无论指定的大小),还是只分配您请求的大小? Really, I'm curious about what happens on subsequent calls to mmap -- would a second call allocate a new page (even if both calls use an amount under the page size) or would it allocate a block adjacent to the previous call? 真的,我很好奇后续调用mmap会发生什么 - 第二次调用会分配一个新页面(即使两个调用都使用页面大小下的数量),还是会在前一个调用旁边分配一个块?

Same thing for mprotect - does that protect the entire page, or just the part specified? 同样适用于mprotect - 是保护整个页面还是仅保护指定的部分?

Yes. 是。

But that is not because of mmap per se , it is because the kernel can't really do anything different. 但这不是因为mmap 本身 ,而是因为内核不能真正做出任何不同的事情。 Memory is organized in pages, and the MMU "thinks" in terms of pages, so there is no way (no sane, reasonable way anyway) to allocate half a page and give the other half to someone else. 内存以页面形式组织,MMU以页面的形式“思考”,因此无法分配半页并将另一半分配给其他人,这是无法理解的。
How would one eg prevent process 2 from stealing confidential data from process 1 if they each have allocated half a page? 如果一个人分配半页,那么如何阻止进程2从进程1中窃取机密数据? The memory protection system doesn't work that way, it would be impossible to prevent that from happening. 内存保护系统不能以这种方式工作,不可能防止这种情况发生。

mmap mandates that length be non-zero, or it will fail. mmap要求长度为非零,否则将失败。 Other than that, it has no requirements on the input parameters (apart from contradicting flags), but of course an implementation is always allowed to have the call fail for other reasons, at its discretion ("implementation" here means for example "Linux"). 除此之外,它对输入参数没有要求(除了矛盾的标志),但当然总是允许实现由于其他原因而使呼叫失败,由其自行决定(这里的“实现”意味着例如“Linux”) )。

The effective address of the mapping (which will be returned by a successful call to mmap ) is an implementation-defined function of the address hint. 映射的有效地址(将通过成功调用mmap返回)是地址提示的实现定义函数。 Practically, this means rounding the hint down to the previous page (usually 4096 bytes) boundary and rounding the length up to the next page boundary. 实际上,这意味着舍入的提示到前一页(通常4096字节)边界和舍入长度下一个页边界。
Different versions of Linux behave differently on some address ranges, for example prior to version 2.6, hints below mmap_min_addr would fail with EINVAL whereas it now rounds the address up so it is valid. 不同版本的Linux在某些地址范围内表现不同,例如在版本2.6之前, mmap_min_addr下面的mmap_min_addr会因EINVAL而失败,而现在它会将地址向上mmap_min_addr以使其有效。

Source: POSIX 资料来源: POSIX

If the length argument is not a page size multiple it will be rounded up to page size multiple. 如果length参数不是页面大小倍数,则它将向上舍入到页面大小倍数。

As a consequence, the answer to your question is yes mmap() virtually allocates only entire pages. 因此,问题的答案是肯定的, mmap()实际上只分配了整个页面。

Regarding mprotect() the man page clearly answer to your question: 关于mprotect() ,手册页清楚地回答了你的问题:

mprotect() changes protection for the calling process's memory page(s) containing any part of the address range in the interval [addr, addr+len-1]. mprotect()更改调用进程的内存页面的保护,该内存页面包含区间[addr,addr + len-1]中地址范围的任何部分。 addr must be aligned to a page boundary. addr必须与页面边界对齐。

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

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