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 3903505 : static inline size_t align_up(size_t x, size_t align)
37 : {
38 3903505 : return (x + align - 1) & ~(align - 1);
39 : }
40 :
41 : /*******************************************************************************/
42 : // Implementation: Arena
43 :
44 6931 : Arena::Arena(void *base_in, size_t size_in, size_t alignment_in):
45 3465 : base(base_in), end(static_cast<char*>(base_in) + size_in), alignment(alignment_in)
46 3466 : {
47 : // Start with one free chunk that covers the entire arena
48 3465 : auto it = size_to_free_chunk.emplace(size_in, base);
49 3465 : chunks_free.emplace(base, it);
50 3465 : chunks_free_end.emplace(static_cast<char*>(base) + size_in, it);
51 3466 : }
52 :
53 22 : Arena::~Arena()
54 22 : {
55 22 : }
56 :
57 3900027 : void* Arena::alloc(size_t size)
58 : {
59 : // Round to next multiple of alignment
60 3900027 : size = align_up(size, alignment);
61 :
62 : // Don't handle zero-sized chunks
63 3900027 : 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 3900025 : auto size_ptr_it = size_to_free_chunk.lower_bound(size);
71 3900025 : 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 3899407 : const size_t size_remaining = size_ptr_it->first - size;
76 3899407 : char* const free_chunk = static_cast<char*>(size_ptr_it->second);
77 3899407 : auto allocated = chunks_used.emplace(free_chunk + size_remaining, size).first;
78 3899407 : chunks_free_end.erase(free_chunk + size_ptr_it->first);
79 3899407 : if (size_ptr_it->first == size) {
80 : // whole chunk is used up
81 1700269 : chunks_free.erase(size_ptr_it->second);
82 1700269 : } else {
83 : // still some memory left in the chunk
84 2199138 : auto it_remaining = size_to_free_chunk.emplace(size_remaining, size_ptr_it->second);
85 2199138 : chunks_free[size_ptr_it->second] = it_remaining;
86 2199138 : chunks_free_end.emplace(free_chunk + size_remaining, it_remaining);
87 : }
88 3899407 : size_to_free_chunk.erase(size_ptr_it);
89 :
90 3899407 : return allocated->first;
91 3900027 : }
92 :
93 3899338 : void Arena::free(void *ptr)
94 : {
95 : // Freeing the nullptr pointer is OK.
96 3899338 : if (ptr == nullptr) {
97 3373 : return;
98 : }
99 :
100 : // Remove chunk from used map
101 3895965 : auto i = chunks_used.find(ptr);
102 3895965 : if (i == chunks_used.end()) {
103 2 : throw std::runtime_error("Arena: invalid or double free");
104 : }
105 3895963 : auto freed = std::make_pair(static_cast<char*>(i->first), i->second);
106 3895963 : chunks_used.erase(i);
107 :
108 : // coalesce freed with previous chunk
109 3895963 : auto prev = chunks_free_end.find(freed.first);
110 3895963 : if (prev != chunks_free_end.end()) {
111 1514840 : freed.first -= prev->second->first;
112 1514840 : freed.second += prev->second->first;
113 1514840 : size_to_free_chunk.erase(prev->second);
114 1514840 : chunks_free_end.erase(prev);
115 1514840 : }
116 :
117 : // coalesce freed with chunk after freed
118 3895963 : auto next = chunks_free.find(freed.first + freed.second);
119 3895963 : if (next != chunks_free.end()) {
120 680854 : freed.second += next->second->first;
121 680854 : size_to_free_chunk.erase(next->second);
122 680854 : chunks_free.erase(next);
123 680854 : }
124 :
125 : // Add/set space with coalesced free chunk
126 3895963 : auto it = size_to_free_chunk.emplace(freed.second, freed.first);
127 3895963 : chunks_free[freed.first] = it;
128 3895963 : chunks_free_end[freed.first + freed.second] = it;
129 3899336 : }
130 :
131 35 : Arena::Stats Arena::stats() const
132 : {
133 35 : Arena::Stats r{ 0, 0, 0, chunks_used.size(), chunks_free.size() };
134 1081 : for (const auto& chunk: chunks_used)
135 1046 : r.used += chunk.second;
136 72 : for (const auto& chunk: chunks_free)
137 37 : r.free += chunk.second->first;
138 35 : r.total = r.used + r.free;
139 35 : 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 6922 : PosixLockedPageAllocator::PosixLockedPageAllocator()
233 6922 : {
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 3461 : page_size = sysconf(_SC_PAGESIZE);
239 : #endif
240 6922 : }
241 :
242 3461 : void *PosixLockedPageAllocator::AllocateLocked(size_t len, bool *lockingSuccess)
243 : {
244 : void *addr;
245 3461 : len = align_up(len, page_size);
246 3461 : addr = mmap(nullptr, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
247 3461 : if (addr == MAP_FAILED) {
248 0 : return nullptr;
249 : }
250 3461 : if (addr) {
251 3461 : *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 3461 : }
258 3461 : return addr;
259 3461 : }
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 3461 : size_t PosixLockedPageAllocator::GetLimit()
268 : {
269 : #ifdef RLIMIT_MEMLOCK
270 : struct rlimit rlim;
271 3461 : if (getrlimit(RLIMIT_MEMLOCK, &rlim) == 0) {
272 3461 : if (rlim.rlim_cur != RLIM_INFINITY) {
273 0 : return rlim.rlim_cur;
274 : }
275 3461 : }
276 : #endif
277 3461 : return std::numeric_limits<size_t>::max();
278 3461 : }
279 : #endif
280 :
281 : /*******************************************************************************/
282 : // Implementation: LockedPool
283 :
284 6925 : LockedPool::LockedPool(std::unique_ptr<LockedPageAllocator> allocator_in, LockingFailed_Callback lf_cb_in)
285 6924 : : allocator(std::move(allocator_in)), lf_cb(lf_cb_in)
286 1 : {
287 1 : }
288 :
289 19 : LockedPool::~LockedPool() = default;
290 :
291 3894176 : void* LockedPool::alloc(size_t size)
292 : {
293 3894176 : std::lock_guard<std::mutex> lock(mutex);
294 :
295 : // Don't handle impossible sizes
296 3894176 : if (size == 0 || size > ARENA_SIZE)
297 2 : return nullptr;
298 :
299 : // Try allocating from each current arena
300 3894183 : for (auto &arena: arenas) {
301 3890718 : void *addr = arena.alloc(size);
302 3890718 : if (addr) {
303 3890709 : return addr;
304 : }
305 : }
306 : // If that fails, create a new one
307 3465 : if (new_arena(ARENA_SIZE, ARENA_ALIGN)) {
308 3464 : return arenas.back().alloc(size);
309 : }
310 1 : return nullptr;
311 3894176 : }
312 :
313 3890729 : void LockedPool::free(void *ptr)
314 : {
315 3890729 : 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 3890735 : for (auto &arena: arenas) {
319 3890736 : if (arena.addressInArena(ptr)) {
320 3890730 : arena.free(ptr);
321 : return;
322 : }
323 : }
324 0 : throw std::runtime_error("LockedPool: invalid address not pointing to any arena");
325 3890731 : }
326 :
327 15 : LockedPool::Stats LockedPool::stats() const
328 : {
329 15 : std::lock_guard<std::mutex> lock(mutex);
330 15 : LockedPool::Stats r{0, 0, 0, cumulative_bytes_locked, 0, 0};
331 30 : for (const auto &arena: arenas) {
332 15 : Arena::Stats i = arena.stats();
333 15 : r.used += i.used;
334 15 : r.free += i.free;
335 15 : r.total += i.total;
336 15 : r.chunks_used += i.chunks_used;
337 15 : r.chunks_free += i.chunks_free;
338 : }
339 : return r;
340 15 : }
341 :
342 3465 : 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 3465 : if (arenas.empty()) {
350 3462 : size_t limit = allocator->GetLimit();
351 3462 : if (limit > 0) {
352 3462 : size = std::min(size, limit);
353 3462 : }
354 3462 : }
355 3465 : void *addr = allocator->AllocateLocked(size, &locked);
356 3465 : if (!addr) {
357 1 : return false;
358 : }
359 3464 : if (locked) {
360 3462 : cumulative_bytes_locked += size;
361 3464 : } 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 3464 : arenas.emplace_back(allocator.get(), addr, size, align);
368 3464 : return true;
369 3465 : }
370 :
371 6928 : LockedPool::LockedPageArena::LockedPageArena(LockedPageAllocator *allocator_in, void *base_in, size_t size_in, size_t align_in):
372 6928 : Arena(base_in, size_in, align_in), base(base_in), size(size_in), allocator(allocator_in)
373 6928 : {
374 6928 : }
375 40 : LockedPool::LockedPageArena::~LockedPageArena()
376 40 : {
377 20 : allocator->FreeLocked(base, size);
378 40 : }
379 :
380 : /*******************************************************************************/
381 : // Implementation: LockedPoolManager
382 : //
383 6922 : LockedPoolManager::LockedPoolManager(std::unique_ptr<LockedPageAllocator> allocator_in):
384 3461 : LockedPool(std::move(allocator_in), &LockedPoolManager::LockingFailed)
385 3461 : {
386 6922 : }
387 :
388 0 : bool LockedPoolManager::LockingFailed()
389 : {
390 : // TODO: log something but how? without including util.h
391 0 : return true;
392 : }
393 :
394 3461 : 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 3461 : std::unique_ptr<LockedPageAllocator> allocator{std::make_unique<PosixLockedPageAllocator>()};
405 : #endif
406 3461 : static LockedPoolManager instance(std::move(allocator));
407 3461 : LockedPoolManager::_instance = &instance;
408 3461 : }
|