Re: Re: about kmalloc and kmem_cache_alloc

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> Can you also explain when to use alloc_pages ? I wanna know in what case to use it in drivers.

alloc_pages is zone allocator which implements buddy allocation algorithm. It is the basic allocation API. Following is the dependency call sequence:

kmalloc() --> kmem_cahce_alloc() --> alloc_pages()

kmalloc/kmem_cache_alloc have specialized use case where you want slab cache features like reuse of a slab object after freeing it. Its generally good for frequently allocated kernel data structures.

alloc_pages is more generic API which allocates multiple pages in order of 2. When your requirements are to allocate a pool of pages on which you may not need slab features, it is an overhead to allocate pages with kmalloc. You can more efficiently manage your pool with alloc_pages(). As an example, page cache allocation for holding files data in cahce is done with alloc_pages and family. Network drivers may allocate their data buffers using alloc_pages. Mostly you would find its use-case for allocating data buffers and not kernel structures.

Rajat

On Sun, Nov 7, 2010 at 5:43 PM, kernel.niko <kernel.niko@xxxxxxxxx> wrote:
thanks for all your guys, it's very clear . It's helpful
 
Can you also explain when to use alloc_pages ? I wanna know in what case to
 
use it in drivers. thanks again .
 
> as i know,kmalloc is based on kmem_cache_alloc ,so what's the different?

kmalloc uses the generic slab caches available to any kernel code. so your module will share slab cache with other components in kernel, but if you specifically want a better slab cache management dedicated to your module only, that too for a specific type of objects, use the lower function i.e. kmem_cache_alloc, it will allocate objects from a dedicated slab cache for your module objects only, you must create the slab cache by calling kmem_cache_create before allocating any object. kmem_cache_create takes sizeof your object you want to create slab of, a name which appears in /proc/slabinfo and flags to govern behavior of your slab cache.

Rajat

On Fri, Nov 5, 2010 at 1:22 PM, Venkatram Tummala <venkatram867@xxxxxxxxx> wrote:
On Thu, Nov 4, 2010 at 8:03 PM, kernel.niko <kernel.niko@xxxxxxxxx> wrote:
> hi,everyone,
> I'm wandering how to use kmalloc and kmem_cache_alloc.
> as i know,kmalloc is based on kmem_cache_alloc ,so what's the different?

kmalloc should be used when you want to allocate byte sized memory as
opposed to allocating memory in units of pages, in which case you
should use alloc_pages(..).

kmem_cache_alloc is a part of the slab cache interface. This is used
for reusing objects in the kernel. Lets take mm_struct objects as an
example. On a live system, processes come & go. Every process needs a
mm_struct object. And when a process dies, you can throw away the
object. Instead of creating & throwing away objects, slab cache
maintains a cache of objects. When you free an object, instead of
deallocating it, you give it back to the cache. Next time, if  you
want to create a new object, slab cache gives you one object from the
slab cache. kmem_cache_alloc is one of the interface functions to use
the slab cache in the kernel. You might want to read about slab caches
in detail if you need more information.

Venkatram Tummala
> when to use kmalloc? and
> in what occasion,kmem_cache_alloc should be used?
> thanks.
>
> 2010-11-05
> ________________________________
> kernel.niko

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ




[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux