上節介紹了Slob分配器的相關概念和思想,這節來看Slob分配器是如何分配對象的。kmem_cache_alloc_node()函數用來分配一個專用緩存的對象:
相關閱讀:
Linux Slob分配器(一)--概述 http://www.linuxidc.com/Linux/2012-07/64107.htm
Linux Slob分配器(三)--釋放對象 http://www.linuxidc.com/Linux/2012-07/64109.htm
- void *kmem_cache_alloc_node(struct kmem_cache *c, gfp_t flags, int node)
- {
- void *b;
-
- if (c->size < PAGE_SIZE) {//對象小於PAGE_SIZE,由Slob分配器進行分配
- b = slob_alloc(c->size, flags, c->align, node);
- trace_kmem_cache_alloc_node(_RET_IP_, b, c->size,
- SLOB_UNITS(c->size) * SLOB_UNIT,
- flags, node);
- } else {//否則通過伙伴系統分配
- b = slob_new_pages(flags, get_order(c->size), node);
- trace_kmem_cache_alloc_node(_RET_IP_, b, c->size,
- PAGE_SIZE << get_order(c->size),
- flags, node);
- }
-
- if (c->ctor)//如果定義了構造函數則調用構造函數
- c->ctor(b);
-
- kmemleak_alloc_recursive(b, c->size, 1, c->flags, flags);
- return b;
- }
由於slob為PAGE_SIZE大小,因此首先要判斷要求分配的對象的大小是否在這個范圍內,如果是,則通過Slob分配器來分配,否則的話通過伙伴系統分配。
來看Slob分配對象的具體過程
- static void *slob_alloc(size_t size, gfp_t gfp, int align, int node)
- {
- struct slob_page *sp;
- struct list_head *prev;
- struct list_head *slob_list;
- slob_t *b = NULL;
- unsigned long flags;
-
- /*根據分配對象的大小選擇從哪個鏈表的slob中進行分配*/
- if (size < SLOB_BREAK1)
- slob_list = &free_slob_small;
- else if (size < SLOB_BREAK2)
- slob_list = &free_slob_medium;
- else
- slob_list = &free_slob_large;
-
- spin_lock_irqsave(&slob_lock, flags);
- /* Iterate through each partially free page, try to find room */
- list_for_each_entry(sp, slob_list, list) {//遍歷slob鏈表
- #ifdef CONFIG_NUMA
- /*
- * If there's a node specification, search for a partial
- * page with a matching node id in the freelist.
- */
- if (node != -1 && page_to_nid(&sp->page) != node)//節點不匹配
- continue;
- #endif
- /* Enough room on this page? */
- if (sp->units < SLOB_UNITS(size))//slob中的空間不夠
- continue;
-
- /* Attempt to alloc */
- prev = sp->list.prev;
- b = slob_page_alloc(sp, size, align);//分配對象
- if (!b)
- continue;
-
- /* Improve fragment distribution and reduce our average
- * search time by starting our next search here. (see
- * Knuth vol 1, sec 2.5, pg 449) */
- /*這裡將slob_list鏈表頭移動到prev->next前面,以便下次遍歷時能夠從prev->next開始遍歷*/
- if (prev != slob_list->prev &&
- slob_list->next != prev->next)
- list_move_tail(slob_list, prev->next);
- break;
- }
- spin_unlock_irqrestore(&slob_lock, flags);
-
- /* Not enough space: must allocate a new page */
- if (!b) {//沒有分配到對象,也就是說slob_list中沒有可以滿足分配要求的slob了
- b = slob_new_pages(gfp & ~__GFP_ZERO, 0, node);//創建新的slob
- if (!b)
- return NULL;
- sp = slob_page(b);//獲取slob的地址
- set_slob_page(sp);
-
- spin_lock_irqsave(&slob_lock, flags);
- sp->units = SLOB_UNITS(PAGE_SIZE);//計算單元數
- sp->free = b; //設置首個空閒塊的地址
- INIT_LIST_HEAD(&sp->list);
- set_slob(b, SLOB_UNITS(PAGE_SIZE), b + SLOB_UNITS(PAGE_SIZE));
- set_slob_page_free(sp, slob_list); //將sp鏈入slob_list
- b = slob_page_alloc(sp, size, align);//從新的slob中分配塊
- BUG_ON(!b);
- spin_unlock_irqrestore(&slob_lock, flags);
- }
- if (unlikely((gfp & __GFP_ZERO) && b))
- memset(b, 0, size);
- return b;
- }
- 首先要根據對象的大小來決定從哪個全局鏈表中尋找slob進行分配
- 遍歷選取的鏈表,找到一個空間足夠滿足分配要求的slob
- 從選取的slob中分配對象塊(slob_page_alloc())
- 如果遍歷完整個鏈表都沒能分配到對象,則創建一個新的slob(slob_new_page()),然後設置slob的屬性,再進行分配,可以看到一個新的slob中只有一個塊,並且下一個空閒對象的指針指向了下一頁的起始處,也就是頁對齊的
來看分配的細節操作slab_page_alloc()
- static void *slob_page_alloc(struct slob_page *sp, size_t size, int align)
- {
- slob_t *prev, *cur, *aligned = NULL;
- int delta = 0, units = SLOB_UNITS(size);
-
- for (prev = NULL, cur = sp->free; ; prev = cur, cur = slob_next(cur)) {
- slobidx_t avail = slob_units(cur);//計算獲取的空閒塊的容量
-
- /*如果設置了對齊值則先將塊進行對齊*/
- if (align) {
- aligned = (slob_t *)ALIGN((unsigned long)cur, align);
- delta = aligned - cur;//計算對齊後的對象增加了多少字節的內存
- }
-
- /*空閒塊內存不小於要求分配的 units+對齊增量*/
- if (avail >= units + delta) { /* room enough? */
- slob_t *next;
-
- /*確實進行了對齊操作*/
- if (delta) { /* need to fragment head to align? */
- next = slob_next(cur);//獲取下一個空閒塊
-
- /*這裡將原本的一個塊分裂成了兩個塊*/
- set_slob(aligned, avail - delta, next);//設置空閒對象偏移aligned-->next
- set_slob(cur, delta, aligned);//設置空閒對象偏移cur--->aligned
-
- /*調整prev指針以及cur指針,都向後移動一個塊*/
- prev = cur;
- cur = aligned;
- avail = slob_units(cur);//重新獲取單元數
- }
-
- next = slob_next(cur);//獲取下一個空閒塊
- if (avail == units) { /* 空閒塊的大小和要求的大小完全相符 */
- if (prev)//存在先驅塊,則將先驅塊的指針指向next塊
- set_slob(prev, slob_units(prev), next);
- else//不存在先驅塊說明為第一個塊,則將free直接指向next
- sp->free = next;
- } else { /* 大小不相符,則要將塊分裂*/
- if (prev)
- set_slob(prev, slob_units(prev), cur + units);
- else
- sp->free = cur + units;
- set_slob(cur + units, avail - units, next);
- }
-
- sp->units -= units;//減少slob的單元數
- if (!sp->units)//單元數為0表明slob沒有空閒單元,則從鏈表中刪除
- clear_slob_page_free(sp);
- return cur;
- }
- if (slob_last(cur))
- return NULL;
- }
- }