From patchwork Thu Mar 30 16:08: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: 13194503 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 79016C761A6 for ; Thu, 30 Mar 2023 16:08:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EDED6900005; Thu, 30 Mar 2023 12:08:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E8F9B6B009C; Thu, 30 Mar 2023 12:08:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D5783900005; Thu, 30 Mar 2023 12:08:26 -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 C45016B009B for ; Thu, 30 Mar 2023 12:08:26 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 88117AB2CE for ; Thu, 30 Mar 2023 16:08:26 +0000 (UTC) X-FDA: 80626047012.04.AB0C608 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf09.hostedemail.com (Postfix) with ESMTP id 6639C140021 for ; Thu, 30 Mar 2023 16:08:24 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZjMpU5bS; spf=pass (imf09.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=1680192504; 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=uhDA/wjfT6X0KP/XkxMZvdeJDhg2SLQS/Rv9HxMM2sI=; b=WSxlB/FTCv2ROBXr3wGf9J5O7JLLkF7XsmplB5CtnyjrbbYOAL48VD89Cklo/6DRG1BhdV tFskqpJcvZBkjIzW1hzoV/itIEotsuWA7xqKukkC/90EGGU7KHruW3SR5FjkoSyBNYY0/g dKvyrOqb6svClSJJC8lm8QLI/d4psog= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZjMpU5bS; spf=pass (imf09.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=1680192504; a=rsa-sha256; cv=none; b=hBts8F/jGZyyRtyOj0el0cvja3kLNRu4Bp2rwdjYAjmQbr+IRRlvXiRk3XOu3dIV81hpQD tCO6IACLJUmpQCwzpRUgrYnvdlxcA9IRGGETFRSuzfESI66fxyTthPqmMCH351bR6aD6Up eNVFChHu05L7I/Q0B0DKAa9IarZ3H+s= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680192503; 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=uhDA/wjfT6X0KP/XkxMZvdeJDhg2SLQS/Rv9HxMM2sI=; b=ZjMpU5bSEyYPn4T9sIEwprY/tTPF2d91qxnWkVrTKWJm23cAhP/dvuJy+fjMDlBwEwzdd0 CRbq1Hwmrt4T3Rib/fhzUS4X27EZeEvxu9wuBKWLb2/tgKC3oKi7hhBWKG66IMEWuNQgTa sLRgfZWJv4KENRWkXFmJkb4vUfPze8Y= 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-279-vzXaHdmmPEumVCu5LdWobA-1; Thu, 30 Mar 2023 12:08:22 -0400 X-MC-Unique: vzXaHdmmPEumVCu5LdWobA-1 Received: by mail-qk1-f199.google.com with SMTP id 198-20020a370bcf000000b007468cffa4e2so9101616qkl.10 for ; Thu, 30 Mar 2023 09:08:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680192502; 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=uhDA/wjfT6X0KP/XkxMZvdeJDhg2SLQS/Rv9HxMM2sI=; b=VRMnwsa+ysj5z1Csf/+SpbpNkBYfX+sgtWJGTyCGvXkBKxObEHilj+4JO9LlijHEh3 pyqBMfphHsMqaodXyiA4FeyM/XpB5srjf9KeV2rOhc0LjhGh5jp0+2KEhePbsg5rJPc+ A74Re+muUBu0VEo+6nZcM+mt6PWvFnyZD5ZZ/bkEqhpfYPLSAQisDB4P0bOpeYvkY05Q GkUW4Epn0mR6L4fmGMiTkLBp2vWCX0oT0lfAf/STGEaT6S/P2zIt84o7KHEHJ64Z5ju3 wXiyLJGk/G4Zwvd6vhmiHynZe73rCPTKP6+5KNS8oOm448bwonLAo567jZMAGIVPAgPY UyqA== X-Gm-Message-State: AAQBX9cgPypnQZwKKiiUlNdtzHDcO8QJEF1GnaoX/srOsoGCDjcwy2QU k2wSF776UikUkeEZ5ZOxQn9qdAWaBg3p1U94C6ZLEqOBx0GeB9SpPI2GMW2qDRlza5R1W3VB7ym 8+FcaQHrRaAg= X-Received: by 2002:a05:6214:4005:b0:56c:d9e:c9a0 with SMTP id kd5-20020a056214400500b0056c0d9ec9a0mr3702700qvb.1.1680192501607; Thu, 30 Mar 2023 09:08:21 -0700 (PDT) X-Google-Smtp-Source: AKy350bIVoRr5JKeD10TL5Z3VboZfFB7o7h5MoQBZi1Ow1eqcSHcj8AcQbyOTnGNgHxF8PIDsqGFfQ== X-Received: by 2002:a05:6214:4005:b0:56c:d9e:c9a0 with SMTP id kd5-20020a056214400500b0056c0d9ec9a0mr3702655qvb.1.1680192501195; Thu, 30 Mar 2023 09:08:21 -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 j5-20020a0ce6a5000000b005dd8b93458esm5515078qvn.38.2023.03.30.09.08.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 09:08:20 -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 21/29] selftests/mm: Add framework for uffd-unit-test Date: Thu, 30 Mar 2023 12:08:18 -0400 Message-Id: <20230330160818.3107545-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-Server: rspam07 X-Rspamd-Queue-Id: 6639C140021 X-Rspam-User: X-Stat-Signature: 68zxg71sh1at54b4k9mnykcsh559bxeb X-HE-Tag: 1680192504-207379 X-HE-Meta: U2FsdGVkX18tc4CUd7pdBPpSdeTsF0i8NnzNWfgMX/bqq3c7XHxuNu3d9UzMJyudYfPm85kGuZyDOQBHIpogqok75433M2R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fgWahvgRQPZKVCJjt4wMU5xlZe+DR/Rh9wtstsXLJJUPt9zhwCU2vTtNePLxfWK+Joz8aRTveEugoHioHBlLas8F0omyTpnDHFQdBD9iabNaIQZOooKHt6NJwmwljWpbJ1Giyt1fex0djVtT6JarAiUUJBZ7Wu0qFWM2n/wvQ3ZscSCNJG48r/7AMjovIE79zDsfCqzweRAx4V7toA32Q8hmk/rNvoDPqKFND0W6+qvw6SHX1s8hfcduflhnQRMZbCYKTtDNZ7wpPiwUpzkq8Lj+JZ8sITwGFZ4DVZr 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 | 125 +++++++++++++++++++ tools/testing/selftests/mm/vm_util.c | 27 ++++ tools/testing/selftests/mm/vm_util.h | 1 + 3 files changed, 153 insertions(+) diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/selftests/mm/uffd-unit-tests.c index dfb44ffad5f5..007145063363 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; + struct { unsigned int pass, skip, fail, total; } uffd_test_acct; @@ -108,9 +168,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); @@ -119,7 +220,31 @@ 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("memory allocation failed"); + continue; + } + test->uffd_fn(); + } + } + uffd_test_report(); + return uffd_test_acct.fail ? KSFT_FAIL : KSFT_PASS; } diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c index 62fcf039d6b7..dad1f62a7ecd 100644 --- a/tools/testing/selftests/mm/vm_util.c +++ b/tools/testing/selftests/mm/vm_util.c @@ -264,3 +264,30 @@ int uffd_open(unsigned int 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 a67db8432855..2edad3256271 100644 --- a/tools/testing/selftests/mm/vm_util.h +++ b/tools/testing/selftests/mm/vm_util.h @@ -51,6 +51,7 @@ int uffd_open(unsigned int flags); int uffd_register(int uffd, void *addr, uint64_t len, bool miss, bool wp, bool minor); int uffd_unregister(int uffd, void *addr, uint64_t len); +int uffd_get_features(uint64_t *features); /* * On ppc64 this will only work with radix 2M hugepage size