Skip to content

Commit 617a184

Browse files
nixprimegvisor-bot
authored andcommitted
nvproxy: implement and use fmt.Stringer.String for handles and class IDs
This makes logging slightly more consistent and less verbose (don't need `h.Val` to avoid extraneous braces in the output), and improves type safety for class IDs. PiperOrigin-RevId: 624316076
1 parent 7ff0b64 commit 617a184

File tree

6 files changed

+43
-17
lines changed

6 files changed

+43
-17
lines changed

pkg/abi/nvgpu/classes.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,24 @@
1414

1515
package nvgpu
1616

17-
// Class handles, from src/nvidia/generated/g_allclasses.h.
17+
import (
18+
"fmt"
19+
)
20+
21+
// ClassID is a client class ID, in the sense of
22+
// src/nvidia/src/kernel/rmapi/resource_desc.h:RS_RESOURCE_DESC::externalClassID.
23+
//
24+
// +marshal
25+
type ClassID uint32
26+
27+
// String implements fmt.Stringer.String.
28+
func (id ClassID) String() string {
29+
// Include leading zeroes for easier searchability, both here and in
30+
// g_allclasses.h.
31+
return fmt.Sprintf("0x%08x", uint32(id))
32+
}
33+
34+
// Class IDs, from src/nvidia/generated/g_allclasses.h.
1835
const (
1936
NV01_ROOT = 0x00000000
2037
NV01_ROOT_NON_PRIV = 0x00000001
@@ -27,9 +44,9 @@ const (
2744
NV_MEMORY_FABRIC = 0x000000f8
2845
NV20_SUBDEVICE_0 = 0x00002080
2946
NV2081_BINAPI = 0x00002081
30-
NV50_MEMORY_VIRTUAL = 0x000050a0
3147
NV50_P2P = 0x0000503b
3248
NV50_THIRD_PARTY_P2P = 0x0000503c
49+
NV50_MEMORY_VIRTUAL = 0x000050a0
3350
GT200_DEBUGGER = 0x000083de
3451
GF100_SUBDEVICE_MASTER = 0x000090e6
3552
FERMI_CONTEXT_SHARE_A = 0x00009067

pkg/abi/nvgpu/frontend.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ type NVOS02Parameters struct {
127127
HRoot Handle
128128
HObjectParent Handle
129129
HObjectNew Handle
130-
HClass uint32
130+
HClass ClassID
131131
Flags uint32
132132
Pad0 [4]byte
133133
PMemory P64 // address of application mapping, without indirection
@@ -177,7 +177,7 @@ type NVOS21Parameters struct {
177177
HRoot Handle
178178
HObjectParent Handle
179179
HObjectNew Handle
180-
HClass uint32
180+
HClass ClassID
181181
PAllocParms P64
182182
ParamsSize uint32
183183
Status uint32
@@ -373,7 +373,7 @@ type NVOS64Parameters struct {
373373
HRoot Handle
374374
HObjectParent Handle
375375
HObjectNew Handle
376-
HClass uint32
376+
HClass ClassID
377377
PAllocParms P64
378378
PRightsRequested P64
379379
ParamsSize uint32

pkg/abi/nvgpu/nvgpu.go

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
// https://github.com/NVIDIA/open-gpu-kernel-modules
1717
package nvgpu
1818

19+
import (
20+
"fmt"
21+
)
22+
1923
// Device numbers.
2024
const (
2125
NV_MAJOR_DEVICE_NUMBER = 195 // from kernel-open/common/inc/nv.h
@@ -30,6 +34,11 @@ type Handle struct {
3034
Val uint32
3135
}
3236

37+
// String implements fmt.Stringer.String.
38+
func (h Handle) String() string {
39+
return fmt.Sprintf("%#x", h.Val)
40+
}
41+
3342
// P64 is NvP64, from src/common/sdk/nvidia/inc/nvtypes.h.
3443
//
3544
// +marshal

pkg/sentry/devices/nvproxy/frontend.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (fd *frontendFD) Ioctl(ctx context.Context, uio usermem.IO, sysno uintptr,
151151
}
152152

153153
if log.IsLogging(log.Debug) {
154-
ctx.Debugf("nvproxy: frontend ioctl: nr = %#08x, argSize = %#08x", nr, argSize)
154+
ctx.Debugf("nvproxy: frontend ioctl: nr = %d = %#x, argSize = %d", nr, nr, argSize)
155155
}
156156

157157
fi := frontendIoctlState{
@@ -329,7 +329,7 @@ func rmAllocMemory(fi *frontendIoctlState) (uintptr, error) {
329329
}
330330

331331
if log.IsLogging(log.Debug) {
332-
fi.ctx.Debugf("nvproxy: NV_ESC_RM_ALLOC_MEMORY class %#08x", ioctlParams.Params.HClass)
332+
fi.ctx.Debugf("nvproxy: NV_ESC_RM_ALLOC_MEMORY class %v", ioctlParams.Params.HClass)
333333
}
334334
// See src/nvidia/arch/nvalloc/unix/src/escape.c:RmIoctl() and
335335
// src/nvidia/interface/deprecated/rmapi_deprecated_allocmemory.c:rmAllocMemoryTable
@@ -338,7 +338,7 @@ func rmAllocMemory(fi *frontendIoctlState) (uintptr, error) {
338338
case nvgpu.NV01_MEMORY_SYSTEM_OS_DESCRIPTOR:
339339
return rmAllocOSDescriptor(fi, &ioctlParams)
340340
default:
341-
fi.ctx.Warningf("nvproxy: unknown NV_ESC_RM_ALLOC_MEMORY class %#08x", ioctlParams.Params.HClass)
341+
fi.ctx.Warningf("nvproxy: unknown NV_ESC_RM_ALLOC_MEMORY class %v", ioctlParams.Params.HClass)
342342
return 0, linuxerr.EINVAL
343343
}
344344
}
@@ -430,7 +430,7 @@ func rmAllocOSDescriptor(fi *frontendIoctlState, ioctlParams *nvgpu.IoctlNVOS02P
430430
fi.fd.nvp.objsLive[sentryIoctlParams.Params.HObjectNew] = &o.object
431431
fi.fd.nvp.objsMu.Unlock()
432432
cu.Release()
433-
fi.ctx.Infof("nvproxy: pinned pages for OS descriptor with handle %#x", sentryIoctlParams.Params.HObjectNew)
433+
fi.ctx.Infof("nvproxy: pinned pages for OS descriptor with handle %v", sentryIoctlParams.Params.HObjectNew)
434434
// Unmap the reserved range, which is no longer required.
435435
unix.RawSyscall(unix.SYS_MUNMAP, m, uintptr(arLen), 0)
436436

@@ -669,7 +669,7 @@ func rmAlloc(fi *frontendIoctlState) (uintptr, error) {
669669

670670
// hClass determines the type of pAllocParms.
671671
if log.IsLogging(log.Debug) {
672-
fi.ctx.Debugf("nvproxy: allocation class %#08x", ioctlParams.HClass)
672+
fi.ctx.Debugf("nvproxy: allocation class %v", ioctlParams.HClass)
673673
}
674674
// Implementors:
675675
// - To map hClass to a symbol, look in
@@ -681,7 +681,7 @@ func rmAlloc(fi *frontendIoctlState) (uintptr, error) {
681681
// - Add handling below.
682682
handler := fi.fd.nvp.abi.allocationClass[ioctlParams.HClass]
683683
if handler == nil {
684-
fi.ctx.Warningf("nvproxy: unknown allocation class %#08x", ioctlParams.HClass)
684+
fi.ctx.Warningf("nvproxy: unknown allocation class %v", ioctlParams.HClass)
685685
// Compare
686686
// src/nvidia/src/kernel/rmapi/alloc_free.c:serverAllocResourceUnderLock(),
687687
// when RsResInfoByExternalClassId() is null.

pkg/sentry/devices/nvproxy/uvm.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ func (fd *uvmFD) Ioctl(ctx context.Context, uio usermem.IO, sysno uintptr, args
131131
panic("Ioctl should be called from a task context")
132132
}
133133

134-
if log.IsLogging(log.Debug) {
135-
ctx.Debugf("nvproxy: uvm ioctl %#08x", cmd)
134+
if ctx.IsLogging(log.Debug) {
135+
ctx.Debugf("nvproxy: uvm ioctl %d = %#x", cmd, cmd)
136136
}
137137

138138
ui := uvmIoctlState{
@@ -144,7 +144,7 @@ func (fd *uvmFD) Ioctl(ctx context.Context, uio usermem.IO, sysno uintptr, args
144144
}
145145
handler := fd.nvp.abi.uvmIoctl[cmd]
146146
if handler == nil {
147-
ctx.Warningf("nvproxy: unknown uvm ioctl %d", cmd)
147+
ctx.Warningf("nvproxy: unknown uvm ioctl %d = %#x", cmd, cmd)
148148
return 0, linuxerr.EINVAL
149149
}
150150
return handler(&ui)

pkg/sentry/devices/nvproxy/version.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ type driverABI struct {
125125
frontendIoctl map[uint32]frontendIoctlHandler
126126
uvmIoctl map[uint32]uvmIoctlHandler
127127
controlCmd map[uint32]controlCmdHandler
128-
allocationClass map[uint32]allocationClassHandler
128+
allocationClass map[nvgpu.ClassID]allocationClassHandler
129129
}
130130

131131
// abis is a global map containing all supported Nvidia driver ABIs. This is
@@ -285,7 +285,7 @@ func Init() {
285285
nvgpu.NV2080_CTRL_CMD_FIFO_DISABLE_CHANNELS: ctrlSubdevFIFODisableChannels,
286286
nvgpu.NV2080_CTRL_CMD_GR_GET_INFO: ctrlSubdevGRGetInfo,
287287
},
288-
allocationClass: map[uint32]allocationClassHandler{
288+
allocationClass: map[nvgpu.ClassID]allocationClassHandler{
289289
nvgpu.NV01_ROOT: rmAllocSimple[nvgpu.Handle],
290290
nvgpu.NV01_ROOT_NON_PRIV: rmAllocSimple[nvgpu.Handle],
291291
nvgpu.NV01_MEMORY_SYSTEM: rmAllocSimple[nvgpu.NV_MEMORY_ALLOCATION_PARAMS],
@@ -421,7 +421,7 @@ func SupportedIoctls(version DriverVersion) (frontendIoctls map[uint32]struct{},
421421
}
422422
allocClasses = make(map[uint32]struct{})
423423
for class := range abi.allocationClass {
424-
allocClasses[class] = struct{}{}
424+
allocClasses[uint32(class)] = struct{}{}
425425
}
426426
return
427427
}

0 commit comments

Comments
 (0)