Linux内核伙伴系统中页面释放,主函数为free_pages()
一、上层操作- /*用虚拟地址进行释放*/
- void free_pages(unsigned long addr, unsigned int order)
- {
- if (addr != 0) {
- VM_BUG_ON(!virt_addr_valid((void *)addr));
- __free_pages(virt_to_page((void *)addr), order);/*具体的释放函数*/
- }
- }
- /*释放页面*/
- void __free_pages(struct page *page, unsigned int order)
- {
- if (put_page_testzero(page)) {/*count值减一为0时释放*/
- /*调试*/
- trace_mm_page_free_direct(page, order);
- if (order == 0)
- free_hot_page(page);/*释放单个页面*/
- else
- __free_pages_ok(page, order);
- }
- }
二、释放单个页面释放单个页面free_hot_page()调用free_hot_cold_page()函数
- static void free_hot_cold_page(struct page *page, int cold)
- {
- struct zone *zone = page_zone(page);
- struct per_cpu_pages *pcp;
- unsigned long flags;
- int migratetype;
- int wasMlocked = __TestClearPageMlocked(page);
- /*调试代码*/
- kmemcheck_free_shadow(page, 0);
-
- if (PageAnon(page))
- page->mapping = NULL;
- if (free_pages_check(page))
- return;
-
- if (!PageHighMem(page)) {
- debug_check_no_locks_freed(page_address(page), PAGE_SIZE);
- debug_check_no_obj_freed(page_address(page), PAGE_SIZE);
- }
- /*x86下为空*/
- arch_free_page(page, 0);
- /*调试用*/
- kernel_map_pages(page, 1, 0);
- /*获得zone对应cpu的pcp*/
- pcp = &zone_pcp(zone, get_cpu())->pcp;
- /*获得页面的migratetype*/
- migratetype = get_pageblock_migratetype(page);
- set_page_private(page, migratetype);/*设置私有位为参数*/
- local_irq_save(flags);/*保存中断*/
- if (unlikely(wasMlocked))
- free_page_mlock(page);
- __count_vm_event(PGFREE);
-
- /*
- * We only track unmovable, reclaimable and movable on pcp lists.
- * Free ISOLATE pages back to the allocator because they are being
- * offlined but treat RESERVE as movable pages so we can get those
- * areas back if necessary. Otherwise, we may have to free
- * excessively into the page allocator
- */
- if (migratetype >= MIGRATE_PCPTYPES) {
- if (unlikely(migratetype == MIGRATE_ISOLATE)) {
- /*释放到伙伴系统 */
- free_one_page(zone, page, 0, migratetype);
- goto out;
- }
- migratetype = MIGRATE_MOVABLE;
- }
-
- if (cold)/*加入到pcp链表尾部*/
- list_add_tail(&page->lru, &pcp->lists[migratetype]);
- else/*加入到pcp链表头部*/
- list_add(&page->lru, &pcp->lists[migratetype]);
- pcp->count++;/*pcp计数加一*/
- if (pcp->count >= pcp->high) {/*当pcp中页面数量超过他的最高值时,
- 释放pcp->batch个页面到伙伴系统中*/
- free_pcppages_bulk(zone, pcp->batch, pcp);
- pcp->count -= pcp->batch;/*页面数减去释放的页面数量*/
- }
-
- out:
- local_irq_restore(flags);/*回复中断*/
- put_cpu();
- }
从pcp中释放页面到伙伴系统中free_pcppages_bulk()
- are in same zone, and of same order.
- * count is the number of pages to free.
- *
- * If the zone was previously in an "all pages pinned" state then look to
- * see if this freeing clears that state.
- *
- * And clear the zone"s pages_scanned counter, to hold off the "all pages are
- * pinned" detection logic.
- */
- /*从PCP中释放count个页面到伙伴系统中*/
- static void free_pcppages_bulk(struct zone *zone, int count,
- struct per_cpu_pages *pcp)
- {
- int migratetype = 0;
- int batch_free = 0;
- /*
- * 虽然管理区可以按照CPU节点分类,但是也可以跨CPU节点进行内存分配,
- * 因此这里需要用自旋锁保护管理区
- * 使用每CPU缓存的目的,也是为了减少使用这把锁。
- */
- spin_lock(&zone->lock);
- /* all_unreclaimable代表了内存紧张程度,释放内存后,将此标志清除 */
- zone_clear_flag(zone, ZONE_ALL_UNRECLAIMABLE);
- zone->pages_scanned = 0;/* pages_scanned代表最后一次内存紧张以来,页面回收过程已经扫描的页数。
- 目前正在释放内存,将此清0,待回收过程随后回收时重新计数 */
-
- /*增加管理区空闲页数*/
- __mod_zone_page_state(zone, NR_FREE_PAGES, count);
- while (count) {
- struct page *page;
- struct list_head *list;
-
- /*
- * Remove pages from lists in a round-robin fashion. A
- * batch_free count is maintained that is incremented when an
- * empty list is encountered. This is so more pages are freed
- * off fuller lists instead of spinning excessively around empty
- * lists
- */
- do {/*从pcp的三类链表中找出不空的一个,释放*/
- batch_free++;/*参看英文注释*/
- if (++migratetype == MIGRATE_PCPTYPES)
- migratetype = 0;
- list = &pcp->lists[migratetype];
- } while (list_empty(list));
-
- do {
- page = list_entry(list->prev, struct page, lru);
- /* must delete as __free_one_page list manipulates */
- list_del(&page->lru);
- /*释放单个页面到伙伴系统,注意这里的分类回收*/
- __free_one_page(page, zone, 0, migratetype);
- trace_mm_page_pcpu_drain(page, 0, migratetype);
- } while (--count && --batch_free && !list_empty(list));
- }
- spin_unlock(&zone->lock);
- }
64位 CentOS 5.6 中 rrdtool 的编译安装Linux内存管理之伙伴系统(内存分配)相关资讯 Linux内存
- Linux内存管理精述 (01月18日)
- 在 Linux x86-32 模式下分析内存映 (02/08/2015 07:25:25)
- Linux下如何释放cache内存 (02/02/2015 13:36:34)
| - 在 Linux x86-64 模式下分析内存映 (02/08/2015 07:33:09)
- Linux系统入门学习:如何检查Linux (02/06/2015 10:36:00)
- Linux内存映射 (04/04/2014 13:21:48)
|
本文评论 查看全部评论 (0)