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); }