我的问题是有多快mprotect
。mprotecting 说 1 MB 的连续内存与 1 GB 的连续内存有什么区别?当然我可以测量时间,但我想知道引擎盖下发生了什么。
问问题
1244 次
2 回答
4
对源的快速检查似乎表明它迭代了所选区域中的进程映射并更改了它们的标志。如果您保护的映射不到整个映射,它将把它分成两个或三个。
所以简而言之,它是O(n)
您n
调用 mmap 的次数。
您可以在/proc/pid/maps
于 2011-08-18T09:26:14.540 回答
1
该区域的页面计数也是 O(n),因为它应该更改所有 PTE 上的访问位(页面转换条目,它描述了 PageTable 中的虚拟->物理页面映射)。调用树:
mprotect
->
mprotect_fixup
->
change_pte_range
http://lxr.free-electrons.com/source/mm/mprotect.c#L32
47 do {
48 oldpte = *pte;
49 if (pte_present(oldpte)) {
50 pte_t ptent;
51
52 ptent = ptep_modify_prot_start(mm, addr, pte);
53 ptent = pte_modify(ptent, newprot);
54
55 /*
56 * Avoid taking write faults for pages we know to be
57 * dirty.
58 */
59 if (dirty_accountable && pte_dirty(ptent))
60 ptent = pte_mkwrite(ptent);
61
62 ptep_modify_prot_commit(mm, addr, pte, ptent);
63 } else if (PAGE_MIGRATION && !pte_file(oldpte)) {
64 swp_entry_t entry = pte_to_swp_entry(oldpte);
65
66 if (is_write_migration_entry(entry)) {
67 /*
68 * A protection check is difficult so
69 * just be safe and disable write
70 */
71 make_migration_entry_read(&entry);
72 set_pte_at(mm, addr, pte,
73 swp_entry_to_pte(entry));
74 }
75 }
76 } while (pte++, addr += PAGE_SIZE, addr != end);
注意增量:addr += PAGE_SIZE, addr != end);
于 2011-08-18T13:16:20.037 回答