1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include "mtlib.h" 30*0Sstevel@tonic-gate #include "umem_base.h" 31*0Sstevel@tonic-gate #include "vmem_base.h" 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #include <unistd.h> 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate /* 36*0Sstevel@tonic-gate * The following functions are for pre- and post-fork1(2) handling. 37*0Sstevel@tonic-gate */ 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate static void 40*0Sstevel@tonic-gate umem_lockup_cache(umem_cache_t *cp) 41*0Sstevel@tonic-gate { 42*0Sstevel@tonic-gate int idx; 43*0Sstevel@tonic-gate int ncpus = cp->cache_cpu_mask + 1; 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate for (idx = 0; idx < ncpus; idx++) 46*0Sstevel@tonic-gate (void) mutex_lock(&cp->cache_cpu[idx].cc_lock); 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate (void) mutex_lock(&cp->cache_depot_lock); 49*0Sstevel@tonic-gate (void) mutex_lock(&cp->cache_lock); 50*0Sstevel@tonic-gate } 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate static void 53*0Sstevel@tonic-gate umem_release_cache(umem_cache_t *cp) 54*0Sstevel@tonic-gate { 55*0Sstevel@tonic-gate int idx; 56*0Sstevel@tonic-gate int ncpus = cp->cache_cpu_mask + 1; 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate (void) mutex_unlock(&cp->cache_lock); 59*0Sstevel@tonic-gate (void) mutex_unlock(&cp->cache_depot_lock); 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate for (idx = 0; idx < ncpus; idx++) 62*0Sstevel@tonic-gate (void) mutex_unlock(&cp->cache_cpu[idx].cc_lock); 63*0Sstevel@tonic-gate } 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate static void 66*0Sstevel@tonic-gate umem_lockup_log_header(umem_log_header_t *lhp) 67*0Sstevel@tonic-gate { 68*0Sstevel@tonic-gate int idx; 69*0Sstevel@tonic-gate if (lhp == NULL) 70*0Sstevel@tonic-gate return; 71*0Sstevel@tonic-gate for (idx = 0; idx < umem_max_ncpus; idx++) 72*0Sstevel@tonic-gate (void) mutex_lock(&lhp->lh_cpu[idx].clh_lock); 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate (void) mutex_lock(&lhp->lh_lock); 75*0Sstevel@tonic-gate } 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate static void 78*0Sstevel@tonic-gate umem_release_log_header(umem_log_header_t *lhp) 79*0Sstevel@tonic-gate { 80*0Sstevel@tonic-gate int idx; 81*0Sstevel@tonic-gate if (lhp == NULL) 82*0Sstevel@tonic-gate return; 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate (void) mutex_unlock(&lhp->lh_lock); 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate for (idx = 0; idx < umem_max_ncpus; idx++) 87*0Sstevel@tonic-gate (void) mutex_unlock(&lhp->lh_cpu[idx].clh_lock); 88*0Sstevel@tonic-gate } 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate static void 91*0Sstevel@tonic-gate umem_lockup(void) 92*0Sstevel@tonic-gate { 93*0Sstevel@tonic-gate umem_cache_t *cp; 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate (void) mutex_lock(&umem_init_lock); 96*0Sstevel@tonic-gate /* 97*0Sstevel@tonic-gate * If another thread is busy initializing the library, we must 98*0Sstevel@tonic-gate * wait for it to complete (by calling umem_init()) before allowing 99*0Sstevel@tonic-gate * the fork() to proceed. 100*0Sstevel@tonic-gate */ 101*0Sstevel@tonic-gate if (umem_ready == UMEM_READY_INITING && umem_init_thr != thr_self()) { 102*0Sstevel@tonic-gate (void) mutex_unlock(&umem_init_lock); 103*0Sstevel@tonic-gate (void) umem_init(); 104*0Sstevel@tonic-gate (void) mutex_lock(&umem_init_lock); 105*0Sstevel@tonic-gate } 106*0Sstevel@tonic-gate (void) mutex_lock(&umem_cache_lock); 107*0Sstevel@tonic-gate (void) mutex_lock(&umem_update_lock); 108*0Sstevel@tonic-gate (void) mutex_lock(&umem_flags_lock); 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate umem_lockup_cache(&umem_null_cache); 111*0Sstevel@tonic-gate for (cp = umem_null_cache.cache_prev; cp != &umem_null_cache; 112*0Sstevel@tonic-gate cp = cp->cache_prev) 113*0Sstevel@tonic-gate umem_lockup_cache(cp); 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate umem_lockup_log_header(umem_transaction_log); 116*0Sstevel@tonic-gate umem_lockup_log_header(umem_content_log); 117*0Sstevel@tonic-gate umem_lockup_log_header(umem_failure_log); 118*0Sstevel@tonic-gate umem_lockup_log_header(umem_slab_log); 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate (void) cond_broadcast(&umem_update_cv); 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate vmem_sbrk_lockup(); 123*0Sstevel@tonic-gate vmem_lockup(); 124*0Sstevel@tonic-gate } 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate static void 127*0Sstevel@tonic-gate umem_release(void) 128*0Sstevel@tonic-gate { 129*0Sstevel@tonic-gate umem_cache_t *cp; 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate vmem_release(); 132*0Sstevel@tonic-gate vmem_sbrk_release(); 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate umem_release_log_header(umem_slab_log); 135*0Sstevel@tonic-gate umem_release_log_header(umem_failure_log); 136*0Sstevel@tonic-gate umem_release_log_header(umem_content_log); 137*0Sstevel@tonic-gate umem_release_log_header(umem_transaction_log); 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate for (cp = umem_null_cache.cache_next; cp != &umem_null_cache; 140*0Sstevel@tonic-gate cp = cp->cache_next) 141*0Sstevel@tonic-gate umem_release_cache(cp); 142*0Sstevel@tonic-gate umem_release_cache(&umem_null_cache); 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate (void) mutex_unlock(&umem_flags_lock); 145*0Sstevel@tonic-gate (void) mutex_unlock(&umem_update_lock); 146*0Sstevel@tonic-gate (void) mutex_unlock(&umem_cache_lock); 147*0Sstevel@tonic-gate (void) mutex_unlock(&umem_init_lock); 148*0Sstevel@tonic-gate } 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gate static void 151*0Sstevel@tonic-gate umem_release_child(void) 152*0Sstevel@tonic-gate { 153*0Sstevel@tonic-gate umem_cache_t *cp; 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate /* 156*0Sstevel@tonic-gate * Clean up the update state 157*0Sstevel@tonic-gate */ 158*0Sstevel@tonic-gate umem_update_thr = 0; 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate if (umem_st_update_thr != thr_self()) { 161*0Sstevel@tonic-gate umem_st_update_thr = 0; 162*0Sstevel@tonic-gate umem_reaping = UMEM_REAP_DONE; 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate for (cp = umem_null_cache.cache_next; cp != &umem_null_cache; 165*0Sstevel@tonic-gate cp = cp->cache_next) { 166*0Sstevel@tonic-gate if (cp->cache_uflags & UMU_NOTIFY) 167*0Sstevel@tonic-gate cp->cache_uflags &= ~UMU_NOTIFY; 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate /* 170*0Sstevel@tonic-gate * If the cache is active, we just re-add it to 171*0Sstevel@tonic-gate * the update list. This will re-do any active 172*0Sstevel@tonic-gate * updates on the cache, but that won't break 173*0Sstevel@tonic-gate * anything. 174*0Sstevel@tonic-gate * 175*0Sstevel@tonic-gate * The worst that can happen is a cache has 176*0Sstevel@tonic-gate * its magazines rescaled twice, instead of once. 177*0Sstevel@tonic-gate */ 178*0Sstevel@tonic-gate if (cp->cache_uflags & UMU_ACTIVE) { 179*0Sstevel@tonic-gate umem_cache_t *cnext, *cprev; 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate ASSERT(cp->cache_unext == NULL && 182*0Sstevel@tonic-gate cp->cache_uprev == NULL); 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate cp->cache_uflags &= ~UMU_ACTIVE; 185*0Sstevel@tonic-gate cp->cache_unext = cnext = &umem_null_cache; 186*0Sstevel@tonic-gate cp->cache_uprev = cprev = 187*0Sstevel@tonic-gate umem_null_cache.cache_uprev; 188*0Sstevel@tonic-gate cnext->cache_uprev = cp; 189*0Sstevel@tonic-gate cprev->cache_unext = cp; 190*0Sstevel@tonic-gate } 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate } 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate umem_release(); 195*0Sstevel@tonic-gate } 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate void 198*0Sstevel@tonic-gate umem_forkhandler_init(void) 199*0Sstevel@tonic-gate { 200*0Sstevel@tonic-gate /* 201*0Sstevel@tonic-gate * There is no way to unregister these atfork functions, 202*0Sstevel@tonic-gate * but we don't need to. The dynamic linker and libc take 203*0Sstevel@tonic-gate * care of unregistering them if/when the library is unloaded. 204*0Sstevel@tonic-gate */ 205*0Sstevel@tonic-gate (void) pthread_atfork(umem_lockup, umem_release, umem_release_child); 206*0Sstevel@tonic-gate } 207