NMI-RCU.txt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/i386/oprofile/nmi_timer_int.c" and in
  7. "arch/i386/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. fastcall 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(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. Strictly speaking, rcu_dereference() is not needed, since this code runs
  37. only on i386, which does not need rcu_dereference() anyway. However,
  38. it is a good documentation aid, particularly for anyone attempting to
  39. do something similar on Alpha.
  40. Quick Quiz: Why might the rcu_dereference() be necessary on Alpha,
  41. given that the code referenced by the pointer is read-only?
  42. Back to the discussion of NMI and RCU...
  43. void set_nmi_callback(nmi_callback_t callback)
  44. {
  45. rcu_assign_pointer(nmi_callback, callback);
  46. }
  47. The set_nmi_callback() function registers an NMI handler. Note that any
  48. data that is to be used by the callback must be initialized up -before-
  49. the call to set_nmi_callback(). On architectures that do not order
  50. writes, the rcu_assign_pointer() ensures that the NMI handler sees the
  51. initialized values.
  52. void unset_nmi_callback(void)
  53. {
  54. rcu_assign_pointer(nmi_callback, dummy_nmi_callback);
  55. }
  56. This function unregisters an NMI handler, restoring the original
  57. dummy_nmi_handler(). However, there may well be an NMI handler
  58. currently executing on some other CPU. We therefore cannot free
  59. up any data structures used by the old NMI handler until execution
  60. of it completes on all other CPUs.
  61. One way to accomplish this is via synchronize_sched(), perhaps as
  62. follows:
  63. unset_nmi_callback();
  64. synchronize_sched();
  65. kfree(my_nmi_data);
  66. This works because synchronize_sched() blocks until all CPUs complete
  67. any preemption-disabled segments of code that they were executing.
  68. Since NMI handlers disable preemption, synchronize_sched() is guaranteed
  69. not to return until all ongoing NMI handlers exit. It is therefore safe
  70. to free up the handler's data as soon as synchronize_sched() returns.
  71. Answer to Quick Quiz
  72. Why might the rcu_dereference() be necessary on Alpha, given
  73. that the code referenced by the pointer is read-only?
  74. Answer: The caller to set_nmi_callback() might well have
  75. initialized some data that is to be used by the
  76. new NMI handler. In this case, the rcu_dereference()
  77. would be needed, because otherwise a CPU that received
  78. an NMI just after the new handler was set might see
  79. the pointer to the new NMI handler, but the old
  80. pre-initialized version of the handler's data.
  81. More important, the rcu_dereference() makes it clear
  82. to someone reading the code that the pointer is being
  83. protected by RCU.