Linux+page+cache+里的几个函数的源码分析

page cache 在linux vfs 中是比较重要的一层,其功能就不详细介绍了。主要介绍了几个关键性函数,容易帮助了解page cache里的整体逻辑和流程

先看一下page 的结构体

[cpp]  view plain copy
  1. /* 
  2.  * Each physical page in the system has a struct page associated with 
  3.  * it to keep track of whatever it is we are using the page for at the 
  4.  * moment. Note that we have no way to track which tasks are using 
  5.  * a page. 
  6.  */  
  7. struct page {  
  8.     unsigned long flags;        /* Atomic flags, some possibly 
  9.                      * updated asynchronously */  
  10.     atomic_t _count;        /* Usage count, see below. */  
  11.     atomic_t _mapcount;     /* Count of ptes mapped in mms, 
  12.                      * to show when page is mapped 
  13.                      * & limit reverse map searches. 
  14.                      */  
  15.     union {  
  16.         struct {  
  17.         unsigned long private;      /* Mapping-private opaque data: 
  18.                          * usually used for buffer_heads 
  19.                          * if PagePrivate set; used for 
  20.                          * swp_entry_t if PageSwapCache; 
  21.                          * indicates order in the buddy 
  22.                          * system if PG_buddy is set. 
  23.                          */  
  24.         struct address_space *mapping;  /* If low bit clear, points to 
  25.                          * inode address_space, or NULL. 
  26.                          * If page mapped as anonymous 
  27.                          * memory, low bit is set, and 
  28.                          * it points to anon_vma object: 
  29.                          * see PAGE_MAPPING_ANON below. 
  30.                          */  
  31.         };  
  32. #if NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS  
  33.         spinlock_t ptl;  
  34. #endif  
  35.     };  
  36.     pgoff_t index;          /* Our offset within mapping. */  
  37.     struct list_head lru;       /* Pageout list, eg. active_list 
  38.                      * protected by zone->lru_lock ! 
  39.                      */  
  40.     /* 
  41.      * On machines where all RAM is mapped into kernel address space, 
  42.      * we can simply calculate the virtual address. On machines with 
  43.      * highmem some memory is mapped into kernel virtual memory 
  44.      * dynamically, so we need a place to store that address. 
  45.      * Note that this field could be 16 bits on x86 ... ;) 
  46.      * 
  47.      * Architectures with slow multiplication can define 
  48.      * WANT_PAGE_VIRTUAL in asm/page.h 
  49.      */  
  50. #if defined(WANT_PAGE_VIRTUAL)  
  51.     void *virtual;          /* Kernel virtual address (NULL if 
  52.                        not kmapped, ie. highmem) */  
  53. #endif /* WANT_PAGE_VIRTUAL */  
  54. };  


 

page_cache_get() 主要是调用函数get_page

[cpp]  view plain copy
  1. static inline void get_page(struct page *page)  
  2. {  
  3.     if (unlikely(PageCompound(page)))  
  4.         page = (struct page *)page_private(page);  
  5.     atomic_inc(&page->_count);  
  6. }  

主要page里的计数器+1,表示page引用的reference 次数

 

page_cache_release() 的核心函数 put_page_testzero

[java]  view plain copy
  1. static inline int put_page_testzero(struct page *page)  
  2. {  
  3.     BUG_ON(atomic_read(&page->_count) == 0);  
  4.     return atomic_dec_and_test(&page->_count);  
  5. }  

显然是page的计数器-1, page的引用被释放

 

page 的flags 参数, 在page 的结构体里定义了flags参数,用bit位来标识page的状态,定义在page-flags.h文件里

这是在32位机 和 64位 系统的关于flags 定义

 32 bit  -------------------------------| FIELDS |       FLAGS         |
 64 bit  |           FIELDS             | ??????         FLAGS         |
            63                                  32                              0

从bit0-bit19是常用的,其他位保留给了mapping zone, node and SPARSEMEM 

