Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp6724896rwi; Mon, 24 Oct 2022 05:27:41 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5N8XIEKWKCw74aqgTLLuh5ndkXDhv4Q2fIads5Kge79q48P2vAmD8/gEZhbgCp8fk42XsX X-Received: by 2002:a62:7bc5:0:b0:56b:47ed:22a7 with SMTP id w188-20020a627bc5000000b0056b47ed22a7mr15219812pfc.63.1666614461104; Mon, 24 Oct 2022 05:27:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666614461; cv=none; d=google.com; s=arc-20160816; b=D6/ecNm8wEVGS4J+NkLCZItiUa2vxpPdRV6c+bSEFY1m92enDWr9XOzYTePB6GjneS hJpSFWT4NHNJIGy2VKB7Xw3mjuk7b0BosVYVyygp5cgki96mQNcwQ+v8vFaRCyhRzUeM JO/nSctJZl0bNZ6FwaOEczRte9tWvA00adWjCTdhCdd76j0xAaDuz47AKFo+QGaUSBzw gmeq+X3IqOph8Tkq7R7EBB0dhJrwFGGHhgKV4FGFnoCs6vyMCPK+HY0w9i4QXcZXNlBz JF/tlFixYb21itV0upwNcELVKXY9LTJuzrxdLtuvzg2MwafoY1Vmuq/mvFG38ZbSyLS+ l5hA== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u4-20020a170902e5c400b001846e0e0511si42742266plf.226.2022.10.24.05.27.29; Mon, 24 Oct 2022 05:27:41 -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 Content-Transfer-Encoding: 8bit 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 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); } -- 2.27.0