转自:http://blog.csdn.net/vanbreaker/article/details/7955713

版权声明:本文为博主原创文章,未经博主允许不得转载。

在pte_handle_fault()中,如果触发异常的页存在于主存中,那么该异常往往是由写了一个只读页触发的,此时需要进行COW(写时复制操作)。如当一个父进程通过fork()创建了一个子进程时,子进程将会共享父进程的页框。之后,无论是父进程还是子进程要对相应的内存进行写操作,都要进行COW,也就是为自己重新分配一个页框,并把之前的数据复制到页框中去,再写。

  1. static inline int handle_pte_fault(struct mm_struct *mm,
  2. struct vm_area_struct *vma, unsigned long address,
  3. pte_t *pte, pmd_t *pmd, unsigned int flags)
  4. {
  5. pte_t entry;
  6. spinlock_t *ptl;
  7. entry = *pte;
  8. ...
  9. ...
  10. ...
  11. /********页在主存中的情况***********/
  12. ptl = pte_lockptr(mm, pmd);
  13. spin_lock(ptl);
  14. if (unlikely(!pte_same(*pte, entry)))
  15. goto unlock;
  16. if (flags & FAULT_FLAG_WRITE) {//异常由写访问触发
  17. if (!pte_write(entry))//而对应的页是不可写的
  18. return do_wp_page(mm, vma, address, //此时必须进行写时复制的操作
  19. pte, pmd, ptl, entry);
  20. entry = pte_mkdirty(entry);
  21. }
  22. entry = pte_mkyoung(entry);
  23. if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) {
  24. update_mmu_cache(vma, address, entry);
  25. } else {
  26. /*
  27. * This is needed only for protection faults but the arch code
  28. * is not yet telling us if this is a protection fault or not.
  29. * This still avoids useless tlb flushes for .text page faults
  30. * with threads.
  31. */
  32. if (flags & FAULT_FLAG_WRITE)
  33. flush_tlb_page(vma, address);
  34. }
  35. unlock:
  36. pte_unmap_unlock(pte, ptl);
  37. return 0;
  38. }

可以看到,hand_pte_fault()函数处理页存在于主存中的情况的关键操作都集中在do_wp_page()函数上。该函数是用来处理COW的,不过在COW之前先要做一些检查,比如说,如果对应的页只有一个进程使用,那么便可以直接修改页的权限为可读可写,而不进行COW。总之,不到不得以的情况下是不会进行COW的。

  1. static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2. unsigned long address, pte_t *page_table, pmd_t *pmd,
  3. spinlock_t *ptl, pte_t orig_pte)
  4. {
  5. struct page *old_page, *new_page;
  6. pte_t entry;
  7. int reuse = 0, ret = 0;
  8. int page_mkwrite = 0;
  9. struct page *dirty_page = NULL;
  10. old_page = vm_normal_page(vma, address, orig_pte);//获取共享页
  11. if (!old_page) {//获取共享页失败
  12. /*
  13. * VM_MIXEDMAP !pfn_valid() case
  14. *
  15. * We should not cow pages in a shared writeable mapping.
  16. * Just mark the pages writable as we can't do any dirty
  17. * accounting on raw pfn maps.
  18. */
  19. /*如果vma的映射本来就是共享且可写的,则跳转至reuse直接使用orig_pte对应的页*/
  20. if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
  21. (VM_WRITE|VM_SHARED))
  22. goto reuse;
  23. /*否则跳转至gotten分配一个页*/
  24. goto gotten;
  25. }
  26. /*
  27. * Take out anonymous pages first, anonymous shared vmas are
  28. * not dirty accountable.
  29. */
  30. /*下面首先判断匿名页的情况,如果old_page是匿名页,并且只有一个进程使用它(reuse为1),则
  31. 则直接使用该页*/
  32. if (PageAnon(old_page) && !PageKsm(old_page)) {
  33. /*这里先判断是否有其他进程竞争,修改了页表*/
  34. if (!trylock_page(old_page)) {
  35. page_cache_get(old_page);
  36. pte_unmap_unlock(page_table, ptl);
  37. lock_page(old_page);
  38. page_table = pte_offset_map_lock(mm, pmd, address,
  39. &ptl);
  40. if (!pte_same(*page_table, orig_pte)) {
  41. unlock_page(old_page);
  42. page_cache_release(old_page);
  43. goto unlock;
  44. }
  45. page_cache_release(old_page);
  46. }
  47. /*确定没有其他进程竞争,则进行reuse判断,通过reuse_swap_page()函数判断
  48. old_page的_mapcount字段是否为0,是的话则表明只有一个进程使用该匿名页*/
  49. reuse = reuse_swap_page(old_page);
  50. unlock_page(old_page);
  51. } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
  52. (VM_WRITE|VM_SHARED))) {//如果vma的映射本来就是共享且可写的
  53. /*
  54. * Only catch write-faults on shared writable pages,
  55. * read-only shared pages can get COWed by
  56. * get_user_pages(.write=1, .force=1).
  57. */
  58. if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
  59. struct vm_fault vmf;
  60. int tmp;
  61. vmf.virtual_address = (void __user *)(address &
  62. PAGE_MASK);
  63. vmf.pgoff = old_page->index;
  64. vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
  65. vmf.page = old_page;
  66. /*
  67. * Notify the address space that the page is about to
  68. * become writable so that it can prohibit this or wait
  69. * for the page to get into an appropriate state.
  70. *
  71. * We do this without the lock held, so that it can
  72. * sleep if it needs to.
  73. */
  74. page_cache_get(old_page);//增加old_page的引用计数作为保护
  75. pte_unmap_unlock(page_table, ptl);
  76. /*这里通知即将修改页的权限*/
  77. tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
  78. /*如果无法修改的话,则跳转到unwritable_page*/
  79. if (unlikely(tmp &
  80. (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
  81. ret = tmp;
  82. goto unwritable_page;
  83. }
  84. if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
  85. lock_page(old_page);
  86. if (!old_page->mapping) {
  87. ret = 0; /* retry the fault */
  88. unlock_page(old_page);
  89. goto unwritable_page;
  90. }
  91. } else
  92. VM_BUG_ON(!PageLocked(old_page));
  93. /*
  94. * Since we dropped the lock we need to revalidate
  95. * the PTE as someone else may have changed it.  If
  96. * they did, we just return, as we can count on the
  97. * MMU to tell us if they didn't also make it writable.
  98. */
  99. /*走到这里表示已经成功修改了页的权限了,这里同样重新获取页表,判断是否和之前一致*/
  100. page_table = pte_offset_map_lock(mm, pmd, address,
  101. &ptl);
  102. if (!pte_same(*page_table, orig_pte)) {
  103. unlock_page(old_page);
  104. page_cache_release(old_page);
  105. goto unlock;
  106. }
  107. page_mkwrite = 1;
  108. }
  109. dirty_page = old_page;
  110. get_page(dirty_page);
  111. reuse = 1;
  112. }
  113. if (reuse) {//reuse处理,也就是说不进行COW,可以直接在old_page上进行写操作
  114. reuse:
  115. flush_cache_page(vma, address, pte_pfn(orig_pte));
  116. entry = pte_mkyoung(orig_pte);//标记_PAGE_ACCESSED位
  117. entry = maybe_mkwrite(pte_mkdirty(entry), vma);//将页的权限修改为可读可写,并且标记为脏页
  118. if (ptep_set_access_flags(vma, address, page_table, entry,1))
  119. update_mmu_cache(vma, address, entry);
  120. ret |= VM_FAULT_WRITE;
  121. goto unlock;
  122. }
  123. /*
  124. * Ok, we need to copy. Oh, well..
  125. */
  126. /***************终于走到了不得已的一步了,下面只好进行COW了********************/
  127. page_cache_get(old_page);
  128. gotten:
  129. pte_unmap_unlock(page_table, ptl);
  130. if (unlikely(anon_vma_prepare(vma)))
  131. goto oom;
  132. if (is_zero_pfn(pte_pfn(orig_pte))) {
  133. new_page = alloc_zeroed_user_highpage_movable(vma, address);//分配一个零页面
  134. if (!new_page)
  135. goto oom;
  136. } else {
  137. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);//分配一个非零页面
  138. if (!new_page)
  139. goto oom;
  140. cow_user_page(new_page, old_page, address, vma);//将old_page中的数据拷贝到new_page
  141. }
  142. __SetPageUptodate(new_page);
  143. /*
  144. * Don't let another task, with possibly unlocked vma,
  145. * keep the mlocked page.
  146. */
  147. if ((vma->vm_flags & VM_LOCKED) && old_page) {
  148. lock_page(old_page);    /* for LRU manipulation */
  149. clear_page_mlock(old_page);
  150. unlock_page(old_page);
  151. }
  152. if (mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))
  153. goto oom_free_new;
  154. /*
  155. * Re-check the pte - we dropped the lock
  156. */
  157. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  158. if (likely(pte_same(*page_table, orig_pte))) {
  159. if (old_page) {
  160. if (!PageAnon(old_page)) {
  161. dec_mm_counter(mm, file_rss);
  162. inc_mm_counter(mm, anon_rss);
  163. }
  164. } else
  165. inc_mm_counter(mm, anon_rss);
  166. flush_cache_page(vma, address, pte_pfn(orig_pte));
  167. entry = mk_pte(new_page, vma->vm_page_prot);//获取new_page的pte
  168. entry = maybe_mkwrite(pte_mkdirty(entry), vma);//修改new_page的权限
  169. /*
  170. * Clear the pte entry and flush it first, before updating the
  171. * pte with the new entry. This will avoid a race condition
  172. * seen in the presence of one thread doing SMC and another
  173. * thread doing COW.
  174. */
  175. ptep_clear_flush(vma, address, page_table);
  176. page_add_new_anon_rmap(new_page, vma, address);
  177. /*
  178. * We call the notify macro here because, when using secondary
  179. * mmu page tables (such as kvm shadow page tables), we want the
  180. * new page to be mapped directly into the secondary page table.
  181. */
  182. set_pte_at_notify(mm, address, page_table, entry);
  183. update_mmu_cache(vma, address, entry);
  184. if (old_page) {
  185. /*
  186. * Only after switching the pte to the new page may
  187. * we remove the mapcount here. Otherwise another
  188. * process may come and find the rmap count decremented
  189. * before the pte is switched to the new page, and
  190. * "reuse" the old page writing into it while our pte
  191. * here still points into it and can be read by other
  192. * threads.
  193. *
  194. * The critical issue is to order this
  195. * page_remove_rmap with the ptp_clear_flush above.
  196. * Those stores are ordered by (if nothing else,)
  197. * the barrier present in the atomic_add_negative
  198. * in page_remove_rmap.
  199. *
  200. * Then the TLB flush in ptep_clear_flush ensures that
  201. * no process can access the old page before the
  202. * decremented mapcount is visible. And the old page
  203. * cannot be reused until after the decremented
  204. * mapcount is visible. So transitively, TLBs to
  205. * old page will be flushed before it can be reused.
  206. */
  207. page_remove_rmap(old_page);
  208. }
  209. /* Free the old page.. */
  210. new_page = old_page;
  211. ret |= VM_FAULT_WRITE;
  212. } else
  213. mem_cgroup_uncharge_page(new_page);
  214. if (new_page)
  215. page_cache_release(new_page);
  216. if (old_page)
  217. page_cache_release(old_page);
  218. unlock:
  219. pte_unmap_unlock(page_table, ptl);
  220. if (dirty_page) {
  221. /*
  222. * Yes, Virginia, this is actually required to prevent a race
  223. * with clear_page_dirty_for_io() from clearing the page dirty
  224. * bit after it clear all dirty ptes, but before a racing
  225. * do_wp_page installs a dirty pte.
  226. *
  227. * do_no_page is protected similarly.
  228. */
  229. if (!page_mkwrite) {
  230. wait_on_page_locked(dirty_page);
  231. set_page_dirty_balance(dirty_page, page_mkwrite);
  232. }
  233. put_page(dirty_page);
  234. if (page_mkwrite) {
  235. struct address_space *mapping = dirty_page->mapping;
  236. set_page_dirty(dirty_page);
  237. unlock_page(dirty_page);
  238. page_cache_release(dirty_page);
  239. if (mapping)    {
  240. /*
  241. * Some device drivers do not set page.mapping
  242. * but still dirty their pages
  243. */
  244. balance_dirty_pages_ratelimited(mapping);
  245. }
  246. }
  247. /* file_update_time outside page_lock */
  248. if (vma->vm_file)
  249. file_update_time(vma->vm_file);
  250. }
  251. return ret;
  252. oom_free_new:
  253. page_cache_release(new_page);
  254. oom:
  255. if (old_page) {
  256. if (page_mkwrite) {
  257. unlock_page(old_page);
  258. page_cache_release(old_page);
  259. }
  260. page_cache_release(old_page);
  261. }
  262. return VM_FAULT_OOM;
  263. unwritable_page:
  264. page_cache_release(old_page);
  265. return ret;
  266. }

用户空间缺页异常pte_handle_fault()分析--(下)--写时复制【转】的更多相关文章

  1. 用户空间缺页异常pte_handle_fault()分析--(上)【转】

    转自:http://blog.csdn.net/vanbreaker/article/details/7881206 版权声明:本文为博主原创文章,未经博主允许不得转载. 前面简单的分析了内核处理用户 ...

  2. fork()和写时复制

    写时复制技术最初产生于Unix系统,用于实现一种傻瓜式的进程创建:当发出fork(  )系统调用时,内核原样复制父进程的整个地址空间并把复制的那一份分配给子进程.这种行为是非常耗时的,因为它需要: · ...

  3. Linux的fork()写时复制原则(转)

    写时复制技术最初产生于Unix系统,用于实现一种傻瓜式的进程创建:当发出fork(  )系统调用时,内核原样复制父进程的整个地址空间并把复制的那一份分配给子进程.这种行为是非常耗时的,因为它需要: · ...

  4. Linux进程管理——fork()和写时复制

    写时复制技术最初产生于Unix系统,用于实现一种傻瓜式的进程创建:当发出fork(  )系统调用时,内核原样复制父进程的整个地址空间并把复制的那一份分配给子进程.这种行为是非常耗时的,因为它需要: · ...

  5. 写时复制和fork,vfork,clone

    写时复制 原理: 用了“引用计数”,会有一个变量用于保存引用的数量.当第一个类构造时,string的构造函数会根据传入的参数从堆上分配内存,当有其它类需要这块内存时,这个计数为自动累加,当有类析构时, ...

  6. Redis持久化之父子进程与写时复制

    之所以将Linux底层的写时复制技术放在Redis篇幅下,是因为Redis进行RDB持久化时,BGSAVE(后面称之为"后台保存")会开辟一个子进程,将数据从内存写进磁盘,这儿我产 ...

  7. php 垃圾回收机制----写时复制和引用计数

    PHP使用引用计数和写时复制来管理内存.写时复制保证了变量间复制值不浪费内存,引用计数保证了当变量不再需要时,将内存释放给操作系统. 要理解PHP内存管理,首先要理解一个概念----符号表. 符号表的 ...

  8. [转]QVector与QByteArray——Qt的写时复制(copy on write)技术

    我们在之前的博文QVector的内存分配策略与再谈QVector与std::vector——使用装饰者让std::vector支持连续赋值中简单聊了聊QVector内存分配和赋值方面的一点东西,今天接 ...

  9. Java进阶知识点6:并发容器背后的设计理念 - 锁分段、写时复制和弱一致性

    一.背景 容器是Java编程中使用频率很高的组件,但Java默认提供的基本容器(ArrayList,HashMap等)均不是线程安全的.当容器和多线程并发编程相遇时,程序员又该何去何从呢? 通常有两种 ...

