Re: Difference between kmalloc and __get_free_pages

From: vishpat (vishpat_at_gmail.com)
Date: 07/09/04


Date: 9 Jul 2004 09:00:48 -0700

Nikhil

I am sorry but I can't post the code until it has been officially
released. However check the website
http://public.lanl.gov/radiant/software/magnet.html
where the patch will be officially released in about a month

- Vishal

Kasper Dupont <remove.invalid@nospam.lir.dk.invalid> wrote in message news:<40EAF741.9822423A@nospam.lir.dk.invalid>...
> vishpat wrote:
> >
> > Hello World
> >
> > I know that the __get_free_pages is used to allocate KERNEL memory in
> > terms of PAGES while kmalloc is used to allocate arbitrary sized
> > CONTINOUS KERNEL memory.
>
> Actually kmalloc use gfp to get the memory. The most
> important advantage of kmalloc is, that it can fit
> multiple small allocations into a single page.
>
> Calling gfp directly will allow you to use different
> options, for example to allocate high memory, which
> is not possible with kmalloc.
>
> >
> > However I was facing a problem when I was using "kmalloc" which got
> > solved when I switched over to "__get_free_pages". Even though I am
> > done with the problem I am curious about what was I doing wrong while
> > using "kmalloc". Perhaps some one can catch the flaw and let me know.
> >
> > I have written a CHAR device driver which I use to export some
> > information from the kernel to the user space programs. Initially I
> > was allocating FIVE pages of kernel memory using "kmalloc"
>
> kmalloc will round up the size you request to one of
> the supported sizes. You can see the sizes in
> /proc/slabinfo. An unmodified kernel will support
> only sizes that are a power of two. That means you
> have actually allocated eight pages.
>
> > and then
> > locking these pages using the function "SetPageReserved". I then
> > memory mapped the memory in the user space. However SOMETIMES whenever
> > I tried reading data from the kernel my machine hung. However this
> > problem got solved when I switched over to "__get_free_pages" and then
> > could easily read the required data from the kernel.
>
> Well, it certainly sounds like a case where kmalloc
> should not be used. When you allocate using kmalloc
> you really don't own the pages, kmalloc owns the
> pages, you are just allowed to use the memory. I'm
> not even sure you are guaranteed that the allocation
> will be page aligned (though I know that is the case
> with the current kmalloc implementation).
>
> I'm not sure exactly what happened, but you may have
> different parts of the kernel accessing the page
> struct each thinking they own it.
>
> Using gfp directly you cannot allocate five pages,
> what you specify is the power, which means 2 is 4
> pages and 3 is 8 pages. If you actually specified
> 5 as the argument you were in fact requesting 32
> pages. If you happen to write beyond the 8 allocated
> pages, the extra pages could explain why it works
> with gfp and not kmalloc. This is of course based on
> a lot of assumptions I can't really know anything
> about without seeing the code.



Relevant Pages

  • Re: Difference between kmalloc and __get_free_pages
    ... > terms of PAGES while kmalloc is used to allocate arbitrary sized ... Actually kmalloc use gfp to get the memory. ... > information from the kernel to the user space programs. ...
    (comp.os.linux.development.system)
  • Re: Sharing memory between kernelspace and userspace
    ... deallocate, on a totally dynamic basis, userspace ... Let userspace allocate shared memory visible to multiple ... and pass that into the kernel for it to write to. ...
    (Linux-Kernel)
  • [CFT] kmem_cache_alloc_node
    ... moves the cpu local structures within slab onto the right node. ... The cache to allocate from. ... * @flags: See kmalloc(). ... And it will allocate memory on the given node, ...
    (Linux-Kernel)
  • [PATCH 1/2] x86: reinstate numa remap for SPARSEMEM on x86 NUMA systems
    ... Recent kernels have been panic'ing trying to allocate memory early in boot, ... at kernel virtual address 0. ...
    (Linux-Kernel)
  • Re: kernel memory
    ... Kernel virtual address space is used for a number of things. ... Pat of this are the memory pools, from which you can allocate memory for your drivers. ... You can allocate physical memory using a DDI like MmAllocatePagesForMdl and then map those into the system address space. ... That's probably the easiest way to explain why you cannot allocate 1GB of non-paged pool for yourself:) ...
    (microsoft.public.development.device.drivers)