Question

My question is how fast is mprotect. What will be the difference between mprotecting say 1 MB of contiguous memory as compared to 1 GB of contiguous memory? Of course I can measure the time, but I want to know what goes under the hood.

Était-ce utile?

La solution

A quick check on the source seems to indicate that it iterates over the process mappings in the selected region and change their flags. If you protect less than a whole mapping it'll split it into two or three.

So in short it's O(n) where n is the number of times you've called mmap.

You can see all the current maps in /proc/pid/maps

Autres conseils

It is O(n) on page count in the region too, because it should change a access bits on all PTEs (Page translation Entries, which describes virtual->physical page mappings in PageTable). Calltree:

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);

Note the increment: addr += PAGE_SIZE, addr != end);

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top