malloc 什么时候处理从虚拟地址到物理地址的映射?

When does malloc handle the mapping from virtual address to physics one?

我正在尝试学习 Linux 内存管理。但是 malloc 行为让我很困惑。

之前,我虽然 malloc() 只是创建了 vma 范围,所以应用程序可以访问这些虚拟地址。当它们第一次被触摸时,handle_mm_fault 将最终处理与 do_anonymous_page 的映射。

但是我做了一个exp,发现好像不对。我用一个c app到malloc 2GB内存(总共524,288页)并遍历它。使用 kretprobe,我只被中断了大约 2000 次 handle_mm_fault 但不是 524,288 页。

所以,当虚拟地址到物理地址映射是在 malloc 中创建的?


一些细节:

内核:Linux 4.11.0

gcc : gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609


vpn 当 do_anonymous_page 被调用时:

PID virtual address diff with pre
17562   7f40ac54b000    
17562   7f40ac54c000    4096
17562   7f40ac54d000    4096
17562   7f40ac54e000    4096
17562   7f40ac54f000    4096
17562   7f40ac550000    4096
17562   7f40ac551000    4096
17562   7f40ac552000    4096
17562   7f40ac553000    4096
17562   7f40ac554000    4096
17562   7f40ac555000    4096
17562   7f40ac556000    4096
17562   7f40ac557000    4096
17562   7f40ac558000    4096
17562   7f40ac559000    4096
17562   7f40ac55a000    4096
17562   7f40ac55b000    4096
17562   7f40ac55c000    4096
17562   7f40ac55d000    4096
17562   7f40ac55e000    4096
17562   7f40ac55f000    4096
17562   7f40ac560000    4096
17562   7f40ac561000    4096
17562   7f40ac562000    4096
17562   7f40ac563000    4096
17562   7f40ac564000    4096
17562   7f40ac565000    4096
17562   7f40ac566000    4096
17562   7f40ac567000    4096
17562   7f40ac568000    4096
17562   7f40ac569000    4096
17562   7f40ac56a000    4096
17562   7f40ac56b000    4096
17562   7f40ac56c000    4096
17562   7f40ac56d000    4096
17562   7f40ac56e000    4096
17562   7f40ac56f000    4096
17562   7f40ac570000    4096
17562   7f40ac571000    4096
17562   7f40ac572000    4096
17562   7f40ac573000    4096
17562   7f40ac574000    4096
17562   7f40ac575000    4096
17562   7f40ac576000    4096
17562   7f40ac577000    4096
17562   7f40ac578000    4096
17562   7f40ac579000    4096
17562   7f40ac57a000    4096
17562   7f40ac57b000    4096
17562   7f40ac57c000    4096
17562   7f40ac57d000    4096
17562   7f40ac57e000    4096
17562   7f40ac57f000    4096
17562   7f40ac580000    4096
17562   7f40ac581000    4096
17562   7f40ac582000    4096
17562   7f40ac583000    4096
17562   7f40ac584000    4096
17562   7f40ac585000    4096
17562   7f40ac586000    4096
17562   7f40ac587000    4096
17562   7f40ac588000    4096
17562   7f40ac589000    4096
17562   7f40ac58a000    4096
17562   7f40ac58b000    4096
17562   7f40ac58c000    4096
17562   7f40ac58d000    4096
17562   7f40ac58e000    4096
17562   7f40ac58f000    4096
17562   7f40ac590000    4096
17562   7f40ac591000    4096
17562   7f40ac592000    4096
17562   7f40ac593000    4096
17562   7f40ac594000    4096
17562   7f40ac595000    4096
17562   7f40ac596000    4096
17562   7f40ac597000    4096
17562   7f40ac598000    4096
17562   7f40ac599000    4096
17562   7f40ac59a000    4096
17562   7f40ac59b000    4096
17562   7f40ac59c000    4096
17562   7f40ac59d000    4096
17562   7f40ac59e000    4096
17562   7f40ac59f000    4096
17562   7f40ac5a0000    4096
17562   7f40ac5a1000    4096
17562   7f40ac5a2000    4096
17562   7f40ac5a3000    4096
17562   7f40ac5a4000    4096
17562   7f40ac5a5000    4096
17562   7f40ac5a6000    4096
17562   7f40ac5a7000    4096
17562   7f40ac5a8000    4096
17562   7f40ac5a9000    4096
17562   7f40ac5aa000    4096
17562   7f40ac5ab000    4096
17562   7f40ac5ac000    4096
17562   7f40ac5ad000    4096
17562   7f40ac5ae000    4096
17562   7f40ac5af000    4096
17562   7f40ac5b0000    4096
17562   7f40ac5b1000    4096
17562   7f40ac5b2000    4096
17562   7f40ac5b3000    4096
17562   7f40ac5b4000    4096
17562   7f40ac5b5000    4096
17562   7f40ac5b6000    4096
17562   7f40ac5b7000    4096
17562   7f40ac5b8000    4096
17562   7f40ac5b9000    4096
17562   7f40ac5ba000    4096
17562   7f40ac5bb000    4096
17562   7f40ac5bc000    4096
17562   7f40ac5bd000    4096
17562   7f40ac5be000    4096
17562   7f40ac5bf000    4096
17562   7f40ac5c0000    4096
17562   7f40ac5c1000    4096
17562   7f40ac5c2000    4096
17562   7f40ac5c3000    4096
17562   7f40ac5c4000    4096
17562   7f40ac5c5000    4096
17562   7f40ac5c6000    4096
17562   7f40ac5c7000    4096
17562   7f40ac5c8000    4096
17562   7f40ac5c9000    4096
17562   7f40ac5ca000    4096
17562   7f40ac5cb000    4096
17562   7f40ac5cc000    4096
17562   7f40ac5cd000    4096
17562   7f40ac5ce000    4096
17562   7f40ac5cf000    4096
17562   7f40ac5d0000    4096
17562   7f40ac5d1000    4096
17562   7f40ac5d2000    4096
17562   7f40ac5d3000    4096
17562   7f40ac5d4000    4096
17562   7f40ac5d5000    4096
17562   7f40ac5d6000    4096
17562   7f40ac5d7000    4096
17562   7f40ac5d8000    4096
17562   7f40ac5d9000    4096
17562   7f40ac5da000    4096
17562   7f40ac5db000    4096
17562   7f40ac5dc000    4096
17562   7f40ac5dd000    4096
17562   7f40ac5de000    4096
17562   7f40ac5df000    4096
17562   7f40ac5e0000    4096
17562   7f40ac5e1000    4096
17562   7f40ac5e2000    4096
17562   7f40ac5e3000    4096
17562   7f40ac5e4000    4096
17562   7f40ac5e5000    4096
17562   7f40ac5e6000    4096
17562   7f40ac5e7000    4096
17562   7f40ac5e8000    4096
17562   7f40ac5e9000    4096
17562   7f40ac5ea000    4096
17562   7f40ac5eb000    4096
17562   7f40ac5ec000    4096
17562   7f40ac5ed000    4096
17562   7f40ac5ee000    4096
17562   7f40ac5ef000    4096
17562   7f40ac5f0000    4096
17562   7f40ac5f1000    4096
17562   7f40ac5f2000    4096
17562   7f40ac5f3000    4096
17562   7f40ac5f4000    4096
17562   7f40ac5f5000    4096
17562   7f40ac5f6000    4096
17562   7f40ac5f7000    4096
17562   7f40ac5f8000    4096
17562   7f40ac5f9000    4096
17562   7f40ac5fa000    4096
17562   7f40ac5fb000    4096
17562   7f40ac5fc000    4096
17562   7f40ac5fd000    4096
17562   7f40ac5fe000    4096
17562   7f40ac5ff000    4096
17562   7f412c400000    2145390592
17562   7f412c401000    4096
17562   7f412c402000    4096
17562   7f412c403000    4096
17562   7f412c404000    4096
17562   7f412c405000    4096
17562   7f412c406000    4096
17562   7f412c407000    4096
17562   7f412c408000    4096
17562   7f412c409000    4096
17562   7f412c40a000    4096
17562   7f412c40b000    4096
17562   7f412c40c000    4096
17562   7f412c40d000    4096
17562   7f412c40e000    4096
17562   7f412c40f000    4096
17562   7f412c410000    4096
17562   7f412c411000    4096
17562   7f412c412000    4096
17562   7f412c413000    4096
17562   7f412c414000    4096
17562   7f412c415000    4096
17562   7f412c416000    4096
17562   7f412c417000    4096
17562   7f412c418000    4096
17562   7f412c419000    4096
17562   7f412c41a000    4096
17562   7f412c41b000    4096
17562   7f412c41c000    4096
17562   7f412c41d000    4096
17562   7f412c41e000    4096
17562   7f412c41f000    4096
17562   7f412c420000    4096
17562   7f412c421000    4096
17562   7f412c422000    4096
17562   7f412c423000    4096
17562   7f412c424000    4096
17562   7f412c425000    4096
17562   7f412c426000    4096
17562   7f412c427000    4096
17562   7f412c428000    4096
17562   7f412c429000    4096
17562   7f412c42a000    4096
17562   7f412c42b000    4096
17562   7f412c42c000    4096
17562   7f412c42d000    4096
17562   7f412c42e000    4096
17562   7f412c42f000    4096
17562   7f412c430000    4096
17562   7f412c431000    4096
17562   7f412c432000    4096
17562   7f412c433000    4096
17562   7f412c434000    4096
17562   7f412c435000    4096
17562   7f412c436000    4096
17562   7f412c437000    4096
17562   7f412c438000    4096
17562   7f412c439000    4096
17562   7f412c43a000    4096
17562   7f412c43b000    4096
17562   7f412c43c000    4096
17562   7f412c43d000    4096
17562   7f412c43e000    4096
17562   7f412c43f000    4096
17562   7f412c440000    4096
17562   7f412c441000    4096
17562   7f412c442000    4096
17562   7f412c443000    4096
17562   7f412c444000    4096
17562   7f412c445000    4096
17562   7f412c446000    4096
17562   7f412c447000    4096
17562   7f412c448000    4096
17562   7f412c449000    4096
17562   7f412c44a000    4096
17562   7f412c44b000    4096
17562   7f412c44c000    4096
17562   7f412c44d000    4096
17562   7f412c44e000    4096
17562   7f412c44f000    4096
17562   7f412c450000    4096
17562   7f412c451000    4096
17562   7f412c452000    4096
17562   7f412c453000    4096
17562   7f412c454000    4096
17562   7f412c455000    4096
17562   7f412c456000    4096
17562   7f412c457000    4096
17562   7f412c458000    4096
17562   7f412c459000    4096
17562   7f412c45a000    4096
17562   7f412c45b000    4096
17562   7f412c45c000    4096
17562   7f412c45d000    4096
17562   7f412c45e000    4096
17562   7f412c45f000    4096
17562   7f412c460000    4096
17562   7f412c461000    4096
17562   7f412c462000    4096
17562   7f412c463000    4096
17562   7f412c464000    4096
17562   7f412c465000    4096
17562   7f412c466000    4096
17562   7f412c467000    4096
17562   7f412c468000    4096
17562   7f412c469000    4096
17562   7f412c46a000    4096
17562   7f412c46b000    4096
17562   7f412c46c000    4096
17562   7f412c46d000    4096
17562   7f412c46e000    4096
17562   7f412c46f000    4096
17562   7f412c470000    4096
17562   7f412c471000    4096
17562   7f412c472000    4096
17562   7f412c473000    4096
17562   7f412c474000    4096
17562   7f412c475000    4096
17562   7f412c476000    4096
17562   7f412c477000    4096
17562   7f412c478000    4096
17562   7f412c479000    4096
17562   7f412c47a000    4096
17562   7f412c47b000    4096
17562   7f412c47c000    4096
17562   7f412c47d000    4096
17562   7f412c47e000    4096
17562   7f412c47f000    4096
17562   7f412c480000    4096
17562   7f412c481000    4096
17562   7f412c482000    4096
17562   7f412c483000    4096
17562   7f412c484000    4096
17562   7f412c485000    4096
17562   7f412c486000    4096
17562   7f412c487000    4096
17562   7f412c488000    4096
17562   7f412c489000    4096
17562   7f412c48a000    4096
17562   7f412c48b000    4096
17562   7f412c48c000    4096
17562   7f412c48d000    4096
17562   7f412c48e000    4096
17562   7f412c48f000    4096
17562   7f412c490000    4096
17562   7f412c491000    4096
17562   7f412c492000    4096
17562   7f412c493000    4096
17562   7f412c494000    4096
17562   7f412c495000    4096
17562   7f412c496000    4096
17562   7f412c497000    4096
17562   7f412c498000    4096
17562   7f412c499000    4096
17562   7f412c49a000    4096
17562   7f412c49b000    4096
17562   7f412c49c000    4096
17562   7f412c49d000    4096
17562   7f412c49e000    4096
17562   7f412c49f000    4096
17562   7f412c4a0000    4096
17562   7f412c4a1000    4096
17562   7f412c4a2000    4096
17562   7f412c4a3000    4096
17562   7f412c4a4000    4096
17562   7f412c4a5000    4096
17562   7f412c4a6000    4096
17562   7f412c4a7000    4096
17562   7f412c4a8000    4096
17562   7f412c4a9000    4096
17562   7f412c4aa000    4096
17562   7f412c4ab000    4096
17562   7f412c4ac000    4096
17562   7f412c4ad000    4096
17562   7f412c4ae000    4096
17562   7f412c4af000    4096
17562   7f412c4b0000    4096
17562   7f412c4b1000    4096
17562   7f412c4b2000    4096
17562   7f412c4b3000    4096
17562   7f412c4b4000    4096
17562   7f412c4b5000    4096
17562   7f412c4b6000    4096
17562   7f412c4b7000    4096
17562   7f412c4b8000    4096
17562   7f412c4b9000    4096
17562   7f412c4ba000    4096
17562   7f412c4bb000    4096
17562   7f412c4bc000    4096
17562   7f412c4bd000    4096
17562   7f412c4be000    4096
17562   7f412c4bf000    4096
17562   7f412c4c0000    4096
17562   7f412c4c1000    4096
17562   7f412c4c2000    4096
17562   7f412c4c3000    4096
17562   7f412c4c4000    4096
17562   7f412c4c5000    4096
17562   7f412c4c6000    4096
17562   7f412c4c7000    4096
17562   7f412c4c8000    4096
17562   7f412c4c9000    4096
17562   7f412c4ca000    4096
17562   7f412c4cb000    4096
17562   7f412c4cc000    4096
17562   7f412c4cd000    4096
17562   7f412c4ce000    4096
17562   7f412c4cf000    4096
17562   7f412c4d0000    4096
17562   7f412c4d1000    4096
17562   7f412c4d2000    4096
17562   7f412c4d3000    4096
17562   7f412c4d4000    4096
17562   7f412c4d5000    4096
17562   7f412c4d6000    4096
17562   7f412c4d7000    4096
17562   7f412c4d8000    4096
17562   7f412c4d9000    4096
17562   7f412c4da000    4096
17562   7f412c4db000    4096
17562   7f412c4dc000    4096
17562   7f412c4dd000    4096
17562   7f412c4de000    4096
17562   7f412c4df000    4096
17562   7f412c4e0000    4096
17562   7f412c4e1000    4096
17562   7f412c4e2000    4096
17562   7f412c4e3000    4096
17562   7f412c4e4000    4096
17562   7f412c4e5000    4096
17562   7f412c4e6000    4096
17562   7f412c4e7000    4096
17562   7f412c4e8000    4096
17562   7f412c4e9000    4096
17562   7f412c4ea000    4096
17562   7f412c4eb000    4096
17562   7f412c4ec000    4096
17562   7f412c4ed000    4096
17562   7f412c4ee000    4096
17562   7f412c4ef000    4096
17562   7f412c4f0000    4096
17562   7f412c4f1000    4096
17562   7f412c4f2000    4096
17562   7f412c4f3000    4096
17562   7f412c4f4000    4096
17562   7f412c4f5000    4096
17562   7f412c4f6000    4096
17562   7f412c4f7000    4096
17562   7f412c4f8000    4096
17562   7f412c4f9000    4096
17562   7f412c4fa000    4096
17562   7f412c4fb000    4096
17562   7f412c4fc000    4096
17562   7f412c4fd000    4096
17562   7f412c4fe000    4096
17562   7f412c4ff000    4096
17562   7f412c500000    4096
17562   7f412c501000    4096
17562   7f412c502000    4096
17562   7f412c503000    4096
17562   7f412c504000    4096
17562   7f412c505000    4096
17562   7f412c506000    4096
17562   7f412c507000    4096
17562   7f412c508000    4096
17562   7f412c509000    4096
17562   7f412c50a000    4096
17562   7f412c50b000    4096
17562   7f412c50c000    4096
17562   7f412c50d000    4096
17562   7f412c50e000    4096
17562   7f412c50f000    4096
17562   7f412c510000    4096
17562   7f412c511000    4096
17562   7f412c512000    4096
17562   7f412c513000    4096
17562   7f412c514000    4096
17562   7f412c515000    4096
17562   7f412c516000    4096
17562   7f412c517000    4096
17562   7f412c518000    4096
17562   7f412c519000    4096
17562   7f412c51a000    4096
17562   7f412c51b000    4096
17562   7f412c51c000    4096
17562   7f412c51d000    4096
17562   7f412c51e000    4096
17562   7f412c51f000    4096
17562   7f412c520000    4096
17562   7f412c521000    4096
17562   7f412c522000    4096
17562   7f412c523000    4096
17562   7f412c524000    4096
17562   7f412c525000    4096
17562   7f412c526000    4096
17562   7f412c527000    4096
17562   7f412c528000    4096
17562   7f412c529000    4096
17562   7f412c52a000    4096
17562   7f412c52b000    4096
17562   7f412c52c000    4096
17562   7f412c52d000    4096
17562   7f412c52e000    4096
17562   7f412c52f000    4096
17562   7f412c530000    4096
17562   7f412c531000    4096
17562   7f412c532000    4096
17562   7f412c533000    4096
17562   7f412c534000    4096
17562   7f412c535000    4096
17562   7f412c536000    4096
17562   7f412c537000    4096
17562   7f412c538000    4096
17562   7f412c539000    4096
17562   7f412c53a000    4096
17562   7f412c53b000    4096
17562   7f412c53c000    4096
17562   7f412c53d000    4096
17562   7f412c53e000    4096
17562   7f412c53f000    4096
17562   7f412c540000    4096
17562   7f412c541000    4096
17562   7f412c542000    4096
17562   7f412c543000    4096
17562   7f412c544000    4096
17562   7f412c545000    4096
17562   7f412c546000    4096
17562   7f412c547000    4096
17562   7f412c548000    4096
17562   7f412c549000    4096
17562   7f412c54a000    4096
17562   7f412c54b000    4096

malloc 只是告诉 OS 你想要一个那个大小的块。当您实际访问内存时,映射由 OS 完成。 似乎您的 OS 实际上映射了 1 MB 的块 --> 因此 2000 次中断用于 2 gig。

我找到原因了。那是因为 Transparent Huge Pages.

的用法

似乎Linux会自动将中间部分视为huge page,这就是为什么我只是在do_anonymous_page中丢失了中间对齐部分的页面错误,而未对齐的部分部分,例如第一个和最后一个会得到。

禁用THP后,我可以得到do_anonymous_page中的所有524288错误。