Re: [RFC] mm: generic adaptive large memory allocation APIs

From: Changli Gao
Date: Wed May 05 2010 - 20:38:22 EST


On Thu, May 6, 2010 at 8:30 AM, Changli Gao <xiaosuo@xxxxxxxxx> wrote:
> kvmalloc() will try to allocate physically contiguous memory first, and try
> vmalloc to allocate virtually contiguous memory when the former allocation
> fails.
>
> kvfree() is used to free the memory allocated by kvmalloc(). It can't be used
> in atomic context. If the callers are in atomic contex, they can use
> kvfree_inatomic() instead.
>
> There is much duplicate code to do such things in kernel, so I generate the
> above APIs.
>
> Thank Eric Dumazet for the "kv" prefix. :)
>
> #include <linux/kernel.h>
> #include <linux/module.h>
> #include <linux/mm.h>
> #include <linux/init.h>
> #include <linux/slab.h>
> #include <linux/vmalloc.h>
> #include <linux/interrupt.h>
>
> void *kvmalloc(size_t size)
> {
> Â Â Â Âvoid *ptr;
>
> Â Â Â Âif (size < PAGE_SIZE)
> Â Â Â Â Â Â Â Âreturn kmalloc(PAGE_SIZE, GFP_KERNEL);
> Â Â Â Âptr = alloc_pages_exact(size, GFP_KERNEL | __GFP_NOWARN);
> Â Â Â Âif (ptr != NULL)
> Â Â Â Â Â Â Â Âreturn ptr;
>
> Â Â Â Âreturn vmalloc(size);
> }
> EXPORT_SYMBOL(kvmalloc);
>
> void kvfree(void *ptr, size_t size)
> {
> Â Â Â Âif (size < PAGE_SIZE)
> Â Â Â Â Â Â Â Âkfree(ptr);
> Â Â Â Âelse if (is_vmalloc_addr(ptr))
> Â Â Â Â Â Â Â Âvfree(ptr);
> Â Â Â Âelse
> Â Â Â Â Â Â Â Âfree_pages_exact(ptr, size);
> }
> EXPORT_SYMBOL(kvfree);
>
> struct kvfree_work_struct {
>    Âstruct work_struct   Âwork;
>    Âvoid          Â*head;
>    Âvoid          Â**ptail;
> };
>
> DEFINE_PER_CPU(struct kvfree_work_struct, kvfree_work_struct);
>
> static void kvfree_work(struct work_struct *_work)
> {
> Â Â Â Âstruct kvfree_work_struct *work;
> Â Â Â Âvoid *head, *tmp;
>
> Â Â Â Âwork = container_of(_work, struct kvfree_work_struct, work);
> Â Â Â Âlocal_bh_disable();
> Â Â Â Âhead = work->head;
> Â Â Â Âwork->head = NULL;
> Â Â Â Âwork->ptail = &work->head;
> Â Â Â Âlocal_bh_enable();

local_bh_disable should be local_irq_disable(), and local_bh_enable()
should be local_irq_enable().

>
> Â Â Â Âwhile (head) {
> Â Â Â Â Â Â Â Âtmp = head;
> Â Â Â Â Â Â Â Âhead = *(void **)head;
> Â Â Â Â Â Â Â Âvfree(tmp);
> Â Â Â Â}
> }
>
> void kvfree_inatomic(void *ptr, size_t size)
> {
> Â Â Â Âif (size < PAGE_SIZE) {
> Â Â Â Â Â Â Â Âkfree(ptr);
> Â Â Â Â} else if (is_vmalloc_addr(ptr)) {
> Â Â Â Â Â Â Â Âstruct kvfree_work_struct *work;
>
> Â Â Â Â Â Â Â Â*(void **)ptr = NULL;
> Â Â Â Â Â Â Â Âlocal_irq_disable();
> Â Â Â Â Â Â Â Âwork = this_cpu_ptr(&kvfree_work_struct);
> Â Â Â Â Â Â Â Â*(work->ptail) = ptr;
> Â Â Â Â Â Â Â Âwork->ptail = (void**)ptr;
> Â Â Â Â Â Â Â Âschedule_work(&work->work);
> Â Â Â Â Â Â Â Âlocal_irq_enable();
> Â Â Â Â} else {
> Â Â Â Â Â Â Â Âfree_pages_exact(ptr, size);
> Â Â Â Â}
> }
> EXPORT_SYMBOL(kvfree_inatomic);
>
> static int kvfree_work_struct_init(void)
> {
> Â Â Â Âint cpu;
> Â Â Â Âstruct kvfree_work_struct *work;
>
> Â Â Â Âfor_each_possible_cpu(cpu) {
> Â Â Â Â Â Â Â Âwork = per_cpu_ptr(&kvfree_work_struct, cpu);
> Â Â Â Â Â Â Â ÂINIT_WORK(&work->work, kvfree_work);
> Â Â Â Â Â Â Â Âwork->head = NULL;
> Â Â Â Â Â Â Â Âwork->ptail = &work->head;
> Â Â Â Â}
>
> Â Â Â Âreturn 0;
> }
> //pure_initcall(kvfree_work_struct_init);
>
> //--------------------
> // for testing
> static int test_init(void)
> {
> Â Â Â Âint size;
> Â Â Â Âvoid *ptr;
>
> Â Â Â Âkvfree_work_struct_init();
> Â Â Â Âfor (size = 1; size < (1<<30); size <<= 1) {
> Â Â Â Â Â Â Â Âptr = kvmalloc(size);
> Â Â Â Â Â Â Â Âif (is_vmalloc_addr(ptr)) {
> Â Â Â Â Â Â Â Â Â Â Â Âprintk("%d\n", size);
> Â Â Â Â Â Â Â Â Â Â Â Âbreak;
> Â Â Â Â Â Â Â Â}
> Â Â Â Â Â Â Â Âkvfree(ptr, size);
> Â Â Â Â}
>
> Â Â Â Âreturn 0;
> }
> module_init(test_init);
>
> static void test_exit(void)
> {
> Â Â Â Âint cpu;
> Â Â Â Âstruct kvfree_work_struct *work;
>
> Â Â Â Âfor_each_possible_cpu(cpu) {
> Â Â Â Â Â Â Â Âwork = per_cpu_ptr(&kvfree_work_struct, cpu);
> Â Â Â Â Â Â Â Âcancel_work_sync(&work->work);
> Â Â Â Â}
> }
> module_exit(test_exit);
>
> MODULE_LICENSE("GPL");
>



--
Regardsï
Changli Gao(xiaosuo@xxxxxxxxx)
¢éì®&Þ~º&¶¬–+-±éÝ¥Šw®žË±Êâmébžìdz¹Þ)í…æèw*jg¬±¨¶‰šŽŠÝj/êäz¹ÞŠà2ŠÞ¨è­Ú&¢)ß«a¶Úþø®G«éh®æj:+v‰¨Šwè†Ù>Wš±êÞiÛaxPjØm¶Ÿÿà -»+ƒùdš_