Assignment 2.2
Assignment 2.2
QUESTION:
1. List and explain methods that are used for allocating kernel memory.
KERNEL MEMORY ALLOCATION.
Kernel memory allocation refers to the process by which the operating system's kernel allocates
memory for its own use. The kernel is the central component of an operating system that manages
system resources, including memory, and provides low-level services to other parts of the operating
system.
The process of allocating kernel memory depends on the specific operating system and programming
language being used.
However, in general, there are several common methods for allocating kernel memory.
1. kmalloc()
The kmalloc() is most commonly used function for kernel memory allocation. It helps us to allocate the
memory from the kernel heap. In other words, kmalloc() is a function used in the Linux kernel to
dynamically allocate memory from the kernel heap.
It is the most commonly used function for kernel memory allocation because it is fast and efficient. It
contains two parameters
When kmalloc() is called, it determines whether the kernel heap has enough free space to meet the
required memory size.
Internally, kmalloc() manage the kernel heap using set of data structures, including a list of free blocks of
memory and a set of slab caches.
Basically, a slab cache is a pre-allocated container of object of a certain size that can be used to improve
the performance by reducing the overhead of allocation and deallocation.
In other words, it checks if a certain size of object requested using kmalloc(), then the function check if
slab cache is existing for that size or not. If it does not exist it will allocate the object from the cache.
Vmalloc()
Vmalloc () is also a function that is used in the Linux kernel to allocate memory from the virtual memory
space of the kernel. Unlike kmalloc (), which allocate the memory physically, vmalloc allocate the
memory that can be non-contiguous and scattered throughout the virtual memory space of the kernel.
As a result, it can be used to allocate sizable memory blocks that might not be accessible as a single,
continuous block.
When vmalloc() is called, it determines whether the virtual memory space of the kernel has enough free
space to support the desired memory size. If so, a block of virtual memory with the required size is
allocated, and a reference to the block's start is returned. The function returns a NULL pointer to
indicate that the allocation failed if there isn't enough available space.
Internally, vmalloc() manage the virtual memory space of the kernel using set of data structure,
including the set of page tables that help us to map the virtual memory to physical memory.
It's vital to keep in mind that devices cannot directly use the memory allocated by vmalloc(). Virtual
memory must instead be mapped to physical memory using the kernel's mapping capabilities. This can
increase complexity and overhead, but it also enables vmalloc() to allocate significantly more memory
than kmalloc ().
GFP_kernel flags
The GFP kernel flags are used in the Linux kernel to describe the kind of memory allocation needed for
kernel-level software. To specify the properties of the memory being allocated, these flags are used as
parameters in memory allocation procedures like kmalloc() and vmalloc(). The "gfp mask" parameter, a
bit mask that controls how the memory allocation function behaves, contains the GFP kernel flags.
The following list of frequently used GFP kernel flags and their definitions:
GFP_KERNEL: The default flag for typical kernel memory allocations is GFP KERNEL. GFP_kernel is a flag
used in Linux kernel memory allocation functions like kmalloc and vmalloc to specify that the allocation
is for kernel use. This means that the allocated memory is intended for internal use by the kernel and is
not accessible to user-space processes. The function can sleep if necessary to wait for memory to
become available. It also says that memory allocation should take place in the standard kernel context.
GFP ATOMIC: This flag is used for memory allocations that must be carried out in atomic contexts,
including interrupt handlers or other crucial areas where sleeping is not permitted. This is crucial in
scenarios where the allocation must be completed without interruption to maintain system stability,
memory allocations using this flag must be finished right away, without any delay or sleep. GFP ATOMIC
should be used sparingly, though, as it can result in memory fragmentation and lower success rates for
memory allocation.
GFP_NOWAIT: Similar to GFP ATOMIC, GFP NOWAIT permits a certain amount of blocking to wait for
memory to become available if necessary. If the memory is not available, the allocation will fail
immediately instead of waiting for it to become available.