Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp376103rwe; Wed, 24 Aug 2022 02:31:00 -0700 (PDT) X-Google-Smtp-Source: AA6agR4pfOHNozrfWIDAoX7SuWKTPQzWyDy8u5Cyp/Qm9IhTSDkBkCsHDm7yRhUrGkuqY3Jurual X-Received: by 2002:a17:907:e8e:b0:73d:8f13:a014 with SMTP id ho14-20020a1709070e8e00b0073d8f13a014mr2392719ejc.525.1661333460171; Wed, 24 Aug 2022 02:31:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661333460; cv=none; d=google.com; s=arc-20160816; b=bOD5a5nN9QLynywtVnrhwIRwKbF0oNM5EoY+ivX22GSfZ+gi/HN/k/gCIkMJGWhJ0w RRJmWMKyxWJ9SYn7k7L12xeCOuD3dyTaLZisIj1ur4mXgNKnKU6145aSB4wLhrlICawY 2gUgoi6gTjTSqYZ3ktF8oB2psxewdP0his6bFWUyUScqMJqgqMK/9E7EVg4AqQ+s3HW/ wUk8bnOc18wKEXt4Uw+Sx3hqWh939hz7UUigJorblMh15kFq8GQFvL2CTQQaUMuMmib8 GtYJO4EzmgfvkVHzmXKd1dldDuWbAtbXXpj5nC7uhWsylIbLxVUm2AhRAcTeaaufQpDO lRSA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=jzU1myB/vdrfPpZrMWk7t7IIQzmVFfHlpO8qK+5C+0c=; b=qjajl4f+4Cb4Vj66d0g6oPGvgwE28Kj03nAh1PYuwQYEqoHK50ryRyxC23VBGxMn8A 1OfnaA+bKtR8Cm++SoEbnLaYyWR4PizdK/Ru50ixNC3IFWD6MIVgi1GbAJdae/Fl3Fai mHSmT/IxW2neQbP6SGQVb3pDZ+fwBy+3cOGkrZ2Q6fe0pNdsnCNVPT68yA5CFIgHCCnv ZC6CEzTb3qC79m3Phk4hF7kbWaPhMgu7EOtZRIZZD8juLzxUDlhKscQfUd4NxsHIoU74 qov8LT3KF47/H6T3ZS7wf2bGLWbV6pKjpyjFaY3uVJFTAygTuSSAkEpHeBTUn2J1cG1Q vBzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VKyfGn8H; 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 sd41-20020a1709076e2900b00730983c4646si1648700ejc.508.2022.08.24.02.30.33; Wed, 24 Aug 2022 02:31:00 -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=VKyfGn8H; 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 S231207AbiHXJGx (ORCPT + 99 others); Wed, 24 Aug 2022 05:06:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234588AbiHXJGv (ORCPT ); Wed, 24 Aug 2022 05:06:51 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE38B78227; Wed, 24 Aug 2022 02:06:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661332010; x=1692868010; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=1ivgFtNK8oJwUiWoMzbdW9yT/YWt2E72t+WodHgJScQ=; b=VKyfGn8HR4S7JDL8O/1u1He1vQtG1Ms48Tidoedq4ctmtT3+F0pE/nc/ 6PYOQvalALUu29nxROR0hcIRV5vXQfaT+H3zS6DpGN94puUl5JmmVSAWn S9WcMnF49o3/f1dCtXPWlnp2li1ipm8pR8x2d4IGsr5hVsR+PiWhZl4k+ Epe6Ip6o06rvM0KIaOWA0ILz9jYncKYDRd+P1GEp9Pp78NWmI5z4/lSJl 3qwc2iX6WaFXsULvLUCe5GzzQ0qxWkctgJHg2xF/BtCrUPmum1zsDBDFG iv+m7urcd3ni7HrshtzitnxuF0M7GsKmJSuujkO9C2c7CV4mMT/PwukXf Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10448"; a="319972877" X-IronPort-AV: E=Sophos;i="5.93,260,1654585200"; d="scan'208";a="319972877" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Aug 2022 02:06:50 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,260,1654585200"; d="scan'208";a="785560403" Received: from dmi-pnp-i7.sh.intel.com ([10.239.159.145]) by orsmga005.jf.intel.com with ESMTP; 24 Aug 2022 02:06:48 -0700 From: Dapeng Mi To: rafael@kernel.org, daniel.lezcano@linaro.org, pbonzini@redhat.com Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, zhenyuw@linux.intel.com, Dapeng Mi Subject: [PATCH] KVM: x86: use TPAUSE to replace PAUSE in halt polling Date: Wed, 24 Aug 2022 17:11:17 +0800 Message-Id: <20220824091117.767363-1-dapeng1.mi@intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 TPAUSE is a new instruction on Intel processors which can instruct processor enters a power/performance optimized state. Halt polling uses PAUSE instruction to wait vCPU is waked up. The polling time could be long and cause extra power consumption in some cases. Use TPAUSE to replace the PAUSE instruction in halt polling to get a better power saving and performance. Signed-off-by: Dapeng Mi --- drivers/cpuidle/poll_state.c | 3 ++- include/linux/kvm_host.h | 20 ++++++++++++++++++++ virt/kvm/kvm_main.c | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/drivers/cpuidle/poll_state.c b/drivers/cpuidle/poll_state.c index f7e83613ae94..51ec333cbf80 100644 --- a/drivers/cpuidle/poll_state.c +++ b/drivers/cpuidle/poll_state.c @@ -7,6 +7,7 @@ #include #include #include +#include #define POLL_IDLE_RELAX_COUNT 200 @@ -25,7 +26,7 @@ static int __cpuidle poll_idle(struct cpuidle_device *dev, limit = cpuidle_poll_time(drv, dev); while (!need_resched()) { - cpu_relax(); + kvm_cpu_poll_pause(limit); if (loop_count++ < POLL_IDLE_RELAX_COUNT) continue; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index f4519d3689e1..810e749949b7 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -2247,6 +2248,25 @@ static inline void kvm_handle_signal_exit(struct kvm_vcpu *vcpu) } #endif /* CONFIG_KVM_XFER_TO_GUEST_WORK */ +/* + * This function is intended to replace the cpu_relax function in + * halt polling. If TPAUSE instruction is supported, use TPAUSE + * instead fo PAUSE to get better power saving and performance. + * Selecting 1 us is a compromise between scheduling latency and + * power saving time. + */ +static inline void kvm_cpu_poll_pause(u64 timeout_ns) +{ +#ifdef CONFIG_X86 + if (static_cpu_has(X86_FEATURE_WAITPKG) && timeout_ns > 1000) + udelay(1); + else + cpu_relax(); +#else + cpu_relax(); +#endif +} + /* * This defines how many reserved entries we want to keep before we * kick the vcpu to the userspace to avoid dirty ring full. This diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 584a5bab3af3..4afa776d21bd 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3510,7 +3510,7 @@ void kvm_vcpu_halt(struct kvm_vcpu *vcpu) */ if (kvm_vcpu_check_block(vcpu) < 0) goto out; - cpu_relax(); + kvm_cpu_poll_pause(vcpu->halt_poll_ns); poll_end = cur = ktime_get(); } while (kvm_vcpu_can_poll(cur, stop)); } -- 2.34.1