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 7912FA00C3; Tue, 20 Sep 2022 05:53:39 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 6DADE42B76; Tue, 20 Sep 2022 05:53:07 +0200 (CEST) Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by mails.dpdk.org (Postfix) with ESMTP id 758B2410EE for ; Tue, 20 Sep 2022 05:53:00 +0200 (CEST) Received: from dggpeml500024.china.huawei.com (unknown [172.30.72.53]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4MWnb85Mn6z14QfQ; Tue, 20 Sep 2022 11:48:52 +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; Tue, 20 Sep 2022 11:52:57 +0800 From: Chengwen Feng To: CC: , , , Subject: [PATCH 3/8] memarea: support alloc/free/update-refcnt API Date: Tue, 20 Sep 2022 03:46:38 +0000 Message-ID: <20220920034643.55476-4-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220920034643.55476-1-fengchengwen@huawei.com> References: <20220721044648.6817-1-fengchengwen@huawei.com> <20220920034643.55476-1-fengchengwen@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.67.165.24] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) 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_alloc()/rte_memarea_free()/ rte_memarea_update_refcnt() API. Signed-off-by: Chengwen Feng --- doc/guides/prog_guide/memarea_lib.rst | 10 ++ lib/memarea/memarea_private.h | 3 + lib/memarea/rte_memarea.c | 140 ++++++++++++++++++++++++++ lib/memarea/rte_memarea.h | 56 +++++++++++ lib/memarea/version.map | 3 + 5 files changed, 212 insertions(+) diff --git a/doc/guides/prog_guide/memarea_lib.rst b/doc/guides/prog_guide/memarea_lib.rst index 1979f0a12c..967129d560 100644 --- a/doc/guides/prog_guide/memarea_lib.rst +++ b/doc/guides/prog_guide/memarea_lib.rst @@ -35,6 +35,16 @@ failed. The ``rte_memarea_destroy()`` function is used to destroy a memarea object. +The ``rte_memarea_alloc()`` function is used to alloc one memory region from +the memarea object. + +The ``rte_memarea_free()`` function is used to free one memory region which +allocated by ``rte_memarea_alloc()``. + +The ``rte_memarea_update_refcnt()`` function is used to update the memory +region's reference count, if the count reaches zero, the memory region will +be freed to memarea object. + Reference --------- diff --git a/lib/memarea/memarea_private.h b/lib/memarea/memarea_private.h index 94931b46e9..f71e01e7b5 100644 --- a/lib/memarea/memarea_private.h +++ b/lib/memarea/memarea_private.h @@ -25,6 +25,9 @@ struct memarea_private { void *area_addr; struct memarea_elem_list elem_list; struct memarea_elem_list free_list; + + uint64_t alloc_fails; + uint64_t refcnt_check_fails; } __rte_cache_aligned; #endif /* MEMAREA_PRIVATE_H */ diff --git a/lib/memarea/rte_memarea.c b/lib/memarea/rte_memarea.c index 3f535d315f..fe4a820173 100644 --- a/lib/memarea/rte_memarea.c +++ b/lib/memarea/rte_memarea.c @@ -3,6 +3,7 @@ */ #include +#include #include #include @@ -73,6 +74,8 @@ memarea_alloc_area(const struct rte_memarea_param *init) ptr = memarea_alloc_from_system_api(init->total_sz); else if (init->source == RTE_MEMAREA_SOURCE_USER_ADDR) ptr = init->user_addr; + else if (init->source == RTE_MEMAREA_SOURCE_USER_MEMAREA) + ptr = rte_memarea_alloc(init->user_memarea, init->total_sz, 0); if (ptr == NULL) RTE_LOG(ERR, MEMAREA, "memarea alloc memory area fail!\n"); @@ -127,6 +130,8 @@ memarea_free_area(struct memarea_private *priv) { if (priv->init.source == RTE_MEMAREA_SOURCE_SYSTEM_API) free(priv->area_addr); + else if (priv->init.source == RTE_MEMAREA_SOURCE_USER_MEMAREA) + rte_memarea_free(priv->init.user_memarea, priv->area_addr); } void @@ -138,3 +143,138 @@ rte_memarea_destroy(struct rte_memarea *ma) rte_free(ma->private_data); rte_free(ma); } + +static inline void +memarea_lock(struct memarea_private *priv) +{ + if (priv->init.mt_safe) + rte_spinlock_lock(&priv->lock); +} + +static inline void +memarea_unlock(struct memarea_private *priv) +{ + if (priv->init.mt_safe) + rte_spinlock_unlock(&priv->lock); +} + +#define roundup(val, align) ((((val) + ((align) - 1)) / (align)) * (align)) + +static inline bool +memarea_whether_add_node(size_t free_size, size_t need_size) +{ + size_t align_size = roundup(need_size, RTE_CACHE_LINE_SIZE); + return free_size > align_size && (free_size - align_size) > sizeof(struct memarea_elem); +} + +static inline void +memarea_add_node(struct memarea_private *priv, struct memarea_elem *elem, size_t need_size) +{ + size_t align_size = roundup(need_size, RTE_CACHE_LINE_SIZE); + struct memarea_elem *new_elem; + 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->cookie = MEMAREA_FREE_ELEM_COOKIE; + new_elem->refcnt = 0; + TAILQ_INSERT_AFTER(&priv->elem_list, elem, new_elem, elem_node); + TAILQ_INSERT_AFTER(&priv->free_list, elem, new_elem, free_node); + elem->size = align_size; +} + +void * +rte_memarea_alloc(struct rte_memarea *ma, size_t size, uint32_t cookie) +{ + struct memarea_private *priv; + struct memarea_elem *elem; + void *ptr = NULL; + + if (unlikely(ma == NULL || size == 0)) + return NULL; + + priv = ma->private_data; + memarea_lock(priv); + TAILQ_FOREACH(elem, &priv->free_list, free_node) { + if (elem->size < size) + continue; + if (memarea_whether_add_node(elem->size, size)) + memarea_add_node(priv, elem, size); + elem->cookie = cookie; + elem->refcnt = 1; + TAILQ_REMOVE(&priv->free_list, elem, free_node); + ptr = (void *)((uintptr_t)elem + sizeof(struct memarea_elem)); + break; + } + if (unlikely(ptr == NULL)) + priv->alloc_fails++; + memarea_unlock(priv); + + return ptr; +} + +void +rte_memarea_free(struct rte_memarea *ma, void *ptr) +{ + rte_memarea_update_refcnt(ma, ptr, -1); +} + +static inline void +memarea_merge_node(struct memarea_private *priv, struct memarea_elem *curr, + struct memarea_elem *next, bool del_next_from_free, + bool add_curr_to_free) +{ + curr->size += next->size + sizeof(struct memarea_elem); + next->size = 0; + next->cookie = 0; + TAILQ_REMOVE(&priv->elem_list, next, elem_node); + if (del_next_from_free) + TAILQ_REMOVE(&priv->free_list, next, free_node); + if (add_curr_to_free) + TAILQ_INSERT_TAIL(&priv->free_list, curr, free_node); +} + +static inline void +memarea_free_elem(struct memarea_private *priv, struct memarea_elem *elem) +{ + struct memarea_elem *prev, *next; + bool merged = false; + prev = TAILQ_PREV(elem, memarea_elem_list, elem_node); + next = TAILQ_NEXT(elem, elem_node); + if (prev != NULL && prev->refcnt == 0) { + memarea_merge_node(priv, prev, elem, false, false); + elem = prev; + merged = true; + } + if (next != NULL && next->refcnt == 0) { + memarea_merge_node(priv, elem, next, true, !merged); + merged = true; + } + if (!merged) { + elem->cookie = MEMAREA_FREE_ELEM_COOKIE; + TAILQ_INSERT_TAIL(&priv->free_list, elem, free_node); + } +} + +void +rte_memarea_update_refcnt(struct rte_memarea *ma, void *ptr, int16_t value) +{ + struct memarea_elem *elem = (struct memarea_elem *)((uintptr_t)ptr - + sizeof(struct memarea_elem)); + struct memarea_private *priv = ma->private_data; + + memarea_lock(priv); + if (unlikely(elem->refcnt <= 0 || elem->refcnt + value < 0)) { + RTE_LOG(ERR, MEMAREA, + "memarea cookie: %u curr refcnt: %d update refcnt: %d check fail!\n", + elem->cookie, elem->refcnt, value); + priv->refcnt_check_fails++; + if (elem->refcnt > 0) + elem->refcnt += value; + memarea_unlock(priv); + return; + } + elem->refcnt += value; + if (elem->refcnt == 0) + memarea_free_elem(priv, elem); + memarea_unlock(priv); +} diff --git a/lib/memarea/rte_memarea.h b/lib/memarea/rte_memarea.h index ddca0a47e2..cded2904e3 100644 --- a/lib/memarea/rte_memarea.h +++ b/lib/memarea/rte_memarea.h @@ -124,6 +124,62 @@ struct rte_memarea *rte_memarea_create(const struct rte_memarea_param *init); __rte_experimental void rte_memarea_destroy(struct rte_memarea *ma); +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Allocate memory from memarea. + * + * Allocate one memory region from the memarea. + * + * @param ma + * The pointer of memarea. + * @param size + * The memory size to be allocated. + * @param cookie + * User-provided footprint which could used to debug memory leak problem. + * + * @return + * Non-NULL on success. Otherwise NULL is returned. + */ +__rte_experimental +void *rte_memarea_alloc(struct rte_memarea *ma, size_t size, uint32_t cookie); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Free memory to memarea. + * + * Free one memory region to the memarea. + * + * @param ma + * The pointer of memarea. + * @param ptr + * The pointer of memory region which need be freed. + */ +__rte_experimental +void rte_memarea_free(struct rte_memarea *ma, void *ptr); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Update memory's refcnt. + * + * Update one memory region's refcnt. + * When refcnt is updated to be zero, the memory region is freed. + * + * @param ma + * The pointer of memarea. + * @param ptr + * The pointer of memory region which need be updated refcnt. + * @param value + * The value which need be updated. + */ +__rte_experimental +void rte_memarea_update_refcnt(struct rte_memarea *ma, void *ptr, int16_t value); + #ifdef __cplusplus } #endif diff --git a/lib/memarea/version.map b/lib/memarea/version.map index f36a04d7cf..a0026fc5f9 100644 --- a/lib/memarea/version.map +++ b/lib/memarea/version.map @@ -1,8 +1,11 @@ EXPERIMENTAL { global: + rte_memarea_alloc; rte_memarea_create; rte_memarea_destroy; + rte_memarea_free; + rte_memarea_update_refcnt; local: *; }; -- 2.17.1