Chinaunix首页 | 论坛 | 博客
  • 博客访问: 294443
  • 博文数量: 44
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 1354
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-08 15:38
个人简介

人生像是在跑马拉松,能够完赛的都是不断地坚持向前迈进;人生就是像在跑马拉松,不断调整步伐,把握好分分秒秒;人生还是像在跑马拉松,能力决定了能跑短程、半程还是全程。人生其实就是一场马拉松,坚持不懈,珍惜时间。

文章分类

分类: LINUX

2015-03-11 01:33:37

此处承接前面未深入分析的页面释放部分,主要详细分析伙伴管理算法中页面释放的实现。页面释放的函数入口是__free_page(),其实则是一个宏定义。

具体实现:

  1. 【file:/include/linux/gfp.h】
  2. #define __free_page(page) __free_pages((page), 0)

__free_pages()的实现:

  1. 【file:/mm/page_alloc.c】
  2. void __free_pages(struct page *page, unsigned int order)
  3. {
  4.     if (put_page_testzero(page)) {
  5.         if (order == 0)
  6.             free_hot_cold_page(page, 0);
  7.         else
  8.             __free_pages_ok(page, order);
  9.     }
  10. }

其中put_page_testzero()是对page结构的_count引用计数做原子减及测试,用于检查内存页面是否仍被使用,如果不再使用,则进行释放。其中order表示页面数量,如果释放的是单页,则会调用free_hot_cold_page()将页面释放至per-cpu page缓存中,而不是伙伴管理算法;真正的释放至伙伴管理算法的是__free_pages_ok(),同时也是用于多个页面释放的情况。

此处接着则由free_hot_cold_page()开始分析:

  1. 【file:/mm/page_alloc.c】
  2. /*
  3.  * Free a 0-order page
  4.  * cold == 1 ? free a cold page : free a hot page
  5.  */
  6. void free_hot_cold_page(struct page *page, int cold)
  7. {
  8.     struct zone *zone = page_zone(page);
  9.     struct per_cpu_pages *pcp;
  10.     unsigned long flags;
  11.     int migratetype;
  12.  
  13.     if (!free_pages_prepare(page, 0))
  14.         return;
  15.  
  16.     migratetype = get_pageblock_migratetype(page);
  17.     set_freepage_migratetype(page, migratetype);
  18.     local_irq_save(flags);
  19.     __count_vm_event(PGFREE);
  20.  
  21.     /*
  22.      * We only track unmovable, reclaimable and movable on pcp lists.
  23.      * Free ISOLATE pages back to the allocator because they are being
  24.      * offlined but treat RESERVE as movable pages so we can get those
  25.      * areas back if necessary. Otherwise, we may have to free
  26.      * excessively into the page allocator
  27.      */
  28.     if (migratetype >= MIGRATE_PCPTYPES) {
  29.         if (unlikely(is_migrate_isolate(migratetype))) {
  30.             free_one_page(zone, page, 0, migratetype);
  31.             goto out;
  32.         }
  33.         migratetype = MIGRATE_MOVABLE;
  34.     }
  35.  
  36.     pcp = &this_cpu_ptr(zone->pageset)->pcp;
  37.     if (cold)
  38.         list_add_tail(&page->lru, &pcp->lists[migratetype]);
  39.     else
  40.         list_add(&page->lru, &pcp->lists[migratetype]);
  41.     pcp->count++;
  42.     if (pcp->count >= pcp->high) {
  43.         unsigned long batch = ACCESS_ONCE(pcp->batch);
  44.         free_pcppages_bulk(zone, batch, pcp);
  45.         pcp->count -= batch;
  46.     }
  47.  
  48. out:
  49.     local_irq_restore(flags);
  50. }

先看一下free_pages_prepare()的实现:

  1. 【file:/mm/page_alloc.c】
  2. static bool free_pages_prepare(struct page *page, unsigned int order)
  3. {
  4.     int i;
  5.     int bad = 0;
  6.  
  7.     trace_mm_page_free(page, order);
  8.     kmemcheck_free_shadow(page, order);
  9.  
  10.     if (PageAnon(page))
  11.         page->mapping = NULL;
  12.     for (i = 0; i < (1 << order); i++)
  13.         bad += free_pages_check(page + i);
  14.     if (bad)
  15.         return false;
  16.  
  17.     if (!PageHighMem(page)) {
  18.         debug_check_no_locks_freed(page_address(page),
  19.                        PAGE_SIZE << order);
  20.         debug_check_no_obj_freed(page_address(page),
  21.                        PAGE_SIZE << order);
  22.     }
  23.     arch_free_page(page, order);
  24.     kernel_map_pages(page, 1 << order, 0);
  25.  
  26.     return true;
  27. }

