blob: a933212827e37334bc392a5d9a60d5093de20d65 [file] [log] [blame]
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08003 * All rights reserved.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -07004 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08005 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070014 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -080015 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070027 */
28
Ryan Prichard083d8502019-01-24 13:47:13 -080029#include "private/bionic_allocator.h"
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070030
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070031#include <stdlib.h>
Ryan Prichard52165b32019-01-23 17:46:24 -080032#include <string.h>
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070033#include <sys/mman.h>
Elliott Hughes99d54652018-08-22 10:36:23 -070034#include <sys/prctl.h>
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070035#include <unistd.h>
36
Ryan Prichard52165b32019-01-23 17:46:24 -080037#include <new>
38
Christopher Ferris7a3681e2017-04-24 17:48:32 -070039#include <async_safe/log.h>
40
Ryan Prichard52165b32019-01-23 17:46:24 -080041#include "private/bionic_page.h"
42
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070043//
Ryan Prichard083d8502019-01-24 13:47:13 -080044// BionicAllocator is a general purpose allocator designed to provide the same
45// functionality as the malloc/free/realloc libc functions.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070046//
47// On alloc:
48// If size is >= 1k allocator proxies malloc call directly to mmap
49// If size < 1k allocator uses SmallObjectAllocator for the size
50// rounded up to the nearest power of two.
51//
52// On free:
53//
54// For a pointer allocated using proxy-to-mmap allocator unmaps
55// the memory.
56//
57// For a pointer allocated using SmallObjectAllocator it adds
Vic Yang54938512018-12-02 23:46:26 -080058// the block to free_blocks_list in the corresponding page. If the number of
59// free pages reaches 2, SmallObjectAllocator munmaps one of the pages keeping
60// the other one in reserve.
61
62// Memory management for large objects is fairly straightforward, but for small
63// objects it is more complicated. If you are changing this code, one simple
64// way to evaluate the memory usage change is by running 'dd' and examine the
65// memory usage by 'showmap $(pidof dd)'. 'dd' is nice in that:
66// 1. It links in quite a few libraries, so you get some linker memory use.
67// 2. When run with no arguments, it sits waiting for input, so it is easy to
68// examine its memory usage with showmap.
69// 3. Since it does nothing while waiting for input, the memory usage is
70// determinisitic.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070071
72static const char kSignature[4] = {'L', 'M', 'A', 1};
73
74static const size_t kSmallObjectMaxSize = 1 << kSmallObjectMaxSizeLog2;
75
76// This type is used for large allocations (with size >1k)
77static const uint32_t kLargeObject = 111;
78
Vic Yang259429b2018-12-04 23:59:57 -080079// Allocated pointers must be at least 16-byte aligned. Round up the size of
80// page_info to multiple of 16.
81static constexpr size_t kPageInfoSize = __BIONIC_ALIGN(sizeof(page_info), 16);
82
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070083static inline uint16_t log2(size_t number) {
84 uint16_t result = 0;
85 number--;
86
87 while (number != 0) {
88 result++;
89 number >>= 1;
90 }
91
92 return result;
93}
94
Ryan Prichard083d8502019-01-24 13:47:13 -080095BionicSmallObjectAllocator::BionicSmallObjectAllocator(uint32_t type,
Vic Yang54938512018-12-02 23:46:26 -080096 size_t block_size)
97 : type_(type),
98 block_size_(block_size),
Vic Yang259429b2018-12-04 23:59:57 -080099 blocks_per_page_((PAGE_SIZE - sizeof(small_object_page_info)) /
100 block_size),
Vic Yang54938512018-12-02 23:46:26 -0800101 free_pages_cnt_(0),
102 page_list_(nullptr) {}
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700103
Ryan Prichard083d8502019-01-24 13:47:13 -0800104void* BionicSmallObjectAllocator::alloc() {
Dimitry Ivanovf8572112016-07-13 10:24:06 -0700105 CHECK(block_size_ != 0);
106
Vic Yang54938512018-12-02 23:46:26 -0800107 if (page_list_ == nullptr) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700108 alloc_page();
109 }
110
Vic Yang54938512018-12-02 23:46:26 -0800111 // Fully allocated pages are de-managed and removed from the page list, so
112 // every page from the page list must be useable. Let's just take the first
113 // one.
114 small_object_page_info* page = page_list_;
115 CHECK(page->free_block_list != nullptr);
116
117 small_object_block_record* const block_record = page->free_block_list;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700118 if (block_record->free_blocks_cnt > 1) {
Vic Yang54938512018-12-02 23:46:26 -0800119 small_object_block_record* next_free =
120 reinterpret_cast<small_object_block_record*>(
121 reinterpret_cast<uint8_t*>(block_record) + block_size_);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700122 next_free->next = block_record->next;
123 next_free->free_blocks_cnt = block_record->free_blocks_cnt - 1;
Vic Yang54938512018-12-02 23:46:26 -0800124 page->free_block_list = next_free;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700125 } else {
Vic Yang54938512018-12-02 23:46:26 -0800126 page->free_block_list = block_record->next;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700127 }
128
Vic Yang259429b2018-12-04 23:59:57 -0800129 if (page->free_blocks_cnt == blocks_per_page_) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700130 free_pages_cnt_--;
131 }
132
Vic Yang54938512018-12-02 23:46:26 -0800133 page->free_blocks_cnt--;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700134
135 memset(block_record, 0, block_size_);
136
Vic Yang54938512018-12-02 23:46:26 -0800137 if (page->free_blocks_cnt == 0) {
138 // De-manage fully allocated pages. These pages will be managed again if
139 // a block is freed.
140 remove_from_page_list(page);
141 }
142
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700143 return block_record;
144}
145
Ryan Prichard083d8502019-01-24 13:47:13 -0800146void BionicSmallObjectAllocator::free_page(small_object_page_info* page) {
Vic Yang259429b2018-12-04 23:59:57 -0800147 CHECK(page->free_blocks_cnt == blocks_per_page_);
Vic Yang54938512018-12-02 23:46:26 -0800148 if (page->prev_page) {
149 page->prev_page->next_page = page->next_page;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700150 }
Vic Yang54938512018-12-02 23:46:26 -0800151 if (page->next_page) {
152 page->next_page->prev_page = page->prev_page;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700153 }
Vic Yang54938512018-12-02 23:46:26 -0800154 if (page_list_ == page) {
155 page_list_ = page->next_page;
156 }
157 munmap(page, PAGE_SIZE);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700158 free_pages_cnt_--;
159}
160
Ryan Prichard083d8502019-01-24 13:47:13 -0800161void BionicSmallObjectAllocator::free(void* ptr) {
Vic Yang54938512018-12-02 23:46:26 -0800162 small_object_page_info* const page =
163 reinterpret_cast<small_object_page_info*>(
164 PAGE_START(reinterpret_cast<uintptr_t>(ptr)));
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700165
Vic Yang259429b2018-12-04 23:59:57 -0800166 if (reinterpret_cast<uintptr_t>(ptr) % block_size_ != 0) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700167 async_safe_fatal("invalid pointer: %p (block_size=%zd)", ptr, block_size_);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700168 }
169
170 memset(ptr, 0, block_size_);
Vic Yang54938512018-12-02 23:46:26 -0800171 small_object_block_record* const block_record =
172 reinterpret_cast<small_object_block_record*>(ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700173
Vic Yang54938512018-12-02 23:46:26 -0800174 block_record->next = page->free_block_list;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700175 block_record->free_blocks_cnt = 1;
176
Vic Yang54938512018-12-02 23:46:26 -0800177 page->free_block_list = block_record;
178 page->free_blocks_cnt++;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700179
Vic Yang259429b2018-12-04 23:59:57 -0800180 if (page->free_blocks_cnt == blocks_per_page_) {
Vic Yang54938512018-12-02 23:46:26 -0800181 if (++free_pages_cnt_ > 1) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700182 // if we already have a free page - unmap this one.
Vic Yang54938512018-12-02 23:46:26 -0800183 free_page(page);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700184 }
Vic Yang54938512018-12-02 23:46:26 -0800185 } else if (page->free_blocks_cnt == 1) {
186 // We just freed from a full page. Add this page back to the list.
187 add_to_page_list(page);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700188 }
189}
190
Ryan Prichard083d8502019-01-24 13:47:13 -0800191void BionicSmallObjectAllocator::alloc_page() {
Vic Yang54938512018-12-02 23:46:26 -0800192 void* const map_ptr = mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE,
193 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700194 if (map_ptr == MAP_FAILED) {
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700195 async_safe_fatal("mmap failed: %s", strerror(errno));
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700196 }
197
Vic Yang54938512018-12-02 23:46:26 -0800198 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, PAGE_SIZE,
Ryan Prichard083d8502019-01-24 13:47:13 -0800199 "bionic_alloc_small_objects");
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700200
Vic Yang54938512018-12-02 23:46:26 -0800201 small_object_page_info* const page =
202 reinterpret_cast<small_object_page_info*>(map_ptr);
203 memcpy(page->info.signature, kSignature, sizeof(kSignature));
204 page->info.type = type_;
205 page->info.allocator_addr = this;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700206
Vic Yang259429b2018-12-04 23:59:57 -0800207 page->free_blocks_cnt = blocks_per_page_;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700208
Vic Yang259429b2018-12-04 23:59:57 -0800209 // Align the first block to block_size_.
210 const uintptr_t first_block_addr =
211 __BIONIC_ALIGN(reinterpret_cast<uintptr_t>(page + 1), block_size_);
Vic Yang54938512018-12-02 23:46:26 -0800212 small_object_block_record* const first_block =
Vic Yang259429b2018-12-04 23:59:57 -0800213 reinterpret_cast<small_object_block_record*>(first_block_addr);
214
Vic Yang54938512018-12-02 23:46:26 -0800215 first_block->next = nullptr;
Vic Yang259429b2018-12-04 23:59:57 -0800216 first_block->free_blocks_cnt = blocks_per_page_;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700217
Vic Yang54938512018-12-02 23:46:26 -0800218 page->free_block_list = first_block;
219
220 add_to_page_list(page);
Vic Yangde696602018-11-27 13:34:44 -0800221
222 free_pages_cnt_++;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700223}
224
Ryan Prichard083d8502019-01-24 13:47:13 -0800225void BionicSmallObjectAllocator::add_to_page_list(small_object_page_info* page) {
Vic Yang54938512018-12-02 23:46:26 -0800226 page->next_page = page_list_;
227 page->prev_page = nullptr;
228 if (page_list_) {
229 page_list_->prev_page = page;
230 }
231 page_list_ = page;
232}
233
Ryan Prichard083d8502019-01-24 13:47:13 -0800234void BionicSmallObjectAllocator::remove_from_page_list(
Vic Yang54938512018-12-02 23:46:26 -0800235 small_object_page_info* page) {
236 if (page->prev_page) {
237 page->prev_page->next_page = page->next_page;
238 }
239 if (page->next_page) {
240 page->next_page->prev_page = page->prev_page;
241 }
242 if (page_list_ == page) {
243 page_list_ = page->next_page;
244 }
245 page->prev_page = nullptr;
246 page->next_page = nullptr;
247}
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700248
Ryan Prichard083d8502019-01-24 13:47:13 -0800249void BionicAllocator::initialize_allocators() {
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700250 if (allocators_ != nullptr) {
251 return;
252 }
253
Ryan Prichard083d8502019-01-24 13:47:13 -0800254 BionicSmallObjectAllocator* allocators =
255 reinterpret_cast<BionicSmallObjectAllocator*>(allocators_buf_);
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700256
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700257 for (size_t i = 0; i < kSmallObjectAllocatorsCount; ++i) {
258 uint32_t type = i + kSmallObjectMinSizeLog2;
Ryan Prichard083d8502019-01-24 13:47:13 -0800259 new (allocators + i) BionicSmallObjectAllocator(type, 1 << type);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700260 }
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700261
262 allocators_ = allocators;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700263}
264
Ryan Prichard083d8502019-01-24 13:47:13 -0800265void* BionicAllocator::alloc_mmap(size_t size) {
Vic Yang259429b2018-12-04 23:59:57 -0800266 size_t allocated_size = PAGE_END(size + kPageInfoSize);
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700267 void* map_ptr = mmap(nullptr, allocated_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
268 -1, 0);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700269
270 if (map_ptr == MAP_FAILED) {
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700271 async_safe_fatal("mmap failed: %s", strerror(errno));
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700272 }
273
Ryan Prichard083d8502019-01-24 13:47:13 -0800274 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, allocated_size, "bionic_alloc_lob");
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700275
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700276 page_info* info = reinterpret_cast<page_info*>(map_ptr);
277 memcpy(info->signature, kSignature, sizeof(kSignature));
278 info->type = kLargeObject;
279 info->allocated_size = allocated_size;
280
Vic Yang259429b2018-12-04 23:59:57 -0800281 return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(info) +
282 kPageInfoSize);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700283}
284
Ryan Prichard083d8502019-01-24 13:47:13 -0800285void* BionicAllocator::alloc(size_t size) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700286 // treat alloc(0) as alloc(1)
287 if (size == 0) {
288 size = 1;
289 }
290
291 if (size > kSmallObjectMaxSize) {
292 return alloc_mmap(size);
293 }
294
295 uint16_t log2_size = log2(size);
296
297 if (log2_size < kSmallObjectMinSizeLog2) {
298 log2_size = kSmallObjectMinSizeLog2;
299 }
300
301 return get_small_object_allocator(log2_size)->alloc();
302}
303
Ryan Prichard083d8502019-01-24 13:47:13 -0800304page_info* BionicAllocator::get_page_info(void* ptr) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700305 page_info* info = reinterpret_cast<page_info*>(PAGE_START(reinterpret_cast<size_t>(ptr)));
306 if (memcmp(info->signature, kSignature, sizeof(kSignature)) != 0) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700307 async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700308 }
309
310 return info;
311}
312
Ryan Prichard083d8502019-01-24 13:47:13 -0800313void* BionicAllocator::realloc(void* ptr, size_t size) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700314 if (ptr == nullptr) {
315 return alloc(size);
316 }
317
318 if (size == 0) {
319 free(ptr);
320 return nullptr;
321 }
322
323 page_info* info = get_page_info(ptr);
324
325 size_t old_size = 0;
326
327 if (info->type == kLargeObject) {
Vic Yang259429b2018-12-04 23:59:57 -0800328 old_size = info->allocated_size - kPageInfoSize;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700329 } else {
Ryan Prichard083d8502019-01-24 13:47:13 -0800330 BionicSmallObjectAllocator* allocator = get_small_object_allocator(info->type);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700331 if (allocator != info->allocator_addr) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700332 async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700333 }
334
335 old_size = allocator->get_block_size();
336 }
337
338 if (old_size < size) {
339 void *result = alloc(size);
340 memcpy(result, ptr, old_size);
341 free(ptr);
342 return result;
343 }
344
345 return ptr;
346}
347
Ryan Prichard083d8502019-01-24 13:47:13 -0800348void BionicAllocator::free(void* ptr) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700349 if (ptr == nullptr) {
350 return;
351 }
352
353 page_info* info = get_page_info(ptr);
354
355 if (info->type == kLargeObject) {
356 munmap(info, info->allocated_size);
357 } else {
Ryan Prichard083d8502019-01-24 13:47:13 -0800358 BionicSmallObjectAllocator* allocator = get_small_object_allocator(info->type);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700359 if (allocator != info->allocator_addr) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700360 async_safe_fatal("invalid pointer %p (invalid allocator address for the page)", ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700361 }
362
363 allocator->free(ptr);
364 }
365}
366
Ryan Prichard083d8502019-01-24 13:47:13 -0800367BionicSmallObjectAllocator* BionicAllocator::get_small_object_allocator(uint32_t type) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700368 if (type < kSmallObjectMinSizeLog2 || type > kSmallObjectMaxSizeLog2) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700369 async_safe_fatal("invalid type: %u", type);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700370 }
371
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700372 initialize_allocators();
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700373 return &allocators_[type - kSmallObjectMinSizeLog2];
374}