Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Pekka J Enberg
Date: Sunday, June 1, 2008 - 4:30 am

On Sun, Jun 01, 2008 at 01:29:39PM +0300, Pekka Enberg wrote:
 
On Sun, 1 Jun 2008, Paul Mundt wrote:

OK. If we do need it, then something like this should work. But I don't 
see how we could have these "arbitrary pointers" (meaning not returned 
by kmalloc()) to compound pages; otherwise the BUG_ON would trigger with 
SLAB as well. I also don't see any call-sites that do this (but I'm not an 
expert on nommu).

		Pekka

diff --git a/mm/nommu.c b/mm/nommu.c
index dca93fc..604e7de 100644
--- a/mm/nommu.c
+++ b/mm/nommu.c
@@ -109,16 +109,39 @@ unsigned int kobjsize(const void *objp)
 	 * If the object we have should not have ksize performed on it,
 	 * return size of 0
 	 */
-	if (!objp || (unsigned long)objp >= memory_end || !((page = virt_to_page(objp))))
+	if (!objp)
 		return 0;
 
+	if ((unsigned long)objp >= memory_end)
+		return 0;
+
+	page = virt_to_head_page(objp);
+	if (!page)
+		return 0;
+
+	/*
+	 * If the allocator sets PageSlab, we know the pointer came from
+	 * kmalloc(). The allocator, however, is not guaranteed to set PageSlab
+	 * so the pointer might still have come from kmalloc() (see the comments
+	 * below).
+	 */
 	if (PageSlab(page))
 		return ksize(objp);
 
-	BUG_ON(page->index < 0);
-	BUG_ON(page->index >= MAX_ORDER);
+	/*
+	 * The ksize() function is only guaranteed to work for pointers
+	 * returned by kmalloc(). So handle arbitrary pointers, that we expect
+	 * always to be compound pages, here. Note: we also handle pointers to
+	 * compound pages that came from kmalloc() here.
+	 */
+	if (PageCompound(page))
+		return PAGE_SIZE << compound_order(page);
 
-	return (PAGE_SIZE << page->index);
+	/*
+	 * Finally, handle pointers that came from kmalloc() that don't have
+	 * PageSlab set.
+	 */
+	return ksize(objp);
 }
 
 /*
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Pekka Enberg, (Wed May 28, 1:03 pm)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Pekka Enberg, (Wed May 28, 1:25 pm)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Christoph Lameter, (Wed May 28, 1:25 pm)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Christoph Lameter, (Wed May 28, 1:29 pm)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, David Howells, (Thu May 29, 6:08 am)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Pekka J Enberg, (Thu May 29, 6:21 am)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Christoph Lameter, (Thu May 29, 8:00 am)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Paul Mundt, (Thu May 29, 2:12 pm)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Pekka Enberg, (Sun Jun 1, 12:58 am)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Pekka J Enberg, (Sun Jun 1, 1:22 am)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Paul Mundt, (Sun Jun 1, 1:22 am)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Paul Mundt, (Sun Jun 1, 1:24 am)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Pekka J Enberg, (Sun Jun 1, 1:36 am)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Pekka J Enberg, (Sun Jun 1, 2:13 am)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Paul Mundt, (Sun Jun 1, 3:24 am)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Pekka Enberg, (Sun Jun 1, 3:29 am)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Paul Mundt, (Sun Jun 1, 4:21 am)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Pekka J Enberg, (Sun Jun 1, 4:30 am)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Paul Mundt, (Sun Jun 1, 10:59 pm)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Pekka Enberg, (Sun Jun 1, 11:32 pm)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Paul Mundt, (Sun Jun 1, 11:50 pm)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Pekka Enberg, (Sun Jun 1, 11:58 pm)
Re: [PATCH] nommu: fix kobjsize() for SLOB and SLUB, Paul Mundt, (Mon Jun 2, 12:01 am)