Linux內核從slab中分配內存空間上層函數由kmalloc()或kmem_cache_alloc()函數實現。
kmalloc()->__kmalloc()->__do_kmalloc()
- /**
- * __do_kmalloc - allocate memory
- * @size: how many bytes of memory are required.
- * @flags: the type of memory to allocate (see kmalloc).
- * @caller: function caller for debug tracking of the caller
- */
- static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
- void *caller)
- {
- struct kmem_cache *cachep;
- void *ret;
-
- /* If you want to save a few bytes .text space: replace
- * __ with kmem_.
- * Then kmalloc uses the uninlined functions instead of the inline
- * functions.
- */
- /*查找指定大小的通用cache,關於sizes[]數組,在前面
- 的初始化中就已經分析過了*/
- cachep = __find_general_cachep(size, flags);
- if (unlikely(ZERO_OR_NULL_PTR(cachep)))
- return cachep;
- /*實際的分配工作*/
- ret = __cache_alloc(cachep, flags, caller);
-
- trace_kmalloc((unsigned long) caller, ret,
- size, cachep->buffer_size, flags);
-
- return ret;
- }
實際的分配工作:__do_cache_alloc()->__cache_alloc()->____cache_alloc()
- /*從指定cache中分配對象*/
- static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
- {
- void *objp;
- struct array_cache *ac;
-
- check_irq_off();
- /* 獲得本CPU的local cache */
- ac = cpu_cache_get(cachep);
- /* 如果local cache中有可用的空閒對象 */
- if (likely(ac->avail)) {
- /* 更新local cache命中計數 */
- STATS_INC_ALLOCHIT(cachep);
- /* touched置1表示最近使用了local cache,這會影響填充
- local cache時的數目,最近使用的填充較多的對象 */
- ac->touched = 1;
- /* 從local cache的entry數組中提取最後面的空閒對象 */
- objp = ac->entry[--ac->avail];
- } else {
- /* local cache中沒有空閒對象,更新未命中計數 */
- STATS_INC_ALLOCMISS(cachep);
- /* 從slab三鏈中提取空閒對象填充到local cache中 */
- objp = cache_alloc_refill(cachep, flags);
- #if 0/*這裡是我新加的,這裡可能是這個版本的一個bug,在最新的內核裡面這塊已經加上了*/
- /*
- * the 'ac' may be updated by cache_alloc_refill(),
- * and kmemleak_erase() requires its correct value.
- */
- /* cache_alloc_refill的cache_grow打開了中斷,local cache指針可能發生了變化,需要重新獲得 */
- ac = cpu_cache_get(cachep);
- #endif
- }
- /*
- * To avoid a false negative, if an object that is in one of the
- * per-CPU caches is leaked, we need to make sure kmemleak doesn't
- * treat the array pointers as a reference to the object.
- */ /* 分配出去的對象,其entry指針指向空 */
- kmemleak_erase(&ac->entry[ac->avail]);
- return objp;
- }
該函數的執行流程:
1,從本地CPU cache中查找是否有空閒的對象;
2,如果本地CPU cache 中沒有空閒對象,從slab三鏈中提取空閒對象,此操作由函數cache_alloc_refill()實現
1)如果存在共享本地cache,那麼將共享本地cache中的對象批量復制到本地cache。
2)如果沒有shared local cache,或是其中沒有空閒的對象,從slab鏈表中分配,其中,從slab中分配時,先查看部分空余鏈表,然後再查看空余鏈表。將slab鏈表中的數據先放到本地CPU cache中。
3) 如果本地CPU cache中任然沒有數據,那麼只有重新創建一個slab,然後再試。
- /*從slab三鏈中提取一部分空閒對象填充到local cache中*/
- static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
- {
- int batchcount;
- struct kmem_list3 *l3;
- struct array_cache *ac;
- int node;
-
- retry:
- check_irq_off();
- /* 獲得本內存節點,UMA只有一個節點 */
- node = numa_node_id();
- /* 獲得本CPU的local cache */
- ac = cpu_cache_get(cachep);
- /* 批量填充的數目,local cache是按批填充的 */
- batchcount = ac->batchcount;
- if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
- /*
- * If there was little recent activity on this cache, then
- * perform only a partial refill. Otherwise we could generate
- * refill bouncing.
- */
- /* 最近未使用過此local cache,沒有必要添加過多的對象
- ,添加的數目為默認的限定值 */
- batchcount = BATCHREFILL_LIMIT;
- }
- /* 獲得本內存節點、本cache的slab三鏈 */
- l3 = cachep->nodelists[node];
-
- BUG_ON(ac->avail > 0 || !l3);
- spin_lock(&l3->list_lock);
-
- /* See if we can refill from the shared array */
- /* shared local cache用於多核系統中,為所有cpu共享
- ,如果slab cache包含一個這樣的結構
- ,那麼首先從shared local cache中批量搬運空閒對象到local cache中
- 。通過shared local cache使填充工作變得簡單。*/
- if (l3->shared && transfer_objects(ac, l3->shared, batchcount))
- goto alloc_done;
-
- /* 如果沒有shared local cache,或是其中沒有空閒的對象
- ,從slab鏈表中分配 */
- while (batchcount > 0) {
- struct list_head *entry;
- struct slab *slabp;
- /* Get slab alloc is to come from. */
-
- /* 先從部分滿slab鏈表中分配 */
- entry = l3->slabs_partial.next;
- /* next指向頭節點本身,說明部分滿slab鏈表為空 */
- if (entry == &l3->slabs_partial) {
- /* 表示剛剛訪問了slab空鏈表 */
- l3->free_touched = 1;
- /* 檢查空slab鏈表 */
- entry = l3->slabs_free.next;
- /* 空slab鏈表也為空,必須增加slab了 */
- if (entry == &l3->slabs_free)
- goto must_grow;
- }
- /* 獲得鏈表節點所在的slab */
- slabp = list_entry(entry, struct slab, list);
- /*調試用*/
- check_slabp(cachep, slabp);
- check_spinlock_acquired(cachep);
-
- /*
- * The slab was either on partial or free list so
- * there must be at least one object available for
- * allocation.
- */
- BUG_ON(slabp->inuse >= cachep->num);
-
- while (slabp->inuse < cachep->num && batchcount--) {
- /* 更新調試用的計數器 */
- STATS_INC_ALLOCED(cachep);
- STATS_INC_ACTIVE(cachep);
- STATS_SET_HIGH(cachep);
- /* 從slab中提取一個空閒對象,將其虛擬地址插入到local cache中 */
- ac->entry[ac->avail++] = slab_get_obj(cachep, slabp,
- node);
- }
- check_slabp(cachep, slabp);
-
- /* move slabp to correct slabp list: */
- /* 從原鏈表中刪除此slab節點,list表示此
- slab位於哪個鏈表(滿、部分滿、空)中 */
- list_del(&slabp->list);
- /*因為從中刪除了一個slab,需要從新檢查*/
- if (slabp->free == BUFCTL_END)
- /* 此slab中已經沒有空閒對象,添加到“full”slab鏈表中 */
- list_add(&slabp->list, &l3->slabs_full);
- else
- /* 還有空閒對象,添加到“partial”slab鏈表中 */
- list_add(&slabp->list, &l3->slabs_partial);
- }
-
- must_grow:
- /* 前面從slab鏈表中添加avail個空閒對象到local cache中
- ,更新slab鏈表的空閒對象數 */
- l3->free_objects -= ac->avail;
- alloc_done:
- spin_unlock(&l3->list_lock);
- /* local cache中仍沒有可用的空閒對象,說明slab
- 三鏈中也沒有空閒對象,需要創建新的空slab了 */
- if (unlikely(!ac->avail)) {
- int x;
- /* 創建一個空slab */
- x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
-
- /* cache_grow can reenable interrupts, then ac could change. */
- /* 上面的操作使能了中斷,此期間local cache指針可能發生了變化,需要重新獲得 */
- ac = cpu_cache_get(cachep);
- /* 無法新增空slab,local cache中也沒有空閒對象,表明系統已經無法分配新的空閒對象了 */
- if (!x && ac->avail == 0) /* no objects in sight? abort */
- return NULL;
- /* 走到這有兩種可能,第一種是無論新增空slab成功或失敗,只要avail不為0
- ,表明是其他進程重填了local cache,本進程就不需要重填了
- ,不執行retry流程。第二種是avail為0,並且新增空slab成功
- ,則進入retry流程,利用新分配的空slab填充local cache */
- if (!ac->avail) /* objects refilled by interrupt? */
- goto retry;
- }
- /* 重填了local cache,設置近期訪問標志 */
- ac->touched = 1;
- /* 返回local cache中最後一個空閒對象的虛擬地址 */
- return ac->entry[--ac->avail];
- }