lockdep.txt 4.4 KB

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