From patchwork Sun Sep 3 15:13:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 13373276 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 7D9C7C83F2D for ; Sun, 3 Sep 2023 15:13:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3072B280016; Sun, 3 Sep 2023 11:13:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2C1D0280012; Sun, 3 Sep 2023 11:13:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0F10B280017; Sun, 3 Sep 2023 11:13:45 -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 D43C0280012 for ; Sun, 3 Sep 2023 11:13:45 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id ABB0A1604BD for ; Sun, 3 Sep 2023 15:13:45 +0000 (UTC) X-FDA: 81195630810.28.C6F2965 Received: from mail-io1-f44.google.com (mail-io1-f44.google.com [209.85.166.44]) by imf01.hostedemail.com (Postfix) with ESMTP id CA85E40017 for ; Sun, 3 Sep 2023 15:13:42 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=joelfernandes.org header.s=google header.b=pM6S44Vh; spf=pass (imf01.hostedemail.com: domain of joel@joelfernandes.org designates 209.85.166.44 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=1693754022; 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=JBAJc+/RO3zfkzT8oQUizmToikDAYdiXUobcga/v+/I=; b=QcPji7iZ170o7dk3988xXs4AydixB4dFtVknjcrcBkzOM9VBAnD68C1SEPh9LkRRkgOqZk 64LjPufPXoNBqWra1zzKleqwg2+xDdotY3o2GSIYcgklrmsbzsK1jebgrZhFvVh6SnCf8u Z4jFAGFpaWlmpqb6MH/TvazV6IDEXIg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693754022; a=rsa-sha256; cv=none; b=8AyXorqvoTBbtWnuYSb5sMzW1J796YQif7IH4NIYRDbzpOJ7JqEqCWlmdlnKkVQYhhqr87 gKR52mDW1S4Lkp0HVe2ronBgb03tFZPR0BgyNvCdv80McppPlhsN07+oeCjV734CoC0WU/ 1DeLj97YlDQ/IOoVnRc4+38/suUZZHw= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=joelfernandes.org header.s=google header.b=pM6S44Vh; spf=pass (imf01.hostedemail.com: domain of joel@joelfernandes.org designates 209.85.166.44 as permitted sender) smtp.mailfrom=joel@joelfernandes.org; dmarc=none Received: by mail-io1-f44.google.com with SMTP id ca18e2360f4ac-77acb04309dso33397939f.2 for ; Sun, 03 Sep 2023 08:13:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; t=1693754022; x=1694358822; 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=JBAJc+/RO3zfkzT8oQUizmToikDAYdiXUobcga/v+/I=; b=pM6S44Vhy+mvVJLWWQu0gVpk4oknXyvY9eE62BJZqeXUy9IH5357AH2kcMabdaL/zn g5Vama/Tvy2OQqFJtwhzjfKQVrTZti9rUYCOct6IM3GlFp60dSB/pExYbmsRxkqsTMhT GWlwTkMmqvMOBNyVm8NLOcR16fRt/Wq2v3C+4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693754022; x=1694358822; 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=JBAJc+/RO3zfkzT8oQUizmToikDAYdiXUobcga/v+/I=; b=kqMVUyImR3NtaPWpwh2XB4uK3N7Birl3B0ewpVqWwsyIp/tG61Eg+Fv1R33oCCfynH IEGjG/btnMILS43z7CSrTOWO+VA1s0iHItB3RxX/a4A5+3EuZfPngyVanq6Gh4qVVOVw 5gPCrSh49XKRxEXCE9xkyqOVbSNlhyeLastFJjlcWKHbTVh1ZKl/xSZn5RChb65Q94xl AuZEJhTEBbRfjCzkRKZbejfoAnWOXjcfZvNtSyH3m8a6y1wF2k6m5rTotuN8oy1tgVW5 OLpzurZS95g/oV6LOLMK1c/25hXSWOraYW8B2ccJ+gCig0mz5yt0vGce30+jdYsQ/p/V aWgA== X-Gm-Message-State: AOJu0YxttG+OokC1Fy5TQpN7RjVUoMwH3xtR+xxvLsakMS5d4LemBpj0 i46fsyZ/6egiFWwld3rcSmpLGQ== X-Google-Smtp-Source: AGHT+IEnJA2vqbUSl47p66mh9CCynh4MUM2TZrhlzcxgAiU4zPqoOBOaRdFGp3Nx/rofoZWjgNCIdw== X-Received: by 2002:a6b:7218:0:b0:77e:3598:e511 with SMTP id n24-20020a6b7218000000b0077e3598e511mr9517002ioc.2.1693754022005; Sun, 03 Sep 2023 08:13:42 -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.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Sep 2023 08:13:41 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Shuah Khan , Vlastimil Babka , Michal Hocko , Linus Torvalds , Lorenzo Stoakes , Kirill A Shutemov , "Liam R. Howlett" , "Paul E. McKenney" , Suren Baghdasaryan , Kalesh Singh , Lokesh Gidra Subject: [PATCH v6 6/7] selftests: mm: Add a test for remapping within a range Date: Sun, 3 Sep 2023 15:13:27 +0000 Message-ID: <20230903151328.2981432-7-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-Rspamd-Queue-Id: CA85E40017 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: xonnumxojec3ecf6ipet8msfye65qhbo X-HE-Tag: 1693754022-28386 X-HE-Meta: U2FsdGVkX18+S3OTke480V8QMI+sIxS0XO7KNFKOmcGpqUNHdIGqc2qhPQbiD1Xq0AXDlChYaTV651x++Sb2yTluA8GChUEX81juhg3uMcYYRvxGuvL8fvGPFKmsL9YnGNMvVesFryson5qvp/CER+vq3DArZtCLKRX2X+VrcyAnZd69cVzU/Cm5Q8xjlLBcVuGoueqT0xnghJgJNvvKJszeyd9FpYxPquOynTEbw3ncJmDYXs6JgDeHIH/D6voBocqodjHH2VEJsrkyiAUPQG2GmWILDy1rQNzbfxK0/Tg3Lou9rDQioC99Q59sp9oP0VQYaxws/aSID5YXMbeyVZNVqUKQD49N/8MMWmpstokVKkXxK18FD9LCli3DGT5PP3lVD1uPQYFLJmM4sMUSnOy5GtRnhxcWmGmVOBi0PK5I2k0khFpeS5EzjXDjjEfPLTUctCxKRHcBNZ9a2w0uu67AA/r2CiELHUUZmLqUlgRjnVpMy6taN0hxmU79qQUupxrhyr/h++ZPbFFxHUmVCwnpkTqlnZufPDCMSrDspTLcGyPPs/LxkJw6V1R0mWaxp7h+3ds2GeCHbpFm69CXPDch1ZBW97hpT8zH4wxtj5zJek+2imZ+nUvl6VxtQCbqSSGGkKfPmUFGOg/Q+G8047/goidXqymodEvNQwYfWgF0xARCXjdXm4xuNquEBdi9ElArJRgb4mYWkQ4j+j1rqSWKnqrnIt5MyUVpPG1a7qdZkNZHk9yGDMEMn76NIfwcxRblqRvpl7v3Vsk5dJO5APLai58qhTjbVa6D4jg9s1CftGxLjve8+49J+LcvkkQyIJewBdVvrNvEk98KD1n8LSPYusG8gdPrnNcxaddMyj+8DC/k5FRinx863XKFZVFdB92okSzLC5doEFETLVh9JfyUbYqvif6EhUKzHW6L6JRaUdxvCgYxhqWrHxNmNbkheSGMsD4/TG+Zkk0M2fH QvhNXVaN MI+USuZRjmsKMtH0lRnZfSkkduIagAk5CNjarqJLsGPzft5wqbbR5fD4PGZY9bGE2ko2TW/VSkgADg0Z7WU7TM5U5EhnHt7t2Ezw4riGzCbkoVxzCueY3cP7mtUqgxs/i5cmIuNEkXlqBIYOiJG92f78Kw5crNzP8HziCoN6M/oAqaidETPz2QtXU7UdcfwKmZ2dmQ8JgemP+r+uFqGn+0Vd4wK+4EfdCs25qe2KK4r91EbGEafB+NrHAhEX8R2CwMIF2C4kpWbcN1+NhYI8QfE4QjfDzNUF7H5kWoQTMIT7mmzrujpbpDAXD4K8JUZutW1UTzMXkVimTngy0EsS7GX5dSAtxA/MNGs6sScIK0oU0kIO6Zt5pkmlJZnxcpkZHxJlFdiwd1iebRTrgkP1nZt4kRcuTGyeElkkLh193asF4A2jZ5X3a31SjmiGQoYsHXtry1+Dps/SyVR/u5vUu51trM05uQyb5ThlC 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: Move a block of memory within a memory range. Any alignment optimization on the source address may cause corruption. Verify using kselftest that it works. I have also verified with tracing that such optimization does not happen due to this check in can_align_down(): if (!for_stack && vma->vm_start != addr_to_align) return false; Signed-off-by: Joel Fernandes (Google) Reviewed-by: Lorenzo Stoakes --- tools/testing/selftests/mm/mremap_test.c | 79 +++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/mremap_test.c b/tools/testing/selftests/mm/mremap_test.c index d7366074e2a8..12a095457f4c 100644 --- a/tools/testing/selftests/mm/mremap_test.c +++ b/tools/testing/selftests/mm/mremap_test.c @@ -23,6 +23,7 @@ #define VALIDATION_NO_THRESHOLD 0 /* Verify the entire region */ #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) +#define SIZE_MB(m) ((size_t)m * (1024 * 1024)) struct config { unsigned long long src_alignment; @@ -226,6 +227,79 @@ static void mremap_expand_merge_offset(FILE *maps_fp, unsigned long page_size) ksft_test_result_fail("%s\n", test_name); } +/* + * Verify that an mremap within a range does not cause corruption + * of unrelated part of range. + * + * Consider the following range which is 2MB aligned and is + * a part of a larger 20MB range which is not shown. Each + * character is 256KB below making the source and destination + * 2MB each. The lower case letters are moved (s to d) and the + * upper case letters are not moved. The below test verifies + * that the upper case S letters are not corrupted by the + * adjacent mremap. + * + * |DDDDddddSSSSssss| + */ +static void mremap_move_within_range(char pattern_seed) +{ + char *test_name = "mremap mremap move within range"; + void *src, *dest; + int i, success = 1; + + size_t size = SIZE_MB(20); + void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (ptr == MAP_FAILED) { + perror("mmap"); + success = 0; + goto out; + } + memset(ptr, 0, size); + + src = ptr + SIZE_MB(6); + src = (void *)((unsigned long)src & ~(SIZE_MB(2) - 1)); + + /* Set byte pattern for source block. */ + srand(pattern_seed); + for (i = 0; i < SIZE_MB(2); i++) { + ((char *)src)[i] = (char) rand(); + } + + dest = src - SIZE_MB(2); + + void *new_ptr = mremap(src + SIZE_MB(1), SIZE_MB(1), SIZE_MB(1), + MREMAP_MAYMOVE | MREMAP_FIXED, dest + SIZE_MB(1)); + if (new_ptr == MAP_FAILED) { + perror("mremap"); + success = 0; + goto out; + } + + /* Verify byte pattern after remapping */ + srand(pattern_seed); + for (i = 0; i < SIZE_MB(1); i++) { + char c = (char) rand(); + + if (((char *)src)[i] != c) { + ksft_print_msg("Data at src at %d got corrupted due to unrelated mremap\n", + i); + ksft_print_msg("Expected: %#x\t Got: %#x\n", c & 0xff, + ((char *) src)[i] & 0xff); + success = 0; + } + } + +out: + if (munmap(ptr, size) == -1) + perror("munmap"); + + if (success) + ksft_test_result_pass("%s\n", test_name); + else + ksft_test_result_fail("%s\n", test_name); +} + /* * Returns the start address of the mapping on success, else returns * NULL on failure. @@ -491,6 +565,7 @@ int main(int argc, char **argv) unsigned int threshold_mb = VALIDATION_DEFAULT_THRESHOLD; unsigned int pattern_seed; int num_expand_tests = 2; + int num_misc_tests = 1; struct test test_cases[MAX_TEST] = {}; struct test perf_test_cases[MAX_PERF_TEST]; int page_size; @@ -572,7 +647,7 @@ int main(int argc, char **argv) (threshold_mb * _1MB >= _1GB); ksft_set_plan(ARRAY_SIZE(test_cases) + (run_perf_tests ? - ARRAY_SIZE(perf_test_cases) : 0) + num_expand_tests); + ARRAY_SIZE(perf_test_cases) : 0) + num_expand_tests + num_misc_tests); for (i = 0; i < ARRAY_SIZE(test_cases); i++) run_mremap_test_case(test_cases[i], &failures, threshold_mb, @@ -590,6 +665,8 @@ int main(int argc, char **argv) fclose(maps_fp); + mremap_move_within_range(pattern_seed); + if (run_perf_tests) { ksft_print_msg("\n%s\n", "mremap HAVE_MOVE_PMD/PUD optimization time comparison for 1GB region:");