Commit 6594e93
committed
mm/fork: same-VA COW stack for deep call chains (opt-in)
Give a forked child its parent's EXACT user stack -- same virtual
addresses, private physical pages -- instead of a relocated copy with a
biased SP. A relocated+biased stack leaves every saved rbp, return
address and &local in the copied frames pointing at the parent's stack
(off by the bias), so a deep unwind in the child dereferences the parent
and faults. tst-fork-deep (fork 12 frames deep, child unwinds all the
way up and _exit()s a checked value) is the test that catches this; it
now passes. Gated behind CONF_fork (default n); conf_fork=0 is unchanged
and builds/boots clean (fork code fully excluded, tst-mmap passes).
Four coupled changes make same-VA correct, layered on the Stage 2 COW
address space and the fork-child lifecycle fix (detached child +
dispose(child) in the reaper):
1. core/mmu.cc: clone_address_space() PRIVATIZES the forking thread's
live stack VA range into the child AS -- fresh private pages that
byte-copy the parent's stack, mapped writable (not COW: OSv runs
kernel code with irqs off on the app stack, a COW fault there is
illegal) at the SAME VA. The parent's PTEs are untouched.
2. arch/x64/fork.cc + libc/process/fork.cc + include/osv/fork.hh: the
child resumes with the caller's FULL callee-saved register context
(rbx, rbp, r12-r15, rsp, rip), captured at fork() entry into
osv::fork_resume_ctx and restored by the child trampoline off a
scratch base register (so the restore never clobbers its own base).
fork() jmps past its own epilogue, so without this the caller would
resume with fork()'s internal register values -- a deep chain that
keeps an accumulator in a callee-saved reg across fork() computes
garbage.
3. arch/x64/arch-switch.hh: defer the fork CR3 switch to EXACTLY the
rsp/rbp swap inside switch_to()'s asm. Loading the incoming AS's
CR3 early, while still executing on the outgoing thread's stack (the
fpu-cw/mxcsr scratch saves), resolved those shared-VA stack writes
through the wrong address space and clobbered a blocked thread's live
stack (root-caused via hardware watchpoint: switch_to wrote garbage
into a blocked parent's on-stack user_mutex slot in AS0).
4. core/lfmutex.cc + core/mmu.cc: a wait_record on a shared kernel
condvar/mutex must live in the identity-mapped kernel heap (coherent
in every AS) whenever a cross-address-space wake is possible. Extend
the Option-A rule from 'current thread is a fork child' to also
cover 'any child address space is live' (live_child_address_spaces
counter), so the parent in AS0 -- woken by a child that walks the
queue through its own page tables -- is safe too.
libc/process/execve.cc: on a fork child, move the thread onto its own
kernel stack before switching to AS0 and running the target, so
reloading CR3 to AS0 cannot alias the same-VA app stack onto the
parent's physical page (which would corrupt a blocked parent). The
child address space is destroyed HERE, on the kernel stack, exactly once:
the reap-time cleanup (libc/process/fork.cc) then sees the thread's AS is
AS0 (!= child_as) and skips its own destroy, so destroy_address_space()
runs once whether the child exits normally (reaper destroys) or execs
(execve destroys) -- no leak, no double-free. A pre-exec existence check
keeps a failed exec returning to the caller.
Validated on x86-64 (m5d.metal, KVM): tst-fork 10/10, tst-fork-cow 6/6,
tst-fork-deep 0 failures, tst-execve 3/3 with clean shutdown (no hang, no
double-free at reap), on smp=1 and smp=4, 5/5 repeat runs; conf_fork=0
builds and boots clean (fork excluded, tst-mmap passes).1 parent a0e9fd8 commit 6594e93
8 files changed
Lines changed: 318 additions & 94 deletions
File tree
- arch
- aarch64
- x64
- core
- include/osv
- libc/process
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | | - | |
| 98 | + | |
99 | 99 | | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
106 | 116 | | |
107 | 117 | | |
108 | 118 | | |
| |||
119 | 129 | | |
120 | 130 | | |
121 | 131 | | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
122 | 160 | | |
123 | 161 | | |
124 | 162 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | | - | |
9 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
10 | 15 | | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
18 | 27 | | |
19 | 28 | | |
20 | 29 | | |
| |||
23 | 32 | | |
24 | 33 | | |
25 | 34 | | |
| 35 | + | |
26 | 36 | | |
27 | 37 | | |
28 | 38 | | |
29 | 39 | | |
30 | 40 | | |
31 | | - | |
| 41 | + | |
| 42 | + | |
32 | 43 | | |
| 44 | + | |
33 | 45 | | |
34 | 46 | | |
35 | 47 | | |
| |||
39 | 51 | | |
40 | 52 | | |
41 | 53 | | |
42 | | - | |
43 | 54 | | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
62 | 63 | | |
63 | 64 | | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
77 | 69 | | |
78 | 70 | | |
79 | | - | |
80 | | - | |
81 | | - | |
| 71 | + | |
82 | 72 | | |
83 | 73 | | |
84 | 74 | | |
85 | 75 | | |
86 | 76 | | |
87 | 77 | | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
88 | 91 | | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
93 | 104 | | |
94 | 105 | | |
95 | 106 | | |
| |||
106 | 117 | | |
107 | 118 | | |
108 | 119 | | |
109 | | - | |
110 | | - | |
| 120 | + | |
| 121 | + | |
111 | 122 | | |
112 | | - | |
| 123 | + | |
113 | 124 | | |
114 | 125 | | |
115 | 126 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | | - | |
21 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
22 | 35 | | |
23 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
24 | 40 | | |
25 | 41 | | |
26 | 42 | | |
| |||
0 commit comments