Skip to contentSkip to conversation

fix slub krealloc()

RFC In review Johannes Berg 3 participants 7 messages Jul 18, 2007 – Jul 26, 2007

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

RFC 1/1 fix slub krealloc()

Commit ef2ad80c7d255ed0449eda947c2d700635b7e0f5 breaks krealloc(NULL, ...) badly, it BUG_ON()s.

This patch fixes it but the fix should probably be in mm/util.c because the documentation says that krealloc(NULL, x, gfp) is equivalent to kmalloc(x, gfp) and that's not true any more even after this patch when you do krealloc(NULL, 0, gfp)

johannes

---
 mm/slub.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slub.c +1 -1
@@ -2394,7 +2394,7 @@ size_t ksize(const void *object)
 	struct page *page;
 	struct kmem_cache *s;
 
-	if (object == ZERO_SIZE_PTR)
+	if (object == ZERO_SIZE_PTR || !object)
 		return 0;
 
 	page = get_object_page(object);
#1 lore raw
Quoted 7 lines · On Wed, 18 Jul 2007 17:42:11 +0200 Johannes Berg <[email protected]>…

Commit ef2ad80c7d255ed0449eda947c2d700635b7e0f5 breaks krealloc(NULL, ...) badly, it BUG_ON()s.

This patch fixes it but the fix should probably be in mm/util.c because the documentation says that krealloc(NULL, x, gfp) is equivalent to kmalloc(x, gfp) and that's not true any more even after this patch when you do krealloc(NULL, 0, gfp)

Right. We need to fix util.c. ksize should not be called with a NULL parameter.

mm/util.c +5 -1
Index: linux-2.6/mm/util.c
===================================================================
@@ -88,7 +88,11 @@ void *krealloc(const void *p, size_t new
 		return ZERO_SIZE_PTR;
 	}
 
-	ks = ksize(p);
+	if (p)
+		ks = ksize(p);
+	else
+		ks = 0;
+
 	if (ks >= new_size)
 		return (void *)p;
 
maintainer
#2 lore raw
Quoted 20 lines · On Mon, 23 Jul 2007 13:32:06 -0700 Christoph Lameter <[email protected]> wrote:
Quoted 7 lines · On Wed, 18 Jul 2007 17:42:11 +0200 Johannes Berg <[email protected]>…

Commit ef2ad80c7d255ed0449eda947c2d700635b7e0f5 breaks krealloc(NULL, ...) badly, it BUG_ON()s.

This patch fixes it but the fix should probably be in mm/util.c because the documentation says that krealloc(NULL, x, gfp) is equivalent to kmalloc(x, gfp) and that's not true any more even after this patch when you do krealloc(NULL, 0, gfp)

Right. We need to fix util.c. ksize should not be called with a NULL parameter.
Index: linux-2.6/mm/util.c
===================================================================

mm/util.c +0 -0
@@ -88,7 +88,11 @@ void *krealloc(const void *p, size_t new
 		return ZERO_SIZE_PTR;
 	}

mm/util.c void *krealloc(const void *p, size_t new · line 98

Quoted 10 lines
mm/util.c +5 -1
 
-	ks = ksize(p);
+	if (p)
+		ks = ksize(p);
+	else
+		ks = 0;
+
 	if (ks >= new_size)
 		return (void *)p;
 

I think we already fixed this?

commit 1d4ec7b1d6f130818f9b62dea3411d9ee2ff6ff6

Author: Roland Dreier <[email protected]>
Date:   Fri Jul 20 12:13:20 2007 -0700

Fix ZERO_OR_NULL_PTR(ZERO_SIZE_PTR)

The comparison with ZERO_SIZE_PTR in ZERO_OR_NULL_PTR() needs to be <= (not just <) so that ZERO_OR_NULL_PTR(ZERO_SIZE_PTR) is 1.

include/linux/slab.h +1 -1
index 7d0ecc1..d859354 100644
@@ -40,7 +40,7 @@ #define SLAB_TRACE		0x00200000UL	/* Trac
  */
 #define ZERO_SIZE_PTR ((void *)16)
 
-#define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) < \
+#define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \
 				(unsigned long)ZERO_SIZE_PTR)
 
 /*
#3 lore raw
Quoted 22 lines · On Mon, 23 Jul 2007 13:47:13 -0700 Andrew Morton <[email protected]>…
Quoted 9 lines

Right. We need to fix util.c. ksize should not be called with a NULL parameter.
Index: linux-2.6/mm/util.c
===================================================================
--- linux-2.6.orig/mm/util.c 2007-07-23 13:29:42.000000000
-0700 +++ linux-2.6/mm/util.c 2007-07-23 13:31:28.000000000
-0700 @@ -88,7 +88,11 @@ void *krealloc(const void *p, size_t new

(quoted diff) +0 -0
 		return ZERO_SIZE_PTR;

Review

Quoted 10 lines
(quoted diff) +5 -1
 	}
 
-	ks = ksize(p);
+	if (p)
+		ks = ksize(p);
+	else
+		ks = 0;
+
 	if (ks >= new_size)
 		return (void *)p;

I think we already fixed this?

Quoted 5 lines

commit 1d4ec7b1d6f130818f9b62dea3411d9ee2ff6ff6

Author: Roland Dreier <[email protected]>
Date:   Fri Jul 20 12:13:20 2007 -0700

Fix ZERO_OR_NULL_PTR(ZERO_SIZE_PTR)

There is no use of ZERO_OR_NULL ptr in krealloc. Linus added a check to ksize() instead so that ksize(NULL) returns 0 instead of failing.

#4 lore raw
Quoted 7 lines
Quoted 4 lines

I think we already fixed this?

Fix ZERO_OR_NULL_PTR(ZERO_SIZE_PTR)

There is no use of ZERO_OR_NULL ptr in krealloc. Linus added a check to ksize() instead so that ksize(NULL) returns 0 instead of failing.

However, this still doesn't fix the other corner case I pointed out:
krealloc(NULL, 0, GFP_KERNEL) will give you a NULL pointer instead of a ZERO_SIZE_PTR afaict.

johannes

#5 lore raw
Quoted 7 lines · Johannes Berg
Quoted 3 lines

There is no use of ZERO_OR_NULL ptr in krealloc. Linus added a check to ksize() instead so that ksize(NULL) returns 0 instead of failing.

However, this still doesn't fix the other corner case I pointed out:
krealloc(NULL, 0, GFP_KERNEL) will give you a NULL pointer instead of a ZERO_SIZE_PTR afaict.

It will give you a ZERO_SIZE_PTR

void *krealloc(const void *p, size_t new_size, gfp_t flags)

{
        void *ret;
        size_t ks;

        if (unlikely(!new_size)) {
                kfree(p);
                return ZERO_SIZE_PTR;
        }
#6 lore raw
Quoted 11 lines · Christoph Lameter

It will give you a ZERO_SIZE_PTR

void *krealloc(const void *p, size_t new_size, gfp_t flags)

{
        void *ret;
        size_t ks;

        if (unlikely(!new_size)) {
                kfree(p);
                return ZERO_SIZE_PTR;
        }

Oh, I thought looked and it returned NULL. Did it change or did I just look wrong? :)

johannes