Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2771446pxb; Sun, 3 Apr 2022 20:40:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwh7R0BaVzbI/OHsYv33QgEtwDj8SZLdNa4hRCzcNRDmdLy9U84XK9T7DQOFLmIrrIQ4jDs X-Received: by 2002:a05:6402:492:b0:404:c4bf:8b7e with SMTP id k18-20020a056402049200b00404c4bf8b7emr30224587edv.318.1649043614404; Sun, 03 Apr 2022 20:40:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649043614; cv=none; d=google.com; s=arc-20160816; b=uavXIi0wXYtj3R0sIUe3l1WtMpu/AvF7aIcnVq8buy5sE6ZIjqM6MShkm0U0vQcyZN NjudidCW9sN1e+fo9PwJiToUHytSXYp8FLTHQ2gfB9U9zLyVeLmJpDeod1rApM/uhtOy CqTi+UqHVCtIWD3LmYs3i5mSE40UJIupwFWbfPAgW/H17caWquqSlxnYOwtqA0ACqgkD uURHRcKdugGG1hXiNkpaO65rmR2Wy7Gj4dUc9WnE8egwywcd9rj/U0DRMjaVkH+mRLoX Ct1zhW4y0ql60lJXwL2vn1TQRKF05yxgRfpE8/07RrQsNPVNcWTabKRimfdBAGTP2dFG lAHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=H/Y/QCdrF+n/HHb09pcVcuLkQKZqKBJKsx5yGAuMqwA=; b=xvSAjpA0Ur61mVL3j0jS093Z9veqpPj5aE32CbyPCW1h/OSJ7RtP1bMtrVycpg3Mns eKcwNAOuS7CPePqqkMlPismTBlH15Ux3Ozw1igaIB+ZtuVo7gZOi8EMFsh9PFexa4TCj Iiq6e9CQ6CDNcR/vqwHojtmtWQEvA1K1SiZL52qNZbxZhfdc+V61JMcmv9hp+98vOZhC MRXRv0dKkil2wsP5HZEYdetdra0O09yNib3W7cJNx+CdIF2oMzIQguE5pkr5HU231g/4 M4jPuIMBtiYzeOFFEftwFdJvaWtOpbjHwS2qKsBo+qfi/3P+8oSu3PmBmdvf4NKNZaLW P3nA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=LiRedP4f; 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 s7-20020a056402520700b00418c2b5be11si6644698edd.243.2022.04.03.20.39.40; Sun, 03 Apr 2022 20: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=LiRedP4f; 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 S1355501AbiDBNgI (ORCPT + 99 others); Sat, 2 Apr 2022 09:36:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230522AbiDBNgG (ORCPT ); Sat, 2 Apr 2022 09:36:06 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CA5813F4A; Sat, 2 Apr 2022 06:34:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1648906455; x=1680442455; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=qgScYmdRz1TKabV+htsOCqYKdWAFLBIHTuv08g3bPCM=; b=LiRedP4fW9rtOAzL50HuuK2ngs06/cBgAdtu/Fyyn6pGRedQvSiB9aCQ xp0uruW9vwsEmjnWy59raJ//VH8RpShIei/AazX28TZ5Dva/J9FyYtgFI AA0MVezOj2xNMYOAuE2XWPzTZZ12mlrZWyKgniaau2GTibEW/ofyLn0gn YclbxXowA3UxYWo4jHVqJmGhtjfKcgXCqhAII6qAiHiCpNFdZ0vB5uMPb jYtAIAeHShZDsb3SGYOHmBT5+474gE62OwYb+oqotZxeoWeqdfMNKW/CR I8IQkbCv1zIullBx9dbhqm6vvKFERHXhvDWyHdzYeik0YaRUQIXD3SmqI A==; X-IronPort-AV: E=McAfee;i="6200,9189,10304"; a="285240650" X-IronPort-AV: E=Sophos;i="5.90,230,1643702400"; d="scan'208";a="285240650" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Apr 2022 06:34:14 -0700 X-IronPort-AV: E=Sophos;i="5.90,230,1643702400"; d="scan'208";a="548138059" Received: from zengguan-mobl1.ccr.corp.intel.com (HELO [10.254.208.38]) ([10.254.208.38]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Apr 2022 06:34:09 -0700 Message-ID: Date: Sat, 2 Apr 2022 21:33:59 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: [PATCH v7 5/8] KVM: x86: Add support for vICR APIC-write VM-Exits in x2APIC mode Content-Language: en-US To: Sean Christopherson Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , "kvm@vger.kernel.org" , Dave Hansen , "Luck, Tony" , Kan Liang , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Kim Phillips , Jarkko Sakkinen , Jethro Beekman , "Huang, Kai" , "x86@kernel.org" , "linux-kernel@vger.kernel.org" , "Hu, Robert" , "Gao, Chao" References: <20220304080725.18135-1-guang.zeng@intel.com> <20220304080725.18135-6-guang.zeng@intel.com> From: Zeng Guang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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,NICE_REPLY_A, RCVD_IN_DNSWL_HI,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 4/1/2022 7:07 AM, Sean Christopherson wrote: > On Fri, Mar 04, 2022, Zeng Guang wrote: >> Upcoming Intel CPUs will support virtual x2APIC MSR writes to the vICR, >> i.e. will trap and generate an APIC-write VM-Exit instead of intercepting >> the WRMSR. Add support for handling "nodecode" x2APIC writes, which >> were previously impossible. >> >> Note, x2APIC MSR writes are 64 bits wide. >> >> Signed-off-by: Zeng Guang >> --- >> arch/x86/kvm/lapic.c | 22 +++++++++++++++++++--- >> 1 file changed, 19 insertions(+), 3 deletions(-) >> >> diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c >> index 629c116b0d3e..22929b5b3f9b 100644 >> --- a/arch/x86/kvm/lapic.c >> +++ b/arch/x86/kvm/lapic.c >> @@ -67,6 +67,7 @@ static bool lapic_timer_advance_dynamic __read_mostly; >> #define LAPIC_TIMER_ADVANCE_NS_MAX 5000 >> /* step-by-step approximation to mitigate fluctuation */ >> #define LAPIC_TIMER_ADVANCE_ADJUST_STEP 8 >> +static int kvm_lapic_msr_read(struct kvm_lapic *apic, u32 reg, u64 *data); >> >> static inline void __kvm_lapic_set_reg(char *regs, int reg_off, u32 val) >> { >> @@ -2227,10 +2228,25 @@ EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi); >> /* emulate APIC access in a trap manner */ >> void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset) >> { >> - u32 val = kvm_lapic_get_reg(vcpu->arch.apic, offset); >> + struct kvm_lapic *apic = vcpu->arch.apic; >> + u64 val; >> + >> + if (apic_x2apic_mode(apic)) { >> + /* >> + * When guest APIC is in x2APIC mode and IPI virtualization >> + * is enabled, accessing APIC_ICR may cause trap-like VM-exit >> + * on Intel hardware. Other offsets are not possible. >> + */ >> + if (WARN_ON_ONCE(offset != APIC_ICR)) >> + return; >> >> - /* TODO: optimize to just emulate side effect w/o one more write */ >> - kvm_lapic_reg_write(vcpu->arch.apic, offset, val); >> + kvm_lapic_msr_read(apic, offset, &val); >> + kvm_apic_send_ipi(apic, (u32)val, (u32)(val >> 32)); > This needs to clear the APIC_ICR_BUSY bit. It'd also be nice to trace this write. > The easiest thing is to use kvm_x2apic_icr_write(). Kinda silly as it'll generate > an extra write, but on the plus side the TODO comment doesn't have to move :-D > > diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c > index c4c3155d98db..58bf296ee313 100644 > --- a/arch/x86/kvm/lapic.c > +++ b/arch/x86/kvm/lapic.c > @@ -2230,6 +2230,7 @@ void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset) > struct kvm_lapic *apic = vcpu->arch.apic; > u64 val; > > + /* TODO: optimize to just emulate side effect w/o one more write */ > if (apic_x2apic_mode(apic)) { > /* > * When guest APIC is in x2APIC mode and IPI virtualization > @@ -2240,10 +2241,9 @@ void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset) > return; > > kvm_lapic_msr_read(apic, offset, &val); > - kvm_apic_send_ipi(apic, (u32)val, (u32)(val >> 32)); > + kvm_x2apic_icr_write(apic, val); As SDM section 10.12.9 "ICR Operation in X2APIC mode" says "Delivery status bit is removed since it is not needed in x2APIC mode" , so that's not necessary to clear the APIC_ICR_BUSY bit here. Alternatively we can add trace to this write by hardware. > } else { > val = kvm_lapic_get_reg(apic, offset); > - /* TODO: optimize to just emulate side effect w/o one more write */ > kvm_lapic_reg_write(apic, offset, (u32)val); > } > } > > >> + } else { >> + val = kvm_lapic_get_reg(apic, offset); >> + /* TODO: optimize to just emulate side effect w/o one more write */ >> + kvm_lapic_reg_write(apic, offset, (u32)val); >> + } >> } >> EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode); >> >> -- >> 2.27.0 >>