From 1afc09bc05a0a7ebe07b9a0c41d71ae2e001cb8a Mon Sep 17 00:00:00 2001 From: mplewka Date: Wed, 18 Jul 2018 10:56:11 +0200 Subject: [PATCH] Fix handling null resource from gtpin Change-Id: I2e6a0992f9c0d672cb42724f2bb1a698b3ba2861 --- runtime/gtpin/gtpin_callbacks.cpp | 10 ++-------- unit_tests/gtpin/gtpin_tests.cpp | 31 +++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/runtime/gtpin/gtpin_callbacks.cpp b/runtime/gtpin/gtpin_callbacks.cpp index d6a6ee38b7..64ad8edb9f 100644 --- a/runtime/gtpin/gtpin_callbacks.cpp +++ b/runtime/gtpin/gtpin_callbacks.cpp @@ -202,13 +202,10 @@ void gtpinNotifyMakeResident(void *pKernel, void *pCSR) { lock.enter(kernelExecQueueLock); size_t numElems = kernelExecQueue.size(); for (size_t n = 0; n < numElems; n++) { - if ((kernelExecQueue[n].pKernel == pKernel) && !kernelExecQueue[n].isResourceResident) { + if ((kernelExecQueue[n].pKernel == pKernel) && !kernelExecQueue[n].isResourceResident && kernelExecQueue[n].gtpinResource) { // It's time for kernel to make resident its GT-Pin resource CommandStreamReceiver *pCommandStreamReceiver = reinterpret_cast(pCSR); cl_mem gtpinBuffer = kernelExecQueue[n].gtpinResource; - if (!gtpinBuffer) { - break; - } auto pBuffer = castToObjectOrAbort(gtpinBuffer); GraphicsAllocation *pGfxAlloc = pBuffer->getGraphicsAllocation(); pCommandStreamReceiver->makeResident(*pGfxAlloc); @@ -226,13 +223,10 @@ void gtpinNotifyUpdateResidencyList(void *pKernel, void *pResVec) { lock.enter(kernelExecQueueLock); size_t numElems = kernelExecQueue.size(); for (size_t n = 0; n < numElems; n++) { - if ((kernelExecQueue[n].pKernel == pKernel) && !kernelExecQueue[n].isResourceResident) { + if ((kernelExecQueue[n].pKernel == pKernel) && !kernelExecQueue[n].isResourceResident && kernelExecQueue[n].gtpinResource) { // It's time for kernel to update its residency list with its GT-Pin resource std::vector *pResidencyVector = (std::vector *)pResVec; cl_mem gtpinBuffer = kernelExecQueue[n].gtpinResource; - if (!gtpinBuffer) { - break; - } auto pBuffer = castToObjectOrAbort(gtpinBuffer); GraphicsAllocation *pGfxAlloc = pBuffer->getGraphicsAllocation(); GeneralSurface *pSurface = new GeneralSurface(pGfxAlloc); diff --git a/unit_tests/gtpin/gtpin_tests.cpp b/unit_tests/gtpin/gtpin_tests.cpp index a2468d6e01..80ff034931 100644 --- a/unit_tests/gtpin/gtpin_tests.cpp +++ b/unit_tests/gtpin/gtpin_tests.cpp @@ -1414,7 +1414,7 @@ TEST_F(GTPinTests, givenInitializedGTPinInterfaceWhenTheSameKerneIsExecutedTwice EXPECT_EQ(CL_SUCCESS, retVal); } -TEST_F(GTPinTests, givenInitializedGTPinInterfaceWhenNullResourceReturnedFromOnKernelSubmitThenResourceNotSetAndNotBecomingResident) { +TEST_F(GTPinTests, givenMultipleKernelSubmissionsWhenOneOfGtpinSurfacesIsNullThenOnlyNonNullSurfacesAreMadeResident) { gtpinCallbacks.onContextCreate = OnContextCreate; gtpinCallbacks.onContextDestroy = OnContextDestroy; gtpinCallbacks.onKernelCreate = OnKernelCreate; @@ -1476,7 +1476,7 @@ TEST_F(GTPinTests, givenInitializedGTPinInterfaceWhenNullResourceReturnedFromOnK auto pCmdQueue = castToObject(cmdQ); gtpinNotifyKernelSubmit(pKernel1, pCmdQueue); - EXPECT_EQ(nullptr, (resource_handle_t)kernelExecQueue[0].gtpinResource); + EXPECT_EQ(nullptr, kernelExecQueue[0].gtpinResource); CommandStreamReceiver &csr = pCmdQueue->getDevice().getCommandStreamReceiver(); gtpinNotifyMakeResident(pKernel1, &csr); @@ -1486,6 +1486,33 @@ TEST_F(GTPinTests, givenInitializedGTPinInterfaceWhenNullResourceReturnedFromOnK gtpinNotifyUpdateResidencyList(pKernel1, &residencyVector); EXPECT_EQ(0u, residencyVector.size()); + returnNullResource = false; + + gtpinNotifyKernelSubmit(pKernel1, pCmdQueue); + EXPECT_NE(nullptr, kernelExecQueue[1].gtpinResource); + gtpinNotifyMakeResident(pKernel1, &csr); + EXPECT_TRUE(kernelExecQueue[1].isResourceResident); + cl_mem gtpinBuffer1 = kernelExecQueue[1].gtpinResource; + + gtpinNotifyKernelSubmit(pKernel1, pCmdQueue); + EXPECT_NE(nullptr, kernelExecQueue[2].gtpinResource); + gtpinNotifyUpdateResidencyList(pKernel1, &residencyVector); + EXPECT_EQ(1u, residencyVector.size()); + EXPECT_TRUE(kernelExecQueue[2].isResourceResident); + EXPECT_FALSE(kernelExecQueue[0].isResourceResident); + + GeneralSurface *pSurf = static_cast(residencyVector[0]); + delete pSurf; + residencyVector.clear(); + + cl_mem gtpinBuffer2 = kernelExecQueue[2].gtpinResource; + + gtpinUnmapBuffer(reinterpret_cast(context), reinterpret_cast(gtpinBuffer1)); + gtpinFreeBuffer(reinterpret_cast(context), reinterpret_cast(gtpinBuffer1)); + + gtpinUnmapBuffer(reinterpret_cast(context), reinterpret_cast(gtpinBuffer2)); + gtpinFreeBuffer(reinterpret_cast(context), reinterpret_cast(gtpinBuffer2)); + retVal = clFinish(cmdQ); EXPECT_EQ(CL_SUCCESS, retVal);