1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_FAULT_INJECT_H
3 #define _LINUX_FAULT_INJECT_H
4 
5 #include <linux/err.h>
6 #include <linux/types.h>
7 
8 struct dentry;
9 struct kmem_cache;
10 
11 #ifdef CONFIG_FAULT_INJECTION
12 
13 #include <linux/atomic.h>
14 #include <linux/configfs.h>
15 #include <linux/ratelimit.h>
16 
17 /*
18  * For explanation of the elements of this struct, see
19  * Documentation/fault-injection/fault-injection.rst
20  */
21 struct fault_attr {
22 	unsigned long probability;
23 	unsigned long interval;
24 	atomic_t times;
25 	atomic_t space;
26 	unsigned long verbose;
27 	bool task_filter;
28 	unsigned long stacktrace_depth;
29 	unsigned long require_start;
30 	unsigned long require_end;
31 	unsigned long reject_start;
32 	unsigned long reject_end;
33 
34 	unsigned long count;
35 	struct ratelimit_state ratelimit_state;
36 	struct dentry *dname;
37 };
38 
39 enum fault_flags {
40 	FAULT_NOWARN =	1 << 0,
41 };
42 
43 #define FAULT_ATTR_INITIALIZER {					\
44 		.interval = 1,						\
45 		.times = ATOMIC_INIT(1),				\
46 		.require_end = ULONG_MAX,				\
47 		.stacktrace_depth = 32,					\
48 		.ratelimit_state = RATELIMIT_STATE_INIT_DISABLED,	\
49 		.verbose = 2,						\
50 		.dname = NULL,						\
51 	}
52 
53 #define DECLARE_FAULT_ATTR(name) struct fault_attr name = FAULT_ATTR_INITIALIZER
54 int setup_fault_attr(struct fault_attr *attr, char *str);
55 bool should_fail_ex(struct fault_attr *attr, ssize_t size, int flags);
56 bool should_fail(struct fault_attr *attr, ssize_t size);
57 
58 #else /* CONFIG_FAULT_INJECTION */
59 
60 struct fault_attr {
61 };
62 
63 #define DECLARE_FAULT_ATTR(name) struct fault_attr name = {}
64 
setup_fault_attr(struct fault_attr * attr,char * str)65 static inline int setup_fault_attr(struct fault_attr *attr, char *str)
66 {
67 	return 0; /* Note: 0 means error for __setup() handlers! */
68 }
should_fail_ex(struct fault_attr * attr,ssize_t size,int flags)69 static inline bool should_fail_ex(struct fault_attr *attr, ssize_t size, int flags)
70 {
71 	return false;
72 }
should_fail(struct fault_attr * attr,ssize_t size)73 static inline bool should_fail(struct fault_attr *attr, ssize_t size)
74 {
75 	return false;
76 }
77 
78 #endif /* CONFIG_FAULT_INJECTION */
79 
80 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
81 
82 struct dentry *fault_create_debugfs_attr(const char *name,
83 			struct dentry *parent, struct fault_attr *attr);
84 
85 #else /* CONFIG_FAULT_INJECTION_DEBUG_FS */
86 
fault_create_debugfs_attr(const char * name,struct dentry * parent,struct fault_attr * attr)87 static inline struct dentry *fault_create_debugfs_attr(const char *name,
88 			struct dentry *parent, struct fault_attr *attr)
89 {
90 	return ERR_PTR(-ENODEV);
91 }
92 
93 #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
94 
95 #ifdef CONFIG_FAULT_INJECTION_CONFIGFS
96 
97 struct fault_config {
98 	struct fault_attr attr;
99 	struct config_group group;
100 };
101 
102 void fault_config_init(struct fault_config *config, const char *name);
103 
104 #else /* CONFIG_FAULT_INJECTION_CONFIGFS */
105 
106 struct fault_config {
107 };
108 
fault_config_init(struct fault_config * config,const char * name)109 static inline void fault_config_init(struct fault_config *config,
110 			const char *name)
111 {
112 }
113 
114 #endif /* CONFIG_FAULT_INJECTION_CONFIGFS */
115 
116 #ifdef CONFIG_FAIL_PAGE_ALLOC
117 bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order);
118 #else
should_fail_alloc_page(gfp_t gfp_mask,unsigned int order)119 static inline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
120 {
121 	return false;
122 }
123 #endif /* CONFIG_FAIL_PAGE_ALLOC */
124 
125 #ifdef CONFIG_FAILSLAB
126 int should_failslab(struct kmem_cache *s, gfp_t gfpflags);
127 #else
should_failslab(struct kmem_cache * s,gfp_t gfpflags)128 static inline int should_failslab(struct kmem_cache *s, gfp_t gfpflags)
129 {
130 	return false;
131 }
132 #endif /* CONFIG_FAILSLAB */
133 
134 #endif /* _LINUX_FAULT_INJECT_H */
135