Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

doc: Add refcount analogy to What is RCU

The reader-writer-lock analogy is a useful way to think about RCU, but
it is not always applicable. It is useful to have other analogies to
work with, and particularly to emphasise that no single analogy is
perfect.

This patch add a "RCU as reference count" to the "what is RCU" document.

See https://lwn.net/Articles/872559/

[ paulmck: Apply Akira Yokosawa feedback. ]
Reviewed-by: Akira Yokosawa <akiyks@gmail.com>
Signed-off-by: NeilBrown <neilb@suse.de>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>

authored by

NeilBrown and committed by
Paul E. McKenney
7c0be9f8 db4cb768

+82 -8
+82 -8
Documentation/RCU/whatisRCU.rst
··· 39 39 40 40 :ref:`6. ANALOGY WITH READER-WRITER LOCKING <6_whatisRCU>` 41 41 42 - :ref:`7. FULL LIST OF RCU APIs <7_whatisRCU>` 42 + :ref:`7. ANALOGY WITH REFERENCE COUNTING <7_whatisRCU>` 43 43 44 - :ref:`8. ANSWERS TO QUICK QUIZZES <8_whatisRCU>` 44 + :ref:`8. FULL LIST OF RCU APIs <8_whatisRCU>` 45 + 46 + :ref:`9. ANSWERS TO QUICK QUIZZES <9_whatisRCU>` 45 47 46 48 People who prefer starting with a conceptual overview should focus on 47 49 Section 1, though most readers will profit by reading this section at ··· 679 677 occur when using this algorithm in a real-world Linux 680 678 kernel? How could this deadlock be avoided? 681 679 682 - :ref:`Answers to Quick Quiz <8_whatisRCU>` 680 + :ref:`Answers to Quick Quiz <9_whatisRCU>` 683 681 684 682 5B. "TOY" EXAMPLE #2: CLASSIC RCU 685 683 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ··· 734 732 Give an example where Classic RCU's read-side 735 733 overhead is **negative**. 736 734 737 - :ref:`Answers to Quick Quiz <8_whatisRCU>` 735 + :ref:`Answers to Quick Quiz <9_whatisRCU>` 738 736 739 737 .. _quiz_3: 740 738 ··· 743 741 critical section, what the heck do you do in 744 742 CONFIG_PREEMPT_RT, where normal spinlocks can block??? 745 743 746 - :ref:`Answers to Quick Quiz <8_whatisRCU>` 744 + :ref:`Answers to Quick Quiz <9_whatisRCU>` 747 745 748 746 .. _6_whatisRCU: 749 747 ··· 874 872 875 873 .. _7_whatisRCU: 876 874 877 - 7. FULL LIST OF RCU APIs 875 + 7. ANALOGY WITH REFERENCE COUNTING 876 + ----------------------------------- 877 + 878 + The reader-writer analogy (illustrated by the previous section) is not 879 + always the best way to think about using RCU. Another helpful analogy 880 + considers RCU an effective reference count on everything which is 881 + protected by RCU. 882 + 883 + A reference count typically does not prevent the referenced object's 884 + values from changing, but does prevent changes to type -- particularly the 885 + gross change of type that happens when that object's memory is freed and 886 + re-allocated for some other purpose. Once a type-safe reference to the 887 + object is obtained, some other mechanism is needed to ensure consistent 888 + access to the data in the object. This could involve taking a spinlock, 889 + but with RCU the typical approach is to perform reads with SMP-aware 890 + operations such as smp_load_acquire(), to perform updates with atomic 891 + read-modify-write operations, and to provide the necessary ordering. 892 + RCU provides a number of support functions that embed the required 893 + operations and ordering, such as the list_for_each_entry_rcu() macro 894 + used in the previous section. 895 + 896 + A more focused view of the reference counting behavior is that, 897 + between rcu_read_lock() and rcu_read_unlock(), any reference taken with 898 + rcu_dereference() on a pointer marked as ``__rcu`` can be treated as 899 + though a reference-count on that object has been temporarily increased. 900 + This prevents the object from changing type. Exactly what this means 901 + will depend on normal expectations of objects of that type, but it 902 + typically includes that spinlocks can still be safely locked, normal 903 + reference counters can be safely manipulated, and ``__rcu`` pointers 904 + can be safely dereferenced. 905 + 906 + Some operations that one might expect to see on an object for 907 + which an RCU reference is held include: 908 + 909 + - Copying out data that is guaranteed to be stable by the object's type. 910 + - Using kref_get_unless_zero() or similar to get a longer-term 911 + reference. This may fail of course. 912 + - Acquiring a spinlock in the object, and checking if the object still 913 + is the expected object and if so, manipulating it freely. 914 + 915 + The understanding that RCU provides a reference that only prevents a 916 + change of type is particularly visible with objects allocated from a 917 + slab cache marked ``SLAB_TYPESAFE_BY_RCU``. RCU operations may yield a 918 + reference to an object from such a cache that has been concurrently 919 + freed and the memory reallocated to a completely different object, 920 + though of the same type. In this case RCU doesn't even protect the 921 + identity of the object from changing, only its type. So the object 922 + found may not be the one expected, but it will be one where it is safe 923 + to take a reference or spinlock and then confirm that the identity 924 + matches the expectations. 925 + 926 + With traditional reference counting -- such as that implemented by the 927 + kref library in Linux -- there is typically code that runs when the last 928 + reference to an object is dropped. With kref, this is the function 929 + passed to kref_put(). When RCU is being used, such finalization code 930 + must not be run until all ``__rcu`` pointers referencing the object have 931 + been updated, and then a grace period has passed. Every remaining 932 + globally visible pointer to the object must be considered to be a 933 + potential counted reference, and the finalization code is typically run 934 + using call_rcu() only after all those pointers have been changed. 935 + 936 + To see how to choose between these two analogies -- of RCU as a 937 + reader-writer lock and RCU as a reference counting system -- it is useful 938 + to reflect on the scale of the thing being protected. The reader-writer 939 + lock analogy looks at larger multi-part objects such as a linked list 940 + and shows how RCU can facilitate concurrency while elements are added 941 + to, and removed from, the list. The reference-count analogy looks at 942 + the individual objects and looks at how they can be accessed safely 943 + within whatever whole they are a part of. 944 + 945 + .. _8_whatisRCU: 946 + 947 + 8. FULL LIST OF RCU APIs 878 948 ------------------------- 879 949 880 950 The RCU APIs are documented in docbook-format header comments in the ··· 1109 1035 Of course, this all assumes that you have determined that RCU is in fact 1110 1036 the right tool for your job. 1111 1037 1112 - .. _8_whatisRCU: 1038 + .. _9_whatisRCU: 1113 1039 1114 - 8. ANSWERS TO QUICK QUIZZES 1040 + 9. ANSWERS TO QUICK QUIZZES 1115 1041 ---------------------------- 1116 1042 1117 1043 Quick Quiz #1: