Skip to content

Commit 6996089

Browse files
committed
TOFIX
Signed-off-by: Evan Lezar <[email protected]>
1 parent d4b331f commit 6996089

File tree

5 files changed

+39
-30
lines changed

5 files changed

+39
-30
lines changed

internal/info/proc/devices/devices_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
func TestNvidiaDevices(t *testing.T) {
28-
perDriverDeviceMaps := map[string]map[string]int{
28+
perDriverDeviceMaps := map[string]map[string]uint32{
2929
"pre550": {
3030
"nvidia-frontend": 195,
3131
"nvidia-nvlink": 234,
@@ -100,7 +100,7 @@ func TestProcessDeviceFileLine(t *testing.T) {
100100
testCases := []struct {
101101
line string
102102
name string
103-
major int
103+
major uint32
104104
err bool
105105
}{
106106
{"", "", 0, true},

internal/system/nvdevices/devices.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"errors"
2121
"fmt"
2222
"path/filepath"
23-
"strconv"
2423
"strings"
2524

2625
"github.com/NVIDIA/go-nvlib/pkg/nvlib/device"
@@ -89,25 +88,25 @@ func New(opts ...Option) (*Interface, error) {
8988
func (m *Interface) CreateDeviceNodes(id device.Identifier) error {
9089
switch {
9190
case id.IsGpuIndex():
92-
index, err := strconv.ParseUint(string(id), 10, 32)
91+
gpuIndex, err := toIndex(string(id))
9392
if err != nil {
9493
return fmt.Errorf("invalid GPU index: %v", id)
9594
}
96-
return m.createGPUDeviceNode(uint32(index))
95+
return m.createGPUDeviceNode(gpuIndex)
9796
case id.IsMigIndex():
9897
indices := strings.Split(string(id), ":")
9998
if len(indices) != 2 {
10099
return fmt.Errorf("invalid MIG index %v", id)
101100
}
102-
gpuIndex, err := strconv.ParseUint(indices[0], 10, 32)
101+
gpuIndex, err := toIndex(indices[0])
103102
if err != nil {
104103
return fmt.Errorf("invalid parent index %v: %w", indices[0], err)
105104
}
106-
if err := m.createGPUDeviceNode(uint32(gpuIndex)); err != nil {
105+
if err := m.createGPUDeviceNode(gpuIndex); err != nil {
107106
return fmt.Errorf("failed to create parent device node: %w", err)
108107
}
109108

110-
return m.createMigDeviceNodes(uint32(gpuIndex))
109+
return m.createMigDeviceNodes(gpuIndex)
111110
case id.IsGpuUUID(), id.IsMigUUID(), id == "all":
112111
return m.createAllGPUDeviceNodes()
113112
default:

internal/system/nvdevices/devices_test.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ func TestCreateControlDevices(t *testing.T) {
3030
logger, _ := testlog.NewNullLogger()
3131

3232
nvidiaDevices := devices.New(
33-
devices.WithDeviceToMajor(map[string]int{
33+
devices.WithDeviceToMajor(map[string]uint32{
3434
"nvidia-frontend": 195,
3535
"nvidia-uvm": 243,
3636
}),
3737
)
3838
nvidia550Devices := devices.New(
39-
devices.WithDeviceToMajor(map[string]int{
39+
devices.WithDeviceToMajor(map[string]uint32{
4040
"nvidia": 195,
4141
"nvidia-uvm": 243,
4242
}),
@@ -52,8 +52,8 @@ func TestCreateControlDevices(t *testing.T) {
5252
expectedError error
5353
expectedCalls []struct {
5454
S string
55-
N1 int
56-
N2 int
55+
V1 uint32
56+
V2 uint32
5757
}
5858
}{
5959
{
@@ -63,8 +63,8 @@ func TestCreateControlDevices(t *testing.T) {
6363
mknodeError: nil,
6464
expectedCalls: []struct {
6565
S string
66-
N1 int
67-
N2 int
66+
V1 uint32
67+
V2 uint32
6868
}{
6969
{"/dev/nvidiactl", 195, 255},
7070
{"/dev/nvidia-modeset", 195, 254},
@@ -79,8 +79,8 @@ func TestCreateControlDevices(t *testing.T) {
7979
mknodeError: nil,
8080
expectedCalls: []struct {
8181
S string
82-
N1 int
83-
N2 int
82+
V1 uint32
83+
V2 uint32
8484
}{
8585
{"/dev/nvidiactl", 195, 255},
8686
{"/dev/nvidia-modeset", 195, 254},
@@ -95,8 +95,8 @@ func TestCreateControlDevices(t *testing.T) {
9595
mknodeError: nil,
9696
expectedCalls: []struct {
9797
S string
98-
N1 int
99-
N2 int
98+
V1 uint32
99+
V2 uint32
100100
}{
101101
{"/some/root/dev/nvidiactl", 195, 255},
102102
{"/some/root/dev/nvidia-modeset", 195, 254},
@@ -112,8 +112,8 @@ func TestCreateControlDevices(t *testing.T) {
112112
// We expect the first call to this to fail, and the rest to be skipped
113113
expectedCalls: []struct {
114114
S string
115-
N1 int
116-
N2 int
115+
V1 uint32
116+
V2 uint32
117117
}{
118118
{"/dev/nvidiactl", 195, 255},
119119
},
@@ -132,7 +132,7 @@ func TestCreateControlDevices(t *testing.T) {
132132
for _, tc := range testCases {
133133
t.Run(tc.description, func(t *testing.T) {
134134
mknode := &mknoderMock{
135-
MknodeFunc: func(string, int, int) error {
135+
MknodeFunc: func(string, uint32, uint32) error {
136136
return tc.mknodeError
137137
},
138138
}

internal/system/nvdevices/gpu-device-nodes.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,44 @@ import (
2020
"errors"
2121
"fmt"
2222
"path/filepath"
23+
"strconv"
2324

2425
"github.com/NVIDIA/go-nvlib/pkg/nvpci"
2526

2627
"github.com/NVIDIA/nvidia-container-toolkit/internal/info/proc/devices"
28+
"github.com/NVIDIA/nvidia-container-toolkit/internal/nvcaps"
2729
)
2830

29-
func (m *Interface) createGPUDeviceNode(gpuIndex uint32) error {
31+
type gpuIndex nvcaps.Index
32+
33+
func toIndex(index string) (gpuIndex, error) {
34+
i, err := strconv.ParseUint(index, 10, 32)
35+
if err != nil {
36+
return 0, err
37+
}
38+
return gpuIndex(i), nil
39+
}
40+
41+
func (m *Interface) createGPUDeviceNode(gpu gpuIndex) error {
3042
major, exists := m.Get(devices.NVIDIAGPU)
3143
if !exists {
3244
return fmt.Errorf("failed to determine device major; nvidia kernel module may not be loaded")
3345
}
3446

35-
deviceNodePath := fmt.Sprintf("/dev/nvidia%d", gpuIndex)
36-
if err := m.createDeviceNode(deviceNodePath, major, uint32(gpuIndex)); err != nil {
47+
deviceNodePath := fmt.Sprintf("/dev/nvidia%d", gpu)
48+
if err := m.createDeviceNode(deviceNodePath, major, uint32(gpu)); err != nil {
3749
return fmt.Errorf("failed to create device node %v: %w", deviceNodePath, err)
3850
}
3951
return nil
4052
}
4153

42-
func (m *Interface) createMigDeviceNodes(gpuIndex uint32) error {
54+
func (m *Interface) createMigDeviceNodes(gpu gpuIndex) error {
4355
capsMajor, exists := m.Get("nvidia-caps")
4456
if !exists {
4557
return nil
4658
}
4759
var errs error
48-
for _, capsDeviceMinor := range m.migCaps.FilterForGPU(int(gpuIndex)) {
60+
for _, capsDeviceMinor := range m.migCaps.FilterForGPU(nvcaps.Index(gpu)) {
4961
capDevicePath := capsDeviceMinor.DevicePath()
5062
err := m.createDeviceNode(capDevicePath, capsMajor, uint32(capsDeviceMinor))
5163
errs = errors.Join(errs, fmt.Errorf("failed to create %v: %w", capDevicePath, err))
@@ -62,13 +74,13 @@ func (m *Interface) createAllGPUDeviceNodes() error {
6274
return fmt.Errorf("failed to get GPU information from PCI: %w", err)
6375
}
6476

65-
count := uint32(len(gpus))
77+
count := gpuIndex(len(gpus))
6678
if count == 0 {
6779
return nil
6880
}
6981

7082
var errs error
71-
for gpuIndex := uint32(0); gpuIndex < count; gpuIndex++ {
83+
for gpuIndex := gpuIndex(0); gpuIndex < count; gpuIndex++ {
7284
errs = errors.Join(errs, m.createGPUDeviceNode(gpuIndex))
7385
errs = errors.Join(errs, m.createMigDeviceNodes(gpuIndex))
7486
}

internal/system/nvdevices/mknod.go

-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import (
2525
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
2626
)
2727

28-
type mint uint32
29-
3028
//go:generate moq -fmt=goimports -rm -stub -out mknod_mock.go . mknoder
3129
type mknoder interface {
3230
Mknode(string, uint32, uint32) error

0 commit comments

Comments
 (0)