From patchwork Sun Sep 3 15:13:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 13373274 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 28FD7CA0FE3 for ; Sun, 3 Sep 2023 15:13:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2B7CE280014; Sun, 3 Sep 2023 11:13:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 26704280012; Sun, 3 Sep 2023 11:13:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E1D8280014; Sun, 3 Sep 2023 11:13:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id EF380280012 for ; Sun, 3 Sep 2023 11:13:43 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id C0D41C04B9 for ; Sun, 3 Sep 2023 15:13:43 +0000 (UTC) X-FDA: 81195630726.07.7ACB72C Received: from mail-io1-f51.google.com (mail-io1-f51.google.com [209.85.166.51]) by imf28.hostedemail.com (Postfix) with ESMTP id E4F2DC000D for ; Sun, 3 Sep 2023 15:13:41 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=joelfernandes.org header.s=google header.b=PJR6zLBt; spf=pass (imf28.hostedemail.com: domain of joel@joelfernandes.org designates 209.85.166.51 as permitted sender) smtp.mailfrom=joel@joelfernandes.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1693754021; 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:in-reply-to:references:references:dkim-signature; bh=+BMtnhYdeu/zB56jX4tl+3iW5OG7Lep/G1Bzlyk4Cto=; b=jVn1v748hT4huRRLS+3f1jji0E/1lPT9ZQoYrNQwSS83vmR1Ix+4VOB8iq8+YqVH0LgdoC mxfastjWV3lruBUnD6WLy8skJq++ucoxKgbhwItANK5XDwt18qzDkEc9djpw9v1K2y60gd GLvPhKs3Mt5/lKIqshO3SOtk2qjvs+8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693754021; a=rsa-sha256; cv=none; b=1R9raHf6fqMdZqqMvmlh7Xcn141DpparXOf1X2Vwzrusg5DQDw+bWg8mIEhh0KeFFXCaJz goNtVK9YwtdeDbrHU7S7FyiueklgACZeXH7uGCJPHDyUr20V5tn/+ywbdvM5xZFJUg0xBC Qx0DyoMJ9wzRsIITN5DaQdsXmCA3JMs= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=joelfernandes.org header.s=google header.b=PJR6zLBt; spf=pass (imf28.hostedemail.com: domain of joel@joelfernandes.org designates 209.85.166.51 as permitted sender) smtp.mailfrom=joel@joelfernandes.org; dmarc=none Received: by mail-io1-f51.google.com with SMTP id ca18e2360f4ac-77a62a84855so30043839f.1 for ; Sun, 03 Sep 2023 08:13:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1693754021; x=1694358821; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+BMtnhYdeu/zB56jX4tl+3iW5OG7Lep/G1Bzlyk4Cto=; b=PJR6zLBtHETgcjJCDh6NMoOddO6nOkjrwFLN5uWok3x+pdeSLVty1veSD9ahbH+qHG qtd13eV9LlsWgs4KQI9+UajAV0vHoHCI9GsV2/2R4KYn1ItOS7ZkI5ZsI6IlInKW8Rhk 6cO++8A82BAekG34NwNroHPnimRkJc15RGBls= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693754021; x=1694358821; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+BMtnhYdeu/zB56jX4tl+3iW5OG7Lep/G1Bzlyk4Cto=; b=hWsADIVeRD26gccMFMBiuCyLX7FZfNoY96+ePQ9+rNH0bB0h8argmTzXMsqg8pWjor 1ybHoeMjyPE5mBdXJzl+5k4UdQBthrZuJH2vEcO14lCybWkaqLChrbbpzjfUzuj9z9Hv s96fD0A+Df6ofRkc+qBdykRzFnNqf9Nttf7LCpC7skW4KrcVXRw0F/g+NMJfu73N2x+F 3bm5hudi/XuE3pq20WtQaus3F8ivZSmNEQyn0AYsye62Yhdc8sjDIsmJ0oFGkyRm4Av9 8fScTH1txrRI/hC3E1Ik50U69iqR9yA4+8gUrDpKpg7SBV0HXmgnD0sTNr3b5rV4Cu3e d9VQ== X-Gm-Message-State: AOJu0YyO9WLhm8+AmVB0Y5nrq3mjsgF5/LF20XxUJ3IfoR/niSr+Cm7X QOTTFggYnarYTq2Gf9My8aRu0A== X-Google-Smtp-Source: AGHT+IGoiNLSWXDDcRwa+CUKmoRj1KyKVnC5w9SpA0dWyRbwniu1q3CeYE3pp5OaUl5HGdBBsO8atw== X-Received: by 2002:a6b:f104:0:b0:795:1a7c:486f with SMTP id e4-20020a6bf104000000b007951a7c486fmr8841640iog.14.1693754021116; Sun, 03 Sep 2023 08:13:41 -0700 (PDT) Received: from joelboxx5.corp.google.com (156.190.123.34.bc.googleusercontent.com. [34.123.190.156]) by smtp.gmail.com with ESMTPSA id f5-20020a6b5105000000b007835a305f61sm2605897iob.36.2023.09.03.08.13.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Sep 2023 08:13:40 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , Lorenzo Stoakes , linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Shuah Khan , Vlastimil Babka , Michal Hocko , Linus Torvalds , Kirill A Shutemov , "Liam R. Howlett" , "Paul E. McKenney" , Suren Baghdasaryan , Kalesh Singh , Lokesh Gidra Subject: [PATCH v6 5/7] selftests: mm: Add a test for remapping to area immediately after existing mapping Date: Sun, 3 Sep 2023 15:13:26 +0000 Message-ID: <20230903151328.2981432-6-joel@joelfernandes.org> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog In-Reply-To: <20230903151328.2981432-1-joel@joelfernandes.org> References: <20230903151328.2981432-1-joel@joelfernandes.org> MIME-Version: 1.0 X-Stat-Signature: 9uibotwr164yf1ikuzbqcnri3ckqotxx X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: E4F2DC000D X-Rspam-User: X-HE-Tag: 1693754021-110043 X-HE-Meta: U2FsdGVkX19uLOGsU52Ny0ynH9SnYsO9+cb8hjwLDLXVtvvNfvGM+pwHrQMYbDoiBP8ox2G8JVzT6RwBdMNcPflDc8HAQpPm8CCTf6PJcv4mJcBBmAkOD4bA5OM+5Q1XYRilqvBHW/MkAhtFPp/Y/eXwkALeelac4rfleA1UGHcv4SG3A7tuYaJc1WFfBYq1ePtnSr+2gmD0P98F7iAP3TjbrOulEQzL7FGv76z4WG1qhsAxU6cR1fvGXNiThJCuXiF9ST+vfUTHCk/8IhAt5WdPQZEM6VmVxK/pb71MEjC4UwU6T4GdFi77Ery4uOVaUgSsXvFKx+ll5j1EvZCm3F562KVvmpw6wyJuCSZRIW/nFyqthJQud3ee3h5mLJX1wD/+GEHfBXGI/RS/xkNgpwgXWuaTivPLUJSjluG/HKG3soBqbB/AGxXCkIcrnRUHsAwuUOhme9a2Lan1PDOlhwGL+JGF3l7y3UCHhZJIMyrYxiPWbygTFKTkrX/mp6CZTvCdx4kyJIEx1f1oJ6oOc581WRde0lzFSSAXzyFKbz5ZkCMBtFSoN6YfBG5PV07rv/DrH5ZcJFGMNQiho/WKB0x2tGENMKkFnAqIobW6Q3W17lkfAzQt0fUOs/UUSYHYMFiFVNgnATpGBEUhHQyiFgHw3esObP5eyL12XdOiERXxj5QGfLZU79cl308pxvw1jvH9Rt84mHcmMlY8VTD533ajga1bhdoKzP8yBwVWUjxlRnNwhx9j0AvYIdBrlSCN32v1hII/W49p88W3pNV4F4QprSQTRaDbjcZMGpvWydtuqS82v4qdlVc/hlsnjmNlrLnNQsxueOqstl4F/1NqVPRENCQvJHIjUt0zsN01l155kJqxXqCjYvTVOYciqBwXd+YFjQiI7Ejl/9DSVvWGWNpnJnZjUUrHmEfffJObICeWxS/yAqIpruKfiX7TeaeGweZ+nHDZ1viOFYX9CKt nY/gFf4M nIDvH5SfscOpqbSlAlpIc2pyJPX1XTnDByKN4AugJ5GCneuQy3xPpAooIppKKc2dJ3n1xQDcsmCuvg1Afc4AjFtxM9XOYlBd5eZUIfjZhbthBvejLe+1BRpkhMUWVIIF/pjYZ8W4x3EE9xpOqDXvhq3KgBPdX7oLrKVC1h6ryqycVPOrQ1cUxiZe/HFL+eA7cnz59z+MVMMU+tuIhh1lCfL0eSla5oT21YCIbC0JhouhnM/q7eoiLcwlVd53JkGiBc7PATcWrxpO76eZ+CrF+fugd2nDuN+tTA/yqxTQOJ1vjYJNu+YOZ4Qelle9GP/P7vcw0xmkj+JLh8Iexdmf5uBvTyLCI0JkEAUuRaZP+1itqMAuqQBB4i0LC1SIy30J7n3jildJq2t9KlTlcTma13/Y+tldXbagxEuzsghQNggGyF1FQO7XDZVNaKycurX/DsYjZcJn3ntNrr0HEozPL9HpyRzgfPDsW3YjMEGuAuexoOcV7Ru4XIWs17w== 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: This patch adds support for verifying that we correctly handle the situation where something is already mapped before the destination of the remap. Any realignment of destination address and PMD-copy will destroy that existing mapping. In such cases, we need to avoid doing the optimization. To test this, we map an area called the preamble before the remap region. Then we verify after the mremap operation that this region did not get corrupted. Putting some prints in the kernel, I verified that we optimize correctly in different situations: Optimize when there is alignment and no previous mapping (this is tested by previous patch). can_align_down(old_vma->vm_start=2900000, old_addr=2900000, mask=-2097152): 0 can_align_down(new_vma->vm_start=2f00000, new_addr=2f00000, mask=-2097152): 0 === Starting move_page_tables === Doing PUD move for 2800000 -> 2e00000 of extent=200000 <-- Optimization Doing PUD move for 2a00000 -> 3000000 of extent=200000 Doing PUD move for 2c00000 -> 3200000 of extent=200000 Don't optimize when there is alignment but there is previous mapping (this is tested by this patch). Notice that can_align_down() returns 1 for the destination mapping as we detected there is something there. can_align_down(old_vma->vm_start=2900000, old_addr=2900000, mask=-2097152): 0 can_align_down(new_vma->vm_start=5700000, new_addr=5700000, mask=-2097152): 1 === Starting move_page_tables === Doing move_ptes for 2900000 -> 5700000 of extent=100000 <-- Unoptimized Doing PUD move for 2a00000 -> 5800000 of extent=200000 Doing PUD move for 2c00000 -> 5a00000 of extent=200000 Reviewed-by: Lorenzo Stoakes Signed-off-by: Joel Fernandes (Google) --- tools/testing/selftests/mm/mremap_test.c | 57 +++++++++++++++++++++--- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/mm/mremap_test.c b/tools/testing/selftests/mm/mremap_test.c index 6304eb0947a3..d7366074e2a8 100644 --- a/tools/testing/selftests/mm/mremap_test.c +++ b/tools/testing/selftests/mm/mremap_test.c @@ -29,6 +29,7 @@ struct config { unsigned long long dest_alignment; unsigned long long region_size; int overlapping; + int dest_preamble_size; }; struct test { @@ -283,7 +284,7 @@ static void *get_source_mapping(struct config c) static long long remap_region(struct config c, unsigned int threshold_mb, char pattern_seed) { - void *addr, *src_addr, *dest_addr; + void *addr, *src_addr, *dest_addr, *dest_preamble_addr; unsigned long long i; struct timespec t_start = {0, 0}, t_end = {0, 0}; long long start_ns, end_ns, align_mask, ret, offset; @@ -300,7 +301,7 @@ static long long remap_region(struct config c, unsigned int threshold_mb, goto out; } - /* Set byte pattern */ + /* Set byte pattern for source block. */ srand(pattern_seed); for (i = 0; i < threshold; i++) memset((char *) src_addr + i, (char) rand(), 1); @@ -312,6 +313,9 @@ static long long remap_region(struct config c, unsigned int threshold_mb, addr = (void *) (((unsigned long long) src_addr + c.region_size + offset) & align_mask); + /* Remap after the destination block preamble. */ + addr += c.dest_preamble_size; + /* See comment in get_source_mapping() */ if (!((unsigned long long) addr & c.dest_alignment)) addr = (void *) ((unsigned long long) addr | c.dest_alignment); @@ -327,6 +331,24 @@ static long long remap_region(struct config c, unsigned int threshold_mb, addr += c.dest_alignment; } + if (c.dest_preamble_size) { + dest_preamble_addr = mmap((void *) addr - c.dest_preamble_size, c.dest_preamble_size, + PROT_READ | PROT_WRITE, + MAP_FIXED_NOREPLACE | MAP_ANONYMOUS | MAP_SHARED, + -1, 0); + if (dest_preamble_addr == MAP_FAILED) { + ksft_print_msg("Failed to map dest preamble region: %s\n", + strerror(errno)); + ret = -1; + goto clean_up_src; + } + + /* Set byte pattern for the dest preamble block. */ + srand(pattern_seed); + for (i = 0; i < c.dest_preamble_size; i++) + memset((char *) dest_preamble_addr + i, (char) rand(), 1); + } + clock_gettime(CLOCK_MONOTONIC, &t_start); dest_addr = mremap(src_addr, c.region_size, c.region_size, MREMAP_MAYMOVE|MREMAP_FIXED, (char *) addr); @@ -335,7 +357,7 @@ static long long remap_region(struct config c, unsigned int threshold_mb, if (dest_addr == MAP_FAILED) { ksft_print_msg("mremap failed: %s\n", strerror(errno)); ret = -1; - goto clean_up_src; + goto clean_up_dest_preamble; } /* Verify byte pattern after remapping */ @@ -353,6 +375,23 @@ static long long remap_region(struct config c, unsigned int threshold_mb, } } + /* Verify the dest preamble byte pattern after remapping */ + if (c.dest_preamble_size) { + srand(pattern_seed); + for (i = 0; i < c.dest_preamble_size; i++) { + char c = (char) rand(); + + if (((char *) dest_preamble_addr)[i] != c) { + ksft_print_msg("Preamble data after remap doesn't match at offset %d\n", + i); + ksft_print_msg("Expected: %#x\t Got: %#x\n", c & 0xff, + ((char *) dest_preamble_addr)[i] & 0xff); + ret = -1; + goto clean_up_dest; + } + } + } + start_ns = t_start.tv_sec * NS_PER_SEC + t_start.tv_nsec; end_ns = t_end.tv_sec * NS_PER_SEC + t_end.tv_nsec; ret = end_ns - start_ns; @@ -365,6 +404,9 @@ static long long remap_region(struct config c, unsigned int threshold_mb, */ clean_up_dest: munmap(dest_addr, c.region_size); +clean_up_dest_preamble: + if (c.dest_preamble_size && dest_preamble_addr) + munmap(dest_preamble_addr, c.dest_preamble_size); clean_up_src: munmap(src_addr, c.region_size); out: @@ -440,7 +482,7 @@ static int parse_args(int argc, char **argv, unsigned int *threshold_mb, return 0; } -#define MAX_TEST 14 +#define MAX_TEST 15 #define MAX_PERF_TEST 3 int main(int argc, char **argv) { @@ -449,7 +491,7 @@ int main(int argc, char **argv) unsigned int threshold_mb = VALIDATION_DEFAULT_THRESHOLD; unsigned int pattern_seed; int num_expand_tests = 2; - struct test test_cases[MAX_TEST]; + struct test test_cases[MAX_TEST] = {}; struct test perf_test_cases[MAX_PERF_TEST]; int page_size; time_t t; @@ -510,6 +552,11 @@ int main(int argc, char **argv) test_cases[13] = MAKE_TEST(_1MB, _1MB, _5MB, NON_OVERLAPPING, EXPECT_SUCCESS, "5MB mremap - Source 1MB-aligned, Destination 1MB-aligned"); + /* Src and Dest addr 1MB aligned. 5MB mremap. */ + test_cases[14] = MAKE_TEST(_1MB, _1MB, _5MB, NON_OVERLAPPING, EXPECT_SUCCESS, + "5MB mremap - Source 1MB-aligned, Dest 1MB-aligned with 40MB Preamble"); + test_cases[14].config.dest_preamble_size = 10 * _4MB; + perf_test_cases[0] = MAKE_TEST(page_size, page_size, _1GB, NON_OVERLAPPING, EXPECT_SUCCESS, "1GB mremap - Source PTE-aligned, Destination PTE-aligned"); /*