From patchwork Wed Apr 12 16:43:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13209358 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 7CB5BC77B6F for ; Wed, 12 Apr 2023 16:43:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21794900008; Wed, 12 Apr 2023 12:43:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1C93A900003; Wed, 12 Apr 2023 12:43:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 041B9900008; Wed, 12 Apr 2023 12:43:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E89D5900003 for ; Wed, 12 Apr 2023 12:43:56 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id BF6FB1601B2 for ; Wed, 12 Apr 2023 16:43:56 +0000 (UTC) X-FDA: 80673310872.09.17D35D2 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf24.hostedemail.com (Postfix) with ESMTP id B0D2C18000C for ; Wed, 12 Apr 2023 16:43:54 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=SkEdKnCi; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf24.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.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=1681317834; 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=ujW8sSFyaGmImGgAfN35Mc7lSxeHHva64g4oK3AlUaQ=; b=xmag6AaGyTso7046hDtzrC3FX9XRfzKBZlhU2Rnq86pGq/jz7IeYYuM4BWnUtOm3mSZn6G 02CqC/SU0rR/tw31CN6+YHoKCU+mVeZKknHo2p09yvJiUbPOXFlO+7eQ6DxW9ZXoa6Aznf wU3sIExUS8u5cvC1OdZtsDG+O050E/w= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=SkEdKnCi; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf24.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681317834; a=rsa-sha256; cv=none; b=VkajbWzA/XiPdIUKEGmSBi425ZGJiZ6WRyBMSjWuxE18vdRp/xqlheaI03yxD9hZZuuZ7/ 7rcEsq+yLIAziaDYsz+26foKyI4LnE8uOL8E/TTGz5/72wdY6sZ3at0teB4LJioxFTCiKv Gcjgx0a9PnlREqsIXlD1gZ+hpl5/uBM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681317833; 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=ujW8sSFyaGmImGgAfN35Mc7lSxeHHva64g4oK3AlUaQ=; b=SkEdKnCirYzy1FQJ9fplYfUhkRnenuDaPMhlVpxouhsrR7FH3AdUXggG0w4u/pPEJUHrTi M4Pejg8rMCaP0k/vilmUUd17dj1fv11S+1vVVnrrL/NmbuaIyYruW35zUdl2ZO2toKJLmu 4dEoN+4eJsKgXhmNVbO38TYnXvmgnj0= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-61-EUsiUzK7NVGKwVJ8jKRsJg-1; Wed, 12 Apr 2023 12:43:52 -0400 X-MC-Unique: EUsiUzK7NVGKwVJ8jKRsJg-1 Received: by mail-qt1-f200.google.com with SMTP id d75a77b69052e-3e948dfe9c9so364941cf.0 for ; Wed, 12 Apr 2023 09:43:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681317832; x=1683909832; 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=ujW8sSFyaGmImGgAfN35Mc7lSxeHHva64g4oK3AlUaQ=; b=abqDozlGIGnga3MKfeq+Ym2nruSD3ZFtal5760BpF3ZiE5Q4Q2zg0BcQ1m9ehggwjX jbIqpcvq2ZDcQUFfWtqna50Xds74TUXLKLVuNj5Q/b5DVRBF4QrYwb70yaXF6TnrYaVN 5u/KNyCtPG9aHGGbeeEkwayhV07YXRuA+XAiAsozFwnVA2eky8VGkMP5oT5rCu5LmW4H uC6YPN85u3GZywSL0LaMM46QwSsTaF/GMmW2WIsOayr4z/Nd68ItdXyxa+XFql/2wKUK omP3yPJywJBvVTTWDBLBNlTG+pnl/cJrj4Bg46JravzDvKm2n77Ag2NEySB5KgFQrlXQ rHog== X-Gm-Message-State: AAQBX9cuipjmWteK/u+vhY7GD4+rp2rKZKMjR2PJcz6eNMv2Zsq6Wc7/ reEBEFMJBv4fZozouW9FxezKcn82PLYwOnMD+lqM/FNjBUSg4nWxJCUGUV3YnTQGOXxOgvd8Bjp JFxKKY+WRjQM= X-Received: by 2002:a05:622a:50d:b0:3e3:1d31:e37 with SMTP id l13-20020a05622a050d00b003e31d310e37mr4709079qtx.1.1681317832366; Wed, 12 Apr 2023 09:43:52 -0700 (PDT) X-Google-Smtp-Source: AKy350aZLU+frgjQvwfaxCNQHkNm3VqYZWvrTQZf2mrndcXgxwvn/h9oIaDpV8SbGuCpWFsDqkjI3A== X-Received: by 2002:a05:622a:50d:b0:3e3:1d31:e37 with SMTP id l13-20020a05622a050d00b003e31d310e37mr4709058qtx.1.1681317832132; Wed, 12 Apr 2023 09:43:52 -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 h2-20020ac87442000000b003e3914c6839sm4369508qtr.43.2023.04.12.09.43.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Apr 2023 09:43:51 -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 22/31] selftests/mm: Add framework for uffd-unit-test Date: Wed, 12 Apr 2023 12:43:48 -0400 Message-Id: <20230412164348.328710-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: rspam02 X-Rspamd-Queue-Id: B0D2C18000C X-Stat-Signature: kde4td8mxkobbjasp7g3mt5qpepsobmd X-HE-Tag: 1681317834-407081 X-HE-Meta: U2FsdGVkX186T2J1KjOHnhsJr5m2tVhLObSqauds6gMMJWVFp9R3Ht5rwHjm/TyM7muaX7H0xlP0kFNzExWohuSgXtsBUC/JX764JIHvUyioSR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ipBYrsFB9ZgXjkrZ053JCUaZxEzpIxmaKjGkF4cRLsmWzcX/1MAZEsmvfyeU/M4T+W6PwYUI6zVfdLvO9wiqDzhtjku+nCs8VNAKFvVMDHAsexZU/gJlz8j1DzgqtQRSblJvpC0n89/56YZWB/RmjVRzJQsFW5FvRwOjDak4HNlCvfYRhbXkZRghXsMrzMw50Vh2YTn5SzTAGOOlefnCdCWl4mOeDlifqY4wZfbT5EB6oTSBMS5gdSv1QfuMDsJXiyHus1/ZccoBHaO3XgfLtDioW3Kgl/YE 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 a framework to be prepared to move unit tests from uffd-stress.c into uffd-unit-tests.c. The goal is to allow detection of uffd features for each test, and also loop over specified types of memory that a test support. Signed-off-by: Peter Xu --- tools/testing/selftests/mm/uffd-unit-tests.c | 124 +++++++++++++++++++ tools/testing/selftests/mm/vm_util.c | 37 ++++++ tools/testing/selftests/mm/vm_util.h | 2 + 3 files changed, 163 insertions(+) diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/selftests/mm/uffd-unit-tests.c index bb492c258486..936b24a6f468 100644 --- a/tools/testing/selftests/mm/uffd-unit-tests.c +++ b/tools/testing/selftests/mm/uffd-unit-tests.c @@ -9,6 +9,66 @@ #ifdef __NR_userfaultfd +/* The unit test doesn't need a large or random size, make it 32MB for now */ +#define UFFD_TEST_MEM_SIZE (32UL << 20) + +#define MEM_ANON BIT_ULL(0) +#define MEM_SHMEM BIT_ULL(1) +#define MEM_SHMEM_PRIVATE BIT_ULL(2) +#define MEM_HUGETLB BIT_ULL(3) +#define MEM_HUGETLB_PRIVATE BIT_ULL(4) + +struct mem_type { + const char *name; + unsigned int mem_flag; + uffd_test_ops_t *mem_ops; + bool shared; +}; +typedef struct mem_type mem_type_t; + +mem_type_t mem_types[] = { + { + .name = "anon", + .mem_flag = MEM_ANON, + .mem_ops = &anon_uffd_test_ops, + .shared = false, + }, + { + .name = "shmem", + .mem_flag = MEM_SHMEM, + .mem_ops = &shmem_uffd_test_ops, + .shared = true, + }, + { + .name = "shmem-private", + .mem_flag = MEM_SHMEM_PRIVATE, + .mem_ops = &shmem_uffd_test_ops, + .shared = false, + }, + { + .name = "hugetlb", + .mem_flag = MEM_HUGETLB, + .mem_ops = &hugetlb_uffd_test_ops, + .shared = true, + }, + { + .name = "hugetlb-private", + .mem_flag = MEM_HUGETLB_PRIVATE, + .mem_ops = &hugetlb_uffd_test_ops, + .shared = false, + }, +}; + +/* Returns: UFFD_TEST_* */ +typedef void (*uffd_test_fn)(void); + +typedef struct { + const char *name; + uffd_test_fn uffd_fn; + unsigned int mem_targets; + uint64_t uffd_feature_required; +} uffd_test_case_t; + static void uffd_test_report(void) { printf("Userfaults unit tests: pass=%u, skip=%u, fail=%u (total=%u)\n", @@ -105,9 +165,50 @@ static int test_uffd_api(bool use_dev) return 1; } +/* + * This function initializes the global variables. TODO: remove global + * vars and then remove this. + */ +static int uffd_setup_environment(uffd_test_case_t *test, mem_type_t *mem_type) +{ + map_shared = mem_type->shared; + uffd_test_ops = mem_type->mem_ops; + + if (mem_type->mem_flag & (MEM_HUGETLB_PRIVATE | MEM_HUGETLB)) + page_size = default_huge_page_size(); + else + page_size = psize(); + + nr_pages = UFFD_TEST_MEM_SIZE / page_size; + /* TODO: remove this global var.. it's so ugly */ + nr_cpus = 1; + + return uffd_test_ctx_init(test->uffd_feature_required); +} + +static bool uffd_feature_supported(uffd_test_case_t *test) +{ + uint64_t features; + + if (uffd_get_features(&features)) + return false; + + return (features & test->uffd_feature_required) == + test->uffd_feature_required; +} + +uffd_test_case_t uffd_tests[] = { +}; + int main(int argc, char *argv[]) { + int n_tests = sizeof(uffd_tests) / sizeof(uffd_test_case_t); + int n_mems = sizeof(mem_types) / sizeof(mem_type_t); + uffd_test_case_t *test; + mem_type_t *mem_type; + char test_name[128]; int has_uffd; + int i, j; has_uffd = test_uffd_api(false); has_uffd |= test_uffd_api(true); @@ -116,6 +217,29 @@ int main(int argc, char *argv[]) printf("Userfaultfd not supported or unprivileged, skip all tests\n"); exit(KSFT_SKIP); } + + for (i = 0; i < n_tests; i++) { + test = &uffd_tests[i]; + for (j = 0; j < n_mems; j++) { + mem_type = &mem_types[j]; + if (!(test->mem_targets & mem_type->mem_flag)) + continue; + snprintf(test_name, sizeof(test_name), + "%s on %s", test->name, mem_type->name); + + uffd_test_start(test_name); + if (!uffd_feature_supported(test)) { + uffd_test_skip("feature missing"); + continue; + } + if (uffd_setup_environment(test, mem_type)) { + uffd_test_skip("environment setup failed"); + continue; + } + test->uffd_fn(); + } + } + uffd_test_report(); return ksft_get_fail_cnt() ? KSFT_FAIL : KSFT_PASS; diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c index 5ee6c4688a7c..1bc0ceb01adb 100644 --- a/tools/testing/selftests/mm/vm_util.c +++ b/tools/testing/selftests/mm/vm_util.c @@ -254,3 +254,40 @@ int uffd_open_sys(unsigned int flags) return -1; #endif } + +int uffd_open(unsigned int flags) +{ + int uffd = uffd_open_sys(flags); + + if (uffd < 0) + uffd = uffd_open_dev(flags); + + return uffd; +} + +int uffd_get_features(uint64_t *features) +{ + struct uffdio_api uffdio_api = { .api = UFFD_API, .features = 0 }; + /* + * This should by default work in most kernels; the feature list + * will be the same no matter what we pass in here. + */ + int fd = uffd_open(UFFD_USER_MODE_ONLY); + + if (fd < 0) + /* Maybe the kernel is older than user-only mode? */ + fd = uffd_open(0); + + if (fd < 0) + return fd; + + if (ioctl(fd, UFFDIO_API, &uffdio_api)) { + close(fd); + return -errno; + } + + *features = uffdio_api.features; + close(fd); + + return 0; +} diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h index 481354141533..634eb2f41145 100644 --- a/tools/testing/selftests/mm/vm_util.h +++ b/tools/testing/selftests/mm/vm_util.h @@ -50,6 +50,8 @@ int uffd_register(int uffd, void *addr, uint64_t len, int uffd_unregister(int uffd, void *addr, uint64_t len); 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); /* * On ppc64 this will only work with radix 2M hugepage size