John
John

Reputation: 3484

How to comprehend "You can use the slab cache allocator(i.e. kmem_cache_create or kmem_cache_create_usercopy) to allocate many identical objects"?

As per the documentation(https://www.kernel.org/doc/html/latest/core-api/memory-allocation.html), which says[emphasis mine]:

If you need to allocate many identical objects you can use the slab cache allocator. The cache should be set up with kmem_cache_create() or kmem_cache_create_usercopy() before it can be used. The second function should be used if a part of the cache might be copied to the userspace. After the cache is created kmem_cache_alloc() and its convenience wrappers can allocate memory from that cache.

What does "allocate many identical objects" mean? Why could not I use kmalloc,kvmalloc,vmalloc and etc under such circumstances?

Upvotes: 0

Views: 1068

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 65928

By "allocate many identical objects" they mean allocation of many objects of the same type.

Why could not I use kmalloc, kvmalloc, vmalloc, etc. under such circumstances?

You may use them, but special cache allocator (created by kmem_cache_create) have several advantages. Among them:

  1. Lesser memory consumption per object.

    When allocate objects from own cache allocator, the actual allocated size could be less than one allocated by kmalloc. This is because every cache allocator may allocate object only of a specific size.

    kmalloc allocate objects from the cache allocators too, but it uses allocator of predefined sizes. Say, kmalloc has allocators for 4K and 8K sizes. When it allocates object of size e.g. 5K, it uses 8K allocator. That is 3K of allocation space is just a waste.

  2. Localization of failures.

    If your own cache will be corrupted (e.g. by double-free), this corruption will affect only an allocation of your own objects. There is a high chance that other allocators remain functional.

  3. Easier debugging of failures.

    If Linux kernel reports about corruption of your own cache allocator, then you know where debugging should be started from: most likely it is the code which works with the given cache.

  4. Clever initialization on allocations.

    When allocate the object, you most likely want it to be initialized, that is set its field into initial values. Normally (when use kmalloc) you need to initialize object every time it is allocated.

    But if your object's fields have initial values at the time you free the object, you no longer need to initialize that object would it be allocated again. Cache allocator provides ability to omit initialization of already initialized object via its constructor (ctor parameter). If an object is allocated first time, the constructor is called automatically. If an object has been allocated before (and freed), the constructor is not called.

    E.g., ext4 filesystem uses constructorsLarge and complex filesystems uses constructors for allocateLinux kernel uses constructors e.g. for inodes

Of course, there would be a little benefit to create allocator when you need to allocate just 5 small objects: An allocator itself consumes a memory, and creation of the allocator consumes a time.

But if you need to allocate 100 objects, creating an allocator for them would be a good idea.

Upvotes: 2

Related Questions