简体   繁体   English

mmap 分配虚拟内存失败

[英]mmap failed to allocate virtual memory

I got the following output in ftrace:我在 ftrace 中得到以下输出:

mmap(0x200000000000, 17179869184, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = -1 ENOMEM (Cannot allocate memory)

My code:我的代码:

void alloc_page_full_reverse()
{
printf("Allocating default pagesize pages > 128TB \n");
mmap_chunks_higher(24575, 0);
printf("Allocating default pagesize pages < 128TB \n");
/* Note: Allocating a 16GB chunk less due to heap space required 
for other mappings */
mmap_chunks_lower(8190, 0);
  }

int mmap_chunks_higher(unsigned long no_of_chunks, unsigned long hugetlb_arg)
 {
unsigned long i;
char *hptr;
char *hint;
int mmap_args = 0;
for (i = 0; i < no_of_chunks; i++){
    hint = hind_addr();
    hptr = mmap(hint, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE,
        MAP_PRIVATE | MAP_ANONYMOUS | hugetlb_arg, -1, 0); // MAP_CHUNK_SIZE = 16GB

    if (hptr == MAP_FAILED){
        printf("\n Map failed at address %p < 384TB in iteration = %d \n", hptr, i);
        exit(-1);   
    }

    if (validate_addr(hptr, 1)){
        printf("\n Address failed, not in > 128Tb iterator = %d\n", i);
        exit(-1);
    }
}
printf("> 128Tb: \n chunks allocated= %d \n", i);
}

static char *hind_addr(void)
{
int bits = 48 + rand() % 15;
return (char *) (1UL << bits);
}

Need to understand before mmap how to validate **void mmap(void addr, size_t length, int prot, int flags, int fd, off_t offset);在 mmap 之前需要了解如何验证 **void mmap(void addr, size_t length, int prot, int flags, int fd, off_t offset); all its argument are validated, EX: size_t length is validated.它的所有参数都经过验证,例如: size_t 长度经过验证。

I still want to make sure I have enough memory before doing a mmap我仍然想确保在做 mmap 之前我有足够的内存

There isn't an interface that allows a process to check this, and for good reason.没有一个接口允许进程检查这一点,这是有充分理由的。 Suppose such a syscall existed, and the kernel told a process it could allocate 1 GB of memory.假设存在这样一个系统调用,并且内核告诉一个进程它可以分配 1 GB 的内存。 However, it is possible the kernel is not able to allocate that memory by the time process actually requests the allocation.但是,内核可能无法在进程实际请求分配时分配该内存。 So, this information would not be useful.因此,这些信息将没有用处。

Instead, you should attempt to allocate memory, and handle ENOMEM.相反,您应该尝试分配内存并处理 ENOMEM。

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

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