Closed Bug 2029736 Opened 7 months ago Closed 7 months ago

Add support for overriding jemalloc arena page allocator

Categories

(Core :: Memory Allocator, enhancement)

enhancement

Tracking

()

RESOLVED FIXED
151 Branch
Tracking Status
firefox151 --- fixed

People

(Reporter: abhishekcs, Assigned: abhishekcs)

References

(Blocks 1 open bug)

Details

Attachments

(4 external files, 1 obsolete file)

As a part of the experimental SpiderMonkey sandboxing effort (Bug 2029057), we need a mechanism to restrict the underlying pages used for all of SpiderMonkey's jemalloc arenas to a specific contiguous region of the process address space (the sandbox memory region). This would need jemalloc to allow plugging in a custom page allocator of sorts into it's allocation/deallocation logic for specific arenas. Additionally, since this is a security hardening measure - it would also entail removing these arenas from the common chunk recycling pool to avoid chunks from outside the sandbox memory region being used for SpiderMonkey memory.

I have a patchset that attempts to enable doing this by introducing the concept of an Arena "address space". The idea is to allow overriding the mechanisms used for all of an arena's page operations - currently map, unmap, commit and decommit seem to be sufficient for this. For existing arenas the default behavior continues to use a global system address space.

The force_zero argument is unused. It is assumed that pages_purge
always zeroes pages so doesn't need to return a result anymore.

Besides the small cleanup this allows a future change that allows
arena chunk deallocs to be oblivious to the ARENA vs HUGE chunk distinction.

Assignee: nobody → abhishekcs
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true

Add new Arena methods to abstract over the chunk/page manipulation
mechanisms at a per-Arena level.

For now these methods just delegate to the previously called
global functions, but when we introduce arena address spaces these
methods depend on internal arena state.

Introduce base_chunk_alloc and arena_chunk_alloc for base/arena chunk
allocations respectively. base_chunk_alloc skips the chunk recycling and
gChunkRTree insertion code paths as before.

Split up arena chunk deallocation into arena_chunk_dealloc and chunk_dealloc.
chunk_dealloc is used as a private helper when the chunk is known to not be
present in gChunkRTree.

chunk_record/chunk_recycle logic does not distinguish between
ARENA_CHUNK or HUGE_CHUNK in a meaningful way. It only checks
whether an incoming chunk is already ZEROED or not. So we can
treat all chunks passed to arena_chunk_dealloc as ARENA_CHUNK.

This change is mainly to allow the arena address space interface
introduced in the next change to not have to care about ChunkType
and avoid making ChunkType public.

The arena_address_space_t is added to the public API of jemalloc and can
be set at the time of arena creation for private arenas. This type is
basically a virtual table that provides implementations for memory
map, unmap, commit and decommit. The arena chunk/page manipulation methods
now use these functions provided by the address space. By default this is a
global system address space that preserves existing functionality.

The way this is currently implemented, an arena with a custom address space
will not have it's chunks recorded/recycled as before and would need to re-implement
that logic.

Blocks: 2029057

Thanks Abhishek Sharma.

I will start reviewing these soon. For context I want to try disabling the shared chunk pool because I suspect it doesn't help at all, most of the memory is decommitted so it's not a cache of memory but an address space reservation. We may want the address space reservation but I'd like to know if performance is affected at all. (Bug 2017208)

I also want to add the ability to mark some arenas as sensitive and, I need to confirm with security people, that they don't/do release chunks to the shared pool, and that they do/don't get chunks from the shared pool (which would then need to never release its own chunks). (Bug 1979830) Basically I need to understand which direction(s) we need to protect against address reuse. Controlling this should be done with arena flags: https://searchfox.org/firefox-main/source/memory/build/mozjemalloc_types.h#59-69,81

It sounds like your work will fit in with these ideas.

See Also: → 2017208
See Also: → 1979830

I see, thanks for the context Paul. Overall removing chunk sharing between arenas and marking them sensitive does overlap with what we're trying to do.
For the SpiderMonkey sandbox in addition to this we need a stronger property - that all arena pages lie in a specific address range. Currently the plan is that this would be a 1TB region of address space that is reserved when SpiderMonkey/the Sandbox is initialized. Now this region is not exclusively for SpiderMonkey's jemalloc arenas - but for all of it's other mappings/allocations too. For that reason, the underlying page allocation mechanism that manages this region would probably be external to jemalloc itself. To that end my patches don't actually change/provide the new page allocation mechanism but simply expose the ability for SpiderMonkey/the Sandbox to be able to provide one when creating the arena.

