drain_cpu_caches()的最终落脚在free_block()函数上,该函数在前面已做过分析,在此不再列出
- static int drain_freelist(struct kmem_cache *cache,
- struct kmem_list3 *l3, int tofree)
- {
- struct list_head *p;
- int nr_freed;
- struct slab *slabp;
- nr_freed = 0;
- /*slab中的对象还未释放完并且free链表不为空*/
- while (nr_freed < tofree && !list_empty(&l3->slabs_free)) {
- spin_lock_irq(&l3->list_lock);
- p = l3->slabs_free.prev;
- if (p == &l3->slabs_free) {/*链表中已无元素*/
- spin_unlock_irq(&l3->list_lock);
- goto out;
- }
- /*从free链表中取出一个slab*/
- slabp = list_entry(p, struct slab, list);
- #if DEBUG
- BUG_ON(slabp->inuse);
- #endif
- /*从链表中删除*/
- list_del(&slabp->list);
- /*
- * Safe to drop the lock. The slab is no longer linked
- * to the cache.
- */
- /*空闲对象数量总数减去num*/
- l3->free_objects -= cache->num;
- spin_unlock_irq(&l3->list_lock);
- /*销毁slab*/
- slab_destroy(cache, slabp);
- nr_freed++;
- }
- out:
- return nr_freed;
- }
slab_destroy()函数已在前文中分析
- static void __kmem_cache_destroy(struct kmem_cache *cachep)
- {
- int i;
- struct kmem_list3 *l3;
- /*释放存储本地高速缓存描述符的对象*/
- for_each_online_cpu(i)
- kfree(cachep->array[i]);
- /* NUMA: free the list3 structures */
- for_each_online_node(i) {
- l3 = cachep->nodelists[i];
- if (l3) {
- /*释放存储共享本地高速缓存描述符的对象*/
- kfree(l3->shared);
- /*释放存储alien本地高速缓存描述符的对象*/
- free_alien_cache(l3->alien);
- /*释放存储kmem_list3描述符的对象*/
- kfree(l3);
- }
- }
- /*释放存储缓存描述符的对象*/
- kmem_cache_free(&cache_cache, cachep);
- }
