Re: [BUG 2.6.24-rc3-git6] SLUB's ksize() fails for size > 2048.

From: Vegard Nossum
Date: Sun Dec 02 2007 - 11:30:29 EST


On Dec 2, 2007 11:39 AM, Tetsuo Handa
<penguin-kernel@xxxxxxxxxxxxxxxxxxx> wrote:
> Hello.
>
> I can't pass memory allocated by kmalloc() to ksize()
> if it is allocated by SLUB allocator and
> size is larger than (I guess) PAGE_SIZE / 2.
>
> Regards.

Take a look at mm/slub.c around line 2560, in __kmalloc:
if (unlikely(size > PAGE_SIZE / 2))
return (void *)__get_free_pages(flags | __GFP_COMP,
get_order(size));


So it seems that kmalloc simply returns pages from the page allocator
in this case. Therefore no SLUB metadata will be available for the
allocation.

The error of ksize() seems to be that it does not check if the
allocation was made by SLUB or the page allocator. Maybe something
like this will fix it? (completely untested)

diff --git a/mm/slub.c b/mm/slub.c
index 9acb413..1cdca59 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2552,6 +2552,7 @@ EXPORT_SYMBOL(__kmalloc_node);
size_t ksize(const void *object)
{
struct page *page;
+ struct page *head;
struct kmem_cache *s;

BUG_ON(!object);
@@ -2560,6 +2561,13 @@ size_t ksize(const void *object)

page = get_object_page(object);
BUG_ON(!page);
+
+ head = compound_head(page);
+ BUG_ON(!head);
+
+ if (unlikely(!(head->flags & PG_slab)))
+ return PAGE_SIZE << compound_order(head);
+
s = page->slab;
BUG_ON(!s);



It's going to round up, though, so you would get ksize(kmalloc(2049))
= PAGE_SIZE.


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