From 026fae434a141faa10da109a2c1b03dc9e06db3f Mon Sep 17 00:00:00 2001 From: Alex Sierra Date: Thu, 9 Jun 2022 11:14:41 -0500 Subject: [PATCH 1/2] libhsakmt: add env var to set max VA alignment order This env variable sets the max VA alignment order size as "PAGE_SIZE * 2^alignment order" during mapping. By default the order size is set to 9(2MB). Signed-off-by: Alex Sierra Change-Id: I01ae4e0963f4d21c7c367464e60f865bc58d7fac (cherry picked from commit f6f13506024470d78c418add42c566521733765f) --- src/fmm.c | 22 +++++++++++++++++++--- src/libhsakmt.h | 1 - 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/fmm.c b/src/fmm.c index 2319513c..35da3b8c 100644 --- a/src/fmm.c +++ b/src/fmm.c @@ -214,6 +214,9 @@ typedef struct { /* whether all memory is coherent (GPU cache disabled) */ bool disable_cache; + + /* specifies the alignment size as PAGE_SIZE * 2^alignment_order */ + uint32_t alignment_order; } svm_t; /* The other apertures are specific to each GPU. gpu_mem_t manages GPU @@ -705,6 +708,7 @@ static void *mmap_aperture_allocate_aligned(manageable_aperture_t *aper, uint64_t size, uint64_t align) { uint64_t aligned_padded_size, guard_size; + uint64_t alignment_size = PAGE_SIZE << svm.alignment_order; void *addr, *aligned_addr, *aligned_end, *mapping_end; if (address) @@ -715,10 +719,14 @@ static void *mmap_aperture_allocate_aligned(manageable_aperture_t *aper, return NULL; } - /* Align big buffers to the next power-of-2 up to 1GB - * size for memory allocation optimization + /* 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 + * PAGE_SIZE * 2^HSA_MAX_VA_ALIGN. Setting HSA_MAX_VA_ALIGN = 18 (1GB), + * improves the time for memory allocation and mapping. But it might lose + * performance when GFX access it, specially for big allocations (>3GB). */ - while (align < GPU_GIANT_PAGE_SIZE && size >= (align << 1)) + while (align < alignment_size && size >= (align << 1)) align <<= 1; /* Add padding to guarantee proper alignment and leave guard @@ -2168,6 +2176,7 @@ HSAKMT_STATUS fmm_init_process_apertures(unsigned int NumNodes) uint32_t num_of_sysfs_nodes; HSAKMT_STATUS ret = HSAKMT_STATUS_SUCCESS; char *disableCache, *pagedUserptr, *checkUserptr, *guardPagesStr, *reserveSvm; + char *maxVaAlignStr; unsigned int guardPages = 1; uint64_t svm_base = 0, svm_limit = 0; uint32_t svm_alignment = 0; @@ -2199,6 +2208,13 @@ HSAKMT_STATUS fmm_init_process_apertures(unsigned int NumNodes) if (!guardPagesStr || sscanf(guardPagesStr, "%u", &guardPages) != 1) guardPages = 1; + /* Sets the max VA alignment order size during mapping. By default the order + * size is set to 9(2MB) + */ + maxVaAlignStr = getenv("HSA_MAX_VA_ALIGN"); + if (!maxVaAlignStr || sscanf(maxVaAlignStr, "%u", &svm.alignment_order) != 1) + svm.alignment_order = 9; + gpu_mem_count = 0; g_first_gpu_mem = NULL; diff --git a/src/libhsakmt.h b/src/libhsakmt.h index e0c42de2..e4246e0b 100644 --- a/src/libhsakmt.h +++ b/src/libhsakmt.h @@ -67,7 +67,6 @@ extern int PAGE_SHIFT; /* 2MB huge page size for 4-level page tables on Vega10 and later GPUs */ #define GPU_HUGE_PAGE_SIZE (2 << 20) -#define GPU_GIANT_PAGE_SIZE (1 << 30) #define CHECK_PAGE_MULTIPLE(x) \ do { if ((uint64_t)PORT_VPTR_TO_UINT64(x) % PAGE_SIZE) return HSAKMT_STATUS_INVALID_PARAMETER; } while(0) From 5a9b486b4bc98acecfc21cdc0e4e18acdac23ddd Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Mon, 27 Feb 2023 15:30:18 +0100 Subject: [PATCH 2/2] Skip getting PAGE_SIZE when defined as macro In some cases, e.g. [when using musl libc][1], PAGE_SIZE is defined as a macro To avoid compilation errors, only get PAGE_SIZE with sysconf when it's not defined [1]: https://elixir.bootlin.com/musl/v1.2.3/source/include/limits.h#L97 --- src/globals.c | 2 ++ src/libhsakmt.h | 2 ++ src/openclose.c | 2 ++ 3 files changed, 6 insertions(+) diff --git a/src/globals.c b/src/globals.c index 3d75b9f1..45ffc90c 100644 --- a/src/globals.c +++ b/src/globals.c @@ -32,5 +32,7 @@ unsigned long kfd_open_count; unsigned long system_properties_count; pthread_mutex_t hsakmt_mutex = PTHREAD_MUTEX_INITIALIZER; bool is_dgpu; +#if !defined(PAGE_SIZE) int PAGE_SIZE; +#endif int PAGE_SHIFT; diff --git a/src/libhsakmt.h b/src/libhsakmt.h index e4246e0b..211b16b9 100644 --- a/src/libhsakmt.h +++ b/src/libhsakmt.h @@ -56,7 +56,9 @@ extern HsaVersionInfo kfd_version_info; do { if ((minor) > kfd_version_info.KernelInterfaceMinorVersion)\ return HSAKMT_STATUS_NOT_SUPPORTED; } while (0) +#if !defined(PAGE_SIZE) extern int PAGE_SIZE; +#endif extern int PAGE_SHIFT; /* VI HW bug requires this virtual address alignment */ diff --git a/src/openclose.c b/src/openclose.c index 19dbf658..b5ceb1f6 100644 --- a/src/openclose.c +++ b/src/openclose.c @@ -108,7 +108,9 @@ static void clear_after_fork(void) static inline void init_page_size(void) { +#if !defined(PAGE_SIZE) PAGE_SIZE = sysconf(_SC_PAGESIZE); +#endif PAGE_SHIFT = ffs(PAGE_SIZE) - 1; }