Skip to content

Commit

Permalink
sysinfo: hugepages: more validation checks
Browse files Browse the repository at this point in the history
add more validations when checking for hugepages,
reducing the chance to read random junk in the system.

Signed-off-by: Francesco Romani <[email protected]>
  • Loading branch information
ffromani committed Dec 11, 2023
1 parent e2a1d06 commit 537a43d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 28 deletions.
10 changes: 8 additions & 2 deletions pkg/sysinfo/hugepages.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"strconv"
"strings"

"k8s.io/klog/v2"

"k8s.io/apimachinery/pkg/api/resource"
)

Expand Down Expand Up @@ -70,14 +72,18 @@ func HugepagesForNode(hnd Handle, nodeID int) ([]Hugepages, error) {
}
for _, entry := range entries {
entryName := entry.Name()
entryPath := filepath.Join(hpPath, entryName)
if !entry.IsDir() {
klog.Warningf("unexpected entry in %q: %q - skipped", hpPath, entryName)
continue
}

var hugepageSizeKB int
if n, err := fmt.Sscanf(entryName, "hugepages-%dkB", &hugepageSizeKB); n != 1 || err != nil {
return hugepages, fmt.Errorf("malformed hugepages entry %q", entryName)
}

totalCount, err := readIntFromFile(filepath.Join(entryPath, "nr_hugepages"))
entryPath := filepath.Join(hpPath, entryName)
hpCountPath, err := filepath.EvalSymlinks(filepath.Join(entryPath, "nr_hugepages"))
if err != nil {
return hugepages, fmt.Errorf("cannot clean %q: %w", entryPath, err)
}
Expand Down
26 changes: 3 additions & 23 deletions pkg/sysinfo/hugepages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestHugepagesForNode(t *testing.T) {
}
defer os.RemoveAll(rootDir) // clean up

if err := makeTree(rootDir, 2); err != nil {
if err := makeMemoryTree(rootDir, 2); err != nil {
t.Errorf("failed to setup the fake tree on %q: %v", rootDir, err)
}
if err := setHPCount(rootDir, 0, HugepageSize2Mi, 6); err != nil {
Expand All @@ -92,34 +92,14 @@ func TestHugepagesForNode(t *testing.T) {
if len(hpCounters["hugepages-1Gi"]) != 2 {
t.Errorf("found unexpected 1Gi hugepages")
}
if hpCounters["hugepages-1Gi"][0] != 0 {
if hpCounters["hugepages-1Gi"][0] != 17179869184 { // from makeMemoryTree builtin
t.Errorf("found unexpected 1Gi hugepages for node 0: %v", hpCounters["hugepages-1Gi"][0])
}
if hpCounters["hugepages-1Gi"][1] != 0 {
if hpCounters["hugepages-1Gi"][1] != 17179869184 { // from makeMemoryTree builtin
t.Errorf("found unexpected 1Gi hugepages for node 1: %v", hpCounters["hugepages-1Gi"][1])
}
}

func makeTree(root string, numNodes int) error {
hnd := Handle{root}
for idx := 0; idx < numNodes; idx++ {
for _, size := range []int{HugepageSize2Mi, HugepageSize1Gi} {
path := filepath.Join(
hnd.SysDevicesNodesNodeNth(idx),
"hugepages",
fmt.Sprintf("hugepages-%dkB", size),
)
if err := os.MkdirAll(path, 0755); err != nil {
return err
}
if err := setHPCount(root, idx, size, 0); err != nil {
return err
}
}
}
return nil
}

func setHPCount(root string, nodeID, pageSize, numPages int) error {
hnd := Handle{root}
path := filepath.Join(
Expand Down
6 changes: 3 additions & 3 deletions pkg/sysinfo/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ limitations under the License.
package sysinfo

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -91,12 +92,11 @@ func makeMemoryTree(root string, numNodes int) error {
return err
}

hpPathSizes := []string{"hugepages-1048576kB", "hugepages-2048kB"}
for _, hpPathSize := range hpPathSizes {
for _, size := range []int{HugepageSize2Mi, HugepageSize1Gi} {
hpPath := filepath.Join(
hnd.SysDevicesNodesNodeNth(idx),
"hugepages",
hpPathSize,
fmt.Sprintf("hugepages-%dkB", size),
)
if err := os.MkdirAll(hpPath, 0755); err != nil {
return err
Expand Down

0 comments on commit 537a43d

Please sign in to comment.