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