加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux-kernel – 为什么COW mmap在大于4GB的(稀疏)文件上出现ENO

发布时间:2020-12-14 00:02:59 所属栏目:Linux 来源:网络整理
导读:当尝试使用写时复制语义(PROT_READ | PROT_WRITE和MAP_PRIVATE)映射5GB文件时,会在2.6.26-2-amd64 Linux内核上发生这种情况.映射小于4GB的文件或仅使用PROT_READ工作正常.这不是 this question中报告的软资源限制问题;虚拟限制大小是无限的. 这是重现问题的
当尝试使用写时复制语义(PROT_READ | PROT_WRITE和MAP_PRIVATE)映射5GB文件时,会在2.6.26-2-amd64 Linux内核上发生这种情况.映射小于4GB的文件或仅使用PROT_READ工作正常.这不是 this question中报告的软资源限制问题;虚拟限制大小是无限的.

这是重现问题的代码(实际代码是Boost.Interprocess的一部分).

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

#include <fcntl.h>
#include <unistd.h>

main()
{
        struct stat b;
        void *base;
        int fd = open("foo.bin",O_RDWR);

        fstat(fd,&b);
        base = mmap(0,b.st_size,PROT_READ | PROT_WRITE,MAP_PRIVATE,fd,0);
        if (base == MAP_FAILED) {
                perror("mmap");
                return 1;
        }
        return 0;
}

以下是发生的事情:

dd if=/dev/zero of=foo.bin bs=1M seek=5000 count=1
./test-mmap
mmap: Cannot allocate memory

这是相关的strace(新编译的4.5.20)输出,如nos所述.

open("foo.bin",O_RDWR)                 = 3
fstat(3,{st_mode=S_IFREG|0644,st_size=5243928576,...}) = 0
mmap(NULL,5243928576,PROT_READ|PROT_WRITE,3,0) = -1 ENOMEM (Cannot allocate memory)
dup(2)                                  = 4
[...]
write(4,"mmap: Cannot allocate memoryn",29mmap: Cannot allocate memory
) = 29

解决方法

尝试在flags字段中传递MAP_NORESERVE,如下所示:

mmap(NULL,MAP_PRIVATE|MAP_NORESERVE,0);

您的交换和物理内存的组合可能小于请求的5GB.

或者,您可以执行此操作以进行测试,如果可行,您可以在上面更改代码:

# echo 0 > /proc/sys/vm/overcommit_memory

以下是手册页的相关摘录.

MMAP(2):

MAP_NORESERVE
          Do  not reserve swap space for this mapping.  When swap space is
          reserved,one has the guarantee that it is  possible  to  modify
          the  mapping.   When  swap  space  is not reserved one might get
          SIGSEGV upon a write if no physical memory  is  available.   See
          also  the  discussion of the file /proc/sys/vm/overcommit_memory
          in proc(5).  In kernels before 2.6,this flag  only  had  effect
          for private writable mappings.

PROC(5):

/proc/sys/vm/overcommit_memory
          This file contains the kernel virtual  memory  accounting  mode.
          Values are:

                 0: heuristic overcommit (this is the default)
                 1: always overcommit,never check
                 2: always check,never overcommit

          In  mode 0,calls of mmap(2) with MAP_NORESERVE are not checked,and the default check is very weak,leading to the risk of  get                        

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读