NMI-RCU.rst 4.2 KB

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