Skip to content

Commit 825eb40

Browse files
nixprimegvisor-bot
authored andcommitted
Internal change.
PiperOrigin-RevId: 833930921
1 parent e3403be commit 825eb40

File tree

17 files changed

+181
-15
lines changed

17 files changed

+181
-15
lines changed

pkg/abi/nvgpu/ctrl.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,10 @@ const (
7575
NV0000_CTRL_CMD_GPU_GET_ACTIVE_DEVICE_IDS = 0x288
7676
NV0000_CTRL_CMD_GPU_ASYNC_ATTACH_ID = 0x289
7777
NV0000_CTRL_CMD_GPU_WAIT_ATTACH_ID = 0x290
78-
)
7978

80-
// From src/common/sdk/nvidia/inc/ctrl/ctrl0000/ctrl0000gsync.h:
81-
const (
82-
NV0000_CTRL_CMD_GSYNC_GET_ATTACHED_IDS = 0x301
79+
NV0000_CTRL_GPU_INVALID_ID = 0xffffffff
80+
NV0000_CTRL_GPU_MAX_PROBED_GPUS = NV_MAX_DEVICES
81+
NV0000_GPU_MAX_GID_LENGTH = 0x100
8382
)
8483

8584
// NV0000_CTRL_GPU_GET_ID_INFO_PARAMS is the param type for NV0000_CTRL_CMD_GPU_GET_ID_INFO,
@@ -98,6 +97,25 @@ type NV0000_CTRL_GPU_GET_ID_INFO_PARAMS struct {
9897
NumaID int32
9998
}
10099

