Skip to content

Commit 6ddb193

Browse files
authored
leave page cache out of the memory guard's overshoot check (agent-substrate#3476)
Follow-up to agent-substrate#3429, from @aramase's review: agent-substrate#3429 (comment) When the pod can lend a microVM snapshot write all the memory it needs, the actor's cgroup has no `memory.high` during the write, so it can end with a lot of dirty page cache. Putting `memory.high` back reclaims what it can, but gives up on pages still dirty or under writeback: https://github.com/torvalds/linux/blob/v7.0/mm/memcontrol.c#L4409-L4429 The memory guard resumes as soon as the write returns, before teardown, and its overshoot check counted that page cache. So it could kill the paused VM and report the actor crashed for exceeding its memory limit. The overshoot check now leaves reclaimable page cache out too, and both checks take what's left from one `memory.stat` read. The kernel only flushes `memory.stat` past a threshold of updates, so it can trail `memory.current`, and subtracting one from the other could count fresh page cache as used: https://github.com/torvalds/linux/blob/v7.0/mm/memcontrol.c#L561-L564 AI use: AI-assisted, I reviewed and tested it. - [x] Tests pass - [x] Docs updated --------- Signed-off-by: Benjamin Elder <[email protected]>
1 parent a2ab46c commit 6ddb193

4 files changed

Lines changed: 97 additions & 21 deletions

File tree

