Skip to content

Commit

Permalink
[PATCH] kref: avoid an atomic operation in kref_put()
Browse files Browse the repository at this point in the history
Avoid an atomic operation in kref_put() when the last reference is
dropped. On most platforms, atomic_read() is a plan read of the counter
and involves no atomic at all.

Signed-off-by: Eric Dumazet <dada1@cosmosbay.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
  • Loading branch information
Eric Dumazet authored and Greg Kroah-Hartman committed Mar 20, 2006
1 parent 4894473 commit 8b5536b
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/kref.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ int kref_put(struct kref *kref, void (*release)(struct kref *kref))
WARN_ON(release == NULL);
WARN_ON(release == (void (*)(struct kref *))kfree);

if (atomic_dec_and_test(&kref->refcount)) {
/*
* if current count is one, we are the last user and can release object
* right now, avoiding an atomic operation on 'refcount'
*/
if ((atomic_read(&kref->refcount) == 1) ||
(atomic_dec_and_test(&kref->refcount))) {
release(kref);
return 1;
}
Expand Down

0 comments on commit 8b5536b

Please sign in to comment.