Kalvin Lee | 5f8555b | 2022-05-16 21:30:20 | [diff] [blame] | 1 | # Glossary |
| 2 | |
| 3 | This page describes some core terminology used in PartitionAlloc. |
| 4 | A weak attempt is made to present terms "in conceptual order" s.t. |
| 5 | each term depends mainly upon previously defined ones. |
| 6 | |
Kalvin Lee | 5f8555b | 2022-05-16 21:30:20 | [diff] [blame] | 7 | * **Partition**: A heap that is separated and protected both from other |
| 8 | partitions and from non-PartitionAlloc memory. Each partition holds |
| 9 | multiple buckets. |
Kalvin Lee | 8338a46 | 2022-07-29 02:19:25 | [diff] [blame] | 10 | |
| 11 | *** promo |
| 12 | **NOTE**: In code (and comments), "partition," "root," and even |
| 13 | "allocator" are all conceptually the same thing. |
| 14 | *** |
| 15 | |
Kalvin Lee | 5f8555b | 2022-05-16 21:30:20 | [diff] [blame] | 16 | ## Pages |
| 17 | |
| 18 | * **System Page**: A memory page defined by the CPU/OS. Commonly |
| 19 | referred to as a "virtual page" in other contexts. This is typically |
| 20 | 4KiB, but it can be larger. PartitionAlloc supports up to 64KiB, |
| 21 | though this constant isn't always known at compile time (depending |
| 22 | on the OS). |
| 23 | * **Partition Page**: The most common granularity used by |
| 24 | PartitionAlloc. Consists of exactly 4 system pages. |
| 25 | * **Super Page**: A 2MiB region, aligned on a 2MiB boundary. Not to |
| 26 | be confused with OS-level terms like "large page" or "huge page", |
| 27 | which are also commonly 2MiB. These have to be fully committed / |
| 28 | uncommitted in memory, whereas super pages can be partially committed |
| 29 | with system page granularity. |
Kalvin Lee | 8338a46 | 2022-07-29 02:19:25 | [diff] [blame] | 30 | * **Extent**: An extent is a run of consecutive super pages (belonging |
| 31 | to a single partition). Extents are to super pages what slot spans are |
| 32 | to slots (see below). |
Kalvin Lee | 5f8555b | 2022-05-16 21:30:20 | [diff] [blame] | 33 | |
| 34 | ## Slots and Spans |
| 35 | |
| 36 | * **Slot**: An indivisible allocation unit. Slot sizes are tied to |
| 37 | buckets. For example, each allocation that falls into the bucket |
| 38 | (224, 256] would be satisfied with a slot of size 256. This |
| 39 | applies only to normal buckets, not to direct map. |
| 40 | * **Slot Span**: A run of same-sized slots that are contiguous in |
| 41 | memory. Slot span size is a multiple of partition page size, but it |
| 42 | isn't always a multiple of slot size, although we try hard for this |
| 43 | to be the case. |
| 44 | * **Small Bucket**: Allocations up to 4 partition pages. In these |
| 45 | cases, slot spans are always between 1 and 4 partition pages in |
| 46 | size. For each slot span size, the slot span is chosen to minimize |
| 47 | number of pages used while keeping the rounding waste under a |
| 48 | reasonable limit. |
| 49 | * For example, for a slot size 96, 64B waste is deemed acceptable |
| 50 | when using a single partition page, but for slot size |
| 51 | 384, the potential waste of 256B wouldn't be, so 3 partition pages |
| 52 | are used to achieve 0B waste. |
| 53 | * PartitionAlloc may avoid waste by lowering the number of committed |
| 54 | system pages compared to the number of reserved pages. For |
| 55 | example, for the slot size of 896B we'd use a slot span of 2 |
| 56 | partition pages of 16KiB, i.e. 8 system pages of 4KiB, but commit |
| 57 | only up to 7, thus resulting in perfect packing. |
| 58 | * **Single-Slot Span**: Allocations above 4 partition pages (but |
| 59 | ≤`kMaxBucketed`). This is because each slot span is guaranteed to |
| 60 | hold exactly one slot. |
| 61 | * Fun fact: there are sizes ≤4 partition pages that result in a |
| 62 | slot span having exactly 1 slot, but nonetheless they're still |
| 63 | classified as small buckets. The reason is that single-slot spans |
| 64 | are often handled by a different code path, and that distinction |
| 65 | is made purely based on slot size, for simplicity and efficiency. |
| 66 | |
Kalvin Lee | 1389225 | 2023-04-13 07:29:23 | [diff] [blame] | 67 | ## Buckets |
| 68 | |
| 69 | * **Bucket**: A collection of regions in a partition that contains |
| 70 | similar-sized objects. For example, one bucket may hold objects of |
| 71 | size (224, 256], another (256, 320], etc. Bucket size |
| 72 | brackets are geometrically spaced, |
| 73 | [going up to `kMaxBucketed`][max-bucket-comment]. |
| 74 | * Plainly put, all slots (ergo the resulting spans) of a given size |
| 75 | class are logically chained into one bucket. |
Kalvin Lee | e42e143 | 2023-04-13 07:52:41 | [diff] [blame] | 76 | |
| 77 | ![A bucket, spanning multiple super pages, collects spans whose |
Kalvin Lee | a928781 | 2023-11-21 00:12:05 | [diff] [blame^] | 78 | slots are of a particular size class.](./src/partition_alloc/dot/bucket.png) |
Kalvin Lee | e42e143 | 2023-04-13 07:52:41 | [diff] [blame] | 79 | |
Kalvin Lee | 1389225 | 2023-04-13 07:29:23 | [diff] [blame] | 80 | * **Normal Bucket**: Any bucket whose size ceiling does not exceed |
| 81 | `kMaxBucketed`. This is the common case in PartitionAlloc, and |
| 82 | the "normal" modifier is often dropped in casual reference. |
| 83 | * **Direct Map (Bucket)**: Any allocation whose size exceeds `kMaxBucketed`. |
| 84 | |
Kalvin Lee | 5f8555b | 2022-05-16 21:30:20 | [diff] [blame] | 85 | ## Other Terms |
| 86 | |
| 87 | * **Object**: A chunk of memory returned to the allocating invoker |
| 88 | of the size requested. It doesn't have to span the entire slot, |
| 89 | nor does it have to begin at the slot start. This term is commonly |
| 90 | used as a parameter name in PartitionAlloc code, as opposed to |
| 91 | `slot_start`. |
| 92 | * **Thread Cache**: A [thread-local structure][pa-thread-cache] that |
| 93 | holds some not-too-large memory chunks, ready to be allocated. This |
| 94 | speeds up in-thread allocation by reducing a lock hold to a |
| 95 | thread-local storage lookup, improving cache locality. |
Bartek Nowierski | f2d03ca | 2022-10-04 10:08:57 | [diff] [blame] | 96 | * **Pool**: A large (and contiguous on 64-bit) virtual address region, housing |
Kalvin Lee | 67bcfa5 | 2022-10-03 02:58:50 | [diff] [blame] | 97 | super pages, etc. from which PartitionAlloc services allocations. The |
| 98 | primary purpose of the pools is to provide a fast answer to the |
| 99 | question, "Did PartitionAlloc allocate the memory for this pointer |
| 100 | from this pool?" with a single bit-masking operation. |
Bartek Nowierski | f2d03ca | 2022-10-04 10:08:57 | [diff] [blame] | 101 | * The regular pool is a general purpose pool that contains allocations that |
| 102 | aren't protected by BackupRefPtr. |
| 103 | * The BRP pool contains all allocations protected by BackupRefPtr. |
| 104 | * [64-bit only] The configurable pool is named generically, because its |
| 105 | primary user (the [V8 Sandbox][v8-sandbox]) can configure it at runtime, |
| 106 | providing a pre-existing mapping. Its allocations aren't protected by |
| 107 | BackupRefPtr. |
Stephen Roettger | 3554d01 | 2023-05-10 09:06:54 | [diff] [blame] | 108 | * [64-bit only] The thread isolated pool is returning memory protected with |
| 109 | per-thread permissions. At the moment, this is implemented for pkeys on x64. |
| 110 | It's primary user is [V8 CFI][v8-cfi]. |
Kalvin Lee | 67bcfa5 | 2022-10-03 02:58:50 | [diff] [blame] | 111 | |
Kalvin Lee | 9dae9c0 | 2023-07-11 08:19:30 | [diff] [blame] | 112 | ![The singular AddressPoolManager mediates access to the separate pools |
Kalvin Lee | a928781 | 2023-11-21 00:12:05 | [diff] [blame^] | 113 | for each PartitionRoot.](./src/partition_alloc/dot/address-space.png) |
Kalvin Lee | 9dae9c0 | 2023-07-11 08:19:30 | [diff] [blame] | 114 | |
Kalvin Lee | 67bcfa5 | 2022-10-03 02:58:50 | [diff] [blame] | 115 | *** promo |
Bartek Nowierski | f2d03ca | 2022-10-04 10:08:57 | [diff] [blame] | 116 | Pools are downgraded into a logical concept in 32-bit environments, |
| 117 | tracking a non-contiguous set of allocations using a bitmap. |
Kalvin Lee | 67bcfa5 | 2022-10-03 02:58:50 | [diff] [blame] | 118 | *** |
| 119 | |
Kalvin Lee | 5f8555b | 2022-05-16 21:30:20 | [diff] [blame] | 120 | * **Payload**: The usable area of a super page in which slot spans |
| 121 | reside. While generally this means "everything between the first |
| 122 | and last guard partition pages in a super page," the presence of |
| 123 | other metadata (e.g. StarScan bitmaps) can bump the starting offset |
| 124 | forward. While this term is entrenched in the code, the team |
| 125 | considers it suboptimal and is actively looking for a replacement. |
Kalvin Lee | dbbd6e4 | 2022-08-09 02:35:37 | [diff] [blame] | 126 | * **Allocation Fast Path**: A path taken during an allocation that is |
| 127 | considered fast. Usually means that an allocation request can be |
| 128 | immediately satisfied by grabbing a slot from the freelist of the |
| 129 | first active slot span in the bucket. |
| 130 | * **Allocation Slow Path**: Anything which is not fast (see above). |
| 131 | Can involve |
| 132 | * finding another active slot span in the list, |
| 133 | * provisioning more slots in a slot span, |
| 134 | * bringing back a free (or decommitted) slot span, |
| 135 | * allocating a new slot span, or even |
| 136 | * allocating a new super page. |
| 137 | |
| 138 | *** aside |
| 139 | By "slow" we may mean something as simple as extra logic (`if` |
| 140 | statements etc.), or something as costly as system calls. |
| 141 | *** |
Kalvin Lee | 5f8555b | 2022-05-16 21:30:20 | [diff] [blame] | 142 | |
Kalvin Lee | 67bcfa5 | 2022-10-03 02:58:50 | [diff] [blame] | 143 | ## Legacy Terms |
| 144 | |
| 145 | These terms are (mostly) deprecated and should not be used. They are |
| 146 | surfaced here to provide a ready reference for readers coming from |
| 147 | older design documents or documentation. |
| 148 | |
| 149 | * **GigaCage**: A memory region several gigabytes wide, reserved by |
| 150 | PartitionAlloc upon initialization, from which nearly all allocations |
| 151 | are taken. _Pools_ have overtaken GigaCage in conceptual importance, |
| 152 | and so and so there is less need today to refer to "GigaCage" or the |
| 153 | "cage." This is especially true given the V8 Sandbox and the |
| 154 | configurable pool (see above). |
| 155 | |
Kalvin Lee | 5f8555b | 2022-05-16 21:30:20 | [diff] [blame] | 156 | ## PartitionAlloc-Everywhere |
| 157 | |
| 158 | Originally, PartitionAlloc was used only in Blink (Chromium's rendering engine). |
| 159 | It was invoked explicitly, by calling PartitionAlloc APIs directly. |
| 160 | |
| 161 | PartitionAlloc-Everywhere is the name of the project that brought PartitionAlloc |
| 162 | to the entire-ish codebase (exclusions apply). This was done by intercepting |
| 163 | `malloc()`, `free()`, `realloc()`, aforementioned `posix_memalign()`, etc. and |
| 164 | routing them into PartitionAlloc. The shim located in |
Yuki Shiino | 550a6e7 | 2023-10-24 23:07:29 | [diff] [blame] | 165 | `base/allocator/partition_allocator/src/partition_alloc/shim/allocator_shim_default_dispatch_to_partition_alloc.h` is |
Kalvin Lee | 5f8555b | 2022-05-16 21:30:20 | [diff] [blame] | 166 | responsible for intercepting. For more details, see |
| 167 | [base/allocator/README.md](../../../base/allocator/README.md). |
| 168 | |
| 169 | A special, catch-it-all *Malloc* partition has been created for the intercepted |
| 170 | `malloc()` et al. This is to isolate from already existing Blink partitions. |
| 171 | The only exception from that is Blink's *FastMalloc* partition, which was also |
| 172 | catch-it-all in nature, so it's perfectly fine to merge these together, to |
| 173 | minimize fragmentation. |
| 174 | |
| 175 | As of 2022, PartitionAlloc-Everywhere is supported on |
| 176 | |
| 177 | * Windows 32- and 64-bit |
| 178 | * Linux |
| 179 | * Android 32- and 64-bit |
| 180 | * macOS |
| 181 | * Fuchsia |
| 182 | |
Arthur Sonzogni | 2f4afa6 | 2023-10-03 08:48:32 | [diff] [blame] | 183 | [max-bucket-comment]: https://source.chromium.org/chromium/chromium/src/+/main:base/allocator/partition_allocator/src/partition_alloc/partition_alloc_constants.h;l=345;drc=667e6b001f438521e1c1a1bc3eabeead7aaa1f37 |
| 184 | [pa-thread-cache]: https://source.chromium.org/chromium/chromium/src/+/main:base/allocator/partition_allocator/src/partition_alloc/thread_cache.h |
Kalvin Lee | 67bcfa5 | 2022-10-03 02:58:50 | [diff] [blame] | 185 | [v8-sandbox]: https://docs.google.com/document/d/1FM4fQmIhEqPG8uGp5o9A-mnPB5BOeScZYpkHjo0KKA8/preview# |
Stephen Roettger | 6b495de | 2022-11-07 15:55:18 | [diff] [blame] | 186 | [v8-cfi]: https://docs.google.com/document/d/1O2jwK4dxI3nRcOJuPYkonhTkNQfbmwdvxQMyXgeaRHo/preview# |