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 2160645A50; Sat, 28 Sep 2024 18:48:32 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 9232C4027A; Sat, 28 Sep 2024 18:48:30 +0200 (CEST) Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by mails.dpdk.org (Postfix) with ESMTP id A2BB940273 for ; Sat, 28 Sep 2024 18:48:26 +0200 (CEST) Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-7198de684a7so2206381b3a.2 for ; Sat, 28 Sep 2024 09:48:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1727542106; x=1728146906; 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=r85PygVEKMsrCf5pc1qY5kVkFIpvpKhIoRqq+vwpAy0=; b=aK8J188GY6JtqVjjqKClb9ihY9Dtyylqe5qlxYbzumKgraiYL0+ge+ee3XlrmMVVY+ 9rSuM8ZzDvZFXfEVgGBs/tLXjEoCeI9KQY2PLu01ba6hyDakm4kAyFcP7iDtPGEkmvb4 SgrpAyRfcLekHqlKHYO7uQyHVd5bWiqgeKR6oOkIMXqPr0Frl6ZKYovTH+W+PZl4pAE+ 3WCDRJfxg3B510Pb1CIUmQKkfMNNtwNixRhCGzrohG6Bs3YoqaXfkvX4fwQ7HcxuTr/W fUpbB9vj+P3DK5T/jZU6dAANqaLUD05/uWf/94agPC65mmMlCWdKn9k+XDJ7yYEBsRt7 I3BA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727542106; x=1728146906; 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=r85PygVEKMsrCf5pc1qY5kVkFIpvpKhIoRqq+vwpAy0=; b=NOPx2p7QGcBdaLW/OZMkkas/qTgIHnYkSBnNFc31PK+AqOMCSvxJ3rP1MI4YRdWh/b lLpo2dDYldrS8Mipvlvb0CmQOp7dfxQ6BkBBAlgtUVo79fUi1alnaDl9ooEP9ys69eeW A/PEcro/rN/GCY9TkiuVJVJ7Noojsv81FS5wDV28sI6OkZST/YeGPwTfxsAGL4MGvFZ/ g2frHmX8GJofMBtKnceCgdZJwdkhd+UjCavmX5o3v/aWo/3RV3pQt5aFVye+z5Ln6TJz 4/yD3wjsLU/tTfxWvhTnt9htx/sjp62DPzlkQP+DgpUxluiHJXL3phyPgVEtkKpx1Vpj xqIA== X-Gm-Message-State: AOJu0Yz/dPa1UuV6CS8i79PmTCDd0/cNpaoc2BKD8K4XyG+8zXrvjtEM n+s4lQdIv7dlmA8nZb63gmwpmW7XHd7fDXONS3Gs09Qk0OsfQH22ZZMarQE436OF1lDJb1c2zjh k X-Google-Smtp-Source: AGHT+IGbhYTSJrIadaYf611BV7qsZBs5j4SF+feEqSQD977VRWWLcnkCAZ2zSEAbinphDkxdOJKwRw== X-Received: by 2002:a17:902:d510:b0:20b:46cb:8cbe with SMTP id d9443c01a7336-20b46cb8f2bmr87467145ad.29.1727542105707; Sat, 28 Sep 2024 09:48:25 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20b37e47c7dsm28854015ad.216.2024.09.28.09.48.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Sep 2024 09:48:25 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Tyler Retzlaff , Anatoly Burakov Subject: [PATCH v2 01/16] eal: add function attributes for allocation functions Date: Sat, 28 Sep 2024 09:47:09 -0700 Message-ID: <20240928164814.861933-2-stephen@networkplumber.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240928164814.861933-1-stephen@networkplumber.org> References: <20240927204742.546164-1-stephen@networkplumber.org> <20240928164814.861933-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..e73c9f2aef 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(...) \ + __attribute__((malloc, malloc(__VA_ARGS__))) + +#elif defined(RTE_CC_GCC) || defined(RTE_CC_CLANG) +#define __rte_alloc_func(...) \ + __attribute__((malloc)) +#else +#define __rte_alloc_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