From patchwork Thu Mar 30 16:08:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13194507 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 2D936C77B62 for ; Thu, 30 Mar 2023 16:08:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BD2ED900008; Thu, 30 Mar 2023 12:08:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B81C26B009D; Thu, 30 Mar 2023 12:08:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9D453900008; Thu, 30 Mar 2023 12:08:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 8A29A6B009C for ; Thu, 30 Mar 2023 12:08:38 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 55090161033 for ; Thu, 30 Mar 2023 16:08:38 +0000 (UTC) X-FDA: 80626047516.03.2E7C0E2 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf05.hostedemail.com (Postfix) with ESMTP id BA3E9100028 for ; Thu, 30 Mar 2023 16:08:35 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ST8XVy04; spf=pass (imf05.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.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=1680192515; 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=Roeuk409dPF2L2QvtyDeIj99sNqVdIN74Z+r6aghdT8=; b=BTPo/mb/9dm9s0VXDaQ+YWy7cQ7LMn1CpMGaRuY6lc6dEelhjaPcn1uM/FySqTEHZjXwMF D8L2xTNTHnxdO/Zxgp7eG1K7XKSAHDtQ66w0hpbinZJD0ECQCbYASGWYibRdMctTtbEbAL oh8rNbCbVw2pyjv2CS29bpW+R3MP25E= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ST8XVy04; spf=pass (imf05.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.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=1680192515; a=rsa-sha256; cv=none; b=qlNXxkvAJ6MDH0Wmb56AOHCJGffdmqAXpvGP+eep2VWyo9CS/LwPL+k8xYhrLEq2XvA363 1m+4gefWLAV3g4I0n4SFkLSQBjjjgXo6+sNiBfBzWlVPHUQ/Tx6TH8RJMp1gxdnkwbW1uI xD91o1ZZC12y3/VMjejOwm3fXo+qRAM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680192515; 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=Roeuk409dPF2L2QvtyDeIj99sNqVdIN74Z+r6aghdT8=; b=ST8XVy047m9ocDOzNXfkP0aTVQFr7Mx9/UImqWnUrq1TsJggztdmzWWqtzT2gc/xwVkNrH i4odHNGoZFPIo1oHgq6EVpAb/KAnQvjsjdXe5vWAcwmx0JvCvNt1hjfvPCmG1SupeQNzI8 lX2D+SB00gbxS1t+I4T2BymrxzfZnXo= 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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-619-xLuMN5Y7O6yb8pDAYZpBaQ-1; Thu, 30 Mar 2023 12:08:33 -0400 X-MC-Unique: xLuMN5Y7O6yb8pDAYZpBaQ-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-746bae78af3so60184185a.1 for ; Thu, 30 Mar 2023 09:08:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680192513; 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=Roeuk409dPF2L2QvtyDeIj99sNqVdIN74Z+r6aghdT8=; b=XJB0QUDdV2yJhV6E9JxrLL/eY32TcTRXk3c49BWA4SzzVPaxMO6t52cY3ABOpVwKNm RhBv2hwocRy8QaqYpqGZMl2iybTr/rXmdx9HsANU7t21aB03n2pQLizVPtRixFZ8tiDO o6XWfP6WSuaj+QO/vLXC+BHNuEuUxc1QDhVSzZuQuOJNJ8b36hO6IR3YQJN4hfpJA98q WQtOGlFsUoicUB2YGwfF3QRDJgKpdKd4dQDGed/jRwoNU/n0zMV7osssy2zC9jtRGGWD eA0EuDignQDsaTUkT95wxeSrVOp4+YxH8QP1LwFnzFfNRm8Plqqakui2wtY9LRvyIVEz h9EA== X-Gm-Message-State: AAQBX9dgW19piBCMR+WZ3KsmYlPbQY5YGgmYl0PVBzyftLvCM27ISr6u RZQGMniXnI/XQVgFW/jOXZF1bAjQ/E3TvHjV9Qflj1EHzEe1NPDjyIZH5w6Zi/4bS0/hhC2WOjz R159VgAfi/y0= X-Received: by 2002:a05:622a:199a:b0:3e4:eb39:eb8b with SMTP id u26-20020a05622a199a00b003e4eb39eb8bmr4380191qtc.5.1680192513135; Thu, 30 Mar 2023 09:08:33 -0700 (PDT) X-Google-Smtp-Source: AKy350Zj+eN5QPeQ5wVil623hZvzJrIMImI+/YUsY/jqTUUYPanRTL0PgneBxr/Nlg9DU4Nsp1MaQg== X-Received: by 2002:a05:622a:199a:b0:3e4:eb39:eb8b with SMTP id u26-20020a05622a199a00b003e4eb39eb8bmr4380140qtc.5.1680192512678; Thu, 30 Mar 2023 09:08:32 -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 s13-20020ac8758d000000b003e4e1ea3cb5sm3925874qtq.51.2023.03.30.09.08.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 09:08:32 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Kravetz , Mike Rapoport , Andrea Arcangeli , peterx@redhat.com, Leonardo Bras Soares Passos , Andrew Morton , David Hildenbrand , Axel Rasmussen , Nadav Amit Subject: [PATCH 25/29] selftests/mm: Move zeropage test into uffd unit tests Date: Thu, 30 Mar 2023 12:08:29 -0400 Message-Id: <20230330160829.3107591-1-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230330155707.3106228-1-peterx@redhat.com> References: <20230330155707.3106228-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Queue-Id: BA3E9100028 X-Stat-Signature: sk64opte1c7wj3dfghjtjykgmgusj4ec X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1680192515-908411 X-HE-Meta: U2FsdGVkX19mNrtPBKkUXwf7VLhxjy19/oDkinfOGTFvmNRwOcnwN7E2ZJe2DB/HyBpu0EuYNTCtDJwVD4FlnFoxSbNDlSIdPh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BizLKPn4yN+z0dLHJkRA2bZ/uMwHq3z952fSLZCePy2v35Bth6ut1qF1l7xAsdmaOmilHVo0r7JlKfeVl1Y/LGlPKvVNZjpeF1aAvRAsgNx00mDz9laMfqXsy29N//6kjKw/4Tfm5XwDZ9untcvj2DuWsjZFmKGQrrgYCXHQlHEKR0F2BJcfTb5UnV1bxpVhu88tFHYOUBTjVlPDpL7rLNx5gkvB+PHiKatikY/I8uoGDSyqFAs3yPS7a7m0md9MM7vdKUWRUSHaSdjDNceVVvyzmDCdwrXsjKDAc5y8UBHj 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: Simplifies it a bit along the way, e.g., drop the never used offset field (which was always the 1st page so offset=0). Signed-off-by: Peter Xu --- tools/testing/selftests/mm/uffd-stress.c | 94 +------------------- tools/testing/selftests/mm/uffd-unit-tests.c | 94 ++++++++++++++++++++ 2 files changed, 95 insertions(+), 93 deletions(-) diff --git a/tools/testing/selftests/mm/uffd-stress.c b/tools/testing/selftests/mm/uffd-stress.c index f3046ae13a90..a6f3609c1ad1 100644 --- a/tools/testing/selftests/mm/uffd-stress.c +++ b/tools/testing/selftests/mm/uffd-stress.c @@ -110,15 +110,6 @@ static inline uint64_t uffd_minor_feature(void) return 0; } -static int my_bcmp(char *str1, char *str2, size_t n) -{ - unsigned long i; - for (i = 0; i < n; i++) - if (str1[i] != str2[i]) - return 1; - return 0; -} - static void *locking_thread(void *arg) { unsigned long cpu = (unsigned long) arg; @@ -274,89 +265,6 @@ static int stress(struct uffd_stats *uffd_stats) return 0; } -static void retry_uffdio_zeropage(int ufd, - struct uffdio_zeropage *uffdio_zeropage, - unsigned long offset) -{ - uffd_test_ops->alias_mapping(&uffdio_zeropage->range.start, - uffdio_zeropage->range.len, - offset); - if (ioctl(ufd, UFFDIO_ZEROPAGE, uffdio_zeropage)) { - if (uffdio_zeropage->zeropage != -EEXIST) - err("UFFDIO_ZEROPAGE error: %"PRId64, - (int64_t)uffdio_zeropage->zeropage); - } else { - err("UFFDIO_ZEROPAGE error: %"PRId64, - (int64_t)uffdio_zeropage->zeropage); - } -} - -static int __uffdio_zeropage(int ufd, unsigned long offset) -{ - struct uffdio_zeropage uffdio_zeropage; - int ret; - bool has_zeropage = !(test_type == TEST_HUGETLB); - __s64 res; - - if (offset >= nr_pages * page_size) - err("unexpected offset %lu", offset); - uffdio_zeropage.range.start = (unsigned long) area_dst + offset; - uffdio_zeropage.range.len = page_size; - uffdio_zeropage.mode = 0; - ret = ioctl(ufd, UFFDIO_ZEROPAGE, &uffdio_zeropage); - res = uffdio_zeropage.zeropage; - if (ret) { - /* real retval in ufdio_zeropage.zeropage */ - if (has_zeropage) - err("UFFDIO_ZEROPAGE error: %"PRId64, (int64_t)res); - else if (res != -EINVAL) - err("UFFDIO_ZEROPAGE not -EINVAL"); - } else if (has_zeropage) { - if (res != page_size) { - err("UFFDIO_ZEROPAGE unexpected size"); - } else { - retry_uffdio_zeropage(ufd, &uffdio_zeropage, - offset); - return 1; - } - } else - err("UFFDIO_ZEROPAGE succeeded"); - - return 0; -} - -static int uffdio_zeropage(int ufd, unsigned long offset) -{ - return __uffdio_zeropage(ufd, offset); -} - -/* exercise UFFDIO_ZEROPAGE */ -static int userfaultfd_zeropage_test(void) -{ - printf("testing UFFDIO_ZEROPAGE: "); - fflush(stdout); - - uffd_test_ctx_init(0); - - if (uffd_register(uffd, area_dst, nr_pages * page_size, - true, test_uffdio_wp, false)) - err("register failure"); - - if (area_dst_alias) { - /* Needed this to test zeropage-retry on shared memory */ - if (uffd_register(uffd, area_dst_alias, nr_pages * page_size, - true, test_uffdio_wp, false)) - err("register failure"); - } - - if (uffdio_zeropage(uffd, 0)) - if (my_bcmp(area_dst, zeropage, page_size)) - err("zeropage is not zero"); - - printf("done.\n"); - return 0; -} - static int userfaultfd_stress(void) { void *area; @@ -468,7 +376,7 @@ static int userfaultfd_stress(void) uffd_stats_report(uffd_stats, nr_cpus); } - return userfaultfd_zeropage_test(); + return 0; } static void set_test_type(const char *type) diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/selftests/mm/uffd-unit-tests.c index ebf45cb0eca8..376dfa320b6f 100644 --- a/tools/testing/selftests/mm/uffd-unit-tests.c +++ b/tools/testing/selftests/mm/uffd-unit-tests.c @@ -663,7 +663,101 @@ static void uffd_events_wp_test(void) uffd_events_test_common(true); } +static void retry_uffdio_zeropage(int ufd, + struct uffdio_zeropage *uffdio_zeropage) +{ + uffd_test_ops->alias_mapping(&uffdio_zeropage->range.start, + uffdio_zeropage->range.len, + 0); + if (ioctl(ufd, UFFDIO_ZEROPAGE, uffdio_zeropage)) { + if (uffdio_zeropage->zeropage != -EEXIST) + err("UFFDIO_ZEROPAGE error: %"PRId64, + (int64_t)uffdio_zeropage->zeropage); + } else { + err("UFFDIO_ZEROPAGE error: %"PRId64, + (int64_t)uffdio_zeropage->zeropage); + } +} + +static bool do_uffdio_zeropage(int ufd, bool has_zeropage) +{ + struct uffdio_zeropage uffdio_zeropage = { 0 }; + int ret; + __s64 res; + + uffdio_zeropage.range.start = (unsigned long) area_dst; + uffdio_zeropage.range.len = page_size; + uffdio_zeropage.mode = 0; + ret = ioctl(ufd, UFFDIO_ZEROPAGE, &uffdio_zeropage); + res = uffdio_zeropage.zeropage; + if (ret) { + /* real retval in ufdio_zeropage.zeropage */ + if (has_zeropage) + err("UFFDIO_ZEROPAGE error: %"PRId64, (int64_t)res); + else if (res != -EINVAL) + err("UFFDIO_ZEROPAGE not -EINVAL"); + } else if (has_zeropage) { + if (res != page_size) + err("UFFDIO_ZEROPAGE unexpected size"); + else + retry_uffdio_zeropage(ufd, &uffdio_zeropage); + return true; + } else + err("UFFDIO_ZEROPAGE succeeded"); + + return false; +} + +/* exercise UFFDIO_ZEROPAGE */ +static void uffd_zeropage_test_common(bool has_zeropage) +{ + if (uffd_register(uffd, area_dst, page_size, + true, false, false)) + err("register"); + + if (area_dst_alias) + if (uffd_register(uffd, area_dst_alias, page_size, + true, false, false)) + err("register"); + + if (do_uffdio_zeropage(uffd, has_zeropage)) { + int i; + + for (i = 0; i < page_size; i++) + if (area_dst[i] != 0) + err("data non-zero at offset %d\n", i); + } + + + if (uffd_unregister(uffd, area_dst, page_size * nr_pages)) + err("unregister"); + + uffd_test_pass(); +} + +static void uffd_zeropage_test(void) +{ + uffd_zeropage_test_common(true); +} + +static void uffd_zeropage_hugetlb_test(void) +{ + uffd_zeropage_test_common(false); +} + uffd_test_case_t uffd_tests[] = { + { + .name = "zeropage", + .uffd_fn = uffd_zeropage_test, + .mem_targets = MEM_ANON | MEM_SHMEM | MEM_SHMEM_PRIVATE, + .uffd_feature_required = 0, + }, + { + .name = "zeropage-hugetlb", + .uffd_fn = uffd_zeropage_hugetlb_test, + .mem_targets = MEM_HUGETLB | MEM_HUGETLB_PRIVATE, + .uffd_feature_required = 0, + }, { .name = "pagemap", .uffd_fn = uffd_pagemap_test,