From 8b5536bbee53620f8d5f367987e5727ba36d886d Mon Sep 17 00:00:00 2001 From: Eric Dumazet Date: Mon, 30 Jan 2006 06:19:35 +0100 Subject: [PATCH] [PATCH] kref: avoid an atomic operation in kref_put() 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 Signed-off-by: Greg Kroah-Hartman --- lib/kref.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/kref.c b/lib/kref.c index 0d07cc31c81..4a467faf136 100644 --- a/lib/kref.c +++ b/lib/kref.c @@ -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; } -- 2.20.1