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 C5EAAA0542; Wed, 5 Oct 2022 05:39:38 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id A2C5F42B7A; Wed, 5 Oct 2022 05:39:10 +0200 (CEST) Received: from out162-62-58-211.mail.qq.com (out162-62-58-211.mail.qq.com [162.62.58.211]) by mails.dpdk.org (Postfix) with ESMTP id ED41A40A7D for ; Wed, 5 Oct 2022 05:39:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1664941144; bh=LMmxCGlG6l0rZDAWe4UHhtK6mISXvZkGWi0AQ2eGU3U=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=OcJTYmOzrFz3dWiGBcagzyos5UUT4Gna6Jx5TuSpWJCmbLBQkIXmF/YpDhczXmtqh w8rM1OXORFGGyhb80jESv6pxxkJdHZoXpPuhUE4DY5dZVFQAeJotiPZftJWIYpFCol WaXL8QBLw9FcgT4r0qi5XDSjpqs0BnfLcKPrNB4o= Received: from qq.com ([120.229.93.179]) by newxmesmtplogicsvrszc13.qq.com (NewEsmtp) with SMTP id 9B40FA58; Wed, 05 Oct 2022 11:38:52 +0800 X-QQ-mid: xmsmtpt1664941143tbaqvahvr Message-ID: X-QQ-XMAILINFO: MBA0Q7b4Uo1hNHCO9UBWqB2oyxElrd/xviCxZ0t/9nniM87/xBy4WZBM8ppIH3 6rI2RjBcXBxmQ6Kb62nEisLkysmSBg23LnZbnhgBoTjkZ2nMAYKgCbGj4fUMzi2dISR0/+1CUXqP Cmt4N2EaMvW7k6TU+3HB1PB7ZBjyzf8V6EEDswQl+3EDyjfLo0F4X/H89DOfdPmztvwGsdDqYsiL cmYVqRE1BvWvL+vHv1+edbNxTNeEYntL2HFZ2RoaQxL7KMcJ19ue8PBlZ/dL9K2xAWZcEcyW14Ro Rd0tRACv1QVq5cdDuizGVXLVx5f29CJ4oHDvW5n4YOKKYerxd5hg/zLz6Ncb/hbRT9pkbDIVPBd+ eUw8JvF3A8xPS2Fu1ZLBlh42IaLZhfblSJdyC/2v4VjwmiK7WD1lEwfp30J13xbZMTfDwdPMHalI oQQKEtHexuWz5kjSUJMeAxpFq3KQE//yrT49LSHisF63uO/cvY6NVOtO6nZc2FGWAtY4peYVUwbL pHFkmqAek82QLurGhhhYFeHn9UoMyIB8OQeeNh3AZBqCiKwUUf5o26CVR4jqp7xzXphawXSsJCpl yPW065djggdQyzHY3lWYkaJpT0AIvCte9jnZ9blMv8H/0ZJMfka5J3rq/TGOFLXNDwPioszNexeD anifJnnq/KI5oUlqeiYGIWKLY1qxhKRNRbLm7uCHgnYzKbeYCqLn4l9g02ZcpdadCvZC+vU+6W84 uc7G5ESLdyGcChnZc0SVHk+TBQ+lZJIt2a3xUgUhlhn8KqWMVexixP2qZRwaF+SY65K09gh+a7CX sJj7xynGtrX12WOIje+WQSRpWUoPN4Okxd5WCTrBlJU+LCHE0Doc6xQ1yM12oyR+HAINhoRHX2zd Ch8Jrw7Fb1wz6vxhBK6SGg0zwOtqZRND8xsTMzplAajxyGOdvVzlSLKUQBx2IGnSLiNkZHnhtpnX kFXi+jR80fNI57QBMqdWmkTtnwqCW14Yy4mMaUf3KE7uvW8b7E8QnpsNeSQk5hNKUGJ6n2LjKx0r AI7DW2Yg== From: datshan To: david.marchand@redhat.com, mb@smartsharesystems.com, anatoly.burakov@intel.com, dmitry.kozliuk@gmail.com, jerinjacobk@gmail.com Cc: thomas@monjalon.net, dev@dpdk.org, Chengwen Feng Subject: [PATCH v4 07/10] memarea: support backup memory mechanism Date: Wed, 5 Oct 2022 11:38:45 +0800 X-OQ-MSGID: <20221005033848.2241-8-datshan@qq.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221005033848.2241-1-datshan@qq.com> References: <20220721044648.6817-1-fengchengwen@huawei.com> <20221005033848.2241-1-datshan@qq.com> 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 From: Chengwen Feng 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; }; /** -- 2.34.1