From: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
To: Catalin Marinas <catalin.marinas@arm.com>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: [PATCH] kmemleak: do not leak object after tree insertion error (v2, fixed)
Date: Tue, 3 Apr 2012 02:06:56 +0300 [thread overview]
Message-ID: <20120402230656.GA4353@swordfish> (raw)
[PATCH] kmemleak: do not leak object after tree insertion error
In case when tree insertion fails due to already existing object
error, pointer to allocated object gets lost due to lookup_object()
overwrite. Free allocated object and return the existing one,
obtained from lookup_object().
Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
---
mm/kmemleak.c | 19 ++++++++++++-------
1 file changed, 12 insertions(+), 7 deletions(-)
diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 45eb621..4177d83 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -516,7 +516,7 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size,
int min_count, gfp_t gfp)
{
unsigned long flags;
- struct kmemleak_object *object;
+ struct kmemleak_object *object, *ex_object;
struct prio_tree_node *node;
object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
@@ -578,17 +578,22 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size,
if (node != &object->tree_node) {
kmemleak_stop("Cannot insert 0x%lx into the object search tree "
"(already existing)\n", ptr);
- object = lookup_object(ptr, 1);
- spin_lock(&object->lock);
- dump_object_info(object);
- spin_unlock(&object->lock);
+ ex_object = lookup_object(ptr, 1);
+ spin_lock(&ex_object->lock);
+ dump_object_info(ex_object);
+ spin_unlock(&ex_object->lock);
- goto out;
+ goto out_error;
}
list_add_tail_rcu(&object->object_list, &object_list);
-out:
+
write_unlock_irqrestore(&kmemleak_lock, flags);
return object;
+out_error:
+ write_unlock_irqrestore(&kmemleak_lock, flags);
+ object->flags &= ~OBJECT_ALLOCATED;
+ put_object(object);
+ return ex_object;
}
/*
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
WARNING: multiple messages have this Message-ID (diff)
From: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
To: Catalin Marinas <catalin.marinas@arm.com>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: [PATCH] kmemleak: do not leak object after tree insertion error (v2, fixed)
Date: Tue, 3 Apr 2012 02:06:56 +0300 [thread overview]
Message-ID: <20120402230656.GA4353@swordfish> (raw)
[PATCH] kmemleak: do not leak object after tree insertion error
In case when tree insertion fails due to already existing object
error, pointer to allocated object gets lost due to lookup_object()
overwrite. Free allocated object and return the existing one,
obtained from lookup_object().
Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
---
mm/kmemleak.c | 19 ++++++++++++-------
1 file changed, 12 insertions(+), 7 deletions(-)
diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 45eb621..4177d83 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -516,7 +516,7 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size,
int min_count, gfp_t gfp)
{
unsigned long flags;
- struct kmemleak_object *object;
+ struct kmemleak_object *object, *ex_object;
struct prio_tree_node *node;
object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
@@ -578,17 +578,22 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size,
if (node != &object->tree_node) {
kmemleak_stop("Cannot insert 0x%lx into the object search tree "
"(already existing)\n", ptr);
- object = lookup_object(ptr, 1);
- spin_lock(&object->lock);
- dump_object_info(object);
- spin_unlock(&object->lock);
+ ex_object = lookup_object(ptr, 1);
+ spin_lock(&ex_object->lock);
+ dump_object_info(ex_object);
+ spin_unlock(&ex_object->lock);
- goto out;
+ goto out_error;
}
list_add_tail_rcu(&object->object_list, &object_list);
-out:
+
write_unlock_irqrestore(&kmemleak_lock, flags);
return object;
+out_error:
+ write_unlock_irqrestore(&kmemleak_lock, flags);
+ object->flags &= ~OBJECT_ALLOCATED;
+ put_object(object);
+ return ex_object;
}
/*
next reply other threads:[~2012-04-02 23:07 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-02 23:06 Sergey Senozhatsky [this message]
2012-04-02 23:06 ` [PATCH] kmemleak: do not leak object after tree insertion error (v2, fixed) Sergey Senozhatsky
2012-04-18 14:40 ` Catalin Marinas
2012-04-18 14:40 ` Catalin Marinas
2012-04-18 15:29 ` Sergey Senozhatsky
2012-04-18 15:29 ` Sergey Senozhatsky
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=20120402230656.GA4353@swordfish \
--to=sergey.senozhatsky@gmail.com \
--cc=catalin.marinas@arm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.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.