Line data Source code
1 : // Copyright (c) 2016-2020 The Bitcoin Core developers
2 : // Distributed under the MIT software license, see the accompanying
3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 :
5 : #include <support/lockedpool.h>
6 : #include <support/cleanse.h>
7 :
8 : #if defined(HAVE_CONFIG_H)
9 : #include <config/bitcoin-config.h>
10 : #endif
11 :
12 : #ifdef WIN32
13 : #include <windows.h>
14 : #else
15 : #include <sys/mman.h> // for mmap
16 : #include <sys/resource.h> // for getrlimit
17 : #include <limits.h> // for PAGESIZE
18 : #include <unistd.h> // for sysconf
19 : #endif
20 :
21 : #include <algorithm>
22 : #include <limits>
23 : #include <stdexcept>
24 : #ifdef ARENA_DEBUG
25 : #include <iomanip>
26 : #include <iostream>
27 : #endif
28 : #include <utility>
29 :
30 : LockedPoolManager* LockedPoolManager::_instance = nullptr;
31 :
32 : /*******************************************************************************/
33 : // Utilities
34 : //
35 : /** Align up to power of 2 */
36 92593 : static inline size_t align_up(size_t x, size_t align)
37 : {
38 92593 : return (x + align - 1) & ~(align - 1);
39 : }
40 :
41 : /*******************************************************************************/
42 : // Implementation: Arena
43 :
44 335 : Arena::Arena(void *base_in, size_t size_in, size_t alignment_in):
45 167 : base(base_in), end(static_cast<char*>(base_in) + size_in), alignment(alignment_in)
46 168 : {
47 : // Start with one free chunk that covers the entire arena
48 167 : auto it = size_to_free_chunk.emplace(size_in, base);
49 167 : chunks_free.emplace(base, it);
50 167 : chunks_free_end.emplace(static_cast<char*>(base) + size_in, it);
51 168 : }
52 :
53 22 : Arena::~Arena()
54 22 : {
55 22 : }
56 :
57 92413 : void* Arena::alloc(size_t size)
58 : {
59 : // Round to next multiple of alignment
60 92413 : size = align_up(size, alignment);
61 :
62 : // Don't handle zero-sized chunks
63 92413 : if (size == 0)
64 2 : return nullptr;
65 :
66 : // Pick a large enough free-chunk. Returns an iterator pointing to the first element that is not less than key.
67 : // This allocation strategy is best-fit. According to "Dynamic Storage Allocation: A Survey and Critical Review",
68 : // Wilson et. al. 1995, https://www.scs.stanford.edu/14wi-cs140/sched/readings/wilson.pdf, best-fit and first-fit
69 : // policies seem to work well in practice.
70 92411 : auto size_ptr_it = size_to_free_chunk.lower_bound(size);
71 92411 : if (size_ptr_it == size_to_free_chunk.end())
72 618 : return nullptr;
73 :
74 : // Create the used-chunk, taking its space from the end of the free-chunk
75 91793 : const size_t size_remaining = size_ptr_it->first - size;
76 91793 : char* const free_chunk = static_cast<char*>(size_ptr_it->second);
77 91793 : auto allocated = chunks_used.emplace(free_chunk + size_remaining, size).first;
78 91793 : chunks_free_end.erase(free_chunk + size_ptr_it->first);
79 91793 : if (size_ptr_it->first == size) {
80 : // whole chunk is used up
81 29045 : chunks_free.erase(size_ptr_it->second);
82 29045 : } else {
83 : // still some memory left in the chunk
84 62748 : auto it_remaining = size_to_free_chunk.emplace(size_remaining, size_ptr_it->second);
85 62748 : chunks_free[size_ptr_it->second] = it_remaining;
86 62748 : chunks_free_end.emplace(free_chunk + size_remaining, it_remaining);
87 : }
88 91793 : size_to_free_chunk.erase(size_ptr_it);
89 :
90 91793 : return allocated->first;
91 92413 : }
92 :
93 95022 : void Arena::free(void *ptr)
94 : {
95 : // Freeing the nullptr pointer is OK.
96 95022 : if (ptr == nullptr) {
97 3373 : return;
98 : }
99 :
100 : // Remove chunk from used map
101 91649 : auto i = chunks_used.find(ptr);
102 91649 : if (i == chunks_used.end()) {
103 2 : throw std::runtime_error("Arena: invalid or double free");
104 : }
105 91647 : auto freed = std::make_pair(static_cast<char*>(i->first), i->second);
106 91647 : chunks_used.erase(i);
107 :
108 : // coalesce freed with previous chunk
109 91647 : auto prev = chunks_free_end.find(freed.first);
110 91647 : if (prev != chunks_free_end.end()) {
111 52853 : freed.first -= prev->second->first;
112 52853 : freed.second += prev->second->first;
113 52853 : size_to_free_chunk.erase(prev->second);
114 52853 : chunks_free_end.erase(prev);
115 52853 : }
116 :
117 : // coalesce freed with chunk after freed
118 91647 : auto next = chunks_free.find(freed.first + freed.second);
119 91647 : if (next != chunks_free.end()) {
120 9749 : freed.second += next->second->first;
121 9749 : size_to_free_chunk.erase(next->second);
122 9749 : chunks_free.erase(next);
123 9749 : }
124 :
125 : // Add/set space with coalesced free chunk
126 91647 : auto it = size_to_free_chunk.emplace(freed.second, freed.first);
127 91647 : chunks_free[freed.first] = it;
128 91647 : chunks_free_end[freed.first + freed.second] = it;
129 95020 : }
130 :
131 33 : Arena::Stats Arena::stats() const
132 : {
133 33 : Arena::Stats r{ 0, 0, 0, chunks_used.size(), chunks_free.size() };
134 1075 : for (const auto& chunk: chunks_used)
135 1042 : r.used += chunk.second;
136 68 : for (const auto& chunk: chunks_free)
137 35 : r.free += chunk.second->first;
138 33 : r.total = r.used + r.free;
139 33 : return r;
140 : }
141 :
142 : #ifdef ARENA_DEBUG
143 : static void printchunk(void* base, size_t sz, bool used) {
144 : std::cout <<
145 : "0x" << std::hex << std::setw(16) << std::setfill('0') << base <<
146 : " 0x" << std::hex << std::setw(16) << std::setfill('0') << sz <<
147 : " 0x" << used << std::endl;
148 : }
149 : void Arena::walk() const
150 : {
151 : for (const auto& chunk: chunks_used)
152 : printchunk(chunk.first, chunk.second, true);
153 : std::cout << std::endl;
154 : for (const auto& chunk: chunks_free)
155 : printchunk(chunk.first, chunk.second->first, false);
156 : std::cout << std::endl;
157 : }
158 : #endif
159 :
160 : /*******************************************************************************/
161 : // Implementation: Win32LockedPageAllocator
162 :
163 : #ifdef WIN32
164 : /** LockedPageAllocator specialized for Windows.
165 : */
166 : class Win32LockedPageAllocator: public LockedPageAllocator
167 : {
168 : public:
169 : Win32LockedPageAllocator();
170 : void* AllocateLocked(size_t len, bool *lockingSuccess) override;
171 : void FreeLocked(void* addr, size_t len) override;
172 : size_t GetLimit() override;
173 : private:
174 : size_t page_size;
175 : };
176 :
177 : Win32LockedPageAllocator::Win32LockedPageAllocator()
178 : {
179 : // Determine system page size in bytes
180 : SYSTEM_INFO sSysInfo;
181 : GetSystemInfo(&sSysInfo);
182 : page_size = sSysInfo.dwPageSize;
183 : }
184 : void *Win32LockedPageAllocator::AllocateLocked(size_t len, bool *lockingSuccess)
185 : {
186 : len = align_up(len, page_size);
187 : void *addr = VirtualAlloc(nullptr, len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
188 : if (addr) {
189 : // VirtualLock is used to attempt to keep keying material out of swap. Note
190 : // that it does not provide this as a guarantee, but, in practice, memory
191 : // that has been VirtualLock'd almost never gets written to the pagefile
192 : // except in rare circumstances where memory is extremely low.
193 : *lockingSuccess = VirtualLock(const_cast<void*>(addr), len) != 0;
194 : }
195 : return addr;
196 : }
197 : void Win32LockedPageAllocator::FreeLocked(void* addr, size_t len)
198 : {
199 : len = align_up(len, page_size);
200 : memory_cleanse(addr, len);
201 : VirtualUnlock(const_cast<void*>(addr), len);
202 : }
203 :
204 : size_t Win32LockedPageAllocator::GetLimit()
205 : {
206 : size_t min, max;
207 : if(GetProcessWorkingSetSize(GetCurrentProcess(), &min, &max) != 0) {
208 : return min;
209 : }
210 : return std::numeric_limits<size_t>::max();
211 : }
212 : #endif
213 :
214 : /*******************************************************************************/
215 : // Implementation: PosixLockedPageAllocator
216 :
217 : #ifndef WIN32
218 : /** LockedPageAllocator specialized for OSes that don't try to be
219 : * special snowflakes.
220 : */
221 : class PosixLockedPageAllocator: public LockedPageAllocator
222 : {
223 : public:
224 : PosixLockedPageAllocator();
225 : void* AllocateLocked(size_t len, bool *lockingSuccess) override;
226 : void FreeLocked(void* addr, size_t len) override;
227 : size_t GetLimit() override;
228 : private:
229 : size_t page_size;
230 : };
231 :
232 326 : PosixLockedPageAllocator::PosixLockedPageAllocator()
233 326 : {
234 : // Determine system page size in bytes
235 : #if defined(PAGESIZE) // defined in limits.h
236 : page_size = PAGESIZE;
237 : #else // assume some POSIX OS
238 163 : page_size = sysconf(_SC_PAGESIZE);
239 : #endif
240 326 : }
241 :
242 163 : void *PosixLockedPageAllocator::AllocateLocked(size_t len, bool *lockingSuccess)
243 : {
244 : void *addr;
245 163 : len = align_up(len, page_size);
246 163 : addr = mmap(nullptr, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
247 163 : if (addr == MAP_FAILED) {
248 0 : return nullptr;
249 : }
250 163 : if (addr) {
251 163 : *lockingSuccess = mlock(addr, len) == 0;
252 : #if defined(MADV_DONTDUMP) // Linux
253 : madvise(addr, len, MADV_DONTDUMP);
254 : #elif defined(MADV_NOCORE) // FreeBSD
255 : madvise(addr, len, MADV_NOCORE);
256 : #endif
257 163 : }
258 163 : return addr;
259 163 : }
260 17 : void PosixLockedPageAllocator::FreeLocked(void* addr, size_t len)
261 : {
262 17 : len = align_up(len, page_size);
263 17 : memory_cleanse(addr, len);
264 17 : munlock(addr, len);
265 17 : munmap(addr, len);
266 17 : }
267 163 : size_t PosixLockedPageAllocator::GetLimit()
268 : {
269 : #ifdef RLIMIT_MEMLOCK
270 : struct rlimit rlim;
271 163 : if (getrlimit(RLIMIT_MEMLOCK, &rlim) == 0) {
272 163 : if (rlim.rlim_cur != RLIM_INFINITY) {
273 0 : return rlim.rlim_cur;
274 : }
275 163 : }
276 : #endif
277 163 : return std::numeric_limits<size_t>::max();
278 163 : }
279 : #endif
280 :
281 : /*******************************************************************************/
282 : // Implementation: LockedPool
283 :
284 329 : LockedPool::LockedPool(std::unique_ptr<LockedPageAllocator> allocator_in, LockingFailed_Callback lf_cb_in)
285 328 : : allocator(std::move(allocator_in)), lf_cb(lf_cb_in)
286 1 : {
287 1 : }
288 :
289 19 : LockedPool::~LockedPool() = default;
290 :
291 86562 : void* LockedPool::alloc(size_t size)
292 : {
293 86562 : std::lock_guard<std::mutex> lock(mutex);
294 :
295 : // Don't handle impossible sizes
296 86562 : if (size == 0 || size > ARENA_SIZE)
297 2 : return nullptr;
298 :
299 : // Try allocating from each current arena
300 86569 : for (auto &arena: arenas) {
301 86402 : void *addr = arena.alloc(size);
302 86402 : if (addr) {
303 86393 : return addr;
304 : }
305 : }
306 : // If that fails, create a new one
307 167 : if (new_arena(ARENA_SIZE, ARENA_ALIGN)) {
308 166 : return arenas.back().alloc(size);
309 : }
310 1 : return nullptr;
311 86562 : }
312 :
313 86414 : void LockedPool::free(void *ptr)
314 : {
315 86414 : std::lock_guard<std::mutex> lock(mutex);
316 : // TODO we can do better than this linear search by keeping a map of arena
317 : // extents to arena, and looking up the address.
318 86420 : for (auto &arena: arenas) {
319 86420 : if (arena.addressInArena(ptr)) {
320 86414 : arena.free(ptr);
321 : return;
322 : }
323 : }
324 0 : throw std::runtime_error("LockedPool: invalid address not pointing to any arena");
325 86414 : }
326 :
327 13 : LockedPool::Stats LockedPool::stats() const
328 : {
329 13 : std::lock_guard<std::mutex> lock(mutex);
330 13 : LockedPool::Stats r{0, 0, 0, cumulative_bytes_locked, 0, 0};
331 26 : for (const auto &arena: arenas) {
332 13 : Arena::Stats i = arena.stats();
333 13 : r.used += i.used;
334 13 : r.free += i.free;
335 13 : r.total += i.total;
336 13 : r.chunks_used += i.chunks_used;
337 13 : r.chunks_free += i.chunks_free;
338 : }
339 : return r;
340 13 : }
341 :
342 167 : bool LockedPool::new_arena(size_t size, size_t align)
343 : {
344 : bool locked;
345 : // If this is the first arena, handle this specially: Cap the upper size
346 : // by the process limit. This makes sure that the first arena will at least
347 : // be locked. An exception to this is if the process limit is 0:
348 : // in this case no memory can be locked at all so we'll skip past this logic.
349 167 : if (arenas.empty()) {
350 164 : size_t limit = allocator->GetLimit();
351 164 : if (limit > 0) {
352 164 : size = std::min(size, limit);
353 164 : }
354 164 : }
355 167 : void *addr = allocator->AllocateLocked(size, &locked);
356 167 : if (!addr) {
357 1 : return false;
358 : }
359 166 : if (locked) {
360 164 : cumulative_bytes_locked += size;
361 166 : } else if (lf_cb) { // Call the locking-failed callback if locking failed
362 0 : if (!lf_cb()) { // If the callback returns false, free the memory and fail, otherwise consider the user warned and proceed.
363 0 : allocator->FreeLocked(addr, size);
364 0 : return false;
365 : }
366 0 : }
367 166 : arenas.emplace_back(allocator.get(), addr, size, align);
368 166 : return true;
369 167 : }
370 :
371 332 : LockedPool::LockedPageArena::LockedPageArena(LockedPageAllocator *allocator_in, void *base_in, size_t size_in, size_t align_in):
372 332 : Arena(base_in, size_in, align_in), base(base_in), size(size_in), allocator(allocator_in)
373 332 : {
374 332 : }
375 40 : LockedPool::LockedPageArena::~LockedPageArena()
376 40 : {
377 20 : allocator->FreeLocked(base, size);
378 40 : }
379 :
380 : /*******************************************************************************/
381 : // Implementation: LockedPoolManager
382 : //
383 326 : LockedPoolManager::LockedPoolManager(std::unique_ptr<LockedPageAllocator> allocator_in):
384 163 : LockedPool(std::move(allocator_in), &LockedPoolManager::LockingFailed)
385 163 : {
386 326 : }
387 :
388 0 : bool LockedPoolManager::LockingFailed()
389 : {
390 : // TODO: log something but how? without including util.h
391 0 : return true;
392 : }
393 :
394 163 : void LockedPoolManager::CreateInstance()
395 : {
396 : // Using a local static instance guarantees that the object is initialized
397 : // when it's first needed and also deinitialized after all objects that use
398 : // it are done with it. I can think of one unlikely scenario where we may
399 : // have a static deinitialization order/problem, but the check in
400 : // LockedPoolManagerBase's destructor helps us detect if that ever happens.
401 : #ifdef WIN32
402 : std::unique_ptr<LockedPageAllocator> allocator{std::make_unique<Win32LockedPageAllocator>()};
403 : #else
404 163 : std::unique_ptr<LockedPageAllocator> allocator{std::make_unique<PosixLockedPageAllocator>()};
405 : #endif
406 163 : static LockedPoolManager instance(std::move(allocator));
407 163 : LockedPoolManager::_instance = &instance;
408 163 : }
|