Skip to contentSkip to conversation

kmemcheck: Test the full object in kmemcheck_is_obj_initialized()

PATCH Applied Catalin Marinas 3 participants 7 messages Feb 8, 2010 – Feb 17, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kmemcheck: Test the full object in kmemcheck_is_obj_initialized()

This is a fix for bug #14845 (bugzilla.kernel.org). The update_checksum() function in mm/kmemleak.c calls kmemcheck_is_obj_initialised() before scanning an object. When KMEMCHECK_PARTIAL_OK is enabled, this function returns true. However, the crc32_le() reads smaller intervals (32-bit) for which kmemleak_is_obj_initialised() may be false leading to a kmemcheck warning.

Note that kmemcheck_is_obj_initialized() is currently only used by kmemleak before scanning a memory location.

Cc Andrew Morton <[email protected]>
Cc Christian Casteyde <[email protected]>
Cc Pekka Enberg <[email protected]>
Cc Vegard Nossum <[email protected]>
---
 arch/x86/mm/kmemcheck/kmemcheck.c |    2 +-
 arch/x86/mm/kmemcheck/shadow.c    |   16 ++++++++++++++--
 arch/x86/mm/kmemcheck/shadow.h    |    2 ++
 3 files changed, 17 insertions(+), 3 deletions(-)
arch/x86/mm/kmemcheck/kmemcheck.c +1 -1
index 8cc1833..b3b531a 100644
@@ -337,7 +337,7 @@ bool kmemcheck_is_obj_initialized(unsigned long addr, size_t size)
 	if (!shadow)
 		return true;
 
-	status = kmemcheck_shadow_test(shadow, size);
+	status = kmemcheck_shadow_test_all(shadow, size);
 
 	return status == KMEMCHECK_SHADOW_INITIALIZED;
 }
arch/x86/mm/kmemcheck/shadow.c +14 -2
index 3f66b82..aec1242 100644
@@ -125,12 +125,12 @@ void kmemcheck_mark_initialized_pages(struct page *p, unsigned int n)
 
 enum kmemcheck_shadow kmemcheck_shadow_test(void *shadow, unsigned int size)
 {
+#ifdef CONFIG_KMEMCHECK_PARTIAL_OK
 	uint8_t *x;
 	unsigned int i;
 
 	x = shadow;
 
-#ifdef CONFIG_KMEMCHECK_PARTIAL_OK
 	/*
 	 * Make sure _some_ bytes are initialized. Gcc frequently generates
 	 * code to access neighboring bytes.
@@ -139,13 +139,25 @@ enum kmemcheck_shadow kmemcheck_shadow_test(void *shadow, unsigned int size)
 		if (x[i] == KMEMCHECK_SHADOW_INITIALIZED)
 			return x[i];
 	}
+
+	return x[0];
 #else
+	return kmemcheck_shadow_test_all(shadow, size);
+#endif
+}
+
+enum kmemcheck_shadow kmemcheck_shadow_test_all(void *shadow, unsigned int size)
+{
+	uint8_t *x;
+	unsigned int i;
+
+	x = shadow;
+
 	/* All bytes must be initialized. */
 	for (i = 0; i < size; ++i) {
 		if (x[i] != KMEMCHECK_SHADOW_INITIALIZED)
 			return x[i];
 	}
-#endif
 
 	return x[0];
 }
