public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH V2 1/1] mm: introduce kvmalloc()/kvfree()
@ 2008-11-17 12:26 Lai Jiangshan
  2008-11-17 13:14 ` Johannes Weiner
  2008-11-18  6:57 ` [PATCH V2 1/1] mm: introduce kvmalloc()/kvfree() Pekka Enberg
  0 siblings, 2 replies; 12+ messages in thread
From: Lai Jiangshan @ 2008-11-17 12:26 UTC (permalink / raw)
  To: Andrew Morton
  Cc: David Miller, Dave Airlie, Paul Menage, kamezawa.hiroyu,
	Balbir Singh, Arjan van de Ven, Jan Kara, Jes Sorensen,
	KOSAKI Motohiro, dada1, Alexey Dobriyan, Jens Axboe,
	Linux Kernel Mailing List


For some reason, we use alternative malloc.
1) we want NUMA spreading and reliable allocation.
2) we use kmalloc(), but we want vmalloc() rarely when required
   memory is very large.
......

there are N copied of such codes in the kernel, it's pure code duplication.
this patch provides two helper functions do such alternative malloc/free,
and duplication codes will be cleanup.

changed from v1:

1) name them kvmalloc()/kvfree() instead of simple_malloc()/simple_free()
2) implement them in mm/util.c
   Since vmalloc()/vfree() is potentially a very expensive operation,
   we should make kvmalloc()/kvfree() uninlined. No need to try to
   save 3 or 4 cpu cycles. This will save icache at least.
3) add gfp_t flags parameter.

changelog of v1:

some subsystem needs vmalloc() when required memory is large.
but current kernel has not APIs for this requirement.
this patch introduces simple_malloc() and simple_free().

Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com>
---
diff --git a/include/linux/mm.h b/include/linux/mm.h
index ffee2f7..ff89388 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -278,6 +278,9 @@ static inline int is_vmalloc_addr(const void *x)
 #endif
 }
 
+void *kvmalloc(unsigned long size, gfp_t flags);
+void kvfree(void *ptr);
+
 static inline struct page *compound_head(struct page *page)
 {
 	if (unlikely(PageTail(page)))
diff --git a/mm/util.c b/mm/util.c
index cb00b74..66d1a12 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -5,6 +5,8 @@
 #include <linux/err.h>
 #include <linux/sched.h>
 #include <asm/uaccess.h>
+#include <linux/interrupt.h>
+#include <linux/vmalloc.h>
 
 /**
  * kstrdup - allocate space for and copy an existing string
@@ -163,6 +165,38 @@ char *strndup_user(const char __user *s, long n)
 }
 EXPORT_SYMBOL(strndup_user);
 
+/**
+ * kvmalloc - allocate memory by kmalloc() or vmalloc()
+ *
+ * if @size <= PAGE_SIZE, memory is allocated by kmalloc(),
+ * otherwise by vmalloc().
+ *
+ * NOTICE: Generally, you should use kmalloc() or vmalloc() directly.
+ * kvmalloc() is provided for some special condition.
+ */
+void *kvmalloc(unsigned long size, gfp_t flags)
+{
+	if (size <= PAGE_SIZE)
+		return kmalloc(size, flags & ~__GFP_HIGH);
+	else
+		return __vmalloc(size, flags | __GFP_HIGHMEM, PAGE_KERNEL);
+}
+EXPORT_SYMBOL(kvmalloc);
+
+/**
+ * kvfree - free the memory by kfree(), or vfree() if it is vmalloc addr
+ */
+void kvfree(void *ptr)
+{
+	BUG_ON(in_interrupt());
+
+	if (is_vmalloc_addr(ptr))
+		vfree(ptr);
+	else
+		kfree(ptr);
+}
+EXPORT_SYMBOL(kvfree);
+
 #ifndef HAVE_ARCH_PICK_MMAP_LAYOUT
 void arch_pick_mmap_layout(struct mm_struct *mm)
 {



^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2008-11-18 12:06 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-17 12:26 [PATCH V2 1/1] mm: introduce kvmalloc()/kvfree() Lai Jiangshan
2008-11-17 13:14 ` Johannes Weiner
2008-11-18  0:28   ` Lai Jiangshan
2008-11-18  8:50   ` [PATCH V2 0/4] use kvmalloc()/kvfree() the second part, about RCU Lai Jiangshan
2008-11-18  8:51   ` [PATCH V2 1/4] vmalloc: introduce vfree_atomic() Lai Jiangshan
2008-11-18  9:19     ` Nick Piggin
2008-11-18 11:38       ` Lai Jiangshan
2008-11-18 12:05         ` Nick Piggin
2008-11-18  8:51   ` [PATCH V2 2/4] mm: introduce kvfree_atomic() Lai Jiangshan
2008-11-18  8:51   ` [PATCH V2 3/4] files: use kvmalloc()/kvfree()/kvfree_atomic() Lai Jiangshan
2008-11-18  8:51   ` [PATCH V2 4/4] sysipc: use kvmalloc()/kvfree_atomic() Lai Jiangshan
2008-11-18  6:57 ` [PATCH V2 1/1] mm: introduce kvmalloc()/kvfree() Pekka Enberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox