lockdep.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ========================
  3. RCU and lockdep checking
  4. ========================
  5. All flavors of RCU have lockdep checking available, so that lockdep is
  6. aware of when each task enters and leaves any flavor of RCU read-side
  7. critical section. Each flavor of RCU is tracked separately (but note
  8. that this is not the case in 2.6.32 and earlier). This allows lockdep's
  9. tracking to include RCU state, which can sometimes help when debugging
  10. deadlocks and the like.
  11. In addition, RCU provides the following primitives that check lockdep's
  12. state::
  13. rcu_read_lock_held() for normal RCU.
  14. rcu_read_lock_bh_held() for RCU-bh.
  15. rcu_read_lock_sched_held() for RCU-sched.
  16. rcu_read_lock_any_held() for any of normal RCU, RCU-bh, and RCU-sched.
  17. srcu_read_lock_held() for SRCU.
  18. rcu_read_lock_trace_held() for RCU Tasks Trace.
  19. These functions are conservative, and will therefore return 1 if they
  20. aren't certain (for example, if CONFIG_DEBUG_LOCK_ALLOC is not set).
  21. This prevents things like WARN_ON(!rcu_read_lock_held()) from giving false
  22. positives when lockdep is disabled.
  23. In addition, a separate kernel config parameter CONFIG_PROVE_RCU enables
  24. checking of rcu_dereference() primitives:
  25. rcu_dereference(p):
  26. Check for RCU read-side critical section.
  27. rcu_dereference_bh(p):
  28. Check for RCU-bh read-side critical section.
  29. rcu_dereference_sched(p):
  30. Check for RCU-sched read-side critical section.
  31. srcu_dereference(p, sp):
  32. Check for SRCU read-side critical section.
  33. rcu_dereference_check(p, c):
  34. Use explicit check expression "c" along with
  35. rcu_read_lock_held(). This is useful in code that is
  36. invoked by both RCU readers and updaters.
  37. rcu_dereference_bh_check(p, c):
  38. Use explicit check expression "c" along with
  39. rcu_read_lock_bh_held(). This is useful in code that
  40. is invoked by both RCU-bh readers and updaters.
  41. rcu_dereference_sched_check(p, c):
  42. Use explicit check expression "c" along with
  43. rcu_read_lock_sched_held(). This is useful in code that
  44. is invoked by both RCU-sched readers and updaters.
  45. srcu_dereference_check(p, c):
  46. Use explicit check expression "c" along with
  47. srcu_read_lock_held(). This is useful in code that
  48. is invoked by both SRCU readers and updaters.
  49. rcu_dereference_raw(p):
  50. Don't check. (Use sparingly, if at all.)
  51. rcu_dereference_raw_check(p):
  52. Don't do lockdep at all. (Use sparingly, if at all.)
  53. rcu_dereference_protected(p, c):
  54. Use explicit check expression "c", and omit all barriers
  55. and compiler constraints. This is useful when the data
  56. structure cannot change, for example, in code that is
  57. invoked only by updaters.
  58. rcu_access_pointer(p):
  59. Return the value of the pointer and omit all barriers,
  60. but retain the compiler constraints that prevent duplicating
  61. or coalescing. This is useful when testing the
  62. value of the pointer itself, for example, against NULL.
  63. The rcu_dereference_check() check expression can be any boolean
  64. expression, but would normally include a lockdep expression. For a
  65. moderately ornate example, consider the following::
  66. file = rcu_dereference_check(fdt->fd[fd],
  67. lockdep_is_held(&files->file_lock) ||
  68. atomic_read(&files->count) == 1);
  69. This expression picks up the pointer "fdt->fd[fd]" in an RCU-safe manner,
  70. and, if CONFIG_PROVE_RCU is configured, verifies that this expression
  71. is used in:
  72. 1. An RCU read-side critical section (implicit), or
  73. 2. with files->file_lock held, or
  74. 3. on an unshared files_struct.
  75. In case (1), the pointer is picked up in an RCU-safe manner for vanilla
  76. RCU read-side critical sections, in case (2) the ->file_lock prevents
  77. any change from taking place, and finally, in case (3) the current task
  78. is the only task accessing the file_struct, again preventing any change
  79. from taking place. If the above statement was invoked only from updater
  80. code, it could instead be written as follows::
  81. file = rcu_dereference_protected(fdt->fd[fd],
  82. lockdep_is_held(&files->file_lock) ||
  83. atomic_read(&files->count) == 1);
  84. This would verify cases #2 and #3 above, and furthermore lockdep would
  85. complain even if this was used in an RCU read-side critical section unless
  86. one of these two cases held. Because rcu_dereference_protected() omits
  87. all barriers and compiler constraints, it generates better code than do
  88. the other flavors of rcu_dereference(). On the other hand, it is illegal
  89. to use rcu_dereference_protected() if either the RCU-protected pointer
  90. or the RCU-protected data that it points to can change concurrently.
  91. Like rcu_dereference(), when lockdep is enabled, RCU list and hlist
  92. traversal primitives check for being called from within an RCU read-side
  93. critical section. However, a lockdep expression can be passed to them
  94. as a additional optional argument. With this lockdep expression, these
  95. traversal primitives will complain only if the lockdep expression is
  96. false and they are called from outside any RCU read-side critical section.
  97. For example, the workqueue for_each_pwq() macro is intended to be used
  98. either within an RCU read-side critical section or with wq->mutex held.
  99. It is thus implemented as follows::
  100. #define for_each_pwq(pwq, wq)
  101. list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node,
  102. lock_is_held(&(wq->mutex).dep_map))