public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Akinobu Mita <mita@miraclelinux.com>
To: linux-kernel@vger.kernel.org
Cc: akpm@osdl.org, Greg KH <greg@kroah.com>,
	Patrick Mochel <mochel@osdl.org>,
	Akinobu Mita <mita@miraclelinux.com>
Subject: [patch 1/2] kref: detect kref_put() with unreferenced object
Date: Tue, 25 Apr 2006 16:21:38 +0800	[thread overview]
Message-ID: <20060425082359.767915000@localhost.localdomain> (raw)
In-Reply-To: 20060425082137.028875000@localhost.localdomain

[-- Attachment #1: kref-more-warn.patch --]
[-- Type: text/plain, Size: 1711 bytes --]

This patch adds warning to detect kref_put() with unreferenced object.

The idea of detection kref_put() with unreferenced object was stolen
from BUG_ON()es in blocks/ll_rw_blk.c and fs/bio.c

ll_rw_blk.c:    BUG_ON(atomic_read(&ioc->refcount) == 0);

bio.c:          BIO_BUG_ON(!atomic_read(&bio->bi_cnt));

But the kref counter usually does not fall to zero. Because kref is
trying to reduce the number of atomic_dec_and_test()

So this patch also set kref counter to zero here:

+	if (atomic_read(&kref->refcount) == 1)
+		atomic_set(&kref->refcount, 0);

Signed-off-by: Akinobu Mita <mita@miraclelinux.com>
CC: Greg KH <greg@kroah.com>
CC: Patrick Mochel <mochel@osdl.org>

 lib/kref.c |   14 ++++++++------
 1 files changed, 8 insertions(+), 6 deletions(-)

Index: 2.6-git/lib/kref.c
===================================================================
--- 2.6-git.orig/lib/kref.c
+++ 2.6-git/lib/kref.c
@@ -49,6 +49,7 @@ void kref_get(struct kref *kref)
  */
 int kref_put(struct kref *kref, void (*release)(struct kref *kref))
 {
+	WARN_ON(atomic_read(&kref->refcount) < 1);
 	WARN_ON(release == NULL);
 	WARN_ON(release == (void (*)(struct kref *))kfree);
 
@@ -56,12 +57,13 @@ int kref_put(struct kref *kref, void (*r
 	 * 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;
-	}
-	return 0;
+	if (atomic_read(&kref->refcount) == 1)
+		atomic_set(&kref->refcount, 0);
+	else if (!atomic_dec_and_test(&kref->refcount))
+		return 0;
+
+	release(kref);
+	return 1;
 }
 
 EXPORT_SYMBOL(kref_init);

--

  reply	other threads:[~2006-04-25  8:24 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-04-25  8:21 [patch 0/2] kref debugging (retry) Akinobu Mita
2006-04-25  8:21 ` Akinobu Mita [this message]
2006-04-26  0:26   ` [patch 1/2] kref: detect kref_put() with unreferenced object Greg KH
2006-04-26  2:17     ` Akinobu Mita
2006-04-25  8:21 ` [patch 2/2] kref: kref debugging config option Akinobu Mita
2006-04-26  0:24   ` Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060425082359.767915000@localhost.localdomain \
    --to=mita@miraclelinux.com \
    --cc=akpm@osdl.org \
    --cc=greg@kroah.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mochel@osdl.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox