rocr: Improve memory protection and WSL compatibility (#2274)

* rocr: Add ProtectMemory API and use it in RemoveAccess
Replace munmap + mmap with mprotect when removing memory access.
This improves performance by 5-10x, ensures atomicity (no race
condition window), and prepares for WSL/DXG compatibility fixes.

Suggested-by: David Yat Sin <David.YatSin@amd.com>
Signed-off-by: Flora Cui <flora.cui@amd.com>
Signed-off-by: Horatio Zhang <Hongkun.Zhang@amd.com>

* rocr: Skip CPU mapping operations on WSL
On WSL, CPU cannot access GPU VRAM due to platform restrictions.
CPU access would fault-in system RAM instead, causing data corruption
and memory leaks. Return HSA_STATUS_ERROR to fail fast rather than
silently creating broken mappings. GPU-to-GPU mappings remain functional.

Signed-off-by: Flora Cui <flora.cui@amd.com>
Signed-off-by: Horatio Zhang <Hongkun.Zhang@amd.com>

* rocr: reduce ifdef linux
v2: Fix IsDXG check logic

Signed-off-by: David Yat Sin <David.YatSin@amd.com>
Signed-off-by: Horatio Zhang <Hongkun.Zhang@amd.com>

---------
Signed-off-by: Horatio Zhang <Hongkun.Zhang@amd.com>
Signed-off-by: David Yat Sin <David.YatSin@amd.com>
Signed-off-by: Flora Cui <flora.cui@amd.com>
This commit is contained in:
hongkzha-amd
2026-01-14 02:08:20 +08:00
committed by GitHub
parent 2e8c863341
commit b3c4e94e70
4 changed files with 31 additions and 14 deletions
@@ -930,6 +930,10 @@ bool UncommitMemory(void* addr, size_t size) {
0) != MAP_FAILED;
}
bool ProtectMemory(void* va, size_t size, MemProt perms) {
return ::mprotect(va, size, MemProtToOsProt(perms)) == 0;
}
uint64_t HostTotalPhysicalMemory() {
static uint64_t totalPhys = 0;
@@ -355,6 +355,8 @@ bool UncommitMemory(void* addr, size_t size);
bool UnmapMemory(void* addr, size_t size);
bool MapMemory(void* addr, size_t size, MemProt prot, int fd, uint64_t cpu_addr);
bool ProtectMemory(void* va, size_t size, MemProt perms);
uint64_t HostTotalPhysicalMemory();
/// Find First Set for any OS
@@ -470,6 +470,14 @@ bool MapMemory(void* addr, size_t size, MemProt perms, int fd [[maybe_unused]],
return VirtualProtect(addr, size, memProtToOsProt(perms), &OldProtect) != 0;
}
bool ProtectMemory(void* va, size_t size, MemProt perms) {
if (perms == MEM_PROT_NONE) {
return UncommitMemory(addr, size);
}
DWORD oldProt;
return VirtualProtect(va, size, memProtToOsProt(perms), &oldProt) != 0;
}
int Ffs(int i) {
int res = 0;
unsigned long index;