From bc9b11d754c6acacf0a5e512ce5c1f3a2375daad Mon Sep 17 00:00:00 2001 From: lyndonli Date: Wed, 11 Sep 2024 10:34:55 +0800 Subject: [PATCH] wsl/hsakmt: Remove pre-allocation free memory check This change removes the check for sufficient free memory before allocation. The previous check could cause performance degradation. Reserving a portion of system memory helps prevent system hangs due to insufficient memory. However, if free memory is still insufficient, memory allocation may still lead to system hangs. Signed-off-by: lyndonli Reviewed-by: Flora Cui Part-of: --- globals.cpp | 4 +++- libhsakmt.h | 1 + memory.cpp | 19 +++---------------- topology.cpp | 5 +++++ 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/globals.cpp b/globals.cpp index bb635fef4b..4f866ac4fc 100644 --- a/globals.cpp +++ b/globals.cpp @@ -44,4 +44,6 @@ bool is_svm_api_supported; /* zfb is mainly used during emulation */ int zfb_support; /* handle vendor specific packet */ -int vendor_packet_support; \ No newline at end of file +int vendor_packet_support; + +size_t max_single_alloc_size = 0; \ No newline at end of file diff --git a/libhsakmt.h b/libhsakmt.h index 96b59a462c..4ac14a304a 100644 --- a/libhsakmt.h +++ b/libhsakmt.h @@ -43,6 +43,7 @@ extern bool is_dgpu; extern bool is_svm_api_supported; extern int zfb_support; extern int vendor_packet_support; +extern size_t max_single_alloc_size; #undef HSAKMTAPI #define HSAKMTAPI __attribute__((visibility ("default"))) diff --git a/memory.cpp b/memory.cpp index acfc3b6d2f..9d4c1679d8 100644 --- a/memory.cpp +++ b/memory.cpp @@ -105,18 +105,6 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtAllocMemory(HSAuint32 PreferredNode, #define POWER_OF_2(x) ((x && (!(x & (x - 1)))) ? 1 : 0) -bool isSystemMemoryAvailable(HSAuint64 SizeInBytes) { - struct sysinfo info; - if (sysinfo(&info) != 0) - return false; - return SizeInBytes <= info.freeram; -} - -bool isLocalMemoryAvailable(wsl::thunk::WDDMDevice *dev, - HSAuint64 SizeInBytes) { - return SizeInBytes <= dev->VramAvail(); -} - HSAKMT_STATUS HSAKMTAPI hsaKmtAllocMemoryAlign(HSAuint32 PreferredNode, HSAuint64 SizeInBytes, HSAuint64 Alignment, @@ -149,17 +137,16 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtAllocMemoryAlign(HSAuint32 PreferredNode, create_info.va_hint = reinterpret_cast(*MemoryAddress); if ((PreferredNode == 0 && !MemFlags.ui32.NonPaged) || zfb_support || MemFlags.ui32.GTTAccess) { + if (SizeInBytes > max_single_alloc_size) + return HSAKMT_STATUS_NO_MEMORY; + /* If allocate VRAM under ZFB mode */ if (zfb_support && MemFlags.ui32.NonPaged == 1) MemFlags.ui32.CoarseGrain = 1; create_info.domain = rocr_proxy::AllocDomain::kSystem; - if (!MemFlags.ui32.OnlyAddress && !isSystemMemoryAvailable(SizeInBytes)) - return HSAKMT_STATUS_NO_MEMORY; } else { create_info.domain = rocr_proxy::AllocDomain::kLocal; - if (!MemFlags.ui32.OnlyAddress && !isLocalMemoryAvailable(dev, SizeInBytes)) - return HSAKMT_STATUS_NO_MEMORY; } if (!MemFlags.ui32.CoarseGrain) diff --git a/topology.cpp b/topology.cpp index 0ac6d48c32..2c8be2c02b 100644 --- a/topology.cpp +++ b/topology.cpp @@ -750,6 +750,11 @@ static HSAKMT_STATUS topology_sysfs_get_mem_props(uint32_t node_id, sysinfo(&info); props->SizeInBytes = info.totalram; + /* props->SizeInBytes is the actual physical system + * memory size. Reserve 1/16th for WSL system usage. + */ + max_single_alloc_size = info.totalram - (info.totalram >> 4); + props->Flags.MemoryProperty = 0; props->Width = 64; props->MemoryClockMax = 2133;