kern/uipc_shm.c: uiomove_object() should xbusy the page it writes to
avg wrote:
- vm_pageout_flush() downgrades the page to shared busy and the swap
pager keeps it shared busy and dirty for the whole duration of the
async write; swp_pager_async_iodone() then does vm_page_undirty()
unconditionally;
- uiomove_object_page() grabs the page with VM_ALLOC_SBUSY |
VM_ALLOC_IGN_SBUSY for writes (as well as for reads), so it can get the
page while it is being paged out, and it modifies the page under the
shared busy;
- vm_page_set_dirty() records a modification only on a clean -> dirty
transition (old == 0 && PGA_SWAP_SPACE), that's when the swap copy is
invalidated.
Here is a sequence of events that may result in a data loss race.
P is the laundry thread and the swap I/O completion, W is a thread in write(2):
1. P: vm_pageout_clean() / vm_pageout_flush(): the page is dirty, write
mappings are removed, xbusy is downgraded to sbusy;
2. P: swap_pager_putpages(): a swap block is assigned,
VPO_SWAPINPROG is set, the swap write is started;
3. the device (or a lower layer) takes its copy of the page
4. W: uiomove_object_page(): the page is sbusied alongside P;
5. W: uiomove_fromphys(): the page content is modified;
6. W: vm_page_set_dirty(): the page is already dirty, old != 0, so
nothing happens; PGA_SWAP_SPACE is not set yet anyway;
7. W: vm_page_activate(), vm_page_sunbusy();
8. P: swp_pager_async_iodone(): VPO_SWAPINPROG is cleared,
PGA_SWAP_SPACE is set, vm_page_undirty(),
vm_page_deactivate_noreuse(), vm_page_sunbusy()...
Now the page is clean and it has a valid swap copy, but the copy does
not have the bytes of step 5.
9. P: vm_pageout_scan_inactive(): the page is clean, so it is freed;
10. the next access pages it in from swap without those bytes.
The race window is steps 3-8, that is, from the moment the page content
is captured for the I/O to the I/O completion. A write between steps 2 and
3 is not lost, because it gets into the swap copy.
Mark the page xbusy as it should be when the page is written to.
Reported by: avg
kern/uipc_shm.c: avoid faulting on the busy page
Since the previous commit, uiomove_object_page() xbusies the page for
write. If the source buffer of write(2) maps the page being written,
and reading it needs a page fault, the thread waits for its own busy.
A read(2) into a writable mapping of the same page could already deadlock
even under the shared busy.
Handle this similarly to vn_io_fault(): for userspace io, try to use
uiomove_nofault() first, and fall back to the slow path which holds the
user buffer's page with vm_fault_quick_hold_pages() on EFAULT.Details
Details
Diff Detail
Diff Detail
- Repository
- rG FreeBSD src repository
- Lint
Lint Skipped - Unit
Tests Skipped