Package: ghc / 8.0.1-17

osdecommitmemory-compat.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Description: rts: Ensure we always give MADV_DONTNEED a chance in osDecommitMemory
Author: Ben Gamari <bgamari.foss@gmail.com>
Origin: upstream, https://phabricator.haskell.org/D2780
Bug: https://ghc.haskell.org/trac/ghc/ticket/12865
Bug-Debian: https://bugs.debian.org/847677
---
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
--- a/rts/posix/OSMem.c
+++ b/rts/posix/OSMem.c
@@ -506,11 +506,24 @@ void osDecommitMemory(void *at, W_ size)
 
 #ifdef MADV_FREE
     // Try MADV_FREE first, FreeBSD has both and MADV_DONTNEED
-    // just swaps memory out
+    // just swaps memory out. Linux >= 4.5 has both DONTNEED and FREE; either
+    // will work as they both allow the system to free anonymous pages.
+    // It is important that we try both methods as the kernel which we were
+    // built on may differ from the kernel we are now running on.
     r = madvise(at, size, MADV_FREE);
-#else
-    r = madvise(at, size, MADV_DONTNEED);
+    if(r < 0) {
+        if (errno == EINVAL) {
+            // Perhaps the system doesn't support MADV_FREE; fall-through and
+            // try MADV_DONTNEED.
+        } else {
+            sysErrorBelch("unable to decommit memory");
+        }
+    } else {
+        return;
+    }
 #endif
+
+    r = madvise(at, size, MADV_DONTNEED);
     if(r < 0)
         sysErrorBelch("unable to decommit memory");
 }