Initial Linux ZFS GIT Repo
[zfs.git] / zfs / lib / libumem / vmem_sbrk.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * Portions Copyright 2006 OmniTI, Inc.
28  */
29
30 /* #pragma ident        "@(#)vmem_sbrk.c        1.4     05/06/08 SMI" */
31
32 /*
33  * The structure of the sbrk backend:
34  *
35  * +-----------+
36  * | sbrk_top  |
37  * +-----------+
38  *      | (vmem_sbrk_alloc(), vmem_free())
39  *      |
40  * +-----------+
41  * | sbrk_heap |
42  * +-----------+
43  *   | | ... |  (vmem_alloc(), vmem_free())
44  * <other arenas>
45  *
46  * The sbrk_top arena holds all controlled memory.  vmem_sbrk_alloc() handles
47  * allocations from it, including growing the heap when we run low.
48  *
49  * Growing the heap is complicated by the fact that we have to extend the
50  * sbrk_top arena (using _vmem_extend_alloc()), and that can fail.  Since
51  * other threads may be actively allocating, we can't return the memory.
52  *
53  * Instead, we put it on a doubly-linked list, sbrk_fails, which we search
54  * before calling sbrk().
55  */
56
57 #include "config.h"
58 /* #include "mtlib.h" */
59 #include <errno.h>
60 #include <limits.h>
61 #ifdef HAVE_SYS_SYSMACROS_H
62 #include <sys/sysmacros.h>
63 #endif
64 #include <sys/mman.h>
65 #include <unistd.h>
66
67 #include "vmem_base.h"
68
69 #include "misc.h"
70
71 size_t vmem_sbrk_pagesize = 0; /* the preferred page size of the heap */
72
73 #define MIN_ALLOC       (64*1024)
74
75 static size_t real_pagesize;
76 static vmem_t *sbrk_heap;
77
78 typedef struct sbrk_fail {
79         struct sbrk_fail *sf_next;
80         struct sbrk_fail *sf_prev;
81         void *sf_base;                  /* == the sbrk_fail's address */
82         size_t sf_size;                 /* the size of this buffer */
83 } sbrk_fail_t;
84
85 static sbrk_fail_t sbrk_fails = {
86         &sbrk_fails,
87         &sbrk_fails,
88         NULL,
89         0
90 };
91
92 static mutex_t sbrk_faillock = DEFAULTMUTEX;
93
94 /*
95  * _sbrk_grow_aligned() aligns the old break to a low_align boundry,
96  * adds min_size, aligns to a high_align boundry, and calls _brk_unlocked()
97  * to set the new break.  The low_aligned-aligned value is returned, and
98  * the actual space allocated is returned through actual_size.
99  *
100  * Unlike sbrk(2), _sbrk_grow_aligned takes an unsigned size, and does
101  * not allow shrinking the heap.
102  */
103 void *
104 _sbrk_grow_aligned(size_t min_size, size_t low_align, size_t high_align,
105     size_t *actual_size)
106 {
107   uintptr_t old_brk;
108   uintptr_t ret_brk;
109   uintptr_t high_brk;
110   uintptr_t new_brk;
111   int brk_result;
112
113 #define ALIGNSZ   16
114 #define BRKALIGN(x) (caddr_t)P2ROUNDUP((uintptr_t)(x), ALIGNSZ)
115   
116   if ((low_align & (low_align - 1)) != 0 ||
117       (high_align & (high_align - 1)) != 0) {
118     errno = EINVAL;
119     return ((void *)-1);
120   }
121   low_align = MAX(low_align, ALIGNSZ);
122   high_align = MAX(high_align, ALIGNSZ);
123
124   old_brk = (uintptr_t)BRKALIGN(sbrk(0));
125   ret_brk = P2ROUNDUP(old_brk, low_align);
126   high_brk = ret_brk + min_size;
127   new_brk = P2ROUNDUP(high_brk, high_align);
128
129   /*
130    * Check for overflow
131    */
132   if (ret_brk < old_brk || high_brk < ret_brk || new_brk < high_brk) {
133     errno = ENOMEM;
134     return ((void *)-1);
135   }
136
137   brk_result = brk((void *)new_brk);
138
139   if (brk_result != 0)
140     return ((void *)-1);
141
142   if (actual_size != NULL)
143     *actual_size = (new_brk - ret_brk);
144   return ((void *)ret_brk);
145 }
146
147 /*
148  * Try to extend src with [pos, pos + size).
149  *
150  * If it fails, add the block to the sbrk_fails list.
151  */
152 static void *
153 vmem_sbrk_extend_alloc(vmem_t *src, void *pos, size_t size, size_t alloc,
154     int vmflags)
155 {
156         sbrk_fail_t *fnext, *fprev, *fp;
157         void *ret;
158
159         ret = _vmem_extend_alloc(src, pos, size, alloc, vmflags);
160         if (ret != NULL)
161                 return (ret);
162
163         fp = (sbrk_fail_t *)pos;
164
165         ASSERT(sizeof (sbrk_fail_t) <= size);
166
167         fp->sf_base = pos;
168         fp->sf_size = size;
169
170         (void) mutex_lock(&sbrk_faillock);
171         fp->sf_next = fnext = &sbrk_fails;
172         fp->sf_prev = fprev = sbrk_fails.sf_prev;
173         fnext->sf_prev = fp;
174         fprev->sf_next = fp;
175         (void) mutex_unlock(&sbrk_faillock);
176
177         return (NULL);
178 }
179
180 /*
181  * Try to add at least size bytes to src, using the sbrk_fails list
182  */
183 static void *
184 vmem_sbrk_tryfail(vmem_t *src, size_t size, int vmflags)
185 {
186         sbrk_fail_t *fp;
187
188         (void) mutex_lock(&sbrk_faillock);
189         for (fp = sbrk_fails.sf_next; fp != &sbrk_fails; fp = fp->sf_next) {
190                 if (fp->sf_size >= size) {
191                         fp->sf_next->sf_prev = fp->sf_prev;
192                         fp->sf_prev->sf_next = fp->sf_next;
193                         fp->sf_next = fp->sf_prev = NULL;
194                         break;
195                 }
196         }
197         (void) mutex_unlock(&sbrk_faillock);
198
199         if (fp != &sbrk_fails) {
200                 ASSERT(fp->sf_base == (void *)fp);
201                 return (vmem_sbrk_extend_alloc(src, fp, fp->sf_size, size,
202                     vmflags));
203         }
204         /*
205          * nothing of the right size on the freelist
206          */
207         return (NULL);
208 }
209
210 static void *
211 vmem_sbrk_alloc(vmem_t *src, size_t size, int vmflags)
212 {
213         extern void *_sbrk_grow_aligned(size_t min_size, size_t low_align,
214             size_t high_align, size_t *actual_size);
215
216         void *ret;
217         void *buf;
218         size_t buf_size;
219
220         int old_errno = errno;
221
222         ret = vmem_alloc(src, size, VM_NOSLEEP);
223         if (ret != NULL) {
224                 errno = old_errno;
225                 return (ret);
226         }
227
228         /*
229          * The allocation failed.  We need to grow the heap.
230          *
231          * First, try to use any buffers which failed earlier.
232          */
233         if (sbrk_fails.sf_next != &sbrk_fails &&
234             (ret = vmem_sbrk_tryfail(src, size, vmflags)) != NULL)
235                 return (ret);
236
237         buf_size = MAX(size, MIN_ALLOC);
238
239         /*
240          * buf_size gets overwritten with the actual allocated size
241          */
242         buf = _sbrk_grow_aligned(buf_size, real_pagesize, vmem_sbrk_pagesize,
243             &buf_size);
244
245         if (buf != MAP_FAILED) {
246                 ret = vmem_sbrk_extend_alloc(src, buf, buf_size, size, vmflags);
247                 if (ret != NULL) {
248                         errno = old_errno;
249                         return (ret);
250                 }
251         }
252
253         /*
254          * Growing the heap failed. The vmem_alloc() above called umem_reap().
255          */
256         ASSERT((vmflags & VM_NOSLEEP) == VM_NOSLEEP);
257
258         errno = old_errno;
259         return (NULL);
260 }
261
262 /*
263  * fork1() support
264  */
265 void
266 vmem_sbrk_lockup(void)
267 {
268         (void) mutex_lock(&sbrk_faillock);
269 }
270
271 void
272 vmem_sbrk_release(void)
273 {
274         (void) mutex_unlock(&sbrk_faillock);
275 }
276
277 vmem_t *
278 vmem_sbrk_arena(vmem_alloc_t **a_out, vmem_free_t **f_out)
279 {
280         if (sbrk_heap == NULL) {
281                 size_t heap_size;
282
283                 real_pagesize = sysconf(_SC_PAGESIZE);
284
285                 heap_size = vmem_sbrk_pagesize;
286
287                 if (issetugid()) {
288                         heap_size = 0;
289                 } else if (heap_size != 0 && !ISP2(heap_size)) {
290                         heap_size = 0;
291                         log_message("ignoring bad pagesize: 0x%p\n", heap_size);
292                 }
293                 if (heap_size <= real_pagesize) {
294                         heap_size = real_pagesize;
295                 } else {
296 #ifdef MHA_MAPSIZE_BSSBRK
297                         struct memcntl_mha mha;
298                         mha.mha_cmd = MHA_MAPSIZE_BSSBRK;
299                         mha.mha_flags = 0;
300                         mha.mha_pagesize = heap_size;
301
302                         if (memcntl(NULL, 0, MC_HAT_ADVISE, (char *)&mha, 0, 0)
303                             == -1) {
304                                 log_message("unable to set MAPSIZE_BSSBRK to "
305                                     "0x%p\n", heap_size);
306                                 heap_size = real_pagesize;
307                         }
308 #else
309                         heap_size = real_pagesize;
310 #endif
311                 }
312                 vmem_sbrk_pagesize = heap_size;
313
314                 sbrk_heap = vmem_init("sbrk_top", real_pagesize,
315                     vmem_sbrk_alloc, vmem_free,
316                     "sbrk_heap", NULL, 0, real_pagesize,
317                     vmem_alloc, vmem_free);
318         }
319
320         if (a_out != NULL)
321                 *a_out = vmem_alloc;
322         if (f_out != NULL)
323                 *f_out = vmem_free;
324
325         return (sbrk_heap);
326 }