Open anarazel opened 2 years ago
Thanks Andres for your report. Note that the warnings in this case are a bit "pedantic": they can be useful to debug performance problems but in principle the "overallocation" path works fine on Linux (just a tad slower). If Linux would have MAP_ALIGNED
(as in BSD) this problem would go away. I like your suggestion though of trying to avoid allocating close to other threads... I give it some thought if this can be done nicely.
The "pointer might be invalid" warning is not generally avoidable for huge blocks (> MI_SEGMENT_SIZE
).. but can be ignored if the next message says it was ok after all :-). Hmm, I think in debug mode I can actually improve the precision of the messages as it doesn't matter if it is_in_heap_region
test takes a bit longer in debug mode -- I'll try to fix that!
Turns out to be hard to really fix the warning in mi_usable_size
but I made a linux specific check to suppress the warning in the latest dev-slice
.
I just got a new machine (riscv/musl) and am seeing these warnings now. For example,
$ MIMALLOC_VERBOSE=1 gcc $CFLAGS $LDFLAGS main.c
mimalloc: process init: 0x3FB78BEDB0
mimalloc: debug level : 2
mimalloc: secure level: 4
mimalloc: mem tracking: none
mimalloc: using 1 numa regions
mimalloc: warning: unable to allocate aligned OS memory directly, fall back to over-allocation (size: 0x2000000 bytes, address: 0x003FB5400000, alignment: 0x2000000, commit: 1)
mimalloc: option 'show_errors': 1
mimalloc: option 'show_stats': 0
mimalloc: option 'verbose': 1
mimalloc: option 'eager_commit': 1
mimalloc: option 'arena_eager_commit': 2
mimalloc: option 'purge_decommits': 1
mimalloc: option 'allow_large_os_pages': 0
mimalloc: option 'reserve_huge_os_pages': 0
mimalloc: option 'reserve_huge_os_pages_at': -1
mimalloc: option 'reserve_os_memory': 0 KiB
mimalloc: option 'deprecated_segment_cache': 0
mimalloc: option 'deprecated_page_reset': 0
mimalloc: option 'abandoned_page_purge': 0
mimalloc: option 'deprecated_segment_reset': 0
mimalloc: option 'eager_commit_delay': 1
mimalloc: option 'purge_delay': 10
mimalloc: option 'use_numa_nodes': 0
mimalloc: option 'disallow_os_alloc': 0
mimalloc: option 'os_tag': 100
mimalloc: option 'max_errors': 32
mimalloc: option 'max_warnings': 32
mimalloc: option 'max_segment_reclaim': 10
mimalloc: option 'destroy_on_exit': 0
mimalloc: option 'arena_reserve': 1048576 KiB
mimalloc: option 'arena_purge_mult': 10
mimalloc: option 'purge_extend_delay': 1
mimalloc: option 'abandoned_reclaim_on_free': 1
mimalloc: option 'disallow_arena_alloc': 0
mimalloc: option 'retry_on_oom': 400
mimalloc: reserved 1048576 KiB memory
Is there anything I should configure to make the aligned allocation succeed, or is it doomed to the overallocation path? (In that case, is there a way to hide the warning?)
Very nice that you have a riscV machine -- I'm jealous :-).
Usually, aligned allocation should succeed -- what OS are you using? Is it 64-bit?
(if you look at src/prim/unix/prim.c
usually there is another warning message if direct or hinted aligned allocation fails but I don't see it in your output so that is a bit strange)
Um, linux? :)
... but due to the weird hardware, that's not the whole story. I'm running a Gentoo userland on the musl C library, but the kernel is a fork of the 6.1.x stable branch: https://github.com/sophgo/linux-riscv
I've also been reconfiguring the kernel myself, trying to figure out what hardware is actually in the box and how to make it work. So it's not even a nice 6.1.x with all modules and features enabled. I know mimalloc is sensitive to things like huge page support, madvise, access to /proc
... it is 64-bit though. (And I haven't turned off anything too important, I also hand-built the amd64 kernel I'm typing this on.)
If you think it should succeed, I can always add a bunch of printf statements and try to allocate a buffer for "Hello, World!"
Hmm, that is strange; I see now that (in src\prim\unix\prim.c
that the extra messages are trace messages, can you rerun with MIMALLOC_VERBOSE=2
(or 3?) and see if there is an extra message (saying "hinted" or "directly" allocated). ?
I wonder if the aligned hint method is failing where mimalloc tries to allocate in a 2TiB+ region in the address space.
Sure thing:
$ MIMALLOC_VERBOSE=3 gcc $CFLAGS $LDFLAGS main.c
mimalloc: process init: 0x3F83C88DB0
mimalloc: debug level : 2
mimalloc: secure level: 4
mimalloc: mem tracking: none
mimalloc: using 1 numa regions
mimalloc: unable to directly request hinted aligned OS memory (error: 2 (0x02), size: 0x2000000 bytes, alignment: 0x2000000, hint address: 0x050ADE000000)
mimalloc: warning: unable to allocate aligned OS memory directly, fall back to over-allocation (size: 0x2000000 bytes, address: 0x003F81800000, alignment: 0x2000000, commit: 1)
...
Interestingly enough, the warnings that I'm seeing during regular use go away with -DNDEBUG
. I can still see them with MIMALLOC_VERBOSE=1
(or higher), but if I compile mimalloc with -DNDEBUG
, then I don't see them when I'm just trying to run my linker.
I think I figured this out. It looks like RISC-V has several different virtual memory layouts,
https://www.kernel.org/doc/html/v6.11-rc7/arch/riscv/vm-layout.html
And I've got as CPU that uses SV39. Based on that chart it, I think the hint area that mimalloc wants to use is unusable, and that's why mmap consistently ignores the (aligned) hint and gives me back something unaligned. I was able to make the warning go away by hacking MI_HINT_BASE
, MI_HINT_AREA
, and MI_HINT_MAX
to much smaller values.
Hi,
I'm using mimalloc indirectly, via the mold linker. This is mimalloc 2.0.6+ds-2+b1 from debian unstable. On a new linux kernel (v6.1-rc3-158-gee6050c8af96) nearly every use of mold results in a lot of warnings. Here's some of the output with MIMALLOC_VERBOSE=1 set:
I created a tracepoint on probe_libmimalloc:mi_os_mem_alloc. The requests for the 64MB aligned allocation come from within mimalloc itself. Either below mi_reserve_os_memory() or mi_segments_page_alloc().
Looking further, I see that an MAP_FIXED mmap fails to return memory at that address:
As far as I can tell there are no conflicting mmap requests made be mimalloc.
My vague suspicion is that this is related to linux promoting one of the pages before / after to use transparent huge pages. The region after did get converted by THP:
That region was concurrently allocated by another thread that finished before the "failing" MAP_FIXED:
It's possible that this is just a short term issue in linux and will get fixed. But it might also be better to not allocate neighboring segments concurrently.
It might be helpful to note the requested address in the warning. Could the "mi_usable_size: pointer might not point to a valid heap region" warnings be avoided in this case?
Regards,
Andres