linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Changli Gao <xiaosuo@gmail.com>
To: akpm@linux-foundation.org
Cc: Eric Dumazet <eric.dumazet@gmail.com>,
	Jiri Slaby <jslaby@suse.cz>, Changli Gao <xiaosuo@gmail.com>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>,
	Alexey Dobriyan <adobriyan@gmail.com>,
	Ingo Molnar <mingo@elte.hu>,
	Peter Zijlstra <peterz@infradead.org>,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Avi Kivity <avi@redhat.com>,
	Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
Subject: Re: [RFC] mm: generic adaptive large memory allocation APIs
Date: Fri, 7 May 2010 12:32:30 +0800	[thread overview]
Message-ID: <t2p412e6f7f1005062132lbcaf5192ua6ead4872465a766@mail.gmail.com> (raw)
In-Reply-To: <1273105838-4441-1-git-send-email-xiaosuo@gmail.com>

On Thu, May 6, 2010 at 8:30 AM, Changli Gao <xiaosuo@gmail.com> wrote:
> 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);
> }

I found a way to eliminate the size argument of kvfree() and
kvfree_inatomic(). We can check where the page_head is slab or
compound, as pages owned by slab  subsystem always have Slab flag set
or are compound.

                page = virt_to_head_page(ptr);
                if (PageSlab(page) || PageCompound(page))
                        kfree(ptr);
                else
                        free_pages_exact(ptr, page->private);

And we can save the size argument in page->private after
alloc_pages_eact() returns.

        ptr = alloc_pages_exact(size, GFP_KERNEL | __GFP_NOWARN);
        if (ptr != NULL) {
                virt_to_head_page(ptr)->private = size;
                return ptr;
        }

here is the test code:

#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>

#define kmalloc(size, gfp) ({ \
        void *ptr = kmalloc(size, gfp); \
        if (ptr != NULL) \
                printk("kmalloc: %p(%u)\n", ptr, size); \
        ptr; })

#define alloc_pages_exact(size, gfp) ({ \
        void *ptr = alloc_pages_exact(size, gfp); \
        if (ptr != NULL) \
                printk("alloc_pages_exact: %p(%u)\n", ptr, size); \
        ptr; })

#define vmalloc(size) ({ \
        void *ptr = vmalloc(size); \
        if (ptr != NULL) \
                printk("vmalloc: %p(%u)\n", ptr, size); \
        ptr; })

#define kfree(ptr) do { printk("kfree: %p\n", ptr); kfree(ptr); } while (0)
#define vfree(ptr) do { printk("vfree: %p\n", ptr); vfree(ptr); } while (0)
#define free_pages_exact(ptr, size) do { \
                printk("free_pages_exact: %p(%u)\n", ptr, size); \
                free_pages_exact(ptr, size); \
        } while (0)

void *kvmalloc(size_t size)
{
        void *ptr;

        if (size < PAGE_SIZE)
                return kmalloc(size, GFP_KERNEL);
        ptr = alloc_pages_exact(size, GFP_KERNEL | __GFP_NOWARN);
        if (ptr != NULL) {
                virt_to_head_page(ptr)->private = size;
                return ptr;
        }

        return vmalloc(size);
}
EXPORT_SYMBOL(kvmalloc);

static void kvfree_work(struct work_struct *work)
{
        vfree(work);
}

static void __kvfree(void *ptr, bool inatomic)
{
        if (unlikely(ZERO_OR_NULL_PTR(ptr)))
                return;
        if (is_vmalloc_addr(ptr)) {
                if (inatomic) {
                        struct work_struct *work;

                        work = ptr;
                        BUILD_BUG_ON(sizeof(struct work_struct) > PAGE_SIZE);
                        INIT_WORK(work, kvfree_work);
                        schedule_work(work);
                } else {
                        vfree(ptr);
                }
        } else {
                struct page *page;

                page = virt_to_head_page(ptr);
                if (PageSlab(page) || PageCompound(page))
                        kfree(ptr);
                else
                        free_pages_exact(ptr, page->private);
        }
}

void kvfree(void *ptr)
{
        __kvfree(ptr, false);
}
EXPORT_SYMBOL(kvfree);

void kvfree_inatomic(void *ptr)
{
        __kvfree(ptr, true);
}
EXPORT_SYMBOL(kvfree_inatomic);

//--------------------
// for testing
static int test_init(void)
{
        int size;
        void *ptr;

        for (size = 1; size < (1<<30); size <<= 1) {
                ptr = kvmalloc(size);
                if (ptr == NULL)
                        return -1;
                if (is_vmalloc_addr(ptr)) {
                        kvfree(ptr);
                        break;
                }
                kvfree(ptr);
        }
        ptr = kvmalloc(size);
        if (ptr == NULL)
                return -1;
        kvfree_inatomic(ptr);

        return 0;
}
module_init(test_init);

static void test_exit(void)
{
}
module_exit(test_exit);

MODULE_LICENSE("GPL");

-- 
Regards,
Changli Gao(xiaosuo@gmail.com)

  parent reply	other threads:[~2010-05-07  4:32 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-06  0:30 [RFC] mm: generic adaptive large memory allocation APIs Changli Gao
2010-05-06  0:37 ` Changli Gao
2010-05-06  1:25 ` Changli Gao
2010-05-06  3:12 ` Tetsuo Handa
2010-05-06  3:22   ` Changli Gao
2010-05-06 15:35 ` Jamie Lokier
2010-05-07  4:32 ` Changli Gao [this message]
2010-05-07 12:42   ` Tetsuo Handa
2010-05-07 12:52     ` Peter Zijlstra
2010-05-13  4:45 ` KOSAKI Motohiro
2010-05-13  8:43   ` Jiri Slaby
2010-05-13  9:05     ` KOSAKI Motohiro
2010-05-13  9:19       ` Jiri Slaby
2010-05-13  9:40         ` KOSAKI Motohiro
2010-05-13 10:16           ` Jiri Slaby
2010-05-13 10:43             ` KOSAKI Motohiro

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=t2p412e6f7f1005062132lbcaf5192ua6ead4872465a766@mail.gmail.com \
    --to=xiaosuo@gmail.com \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=avi@redhat.com \
    --cc=eric.dumazet@gmail.com \
    --cc=jslaby@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=penguin-kernel@i-love.sakura.ne.jp \
    --cc=peterz@infradead.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).