From 13282b911587887a39bbc1bfad7a7eae22b7c82b Mon Sep 17 00:00:00 2001 From: Amber Lin Date: Mon, 28 Aug 2017 16:35:18 -0400 Subject: [PATCH] Fix mmap when the count reaches the max Applications may try to allocate lots host memory and reaches the mmap limit (/proc/sys/vm/max_map_count). When Applications fails to allocate memory and calls hsaKmtFreeMemory to release the memory, Thunk fails to reduce the maps count so the following hsaKmtAllocMemory calls continue to fail, which doesn't make sense to the application. This patch checks the mmap to NORESERVE return value. If it fails and the error number is ENOMEM, reduce the map count by munmap and map it again immediately. Change-Id: I127cb479dfd86b199172eef269d59426f23859ea Signed-off-by: Amber Lin [ROCm/ROCR-Runtime commit: a81b29890c6bbd3e4545fd7f94804688e96714cc] --- projects/rocr-runtime/src/fmm.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/projects/rocr-runtime/src/fmm.c b/projects/rocr-runtime/src/fmm.c index 115137a201..063944b39c 100644 --- a/projects/rocr-runtime/src/fmm.c +++ b/projects/rocr-runtime/src/fmm.c @@ -1266,6 +1266,7 @@ static void __fmm_release(void *address, manageable_aperture_t *aperture) { struct kfd_ioctl_free_memory_of_gpu_args args; vm_object_t *object; + void *mmap_ret; if (!address) return; @@ -1290,8 +1291,19 @@ static void __fmm_release(void *address, manageable_aperture_t *aperture) if (address >= dgpu_shared_aperture_base && address <= dgpu_shared_aperture_limit) { /* Remove any CPU mapping, but keep the address range reserved */ - mmap(address, object->size, PROT_NONE, - MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE | MAP_FIXED, -1, 0); + mmap_ret = mmap(address, object->size, PROT_NONE, + MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE | MAP_FIXED, + -1, 0); + if (mmap_ret == MAP_FAILED && errno == ENOMEM) { + /* When mmap count reaches max_map_count, any mmap will + * fail. Reduce the count with munmap then map it as + * NORESERVE immediately. + */ + munmap(address, object->size); + mmap(address, object->size, PROT_NONE, + MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE | MAP_FIXED, + -1, 0); + } } aperture_release_area(aperture, address, object->size);