From patchwork Mon Oct 24 11:34:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8378 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp394026wru; Mon, 24 Oct 2022 04:39:09 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5tmbkwbOV38lB2gyucbfHibWYW/dY4d3muTmYNX9pmf8jBK1Aq9Or+j9YIRfs7FEUhBLHx X-Received: by 2002:a17:906:6a09:b0:78d:ed9c:d880 with SMTP id qw9-20020a1709066a0900b0078ded9cd880mr26004608ejc.424.1666611549014; Mon, 24 Oct 2022 04:39:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611549; cv=none; d=google.com; s=arc-20160816; b=xxWXy4HFnKXx0608CBQYIdOrqb40bCgsEWzy9yOoNgJdQUamGXgwgAzexCs8diWd9e 3RtPIa8xYYsnaJEk4a3+7ZS6PCrl0c09ikrS4BG+i7MERf0c7cRKUqA0UIjwGS4TCd4t ffcIig4xco7+wLD6ez3H7D3/eJ6vlrkZ3qG38ryYAmfrnIBeDZGkLSczV4GOUPkblLOg 6C20oS8lGAge1uRYM53Cfy99x9FDSmo1Z6J4nfeypjIwMb8e8YmQb3fJkxVuZtiyGKoy o/XoOmwHDxcomX4MV9cMx5anVJi/CsghXTKjgS+Q9iWAqpvLyBGq2o9WDZ5r0u5sscm0 CfwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=wgkWB4gyMLfqs6efXEOBgP4Iti9Rim3RdupvbijHeU8=; b=edXK38z3NgY8YGsiwPN6FNa+0kCEp2LU6HgvVvSMPfOY2IcXtBTPIeek/lZ3uLiPT8 48cSqxzFhqQdFkvL7e6fDQ0vp7l3oDhEy//MVqQR/Ag5QAzQxsY+Bt5S12aLtd3vT8VW cImhlFBtZYC5FqzXizEYQRo05HmlYKMmy/FufwYT+tSTLkMFUUnqjfsjLDdVzxXpfh4W KbBPcjx0uGYWj1AOIvoLG3I5QNHIOfjVctyvKYv7tR33A+Fy9nFIxAZlN3HYSAEvyhz5 4bSAI0wMXZ5GHWNYYbghAnyJ2anbcZsRjIqnObqjnhshLNRmumoZPXCgL5nBg4AXNO4w 33Nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GZwwi4fS; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dm12-20020a170907948c00b00791a3dd01b6si16337818ejc.864.2022.10.24.04.38.43; Mon, 24 Oct 2022 04:39:09 -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=@intel.com header.s=Intel header.b=GZwwi4fS; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231167AbiJXLiQ (ORCPT + 99 others); Mon, 24 Oct 2022 07:38:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230294AbiJXLhl (ORCPT ); Mon, 24 Oct 2022 07:37:41 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37E9BB1E9; Mon, 24 Oct 2022 04:37:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611439; x=1698147439; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=t7W7XVUZQguRPZuAEYrnWIs6HnDF1NedL9PgtWfNfnk=; b=GZwwi4fSG7zklKLohEZLz+YWkpgQo1cRPqyW7+jcNrXVDRbCeyYgzAy9 lejm09s6EYNfgJtKsJTehYqj1V2gXeGsbRmpsaJEpDrBFmbzA3CPIj8fa IQUEEi3KwVrTk9duCBx7I8Qm/jvoesABLwZrByhvs5JJi5gZRXTyD0RPV 4rwZw34dYKu4irYtd2IW7NtqQWp8EVbKR+jEx/3QO1as2k1UGvpJoTzDK F387Th2L5Y411JmVGyLVQ+z+CAhnBrIko9l4XV3/875B43JPGOGVMEePV 9XhVOqASlourqEaaAXUjInwh8gy7PQGyLGrWsQL5frFA3vtrjEtHnDkni g==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="371612763" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="371612763" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:34:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773784606" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773784606" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:34:50 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 01/18] KVM: selftests/kvm_util: use array of pointers to maintain vcpus in kvm_vm Date: Mon, 24 Oct 2022 19:34:28 +0800 Message-Id: <20221024113445.1022147-2-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747568871301699948?= X-GMAIL-MSGID: =?utf-8?q?1747568871301699948?= Each vcpu has an id associated with it and is intrinsically faster and easier to be referenced by indexing into an array with "vcpu->id", compared to using a list of vcpus in the current implementation. Change the vcpu list to an array of vcpu pointers. Users then don't need to allocate such a vcpu array on their own, and instead, they can reuse the one maintained in kvm_vm. Signed-off-by: Wei Wang --- .../testing/selftests/kvm/include/kvm_util.h | 4 +++ .../selftests/kvm/include/kvm_util_base.h | 3 +- tools/testing/selftests/kvm/lib/kvm_util.c | 34 ++++++------------- tools/testing/selftests/kvm/lib/x86_64/vmx.c | 2 +- 4 files changed, 17 insertions(+), 26 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index c9286811a4cb..5d5c8968fb06 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -10,4 +10,8 @@ #include "kvm_util_base.h" #include "ucall_common.h" +#define vm_iterate_over_vcpus(vm, vcpu, i) \ + for (i = 0, vcpu = vm->vcpus[0]; \ + vcpu && i < KVM_MAX_VCPUS; vcpu = vm->vcpus[++i]) + #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]; }; diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index f1cb1627161f..941f6c3ea9dc 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -195,7 +195,6 @@ struct kvm_vm *____vm_create(enum vm_guest_mode mode, uint64_t nr_pages) vm = calloc(1, sizeof(*vm)); TEST_ASSERT(vm != NULL, "Insufficient Memory"); - INIT_LIST_HEAD(&vm->vcpus); vm->regions.gpa_tree = RB_ROOT; vm->regions.hva_tree = RB_ROOT; hash_init(vm->regions.slot_hash); @@ -534,6 +533,10 @@ __weak void vcpu_arch_free(struct kvm_vcpu *vcpu) static void vm_vcpu_rm(struct kvm_vm *vm, struct kvm_vcpu *vcpu) { int ret; + uint32_t vcpu_id = vcpu->id; + + TEST_ASSERT(!!vm->vcpus[vcpu_id], "vCPU%d wasn't added\n", vcpu_id); + vm->vcpus[vcpu_id] = NULL; if (vcpu->dirty_gfns) { ret = munmap(vcpu->dirty_gfns, vm->dirty_ring_size); @@ -547,18 +550,16 @@ static void vm_vcpu_rm(struct kvm_vm *vm, struct kvm_vcpu *vcpu) ret = close(vcpu->fd); TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret)); - list_del(&vcpu->list); - vcpu_arch_free(vcpu); free(vcpu); } void kvm_vm_release(struct kvm_vm *vmp) { - struct kvm_vcpu *vcpu, *tmp; - int ret; + struct kvm_vcpu *vcpu; + int i, ret; - list_for_each_entry_safe(vcpu, tmp, &vmp->vcpus, list) + vm_iterate_over_vcpus(vmp, vcpu, i) vm_vcpu_rm(vmp, vcpu); ret = close(vmp->fd); @@ -1085,18 +1086,6 @@ static int vcpu_mmap_sz(void) return ret; } -static bool vcpu_exists(struct kvm_vm *vm, uint32_t vcpu_id) -{ - struct kvm_vcpu *vcpu; - - list_for_each_entry(vcpu, &vm->vcpus, list) { - if (vcpu->id == vcpu_id) - return true; - } - - return false; -} - /* * Adds a virtual CPU to the VM specified by vm with the ID given by vcpu_id. * No additional vCPU setup is done. Returns the vCPU. @@ -1106,7 +1095,7 @@ struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) struct kvm_vcpu *vcpu; /* Confirm a vcpu with the specified id doesn't already exist. */ - TEST_ASSERT(!vcpu_exists(vm, vcpu_id), "vCPU%d already exists\n", vcpu_id); + TEST_ASSERT(!vm->vcpus[vcpu_id], "vCPU%d already exists\n", vcpu_id); /* Allocate and initialize new vcpu structure. */ vcpu = calloc(1, sizeof(*vcpu)); @@ -1125,8 +1114,7 @@ struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) TEST_ASSERT(vcpu->run != MAP_FAILED, __KVM_SYSCALL_ERROR("mmap()", (int)(unsigned long)MAP_FAILED)); - /* Add to linked-list of VCPUs. */ - list_add(&vcpu->list, &vm->vcpus); + vm->vcpus[vcpu_id] = vcpu; return vcpu; } @@ -1684,7 +1672,7 @@ void kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing) */ void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent) { - int ctr; + int i, ctr; struct userspace_mem_region *region; struct kvm_vcpu *vcpu; @@ -1712,7 +1700,7 @@ void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent) } fprintf(stream, "%*sVCPUs:\n", indent, ""); - list_for_each_entry(vcpu, &vm->vcpus, list) + vm_iterate_over_vcpus(vm, vcpu, i) vcpu_dump(stream, vcpu, indent + 2); } diff --git a/tools/testing/selftests/kvm/lib/x86_64/vmx.c b/tools/testing/selftests/kvm/lib/x86_64/vmx.c index d21049c38fc5..77812dd03647 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/vmx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/vmx.c @@ -549,7 +549,7 @@ bool kvm_vm_has_ept(struct kvm_vm *vm) struct kvm_vcpu *vcpu; uint64_t ctrl; - vcpu = list_first_entry(&vm->vcpus, struct kvm_vcpu, list); + vcpu = vm->vcpus[0]; TEST_ASSERT(vcpu, "Cannot determine EPT support without vCPUs.\n"); ctrl = vcpu_get_msr(vcpu, MSR_IA32_VMX_TRUE_PROCBASED_CTLS) >> 32; From patchwork Mon Oct 24 11:34:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8375 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp393941wru; Mon, 24 Oct 2022 04:38:55 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7ZTUBWy3vgI5A4vKkemWtaW2+1MJ3drHTyWkAOV3TOJVxAW4WnoXm1UZ28AYHnFryW4c7R X-Received: by 2002:a17:907:1623:b0:78d:9e8b:171d with SMTP id hb35-20020a170907162300b0078d9e8b171dmr27014509ejc.309.1666611525215; Mon, 24 Oct 2022 04:38:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611525; cv=none; d=google.com; s=arc-20160816; b=Zb+fpBKpAOy/DGwX9aGUljeoVVzGxya4E00JE6WHeFEa4Wh54IivISOCmRJgkz7k15 J7aHNqBdoBX8/j1XOmSKEWAEw4tKb6fkBvbeZTMwXu9QtQ37NCs8XyjmPSXRaexBu50I zvefHKDLcqZ7cf/Q5/tkbwZMhPhL8ynkcoYfJyQdZQOF4OQIWsU3hsRN76lPb3hk6Cyw xIol8KPro8BjzJFVGExDd+ZlM9CymX5oNHJ3R2UcClgi6so3No0Aj7qYrcfoZQm5dQ5W ptY9d5uAfocycIhWrn/+vbV0w+sHPZNqYOB8LZhNu3QKu47gw78JUxR0SLDtOEMOGzeT AWog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=5YaPRYeI2AsPheBSofsV50OmpxolEAvIbe4rB1cNFjM=; b=pcp0iOSvC9bSURs4lWFeqL65fPobdxT9exeIZu4M/sixCQUz/GjN+DP+Pw7savVm48 RT3N5uIb13PnaR2Age1/62giwszd7YGwxoSU4NbY6ofg1kkcBZ6hW+WeeGQ5XsT/DbAm wog8gmjG2kyEBqPZqOGnkYkL/zQr07+yh7fbY4gKskUHlYzj9kbZTgPA/8HZF36QdlSX /sbJR6/uZ/5+Fwvnyy9BeQoWGUhdT558mWIUq8KY7wGitHTW8OAwfAbUfjWaGyKMptIp 16M1eE+clyhyx4kxk026Z7DFypKv5UaNsQKA1C92p86TY5A+t717rj3KdPuQO5RM+RRU zmrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fgPSGzF7; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nc4-20020a1709071c0400b0078d810e16a4si29629734ejc.486.2022.10.24.04.38.21; Mon, 24 Oct 2022 04:38:45 -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=@intel.com header.s=Intel header.b=fgPSGzF7; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230342AbiJXLh7 (ORCPT + 99 others); Mon, 24 Oct 2022 07:37:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230047AbiJXLhc (ORCPT ); Mon, 24 Oct 2022 07:37:32 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AECFC2338C; Mon, 24 Oct 2022 04:37:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611441; x=1698147441; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6ZWYt19HkbGichJe8gxDpcMzSf3niaLPKuq8RXWtguo=; b=fgPSGzF7FkxLRh2FmhKuElG9S69oXengpHvWxF3vD7nzDVRsqeWrapN7 U3eVAIBIZtQyR7gyLm0Ca03brLBNE7GapEjHB/+QS7fOG+FgZJxc6uII1 MwTaVrDBAFcfCRRAOMS69VDXVWx27B8uRkLyhs+9NxfdhkKAbRZb4gK6G tvs66PPRO9idiR0qAHvtNvjdaNJt8sEAEy3lJulF/Xi/WPcX/DEig9tR7 OMHHrHZtU77/0cAx+FIGv2QzSypC1hKHQUCbGYZAwRh/xCNl0P5LI2wWi fQ1Wcv7WNr7PdmPU8lpkRgMLz0WRF1pgyPViMlgWLjpsfoSYkI5ZjM7Fc Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="371612770" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="371612770" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:34:55 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773784625" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773784625" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:34:53 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 02/18] KVM: selftests/kvm_util: use vm->vcpus[] when create vm with vcpus Date: Mon, 24 Oct 2022 19:34:29 +0800 Message-Id: <20221024113445.1022147-3-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747568846840376576?= X-GMAIL-MSGID: =?utf-8?q?1747568846840376576?= The kvm_vm struct has been changed to maintain vcpus via an array of vcpu pointers, there is no need to require users to allocate and pass in another vcpu array when create the vm. Signed-off-by: Wei Wang --- tools/testing/selftests/kvm/lib/kvm_util.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 941f6c3ea9dc..1f69f5ca8356 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -371,12 +371,10 @@ struct kvm_vm *__vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus struct kvm_vm *vm; int i; - TEST_ASSERT(!nr_vcpus || vcpus, "Must provide vCPU array"); - vm = __vm_create(mode, nr_vcpus, extra_mem_pages); for (i = 0; i < nr_vcpus; ++i) - vcpus[i] = vm_vcpu_add(vm, i, guest_code); + vm->vcpus[i] = vm_vcpu_add(vm, i, guest_code); return vm; } @@ -385,13 +383,12 @@ struct kvm_vm *__vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, uint64_t extra_mem_pages, void *guest_code) { - struct kvm_vcpu *vcpus[1]; struct kvm_vm *vm; vm = __vm_create_with_vcpus(VM_MODE_DEFAULT, 1, extra_mem_pages, - guest_code, vcpus); + guest_code, NULL); - *vcpu = vcpus[0]; + *vcpu = vm->vcpus[0]; return vm; } From patchwork Mon Oct 24 11:34:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8374 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp393902wru; Mon, 24 Oct 2022 04:38:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM59aVQ+Vo6AL0x0ZYkY9x/yaFT8CAfmFC4XsXtf13HBzBkZFnAp4NzgAr2pIQJB3EDhAQps X-Received: by 2002:a17:907:d0f:b0:78d:a335:8368 with SMTP id gn15-20020a1709070d0f00b0078da3358368mr27774301ejc.260.1666611531639; Mon, 24 Oct 2022 04:38:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611531; cv=none; d=google.com; s=arc-20160816; b=JnDNEbDLpweN8zqoNaFRL9RuQKOmZdXl08r71SYbyhBhO+OrPYMwhlpp9CfLqKE29Y QdBAYvwXhaKGcBlGZc6xcg4OJW92NyJCumtpC6gRmibLms+aUZARg/DPs6UGWR1jL4di j4cRfBdYB8nfeVzLJbFDV4XFjpeaK2VqBD1g3utp9lu4x3SkYt8x2o9XwLWtLUSTUNot cndKifLELHdDVmYd/PE9u/NaMG2ugeJIEcRosU7JWUyrRJaJNcb0Lm/8/nMt7PFFoAvc xPLRUQnyXdFw8pLPBmKtJYwReI12gAtcKztRtpA9esg+VjFiOxg0kwBMh3W4TBc09pOg 2BaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=w4I2iJTWRrKz8ZZSo2OcJ7Jtcz//TJIcLCjALG3VUCA=; b=NWkUsV+iSL/mHV90/ZoP118jFxqy/rwm5Zlh/my20eJfnDqZLURY8xg/WFzyvTchIG rZIDJGpJ6SCAvuE5birAFcw9q9uQOk9evXD3qWQmfQarf4sGyTb2Rb8/+TaU0zW2yA8R /XQY1HzrUt7JW1XIXzpIiOZwqkXYX1TC4cQ/HrlKjST7QYWVUWaKA7k7eO8WV0Y46pTZ dYgz6C3lUmKgI3u0LSOkldirAXkoLabJT31IW7lOPzDtaw9hG5zV/Bk36A0hl3/T5Syo PjmbN23laiL0MsQj/HIiSDxDXPE6CwOEjUAzpVYy+pNwDwyKGygEPaU92otHsXGJAjxz A5bA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WQybs17x; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y18-20020a50e612000000b0045caa1628c6si25415696edm.216.2022.10.24.04.38.27; Mon, 24 Oct 2022 04:38:51 -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=@intel.com header.s=Intel header.b=WQybs17x; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231146AbiJXLiD (ORCPT + 99 others); Mon, 24 Oct 2022 07:38:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230041AbiJXLhi (ORCPT ); Mon, 24 Oct 2022 07:37:38 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1D051C418; Mon, 24 Oct 2022 04:37:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611444; x=1698147444; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=KALREN8qvMPmTQ0EIZa6eHxaQ3wxIRqxncgPOySP9uo=; b=WQybs17xyJ7CJV8sX8JkR9kLnR5JnkC3mINNRtiXR+AoBbPZkW5gWGdJ lmV0lyHwc2pyLOGLYOGgHe2P5F+6egLEXNcP1zuxUiGiphOnStyKDJKUr DJgEY5mP+nnG9Xlbs2BdXefthTtAdsP+3N+4cgbRKVyH+3m4LaO8K9xT4 Pxt27XUAWXCtXUw9lDxS7ATZiwAZGEg8Phax2RoIKFxhjEmUHsacLsMGH ChEma2wuWJRtHDY1VF374ju79H3lwg37v7tJsdXLJqmU29eq/WEE7a5Us dE+UAs+T8ydCE0/MQL4OckfvY2YFItKY8zFs4Ql9dqMu21KWy/IkBk5vA g==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="371612780" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="371612780" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:34:58 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773784645" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773784645" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:34:55 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 03/18] KVM: selftests/kvm_util: helper functions for vcpus and threads Date: Mon, 24 Oct 2022 19:34:30 +0800 Message-Id: <20221024113445.1022147-4-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747568853285454780?= X-GMAIL-MSGID: =?utf-8?q?1747568853285454780?= Add a vcpu thread field to the kvm_vcpu struct, so that each user doesn't need to define an array of such threads on their own. The private_data pointer is added and optionally used to hold user specific data, and type casting to the user's data type will be performed in the user vcpu thread's start_routine. A couple of the helper functions are added to support vcpu related operations: pthread_create_with_name is provided to create general threads with user specified name. vcpu_thread_create is provided to create a vcpu thread with name in "vcpu##id" format, vm_vcpu_threads_create is provided to create vcpu threads for the vcpus that have been created for a vm. The thread naming facilitates debugging, performance tuning, runtime pining etc. An example is shown below reported from "top". With naming the vcpu threads, the per-vcpu info becomes more noticeable: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 4464 root 20 0 4248684 4.0g 1628 R 99.9 26.2 0:50.97 dirty_log_perf_ 4467 root 20 0 4248684 4.0g 1628 R 99.9 26.2 0:50.93 vcpu0 4469 root 20 0 4248684 4.0g 1628 R 99.9 26.2 0:50.93 vcpu2 4470 root 20 0 4248684 4.0g 1628 R 99.9 26.2 0:50.94 vcpu3 4468 root 20 0 4248684 4.0g 1628 R 99.7 26.2 0:50.93 vcpu1 vm_vcpu_threads_join is provided to join all the vcpu threads. vm_vcpu_threads_private_data_alloc is provided to allocate memory used for user specific private data to each vcpu that have been created to the vm. Signed-off-by: Wei Wang --- .../testing/selftests/kvm/include/kvm_util.h | 20 ++ .../selftests/kvm/include/kvm_util_base.h | 2 + tools/testing/selftests/kvm/lib/kvm_util.c | 172 ++++++++++++++++++ 3 files changed, 194 insertions(+) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index 5d5c8968fb06..036ed05e72e6 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -6,6 +6,7 @@ */ #ifndef SELFTEST_KVM_UTIL_H #define SELFTEST_KVM_UTIL_H +#include #include "kvm_util_base.h" #include "ucall_common.h" @@ -14,4 +15,23 @@ for (i = 0, vcpu = vm->vcpus[0]; \ vcpu && i < KVM_MAX_VCPUS; vcpu = vm->vcpus[++i]) +void __pthread_create_with_name(pthread_t *thread, const pthread_attr_t *attr, + void *(*start_routine)(void *), void *arg, char *name); + +void pthread_create_with_name(pthread_t *thread, + void *(*start_routine)(void *), void *arg, char *name); + +void __vcpu_thread_create(struct kvm_vcpu *vcpu, const pthread_attr_t *attr, + void *(*start_routine)(void *), uint32_t private_data_size); + +void vcpu_thread_create(struct kvm_vcpu *vcpu, void *(*start_routine)(void *), + uint32_t private_data_size); + +void vm_vcpu_threads_create(struct kvm_vm *vm, + void *(*start_routine)(void *), uint32_t private_data_size); + +void vm_vcpu_threads_join(struct kvm_vm *vm); + +void vm_vcpu_threads_private_data_alloc(struct kvm_vm *vm, uint32_t data_size); + #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 c90a9609b853..d0d6aaec0098 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -55,6 +55,8 @@ struct kvm_vcpu { struct kvm_dirty_gfn *dirty_gfns; uint32_t fetch_index; uint32_t dirty_gfns_count; + pthread_t thread; + void *private_data; }; struct userspace_mem_regions { diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 1f69f5ca8356..ba3e774087fb 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -2006,3 +2006,175 @@ void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data, break; } } + +/* + * Create a named thread with user's attribute + * + * Input Args: + * attr - the attribute of the thread to create + * start_routine - the routine to run in the thread context + * arg - the argument passed to start_routine + * name - the name of the thread + * + * Output Args: + * thread - the thread to be created + * + * Create a thread with a user specified name. + */ +void __pthread_create_with_name(pthread_t *thread, const pthread_attr_t *attr, + void *(*start_routine)(void *), void *arg, char *name) +{ + int r; + + r = pthread_create(thread, NULL, start_routine, arg); + TEST_ASSERT(!r, "thread(%s) creation failed, r = %d", name, r); + r = pthread_setname_np(*thread, name); + TEST_ASSERT(!r, "thread(%s) setting name failed, r = %d", name, r); +} + +/* + * Create a named thread with the default thread attribute + * + * Input Args: + * start_routine - the routine to run in the thread context + * arg - the argument passed to start_routine + * name - the name of the thread + * + * Output Args: + * thread - the thread to be created + * + * Create a thread with a user specified name and default thread attribute. + */ +void pthread_create_with_name(pthread_t *thread, + void *(*start_routine)(void *), void *arg, char *name) +{ + __pthread_create_with_name(thread, NULL, start_routine, arg, name); +} + +/* + * Create a vcpu thread with user's attribute + * + * Input Args: + * vcpu - the vcpu for which the thread is created + * attr - the attribute of the vcpu thread + * start_routine - the routine to run in the thread context + * private_data_size - the size of the user's per-vcpu private_data + * + * Output Args: + * None + * + * Create a vcpu thread with user provided attribute and the name in + * "vcpu-##id" format. + */ +void __vcpu_thread_create(struct kvm_vcpu *vcpu, const pthread_attr_t *attr, + void *(*start_routine)(void *), uint32_t private_data_size) +{ + char vcpu_name[16]; + + if (private_data_size) { + vcpu->private_data = calloc(1, private_data_size); + TEST_ASSERT(vcpu->private_data, "%s: failed", __func__); + } + + sprintf(vcpu_name, "vcpu-%d", vcpu->id); + __pthread_create_with_name(&vcpu->thread, attr, + start_routine, (void *)vcpu, vcpu_name); +} + +/* + * Create a vcpu thread with the default thread attribute + * + * Input Args: + * vcpu - the vcpu for which the thread is created + * start_routine - the routine to run in the thread context + * private_data_size - the size of the user's per-vcpu private_data + * + * Output Args: + * None + * + * Create a vcpu thread with the default thread attribute and the name in + * "vcpu-##id" format, and allocate memory to be used as the vcpu thread's + * private data if private_data_size isn't 0. + */ +void vcpu_thread_create(struct kvm_vcpu *vcpu, void *(*start_routine)(void *), + uint32_t private_data_size) +{ + __vcpu_thread_create(vcpu, NULL, start_routine, private_data_size); +} + +/* + * Create vcpu threads for all the vcpus that have been created for a VM + * + * Input Args: + * vm - the VM for which the vcpu threads are created + * start_routine - the routine to run in the thread context + * private_data_size - the size of the user's per-vcpu private_data + * + * Output Args: + * None + * + * Create vcpu threads for all the vcpus that have been created for the VM, + * and the thread name in "vcpu-##id" format. Allocate memory to each vcpu + * thread to be used for its private data if private_data_size isn't 0. + */ +void vm_vcpu_threads_create(struct kvm_vm *vm, + void *(*start_routine)(void *), uint32_t private_data_size) +{ + struct kvm_vcpu *vcpu; + uint32_t i; + + vm_iterate_over_vcpus(vm, vcpu, i) + vcpu_thread_create(vcpu, start_routine, private_data_size); + +} + +/* + * Join the VM's vcpu threads + * + * Input Args: + * vm - the VM for which its vcpu threads should join + * + * Output Args: + * None + * + * Iterate over all the vcpus and join the threads. + */ +void vm_vcpu_threads_join(struct kvm_vm *vm) +{ + struct kvm_vcpu *vcpu; + void *one_failure; + unsigned long failures = 0; + int r, i; + + vm_iterate_over_vcpus(vm, vcpu, i) { + r = pthread_join(vcpu->thread, &one_failure); + TEST_ASSERT(r == 0, "failed to join vcpu %d thread", i); + failures += (unsigned long)one_failure; + } + + TEST_ASSERT(!failures, "%s: failed", __func__); +} + +/* + * Allocate memory used for private data of the vm's vcpus + * + * Input Args: + * vm - the VM for which its vcpus will be assigned the allocated memory + * data_size - the size of the memory to allocate + * + * Output Args: + * None + * + * Allocate memory to be used for private data of each vcpu that has been + * created for vm. + */ +void vm_vcpu_threads_private_data_alloc(struct kvm_vm *vm, uint32_t data_size) +{ + struct kvm_vcpu *vcpu; + int i; + + vm_iterate_over_vcpus(vm, vcpu, i) { + vcpu->private_data = calloc(1, data_size); + TEST_ASSERT(vcpu->private_data, "%s: failed", __func__); + } +} From patchwork Mon Oct 24 11:34:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8371 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp393670wru; Mon, 24 Oct 2022 04:38:25 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5/baMdRriMDDL147TShAjrRPM+QQrwxk5HPvZ4ZA3oGAb2fbZ3+lQnedHcryfRntS722Yf X-Received: by 2002:a17:906:2681:b0:783:6a92:4c38 with SMTP id t1-20020a170906268100b007836a924c38mr27551293ejc.75.1666611505425; Mon, 24 Oct 2022 04:38:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611505; cv=none; d=google.com; s=arc-20160816; b=M9McktUF0NdU+PFVO46IySfeiaqkY4g4nqm7KAoNAuAyRTgVtN3J7/Fdj2LWuzMxRc 27BWFfkuoxmCiXyKsubDQyfpvajWUqixKscz7ImtkwtOaRgXXYDq4v4PxSrX6mfOPsCx R6+A0uh+zZGaMHt6ju4toA5mmQidy9qvqjv0LqMfTsNpP2e4u/Cm1m2F+oCNX9MeHzYp siFTrvFC3eTrFDFrfM7vyyiaIDKhDWNwm5P8pJzoA7HyXHsQvZoYBTnlVHnFzzuJIagP wDUu1S7NcNYAMutMrPZbn6ewGlJqx57ZkwsudbwZSH9giY5lGd00RHJOsYYaLy3bTx64 EAAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0U3uBHtfuosz/DxWCYJDMDl1IgKTjIh5upix3ZnaHHI=; b=pr8LY39y3CZ9fcHJvDLKKn1L9Q0xVjf5Z6toAkQ94t4njEkHSpP/Ur9Rwpd3e/GtLL QO3q4vlBMKbxDyfk7DBEkGef9mabSRNNJ35b97i5wOIXsMfjvikGdu0bMFob0V05DIkr SrPG8iCQLmmyiZvijjJS+Pg75yyzKHKtKCW64rpUDa+Rf1zK+fyUFAKDsv7zdoWgxPPY ZTJ6Cul66hViGriIZ8GDYT5CfXkJbNHvBcoFNGDyy6ANpcWZdZfBMp+7TvFyJHb0hthw GdRk11b7kXgB/qx5W64SZtLsIXYbsSC9zESd4BXHwAQR2L//xlDe8nuCgnMEMUAGRi8Q KT2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jnfl6gjJ; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id pj19-20020a170906d79300b00783ddc8b643si25597003ejb.317.2022.10.24.04.38.01; Mon, 24 Oct 2022 04:38:25 -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=@intel.com header.s=Intel header.b=jnfl6gjJ; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229940AbiJXLhi (ORCPT + 99 others); Mon, 24 Oct 2022 07:37:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230428AbiJXLhX (ORCPT ); Mon, 24 Oct 2022 07:37:23 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B08A8659F6; Mon, 24 Oct 2022 04:37:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611421; x=1698147421; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=VPV00uz2lI67kSDFo1lvZX3bcVuhMRyuHgzIy6XvjFA=; b=jnfl6gjJxawfdf9ie4wZzMy7tJs00Bbtilxjbwu6VvnanU8ckXWpuIe8 aaeTdjfHNrcVwZUj4pMpcShPYSnjoUN/Vekbks39smsl89eH7PyuEXERI z5jj+F/TrlJQ6TFatZr8WYJfapaBaX+k+MLNnrDYUMVtv0d47wwAjCuk7 iZpNJcXhpQkQpfFStK4O6E2ym5iS/OcDrzmyFjnasm6N2ccjPlzBt2vGs tJ/iboJEfBqjVLt2wJ/9eoCT+ZfnpLevwFPSy6eVvFugodiNZHjrim/qk Z4jqWrfI6i86hnodTA8Jt4nNWZ8Ur3P9oXgOdYwqIRbeA5zUX/y+eoKp9 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="369462405" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="369462405" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:35:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773784675" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773784675" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:34:58 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 04/18] KVM: selftests/kvm_page_table_test: vcpu related code consolidation Date: Mon, 24 Oct 2022 19:34:31 +0800 Message-Id: <20221024113445.1022147-5-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747568826054440676?= X-GMAIL-MSGID: =?utf-8?q?1747568826054440676?= kvm_vm has changed to use an array of vcpu pointers (i.e. *vcpus[]) and the vcpu thread has been included in kvm_vcpu. Remove the unnecessary array of vcpu poniters and vcpu threads allocation. Use the helper functions to create and join the vcpu threads. Signed-off-by: Wei Wang --- .../testing/selftests/kvm/kvm_page_table_test.c | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/tools/testing/selftests/kvm/kvm_page_table_test.c b/tools/testing/selftests/kvm/kvm_page_table_test.c index f42c6ac6d71d..4c3df48d80fc 100644 --- a/tools/testing/selftests/kvm/kvm_page_table_test.c +++ b/tools/testing/selftests/kvm/kvm_page_table_test.c @@ -14,7 +14,6 @@ #include #include #include -#include #include #include "test_util.h" @@ -55,7 +54,6 @@ struct test_args { uint64_t large_num_pages; uint64_t host_pages_per_lpage; enum vm_mem_backing_src_type src_type; - struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; }; /* @@ -255,7 +253,7 @@ static struct kvm_vm *pre_init_before_test(enum vm_guest_mode mode, void *arg) /* Create a VM with enough guest pages */ guest_num_pages = test_mem_size / guest_page_size; vm = __vm_create_with_vcpus(mode, nr_vcpus, guest_num_pages, - guest_code, test_args.vcpus); + guest_code, NULL); /* Align down GPA of the testing memslot */ if (!p->phys_offset) @@ -343,7 +341,6 @@ static void vcpus_complete_new_stage(enum test_stage stage) static void run_test(enum vm_guest_mode mode, void *arg) { - pthread_t *vcpu_threads; struct kvm_vm *vm; struct timespec start; struct timespec ts_diff; @@ -352,15 +349,10 @@ static void run_test(enum vm_guest_mode mode, void *arg) /* Create VM with vCPUs and make some pre-initialization */ vm = pre_init_before_test(mode, arg); - vcpu_threads = malloc(nr_vcpus * sizeof(*vcpu_threads)); - TEST_ASSERT(vcpu_threads, "Memory allocation failed"); - host_quit = false; *current_stage = KVM_BEFORE_MAPPINGS; - for (i = 0; i < nr_vcpus; i++) - pthread_create(&vcpu_threads[i], NULL, vcpu_worker, - test_args.vcpus[i]); + vm_vcpu_threads_create(vm, vcpu_worker, 0); vcpus_complete_new_stage(*current_stage); pr_info("Started all vCPUs successfully\n"); @@ -407,8 +399,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) TEST_ASSERT(ret == 0, "Error in sem_post"); } - for (i = 0; i < nr_vcpus; i++) - pthread_join(vcpu_threads[i], NULL); + vm_vcpu_threads_join(vm); ret = sem_destroy(&test_stage_updated); TEST_ASSERT(ret == 0, "Error in sem_destroy"); @@ -416,7 +407,6 @@ static void run_test(enum vm_guest_mode mode, void *arg) ret = sem_destroy(&test_stage_completed); TEST_ASSERT(ret == 0, "Error in sem_destroy"); - free(vcpu_threads); ucall_uninit(vm); kvm_vm_free(vm); } From patchwork Mon Oct 24 11:34:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8373 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp393888wru; Mon, 24 Oct 2022 04:38:50 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4Q+EbSiUMGUNsZoHySz1z3X1CD2fC7JLeqBX5jvIUb6IvWoo+lGka4McvK8l3+rt6o8zSg X-Received: by 2002:a17:906:ef8c:b0:7a4:a4b4:9fcf with SMTP id ze12-20020a170906ef8c00b007a4a4b49fcfmr6794369ejb.727.1666611530125; Mon, 24 Oct 2022 04:38:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611530; cv=none; d=google.com; s=arc-20160816; b=ZzIua6Pp+tJVopgSRZgdV1wHc9CV2ToywyivpO2P5vCTVbnl3oKp+kzvrhwKrv5c5v XD93UfCz00+LodfNoiX2O6WRUKQFQ1v/69DxlzwdAjEUf+iPut/9wCff0qVGFAShs/2/ 9uE1rdu1ldh9Rtf2EiDBsGSvLaybPZTwz1d1AgqBaS5w6B9ykFF7T+MrNkMrJJj45a1i NS0pPozfmh+c+owUtAFJkCkVKR/LzjRVHpUulEAHzSnYcbwhfrE7fxXoL/Bo2Pypn6DJ kyntIqCk2d8W7it3AV6hbb35Us78uT7qoxgcXVKxfK+2wBSuUzGdH9wb4ItMGi0NQ2HO wX2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XvmXwXzCteGkzpcll2vNsOV+OFAeMW8Fvwt+4ml2css=; b=iUWfiMgaIuUw5+0J4szvGX5aRdYhqw4WS6AKG9u1/XLL81okhHGNW4Rygaq/9YsEne uI6LAUo9KB/t9WhQrDlTr9XipS/zWSdBeIy3fL8XImNNObXfHpQ5YbI3jebLC5x2xszB zlJ2uCLW04m8TdMjc5Nbe2sItHxEmUt3KsR4Mw+tl/VIYzBbiuSTXHMoRHueMafvwZmp NVjW3fFbQSCcrVLALrTcwmdphrS7W4DOkcKob6rtQUuvMCacGL462XVh90TdHimy7WK7 6L3/JE1NBFhH7PIBWoUV0eyh58cBM0NDw6+ftUyc8ZHdAefzti6RhMME1n8dxFXey/vh l1lA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=OzadMmOH; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hv13-20020a17090760cd00b007994dcae65fsi11321263ejc.613.2022.10.24.04.38.26; Mon, 24 Oct 2022 04:38:50 -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=@intel.com header.s=Intel header.b=OzadMmOH; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229876AbiJXLhu (ORCPT + 99 others); Mon, 24 Oct 2022 07:37:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230320AbiJXLh3 (ORCPT ); Mon, 24 Oct 2022 07:37:29 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD8F150FBA; Mon, 24 Oct 2022 04:37:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611437; x=1698147437; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RgJBG8ARI2ObMkQFmZbYJeEN4Gn6KZAqtFE7Cxj6InY=; b=OzadMmOHTJ+Je38W4wbsGDzpUHjgZvlHzWe7kB/olSxgu/gnwwoDwoSf CbiFX3Do3s77dVZpmqLY+8W9EFzs236Kwm/iJRh8hNWzp/Tr/1saujzkO JhbC0H2AVuzRpjRZx5SSZ7/IO8aaPEahGvCj3Yj8g7LBRzEnbtzH2ze5A YG2Jc4l373c30beyypSkIEzITtXmxKdH6BbCKHtXL0c7Wu7NNfl1FVc4D XZGvldwqNdhJ045TW979gaUUk2mNStyakwrgpFG0aUqUGxVfVPaqKYi71 bdK/1KyZmMA+7RxqC7dTaKtTC3lWE96WpDDaEzoMbO2BR9qT0DLNGTBfm g==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="369462409" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="369462409" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:35:03 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773784714" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773784714" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:35:00 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 05/18] KVM: selftests/hardware_disable_test: code consolidation and cleanup Date: Mon, 24 Oct 2022 19:34:32 +0800 Message-Id: <20221024113445.1022147-6-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747568851560199209?= X-GMAIL-MSGID: =?utf-8?q?1747568851560199209?= Remove the unnecessary definition of the threads[] array, and use the helper functions to create and join threads. Also move setting of the thread affinity to __vcpu_thread_create using attribute. This avoids an explicit step to set it after thread creation. Signed-off-by: Wei Wang --- .../selftests/kvm/hardware_disable_test.c | 56 +++++-------------- 1 file changed, 15 insertions(+), 41 deletions(-) diff --git a/tools/testing/selftests/kvm/hardware_disable_test.c b/tools/testing/selftests/kvm/hardware_disable_test.c index f5d59b9934f1..c212d34a6714 100644 --- a/tools/testing/selftests/kvm/hardware_disable_test.c +++ b/tools/testing/selftests/kvm/hardware_disable_test.c @@ -8,7 +8,6 @@ #define _GNU_SOURCE #include -#include #include #include #include @@ -59,64 +58,39 @@ static void *sleeping_thread(void *arg) pthread_exit(NULL); } -static inline void check_create_thread(pthread_t *thread, pthread_attr_t *attr, - void *(*f)(void *), void *arg) -{ - int r; - - r = pthread_create(thread, attr, f, arg); - TEST_ASSERT(r == 0, "%s: failed to create thread", __func__); -} - -static inline void check_set_affinity(pthread_t thread, cpu_set_t *cpu_set) -{ - int r; - - r = pthread_setaffinity_np(thread, sizeof(cpu_set_t), cpu_set); - TEST_ASSERT(r == 0, "%s: failed set affinity", __func__); -} - -static inline void check_join(pthread_t thread, void **retval) -{ - int r; - - r = pthread_join(thread, retval); - TEST_ASSERT(r == 0, "%s: failed to join thread", __func__); -} - static void run_test(uint32_t run) { struct kvm_vcpu *vcpu; struct kvm_vm *vm; cpu_set_t cpu_set; - pthread_t threads[VCPU_NUM]; pthread_t throw_away; - void *b; + pthread_attr_t attr; uint32_t i, j; + int r; CPU_ZERO(&cpu_set); for (i = 0; i < VCPU_NUM; i++) CPU_SET(i, &cpu_set); + r = pthread_attr_init(&attr); + TEST_ASSERT(!r, "%s: failed to init thread attr, r = %d", __func__, r); + r = pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpu_set); + TEST_ASSERT(!r, "%s: failed to set affinity, r = %d", __func__, r); - vm = vm_create(VCPU_NUM); + vm = vm_create_with_vcpus(VCPU_NUM, guest_code, NULL); pr_debug("%s: [%d] start vcpus\n", __func__, run); - for (i = 0; i < VCPU_NUM; ++i) { - vcpu = vm_vcpu_add(vm, i, guest_code); + vm_iterate_over_vcpus(vm, vcpu, i) { + __vcpu_thread_create(vcpu, &attr, run_vcpu, 0); - check_create_thread(&threads[i], NULL, run_vcpu, vcpu); - check_set_affinity(threads[i], &cpu_set); - - for (j = 0; j < SLEEPING_THREAD_NUM; ++j) { - check_create_thread(&throw_away, NULL, sleeping_thread, - (void *)NULL); - check_set_affinity(throw_away, &cpu_set); - } + for (j = 0; j < SLEEPING_THREAD_NUM; ++j) + __pthread_create_with_name(&throw_away, &attr, + sleeping_thread, (void *)NULL, + "sleeping-thread"); } pr_debug("%s: [%d] all threads launched\n", __func__, run); sem_post(sem); - for (i = 0; i < VCPU_NUM; ++i) - check_join(threads[i], &b); + + vm_vcpu_threads_join(vm); /* Should not be reached */ TEST_ASSERT(false, "%s: [%d] child escaped the ninja\n", __func__, run); } From patchwork Mon Oct 24 11:34:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8376 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp393982wru; Mon, 24 Oct 2022 04:39:02 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6Ucf+dx0mkfM1R17fEdI5L/9X+1m+/0GTPE7gb2A8fQc01koJLOCL1uLzsaF4Pr+A3JoqT X-Received: by 2002:a17:907:808:b0:730:54cc:b597 with SMTP id wv8-20020a170907080800b0073054ccb597mr26997881ejb.434.1666611542010; Mon, 24 Oct 2022 04:39:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611542; cv=none; d=google.com; s=arc-20160816; b=IM6w7zaAypBXS47fQgUIMjIGDXRZX+4KdjhYy24nRQvSmKs0Ukt2iKeIltYhh3j2dd myV+kPxpVgTv1H5vadvCTwyGsOjOqed1x4grmpPj6ye22+FXp85wMEgw2aq9cX5KDj2z sDygvPZffeyPK044QYRhprjD2f0AgfyIcyGCHUtaC2katbYqszSD7qFPNwojXp03YufK QQBHTZ5toQoBC1m5R6ElvA3EMA7cPxhJRydFPYa+FTX/VamQHFfasO/VuKxpt0nWaVP1 tPOiDR7g+FCRhY6WTvBTjdNq4QK+UvQV2pfx85oA0q73kFOM18WWFWT0pAt1hZJ0bozW u6qA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=P4Ci6AD6pZrJywl7rN+Tr0K3QpSGHFQiNjMn6YaKAtE=; b=vxOJN05muqjikZQ8W9qiEpX+1fycPwrYSp25Vno4ue5TnJx7Mug52gcsjfUzNu3Icx kUWFKrsBXiXDWTiFBrKoEpj/Njwhmuof8WZig0Ah6eH2DPXcoDCBvdGk8Rk8ltU/M86a QK77ApQThpJQ4kpLe09xBOa39UAhbhr7N60SKBKgo6gsPJK0FsCqAyDi0Mf+OYh+Dp9X RyEcBcnmdzK/igfoAKbVTM+SxN77tbQT+yiYmAstl/I7QF6mIQolUOGfEH/iMLZqbc6T td6ikvZHJdNSidzsEuDpQXaRj7jXcMWb1tG5ZK8wEeBvxZXIw2N+6YcI33rOCfpjhRG5 3eAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=kDl178Yi; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gs9-20020a170906f18900b0078dd7383ed8si23418209ejb.414.2022.10.24.04.38.36; Mon, 24 Oct 2022 04:39:01 -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=@intel.com header.s=Intel header.b=kDl178Yi; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231156AbiJXLiI (ORCPT + 99 others); Mon, 24 Oct 2022 07:38:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230419AbiJXLhf (ORCPT ); Mon, 24 Oct 2022 07:37:35 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 174B065240; Mon, 24 Oct 2022 04:37:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611443; x=1698147443; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mKq2NUT7YOX2uBR+Ke1CoZuizyxYRwCCOE+dvTpxS40=; b=kDl178Yi+/m+aNCVfLI5i/erG9pOJsBXIeHSvN0CgLZ5jb1dN7SO+Edw cL/VxnlXR3g/0s6WVDzgN7EJfPENcT8MmGKYtX6amXc+mGw0YHQJ8F24K knSqUKICLr+yue2LbtDStAojmbMkAFzbBpLRdoO34zZZcPU65MlSD8zKP czFI6M0t+IMtMyxkby+9z66mBTgtK9rYI5n0ZAEibRAUZWuSHVBqxiy2K c0WtCL9sFvLATiYBUuKEqSM5DLhF2Spt0ZJkRRRqHsgKIqRih3OIKNSBK i0eXcN6HbJJ9toPHdwhAp74S4SGFvs2r5bw0jRQUdNsW5YiAGhSBx71Gd w==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="369462415" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="369462415" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:35:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773784761" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773784761" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:35:03 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 06/18] KVM: selftests/dirty_log_test: vcpu related code consolidation Date: Mon, 24 Oct 2022 19:34:33 +0800 Message-Id: <20221024113445.1022147-7-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747568863798879247?= X-GMAIL-MSGID: =?utf-8?q?1747568863798879247?= Remove the globally defined vcpu thread, and reuse the one from kvm_vcpu. Also, use the helper functions to create and join the vcpu thread, which has implemented error check (i.e. TEST_ASSERT) inside. Signed-off-by: Wei Wang --- tools/testing/selftests/kvm/dirty_log_test.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c index b5234d6efbe1..9177b8ca004d 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -9,7 +9,6 @@ #include #include -#include #include #include #include @@ -188,12 +187,11 @@ enum log_mode_t { static enum log_mode_t host_log_mode_option = LOG_MODE_ALL; /* Logging mode for current run */ static enum log_mode_t host_log_mode; -static pthread_t vcpu_thread; static uint32_t test_dirty_ring_count = TEST_DIRTY_RING_COUNT; -static void vcpu_kick(void) +static void vcpu_kick(struct kvm_vcpu *vcpu) { - pthread_kill(vcpu_thread, SIG_IPI); + pthread_kill(vcpu->thread, SIG_IPI); } /* @@ -315,10 +313,10 @@ static uint32_t dirty_ring_collect_one(struct kvm_dirty_gfn *dirty_gfns, return count; } -static void dirty_ring_wait_vcpu(void) +static void dirty_ring_wait_vcpu(struct kvm_vcpu *vcpu) { /* This makes sure that hardware PML cache flushed */ - vcpu_kick(); + vcpu_kick(vcpu); sem_wait_until(&sem_vcpu_stop); } @@ -336,7 +334,7 @@ static void dirty_ring_collect_dirty_pages(struct kvm_vcpu *vcpu, int slot, uint32_t count = 0, cleared; bool continued_vcpu = false; - dirty_ring_wait_vcpu(); + dirty_ring_wait_vcpu(vcpu); if (!dirty_ring_vcpu_ring_full) { /* @@ -772,7 +770,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) host_clear_count = 0; host_track_next_count = 0; - pthread_create(&vcpu_thread, NULL, vcpu_worker, vcpu); + vcpu_thread_create(vcpu, vcpu_worker, 0); while (iteration < p->iterations) { /* Give the vcpu thread some time to dirty some pages */ @@ -805,7 +803,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) /* Tell the vcpu thread to quit */ host_quit = true; log_mode_before_vcpu_join(); - pthread_join(vcpu_thread, NULL); + vm_vcpu_threads_join(vm); pr_info("Total bits checked: dirty (%"PRIu64"), clear (%"PRIu64"), " "track_next (%"PRIu64")\n", host_dirty_count, host_clear_count, From patchwork Mon Oct 24 11:34:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8372 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp393764wru; Mon, 24 Oct 2022 04:38:37 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4XNngzwsF5a/wR1f5XTMKbY5VZKlcQpZpnxTpU1WbQxz9faalQ6AyReP2ZhqkUfOHZAZg8 X-Received: by 2002:a05:6402:1941:b0:457:138:1e88 with SMTP id f1-20020a056402194100b0045701381e88mr30650304edz.394.1666611517096; Mon, 24 Oct 2022 04:38:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611517; cv=none; d=google.com; s=arc-20160816; b=T8NhZzIHtwZfhfaHjadnnUyAnMCNSa836MAFPVeJdKYl9R1PFNP6olPdzT3prAmXxV uLIBwHJYq8UQrcHeYMHXqAc2JzV+M+OzI/fCEbxflX07lsbdVnGyTH4O2P8ZBXT/t90S 4hI6lrdsgO2X0+YuQYolz3X5juhCpOdGsQEG/qIgBK2a+97GA3qvndY8jBcLniPgeQe1 hUJsz2EK2mhea//s4YfGikqa8LENokeFPfCV/8nCSo+Ydo1+XS20YODY6ogdNYRDlHNk fBbpFyb8whlbvtuw6oT7fYmnBi6NVivaFRmgdklOzpCvp/nF3foLS6OmiN8ZHZcmPtze 5ULw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=S2BkBVj1L+aZ1TmZ6msCxdny2tGFJ+U34TtoSbCfXaM=; b=tfrF3QHHAq5Yr4s1OK5cDk+EuCpqk+FJxPL+FLqdi/5eGl4J4oMkJlh4947vZG8qyv 9ILHoGGD5sTPVbO3aemQIfvsH8A9WjL+xeJJxFQCPtOt/A+adHAW876gJ9/JOGauUGBU U4rMrmEbLv+gctSJOz/urhZcbdJkHRaTsg6ztUHAc/ncPJ1wu+eC0RLUvNCG/ksVE+9+ MLvZgnYp3FGd+BpxACvyDAJclFYqtpcjS8Y8JUdFiVRzdrcAQ0nfT8QT2LJHT8u89FWN ++OZ6J3P5DwXWNs76+83eRN6f5ZisXvFAYauHMGwGdUc0QzNiK84WTWhNP4hIEBmeG0/ WPpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Zl90wj1b; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bx13-20020a0564020b4d00b0046107f951f9si9465469edb.237.2022.10.24.04.38.13; Mon, 24 Oct 2022 04:38:37 -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=@intel.com header.s=Intel header.b=Zl90wj1b; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230272AbiJXLh4 (ORCPT + 99 others); Mon, 24 Oct 2022 07:37:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230298AbiJXLhb (ORCPT ); Mon, 24 Oct 2022 07:37:31 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 109E352FE8; Mon, 24 Oct 2022 04:37:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611449; x=1698147449; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Da+ApiJuZCWPMMb4BYSHDLZOCQ5nv4rti0CmXFAl03s=; b=Zl90wj1bhcRXKhyN9mFMb2nthcI5KrWbuHMnAw5kYfHqm692UOsExDvI C/Qch7iy3XME5sFuHK23k+PBITr8Adu1tEfDQSg1ACrVC+tufbCHDzyXe +BcFVm2vuu3M5VWsX/3M0N4R6t8X/EqSZ43q3PXfApe/zxiQacd2mu9fS 34WGXFh/MMp/op6EWewn+qfhQJEz5yUU8KcObpgb6OD0DeAFVpr57+vtM b7KKrdrfiW4aGvzBNhlKUOGmi0hho6CvljRGCyD7ePEvN2XCI3JzKk+m2 ++rPEalz/hrHud74Mvomsfx+2ysMcBFu/4t97AaYf9Z1TgUDJ7HYEge8N w==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="369462419" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="369462419" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:35:08 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773784810" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773784810" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:35:05 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 07/18] KVM: selftests/max_guest_memory_test: vcpu related code consolidation Date: Mon, 24 Oct 2022 19:34:34 +0800 Message-Id: <20221024113445.1022147-8-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747568838355345300?= X-GMAIL-MSGID: =?utf-8?q?1747568838355345300?= Remove the unnecessary allocation of the vcpu and threads array, and use the helper functinos to create and join the vcpu threads. As the vcpu thread's start routine (i.e. vcpu_worker) uses kvm_vcpu as the interface, change vcpu_info to be the vcpu thread's private data to have it passed to the thread's start routine. Signed-off-by: Wei Wang --- .../selftests/kvm/max_guest_memory_test.c | 53 +++++++------------ 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/tools/testing/selftests/kvm/max_guest_memory_test.c b/tools/testing/selftests/kvm/max_guest_memory_test.c index 9a6e4f3ad6b5..2d9c83e36e65 100644 --- a/tools/testing/selftests/kvm/max_guest_memory_test.c +++ b/tools/testing/selftests/kvm/max_guest_memory_test.c @@ -3,7 +3,6 @@ #include #include -#include #include #include #include @@ -27,8 +26,7 @@ static void guest_code(uint64_t start_gpa, uint64_t end_gpa, uint64_t stride) GUEST_DONE(); } -struct vcpu_info { - struct kvm_vcpu *vcpu; +struct vcpu_thread_data { uint64_t start_gpa; uint64_t end_gpa; }; @@ -59,13 +57,15 @@ static void run_vcpu(struct kvm_vcpu *vcpu) static void *vcpu_worker(void *data) { - struct vcpu_info *info = data; - struct kvm_vcpu *vcpu = info->vcpu; + struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; + struct vcpu_thread_data *thread_data = + (struct vcpu_thread_data *)vcpu->private_data; struct kvm_vm *vm = vcpu->vm; struct kvm_sregs sregs; struct kvm_regs regs; - vcpu_args_set(vcpu, 3, info->start_gpa, info->end_gpa, vm->page_size); + vcpu_args_set(vcpu, 3, thread_data->start_gpa, + thread_data->end_gpa, vm->page_size); /* Snapshot regs before the first run. */ vcpu_regs_get(vcpu, ®s); @@ -88,31 +88,24 @@ static void *vcpu_worker(void *data) return NULL; } -static pthread_t *spawn_workers(struct kvm_vm *vm, struct kvm_vcpu **vcpus, - uint64_t start_gpa, uint64_t end_gpa) +static void vm_vcpu_threads_data_init(struct kvm_vm *vm, + uint64_t start_gpa, uint64_t end_gpa) { - struct vcpu_info *info; + struct kvm_vcpu *vcpu; + struct vcpu_thread_data *thread_data; uint64_t gpa, nr_bytes; - pthread_t *threads; int i; - threads = malloc(nr_vcpus * sizeof(*threads)); - TEST_ASSERT(threads, "Failed to allocate vCPU threads"); - - info = malloc(nr_vcpus * sizeof(*info)); - TEST_ASSERT(info, "Failed to allocate vCPU gpa ranges"); - nr_bytes = ((end_gpa - start_gpa) / nr_vcpus) & ~((uint64_t)vm->page_size - 1); TEST_ASSERT(nr_bytes, "C'mon, no way you have %d CPUs", nr_vcpus); - for (i = 0, gpa = start_gpa; i < nr_vcpus; i++, gpa += nr_bytes) { - info[i].vcpu = vcpus[i]; - info[i].start_gpa = gpa; - info[i].end_gpa = gpa + nr_bytes; - pthread_create(&threads[i], NULL, vcpu_worker, &info[i]); + vm_iterate_over_vcpus(vm, vcpu, i) { + thread_data = (struct vcpu_thread_data *)vcpu->private_data; + gpa = start_gpa + i * nr_bytes; + thread_data->start_gpa = gpa; + thread_data->end_gpa = gpa + nr_bytes; } - return threads; } static void rendezvous_with_vcpus(struct timespec *time, const char *name) @@ -170,8 +163,6 @@ int main(int argc, char *argv[]) uint64_t max_gpa, gpa, slot_size, max_mem, i; int max_slots, slot, opt, fd; bool hugepages = false; - struct kvm_vcpu **vcpus; - pthread_t *threads; struct kvm_vm *vm; void *mem; @@ -214,10 +205,7 @@ int main(int argc, char *argv[]) } } - vcpus = malloc(nr_vcpus * sizeof(*vcpus)); - TEST_ASSERT(vcpus, "Failed to allocate vCPU array"); - - vm = vm_create_with_vcpus(nr_vcpus, guest_code, vcpus); + vm = vm_create_with_vcpus(nr_vcpus, guest_code, NULL); max_gpa = vm->max_gfn << vm->page_shift; TEST_ASSERT(max_gpa > (4 * slot_size), "MAXPHYADDR <4gb "); @@ -254,10 +242,10 @@ int main(int argc, char *argv[]) } atomic_set(&rendezvous, nr_vcpus + 1); - threads = spawn_workers(vm, vcpus, start_gpa, gpa); - free(vcpus); - vcpus = NULL; + vm_vcpu_threads_create(vm, vcpu_worker, + sizeof(struct vcpu_thread_data)); + vm_vcpu_threads_data_init(vm, start_gpa, gpa); pr_info("Running with %lugb of guest memory and %u vCPUs\n", (gpa - start_gpa) / size_1gb, nr_vcpus); @@ -287,8 +275,7 @@ int main(int argc, char *argv[]) munmap(mem, slot_size / 2); /* Sanity check that the vCPUs actually ran. */ - for (i = 0; i < nr_vcpus; i++) - pthread_join(threads[i], NULL); + vm_vcpu_threads_join(vm); /* * Deliberately exit without deleting the remaining memslots or closing From patchwork Mon Oct 24 11:34:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8379 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp394061wru; Mon, 24 Oct 2022 04:39:13 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6+dczzzOREjYzI5dcjiiOPPn+w5SqTGqdCohOMvAtjgyG6UtwX68ds5aKf/xlN1lcfnnxQ X-Received: by 2002:a17:906:dc93:b0:791:95dd:aaf6 with SMTP id cs19-20020a170906dc9300b0079195ddaaf6mr26557905ejc.456.1666611553198; Mon, 24 Oct 2022 04:39:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611553; cv=none; d=google.com; s=arc-20160816; b=h0ZrcRMFrYPFhzJ9X8zSaNY0dY30jhB86GlATs+GqTYOZ6SNVKoZGAoDoY0A0pKPwp /ropaOKnSVhqwCd1qxr4mYhKfEpexKDoGuJZ2aoZpVL6/PemI7JJJPvCsWOz63nNvf6p buTj9+/ZhfpV62lKfRzvKpOdwMVWIHT93EU6+vCS7THkVioUTESGKmg+IU96hdTVCArw er1km3fBQXQ/423O3W/2+BEjulVjpKXKzDKy1rOglDl54ZMlwTMmsRoSpfhYJfNMAQ77 H67lVwWcu9wHH+eeNKAlnHsdXNsNTvyKp23o13sINWYdW+n+UwBcfhHvQHfdPXo61J+9 ivgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=gPbibfZbomSx7Cn2HL6z8qN+ICCXTEw8tsJaaVP2O30=; b=Cipe40la5pHoNPus8cL4TgthPJwF9K+/2lwG5rIBk2S1Ke8Ul7qBVeuRf1c7UZ3pP8 nzHlIi1elzwmY+gMeNLqabTp3ExzPK0o3gAAFYKaEY9nVyOAygLCDViVlDuBhGQd9BCs kSsgKOl4QCwoagTv5rZ1uoHzQ2x1mJeM9GVyRJf0R0aGWdslUhSMqOhDvtishDlzLKj5 vy/2qHmVIGVBwz0oyFpmsNmZYG0DlfhIqTF6R2HrhxPFEf7kw3vlor/OH1xlbXw5F84m s1obHdk2SsCWFlZNDtAyrFBsWgmC7cgu4I2ePoh4ReNNUyHjChuaqadzZcN8B5OdyDSn 4OUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WrOgQsMI; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c12-20020aa7df0c000000b00459fc3fcf3csi24989683edy.102.2022.10.24.04.38.47; Mon, 24 Oct 2022 04:39:13 -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=@intel.com header.s=Intel header.b=WrOgQsMI; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231174AbiJXLiT (ORCPT + 99 others); Mon, 24 Oct 2022 07:38:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50412 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230397AbiJXLhp (ORCPT ); Mon, 24 Oct 2022 07:37:45 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF09263AC; Mon, 24 Oct 2022 04:37:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611452; x=1698147452; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DB1bYR1A7NeiADV6itewTi7E2/TdqRod02cjfT4etd0=; b=WrOgQsMIS3pkJnbunaNVVDYbr44SrfyC70DZPCvFsz/DBSfgOI/z2qiF TsXaiQnySQfxYtKwU8lNN46MWAKSnCQPCcv45usw8MQr86bm+NWJema+v 2OB6sVu1jnyPYJq2q2Vgk7U1WtWeJlzw2yHk9jdIoFYb2krErdQwwUGk9 7X++9cInskc5/1KL4zwo3FP77Vc94aIhnM1XuFIHXUTO9kcM3iV4HhzYm vLtJyWi4iqgfnq6ihX0YVNz00c7d1Cx/TjkxBXekeJALy6mJjXjxMABvr ENJtXeh7JaXYXvQ6Rzft5x/2iM9SpOyqoZBe0JUVw3+4v+UirH6JWyfog A==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="369462433" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="369462433" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:35:10 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773784858" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773784858" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:35:08 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 08/18] KVM: selftests/set_memory_region_test: vcpu related code consolidation Date: Mon, 24 Oct 2022 19:34:35 +0800 Message-Id: <20221024113445.1022147-9-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747568876090390724?= X-GMAIL-MSGID: =?utf-8?q?1747568876090390724?= Remove the unnecessary vcpu_thread definition and remove it from the related funtions' input as it can be referenced from the kvm_vcpu struct. Also use the helper functinos to create and join the vcpu thread. Signed-off-by: Wei Wang --- .../selftests/kvm/set_memory_region_test.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c index 0d55f508d595..d233668957da 100644 --- a/tools/testing/selftests/kvm/set_memory_region_test.c +++ b/tools/testing/selftests/kvm/set_memory_region_test.c @@ -1,7 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #define _GNU_SOURCE /* for program_invocation_short_name */ #include -#include #include #include #include @@ -108,8 +107,7 @@ static void wait_for_vcpu(void) usleep(100000); } -static struct kvm_vm *spawn_vm(struct kvm_vcpu **vcpu, pthread_t *vcpu_thread, - void *guest_code) +static struct kvm_vm *spawn_vm(struct kvm_vcpu **vcpu, void *guest_code) { struct kvm_vm *vm; uint64_t *hva; @@ -134,7 +132,7 @@ static struct kvm_vm *spawn_vm(struct kvm_vcpu **vcpu, pthread_t *vcpu_thread, hva = addr_gpa2hva(vm, MEM_REGION_GPA); memset(hva, 0, 2 * 4096); - pthread_create(vcpu_thread, NULL, vcpu_worker, *vcpu); + vm_vcpu_threads_create(vm, vcpu_worker, 0); /* Ensure the guest thread is spun up. */ wait_for_vcpu(); @@ -175,12 +173,11 @@ static void guest_code_move_memory_region(void) static void test_move_memory_region(void) { - pthread_t vcpu_thread; struct kvm_vcpu *vcpu; struct kvm_vm *vm; uint64_t *hva; - vm = spawn_vm(&vcpu, &vcpu_thread, guest_code_move_memory_region); + vm = spawn_vm(&vcpu, guest_code_move_memory_region); hva = addr_gpa2hva(vm, MEM_REGION_GPA); @@ -211,7 +208,7 @@ static void test_move_memory_region(void) /* Defered sync from when the memslot was misaligned (above). */ wait_for_vcpu(); - pthread_join(vcpu_thread, NULL); + vm_vcpu_threads_join(vm); kvm_vm_free(vm); } @@ -254,13 +251,12 @@ static void guest_code_delete_memory_region(void) static void test_delete_memory_region(void) { - pthread_t vcpu_thread; struct kvm_vcpu *vcpu; struct kvm_regs regs; struct kvm_run *run; struct kvm_vm *vm; - vm = spawn_vm(&vcpu, &vcpu_thread, guest_code_delete_memory_region); + vm = spawn_vm(&vcpu, guest_code_delete_memory_region); /* Delete the memory region, the guest should not die. */ vm_mem_region_delete(vm, MEM_REGION_SLOT); @@ -282,7 +278,7 @@ static void test_delete_memory_region(void) */ vm_mem_region_delete(vm, 0); - pthread_join(vcpu_thread, NULL); + vm_vcpu_threads_join(vm); run = vcpu->run; From patchwork Mon Oct 24 11:34:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8380 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp394283wru; Mon, 24 Oct 2022 04:39:49 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5zDNyf69tTwXG/oQlyCfx9m51eqLor6SYCtviJaTPYlYqUg+yzqy/B2xDeRoQJbH2BBTRn X-Received: by 2002:a17:906:844c:b0:78d:ce8f:fc81 with SMTP id e12-20020a170906844c00b0078dce8ffc81mr27631725ejy.128.1666611589268; Mon, 24 Oct 2022 04:39:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611589; cv=none; d=google.com; s=arc-20160816; b=hgnz0d1ZtDfdNVg/3Sd47g8/5b1Qo9iYAjr7xzPOf7wYLJSmuuu7ky/h90pW4EYBiJ TsHJRDB9NsIZS46dp8QzNXycRt7A8BQLgYIoVPJ4dax7n55exe9HoJQDk9D7y2E/+p12 REMgsqLIlsNxYZpLdjOJvwDIg/y6k+yeideGWqeBcRPMAkDeq2daMmoH0jqVnphJgIEK w1YXiBSTl51G4NrLo2kdZD3cPgEFO9sWvWC2WZ8zbVPh3kBtKIVrc7HOMxRztb2fKT64 PjNZp8jc+9YMdzkUI2+6Ph/2ouEQVVaXMoLu/Rnc3nZhrWgoukXgXce/deRZya9n+RRf CcqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=PNHfXv9jcJ/v1/JS59IWZiwsYPKz6JJYguDj1S9zsfo=; b=IT3NPquntTXBuLQJxf1sV0uc/ORyi6Qwf89T/KxBmnB8KrLWttDRzcGIf26bswUh1t ip+odM926M1dpQDkGkoKr5BscNEMItkoTJUisKIwsdudWS1dWWps4VVXHKPX5/A0oqwx p9taqEXDsV4MXva1lYWqxVAxkcsc2hFUlCaDVoaLLi+oURRPq+XJ6e3idN5+Z1i142eg bWw/H1ni4MR8JQXVtVGmsLKH8Pu6e0LPrSOcMagXQiP08Vpi81Im7uf1l7fFKJz126eP XfbHtF0gPxHXBhjdd9sWh+4TXCTPlYruQ0meG/cuXoJeLWxEQfsJ/ondZ2KkOovc+pd+ KXeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=g9vWJ4It; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id js4-20020a17090797c400b0072b0f6f1456si31777976ejc.612.2022.10.24.04.39.24; Mon, 24 Oct 2022 04:39:49 -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=@intel.com header.s=Intel header.b=g9vWJ4It; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229649AbiJXLi1 (ORCPT + 99 others); Mon, 24 Oct 2022 07:38:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230475AbiJXLhp (ORCPT ); Mon, 24 Oct 2022 07:37:45 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07BA13ECF9; Mon, 24 Oct 2022 04:37:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611454; x=1698147454; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/8kRGW6jheanoEsx0SN1Z08oqwhwoihZSgoeWklWb5s=; b=g9vWJ4ItRESk1S8Gdme3JEU+CmkNT0pcy72yxr4g58H5nKWoCWvkRh8Z CJZOdz9oCyU8Zum1wtMHuHIihHjVt5Ns09h3DspA7q0xzYTor11kl/6/7 3gIq7Ho44qgQAYe8O7DbsEXyRxIOobv7DEFTtLgaHbeHccTndkMcUz4yp Iyaq0ugxQ2eWQlditiIzDrpQpEATbDNilrtYVOmijvYjvLIQ0+9SluN2K 2Kh2oTSBGdc8Y+48U79IJKL87W4hBhEO0AtLD6Oluv7Jpp+dW71SZMHqT 3BAhePG2vixWNZ+PPppHPn++4WPqlF23/BVbhdq3GbRikteqsBXLl79f5 g==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="369462439" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="369462439" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:35:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773784910" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773784910" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:35:10 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 09/18] KVM: selftests/steal_time: vcpu related code consolidation and cleanup Date: Mon, 24 Oct 2022 19:34:36 +0800 Message-Id: <20221024113445.1022147-10-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747568914146175330?= X-GMAIL-MSGID: =?utf-8?q?1747568914146175330?= Remove the unnecessary definition of array of the vcpu pointers and re-use the one from the kvm_vm struct (i.e. vm->vcpus[]). Use the helper function to create the time stealing thread with name. Also add a check of the pthread_join return value. Signed-off-by: Wei Wang --- tools/testing/selftests/kvm/steal_time.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/kvm/steal_time.c b/tools/testing/selftests/kvm/steal_time.c index db8967f1a17b..857ed2c073fc 100644 --- a/tools/testing/selftests/kvm/steal_time.c +++ b/tools/testing/selftests/kvm/steal_time.c @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -241,7 +240,7 @@ static void run_vcpu(struct kvm_vcpu *vcpu) int main(int ac, char **av) { - struct kvm_vcpu *vcpus[NR_VCPUS]; + struct kvm_vcpu **vcpus; struct kvm_vm *vm; pthread_attr_t attr; pthread_t thread; @@ -250,7 +249,7 @@ int main(int ac, char **av) long stolen_time; long run_delay; bool verbose; - int i; + int i, r; verbose = ac > 1 && (!strncmp(av[1], "-v", 3) || !strncmp(av[1], "--verbose", 10)); @@ -262,7 +261,8 @@ int main(int ac, char **av) pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); /* Create a VM and an identity mapped memslot for the steal time structure */ - vm = vm_create_with_vcpus(NR_VCPUS, guest_code, vcpus); + vm = vm_create_with_vcpus(NR_VCPUS, guest_code, NULL); + vcpus = vm->vcpus; gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, STEAL_TIME_SIZE * NR_VCPUS); vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, gpages, 0); virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, gpages); @@ -290,11 +290,14 @@ int main(int ac, char **av) /* Steal time from the VCPU. The steal time thread has the same CPU affinity as the VCPUs. */ run_delay = get_run_delay(); - pthread_create(&thread, &attr, do_steal_time, NULL); + __pthread_create_with_name(&thread, &attr, + do_steal_time, NULL, "steal-time"); do sched_yield(); while (get_run_delay() - run_delay < MIN_RUN_DELAY_NS); - pthread_join(thread, NULL); + r = pthread_join(thread, NULL); + TEST_ASSERT(!r, "failed to join the time stealing thread"); + run_delay = get_run_delay() - run_delay; TEST_ASSERT(run_delay >= MIN_RUN_DELAY_NS, "Expected run_delay >= %ld, got %ld", From patchwork Mon Oct 24 11:34:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8381 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp394306wru; Mon, 24 Oct 2022 04:39:52 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5fFO25CNUvLM9+lZOmvhUZ0hiDUX5kxHGpVjU9UDDK5UetEyhplaSN16eX+DjzydbLbyBf X-Received: by 2002:a05:6402:1d4a:b0:461:d375:7927 with SMTP id dz10-20020a0564021d4a00b00461d3757927mr3126875edb.18.1666611591985; Mon, 24 Oct 2022 04:39:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611591; cv=none; d=google.com; s=arc-20160816; b=r5n+rCosy1Reb38TPGqO7GDOx6JiaGocZ6+VFmMxy/7CGcjYZjo551sLy4xOuyAJeS j8aBJfXWDSqRK9vth7zjjhNztElUw5avM/hd1w5mspPWul0KTjNRj+WjZ5S76aBbXxt8 /829e/r7a44NEQ0kUWkgS0P/RwGyPLjAWX7u/6cTWIEaaXYzj1eGvWtMEc685I/HTrYe 3zOOIjLBiKRIvGmDPfGewGt8OFx9ymo89ZjWBArlS8P0G845Pt5YuQnixTXTi7BYvGmN Dwwb4CUsrGBN6D/VwJjC8VsBFE9DBTtzm213btlQJh+RMHlRA2v5m2lyJwoT40TD8EtQ Y5Kw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ko7f6d0FTuAy/3ubruVUtPM91P7qP1E08B/BoDaj22Y=; b=lrvL+3FULR5dAFrRLm2bd6gH5xguO94+dERCANMQJ8sYuZNXKggTvs0WVra2SNPfd6 /GpeERarfUjSPuVIiT8t6lBNXPgHsTK4xeIUDf2Fat2DE8ktPG/Oc4usyMB0rHmjPXjx iCvQWZGq3qDwjGqh7g8rbQrS8pKYVeUQYodQFujbhvpgZcbFNmStvQeiW8rXuCBONLPr 4ycAleReQf/ldDLLXpfro4WlPSHsd+oenmiuS6vNmyUhyeK6u2OvjWcR78UvfcNi1ZSg 1NhV7LlN26K6Fb4D9laO5YIC+nV8hwjv1eYsCxHwcKh9TOmxXKFRU4yjHl0DIFTeOR6V OxJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=MZl9zgor; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hb13-20020a170907160d00b0078b0865d468si27586359ejc.549.2022.10.24.04.39.28; Mon, 24 Oct 2022 04:39:51 -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=@intel.com header.s=Intel header.b=MZl9zgor; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231205AbiJXLig (ORCPT + 99 others); Mon, 24 Oct 2022 07:38:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230490AbiJXLhq (ORCPT ); Mon, 24 Oct 2022 07:37:46 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B93020355; Mon, 24 Oct 2022 04:37:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611455; x=1698147455; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=KJBfPBUBTZiaXIiWbe2G6kNdjfIEI/B2n2ehEAeUbhI=; b=MZl9zgorN8+46itPUOm8HIkfAbSErY3MWfjzKCOLdET+bm6L4W6jXIIf O+mj7pa008+zXjct8s0jKFk5Gm3lvHD0nyGWwRwj8/OFgMX5diZs1zGHl mdZzxCusT5bxWSO1xjAztYAx95dkwM83Ti5R//XkyjyatiUHyeayWvPz5 tdOQc0OJ9gK3vXnsIE7dpSYbpVzzu0/IhFZbyWNI+xq65eUSSPqa1iesy N0CczhYKL7HbzdTUI4K05dbQZwKuY1Xn3X4D5YkPkrZkEhpMwNof3s2vh aOeRcH1vVPry/v3l3kqSSKNtgmqqfTLpYSfVXEe5nRyPG50XYjP8wU5Do w==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="369462443" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="369462443" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:35:15 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773784960" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773784960" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:35:12 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 10/18] KVM: selftests/tsc_scaling_sync: vcpu related code consolidation Date: Mon, 24 Oct 2022 19:34:37 +0800 Message-Id: <20221024113445.1022147-11-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747568916607682004?= X-GMAIL-MSGID: =?utf-8?q?1747568916607682004?= Remove the unnecessary definition of the vcpu_threads[] array, as it has beend included in the kvm_vcpu struct. Use the helper functions to create and join the vcpu threads. Signed-off-by: Wei Wang --- .../selftests/kvm/x86_64/tsc_scaling_sync.c | 25 ++++--------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/tsc_scaling_sync.c b/tools/testing/selftests/kvm/x86_64/tsc_scaling_sync.c index 47139aab7408..34a8beef42b6 100644 --- a/tools/testing/selftests/kvm/x86_64/tsc_scaling_sync.c +++ b/tools/testing/selftests/kvm/x86_64/tsc_scaling_sync.c @@ -15,7 +15,6 @@ #include #include #include -#include #define NR_TEST_VCPUS 20 @@ -44,18 +43,15 @@ static void guest_code(void) } -static void *run_vcpu(void *_cpu_nr) +static void *run_vcpu(void *data) { - unsigned long vcpu_id = (unsigned long)_cpu_nr; + struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; unsigned long failures = 0; static bool first_cpu_done; - struct kvm_vcpu *vcpu; /* The kernel is fine, but vm_vcpu_add() needs locking */ pthread_spin_lock(&create_lock); - vcpu = vm_vcpu_add(vm, vcpu_id, guest_code); - if (!first_cpu_done) { first_cpu_done = true; vcpu_set_msr(vcpu, MSR_IA32_TSC, TEST_TSC_OFFSET); @@ -95,23 +91,12 @@ int main(int argc, char *argv[]) { TEST_REQUIRE(kvm_has_cap(KVM_CAP_VM_TSC_CONTROL)); - vm = vm_create(NR_TEST_VCPUS); + vm = vm_create_with_vcpus(NR_TEST_VCPUS, guest_code, NULL); vm_ioctl(vm, KVM_SET_TSC_KHZ, (void *) TEST_TSC_KHZ); pthread_spin_init(&create_lock, PTHREAD_PROCESS_PRIVATE); - pthread_t cpu_threads[NR_TEST_VCPUS]; - unsigned long cpu; - for (cpu = 0; cpu < NR_TEST_VCPUS; cpu++) - pthread_create(&cpu_threads[cpu], NULL, run_vcpu, (void *)cpu); - - unsigned long failures = 0; - for (cpu = 0; cpu < NR_TEST_VCPUS; cpu++) { - void *this_cpu_failures; - pthread_join(cpu_threads[cpu], &this_cpu_failures); - failures += (unsigned long)this_cpu_failures; - } - - TEST_ASSERT(!failures, "TSC sync failed"); + vm_vcpu_threads_create(vm, run_vcpu, 0); + vm_vcpu_threads_join(vm); pthread_spin_destroy(&create_lock); kvm_vm_free(vm); return 0; From patchwork Mon Oct 24 11:34:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8382 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp394326wru; Mon, 24 Oct 2022 04:39:54 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7MSvM9WYbPoyRvYEWzoOVi3kDWQmoc1GvZCpVxnO2O3L7ifemd5etySKHB7z05UJ3YCBfx X-Received: by 2002:aa7:d9d1:0:b0:461:9556:23e6 with SMTP id v17-20020aa7d9d1000000b00461955623e6mr8759469eds.25.1666611594242; Mon, 24 Oct 2022 04:39:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611594; cv=none; d=google.com; s=arc-20160816; b=zMJ3mRqJ0PniN2I5LSASNHYy/Q4W1IVeDtEz7Qo+vytarHYPm4kTG2JVukIITDmXeY hygldVV2kwgK8SCIPkHHz8z/eCQLdoF5faavpl89n2EmrtGoQgpZBOu0I8036A9ts46o udKf2mwVFhv/F7rulm06rDdt8lUYygqc9Qnlh2Y5K9+rEtJU9Jt50i6RUFJKlRXno3Nt ogvipAZaeAwPnSHOfIl/PBpYTwAeuBH2+HDN4jZyrpyzpZfx/mhXYSEVt1RYyZ79rdrK znocs5yjMdXcWNuhU3Unhbj4SZTk7QtV0knis5SrpeC98icIoAnKEAoOHjy/RM7v1Koe NGDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8I4b7T/weYstRqTwkVZmmDu1hXioH06mbThBj7/W1WA=; b=odT0ejyMD7SwSIgggHfKx1IZxig+e4MeeP0LqBmIRG6EqpLOA0RfjcTnHuYypJQKFT xwhRDir9ZFkS2Oqh8cJQA9H5Qx1y6uxEs4OBvYwubNwyC4g16Pi6dq+vuOMnbNnWzQbd KUcSlJIm3lMnRx5qC2e6E6FiVwyUCJln1SpcpzYDPakAi72uwFJRd8SGURjkIzC5SeNU le7xA9oq8a0sgN0ILVgo1F/diam+SGZJWg0oKfjpbvWNrf+axurslOgrBNnlu5E+hO1r w6h6QecGGv3WdkD8xhFi68cIE8iipm/ILoADLHsCv9zeqsPbglgFD1ho7LKvCe+ThgyT 9d9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=aU7daYoW; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 20-20020a170906309400b00780b09a67dcsi23401853ejv.706.2022.10.24.04.39.30; Mon, 24 Oct 2022 04:39:54 -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=@intel.com header.s=Intel header.b=aU7daYoW; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231224AbiJXLim (ORCPT + 99 others); Mon, 24 Oct 2022 07:38:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230527AbiJXLhz (ORCPT ); Mon, 24 Oct 2022 07:37:55 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6C2A67048; Mon, 24 Oct 2022 04:37:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611465; x=1698147465; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=E0BMcyV5C3nvC2wXZ/MmiAw+7vmFX/E/IWaLJ0N1vZg=; b=aU7daYoWeQYawRd1J5WKd+xG4qhd2jXD6p785+ppy4AYuP6NB08qPnVf Wk+4IWAf79hSzMZz7FWjZucm92yWHdcF9KRvLumJX7cZXXPptsfkaj+SM am2HBb9uZoO3b1Gh5478bUvWpX7adYxrn5yGeEN7YY8LCEdJknd7QqHFT fN99x8uGxsSof9TFovmYjjGX5Sx4ATPXnLg+lVwEfq7UztffpPrOGeHZM KL1EvE+2HmB+jocrdhTS7kfV0rwJLnOvy570fnC1SOJrSq6FcvigdPpVr HNy7DaeUaBb+u8UVDG8GW85OfREa/PPuwc6DwOeGx4PVCrNhKJd/L8iBH w==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="369462448" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="369462448" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:35:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773784995" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773784995" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:35:15 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 11/18] KVM: selftest/xapic_ipi_test: vcpu related code consolidation Date: Mon, 24 Oct 2022 19:34:38 +0800 Message-Id: <20221024113445.1022147-12-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747568918683257386?= X-GMAIL-MSGID: =?utf-8?q?1747568918683257386?= Remove the unnecessary definition of the theads[] array, and change thread_params to be the vcpu's private data. Use the helper function to create the threads. Signed-off-by: Wei Wang --- .../selftests/kvm/x86_64/xapic_ipi_test.c | 54 +++++++++---------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/xapic_ipi_test.c b/tools/testing/selftests/kvm/x86_64/xapic_ipi_test.c index 3d272d7f961e..cc2630429067 100644 --- a/tools/testing/selftests/kvm/x86_64/xapic_ipi_test.c +++ b/tools/testing/selftests/kvm/x86_64/xapic_ipi_test.c @@ -22,7 +22,6 @@ #define _GNU_SOURCE /* for program_invocation_short_name */ #include -#include #include #include #include @@ -76,7 +75,6 @@ struct test_data_page { struct thread_params { struct test_data_page *data; - struct kvm_vcpu *vcpu; uint64_t *pipis_rcvd; /* host address of ipis_rcvd global */ }; @@ -193,8 +191,9 @@ static void sender_guest_code(struct test_data_page *data) static void *vcpu_thread(void *arg) { - struct thread_params *params = (struct thread_params *)arg; - struct kvm_vcpu *vcpu = params->vcpu; + struct kvm_vcpu *vcpu = (struct kvm_vcpu *)arg; + struct thread_params *params = + (struct thread_params *)vcpu->private_data; struct ucall uc; int old; int r; @@ -233,17 +232,17 @@ static void *vcpu_thread(void *arg) return NULL; } -static void cancel_join_vcpu_thread(pthread_t thread, struct kvm_vcpu *vcpu) +static void cancel_join_vcpu_thread(struct kvm_vcpu *vcpu) { void *retval; int r; - r = pthread_cancel(thread); + r = pthread_cancel(vcpu->thread); TEST_ASSERT(r == 0, "pthread_cancel on vcpu_id=%d failed with errno=%d", vcpu->id, r); - r = pthread_join(thread, &retval); + r = pthread_join(vcpu->thread, &retval); TEST_ASSERT(r == 0, "pthread_join on vcpu_id=%d failed with errno=%d", vcpu->id, r); @@ -393,17 +392,16 @@ void get_cmdline_args(int argc, char *argv[], int *run_secs, int main(int argc, char *argv[]) { - int r; - int wait_secs; + int i, wait_secs; const int max_halter_wait = 10; int run_secs = 0; int delay_usecs = 0; struct test_data_page *data; vm_vaddr_t test_data_page_vaddr; bool migrate = false; - pthread_t threads[2]; - struct thread_params params[2]; + struct thread_params *params; struct kvm_vm *vm; + struct kvm_vcpu *vcpu; uint64_t *pipis_rcvd; get_cmdline_args(argc, argv, &run_secs, &migrate, &delay_usecs); @@ -412,33 +410,31 @@ int main(int argc, char *argv[]) if (delay_usecs <= 0) delay_usecs = DEFAULT_DELAY_USECS; - vm = vm_create_with_one_vcpu(¶ms[0].vcpu, halter_guest_code); + vm = vm_create_with_one_vcpu(&vcpu, halter_guest_code); vm_init_descriptor_tables(vm); - vcpu_init_descriptor_tables(params[0].vcpu); + vcpu_init_descriptor_tables(vcpu); vm_install_exception_handler(vm, IPI_VECTOR, guest_ipi_handler); virt_pg_map(vm, APIC_DEFAULT_GPA, APIC_DEFAULT_GPA); - params[1].vcpu = vm_vcpu_add(vm, 1, sender_guest_code); + vcpu = vm_vcpu_add(vm, 1, sender_guest_code); + vm_vcpu_threads_private_data_alloc(vm, sizeof(struct thread_params)); test_data_page_vaddr = vm_vaddr_alloc_page(vm); data = addr_gva2hva(vm, test_data_page_vaddr); memset(data, 0, sizeof(*data)); - params[0].data = data; - params[1].data = data; - - vcpu_args_set(params[0].vcpu, 1, test_data_page_vaddr); - vcpu_args_set(params[1].vcpu, 1, test_data_page_vaddr); - pipis_rcvd = (uint64_t *)addr_gva2hva(vm, (uint64_t)&ipis_rcvd); - params[0].pipis_rcvd = pipis_rcvd; - params[1].pipis_rcvd = pipis_rcvd; + + vm_iterate_over_vcpus(vm, vcpu, i) { + params = (struct thread_params *)vcpu->private_data; + params->data = data; + params->pipis_rcvd = pipis_rcvd; + vcpu_args_set(vcpu, 1, test_data_page_vaddr); + } /* Start halter vCPU thread and wait for it to execute first HLT. */ - r = pthread_create(&threads[0], NULL, vcpu_thread, ¶ms[0]); - TEST_ASSERT(r == 0, - "pthread_create halter failed errno=%d", errno); + vcpu_thread_create(vm->vcpus[0], vcpu_thread, 0); fprintf(stderr, "Halter vCPU thread started\n"); wait_secs = 0; @@ -455,9 +451,7 @@ int main(int argc, char *argv[]) "Halter vCPU thread reported its APIC ID: %u after %d seconds.\n", data->halter_apic_id, wait_secs); - r = pthread_create(&threads[1], NULL, vcpu_thread, ¶ms[1]); - TEST_ASSERT(r == 0, "pthread_create sender failed errno=%d", errno); - + vcpu_thread_create(vm->vcpus[1], vcpu_thread, 0); fprintf(stderr, "IPI sender vCPU thread started. Letting vCPUs run for %d seconds.\n", run_secs); @@ -470,8 +464,8 @@ int main(int argc, char *argv[]) /* * Cancel threads and wait for them to stop. */ - cancel_join_vcpu_thread(threads[0], params[0].vcpu); - cancel_join_vcpu_thread(threads[1], params[1].vcpu); + cancel_join_vcpu_thread(vm->vcpus[0]); + cancel_join_vcpu_thread(vm->vcpus[1]); fprintf(stderr, "Test successful after running for %d seconds.\n" From patchwork Mon Oct 24 11:34:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8400 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp397679wru; Mon, 24 Oct 2022 04:42:54 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7qMavpOEX5/2znSHS3TvorCO2t34otYtTh2FMsVQk0jlpfNleAiOBjsK0z2UZNoYaibg7r X-Received: by 2002:a17:907:7250:b0:791:9093:47f7 with SMTP id ds16-20020a170907725000b00791909347f7mr27267502ejc.278.1666611774288; Mon, 24 Oct 2022 04:42:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611774; cv=none; d=google.com; s=arc-20160816; b=LS+iG2/ONVgABQtizDpzkZJrTSaybncDCbq4SvZ29QCc4H5j9hipUJh9sVNu1pDEHy m/TBbKX94OVjL/xDG1byUZQ4n8q++3qY0FOExmfXsVkog8X6antZ/2664c+urrujqw0s WzIGXcUkjaZ79nHQO1nHDiHX6iIaypLPD1PfKIiRmCAUg4cwH9HzRsdN3fe9wicUAghY XwFHOH8+hZM2BpnfoLqm7pAK7Tj093OWsYTg7iZoAPkNQPF7B1z9NdwanpgtVGKYFXWd gCfDgEPBf5+HMxIjaVoIMJM6gGqizdggu46eftbNwey6OBq8xNgHCHo7A9GgyE1Tuxy2 E7uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4Fuc8zD+DEUDsp42TAWa2J+B0bdTco1U04Es5VdkyF0=; b=yJoS/Yfbf4tWfw0mOSsfcHZ4xT2eUog/ha4ag4UzWEN4tPVOm0MnQSefVLrBejdC0S imLjLM4c/z2GsB7TdyBupP/1neZZ93IwALN+E1Nh/LanbvCzDugPXsd4dKu4Rpn4fV+Q CzcNQgKS9T4Gnf/yuCLJkogTErWaxK9I9aj2AWj+TsA5aSuZf6ybx0JqkP/UsStsw93E FItAjg2xPK1Wehv7sSQmGJ0N7plzibHpewny+rnbQ3P5HVzh0OoctrRCWk6aaheLj1/L mpEjj00XRsW7pkp9vEOyBlPA9xlo/gBBQBhnhYqiqlw2Og305fUYigdLepkDtjdSrzlv AzHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=f9kylC3x; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gn41-20020a1709070d2900b0077cfdda438csi28888104ejc.35.2022.10.24.04.42.30; Mon, 24 Oct 2022 04:42:54 -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=@intel.com header.s=Intel header.b=f9kylC3x; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231272AbiJXLlB (ORCPT + 99 others); Mon, 24 Oct 2022 07:41:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231214AbiJXLih (ORCPT ); Mon, 24 Oct 2022 07:38:37 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5F50696FD; Mon, 24 Oct 2022 04:37:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611466; x=1698147466; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tOCK3h3jPTF9+zsBKkGPw4b8sRWy9dxpSKt7joVm7RQ=; b=f9kylC3xIZwJfYktCAHkH6xe+FHSr026mckP+weduDPuf8m/0GfyqAGm sdlP4LGQXBT6SGvWtajTRgkoeog5qeHCJtHGkWY5icGygJ/mJdGQujNC/ 9jNsRAr547JggRqYGDwaUPLoOtfEWMM4IVpkdvhZqn09gSlkzoROPPrRm 59klknAifdEnu3PYomagMYaFTYXKxIQjIRUWkJ8CgLm6ZRISnGuD5Enkm B5LApfNoaEJghPfKIQ/ib4IczjVaq8nqh73iyJYUBtzR1+9poyxTaXiJM nNqOIz2wxdLqEYoXqbFL9QSQDflm92b646UeRS2bFs5lDqNT856JXzZpY g==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="369462454" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="369462454" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:35:19 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773785009" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773785009" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:35:17 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 12/18] KVM: selftests/rseq_test: name the migration thread and some cleanup Date: Mon, 24 Oct 2022 19:34:39 +0800 Message-Id: <20221024113445.1022147-13-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747569107500396987?= X-GMAIL-MSGID: =?utf-8?q?1747569107500396987?= Use the helper function to create the migration thread with name. Change the global defination of migration_thread to local, as it's not referenced anywhere outside main(). Aslo, check the return value from pthread_join and assert on errors. Signed-off-by: Wei Wang --- tools/testing/selftests/kvm/rseq_test.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/rseq_test.c b/tools/testing/selftests/kvm/rseq_test.c index 6f88da7e60be..c124f00ca4fe 100644 --- a/tools/testing/selftests/kvm/rseq_test.c +++ b/tools/testing/selftests/kvm/rseq_test.c @@ -2,7 +2,6 @@ #define _GNU_SOURCE /* for program_invocation_short_name */ #include #include -#include #include #include #include @@ -28,7 +27,6 @@ */ #define NR_TASK_MIGRATIONS 100000 -static pthread_t migration_thread; static cpu_set_t possible_mask; static int min_cpu, max_cpu; static bool done; @@ -204,6 +202,7 @@ int main(int argc, char *argv[]) struct kvm_vm *vm; struct kvm_vcpu *vcpu; u32 cpu, rseq_cpu; + pthread_t migration_thread; /* Tell stdout not to buffer its content */ setbuf(stdout, NULL); @@ -226,8 +225,8 @@ int main(int argc, char *argv[]) vm = vm_create_with_one_vcpu(&vcpu, guest_code); ucall_init(vm, NULL); - pthread_create(&migration_thread, NULL, migration_worker, - (void *)(unsigned long)syscall(SYS_gettid)); + pthread_create_with_name(&migration_thread, migration_worker, + (void *)(unsigned long)syscall(SYS_gettid), "mig-thread"); for (i = 0; !done; i++) { vcpu_run(vcpu); @@ -272,7 +271,8 @@ int main(int argc, char *argv[]) TEST_ASSERT(i > (NR_TASK_MIGRATIONS / 2), "Only performed %d KVM_RUNs, task stalled too much?\n", i); - pthread_join(migration_thread, NULL); + r = pthread_join(migration_thread, NULL); + TEST_ASSERT(r == 0, "failed to join the migration thread"); kvm_vm_free(vm); From patchwork Mon Oct 24 11:34:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8385 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp394379wru; Mon, 24 Oct 2022 04:40:02 -0700 (PDT) X-Google-Smtp-Source: AMsMyM45FiS3KY77Ykl28pAks28mq6VLo1YzgCAukohjP3ZX/a74D+qVK7LWT8ydOeOND8XTmbIP X-Received: by 2002:a17:906:5daa:b0:78d:fa76:f837 with SMTP id n10-20020a1709065daa00b0078dfa76f837mr26363276ejv.239.1666611602178; Mon, 24 Oct 2022 04:40:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611602; cv=none; d=google.com; s=arc-20160816; b=kOuvF4BAnZdJogmjc6NcKSmfMrSMIeiY91XIqsC6NYoZKttpt3892/S/+McJP7DNei kz+8D2Lzpa3/LgMofKYE/x4MY+hsDiQprBe3ztJ/04OEMUQ7GMf7vNVF5qD/dceh2z1a +/VzdAFWpIau+gTe4bu3KSMxavb9hYArL+us0xIbzPratC6Fq4kjYsMVaL7KibigOw4I BIRb0KpgiNeoAtCjFfGgWt5Mal6TYA6WYHC7YtPgRD1GSriY8LJD4ZTzBUceLC7sUNEK 7+7th9qbEmLDkQXHJuODsMc57KprRn6iC+1rGO8Pv+vB1knx1SWSyaehjYcsIac+HLch 8kPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=SYGuplOs0ImrIr2khjfhXgcmLt28BvUSeHY2Qbcqops=; b=UQoSdWpNstJZUsr8p2HJjLUuqe8jZrbfiU/LGqMTxTyO86yHDsYeFaBGbmhfKT2Eo+ KO2b4Y+MFItkDiJ8fia14Bi1ycpkWN+nvekk/xLGYh8HQGcdZfqFVIipmbCf4KRUIXKD HgKyuz8x8Wt82XmIfCCP9HYvhS/UQtJHy7uyvk3F3U+9bX8lCEJQArF4uXqLNyFNv8Kn P8FrKY4MfmVZJcbTsFDLOEa49kN1Gre23BCE+wti/MwgoRt2AKn0LUvIyfSttikhY1uu S0OjeaVBH5oa3J4mi3vpo4+sA3Y2JHL4jIzPEJbvYLpgifkmwuNF/Z7tqbyYFnv9nWP0 GWEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=FksJcDXW; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g7-20020a1709065d0700b007316ac034a1si23233344ejt.831.2022.10.24.04.39.37; Mon, 24 Oct 2022 04:40:02 -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=@intel.com header.s=Intel header.b=FksJcDXW; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231312AbiJXLjB (ORCPT + 99 others); Mon, 24 Oct 2022 07:39:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230442AbiJXLiF (ORCPT ); Mon, 24 Oct 2022 07:38:05 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A2DCF02B; Mon, 24 Oct 2022 04:37:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611467; x=1698147467; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xM7HXxH+B48BG++NWUWJKnJ1CNJFnMO26T4ZGCyIuOg=; b=FksJcDXWvdP6ERPx+dsjiApPoiDY8EAvyj2y5RctRECnaFCFAkB8eAF9 0bSKz8+UxESa5BrGQt9Cwzen8FNRhf0nsUJsZhwMoP0GwTgwWu3B8s4Qa 9lGDfqucjhdKoenpjCCXIElDP3npVv/D+Qi6GElT0yAMNYmJXBErTn8a5 X5SEJWefkuWud9tYJ925vTerZeJSkuNCDvlSQCUccw3WlBp/Z3yGXN/8W GL0OxVnW9Jwhtnl7jiPn41zJ4SPwr7LG/QpnCLQZ8sDfsPgzZit1d0nP3 0NdoawiaL1TDOd7h8EgbgOl1CLCNVXwg9uMUjgmi+rb4Lj6R6WsNXs3Xg g==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="369462464" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="369462464" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:35:22 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773785028" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773785028" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:35:19 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 13/18] KVM: selftests/perf_test_util: vcpu related code consolidation Date: Mon, 24 Oct 2022 19:34:40 +0800 Message-Id: <20221024113445.1022147-14-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747568927210242914?= X-GMAIL-MSGID: =?utf-8?q?1747568927210242914?= Peform vcpu related code consolidation in lib/perf_test_util.c and its users. To be more precise: For the lib, perf_test_util.c: - remove the globally defined *vcpus[] array, as it is a duplicate of vm->vcpus[i], and accordingly, remove the "*vcpus[]" input parameters from the related APIs (e.g. perf_test_setup_vcpus); - remove the globally defined vcpu_thread array, as the vcpu thread has been included into the kvm_vcpu struct, and simplify the implementation in perf_test_start_vcpu_threads by using the related helper functions; - remove the redundant fields in "struct vcpu_thread" (e.g. vcpu_idx), as they are already part of the vcpu struct. Also rename it to "struct vcpu_thread_data" and change it to the vcpu thread's private_data, which is passed to the vcpu threads' start_routine (i.e. vcpu_thread_main). - remove perf_test_join_vcpu_threads as we have a helper function to join the vcpu threads (i.e. vm_vcpu_threads_join), and put it in perf_test_destroy_vm so that users don't need to call threads_join and destroy_vm separately. - change vcpu_fn (per-user vcpu hread's callback routine) to use "struct kvm_vcpu" as an interface, as it is easier to get the related info from vcpu (e.g. vcpu->id); For the users, access_tracking_perf_test.c, demand_paging_test.c, and memslot_modification_stress_test.c, dirty_log_perf_test.c: - change the input parameters of the functions (e.g. vcpu_thread_main) to use "struct kvm_vcpu" as an interface to match the change in the lib; Finally, have the lib and user changes in one patch to ensure the interface and its users are updated together, so that the compilation doesn't complain with errors. Signed-off-by: Wei Wang --- .../selftests/kvm/access_tracking_perf_test.c | 18 +++-- .../selftests/kvm/demand_paging_test.c | 9 +-- .../selftests/kvm/dirty_log_perf_test.c | 11 ++- .../selftests/kvm/include/perf_test_util.h | 9 ++- .../selftests/kvm/lib/perf_test_util.c | 68 +++++++------------ .../selftests/kvm/lib/x86_64/perf_test_util.c | 11 +-- .../kvm/memslot_modification_stress_test.c | 9 +-- 7 files changed, 53 insertions(+), 82 deletions(-) diff --git a/tools/testing/selftests/kvm/access_tracking_perf_test.c b/tools/testing/selftests/kvm/access_tracking_perf_test.c index 76c583a07ea2..878b9189774c 100644 --- a/tools/testing/selftests/kvm/access_tracking_perf_test.c +++ b/tools/testing/selftests/kvm/access_tracking_perf_test.c @@ -125,10 +125,10 @@ static void mark_page_idle(int page_idle_fd, uint64_t pfn) "Set page_idle bits for PFN 0x%" PRIx64, pfn); } -static void mark_vcpu_memory_idle(struct kvm_vm *vm, - struct perf_test_vcpu_args *vcpu_args) +static void mark_vcpu_memory_idle(struct kvm_vm *vm, int vcpu_idx) { - int vcpu_idx = vcpu_args->vcpu_idx; + struct perf_test_vcpu_args *vcpu_args = + &perf_test_args.vcpu_args[vcpu_idx]; uint64_t base_gva = vcpu_args->gva; uint64_t pages = vcpu_args->pages; uint64_t page; @@ -220,11 +220,10 @@ static bool spin_wait_for_next_iteration(int *current_iteration) return true; } -static void vcpu_thread_main(struct perf_test_vcpu_args *vcpu_args) +static void vcpu_thread_main(struct kvm_vcpu *vcpu) { - struct kvm_vcpu *vcpu = vcpu_args->vcpu; - struct kvm_vm *vm = perf_test_args.vm; - int vcpu_idx = vcpu_args->vcpu_idx; + struct kvm_vm *vm = vcpu->vm; + int vcpu_idx = vcpu->id; int current_iteration = 0; while (spin_wait_for_next_iteration(¤t_iteration)) { @@ -234,7 +233,7 @@ static void vcpu_thread_main(struct perf_test_vcpu_args *vcpu_args) assert_ucall(vcpu, UCALL_SYNC); break; case ITERATION_MARK_IDLE: - mark_vcpu_memory_idle(vm, vcpu_args); + mark_vcpu_memory_idle(vm, vcpu_idx); break; }; @@ -306,7 +305,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) vm = perf_test_create_vm(mode, nr_vcpus, params->vcpu_memory_bytes, 1, params->backing_src, !overlap_memory_access); - perf_test_start_vcpu_threads(nr_vcpus, vcpu_thread_main); + perf_test_start_vcpu_threads(vm, vcpu_thread_main); pr_info("\n"); access_memory(vm, nr_vcpus, ACCESS_WRITE, "Populating memory"); @@ -324,7 +323,6 @@ static void run_test(enum vm_guest_mode mode, void *arg) /* Set done to signal the vCPU threads to exit */ done = true; - perf_test_join_vcpu_threads(nr_vcpus); perf_test_destroy_vm(vm); } diff --git a/tools/testing/selftests/kvm/demand_paging_test.c b/tools/testing/selftests/kvm/demand_paging_test.c index 779ae54f89c4..7b8aaf3a5d57 100644 --- a/tools/testing/selftests/kvm/demand_paging_test.c +++ b/tools/testing/selftests/kvm/demand_paging_test.c @@ -14,7 +14,6 @@ #include #include #include -#include #include #include @@ -42,10 +41,9 @@ static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE; static size_t demand_paging_size; static char *guest_data_prototype; -static void vcpu_worker(struct perf_test_vcpu_args *vcpu_args) +static void vcpu_worker(struct kvm_vcpu *vcpu) { - struct kvm_vcpu *vcpu = vcpu_args->vcpu; - int vcpu_idx = vcpu_args->vcpu_idx; + int vcpu_idx = vcpu->id; struct kvm_run *run = vcpu->run; struct timespec start; struct timespec ts_diff; @@ -336,10 +334,9 @@ static void run_test(enum vm_guest_mode mode, void *arg) pr_info("Finished creating vCPUs and starting uffd threads\n"); clock_gettime(CLOCK_MONOTONIC, &start); - perf_test_start_vcpu_threads(nr_vcpus, vcpu_worker); + perf_test_start_vcpu_threads(vm, vcpu_worker); pr_info("Started all vCPUs\n"); - perf_test_join_vcpu_threads(nr_vcpus); ts_diff = timespec_elapsed(start); pr_info("All vCPU threads joined\n"); diff --git a/tools/testing/selftests/kvm/dirty_log_perf_test.c b/tools/testing/selftests/kvm/dirty_log_perf_test.c index f99e39a672d3..808d3d768c82 100644 --- a/tools/testing/selftests/kvm/dirty_log_perf_test.c +++ b/tools/testing/selftests/kvm/dirty_log_perf_test.c @@ -11,7 +11,6 @@ #include #include #include -#include #include #include "kvm_util.h" @@ -67,10 +66,11 @@ static bool host_quit; static int iteration; static int vcpu_last_completed_iteration[KVM_MAX_VCPUS]; -static void vcpu_worker(struct perf_test_vcpu_args *vcpu_args) +static void vcpu_worker(struct kvm_vcpu *vcpu) { - struct kvm_vcpu *vcpu = vcpu_args->vcpu; - int vcpu_idx = vcpu_args->vcpu_idx; + int vcpu_idx = vcpu->id; + struct perf_test_vcpu_args *vcpu_args = + &perf_test_args.vcpu_args[vcpu_idx]; uint64_t pages_count = 0; struct kvm_run *run; struct timespec start; @@ -248,7 +248,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) for (i = 0; i < nr_vcpus; i++) vcpu_last_completed_iteration[i] = -1; - perf_test_start_vcpu_threads(nr_vcpus, vcpu_worker); + perf_test_start_vcpu_threads(vm, vcpu_worker); /* Allow the vCPUs to populate memory */ pr_debug("Starting iteration %d - Populating\n", iteration); @@ -329,7 +329,6 @@ static void run_test(enum vm_guest_mode mode, void *arg) * wait for them to exit. */ host_quit = true; - perf_test_join_vcpu_threads(nr_vcpus); avg = timespec_div(get_dirty_log_total, p->iterations); pr_info("Get dirty log over %lu iterations took %ld.%.9lds. (Avg %ld.%.9lds/iteration)\n", diff --git a/tools/testing/selftests/kvm/include/perf_test_util.h b/tools/testing/selftests/kvm/include/perf_test_util.h index eaa88df0555a..43816756c1da 100644 --- a/tools/testing/selftests/kvm/include/perf_test_util.h +++ b/tools/testing/selftests/kvm/include/perf_test_util.h @@ -24,8 +24,7 @@ struct perf_test_vcpu_args { uint64_t gva; uint64_t pages; - /* Only used by the host userspace part of the vCPU thread */ - struct kvm_vcpu *vcpu; + /* For guest to check if data is corrupted */ int vcpu_idx; }; @@ -53,11 +52,11 @@ void perf_test_destroy_vm(struct kvm_vm *vm); void perf_test_set_wr_fract(struct kvm_vm *vm, int wr_fract); -void perf_test_start_vcpu_threads(int vcpus, void (*vcpu_fn)(struct perf_test_vcpu_args *)); -void perf_test_join_vcpu_threads(int vcpus); +void perf_test_start_vcpu_threads(struct kvm_vm *vm, + void (*vcpu_fn)(struct kvm_vcpu *vcpu)); void perf_test_guest_code(uint32_t vcpu_id); uint64_t perf_test_nested_pages(int nr_vcpus); -void perf_test_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_vcpu *vcpus[]); +void perf_test_setup_nested(struct kvm_vm *vm); #endif /* SELFTEST_KVM_PERF_TEST_UTIL_H */ diff --git a/tools/testing/selftests/kvm/lib/perf_test_util.c b/tools/testing/selftests/kvm/lib/perf_test_util.c index 9618b37c66f7..94c0f496c9c1 100644 --- a/tools/testing/selftests/kvm/lib/perf_test_util.c +++ b/tools/testing/selftests/kvm/lib/perf_test_util.c @@ -16,28 +16,17 @@ struct perf_test_args perf_test_args; */ static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM; -struct vcpu_thread { - /* The index of the vCPU. */ - int vcpu_idx; - - /* The pthread backing the vCPU. */ - pthread_t thread; - +struct vcpu_thread_data { /* Set to true once the vCPU thread is up and running. */ bool running; }; -/* The vCPU threads involved in this test. */ -static struct vcpu_thread vcpu_threads[KVM_MAX_VCPUS]; - /* The function run by each vCPU thread, as provided by the test. */ -static void (*vcpu_thread_fn)(struct perf_test_vcpu_args *); +static void (*vcpu_thread_fn)(struct kvm_vcpu *); /* Set to true once all vCPU threads are up and running. */ static bool all_vcpu_threads_running; -static struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; - /* * Continuously write to the first 8 bytes of each page in the * specified region. @@ -71,7 +60,6 @@ void perf_test_guest_code(uint32_t vcpu_idx) } void perf_test_setup_vcpus(struct kvm_vm *vm, int nr_vcpus, - struct kvm_vcpu *vcpus[], uint64_t vcpu_memory_bytes, bool partition_vcpu_memory_access) { @@ -82,7 +70,6 @@ void perf_test_setup_vcpus(struct kvm_vm *vm, int nr_vcpus, for (i = 0; i < nr_vcpus; i++) { vcpu_args = &pta->vcpu_args[i]; - vcpu_args->vcpu = vcpus[i]; vcpu_args->vcpu_idx = i; if (partition_vcpu_memory_access) { @@ -98,7 +85,7 @@ void perf_test_setup_vcpus(struct kvm_vm *vm, int nr_vcpus, vcpu_args->gpa = pta->gpa; } - vcpu_args_set(vcpus[i], 1, i); + vcpu_args_set(vm->vcpus[i], 1, i); pr_debug("Added VCPU %d with test mem gpa [%lx, %lx)\n", i, vcpu_args->gpa, vcpu_args->gpa + @@ -153,7 +140,7 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int nr_vcpus, * effect as KVM allows aliasing HVAs in meslots. */ vm = __vm_create_with_vcpus(mode, nr_vcpus, slot0_pages + guest_num_pages, - perf_test_guest_code, vcpus); + perf_test_guest_code, NULL); pta->vm = vm; @@ -201,12 +188,12 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int nr_vcpus, /* Do mapping for the demand paging memory slot */ virt_map(vm, guest_test_virt_mem, pta->gpa, guest_num_pages); - perf_test_setup_vcpus(vm, nr_vcpus, vcpus, vcpu_memory_bytes, + perf_test_setup_vcpus(vm, nr_vcpus, vcpu_memory_bytes, partition_vcpu_memory_access); if (pta->nested) { pr_info("Configuring vCPUs to run in L2 (nested).\n"); - perf_test_setup_nested(vm, nr_vcpus, vcpus); + perf_test_setup_nested(vm); } ucall_init(vm, NULL); @@ -219,6 +206,9 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int nr_vcpus, void perf_test_destroy_vm(struct kvm_vm *vm) { + vm_vcpu_threads_join(vm); + pr_info("All vCPU threads joined\n"); + ucall_uninit(vm); kvm_vm_free(vm); } @@ -234,7 +224,7 @@ uint64_t __weak perf_test_nested_pages(int nr_vcpus) return 0; } -void __weak perf_test_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_vcpu **vcpus) +void __weak perf_test_setup_nested(struct kvm_vm *vm) { pr_info("%s() not support on this architecture, skipping.\n", __func__); exit(KSFT_SKIP); @@ -242,9 +232,11 @@ void __weak perf_test_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_v static void *vcpu_thread_main(void *data) { - struct vcpu_thread *vcpu = data; + struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; + struct vcpu_thread_data *thread_data = + (struct vcpu_thread_data *)vcpu->private_data; - WRITE_ONCE(vcpu->running, true); + WRITE_ONCE(thread_data->running, true); /* * Wait for all vCPU threads to be up and running before calling the test- @@ -255,40 +247,30 @@ static void *vcpu_thread_main(void *data) while (!READ_ONCE(all_vcpu_threads_running)) ; - vcpu_thread_fn(&perf_test_args.vcpu_args[vcpu->vcpu_idx]); + vcpu_thread_fn(vcpu); return NULL; } -void perf_test_start_vcpu_threads(int nr_vcpus, - void (*vcpu_fn)(struct perf_test_vcpu_args *)) +void perf_test_start_vcpu_threads(struct kvm_vm *vm, + void (*vcpu_fn)(struct kvm_vcpu *)) { int i; + struct kvm_vcpu *vcpu; + struct vcpu_thread_data *thread_data; vcpu_thread_fn = vcpu_fn; WRITE_ONCE(all_vcpu_threads_running, false); - for (i = 0; i < nr_vcpus; i++) { - struct vcpu_thread *vcpu = &vcpu_threads[i]; - - vcpu->vcpu_idx = i; - WRITE_ONCE(vcpu->running, false); + /* thread_data->running already false-initialized on allocation */ + vm_vcpu_threads_create(vm, vcpu_thread_main, + sizeof(struct vcpu_thread_data)); - pthread_create(&vcpu->thread, NULL, vcpu_thread_main, vcpu); - } - - for (i = 0; i < nr_vcpus; i++) { - while (!READ_ONCE(vcpu_threads[i].running)) + vm_iterate_over_vcpus(vm, vcpu, i) { + thread_data = (struct vcpu_thread_data *)vcpu->private_data; + while (!READ_ONCE(thread_data->running)) ; } WRITE_ONCE(all_vcpu_threads_running, true); } - -void perf_test_join_vcpu_threads(int nr_vcpus) -{ - int i; - - for (i = 0; i < nr_vcpus; i++) - pthread_join(vcpu_threads[i].thread, NULL); -} diff --git a/tools/testing/selftests/kvm/lib/x86_64/perf_test_util.c b/tools/testing/selftests/kvm/lib/x86_64/perf_test_util.c index 0f344a7c89c4..8c4c87df5b8d 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/perf_test_util.c +++ b/tools/testing/selftests/kvm/lib/x86_64/perf_test_util.c @@ -77,16 +77,17 @@ void perf_test_setup_ept(struct vmx_pages *vmx, struct kvm_vm *vm) nested_identity_map_1g(vmx, vm, start, end - start); } -void perf_test_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_vcpu *vcpus[]) +void perf_test_setup_nested(struct kvm_vm *vm) { struct vmx_pages *vmx, *vmx0 = NULL; struct kvm_regs regs; vm_vaddr_t vmx_gva; int vcpu_id; + struct kvm_vcpu *vcpu; TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX)); - for (vcpu_id = 0; vcpu_id < nr_vcpus; vcpu_id++) { + vm_iterate_over_vcpus(vm, vcpu, vcpu_id) { vmx = vcpu_alloc_vmx(vm, &vmx_gva); if (vcpu_id == 0) { @@ -103,9 +104,9 @@ void perf_test_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_vcpu *vc * Override the vCPU to run perf_test_l1_guest_code() which will * bounce it into L2 before calling perf_test_guest_code(). */ - vcpu_regs_get(vcpus[vcpu_id], ®s); + vcpu_regs_get(vcpu, ®s); regs.rip = (unsigned long) perf_test_l1_guest_code; - vcpu_regs_set(vcpus[vcpu_id], ®s); - vcpu_args_set(vcpus[vcpu_id], 2, vmx_gva, vcpu_id); + vcpu_regs_set(vcpu, ®s); + vcpu_args_set(vcpu, 2, vmx_gva, vcpu->id); } } diff --git a/tools/testing/selftests/kvm/memslot_modification_stress_test.c b/tools/testing/selftests/kvm/memslot_modification_stress_test.c index bb1d17a1171b..d41d2b989a91 100644 --- a/tools/testing/selftests/kvm/memslot_modification_stress_test.c +++ b/tools/testing/selftests/kvm/memslot_modification_stress_test.c @@ -16,7 +16,6 @@ #include #include #include -#include #include #include #include @@ -36,9 +35,8 @@ static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE; static bool run_vcpus = true; -static void vcpu_worker(struct perf_test_vcpu_args *vcpu_args) +static void vcpu_worker(struct kvm_vcpu *vcpu) { - struct kvm_vcpu *vcpu = vcpu_args->vcpu; struct kvm_run *run; int ret; @@ -103,7 +101,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) pr_info("Finished creating vCPUs\n"); - perf_test_start_vcpu_threads(nr_vcpus, vcpu_worker); + perf_test_start_vcpu_threads(vm, vcpu_worker); pr_info("Started all vCPUs\n"); @@ -112,9 +110,6 @@ static void run_test(enum vm_guest_mode mode, void *arg) run_vcpus = false; - perf_test_join_vcpu_threads(nr_vcpus); - pr_info("All vCPU threads joined\n"); - perf_test_destroy_vm(vm); } From patchwork Mon Oct 24 11:34:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8397 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp395995wru; Mon, 24 Oct 2022 04:41:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4YTDKvAZKmmn1fHQQQr11BnHio7pOWTEs3EQHQdFQgEs3A1kLDx2K+Zb5J+s/09Zmaouk1 X-Received: by 2002:a17:907:7e90:b0:78d:50f7:8abd with SMTP id qb16-20020a1709077e9000b0078d50f78abdmr26159854ejc.523.1666611709688; Mon, 24 Oct 2022 04:41:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611709; cv=none; d=google.com; s=arc-20160816; b=lwEvrACKm9JLQVewqcRQuag/w2YeyuqYjn7zCmq2B6Q8Mred3H7vUxo6AYicYjp4JN U8/Jqe5w+EdhCo752XN8VVTeXt/mKWCiM3wdbPKi1DcNGx9yhpRzWwZToMT4eWqY79ET 1oGXigS6xLeJj3IIa9AscvWIsC/ZiBdyMlcy7Q5LSt1ZgWrAsUMQ8eJnmkvpw5emzXyB fRsgOLMefozofRZcLgwgD340o0AzHgvUKUG+AW83fCXgZjt2Zifz+ygwpRuqToI5Jcle ivXhZ256Ptuam0ObkxiAPKCGuyQaL3O+/BabHUCZ1hoGcfWgG77DZSDIT7LBPAST4TjJ HLWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JNrg7Zp9vUpUfrDGa7SXdwHWBMeW1Vnb11j9leIRYMU=; b=H1H7rXNx/PIJWgS0lZK/J7UwAEHsNzUhyk62y1KfKT69/jyJMd5U9LJJopPKs+2JfL IajSAGioKFJoSyDSBBrURIJDZNS/lWW91rzCz8TX2VcXed2NWnzCa6XB6MXsN21FpG4Z p69NG5PDzyB6zDlNN9vGcipS9K2y6LwgpN56sHNzHHMGTPiKL2SAeUQKzmi37SShYk+h W1MdJuLfRiwr8tuNmjqFhS25k6ekmusbFdDXvhpXat3vT8jeZKKKV5nQbEfIx2OUoD7P 5ZWuQHLWDVhNwVZXtO7AN989IYhUULyCieox5F5hW6k2QPupc9MoOOzbJ126c+6ZbDnQ EjEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=OKB6cXOJ; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a15-20020aa7d74f000000b004614db9789asi8076104eds.127.2022.10.24.04.41.26; Mon, 24 Oct 2022 04:41:49 -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=@intel.com header.s=Intel header.b=OKB6cXOJ; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231376AbiJXLkZ (ORCPT + 99 others); Mon, 24 Oct 2022 07:40:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231360AbiJXLjF (ORCPT ); Mon, 24 Oct 2022 07:39:05 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85B98356CB; Mon, 24 Oct 2022 04:37:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611475; x=1698147475; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4jBh95k+8xwo1JohtTSjVth9N3E+IK9boL8mgzWoN7E=; b=OKB6cXOJBYkJsTt5y34AAkO4QxMqO5Fq5IsnDz+DNQUqjATYN43tC8jz WBcclExBf6f2mK3nltTNmuxs8ctAFVCskdxOCZzJhfkLoskEKRKNo3G+d I3Gbef+YcNeD1VuK90ZwBaJiw2Et4sTirbde9O6fmPO4QR59cPtHjsc7R YYlRbxaeHWHWdXurWSaD8Z+SFNS6u5x5h8ovPhC9dZlUpyAu2UlKftCGN Jy1kdnVlDJHIkQgEd4RqkhSSYltcB9P6t36HwG5sCLwzCKEI534KjPVbk dIJY9HHX0cLLvsZs2tn72DpGPOYv7yedxK6eYcpUqsc2wZ2JtbS6Ou82p g==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="369462469" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="369462469" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:35:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773785039" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773785039" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:35:22 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 14/18] KVM: selftest/memslot_perf_test: vcpu related code consolidation Date: Mon, 24 Oct 2022 19:34:41 +0800 Message-Id: <20221024113445.1022147-15-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747569040236932301?= X-GMAIL-MSGID: =?utf-8?q?1747569040236932301?= Remove the vcpu and vcpu_thread fields in the vm_data struct and reuse the one in the kvm_vm strut. Rename vm_data to vcpu_thread_data and make it be the vcpu thread's private data. Also use the helper functions to create and join the vcpu thread. Signed-off-by: Wei Wang --- .../testing/selftests/kvm/memslot_perf_test.c | 137 +++++++++--------- 1 file changed, 71 insertions(+), 66 deletions(-) diff --git a/tools/testing/selftests/kvm/memslot_perf_test.c b/tools/testing/selftests/kvm/memslot_perf_test.c index 44995446d942..bf37d1bb8d50 100644 --- a/tools/testing/selftests/kvm/memslot_perf_test.c +++ b/tools/testing/selftests/kvm/memslot_perf_test.c @@ -6,7 +6,6 @@ * * Basic guest setup / host vCPU thread code lifted from set_memory_region_test. */ -#include #include #include #include @@ -86,10 +85,7 @@ static_assert(MEM_TEST_MOVE_SIZE <= MEM_TEST_SIZE, #define MEM_TEST_VAL_1 0x1122334455667788 #define MEM_TEST_VAL_2 0x99AABBCCDDEEFF00 -struct vm_data { - struct kvm_vm *vm; - struct kvm_vcpu *vcpu; - pthread_t vcpu_thread; +struct vcpu_thread_data { uint32_t nslots; uint64_t npages; uint64_t pages_per_slot; @@ -126,7 +122,7 @@ static bool verbose; pr_info(__VA_ARGS__); \ } while (0) -static void check_mmio_access(struct vm_data *data, struct kvm_run *run) +static void check_mmio_access(struct vcpu_thread_data *data, struct kvm_run *run) { TEST_ASSERT(data->mmio_ok, "Unexpected mmio exit"); TEST_ASSERT(run->mmio.is_write, "Unexpected mmio read"); @@ -140,8 +136,9 @@ static void check_mmio_access(struct vm_data *data, struct kvm_run *run) static void *vcpu_worker(void *__data) { - struct vm_data *data = __data; - struct kvm_vcpu *vcpu = data->vcpu; + struct kvm_vcpu *vcpu = (struct kvm_vcpu *)__data; + struct vcpu_thread_data *data = + (struct vcpu_thread_data *)vcpu->private_data; struct kvm_run *run = vcpu->run; struct ucall uc; @@ -187,7 +184,8 @@ static void wait_for_vcpu(void) "sem_timedwait() failed: %d\n", errno); } -static void *vm_gpa2hva(struct vm_data *data, uint64_t gpa, uint64_t *rempages) +static void *vm_gpa2hva(struct vcpu_thread_data *data, + uint64_t gpa, uint64_t *rempages) { uint64_t gpage, pgoffs; uint32_t slot, slotoffs; @@ -220,31 +218,19 @@ static void *vm_gpa2hva(struct vm_data *data, uint64_t gpa, uint64_t *rempages) return (uint8_t *)base + slotoffs * 4096 + pgoffs; } -static uint64_t vm_slot2gpa(struct vm_data *data, uint32_t slot) +static uint64_t vm_slot2gpa(struct vcpu_thread_data *data, uint32_t slot) { TEST_ASSERT(slot < data->nslots, "Too high slot number"); return MEM_GPA + slot * data->pages_per_slot * 4096; } -static struct vm_data *alloc_vm(void) -{ - struct vm_data *data; - - data = malloc(sizeof(*data)); - TEST_ASSERT(data, "malloc(vmdata) failed"); - - data->vm = NULL; - data->vcpu = NULL; - data->hva_slots = NULL; - - return data; -} - -static bool prepare_vm(struct vm_data *data, int nslots, uint64_t *maxslots, - void *guest_code, uint64_t mempages, - struct timespec *slot_runtime) +static bool prepare_vm(struct kvm_vm **out_vm, int nslots, uint64_t *maxslots, + void *guest_code, uint64_t mempages, struct timespec *slot_runtime) { + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + struct vcpu_thread_data *data; uint32_t max_mem_slots; uint64_t rempages; uint64_t guest_addr; @@ -263,6 +249,11 @@ static bool prepare_vm(struct vm_data *data, int nslots, uint64_t *maxslots, TEST_ASSERT(mempages > 1, "Can't test without any memory"); + vm = __vm_create_with_one_vcpu(&vcpu, mempages, guest_code); + *out_vm = vm; + vm_vcpu_threads_private_data_alloc(vm, + sizeof(struct vcpu_thread_data)); + data = (struct vcpu_thread_data *)vcpu->private_data; data->npages = mempages; data->nslots = max_mem_slots - 1; @@ -276,8 +267,7 @@ static bool prepare_vm(struct vm_data *data, int nslots, uint64_t *maxslots, data->hva_slots = malloc(sizeof(*data->hva_slots) * data->nslots); TEST_ASSERT(data->hva_slots, "malloc() fail"); - data->vm = __vm_create_with_one_vcpu(&data->vcpu, mempages, guest_code); - ucall_init(data->vm, NULL); + ucall_init(vm, NULL); pr_info_v("Adding slots 1..%i, each slot with %"PRIu64" pages + %"PRIu64" extra pages last\n", max_mem_slots - 1, data->pages_per_slot, rempages); @@ -290,7 +280,7 @@ static bool prepare_vm(struct vm_data *data, int nslots, uint64_t *maxslots, if (slot == max_mem_slots - 1) npages += rempages; - vm_userspace_mem_region_add(data->vm, VM_MEM_SRC_ANONYMOUS, + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, guest_addr, slot, npages, 0); guest_addr += npages * 4096; @@ -305,18 +295,18 @@ static bool prepare_vm(struct vm_data *data, int nslots, uint64_t *maxslots, if (slot == max_mem_slots - 2) npages += rempages; - gpa = vm_phy_pages_alloc(data->vm, npages, guest_addr, + gpa = vm_phy_pages_alloc(vm, npages, guest_addr, slot + 1); TEST_ASSERT(gpa == guest_addr, "vm_phy_pages_alloc() failed\n"); - data->hva_slots[slot] = addr_gpa2hva(data->vm, guest_addr); + data->hva_slots[slot] = addr_gpa2hva(vm, guest_addr); memset(data->hva_slots[slot], 0, npages * 4096); guest_addr += npages * 4096; } - virt_map(data->vm, MEM_GPA, MEM_GPA, mempages); + virt_map(vm, MEM_GPA, MEM_GPA, mempages); sync = (typeof(sync))vm_gpa2hva(data, MEM_SYNC_GPA, NULL); atomic_init(&sync->start_flag, false); @@ -328,26 +318,22 @@ static bool prepare_vm(struct vm_data *data, int nslots, uint64_t *maxslots, return true; } -static void launch_vm(struct vm_data *data) +static void launch_vm(struct kvm_vm *vm) { pr_info_v("Launching the test VM\n"); - pthread_create(&data->vcpu_thread, NULL, vcpu_worker, data); + vm_vcpu_threads_create(vm, vcpu_worker, 0); /* Ensure the guest thread is spun up. */ wait_for_vcpu(); } -static void free_vm(struct vm_data *data) +static void vcpu_thread_data_free(struct vcpu_thread_data *data) { - kvm_vm_free(data->vm); - free(data->hva_slots); - free(data); -} - -static void wait_guest_exit(struct vm_data *data) -{ - pthread_join(data->vcpu_thread, NULL); + if (data) { + free(data->hva_slots); + free(data); + } } static void let_guest_run(struct sync_area *sync) @@ -535,7 +521,7 @@ static void guest_code_test_memslot_rw(void) GUEST_DONE(); } -static bool test_memslot_move_prepare(struct vm_data *data, +static bool test_memslot_move_prepare(struct vcpu_thread_data *data, struct sync_area *sync, uint64_t *maxslots, bool isactive) { @@ -565,31 +551,33 @@ static bool test_memslot_move_prepare(struct vm_data *data, return true; } -static bool test_memslot_move_prepare_active(struct vm_data *data, +static bool test_memslot_move_prepare_active(struct vcpu_thread_data *data, struct sync_area *sync, uint64_t *maxslots) { return test_memslot_move_prepare(data, sync, maxslots, true); } -static bool test_memslot_move_prepare_inactive(struct vm_data *data, +static bool test_memslot_move_prepare_inactive(struct vcpu_thread_data *data, struct sync_area *sync, uint64_t *maxslots) { return test_memslot_move_prepare(data, sync, maxslots, false); } -static void test_memslot_move_loop(struct vm_data *data, struct sync_area *sync) +static void test_memslot_move_loop(struct kvm_vcpu *vcpu, struct sync_area *sync) { uint64_t movesrcgpa; + struct vcpu_thread_data *data = + (struct vcpu_thread_data *)vcpu->private_data; movesrcgpa = vm_slot2gpa(data, data->nslots - 1); - vm_mem_region_move(data->vm, data->nslots - 1 + 1, + vm_mem_region_move(vcpu->vm, data->nslots - 1 + 1, MEM_TEST_MOVE_GPA_DEST); - vm_mem_region_move(data->vm, data->nslots - 1 + 1, movesrcgpa); + vm_mem_region_move(vcpu->vm, data->nslots - 1 + 1, movesrcgpa); } -static void test_memslot_do_unmap(struct vm_data *data, +static void test_memslot_do_unmap(struct vcpu_thread_data *data, uint64_t offsp, uint64_t count) { uint64_t gpa, ctr; @@ -613,7 +601,7 @@ static void test_memslot_do_unmap(struct vm_data *data, "madvise(MADV_DONTNEED) should exactly cover all of the requested area"); } -static void test_memslot_map_unmap_check(struct vm_data *data, +static void test_memslot_map_unmap_check(struct vcpu_thread_data *data, uint64_t offsp, uint64_t valexp) { uint64_t gpa; @@ -630,8 +618,11 @@ static void test_memslot_map_unmap_check(struct vm_data *data, *val = 0; } -static void test_memslot_map_loop(struct vm_data *data, struct sync_area *sync) +static void test_memslot_map_loop(struct kvm_vcpu *vcpu, + struct sync_area *sync) { + struct vcpu_thread_data *data = + (struct vcpu_thread_data *)vcpu->private_data; /* * Unmap the second half of the test area while guest writes to (maps) * the first half. @@ -670,7 +661,7 @@ static void test_memslot_map_loop(struct vm_data *data, struct sync_area *sync) MEM_TEST_VAL_2); } -static void test_memslot_unmap_loop_common(struct vm_data *data, +static void test_memslot_unmap_loop_common(struct vcpu_thread_data *data, struct sync_area *sync, uint64_t chunk) { @@ -697,21 +688,30 @@ static void test_memslot_unmap_loop_common(struct vm_data *data, test_memslot_do_unmap(data, ctr, chunk); } -static void test_memslot_unmap_loop(struct vm_data *data, +static void test_memslot_unmap_loop(struct kvm_vcpu *vcpu, struct sync_area *sync) { + struct vcpu_thread_data *data = + (struct vcpu_thread_data *)vcpu->private_data; + test_memslot_unmap_loop_common(data, sync, 1); } -static void test_memslot_unmap_loop_chunked(struct vm_data *data, +static void test_memslot_unmap_loop_chunked(struct kvm_vcpu *vcpu, struct sync_area *sync) { + struct vcpu_thread_data *data = + (struct vcpu_thread_data *)vcpu->private_data; + test_memslot_unmap_loop_common(data, sync, MEM_TEST_UNMAP_CHUNK_PAGES); } -static void test_memslot_rw_loop(struct vm_data *data, struct sync_area *sync) +static void test_memslot_rw_loop(struct kvm_vcpu *vcpu, + struct sync_area *sync) { uint64_t gptr; + struct vcpu_thread_data *data = + (struct vcpu_thread_data *)vcpu->private_data; for (gptr = MEM_TEST_GPA + 4096 / 2; gptr < MEM_TEST_GPA + MEM_TEST_SIZE; gptr += 4096) @@ -737,9 +737,9 @@ struct test_data { const char *name; uint64_t mem_size; void (*guest_code)(void); - bool (*prepare)(struct vm_data *data, struct sync_area *sync, + bool (*prepare)(struct vcpu_thread_data *data, struct sync_area *sync, uint64_t *maxslots); - void (*loop)(struct vm_data *data, struct sync_area *sync); + void (*loop)(struct kvm_vcpu *vcpu, struct sync_area *sync); }; static bool test_execute(int nslots, uint64_t *maxslots, @@ -750,18 +750,22 @@ static bool test_execute(int nslots, uint64_t *maxslots, struct timespec *guest_runtime) { uint64_t mem_size = tdata->mem_size ? : MEM_SIZE_PAGES; - struct vm_data *data; + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + struct vcpu_thread_data *data = NULL; struct sync_area *sync; struct timespec tstart; bool ret = true; - data = alloc_vm(); - if (!prepare_vm(data, nslots, maxslots, tdata->guest_code, + if (!prepare_vm(&vm, nslots, maxslots, tdata->guest_code, mem_size, slot_runtime)) { ret = false; goto exit_free; } + vcpu = vm->vcpus[0]; + data = (struct vcpu_thread_data *)vcpu->private_data; + sync = (typeof(sync))vm_gpa2hva(data, MEM_SYNC_GPA, NULL); if (tdata->prepare && @@ -770,7 +774,7 @@ static bool test_execute(int nslots, uint64_t *maxslots, goto exit_free; } - launch_vm(data); + launch_vm(vm); clock_gettime(CLOCK_MONOTONIC, &tstart); let_guest_run(sync); @@ -780,16 +784,17 @@ static bool test_execute(int nslots, uint64_t *maxslots, if (guest_runtime->tv_sec >= maxtime) break; - tdata->loop(data, sync); + tdata->loop(vcpu, sync); (*nloops)++; } make_guest_exit(sync); - wait_guest_exit(data); + vm_vcpu_threads_join(vm); exit_free: - free_vm(data); + vcpu_thread_data_free(data); + kvm_vm_free(vm); return ret; } From patchwork Mon Oct 24 11:34:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8386 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp394471wru; Mon, 24 Oct 2022 04:40:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4HSIgVy4w5EK0KJzI3EubhX/TRA+9Igi6ZQ0lZp9/g/1pMakwcyIxeD68Hb/fipc+o7ttd X-Received: by 2002:a05:6402:b6b:b0:461:b9b2:6d58 with SMTP id cb11-20020a0564020b6b00b00461b9b26d58mr5627595edb.186.1666611614788; Mon, 24 Oct 2022 04:40:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611614; cv=none; d=google.com; s=arc-20160816; b=qLwi+4BGXH99oHyqws77rjYMjW3XX61mFv5GKFODV5gBr5Xm5lY49/WcrGhz1anyQt MXduwYR2+EoWvjwvR70jdssUbNYegMlOvh5dWFrgeiEF3e7WLDEbZwdWmiva8tV65JCS fSRfdNVCzRv5fjjoQ39PpRGeAbFB/YTKQIjfW2O5oThiQWxEUQz079/N4CmBDc6+KjhB 8FCvII+5Pi6r4RycyzrXuxw36nz3A7qYm4cQaeajWwJujXG5BWMvA8w6yJNQkOxV4DhJ nN6sMuElmZUd+Ql5U2lD7eoY+Kk92CU1+1vBsMW4bf8hhyzlwZX0AQ/RRw33LmLEtV1T n29g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=sN0XhYzDJIMNgBzjisHXk3C9LHGA2Vf28iZrlTlTnyk=; b=UMejz0Nsu55oHC083+gj6JVk1wul3mJDDIkmoJkNglzLV5ND1KMn1pJpD8GwfXrOj7 pi6Fr1s0MDR4AHmHNrs/eWzOIX7L2wssBnE/DEhuIaSdIprBzc9SGFKlk1CpbRDOqp/G IJpA+DrTYizMvbznf5iSL+e9XG35dOCVsz4EnPhTyt2azaijX114jM7+w68s7B2ErE0p bWJJ6PQr4aFYd4BndPjLjHxaRMGUqVlbxzxyLXIK5+2SzjUS+GLnEcDPYwua8jHqKHli m9E6xk+qtHA8FGGCK78zK8oDoKUZNZRJHoSC2N6gt9uRqa68Mtb4bV43qx1yIpmwLCMc Gcew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=gvdUL6By; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ay17-20020a170906d29100b0078df40b4300si22093132ejb.53.2022.10.24.04.39.51; Mon, 24 Oct 2022 04:40:14 -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=@intel.com header.s=Intel header.b=gvdUL6By; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230478AbiJXLj2 (ORCPT + 99 others); Mon, 24 Oct 2022 07:39:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230487AbiJXLie (ORCPT ); Mon, 24 Oct 2022 07:38:34 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA50FB1E9; Mon, 24 Oct 2022 04:38:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611482; x=1698147482; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=VfbQaPDh8z2xxcOon1s6cTrPqmF5WJAx6/Ye0pT0g4o=; b=gvdUL6ByCWqDhy8Km5LfNivDeZq98j17Oax7mlmuybFDO6fA6zELwBvc 7HTOIR2OxMSsVYABS8eVtSQ8/nSzIoKSsgot0fB5HN6AC5KGzZyRKNqvM ZRD7cn7ZjSQW6MnwkiErKmfFAzQotpnX8TAJdlOUKgpm84pFCm5CvktLt VmlRSFIErg6vVRYFdjamTQIYy2DF7t0F1lEObIwxL00gkoAq69okq3aFZ LXhowZVv1jMDi229AtKfmD//ZmOfvvnWrqF3RxslRPbTDC4MfqymZEUmT t3I7Vn0H8EmSgTpFwLKPwsBScM0WRzI5a/JtNfPXKaPO4oFz1Hk6gFo8R w==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="369462478" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="369462478" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:35:27 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773785057" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773785057" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:35:25 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 15/18] KVM: selftests/vgic_init: vcpu related code consolidation Date: Mon, 24 Oct 2022 19:34:42 +0800 Message-Id: <20221024113445.1022147-16-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747568940719855389?= X-GMAIL-MSGID: =?utf-8?q?1747568940719855389?= Remove the unnecessary definition of the *vcpu[] array and reuse the one from kvm_vm. Simplify the related function interface by removeing the *vcpu[]. Signed-off-by: Wei Wang --- .../testing/selftests/kvm/aarch64/vgic_init.c | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/tools/testing/selftests/kvm/aarch64/vgic_init.c b/tools/testing/selftests/kvm/aarch64/vgic_init.c index 9c131d977a1b..e24130a49581 100644 --- a/tools/testing/selftests/kvm/aarch64/vgic_init.c +++ b/tools/testing/selftests/kvm/aarch64/vgic_init.c @@ -74,13 +74,12 @@ static int run_vcpu(struct kvm_vcpu *vcpu) } static struct vm_gic vm_gic_create_with_vcpus(uint32_t gic_dev_type, - uint32_t nr_vcpus, - struct kvm_vcpu *vcpus[]) + uint32_t nr_vcpus) { struct vm_gic v; v.gic_dev_type = gic_dev_type; - v.vm = vm_create_with_vcpus(nr_vcpus, guest_code, vcpus); + v.vm = vm_create_with_vcpus(nr_vcpus, guest_code, NULL); v.gic_fd = kvm_create_device(v.vm, gic_dev_type); return v; @@ -324,14 +323,15 @@ static void subtest_v3_redist_regions(struct vm_gic *v) */ static void test_vgic_then_vcpus(uint32_t gic_dev_type) { - struct kvm_vcpu *vcpus[NR_VCPUS]; + struct kvm_vcpu **vcpus; struct vm_gic v; int ret, i; - v = vm_gic_create_with_vcpus(gic_dev_type, 1, vcpus); + v = vm_gic_create_with_vcpus(gic_dev_type, 1); subtest_dist_rdist(&v); + vcpus = v.vm->vcpus; /* Add the rest of the VCPUs */ for (i = 1; i < NR_VCPUS; ++i) vcpus[i] = vm_vcpu_add(v.vm, i, guest_code); @@ -345,15 +345,14 @@ static void test_vgic_then_vcpus(uint32_t gic_dev_type) /* All the VCPUs are created before the VGIC KVM device gets initialized */ static void test_vcpus_then_vgic(uint32_t gic_dev_type) { - struct kvm_vcpu *vcpus[NR_VCPUS]; struct vm_gic v; int ret; - v = vm_gic_create_with_vcpus(gic_dev_type, NR_VCPUS, vcpus); + v = vm_gic_create_with_vcpus(gic_dev_type, NR_VCPUS); subtest_dist_rdist(&v); - ret = run_vcpu(vcpus[3]); + ret = run_vcpu(v.vm->vcpus[3]); TEST_ASSERT(ret == -EINVAL, "dist/rdist overlap detected on 1st vcpu run"); vm_gic_destroy(&v); @@ -361,24 +360,25 @@ static void test_vcpus_then_vgic(uint32_t gic_dev_type) static void test_v3_new_redist_regions(void) { - struct kvm_vcpu *vcpus[NR_VCPUS]; + struct kvm_vcpu **vcpus; void *dummy = NULL; struct vm_gic v; uint64_t addr; int ret; - v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS, vcpus); + v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS); subtest_v3_redist_regions(&v); kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, KVM_DEV_ARM_VGIC_CTRL_INIT, NULL); + vcpus = v.vm->vcpus; ret = run_vcpu(vcpus[3]); TEST_ASSERT(ret == -ENXIO, "running without sufficient number of rdists"); vm_gic_destroy(&v); /* step2 */ - v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS, vcpus); + v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS); subtest_v3_redist_regions(&v); addr = REDIST_REGION_ATTR_ADDR(1, 0x280000, 0, 2); @@ -392,7 +392,7 @@ static void test_v3_new_redist_regions(void) /* step 3 */ - v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS, vcpus); + v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS); subtest_v3_redist_regions(&v); ret = __kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, @@ -559,12 +559,12 @@ static void test_v3_last_bit_single_rdist(void) /* Uses the legacy REDIST region API. */ static void test_v3_redist_ipa_range_check_at_vcpu_run(void) { - struct kvm_vcpu *vcpus[NR_VCPUS]; + struct kvm_vcpu **vcpus; struct vm_gic v; int ret, i; uint64_t addr; - v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, 1, vcpus); + v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, 1); /* Set space for 3 redists, we have 1 vcpu, so this succeeds. */ addr = max_phys_size - (3 * 2 * 0x10000); @@ -575,6 +575,7 @@ static void test_v3_redist_ipa_range_check_at_vcpu_run(void) kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, KVM_VGIC_V3_ADDR_TYPE_DIST, &addr); + vcpus = v.vm->vcpus; /* Add the rest of the VCPUs */ for (i = 1; i < NR_VCPUS; ++i) vcpus[i] = vm_vcpu_add(v.vm, i, guest_code); @@ -592,12 +593,11 @@ static void test_v3_redist_ipa_range_check_at_vcpu_run(void) static void test_v3_its_region(void) { - struct kvm_vcpu *vcpus[NR_VCPUS]; struct vm_gic v; uint64_t addr; int its_fd, ret; - v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS, vcpus); + v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS); its_fd = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_ITS); addr = 0x401000; @@ -637,12 +637,11 @@ static void test_v3_its_region(void) */ int test_kvm_device(uint32_t gic_dev_type) { - struct kvm_vcpu *vcpus[NR_VCPUS]; struct vm_gic v; uint32_t other; int ret; - v.vm = vm_create_with_vcpus(NR_VCPUS, guest_code, vcpus); + v.vm = vm_create_with_vcpus(NR_VCPUS, guest_code, NULL); /* try to create a non existing KVM device */ ret = __kvm_test_create_device(v.vm, 0); From patchwork Mon Oct 24 11:34:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8391 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp394803wru; Mon, 24 Oct 2022 04:41:03 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5mHiY3RylGTxK8nOqkxtgRDZXKKbAJDgbxXXFQlrwrdaDlXo9KjI6JyD/ke5C3pG4wjz61 X-Received: by 2002:a17:906:4a09:b0:78d:b6bb:ff5f with SMTP id w9-20020a1709064a0900b0078db6bbff5fmr26107308eju.538.1666611663601; Mon, 24 Oct 2022 04:41:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611663; cv=none; d=google.com; s=arc-20160816; b=cg+HU++BIs237WwPExUXz08ji2tNXNp5y6BswUTfs0/H9CVa4eJ60yIhy0DnsEd6qn HhyBOckHmOI6H+Y8Ob2qramas1Thd8aaPqPuosJJ2jqerj4yeF8pxfIj1zuOWZFHd/Lu JsYgNwahMYHWYIDnEcUg4/zBeo7GKKuYQvjv0ctV/AOZGw+pxGvxyQ9rgfAMbnH9qcmu LKmhTbDuaxVdPL4IFpRzrvyyOO4QjiZongVUGLfLkp8LKhfoYzrwTsyZ60gw4t+1ywOj mgVHvWA6nqr2622A7DJAu7kbS0UgIgVZzkBweLKDuqktP3GhXO4VHkkCl6Di7+FajhUY ZFeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Gol6MPyCWQU+6r8W5HNxm00wItBU79rs6TogobByLTw=; b=AsgKg+XSBGZ38lxI6Wg4vdQoqqfzysSmzdSV8t51u0Lk4GjbaIne5OIMT4CRAeBp1i VlITDomIzk8qjuHpsLiRl1zk2m2mTN96LlFWvNiafxJjR9sMYdk8URdh+R61NgcO5SGS oun4WAAM1B/uTKHzZvijieALlBjb4CLDg4dfJWAet/SQ7ShIy3G8Zz+X/kLqqsaczkRO Z9IZ+6oXk0AnqMLwfCn3tIFMggCAsp5Cl4mP50vWiS0b5DdYAlijI24c1XfJmNn/SYMR AKw+ejmJPsmgZCqrhqKUwlEhcoBJXS+u+hwMr/LKJHJpXm6iARzDSkYI7XHIKxy4z3xF 1eNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ge07G5Y9; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t4-20020a056402020400b00458b42ff246si23959252edv.236.2022.10.24.04.40.39; Mon, 24 Oct 2022 04:41:03 -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=@intel.com header.s=Intel header.b=ge07G5Y9; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231191AbiJXLjl (ORCPT + 99 others); Mon, 24 Oct 2022 07:39:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231153AbiJXLiq (ORCPT ); Mon, 24 Oct 2022 07:38:46 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B47B6C751; Mon, 24 Oct 2022 04:38:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611486; x=1698147486; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qLJRZLD+f1gvO5Oh214ugWG3QwbmcspGXqq4LSrfK9k=; b=ge07G5Y993pPQWLKVK7sWIbDJ7y4EqEa0TTQ2C9DJ+vzouz7yRSK6Z1B SpVvLUmUu3YVMto16lye7T6iIUSAiGsQLtBbe2BhYz9UaU1xWT++XVmIi RaN/kskWJr8KpedrxG7TSoOs16Dg9HTbv348rKT12Lzlj0u6HQBbOV9Fl 9RojqJ9praESpA81Z+fjFUIwWUwr7JY0n66NqekWs/L+6BQn0AH0C3bNa Y4sWFBRLBaU7F6Z+Tna3ddhFsdfpNJMgFh4vz6/M6MFGqoXPF9NE51QDv CgQ8azQFK4CtfJ7tYjp9/YBdqX4uY0nbY2NGofyJSwAl5DRc0MOhlu1m1 g==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="369462488" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="369462488" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:35:29 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773785071" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773785071" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:35:27 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 16/18] KVM: selftest/arch_timer: vcpu related code consolidation Date: Mon, 24 Oct 2022 19:34:43 +0800 Message-Id: <20221024113445.1022147-17-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747568991600214284?= X-GMAIL-MSGID: =?utf-8?q?1747568991600214284?= Remove the globally defined vcpu and pthread arrays, and reuse the one from kvm_vm and kvm_vcpu. Also use the helper functions to create vcpu threads with name. Signed-off-by: Wei Wang --- .../selftests/kvm/aarch64/arch_timer.c | 42 +++++++------------ 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/tools/testing/selftests/kvm/aarch64/arch_timer.c b/tools/testing/selftests/kvm/aarch64/arch_timer.c index 574eb73f0e90..7c1057e8fca7 100644 --- a/tools/testing/selftests/kvm/aarch64/arch_timer.c +++ b/tools/testing/selftests/kvm/aarch64/arch_timer.c @@ -23,7 +23,6 @@ #define _GNU_SOURCE #include -#include #include #include #include @@ -76,8 +75,6 @@ struct test_vcpu_shared_data { uint64_t xcnt; }; -static struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; -static pthread_t pt_vcpu_run[KVM_MAX_VCPUS]; static struct test_vcpu_shared_data vcpu_shared_data[KVM_MAX_VCPUS]; static int vtimer_irq, ptimer_irq; @@ -212,7 +209,8 @@ static void guest_code(void) static void *test_vcpu_run(void *arg) { - unsigned int vcpu_idx = (unsigned long)arg; + struct kvm_vcpu *vcpu = (struct kvm_vcpu *)arg; + unsigned int vcpu_idx = vcpu->id; struct ucall uc; struct kvm_vcpu *vcpu = vcpus[vcpu_idx]; struct kvm_vm *vm = vcpu->vm; @@ -263,18 +261,19 @@ static uint32_t test_get_pcpu(void) return pcpu; } -static int test_migrate_vcpu(unsigned int vcpu_idx) +static int test_migrate_vcpu(struct kvm_vcpu *vcpu) { int ret; cpu_set_t cpuset; uint32_t new_pcpu = test_get_pcpu(); + uint32_t vcpu_idx = vcpu->id; CPU_ZERO(&cpuset); CPU_SET(new_pcpu, &cpuset); pr_debug("Migrating vCPU: %u to pCPU: %u\n", vcpu_idx, new_pcpu); - ret = pthread_setaffinity_np(pt_vcpu_run[vcpu_idx], + ret = pthread_setaffinity_np(vcpu->thread, sizeof(cpuset), &cpuset); /* Allow the error where the vCPU thread is already finished */ @@ -287,6 +286,7 @@ static int test_migrate_vcpu(unsigned int vcpu_idx) static void *test_vcpu_migration(void *arg) { + struct kvm_vm *vm = (struct kvm_vm *)arg; unsigned int i, n_done; bool vcpu_done; @@ -303,7 +303,7 @@ static void *test_vcpu_migration(void *arg) continue; } - test_migrate_vcpu(i); + test_migrate_vcpu(vm->vcpus[i]); } } while (test_args.nr_vcpus != n_done); @@ -314,31 +314,21 @@ static void test_run(struct kvm_vm *vm) { pthread_t pt_vcpu_migration; unsigned int i; - int ret; pthread_mutex_init(&vcpu_done_map_lock, NULL); vcpu_done_map = bitmap_zalloc(test_args.nr_vcpus); TEST_ASSERT(vcpu_done_map, "Failed to allocate vcpu done bitmap\n"); - for (i = 0; i < (unsigned long)test_args.nr_vcpus; i++) { - ret = pthread_create(&pt_vcpu_run[i], NULL, test_vcpu_run, - (void *)(unsigned long)i); - TEST_ASSERT(!ret, "Failed to create vCPU-%d pthread\n", i); - } + vm_vcpu_threads_create(vm, test_vcpu_run, 0); /* Spawn a thread to control the vCPU migrations */ if (test_args.migration_freq_ms) { srand(time(NULL)); - - ret = pthread_create(&pt_vcpu_migration, NULL, - test_vcpu_migration, NULL); - TEST_ASSERT(!ret, "Failed to create the migration pthread\n"); + pthread_create_with_name(&pt_vcpu_migration, + test_vcpu_migration, vm, "control-thread"); } - - for (i = 0; i < test_args.nr_vcpus; i++) - pthread_join(pt_vcpu_run[i], NULL); - + vm_vcpu_threads_join(vm); if (test_args.migration_freq_ms) pthread_join(pt_vcpu_migration, NULL); @@ -364,16 +354,16 @@ static int gic_fd; static struct kvm_vm *test_vm_create(void) { struct kvm_vm *vm; - unsigned int i; - int nr_vcpus = test_args.nr_vcpus; + struct kvm_vcpu *vcpu; + int i, nr_vcpus = test_args.nr_vcpus; - vm = vm_create_with_vcpus(nr_vcpus, guest_code, vcpus); + vm = vm_create_with_vcpus(nr_vcpus, guest_code, NULL); vm_init_descriptor_tables(vm); vm_install_exception_handler(vm, VECTOR_IRQ_CURRENT, guest_irq_handler); - for (i = 0; i < nr_vcpus; i++) - vcpu_init_descriptor_tables(vcpus[i]); + vm_iterate_over_cpus(vm, vcpu, i) + vcpu_init_descriptor_tables(vcpu); ucall_init(vm, NULL); test_init_timer_irq(vm); From patchwork Mon Oct 24 11:34:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8387 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp394611wru; Mon, 24 Oct 2022 04:40:34 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5bdEtm68Rx/BWlfCJWw17BV6jFWu87ENVC5vfOcCkzHCLWOaakhnI0fsbs86aO1VJ2rQxe X-Received: by 2002:a17:907:7203:b0:7a5:b062:2338 with SMTP id dr3-20020a170907720300b007a5b0622338mr5957090ejc.8.1666611634500; Mon, 24 Oct 2022 04:40:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611634; cv=none; d=google.com; s=arc-20160816; b=y3G8+TdRDnzHJxTbN9m0SF+a0zgK70BP+0RU07f6BZcCwZbmHjNUTvhwEvdy/vtx78 H56jaZRcbPMfc8BLROdFmj4YL+4BcwcuMevdbamyo/o+UUQLtDqbhuMUjsPwev4V1nzn KVenI6ueeaMY0g5xGT/IwTkuFNvU2D6ON1FlnF3UrW58MkqWu9uT7CZqecYFw5Qa0OCc ORMu+YqDeQfu5PgFmOeUunbT6/2+lEa6A0Or8//lVHYtZPg4M38WuA5hEwXTnGAtnuzS 1kSKqeSSzOAYen0C6C3/H5KCPbvCZynwOgmL5YpRm8B+gTYtxixFRUzmTL1n4xnn+tD7 vwfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=/b6DYAu5QeSEHp5vYswS9UCNWikkoDWKlYpLjdlao2I=; b=GN/7TOwmE5EAOUFPiSzQC4turD00iRPhMtawhwpxGE0E2JZnP2CCRiN3YB78cOU8CG yLgjFjPv4M1hhrbgxLI54dPeLAF/7aHwus60un0pYNqkaUJqe36FGnlB2EleWIdkOfe9 qwOFUD1nvXAVwH7ykKX9lHmIsqsf8wCtbnguO4AKZ6jAbI56EE0D4ab1walpDqizZ/BG yLzdqR+3aji48M23vkXOzxAwo1Zj2EQ2vtyVsDDPIOXJ7l6wDpzYE+5hnOTmSAEfrVYO ZBUwS3Gj8fVkaXSAIpSTiBKvmM4SpPGWjCzUGOKh9HSeZ7n3zYmaEpemFFliwDjA0vAC 1XBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QngsEM0i; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j25-20020aa7c419000000b0046199bc9130si4056062edq.467.2022.10.24.04.40.10; Mon, 24 Oct 2022 04:40:34 -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=@intel.com header.s=Intel header.b=QngsEM0i; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231154AbiJXLjg (ORCPT + 99 others); Mon, 24 Oct 2022 07:39:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231223AbiJXLij (ORCPT ); Mon, 24 Oct 2022 07:38:39 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 056D16B65E; Mon, 24 Oct 2022 04:38:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611491; x=1698147491; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=VpBbmMaQb192ltdQ0fPh97UAauMJ1f/HXW0McbVeszM=; b=QngsEM0in05SLg3iZlwtTNP3B0no2GqcG+DUdUci2dM67m2jJMOsUdIZ KN4omqXcksJ5bJWrCpJ7/+zlNXdP4CJ0E5h52uYH/ReBD4Iwpnfap7Unp qW3SGmHb1C+sxMp1OQhZG0ktE1kFPK9NMdR+gDXbt/Zo5/mztgm5regS8 He3mO4s1b1nSBEh78WYCG7dT6Uvh+RcAPE3Uy7326PzL6Q7CYUqZFs7bU 4b3oOeHGaPGsmjvPObHvgHONh5oeFB4MY8clHQjwLvmAM2MN9/OvMf3d3 bY6U78MqIKkhUPDSgQ52rKnAU5p01wAEaH9yjWyF2MIeP60dYOmL5+T24 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="369462498" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="369462498" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:35:33 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773785079" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773785079" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:35:30 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 17/18] KVM: selftests: remove the *vcpu[] input from __vm_create_with_vcpus Date: Mon, 24 Oct 2022 19:34:44 +0800 Message-Id: <20221024113445.1022147-18-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747568961452686252?= X-GMAIL-MSGID: =?utf-8?q?1747568961452686252?= kvm_vm has included an array of vcpu pointers (i.e. *vcpu[]) to the added vcpus, so there is no need for users to supply its own *vcpu[]. Remove the *vcpu[] from __vm_create_with_vcpus and the related callers. Signed-off-by: Wei Wang --- tools/testing/selftests/kvm/aarch64/arch_timer.c | 2 +- tools/testing/selftests/kvm/aarch64/vgic_init.c | 4 ++-- tools/testing/selftests/kvm/hardware_disable_test.c | 2 +- tools/testing/selftests/kvm/include/kvm_util_base.h | 7 +++---- tools/testing/selftests/kvm/kvm_page_table_test.c | 2 +- tools/testing/selftests/kvm/lib/kvm_util.c | 4 ++-- tools/testing/selftests/kvm/lib/perf_test_util.c | 2 +- tools/testing/selftests/kvm/max_guest_memory_test.c | 2 +- tools/testing/selftests/kvm/steal_time.c | 2 +- tools/testing/selftests/kvm/x86_64/tsc_scaling_sync.c | 2 +- 10 files changed, 14 insertions(+), 15 deletions(-) diff --git a/tools/testing/selftests/kvm/aarch64/arch_timer.c b/tools/testing/selftests/kvm/aarch64/arch_timer.c index 7c1057e8fca7..1373e41ef365 100644 --- a/tools/testing/selftests/kvm/aarch64/arch_timer.c +++ b/tools/testing/selftests/kvm/aarch64/arch_timer.c @@ -357,7 +357,7 @@ static struct kvm_vm *test_vm_create(void) struct kvm_vcpu *vcpu; int i, nr_vcpus = test_args.nr_vcpus; - vm = vm_create_with_vcpus(nr_vcpus, guest_code, NULL); + vm = vm_create_with_vcpus(nr_vcpus, guest_code); vm_init_descriptor_tables(vm); vm_install_exception_handler(vm, VECTOR_IRQ_CURRENT, guest_irq_handler); diff --git a/tools/testing/selftests/kvm/aarch64/vgic_init.c b/tools/testing/selftests/kvm/aarch64/vgic_init.c index e24130a49581..b5defd94dd2e 100644 --- a/tools/testing/selftests/kvm/aarch64/vgic_init.c +++ b/tools/testing/selftests/kvm/aarch64/vgic_init.c @@ -79,7 +79,7 @@ static struct vm_gic vm_gic_create_with_vcpus(uint32_t gic_dev_type, struct vm_gic v; v.gic_dev_type = gic_dev_type; - v.vm = vm_create_with_vcpus(nr_vcpus, guest_code, NULL); + v.vm = vm_create_with_vcpus(nr_vcpus, guest_code); v.gic_fd = kvm_create_device(v.vm, gic_dev_type); return v; @@ -641,7 +641,7 @@ int test_kvm_device(uint32_t gic_dev_type) uint32_t other; int ret; - v.vm = vm_create_with_vcpus(NR_VCPUS, guest_code, NULL); + v.vm = vm_create_with_vcpus(NR_VCPUS, guest_code); /* try to create a non existing KVM device */ ret = __kvm_test_create_device(v.vm, 0); diff --git a/tools/testing/selftests/kvm/hardware_disable_test.c b/tools/testing/selftests/kvm/hardware_disable_test.c index c212d34a6714..f16e07485380 100644 --- a/tools/testing/selftests/kvm/hardware_disable_test.c +++ b/tools/testing/selftests/kvm/hardware_disable_test.c @@ -76,7 +76,7 @@ static void run_test(uint32_t run) r = pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpu_set); TEST_ASSERT(!r, "%s: failed to set affinity, r = %d", __func__, r); - vm = vm_create_with_vcpus(VCPU_NUM, guest_code, NULL); + vm = vm_create_with_vcpus(VCPU_NUM, guest_code); pr_debug("%s: [%d] start vcpus\n", __func__, run); vm_iterate_over_vcpus(vm, vcpu, i) { diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index d0d6aaec0098..5a5b7210cf7c 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -663,14 +663,13 @@ static inline struct kvm_vm *vm_create(uint32_t nr_runnable_vcpus) struct kvm_vm *__vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus, uint64_t extra_mem_pages, - void *guest_code, struct kvm_vcpu *vcpus[]); + void *guest_code); static inline struct kvm_vm *vm_create_with_vcpus(uint32_t nr_vcpus, - void *guest_code, - struct kvm_vcpu *vcpus[]) + void *guest_code) { return __vm_create_with_vcpus(VM_MODE_DEFAULT, nr_vcpus, 0, - guest_code, vcpus); + guest_code); } /* diff --git a/tools/testing/selftests/kvm/kvm_page_table_test.c b/tools/testing/selftests/kvm/kvm_page_table_test.c index 4c3df48d80fc..1a9dd189c225 100644 --- a/tools/testing/selftests/kvm/kvm_page_table_test.c +++ b/tools/testing/selftests/kvm/kvm_page_table_test.c @@ -253,7 +253,7 @@ static struct kvm_vm *pre_init_before_test(enum vm_guest_mode mode, void *arg) /* Create a VM with enough guest pages */ guest_num_pages = test_mem_size / guest_page_size; vm = __vm_create_with_vcpus(mode, nr_vcpus, guest_num_pages, - guest_code, NULL); + guest_code); /* Align down GPA of the testing memslot */ if (!p->phys_offset) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index ba3e774087fb..69dad4fa9ca1 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -366,7 +366,7 @@ struct kvm_vm *__vm_create(enum vm_guest_mode mode, uint32_t nr_runnable_vcpus, */ struct kvm_vm *__vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus, uint64_t extra_mem_pages, - void *guest_code, struct kvm_vcpu *vcpus[]) + void *guest_code) { struct kvm_vm *vm; int i; @@ -386,7 +386,7 @@ struct kvm_vm *__vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, struct kvm_vm *vm; vm = __vm_create_with_vcpus(VM_MODE_DEFAULT, 1, extra_mem_pages, - guest_code, NULL); + guest_code); *vcpu = vm->vcpus[0]; return vm; diff --git a/tools/testing/selftests/kvm/lib/perf_test_util.c b/tools/testing/selftests/kvm/lib/perf_test_util.c index 94c0f496c9c1..3103c9f40e76 100644 --- a/tools/testing/selftests/kvm/lib/perf_test_util.c +++ b/tools/testing/selftests/kvm/lib/perf_test_util.c @@ -140,7 +140,7 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int nr_vcpus, * effect as KVM allows aliasing HVAs in meslots. */ vm = __vm_create_with_vcpus(mode, nr_vcpus, slot0_pages + guest_num_pages, - perf_test_guest_code, NULL); + perf_test_guest_code); pta->vm = vm; diff --git a/tools/testing/selftests/kvm/max_guest_memory_test.c b/tools/testing/selftests/kvm/max_guest_memory_test.c index 2d9c83e36e65..7480730caeeb 100644 --- a/tools/testing/selftests/kvm/max_guest_memory_test.c +++ b/tools/testing/selftests/kvm/max_guest_memory_test.c @@ -205,7 +205,7 @@ int main(int argc, char *argv[]) } } - vm = vm_create_with_vcpus(nr_vcpus, guest_code, NULL); + vm = vm_create_with_vcpus(nr_vcpus, guest_code); max_gpa = vm->max_gfn << vm->page_shift; TEST_ASSERT(max_gpa > (4 * slot_size), "MAXPHYADDR <4gb "); diff --git a/tools/testing/selftests/kvm/steal_time.c b/tools/testing/selftests/kvm/steal_time.c index 857ed2c073fc..530b08e64846 100644 --- a/tools/testing/selftests/kvm/steal_time.c +++ b/tools/testing/selftests/kvm/steal_time.c @@ -261,7 +261,7 @@ int main(int ac, char **av) pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); /* Create a VM and an identity mapped memslot for the steal time structure */ - vm = vm_create_with_vcpus(NR_VCPUS, guest_code, NULL); + vm = vm_create_with_vcpus(NR_VCPUS, guest_code); vcpus = vm->vcpus; gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, STEAL_TIME_SIZE * NR_VCPUS); vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, gpages, 0); diff --git a/tools/testing/selftests/kvm/x86_64/tsc_scaling_sync.c b/tools/testing/selftests/kvm/x86_64/tsc_scaling_sync.c index 34a8beef42b6..3c050ffe5edb 100644 --- a/tools/testing/selftests/kvm/x86_64/tsc_scaling_sync.c +++ b/tools/testing/selftests/kvm/x86_64/tsc_scaling_sync.c @@ -91,7 +91,7 @@ int main(int argc, char *argv[]) { TEST_REQUIRE(kvm_has_cap(KVM_CAP_VM_TSC_CONTROL)); - vm = vm_create_with_vcpus(NR_TEST_VCPUS, guest_code, NULL); + vm = vm_create_with_vcpus(NR_TEST_VCPUS, guest_code); vm_ioctl(vm, KVM_SET_TSC_KHZ, (void *) TEST_TSC_KHZ); pthread_spin_init(&create_lock, PTHREAD_PROCESS_PRIVATE); From patchwork Mon Oct 24 11:34:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 8392 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp394866wru; Mon, 24 Oct 2022 04:41:12 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5IYz5EYCLaAPtDJ58lIcFcsm4+HvvpgiWLw78wwH4RvKz8MbAg1LZ1w5otT2hxVvGW9WfL X-Received: by 2002:a17:906:8a47:b0:7a5:8160:19a6 with SMTP id gx7-20020a1709068a4700b007a5816019a6mr6092934ejc.477.1666611671932; Mon, 24 Oct 2022 04:41:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666611671; cv=none; d=google.com; s=arc-20160816; b=yA2HqcT3JoObDFPi5OrFhTmu5UgV/fSIGeyLyxdop08CRjIDUVb2Tt9u4GdjX1iiQd yzZQ0B+UCepS6JdzxSrnNsSrbunhlabDc8y02x/1zQhP0dYF+gwVYr25+227NJpJVXYp H9s7LPzajb14wIV9YIeUIQ+/PDfnJuXVVDRaF/nVrZJGzJYXDnQ7SeZQffXI5zp+uTYH 5+8hIUK2HnbGcdRM+jWnqEgJVgAnJPbE2TOueHgNQ8PKCBUjlL1KqaY78wwBCVBs4OVc O1mDc7yHAkBXraTsSDbXaSLEI4iW2habMPechCt3iDcsl8zgjAYFpAi+Q//gesR+hpJ+ 13cQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=RFF0ZkMzxjnk+P7udhWyIbAR55rPVMyNUTVhr9cQdpE=; b=er2xLofxyPfpXJt+ek8DW9B4KDkzMnY03uuLfiWlagazgG2sqk0u9btSy5BYfo9/lg KFz7NsPJKHyuVvO08l/wHtq/gVc22OK9N6XQGt4cicPHtrd4PAcJ+KeGw4QTDp+lnScy QkHK7RAmbsKzhgediDRYlOTnOQ3YMTJt61YfPsLvUXLYvTgsBjkltuarf0EgUBAFa/MF ATw5h+cVGqUVLcnBmVSRTcVTV9HhvtBn/WnOXa4bKjVxC6GUGs4CHGZvfpdVG7SnFyig T4eYv6JRHuCBVbVtKNy6Vowaq+z5bYjSVhIBzosC0QmtLqFVVvJ162aCMrlNv1vERPMe AMTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=FWgR9JmZ; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r15-20020a17090638cf00b0078e11e92257si23315125ejd.333.2022.10.24.04.40.47; Mon, 24 Oct 2022 04:41:11 -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=@intel.com header.s=Intel header.b=FWgR9JmZ; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231338AbiJXLkI (ORCPT + 99 others); Mon, 24 Oct 2022 07:40:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231289AbiJXLi7 (ORCPT ); Mon, 24 Oct 2022 07:38:59 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65C496D877; Mon, 24 Oct 2022 04:38:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666611494; x=1698147494; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=V5CMqFlWnS5AeCB0CE61Ephj3ol/+ZP60Gy/5sHQJ6E=; b=FWgR9JmZdq3Jga8/AXjnwgFwNWgipP9yWdJnBRqqDmLizCdgaMOWNGIJ fuKbFlT77/El+F83NAwRR5JvcfY0LMjyFUFIv1yPTioU4eYCa+tTyxENk KphQaczZSDbkN4ElsXn5DTzmpggUob7YDB7+Ds8ux6PTrTGHhp8QkHnHh y1NY56PdiCdTkM/WBTfUKCBQ365G72ffgs+Bk6YWd5JH8o+qOnaEssY0p WRn/Blmw2Jqkk5rvZpRoV8CLbwdXdP5dY990fSSsONh+jdz9OfD7wvBv+ Mv4+E5/sU3vL5bMChBAiRxwN67a6gR3B5sq0LotOLSmp+AiRtxfsXDCvp A==; X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="369462511" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="369462511" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2022 04:35:34 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10509"; a="773785085" X-IronPort-AV: E=Sophos;i="5.95,209,1661842800"; d="scan'208";a="773785085" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2022 04:35:32 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com Cc: dmatlack@google.com, vipinsh@google.com, ajones@ventanamicro.com, eric.auger@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1 18/18] KVM: selftests/kvm_create_max_vcpus: check KVM_MAX_VCPUS Date: Mon, 24 Oct 2022 19:34:45 +0800 Message-Id: <20221024113445.1022147-19-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221024113445.1022147-1-wei.w.wang@intel.com> References: <20221024113445.1022147-1-wei.w.wang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747569000496205106?= X-GMAIL-MSGID: =?utf-8?q?1747569000496205106?= If the KVM side max vcpu number is larger than the one supported by the userspace selftests, adjust the max number. Signed-off-by: Wei Wang --- tools/testing/selftests/kvm/kvm_create_max_vcpus.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/testing/selftests/kvm/kvm_create_max_vcpus.c b/tools/testing/selftests/kvm/kvm_create_max_vcpus.c index 31b3cb24b9a7..bbdb371e21ed 100644 --- a/tools/testing/selftests/kvm/kvm_create_max_vcpus.c +++ b/tools/testing/selftests/kvm/kvm_create_max_vcpus.c @@ -50,6 +50,13 @@ int main(int argc, char *argv[]) pr_info("KVM_CAP_MAX_VCPU_ID: %d\n", kvm_max_vcpu_id); pr_info("KVM_CAP_MAX_VCPUS: %d\n", kvm_max_vcpus); + pr_info("selftests KVM_MAX_VCPUS: %d\n", KVM_MAX_VCPUS); + + if (kvm_max_vcpu_id > KVM_MAX_VCPUS) + kvm_max_vcpu_id = KVM_MAX_VCPUS; + + if (kvm_max_vcpus > KVM_MAX_VCPUS) + kvm_max_vcpus = KVM_MAX_VCPUS; /* * Check that we're allowed to open nr_fds_wanted file descriptors and