diff options
-rw-r--r-- | sys/conf/options | 1 | ||||
-rw-r--r-- | sys/kern/kern_malloc.c | 26 |
2 files changed, 27 insertions, 0 deletions
diff --git a/sys/conf/options b/sys/conf/options index 82f2fce..2c496b5 100644 --- a/sys/conf/options +++ b/sys/conf/options @@ -473,6 +473,7 @@ VM_KMEM_SIZE opt_vm.h VM_KMEM_SIZE_SCALE opt_vm.h VM_KMEM_SIZE_MAX opt_vm.h NO_SWAPPING opt_vm.h +MALLOC_MAKE_FAILURES opt_vm.h MALLOC_PROFILE opt_vm.h PQ_NOOPT opt_vmpage.h PQ_NORMALCACHE opt_vmpage.h diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c index 34f791a..048c4b34 100644 --- a/sys/kern/kern_malloc.c +++ b/sys/kern/kern_malloc.c @@ -138,6 +138,23 @@ static int sysctl_kern_malloc(SYSCTL_HANDLER_ARGS); /* time_uptime of last malloc(9) failure */ static time_t t_malloc_fail; +#ifdef MALLOC_MAKE_FAILURES +/* + * Causes malloc failures every (n) mallocs with M_NOWAIT. If set to 0, + * doesn't cause failures. + */ +SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0, + "Kernel malloc debugging options"); + +static int malloc_failure_rate; +static int malloc_nowait_count; +static int malloc_failure_count; +SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RW, + &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail"); +SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD, + &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures"); +#endif + int malloc_last_fail(void) { @@ -188,6 +205,15 @@ malloc(size, type, flags) if (size == 0) Debugger("zero size malloc"); #endif +#ifdef MALLOC_MAKE_FAILURES + if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) { + atomic_add_int(&malloc_nowait_count, 1); + if ((malloc_nowait_count % malloc_failure_rate) == 0) { + atomic_add_int(&malloc_failure_count, 1); + return (NULL); + } + } +#endif if (flags & M_WAITOK) KASSERT(curthread->td_intr_nesting_level == 0, ("malloc(M_WAITOK) in interrupt context")); |