From 0cb538eae8651842b29a15240e178ea57963d13e Mon Sep 17 00:00:00 2001 From: Philip Yang Date: Tue, 10 Sep 2019 11:49:56 -0400 Subject: [PATCH] libhsakmt: fix mbind failed on docker Docker seccomp by default blocks mbind system call, so mbind return failed on docker. thunk should not fail this otherwise application cannot allocate system memory on docker. Use pr_warn_once and pr_err_once to avoid duplicate same error messages Change-Id: I61a7c0e4abaa3dcfe7abf2ea48db90f669f9638a Signed-off-by: Philip Yang [ROCm/ROCR-Runtime commit: 4da09813a31529c80eedce92dd333b6fe716ffb5] --- projects/rocr-runtime/src/fmm.c | 38 ++++++++++++++++++--------- projects/rocr-runtime/src/libhsakmt.h | 16 +++++++++++ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/projects/rocr-runtime/src/fmm.c b/projects/rocr-runtime/src/fmm.c index ea798d1ae2..e7c83d3fd3 100644 --- a/projects/rocr-runtime/src/fmm.c +++ b/projects/rocr-runtime/src/fmm.c @@ -1455,11 +1455,12 @@ static void *fmm_allocate_host_cpu(void *address, uint64_t MemorySizeInBytes, } static int bind_mem_to_numa(uint32_t node_id, void *mem, - uint64_t MemorySizeInBytes, HsaMemFlags flags) + uint64_t SizeInBytes, HsaMemFlags flags) { int mode = MPOL_F_STATIC_NODES; struct bitmask *node_mask; int num_node; + long r; if (numa_available() == -1) return 0; @@ -1472,22 +1473,33 @@ static int bind_mem_to_numa(uint32_t node_id, void *mem, return 0; } - if (num_node > 1) { - node_mask = numa_bitmask_alloc(num_node); - if (!node_mask) - return -ENOMEM; + if (num_node <= 1) + return 0; - numa_bitmask_setbit(node_mask, node_id); - mode |= flags.ui32.NoSubstitute ? MPOL_BIND : MPOL_PREFERRED; - if (mbind(mem, MemorySizeInBytes, mode, node_mask->maskp, - num_node + 1, 0)) { - pr_warn("Failed to set NUMA policy for %p\n", mem); + node_mask = numa_bitmask_alloc(num_node); + if (!node_mask) + return -ENOMEM; - numa_bitmask_free(node_mask); - return -EFAULT; + numa_bitmask_setbit(node_mask, node_id); + mode |= flags.ui32.NoSubstitute ? MPOL_BIND : MPOL_PREFERRED; + r = mbind(mem, SizeInBytes, mode, node_mask->maskp, num_node + 1, 0); + numa_bitmask_free(node_mask); + + if (r) { + pr_warn_once("Failed to set NUMA policy for %p: %s\n", mem, + strerror(errno)); + + /* If applcation is running inside docker, still return + * ok because docker seccomp blocks mbind by default, + * otherwise application cannot allocate system memory. + */ + if (errno == EPERM) { + pr_err_once("mbind is blocked by seccomp\n"); + + return 0; } - numa_bitmask_free(node_mask); + return -EFAULT; } return 0; diff --git a/projects/rocr-runtime/src/libhsakmt.h b/projects/rocr-runtime/src/libhsakmt.h index 7f2c0670ea..63e3efa0f3 100644 --- a/projects/rocr-runtime/src/libhsakmt.h +++ b/projects/rocr-runtime/src/libhsakmt.h @@ -91,6 +91,22 @@ extern int hsakmt_debug_level; hsakmt_print(HSAKMT_DEBUG_LEVEL_INFO, fmt, ##__VA_ARGS__) #define pr_debug(fmt, ...) \ hsakmt_print(HSAKMT_DEBUG_LEVEL_DEBUG, fmt, ##__VA_ARGS__) +#define pr_err_once(fmt, ...) \ +({ \ + static bool __print_once; \ + if (!__print_once) { \ + __print_once = true; \ + pr_err(fmt, ##__VA_ARGS__); \ + } \ +}) +#define pr_warn_once(fmt, ...) \ +({ \ + static bool __print_once; \ + if (!__print_once) { \ + __print_once = true; \ + pr_warn(fmt, ##__VA_ARGS__); \ + } \ +}) enum asic_family_type { CHIP_KAVERI = 0,