Skip to content

[JITLink] Always unmap standard segments in InProcessMemoryManager::deallocate #81943

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

Merged
merged 2 commits into from
Feb 17, 2024

Conversation

mshockwave
Copy link
Member

Right now InProcessMemoryManager only releases a standard segment (via sys::Memory::releaseMappedMemory) in deallocate when there is a DeallocAction associated, leaving residual memory pages in the process until termination.
Despite being a de facto memory leak, it won't cause a major issue if users only create a single LLJIT instance per process, which is the most common use cases. It will, however, drain virtual memory pages if we create thousands of ephemeral LLJIT instances in the same process.

This patch fixes this issue by releasing every standard segments regardless of the attached DeallocAction.

…eallocate

Right now InProcessMemoryManager only releases a standard segment (via
sys::Memory::releaseMappedMemory) in `deallocate` when there is a
DeallocAction associated, leaving residual memory pages in the process
until termination.
Despite being a de facto memory leak, it won't cause a major issue if
users only create a single LLJIT instance per process, which is the most
common use cases. It will, however, drain virtual memory pages if we create
thousands of ephemeral LLJIT instances in the same process.

This patch fixes this issue by releasing every standard segments
regardless of the attached DeallocAction.
@mshockwave mshockwave requested a review from lhames February 15, 2024 23:58
@mshockwave
Copy link
Member Author

I'm not sure how to add new tests for this (it passes existing x86_64 JITLink tests though). I pin-pointed this issue by checking the /proc/<PID>/maps on Linux but I guess we can't add that as a test.

@lhames
Copy link
Contributor

lhames commented Feb 16, 2024

Thanks for catching this @mshockwave!

Rather than using an optional, what if we removed the condition here:

      if (!FA->DeallocActions.empty())
	DeallocActionsList.push_back(std::move(FA->DeallocActions));

and just unconditionally moved the DeallocActions vector? I think that should be as cheap as the optional, and otherwise have the same effect.

@mshockwave
Copy link
Member Author

Thanks for catching this @mshockwave!

Rather than using an optional, what if we removed the condition here:

      if (!FA->DeallocActions.empty())
	DeallocActionsList.push_back(std::move(FA->DeallocActions));

and just unconditionally moved the DeallocActions vector? I think that should be as cheap as the optional, and otherwise have the same effect.

You're right, that's more efficient. I've fixed that.

Copy link
Contributor

@lhames lhames left a comment

Choose a reason for hiding this comment

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

I'm not sure how to add new tests for this...

I don't have a great idea either. I'm happy for it to land as is. Thanks again for fixing this!

@mshockwave mshockwave merged commit 3d67cf6 into llvm:main Feb 17, 2024
@mshockwave mshockwave deleted the patch/jitlink-missing-unmmap branch February 17, 2024 00:20
Icenowy pushed a commit to AOSC-Tracking/llvm-project that referenced this pull request Aug 30, 2024
…eallocate (llvm#81943)

Right now InProcessMemoryManager only releases a standard segment (via
sys::Memory::releaseMappedMemory) in `deallocate` when there is a
DeallocAction associated, leaving residual memory pages in the process
until termination.
Despite being a de facto memory leak, it won't cause a major issue if
users only create a single LLJIT instance per process, which is the most
common use cases. It will, however, drain virtual memory pages if we
create thousands of ephemeral LLJIT instances in the same process.

This patch fixes this issue by releasing every standard segments
regardless of the attached DeallocAction.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants