]> Cypherpunks repositories - gostls13.git/commit
runtime: fall back on mmap if madvise is unsupported
authorLance Yang <ioworker0@gmail.com>
Tue, 16 May 2023 08:20:42 +0000 (08:20 +0000)
committerMichael Knyszek <mknyszek@google.com>
Tue, 23 May 2023 15:45:55 +0000 (15:45 +0000)
commit5ffdc1f15c66f4f31124a43c4bd9de94b9131e15
tree8bac52bdc6eb2cd4e240e631d25d5d7970665b34
parent4bb38fe458cab82711b9a3c96c57a9ea106b044a
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>
src/runtime/mem_linux.go