{"id":3537,"date":"2011-11-21T18:04:39","date_gmt":"2011-11-21T10:04:39","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=3537"},"modified":"2011-11-22T09:25:32","modified_gmt":"2011-11-22T01:25:32","slug":"%e9%9d%9e%e8%bf%9e%e7%bb%ad%e5%86%85%e5%ad%98%e5%8c%ba%e5%92%8cvmalloc","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/3537","title":{"rendered":"vmalloc()\u7684\u57fa\u672c\u5b9e\u73b0"},"content":{"rendered":"<p>\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u62e5\u6709\u4e00\u6bb5\u8fde\u7eed\u800c\u4e14\u5e73\u5766\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\uff0c\u8fd9\u6bb5\u8fde\u7eed\u7684\u7a7a\u95f4\u88ab\u5212\u5206\u4e3a\u4e24\u5927\u90e8\u5206\uff1a\u7528\u6237\u7a7a\u95f4\u548c\u5185\u6838\u7a7a\u95f4\u3002\u5728x86-32\u67b6\u6784\u4e0b\uff0c\u7528\u6237\u7a7a\u95f4\u5360\u636e\u6700\u4f4e\u7aef\u76843G\uff0c\u5185\u6838\u7a7a\u95f4\u5360\u636e\u6700\u9ad8\u76841G\u3002\u4e8b\u5b9e\u4e0a\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u5e76\u4e0d\u4f1a\u540c\u65f6\u4f7f\u7528\u6389\u6574\u4e2a3G\u7684\u5730\u5740\u7a7a\u95f4\uff0c\u56e0\u6b64\u6574\u4e2a\u7528\u6237\u7a7a\u95f4\u53c8\u8fdb\u4e00\u6b65\u88ab\u5212\u5206\u4e3a\u82e5\u5e72\u4e2a\u865a\u62df\u5185\u5b58\u533a\u57df(struct vm_area_struct)\uff0c\u6bcf\u4e2a\u5185\u5b58\u533a\u57df\u90fd\u6709\u76f8\u5e94\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u5e76\u4e14\u9488\u5bf9\u5f53\u524d\u7684\u5185\u5b58\u533a\u57df\u8fd8\u6709\u5177\u4f53\u7684\u64cd\u4f5c\u51fd\u6570\u3002<\/p>\n<p>\u5bf9\u4e8e\u5185\u6838\u7a7a\u95f4\u800c\u8a00\uff0c\u6839\u636e\u4e0d\u540c\u7684\u6620\u5c04\u89c4\u5219\uff0c\u6574\u4e2a\u5185\u6838\u7a7a\u95f4\u5212\u5206\u4e3a\u56db\u5927\u90e8\u5206\uff1a\u7269\u7406\u5185\u5b58\u6620\u5c04\u533a\u3001vmalloc\u533a\u3001\u6c38\u4e45\u5185\u6838\u6620\u5c04\u533a\u548c\u56fa\u5b9a\u6620\u5c04\u7684\u7ebf\u6027\u5730\u5740\u533a\u57df\u3002\u5185\u6838\u7a7a\u95f4\u7684\u6620\u5c04\u60c5\u51b5\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p>\u5176\u4e2dvmalloc\u533a(struct vm_struct)\u8ddf\u7528\u6237\u7a7a\u95f4\u7684\u865a\u62df\u5185\u5b58\u533a\u57df\u6709\u4e9b\u7c7b\u4f3c\uff0c\u5b83\u4eec\u90fd\u662f\u5229\u7528\u5206\u6563\u7684\u7269\u7406\u9875\u6846\u6784\u5efa\u8fde\u7eed\u7684\u865a\u62df\u5730\u5740\u533a\u95f4\u3002<\/p>\n<h3>\u975e\u8fde\u7eed\u5185\u5b58\u533a\u7684\u6570\u636e\u7ed3\u6784<\/h3>\n<p>vmalloc\u533a\u4e5f\u88ab\u79f0\u4e3a\u975e\u8fde\u7eed\u5185\u5b58\u533a\u57df\uff0c\u6574\u4e2a\u975e\u8fde\u7eed\u5185\u5b58\u533a\u7684\u8d77\u59cb\u5730\u5740\u5b9a\u4e49\u4e3aVMALLOC_START\u5b8f\uff0c\u7ed3\u675f\u5730\u5740\u5b9a\u4e49\u4e3aVMALLOC_END\u5b8f\u3002\u5b83\u7531\u82e5\u5e72\u4e2avmalloc\u533a\u7ec4\u6210\uff0c\u6bcf\u4e2avmalloc\u533a\u4e4b\u95f4\u95f4\u96944KB\uff0c\u8fd9\u662f\u4e3a\u4e86\u9632\u6b62\u975e\u6cd5\u7684\u5185\u5b58\u8bbf\u95ee\u3002\u5185\u6838\u4e2d\u4f7f\u7528vm_struct\u7ed3\u6784\u6765\u8868\u793a\u6bcf\u4e2avmalloc\u533a\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u6bcf\u6b21\u8c03\u7528vmalloc()\u51fd\u6570\u5728\u5185\u6838\u4e2d\u7533\u8bf7\u4e00\u6bb5\u8fde\u7eed\u7684\u5185\u5b58\u540e\uff0c\u90fd\u5bf9\u5e94\u7740\u4e00\u4e2avm_struct\uff0c\u7cfb\u7edf\u4e2d\u6240\u6709\u7684vmalloc\u533a\u7ec4\u6210\u4e00\u4e2a\u94fe\u8868\uff0c\u94fe\u8868\u5934\u6307\u9488\u4e3avmlist\u3002vm_sttruct\u7ed3\u6784\u5728\u6700\u65b0\u5185\u6838\u6e90\u7801\u7684\u63cf\u8ff0\u5982\u4e0b(\u672c\u6587\u6240\u6d89\u53ca\u7684\u5185\u6838\u6e90\u7801\u5747\u6765\u81eav3.0.4)\uff1a<\/p>\n<pre class=\"brush:c\">struct vm_struct {\r\n        struct vm_struct        *next;\r\n        void                    *addr;\r\n        unsigned long           size;\r\n        unsigned long           flags;\r\n        struct page             **pages;\r\n        unsigned int            nr_pages;\r\n        unsigned long           phys_addr;\r\n        void                    *caller;\r\n};<\/pre>\n<p>\u4e0b\u9762\u662f\u8fd9\u4e2a\u7ed3\u6784\u4e2d\u5404\u4e2a\u5b57\u6bb5\u7684\u89e3\u91ca\uff1a<\/p>\n<p>next\uff1a\u6240\u6709\u7684vm_struct\u7ed3\u6784\u7ec4\u6210\u4e00\u4e2avmlist\u94fe\u8868\uff0c\u8be5\u5b57\u6bb5\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\uff1b<\/p>\n<p>addr\uff1avmalloc()\u6700\u7ec8\u662f\u5728\u5185\u6838\u7a7a\u95f4\u4e2d\u7533\u8bf7\u4e00\u4e2a\u5185\u5b58\u533a\u57df\uff0caddr\u4ee3\u8868\u8fd9\u6bb5\u5b50\u533a\u57df\u7684\u8d77\u59cb\u5730\u5740\uff1b<\/p>\n<p>size\uff1a\u8868\u793a\u5b50\u533a\u57df\u7684\u5927\u5c0f\uff1b<\/p>\n<p>flags\uff1a\u8868\u793a\u8be5\u975e\u8fde\u7eed\u5185\u5b58\u533a\u7684\u7c7b\u578b\uff0cVM_ALLOC\u8868\u793a\u7531vmalloc()\u6620\u5c04\u7684\u5185\u5b58\u533a\uff0cVM_MAP\u8868\u793a\u901a\u8fc7vmap()\u6620\u5c04\u7684\u5185\u5b58\u533a\uff0cVM_IOREMAP\u8868\u793a\u901a\u8fc7ioremap()\u5c06\u786c\u4ef6\u8bbe\u5907\u7684\u5185\u5b58\u6620\u5c04\u5230\u5185\u6838\u7684\u4e00\u6bb5\u5185\u5b58\u533a\uff1b<\/p>\n<p>pages\uff1a\u6307\u9488\u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u7684\u6210\u5458\u662fstruct page*\u7c7b\u578b\u7684\u6307\u9488\uff0c\u6bcf\u4e2a\u6210\u5458\u90fd\u5173\u8054\u4e00\u4e2a\u6620\u5c04\u5230\u8be5\u865a\u62df\u5185\u5b58\u533a\u7684\u7269\u7406\u9875\u6846\uff1b<\/p>\n<p>nr_pages\uff1apages\u6570\u7ec4\u4e2dpage\u7ed3\u6784\u7684\u603b\u6570\uff1b<\/p>\n<p>phys_addr\uff1a\u901a\u5e38\u4e3a0\uff0c\u5f53\u4f7f\u7528ioremap()\u6620\u5c04\u4e00\u4e2a\u786c\u4ef6\u8bbe\u5907\u7684\u7269\u7406\u5185\u5b58\u65f6\u624d\u586b\u5145\u6b64\u5b57\u6bb5\uff1b<\/p>\n<p>caller\uff1a\u8868\u793a\u4e00\u4e2a\u8fd4\u56de\u5730\u5740\uff1b<\/p>\n<h3>vmalloc()\u7684\u5b9e\u73b0<\/h3>\n<p>vmalloc()\u5185\u90e8\u5c01\u88c5\u4e86__vmalloc_node()\uff0c\u8be5\u51fd\u6570\u7684\u539f\u578b\u548c\u8c03\u7528\u5982\u4e0b\u4ee3\u7801\u6240\u793a\u3002\u5176\u4e2d\uff0csize\u8868\u793a\u8981\u5206\u914d\u5b50\u5185\u5b58\u533a\u7684\u5927\u5c0f\uff0c\u5b83\u901a\u8fc7vmalloc()\u4f20\u9012\u8fc7\u6765\u7684\uff1balign\u8868\u793a\u5c06\u6240\u7533\u8bf7\u957f\u5ea6\u7684\u5185\u5b58\u533a\u5206\u4e3a\u51e0\u90e8\u5206\uff0c1\u8868\u793a\u5c06size\u5927\u5c0f\u7684\u865a\u62df\u5185\u5b58\u533a\u4f5c\u4e3a\u4e00\u4e2a\u6574\u4f53\uff1bgfp_mask\u63cf\u8ff0\u9875\u9762\u5206\u914d\u7684\u6807\u5fd7\uff0cGFP_KERNEL|__GFP_HIGHMEM\u8868\u660e\u5185\u5b58\u7ba1\u7406\u5b50\u7cfb\u7edf\u5c06\u4ece\u9ad8\u7aef\u5185\u5b58\u533a(ZONE_HIGHMEM)\u4e2d\u5206\u914d\u5185\u5b58\u7a7a\u95f4\uff1bprot\u63cf\u8ff0\u5f53\u524d\u9875\u7684\u4fdd\u62a4\u6807\u5fd7\uff1bnode\u8868\u793a\u5728\u54ea\u4e2a\u8282\u70b9(struct pg_data_t)\u4e0a\u4e3a\u8fd9\u6bb5\u5b50\u5185\u5b58\u533a\u5206\u914d\u7a7a\u95f4\uff0c-1\u8868\u660e\u5728\u5f53\u524d\u8282\u70b9\u4e2d\u5206\u914d\uff1bcaller\u8868\u793a\u8be5\u51fd\u6570\u7684\u8fd4\u56de\u5730\u5740\u3002<\/p>\n<pre class=\"brush:c\">static void *__vmalloc_node(unsigned long size, unsigned long align,\r\n                            gfp_t gfp_mask, pgprot_t prot,\r\n                            int node, void *caller)\r\nvoid *vmalloc(unsigned long size)\r\n{\r\n        return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,\r\n                                        -1, __builtin_return_address(0));\r\n}<\/pre>\n<p>__vmalloc_node\u51fd\u6570\u7684\u4e3b\u8981\u529f\u80fd\u5206\u4e3a\u4e24\u90e8\u5206\uff1a<\/p>\n<p>1.\u5728\u975e\u8fde\u7eed\u5185\u5b58\u533a\u7684\u8d77\u59cb\u548c\u7ec8\u6b62\u5730\u5740\u4e4b\u95f4\u67e5\u627e\u4e00\u4e2a\u7a7a\u95f2\u7684\u5185\u5b58\u533a\u57df\uff0c\u8fd9\u90e8\u5206\u7531__get_vm_area_node()\u5b8c\u6210\u3002<\/p>\n<p>2.\u4e3a\u8be5\u5b50\u5185\u5b58\u533a\u5206\u914d\u7269\u7406\u9875\u6846\uff0c\u5e76\u5c06\u5206\u6563\u7684\u7269\u7406\u9875\u6846\u5206\u522b\u6620\u5c04\u5230\u8fde\u7eed\u7684vmalloc\u533a\u4e2d\uff0c\u8fd9\u90e8\u5206\u7531__vmalloc_area_node()\u5b8c\u6210\u3002<\/p>\n<p>__vmalloc_node()\u4e00\u5f00\u59cb\u4f1a\u5148\u4fee\u6b63\u4e00\u4e0b\u81ea\u5185\u5b58\u53d6\u7684\u5927\u5c0f\uff0cPAGE_ALIGN\u5c06size\u7684\u5927\u5c0f\u4fee\u6539\u6210\u9875\u5927\u5c0f\u7684\u500d\u6570\u3002\u5047\u5982\u8981\u7533\u8bf71KB\u7684\u5185\u5b58\u533a\uff0c\u90a3\u4e48\u4e8b\u5b9e\u4e0a\u5206\u914d\u7684\u662f4KB\u5927\u5c0f(\u4e00\u4e2a\u9875\u5927\u5c0f)\u7684\u533a\u57df\u3002\u63a5\u7740\u8fdb\u884csize\u5408\u6cd5\u6027\u7684\u68c0\u67e5\uff0c\u5982\u679csize\u4e3a0\uff0c\u6216\u8005size\u6240\u5360\u9875\u6846\u6570\u5927\u4e8e\u7cfb\u7edf\u5f53\u524d\u7a7a\u95f2\u7684\u9875\u6846\u6570(totalram_pages)\uff0c\u5c06\u8fd4\u56deNULL\uff0c\u65e2\u7533\u8bf7\u5931\u8d25\u3002<\/p>\n<p>\u5982\u679c\u5b50\u5185\u5b58\u533a\u5927\u5c0f\u5408\u6cd5\uff0c__get_vm_area_node()\u5c06\u5728\u6574\u4e2a\u975e\u8fde\u7eed\u5185\u5b58\u533a\u4e2d\u67e5\u627e\u4e00\u4e2asize\u5927\u5c0f\u7684\u5b50\u5185\u5b58\u533a\u3002\u8be5\u51fd\u6570\u5148\u904d\u5386\u6574\u4e2avmlist\u94fe\u8868\uff0c\u4f9d\u6b21\u6bd4\u5bf9\u6bcf\u4e2avmalloc\u533a\uff0c\u76f4\u5230\u627e\u5230\u6ee1\u8db3\u8981\u6c42\u7684\u5b50\u5185\u5b58\u533a\u4e3a\u6b62\u3002\u63a5\u7740\u4e3a\u8fd9\u4e2a\u5b50\u5185\u5b58\u533a\u5efa\u7acb\u4e00\u4e2avm_struct\u7ed3\u6784\uff0c\u518d\u5c06\u8fd9\u4e2a\u7ed3\u6784\u63d2\u5165\u5230\u6574\u4e2avmlist\u94fe\u8868\u4e2d\u3002\u8be5\u51fd\u6570\u7684\u8be6\u7ec6\u5b9e\u73b0\u8fc7\u7a0b\u672c\u6587\u4e0d\u505a\u5206\u6790\u3002<\/p>\n<pre class=\"brush:c\">static void *__vmalloc_node(unsigned long size, unsigned long align,\r\n                            gfp_t gfp_mask, pgprot_t prot,\r\n                            int node, void *caller)\r\n{\r\n        struct vm_struct *area;\r\n        void *addr;\r\n        unsigned long real_size = size;\r\n\r\n        size = PAGE_ALIGN(size);\r\n        if (!size || (size &gt;&gt; PAGE_SHIFT) &gt; totalram_pages)\r\n                return NULL;\r\n\r\n        area = __get_vm_area_node(size, align, VM_ALLOC, VMALLOC_START,\r\n                                  VMALLOC_END, node, gfp_mask, caller);\r\n\r\n        if (!area)\r\n                return NULL;\r\n\r\n        addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller);\r\n\r\n        kmemleak_alloc(addr, real_size, 3, gfp_mask);\r\n\r\n        return addr;\r\n}<\/pre>\n<h3>__vmalloc_area_node()\u7684\u5b9e\u73b0<\/h3>\n<p>\u5f53__get_vm_area_node()\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684vm_struct\u7ed3\u6784\u540e\uff0c\u63a5\u4e0b\u6765\u5c31\u8981\u901a\u8fc7__vmalloc_area_node()\u4e3a\u8fd9\u4e2a\u5b50\u5185\u5b58\u533a\u5206\u914d\u771f\u6b63\u7684\u7269\u7406\u9875\u3002<br \/>\n\u9996\u5148\u8ba1\u7b97\u901a\u8fc7\u53f3\u79fbPAGE_SHIFT\u4f4d\u6765\u8ba1\u7b97nr_pages\uff0c\u5b83\u8868\u793a\u8fd9\u4e2a\u5b50\u5185\u5b58\u533a\u6620\u5c04\u7684\u9875\u6570\u3002\u63a5\u7740\u6839\u636e\u5b50\u5185\u5b58\u533a\u6240\u6620\u5c04\u7684\u9875\u6846\u6570\u8ba1\u7b97pages\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u8fd9\u4e2a\u6570\u7ec4\u7684\u5143\u7d20\u4e3astruct page*\u578b\uff0c\u6bcf\u4e2a\u5143\u7d20\u90fd\u6307\u5411\u4e00\u4e2a\u7528\u6765\u63cf\u8ff0\u7269\u7406\u9875\u6846\u7684page\u7ed3\u6784\u3002<\/p>\n<pre class=\"brush:c\">static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,\r\n                                 pgprot_t prot, int node, void *caller)\r\n{\r\n        struct page **pages;\r\n        unsigned int nr_pages, array_size, i;\r\n        gfp_t nested_gfp = (gfp_mask &amp; GFP_RECLAIM_MASK) | __GFP_ZERO;\r\n\r\n        nr_pages = (area-&gt;size - PAGE_SIZE) &gt;&gt; PAGE_SHIFT;\r\n        array_size = (nr_pages * sizeof(struct page *));\r\n\r\n        area-&gt;nr_pages = nr_pages;<\/pre>\n<p>\u63a5\u7740\uff0c__vmalloc_area_node()\u4e3a\u9875\u63cf\u8ff0\u7b26\u6307\u9488\u6570\u7ec4\u5206\u914d\u7a7a\u95f4\u3002\u5982\u679c\u8fd9\u4e2a\u6307\u9488\u6570\u7ec4\u7684\u5927\u5c0f\u8d85\u8fc7\u4e00\u4e2a\u9875\u7684\u5927\u5c0f\uff0c\u90a3\u4e48\u9012\u5f52\u8c03\u7528__vmalloc_node()\u4e3a\u5176\u5206\u914d\u7a7a\u95f4\uff0c\u4e5f\u5c31\u662f\u8bf4pages\u6570\u7ec4\u672c\u8eab\u5c31\u91c7\u7528vmalloc\u533a\u6765\u5b58\u50a8\uff1b\u5426\u5219\uff0c\u901a\u8fc7kmalloc_node()\u4e3apages\u6570\u7ec4\u5206\u914d\u4e00\u6bb5\u8fde\u7eed\u7684\u7a7a\u95f4\uff0c\u8fd9\u6bb5\u7a7a\u95f4\u65e2\u4f4d\u4e8e\u5185\u6838\u7a7a\u95f4\u7684\u7269\u7406\u5185\u5b58\u7ebf\u6027\u6620\u5c04\u533a\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u7528\u521a\u624d\u7684\u5c40\u90e8\u53d8\u91cfpages\u66f4\u65b0area\u4e2d\u7684pages\u3002\u5982\u679cpages\u6570\u7ec4\u5206\u914d\u5931\u8d25\uff0c\u5219\u8c03\u7528remove_vm_area()\u5c06__get_vm_area_node()\u7684\u5230\u7684vm_struct\u7ed3\u6784\u4ecevmlist\u4e2d\u79fb\u9664\uff0c\u5e76\u8fd4\u56deNULL\uff0c\u8868\u793avmalloc()\u7533\u8bf7\u5931\u8d25\u3002<\/p>\n<pre class=\"brush:c\">        if (array_size &gt; PAGE_SIZE) {\r\n                pages = __vmalloc_node(array_size, 1, nested_gfp|__GFP_HIGHMEM,\r\n                                PAGE_KERNEL, node, caller);\r\n                area-&gt;flags |= VM_VPAGES;\r\n        } else {\r\n                pages = kmalloc_node(array_size, nested_gfp, node);\r\n        }\r\n        area-&gt;pages = pages;\r\n        area-&gt;caller = caller;\r\n        if (!area-&gt;pages) {\r\n                remove_vm_area(area-&gt;addr);\r\n                kfree(area);\r\n                return NULL;\r\n        }<\/pre>\n<p>\u73b0\u5728\u5230\u4e86\u6700\u5173\u952e\u7684\u65f6\u523b\uff0c\u901a\u8fc7\u4e00\u4e2a\u5faa\u73af\u4f9d\u6b21\u4e3apages\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u9875\u9762\u63cf\u8ff0\u7b26\u5206\u914d\u771f\u6b63\u7684\u7269\u7406\u9875\u6846\u3002\u9700\u8981\u6ce8\u610f\u7684\u662fpage\u7ed3\u6784\u5e76\u4e0d\u662f\u4ee3\u8868\u4e00\u4e2a\u5177\u4f53\u7684\u7269\u7406\u9875\u6846\uff0c\u53ea\u662f\u7528\u6765\u63cf\u8ff0\u7269\u7406\u9875\u6846\u7684\u6570\u636e\u7ed3\u6784\u800c\u5df2\u3002\u5982\u679cnode\u5c0f\u4e8e0\uff0c\u4e5f\u5c31\u662f\u672a\u6307\u5b9a\u7269\u7406\u5185\u5b58\u6240\u5728\u8282\u70b9\uff0c\u90a3\u4e48\u4f7f\u7528alloc_page()\u5206\u914d\u4e00\u4e2a\u9875\u6846\uff0c\u5e76\u5c06\u8be5\u9875\u6846\u5bf9\u5e94\u7684\u9875\u63cf\u8ff0\u7b26\u6307\u9488\u8d4b\u503c\u7ed9page\u4e34\u65f6\u53d8\u91cf\uff1b\u5426\u5219\u901a\u8fc7alloc_pages_node()\u5728\u6307\u5b9a\u7684\u8282\u70b9\u4e0a\u5206\u914d\u7269\u7406\u9875\u6846\u3002\u63a5\u7740\u5c06\u521a\u521a\u5206\u914d\u7684\u7269\u7406\u9875\u6846\u5bf9\u5e94\u7684\u9875\u63cf\u8ff0\u7b26\u8d4b\u503c\u7ed9pages\u6570\u7ec4\u7684\u7b2ci\u4e2a\u5143\u7d20\u3002\u4e00\u65e6\u67d0\u4e2a\u7269\u7406\u9875\u6846\u5206\u914d\u5931\u8d25\u5219\u76f4\u63a5\u8fd4\u56deNULL\uff0c\u8868\u793a\u672c\u6b21vmalloc()\u64cd\u4f5c\u5931\u8d25\u3002<\/p>\n<pre class=\"brush:c\">\r\n        for (i = 0; i < area->nr_pages; i++) {\r\n                struct page *page;\r\n                if (node < 0)\r\n                        page = alloc_page(gfp_mask);\r\n                else\r\n                        page = alloc_pages_node(node, gfp_mask, 0);\r\n\r\n                if (unlikely(!page)) {\r\n                        area->nr_pages = i;\r\n                        goto fail;\r\n                }\r\n                area->pages[i] = page;\r\n        }\r\n\r\n        if (map_vm_area(area, prot, &pages))\r\n                goto fail;\r\n        return area->addr;\r\n\r\nfail:\r\n        vfree(area->addr);\r\n        return NULL;\r\n}\r\n<\/pre>\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c__vmalloc_area_node()\u5df2\u7ecf\u5206\u914d\u4e86\u6240\u9700\u7684\u7269\u7406\u9875\u6846\uff0c\u4f46\u662f\u8fd9\u4e9b\u5206\u6563\u7684\u9875\u6846\u5e76\u6ca1\u6709\u6620\u5c04\u5230area\u6240\u4ee3\u8868\u7684\u90a3\u4e2a\u8fde\u7eedvmalloc\u533a\u4e2d\u3002map_vm_area()\u5c06\u5b8c\u6210\u6620\u5c04\u5de5\u4f5c\uff0c\u5b83\u4f9d\u6b21\u4fee\u6539\u5185\u6838\u4f7f\u7528\u7684\u9875\u8868\u9879\uff0c\u5c06pages\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u9875\u6846\u5206\u522b\u6620\u5c04\u5230\u8fde\u7eed\u7684vmalloc\u533a\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u62e5\u6709\u4e00\u6bb5\u8fde\u7eed\u800c\u4e14\u5e73\u5766\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\uff0c\u8fd9\u6bb5\u8fde\u7eed\u7684\u7a7a\u95f4\u88ab\u5212\u5206\u4e3a\u4e24\u5927\u90e8\u5206\uff1a\u7528\u6237\u7a7a\u95f4\u548c\u5185\u6838\u7a7a\u95f4\u3002\u5728x86-3 [&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":[289,407,288],"class_list":["post-3537","post","type-post","status-publish","format-standard","hentry","category-266","tag-vmalloc","tag-407","tag-288"],"views":7767,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3537","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=3537"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3537\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=3537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=3537"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=3537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}