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
This commit is contained in:
Jamie Liu
2024-04-12 15:57:19 -07:00
committed by gVisor bot
parent 7ff0b64d6e
commit 617a184cf2
6 changed files with 43 additions and 17 deletions
+19 -2
View File
@@ -14,7 +14,24 @@
package nvgpu
// Class handles, from src/nvidia/generated/g_allclasses.h.
import (
"fmt"
)
// ClassID is a client class ID, in the sense of
// src/nvidia/src/kernel/rmapi/resource_desc.h:RS_RESOURCE_DESC::externalClassID.
//
// +marshal
type ClassID uint32
// String implements fmt.Stringer.String.
func (id ClassID) String() string {
// Include leading zeroes for easier searchability, both here and in
// g_allclasses.h.
return fmt.Sprintf("0x%08x", uint32(id))
}
// Class IDs, from src/nvidia/generated/g_allclasses.h.
const (
NV01_ROOT = 0x00000000
NV01_ROOT_NON_PRIV = 0x00000001
@@ -27,9 +44,9 @@ const (
NV_MEMORY_FABRIC = 0x000000f8
NV20_SUBDEVICE_0 = 0x00002080
NV2081_BINAPI = 0x00002081
NV50_MEMORY_VIRTUAL = 0x000050a0
NV50_P2P = 0x0000503b
NV50_THIRD_PARTY_P2P = 0x0000503c
NV50_MEMORY_VIRTUAL = 0x000050a0
GT200_DEBUGGER = 0x000083de
GF100_SUBDEVICE_MASTER = 0x000090e6
FERMI_CONTEXT_SHARE_A = 0x00009067
+3 -3
View File
@@ -127,7 +127,7 @@ type NVOS02Parameters struct {
HRoot Handle
HObjectParent Handle
HObjectNew Handle
HClass uint32
HClass ClassID
Flags uint32
Pad0 [4]byte
PMemory P64 // address of application mapping, without indirection
@@ -177,7 +177,7 @@ type NVOS21Parameters struct {
HRoot Handle
HObjectParent Handle
HObjectNew Handle
HClass uint32
HClass ClassID
PAllocParms P64
ParamsSize uint32
Status uint32
@@ -373,7 +373,7 @@ type NVOS64Parameters struct {
HRoot Handle
HObjectParent Handle
HObjectNew Handle
HClass uint32
HClass ClassID
PAllocParms P64
PRightsRequested P64
ParamsSize uint32
+9
View File
@@ -16,6 +16,10 @@
// https://github.com/NVIDIA/open-gpu-kernel-modules
package nvgpu
import (
"fmt"
)
// Device numbers.
const (
NV_MAJOR_DEVICE_NUMBER = 195 // from kernel-open/common/inc/nv.h
@@ -30,6 +34,11 @@ type Handle struct {
Val uint32
}
// String implements fmt.Stringer.String.
func (h Handle) String() string {
return fmt.Sprintf("%#x", h.Val)
}
// P64 is NvP64, from src/common/sdk/nvidia/inc/nvtypes.h.
//
// +marshal
+6 -6
View File
@@ -151,7 +151,7 @@ func (fd *frontendFD) Ioctl(ctx context.Context, uio usermem.IO, sysno uintptr,
}
if log.IsLogging(log.Debug) {
ctx.Debugf("nvproxy: frontend ioctl: nr = %#08x, argSize = %#08x", nr, argSize)
ctx.Debugf("nvproxy: frontend ioctl: nr = %d = %#x, argSize = %d", nr, nr, argSize)
}
fi := frontendIoctlState{
@@ -329,7 +329,7 @@ func rmAllocMemory(fi *frontendIoctlState) (uintptr, error) {
}
if log.IsLogging(log.Debug) {
fi.ctx.Debugf("nvproxy: NV_ESC_RM_ALLOC_MEMORY class %#08x", ioctlParams.Params.HClass)
fi.ctx.Debugf("nvproxy: NV_ESC_RM_ALLOC_MEMORY class %v", ioctlParams.Params.HClass)
}
// See src/nvidia/arch/nvalloc/unix/src/escape.c:RmIoctl() and
// src/nvidia/interface/deprecated/rmapi_deprecated_allocmemory.c:rmAllocMemoryTable
@@ -338,7 +338,7 @@ func rmAllocMemory(fi *frontendIoctlState) (uintptr, error) {
case nvgpu.NV01_MEMORY_SYSTEM_OS_DESCRIPTOR:
return rmAllocOSDescriptor(fi, &ioctlParams)
default:
fi.ctx.Warningf("nvproxy: unknown NV_ESC_RM_ALLOC_MEMORY class %#08x", ioctlParams.Params.HClass)
fi.ctx.Warningf("nvproxy: unknown NV_ESC_RM_ALLOC_MEMORY class %v", ioctlParams.Params.HClass)
return 0, linuxerr.EINVAL
}
}
@@ -430,7 +430,7 @@ func rmAllocOSDescriptor(fi *frontendIoctlState, ioctlParams *nvgpu.IoctlNVOS02P
fi.fd.nvp.objsLive[sentryIoctlParams.Params.HObjectNew] = &o.object
fi.fd.nvp.objsMu.Unlock()
cu.Release()
fi.ctx.Infof("nvproxy: pinned pages for OS descriptor with handle %#x", sentryIoctlParams.Params.HObjectNew)
fi.ctx.Infof("nvproxy: pinned pages for OS descriptor with handle %v", sentryIoctlParams.Params.HObjectNew)
// Unmap the reserved range, which is no longer required.
unix.RawSyscall(unix.SYS_MUNMAP, m, uintptr(arLen), 0)
@@ -669,7 +669,7 @@ func rmAlloc(fi *frontendIoctlState) (uintptr, error) {
// hClass determines the type of pAllocParms.
if log.IsLogging(log.Debug) {
fi.ctx.Debugf("nvproxy: allocation class %#08x", ioctlParams.HClass)
fi.ctx.Debugf("nvproxy: allocation class %v", ioctlParams.HClass)
}
// Implementors:
// - To map hClass to a symbol, look in
@@ -681,7 +681,7 @@ func rmAlloc(fi *frontendIoctlState) (uintptr, error) {
// - Add handling below.
handler := fi.fd.nvp.abi.allocationClass[ioctlParams.HClass]
if handler == nil {
fi.ctx.Warningf("nvproxy: unknown allocation class %#08x", ioctlParams.HClass)
fi.ctx.Warningf("nvproxy: unknown allocation class %v", ioctlParams.HClass)
// Compare
// src/nvidia/src/kernel/rmapi/alloc_free.c:serverAllocResourceUnderLock(),
// when RsResInfoByExternalClassId() is null.
+3 -3
View File
@@ -131,8 +131,8 @@ func (fd *uvmFD) Ioctl(ctx context.Context, uio usermem.IO, sysno uintptr, args
panic("Ioctl should be called from a task context")
}
if log.IsLogging(log.Debug) {
ctx.Debugf("nvproxy: uvm ioctl %#08x", cmd)
if ctx.IsLogging(log.Debug) {
ctx.Debugf("nvproxy: uvm ioctl %d = %#x", cmd, cmd)
}
ui := uvmIoctlState{
@@ -144,7 +144,7 @@ func (fd *uvmFD) Ioctl(ctx context.Context, uio usermem.IO, sysno uintptr, args
}
handler := fd.nvp.abi.uvmIoctl[cmd]
if handler == nil {
ctx.Warningf("nvproxy: unknown uvm ioctl %d", cmd)
ctx.Warningf("nvproxy: unknown uvm ioctl %d = %#x", cmd, cmd)
return 0, linuxerr.EINVAL
}
return handler(&ui)
+3 -3
View File
@@ -125,7 +125,7 @@ type driverABI struct {
frontendIoctl map[uint32]frontendIoctlHandler
uvmIoctl map[uint32]uvmIoctlHandler
controlCmd map[uint32]controlCmdHandler
allocationClass map[uint32]allocationClassHandler
allocationClass map[nvgpu.ClassID]allocationClassHandler
}
// abis is a global map containing all supported Nvidia driver ABIs. This is
@@ -285,7 +285,7 @@ func Init() {
nvgpu.NV2080_CTRL_CMD_FIFO_DISABLE_CHANNELS: ctrlSubdevFIFODisableChannels,
nvgpu.NV2080_CTRL_CMD_GR_GET_INFO: ctrlSubdevGRGetInfo,
},
allocationClass: map[uint32]allocationClassHandler{
allocationClass: map[nvgpu.ClassID]allocationClassHandler{
nvgpu.NV01_ROOT: rmAllocSimple[nvgpu.Handle],
nvgpu.NV01_ROOT_NON_PRIV: rmAllocSimple[nvgpu.Handle],
nvgpu.NV01_MEMORY_SYSTEM: rmAllocSimple[nvgpu.NV_MEMORY_ALLOCATION_PARAMS],
@@ -421,7 +421,7 @@ func SupportedIoctls(version DriverVersion) (frontendIoctls map[uint32]struct{},
}
allocClasses = make(map[uint32]struct{})
for class := range abi.allocationClass {
allocClasses[class] = struct{}{}
allocClasses[uint32(class)] = struct{}{}
}
return
}