From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 719BC45A48; Fri, 27 Sep 2024 22:48:06 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id D5885402D5; Fri, 27 Sep 2024 22:48:00 +0200 (CEST) Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) by mails.dpdk.org (Postfix) with ESMTP id 21DA440144 for ; Fri, 27 Sep 2024 22:47:58 +0200 (CEST) Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-7db238d07b3so2076433a12.2 for ; Fri, 27 Sep 2024 13:47:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1727470077; x=1728074877; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=jDZJ2YCTlkRNkG14wtS36FucV0ugbgEwsBEU7Afy55M=; b=pgnQMKbNVzH6lk3QdIlLCg4iv8DUaZMuy6i0a0sGy+gXZp2jj8GpuJadYd4hlCUdzo Fkxpkp+IbKrZ7MxvhMpM5lLmcr/jsr8gqh2d/0mLwl90eBFo4qQtwJw+5GT1LccNUHph 8LVdlpmrgOOn41oHAZqxQBy/hUP404zuopyZuCq2nySN4PwZ6yFEMRA/ZWvtFtHogY7o 8+IJba+n1GPsxWQgh0akig4cRUWaSUEY6M8dN+u9TMOiNQLUbMhOicDAlO1OxxneJhiw ah0o30clUg4lce0wgySTMYY62rj5l0E7aFBVV7A49Beu4IDkJ8J4JXWYckaTuu8npJay M/lQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727470077; x=1728074877; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jDZJ2YCTlkRNkG14wtS36FucV0ugbgEwsBEU7Afy55M=; b=AcnFFe1rg+wTmSrgweSJJXZatnrXidzyGaszjRgvr4VnC8L6gsIkRRlTA2j5Os3/ph Cl13GyuaMYNnEbbBw6G4wLk0j8LskNdx50MS3qx3zji9XtsVqLftSlnxF2x/aw1uQuM6 koC4lIWSHxZVdDeA0xDn+3bD4zMFPA1ApuMqRT+CWPUzrQ87TdTwjcNiraFJJZvtmUzy d3UJXPnloHnbhteh2zZ7/GqDilsDbIHbJg0YlH6x07Pic7QLBHxL7wu6A9zr4MoiXmnf rDVYFvXSWOakpq1w7moWUu1NAKeNpUc8qAJFDDx/ac5Wy87yOikqu67o11Ep/a0K1cTB c/nw== X-Gm-Message-State: AOJu0YyKtCM8q1XpKhb67a5Pc5jdGiXffg74XZHiA4cgn09eCtJLtbOd /hiYT+7JhhSOfb3beyDg3u9/DpMtdO5KLKOk3D7EG7o4DUzQWBgxjg/JzwqujMsVdI1WqwjM4vC g X-Google-Smtp-Source: AGHT+IFqY2PbyyHM/0VZrjRD9tvYMP6rpLM+j+S0eR625gxqu1kR9GGyCgAEMw9U0dhaqHnEpm11Nw== X-Received: by 2002:a05:6a21:1192:b0:1cf:2a35:6d21 with SMTP id adf61e73a8af0-1d4fa7b53cdmr6064634637.45.1727470077233; Fri, 27 Sep 2024 13:47:57 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7e6db294533sm2036531a12.12.2024.09.27.13.47.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 13:47:56 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Tyler Retzlaff , Anatoly Burakov Subject: [PATCH 01/16] eal: add function attributes for allocation functions Date: Fri, 27 Sep 2024 13:45:37 -0700 Message-ID: <20240927204742.546164-2-stephen@networkplumber.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240927204742.546164-1-stephen@networkplumber.org> References: <20240927204742.546164-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org The allocation functions take a alignment argument that can be useful to hint the compiler optimizer. This is supported by Gcc and Clang but only useful with Gcc because Clang gives warning if alignment is 0. Recent versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with rte_malloc() that is then incorrectly freed using free(). Signed-off-by: Stephen Hemminger --- lib/eal/include/rte_common.h | 30 ++++++++++++++++++++++++++++++ lib/eal/include/rte_malloc.h | 24 ++++++++++++++++-------- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h index eec0400dad..1b3781274d 100644 --- a/lib/eal/include/rte_common.h +++ b/lib/eal/include/rte_common.h @@ -228,6 +228,36 @@ typedef uint16_t unaligned_uint16_t; #define __rte_alloc_size(...) #endif +/** + * Tells the compiler that the function returns a value that points to + * memory aligned by a function argument. + * Not enabled on clang because it warns if align argument is zero. + */ +#if defined(RTE_CC_GCC) +#define __rte_alloc_align(align_arg) \ + __attribute__((alloc_align(align_arg))) +#else +#define __rte_alloc_align(...) +#endif + +/** + * Tells the compiler this is a function like malloc and that the pointer + * returned cannot alias any other pointer (ie new memory). + * + * Also, with recent GCC versions also able to track that proper + * dealloctor function is used for this pointer. + */ +#if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 110000) +#define __rte_alloc_func(free_func) \ + __attribute__((malloc, malloc(free_func))) + +#elif defined(RTE_CC_GCC) || defined(RTE_CC_CLANG) +#define __rte_alloc_func(free_func) \ + __attribute__((malloc)) +#else +#define __rte_alloc_func(free_func) +#endif + #define RTE_PRIORITY_LOG 101 #define RTE_PRIORITY_BUS 110 #define RTE_PRIORITY_CLASS 120 diff --git a/lib/eal/include/rte_malloc.h b/lib/eal/include/rte_malloc.h index 1f91e7bdde..cf3c174022 100644 --- a/lib/eal/include/rte_malloc.h +++ b/lib/eal/include/rte_malloc.h @@ -54,7 +54,8 @@ struct rte_malloc_socket_stats { */ void * rte_malloc(const char *type, size_t size, unsigned align) - __rte_alloc_size(2); + __rte_alloc_size(2) + __rte_alloc_align(3); /** * Allocate zeroed memory from the heap. @@ -81,7 +82,8 @@ rte_malloc(const char *type, size_t size, unsigned align) */ void * rte_zmalloc(const char *type, size_t size, unsigned align) - __rte_alloc_size(2); + __rte_alloc_size(2) + __rte_alloc_align(3); /** * Replacement function for calloc(), using huge-page memory. Memory area is @@ -108,7 +110,8 @@ rte_zmalloc(const char *type, size_t size, unsigned align) */ void * rte_calloc(const char *type, size_t num, size_t size, unsigned align) - __rte_alloc_size(2, 3); + __rte_alloc_size(2, 3) + __rte_alloc_align(4); /** * Replacement function for realloc(), using huge-page memory. Reserved area @@ -132,7 +135,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align) */ void * rte_realloc(void *ptr, size_t size, unsigned int align) - __rte_alloc_size(2); + __rte_alloc_size(2) + __rte_alloc_align(3); /** * Replacement function for realloc(), using huge-page memory. Reserved area @@ -158,7 +162,8 @@ rte_realloc(void *ptr, size_t size, unsigned int align) */ void * rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) - __rte_alloc_size(2); + __rte_alloc_size(2) + __rte_alloc_align(3); /** * This function allocates memory from the huge-page area of memory. The memory @@ -185,7 +190,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) */ void * rte_malloc_socket(const char *type, size_t size, unsigned align, int socket) - __rte_alloc_size(2); + __rte_alloc_size(2) + __rte_alloc_align(3); /** * Allocate zeroed memory from the heap. @@ -214,7 +220,8 @@ rte_malloc_socket(const char *type, size_t size, unsigned align, int socket) */ void * rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket) - __rte_alloc_size(2); + __rte_alloc_size(2) + __rte_alloc_align(3); /** * Replacement function for calloc(), using huge-page memory. Memory area is @@ -243,7 +250,8 @@ rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket) */ void * rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket) - __rte_alloc_size(2, 3); + __rte_alloc_size(2, 3) + __rte_alloc_align(4); /** * Frees the memory space pointed to by the provided pointer. -- 2.45.2