From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id D47B6A04B5; Wed, 28 Oct 2020 00:28:06 +0100 (CET) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 633B92BFA; Wed, 28 Oct 2020 00:24:34 +0100 (CET) Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129]) by dpdk.org (Postfix) with ESMTP id 83ED72C55 for ; Wed, 28 Oct 2020 00:24:00 +0100 (CET) Received: from Internal Mail-Server by MTLPINE1 (envelope-from ophirmu@nvidia.com) with SMTP; 28 Oct 2020 01:23:54 +0200 Received: from nvidia.com (pegasus05.mtr.labs.mlnx [10.210.16.100]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id 09RNNrrR026642; Wed, 28 Oct 2020 01:23:54 +0200 From: Ophir Munk To: dev@dpdk.org, Raslan Darawsheh Cc: Ophir Munk , Matan Azrad , Tal Shnaiderman , Thomas Monjalon Date: Tue, 27 Oct 2020 23:22:38 +0000 Message-Id: <20201027232335.31427-16-ophirmu@nvidia.com> X-Mailer: git-send-email 2.8.4 In-Reply-To: <20201027232335.31427-1-ophirmu@nvidia.com> References: <20201027232335.31427-1-ophirmu@nvidia.com> Subject: [dpdk-dev] [PATCH v1 15/72] common/mlx5/linux: handle memory allocations with alignment X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" mlx5_malloc() API has an alignment parameter for system memory allocations. malloc() is called for non-aligned allocations and posix_memalign() is called for aligned allocations. When calling mlx5_free() there is no distinction whether the memory was originally allocated with or without alignment. Freeing a memory may be handled differently by operating systems. Therefore this commit wraps these APIs with OS specific calls: mlx5_os_malloc(), mlx5_os_free(). Signed-off-by: Ophir Munk Acked-by: Matan Azrad --- drivers/common/mlx5/linux/mlx5_common_os.h | 38 ++++++++++++++++++++++++++++++ drivers/common/mlx5/mlx5_malloc.c | 14 +++++------ 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/drivers/common/mlx5/linux/mlx5_common_os.h b/drivers/common/mlx5/linux/mlx5_common_os.h index f8b215c..bd44ecb 100644 --- a/drivers/common/mlx5/linux/mlx5_common_os.h +++ b/drivers/common/mlx5/linux/mlx5_common_os.h @@ -6,6 +6,7 @@ #define RTE_PMD_MLX5_COMMON_OS_H_ #include +#include #include #include @@ -16,6 +17,7 @@ #include "mlx5_autoconf.h" #include "mlx5_glue.h" +#include "mlx5_malloc.h" /** * Get device name. Given an ibv_device pointer - return a @@ -224,4 +226,40 @@ mlx5_os_umem_dereg(void *pumem) { return mlx5_glue->devx_umem_dereg(pumem); } + +/** + * Memory allocation optionally with alignment. + * + * @param[in] align + * Alignment size (may be zero) + * @param[in] size + * Size in bytes to allocate + * + * @return + * Valid pointer to allocated memory, NULL in case of failure + */ +static inline void * +mlx5_os_malloc(size_t align, size_t size) +{ + void *buf; + + if (posix_memalign(&buf, align, size)) + return NULL; + return buf; +} + +/** + * This API de-allocates a memory that originally could have been + * allocated aligned or non-aligned. In Linux it is a wrapper + * around free(). + * + * @param[in] addr + * Pointer to address to free + * + */ +static inline void +mlx5_os_free(void *addr) +{ + free(addr); +} #endif /* RTE_PMD_MLX5_COMMON_OS_H_ */ diff --git a/drivers/common/mlx5/mlx5_malloc.c b/drivers/common/mlx5/mlx5_malloc.c index 4489971..dd8b793 100644 --- a/drivers/common/mlx5/mlx5_malloc.c +++ b/drivers/common/mlx5/mlx5_malloc.c @@ -11,6 +11,7 @@ #include #include "mlx5_common_utils.h" +#include "mlx5_common_os.h" #include "mlx5_malloc.h" struct mlx5_sys_mem { @@ -151,14 +152,11 @@ static void * mlx5_alloc_align(size_t size, unsigned int align, unsigned int zero) { void *buf; - int ret; - - ret = posix_memalign(&buf, align, size); - if (ret) { - DRV_LOG(ERR, - "Couldn't allocate buf size=%zu align=%u. Err=%d\n", - size, align, ret); + buf = mlx5_os_malloc(align, size); + if (!buf) { + DRV_LOG(ERR, "Couldn't allocate buf size=%zu align=%u.", + size, align); return NULL; } if (zero) @@ -262,7 +260,7 @@ mlx5_free(void *addr) #ifdef RTE_LIBRTE_MLX5_DEBUG rte_atomic64_inc(&mlx5_sys_mem.free_sys); #endif - free(addr); + mlx5_os_free(addr); } else { #ifdef RTE_LIBRTE_MLX5_DEBUG rte_atomic64_inc(&mlx5_sys_mem.free_rte); -- 2.8.4