其中trace_mm_page_free()用于trace追踪机制;而kmemcheck_free_shadow()用于内存检测工具kmemcheck,如果未定义CONFIG_KMEMCHECK的情况下,它是一个空函数。接着后面的PageAnon()等都是用于检查页面状态的情况,以判断页面是否允许释放,避免错误释放页面。由此可知该函数主要作用是检查和调试。

接着回到free_hot_cold_page()函数中,get_pageblock_migratetype()set_freepage_migratetype()分别是获取和设置页面的迁移类型,即设置到page->indexlocal_irq_save()和末尾的local_irq_restore()则用于保存恢复中断请求标识。

    if (migratetype >= MIGRATE_PCPTYPES) {

        if (unlikely(is_migrate_isolate(migratetype))) {

            free_one_page(zone, page, 0, migratetype);

            goto out;

        }

        migratetype = MIGRATE_MOVABLE;

    }

这里面的MIGRATE_PCPTYPES用来表示每CPU页框高速缓存的数据结构中的链表的迁移类型数目,如果某个页面类型大于MIGRATE_PCPTYPES则表示其可挂到可移动列表中,如果迁移类型是MIGRATE_ISOLATE则直接将该其释放到伙伴管理算法中。

末尾部分:

    pcp = &this_cpu_ptr(zone->pageset)->pcp;

    if (cold)

        list_add_tail(&page->lru, &pcp->lists[migratetype]);

    else

        list_add(&page->lru, &pcp->lists[migratetype]);

    pcp->count++;

    if (pcp->count >= pcp->high) {

        unsigned long batch = ACCESS_ONCE(pcp->batch);

        free_pcppages_bulk(zone, batch, pcp);

        pcp->count -= batch;

    }

其中pcp表示内存管理区的每CPU管理结构,cold表示冷热页面,如果是冷页就将其挂接到对应迁移类型的链表尾,而若是热页则挂接到对应迁移类型的链表头。其中if (pcp->count >= pcp->high)判断值得注意,其用于如果释放的页面超过了每CPU缓存的最大页面数时,则将其批量释放至伙伴管理算法中,其中批量数为pcp->batch

具体分析一下释放至伙伴管理算法的实现free_pcppages_bulk()

  1. 【file:/mm/page_alloc.c】
  2. /*
  3.  * Frees a number of pages from the PCP lists
  4.  * Assumes all pages on list are in same zone, and of same order.
  5.  * count is the number of pages to free.
  6.  *
  7.  * If the zone was previously in an "all pages pinned" state then look to
  8.  * see if this freeing clears that state.
  9.  *
  10.  * And clear the zone's pages_scanned counter, to hold off the "all pages are
  11.  * pinned" detection logic.
  12.  */
  13. static void free_pcppages_bulk(struct zone *zone, int count,
  14.                     struct per_cpu_pages *pcp)
  15. {
  16.     int migratetype = 0;
  17.     int batch_free = 0;
  18.     int to_free = count;
  19.  
  20.     spin_lock(&zone->lock);
  21.     zone->pages_scanned = 0;
  22.  
  23.     while (to_free) {
  24.         struct page *page;
  25.         struct list_head *list;
  26.  
  27.         /*
  28.          * Remove pages from lists in a round-robin fashion. A
  29.          * batch_free count is maintained that is incremented when an
  30.          * empty list is encountered. This is so more pages are freed
  31.          * off fuller lists instead of spinning excessively around empty
  32.          * lists
  33.          */
  34.         do {
  35.             batch_free++;
  36.             if (++migratetype == MIGRATE_PCPTYPES)
  37.                 migratetype = 0;
  38.             list = &pcp->lists[migratetype];
  39.         } while (list_empty(list));
  40.  
  41.         /* This is the only non-empty list. Free them all. */
  42.         if (batch_free == MIGRATE_PCPTYPES)
  43.             batch_free = to_free;
  44.  
  45.         do {
  46.             int mt; /* migratetype of the to-be-freed page */
  47.  
  48.             page = list_entry(list->prev, struct page, lru);
  49.             /* must delete as __free_one_page list manipulates */
  50.             list_del(&page->lru);
  51.             mt = get_freepage_migratetype(page);
  52.             /* MIGRATE_MOVABLE list may include MIGRATE_RESERVEs */
  53.             __free_one_page(page, zone, 0, mt);
  54.             trace_mm_page_pcpu_drain(page, 0, mt);
  55.             if (likely(!is_migrate_isolate_page(page))) {
  56.                 __mod_zone_page_state(zone, NR_FREE_PAGES, 1);
  57.                 if (is_migrate_cma(mt))
  58.                     __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, 1);
  59.             }
  60.         } while (--to_free && --batch_free && !list_empty(list));
  61.     }
  62.     spin_unlock(&zone->lock);
  63. }

