0

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?

John
  • 2,963
  • 11
  • 33

1 Answers1

2

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.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • Thank you for the clarification. I understand it at a different level with help.Quote from your answer[emphasis mine]:" 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**." What about allocating an object of size e.g.100Bytes?Still needs 4K allocator? – John Jun 20 '20 at 12:28
  • 1
    Actually, 4K is not the minimal cache size for kmalloc allocators. Their sizes [starts with 32](https://elixir.bootlin.com/linux/v3.9.11/source/include/linux/kmalloc_sizes.h). So, for object of size 100 it will use 128 bytes allocator, which is not a big problem. But 4K and 8K are actually successive sizes for `kmalloc` allocators, so my example is a real one. – Tsyvarev Jun 20 '20 at 12:52
  • I have a deeper understanding of this question with your help. Thank you for your generous help. BTW, it's interesting to using the word "cache". You see, the name of the said function is `kmem_cache_create` and the aforementioned document also uses marco `CACHE(32)`, `CACHE(64)` and etc. Do you have any idea about it? – John Jun 20 '20 at 13:16