mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Pekka Enberg <penberg@cs.helsinki.fi>
To: Catalin Marinas <catalin.marinas@arm.com>
Cc: Vegard Nossum <vegard.nossum@gmail.com>,
	Ingo Molnar <mingo@elte.hu>,
	linux-kernel@vger.kernel.org
Subject: Re: WARNING: kmemcheck: Caught 32-bit read from uninitialized memory  (f6f6e1a4), by kmemleak's scan_block()
Date: Tue, 25 Aug 2009 12:26:55 +0300	[thread overview]
Message-ID: <1251192415.26351.5.camel@penberg-laptop> (raw)
In-Reply-To: <1251192069.15678.21.camel@pc1117.cambridge.arm.com>

On Tue, 2009-08-25 at 10:21 +0100, Catalin Marinas wrote:
> On Tue, 2009-08-25 at 12:11 +0300, Pekka Enberg wrote:
> > On Tue, 2009-08-25 at 11:03 +0200, Vegard Nossum wrote:
> > > I don't know so much about the kmemleak internals, but this I can say
> > > about the kmemcheck part: According to your definition, an object is
> > > initialized if all the bytes of an object are initialized.
> > > 
> > > Is it possible that because of this, if we have a partially
> > > uninitialized object, kmemleak will not record the pointers found in
> > > that object? If so, it might skip valid pointers, and deem an object
> > > unreferenced. Which could make kmemleak give false-positives.
> > > 
> > > I think it would be better to ask kmemcheck on a per-pointer basis
> > > (i.e. for each pointer-sized word in the object), whether it is
> > > initialized or not.
> > 
> > Yeah, makes sense.
> 
> I think this patch should work. With a few minor (aesthetic) things
> below and assuming that Ingo tests it (I don't have x86 hardware at hand
> now):

Does this look OK to you?

>From 2ee25007d6bfe931ff2ae6c7e22a098578fb2dc5 Mon Sep 17 00:00:00 2001
From: Pekka Enberg <penberg@cs.helsinki.fi>
Date: Tue, 25 Aug 2009 12:22:51 +0300
Subject: [PATCH] kmemleak: Don't scan uninitialized memory when kmemcheck is enabled

