From patchwork Thu Apr 13 23:11:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13210721 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 B0E8BC77B61 for ; Thu, 13 Apr 2023 23:11:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 49B1D280002; Thu, 13 Apr 2023 19:11:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 424D6280001; Thu, 13 Apr 2023 19:11:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 29ECB280002; Thu, 13 Apr 2023 19:11:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 1C424280001 for ; Thu, 13 Apr 2023 19:11:36 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id DA919140497 for ; Thu, 13 Apr 2023 23:11:35 +0000 (UTC) X-FDA: 80677916550.30.10ABF76 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf06.hostedemail.com (Postfix) with ESMTP id B5CE218000E for ; Thu, 13 Apr 2023 23:11:33 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=JuIvPqfd; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf06.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681427493; 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=Cj8IkuyftFS4nNtJVxOzgDd0ZAbstFz3mPgyDW94Wyq6X/JHYcNmAge6KXc5+9AlZpk+WG 4V6QaO5TZPYzpKhr3+LxcnNn6em41FhPanZn8w94eMfWHZHJCy7j0xWBp8yAu+Q3byeeQf FBzoPrcFyRF7uNFYB+/CVK/Dnkbn++Q= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=JuIvPqfd; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf06.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681427493; a=rsa-sha256; cv=none; b=f37A9CYi6wYIV+Z2nOP+xPZjDG0B3LEq/UYarcIk/JBtenbw6CPhKmyGAvtoyPmCf10P5p qp/M3/ymXssdb5rb9bssLfhQjLrRzkQRR2jQSfLvmji+EOdzahhNn64ym2AMgshafNEmuE uWs2BiA5OGhpUGVQVx0FKJyVJCafsHc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681427493; 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=JuIvPqfd8MUj3XlrvhNJ04Ljv3pgjIq2eZOtSwqcS6dPe3qBCLCxuRJTv7gO4fEoJmNrC1 l47Y7bHmfzLf7UTPOqIxr0xuP3uPoexl644XjMViNd77Pbq/6sTD22cl+hwajGXuoVFJt+ VOqmgeiZiVTxUrIRF+Qrh1CPKI+dKN0= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-440-fBPtmP9ZN960Wx1xecQKKg-1; Thu, 13 Apr 2023 19:11:32 -0400 X-MC-Unique: fBPtmP9ZN960Wx1xecQKKg-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-3eab4df3ef0so822631cf.0 for ; Thu, 13 Apr 2023 16:11:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681427491; x=1684019491; 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=mDDmaaqpZNV+xeFc64MlOXQWbWzczNYbCJG0aBdcbLqowyZ4CmViYRIieryAF5v6Hr t+/cYp15364kMoswzqApZLaJrn0rfB1/IZdnoVSEHL7AL3qWyjkKk5Kxo6NTNctacQqt arANIoTYGCrvSd+LsRkRPsa58S2khyooFFYzLF2UhGg+4xgSJB/GONnyZ6BmcNZ3zqPI XXk/D1QPzIVp02xctVCgFTVuhv7UYa25bGO65Bh2vwA6na0S/ZwdVQnTsx21KvmpcR9L sA+dhYzHN9HlZMFYcLsaQR5EePSZVlxkYhfAP6JzkTFeTyqRhQI8mvVu1fIIgYce8qla yw7w== X-Gm-Message-State: AAQBX9fkoDhBTWvNlb7BZYj0ulGcDTkBx/t8R28DOzIOccV2XGKq4PUE 1vZVJyjKrBTuCHpo15LFmAg2b+IfwMiWd+qwFfBCLpESBWTzVjmYP0JvoHmt+WTtGd2ID4E1vHI /OmGKtuy2alM= X-Received: by 2002:ac8:5813:0:b0:3e6:707e:d3b2 with SMTP id g19-20020ac85813000000b003e6707ed3b2mr1243716qtg.0.1681427491441; Thu, 13 Apr 2023 16:11:31 -0700 (PDT) X-Google-Smtp-Source: AKy350YVqAYBJtjYRo4m3UT95yOx2hm06W56ReVZ3RTAjywmwDlKXwFvUxElkGFKWsmcn3tNMj/n2g== X-Received: by 2002:ac8:5813:0:b0:3e6:707e:d3b2 with SMTP id g19-20020ac85813000000b003e6707ed3b2mr1243678qtg.0.1681427491125; Thu, 13 Apr 2023 16:11:31 -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 g3-20020ac81243000000b003ea1b97acfasm612446qtj.49.2023.04.13.16.11.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Apr 2023 16:11:29 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Axel Rasmussen , Andrew Morton , David Hildenbrand , peterx@redhat.com, Mike Kravetz , Nadav Amit , Andrea Arcangeli Subject: [PATCH 4/6] selftests/mm: Extend and rename uffd pagemap test Date: Thu, 13 Apr 2023 19:11:18 -0400 Message-Id: <20230413231120.544685-5-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230413231120.544685-1-peterx@redhat.com> References: <20230413231120.544685-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Queue-Id: B5CE218000E X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: oqrpp896481ebinx8xo6wqf3mwsox738 X-HE-Tag: 1681427493-806514 X-HE-Meta: U2FsdGVkX19FYPX0XfhtAQQAZHPHH8e4aiTNsZWW8whLiI6l0YmPXXHmXofq+V05752zbz0Ty13O/I+H0qqKOywjxwjgARZxzLnTphMp6tbsfjLrWL9CWDh5XAIuAFsnNvooEbCaNA7tdainkrRb5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HsG/MrF7Vbo82XfPDJo0hSVaceeaEy6TzvkEtVDGzuzdvwq7DXAEt6huTxbaiC7U2K/2tNN0NLEZn1LdVfoS6d+wPOsG0Zx2L1q6bYOKl3zS83/YL/xjGzKHjFUN2RZ0+0ft4VtqBxPEAzBH732aeozJ77qJPhrgf+lgD2YmiEQsAu+sHCbiibvfglZhN4MkuIjHUjSQcwMsepcdjtjhID66pj/U1gq3sl30DrXt1Mtyf7pZ6BQCTWDek0g 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",