arch/x86/mm/kmemcheck/shadow.h +2 -0
index af46d9a..ff0b2f7 100644
@@ -11,6 +11,8 @@ enum kmemcheck_shadow {
 void *kmemcheck_shadow_lookup(unsigned long address);
 
 enum kmemcheck_shadow kmemcheck_shadow_test(void *shadow, unsigned int size);
+enum kmemcheck_shadow kmemcheck_shadow_test_all(void *shadow,
+						unsigned int size);
 void kmemcheck_shadow_set(void *shadow, unsigned int size);
 
 #endif
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Catalin Marinas

This is a fix for bug #14845 (bugzilla.kernel.org). The update_checksum() function in mm/kmemleak.c calls kmemcheck_is_obj_initialised() before scanning an object. When KMEMCHECK_PARTIAL_OK is enabled, this function returns true. However, the crc32_le() reads smaller intervals (32-bit) for which kmemleak_is_obj_initialised() may be false leading to a kmemcheck warning.

Quoted 8 lines

Note that kmemcheck_is_obj_initialized() is currently only used by kmemleak before scanning a memory location.

Cc Andrew Morton <[email protected]>
Cc Christian Casteyde <[email protected]>
Cc Pekka Enberg <[email protected]>
Cc Vegard Nossum <[email protected]>

Andrew, Vegard has been rather quiet lately so do you mind picking up this patch? Alternatively, I can pick it up in slab.git if you so prefer.

Quoted 84 lines

---
 arch/x86/mm/kmemcheck/kmemcheck.c |    2 +- arch/x86/mm/kmemcheck/shadow.c    |   16 ++++++++++++++-- arch/x86/mm/kmemcheck/shadow.h    |    2 ++

 3 files changed, 17 insertions(+), 3 deletions(-)
arch/x86/mm/kmemcheck/kmemcheck.c +1 -1
index 8cc1833..b3b531a 100644
@@ -337,7 +337,7 @@ bool kmemcheck_is_obj_initialized(unsigned long addr, size_t size)
        if (!shadow)
                return true;
 
-       status = kmemcheck_shadow_test(shadow, size);
+       status = kmemcheck_shadow_test_all(shadow, size);
 
        return status == KMEMCHECK_SHADOW_INITIALIZED;
  }
arch/x86/mm/kmemcheck/shadow.c +14 -2
index 3f66b82..aec1242 100644
@@ -125,12 +125,12 @@ void kmemcheck_mark_initialized_pages(struct page *p, unsigned int n)
 
  enum kmemcheck_shadow kmemcheck_shadow_test(void *shadow, unsigned int size)
  {
+#ifdef CONFIG_KMEMCHECK_PARTIAL_OK
        uint8_t *x;
        unsigned int i;
 
        x = shadow;
 
-#ifdef CONFIG_KMEMCHECK_PARTIAL_OK
        /*
         * Make sure _some_ bytes are initialized. Gcc frequently generates
         * code to access neighboring bytes.
@@ -139,13 +139,25 @@ enum kmemcheck_shadow kmemcheck_shadow_test(void *shadow, unsigned int size)
                if (x[i] == KMEMCHECK_SHADOW_INITIALIZED)
                        return x[i];
        }
+
+       return x[0];
  #else
+       return kmemcheck_shadow_test_all(shadow, size);
+#endif
+}
+
+enum kmemcheck_shadow kmemcheck_shadow_test_all(void *shadow, unsigned int size)
+{
+       uint8_t *x;
+       unsigned int i;
+
+       x = shadow;
+
        /* All bytes must be initialized. */
        for (i = 0; i < size; ++i) {
                if (x[i] != KMEMCHECK_SHADOW_INITIALIZED)
                        return x[i];
        }
-#endif
 
        return x[0];
  }
