nullfs: take lower vnode's rangelock for bypass of null_read_pgcache()
tmpfs_read_pgcache() assumes the size cannot become shorter due to
rangelock, and that assumption failed for reads through nullfs.
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.
kern/uipc_shm.c: allow uiomove_object() to not busy object pages
Add the UIOMOVE_OBJ_NOBUSY flag, for reads only. With it,
uiomove_object() wires the object page for the copy instead of busying
it. The caller must guarantee that the page is neither modified, other
than through mappings, nor removed from the object meanwhile.
Use it for:
- tmpfs_read(), where the vnode lock excludes tmpfs writes and truncations;
- posix shm reads, where the shm rangelock excludes them, since all shm
writes and truncations take it;
- ksyms reads, since the ksyms object is private and only written at open.
tmpfs_read_pgcache() keeps busying the pages, because it holds only the
rangelock, and not all writers take it, e.g. nfsd and md(4), or writes
through nullfs, which lock the nullfs vnode's range.
Suggested by: markjDetails
Diff Detail
- Repository
- rG FreeBSD src repository
- Lint
Lint Skipped - Unit
Tests Skipped
Event Timeline
| sys/kern/uipc_shm.c | ||
|---|---|---|
| 239–240 | Use bool? | |
| sys/kern/uipc_shm.c | ||
|---|---|---|
| 239–240 | It is also counter, it is passed to vm_page_unhold_pages(). | |
| sys/kern/uipc_shm.c | ||
|---|---|---|
| 222 | If one is reading data into a buffer that is not entered into the pmap, then we will call vm_fault_quick_hold_pages()->vm_fault() separately for each page in the buffer, no? vm_fault() will not do any prefaulting for anonymous memory. So, this could be quite expensive. Can we get away without busying the page at all when reading? That is, just wire each page and copy. tmpfs and posixshm are responsible for ensuring consistency in the face of concurrent truncation, e.g., with range locking or vnode locking. | |
| 270 | ||
| sys/kern/uipc_shm.c | ||
|---|---|---|
| 239–240 | write is not passed to vm_page_unhold_pages(). held is. write is only used as a bool. | |
| sys/kern/uipc_shm.c | ||
|---|---|---|
| 222 | As it is now, uiomove_object() is a generic function. Utilizing these assumptions would make it tied to the current consumers. I can agree to e.g. add a bool argument specifying 'do not busy on read' and pass true from tmpfs and shm. Are you fine with that? | |
| sys/sys/uio.h | ||
|---|---|---|
| 79 ↗ | (On Diff #194673) | object's |
| sys/fs/tmpfs/tmpfs_vnops.c | ||
|---|---|---|
| 646 ↗ | (On Diff #194676) | Is this comment outdated given what the new comment below states (correctly, AFAICS) ? |
| 650 ↗ | (On Diff #194676) | Minor nit: I'd add a blank line before the multi-line comment. |
| sys/kern/uipc_shm.c | ||
| 313–324 | Can uiomove_fromphys be used here as well? | |