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 90958A0542; Wed, 5 Oct 2022 06:10:43 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id B05D542B82; Wed, 5 Oct 2022 06:10:11 +0200 (CEST) Received: from out203-205-221-242.mail.qq.com (out203-205-221-242.mail.qq.com [203.205.221.242]) by mails.dpdk.org (Postfix) with ESMTP id 672CC42B7B for ; Wed, 5 Oct 2022 06:10:09 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1664943007; bh=iFfQNb9bU7wXQev4mmnqW4n48Bba+VmYFGn37TYZ/jY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=csmAdfAhRuPIh01onNMo6MBN3mxzMPzFLr6ZAZlcK+3ItS/278jy3SRqnX+zBL+Kv JbxVUAX2r5ASqpnuCfkUwoUCbonvuOAfCBuUPWm19Zr4YY7IVwsHWIwzPgf0N9bdog Q//i+1Gw0MG+xlRGohlewL38h0dcZz/iB+khBLK8= Received: from qq.com ([120.229.93.179]) by newxmesmtplogicsvrsza31.qq.com (NewEsmtp) with SMTP id 27598E97; Wed, 05 Oct 2022 12:09:53 +0800 X-QQ-mid: xmsmtpt1664943006tm17swfej Message-ID: X-QQ-XMAILINFO: MGSlRwRrdVfImC/86xI5e+cn/NEdcA2RP/Hscp5pboW5yyk3m4MeSUpo1Dfnsr szFcorIuf09BW7dOc9nT84g2vG36slR4HDSv9ZwMn36akNq/8QIDs9t2NG5e4MR4crxT+meEfcX7 UPes8ga+S06KVAj6hbXhwSpNNs1SuAPZJmeAXr5iZ/oMrfk24sQdh/2xGqDX2+iKEDpXusPhoUc7 XD4Gd+1jZ5HzSfoPzK4y2qZhO42Q/tYycFn+4UkA3Jt2F3Q/2LyX+KKGIsTtnTESPdIqHZtv2x9g kW/bRBF3Z6nYbjfoHbwhIeLNeAWRUUuIijhDwtGNCgAQU9gDqbNLYLvnOErP/X6c6hMT9YGMn2/A cixubLkjoDTKQsF1HvKu9iPHe+L/hOAi4UuCgCT/8IFr80HYrjch5gmbqam5v3VGk8R+NAABu77C +GllXvRIAZo7j4oqHEXecvkL3sH/bufflz2cpXvW3ZX0730GBwGpavyB4sIpo1wV7rUZ2ZZsCeTy CvTWDD4Yzf5qkItaubxRjbuNrRRAsNxFXk4EGJDaJvAv8DSGfpKmh4BJlCbF6gGHY6aOqr//yZ6x TINLRU46N13o8D1QHK5YH+3UtO/IbIGQonoO83//AtMIibG2IRGgyio6mlTHquVDJzvRIrztX/sT roEPmnTn5kF+U5COY36oVfE1mhB+FPxjhzg5DmqT3ZjKrIbkQZwzNss6zesbSCPkDvZPYUB0veSL SKlRYArEM18B05alPbv79sKCSUj5FOblhIDTKdv474MNcIV5zx/obFVGZMV3E66VX1E9CADqwtjy cS/ZASRwWgIqTS6K5LiYFeS+nBXkYK2TRkwv4Z12OySHHkOWHDWvcU0av2wgm4QLMQ9PgPxsEiEZ n437ojjY+mdoP75hJU0CSjbGSX6SjKLKE4PEjuf3eHnrxG8alTmSy1wdCyVyCarjaN54dBsbPhMc xhJi8Y/6ZoZBaCKJkXrllz25iuuagr8fOGuaYJHzczJEZ9lRYFEGuVIQ2qBYWlwv+eKiDo9Czeiz u3hKHpOEmM23/zxammohI5wC5C/dF7N2HVYJxWPjYxgnUXch0z5k40471hWLQ= 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 v5 09/10] memarea: detect memory corruption based on magic Date: Wed, 5 Oct 2022 12:09:51 +0800 X-OQ-MSGID: <20221005040952.8166-10-datshan@qq.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221005040952.8166-1-datshan@qq.com> References: <20220721044648.6817-1-fengchengwen@huawei.com> <20221005040952.8166-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 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..6290e3449a 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 check fail!\n", elem->magic); + break; + } fprintf(f, " size: 0x%zx cookie: 0x%x refcnt: %d\n", elem->size, elem->cookie, elem->refcnt); + } } int -- 2.34.1