From patchwork Sat Apr 22 20:54:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13221120 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 A280FC77B78 for ; Sat, 22 Apr 2023 20:56:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0C40B6B0072; Sat, 22 Apr 2023 16:56:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 075C96B0074; Sat, 22 Apr 2023 16:56:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E7DCF6B0075; Sat, 22 Apr 2023 16:56:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D462D6B0072 for ; Sat, 22 Apr 2023 16:56:36 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 8F47EC01A9 for ; Sat, 22 Apr 2023 20:56:36 +0000 (UTC) X-FDA: 80710235592.25.4846787 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf02.hostedemail.com (Postfix) with ESMTP id DC46480003 for ; Sat, 22 Apr 2023 20:56:34 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=aMnj9mBp; spf=pass (imf02.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682196994; 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:in-reply-to:references:references:dkim-signature; bh=Ui2WFc3XFE2+tQTFbxU0igT43p1ZENYfzH1ADoeycVQ=; b=WocEmkIqp/tQaC0vNL/FhAG492JdMpClUQpCYMyjog2EbLV7eeaXQFKixntlxPo5UZrTIh wqq//4xMhNFevxLTVnoO8yw8D1i4fL+YiysAcI65d8zrAEe3zRPRjtDZa997kVxs+SfJAH PBD9xYL8ntLIggn/DXT2SOViF+xsZOM= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=aMnj9mBp; spf=pass (imf02.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682196994; a=rsa-sha256; cv=none; b=NnU4RzsekxXrouVAoSK0vMdG1N2k8f3Srl4cWzp5Tv9RKAoSrrLF6t+iy7RFBFYUzYat1M ei+0HOzuy7DjlXlCs6P5RoiSqybghRbo9lIl99wGBEmDiXkWVcx/AsAVohEL9/7q9pAc7N LZUThw+qOaZhZ0lBh/APnSGjn7Ko5VU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682196994; 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=Ui2WFc3XFE2+tQTFbxU0igT43p1ZENYfzH1ADoeycVQ=; b=aMnj9mBpEe1hRn+HWbO2vwzciNRyVLZVWMXctjQ1mwEx8Rz1kNlvUCPfNd5JDeMv7fw3qg nXL65UNHPTKwiXRzT6/w8fuh5g7bWdN0P/eF6dyfAY7fn11Ims3DaEM/kmMCC7L39QeGLL FSSjnEG5WDAsocBBx7uWD7QkJA6LaHQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-517-1KOLu_GvNNudAnb8kvSgFA-1; Sat, 22 Apr 2023 16:54:43 -0400 X-MC-Unique: 1KOLu_GvNNudAnb8kvSgFA-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0CA0D884EC4; Sat, 22 Apr 2023 20:54:43 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.192.6]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6C40E492B03; Sat, 22 Apr 2023 20:54:39 +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 mm-stable v2 2/3] selftests/ksm: ksm_functional_tests: add prctl unmerge test Date: Sat, 22 Apr 2023 22:54:19 +0200 Message-Id: <20230422205420.30372-3-david@redhat.com> In-Reply-To: <20230422205420.30372-1-david@redhat.com> References: <20230422205420.30372-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Stat-Signature: tzo7d4fn6yiyzf44dxy5acq8aubsbatp X-Rspam-User: X-Rspamd-Queue-Id: DC46480003 X-Rspamd-Server: rspam06 X-HE-Tag: 1682196994-201069 X-HE-Meta: U2FsdGVkX19h/y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cmSjTBJEwVxakg8ouYsEhZ10YRQf/cYyTelm7WiEqp+/QBmdnAfO7a+zl20xuz4fUFVkeuUyGrgRkadylJ1quZEKU5OHqYBa/5wzDP9k3nQbEfMpZ+sHb96rwWHa5NYPm1SRiS8bh 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: 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. Acked-by: Stefan Roesch 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)