100+
// +marshal
101+
type NV0000_CTRL_GPU_ATTACH_IDS_PARAMS struct {
102+
GPUIDs [NV0000_CTRL_GPU_MAX_PROBED_GPUS]uint32
103+
FailedID uint32
104+
}
105+
106+
// +marshal
107+
type NV0000_CTRL_GPU_GET_UUID_FROM_GPU_ID_PARAMS struct {
108+
GPUID uint32
109+
Flags uint32
110+
GPUUUID [NV0000_GPU_MAX_GID_LENGTH]byte
111+
UUIDStrLen uint32
112+
}
113+
114+
// From src/common/sdk/nvidia/inc/ctrl/ctrl0000/ctrl0000gsync.h:
115+
const (
116+
NV0000_CTRL_CMD_GSYNC_GET_ATTACHED_IDS = 0x301
117+
)
118+
101119
// From src/common/sdk/nvidia/inc/ctrl/ctrl0000/ctrl0000syncgpuboost.h:
102120
const (
103121
NV0000_CTRL_CMD_SYNC_GPU_BOOST_GROUP_INFO = 0xa04

pkg/abi/nvgpu/frontend.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,39 @@ const (
5959
// Frontend ioctl parameter structs, from src/common/sdk/nvidia/inc/nvos.h or
6060
// kernel-open/common/inc/nv-ioctl.h.
6161

62+
// IoctlCardInfo is nv_ioctl_card_info_t. NV_ESC_CARD_INFO takes an array of
63+
// IoctlCardInfo as parameter.
64+
//
65+
// +marshal
66+
type IoctlCardInfo struct {
67+
Valid uint8
68+
Pad0 [3]byte
69+
PCIInfo PCIInfo
70+
GPUID uint32
71+
InterruptLine uint16
72+
Pad1 [2]byte
73+
RegAddress uint64
74+
RegSize uint64
75+
FBAddress uint64
76+
FBSize uint64
77+
MinorNumber uint32
78+
DevName [10]byte
79+
Pad2 [2]byte
80+
}
81+
82+
// PCIInfo is nv_pci_info_t.
83+
//
84+
// +marshal
85+
type PCIInfo struct {
86+
Domain uint32
87+
Bus uint8
88+
Slot uint8
89+
Function uint8
90+
Pad0 uint8
91+
VendorID uint16
92+
DeviceID uint16
93+
}
94+
6295
// IoctlRegisterFD is the parameter type for NV_ESC_REGISTER_FD.
6396
//
6497
// +marshal

pkg/abi/nvgpu/nvgpu.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ import (
2222

2323
// Device numbers.
2424
const (
25-
NV_MAJOR_DEVICE_NUMBER = 195 // from kernel-open/common/inc/nv.h
26-
NV_CONTROL_DEVICE_MINOR = 255 // from kernel-open/common/inc/nv-linux.h
27-
NVIDIA_UVM_PRIMARY_MINOR_NUMBER = 0 // from kernel-open/nvidia-uvm/uvm_common.h
25+
// From kernel-open/common/inc/nv-chardev-numbers.h:
26+
NV_MAJOR_DEVICE_NUMBER = 195
27+
NV_MINOR_DEVICE_NUMBER_REGULAR_MAX = 247
28+
NV_MINOR_DEVICE_NUMBER_CONTROL_DEVICE = 255
29+
30+
// From kernel-open/nvidia-uvm/uvm_common.h:
31+
NVIDIA_UVM_PRIMARY_MINOR_NUMBER = 0
2832
)
2933

3034
// Handle is NvHandle, from src/common/sdk/nvidia/inc/nvtypes.h.

pkg/fsutil/fsutil.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ type DirentHandler func(ino uint64, off int64, ftype uint8, name string, reclen
2727
// ForEachDirent retrieves all dirents from dirfd using getdents64(2) and
2828
// invokes handleDirent on them.
2929
func ForEachDirent(dirfd int, handleDirent DirentHandler) error {
30+
if _, err := unix.Seek(dirfd, unix.SEEK_SET, 0); err != nil {
31+
return err
32+
}
3033
var direntsBuf [8192]byte
3134
for {
3235
n, err := unix.Getdents(dirfd, direntsBuf[:])

pkg/sentry/devices/nvproxy/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ go_library(
5050
"frontend_unsafe.go",
5151
"handlers.go",
5252
"nvproxy.go",
53+
"nvproxy_impl.go",
5354
"nvproxy_unsafe.go",
5455
"object.go",
5556
"object_free_list.go",

pkg/sentry/devices/nvproxy/frontend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type frontendDevice struct {
4848
}
4949

5050
func (dev *frontendDevice) isCtlDevice() bool {
51-
return dev.minor == nvgpu.NV_CONTROL_DEVICE_MINOR
51+
return dev.minor == nvgpu.NV_MINOR_DEVICE_NUMBER_CONTROL_DEVICE
5252
}
5353

5454
func (dev *frontendDevice) basename() string {

pkg/sentry/devices/nvproxy/nvproxy.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,26 @@ func Register(vfsObj *vfs.VirtualFilesystem, version nvconf.DriverVersion, drive
5656
frontendFDs: make(map[*frontendFD]struct{}),
5757
clients: make(map[nvgpu.Handle]*rootClient),
5858
}
59-
for minor := uint32(0); minor <= nvgpu.NV_CONTROL_DEVICE_MINOR; minor++ {
60-
if err := vfsObj.RegisterDevice(vfs.CharDevice, nvgpu.NV_MAJOR_DEVICE_NUMBER, minor, &frontendDevice{
59+
for minor := uint32(0); minor <= nvgpu.NV_MINOR_DEVICE_NUMBER_REGULAR_MAX; minor++ {
60+
dev := &frontendDevice{
6161
nvp: nvp,
6262
minor: minor,
63-
}, &vfs.RegisterDeviceOptions{
63+
}
64+
nvp.regularDevs[minor] = dev
65+
if err := vfsObj.RegisterDevice(vfs.CharDevice, nvgpu.NV_MAJOR_DEVICE_NUMBER, minor, dev, &vfs.RegisterDeviceOptions{
6466
GroupName: "nvidia-frontend",
6567
}); err != nil {
6668
return err
6769
}
6870
}
71+
if err := vfsObj.RegisterDevice(vfs.CharDevice, nvgpu.NV_MAJOR_DEVICE_NUMBER, nvgpu.NV_MINOR_DEVICE_NUMBER_CONTROL_DEVICE, &frontendDevice{
72+
nvp: nvp,
73+
minor: nvgpu.NV_MINOR_DEVICE_NUMBER_CONTROL_DEVICE,
74+
}, &vfs.RegisterDeviceOptions{
75+
GroupName: "nvidia-frontend",
76+
}); err != nil {
77+
return err
78+
}
6979
if err := vfsObj.RegisterDevice(vfs.CharDevice, uvmDevMajor, nvgpu.NVIDIA_UVM_PRIMARY_MINOR_NUMBER, &uvmDevice{
7080
nvp: nvp,
7181
}, &vfs.RegisterDeviceOptions{
@@ -82,12 +92,15 @@ type nvproxy struct {
8292
version nvconf.DriverVersion
8393
capsEnabled nvconf.DriverCaps
8494
useDevGofer bool
95+
regularDevs [nvgpu.NV_MINOR_DEVICE_NUMBER_REGULAR_MAX + 1]*frontendDevice
8596

8697
fdsMu fdsMutex `state:"nosave"`
8798
frontendFDs map[*frontendFD]struct{}
8899

89100
clientsMu sync.RWMutex `state:"nosave"`
90101
clients map[nvgpu.Handle]*rootClient
102+
103+
nvproxyExtra
91104
}
92105

93106
type marshalPtr[T any] interface {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2025 The gVisor Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build !false
16+
// +build !false
17+
18+
package nvproxy
19+
20+
// +stateify savable
21+
type nvproxyExtra struct {
22+
}

pkg/sentry/devices/nvproxy/version.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ func Init() {
433433
getInfo: func() *DriverABIInfo {
434434
return &DriverABIInfo{
435435
FrontendInfos: map[uint32]IoctlInfo{
436-
nvgpu.NV_ESC_CARD_INFO: simpleIoctlInfo("NV_ESC_CARD_INFO", "nv_ioctl_card_info_t"),
436+
nvgpu.NV_ESC_CARD_INFO: ioctlInfoWithStructName("NV_ESC_CARD_INFO", nvgpu.IoctlCardInfo{}, "nv_ioctl_card_info_t"),
437437
nvgpu.NV_ESC_CHECK_VERSION_STR: ioctlInfoWithStructName("NV_ESC_CHECK_VERSION_STR", nvgpu.RMAPIVersion{}, "nv_ioctl_rm_api_version_t"),
438438
nvgpu.NV_ESC_ATTACH_GPUS_TO_FD: simpleIoctlInfo("NV_ESC_ATTACH_GPUS_TO_FD"), // No params struct, params is a NvU32 array containing GPU IDs
439439
nvgpu.NV_ESC_SYS_PARAMS: ioctlInfoWithStructName("NV_ESC_SYS_PARAMS", nvgpu.IoctlSysParams{}, "nv_ioctl_sys_params_t"),
@@ -494,10 +494,10 @@ func Init() {
494494
nvgpu.NV0000_CTRL_CMD_GPU_GET_DEVICE_IDS: simpleIoctlInfo("NV0000_CTRL_CMD_GPU_GET_DEVICE_IDS", "NV0000_CTRL_GPU_GET_DEVICE_IDS_PARAMS"),
495495
nvgpu.NV0000_CTRL_CMD_GPU_GET_ID_INFO_V2: simpleIoctlInfo("NV0000_CTRL_CMD_GPU_GET_ID_INFO_V2", "NV0000_CTRL_GPU_GET_ID_INFO_V2_PARAMS"),
496496
nvgpu.NV0000_CTRL_CMD_GPU_GET_PROBED_IDS: simpleIoctlInfo("NV0000_CTRL_CMD_GPU_GET_PROBED_IDS", "NV0000_CTRL_GPU_GET_PROBED_IDS_PARAMS"),
497-
nvgpu.NV0000_CTRL_CMD_GPU_ATTACH_IDS: simpleIoctlInfo("NV0000_CTRL_CMD_GPU_ATTACH_IDS", "NV0000_CTRL_GPU_ATTACH_IDS_PARAMS"),
497+
nvgpu.NV0000_CTRL_CMD_GPU_ATTACH_IDS: ioctlInfo("NV0000_CTRL_CMD_GPU_ATTACH_IDS", nvgpu.NV0000_CTRL_GPU_ATTACH_IDS_PARAMS{}),
498498
nvgpu.NV0000_CTRL_CMD_GPU_DETACH_IDS: simpleIoctlInfo("NV0000_CTRL_CMD_GPU_DETACH_IDS", "NV0000_CTRL_GPU_DETACH_IDS_PARAMS"),
499499
nvgpu.NV0000_CTRL_CMD_GPU_GET_PCI_INFO: simpleIoctlInfo("NV0000_CTRL_CMD_GPU_GET_PCI_INFO", "NV0000_CTRL_GPU_GET_PCI_INFO_PARAMS"),
500-
nvgpu.NV0000_CTRL_CMD_GPU_GET_UUID_FROM_GPU_ID: simpleIoctlInfo("NV0000_CTRL_CMD_GPU_GET_UUID_FROM_GPU_ID", "NV0000_CTRL_GPU_GET_UUID_FROM_GPU_ID_PARAMS"),
500+
nvgpu.NV0000_CTRL_CMD_GPU_GET_UUID_FROM_GPU_ID: ioctlInfo("NV0000_CTRL_CMD_GPU_GET_UUID_FROM_GPU_ID", nvgpu.NV0000_CTRL_GPU_GET_UUID_FROM_GPU_ID_PARAMS{}),
501501
nvgpu.NV0000_CTRL_CMD_GPU_QUERY_DRAIN_STATE: simpleIoctlInfo("NV0000_CTRL_CMD_GPU_QUERY_DRAIN_STATE", "NV0000_CTRL_GPU_QUERY_DRAIN_STATE_PARAMS"),
502502
nvgpu.NV0000_CTRL_CMD_GPU_GET_MEMOP_ENABLE: simpleIoctlInfo("NV0000_CTRL_CMD_GPU_GET_MEMOP_ENABLE", "NV0000_CTRL_GPU_GET_MEMOP_ENABLE_PARAMS"),
503503
nvgpu.NV0000_CTRL_CMD_GSYNC_GET_ATTACHED_IDS: simpleIoctlInfo("NV0000_CTRL_CMD_GSYNC_GET_ATTACHED_IDS", "NV0000_CTRL_GSYNC_GET_ATTACHED_IDS_PARAMS"),

pkg/sentry/vfs/device.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ func (vfs *VirtualFilesystem) IsDeviceRegistered(kind DeviceKind, major, minor u
119119
return ok
120120
}
121121

122+
// GetRegisteredDevice returns the device registered for the given (kind,
123+
// major, minor) tuple.
124+
func (vfs *VirtualFilesystem) GetRegisteredDevice(kind DeviceKind, major, minor uint32) Device {
125+
tup := devTuple{kind, major, minor}
126+
vfs.devicesMu.RLock()
127+
defer vfs.devicesMu.RUnlock()
128+
rd, ok := vfs.devices[tup]
129+
if !ok {
130+
return nil
131+
}
132+
return rd.dev
133+
}
134+
122135
// OpenDeviceSpecialFile returns a FileDescription representing the given
123136
// device.
124137
func (vfs *VirtualFilesystem) OpenDeviceSpecialFile(ctx context.Context, mnt *Mount, d *Dentry, kind DeviceKind, major, minor uint32, opts *OpenOptions) (*FileDescription, error) {

0 commit comments

Comments
 (0)