上一篇回收slab对象时,先考虑放入local cache,当local cache已超过上限时,尝试放入shared cache。
否则从local cache中批量释放batch_count对象放入slab三链。
当slab三链的空闲对象超过上限,并且待释放的slab对象对应的slab中已经没有正在使用的对象时,将slab直接释放掉。
释放slab对应的函数是slab_destroy,下面分析一下。
/**
* slab_destroy - destroy and release all objects in a slab
* @cachep: cache pointer being destroyed
* @slabp: slab pointer being destroyed
*
* Destroy all the objs in a slab, and release the mem back to the system.
* Before calling the slab must have been unlinked from the cache. The
* cache-lock is not held/needed.
*/
static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
{
/* slabp->s_mem记录的是slab对象的起始地址,由于着色偏移,所以需要减去colouroff指向slab首页面的地址 */
void *addr = slabp->s_mem - slabp->colouroff;
slab_destroy_debugcheck(cachep, slabp);
/* 判断一下使用那种销毁方式,rcu是多核中的一种同步机制 */
if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
struct slab_rcu *slab_rcu;
slab_rcu = (struct slab_rcu *)slabp;
slab_rcu->cachep = cachep;
slab_rcu->addr = addr;
call_rcu(&slab_rcu->head, kmem_rcu_free);
} else {
/* 释放slab占用的页面,如果slab管理对象内置的话,随着页面一起释放了 */
kmem_freepages(cachep, addr);
/* 如果slab管理对象是外置的花,需要单独释放一下 */
if (OFF_SLAB(cachep))
kmem_cache_free(cachep->slabp_cache, slabp);
}
}
函数比较简单,kmem_freepages在后续分析伙伴关系系统时,再来分析。