Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp803021pxj; Thu, 13 May 2021 18:04:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwyp4SfYHegNjNAmcIJoB6UE3uKFQTtuJWqVGDpzQz3tjECwY7ppSImdMtVkSkbc6q5xG1p X-Received: by 2002:a6b:14d2:: with SMTP id 201mr32607369iou.15.1620954242314; Thu, 13 May 2021 18:04:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620954242; cv=none; d=google.com; s=arc-20160816; b=QLYDPBSvrtA7LMXK2Hu6hzR+RpxIn+GWvC3Li3ruyJ1L49+RIha43PFAfE+V5nmEhH sKcFIoU0R8D8WThfGkXIzVMhN4nAQVwmvJ8Jk19jAgmzjW7RfSsFha+e0tERNlAe5W8v yoGxDHJ0MKB5LwrwbsC8tCywlJ2aEYXIAIkMKHWceP4F/PSM0SbWokkOhq0oUR7xSwn3 yN4hY9vNqvWgmy1S1751ouom6PoJ6zSYaNwxCxFUgegK6fM7fr7baHRzIPPjY7c8ddLr L1n+3jvEcRZsRT7bgYPhIBHwT2jk3a2PxAYPXX72jc5zVeftL5CdvjM/W1kW+ouLb2+F joWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=PJaJODXKMxjVlXSx3cQ/3A1Pv1xFsKF6bksiX7iej1w=; b=JgX5ZJkgWRuZPzKQfyegrhIoEcE/974/fj4DVutFYcrWctCYqZ7b8qsjDxuZ3o5ST+ 4OOjCnNXKp2MjDnwhe/IALCaHzehH1EDnavm7Aj9Iadl4pNtOEzl+Yqj6dy+l+JyZbQV 98i2XA+wGJhvW6rLX6hlOFAVudUmJljDqrWN3GMfYhLSZbC5UWh8zwtZGvXm7lKzPztG F1ANGEmozYjmVoxWShjOVmWh+ByaxDuWr5aJEtqVkr07i3Mfjierx38Ok9U+2S9knnU+ UXFaozK0d0uOALZ0O6IhzKWpemSrxgLHmC7nOPXKmB9B/tW/tfLRkPrKabP0jwFATaKB YCPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=J1DE9syf; 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 h7si5215344ilo.77.2021.05.13.18.03.49; Thu, 13 May 2021 18:04:02 -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=J1DE9syf; 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 S233587AbhEMVrm (ORCPT + 99 others); Thu, 13 May 2021 17:47:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233525AbhEMVrm (ORCPT ); Thu, 13 May 2021 17:47:42 -0400 Received: from mail-io1-xd30.google.com (mail-io1-xd30.google.com [IPv6:2607:f8b0:4864:20::d30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC42CC061574 for ; Thu, 13 May 2021 14:46:31 -0700 (PDT) Received: by mail-io1-xd30.google.com with SMTP id k25so26346401iob.6 for ; Thu, 13 May 2021 14:46:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=PJaJODXKMxjVlXSx3cQ/3A1Pv1xFsKF6bksiX7iej1w=; b=J1DE9syfxUpi567p+3iZMa59SpwJFqHrKG4xgw70Iqd/MDnKI+O6GFSeSsyyYr2CZD S93zeGFmXMaKr/PGheaB1+3NsWiCFwWzQgcr64QvRJlyr1x5BZPO3UlIQko4/Rikt7Im BfU18WHoZH2Q+Il+1T0NCbEB1DtkvDzykoE4WvExCQOIMoONp2MA0WhPHE8LexDl0/Pp BCnZ3P8X+w6B2zHdnyVyHrCidJBQU75TIqXvLuuWQ8xPzf2UxqUuT0m/BRqFoDO1wI4W SSBhwMc9xcIlMAFU4fVo6wtUrsRvOuJg63gxvWjP6bhMpolytl7HEkUyzaARZCvJsPuz 3JrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=PJaJODXKMxjVlXSx3cQ/3A1Pv1xFsKF6bksiX7iej1w=; b=QGD55E6OkuCbkhKph7rFIUqd99D6mY9ZDXQ+mKpcg1d9D9VT3zO4/1HNCf8pPyBi2j zqK6ZA6fEDJ+BcYgFrVw4it21V5FffUINlV66g0fRp/duo0IKNMoG12VqpxbHck9eEoE WF8jYiWHcp2xOriNjPhFtCKtzCbRS5TLQEYntm0yxzYB5oVNhKOnShGSIHH+Yd9qqWED 6v+OousXra+dwYvNo1DbPBZ47ynTl0lnFs16oblHFmevqDtPvrKNhh9wwCvAFcCvM/Gi 496S20wHEvXww7X6OaBTUZTeF4sQ5HGVsOPzmSYA/49i9beuTNTKRG9XG/P+8QEqXKEd NTwQ== X-Gm-Message-State: AOAM5324F63E9cGw6wCy/CYIuTH9Lz9PmkoijHtYCE7yQS0PycDwqvOO A4wmKcSN6/WzZBlhQJmlJN3bmFbPpx+A04TlEkBIqw== X-Received: by 2002:a02:b057:: with SMTP id q23mr18067476jah.25.1620942391036; Thu, 13 May 2021 14:46:31 -0700 (PDT) MIME-Version: 1.0 References: <20210512214502.2047008-1-axelrasmussen@google.com> <20210512214502.2047008-3-axelrasmussen@google.com> In-Reply-To: <20210512214502.2047008-3-axelrasmussen@google.com> From: Ben Gardon Date: Thu, 13 May 2021 14:46:20 -0700 Message-ID: Subject: Re: [PATCH 2/5] KVM: selftests: add shmem backing source type To: Axel Rasmussen Cc: Aaron Lewis , Alexander Graf , Andrew Jones , Andrew Morton , Emanuele Giuseppe Esposito , Eric Auger , Jacob Xu , Makarand Sonare , Oliver Upton , Paolo Bonzini , Peter Xu , Shuah Khan , Yanan Wang , kvm , LKML , linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, May 12, 2021 at 2:45 PM Axel Rasmussen wrote: > > 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. It would probably make sense to separate this rename out into a separate commit. > * 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, You could also do this refactoring in a separate commit, which would make the change to support shmem quite small. > }, > [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 >