From patchwork Sat Sep 24 07:49:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: fengchengwen X-Patchwork-Id: 116808 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 0C3CDA0542; Sat, 24 Sep 2022 09:57:02 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id D242042C18; Sat, 24 Sep 2022 09:56:20 +0200 (CEST) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by mails.dpdk.org (Postfix) with ESMTP id 9FC7742BD0 for ; Sat, 24 Sep 2022 09:56:11 +0200 (CEST) Received: from dggpeml500024.china.huawei.com (unknown [172.30.72.54]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MZLqJ5PxBzpV4Q; Sat, 24 Sep 2022 15:53:16 +0800 (CST) Received: from localhost.localdomain (10.67.165.24) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 24 Sep 2022 15:56:08 +0800 From: Chengwen Feng To: CC: , , , Subject: [PATCH v3 01/10] memarea: introduce memarea library Date: Sat, 24 Sep 2022 07:49:42 +0000 Message-ID: <20220924074951.31814-2-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220924074951.31814-1-fengchengwen@huawei.com> References: <20220721044648.6817-1-fengchengwen@huawei.com> <20220924074951.31814-1-fengchengwen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.165.24] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500024.china.huawei.com (7.185.36.10) X-CFilter-Loop: Reflected 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 memarea library is an allocator of variable-size object which based on a memory region. This patch provides create/destroy API. Signed-off-by: Chengwen Feng --- MAINTAINERS | 5 + doc/api/doxy-api-index.md | 3 +- doc/api/doxy-api.conf.in | 1 + doc/guides/prog_guide/index.rst | 1 + doc/guides/prog_guide/memarea_lib.rst | 39 ++++++ doc/guides/rel_notes/release_22_11.rst | 6 + lib/eal/common/eal_common_log.c | 1 + lib/eal/include/rte_log.h | 1 + lib/memarea/memarea_private.h | 30 +++++ lib/memarea/meson.build | 16 +++ lib/memarea/rte_memarea.c | 157 +++++++++++++++++++++++++ lib/memarea/rte_memarea.h | 145 +++++++++++++++++++++++ lib/memarea/version.map | 12 ++ lib/meson.build | 1 + 14 files changed, 417 insertions(+), 1 deletion(-) create mode 100644 doc/guides/prog_guide/memarea_lib.rst create mode 100644 lib/memarea/memarea_private.h create mode 100644 lib/memarea/meson.build create mode 100644 lib/memarea/rte_memarea.c create mode 100644 lib/memarea/rte_memarea.h create mode 100644 lib/memarea/version.map diff --git a/MAINTAINERS b/MAINTAINERS index 26d3a7077c..4d5bf986c6 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1558,6 +1558,11 @@ F: app/test/test_lpm* F: app/test/test_func_reentrancy.c F: app/test/test_xmmt_ops.h +Memarea - EXPERIMENTAL +M: Chengwen Feng +F: lib/memarea +F: doc/guides/prog_guide/memarea_lib.rst + Membership - EXPERIMENTAL M: Yipeng Wang M: Sameh Gobriel diff --git a/doc/api/doxy-api-index.md b/doc/api/doxy-api-index.md index 186a258be4..25dbef0b68 100644 --- a/doc/api/doxy-api-index.md +++ b/doc/api/doxy-api-index.md @@ -64,7 +64,8 @@ The public API headers are grouped by topics: [memzone](@ref rte_memzone.h), [mempool](@ref rte_mempool.h), [malloc](@ref rte_malloc.h), - [memcpy](@ref rte_memcpy.h) + [memcpy](@ref rte_memcpy.h), + [memarea](@ref rte_memarea.h) - **timers**: [cycles](@ref rte_cycles.h), diff --git a/doc/api/doxy-api.conf.in b/doc/api/doxy-api.conf.in index 608494a7c0..bdc5d2b0d5 100644 --- a/doc/api/doxy-api.conf.in +++ b/doc/api/doxy-api.conf.in @@ -55,6 +55,7 @@ INPUT = @TOPDIR@/doc/api/doxy-api-index.md \ @TOPDIR@/lib/latencystats \ @TOPDIR@/lib/lpm \ @TOPDIR@/lib/mbuf \ + @TOPDIR@/lib/memarea \ @TOPDIR@/lib/member \ @TOPDIR@/lib/mempool \ @TOPDIR@/lib/meter \ diff --git a/doc/guides/prog_guide/index.rst b/doc/guides/prog_guide/index.rst index 8564883018..e9015d65e3 100644 --- a/doc/guides/prog_guide/index.rst +++ b/doc/guides/prog_guide/index.rst @@ -37,6 +37,7 @@ Programmer's Guide hash_lib toeplitz_hash_lib efd_lib + memarea_lib member_lib lpm_lib lpm6_lib diff --git a/doc/guides/prog_guide/memarea_lib.rst b/doc/guides/prog_guide/memarea_lib.rst new file mode 100644 index 0000000000..b96dad15f6 --- /dev/null +++ b/doc/guides/prog_guide/memarea_lib.rst @@ -0,0 +1,39 @@ +.. SPDX-License-Identifier: BSD-3-Clause + Copyright(c) 2022 HiSilicon Limited + +Memarea Library +=============== + +Introduction +------------ + +The memarea library provides an allocator of variable-size objects, it is +oriented towards the application layer, which could provides 'region-based +memory management' function [1]. + +The main features are as follows: + +* The default aligement size is ``RTE_CACHE_LINE_SIZE``. + +* The memory region can be initialized from the following memory sources: + a) RTE memory: e.g. invoke ``rte_malloc_socket`` to obtain. b) System API: + e.g. invoke posix_memalign to obtain. c) User provided address: it can be from + extendedd memory as long as it is available. d) User provided memarea: it can + be from another memarea. + +* It provides refcnt feature which could be useful in multi-reader scenario. + +* It supports MT-safe as long as it's specified at creation time. + +Library API Overview +-------------------- + +The ``rte_memarea_create()`` function is used to create a memarea, the function +returns the pointer to the created memarea or ``NULL`` if the creation failed. + +The ``rte_memarea_destroy()`` function is used to destroy a memarea. + +Reference +--------- + +[1] https://en.wikipedia.org/wiki/Region-based_memory_management diff --git a/doc/guides/rel_notes/release_22_11.rst b/doc/guides/rel_notes/release_22_11.rst index 43502349bd..1625424141 100644 --- a/doc/guides/rel_notes/release_22_11.rst +++ b/doc/guides/rel_notes/release_22_11.rst @@ -55,6 +55,12 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **Added memarea library.** + + The memarea library is an allocator of variable-size objects, it is oriented + towards the application layer, which could provides 'region-based memory + management' function. + Removed Items ------------- diff --git a/lib/eal/common/eal_common_log.c b/lib/eal/common/eal_common_log.c index bd7b188ceb..3d62af59c6 100644 --- a/lib/eal/common/eal_common_log.c +++ b/lib/eal/common/eal_common_log.c @@ -369,6 +369,7 @@ static const struct logtype logtype_strings[] = { {RTE_LOGTYPE_EFD, "lib.efd"}, {RTE_LOGTYPE_EVENTDEV, "lib.eventdev"}, {RTE_LOGTYPE_GSO, "lib.gso"}, + {RTE_LOGTYPE_MEMAREA, "lib.memarea"}, {RTE_LOGTYPE_USER1, "user1"}, {RTE_LOGTYPE_USER2, "user2"}, {RTE_LOGTYPE_USER3, "user3"}, diff --git a/lib/eal/include/rte_log.h b/lib/eal/include/rte_log.h index 25ce42cdfc..708f3a39dd 100644 --- a/lib/eal/include/rte_log.h +++ b/lib/eal/include/rte_log.h @@ -48,6 +48,7 @@ extern "C" { #define RTE_LOGTYPE_EFD 18 /**< Log related to EFD. */ #define RTE_LOGTYPE_EVENTDEV 19 /**< Log related to eventdev. */ #define RTE_LOGTYPE_GSO 20 /**< Log related to GSO. */ +#define RTE_LOGTYPE_MEMAREA 21 /**< Log related to memarea. */ /* these log types can be used in an application */ #define RTE_LOGTYPE_USER1 24 /**< User-defined log type 1. */ diff --git a/lib/memarea/memarea_private.h b/lib/memarea/memarea_private.h new file mode 100644 index 0000000000..c76392d3e6 --- /dev/null +++ b/lib/memarea/memarea_private.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2022 HiSilicon Limited + */ + +#ifndef MEMAREA_PRIVATE_H +#define MEMAREA_PRIVATE_H + +#include + +#define MEMAREA_FREE_ELEM_COOKIE 0xFFFFFFFF + +struct memarea_elem { + size_t size; + uint32_t cookie; + int32_t refcnt; /* Non-zero indicates that it has been allocated */ + TAILQ_ENTRY(memarea_elem) elem_node; + TAILQ_ENTRY(memarea_elem) free_node; +} __rte_cache_aligned; + +TAILQ_HEAD(memarea_elem_list, memarea_elem); + +struct rte_memarea { + struct rte_memarea_param init; + rte_spinlock_t lock; + void *area_addr; + struct memarea_elem_list elem_list; + struct memarea_elem_list free_list; +} __rte_cache_aligned; + +#endif /* MEMAREA_PRIVATE_H */ diff --git a/lib/memarea/meson.build b/lib/memarea/meson.build new file mode 100644 index 0000000000..0a74fb4cd1 --- /dev/null +++ b/lib/memarea/meson.build @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2022 HiSilicon Limited + +if is_windows + build = false + reason = 'not supported on Windows' + subdir_done() +endif + +sources = files( + 'rte_memarea.c', +) +headers = files( + 'rte_memarea.h', +) +deps += [] diff --git a/lib/memarea/rte_memarea.c b/lib/memarea/rte_memarea.c new file mode 100644 index 0000000000..868da7661d --- /dev/null +++ b/lib/memarea/rte_memarea.c @@ -0,0 +1,157 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2022 HiSilicon Limited + */ + +#include +#include + +#include +#include +#include +#include + +#include "rte_memarea.h" +#include "memarea_private.h" + +static int +memarea_check_param(const struct rte_memarea_param *init) +{ + size_t len; + + if (init == NULL) { + RTE_LOG(ERR, MEMAREA, "memarea init param is NULL!\n"); + return -EINVAL; + } + + len = strnlen(init->name, RTE_MEMAREA_NAMESIZE); + if (len == 0 || len >= RTE_MEMAREA_NAMESIZE) { + RTE_LOG(ERR, MEMAREA, "memarea name size %zu invalid!\n", len); + return -EINVAL; + } + + if (init->source != RTE_MEMAREA_SOURCE_RTE_MEMORY && + init->source != RTE_MEMAREA_SOURCE_SYSTEM_API && + init->source != RTE_MEMAREA_SOURCE_USER_ADDR && + init->source != RTE_MEMAREA_SOURCE_USER_MEMAREA) { + RTE_LOG(ERR, MEMAREA, "memarea: %s source: %d not supported!\n", + init->name, init->source); + return -EINVAL; + } + + if (init->total_sz <= sizeof(struct memarea_elem)) { + RTE_LOG(ERR, MEMAREA, "memarea: %s total-size: %zu too small!\n", + init->name, init->total_sz); + return -EINVAL; + } + + if (init->source == RTE_MEMAREA_SOURCE_USER_ADDR && init->user_addr == NULL) { + RTE_LOG(ERR, MEMAREA, "memarea: %s user provided addr is NULL!\n", init->name); + return -EINVAL; + } + + if (init->source == RTE_MEMAREA_SOURCE_USER_ADDR && + ((uintptr_t)init->user_addr & (RTE_CACHE_LINE_SIZE - 1))) { + RTE_LOG(ERR, MEMAREA, "memarea: %s user provided addr should align: %d!\n", + init->name, RTE_CACHE_LINE_SIZE); + return -EINVAL; + } + + if (init->source == RTE_MEMAREA_SOURCE_USER_MEMAREA && init->user_memarea == NULL) { + RTE_LOG(ERR, MEMAREA, "memarea: %s user provided memarea is NULL!\n", init->name); + return -EINVAL; + } + + if (init->alg != RTE_MEMAREA_ALG_DEFAULT) { + RTE_LOG(ERR, MEMAREA, "memarea: %s alg: %d not supported!\n", + init->name, init->alg); + return -EINVAL; + } + + return 0; +} + +static void * +memarea_alloc_from_system_api(size_t size) +{ + void *ptr = NULL; + int ret; + + ret = posix_memalign(&ptr, RTE_CACHE_LINE_SIZE, size); + if (ret) + return NULL; + return ptr; +} + +static void * +memarea_alloc_area(const struct rte_memarea_param *init) +{ + void *ptr = NULL; + + if (init->source == RTE_MEMAREA_SOURCE_RTE_MEMORY) + ptr = rte_malloc_socket(NULL, init->total_sz, RTE_CACHE_LINE_SIZE, + init->numa_socket); + else if (init->source == RTE_MEMAREA_SOURCE_SYSTEM_API) + ptr = memarea_alloc_from_system_api(init->total_sz); + else if (init->source == RTE_MEMAREA_SOURCE_USER_ADDR) + ptr = init->user_addr; + + if (ptr == NULL) + RTE_LOG(ERR, MEMAREA, "memarea: %s alloc memory area fail!\n", init->name); + + return ptr; +} + +struct rte_memarea * +rte_memarea_create(const struct rte_memarea_param *init) +{ + struct memarea_elem *elem; + struct rte_memarea *ma; + void *addr; + int ret; + + ret = memarea_check_param(init); + if (ret) + return NULL; + + addr = memarea_alloc_area(init); + if (addr == NULL) + return NULL; + + ma = rte_zmalloc(NULL, sizeof(struct rte_memarea), RTE_CACHE_LINE_SIZE); + if (ma == NULL) { + RTE_LOG(ERR, MEMAREA, "malloc memarea: %s management obj fail!\n", init->name); + return NULL; + } + + ma->init = *init; + rte_spinlock_init(&ma->lock); + TAILQ_INIT(&ma->elem_list); + TAILQ_INIT(&ma->free_list); + ma->area_addr = addr; + elem = addr; + elem->size = init->total_sz - sizeof(struct memarea_elem); + elem->cookie = MEMAREA_FREE_ELEM_COOKIE; + elem->refcnt = 0; + TAILQ_INSERT_TAIL(&ma->elem_list, elem, elem_node); + TAILQ_INSERT_TAIL(&ma->free_list, elem, free_node); + + return ma; +} + +static void +memarea_free_area(struct rte_memarea *ma) +{ + if (ma->init.source == RTE_MEMAREA_SOURCE_RTE_MEMORY) + rte_free(ma->area_addr); + else if (ma->init.source == RTE_MEMAREA_SOURCE_SYSTEM_API) + free(ma->area_addr); +} + +void +rte_memarea_destroy(struct rte_memarea *ma) +{ + if (ma == NULL) + return; + memarea_free_area(ma); + rte_free(ma); +} diff --git a/lib/memarea/rte_memarea.h b/lib/memarea/rte_memarea.h new file mode 100644 index 0000000000..543cda4cac --- /dev/null +++ b/lib/memarea/rte_memarea.h @@ -0,0 +1,145 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2022 HiSilicon Limited + */ + +#ifndef RTE_MEMAREA_H +#define RTE_MEMAREA_H + +/** + * @file + * RTE Memarea. + * + * The memarea is an allocator of variable-size object which based on a memory + * region. It has the following features: + * + * - The default alignment size is RTE_CACHE_LINE_SIZE. + * - The memory region can be initialized from the following memory sources: + * 1. RTE memory: e.g. invoke rte_malloc_socket to obtain. + * 2. System API: e.g. invoke posix_memalign to obtain. + * 3. User provided address: it can be from extended memory as long as it is + * available. The address must be aligned to RTE_CACHE_LINE_SIZE. + * 4) User provided memarea: it can be from another memarea. So we can build + * the following memory management structure: + * \code{.unparsed} + * ------------- + * | memarea-1 | + * ------------- + * | + * v + * ------------------------------- + * | | | + * v v v + * ------------- ------------- ---------- + * | memarea-2 | | memarea-3 | | object | + * ------------- ------------- ---------- + * \endcode + * As shown above, the memarea-2/3 both create from memarea-1's memory. + * - It provides refcnt feature which could be useful in multi-reader scenario. + * - It supports MT-safe as long as it's specified at creation time. If not + * specified, all the functions of the memarea API are lock-free, and assume + * to not be invoked in parallel on different logical cores to work on the + * same memarea. + */ + +#include +#include +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define RTE_MEMAREA_NAMESIZE 64 + +/** + * Memarea memory source. + */ +enum rte_memarea_source { + /** Memory source comes from rte memory. */ + RTE_MEMAREA_SOURCE_RTE_MEMORY, + /** Memory source comes from system API. */ + RTE_MEMAREA_SOURCE_SYSTEM_API, + /** Memory source comes from user-provided address. */ + RTE_MEMAREA_SOURCE_USER_ADDR, + /** Memory source comes from user-provided memarea. */ + RTE_MEMAREA_SOURCE_USER_MEMAREA, +}; + +/** + * Memarea memory management algorithm. + */ +enum rte_memarea_alg { + /* The default management algorithm is a variant of the next fit + * algorithm. It uses a free-list to apply for memory and uses an + * element-list in ascending order of address to support merging + * upon free. + */ + RTE_MEMAREA_ALG_DEFAULT, +}; + +struct rte_memarea; + +struct rte_memarea_param { + char name[RTE_MEMAREA_NAMESIZE]; /**< Name of memarea. */ + enum rte_memarea_source source; /**< Memory source of memarea. */ + enum rte_memarea_alg alg; /**< Memory management algorithm. */ + size_t total_sz; /**< total size (bytes) of memarea. */ + /** Indicates whether the memarea API should be MT-safe. */ + uint32_t mt_safe : 1; + union { + /** Numa socket from which to apply for memarea's memory, this + * field is valid only when the source is set to be + * RTE_MEMAREA_SOURCE_RTE_MEMORY. + */ + int numa_socket; + /** User provided address, this field is valid only when the + * source is set to be RTE_MEMAREA_SOURCE_USER_ADDR. + * Note: the provided address must align at least + * RTE_CACHE_LINE_SIZE. + */ + void *user_addr; + /** User provided memarea, this field is valid only when the + * source is set to be RTE_MEMAREA_SOURCE_USER_MEMAREA. + */ + struct rte_memarea *user_memarea; + }; +}; + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Create memarea. + * + * Create one new memarea. + * + * @param init + * The init parameter of memarea. + * + * @return + * Non-NULL on success. Otherwise NULL is returned. + */ +__rte_experimental +struct rte_memarea *rte_memarea_create(const struct rte_memarea_param *init); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Destroy memarea. + * + * Destroy the memarea. + * + * @param ma + * The pointer of memarea. + */ +__rte_experimental +void rte_memarea_destroy(struct rte_memarea *ma); + +#ifdef __cplusplus +} +#endif + +#endif /* RTE_MEMAREA_H */ diff --git a/lib/memarea/version.map b/lib/memarea/version.map new file mode 100644 index 0000000000..f36a04d7cf --- /dev/null +++ b/lib/memarea/version.map @@ -0,0 +1,12 @@ +EXPERIMENTAL { + global: + + rte_memarea_create; + rte_memarea_destroy; + + local: *; +}; + +INTERNAL { + local: *; +}; diff --git a/lib/meson.build b/lib/meson.build index c648f7d800..521a25d6c0 100644 --- a/lib/meson.build +++ b/lib/meson.build @@ -42,6 +42,7 @@ libraries = [ 'kni', 'latencystats', 'lpm', + 'memarea', 'member', 'pcapng', 'power', From patchwork Sat Sep 24 07:49:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: fengchengwen X-Patchwork-Id: 116804 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 4F3DAA0542; Sat, 24 Sep 2022 09:56:42 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 6EB7342BFC; Sat, 24 Sep 2022 09:56:17 +0200 (CEST) Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by mails.dpdk.org (Postfix) with ESMTP id 663F242BCE for ; Sat, 24 Sep 2022 09:56:11 +0200 (CEST) Received: from dggpeml500024.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4MZLp05vZGzWgtq; Sat, 24 Sep 2022 15:52:08 +0800 (CST) Received: from localhost.localdomain (10.67.165.24) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 24 Sep 2022 15:56:08 +0800 From: Chengwen Feng To: CC: , , , Subject: [PATCH v3 02/10] test/memarea: support memarea test Date: Sat, 24 Sep 2022 07:49:43 +0000 Message-ID: <20220924074951.31814-3-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220924074951.31814-1-fengchengwen@huawei.com> References: <20220721044648.6817-1-fengchengwen@huawei.com> <20220924074951.31814-1-fengchengwen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.165.24] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500024.china.huawei.com (7.185.36.10) X-CFilter-Loop: Reflected 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 This patch supports memarea test about API rte_memarea_create and rte_memarea_destroy. Signed-off-by: Chengwen Feng --- MAINTAINERS | 1 + app/test/meson.build | 2 + app/test/test_memarea.c | 149 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 app/test/test_memarea.c diff --git a/MAINTAINERS b/MAINTAINERS index 4d5bf986c6..eb2de32884 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1562,6 +1562,7 @@ Memarea - EXPERIMENTAL M: Chengwen Feng F: lib/memarea F: doc/guides/prog_guide/memarea_lib.rst +F: app/test/test_memarea* Membership - EXPERIMENTAL M: Yipeng Wang diff --git a/app/test/meson.build b/app/test/meson.build index d5cad72116..778de8d65d 100644 --- a/app/test/meson.build +++ b/app/test/meson.build @@ -84,6 +84,7 @@ test_sources = files( 'test_malloc.c', 'test_malloc_perf.c', 'test_mbuf.c', + 'test_memarea.c', 'test_member.c', 'test_member_perf.c', 'test_memcpy.c', @@ -199,6 +200,7 @@ fast_tests = [ ['malloc_autotest', false, true], ['mbuf_autotest', false, true], ['mcslock_autotest', false, true], + ['memarea_autotest', true, true], ['memcpy_autotest', true, true], ['memory_autotest', false, true], ['mempool_autotest', false, true], diff --git a/app/test/test_memarea.c b/app/test/test_memarea.c new file mode 100644 index 0000000000..7c3d78652d --- /dev/null +++ b/app/test/test_memarea.c @@ -0,0 +1,149 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2022 HiSilicon Limited + */ + +#include +#include + +#include "test.h" + +#ifdef RTE_EXEC_ENV_WINDOWS +static int +test_memarea(void) +{ + printf("memarea not supported on Windows, skipping test\n"); + return TEST_SKIPPED; +} + +#else + +#include +#include + +#define MEMAREA_TEST_DEFAULT_SIZE 0x1000 + +#define MEMAREA_TEST_API_RUN(test_func) \ + do { \ + int ret = test_func(); \ + if (ret < 0) \ + printf("%s Failed\n", #test_func); \ + else \ + printf("%s Passed\n", #test_func); \ + } while (0) + +static void +test_memarea_init_def_param(struct rte_memarea_param *init) +{ + memset(init, 0, sizeof(struct rte_memarea_param)); + sprintf(init->name, "%s", "test-memarea"); + init->source = RTE_MEMAREA_SOURCE_SYSTEM_API; + init->total_sz = MEMAREA_TEST_DEFAULT_SIZE; + init->mt_safe = 1; +} + +static int +test_memarea_create_bad_param(void) +{ + struct rte_memarea_param init; + struct rte_memarea *ma; + int i; + + /* test for NULL */ + ma = rte_memarea_create(NULL); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + + /* test for invalid name */ + memset(&init, 0, sizeof(init)); + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + memset(&init.name, 1, sizeof(init.name)); + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + + /* test for invalid source */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_USER_MEMAREA + 1; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + + /* test for total_sz */ + test_memarea_init_def_param(&init); + init.total_sz = 0; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + + /* test for user address NULL */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_USER_ADDR; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + + /* test for user address align invalid */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_USER_ADDR; + for (i = 1; i < RTE_CACHE_LINE_SIZE; i++) { + init.user_addr = (void *)((uintptr_t)i); + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + } + + /* test for user memarea NULL */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_USER_MEMAREA; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + + /* test for alg invalid */ + test_memarea_init_def_param(&init); + init.alg = RTE_MEMAREA_ALG_DEFAULT + 1; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + + return 0; +} + +static int +test_memarea_create_destroy(void) +{ + uint8_t user_buffer[MEMAREA_TEST_DEFAULT_SIZE + RTE_CACHE_LINE_SIZE]; + struct rte_memarea_param init; + struct rte_memarea *ma; + + /* test for create with RTE memory */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_RTE_MEMORY; + init.numa_socket = SOCKET_ID_ANY; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL"); + rte_memarea_destroy(ma); + + /* test for create with system API */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_SYSTEM_API; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL"); + rte_memarea_destroy(ma); + + /* test for create with user-address */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_USER_ADDR; + init.user_addr = (void *)(((uintptr_t)user_buffer + RTE_CACHE_LINE_SIZE) & + ~(RTE_CACHE_LINE_SIZE - 1)); + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL"); + rte_memarea_destroy(ma); + + return 0; +} + +static int +test_memarea(void) +{ + MEMAREA_TEST_API_RUN(test_memarea_create_bad_param); + MEMAREA_TEST_API_RUN(test_memarea_create_destroy); + return 0; +} + +#endif /* !RTE_EXEC_ENV_WINDOWS */ + +REGISTER_TEST_COMMAND(memarea_autotest, test_memarea); From patchwork Sat Sep 24 07:49:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: fengchengwen X-Patchwork-Id: 116805 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 8AE53A0542; Sat, 24 Sep 2022 09:56:47 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 4450E42C08; Sat, 24 Sep 2022 09:56:18 +0200 (CEST) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by mails.dpdk.org (Postfix) with ESMTP id A3FF242BD7 for ; Sat, 24 Sep 2022 09:56:11 +0200 (CEST) Received: from dggpeml500024.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MZLqK2VvSzpVTL; Sat, 24 Sep 2022 15:53:17 +0800 (CST) Received: from localhost.localdomain (10.67.165.24) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 24 Sep 2022 15:56:09 +0800 From: Chengwen Feng To: CC: , , , Subject: [PATCH v3 05/10] memarea: support dump API Date: Sat, 24 Sep 2022 07:49:46 +0000 Message-ID: <20220924074951.31814-6-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220924074951.31814-1-fengchengwen@huawei.com> References: <20220721044648.6817-1-fengchengwen@huawei.com> <20220924074951.31814-1-fengchengwen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.165.24] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500024.china.huawei.com (7.185.36.10) X-CFilter-Loop: Reflected 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 This patch supports rte_memarea_dump() API which could be used for debug. Signed-off-by: Chengwen Feng --- doc/guides/prog_guide/memarea_lib.rst | 3 + lib/memarea/rte_memarea.c | 85 +++++++++++++++++++++++++++ lib/memarea/rte_memarea.h | 21 +++++++ lib/memarea/version.map | 1 + 4 files changed, 110 insertions(+) diff --git a/doc/guides/prog_guide/memarea_lib.rst b/doc/guides/prog_guide/memarea_lib.rst index 41bc0a90cd..c77012fe44 100644 --- a/doc/guides/prog_guide/memarea_lib.rst +++ b/doc/guides/prog_guide/memarea_lib.rst @@ -43,6 +43,9 @@ The ``rte_memarea_update_refcnt()`` function is used to update the memory object's reference count, if the count reaches zero, the memory object will be freed to memarea. ++The ``rte_memarea_dump()`` function is used to dump the internal information ++of a memarea. + Reference --------- diff --git a/lib/memarea/rte_memarea.c b/lib/memarea/rte_memarea.c index a072f07f20..b70830d0bb 100644 --- a/lib/memarea/rte_memarea.c +++ b/lib/memarea/rte_memarea.c @@ -2,6 +2,7 @@ * Copyright(c) 2022 HiSilicon Limited */ +#include #include #include #include @@ -298,3 +299,87 @@ rte_memarea_update_refcnt(struct rte_memarea *ma, void *ptr, int16_t value) memarea_free_elem(ma, elem); memarea_unlock(ma); } + +static const char * +memarea_source_name(enum rte_memarea_source source) +{ + if (source == RTE_MEMAREA_SOURCE_RTE_MEMORY) + return "rte-memory"; + else if (source == RTE_MEMAREA_SOURCE_SYSTEM_API) + return "system-api"; + else if (source == RTE_MEMAREA_SOURCE_USER_ADDR) + return "user-addr"; + else if (source == RTE_MEMAREA_SOURCE_USER_MEMAREA) + return "user-memarea"; + else + return "unknown"; +} + +static const char * +memarea_alg_name(enum rte_memarea_alg alg) +{ + if (alg == RTE_MEMAREA_ALG_DEFAULT) + return "default"; + else + return "unknown"; +} + +static uint32_t +memarea_elem_list_num(struct rte_memarea *ma) +{ + struct memarea_elem *elem; + uint32_t num = 0; + + TAILQ_FOREACH(elem, &ma->elem_list, elem_node) + num++; + + return num; +} + +static uint32_t +memarea_free_list_num(struct rte_memarea *ma) +{ + struct memarea_elem *elem; + uint32_t num = 0; + + TAILQ_FOREACH(elem, &ma->free_list, free_node) + num++; + + return num; +} + +static void +memarea_dump_all(struct rte_memarea *ma, FILE *f) +{ + struct memarea_elem *elem; + + fprintf(f, " regions:\n"); + TAILQ_FOREACH(elem, &ma->elem_list, elem_node) + fprintf(f, " size: 0x%zx cookie: 0x%x refcnt: %d\n", + elem->size, elem->cookie, elem->refcnt); +} + +int +rte_memarea_dump(struct rte_memarea *ma, FILE *f, bool dump_all) +{ + if (ma == NULL || f == NULL) + return -EINVAL; + + memarea_lock(ma); + fprintf(f, "memarea name: %s\n", ma->init.name); + fprintf(f, " source: %s\n", memarea_source_name(ma->init.source)); + if (ma->init.source == RTE_MEMAREA_SOURCE_USER_MEMAREA) + fprintf(f, " source-user-memarea: %s\n", ma->init.user_memarea->init.name); + fprintf(f, " algorithm: %s\n", memarea_alg_name(ma->init.alg)); + fprintf(f, " total-size: 0x%zx\n", ma->init.total_sz); + fprintf(f, " mt-safe: %s\n", ma->init.mt_safe ? "yes" : "no"); + fprintf(f, " total-regions: %u\n", memarea_elem_list_num(ma)); + fprintf(f, " total-free-regions: %u\n", memarea_free_list_num(ma)); + fprintf(f, " alloc_fails: %" PRIu64 "\n", ma->alloc_fails); + fprintf(f, " refcnt_check_fails: %" PRIu64 "\n", ma->refcnt_check_fails); + if (dump_all) + memarea_dump_all(ma, f); + memarea_unlock(ma); + + return 0; +} diff --git a/lib/memarea/rte_memarea.h b/lib/memarea/rte_memarea.h index 10e0d6ad5a..10b8229c64 100644 --- a/lib/memarea/rte_memarea.h +++ b/lib/memarea/rte_memarea.h @@ -194,6 +194,27 @@ void rte_memarea_free(struct rte_memarea *ma, void *ptr); __rte_experimental void rte_memarea_update_refcnt(struct rte_memarea *ma, void *ptr, int16_t value); +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Dump memarea. + * + * Dump one memarea. + * + * @param ma + * The pointer of memarea. + * @param f + * The file to write the output to. + * @param dump_all + * Indicate whether to dump the allocated and free memory objects information. + * + * @return + * 0 on success. Otherwise negative value is returned. + */ +__rte_experimental +int rte_memarea_dump(struct rte_memarea *ma, FILE *f, bool dump_all); + #ifdef __cplusplus } #endif diff --git a/lib/memarea/version.map b/lib/memarea/version.map index a0026fc5f9..d8ddd93c13 100644 --- a/lib/memarea/version.map +++ b/lib/memarea/version.map @@ -4,6 +4,7 @@ EXPERIMENTAL { rte_memarea_alloc; rte_memarea_create; rte_memarea_destroy; + rte_memarea_dump; rte_memarea_free; rte_memarea_update_refcnt; From patchwork Sat Sep 24 07:49:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: fengchengwen X-Patchwork-Id: 116806 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 94628A0542; Sat, 24 Sep 2022 09:56:52 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 338AF42C0B; Sat, 24 Sep 2022 09:56:19 +0200 (CEST) Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by mails.dpdk.org (Postfix) with ESMTP id A806942BD8 for ; Sat, 24 Sep 2022 09:56:11 +0200 (CEST) Received: from dggpeml500024.china.huawei.com (unknown [172.30.72.54]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4MZLnp6lQqz1P6wh; Sat, 24 Sep 2022 15:51:58 +0800 (CST) Received: from localhost.localdomain (10.67.165.24) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 24 Sep 2022 15:56:09 +0800 From: Chengwen Feng To: CC: , , , Subject: [PATCH v3 07/10] memarea: support backup memory mechanism Date: Sat, 24 Sep 2022 07:49:48 +0000 Message-ID: <20220924074951.31814-8-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220924074951.31814-1-fengchengwen@huawei.com> References: <20220721044648.6817-1-fengchengwen@huawei.com> <20220924074951.31814-1-fengchengwen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.165.24] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500024.china.huawei.com (7.185.36.10) X-CFilter-Loop: Reflected 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 This patch supports backup memory mechanism, the memarea could use another memarea as a backup. Signed-off-by: Chengwen Feng --- doc/guides/prog_guide/memarea_lib.rst | 3 +++ lib/memarea/memarea_private.h | 2 ++ lib/memarea/rte_memarea.c | 22 ++++++++++++++++++++++ lib/memarea/rte_memarea.h | 7 +++++++ 4 files changed, 34 insertions(+) diff --git a/doc/guides/prog_guide/memarea_lib.rst b/doc/guides/prog_guide/memarea_lib.rst index c77012fe44..842d35f77a 100644 --- a/doc/guides/prog_guide/memarea_lib.rst +++ b/doc/guides/prog_guide/memarea_lib.rst @@ -25,6 +25,9 @@ The main features are as follows: * It supports MT-safe as long as it's specified at creation time. +* It provides backup memory mechanism, the memarea could use another memarea + as a backup. + Library API Overview -------------------- diff --git a/lib/memarea/memarea_private.h b/lib/memarea/memarea_private.h index 98406879b9..08735ca81f 100644 --- a/lib/memarea/memarea_private.h +++ b/lib/memarea/memarea_private.h @@ -23,11 +23,13 @@ struct rte_memarea { struct rte_memarea_param init; rte_spinlock_t lock; void *area_addr; + void *top_addr; struct memarea_elem_list elem_list; struct memarea_elem_list free_list; uint64_t alloc_fails; uint64_t refcnt_check_fails; + uint64_t bak_alloc_fails; } __rte_cache_aligned; #endif /* MEMAREA_PRIVATE_H */ diff --git a/lib/memarea/rte_memarea.c b/lib/memarea/rte_memarea.c index b70830d0bb..f45191aa7f 100644 --- a/lib/memarea/rte_memarea.c +++ b/lib/memarea/rte_memarea.c @@ -132,6 +132,7 @@ rte_memarea_create(const struct rte_memarea_param *init) TAILQ_INIT(&ma->elem_list); TAILQ_INIT(&ma->free_list); ma->area_addr = addr; + ma->top_addr = (void *)((uintptr_t)addr + init->total_sz - 1); elem = addr; elem->size = init->total_sz - sizeof(struct memarea_elem); elem->cookie = MEMAREA_FREE_ELEM_COOKIE; @@ -200,6 +201,15 @@ memarea_add_node(struct rte_memarea *ma, struct memarea_elem *elem, size_t need_ elem->size = align_size; } +static inline void * +memarea_alloc_backup(struct rte_memarea *ma, size_t size, uint32_t cookie) +{ + void *ptr = rte_memarea_alloc(ma->init.bak_memarea, size, cookie); + if (unlikely(ptr == NULL)) + ma->bak_alloc_fails++; + return ptr; +} + void * rte_memarea_alloc(struct rte_memarea *ma, size_t size, uint32_t cookie) { @@ -221,6 +231,8 @@ rte_memarea_alloc(struct rte_memarea *ma, size_t size, uint32_t cookie) ptr = (void *)((uintptr_t)elem + sizeof(struct memarea_elem)); break; } + if (ptr == NULL && ma->init.bak_memarea != NULL) + ptr = memarea_alloc_backup(ma, size, cookie); if (unlikely(ptr == NULL)) ma->alloc_fails++; memarea_unlock(ma); @@ -283,6 +295,12 @@ rte_memarea_update_refcnt(struct rte_memarea *ma, void *ptr, int16_t value) return; memarea_lock(ma); + if (ptr < ma->area_addr || ptr > ma->top_addr) { + rte_memarea_update_refcnt(ma->init.bak_memarea, ptr, value); + memarea_unlock(ma); + return; + } + if (unlikely(elem->refcnt <= 0 || elem->refcnt + value < 0)) { RTE_LOG(ERR, MEMAREA, "memarea: %s cookie: 0x%x curr refcnt: %d update refcnt: %d check fail!\n", @@ -373,10 +391,14 @@ rte_memarea_dump(struct rte_memarea *ma, FILE *f, bool dump_all) fprintf(f, " algorithm: %s\n", memarea_alg_name(ma->init.alg)); fprintf(f, " total-size: 0x%zx\n", ma->init.total_sz); fprintf(f, " mt-safe: %s\n", ma->init.mt_safe ? "yes" : "no"); + if (ma->init.bak_memarea) + fprintf(f, " backup-memarea-name: %s\n", ma->init.bak_memarea->init.name); fprintf(f, " total-regions: %u\n", memarea_elem_list_num(ma)); fprintf(f, " total-free-regions: %u\n", memarea_free_list_num(ma)); fprintf(f, " alloc_fails: %" PRIu64 "\n", ma->alloc_fails); fprintf(f, " refcnt_check_fails: %" PRIu64 "\n", ma->refcnt_check_fails); + if (ma->init.bak_memarea) + fprintf(f, " backup_alloc_fails: %" PRIu64 "\n", ma->bak_alloc_fails); if (dump_all) memarea_dump_all(ma, f); memarea_unlock(ma); diff --git a/lib/memarea/rte_memarea.h b/lib/memarea/rte_memarea.h index 10b8229c64..348febab7f 100644 --- a/lib/memarea/rte_memarea.h +++ b/lib/memarea/rte_memarea.h @@ -39,6 +39,9 @@ * specified, all the functions of the memarea API are lock-free, and assume * to not be invoked in parallel on different logical cores to work on the * same memarea. + * - It provides backup memory mechanism, the memarea could use another memarea + * as a backup. It will attempts to allocate object from backup memarea when + * the current memarea failed to allocate. */ #include @@ -105,6 +108,10 @@ struct rte_memarea_param { */ struct rte_memarea *user_memarea; }; + /** Backup memarea, which is used to handle the scenario where the + * current memarea allocation failure. + */ + struct rte_memarea *bak_memarea; }; /** From patchwork Sat Sep 24 07:49:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: fengchengwen X-Patchwork-Id: 116807 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 51AADA0542; Sat, 24 Sep 2022 09:56:57 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id F31CA42C11; Sat, 24 Sep 2022 09:56:19 +0200 (CEST) Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by mails.dpdk.org (Postfix) with ESMTP id AD24142BD9 for ; Sat, 24 Sep 2022 09:56:11 +0200 (CEST) Received: from dggpeml500024.china.huawei.com (unknown [172.30.72.53]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4MZLr51RSSzHqJS; Sat, 24 Sep 2022 15:53:57 +0800 (CST) Received: from localhost.localdomain (10.67.165.24) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 24 Sep 2022 15:56:09 +0800 From: Chengwen Feng To: CC: , , , Subject: [PATCH v3 09/10] memarea: detect memory corruption based on magic Date: Sat, 24 Sep 2022 07:49:50 +0000 Message-ID: <20220924074951.31814-10-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220924074951.31814-1-fengchengwen@huawei.com> References: <20220721044648.6817-1-fengchengwen@huawei.com> <20220924074951.31814-1-fengchengwen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.165.24] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500024.china.huawei.com (7.185.36.10) X-CFilter-Loop: Reflected 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 This patch provides lightweight mechanism for detecting memory corruption which based on magic field in each element node. Signed-off-by: Chengwen Feng --- lib/memarea/memarea_private.h | 2 ++ lib/memarea/rte_memarea.c | 29 ++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/memarea/memarea_private.h b/lib/memarea/memarea_private.h index 08735ca81f..4f5393e6f9 100644 --- a/lib/memarea/memarea_private.h +++ b/lib/memarea/memarea_private.h @@ -7,10 +7,12 @@ #include +#define MEMAREA_ELEM_MAGIC_NUM 0xbeef1234 #define MEMAREA_FREE_ELEM_COOKIE 0xFFFFFFFF struct memarea_elem { size_t size; + uint32_t magic; uint32_t cookie; int32_t refcnt; /* Non-zero indicates that it has been allocated */ TAILQ_ENTRY(memarea_elem) elem_node; diff --git a/lib/memarea/rte_memarea.c b/lib/memarea/rte_memarea.c index f45191aa7f..c81d4dd3fa 100644 --- a/lib/memarea/rte_memarea.c +++ b/lib/memarea/rte_memarea.c @@ -135,6 +135,7 @@ rte_memarea_create(const struct rte_memarea_param *init) ma->top_addr = (void *)((uintptr_t)addr + init->total_sz - 1); elem = addr; elem->size = init->total_sz - sizeof(struct memarea_elem); + elem->magic = MEMAREA_ELEM_MAGIC_NUM; elem->cookie = MEMAREA_FREE_ELEM_COOKIE; elem->refcnt = 0; TAILQ_INSERT_TAIL(&ma->elem_list, elem, elem_node); @@ -194,6 +195,7 @@ memarea_add_node(struct rte_memarea *ma, struct memarea_elem *elem, size_t need_ new_elem = (struct memarea_elem *)((uintptr_t)elem + sizeof(struct memarea_elem) + align_size); new_elem->size = elem->size - align_size - sizeof(struct memarea_elem); + new_elem->magic = MEMAREA_ELEM_MAGIC_NUM; new_elem->cookie = MEMAREA_FREE_ELEM_COOKIE; new_elem->refcnt = 0; TAILQ_INSERT_AFTER(&ma->elem_list, elem, new_elem, elem_node); @@ -221,6 +223,8 @@ rte_memarea_alloc(struct rte_memarea *ma, size_t size, uint32_t cookie) memarea_lock(ma); TAILQ_FOREACH(elem, &ma->free_list, free_node) { + if (unlikely(elem->magic != MEMAREA_ELEM_MAGIC_NUM)) + break; if (elem->size < size) continue; if (memarea_whether_add_node(elem->size, size)) @@ -253,6 +257,7 @@ memarea_merge_node(struct rte_memarea *ma, struct memarea_elem *curr, { curr->size += next->size + sizeof(struct memarea_elem); next->size = 0; + next->magic = ~MEMAREA_ELEM_MAGIC_NUM; next->cookie = 0; TAILQ_REMOVE(&ma->elem_list, next, elem_node); if (del_next_from_free) @@ -295,6 +300,13 @@ rte_memarea_update_refcnt(struct rte_memarea *ma, void *ptr, int16_t value) return; memarea_lock(ma); + if (unlikely(elem->magic != MEMAREA_ELEM_MAGIC_NUM)) { + RTE_LOG(ERR, MEMAREA, "memarea: %s magic: 0x%x check fail!\n", + ma->init.name, elem->magic); + memarea_unlock(ma); + return; + } + if (ptr < ma->area_addr || ptr > ma->top_addr) { rte_memarea_update_refcnt(ma->init.bak_memarea, ptr, value); memarea_unlock(ma); @@ -348,8 +360,11 @@ memarea_elem_list_num(struct rte_memarea *ma) struct memarea_elem *elem; uint32_t num = 0; - TAILQ_FOREACH(elem, &ma->elem_list, elem_node) + TAILQ_FOREACH(elem, &ma->elem_list, elem_node) { + if (elem->magic != MEMAREA_ELEM_MAGIC_NUM) + break; num++; + } return num; } @@ -360,8 +375,11 @@ memarea_free_list_num(struct rte_memarea *ma) struct memarea_elem *elem; uint32_t num = 0; - TAILQ_FOREACH(elem, &ma->free_list, free_node) + TAILQ_FOREACH(elem, &ma->free_list, free_node) { + if (elem->magic != MEMAREA_ELEM_MAGIC_NUM) + break; num++; + } return num; } @@ -372,9 +390,14 @@ memarea_dump_all(struct rte_memarea *ma, FILE *f) struct memarea_elem *elem; fprintf(f, " regions:\n"); - TAILQ_FOREACH(elem, &ma->elem_list, elem_node) + TAILQ_FOREACH(elem, &ma->elem_list, elem_node) { + if (elem->magic != MEMAREA_ELEM_MAGIC_NUM) { + fprintf(f, " magic: 0x%x chech fail!\n", elem->magic); + break; + } fprintf(f, " size: 0x%zx cookie: 0x%x refcnt: %d\n", elem->size, elem->cookie, elem->refcnt); + } } int From patchwork Sat Sep 24 07:49:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: fengchengwen X-Patchwork-Id: 116809 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id E90ECA0542; Sat, 24 Sep 2022 09:57:11 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 350E842C34; Sat, 24 Sep 2022 09:56:22 +0200 (CEST) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by mails.dpdk.org (Postfix) with ESMTP id D7CB6400D4 for ; Sat, 24 Sep 2022 09:56:12 +0200 (CEST) Received: from dggpeml500024.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MZLqL0WclzpVTl; Sat, 24 Sep 2022 15:53:18 +0800 (CST) Received: from localhost.localdomain (10.67.165.24) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 24 Sep 2022 15:56:09 +0800 From: Chengwen Feng To: CC: , , , Subject: [PATCH v3 10/10] test/memarea: support no MT-safe test Date: Sat, 24 Sep 2022 07:49:51 +0000 Message-ID: <20220924074951.31814-11-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220924074951.31814-1-fengchengwen@huawei.com> References: <20220721044648.6817-1-fengchengwen@huawei.com> <20220924074951.31814-1-fengchengwen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.165.24] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500024.china.huawei.com (7.185.36.10) X-CFilter-Loop: Reflected 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 MT-safe is enabled by default in previous test, this patch adds no MT-safe test. Signed-off-by: Chengwen Feng --- app/test/test_memarea.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/app/test/test_memarea.c b/app/test/test_memarea.c index ec3475c354..b4012086fc 100644 --- a/app/test/test_memarea.c +++ b/app/test/test_memarea.c @@ -353,6 +353,35 @@ test_memarea_backup(void) return 0; } +static int +test_memarea_no_mt_safe(void) +{ + struct rte_memarea_param init; + struct rte_memarea *ma; + int ret; + + /* prepare env */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_SYSTEM_API; + init.total_sz = MEMAREA_TEST_DEFAULT_SIZE; + init.mt_safe = false; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL"); + + /* test for all API */ + (void)rte_memarea_alloc(ma, 1, 0); + (void)rte_memarea_alloc(ma, 1, 0); + rte_memarea_free(ma, rte_memarea_alloc(ma, 1, 0)); + rte_memarea_update_refcnt(ma, rte_memarea_alloc(ma, 1, 0), 1); + rte_memarea_update_refcnt(ma, rte_memarea_alloc(ma, 1, 0), -1); + ret = rte_memarea_dump(ma, stderr, true); + RTE_TEST_ASSERT(ret == 0, "Expected ZERO"); + + rte_memarea_destroy(ma); + + return 0; +} + static int test_memarea(void) { @@ -363,6 +392,7 @@ test_memarea(void) MEMAREA_TEST_API_RUN(test_memarea_alloc_free); MEMAREA_TEST_API_RUN(test_memarea_dump); MEMAREA_TEST_API_RUN(test_memarea_backup); + MEMAREA_TEST_API_RUN(test_memarea_no_mt_safe); return 0; }