Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp78708pxf; Tue, 23 Mar 2021 22:50:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwk7W+l1rfuSCcFHFajxFzfeW8WjHQ/6fV+DXonV4pXGySEXumRoF48+Q4cCj4Zu2y9hB7k X-Received: by 2002:a17:906:719b:: with SMTP id h27mr1778734ejk.123.1616565040334; Tue, 23 Mar 2021 22:50:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616565040; cv=none; d=google.com; s=arc-20160816; b=Jte2WutOHzx8SwTFbV24/wGe72t9U7fRa5m8YWafuwW6y90+hZkaJ1zv2BtWgwtAJJ 1Mf2FrglMqJDNXPNvtI4LY/JuMNhsft+8tpONh6vZD+XdUdUEnzkKQJ1pIFuvGD3/UHl dQYifobJk55snWS/folfERJH2s6D9ZcVVh6TpQaEyTOqWeoIPoE7u87TRHTh0OWhO4VZ QliiIwFdxfGUDj0L9A6V88IYXp8Mc7PTN1kUBnHVqQRlIy0i3ICOYkHkAkFPMYHd2Hdp f12yl74TYdtaTgS9llOJ4GAAMn5QRbgAFjUlvO+ok3jG/6Db9Dhae9iGc1c5qRUXGiug 6Uag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=XPRq1W/wuES2HJCP2WXgJHm10YoXyWPHE5kWVTMZXI4=; b=tniOBkxJ2eMthY2EjX8Ay+Xpbgvz/YLGHJgqufFw4w4zFvTc5qzRThw/2XDztKD4Dw wM7CI0NWbPM0qs+1THdWiOueSJdGbSy93DC4q3+xNPqu4rXbOz57dMf/YzTuhwot8tXg zt93cFhMlSfPacnkbG0HWgKjBUvTPzxh7Oa/i1QfO6i2Kyj6UueS3Szd07Es27zd4Don 83mcEsbDIBZQ62RPlcAsKyjCl2w26pB/bA/UKjaJlh6fwd1JcHfiSgy8KE/myv/tCKDm AQj49XYlY5u3DzFpSCb8XJC3heGJdh3oV2KjnlV0xSWZHE0AvNG0CHmZMmASEALwo3ti p24Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Z2BuAR3K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id c11si983557ejs.584.2021.03.23.22.50.18; Tue, 23 Mar 2021 22:50:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Z2BuAR3K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S233257AbhCWQ06 (ORCPT + 99 others); Tue, 23 Mar 2021 12:26:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233152AbhCWQ0X (ORCPT ); Tue, 23 Mar 2021 12:26:23 -0400 Received: from mail-qt1-x84a.google.com (mail-qt1-x84a.google.com [IPv6:2607:f8b0:4864:20::84a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66AC0C061574 for ; Tue, 23 Mar 2021 09:26:23 -0700 (PDT) Received: by mail-qt1-x84a.google.com with SMTP id f26so1525080qtq.17 for ; Tue, 23 Mar 2021 09:26:23 -0700 (PDT) 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=XPRq1W/wuES2HJCP2WXgJHm10YoXyWPHE5kWVTMZXI4=; b=Z2BuAR3KJ1S3vRUxnuAIr23jCEDLS3JCMZT54LNGkWacyQ5/tTVzFNsfhXV/iifyf0 UMYh70Gvk7dGWuubbFlPUZmxni1D3bn7Dbes24AkjDkZIhRl9yC61pAzMKtkgENZx4kq P+PBjD9MqmAGYbHpeZ0eXb0f2gJQaARF2P4aw6hlq0gtwPIEulhe3cHJ+VyoIZZawZ6X 399Z71YgRFMGdFR4U7X7D4jxBeOCuP19SB37meORrEb9mLXtGlOBevosPF7Sp/OENwcK uCCiXL/7rQjdgagMSw6Cd2396aEhXZvrYrO7PQ1RO0jnYXmc/nKMcko3Wd22ptqvvEdb OK6Q== 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=XPRq1W/wuES2HJCP2WXgJHm10YoXyWPHE5kWVTMZXI4=; b=KVxf5Q0vBAAIZkYO7xkrGu0WnwcygPZK+X1yXwZsbD++7R7kCK8VXJ6+rbI5bfRMR3 nNTXQKrqoThIcpzeZ6lpIpGdKoSm0gQuBcFbmGENLcCfiKuP0wdmxmJGn9LZCGMy2pRs Pt9mFXzmIfQwM7YVxo1FLH+yOif/6e/ev9y5bJdjJ+TakFWsi+9sPmtJdOw02QnLAN8R bbwqFUKPK8iBAkl+KRc4vJc9pLGUBXe2KlV4SPNqvQ3UsX4n0W0JRivUdKWME/XNULlM whwW9Q5+1vsGfhfL5CRO0QudHM3QwqNpkhPWVY6EBWL0RTHeZTVhWiodNA27/n9qdY+t tvxg== X-Gm-Message-State: AOAM5334ND1JGhn9jblj+7pBPvqSKHAxDi/CO1XgVB/lsN1UvulHzO8o f89WgZljX2vO+jo6Q6/zxyAcYkKTm1ES X-Received: from bg.sfo.corp.google.com ([2620:15c:8:10:b5cc:cacb:2875:ae6e]) (user=bgeffon job=sendgmr) by 2002:a05:6214:80a:: with SMTP id df10mr6038618qvb.46.1616516782596; Tue, 23 Mar 2021 09:26:22 -0700 (PDT) Date: Tue, 23 Mar 2021 09:26:10 -0700 In-Reply-To: <20210323162611.2398613-1-bgeffon@google.com> Message-Id: <20210323162611.2398613-3-bgeffon@google.com> Mime-Version: 1.0 References: <20210303175235.3308220-1-bgeffon@google.com> <20210323162611.2398613-1-bgeffon@google.com> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog Subject: [PATCH v4 3/3] selftests: Add a MREMAP_DONTUNMAP selftest for shmem From: Brian Geffon To: Andrew Morton Cc: Axel Rasmussen , Lokesh Gidra , Mike Rapoport , Peter Xu , Hugh Dickins , "Michael S . Tsirkin" , Brian Geffon , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andy Lutomirski , Vlastimil Babka , Andrea Arcangeli , Sonny Rao , Minchan Kim , "Kirill A . Shutemov" , Dmitry Safonov , Michael Kerrisk , Alejandro Colomar Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This test extends the current mremap tests to validate that the MREMAP_DONTUNMAP operation can be performed on shmem mappings. Signed-off-by: Brian Geffon --- tools/testing/selftests/vm/mremap_dontunmap.c | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tools/testing/selftests/vm/mremap_dontunmap.c b/tools/testing/selftests/vm/mremap_dontunmap.c index 3a7b5ef0b0c6..f01dc4a85b0b 100644 --- a/tools/testing/selftests/vm/mremap_dontunmap.c +++ b/tools/testing/selftests/vm/mremap_dontunmap.c @@ -127,6 +127,57 @@ static void mremap_dontunmap_simple() "unable to unmap source mapping"); } +// This test validates that MREMAP_DONTUNMAP on a shared mapping works as expected. +static void mremap_dontunmap_simple_shmem() +{ + unsigned long num_pages = 5; + + int mem_fd = memfd_create("memfd", MFD_CLOEXEC); + BUG_ON(mem_fd < 0, "memfd_create"); + + BUG_ON(ftruncate(mem_fd, num_pages * page_size) < 0, + "ftruncate"); + + void *source_mapping = + mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE, + MAP_FILE | MAP_SHARED, mem_fd, 0); + BUG_ON(source_mapping == MAP_FAILED, "mmap"); + + BUG_ON(close(mem_fd) < 0, "close"); + + 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); + if (dest_mapping == MAP_FAILED && errno == EINVAL) { + // Old kernel which doesn't support MREMAP_DONTUNMAP on shmem. + BUG_ON(munmap(source_mapping, num_pages * page_size) == -1, + "unable to unmap source mapping"); + return; + } + + 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"); + + // Because the region is backed by shmem, we will actually see the same + // memory at the source location still. + BUG_ON(check_region_contains_byte + (source_mapping, num_pages * page_size, 'a') != 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. @@ -300,6 +351,7 @@ int main(void) BUG_ON(page_buffer == MAP_FAILED, "unable to mmap a page."); mremap_dontunmap_simple(); + mremap_dontunmap_simple_shmem(); mremap_dontunmap_simple_fixed(); mremap_dontunmap_partial_mapping(); mremap_dontunmap_partial_mapping_overwrite(); -- 2.31.0.rc2.261.g7f71774620-goog