1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
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 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/time.h>
30 #include <sys/sysmacros.h>
31 #include <sys/systm.h>
32 #include <sys/proc.h>
33 #include <sys/mutex.h>
34 #include <sys/condvar.h>
35 #include <sys/callb.h>
36 #include <sys/kmem.h>
37 #include <sys/cmn_err.h>
38 #include <sys/debug.h>
39 #include <sys/kobj.h>
40 #include <sys/systm.h> /* for delay() */
41 #include <sys/taskq.h> /* For TASKQ_NAMELEN */
42 #include <sys/kernel.h>
43
44 #define CB_MAXNAME TASKQ_NAMELEN
45
46 /*
47 * The callb mechanism provides generic event scheduling/echoing.
48 * A callb function is registered and called on behalf of the event.
49 */
50 typedef struct callb {
51 struct callb *c_next; /* next in class or on freelist */
52 kthread_id_t c_thread; /* ptr to caller's thread struct */
53 char c_flag; /* info about the callb state */
54 uchar_t c_class; /* this callb's class */
55 kcondvar_t c_done_cv; /* signal callb completion */
56 boolean_t (*c_func)(void *, int);
57 /* 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 boolean_t ct_busy; /* B_TRUE 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 static void
callb_init(void * dummy __unused)100 callb_init(void *dummy __unused)
101 {
102 callb_table.ct_busy = B_FALSE; /* 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 static void
callb_fini(void * dummy __unused)108 callb_fini(void *dummy __unused)
109 {
110 callb_t *cp;
111 int i;
112
113 mutex_enter(&ct->ct_lock);
114 for (i = 0; i < 16; i++) {
115 while ((cp = ct->ct_freelist) != NULL) {
116 ct->ct_freelist = cp->c_next;
117 ct->ct_ncallb--;
118 kmem_free(cp, sizeof (callb_t));
119 }
120 if (ct->ct_ncallb == 0)
121 break;
122 /* Not all callbacks finished, waiting for the rest. */
123 mutex_exit(&ct->ct_lock);
124 tsleep(ct, 0, "callb", hz / 4);
125 mutex_enter(&ct->ct_lock);
126 }
127 if (ct->ct_ncallb > 0)
128 printf("%s: Leaked %d callbacks!\n", __func__, ct->ct_ncallb);
129 mutex_exit(&ct->ct_lock);
130 mutex_destroy(&callb_safe_mutex);
131 mutex_destroy(&callb_table.ct_lock);
132 }
133
134 /*
135 * callout_add() is called to register func() be called later.
136 */
137 static callb_id_t
callb_add_common(boolean_t (* func)(void * arg,int code),void * arg,int class,char * name,kthread_id_t t)138 callb_add_common(boolean_t (*func)(void *arg, int code),
139 void *arg, int class, char *name, kthread_id_t t)
140 {
141 callb_t *cp;
142
143 ASSERT3S(class, <, NCBCLASS);
144
145 mutex_enter(&ct->ct_lock);
146 while (ct->ct_busy)
147 cv_wait(&ct->ct_busy_cv, &ct->ct_lock);
148 if ((cp = ct->ct_freelist) == NULL) {
149 ct->ct_ncallb++;
150 cp = kmem_zalloc(sizeof (callb_t), KM_SLEEP);
151 }
152 ct->ct_freelist = cp->c_next;
153 cp->c_thread = t;
154 cp->c_func = func;
155 cp->c_arg = arg;
156 cp->c_class = (uchar_t)class;
157 cp->c_flag |= CALLB_TAKEN;
158 #ifdef ZFS_DEBUG
159 if (strlen(name) > CB_MAXNAME)
160 cmn_err(CE_WARN, "callb_add: name of callback function '%s' "
161 "too long -- truncated to %d chars",
162 name, CB_MAXNAME);
163 #endif
164 (void) strlcpy(cp->c_name, name, sizeof (cp->c_name));
165
166 /*
167 * Insert the new callb at the head of its class list.
168 */
169 cp->c_next = ct->ct_first_cb[class];
170 ct->ct_first_cb[class] = cp;
171
172 mutex_exit(&ct->ct_lock);
173 return ((callb_id_t)cp);
174 }
175
176 /*
177 * The default function to add an entry to the callback table. Since
178 * it uses curthread as the thread identifier to store in the table,
179 * it should be used for the normal case of a thread which is calling
180 * to add ITSELF to the table.
181 */
182 callb_id_t
callb_add(boolean_t (* func)(void * arg,int code),void * arg,int class,char * name)183 callb_add(boolean_t (*func)(void *arg, int code),
184 void *arg, int class, char *name)
185 {
186 return (callb_add_common(func, arg, class, name, curthread));
187 }
188
189 /*
190 * A special version of callb_add() above for use by threads which
191 * might be adding an entry to the table on behalf of some other
192 * thread (for example, one which is constructed but not yet running).
193 * In this version the thread id is an argument.
194 */
195 callb_id_t
callb_add_thread(boolean_t (* func)(void * arg,int code),void * arg,int class,char * name,kthread_id_t t)196 callb_add_thread(boolean_t (*func)(void *arg, int code),
197 void *arg, int class, char *name, kthread_id_t t)
198 {
199 return (callb_add_common(func, arg, class, name, t));
200 }
201
202 /*
203 * callout_delete() is called to remove an entry identified by id
204 * that was originally placed there by a call to callout_add().
205 * return -1 if fail to delete a callb entry otherwise return 0.
206 */
207 int
callb_delete(callb_id_t id)208 callb_delete(callb_id_t id)
209 {
210 callb_t **pp;
211 callb_t *me = (callb_t *)id;
212
213 mutex_enter(&ct->ct_lock);
214
215 for (;;) {
216 pp = &ct->ct_first_cb[me->c_class];
217 while (*pp != NULL && *pp != me)
218 pp = &(*pp)->c_next;
219
220 #ifdef ZFS_DEBUG
221 if (*pp != me) {
222 cmn_err(CE_WARN, "callb delete bogus entry 0x%p",
223 (void *)me);
224 mutex_exit(&ct->ct_lock);
225 return (-1);
226 }
227 #endif /* DEBUG */
228
229 /*
230 * It is not allowed to delete a callb in the middle of
231 * executing otherwise, the callb_execute() will be confused.
232 */
233 if (!(me->c_flag & CALLB_EXECUTING))
234 break;
235
236 cv_wait(&me->c_done_cv, &ct->ct_lock);
237 }
238 /* relink the class list */
239 *pp = me->c_next;
240
241 /* clean up myself and return the free callb to the head of freelist */
242 me->c_flag = CALLB_FREE;
243 me->c_next = ct->ct_freelist;
244 ct->ct_freelist = me;
245
246 mutex_exit(&ct->ct_lock);
247 return (0);
248 }
249
250 /*
251 * class: indicates to execute all callbs in the same class;
252 * code: optional argument for the callb functions.
253 * return: = 0: success
254 * != 0: ptr to string supplied when callback was registered
255 */
256 void *
callb_execute_class(int class,int code)257 callb_execute_class(int class, int code)
258 {
259 callb_t *cp;
260 void *ret = NULL;
261
262 ASSERT3S(class, <, NCBCLASS);
263
264 mutex_enter(&ct->ct_lock);
265
266 for (cp = ct->ct_first_cb[class];
267 cp != NULL && ret == NULL; cp = cp->c_next) {
268 while (cp->c_flag & CALLB_EXECUTING)
269 cv_wait(&cp->c_done_cv, &ct->ct_lock);
270 /*
271 * cont if the callb is deleted while we're sleeping
272 */
273 if (cp->c_flag == CALLB_FREE)
274 continue;
275 cp->c_flag |= CALLB_EXECUTING;
276
277 #ifdef CALLB_DEBUG
278 printf("callb_execute: name=%s func=%p arg=%p\n",
279 cp->c_name, (void *)cp->c_func, (void *)cp->c_arg);
280 #endif /* CALLB_DEBUG */
281
282 mutex_exit(&ct->ct_lock);
283 /* If callback function fails, pass back client's name */
284 if (!(*cp->c_func)(cp->c_arg, code))
285 ret = cp->c_name;
286 mutex_enter(&ct->ct_lock);
287
288 cp->c_flag &= ~CALLB_EXECUTING;
289 cv_broadcast(&cp->c_done_cv);
290 }
291 mutex_exit(&ct->ct_lock);
292 return (ret);
293 }
294
295 /*
296 * callers make sure no recursive entries to this func.
297 * dp->cc_lockp is registered by callb_add to protect callb_cpr_t structure.
298 *
299 * When calling to stop a kernel thread (code == CB_CODE_CPR_CHKPT) we
300 * use a cv_timedwait() in case the kernel thread is blocked.
301 *
302 * Note that this is a generic callback handler for daemon CPR and
303 * should NOT be changed to accommodate any specific requirement in a daemon.
304 * Individual daemons that require changes to the handler shall write
305 * callback routines in their own daemon modules.
306 */
307 boolean_t
callb_generic_cpr(void * arg,int code)308 callb_generic_cpr(void *arg, int code)
309 {
310 callb_cpr_t *cp = (callb_cpr_t *)arg;
311 clock_t ret = 0; /* assume success */
312
313 mutex_enter(cp->cc_lockp);
314
315 switch (code) {
316 case CB_CODE_CPR_CHKPT:
317 cp->cc_events |= CALLB_CPR_START;
318 #ifdef CPR_NOT_THREAD_SAFE
319 while (!(cp->cc_events & CALLB_CPR_SAFE))
320 /* cv_timedwait() returns -1 if it times out. */
321 if ((ret = cv_reltimedwait(&cp->cc_callb_cv,
322 cp->cc_lockp, (callb_timeout_sec * hz),
323 TR_CLOCK_TICK)) == -1)
324 break;
325 #endif
326 break;
327
328 case CB_CODE_CPR_RESUME:
329 cp->cc_events &= ~CALLB_CPR_START;
330 cv_signal(&cp->cc_stop_cv);
331 break;
332 }
333 mutex_exit(cp->cc_lockp);
334 return (ret != -1);
335 }
336
337 /*
338 * The generic callback function associated with kernel threads which
339 * are always considered safe.
340 */
341 boolean_t
callb_generic_cpr_safe(void * arg,int code)342 callb_generic_cpr_safe(void *arg, int code)
343 {
344 (void) arg, (void) code;
345 return (B_TRUE);
346 }
347 /*
348 * Prevent additions to callback table.
349 */
350 void
callb_lock_table(void)351 callb_lock_table(void)
352 {
353 mutex_enter(&ct->ct_lock);
354 ASSERT(!ct->ct_busy);
355 ct->ct_busy = B_TRUE;
356 mutex_exit(&ct->ct_lock);
357 }
358
359 /*
360 * Allow additions to callback table.
361 */
362 void
callb_unlock_table(void)363 callb_unlock_table(void)
364 {
365 mutex_enter(&ct->ct_lock);
366 ASSERT(ct->ct_busy);
367 ct->ct_busy = B_FALSE;
368 cv_broadcast(&ct->ct_busy_cv);
369 mutex_exit(&ct->ct_lock);
370 }
371
372 SYSINIT(sol_callb, SI_SUB_DRIVERS, SI_ORDER_FIRST, callb_init, NULL);
373 SYSUNINIT(sol_callb, SI_SUB_DRIVERS, SI_ORDER_FIRST, callb_fini, NULL);
374