From: Mateusz Guzik <mguzik@redhat.com>
To: Mikulas Patocka <mpatocka@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
linux-kernel@vger.kernel.org,
"Nicholas A. Bellinger" <nab@linux-iscsi.org>,
linux-scsi@vger.kernel.org, target-devel@vger.kernel.org
Subject: Re: [PATCH] kref: warn on uninitialized kref
Date: Sat, 17 May 2014 13:04:55 +0200 [thread overview]
Message-ID: <20140517110454.GA1939@mguzik.redhat.com> (raw)
In-Reply-To: <alpine.LRH.2.02.1405170650440.15837@file01.intranet.prod.int.rdu2.redhat.com>
On Sat, May 17, 2014 at 06:53:17AM -0400, Mikulas Patocka wrote:
> I found a memory leak in iSCSI target that was caused by kref initialized
> to zero (the memory object was allocated with kzalloc, kref_init was not
> called and kref_put_spinlock_irqsave was called which changed "0" to "-1"
> and didn't free the object).
>
> Similar bugs may exist in other kernel areas, so I submit this patch that
> adds a check to kref.h. If the value is zero or negative, we can assume
> that it is uninitialized and we warn about it.
>
> Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
>
> ---
> include/linux/kref.h | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> Index: linux-3.15-rc5/include/linux/kref.h
> ===================================================================
> --- linux-3.15-rc5.orig/include/linux/kref.h 2013-07-02 22:23:38.000000000 +0200
> +++ linux-3.15-rc5/include/linux/kref.h 2014-05-16 18:56:18.000000000 +0200
> @@ -69,7 +69,7 @@ static inline int kref_sub(struct kref *
> void (*release)(struct kref *kref))
> {
> WARN_ON(release == NULL);
> -
> + WARN_ON_ONCE(atomic_read(&kref->refcount) <= 0);
> if (atomic_sub_and_test((int) count, &kref->refcount)) {
> release(kref);
> return 1;
> @@ -119,6 +119,7 @@ static inline int kref_put_spinlock_irqs
> unsigned long flags;
>
> WARN_ON(release == NULL);
> + WARN_ON_ONCE(atomic_read(&kref->refcount) <= 0);
> if (atomic_add_unless(&kref->refcount, -1, 1))
> return 0;
> spin_lock_irqsave(lock, flags);
> @@ -136,6 +137,7 @@ static inline int kref_put_mutex(struct
> struct mutex *lock)
> {
> WARN_ON(release == NULL);
> + WARN_ON_ONCE(atomic_read(&kref->refcount) <= 0);
> if (unlikely(!atomic_add_unless(&kref->refcount, -1, 1))) {
> mutex_lock(lock);
> if (unlikely(!atomic_dec_and_test(&kref->refcount))) {
This has a side effect of detecting some overputs, which is nice.
However, could be made better if kref_sub checked that refs you want to
take don't put the count below 0.
i.e.
WARN_ON(count > atomic_read(&kref->refcount));
this also detects your original problem.
--
Mateusz Guzik
next prev parent reply other threads:[~2014-05-17 11:04 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-05-17 10:49 [PATCH] target: fix memory leak on XCOPY Mikulas Patocka
2014-05-17 10:53 ` [PATCH] kref: warn on uninitialized kref Mikulas Patocka
2014-05-17 11:04 ` Mateusz Guzik [this message]
2014-05-17 12:36 ` Mikulas Patocka
2014-05-17 12:38 ` [PATCH v2] " Mikulas Patocka
2014-05-17 13:50 ` Bart Van Assche
2014-05-17 21:14 ` Mikulas Patocka
2014-05-18 7:17 ` Bart Van Assche
2014-05-19 8:19 ` Peter Zijlstra
2014-05-17 22:52 ` [PATCH] target: fix memory leak on XCOPY Nicholas A. Bellinger
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=20140517110454.GA1939@mguzik.redhat.com \
--to=mguzik@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=mpatocka@redhat.com \
--cc=nab@linux-iscsi.org \
--cc=peterz@infradead.org \
--cc=target-devel@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.