17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5789d94c2Sjwadams * Common Development and Distribution License (the "License"). 6789d94c2Sjwadams * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21e8031f0aSraf 227c478bd9Sstevel@tonic-gate /* 23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * The structure of the sbrk backend: 317c478bd9Sstevel@tonic-gate * 327c478bd9Sstevel@tonic-gate * +-----------+ 337c478bd9Sstevel@tonic-gate * | sbrk_top | 347c478bd9Sstevel@tonic-gate * +-----------+ 357c478bd9Sstevel@tonic-gate * | (vmem_sbrk_alloc(), vmem_free()) 367c478bd9Sstevel@tonic-gate * | 377c478bd9Sstevel@tonic-gate * +-----------+ 387c478bd9Sstevel@tonic-gate * | sbrk_heap | 397c478bd9Sstevel@tonic-gate * +-----------+ 407c478bd9Sstevel@tonic-gate * | | ... | (vmem_alloc(), vmem_free()) 417c478bd9Sstevel@tonic-gate * <other arenas> 427c478bd9Sstevel@tonic-gate * 437c478bd9Sstevel@tonic-gate * The sbrk_top arena holds all controlled memory. vmem_sbrk_alloc() handles 447c478bd9Sstevel@tonic-gate * allocations from it, including growing the heap when we run low. 457c478bd9Sstevel@tonic-gate * 467c478bd9Sstevel@tonic-gate * Growing the heap is complicated by the fact that we have to extend the 477c478bd9Sstevel@tonic-gate * sbrk_top arena (using _vmem_extend_alloc()), and that can fail. Since 487c478bd9Sstevel@tonic-gate * other threads may be actively allocating, we can't return the memory. 497c478bd9Sstevel@tonic-gate * 507c478bd9Sstevel@tonic-gate * Instead, we put it on a doubly-linked list, sbrk_fails, which we search 517c478bd9Sstevel@tonic-gate * before calling sbrk(). 527c478bd9Sstevel@tonic-gate */ 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate #include <errno.h> 557c478bd9Sstevel@tonic-gate #include <limits.h> 567c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 577c478bd9Sstevel@tonic-gate #include <sys/mman.h> 587c478bd9Sstevel@tonic-gate #include <unistd.h> 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate #include "vmem_base.h" 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate #include "misc.h" 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate size_t vmem_sbrk_pagesize = 0; /* the preferred page size of the heap */ 657c478bd9Sstevel@tonic-gate 66789d94c2Sjwadams #define VMEM_SBRK_MINALLOC (64 * 1024) 67789d94c2Sjwadams size_t vmem_sbrk_minalloc = VMEM_SBRK_MINALLOC; /* minimum allocation */ 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate static size_t real_pagesize; 707c478bd9Sstevel@tonic-gate static vmem_t *sbrk_heap; 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate typedef struct sbrk_fail { 737c478bd9Sstevel@tonic-gate struct sbrk_fail *sf_next; 747c478bd9Sstevel@tonic-gate struct sbrk_fail *sf_prev; 757c478bd9Sstevel@tonic-gate void *sf_base; /* == the sbrk_fail's address */ 767c478bd9Sstevel@tonic-gate size_t sf_size; /* the size of this buffer */ 777c478bd9Sstevel@tonic-gate } sbrk_fail_t; 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate static sbrk_fail_t sbrk_fails = { 807c478bd9Sstevel@tonic-gate &sbrk_fails, 817c478bd9Sstevel@tonic-gate &sbrk_fails, 827c478bd9Sstevel@tonic-gate NULL, 837c478bd9Sstevel@tonic-gate 0 847c478bd9Sstevel@tonic-gate }; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate static mutex_t sbrk_faillock = DEFAULTMUTEX; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate /* 897c478bd9Sstevel@tonic-gate * Try to extend src with [pos, pos + size). 907c478bd9Sstevel@tonic-gate * 917c478bd9Sstevel@tonic-gate * If it fails, add the block to the sbrk_fails list. 927c478bd9Sstevel@tonic-gate */ 937c478bd9Sstevel@tonic-gate static void * 947c478bd9Sstevel@tonic-gate vmem_sbrk_extend_alloc(vmem_t *src, void *pos, size_t size, size_t alloc, 957c478bd9Sstevel@tonic-gate int vmflags) 967c478bd9Sstevel@tonic-gate { 977c478bd9Sstevel@tonic-gate sbrk_fail_t *fnext, *fprev, *fp; 987c478bd9Sstevel@tonic-gate void *ret; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate ret = _vmem_extend_alloc(src, pos, size, alloc, vmflags); 1017c478bd9Sstevel@tonic-gate if (ret != NULL) 1027c478bd9Sstevel@tonic-gate return (ret); 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate fp = (sbrk_fail_t *)pos; 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate ASSERT(sizeof (sbrk_fail_t) <= size); 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate fp->sf_base = pos; 1097c478bd9Sstevel@tonic-gate fp->sf_size = size; 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate (void) mutex_lock(&sbrk_faillock); 1127c478bd9Sstevel@tonic-gate fp->sf_next = fnext = &sbrk_fails; 1137c478bd9Sstevel@tonic-gate fp->sf_prev = fprev = sbrk_fails.sf_prev; 1147c478bd9Sstevel@tonic-gate fnext->sf_prev = fp; 1157c478bd9Sstevel@tonic-gate fprev->sf_next = fp; 1167c478bd9Sstevel@tonic-gate (void) mutex_unlock(&sbrk_faillock); 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate return (NULL); 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate /* 1227c478bd9Sstevel@tonic-gate * Try to add at least size bytes to src, using the sbrk_fails list 1237c478bd9Sstevel@tonic-gate */ 1247c478bd9Sstevel@tonic-gate static void * 1257c478bd9Sstevel@tonic-gate vmem_sbrk_tryfail(vmem_t *src, size_t size, int vmflags) 1267c478bd9Sstevel@tonic-gate { 1277c478bd9Sstevel@tonic-gate sbrk_fail_t *fp; 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate (void) mutex_lock(&sbrk_faillock); 1307c478bd9Sstevel@tonic-gate for (fp = sbrk_fails.sf_next; fp != &sbrk_fails; fp = fp->sf_next) { 1317c478bd9Sstevel@tonic-gate if (fp->sf_size >= size) { 1327c478bd9Sstevel@tonic-gate fp->sf_next->sf_prev = fp->sf_prev; 1337c478bd9Sstevel@tonic-gate fp->sf_prev->sf_next = fp->sf_next; 1347c478bd9Sstevel@tonic-gate fp->sf_next = fp->sf_prev = NULL; 1357c478bd9Sstevel@tonic-gate break; 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate (void) mutex_unlock(&sbrk_faillock); 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate if (fp != &sbrk_fails) { 1417c478bd9Sstevel@tonic-gate ASSERT(fp->sf_base == (void *)fp); 1427c478bd9Sstevel@tonic-gate return (vmem_sbrk_extend_alloc(src, fp, fp->sf_size, size, 1437c478bd9Sstevel@tonic-gate vmflags)); 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate /* 1467c478bd9Sstevel@tonic-gate * nothing of the right size on the freelist 1477c478bd9Sstevel@tonic-gate */ 1487c478bd9Sstevel@tonic-gate return (NULL); 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate static void * 1527c478bd9Sstevel@tonic-gate vmem_sbrk_alloc(vmem_t *src, size_t size, int vmflags) 1537c478bd9Sstevel@tonic-gate { 1547c478bd9Sstevel@tonic-gate extern void *_sbrk_grow_aligned(size_t min_size, size_t low_align, 1557c478bd9Sstevel@tonic-gate size_t high_align, size_t *actual_size); 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate void *ret; 1587c478bd9Sstevel@tonic-gate void *buf; 1597c478bd9Sstevel@tonic-gate size_t buf_size; 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate int old_errno = errno; 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate ret = vmem_alloc(src, size, VM_NOSLEEP); 1647c478bd9Sstevel@tonic-gate if (ret != NULL) { 1657c478bd9Sstevel@tonic-gate errno = old_errno; 1667c478bd9Sstevel@tonic-gate return (ret); 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate /* 1707c478bd9Sstevel@tonic-gate * The allocation failed. We need to grow the heap. 1717c478bd9Sstevel@tonic-gate * 1727c478bd9Sstevel@tonic-gate * First, try to use any buffers which failed earlier. 1737c478bd9Sstevel@tonic-gate */ 1747c478bd9Sstevel@tonic-gate if (sbrk_fails.sf_next != &sbrk_fails && 1757c478bd9Sstevel@tonic-gate (ret = vmem_sbrk_tryfail(src, size, vmflags)) != NULL) 1767c478bd9Sstevel@tonic-gate return (ret); 1777c478bd9Sstevel@tonic-gate 178789d94c2Sjwadams buf_size = MAX(size, vmem_sbrk_minalloc); 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate /* 1817c478bd9Sstevel@tonic-gate * buf_size gets overwritten with the actual allocated size 1827c478bd9Sstevel@tonic-gate */ 1837c478bd9Sstevel@tonic-gate buf = _sbrk_grow_aligned(buf_size, real_pagesize, vmem_sbrk_pagesize, 1847c478bd9Sstevel@tonic-gate &buf_size); 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate if (buf != MAP_FAILED) { 1877c478bd9Sstevel@tonic-gate ret = vmem_sbrk_extend_alloc(src, buf, buf_size, size, vmflags); 1887c478bd9Sstevel@tonic-gate if (ret != NULL) { 1897c478bd9Sstevel@tonic-gate errno = old_errno; 1907c478bd9Sstevel@tonic-gate return (ret); 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate /* 1957c478bd9Sstevel@tonic-gate * Growing the heap failed. The vmem_alloc() above called umem_reap(). 1967c478bd9Sstevel@tonic-gate */ 1977c478bd9Sstevel@tonic-gate ASSERT((vmflags & VM_NOSLEEP) == VM_NOSLEEP); 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate errno = old_errno; 2007c478bd9Sstevel@tonic-gate return (NULL); 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate /* 2047c478bd9Sstevel@tonic-gate * fork1() support 2057c478bd9Sstevel@tonic-gate */ 2067c478bd9Sstevel@tonic-gate void 2077c478bd9Sstevel@tonic-gate vmem_sbrk_lockup(void) 2087c478bd9Sstevel@tonic-gate { 2097c478bd9Sstevel@tonic-gate (void) mutex_lock(&sbrk_faillock); 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate void 2137c478bd9Sstevel@tonic-gate vmem_sbrk_release(void) 2147c478bd9Sstevel@tonic-gate { 2157c478bd9Sstevel@tonic-gate (void) mutex_unlock(&sbrk_faillock); 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate vmem_t * 2197c478bd9Sstevel@tonic-gate vmem_sbrk_arena(vmem_alloc_t **a_out, vmem_free_t **f_out) 2207c478bd9Sstevel@tonic-gate { 2217c478bd9Sstevel@tonic-gate if (sbrk_heap == NULL) { 2227c478bd9Sstevel@tonic-gate size_t heap_size; 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate real_pagesize = sysconf(_SC_PAGESIZE); 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate heap_size = vmem_sbrk_pagesize; 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate if (issetugid()) { 2297c478bd9Sstevel@tonic-gate heap_size = 0; 2307c478bd9Sstevel@tonic-gate } else if (heap_size != 0 && !ISP2(heap_size)) { 2317c478bd9Sstevel@tonic-gate heap_size = 0; 2327c478bd9Sstevel@tonic-gate log_message("ignoring bad pagesize: 0x%p\n", heap_size); 2337c478bd9Sstevel@tonic-gate } 2347c478bd9Sstevel@tonic-gate if (heap_size <= real_pagesize) { 2357c478bd9Sstevel@tonic-gate heap_size = real_pagesize; 2367c478bd9Sstevel@tonic-gate } else { 2377c478bd9Sstevel@tonic-gate struct memcntl_mha mha; 2387c478bd9Sstevel@tonic-gate mha.mha_cmd = MHA_MAPSIZE_BSSBRK; 2397c478bd9Sstevel@tonic-gate mha.mha_flags = 0; 2407c478bd9Sstevel@tonic-gate mha.mha_pagesize = heap_size; 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate if (memcntl(NULL, 0, MC_HAT_ADVISE, (char *)&mha, 0, 0) 2437c478bd9Sstevel@tonic-gate == -1) { 2447c478bd9Sstevel@tonic-gate log_message("unable to set MAPSIZE_BSSBRK to " 2457c478bd9Sstevel@tonic-gate "0x%p\n", heap_size); 2467c478bd9Sstevel@tonic-gate heap_size = real_pagesize; 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate vmem_sbrk_pagesize = heap_size; 2507c478bd9Sstevel@tonic-gate 251789d94c2Sjwadams /* validate vmem_sbrk_minalloc */ 252789d94c2Sjwadams if (vmem_sbrk_minalloc < VMEM_SBRK_MINALLOC) 253789d94c2Sjwadams vmem_sbrk_minalloc = VMEM_SBRK_MINALLOC; 254789d94c2Sjwadams vmem_sbrk_minalloc = P2ROUNDUP(vmem_sbrk_minalloc, heap_size); 255789d94c2Sjwadams 2567c478bd9Sstevel@tonic-gate sbrk_heap = vmem_init("sbrk_top", real_pagesize, 2577c478bd9Sstevel@tonic-gate vmem_sbrk_alloc, vmem_free, 2587c478bd9Sstevel@tonic-gate "sbrk_heap", NULL, 0, real_pagesize, 2597c478bd9Sstevel@tonic-gate vmem_alloc, vmem_free); 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate if (a_out != NULL) 2637c478bd9Sstevel@tonic-gate *a_out = vmem_alloc; 2647c478bd9Sstevel@tonic-gate if (f_out != NULL) 2657c478bd9Sstevel@tonic-gate *f_out = vmem_free; 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate return (sbrk_heap); 2687c478bd9Sstevel@tonic-gate } 269