From patchwork Fri Oct 18 03:53:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zheng Yejian X-Patchwork-Id: 13841080 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 0BFBBD3C548 for ; Fri, 18 Oct 2024 03:53:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9151B6B0082; Thu, 17 Oct 2024 23:53:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 89C716B0083; Thu, 17 Oct 2024 23:53:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 747B46B0085; Thu, 17 Oct 2024 23:53:16 -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 55E2E6B0082 for ; Thu, 17 Oct 2024 23:53:16 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id A4E95A043F for ; Fri, 18 Oct 2024 03:52:54 +0000 (UTC) X-FDA: 82685352372.27.625DF09 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) by imf04.hostedemail.com (Postfix) with ESMTP id 804EC40004 for ; Fri, 18 Oct 2024 03:52:57 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; spf=pass (imf04.hostedemail.com: domain of zhengyejian@huaweicloud.com designates 45.249.212.56 as permitted sender) smtp.mailfrom=zhengyejian@huaweicloud.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729223448; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references; bh=QtzMbCZzZx/gdXwDPZNwWi3gVBex/YGyOBek+2ivCyc=; b=gDMbnmCMDx9Kb2xbC9qDqj5rg4Tu3oZN7WdxYMNXrAeGahZzoVq2C+MSxrIzNUGCgMMtjt YCbPKtsR0jg6YmdZpgzfJVPgupfT9z4HN1DhI8cOcYtC9T7YHCqavNT8dU6gfFIcqH8XQQ 4AZbXaOMDe7u/Eck9QFNKIT8t2c32PU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729223448; a=rsa-sha256; cv=none; b=aSU4hgUKIKRBgocU3E1m/ZITZv/TsFXCtBlD2uY+MGb6/k3SMcyAL8MARg/Qv65SCwlxHe b7bG4Mo+YZYOp/2FQ4CKWdFTU1BVx3k65eO5oOsLN4YHyOXRB6iILa/M/txRfK+qsEOvpj ofoOrrUnvr2GHpnegTXtngycHDMY3F4= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; spf=pass (imf04.hostedemail.com: domain of zhengyejian@huaweicloud.com designates 45.249.212.56 as permitted sender) smtp.mailfrom=zhengyejian@huaweicloud.com; dmarc=none Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4XV9lN1Hdpz4f3jXv for ; Fri, 18 Oct 2024 11:52:48 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 60E2F1A06D7 for ; Fri, 18 Oct 2024 11:53:05 +0800 (CST) Received: from localhost.localdomain (unknown [10.67.175.61]) by APP4 (Coremail) with SMTP id gCh0CgDnacac2xFn3SagEQ--.674S2; Fri, 18 Oct 2024 11:53:05 +0800 (CST) From: Zheng Yejian To: sj@kernel.org, akpm@linux-foundation.org, sieberf@amazon.com, shakeel.butt@linux.dev, foersleo@amazon.de Cc: damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, zhengyejian@huaweicloud.com Subject: [PATCH] mm/damon/vaddr: Fix issue in damon_va_evenly_split_region() Date: Fri, 18 Oct 2024 11:53:04 +0800 Message-Id: <20241018035304.1050135-1-zhengyejian@huaweicloud.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-CM-TRANSID: gCh0CgDnacac2xFn3SagEQ--.674S2 X-Coremail-Antispam: 1UD129KBjvJXoWxXw4UZrWruFWfXF4DGF4DJwb_yoWrWF1Up3 93WrWfKrWDtFy2vw13Zan8ZrnxArW8tF1UCryfJF1qyFnYg34DtFWUJ345tFyrCayxZ34f Zw10vrW3GFZ8Ca7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBI14x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26F1j6w1UM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4U JVWxJr1l84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gc CE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E 2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJV W8JwACjcxG0xvY0x0EwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1lFIxGxcIEc7CjxVA2 Y2ka0xkIwI1lc7CjxVAaw2AFwI0_Jw0_GFylc7CjxVAKzI0EY4vE52x082I5MxAIw28Icx kI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2Iq xVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42 IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY 6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aV CY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7VUj8pnPUUUUU== X-CM-SenderInfo: x2kh0w51hmxt3q6k3tpzhluzxrxghudrp/ X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 804EC40004 X-Stat-Signature: kehjckeuwacdsuzyu77twe4xmii5z3zs X-HE-Tag: 1729223577-364362 X-HE-Meta: U2FsdGVkX1/JHQt/cfjjD/71yamVp8N11wjxhBgdbfOmGSNWygJA+X4zF5jc6uSu/iyb4l/zJfuREJI9aY44qs5sq3Hf2orlksHxneWs1/9j4X06K+c6q3zGDIDTp0HqoTm8HWRv74qG+Co38Q2LAdSIEcto/WBbr/0lwyCRhE5XQ0DBRCKwBXsmeI+saHEniJJHaxME9ghFdpHMDSrTQkvRlplFr0nYXJjYkf7wNz3mAtriMWw5B9W3f5meA44TxuO075HEVCLtXmMntF4IbhBPr/Tic31b6AkQZFlvZb+exwyj49+JIyB+vQUL8hISC7DepH+3zrouwSU3evbfUOq0ql7WhVSaxL2Hq8wkqK0O1CGUuyF+Sn8eUEKFJnXZ4PDgtJQSoUfoyfikc+zZtzLLn8u8N0v214KIHZ+lgkx6Uu4+xYFFKSn8n0x5Vzu3d8DG+gmJ5Gw7ZRpvl6JQ+KE0H5m0yfMGdVoma8o6QaM7cPLOFiDUnbqcmqKZxjVhWygBpk6buXgCcGHDbzYtiiZ+DTk6miA5KiyY78a9xubrlBg3dROt/YwxWVnX6IWQBF42K+wEOmXexhRIklC7KAbQh87UYleB2c9a884F5Jb4Qy+J5plzfiq3OGA1Hd4g6nYjCb9idi01RIAQuTH9TQCa15inqVaqou3/aKBeLNvJ4oU982ZGyIJw3uRHcnNOs9Yu2YnUIL3kxPxc9+k9xqW7laSnYJiHIM5U/mFbcKTM16Y3azeVmKIgtTKUu3moh3OXQTMoENWB1kvnJ3jOrquszWXXXrT+QfIYyXhcmo/ZMFDlmYasqRfngi0oN1Bgz8QSGCtV1hn5gmOtunlwCH0hUD0EaFHWBFgYQmPz9QSGxrXUPnF8iDcgF5W3zoeMtVEUFKRe93mvVMhx31aUtiZVnHLS1RLtFMaeWVQWc6r4CSa6xhluTpZWrEdadU1md/DVbslFr+Mr1ljfltw PJ4BUqri p7VfibPO1tb34eVXrh7fvZASqO3hqqWFhi1W5wUdXIFgf8HP5mtYlGpAxamy+w8KYRILOXIJaL6lGNi98ZsHupSSwmjIA/6DEgrbUrTMj7vmKJcl7XW6t5hcq0lNps2R5vF1AGsjpEDPrvCKQFPB0STL/ep994Y7zqR4392WeRjihOr5WkT7SZOtCvQ== 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: List-Subscribe: List-Unsubscribe: According to the logic of damon_va_evenly_split_region(), currently at least following split cases would not meet the expectation: Suppose DAMON_MIN_REGION=0x1000, Case1: Split [0x0, 0x1100) into 1 pieces, then the result would be acutually [0x0, 0x1000), but NOT the expected [0x0, 0x1100) !!! Case2: Split [0x0, 0x3000) into 2 pieces, then the result would be acutually 3 regions: [0x0, 0x1000), [0x1000, 0x2000), [0x2000, 0x3000) but NOT the expected 2 regions: [0x0, 0x1000), [0x1000, 0x3000) !!! The root cause is that when calculating size of each split piece in damon_va_evenly_split_region(): `sz_piece = ALIGN_DOWN(sz_orig / nr_pieces, DAMON_MIN_REGION);` both the dividing and the ALIGN_DOWN may cause loss of precision, then each time split one piece of size 'sz_piece' from origin 'start' to 'end' would cause: 1. For the above Case1, the 'end' value of the split 1 piece is aligned but not updated!!! 2. For the above Case2, more pieces are split out than expected!!! To fix it, in this patch: - As for the expect to split 1 piece, just return 0; - Count for each piece split and make sure no more than 'nr_pieces'; - Add above two cases into damon_test_split_evenly(). BTW, currently when running kunit test, DAMON_MIN_REGION is redefined as 1, then above ALIGN_DOWN cases may not be test, since every int value is ALIGN-ed to 1. After this patch, damon-operations test passed: # ./tools/testing/kunit/kunit.py run damon-operations [...] ============== damon-operations (6 subtests) =============== [PASSED] damon_test_three_regions_in_vmas [PASSED] damon_test_apply_three_regions1 [PASSED] damon_test_apply_three_regions2 [PASSED] damon_test_apply_three_regions3 [PASSED] damon_test_apply_three_regions4 [PASSED] damon_test_split_evenly ================ [PASSED] damon-operations ================= Fixes: 3f49584b262c ("mm/damon: implement primitives for the virtual memory address spaces") Signed-off-by: Zheng Yejian --- mm/damon/tests/vaddr-kunit.h | 2 ++ mm/damon/vaddr.c | 13 +++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/mm/damon/tests/vaddr-kunit.h b/mm/damon/tests/vaddr-kunit.h index a339d117150f..b9a03e4e29e5 100644 --- a/mm/damon/tests/vaddr-kunit.h +++ b/mm/damon/tests/vaddr-kunit.h @@ -300,6 +300,8 @@ static void damon_test_split_evenly(struct kunit *test) damon_test_split_evenly_fail(test, 0, 100, 0); damon_test_split_evenly_succ(test, 0, 100, 10); damon_test_split_evenly_succ(test, 5, 59, 5); + damon_test_split_evenly_succ(test, 4, 6, 1); + damon_test_split_evenly_succ(test, 0, 3, 2); damon_test_split_evenly_fail(test, 5, 6, 2); } diff --git a/mm/damon/vaddr.c b/mm/damon/vaddr.c index 08cfd22b5249..1f3cebd20829 100644 --- a/mm/damon/vaddr.c +++ b/mm/damon/vaddr.c @@ -67,10 +67,14 @@ static int damon_va_evenly_split_region(struct damon_target *t, unsigned long sz_orig, sz_piece, orig_end; struct damon_region *n = NULL, *next; unsigned long start; + int i; if (!r || !nr_pieces) return -EINVAL; + if (nr_pieces == 1) + return 0; + orig_end = r->ar.end; sz_orig = damon_sz_region(r); sz_piece = ALIGN_DOWN(sz_orig / nr_pieces, DAMON_MIN_REGION); @@ -79,9 +83,11 @@ static int damon_va_evenly_split_region(struct damon_target *t, return -EINVAL; r->ar.end = r->ar.start + sz_piece; + /* origin region will be updated as the first one after splitting */ + i = 1; + n = r; next = damon_next_region(r); - for (start = r->ar.end; start + sz_piece <= orig_end; - start += sz_piece) { + for (start = r->ar.end; i < nr_pieces; start += sz_piece, i++) { n = damon_new_region(start, start + sz_piece); if (!n) return -ENOMEM; @@ -89,8 +95,7 @@ static int damon_va_evenly_split_region(struct damon_target *t, r = n; } /* complement last region for possible rounding error */ - if (n) - n->ar.end = orig_end; + n->ar.end = orig_end; return 0; }