Ingo Molnar reported the following kmemcheck warning when running both
kmemleak and kmemcheck enabled:

  PM: Adding info for No Bus:vcsa7
  WARNING: kmemcheck: Caught 32-bit read from uninitialized memory
  (f6f6e1a4)
  d873f9f600000000c42ae4c1005c87f70000000070665f666978656400000000
   i i i i u u u u i i i i i i i i i i i i i i i i i i i i i u u u
           ^

  Pid: 3091, comm: kmemleak Not tainted (2.6.31-rc7-tip #1303) P4DC6
  EIP: 0060:[<c110301f>] EFLAGS: 00010006 CPU: 0
  EIP is at scan_block+0x3f/0xe0
  EAX: f40bd700 EBX: f40bd780 ECX: f16b46c0 EDX: 00000001
  ESI: f6f6e1a4 EDI: 00000000 EBP: f10f3f4c ESP: c2605fcc
   DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068
  CR0: 8005003b CR2: e89a4844 CR3: 30ff1000 CR4: 000006f0
  DR0: 00000000 DR1: 00000000 DR2: 00000000 DR3: 00000000
  DR6: ffff4ff0 DR7: 00000400
   [<c110313c>] scan_object+0x7c/0xf0
   [<c1103389>] kmemleak_scan+0x1d9/0x400
   [<c1103a3c>] kmemleak_scan_thread+0x4c/0xb0
   [<c10819d4>] kthread+0x74/0x80
   [<c10257db>] kernel_thread_helper+0x7/0x3c
   [<ffffffff>] 0xffffffff
  kmemleak: 515 new suspected memory leaks (see
  /sys/kernel/debug/kmemleak)
  kmemleak: 42 new suspected memory leaks (see /sys/kernel/debug/kmemleak)

The problem here is that kmemleak will scan partially initialized
objects that makes kmemcheck complain. Fix that up by skipping
uninitialized memory regions when kmemcheck is enabled.

Reported-by: Ingo Molnar <mingo@elte.hu>
Acked-by: Catalin Marinas <catalin.marinas@arm.com>
Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi>
---
 arch/x86/mm/kmemcheck/kmemcheck.c |   14 ++++++++++++++
 include/linux/kmemcheck.h         |    7 +++++++
 mm/kmemleak.c                     |   12 ++++++++++--
 3 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/arch/x86/mm/kmemcheck/kmemcheck.c b/arch/x86/mm/kmemcheck/kmemcheck.c
index 2c55ed0..528bf95 100644
--- a/arch/x86/mm/kmemcheck/kmemcheck.c
+++ b/arch/x86/mm/kmemcheck/kmemcheck.c
@@ -331,6 +331,20 @@ static void kmemcheck_read_strict(struct pt_regs *regs,
 	kmemcheck_shadow_set(shadow, size);
 }
 
+bool kmemcheck_is_obj_initialized(unsigned long addr, size_t size)
+{
+	enum kmemcheck_shadow status;
+	void *shadow;
+
+	shadow = kmemcheck_shadow_lookup(addr);
+	if (!shadow)
+		return true;
+
+	status = kmemcheck_shadow_test(shadow, size);
+
+	return status == KMEMCHECK_SHADOW_INITIALIZED;
+}
+
 /* Access may cross page boundary */
 static void kmemcheck_read(struct pt_regs *regs,
 	unsigned long addr, unsigned int size)
diff --git a/include/linux/kmemcheck.h b/include/linux/kmemcheck.h
index 47b39b7..dc2fd54 100644
--- a/include/linux/kmemcheck.h
+++ b/include/linux/kmemcheck.h
@@ -34,6 +34,8 @@ void kmemcheck_mark_initialized_pages(struct page *p, unsigned int n);
 int kmemcheck_show_addr(unsigned long address);
 int kmemcheck_hide_addr(unsigned long address);
 
+bool kmemcheck_is_obj_initialized(unsigned long addr, size_t size);
+
 #else
 #define kmemcheck_enabled 0
 
@@ -99,6 +101,11 @@ static inline void kmemcheck_mark_initialized_pages(struct page *p,
 {
 }
 
+static inline bool kmemcheck_is_obj_initialized(unsigned long addr, size_t size)
+{
+	return true;
+}
+
 #endif /* CONFIG_KMEMCHECK */
 
 /*
diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 6debe0d..e217c21 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -97,6 +97,7 @@
 #include <asm/processor.h>
 #include <asm/atomic.h>
 
+#include <linux/kmemcheck.h>
 #include <linux/kmemleak.h>
 
 /*
@@ -884,15 +885,22 @@ static void scan_block(void *_start, void *_end,
 	unsigned long *end = _end - (BYTES_PER_POINTER - 1);
 
 	for (ptr = start; ptr < end; ptr++) {
-		unsigned long flags;
-		unsigned long pointer = *ptr;
 		struct kmemleak_object *object;
+		unsigned long flags;
+		unsigned long pointer;
 
 		if (allow_resched)
 			cond_resched();
 		if (scan_should_stop())
 			break;
 
+		/* Don't scan uninitialized memory. */
+		if (!kmemcheck_is_obj_initialized((unsigned long) ptr,
+							BYTES_PER_POINTER))
+			continue;
+
+		pointer = *ptr;
+
 		object = find_and_get_object(pointer, 1);
 		if (!object)
 			continue;
-- 
1.5.6.3




  reply	other threads:[~2009-08-25  9:26 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-25  7:19 Ingo Molnar
2009-08-25  8:04 ` Vegard Nossum
2009-08-25  8:08   ` Pekka Enberg
2009-08-25  8:27     ` Catalin Marinas
2009-08-25  8:31       ` Pekka Enberg
2009-08-25  8:40         ` Ingo Molnar
2009-08-25  8:48           ` Pekka Enberg
2009-08-25  8:32   ` Ingo Molnar
2009-08-25  8:45     ` Pekka Enberg
2009-08-25  8:48       ` Ingo Molnar
2009-08-25  8:54         ` Pekka Enberg
2009-08-25  9:03           ` Vegard Nossum
2009-08-25  9:11             ` Catalin Marinas
2009-08-25  9:15               ` Pekka Enberg
2009-08-25  9:25                 ` Catalin Marinas
2009-08-25  9:11             ` Pekka Enberg
2009-08-25  9:21               ` Catalin Marinas
2009-08-25  9:26                 ` Pekka Enberg [this message]
2009-08-25  9:28                   ` Catalin Marinas
2009-08-25  9:31                     ` Pekka Enberg
2009-08-25  9:34                       ` Catalin Marinas
2009-08-25  9:34                       ` Ingo Molnar
2009-08-25 10:43                         ` Ingo Molnar
2009-08-25 13:57                           ` Catalin Marinas
2009-08-26 10:48                         ` Pekka Enberg
2009-08-26 11:17                           ` Ingo Molnar
2009-08-25  9:25               ` Ingo Molnar

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=1251192415.26351.5.camel@penberg-laptop \
    --to=penberg@cs.helsinki.fi \
    --cc=catalin.marinas@arm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=vegard.nossum@gmail.com \
    /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

all inboxes | Powered by JetHome®