diff --git a/src/fmm.c b/src/fmm.c index 4e8293fbcf..f552c21daf 100644 --- a/src/fmm.c +++ b/src/fmm.c @@ -785,14 +785,38 @@ static void *mmap_aperture_allocate_aligned(manageable_aperture_t *aper, uint64_t alignment_size = PAGE_SIZE << svm.alignment_order; uint64_t guard_size; - if (address) - return NULL; - if (!aper->is_cpu_accessible) { pr_err("MMap Aperture must be CPU accessible\n"); return NULL; } + if (address) { + void *addr; + +#ifdef MAP_FIXED_NOREPLACE + addr = mmap(address, size, PROT_NONE, + MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE | MAP_FIXED_NOREPLACE, + -1, 0); +#else + addr = mmap(address, size, PROT_NONE, + MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE, + -1, 0); +#endif + if (addr == MAP_FAILED) { + pr_err("mmap failed: %s\n", strerror(errno)); + return NULL; + } + +#ifndef MAP_FIXED_NOREPLACE + if (address != addr) { + pr_err("mmap failed to return addr asked\n"); + munmap(addr, size); + return NULL; + } +#endif + return addr; + } + /* Align big buffers to the next power-of-2. By default, the max alignment * size is set to 2MB. This can be modified by the env variable * HSA_MAX_VA_ALIGN. This variable sets the order of the alignment size as