Commit Graph

123 Commits

Author SHA1 Message Date
Matthew Parkinson
f0e2ab702a Major refactor of snmalloc (#343)
# Pagemap
 
The Pagemap now stores all the meta-data for the object allocation. The meta-data in the pagemap is effectively a triple of the sizeclass, the remote allocator, and a pointer to a 64 byte block of meta-data for this chunk of memory. By storing the pointer to a block, it allows the pagemap to handle multiple slab sizes without branching on the fast path. There is one entry in the pagemap per 16KiB of address space, but by using the same entry in the pagemap for 4 adjacent entries, then we can treat a 64KiB range can be treated as a single slab of allocations.

This change also means there is almost no capability amplification required by the implementation on CHERI for finding meta-data. The only amplification is required, when we change the way a chunk is used to a size of object allocation.


# Backend

There is a second major aspect of the refactor that there is now a narrow API that abstracts the Pagemap, PAL and address space management. This should better enable the compartmentalisation and makes it easier to produce alternative backends for various research directions. This is a template parameter that can be used to specialised by the front-end in different ways.

# Thread local state

The thread local state has been refactored into two components, one (called 'localalloc') that is stored directly in the TLS and is constant initialised, and one that is allocated in the address space (called 'coreallloc') which is lazily created and pooled.

# Difference

This removes Superslabs/Medium slabs as there meta-data is now part of the pagemap.
2021-07-12 15:53:36 +01:00
Istvan Haller
18d7cc99b6 Extended TrivialInitAtomic (#340) 2021-06-23 10:50:28 +01:00
Theo Butler
7d346c0b2d Add init method to ABA on ARM (#331)
This method is used in the Verona SPMCQ.
2021-05-26 21:06:42 +01:00
David Carlier
15a7e159c0 consteval introduction proposal. 2021-05-04 14:43:27 +01:00
David Carlier
f3a9d3a682 c++20 (timid) introduction of constinit proposal. 2021-05-04 14:43:27 +01:00
David Carlier
e3a7eab789 unlikely annotation introduction proposal. 2021-04-30 09:49:28 +01:00
Nathaniel Filardo
80c6e95210 C++20: Add TrivialInitAtomic
C++20 does away with trivial initializers for std::atomic<T>, which means our
global pagemaps always get zeroed, sometimes after other static ctors have run
(fun fun!).  Use the new std::atomic_ref<T> when available.  Abstract all this
behind an #ifdef-ful wrapper.
2021-04-22 01:28:24 +01:00
Nathaniel Filardo
e31751fc94 Add workaround for MSVC vs. CapPtr constructor 2021-04-09 12:39:29 +01:00
Nathaniel Filardo
294887ad74 SP: introduce CapPtr<> wrapper type & bounds taxonomy
This commit just fills out ds/ptrwrap.h with the new types and adds utility
methods to ds/address.h.
2021-04-09 12:39:29 +01:00
Matthew Parkinson
7202f9e091 Handle UB that was not exercised originally. 2021-04-07 11:43:53 +01:00
Nathaniel Filardo
e5a94ed902 NFC: constexpr some subexpressions in ds/bits
MSVC throws warnings for things that could be but aren't constexpr.
2021-03-24 11:55:05 +00:00
Matthew Parkinson
fd7b040823 Align wrapper for array. 2021-03-19 20:11:15 +00:00
Nathaniel Filardo
1549e40705 NFC: cdllist: some residual pointer wrapper violence
Missed this the last time around
2021-03-19 15:17:56 +00:00
Nathaniel Filardo
5938f0b5a6 NFC: add align_{down,up}<size_t>(address_t) 2021-03-19 15:17:56 +00:00
Nathaniel Filardo
ebc02a141e NFC: MPMCStack: prepare for pointer wrappers 2021-03-19 15:17:56 +00:00
Nathaniel Filardo
414be336f5 NFC: mpscq: prepare for pointer wrappers 2021-03-16 09:29:19 +00:00
Nathaniel Filardo
93024a2471 NFC: dllist: prepare for pointer wrapping 2021-03-16 09:29:19 +00:00
Nathaniel Filardo
1d1b013d85 NFC: cdllist: prepare for pointer wrappers 2021-03-16 09:29:19 +00:00
Nathaniel Filardo
73b86e6dff NFC: Introduce skeletal ds/ptrwrap.h
This lets us go ahead and land several preparatory commits without waiting for
the real AuthPtr<> types to show up.
2021-03-16 09:29:19 +00:00
Nathaniel Filardo
cbab7a3455 NFC: pointer_offset* functions always return void*
This requires that the caller perform the cast on the output rather than the
input, which is a little closer to the truth.  Shuffle some casts into the right
position.
2021-03-16 09:29:19 +00:00
Nathaniel Filardo
db0ca64ff3 NFC: Add an AAL Concept, too
While here, pull out some constants to their own header.  Eventually we'll
want to match on AalFeatures in the AAL Concept.
2021-03-16 09:29:19 +00:00
Nathaniel Filardo
83b72722cf NFC: Split InvalidPointer out of ds/dllist.h
This lets us use Pagemaps without requiring dllists in scope
2021-03-16 09:29:19 +00:00
Matthew Parkinson
59edf294d0 Move link out of object space.
The link object was previously stored in a disused object.  This is
good for reducing meta-data, but if we want to reduce the meta-data
corruption potential, then this is not a good design choice.

This commit moves it into the Metaslab.
2021-03-15 13:28:03 +00:00
Matthew Parkinson
1d12e34b9f Fix for Mac OS X 10.14
The dllist was able to call delete during a destructor if a template
flag was set.  This flag is never set in snmalloc, and was included
to enable reuse in another project.  This was triggering an error on
older mac builds.

This PR calls a templated function when the DLList is destructed.  Hence
other projects can specify the `delete` behaviour if required.
2021-03-09 15:07:21 +00:00
Matthew Parkinson
a3660c4069 Update to use a more efficient power of 2 check. (#274) 2021-01-27 11:58:41 +00:00
Matthew Parkinson
923705e514 Natural alignment for USE_MALLOC (#248)
* Add concept of natural alignment to tests.

snmalloc naturally aligns blocks very heavily, so that
the largest power-of-two in the rounded size is the alignment.
This checks that in the test, and provides a method for
finding the natural alignment of a block.

* Improve USE_MALLOC to provide alignment

snmalloc provides a lot of alginment guarantees. This ensures that when
we pass through to the system allocator we still get those alignment
guarantees.

The commit also fixes the tests to work with USE_MALLOC, and builds a
set of unit tests for ctest to check behaviour.
2020-09-28 10:08:19 +01:00
Matthew Parkinson
d3ecd66f73 Add constexpr annotation to align_up/down. 2020-09-11 14:08:01 +01:00
Nathaniel Filardo
3e21ea1f65 Add C++ concept for PAL
This will not be used unless the C++ standard version is raised to 20.  As
concepts and C++20 more generally are quite new, this does not do so.
Nevertheless, the use of concepts can improve the local development experience
as type mismatches are discovered earlier (at template invocation rather than
only during expansion).
2020-09-09 12:55:48 +01:00
Nathaniel Filardo
82bc0e6852 ds/aba: make Cmp constructor explicit
For reasons unknown, this appeases MSVC using C++20.
2020-09-09 12:55:48 +01:00
Matthew Parkinson
234c0e2e8e Clang format 2020-08-26 17:51:46 +01:00
Matthew Parkinson
3adfe00f51 Missing 32bit case. 2020-08-26 17:51:46 +01:00
Matthew Parkinson
dc36849dd0 Reinstate staticcasts. 2020-08-26 17:51:46 +01:00
Matthew Parkinson
0728db1413 Fixes to ctz on Windows Clang. 2020-08-26 17:51:46 +01:00
Matthew Parkinson
87dfd41553 Improve code quality for alloc_size (#196)
* Improve code quality for alloc_size

* Made error noreturn.

* Update docs.

* Move annoation
2020-05-23 16:16:12 +01:00
Matthew Parkinson
fbbc2ddb76 Merge pull request #191 from nwf/aal-address_t
Allow the AAL to define address_t
2020-05-23 08:26:29 +01:00
Nathaniel Filardo
12111ae7d3 ds/cdllist: factor out next to base classes
Choose between implementations based on aal's requirement of strict
provenance.
2020-05-21 12:43:38 +00:00
Nathaniel Filardo
a1704a82e1 ds/dllist: sentinels are always addresses 2020-05-20 14:36:06 +00:00
Nathaniel Filardo
7e4d6916e0 ds/address::is_aligned_block: don't cast to size_t
Now that address_t is always a scalar, there's no reason to cast to
size_t.
2020-05-20 14:36:06 +00:00
Nathaniel Filardo
7b296a91fa Let the AAL define address_t. 2020-05-20 14:36:06 +00:00
Nathaniel Filardo
3f32f37e60 Eliminate pointer_cast()
Since we anticipate address_t not carrying provenance on CHERI, but
rather being vaddr_t there, it doesn't make sense to offer conversion
back to a provenance-carrying pointer.

Thankfully, there is not much to be done here: the uses were few and
could be replaced with the vocabulary of other pointer operations in
ds/address.h
2020-05-19 14:53:55 +00:00
Matthew Parkinson
a9cfc3a2b4 Various minor changes to aid compiling with std14 (#182)
These changes make the code compile in clang10 with -std14.
2020-05-07 15:02:31 +01:00
Paul Liétar
0f5cc165e8 Don't require 16-byte CAS on x86. (#173)
* Don't require 16-byte CAS on x86.

We only need to CAS 2 pointers, which is always possible on x86.

* Fix the way the Image variable is referenced.

* Replace failOnStderr by `set -eo pipefail` on Linux.

The x86 image doesn't have clangformat, which causes cmake to print a
warning on the stderr. Exit codes should be enough to detect failure.

* Use x86 images from snmallocciteam.

* clang-format
2020-04-16 07:33:02 +01:00
Matthew Parkinson
fe8b8a0891 Improved assume defines. 2020-04-13 08:33:52 +01:00
Matthew Parkinson
cf9c2eb9d9 Addressing OE linking issues when built with GCC (#167)
* Use C++17 inline statics

This leads to better codegen in GCC, and fixes some linking issues in OE.

* Detect GCC and OE combination and fall-back to lock based ABA.

* clangformat
2020-04-10 13:49:39 +01:00
Paul Liétar
6f697e06ef Add missing closing > 2020-04-10 12:54:09 +02:00
Paul Liétar
1ccb808a18 Fix clang-tidy warning and CR comments. 2020-04-10 12:36:36 +02:00
Paul Liétar
794a5912c7 Replace uses of std::function by function_ref. 2020-04-09 15:47:11 +02:00
Paul Liétar
89523a96bd Add back the peek methods to ABA. (#165)
These are used by the Verona runtime.
2020-04-09 14:13:57 +01:00
Matthew Parkinson
74657d9dbc Defensive code for alloc/dealloc during TLS teardown (#161)
* Defensive code for alloc/dealloc during TLS teardown

If an allocation or deallocation occurs during TLS teardown, then it is
possible for a new allocator to be created and then this is leaked. On
the mimalloc-bench mstressN benchmark this was observed leading to a
large memory leak.

This fix, detects if we are in the TLS teardown phase, and if so,
the calls to alloc or dealloc must return the allocator once they have
perform the specific operation.

Uses a separate variable to represent if a thread_local's destructor has
run already.  This is used to detect thread teardown to put the
allocator into a special slow path to avoid leaks.

* Added some printing first operation to track progress

* Improve error messages on posix

Flush errors, print assert details, and present stack traces.

* Detect incorrect use of pool.

* Clang format.

* Replace broken LL/SC implementation

LL/SC implementation was broken, this replaces it with
a locking implementation. Changes the API to support LL/SC
for future implementation on ARM.

* Improve TLS teardown.

* Make std::function fully inlined.

* Factor out PALLinux stack trace.

* Add checks for leaking allocators.

* Add release build of Windows Clang
2020-04-07 15:37:26 +01:00
Matthew Parkinson
d900e29424 Improve slow path performance for allocation (#143)
* Remote dealloc refactor.

* Improve remote dealloc

Change remote to count down to 0, so fast path does not need a constant.

Use signed value so that branch does not depend on addition.

* Inline remote_dealloc

The fast path of remote_dealloc is sufficiently compact that it can be
inlined.

* Improve fast path in Slab::alloc

Turn the internal structure into tail calls, to improve fast path.
Should be no algorithmic changes.

* Refactor initialisation to help fast path.

Break lazy initialisation into two functions, so it is easier to codegen
fast paths.

* Minor tidy to statically sized dealloc.

* Refactor semi-slow path for alloc

Make the backup path a bit faster.  Only algorithmic change is to delay
checking for first allocation. Otherwise, should be unchanged.

* Test initial operation of a thread

The first operation a new thread takes is special.  It results in
allocating an allocator, and swinging it into the TLS.  This makes
this a very special path, that is rarely tested.  This test generates
a lot of threads to cover the first alloc and dealloc operations.

* Correctly handle reusing get_noncachable

* Fix large alloc stats

Large alloc stats aren't necessarily balanced on a thread, this changes
to tracking individual pushs and pops, rather than the net effect
(with an unsigned value).

* Fix TLS init on large alloc path

* Add Bump ptrs to allocator

Each allocator has a bump ptr for each size class.  This is no longer
slab local.

Slabs that haven't been fully allocated no longer need to be in the DLL
for this sizeclass.

* Change to a cycle non-empty list

This change reduces the branching in the case of finding a new free
list. Using a non-empty cyclic list enables branch free add, and a
single branch in remove to detect the empty case.

* Update differences

* Rename first allocation

Use needs initialisation as makes more sense for other scenarios.

* Use a ptrdiff to help with zero init.

* Make GlobalPlaceholder zero init

The GlobalPlaceholder allocator is now a zero init block of memory.
This removes various issues for when things are initialised. It is made read-only
to we detect write to it on some platforms.
2020-03-31 09:17:53 +01:00