Skip to contentSkip to conversation

add tlsf option for xmalloc

RFC PATCH In review Dan Magenheimer 2 participants 4 messages Oct 15, 2008

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 add tlsf option for xmalloc

This patch is a first step for replacing the current xmalloc engine with a tlsf-based engine. The first patch simply adds Nitin Gupta's current tlsf-kmod files to Xen and the second modifies them (and xmalloc) to adapt them to work with Xen.

The tlsf engine can be turned on (and then overrides the xmalloc engine) with a Xen boot option of "tlsf". This defaults to off, so the patch should have no effect without the boot option.

Note that this is compile-tested but not run tested yet. I thought I'd get feedback on the approach before I get much further.

Thanks,
Dan

docs/misc/tlsf.CREDITS new file +39 -0
diff -r 8c3144b1ff63 docs/misc/tlsf.CREDITS
@@ -0,0 +1,39 @@
+
+Code contributions:
+
+(Feb 17 2008) Nitin Gupta <[email protected]>
+ - Ported to Linux kernel.
+ - A "pool" is now a collection of contiguous memory regions.
+ - Pool can dynamically grow and shrink.
+ - Above chages required changes to TLSF interface for creating
+   new memory pool.
+ - Removed realloc(): No immediate requirement.
+ - Other misc changes.
+
+(Jul 28 2007) Herman ten Brugge <[email protected]>:
+
+ - Add 64 bit support. It now runs on x86_64 and solaris64.
+ - I also tested this on vxworks/32and solaris/32 and i386/32 processors.
+ - Remove assembly code. I could not measure any performance difference
+   on my core2 processor. This also makes the code more portable.
+ - Moved defines/typedefs from tlsf.h to tlsf.c
+ - Changed MIN_BLOCK_SIZE to sizeof (free_ptr_t) and BHDR_OVERHEAD to
+   (sizeof (bhdr_t) - MIN_BLOCK_SIZE). This does not change the fact
+    that the minumum size is still sizeof
+   (bhdr_t).
+ - Changed all C++ comment style to C style. (// -> /.
+ - Used ls_bit instead of ffs and ms_bit instead of fls. I did this to
+   avoid confusion with the standard ffs function which returns
+   different values.
+ - Created set_bit/clear_bit fuctions because they are not present
+   on x86_64.
+ - Added locking support + extra file target.h to show how to use it.
+ - Added get_used_size function
+ - Added rtl_realloc and rtl_calloc function
+ - Implemented realloc clever support.
+ - Added some test code in the example directory.
+
+
+(Oct 23 2006) Adam Scislowicz:
+
+ - Support for ARMv5 implemented
xen/common/tlsf.c new file +677 -0

677 lines not on this page · raw message

xen/include/xen/tlsf.h new file +93 -0

93 lines not on this page · raw message

xen/include/xen/tlsf_int.h new file +146 -0

146 lines not on this page · raw message

xen/common/Makefile +1 -0
diff -r 93b0bba73289 xen/common/Makefile
@@ -26,6 +26,7 @@ obj-y += version.o
 obj-y += version.o
 obj-y += vsprintf.o
 obj-y += xmalloc.o
+obj-y += tlsf.o
 obj-y += rcupdate.o
 
 obj-$(perfc)       += perfc.o
xen/common/tlsf.c +84 -199

379 lines not on this page · raw message

xen/common/xmalloc.c +10 -0
diff -r 93b0bba73289 xen/common/xmalloc.c
@@ -35,6 +35,10 @@
 #include <xen/prefetch.h>
 #include <xen/irq.h>
 #include <xen/smp.h>
+
+static bool_t use_tlsf = 0;
+boolean_param("tlsf",use_tlsf);
+#include <xen/tlsf.h>
 
 /*
  * XMALLOC_DEBUG:
@@ -180,6 +184,9 @@ void *_xmalloc(size_t size, size_t align
     /* We currently always return cacheline aligned. */
     BUG_ON(align > SMP_CACHE_BYTES);
 
+    if ( use_tlsf )
+        return tlsf_xenheap_malloc(size, align);
+
 #if XMALLOC_DEBUG
     /* Add room for canaries at start and end of data block. */
     size += 2 * SMP_CACHE_BYTES;
@@ -218,6 +225,9 @@ void xfree(void *p)
 
     if ( p == NULL )
         return;
+
+    if ( use_tlsf )
+        return tlsf_xenheap_free(p);
 
     hdr = header_from_data(p);
 
xen/include/xen/tlsf.h +4 -0
diff -r 93b0bba73289 xen/include/xen/tlsf.h
@@ -90,4 +90,8 @@ extern size_t tlsf_get_used_size(void *m
  */
 extern size_t tlsf_get_total_size(void *mem_pool);
 
+/* xenheap xmalloc interface */
+extern void *tlsf_xenheap_malloc(size_t size, size_t align);
+extern void tlsf_xenheap_free(void *p);
+
 #endif
xen/include/xen/tlsf_int.h +0 -25
diff -r 93b0bba73289 xen/include/xen/tlsf_int.h
@@ -66,10 +66,6 @@
 #define PREV_FREE	(0x2)
 #define PREV_USED	(0x0)
 
-#if defined(CONFIG_TLSF_DEBUG)
-#define MAX_RETRY_EXPAND	10
-#endif
-
 struct free_ptr {
 	struct bhdr *prev;
 	struct bhdr *next;
@@ -117,27 +113,6 @@ struct pool {
 
 	struct list_head list;
 
-#if defined(CONFIG_TLSF_STATS)
-	/* Extra stats */
-	size_t peak_used;
-	size_t peak_total;
-	size_t peak_extra;	/* MAX(Total - Used) */
-	size_t count_alloc;
-	size_t count_free;
-	size_t count_region_alloc;
-	size_t count_region_free;
-	size_t count_failed_alloc;
-#endif
-
-#if defined(CONFIG_TLSF_DEBUG)
-	/*
-	 * Pool used size must be 0 when its destroyed.
-	 * When non-empty pool is destroyed, it suggests
-	 * memory leak. Such pools are marked invalid
-	 * and kept in pool list for later debugging.
-	 */
-	unsigned int valid;
-#endif
 	void *init_region;
 	char name[MAX_POOL_NAME_LEN];
 };

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

#1 lore raw
Quoted 3 lines · Dan Magenheimer

Note that this is compile-tested but not run tested yet. I thought I'd get feedback on the approach before I get much further.

The approach is fine as far as I'm concerned. It should just replace the existing xmalloc.c of course: no point in keeping the old one around. Therefore may as well put the whole implementation in xmalloc.c, or call it xmalloc_tlsf.c if you're really keen to keep the name of the algorithm in the file name (in which case xmalloc.c should be entirely deleted; or perhaps temporarily make it a build option which .c file gets built if really worried that the new xmalloc implementation may have subtle bugs and we'd like to be able to revert for testing).

Structure definitions and the like which are private to TLSF should live at the top of the .c file: I prefer that to the <xen/*_int.h> approach as it keeps private non-shared stuff all in one place.

-- Keir

#2 lore raw
Quoted 17 lines
Quoted 3 lines

Note that this is compile-tested but not run tested yet. I thought I'd get feedback on the approach before I get much further.

The approach is fine as far as I'm concerned. It should just replace the
existing xmalloc.c of course: no point in keeping the old one around. Therefore may as well put the whole implementation in xmalloc.c, or call it
xmalloc_tlsf.c if you're really keen to keep the name of the algorithm in
the file name (in which case xmalloc.c should be entirely deleted; or perhaps temporarily make it a build option which .c file gets built if really worried that the new xmalloc implementation may have subtle bugs and
we'd like to be able to revert for testing).

Quoted 5 lines

Structure definitions and the like which are private to TLSF should live at
the top of the .c file: I prefer that to the <xen/*_int.h> approach as it
keeps private non-shared stuff all in one place.

Sounds good.

You may note that the tlsf files borrowed from Nitin Gupta use Linux-style indentation/formatting. One of my reasons for not merging the tlsf allocation with xmalloc is I was too lazy to reformat ;-) Do I need to do that or can I leave it as is or do you have a tool that will auto-reformat it to meet the Xen style requirements?

Thanks,
Dan

P.S. Posted patch boots fine.

#3 lore raw
Quoted 6 lines · Dan Magenheimer
Quoted 5 lines

Structure definitions and the like which are private to TLSF should live at
the top of the .c file: I prefer that to the <xen/*_int.h> approach as it
keeps private non-shared stuff all in one place.

Quoted 7 lines

Sounds good.

You may note that the tlsf files borrowed from Nitin Gupta use Linux-style indentation/formatting. One of my reasons for not merging the tlsf allocation with xmalloc is I was too lazy to reformat ;-) Do I need to do that or can I leave it as is or do you have a tool that will auto-reformat it to meet the Xen style requirements?

If the patch is actually working fine then I can take it and do the necessary reformatting. It only takes a short while even to do it manually.

You need to provide a signed-off-by line, by the way.

-- Keir