/src/cpython/Objects/mimalloc/os.c
Line | Count | Source |
1 | | /* ---------------------------------------------------------------------------- |
2 | | Copyright (c) 2018-2025, Microsoft Research, Daan Leijen |
3 | | This is free software; you can redistribute it and/or modify it under the |
4 | | terms of the MIT license. A copy of the license can be found in the file |
5 | | "LICENSE" at the root of this distribution. |
6 | | -----------------------------------------------------------------------------*/ |
7 | | #include "mimalloc.h" |
8 | | #include "mimalloc/internal.h" |
9 | | #include "mimalloc/atomic.h" |
10 | | #include "mimalloc/prim.h" |
11 | | |
12 | 0 | #define mi_os_stat_increase(stat,amount) _mi_stat_increase(&_mi_stats_main.stat, amount) |
13 | 0 | #define mi_os_stat_decrease(stat,amount) _mi_stat_decrease(&_mi_stats_main.stat, amount) |
14 | 0 | #define mi_os_stat_counter_increase(stat,inc) _mi_stat_counter_increase(&_mi_stats_main.stat, inc) |
15 | | |
16 | | /* ----------------------------------------------------------- |
17 | | Initialization. |
18 | | ----------------------------------------------------------- */ |
19 | | #ifndef MI_DEFAULT_VIRTUAL_ADDRESS_BITS |
20 | | #if MI_INTPTR_SIZE < 8 |
21 | | #define MI_DEFAULT_VIRTUAL_ADDRESS_BITS 32 |
22 | | #else |
23 | | #define MI_DEFAULT_VIRTUAL_ADDRESS_BITS 48 |
24 | | #endif |
25 | | #endif |
26 | | |
27 | | #ifndef MI_DEFAULT_PHYSICAL_MEMORY_IN_KIB |
28 | | #if MI_INTPTR_SIZE < 8 |
29 | | #define MI_DEFAULT_PHYSICAL_MEMORY_IN_KIB 4*MI_MiB // 4 GiB |
30 | | #else |
31 | | #define MI_DEFAULT_PHYSICAL_MEMORY_IN_KIB 32*MI_MiB // 32 GiB |
32 | | #endif |
33 | | #endif |
34 | | |
35 | | static mi_os_mem_config_t mi_os_mem_config = { |
36 | | 4096, // page size |
37 | | 0, // large page size (usually 2MiB) |
38 | | 4096, // allocation granularity |
39 | | MI_DEFAULT_PHYSICAL_MEMORY_IN_KIB, |
40 | | MI_DEFAULT_VIRTUAL_ADDRESS_BITS, |
41 | | true, // has overcommit? (if true we use MAP_NORESERVE on mmap systems) |
42 | | false, // can we partially free allocated blocks? (on mmap systems we can free anywhere in a mapped range, but on Windows we must free the entire span) |
43 | | true // has virtual reserve? (if true we can reserve virtual address space without using commit or physical memory) |
44 | | }; |
45 | | |
46 | 0 | bool _mi_os_has_overcommit(void) { |
47 | 0 | return mi_os_mem_config.has_overcommit; |
48 | 0 | } |
49 | | |
50 | 0 | bool _mi_os_has_virtual_reserve(void) { |
51 | 0 | return mi_os_mem_config.has_virtual_reserve; |
52 | 0 | } |
53 | | |
54 | | |
55 | | // OS (small) page size |
56 | 0 | size_t _mi_os_page_size(void) { |
57 | 0 | return mi_os_mem_config.page_size; |
58 | 0 | } |
59 | | |
60 | | // if large OS pages are supported (2 or 4MiB), then return the size, otherwise return the small page size (4KiB) |
61 | 0 | size_t _mi_os_large_page_size(void) { |
62 | 0 | return (mi_os_mem_config.large_page_size != 0 ? mi_os_mem_config.large_page_size : _mi_os_page_size()); |
63 | 0 | } |
64 | | |
65 | 0 | bool _mi_os_canuse_large_page(size_t size, size_t alignment) { |
66 | | // if we have access, check the size and alignment requirements |
67 | 0 | if (mi_os_mem_config.large_page_size == 0) return false; |
68 | 0 | return ((size % mi_os_mem_config.large_page_size) == 0 && (alignment % mi_os_mem_config.large_page_size) == 0); |
69 | 0 | } |
70 | | |
71 | | // round to a good OS allocation size (bounded by max 12.5% waste) |
72 | 0 | size_t _mi_os_good_alloc_size(size_t size) { |
73 | 0 | size_t align_size; |
74 | 0 | if (size < 512*MI_KiB) align_size = _mi_os_page_size(); |
75 | 0 | else if (size < 2*MI_MiB) align_size = 64*MI_KiB; |
76 | 0 | else if (size < 8*MI_MiB) align_size = 256*MI_KiB; |
77 | 0 | else if (size < 32*MI_MiB) align_size = 1*MI_MiB; |
78 | 0 | else align_size = 4*MI_MiB; |
79 | 0 | if mi_unlikely(size >= (SIZE_MAX - align_size)) return size; // possible overflow? |
80 | 0 | return _mi_align_up(size, align_size); |
81 | 0 | } |
82 | | |
83 | 37 | void _mi_os_init(void) { |
84 | 37 | _mi_prim_mem_init(&mi_os_mem_config); |
85 | 37 | } |
86 | | |
87 | | |
88 | | /* ----------------------------------------------------------- |
89 | | Util |
90 | | -------------------------------------------------------------- */ |
91 | | bool _mi_os_decommit(void* addr, size_t size); |
92 | | bool _mi_os_commit(void* addr, size_t size, bool* is_zero); |
93 | | |
94 | | /* ----------------------------------------------------------- |
95 | | aligned hinting |
96 | | -------------------------------------------------------------- */ |
97 | | |
98 | | // On systems with enough virtual address bits, we can do efficient aligned allocation by using |
99 | | // the 2TiB to 30TiB area to allocate those. If we have at least 46 bits of virtual address |
100 | | // space (64TiB) we use this technique. (but see issue #939) |
101 | | #if (MI_INTPTR_SIZE >= 8) && !defined(MI_NO_ALIGNED_HINT) |
102 | | static mi_decl_cache_align _Atomic(uintptr_t)aligned_base; |
103 | | |
104 | | // Return a MI_SEGMENT_SIZE aligned address that is probably available. |
105 | | // If this returns NULL, the OS will determine the address but on some OS's that may not be |
106 | | // properly aligned which can be more costly as it needs to be adjusted afterwards. |
107 | | // In secure mode, for a size > 1GiB this always returns NULL in order to guarantee good ASLR randomization. |
108 | | // (otherwise an initial large allocation of say 2TiB has a 50% chance to include (known) addresses |
109 | | // in the middle of the 2TiB - 6TiB address range (see issue #372)) |
110 | | |
111 | 0 | #define MI_HINT_BASE ((uintptr_t)2 << 40) // 2TiB start |
112 | 0 | #define MI_HINT_AREA ((uintptr_t)4 << 40) // upto (2+4) 6TiB (since before win8 there is "only" 8TiB available to processes) |
113 | 0 | #define MI_HINT_MAX ((uintptr_t)30 << 40) // wrap after 30TiB (area after 32TiB is used for huge OS pages) |
114 | | |
115 | | void* _mi_os_get_aligned_hint(size_t try_alignment, size_t size) |
116 | 0 | { |
117 | 0 | if (try_alignment <= 1 || try_alignment > MI_SEGMENT_SIZE) return NULL; |
118 | 0 | if (mi_os_mem_config.virtual_address_bits < 46) return NULL; // < 64TiB virtual address space |
119 | 0 | size = _mi_align_up(size, MI_SEGMENT_SIZE); |
120 | | #if (MI_SECURE>0) |
121 | | if (size > 1*MI_GiB) return NULL; // guarantee the chance of fixed valid address is at most 1/(MI_HINT_AREA / 1<<30) = 1/4096. |
122 | | #endif |
123 | 0 | size += MI_SEGMENT_SIZE; // put in `MI_SEGMENT_SIZE` virtual gaps between hinted blocks; this splits VLA's but increases guarded areas. |
124 | |
|
125 | 0 | uintptr_t hint = mi_atomic_add_acq_rel(&aligned_base, size); |
126 | 0 | if (hint == 0 || hint > MI_HINT_MAX) { // wrap or initialize |
127 | 0 | uintptr_t init = MI_HINT_BASE; |
128 | 0 | #if (MI_SECURE>0 || MI_DEBUG==0) // security: randomize start of aligned allocations unless in debug mode |
129 | 0 | mi_heap_t* heap = mi_prim_get_default_heap(); |
130 | | // gh-123022: default heap may not be initialized in CPython in background threads |
131 | 0 | if (mi_heap_is_initialized(heap)) { |
132 | 0 | uintptr_t r = _mi_heap_random_next(heap); |
133 | 0 | init = init + ((MI_SEGMENT_SIZE * ((r>>17) & 0xFFFFF)) % MI_HINT_AREA); // (randomly 20 bits)*4MiB == 0 to 4TiB |
134 | 0 | } |
135 | 0 | #endif |
136 | 0 | uintptr_t expected = hint + size; |
137 | 0 | mi_atomic_cas_strong_acq_rel(&aligned_base, &expected, init); |
138 | 0 | hint = mi_atomic_add_acq_rel(&aligned_base, size); // this may still give 0 or > MI_HINT_MAX but that is ok, it is a hint after all |
139 | 0 | } |
140 | 0 | if (hint%try_alignment != 0) return NULL; |
141 | 0 | return (void*)hint; |
142 | 0 | } |
143 | | #else |
144 | | void* _mi_os_get_aligned_hint(size_t try_alignment, size_t size) { |
145 | | MI_UNUSED(try_alignment); MI_UNUSED(size); |
146 | | return NULL; |
147 | | } |
148 | | #endif |
149 | | |
150 | | /* ----------------------------------------------------------- |
151 | | Free memory |
152 | | -------------------------------------------------------------- */ |
153 | | |
154 | | static void mi_os_free_huge_os_pages(void* p, size_t size); |
155 | | |
156 | 0 | static void mi_os_prim_free(void* addr, size_t size, size_t commit_size) { |
157 | 0 | mi_assert_internal((size % _mi_os_page_size()) == 0); |
158 | 0 | if (addr == NULL) return; // || _mi_os_is_huge_reserved(addr) |
159 | 0 | int err = _mi_prim_free(addr, size); // allow size==0 (issue #1041) |
160 | 0 | if (err != 0) { |
161 | 0 | _mi_warning_message("unable to free OS memory (error: %d (0x%x), size: 0x%zx bytes, address: %p)\n", err, err, size, addr); |
162 | 0 | } |
163 | 0 | if (commit_size > 0) { |
164 | 0 | mi_os_stat_decrease(committed, commit_size); |
165 | 0 | } |
166 | 0 | mi_os_stat_decrease(reserved, size); |
167 | 0 | } |
168 | | |
169 | 0 | void _mi_os_free_ex(void* addr, size_t size, bool still_committed, mi_memid_t memid) { |
170 | 0 | if (mi_memkind_is_os(memid.memkind)) { |
171 | 0 | size_t csize = memid.mem.os.size; |
172 | 0 | if (csize==0) { csize = _mi_os_good_alloc_size(size); } |
173 | 0 | mi_assert_internal(csize >= size); |
174 | 0 | size_t commit_size = (still_committed ? csize : 0); |
175 | 0 | void* base = addr; |
176 | | // different base? (due to alignment) |
177 | 0 | if (memid.mem.os.base != base) { |
178 | 0 | mi_assert(memid.mem.os.base <= addr); |
179 | 0 | base = memid.mem.os.base; |
180 | 0 | const size_t diff = (uint8_t*)addr - (uint8_t*)memid.mem.os.base; |
181 | 0 | if (memid.mem.os.size==0) { |
182 | 0 | csize += diff; |
183 | 0 | } |
184 | 0 | if (still_committed) { |
185 | 0 | commit_size -= diff; // the (addr-base) part was already un-committed |
186 | 0 | } |
187 | 0 | } |
188 | | // free it |
189 | 0 | if (memid.memkind == MI_MEM_OS_HUGE) { |
190 | 0 | mi_assert(memid.is_pinned); |
191 | 0 | mi_os_free_huge_os_pages(base, csize); |
192 | 0 | } |
193 | 0 | else { |
194 | 0 | mi_os_prim_free(base, csize, (still_committed ? commit_size : 0)); |
195 | 0 | } |
196 | 0 | } |
197 | 0 | else { |
198 | | // nothing to do |
199 | 0 | mi_assert(memid.memkind < MI_MEM_OS); |
200 | 0 | } |
201 | 0 | } |
202 | | |
203 | 0 | void _mi_os_free(void* p, size_t size, mi_memid_t memid) { |
204 | 0 | _mi_os_free_ex(p, size, true, memid); |
205 | 0 | } |
206 | | |
207 | | |
208 | | /* ----------------------------------------------------------- |
209 | | Primitive allocation from the OS. |
210 | | -------------------------------------------------------------- */ |
211 | | |
212 | | // Note: the `try_alignment` is just a hint and the returned pointer is not guaranteed to be aligned. |
213 | | // Also `hint_addr` is a hint and may be ignored. |
214 | 0 | static void* mi_os_prim_alloc_at(void* hint_addr, size_t size, size_t try_alignment, bool commit, bool allow_large, bool* is_large, bool* is_zero) { |
215 | 0 | mi_assert_internal(size > 0 && (size % _mi_os_page_size()) == 0); |
216 | 0 | mi_assert_internal(is_zero != NULL); |
217 | 0 | mi_assert_internal(is_large != NULL); |
218 | 0 | if (size == 0) return NULL; |
219 | 0 | if (!commit) { allow_large = false; } |
220 | 0 | if (try_alignment == 0) { try_alignment = 1; } // avoid 0 to ensure there will be no divide by zero when aligning |
221 | | |
222 | | // try to align along large OS page size for larger allocations |
223 | 0 | const size_t large_page_size = mi_os_mem_config.large_page_size; |
224 | 0 | if (large_page_size > 0 && hint_addr == NULL && size >= 8*large_page_size && _mi_is_power_of_two(try_alignment) && try_alignment < large_page_size) { |
225 | 0 | try_alignment = large_page_size; |
226 | 0 | } |
227 | |
|
228 | 0 | *is_zero = false; |
229 | 0 | void* p = NULL; |
230 | 0 | int err = _mi_prim_alloc(hint_addr, size, try_alignment, commit, allow_large, is_large, is_zero, &p); |
231 | 0 | if (err != 0) { |
232 | 0 | _mi_warning_message("unable to allocate OS memory (error: %d (0x%x), addr: %p, size: 0x%zx bytes, align: 0x%zx, commit: %d, allow large: %d)\n", err, err, hint_addr, size, try_alignment, commit, allow_large); |
233 | 0 | } |
234 | | |
235 | | |
236 | |
|
237 | 0 | mi_os_stat_counter_increase(mmap_calls, 1); |
238 | 0 | if (p != NULL) { |
239 | 0 | mi_os_stat_increase(reserved, size); |
240 | 0 | if (commit) { |
241 | 0 | mi_os_stat_increase(committed, size); |
242 | | // seems needed for asan (or `mimalloc-test-api` fails) |
243 | | #ifdef MI_TRACK_ASAN |
244 | | if (*is_zero) { mi_track_mem_defined(p,size); } |
245 | | else { mi_track_mem_undefined(p,size); } |
246 | | #endif |
247 | 0 | } |
248 | 0 | } |
249 | 0 | return p; |
250 | 0 | } |
251 | | |
252 | 0 | static void* mi_os_prim_alloc(size_t size, size_t try_alignment, bool commit, bool allow_large, bool* is_large, bool* is_zero) { |
253 | 0 | return mi_os_prim_alloc_at(NULL, size, try_alignment, commit, allow_large, is_large, is_zero); |
254 | 0 | } |
255 | | |
256 | | |
257 | | // Primitive aligned allocation from the OS. |
258 | | // This function guarantees the allocated memory is aligned. |
259 | 0 | static void* mi_os_prim_alloc_aligned(size_t size, size_t alignment, bool commit, bool allow_large, mi_memid_t* memid) { |
260 | 0 | mi_assert_internal(memid!=NULL); |
261 | 0 | mi_assert_internal(alignment >= _mi_os_page_size() && ((alignment & (alignment - 1)) == 0)); |
262 | 0 | mi_assert_internal(size > 0 && (size % _mi_os_page_size()) == 0); |
263 | 0 | _mi_memzero(memid,sizeof(*memid)); |
264 | 0 | if (!commit) allow_large = false; |
265 | 0 | if (!(alignment >= _mi_os_page_size() && ((alignment & (alignment - 1)) == 0))) return NULL; |
266 | 0 | size = _mi_align_up(size, _mi_os_page_size()); |
267 | | |
268 | | // try first with a requested alignment hint (this will usually be aligned directly on Win 10+ or BSD) |
269 | 0 | bool os_is_large = false; |
270 | 0 | bool os_is_zero = false; |
271 | 0 | void* os_base = NULL; |
272 | 0 | size_t os_size = size; |
273 | 0 | void* p = mi_os_prim_alloc(size, alignment, commit, allow_large, &os_is_large, &os_is_zero); |
274 | 0 | if (p == NULL) return NULL; |
275 | | |
276 | | // aligned already? |
277 | 0 | if (((uintptr_t)p % alignment) == 0) { |
278 | 0 | os_base = p; |
279 | 0 | } |
280 | 0 | else { |
281 | | // if not aligned, free it, overallocate, and unmap around it |
282 | 0 | #if !MI_TRACK_ASAN |
283 | | // CPython: downgraded from _mi_warning_message, the warning is noisy in the test suite (gh-112027) |
284 | 0 | _mi_verbose_message("unable to allocate aligned OS memory directly, fall back to over-allocation (size: 0x%zx bytes, address: %p, alignment: 0x%zx, commit: %d)\n", size, p, alignment, commit); |
285 | 0 | #endif |
286 | 0 | if (p != NULL) { mi_os_prim_free(p, size, (commit ? size : 0)); } |
287 | 0 | if (size >= (SIZE_MAX - alignment)) return NULL; // overflow |
288 | 0 | const size_t over_size = size + alignment; |
289 | |
|
290 | 0 | if (!mi_os_mem_config.has_partial_free) { // win32 virtualAlloc cannot free parts of an allocated block |
291 | | // over-allocate uncommitted (virtual) memory |
292 | 0 | p = mi_os_prim_alloc(over_size, 1 /*alignment*/, false /* commit? */, false /* allow_large */, &os_is_large, &os_is_zero); |
293 | 0 | if (p == NULL) return NULL; |
294 | | |
295 | | // set p to the aligned part in the full region |
296 | | // note: this is dangerous on Windows as VirtualFree needs the actual base pointer |
297 | | // this is handled though by having the `base` field in the memid's |
298 | 0 | os_base = p; // remember the base |
299 | 0 | os_size = size; // not over_size as otherwise we over-decrement commit stats on free |
300 | 0 | p = _mi_align_up_ptr(p, alignment); |
301 | | |
302 | | // explicitly commit only the aligned part |
303 | 0 | if (commit) { |
304 | 0 | if (!_mi_os_commit(p, size, NULL)) { |
305 | 0 | mi_os_prim_free(os_base, over_size, 0); |
306 | 0 | return NULL; |
307 | 0 | } |
308 | 0 | } |
309 | 0 | } |
310 | 0 | else { // mmap can free inside an allocation |
311 | | // overallocate... |
312 | 0 | p = mi_os_prim_alloc(over_size, 1, commit, false, &os_is_large, &os_is_zero); |
313 | 0 | if (p == NULL) return NULL; |
314 | | |
315 | | // and selectively unmap parts around the over-allocated area. |
316 | 0 | void* const aligned_p = _mi_align_up_ptr(p, alignment); |
317 | 0 | const size_t pre_size = (uint8_t*)aligned_p - (uint8_t*)p; |
318 | 0 | const size_t mid_size = _mi_align_up(size, _mi_os_page_size()); |
319 | 0 | const size_t post_size = over_size - pre_size - mid_size; |
320 | 0 | mi_assert_internal(pre_size < over_size&& post_size < over_size&& mid_size >= size); |
321 | 0 | if (pre_size > 0) { mi_os_prim_free(p, pre_size, (commit ? pre_size : 0)); } |
322 | 0 | if (post_size > 0) { mi_os_prim_free((uint8_t*)aligned_p + mid_size, post_size, (commit ? post_size : 0)); } |
323 | | // we can return the aligned pointer on `mmap` systems |
324 | 0 | p = aligned_p; |
325 | 0 | os_base = aligned_p; // since we freed the pre part, `*base == p`. |
326 | 0 | os_size = mid_size; |
327 | 0 | } |
328 | 0 | } |
329 | | |
330 | 0 | mi_assert_internal(p != NULL && os_base != NULL && _mi_is_aligned(p,alignment)); |
331 | 0 | mi_assert_internal(os_base <= p && size <= os_size); |
332 | 0 | *memid = _mi_memid_create_os(os_base,os_size,commit,os_is_zero,os_is_large); |
333 | 0 | return p; |
334 | 0 | } |
335 | | |
336 | | |
337 | | /* ----------------------------------------------------------- |
338 | | OS API: alloc and alloc_aligned |
339 | | ----------------------------------------------------------- */ |
340 | | |
341 | 0 | void* _mi_os_alloc(size_t size, mi_memid_t* memid) { |
342 | 0 | *memid = _mi_memid_none(); |
343 | 0 | if (size == 0) return NULL; |
344 | 0 | size = _mi_os_good_alloc_size(size); |
345 | 0 | bool os_is_large = false; |
346 | 0 | bool os_is_zero = false; |
347 | 0 | void* p = mi_os_prim_alloc(size, 0, true, false, &os_is_large, &os_is_zero); |
348 | 0 | if (p == NULL) return NULL; |
349 | | |
350 | 0 | *memid = _mi_memid_create_os(p, size, true, os_is_zero, os_is_large); |
351 | 0 | mi_assert_internal(memid->mem.os.size >= size); |
352 | 0 | mi_assert_internal(memid->initially_committed); |
353 | 0 | return p; |
354 | 0 | } |
355 | | |
356 | | void* _mi_os_alloc_aligned(size_t size, size_t alignment, bool commit, bool allow_large, mi_memid_t* memid) |
357 | 0 | { |
358 | 0 | MI_UNUSED(&_mi_os_get_aligned_hint); // suppress unused warnings |
359 | 0 | *memid = _mi_memid_none(); |
360 | 0 | if (size == 0) return NULL; |
361 | 0 | size = _mi_os_good_alloc_size(size); |
362 | 0 | alignment = _mi_align_up(alignment, _mi_os_page_size()); |
363 | |
|
364 | 0 | void* p = mi_os_prim_alloc_aligned(size, alignment, commit, allow_large, memid ); |
365 | 0 | if (p == NULL) return NULL; |
366 | | |
367 | 0 | mi_assert_internal(memid->mem.os.size >= size); |
368 | 0 | mi_assert_internal(_mi_is_aligned(p,alignment)); |
369 | 0 | if (commit) { mi_assert_internal(memid->initially_committed); } |
370 | 0 | return p; |
371 | 0 | } |
372 | | |
373 | | |
374 | 0 | mi_decl_nodiscard static void* mi_os_ensure_zero(void* p, size_t size, mi_memid_t* memid) { |
375 | 0 | if (p==NULL || size==0) return p; |
376 | | // ensure committed |
377 | 0 | if (!memid->initially_committed) { |
378 | 0 | bool is_zero = false; |
379 | 0 | if (!_mi_os_commit(p, size, &is_zero)) { |
380 | 0 | _mi_os_free(p, size, *memid); |
381 | 0 | return NULL; |
382 | 0 | } |
383 | 0 | memid->initially_committed = true; |
384 | 0 | } |
385 | | // ensure zero'd |
386 | 0 | if (memid->initially_zero) return p; |
387 | 0 | _mi_memzero_aligned(p,size); |
388 | 0 | memid->initially_zero = true; |
389 | 0 | return p; |
390 | 0 | } |
391 | | |
392 | 0 | void* _mi_os_zalloc(size_t size, mi_memid_t* memid) { |
393 | 0 | void* p = _mi_os_alloc(size,memid); |
394 | 0 | return mi_os_ensure_zero(p, size, memid); |
395 | 0 | } |
396 | | |
397 | | /* ----------------------------------------------------------- |
398 | | OS aligned allocation with an offset. This is used |
399 | | for large alignments > MI_BLOCK_ALIGNMENT_MAX. We use a large mimalloc |
400 | | page where the object can be aligned at an offset from the start of the segment. |
401 | | As we may need to overallocate, we need to free such pointers using `mi_free_aligned` |
402 | | to use the actual start of the memory region. |
403 | | ----------------------------------------------------------- */ |
404 | | |
405 | 0 | void* _mi_os_alloc_aligned_at_offset(size_t size, size_t alignment, size_t offset, bool commit, bool allow_large, mi_memid_t* memid) { |
406 | 0 | mi_assert(offset <= MI_SEGMENT_SIZE); |
407 | 0 | mi_assert(offset <= size); |
408 | 0 | mi_assert((alignment % _mi_os_page_size()) == 0); |
409 | 0 | *memid = _mi_memid_none(); |
410 | 0 | if (offset > MI_SEGMENT_SIZE || offset > size) return NULL; |
411 | 0 | if (offset == 0) { |
412 | | // regular aligned allocation |
413 | 0 | return _mi_os_alloc_aligned(size, alignment, commit, allow_large, memid); |
414 | 0 | } |
415 | 0 | else { |
416 | | // overallocate to align at an offset |
417 | 0 | const size_t extra = _mi_align_up(offset, alignment) - offset; |
418 | 0 | if (size >= SIZE_MAX - extra) return NULL; // too large |
419 | 0 | const size_t oversize = size + extra; |
420 | 0 | void* const start = _mi_os_alloc_aligned(oversize, alignment, commit, allow_large, memid); |
421 | 0 | if (start == NULL) return NULL; |
422 | | |
423 | 0 | void* const p = (uint8_t*)start + extra; |
424 | 0 | mi_assert(_mi_is_aligned((uint8_t*)p + offset, alignment)); |
425 | | // decommit the overallocation at the start |
426 | | // note: this double counts the decommit when freeing `memid`. Should we keep commit size in the memid as well? |
427 | 0 | if (commit && extra >= _mi_os_page_size()) { |
428 | 0 | _mi_os_decommit(start, extra); |
429 | 0 | } |
430 | 0 | return p; |
431 | 0 | } |
432 | 0 | } |
433 | | |
434 | | /* ----------------------------------------------------------- |
435 | | OS memory API: reset, commit, decommit, protect, unprotect. |
436 | | ----------------------------------------------------------- */ |
437 | | |
438 | | // OS page align within a given area, either conservative (pages inside the area only), |
439 | | // or not (straddling pages outside the area is possible) |
440 | 0 | static void* mi_os_page_align_areax(bool conservative, void* addr, size_t size, size_t* newsize) { |
441 | 0 | mi_assert(addr != NULL && size > 0); |
442 | 0 | if (newsize != NULL) *newsize = 0; |
443 | 0 | if (size == 0 || addr == NULL) return NULL; |
444 | | |
445 | | // page align conservatively within the range |
446 | 0 | void* start = (conservative ? _mi_align_up_ptr(addr, _mi_os_page_size()) |
447 | 0 | : _mi_align_down_ptr(addr, _mi_os_page_size())); |
448 | 0 | void* end = (conservative ? _mi_align_down_ptr((uint8_t*)addr + size, _mi_os_page_size()) |
449 | 0 | : _mi_align_up_ptr((uint8_t*)addr + size, _mi_os_page_size())); |
450 | 0 | ptrdiff_t diff = (uint8_t*)end - (uint8_t*)start; |
451 | 0 | if (diff <= 0) return NULL; |
452 | | |
453 | 0 | mi_assert_internal((conservative && (size_t)diff <= size) || (!conservative && (size_t)diff >= size)); |
454 | 0 | if (newsize != NULL) *newsize = (size_t)diff; |
455 | 0 | return start; |
456 | 0 | } |
457 | | |
458 | 0 | static void* mi_os_page_align_area_conservative(void* addr, size_t size, size_t* newsize) { |
459 | 0 | return mi_os_page_align_areax(true, addr, size, newsize); |
460 | 0 | } |
461 | | |
462 | 0 | bool _mi_os_commit_ex(void* addr, size_t size, bool* is_zero, size_t stat_size) { |
463 | 0 | if (is_zero != NULL) { *is_zero = false; } |
464 | 0 | mi_os_stat_counter_increase(commit_calls, 1); |
465 | | |
466 | | // page align range |
467 | 0 | size_t csize; |
468 | 0 | void* start = mi_os_page_align_areax(false /* conservative? */, addr, size, &csize); |
469 | 0 | if (csize == 0) return true; |
470 | | |
471 | | // commit |
472 | 0 | bool os_is_zero = false; |
473 | 0 | int err = _mi_prim_commit(start, csize, &os_is_zero); |
474 | 0 | if (err != 0) { |
475 | 0 | _mi_warning_message("cannot commit OS memory (error: %d (0x%x), address: %p, size: 0x%zx bytes)\n", err, err, start, csize); |
476 | 0 | return false; |
477 | 0 | } |
478 | 0 | if (os_is_zero && is_zero != NULL) { |
479 | 0 | *is_zero = true; |
480 | 0 | mi_assert_expensive(mi_mem_is_zero(start, csize)); |
481 | 0 | } |
482 | | // note: the following seems required for asan (otherwise `mimalloc-test-stress` fails) |
483 | | #ifdef MI_TRACK_ASAN |
484 | | if (os_is_zero) { mi_track_mem_defined(start,csize); } |
485 | | else { mi_track_mem_undefined(start,csize); } |
486 | | #endif |
487 | 0 | mi_os_stat_increase(committed, stat_size); // use size for precise commit vs. decommit |
488 | 0 | return true; |
489 | 0 | } |
490 | | |
491 | 0 | bool _mi_os_commit(void* addr, size_t size, bool* is_zero) { |
492 | 0 | return _mi_os_commit_ex(addr, size, is_zero, size); |
493 | 0 | } |
494 | | |
495 | 0 | static bool mi_os_decommit_ex(void* addr, size_t size, bool* needs_recommit, size_t stat_size) { |
496 | 0 | mi_assert_internal(needs_recommit!=NULL); |
497 | | |
498 | | // page align |
499 | 0 | size_t csize; |
500 | 0 | void* start = mi_os_page_align_area_conservative(addr, size, &csize); |
501 | 0 | if (csize == 0) return true; |
502 | | |
503 | | // decommit |
504 | 0 | *needs_recommit = true; |
505 | 0 | int err = _mi_prim_decommit(start,csize,needs_recommit); |
506 | 0 | if (err != 0) { |
507 | 0 | _mi_warning_message("cannot decommit OS memory (error: %d (0x%x), address: %p, size: 0x%zx bytes)\n", err, err, start, csize); |
508 | 0 | } |
509 | 0 | else if (*needs_recommit) { |
510 | 0 | mi_os_stat_decrease(committed, stat_size); |
511 | 0 | } |
512 | 0 | mi_assert_internal(err == 0); |
513 | 0 | return (err == 0); |
514 | 0 | } |
515 | | |
516 | 0 | bool _mi_os_decommit(void* addr, size_t size) { |
517 | 0 | bool needs_recommit; |
518 | 0 | return mi_os_decommit_ex(addr, size, &needs_recommit, size); |
519 | 0 | } |
520 | | |
521 | | |
522 | | // Signal to the OS that the address range is no longer in use |
523 | | // but may be used later again. This will release physical memory |
524 | | // pages and reduce swapping while keeping the memory committed. |
525 | | // We page align to a conservative area inside the range to reset. |
526 | 0 | bool _mi_os_reset(void* addr, size_t size) { |
527 | | // page align conservatively within the range |
528 | 0 | size_t csize; |
529 | 0 | void* start = mi_os_page_align_area_conservative(addr, size, &csize); |
530 | 0 | if (csize == 0) return true; // || _mi_os_is_huge_reserved(addr) |
531 | 0 | mi_os_stat_counter_increase(reset, csize); |
532 | 0 | mi_os_stat_counter_increase(reset_calls, 1); |
533 | |
|
534 | | #if (MI_DEBUG>1) && !MI_SECURE && !MI_TRACK_ENABLED // && !MI_TSAN |
535 | | memset(start, 0, csize); // pretend it is eagerly reset |
536 | | #endif |
537 | |
|
538 | 0 | int err = _mi_prim_reset(start, csize); |
539 | 0 | if (err != 0) { |
540 | 0 | _mi_warning_message("cannot reset OS memory (error: %d (0x%x), address: %p, size: 0x%zx bytes)\n", err, err, start, csize); |
541 | 0 | } |
542 | 0 | return (err == 0); |
543 | 0 | } |
544 | | |
545 | | |
546 | 0 | void _mi_os_reuse( void* addr, size_t size ) { |
547 | | // page align conservatively within the range |
548 | 0 | size_t csize = 0; |
549 | 0 | void* const start = mi_os_page_align_area_conservative(addr, size, &csize); |
550 | 0 | if (csize == 0) return; |
551 | 0 | const int err = _mi_prim_reuse(start, csize); |
552 | 0 | if (err != 0) { |
553 | 0 | _mi_warning_message("cannot reuse OS memory (error: %d (0x%x), address: %p, size: 0x%zx bytes)\n", err, err, start, csize); |
554 | 0 | } |
555 | 0 | } |
556 | | |
557 | | // either resets or decommits memory, returns true if the memory needs |
558 | | // to be recommitted if it is to be re-used later on. |
559 | | bool _mi_os_purge_ex(void* p, size_t size, bool allow_reset, size_t stat_size) |
560 | 0 | { |
561 | 0 | if (mi_option_get(mi_option_purge_delay) < 0) return false; // is purging allowed? |
562 | 0 | mi_os_stat_counter_increase(purge_calls, 1); |
563 | 0 | mi_os_stat_counter_increase(purged, size); |
564 | |
|
565 | 0 | if (mi_option_is_enabled(mi_option_purge_decommits) && // should decommit? |
566 | 0 | !_mi_preloading()) // don't decommit during preloading (unsafe) |
567 | 0 | { |
568 | 0 | bool needs_recommit = true; |
569 | 0 | mi_os_decommit_ex(p, size, &needs_recommit, stat_size); |
570 | 0 | return needs_recommit; |
571 | 0 | } |
572 | 0 | else { |
573 | 0 | if (allow_reset) { // this can sometimes be not allowed if the range is not fully committed |
574 | 0 | _mi_os_reset(p, size); |
575 | 0 | } |
576 | 0 | return false; // needs no recommit |
577 | 0 | } |
578 | 0 | } |
579 | | |
580 | | // either resets or decommits memory, returns true if the memory needs |
581 | | // to be recommitted if it is to be re-used later on. |
582 | 0 | bool _mi_os_purge(void* p, size_t size) { |
583 | 0 | return _mi_os_purge_ex(p, size, true, size); |
584 | 0 | } |
585 | | |
586 | | // Protect a region in memory to be not accessible. |
587 | 0 | static bool mi_os_protectx(void* addr, size_t size, bool protect) { |
588 | | // page align conservatively within the range |
589 | 0 | size_t csize = 0; |
590 | 0 | void* start = mi_os_page_align_area_conservative(addr, size, &csize); |
591 | 0 | if (csize == 0) return false; |
592 | | /* |
593 | | if (_mi_os_is_huge_reserved(addr)) { |
594 | | _mi_warning_message("cannot mprotect memory allocated in huge OS pages\n"); |
595 | | } |
596 | | */ |
597 | 0 | int err = _mi_prim_protect(start,csize,protect); |
598 | 0 | if (err != 0) { |
599 | 0 | _mi_warning_message("cannot %s OS memory (error: %d (0x%x), address: %p, size: 0x%zx bytes)\n", (protect ? "protect" : "unprotect"), err, err, start, csize); |
600 | 0 | } |
601 | 0 | return (err == 0); |
602 | 0 | } |
603 | | |
604 | 0 | bool _mi_os_protect(void* addr, size_t size) { |
605 | 0 | return mi_os_protectx(addr, size, true); |
606 | 0 | } |
607 | | |
608 | 0 | bool _mi_os_unprotect(void* addr, size_t size) { |
609 | 0 | return mi_os_protectx(addr, size, false); |
610 | 0 | } |
611 | | |
612 | | |
613 | | |
614 | | /* ---------------------------------------------------------------------------- |
615 | | Support for allocating huge OS pages (1Gib) that are reserved up-front |
616 | | and possibly associated with a specific NUMA node. (use `numa_node>=0`) |
617 | | -----------------------------------------------------------------------------*/ |
618 | 0 | #define MI_HUGE_OS_PAGE_SIZE (MI_GiB) |
619 | | |
620 | | |
621 | | #if (MI_INTPTR_SIZE >= 8) |
622 | | // To ensure proper alignment, use our own area for huge OS pages |
623 | | static mi_decl_cache_align _Atomic(uintptr_t) mi_huge_start; // = 0 |
624 | | |
625 | | // Claim an aligned address range for huge pages |
626 | 0 | static uint8_t* mi_os_claim_huge_pages(size_t pages, size_t* total_size) { |
627 | 0 | if (total_size != NULL) *total_size = 0; |
628 | 0 | size_t size = 0; |
629 | 0 | if (mi_mul_overflow(pages,MI_HUGE_OS_PAGE_SIZE,&size)) { |
630 | 0 | _mi_warning_message("too many huge pages requested: %zu\n", pages); |
631 | 0 | return NULL; |
632 | 0 | } |
633 | | |
634 | 0 | uintptr_t start = 0; |
635 | 0 | uintptr_t end = 0; |
636 | 0 | uintptr_t huge_start = mi_atomic_load_relaxed(&mi_huge_start); |
637 | 0 | do { |
638 | 0 | start = huge_start; |
639 | 0 | if (start == 0) { |
640 | | // Initialize the start address after the 32TiB area |
641 | 0 | start = ((uintptr_t)32 << 40); // 32TiB virtual start address |
642 | 0 | #if (MI_SECURE>0 || MI_DEBUG==0) // security: randomize start of huge pages unless in debug mode |
643 | 0 | mi_heap_t* heap = mi_prim_get_default_heap(); |
644 | | // gh-123022: default heap may not be initialized in CPython in background threads |
645 | 0 | if (mi_heap_is_initialized(heap)) { |
646 | 0 | uintptr_t r = _mi_heap_random_next(heap); |
647 | 0 | start = start + ((uintptr_t)MI_HUGE_OS_PAGE_SIZE * ((r>>17) & 0x0FFF)); // (randomly 12bits)*1GiB == between 0 to 4TiB |
648 | 0 | } |
649 | 0 | #endif |
650 | 0 | } |
651 | 0 | end = start + size; |
652 | 0 | mi_assert_internal(end % MI_SEGMENT_SIZE == 0); |
653 | 0 | } while (!mi_atomic_cas_strong_acq_rel(&mi_huge_start, &huge_start, end)); |
654 | |
|
655 | 0 | if (total_size != NULL) *total_size = size; |
656 | 0 | return (uint8_t*)start; |
657 | 0 | } |
658 | | #else |
659 | | static uint8_t* mi_os_claim_huge_pages(size_t pages, size_t* total_size) { |
660 | | MI_UNUSED(pages); |
661 | | if (total_size != NULL) *total_size = 0; |
662 | | return NULL; |
663 | | } |
664 | | #endif |
665 | | |
666 | | // Allocate MI_SEGMENT_SIZE aligned huge pages |
667 | 0 | void* _mi_os_alloc_huge_os_pages(size_t pages, int numa_node, mi_msecs_t max_msecs, size_t* pages_reserved, size_t* psize, mi_memid_t* memid) { |
668 | 0 | *memid = _mi_memid_none(); |
669 | 0 | if (psize != NULL) *psize = 0; |
670 | 0 | if (pages_reserved != NULL) *pages_reserved = 0; |
671 | 0 | size_t size = 0; |
672 | 0 | uint8_t* const start = mi_os_claim_huge_pages(pages, &size); |
673 | 0 | if (start == NULL) return NULL; // or 32-bit systems |
674 | | |
675 | | // Allocate one page at the time but try to place them contiguously |
676 | | // We allocate one page at the time to be able to abort if it takes too long |
677 | | // or to at least allocate as many as available on the system. |
678 | 0 | mi_msecs_t start_t = _mi_clock_start(); |
679 | 0 | size_t page = 0; |
680 | 0 | bool all_zero = true; |
681 | 0 | while (page < pages) { |
682 | | // allocate a page |
683 | 0 | bool is_zero = false; |
684 | 0 | void* addr = start + (page * MI_HUGE_OS_PAGE_SIZE); |
685 | 0 | void* p = NULL; |
686 | 0 | int err = _mi_prim_alloc_huge_os_pages(addr, MI_HUGE_OS_PAGE_SIZE, numa_node, &is_zero, &p); |
687 | 0 | if (!is_zero) { all_zero = false; } |
688 | 0 | if (err != 0) { |
689 | 0 | _mi_warning_message("unable to allocate huge OS page (error: %d (0x%x), address: %p, size: %zx bytes)\n", err, err, addr, MI_HUGE_OS_PAGE_SIZE); |
690 | 0 | break; |
691 | 0 | } |
692 | | |
693 | | // Did we succeed at a contiguous address? |
694 | 0 | if (p != addr) { |
695 | | // no success, issue a warning and break |
696 | 0 | if (p != NULL) { |
697 | 0 | _mi_warning_message("could not allocate contiguous huge OS page %zu at %p\n", page, addr); |
698 | 0 | mi_os_prim_free(p, MI_HUGE_OS_PAGE_SIZE, MI_HUGE_OS_PAGE_SIZE); |
699 | 0 | } |
700 | 0 | break; |
701 | 0 | } |
702 | | |
703 | | // success, record it |
704 | 0 | page++; // increase before timeout check (see issue #711) |
705 | 0 | mi_os_stat_increase(committed, MI_HUGE_OS_PAGE_SIZE); |
706 | 0 | mi_os_stat_increase(reserved, MI_HUGE_OS_PAGE_SIZE); |
707 | | |
708 | | // check for timeout |
709 | 0 | if (max_msecs > 0) { |
710 | 0 | mi_msecs_t elapsed = _mi_clock_end(start_t); |
711 | 0 | if (page >= 1) { |
712 | 0 | mi_msecs_t estimate = ((elapsed / (page==0 ? 1 : page)) * pages); |
713 | 0 | if (estimate > 2*max_msecs) { // seems like we are going to timeout, break |
714 | 0 | elapsed = max_msecs + 1; |
715 | 0 | } |
716 | 0 | } |
717 | 0 | if (elapsed > max_msecs) { |
718 | 0 | _mi_warning_message("huge OS page allocation timed out (after allocating %zu page(s))\n", page); |
719 | 0 | break; |
720 | 0 | } |
721 | 0 | } |
722 | 0 | } |
723 | 0 | const size_t allocated = page * MI_HUGE_OS_PAGE_SIZE; |
724 | 0 | mi_assert_internal(allocated <= size); |
725 | 0 | if (pages_reserved != NULL) { *pages_reserved = page; } |
726 | 0 | if (psize != NULL) { *psize = allocated; } |
727 | 0 | if (page != 0) { |
728 | 0 | mi_assert(start != NULL); |
729 | 0 | *memid = _mi_memid_create_os(start, allocated, true /* is committed */, all_zero, true /* is_large */); |
730 | 0 | memid->memkind = MI_MEM_OS_HUGE; |
731 | 0 | mi_assert(memid->is_pinned); |
732 | | #ifdef MI_TRACK_ASAN |
733 | | if (all_zero) { mi_track_mem_defined(start,allocated); } |
734 | | #endif |
735 | 0 | } |
736 | 0 | return (page == 0 ? NULL : start); |
737 | 0 | } |
738 | | |
739 | | // free every huge page in a range individually (as we allocated per page) |
740 | | // note: needed with VirtualAlloc but could potentially be done in one go on mmap'd systems. |
741 | 0 | static void mi_os_free_huge_os_pages(void* p, size_t size) { |
742 | 0 | if (p==NULL || size==0) return; |
743 | 0 | uint8_t* base = (uint8_t*)p; |
744 | 0 | while (size >= MI_HUGE_OS_PAGE_SIZE) { |
745 | 0 | mi_os_prim_free(base, MI_HUGE_OS_PAGE_SIZE, MI_HUGE_OS_PAGE_SIZE); |
746 | 0 | size -= MI_HUGE_OS_PAGE_SIZE; |
747 | 0 | base += MI_HUGE_OS_PAGE_SIZE; |
748 | 0 | } |
749 | 0 | } |
750 | | |
751 | | |
752 | | /* ---------------------------------------------------------------------------- |
753 | | Support NUMA aware allocation |
754 | | -----------------------------------------------------------------------------*/ |
755 | | |
756 | | static _Atomic(size_t) mi_numa_node_count; // = 0 // cache the node count |
757 | | |
758 | 0 | int _mi_os_numa_node_count(void) { |
759 | 0 | size_t count = mi_atomic_load_acquire(&mi_numa_node_count); |
760 | 0 | if mi_unlikely(count == 0) { |
761 | 0 | long ncount = mi_option_get(mi_option_use_numa_nodes); // given explicitly? |
762 | 0 | if (ncount > 0 && ncount < INT_MAX) { |
763 | 0 | count = (size_t)ncount; |
764 | 0 | } |
765 | 0 | else { |
766 | 0 | const size_t n = _mi_prim_numa_node_count(); // or detect dynamically |
767 | 0 | if (n == 0 || n > INT_MAX) { count = 1; } |
768 | 0 | else { count = n; } |
769 | 0 | } |
770 | 0 | mi_atomic_store_release(&mi_numa_node_count, count); // save it |
771 | 0 | _mi_verbose_message("using %zd numa regions\n", count); |
772 | 0 | } |
773 | 0 | mi_assert_internal(count > 0 && count <= INT_MAX); |
774 | 0 | return (int)count; |
775 | 0 | } |
776 | | |
777 | 0 | static int mi_os_numa_node_get(void) { |
778 | 0 | int numa_count = _mi_os_numa_node_count(); |
779 | 0 | if (numa_count<=1) return 0; // optimize on single numa node systems: always node 0 |
780 | | // never more than the node count and >= 0 |
781 | 0 | const size_t n = _mi_prim_numa_node(); |
782 | 0 | int numa_node = (n < INT_MAX ? (int)n : 0); |
783 | 0 | if (numa_node >= numa_count) { numa_node = numa_node % numa_count; } |
784 | 0 | return numa_node; |
785 | 0 | } |
786 | | |
787 | 0 | int _mi_os_numa_node(void) { |
788 | 0 | if mi_likely(mi_atomic_load_relaxed(&mi_numa_node_count) == 1) { |
789 | 0 | return 0; |
790 | 0 | } |
791 | 0 | else { |
792 | 0 | return mi_os_numa_node_get(); |
793 | 0 | } |
794 | 0 | } |