Skip to content

Commit c9d221f

Browse files
mitatorvalds
authored andcommitted
fault-injection: add CPU notifier error injection module
I used this module to test the series of modification to the cpu notifiers code. Example1: inject CPU offline error (-1 == -EPERM) # modprobe cpu-notifier-error-inject cpu_down_prepare_error=-1 # echo 0 > /sys/devices/system/cpu/cpu1/online bash: echo: write error: Operation not permitted Example2: inject CPU online error (-2 == -ENOENT) # modprobe cpu-notifier-error-inject cpu_up_prepare_error=-2 # echo 1 > /sys/devices/system/cpu/cpu1/online bash: echo: write error: No such file or directory [[email protected]: fix Kconfig help text] Signed-off-by: Akinobu Mita <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 55af6bb commit c9d221f

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

lib/Kconfig.debug

+12
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,18 @@ config LKDTM
898898
Documentation on how to use the module can be found in
899899
Documentation/fault-injection/provoke-crashes.txt
900900

901+
config CPU_NOTIFIER_ERROR_INJECT
902+
tristate "CPU notifier error injection module"
903+
depends on HOTPLUG_CPU && DEBUG_KERNEL
904+
help
905+
This option provides a kernel module that can be used to test
906+
the error handling of the cpu notifiers
907+
908+
To compile this code as a module, choose M here: the module will
909+
be called cpu-notifier-error-inject.
910+
911+
If unsure, say N.
912+
901913
config FAULT_INJECTION
902914
bool "Fault-injection framework"
903915
depends on DEBUG_KERNEL

lib/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ obj-$(CONFIG_AUDIT_GENERIC) += audit.o
8585
obj-$(CONFIG_SWIOTLB) += swiotlb.o
8686
obj-$(CONFIG_IOMMU_HELPER) += iommu-helper.o
8787
obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o
88+
obj-$(CONFIG_CPU_NOTIFIER_ERROR_INJECT) += cpu-notifier-error-inject.o
8889

8990
lib-$(CONFIG_GENERIC_BUG) += bug.o
9091

lib/cpu-notifier-error-inject.c

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <linux/kernel.h>
2+
#include <linux/cpu.h>
3+
#include <linux/module.h>
4+
#include <linux/notifier.h>
5+
6+
static int priority;
7+
static int cpu_up_prepare_error;
8+
static int cpu_down_prepare_error;
9+
10+
module_param(priority, int, 0);
11+
MODULE_PARM_DESC(priority, "specify cpu notifier priority");
12+
13+
module_param(cpu_up_prepare_error, int, 0644);
14+
MODULE_PARM_DESC(cpu_up_prepare_error,
15+
"specify error code to inject CPU_UP_PREPARE action");
16+
17+
module_param(cpu_down_prepare_error, int, 0644);
18+
MODULE_PARM_DESC(cpu_down_prepare_error,
19+
"specify error code to inject CPU_DOWN_PREPARE action");
20+
21+
static int err_inject_cpu_callback(struct notifier_block *nfb,
22+
unsigned long action, void *hcpu)
23+
{
24+
int err = 0;
25+
26+
switch (action) {
27+
case CPU_UP_PREPARE:
28+
case CPU_UP_PREPARE_FROZEN:
29+
err = cpu_up_prepare_error;
30+
break;
31+
case CPU_DOWN_PREPARE:
32+
case CPU_DOWN_PREPARE_FROZEN:
33+
err = cpu_down_prepare_error;
34+
break;
35+
}
36+
if (err)
37+
printk(KERN_INFO "Injecting error (%d) at cpu notifier\n", err);
38+
39+
return notifier_from_errno(err);
40+
}
41+
42+
static struct notifier_block err_inject_cpu_notifier = {
43+
.notifier_call = err_inject_cpu_callback,
44+
};
45+
46+
static int err_inject_init(void)
47+
{
48+
err_inject_cpu_notifier.priority = priority;
49+
50+
return register_hotcpu_notifier(&err_inject_cpu_notifier);
51+
}
52+
53+
static void err_inject_exit(void)
54+
{
55+
unregister_hotcpu_notifier(&err_inject_cpu_notifier);
56+
}
57+
58+
module_init(err_inject_init);
59+
module_exit(err_inject_exit);
60+
61+
MODULE_DESCRIPTION("CPU notifier error injection module");
62+
MODULE_LICENSE("GPL");
63+
MODULE_AUTHOR("Akinobu Mita <[email protected]>");

0 commit comments

Comments
 (0)