cloudius-systems / osv

OSv, a new operating system for the cloud.
osv.io
Other
4.12k stars 605 forks source link

posix_memalign fails when size and alignment is large #1304

Open Meandres opened 8 months ago

Meandres commented 8 months ago

posix_memalign() fails at the assertion that checks if the memory is aligned when calling it with an alignment bigger than the page size and a size bigger than 2088961. I found out about this when trying to run https://github.com/ssvb/tinymembench, which, during the latency benchmark, allocates a 64MB region aligned to 4MB. I managed to make this allocation work by changing the continuous flag to true in https://github.com/cloudius-systems/osv/blob/master/core/mempool.cc#L1854

Of course, posix_memalign does not have this problem on Linux. It might be quite a niche problem.

nyh commented 8 months ago

The bug is that malloc_large() when size is bigger than mmu::huge_page_size (2MB) and contiguous is not requested, calls mapped_malloc_large(size, offset) - this completely ignores the alignment parameter, and you always get a single-page (4KB) alignment - so the assertion in posix_memalign that it got the right alignment can fail.

By the way, the same call that ignores alignment is also used at the end of malloc_large() if attempting a continugous allocation failed.

I guess the best way to fix this bug would be to add an alignment parameter (which will need to be more than one page) to map_anon, and basically to allocate() in core/mmu.cc and find_hole(). I think it's not hard, but will be a bit ugly to do all these changes for such an obscure edge case :-(