Re: about kmalloc and kmem_cache_alloc

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

 



> Unless you are creating your own subsystem(or complex driver) and will
> be responsible for allocating a lot of one specific data structure
> there is no reason to create your own cache.  Even in the driver case
> you will often know ahead of time the number of data structures to
> preallocate and just use those in a ring buffer or similar.

Right, kmeme_cache_alloc should be avoided whenever possible. One good example where kmem_cache_alloc is useful is the regular practice which almost every filesystem in Linux follows - inode slab cache. There are generic inode caches although for VFS inode, but filesystems may want to extend the VFS inode structure with some of their own fields, by making a wrapper structure over VFS inode, so they maintain there own slab cache for these internal structures using kmem_cache_(create/alloc). Inode is a right candidate for slab cache because there can be as many inodes as number of files, directories, symbolic links, special device file fetched into the memory.

Rajat

On Sun, Nov 7, 2010 at 11:43 AM, John Mahoney <jmahoney@xxxxxxxx> wrote:
On Sat, Nov 6, 2010 at 2:09 AM, Rajat Sharma <fs.rajat@xxxxxxxxx> wrote:
>> 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

My two cents to add to the fine comment above, which re probably not
even worth that much, but your getting for free :)

If you are asking this question I would probably guess you should just
stick to kmalloc, that sounds rude, but I say it with all respect.

Unless you are creating your own subsystem(or complex driver) and will
be responsible for allocating a lot of one specific data structure
there is no reason to create your own cache.  Even in the driver case
you will often know ahead of time the number of data structures to
preallocate and just use those in a ring buffer or similar.

--
john


[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