From mboxrd@z Thu Jan 1 00:00:00 1970 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 96BFFC77B78 for ; Mon, 17 Apr 2023 19:53:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 269038E0001; Mon, 17 Apr 2023 15:53:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 23F1B6B0085; Mon, 17 Apr 2023 15:53:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0B99A8E0001; Mon, 17 Apr 2023 15:53: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 ED20F6B0083 for ; Mon, 17 Apr 2023 15:53:35 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id EDD97406A6 for ; Mon, 17 Apr 2023 19:53:33 +0000 (UTC) X-FDA: 80691932706.28.03E7FD5 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.145.221.124]) by imf24.hostedemail.com (Postfix) with ESMTP id D0BCE180008 for ; Mon, 17 Apr 2023 19:53:31 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Tf8u88+e; spf=fail (imf24.hostedemail.com: domain of peterx@redhat.com does not designate 216.145.221.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681761211; a=rsa-sha256; cv=none; b=D8ZcCawecMep8gPpsXQiFawGsbLsS21L3cY+GUEK99yB9T+9M0uqLmOziUUEADWM5EKPS7 9QcZlSGbbRqSGxSeNF+wYkg9655gxUfgSyIFBv9MBYduOpX4WKnQmVkhFf5In5UNOtPPA2 XTNPqXp7UaKz6GbITtJTM74lSxdc5CQ= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Tf8u88+e; spf=fail (imf24.hostedemail.com: domain of peterx@redhat.com does not designate 216.145.221.124 as permitted sender) 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=1681761211; 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=PiB4O8c7FzfgLpxlGuxco4WipLi6p7uHP+fG5udokS8=; b=bKARL3IrexsppLzPgsHthXh5jBWTTgcgQuviAuzUWURQXem9//1eWm8GsUomTF7KSUus4s iKgpitvFmPOstO3SUznqxAqR6Ynd1EmDYt5Gbv1aTy9DmSaeLm27XNy0GdmQSIvNE0LImZ +GILUGeBDoMh4Ea3uc4Cf/HC4IW9V4k= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681761211; 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=PiB4O8c7FzfgLpxlGuxco4WipLi6p7uHP+fG5udokS8=; b=Tf8u88+epEWJXGLRprZSoxk4cdAPRPY0rewzIs+tX3j2WbnLQLGiMmgtImxlUymoKTlAn6 wCaqxBG1XjINeCoc621shbki4U7jn4qOMsNPH0dxjZ5emXoJ8EA667KJOQyue/4GICqgCm hTHjEbz3zzAdiErONminIFE6BWub8jo= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-2-CZ1nmkKgMUOL7c-j2WG8TQ-1; Mon, 17 Apr 2023 15:53:30 -0400 X-MC-Unique: CZ1nmkKgMUOL7c-j2WG8TQ-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-74ad69ad9a1so62327785a.0 for ; Mon, 17 Apr 2023 12:53:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681761209; x=1684353209; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PiB4O8c7FzfgLpxlGuxco4WipLi6p7uHP+fG5udokS8=; b=KCOLtjp4r5l5RhTLDFcRW/tJ6/wlY7XkzyRNI3Wfq6E4q0HhIDrbq6yLTaRc8U3juF 1Kmhv5HI+sZZc0PqRUMFYfs+E9Skwdkm4FMR4jdk+jfbKeyuUxq0RpYAwAois9gsgtb9 I5XlWLDQZGiHE5ODZT/D0VKWcoZa7j/hAUT/h7EEh8j1NOrBGleswhwffha1azztdY2B 7gUb2o58aNR3iOTw66idCvgmQjO81RQKgjwnpQ+oC984PTmSh1aMtY/nG9nfekIomUrN xdW+s52QqpmO/m+jaWTwckF5OaBwNmMxWmUpozy0QWP7r6Z6iUPUvI1zeyoeTkKaSW15 6URA== X-Gm-Message-State: AAQBX9dUTZxDFX2ddsPPbF1HuiWvx5ax/oYc1MnaM3h7jxQHRO2WCYA4 ZzPOR2TFUNhbIpESn4hBWPrWOcSc8NxekqV2CJ3ZhIpYwawEKQ0TiRwjpk4q9cbcwfC9z1f4v51 /vHhNK5+rCBQ= X-Received: by 2002:a05:622a:1352:b0:3ef:37fa:e1d6 with SMTP id w18-20020a05622a135200b003ef37fae1d6mr1229501qtk.2.1681761209320; Mon, 17 Apr 2023 12:53:29 -0700 (PDT) X-Google-Smtp-Source: AKy350Y/Xr56P7wXtTgHgR9HpSg1x1E8ea69mUaTjl3roEaHjKyTAe98r0gM0V7aTgCT/jxwZH4gBA== X-Received: by 2002:a05:622a:1352:b0:3ef:37fa:e1d6 with SMTP id w18-20020a05622a135200b003ef37fae1d6mr1229477qtk.2.1681761209061; Mon, 17 Apr 2023 12:53:29 -0700 (PDT) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-40-70-52-229-124.dsl.bell.ca. [70.52.229.124]) by smtp.gmail.com with ESMTPSA id r17-20020ac87ef1000000b003edfb5d7637sm1731278qtc.73.2023.04.17.12.53.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Apr 2023 12:53:28 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Kravetz , Andrea Arcangeli , =?UTF-8?q?Mika=20Penttil=C3=A4?= , Andrew Morton , peterx@redhat.com, Axel Rasmussen , Nadav Amit , David Hildenbrand Subject: [PATCH v2 4/6] selftests/mm: Extend and rename uffd pagemap test Date: Mon, 17 Apr 2023 15:53:15 -0400 Message-Id: <20230417195317.898696-5-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230417195317.898696-1-peterx@redhat.com> References: <20230417195317.898696-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Rspam-User: X-Rspamd-Queue-Id: D0BCE180008 X-Rspamd-Server: rspam01 X-Stat-Signature: 8f4daec3izog7n7ed9wdsn1u1sz9cj1q X-HE-Tag: 1681761211-290510 X-HE-Meta: U2FsdGVkX1+HnZrGhq1auyMD4pRCCq5kuE9H3g2oPEaIjuvJUepNlT2xXwXrNf2QcCbMETiozShCbHa9rzXpqeeXmvgnVzC+ul/flHioZ6q3vC9gJU7ZSyDEV8+A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72N0HcfWrN++FWs0AInHDkZE+aG+diwws4X2D3/uBigeqZ4ZApaUGWL99NyO24KE2kEO1TkrMNsKtgjPV8bJ7mJ6tsOg1lldFkjVMozr4OMLYvQ5PPNrd98D/UJ1erBBybLjM/Ca+iyTvHMnR2QAa4w1eunGesQ0pDL/78Q3M9y3ORPsk5ZaIqR8GkFffKmrqbqBS9iPka9bXPSzBm8MgJ8XCXpGnFx6tEk8Cyy9c5pbxzwQLr0TC+HqaumRxiEV3t+5VJZJLtlql3K3ov9UitRLnvNYeyNSd8VJB6kTcFQ80ztIJhQxgxbt3hT3fbAcRYpzatzb1g+SQXx1naSCWzuVbJpgPDMK8bd/F6V+6BwkyifUVV8M3maT8Jq3Q 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: Extend it to all types of mem, meanwhile add one parallel test when EVENT_FORK is enabled, where uffd-wp bits should be persisted rather than dropped. Since at it, rename the test to "wp-fork" to better show what it means. Making the new test called "wp-fork-with-event". Before: Testing pagemap on anon... done After: Testing wp-fork on anon... done Testing wp-fork on shmem... done Testing wp-fork on shmem-private... done Testing wp-fork on hugetlb... done Testing wp-fork on hugetlb-private... done Testing wp-fork-with-event on anon... done Testing wp-fork-with-event on shmem... done Testing wp-fork-with-event on shmem-private... done Testing wp-fork-with-event on hugetlb... done Testing wp-fork-with-event on hugetlb-private... done Signed-off-by: Peter Xu --- tools/testing/selftests/mm/uffd-unit-tests.c | 130 +++++++++++++++---- 1 file changed, 106 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/selftests/mm/uffd-unit-tests.c index 452ca05a829d..739fc4d30342 100644 --- a/tools/testing/selftests/mm/uffd-unit-tests.c +++ b/tools/testing/selftests/mm/uffd-unit-tests.c @@ -227,25 +227,65 @@ static int pagemap_open(void) err("pagemap uffd-wp bit error: 0x%"PRIx64, value); \ } while (0) -static int pagemap_test_fork(bool present) +typedef struct { + int parent_uffd, child_uffd; +} fork_event_args; + +static void *fork_event_consumer(void *data) { - pid_t child = fork(); + fork_event_args *args = data; + struct uffd_msg msg = { 0 }; + + /* Read until a full msg received */ + while (uffd_read_msg(args->parent_uffd, &msg)); + + if (msg.event != UFFD_EVENT_FORK) + err("wrong message: %u\n", msg.event); + + /* Just to be properly freed later */ + args->child_uffd = msg.arg.fork.ufd; + return NULL; +} + +static int pagemap_test_fork(int uffd, bool with_event) +{ + fork_event_args args = { .parent_uffd = uffd, .child_uffd = -1 }; + pthread_t thread; + pid_t child; uint64_t value; int fd, result; + /* Prepare a thread to resolve EVENT_FORK */ + if (with_event) { + if (pthread_create(&thread, NULL, fork_event_consumer, &args)) + err("pthread_create()"); + } + + child = fork(); if (!child) { /* Open the pagemap fd of the child itself */ fd = pagemap_open(); value = pagemap_get_entry(fd, area_dst); /* - * After fork() uffd-wp bit should be gone as long as we're - * without UFFD_FEATURE_EVENT_FORK + * After fork(), we should handle uffd-wp bit differently: + * + * (1) when with EVENT_FORK, it should persist + * (2) when without EVENT_FORK, it should be dropped */ - pagemap_check_wp(value, false); + pagemap_check_wp(value, with_event); /* Succeed */ exit(0); } waitpid(child, &result, 0); + + if (with_event) { + if (pthread_join(thread, NULL)) + err("pthread_join()"); + if (args.child_uffd < 0) + err("Didn't receive child uffd"); + close(args.child_uffd); + } + return result; } @@ -295,7 +335,8 @@ static void uffd_wp_unpopulated_test(uffd_test_args_t *args) uffd_test_pass(); } -static void uffd_pagemap_test(uffd_test_args_t *args) +static void uffd_wp_fork_test_common(uffd_test_args_t *args, + bool with_event) { int pagemap_fd; uint64_t value; @@ -311,23 +352,42 @@ static void uffd_pagemap_test(uffd_test_args_t *args) wp_range(uffd, (uint64_t)area_dst, page_size, true); value = pagemap_get_entry(pagemap_fd, area_dst); pagemap_check_wp(value, true); - /* Make sure uffd-wp bit dropped when fork */ - if (pagemap_test_fork(true)) - err("Detected stall uffd-wp bit in child"); - - /* Exclusive required or PAGEOUT won't work */ - if (!(value & PM_MMAP_EXCLUSIVE)) - err("multiple mapping detected: 0x%"PRIx64, value); + if (pagemap_test_fork(uffd, with_event)) { + uffd_test_fail("Detected %s uffd-wp bit in child in present pte", + with_event ? "missing" : "stall"); + goto out; + } - if (madvise(area_dst, page_size, MADV_PAGEOUT)) - err("madvise(MADV_PAGEOUT) failed"); + /* + * This is an attempt for zapping the pgtable so as to test the + * markers. + * + * For private mappings, PAGEOUT will only work on exclusive ptes + * (PM_MMAP_EXCLUSIVE) which we should satisfy. + * + * For shared, PAGEOUT may not work. Use DONTNEED instead which + * plays a similar role of zapping (rather than freeing the page) + * to expose pte markers. + */ + if (args->mem_type->shared) { + if (madvise(area_dst, page_size, MADV_DONTNEED)) + err("MADV_DONTNEED"); + } else { + /* + * NOTE: ignore retval because private-hugetlb doesn't yet + * support swapping, so it could fail. + */ + madvise(area_dst, page_size, MADV_PAGEOUT); + } /* Uffd-wp should persist even swapped out */ value = pagemap_get_entry(pagemap_fd, area_dst); pagemap_check_wp(value, true); - /* Make sure uffd-wp bit dropped when fork */ - if (pagemap_test_fork(false)) - err("Detected stall uffd-wp bit in child"); + if (pagemap_test_fork(uffd, with_event)) { + uffd_test_fail("Detected %s uffd-wp bit in child in zapped pte", + with_event ? "missing" : "stall"); + goto out; + } /* Unprotect; this tests swap pte modifications */ wp_range(uffd, (uint64_t)area_dst, page_size, false); @@ -338,9 +398,21 @@ static void uffd_pagemap_test(uffd_test_args_t *args) *area_dst = 2; value = pagemap_get_entry(pagemap_fd, area_dst); pagemap_check_wp(value, false); - - close(pagemap_fd); uffd_test_pass(); +out: + if (uffd_unregister(uffd, area_dst, nr_pages * page_size)) + err("unregister failed"); + close(pagemap_fd); +} + +static void uffd_wp_fork_test(uffd_test_args_t *args) +{ + uffd_wp_fork_test_common(args, false); +} + +static void uffd_wp_fork_with_event_test(uffd_test_args_t *args) +{ + uffd_wp_fork_test_common(args, true); } static void check_memory_contents(char *p) @@ -836,10 +908,20 @@ uffd_test_case_t uffd_tests[] = { .uffd_feature_required = 0, }, { - .name = "pagemap", - .uffd_fn = uffd_pagemap_test, - .mem_targets = MEM_ANON, - .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP, + .name = "wp-fork", + .uffd_fn = uffd_wp_fork_test, + .mem_targets = MEM_ALL, + .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP | + UFFD_FEATURE_WP_HUGETLBFS_SHMEM, + }, + { + .name = "wp-fork-with-event", + .uffd_fn = uffd_wp_fork_with_event_test, + .mem_targets = MEM_ALL, + .uffd_feature_required = UFFD_FEATURE_PAGEFAULT_FLAG_WP | + UFFD_FEATURE_WP_HUGETLBFS_SHMEM | + /* when set, child process should inherit uffd-wp bits */ + UFFD_FEATURE_EVENT_FORK, }, { .name = "wp-unpopulated", -- 2.39.1