Kmalloc Vs. Vmalloc

Dynamic memory allocation in Device Driver by Kmalloc and Vmalloc

In this chapter we will discuss about memory allocation in Linux device drivers, kmalloc and vmalloc , what are the differences between kmalloc and vmalloc, when to use kmalloc and vmalloc.

While writing device drivers we might need to allocate some memory in kernel space to store some valuable data which can be used for multiple purpose further. Linux kernel provides us various method of allocating memory in kernel space. But following are two kernel memory allocation functions which is majorly used by almost all the drivers.

1) Kmalloc

2)Vmalloc

Kmalloc

Kmalloc is similar to malloc function, we use in our C program to allocate memory in user space. kmalloc allocates memory in kernel space. kmalloc allocates contiguous memory in physical memory as well as virtual memory.

kmalloc

kmalloc is declared in <linux/slab.h>. So if you are using kmalloc call in your driver you have to include <linux/slab.h> file in your driver. Below is the prototype of kmalloc

void *kmalloc(size_t size, int flags);

kmalloc takes two arguments

1)size:- it specify the size of memory to be allocated.

2)flag:- flag argument denotes the behavior of kmalloc call while allocating                    memory. Linux kernel defined various flags and their different meaning. we will discuss two most widely used flags (GFP_KERNEL and GFP_ATOMIC).

GFP_KERNEL:- use of GFP_KERNEL means kmalloc can put current process in sleep state if memory is low . Hence we cannot use this flag while allocating memory in interrupt handler as we discussed in interrupt handling that we cannot sleep while executing interrupt handler.

GFP_ATOMIC:- GFP_ATOMIC flag ensure that it won’t let current process to sleep if memory is low. So this flag is safe to allocate memory in interrupt handler.

Return value:-

On successful allocation of memory kmalloc return the virtual address of first page allocated. On error it returns NULL.

 

Kfree()

Kfree function is used to freeing the memory allocated by kmalloc. its declared in <Linux/slab.h> Below is the prototype of kfree();

void kfree(const void *ptr)

It takes address returned by kmalloc while allocation memory as an argument.

Lets understand the use of kmalloc by writing a simple driver which allocates some chunk of memory

=====================================================================

 

#include<linux/init.h>

#include<linux/module.h>

#include <linux/slab.h>

#include <linux/mm.h>

MODULE_LICENSE(“GPL”);

MODULE_AUTHOR(“tutorialsdaddy.com”);

 

void *ptr;

static int kmalloc_init(void) //kmalloc_init function to be called at the time of insmod

{

 

size_t size = 1024; //allocate 1024 bytes

ptr = (void *) kmalloc(size,GFP_KERNEL); //in the kernel segment

if(ptr == (void *) 0)

{

printk(“memory allocation failed\n”);

return 0;

}

else

printk(“Memory allocated successfully\n”);

return 0;

 

}

 

static void kmalloc_exit(void) //kmalloc_exit function to be called at the time of rmmod

{

 

kfree(ptr); //free the allocated memory

printk(“Memory freed\n”);

}

module_init(kmalloc_init);

module_exit(kmalloc_exit);

=====================================================================

Above driver code allocates 1024 bytes of memory in init kmalloc_init() function. you can compile and load this driver in the same way as we tried while writing character device driver(==>)

When to use Kmalloc :-

If memory required in driver needs to be contiguous in physical memory then we have to use kmalloc for allocation of memory.

2)Vmalloc:-

vmalloc is the other call to allocate memory in kernel space as like kmalloc.

vmalloc allocates contiguous memory in virtual memory but it doesn’t guarantee that memory allocated in physical memory will be contiguous.

vmalloc

Vmalloc is declared in <Linux/vmalloc.h> . usage of vmalloc call is similar to malloc call. below is the prototype of vmalloc

void * vmalloc(undigned long size).

argument size is the size of memory required to be allocated.

Return value:-

Upon successful allocation of memory it return address of first byte of allocated memory block. On failure it returns NULL.

vfree():-

vfree function is used to freeing the memory allocated by vmalloc(). below is the prototype of vfree().

void *vfree(const void *ptr);

argument ptr is the address returned while allocating memory using vmalloc.

Lets understand vmalloc() by writing a simple driver which allocates 1024 byte of memory using vmalloc().

 

=========================================================================

#include<linux/init.h>

#include<linux/module.h>

#include <linux/vmalloc.h>

MODULE_LICENSE(“GPL”);

MODULE_AUTHOR(“tutorialsdaddy.com”);

 

void *ptr;

static int my_vmalloc_init(void) //vmalloc_init function to be called at the time of insmod

{

 

unsigned long size = 1024; //allocate 1024 bytes

ptr = (void *) vmalloc(size); //in the kernel segment

if(ptr == (void *) 0)

{

printk(“memory allocation failed\n”);

return 0;

}

else

printk(“Memory allocated successfully\n”);

return 0;

 

}

 

static void my_vmalloc_exit(void) //vmalloc_exit function to be called at the time of rmmod

{

 

vfree(ptr); //free the allocated memory

printk(“Memory freed\n”);

}

module_init(my_vmalloc_init);

module_exit(my_vmalloc_exit);

=========================================================================

above driver allocates 1024 byte of memory in init function and free it in exit function.

When to use vmalloc: when you are sure that memory required will never needs to interact with hardware in future or we can if you required memory only for software purpose then we should use vmalloc for allocating memory.

 

Difference between kmalloc vs vmalloc

kmalloc_vs_vmalloc

adana elektrikci

', 'auto'); ga('send', 'pageview');