From patchwork Thu Mar 30 16:07:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13194498 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 631C7C761A6 for ; Thu, 30 Mar 2023 16:08:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 013CB6B007D; Thu, 30 Mar 2023 12:08:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F0463900002; Thu, 30 Mar 2023 12:08:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DA5C66B009B; Thu, 30 Mar 2023 12:08:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C74926B007D for ; Thu, 30 Mar 2023 12:08:10 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 85B72120ABD for ; Thu, 30 Mar 2023 16:08:10 +0000 (UTC) X-FDA: 80626046340.26.FA4CD02 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 0971D10000B for ; Thu, 30 Mar 2023 16:08:07 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eD1kvA8c; 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=1680192488; 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=Pi0McrG33VvWSi8wJEF+ZnP9lqDjRKaT61ZsjgmAIPc=; b=IvCEJhAAZy8Ng3cYlSRTzXM0tUKcJbCD89kXqQShp82MN0LeAchCJMk+BaQlj9w3dKACOP Bn1fj6LZlhWRzcIHIq+lE3GgwBsO/jA3zzs7B+b23TiOXePjdqvLJnfQhXQwQrWIAxqL4d 6HkRNF9fYn0tZnF16jvtpn4NOBB3Pjc= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eD1kvA8c; 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=1680192488; a=rsa-sha256; cv=none; b=qzfqJetPQ9ygXern5APx7BP/oEUaJE5pPwKD60YJw1OSeFf5X/xA09BFofzR4zdjQE9pIi zLn+ddqb5hvSbztOQF+hVURxNelqOLqabGvSI8tZT+yTYlrr+cwHNy49snuEfxzjtZQHPv TsQV+TdUu/ZB/nkMTgObBKFSk30cp3w= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680192487; 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=Pi0McrG33VvWSi8wJEF+ZnP9lqDjRKaT61ZsjgmAIPc=; b=eD1kvA8cuhW/LlQs5JnYhy8M7UrJcwPn02JK3B1Eg9+GQUrCnPzGHmT++jsOpFKJrakymS 40PQyoMLs+GLxZXWYcoU4uezTdfMdTR7vueGsgJQ+JjUVAkjcP6JEw85DDCE7LqYP7Llik OzBsSBWzOcN7YnaF7S+5+Lks082LPHs= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-547-sKLJ8rUVOya-SKjBwx--kA-1; Thu, 30 Mar 2023 12:08:04 -0400 X-MC-Unique: sKLJ8rUVOya-SKjBwx--kA-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-5aae34d87f7so13016356d6.0 for ; Thu, 30 Mar 2023 09:07:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680192475; 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=Pi0McrG33VvWSi8wJEF+ZnP9lqDjRKaT61ZsjgmAIPc=; b=PtUlYRLzjJdO441eA3k5uNbCE7+TmiTvihXdLB5TjerUVsh1c0w0i3mLtXZ/1/jLXC /i6OA5buQAUQdDtrIR3uEaJvYab485n00lEMYS7XrApHrWZKqBI0MDSi7lkp9mvAPnEd RjGxzH0dnhB0kt4KjKIymPzBdkcd1xUSA3AeuMuLloHJfyoVzAX5GHxTO1gMbZMiztW1 PNNvEaeYC/ubIqQfNNZ2xYQF0jbtUtGvuUn4RCJkEMG/ETWHHmIqvqh0eCpNzGeTheMz t4mBbqXJfK6iw0cR63WMZx5ma7qaUEiNSIkQbbg1JUHz2Ls68e2S6+shYSsn6WORmB2o iQsw== X-Gm-Message-State: AAQBX9f1ftsLo//kh7OrgX4l299JQt9npmMoMBBgUWwHZQcpk7JZpX53 8AYZ91MnuxZC/cbpjuhtZ99MRY/VUl5a50Njz7Vs1htC7OWryV0ZZSBT0lXmcE4vj0IS2ErEUvs TrdHEEh+42vr/FjL1HH7yyKhA70mj3iUPqJkJNjwJi96LhRd3ouBrfv/2/amWlU9pPmUD X-Received: by 2002:a05:6214:528f:b0:5a5:e941:f33d with SMTP id kj15-20020a056214528f00b005a5e941f33dmr3649494qvb.3.1680192475320; Thu, 30 Mar 2023 09:07:55 -0700 (PDT) X-Google-Smtp-Source: AKy350Y+5Of9BQ51FgDOPQFkGMpVhdWP6DIaYdi2MOHzvLJbq+jW3Pu9yxIViDQt4nTW5eY4voEBhg== X-Received: by 2002:a05:6214:528f:b0:5a5:e941:f33d with SMTP id kj15-20020a056214528f00b005a5e941f33dmr3649415qvb.3.1680192474610; Thu, 30 Mar 2023 09:07:54 -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 bl28-20020a05620a1a9c00b007339c5114a9sm19630391qkb.103.2023.03.30.09.07.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 09:07:53 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrea Arcangeli , peterx@redhat.com, David Hildenbrand , Andrew Morton , Nadav Amit , Mike Kravetz , Axel Rasmussen , Leonardo Bras Soares Passos , Mike Rapoport Subject: [PATCH 16/29] selftests/mm: UFFDIO_API test Date: Thu, 30 Mar 2023 12:07:52 -0400 Message-Id: <20230330160752.3107283-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-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 37thwwpadxdcfbwi614it1epfmcypasm X-Rspamd-Queue-Id: 0971D10000B X-HE-Tag: 1680192487-235923 X-HE-Meta: U2FsdGVkX19qAMRQmRygMGZHd5swBRKSjfHguaI+zXigEeRGOBzIXeioM8S2y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hf2MGKjGZN9IvTPC1nwPX+zc0ULhbPckriBMH/A0pg5Tt+o7R5BQc+ubt1cYpLOHnSRwfrCKJeocN6Q6wdWdvahQdxvXx7E/OVvu086XBwHtKjQtBe3mobWpTRHx++XoCuzqkSXlP8v/i4WUipJiBsgwyZ1OcugqI7FBRFegMcooP4v/SXItUc1fx452z+6pO3VRFpCG05URDPLg8EbuHX5eveA+1ubaPHRswKWrsEoT07EQinR54VSL1uxfBY8Wet9EMg/p/hrwbVKxShLHg5NWMS0RPHI0GaHdF3sHwpkv3/8t5OznoFXkO2PI3vz4aosSBPvE4ZbqXTXi7LBTNo2UN+gtEV/thPTTojFXr2n4bHoG1zPXlMw= 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 one simple test for UFFDIO_API. With that, I also added a bunch of small but handy helpers along the way. Signed-off-by: Peter Xu --- tools/testing/selftests/mm/uffd-unit-tests.c | 111 ++++++++++++++++++- tools/testing/selftests/mm/vm_util.c | 10 ++ 2 files changed, 120 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/selftests/mm/uffd-unit-tests.c index 6857388783be..dfb44ffad5f5 100644 --- a/tools/testing/selftests/mm/uffd-unit-tests.c +++ b/tools/testing/selftests/mm/uffd-unit-tests.c @@ -9,9 +9,118 @@ #ifdef __NR_userfaultfd +struct { + unsigned int pass, skip, fail, total; +} uffd_test_acct; + +static void uffd_test_report(void) +{ + printf("Userfaults unit tests: pass=%u, skip=%u, fail=%u (total=%u)\n", + uffd_test_acct.pass, + uffd_test_acct.skip, + uffd_test_acct.fail, + uffd_test_acct.total); +} + +static void uffd_test_pass(void) +{ + printf("done\n"); + uffd_test_acct.pass++; +} + +#define uffd_test_start(...) do { \ + printf(__VA_ARGS__); \ + printf("... "); \ + uffd_test_acct.total++; \ + } while (0) + +#define uffd_test_fail(...) do { \ + printf("failed [reason: "); \ + printf(__VA_ARGS__); \ + printf("]\n"); \ + uffd_test_acct.fail++; \ + } while (0) + +#define uffd_test_skip(...) do { \ + printf("skipped [reason: "); \ + printf(__VA_ARGS__); \ + printf("]\n"); \ + uffd_test_acct.skip++; \ + } while (0) + +/* + * Returns 1 if specific userfaultfd supported, 0 otherwise. Note, we'll + * return 1 even if some test failed as long as uffd supported, because in + * that case we still want to proceed with the rest uffd unit tests. + */ +static int test_uffd_api(bool use_dev) +{ + struct uffdio_api uffdio_api; + int uffd; + + uffd_test_start("UFFDIO_API (with %s)", + use_dev ? "/dev/userfaultfd" : "syscall"); + + if (use_dev) + uffd = uffd_open_dev(UFFD_FLAGS); + else + uffd = uffd_open_sys(UFFD_FLAGS); + if (uffd < 0) { + uffd_test_skip("cannot open userfaultfd handle"); + return 0; + } + + /* Test wrong UFFD_API */ + uffdio_api.api = 0xab; + uffdio_api.features = 0; + if (ioctl(uffd, UFFDIO_API, &uffdio_api) == 0) { + uffd_test_fail("UFFDIO_API should fail with wrong api but didn't"); + goto out; + } + + /* Test wrong feature bit */ + uffdio_api.api = UFFD_API; + uffdio_api.features = BIT_ULL(63); + if (ioctl(uffd, UFFDIO_API, &uffdio_api) == 0) { + uffd_test_fail("UFFDIO_API should fail with wrong feature but didn't"); + goto out; + } + + /* Test normal UFFDIO_API */ + uffdio_api.api = UFFD_API; + uffdio_api.features = 0; + if (ioctl(uffd, UFFDIO_API, &uffdio_api)) { + uffd_test_fail("UFFDIO_API should succeed but failed"); + goto out; + } + + /* Test double requests of UFFDIO_API with a random feature set */ + uffdio_api.features = BIT_ULL(0); + if (ioctl(uffd, UFFDIO_API, &uffdio_api) == 0) { + uffd_test_fail("UFFDIO_API should reject initialized uffd"); + goto out; + } + + uffd_test_pass(); +out: + close(uffd); + /* We have a valid uffd handle */ + return 1; +} + int main(int argc, char *argv[]) { - return KSFT_PASS; + int has_uffd; + + has_uffd = test_uffd_api(false); + has_uffd |= test_uffd_api(true); + + if (!has_uffd) { + printf("Userfaultfd not supported or unprivileged, skip all tests\n"); + exit(KSFT_SKIP); + } + uffd_test_report(); + return uffd_test_acct.fail ? KSFT_FAIL : KSFT_PASS; } #else /* __NR_userfaultfd */ diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c index 7c2bf88d6393..62fcf039d6b7 100644 --- a/tools/testing/selftests/mm/vm_util.c +++ b/tools/testing/selftests/mm/vm_util.c @@ -254,3 +254,13 @@ 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; +}