From patchwork Wed Apr 12 16:44:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13209362 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 7F685C7619A for ; Wed, 12 Apr 2023 16:44:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1C92190000C; Wed, 12 Apr 2023 12:44:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 17A8E900003; Wed, 12 Apr 2023 12:44:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 019EF90000C; Wed, 12 Apr 2023 12:44:18 -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 E70FC900003 for ; Wed, 12 Apr 2023 12:44:18 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id C241A1A01B2 for ; Wed, 12 Apr 2023 16:44:18 +0000 (UTC) X-FDA: 80673311796.11.D814946 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf10.hostedemail.com (Postfix) with ESMTP id AB9FBC001F for ; Wed, 12 Apr 2023 16:44:16 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=M2TQoZGh; spf=pass (imf10.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.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=1681317856; 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=tbgfGOfUxiSSCaOkyoncCcfniesjTCna7R8zm3hOkOs=; b=DQKUmFB6UjuUp7JmPLMyZO1vXBbZh2VR5nBJohikMRWPaKO/mkSI1lQtnFoITZPcqxGBHn w62mXPilKkg5OmcvciQKEqe7JdjDjGNt6qRQPqDhuBr3EQm5sxQueI/GEWg2qfigD9sPQT fk0erh4AVSQ1nUjSO/nux1aeLbRU1zM= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=M2TQoZGh; spf=pass (imf10.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.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=1681317856; a=rsa-sha256; cv=none; b=dEEK0+56aSU/F0xmigTxdso9KH06Igqk9uPodGuolUSWgTJljrvDvCsUxQWIfvqXxaEveF GirpUBKWsVqOeC6UAqSAOap7ssqiVU31duxZvsLvFkXzsqZV+6OenMVVhH9aghxB7fiwI2 nTC3ztzESrF6eGFNGHEQcZpg1WPw2Pw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681317855; 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=tbgfGOfUxiSSCaOkyoncCcfniesjTCna7R8zm3hOkOs=; b=M2TQoZGhSAoR3MYaHIuFSFyti8YGvBhj8gEZFYvR7VegPPnTioYA3P2Edyu4vTlxK5HPKd QSh3I9mg2sOyWpnPDgcKiq5GZ1AYN56foMekzCjI2waDzgTbb2UE9AHH3iXS1onJZK9EOv EyWdkCigabP9HrKoD6R68jCC+HWS/WU= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-398-Dc3UNBZZMO2PhJp2vacRvA-1; Wed, 12 Apr 2023 12:44:13 -0400 X-MC-Unique: Dc3UNBZZMO2PhJp2vacRvA-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-74accc750c1so3424685a.0 for ; Wed, 12 Apr 2023 09:44:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681317850; x=1683909850; 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=tbgfGOfUxiSSCaOkyoncCcfniesjTCna7R8zm3hOkOs=; b=KgUhUv6BLVXfLib/JD0xoNcPebU3YUQfcNCsMl/m+1lDY4VfAWoF9sLqsu/efZ26iG 4h6vRNT4pMjzwivROn+FoSMxuw6iOtgv7z9DdbNfyF8obpWxv7Ht4zUL/RY02fhGw6PO WZaVNR9OACETZr8Yy+uOmsvjufHLECP1dfYxo4/I7UG/gsAQ8fAUDKShI7xqra/2YWUY vfRKQesWt8WAmgZ7oh6RZkzFYfPqCpErku17r34o9GCmwLhzqLMaYd/APonnsbG2mFVD F9yIk4ldsivtiy/BlVkKaK5bKEaV8xpHxy1ocS7a/6OM8+su25qjhkulbB7C+UfAi09z QGTw== X-Gm-Message-State: AAQBX9eAMGiEWfXI0E1LxFW1lfjUGS8YJkyvo1o3APBRtqmGbluJMU4t Tbf9+GuOk/+yT9xsI9aVr0yN0/kYDfzpms6UzrLRp0l6yYW9olBetnRW4vuzFnMZ/G5PWZIFiCj //ZvPv7DTvAY= X-Received: by 2002:a05:6214:528e:b0:5a5:e941:f33d with SMTP id kj14-20020a056214528e00b005a5e941f33dmr4707104qvb.3.1681317850514; Wed, 12 Apr 2023 09:44:10 -0700 (PDT) X-Google-Smtp-Source: AKy350YgYkqzXY4OPOAVlwyIH89dfszaQ4CVLKQFhdriMwZPec01lMU25gulp094+Es9DxvsBiKi5Q== X-Received: by 2002:a05:6214:528e:b0:5a5:e941:f33d with SMTP id kj14-20020a056214528e00b005a5e941f33dmr4707078qvb.3.1681317850234; Wed, 12 Apr 2023 09:44:10 -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 b4-20020a0cfb44000000b005dd8b93457csm2915318qvq.20.2023.04.12.09.44.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Apr 2023 09:44:09 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: David Hildenbrand , Andrew Morton , Leonardo Bras Soares Passos , peterx@redhat.com, Andrea Arcangeli , Nadav Amit , Mike Rapoport , Axel Rasmussen , Mike Kravetz Subject: [PATCH v2 26/31] selftests/mm: Move zeropage test into uffd unit tests Date: Wed, 12 Apr 2023 12:44:04 -0400 Message-Id: <20230412164404.328815-1-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230412163922.327282-1-peterx@redhat.com> References: <20230412163922.327282-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: AB9FBC001F X-Stat-Signature: s6e444byha6dfc3qc5q4igp64yfrncuc X-HE-Tag: 1681317856-237141 X-HE-Meta: U2FsdGVkX19aOTIc50uqTg0NBRm1ZGdPKcPTzryjMGVEDhVPZPYZKKFT4jLpw/R2xn7USwoCk4jegG6uCERyaDAr5CrYJ4kGySA8BE/VPmHMZr+GSZsXTDLj6j7O20G6zKW+k2db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W+EFPoNNtzDv79rzYceMH+HQaF7yGlFjaigZwZi830OdnrmvIhZmeBKyjc8jUkrfg9L9nbP/hKrP7Kw9a3dEGeSkJ8qoSFAlXcgkjVpHKxcfk4sTWzPGDtTpiVXve9X5WdwGExbvQhlftDLTOyEz5M1X7fukPjtId18je/7YZlJk1q9+kdOSelr/Wn6mPm3U0Up+iN3uxNnrZPfCg32YBIyoiyu5rbeW/WCUERvoPJrKqLHSQFcTBji 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). Introduce uffd_register_with_ioctls() out of uffd_register() to detect uffdio_register.ioctls got returned. Check that automatically when testing UFFDIO_ZEROPAGE on different types of memory (and kernel). Signed-off-by: Peter Xu --- tools/testing/selftests/mm/uffd-stress.c | 94 +------------------- tools/testing/selftests/mm/uffd-unit-tests.c | 93 +++++++++++++++++++ tools/testing/selftests/mm/vm_util.c | 14 ++- tools/testing/selftests/mm/vm_util.h | 2 + 4 files changed, 108 insertions(+), 95 deletions(-) diff --git a/tools/testing/selftests/mm/uffd-stress.c b/tools/testing/selftests/mm/uffd-stress.c index ce51180238d8..d78f88850011 100644 --- a/tools/testing/selftests/mm/uffd-stress.c +++ b/tools/testing/selftests/mm/uffd-stress.c @@ -109,15 +109,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; @@ -273,89 +264,6 @@ static int stress(struct uffd_args *args) 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; @@ -467,7 +375,7 @@ static int userfaultfd_stress(void) uffd_stats_report(args, 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 94549696f4b2..160bd8ccda55 100644 --- a/tools/testing/selftests/mm/uffd-unit-tests.c +++ b/tools/testing/selftests/mm/uffd-unit-tests.c @@ -660,7 +660,100 @@ 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; +} + +/* + * Registers a range with MISSING mode only for zeropage test. Return true + * if UFFDIO_ZEROPAGE supported, false otherwise. Can't use uffd_register() + * because we want to detect .ioctls along the way. + */ +static bool +uffd_register_detect_zeropage(int uffd, void *addr, uint64_t len) +{ + uint64_t ioctls = 0; + + if (uffd_register_with_ioctls(uffd, addr, len, true, + false, false, &ioctls)) + err("zeropage register fail"); + + return ioctls & (1 << _UFFDIO_ZEROPAGE); +} + +/* exercise UFFDIO_ZEROPAGE */ +static void uffd_zeropage_test(void) +{ + bool has_zeropage; + int i; + + has_zeropage = uffd_register_detect_zeropage(uffd, area_dst, page_size); + if (area_dst_alias) + /* Ignore the retval; we already have it */ + uffd_register_detect_zeropage(uffd, area_dst_alias, page_size); + + if (do_uffdio_zeropage(uffd, has_zeropage)) + 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)) + err("unregister"); + + if (area_dst_alias && uffd_unregister(uffd, area_dst_alias, page_size)) + err("unregister"); + + uffd_test_pass(); +} + uffd_test_case_t uffd_tests[] = { + { + .name = "zeropage", + .uffd_fn = uffd_zeropage_test, + .mem_targets = MEM_ALL, + .uffd_feature_required = 0, + }, { .name = "pagemap", .uffd_fn = uffd_pagemap_test, diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c index 1bc0ceb01adb..9b06a5034808 100644 --- a/tools/testing/selftests/mm/vm_util.c +++ b/tools/testing/selftests/mm/vm_util.c @@ -198,8 +198,9 @@ unsigned long default_huge_page_size(void) return hps; } -int uffd_register(int uffd, void *addr, uint64_t len, - bool miss, bool wp, bool minor) +/* If `ioctls' non-NULL, the allowed ioctls will be returned into the var */ +int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len, + bool miss, bool wp, bool minor, uint64_t *ioctls) { struct uffdio_register uffdio_register = { 0 }; uint64_t mode = 0; @@ -218,10 +219,19 @@ int uffd_register(int uffd, void *addr, uint64_t len, if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1) ret = -errno; + else if (ioctls) + *ioctls = uffdio_register.ioctls; return ret; } +int uffd_register(int uffd, void *addr, uint64_t len, + bool miss, bool wp, bool minor) +{ + return uffd_register_with_ioctls(uffd, addr, len, + miss, wp, minor, NULL); +} + int uffd_unregister(int uffd, void *addr, uint64_t len) { struct uffdio_range range = { .start = (uintptr_t)addr, .len = len }; diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h index 634eb2f41145..b950bd16083a 100644 --- a/tools/testing/selftests/mm/vm_util.h +++ b/tools/testing/selftests/mm/vm_util.h @@ -52,6 +52,8 @@ int uffd_open_dev(unsigned int flags); int uffd_open_sys(unsigned int flags); int uffd_open(unsigned int flags); int uffd_get_features(uint64_t *features); +int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len, + bool miss, bool wp, bool minor, uint64_t *ioctls); /* * On ppc64 this will only work with radix 2M hugepage size