From patchwork Mon Jul 25 14:20:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12928121 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 5AC96C43334 for ; Mon, 25 Jul 2022 14:21:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F1A9F900003; Mon, 25 Jul 2022 10:21:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ECA5B900002; Mon, 25 Jul 2022 10:21:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D6AF3900003; Mon, 25 Jul 2022 10:21:03 -0400 (EDT) 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 C6811900002 for ; Mon, 25 Jul 2022 10:21:03 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 9AA29A053E for ; Mon, 25 Jul 2022 14:21:03 +0000 (UTC) X-FDA: 79725834006.29.CCF8FAF Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf11.hostedemail.com (Postfix) with ESMTP id 33E1B400AD for ; Mon, 25 Jul 2022 14:21:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1658758862; 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=STjP3t9YbLJwojrLrn71AA0kDkPsgXvwkazzQEZtuik=; b=WSntHzKcjzumJi+/bfPa1ZxJZNlEOHa7Pqh5TJq++eHreDcUkcB9eSnov7y9KiTuAnky+t f8pNUZkkg4HEqX2IwBEJeg3OaFFeSNRKA0sD9/1y/9I/nEzj+t3txq2z1KjMvdEn2JQK23 rlfee3IHN04rLatFFSwbJDlIUv+a2fU= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-668-O7RiQIpYNPqBzTnS2lPAgQ-1; Mon, 25 Jul 2022 10:20:53 -0400 X-MC-Unique: O7RiQIpYNPqBzTnS2lPAgQ-1 Received: by mail-qk1-f199.google.com with SMTP id v13-20020a05620a0f0d00b006b5f0ec742eso9848623qkl.2 for ; Mon, 25 Jul 2022 07:20:53 -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=STjP3t9YbLJwojrLrn71AA0kDkPsgXvwkazzQEZtuik=; b=GhXxd4g6jRTgfSRv+hXxbZZFYn1Mtyc+ia+dPNL99+xueC0Avl3TFU8M9n5C0Rw/tQ rrtjjOXrXec0jeVjMBUYTO5DN8Ej2VjxC2ZiwpFYCabuBhU79fLMMq1Y6g2AJAzjX5Ph 0IXuGy9bMyGlbfbsf+V8PSf18RcWz04OoD65Wlt4e32/Mna0JchRAKHFvw81a7bAMOUX fPGVvmym3IMt2J6QWsj4qvAkl2jWna8FYb8htLqoH1MopQsyVwPBA+tDucwN6THy7lHH xkMLOQjVFxKezWXsfpqf6vumRe4z3wN2g5Sw3t8JVFQCMuJCEuoLkyJ0WwTQvXdurV3a g4aA== X-Gm-Message-State: AJIora8khXXt+rvGYv+jgXpZeLD15IS3GpUadTRYITazfqB0xwLTHJ44 WIMFn265B9YBCzdu8UMB5nPgtJCKK+PgGLw6oSpoXOQIwXfKyvoSdQyG/MvU4btyrmY4V7PSmXa qETaxrobVMrU= X-Received: by 2002:ac8:5c4d:0:b0:31e:f794:1b18 with SMTP id j13-20020ac85c4d000000b0031ef7941b18mr10146897qtj.282.1658758853390; Mon, 25 Jul 2022 07:20:53 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sStdRCgHvQrB2KOO87tQRFjODWSSk/7IEOUYRzTexVJXfNYA2gVOTDz2PPoGKw5RZmrvYmsQ== X-Received: by 2002:ac8:5c4d:0:b0:31e:f794:1b18 with SMTP id j13-20020ac85c4d000000b0031ef7941b18mr10146880qtj.282.1658758853153; Mon, 25 Jul 2022 07:20:53 -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 ey14-20020a05622a4c0e00b0031f0ab4eceasm7626071qtb.7.2022.07.25.07.20.51 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 25 Jul 2022 07:20:52 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: peterx@redhat.com, Nadav Amit , Andrea Arcangeli , Andrew Morton , David Hildenbrand Subject: [PATCH v4 2/3] selftests: soft-dirty: Add test for mprotect Date: Mon, 25 Jul 2022 10:20:47 -0400 Message-Id: <20220725142048.30450-3-peterx@redhat.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220725142048.30450-1-peterx@redhat.com> References: <20220725142048.30450-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-type: text/plain ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1658758863; 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=STjP3t9YbLJwojrLrn71AA0kDkPsgXvwkazzQEZtuik=; b=cl8AvMomTJPwWjFxdrhQrBt8rZ4gI+zYwdSKP1YeTE3iAW/FegIP11GAJgKtwpO9EmdkRD WIaq8qLifRy0/Qdz1PJpNWLENOE//Bs5Eok7QNtJ/5BxsuwenvRMnDjkSoTyjj9kbAP/5i L8REmtw/xLiqQSJNvkHsWRs2+LVPySA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1658758863; a=rsa-sha256; cv=none; b=0/0r3gj+XVbzyAiOU3HEHbgUAlAUrAbYq6A0CccGdOL23x+b6HwQTL1gnc8VVESBOqsa8H MUCoFa+9/SPO6RSyWy+E+ofjR6ggY3NvmGziYPRugvESTDG0G9ePWNjIQ96qFLos/X2K4E Ez+MYl8P4mZvZbORvc/Hw6PUoQJz+bc= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WSntHzKc; spf=none (imf11.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 Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=WSntHzKc; spf=none (imf11.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-Rspam-User: X-Rspamd-Queue-Id: 33E1B400AD X-Rspamd-Server: rspam05 X-Stat-Signature: pneib9bdhxi17ayyp778i9tzqdbuk3kz X-HE-Tag: 1658758862-51425 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-dirty test cases for mprotect() on both anon or file. Signed-off-by: Peter Xu Reviewed-by: David Hildenbrand --- tools/testing/selftests/vm/soft-dirty.c | 67 ++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/vm/soft-dirty.c b/tools/testing/selftests/vm/soft-dirty.c index 08ab62a4a9d0..e3a43f5d4fa2 100644 --- a/tools/testing/selftests/vm/soft-dirty.c +++ b/tools/testing/selftests/vm/soft-dirty.c @@ -121,13 +121,76 @@ 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 { + test_fd = open(fname, O_RDWR | O_CREAT); + if (test_fd < 0) { + ksft_test_result_skip("Test %s open() file failed\n", __func__); + return; + } + unlink(fname); + 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); +} + +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 +201,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);