From patchwork Wed Sep 9 08:43:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ophir Munk X-Patchwork-Id: 77016 X-Patchwork-Delegate: rasland@nvidia.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 40BC3A04B1; Wed, 9 Sep 2020 10:43:45 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 0742B1C0B4; Wed, 9 Sep 2020 10:43:44 +0200 (CEST) Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129]) by dpdk.org (Postfix) with ESMTP id 331081BF8D for ; Wed, 9 Sep 2020 10:43:42 +0200 (CEST) Received: from Internal Mail-Server by MTLPINE1 (envelope-from ophirmu@nvidia.com) with SMTP; 9 Sep 2020 11:43:37 +0300 Received: from nvidia.com (pegasus05.mtr.labs.mlnx [10.210.16.100]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id 0898hboH009909; Wed, 9 Sep 2020 11:43:37 +0300 From: Ophir Munk To: dev@dpdk.org Cc: Raslan Darawsheh , Ophir Munk , Matan Azrad , stable@dpdk.org Date: Wed, 9 Sep 2020 08:43:17 +0000 Message-Id: <20200909084317.21569-1-ophirmu@nvidia.com> X-Mailer: git-send-email 2.8.4 Subject: [dpdk-dev] [PATCH v1] common/mlx5: fix mlx5 aligned malloc 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" Before this commit system call memalign was used for aligned allocations, however memalign is deprecated. Based on (1) - POSIX requires that memory aligned allocations can be freed using free. Some systems provide no way to reclaim memory allocated with memalign (because one can only pass to free a pointer gotten from malloc, while, memalign would call malloc and then align the obtained value). Another issue is that 64/32 bits architectures use a minimal alignment size. So any requested alignment below the minimal system size can be simplified by calling malloc. The glibc implementation allows memory obtained from posix_memalign to be reclaimed with free. This commit replaces system call memalign with system call posix_memalign. It also calls malloc in case the requested alignment is below the minimal system size. (1) https://linux.die.net/man/3/memalign Fixes: d38e3d526657 ("common/mlx5: add memory management functions") Cc: stable@dpdk.org Signed-off-by: Ophir Munk Acked-by: Matan Azrad --- drivers/common/mlx5/mlx5_malloc.c | 13 +++++++++---- drivers/common/mlx5/mlx5_malloc.h | 8 ++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/drivers/common/mlx5/mlx5_malloc.c b/drivers/common/mlx5/mlx5_malloc.c index 316305d..4489971 100644 --- a/drivers/common/mlx5/mlx5_malloc.c +++ b/drivers/common/mlx5/mlx5_malloc.c @@ -151,9 +151,14 @@ static void * mlx5_alloc_align(size_t size, unsigned int align, unsigned int zero) { void *buf; - buf = memalign(align, size); - if (!buf) { - DRV_LOG(ERR, "Couldn't allocate buf.\n"); + 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); + return NULL; } if (zero) @@ -190,7 +195,7 @@ mlx5_malloc(uint32_t flags, size_t size, unsigned int align, int socket) return addr; } /* The memory will be allocated from system. */ - if (align) + if (align > MLX5_MALLOC_ALIGNMENT) addr = mlx5_alloc_align(size, align, !!(flags & MLX5_MEM_ZERO)); else if (flags & MLX5_MEM_ZERO) addr = calloc(1, size); diff --git a/drivers/common/mlx5/mlx5_malloc.h b/drivers/common/mlx5/mlx5_malloc.h index d3e5f5b..8aea414 100644 --- a/drivers/common/mlx5/mlx5_malloc.h +++ b/drivers/common/mlx5/mlx5_malloc.h @@ -9,6 +9,14 @@ extern "C" { #endif +#ifndef MLX5_MALLOC_ALIGNMENT +#ifndef RTE_ARCH_64 +#define MLX5_MALLOC_ALIGNMENT 8 +#else +#define MLX5_MALLOC_ALIGNMENT 16 +#endif +#endif + enum mlx5_mem_flags { MLX5_MEM_ANY = 0, /* Memory will be allocated dpends on sys_mem_en. */