Skip to contentSkip to conversation

UML - free() wrapper should call libc free

PATCH In review Jeff Dike 2 participants 3 messages May 1, 2007 – May 2, 2007 User-mode Linux (UML)

lore
Share & tools
lore

Jeff Dike

author
#0 lore raw

PATCH 4/6 UML - free() wrapper should call libc free

The libc free wrapper wasn't correctly detecting buffers obtained with malloc(). This is now done by seeing if the page was reserved. This is the case for memory which is left aside for libc and isn't given to the page allocator. If we free a pointer in a reserved page, it is given to free() rather than kfree().

--

 arch/um/include/user.h   |    1 +
 arch/um/kernel/um_arch.c |    7 +++++++
 arch/um/os-Linux/main.c  |    6 +++++-
 3 files changed, 13 insertions(+), 1 deletion(-)
arch/um/include/user.h +1 -0
Index: linux-2.6.21-mm/arch/um/include/user.h
===================================================================
@@ -27,5 +27,6 @@ extern int in_aton(char *str);
 extern int open_gdb_chan(void);
 extern size_t strlcpy(char *, const char *, size_t);
 extern size_t strlcat(char *, const char *, size_t);
+extern int reserved_address(void *addr);
 
 #endif
arch/um/kernel/um_arch.c +7 -0
Index: linux-2.6.21-mm/arch/um/kernel/um_arch.c
===================================================================
@@ -500,6 +500,13 @@ void __init check_bugs(void)
 	os_check_bugs();
 }
 
+int reserved_address(void *addr)
+{
+	struct page *page = virt_to_page(addr);
+
+	return(PageReserved(page));
+}
+
 void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
 {
 }
arch/um/os-Linux/main.c +5 -1
Index: linux-2.6.21-mm/arch/um/os-Linux/main.c
===================================================================
@@ -266,6 +266,8 @@ void __wrap_free(void *ptr)
 	/* We need to know how the allocation happened, so it can be correctly
 	 * freed.  This is done by seeing what region of memory the pointer is
 	 * in -
+	 *	in a reserved page - free, assume the pointer was
+	 *	    acquired with malloc, since it couldn't have been kmalloced.
 	 * 	physical memory - kmalloc/kfree
 	 *	kernel virtual memory - vmalloc/vfree
 	 * 	anywhere else - malloc/free
@@ -281,7 +283,9 @@ void __wrap_free(void *ptr)
 	 * there is a possibility for memory leaks.
 	 */
 
-	if((addr >= uml_physmem) && (addr < high_physmem)){
+	if(kmalloc_ok && reserved_address(ptr))
+		__real_free(ptr);
+	else if((addr >= uml_physmem) && (addr < high_physmem)){
 		if(CAN_KMALLOC())
 			kfree(ptr);
 	}

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
User-mode-linux-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel

#1 lore raw
Quoted 5 lines · Jeff Dike

The libc free wrapper wasn't correctly detecting buffers obtained with malloc(). This is now done by seeing if the page was reserved. This is the case for memory which is left aside for libc and isn't given to the page allocator. If we free a pointer in a reserved page, it is given to free() rather than kfree().

I want to get rid of PG_reserved eventually. Is it possible to use PG_arch_1 for this?

Quoted 67 lines

--

 arch/um/include/user.h   |    1 +
 arch/um/kernel/um_arch.c |    7 +++++++
 arch/um/os-Linux/main.c  |    6 +++++-
 3 files changed, 13 insertions(+), 1 deletion(-)

Index: linux-2.6.21-mm/arch/um/include/user.h
===================================================================

arch/um/include/user.h +1 -0
@@ -27,5 +27,6 @@ extern int in_aton(char *str);
 extern int open_gdb_chan(void);
 extern size_t strlcpy(char *, const char *, size_t);
 extern size_t strlcat(char *, const char *, size_t);
+extern int reserved_address(void *addr);
 
 #endif

Index: linux-2.6.21-mm/arch/um/kernel/um_arch.c
===================================================================

arch/um/kernel/um_arch.c +7 -0
@@ -500,6 +500,13 @@ void __init check_bugs(void)
 	os_check_bugs();
 }
 
+int reserved_address(void *addr)
+{
+	struct page *page = virt_to_page(addr);
+
+	return(PageReserved(page));
+}
+
 void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
 {
 }

Index: linux-2.6.21-mm/arch/um/os-Linux/main.c
===================================================================

arch/um/os-Linux/main.c +5 -2
@@ -266,6 +266,8 @@ void __wrap_free(void *ptr)
 	/* We need to know how the allocation happened, so it can be correctly
 	 * freed.  This is done by seeing what region of memory the pointer is
 	 * in -
+	 *	in a reserved page - free, assume the pointer was
+	 *	    acquired with malloc, since it couldn't have been kmalloced.
 	 * 	physical memory - kmalloc/kfree
 	 *	kernel virtual memory - vmalloc/vfree
 	 * 	anywhere else - malloc/free
@@ -281,7 +283,9 @@ void __wrap_free(void *ptr)
 	 * there is a possibility for memory leaks.
 	 */
 
-	if((addr >= uml_physmem) && (addr < high_physmem)){
+	if(kmalloc_ok && reserved_address(ptr))
+		__real_free(ptr);
+	else if((addr >= uml_physmem) && (addr < high_physmem)){
 		if(CAN_KMALLOC())
 			kfree(ptr);
 	}
-

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

Signature · 1 line
SUSE Labs, Novell Inc.
author
#2 lore raw
Quoted 2 lines · Nick Piggin

I want to get rid of PG_reserved eventually. Is it possible to use PG_arch_1 for this?

Yup.

Andrew - Feel free to drop this or just sit on it until I send a replacement. Whichever is easier.

Jeff

Signature · 11 lines
Work email - jdike at linux dot intel dot com

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
User-mode-linux-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel