{"id":6426,"date":"2022-02-10T16:12:44","date_gmt":"2022-02-10T08:12:44","guid":{"rendered":"https:\/\/www.wangonc.com\/?p=6426"},"modified":"2024-03-01T15:29:24","modified_gmt":"2024-03-01T07:29:24","slug":"memory-management","status":"publish","type":"post","link":"https:\/\/www.wangonc.com\/index.php\/2022\/02\/10\/memory-management\/","title":{"rendered":"\u5185\u5b58\u7ba1\u7406"},"content":{"rendered":"<h2>\u4e00\u3001\u5185\u5b58\u521d\u59cb\u5316<\/h2>\n<p>1\u3001\u5185\u5b58\u7684\u521d\u59cb\u5316\u4e00\u822c\u7531BIOS\u6216boot loader\u5b8c\u6210\uff0c\u7531bios\u6216boot loader\u5c06\u5185\u5b58\u7684\u5927\u5c0f\u4f20\u9012\u7ed9linux\u5185\u6838<\/p>\n<p>2\u3001\u5728arm\u8bbe\u5907\u4e2d\uff0c\u8bbe\u5907\u7531DTS(Device Tree Source)\u63cf\u8ff0,\u5b89\u5353\u4e2d\u4e5f\u662f\u5982\u6b64<\/p>\n<p>3\u3001\u5728\u5185\u6838\u542f\u52a8\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u89e3\u6790dts\u6587\u4ef6\uff0c\u5176\u4e2d\u4f7f\u7528early_init_dt_scan_memory()\u51fd\u6570\u6765\u89e3\u6790\u5185\u5b58\u4fe1\u606f\uff0c\u89e3\u6790\u5185\u5b58\u76f8\u5173\u7684\u4fe1\u606f\u5f97\u5230\u57fa\u5730\u5740\uff08base address\uff09\u548c\u5927\u5c0f\uff08size\uff09\uff0c\u6700\u540e\u901a\u8fc7memblock_add()\u51fd\u6570\u6dfb\u52a0\u5230memblock\u4e2d<\/p>\n<p>4\u3001memblock\u662f\u4e00\u79cd\u65e9\u671f\u5185\u5b58\u5206\u914d\u5668\uff0c\u7528\u4e8e\u5728\u5185\u6838\u542f\u52a8\u521d\u671f\u8fdb\u884c\u7b80\u5355\u7684\u5185\u5b58\u7ba1\u7406<\/p>\n<p>5\u3001\u5728\u5185\u5b58\u6620\u5c04\u65f6\uff0c\u9700\u8981\u9996\u5148\u6e05\u7406\u9875\u8868\uff08prepare_page_table()-&gt;pmd_clear()\uff09\uff0c\u7136\u540e\u521b\u5efa\u65b0\u7684\u9875\u8868\uff08map_lowmem()-&gt;create_mapping()\uff09\uff0c\u5b8c\u6210\u521d\u59cb\u5316<\/p>\n<p>6\u3001\u4e0a\u8ff0\u7684\u521d\u59cb\u5316\u8fc7\u7a0b\uff08\u7269\u7406\u5185\u5b58\u6620\u5c04\uff09\u4f1a\u4ece\u5185\u5b58\u5f00\u59cb\u5904\uff08dts\u4e2d\uff09\u8986\u76d6\u81f3arm_lowmem_limit\u5904<\/p>\n<p>7\u3001\u7d27\u63a5\u7740\u4f1a\u8fdb\u884czone\u521d\u59cb\u5316\uff0c\u4e3b\u8981\u5728bootmem_init\u51fd\u6570\u4e2d\u5b8c\u6210<\/p>\n<p>8\u3001zone\u521d\u59cb\u5316\u5b8c\u6210\u540e\uff0c\u5185\u6838\u4f1a\u8fdb\u884c\u7a7a\u95f4\u5212\u5206\uff0c\u901a\u5e38\u7528\u6237\u7a7a\u95f4\u548c\u5185\u6838\u7a7a\u95f4\u6309\u71673\uff1a1\u5212\u5206<\/p>\n<h2>\u4e8c\u3001\u7269\u7406\u9875\u9762\u5206\u914d<\/h2>\n<p>1\u3001\u5185\u6838\u4f7f\u7528alloc_pages()\u51fd\u6570\u6765\u5206\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u4e86i\u6309\u9700\u7684\u7269\u7406\u9875\u9762\uff0c\u4f46\u5206\u914d\u7684\u9875\u9762\u53ea\u80fd\u662f2\u7684\u6574\u6570\u5e42\u6b21\u4e2a<\/p>\n<p>2\u3001<\/p>\n<h2>\u4e09\u3001\u5185\u5b58\u7ba1\u7406<\/h2>\n<p>1\u3001\u5185\u5b58\u8282\u70b9\u7684\u6982\u5ff5\u662f\u9488\u5bf9\u5927\u578b\u673a\u5668\uff08\u591a\u5904\u7406\u5668\u673a\u5668\uff09\u800c\u63d0\u51fa\u7684\uff0c\u5355\u5904\u7406\u5668\u673a\u5668\u4e2d\u4e5f\u6709\u8fd9\u4e2a\u673a\u5236\uff08\u786c\u4ef6\u517c\u5bb9\u6027\uff09\uff0c\u53ea\u4e0d\u8fc7\u53ea\u4f7f\u7528\u4e00\u4e2a\u8282\u70b9\u6765\u7ba1\u7406\u5185\u5b58<\/p>\n<p>2\u3001\u5728NUMA\u6a21\u5f0f\u4e0b\uff0cCPU\u88ab\u5212\u5206\u6210\u591a\u4e2a\u8282\u70b9\uff0c\u6839\u636e\u5904\u7406\u5668\u8bbf\u95ee\u5185\u5b58\u7684\u4ee3\u4ef7\uff08\u8ddd\u79bb\uff09\u4e0d\u540c\uff0c\u5c06\u4e0d\u540c\u7684\u5185\u5b58\u7c07\u6307\u6d3e\u7ed9\u4e0d\u540c\u7684\u5904\u7406\u5668\uff0c\u8fd9\u6837\u7684\u5185\u5b58\u7c07\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u5185\u5b58\u8282\u70b9<\/p>\n<p>3\u3001\u8282\u70b9\u88ab\u5206\u4e3a\u8bb8\u591azone\uff0c\u7531zone\u7ed3\u6784\u4f53\u63cf\u8ff0\uff0c\u5728\u5b89\u5353\u4f7f\u7528\u7684Linux\u5185\u6838\u4e2d\uff0c\u5b9a\u4e49\u4e86ZONE_DMA,ZONE_DMA32,ZONE_HIGHMEM,ZONE_NORMAL<\/p>\n<p>4\u3001\u8282\u70b9\u3001zone\u3001\u9875\u5173\u7cfb\u5982\u4e0b\u56fe<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/www.wangonc.com\/wp-content\/uploads\/2022\/01\/Untitled-3.png'><img class=\"lazyload lazyload-style-3\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/www.wangonc.com\/wp-content\/uploads\/2022\/01\/Untitled-3.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"Untitled.png\/\" \/><\/div><\/p>\n<p>5\u3001zone\u7ed3\u6784\u4f53\u5b57\u6bb5\u53ca\u4f5c\u7528\u5982\u4e0b\uff08\u65b0\u7248\u6e90\u7801\u4e2d\u8fd8\u5305\u542b\u6709\u5176\u4ed6\u5b57\u6bb5\uff09<\/p>\n<table>\n<thead>\n<tr>\n<th>\u5b57\u6bb5<\/th>\n<th>\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>watermark<\/td>\n<td>\u6bcf\u4e2a zone \u5728\u7cfb\u7edf\u542f\u52a8\u65f6\u4f1a\u8ba1\u7b97\u51fa 3 \u4e2a\u6c34\u4f4d\u503c, \u5206\u522b\u4e3a WMAKR_MIN, WMARK_LOW, WMARK_HIGH \u6c34\u4f4d, \u8fd9\u5728\u9875\u9762\u5206\u914d\u5668\u548c kswapd \u9875\u9762\u56de\u6536\u4e2d\u4f1a\u7528\u5230<\/td>\n<\/tr>\n<tr>\n<td>lowmem_reserve[MAX_NR_ZONES]<\/td>\n<td>zone \u4e2d\u9884\u7559\u7684\u5185\u5b58, \u4e3a\u4e86\u9632\u6b62\u4e00\u4e9b\u4ee3\u7801\u5fc5\u987b\u8fd0\u884c\u5728\u4f4e\u5730\u5740\u533a\u57df\uff0c\u6240\u4ee5\u4e8b\u5148\u4fdd\u7559\u4e00\u4e9b\u4f4e\u5730\u5740\u533a\u57df\u7684\u5185\u5b58<\/td>\n<\/tr>\n<tr>\n<td>pageset<\/td>\n<td>page\u7ba1\u7406\u7684\u6570\u636e\u7ed3\u6784\u5bf9\u8c61\uff0c\u5185\u90e8\u6709\u4e00\u4e2apage\u7684\u5217\u8868(list)\u6765\u7ba1\u7406\u3002\u6bcf\u4e2aCPU\u7ef4\u62a4\u4e00\u4e2apage list\uff0c\u907f\u514d\u81ea\u65cb\u9501\u7684\u51b2\u7a81\u3002\u8fd9\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f\u548cNR_CPUS(CPU\u7684\u6570\u91cf\uff09\u6709\u5173\uff0c\u8fd9\u4e2a\u503c\u662f\u7f16\u8bd1\u7684\u65f6\u5019\u786e\u5b9a\u7684<\/td>\n<\/tr>\n<tr>\n<td>lock<\/td>\n<td>\u5bf9zone\u5e76\u53d1\u8bbf\u95ee\u7684\u4fdd\u62a4\u7684\u81ea\u65cb\u9501<\/td>\n<\/tr>\n<tr>\n<td>free_area[MAX_ORDER]<\/td>\n<td>\u9875\u9762\u4f7f\u7528\u72b6\u6001\u7684\u4fe1\u606f\uff0c\u4ee5\u6bcf\u4e2abit\u6807\u8bc6\u5bf9\u5e94\u7684page\u662f\u5426\u53ef\u4ee5\u5206\u914d<\/td>\n<\/tr>\n<tr>\n<td>lru_lock<\/td>\n<td>LRU(\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7b97\u6cd5)\u7684\u81ea\u65cb\u9501<\/td>\n<\/tr>\n<tr>\n<td>wait_table<\/td>\n<td>\u5f85\u4e00\u4e2apage\u91ca\u653e\u7684\u7b49\u5f85\u961f\u5217\u54c8\u5e0c\u8868\u3002\u5b83\u4f1a\u88abwait_on_page()\uff0cunlock_page()\u51fd\u6570\u4f7f\u7528. \u7528\u54c8\u5e0c\u8868\uff0c\u800c\u4e0d\u7528\u4e00\u4e2a\u7b49\u5f85\u961f\u5217\u7684\u539f\u56e0\uff0c\u9632\u6b62\u8fdb\u7a0b\u957f\u671f\u7b49\u5f85\u8d44\u6e90<\/td>\n<\/tr>\n<tr>\n<td>wait_table_hash_nr_entries<\/td>\n<td>\u54c8\u5e0c\u8868\u4e2d\u7684\u7b49\u5f85\u961f\u5217\u7684\u6570\u91cf<\/td>\n<\/tr>\n<tr>\n<td>zone_pgdat<\/td>\n<td>\u6307\u5411\u8fd9\u4e2azone\u6240\u5728\u7684pglist_data\u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td>zone_start_pfn<\/td>\n<td>\u548cnode_start_pfn\u7684\u542b\u4e49\u4e00\u6837\u3002\u8fd9\u4e2a\u6210\u5458\u662f\u7528\u4e8e\u8868\u793azone\u4e2d\u7684\u5f00\u59cb\u90a3\u4e2apage\u5728\u7269\u7406\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\u7684present_pages\uff0c spanned_pages: \u548cnode\u4e2d\u7684\u7c7b\u4f3c\u7684\u6210\u5458\u542b\u4e49\u4e00\u6837<\/td>\n<\/tr>\n<tr>\n<td>name<\/td>\n<td>zone\u7684\u540d\u5b57\uff0c\u5b57\u7b26\u4e32\u8868\u793a\uff1a \u201cDMA\u201d\uff0c\u201cNormal\u201d \u548c\u201cHighMem\u201d<\/td>\n<\/tr>\n<tr>\n<td>totalreserve_pages<\/td>\n<td>\u6bcf\u4e2a\u533a\u57df\u4fdd\u7559\u7684\u4e0d\u80fd\u88ab\u7528\u6237\u7a7a\u95f4\u5206\u914d\u7684\u9875\u9762\u6570\u76ee<\/td>\n<\/tr>\n<tr>\n<td>ZONE_PADDING<\/td>\n<td>\u7531\u4e8e\u81ea\u65cb\u9501\u9891\u7e41\u7684\u88ab\u4f7f\u7528\uff0c\u56e0\u6b64\u4e3a\u4e86\u6027\u80fd\u4e0a\u7684\u8003\u8651\uff0c\u5c06\u67d0\u4e9b\u6210\u5458\u5bf9\u9f50\u5230cache line\u4e2d\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u6267\u884c\u7684\u6027\u80fd\u3002\u4f7f\u7528\u8fd9\u4e2a\u5b8f\uff0c\u53ef\u4ee5\u786e\u5b9azone-&gt;lock\uff0czone-&gt;lru_lock\uff0czone-&gt;pageset\u8fd9\u4e9b\u6210\u5458\u4f7f\u7528\u4e0d\u540c\u7684cache line.<\/td>\n<\/tr>\n<tr>\n<td>managed_pages<\/td>\n<td>zone \u4e2d\u88ab\u4f19\u4f34\u7cfb\u7edf\u7ba1\u7406\u7684\u9875\u9762\u6570\u91cf<\/td>\n<\/tr>\n<tr>\n<td>spanned_pages<\/td>\n<td>zone \u4e2d\u5305\u542b\u7684\u9875\u9762\u6570\u91cf<\/td>\n<\/tr>\n<tr>\n<td>present_pages<\/td>\n<td>zone \u4e2d\u5b9e\u9645\u7ba1\u7406\u7684\u9875\u9762\u6570\u91cf. \u5bf9\u4e00\u4e9b\u4f53\u7cfb\u7ed3\u6784\u6765\u8bf4, \u5176\u503c\u548c spanned_pages \u76f8\u7b49<\/td>\n<\/tr>\n<tr>\n<td>lruvec<\/td>\n<td>LRU \u94fe\u8868\u96c6\u5408<\/td>\n<\/tr>\n<tr>\n<td>vm_stat<\/td>\n<td>zone \u8ba1\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>6\u3001zone\u7684\u521d\u59cb\u5316\u5728bootmem_init\u51fd\u6570\u4e2d\u5b8c\u6210<\/p>\n<p>7\u3001find_limit\u51fd\u6570\u4e2d\u4f1a\u8ba1\u7b97\u51famin_low_pfn\u3001max_low_pfn\u3001max_pfn,\u5176\u4e2dmin_low_pfn\u662f\u5185\u5b58\u5757\u5f00\u59cb\u5730\u5740\u7684\u9875\u5e27\u53f7\uff0cmax_low_pfn\u662fnormal\u533a\u7ed3\u675f\u7684\u533a\u5e27\u53f7\uff08\u8fd9\u4e2a\u503c\u7531arm_lowmem_init\u5f97\u6765\uff09\uff0cmax_pfn\u662f\u5185\u5b58\u5757\u7ed3\u675f\u7684\u9875\u5e27\u53f7<\/p>\n<h2><strong>Q&amp;A<\/strong><\/h2>\n<p>1\u3001 \u6570\u636e\u5220\u9664\u7684\u539f\u7406\u662f\u4ec0\u4e48\uff0c\u5982\u4f55\u5c06\u6570\u636e\u5220\u9664\u5e72\u51c0?<\/p>\n<p>\u7f8e\u56fd\u56fd\u9632\u90e8\u5728\u300a\u56fd\u5bb6\u5de5\u4e1a\u5b89\u5168\u7a0b\u5e8f\u64cd\u4f5c\u624b\u518c\u300b\u4e2d\u5bf9\u786c\u76d8\u6570\u636e\u6e05\u9664\u4f5c\u4e86\u4e13\u95e8\u7684\u63cf\u8ff0\u3002\u8be5\u624b\u518c\u5efa\u8bae\u5bf9\u6240\u8981\u6e05\u9664\u7684\u6570\u636e\u533a\u8fdb\u884c\u4e09\u6b21\u8986\u76d6\uff0c\u7b2c\u4e00\u6b21\u4f7f\u7528\u4e00\u4e2a8\u4f4d\u7684\u5b57\u7b26\uff0c\u7b2c\u4e8c\u6b21\u4f7f\u7528\u8be5\u5b57\u7b26\u7684\u4e92\u8865\u5b57\u7b26\uff0c\u6700\u540e\u518d\u4f7f\u7528\u968f\u673a\u5b57\u7b26\u8fdb\u884c\u8986\u76d6\u3002\u4fdd\u5b58\u6709\u6781\u7aef\u91cd\u8981\u6570\u636e\u7684\u786c\u76d8\u5e94\u8be5\u88ab\u6c38\u4e45\u6d88\u78c1\u6216\u8005\u5f7b\u5e95\u9500\u6bc1\u3002<\/p>\n<p>\u5bf9\u4e8e\u6570\u636e\u5220\u9664\uff0c\u672c\u8d28\u4e0a\u8bb2\u662f\u5c06\u60f3\u8981\u5220\u9664\u7684\u6570\u636e\u6807\u6240\u5bf9\u5e94\u7a7a\u95f4\u6807\u8bb0\u4e3a\u7a7a\u95f2\uff0c\u8ba9\u5bf9\u5e94\u7a7a\u95f4\u53ef\u4ee5\u91cd\u65b0\u50a8\u5b58\u6570\u636e\uff0c\u4f46\u662f\u539f\u5148\u7684\u6570\u636e\u4ecd\u65e7\u5b58\u5728\u3002<\/p>\n<p>\u5bf9\u4e8eLinux\u6765\u8bf4\uff0c\u5220\u9664\u6587\u4ef6\u7684\u884c\u4e3a\u662f\u5c06inode\u7ed3\u6784\u4f53\u4e2d\u7684i_nlink\u5b57\u6bb5\uff08\u786c\u94fe\u63a5\u6570\u76ee\uff09\u503c\u51cf\u5c11\uff0c\u76f4\u5230i_nlink \u548c i_count \u5747\u4e3a 0 \u65f6\uff0c\u6587\u4ef6\u88ab\u5220\u9664\uff0c\u4e5f\u5c31\u662f\u8bf4\u6587\u4ef6\u540d\u5230inode\u7684\u94fe\u63a5\u5220\u9664\u4e86\uff0c\u78c1\u76d8\u4e0a\u7684\u6570\u636e\u5757\u5e76\u672a\u88ab\u5220\u9664<\/p>\n<p>2\u3001\u6570\u636e\u6062\u590d\u7684\u65b9\u6cd5\u548c\u539f\u7406\u90fd\u662f\u4ec0\u4e48<\/p>\n<p>\u5728Linux\u4e0b\u6062\u590d\u6570\u636e\uff0cext3\u548cext4\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u5143\u6570\u636e\u5728\u65e5\u5fd7\u6587\u4ef6\u4e2d\u7684\u5907\u4efd\u6765\u8fdb\u884c\u6062\u590d\uff0c\u4f46\u65e5\u5fd7\u6587\u4ef6\u7a7a\u95f4\u4f18\u5148\u4e14\u662f\u5faa\u73af\u4f7f\u7528\u7684\uff0c\u6240\u4ee5\u7ecf\u5386\u5927\u91cf\u8bfb\u5199\u540e\u53ef\u80fd\u65e0\u6cd5\u6062\u590d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5185\u5b58\u521d\u59cb\u5316 1\u3001\u5185\u5b58\u7684\u521d\u59cb\u5316\u4e00\u822c\u7531BIOS\u6216boot loader\u5b8c\u6210\uff0c\u7531bios\u6216boot loader [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6612,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,2],"tags":[],"series":[],"class_list":["post-6426","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux-kernel","category-study-notes"],"_links":{"self":[{"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/posts\/6426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/comments?post=6426"}],"version-history":[{"count":4,"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/posts\/6426\/revisions"}],"predecessor-version":[{"id":7393,"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/posts\/6426\/revisions\/7393"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/media\/6612"}],"wp:attachment":[{"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/media?parent=6426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/categories?post=6426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/tags?post=6426"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/series?post=6426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}