blob: 374bef30d9a718fa016ccb620bf2362a76e7559d [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"
Dimitry Ivanovf8572112016-07-13 10:24:06 -070030#include "linker_debug.h"
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070031#include "linker.h"
32
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070033#include <stdlib.h>
34#include <sys/mman.h>
Elliott Hughes99d54652018-08-22 10:36:23 -070035#include <sys/prctl.h>
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070036#include <unistd.h>
37
Christopher Ferris7a3681e2017-04-24 17:48:32 -070038#include <async_safe/log.h>
39
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070040//
41// LinkerMemeoryAllocator is general purpose allocator
42// designed to provide the same functionality as the malloc/free/realloc
43// libc functions.
44//
45// On alloc:
46// If size is >= 1k allocator proxies malloc call directly to mmap
47// If size < 1k allocator uses SmallObjectAllocator for the size
48// rounded up to the nearest power of two.
49//
50// On free:
51//
52// For a pointer allocated using proxy-to-mmap allocator unmaps
53// the memory.
54//
55// For a pointer allocated using SmallObjectAllocator it adds
Vic Yang54938512018-12-02 23:46:26 -080056// the block to free_blocks_list in the corresponding page. If the number of
57// free pages reaches 2, SmallObjectAllocator munmaps one of the pages keeping
58// the other one in reserve.
59
60// Memory management for large objects is fairly straightforward, but for small
61// objects it is more complicated. If you are changing this code, one simple
62// way to evaluate the memory usage change is by running 'dd' and examine the
63// memory usage by 'showmap $(pidof dd)'. 'dd' is nice in that:
64// 1. It links in quite a few libraries, so you get some linker memory use.
65// 2. When run with no arguments, it sits waiting for input, so it is easy to
66// examine its memory usage with showmap.
67// 3. Since it does nothing while waiting for input, the memory usage is
68// determinisitic.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070069
70static const char kSignature[4] = {'L', 'M', 'A', 1};
71
72static const size_t kSmallObjectMaxSize = 1 << kSmallObjectMaxSizeLog2;
73
74// This type is used for large allocations (with size >1k)
75static const uint32_t kLargeObject = 111;
76
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070077static inline uint16_t log2(size_t number) {
78 uint16_t result = 0;
79 number--;
80
81 while (number != 0) {
82 result++;
83 number >>= 1;
84 }
85
86 return result;
87}
88
Vic Yang54938512018-12-02 23:46:26 -080089LinkerSmallObjectAllocator::LinkerSmallObjectAllocator(uint32_t type,
90 size_t block_size)
91 : type_(type),
92 block_size_(block_size),
93 free_pages_cnt_(0),
94 page_list_(nullptr) {}
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070095
96void* LinkerSmallObjectAllocator::alloc() {
Dimitry Ivanovf8572112016-07-13 10:24:06 -070097 CHECK(block_size_ != 0);
98
Vic Yang54938512018-12-02 23:46:26 -080099 if (page_list_ == nullptr) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700100 alloc_page();
101 }
102
Vic Yang54938512018-12-02 23:46:26 -0800103 // Fully allocated pages are de-managed and removed from the page list, so
104 // every page from the page list must be useable. Let's just take the first
105 // one.
106 small_object_page_info* page = page_list_;
107 CHECK(page->free_block_list != nullptr);
108
109 small_object_block_record* const block_record = page->free_block_list;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700110 if (block_record->free_blocks_cnt > 1) {
Vic Yang54938512018-12-02 23:46:26 -0800111 small_object_block_record* next_free =
112 reinterpret_cast<small_object_block_record*>(
113 reinterpret_cast<uint8_t*>(block_record) + block_size_);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700114 next_free->next = block_record->next;
115 next_free->free_blocks_cnt = block_record->free_blocks_cnt - 1;
Vic Yang54938512018-12-02 23:46:26 -0800116 page->free_block_list = next_free;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700117 } else {
Vic Yang54938512018-12-02 23:46:26 -0800118 page->free_block_list = block_record->next;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700119 }
120
Vic Yang54938512018-12-02 23:46:26 -0800121 if (page->allocated_blocks_cnt == 0) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700122 free_pages_cnt_--;
123 }
124
Vic Yang54938512018-12-02 23:46:26 -0800125 page->free_blocks_cnt--;
126 page->allocated_blocks_cnt++;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700127
128 memset(block_record, 0, block_size_);
129
Vic Yang54938512018-12-02 23:46:26 -0800130 if (page->free_blocks_cnt == 0) {
131 // De-manage fully allocated pages. These pages will be managed again if
132 // a block is freed.
133 remove_from_page_list(page);
134 }
135
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700136 return block_record;
137}
138
Vic Yang54938512018-12-02 23:46:26 -0800139void LinkerSmallObjectAllocator::free_page(small_object_page_info* page) {
140 CHECK(page->allocated_blocks_cnt == 0);
141 if (page->prev_page) {
142 page->prev_page->next_page = page->next_page;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700143 }
Vic Yang54938512018-12-02 23:46:26 -0800144 if (page->next_page) {
145 page->next_page->prev_page = page->prev_page;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700146 }
Vic Yang54938512018-12-02 23:46:26 -0800147 if (page_list_ == page) {
148 page_list_ = page->next_page;
149 }
150 munmap(page, PAGE_SIZE);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700151 free_pages_cnt_--;
152}
153
154void LinkerSmallObjectAllocator::free(void* ptr) {
Vic Yang54938512018-12-02 23:46:26 -0800155 small_object_page_info* const page =
156 reinterpret_cast<small_object_page_info*>(
157 PAGE_START(reinterpret_cast<uintptr_t>(ptr)));
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700158
Vic Yang54938512018-12-02 23:46:26 -0800159 const ssize_t offset =
160 reinterpret_cast<uintptr_t>(ptr) - sizeof(small_object_page_info);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700161
162 if (offset % block_size_ != 0) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700163 async_safe_fatal("invalid pointer: %p (block_size=%zd)", ptr, block_size_);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700164 }
165
166 memset(ptr, 0, block_size_);
Vic Yang54938512018-12-02 23:46:26 -0800167 small_object_block_record* const block_record =
168 reinterpret_cast<small_object_block_record*>(ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700169
Vic Yang54938512018-12-02 23:46:26 -0800170 block_record->next = page->free_block_list;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700171 block_record->free_blocks_cnt = 1;
172
Vic Yang54938512018-12-02 23:46:26 -0800173 page->free_block_list = block_record;
174 page->free_blocks_cnt++;
175 page->allocated_blocks_cnt--;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700176
Vic Yang54938512018-12-02 23:46:26 -0800177 if (page->allocated_blocks_cnt == 0) {
178 if (++free_pages_cnt_ > 1) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700179 // if we already have a free page - unmap this one.
Vic Yang54938512018-12-02 23:46:26 -0800180 free_page(page);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700181 }
Vic Yang54938512018-12-02 23:46:26 -0800182 } else if (page->free_blocks_cnt == 1) {
183 // We just freed from a full page. Add this page back to the list.
184 add_to_page_list(page);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700185 }
186}
187
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700188void LinkerSmallObjectAllocator::alloc_page() {
Vic Yang54938512018-12-02 23:46:26 -0800189 static_assert(sizeof(small_object_page_info) % 16 == 0,
190 "sizeof(small_object_page_info) is not multiple of 16");
191 void* const map_ptr = mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE,
192 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700193 if (map_ptr == MAP_FAILED) {
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700194 async_safe_fatal("mmap failed: %s", strerror(errno));
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700195 }
196
Vic Yang54938512018-12-02 23:46:26 -0800197 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, PAGE_SIZE,
198 "linker_alloc_small_objects");
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700199
Vic Yang54938512018-12-02 23:46:26 -0800200 small_object_page_info* const page =
201 reinterpret_cast<small_object_page_info*>(map_ptr);
202 memcpy(page->info.signature, kSignature, sizeof(kSignature));
203 page->info.type = type_;
204 page->info.allocator_addr = this;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700205
Vic Yang54938512018-12-02 23:46:26 -0800206 const size_t free_blocks_cnt =
207 (PAGE_SIZE - sizeof(small_object_page_info)) / block_size_;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700208
Vic Yang54938512018-12-02 23:46:26 -0800209 page->free_blocks_cnt = free_blocks_cnt;
210 page->allocated_blocks_cnt = 0;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700211
Vic Yang54938512018-12-02 23:46:26 -0800212 small_object_block_record* const first_block =
213 reinterpret_cast<small_object_block_record*>(page + 1);
214 first_block->next = nullptr;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700215 first_block->free_blocks_cnt = free_blocks_cnt;
216
Vic Yang54938512018-12-02 23:46:26 -0800217 page->free_block_list = first_block;
218
219 add_to_page_list(page);
Vic Yangde696602018-11-27 13:34:44 -0800220
221 free_pages_cnt_++;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700222}
223
Vic Yang54938512018-12-02 23:46:26 -0800224void LinkerSmallObjectAllocator::add_to_page_list(small_object_page_info* page) {
225 page->next_page = page_list_;
226 page->prev_page = nullptr;
227 if (page_list_) {
228 page_list_->prev_page = page;
229 }
230 page_list_ = page;
231}
232
233void LinkerSmallObjectAllocator::remove_from_page_list(
234 small_object_page_info* page) {
235 if (page->prev_page) {
236 page->prev_page->next_page = page->next_page;
237 }
238 if (page->next_page) {
239 page->next_page->prev_page = page->prev_page;
240 }
241 if (page_list_ == page) {
242 page_list_ = page->next_page;
243 }
244 page->prev_page = nullptr;
245 page->next_page = nullptr;
246}
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700247
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700248void LinkerMemoryAllocator::initialize_allocators() {
249 if (allocators_ != nullptr) {
250 return;
251 }
252
253 LinkerSmallObjectAllocator* allocators =
254 reinterpret_cast<LinkerSmallObjectAllocator*>(allocators_buf_);
255
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700256 for (size_t i = 0; i < kSmallObjectAllocatorsCount; ++i) {
257 uint32_t type = i + kSmallObjectMinSizeLog2;
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700258 new (allocators + i) LinkerSmallObjectAllocator(type, 1 << type);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700259 }
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700260
261 allocators_ = allocators;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700262}
263
264void* LinkerMemoryAllocator::alloc_mmap(size_t size) {
265 size_t allocated_size = PAGE_END(size + sizeof(page_info));
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700266 void* map_ptr = mmap(nullptr, allocated_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
267 -1, 0);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700268
269 if (map_ptr == MAP_FAILED) {
Elliott Hughes7b0af7a2017-09-15 16:09:22 -0700270 async_safe_fatal("mmap failed: %s", strerror(errno));
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700271 }
272
273 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, allocated_size, "linker_alloc_lob");
274
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700275 page_info* info = reinterpret_cast<page_info*>(map_ptr);
276 memcpy(info->signature, kSignature, sizeof(kSignature));
277 info->type = kLargeObject;
278 info->allocated_size = allocated_size;
279
280 return info + 1;
281}
282
283void* LinkerMemoryAllocator::alloc(size_t size) {
284 // treat alloc(0) as alloc(1)
285 if (size == 0) {
286 size = 1;
287 }
288
289 if (size > kSmallObjectMaxSize) {
290 return alloc_mmap(size);
291 }
292
293 uint16_t log2_size = log2(size);
294
295 if (log2_size < kSmallObjectMinSizeLog2) {
296 log2_size = kSmallObjectMinSizeLog2;
297 }
298
299 return get_small_object_allocator(log2_size)->alloc();
300}
301
302page_info* LinkerMemoryAllocator::get_page_info(void* ptr) {
303 page_info* info = reinterpret_cast<page_info*>(PAGE_START(reinterpret_cast<size_t>(ptr)));
304 if (memcmp(info->signature, kSignature, sizeof(kSignature)) != 0) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700305 async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700306 }
307
308 return info;
309}
310
311void* LinkerMemoryAllocator::realloc(void* ptr, size_t size) {
312 if (ptr == nullptr) {
313 return alloc(size);
314 }
315
316 if (size == 0) {
317 free(ptr);
318 return nullptr;
319 }
320
321 page_info* info = get_page_info(ptr);
322
323 size_t old_size = 0;
324
325 if (info->type == kLargeObject) {
326 old_size = info->allocated_size - sizeof(page_info);
327 } else {
328 LinkerSmallObjectAllocator* allocator = get_small_object_allocator(info->type);
329 if (allocator != info->allocator_addr) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700330 async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700331 }
332
333 old_size = allocator->get_block_size();
334 }
335
336 if (old_size < size) {
337 void *result = alloc(size);
338 memcpy(result, ptr, old_size);
339 free(ptr);
340 return result;
341 }
342
343 return ptr;
344}
345
346void LinkerMemoryAllocator::free(void* ptr) {
347 if (ptr == nullptr) {
348 return;
349 }
350
351 page_info* info = get_page_info(ptr);
352
353 if (info->type == kLargeObject) {
354 munmap(info, info->allocated_size);
355 } else {
356 LinkerSmallObjectAllocator* allocator = get_small_object_allocator(info->type);
357 if (allocator != info->allocator_addr) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700358 async_safe_fatal("invalid pointer %p (invalid allocator address for the page)", ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700359 }
360
361 allocator->free(ptr);
362 }
363}
364
365LinkerSmallObjectAllocator* LinkerMemoryAllocator::get_small_object_allocator(uint32_t type) {
366 if (type < kSmallObjectMinSizeLog2 || type > kSmallObjectMaxSizeLog2) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700367 async_safe_fatal("invalid type: %u", type);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700368 }
369
Dimitry Ivanov65707b62016-07-29 13:25:33 -0700370 initialize_allocators();
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700371 return &allocators_[type - kSmallObjectMinSizeLog2];
372}