paint-brush
Understanding Kernel Memory Allocation using Buddy and Slab Systemsby@ishitajuneja
1,351 reads
1,351 reads

Understanding Kernel Memory Allocation using Buddy and Slab Systems

by Ishita JunejaMarch 10th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Buddy systems in OS and slab systems are two efficient techniques used to allocate free memory in the kernel processes. Buddy system somehow exists between dynamic and static programming. Slab system uses block sizes of 16, 32, 48, 80, and 80, 208 bytes. Each block of size 2k +2 is split into 2k and 3k block sizes.
featured image - Understanding Kernel Memory Allocation using Buddy and Slab Systems
Ishita Juneja HackerNoon profile picture


  • Do you know how to allocate kernel memory?
  • What would be the ideal strategy for allocating kernel memory?
  • How can you use the buddy system efficiently in OS?


If all these questions related to memory allocation are on your mind, then this blog post is an ideal read for you.


Buddy systems in OS and slab systems are two efficient techniques used to allocate free memory in the kernel processes. As Kernel is touted to offer physical contiguous memory, it often needs memory areas in order to allocate memory or minimize external fragmentation.


This is when the concepts of buddy systems and slab systems come into being!


Allocating the kernel memory

Firstly, we should consider why there is a need to allocate kernel memory in the first place. When a process runs, it may require some additional memory. Although the pages will be allocated from the list of the page frames which are maintained by the kernel, they are mostly free pages.


This kind of memory is usually allocated from the memory pool to satisfy the ordinary mode process because:


  • The requested memory in the data structure may vary in size or pages.  So the memory needs to be used conservatively because of fragmentation. This is important as the operating system may not use kernel code for the paging systems.


  • The pages that can be allocated may not have to be stored in the contiguous physical memory. Though, certain hardware devices can interact with physical memory directly. Though, this may also require memory which resides in the physical memory pages.


Therefore, there is a need to manage the memory involved in the kernel processes. That’s why we usually follow the two strategies which are the buddy system in the OS and the slab system.


Buddy system in the OS

This is an ideal memory allocation concept that divides your memory into the power of two. It tries to satisfy your memory request by partitioning the memory and using it in equal halves to see where it fits the best.


Assume your memory is of the size 2m. The size of this process is denoted by P.


In the case  2m-1 < P<= 2m, you can calculate the memory in your process. Otherwise, we can allocate the memory into halves. If it does not satisfy the given condition, repeat the steps till the above condition is satisfied.


Example:


We are given the buddy system with the physical address, 256 KB. Then, we need to find the partition size of 36 KB.


As 36 kb is greater than 32 kb but less than the available 64 kb. Therefore, 64 kb of the memory is suitable to store the process.



The Importance of the buddy system in the OS


There is an essential need for the buddy system in your OS because of the following reasons:


  • There’s a limit on the number of given processes in the static positioning. The space will not be enough if there’s a big difference in the process size and the partition size.


  • Dynamic programming is considered to be more complex because the partition size changes while allocating the memory to new processes.


  • The Buddy system somehow exists between dynamic and static programming. Although support may be limited we need to combine the memory blocks


Types of buddy systems


There are various types of buddy systems that you should take into consideration.

They are:


  • Binary buddy system: In this buddy system, the memory blocks are divided into equal parts to satisfy the recurrence relation of Li = Li - 1 + Li  - 1


  • Fibonacci buddy system: It uses block sizes of 16, 32, 48, 80, 128, and 208 bytes. Each block will be the sum of its two proceeding blocks.


  • Weighted buddy system: Here the memory block of size 2k +2 is split into 2k and 3.2k block sizes


Slab System

The second strategy which is usually implemented for kernel memory allocation is the slab system. A slab consists of one or more than one contiguous page. For example; there will be a separate cache for data structures, file objects, semaphores, and so on.


Take another example of two kernels of 3 KB with three objects of size 7 KB. The slab allocation uses catches for storing the object. These are marked as free and will be used to allocate the objects.


A 12 kb slab in this case will store at least 6 2kb objects, with all of them marked as free. The new objects will be marked for the needed data structure. The kernel request in the slab allocator describes the process description. In a Linux system, the process descriptor will be of the size type struct with a maximum of 1.7 kb memory. This will create some new tasks for the objects of that cache.


This cache would be able to fulfill objects that are allocated in the slab so as to mark them free.


In Linux, the given slab will be of three states:

  • Full: All the objects here will be marked as the used
  • Empty: All the objects in your slab are marked as free
  • Partial: The slab in this case consists of all free or the used objects


The basic aim of a slab allocator will be to satisfy requests given in the partial slab. If none of it exists, the free object will get assigned to your empty slab.


Add-on learning: Belady's anomaly in OS

Another essential concept that you should know if you are involved with the operating system is Belady's anomaly in os. It is seen as the phenomenon which helps to increase the number of page frames to increase the number of page faults in your memory.


Wrapping Up

With this guide, I hope you’ve gained an in-depth understanding of the buddy system in OS and the slab system for allocating kernel memory. Stick to this blog and equip your knowledge bank.