From patchwork Wed Apr 12 16:43:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13209356 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 5A256C7619A for ; Wed, 12 Apr 2023 16:43:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F0F58900005; Wed, 12 Apr 2023 12:43:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EBFA6900003; Wed, 12 Apr 2023 12:43:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D6092900005; Wed, 12 Apr 2023 12:43:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C5FAF900003 for ; Wed, 12 Apr 2023 12:43:53 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 7D1511601B8 for ; Wed, 12 Apr 2023 16:43:53 +0000 (UTC) X-FDA: 80673310746.11.F173692 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf06.hostedemail.com (Postfix) with ESMTP id 713B218000E for ; Wed, 12 Apr 2023 16:43:51 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=SbpZYaZz; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf06.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681317831; 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=q6fb7dGe8Qh9zpC1XHTDp4auTsAlmohzusSG+yhHPLI=; b=M7lRacX4HYCrGiSeKuUADOdOEsxox+37yiLVqdCRK9h2n4QO7IX3/RM+/DIuHeQ7XdSQVB 4CPPznK10htq2s4ingHvFEnqiyPC3U3hAz8KRA31/UT3mj30ysASIIwG3OQ6L+1estuvsR i8/GSXVUAc6kCwXBenWhUntysbcRSiY= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=SbpZYaZz; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf06.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681317831; a=rsa-sha256; cv=none; b=m8CQ9MuZ9vcNBl9rPyHSUlNN+w86h0XBZcdQ4rsFEN6gP2GanfSef9iI0hW2zh2gRUSIv9 zKigXcM4DHmWYK4mWmluUyMEaKSMx5nLIjhdv9v3OBg3Uv3FuSsiv1N9X5CgQJMeRKb8SC ZzFn5d1ElREgFrNjZZdber2ZNBZwT8s= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681317830; 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=q6fb7dGe8Qh9zpC1XHTDp4auTsAlmohzusSG+yhHPLI=; b=SbpZYaZzvcjlK4be1XLacjpcUdlOPr4MufJ9Lwv5Qvo6ye4VceiPSq9pBLV6p8VTRzvX/J VPOqtm0XZOmT1dIUBBWCC1GIRg3a5ghWW3iOjpD+JwQS4PMUQ/9BDypVriv0Yzm627BV5B R2cqJg422n2mwxoOe1Gftt+OxM2h9JM= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-649-sdb1MHR9O7aYi9oSJjBjCg-1; Wed, 12 Apr 2023 12:43:49 -0400 X-MC-Unique: sdb1MHR9O7aYi9oSJjBjCg-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-74a65b255beso53920585a.0 for ; Wed, 12 Apr 2023 09:43:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681317829; x=1683909829; 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=q6fb7dGe8Qh9zpC1XHTDp4auTsAlmohzusSG+yhHPLI=; b=ICHoUCILaQam7K5QfY3TZXgJ2dLG4wRpJxw6+XLdg3UnchJwYEr6Mpvimo13UvLNlb D/BV75v0B3bOPutTyxqC8dqTcDE4Yfh5WHP+0BsyuA+P2yuAKYPuSsQrBNrqX4vDXjQ0 Z7oQ8V5KTYcwUsJ9keXfISs/lxFKo8vdh6uHjvjnTxRpdh2RIWG/SFDaRSz4AVXr0/k2 zyQJ6rffrGOb8cgYmIzXPGAR6LkSwydX5gGyMxOealDOJwg5tFriqDDjUDGFbV11dqPE YxSFgQ53nFKUMTyEBZkjWqmw4/q+/Qem18qrcp8N3MmxQuZ4SGefBKt/z4G4Rf45AMW/ Y+HA== X-Gm-Message-State: AAQBX9fWPLPlsZxABqv5jguIMgmLjTFac8X9K68rW2RR1JU1n+sTrmNP CuF0m4u9y+u9uMquNLtuSwch6K6ucp8AsQWplnFk7MEdXhfI1twEAAcJ+F2029I5xqrn68gdV+7 hLlOrR2z+pHo= X-Received: by 2002:a05:6214:410e:b0:5e8:e227:982e with SMTP id kc14-20020a056214410e00b005e8e227982emr4492225qvb.3.1681317828924; Wed, 12 Apr 2023 09:43:48 -0700 (PDT) X-Google-Smtp-Source: AKy350bUncXTyJW92k3ESgGa7qeDhT/mpGdY2qSDyncYCia8CnYIFPtqWX76/SVSSYIWo1RN9ImdVA== X-Received: by 2002:a05:6214:410e:b0:5e8:e227:982e with SMTP id kc14-20020a056214410e00b005e8e227982emr4492202qvb.3.1681317828637; Wed, 12 Apr 2023 09:43:48 -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 w23-20020ac86b17000000b003e6948a8966sm3274810qts.21.2023.04.12.09.43.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Apr 2023 09:43:47 -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 21/31] selftests/mm: Allow allocate_area() to fail properly Date: Wed, 12 Apr 2023 12:43:45 -0400 Message-Id: <20230412164345.328659-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-Rspamd-Queue-Id: 713B218000E X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: mtuzwwwo8aaxq9581r1a9x8w1zuugtci X-HE-Tag: 1681317831-722753 X-HE-Meta: U2FsdGVkX1/YvBmrd4UqvDXptFgndo2mZfnsZsMlRBPAczbxPnEPwZZGwFPAQFrUNrVg97X7T26mF/TkPAUivpmCjp0aNOQhQdMHN3XLZxrtRUrtqd/i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kA/kKdQygb/0BMOIlBEISwStJzzZWkRpk7okOczNW+eI38R+YO5CKyUzZH3zStzuquB8A1b4mWKBWzluFqoLtD0CsSEUDAv/YuWH9jQrKA74iqyO9gGmzAVXz8edFTki1HFmkt6azqMekTvnddVRLV6SywsKFhnTGvuL0GaO+VNjaz/uyNWsumzGNYUGGTCCYtZxj9zwXkreRq+NBNky/cTcw7WPWAcOJPQdQKgUSh0AuxDNeCyLNnZw+/zphGdYU+d2h4kKMlm3p3h5Ktyy88ygZzQq9is8+U65aaAKvc= 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: Mostly to detect hugetlb allocation errors and skip hugetlb tests when pages are not allocated. Signed-off-by: Peter Xu --- tools/testing/selftests/mm/uffd-common.c | 47 +++++++++++++++++------- tools/testing/selftests/mm/uffd-common.h | 4 +- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selftests/mm/uffd-common.c index daa2a95408e6..bc6c5c38d6dd 100644 --- a/tools/testing/selftests/mm/uffd-common.c +++ b/tools/testing/selftests/mm/uffd-common.c @@ -44,10 +44,15 @@ static void anon_release_pages(char *rel_area) err("madvise(MADV_DONTNEED) failed"); } -static void anon_allocate_area(void **alloc_area, bool is_src) +static int anon_allocate_area(void **alloc_area, bool is_src) { *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + if (*alloc_area == MAP_FAILED) { + *alloc_area = NULL; + return -errno; + } + return 0; } static void noop_alias_mapping(__u64 *start, size_t len, unsigned long offset) @@ -65,7 +70,7 @@ static void hugetlb_release_pages(char *rel_area) } } -static void hugetlb_allocate_area(void **alloc_area, bool is_src) +static int hugetlb_allocate_area(void **alloc_area, bool is_src) { off_t size = nr_pages * page_size; off_t offset = is_src ? 0 : size; @@ -77,14 +82,16 @@ static void hugetlb_allocate_area(void **alloc_area, bool is_src) (map_shared ? MAP_SHARED : MAP_PRIVATE) | (is_src ? 0 : MAP_NORESERVE), mem_fd, offset); - if (*alloc_area == MAP_FAILED) - err("mmap of hugetlbfs file failed"); + if (*alloc_area == MAP_FAILED) { + *alloc_area = NULL; + return -errno; + } if (map_shared) { area_alias = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, offset); if (area_alias == MAP_FAILED) - err("mmap of hugetlb file alias failed"); + return -errno; } if (is_src) { @@ -96,6 +103,7 @@ static void hugetlb_allocate_area(void **alloc_area, bool is_src) *alloc_area_alias = area_alias; close(mem_fd); + return 0; } static void hugetlb_alias_mapping(__u64 *start, size_t len, unsigned long offset) @@ -112,7 +120,7 @@ static void shmem_release_pages(char *rel_area) err("madvise(MADV_REMOVE) failed"); } -static void shmem_allocate_area(void **alloc_area, bool is_src) +static int shmem_allocate_area(void **alloc_area, bool is_src) { void *area_alias = NULL; size_t bytes = nr_pages * page_size, hpage_size = read_pmd_pagesize(); @@ -132,15 +140,20 @@ static void shmem_allocate_area(void **alloc_area, bool is_src) *alloc_area = mmap(p, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, offset); - if (*alloc_area == MAP_FAILED) - err("mmap of memfd failed"); + if (*alloc_area == MAP_FAILED) { + *alloc_area = NULL; + return -errno; + } if (test_collapse && *alloc_area != p) err("mmap of memfd failed at %p", p); area_alias = mmap(p_alias, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, offset); - if (area_alias == MAP_FAILED) - err("mmap of memfd alias failed"); + if (area_alias == MAP_FAILED) { + munmap(*alloc_area, bytes); + *alloc_area = NULL; + return -errno; + } if (test_collapse && area_alias != p_alias) err("mmap of anonymous memory failed at %p", p_alias); @@ -150,6 +163,7 @@ static void shmem_allocate_area(void **alloc_area, bool is_src) area_dst_alias = area_alias; close(mem_fd); + return 0; } static void shmem_alias_mapping(__u64 *start, size_t len, unsigned long offset) @@ -282,14 +296,19 @@ static void uffd_test_ctx_clear(void) munmap_area((void **)&area_remap); } -void uffd_test_ctx_init(uint64_t features) +int uffd_test_ctx_init(uint64_t features) { unsigned long nr, cpu; + int ret; uffd_test_ctx_clear(); - uffd_test_ops->allocate_area((void **)&area_src, true); - uffd_test_ops->allocate_area((void **)&area_dst, false); + 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) + return ret; userfaultfd_open(&features); @@ -337,6 +356,8 @@ void uffd_test_ctx_init(uint64_t features) for (cpu = 0; cpu < nr_cpus; cpu++) if (pipe2(&pipefd[cpu * 2], O_CLOEXEC | O_NONBLOCK)) err("pipe"); + + return 0; } void wp_range(int ufd, __u64 start, __u64 len, bool wp) diff --git a/tools/testing/selftests/mm/uffd-common.h b/tools/testing/selftests/mm/uffd-common.h index 0ec07d025cfe..9479a0649d7f 100644 --- a/tools/testing/selftests/mm/uffd-common.h +++ b/tools/testing/selftests/mm/uffd-common.h @@ -80,7 +80,7 @@ struct uffd_args { }; struct uffd_test_ops { - void (*allocate_area)(void **alloc_area, bool is_src); + int (*allocate_area)(void **alloc_area, bool is_src); void (*release_pages)(char *rel_area); void (*alias_mapping)(__u64 *start, size_t len, unsigned long offset); void (*check_pmd_mapping)(void *p, int expect_nr_hpages); @@ -101,7 +101,7 @@ 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); -void uffd_test_ctx_init(uint64_t features); +int uffd_test_ctx_init(uint64_t features); void 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);