xref: /titanic_52/usr/src/lib/libumem/common/umem_fork.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
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
5*7257d1b4Sraf  * Common Development and Distribution License (the "License").
6*7257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21e8031f0aSraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "umem_base.h"
307c478bd9Sstevel@tonic-gate #include "vmem_base.h"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
351c326e94Sjwadams  * The following functions are for pre- and post-fork1(2) handling.  See
361c326e94Sjwadams  * "Lock Ordering" in lib/libumem/common/umem.c for the lock ordering used.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static void
407c478bd9Sstevel@tonic-gate umem_lockup_cache(umem_cache_t *cp)
417c478bd9Sstevel@tonic-gate {
427c478bd9Sstevel@tonic-gate 	int idx;
437c478bd9Sstevel@tonic-gate 	int ncpus = cp->cache_cpu_mask + 1;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < ncpus; idx++)
467c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&cp->cache_cpu[idx].cc_lock);
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cp->cache_depot_lock);
497c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cp->cache_lock);
507c478bd9Sstevel@tonic-gate }
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static void
537c478bd9Sstevel@tonic-gate umem_release_cache(umem_cache_t *cp)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	int idx;
567c478bd9Sstevel@tonic-gate 	int ncpus = cp->cache_cpu_mask + 1;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cp->cache_lock);
597c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cp->cache_depot_lock);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < ncpus; idx++)
627c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cp->cache_cpu[idx].cc_lock);
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static void
667c478bd9Sstevel@tonic-gate umem_lockup_log_header(umem_log_header_t *lhp)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	int idx;
697c478bd9Sstevel@tonic-gate 	if (lhp == NULL)
707c478bd9Sstevel@tonic-gate 		return;
717c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < umem_max_ncpus; idx++)
727c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&lhp->lh_cpu[idx].clh_lock);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&lhp->lh_lock);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static void
787c478bd9Sstevel@tonic-gate umem_release_log_header(umem_log_header_t *lhp)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	int idx;
817c478bd9Sstevel@tonic-gate 	if (lhp == NULL)
827c478bd9Sstevel@tonic-gate 		return;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&lhp->lh_lock);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < umem_max_ncpus; idx++)
877c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&lhp->lh_cpu[idx].clh_lock);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static void
917c478bd9Sstevel@tonic-gate umem_lockup(void)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	umem_cache_t *cp;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&umem_init_lock);
967c478bd9Sstevel@tonic-gate 	/*
977c478bd9Sstevel@tonic-gate 	 * If another thread is busy initializing the library, we must
987c478bd9Sstevel@tonic-gate 	 * wait for it to complete (by calling umem_init()) before allowing
997c478bd9Sstevel@tonic-gate 	 * the fork() to proceed.
1007c478bd9Sstevel@tonic-gate 	 */
1017c478bd9Sstevel@tonic-gate 	if (umem_ready == UMEM_READY_INITING && umem_init_thr != thr_self()) {
1027c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&umem_init_lock);
1037c478bd9Sstevel@tonic-gate 		(void) umem_init();
1047c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&umem_init_lock);
1057c478bd9Sstevel@tonic-gate 	}
1061c326e94Sjwadams 
1071c326e94Sjwadams 	vmem_lockup();
1081c326e94Sjwadams 	vmem_sbrk_lockup();
1091c326e94Sjwadams 
1107c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&umem_cache_lock);
1117c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&umem_update_lock);
1127c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&umem_flags_lock);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	umem_lockup_cache(&umem_null_cache);
1157c478bd9Sstevel@tonic-gate 	for (cp = umem_null_cache.cache_prev; cp != &umem_null_cache;
1167c478bd9Sstevel@tonic-gate 	    cp = cp->cache_prev)
1177c478bd9Sstevel@tonic-gate 		umem_lockup_cache(cp);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	umem_lockup_log_header(umem_transaction_log);
1207c478bd9Sstevel@tonic-gate 	umem_lockup_log_header(umem_content_log);
1217c478bd9Sstevel@tonic-gate 	umem_lockup_log_header(umem_failure_log);
1227c478bd9Sstevel@tonic-gate 	umem_lockup_log_header(umem_slab_log);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	(void) cond_broadcast(&umem_update_cv);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static void
1291c326e94Sjwadams umem_do_release(int as_child)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	umem_cache_t *cp;
1321c326e94Sjwadams 	int cleanup_update = 0;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	/*
1351c326e94Sjwadams 	 * Clean up the update state if we are the child process and
1361c326e94Sjwadams 	 * another thread was processing updates.
1377c478bd9Sstevel@tonic-gate 	 */
1381c326e94Sjwadams 	if (as_child) {
1391c326e94Sjwadams 		if (umem_update_thr != thr_self()) {
1407c478bd9Sstevel@tonic-gate 			umem_update_thr = 0;
1411c326e94Sjwadams 			cleanup_update = 1;
1421c326e94Sjwadams 		}
1437c478bd9Sstevel@tonic-gate 		if (umem_st_update_thr != thr_self()) {
1447c478bd9Sstevel@tonic-gate 			umem_st_update_thr = 0;
1451c326e94Sjwadams 			cleanup_update = 1;
1461c326e94Sjwadams 		}
1471c326e94Sjwadams 	}
1481c326e94Sjwadams 
1491c326e94Sjwadams 	if (cleanup_update) {
1507c478bd9Sstevel@tonic-gate 		umem_reaping = UMEM_REAP_DONE;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		for (cp = umem_null_cache.cache_next; cp != &umem_null_cache;
1537c478bd9Sstevel@tonic-gate 		    cp = cp->cache_next) {
1547c478bd9Sstevel@tonic-gate 			if (cp->cache_uflags & UMU_NOTIFY)
1557c478bd9Sstevel@tonic-gate 				cp->cache_uflags &= ~UMU_NOTIFY;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 			/*
1587c478bd9Sstevel@tonic-gate 			 * If the cache is active, we just re-add it to
1597c478bd9Sstevel@tonic-gate 			 * the update list.  This will re-do any active
1607c478bd9Sstevel@tonic-gate 			 * updates on the cache, but that won't break
1617c478bd9Sstevel@tonic-gate 			 * anything.
1627c478bd9Sstevel@tonic-gate 			 *
1637c478bd9Sstevel@tonic-gate 			 * The worst that can happen is a cache has
1647c478bd9Sstevel@tonic-gate 			 * its magazines rescaled twice, instead of once.
1657c478bd9Sstevel@tonic-gate 			 */
1667c478bd9Sstevel@tonic-gate 			if (cp->cache_uflags & UMU_ACTIVE) {
1677c478bd9Sstevel@tonic-gate 				umem_cache_t *cnext, *cprev;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 				ASSERT(cp->cache_unext == NULL &&
1707c478bd9Sstevel@tonic-gate 				    cp->cache_uprev == NULL);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 				cp->cache_uflags &= ~UMU_ACTIVE;
1737c478bd9Sstevel@tonic-gate 				cp->cache_unext = cnext = &umem_null_cache;
1747c478bd9Sstevel@tonic-gate 				cp->cache_uprev = cprev =
1757c478bd9Sstevel@tonic-gate 				    umem_null_cache.cache_uprev;
1767c478bd9Sstevel@tonic-gate 				cnext->cache_uprev = cp;
1777c478bd9Sstevel@tonic-gate 				cprev->cache_unext = cp;
1787c478bd9Sstevel@tonic-gate 			}
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1821c326e94Sjwadams 	umem_release_log_header(umem_slab_log);
1831c326e94Sjwadams 	umem_release_log_header(umem_failure_log);
1841c326e94Sjwadams 	umem_release_log_header(umem_content_log);
1851c326e94Sjwadams 	umem_release_log_header(umem_transaction_log);
1861c326e94Sjwadams 
1871c326e94Sjwadams 	for (cp = umem_null_cache.cache_next; cp != &umem_null_cache;
1881c326e94Sjwadams 	    cp = cp->cache_next)
1891c326e94Sjwadams 		umem_release_cache(cp);
1901c326e94Sjwadams 	umem_release_cache(&umem_null_cache);
1911c326e94Sjwadams 
1921c326e94Sjwadams 	(void) mutex_unlock(&umem_flags_lock);
1931c326e94Sjwadams 	(void) mutex_unlock(&umem_update_lock);
1941c326e94Sjwadams 	(void) mutex_unlock(&umem_cache_lock);
1951c326e94Sjwadams 
1961c326e94Sjwadams 	vmem_sbrk_release();
1971c326e94Sjwadams 	vmem_release();
1981c326e94Sjwadams 
1991c326e94Sjwadams 	(void) mutex_unlock(&umem_init_lock);
2001c326e94Sjwadams }
2011c326e94Sjwadams 
2021c326e94Sjwadams static void
2031c326e94Sjwadams umem_release(void)
2041c326e94Sjwadams {
2051c326e94Sjwadams 	umem_do_release(0);
2061c326e94Sjwadams }
2071c326e94Sjwadams 
2081c326e94Sjwadams static void
2091c326e94Sjwadams umem_release_child(void)
2101c326e94Sjwadams {
2111c326e94Sjwadams 	umem_do_release(1);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate void
2157c478bd9Sstevel@tonic-gate umem_forkhandler_init(void)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	/*
2187c478bd9Sstevel@tonic-gate 	 * There is no way to unregister these atfork functions,
2197c478bd9Sstevel@tonic-gate 	 * but we don't need to.  The dynamic linker and libc take
2207c478bd9Sstevel@tonic-gate 	 * care of unregistering them if/when the library is unloaded.
2217c478bd9Sstevel@tonic-gate 	 */
2227c478bd9Sstevel@tonic-gate 	(void) pthread_atfork(umem_lockup, umem_release, umem_release_child);
2237c478bd9Sstevel@tonic-gate }
224