‎cmd/ateworker-gvisor/memlimit_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ func TestCheckMemoryKillsOncePausesForCheckpoint(t *testing.T) {
109109
const limit = 256 << 20
110110
writeCgroupFiles(t, leaf, map[string]string{
111111
"memory.current": strconv.Itoa(limit+32<<20) + "\n",
112+
"memory.stat": "anon " + strconv.Itoa(limit+32<<20) + "\n",
112113
"cgroup.kill": "",
113114
"cgroup.events": "populated 0\nfrozen 0\n",
114115
})

‎internal/ateworker/cgroup/memguard_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func overLimitLeaf(t *testing.T, limit int) (root, name string) {
3535
}
3636
for f, v := range map[string]string{
3737
"memory.current": strconv.Itoa(limit+32<<20) + "\n",
38+
"memory.stat": "anon " + strconv.Itoa(limit+32<<20) + "\n",
3839
"cgroup.kill": "",
3940
"cgroup.events": "populated 0\nfrozen 0\n",
4041
} {

‎internal/ateworker/cgroup/memwatch.go‎

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ type stallSample struct {
5555
}
5656

5757
// Check reports whether the cgroup in dir is stuck at limit, along with its
58-
// memory.current: stalled on memory for at least memoryStallKill of the last
59-
// memoryStallWindow while hitting its own memory.high with at most 1/16 of the
60-
// limit in reclaimable page cache, or over the limit by more than 1/16 of it
61-
// or memoryOvershootMax at all.
58+
// memory.current. Page cache aside, it is stuck when within 1/16 of the limit
59+
// while stalled on memory for at least memoryStallKill of the last
60+
// memoryStallWindow and hitting its own memory.high, or when over the limit by
61+
// more than 1/16 of it or memoryOvershootMax at all.
6262
//
6363
// Stalls rather than memory.current over the limit, because reclaim keeps
6464
// pulling a throttled cgroup back under memory.high between allocations, and
@@ -71,18 +71,22 @@ func (w *MemoryWatch) Check(dir string, limit uint64, now time.Time) (bool, uint
7171
if err != nil {
7272
return false, 0, err
7373
}
74-
if current > limit+min(limit/16, memoryOvershootMax) {
75-
return true, current, nil
76-
}
74+
// used is part of current, so it's under the limit too, and the rest of
75+
// the cgroup's files needn't be read
7776
if current < limit-limit/16 {
7877
w.samples = w.samples[:0]
7978
return false, current, nil
8079
}
81-
cache, err := fileCache(dir)
80+
// page cache aside, e.g. a snapshot write's dirty pages, which reclaim
81+
// can't drop until they're written back
82+
used, err := memoryUsed(dir)
8283
if err != nil {
8384
return false, current, err
8485
}
85-
if current-min(cache, current) < limit-limit/16 {
86+
if used > limit+min(limit/16, memoryOvershootMax) {
87+
return true, current, nil
88+
}
89+
if used < limit-limit/16 {
8690
w.samples = w.samples[:0]
8791
return false, current, nil
8892
}
@@ -118,20 +122,41 @@ func memoryCurrent(dir string) (uint64, error) {
118122
return strconv.ParseUint(strings.TrimSpace(string(b)), 10, 64)
119123
}
120124

121-
// fileCache is memory.stat's active_file plus inactive_file: the page cache on
122-
// the file LRU, which the kernel can reclaim. shmem sits on the anon LRU, so
123-
// memfd-backed memory such as a gVisor sentry's is not counted.
124-
func fileCache(dir string) (uint64, error) {
125+
// memoryUsed is what the cgroup holds besides page cache, from one
126+
// memory.stat read. memory.stat splits charged memory into anon, file, kernel
127+
// and sock, where file is page cache plus shmem (memfd-backed memory such as a
128+
// gVisor sentry's or a microVM guest's), so this is anon + shmem + kernel +
129+
// sock. The kernel only flushes memory.stat past a threshold of updates, so it
130+
// can trail memory.current, and mixing the two could count fresh page cache as
131+
// used.
132+
// https://github.com/torvalds/linux/blob/v7.0/mm/memcontrol.c#L561-L564
133+
func memoryUsed(dir string) (uint64, error) {
125134
path := filepath.Join(dir, "memory.stat")
126-
active, err := keyed(path, "active_file")
135+
b, err := os.ReadFile(path)
127136
if err != nil {
128137
return 0, err
129138
}
130-
inactive, err := keyed(path, "inactive_file")
131-
if err != nil {
132-
return 0, err
139+
var used uint64
140+
var anon bool
141+
for line := range bytes.Lines(b) {
142+
f := bytes.Fields(line)
143+
if len(f) != 2 {
144+
continue
145+
}
146+
switch k := string(f[0]); k {
147+
case "anon", "shmem", "kernel", "sock":
148+
v, err := strconv.ParseUint(string(f[1]), 10, 64)
149+
if err != nil {
150+
return 0, fmt.Errorf("parsing %s in %q: %w", k, path, err)
151+
}
152+
used += v
153+
anon = anon || k == "anon"
154+
}
155+
}
156+
if !anon {
157+
return 0, fmt.Errorf("no anon in %q", path)
133158
}
134-
return active + inactive, nil
159+
return used, nil
135160
}
136161

137162
// memoryStall is memory.pressure's some total: microseconds during which at
@@ -153,7 +178,7 @@ func memoryStall(dir string) (uint64, error) {
153178
}
154179
}
155180
}
156-
return 0, fmt.Errorf("no some total in %q", path)
181+
return 0, fmt.Errorf("no some line with a total in %q", path)
157182
}
158183

159184
// memoryHighEvents is memory.events.local's high: how many times the cgroup

‎internal/ateworker/cgroup/memwatch_test.go‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ func writeCgroupFiles(t *testing.T, dir string, files map[string]string) {
3333
}
3434
}
3535

36+
// memoryStat is a memory.stat with used bytes outside page cache and cache
37+
// bytes of page cache, split across both file LRUs.
38+
func memoryStat(used, cache uint64) string {
39+
return fmt.Sprintf("anon %d\nfile %d\nkernel 0\nsock 0\nshmem 0\ninactive_file %d\nactive_file %d\n", used, cache, cache-cache/2, cache/2)
40+
}
41+
3642
func TestMemoryWatch(t *testing.T) {
3743
const limit = 256 << 20
3844
type step struct {
@@ -96,6 +102,19 @@ func TestMemoryWatch(t *testing.T) {
96102
{0, limit + limit/16 + 1, 0, 0, true, 0},
97103
},
98104
},
105+
{
106+
// e.g. a snapshot write's dirty pages after memory.high comes back
107+
name: "far over on page cache is not killed",
108+
steps: []step{
109+
{0, 2*limit + memoryOvershootMax, limit, 0, false, 0},
110+
},
111+
},
112+
{
113+
name: "far over besides page cache is killed at once",
114+
steps: []step{
115+
{0, 2*limit + memoryOvershootMax + 1, limit, 0, true, 0},
116+
},
117+
},
99118
{
100119
name: "stalls age out of the window",
101120
steps: []step{
@@ -117,7 +136,7 @@ func TestMemoryWatch(t *testing.T) {
117136
writeCgroupFiles(t, dir, map[string]string{
118137
"memory.events.local": fmt.Sprintf("low 0\nhigh %d\nmax 0\noom 0\noom_kill 0\n", high),
119138
"memory.current": fmt.Sprint(s.current),
120-
"memory.stat": fmt.Sprintf("anon 1\ninactive_file %d\nactive_file 0\n", s.cache),
139+
"memory.stat": memoryStat(s.current-min(s.cache, s.current), s.cache),
121140
"memory.pressure": fmt.Sprintf("some avg10=0.00 avg60=0.00 avg300=0.00 total=%d\nfull avg10=0.00 avg60=0.00 avg300=0.00 total=0\n", s.stallMs*1000),
122141
})
123142
got, current, err := w.Check(dir, limit, start.Add(s.after))
@@ -145,7 +164,7 @@ func TestMemoryWatchCapsOvershoot(t *testing.T) {
145164
dir := t.TempDir()
146165
writeCgroupFiles(t, dir, map[string]string{
147166
"memory.current": fmt.Sprint(tc.current),
148-
"memory.stat": "anon 1\ninactive_file 0\nactive_file 0\n",
167+
"memory.stat": memoryStat(tc.current, 0),
149168
"memory.pressure": "some avg10=0.00 avg60=0.00 avg300=0.00 total=0\nfull avg10=0.00 avg60=0.00 avg300=0.00 total=0\n",
150169
"memory.events.local": "high 0\n",
151170
})
@@ -162,3 +181,33 @@ func TestMemoryWatchMissingCgroup(t *testing.T) {
162181
t.Error("Check() on a missing cgroup succeeded")
163182
}
164183
}
184+
185+
// memory.stat is flushed lazily, so right after a big write it can trail
186+
// memory.current. Fresh page cache counted by memory.current but not yet by
187+
// memory.stat must not look like used memory.
188+
func TestMemoryWatchStaleStat(t *testing.T) {
189+
const limit = 256 << 20
190+
dir := t.TempDir()
191+
writeCgroupFiles(t, dir, map[string]string{
192+
"memory.current": fmt.Sprint(2 * limit),
193+
"memory.stat": memoryStat(limit/2, 0),
194+
"memory.pressure": "some avg10=0.00 avg60=0.00 avg300=0.00 total=0\nfull avg10=0.00 avg60=0.00 avg300=0.00 total=0\n",
195+
"memory.events.local": "high 0\n",
196+
})
197+
var w MemoryWatch
198+
if got, _, err := w.Check(dir, limit, time.Now()); err != nil || got {
199+
t.Errorf("Check() = %v, %v, want false", got, err)
200+
}
201+
}
202+
203+
// Everything but page cache counts as used: anon, shmem such as a guest's
204+
// memfd, kernel and sock.
205+
func TestMemoryUsed(t *testing.T) {
206+
dir := t.TempDir()
207+
writeCgroupFiles(t, dir, map[string]string{
208+
"memory.stat": "anon 1\nfile 104\nkernel 2\nsock 4\nshmem 8\nfile_dirty 16\ninactive_file 32\nactive_file 64\n",
209+
})
210+
if got, err := memoryUsed(dir); err != nil || got != 1+2+4+8 {
211+
t.Errorf("memoryUsed() = %d, %v, want 15", got, err)
212+
}
213+
}

0 commit comments

Comments
 (0)