Skip to content

Commit

Permalink
nvproxy: implement and use fmt.Stringer.String for handles and class IDs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nixprime authored and gvisor-bot committed Apr 12, 2024
1 parent 7ff0b64 commit 617a184
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
21 changes: 19 additions & 2 deletions pkg/abi/nvgpu/classes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/abi/nvgpu/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -177,7 +177,7 @@ type NVOS21Parameters struct {
HRoot Handle
HObjectParent Handle
HObjectNew Handle
HClass uint32
HClass ClassID
PAllocParms P64
ParamsSize uint32
Status uint32
Expand Down Expand Up @@ -373,7 +373,7 @@ type NVOS64Parameters struct {
HRoot Handle
HObjectParent Handle
HObjectNew Handle
HClass uint32
HClass ClassID
PAllocParms P64
PRightsRequested P64
ParamsSize uint32
Expand Down
9 changes: 9 additions & 0 deletions pkg/abi/nvgpu/nvgpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions pkg/sentry/devices/nvproxy/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions pkg/sentry/devices/nvproxy/uvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pkg/sentry/devices/nvproxy/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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
}

0 comments on commit 617a184

Please sign in to comment.