Skip to content

Commit 86abc85

Browse files
committed
Merge pull request #11473 from Champ-Goblem:shim-add-cgroup-v2-metrics-support
PiperOrigin-RevId: 730560110
2 parents 84670a4 + b602afb commit 86abc85

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+301
-136
lines changed

BUILD

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ go_path(
133133
# binaries have been factored into a cli package, which is
134134
# a good practice in any case.
135135
"//runsc/cli",
136-
"//shim/cli",
136+
"//shim/v1/cli",
137137
"//webhook/pkg/cli",
138138
"//tools/checklocks",
139139

nogo.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ global:
8484
- "panic recovered: .*types/sizes.go:82: assertion failed"
8585
exclude:
8686
# Generated: exempt all.
87-
- pkg/shim/runtimeoptions/runtimeoptions_cri.go
88-
- pkg/shim/runtimeoptions/v14/runtimeoptions_cri.go
87+
- pkg/shim/v1/runtimeoptions/runtimeoptions_cri.go
88+
- pkg/shim/v1/runtimeoptions/v14/runtimeoptions_cri.go
8989
analyzers:
9090
asmdecl:
9191
generated: # Enabled.

pkg/sentry/control/state.go

+1-14
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
"errors"
1919
"fmt"
2020

21-
"gvisor.dev/gvisor/pkg/abi/linux"
22-
"gvisor.dev/gvisor/pkg/log"
2321
"gvisor.dev/gvisor/pkg/sentry/kernel"
2422
"gvisor.dev/gvisor/pkg/sentry/pgalloc"
2523
"gvisor.dev/gvisor/pkg/sentry/state"
@@ -84,18 +82,7 @@ func (s *State) Save(o *SaveOpts, _ *struct{}) error {
8482
Key: o.Key,
8583
Metadata: o.Metadata,
8684
MemoryFileSaveOpts: o.MemoryFileSaveOpts,
87-
Callback: func(err error) {
88-
if err == nil {
89-
log.Infof("Save succeeded: exiting...")
90-
s.Kernel.SetSaveSuccess(false /* autosave */)
91-
} else {
92-
log.Warningf("Save failed: %v", err)
93-
s.Kernel.SetSaveError(err)
94-
}
95-
if !o.Resume {
96-
s.Kernel.Kill(linux.WaitStatusExit(0))
97-
}
98-
},
85+
Resume: o.Resume,
9986
}
10087
if o.HavePagesFile {
10188
saveOpts.PagesMetadata, err = o.ReleaseFD(1)

pkg/sentry/fsimpl/gofer/save_restore.go

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131
"gvisor.dev/gvisor/pkg/sentry/vfs"
3232
)
3333

34+
var _ vfs.FilesystemImplSaveRestoreExtension = (*filesystem)(nil)
35+
3436
// +stateify savable
3537
type savedDentryRW struct {
3638
read bool
@@ -134,6 +136,11 @@ func (d *dentry) beforeSave() {
134136
}
135137
}
136138

139+
// BeforeResume implements vfs.FilesystemImplSaveRestoreExtension.BeforeResume.
140+
func (fs *filesystem) BeforeResume(ctx context.Context) {
141+
fs.savedDentryRW = nil
142+
}
143+
137144
// afterLoad is invoked by stateify.
138145
func (fs *filesystem) afterLoad(ctx goContext.Context) {
139146
fs.mf = pgalloc.MemoryFileFromContext(ctx)

pkg/sentry/fsimpl/proc/tasks.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"gvisor.dev/gvisor/pkg/abi/linux"
2323
"gvisor.dev/gvisor/pkg/context"
2424
"gvisor.dev/gvisor/pkg/errors/linuxerr"
25+
"gvisor.dev/gvisor/pkg/log"
2526
"gvisor.dev/gvisor/pkg/sentry/fsimpl/kernfs"
2627
"gvisor.dev/gvisor/pkg/sentry/kernel"
2728
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
@@ -68,6 +69,14 @@ var _ kernfs.Inode = (*tasksInode)(nil)
6869
func (fs *filesystem) newTasksInode(ctx context.Context, k *kernel.Kernel, pidns *kernel.PIDNamespace, internalData *InternalData) *tasksInode {
6970
root := auth.NewRootCredentials(pidns.UserNamespace())
7071

72+
// /proc is expected to have inode number
73+
// include/linux/proc_ns.h:PROC_ROOT_INO == 1; ensure that this is the case
74+
// by generating its inode number before any of its descendants.
75+
rootIno := fs.NextIno()
76+
if rootIno != 1 {
77+
log.Traceback("proc root inode has number %d", rootIno)
78+
}
79+
7180
contents := map[string]kernfs.Inode{
7281
"cmdline": fs.newInode(ctx, root, 0444, &cmdLineData{}),
7382
"cpuinfo": fs.newInode(ctx, root, 0444, newStaticFileSetStat(cpuInfoData(k))),
@@ -99,7 +108,7 @@ func (fs *filesystem) newTasksInode(ctx context.Context, k *kernel.Kernel, pidns
99108
fs: fs,
100109
fakeCgroupControllers: internalData.Cgroups,
101110
}
102-
inode.InodeAttrs.Init(ctx, root, linux.UNNAMED_MAJOR, fs.devMinor, fs.NextIno(), linux.ModeDirectory|0555)
111+
inode.InodeAttrs.Init(ctx, root, linux.UNNAMED_MAJOR, fs.devMinor, rootIno, linux.ModeDirectory|0555)
103112
inode.InitRefs()
104113

105114
inode.OrderedChildren.Init(kernfs.OrderedChildrenOptions{})

pkg/sentry/fsimpl/tmpfs/save_restore.go

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"gvisor.dev/gvisor/pkg/sentry/vfs"
2424
)
2525

26+
var _ vfs.FilesystemImplSaveRestoreExtension = (*filesystem)(nil)
27+
2628
// saveMf is called by stateify.
2729
func (fs *filesystem) saveMf() string {
2830
if !fs.mf.IsSavable() {
@@ -75,6 +77,9 @@ func (fs *filesystem) PrepareSave(ctx context.Context) error {
7577
return nil
7678
}
7779

80+
// BeforeResume implements vfs.FilesystemImplSaveRestoreExtension.BeforeResume.
81+
func (fs *filesystem) BeforeResume(ctx context.Context) {}
82+
7883
// CompleteRestore implements
7984
// vfs.FilesystemImplSaveRestoreExtension.CompleteRestore.
8085
func (fs *filesystem) CompleteRestore(ctx context.Context, opts vfs.CompleteRestoreOptions) error {

pkg/sentry/hostmm/hostmm.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,30 @@ import (
2626
"gvisor.dev/gvisor/pkg/log"
2727
)
2828

29-
// GetTransparentHugepageEnum returns the currently selected option for
29+
// ReadTransparentHugepageEnum returns the currently selected option for
3030
// whichever of
3131
// /sys/kernel/mm/transparent_hugepage/{enabled,shmem_enabled,defrag} is
3232
// specified by filename. (Only the basename is required, not the full path.)
33-
func GetTransparentHugepageEnum(filename string) (string, error) {
33+
func ReadTransparentHugepageEnum(filename string) (string, error) {
3434
pathname := path.Join("/sys/kernel/mm/transparent_hugepage/", filename)
3535
data, err := os.ReadFile(pathname)
3636
if err != nil {
3737
return "", err
3838
}
39+
return GetTransparentHugepageEnum(string(data))
40+
}
41+
42+
// GetTransparentHugepageEnum returns the currently selected option given the
43+
// contents of any of
44+
// /sys/kernel/mm/transparent_hugepage/{enabled,shmem_enabled,defrag}.
45+
func GetTransparentHugepageEnum(data string) (string, error) {
3946
// In these files, the selected option is highlighted by square brackets.
40-
m := regexp.MustCompile(`\[.*\]`).Find(data)
41-
if m == nil {
42-
return "", fmt.Errorf("failed to parse %s: %q", pathname, data)
47+
m := regexp.MustCompile(`\[.*\]`).FindString(data)
48+
if m == "" {
49+
return "", fmt.Errorf("failed to find selected option in %q", data)
4350
}
4451
// Remove the square brackets.
45-
return string(m[1 : len(m)-1]), nil
52+
return m[1 : len(m)-1], nil
4653
}
4754

4855
// NotifyCurrentMemcgPressureCallback requests that f is called whenever the

pkg/sentry/kernel/kernel.go

+5
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,11 @@ func (k *Kernel) SaveTo(ctx context.Context, w, pagesMetadata io.Writer, pagesFi
702702
return nil
703703
}
704704

705+
// BeforeResume is called before the kernel is resumed after save.
706+
func (k *Kernel) BeforeResume(ctx context.Context) {
707+
k.vfs.BeforeResume(ctx)
708+
}
709+
705710
func (k *Kernel) saveMemoryFiles(ctx context.Context, w, pagesMetadata io.Writer, pagesFile *fd.FD, mfsToSave map[string]*pgalloc.MemoryFile, mfOpts pgalloc.SaveOpts) error {
706711
// Save the memory files' state.
707712
memoryStart := time.Now()

pkg/sentry/state/state.go

+23-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"io"
2222

23+
"gvisor.dev/gvisor/pkg/abi/linux"
2324
"gvisor.dev/gvisor/pkg/context"
2425
"gvisor.dev/gvisor/pkg/errors/linuxerr"
2526
"gvisor.dev/gvisor/pkg/fd"
@@ -68,15 +69,18 @@ type SaveOpts struct {
6869
// MemoryFileSaveOpts is passed to calls to pgalloc.MemoryFile.SaveTo().
6970
MemoryFileSaveOpts pgalloc.SaveOpts
7071

71-
// Callback is called prior to unpause, with any save error.
72-
Callback func(err error)
73-
7472
// Resume indicates if the statefile is used for save-resume.
7573
Resume bool
74+
75+
// Autosave indicates if the statefile is used for autosave.
76+
Autosave bool
7677
}
7778

7879
// Save saves the system state.
7980
func (opts SaveOpts) Save(ctx context.Context, k *kernel.Kernel, w *watchdog.Watchdog) error {
81+
t, _ := CPUTime()
82+
log.Infof("Before save CPU usage: %s", t.String())
83+
8084
log.Infof("Sandbox save started, pausing all tasks.")
8185
k.Pause()
8286
k.ReceiveTaskStates()
@@ -127,7 +131,22 @@ func (opts SaveOpts) Save(ctx context.Context, k *kernel.Kernel, w *watchdog.Wat
127131
}
128132
}
129133
}
130-
opts.Callback(err)
134+
135+
t1, _ := CPUTime()
136+
log.Infof("Save CPU usage: %s", (t1 - t).String())
137+
if err == nil {
138+
log.Infof("Save succeeded: exiting...")
139+
k.SetSaveSuccess(opts.Autosave)
140+
} else {
141+
log.Warningf("Save failed: exiting... %v", err)
142+
k.SetSaveError(err)
143+
}
144+
if opts.Resume {
145+
k.BeforeResume(ctx)
146+
} else {
147+
// Kill the sandbox.
148+
k.Kill(linux.WaitStatusExit(0))
149+
}
131150
return err
132151
}
133152

pkg/sentry/vfs/save_restore.go

+15
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ type FilesystemImplSaveRestoreExtension interface {
5454
// PrepareSave prepares this filesystem for serialization.
5555
PrepareSave(ctx context.Context) error
5656

57+
// BeforeResume is called before the kernel is resumed after save. It can be
58+
// used to clean up any state that should be discarded after save.
59+
BeforeResume(ctx context.Context)
60+
5761
// CompleteRestore completes restoration from checkpoint for this
5862
// filesystem after deserialization.
5963
CompleteRestore(ctx context.Context, opts CompleteRestoreOptions) error
@@ -73,6 +77,17 @@ func (vfs *VirtualFilesystem) PrepareSave(ctx context.Context) error {
7377
return nil
7478
}
7579

80+
// BeforeResume is called before the kernel is resumed after save and allows
81+
// filesystems to clean up S/R state.
82+
func (vfs *VirtualFilesystem) BeforeResume(ctx context.Context) {
83+
for fs := range vfs.getFilesystems() {
84+
if ext, ok := fs.impl.(FilesystemImplSaveRestoreExtension); ok {
85+
ext.BeforeResume(ctx)
86+
}
87+
fs.DecRef(ctx)
88+
}
89+
}
90+
7691
// CompleteRestore completes restoration from checkpoint for all filesystems
7792
// after deserialization.
7893
func (vfs *VirtualFilesystem) CompleteRestore(ctx context.Context, opts *CompleteRestoreOptions) error {

pkg/shim/BUILD pkg/shim/v1/BUILD

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ package(
66
)
77

88
go_library(
9-
name = "shim",
9+
name = "v1",
1010
srcs = ["service.go"],
1111
visibility = ["//shim:__subpackages__"],
1212
deps = [
1313
"//pkg/cleanup",
14-
"//pkg/shim/extension",
15-
"//pkg/shim/runsc",
14+
"//pkg/shim/v1/extension",
15+
"//pkg/shim/v1/runsc",
1616
"//pkg/sync",
1717
"@com_github_containerd_containerd//namespaces:go_default_library",
1818
"@com_github_containerd_containerd//runtime/v2/shim:go_default_library",
File renamed without changes.
File renamed without changes.

pkg/shim/proc/BUILD pkg/shim/v1/proc/BUILD

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ go_library(
2525
deps = [
2626
"//pkg/atomicbitops",
2727
"//pkg/cleanup",
28-
"//pkg/shim/extension",
29-
"//pkg/shim/runsccmd",
30-
"//pkg/shim/utils",
28+
"//pkg/shim/v1/extension",
29+
"//pkg/shim/v1/runsccmd",
30+
"//pkg/shim/v1/utils",
3131
"@com_github_containerd_console//:go_default_library",
3232
"@com_github_containerd_containerd//mount:go_default_library",
3333
"@com_github_containerd_containerd//pkg/stdio:go_default_library",

pkg/shim/proc/deleted_state.go pkg/shim/v1/proc/deleted_state.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/containerd/console"
2323
"github.com/containerd/errdefs"
2424
runc "github.com/containerd/go-runc"
25-
"gvisor.dev/gvisor/pkg/shim/extension"
25+
"gvisor.dev/gvisor/pkg/shim/v1/extension"
2626
)
2727

2828
type deletedState struct{}

pkg/shim/proc/exec.go pkg/shim/v1/proc/exec.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import (
3333
specs "github.com/opencontainers/runtime-spec/specs-go"
3434
"golang.org/x/sys/unix"
3535
"gvisor.dev/gvisor/pkg/cleanup"
36-
"gvisor.dev/gvisor/pkg/shim/extension"
37-
"gvisor.dev/gvisor/pkg/shim/runsccmd"
36+
"gvisor.dev/gvisor/pkg/shim/v1/extension"
37+
"gvisor.dev/gvisor/pkg/shim/v1/runsccmd"
3838
)
3939

4040
type execProcess struct {

pkg/shim/proc/exec_state.go pkg/shim/v1/proc/exec_state.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"fmt"
2121

2222
"github.com/containerd/console"
23-
"gvisor.dev/gvisor/pkg/shim/extension"
23+
"gvisor.dev/gvisor/pkg/shim/v1/extension"
2424
)
2525

2626
type execState interface {

pkg/shim/proc/init.go pkg/shim/v1/proc/init.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ import (
3636
runc "github.com/containerd/go-runc"
3737
specs "github.com/opencontainers/runtime-spec/specs-go"
3838
"golang.org/x/sys/unix"
39-
"gvisor.dev/gvisor/pkg/shim/extension"
40-
"gvisor.dev/gvisor/pkg/shim/runsccmd"
41-
"gvisor.dev/gvisor/pkg/shim/utils"
39+
"gvisor.dev/gvisor/pkg/shim/v1/extension"
40+
"gvisor.dev/gvisor/pkg/shim/v1/runsccmd"
41+
"gvisor.dev/gvisor/pkg/shim/v1/utils"
4242
)
4343

4444
const statusStopped = "stopped"

pkg/shim/proc/init_state.go pkg/shim/v1/proc/init_state.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
runc "github.com/containerd/go-runc"
2424
"golang.org/x/sys/unix"
2525

26-
"gvisor.dev/gvisor/pkg/shim/extension"
26+
"gvisor.dev/gvisor/pkg/shim/v1/extension"
2727
)
2828

2929
type stateTransition int
File renamed without changes.
File renamed without changes.
File renamed without changes.

pkg/shim/proc/utils.go pkg/shim/v1/proc/utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"strings"
2323
"time"
2424

25-
"gvisor.dev/gvisor/pkg/shim/runsccmd"
25+
"gvisor.dev/gvisor/pkg/shim/v1/runsccmd"
2626
)
2727

2828
const (

pkg/shim/runsc/BUILD pkg/shim/v1/runsc/BUILD

+9-7
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@ go_library(
2020
visibility = ["//pkg/shim:__subpackages__"],
2121
deps = [
2222
"//pkg/cleanup",
23-
"//pkg/shim/extension",
24-
"//pkg/shim/proc",
25-
"//pkg/shim/runsccmd",
26-
"//pkg/shim/runtimeoptions",
27-
"//pkg/shim/runtimeoptions/v14",
28-
"//pkg/shim/utils",
23+
"//pkg/shim/v1/extension",
24+
"//pkg/shim/v1/proc",
25+
"//pkg/shim/v1/runsccmd",
26+
"//pkg/shim/v1/runtimeoptions",
27+
"//pkg/shim/v1/runtimeoptions/v14",
28+
"//pkg/shim/v1/utils",
2929
"//runsc/specutils",
3030
"@com_github_burntsushi_toml//:go_default_library",
3131
"@com_github_containerd_cgroups//:go_default_library",
3232
"@com_github_containerd_cgroups//stats/v1:go_default_library",
3333
"@com_github_containerd_cgroups//v2:go_default_library",
34+
"@com_github_containerd_cgroups//v2/stats:go_default_library",
3435
"@com_github_containerd_console//:go_default_library",
3536
"@com_github_containerd_containerd//api/events:go_default_library",
3637
"@com_github_containerd_containerd//api/types/task:go_default_library",
@@ -46,6 +47,7 @@ go_library(
4647
"@com_github_containerd_containerd//sys/reaper:go_default_library",
4748
"@com_github_containerd_errdefs//:go_default_library",
4849
"@com_github_containerd_fifo//:go_default_library",
50+
"@com_github_containerd_go_runc//:go_default_library",
4951
"@com_github_containerd_log//:go_default_library",
5052
"@com_github_containerd_typeurl//:go_default_library",
5153
"@com_github_gogo_protobuf//types:go_default_library",
@@ -60,7 +62,7 @@ go_test(
6062
srcs = ["service_test.go"],
6163
library = ":runsc",
6264
deps = [
63-
"//pkg/shim/utils",
65+
"//pkg/shim/v1/utils",
6466
"@com_github_opencontainers_runtime_spec//specs-go:go_default_library",
6567
],
6668
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)