refcount-vs-atomic.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ===================================
  2. refcount_t API compared to atomic_t
  3. ===================================
  4. .. contents:: :local:
  5. Introduction
  6. ============
  7. The goal of refcount_t API is to provide a minimal API for implementing
  8. an object's reference counters. While a generic architecture-independent
  9. implementation from lib/refcount.c uses atomic operations underneath,
  10. there are a number of differences between some of the ``refcount_*()`` and
  11. ``atomic_*()`` functions with regards to the memory ordering guarantees.
  12. This document outlines the differences and provides respective examples
  13. in order to help maintainers validate their code against the change in
  14. these memory ordering guarantees.
  15. The terms used through this document try to follow the formal LKMM defined in
  16. tools/memory-model/Documentation/explanation.txt.
  17. memory-barriers.txt and atomic_t.txt provide more background to the
  18. memory ordering in general and for atomic operations specifically.
  19. Relevant types of memory ordering
  20. =================================
  21. .. note:: The following section only covers some of the memory
  22. ordering types that are relevant for the atomics and reference
  23. counters and used through this document. For a much broader picture
  24. please consult memory-barriers.txt document.
  25. In the absence of any memory ordering guarantees (i.e. fully unordered)
  26. atomics & refcounters only provide atomicity and
  27. program order (po) relation (on the same CPU). It guarantees that
  28. each ``atomic_*()`` and ``refcount_*()`` operation is atomic and instructions
  29. are executed in program order on a single CPU.
  30. This is implemented using :c:func:`READ_ONCE`/:c:func:`WRITE_ONCE` and
  31. compare-and-swap primitives.
  32. A strong (full) memory ordering guarantees that all prior loads and
  33. stores (all po-earlier instructions) on the same CPU are completed
  34. before any po-later instruction is executed on the same CPU.
  35. It also guarantees that all po-earlier stores on the same CPU
  36. and all propagated stores from other CPUs must propagate to all
  37. other CPUs before any po-later instruction is executed on the original
  38. CPU (A-cumulative property). This is implemented using :c:func:`smp_mb`.
  39. A RELEASE memory ordering guarantees that all prior loads and
  40. stores (all po-earlier instructions) on the same CPU are completed
  41. before the operation. It also guarantees that all po-earlier
  42. stores on the same CPU and all propagated stores from other CPUs
  43. must propagate to all other CPUs before the release operation
  44. (A-cumulative property). This is implemented using
  45. :c:func:`smp_store_release`.
  46. A control dependency (on success) for refcounters guarantees that
  47. if a reference for an object was successfully obtained (reference
  48. counter increment or addition happened, function returned true),
  49. then further stores are ordered against this operation.
  50. Control dependency on stores are not implemented using any explicit
  51. barriers, but rely on CPU not to speculate on stores. This is only
  52. a single CPU relation and provides no guarantees for other CPUs.
  53. Comparison of functions
  54. =======================
  55. case 1) - non-"Read/Modify/Write" (RMW) ops
  56. -------------------------------------------
  57. Function changes:
  58. * :c:func:`atomic_set` --> :c:func:`refcount_set`
  59. * :c:func:`atomic_read` --> :c:func:`refcount_read`
  60. Memory ordering guarantee changes:
  61. * none (both fully unordered)
  62. case 2) - increment-based ops that return no value
  63. --------------------------------------------------
  64. Function changes:
  65. * :c:func:`atomic_inc` --> :c:func:`refcount_inc`
  66. * :c:func:`atomic_add` --> :c:func:`refcount_add`
  67. Memory ordering guarantee changes:
  68. * none (both fully unordered)
  69. case 3) - decrement-based RMW ops that return no value
  70. ------------------------------------------------------
  71. Function changes:
  72. * :c:func:`atomic_dec` --> :c:func:`refcount_dec`
  73. Memory ordering guarantee changes:
  74. * fully unordered --> RELEASE ordering
  75. case 4) - increment-based RMW ops that return a value
  76. -----------------------------------------------------
  77. Function changes:
  78. * :c:func:`atomic_inc_not_zero` --> :c:func:`refcount_inc_not_zero`
  79. * no atomic counterpart --> :c:func:`refcount_add_not_zero`
  80. Memory ordering guarantees changes:
  81. * fully ordered --> control dependency on success for stores
  82. .. note:: We really assume here that necessary ordering is provided as a
  83. result of obtaining pointer to the object!
  84. case 5) - decrement-based RMW ops that return a value
  85. -----------------------------------------------------
  86. Function changes:
  87. * :c:func:`atomic_dec_and_test` --> :c:func:`refcount_dec_and_test`
  88. * :c:func:`atomic_sub_and_test` --> :c:func:`refcount_sub_and_test`
  89. * no atomic counterpart --> :c:func:`refcount_dec_if_one`
  90. * ``atomic_add_unless(&var, -1, 1)`` --> ``refcount_dec_not_one(&var)``
  91. Memory ordering guarantees changes:
  92. * fully ordered --> RELEASE ordering + control dependency
  93. .. note:: :c:func:`atomic_add_unless` only provides full order on success.
  94. case 6) - lock-based RMW
  95. ------------------------
  96. Function changes:
  97. * :c:func:`atomic_dec_and_lock` --> :c:func:`refcount_dec_and_lock`
  98. * :c:func:`atomic_dec_and_mutex_lock` --> :c:func:`refcount_dec_and_mutex_lock`
  99. Memory ordering guarantees changes:
  100. * fully ordered --> RELEASE ordering + control dependency + hold
  101. :c:func:`spin_lock` on success