Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp798583pxb; Thu, 25 Feb 2021 15:49:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJyPdMfz7Xy+MUpQAhYdNMWynlm4rgwgzrfN2nFjhUoSPPzdfeBsXvPueYOXw8lzs3vx/zB7 X-Received: by 2002:a05:6402:229a:: with SMTP id cw26mr435866edb.224.1614296951269; Thu, 25 Feb 2021 15:49:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614296951; cv=none; d=google.com; s=arc-20160816; b=i16w437KzpEo53auk1F9lYpE5TJ/B0RwMsmmQ1i//VAeFSFKKfkYalNfwzQdqHQbYS Y3/Gr/lSoLiwNnasnufXK7fD0P7ewTrmhgn2jMx/mYtg8PTTAgS/mh5WI5IsKD5MOgWL qw0tzLUx95BP3NZAo0M+uNO/e57Xt55VShfkcl400JQq/F6uHtm87tBhi26YXeQg99P7 IeunWL9W2Q1UD3gvpXvNSqBbWz01rag35sn3elHKMIl3lX1l3iGaO2n0KgJIjRxQ1xjD oJlQ1ySSFuEDoiELGDSRMUTTr3MSwVheFBnFUbOnYaIiLqU2vu2ToMibTrxXckb00bOS BS7w== 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=dLG1p58PGnMeY59AWuFvydGWLoGBCF7NOPt6FH+DeFY=; b=sUwHmvfzY96f2+LDfQ7zUgUM4BwctaKsVCzogd5PjjQammHw1edbDW7KzalJ3LgFGd ZwhjINpdzLY/xdH63XJjj8NvhXr8INqeRB8nWBYARU/Qn8HiKpTCsJkE9KpYJ6hAMoFl 6fKtTm/tOZX+fcfV9FlmPpljM2bIJwsmf+63HrkF32yzWwDHXKozbBfYh9cQ+V5R9Hqq p6ycuynhU/nPaKAjgx5GihNici86Lma/IDsYyVhW22KN7tAkJ9hx16J+m8dpWO+PxWPT VhFN97C9S7MzbyHm0HjlYhAnt/0oRS290ARS03X0ciy+Jj650eNSwECLtwhFD7XsLjS9 XbEQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=heegkJvk; 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 v7si4511663edj.572.2021.02.25.15.48.48; Thu, 25 Feb 2021 15:49:11 -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=heegkJvk; 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 S232077AbhBYXra (ORCPT + 99 others); Thu, 25 Feb 2021 18:47:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233204AbhBYXpU (ORCPT ); Thu, 25 Feb 2021 18:45:20 -0500 Received: from mail-il1-x133.google.com (mail-il1-x133.google.com [IPv6:2607:f8b0:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC6EBC061756 for ; Thu, 25 Feb 2021 15:44:40 -0800 (PST) Received: by mail-il1-x133.google.com with SMTP id d5so6537733iln.6 for ; Thu, 25 Feb 2021 15:44:40 -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=dLG1p58PGnMeY59AWuFvydGWLoGBCF7NOPt6FH+DeFY=; b=heegkJvkrTBZirIS56mT/RBkWAmZwf/NMGW4/ZI0wY4XFSepwTMXcfEs8vlEQ3UpWE AsizNPevw//YyLeJciOVLFdlcHc/6i6y3vlcDpzOPRamaF0e/QUGSsgHRcEa+y8xgO7y 1ohxFWP38vpxKlsbj2JxS2iqbASH1qqlDg6X3+kFob0vqlUARsPG8lJpEtoyt0B77HrR 9XmvSFzxvyPBwJg7ixL61/vwx0YOoL+M2+wXz/DrkI86wjOqRfX54hhiA5OHGV+Y41RN aZK+HJcWADj1E4IKNABpA2CeEhklAGH3SXV5ykkjeiYkekW42pQmjGTf3EfZUXVL2CgX ul9A== 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=dLG1p58PGnMeY59AWuFvydGWLoGBCF7NOPt6FH+DeFY=; b=X3Am/jRm3VC+v1CK757jgG50hYYfDQbequeoXAt7hVOzozpUynYSSLmRpL1M8cLs75 QUiN8XgCCuIMijdw35uVpW/dmCYk/J7077ALK6nZr2zlQynze+0/5C6rqmUiGikVBgpM V21PZq+A/+v7nCp1j2yn/h0ogw0FsI6eBzrXY+wzzUBcG/WhJv6AnGNrjDWIyih2bjML EXl4cDQAsK1bRqkPFkLGGn8Mj4AixFfMiQw9uAal0cgrz6Cgvb+okkkEQ/Vn5lBMPW40 PXvMfXKZFMqQ1w0Eiyoq2Z9qjWrRPQ03EUDXH0D+boihU9ZAe2JAZe9kG7+UKklKLwzK ZTEg== X-Gm-Message-State: AOAM532JZJ9J5BWP6Fpclf8YGGZrUfqB0AaZ0ZtGinhc/Tr8ULLMS7Bn zC6MyoB7q2fVAUqc7mxWfkFbJCBXAAINh9h32BN+jhz4HUs= X-Received: by 2002:a05:6e02:1888:: with SMTP id o8mr151577ilu.154.1614296679963; Thu, 25 Feb 2021 15:44:39 -0800 (PST) MIME-Version: 1.0 References: <20210225055940.18748-1-wangyanan55@huawei.com> <20210225055940.18748-7-wangyanan55@huawei.com> In-Reply-To: <20210225055940.18748-7-wangyanan55@huawei.com> From: Ben Gardon Date: Thu, 25 Feb 2021 15:44:28 -0800 Message-ID: Subject: Re: [RFC PATCH v2 6/7] KVM: selftests: Adapt vm_userspace_mem_region_add to new helpers 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_THP specified in vm_userspace_mem_region_add(), > we have to get the transparent hugepage size for HVA alignment. With the > new helpers, we can use get_backing_src_pagesz() to check whether THP is > configured and then get the exact configured hugepage size. > > As different architectures may have different THP page sizes configured, > this can get the accurate THP page sizes on any platform. > > Signed-off-by: Yanan Wang > --- > tools/testing/selftests/kvm/lib/kvm_util.c | 27 +++++++--------------- > 1 file changed, 8 insertions(+), 19 deletions(-) > > diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c > index b91c8e3a7ee1..0105fbfed036 100644 > --- a/tools/testing/selftests/kvm/lib/kvm_util.c > +++ b/tools/testing/selftests/kvm/lib/kvm_util.c > @@ -18,7 +18,6 @@ > #include > #include > > -#define KVM_UTIL_PGS_PER_HUGEPG 512 > #define KVM_UTIL_MIN_PFN 2 > > /* Aligns x up to the next multiple of size. Size must be a power of 2. */ > @@ -686,7 +685,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, > { > int ret; > struct userspace_mem_region *region; > - size_t huge_page_size = KVM_UTIL_PGS_PER_HUGEPG * vm->page_size; > + size_t backing_src_pagesz = get_backing_src_pagesz(src_type); > size_t alignment; > > TEST_ASSERT(vm_adjust_num_guest_pages(vm->mode, npages) == npages, > @@ -748,7 +747,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, > #endif > > if (src_type == VM_MEM_SRC_ANONYMOUS_THP) > - alignment = max(huge_page_size, alignment); > + alignment = max(backing_src_pagesz, alignment); > > /* Add enough memory to align up if necessary */ > if (alignment > 1) > @@ -767,22 +766,12 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, > region->host_mem = align(region->mmap_start, alignment); > > /* As needed perform madvise */ > - if (src_type == VM_MEM_SRC_ANONYMOUS || src_type == VM_MEM_SRC_ANONYMOUS_THP) { > - struct stat statbuf; > - > - ret = stat("/sys/kernel/mm/transparent_hugepage", &statbuf); > - TEST_ASSERT(ret == 0 || (ret == -1 && errno == ENOENT), > - "stat /sys/kernel/mm/transparent_hugepage"); > - > - TEST_ASSERT(ret == 0 || src_type != VM_MEM_SRC_ANONYMOUS_THP, > - "VM_MEM_SRC_ANONYMOUS_THP requires THP to be configured in the host kernel"); > - > - if (ret == 0) { > - ret = madvise(region->host_mem, npages * vm->page_size, > - src_type == VM_MEM_SRC_ANONYMOUS ? MADV_NOHUGEPAGE : MADV_HUGEPAGE); > - TEST_ASSERT(ret == 0, "madvise failed, addr: %p length: 0x%lx src_type: %x", > - region->host_mem, npages * vm->page_size, src_type); > - } > + if (src_type <= VM_MEM_SRC_ANONYMOUS_THP && thp_configured()) { This check relies on an unstated property of the backing src type enums where VM_MEM_SRC_ANONYMOUS and VM_MEM_SRC_ANONYMOUS_THP are declared first. It would probably be more readable for folks if the check was explicit: if ((src_type == VM_MEM_SRC_ANONYMOUS || src_type == VM_MEM_SRC_ANONYMOUS_THP) && thp_configured()) { > + ret = madvise(region->host_mem, npages * vm->page_size, > + src_type == VM_MEM_SRC_ANONYMOUS ? MADV_NOHUGEPAGE : MADV_HUGEPAGE); > + TEST_ASSERT(ret == 0, "madvise failed, addr: %p length: 0x%lx src_type: %s", > + region->host_mem, npages * vm->page_size, > + vm_mem_backing_src_alias(src_type)->name); > } > > region->unused_phy_pages = sparsebit_alloc(); > -- > 2.19.1 >