arch/x86/mm/kmemcheck/shadow.h +2 -1
index af46d9a..ff0b2f7 100644
@@ -11,6 +11,8 @@ enum kmemcheck_shadow {
  void *kmemcheck_shadow_lookup(unsigned long address);
 
  enum kmemcheck_shadow kmemcheck_shadow_test(void *shadow, unsigned int size);
+enum kmemcheck_shadow kmemcheck_shadow_test_all(void *shadow,
+                                               unsigned int size);
  void kmemcheck_shadow_set(void *shadow, unsigned int size);
 
  #endif
 
--

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

#2 lore raw
Quoted 18 lines · Pekka Enberg
Quoted 8 lines · Catalin Marinas

This is a fix for bug #14845 (bugzilla.kernel.org). The update_checksum() function in mm/kmemleak.c calls kmemcheck_is_obj_initialised() before scanning an object. When KMEMCHECK_PARTIAL_OK is enabled, this function returns true. However, the crc32_le() reads smaller intervals (32-bit) for which kmemleak_is_obj_initialised() may be false leading to a kmemcheck warning.

Quoted 8 lines

Note that kmemcheck_is_obj_initialized() is currently only used by kmemleak before scanning a memory location.

Cc Andrew Morton <[email protected]>
Cc Christian Casteyde <[email protected]>
Cc Pekka Enberg <[email protected]>
Cc Vegard Nossum <[email protected]>
Quoted 5 lines

Andrew, Vegard has been rather quiet lately so do you mind picking up this patch? Alternatively, I can pick it up in slab.git if you so prefer.

It's been pretty late indeed, sorry about that (I mostly work on ARM hardware where kmemcheck isn't available and couldn't test the patch earlier).

Anyway, IMHO it's not critical to be merged in 2.6.33 but it's up to you.

Thanks.

Signature · 1 line
Catalin
#3 lore raw
Quoted 25 lines · Catalin Marinas
Quoted 18 lines · Pekka Enberg
Quoted 8 lines · Catalin Marinas

This is a fix for bug #14845 (bugzilla.kernel.org). The update_checksum() function in mm/kmemleak.c calls kmemcheck_is_obj_initialised() before scanning an object. When KMEMCHECK_PARTIAL_OK is enabled, this function returns true. However, the crc32_le() reads smaller intervals (32-bit) for which kmemleak_is_obj_initialised() may be false leading to a kmemcheck warning.

Quoted 8 lines

Note that kmemcheck_is_obj_initialized() is currently only used by kmemleak before scanning a memory location.

Cc Andrew Morton <[email protected]>
Cc Christian Casteyde <[email protected]>
Cc Pekka Enberg <[email protected]>
Cc Vegard Nossum <[email protected]>
Quoted 5 lines

Andrew, Vegard has been rather quiet lately so do you mind picking up this patch? Alternatively, I can pick it up in slab.git if you so prefer.

Quoted 6 lines

It's been pretty late indeed, sorry about that (I mostly work on ARM hardware where kmemcheck isn't available and couldn't test the patch earlier).

Anyway, IMHO it's not critical to be merged in 2.6.33 but it's up to you.

I think it's definitely .34 material but probably should go to -stable.

Pekka

#4 lore raw
In reply to Catalin Marinas
Quoted 40 lines · Catalin Marinas

This is a fix for bug #14845 (bugzilla.kernel.org). The update_checksum() function in mm/kmemleak.c calls kmemcheck_is_obj_initialised() before scanning an object. When KMEMCHECK_PARTIAL_OK is enabled, this function returns true. However, the crc32_le() reads smaller intervals (32-bit) for which kmemleak_is_obj_initialised() may be false leading to a kmemcheck warning.

Note that kmemcheck_is_obj_initialized() is currently only used by kmemleak before scanning a memory location.

 enum kmemcheck_shadow kmemcheck_shadow_test(void *shadow, unsigned int size) {
+#ifdef CONFIG_KMEMCHECK_PARTIAL_OK
       uint8_t *x;
       unsigned int i;

       x = shadow;

-#ifdef CONFIG_KMEMCHECK_PARTIAL_OK
       /*
        * Make sure _some_ bytes are initialized. Gcc frequently generates
        * code to access neighboring bytes.

arch/x86/mm/kmemcheck/shadow.c +12 -0
@@ -139,13 +139,25 @@ enum kmemcheck_shadow kmemcheck_shadow_test(void *shadow, unsigned int size)
                if (x[i] == KMEMCHECK_SHADOW_INITIALIZED)
                        return x[i];
        }
+
+       return x[0];
  #else
+       return kmemcheck_shadow_test_all(shadow, size);
+#endif
+}
+
+enum kmemcheck_shadow kmemcheck_shadow_test_all(void *shadow, unsigned int size)
+{
+       uint8_t *x;
+       unsigned int i;
+
+       x = shadow;

arch/x86/mm/kmemcheck/shadow.c enum kmemcheck_shadow kmemcheck_shadow_test(void *shadow, unsigned int size) · line 163

Quoted 10 lines
arch/x86/mm/kmemcheck/shadow.c +1 -1
+
        /* All bytes must be initialized. */
        for (i = 0; i < size; ++i) {
                if (x[i] != KMEMCHECK_SHADOW_INITIALIZED)
                        return x[i];
        }
-#endif
 
        return x[0];
  }

Are we certain that size cannot be 0 in kmemcheck_shadow_test() and kmemcheck_shadow_test_all() or other functions in arch/x86/mm/kmemcheck/shadow.c with these unsigned comparisons in loops?

Roel

#5 lore raw
Quoted 49 lines · roel kluin
Quoted 48 lines · Catalin Marinas

This is a fix for bug #14845 (bugzilla.kernel.org). The update_checksum() function in mm/kmemleak.c calls kmemcheck_is_obj_initialised() before scanning an object. When KMEMCHECK_PARTIAL_OK is enabled, this function returns true. However, the crc32_le() reads smaller intervals (32-bit) for which kmemleak_is_obj_initialised() may be false leading to a kmemcheck warning.

Note that kmemcheck_is_obj_initialized() is currently only used by kmemleak before scanning a memory location.

enum kmemcheck_shadow kmemcheck_shadow_test(void *shadow, unsigned int size) {

arch/x86/mm/kmemcheck/shadow.c +14 -2
+#ifdef CONFIG_KMEMCHECK_PARTIAL_OK
       uint8_t *x;
       unsigned int i;
 
       x = shadow;
 
-#ifdef CONFIG_KMEMCHECK_PARTIAL_OK
       /*
        * Make sure _some_ bytes are initialized. Gcc frequently generates
        * code to access neighboring bytes.
@@ -139,13 +139,25 @@ enum kmemcheck_shadow kmemcheck_shadow_test(void *shadow, unsigned int size)
               if (x[i] == KMEMCHECK_SHADOW_INITIALIZED)
                       return x[i];
       }
+
+       return x[0];
 #else
+       return kmemcheck_shadow_test_all(shadow, size);
+#endif
+}
+
+enum kmemcheck_shadow kmemcheck_shadow_test_all(void *shadow, unsigned int size)
+{
+       uint8_t *x;
+       unsigned int i;
+
+       x = shadow;
+
       /* All bytes must be initialized. */
       for (i = 0; i < size; ++i) {
               if (x[i] != KMEMCHECK_SHADOW_INITIALIZED)
                       return x[i];
       }
-#endif
 
Quoted 7 lines

arch/x86/mm/kmemcheck/shadow.c enum kmemcheck_shadow kmemcheck_shadow_test(void *shadow, unsigned int size) · line 163

Quoted 2 lines
arch/x86/mm/kmemcheck/shadow.c +0 -0
       return x[0];
 }

Are we certain that size cannot be 0 in kmemcheck_shadow_test() and kmemcheck_shadow_test_all() or other functions in arch/x86/mm/kmemcheck/shadow.c with these unsigned comparisons in loops?

At least in the kmemleak use-case, I don't think one can allocate a zero-size object, so it should be safe. I can't tell about the other cases but AFAICT, this function is called as a result of a memory access which is always greater than 0.

Thanks.

Signature · 1 line
Catalin
Applied
#6 lore raw
In reply to Catalin Marinas
Quoted 8 lines · Catalin Marinas

This is a fix for bug #14845 (bugzilla.kernel.org). The update_checksum() function in mm/kmemleak.c calls kmemcheck_is_obj_initialised() before scanning an object. When KMEMCHECK_PARTIAL_OK is enabled, this function returns true. However, the crc32_le() reads smaller intervals (32-bit) for which kmemleak_is_obj_initialised() may be false leading to a kmemcheck warning.

Quoted 8 lines

Note that kmemcheck_is_obj_initialized() is currently only used by kmemleak before scanning a memory location.

Cc Andrew Morton <[email protected]>
Cc Christian Casteyde <[email protected]>
Cc Pekka Enberg <[email protected]>
Cc Vegard Nossum <[email protected]>

Applied to slab.git.