Skip to content

Commit 15ec487

Browse files
author
chs
committed
uvm: prevent TLB invalidation races during COW resolution
When a thread takes a page fault which results in COW resolution, other threads in the same process can be concurrently accessing that same mapping on other CPUs. When the faulting thread updates the pmap entry at the end of COW processing, the resulting TLB invalidations to other CPUs are not done atomically, so another thread can write to the new writable page and then a third thread might still read from the old read-only page, resulting in inconsistent views of the page by the latter two threads. Fix this by removing the pmap entry entirely for the original page before we install the new pmap entry for the new page, so that the new page can only be modified after the old page is no longer accessible. This fixes PR 56535 as well as the netbsd versions of problems described in various bug trackers: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=225584 https://reviews.freebsd.org/D14347 golang/go#34988
1 parent 9d303be commit 15ec487

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

sys/uvm/uvm_fault.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: uvm_fault.c,v 1.233 2023/07/17 12:55:37 riastradh Exp $ */
1+
/* $NetBSD: uvm_fault.c,v 1.234 2023/08/13 23:06:07 chs Exp $ */
22

33
/*
44
* Copyright (c) 1997 Charles D. Cranor and Washington University.
@@ -32,7 +32,7 @@
3232
*/
3333

3434
#include <sys/cdefs.h>
35-
__KERNEL_RCSID(0, "$NetBSD: uvm_fault.c,v 1.233 2023/07/17 12:55:37 riastradh Exp $");
35+
__KERNEL_RCSID(0, "$NetBSD: uvm_fault.c,v 1.234 2023/08/13 23:06:07 chs Exp $");
3636

3737
#include "opt_uvmhist.h"
3838

@@ -634,8 +634,17 @@ uvmfault_promote(struct uvm_faultinfo *ufi,
634634
goto done;
635635
}
636636

637-
/* copy page [pg now dirty] */
637+
/*
638+
* copy the page [pg now dirty]
639+
*
640+
* Remove the pmap entry now for the old page at this address
641+
* so that no thread can modify the new page while any thread
642+
* might still see the old page.
643+
*/
638644
if (opg) {
645+
pmap_remove(vm_map_pmap(ufi->orig_map), ufi->orig_rvaddr,
646+
ufi->orig_rvaddr + PAGE_SIZE);
647+
pmap_update(vm_map_pmap(ufi->orig_map));
639648
uvm_pagecopy(opg, pg);
640649
}
641650
KASSERT(uvm_pagegetdirty(pg) == UVM_PAGE_STATUS_DIRTY);

0 commit comments

Comments
 (0)