Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common/MemArena: Set MAP_NORESERVE in LazyMemoryRegion on Linux. #12177

Merged
merged 1 commit into from Sep 12, 2023

Conversation

AdmiralCurtiss
Copy link
Contributor

This came up during the discussion that led to the #12173 fixes.

In short: When /proc/sys/vm/overcommit_memory is 0 (which appears to be the default in the Linux kernel), Linux refuses mmap requests of private anonymous memory that it knows cannot actually be fulfilled by the current amount of free RAM and swap. This causes the fast block lookup optimization to fail on low-memory (<= 4 GB) devices. This behavior makes sense (more or less) if you assume that all the allocated memory will actually be written to, but with our LazyMemoryRegion here, we assume that only a tiny amount will actually be touched before the memory will be reset or free'd. So the suggestion came up to just set MAP_NORESERVE -- which supresses the check and allows the allocation anyway.

I think this is reasonable and basically matches what Windows and Android do, so might as well. (For reference, on Android, the default of /proc/sys/vm/overcommit_memory is 1, so it never checks for anything and just allows the request to go through. We could set MAP_NORESERVE there too but it wouldn't really change anything unless someone messed with that setting.)

@AdmiralCurtiss
Copy link
Contributor Author

Oh that's fun, FreeBSD apparently doesn't implement MAP_NORESERVE: https://reviews.freebsd.org/rS273250. How does this allocation behave over there then?

@AdmiralCurtiss
Copy link
Contributor Author

Googling around suggests FreeBSD also by-default just lets all allocations go through, so we can just not set the flag there and be fine.

@@ -117,14 +117,21 @@ LazyMemoryRegion::~LazyMemoryRegion()
Release();
}

#if defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should also check if MAP_NORESERVE is defined? Just in case it gets implemented in the future.

@AdmiralCurtiss AdmiralCurtiss merged commit 3dada56 into dolphin-emu:master Sep 12, 2023
11 checks passed
@AdmiralCurtiss AdmiralCurtiss deleted the noreserve branch September 12, 2023 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants