{"id":4174,"date":"2012-09-02T19:09:52","date_gmt":"2012-09-02T11:09:52","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=4174"},"modified":"2012-09-25T17:55:20","modified_gmt":"2012-09-25T09:55:20","slug":"malloc%e4%b9%8b%e5%90%8e%ef%bc%8c%e5%86%85%e6%a0%b8%e5%8f%91%e7%94%9f%e4%ba%86%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/4174","title":{"rendered":"malloc()\u4e4b\u540e\uff0c\u5185\u6838\u53d1\u751f\u4e86\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p>\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u5e38\u89c1\u7684\u60c5\u51b5\uff1a\u7528\u6237\u8fdb\u7a0b\u8c03\u7528malloc()\u52a8\u6001\u5206\u914d\u4e86\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\uff0c\u518d\u5bf9\u8fd9\u5757\u5185\u5b58\u8fdb\u884c\u8bbf\u95ee\u3002\u8fd9\u4e9b\u7528\u6237\u7a7a\u95f4\u53d1\u751f\u7684\u4e8b\u4f1a\u5f15\u53d1\u5185\u6838\u7a7a\u95f4\u7684\u90a3\u4e9b\u53cd\u6620\uff1f\u672c\u6587\u5c06\u7b80\u5355\u4e3a\u60a8\u89e3\u7b54\u3002<\/p>\n<h3>1.brk\u7cfb\u7edf\u8c03\u7528\u670d\u52a1\u4f8b\u7a0b<\/h3>\n<p>malloc()\u662f\u4e00\u4e2aAPI\uff0c\u8fd9\u4e2a\u51fd\u6570\u5728\u5e93\u4e2d\u5c01\u88c5\u4e86\u7cfb\u7edf\u8c03\u7528brk\u3002\u56e0\u6b64\u5982\u679c\u8c03\u7528malloc\uff0c\u90a3\u4e48\u9996\u5148\u4f1a\u5f15\u53d1brk\u7cfb\u7edf\u8c03\u7528\u6267\u884c\u7684\u8fc7\u7a0b\u3002brk()\u5728\u5185\u6838\u4e2d\u5bf9\u5e94\u7684\u7cfb\u7edf\u8c03\u7528\u670d\u52a1\u4f8b\u7a0b\u4e3aSYSCALL_DEFINE1(brk, unsigned long, brk)\uff0c\u53c2\u6570brk\u7528\u6765\u6307\u5b9aheap\u6bb5\u65b0\u7684\u7ed3\u675f\u5730\u5740\uff0c\u4e5f\u5c31\u662f\u91cd\u65b0\u6307\u5b9amm_struct\u7ed3\u6784\u4e2d\u7684brk\u5b57\u6bb5\u3002<\/p>\n<p>brk\u7cfb\u7edf\u8c03\u7528\u670d\u52a1\u4f8b\u7a0b\u9996\u5148\u4f1a\u786e\u5b9aheap\u6bb5\u7684\u8d77\u59cb\u5730\u5740min_brk\uff0c\u7136\u540e\u518d\u68c0\u67e5\u8d44\u6e90\u7684\u9650\u5236\u95ee\u9898\u3002\u63a5\u7740\uff0c\u5c06\u65b0\u8001heap\u5730\u5740\u5206\u522b\u6309\u7167\u9875\u5927\u5c0f\u5bf9\u9f50\uff0c\u5bf9\u9f50\u540e\u7684\u5730\u5740\u5206\u522b\u5b58\u50a8\u4e0enewbrk\u548cokdbrk\u4e2d\u3002<\/p>\n<p>brk()\u7cfb\u7edf\u8c03\u7528\u672c\u8eab\u65e2\u53ef\u4ee5\u7f29\u5c0f\u5806\u5927\u5c0f\uff0c\u53c8\u53ef\u4ee5\u6269\u5927\u5806\u5927\u5c0f\u3002\u7f29\u5c0f\u5806\u8fd9\u4e2a\u529f\u80fd\u662f\u901a\u8fc7\u8c03\u7528do_munmap()\u5b8c\u6210\u7684\u3002\u5982\u679c\u8981\u6269\u5927\u5806\u7684\u5927\u5c0f\uff0c\u90a3\u4e48\u5fc5\u987b\u5148\u901a\u8fc7find_vma_intersection()\u68c0\u67e5\u6269\u5927\u4ee5\u540e\u7684\u5806\u662f\u5426\u4e0e\u5df2\u7ecf\u5b58\u5728\u7684\u67d0\u4e2a\u865a\u62df\u5185\u5b58\u91cd\u5408\uff0c\u5982\u4f55\u91cd\u5408\u5219\u76f4\u63a5\u9000\u51fa\u3002\u5426\u5219\uff0c\u8c03\u7528do_brk()\u8fdb\u884c\u63a5\u4e0b\u6765\u6269\u5927\u5806\u7684\u5404\u79cd\u5de5\u4f5c\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nSYSCALL_DEFINE1(brk, unsigned long, brk)\r\n{\r\n        unsigned long rlim, retval;\r\n        unsigned long newbrk, oldbrk;\r\n        struct mm_struct *mm = current-&gt;mm;\r\n        unsigned long min_brk;\r\n\r\n        down_write(&amp;mm-&gt;mmap_sem);\r\n\r\n#ifdef CONFIG_COMPAT_BRK\r\n        min_brk = mm-&gt;end_code;\r\n#else\r\n        min_brk = mm-&gt;start_brk;\r\n#endif\r\n        if (brk &lt; min_brk)\r\n                goto out;\r\n\r\n        rlim = rlimit(RLIMIT_DATA);\r\n        if (rlim &lt; RLIM_INFINITY &amp;&amp; (brk - mm-&gt;start_brk) +\r\n                        (mm-&gt;end_data - mm-&gt;start_data) &gt; rlim)\r\n\r\n        newbrk = PAGE_ALIGN(brk);\r\n        oldbrk = PAGE_ALIGN(mm-&gt;brk);\r\n        if (oldbrk == newbrk)\r\n                goto set_brk;\r\n\r\n        if (brk brk) {\r\n                if (!do_munmap(mm, newbrk, oldbrk-newbrk))\r\n                        goto set_brk;\r\n                goto out;\r\n        }\r\n\r\n        if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))\r\n                goto out;\r\n\r\n        if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)\r\n                goto out;\r\nset_brk:\r\n        mm-&gt;brk = brk;\r\nout:\r\n        retval = mm-&gt;brk;\r\n        up_write(&amp;mm-&gt;mmap_sem);\r\n        return retval;\r\n}\r\n<\/pre>\n<p>brk\u7cfb\u7edf\u8c03\u7528\u670d\u52a1\u4f8b\u7a0b\u6700\u540e\u5c06\u8fd4\u56de\u5806\u7684\u65b0\u7ed3\u675f\u5730\u5740\u3002<\/p>\n<h3>2.\u6269\u5927\u5806<\/h3>\n<p>\u7528\u6237\u8fdb\u7a0b\u8c03\u7528malloc()\u4f1a\u4f7f\u5f97\u5185\u6838\u8c03\u7528brk\u7cfb\u7edf\u8c03\u7528\u670d\u52a1\u4f8b\u7a0b\uff0c\u56e0\u4e3amalloc\u603b\u662f\u52a8\u6001\u7684\u5206\u914d\u5185\u5b58\u7a7a\u95f4\uff0c\u56e0\u6b64\u8be5\u670d\u52a1\u4f8b\u7a0b\u6b64\u65f6\u4f1a\u8fdb\u5165\u7b2c\u4e8c\u6761\u6267\u884c\u8def\u5f84\u4e2d\uff0c\u5373\u6269\u5927\u5806\u3002do_brk()\u4e3b\u8981\u5b8c\u6210\u4ee5\u4e0b\u5de5\u4f5c\uff1a<\/p>\n<p>1.\u901a\u8fc7get_unmapped_area()\u5728\u5f53\u524d\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u4e2d\u67e5\u627e\u4e00\u4e2a\u7b26\u5408len\u5927\u5c0f\u7684\u7ebf\u6027\u533a\u95f4\uff0c\u5e76\u4e14\u8be5\u7ebf\u6027\u533a\u95f4\u7684\u5fc5\u987b\u5728addr\u5730\u5740\u4e4b\u540e\u3002\u5982\u679c\u627e\u5230\u4e86\u8fd9\u4e2a\u7a7a\u95f2\u7684\u7ebf\u6027\u533a\u95f4\uff0c\u5219\u8fd4\u56de\u8be5\u533a\u95f4\u7684\u8d77\u59cb\u5730\u5740\uff0c\u5426\u5219\u8fd4\u56de\u9519\u8bef\u4ee3\u7801-ENOMEM\uff1b<\/p>\n<p>2.\u901a\u8fc7find_vma_prepare()\u5728\u5f53\u524d\u8fdb\u7a0b\u6240\u6709\u7ebf\u6027\u533a\u7ec4\u6210\u7684\u7ea2\u9ed1\u6811\u4e2d\u4f9d\u6b21\u904d\u5386\u6bcf\u4e2avma\uff0c\u4ee5\u786e\u5b9a\u4e0a\u4e00\u6b65\u627e\u5230\u7684\u65b0\u533a\u95f4\u4e4b\u524d\u7684\u7ebf\u6027\u533a\u5bf9\u8c61\u7684\u4f4d\u7f6e\u3002\u5982\u679caddr\u4f4d\u4e8e\u67d0\u4e2a\u73b0\u5b58\u7684vma\u4e2d\uff0c\u5219\u8c03\u7528do_munmap()\u5220\u9664\u8fd9\u4e2a\u7ebf\u6027\u533a\u3002\u5982\u679c\u5220\u9664\u6210\u529f\u5219\u7ee7\u7eed\u67e5\u627e\uff0c\u5426\u5219\u8fd4\u56de\u9519\u8bef\u4ee3\u7801\u3002<\/p>\n<p>3.\u76ee\u524d\u5df2\u7ecf\u627e\u5230\u4e86\u4e00\u4e2a\u5408\u9002\u5927\u5c0f\u7684\u7a7a\u95f2\u7ebf\u6027\u533a\uff0c\u63a5\u4e0b\u6765\u901a\u8fc7vma_merge()\u53bb\u8bd5\u7740\u5c06\u5f53\u524d\u7684\u7ebf\u6027\u533a\u4e0e\u4e34\u8fd1\u7684\u7ebf\u6027\u533a\u8fdb\u884c\u5408\u5e76\u3002\u5982\u679c\u5408\u5e76\u6210\u529f\uff0c\u90a3\u4e48\u8be5\u51fd\u6570\u5c06\u8fd4\u56deprev\u8fd9\u4e2a\u7ebf\u6027\u533a\u7684vm_area_struct\u7ed3\u6784\u6307\u9488\uff0c\u540c\u65f6\u7ed3\u675fdo_brk()\u3002\u5426\u5219\uff0c\u7ee7\u7eed\u5206\u914d\u65b0\u7684\u7ebf\u6027\u533a\u3002<\/p>\n<p>4.\u63a5\u4e0b\u6765\u901a\u8fc7kmem_cache_zalloc()\u5728\u7279\u5b9a\u7684slab\u9ad8\u901f\u7f13\u5b58vm_area_cachep\u4e2d\u4e3a\u8fd9\u4e2a\u7ebf\u6027\u533a\u5206\u914dvm_area_struct\u7ed3\u6784\u7684\u63cf\u8ff0\u7b26\u3002<\/p>\n<p>5.\u521d\u59cb\u5316vma\u7ed3\u6784\u4e2d\u7684\u5404\u4e2a\u5b57\u6bb5\u3002<\/p>\n<p>6.\u66f4\u65b0mm_struct\u7ed3\u6784\u4e2d\u7684vm_total\u5b57\u6bb5\uff0c\u5b83\u7528\u6765\u540c\u7ea7\u5f53\u524d\u8fdb\u7a0b\u6240\u62e5\u6709\u7684vma\u6570\u91cf\u3002<\/p>\n<p>7.\u5982\u679c\u5f53\u524dvma\u8bbe\u7f6e\u4e86VM_LOCKED\u5b57\u6bb5\uff0c\u90a3\u4e48\u901a\u8fc7mlock_vma_pages_range()\u7acb\u5373\u4e3a\u8fd9\u4e2a\u7ebf\u6027\u533a\u5206\u914d\u7269\u7406\u9875\u6846\u3002\u5426\u5219\uff0cdo_brk()\u7ed3\u675f\u3002<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0cdo_brk()\u4e3b\u8981\u662f\u4e3a\u5f53\u524d\u8fdb\u7a0b\u5206\u914d\u4e00\u4e2a\u65b0\u7684\u7ebf\u6027\u533a\uff0c\u5728\u6ca1\u6709\u8bbe\u7f6eVM_LOCKED\u6807\u5fd7\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u4e0d\u4f1a\u7acb\u523b\u4e3a\u8be5\u7ebf\u6027\u533a\u5206\u914d\u7269\u7406\u9875\u6846\uff0c\u800c\u662f\u901a\u8fc7vma\u4e00\u76f4\u5c06\u5206\u914d\u7269\u7406\u5185\u5b58\u7684\u5de5\u4f5c\u8fdb\u884c\u5ef6\u8fdf\uff0c\u76f4\u81f3\u53d1\u751f\u7f3a\u9875\u5f02\u5e38\u3002<\/p>\n<h3>3.\u7f3a\u9875\u5f02\u5e38\u7684\u5904\u7406\u8fc7\u7a0b<\/h3>\n<p>\u7ecf\u8fc7\u4e0a\u9762\u7684\u8fc7\u7a0b\uff0cmalloc()\u8fd4\u56de\u4e86\u7ebf\u6027\u5730\u5740\uff0c\u5982\u679c\u6b64\u65f6\u7528\u6237\u8fdb\u7a0b\u8bbf\u95ee\u8fd9\u4e2a\u7ebf\u6027\u5730\u5740\uff0c\u90a3\u4e48\u5c31\u4f1a\u53d1\u751f\u7f3a\u9875\u5f02\u5e38\uff08Page Fault\uff09\u3002\u6574\u4e2a\u7f3a\u9875\u5f02\u5e38\u7684\u5904\u7406\u8fc7\u7a0b\u975e\u5e38\u590d\u6742\uff0c\u6211\u4eec\u8fd9\u91cc\u53ea\u5173\u6ce8\u4e0emalloc()\u6709\u5173\u7684\u90a3\u4e00\u6761\u6267\u884c\u8def\u5f84\u3002<\/p>\n<p>\u5f53CPU\u4ea7\u751f\u4e00\u4e2a\u5f02\u5e38\u65f6\uff0c\u5c06\u4f1a\u8df3\u8f6c\u5230\u5f02\u5e38\u5904\u7406\u7684\u6574\u4e2a\u5904\u7406\u6d41\u7a0b\u4e2d\u3002\u5bf9\u4e8e\u7f3a\u9875\u5f02\u5e38\uff0cCPU\u5c06\u8df3\u8f6c\u5230page_fault\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u4e2d\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/\/linux-2.6.34\/arch\/x86\/kernel\/entry_32.S\r\nENTRY(page_fault)\r\n        RING0_EC_FRAME\r\n        pushl $do_page_fault\r\n        CFI_ADJUST_CFA_OFFSET 4\r\n        ALIGN\r\nerror_code:\r\n        \u2026\u2026\u2026\u2026\r\n        jmp ret_from_exception\r\n        CFI_ENDPROC\r\nEND(page_fault)\r\n<\/pre>\n<p>\u8be5\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u4f1a\u8c03\u7528do_page_fault()\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u901a\u8fc7\u8bfb\u53d6CR2\u5bc4\u5b58\u5668\u83b7\u5f97\u5f15\u8d77\u7f3a\u9875\u7684\u7ebf\u6027\u5730\u5740\uff0c\u901a\u8fc7\u5404\u79cd\u6761\u4ef6\u5224\u65ad\u4ee5\u4fbf\u786e\u5b9a\u4e00\u4e2a\u5408\u9002\u7684\u65b9\u6848\u6765\u5904\u7406\u8fd9\u4e2a\u5f02\u5e38\u3002<\/p>\n<h4>3.1.do_page_fault()<\/h4>\n<p>\u8be5\u51fd\u6570\u901a\u8fc7\u5404\u79cd\u6761\u4ef6\u6765\u68c0\u6d4b\u5f53\u524d\u53d1\u751f\u5f02\u5e38\u7684\u60c5\u51b5\uff0c\u4f46\u81f3\u5c11do_page_fault()\u4f1a\u533a\u5206\u51fa\u5f15\u53d1\u7f3a\u9875\u7684\u4e24\u79cd\u60c5\u51b5\uff1a\u7531\u7f16\u7a0b\u9519\u8bef\u5f15\u53d1\u5f02\u5e38\uff0c\u4ee5\u53ca\u7531\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u4e2d\u8fd8\u672a\u5206\u914d\u7269\u7406\u5185\u5b58\u7684\u7ebf\u6027\u5730\u5740\u5f15\u53d1\u3002\u5bf9\u4e8e\u540e\u4e00\u79cd\u60c5\u51b5\uff0c\u901a\u5e38\u8fd8\u5206\u4e3a\u7528\u6237\u7a7a\u95f4\u6240\u5f15\u53d1\u7684\u7f3a\u9875\u5f02\u5e38\u548c\u5185\u6838\u7a7a\u95f4\u5f15\u53d1\u7684\u7f3a\u9875\u5f02\u5e38\u3002<\/p>\n<p>\u5185\u6838\u5f15\u53d1\u7684\u5f02\u5e38\u662f\u7531vmalloc()\u4ea7\u751f\u7684\uff0c\u5b83\u53ea\u7528\u4e8e\u5185\u6838\u7a7a\u95f4\u5185\u5b58\u7684\u5206\u914d\u3002\u663e\u7136\uff0c\u6211\u4eec\u8fd9\u91cc\u9700\u8981\u5173\u6ce8\u7684\u662f\u7528\u6237\u7a7a\u95f4\u6240\u5f15\u53d1\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u8fd9\u90e8\u5206\u5de5\u4f5c\u4ecedo_page_fault()\u4e2d\u7684good_area\u6807\u53f7\u5904\u5f00\u59cb\u6267\u884c\uff0c\u4e3b\u8981\u901a\u8fc7handle_mm_fault()\u5b8c\u6210\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/\/linux-2.6.34\/arch\/x86\/mm\/fault.c\r\ndotraplinkage void __kprobes\r\ndo_page_fault(struct pt_regs *regs, unsigned long error_code)\r\n{\r\n\u2026\u2026 \u2026\u2026\r\ngood_area:\r\n        write = error_code &amp; PF_WRITE;\r\n\r\n        if (unlikely(access_error(error_code, write, vma))) {\r\n                bad_area_access_error(regs, error_code, address);\r\n                return;\r\n        }\r\n        fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);\r\n\u2026\u2026 \u2026\u2026\r\n}\r\n<\/pre>\n<h4>3.2.handle_mm_fault()<\/h4>\n<p>\u8be5\u51fd\u6570\u7684\u4e3b\u8981\u529f\u80fd\u662f\u4e3a\u5f15\u53d1\u7f3a\u9875\u7684\u8fdb\u7a0b\u5206\u914d\u4e00\u4e2a\u7269\u7406\u9875\u6846\uff0c\u5b83\u5148\u786e\u5b9a\u4e0e\u5f15\u53d1\u7f3a\u9875\u7684\u7ebf\u6027\u5730\u5740\u5bf9\u5e94\u7684\u5404\u7ea7\u9875\u76ee\u5f55\u9879\u662f\u5426\u5b58\u5728\uff0c\u5982\u4f55\u4e0d\u5b58\u5728\u5219\u5206\u8fdb\u884c\u5206\u914d\u3002\u5177\u4f53\u5982\u4f55\u5206\u914d\u8fd9\u4e2a\u9875\u6846\u662f\u901a\u8fc7\u8c03\u7528handle_pte_fault()\u5b8c\u6210\u7684\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nint handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,\r\n                unsigned long address, unsigned int flags)\r\n{\r\n        pgd_t *pgd;\r\n        pud_t *pud;\r\n        pmd_t *pmd;\r\n        pte_t *pte;\r\n        \u2026\u2026 \u2026\u2026\r\n        pgd = pgd_offset(mm, address);\r\n        pud = pud_alloc(mm, pgd, address);\r\n        if (!pud)\r\n                return VM_FAULT_OOM;\r\n        pmd = pmd_alloc(mm, pud, address);\r\n        if (!pmd)\r\n                return VM_FAULT_OOM;\r\n        pte = pte_alloc_map(mm, pmd, address);\r\n        if (!pte)\r\n                return VM_FAULT_OOM;\r\n          return handle_pte_fault(mm, vma, address, pte, pmd, flags);\r\n}\r\n<\/pre>\n<h4>3.3.handle_pte_fault()<\/h4>\n<p>\u8be5\u51fd\u6570\u6839\u636e\u9875\u8868\u9879pte\u6240\u63cf\u8ff0\u7684\u7269\u7406\u9875\u6846\u662f\u5426\u5728\u7269\u7406\u5185\u5b58\u4e2d\uff0c\u5206\u4e3a\u4e24\u5927\u7c7b\uff1a<\/p>\n<p>\u8bf7\u6c42\u8c03\u9875\uff1a\u88ab\u8bbf\u95ee\u7684\u9875\u6846\u4e0d\u518d\u4e3b\u5b58\u4e2d\uff0c\u90a3\u4e48\u6b64\u65f6\u5fc5\u987b\u5206\u914d\u4e00\u4e2a\u9875\u6846\u3002<\/p>\n<p>\u5199\u65f6\u590d\u5236\uff1a\u88ab\u8bbf\u95ee\u7684\u9875\u5b58\u5728\uff0c\u4f46\u662f\u8be5\u9875\u662f\u53ea\u8bfb\u7684\uff0c\u5185\u6838\u9700\u8981\u5bf9\u8be5\u9875\u8fdb\u884c\u5199\u64cd\u4f5c\uff0c\u6b64\u65f6\u5185\u6838\u5c06\u8fd9\u4e2a\u5df2\u5b58\u5728\u7684\u53ea\u8bfb\u9875\u4e2d\u7684\u6570\u636e\u590d\u5236\u5230\u4e00\u4e2a\u65b0\u7684\u9875\u6846\u4e2d\u3002<\/p>\n<p>\u7528\u6237\u8fdb\u7a0b\u8bbf\u95ee\u7531malloc()\u5206\u914d\u7684\u5185\u5b58\u7a7a\u95f4\u5c5e\u4e8e\u7b2c\u4e00\u79cd\u60c5\u51b5\u3002\u5bf9\u4e8e\u8bf7\u6c42\u8c03\u9875\uff0chandle_pte_fault()\u4ecd\u7136\u5c06\u5176\u7ec6\u5206\u4e3a\u4e09\u79cd\u60c5\u51b5\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstatic inline int handle_pte_fault(struct mm_struct *mm,\r\n                struct vm_area_struct *vma, unsigned long address,\r\n                pte_t *pte, pmd_t *pmd, unsigned int flags)\r\n{\r\n        \u2026\u2026 \u2026\u2026\r\n        if (!pte_present(entry)) {\r\n                if (pte_none(entry)) {\r\n                        if (vma-&gt;vm_ops) {\r\n                                if (likely(vma-&gt;vm_ops-&gt;fault))\r\n                                        return do_linear_fault(mm, vma, address,\r\n                                                pte, pmd, flags, entry);\r\n                        }\r\n                        return do_anonymous_page(mm, vma, address,\r\n                                                 pte, pmd, flags);\r\n                }\r\n                if (pte_file(entry))\r\n                        return do_nonlinear_fault(mm, vma, address,\r\n                                        pte, pmd, flags, entry);\r\n                return do_swap_page(mm, vma, address,\r\n                                        pte, pmd, flags, entry);\r\n        }\r\n\u2026\u2026 \u2026\u2026\r\n}\r\n<\/pre>\n<p>1.\u5982\u679c\u9875\u8868\u9879\u786e\u5b9e\u4e3a\u7a7a\uff08pte_none(entry)\uff09\uff0c\u90a3\u4e48\u5fc5\u987b\u5206\u914d\u9875\u6846\u3002\u5982\u679c\u5f53\u524d\u8fdb\u7a0b\u5b9e\u73b0\u4e86vma\u64cd\u4f5c\u51fd\u6570\u96c6\u5408\u4e2d\u7684fault\u94a9\u5b50\u51fd\u6570\uff0c\u90a3\u4e48\u8fd9\u79cd\u60c5\u51b5\u5c5e\u4e8e\u57fa\u4e8e\u6587\u4ef6\u7684\u5185\u5b58\u6620\u5c04\uff0c\u5b83\u8c03\u7528do_linear_fault()\u8fdb\u884c\u5206\u914d\u7269\u7406\u9875\u6846\u3002\u5426\u5219\uff0c\u5185\u6838\u5c06\u8c03\u7528\u9488\u5bf9\u533f\u540d\u6620\u5c04\u5206\u914d\u7269\u7406\u9875\u6846\u7684\u51fd\u6570do_anonymous_page()\u3002<\/p>\n<p>2.\u5982\u679c\u68c0\u6d4b\u51fa\u8be5\u9875\u8868\u9879\u4e3a\u975e\u7ebf\u6027\u6620\u5c04\uff08pte_file(entry)\uff09\uff0c\u5219\u8c03\u7528do_nonlinear_fault()\u5206\u914d\u7269\u7406\u9875\u3002<\/p>\n<p>3.\u5982\u679c\u9875\u6846\u4e8b\u5148\u88ab\u5206\u914d\uff0c\u4f46\u662f\u6b64\u523b\u5df2\u7ecf\u7531\u4e3b\u5b58\u6362\u51fa\u5230\u4e86\u5916\u5b58\uff0c\u5219\u8c03\u7528do_swap_page()\u5b8c\u6210\u9875\u6846\u5206\u914d\u3002<\/p>\n<p>\u7531malloc\u5206\u914d\u7684\u5185\u5b58\u5c06\u4f1a\u8c03\u7528do_anonymous_page()\u5206\u914d\u7269\u7406\u9875\u6846\u3002<\/p>\n<h4>3.4.do_anonymous_page()<\/h4>\n<p>\u6b64\u65f6\uff0c\u7f3a\u9875\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u7ec8\u4e8e\u8981\u4e3a\u5f53\u524d\u8fdb\u7a0b\u5206\u914d\u7269\u7406\u9875\u6846\u4e86\u3002\u5b83\u901a\u8fc7alloc_zeroed_user_highpage_movable()\u6765\u5b8c\u6210\u8fd9\u4e2a\u8fc7\u7a0b\u3002\u6211\u4eec\u5c42\u5c42\u62e8\u5f00\u8fd9\u4e2a\u51fd\u6570\u7684\u5916\u8863\uff0c\u53d1\u73b0\u5b83\u6700\u7ec8\u8c03\u7528\u4e86alloc_pages()\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstatic int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,\r\n                unsigned long address, pte_t *page_table, pmd_t *pmd,\r\n                unsigned int flags)\r\n{\r\n\u2026\u2026 \u2026\u2026\r\n        if (unlikely(anon_vma_prepare(vma)))\r\n                goto oom;\r\n        page = alloc_zeroed_user_highpage_movable(vma, address);\r\n        if (!page)\r\n                goto oom;\r\n\u2026\u2026 \u2026\u2026\r\n}\r\n<\/pre>\n<p>\u7ecf\u8fc7\u8fd9\u6837\u4e00\u4e2a\u590d\u6742\u7684\u8fc7\u7a0b\uff0c\u7528\u6237\u8fdb\u7a0b\u6240\u8bbf\u95ee\u7684\u7ebf\u6027\u5730\u5740\u7ec8\u4e8e\u5bf9\u5e94\u5230\u4e86\u4e00\u5757\u7269\u7406\u5185\u5b58\u3002<\/p>\n<p>\u53c2\u8003\uff1a<\/p>\n<p>1.\u300a\u6df1\u5165\u7406\u89e3LINUX\u5185\u6838\u300b<\/p>\n<p>2.\u300a\u6df1\u5165LINUX\u5185\u6838\u67b6\u6784\u300b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u5e38\u89c1\u7684\u60c5\u51b5\uff1a\u7528\u6237\u8fdb\u7a0b\u8c03\u7528malloc()\u52a8\u6001\u5206\u914d\u4e86\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\uff0c\u518d\u5bf9\u8fd9\u5757\u5185\u5b58\u8fdb\u884c\u8bbf\u95ee\u3002\u8fd9\u4e9b\u7528\u6237\u7a7a\u95f4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[266],"tags":[357,358,335],"class_list":["post-4174","post","type-post","status-publish","format-standard","hentry","category-266","tag-brk","tag-malloc","tag-335"],"views":47004,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=4174"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4174\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=4174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=4174"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=4174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}