Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1293430ybv; Thu, 13 Feb 2020 20:11:05 -0800 (PST) X-Google-Smtp-Source: APXvYqxBwImCZb5Edpas0gBo84SwgEk6E9Pcm8OMeqyxUeo3u+8x0CNxQR4lvAxRnKSYLohK9RZC X-Received: by 2002:aca:ac10:: with SMTP id v16mr565699oie.123.1581653465656; Thu, 13 Feb 2020 20:11:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581653465; cv=none; d=google.com; s=arc-20160816; b=RXSTgby2hwhQ+DD90DtzbeeJLrBH9fg1m8xqZyw3WclAfABLhWa86SFgzfFPad53ms vvrRO0iscKRuHSt6699eh4O4q2xPG+LtJXbhL99CyCqs9fs5TJf8sM1tCempRzYjQ9rT S3wiFP36emnLcyZliCVntnusZ5AO3qSZSbhFct44mQNntkS5TT0G1qvRDwf5HiVLJN98 /djQiwOvS5UXNGdI54HcoVsY2mGqV373MGCMeCQGfzuzRlmhStDcrt5kH5rzXOxyDa9x g3dpybXjDtEZ34ONzqM4GFvMQlQ1oGFv/uiKB7SDZaXcv4np2rdCtkGUfrYbiotQTmnT x0Rw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=2uoNUUGtHwnITLt2DMb1JQQT22YDWW1bocfB8RTH9Fw=; b=0pHg/NGfIaaMWWTDdHeBX5FiR/G3HYtxkZdDCgDU5TQng/6jx4dT3sncNBMen2qT/s HEYGnfQUWaCtIEgN2hJwTgK9zVwVXvUd6cK1VeJA3NHiFHbppanf1VLcw4jW2HWVz38/ Ku/WDNuwBklWE4CsPfi12yU9GvjjrpZU2SSujxsBAJvifVMlqFkSv7+Qdoap5wCTYRbR HFDamEv8bUZbu14YVsfYNe4TvJvbh4Zv8ST9kc0g8zNWW4aQhd8i7AEW90/NuN3l/jfP ui8vt58ywh7l2yyxICTmscsJWfy+isNt+pVdH/5PZ/3GBWDtSpYBkt/o9hc8Fz4T2xKv Lytw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=LQPVFDje; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b191si2008925oii.266.2020.02.13.20.10.53; Thu, 13 Feb 2020 20:11:05 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=LQPVFDje; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728416AbgBNEKi (ORCPT + 99 others); Thu, 13 Feb 2020 23:10:38 -0500 Received: from mail-ua1-f73.google.com ([209.85.222.73]:35897 "EHLO mail-ua1-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728595AbgBNEKi (ORCPT ); Thu, 13 Feb 2020 23:10:38 -0500 Received: by mail-ua1-f73.google.com with SMTP id b18so2049838uaj.3 for ; Thu, 13 Feb 2020 20:10:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=2uoNUUGtHwnITLt2DMb1JQQT22YDWW1bocfB8RTH9Fw=; b=LQPVFDje4iIkzs/Lq0cNUS+xTINGSTgVULCElCh14tLPD2INxCJgjei1vspVBaASzc SNvStdSwbZHxBn9lokVpawwO/wQYX96pM0XptK3rG94rH8GSzWDQETZ5vx6BxXjFOI94 mF6mjr/L1Hop7uop0gby+4xGRz0rXQGcFmb1T6sELlHiQQRrZvO7osvNUuCtj3oBOz9A Jslh6nUYoKu5+/57XX62hH5IYioZBtV0Ds/zIl5qaTMp9CII8H7XfKf3KWQyC1HWgI33 0x5LGgag6SOYQOCN8MCorkurk+8dmPR4pewBKH2QD9hxq74tVaJVqY7KipQc5zM+lw3a xX+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=2uoNUUGtHwnITLt2DMb1JQQT22YDWW1bocfB8RTH9Fw=; b=COAttgWLdrKcWURS17OscrfzwdZ8Ln5WL4Iytr0KL+X6PMMiR+FvfGJOa6jCfL+JeI 05xyVfNgeD0wYbM7iMrdzMCf8LQHVHt1IgARIqYh0W6OYhDGOOWJcLR2aN4kJbAH/gyp CmFnAHhV5RoIi/+GbyXLDbqjzgadFXwKaJRZ6feTsqZkQ7OCAV+1KDZomj8seWcqfzwF 1s7PRhCocjtTNRkpV6oTEcoYrArP9ct7cUCSp7XPY9AA3NqR+ShtnGsD3+A5HL2iQawn hoBrvmbMkjGCDnzcrAzkHEFUEX4ZGK7CmJYJLWM7hd4bpUpZ4FuxGhsmYTjfkBkEI1ua HWLg== X-Gm-Message-State: APjAAAWQlK4LO6rZ2Z+j62mpAbkp1cd5Bh8TdTdm4xF3SfP07zMlaPAk NSHCnncCmYVhQiMss2opYqW0cf5JvAuz X-Received: by 2002:ab0:248a:: with SMTP id i10mr590373uan.108.1581653435831; Thu, 13 Feb 2020 20:10:35 -0800 (PST) Date: Thu, 13 Feb 2020 20:09:52 -0800 In-Reply-To: <20200214040952.43195-1-bgeffon@google.com> Message-Id: <20200214040952.43195-2-bgeffon@google.com> Mime-Version: 1.0 References: <20200207201856.46070-1-bgeffon@google.com> <20200214040952.43195-1-bgeffon@google.com> X-Mailer: git-send-email 2.25.0.265.gbab2e86ba0-goog Subject: [PATCH v5 2/2] selftest: Add MREMAP_DONTUNMAP selftest. From: Brian Geffon To: Andrew Morton Cc: "Michael S . Tsirkin" , Brian Geffon , Arnd Bergmann , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-api@vger.kernel.org, Andy Lutomirski , Will Deacon , Andrea Arcangeli , Sonny Rao , Minchan Kim , Joel Fernandes , Yu Zhao , Jesse Barnes , Nathan Chancellor , Florian Weimer , "Kirill A . Shutemov" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a few simple self tests for the new flag MREMAP_DONTUNMAP, they are simple smoke tests which also demonstrate the behavior. Signed-off-by: Brian Geffon --- tools/testing/selftests/vm/Makefile | 1 + tools/testing/selftests/vm/mremap_dontunmap.c | 326 ++++++++++++++++++ tools/testing/selftests/vm/run_vmtests | 15 + 3 files changed, 342 insertions(+) create mode 100644 tools/testing/selftests/vm/mremap_dontunmap.c diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile index 9534dc2bc929..4b2b969fc3c7 100644 --- a/tools/testing/selftests/vm/Makefile +++ b/tools/testing/selftests/vm/Makefile @@ -12,6 +12,7 @@ TEST_GEN_FILES += map_fixed_noreplace TEST_GEN_FILES += map_populate TEST_GEN_FILES += mlock-random-test TEST_GEN_FILES += mlock2-tests +TEST_GEN_FILES += mremap_dontunmap TEST_GEN_FILES += on-fault-limit TEST_GEN_FILES += thuge-gen TEST_GEN_FILES += transhuge-stress diff --git a/tools/testing/selftests/vm/mremap_dontunmap.c b/tools/testing/selftests/vm/mremap_dontunmap.c new file mode 100644 index 000000000000..de2a861c7c6d --- /dev/null +++ b/tools/testing/selftests/vm/mremap_dontunmap.c @@ -0,0 +1,326 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Tests for mremap w/ MREMAP_DONTUNMAP. + * + * Copyright 2020, Brian Geffon + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +#ifndef MREMAP_DONTUNMAP +#define MREMAP_DONTUNMAP 4 +#endif + +unsigned long page_size; +char *page_buffer; + +static void dump_maps(void) +{ + char cmd[32]; + + snprintf(cmd, sizeof(cmd), "cat /proc/%d/maps", getpid()); + system(cmd); +} + +#define BUG_ON(condition, description) \ + do { \ + if (condition) { \ + fprintf(stderr, "[FAIL]\t%s():%d\t%s:%s\n", __func__, \ + __LINE__, (description), strerror(errno)); \ + dump_maps(); \ + exit(1); \ + } \ + } while (0) + +// Try a simple operation for to "test" for kernel support this prevents +// reporting tests as failed when it's run on an older kernel. +static int kernel_support_for_mremap_dontunmap() +{ + int ret = 0; + unsigned long num_pages = 1; + void *source_mapping = mmap(NULL, num_pages * page_size, PROT_NONE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + BUG_ON(source_mapping == MAP_FAILED, "mmap"); + + // This simple remap should only fail if MREMAP_DONTUNMAP isn't + // supported. + void *dest_mapping = + mremap(source_mapping, num_pages * page_size, num_pages * page_size, + MREMAP_DONTUNMAP | MREMAP_MAYMOVE, 0); + if (dest_mapping == MAP_FAILED) { + ret = errno; + } else { + BUG_ON(munmap(dest_mapping, num_pages * page_size) == -1, + "unable to unmap destination mapping"); + } + + BUG_ON(munmap(source_mapping, num_pages * page_size) == -1, + "unable to unmap source mapping"); + return ret; +} + +// This helper will just validate that an entire mapping contains the expected +// byte. +static int check_region_contains_byte(void *addr, unsigned long size, char byte) +{ + BUG_ON(size & (page_size - 1), + "check_region_contains_byte expects page multiples"); + BUG_ON((unsigned long)addr & (page_size - 1), + "check_region_contains_byte expects page alignment"); + + memset(page_buffer, byte, page_size); + + unsigned long num_pages = size / page_size; + unsigned long i; + + // Compare each page checking that it contains our expected byte. + for (i = 0; i < num_pages; ++i) { + int ret = + memcmp(addr + (i * page_size), page_buffer, page_size); + if (ret) { + return ret; + } + } + + return 0; +} + +// this test validates that MREMAP_DONTUNMAP moves the pagetables while leaving +// the source mapping mapped. +static void mremap_dontunmap_simple() +{ + unsigned long num_pages = 5; + + void *source_mapping = + mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + BUG_ON(source_mapping == MAP_FAILED, "mmap"); + + memset(source_mapping, 'a', num_pages * page_size); + + // Try to just move the whole mapping anywhere (not fixed). + void *dest_mapping = + mremap(source_mapping, num_pages * page_size, num_pages * page_size, + MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL); + BUG_ON(dest_mapping == MAP_FAILED, "mremap"); + + // Validate that the pages have been moved, we know they were moved if + // the dest_mapping contains a's. + BUG_ON(check_region_contains_byte + (dest_mapping, num_pages * page_size, 'a') != 0, + "pages did not migrate"); + BUG_ON(check_region_contains_byte + (source_mapping, num_pages * page_size, 0) != 0, + "source should have no ptes"); + + BUG_ON(munmap(dest_mapping, num_pages * page_size) == -1, + "unable to unmap destination mapping"); + BUG_ON(munmap(source_mapping, num_pages * page_size) == -1, + "unable to unmap source mapping"); +} + +// This test validates MREMAP_DONTUNMAP will move page tables to a specific +// destination using MREMAP_FIXED, also while validating that the source +// remains intact. +static void mremap_dontunmap_simple_fixed() +{ + unsigned long num_pages = 5; + + // Since we want to guarantee that we can remap to a point, we will + // create a mapping up front. + void *dest_mapping = + mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + BUG_ON(dest_mapping == MAP_FAILED, "mmap"); + memset(dest_mapping, 'X', num_pages * page_size); + + void *source_mapping = + mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + BUG_ON(source_mapping == MAP_FAILED, "mmap"); + memset(source_mapping, 'a', num_pages * page_size); + + void *remapped_mapping = + mremap(source_mapping, num_pages * page_size, num_pages * page_size, + MREMAP_FIXED | MREMAP_DONTUNMAP | MREMAP_MAYMOVE, + dest_mapping); + BUG_ON(remapped_mapping == MAP_FAILED, "mremap"); + BUG_ON(remapped_mapping != dest_mapping, + "mremap should have placed the remapped mapping at dest_mapping"); + + // The dest mapping will have been unmap by mremap so we expect the Xs + // to be gone and replaced with a's. + BUG_ON(check_region_contains_byte + (dest_mapping, num_pages * page_size, 'a') != 0, + "pages did not migrate"); + + // And the source mapping will have had its ptes dropped. + BUG_ON(check_region_contains_byte + (source_mapping, num_pages * page_size, 0) != 0, + "source should have no ptes"); + + BUG_ON(munmap(dest_mapping, num_pages * page_size) == -1, + "unable to unmap destination mapping"); + BUG_ON(munmap(source_mapping, num_pages * page_size) == -1, + "unable to unmap source mapping"); +} + +// This test validates that we can MREMAP_DONTUNMAP for a portion of an +// existing mapping. +static void mremap_dontunmap_partial_mapping() +{ + /* + * source mapping: + * -------------- + * | aaaaaaaaaa | + * -------------- + * to become: + * -------------- + * | aaaaa00000 | + * -------------- + * With the destination mapping containing 5 pages of As. + * --------- + * | aaaaa | + * --------- + */ + unsigned long num_pages = 10; + void *source_mapping = + mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + BUG_ON(source_mapping == MAP_FAILED, "mmap"); + memset(source_mapping, 'a', num_pages * page_size); + + // We will grab the last 5 pages of the source and move them. + void *dest_mapping = + mremap(source_mapping + (5 * page_size), 5 * page_size, + 5 * page_size, + MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL); + BUG_ON(dest_mapping == MAP_FAILED, "mremap"); + + // We expect the first 5 pages of the source to contain a's and the + // final 5 pages to contain zeros. + BUG_ON(check_region_contains_byte(source_mapping, 5 * page_size, 'a') != + 0, "first 5 pages of source should have original pages"); + BUG_ON(check_region_contains_byte + (source_mapping + (5 * page_size), 5 * page_size, 0) != 0, + "final 5 pages of source should have no ptes"); + + // Finally we expect the destination to have 5 pages worth of a's. + BUG_ON(check_region_contains_byte(dest_mapping, 5 * page_size, 'a') != + 0, "dest mapping should contain ptes from the source"); + + BUG_ON(munmap(dest_mapping, 5 * page_size) == -1, + "unable to unmap destination mapping"); + BUG_ON(munmap(source_mapping, num_pages * page_size) == -1, + "unable to unmap source mapping"); +} + +// This test validates that we can shrink an existing mapping via the normal +// mremap behavior along with the MREMAP_DONTUNMAP flag. +static void mremap_dontunmap_shrink_mapping() +{ + /* + * We shrink the source by 5 pages while remapping. + * source mapping: + * -------------- + * | aaaaaaaaaa | + * -------------- + * to become: + * --------- + * | 00000 | + * --------- + * With the destination mapping containing 5 pages of As followed by + * the original pages of Xs. + * -------------- + * | aaaaaXXXXX | + * -------------- + */ + + unsigned long num_pages = 10; + + // We use MREMAP_FIXED because we don't want the mremap to place the + // remapped mapping behind the source, if it did + // we wouldn't be able to validate that the mapping was in fact + // adjusted. + void *dest_mapping = + mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + BUG_ON(dest_mapping == MAP_FAILED, "mmap"); + memset(dest_mapping, 'X', num_pages * page_size); + + void *source_mapping = + mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + BUG_ON(source_mapping == MAP_FAILED, "mmap"); + memset(source_mapping, 'a', num_pages * page_size); + + // We are shrinking the mapping while also using MREMAP_DONTUNMAP + void *remapped_mapping = + mremap(source_mapping, num_pages * page_size, 5 * page_size, + MREMAP_FIXED | MREMAP_DONTUNMAP | MREMAP_MAYMOVE, + dest_mapping); + BUG_ON(remapped_mapping == MAP_FAILED, "mremap"); + BUG_ON(remapped_mapping != dest_mapping, + "expected mremap to place mapping at dest"); + + // The last 5 pages of source should have become unmapped while the + // first 5 remain. + unsigned char buf[5]; + int ret = mincore(source_mapping + (5 * page_size), 5 * page_size, buf); + BUG_ON((ret != -1 || (ret == -1 && errno != ENOMEM)), + "we expect -ENOMEM from mincore."); + + BUG_ON(check_region_contains_byte(source_mapping, 5 * page_size, 0) != + 0, "source should have no ptes"); + BUG_ON(check_region_contains_byte(dest_mapping, 5 * page_size, 'a') != + 0, "dest mapping should contain ptes from the source"); + + // And the second half of the destination should be unchanged. + BUG_ON(check_region_contains_byte(dest_mapping + (5 * page_size), + 5 * page_size, 'X') != 0, + "second half of dest shouldn't be touched"); + + // Cleanup + BUG_ON(munmap(dest_mapping, num_pages * page_size) == -1, + "unable to unmap destination mapping"); + BUG_ON(munmap(source_mapping, 5 * page_size) == -1, + "unable to unmap source mapping"); +} + +int main(void) +{ + page_size = sysconf(_SC_PAGE_SIZE); + + // test for kernel support for MREMAP_DONTUNMAP skipping the test if + // not. + if (kernel_support_for_mremap_dontunmap() != 0) { + printf("No kernel support for MREMAP_DONTUNMAP\n"); + return KSFT_SKIP; + } + + // Keep a page sized buffer around for when we need it. + page_buffer = + mmap(NULL, page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + BUG_ON(page_buffer == MAP_FAILED, "unable to mmap a page."); + + mremap_dontunmap_simple(); + mremap_dontunmap_simple_fixed(); + mremap_dontunmap_partial_mapping(); + mremap_dontunmap_shrink_mapping(); + + BUG_ON(munmap(page_buffer, page_size) == -1, + "unable to unmap page buffer"); + + printf("OK\n"); + return 0; +} diff --git a/tools/testing/selftests/vm/run_vmtests b/tools/testing/selftests/vm/run_vmtests index 951c507a27f7..d380b95c5de5 100755 --- a/tools/testing/selftests/vm/run_vmtests +++ b/tools/testing/selftests/vm/run_vmtests @@ -227,4 +227,19 @@ else exitcode=1 fi +echo "------------------------------------" +echo "running MREMAP_DONTUNMAP smoke test" +echo "------------------------------------" +./mremap_dontunmap +ret_val=$? + +if [ $ret_val -eq 0 ]; then + echo "[PASS]" +elif [ $ret_val -eq $ksft_skip ]; then + echo "[SKIP]" + exitcode=$ksft_skip +else + echo "[FAIL]" + exitcode=1 +fi exit $exitcode -- 2.25.0.265.gbab2e86ba0-goog