Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp796551pxb; Thu, 25 Feb 2021 15:44:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJwwYom9QLt8NL2gomJjPcAegc9v2gVj6TSb8StuyQq85qfCxvxRmv0vCKSjuzlFEMuBaxfw X-Received: by 2002:a17:906:4d99:: with SMTP id s25mr80263eju.351.1614296677283; Thu, 25 Feb 2021 15:44:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614296677; cv=none; d=google.com; s=arc-20160816; b=lZZSseX3/jzabAnj/nKkWEHG0fd8v1LhLBU0vH+YbcRkITyvmIcjLVmxYUAwy53tX/ bl9ytmcOF/qj9Q351rjlWy5C94ChJU40hfrBvwgWNGSRl1aK7ooO5pqoakbfdN/uK9n9 03qiLzigNz9DofQe3+PGl3bT21VmQ+ZiYatgvyGKEHR1LWkvDw50JywtxpEeOH81DaaE c0/YJbAkI3owu98mXR0AxlUX6K8+9g5eZt39jgX8MEStpLpvL3QMEXE4ueUrPMmPXLM0 899R3v2wQl37q05bK/Hb+t00H4ilVLDA/F+M7QT+DpDRxXZ0aLJOeHGpYDalXz968Niq EH6w== 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=XphAUbG60f0ERLCc6OAZU4eM2wreH7bgE4/Hsccw1VQ=; b=gGdPzI0wt1EuGCRHSGLIP5A+Q7niVvKpz0LHvql+IJ+0QEqAmubGgWVQedSZ0VDjDD xYppjknfXb6j7+NenvowkvStaWN2IYm1zhJZ+5ulCpuqbVqLFYJLWcwQjO88Y1paHorI +UnnrBqX7TsuwbTnGYbXdJwEL4nZaTTDKzC3AmwW9MIawxsz+1P8ZkQXd4P/TZyLjTw+ jh3+mlBW3HoZLFkTSjMrZCxaF+EwM+HOOjv65uqsgRbGfp1ND2A6Fv9oFq1dCvd6AeJE Or40GPj6lm4xPfNqSm1K1mzNe6iREpQXIhICHiYduu7kpJgW7vI3cYxUnkEC4JrNWnM+ cwkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tcaXJM4p; 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 a15si4018310ejc.294.2021.02.25.15.44.13; Thu, 25 Feb 2021 15:44:37 -0800 (PST) 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=tcaXJM4p; 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 S231822AbhBYXnk (ORCPT + 99 others); Thu, 25 Feb 2021 18:43:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229966AbhBYXni (ORCPT ); Thu, 25 Feb 2021 18:43:38 -0500 Received: from mail-il1-x12a.google.com (mail-il1-x12a.google.com [IPv6:2607:f8b0:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95204C06174A for ; Thu, 25 Feb 2021 15:42:58 -0800 (PST) Received: by mail-il1-x12a.google.com with SMTP id e7so6507275ile.7 for ; Thu, 25 Feb 2021 15:42:58 -0800 (PST) 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=XphAUbG60f0ERLCc6OAZU4eM2wreH7bgE4/Hsccw1VQ=; b=tcaXJM4pFjBOUGyrlT+AD3x0gZy3lr4vUnBA82I8J97gAY/r+Wbo5KFQKNhH+QK+Ye VdG/gLColnZP9NkLrsMwPBxd1JZRCCLwMMPXTNtdEEaAX8zm6yM1EwFa5HUR2cFOCmyA D6CiUKjIVVaQ9vFZJkuNB2Oxio27nJWRGHE0f6YMoeqWiHBLRnqpzafFtV4Cv0KDcfkU NtlI76N7V0XVaq03BzSDZL9CAUT7Q1P8FSi5/M7aRd0X6+Fvz8mdkMtDWURV0qzichsR 4VuqG/6nrryVSRYPcu2y5GAsQckyNFAfrag6tnoR/opBuoj+mORijIzdfptff3fU30a2 VP5w== 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=XphAUbG60f0ERLCc6OAZU4eM2wreH7bgE4/Hsccw1VQ=; b=rZDi47wvZjypqK+984I3N1Sxjft+EPd4Y9WXuwpkCEmoCul1yY4Un2vcLbKM3pW2jv NHnw7q4JpxzPyBRwUSu/pVvNj+rqwnUaVg7yPX45D/RfKnNV1dut0mScljjp1V3l4Nhc d6wWZj8pK4jOya0Xy81T6ecjqpiEkbLk4H5QImy72iBaOCcdAUXC4tGg/YyEFAA8krzQ imjPRAzW0U0A020DwuDP0JzNqjXURSZ50QV3GPlDUcM2F/5fItEJ6ia+5xJlDBKJo8Ua Q6PUqjG9JZlk/o9zqV0GeVa8d9fEM0CDgeiTezH98SfshEBndK60P60GGblLSDLDf8iG OSMQ== X-Gm-Message-State: AOAM533P5QdNH+XIanO7c7ADxtr2gcgufysUKuBxss9uc5Dpt69AU3Xv W/GKoMsyXkAW08T1voXFDdyMyOqxyILx6uWVbrxTYA== X-Received: by 2002:a92:c54e:: with SMTP id a14mr119129ilj.285.1614296577593; Thu, 25 Feb 2021 15:42:57 -0800 (PST) MIME-Version: 1.0 References: <20210225055940.18748-1-wangyanan55@huawei.com> <20210225055940.18748-6-wangyanan55@huawei.com> In-Reply-To: <20210225055940.18748-6-wangyanan55@huawei.com> From: Ben Gardon Date: Thu, 25 Feb 2021 15:42:46 -0800 Message-ID: Subject: Re: [RFC PATCH v2 5/7] KVM: selftests: List all hugetlb src types specified with page sizes To: Yanan Wang Cc: kvm , linux-kselftest@vger.kernel.org, LKML , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Arnaldo Carvalho de Melo , Ingo Molnar , Andrew Jones , Peter Xu , Marc Zyngier , wanghaibin.wang@huawei.com, yuzenghui@huawei.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 24, 2021 at 10:03 PM Yanan Wang wrote: > > With VM_MEM_SRC_ANONYMOUS_HUGETLB, we currently can only use system > default hugetlb pages to back the testing guest memory. In order to > add flexibility, now list all the known hugetlb backing src types with > different page sizes, so that we can specify use of hugetlb pages of the > exact granularity that we want. And as all the known hugetlb page sizes > are listed, it's appropriate for all architectures. > > Besides, the helper get_backing_src_pagesz() is added to get the > granularity of different backing src types(anonumous, thp, hugetlb). > > Signed-off-by: Yanan Wang > --- > .../testing/selftests/kvm/include/test_util.h | 19 ++++++- > tools/testing/selftests/kvm/lib/kvm_util.c | 2 +- > tools/testing/selftests/kvm/lib/test_util.c | 56 +++++++++++++++---- > 3 files changed, 63 insertions(+), 14 deletions(-) > > diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h > index ef24c76ba89a..be5d08bcdca7 100644 > --- a/tools/testing/selftests/kvm/include/test_util.h > +++ b/tools/testing/selftests/kvm/include/test_util.h > @@ -70,16 +70,31 @@ struct timespec timespec_div(struct timespec ts, int divisor); > enum vm_mem_backing_src_type { > VM_MEM_SRC_ANONYMOUS, > VM_MEM_SRC_ANONYMOUS_THP, > - VM_MEM_SRC_ANONYMOUS_HUGETLB, > + VM_MEM_SRC_ANONYMOUS_HUGETLB_16KB, > + VM_MEM_SRC_ANONYMOUS_HUGETLB_64KB, > + VM_MEM_SRC_ANONYMOUS_HUGETLB_512KB, > + VM_MEM_SRC_ANONYMOUS_HUGETLB_1MB, > + VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB, > + VM_MEM_SRC_ANONYMOUS_HUGETLB_8MB, > + VM_MEM_SRC_ANONYMOUS_HUGETLB_16MB, > + VM_MEM_SRC_ANONYMOUS_HUGETLB_32MB, > + VM_MEM_SRC_ANONYMOUS_HUGETLB_256MB, > + VM_MEM_SRC_ANONYMOUS_HUGETLB_512MB, > + VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB, > + VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB, > + VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB, > + NUM_SRC_TYPES, > }; > > struct vm_mem_backing_src_alias { > const char *name; > - enum vm_mem_backing_src_type type; > + uint32_t flag; > }; > > bool thp_configured(void); > size_t get_trans_hugepagesz(void); > +const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i); > +size_t get_backing_src_pagesz(uint32_t i); > void backing_src_help(void); > enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name); > > diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c > index cc22c4ab7d67..b91c8e3a7ee1 100644 > --- a/tools/testing/selftests/kvm/lib/kvm_util.c > +++ b/tools/testing/selftests/kvm/lib/kvm_util.c > @@ -757,7 +757,7 @@ 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 > - | (src_type == VM_MEM_SRC_ANONYMOUS_HUGETLB ? MAP_HUGETLB : 0), > + | vm_mem_backing_src_alias(src_type)->flag, > -1, 0); > TEST_ASSERT(region->mmap_start != MAP_FAILED, > "test_malloc failed, mmap_start: %p errno: %i", > diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c > index f2d133f76c67..6780aa058f35 100644 > --- a/tools/testing/selftests/kvm/lib/test_util.c > +++ b/tools/testing/selftests/kvm/lib/test_util.c > @@ -11,6 +11,7 @@ > #include > #include > #include > +#include > #include "linux/kernel.h" > > #include "test_util.h" > @@ -112,12 +113,6 @@ void print_skip(const char *fmt, ...) > puts(", skipping test"); > } > > -const struct vm_mem_backing_src_alias backing_src_aliases[] = { > - {"anonymous", VM_MEM_SRC_ANONYMOUS,}, > - {"anonymous_thp", VM_MEM_SRC_ANONYMOUS_THP,}, > - {"anonymous_hugetlb", VM_MEM_SRC_ANONYMOUS_HUGETLB,}, > -}; > - > bool thp_configured(void) > { > int ret; > @@ -153,22 +148,61 @@ size_t get_trans_hugepagesz(void) > return size; > } > > +const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i) > +{ > + static const struct vm_mem_backing_src_alias aliases[] = { > + { "anonymous", 0 }, > + { "anonymous_thp", 0 }, > + { "anonymous_hugetlb_16kb", MAP_HUGETLB | MAP_HUGE_16KB }, > + { "anonymous_hugetlb_64kb", MAP_HUGETLB | MAP_HUGE_64KB }, > + { "anonymous_hugetlb_512kb", MAP_HUGETLB | MAP_HUGE_512KB }, > + { "anonymous_hugetlb_1mb", MAP_HUGETLB | MAP_HUGE_1MB }, > + { "anonymous_hugetlb_2mb", MAP_HUGETLB | MAP_HUGE_2MB }, > + { "anonymous_hugetlb_8mb", MAP_HUGETLB | MAP_HUGE_8MB }, > + { "anonymous_hugetlb_16mb", MAP_HUGETLB | MAP_HUGE_16MB }, > + { "anonymous_hugetlb_32mb", MAP_HUGETLB | MAP_HUGE_32MB }, > + { "anonymous_hugetlb_256mb", MAP_HUGETLB | MAP_HUGE_256MB }, > + { "anonymous_hugetlb_512mb", MAP_HUGETLB | MAP_HUGE_512MB }, > + { "anonymous_hugetlb_1gb", MAP_HUGETLB | MAP_HUGE_1GB }, > + { "anonymous_hugetlb_2gb", MAP_HUGETLB | MAP_HUGE_2GB }, > + { "anonymous_hugetlb_16gb", MAP_HUGETLB | MAP_HUGE_16GB }, > + }; > + _Static_assert(ARRAY_SIZE(aliases) == NUM_SRC_TYPES, > + "Missing new backing src types?"); > + > + TEST_ASSERT(i < NUM_SRC_TYPES, "Backing src type ID %d too big", i); > + > + return aliases + i; NIT: This would be easier for me to read as return &aliases[i]; > +} > + > +size_t get_backing_src_pagesz(uint32_t i) > +{ > + uint32_t flag = vm_mem_backing_src_alias(i)->flag; > + > + if (i == VM_MEM_SRC_ANONYMOUS) > + return getpagesize(); > + if (i == VM_MEM_SRC_ANONYMOUS_THP) > + return get_trans_hugepagesz(); > + > + return 1ULL << (flag >> HUGETLB_FLAG_ENCODE_SHIFT); It might also be nice to add a macro where MAP_HUGE_1GB and company are defined to get the size for a given hugeTLB type. I think you're mixing macros here too (which works because they're defined off one another) but it's a little confusing. MAP_HUGE_1GB and company are defined in tools/include/uapi/linux/mman.h as equal to the values HUGETLB_FLAG_ENCODE_1GB and friends in tools/include/asm-generic/hugetlb_encode.h, but it seems like it would be good to use MAP_HUGE_SHIFT to match in that case. I think this all also relies on MAP_HUGETLB being in the bits less than MAP_HUGE_SHIFT which isn't stated here. All up, I would add a macro to tools/include/uapi/linux/mman.h: #define MAP_HUGE_PAGE_SIZE(x) (1ULL << ((x >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK)) Or something similar, and use that here. > +} > + > void backing_src_help(void) > { > int i; > > printf("Available backing src types:\n"); > - for (i = 0; i < ARRAY_SIZE(backing_src_aliases); i++) > - printf("\t%s\n", backing_src_aliases[i].name); > + for (i = 0; i < NUM_SRC_TYPES; i++) > + printf("\t%s\n", vm_mem_backing_src_alias(i)->name); > } > > enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name) > { > int i; > > - for (i = 0; i < ARRAY_SIZE(backing_src_aliases); i++) > - if (!strcmp(type_name, backing_src_aliases[i].name)) > - return backing_src_aliases[i].type; > + for (i = 0; i < NUM_SRC_TYPES; i++) > + if (!strcmp(type_name, vm_mem_backing_src_alias(i)->name)) > + return i; > > backing_src_help(); > TEST_FAIL("Unknown backing src type: %s", type_name); > -- > 2.19.1 >