Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4875599pxj; Wed, 12 May 2021 15:24:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxUnKmidqZ2YCNc2YPEo3bpx/cw1baQDSlCOoyloN+imMBWCj2n+zSv+P5Vv5aLCdySwQLI X-Received: by 2002:a17:906:81c4:: with SMTP id e4mr1662519ejx.27.1620858270534; Wed, 12 May 2021 15:24:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620858270; cv=none; d=google.com; s=arc-20160816; b=Hudxy+cLEYac89VIasNiFcYIFWSmw/Xx84Uamd7ktAeVlKM7B+b4/OvF7Sy+dV2ujV IvPpQYAIjrZu2UT5Rrnhepanftp0tabVWOasDVnB1NOgJuGEJR0q1eokFNAwXYqsb/PT 4NUkPg5Sa91Zs14T8E+4jx9Dtb+vFMyEBrA+4j2ovFfIODu58RRmoL3FAU0wwU08PJCZ Yycu6r8EL1Ff9rvaHbmxLNqrQ8VvBEbUrTAg7cugZejqclQJeBczXuev7Yl4vUDSCbxL InC7V6ENU++rQNzQ5UFlwVM2zhOIfrfeW4hCTjHi8GDTF3tv6/zTQKqn8vbK+fJK7pqM L0Vg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=wp+l+etDZaK1RWI1meWHbah0a6PWFu25ZFjki7jiu7w=; b=NwxCMaGXGLGpMuLfp6TGqfRACF6YODz+em7ueomWsbtx2/CgCeYTdESBl1fSP2h1Hj u3ffh6kjAn1QrAtJDhX3ykf89PMaPjvorGbLXO1T5CwwNRke+Pt4dhtV9xiPhRH2KYUU zudpWEkPNSG5f74IMa0Jhqj6o2cneGeXXP97Mqgujei791P/KzO5vVxmZrD4Y61YH9WM yn7LfjPlCv7JUzCVEZ+M4K7wE1fx3NZP4hzV1yvDrzVIWBno1lrG16BPa2jayEo4vYAL CjOYTjI/IbPyv+P1DGkNJCWph9Wb3/cK17jhjtqzN6mDOukYE1HAODh/831Ey/zMu7W1 a2sA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=EkF3AZss; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i8si1504947ejj.196.2021.05.12.15.24.06; Wed, 12 May 2021 15:24:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=EkF3AZss; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443296AbhELWR1 (ORCPT + 99 others); Wed, 12 May 2021 18:17:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1389444AbhELVtS (ORCPT ); Wed, 12 May 2021 17:49:18 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 68F72C06125E for ; Wed, 12 May 2021 14:45:10 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id l9-20020a5b0b890000b02904f7fb53ca12so29855809ybq.15 for ; Wed, 12 May 2021 14:45:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=wp+l+etDZaK1RWI1meWHbah0a6PWFu25ZFjki7jiu7w=; b=EkF3AZsswZ4D1DidUCRt1PAYTCJt/Tm1mwc+1G7Hd+1LrjWF/Lq7S8NYIvvzN3OTaY u19y/WeHzEAqhEDsiSgBEjB/Wu/CIQbM/lNCQNcqY2LtHCG6FjAFdtOpDSZE6agd4q3V Eyv4rgoVfy1GKkqvmm2fFPvkVVQUnmYjT0jHK7YcldAYWX+fCFnbqTBjo9wvLEagbDPQ /4f/OBU/yfSDs99EVlVZQ2O193et92/C1dahmzb+0NMqH4lmGoh1SLkVcFffHvVJ/i4F Lpv9XUD551KaahorSX786BUiZIPjUXkLLCtoETZiK3moP3QqiGpQXdyYcVFzLHxK+Ic9 nhTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=wp+l+etDZaK1RWI1meWHbah0a6PWFu25ZFjki7jiu7w=; b=TAQ7bydrc0AnyO3o2v+eF1vsEAfh+rzFbzBVU1bL6MIjSi1lyWwNG6GFXbUqDJYEyk U1PpDkP39xmKIGhOFRmZvGz1IA3LgrphN7oL3xMqUx86D3kZDBAXCYqg2VMmj+g+AEwM QSTJnPYYJCY+hOQ+ZigrXCpRP3ALjwZvEW0/UsD1X+fl2ZGDCpgyy2FyulFBbVu6rM9D NhOT5K/RXTYeONOdonjJZ3PP8UNwo8CtNIKeXZ9+O8dyftJaE4Kmk49WUpDFYP3sMXt6 jKsEkOHsC55Xakxpqu/auukvYSMou1v1e1D3HNMxAzMHc8sI9Kr11Z3kLPknLABak6X9 ZXSA== X-Gm-Message-State: AOAM530kIxs7FVYoNiDoXnCfD9V/rMnQm6hjBzSnmP6X82g5ByO/YizR qF1gtfpMubqGV2PYGp2bnY70JnB1TlhYiwTpX8ve X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:29e5:10fc:1128:b0c0]) (user=axelrasmussen job=sendgmr) by 2002:a25:3213:: with SMTP id y19mr731816yby.236.1620855909652; Wed, 12 May 2021 14:45:09 -0700 (PDT) Date: Wed, 12 May 2021 14:44:59 -0700 In-Reply-To: <20210512214502.2047008-1-axelrasmussen@google.com> Message-Id: <20210512214502.2047008-3-axelrasmussen@google.com> Mime-Version: 1.0 References: <20210512214502.2047008-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.31.1.607.g51e8a6a459-goog Subject: [PATCH 2/5] KVM: selftests: add shmem backing source type From: Axel Rasmussen To: Aaron Lewis , Alexander Graf , Andrew Jones , Andrew Morton , Ben Gardon , Emanuele Giuseppe Esposito , Eric Auger , Jacob Xu , Makarand Sonare , Oliver Upton , Paolo Bonzini , Peter Xu , Shuah Khan , Yanan Wang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Axel Rasmussen Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This lets us run the demand paging test on top of a shmem-backed area. In follow-up commits, we'll 1) leverage this new capability to create an alias mapping, and then 2) use the alias mapping to exercise UFFD minor faults. Signed-off-by: Axel Rasmussen --- .../testing/selftests/kvm/include/test_util.h | 1 + tools/testing/selftests/kvm/lib/kvm_util.c | 24 ++++++++--- tools/testing/selftests/kvm/lib/test_util.c | 40 +++++++++++-------- 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h index fade3130eb01..7377f00469ef 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -84,6 +84,7 @@ enum vm_mem_backing_src_type { VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB, VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB, VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB, + VM_MEM_SRC_SHMEM, NUM_SRC_TYPES, }; diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index fc83f6c5902d..6fbe124e0e16 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -663,8 +663,8 @@ int kvm_memcmp_hva_gva(void *hva, struct kvm_vm *vm, vm_vaddr_t gva, size_t len) * * Input Args: * vm - Virtual Machine - * backing_src - Storage source for this region. - * NULL to use anonymous memory. + * src_type - Storage source for this region. + * NULL to use anonymous memory. * guest_paddr - Starting guest physical address * slot - KVM region slot * npages - Number of physical pages @@ -755,11 +755,25 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, if (alignment > 1) region->mmap_size += alignment; + region->fd = -1; + if (src_type == VM_MEM_SRC_SHMEM) { + region->fd = memfd_create("kvm_selftest", MFD_CLOEXEC); + TEST_ASSERT(region->fd != -1, + "memfd_create failed, errno: %i", errno); + + ret = ftruncate(region->fd, region->mmap_size); + TEST_ASSERT(ret == 0, "ftruncate failed, errno: %i", errno); + + ret = fallocate(region->fd, + FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, + region->mmap_size); + TEST_ASSERT(ret == 0, "fallocate failed, errno: %i", errno); + } + region->mmap_start = mmap(NULL, region->mmap_size, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS - | vm_mem_backing_src_alias(src_type)->flag, - -1, 0); + vm_mem_backing_src_alias(src_type)->flag, + region->fd, 0); TEST_ASSERT(region->mmap_start != MAP_FAILED, "test_malloc failed, mmap_start: %p errno: %i", region->mmap_start, errno); diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c index 63d2bc7d757b..c7a265da5090 100644 --- a/tools/testing/selftests/kvm/lib/test_util.c +++ b/tools/testing/selftests/kvm/lib/test_util.c @@ -168,70 +168,77 @@ size_t get_def_hugetlb_pagesz(void) const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i) { + static const int anon_flags = MAP_PRIVATE | MAP_ANONYMOUS; + static const int anon_huge_flags = anon_flags | MAP_HUGETLB; + static const struct vm_mem_backing_src_alias aliases[] = { [VM_MEM_SRC_ANONYMOUS] = { .name = "anonymous", - .flag = 0, + .flag = anon_flags, }, [VM_MEM_SRC_ANONYMOUS_THP] = { .name = "anonymous_thp", - .flag = 0, + .flag = anon_flags, }, [VM_MEM_SRC_ANONYMOUS_HUGETLB] = { .name = "anonymous_hugetlb", - .flag = MAP_HUGETLB, + .flag = anon_huge_flags, }, [VM_MEM_SRC_ANONYMOUS_HUGETLB_16KB] = { .name = "anonymous_hugetlb_16kb", - .flag = MAP_HUGETLB | MAP_HUGE_16KB, + .flag = anon_huge_flags | MAP_HUGE_16KB, }, [VM_MEM_SRC_ANONYMOUS_HUGETLB_64KB] = { .name = "anonymous_hugetlb_64kb", - .flag = MAP_HUGETLB | MAP_HUGE_64KB, + .flag = anon_huge_flags | MAP_HUGE_64KB, }, [VM_MEM_SRC_ANONYMOUS_HUGETLB_512KB] = { .name = "anonymous_hugetlb_512kb", - .flag = MAP_HUGETLB | MAP_HUGE_512KB, + .flag = anon_huge_flags | MAP_HUGE_512KB, }, [VM_MEM_SRC_ANONYMOUS_HUGETLB_1MB] = { .name = "anonymous_hugetlb_1mb", - .flag = MAP_HUGETLB | MAP_HUGE_1MB, + .flag = anon_huge_flags | MAP_HUGE_1MB, }, [VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB] = { .name = "anonymous_hugetlb_2mb", - .flag = MAP_HUGETLB | MAP_HUGE_2MB, + .flag = anon_huge_flags | MAP_HUGE_2MB, }, [VM_MEM_SRC_ANONYMOUS_HUGETLB_8MB] = { .name = "anonymous_hugetlb_8mb", - .flag = MAP_HUGETLB | MAP_HUGE_8MB, + .flag = anon_huge_flags | MAP_HUGE_8MB, }, [VM_MEM_SRC_ANONYMOUS_HUGETLB_16MB] = { .name = "anonymous_hugetlb_16mb", - .flag = MAP_HUGETLB | MAP_HUGE_16MB, + .flag = anon_huge_flags | MAP_HUGE_16MB, }, [VM_MEM_SRC_ANONYMOUS_HUGETLB_32MB] = { .name = "anonymous_hugetlb_32mb", - .flag = MAP_HUGETLB | MAP_HUGE_32MB, + .flag = anon_huge_flags | MAP_HUGE_32MB, }, [VM_MEM_SRC_ANONYMOUS_HUGETLB_256MB] = { .name = "anonymous_hugetlb_256mb", - .flag = MAP_HUGETLB | MAP_HUGE_256MB, + .flag = anon_huge_flags | MAP_HUGE_256MB, }, [VM_MEM_SRC_ANONYMOUS_HUGETLB_512MB] = { .name = "anonymous_hugetlb_512mb", - .flag = MAP_HUGETLB | MAP_HUGE_512MB, + .flag = anon_huge_flags | MAP_HUGE_512MB, }, [VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB] = { .name = "anonymous_hugetlb_1gb", - .flag = MAP_HUGETLB | MAP_HUGE_1GB, + .flag = anon_huge_flags | MAP_HUGE_1GB, }, [VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB] = { .name = "anonymous_hugetlb_2gb", - .flag = MAP_HUGETLB | MAP_HUGE_2GB, + .flag = anon_huge_flags | MAP_HUGE_2GB, }, [VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB] = { .name = "anonymous_hugetlb_16gb", - .flag = MAP_HUGETLB | MAP_HUGE_16GB, + .flag = anon_huge_flags | MAP_HUGE_16GB, + }, + [VM_MEM_SRC_SHMEM] = { + .name = "shmem", + .flag = MAP_SHARED, }, }; _Static_assert(ARRAY_SIZE(aliases) == NUM_SRC_TYPES, @@ -250,6 +257,7 @@ size_t get_backing_src_pagesz(uint32_t i) switch (i) { case VM_MEM_SRC_ANONYMOUS: + case VM_MEM_SRC_SHMEM: return getpagesize(); case VM_MEM_SRC_ANONYMOUS_THP: return get_trans_hugepagesz(); -- 2.31.1.607.g51e8a6a459-goog