From 4dd9dbb128191612dcea77885c46e08e275aa87f Mon Sep 17 00:00:00 2001 From: Amber Lin Date: Tue, 1 Dec 2015 14:41:59 -0500 Subject: [PATCH] Free resources when dlclose is called When the Thunk is initialized multiple times in the lifetime of a single process , some global resources are leaked. This can happen when dlopen and dlclose are used to load the library at runtime, rather than linking the runtime against the Thunk. This patch adds the destructor to release global resources when dlclose is called. Change-Id: Ia00da0d41f095d0b2706f98c0e75effedd596f49 [ROCm/ROCR-Runtime commit: 582b70f9c323daf9b2cf89bb5393a7eb3994b4b6] --- projects/rocr-runtime/src/fmm.c | 14 ++++++++++++++ projects/rocr-runtime/src/perfctr.c | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/projects/rocr-runtime/src/fmm.c b/projects/rocr-runtime/src/fmm.c index af4840268d..cf1be8ac45 100644 --- a/projects/rocr-runtime/src/fmm.c +++ b/projects/rocr-runtime/src/fmm.c @@ -139,6 +139,7 @@ static void __fmm_release(void *address, static int _fmm_unmap_from_gpu_scratch(uint32_t gpu_id, manageble_aperture_t *aperture, void *address); +void __attribute__ ((destructor)) fmm_release_global_resources(void); static vm_area_t *vm_create_and_init_area(void *start, void *end) { @@ -1544,3 +1545,16 @@ bool fmm_get_handle(void *address, uint64_t *handle) return found; } + +void fmm_release_global_resources(void) +{ + uint64_t len; + + if (dgpu_shared_aperture_base) { + len = (uint64_t)dgpu_shared_aperture_limit + - (uint64_t)dgpu_shared_aperture_base + 1; + munmap(dgpu_shared_aperture_base, len); + } + dgpu_shared_aperture_base = NULL; + dgpu_shared_aperture_limit = NULL; +} diff --git a/projects/rocr-runtime/src/perfctr.c b/projects/rocr-runtime/src/perfctr.c index 64ab168fda..403ff4ad3e 100644 --- a/projects/rocr-runtime/src/perfctr.c +++ b/projects/rocr-runtime/src/perfctr.c @@ -48,6 +48,8 @@ extern int amd_hsa_thunk_lock_fd; static HsaCounterProperties *counter_props[MAX_NODES] = {NULL}; +void __attribute__ ((destructor)) perfctr_release_global_resources(void); + static int blockid2uuid(enum perf_block_id block_id, HSA_UUID *uuid) { int rc = 0; @@ -368,3 +370,14 @@ hsaKmtPmcStopTrace( return HSAKMT_STATUS_SUCCESS; } + +void perfctr_release_global_resources(void) +{ + int i; + + for (i=0; i