xref: /freebsd/sys/sys/fail.h (revision b0b1dbdd)
1 /*-
2  * Copyright (c) 2009 Isilon Inc http://www.isilon.com/
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 /**
28  * @file
29  *
30  * Main header for failpoint facility.
31  */
32 #ifndef _SYS_FAIL_H_
33 #define _SYS_FAIL_H_
34 
35 #include <sys/param.h>
36 #include <sys/cdefs.h>
37 #include <sys/linker_set.h>
38 #include <sys/queue.h>
39 #include <sys/sysctl.h>
40 #include <sys/condvar.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/systm.h>
45 
46 /**
47  * Failpoint return codes, used internally.
48  * @ingroup failpoint_private
49  */
50 enum fail_point_return_code {
51 	FAIL_POINT_RC_CONTINUE = 0,	/**< Continue with normal execution */
52 	FAIL_POINT_RC_RETURN,		/**< FP evaluated to 'return' */
53 	FAIL_POINT_RC_QUEUED,		/**< sleep_fn will be called */
54 };
55 
56 struct fail_point_entry;
57 struct fail_point_setting;
58 
59 /**
60  * Internal failpoint structure, tracking all the current details of the
61  * failpoint.  This structure is the core component shared between the
62  * failure-injection code and the user-interface.
63  * @ingroup failpoint_private
64  */
65 struct fail_point {
66 	const char *fp_name;		/* name of fail point */
67 	const char *fp_location;	/* file:line of fail point */
68 	volatile int fp_ref_cnt;	/**
69 	                             * protects fp_setting: while holding
70 	                             * a ref, fp_setting points to an
71 	                             * unfreed fail_point_setting
72 	                             */
73 	struct fail_point_setting * volatile fp_setting;
74 	int fp_flags;
75 
76 	/**< Function to call before sleep or pause */
77 	void (*fp_pre_sleep_fn)(void *);
78 	/**< Arg for fp_pre_sleep_fn */
79 	void *fp_pre_sleep_arg;
80 
81 	/**< Function to call after waking from sleep or pause */
82 	void (*fp_post_sleep_fn)(void *);
83 	/**< Arg for fp_post_sleep_fn */
84 	void *fp_post_sleep_arg;
85 };
86 
87 #define	FAIL_POINT_DYNAMIC_NAME	0x01	/**< Must free name on destroy */
88 /**< Use timeout path for sleep instead of msleep */
89 #define FAIL_POINT_USE_TIMEOUT_PATH 0x02
90 /**< If fail point is set to sleep, replace the sleep call with delay */
91 #define FAIL_POINT_NONSLEEPABLE 0x04
92 
93 #define FAIL_POINT_CV_DESC "fp cv no iterators"
94 #define FAIL_POINT_IS_OFF(fp) (__predict_true((fp)->fp_setting == NULL) || \
95         __predict_true(fail_point_is_off(fp)))
96 
97 __BEGIN_DECLS
98 
99 /* Private failpoint eval function -- use fail_point_eval() instead. */
100 enum fail_point_return_code fail_point_eval_nontrivial(struct fail_point *,
101         int *ret);
102 
103 /**
104  * @addtogroup failpoint
105  * @{
106  */
107 /*
108  * Initialize a fail-point.  The name is formed in printf-like fashion
109  * from "fmt" and the subsequent arguments.
110  * Pair with fail_point_destroy().
111  */
112 void fail_point_init(struct fail_point *, const char *fmt, ...)
113     __printflike(2, 3);
114 
115 /* Return true iff this fail point is set to off, false otherwise */
116 bool fail_point_is_off(struct fail_point *fp);
117 
118 /**
119  * Set the pre-sleep function for a fail point
120  * If fp_post_sleep_fn is specified, then FAIL_POINT_SLEEP will result in a
121  * (*fp->fp_pre_sleep_fn)(fp->fp_pre_sleep_arg) call by the thread.
122  */
123 static inline void
124 fail_point_sleep_set_pre_func(struct fail_point *fp, void (*sleep_fn)(void *))
125 {
126 	fp->fp_pre_sleep_fn = sleep_fn;
127 }
128 
129 static inline void
130 fail_point_sleep_set_pre_arg(struct fail_point *fp, void *sleep_arg)
131 {
132 	fp->fp_pre_sleep_arg = sleep_arg;
133 }
134 
135 /**
136  * Set the post-sleep function.  This will be passed to timeout if we take
137  * the timeout path. This must be set if you sleep using the timeout path.
138  */
139 static inline void
140 fail_point_sleep_set_post_func(struct fail_point *fp, void (*sleep_fn)(void *))
141 {
142 	fp->fp_post_sleep_fn = sleep_fn;
143 }
144 
145 static inline void
146 fail_point_sleep_set_post_arg(struct fail_point *fp, void *sleep_arg)
147 {
148 	fp->fp_post_sleep_arg = sleep_arg;
149 }
150 /**
151  * If the FAIL_POINT_USE_TIMEOUT flag is set on a failpoint, then
152  * FAIL_POINT_SLEEP will result in a call to timeout instead of
153  * msleep. Note that if you sleep while this flag is set, you must
154  * set fp_post_sleep_fn or an error will occur upon waking.
155  */
156 static inline void
157 fail_point_use_timeout_path(struct fail_point *fp, bool use_timeout,
158         void (*post_sleep_fn)(void *))
159 {
160 	KASSERT(!use_timeout || post_sleep_fn != NULL ||
161 	        (post_sleep_fn == NULL && fp->fp_post_sleep_fn != NULL),
162 	        ("Setting fp to use timeout, but not setting post_sleep_fn\n"));
163 
164 	if (use_timeout)
165 		fp->fp_flags |= FAIL_POINT_USE_TIMEOUT_PATH;
166 	else
167 		fp->fp_flags &= ~FAIL_POINT_USE_TIMEOUT_PATH;
168 
169 	if (post_sleep_fn != NULL)
170 		fp->fp_post_sleep_fn = post_sleep_fn;
171 }
172 
173 /**
174  * Free the resources used by a fail-point.  Pair with fail_point_init().
175  */
176 void fail_point_destroy(struct fail_point *);
177 
178 /**
179  * Evaluate a failpoint.
180  */
181 static inline enum fail_point_return_code
182 fail_point_eval(struct fail_point *fp, int *ret)
183 {
184 	if (__predict_true(fp->fp_setting == NULL))
185 		return (FAIL_POINT_RC_CONTINUE);
186 	return (fail_point_eval_nontrivial(fp, ret));
187 }
188 
189 __END_DECLS
190 
191 /* Declare a fail_point and its sysctl in a function. */
192 #define _FAIL_POINT_NAME(name) _fail_point_##name
193 #define _FAIL_POINT_LOCATION() "(" __FILE__ ":" __XSTRING(__LINE__) ")"
194 #define _FAIL_POINT_INIT(parent, name, flags) \
195 	static struct fail_point _FAIL_POINT_NAME(name) = { \
196 	        .fp_name = #name, \
197 	        .fp_location = _FAIL_POINT_LOCATION(), \
198 	        .fp_ref_cnt = 0, \
199 	        .fp_setting = NULL, \
200 	        .fp_flags = (flags), \
201 	        .fp_pre_sleep_fn = NULL, \
202 	        .fp_pre_sleep_arg = NULL, \
203 	        .fp_post_sleep_fn = NULL, \
204 	        .fp_post_sleep_arg = NULL, \
205 	}; \
206 	SYSCTL_OID(parent, OID_AUTO, name, \
207 	        CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, \
208 	        &_FAIL_POINT_NAME(name), 0, fail_point_sysctl, \
209 	        "A", ""); \
210 	SYSCTL_OID(parent, OID_AUTO, status_##name, \
211 	        CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, \
212 	        &_FAIL_POINT_NAME(name), 0, \
213 	        fail_point_sysctl_status, "A", "");
214 #define _FAIL_POINT_EVAL(name, cond, code...) \
215 	int RETURN_VALUE; \
216  \
217 	if (__predict_false(cond && \
218 	        fail_point_eval(&_FAIL_POINT_NAME(name), &RETURN_VALUE))) { \
219  \
220 		code; \
221  \
222 	}
223 
224 
225 /**
226  * Instantiate a failpoint which returns "RETURN_VALUE" from the function
227  * when triggered.
228  * @param parent  The parent sysctl under which to locate the fp's sysctl
229  * @param name    The name of the failpoint in the sysctl tree (and printouts)
230  * @return        Instantly returns the RETURN_VALUE specified in the
231  *                failpoint, if triggered.
232  */
233 #define KFAIL_POINT_RETURN(parent, name) \
234 	KFAIL_POINT_CODE(parent, name, return RETURN_VALUE)
235 
236 /**
237  * Instantiate a failpoint which returns (void) from the function when
238  * triggered.
239  * @param parent  The parent sysctl under which to locate the sysctl
240  * @param name    The name of the failpoint in the sysctl tree (and printouts)
241  * @return        Instantly returns void, if triggered in the failpoint.
242  */
243 #define KFAIL_POINT_RETURN_VOID(parent, name) \
244 	KFAIL_POINT_CODE(parent, name, return)
245 
246 /**
247  * Instantiate a failpoint which sets an error when triggered.
248  * @param parent     The parent sysctl under which to locate the sysctl
249  * @param name       The name of the failpoint in the sysctl tree (and
250  *                   printouts)
251  * @param error_var  A variable to set to the failpoint's specified
252  *                   return-value when triggered
253  */
254 #define KFAIL_POINT_ERROR(parent, name, error_var) \
255 	KFAIL_POINT_CODE(parent, name, (error_var) = RETURN_VALUE)
256 
257 /**
258  * Instantiate a failpoint which sets an error and then goes to a
259  * specified label in the function when triggered.
260  * @param parent     The parent sysctl under which to locate the sysctl
261  * @param name       The name of the failpoint in the sysctl tree (and
262  *                   printouts)
263  * @param error_var  A variable to set to the failpoint's specified
264  *                   return-value when triggered
265  * @param label      The location to goto when triggered.
266  */
267 #define KFAIL_POINT_GOTO(parent, name, error_var, label) \
268 	KFAIL_POINT_CODE(parent, name, (error_var) = RETURN_VALUE; goto label)
269 
270 /**
271  * Instantiate a failpoint which sets its pre- and post-sleep callback
272  * mechanisms.
273  * @param parent     The parent sysctl under which to locate the sysctl
274  * @param name       The name of the failpoint in the sysctl tree (and
275  *                   printouts)
276  * @param pre_func   Function pointer to the pre-sleep function, which will be
277  *                   called directly before going to sleep.
278  * @param pre_arg    Argument to the pre-sleep function
279  * @param post_func  Function pointer to the pot-sleep function, which will be
280  *                   called directly before going to sleep.
281  * @param post_arg   Argument to the post-sleep function
282  */
283 #define KFAIL_POINT_SLEEP_CALLBACKS(parent, name, pre_func, pre_arg, \
284         post_func, post_arg) \
285 	KFAIL_POINT_CODE_SLEEP_CALLBACKS(parent, name, pre_func, \
286 	    pre_arg, post_func, post_arg, return RETURN_VALUE)
287 
288 /**
289  * Instantiate a failpoint which runs arbitrary code when triggered, and sets
290  * its pre- and post-sleep callback mechanisms
291  * @param parent     The parent sysctl under which to locate the sysctl
292  * @param name       The name of the failpoint in the sysctl tree (and
293  *                   printouts)
294  * @param pre_func   Function pointer to the pre-sleep function, which will be
295  *                   called directly before going to sleep.
296  * @param pre_arg    Argument to the pre-sleep function
297  * @param post_func  Function pointer to the pot-sleep function, which will be
298  *                   called directly before going to sleep.
299  * @param post_arg   Argument to the post-sleep function
300  * @param code       The arbitrary code to run when triggered.  Can reference
301  *                   "RETURN_VALUE" if desired to extract the specified
302  *                   user return-value when triggered.  Note that this is
303  *                   implemented with a do-while loop so be careful of
304  *                   break and continue statements.
305  */
306 #define KFAIL_POINT_CODE_SLEEP_CALLBACKS(parent, name, pre_func, pre_arg, \
307         post_func, post_arg, code...) \
308 	do { \
309 		_FAIL_POINT_INIT(parent, name) \
310 		_FAIL_POINT_NAME(name).fp_pre_sleep_fn = pre_func; \
311 		_FAIL_POINT_NAME(name).fp_pre_sleep_arg = pre_arg; \
312 		_FAIL_POINT_NAME(name).fp_post_sleep_fn = post_func; \
313 		_FAIL_POINT_NAME(name).fp_post_sleep_arg = post_arg; \
314 		_FAIL_POINT_EVAL(name, true, code) \
315 	} while (0)
316 
317 
318 /**
319  * Instantiate a failpoint which runs arbitrary code when triggered.
320  * @param parent     The parent sysctl under which to locate the sysctl
321  * @param name       The name of the failpoint in the sysctl tree
322  *                   (and printouts)
323  * @param code       The arbitrary code to run when triggered.  Can reference
324  *                   "RETURN_VALUE" if desired to extract the specified
325  *                   user return-value when triggered.  Note that this is
326  *                   implemented with a do-while loop so be careful of
327  *                   break and continue statements.
328  */
329 #define KFAIL_POINT_CODE(parent, name, code...) \
330 	do { \
331 		_FAIL_POINT_INIT(parent, name, 0) \
332 		_FAIL_POINT_EVAL(name, true, code) \
333 	} while (0)
334 
335 #define KFAIL_POINT_CODE_FLAGS(parent, name, flags, code...) \
336 	do { \
337 		_FAIL_POINT_INIT(parent, name, flags) \
338 		_FAIL_POINT_EVAL(name, true, code) \
339 	} while (0)
340 
341 #define KFAIL_POINT_CODE_COND(parent, name, cond, flags, code...) \
342 	do { \
343 		_FAIL_POINT_INIT(parent, name, flags) \
344 		_FAIL_POINT_EVAL(name, cond, code) \
345 	} while (0)
346 
347 /**
348  * @}
349  * (end group failpoint)
350  */
351 
352 #ifdef _KERNEL
353 int fail_point_sysctl(SYSCTL_HANDLER_ARGS);
354 int fail_point_sysctl_status(SYSCTL_HANDLER_ARGS);
355 
356 /* The fail point sysctl tree. */
357 SYSCTL_DECL(_debug_fail_point);
358 #define	DEBUG_FP	_debug_fail_point
359 #endif
360 
361 #endif /* _SYS_FAIL_H_ */
362