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