{"id":3738,"date":"2012-02-28T20:13:39","date_gmt":"2012-02-28T12:13:39","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=3738"},"modified":"2012-03-27T16:43:52","modified_gmt":"2012-03-27T08:43:52","slug":"linux%e9%a1%b5%e6%a1%86%e5%88%86%e9%85%8d%e5%87%bd%e6%95%b0%e7%9a%84%e5%ae%9e%e7%8e%b0-%e6%85%a2%e9%80%9f%e5%86%85%e5%ad%98%e5%88%86%e9%85%8d","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/3738","title":{"rendered":"Linux\u9875\u6846\u5206\u914d\u51fd\u6570\u7684\u5b9e\u73b0\uff082\uff09-\u6162\u901f\u5185\u5b58\u5206\u914d"},"content":{"rendered":"<h3>2. \u6162\u901f\u5206\u914d\u51fd\u6570<\/h3>\n<p>\u8fdb\u5165\u6162\u901f\u5206\u914d\u51fd\u6570\u540e\uff0c\u5148\u68c0\u67e5\u6240\u8bf7\u6c42\u7684\u5206\u914d\u9636\u662f\u5426\u8d85\u8fc7\u4e86MAX_ORDER\u3002\u5982\u679c\u6307\u5b9a\u4e86GFP_THISNODE\u6807\u5fd7\u540e\uff0c\u5219\u4e0d\u80fd\u7ee7\u7eed\u8fdb\u884c\u6162\u901f\u5185\u5b58\u5206\u914d\uff0c\u56e0\u4e3a\u8be5\u6807\u5fd7\u6307\u660e\u4e86\u5185\u5b58\u4e0d\u80fd\u8fdb\u884c\u56de\u6536\uff0c\u56e0\u6b64\u76f4\u63a5\u8df3\u5230nopage\u5904\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u5728\u7ecf\u5386\u4e00\u7cfb\u5217\u7684\u53c2\u6570\u68c0\u67e5\u4e4b\u540e\uff0c\u8be5\u51fd\u6570\u901a\u8fc7\u8c03\u7528wake_all_kswapd()\u5524\u9192\u6bcf\u4e2azone\u6240\u5c5enode\u4e2d\u7684kswapd\u5b88\u62a4\u8fdb\u7a0b\u3002\u8fd9\u4e2a\u5b88\u62a4\u8fdb\u7a0b\u8d1f\u8d23\u6362\u51fa\u5f88\u5c11\u4f7f\u7528\u7684\u9875\uff0c\u4ee5\u63d0\u9ad8\u76ee\u524d\u7cfb\u7edf\u53ef\u4ee5\u7528\u7684\u7a7a\u95f2\u9875\u6846\u3002<\/p>\n<p>\u5728kswapd\u4ea4\u6362\u8fdb\u7a0b\u88ab\u5524\u9192\u4e4b\u540e\uff0c\u8be5\u51fd\u6570\u5f00\u59cb\u5c1d\u8bd5\u65b0\u4e00\u8f6e\u7684\u5206\u914d\u3002\u5b83\u9996\u5148\u901a\u8fc7gfp_to_alloc_flags()\u5bf9\u5206\u914d\u6807\u5fd7\u8fdb\u884c\u8c03\u6574\uff0c\u7a0d\u5fae\u964d\u4f4e\u5206\u914d\u6807\u51c6\u4ee5\u4fbf\u8fd9\u6b21\u8c03\u7528get_page_from_freelist()\u6709\u53ef\u80fd\u5206\u914d\u5230\u5185\u5b58\u3002<\/p>\n<pre class=\"brush:c\">static inline struct page *\r\n__alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,\r\n        struct zonelist *zonelist, enum zone_type high_zoneidx,\r\n        nodemask_t *nodemask, struct zone *preferred_zone,\r\n        int migratetype)\r\n{\r\n        const gfp_t wait = gfp_mask &amp; __GFP_WAIT;\r\n        struct page *page = NULL;\r\n        int alloc_flags;\r\n        unsigned long pages_reclaimed = 0;\r\n        unsigned long did_some_progress;\r\n        struct task_struct *p = current;\r\n\r\n        if (order &gt;= MAX_ORDER) {\r\n                WARN_ON_ONCE(!(gfp_mask &amp; __GFP_NOWARN));\r\n                return NULL;\r\n        }\r\n\r\n        if (NUMA_BUILD &amp;&amp; (gfp_mask &amp; GFP_THISNODE) == GFP_THISNODE)\r\n                goto nopage;\r\n\r\nrestart:\r\n        wake_all_kswapd(order, zonelist, high_zoneidx);\r\n        alloc_flags = gfp_to_alloc_flags(gfp_mask);\r\n        page = get_page_from_freelist(gfp_mask, nodemask, order, zonelist,\r\n                        high_zoneidx, alloc_flags &amp; ~ALLOC_NO_WATERMARKS,\r\n                        preferred_zone, migratetype);\r\n        if (page)\r\n                goto got_pg;<\/pre>\n<p>\u5982\u679cpage\u4e0d\u4e3a\u7a7a\uff0c\u5219\u8bf4\u660e\u5185\u5b58\u7533\u8bf7\u6210\u529f\uff0c\u5426\u5219\u7ee7\u7eed\u8fdb\u884c\u6162\u901f\u5185\u5b58\u5206\u914d\u3002\u5982\u679c\u8bbe\u7f6e\u4e86ALLOC_NO_WATERMARKS\u6807\u5fd7\uff0c\u90a3\u4e48\u6b64\u65f6\u4f1a\u5ffd\u7565\u6c34\u5370\uff0c\u5e76\u6b64\u65f6\u8fdb\u5165__alloc_pages_high_priority()\u3002\u8be5\u51fd\u6570\u5185\u90e8\u4f1a\u81f3\u5c11\u4f1a\u518d\u8c03\u7528\u4e00\u6b21get_page_from_freelist()\uff0c\u5982\u679c\u8bbe\u7f6e\u4e86__GFP_NOFAIL\u6807\u5fd7\uff0c\u5219\u4e0d\u65ad\u7684\u5faa\u73af\u7b49\u5f85\u5e76\u5c1d\u8bd5\u8fdb\u884c\u5185\u5b58\u5206\u914d\u3002<\/p>\n<pre class=\"brush:c\">rebalance:\r\n        if (alloc_flags &amp; ALLOC_NO_WATERMARKS) {\r\n                page = __alloc_pages_high_priority(gfp_mask, order,\r\n                                zonelist, high_zoneidx, nodemask,\r\n                                preferred_zone, migratetype);\r\n                if (page)\r\n                        goto got_pg;\r\n        }<\/pre>\n<p>\u5982\u679c\u6ca1\u6709\u8bbe\u7f6e__GFP_WAIT\uff0c\u5373wait\u4e3a0\uff0c\u5219\u4e0d\u7ee7\u7eed\u8fdb\u884c\u5185\u5b58\u5206\u914d\uff0c\u76f4\u63a5\u8df3\u5230nopage\u5904\u3002\u5982\u679cPF_MEMALLOC\u88ab\u8bbe\u7f6e\uff0c\u4e5f\u5c31\u662f\u8bf4\u8c03\u7528\u5185\u5b58\u5206\u914d\u51fd\u6570\u7740\u672c\u8eab\u5c31\u662f\u5185\u5b58\u56de\u6536\u8fdb\u7a0b\uff0c\u5219\u76f4\u63a5\u8df3\u5230nopage\u5904\u3002<\/p>\n<pre class=\"brush:c\">        if (!wait)\r\n                goto nopage;\r\n\r\n        if (p-&gt;flags &amp; PF_MEMALLOC)\r\n                goto nopage;\r\n\r\n        if (test_thread_flag(TIF_MEMDIE) &amp;&amp; !(gfp_mask &amp; __GFP_NOFAIL))\r\n                goto nopage;<\/pre>\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u5206\u914d\u51fd\u6570\u5df2\u7ecf\u5c1d\u8bd5\u597d\u51e0\u6b21\u9875\u6846\u5206\u914d\u3002\u5982\u679c\u73b0\u5728\u4ecd\u672a\u5206\u914d\u5230\u8bf7\u6c42\u7684\u5185\u5b58\uff0c\u90a3\u4e48\u63a5\u4e0b\u6765\u5c06\u8fdb\u5165\u4e00\u4e2a\u6bd4\u8f83\u8017\u65f6\u7684\u9636\u6bb5\u3002\u5185\u6838\u901a\u8fc7\u5c06\u5f88\u5c11\u4f7f\u7528\u7684\u9875\u6362\u51fa\u5230\u78c1\u76d8\u4e0a\uff0c\u4ee5\u4fbf\u5728\u7269\u7406\u5185\u5b58\u4e2d\u6709\u66f4\u591a\u7684\u7a7a\u95f2\u9875\u6846\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u80fd\u4f1a\u4ea7\u751f\u963b\u585e\uff0c\u4e5f\u5c31\u662f\u8bf4\u4f1a\u4ea7\u751f\u7761\u7720\uff0c\u56e0\u6b64\u5b83\u6bd4\u8f83\u8017\u65f6\u3002<\/p>\n<p>__alloc_pages_direct_reclaim()\u7684\u4f5c\u7528\u5c31\u662f\u5148\u901a\u8fc7try_to_free_pages()\u56de\u6536\u4e00\u4e9b\u6700\u8fd1\u5f88\u5c11\u7528\u7684\u9875\uff0c\u5c06\u5176\u5199\u56de\u78c1\u76d8\u4e0a\u7684\u4ea4\u6362\u533a\uff0c\u4ee5\u4fbf\u5728\u7269\u7406\u5185\u5b58\u4e2d\u817e\u51fa\u66f4\u591a\u7684\u7a7a\u95f4\u3002\u63a5\u7740\uff0c\u5185\u6838\u4f1a\u518d\u6b21\u8c03\u7528get_page_from_freelist()\u5c1d\u8bd5\u5206\u914d\u5185\u5b58\u3002<\/p>\n<pre class=\"brush:c\">        page = __alloc_pages_direct_reclaim(gfp_mask, order,\r\n                                        zonelist, high_zoneidx,\r\n                                        nodemask,\r\n                                        alloc_flags, preferred_zone,\r\n                                        migratetype, &amp;did_some_progress);\r\n        if (page)\r\n                goto got_pg;<\/pre>\n<p>\u5982\u679c\u5185\u6838\u8fdb\u884c\u4e86\u4e0a\u8ff0\u7684\u56de\u6536\u548c\u91cd\u65b0\u5206\u914d\u7684\u8fc7\u7a0b\u540e\uff0c\u4ecd\u672a\u5206\u914d\u6210\u529f\uff0c\u65e2did_some_progress\u4e3a0\uff0c\u90a3\u4e48\u6b64\u65f6\u5185\u6838\u4e0d\u7684\u4e0d\u8003\u8651\u662f\u5426\u53d1\u751f\u4e86OOM(out of memory)\u3002\u5982\u679c\u5f53\u524d\u8bf7\u6c42\u5185\u5b58\u7684\u8fdb\u7a0b\u53d1\u751f\u4e86OOM\uff0c\u4e5f\u5c31\u662f\u8bf4\u8be5\u8fdb\u7a0b\u8bd5\u56fe\u62e5\u6709\u8fc7\u591a\u7684\u5185\u5b58\uff0c\u90a3\u4e48\u6b64\u65f6\u5185\u6838\u4f1a\u8c03\u7528OOM killer\u6740\u6b7b\u5b83\u3002\u5e76\u4e14\u8df3\u8f6c\u5230restart\u5904\uff0c\u91cd\u65b0\u8fdb\u884c\u5185\u5b58\u5206\u914d\u3002<\/p>\n<pre class=\"brush:c\">        if (!did_some_progress) {\r\n                if ((gfp_mask &amp; __GFP_FS) &amp;&amp; !(gfp_mask &amp; __GFP_NORETRY)) {\r\n                        if (oom_killer_disabled)\r\n                                goto nopage;\r\n                        page = __alloc_pages_may_oom(gfp_mask, order,\r\n                                        zonelist, high_zoneidx,\r\n                                        nodemask, preferred_zone,\r\n                                        migratetype);\r\n                        if (page)\r\n                                goto got_pg;\r\n\r\n                        if (order &gt; PAGE_ALLOC_COSTLY_ORDER &amp;&amp;\r\n                                                !(gfp_mask &amp; __GFP_NOFAIL))\r\n                                goto nopage;\r\n\r\n                        goto restart;\r\n                }\r\n        }<\/pre>\n<p>\u6b64\u65f6\u518d\u6b21\u5224\u65ad\u662f\u5426\u8981\u91cd\u65b0\u8fdb\u884c\u4e00\u6b21\u5185\u5b58\u7533\u8bf7\u3002\u5982\u679c\u6709\u8fd9\u4e2a\u5fc5\u8981\uff0c\u90a3\u4e48\u7b49\u5f85\u5199\u64cd\u4f5c\u5b8c\u6210\u540e\u518d\u6b21\u8df3\u5230rebalance\u5904\u91cd\u8bd5\u3002<\/p>\n<pre class=\"brush:c\">        pages_reclaimed += did_some_progress;\r\n        if (should_alloc_retry(gfp_mask, order, pages_reclaimed)) {\r\n                congestion_wait(BLK_RW_ASYNC, HZ\/50);\r\n                goto rebalance;\r\n        }<\/pre>\n<p>\u9875\u6846\u5206\u914d\u51fd\u6570\u7ed3\u675f\u65f6\u5019\u4e00\u822c\u6709\u4e24\u79cd\u60c5\u51b5\uff0c\u5176\u4e2d\u4e4b\u4e00\u5373\u4e3a\u5206\u914d\u5931\u8d25\uff0c\u5e76\u6ca1\u6709\u5f97\u5230\u6240\u9700\u9875\u6846\uff0c\u56e0\u6b64\u6253\u5370\u4e00\u4e9b\u5185\u5b58\u5206\u914d\u5931\u8d25\u7684\u4fe1\u606f\u3002<\/p>\n<pre class=\"brush:c\">nopage:\r\n        if (!(gfp_mask &amp; __GFP_NOWARN) &amp;&amp; printk_ratelimit()) {\r\n                printk(KERN_WARNING \"%s: page allocation failure.\"\r\n                        \" order:%d, mode:0x%x\\n\",\r\n                        p-&gt;comm, order, gfp_mask);\r\n                dump_stack();\r\n                show_mem();\r\n        }\r\n        return page;<\/pre>\n<p>\u53e6\u4e00\u79cd\u60c5\u51b5\uff0c\u4e5f\u5c31\u662f\u5f97\u5230\u4e86\u6240\u9700\u9875\u6846\uff0c\u90a3\u4e48\u76f4\u63a5\u8fd4\u56de\u8fd9\u7ec4\u9875\u6846\u7684\u9996\u9875\u6846\u63cf\u8ff0\u7b26\u3002<\/p>\n<pre class=\"brush:c\">got_pg:\r\n        if (kmemcheck_enabled)\r\n                kmemcheck_pagealloc_alloc(page, order, gfp_mask);\r\n        return page;\r\n\r\n}<\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u7684\u8fc7\u7a0b\u53ef\u4ee5\u770b\u5230\uff0c\u9875\u6846\u5206\u914d\u51fd\u6570__alloc_pages()\u4f1a\u591a\u6b21\u5c1d\u8bd5\u8fdb\u884c\u5206\u914d\u5185\u5b58\u3002\u800c\u5177\u4f53\u7684\u9875\u6846\u5206\u914d\u64cd\u4f5c\u662f\u5728get_page_from_freelist()\u4e2d\u5b8c\u6210\u7684\uff0c\u5b83\u6839\u636e\u4f19\u4f34\u7b97\u6cd5\u5206\u914d\u6240\u9700\u5927\u5c0f\u7684\u9875\u6846\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>2. \u6162\u901f\u5206\u914d\u51fd\u6570 \u8fdb\u5165\u6162\u901f\u5206\u914d\u51fd\u6570\u540e\uff0c\u5148\u68c0\u67e5\u6240\u8bf7\u6c42\u7684\u5206\u914d\u9636\u662f\u5426\u8d85\u8fc7\u4e86MAX_ORDER\u3002\u5982\u679c\u6307\u5b9a\u4e86GFP_T [&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":[312,407,304,302],"class_list":["post-3738","post","type-post","status-publish","format-standard","hentry","category-266","tag-alloc_page","tag-407","tag-304","tag-302"],"views":8707,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3738","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=3738"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3738\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=3738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=3738"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=3738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}