From nobody Wed Dec 17 10:53:23 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A89DCC7619A for ; Wed, 12 Apr 2023 16:45:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231331AbjDLQpy (ORCPT ); Wed, 12 Apr 2023 12:45:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40480 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231262AbjDLQpN (ORCPT ); Wed, 12 Apr 2023 12:45:13 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 59BB7976C for ; Wed, 12 Apr 2023 09:43:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681317835; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ujW8sSFyaGmImGgAfN35Mc7lSxeHHva64g4oK3AlUaQ=; b=WXECe1r6sPYXuk3zpYqSWkeXZe2UPAHh4H3sw6TFjkoID1Vo2eKaDVjsrU7EKg9iDY6/yE irPfZkc4yVDT1ODIAGZwI44RLEj9kBi7UBaMV8PTa3WsHT7R2ChV4YhXSJ0YqqdW28A2YC NGonSFHPFkvm86ASfkzCA/x1mHIezIE= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-636-hVDJx8isO0GSeEqGox967A-1; Wed, 12 Apr 2023 12:43:54 -0400 X-MC-Unique: hVDJx8isO0GSeEqGox967A-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-3e948dfe9c9so365011cf.0 for ; Wed, 12 Apr 2023 09:43:54 -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=eHuJap8/sNTuLd5HBkPYq0f8xLqrGdE+eFLnCfDX+nQatqv+nSLIYQc1LwBaGtrDYs 3R7IVaaAqmrOboBui2xQp2vz22A1w3MNiFccJjJK6ZC525DpnJUQ8Log9dtA5azfS6EJ SCKzobdXVZYLt+jNkpmnjajK/xmhkLgTl5o2e2qfsdoaPDHaZu87ZIkuiB+dN3nuWc4c mAOFEiuYPRkz8b5fi4Z5Y4oBWGNd487q/NT3eimB63Kd4kw96cK4YZR5hpmPNtkQwb9i utGiLWnaAIHbRMvmPaWjzIcAHWUO50bkdznkb+pa1QdbmkEydI+rlMJ+JfR82ZGn2MBg qDhg== X-Gm-Message-State: AAQBX9enWlpzQqMmwv9/ClpFHRtqn2s8YWiMtbUvtOC0tm16krG0dydn VMskcd+Yr3RROglRvJ3KI81BAwU/Mo5Y5aTj2+bD3tWibcwpUP9uFZiFlzoE+v24kjKxkn5K7Ur izW5z2uEE/ahqBxgk1gCVt5tY1ANfBBrLZO1MHcDL7TrB2fytObva5YSXXE2R44RI4RkvMcVitK 3pyIfRlw== X-Received: by 2002:a05:622a:50d:b0:3e3:1d31:e37 with SMTP id l13-20020a05622a050d00b003e31d310e37mr4709095qtx.1.1681317832542; 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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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/s= elftests/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 @@ =20 #ifdef __NR_userfaultfd =20 +/* 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[] =3D { + { + .name =3D "anon", + .mem_flag =3D MEM_ANON, + .mem_ops =3D &anon_uffd_test_ops, + .shared =3D false, + }, + { + .name =3D "shmem", + .mem_flag =3D MEM_SHMEM, + .mem_ops =3D &shmem_uffd_test_ops, + .shared =3D true, + }, + { + .name =3D "shmem-private", + .mem_flag =3D MEM_SHMEM_PRIVATE, + .mem_ops =3D &shmem_uffd_test_ops, + .shared =3D false, + }, + { + .name =3D "hugetlb", + .mem_flag =3D MEM_HUGETLB, + .mem_ops =3D &hugetlb_uffd_test_ops, + .shared =3D true, + }, + { + .name =3D "hugetlb-private", + .mem_flag =3D MEM_HUGETLB_PRIVATE, + .mem_ops =3D &hugetlb_uffd_test_ops, + .shared =3D 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=3D%u, skip=3D%u, fail=3D%u (total=3D%= u)\n", @@ -105,9 +165,50 @@ static int test_uffd_api(bool use_dev) return 1; } =20 +/* + * 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 =3D mem_type->shared; + uffd_test_ops =3D mem_type->mem_ops; + + if (mem_type->mem_flag & (MEM_HUGETLB_PRIVATE | MEM_HUGETLB)) + page_size =3D default_huge_page_size(); + else + page_size =3D psize(); + + nr_pages =3D UFFD_TEST_MEM_SIZE / page_size; + /* TODO: remove this global var.. it's so ugly */ + nr_cpus =3D 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) =3D=3D + test->uffd_feature_required; +} + +uffd_test_case_t uffd_tests[] =3D { +}; + int main(int argc, char *argv[]) { + int n_tests =3D sizeof(uffd_tests) / sizeof(uffd_test_case_t); + int n_mems =3D 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; =20 has_uffd =3D test_uffd_api(false); has_uffd |=3D 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 =3D 0; i < n_tests; i++) { + test =3D &uffd_tests[i]; + for (j =3D 0; j < n_mems; j++) { + mem_type =3D &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(); =20 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 =3D uffd_open_sys(flags); + + if (uffd < 0) + uffd =3D uffd_open_dev(flags); + + return uffd; +} + +int uffd_get_features(uint64_t *features) +{ + struct uffdio_api uffdio_api =3D { .api =3D UFFD_API, .features =3D 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 =3D uffd_open(UFFD_USER_MODE_ONLY); + + if (fd < 0) + /* Maybe the kernel is older than user-only mode? */ + fd =3D uffd_open(0); + + if (fd < 0) + return fd; + + if (ioctl(fd, UFFDIO_API, &uffdio_api)) { + close(fd); + return -errno; + } + + *features =3D 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); =20 /* * On ppc64 this will only work with radix 2M hugepage size --=20 2.39.1