Beware that some platforms don't have 1TB of address space. 32-bits architectures, obviously, but also some aarch64 (mostly Android) where the limit is IIRC 512GB.

Yup, our plan for the JIT sandboxing MVP, is to keep the focus to Linux/Mac 64-bit as a start to keep things simple.

One aspect I want to clarify and document when I update the patches to address the comments in the review is whether an address space / chunk allocator implementation should be allowed to rely on jemalloc for it's own dynamic memory requirements?

Obviously this would need to be a different jemalloc arena to avoid a recursive/cyclical call into the same chunk allocator, but additionally requires jemalloc never hold non-arena specific locks before calling into the chunk allocator to avoid a deadlock - which I believe is the case currently.

The alternative would be to require any non-default chunk allocator implementations to use their own equivalent of a BaseAlloc-like base allocator.

Attachment #9566215 - Attachment description: Bug 2029736 - pt 3. Refactor to separate base chunk allocator and arena chunk alloc/dealloc r=glandium,pbone → Bug 2029736 - pt 3. Refactor to separate base chunk and arena chunk alloc/dealloc r=glandium,pbone
Attachment #9566217 - Attachment description: Bug 2029736 - pt 5. Introduce Arena address spaces that abstract over Arena page operations r=glandium,pbone → Bug 2029736 - pt 5. Introduce chunk allocators that abstract over Arena page operations r=glandium,pbone
Attachment #9566214 - Attachment is obsolete: true

