From patchwork Tue Dec 10 21:30:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Geffon X-Patchwork-Id: 13902098 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 320A0E77180 for ; Tue, 10 Dec 2024 21:31:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8C0898D0017; Tue, 10 Dec 2024 16:31:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 84B8A8D000B; Tue, 10 Dec 2024 16:31:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6006C8D0017; Tue, 10 Dec 2024 16:31:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 3C7A08D000B for ; Tue, 10 Dec 2024 16:31:07 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E106E42868 for ; Tue, 10 Dec 2024 21:31:06 +0000 (UTC) X-FDA: 82880344008.27.0D12574 Received: from mail-qt1-f201.google.com (mail-qt1-f201.google.com [209.85.160.201]) by imf10.hostedemail.com (Postfix) with ESMTP id 2D8F1C000C for ; Tue, 10 Dec 2024 21:30:55 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=mH4u4kST; spf=pass (imf10.hostedemail.com: domain of 3F7NYZwcKCCI9ECDDMLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--bgeffon.bounces.google.com designates 209.85.160.201 as permitted sender) smtp.mailfrom=3F7NYZwcKCCI9ECDDMLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--bgeffon.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733866254; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=p+Ew5mGOm8qX6hJiqcK96oIfktQ2zB7aJWIvrQrH8Us=; b=AbWe/9YMTQzB8eNAi31eMXY5snKJAAWes4vLRCtR6VmUXDdJuigdq2dvg1nwjzTRWKBzTq QWABkbw+rXsdWUzuYdgCBWG7KstqEQgxAVTS+zz6f/yR9U6J8PDXcAAig3jQYesh9Farps 48j3QlzOUF7lPp5raYDaRd8DbyXsLkg= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=mH4u4kST; spf=pass (imf10.hostedemail.com: domain of 3F7NYZwcKCCI9ECDDMLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--bgeffon.bounces.google.com designates 209.85.160.201 as permitted sender) smtp.mailfrom=3F7NYZwcKCCI9ECDDMLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--bgeffon.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733866254; a=rsa-sha256; cv=none; b=jI1c/ApvHAV6cwzg4eFXNk681BHS8ZCHdfktE319dikqB0Drbk+ZeB7w5bK0PYM+77gwNs P9Es81Ic9eDnjgOC2P7uSb6GJuDtApWikoOVPT573Y7IfHibhA0rV3L9P/uBUsB8olFGFe XH9wtqxbBh03ePvYJHQSZXp6Hb9lHUI= Received: by mail-qt1-f201.google.com with SMTP id d75a77b69052e-466bc740022so64238151cf.0 for ; Tue, 10 Dec 2024 13:31:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733866264; x=1734471064; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=p+Ew5mGOm8qX6hJiqcK96oIfktQ2zB7aJWIvrQrH8Us=; b=mH4u4kSTCo4kFXFlMuvSrq5Oz+cESU6Tp9Czn5l4S6h9IyTbfv8II7JXuaPh3FZjHE f7XCDNYZJHNiPRDX8/dF9aNo7zysjhUeO/QcY/RGx/vSKeNh+M3ZHnszXEQZkevCGrYd twAXucw8TBdrtCX24YBPYPLElhqMipFaVkACiGoHPgs8amcjXLRULPWBOLJtA2QeObQX d8/aUuwFKhAmaNA7LBT4fqz9FTN7rAe1yVlrQp/ErbqwYU7N46Gml5yjyXpqKkVa0y+z mZYL35o6Jpx6aTx6JrWdDBvaD56ff0NCRoNJ4sY2Y75CfQPQVQXWERiHzdHF7fAeRjVD srEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733866264; x=1734471064; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=p+Ew5mGOm8qX6hJiqcK96oIfktQ2zB7aJWIvrQrH8Us=; b=PIEE7o8i8ZtQKAOS0kRrbsaLzAAhXFm6KRV0xB+FszlwXAbsM1fnkJLk0cBE3L9bde xjODR5dvh29gKNz/QaO/O7WkE4wP7fp8pobvNsrG9hIPaG93BnXni05Fq21MxOqnSF4g fIJ+AVYYV4ljkecEzYeE/mwUOwP+4OezGH3YOme87EOxsvo1I4B1mf1f1Uh01lz9buxz 6ayCWCn+o/xzGkf6gKrQ5qxLRAX2DqvJCWi5bCMTICm3IAkgdNYRj8CXKDBygKkYRhc1 3UV+xPFjRCA3Qx0ii4gy+Lr1WQu10ke1Q+RzJwIVmNhyW06EfLMvVPoM1jS3S4ohS00n +pUg== X-Forwarded-Encrypted: i=1; AJvYcCVBN7w9fmOVkqmimfQqUipW0bViUpYWLcwWOmEVBpu6ofgSaPln1n34K4de1on1teNr4FKnGPem/g==@kvack.org X-Gm-Message-State: AOJu0YzWuRBk0Gl+sH2vhCyvlNU01nNs9Uoi0N16AiVod/Pid+K2pX0n 97qkLYh6xiiuwZmIHpqi8kc6CFP6Cug2lmzg+Kkx4EDBUK3VNoE6y3UEDv917FGdwK3MBdHHTBT BHto4LQ== X-Google-Smtp-Source: AGHT+IHcROXBNbjH71/lURsLY2Z1G5CKon5Ljv9fq9orhUweiNnkkP9Uhr+rnjfYEZKZsBEOScmUDcRF+8Fv X-Received: from qtbcg3.prod.google.com ([2002:a05:622a:4083:b0:466:9f81:8c8c]) (user=bgeffon job=prod-delivery.src-stubby-dispatcher) by 2002:a05:622a:1389:b0:467:7fbf:d115 with SMTP id d75a77b69052e-467892a43f3mr8418101cf.12.1733866263990; Tue, 10 Dec 2024 13:31:03 -0800 (PST) Date: Tue, 10 Dec 2024 16:30:50 -0500 In-Reply-To: <20241210213050.2839638-1-bgeffon@google.com> Mime-Version: 1.0 References: <20241210213050.2839638-1-bgeffon@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241210213050.2839638-6-bgeffon@google.com> Subject: [RFC PATCH 5/5] selftests: mm: Add selftest for new_addr hint with MREMAP_MAYMOVE. From: Brian Geffon To: Andrew Morton Cc: Lorenzo Stoakes , Jann Horn , Vlastimil Babka , "Liam R. Howlett" , linux-mm@kvack.org, Marco Vanotti , linux-kernel@vger.kernel.org, Brian Geffon X-Rspamd-Queue-Id: 2D8F1C000C X-Stat-Signature: 484y6s1sid9a5c4bp9nua4sj4whg1at8 X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1733866254-398174 X-HE-Meta: U2FsdGVkX19t9OY3nBe2Sz5ZGD5VC/p7N0w8hlKTcUVhIIqe7zJC6cOsD2AdElCKQbSaSgvko1wuR37s3Be0yhDlytT2CSEHBupouEKKSpekMUFYJ6CrFajLOFKoWNjkzO5gZ380Fokv4hbJ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Lfw8vtSgENYj7kQFPPP8H0baK3tPjG8JcPwm/3A4uTn7m59r+B5bV6PjYT9BRFQpuLv+Yc6CmfXEc9E5LSJDSfuMo8lOEr2iwdPuBwYRokq6yJrfwOQcB78av5SkmsE22QENK2SkfhfGJ95ESTSkr+iokkdweputxFtlu9vCe+JqIBhEJEeWf6v0Lr2WuDAuAC/2vVVJG1HMDdD67hyyYDV+FhyB6QkvAcyVcYmPXasO8l2JzPUjNWPwocPS44Wv6QoA05SCCR3KajPPSXFIT8BVOyeDaUVhPW5sVuZs2DI84Na1Aa9ubnurKSSM+FTGHVea2Tk4B+T0QqX8BB/A0R/VN54kKPJlaMdUExaEgVPlH5lqcIhVRJqjEFB42QFfSo7Tpby/ZN5bk2a3ad6z1T0tIqyQzZFWxAC4ETaMw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.020596, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This selftest tries to validate that a hint will be used with MREMAP_MAYMOVE. Given it's just a hint we try to structure the test in a way where it will succeed. To ensure we can validate the behavior we create a single mapping and split it by unmapping the middle and we'll use that as a hint for MREMAP_MAYMOVE without using MREMAP_FIXED. Signed-off-by: Brian Geffon --- tools/testing/selftests/mm/mremap_test.c | 113 ++++++++++++++++++++++- 1 file changed, 112 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/mremap_test.c b/tools/testing/selftests/mm/mremap_test.c index 5a3a9bcba640..42ed869e2e01 100644 --- a/tools/testing/selftests/mm/mremap_test.c +++ b/tools/testing/selftests/mm/mremap_test.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -270,6 +271,115 @@ static void mremap_expand_merge(FILE *maps_fp, unsigned long page_size) ksft_test_result_fail("%s\n", test_name); } +/* + * This test validates that mremap(2) with MREMAP_MAYMOVE uses the new + * address as a hint. + */ +static void mremap_maymove_hint(FILE *maps_fp, unsigned long page_size) +{ + char *test_name = "mremap MAY_MOVE with hint"; + void *mapping_a, *mapping_b, *mapping_c, *remapped, *hint; + intptr_t base_map_addr = 0x8FF00000; + +#if !defined(__i386__) && !defined(__x86_64__) + /* + * This test is written with knowledge about the architecture specific behavior of + * get_unmapped_area(). For that reason this specific test is only applicable to x86. + */ + ksft_test_result_skip("%s\n", test_name); + return; +#endif + + /* + * To validate the behavior we'll use the following layout: + * + * | mapping a | | mapping b | mapping c | + * | 1 page | 10 pages unmapped | 2 pages mapped | 1 page mapped | + * + * To guarantee we can get this layout we'll do a single mmap and then + * munmap and mprotect accordingly, this will prevent the test from being + * flaky. + * + * We'll attempt to resize mapping b to 3 pages using MAYMOVE, because + * mapping c is beyond it it'll have to be moved. We will use mapping a + * as the hint to validate it lands just beyond it. The final result: + * + * | mapping a | | mapping b | | mapping c | + * | 1 page | 2 pages unmapped | 3 pages mapped | 7 pages unampped | 1 page mapped | + * + */ + mapping_a = mmap((void*)base_map_addr, 14 * page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + if (mapping_a == MAP_FAILED) { + ksft_print_msg("mmap failed: %s\n", strerror(errno)); + goto out_fail; + } + + mapping_b = (void*)((intptr_t)mapping_a + 11*page_size); + mapping_c = (void*)((intptr_t)mapping_a + 13*page_size); + + /* unmap the 10 pages after mapping a */ + munmap((void*)((intptr_t)(mapping_a) + page_size), 10*page_size); + + /* make mapping a and c PROT_NONE to complete the vma splitting */ + mprotect(mapping_a, page_size, PROT_NONE); + mprotect(mapping_c, page_size, PROT_NONE); + + /* + * Validate the split: mapping a, b, and c are mapped with a gap after 'a'. + */ + if (!is_range_mapped(maps_fp, (unsigned long)mapping_a, + (unsigned long)(mapping_a + page_size))) { + ksft_print_msg("mapping 'a' was not mapped at %p\n", mapping_a); + goto out_fail; + } + + if (is_range_mapped(maps_fp, (unsigned long)mapping_a + page_size, + (unsigned long)mapping_a + 10*page_size)) { + ksft_print_msg("unmapped area after mapping 'a' not found\n"); + goto out_fail; + } + + if (!is_range_mapped(maps_fp, (unsigned long)mapping_b, + (unsigned long)(mapping_b + 2*page_size))) { + ksft_print_msg("mapping 'b' was not mapped at %p\n", mapping_b); + goto out_fail; + } + + if (!is_range_mapped(maps_fp, (unsigned long)mapping_c, + (unsigned long)(mapping_c + page_size))) { + ksft_print_msg("mapping 'c' was not mapped at %p\n", mapping_c); + goto out_fail; + } + + /* + * Now try to mremap mapping 'b' using a hint, it will be increased in size + * so that the VMA must be moved. Bypass the glibc wrapper of mremap(2) becuase + * it will attempt to 0 the hint unless MREMAP_DONTUNMAP is set, for reference: + * https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=6c40cb0e9f893d49dc7caee580a055de53562206;hp=54252394c25ddf0062e288d4a6ab7a885f8ae009 + */ + hint = (void*)((intptr_t)mapping_a + 3*page_size); + remapped = (void*)syscall(SYS_mremap, mapping_b, 2*page_size, 3*page_size, MREMAP_MAYMOVE, hint); + if ((intptr_t)remapped != (intptr_t)hint) { + if (remapped == MAP_FAILED) + ksft_print_msg("remap of 'b' failed %s\n", strerror(errno)); + else + ksft_print_msg("mapping 'b' was unexpectedly remapped from %p to %p. expected: %p\n", + mapping_b, remapped, hint); + goto out_fail; + } + + munmap(mapping_a, page_size); + munmap(remapped, 3 * page_size); + munmap(mapping_c, page_size); + ksft_test_result_pass("%s\n", test_name); + return; + +out_fail: + 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 @@ -720,7 +830,7 @@ int main(int argc, char **argv) char *rand_addr; size_t rand_size; int num_expand_tests = 2; - int num_misc_tests = 2; + int num_misc_tests = 3; struct test test_cases[MAX_TEST] = {}; struct test perf_test_cases[MAX_PERF_TEST]; int page_size; @@ -842,6 +952,7 @@ int main(int argc, char **argv) mremap_expand_merge(maps_fp, page_size); mremap_expand_merge_offset(maps_fp, page_size); + mremap_maymove_hint(maps_fp, page_size); fclose(maps_fp);