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 8E3E74239A; Tue, 10 Jan 2023 03:16:29 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 3547840689; Tue, 10 Jan 2023 03:16:29 +0100 (CET) Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by mails.dpdk.org (Postfix) with ESMTP id F26B140150 for ; Tue, 10 Jan 2023 03:16:26 +0100 (CET) Received: from kwepemi500017.china.huawei.com (unknown [172.30.72.55]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4NrZ820h5bzJq8F; Tue, 10 Jan 2023 10:12:18 +0800 (CST) Received: from [10.67.103.235] (10.67.103.235) by kwepemi500017.china.huawei.com (7.221.188.110) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Tue, 10 Jan 2023 10:16:23 +0800 Subject: Re: [PATCH v11 3/6] memarea: support alloc/free/refcnt-update API To: Chengwen Feng , , , , , , References: <20220721044648.6817-1-fengchengwen@huawei.com> <20221213091358.49237-1-fengchengwen@huawei.com> <20221213091358.49237-4-fengchengwen@huawei.com> CC: , From: Dongdong Liu Message-ID: <4cc0a95f-81dd-e89c-05aa-11dcdc20e6da@huawei.com> Date: Tue, 10 Jan 2023 10:16:23 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1 MIME-Version: 1.0 In-Reply-To: <20221213091358.49237-4-fengchengwen@huawei.com> Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.67.103.235] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemi500017.china.huawei.com (7.221.188.110) 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 On 2022/12/13 17:13, Chengwen Feng wrote: > This patch supports rte_memarea_alloc()/rte_memarea_free()/ > rte_memarea_refcnt_update() 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 | 157 ++++++++++++++++++++++++++ > lib/memarea/rte_memarea.h | 74 ++++++++++++ > lib/memarea/version.map | 3 + > 5 files changed, 247 insertions(+) > > diff --git a/doc/guides/prog_guide/memarea_lib.rst b/doc/guides/prog_guide/memarea_lib.rst > index d032b24ce9..8fbfd06a5d 100644 > --- a/doc/guides/prog_guide/memarea_lib.rst > +++ b/doc/guides/prog_guide/memarea_lib.rst > @@ -33,6 +33,16 @@ returns the pointer to the created memarea or ``NULL`` if the creation failed. > > The ``rte_memarea_destroy()`` function is used to destroy a memarea. > > +The ``rte_memarea_alloc()`` function is used to alloc one memory object from > +the memarea. > + > +The ``rte_memarea_free()`` function is used to free one memory object which > +allocated by ``rte_memarea_alloc()``. > + > +The ``rte_memarea_refcnt_update()`` function is used to update the memory > +object's reference count, if the count reaches zero, the memory object will > +be freed to memarea. It seems provide rte_memarea_refcnt_update() API is too usage-specific. can we delete this ? Thanks, Dongdong > + > Reference > --------- > > diff --git a/lib/memarea/memarea_private.h b/lib/memarea/memarea_private.h > index 59be9c1d00..f5accf2987 100644 > --- a/lib/memarea/memarea_private.h > +++ b/lib/memarea/memarea_private.h > @@ -28,6 +28,9 @@ struct rte_memarea { > 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 41e5c007c2..a0935dafb6 100644 > --- a/lib/memarea/rte_memarea.c > +++ b/lib/memarea/rte_memarea.c > @@ -4,6 +4,7 @@ > > #include > #include > +#include > > #include > #include > @@ -84,6 +85,8 @@ memarea_alloc_area(const struct rte_memarea_param *init) > init->numa_socket); > else if (init->source == RTE_MEMAREA_SOURCE_LIBC) > ptr = memarea_alloc_from_libc(init->total_sz); > + else if (init->source == RTE_MEMAREA_SOURCE_MEMAREA) > + ptr = rte_memarea_alloc(init->src_memarea, init->total_sz, 0); > > if (ptr == NULL) > RTE_LOG(ERR, MEMAREA, "memarea: %s alloc memory area fail!\n", init->name); > @@ -146,6 +149,8 @@ memarea_free_area(struct rte_memarea *ma) > rte_free(ma->area_addr); > else if (ma->init.source == RTE_MEMAREA_SOURCE_LIBC) > free(ma->area_addr); > + else if (ma->init.source == RTE_MEMAREA_SOURCE_MEMAREA) > + rte_memarea_free(ma->init.src_memarea, ma->area_addr); > } > > void > @@ -156,3 +161,155 @@ rte_memarea_destroy(struct rte_memarea *ma) > memarea_free_area(ma); > rte_free(ma); > } > + > +static inline void > +memarea_lock(struct rte_memarea *ma) > +{ > + if (ma->init.mt_safe) > + rte_spinlock_lock(&ma->lock); > +} > + > +static inline void > +memarea_unlock(struct rte_memarea *ma) > +{ > + if (ma->init.mt_safe) > + rte_spinlock_unlock(&ma->lock); > +} > + > +static inline bool > +memarea_whether_add_node(size_t free_size, size_t need_size) > +{ > + size_t align_size = RTE_ALIGN_CEIL(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 rte_memarea *ma, struct memarea_elem *elem, size_t need_size) > +{ > + size_t align_size = RTE_ALIGN_CEIL(need_size, RTE_CACHE_LINE_SIZE); > + struct memarea_elem *new_elem; > + > + new_elem = (struct memarea_elem *)RTE_PTR_ADD(elem, sizeof(struct memarea_elem) + > + align_size); > + new_elem->size = elem->size - align_size - sizeof(struct memarea_elem); > + new_elem->magic = MEMAREA_AVAILABLE_ELEM_MAGIC; > + new_elem->cookie = MEMAREA_AVAILABLE_ELEM_COOKIE; > + new_elem->refcnt = 0; > + TAILQ_INSERT_AFTER(&ma->elem_list, elem, new_elem, elem_node); > + TAILQ_INSERT_AFTER(&ma->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_elem *elem; > + void *ptr = NULL; > + > + if (unlikely(ma == NULL || size == 0)) > + return NULL; > + > + memarea_lock(ma); > + TAILQ_FOREACH(elem, &ma->free_list, free_node) { > + if (unlikely(elem->magic != MEMAREA_AVAILABLE_ELEM_MAGIC)) > + break; > + if (elem->size < size) > + continue; > + if (memarea_whether_add_node(elem->size, size)) > + memarea_add_node(ma, elem, size); > + elem->magic = MEMAREA_ALLOCATED_ELEM_MAGIC; > + elem->cookie = cookie; > + elem->refcnt = 1; > + TAILQ_REMOVE(&ma->free_list, elem, free_node); > + ptr = RTE_PTR_ADD(elem, sizeof(struct memarea_elem)); > + break; > + } > + if (unlikely(ptr == NULL)) > + ma->alloc_fails++; > + memarea_unlock(ma); > + > + return ptr; > +} > + > +void > +rte_memarea_free(struct rte_memarea *ma, void *ptr) > +{ > + if (unlikely(ma == NULL || ptr == NULL)) > + return; > + > + rte_memarea_refcnt_update(ma, ptr, -1); > +} > + > +static inline void > +memarea_merge_node(struct rte_memarea *ma, 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->magic = 0; > + next->cookie = 0; > + TAILQ_REMOVE(&ma->elem_list, next, elem_node); > + if (del_next_from_free) > + TAILQ_REMOVE(&ma->free_list, next, free_node); > + if (add_curr_to_free) { > + curr->magic = MEMAREA_AVAILABLE_ELEM_MAGIC; > + curr->cookie = MEMAREA_AVAILABLE_ELEM_COOKIE; > + TAILQ_INSERT_TAIL(&ma->free_list, curr, free_node); > + } > +} > + > +static inline void > +memarea_free_elem(struct rte_memarea *ma, 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(ma, prev, elem, false, false); > + elem = prev; > + merged = true; > + } > + if (next != NULL && next->refcnt == 0) { > + memarea_merge_node(ma, elem, next, true, !merged); > + merged = true; > + } > + if (!merged) { > + elem->magic = MEMAREA_AVAILABLE_ELEM_MAGIC; > + elem->cookie = MEMAREA_AVAILABLE_ELEM_COOKIE; > + TAILQ_INSERT_TAIL(&ma->free_list, elem, free_node); > + } > +} > + > +void > +rte_memarea_refcnt_update(struct rte_memarea *ma, void *ptr, int32_t value) > +{ > + struct memarea_elem *elem = (struct memarea_elem *)RTE_PTR_SUB(ptr, > + sizeof(struct memarea_elem)); > + > + memarea_lock(ma); > + if (unlikely(elem->magic != MEMAREA_ALLOCATED_ELEM_MAGIC)) { > + RTE_LOG(ERR, MEMAREA, "memarea: %s magic: 0x%x check fail!\n", > + ma->init.name, elem->magic); > + 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", > + ma->init.name, elem->cookie, elem->refcnt, value); > + ma->refcnt_check_fails++; > + if (elem->refcnt > 0) > + elem->refcnt += value; > + memarea_unlock(ma); > + return; > + } > + > + elem->refcnt += value; > + if (elem->refcnt == 0) > + memarea_free_elem(ma, elem); > + memarea_unlock(ma); > +} > diff --git a/lib/memarea/rte_memarea.h b/lib/memarea/rte_memarea.h > index d48ceaf733..31c499ec07 100644 > --- a/lib/memarea/rte_memarea.h > +++ b/lib/memarea/rte_memarea.h > @@ -117,6 +117,80 @@ 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 object 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. > + * > + * @return > + * - NULL on error. Not enough memory, or invalid arguments (ma is NULL, > + * size is 0). > + * - Otherwise, the pointer to the allocated object. > + */ > +__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 object to the memarea. If the object's refcnt is greater > + * than one, only the refcnt is decremented by one. Otherwise the object is > + * released. > + * @note The memory object must have been returned by a previous call to > + * rte_memarea_alloc(), if it is allocated from memarea-A, it must be freed to > + * the same memarea-A. The behaviour of rte_memarea_free() is undefined if the > + * memarea or pointer does not match these requirements. > + * > + * @param ma > + * The pointer of memarea. If the ma is NULL, the function does nothing. > + * @param ptr > + * The pointer of memory object which need be freed. If the pointer is NULL, > + * the function does nothing. > + */ > +__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 object's refcnt. When refcnt is updated to be zero, the > + * memory object is freed. > + * > + * @note The memory object must have been returned by a previous call to > + * rte_memarea_alloc(), if it is allocated from memarea-A, the refcnt update > + * must be operated to the same memarea-A. The behaviour of > + * rte_memarea_refcnt_update() is undefined if the memarea or pointer does not > + * match these requirements. > + * > + * @note If the memory object's refcnt updated to be lower than zero, an error > + * message will be printed, and the memory object will not freed to memrea. > + * > + * @param ma > + * The pointer of memarea. > + * @param ptr > + * The pointer of memory object which need be updated refcnt. > + * @param value > + * The value which need be updated. > + */ > +__rte_experimental > +void rte_memarea_refcnt_update(struct rte_memarea *ma, void *ptr, int32_t value); > + > #ifdef __cplusplus > } > #endif > diff --git a/lib/memarea/version.map b/lib/memarea/version.map > index f36a04d7cf..a087d40d5f 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_refcnt_update; > > local: *; > }; >