cpu-notifier-error-inject.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/cpu.h>
  4. #include "notifier-error-inject.h"
  5. static int priority;
  6. module_param(priority, int, 0);
  7. MODULE_PARM_DESC(priority, "specify cpu notifier priority");
  8. #define UP_PREPARE 0
  9. #define UP_PREPARE_FROZEN 0
  10. #define DOWN_PREPARE 0
  11. #define DOWN_PREPARE_FROZEN 0
  12. static struct notifier_err_inject cpu_notifier_err_inject = {
  13. .actions = {
  14. { NOTIFIER_ERR_INJECT_ACTION(UP_PREPARE) },
  15. { NOTIFIER_ERR_INJECT_ACTION(UP_PREPARE_FROZEN) },
  16. { NOTIFIER_ERR_INJECT_ACTION(DOWN_PREPARE) },
  17. { NOTIFIER_ERR_INJECT_ACTION(DOWN_PREPARE_FROZEN) },
  18. {}
  19. }
  20. };
  21. static int notf_err_handle(struct notifier_err_inject_action *action)
  22. {
  23. int ret;
  24. ret = action->error;
  25. if (ret)
  26. pr_info("Injecting error (%d) to %s\n", ret, action->name);
  27. return ret;
  28. }
  29. static int notf_err_inj_up_prepare(unsigned int cpu)
  30. {
  31. if (!cpuhp_tasks_frozen)
  32. return notf_err_handle(&cpu_notifier_err_inject.actions[0]);
  33. else
  34. return notf_err_handle(&cpu_notifier_err_inject.actions[1]);
  35. }
  36. static int notf_err_inj_dead(unsigned int cpu)
  37. {
  38. if (!cpuhp_tasks_frozen)
  39. return notf_err_handle(&cpu_notifier_err_inject.actions[2]);
  40. else
  41. return notf_err_handle(&cpu_notifier_err_inject.actions[3]);
  42. }
  43. static struct dentry *dir;
  44. static int err_inject_init(void)
  45. {
  46. int err;
  47. dir = notifier_err_inject_init("cpu", notifier_err_inject_dir,
  48. &cpu_notifier_err_inject, priority);
  49. if (IS_ERR(dir))
  50. return PTR_ERR(dir);
  51. err = cpuhp_setup_state_nocalls(CPUHP_NOTF_ERR_INJ_PREPARE,
  52. "cpu-err-notif:prepare",
  53. notf_err_inj_up_prepare,
  54. notf_err_inj_dead);
  55. if (err)
  56. debugfs_remove_recursive(dir);
  57. return err;
  58. }
  59. static void err_inject_exit(void)
  60. {
  61. cpuhp_remove_state_nocalls(CPUHP_NOTF_ERR_INJ_PREPARE);
  62. debugfs_remove_recursive(dir);
  63. }
  64. module_init(err_inject_init);
  65. module_exit(err_inject_exit);
  66. MODULE_DESCRIPTION("CPU notifier error injection module");
  67. MODULE_LICENSE("GPL");
  68. MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");