From patchwork Wed Apr 12 16:45:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13209364 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 9801EC7619A for ; Wed, 12 Apr 2023 16:45:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 39D3E90000A; Wed, 12 Apr 2023 12:45:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 33336900003; Wed, 12 Apr 2023 12:45:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A12290000A; Wed, 12 Apr 2023 12:45:35 -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 0490B900003 for ; Wed, 12 Apr 2023 12:45:35 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id B98C41C49A4 for ; Wed, 12 Apr 2023 16:45:34 +0000 (UTC) X-FDA: 80673314988.25.735037F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf18.hostedemail.com (Postfix) with ESMTP id 6D9E41C0017 for ; Wed, 12 Apr 2023 16:45:32 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="QD9/AnEa"; spf=pass (imf18.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=1681317932; 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=RvUBLMIj6eaAe1GDo1uBeLCLb+kkyu1V0plV870AgzY=; b=hKu9ZpIS0L8ZYtAX2Id8R7CnwdI4ZmvBtI/zTADkAjo+QSjQSzYDroKzcmuuBIlMxQIBWc afG/MuJw++57CMNaJVOIBChmYc1kyY6uyYJXdo23cjvf5HqE0nN8+YFryDtd6IlItBEnPx /xgcLIWalwwoWnLimm6vxf4EzBZLGVE= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="QD9/AnEa"; spf=pass (imf18.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=1681317932; a=rsa-sha256; cv=none; b=A3fSk3TdPhTjCgsQwvB+z13HfOEs0RaOmYYfMqNzpYV9vjhhwyQAYrQxvNIyY+UblATquh OdaiOx/pMIFF6UBzbJ/jUaQlcWSTHXg6QBBgyBU5lbUkne4bT2XnMnpPp/IHcZQLacduNS y+nakzBf6KhIDdwKD0Aghu3rO8ThH3A= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681317931; 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=RvUBLMIj6eaAe1GDo1uBeLCLb+kkyu1V0plV870AgzY=; b=QD9/AnEaA+ZpBu7gt6gieql97fbcPr9RunIubIUO2rgDkRRnwyF7yrEHyNE62XrwVoQofU weihL2PUK4WTnhsUbxCSGTmoVWuih4jYNU9h8fvRSngiTvllO7i81WQU9BcjlhEV3PqwpZ cQj+qEbyRrdAya7ReSPBKD9U03J08qs= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-204-AR7LlFRUOk6h8vGjykuQ4g-1; Wed, 12 Apr 2023 12:45:30 -0400 X-MC-Unique: AR7LlFRUOk6h8vGjykuQ4g-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-3e69901d854so896961cf.0 for ; Wed, 12 Apr 2023 09:45:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681317925; x=1683909925; 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=RvUBLMIj6eaAe1GDo1uBeLCLb+kkyu1V0plV870AgzY=; b=Z5r+epkZ50gFRvZPD72li/lbonn8Z5PnhZc/sQJLsVBWvonZuYoEjK8tqvQJXgK28A 4M1+ho83aUyB6BBEAIeuVs8xpauaJl/2yUuQSXF5kcqJ5GcHOWba7X2MvbWSleiR3OWy S1gqOKzmItX52Cmr8qrG+rREgV5QrPV8QYIBHf+Pe2KSqvrGp3kgVUo+FNxHAYHeQLuW 73LgfXjseiXFi/LqIYl+ScMoazAYiZH8+XbinuWrsOHOOUh4jr6g4oiPfYhTevxHSZ1H QCXXILdzg8Eq9aZuT1cVjCv7KGbV7fVUfPpcwzuqMIpK+evAljr0y+WW83e2AwJ8xn9g JicA== X-Gm-Message-State: AAQBX9dtEw9nVdIaeNplTLVf1jolUlL2n3yzoZoWoY/M+U5pAxLXNlMr UkeNIYZVWqO1qKm2oh+/EiSzp6rI9JeBmvggErdiLEPq1NcF38spa0sNPJn5CV7otl63csPBfkB 7lU5tzSUAp4GV7WGlqggktGCxTTjTsDuYu5SdloGZ0AexjqtafAjjEcJ/RIsJMRTkmIKP X-Received: by 2002:a05:622a:1816:b0:3e6:45c7:cf36 with SMTP id t22-20020a05622a181600b003e645c7cf36mr4507555qtc.2.1681317925314; Wed, 12 Apr 2023 09:45:25 -0700 (PDT) X-Google-Smtp-Source: AKy350Z+HrIjE4VkqiqM0waR7nZs5KpOic28Y2xavh1QsIMJZkK6W8GnSwnURyYGigGHyvQoxq9B1Q== X-Received: by 2002:a05:622a:1816:b0:3e6:45c7:cf36 with SMTP id t22-20020a05622a181600b003e645c7cf36mr4507479qtc.2.1681317924355; Wed, 12 Apr 2023 09:45:24 -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 73-20020a37064c000000b0074acd1b32f5sm112009qkg.83.2023.04.12.09.45.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Apr 2023 09:45:23 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Nadav Amit , Mike Rapoport , Leonardo Bras Soares Passos , peterx@redhat.com, David Hildenbrand , Axel Rasmussen , Andrea Arcangeli , Mike Kravetz , Andrew Morton Subject: [PATCH v2 28/31] selftests/mm: Allow uffd test to skip properly with no privilege Date: Wed, 12 Apr 2023 12:45:20 -0400 Message-Id: <20230412164520.329163-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: rspam04 X-Rspamd-Queue-Id: 6D9E41C0017 X-Stat-Signature: azpny853to6ekiwoxqjfcpi449jd6w1c X-HE-Tag: 1681317932-74508 X-HE-Meta: U2FsdGVkX18my9kzE3hPvG13I+d8F7TIZwUAIgqftRfLq+uQig4uU2tXTUIK7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0IO3EE5+o5aKQ5reUlZv9g9n8BkhxYlDGQiCal8i1FaNZVWF5b/QQp1iz7TkMRwFPxRsgqOhMAYZExwMYY+DqvqeDse97cnAgPPHkIN/GFybJi4kdqwWveR1PV/mcDe0XrkZSYm2ilQ8AxL+R2jEg47b6qeHmbH6UBsxYwWEwkDaXF0x5nW3haalMG5ZV83RHT63TAzQwOHfJuYP3tegJ/lNUEzy+7vWhZfvruT6BRdf8nMDIcEn48yeMLvH27Oeeqf6ko30OU+7hr5Mpgn90I0Q6I5O/wGUrUFb2u7+TtCZTXB+lpD77eM3OJ1Oh4hF+8dUlon0HkI 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 12ac84712a38..3e98e129f8bd 100644 --- a/tools/testing/selftests/mm/uffd-common.c +++ b/tools/testing/selftests/mm/uffd-common.c @@ -232,7 +232,7 @@ void uffd_stats_report(struct uffd_args *args, int n_cpus) printf("\n"); } -void userfaultfd_open(uint64_t *features) +int userfaultfd_open(uint64_t *features) { struct uffdio_api uffdio_api; @@ -241,18 +241,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) @@ -295,7 +296,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; @@ -303,13 +304,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 4bd5915cf5b4..32e590ce9442 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_args *args, 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_args *args); diff --git a/tools/testing/selftests/mm/uffd-stress.c b/tools/testing/selftests/mm/uffd-stress.c index d78f88850011..c0e804f05002 100644 --- a/tools/testing/selftests/mm/uffd-stress.c +++ b/tools/testing/selftests/mm/uffd-stress.c @@ -271,7 +271,8 @@ static int userfaultfd_stress(void) struct uffd_args args[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"); @@ -435,7 +436,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 a33d6c928eeb..b0acf558e8cb 100644 --- a/tools/testing/selftests/mm/uffd-unit-tests.c +++ b/tools/testing/selftests/mm/uffd-unit-tests.c @@ -172,7 +172,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; @@ -186,7 +187,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) @@ -835,6 +836,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; @@ -860,8 +862,8 @@ int main(int argc, char *argv[]) uffd_test_skip("feature missing"); continue; } - if (uffd_setup_environment(test, mem_type)) { - uffd_test_skip("environment setup failed"); + if (uffd_setup_environment(test, mem_type, &errmsg)) { + uffd_test_skip(errmsg); continue; } test->uffd_fn();