aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJerome Marchand <jmarchan@redhat.com>2011-01-05 10:57:37 -0500
committerJens Axboe <jaxboe@fusionio.com>2011-01-05 10:57:37 -0500
commite4a683c899cd5a49f8d684a054c95bd115a0c005 (patch)
treec7e79e24199a5ed34a488b06cfd3fa890637db7d
parenta6e8dc46ff0b7defbfa4f29a71aee263377ec573 (diff)
kref: add kref_test_and_get
Add kref_test_and_get() function, which atomically add a reference only if refcount is not zero. This prevent to add a reference to an object that is already being removed. Signed-off-by: Jerome Marchand <jmarchan@redhat.com> Cc: stable@kernel.org Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
-rw-r--r--include/linux/kref.h1
-rw-r--r--lib/kref.c12
2 files changed, 13 insertions, 0 deletions
diff --git a/include/linux/kref.h b/include/linux/kref.h
index 6cc38fc07ab7..90b9e44abf54 100644
--- a/include/linux/kref.h
+++ b/include/linux/kref.h
@@ -23,6 +23,7 @@ struct kref {
23 23
24void kref_init(struct kref *kref); 24void kref_init(struct kref *kref);
25void kref_get(struct kref *kref); 25void kref_get(struct kref *kref);
26int kref_test_and_get(struct kref *kref);
26int kref_put(struct kref *kref, void (*release) (struct kref *kref)); 27int kref_put(struct kref *kref, void (*release) (struct kref *kref));
27 28
28#endif /* _KREF_H_ */ 29#endif /* _KREF_H_ */
diff --git a/lib/kref.c b/lib/kref.c
index d3d227a08a4b..e7a6e1067122 100644
--- a/lib/kref.c
+++ b/lib/kref.c
@@ -37,6 +37,18 @@ void kref_get(struct kref *kref)
37} 37}
38 38
39/** 39/**
40 * kref_test_and_get - increment refcount for object only if refcount is not
41 * zero.
42 * @kref: object.
43 *
44 * Return non-zero if the refcount was incremented, 0 otherwise
45 */
46int kref_test_and_get(struct kref *kref)
47{
48 return atomic_inc_not_zero(&kref->refcount);
49}
50
51/**
40 * kref_put - decrement refcount for object. 52 * kref_put - decrement refcount for object.
41 * @kref: object. 53 * @kref: object.
42 * @release: pointer to the function that will clean up the object when the 54 * @release: pointer to the function that will clean up the object when the