Skip to content

Commit 07696b6

Browse files
committed
Read the cgroup memory limit again before restore
The host can lower a created sandbox's memory limit before restoring it. Pass the current limit into the restore RPC so that GC does not suspend under a budget larger than the enforced limit.
1 parent 874db9f commit 07696b6

6 files changed

Lines changed: 34 additions & 8 deletions

File tree

‎pkg/sentry/state/state.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,10 @@ func totalMemoryLimit() uint64 {
263263
}
264264

265265
// SuspendGCForRestore suspends GC for a checkpoint restore.
266-
func SuspendGCForRestore(metadata map[string]string) *gccontrol.Suspension {
266+
func SuspendGCForRestore(total uint64, metadata map[string]string) *gccontrol.Suspension {
267267
var savedMemoryFileBytes uint64
268268
if v, err := strconv.ParseUint(metadata[metadataMemoryFileUsedBytesKey], 10 /* base */, 64 /* bitSize */); err == nil {
269269
savedMemoryFileBytes = v
270270
}
271-
return gccontrol.Suspend(totalMemoryLimit(), savedMemoryFileBytes)
271+
return gccontrol.Suspend(total, savedMemoryFileBytes)
272272
}

‎pkg/sentry/state/state_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestSuspendGCForRestore(t *testing.T) {
5656
} {
5757
t.Run(tc.name, func(t *testing.T) {
5858
usage.MaximumTotalMemoryBytes = tc.totalMem
59-
s := SuspendGCForRestore(tc.metadata)
59+
s := SuspendGCForRestore(totalMemoryLimit(), tc.metadata)
6060
defer s.End()
6161
if got := debug.SetMemoryLimit(-1 /* limit */); got != tc.want {
6262
t.Fatalf("MemoryLimit = %d, want %d", got, tc.want)

‎runsc/boot/controller.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,10 @@ func (cm *containerManager) Restore(o *sentryapi.RestoreOpts, reply *sentryapi.R
418418
timer.Reached("got restore readers")
419419

420420
cm.restorer = &restorer{
421-
cm: cm,
422-
background: o.Background,
423-
timer: timer,
421+
memoryLimit: o.MemoryLimit,
422+
cm: cm,
423+
background: o.Background,
424+
timer: timer,
424425
}
425426

426427
// Create the main MemoryFile.

‎runsc/boot/restore.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ func newProcInternalData(conf *config.Config, spec *specs.Spec, containerName st
225225
// all containers and triggers the full sandbox restore after the last
226226
// container is restored.
227227
type restorer struct {
228-
mu sync.Mutex
228+
memoryLimit uint64
229+
mu sync.Mutex
229230

230231
// totalContainers is the number of containers expected to be restored in
231232
// the sandbox. Sandbox restore can only happen, after all containers have
@@ -375,7 +376,7 @@ func (r *restorer) restore(l *Loader) error {
375376
// Using cleanup to end gcSuspension on failure.
376377
// On success the ownership is transferred to l.startupGC
377378
// and cleanup is cancelled.
378-
gcSuspension := state.SuspendGCForRestore(r.metadata)
379+
gcSuspension := state.SuspendGCForRestore(r.memoryLimit, r.metadata)
379380
gcCleanup := cleanup.Make(gcSuspension.End)
380381
defer gcCleanup.Clean()
381382

‎runsc/boot/sentryapi/controller.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ const ContMgrRestore = "containerManager.Restore"
112112

113113
// RestoreOpts contains options related to restoring a container's file system.
114114
type RestoreOpts struct {
115+
// MemoryLimit is the host memory bound at restore, or zero if unknown.
116+
MemoryLimit uint64
117+
115118
// FilePayload contains, in order:
116119
// 1. checkpoint state file.
117120
// 2. optional checkpoint pages metadata file.

‎runsc/sandbox/sandbox.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,26 @@ func (d DirectIO) String() string {
605605
panic(fmt.Sprintf("invalid direct I/O mode %d", d))
606606
}
607607

608+
// restoreMemoryLimit reads the current cgroup limit because an OCI update can
609+
// change it between creating the sandbox and restoring it. If the bound is
610+
// unknown, leave GC enabled.
611+
func (s *Sandbox) restoreMemoryLimit() uint64 {
612+
if s.CgroupJSON.Cgroup == nil {
613+
return 0
614+
}
615+
host, err := hostos.TotalSystemMemory()
616+
if err != nil {
617+
log.Warningf("Reading host memory limit for restore: %v", err)
618+
return 0
619+
}
620+
limit, err := s.CgroupJSON.Cgroup.MemoryLimit()
621+
if err != nil {
622+
log.Warningf("Reading cgroup memory limit for restore: %v", err)
623+
return 0
624+
}
625+
return min(host, limit)
626+
}
627+
608628
// Restore sends the restore call for a container in the sandbox.
609629
func (s *Sandbox) Restore(conf *config.Config, spec *specs.Spec, cid string, imagePath string, directIO DirectIO, background bool, networkArgs *sentryapi.CreateLinksAndRoutesArgs) error {
610630
waitRegisteredMetrics := s.fetchRegisteredMetrics()
@@ -683,6 +703,7 @@ func (s *Sandbox) Restore(conf *config.Config, spec *specs.Spec, cid string, ima
683703

684704
// Restore the container and start the root container.
685705
var reply sentryapi.RestoreReply
706+
opt.MemoryLimit = s.restoreMemoryLimit()
686707
if err := conn.Call(sentryapi.ContMgrRestore, &opt, &reply); err != nil {
687708
if opt.UseCheckpointGofer {
688709
if target := getGCSURIFromImagePath(imagePath); target != "" {

0 commit comments

Comments
 (0)