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 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 #include <sys/param.h>
27 #include <sys/t_lock.h>
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #include <sys/sysmacros.h>
31 #include <sys/systm.h>
32 #include <sys/cpuvar.h>
33 #include <sys/user.h>
34 #include <sys/proc.h>
35 #include <sys/callb.h>
36 #include <sys/kmem.h>
37 #include <sys/cmn_err.h>
38 #include <sys/swap.h>
39 #include <sys/vmsystm.h>
40 #include <sys/class.h>
41 #include <sys/debug.h>
42 #include <sys/thread.h>
43 #include <sys/kobj.h>
44 #include <sys/ddi.h> /* for delay() */
45 #include <sys/taskq.h> /* For TASKQ_NAMELEN */
46 #include <sys/list.h>
47
48 #define CB_MAXNAME TASKQ_NAMELEN
49
50 /*
51 * The callb mechanism provides generic event scheduling/echoing.
52 * A callb function is registered and called on behalf of the event.
53 */
54 typedef struct callb {
55 list_node_t c_list; /* list in class */
56 kthread_id_t c_thread; /* ptr to caller's thread struct */
57 int c_refcount; /* don't free this callb_t if > 0 */
58 char c_executing; /* serialize call of c_func */
59 uchar_t c_class; /* this callb's class */
60 kcondvar_t c_done_cv; /* signal callb completion */
61 boolean_t (*c_func)(); /* cb function: returns true if ok */
62 void *c_arg; /* arg to c_func */
63 char c_name[CB_MAXNAME+1]; /* debug:max func name length */
64 } callb_t;
65
66 /*
67 * Basic structure for a callb table.
68 * All callbs are organized into different class groups described
69 * by ct_class array.
70 * The callbs within a class are single-linked and normally run by a
71 * serial execution.
72 */
73 typedef struct callb_table {
74 kmutex_t ct_lock; /* protect all callb states */
75 int ct_busy; /* != 0 prevents additions */
76 kcondvar_t ct_busy_cv; /* to wait for not busy */
77 list_t ct_cb[NCBCLASS]; /* list of callb in a class */
78 } callb_table_t;
79
80 int callb_timeout_sec = CPR_KTHREAD_TIMEOUT_SEC;
81
82 static callb_id_t callb_add_common(boolean_t (*)(void *, int),
83 void *, int, char *, kthread_id_t);
84
85 static callb_table_t callb_table; /* system level callback table */
86 static callb_table_t *ct = &callb_table;
87 static kmutex_t callb_safe_mutex;
88 callb_cpr_t callb_cprinfo_safe = {
89 &callb_safe_mutex, CALLB_CPR_ALWAYS_SAFE, 0, 0, 0 };
90
91 /*
92 * Init all callb tables in the system.
93 */
94 void
callb_init()95 callb_init()
96 {
97 int class;
98
99 callb_table.ct_busy = 0; /* mark table open for additions */
100 mutex_init(&callb_safe_mutex, NULL, MUTEX_DEFAULT, NULL);
101 mutex_init(&callb_table.ct_lock, NULL, MUTEX_DEFAULT, NULL);
102 for (class = 0; class < NCBCLASS; class++) {
103 list_create(&callb_table.ct_cb[class], sizeof(callb_t),
104 offsetof(callb_t, c_list));
105 }
106 }
107
108 /*
109 * callout_add() is called to register func() be called later.
110 */
111 static callb_id_t
callb_add_common(boolean_t (* func)(void * arg,int code),void * arg,int class,char * name,kthread_id_t t)112 callb_add_common(boolean_t (*func)(void *arg, int code),
113 void *arg, int class, char *name, kthread_id_t t)
114 {
115 callb_t *cp;
116
117 ASSERT(class < NCBCLASS);
118
119 mutex_enter(&ct->ct_lock);
120 while (ct->ct_busy)
121 cv_wait(&ct->ct_busy_cv, &ct->ct_lock);
122 cp = (callb_t *)kmem_zalloc(sizeof (callb_t), KM_SLEEP);
123 cp->c_thread = t;
124 cp->c_func = func;
125 cp->c_arg = arg;
126 cp->c_class = (uchar_t)class;
127 #ifdef DEBUG
128 if (strlen(name) > CB_MAXNAME)
129 cmn_err(CE_WARN, "callb_add: name of callback function '%s' "
130 "too long -- truncated to %d chars",
131 name, CB_MAXNAME);
132 #endif
133 (void) strncpy(cp->c_name, name, CB_MAXNAME);
134 cp->c_name[CB_MAXNAME] = '\0';
135
136 /*
137 * Insert the new callb at the head of its class list.
138 */
139 list_insert_head(&ct->ct_cb[class], cp);
140
141 mutex_exit(&ct->ct_lock);
142 return ((callb_id_t)cp);
143 }
144
145 /*
146 * The default function to add an entry to the callback table. Since
147 * it uses curthread as the thread identifier to store in the table,
148 * it should be used for the normal case of a thread which is calling
149 * to add ITSELF to the table.
150 */
151 callb_id_t
callb_add(boolean_t (* func)(void * arg,int code),void * arg,int class,char * name)152 callb_add(boolean_t (*func)(void *arg, int code),
153 void *arg, int class, char *name)
154 {
155 return (callb_add_common(func, arg, class, name, curthread));
156 }
157
158 /*
159 * A special version of callb_add() above for use by threads which
160 * might be adding an entry to the table on behalf of some other
161 * thread (for example, one which is constructed but not yet running).
162 * In this version the thread id is an argument.
163 */
164 callb_id_t
callb_add_thread(boolean_t (* func)(void * arg,int code),void * arg,int class,char * name,kthread_id_t t)165 callb_add_thread(boolean_t (*func)(void *arg, int code),
166 void *arg, int class, char *name, kthread_id_t t)
167 {
168 return (callb_add_common(func, arg, class, name, t));
169 }
170
171 /*
172 * callout_delete() is called to remove an entry identified by id
173 * that was originally placed there by a call to callout_add().
174 * return -1 if fail to delete a callb entry otherwise return 0.
175 */
176 int
callb_delete(callb_id_t id)177 callb_delete(callb_id_t id)
178 {
179 callb_t *me = (callb_t *)id;
180
181 mutex_enter(&ct->ct_lock);
182
183 for (;;) {
184 #ifdef DEBUG
185 callb_t *cp;
186
187 cp = (callb_t *)list_head(&ct->ct_cb[me->c_class]);
188 while (cp != NULL && cp != me)
189 cp = (callb_t *)list_next(&ct->ct_cb[me->c_class], cp);
190
191 if (cp != me) {
192 cmn_err(CE_WARN, "callb delete bogus entry 0x%p",
193 (void *)me);
194 mutex_exit(&ct->ct_lock);
195 return (-1);
196 }
197 #endif /* DEBUG */
198
199 /*
200 * It is not allowed to delete a callb in the middle of
201 * executing otherwise, the callb_execute() will be confused.
202 */
203 if (me->c_refcount == 0)
204 break;
205
206 cv_wait(&me->c_done_cv, &ct->ct_lock);
207 }
208 /* relink the class list */
209 list_remove(&ct->ct_cb[me->c_class], me);
210
211 /* free callb */
212 kmem_free(me, sizeof (callb_t));
213
214 mutex_exit(&ct->ct_lock);
215 return (0);
216 }
217
218 /*
219 * class: indicates to execute all callbs in the same class;
220 * code: optional argument for the callb functions.
221 * return: = 0: success
222 * != 0: ptr to string supplied when callback was registered
223 */
224 void *
callb_execute_class(int class,int code)225 callb_execute_class(int class, int code)
226 {
227 callb_t *cp;
228 void *ret = NULL;
229
230 ASSERT(class < NCBCLASS);
231
232 mutex_enter(&ct->ct_lock);
233
234 for (cp = (callb_t *)list_head(&ct->ct_cb[class]);
235 cp != NULL && ret == 0;
236 cp = (callb_t *)list_next(&ct->ct_cb[class], cp)) {
237 cp->c_refcount++;
238
239 while (cp->c_executing)
240 cv_wait(&cp->c_done_cv, &ct->ct_lock);
241 cp->c_executing = 1;
242
243 #ifdef CALLB_DEBUG
244 printf("callb_execute: name=%s func=%p arg=%p\n",
245 cp->c_name, (void *)cp->c_func, (void *)cp->c_arg);
246 #endif /* CALLB_DEBUG */
247
248 mutex_exit(&ct->ct_lock);
249 /* If callback function fails, pass back client's name */
250 if (!(*cp->c_func)(cp->c_arg, code))
251 ret = cp->c_name;
252 mutex_enter(&ct->ct_lock);
253
254 cp->c_executing = 0;
255 cp->c_refcount--;
256 cv_broadcast(&cp->c_done_cv);
257 }
258 mutex_exit(&ct->ct_lock);
259 return (ret);
260 }
261
262 /*
263 * callers make sure no recursive entries to this func.
264 * dp->cc_lockp is registered by callb_add to protect callb_cpr_t structure.
265 *
266 * When calling to stop a kernel thread (code == CB_CODE_CPR_CHKPT) we
267 * use a cv_timedwait() in case the kernel thread is blocked.
268 *
269 * Note that this is a generic callback handler for daemon CPR and
270 * should NOT be changed to accommodate any specific requirement in a daemon.
271 * Individual daemons that require changes to the handler shall write
272 * callback routines in their own daemon modules.
273 */
274 boolean_t
callb_generic_cpr(void * arg,int code)275 callb_generic_cpr(void *arg, int code)
276 {
277 callb_cpr_t *cp = (callb_cpr_t *)arg;
278 clock_t ret = 0; /* assume success */
279
280 mutex_enter(cp->cc_lockp);
281
282 switch (code) {
283 case CB_CODE_CPR_CHKPT:
284 cp->cc_events |= CALLB_CPR_START;
285 #ifdef CPR_NOT_THREAD_SAFE
286 while (!(cp->cc_events & CALLB_CPR_SAFE))
287 /* cv_timedwait() returns -1 if it times out. */
288 if ((ret = cv_reltimedwait(&cp->cc_callb_cv,
289 cp->cc_lockp, (callb_timeout_sec * hz),
290 TR_CLOCK_TICK)) == -1)
291 break;
292 #endif
293 break;
294
295 case CB_CODE_CPR_RESUME:
296 cp->cc_events &= ~CALLB_CPR_START;
297 cv_signal(&cp->cc_stop_cv);
298 break;
299 }
300 mutex_exit(cp->cc_lockp);
301 return (ret != -1);
302 }
303
304 /*
305 * The generic callback function associated with kernel threads which
306 * are always considered safe.
307 */
308 /* ARGSUSED */
309 boolean_t
callb_generic_cpr_safe(void * arg,int code)310 callb_generic_cpr_safe(void *arg, int code)
311 {
312 return (B_TRUE);
313 }
314 /*
315 * Prevent additions to callback table.
316 */
317 void
callb_lock_table(void)318 callb_lock_table(void)
319 {
320 mutex_enter(&ct->ct_lock);
321 ASSERT(ct->ct_busy == 0);
322 ct->ct_busy = 1;
323 mutex_exit(&ct->ct_lock);
324 }
325
326 /*
327 * Allow additions to callback table.
328 */
329 void
callb_unlock_table(void)330 callb_unlock_table(void)
331 {
332 mutex_enter(&ct->ct_lock);
333 ASSERT(ct->ct_busy != 0);
334 ct->ct_busy = 0;
335 cv_broadcast(&ct->ct_busy_cv);
336 mutex_exit(&ct->ct_lock);
337 }
338
339 /*
340 * Return a boolean value indicating whether a particular kernel thread is
341 * stopped in accordance with the cpr callback protocol. If returning
342 * false, also return a pointer to the thread name via the 2nd argument.
343 */
344 boolean_t
callb_is_stopped(kthread_id_t tp,caddr_t * thread_name)345 callb_is_stopped(kthread_id_t tp, caddr_t *thread_name)
346 {
347 callb_t *cp;
348 boolean_t ret_val;
349
350 mutex_enter(&ct->ct_lock);
351
352 for (cp = (callb_t *)list_head(&ct->ct_cb[CB_CL_CPR_DAEMON]);
353 cp != NULL && tp != cp->c_thread;
354 cp = (callb_t *)list_next(&ct->ct_cb[CB_CL_CPR_DAEMON], cp))
355 ;
356
357 ret_val = (cp != NULL);
358 if (ret_val) {
359 /*
360 * We found the thread in the callback table and have
361 * provisionally set the return value to true. Now
362 * see if it is marked "safe" and is sleeping or stopped.
363 */
364 callb_cpr_t *ccp = (callb_cpr_t *)cp->c_arg;
365
366 *thread_name = cp->c_name; /* in case not stopped */
367 mutex_enter(ccp->cc_lockp);
368
369 if (ccp->cc_events & CALLB_CPR_SAFE) {
370 int retry;
371
372 mutex_exit(ccp->cc_lockp);
373 for (retry = 0; retry < CALLB_MAX_RETRY; retry++) {
374 thread_lock(tp);
375 if (tp->t_state & (TS_SLEEP | TS_STOPPED)) {
376 thread_unlock(tp);
377 break;
378 }
379 thread_unlock(tp);
380 delay(CALLB_THREAD_DELAY);
381 }
382 ret_val = retry < CALLB_MAX_RETRY;
383 } else {
384 ret_val =
385 (ccp->cc_events & CALLB_CPR_ALWAYS_SAFE) != 0;
386 mutex_exit(ccp->cc_lockp);
387 }
388 } else {
389 /*
390 * Thread not found in callback table. Make the best
391 * attempt to identify the thread in the error message.
392 */
393 ulong_t offset;
394 char *sym = kobj_getsymname((uintptr_t)tp->t_startpc,
395 &offset);
396
397 *thread_name = sym ? sym : "*unknown*";
398 }
399
400 mutex_exit(&ct->ct_lock);
401 return (ret_val);
402 }
403