Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp993940rwi; Thu, 27 Oct 2022 09:51:31 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5OQl2Ikx8aeclk8tJGk0k4T3iyAw3IQIfMG8ozxr8PBtSQRFnScLnUXTHspCncuiyRDAEE X-Received: by 2002:aa7:c792:0:b0:453:98b7:213c with SMTP id n18-20020aa7c792000000b0045398b7213cmr45891399eds.159.1666889491216; Thu, 27 Oct 2022 09:51:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666889491; cv=none; d=google.com; s=arc-20160816; b=kY2SyAsx+6V2wwgx8YMRISPwc9w5DNr8T/x5lc+lenp2E+GYQMWJUYA4eBni3koUpD Bu7vSmXfwvEqroloX4kz0XaVoBT5D/xjrX0VrWC6Qlu+mlTF8FkGNjrTMWp28VppNGVu bmq5nCsHH1Om5LqSCFLay1Q550U8mJEjnYwb2Jpj28VVrURaUITluJREUYQ9jr+ZpRf9 y8kJ3+fr5Llk9LjsSpCQGrqqCbkRPOzQj47ok0U4BUwUsXqQLzKP1t7BrAwNEtAY2yxO YUgCe1BPeYYXQc9fd0iNIXDzXvlx2CTjcEx/s6QdftyJlF6qrEmOeoTerbg7DeM2NayA kgTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=+tj9p+sLn7YeP5Xd0DfI8j+ikuZzVppjGSJZ7yYzzQ0=; b=l8Ug4iSlgC8u84isCtIznPSpfmFyc4ukX5yrQjHx+WheLPcvtuvGQwg2e0YuE3wMH3 GffNpoZSFdD/Qb+M7kxFpSm4CHup68aBqcRpuc7eBLgpgcROUVhTJteQk6I4XQ+Dswxx uTBiF5By0PmKmzvw7L30mHC9M7w13hy2L5VUUqXn98nbJRxBTr77kSCZPyuPiNsTNTJQ YpwNJ2jtuG2MvWQmnfLtZLmYa0sFAeob8MADBIaJi+rxJ2+sFVAkhCXLL+oyW4UfjFDP xTVK1sRagbsBaRJFpz/h7phFfbUy5KDP00bAG3qEGy1yKN2YEJlxRrvdqW/RJDXtQgAg WRZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=FaLH9Tp5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e13-20020a056402190d00b00443e3fe60a0si1837675edz.622.2022.10.27.09.51.05; Thu, 27 Oct 2022 09:51:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=FaLH9Tp5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S235768AbiJ0P1l (ORCPT + 99 others); Thu, 27 Oct 2022 11:27:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235870AbiJ0P1a (ORCPT ); Thu, 27 Oct 2022 11:27:30 -0400 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA06117652D for ; Thu, 27 Oct 2022 08:27:25 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id 4so1891278pli.0 for ; Thu, 27 Oct 2022 08:27:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=+tj9p+sLn7YeP5Xd0DfI8j+ikuZzVppjGSJZ7yYzzQ0=; b=FaLH9Tp5QxyOKTbifXkw7jSCzqnLcRtAmkpF5ytRaAoOjzoVVjTW0Qj4Lwf1h+9izN GA8K5moe7cs2f3fJRy6Jy9vfquzkNVgrjj3Ss8937MHcM064xjl9bB3U2djzYteM12WN GP/vz51Cd3EU0brsbHXRMqdFnJyzBRJICz5l7A9ZFFmxP31E/jmT42Om8eeShZPhM6Ja 5Im4FTuZGI+IzbRaM4yfcm8r8qOo+0S/8FAbDdJ1RO2J7FJ4E5SbvHnuncFTlr6Iz/HH S5yirdCBxzXVSt2hMw1CHbJ7R1Mf6NCh3+Yt+Kz1d1itycg5XoQwK5fC+ejNAxkqvE+t MRfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=+tj9p+sLn7YeP5Xd0DfI8j+ikuZzVppjGSJZ7yYzzQ0=; b=SEKIGtFwfhm8ko3wnTZgdTrS9ghM1MzFnlYTno93kzErivfzOSyt5/fbv1uvAQwuAh 49wvC5WxOrLV8NmzRLiRrGixkGXl4Wdi2GnLbpnIf7/csVuyzI2oGEwwdRcThjY2I8df 1fYjo+1kVOe8uQd5BYBzptUs5DQKBJ8jNGfr3lkPJ3YRTG3kWxnZs3ZwhcLlCwtAZeyT MG4jGMZc+uvzXYrvj98calEKQrzqmdutmCw+7zFXUHd/e5dMYR1feMe6JQPU+K0JFvKU KezSjRFN0gx+dKBQolcnVmY5Nj6bMIWsdq2y4buE0qcwLRzKq5tdFuQ8f7FGQPhSlSg9 ySww== X-Gm-Message-State: ACrzQf3E1hXwCrIJ6XDuChS2lUI5vF+y+aC+rM6/TMnuiKJtmhnWgS7y P+tofmTPsQ5VibkWVpWKOrDPow== X-Received: by 2002:a17:903:1c9:b0:186:91fa:59ad with SMTP id e9-20020a17090301c900b0018691fa59admr27726524plh.35.1666884445081; Thu, 27 Oct 2022 08:27:25 -0700 (PDT) Received: from google.com (7.104.168.34.bc.googleusercontent.com. [34.168.104.7]) by smtp.gmail.com with ESMTPSA id d20-20020a621d14000000b0056c47a5c34dsm1293083pfd.122.2022.10.27.08.27.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Oct 2022 08:27:24 -0700 (PDT) Date: Thu, 27 Oct 2022 15:27:21 +0000 From: Sean Christopherson To: "Wang, Wei W" Cc: "pbonzini@redhat.com" , "dmatlack@google.com" , "vipinsh@google.com" , "ajones@ventanamicro.com" , "eric.auger@redhat.com" , "kvm@vger.kernel.org" , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH v1 01/18] KVM: selftests/kvm_util: use array of pointers to maintain vcpus in kvm_vm Message-ID: References: <20221024113445.1022147-1-wei.w.wang@intel.com> <20221024113445.1022147-2-wei.w.wang@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 27, 2022, Wang, Wei W wrote: > On Thursday, October 27, 2022 7:48 AM, Sean Christopherson wrote: > > > + for (i = 0, vcpu = vm->vcpus[0]; \ > > > + vcpu && i < KVM_MAX_VCPUS; vcpu = vm->vcpus[++i]) > > > > I hate pointer arithmetic more than most people, but in this case it avoids the > > need to pass in 'i', which in turn cuts down on boilerplate and churn. > > Hmm, indeed, this can be improved, how about this one: > > +#define vm_iterate_over_vcpus(vm, vcpu) \ > + for (vcpu = vm->vcpus[0]; vcpu; vcpu = vm->vcpus[vcpu->id + 1]) \ Needs to be bounded by the size of the array. > > > #endif /* SELFTEST_KVM_UTIL_H */ > > > diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h > > > b/tools/testing/selftests/kvm/include/kvm_util_base.h > > > index e42a09cd24a0..c90a9609b853 100644 > > > --- a/tools/testing/selftests/kvm/include/kvm_util_base.h > > > +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h > > > @@ -45,7 +45,6 @@ struct userspace_mem_region { }; > > > > > > struct kvm_vcpu { > > > - struct list_head list; > > > uint32_t id; > > > int fd; > > > struct kvm_vm *vm; > > > @@ -75,7 +74,6 @@ struct kvm_vm { > > > unsigned int pa_bits; > > > unsigned int va_bits; > > > uint64_t max_gfn; > > > - struct list_head vcpus; > > > struct userspace_mem_regions regions; > > > struct sparsebit *vpages_valid; > > > struct sparsebit *vpages_mapped; > > > @@ -92,6 +90,7 @@ struct kvm_vm { > > > int stats_fd; > > > struct kvm_stats_header stats_header; > > > struct kvm_stats_desc *stats_desc; > > > + struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; > > > > We can dynamically allocate the array without too much trouble, though I'm not > > sure it's worth shaving the few KiB of memory. For __vm_create(), the number > > of vCPUs is known when the VM is created. For vm_create_barebones(), we > > could do the simple thing of allocating KVM_MAX_VCPU. > > The issue with dynamic allocation is that some users start with > __vm_create(nr_vcpus), and later could add more cpus with vm_vcpu_add (e.g. > x86_64/xapic_ipi_test.c). To support this we may need to re-allocate the > array for later vm_vcpu_add(), and also need to add nr_vcpus to indicate the > size. Hrm, right, the number of runnable CPUs isn't a hard upper bound. Ideally it would be, as the number of pages required for guest memory will fail to account for the "extra" vcpus. E.g. that test should really do vm_create(2) and then manually add each vCPU. > It's userspace memory, and not a problem to use a bit larger virtual memory > (memory are not really allocated until we have that many vcpus to touch the > array entries), I think. Yeah, just allocate the max for now, though the array still needs to be dynamically allocated based on the actual maximum number of vCPUs. Oh, duh, we can do the easy thing and just bump KVM_MAX_VCPUS to 1024 to match KVM. And then assert that kvm_check_cap(KVM_CAP_MAX_VCPUS) == KVM_CAP_MAX_VCPUS in kvm_create_max_vcpus.c.