1 #define JEMALLOC_PAGES_C_
2 #include "jemalloc/internal/jemalloc_internal.h"
3
4 #ifdef JEMALLOC_SYSCTL_VM_OVERCOMMIT
5 #include <sys/sysctl.h>
6 #endif
7
8 /******************************************************************************/
9 /* Data. */
10
11 #ifndef _WIN32
12 # define PAGES_PROT_COMMIT (PROT_READ | PROT_WRITE)
13 # define PAGES_PROT_DECOMMIT (PROT_NONE)
14 static int mmap_flags;
15 #endif
16 static bool os_overcommits;
17
18 /******************************************************************************/
19 /* Defines/includes needed for special android code. */
20
21 #if defined(__ANDROID__)
22 #include <sys/prctl.h>
23
24 /* Definitions of prctl arguments to set a vma name in Android kernels. */
25 #define ANDROID_PR_SET_VMA 0x53564d41
26 #define ANDROID_PR_SET_VMA_ANON_NAME 0
27 #endif
28
29 /******************************************************************************/
30
31 void *
pages_map(void * addr,size_t size,bool * commit)32 pages_map(void *addr, size_t size, bool *commit)
33 {
34 void *ret;
35
36 assert(size != 0);
37
38 if (os_overcommits)
39 *commit = true;
40
41 #ifdef _WIN32
42 /*
43 * If VirtualAlloc can't allocate at the given address when one is
44 * given, it fails and returns NULL.
45 */
46 ret = VirtualAlloc(addr, size, MEM_RESERVE | (*commit ? MEM_COMMIT : 0),
47 PAGE_READWRITE);
48 #else
49 /*
50 * We don't use MAP_FIXED here, because it can cause the *replacement*
51 * of existing mappings, and we only want to create new mappings.
52 */
53 {
54 int prot = *commit ? PAGES_PROT_COMMIT : PAGES_PROT_DECOMMIT;
55
56 ret = mmap(addr, size, prot, mmap_flags, -1, 0);
57 }
58 assert(ret != NULL);
59
60 if (ret == MAP_FAILED)
61 ret = NULL;
62 else if (addr != NULL && ret != addr) {
63 /*
64 * We succeeded in mapping memory, but not in the right place.
65 */
66 pages_unmap(ret, size);
67 ret = NULL;
68 }
69 #endif
70 #if defined(__ANDROID__)
71 if (ret != NULL) {
72 /* Name this memory as being used by libc */
73 prctl(ANDROID_PR_SET_VMA, ANDROID_PR_SET_VMA_ANON_NAME, ret,
74 size, "libc_malloc");
75 }
76 #endif
77 assert(ret == NULL || (addr == NULL && ret != addr)
78 || (addr != NULL && ret == addr));
79 return (ret);
80 }
81
82 void
pages_unmap(void * addr,size_t size)83 pages_unmap(void *addr, size_t size)
84 {
85
86 #ifdef _WIN32
87 if (VirtualFree(addr, 0, MEM_RELEASE) == 0)
88 #else
89 if (munmap(addr, size) == -1)
90 #endif
91 {
92 char buf[BUFERROR_BUF];
93
94 buferror(get_errno(), buf, sizeof(buf));
95 malloc_printf("<jemalloc>: Error in "
96 #ifdef _WIN32
97 "VirtualFree"
98 #else
99 "munmap"
100 #endif
101 "(): %s\n", buf);
102 if (opt_abort)
103 abort();
104 }
105 }
106
107 void *
pages_trim(void * addr,size_t alloc_size,size_t leadsize,size_t size,bool * commit)108 pages_trim(void *addr, size_t alloc_size, size_t leadsize, size_t size,
109 bool *commit)
110 {
111 void *ret = (void *)((uintptr_t)addr + leadsize);
112
113 assert(alloc_size >= leadsize + size);
114 #ifdef _WIN32
115 {
116 void *new_addr;
117
118 pages_unmap(addr, alloc_size);
119 new_addr = pages_map(ret, size, commit);
120 if (new_addr == ret)
121 return (ret);
122 if (new_addr)
123 pages_unmap(new_addr, size);
124 return (NULL);
125 }
126 #else
127 {
128 size_t trailsize = alloc_size - leadsize - size;
129
130 if (leadsize != 0)
131 pages_unmap(addr, leadsize);
132 if (trailsize != 0)
133 pages_unmap((void *)((uintptr_t)ret + size), trailsize);
134 return (ret);
135 }
136 #endif
137 }
138
139 static bool
pages_commit_impl(void * addr,size_t size,bool commit)140 pages_commit_impl(void *addr, size_t size, bool commit)
141 {
142
143 if (os_overcommits)
144 return (true);
145
146 #ifdef _WIN32
147 return (commit ? (addr != VirtualAlloc(addr, size, MEM_COMMIT,
148 PAGE_READWRITE)) : (!VirtualFree(addr, size, MEM_DECOMMIT)));
149 #else
150 {
151 int prot = commit ? PAGES_PROT_COMMIT : PAGES_PROT_DECOMMIT;
152 void *result = mmap(addr, size, prot, mmap_flags | MAP_FIXED,
153 -1, 0);
154 if (result == MAP_FAILED)
155 return (true);
156 if (result != addr) {
157 /*
158 * We succeeded in mapping memory, but not in the right
159 * place.
160 */
161 pages_unmap(result, size);
162 return (true);
163 }
164 return (false);
165 }
166 #endif
167 }
168
169 bool
pages_commit(void * addr,size_t size)170 pages_commit(void *addr, size_t size)
171 {
172
173 return (pages_commit_impl(addr, size, true));
174 }
175
176 bool
pages_decommit(void * addr,size_t size)177 pages_decommit(void *addr, size_t size)
178 {
179
180 return (pages_commit_impl(addr, size, false));
181 }
182
183 bool
pages_purge(void * addr,size_t size)184 pages_purge(void *addr, size_t size)
185 {
186 bool unzeroed;
187
188 #ifdef _WIN32
189 VirtualAlloc(addr, size, MEM_RESET, PAGE_READWRITE);
190 unzeroed = true;
191 #elif (defined(JEMALLOC_PURGE_MADVISE_FREE) || \
192 defined(JEMALLOC_PURGE_MADVISE_DONTNEED))
193 # if defined(JEMALLOC_PURGE_MADVISE_FREE)
194 # define JEMALLOC_MADV_PURGE MADV_FREE
195 # define JEMALLOC_MADV_ZEROS false
196 # elif defined(JEMALLOC_PURGE_MADVISE_DONTNEED)
197 # define JEMALLOC_MADV_PURGE MADV_DONTNEED
198 # define JEMALLOC_MADV_ZEROS true
199 # else
200 # error No madvise(2) flag defined for purging unused dirty pages
201 # endif
202 int err = madvise(addr, size, JEMALLOC_MADV_PURGE);
203 unzeroed = (!JEMALLOC_MADV_ZEROS || err != 0);
204 # undef JEMALLOC_MADV_PURGE
205 # undef JEMALLOC_MADV_ZEROS
206 #else
207 /* Last resort no-op. */
208 unzeroed = true;
209 #endif
210 return (unzeroed);
211 }
212
213 bool
pages_huge(void * addr,size_t size)214 pages_huge(void *addr, size_t size)
215 {
216
217 assert(PAGE_ADDR2BASE(addr) == addr);
218 assert(PAGE_CEILING(size) == size);
219
220 #ifdef JEMALLOC_THP
221 return (madvise(addr, size, MADV_HUGEPAGE) != 0);
222 #else
223 return (false);
224 #endif
225 }
226
227 bool
pages_nohuge(void * addr,size_t size)228 pages_nohuge(void *addr, size_t size)
229 {
230
231 assert(PAGE_ADDR2BASE(addr) == addr);
232 assert(PAGE_CEILING(size) == size);
233
234 #ifdef JEMALLOC_THP
235 return (madvise(addr, size, MADV_NOHUGEPAGE) != 0);
236 #else
237 return (false);
238 #endif
239 }
240
241 #ifdef JEMALLOC_SYSCTL_VM_OVERCOMMIT
242 static bool
os_overcommits_sysctl(void)243 os_overcommits_sysctl(void)
244 {
245 int vm_overcommit;
246 size_t sz;
247
248 sz = sizeof(vm_overcommit);
249 if (sysctlbyname("vm.overcommit", &vm_overcommit, &sz, NULL, 0) != 0)
250 return (false); /* Error. */
251
252 return ((vm_overcommit & 0x3) == 0);
253 }
254 #endif
255
256 #ifdef JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY
257 /*
258 * Use syscall(2) rather than {open,read,close}(2) when possible to avoid
259 * reentry during bootstrapping if another library has interposed system call
260 * wrappers.
261 */
262 static bool
os_overcommits_proc(void)263 os_overcommits_proc(void)
264 {
265 int fd;
266 char buf[1];
267 ssize_t nread;
268
269 #if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_open)
270 fd = (int)syscall(SYS_open, "/proc/sys/vm/overcommit_memory", O_RDONLY);
271 #else
272 fd = open("/proc/sys/vm/overcommit_memory", O_RDONLY);
273 #endif
274 if (fd == -1)
275 return (false); /* Error. */
276
277 #if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_read)
278 nread = (ssize_t)syscall(SYS_read, fd, &buf, sizeof(buf));
279 #else
280 nread = read(fd, &buf, sizeof(buf));
281 #endif
282
283 #if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_close)
284 syscall(SYS_close, fd);
285 #else
286 close(fd);
287 #endif
288
289 if (nread < 1)
290 return (false); /* Error. */
291 /*
292 * /proc/sys/vm/overcommit_memory meanings:
293 * 0: Heuristic overcommit.
294 * 1: Always overcommit.
295 * 2: Never overcommit.
296 */
297 return (buf[0] == '0' || buf[0] == '1');
298 }
299 #endif
300
301 void
pages_boot(void)302 pages_boot(void)
303 {
304
305 #ifndef _WIN32
306 mmap_flags = MAP_PRIVATE | MAP_ANON;
307 #endif
308
309 #ifdef JEMALLOC_SYSCTL_VM_OVERCOMMIT
310 os_overcommits = os_overcommits_sysctl();
311 #elif defined(JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY)
312 os_overcommits = os_overcommits_proc();
313 # ifdef MAP_NORESERVE
314 if (os_overcommits)
315 mmap_flags |= MAP_NORESERVE;
316 # endif
317 #else
318 os_overcommits = false;
319 #endif
320 }
321