Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp5573452rwe; Tue, 18 Apr 2023 08:32:36 -0700 (PDT) X-Google-Smtp-Source: AKy350ZSDkSURt1eBxOId+VsONcTbEkW2RsRKV7GSHW8rgOL7IifK6fOuNH/XfcEbhjhV77XFg9j X-Received: by 2002:a05:6a20:1604:b0:f0:86ef:d8b3 with SMTP id l4-20020a056a20160400b000f086efd8b3mr208551pzj.23.1681831955631; Tue, 18 Apr 2023 08:32:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681831955; cv=none; d=google.com; s=arc-20160816; b=uz2gmSgmLciXUddKeeTiS9cZTTEAW5KtviA4ckxzS4uJBTV20O/HDQ/p3wBtMB0X7F TWgxbGoKjNN9uN7L5X+C/2wJMpl973H5a40ZjvpnL/Rnp14pqVd8Qx85RKDvNP7i6V2E VM6EvtsXFCT1AevLLsBk3BUfmv4yNUmhm2ogOZDnPrYrgBbHnwM3zBFV0TKbwozjGuvx za1KwKpMuWmCKJYpB+uvd2S1cVANbehyFJjuLACwf/nN/D5LBZJYe8h7DGNRQ76QxJy1 NOx//nsEGMO8WONd0pDjxolkM5JGn9o3/cS/Krm4cVXCq5Iosqd8kEOoajNaImqeApaG F5jw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=O7hwLzEYfyCC+1HV0+CQuKr6XrxBkPdmXIv3+pwAsAU=; b=Q5VVpiawXZ5stn/5QDsjf58hwNIRVgv+xufR6f/91rAkPtqd67gKo8ahRaPjB0X1pi VPyPHXAxXuoeDl6vxEd0maHMzGAeQFqw0nmw8pNPyS3zNob4goRjZzR4XMJUVIn3syQX coi8BxBea/B9jXqGEPlRxEAxxTsgsgRVFQqJXGef2dayTmSGPfT93kqmP1YEQA4H0Nly kIn06v6+GKHW8yLt3ULHW4coqipR1qVIcZjad7d5OEKd1GwXIfeAamvyUq0SodjDjIoT juPnAZC73uOWGest5V8NQD1N9OyUsC+XXDOWHtiVM5tZhwThsAon8V7NtXccmVDT14/b 7hRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bmfoqXYv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 141-20020a630093000000b0051f32c847a7si1783166pga.449.2023.04.18.08.32.16; Tue, 18 Apr 2023 08:32:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bmfoqXYv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231882AbjDRPak (ORCPT + 99 others); Tue, 18 Apr 2023 11:30:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45824 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231701AbjDRPaf (ORCPT ); Tue, 18 Apr 2023 11:30:35 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 152A01FCA for ; Tue, 18 Apr 2023 08:29:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681831744; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=O7hwLzEYfyCC+1HV0+CQuKr6XrxBkPdmXIv3+pwAsAU=; b=bmfoqXYvEjJIxh9L2VOAgzyxW6hxU1TVQEii7fKesGNn04IKohG0D51mitHh4XoyMpvVNG dMloIUPNiWlTzyb14lzh6OjvhpEUE8rU8n71TdfgDBBUJnwhBKh3DHUChk1ii7zUu9aCZT 4RiNPu6hRGq9t3fzWT+/LpOAcH23SAw= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-283-tx5a0GuAONunj9TrLi1nxQ-1; Tue, 18 Apr 2023 11:29:01 -0400 X-MC-Unique: tx5a0GuAONunj9TrLi1nxQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 048921C0A58F; Tue, 18 Apr 2023 15:29:00 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.194.149]) by smtp.corp.redhat.com (Postfix) with ESMTP id B148314152F6; Tue, 18 Apr 2023 15:28:57 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-s390@vger.kernel.org, linux-kselftest@vger.kernel.org, David Hildenbrand , Andrew Morton , Stefan Roesch , Rik van Riel , Johannes Weiner , Michal Hocko , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Heiko Carstens , Vasily Gorbik , Sven Schnelle , Shuah Khan Subject: [PATCH v1 2/3] selftests/ksm: ksm_functional_tests: add prctl unmerge test Date: Tue, 18 Apr 2023 17:28:48 +0200 Message-Id: <20230418152849.505124-3-david@redhat.com> In-Reply-To: <20230418152849.505124-1-david@redhat.com> References: <20230418051342.1919757-1-shr@devkernel.io> <20230418152849.505124-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Let's test whether setting PR_SET_MEMORY_MERGE to 0 after setting it to 1 will unmerge pages, similar to how setting MADV_UNMERGEABLE after setting MADV_MERGEABLE would. Signed-off-by: David Hildenbrand --- .../selftests/mm/ksm_functional_tests.c | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c index 7bc9fc17c9f0..26853badae70 100644 --- a/tools/testing/selftests/mm/ksm_functional_tests.c +++ b/tools/testing/selftests/mm/ksm_functional_tests.c @@ -91,9 +91,10 @@ static int ksm_merge(void) return 0; } -static char *mmap_and_merge_range(char val, unsigned long size) +static char *mmap_and_merge_range(char val, unsigned long size, bool use_prctl) { char *map; + int ret; map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); @@ -110,7 +111,17 @@ static char *mmap_and_merge_range(char val, unsigned long size) /* Make sure each page contains the same values to merge them. */ memset(map, val, size); - if (madvise(map, size, MADV_MERGEABLE)) { + + if (use_prctl) { + ret = prctl(PR_SET_MEMORY_MERGE, 1, 0, 0, 0); + if (ret < 0 && errno == EINVAL) { + ksft_test_result_skip("PR_SET_MEMORY_MERGE not supported\n"); + goto unmap; + } else if (ret) { + ksft_test_result_fail("PR_SET_MEMORY_MERGE=1 failed\n"); + goto unmap; + } + } else if (madvise(map, size, MADV_MERGEABLE)) { ksft_test_result_fail("MADV_MERGEABLE failed\n"); goto unmap; } @@ -133,7 +144,7 @@ static void test_unmerge(void) ksft_print_msg("[RUN] %s\n", __func__); - map = mmap_and_merge_range(0xcf, size); + map = mmap_and_merge_range(0xcf, size, false); if (map == MAP_FAILED) return; @@ -155,7 +166,7 @@ static void test_unmerge_discarded(void) ksft_print_msg("[RUN] %s\n", __func__); - map = mmap_and_merge_range(0xcf, size); + map = mmap_and_merge_range(0xcf, size, false); if (map == MAP_FAILED) return; @@ -187,7 +198,7 @@ static void test_unmerge_uffd_wp(void) ksft_print_msg("[RUN] %s\n", __func__); - map = mmap_and_merge_range(0xcf, size); + map = mmap_and_merge_range(0xcf, size, false); if (map == MAP_FAILED) return; @@ -323,9 +334,31 @@ static void test_prctl_fork(void) ksft_test_result_pass("PR_SET_MEMORY_MERGE value is inherited\n"); } +static void test_prctl_unmerge(void) +{ + const unsigned int size = 2 * MiB; + char *map; + + ksft_print_msg("[RUN] %s\n", __func__); + + map = mmap_and_merge_range(0xcf, size, true); + if (map == MAP_FAILED) + return; + + if (prctl(PR_SET_MEMORY_MERGE, 0, 0, 0, 0)) { + ksft_test_result_fail("PR_SET_MEMORY_MERGE=0 failed\n"); + goto unmap; + } + + ksft_test_result(!range_maps_duplicates(map, size), + "Pages were unmerged\n"); +unmap: + munmap(map, size); +} + int main(int argc, char **argv) { - unsigned int tests = 4; + unsigned int tests = 5; int err; #ifdef __NR_userfaultfd @@ -355,6 +388,7 @@ int main(int argc, char **argv) test_prctl(); test_prctl_fork(); + test_prctl_unmerge(); err = ksft_get_fail_cnt(); if (err) -- 2.39.2