http://www.ibm.com/developerworks/linux/library/l-linux-slab-allocator/?ca=dgr-lnxw16LinuxSlabAllo
这个文章不错。
经过这两天的学习,了解到,在kernel中分配内存 至少有两种方式:
1. kmalloc
2. kmem_cache
其实都是用了kmem_cache。 只是用kmem_cache_alloc的时候,用了一种比较特殊的,有名字定义的kmem_cache。
而且这两种申请方式,都可以从/proc/slabinfo文件中看到。
下面是我写的一个解释。。。
I did some reading and experiment on memory allocation in kernel space. Hope this could be useful for you.
As in user space, we need allocate memory dynamically in kernel space, while it use different mechanism. In order to increase the efficiency and alleviate the fragmentation, kernel always allocate fixed size memory from different memory pools, called slab.
There are two common memory allocation method in kernel space.
Method 1. kmalloc, kfree.
Method 2. kmem_cache_create, kmem_cache_alloc, kmem_cache_free, kmem_cache_destroy.
Apparently, these are two forms, but the idea behind them is the same. I try to draw an analogy to make it clear.
If we think kernel as a factory, memory as boxes we want from the factory.
This factory has some pre-produced boxes with several fixed size boxes. If this can meet your needs, ok, just ask for this(use method 1).
While some time this may not meet your need, such as size is not accurate or you need a huge amount of boxes, you could design your own box, and ask the factory to produce it(use method 2).
One fun thing is the factory(kernel), also provides you a way to track the "order". In /proc/slabinfo, you can see your boxes produced(active) and boxes in warehouse(total numbers).
For method 1, you see these in the bottom of the file, start with "size-", with smallest size just fit your requirement.
For method 2, you see these lines start with the name you defined in kmem_cache_create.
Attachment is the kernel module I used for experiment, you can make it and run on our board. To see what happens in /proc/slabinfo.