Re: [PATCH v2 06/10] KVM: selftests: refactor vm_mem_backing_src_type flags

From: Ben Gardon
Date: Wed May 19 2021 - 18:25:27 EST


On Wed, May 19, 2021 at 3:17 PM Axel Rasmussen <axelrasmussen@xxxxxxxxxx> wrote:
>
> On Wed, May 19, 2021 at 3:02 PM Ben Gardon <bgardon@xxxxxxxxxx> wrote:
> >
> > On Wed, May 19, 2021 at 1:04 PM Axel Rasmussen <axelrasmussen@xxxxxxxxxx> wrote:
> > >
> > > Each struct vm_mem_backing_src_alias has a flags field, which denotes
> > > the flags used to mmap() an area of that type. Previously, this field
> > > never included MAP_PRIVATE | MAP_ANONYMOUS, because
> > > vm_userspace_mem_region_add assumed that *all* types would always use
> > > those flags, and so it hardcoded them.
> > >
> > > In a follow-up commit, we'll add a new type: shmem. Areas of this type
> > > must not have MAP_PRIVATE | MAP_ANONYMOUS, and instead they must have
> > > MAP_SHARED.
> > >
> > > So, refactor things. Make it so that the flags field of
> > > struct vm_mem_backing_src_alias really is a complete set of flags, and
> > > don't add in any extras in vm_userspace_mem_region_add. This will let us
> > > easily tack on shmem.
> > >
> > > Signed-off-by: Axel Rasmussen <axelrasmussen@xxxxxxxxxx>
> > > ---
> > > tools/testing/selftests/kvm/lib/kvm_util.c | 5 ++-
> > > tools/testing/selftests/kvm/lib/test_util.c | 35 +++++++++++----------
> > > 2 files changed, 21 insertions(+), 19 deletions(-)
> > >
> > > diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
> > > index 0d6ddee429b9..bc405785ac8b 100644
> > > --- a/tools/testing/selftests/kvm/lib/kvm_util.c
> > > +++ b/tools/testing/selftests/kvm/lib/kvm_util.c
> > > @@ -759,9 +759,8 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
> > >
> > > 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);
> >
> > I don't see the region->fd change mentioned in the patch description
> > or elsewhere in this patch. Is something setting region->fd to -1 or
> > should this be part of another patch in the series?
>
> Ah, apologies, this is a mistake from splitting up the commits. When
> they were all squashed together, we set region->fd = -1 explicitly
> just above here, but with them separated we can't depend on that. I'll
> fix this in a v3.

Thanks for fixing that and thanks for splitting up the patches in this
series. It made them super easy to review.

>
> >
> > > 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..06ddde068736 100644
> > > --- a/tools/testing/selftests/kvm/lib/test_util.c
> > > +++ b/tools/testing/selftests/kvm/lib/test_util.c
> > > @@ -168,70 +168,73 @@ 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,
> > > },
> > > };
> > > _Static_assert(ARRAY_SIZE(aliases) == NUM_SRC_TYPES,
> > > --
> > > 2.31.1.751.gd2f1c929bd-goog
> > >