Skip to contentSkip to conversation

xmalloc: return unused full pages on multi-page allocations

PATCH In review Jan Beulich 2 participants 2 messages Oct 13, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xmalloc: return unused full pages on multi-page allocations

Certain (boot time) allocations are relatively large (particularly when building with high NR_CPUS), but can also happen to be pretty far away from a power-of-two size. Utilize the page allocator's (other than Linux'es) capability of allowing to return space from higher-order allocations in smaller pieces to return the unused parts immediately.

xen/common/xmalloc_tlsf.c +24 -3
@@ -527,13 +527,21 @@ static void xmalloc_pool_put(void *p)
 static void *xmalloc_whole_pages(unsigned long size)
 {
     struct bhdr *b;
-    unsigned int pageorder = get_order_from_bytes(size + BHDR_OVERHEAD);
+    unsigned int i, pageorder = get_order_from_bytes(size + BHDR_OVERHEAD);
+    char *p;
 
     b = alloc_xenheap_pages(pageorder, 0);
     if ( b == NULL )
         return NULL;
 
-    b->size = (1 << (pageorder + PAGE_SHIFT));
+    b->size = PAGE_ALIGN(size + BHDR_OVERHEAD);
+    for ( p = (char *)b + b->size, i = 0; i < pageorder; ++i )
+        if ( (unsigned long)p & (PAGE_SIZE << i) )
+        {
+            free_xenheap_pages(p, i);
+            p += PAGE_SIZE << i;
+        }
+
     return (void *)b->ptr.buffer;
 }
 
@@ -611,7 +619,20 @@ void xfree(void *p)
     }
 
     if ( b->size >= PAGE_SIZE )
-        free_xenheap_pages((void *)b, get_order_from_bytes(b->size));
+    {
+        unsigned int i, order = get_order_from_bytes(b->size);
+
+        BUG_ON((unsigned long)b & ((PAGE_SIZE << order) - 1));
+        for ( i = 0; ; ++i )
+        {
+            if ( !(b->size & (PAGE_SIZE << i)) )
+                continue;
+            b->size -= PAGE_SIZE << i;
+            free_xenheap_pages((void *)b + b->size, i);
+            if ( i + 1 >= order )
+                break;
+        }
+    }
     else
         xmem_pool_free(p, xenpool);
 }
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Jan Beulich

Certain (boot time) allocations are relatively large (particularly when building with high NR_CPUS), but can also happen to be pretty far away from a power-of-two size. Utilize the page allocator's (other than Linux'es) capability of allowing to return space from higher-order allocations in smaller pieces to return the unused parts immediately.

Quoted 56 lines
xen/common/xmalloc_tlsf.c +24 -3
@@ -527,13 +527,21 @@ static void xmalloc_pool_put(void *p)
 static void *xmalloc_whole_pages(unsigned long size)
 {
     struct bhdr *b;
-    unsigned int pageorder = get_order_from_bytes(size + BHDR_OVERHEAD);
+    unsigned int i, pageorder = get_order_from_bytes(size + BHDR_OVERHEAD);
+    char *p;
 
     b = alloc_xenheap_pages(pageorder, 0);
     if ( b == NULL )
         return NULL;
 
-    b->size = (1 << (pageorder + PAGE_SHIFT));
+    b->size = PAGE_ALIGN(size + BHDR_OVERHEAD);
+    for ( p = (char *)b + b->size, i = 0; i < pageorder; ++i )
+        if ( (unsigned long)p & (PAGE_SIZE << i) )
+        {
+            free_xenheap_pages(p, i);
+            p += PAGE_SIZE << i;
+        }
+
     return (void *)b->ptr.buffer;
 }
 
@@ -611,7 +619,20 @@ void xfree(void *p)
     }
 
     if ( b->size >= PAGE_SIZE )
-        free_xenheap_pages((void *)b, get_order_from_bytes(b->size));
+    {
+        unsigned int i, order = get_order_from_bytes(b->size);
+
+        BUG_ON((unsigned long)b & ((PAGE_SIZE << order) - 1));
+        for ( i = 0; ; ++i )
+        {
+            if ( !(b->size & (PAGE_SIZE << i)) )
+                continue;
+            b->size -= PAGE_SIZE << i;
+            free_xenheap_pages((void *)b + b->size, i);
+            if ( i + 1 >= order )
+                break;
+        }
+    }
     else
         xmem_pool_free(p, xenpool);
 }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel