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

kref: Improve documentation

There is already kernel-doc written for many of the functions in kref.h
but it's not linked into the html docs anywhere. Add it to kref.rst.

Improve the kref documentation by using the standard Return: section,
rewording some unclear verbiage and adding docs for some undocumented
functions.

Update Thomas' email address to his current one.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Tested-by: Randy Dunlap <rdunlap@infradead.org>
Acked-by: Randy Dunlap <rdunlap@infradead.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/20241209160953.757673-1-willy@infradead.org

authored by

Matthew Wilcox (Oracle) and committed by
Jonathan Corbet
b76d3242 9fb89b97

+40 -15
+6 -1
Documentation/core-api/kref.rst
··· 3 3 =================================================== 4 4 5 5 :Author: Corey Minyard <minyard@acm.org> 6 - :Author: Thomas Hellstrom <thellstrom@vmware.com> 6 + :Author: Thomas Hellström <thomas.hellstrom@linux.intel.com> 7 7 8 8 A lot of this was lifted from Greg Kroah-Hartman's 2004 OLS paper and 9 9 presentation on krefs, which can be found at: ··· 321 321 by using kfree_rcu(entry, rhead) as done above, or by calling synchronize_rcu() 322 322 before using kfree, but note that synchronize_rcu() may sleep for a 323 323 substantial amount of time. 324 + 325 + Functions and structures 326 + ======================== 327 + 328 + .. kernel-doc:: include/linux/kref.h
+34 -14
include/linux/kref.h
··· 46 46 } 47 47 48 48 /** 49 - * kref_put - decrement refcount for object. 50 - * @kref: object. 51 - * @release: pointer to the function that will clean up the object when the 49 + * kref_put - Decrement refcount for object 50 + * @kref: Object 51 + * @release: Pointer to the function that will clean up the object when the 52 52 * last reference to the object is released. 53 - * This pointer is required, and it is not acceptable to pass kfree 54 - * in as this function. 55 53 * 56 - * Decrement the refcount, and if 0, call release(). 57 - * Return 1 if the object was removed, otherwise return 0. Beware, if this 58 - * function returns 0, you still can not count on the kref from remaining in 59 - * memory. Only use the return value if you want to see if the kref is now 60 - * gone, not present. 54 + * Decrement the refcount, and if 0, call @release. The caller may not 55 + * pass NULL or kfree() as the release function. 56 + * 57 + * Return: 1 if this call removed the object, otherwise return 0. Beware, 58 + * if this function returns 0, another caller may have removed the object 59 + * by the time this function returns. The return value is only certain 60 + * if you want to see if the object is definitely released. 61 61 */ 62 62 static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref)) 63 63 { ··· 68 68 return 0; 69 69 } 70 70 71 + /** 72 + * kref_put_mutex - Decrement refcount for object 73 + * @kref: Object 74 + * @release: Pointer to the function that will clean up the object when the 75 + * last reference to the object is released. 76 + * @mutex: Mutex which protects the release function. 77 + * 78 + * This variant of kref_lock() calls the @release function with the @mutex 79 + * held. The @release function will release the mutex. 80 + */ 71 81 static inline int kref_put_mutex(struct kref *kref, 72 82 void (*release)(struct kref *kref), 73 - struct mutex *lock) 83 + struct mutex *mutex) 74 84 { 75 - if (refcount_dec_and_mutex_lock(&kref->refcount, lock)) { 85 + if (refcount_dec_and_mutex_lock(&kref->refcount, mutex)) { 76 86 release(kref); 77 87 return 1; 78 88 } 79 89 return 0; 80 90 } 81 91 92 + /** 93 + * kref_put_lock - Decrement refcount for object 94 + * @kref: Object 95 + * @release: Pointer to the function that will clean up the object when the 96 + * last reference to the object is released. 97 + * @lock: Spinlock which protects the release function. 98 + * 99 + * This variant of kref_lock() calls the @release function with the @lock 100 + * held. The @release function will release the lock. 101 + */ 82 102 static inline int kref_put_lock(struct kref *kref, 83 103 void (*release)(struct kref *kref), 84 104 spinlock_t *lock) ··· 114 94 * kref_get_unless_zero - Increment refcount for object unless it is zero. 115 95 * @kref: object. 116 96 * 117 - * Return non-zero if the increment succeeded. Otherwise return 0. 118 - * 119 97 * This function is intended to simplify locking around refcounting for 120 98 * objects that can be looked up from a lookup structure, and which are 121 99 * removed from that lookup structure in the object destructor. ··· 123 105 * With a lookup followed by a kref_get_unless_zero *with return value check* 124 106 * locking in the kref_put path can be deferred to the actual removal from 125 107 * the lookup structure and RCU lookups become trivial. 108 + * 109 + * Return: non-zero if the increment succeeded. Otherwise return 0. 126 110 */ 127 111 static inline int __must_check kref_get_unless_zero(struct kref *kref) 128 112 {