Skip to content

Commit b76d324

Browse files
Matthew Wilcox (Oracle)Jonathan Corbet
authored andcommitted
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) <[email protected]> Tested-by: Randy Dunlap <[email protected]> Acked-by: Randy Dunlap <[email protected]> Signed-off-by: Jonathan Corbet <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 9fb89b9 commit b76d324

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

Documentation/core-api/kref.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Adding reference counters (krefs) to kernel objects
33
===================================================
44

55
:Author: Corey Minyard <[email protected]>
6-
:Author: Thomas Hellstrom <thellstrom@vmware.com>
6+
:Author: Thomas Hellström <[email protected].com>
77

88
A lot of this was lifted from Greg Kroah-Hartman's 2004 OLS paper and
99
presentation on krefs, which can be found at:
@@ -321,3 +321,8 @@ rcu grace period after release_entry_rcu was called. That can be accomplished
321321
by using kfree_rcu(entry, rhead) as done above, or by calling synchronize_rcu()
322322
before using kfree, but note that synchronize_rcu() may sleep for a
323323
substantial amount of time.
324+
325+
Functions and structures
326+
========================
327+
328+
.. kernel-doc:: include/linux/kref.h

include/linux/kref.h

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@ static inline void kref_get(struct kref *kref)
4646
}
4747

4848
/**
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
5252
* 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.
5553
*
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.
6161
*/
6262
static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref))
6363
{
@@ -68,17 +68,37 @@ static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref)
6868
return 0;
6969
}
7070

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+
*/
7181
static inline int kref_put_mutex(struct kref *kref,
7282
void (*release)(struct kref *kref),
73-
struct mutex *lock)
83+
struct mutex *mutex)
7484
{
75-
if (refcount_dec_and_mutex_lock(&kref->refcount, lock)) {
85+
if (refcount_dec_and_mutex_lock(&kref->refcount, mutex)) {
7686
release(kref);
7787
return 1;
7888
}
7989
return 0;
8090
}
8191

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+
*/
82102
static inline int kref_put_lock(struct kref *kref,
83103
void (*release)(struct kref *kref),
84104
spinlock_t *lock)
@@ -94,8 +114,6 @@ static inline int kref_put_lock(struct kref *kref,
94114
* kref_get_unless_zero - Increment refcount for object unless it is zero.
95115
* @kref: object.
96116
*
97-
* Return non-zero if the increment succeeded. Otherwise return 0.
98-
*
99117
* This function is intended to simplify locking around refcounting for
100118
* objects that can be looked up from a lookup structure, and which are
101119
* removed from that lookup structure in the object destructor.
@@ -105,6 +123,8 @@ static inline int kref_put_lock(struct kref *kref,
105123
* With a lookup followed by a kref_get_unless_zero *with return value check*
106124
* locking in the kref_put path can be deferred to the actual removal from
107125
* the lookup structure and RCU lookups become trivial.
126+
*
127+
* Return: non-zero if the increment succeeded. Otherwise return 0.
108128
*/
109129
static inline int __must_check kref_get_unless_zero(struct kref *kref)
110130
{

0 commit comments

Comments
 (0)