From a45f47fad1068ec4dc795db97d324d0799d7ef54 Mon Sep 17 00:00:00 2001 From: John Falkowski Date: Fri, 1 Aug 2025 06:14:40 +0000 Subject: [PATCH] refactor: Disable Shared System USM if CPU addr range unacceptable Related-To: NEO-15680 Signed-off-by: John Falkowski --- shared/source/os_interface/linux/drm_neo.cpp | 30 +++++++------- shared/source/os_interface/linux/drm_neo.h | 4 +- .../os_interface/linux/xe/ioctl_helper_xe.cpp | 3 +- .../os_interface/linux/drm_tests.cpp | 41 ++++++++++++------- .../linux/xe/ioctl_helper_xe_tests.cpp | 16 -------- 5 files changed, 47 insertions(+), 47 deletions(-) diff --git a/shared/source/os_interface/linux/drm_neo.cpp b/shared/source/os_interface/linux/drm_neo.cpp index c76897bc3a..4800796be0 100644 --- a/shared/source/os_interface/linux/drm_neo.cpp +++ b/shared/source/os_interface/linux/drm_neo.cpp @@ -1684,20 +1684,6 @@ int Drm::unbindBufferObject(OsContext *osContext, uint32_t vmHandleId, BufferObj return changeBufferObjectBinding(this, osContext, vmHandleId, bo, false, false); } -uint64_t Drm::getSharedSystemAllocAddressRange() { - /*Setting GPU address larger than actual may lead to crash */ - if (debugManager.flags.OverrideGpuAddressSpace.get() != -1) { - this->getRootDeviceEnvironment().getMutableHardwareInfo()->capabilityTable.gpuAddressSpace = maxNBitValue(static_cast(debugManager.flags.OverrideGpuAddressSpace.get())); - } - /* Use full address range */ - uint64_t gpuAddressLength = (this->getRootDeviceEnvironment().getMutableHardwareInfo()->capabilityTable.gpuAddressSpace + 1); - uint64_t cpuAddressLength = (0x1ull << (NEO::CpuInfo::getInstance().getVirtualAddressSize())); - if (cpuAddressLength < gpuAddressLength) { - gpuAddressLength = cpuAddressLength; - } - return gpuAddressLength; -} - int Drm::createDrmVirtualMemory(uint32_t &drmVmId) { GemVmControl ctl{}; @@ -1922,7 +1908,21 @@ bool Drm::queryDeviceIdAndRevision() { } void Drm::adjustSharedSystemMemCapabilities() { - if (!this->isSharedSystemAllocEnabled()) { + if (this->isSharedSystemAllocEnabled()) { + uint64_t gpuAddressLength = (this->getRootDeviceEnvironment().getMutableHardwareInfo()->capabilityTable.gpuAddressSpace + 1); + uint64_t cpuAddressLength = (0x1ull << (NEO::CpuInfo::getInstance().getVirtualAddressSize())); + if ((cpuAddressLength > (maxNBitValue(48) + 1)) && !(CpuInfo::getInstance().isCpuFlagPresent("la57"))) { + cpuAddressLength = (maxNBitValue(48) + 1); + } + if (gpuAddressLength < cpuAddressLength) { + this->setSharedSystemAllocEnable(false); + this->getRootDeviceEnvironment().getMutableHardwareInfo()->capabilityTable.sharedSystemMemCapabilities = 0; + printDebugString(debugManager.flags.PrintDebugMessages.get(), stderr, "%s", "Shared System USM NOT allowed: CPU address range > GPU address range\n"); + } else { + this->setSharedSystemAllocAddressRange(cpuAddressLength); + this->setPageFaultSupported(true); + } + } else { this->getRootDeviceEnvironment().getMutableHardwareInfo()->capabilityTable.sharedSystemMemCapabilities = 0; } } diff --git a/shared/source/os_interface/linux/drm_neo.h b/shared/source/os_interface/linux/drm_neo.h index cb15341b68..ad6023af56 100644 --- a/shared/source/os_interface/linux/drm_neo.h +++ b/shared/source/os_interface/linux/drm_neo.h @@ -269,7 +269,8 @@ class Drm : public DriverModel { static std::string getDrmVersion(int fileDescriptor); MOCKABLE_VIRTUAL uint32_t getAggregatedProcessCount() const; uint32_t getVmIdForContext(OsContext &osContext, uint32_t vmHandleId) const; - uint64_t getSharedSystemAllocAddressRange(); + MOCKABLE_VIRTUAL void setSharedSystemAllocAddressRange(uint64_t value) { this->sharedSystemAllocAddressRange = value; } + MOCKABLE_VIRTUAL uint64_t getSharedSystemAllocAddressRange() const { return this->sharedSystemAllocAddressRange; } protected: Drm() = delete; @@ -353,6 +354,7 @@ class Drm : public DriverModel { bool bindAvailable = false; bool directSubmissionActive = false; bool sharedSystemAllocEnable = false; + uint64_t sharedSystemAllocAddressRange = 0lu; bool setPairAvailable = false; bool chunkingAvailable = false; uint32_t chunkingMode = 0; diff --git a/shared/source/os_interface/linux/xe/ioctl_helper_xe.cpp b/shared/source/os_interface/linux/xe/ioctl_helper_xe.cpp index 3e69e77863..2cbe16e149 100644 --- a/shared/source/os_interface/linux/xe/ioctl_helper_xe.cpp +++ b/shared/source/os_interface/linux/xe/ioctl_helper_xe.cpp @@ -163,7 +163,8 @@ bool IoctlHelperXe::queryDeviceIdAndRevision(Drm &drm) { if ((debugManager.flags.EnableRecoverablePageFaults.get() != 0) && (debugManager.flags.EnableSharedSystemUsmSupport.get() == 1) && (config->info[DRM_XE_QUERY_CONFIG_FLAGS] & DRM_XE_QUERY_CONFIG_FLAG_HAS_CPU_ADDR_MIRROR)) { drm.setSharedSystemAllocEnable(true); - drm.setPageFaultSupported(true); + } else { + printDebugString(debugManager.flags.PrintDebugMessages.get(), stderr, "%s", "Shared System USM NOT allowed: KMD does not support\n"); } return true; } diff --git a/shared/test/unit_test/os_interface/linux/drm_tests.cpp b/shared/test/unit_test/os_interface/linux/drm_tests.cpp index 2d420aca91..bf5c892a5c 100644 --- a/shared/test/unit_test/os_interface/linux/drm_tests.cpp +++ b/shared/test/unit_test/os_interface/linux/drm_tests.cpp @@ -16,6 +16,7 @@ #include "shared/source/os_interface/linux/os_context_linux.h" #include "shared/source/os_interface/linux/os_inc.h" #include "shared/source/os_interface/os_interface.h" +#include "shared/source/unified_memory/usm_memory_support.h" #include "shared/source/utilities/cpu_info.h" #include "shared/source/utilities/directory.h" #include "shared/test/common/helpers/debug_manager_state_restore.h" @@ -2556,33 +2557,45 @@ TEST(DrmTest, GivenSysFsPciPathWhenCallinggetSysFsPciPathBaseNameThenResultIsCor EXPECT_STREQ("card8", drm.getSysFsPciPathBaseName().c_str()); } -TEST(DrmTest, GivenGpuAddressRangeAndCpuAddressRangeReturnCorrectRange) { +TEST(DrmTest, GivenSharedSystemAllocNotEnabledByKmdSharedSystemAllocAddressRangeReturnsZero) { auto executionEnvironment = std::make_unique(); DrmMock drm{*executionEnvironment->rootDeviceEnvironments[0]}; - uint64_t gpuAddrRange = executionEnvironment->rootDeviceEnvironments[0]->getMutableHardwareInfo()->capabilityTable.gpuAddressSpace + 1; - uint64_t cpuAddrRange = (0x1ull << (NEO::CpuInfo::getInstance().getVirtualAddressSize())); + executionEnvironment->rootDeviceEnvironments[0]->getMutableHardwareInfo()->capabilityTable.gpuAddressSpace = maxNBitValue(48); + drm.setSharedSystemAllocEnable(false); + drm.adjustSharedSystemMemCapabilities(); + EXPECT_FALSE(drm.isSharedSystemAllocEnabled()); uint64_t sharedSystemRange = drm.getSharedSystemAllocAddressRange(); - EXPECT_LE(sharedSystemRange, gpuAddrRange); - EXPECT_LE(sharedSystemRange, cpuAddrRange); + EXPECT_EQ(0lu, sharedSystemRange); } -TEST(DrmTest, GivenReducedGpuAddressRangeAndCpuAddressRangeReturnReducedRange) { +TEST(DrmTest, GivenGpuAddressRangeAndCpuAddressRangeSharedSystemAllocEnableIsTrue) { auto executionEnvironment = std::make_unique(); DrmMock drm{*executionEnvironment->rootDeviceEnvironments[0]}; - DebugManagerStateRestore restore; - debugManager.flags.OverrideGpuAddressSpace.set(10); + executionEnvironment->rootDeviceEnvironments[0]->getMutableHardwareInfo()->capabilityTable.gpuAddressSpace = maxNBitValue(48); + drm.setSharedSystemAllocEnable(true); + uint64_t caps = (UnifiedSharedMemoryFlags::access | UnifiedSharedMemoryFlags::atomicAccess | UnifiedSharedMemoryFlags::concurrentAccess | UnifiedSharedMemoryFlags::concurrentAtomicAccess); + executionEnvironment->rootDeviceEnvironments[0]->getMutableHardwareInfo()->capabilityTable.sharedSystemMemCapabilities = caps; + drm.adjustSharedSystemMemCapabilities(); + EXPECT_TRUE(drm.isSharedSystemAllocEnabled()); + EXPECT_EQ(caps, executionEnvironment->rootDeviceEnvironments[0]->getMutableHardwareInfo()->capabilityTable.sharedSystemMemCapabilities); + EXPECT_TRUE(drm.hasPageFaultSupport()); uint64_t sharedSystemRange = drm.getSharedSystemAllocAddressRange(); - EXPECT_EQ(sharedSystemRange, maxNBitValue(10) + 1); + EXPECT_EQ((maxNBitValue(48) + 1), sharedSystemRange); } -TEST(DrmTest, GivenIncreasedGpuAddressRangeAndCpuAddressRangeReturnCpuRange) { +TEST(DrmTest, GivenGpuAddressRangeAndCpuAddressRangeSharedSystemAllocEnableIsFalse) { auto executionEnvironment = std::make_unique(); DrmMock drm{*executionEnvironment->rootDeviceEnvironments[0]}; - DebugManagerStateRestore restore; - debugManager.flags.OverrideGpuAddressSpace.set(60); - uint64_t cpuAddrRange = (0x1ull << (NEO::CpuInfo::getInstance().getVirtualAddressSize())); + executionEnvironment->rootDeviceEnvironments[0]->getMutableHardwareInfo()->capabilityTable.gpuAddressSpace = maxNBitValue(32); + drm.setSharedSystemAllocEnable(true); + uint64_t caps = (UnifiedSharedMemoryFlags::access | UnifiedSharedMemoryFlags::atomicAccess | UnifiedSharedMemoryFlags::concurrentAccess | UnifiedSharedMemoryFlags::concurrentAtomicAccess); + executionEnvironment->rootDeviceEnvironments[0]->getMutableHardwareInfo()->capabilityTable.sharedSystemMemCapabilities = caps; + drm.adjustSharedSystemMemCapabilities(); + EXPECT_FALSE(drm.isSharedSystemAllocEnabled()); + EXPECT_EQ(0lu, executionEnvironment->rootDeviceEnvironments[0]->getMutableHardwareInfo()->capabilityTable.sharedSystemMemCapabilities); + EXPECT_FALSE(drm.hasPageFaultSupport()); uint64_t sharedSystemRange = drm.getSharedSystemAllocAddressRange(); - EXPECT_EQ(sharedSystemRange, cpuAddrRange); + EXPECT_EQ(0lu, sharedSystemRange); } using DrmHwTest = ::testing::Test; diff --git a/shared/test/unit_test/os_interface/linux/xe/ioctl_helper_xe_tests.cpp b/shared/test/unit_test/os_interface/linux/xe/ioctl_helper_xe_tests.cpp index 2586f81680..10a961bfad 100644 --- a/shared/test/unit_test/os_interface/linux/xe/ioctl_helper_xe_tests.cpp +++ b/shared/test/unit_test/os_interface/linux/xe/ioctl_helper_xe_tests.cpp @@ -11,7 +11,6 @@ #include "shared/source/os_interface/linux/memory_info.h" #include "shared/source/os_interface/linux/os_context_linux.h" #include "shared/source/os_interface/product_helper.h" -#include "shared/source/unified_memory/usm_memory_support.h" #include "shared/test/common/helpers/debug_manager_state_restore.h" #include "shared/test/common/helpers/engine_descriptor_helper.h" #include "shared/test/common/helpers/stream_capture.h" @@ -2870,11 +2869,6 @@ TEST_F(IoctlHelperXeTest, whenQueryDeviceIdAndRevisionConfigFlagHasGpuAddrMirror EXPECT_TRUE(IoctlHelperXe::queryDeviceIdAndRevision(*drm)); EXPECT_TRUE(drm->isSharedSystemAllocEnabled()); - uint64_t caps = (UnifiedSharedMemoryFlags::access | UnifiedSharedMemoryFlags::atomicAccess | UnifiedSharedMemoryFlags::concurrentAccess | UnifiedSharedMemoryFlags::concurrentAtomicAccess); - drm->getRootDeviceEnvironment().getMutableHardwareInfo()->capabilityTable.sharedSystemMemCapabilities = caps; - drm->adjustSharedSystemMemCapabilities(); - EXPECT_EQ(caps, drm->getRootDeviceEnvironment().getMutableHardwareInfo()->capabilityTable.sharedSystemMemCapabilities); - EXPECT_TRUE(drm->hasPageFaultSupport()); } TEST_F(IoctlHelperXeTest, whenQueryDeviceIdAndRevisionConfigFlagHasGpuAddrMirrorSetButDebugFlagNotSetThenSharedSystemAllocEnableFalse) { @@ -2901,7 +2895,6 @@ TEST_F(IoctlHelperXeTest, whenQueryDeviceIdAndRevisionConfigFlagHasGpuAddrMirror EXPECT_TRUE(IoctlHelperXe::queryDeviceIdAndRevision(*drm)); EXPECT_FALSE(drm->isSharedSystemAllocEnabled()); - EXPECT_FALSE(drm->hasPageFaultSupport()); } TEST_F(IoctlHelperXeTest, whenQueryDeviceIdAndRevisionAndConfigFlagHasGpuAddrMirrorClearThenSharedSystemAllocEnableFalse) { @@ -2927,10 +2920,6 @@ TEST_F(IoctlHelperXeTest, whenQueryDeviceIdAndRevisionAndConfigFlagHasGpuAddrMir EXPECT_TRUE(IoctlHelperXe::queryDeviceIdAndRevision(*drm)); EXPECT_FALSE(drm->isSharedSystemAllocEnabled()); - uint64_t caps = (UnifiedSharedMemoryFlags::access | UnifiedSharedMemoryFlags::atomicAccess | UnifiedSharedMemoryFlags::concurrentAccess | UnifiedSharedMemoryFlags::concurrentAtomicAccess); - drm->getRootDeviceEnvironment().getMutableHardwareInfo()->capabilityTable.sharedSystemMemCapabilities = caps; - drm->adjustSharedSystemMemCapabilities(); - EXPECT_EQ(0lu, drm->getRootDeviceEnvironment().getMutableHardwareInfo()->capabilityTable.sharedSystemMemCapabilities); } TEST_F(IoctlHelperXeTest, whenQueryDeviceIdAndRevisionAndSharedSystemUsmSupportDebugFlagClearThenSharedSystemAllocEnableFalse) { @@ -2957,11 +2946,6 @@ TEST_F(IoctlHelperXeTest, whenQueryDeviceIdAndRevisionAndSharedSystemUsmSupportD EXPECT_TRUE(IoctlHelperXe::queryDeviceIdAndRevision(*drm)); EXPECT_FALSE(drm->isSharedSystemAllocEnabled()); - uint64_t caps = (UnifiedSharedMemoryFlags::access | UnifiedSharedMemoryFlags::atomicAccess | UnifiedSharedMemoryFlags::concurrentAccess | UnifiedSharedMemoryFlags::concurrentAtomicAccess); - drm->getRootDeviceEnvironment().getMutableHardwareInfo()->capabilityTable.sharedSystemMemCapabilities = caps; - drm->adjustSharedSystemMemCapabilities(); - EXPECT_EQ(0lu, drm->getRootDeviceEnvironment().getMutableHardwareInfo()->capabilityTable.sharedSystemMemCapabilities); - EXPECT_FALSE(drm->hasPageFaultSupport()); } TEST_F(IoctlHelperXeTest, givenXeIoctlHelperAndDeferBackingFlagSetToTrueWhenMakeResidentBeforeLockNeededIsCalledThenVerifyTrueIsReturned) {