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