1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
#!/bin/sh
#
# Copyright (c) 2016 EMC Corp.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# uma_zalloc_arc() fail point test scenario.
# Test scenario by Ryan Libby <rlibby@gmail.com>.
# "panic: backing_object 0xfffff8016dd74420 was somehow re-referenced during
# collapse!" seen.
# https://people.freebsd.org/~pho/stress/log/uma_zalloc_arg.txt
# Hang seen:
# https://people.freebsd.org/~pho/stress/log/kostik869.txt
[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
. ../default.cfg
sysctl debug.mnowait_failure.zalloc_whitelist > /dev/null 2>&1 || exit 0
s1=`sysctl -n debug.mnowait_failure.zalloc_whitelist`
s2=`sysctl -n debug.fail_point.uma_zalloc_arg`
cleanup() {
sysctl debug.mnowait_failure.zalloc_whitelist="$s1" > /dev/null
sysctl debug.fail_point.uma_zalloc_arg="$s2" > /dev/null
}
trap "cleanup" EXIT INT
sysctl debug.mnowait_failure.zalloc_whitelist='RADIX NODE'
sysctl debug.fail_point.uma_zalloc_arg='1%return'
start=`date '+%s'`
while [ $((`date '+%s'` - start)) -lt 300 ]; do
sh -c "echo | cat | cat > /dev/null"
done
exit 0
The patch from
https://github.com/freebsd/freebsd/compare/master...rlibby:mnowait-dbg
diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c
index 01aff78..9d557a1 100644
--- a/sys/kern/kern_malloc.c
+++ b/sys/kern/kern_malloc.c
@@ -50,6 +50,7 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
+#include <sys/fail.h>
#include <sys/kdb.h>
#include <sys/kernel.h>
#include <sys/lock.h>
@@ -472,6 +473,19 @@ malloc(unsigned long size, struct malloc_type *mtp, int flags)
}
}
#endif
+ if (flags & M_NOWAIT) {
+ KFAIL_POINT_CODE(DEBUG_FP, malloc, {
+ if (uma_dbg_nowait_fail_enabled_malloc(
+ mtp->ks_shortdesc)) {
+ /* XXX record call stack */
+#ifdef MALLOC_MAKE_FAILURES
+ atomic_add_int(&malloc_failure_count, 1);
+ t_malloc_fail = time_uptime;
+#endif
+ return (NULL);
+ }
+ });
+ }
if (flags & M_WAITOK)
KASSERT(curthread->td_intr_nesting_level == 0,
("malloc(M_WAITOK) in interrupt context"));
diff --git a/sys/vm/uma_core.c b/sys/vm/uma_core.c
index 1f57dff..dfa18e6 100644
--- a/sys/vm/uma_core.c
+++ b/sys/vm/uma_core.c
@@ -64,6 +64,7 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bitset.h>
+#include <sys/fail.h>
#include <sys/kernel.h>
#include <sys/types.h>
#include <sys/queue.h>
@@ -2148,6 +2149,23 @@ uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
if (flags & M_WAITOK) {
WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
"uma_zalloc_arg: zone \"%s\"", zone->uz_name);
+ } else {
+ KFAIL_POINT_CODE(DEBUG_FP, uma_zalloc_arg, {
+ /*
+ * XXX hack. Setting the fail point to 0 (default)
+ * causes it to ignore malloc zones, nonzero causes it
+ * to inject failures for malloc zones regardless of
+ * the malloc black/white lists.
+ */
+ if (((zone->uz_flags & UMA_ZONE_MALLOC) == 0 ||
+ RETURN_VALUE != 0) &&
+ uma_dbg_nowait_fail_enabled_zalloc(
+ zone->uz_name)) {
+ /* XXX record call stack */
+ atomic_add_long(&zone->uz_fails, 1);
+ return NULL;
+ }
+ });
}
KASSERT(curthread->td_critnest == 0,
diff --git a/sys/vm/uma_dbg.c b/sys/vm/uma_dbg.c
index 3fbd29b..bed3130 100644
--- a/sys/vm/uma_dbg.c
+++ b/sys/vm/uma_dbg.c
@@ -42,6 +42,8 @@ __FBSDID("$FreeBSD$");
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/malloc.h>
+#include <sys/rwlock.h>
+#include <sys/sysctl.h>
#include <vm/vm.h>
#include <vm/vm_object.h>
@@ -292,4 +294,143 @@ uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
BIT_CLR_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
}
+/* XXX explain */
+struct rwlock g_uma_dbg_nowait_lock;
+RW_SYSINIT(uma_dbg_nowait, &g_uma_dbg_nowait_lock, "uma dbg nowait");
+
+#define NOWAIT_FAIL_LIST_BUFSIZE 4096
+char malloc_fail_blacklist[NOWAIT_FAIL_LIST_BUFSIZE] = "kobj";
+char malloc_fail_whitelist[NOWAIT_FAIL_LIST_BUFSIZE] = "";
+char zalloc_fail_blacklist[NOWAIT_FAIL_LIST_BUFSIZE] =
+ "BUF TRIE,ata_request,sackhole";
+char zalloc_fail_whitelist[NOWAIT_FAIL_LIST_BUFSIZE] = "";
+
+static bool
+str_in_list(const char *list, char delim, const char *str)
+{
+ const char *b, *e;
+ size_t blen, slen;
+
+ b = list;
+ slen = strlen(str);
+ for (;;) {
+ e = strchr(b, delim);
+ blen = e == NULL ? strlen(b) : e - b;
+ if (blen == slen && strncmp(b, str, slen) == 0)
+ return true;
+ if (e == NULL)
+ break;
+ b = e + 1;
+ }
+ return false;
+}
+
+static bool
+uma_dbg_nowait_fail_enabled_internal(const char *blacklist,
+ const char *whitelist, const char *name)
+{
+ bool fail;
+
+ /* Protect ourselves from the sysctl handlers. */
+ rw_rlock(&g_uma_dbg_nowait_lock);
+ if (whitelist[0] == '\0')
+ fail = !str_in_list(blacklist, ',', name);
+ else
+ fail = str_in_list(whitelist, ',', name);
+ rw_runlock(&g_uma_dbg_nowait_lock);
+
+ return fail;
+}
+
+bool
+uma_dbg_nowait_fail_enabled_malloc(const char *name)
+{
+ return uma_dbg_nowait_fail_enabled_internal(malloc_fail_blacklist,
+ malloc_fail_whitelist, name);
+}
+
+bool
+uma_dbg_nowait_fail_enabled_zalloc(const char *name)
+{
+ return uma_dbg_nowait_fail_enabled_internal(zalloc_fail_blacklist,
+ zalloc_fail_whitelist, name);
+}
+
+/*
+ * XXX provide SYSCTL_STRING_LOCKED / sysctl_string_locked_handler?
+ * This is basically just a different sysctl_string_handler. This one wraps
+ * the string manipulation in a lock and in a way that will not cause a sleep
+ * under that lock.
+ */
+static int
+sysctl_debug_mnowait_failure_list(SYSCTL_HANDLER_ARGS)
+{
+ char *newbuf = NULL;
+ int error, newlen;
+ bool have_lock = false;
+
+ if (req->newptr != NULL) {
+ newlen = req->newlen - req->newidx;
+ if (newlen >= arg2) {
+ error = EINVAL;
+ goto out;
+ }
+ newbuf = malloc(newlen, M_TEMP, M_WAITOK);
+ error = SYSCTL_IN(req, newbuf, newlen);
+ if (error != 0)
+ goto out;
+ }
+
+ error = sysctl_wire_old_buffer(req, arg2);
+ if (error != 0)
+ goto out;
+
+ rw_wlock(&g_uma_dbg_nowait_lock);
+ have_lock = true;
+
+ error = SYSCTL_OUT(req, arg1, strnlen(arg1, arg2 - 1) + 1);
+ if (error != 0)
+ goto out;
+
+ if (newbuf == NULL)
+ goto out;
+
+ bcopy(newbuf, arg1, newlen);
+ ((char *)arg1)[newlen] = '\0';
+ out:
+ if (have_lock)
+ rw_wunlock(&g_uma_dbg_nowait_lock);
+ free(newbuf, M_TEMP);
+ return error;
+}
+
+SYSCTL_NODE(_debug, OID_AUTO, mnowait_failure, CTLFLAG_RW, 0,
+ "Control of M_NOWAIT memory allocation failure injection.");
+
+SYSCTL_PROC(_debug_mnowait_failure, OID_AUTO, malloc_blacklist,
+ CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, malloc_fail_blacklist,
+ sizeof(malloc_fail_blacklist), sysctl_debug_mnowait_failure_list, "A",
+ "With debug.fail_point.malloc and with an empty whitelist, CSV list of "
+ "zones which remain unaffected.");
+
+SYSCTL_PROC(_debug_mnowait_failure, OID_AUTO, malloc_whitelist,
+ CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, malloc_fail_whitelist,
+ sizeof(malloc_fail_whitelist), sysctl_debug_mnowait_failure_list, "A",
+ "With debug.fail_point.malloc, CSV list of zones exclusively affected. "
+ "With an empty whitelist, all zones but those on the blacklist"
+ "are affected.");
+
+SYSCTL_PROC(_debug_mnowait_failure, OID_AUTO, zalloc_blacklist,
+ CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, zalloc_fail_blacklist,
+ sizeof(zalloc_fail_blacklist), sysctl_debug_mnowait_failure_list, "A",
+ "With debug.fail_point.uma_zalloc_arg and with an empty whitelist, CSV "
+ "list of zones which remain unaffected.");
+
+SYSCTL_PROC(_debug_mnowait_failure, OID_AUTO, zalloc_whitelist,
+ CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, zalloc_fail_whitelist,
+ sizeof(zalloc_fail_whitelist), sysctl_debug_mnowait_failure_list, "A",
+ "With debug.fail_point.uma_zalloc_arg, CSV list of zones exclusively "
+ "affected. With an empty whitelist, all zones but those on the blacklist"
+ "are affected.");
+
#endif /* INVARIANTS */
diff --git a/sys/vm/uma_int.h b/sys/vm/uma_int.h
index ad2a405..284747f 100644
--- a/sys/vm/uma_int.h
+++ b/sys/vm/uma_int.h
@@ -427,6 +427,9 @@ vsetslab(vm_offset_t va, uma_slab_t slab)
void *uma_small_alloc(uma_zone_t zone, vm_size_t bytes, uint8_t *pflag,
int wait);
void uma_small_free(void *mem, vm_size_t size, uint8_t flags);
+
+bool uma_dbg_nowait_fail_enabled_malloc(const char *name);
+bool uma_dbg_nowait_fail_enabled_zalloc(const char *name);
#endif /* _KERNEL */
#endif /* VM_UMA_INT_H */
|