里面while大循环用于计数释放指定批量数的页面。其中释放方式是先自MIGRATE_UNMOVABLE迁移类型起(止于MIGRATE_PCPTYPES迁移类型),遍历各个链表统计其链表中页面数:

        do {

            batch_free++;

            if (++migratetype == MIGRATE_PCPTYPES)

                migratetype = 0;

            list = &pcp->lists[migratetype];

        } while (list_empty(list));

如果只有MIGRATE_PCPTYPES迁移类型的链表为非空链表,则全部页面将从该链表中释放。

后面的do{}while()里面,其先将页面从lru链表中去除,然后获取页面的迁移类型,通过__free_one_page()释放页面,最后使用__mod_zone_page_state()修改管理区的状态值。

着重分析一下__free_one_page()的实现:

  1. 【file:/mm/page_alloc.c】
  2. /*
  3.  * Freeing function for a buddy system allocator.
  4.  *
  5.  * The concept of a buddy system is to maintain direct-mapped table
  6.  * (containing bit values) for memory blocks of various "orders".
  7.  * The bottom level table contains the map for the smallest allocatable
  8.  * units of memory (here, pages), and each level above it describes
  9.  * pairs of units from the levels below, hence, "buddies".
  10.  * At a high level, all that happens here is marking the table entry
  11.  * at the bottom level available, and propagating the changes upward
  12.  * as necessary, plus some accounting needed to play nicely with other
  13.  * parts of the VM system.
  14.  * At each level, we keep a list of pages, which are heads of continuous
  15.  * free pages of length of (1 << order) and marked with _mapcount
  16.  * PAGE_BUDDY_MAPCOUNT_VALUE. Page's order is recorded in page_private(page)
  17.  * field.
  18.  * So when we are allocating or freeing one, we can derive the state of the
  19.  * other. That is, if we allocate a small block, and both were
  20.  * free, the remainder of the region must be split into blocks.
  21.  * If a block is freed, and its buddy is also free, then this
  22.  * triggers coalescing into a block of larger size.
  23.  *
  24.  * -- nyc
  25.  */
  26.  
  27. static inline void __free_one_page(struct page *page,
  28.         struct zone *zone, unsigned int order,
  29.         int migratetype)
  30. {
  31.     unsigned long page_idx;
  32.     unsigned long combined_idx;
  33.     unsigned long uninitialized_var(buddy_idx);
  34.     struct page *buddy;
  35.  
  36.     VM_BUG_ON(!zone_is_initialized(zone));
  37.  
  38.     if (unlikely(PageCompound(page)))
  39.         if (unlikely(destroy_compound_page(page, order)))
  40.             return;
  41.  
  42.     VM_BUG_ON(migratetype == -1);
  43.  
  44.     page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
  45.  
  46.     VM_BUG_ON_PAGE(page_idx & ((1 << order) - 1), page);
  47.     VM_BUG_ON_PAGE(bad_range(zone, page), page);
  48.  
  49.     while (order < MAX_ORDER-1) {
  50.         buddy_idx = __find_buddy_index(page_idx, order);
  51.         buddy = page + (buddy_idx - page_idx);
  52.         if (!page_is_buddy(page, buddy, order))
  53.             break;
  54.         /*
  55.          * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page,
  56.          * merge with it and move up one order.
  57.          */
  58.         if (page_is_guard(buddy)) {
  59.             clear_page_guard_flag(buddy);
  60.             set_page_private(page, 0);
  61.             __mod_zone_freepage_state(zone, 1 << order,
  62.                           migratetype);
  63.         } else {
  64.             list_del(&buddy->lru);
  65.             zone->free_area[order].nr_free--;
  66.             rmv_page_order(buddy);
  67.         }
  68.         combined_idx = buddy_idx & page_idx;
  69.         page = page + (combined_idx - page_idx);
  70.         page_idx = combined_idx;
  71.         order++;
  72.     }
  73.     set_page_order(page, order);
  74.  
  75.     /*
  76.      * If this is not the largest possible page, check if the buddy
  77.      * of the next-highest order is free. If it is, it's possible
  78.      * that pages are being freed that will coalesce soon. In case,
  79.      * that is happening, add the free page to the tail of the list
  80.      * so it's less likely to be used soon and more likely to be merged
  81.      * as a higher order page
  82.      */
  83.     if ((order < MAX_ORDER-2) && pfn_valid_within(page_to_pfn(buddy))) {
  84.         struct page *higher_page, *higher_buddy;
  85.         combined_idx = buddy_idx & page_idx;
  86.         higher_page = page + (combined_idx - page_idx);
  87.         buddy_idx = __find_buddy_index(combined_idx, order + 1);
  88.         higher_buddy = higher_page + (buddy_idx - combined_idx);
  89.         if (page_is_buddy(higher_page, higher_buddy, order + 1)) {
  90.             list_add_tail(&page->lru,
  91.                 &zone->free_area[order].free_list[migratetype]);
  92.             goto out;
  93.         }
  94.     }
  95.  
  96.     list_add(&page->lru, &zone->free_area[order].free_list[migratetype]);
  97. out:
  98.     zone->free_area[order].nr_free++;
  99. }

