From patchwork Thu Jul 21 18:33:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12925685 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 41377C43334 for ; Thu, 21 Jul 2022 18:33:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A14CE6B0073; Thu, 21 Jul 2022 14:33:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9C4F78E0002; Thu, 21 Jul 2022 14:33:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 83DED8E0001; Thu, 21 Jul 2022 14:33:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 783336B0073 for ; Thu, 21 Jul 2022 14:33:46 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 4F1A540ABE for ; Thu, 21 Jul 2022 18:33:46 +0000 (UTC) X-FDA: 79711955652.16.04709E7 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf02.hostedemail.com (Postfix) with ESMTP id CDA2E8005B for ; Thu, 21 Jul 2022 18:33:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1658428425; h=from:from: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nbIFjjgDA4ooaBqJdaxSYkeX6CtTRCPcitxkJ2jJ7zw=; b=fu2/XqBvmXDQ2TkRqQa8RnSAE3XepU07P6CCUQnUcg7npyhiu/BQ4CWQN/OeMjt56J+J0A jiroxlZRR9cMh6JRI1sX1hLI7JmuSpiuxLDyMQ2knljhi2SnPLhiePy0JBLl4DAccsiPMI poWyaT1R4CYf8uFsCSwLdpElzPunW68= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-592-1lbUNxXoMyGVgWZ5x27W3Q-1; Thu, 21 Jul 2022 14:33:44 -0400 X-MC-Unique: 1lbUNxXoMyGVgWZ5x27W3Q-1 Received: by mail-qv1-f70.google.com with SMTP id od5-20020a0562142f0500b00473838e0feeso1548204qvb.9 for ; Thu, 21 Jul 2022 11:33:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nbIFjjgDA4ooaBqJdaxSYkeX6CtTRCPcitxkJ2jJ7zw=; b=16NIDD/QzShbZz23SDmM9pr6onJqT2Yeq6jwym4DkuSWGUtABc5Jk5B7KpkMdlXecS GZQdIGhhPEAf1c35Ru9CpMTMfLKb7+pgPtZbTR9tURV7+KU3nJF8Vgnwc7A+e0n7AM+h eb4xVcmvMc5sGjwYibKbzTtRJlKIHi09KcfTuaDRMYI9RjGSsvhvSkIDXUGo7wqboA5o RcsejMdTHoUSHwwZHK0ZAj1huDGrrWnjmFhTUk9XYOWNuf25kfceenBPHi9an8ORKimd CeJsns70iUohjWR6Fsvq9PAEgoDfKRdaqRzFNTbGqgAHQK0LCi1u/QgcyiRuzF6l/WVC bYwQ== X-Gm-Message-State: AJIora/00UD1z6DH5n6VlTQ+6Cy+Q8gTWOWGkQparTOf/7vGNlkqDVNh dY6fhUjoynNryV2R6eEGFKxn1ponFmNXA3yDPxW79dq5ljc48nJv4JKJcw5VZVepVRFgdhOKwH+ 7ZPrLe4qg/1s= X-Received: by 2002:a05:620a:4512:b0:6b6:2fe:6dc3 with SMTP id t18-20020a05620a451200b006b602fe6dc3mr9582936qkp.124.1658428423927; Thu, 21 Jul 2022 11:33:43 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vyEE7j8jfEYZlnYclQKsnWvrL6a7mIg2DOWksGCUaTXhUXJZfkl0sPpefsfJuH4H4kzX7CcQ== X-Received: by 2002:a05:620a:4512:b0:6b6:2fe:6dc3 with SMTP id t18-20020a05620a451200b006b602fe6dc3mr9582910qkp.124.1658428423448; Thu, 21 Jul 2022 11:33:43 -0700 (PDT) Received: from localhost.localdomain (bras-base-aurron9127w-grc-35-70-27-3-10.dsl.bell.ca. [70.27.3.10]) by smtp.gmail.com with ESMTPSA id bj9-20020a05620a190900b006a6a7b4e7besm1878519qkb.109.2022.07.21.11.33.42 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 21 Jul 2022 11:33:42 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Nadav Amit , peterx@redhat.com, Andrew Morton , David Hildenbrand , Andrea Arcangeli Subject: [PATCH v3 2/3] selftests: soft-dirty: Add test for mprotect Date: Thu, 21 Jul 2022 14:33:37 -0400 Message-Id: <20220721183338.27871-3-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220721183338.27871-1-peterx@redhat.com> References: <20220721183338.27871-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-type: text/plain ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1658428425; a=rsa-sha256; cv=none; b=iHmPnVbDkbxsm0yUtg8Hvqu7sybzx3RvU78r+YkCeHvhtp3UEexLWB41Y/ID//fjOjIjq6 H7EPOzMNL5s/pI7rPb+RQhdNc86mR20ggUZmq/7PhvPOXKOru8ERZ3LtyGDz3LnvgAsr4J b763X3gUA+pbJbg741u+yg2+laVsaKo= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="fu2/XqBv"; spf=none (imf02.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@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=1658428425; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=nbIFjjgDA4ooaBqJdaxSYkeX6CtTRCPcitxkJ2jJ7zw=; b=8icFLQjOEa7XwDxBPQB1I1ymL+MyO11cIjpiolWWGYIhowfmIkTzAwkldfah+oOZKBM9YY chbWg2MV9hKQX9pmloc/fPFj8rC9Nq9uusMgwxM7Jhz3YRFu6TEEkVKxAeblzYK/J2MO92 aHHXtpChODd1/SqbzXGs5C7U+WPRvZU= Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="fu2/XqBv"; spf=none (imf02.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: CDA2E8005B X-Stat-Signature: qnpxsjwqqzgtb9p43ho5ayiq8qrf7b65 X-Rspam-User: X-HE-Tag: 1658428425-784669 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: Add two soft-diryt test cases for mprotect() on both anon or file. Signed-off-by: Peter Xu --- tools/testing/selftests/vm/soft-dirty.c | 69 ++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/vm/soft-dirty.c b/tools/testing/selftests/vm/soft-dirty.c index 08ab62a4a9d0..7d93906aa43f 100644 --- a/tools/testing/selftests/vm/soft-dirty.c +++ b/tools/testing/selftests/vm/soft-dirty.c @@ -121,13 +121,78 @@ static void test_hugepage(int pagemap_fd, int pagesize) free(map); } +static void test_mprotect(int pagemap_fd, int pagesize, bool anon) +{ + const char *type[] = {"file", "anon"}; + const char *fname = "./soft-dirty-test-file"; + int test_fd; + char *map; + + if (anon) { + map = mmap(NULL, pagesize, PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); + if (!map) + ksft_exit_fail_msg("anon mmap failed\n"); + } else { + unlink(fname); + test_fd = open(fname, O_RDWR | O_CREAT); + if (test_fd < 0) { + ksft_test_result_skip("Test %s huge page allocation\n", __func__); + return; + } + ftruncate(test_fd, pagesize); + map = mmap(NULL, pagesize, PROT_READ|PROT_WRITE, + MAP_SHARED, test_fd, 0); + if (!map) + ksft_exit_fail_msg("file mmap failed\n"); + } + + *map = 1; + ksft_test_result(pagemap_is_softdirty(pagemap_fd, map) == 1, + "Test %s-%s dirty bit of new written page\n", + __func__, type[anon]); + clear_softdirty(); + ksft_test_result(pagemap_is_softdirty(pagemap_fd, map) == 0, + "Test %s-%s soft-dirty clear after clear_refs\n", + __func__, type[anon]); + mprotect(map, pagesize, PROT_READ); + ksft_test_result(pagemap_is_softdirty(pagemap_fd, map) == 0, + "Test %s-%s soft-dirty clear after marking RO\n", + __func__, type[anon]); + mprotect(map, pagesize, PROT_READ|PROT_WRITE); + ksft_test_result(pagemap_is_softdirty(pagemap_fd, map) == 0, + "Test %s-%s soft-dirty clear after marking RW\n", + __func__, type[anon]); + *map = 2; + ksft_test_result(pagemap_is_softdirty(pagemap_fd, map) == 1, + "Test %s-%s soft-dirty after rewritten\n", + __func__, type[anon]); + + munmap(map, pagesize); + + if (!anon) { + close(test_fd); + unlink(fname); + } +} + +static void test_mprotect_anon(int pagemap_fd, int pagesize) +{ + test_mprotect(pagemap_fd, pagesize, true); +} + +static void test_mprotect_file(int pagemap_fd, int pagesize) +{ + test_mprotect(pagemap_fd, pagesize, false); +} + int main(int argc, char **argv) { int pagemap_fd; int pagesize; ksft_print_header(); - ksft_set_plan(5); + ksft_set_plan(15); pagemap_fd = open(PAGEMAP_FILE_PATH, O_RDONLY); if (pagemap_fd < 0) @@ -138,6 +203,8 @@ int main(int argc, char **argv) test_simple(pagemap_fd, pagesize); test_vma_reuse(pagemap_fd, pagesize); test_hugepage(pagemap_fd, pagesize); + test_mprotect_anon(pagemap_fd, pagesize); + test_mprotect_file(pagemap_fd, pagesize); close(pagemap_fd);