runtime: fall back on mmap if madvise is unsupported
Since Linux 3.18, support for madvise is optional, depending on
the setting of the CONFIG_ADVISE_SYSCALLS configuration option.
The Go runtime currently assumes in several places that we
do not unmap heap memory; that needs to remain true. So, if
madvise is unsupported, we cannot fall back on munmap. AFAIK,
the only way to free the pages is to remap the memory region.
For the x86, the system call mmap() is implemented by sys_mmap2()
which calls do_mmap2() directly with the same parameters. The main
call trace for
mmap(v, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1, 0)
is as follows:
```
do_mmap2()
\- do_mmap_pgoff()
\- get_unmapped_area()
\- find_vma_prepare()
// If a VMA was found and it is part of the new mmaping, remove
// the old mapping as the new one will cover both.
// Unmap all the pages in the region to be unmapped.
\- do_munmap()
// Allocate a VMA from the slab allocator.
\- kmem_cache_alloc()
// Link in the new vm_area_struct.
\- vma_link()
```
So, it's safe to fall back on mmap().
See D.2 https://www.kernel.org/doc/gorman/html/understand/understand021.html
Change-Id: Ia2b4234bc0bf8a4631a9926364598854618fe270
GitHub-Last-Rev:
179f04715442b44cd4b7bf3e6cae3dd9092128e7
GitHub-Pull-Request: golang/go#60218
Reviewed-on: https://go-review.googlesource.com/c/go/+/495081
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>