Skip to contentSkip to conversation

mmap.2: ENOMEM possible when exceeding VA space

PATCH In review Topi Miettinen 3 participants 4 messages Nov 11, 2021 – Nov 22, 2021 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mmap.2: ENOMEM possible when exceeding VA space

A bit surprisingly, mmap(2) returns ENOMEM when the virtual address space of the CPU is exceeded.

The expectation could be EINVAL instead ("We don't like _addr_, length, or offset (e.g., they are _too large_, or not aligned on a page boundary)").

This is demonstrated with the following program:

#include <stdio.h>
#include <sys/mman.h>

int main(void) {

 	for (int i = 12; i < 64; i++) {
		void *addr = mmap((void *)(1UL << i), 4096, PROT_NONE,
				  MAP_ANON | MAP_FIXED_NOREPLACE | MAP_PRIVATE,
				  -1, 0);
		if (addr == MAP_FAILED)
			fprintf(stderr, "mmap %lx: %m\n", (1UL << i));
		continue;
		munmap(addr, 4096);
	}

}

It gives the following output when running on CPU with 48 bit VA space:

mmap 800000000000: Cannot allocate memory
mmap 1000000000000: Cannot allocate memory
mmap 2000000000000: Cannot allocate memory
mmap 4000000000000: Cannot allocate memory
mmap 8000000000000: Cannot allocate memory
mmap 10000000000000: Cannot allocate memory
mmap 20000000000000: Cannot allocate memory
mmap 40000000000000: Cannot allocate memory
mmap 80000000000000: Cannot allocate memory
mmap 100000000000000: Cannot allocate memory
mmap 200000000000000: Cannot allocate memory
mmap 400000000000000: Cannot allocate memory
mmap 800000000000000: Cannot allocate memory
mmap 1000000000000000: Cannot allocate memory
mmap 2000000000000000: Cannot allocate memory
mmap 4000000000000000: Cannot allocate memory
mmap 8000000000000000: Cannot allocate memory

---
 man2/mmap.2 | 5 +++++
 1 file changed, 5 insertions(+)
man2/mmap.2 +5 -0
index 96b7444b0..59fd5c904 100644
@@ -603,6 +603,11 @@ limit, described in
 .BR getrlimit (2),
 would have been exceeded.
 .TP
+.B ENOMEM
+We don't like
+.IR addr ,
+because it exceeds the virtual address space of the CPU.
+.TP
 .B EOVERFLOW
 On 32-bit architecture together with the large file extension
 (i.e., using 64-bit
Signature · 1 line
2.33.0
#1 lore raw

* Topi Miettinen <[email protected]>, 2021-11-11, 20:04:

man2/mmap.2 limit, described in · line 610

Quoted 5 lines

+.B ENOMEM

man2/mmap.2 +4 -0
+We don't like
+.IR addr ,
+because it exceeds the virtual address space of the CPU.
+.TP

Who is "we"?

FWIW, failing with ENOMEM in this case may seem weird, but this is what POSIX prescribes.

Signature · 1 line
Jakub Wilk
#2 lore raw

Hi Jakub,

Quoted 8 lines · Jakub Wilk

* Topi Miettinen <[email protected]>, 2021-11-11, 20:04:

man2/mmap.2 limit, described in · line 610

Quoted 5 lines

+.B ENOMEM

man2/mmap.2 +4 -0
+We don't like
+.IR addr ,
+because it exceeds the virtual address space of the CPU.
+.TP

Who is "we"?

I was going to ask the same, but that page already describes other errors like that. If only for consistency, and avoiding a complete rewrite of that ERRORS section, I applied the patch as is.

But yes, "we" doesn't sound like very technical. Maybe we should fix this.

Quoted 3 lines

FWIW, failing with ENOMEM in this case may seem weird, but this is what POSIX prescribes.

Thanks,
Alex

Signature · 3 lines
Alejandro Colomar
Linux man-pages comaintainer; http://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/
#3 lore raw
In reply to Topi Miettinen

Hi Topi,

Quoted 45 lines · Topi Miettinen

A bit surprisingly, mmap(2) returns ENOMEM when the virtual address space of the CPU is exceeded.

The expectation could be EINVAL instead ("We don't like _addr_, length, or offset (e.g., they are _too large_, or not aligned on a page boundary)").

This is demonstrated with the following program:

  #include <stdio.h>
  #include <sys/mman.h>

int main(void) {

  	for (int i = 12; i < 64; i++) {
		void *addr = mmap((void *)(1UL << i), 4096, PROT_NONE,
				  MAP_ANON | MAP_FIXED_NOREPLACE | MAP_PRIVATE,
				  -1, 0);
		if (addr == MAP_FAILED)
			fprintf(stderr, "mmap %lx: %m\n", (1UL << i));
		continue;
		munmap(addr, 4096);
	}

}

It gives the following output when running on CPU with 48 bit VA space:

mmap 800000000000: Cannot allocate memory
mmap 1000000000000: Cannot allocate memory
mmap 2000000000000: Cannot allocate memory
mmap 4000000000000: Cannot allocate memory
mmap 8000000000000: Cannot allocate memory
mmap 10000000000000: Cannot allocate memory
mmap 20000000000000: Cannot allocate memory
mmap 40000000000000: Cannot allocate memory
mmap 80000000000000: Cannot allocate memory
mmap 100000000000000: Cannot allocate memory
mmap 200000000000000: Cannot allocate memory
mmap 400000000000000: Cannot allocate memory
mmap 800000000000000: Cannot allocate memory
mmap 1000000000000000: Cannot allocate memory
mmap 2000000000000000: Cannot allocate memory
mmap 4000000000000000: Cannot allocate memory
mmap 8000000000000000: Cannot allocate memory

Quoted 1 line

Patch applied!

Thanks,
Alex

Quoted 21 lines
---
  man2/mmap.2 | 5 +++++
  1 file changed, 5 insertions(+)
man2/mmap.2 +5 -0
index 96b7444b0..59fd5c904 100644
@@ -603,6 +603,11 @@ limit, described in
  .BR getrlimit (2),
  would have been exceeded.
  .TP
+.B ENOMEM
+We don't like
+.IR addr ,
+because it exceeds the virtual address space of the CPU.
+.TP
  .B EOVERFLOW
  On 32-bit architecture together with the large file extension
  (i.e., using 64-bit
Signature · 3 lines
Alejandro Colomar
Linux man-pages comaintainer; http://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/