{"id":3681,"date":"2012-01-11T11:35:22","date_gmt":"2012-01-11T03:35:22","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=3681"},"modified":"2012-03-27T16:41:01","modified_gmt":"2012-03-27T08:41:01","slug":"linux%e9%a1%b5%e6%a1%86%e5%88%86%e9%85%8d%e5%87%bd%e6%95%b0%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/3681","title":{"rendered":"Linux\u9875\u6846\u5206\u914d\u51fd\u6570\u7684\u5b9e\u73b0\uff081\uff09-\u4e3b\u4f53\u5206\u914d\u51fd\u6570"},"content":{"rendered":"<p>\u5185\u6838\u4e2d\u6709\u516d\u4e2a\u57fa\u672c\u7684\u9875\u6846\u5206\u914d\u51fd\u6570\uff0c\u5b83\u4eec\u5185\u90e8\u7ecf\u8fc7\u5c01\u88c5\uff0c\u6700\u7ec8\u90fd\u4f1a\u8c03\u7528alloc_pages_node()\u3002\u8fd9\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u6bd4alloc_pages()\u591a\u4e86\u4e00\u4e2anid\uff0c\u5b83\u7528\u6765\u6307\u5b9a\u8282\u70b9id\uff0c\u5982\u679cnid\u5c0f\u4e8e0\uff0c\u5219\u8bf4\u660e\u5728\u5f53\u524d\u8282\u70b9\u4e0a\u5206\u914d\u9875\u6846\u3002\u6b63\u786e\u83b7\u53d6\u5230\u8282\u70b9id\u540e\uff0c\u63a5\u4e0b\u6765\u8c03\u7528__alloc_pages()\u3002<\/p>\n<pre class=\"brush:c\">static inline struct page *alloc_pages_node(int nid, gfp_t gfp_mask, unsigned int order)\r\n{\r\n        if (nid &lt; 0)\r\n                nid = numa_node_id();\r\n\r\n        return __alloc_pages(gfp_mask, order, node_zonelist(nid, gfp_mask));\r\n}<\/pre>\n<p>__alloc_pages()\u7b2c\u4e09\u4e2a\u53c2\u6570\u6839\u636enid\u548cgfp_mask\u5f97\u5230\u9002\u5f53\u7684zonelist\u94fe\u8868\uff0c\u8be5\u8fc7\u7a0b\u901a\u8fc7node_zonelist()\u5b8c\u6210\u3002\u8be5\u51fd\u6570\u7684\u5b9e\u73b0\u6bd4\u8f83\u7b80\u5355\uff0c\u5176\u4e2dNODE_DATA()\u6839\u636enid\u8fd4\u56de\u5bf9\u5e94\u7684\u5185\u5b58\u8282\u70b9\u63cf\u8ff0\u7b26\uff0c\u800cgfp_zonelist()\u6839\u636eflags\u6807\u5fd7\u9009\u53d6\u5bf9\u5e94\u7684\u5185\u5b58\u7ba1\u7406\u533a\u94fe\u8868\u3002\u5176\u5b9enode_zonelist()\u5c31\u662f\u6839\u636eflags\u5728\u76f8\u5e94\u5185\u5b58\u8282\u70b9\u7684node_zonelists\u6570\u7ec4\u4e2d\u9009\u62e9\u4e00\u4e2a\u4f55\u65f6\u7684\u5185\u5b58\u7ba1\u7406\u533a\u94fe\u8868zonelist\u3002<\/p>\n<pre class=\"brush:c\">static inline int gfp_zonelist(gfp_t flags)\r\n{\r\n        if (NUMA_BUILD &amp;&amp; unlikely(flags &amp; __GFP_THISNODE))\r\n                return 1;\r\n\r\n        return 0;\r\n}<\/pre>\n<p>\u7531\u4e8enode_zonelists\u6570\u7ec4\u7684\u5143\u7d20\u4e2a\u6570\u6700\u5927\u4e3a2\uff0c\u56e0\u6b64gfp_zonelist()\u8fd4\u56de0\u6216\u80051\u3002\u5982\u679cflags\u4e2d\u8bbe\u7f6e\u4e86__GFP_THISNODE\u5e76\u4e14NUMA\u88ab\u8bbe\u7f6e\uff0c\u5219\u8868\u660e\u4f7f\u7528\u5f53\u524d\u8282\u70b9\u5bf9\u5e94\u7684zonelist\uff0c\u8fd4\u56de1\u3002\u5426\u5219\u4f7f\u7528\u5907\u7528zonelist\uff0c\u4e5f\u5c31\u662f\u8bf4\u5f53\u672c\u5730\u8282\u70b9\u4e2dzone\u4e0d\u8db3\u65f6\uff0c\u5728\u5176\u4ed6\u8282\u70b9\u4e2d\u7533\u8bf7\u9875\u6846\u3002<\/p>\n<pre class=\"brush:c\">static inline int gfp_zonelist(gfp_t flags)\r\n{\r\n        if (NUMA_BUILD &amp;&amp; unlikely(flags &amp; __GFP_THISNODE))\r\n                return 1;\r\n\r\n        return 0;\r\n}<\/pre>\n<p>__alloc_pages()\u5185\u90e8\u518d\u6b21\u5c01\u88c5__alloc_pages_nodemask()\u3002<\/p>\n<pre class=\"brush:c\">static inline struct page *\r\n__alloc_pages(gfp_t gfp_mask, unsigned int order, struct zonelist *zonelist)\r\n{\r\n        return __alloc_pages_nodemask(gfp_mask, order, zonelist, NULL);\r\n}<\/pre>\n<h3>1. \u4e3b\u4f53\u5206\u914d\u51fd\u6570<\/h3>\n<p>\u73b0\u5728\u8fdb\u5165__alloc_pages_nodemask()\uff0c\u5b83\u4f5c\u4e3a\u9875\u6846\u5206\u914d\u51fd\u6570\u7684\u6838\u5fc3\u90e8\u5206\u3002\u8be5\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7get_page_from_freelist()\u5feb\u901f\u5206\u914d\u6240\u8bf7\u6c42\u7684\u5185\u5b58\uff0c\u4f46\u662f\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u8c03\u7528\u8be5\u51fd\u6570\u90fd\u4f1a\u5931\u8d25\uff0c\u56e0\u4e3a\u901a\u5e38\u7269\u7406\u5185\u5b58\u7684\u4f7f\u7528\u60c5\u51b5\u90fd\u6bd4\u8f83\u7d27\u5f20\uff0c\u8fd9\u4e00\u70b9\u4ece\u5176\u540eif\u8bed\u53e5\u4e2d\u7684unlikely\u5c31\u53ef\u4ee5\u770b\u51fa\u3002<\/p>\n<pre class=\"brush:c\">struct page *\r\n__alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,struct zonelist *zonelist, nodemask_t *nodemask)\r\n{\r\n        enum zone_type high_zoneidx = gfp_zone(gfp_mask);\r\n        struct zone *preferred_zone;\r\n        struct page *page;\r\n        int migratetype = allocflags_to_migratetype(gfp_mask);\r\n\r\n        gfp_mask &amp;= gfp_allowed_mask;\r\n\r\n        lockdep_trace_alloc(gfp_mask);\r\n\r\n        might_sleep_if(gfp_mask &amp; __GFP_WAIT);\r\n\r\n        if (should_fail_alloc_page(gfp_mask, order))\r\n                return NULL; \r\n\r\n        if (unlikely(!zonelist-&gt;_zonerefs-&gt;zone))\r\n                return NULL;            \r\n\r\n        first_zones_zonelist(zonelist, high_zoneidx, nodemask, &amp;preferred_zone);\r\n        if (!preferred_zone)\r\n                return NULL;\r\n\r\n        page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask, order,\r\n                        zonelist, high_zoneidx, ALLOC_WMARK_LOW|ALLOC_CPUSET,\r\n                        preferred_zone, migratetype);\r\n        if (unlikely(!page))\r\n                page = __alloc_pages_slowpath(gfp_mask, order,\r\n                                zonelist, high_zoneidx, nodemask,\r\n                                preferred_zone, migratetype);\r\n\r\n        trace_mm_page_alloc(page, order, gfp_mask, migratetype);\r\n        return page;\r\n}<\/pre>\n<p>\u9996\u5148\uff0cgfp_zone()\u6839\u636egfp_mask\u9009\u53d6\u9002\u5f53\u7c7b\u578b\u7684zone\u3002\u5728\u7ecf\u8fc7\u51e0\u9879\u53c2\u6570\u68c0\u67e5\u540e\uff0c\u8be5\u51fd\u6570\u901a\u8fc7zonelist-&gt;_zonerefs-&gt;zone\u5224\u65adzonelist\u662f\u5426\u4e3a\u7a7a\uff0c\u65e2\u81f3\u5c11\u9700\u8981\u4e00\u4e2azone\u53ef\u7528\u3002\u63a5\u7740\u6839\u636e\u4e00\u5f00\u59cb\u9009\u53d6\u7684zone\u7c7b\u578bhigh_zoneidx\uff0c\u901a\u8fc7first_zones_zonelist()\u786e\u5b9a\u4f18\u5148\u5206\u914d\u5185\u5b58\u7684\u5185\u5b58\u7ba1\u7406\u533a\u3002<\/p>\n<p>\u5982\u679c\u4e00\u5207\u987a\u5229\uff0c\u5c06\u4f1a\u8fdb\u5165get_page_from_freelist()\uff0c\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u770b\u4f5c\u662f\u4f19\u4f34\u7b97\u6cd5\u7684\u524d\u7f6e\u51fd\u6570\uff0c\u5b83\u901a\u8fc7\u5206\u914d\u6807\u5fd7\u548c\u5206\u914d\u9636\u5224\u65ad\u662f\u5426\u80fd\u8fdb\u884c\u6b64\u6b21\u5185\u5b58\u5206\u914d\u3002\u5982\u679c\u53ef\u4ee5\u5206\u914d\uff0c\u5219\u5b83\u8fdb\u884c\u5b9e\u9645\u7684\u5185\u5b58\u5206\u914d\u5de5\u4f5c\uff0c\u65e2\u5229\u7528\u4f19\u4f34\u7b97\u6cd5\u8fdb\u884c\u5206\u914d\u5185\u5b58\u3002\u5426\u5219\uff0c\u8fdb\u5165__alloc_pages_slowpath()\uff0c\u6b64\u65f6\u5185\u6838\u9700\u8981\u653e\u5bbd\u4e00\u4e9b\u5206\u914d\u6761\u4ef6\uff0c\u6216\u56de\u6536\u4e00\u4e9b\u7cfb\u7edf\u7684\u5185\u5b58\uff0c\u7136\u540e\u518d\u8c03\u7528\u51e0\u6b21get_page_from_freelist()\u4ee5\u7533\u8bf7\u6240\u9700\u5185\u5b58\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5185\u6838\u4e2d\u6709\u516d\u4e2a\u57fa\u672c\u7684\u9875\u6846\u5206\u914d\u51fd\u6570\uff0c\u5b83\u4eec\u5185\u90e8\u7ecf\u8fc7\u5c01\u88c5\uff0c\u6700\u7ec8\u90fd\u4f1a\u8c03\u7528alloc_pages_node()\u3002\u8fd9\u4e2a\u51fd\u6570\u7684 [&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":[407,304,299,302],"class_list":["post-3681","post","type-post","status-publish","format-standard","hentry","category-266","tag-407","tag-304","tag-299","tag-302"],"views":10482,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3681","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=3681"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3681\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=3681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=3681"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=3681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}