]> Cypherpunks repositories - gostls13.git/commitdiff
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)
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

index b456f7f2ff5f77b7689f02abbc5881c06c48e025..bdfab13fed9c8255a504f0a6fb0715c3178a2635 100644 (file)
@@ -36,6 +36,8 @@ func sysAllocOS(n uintptr) unsafe.Pointer {
 
 var adviseUnused = uint32(_MADV_FREE)
 
+const madviseUnsupported = 0
+
 func sysUnusedOS(v unsafe.Pointer, n uintptr) {
        if uintptr(v)&(physPageSize-1) != 0 || n&(physPageSize-1) != 0 {
                // madvise will round this to any physical page
@@ -44,17 +46,31 @@ func sysUnusedOS(v unsafe.Pointer, n uintptr) {
                throw("unaligned sysUnused")
        }
 
-       var advise uint32
-       if debug.madvdontneed != 0 {
+       advise := atomic.Load(&adviseUnused)
+       if debug.madvdontneed != 0 && advise != madviseUnsupported {
                advise = _MADV_DONTNEED
-       } else {
-               advise = atomic.Load(&adviseUnused)
        }
-       if errno := madvise(v, n, int32(advise)); advise == _MADV_FREE && errno != 0 {
-               // MADV_FREE was added in Linux 4.5. Fall back to MADV_DONTNEED if it is
-               // not supported.
+       switch advise {
+       case _MADV_FREE:
+               if madvise(v, n, _MADV_FREE) == 0 {
+                       break
+               }
                atomic.Store(&adviseUnused, _MADV_DONTNEED)
-               madvise(v, n, _MADV_DONTNEED)
+               fallthrough
+       case _MADV_DONTNEED:
+               // MADV_FREE was added in Linux 4.5. Fall back on MADV_DONTNEED if it's
+               // not supported.
+               if madvise(v, n, _MADV_DONTNEED) == 0 {
+                       break
+               }
+               atomic.Store(&adviseUnused, madviseUnsupported)
+               fallthrough
+       case madviseUnsupported:
+               // Since Linux 3.18, support for madvise is optional.
+               // Fall back on mmap if it's not supported.
+               // _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE will unmap all the
+               // pages in the old mapping, and remap the memory region.
+               mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0)
        }
 
        if debug.harddecommit > 0 {