# Facing "mmap 4096 bytes at (nil): Cannot allocate memory"? You're not alone!

**URL:** https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259
**Category:** Uncategorized
**Created:** [May 9, 2023, 5:20pm UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259 "2023-05-09T17:20:29Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Kleidukos](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/kleidukos/32/1213_2.png) [@Kleidukos](https://discourse.haskell.org/u/Kleidukos)
#### Post date: [May 9, 2023, 5:20pm UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/1 "2023-05-09T17:20:29Z")

</div>

You! yes, you!

Are you facing a segfault with the following error?

```haskell
GHCi, version 9.2.7: https://www.haskell.org/ghc/ :? for help
ghci> 40000 / 9
4444.444444444444
ghc: mmap 4096 bytes at (nil): Cannot allocate memory
ghc: Try specifying an address with +RTS -xm<addr> -RTS
fish: Job 1, 'ghci' terminated by signal SIGSEGV (Address boundary error)

```

You will be pleased to know that migrating to GHC 9.4 solves this problem, as explained in [#19421: internal error: m32\_allocator\_init: Failed to map · Issues · Glasgow Haskell Compiler / GHC · GitLab](https://gitlab.haskell.org/ghc/ghc/-/issues/19421).

---

<div class="post-metadata">

### Author: ![gilmi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/gilmi/32/45_2.png) [@gilmi](https://discourse.haskell.org/u/gilmi)
#### Post date: [May 9, 2023, 8:34pm UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/2 "2023-05-09T20:34:09Z")

</div>

and if you can’t upgrade your ghc, upgrade your linux kernel to 6.2.12 or something (technically [6.2.9](https://lwn.net/Articles/927858/) or later with [this patch](https://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git/commit/queue-6.2/maple_tree-fix-mas_skip_node-end-slot-detection.patch?id=79583022efa64b59298c2cf4396bec1deb8d7db0))

---

<div class="post-metadata">

### Author: ![fintara](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/fintara/32/3151_2.png) [@fintara](https://discourse.haskell.org/u/fintara)
#### Post date: [May 10, 2023, 5:54am UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/3 "2023-05-10T05:54:33Z")

</div>

I’m afraid the problem is present even with 6.3.1 linux kernel. So it seems GHC 9.4 is the only way.

Why wouldn’t the fix be backported to 9.2 though?

Funnily, this issue did get worse with newer kernels.

Btw, can someone confirm that adding `+RTS -xp -RTS` (or `export GHCRTS='-xp'`) actually fixes the problem (or makes it much more unlikely)?

---

<div class="post-metadata">

### Author: ![gilmi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/gilmi/32/45_2.png) [@gilmi](https://discourse.haskell.org/u/gilmi)
#### Post date: [May 10, 2023, 8:04am UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/4 "2023-05-10T08:04:34Z")

</div>

It’s present in 6.3.1 for some reason, but not in 6.2.12 (or maybe others in the 6.2.x line, I haven’t tested them all). I think it was even fixed in 6.3.0-rc7 but for some reason not in 6.3.1.

---

<div class="post-metadata">

### Author: ![wiz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/wiz/32/2408_2.png) [@wiz](https://discourse.haskell.org/u/wiz)
#### Post date: [May 10, 2023, 9:46am UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/5 "2023-05-10T09:46:56Z")

</div>

The kernel people promised to backport the fix for all the affected versions.  
But I’m not seeing that in my Ubuntu mainline archive yet…

---

<div class="post-metadata">

### Author: ![janus](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/janus/32/1060_2.png) [@janus](https://discourse.haskell.org/u/janus)
#### Post date: [May 10, 2023, 1:42pm UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/6 "2023-05-10T13:42:28Z")

</div>

Are people hitting this on 6.3 also able to reproduce with the C programs from int-e (@int-index) on [Memory allocation errors on 6.1.1 / Kernel & Hardware / Arch Linux Forums](https://bbs.archlinux.org/viewtopic.php?id=282429) ? I am curious whether it is a separate bug. But it would be odd if it wasn’t also related to [the new maple trees](https://lwn.net/Articles/845507/).

---

<div class="post-metadata">

### Author: ![tcard](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/tcard/32/4823_2.png) [@tcard](https://discourse.haskell.org/u/tcard)
#### Post date: [May 10, 2023, 8:48pm UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/7 "2023-05-10T20:48:14Z")

</div>

Yes, the test program stops after relatively few requests using kernel version 6.3.1 on my machine. The shortest of multiple runs was only 23 requests, shown below!

* * *

[`mmap-test.c`](https://bbs.archlinux.org/viewtopic.php?pid=2090686#p2090686):

```haskell
#include <sys/mman.h>
#include <stddef.h>
#include <stdio.h>

int main()
{
    for (int i = 0; i < (1 << 16); i++) {
        void *p = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_32BIT, -1, 0);
        printf("%p\n", p);
        if (p == (void *)-1) {
            printf("%d\n", i);
            return 0;
        }
    }
}

```

Test:

```nohighlight
$ uname -srvmo
Linux 6.3.1-arch1-1 #1 SMP PREEMPT_DYNAMIC Mon, 01 May 2023 17:42:39 +0000 x86_64 GNU/Linux
$ gcc -o mmap-test mmap-test.c
$ ./mmap-test
0x40898000
...
0x417e2000
0xffffffffffffffff
23

```

---

<div class="post-metadata">

### Author: ![fommil](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/fommil/32/34_2.png) [@fommil](https://discourse.haskell.org/u/fommil)
#### Post date: [May 11, 2023, 8:22am UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/8 "2023-05-11T08:22:31Z")

</div>

Unfortunately, upgrading to ghc 9.4 isn’t an option for many, including those of us who need to check that library code is compiling on older versions of ghc.

I’m experiencing this with both the current and LTS versions of the linux kernel on ArchLinux (6.3.1-arch2-1 and 6.1.27-1). Do we know if the upstream kernel is going to be fixing this promptly or should we be looking into compiling our own kernels and running them (or downgrading)? 6.2.9 works for me.

---

<div class="post-metadata">

### Author: ![fommil](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/fommil/32/34_2.png) [@fommil](https://discourse.haskell.org/u/fommil)
#### Post date: [May 11, 2023, 8:25am UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/9 "2023-05-11T08:25:39Z")

</div>

using  
`export GHCRTS='-xp'`  
gives me a

```haskell
R_X86_64_32S relocation out of range: (noname) = 7fdf70318050

```

on ghc-8.10.7 compiled apps, vs  
` mmap 4964352 bytes at (nil): Cannot allocate memory` when I don’t use any flags.

---

<div class="post-metadata">

### Author: ![janus](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/janus/32/1060_2.png) [@janus](https://discourse.haskell.org/u/janus)
#### Post date: [May 12, 2023, 9:13pm UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/10 "2023-05-12T21:13:43Z")

</div>

> [@fommil](#):
>
> Do we know if the upstream kernel is going to be fixing this promptly

Liam Howlett just mailed in 35 patches that seem related: ['[PATCH v3 00/35] Maple tree mas\_{next,prev}\_range() and cleanup' - MARC](https://marc.info/?l=linux-mm&m=168391557824417&w=2)

---

<div class="post-metadata">

### Author: ![janus](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/janus/32/1060_2.png) [@janus](https://discourse.haskell.org/u/janus)
#### Post date: [May 14, 2023, 3:42pm UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/11 "2023-05-14T15:42:37Z")

</div>

> [@fommil](#):
>
> I’m experiencing this with […] 6.1.27-1

[Robert Hensing reports](https://lore.kernel.org/lkml/190c5b1b-df67-eb54-1559-706ed4e5719a@hercules-ci.com/) (possibly using bisect?) that an issue was reintroduced into the 6.1 series in commit [58c5d0d](https://github.com/torvalds/linux/commit/58c5d0d6d522112577c7eeb71d382ea642ed7be4), titled `mm/mmap: regression fix for unmapped_area{_topdown}`. Googling this commit, I found it in the [6.1.26 changelog](https://cdn.kernel.org/pub/linux/kernel/v6.x/ChangeLog-6.1.26) and the [6.3 changelog](https://cdn.kernel.org/pub/linux/kernel/v6.x/ChangeLog-6.3).

So it’s likely that 6.1.25 is ok.

> [@gilmi](#):
>
> I think it was even fixed in 6.3.0-rc7 but for some reason not in 6.3.1.

The title of commit 58c5d0d was [not listed in the rc7 changelog](https://lwn.net/Articles/929226/) (compare [6.3 announcement](https://lwn.net/Articles/929943/), where it is present). So this is consistent with your observation.

---

<div class="post-metadata">

### Author: ![juhp](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/juhp/32/323_2.png) [@juhp](https://discourse.haskell.org/u/juhp)
#### Post date: [May 18, 2023, 6:10am UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/12 "2023-05-18T06:10:27Z")

</div>

Unfortunately the ghci 9.2 crash is happening sometimes in current Fedora releases now (~10% of the time in kernel-6.2.15-300; 6.2.9-300 is okay) and more often with kernel-6.3.3-200 and 6.4.0 rc’s (rawhide).  
So I am starting to wonder if this behavior will really change (back) in the Linux kernel or not?  
(Though I am hoping to ship ghc-9.4 in Fedora 39…)

The crash happens for me only sometimes and before ghci even shows the initial prompt  
(the core files are about 4MB apparently).

Also worth mentioning Sylvain’s [draft backport MR](https://gitlab.haskell.org/ghc/ghc/-/merge_requests/10453) too (not a small patch though by any definition). If you are using Fedora 38 you can update to this [temporary scratch build](https://koji.fedoraproject.org/koji/taskinfo?taskID=101285362) which includes the patches, eg using `koji-tool install 101285362`.

---

<div class="post-metadata">

### Author: ![fommil](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/fommil/32/34_2.png) [@fommil](https://discourse.haskell.org/u/fommil)
#### Post date: [May 18, 2023, 9:54am UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/13 "2023-05-18T09:54:29Z")

</div>

I don’t know if all those patches are included, but I can confirm that 6.3.2 does NOT fix the problem. I’m holding onto 6.2.9 for now.

---

<div class="post-metadata">

### Author: ![juhp](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/juhp/32/323_2.png) [@juhp](https://discourse.haskell.org/u/juhp)
#### Post date: [May 18, 2023, 3:49pm UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/14 "2023-05-18T15:49:53Z")

</div>

Is there a minimal reproducer in Haskell?

---

<div class="post-metadata">

### Author: ![tcard](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/tcard/32/4823_2.png) [@tcard](https://discourse.haskell.org/u/tcard)
#### Post date: [May 18, 2023, 11:50pm UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/15 "2023-05-18T23:50:14Z")

</div>

I am working on a [Reflex FRP](https://reflex-frp.org/) project, where upgrading to GHC 9.4 is not an option. Using `export GHCRTS='-xp'` causes [Obelisk](https://github.com/obsidiansystems/obelisk#readme) to fail, reporting that it does not support that RTS option, but adding `+RTS -xp -RTS` to [`baseGhciOptions`](https://github.com/obsidiansystems/obelisk/blob/6125e83cb7eee8b5fbd5a4694db85fee9f286c6f/lib/command/src/Obelisk/Command/Run.hs#L549-L555) as follows makes the `ob run` and `ob repl` commands work for me. Thank you @fintara for the hint about using that RTS option!

```diff
diff --git a/lib/command/src/Obelisk/Command/Run.hs b/lib/command/src/Obelisk/Command/Run.hs
index 0e2b6b3d..6fce8551 100644
--- a/lib/command/src/Obelisk/Command/Run.hs
+++ b/lib/command/src/Obelisk/Command/Run.hs
@@ -552,6 +552,7 @@ baseGhciOptions =
   , "-no-user-package-db"
   , "-hide-all-packages"
   , "-package-env", "-"
+ , "+RTS", "-xp", "-RTS"
   ]

 -- | Run ghci repl

```

For Nix builds that have caused problems, I am simply doing those in a VM running an older kernel.

```nohighlight
$ uname -a
Linux ghc810 5.15.110 #1-NixOS SMP Sun Apr 30 23:23:24 UTC 2023 x86_64 GNU/Linux

```

---

<div class="post-metadata">

### Author: ![wiz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/wiz/32/2408_2.png) [@wiz](https://discourse.haskell.org/u/wiz)
#### Post date: [May 19, 2023, 12:49pm UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/16 "2023-05-19T12:49:59Z")

</div>

Just checked 6.3.3, no good.

---

<div class="post-metadata">

### Author: ![fommil](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/fommil/32/34_2.png) [@fommil](https://discourse.haskell.org/u/fommil)
#### Post date: [May 25, 2023, 4:15pm UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/17 "2023-05-25T16:15:18Z")

</div>

this is now fixed (again) in linux kernel `6.3.4`

---

<div class="post-metadata">

### Author: ![juhp](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/juhp/32/323_2.png) [@juhp](https://discourse.haskell.org/u/juhp)
#### Post date: [May 26, 2023, 6:42am UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/18 "2023-05-26T06:42:30Z")

</div>

Thanks @fommil

The maple\_tree fix also appears to be in Linux kernel [6.4-rc3](https://lore.kernel.org/lkml/CAHk-=wgL4Sa64qUZkpAs06t9UKoNyAYqBpzmukefJz8P8sa2=Q@mail.gmail.com/), so that is also good news.  
I tested it too successfully already in Fedora Rawhide.

Also I have pushed Fedora updates for the ghc and ghc9.2 packages with the backport into testing.

---

<div class="post-metadata">

### Author: ![mpickering](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/mpickering/32/4585_2.png) [@mpickering](https://discourse.haskell.org/u/mpickering)
#### Post date: [May 26, 2023, 3:03pm UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/19 "2023-05-26T15:03:28Z")

</div>

We have released [ghc-9.2.8](http://discourse.haskell.org/t/ghc-9-2-8-is-now-available/6328) with a fix to this issue.

---

<div class="post-metadata">

### Author: ![markus](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/markus/32/2194_2.png) [@markus](https://discourse.haskell.org/u/markus)
#### Post date: [July 6, 2023, 9:40am UTC](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259/21 "2023-07-06T09:40:10Z")

</div>

For anyone still stuck on a ghc prior to 9.2.8, adding the following to your cabal component works:

```haskell
  ghc-options:
    +RTS -xp -RTS

```

You could do `cabal repl --ghc-options='+RTS -xp -RTS' some-target` but that will also rebuild dependencies.

[Next page](https://discourse.haskell.org/t/facing-mmap-4096-bytes-at-nil-cannot-allocate-memory-youre-not-alone/6259.md?page=2)
