summary refs log tree commit diff
diff options
context:
space:
mode:
authorDanny Milosavljevic <dannym@scratchpost.org>2018-02-15 17:19:28 +0100
committerLeo Famulari <leo@famulari.name>2018-02-16 12:41:01 -0500
commitf6cccefed599e06a814b702aa79b8a09f01ec41c (patch)
treeb66a3487ad79bb59b716ff10088490d15fc8c843
parent4aa0a1df2dd89ba0e5f76d33b37383fdb530e0b8 (diff)
downloadguix-f6cccefed599e06a814b702aa79b8a09f01ec41c.tar.gz
gnu: ghc: Make memory allocator decommit memory on Linux < 4.5.
* gnu/packages/patches/ghc-8.0-fall-back-to-madv_dontneed.patch: New file.
* gnu/local.mk (dist_patch_DATA): Add it.
* gnu/packages/haskell.scm (ghc-8): Use it.
-rw-r--r--gnu/local.mk1
-rw-r--r--gnu/packages/haskell.scm3
-rw-r--r--gnu/packages/patches/ghc-8.0-fall-back-to-madv_dontneed.patch61
3 files changed, 64 insertions, 1 deletions
diff --git a/gnu/local.mk b/gnu/local.mk
index d923a52aae..702588ae6d 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -683,6 +683,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/gegl-CVE-2012-4433.patch			\
   %D%/packages/patches/gemma-intel-compat.patch			\
   %D%/packages/patches/geoclue-config.patch			\
+  %D%/packages/patches/ghc-8.0-fall-back-to-madv_dontneed.patch \
   %D%/packages/patches/ghc-dont-pass-linker-flags-via-response-files.patch	\
   %D%/packages/patches/ghostscript-no-header-id.patch		\
   %D%/packages/patches/ghostscript-no-header-uuid.patch		\
diff --git a/gnu/packages/haskell.scm b/gnu/packages/haskell.scm
index b5950051a5..e0ac1b8544 100644
--- a/gnu/packages/haskell.scm
+++ b/gnu/packages/haskell.scm
@@ -328,7 +328,8 @@ interactive environment for the functional language Haskell.")
        (base32 "1c8qc4fhkycynk4g1f9hvk53dj6a1vvqi6bklqznns6hw59m8qhi"))
       (patches
        (search-patches
-        "ghc-dont-pass-linker-flags-via-response-files.patch"))))
+        "ghc-dont-pass-linker-flags-via-response-files.patch"
+        "ghc-8.0-fall-back-to-madv_dontneed.patch"))))
     (build-system gnu-build-system)
     (supported-systems '("i686-linux" "x86_64-linux"))
     (outputs '("out" "doc"))
diff --git a/gnu/packages/patches/ghc-8.0-fall-back-to-madv_dontneed.patch b/gnu/packages/patches/ghc-8.0-fall-back-to-madv_dontneed.patch
new file mode 100644
index 0000000000..1838c5bd35
--- /dev/null
+++ b/gnu/packages/patches/ghc-8.0-fall-back-to-madv_dontneed.patch
@@ -0,0 +1,61 @@
+ghc runtime by default (otherwise depending on a "configure" option)
+does memory allocation on their own by first mmapping a 1 TB range of
+memory into the process and then parceling out chunks from it.
+
+If one of the chunks is not needed, the kernel needs to be informed -
+otherwise the system would quickly run out of available RAM.
+
+ghc does that via madvise(2).
+
+There are two options when doing this informing:
+
+MADV_FREE - Means "I don't need this range or the data in it any more". 
+Kernel promises to fail later accesses to it.
+
+MADV_DONTNEED - Means "I don't need this range right now - and I don't
+need the data in it anymore". Kernel promises to make later accesses to
+it succeed (if necessary by providing a new page initialized with zeroes).
+
+MADV_FREE was introduced in Linux 4.5.
+glibc 2.25 and later always define MADV_FREE.
+
+Unpatched ghc 8.0.2 will use either MADV_FREE or MADV_DONTNEED, determined
+at ghc compile time.  Which of them will actually succeed is determined
+by the Linux kernel at run time.
+
+This patch makes ghc try MADV_FREE.  If it doesn't work, it falls back to
+MADV_DONTNEED.
+
+The end result is that ghc programs free their memory with Linux < 4.5 again.
+
+See https://git.haskell.org/ghc.git/commitdiff/6576bf83cdf4eac05eb88a24aa934a736c91e3da for more information.
+--- a/rts/posix/OSMem.c
++++ b/rts/posix/OSMem.c
+@@ -541,11 +541,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");
+ }