[comp.os.mach] bug in Mach 2.5 in copy-on-write?

shekita@provolone.cs.wisc.edu (E Shekita) (07/21/90)

There seems to be a bug in Mach 2.5 with copy-on-write (COW),
where it forgets to unmark a page as COW once the virtual copy
of the page has taken a COW fault. Here's an example that 
picks up the bug:

-----------------------------------------------------------------------
int     i;			/* index */
char*   source_page;            /* vm_read() source */
char*   destination_page;	/* vm_read() destination */

vm_allocate(task_self(), (vm_address_t*) &source_page, vm_page_size, TRUE);

for (i = 0; i < 100; i++) {
    /*
     * Update the source page. This shouldn't trigger a COW fault.
     */
    source_page[0] = 'a'; 

    /*
     * Virtually copy the source page to the destination page. 
     * After the copy, the source and destination pages should
     * both be marked as COW.
     */
    vm_read(task_self(), (vm_address_t) source_page, vm_page_size, 
	   (vm_address_t*) &destination_page, &readCount);

    /*
     * Update the destination page. This should trigger a COW fault,
     * but it should also unmark the source page as COW.
     */
    destination_page[0] = 'b';
}
---------------------------------------------------------------------------

If you run this loop and calculate the number of COW faults
that occured in it using vm_statistics(), you'll find that the
number of COW faults is 199. Shouldn't it be on the order 
of 100?... Any comments from the CMU folks?

Gene

avie@wb1.cs.cmu.edu (Avadis Tevanian) (08/04/90)

In article <10871@spool.cs.wisc.edu> shekita@provolone.cs.wisc.edu (E Shekita) writes:
>
>for (i = 0; i < 100; i++) {
....
>    source_page[0] = 'a'; 
....
>    vm_read(task_self(), (vm_address_t) source_page, vm_page_size, 
>	   (vm_address_t*) &destination_page, &readCount);
....
>    destination_page[0] = 'b';

No bug here... if you remember that both the source and destination pages
are set to copy-on-write then you realize that each time you touch the 
source page after the vm_read you generate another COW fault.  The result
you get of 199 is exactly as expected.

-- 
Avadis Tevanian, Jr.    (Avie)
Manager, System Software
NeXT, Inc.
avie@NeXT.COM

Richard.Draves@CS.CMU.EDU (08/04/90)

> Excerpts from netnews.comp.os.mach: 4-Aug-90 Re: bug in Mach 2.5 in
> copy.. Avadis Tevanian@wb1.cs.c (661)

> In article <10871@spool.cs.wisc.edu> shekita@provolone.cs.wisc.edu (E
> Shekita) writes:
> >
> >for (i = 0; i < 100; i++) {
> ....
> >    source_page[0] = 'a'; 
> ....
> >    vm_read(task_self(), (vm_address_t) source_page, vm_page_size, 
> >	   (vm_address_t*) &destination_page, &readCount);
> ....
> >    destination_page[0] = 'b';

> No bug here... if you remember that both the source and destination pages
> are set to copy-on-write then you realize that each time you touch the 
> source page after the vm_read you generate another COW fault.  The result
> you get of 199 is exactly as expected.

Well, it isn't a bug, because that is how the VM implementation is
designed to work, but there is a point to be made.  The COW faults on
the source are unnecessary, and a better implementation would avoid them.

Rich