From 88477bca6e8f83d4c6e8bc7f62b88e7a99d1abee Mon Sep 17 00:00:00 2001 From: zhongjiawei Date: Fri, 23 Feb 2024 14:39:16 +0800 Subject: [PATCH] containerd:disable Transparent HugePage for shim process if SHIM_DISABLE_THP is set (cherry picked from commit 0b41680949f9f0a8518fa1932a3ab469bd2764ec) --- containerd.spec | 8 +- git-commit | 2 +- ...le-Transparent-HugePage-for-shim-pro.patch | 178 ++++++++++++++++++ series.conf | 1 + 4 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 patch/0111-containerd-disable-Transparent-HugePage-for-shim-pro.patch diff --git a/containerd.spec b/containerd.spec index f01a3a1..f49e07c 100644 --- a/containerd.spec +++ b/containerd.spec @@ -2,7 +2,7 @@ %global debug_package %{nil} Version: 1.2.0 Name: containerd -Release: 317 +Release: 318 Summary: An industry-standard container runtime License: ASL 2.0 URL: https://containerd.io @@ -72,6 +72,12 @@ install -p -m 755 bin/ctr $RPM_BUILD_ROOT/%{_bindir}/ctr %{_bindir}/ctr %changelog +* Fri Feb 23 2024 zhongjiawei - 1.2.0-318 +- Type:bugfix +- ID:NA +- SUG:NA +- DESC:disable Transparent HugePage for shim process if SHIM_DISABLE_THP is set + * Tue Feb 06 2024 zhongjiawei - 1.2.0-317 - Type:bugfix - ID:NA diff --git a/git-commit b/git-commit index 8d53332..3cf2d7e 100644 --- a/git-commit +++ b/git-commit @@ -1 +1 @@ -6062133c573a214534f51095586c54aefc690369 +03dd19b172a5ecece6fe3fba8bb241ffaac7deac diff --git a/patch/0111-containerd-disable-Transparent-HugePage-for-shim-pro.patch b/patch/0111-containerd-disable-Transparent-HugePage-for-shim-pro.patch new file mode 100644 index 0000000..be5a841 --- /dev/null +++ b/patch/0111-containerd-disable-Transparent-HugePage-for-shim-pro.patch @@ -0,0 +1,178 @@ +From 746264387fe1f3d0daa17d05ee7a1751f526dde8 Mon Sep 17 00:00:00 2001 +From: Song Zhang +Date: Thu, 21 Dec 2023 18:57:19 +0800 +Subject: [PATCH] containerd: disable Transparent HugePage for shim process if + SHIM_DISABLE_THP is set + +Signed-off-by: Song Zhang +--- + runtime/v1/shim/client/client.go | 13 +++++++ + runtime/v1/shim/reaper.go | 13 +++++++ + sys/thp.go | 34 +++++++++++++++++++ + sys/thp_amd64.go | 3 ++ + sys/thp_arm64.go | 3 ++ + .../github.com/containerd/go-runc/monitor.go | 20 +++++++++-- + 6 files changed, 83 insertions(+), 3 deletions(-) + create mode 100644 sys/thp.go + create mode 100644 sys/thp_amd64.go + create mode 100644 sys/thp_arm64.go + +diff --git a/runtime/v1/shim/client/client.go b/runtime/v1/shim/client/client.go +index 4c134c2..29a89e7 100644 +--- a/runtime/v1/shim/client/client.go ++++ b/runtime/v1/shim/client/client.go +@@ -105,9 +105,22 @@ func WithStart(binary, address, daemonAddress, cgroup string, debug bool, exitHa + if err != nil { + return nil, nil, err + } ++ // Set THP disabled for shim process ++ if err := sys.SetTHPState(1, false); err != nil { ++ if err := sys.SetTHPState(0, true); err != nil { ++ return nil, nil, err ++ } ++ return nil, nil, err ++ } + if err := cmd.Start(); err != nil { ++ if err := sys.SetTHPState(0, true); err != nil { ++ return nil, nil, err ++ } + return nil, nil, errors.Wrapf(err, "failed to start shim") + } ++ if err := sys.SetTHPState(0, true); err != nil { ++ return nil, nil, err ++ } + defer func() { + if err != nil { + cmd.Process.Kill() +diff --git a/runtime/v1/shim/reaper.go b/runtime/v1/shim/reaper.go +index f5f8096..03759a6 100644 +--- a/runtime/v1/shim/reaper.go ++++ b/runtime/v1/shim/reaper.go +@@ -76,9 +76,22 @@ type Monitor struct { + + // Start starts the command a registers the process with the reaper + func (m *Monitor) Start(c *exec.Cmd) (chan runc.Exit, error) { ++ // Set THP enabled for subprocess. ++ if err := sys.SetTHPState(0, false); err != nil { ++ if err := sys.SetTHPState(1, true); err != nil { ++ return nil, err ++ } ++ return nil, err ++ } + ec := m.Subscribe() + if err := c.Start(); err != nil { + m.Unsubscribe(ec) ++ if err := sys.SetTHPState(1, true); err != nil { ++ return nil, err ++ } ++ return nil, err ++ } ++ if err := sys.SetTHPState(1, true); err != nil { + return nil, err + } + return ec, nil +diff --git a/sys/thp.go b/sys/thp.go +new file mode 100644 +index 0000000..3bbbd00 +--- /dev/null ++++ b/sys/thp.go +@@ -0,0 +1,34 @@ ++package sys ++ ++import ( ++ "os" ++ "runtime" ++ "syscall" ++ ++ "github.com/sirupsen/logrus" ++) ++ ++const ( ++ PR_SET_THP_DISABLE = 41 ++) ++ ++func SetTHPState(flag int, resume bool) error { ++ logrus.Debug("start to set THP") ++ if os.Getenv("SHIM_DISABLE_THP") != "1" { ++ logrus.Debug("skip set THP") ++ return nil ++ } ++ ++ if resume { ++ defer runtime.UnlockOSThread() ++ } else { ++ runtime.LockOSThread() ++ } ++ ++ _, _, errno := syscall.RawSyscall6(uintptr(PRCTL_SYSCALL), uintptr(PR_SET_THP_DISABLE), uintptr(flag), 0, 0, 0, 0) ++ if errno != 0 { ++ logrus.Errorf("disable THP failed: %v", errno) ++ return errno ++ } ++ return nil ++} +diff --git a/sys/thp_amd64.go b/sys/thp_amd64.go +new file mode 100644 +index 0000000..e1e977e +--- /dev/null ++++ b/sys/thp_amd64.go +@@ -0,0 +1,3 @@ ++package sys ++ ++const PRCTL_SYSCALL = 157 +diff --git a/sys/thp_arm64.go b/sys/thp_arm64.go +new file mode 100644 +index 0000000..a6db8d6 +--- /dev/null ++++ b/sys/thp_arm64.go +@@ -0,0 +1,3 @@ ++package sys ++ ++const PRCTL_SYSCALL = 167 +diff --git a/vendor/github.com/containerd/go-runc/monitor.go b/vendor/github.com/containerd/go-runc/monitor.go +index bb8bbab..a9ac837 100644 +--- a/vendor/github.com/containerd/go-runc/monitor.go ++++ b/vendor/github.com/containerd/go-runc/monitor.go +@@ -17,14 +17,15 @@ + package runc + + import ( +- "os/exec" +- "syscall" +- "time" + "io/ioutil" ++ "os/exec" + "path/filepath" + "strconv" + "strings" ++ "syscall" ++ "time" + ++ "github.com/containerd/containerd/sys" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + ) +@@ -54,7 +55,20 @@ type defaultMonitor struct { + } + + func (m *defaultMonitor) Start(c *exec.Cmd) (chan Exit, error) { ++ // Set THP enabled for runc process. ++ if err := sys.SetTHPState(0, false); err != nil { ++ if err := sys.SetTHPState(1, true); err != nil { ++ return nil, err ++ } ++ return nil, err ++ } + if err := c.Start(); err != nil { ++ if err := sys.SetTHPState(1, true); err != nil { ++ return nil, err ++ } ++ return nil, err ++ } ++ if err := sys.SetTHPState(1, true); err != nil { + return nil, err + } + ec := make(chan Exit, 1) +-- +2.33.0 + diff --git a/series.conf b/series.conf index e1af056..66dda09 100644 --- a/series.conf +++ b/series.conf @@ -118,4 +118,5 @@ patch/0107-containerd-Fix-missing-closed-fifo.patch patch/0108-containerd-Update-TTRPC-and-Protobuild-dependencies.patch patch/0109-containerd-Backport-net-http-regenerate-h2_bundle.go.patch patch/0110-containerd-update-vendored-golang.org-x-net.patch +patch/0111-containerd-disable-Transparent-HugePage-for-shim-pro.patch # end -- Gitee