NMI-RCU.txt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. Using RCU to Protect Dynamic NMI Handlers
  2. Although RCU is usually used to protect read-mostly data structures,
  3. it is possible to use RCU to provide dynamic non-maskable interrupt
  4. handlers, as well as dynamic irq handlers. This document describes
  5. how to do this, drawing loosely from Zwane Mwaikambo's NMI-timer
  6. work in "arch/x86/oprofile/nmi_timer_int.c" and in
  7. "arch/x86/kernel/traps.c".
  8. The relevant pieces of code are listed below, each followed by a
  9. brief explanation.
  10. static int dummy_nmi_callback(struct pt_regs *regs, int cpu)
  11. {
  12. return 0;
  13. }
  14. The dummy_nmi_callback() function is a "dummy" NMI handler that does
  15. nothing, but returns zero, thus saying that it did nothing, allowing
  16. the NMI handler to take the default machine-specific action.
  17. static nmi_callback_t nmi_callback = dummy_nmi_callback;
  18. This nmi_callback variable is a global function pointer to the current
  19. NMI handler.
  20. void do_nmi(struct pt_regs * regs, long error_code)
  21. {
  22. int cpu;
  23. nmi_enter();
  24. cpu = smp_processor_id();
  25. ++nmi_count(cpu);
  26. if (!rcu_dereference_sched(nmi_callback)(regs, cpu))
  27. default_do_nmi(regs);
  28. nmi_exit();
  29. }
  30. The do_nmi() function processes each NMI. It first disables preemption
  31. in the same way that a hardware irq would, then increments the per-CPU
  32. count of NMIs. It then invokes the NMI handler stored in the nmi_callback
  33. function pointer. If this handler returns zero, do_nmi() invokes the
  34. default_do_nmi() function to handle a machine-specific NMI. Finally,
  35. preemption is restored.
  36. In theory, rcu_dereference_sched() is not needed, since this code runs
  37. only on i386, which in theory does not need rcu_dereference_sched()
  38. anyway. However, in practice it is a good documentation aid, particularly
  39. for anyone attempting to do something similar on Alpha or on systems
  40. with aggressive optimizing compilers.
  41. Quick Quiz: Why might the rcu_dereference_sched() be necessary on Alpha,
  42. given that the code referenced by the pointer is read-only?
  43. Back to the discussion of NMI and RCU...
  44. void set_nmi_callback(nmi_callback_t callback)
  45. {
  46. rcu_assign_pointer(nmi_callback, callback);
  47. }
  48. The set_nmi_callback() function registers an NMI handler. Note that any
  49. data that is to be used by the callback must be initialized up -before-
  50. the call to set_nmi_callback(). On architectures that do not order
  51. writes, the rcu_assign_pointer() ensures that the NMI handler sees the
  52. initialized values.
  53. void unset_nmi_callback(void)
  54. {
  55. rcu_assign_pointer(nmi_callback, dummy_nmi_callback);
  56. }
  57. This function unregisters an NMI handler, restoring the original
  58. dummy_nmi_handler(). However, there may well be an NMI handler
  59. currently executing on some other CPU. We therefore cannot free
  60. up any data structures used by the old NMI handler until execution
  61. of it completes on all other CPUs.
  62. One way to accomplish this is via synchronize_sched(), perhaps as
  63. follows:
  64. unset_nmi_callback();
  65. synchronize_sched();
  66. kfree(my_nmi_data);
  67. This works because synchronize_sched() blocks until all CPUs complete
  68. any preemption-disabled segments of code that they were executing.
  69. Since NMI handlers disable preemption, synchronize_sched() is guaranteed
  70. not to return until all ongoing NMI handlers exit. It is therefore safe
  71. to free up the handler's data as soon as synchronize_sched() returns.
  72. Important note: for this to work, the architecture in question must
  73. invoke nmi_enter() and nmi_exit() on NMI entry and exit, respectively.
  74. Answer to Quick Quiz
  75. Why might the rcu_dereference_sched() be necessary on Alpha, given
  76. that the code referenced by the pointer is read-only?
  77. Answer: The caller to set_nmi_callback() might well have
  78. initialized some data that is to be used by the new NMI
  79. handler. In this case, the rcu_dereference_sched() would
  80. be needed, because otherwise a CPU that received an NMI
  81. just after the new handler was set might see the pointer
  82. to the new NMI handler, but the old pre-initialized
  83. version of the handler's data.
  84. This same sad story can happen on other CPUs when using
  85. a compiler with aggressive pointer-value speculation
  86. optimizations.
  87. More important, the rcu_dereference_sched() makes it
  88. clear to someone reading the code that the pointer is
  89. being protected by RCU-sched.