(In reply to Abhishek Sharma from comment #10)

One aspect I want to clarify and document when I update the patches to address the comments in the review is whether an address space / chunk allocator implementation should be allowed to rely on jemalloc for it's own dynamic memory requirements?

Obviously this would need to be a different jemalloc arena to avoid a recursive/cyclical call into the same chunk allocator, but additionally requires jemalloc never hold non-arena specific locks before calling into the chunk allocator to avoid a deadlock - which I believe is the case currently.

The alternative would be to require any non-default chunk allocator implementations to use their own equivalent of a BaseAlloc-like base allocator.

As much as possible we try to avoid dynamic memory within the allocator, there are currently only 4 places where dynamic memory is required

  • arena_t structures,
  • extent_t structures - used by the chunk allocator for chunk recycling.
  • RadixTree nodes - also used by the chunk allocator.
  • PRNG.

All of these are fixed size, just that the number of them wasn't known at compile time so a very simple base allocator was used to allocate them. But because of work on thread local allocation I needed to update the base allocator to support arrays of arbitrary size that could be realloced. So Bug 1976184 makes that feasible. Now you should be able to use that base allocator by calling sBaseAlloc.alloc() or by deriving BaseAllocClass.

As you noticed the chunk allocator needs to avoid holding a lock when it calls the base allocator to prevent deadlock, but this could probably be improved. Although the chunk allocator should probably still avoid creating bookkeeping for the base allocator's chunks otherwise we'd need to modify https://searchfox.org/firefox-main/source/memory/build/mozjemalloc.cpp#2546-2552 to handle bookkeeping chunks.

Anyway:

  • Try to avoid dynamic allocation.
  • When you need dynamic allocation use the new base allocator (just landed!!), don't use a normal arena.
  • You should be okay with locking from your custom chunk allocator to the base allocator because the base allocator won't use your custom chunk allocator.

Oh, an idea. Once you have this done we could write a custom chunk allocator FOR the base allocator which may make some of the locking problems go away.

(In reply to Paul Bone [:pbone] from comment #11)

Ah right, the new base allocator should work well for custom chunk allocator implementers. But wouldn't this also need exporting BaseAlloc.h/ (or at least BaseAllocClass) from jemalloc - which is currently private. I can add that to my patchset if it is going to be the prescribed way for custom chunk allocators to make dynamic allocations.

(In reply to Paul Bone [:pbone] from comment #11)

Oh, an idea. Once you have this done we could write a custom chunk allocator FOR the base allocator which may make some of the locking problems go away.

Nevermind, you kind of did that already!

(In reply to Abhishek Sharma from comment #12)

(In reply to Paul Bone [:pbone] from comment #11)

Ah right, the new base allocator should work well for custom chunk allocator implementers. But wouldn't this also need exporting BaseAlloc.h/ (or at least BaseAllocClass) from jemalloc - which is currently private. I can add that to my patchset if it is going to be the prescribed way for custom chunk allocators to make dynamic allocations.

Maybe, it's not what I had in mind for the base allocator. The only problem I can see is by exporting it we endorse other parts of firefox to use it and IMHO it's just not suitable for that. It won't scale as well as normal allocation (for size OR across threads) and lacks some of the security features.

So I would lean to using either a specific arena or the default arena and ensuring that whatever arena is used does not have a custom chunk allocator.

Don't forget to renumber patches 3-5.

Attachment #9566215 - Attachment description: Bug 2029736 - pt 3. Refactor to separate base chunk and arena chunk alloc/dealloc r=glandium,pbone → Bug 2029736 - pt 2. Refactor to separate base chunk and arena chunk alloc/dealloc r=glandium,pbone
Attachment #9566216 - Attachment description: Bug 2029736 - pt 4. Make arena_chunk_dealloc independent of ChunkType r=glandium,pbone → Bug 2029736 - pt 3. Make arena_chunk_dealloc independent of ChunkType r=glandium,pbone
Attachment #9566217 - Attachment description: Bug 2029736 - pt 5. Introduce chunk allocators that abstract over Arena page operations r=glandium,pbone → Bug 2029736 - pt 4. Introduce chunk allocators that abstract over Arena page operations r=glandium,pbone

(In reply to Paul Bone [:pbone] from comment #14)

Maybe, it's not what I had in mind for the base allocator. The only problem I can see is by exporting it we endorse other parts of firefox to use it and IMHO it's just not suitable for that. It won't scale as well as normal allocation (for size OR across threads) and lacks some of the security features.

So I would lean to using either a specific arena or the default arena and ensuring that whatever arena is used does not have a custom chunk allocator.

Yeah that makes sense.

(In reply to Paul Bone [:pbone] from comment #11)

Oh, an idea. Once you have this done we could write a custom chunk allocator FOR the base allocator which may make some of the locking problems go away.

With the base allocator separated from the arena chunk allocator and the new BaseAlloc implementation - maybe one option to simplify locking concerns is to stop calling chunk_record on the base_chunk_dealloc path. This would keep both base_chunk_alloc and base_chunk_dealloc very simple and may be a place to start exploring removing chunk recycling altogether which you hinted at earlier.

Are you ready for me to land these patches?

Flags: needinfo?(abhishekcs)

Yes, this can be landed.

Flags: needinfo?(abhishekcs)
Pushed by rperta@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/8cb5b59dd92d https://hg.mozilla.org/integration/autoland/rev/01f2439590b1 Revert "Bug 2029736 - pt 4. Introduce chunk allocators that abstract over Arena page operations r=pbone" for causing hazard bustages

Backed out for causing hazard bustages
Backout link
Push with failures
Failure log(s)

Flags: needinfo?(abhishekcs)

Also this

Fixed a couple of issues:

The Jemalloc gtest failures were because the chunk allocator was not being set (was NULL) for arenas created without any arena_params. This was an oversight on my part - it looks like mach try auto does not trigger any job that catches this.

Separately, the GC rooting hazard analysis fails due to the introduction of new indirect calls to the jemalloc allocation path. I added the chunk_allocator_s struct to the ignoreClasses list so that it is not considered a hazard.

Flags: needinfo?(abhishekcs)

(In reply to Abhishek Sharma from comment #23)

Fixed a couple of issues:

The Jemalloc gtest failures were because the chunk allocator was not being set (was NULL) for arenas created without any arena_params. This was an oversight on my part - it looks like mach try auto does not trigger any job that catches this.

Locally you can run ./mach gtest Jemalloc\* to run some unit tests.

Yes, I did run the tests locally and they seem to pass now. Although a few of the tests end with a message that looks like this (and seemingly hanging / waiting for gdb attachment):

[WARNING] :: Death testsuse fork(), which is unsafe particularly in a threaded context. For this test, Google Test detected 21 threads. See https://github.com/google/googletest/blob/main/docs/advanced.md#death-tests-and-threads for more explanation and suggested solutions, especially if this is the last message you see before your test times out.

I'm guessing that is expected since gtest still tells me there are 0 failures.

(In reply to Abhishek Sharma from comment #25)

Yes, I did run the tests locally and they seem to pass now. Although a few of the tests end with a message that looks like this (and seemingly hanging / waiting for gdb attachment):

[WARNING] :: Death testsuse fork(), which is unsafe particularly in a threaded context. For this test, Google Test detected 21 threads. See https://github.com/google/googletest/blob/main/docs/advanced.md#death-tests-and-threads for more explanation and suggested solutions, especially if this is the last message you see before your test times out.

I'm guessing that is expected since gtest still tells me there are 0 failures.

Yes, that's expected. Sometimes it fails due to that warning, but it mostly works.

QA Whiteboard: [qa-triage-done-c152/b151]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: