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 7BDC345A5D; Sun, 29 Sep 2024 17:43:09 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id BE11040A7A; Sun, 29 Sep 2024 17:41:46 +0200 (CEST) Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by mails.dpdk.org (Postfix) with ESMTP id 52F06402A7 for ; Sun, 29 Sep 2024 17:41:34 +0200 (CEST) Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-71b0722f221so2748504b3a.3 for ; Sun, 29 Sep 2024 08:41:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1727624493; x=1728229293; 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=sKvvLy0/SO9ub+FhtpXDLkgOKjVdKnoU1Jo+zRIanDXqGkFfqIWM4SNl3UeMrh2TU+ LGlaprA9SS1nVAc27PwVFrI5hISg0Fx2Dn2iVWkqJM6d6s2K2mRbl13Pj7G0/ef4xt14 LvYTcDERLXuX8+4bAe6+6jaybOOLsPuL8zBwwNVA4cqKMWrfrUpNL/3AxE2/JBi4R0bu oCSKHxcuVv9tLhq44J6T4vUosZgwXDsNr2U2PzcxHgzYhqir0C9szgkUwXv9WCtzuMCQ aZZTnXBYF2C3aafwVBT3fTq43kdo8Vt9M8j/hCr+nYtiItykwySKhBsZFBHUIW9ypOFU LleA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727624493; x=1728229293; 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=BXy1jTxofYrs44sa6D4oM4CoVTf7rNGmyT9PN4ZnpPo444p2t3zGI2Bzx/WVY3lnVF frvzeQDjPBCGRsRVoX0/+u8G5mhZbNF0yDwNkJJWMPmiESA2DDV3DtVN3SUcZ4/J9JFH fK2pveC4q4bKc9jzAboG6cxvwhGuNfoKQuzPQnrPCzCenvnughChC2OK+NfdJkTfoxJS XfocLeiFPjVpwg0cNM5HZnP6mXCXV0Bdny3oJymt8y6siLDNQj1O5KKbkVZUV18Hvv/e 6rWyuLmtZe9av0BMaFZ/d/1rzmlEr0U7puqEG57g3P8FJNHnsOTr9/XPZLU5jGG4wH7Q 22NQ== X-Gm-Message-State: AOJu0YzBUTFt/Zo/iLo/spRn6AsD/xVeETQ/og+wymbvKiXUNCIPfdPr LwiFHDuHwTeEWHxptXg2uCQIPA43phvjd2moM2U7eqn/45gF8RHmnZJNKFUBPULxZyMLxWgPRmS Vydc= X-Google-Smtp-Source: AGHT+IFf4nmkAuI+2P6Vg0bYmY+g3n9TECM9l7wultkXfufzlusAh2IxnVEBpiE1s8IzH5Lc/HgeYw== X-Received: by 2002:a05:6a21:1583:b0:1d4:e418:c2b6 with SMTP id adf61e73a8af0-1d4fa64d8c6mr14927440637.10.1727624493522; Sun, 29 Sep 2024 08:41:33 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71b2649c775sm4707416b3a.29.2024.09.29.08.41.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Sep 2024 08:41:33 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Tyler Retzlaff , Anatoly Burakov Subject: [PATCH v3 17/18] eal: add function attributes for allocation functions Date: Sun, 29 Sep 2024 08:34:42 -0700 Message-ID: <20240929154107.62539-18-stephen@networkplumber.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240929154107.62539-1-stephen@networkplumber.org> References: <20240927204742.546164-1-stephen@networkplumber.org> <20240929154107.62539-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