Why a locked page must be copied in COW?

Peter Teoh htmldeveloper at gmail.com
Wed Sep 26 11:02:41 EDT 2012

Let me share my thoughts from what I have read elsewhere (same concepts
below applies to many other OS like Solaris too, where a lot of the memory
concepts comes from) - in general understanding the principles so that u
will not get lost in the codes...which can be buggy as well:

a.    COW:   the reason for this is to with efficiency and performance.
When u fork a process, the memory is shared between the parent and child
process, but when the parent or child start writing to the memory, the
sharing has to stop.   so that's the reason for "copy on write".   most of
the shared libraries are only for reading, so u can imagine the huge
saving, since multiple processes will be having a single physical copy of
library in memory.

b.   Locking pages:   generally, if both parent and child are sharing the
same page, then the page should have its write-protection enabled, so that
there is no need to duplicate the page, and there is assurance of reading
the page without fear of being modified.    but if either party want to
write to it, then just make sure the page is first physically duplicated
into another page and then lock is removed, and now u have two unlocked
page accessible via two different process.

make sense?   all those PTE stuff is just making sure that the page is
readable from its respective process, as different process have different
page table.

Just sidetracking a little, this concept is also applicable to filesystem.

On Sat, Sep 15, 2012 at 10:51 PM, Parmenides <mobile.parmenides at gmail.com>wrote:

> Hi,
> The do_wp_page() implements the Copy-On-write approach. I have no idea
> about its doing concerning locked pages.
> static int do_wp_page(struct mm_struct *mm, struct vm_area_struct * vma,
>         unsigned long address, pte_t *page_table, pmd_t *pmd, pte_t pte)
> {
>         old_page = pfn_to_page(pfn);   <-- old_page is in quesiton.
>         ... ... ...
>         if (!TestSetPageLocked(old_page)) {   <-- We test the 'PG_locked'
> flag of old_page.
>     If it is not set, we can further check whether
>     the page should be copied.
>                 int reuse = can_share_swap_page(old_page);
>                 unlock_page(old_page);
>                 if (reuse) {                                   <-- If only
> one
> process owns old_page, we should copy it.
>                         flush_cache_page(vma, address);
>                         entry =
> maybe_mkwrite(pte_mkyoung(pte_mkdirty(pte)),
>                                               vma);
>                         ptep_set_access_flags(vma, address, page_table,
> entry, 1);
>                         update_mmu_cache(vma, address, entry);
>                         pte_unmap(page_table);
>                         spin_unlock(&mm->page_table_lock);
>                         return VM_FAULT_MINOR;
>                 }
>         }
>         pte_unmap(page_table);
>         /*
>          * Ok, we need to copy. Oh, well..
>          */                                                   <-- At this
> point, we should copy old_page.
>         ... ... ...
> }
> In conclusion, if old_page is locked or it is not locked and two or
> more  processes own old_page, we should copy it. I don't understand
> why a locked page must be copied regardless of how many processes own
> it. Why do we give a locked page a special consideration?
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

Peter Teoh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20120926/31813118/attachment.html 

More information about the Kernelnewbies mailing list