[cpp]  view plain copy
  1. #define PG_locked        0  /* Page is locked. Don't touch. */  
  2. #define PG_error         1  
  3. #define PG_referenced        2  
  4. #define PG_uptodate      3  
  5.   
  6. #define PG_dirty         4  
  7. #define PG_lru           5  
  8. #define PG_active        6  
  9. #define PG_slab          7  /* slab debug (Suparna wants this) */  
  10.   
  11. #define PG_checked       8  /* kill me in 2.5.<early>. */  
  12. #define PG_arch_1        9  
  13. #define PG_reserved     10  
  14. #define PG_private      11  /* Has something at ->private */  
  15.   
  16. #define PG_writeback        12  /* Page is under writeback */  
  17. #define PG_nosave       13  /* Used for system suspend/resume */  
  18. #define PG_compound     14  /* Part of a compound page */  
  19. #define PG_swapcache        15  /* Swap page: swp_entry_t in private */  
  20.   
  21. #define PG_mappedtodisk     16  /* Has blocks allocated on-disk */  
  22. #define PG_reclaim      17  /* To be reclaimed asap */  
  23. #define PG_nosave_free      18  /* Free, should not be written */  
  24. #define PG_buddy        19  /* Page is free, on buddy lists */  

 

SetPageUptodate 原子设置bit PG_uptodate 状态为1,表示改页被更新

#define SetPageUptodate(page) set_bit(PG_uptodate, &(page)->flags)

 

ClearPageUptodate 原子设置bit PG_uptodate 状态为0,表示页没有被更新

#define ClearPageUptodate(page) clear_bit(PG_uptodate, &(page)->flags)

 


TestSetPageLocked 设置原子设置page locked状态,并返回改变前的原来状态

[java]  view plain copy
  1. #define TestSetPageLocked(page)     \  
  2.         test_and_set_bit(PG_locked, &(page)->flags)  


 

__lock_page 函数

[cpp]  view plain copy
  1. void fastcall __lock_page(struct page *page)  
  2. {  
  3.     DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);  
  4.   
  5.     __wait_on_bit_lock(page_waitqueue(page), &wait, sync_page,  
  6.                             TASK_UNINTERRUPTIBLE);  
  7. }  
  8. EXPORT_SYMBOL(__lock_page);  

将当前进程设置成Task_uninterruptible状态,并将进程挂载到 wait对队列中,如果PG_Locked的状态为1时,触发sync_page的方法,只有在sync_page方法中才会调用schedule()调度当前进程,直到PG_locked的状态为0,注意当执行完__wait_on_bit_lock  的时候PG_locked仍然是1,因为__wait_on_bit_lock是用test_and_set_bit来进行while条件判断的,最后将进程设置成 TASK_RUNNING 状态,把该进程从wait 队列中移除。 

 

unlock_page 函数

[cpp]  view plain copy
  1. void fastcall unlock_page(struct page *page)  
  2. {  
  3.     smp_mb__before_clear_bit();  
  4.     if (!TestClearPageLocked(page))  
  5.         BUG();  
  6.     smp_mb__after_clear_bit();   
  7.     wake_up_page(page, PG_locked);  
  8. }  
  9. EXPORT_SYMBOL(unlock_page);  


设置PG_Locked 的状态是0,遍历等待队列,执行唤醒函数

[cpp]  view plain copy
  1. static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,  
  2.                  int nr_exclusive, int sync, void *key)  
  3. {  
  4.     struct list_head *tmp, *next;  
  5.   
  6.     list_for_each_safe(tmp, next, &q->task_list) {  
  7.         wait_queue_t *curr = list_entry(tmp, wait_queue_t, task_list);  
  8.         unsigned flags = curr->flags;  
  9.   
  10.         if (curr->func(curr, mode, sync, key) &&  
  11.                 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)  
  12.             break;  
  13.     }  
  14. }  

其中func的定义是    

[cpp]  view plain copy
  1. .func       = autoremove_wake_function,  

在autoremove_wake_function里,调用sched.c 的default_wake_function -> try_to_wake_up

将等待队列里的线程状态置为 TASK_RUNNING 并放置到运行队列中去

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值