Testing Slob Allocator

Leave a comment

Linux Kernel hast three memory allocator named SLOB, SLAB and SLUB. SLOB allocator is the memory allocator that is used to allocate small amount of memory for Linux kernel objects. Linux kernel objects are different from user mode objects. So we can’t use a user mode application for testing SLOB allocator. For example, if we modify the algorithm used in SLOB allocator, we have to test it using some kernel module which can make memory request to our new SLOB allocator.

 

 

Testing SLOB allocator has to be done from kernel mode. From the kernel module we can use the kmalloc function for allocating memory from SLOB. The module doesn’t need to be fancy. We can use a hello world module which will make a few kmalloc function call. Use the simple kernel module below (name it SLOB_TEST.c) for testing SLOB.

#include <linux/module.h>
#include <linux/random.h>

#define NBYTES	1000

char buff[NBYTES];
char *mem_ptr[NBYTES];

int SCALE(int n){
    if (n == 0) n = 256;

    return n > 0 ? n : (-1) * n;
}

static int slob_test_init(void)
{
    int i = 0;
    int alloc = 0;
    printk("ENTER SLOB_TESTn");
    get_random_bytes(buff, NBYTES);

    for (i=0; i<NBYTES; i++){
        alloc = SCALE((int)buff[i]);
        mem_ptr[i] = kmalloc(alloc, GFP_KERNEL);
    }

    return 0;
}

static void slob_test_exit(void)
{
    int i = 0;
    printk("EXIT SLOB_TESTn");

    for (i=0; i<NBYTES; i++){
        if (mem_ptr[i]){
            kfree(mem_ptr[i]);
        }
    }
}

module_init(slob_test_init);
module_exit(slob_test_exit);

MODULE_AUTHOR("IsonProjects");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("SLOB TESTING MODULE");

 

Use the Makefile below for compiling the module:

obj-m := SLOB_TEST.o

KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

all:
	$(MAKE) -C $(KERNELDIR) M=$(PWD)

clean:
	rm -f *.o *~ core .depend .*.cmd *.ko *.mod.c
	rm -rf .tmp_versions
	rm Module.symvers
	rm modules.order

 

This example is only for a guideline on how to test SLOB allocator. You can modify it any way according to your need.  For example, you can make more than thousand kmalloc requests or instead of allocating random bytes you can follow some pattern. Its up to you how you design your test cases.

 

Leave a Reply