From 11862b9f61db45701885e11aa32e7e7d81f005db Mon Sep 17 00:00:00 2001 From: Felix Kuehling Date: Thu, 30 Mar 2017 11:26:22 -0400 Subject: [PATCH] Add guard page after each address space reservation Guard pages help catch out-of-bounds memory accesses by applications by generating VM faults (GPU) and segfaults (CPU). Remove address space reservation from scratch aperture. That address space is managed by the Thunk client. Guard pages would cause Thunk's address space management to get out of sync with the client's. Change-Id: I2e5aee2923a90186358cc7b0e131baf547996df6 Signed-off-by: Felix Kuehling --- src/fmm.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/fmm.c b/src/fmm.c index 1a10aa82d2..f8bd6f6db3 100644 --- a/src/fmm.c +++ b/src/fmm.c @@ -475,6 +475,16 @@ static bool aperture_is_valid(void *app_base, void *app_limit) return false; } +/* Align size of a VM area + * + * Leave at least one guard page after every object to catch + * out-of-bounds accesses with VM faults. + */ +static uint64_t vm_align_area_size(manageble_aperture_t *app, uint64_t size) +{ + return ALIGN_UP(size + PAGE_SIZE, app->align); +} + /* * Assumes that fmm_mutex is locked on entry. */ @@ -484,7 +494,7 @@ static void aperture_release_area(manageble_aperture_t *app, void *address, vm_area_t *area; uint64_t SizeOfRegion; - MemorySizeInBytes = ALIGN_UP(MemorySizeInBytes, app->align); + MemorySizeInBytes = vm_align_area_size(app, MemorySizeInBytes); area = vm_find(app, address); if (!area) @@ -522,7 +532,7 @@ static void *aperture_allocate_area_aligned(manageble_aperture_t *app, vm_area_t *cur, *next; void *start; - MemorySizeInBytes = ALIGN_UP(MemorySizeInBytes, app->align); + MemorySizeInBytes = vm_align_area_size(app, MemorySizeInBytes); if (align < app->align) align = app->align; @@ -1697,7 +1707,6 @@ static int _fmm_map_to_gpu_scratch(uint32_t gpu_id, manageble_aperture_t *apertu void *address, uint64_t size) { int32_t gpu_mem_id; - uint64_t offset; void *mem = NULL; int ret; bool is_debugger = 0; @@ -1720,20 +1729,11 @@ static int _fmm_map_to_gpu_scratch(uint32_t gpu_id, manageble_aperture_t *apertu ret = debug_get_reg_status(gpu_mem[gpu_mem_id].node_id, &is_debugger); /* allocate object within the scratch backing aperture */ if (!ret && !is_debugger) { - offset = VOID_PTRS_SUB(address, aperture->base); - mem = __fmm_allocate_device(gpu_id, size, aperture, offset, - NULL, KFD_IOC_ALLOC_MEM_FLAGS_DGPU_DEVICE, NULL); - if (mem == NULL) + vm_object_t *obj = fmm_allocate_memory_in_device( + gpu_id, address, size, aperture, + NULL, KFD_IOC_ALLOC_MEM_FLAGS_DGPU_DEVICE); + if (obj == NULL) return -1; - - if (mem != address) { - fprintf(stderr, - "Got unexpected address for scratch mapping.\n" - " expected: %p\n" - " got: %p\n", address, mem); - __fmm_release(mem, aperture); - return -1; - } } else { fmm_allocate_memory_in_device(gpu_id, address,