while (order < MAX_ORDER-1)前面主要是对释放的页面进行检查校验操作。而while循环内,通过__find_buddy_index()获取与当前释放的页面处于同一阶的伙伴页面索引值,同时藉此索引值计算出伙伴页面地址,并做伙伴页面检查以确定其是否可以合并,若否则退出;接着if (page_is_guard(buddy))用于对页面的debug_flags成员做检查,由于未配置CONFIG_DEBUG_PAGEALLOCpage_is_guard()固定返回false;则剩下的操作主要就是将页面从分配链中摘除,同时将页面合并并将其处于的阶提升一级。

退出while循环后,通过set_page_order()设置页面最终可合并成为的管理阶。最后判断当前合并的页面是否为最大阶,否则将页面放至伙伴管理链表的末尾,避免其过早被分配,得以机会进一步与高阶页面进行合并。末了,将最后的挂入的阶的空闲计数加1

至此伙伴管理算法的页面释放完毕。

__free_pages_ok()的页面释放实现调用栈则是:

__free_pages_ok()

>free_one_page()

>__free_one_page()

殊途同归,最终还是__free_one_page()来释放,具体的过程就不再仔细分析了。

 

【篇外小记】

trace_mm_page_free()具体实现位置:

  1. 【file:/include/trace/event/kmem.h】
  2. TRACE_EVENT(mm_page_free,
  3.  
  4.     TP_PROTO(struct page *page, unsigned int order),
  5.  
  6.     TP_ARGS(page, order),
  7.  
  8.     TP_STRUCT__entry(
  9.         __field( struct page *, page )
  10.         __field( unsigned int, order )
  11.     ),
  12.  
  13.     TP_fast_assign(
  14.         __entry->page = page;
  15.         __entry->order = order;
  16.     ),
  17.  
  18.     TP_printk("page=%p pfn=%lu order=%d",
  19.             __entry->page,
  20.             page_to_pfn(__entry->page),
  21.             __entry->order)
  22. );

    其TRACE_EVENT()是一个宏,具体实现:

  1. 【file:/include/linux/tracepoint.h】
  2. #define TRACE_EVENT(name, proto, args, struct, assign, print) \
  3.     DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))

    继而查找DECLARE_TRACE()宏定义:

  1. 【file:/include/linux/tracepoint.h】
  2. #define DECLARE_TRACE(name, proto, args) \
  3.         __DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), 1, \
  4.                 PARAMS(void *__data, proto), \
  5.                 PARAMS(__data, args))

    最后由__DECLARE_TRACE()宏展开:

  1. 【file:/include/linux/tracepoint.h】
  2. #define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
  3.     extern struct tracepoint __tracepoint_##name; \
  4.     static inline void trace_##name(proto) \
  5.     { \
  6.         if (static_key_false(&__tracepoint_##name.key)) \
  7.             __DO_TRACE(&__tracepoint_##name, \
  8.                 TP_PROTO(data_proto), \
  9.                 TP_ARGS(data_args), \
  10.                 TP_CONDITION(cond),,); \
  11.     } \
  12.     __DECLARE_TRACE_RCU(name, PARAMS(proto), PARAMS(args), \
  13.         PARAMS(cond), PARAMS(data_proto), PARAMS(data_args)) \
  14.     static inline int \
  15.     register_trace_##name(void (*probe)(data_proto), void *data) \
  16.     { \
  17.         return tracepoint_probe_register(#name, (void *)probe, \
  18.                          data); \
  19.     } \
  20.     static inline int \
  21.     unregister_trace_##name(void (*probe)(data_proto), void *data) \
  22.     { \
  23.         return tracepoint_probe_unregister(#name, (void *)probe, \
  24.                            data); \
  25.     } \
  26.     static inline void \
  27.     check_trace_callback_type_##name(void (*cb)(data_proto)) \
  28.     { \
  29.     }

C语言中,宏里面的双井号“##”被称为连接符,是一种预处理运算符,用于把两个语言符号连接组合成单个语言符号。于是乎,tracename串起来则会成为trace_mm_page_free。类似这样的定义还特别多,大部分trace函数都是这么来的。值得注意的是__DECLARE_TRACE()不仅仅是定义实现了trace函数,同时还定义实现了trace函数的注册及去注册。

诸如此函数的还有trace_mm_page_pcpu_drain等函数。

阅读(3031) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~