From patchwork Thu Mar 30 16:08:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13194509 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 E5E2CC77B60 for ; Thu, 30 Mar 2023 16:08:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4CDF7900002; Thu, 30 Mar 2023 12:08:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 458126B009D; Thu, 30 Mar 2023 12:08:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 234FC900002; Thu, 30 Mar 2023 12:08:43 -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 041806B009C for ; Thu, 30 Mar 2023 12:08:43 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id DC742161041 for ; Thu, 30 Mar 2023 16:08:42 +0000 (UTC) X-FDA: 80626047684.26.7F438F6 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf04.hostedemail.com (Postfix) with ESMTP id 9E39C4000C for ; Thu, 30 Mar 2023 16:08:40 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=f8yFnMQA; spf=pass (imf04.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=1680192520; 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=WAXTpuab088GPUG02wNB3TGzyfzkRuRXgQ9Q53l/WHY=; b=abngeQV9UJgERbxto2BT1meXaIno2Vch5YfGbsC3XoU5AGyeebq7xg7cr6BlVOit9XWHOQ p0uwx8SmiEFAJP9QoIVcR9gj1/n1mWfPtREa4TcTtz1P7c/Rdwk1l2cmGlhyJjoUbWAC6T kQCQBkmbHMIoItcx/I0z6TQ02kR1csc= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=f8yFnMQA; spf=pass (imf04.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=1680192520; a=rsa-sha256; cv=none; b=CGIZG6XaCmWGj+0lWNjNq0HVS+DU1/Pb9bvny+UpwMp2xKUI1lcv5IFkVosOt8PHoIvde5 a3nlU4kO6KSjBkWUwA9GvCXI8erKc9N1RtzOvokn4yOZnDwIYJKjqcFUOBUtLDPZZZ20mb DjSRnrB7ZIjPxstPz3GmaUHzZUBYMkU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680192519; 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=WAXTpuab088GPUG02wNB3TGzyfzkRuRXgQ9Q53l/WHY=; b=f8yFnMQAPIs1851ubDzo5cfVO60+8k+5FcJ0BoAzeVMZevgeZPvFS8tJ1Heg7qo/GgPBne wnBWnxsK2BJ8tGDx3wrqM5DPcT9ax4j2PsahKnes3bZvJS23QSqxvwKX3wYUurBBFM0Bty DPsuq1Osft5ychZvsfrvzHJMSewWtAY= 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-526-1vQMHZd3MpeL58fhYDzxbg-1; Thu, 30 Mar 2023 12:08:38 -0400 X-MC-Unique: 1vQMHZd3MpeL58fhYDzxbg-1 Received: by mail-qt1-f197.google.com with SMTP id b11-20020ac87fcb000000b003e37d72d532so12709283qtk.18 for ; Thu, 30 Mar 2023 09:08:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680192518; x=1682784518; 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=WAXTpuab088GPUG02wNB3TGzyfzkRuRXgQ9Q53l/WHY=; b=sOfMkWuoKxvg66zzUvDV/5MnpEeRHnfKsBR7U6gpQyM1o+1xrNcSeekVge/hIuO+IG 01p/iZbj0UvEmkXsScqLs+dFGFu/JgltWPBmCUvLyGZdcOuZrCVRSqdJnPdDQSwL4Qn2 4CkmpZ9t5VYfnjNyeIw5rF21XMh+iIU7qejABmZFfQxnwJfOkpjZjlsy8pTsE8Ye8e5W rVySViY6kriuMcGWbLAJ9VR1caXL66ZNk+JtVemm+DiGV9pfp+s76wkg0TbTkLBHdHs1 2aYln17XX3eiRo32K8NvUyVwJjDhJpnVyq27ejoGA/mQJsyeDhjZiewRYER2IwcqNlPL mVlQ== X-Gm-Message-State: AO0yUKXdtBzbTHB3cIT2K7ZBfbXpi0J7Z0JwnJb4nqXsJrX1mlq789ma zaW1iKrBCN6gExhKaDPFkECrUxPv8JW+Ttnq7aXfFEcslNbiXUKlFCuZrYG+/mOmoVh2SSVixMs Ph9KQb4FKk8Q= X-Received: by 2002:a05:622a:3c9:b0:3b8:36f8:830e with SMTP id k9-20020a05622a03c900b003b836f8830emr37735697qtx.6.1680192517752; Thu, 30 Mar 2023 09:08:37 -0700 (PDT) X-Google-Smtp-Source: AK7set812uf+kM7Yhq9Ka7uXYBi3m3muJq4kJHafeEAcNFKKp1xJShq8EXVPV3LRZllFJWeH38i74A== X-Received: by 2002:a05:622a:3c9:b0:3b8:36f8:830e with SMTP id k9-20020a05622a03c900b003b836f8830emr37735663qtx.6.1680192517402; Thu, 30 Mar 2023 09:08:37 -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 r13-20020ac8794d000000b003e638d0aa67sm195332qtt.93.2023.03.30.09.08.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 09:08:36 -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 27/29] selftests/mm: Allow uffd test to skip properly with no privilege Date: Thu, 30 Mar 2023 12:08:35 -0400 Message-Id: <20230330160835.3107613-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: 9E39C4000C X-Rspam-User: X-Stat-Signature: pi6p587yjqrje47ecqxe1gbergb87iox X-HE-Tag: 1680192520-441508 X-HE-Meta: U2FsdGVkX1+LR94nLluQ/NueGSnb6JoP4wKES4q/4dcm+sLmuAykLnPfp3j4pvd6N2vrt8F4TMJAL1fUUuRLWe0tRZPx20m9dJ1uUkx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Pua/38fdXEvr+WyLtIc3JusHykzrn4ea2tozKRKfmJnqKuFOdBr/bPSHSPcO628JTrsXu7PSbRmVM2XFjUNWnZcb2mlwaiINC+KIDcRIV/KtM5gThgera21gpanOV/ouGKujuwlyjzxhBv4KMjq6roYaKAKRiq9FYDznjK/a9/DLF6zwJ6zpNzLMBQZpXmP5L9RulJdioPZkvmc2qCmkfzQ+YwOrD8sADuCtAHhMNBF26au40I+187FJ9BcmHdH4J0yxfoXht2enA+4Pb2FzOF24GU6yR 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: Allow skip a unit test properly due to no privilege (e.g. sigbus and events tests). Signed-off-by: Peter Xu --- tools/testing/selftests/mm/uffd-common.c | 27 ++++++++++++-------- tools/testing/selftests/mm/uffd-common.h | 4 +-- tools/testing/selftests/mm/uffd-stress.c | 6 +++-- tools/testing/selftests/mm/uffd-unit-tests.c | 10 +++++--- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selftests/mm/uffd-common.c index 95ad619d0df4..964fa57b710a 100644 --- a/tools/testing/selftests/mm/uffd-common.c +++ b/tools/testing/selftests/mm/uffd-common.c @@ -225,7 +225,7 @@ void uffd_stats_report(struct uffd_stats *stats, int n_cpus) printf("\n"); } -void userfaultfd_open(uint64_t *features) +int userfaultfd_open(uint64_t *features) { struct uffdio_api uffdio_api; @@ -234,18 +234,19 @@ void userfaultfd_open(uint64_t *features) else uffd = uffd_open_sys(UFFD_FLAGS); if (uffd < 0) - err("uffd open failed (dev=%d)", test_dev_userfaultfd); + return -1; uffd_flags = fcntl(uffd, F_GETFD, NULL); uffdio_api.api = UFFD_API; uffdio_api.features = *features; if (ioctl(uffd, UFFDIO_API, &uffdio_api)) - err("UFFDIO_API failed.\nPlease make sure to " - "run with either root or ptrace capability."); + /* Probably lack of CAP_PTRACE? */ + return -1; if (uffdio_api.api != UFFD_API) err("UFFDIO_API error: %" PRIu64, (uint64_t)uffdio_api.api); *features = uffdio_api.features; + return 0; } static inline void munmap_area(void **area) @@ -288,7 +289,7 @@ static void uffd_test_ctx_clear(void) munmap_area((void **)&area_remap); } -int uffd_test_ctx_init(uint64_t features) +int uffd_test_ctx_init(uint64_t features, const char **errmsg) { unsigned long nr, cpu; int ret; @@ -296,13 +297,19 @@ int uffd_test_ctx_init(uint64_t features) uffd_test_ctx_clear(); ret = uffd_test_ops->allocate_area((void **)&area_src, true); - if (ret) - return ret; - ret = uffd_test_ops->allocate_area((void **)&area_dst, false); - if (ret) + ret |= uffd_test_ops->allocate_area((void **)&area_dst, false); + if (ret) { + if (errmsg) + *errmsg = "memory allocation failed"; return ret; + } - userfaultfd_open(&features); + ret = userfaultfd_open(&features); + if (ret) { + if (errmsg) + *errmsg = "possible lack of priviledge"; + return ret; + } count_verify = malloc(nr_pages * sizeof(unsigned long long)); if (!count_verify) diff --git a/tools/testing/selftests/mm/uffd-common.h b/tools/testing/selftests/mm/uffd-common.h index 16d32ddf8412..07d00e9809dc 100644 --- a/tools/testing/selftests/mm/uffd-common.h +++ b/tools/testing/selftests/mm/uffd-common.h @@ -101,8 +101,8 @@ extern uffd_test_ops_t hugetlb_uffd_test_ops; extern uffd_test_ops_t *uffd_test_ops; void uffd_stats_report(struct uffd_stats *stats, int n_cpus); -int uffd_test_ctx_init(uint64_t features); -void userfaultfd_open(uint64_t *features); +int uffd_test_ctx_init(uint64_t features, const char **errmsg); +int userfaultfd_open(uint64_t *features); int uffd_read_msg(int ufd, struct uffd_msg *msg); void wp_range(int ufd, __u64 start, __u64 len, bool wp); void uffd_handle_page_fault(struct uffd_msg *msg, struct uffd_stats *stats); diff --git a/tools/testing/selftests/mm/uffd-stress.c b/tools/testing/selftests/mm/uffd-stress.c index a6f3609c1ad1..8f41bef2fbda 100644 --- a/tools/testing/selftests/mm/uffd-stress.c +++ b/tools/testing/selftests/mm/uffd-stress.c @@ -272,7 +272,8 @@ static int userfaultfd_stress(void) struct uffd_stats uffd_stats[nr_cpus]; uint64_t mem_size = nr_pages * page_size; - uffd_test_ctx_init(UFFD_FEATURE_WP_UNPOPULATED); + if (uffd_test_ctx_init(UFFD_FEATURE_WP_UNPOPULATED, NULL)) + err("context init failed"); if (posix_memalign(&area, page_size, page_size)) err("out of memory"); @@ -436,7 +437,8 @@ static void parse_test_type_arg(const char *raw_type) * feature. */ - userfaultfd_open(&features); + if (userfaultfd_open(&features)) + err("Userfaultfd open failed"); test_uffdio_wp = test_uffdio_wp && (features & UFFD_FEATURE_PAGEFAULT_FLAG_WP); diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/selftests/mm/uffd-unit-tests.c index 82fd3aaa06c2..9e7f7c7f2982 100644 --- a/tools/testing/selftests/mm/uffd-unit-tests.c +++ b/tools/testing/selftests/mm/uffd-unit-tests.c @@ -175,7 +175,8 @@ static int test_uffd_api(bool use_dev) * 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) +static int uffd_setup_environment(uffd_test_case_t *test, mem_type_t *mem_type, + const char **errmsg) { map_shared = mem_type->shared; uffd_test_ops = mem_type->mem_ops; @@ -189,7 +190,7 @@ static int uffd_setup_environment(uffd_test_case_t *test, mem_type_t *mem_type) /* TODO: remove this global var.. it's so ugly */ nr_cpus = 1; - return uffd_test_ctx_init(test->uffd_feature_required); + return uffd_test_ctx_init(test->uffd_feature_required, errmsg); } static bool uffd_feature_supported(uffd_test_case_t *test) @@ -838,6 +839,7 @@ int main(int argc, char *argv[]) uffd_test_case_t *test; mem_type_t *mem_type; char test_name[128]; + const char *errmsg; int has_uffd; int i, j; @@ -863,8 +865,8 @@ int main(int argc, char *argv[]) uffd_test_skip("feature missing"); continue; } - if (uffd_setup_environment(test, mem_type)) { - uffd_test_skip("memory allocation failed"); + if (uffd_setup_environment(test, mem_type, &errmsg)) { + uffd_test_skip(errmsg); continue; } test->uffd_fn();