随机推荐

  1. 遗传算法 | Java版GA_TSP(我的第一个Java程序)

    嗯哼,第一次写博客,准确说是第一次通过文字的方式记录自己的工作,闲话少叙,技术汪的博客就该直奔技术主题(关于排版问题,会在不断写博客的过程中慢慢学习,先将就着用吧,重在技术嘛~~~). 遗传算法(Ge ...

  2. Servlet过滤器---编码转换过滤器

    该实例用于将请求与相应的编码设置为当前网站的默认编码 java类: import java.io.IOException; import javax.servlet.Filter; import ja ...

  3. 1-Linux运维人员要求

    linux 运维: 1.linux基础操作命令2.linux基础服务搭建3.文本处理命令4.shell脚本编程 python perl php5.数据库 mysql oracle6.lvs集群 热备 ...

  4. Win7更换锁屏和开机画面

    技术交流群:233513714 每次开机被Windows千年不变的开机画面和锁屏画面丑到的小伙伴们可以看过来,通过简单的几步就可以改掉系统默认的开机画面. 1.首先Windows+r键输入regedi ...

  5. Redis数据更新

    技术交流群: 233513714

  6. 通俗版解释网关,IP地址,ARP欺骗,DDOS攻击

    计算机主机网关的作用是什么? 假设你的名字叫小不点,你住在一个大院子里,你的邻居有很多小伙伴,在门口传达室还有个看大门的李大爷,李大爷就是你的网关.当你想跟院子里的某个小伙伴玩,只要你在院子里大喊一声 ...

  7. ubuntu上通用解压方式

    ubuntu上通用解压方式 tar xvf *.bin.tar.gz,gz,tar.tgz

  8. 每天一个Linux命令(8):chmod命令

    chmod命令用来变更文件或目录的权限. 权限范围的表示法如下: u   User,即文件或目录的拥有者:g  Group,即文件或目录的所属群组:o   Other,除了文件或目录拥有者或所属群组之 ...

  9. python练习题及实现--文件处理、date日期

    练习题作者:Vamei 出处:http://www.cnblogs.com/vamei http://www.cnblogs.com/vamei/archive/2012/07/19/2600135. ...

  10. CCF-NOIP-2018 提高组(复赛) 模拟试题(一)

    T1 帽子戏法 问题描述 小 Y 有一个\(n*n*n\)的"帽子立方体" ,即一个\(n\)层的立方体,每层的帽子都 可以排成\(n*n\)的矩阵. "帽子立方体&qu ...