辅助函数:
为slab分配页框
- static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
- {
- struct page *page;
- int nr_pages;
- int i;
- #ifndef CONFIG_MMU
- /*
- * Nommu uses slab's for process anonymous memory allocations, and thus
- * requires __GFP_COMP to properly refcount higher order allocations
- */
- flags |= __GFP_COMP;
- #endif
- flags |= cachep->gfpflags;
- if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
- flags |= __GFP_RECLAIMABLE;
- /*从特定的节点分配2^gfporder个连续页*/
- page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
- if (!page)
- return NULL;
- nr_pages = (1 << cachep->gfporder);
- if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
- add_zone_page_state(page_zone(page),
- NR_SLAB_RECLAIMABLE, nr_pages);
- else
- add_zone_page_state(page_zone(page),
- NR_SLAB_UNRECLAIMABLE, nr_pages);
- for (i = 0; i < nr_pages; i++)
- __SetPageSlab(page + i);
- if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
- kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
- if (cachep->ctor)
- kmemcheck_mark_uninitialized_pages(page, nr_pages);
- else
- kmemcheck_mark_unallocated_pages(page, nr_pages);
- }
- /*返回首页的虚拟地址*/
- return page_address(page);
- }
为slab管理区分配空间:
- static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
- int colour_off, gfp_t local_flags,
- int nodeid)
- {
- struct slab *slabp;
- /*如果slab管理区位于slab外,则在指定的slabp_cache中分配空间*/
- if (OFF_SLAB(cachep)) {
- /* Slab management obj is off-slab. */
- slabp = kmem_cache_alloc_node(cachep->slabp_cache,
- local_flags, nodeid);
- /*
- * If the first object in the slab is leaked (it's allocated
- * but no one has a reference to it), we want to make sure
- * kmemleak does not treat the ->s_mem pointer as a reference
- * to the object. Otherwise we will not report the leak.
- */
- kmemleak_scan_area(slabp, offsetof(struct slab, list),
- sizeof(struct list_head), local_flags);
- if (!slabp)
- return NULL;
- } else {/*slab管理区处于slab中*/
- /*slab管理区从slab首部偏移颜色值的地方开始*/
- slabp = objp + colour_off;
- colour_off += cachep->slab_size;
- }
- slabp->inuse = 0;/*对象全为空闲*/
- slabp->colouroff = colour_off; /*刷新第一个对象的偏移*/
- slabp->s_mem = objp + colour_off;/*确定第一个对象的位置*/
- slabp->nodeid = nodeid;/*标识节点*/
- slabp->free = 0; /*下一个空闲对象位于s_mem起始处*/
- return slabp;
- }
