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 5aaa10e67Sha137994 * Common Development and Distribution License (the "License"). 6aaa10e67Sha137994 * 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 */ 217c478bd9Sstevel@tonic-gate /* 224a7ceddaSBlake Jones * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 27*47bb2664SMatthew Ahrens * Copyright (c) 2012, 2015 by Delphix. All rights reserved. 280ee475b2SBryan Cantrill * Copyright (c) 2012, Joyent, Inc. All rights reserved. 2994dd93aeSGeorge Wilson */ 3094dd93aeSGeorge Wilson 3194dd93aeSGeorge Wilson /* 327c478bd9Sstevel@tonic-gate * Big Theory Statement for the virtual memory allocator. 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate * For a more complete description of the main ideas, see: 357c478bd9Sstevel@tonic-gate * 367c478bd9Sstevel@tonic-gate * Jeff Bonwick and Jonathan Adams, 377c478bd9Sstevel@tonic-gate * 387c478bd9Sstevel@tonic-gate * Magazines and vmem: Extending the Slab Allocator to Many CPUs and 397c478bd9Sstevel@tonic-gate * Arbitrary Resources. 407c478bd9Sstevel@tonic-gate * 417c478bd9Sstevel@tonic-gate * Proceedings of the 2001 Usenix Conference. 427c478bd9Sstevel@tonic-gate * Available as http://www.usenix.org/event/usenix01/bonwick.html 437c478bd9Sstevel@tonic-gate * 447c478bd9Sstevel@tonic-gate * 457c478bd9Sstevel@tonic-gate * 1. General Concepts 467c478bd9Sstevel@tonic-gate * ------------------- 477c478bd9Sstevel@tonic-gate * 487c478bd9Sstevel@tonic-gate * 1.1 Overview 497c478bd9Sstevel@tonic-gate * ------------ 507c478bd9Sstevel@tonic-gate * We divide the kernel address space into a number of logically distinct 517c478bd9Sstevel@tonic-gate * pieces, or *arenas*: text, data, heap, stack, and so on. Within these 527c478bd9Sstevel@tonic-gate * arenas we often subdivide further; for example, we use heap addresses 537c478bd9Sstevel@tonic-gate * not only for the kernel heap (kmem_alloc() space), but also for DVMA, 547c478bd9Sstevel@tonic-gate * bp_mapin(), /dev/kmem, and even some device mappings like the TOD chip. 557c478bd9Sstevel@tonic-gate * The kernel address space, therefore, is most accurately described as 567c478bd9Sstevel@tonic-gate * a tree of arenas in which each node of the tree *imports* some subset 577c478bd9Sstevel@tonic-gate * of its parent. The virtual memory allocator manages these arenas and 587c478bd9Sstevel@tonic-gate * supports their natural hierarchical structure. 597c478bd9Sstevel@tonic-gate * 607c478bd9Sstevel@tonic-gate * 1.2 Arenas 617c478bd9Sstevel@tonic-gate * ---------- 627c478bd9Sstevel@tonic-gate * An arena is nothing more than a set of integers. These integers most 637c478bd9Sstevel@tonic-gate * commonly represent virtual addresses, but in fact they can represent 647c478bd9Sstevel@tonic-gate * anything at all. For example, we could use an arena containing the 657c478bd9Sstevel@tonic-gate * integers minpid through maxpid to allocate process IDs. vmem_create() 667c478bd9Sstevel@tonic-gate * and vmem_destroy() create and destroy vmem arenas. In order to 677c478bd9Sstevel@tonic-gate * differentiate between arenas used for adresses and arenas used for 687c478bd9Sstevel@tonic-gate * identifiers, the VMC_IDENTIFIER flag is passed to vmem_create(). This 697c478bd9Sstevel@tonic-gate * prevents identifier exhaustion from being diagnosed as general memory 707c478bd9Sstevel@tonic-gate * failure. 717c478bd9Sstevel@tonic-gate * 727c478bd9Sstevel@tonic-gate * 1.3 Spans 737c478bd9Sstevel@tonic-gate * --------- 747c478bd9Sstevel@tonic-gate * We represent the integers in an arena as a collection of *spans*, or 757c478bd9Sstevel@tonic-gate * contiguous ranges of integers. For example, the kernel heap consists 767c478bd9Sstevel@tonic-gate * of just one span: [kernelheap, ekernelheap). Spans can be added to an 777c478bd9Sstevel@tonic-gate * arena in two ways: explicitly, by vmem_add(), or implicitly, by 787c478bd9Sstevel@tonic-gate * importing, as described in Section 1.5 below. 797c478bd9Sstevel@tonic-gate * 807c478bd9Sstevel@tonic-gate * 1.4 Segments 817c478bd9Sstevel@tonic-gate * ------------ 827c478bd9Sstevel@tonic-gate * Spans are subdivided into *segments*, each of which is either allocated 837c478bd9Sstevel@tonic-gate * or free. A segment, like a span, is a contiguous range of integers. 847c478bd9Sstevel@tonic-gate * Each allocated segment [addr, addr + size) represents exactly one 857c478bd9Sstevel@tonic-gate * vmem_alloc(size) that returned addr. Free segments represent the space 867c478bd9Sstevel@tonic-gate * between allocated segments. If two free segments are adjacent, we 877c478bd9Sstevel@tonic-gate * coalesce them into one larger segment; that is, if segments [a, b) and 887c478bd9Sstevel@tonic-gate * [b, c) are both free, we merge them into a single segment [a, c). 897c478bd9Sstevel@tonic-gate * The segments within a span are linked together in increasing-address order 907c478bd9Sstevel@tonic-gate * so we can easily determine whether coalescing is possible. 917c478bd9Sstevel@tonic-gate * 927c478bd9Sstevel@tonic-gate * Segments never cross span boundaries. When all segments within 937c478bd9Sstevel@tonic-gate * an imported span become free, we return the span to its source. 947c478bd9Sstevel@tonic-gate * 957c478bd9Sstevel@tonic-gate * 1.5 Imported Memory 967c478bd9Sstevel@tonic-gate * ------------------- 977c478bd9Sstevel@tonic-gate * As mentioned in the overview, some arenas are logical subsets of 987c478bd9Sstevel@tonic-gate * other arenas. For example, kmem_va_arena (a virtual address cache 997c478bd9Sstevel@tonic-gate * that satisfies most kmem_slab_create() requests) is just a subset 1007c478bd9Sstevel@tonic-gate * of heap_arena (the kernel heap) that provides caching for the most 1017c478bd9Sstevel@tonic-gate * common slab sizes. When kmem_va_arena runs out of virtual memory, 1027c478bd9Sstevel@tonic-gate * it *imports* more from the heap; we say that heap_arena is the 1037c478bd9Sstevel@tonic-gate * *vmem source* for kmem_va_arena. vmem_create() allows you to 1047c478bd9Sstevel@tonic-gate * specify any existing vmem arena as the source for your new arena. 1057c478bd9Sstevel@tonic-gate * Topologically, since every arena is a child of at most one source, 1067c478bd9Sstevel@tonic-gate * the set of all arenas forms a collection of trees. 1077c478bd9Sstevel@tonic-gate * 1087c478bd9Sstevel@tonic-gate * 1.6 Constrained Allocations 1097c478bd9Sstevel@tonic-gate * --------------------------- 1107c478bd9Sstevel@tonic-gate * Some vmem clients are quite picky about the kind of address they want. 1117c478bd9Sstevel@tonic-gate * For example, the DVMA code may need an address that is at a particular 1127c478bd9Sstevel@tonic-gate * phase with respect to some alignment (to get good cache coloring), or 1137c478bd9Sstevel@tonic-gate * that lies within certain limits (the addressable range of a device), 1147c478bd9Sstevel@tonic-gate * or that doesn't cross some boundary (a DMA counter restriction) -- 1157c478bd9Sstevel@tonic-gate * or all of the above. vmem_xalloc() allows the client to specify any 1167c478bd9Sstevel@tonic-gate * or all of these constraints. 1177c478bd9Sstevel@tonic-gate * 1187c478bd9Sstevel@tonic-gate * 1.7 The Vmem Quantum 1197c478bd9Sstevel@tonic-gate * -------------------- 1207c478bd9Sstevel@tonic-gate * Every arena has a notion of 'quantum', specified at vmem_create() time, 1217c478bd9Sstevel@tonic-gate * that defines the arena's minimum unit of currency. Most commonly the 1227c478bd9Sstevel@tonic-gate * quantum is either 1 or PAGESIZE, but any power of 2 is legal. 1237c478bd9Sstevel@tonic-gate * All vmem allocations are guaranteed to be quantum-aligned. 1247c478bd9Sstevel@tonic-gate * 1257c478bd9Sstevel@tonic-gate * 1.8 Quantum Caching 1267c478bd9Sstevel@tonic-gate * ------------------- 1277c478bd9Sstevel@tonic-gate * A vmem arena may be so hot (frequently used) that the scalability of vmem 1287c478bd9Sstevel@tonic-gate * allocation is a significant concern. We address this by allowing the most 1297c478bd9Sstevel@tonic-gate * common allocation sizes to be serviced by the kernel memory allocator, 1307c478bd9Sstevel@tonic-gate * which provides low-latency per-cpu caching. The qcache_max argument to 1317c478bd9Sstevel@tonic-gate * vmem_create() specifies the largest allocation size to cache. 1327c478bd9Sstevel@tonic-gate * 1337c478bd9Sstevel@tonic-gate * 1.9 Relationship to Kernel Memory Allocator 1347c478bd9Sstevel@tonic-gate * ------------------------------------------- 1357c478bd9Sstevel@tonic-gate * Every kmem cache has a vmem arena as its slab supplier. The kernel memory 1367c478bd9Sstevel@tonic-gate * allocator uses vmem_alloc() and vmem_free() to create and destroy slabs. 1377c478bd9Sstevel@tonic-gate * 1387c478bd9Sstevel@tonic-gate * 1397c478bd9Sstevel@tonic-gate * 2. Implementation 1407c478bd9Sstevel@tonic-gate * ----------------- 1417c478bd9Sstevel@tonic-gate * 1427c478bd9Sstevel@tonic-gate * 2.1 Segment lists and markers 1437c478bd9Sstevel@tonic-gate * ----------------------------- 1447c478bd9Sstevel@tonic-gate * The segment structure (vmem_seg_t) contains two doubly-linked lists. 1457c478bd9Sstevel@tonic-gate * 1467c478bd9Sstevel@tonic-gate * The arena list (vs_anext/vs_aprev) links all segments in the arena. 1477c478bd9Sstevel@tonic-gate * In addition to the allocated and free segments, the arena contains 1487c478bd9Sstevel@tonic-gate * special marker segments at span boundaries. Span markers simplify 1497c478bd9Sstevel@tonic-gate * coalescing and importing logic by making it easy to tell both when 1507c478bd9Sstevel@tonic-gate * we're at a span boundary (so we don't coalesce across it), and when 1517c478bd9Sstevel@tonic-gate * a span is completely free (its neighbors will both be span markers). 1527c478bd9Sstevel@tonic-gate * 1537c478bd9Sstevel@tonic-gate * Imported spans will have vs_import set. 1547c478bd9Sstevel@tonic-gate * 1557c478bd9Sstevel@tonic-gate * The next-of-kin list (vs_knext/vs_kprev) links segments of the same type: 1567c478bd9Sstevel@tonic-gate * (1) for allocated segments, vs_knext is the hash chain linkage; 1577c478bd9Sstevel@tonic-gate * (2) for free segments, vs_knext is the freelist linkage; 1587c478bd9Sstevel@tonic-gate * (3) for span marker segments, vs_knext is the next span marker. 1597c478bd9Sstevel@tonic-gate * 1607c478bd9Sstevel@tonic-gate * 2.2 Allocation hashing 1617c478bd9Sstevel@tonic-gate * ---------------------- 1627c478bd9Sstevel@tonic-gate * We maintain a hash table of all allocated segments, hashed by address. 1637c478bd9Sstevel@tonic-gate * This allows vmem_free() to discover the target segment in constant time. 1647c478bd9Sstevel@tonic-gate * vmem_update() periodically resizes hash tables to keep hash chains short. 1657c478bd9Sstevel@tonic-gate * 1667c478bd9Sstevel@tonic-gate * 2.3 Freelist management 1677c478bd9Sstevel@tonic-gate * ----------------------- 1687c478bd9Sstevel@tonic-gate * We maintain power-of-2 freelists for free segments, i.e. free segments 1697c478bd9Sstevel@tonic-gate * of size >= 2^n reside in vmp->vm_freelist[n]. To ensure constant-time 1707c478bd9Sstevel@tonic-gate * allocation, vmem_xalloc() looks not in the first freelist that *might* 1717c478bd9Sstevel@tonic-gate * satisfy the allocation, but in the first freelist that *definitely* 1727c478bd9Sstevel@tonic-gate * satisfies the allocation (unless VM_BESTFIT is specified, or all larger 1737c478bd9Sstevel@tonic-gate * freelists are empty). For example, a 1000-byte allocation will be 1747c478bd9Sstevel@tonic-gate * satisfied not from the 512..1023-byte freelist, whose members *might* 1757c478bd9Sstevel@tonic-gate * contains a 1000-byte segment, but from a 1024-byte or larger freelist, 1767c478bd9Sstevel@tonic-gate * the first member of which will *definitely* satisfy the allocation. 1777c478bd9Sstevel@tonic-gate * This ensures that vmem_xalloc() works in constant time. 1787c478bd9Sstevel@tonic-gate * 1797c478bd9Sstevel@tonic-gate * We maintain a bit map to determine quickly which freelists are non-empty. 1807c478bd9Sstevel@tonic-gate * vmp->vm_freemap & (1 << n) is non-zero iff vmp->vm_freelist[n] is non-empty. 1817c478bd9Sstevel@tonic-gate * 1827c478bd9Sstevel@tonic-gate * The different freelists are linked together into one large freelist, 1837c478bd9Sstevel@tonic-gate * with the freelist heads serving as markers. Freelist markers simplify 1847c478bd9Sstevel@tonic-gate * the maintenance of vm_freemap by making it easy to tell when we're taking 1857c478bd9Sstevel@tonic-gate * the last member of a freelist (both of its neighbors will be markers). 1867c478bd9Sstevel@tonic-gate * 1877c478bd9Sstevel@tonic-gate * 2.4 Vmem Locking 1887c478bd9Sstevel@tonic-gate * ---------------- 1897c478bd9Sstevel@tonic-gate * For simplicity, all arena state is protected by a per-arena lock. 1907c478bd9Sstevel@tonic-gate * For very hot arenas, use quantum caching for scalability. 1917c478bd9Sstevel@tonic-gate * 1927c478bd9Sstevel@tonic-gate * 2.5 Vmem Population 1937c478bd9Sstevel@tonic-gate * ------------------- 1947c478bd9Sstevel@tonic-gate * Any internal vmem routine that might need to allocate new segment 1957c478bd9Sstevel@tonic-gate * structures must prepare in advance by calling vmem_populate(), which 1967c478bd9Sstevel@tonic-gate * will preallocate enough vmem_seg_t's to get is through the entire 1977c478bd9Sstevel@tonic-gate * operation without dropping the arena lock. 1987c478bd9Sstevel@tonic-gate * 1997c478bd9Sstevel@tonic-gate * 2.6 Auditing 2007c478bd9Sstevel@tonic-gate * ------------ 2017c478bd9Sstevel@tonic-gate * If KMF_AUDIT is set in kmem_flags, we audit vmem allocations as well. 2027c478bd9Sstevel@tonic-gate * Since virtual addresses cannot be scribbled on, there is no equivalent 2037c478bd9Sstevel@tonic-gate * in vmem to redzone checking, deadbeef, or other kmem debugging features. 2047c478bd9Sstevel@tonic-gate * Moreover, we do not audit frees because segment coalescing destroys the 2057c478bd9Sstevel@tonic-gate * association between an address and its segment structure. Auditing is 2067c478bd9Sstevel@tonic-gate * thus intended primarily to keep track of who's consuming the arena. 2077c478bd9Sstevel@tonic-gate * Debugging support could certainly be extended in the future if it proves 2087c478bd9Sstevel@tonic-gate * necessary, but we do so much live checking via the allocation hash table 2097c478bd9Sstevel@tonic-gate * that even non-DEBUG systems get quite a bit of sanity checking already. 2107c478bd9Sstevel@tonic-gate */ 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate #include <sys/vmem_impl.h> 2137c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 2147c478bd9Sstevel@tonic-gate #include <sys/kstat.h> 2157c478bd9Sstevel@tonic-gate #include <sys/param.h> 2167c478bd9Sstevel@tonic-gate #include <sys/systm.h> 2177c478bd9Sstevel@tonic-gate #include <sys/atomic.h> 2187c478bd9Sstevel@tonic-gate #include <sys/bitmap.h> 2197c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 2207c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 2217c478bd9Sstevel@tonic-gate #include <sys/debug.h> 2227c478bd9Sstevel@tonic-gate #include <sys/panic.h> 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate #define VMEM_INITIAL 10 /* early vmem arenas */ 2257c478bd9Sstevel@tonic-gate #define VMEM_SEG_INITIAL 200 /* early segments */ 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate /* 2287c478bd9Sstevel@tonic-gate * Adding a new span to an arena requires two segment structures: one to 2297c478bd9Sstevel@tonic-gate * represent the span, and one to represent the free segment it contains. 2307c478bd9Sstevel@tonic-gate */ 2317c478bd9Sstevel@tonic-gate #define VMEM_SEGS_PER_SPAN_CREATE 2 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate /* 2347c478bd9Sstevel@tonic-gate * Allocating a piece of an existing segment requires 0-2 segment structures 2357c478bd9Sstevel@tonic-gate * depending on how much of the segment we're allocating. 2367c478bd9Sstevel@tonic-gate * 2377c478bd9Sstevel@tonic-gate * To allocate the entire segment, no new segment structures are needed; we 2387c478bd9Sstevel@tonic-gate * simply move the existing segment structure from the freelist to the 2397c478bd9Sstevel@tonic-gate * allocation hash table. 2407c478bd9Sstevel@tonic-gate * 2417c478bd9Sstevel@tonic-gate * To allocate a piece from the left or right end of the segment, we must 2427c478bd9Sstevel@tonic-gate * split the segment into two pieces (allocated part and remainder), so we 2437c478bd9Sstevel@tonic-gate * need one new segment structure to represent the remainder. 2447c478bd9Sstevel@tonic-gate * 2457c478bd9Sstevel@tonic-gate * To allocate from the middle of a segment, we need two new segment strucures 2467c478bd9Sstevel@tonic-gate * to represent the remainders on either side of the allocated part. 2477c478bd9Sstevel@tonic-gate */ 2487c478bd9Sstevel@tonic-gate #define VMEM_SEGS_PER_EXACT_ALLOC 0 2497c478bd9Sstevel@tonic-gate #define VMEM_SEGS_PER_LEFT_ALLOC 1 2507c478bd9Sstevel@tonic-gate #define VMEM_SEGS_PER_RIGHT_ALLOC 1 2517c478bd9Sstevel@tonic-gate #define VMEM_SEGS_PER_MIDDLE_ALLOC 2 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate /* 2547c478bd9Sstevel@tonic-gate * vmem_populate() preallocates segment structures for vmem to do its work. 2557c478bd9Sstevel@tonic-gate * It must preallocate enough for the worst case, which is when we must import 2567c478bd9Sstevel@tonic-gate * a new span and then allocate from the middle of it. 2577c478bd9Sstevel@tonic-gate */ 2587c478bd9Sstevel@tonic-gate #define VMEM_SEGS_PER_ALLOC_MAX \ 2597c478bd9Sstevel@tonic-gate (VMEM_SEGS_PER_SPAN_CREATE + VMEM_SEGS_PER_MIDDLE_ALLOC) 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate /* 2627c478bd9Sstevel@tonic-gate * The segment structures themselves are allocated from vmem_seg_arena, so 2637c478bd9Sstevel@tonic-gate * we have a recursion problem when vmem_seg_arena needs to populate itself. 2647c478bd9Sstevel@tonic-gate * We address this by working out the maximum number of segment structures 2657c478bd9Sstevel@tonic-gate * this act will require, and multiplying by the maximum number of threads 2667c478bd9Sstevel@tonic-gate * that we'll allow to do it simultaneously. 2677c478bd9Sstevel@tonic-gate * 2687c478bd9Sstevel@tonic-gate * The worst-case segment consumption to populate vmem_seg_arena is as 2697c478bd9Sstevel@tonic-gate * follows (depicted as a stack trace to indicate why events are occurring): 2707c478bd9Sstevel@tonic-gate * 2717c478bd9Sstevel@tonic-gate * (In order to lower the fragmentation in the heap_arena, we specify a 2727c478bd9Sstevel@tonic-gate * minimum import size for the vmem_metadata_arena which is the same size 2737c478bd9Sstevel@tonic-gate * as the kmem_va quantum cache allocations. This causes the worst-case 2747c478bd9Sstevel@tonic-gate * allocation from the vmem_metadata_arena to be 3 segments.) 2757c478bd9Sstevel@tonic-gate * 2767c478bd9Sstevel@tonic-gate * vmem_alloc(vmem_seg_arena) -> 2 segs (span create + exact alloc) 2777c478bd9Sstevel@tonic-gate * segkmem_alloc(vmem_metadata_arena) 2787c478bd9Sstevel@tonic-gate * vmem_alloc(vmem_metadata_arena) -> 3 segs (span create + left alloc) 2797c478bd9Sstevel@tonic-gate * vmem_alloc(heap_arena) -> 1 seg (left alloc) 2807c478bd9Sstevel@tonic-gate * page_create() 2817c478bd9Sstevel@tonic-gate * hat_memload() 2827c478bd9Sstevel@tonic-gate * kmem_cache_alloc() 2837c478bd9Sstevel@tonic-gate * kmem_slab_create() 2847c478bd9Sstevel@tonic-gate * vmem_alloc(hat_memload_arena) -> 2 segs (span create + exact alloc) 2857c478bd9Sstevel@tonic-gate * segkmem_alloc(heap_arena) 2867c478bd9Sstevel@tonic-gate * vmem_alloc(heap_arena) -> 1 seg (left alloc) 2877c478bd9Sstevel@tonic-gate * page_create() 2887c478bd9Sstevel@tonic-gate * hat_memload() -> (hat layer won't recurse further) 2897c478bd9Sstevel@tonic-gate * 2907c478bd9Sstevel@tonic-gate * The worst-case consumption for each arena is 3 segment structures. 2917c478bd9Sstevel@tonic-gate * Of course, a 3-seg reserve could easily be blown by multiple threads. 2927c478bd9Sstevel@tonic-gate * Therefore, we serialize all allocations from vmem_seg_arena (which is OK 2937c478bd9Sstevel@tonic-gate * because they're rare). We cannot allow a non-blocking allocation to get 2947c478bd9Sstevel@tonic-gate * tied up behind a blocking allocation, however, so we use separate locks 29510b95589Sbonwick * for VM_SLEEP and VM_NOSLEEP allocations. Similarly, VM_PUSHPAGE allocations 29610b95589Sbonwick * must not block behind ordinary VM_SLEEPs. In addition, if the system is 2977c478bd9Sstevel@tonic-gate * panicking then we must keep enough resources for panic_thread to do its 29810b95589Sbonwick * work. Thus we have at most four threads trying to allocate from 2997c478bd9Sstevel@tonic-gate * vmem_seg_arena, and each thread consumes at most three segment structures, 30010b95589Sbonwick * so we must maintain a 12-seg reserve. 3017c478bd9Sstevel@tonic-gate */ 30210b95589Sbonwick #define VMEM_POPULATE_RESERVE 12 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate /* 3057c478bd9Sstevel@tonic-gate * vmem_populate() ensures that each arena has VMEM_MINFREE seg structures 3067c478bd9Sstevel@tonic-gate * so that it can satisfy the worst-case allocation *and* participate in 3077c478bd9Sstevel@tonic-gate * worst-case allocation from vmem_seg_arena. 3087c478bd9Sstevel@tonic-gate */ 3097c478bd9Sstevel@tonic-gate #define VMEM_MINFREE (VMEM_POPULATE_RESERVE + VMEM_SEGS_PER_ALLOC_MAX) 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate static vmem_t vmem0[VMEM_INITIAL]; 3127c478bd9Sstevel@tonic-gate static vmem_t *vmem_populator[VMEM_INITIAL]; 3137c478bd9Sstevel@tonic-gate static uint32_t vmem_id; 3147c478bd9Sstevel@tonic-gate static uint32_t vmem_populators; 3157c478bd9Sstevel@tonic-gate static vmem_seg_t vmem_seg0[VMEM_SEG_INITIAL]; 3167c478bd9Sstevel@tonic-gate static vmem_seg_t *vmem_segfree; 3177c478bd9Sstevel@tonic-gate static kmutex_t vmem_list_lock; 3187c478bd9Sstevel@tonic-gate static kmutex_t vmem_segfree_lock; 3197c478bd9Sstevel@tonic-gate static kmutex_t vmem_sleep_lock; 3207c478bd9Sstevel@tonic-gate static kmutex_t vmem_nosleep_lock; 32110b95589Sbonwick static kmutex_t vmem_pushpage_lock; 3227c478bd9Sstevel@tonic-gate static kmutex_t vmem_panic_lock; 3237c478bd9Sstevel@tonic-gate static vmem_t *vmem_list; 3247c478bd9Sstevel@tonic-gate static vmem_t *vmem_metadata_arena; 3257c478bd9Sstevel@tonic-gate static vmem_t *vmem_seg_arena; 3267c478bd9Sstevel@tonic-gate static vmem_t *vmem_hash_arena; 3277c478bd9Sstevel@tonic-gate static vmem_t *vmem_vmem_arena; 3287c478bd9Sstevel@tonic-gate static long vmem_update_interval = 15; /* vmem_update() every 15 seconds */ 3297c478bd9Sstevel@tonic-gate uint32_t vmem_mtbf; /* mean time between failures [default: off] */ 3307c478bd9Sstevel@tonic-gate size_t vmem_seg_size = sizeof (vmem_seg_t); 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate static vmem_kstat_t vmem_kstat_template = { 3337c478bd9Sstevel@tonic-gate { "mem_inuse", KSTAT_DATA_UINT64 }, 3347c478bd9Sstevel@tonic-gate { "mem_import", KSTAT_DATA_UINT64 }, 3357c478bd9Sstevel@tonic-gate { "mem_total", KSTAT_DATA_UINT64 }, 3367c478bd9Sstevel@tonic-gate { "vmem_source", KSTAT_DATA_UINT32 }, 3377c478bd9Sstevel@tonic-gate { "alloc", KSTAT_DATA_UINT64 }, 3387c478bd9Sstevel@tonic-gate { "free", KSTAT_DATA_UINT64 }, 3397c478bd9Sstevel@tonic-gate { "wait", KSTAT_DATA_UINT64 }, 3407c478bd9Sstevel@tonic-gate { "fail", KSTAT_DATA_UINT64 }, 3417c478bd9Sstevel@tonic-gate { "lookup", KSTAT_DATA_UINT64 }, 3427c478bd9Sstevel@tonic-gate { "search", KSTAT_DATA_UINT64 }, 3437c478bd9Sstevel@tonic-gate { "populate_wait", KSTAT_DATA_UINT64 }, 3447c478bd9Sstevel@tonic-gate { "populate_fail", KSTAT_DATA_UINT64 }, 3457c478bd9Sstevel@tonic-gate { "contains", KSTAT_DATA_UINT64 }, 3467c478bd9Sstevel@tonic-gate { "contains_search", KSTAT_DATA_UINT64 }, 3477c478bd9Sstevel@tonic-gate }; 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate /* 3507c478bd9Sstevel@tonic-gate * Insert/delete from arena list (type 'a') or next-of-kin list (type 'k'). 3517c478bd9Sstevel@tonic-gate */ 3527c478bd9Sstevel@tonic-gate #define VMEM_INSERT(vprev, vsp, type) \ 3537c478bd9Sstevel@tonic-gate { \ 3547c478bd9Sstevel@tonic-gate vmem_seg_t *vnext = (vprev)->vs_##type##next; \ 3557c478bd9Sstevel@tonic-gate (vsp)->vs_##type##next = (vnext); \ 3567c478bd9Sstevel@tonic-gate (vsp)->vs_##type##prev = (vprev); \ 3577c478bd9Sstevel@tonic-gate (vprev)->vs_##type##next = (vsp); \ 3587c478bd9Sstevel@tonic-gate (vnext)->vs_##type##prev = (vsp); \ 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate #define VMEM_DELETE(vsp, type) \ 3627c478bd9Sstevel@tonic-gate { \ 3637c478bd9Sstevel@tonic-gate vmem_seg_t *vprev = (vsp)->vs_##type##prev; \ 3647c478bd9Sstevel@tonic-gate vmem_seg_t *vnext = (vsp)->vs_##type##next; \ 3657c478bd9Sstevel@tonic-gate (vprev)->vs_##type##next = (vnext); \ 3667c478bd9Sstevel@tonic-gate (vnext)->vs_##type##prev = (vprev); \ 3677c478bd9Sstevel@tonic-gate } 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate /* 3707c478bd9Sstevel@tonic-gate * Get a vmem_seg_t from the global segfree list. 3717c478bd9Sstevel@tonic-gate */ 3727c478bd9Sstevel@tonic-gate static vmem_seg_t * 3737c478bd9Sstevel@tonic-gate vmem_getseg_global(void) 3747c478bd9Sstevel@tonic-gate { 3757c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate mutex_enter(&vmem_segfree_lock); 3787c478bd9Sstevel@tonic-gate if ((vsp = vmem_segfree) != NULL) 3797c478bd9Sstevel@tonic-gate vmem_segfree = vsp->vs_knext; 3807c478bd9Sstevel@tonic-gate mutex_exit(&vmem_segfree_lock); 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate return (vsp); 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate /* 3867c478bd9Sstevel@tonic-gate * Put a vmem_seg_t on the global segfree list. 3877c478bd9Sstevel@tonic-gate */ 3887c478bd9Sstevel@tonic-gate static void 3897c478bd9Sstevel@tonic-gate vmem_putseg_global(vmem_seg_t *vsp) 3907c478bd9Sstevel@tonic-gate { 3917c478bd9Sstevel@tonic-gate mutex_enter(&vmem_segfree_lock); 3927c478bd9Sstevel@tonic-gate vsp->vs_knext = vmem_segfree; 3937c478bd9Sstevel@tonic-gate vmem_segfree = vsp; 3947c478bd9Sstevel@tonic-gate mutex_exit(&vmem_segfree_lock); 3957c478bd9Sstevel@tonic-gate } 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate /* 3987c478bd9Sstevel@tonic-gate * Get a vmem_seg_t from vmp's segfree list. 3997c478bd9Sstevel@tonic-gate */ 4007c478bd9Sstevel@tonic-gate static vmem_seg_t * 4017c478bd9Sstevel@tonic-gate vmem_getseg(vmem_t *vmp) 4027c478bd9Sstevel@tonic-gate { 4037c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate ASSERT(vmp->vm_nsegfree > 0); 4067c478bd9Sstevel@tonic-gate 4077c478bd9Sstevel@tonic-gate vsp = vmp->vm_segfree; 4087c478bd9Sstevel@tonic-gate vmp->vm_segfree = vsp->vs_knext; 4097c478bd9Sstevel@tonic-gate vmp->vm_nsegfree--; 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate return (vsp); 4127c478bd9Sstevel@tonic-gate } 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate /* 4157c478bd9Sstevel@tonic-gate * Put a vmem_seg_t on vmp's segfree list. 4167c478bd9Sstevel@tonic-gate */ 4177c478bd9Sstevel@tonic-gate static void 4187c478bd9Sstevel@tonic-gate vmem_putseg(vmem_t *vmp, vmem_seg_t *vsp) 4197c478bd9Sstevel@tonic-gate { 4207c478bd9Sstevel@tonic-gate vsp->vs_knext = vmp->vm_segfree; 4217c478bd9Sstevel@tonic-gate vmp->vm_segfree = vsp; 4227c478bd9Sstevel@tonic-gate vmp->vm_nsegfree++; 4237c478bd9Sstevel@tonic-gate } 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate /* 4267c478bd9Sstevel@tonic-gate * Add vsp to the appropriate freelist. 4277c478bd9Sstevel@tonic-gate */ 4287c478bd9Sstevel@tonic-gate static void 4297c478bd9Sstevel@tonic-gate vmem_freelist_insert(vmem_t *vmp, vmem_seg_t *vsp) 4307c478bd9Sstevel@tonic-gate { 4317c478bd9Sstevel@tonic-gate vmem_seg_t *vprev; 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate ASSERT(*VMEM_HASH(vmp, vsp->vs_start) != vsp); 4347c478bd9Sstevel@tonic-gate 4357c478bd9Sstevel@tonic-gate vprev = (vmem_seg_t *)&vmp->vm_freelist[highbit(VS_SIZE(vsp)) - 1]; 4367c478bd9Sstevel@tonic-gate vsp->vs_type = VMEM_FREE; 4377c478bd9Sstevel@tonic-gate vmp->vm_freemap |= VS_SIZE(vprev); 4387c478bd9Sstevel@tonic-gate VMEM_INSERT(vprev, vsp, k); 4397c478bd9Sstevel@tonic-gate 4407c478bd9Sstevel@tonic-gate cv_broadcast(&vmp->vm_cv); 4417c478bd9Sstevel@tonic-gate } 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate /* 4447c478bd9Sstevel@tonic-gate * Take vsp from the freelist. 4457c478bd9Sstevel@tonic-gate */ 4467c478bd9Sstevel@tonic-gate static void 4477c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmem_t *vmp, vmem_seg_t *vsp) 4487c478bd9Sstevel@tonic-gate { 4497c478bd9Sstevel@tonic-gate ASSERT(*VMEM_HASH(vmp, vsp->vs_start) != vsp); 4507c478bd9Sstevel@tonic-gate ASSERT(vsp->vs_type == VMEM_FREE); 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate if (vsp->vs_knext->vs_start == 0 && vsp->vs_kprev->vs_start == 0) { 4537c478bd9Sstevel@tonic-gate /* 4547c478bd9Sstevel@tonic-gate * The segments on both sides of 'vsp' are freelist heads, 4557c478bd9Sstevel@tonic-gate * so taking vsp leaves the freelist at vsp->vs_kprev empty. 4567c478bd9Sstevel@tonic-gate */ 4577c478bd9Sstevel@tonic-gate ASSERT(vmp->vm_freemap & VS_SIZE(vsp->vs_kprev)); 4587c478bd9Sstevel@tonic-gate vmp->vm_freemap ^= VS_SIZE(vsp->vs_kprev); 4597c478bd9Sstevel@tonic-gate } 4607c478bd9Sstevel@tonic-gate VMEM_DELETE(vsp, k); 4617c478bd9Sstevel@tonic-gate } 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate /* 4647c478bd9Sstevel@tonic-gate * Add vsp to the allocated-segment hash table and update kstats. 4657c478bd9Sstevel@tonic-gate */ 4667c478bd9Sstevel@tonic-gate static void 4677c478bd9Sstevel@tonic-gate vmem_hash_insert(vmem_t *vmp, vmem_seg_t *vsp) 4687c478bd9Sstevel@tonic-gate { 4697c478bd9Sstevel@tonic-gate vmem_seg_t **bucket; 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate vsp->vs_type = VMEM_ALLOC; 4727c478bd9Sstevel@tonic-gate bucket = VMEM_HASH(vmp, vsp->vs_start); 4737c478bd9Sstevel@tonic-gate vsp->vs_knext = *bucket; 4747c478bd9Sstevel@tonic-gate *bucket = vsp; 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate if (vmem_seg_size == sizeof (vmem_seg_t)) { 4777c478bd9Sstevel@tonic-gate vsp->vs_depth = (uint8_t)getpcstack(vsp->vs_stack, 4787c478bd9Sstevel@tonic-gate VMEM_STACK_DEPTH); 4797c478bd9Sstevel@tonic-gate vsp->vs_thread = curthread; 4807c478bd9Sstevel@tonic-gate vsp->vs_timestamp = gethrtime(); 4817c478bd9Sstevel@tonic-gate } else { 4827c478bd9Sstevel@tonic-gate vsp->vs_depth = 0; 4837c478bd9Sstevel@tonic-gate } 4847c478bd9Sstevel@tonic-gate 4857c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_alloc.value.ui64++; 4867c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_mem_inuse.value.ui64 += VS_SIZE(vsp); 4877c478bd9Sstevel@tonic-gate } 4887c478bd9Sstevel@tonic-gate 4897c478bd9Sstevel@tonic-gate /* 4907c478bd9Sstevel@tonic-gate * Remove vsp from the allocated-segment hash table and update kstats. 4917c478bd9Sstevel@tonic-gate */ 4927c478bd9Sstevel@tonic-gate static vmem_seg_t * 4937c478bd9Sstevel@tonic-gate vmem_hash_delete(vmem_t *vmp, uintptr_t addr, size_t size) 4947c478bd9Sstevel@tonic-gate { 4957c478bd9Sstevel@tonic-gate vmem_seg_t *vsp, **prev_vspp; 4967c478bd9Sstevel@tonic-gate 4977c478bd9Sstevel@tonic-gate prev_vspp = VMEM_HASH(vmp, addr); 4987c478bd9Sstevel@tonic-gate while ((vsp = *prev_vspp) != NULL) { 4997c478bd9Sstevel@tonic-gate if (vsp->vs_start == addr) { 5007c478bd9Sstevel@tonic-gate *prev_vspp = vsp->vs_knext; 5017c478bd9Sstevel@tonic-gate break; 5027c478bd9Sstevel@tonic-gate } 5037c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_lookup.value.ui64++; 5047c478bd9Sstevel@tonic-gate prev_vspp = &vsp->vs_knext; 5057c478bd9Sstevel@tonic-gate } 5067c478bd9Sstevel@tonic-gate 5077c478bd9Sstevel@tonic-gate if (vsp == NULL) 5087c478bd9Sstevel@tonic-gate panic("vmem_hash_delete(%p, %lx, %lu): bad free", 509903a11ebSrh87107 (void *)vmp, addr, size); 5107c478bd9Sstevel@tonic-gate if (VS_SIZE(vsp) != size) 5117c478bd9Sstevel@tonic-gate panic("vmem_hash_delete(%p, %lx, %lu): wrong size (expect %lu)", 512903a11ebSrh87107 (void *)vmp, addr, size, VS_SIZE(vsp)); 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_free.value.ui64++; 5157c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_mem_inuse.value.ui64 -= size; 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate return (vsp); 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate 5207c478bd9Sstevel@tonic-gate /* 5217c478bd9Sstevel@tonic-gate * Create a segment spanning the range [start, end) and add it to the arena. 5227c478bd9Sstevel@tonic-gate */ 5237c478bd9Sstevel@tonic-gate static vmem_seg_t * 5247c478bd9Sstevel@tonic-gate vmem_seg_create(vmem_t *vmp, vmem_seg_t *vprev, uintptr_t start, uintptr_t end) 5257c478bd9Sstevel@tonic-gate { 5267c478bd9Sstevel@tonic-gate vmem_seg_t *newseg = vmem_getseg(vmp); 5277c478bd9Sstevel@tonic-gate 5287c478bd9Sstevel@tonic-gate newseg->vs_start = start; 5297c478bd9Sstevel@tonic-gate newseg->vs_end = end; 5307c478bd9Sstevel@tonic-gate newseg->vs_type = 0; 5317c478bd9Sstevel@tonic-gate newseg->vs_import = 0; 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate VMEM_INSERT(vprev, newseg, a); 5347c478bd9Sstevel@tonic-gate 5357c478bd9Sstevel@tonic-gate return (newseg); 5367c478bd9Sstevel@tonic-gate } 5377c478bd9Sstevel@tonic-gate 5387c478bd9Sstevel@tonic-gate /* 5397c478bd9Sstevel@tonic-gate * Remove segment vsp from the arena. 5407c478bd9Sstevel@tonic-gate */ 5417c478bd9Sstevel@tonic-gate static void 5427c478bd9Sstevel@tonic-gate vmem_seg_destroy(vmem_t *vmp, vmem_seg_t *vsp) 5437c478bd9Sstevel@tonic-gate { 5447c478bd9Sstevel@tonic-gate ASSERT(vsp->vs_type != VMEM_ROTOR); 5457c478bd9Sstevel@tonic-gate VMEM_DELETE(vsp, a); 5467c478bd9Sstevel@tonic-gate 5477c478bd9Sstevel@tonic-gate vmem_putseg(vmp, vsp); 5487c478bd9Sstevel@tonic-gate } 5497c478bd9Sstevel@tonic-gate 5507c478bd9Sstevel@tonic-gate /* 5517c478bd9Sstevel@tonic-gate * Add the span [vaddr, vaddr + size) to vmp and update kstats. 5527c478bd9Sstevel@tonic-gate */ 5537c478bd9Sstevel@tonic-gate static vmem_seg_t * 5547c478bd9Sstevel@tonic-gate vmem_span_create(vmem_t *vmp, void *vaddr, size_t size, uint8_t import) 5557c478bd9Sstevel@tonic-gate { 5567c478bd9Sstevel@tonic-gate vmem_seg_t *newseg, *span; 5577c478bd9Sstevel@tonic-gate uintptr_t start = (uintptr_t)vaddr; 5587c478bd9Sstevel@tonic-gate uintptr_t end = start + size; 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&vmp->vm_lock)); 5617c478bd9Sstevel@tonic-gate 5627c478bd9Sstevel@tonic-gate if ((start | end) & (vmp->vm_quantum - 1)) 5637c478bd9Sstevel@tonic-gate panic("vmem_span_create(%p, %p, %lu): misaligned", 564903a11ebSrh87107 (void *)vmp, vaddr, size); 5657c478bd9Sstevel@tonic-gate 5667c478bd9Sstevel@tonic-gate span = vmem_seg_create(vmp, vmp->vm_seg0.vs_aprev, start, end); 5677c478bd9Sstevel@tonic-gate span->vs_type = VMEM_SPAN; 5687c478bd9Sstevel@tonic-gate span->vs_import = import; 5697c478bd9Sstevel@tonic-gate VMEM_INSERT(vmp->vm_seg0.vs_kprev, span, k); 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate newseg = vmem_seg_create(vmp, span, start, end); 5727c478bd9Sstevel@tonic-gate vmem_freelist_insert(vmp, newseg); 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate if (import) 5757c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_mem_import.value.ui64 += size; 5767c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_mem_total.value.ui64 += size; 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate return (newseg); 5797c478bd9Sstevel@tonic-gate } 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate /* 5827c478bd9Sstevel@tonic-gate * Remove span vsp from vmp and update kstats. 5837c478bd9Sstevel@tonic-gate */ 5847c478bd9Sstevel@tonic-gate static void 5857c478bd9Sstevel@tonic-gate vmem_span_destroy(vmem_t *vmp, vmem_seg_t *vsp) 5867c478bd9Sstevel@tonic-gate { 5877c478bd9Sstevel@tonic-gate vmem_seg_t *span = vsp->vs_aprev; 5887c478bd9Sstevel@tonic-gate size_t size = VS_SIZE(vsp); 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&vmp->vm_lock)); 5917c478bd9Sstevel@tonic-gate ASSERT(span->vs_type == VMEM_SPAN); 5927c478bd9Sstevel@tonic-gate 5937c478bd9Sstevel@tonic-gate if (span->vs_import) 5947c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_mem_import.value.ui64 -= size; 5957c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_mem_total.value.ui64 -= size; 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate VMEM_DELETE(span, k); 5987c478bd9Sstevel@tonic-gate 5997c478bd9Sstevel@tonic-gate vmem_seg_destroy(vmp, vsp); 6007c478bd9Sstevel@tonic-gate vmem_seg_destroy(vmp, span); 6017c478bd9Sstevel@tonic-gate } 6027c478bd9Sstevel@tonic-gate 6037c478bd9Sstevel@tonic-gate /* 6047c478bd9Sstevel@tonic-gate * Allocate the subrange [addr, addr + size) from segment vsp. 6057c478bd9Sstevel@tonic-gate * If there are leftovers on either side, place them on the freelist. 6067c478bd9Sstevel@tonic-gate * Returns a pointer to the segment representing [addr, addr + size). 6077c478bd9Sstevel@tonic-gate */ 6087c478bd9Sstevel@tonic-gate static vmem_seg_t * 6097c478bd9Sstevel@tonic-gate vmem_seg_alloc(vmem_t *vmp, vmem_seg_t *vsp, uintptr_t addr, size_t size) 6107c478bd9Sstevel@tonic-gate { 6117c478bd9Sstevel@tonic-gate uintptr_t vs_start = vsp->vs_start; 6127c478bd9Sstevel@tonic-gate uintptr_t vs_end = vsp->vs_end; 6137c478bd9Sstevel@tonic-gate size_t vs_size = vs_end - vs_start; 6147c478bd9Sstevel@tonic-gate size_t realsize = P2ROUNDUP(size, vmp->vm_quantum); 6157c478bd9Sstevel@tonic-gate uintptr_t addr_end = addr + realsize; 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate ASSERT(P2PHASE(vs_start, vmp->vm_quantum) == 0); 6187c478bd9Sstevel@tonic-gate ASSERT(P2PHASE(addr, vmp->vm_quantum) == 0); 6197c478bd9Sstevel@tonic-gate ASSERT(vsp->vs_type == VMEM_FREE); 6207c478bd9Sstevel@tonic-gate ASSERT(addr >= vs_start && addr_end - 1 <= vs_end - 1); 6217c478bd9Sstevel@tonic-gate ASSERT(addr - 1 <= addr_end - 1); 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate /* 6247c478bd9Sstevel@tonic-gate * If we're allocating from the start of the segment, and the 6257c478bd9Sstevel@tonic-gate * remainder will be on the same freelist, we can save quite 6267c478bd9Sstevel@tonic-gate * a bit of work. 6277c478bd9Sstevel@tonic-gate */ 6287c478bd9Sstevel@tonic-gate if (P2SAMEHIGHBIT(vs_size, vs_size - realsize) && addr == vs_start) { 6297c478bd9Sstevel@tonic-gate ASSERT(highbit(vs_size) == highbit(vs_size - realsize)); 6307c478bd9Sstevel@tonic-gate vsp->vs_start = addr_end; 6317c478bd9Sstevel@tonic-gate vsp = vmem_seg_create(vmp, vsp->vs_aprev, addr, addr + size); 6327c478bd9Sstevel@tonic-gate vmem_hash_insert(vmp, vsp); 6337c478bd9Sstevel@tonic-gate return (vsp); 6347c478bd9Sstevel@tonic-gate } 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, vsp); 6377c478bd9Sstevel@tonic-gate 6387c478bd9Sstevel@tonic-gate if (vs_end != addr_end) 6397c478bd9Sstevel@tonic-gate vmem_freelist_insert(vmp, 6407c478bd9Sstevel@tonic-gate vmem_seg_create(vmp, vsp, addr_end, vs_end)); 6417c478bd9Sstevel@tonic-gate 6427c478bd9Sstevel@tonic-gate if (vs_start != addr) 6437c478bd9Sstevel@tonic-gate vmem_freelist_insert(vmp, 6447c478bd9Sstevel@tonic-gate vmem_seg_create(vmp, vsp->vs_aprev, vs_start, addr)); 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate vsp->vs_start = addr; 6477c478bd9Sstevel@tonic-gate vsp->vs_end = addr + size; 6487c478bd9Sstevel@tonic-gate 6497c478bd9Sstevel@tonic-gate vmem_hash_insert(vmp, vsp); 6507c478bd9Sstevel@tonic-gate return (vsp); 6517c478bd9Sstevel@tonic-gate } 6527c478bd9Sstevel@tonic-gate 6537c478bd9Sstevel@tonic-gate /* 6547c478bd9Sstevel@tonic-gate * Returns 1 if we are populating, 0 otherwise. 6557c478bd9Sstevel@tonic-gate * Call it if we want to prevent recursion from HAT. 6567c478bd9Sstevel@tonic-gate */ 6577c478bd9Sstevel@tonic-gate int 6587c478bd9Sstevel@tonic-gate vmem_is_populator() 6597c478bd9Sstevel@tonic-gate { 6607c478bd9Sstevel@tonic-gate return (mutex_owner(&vmem_sleep_lock) == curthread || 6617c478bd9Sstevel@tonic-gate mutex_owner(&vmem_nosleep_lock) == curthread || 66210b95589Sbonwick mutex_owner(&vmem_pushpage_lock) == curthread || 6637c478bd9Sstevel@tonic-gate mutex_owner(&vmem_panic_lock) == curthread); 6647c478bd9Sstevel@tonic-gate } 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate /* 6677c478bd9Sstevel@tonic-gate * Populate vmp's segfree list with VMEM_MINFREE vmem_seg_t structures. 6687c478bd9Sstevel@tonic-gate */ 6697c478bd9Sstevel@tonic-gate static int 6707c478bd9Sstevel@tonic-gate vmem_populate(vmem_t *vmp, int vmflag) 6717c478bd9Sstevel@tonic-gate { 6727c478bd9Sstevel@tonic-gate char *p; 6737c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 6747c478bd9Sstevel@tonic-gate ssize_t nseg; 6757c478bd9Sstevel@tonic-gate size_t size; 6767c478bd9Sstevel@tonic-gate kmutex_t *lp; 6777c478bd9Sstevel@tonic-gate int i; 6787c478bd9Sstevel@tonic-gate 6797c478bd9Sstevel@tonic-gate while (vmp->vm_nsegfree < VMEM_MINFREE && 6807c478bd9Sstevel@tonic-gate (vsp = vmem_getseg_global()) != NULL) 6817c478bd9Sstevel@tonic-gate vmem_putseg(vmp, vsp); 6827c478bd9Sstevel@tonic-gate 6837c478bd9Sstevel@tonic-gate if (vmp->vm_nsegfree >= VMEM_MINFREE) 6847c478bd9Sstevel@tonic-gate return (1); 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate /* 6877c478bd9Sstevel@tonic-gate * If we're already populating, tap the reserve. 6887c478bd9Sstevel@tonic-gate */ 6897c478bd9Sstevel@tonic-gate if (vmem_is_populator()) { 6907c478bd9Sstevel@tonic-gate ASSERT(vmp->vm_cflags & VMC_POPULATOR); 6917c478bd9Sstevel@tonic-gate return (1); 6927c478bd9Sstevel@tonic-gate } 6937c478bd9Sstevel@tonic-gate 6947c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 6957c478bd9Sstevel@tonic-gate 6967c478bd9Sstevel@tonic-gate if (panic_thread == curthread) 6977c478bd9Sstevel@tonic-gate lp = &vmem_panic_lock; 6987c478bd9Sstevel@tonic-gate else if (vmflag & VM_NOSLEEP) 6997c478bd9Sstevel@tonic-gate lp = &vmem_nosleep_lock; 70010b95589Sbonwick else if (vmflag & VM_PUSHPAGE) 70110b95589Sbonwick lp = &vmem_pushpage_lock; 7027c478bd9Sstevel@tonic-gate else 7037c478bd9Sstevel@tonic-gate lp = &vmem_sleep_lock; 7047c478bd9Sstevel@tonic-gate 7057c478bd9Sstevel@tonic-gate mutex_enter(lp); 7067c478bd9Sstevel@tonic-gate 7077c478bd9Sstevel@tonic-gate nseg = VMEM_MINFREE + vmem_populators * VMEM_POPULATE_RESERVE; 7087c478bd9Sstevel@tonic-gate size = P2ROUNDUP(nseg * vmem_seg_size, vmem_seg_arena->vm_quantum); 7097c478bd9Sstevel@tonic-gate nseg = size / vmem_seg_size; 7107c478bd9Sstevel@tonic-gate 7117c478bd9Sstevel@tonic-gate /* 7127c478bd9Sstevel@tonic-gate * The following vmem_alloc() may need to populate vmem_seg_arena 7137c478bd9Sstevel@tonic-gate * and all the things it imports from. When doing so, it will tap 7147c478bd9Sstevel@tonic-gate * each arena's reserve to prevent recursion (see the block comment 7157c478bd9Sstevel@tonic-gate * above the definition of VMEM_POPULATE_RESERVE). 7167c478bd9Sstevel@tonic-gate */ 7177c478bd9Sstevel@tonic-gate p = vmem_alloc(vmem_seg_arena, size, vmflag & VM_KMFLAGS); 7187c478bd9Sstevel@tonic-gate if (p == NULL) { 7197c478bd9Sstevel@tonic-gate mutex_exit(lp); 7207c478bd9Sstevel@tonic-gate mutex_enter(&vmp->vm_lock); 7217c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_populate_fail.value.ui64++; 7227c478bd9Sstevel@tonic-gate return (0); 7237c478bd9Sstevel@tonic-gate } 7247c478bd9Sstevel@tonic-gate 7257c478bd9Sstevel@tonic-gate /* 7267c478bd9Sstevel@tonic-gate * Restock the arenas that may have been depleted during population. 7277c478bd9Sstevel@tonic-gate */ 7287c478bd9Sstevel@tonic-gate for (i = 0; i < vmem_populators; i++) { 7297c478bd9Sstevel@tonic-gate mutex_enter(&vmem_populator[i]->vm_lock); 7307c478bd9Sstevel@tonic-gate while (vmem_populator[i]->vm_nsegfree < VMEM_POPULATE_RESERVE) 7317c478bd9Sstevel@tonic-gate vmem_putseg(vmem_populator[i], 7327c478bd9Sstevel@tonic-gate (vmem_seg_t *)(p + --nseg * vmem_seg_size)); 7337c478bd9Sstevel@tonic-gate mutex_exit(&vmem_populator[i]->vm_lock); 7347c478bd9Sstevel@tonic-gate } 7357c478bd9Sstevel@tonic-gate 7367c478bd9Sstevel@tonic-gate mutex_exit(lp); 7377c478bd9Sstevel@tonic-gate mutex_enter(&vmp->vm_lock); 7387c478bd9Sstevel@tonic-gate 7397c478bd9Sstevel@tonic-gate /* 7407c478bd9Sstevel@tonic-gate * Now take our own segments. 7417c478bd9Sstevel@tonic-gate */ 7427c478bd9Sstevel@tonic-gate ASSERT(nseg >= VMEM_MINFREE); 7437c478bd9Sstevel@tonic-gate while (vmp->vm_nsegfree < VMEM_MINFREE) 7447c478bd9Sstevel@tonic-gate vmem_putseg(vmp, (vmem_seg_t *)(p + --nseg * vmem_seg_size)); 7457c478bd9Sstevel@tonic-gate 7467c478bd9Sstevel@tonic-gate /* 7477c478bd9Sstevel@tonic-gate * Give the remainder to charity. 7487c478bd9Sstevel@tonic-gate */ 7497c478bd9Sstevel@tonic-gate while (nseg > 0) 7507c478bd9Sstevel@tonic-gate vmem_putseg_global((vmem_seg_t *)(p + --nseg * vmem_seg_size)); 7517c478bd9Sstevel@tonic-gate 7527c478bd9Sstevel@tonic-gate return (1); 7537c478bd9Sstevel@tonic-gate } 7547c478bd9Sstevel@tonic-gate 7557c478bd9Sstevel@tonic-gate /* 7567c478bd9Sstevel@tonic-gate * Advance a walker from its previous position to 'afterme'. 7577c478bd9Sstevel@tonic-gate * Note: may drop and reacquire vmp->vm_lock. 7587c478bd9Sstevel@tonic-gate */ 7597c478bd9Sstevel@tonic-gate static void 7607c478bd9Sstevel@tonic-gate vmem_advance(vmem_t *vmp, vmem_seg_t *walker, vmem_seg_t *afterme) 7617c478bd9Sstevel@tonic-gate { 7627c478bd9Sstevel@tonic-gate vmem_seg_t *vprev = walker->vs_aprev; 7637c478bd9Sstevel@tonic-gate vmem_seg_t *vnext = walker->vs_anext; 7647c478bd9Sstevel@tonic-gate vmem_seg_t *vsp = NULL; 7657c478bd9Sstevel@tonic-gate 7667c478bd9Sstevel@tonic-gate VMEM_DELETE(walker, a); 7677c478bd9Sstevel@tonic-gate 7687c478bd9Sstevel@tonic-gate if (afterme != NULL) 7697c478bd9Sstevel@tonic-gate VMEM_INSERT(afterme, walker, a); 7707c478bd9Sstevel@tonic-gate 7717c478bd9Sstevel@tonic-gate /* 7727c478bd9Sstevel@tonic-gate * The walker segment's presence may have prevented its neighbors 7737c478bd9Sstevel@tonic-gate * from coalescing. If so, coalesce them now. 7747c478bd9Sstevel@tonic-gate */ 7757c478bd9Sstevel@tonic-gate if (vprev->vs_type == VMEM_FREE) { 7767c478bd9Sstevel@tonic-gate if (vnext->vs_type == VMEM_FREE) { 7777c478bd9Sstevel@tonic-gate ASSERT(vprev->vs_end == vnext->vs_start); 7787c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, vnext); 7797c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, vprev); 7807c478bd9Sstevel@tonic-gate vprev->vs_end = vnext->vs_end; 7817c478bd9Sstevel@tonic-gate vmem_freelist_insert(vmp, vprev); 7827c478bd9Sstevel@tonic-gate vmem_seg_destroy(vmp, vnext); 7837c478bd9Sstevel@tonic-gate } 7847c478bd9Sstevel@tonic-gate vsp = vprev; 7857c478bd9Sstevel@tonic-gate } else if (vnext->vs_type == VMEM_FREE) { 7867c478bd9Sstevel@tonic-gate vsp = vnext; 7877c478bd9Sstevel@tonic-gate } 7887c478bd9Sstevel@tonic-gate 7897c478bd9Sstevel@tonic-gate /* 7907c478bd9Sstevel@tonic-gate * vsp could represent a complete imported span, 7917c478bd9Sstevel@tonic-gate * in which case we must return it to the source. 7927c478bd9Sstevel@tonic-gate */ 7937c478bd9Sstevel@tonic-gate if (vsp != NULL && vsp->vs_aprev->vs_import && 7947c478bd9Sstevel@tonic-gate vmp->vm_source_free != NULL && 7957c478bd9Sstevel@tonic-gate vsp->vs_aprev->vs_type == VMEM_SPAN && 7967c478bd9Sstevel@tonic-gate vsp->vs_anext->vs_type == VMEM_SPAN) { 7977c478bd9Sstevel@tonic-gate void *vaddr = (void *)vsp->vs_start; 7987c478bd9Sstevel@tonic-gate size_t size = VS_SIZE(vsp); 7997c478bd9Sstevel@tonic-gate ASSERT(size == VS_SIZE(vsp->vs_aprev)); 8007c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, vsp); 8017c478bd9Sstevel@tonic-gate vmem_span_destroy(vmp, vsp); 8027c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 8037c478bd9Sstevel@tonic-gate vmp->vm_source_free(vmp->vm_source, vaddr, size); 8047c478bd9Sstevel@tonic-gate mutex_enter(&vmp->vm_lock); 8057c478bd9Sstevel@tonic-gate } 8067c478bd9Sstevel@tonic-gate } 8077c478bd9Sstevel@tonic-gate 8087c478bd9Sstevel@tonic-gate /* 8097c478bd9Sstevel@tonic-gate * VM_NEXTFIT allocations deliberately cycle through all virtual addresses 8107c478bd9Sstevel@tonic-gate * in an arena, so that we avoid reusing addresses for as long as possible. 8117c478bd9Sstevel@tonic-gate * This helps to catch used-after-freed bugs. It's also the perfect policy 8127c478bd9Sstevel@tonic-gate * for allocating things like process IDs, where we want to cycle through 8137c478bd9Sstevel@tonic-gate * all values in order. 8147c478bd9Sstevel@tonic-gate */ 8157c478bd9Sstevel@tonic-gate static void * 8167c478bd9Sstevel@tonic-gate vmem_nextfit_alloc(vmem_t *vmp, size_t size, int vmflag) 8177c478bd9Sstevel@tonic-gate { 8187c478bd9Sstevel@tonic-gate vmem_seg_t *vsp, *rotor; 8197c478bd9Sstevel@tonic-gate uintptr_t addr; 8207c478bd9Sstevel@tonic-gate size_t realsize = P2ROUNDUP(size, vmp->vm_quantum); 8217c478bd9Sstevel@tonic-gate size_t vs_size; 8227c478bd9Sstevel@tonic-gate 8237c478bd9Sstevel@tonic-gate mutex_enter(&vmp->vm_lock); 8247c478bd9Sstevel@tonic-gate 8257c478bd9Sstevel@tonic-gate if (vmp->vm_nsegfree < VMEM_MINFREE && !vmem_populate(vmp, vmflag)) { 8267c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 8277c478bd9Sstevel@tonic-gate return (NULL); 8287c478bd9Sstevel@tonic-gate } 8297c478bd9Sstevel@tonic-gate 8307c478bd9Sstevel@tonic-gate /* 8317c478bd9Sstevel@tonic-gate * The common case is that the segment right after the rotor is free, 8327c478bd9Sstevel@tonic-gate * and large enough that extracting 'size' bytes won't change which 8337c478bd9Sstevel@tonic-gate * freelist it's on. In this case we can avoid a *lot* of work. 8347c478bd9Sstevel@tonic-gate * Instead of the normal vmem_seg_alloc(), we just advance the start 8357c478bd9Sstevel@tonic-gate * address of the victim segment. Instead of moving the rotor, we 8367c478bd9Sstevel@tonic-gate * create the new segment structure *behind the rotor*, which has 8377c478bd9Sstevel@tonic-gate * the same effect. And finally, we know we don't have to coalesce 8387c478bd9Sstevel@tonic-gate * the rotor's neighbors because the new segment lies between them. 8397c478bd9Sstevel@tonic-gate */ 8407c478bd9Sstevel@tonic-gate rotor = &vmp->vm_rotor; 8417c478bd9Sstevel@tonic-gate vsp = rotor->vs_anext; 8427c478bd9Sstevel@tonic-gate if (vsp->vs_type == VMEM_FREE && (vs_size = VS_SIZE(vsp)) > realsize && 8437c478bd9Sstevel@tonic-gate P2SAMEHIGHBIT(vs_size, vs_size - realsize)) { 8447c478bd9Sstevel@tonic-gate ASSERT(highbit(vs_size) == highbit(vs_size - realsize)); 8457c478bd9Sstevel@tonic-gate addr = vsp->vs_start; 8467c478bd9Sstevel@tonic-gate vsp->vs_start = addr + realsize; 8477c478bd9Sstevel@tonic-gate vmem_hash_insert(vmp, 8487c478bd9Sstevel@tonic-gate vmem_seg_create(vmp, rotor->vs_aprev, addr, addr + size)); 8497c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 8507c478bd9Sstevel@tonic-gate return ((void *)addr); 8517c478bd9Sstevel@tonic-gate } 8527c478bd9Sstevel@tonic-gate 8537c478bd9Sstevel@tonic-gate /* 8547c478bd9Sstevel@tonic-gate * Starting at the rotor, look for a segment large enough to 8557c478bd9Sstevel@tonic-gate * satisfy the allocation. 8567c478bd9Sstevel@tonic-gate */ 8577c478bd9Sstevel@tonic-gate for (;;) { 8587c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_search.value.ui64++; 8597c478bd9Sstevel@tonic-gate if (vsp->vs_type == VMEM_FREE && VS_SIZE(vsp) >= size) 8607c478bd9Sstevel@tonic-gate break; 8617c478bd9Sstevel@tonic-gate vsp = vsp->vs_anext; 8627c478bd9Sstevel@tonic-gate if (vsp == rotor) { 8637c478bd9Sstevel@tonic-gate /* 8647c478bd9Sstevel@tonic-gate * We've come full circle. One possibility is that the 8657c478bd9Sstevel@tonic-gate * there's actually enough space, but the rotor itself 8667c478bd9Sstevel@tonic-gate * is preventing the allocation from succeeding because 8677c478bd9Sstevel@tonic-gate * it's sitting between two free segments. Therefore, 8687c478bd9Sstevel@tonic-gate * we advance the rotor and see if that liberates a 8697c478bd9Sstevel@tonic-gate * suitable segment. 8707c478bd9Sstevel@tonic-gate */ 8717c478bd9Sstevel@tonic-gate vmem_advance(vmp, rotor, rotor->vs_anext); 8727c478bd9Sstevel@tonic-gate vsp = rotor->vs_aprev; 8737c478bd9Sstevel@tonic-gate if (vsp->vs_type == VMEM_FREE && VS_SIZE(vsp) >= size) 8747c478bd9Sstevel@tonic-gate break; 8757c478bd9Sstevel@tonic-gate /* 8767c478bd9Sstevel@tonic-gate * If there's a lower arena we can import from, or it's 8777c478bd9Sstevel@tonic-gate * a VM_NOSLEEP allocation, let vmem_xalloc() handle it. 8787c478bd9Sstevel@tonic-gate * Otherwise, wait until another thread frees something. 8797c478bd9Sstevel@tonic-gate */ 8807c478bd9Sstevel@tonic-gate if (vmp->vm_source_alloc != NULL || 8817c478bd9Sstevel@tonic-gate (vmflag & VM_NOSLEEP)) { 8827c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 8837c478bd9Sstevel@tonic-gate return (vmem_xalloc(vmp, size, vmp->vm_quantum, 8847c478bd9Sstevel@tonic-gate 0, 0, NULL, NULL, vmflag & VM_KMFLAGS)); 8857c478bd9Sstevel@tonic-gate } 8867c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_wait.value.ui64++; 8877c478bd9Sstevel@tonic-gate cv_wait(&vmp->vm_cv, &vmp->vm_lock); 8887c478bd9Sstevel@tonic-gate vsp = rotor->vs_anext; 8897c478bd9Sstevel@tonic-gate } 8907c478bd9Sstevel@tonic-gate } 8917c478bd9Sstevel@tonic-gate 8927c478bd9Sstevel@tonic-gate /* 8937c478bd9Sstevel@tonic-gate * We found a segment. Extract enough space to satisfy the allocation. 8947c478bd9Sstevel@tonic-gate */ 8957c478bd9Sstevel@tonic-gate addr = vsp->vs_start; 8967c478bd9Sstevel@tonic-gate vsp = vmem_seg_alloc(vmp, vsp, addr, size); 8977c478bd9Sstevel@tonic-gate ASSERT(vsp->vs_type == VMEM_ALLOC && 8987c478bd9Sstevel@tonic-gate vsp->vs_start == addr && vsp->vs_end == addr + size); 8997c478bd9Sstevel@tonic-gate 9007c478bd9Sstevel@tonic-gate /* 9017c478bd9Sstevel@tonic-gate * Advance the rotor to right after the newly-allocated segment. 9027c478bd9Sstevel@tonic-gate * That's where the next VM_NEXTFIT allocation will begin searching. 9037c478bd9Sstevel@tonic-gate */ 9047c478bd9Sstevel@tonic-gate vmem_advance(vmp, rotor, vsp); 9057c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 9067c478bd9Sstevel@tonic-gate return ((void *)addr); 9077c478bd9Sstevel@tonic-gate } 9087c478bd9Sstevel@tonic-gate 9097c478bd9Sstevel@tonic-gate /* 9107c478bd9Sstevel@tonic-gate * Checks if vmp is guaranteed to have a size-byte buffer somewhere on its 9117c478bd9Sstevel@tonic-gate * freelist. If size is not a power-of-2, it can return a false-negative. 9127c478bd9Sstevel@tonic-gate * 9137c478bd9Sstevel@tonic-gate * Used to decide if a newly imported span is superfluous after re-acquiring 9147c478bd9Sstevel@tonic-gate * the arena lock. 9157c478bd9Sstevel@tonic-gate */ 9167c478bd9Sstevel@tonic-gate static int 9177c478bd9Sstevel@tonic-gate vmem_canalloc(vmem_t *vmp, size_t size) 9187c478bd9Sstevel@tonic-gate { 9197c478bd9Sstevel@tonic-gate int hb; 9207c478bd9Sstevel@tonic-gate int flist = 0; 9217c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&vmp->vm_lock)); 9227c478bd9Sstevel@tonic-gate 923de710d24SJosef 'Jeff' Sipek if (ISP2(size)) 9247c478bd9Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, size)); 9257c478bd9Sstevel@tonic-gate else if ((hb = highbit(size)) < VMEM_FREELISTS) 9267c478bd9Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, 1UL << hb)); 9277c478bd9Sstevel@tonic-gate 9287c478bd9Sstevel@tonic-gate return (flist); 9297c478bd9Sstevel@tonic-gate } 9307c478bd9Sstevel@tonic-gate 9317c478bd9Sstevel@tonic-gate /* 9327c478bd9Sstevel@tonic-gate * Allocate size bytes at offset phase from an align boundary such that the 9337c478bd9Sstevel@tonic-gate * resulting segment [addr, addr + size) is a subset of [minaddr, maxaddr) 9347c478bd9Sstevel@tonic-gate * that does not straddle a nocross-aligned boundary. 9357c478bd9Sstevel@tonic-gate */ 9367c478bd9Sstevel@tonic-gate void * 9377c478bd9Sstevel@tonic-gate vmem_xalloc(vmem_t *vmp, size_t size, size_t align_arg, size_t phase, 9387c478bd9Sstevel@tonic-gate size_t nocross, void *minaddr, void *maxaddr, int vmflag) 9397c478bd9Sstevel@tonic-gate { 9407c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 9417c478bd9Sstevel@tonic-gate vmem_seg_t *vbest = NULL; 9427c478bd9Sstevel@tonic-gate uintptr_t addr, taddr, start, end; 9437c478bd9Sstevel@tonic-gate uintptr_t align = (align_arg != 0) ? align_arg : vmp->vm_quantum; 9447c478bd9Sstevel@tonic-gate void *vaddr, *xvaddr = NULL; 9457c478bd9Sstevel@tonic-gate size_t xsize; 9467c478bd9Sstevel@tonic-gate int hb, flist, resv; 9477c478bd9Sstevel@tonic-gate uint32_t mtbf; 9487c478bd9Sstevel@tonic-gate 9497c478bd9Sstevel@tonic-gate if ((align | phase | nocross) & (vmp->vm_quantum - 1)) 9507c478bd9Sstevel@tonic-gate panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): " 9517c478bd9Sstevel@tonic-gate "parameters not vm_quantum aligned", 9527c478bd9Sstevel@tonic-gate (void *)vmp, size, align_arg, phase, nocross, 9537c478bd9Sstevel@tonic-gate minaddr, maxaddr, vmflag); 9547c478bd9Sstevel@tonic-gate 9557c478bd9Sstevel@tonic-gate if (nocross != 0 && 9567c478bd9Sstevel@tonic-gate (align > nocross || P2ROUNDUP(phase + size, align) > nocross)) 9577c478bd9Sstevel@tonic-gate panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): " 9587c478bd9Sstevel@tonic-gate "overconstrained allocation", 9597c478bd9Sstevel@tonic-gate (void *)vmp, size, align_arg, phase, nocross, 9607c478bd9Sstevel@tonic-gate minaddr, maxaddr, vmflag); 9617c478bd9Sstevel@tonic-gate 962de710d24SJosef 'Jeff' Sipek if (phase >= align || !ISP2(align) || !ISP2(nocross)) 9637c478bd9Sstevel@tonic-gate panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): " 9647c478bd9Sstevel@tonic-gate "parameters inconsistent or invalid", 9657c478bd9Sstevel@tonic-gate (void *)vmp, size, align_arg, phase, nocross, 9667c478bd9Sstevel@tonic-gate minaddr, maxaddr, vmflag); 9677c478bd9Sstevel@tonic-gate 9687c478bd9Sstevel@tonic-gate if ((mtbf = vmem_mtbf | vmp->vm_mtbf) != 0 && gethrtime() % mtbf == 0 && 9697c478bd9Sstevel@tonic-gate (vmflag & (VM_NOSLEEP | VM_PANIC)) == VM_NOSLEEP) 9707c478bd9Sstevel@tonic-gate return (NULL); 9717c478bd9Sstevel@tonic-gate 9727c478bd9Sstevel@tonic-gate mutex_enter(&vmp->vm_lock); 9737c478bd9Sstevel@tonic-gate for (;;) { 9747c478bd9Sstevel@tonic-gate if (vmp->vm_nsegfree < VMEM_MINFREE && 9757c478bd9Sstevel@tonic-gate !vmem_populate(vmp, vmflag)) 9767c478bd9Sstevel@tonic-gate break; 9777c478bd9Sstevel@tonic-gate do_alloc: 9787c478bd9Sstevel@tonic-gate /* 9797c478bd9Sstevel@tonic-gate * highbit() returns the highest bit + 1, which is exactly 9807c478bd9Sstevel@tonic-gate * what we want: we want to search the first freelist whose 9817c478bd9Sstevel@tonic-gate * members are *definitely* large enough to satisfy our 9827c478bd9Sstevel@tonic-gate * allocation. However, there are certain cases in which we 9837c478bd9Sstevel@tonic-gate * want to look at the next-smallest freelist (which *might* 9847c478bd9Sstevel@tonic-gate * be able to satisfy the allocation): 9857c478bd9Sstevel@tonic-gate * 9867c478bd9Sstevel@tonic-gate * (1) The size is exactly a power of 2, in which case 9877c478bd9Sstevel@tonic-gate * the smaller freelist is always big enough; 9887c478bd9Sstevel@tonic-gate * 9897c478bd9Sstevel@tonic-gate * (2) All other freelists are empty; 9907c478bd9Sstevel@tonic-gate * 9917c478bd9Sstevel@tonic-gate * (3) We're in the highest possible freelist, which is 9927c478bd9Sstevel@tonic-gate * always empty (e.g. the 4GB freelist on 32-bit systems); 9937c478bd9Sstevel@tonic-gate * 9947c478bd9Sstevel@tonic-gate * (4) We're doing a best-fit or first-fit allocation. 9957c478bd9Sstevel@tonic-gate */ 996de710d24SJosef 'Jeff' Sipek if (ISP2(size)) { 9977c478bd9Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, size)); 9987c478bd9Sstevel@tonic-gate } else { 9997c478bd9Sstevel@tonic-gate hb = highbit(size); 10007c478bd9Sstevel@tonic-gate if ((vmp->vm_freemap >> hb) == 0 || 10017c478bd9Sstevel@tonic-gate hb == VMEM_FREELISTS || 10027c478bd9Sstevel@tonic-gate (vmflag & (VM_BESTFIT | VM_FIRSTFIT))) 10037c478bd9Sstevel@tonic-gate hb--; 10047c478bd9Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, 1UL << hb)); 10057c478bd9Sstevel@tonic-gate } 10067c478bd9Sstevel@tonic-gate 10077c478bd9Sstevel@tonic-gate for (vbest = NULL, vsp = (flist == 0) ? NULL : 10087c478bd9Sstevel@tonic-gate vmp->vm_freelist[flist - 1].vs_knext; 10097c478bd9Sstevel@tonic-gate vsp != NULL; vsp = vsp->vs_knext) { 10107c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_search.value.ui64++; 10117c478bd9Sstevel@tonic-gate if (vsp->vs_start == 0) { 10127c478bd9Sstevel@tonic-gate /* 10137c478bd9Sstevel@tonic-gate * We're moving up to a larger freelist, 10147c478bd9Sstevel@tonic-gate * so if we've already found a candidate, 10157c478bd9Sstevel@tonic-gate * the fit can't possibly get any better. 10167c478bd9Sstevel@tonic-gate */ 10177c478bd9Sstevel@tonic-gate if (vbest != NULL) 10187c478bd9Sstevel@tonic-gate break; 10197c478bd9Sstevel@tonic-gate /* 10207c478bd9Sstevel@tonic-gate * Find the next non-empty freelist. 10217c478bd9Sstevel@tonic-gate */ 10227c478bd9Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, 10237c478bd9Sstevel@tonic-gate VS_SIZE(vsp))); 10247c478bd9Sstevel@tonic-gate if (flist-- == 0) 10257c478bd9Sstevel@tonic-gate break; 10267c478bd9Sstevel@tonic-gate vsp = (vmem_seg_t *)&vmp->vm_freelist[flist]; 10277c478bd9Sstevel@tonic-gate ASSERT(vsp->vs_knext->vs_type == VMEM_FREE); 10287c478bd9Sstevel@tonic-gate continue; 10297c478bd9Sstevel@tonic-gate } 10307c478bd9Sstevel@tonic-gate if (vsp->vs_end - 1 < (uintptr_t)minaddr) 10317c478bd9Sstevel@tonic-gate continue; 10327c478bd9Sstevel@tonic-gate if (vsp->vs_start > (uintptr_t)maxaddr - 1) 10337c478bd9Sstevel@tonic-gate continue; 10347c478bd9Sstevel@tonic-gate start = MAX(vsp->vs_start, (uintptr_t)minaddr); 10357c478bd9Sstevel@tonic-gate end = MIN(vsp->vs_end - 1, (uintptr_t)maxaddr - 1) + 1; 10367c478bd9Sstevel@tonic-gate taddr = P2PHASEUP(start, align, phase); 103788b7b0f2SMatthew Ahrens if (P2BOUNDARY(taddr, size, nocross)) 10387c478bd9Sstevel@tonic-gate taddr += 10397c478bd9Sstevel@tonic-gate P2ROUNDUP(P2NPHASE(taddr, nocross), align); 10407c478bd9Sstevel@tonic-gate if ((taddr - start) + size > end - start || 10417c478bd9Sstevel@tonic-gate (vbest != NULL && VS_SIZE(vsp) >= VS_SIZE(vbest))) 10427c478bd9Sstevel@tonic-gate continue; 10437c478bd9Sstevel@tonic-gate vbest = vsp; 10447c478bd9Sstevel@tonic-gate addr = taddr; 10457c478bd9Sstevel@tonic-gate if (!(vmflag & VM_BESTFIT) || VS_SIZE(vbest) == size) 10467c478bd9Sstevel@tonic-gate break; 10477c478bd9Sstevel@tonic-gate } 10487c478bd9Sstevel@tonic-gate if (vbest != NULL) 10497c478bd9Sstevel@tonic-gate break; 10507c478bd9Sstevel@tonic-gate ASSERT(xvaddr == NULL); 10517c478bd9Sstevel@tonic-gate if (size == 0) 10527c478bd9Sstevel@tonic-gate panic("vmem_xalloc(): size == 0"); 10537c478bd9Sstevel@tonic-gate if (vmp->vm_source_alloc != NULL && nocross == 0 && 10547c478bd9Sstevel@tonic-gate minaddr == NULL && maxaddr == NULL) { 10557c478bd9Sstevel@tonic-gate size_t aneeded, asize; 10567c478bd9Sstevel@tonic-gate size_t aquantum = MAX(vmp->vm_quantum, 10577c478bd9Sstevel@tonic-gate vmp->vm_source->vm_quantum); 10587c478bd9Sstevel@tonic-gate size_t aphase = phase; 1059aaa10e67Sha137994 if ((align > aquantum) && 1060aaa10e67Sha137994 !(vmp->vm_cflags & VMC_XALIGN)) { 10617c478bd9Sstevel@tonic-gate aphase = (P2PHASE(phase, aquantum) != 0) ? 10627c478bd9Sstevel@tonic-gate align - vmp->vm_quantum : align - aquantum; 10637c478bd9Sstevel@tonic-gate ASSERT(aphase >= phase); 10647c478bd9Sstevel@tonic-gate } 10657c478bd9Sstevel@tonic-gate aneeded = MAX(size + aphase, vmp->vm_min_import); 10667c478bd9Sstevel@tonic-gate asize = P2ROUNDUP(aneeded, aquantum); 10677c478bd9Sstevel@tonic-gate 10680ee475b2SBryan Cantrill if (asize < size) { 10690ee475b2SBryan Cantrill /* 10700ee475b2SBryan Cantrill * The rounding induced overflow; return NULL 10710ee475b2SBryan Cantrill * if we are permitted to fail the allocation 10720ee475b2SBryan Cantrill * (and explicitly panic if we aren't). 10730ee475b2SBryan Cantrill */ 10740ee475b2SBryan Cantrill if ((vmflag & VM_NOSLEEP) && 10750ee475b2SBryan Cantrill !(vmflag & VM_PANIC)) { 10760ee475b2SBryan Cantrill mutex_exit(&vmp->vm_lock); 10770ee475b2SBryan Cantrill return (NULL); 10780ee475b2SBryan Cantrill } 10790ee475b2SBryan Cantrill 10800ee475b2SBryan Cantrill panic("vmem_xalloc(): size overflow"); 10810ee475b2SBryan Cantrill } 10820ee475b2SBryan Cantrill 10837c478bd9Sstevel@tonic-gate /* 10847c478bd9Sstevel@tonic-gate * Determine how many segment structures we'll consume. 10857c478bd9Sstevel@tonic-gate * The calculation must be precise because if we're 10867c478bd9Sstevel@tonic-gate * here on behalf of vmem_populate(), we are taking 10877c478bd9Sstevel@tonic-gate * segments from a very limited reserve. 10887c478bd9Sstevel@tonic-gate */ 10897c478bd9Sstevel@tonic-gate if (size == asize && !(vmp->vm_cflags & VMC_XALLOC)) 10907c478bd9Sstevel@tonic-gate resv = VMEM_SEGS_PER_SPAN_CREATE + 10917c478bd9Sstevel@tonic-gate VMEM_SEGS_PER_EXACT_ALLOC; 10927c478bd9Sstevel@tonic-gate else if (phase == 0 && 10937c478bd9Sstevel@tonic-gate align <= vmp->vm_source->vm_quantum) 10947c478bd9Sstevel@tonic-gate resv = VMEM_SEGS_PER_SPAN_CREATE + 10957c478bd9Sstevel@tonic-gate VMEM_SEGS_PER_LEFT_ALLOC; 10967c478bd9Sstevel@tonic-gate else 10977c478bd9Sstevel@tonic-gate resv = VMEM_SEGS_PER_ALLOC_MAX; 10987c478bd9Sstevel@tonic-gate 10997c478bd9Sstevel@tonic-gate ASSERT(vmp->vm_nsegfree >= resv); 11007c478bd9Sstevel@tonic-gate vmp->vm_nsegfree -= resv; /* reserve our segs */ 11017c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 11027c478bd9Sstevel@tonic-gate if (vmp->vm_cflags & VMC_XALLOC) { 11037c478bd9Sstevel@tonic-gate size_t oasize = asize; 11047c478bd9Sstevel@tonic-gate vaddr = ((vmem_ximport_t *) 11057c478bd9Sstevel@tonic-gate vmp->vm_source_alloc)(vmp->vm_source, 1106aaa10e67Sha137994 &asize, align, vmflag & VM_KMFLAGS); 11077c478bd9Sstevel@tonic-gate ASSERT(asize >= oasize); 11087c478bd9Sstevel@tonic-gate ASSERT(P2PHASE(asize, 11097c478bd9Sstevel@tonic-gate vmp->vm_source->vm_quantum) == 0); 1110aaa10e67Sha137994 ASSERT(!(vmp->vm_cflags & VMC_XALIGN) || 1111aaa10e67Sha137994 IS_P2ALIGNED(vaddr, align)); 11127c478bd9Sstevel@tonic-gate } else { 11137c478bd9Sstevel@tonic-gate vaddr = vmp->vm_source_alloc(vmp->vm_source, 11147c478bd9Sstevel@tonic-gate asize, vmflag & VM_KMFLAGS); 11157c478bd9Sstevel@tonic-gate } 11167c478bd9Sstevel@tonic-gate mutex_enter(&vmp->vm_lock); 11177c478bd9Sstevel@tonic-gate vmp->vm_nsegfree += resv; /* claim reservation */ 11187c478bd9Sstevel@tonic-gate aneeded = size + align - vmp->vm_quantum; 11197c478bd9Sstevel@tonic-gate aneeded = P2ROUNDUP(aneeded, vmp->vm_quantum); 11207c478bd9Sstevel@tonic-gate if (vaddr != NULL) { 11217c478bd9Sstevel@tonic-gate /* 11227c478bd9Sstevel@tonic-gate * Since we dropped the vmem lock while 11237c478bd9Sstevel@tonic-gate * calling the import function, other 11247c478bd9Sstevel@tonic-gate * threads could have imported space 11257c478bd9Sstevel@tonic-gate * and made our import unnecessary. In 11267c478bd9Sstevel@tonic-gate * order to save space, we return 11277c478bd9Sstevel@tonic-gate * excess imports immediately. 11287c478bd9Sstevel@tonic-gate */ 11297c478bd9Sstevel@tonic-gate if (asize > aneeded && 11307c478bd9Sstevel@tonic-gate vmp->vm_source_free != NULL && 11317c478bd9Sstevel@tonic-gate vmem_canalloc(vmp, aneeded)) { 11327c478bd9Sstevel@tonic-gate ASSERT(resv >= 11337c478bd9Sstevel@tonic-gate VMEM_SEGS_PER_MIDDLE_ALLOC); 11347c478bd9Sstevel@tonic-gate xvaddr = vaddr; 11357c478bd9Sstevel@tonic-gate xsize = asize; 11367c478bd9Sstevel@tonic-gate goto do_alloc; 11377c478bd9Sstevel@tonic-gate } 11387c478bd9Sstevel@tonic-gate vbest = vmem_span_create(vmp, vaddr, asize, 1); 11397c478bd9Sstevel@tonic-gate addr = P2PHASEUP(vbest->vs_start, align, phase); 11407c478bd9Sstevel@tonic-gate break; 11417c478bd9Sstevel@tonic-gate } else if (vmem_canalloc(vmp, aneeded)) { 11427c478bd9Sstevel@tonic-gate /* 11437c478bd9Sstevel@tonic-gate * Our import failed, but another thread 11447c478bd9Sstevel@tonic-gate * added sufficient free memory to the arena 11457c478bd9Sstevel@tonic-gate * to satisfy our request. Go back and 11467c478bd9Sstevel@tonic-gate * grab it. 11477c478bd9Sstevel@tonic-gate */ 11487c478bd9Sstevel@tonic-gate ASSERT(resv >= VMEM_SEGS_PER_MIDDLE_ALLOC); 11497c478bd9Sstevel@tonic-gate goto do_alloc; 11507c478bd9Sstevel@tonic-gate } 11517c478bd9Sstevel@tonic-gate } 11527c478bd9Sstevel@tonic-gate 11537c478bd9Sstevel@tonic-gate /* 11547c478bd9Sstevel@tonic-gate * If the requestor chooses to fail the allocation attempt 11557c478bd9Sstevel@tonic-gate * rather than reap wait and retry - get out of the loop. 11567c478bd9Sstevel@tonic-gate */ 11577c478bd9Sstevel@tonic-gate if (vmflag & VM_ABORT) 11587c478bd9Sstevel@tonic-gate break; 11597c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 11607c478bd9Sstevel@tonic-gate if (vmp->vm_cflags & VMC_IDENTIFIER) 11617c478bd9Sstevel@tonic-gate kmem_reap_idspace(); 11627c478bd9Sstevel@tonic-gate else 11637c478bd9Sstevel@tonic-gate kmem_reap(); 11647c478bd9Sstevel@tonic-gate mutex_enter(&vmp->vm_lock); 11657c478bd9Sstevel@tonic-gate if (vmflag & VM_NOSLEEP) 11667c478bd9Sstevel@tonic-gate break; 11677c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_wait.value.ui64++; 11687c478bd9Sstevel@tonic-gate cv_wait(&vmp->vm_cv, &vmp->vm_lock); 11697c478bd9Sstevel@tonic-gate } 11707c478bd9Sstevel@tonic-gate if (vbest != NULL) { 11717c478bd9Sstevel@tonic-gate ASSERT(vbest->vs_type == VMEM_FREE); 11727c478bd9Sstevel@tonic-gate ASSERT(vbest->vs_knext != vbest); 11730616c1c3SMichael Corcoran /* re-position to end of buffer */ 11740616c1c3SMichael Corcoran if (vmflag & VM_ENDALLOC) { 11750616c1c3SMichael Corcoran addr += ((vbest->vs_end - (addr + size)) / align) * 11760616c1c3SMichael Corcoran align; 11770616c1c3SMichael Corcoran } 11787c478bd9Sstevel@tonic-gate (void) vmem_seg_alloc(vmp, vbest, addr, size); 11797c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 11807c478bd9Sstevel@tonic-gate if (xvaddr) 11817c478bd9Sstevel@tonic-gate vmp->vm_source_free(vmp->vm_source, xvaddr, xsize); 11827c478bd9Sstevel@tonic-gate ASSERT(P2PHASE(addr, align) == phase); 118388b7b0f2SMatthew Ahrens ASSERT(!P2BOUNDARY(addr, size, nocross)); 11847c478bd9Sstevel@tonic-gate ASSERT(addr >= (uintptr_t)minaddr); 11857c478bd9Sstevel@tonic-gate ASSERT(addr + size - 1 <= (uintptr_t)maxaddr - 1); 11867c478bd9Sstevel@tonic-gate return ((void *)addr); 11877c478bd9Sstevel@tonic-gate } 11887c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_fail.value.ui64++; 11897c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 11907c478bd9Sstevel@tonic-gate if (vmflag & VM_PANIC) 11917c478bd9Sstevel@tonic-gate panic("vmem_xalloc(%p, %lu, %lu, %lu, %lu, %p, %p, %x): " 11927c478bd9Sstevel@tonic-gate "cannot satisfy mandatory allocation", 11937c478bd9Sstevel@tonic-gate (void *)vmp, size, align_arg, phase, nocross, 11947c478bd9Sstevel@tonic-gate minaddr, maxaddr, vmflag); 11957c478bd9Sstevel@tonic-gate ASSERT(xvaddr == NULL); 11967c478bd9Sstevel@tonic-gate return (NULL); 11977c478bd9Sstevel@tonic-gate } 11987c478bd9Sstevel@tonic-gate 11997c478bd9Sstevel@tonic-gate /* 12007c478bd9Sstevel@tonic-gate * Free the segment [vaddr, vaddr + size), where vaddr was a constrained 12017c478bd9Sstevel@tonic-gate * allocation. vmem_xalloc() and vmem_xfree() must always be paired because 12027c478bd9Sstevel@tonic-gate * both routines bypass the quantum caches. 12037c478bd9Sstevel@tonic-gate */ 12047c478bd9Sstevel@tonic-gate void 12057c478bd9Sstevel@tonic-gate vmem_xfree(vmem_t *vmp, void *vaddr, size_t size) 12067c478bd9Sstevel@tonic-gate { 12077c478bd9Sstevel@tonic-gate vmem_seg_t *vsp, *vnext, *vprev; 12087c478bd9Sstevel@tonic-gate 12097c478bd9Sstevel@tonic-gate mutex_enter(&vmp->vm_lock); 12107c478bd9Sstevel@tonic-gate 12117c478bd9Sstevel@tonic-gate vsp = vmem_hash_delete(vmp, (uintptr_t)vaddr, size); 12127c478bd9Sstevel@tonic-gate vsp->vs_end = P2ROUNDUP(vsp->vs_end, vmp->vm_quantum); 12137c478bd9Sstevel@tonic-gate 12147c478bd9Sstevel@tonic-gate /* 12157c478bd9Sstevel@tonic-gate * Attempt to coalesce with the next segment. 12167c478bd9Sstevel@tonic-gate */ 12177c478bd9Sstevel@tonic-gate vnext = vsp->vs_anext; 12187c478bd9Sstevel@tonic-gate if (vnext->vs_type == VMEM_FREE) { 12197c478bd9Sstevel@tonic-gate ASSERT(vsp->vs_end == vnext->vs_start); 12207c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, vnext); 12217c478bd9Sstevel@tonic-gate vsp->vs_end = vnext->vs_end; 12227c478bd9Sstevel@tonic-gate vmem_seg_destroy(vmp, vnext); 12237c478bd9Sstevel@tonic-gate } 12247c478bd9Sstevel@tonic-gate 12257c478bd9Sstevel@tonic-gate /* 12267c478bd9Sstevel@tonic-gate * Attempt to coalesce with the previous segment. 12277c478bd9Sstevel@tonic-gate */ 12287c478bd9Sstevel@tonic-gate vprev = vsp->vs_aprev; 12297c478bd9Sstevel@tonic-gate if (vprev->vs_type == VMEM_FREE) { 12307c478bd9Sstevel@tonic-gate ASSERT(vprev->vs_end == vsp->vs_start); 12317c478bd9Sstevel@tonic-gate vmem_freelist_delete(vmp, vprev); 12327c478bd9Sstevel@tonic-gate vprev->vs_end = vsp->vs_end; 12337c478bd9Sstevel@tonic-gate vmem_seg_destroy(vmp, vsp); 12347c478bd9Sstevel@tonic-gate vsp = vprev; 12357c478bd9Sstevel@tonic-gate } 12367c478bd9Sstevel@tonic-gate 12377c478bd9Sstevel@tonic-gate /* 12387c478bd9Sstevel@tonic-gate * If the entire span is free, return it to the source. 12397c478bd9Sstevel@tonic-gate */ 12407c478bd9Sstevel@tonic-gate if (vsp->vs_aprev->vs_import && vmp->vm_source_free != NULL && 12417c478bd9Sstevel@tonic-gate vsp->vs_aprev->vs_type == VMEM_SPAN && 12427c478bd9Sstevel@tonic-gate vsp->vs_anext->vs_type == VMEM_SPAN) { 12437c478bd9Sstevel@tonic-gate vaddr = (void *)vsp->vs_start; 12447c478bd9Sstevel@tonic-gate size = VS_SIZE(vsp); 12457c478bd9Sstevel@tonic-gate ASSERT(size == VS_SIZE(vsp->vs_aprev)); 12467c478bd9Sstevel@tonic-gate vmem_span_destroy(vmp, vsp); 12477c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 12487c478bd9Sstevel@tonic-gate vmp->vm_source_free(vmp->vm_source, vaddr, size); 12497c478bd9Sstevel@tonic-gate } else { 12507c478bd9Sstevel@tonic-gate vmem_freelist_insert(vmp, vsp); 12517c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 12527c478bd9Sstevel@tonic-gate } 12537c478bd9Sstevel@tonic-gate } 12547c478bd9Sstevel@tonic-gate 12557c478bd9Sstevel@tonic-gate /* 12567c478bd9Sstevel@tonic-gate * Allocate size bytes from arena vmp. Returns the allocated address 12577c478bd9Sstevel@tonic-gate * on success, NULL on failure. vmflag specifies VM_SLEEP or VM_NOSLEEP, 12587c478bd9Sstevel@tonic-gate * and may also specify best-fit, first-fit, or next-fit allocation policy 12597c478bd9Sstevel@tonic-gate * instead of the default instant-fit policy. VM_SLEEP allocations are 12607c478bd9Sstevel@tonic-gate * guaranteed to succeed. 12617c478bd9Sstevel@tonic-gate */ 12627c478bd9Sstevel@tonic-gate void * 12637c478bd9Sstevel@tonic-gate vmem_alloc(vmem_t *vmp, size_t size, int vmflag) 12647c478bd9Sstevel@tonic-gate { 12657c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 12667c478bd9Sstevel@tonic-gate uintptr_t addr; 12677c478bd9Sstevel@tonic-gate int hb; 12687c478bd9Sstevel@tonic-gate int flist = 0; 12697c478bd9Sstevel@tonic-gate uint32_t mtbf; 12707c478bd9Sstevel@tonic-gate 12717c478bd9Sstevel@tonic-gate if (size - 1 < vmp->vm_qcache_max) 12727c478bd9Sstevel@tonic-gate return (kmem_cache_alloc(vmp->vm_qcache[(size - 1) >> 12737c478bd9Sstevel@tonic-gate vmp->vm_qshift], vmflag & VM_KMFLAGS)); 12747c478bd9Sstevel@tonic-gate 12757c478bd9Sstevel@tonic-gate if ((mtbf = vmem_mtbf | vmp->vm_mtbf) != 0 && gethrtime() % mtbf == 0 && 12767c478bd9Sstevel@tonic-gate (vmflag & (VM_NOSLEEP | VM_PANIC)) == VM_NOSLEEP) 12777c478bd9Sstevel@tonic-gate return (NULL); 12787c478bd9Sstevel@tonic-gate 12797c478bd9Sstevel@tonic-gate if (vmflag & VM_NEXTFIT) 12807c478bd9Sstevel@tonic-gate return (vmem_nextfit_alloc(vmp, size, vmflag)); 12817c478bd9Sstevel@tonic-gate 12827c478bd9Sstevel@tonic-gate if (vmflag & (VM_BESTFIT | VM_FIRSTFIT)) 12837c478bd9Sstevel@tonic-gate return (vmem_xalloc(vmp, size, vmp->vm_quantum, 0, 0, 12847c478bd9Sstevel@tonic-gate NULL, NULL, vmflag)); 12857c478bd9Sstevel@tonic-gate 12867c478bd9Sstevel@tonic-gate /* 12877c478bd9Sstevel@tonic-gate * Unconstrained instant-fit allocation from the segment list. 12887c478bd9Sstevel@tonic-gate */ 12897c478bd9Sstevel@tonic-gate mutex_enter(&vmp->vm_lock); 12907c478bd9Sstevel@tonic-gate 12917c478bd9Sstevel@tonic-gate if (vmp->vm_nsegfree >= VMEM_MINFREE || vmem_populate(vmp, vmflag)) { 1292de710d24SJosef 'Jeff' Sipek if (ISP2(size)) 12937c478bd9Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, size)); 12947c478bd9Sstevel@tonic-gate else if ((hb = highbit(size)) < VMEM_FREELISTS) 12957c478bd9Sstevel@tonic-gate flist = lowbit(P2ALIGN(vmp->vm_freemap, 1UL << hb)); 12967c478bd9Sstevel@tonic-gate } 12977c478bd9Sstevel@tonic-gate 12987c478bd9Sstevel@tonic-gate if (flist-- == 0) { 12997c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 13007c478bd9Sstevel@tonic-gate return (vmem_xalloc(vmp, size, vmp->vm_quantum, 13017c478bd9Sstevel@tonic-gate 0, 0, NULL, NULL, vmflag)); 13027c478bd9Sstevel@tonic-gate } 13037c478bd9Sstevel@tonic-gate 13047c478bd9Sstevel@tonic-gate ASSERT(size <= (1UL << flist)); 13057c478bd9Sstevel@tonic-gate vsp = vmp->vm_freelist[flist].vs_knext; 13067c478bd9Sstevel@tonic-gate addr = vsp->vs_start; 13070616c1c3SMichael Corcoran if (vmflag & VM_ENDALLOC) { 13080616c1c3SMichael Corcoran addr += vsp->vs_end - (addr + size); 13090616c1c3SMichael Corcoran } 13107c478bd9Sstevel@tonic-gate (void) vmem_seg_alloc(vmp, vsp, addr, size); 13117c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 13127c478bd9Sstevel@tonic-gate return ((void *)addr); 13137c478bd9Sstevel@tonic-gate } 13147c478bd9Sstevel@tonic-gate 13157c478bd9Sstevel@tonic-gate /* 13167c478bd9Sstevel@tonic-gate * Free the segment [vaddr, vaddr + size). 13177c478bd9Sstevel@tonic-gate */ 13187c478bd9Sstevel@tonic-gate void 13197c478bd9Sstevel@tonic-gate vmem_free(vmem_t *vmp, void *vaddr, size_t size) 13207c478bd9Sstevel@tonic-gate { 13217c478bd9Sstevel@tonic-gate if (size - 1 < vmp->vm_qcache_max) 13227c478bd9Sstevel@tonic-gate kmem_cache_free(vmp->vm_qcache[(size - 1) >> vmp->vm_qshift], 13237c478bd9Sstevel@tonic-gate vaddr); 13247c478bd9Sstevel@tonic-gate else 13257c478bd9Sstevel@tonic-gate vmem_xfree(vmp, vaddr, size); 13267c478bd9Sstevel@tonic-gate } 13277c478bd9Sstevel@tonic-gate 13287c478bd9Sstevel@tonic-gate /* 13297c478bd9Sstevel@tonic-gate * Determine whether arena vmp contains the segment [vaddr, vaddr + size). 13307c478bd9Sstevel@tonic-gate */ 13317c478bd9Sstevel@tonic-gate int 13327c478bd9Sstevel@tonic-gate vmem_contains(vmem_t *vmp, void *vaddr, size_t size) 13337c478bd9Sstevel@tonic-gate { 13347c478bd9Sstevel@tonic-gate uintptr_t start = (uintptr_t)vaddr; 13357c478bd9Sstevel@tonic-gate uintptr_t end = start + size; 13367c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 13377c478bd9Sstevel@tonic-gate vmem_seg_t *seg0 = &vmp->vm_seg0; 13387c478bd9Sstevel@tonic-gate 13397c478bd9Sstevel@tonic-gate mutex_enter(&vmp->vm_lock); 13407c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_contains.value.ui64++; 13417c478bd9Sstevel@tonic-gate for (vsp = seg0->vs_knext; vsp != seg0; vsp = vsp->vs_knext) { 13427c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_contains_search.value.ui64++; 13437c478bd9Sstevel@tonic-gate ASSERT(vsp->vs_type == VMEM_SPAN); 13447c478bd9Sstevel@tonic-gate if (start >= vsp->vs_start && end - 1 <= vsp->vs_end - 1) 13457c478bd9Sstevel@tonic-gate break; 13467c478bd9Sstevel@tonic-gate } 13477c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 13487c478bd9Sstevel@tonic-gate return (vsp != seg0); 13497c478bd9Sstevel@tonic-gate } 13507c478bd9Sstevel@tonic-gate 13517c478bd9Sstevel@tonic-gate /* 13527c478bd9Sstevel@tonic-gate * Add the span [vaddr, vaddr + size) to arena vmp. 13537c478bd9Sstevel@tonic-gate */ 13547c478bd9Sstevel@tonic-gate void * 13557c478bd9Sstevel@tonic-gate vmem_add(vmem_t *vmp, void *vaddr, size_t size, int vmflag) 13567c478bd9Sstevel@tonic-gate { 13577c478bd9Sstevel@tonic-gate if (vaddr == NULL || size == 0) 1358903a11ebSrh87107 panic("vmem_add(%p, %p, %lu): bad arguments", 1359903a11ebSrh87107 (void *)vmp, vaddr, size); 13607c478bd9Sstevel@tonic-gate 13617c478bd9Sstevel@tonic-gate ASSERT(!vmem_contains(vmp, vaddr, size)); 13627c478bd9Sstevel@tonic-gate 13637c478bd9Sstevel@tonic-gate mutex_enter(&vmp->vm_lock); 13647c478bd9Sstevel@tonic-gate if (vmem_populate(vmp, vmflag)) 13657c478bd9Sstevel@tonic-gate (void) vmem_span_create(vmp, vaddr, size, 0); 13667c478bd9Sstevel@tonic-gate else 13677c478bd9Sstevel@tonic-gate vaddr = NULL; 13687c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 13697c478bd9Sstevel@tonic-gate return (vaddr); 13707c478bd9Sstevel@tonic-gate } 13717c478bd9Sstevel@tonic-gate 13727c478bd9Sstevel@tonic-gate /* 13737c478bd9Sstevel@tonic-gate * Walk the vmp arena, applying func to each segment matching typemask. 13747c478bd9Sstevel@tonic-gate * If VMEM_REENTRANT is specified, the arena lock is dropped across each 13757c478bd9Sstevel@tonic-gate * call to func(); otherwise, it is held for the duration of vmem_walk() 13767c478bd9Sstevel@tonic-gate * to ensure a consistent snapshot. Note that VMEM_REENTRANT callbacks 13777c478bd9Sstevel@tonic-gate * are *not* necessarily consistent, so they may only be used when a hint 13787c478bd9Sstevel@tonic-gate * is adequate. 13797c478bd9Sstevel@tonic-gate */ 13807c478bd9Sstevel@tonic-gate void 13817c478bd9Sstevel@tonic-gate vmem_walk(vmem_t *vmp, int typemask, 13827c478bd9Sstevel@tonic-gate void (*func)(void *, void *, size_t), void *arg) 13837c478bd9Sstevel@tonic-gate { 13847c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 13857c478bd9Sstevel@tonic-gate vmem_seg_t *seg0 = &vmp->vm_seg0; 13867c478bd9Sstevel@tonic-gate vmem_seg_t walker; 13877c478bd9Sstevel@tonic-gate 13887c478bd9Sstevel@tonic-gate if (typemask & VMEM_WALKER) 13897c478bd9Sstevel@tonic-gate return; 13907c478bd9Sstevel@tonic-gate 13917c478bd9Sstevel@tonic-gate bzero(&walker, sizeof (walker)); 13927c478bd9Sstevel@tonic-gate walker.vs_type = VMEM_WALKER; 13937c478bd9Sstevel@tonic-gate 13947c478bd9Sstevel@tonic-gate mutex_enter(&vmp->vm_lock); 13957c478bd9Sstevel@tonic-gate VMEM_INSERT(seg0, &walker, a); 13967c478bd9Sstevel@tonic-gate for (vsp = seg0->vs_anext; vsp != seg0; vsp = vsp->vs_anext) { 13977c478bd9Sstevel@tonic-gate if (vsp->vs_type & typemask) { 13987c478bd9Sstevel@tonic-gate void *start = (void *)vsp->vs_start; 13997c478bd9Sstevel@tonic-gate size_t size = VS_SIZE(vsp); 14007c478bd9Sstevel@tonic-gate if (typemask & VMEM_REENTRANT) { 14017c478bd9Sstevel@tonic-gate vmem_advance(vmp, &walker, vsp); 14027c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 14037c478bd9Sstevel@tonic-gate func(arg, start, size); 14047c478bd9Sstevel@tonic-gate mutex_enter(&vmp->vm_lock); 14057c478bd9Sstevel@tonic-gate vsp = &walker; 14067c478bd9Sstevel@tonic-gate } else { 14077c478bd9Sstevel@tonic-gate func(arg, start, size); 14087c478bd9Sstevel@tonic-gate } 14097c478bd9Sstevel@tonic-gate } 14107c478bd9Sstevel@tonic-gate } 14117c478bd9Sstevel@tonic-gate vmem_advance(vmp, &walker, NULL); 14127c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 14137c478bd9Sstevel@tonic-gate } 14147c478bd9Sstevel@tonic-gate 14157c478bd9Sstevel@tonic-gate /* 14167c478bd9Sstevel@tonic-gate * Return the total amount of memory whose type matches typemask. Thus: 14177c478bd9Sstevel@tonic-gate * 14187c478bd9Sstevel@tonic-gate * typemask VMEM_ALLOC yields total memory allocated (in use). 14197c478bd9Sstevel@tonic-gate * typemask VMEM_FREE yields total memory free (available). 14207c478bd9Sstevel@tonic-gate * typemask (VMEM_ALLOC | VMEM_FREE) yields total arena size. 14217c478bd9Sstevel@tonic-gate */ 14227c478bd9Sstevel@tonic-gate size_t 14237c478bd9Sstevel@tonic-gate vmem_size(vmem_t *vmp, int typemask) 14247c478bd9Sstevel@tonic-gate { 14257c478bd9Sstevel@tonic-gate uint64_t size = 0; 14267c478bd9Sstevel@tonic-gate 14277c478bd9Sstevel@tonic-gate if (typemask & VMEM_ALLOC) 14287c478bd9Sstevel@tonic-gate size += vmp->vm_kstat.vk_mem_inuse.value.ui64; 14297c478bd9Sstevel@tonic-gate if (typemask & VMEM_FREE) 14307c478bd9Sstevel@tonic-gate size += vmp->vm_kstat.vk_mem_total.value.ui64 - 14317c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_mem_inuse.value.ui64; 14327c478bd9Sstevel@tonic-gate return ((size_t)size); 14337c478bd9Sstevel@tonic-gate } 14347c478bd9Sstevel@tonic-gate 14357c478bd9Sstevel@tonic-gate /* 14367c478bd9Sstevel@tonic-gate * Create an arena called name whose initial span is [base, base + size). 14377c478bd9Sstevel@tonic-gate * The arena's natural unit of currency is quantum, so vmem_alloc() 14387c478bd9Sstevel@tonic-gate * guarantees quantum-aligned results. The arena may import new spans 14397c478bd9Sstevel@tonic-gate * by invoking afunc() on source, and may return those spans by invoking 14407c478bd9Sstevel@tonic-gate * ffunc() on source. To make small allocations fast and scalable, 14417c478bd9Sstevel@tonic-gate * the arena offers high-performance caching for each integer multiple 14427c478bd9Sstevel@tonic-gate * of quantum up to qcache_max. 14437c478bd9Sstevel@tonic-gate */ 14447c478bd9Sstevel@tonic-gate static vmem_t * 14457c478bd9Sstevel@tonic-gate vmem_create_common(const char *name, void *base, size_t size, size_t quantum, 14467c478bd9Sstevel@tonic-gate void *(*afunc)(vmem_t *, size_t, int), 14477c478bd9Sstevel@tonic-gate void (*ffunc)(vmem_t *, void *, size_t), 14487c478bd9Sstevel@tonic-gate vmem_t *source, size_t qcache_max, int vmflag) 14497c478bd9Sstevel@tonic-gate { 14507c478bd9Sstevel@tonic-gate int i; 14517c478bd9Sstevel@tonic-gate size_t nqcache; 14527c478bd9Sstevel@tonic-gate vmem_t *vmp, *cur, **vmpp; 14537c478bd9Sstevel@tonic-gate vmem_seg_t *vsp; 14547c478bd9Sstevel@tonic-gate vmem_freelist_t *vfp; 14551a5e258fSJosef 'Jeff' Sipek uint32_t id = atomic_inc_32_nv(&vmem_id); 14567c478bd9Sstevel@tonic-gate 14577c478bd9Sstevel@tonic-gate if (vmem_vmem_arena != NULL) { 14587c478bd9Sstevel@tonic-gate vmp = vmem_alloc(vmem_vmem_arena, sizeof (vmem_t), 14597c478bd9Sstevel@tonic-gate vmflag & VM_KMFLAGS); 14607c478bd9Sstevel@tonic-gate } else { 14617c478bd9Sstevel@tonic-gate ASSERT(id <= VMEM_INITIAL); 14627c478bd9Sstevel@tonic-gate vmp = &vmem0[id - 1]; 14637c478bd9Sstevel@tonic-gate } 14647c478bd9Sstevel@tonic-gate 14657c478bd9Sstevel@tonic-gate /* An identifier arena must inherit from another identifier arena */ 14667c478bd9Sstevel@tonic-gate ASSERT(source == NULL || ((source->vm_cflags & VMC_IDENTIFIER) == 14677c478bd9Sstevel@tonic-gate (vmflag & VMC_IDENTIFIER))); 14687c478bd9Sstevel@tonic-gate 14697c478bd9Sstevel@tonic-gate if (vmp == NULL) 14707c478bd9Sstevel@tonic-gate return (NULL); 14717c478bd9Sstevel@tonic-gate bzero(vmp, sizeof (vmem_t)); 14727c478bd9Sstevel@tonic-gate 14737c478bd9Sstevel@tonic-gate (void) snprintf(vmp->vm_name, VMEM_NAMELEN, "%s", name); 14747c478bd9Sstevel@tonic-gate mutex_init(&vmp->vm_lock, NULL, MUTEX_DEFAULT, NULL); 14757c478bd9Sstevel@tonic-gate cv_init(&vmp->vm_cv, NULL, CV_DEFAULT, NULL); 14767c478bd9Sstevel@tonic-gate vmp->vm_cflags = vmflag; 14777c478bd9Sstevel@tonic-gate vmflag &= VM_KMFLAGS; 14787c478bd9Sstevel@tonic-gate 14797c478bd9Sstevel@tonic-gate vmp->vm_quantum = quantum; 14807c478bd9Sstevel@tonic-gate vmp->vm_qshift = highbit(quantum) - 1; 14817c478bd9Sstevel@tonic-gate nqcache = MIN(qcache_max >> vmp->vm_qshift, VMEM_NQCACHE_MAX); 14827c478bd9Sstevel@tonic-gate 14837c478bd9Sstevel@tonic-gate for (i = 0; i <= VMEM_FREELISTS; i++) { 14847c478bd9Sstevel@tonic-gate vfp = &vmp->vm_freelist[i]; 14857c478bd9Sstevel@tonic-gate vfp->vs_end = 1UL << i; 14867c478bd9Sstevel@tonic-gate vfp->vs_knext = (vmem_seg_t *)(vfp + 1); 14877c478bd9Sstevel@tonic-gate vfp->vs_kprev = (vmem_seg_t *)(vfp - 1); 14887c478bd9Sstevel@tonic-gate } 14897c478bd9Sstevel@tonic-gate 14907c478bd9Sstevel@tonic-gate vmp->vm_freelist[0].vs_kprev = NULL; 14917c478bd9Sstevel@tonic-gate vmp->vm_freelist[VMEM_FREELISTS].vs_knext = NULL; 14927c478bd9Sstevel@tonic-gate vmp->vm_freelist[VMEM_FREELISTS].vs_end = 0; 14937c478bd9Sstevel@tonic-gate vmp->vm_hash_table = vmp->vm_hash0; 14947c478bd9Sstevel@tonic-gate vmp->vm_hash_mask = VMEM_HASH_INITIAL - 1; 14957c478bd9Sstevel@tonic-gate vmp->vm_hash_shift = highbit(vmp->vm_hash_mask); 14967c478bd9Sstevel@tonic-gate 14977c478bd9Sstevel@tonic-gate vsp = &vmp->vm_seg0; 14987c478bd9Sstevel@tonic-gate vsp->vs_anext = vsp; 14997c478bd9Sstevel@tonic-gate vsp->vs_aprev = vsp; 15007c478bd9Sstevel@tonic-gate vsp->vs_knext = vsp; 15017c478bd9Sstevel@tonic-gate vsp->vs_kprev = vsp; 15027c478bd9Sstevel@tonic-gate vsp->vs_type = VMEM_SPAN; 15037c478bd9Sstevel@tonic-gate 15047c478bd9Sstevel@tonic-gate vsp = &vmp->vm_rotor; 15057c478bd9Sstevel@tonic-gate vsp->vs_type = VMEM_ROTOR; 15067c478bd9Sstevel@tonic-gate VMEM_INSERT(&vmp->vm_seg0, vsp, a); 15077c478bd9Sstevel@tonic-gate 15087c478bd9Sstevel@tonic-gate bcopy(&vmem_kstat_template, &vmp->vm_kstat, sizeof (vmem_kstat_t)); 15097c478bd9Sstevel@tonic-gate 15107c478bd9Sstevel@tonic-gate vmp->vm_id = id; 15117c478bd9Sstevel@tonic-gate if (source != NULL) 15127c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_source_id.value.ui32 = source->vm_id; 15137c478bd9Sstevel@tonic-gate vmp->vm_source = source; 15147c478bd9Sstevel@tonic-gate vmp->vm_source_alloc = afunc; 15157c478bd9Sstevel@tonic-gate vmp->vm_source_free = ffunc; 15167c478bd9Sstevel@tonic-gate 15177c478bd9Sstevel@tonic-gate /* 15187c478bd9Sstevel@tonic-gate * Some arenas (like vmem_metadata and kmem_metadata) cannot 15197c478bd9Sstevel@tonic-gate * use quantum caching to lower fragmentation. Instead, we 15207c478bd9Sstevel@tonic-gate * increase their imports, giving a similar effect. 15217c478bd9Sstevel@tonic-gate */ 15227c478bd9Sstevel@tonic-gate if (vmp->vm_cflags & VMC_NO_QCACHE) { 15237c478bd9Sstevel@tonic-gate vmp->vm_min_import = 15247c478bd9Sstevel@tonic-gate VMEM_QCACHE_SLABSIZE(nqcache << vmp->vm_qshift); 15257c478bd9Sstevel@tonic-gate nqcache = 0; 15267c478bd9Sstevel@tonic-gate } 15277c478bd9Sstevel@tonic-gate 15287c478bd9Sstevel@tonic-gate if (nqcache != 0) { 15297c478bd9Sstevel@tonic-gate ASSERT(!(vmflag & VM_NOSLEEP)); 15307c478bd9Sstevel@tonic-gate vmp->vm_qcache_max = nqcache << vmp->vm_qshift; 15317c478bd9Sstevel@tonic-gate for (i = 0; i < nqcache; i++) { 15327c478bd9Sstevel@tonic-gate char buf[VMEM_NAMELEN + 21]; 15337c478bd9Sstevel@tonic-gate (void) sprintf(buf, "%s_%lu", vmp->vm_name, 15347c478bd9Sstevel@tonic-gate (i + 1) * quantum); 15357c478bd9Sstevel@tonic-gate vmp->vm_qcache[i] = kmem_cache_create(buf, 15367c478bd9Sstevel@tonic-gate (i + 1) * quantum, quantum, NULL, NULL, NULL, 15377c478bd9Sstevel@tonic-gate NULL, vmp, KMC_QCACHE | KMC_NOTOUCH); 15387c478bd9Sstevel@tonic-gate } 15397c478bd9Sstevel@tonic-gate } 15407c478bd9Sstevel@tonic-gate 15417c478bd9Sstevel@tonic-gate if ((vmp->vm_ksp = kstat_create("vmem", vmp->vm_id, vmp->vm_name, 15427c478bd9Sstevel@tonic-gate "vmem", KSTAT_TYPE_NAMED, sizeof (vmem_kstat_t) / 15437c478bd9Sstevel@tonic-gate sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL)) != NULL) { 15447c478bd9Sstevel@tonic-gate vmp->vm_ksp->ks_data = &vmp->vm_kstat; 15457c478bd9Sstevel@tonic-gate kstat_install(vmp->vm_ksp); 15467c478bd9Sstevel@tonic-gate } 15477c478bd9Sstevel@tonic-gate 15487c478bd9Sstevel@tonic-gate mutex_enter(&vmem_list_lock); 15497c478bd9Sstevel@tonic-gate vmpp = &vmem_list; 15507c478bd9Sstevel@tonic-gate while ((cur = *vmpp) != NULL) 15517c478bd9Sstevel@tonic-gate vmpp = &cur->vm_next; 15527c478bd9Sstevel@tonic-gate *vmpp = vmp; 15537c478bd9Sstevel@tonic-gate mutex_exit(&vmem_list_lock); 15547c478bd9Sstevel@tonic-gate 15557c478bd9Sstevel@tonic-gate if (vmp->vm_cflags & VMC_POPULATOR) { 15567c478bd9Sstevel@tonic-gate ASSERT(vmem_populators < VMEM_INITIAL); 15571a5e258fSJosef 'Jeff' Sipek vmem_populator[atomic_inc_32_nv(&vmem_populators) - 1] = vmp; 15587c478bd9Sstevel@tonic-gate mutex_enter(&vmp->vm_lock); 15597c478bd9Sstevel@tonic-gate (void) vmem_populate(vmp, vmflag | VM_PANIC); 15607c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 15617c478bd9Sstevel@tonic-gate } 15627c478bd9Sstevel@tonic-gate 15637c478bd9Sstevel@tonic-gate if ((base || size) && vmem_add(vmp, base, size, vmflag) == NULL) { 15647c478bd9Sstevel@tonic-gate vmem_destroy(vmp); 15657c478bd9Sstevel@tonic-gate return (NULL); 15667c478bd9Sstevel@tonic-gate } 15677c478bd9Sstevel@tonic-gate 15687c478bd9Sstevel@tonic-gate return (vmp); 15697c478bd9Sstevel@tonic-gate } 15707c478bd9Sstevel@tonic-gate 15717c478bd9Sstevel@tonic-gate vmem_t * 15727c478bd9Sstevel@tonic-gate vmem_xcreate(const char *name, void *base, size_t size, size_t quantum, 15737c478bd9Sstevel@tonic-gate vmem_ximport_t *afunc, vmem_free_t *ffunc, vmem_t *source, 15747c478bd9Sstevel@tonic-gate size_t qcache_max, int vmflag) 15757c478bd9Sstevel@tonic-gate { 15767c478bd9Sstevel@tonic-gate ASSERT(!(vmflag & (VMC_POPULATOR | VMC_XALLOC))); 15777c478bd9Sstevel@tonic-gate vmflag &= ~(VMC_POPULATOR | VMC_XALLOC); 15787c478bd9Sstevel@tonic-gate 15797c478bd9Sstevel@tonic-gate return (vmem_create_common(name, base, size, quantum, 15807c478bd9Sstevel@tonic-gate (vmem_alloc_t *)afunc, ffunc, source, qcache_max, 15817c478bd9Sstevel@tonic-gate vmflag | VMC_XALLOC)); 15827c478bd9Sstevel@tonic-gate } 15837c478bd9Sstevel@tonic-gate 15847c478bd9Sstevel@tonic-gate vmem_t * 15857c478bd9Sstevel@tonic-gate vmem_create(const char *name, void *base, size_t size, size_t quantum, 15867c478bd9Sstevel@tonic-gate vmem_alloc_t *afunc, vmem_free_t *ffunc, vmem_t *source, 15877c478bd9Sstevel@tonic-gate size_t qcache_max, int vmflag) 15887c478bd9Sstevel@tonic-gate { 1589aaa10e67Sha137994 ASSERT(!(vmflag & (VMC_XALLOC | VMC_XALIGN))); 1590aaa10e67Sha137994 vmflag &= ~(VMC_XALLOC | VMC_XALIGN); 15917c478bd9Sstevel@tonic-gate 15927c478bd9Sstevel@tonic-gate return (vmem_create_common(name, base, size, quantum, 15937c478bd9Sstevel@tonic-gate afunc, ffunc, source, qcache_max, vmflag)); 15947c478bd9Sstevel@tonic-gate } 15957c478bd9Sstevel@tonic-gate 15967c478bd9Sstevel@tonic-gate /* 15977c478bd9Sstevel@tonic-gate * Destroy arena vmp. 15987c478bd9Sstevel@tonic-gate */ 15997c478bd9Sstevel@tonic-gate void 16007c478bd9Sstevel@tonic-gate vmem_destroy(vmem_t *vmp) 16017c478bd9Sstevel@tonic-gate { 16027c478bd9Sstevel@tonic-gate vmem_t *cur, **vmpp; 16037c478bd9Sstevel@tonic-gate vmem_seg_t *seg0 = &vmp->vm_seg0; 16044a7ceddaSBlake Jones vmem_seg_t *vsp, *anext; 16057c478bd9Sstevel@tonic-gate size_t leaked; 16067c478bd9Sstevel@tonic-gate int i; 16077c478bd9Sstevel@tonic-gate 16087c478bd9Sstevel@tonic-gate mutex_enter(&vmem_list_lock); 16097c478bd9Sstevel@tonic-gate vmpp = &vmem_list; 16107c478bd9Sstevel@tonic-gate while ((cur = *vmpp) != vmp) 16117c478bd9Sstevel@tonic-gate vmpp = &cur->vm_next; 16127c478bd9Sstevel@tonic-gate *vmpp = vmp->vm_next; 16137c478bd9Sstevel@tonic-gate mutex_exit(&vmem_list_lock); 16147c478bd9Sstevel@tonic-gate 16157c478bd9Sstevel@tonic-gate for (i = 0; i < VMEM_NQCACHE_MAX; i++) 16167c478bd9Sstevel@tonic-gate if (vmp->vm_qcache[i]) 16177c478bd9Sstevel@tonic-gate kmem_cache_destroy(vmp->vm_qcache[i]); 16187c478bd9Sstevel@tonic-gate 16197c478bd9Sstevel@tonic-gate leaked = vmem_size(vmp, VMEM_ALLOC); 16207c478bd9Sstevel@tonic-gate if (leaked != 0) 16217c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "vmem_destroy('%s'): leaked %lu %s", 16227c478bd9Sstevel@tonic-gate vmp->vm_name, leaked, (vmp->vm_cflags & VMC_IDENTIFIER) ? 16237c478bd9Sstevel@tonic-gate "identifiers" : "bytes"); 16247c478bd9Sstevel@tonic-gate 16257c478bd9Sstevel@tonic-gate if (vmp->vm_hash_table != vmp->vm_hash0) 16267c478bd9Sstevel@tonic-gate vmem_free(vmem_hash_arena, vmp->vm_hash_table, 16277c478bd9Sstevel@tonic-gate (vmp->vm_hash_mask + 1) * sizeof (void *)); 16287c478bd9Sstevel@tonic-gate 16297c478bd9Sstevel@tonic-gate /* 16307c478bd9Sstevel@tonic-gate * Give back the segment structures for anything that's left in the 16317c478bd9Sstevel@tonic-gate * arena, e.g. the primary spans and their free segments. 16327c478bd9Sstevel@tonic-gate */ 16337c478bd9Sstevel@tonic-gate VMEM_DELETE(&vmp->vm_rotor, a); 16344a7ceddaSBlake Jones for (vsp = seg0->vs_anext; vsp != seg0; vsp = anext) { 16354a7ceddaSBlake Jones anext = vsp->vs_anext; 16367c478bd9Sstevel@tonic-gate vmem_putseg_global(vsp); 16374a7ceddaSBlake Jones } 16387c478bd9Sstevel@tonic-gate 16397c478bd9Sstevel@tonic-gate while (vmp->vm_nsegfree > 0) 16407c478bd9Sstevel@tonic-gate vmem_putseg_global(vmem_getseg(vmp)); 16417c478bd9Sstevel@tonic-gate 16427c478bd9Sstevel@tonic-gate kstat_delete(vmp->vm_ksp); 16437c478bd9Sstevel@tonic-gate 16447c478bd9Sstevel@tonic-gate mutex_destroy(&vmp->vm_lock); 16457c478bd9Sstevel@tonic-gate cv_destroy(&vmp->vm_cv); 16467c478bd9Sstevel@tonic-gate vmem_free(vmem_vmem_arena, vmp, sizeof (vmem_t)); 16477c478bd9Sstevel@tonic-gate } 16487c478bd9Sstevel@tonic-gate 16497c478bd9Sstevel@tonic-gate /* 1650*47bb2664SMatthew Ahrens * Only shrink vmem hashtable if it is 1<<vmem_rescale_minshift times (8x) 1651*47bb2664SMatthew Ahrens * larger than necessary. 1652*47bb2664SMatthew Ahrens */ 1653*47bb2664SMatthew Ahrens int vmem_rescale_minshift = 3; 1654*47bb2664SMatthew Ahrens 1655*47bb2664SMatthew Ahrens /* 16567c478bd9Sstevel@tonic-gate * Resize vmp's hash table to keep the average lookup depth near 1.0. 16577c478bd9Sstevel@tonic-gate */ 16587c478bd9Sstevel@tonic-gate static void 16597c478bd9Sstevel@tonic-gate vmem_hash_rescale(vmem_t *vmp) 16607c478bd9Sstevel@tonic-gate { 16617c478bd9Sstevel@tonic-gate vmem_seg_t **old_table, **new_table, *vsp; 16627c478bd9Sstevel@tonic-gate size_t old_size, new_size, h, nseg; 16637c478bd9Sstevel@tonic-gate 16647c478bd9Sstevel@tonic-gate nseg = (size_t)(vmp->vm_kstat.vk_alloc.value.ui64 - 16657c478bd9Sstevel@tonic-gate vmp->vm_kstat.vk_free.value.ui64); 16667c478bd9Sstevel@tonic-gate 16677c478bd9Sstevel@tonic-gate new_size = MAX(VMEM_HASH_INITIAL, 1 << (highbit(3 * nseg + 4) - 2)); 16687c478bd9Sstevel@tonic-gate old_size = vmp->vm_hash_mask + 1; 16697c478bd9Sstevel@tonic-gate 1670*47bb2664SMatthew Ahrens if ((old_size >> vmem_rescale_minshift) <= new_size && 1671*47bb2664SMatthew Ahrens new_size <= (old_size << 1)) 16727c478bd9Sstevel@tonic-gate return; 16737c478bd9Sstevel@tonic-gate 16747c478bd9Sstevel@tonic-gate new_table = vmem_alloc(vmem_hash_arena, new_size * sizeof (void *), 16757c478bd9Sstevel@tonic-gate VM_NOSLEEP); 16767c478bd9Sstevel@tonic-gate if (new_table == NULL) 16777c478bd9Sstevel@tonic-gate return; 16787c478bd9Sstevel@tonic-gate bzero(new_table, new_size * sizeof (void *)); 16797c478bd9Sstevel@tonic-gate 16807c478bd9Sstevel@tonic-gate mutex_enter(&vmp->vm_lock); 16817c478bd9Sstevel@tonic-gate 16827c478bd9Sstevel@tonic-gate old_size = vmp->vm_hash_mask + 1; 16837c478bd9Sstevel@tonic-gate old_table = vmp->vm_hash_table; 16847c478bd9Sstevel@tonic-gate 16857c478bd9Sstevel@tonic-gate vmp->vm_hash_mask = new_size - 1; 16867c478bd9Sstevel@tonic-gate vmp->vm_hash_table = new_table; 16877c478bd9Sstevel@tonic-gate vmp->vm_hash_shift = highbit(vmp->vm_hash_mask); 16887c478bd9Sstevel@tonic-gate 16897c478bd9Sstevel@tonic-gate for (h = 0; h < old_size; h++) { 16907c478bd9Sstevel@tonic-gate vsp = old_table[h]; 16917c478bd9Sstevel@tonic-gate while (vsp != NULL) { 16927c478bd9Sstevel@tonic-gate uintptr_t addr = vsp->vs_start; 16937c478bd9Sstevel@tonic-gate vmem_seg_t *next_vsp = vsp->vs_knext; 16947c478bd9Sstevel@tonic-gate vmem_seg_t **hash_bucket = VMEM_HASH(vmp, addr); 16957c478bd9Sstevel@tonic-gate vsp->vs_knext = *hash_bucket; 16967c478bd9Sstevel@tonic-gate *hash_bucket = vsp; 16977c478bd9Sstevel@tonic-gate vsp = next_vsp; 16987c478bd9Sstevel@tonic-gate } 16997c478bd9Sstevel@tonic-gate } 17007c478bd9Sstevel@tonic-gate 17017c478bd9Sstevel@tonic-gate mutex_exit(&vmp->vm_lock); 17027c478bd9Sstevel@tonic-gate 17037c478bd9Sstevel@tonic-gate if (old_table != vmp->vm_hash0) 17047c478bd9Sstevel@tonic-gate vmem_free(vmem_hash_arena, old_table, 17057c478bd9Sstevel@tonic-gate old_size * sizeof (void *)); 17067c478bd9Sstevel@tonic-gate } 17077c478bd9Sstevel@tonic-gate 17087c478bd9Sstevel@tonic-gate /* 17097c478bd9Sstevel@tonic-gate * Perform periodic maintenance on all vmem arenas. 17107c478bd9Sstevel@tonic-gate */ 17117c478bd9Sstevel@tonic-gate void 17127c478bd9Sstevel@tonic-gate vmem_update(void *dummy) 17137c478bd9Sstevel@tonic-gate { 17147c478bd9Sstevel@tonic-gate vmem_t *vmp; 17157c478bd9Sstevel@tonic-gate 17167c478bd9Sstevel@tonic-gate mutex_enter(&vmem_list_lock); 17177c478bd9Sstevel@tonic-gate for (vmp = vmem_list; vmp != NULL; vmp = vmp->vm_next) { 17187c478bd9Sstevel@tonic-gate /* 17197c478bd9Sstevel@tonic-gate * If threads are waiting for resources, wake them up 17207c478bd9Sstevel@tonic-gate * periodically so they can issue another kmem_reap() 17217c478bd9Sstevel@tonic-gate * to reclaim resources cached by the slab allocator. 17227c478bd9Sstevel@tonic-gate */ 17237c478bd9Sstevel@tonic-gate cv_broadcast(&vmp->vm_cv); 17247c478bd9Sstevel@tonic-gate 17257c478bd9Sstevel@tonic-gate /* 17267c478bd9Sstevel@tonic-gate * Rescale the hash table to keep the hash chains short. 17277c478bd9Sstevel@tonic-gate */ 17287c478bd9Sstevel@tonic-gate vmem_hash_rescale(vmp); 17297c478bd9Sstevel@tonic-gate } 17307c478bd9Sstevel@tonic-gate mutex_exit(&vmem_list_lock); 17317c478bd9Sstevel@tonic-gate 17327c478bd9Sstevel@tonic-gate (void) timeout(vmem_update, dummy, vmem_update_interval * hz); 17337c478bd9Sstevel@tonic-gate } 17347c478bd9Sstevel@tonic-gate 173594dd93aeSGeorge Wilson void 173694dd93aeSGeorge Wilson vmem_qcache_reap(vmem_t *vmp) 173794dd93aeSGeorge Wilson { 173894dd93aeSGeorge Wilson int i; 173994dd93aeSGeorge Wilson 174094dd93aeSGeorge Wilson /* 174194dd93aeSGeorge Wilson * Reap any quantum caches that may be part of this vmem. 174294dd93aeSGeorge Wilson */ 174394dd93aeSGeorge Wilson for (i = 0; i < VMEM_NQCACHE_MAX; i++) 174494dd93aeSGeorge Wilson if (vmp->vm_qcache[i]) 174594dd93aeSGeorge Wilson kmem_cache_reap_now(vmp->vm_qcache[i]); 174694dd93aeSGeorge Wilson } 174794dd93aeSGeorge Wilson 17487c478bd9Sstevel@tonic-gate /* 17497c478bd9Sstevel@tonic-gate * Prepare vmem for use. 17507c478bd9Sstevel@tonic-gate */ 17517c478bd9Sstevel@tonic-gate vmem_t * 17527c478bd9Sstevel@tonic-gate vmem_init(const char *heap_name, 17537c478bd9Sstevel@tonic-gate void *heap_start, size_t heap_size, size_t heap_quantum, 17547c478bd9Sstevel@tonic-gate void *(*heap_alloc)(vmem_t *, size_t, int), 17557c478bd9Sstevel@tonic-gate void (*heap_free)(vmem_t *, void *, size_t)) 17567c478bd9Sstevel@tonic-gate { 17577c478bd9Sstevel@tonic-gate uint32_t id; 17587c478bd9Sstevel@tonic-gate int nseg = VMEM_SEG_INITIAL; 17597c478bd9Sstevel@tonic-gate vmem_t *heap; 17607c478bd9Sstevel@tonic-gate 17617c478bd9Sstevel@tonic-gate while (--nseg >= 0) 17627c478bd9Sstevel@tonic-gate vmem_putseg_global(&vmem_seg0[nseg]); 17637c478bd9Sstevel@tonic-gate 17647c478bd9Sstevel@tonic-gate heap = vmem_create(heap_name, 17657c478bd9Sstevel@tonic-gate heap_start, heap_size, heap_quantum, 17667c478bd9Sstevel@tonic-gate NULL, NULL, NULL, 0, 17677c478bd9Sstevel@tonic-gate VM_SLEEP | VMC_POPULATOR); 17687c478bd9Sstevel@tonic-gate 17697c478bd9Sstevel@tonic-gate vmem_metadata_arena = vmem_create("vmem_metadata", 17707c478bd9Sstevel@tonic-gate NULL, 0, heap_quantum, 17717c478bd9Sstevel@tonic-gate vmem_alloc, vmem_free, heap, 8 * heap_quantum, 17727c478bd9Sstevel@tonic-gate VM_SLEEP | VMC_POPULATOR | VMC_NO_QCACHE); 17737c478bd9Sstevel@tonic-gate 17747c478bd9Sstevel@tonic-gate vmem_seg_arena = vmem_create("vmem_seg", 17757c478bd9Sstevel@tonic-gate NULL, 0, heap_quantum, 17767c478bd9Sstevel@tonic-gate heap_alloc, heap_free, vmem_metadata_arena, 0, 17777c478bd9Sstevel@tonic-gate VM_SLEEP | VMC_POPULATOR); 17787c478bd9Sstevel@tonic-gate 17797c478bd9Sstevel@tonic-gate vmem_hash_arena = vmem_create("vmem_hash", 17807c478bd9Sstevel@tonic-gate NULL, 0, 8, 17817c478bd9Sstevel@tonic-gate heap_alloc, heap_free, vmem_metadata_arena, 0, 17827c478bd9Sstevel@tonic-gate VM_SLEEP); 17837c478bd9Sstevel@tonic-gate 17847c478bd9Sstevel@tonic-gate vmem_vmem_arena = vmem_create("vmem_vmem", 17857c478bd9Sstevel@tonic-gate vmem0, sizeof (vmem0), 1, 17867c478bd9Sstevel@tonic-gate heap_alloc, heap_free, vmem_metadata_arena, 0, 17877c478bd9Sstevel@tonic-gate VM_SLEEP); 17887c478bd9Sstevel@tonic-gate 17897c478bd9Sstevel@tonic-gate for (id = 0; id < vmem_id; id++) 17907c478bd9Sstevel@tonic-gate (void) vmem_xalloc(vmem_vmem_arena, sizeof (vmem_t), 17917c478bd9Sstevel@tonic-gate 1, 0, 0, &vmem0[id], &vmem0[id + 1], 17927c478bd9Sstevel@tonic-gate VM_NOSLEEP | VM_BESTFIT | VM_PANIC); 17937c478bd9Sstevel@tonic-gate 17947c478bd9Sstevel@tonic-gate return (heap); 17957c478bd9Sstevel@tonic-gate } 1796