From patchwork Mon Sep 18 12:09:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinjie Ruan X-Patchwork-Id: 13389537 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 24407CD37B0 for ; Mon, 18 Sep 2023 12:10:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B19376B0329; Mon, 18 Sep 2023 08:10:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AC7046B032B; Mon, 18 Sep 2023 08:10:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9DF346B032D; Mon, 18 Sep 2023 08:10:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 8F1586B0329 for ; Mon, 18 Sep 2023 08:10:36 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 68D8A16033A for ; Mon, 18 Sep 2023 12:10:36 +0000 (UTC) X-FDA: 81249601272.22.A4110CF Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf26.hostedemail.com (Postfix) with ESMTP id D541114000A for ; Mon, 18 Sep 2023 12:10:32 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf26.hostedemail.com: domain of ruanjinjie@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=ruanjinjie@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695039033; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SQj0XjZlvDVuTPBNiLfEoMO3isjlh9uqOVrXvHZdD30=; b=vPp7unbGLkD1NiqYX6n0+mc78dcvFgePv3vvytCzit5GMUYYwQ4uDQQCNNM0scPYhJ3MGz A+lZ3joRgs7yPesSZIDZapT5C5rEpYGaOEplyn7W+sEZUUYBhqyKX0ixgIiWGPng7opg8t Th87167ce1jve9LW66UwXE5Z23vXJjg= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf26.hostedemail.com: domain of ruanjinjie@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=ruanjinjie@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695039033; a=rsa-sha256; cv=none; b=H3ElbfRSk1U9tZZaBbS6/xsJk2f1J5qR+wCPXekdIhiGja/xXkdO9lHBn31mVObvtJE3B4 YvWJPGKL22gU0tUckhpewTynEwggC46T1LKcs3oLUqI2zMnBO4rpd8GEEew7RFybGXiOia V5V8wV/2vs7Zyt5tkt45k4+SKebRxg4= Received: from kwepemi500008.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Rq3RV4p9WztSbb; Mon, 18 Sep 2023 20:06:14 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemi500008.china.huawei.com (7.221.188.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.31; Mon, 18 Sep 2023 20:10:26 +0800 From: Jinjie Ruan To: , , , , , , , , CC: Subject: [PATCH v3 1/2] mm/damon/core-test: Fix memory leak in damon_new_region() Date: Mon, 18 Sep 2023 20:09:50 +0800 Message-ID: <20230918120951.2230468-2-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230918120951.2230468-1-ruanjinjie@huawei.com> References: <20230918120951.2230468-1-ruanjinjie@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.90.53.73] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemi500008.china.huawei.com (7.221.188.139) X-CFilter-Loop: Reflected X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: D541114000A X-Stat-Signature: dtwuyscda4ks7cwkpjf6zhnp7b1yefrb X-HE-Tag: 1695039032-907153 X-HE-Meta: U2FsdGVkX18PTbxLgH3jNBq6KqV5Qvduh0S5s6VHLle/xH///Qhn5+1uw3D/jsUni/u5x8BNl3PxOF3xtv8Akui3SchCv6VdXAhqaVMQhtl+AtlRsF3h5yovwpRDXUM5uRS1oHnOrA+3pvbeB1+uXpLQC4MctzZMAMUJOVmtBtB5X+S37bB5886uuvJUjQM+6PojVbWF7nJaARlVKhFC3cKn2Q7Y0gL52a1pCKJf+0M3p0iipu/2JmuoQRN7EBX9jWcKeUsY/c/DuFg+bno5CvaQxyGwq6bZHtJDaizge3gCfCigOPzyHNipd9GKRcXbx9sOYc7Apgi7+zj2H1PWRcmtwHGvJ6e1vHEvGnXofXXrm0S3gR7KkuDWCsyxpCRQrCLlm6EZXNk/lzOqtWqX7MBnX8giWPmZ3g38aGH2MVlswLC5o7QWZ6sJ2d8lVFN5dUmUMJ0OMDdf2wIkqYAUCnsJ7oVhh1f7KT+RaqkYrzH3qCpIhTWeHxIm3VOSzOshzup/vZub4B7xTEGB/yEQbpn5TFLiWeA0bLT5FQcNcHOZvjMGVEHKBZwdcK3FFNzhVgaGNW9iQ9jVsBdN6GeIFFqJoiOclUaALQIoCnH6ZM2lkLuJG2Xd/4NwaVZjx8Rw1qmD6NvutW9KsWU9xL07DUKjn7qvrDpGr8Nq6BAzxIaXSrsF27E+KwMfXJql8+Jqm+ofmGdFfqzvnoy+h3e61dUV+SSjRPYASmHunYgOgzziKNrI47aghOTJqXr0GZpFqu13ippjsS1KizhcV6SUzKDDzj2u1we+RDeRJjmBnGFs7TLC0gQ3rDaNyjobf5+WA3423Ro0BjAd1TQ3P0e1v6TYUvZzD/hCqu1jPRmSu5/pxCwWaVNKbLNa2hXBnVZi75PvooGz+VW91OxJC2ygfn2RGZBUWvs6jpOZ0HkAkI2BbpE1l0q2w1jqlkU9DUjBmo4DBEKOvXk8rLzKaOW bafOMwi1 fuSMIxFz8xZKd272ZMWnczr3YC+u/k7rnWCGfd4bH6512H3cc3Uf2pHVtPrU62PZe8G6WyXGu9/+pCvXo0PGBjZHyW2NvG0bt/e77FdBu6AxNMlE26HBXf8w+3nNKclf7wX2NTm+2L0oY6+e7BBQRWjAnDNsJuWbDGq9ERU5vaVeqpkCouG12Xcy6NrcgjDFtTb6kDO+qYdGtnrgVfk7ksT2Qr0Bs5UGqySiujIyc7iEusBpsZmGsnGZCHGtn5QLT43xQ X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: When CONFIG_DAMON_KUNIT_TEST=y and making CONFIG_DEBUG_KMEMLEAK=y and CONFIG_DEBUG_KMEMLEAK_AUTO_SCAN=y, the below memory leak is detected. The damon_region which is allocated by kmem_cache_alloc() in damon_new_region() in damon_test_regions() and damon_test_update_monitoring_result() are not freed. So for damon_test_regions(), replace damon_del_region() call with damon_destroy_region() so that it calls both damon_del_region() and damon_free_region(), the latter will free the damon_region. For damon_test_update_monitoring_result(), call damon_free_region() to free it. After applying this patch, the following memory leak is never detected. unreferenced object 0xffff2b49c3edc000 (size 56): comm "kunit_try_catch", pid 338, jiffies 4294895280 (age 557.084s) hex dump (first 32 bytes): 01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 49 2b ff ff ............I+.. backtrace: [<0000000088e71769>] slab_post_alloc_hook+0xb8/0x368 [<00000000b528f67c>] kmem_cache_alloc+0x168/0x284 [<000000008603f022>] damon_new_region+0x28/0x54 [<00000000a3b8c64e>] damon_test_regions+0x38/0x270 [<00000000559c4801>] kunit_try_run_case+0x50/0xac [<000000003932ed49>] kunit_generic_run_threadfn_adapter+0x20/0x2c [<000000003c3e9211>] kthread+0x124/0x130 [<0000000028f85bdd>] ret_from_fork+0x10/0x20 unreferenced object 0xffff2b49c5b20000 (size 56): comm "kunit_try_catch", pid 354, jiffies 4294895304 (age 556.988s) hex dump (first 32 bytes): 03 00 00 00 00 00 00 00 07 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 96 00 00 00 49 2b ff ff ............I+.. backtrace: [<0000000088e71769>] slab_post_alloc_hook+0xb8/0x368 [<00000000b528f67c>] kmem_cache_alloc+0x168/0x284 [<000000008603f022>] damon_new_region+0x28/0x54 [<00000000ca019f80>] damon_test_update_monitoring_result+0x18/0x34 [<00000000559c4801>] kunit_try_run_case+0x50/0xac [<000000003932ed49>] kunit_generic_run_threadfn_adapter+0x20/0x2c [<000000003c3e9211>] kthread+0x124/0x130 [<0000000028f85bdd>] ret_from_fork+0x10/0x20 Fixes: 17ccae8bb5c9 ("mm/damon: add kunit tests") Fixes: f4c978b6594b ("mm/damon/core-test: add a test for damon_update_monitoring_results()") Signed-off-by: Jinjie Ruan Reviewed-by: SeongJae Park --- v3: - Update the commit message to be more accurate. - Add Reviewed-by. v2: - Replace the damon_del_region() with damon_destroy_region() rather than calling damon_free_region(). - Update the commit message. --- mm/damon/core-test.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mm/damon/core-test.h b/mm/damon/core-test.h index 6cc8b245586d..28d24e698318 100644 --- a/mm/damon/core-test.h +++ b/mm/damon/core-test.h @@ -30,7 +30,7 @@ static void damon_test_regions(struct kunit *test) damon_add_region(r, t); KUNIT_EXPECT_EQ(test, 1u, damon_nr_regions(t)); - damon_del_region(r, t); + damon_destroy_region(r, t); KUNIT_EXPECT_EQ(test, 0u, damon_nr_regions(t)); damon_free_target(t); @@ -316,6 +316,8 @@ static void damon_test_update_monitoring_result(struct kunit *test) damon_update_monitoring_result(r, &old_attrs, &new_attrs); KUNIT_EXPECT_EQ(test, r->nr_accesses, 150); KUNIT_EXPECT_EQ(test, r->age, 20); + + damon_free_region(r); } static void damon_test_set_attrs(struct kunit *test)