blob: df7c999ad8d5e50d13a4bca4459dd45af8a81b65 [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
29#include "linker_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//
44// LinkerMemeoryAllocator is general purpose allocator
45// designed to provide the same functionality as the malloc/free/realloc
46// libc functions.
47//
48// On alloc:
49// If size is >= 1k allocator proxies malloc call directly to mmap
50// If size < 1k allocator uses SmallObjectAllocator for the size
51// rounded up to the nearest power of two.
52//
53// On free:
54//
55// For a pointer allocated using proxy-to-mmap allocator unmaps
56// the memory.
57//
58// For a pointer allocated using SmallObjectAllocator it adds
Vic Yang54938512018-12-02 23:46:26 -080059// the block to free_blocks_list in the corresponding page. If the number of
60// free pages reaches 2, SmallObjectAllocator munmaps one of the pages keeping
61// the other one in reserve.
62
63// Memory management for large objects is fairly straightforward, but for small
64// objects it is more complicated. If you are changing this code, one simple
65// way to evaluate the memory usage change is by running 'dd' and examine the
66// memory usage by 'showmap $(pidof dd)'. 'dd' is nice in that:
67// 1. It links in quite a few libraries, so you get some linker memory use.
68// 2. When run with no arguments, it sits waiting for input, so it is easy to
69// examine its memory usage with showmap.
70// 3. Since it does nothing while waiting for input, the memory usage is
71// determinisitic.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070072
73static const char kSignature[4] = {'L', 'M', 'A', 1};
74
75static const size_t kSmallObjectMaxSize = 1 << kSmallObjectMaxSizeLog2;
76
77// This type is used for large allocations (with size >1k)
78static const uint32_t kLargeObject = 111;
79
Vic Yang259429b2018-12-04 23:59:57 -080080// Allocated pointers must be at least 16-byte aligned. Round up the size of
81// page_info to multiple of 16.
82static constexpr size_t kPageInfoSize = __BIONIC_ALIGN(sizeof(page_info), 16);
83
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070084static inline uint16_t log2(size_t number) {
85 uint16_t result = 0;
86 number--;
87
88 while (number != 0) {
89 result++;
90 number >>= 1;
91 }
92
93 return result;
94}
95
Vic Yang54938512018-12-02 23:46:26 -080096LinkerSmallObjectAllocator::LinkerSmallObjectAllocator(uint32_t type,
97 size_t block_size)
98 : type_(type),
99 block_size_(block_size),
Vic Yang259429b2018-12-04 23:59:57 -0800100 blocks_per_page_((PAGE_SIZE - sizeof(small_object_page_info)) /
101 block_size),
Vic Yang54938512018-12-02 23:46:26 -0800102 free_pages_cnt_(0),
103 page_list_(nullptr) {}
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700104
105void* LinkerSmallObjectAllocator::alloc() {
Dimitry Ivanovf8572112016-07-13 10:24:06 -0700106 CHECK(block_size_ != 0);
107
Vic Yang54938512018-12-02 23:46:26 -0800108 if (page_list_ == nullptr) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700109 alloc_page();
110 }
111
Vic Yang54938512018-12-02 23:46:26 -0800112 // Fully allocated pages are de-managed and removed from the page list, so
113 // every page from the page list must be useable. Let's just take the first
114 // one.
115 small_object_page_info* page = page_list_;
116 CHECK(page->free_block_list != nullptr);
117
118 small_object_block_record* const block_record = page->free_block_list;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700119 if (block_record->free_blocks_cnt > 1) {
Vic Yang54938512018-12-02 23:46:26 -0800120 small_object_block_record* next_free =
121 reinterpret_cast<small_object_block_record*>(
122 reinterpret_cast<uint8_t*>(block_record) + block_size_);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700123 next_free->next = block_record->next;
124 next_free->free_blocks_cnt = block_record->free_blocks_cnt - 1;
Vic Yang54938512018-12-02 23:46:26 -0800125 page->free_block_list = next_free;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700126 } else {
Vic Yang54938512018-12-02 23:46:26 -0800127 page->free_block_list = block_record->next;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700128 }
129
Vic Yang259429b2018-12-04 23:59:57 -0800130 if (page->free_blocks_cnt == blocks_per_page_) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700131 free_pages_cnt_--;
132 }
133
Vic Yang54938512018-12-02 23:46:26 -0800134 page->free_blocks_cnt--;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700135
136 memset(block_record, 0, block_size_);
137
Vic Yang54938512018-12-02 23:46:26 -0800138 if (page->free_blocks_cnt == 0) {
139 // De-manage fully allocated pages. These pages will be managed again if
140 // a block is freed.
141 remove_from_page_list(page);
142 }
143
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700144 return block_record;
145}
146
Vic Yang54938512018-12-02 23:46:26 -0800147void LinkerSmallObjectAllocator::free_page(small_object_page_info* page) {
Vic Yang259429b2018-12-04 23:59:57 -0800148 CHECK(page->free_blocks_cnt == blocks_per_page_);
Vic Yang54938512018-12-02 23:46:26 -0800149 if (page->prev_page) {
150 page->prev_page->next_page = page->next_page;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700151 }
Vic Yang54938512018-12-02 23:46:26 -0800152 if (page->next_page) {
153 page->next_page->prev_page = page->prev_page;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700154 }
Vic Yang54938512018-12-02 23:46:26 -0800155 if (page_list_ == page) {
156 page_list_ = page->next_page;
157 }
158 munmap(page, PAGE_SIZE);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700159 free_pages_cnt_--;
160}
161
162void LinkerSmallObjectAllocator::free(void* ptr) {
Vic Yang54938512018-12-02 23:46:26 -0800163 small_object_page_info* const page =
164 reinterpret_cast<small_object_page_info*>(
165 PAGE_START(reinterpret_cast<uintptr_t>(ptr)));
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700166
Vic Yang259429b2018-12-04 23:59:57 -0800167 if (reinterpret_cast<uintptr_t>(ptr) % block_size_ != 0) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700168 async_safe_fatal("invalid pointer: %p (block_size=%zd)", ptr, block_size_);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700169 }
170
171 memset(ptr, 0, block_size_);
Vic Yang54938512018-12-02 23:46:26 -0800172 small_object_block_record* const block_record =
173 reinterpret_cast<small_object_block_record*>(ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700174
Vic Yang54938512018-12-02 23:46:26 -0800175 block_record->next = page->free_block_list;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700176 block_record->free_blocks_cnt = 1;
177
Vic Yang54938512018-12-02 23:46:26 -0800178 page->free_block_list = block_record;
179 page->free_blocks_cnt++;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700180
Vic Yang259429b2018-12-04 23:59:57 -0800181 if (page->free_blocks_cnt == blocks_per_page_) {
Vic Yang54938512018-12-02 23:46:26 -0800182 if (++free_pages_cnt_ > 1) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700183 // if we already have a free page - unmap this one.
Vic Yang54938512018-12-02 23:46:26 -0800184 free_page(page);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700185 }
Vic Yang54938512018-12-02 23:46:26 -0800186 } else if (page->free_blocks_cnt == 1) {
187 // We just freed from a full page. Add this page back to the list.
188 add_to_page_list(page);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700189 }
190}
191
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700192void LinkerSmallObjectAllocator::alloc_page() {
Vic Yang54938512018-12-02 23:46:26 -0800193 void* const map_ptr = mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE,
194 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700195 if (map_ptr == MAP_FAILED) {
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700196 async_safe_fatal("mmap failed: %s", strerror(errno));
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700197 }
198
Vic Yang54938512018-12-02 23:46:26 -0800199 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, PAGE_SIZE,
200 "linker_alloc_small_objects");
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700201
Vic Yang54938512018-12-02 23:46:26 -0800202 small_object_page_info* const page =
203 reinterpret_cast<small_object_page_info*>(map_ptr);
204 memcpy(page->info.signature, kSignature, sizeof(kSignature));
205 page->info.type = type_;
206 page->info.allocator_addr = this;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700207
Vic Yang259429b2018-12-04 23:59:57 -0800208 page->free_blocks_cnt = blocks_per_page_;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700209
Vic Yang259429b2018-12-04 23:59:57 -0800210 // Align the first block to block_size_.
211 const uintptr_t first_block_addr =
212 __BIONIC_ALIGN(reinterpret_cast<uintptr_t>(page + 1), block_size_);
Vic Yang54938512018-12-02 23:46:26 -0800213 small_object_block_record* const first_block =
Vic Yang259429b2018-12-04 23:59:57 -0800214 reinterpret_cast<small_object_block_record*>(first_block_addr);
215
Vic Yang54938512018-12-02 23:46:26 -0800216 first_block->next = nullptr;
Vic Yang259429b2018-12-04 23:59:57 -0800217 first_block->free_blocks_cnt = blocks_per_page_;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700218
Vic Yang54938512018-12-02 23:46:26 -0800219 page->free_block_list = first_block;
220
221 add_to_page_list(page);
Vic Yangde696602018-11-27 13:34:44 -0800222
223 free_pages_cnt_++;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700224}
225
Vic Yang54938512018-12-02 23:46:26 -0800226void LinkerSmallObjectAllocator::add_to_page_list(small_object_page_info* page) {
227 page->next_page = page_list_;
228 page->prev_page = nullptr;
229 if (page_list_) {
230 page_list_->prev_page = page;
231 }
232 page_list_ = page;
233}
234
235void LinkerSmallObjectAllocator::remove_from_page_list(
236 small_object_page_info* page) {
237 if (page->prev_page) {
238 page->prev_page->next_page = page->next_page;
239 }
240 if (page->next_page) {
241 page->next_page->prev_page = page->prev_page;
242 }
243 if (page_list_ == page) {
244 page_list_ = page->next_page;
245 }
246 page->prev_page = nullptr;
247 page->next_page = nullptr;
248}
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700249
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700250void LinkerMemoryAllocator::initialize_allocators() {
251 if (allocators_ != nullptr) {
252 return;
253 }
254
255 LinkerSmallObjectAllocator* allocators =
256 reinterpret_cast<LinkerSmallObjectAllocator*>(allocators_buf_);
257
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700258 for (size_t i = 0; i < kSmallObjectAllocatorsCount; ++i) {
259 uint32_t type = i + kSmallObjectMinSizeLog2;
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700260 new (allocators + i) LinkerSmallObjectAllocator(type, 1 << type);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700261 }
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700262
263 allocators_ = allocators;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700264}
265
266void* LinkerMemoryAllocator::alloc_mmap(size_t size) {
Vic Yang259429b2018-12-04 23:59:57 -0800267 size_t allocated_size = PAGE_END(size + kPageInfoSize);
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700268 void* map_ptr = mmap(nullptr, allocated_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
269 -1, 0);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700270
271 if (map_ptr == MAP_FAILED) {
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700272 async_safe_fatal("mmap failed: %s", strerror(errno));
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700273 }
274
275 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, allocated_size, "linker_alloc_lob");
276
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700277 page_info* info = reinterpret_cast<page_info*>(map_ptr);
278 memcpy(info->signature, kSignature, sizeof(kSignature));
279 info->type = kLargeObject;
280 info->allocated_size = allocated_size;
281
Vic Yang259429b2018-12-04 23:59:57 -0800282 return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(info) +
283 kPageInfoSize);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700284}
285
286void* LinkerMemoryAllocator::alloc(size_t size) {
287 // treat alloc(0) as alloc(1)
288 if (size == 0) {
289 size = 1;
290 }
291
292 if (size > kSmallObjectMaxSize) {
293 return alloc_mmap(size);
294 }
295
296 uint16_t log2_size = log2(size);
297
298 if (log2_size < kSmallObjectMinSizeLog2) {
299 log2_size = kSmallObjectMinSizeLog2;
300 }
301
302 return get_small_object_allocator(log2_size)->alloc();
303}
304
305page_info* LinkerMemoryAllocator::get_page_info(void* ptr) {
306 page_info* info = reinterpret_cast<page_info*>(PAGE_START(reinterpret_cast<size_t>(ptr)));
307 if (memcmp(info->signature, kSignature, sizeof(kSignature)) != 0) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700308 async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700309 }
310
311 return info;
312}
313
314void* LinkerMemoryAllocator::realloc(void* ptr, size_t size) {
315 if (ptr == nullptr) {
316 return alloc(size);
317 }
318
319 if (size == 0) {
320 free(ptr);
321 return nullptr;
322 }
323
324 page_info* info = get_page_info(ptr);
325
326 size_t old_size = 0;
327
328 if (info->type == kLargeObject) {
Vic Yang259429b2018-12-04 23:59:57 -0800329 old_size = info->allocated_size - kPageInfoSize;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700330 } else {
331 LinkerSmallObjectAllocator* allocator = get_small_object_allocator(info->type);
332 if (allocator != info->allocator_addr) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700333 async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700334 }
335
336 old_size = allocator->get_block_size();
337 }
338
339 if (old_size < size) {
340 void *result = alloc(size);
341 memcpy(result, ptr, old_size);
342 free(ptr);
343 return result;
344 }
345
346 return ptr;
347}
348
349void LinkerMemoryAllocator::free(void* ptr) {
350 if (ptr == nullptr) {
351 return;
352 }
353
354 page_info* info = get_page_info(ptr);
355
356 if (info->type == kLargeObject) {
357 munmap(info, info->allocated_size);
358 } else {
359 LinkerSmallObjectAllocator* allocator = get_small_object_allocator(info->type);
360 if (allocator != info->allocator_addr) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700361 async_safe_fatal("invalid pointer %p (invalid allocator address for the page)", ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700362 }
363
364 allocator->free(ptr);
365 }
366}
367
368LinkerSmallObjectAllocator* LinkerMemoryAllocator::get_small_object_allocator(uint32_t type) {
369 if (type < kSmallObjectMinSizeLog2 || type > kSmallObjectMaxSizeLog2) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700370 async_safe_fatal("invalid type: %u", type);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700371 }
372
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700373 initialize_allocators();
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700374 return &allocators_[type - kSmallObjectMinSizeLog2];
375}