nvproxy: Refactor UVM code to not copy entire ioctlParams struct.

For consistency with remaining codebase. And also for performance; no need to
copy entire struct when we only want to replace a field.

PiperOrigin-RevId: 634809161
This commit is contained in:
Ayush Ranjan
2024-05-17 10:01:19 -07:00
committed by gVisor bot
parent 94c5177409
commit 4f08fc4814
+18 -26
View File
@@ -184,19 +184,17 @@ func uvmInitialize(ui *uvmIoctlState) (uintptr, error) {
if _, err := ioctlParams.CopyIn(ui.t, ui.ioctlParamsAddr); err != nil {
return 0, err
}
sentryIoctlParams := ioctlParams
origFlags := ioctlParams.Flags
// This is necessary to share the host UVM FD between sentry and
// application processes.
sentryIoctlParams.Flags = ioctlParams.Flags | nvgpu.UVM_INIT_FLAGS_MULTI_PROCESS_SHARING_MODE
n, err := uvmIoctlInvoke(ui, &sentryIoctlParams)
ioctlParams.Flags = ioctlParams.Flags | nvgpu.UVM_INIT_FLAGS_MULTI_PROCESS_SHARING_MODE
n, err := uvmIoctlInvoke(ui, &ioctlParams)
// Only expose the MULTI_PROCESS_SHARING_MODE flag if it was already present.
ioctlParams.Flags &^= ^origFlags & nvgpu.UVM_INIT_FLAGS_MULTI_PROCESS_SHARING_MODE
if err != nil {
return n, err
}
outIoctlParams := sentryIoctlParams
// Only expose the MULTI_PROCESS_SHARING_MODE flag if it was present in
// ioctlParams.
outIoctlParams.Flags &^= ^ioctlParams.Flags & nvgpu.UVM_INIT_FLAGS_MULTI_PROCESS_SHARING_MODE
if _, err := outIoctlParams.CopyOut(ui.t, ui.ioctlParamsAddr); err != nil {
if _, err := ioctlParams.CopyOut(ui.t, ui.ioctlParamsAddr); err != nil {
return n, err
}
return n, nil
@@ -225,16 +223,14 @@ func uvmMMInitialize(ui *uvmIoctlState) (uintptr, error) {
return 0, failWithStatus(nvgpu.NV_ERR_INVALID_ARGUMENT)
}
sentryIoctlParams := ioctlParams
sentryIoctlParams.UvmFD = uvmFile.hostFD
n, err := uvmIoctlInvoke(ui, &sentryIoctlParams)
origFD := ioctlParams.UvmFD
ioctlParams.UvmFD = uvmFile.hostFD
n, err := uvmIoctlInvoke(ui, &ioctlParams)
ioctlParams.UvmFD = origFD
if err != nil {
return n, err
}
outIoctlParams := sentryIoctlParams
outIoctlParams.UvmFD = ioctlParams.UvmFD
if _, err := outIoctlParams.CopyOut(ui.t, ui.ioctlParamsAddr); err != nil {
if _, err := ioctlParams.CopyOut(ui.t, ui.ioctlParamsAddr); err != nil {
return n, err
}
return n, nil
@@ -246,8 +242,8 @@ func uvmIoctlHasFrontendFD[Params any, PtrParams hasFrontendFDPtr[Params]](ui *u
return 0, err
}
rmCtrlFD := (PtrParams)(&ioctlParams).GetFrontendFD()
if rmCtrlFD < 0 {
origFD := (PtrParams)(&ioctlParams).GetFrontendFD()
if origFD < 0 {
n, err := uvmIoctlInvoke(ui, &ioctlParams)
if err != nil {
return n, err
@@ -258,7 +254,7 @@ func uvmIoctlHasFrontendFD[Params any, PtrParams hasFrontendFDPtr[Params]](ui *u
return n, nil
}
ctlFileGeneric, _ := ui.t.FDTable().Get(rmCtrlFD)
ctlFileGeneric, _ := ui.t.FDTable().Get(origFD)
if ctlFileGeneric == nil {
return 0, linuxerr.EINVAL
}
@@ -268,18 +264,14 @@ func uvmIoctlHasFrontendFD[Params any, PtrParams hasFrontendFDPtr[Params]](ui *u
return 0, linuxerr.EINVAL
}
sentryIoctlParams := ioctlParams
(PtrParams)(&sentryIoctlParams).SetFrontendFD(ctlFile.hostFD)
n, err := uvmIoctlInvoke(ui, &sentryIoctlParams)
(PtrParams)(&ioctlParams).SetFrontendFD(ctlFile.hostFD)
n, err := uvmIoctlInvoke(ui, &ioctlParams)
(PtrParams)(&ioctlParams).SetFrontendFD(origFD)
if err != nil {
return n, err
}
outIoctlParams := sentryIoctlParams
(PtrParams)(&outIoctlParams).SetFrontendFD(rmCtrlFD)
if _, err := (PtrParams)(&outIoctlParams).CopyOut(ui.t, ui.ioctlParamsAddr); err != nil {
if _, err := (PtrParams)(&ioctlParams).CopyOut(ui.t, ui.ioctlParamsAddr); err != nil {
return n, err
}
return n, nil
}