blob: 8c6793957d953355d0faa01857bbb70e5f29910d [file] [log] [blame]
Jason Evans4201af02010-01-24 02:53:40 -08001#define JEMALLOC_CHUNK_DSS_C_
Jason Evans376b1522010-02-11 14:45:59 -08002#include "jemalloc/internal/jemalloc_internal.h"
Jason Evans4201af02010-01-24 02:53:40 -08003/******************************************************************************/
4/* Data. */
5
Jason Evans609ae592012-10-11 13:53:15 -07006const char *dss_prec_names[] = {
7 "disabled",
8 "primary",
9 "secondary",
10 "N/A"
11};
12
Jason Evans4e2e3dd2012-03-13 16:31:41 -070013/*
Jason Evanse2bcf032016-10-13 12:18:38 -070014 * Current dss precedence default, used when creating new arenas. NB: This is
15 * stored as unsigned rather than dss_prec_t because in principle there's no
16 * guarantee that sizeof(dss_prec_t) is the same as sizeof(unsigned), and we use
17 * atomic operations to synchronize the setting.
Jason Evans4e2e3dd2012-03-13 16:31:41 -070018 */
Jason Evanse2bcf032016-10-13 12:18:38 -070019static unsigned dss_prec_default = (unsigned)DSS_PREC_DEFAULT;
Jason Evans4201af02010-01-24 02:53:40 -080020
21/* Base address of the DSS. */
Jason Evans4e2e3dd2012-03-13 16:31:41 -070022static void *dss_base;
Jason Evanse2bcf032016-10-13 12:18:38 -070023/* Atomic boolean indicating whether the DSS is exhausted. */
24static unsigned dss_exhausted;
25/* Atomic current upper limit on DSS addresses. */
Jason Evans4e2e3dd2012-03-13 16:31:41 -070026static void *dss_max;
Jason Evans4201af02010-01-24 02:53:40 -080027
Jason Evans4201af02010-01-24 02:53:40 -080028/******************************************************************************/
29
30static void *
Jason Evans66688532013-12-03 21:49:36 -080031chunk_dss_sbrk(intptr_t increment)
Jason Evans4201af02010-01-24 02:53:40 -080032{
Jason Evans4201af02010-01-24 02:53:40 -080033
Jason Evans4d434ad2014-04-15 12:09:48 -070034#ifdef JEMALLOC_DSS
Jason Evans66688532013-12-03 21:49:36 -080035 return (sbrk(increment));
36#else
Jason Evans7ca0fdf2012-04-12 20:20:58 -070037 not_implemented();
Jason Evans7ca0fdf2012-04-12 20:20:58 -070038 return (NULL);
Jason Evans7ca0fdf2012-04-12 20:20:58 -070039#endif
Jason Evans66688532013-12-03 21:49:36 -080040}
Jason Evans4201af02010-01-24 02:53:40 -080041
Jason Evans609ae592012-10-11 13:53:15 -070042dss_prec_t
Jason Evanse2bcf032016-10-13 12:18:38 -070043chunk_dss_prec_get(void)
Jason Evans609ae592012-10-11 13:53:15 -070044{
45 dss_prec_t ret;
46
Jason Evans551ebc42014-10-03 10:16:09 -070047 if (!have_dss)
Jason Evans609ae592012-10-11 13:53:15 -070048 return (dss_prec_disabled);
Jason Evanse2bcf032016-10-13 12:18:38 -070049 ret = (dss_prec_t)atomic_read_u(&dss_prec_default);
Jason Evans609ae592012-10-11 13:53:15 -070050 return (ret);
51}
52
53bool
Jason Evanse2bcf032016-10-13 12:18:38 -070054chunk_dss_prec_set(dss_prec_t dss_prec)
Jason Evans609ae592012-10-11 13:53:15 -070055{
56
Jason Evans551ebc42014-10-03 10:16:09 -070057 if (!have_dss)
Jason Evans4d434ad2014-04-15 12:09:48 -070058 return (dss_prec != dss_prec_disabled);
Jason Evanse2bcf032016-10-13 12:18:38 -070059 atomic_write_u(&dss_prec_default, (unsigned)dss_prec);
Jason Evans609ae592012-10-11 13:53:15 -070060 return (false);
61}
62
Jason Evanse2bcf032016-10-13 12:18:38 -070063static void *
64chunk_dss_max_update(void *new_addr)
65{
66 void *max_cur;
67 spin_t spinner;
68
69 /*
70 * Get the current end of the DSS as max_cur and assure that dss_max is
71 * up to date.
72 */
73 spin_init(&spinner);
74 while (true) {
75 void *max_prev = atomic_read_p(&dss_max);
76
77 max_cur = chunk_dss_sbrk(0);
78 if ((uintptr_t)max_prev > (uintptr_t)max_cur) {
79 /*
80 * Another thread optimistically updated dss_max. Wait
81 * for it to finish.
82 */
83 spin_adaptive(&spinner);
84 continue;
85 }
86 if (!atomic_cas_p(&dss_max, max_prev, max_cur))
87 break;
88 }
89 /* Fixed new_addr can only be supported if it is at the edge of DSS. */
90 if (new_addr != NULL && max_cur != new_addr)
91 return (NULL);
92
93 return (max_cur);
94}
95
Jason Evans4201af02010-01-24 02:53:40 -080096void *
Jason Evansc1e00ef2016-05-10 22:21:10 -070097chunk_alloc_dss(tsdn_t *tsdn, arena_t *arena, void *new_addr, size_t size,
Jason Evansb2c0d632016-04-13 23:36:15 -070098 size_t alignment, bool *zero, bool *commit)
Jason Evans4201af02010-01-24 02:53:40 -080099{
Jason Evans4d434ad2014-04-15 12:09:48 -0700100 cassert(have_dss);
Mike Hommeyeae26902012-04-10 19:50:33 +0200101 assert(size > 0 && (size & chunksize_mask) == 0);
102 assert(alignment > 0 && (alignment & chunksize_mask) == 0);
Jason Evans7372b152012-02-10 20:22:09 -0800103
Jason Evans4201af02010-01-24 02:53:40 -0800104 /*
105 * sbrk() uses a signed increment argument, so take care not to
106 * interpret a huge allocation request as a negative increment.
107 */
108 if ((intptr_t)size < 0)
109 return (NULL);
110
Jason Evanse2bcf032016-10-13 12:18:38 -0700111 if (!atomic_read_u(&dss_exhausted)) {
Jason Evans4201af02010-01-24 02:53:40 -0800112 /*
113 * The loop is necessary to recover from races with other
114 * threads that are using the DSS for something other than
115 * malloc.
116 */
Jason Evanse2bcf032016-10-13 12:18:38 -0700117 while (true) {
Jason Evanscd7073e2017-02-24 09:45:33 -0800118 void *ret, *max_cur, *dss_next, *dss_prev;
119 void *gap_addr_chunk, *gap_addr_subchunk;
120 size_t gap_size_chunk, gap_size_subchunk;
Dmitry-Me78ae1ac2015-09-08 15:09:20 +0300121 intptr_t incr;
Daniel Micay879e76a2014-11-03 14:02:52 -0500122
Jason Evanse2bcf032016-10-13 12:18:38 -0700123 max_cur = chunk_dss_max_update(new_addr);
124 if (max_cur == NULL)
125 goto label_oom;
Daniel Micay879e76a2014-11-03 14:02:52 -0500126
Jason Evans4201af02010-01-24 02:53:40 -0800127 /*
Jason Evanscd7073e2017-02-24 09:45:33 -0800128 * Compute how much chunk-aligned gap space (if any) is
Mike Hommeyeae26902012-04-10 19:50:33 +0200129 * necessary to satisfy alignment. This space can be
130 * recycled for later use.
131 */
Jason Evanscd7073e2017-02-24 09:45:33 -0800132 gap_addr_chunk = (void *)(CHUNK_CEILING(
133 (uintptr_t)max_cur));
134 ret = (void *)ALIGNMENT_CEILING(
135 (uintptr_t)gap_addr_chunk, alignment);
136 gap_size_chunk = (uintptr_t)ret -
137 (uintptr_t)gap_addr_chunk;
138 /*
139 * Compute the address just past the end of the desired
140 * allocation space.
141 */
Mike Hommeyeae26902012-04-10 19:50:33 +0200142 dss_next = (void *)((uintptr_t)ret + size);
Jason Evanscd7073e2017-02-24 09:45:33 -0800143 if ((uintptr_t)ret < (uintptr_t)max_cur ||
144 (uintptr_t)dss_next < (uintptr_t)max_cur)
Jason Evanse2bcf032016-10-13 12:18:38 -0700145 goto label_oom; /* Wrap-around. */
Jason Evanscd7073e2017-02-24 09:45:33 -0800146 /* Compute the increment, including subchunk bytes. */
147 gap_addr_subchunk = max_cur;
148 gap_size_subchunk = (uintptr_t)ret -
149 (uintptr_t)gap_addr_subchunk;
150 incr = gap_size_subchunk + size;
151
152 assert((uintptr_t)max_cur + incr == (uintptr_t)ret +
153 size);
Jason Evanse2bcf032016-10-13 12:18:38 -0700154
155 /*
156 * Optimistically update dss_max, and roll back below if
157 * sbrk() fails. No other thread will try to extend the
158 * DSS while dss_max is greater than the current DSS
159 * max reported by sbrk(0).
160 */
161 if (atomic_cas_p(&dss_max, max_cur, dss_next))
162 continue;
163
164 /* Try to allocate. */
Jason Evans66688532013-12-03 21:49:36 -0800165 dss_prev = chunk_dss_sbrk(incr);
Jason Evanse2bcf032016-10-13 12:18:38 -0700166 if (dss_prev == max_cur) {
Jason Evans4201af02010-01-24 02:53:40 -0800167 /* Success. */
Jason Evanscd7073e2017-02-24 09:45:33 -0800168 if (gap_size_chunk != 0) {
Jason Evansb49a3342015-07-28 11:28:19 -0400169 chunk_hooks_t chunk_hooks =
170 CHUNK_HOOKS_INITIALIZER;
Jason Evansc1e00ef2016-05-10 22:21:10 -0700171 chunk_dalloc_wrapper(tsdn, arena,
Jason Evanscd7073e2017-02-24 09:45:33 -0800172 &chunk_hooks, gap_addr_chunk,
173 gap_size_chunk,
Jason Evans5c77af92016-11-14 18:27:23 -0800174 arena_extent_sn_next(arena), false,
175 true);
Jason Evansee41ad42015-02-15 18:04:46 -0800176 }
Jason Evans7ad54c12012-04-21 16:04:51 -0700177 if (*zero) {
Jason Evansbd87b012014-04-15 16:35:08 -0700178 JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(
179 ret, size);
Jason Evans7ad54c12012-04-21 16:04:51 -0700180 memset(ret, 0, size);
181 }
Jason Evans03bf5b62015-08-12 10:26:54 -0700182 if (!*commit)
183 *commit = pages_decommit(ret, size);
Jason Evans4201af02010-01-24 02:53:40 -0800184 return (ret);
185 }
Jason Evans4201af02010-01-24 02:53:40 -0800186
Jason Evanse2bcf032016-10-13 12:18:38 -0700187 /*
188 * Failure, whether due to OOM or a race with a raw
189 * sbrk() call from outside the allocator. Try to roll
190 * back optimistic dss_max update; if rollback fails,
191 * it's due to another caller of this function having
192 * succeeded since this invocation started, in which
193 * case rollback is not necessary.
194 */
195 atomic_cas_p(&dss_max, dss_next, max_cur);
196 if (dss_prev == (void *)-1) {
197 /* OOM. */
198 atomic_write_u(&dss_exhausted, (unsigned)true);
199 goto label_oom;
200 }
201 }
202 }
203label_oom:
Jason Evans4201af02010-01-24 02:53:40 -0800204 return (NULL);
205}
206
Jason Evanse2bcf032016-10-13 12:18:38 -0700207static bool
208chunk_in_dss_helper(void *chunk, void *max)
Jason Evanscfdc8cf2010-11-30 16:50:58 -0800209{
Jason Evanscfdc8cf2010-11-30 16:50:58 -0800210
Jason Evanse2bcf032016-10-13 12:18:38 -0700211 return ((uintptr_t)chunk >= (uintptr_t)dss_base && (uintptr_t)chunk <
212 (uintptr_t)max);
Jason Evanscfdc8cf2010-11-30 16:50:58 -0800213}
214
215bool
Jason Evanse2bcf032016-10-13 12:18:38 -0700216chunk_in_dss(void *chunk)
217{
218
219 cassert(have_dss);
220
221 return (chunk_in_dss_helper(chunk, atomic_read_p(&dss_max)));
222}
223
224bool
225chunk_dss_mergeable(void *chunk_a, void *chunk_b)
226{
227 void *max;
228
229 cassert(have_dss);
230
231 max = atomic_read_p(&dss_max);
232 return (chunk_in_dss_helper(chunk_a, max) ==
233 chunk_in_dss_helper(chunk_b, max));
234}
235
236void
Jason Evans4201af02010-01-24 02:53:40 -0800237chunk_dss_boot(void)
238{
239
Jason Evans4d434ad2014-04-15 12:09:48 -0700240 cassert(have_dss);
Jason Evans7372b152012-02-10 20:22:09 -0800241
Jason Evans66688532013-12-03 21:49:36 -0800242 dss_base = chunk_dss_sbrk(0);
Jason Evanse2bcf032016-10-13 12:18:38 -0700243 dss_exhausted = (unsigned)(dss_base == (void *)-1);
Jason Evans4201af02010-01-24 02:53:40 -0800244 dss_max = dss_base;
Jason Evans4e2e3dd2012-03-13 16:31:41 -0700245}
246
Jason Evans4201af02010-01-24 02:53:40 -0800247/******************************************************************************/