Add support for overriding jemalloc arena page allocator
Categories
(Core :: Memory Allocator, enhancement)
Tracking
()
| 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.
| Assignee | ||
Comment 1•7 months ago
|
||
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.
Updated•7 months ago
|
| Assignee | ||
Comment 2•7 months ago
|
||
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.
| Assignee | ||
Comment 3•7 months ago
|
||
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.
| Assignee | ||
Comment 4•7 months ago
|
||
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.
| Assignee | ||
Comment 5•7 months ago
|
||
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.
Comment 6•7 months ago
•
|
||
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.
| Assignee | ||
Comment 7•7 months ago
|
||
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.
Comment 8•7 months ago
|
||
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.
Comment 9•7 months ago
|
||
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.
| Assignee | ||
Comment 10•7 months ago
|
||
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.
Updated•7 months ago
|
Updated•7 months ago
|
Updated•7 months ago
|
Comment 11•7 months ago
|
||
(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.
| Assignee | ||
Comment 12•7 months ago
|
||
(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.
Comment 13•7 months ago
|
||
(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!
Comment 14•7 months ago
|
||
(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.
Comment 15•7 months ago
|
||
Don't forget to renumber patches 3-5.
Updated•7 months ago
|
Updated•7 months ago
|
Updated•7 months ago
|
| Assignee | ||
Comment 16•7 months ago
|
||
(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.
Comment 19•7 months ago
|
||
Comment 20•7 months ago
|
||
Comment 21•7 months ago
|
||
Backed out for causing hazard bustages
Backout link
Push with failures
Failure log(s)
Comment 22•7 months ago
|
||
Also this
| Assignee | ||
Comment 23•7 months ago
|
||
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.
Comment 24•7 months ago
|
||
(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 likemach try autodoes not trigger any job that catches this.
Locally you can run ./mach gtest Jemalloc\* to run some unit tests.
| Assignee | ||
Comment 25•7 months ago
|
||
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.
Comment 26•7 months ago
|
||
Comment 27•7 months ago
|
||
(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.
Comment 28•7 months ago
|
||
| bugherder | ||
https://hg.mozilla.org/mozilla-central/rev/f652c061b5d1
https://hg.mozilla.org/mozilla-central/rev/22c7b1b4a7b6
https://hg.mozilla.org/mozilla-central/rev/609e447b5772
https://hg.mozilla.org/mozilla-central/rev/388015d75210
Updated•7 months ago
|
Description
•