From patchwork Fri Dec 16 21:44:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13075469 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 7A303C4332F for ; Fri, 16 Dec 2022 21:45:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6A3C38E0002; Fri, 16 Dec 2022 16:45:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 652ED8E0001; Fri, 16 Dec 2022 16:45:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4F3048E0002; Fri, 16 Dec 2022 16:45:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 3F7A78E0001 for ; Fri, 16 Dec 2022 16:45:09 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 1407EAB9C9 for ; Fri, 16 Dec 2022 21:45:09 +0000 (UTC) X-FDA: 80249500338.11.D1DA486 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) by imf20.hostedemail.com (Postfix) with ESMTP id 7A11D1C001E for ; Fri, 16 Dec 2022 21:45:07 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=FqV5ZpDN; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf20.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.44 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671227107; 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:dkim-signature; bh=GRK3ufbYho0z9LiSN86FnA5BN6pv7SkyPTFsiET5xIc=; b=uJSW2D1rkTeIB/hytWS74apOzWr4SLaL00s7cgBPEQr6RwPFXBLhwgtQb3mfOjJuLXEfSY B/Byj5y2y8wwSIv5XNYuOMNnRb4rn6WgyrSYJbM/v6MAak3x/uraC/UFUzMfcq0RNJPido 2b0hcdUDrl0Mt+2KimyXUfEtasr1kPo= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=FqV5ZpDN; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf20.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.44 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671227107; a=rsa-sha256; cv=none; b=xnmrm7zuihqdglv8zZBszqxfh7TuxbDPDVgP+hs0tdgfL8NAgsU0vKocOQO/3fRYheQzDc bliqKNQ7K8p95gWc7+FPQ41FnqYKvjp6JspLQN/3+Zs4iCoFCv37SwWcf/giTGS/KkYEaO 9PTKzbEdY+RpXX/ikIfR+cx8BG8Ubg8= Received: by mail-wm1-f44.google.com with SMTP id m5-20020a7bca45000000b003d2fbab35c6so2686277wml.4 for ; Fri, 16 Dec 2022 13:45:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=GRK3ufbYho0z9LiSN86FnA5BN6pv7SkyPTFsiET5xIc=; b=FqV5ZpDNvorXYyLpowDzrdKInEILpDvNy30qKoV0gMOzQMUp5S8UxLXlhmghPi9Hkg Em+Oib6+DVksyWYEIGRYuMAS520rW7LCPEvDvcMkdgjPhzWlNas86EaayO0lrmy8goAp zmrgLWBmZnD3wuFLt24FMF+qqE/GNufngpY7dLDdqpIiFJED7tsLdFHZUMkOYZzNPRYs MjepnAYaSUsxxARCMosTLXJfDjEjEvIwupu6S278/exYqeXhmP15EpdYRpf0kmJuZ450 tjn7uvnzevFuIf6z8pKic6CcVSKcgaHOVVZe1XkjrtbxBbi2oYQsCf2GPThziw0XxqVD w7SA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=GRK3ufbYho0z9LiSN86FnA5BN6pv7SkyPTFsiET5xIc=; b=iJ+Wiwv26posO0V4xuXco0LuFSDKRkImXTZzQRwy2VXSyZYML7WemovJi6dMNQVOrI Bq9n4XwM2FribSR7jvrjfAyU2UTiFt1F7Nvox9cGz7+/eb4MKYp8oB6QgUkVN4C0PM2n ldmqRJzLS6Xl88d4SjhURm1fM/HFDYAznqoD6YGL7bVsl67OHRMZvQw2MWlK6NREPKha nF/rndLeCdTSRe/TDQjU+hLuOQ9q86ZyHSFHDYpU60SYtAzRGecpPbjPB6Dl5AmEJTXG 1aP/nr8QdnlMtwO76r2PsY1LtZiy77LK7gYbU1/ovlqerfHAsu4BCcbju7FFAFZ5plxl VlYg== X-Gm-Message-State: ANoB5pk7l7RtCmBIhKA+k3btRB+dYlUF3Ijb3qKQvYVObVs4MuOxM3x6 VQwV1fcktj4IUyvPSXKht0kqa4uIMxo= X-Google-Smtp-Source: AA0mqf6VMLjHVjuSEBVqErF+mL/E0FJGDdBthNbYiNUdxFDFCpUa//9B+fdATpcwXQjf3zXLFPzupg== X-Received: by 2002:a05:600c:4fd1:b0:3cf:a08f:10a5 with SMTP id o17-20020a05600c4fd100b003cfa08f10a5mr27514173wmq.31.1671227105543; Fri, 16 Dec 2022 13:45:05 -0800 (PST) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id z19-20020a05600c221300b003a3170a7af9sm3728762wml.4.2022.12.16.13.45.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Dec 2022 13:45:04 -0800 (PST) From: Lorenzo Stoakes To: linux-mm@kvack.org, Andrew Morton , Shuah Khan , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Vlastimil Babka , Jakub Matena , Matthew Wilcox , Mel Gorman , Michal Hocko , Lorenzo Stoakes Subject: [PATCH] selftest/vm: add mremap expand merge offset test Date: Fri, 16 Dec 2022 21:44:36 +0000 Message-Id: <20221216214436.405071-1-lstoakes@gmail.com> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: 7A11D1C001E X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: rg53e7677ex653kiqm6rzx94xbgme56k X-HE-Tag: 1671227107-124860 X-HE-Meta: U2FsdGVkX1+1v3fcE+VJqKBxRedioijOa+M4MVs3vh8zRU2HEdLuc5CMcRscZY50j08aBHPJO9fw/A2AeyvCIfNx0/a7gZM5ndudq+5GKOvUsOBjSHQyfBlZUXRLfv+WWp3KY/pNjWldV1Nk4oZNr+WIOAxpnWwqXU1TmnkXFX8QC1liC81M4UMruv9s6yAFaf9f6GX2Wk41aCk+rSxdLGBrTCCAn4mvXqlTzF+NMfNg0JPIio2Rfx3a610iNBgCt0iwkG2L11H/zgzaPkDXK2liRNjWJaHQC/W2mVKrkaiije5elUQ0AC3kNLFuXcObGsaVj1rV0Q5Dw5S3z+uH3Uv8Ax4mgUy4h+qhtO/Krxzx1RPiUc3NHKd6Juwa+PnC3ExnVg5eI1/nXGsrtXTrdXfgvgEbAX9zLtFS7FWdq241LWazeOC6awy367TTQuwNStfnmGul0yhGhOtujqJ/qXsI9BhuHc4B1yKNm4UERzLystuIHYwEp1sjNpGQIiLvU/p799rU9kUNaSFZ5LFwy85CA4oAQngwQOrkj70VpiTAdiA4ZdnSbxKZPe29+SLEHsacjHjq30yqC3BwAecbBeBh2mapclvZywwcqrSOtps0YDSKMmw53QNeP3uIYadRjuGzGtXL7gTO2ka2fWj4iZA/nyDJ5vk0RVAmDtMwQ9Q7rgHdrgimqBzEjVGztDjfp/2/dHymQx7PlUWqpVXG5wBjmZVRtOR5GatQeZcwUXWAwAbDEIrEcKjS+51q8+tu0O5P8irX9voj42VAMWKtlgoJoj2ku0GFnPG/qi5SSkGrNz9/WB9ZCUX6ZjvgiBfxlWyrM5cwlATmtaOYQ0JH8P8aMXnK8z0z10+Kydtz/WcuXhTB1wjcfzuq5vAzf2Bj1Dv9f/IiRK3aEEhmI/85kiCkj1u4edvUMcfsSpWFGEb0oiovMwq9ToxR7lAGvgP0xUBkMfILfW0LtQHjw9V k60FrR/o 1nzXwqgE2kXlaWxUSsG+30G9uO5aT3mawM21GE/M6pwKW6zLMhnn5/fkfScCmFYy303P6h/9bUXQZ5guheSj4mtxhfh+kxxskEIM+gVKlpY8Nk0d+/APtTA6448RxZWcW4GOl 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: Add a test to assert that we can mremap() and expand a mapping starting from an offset within an existing mapping. We unmap the last page in a 3 page mapping to ensure that the remap should always succeed, before remapping from the 2nd page. This is additionally a regression test for the issue solved in "mm, mremap: fix mremap() expanding vma with addr inside vma" and confirmed to fail prior to the change and pass after it. Finally, this patch updates the existing mremap expand merge test to check error conditions and reduce code duplication between the two tests. Signed-off-by: Lorenzo Stoakes --- tools/testing/selftests/vm/mremap_test.c | 111 +++++++++++++++++++---- 1 file changed, 93 insertions(+), 18 deletions(-) -- 2.38.1 diff --git a/tools/testing/selftests/vm/mremap_test.c b/tools/testing/selftests/vm/mremap_test.c index 9496346973d4..28a17d4e8afd 100644 --- a/tools/testing/selftests/vm/mremap_test.c +++ b/tools/testing/selftests/vm/mremap_test.c @@ -119,30 +119,19 @@ static unsigned long long get_mmap_min_addr(void) } /* - * This test validates that merge is called when expanding a mapping. - * Mapping containing three pages is created, middle page is unmapped - * and then the mapping containing the first page is expanded so that - * it fills the created hole. The two parts should merge creating - * single mapping with three pages. + * Using /proc/self/maps, assert that the specified address range is contained + * within a single mapping. */ -static void mremap_expand_merge(unsigned long page_size) +static bool is_range_mapped(void *start, void *end) { - char *test_name = "mremap expand merge"; FILE *fp; char *line = NULL; size_t len = 0; bool success = false; - char *start = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - - munmap(start + page_size, page_size); - mremap(start, page_size, 2 * page_size, 0); fp = fopen("/proc/self/maps", "r"); - if (fp == NULL) { - ksft_test_result_fail("%s\n", test_name); - return; - } + if (fp == NULL) + return false; while (getline(&line, &len, fp) != -1) { char *first = strtok(line, "- "); @@ -150,16 +139,101 @@ static void mremap_expand_merge(unsigned long page_size) char *second = strtok(NULL, "- "); void *second_val = (void *) strtol(second, NULL, 16); - if (first_val == start && second_val == start + 3 * page_size) { + if (first_val <= start && second_val >= end) { success = true; break; } } + + fclose(fp); + return success; +} + +/* + * This test validates that merge is called when expanding a mapping. + * Mapping containing three pages is created, middle page is unmapped + * and then the mapping containing the first page is expanded so that + * it fills the created hole. The two parts should merge creating + * single mapping with three pages. + */ +static void mremap_expand_merge(unsigned long page_size) +{ + char *test_name = "mremap expand merge"; + bool success = false; + int errsv = 0; + char *remap; + char *start = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + if (start == MAP_FAILED) { + errsv = errno; + goto error; + } + + munmap(start + page_size, page_size); + remap = mremap(start, page_size, 2 * page_size, 0); + if (remap == MAP_FAILED) { + errsv = errno; + munmap(start, page_size); + munmap(start + 2 * page_size, page_size); + goto error; + } + + success = is_range_mapped(start, start + 3 * page_size); + + munmap(start, 3 * page_size); + goto out; + +error: + ksft_print_msg("Unexpected mapping/remapping error: %s\n", + strerror(errsv)); +out: + if (success) + ksft_test_result_pass("%s\n", test_name); + else + ksft_test_result_fail("%s\n", test_name); +} + +/* + * Similar to mremap_expand_merge() except instead of removing the middle page, + * we remove the last then attempt to remap offset from the second page. This + * should result in the mapping being restored to its former state. + */ +static void mremap_expand_merge_offset(unsigned long page_size) +{ + + char *test_name = "mremap expand merge offset"; + bool success = false; + int errsv = 0; + char *remap; + char *start = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + if (start == MAP_FAILED) { + errsv = errno; + goto error; + } + + /* Unmap final page to ensure we have space to expand. */ + munmap(start + 2 * page_size, page_size); + remap = mremap(start + page_size, page_size, 2 * page_size, 0); + if (remap == MAP_FAILED) { + errsv = errno; + munmap(start, 2 * page_size); + goto error; + } + + success = is_range_mapped(start, start + 3 * page_size); + goto out; + +error: + ksft_print_msg("Unexpected mapping/remapping error: %s\n", + strerror(errsv)); +out: if (success) ksft_test_result_pass("%s\n", test_name); else ksft_test_result_fail("%s\n", test_name); - fclose(fp); } /* @@ -459,6 +533,7 @@ int main(int argc, char **argv) pattern_seed); mremap_expand_merge(page_size); + mremap_expand_merge_offset(page_size); if (run_perf_tests) { ksft_print_msg("\n%s\n",