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 683FDA00C3; Tue, 20 Sep 2022 05:53:13 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 3AB9F427FF; Tue, 20 Sep 2022 05:53:03 +0200 (CEST) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by mails.dpdk.org (Postfix) with ESMTP id 027BF40E0F for ; Tue, 20 Sep 2022 05:53:00 +0200 (CEST) Received: from dggpeml500024.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MWncg5V1TzpT3J; Tue, 20 Sep 2022 11:50:11 +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:58 +0800 From: Chengwen Feng To: CC: , , , Subject: [PATCH 7/8] memarea: support backup memory mechanism Date: Tue, 20 Sep 2022 03:46:42 +0000 Message-ID: <20220920034643.55476-8-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 backup memory mechanism, the memarea object could use another memarea object 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 | 6 ++++++ 4 files changed, 33 insertions(+) diff --git a/doc/guides/prog_guide/memarea_lib.rst b/doc/guides/prog_guide/memarea_lib.rst index 7686625c90..c52ab95aeb 100644 --- a/doc/guides/prog_guide/memarea_lib.rst +++ b/doc/guides/prog_guide/memarea_lib.rst @@ -26,6 +26,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 memory object could use another + memarea object as a backup. + Library API Overview -------------------- diff --git a/lib/memarea/memarea_private.h b/lib/memarea/memarea_private.h index f71e01e7b5..ce765ed801 100644 --- a/lib/memarea/memarea_private.h +++ b/lib/memarea/memarea_private.h @@ -23,11 +23,13 @@ struct memarea_private { 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 e40715d16a..3efe8d4819 100644 --- a/lib/memarea/rte_memarea.c +++ b/lib/memarea/rte_memarea.c @@ -116,6 +116,7 @@ rte_memarea_create(const struct rte_memarea_param *init) TAILQ_INIT(&priv->elem_list); TAILQ_INIT(&priv->free_list); priv->area_addr = addr; + priv->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; @@ -183,6 +184,15 @@ memarea_add_node(struct memarea_private *priv, struct memarea_elem *elem, size_t elem->size = align_size; } +static inline void * +memarea_alloc_backup(struct memarea_private *priv, size_t size, uint32_t cookie) +{ + void *ptr = rte_memarea_alloc(priv->init.bak_memarea, size, cookie); + if (unlikely(ptr == NULL)) + priv->bak_alloc_fails++; + return ptr; +} + void * rte_memarea_alloc(struct rte_memarea *ma, size_t size, uint32_t cookie) { @@ -206,6 +216,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 && priv->init.bak_memarea != NULL) + ptr = memarea_alloc_backup(priv, size, cookie); if (unlikely(ptr == NULL)) priv->alloc_fails++; memarea_unlock(priv); @@ -264,6 +276,12 @@ rte_memarea_update_refcnt(struct rte_memarea *ma, void *ptr, int16_t value) struct memarea_private *priv = ma->private_data; memarea_lock(priv); + if (ptr < priv->area_addr || ptr > priv->top_addr) { + rte_memarea_update_refcnt(priv->init.bak_memarea, ptr, value); + memarea_unlock(priv); + return; + } + 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", @@ -274,6 +292,7 @@ rte_memarea_update_refcnt(struct rte_memarea *ma, void *ptr, int16_t value) memarea_unlock(priv); return; } + elem->refcnt += value; if (elem->refcnt == 0) memarea_free_elem(priv, elem); @@ -351,10 +370,13 @@ rte_memarea_dump(struct rte_memarea *ma, FILE *f, bool dump_all) fprintf(f, " source-user-memarea: %s\n", memarea_name(priv->init.user_memarea)); fprintf(f, " total-size: 0x%lx\n", priv->init.total_sz); fprintf(f, " mt-safe: %s\n", priv->init.mt_safe ? "yes" : "no"); + if (priv->init.bak_memarea) + fprintf(f, " backup-memarea-name: %s\n", memarea_name(priv->init.bak_memarea)); fprintf(f, " total-regions: %u\n", memarea_elem_list_num(priv)); fprintf(f, " total-free-regions: %u\n", memarea_free_list_num(priv)); fprintf(f, " alloc_fails: %" PRIu64 "\n", priv->alloc_fails); fprintf(f, " refcnt_check_fails: %" PRIu64 "\n", priv->refcnt_check_fails); + fprintf(f, " backup_alloc_fails: %" PRIu64 "\n", priv->bak_alloc_fails); if (dump_all) memarea_dump_all(priv, f); memarea_unlock(priv); diff --git a/lib/memarea/rte_memarea.h b/lib/memarea/rte_memarea.h index d1383f18c3..a467627523 100644 --- a/lib/memarea/rte_memarea.h +++ b/lib/memarea/rte_memarea.h @@ -41,6 +41,8 @@ * 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 object. + * f) It provides backup memory mechanism, the memarea object could use + * another memarea object as a backup. */ #include @@ -91,6 +93,10 @@ struct rte_memarea_param { */ struct rte_memarea *user_memarea; }; + /** Backup memarea, which is used to handle the scenario where the + * current memarea object allocation failure. + */ + struct rte_memarea *bak_memarea; }; /** -- 2.17.1