辅助数据结构与变量
Linux内核中将所有的通用cache以不同的大小存放在数组中,以方便查找。其中malloc_sizes[]数组为cache_sizes类型的数组,存放各个cache的大小;cache_names[]数组为cache_names结构类型数组,存放各个cache大小的名称;malloc_sizes[]数组和cache_names[]数组下标对应,也就是说cache_names[i]名称的cache对应的大小为malloc_sizes[i]。
- /* Size description struct for general caches. */
- struct cache_sizes {
- size_t cs_size;
- struct kmem_cache *cs_cachep;
- #ifdef CONFIG_ZONE_DMA
- struct kmem_cache *cs_dmacachep;
- #endif
- };
- /*
- * These are the default caches for kmalloc. Custom caches can have other sizes.
- */
- struct cache_sizes malloc_sizes[] = {
- #define CACHE(x) { .cs_size = (x) },
- #include <linux/kmalloc_sizes.h>
- CACHE(ULONG_MAX)
- #undef CACHE
- };
- /* Must match cache_sizes above. Out of line to keep cache footprint low. */
- struct cache_names {
- char *name;
- char *name_dma;
- };
- static struct cache_names __initdata cache_names[] = {
- #define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
- #include <linux/kmalloc_sizes.h>
- {NULL,}
- #undef CACHE
- };
二、内核启动末期初始化
1,根据对象大小计算local cache中对象数目上限;
2,借助数据结构ccupdate_struct操作cpu本地cache。为每个在线cpu分配cpu本地cache;
3,用新分配的cpu本地cache替换原有的cache;
4,更新slab三链以及cpu本地共享cache。
第二阶段代码分析
Start_kernel()->kmem_cache_init_late()
- /*Slab系统初始化分两个部分,先初始化一些基本的,待系统初始化工作进行的差不多时,再配置一些特殊功能。*/
- void __init kmem_cache_init_late(void)
- {
- struct kmem_cache *cachep;
- /* 初始化阶段local cache的大小是固定的,要根据对象大小重新计算 */
- /* 6) resize the head arrays to their final sizes */
- mutex_lock(&cache_chain_mutex);
- list_for_each_entry(cachep, &cache_chain, next)
- if (enable_cpucache(cachep, GFP_NOWAIT))
- BUG();
- mutex_unlock(&cache_chain_mutex);
- /* Done! */
- /* 大功告成,general cache终于全部建立起来了 */
- g_cpucache_up = FULL;
- /* Annotate slab for lockdep -- annotate the malloc caches */
- init_lock_keys();
- /*
- * Register a cpu startup notifier callback that initializes
- * cpu_cache_get for all new cpus
- */
- /* 注册cpu up回调函数,cpu up时配置local cache */
- register_cpu_notifier(&cpucache_notifier);
- /*
- * The reap timers are started later, with a module init call: That part
- * of the kernel is not yet operational.
- */
- }
- /* Called with cache_chain_mutex held always */
- /*local cache 初始化*/
- static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
- {
- int err;
- int limit, shared;
- /*
- * The head array serves three purposes:
- * - create a LIFO ordering, i.e. return objects that are cache-warm
- * - reduce the number of spinlock operations.
- * - reduce the number of linked list operations on the slab and
- * bufctl chains: array operations are cheaper.
- * The numbers are guessed, we should auto-tune as described by
- * Bonwick.
- */ /* 根据对象大小计算local cache中对象数目上限 */
- if (cachep->buffer_size > 131072)
- limit = 1;
- else if (cachep->buffer_size > PAGE_SIZE)
- limit = 8;
- else if (cachep->buffer_size > 1024)
- limit = 24;
- else if (cachep->buffer_size > 256)
- limit = 54;
- else
- limit = 120;
- /*
- * CPU bound tasks (e.g. network routing) can exhibit cpu bound
- * allocation behaviour: Most allocs on one cpu, most free operations
- * on another cpu. For these cases, an efficient object passing between
- * cpus is necessary. This is provided by a shared array. The array
- * replaces Bonwick's magazine layer.
- * On uniprocessor, it's functionally equivalent (but less efficient)
- * to a larger limit. Thus disabled by default.
- */
- shared = 0;
- /* 多核系统,设置shared local cache中对象数目 */
- if (cachep->buffer_size <= PAGE_SIZE && num_possible_cpus() > 1)
- shared = 8;
- #if DEBUG
- /*
- * With debugging enabled, large batchcount lead to excessively long
- * periods with disabled local interrupts. Limit the batchcount
- */
- if (limit > 32)
- limit = 32;
- #endif
- /* 配置local cache */
- err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared, gfp);
- if (err)
- printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
- cachep->name, -err);
- return err;
- }
- /* Always called with the cache_chain_mutex held */
- /*配置local cache、shared local cache和slab三链*/
- static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
- int batchcount, int shared, gfp_t gfp)
- {
- struct ccupdate_struct *new;
- int i;
- new = kzalloc(sizeof(*new), gfp);
- if (!new)
- return -ENOMEM;
- /* 为每个cpu分配新的struct array_cache对象 */
- for_each_online_cpu(i) {
- new->new[i] = alloc_arraycache(cpu_to_node(i), limit,
- batchcount, gfp);
- if (!new->new[i]) {
- for (i--; i >= 0; i--)
- kfree(new->new[i]);
- kfree(new);
- return -ENOMEM;
- }
- }
- new->cachep = cachep;
- /* 用新的struct array_cache对象替换旧的struct array_cache对象
- ,在支持cpu热插拔的系统上,离线cpu可能没有释放local cache
- ,使用的仍是旧local cache,参见__kmem_cache_destroy函数
- 。虽然cpu up时要重新配置local cache,也无济于事。考虑下面的情景
- :共有Cpu A和Cpu B,Cpu B down后,destroy Cache X,由于此时Cpu B是down状态
- ,所以Cache X中Cpu B的local cache未释放,过一段时间Cpu B又up了
- ,更新cache_chain 链中所有cache的local cache,但此时Cache X对象已经释放回
- cache_cache中了,其Cpu B local cache并未被更新。又过了一段时间
- ,系统需要创建新的cache,将Cache X对象分配出去,其Cpu B仍然是旧的
- local cache,需要进行更新。
- */
- on_each_cpu(do_ccupdate_local, (void *)new, 1);
- check_irq_on();
- cachep->batchcount = batchcount;
- cachep->limit = limit;
- cachep->shared = shared;
- /* 释放旧的local cache */
- for_each_online_cpu(i) {
- struct array_cache *ccold = new->new[i];
- if (!ccold)
- continue;
- spin_lock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
- /* 释放旧local cache中的对象 */
- free_block(cachep, ccold->entry, ccold->avail, cpu_to_node(i));
- spin_unlock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
- /* 释放旧的struct array_cache对象 */
- kfree(ccold);
- }
- kfree(new);
- /* 初始化shared local cache 和slab三链 */
- return alloc_kmemlist(cachep, gfp);
- }
