Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp1253701pxb; Fri, 1 Apr 2022 08:27:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyR2UCx/Zvx8djvmpySRBzfXmJ5nIHd2sVx4Tkyj4pfqwqm9Iy6bzFsmB+oW4ZRI7tiNclE X-Received: by 2002:a17:907:7da6:b0:6e0:5b7c:e3f9 with SMTP id oz38-20020a1709077da600b006e05b7ce3f9mr297877ejc.239.1648826879466; Fri, 01 Apr 2022 08:27:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648826879; cv=none; d=google.com; s=arc-20160816; b=N+ucXOavY+HG3j6XYxctOBiHXlSP1JEdMBpRN8dvQAGuaaujQSJgL0uCsKT4yI0wO6 bqk4y/00CmTgd+XgQ7XN5s6t1CKO9YwsMrBur0woixnuF0ZNzLQQQ2nYsBUO73Rkgw3L hiGfXMH/SFOKa92HC2FuJxfVcR/wnxq1M8Wjni/uSULAt4fFXqzqDvuwzz5aFJQKBc9w Lg+CIVLGaIFd2OrBCjrWVmo+0iV91GZuyoQGcyygpX3KeL9Zc1Buf4gs/dKEuyOQw0vC qTo6gXUGcAchkNiobYUYuoxiG3hH36CNj+qtVf9De/AUd18FCaCu11hZsr5PocxdmO4f WxYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=qRJmN8JDG7m72p3VxsXuKnTc88XHvYZJpC/RFCon9dM=; b=HxqccrbCybQOxThwGCCEkLdS/qrG9MUv7Kt6dQ4b4vjL3IPyUHByjislnjlUsIzrAl tZ2m9VInkHySCBQNYVuUwcB8j8qYQhlUM9j1I0Zp5Ui+DSRhZZ09PIbf3LTLMEEMy2tR DH1EH2HT89kH+1kZj5fLyEewdOFKaWticvNVGUUfdhhdssbq390mx57T9v0dczdPhJCV b1vEW2rD9Bdn8aPX3ICBmEXYLrpV6TXyGgU4mZaRPALeN+445Crnaf6czinqERKhJ943 LiWq09ym5KNpJhgrl4e1szK6pUWlUf2ooqqeu6O1YSEFUN+ICFWY6SSsLxS/uSwqEvQh dYhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=aMwVbgfW; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kz20-20020a17090777d400b006df7808f11fsi1736566ejc.913.2022.04.01.08.27.33; Fri, 01 Apr 2022 08:27:59 -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=@google.com header.s=20210112 header.b=aMwVbgfW; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242806AbiCaXJa (ORCPT + 99 others); Thu, 31 Mar 2022 19:09:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242801AbiCaXJ1 (ORCPT ); Thu, 31 Mar 2022 19:09:27 -0400 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C54124B5C9 for ; Thu, 31 Mar 2022 16:07:35 -0700 (PDT) Received: by mail-pj1-x102c.google.com with SMTP id mj15-20020a17090b368f00b001c637aa358eso3742075pjb.0 for ; Thu, 31 Mar 2022 16:07:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=qRJmN8JDG7m72p3VxsXuKnTc88XHvYZJpC/RFCon9dM=; b=aMwVbgfWGFLicig1gsSp/2jhSdYkiuTO1sd60UIlMKfjmpZpl5LkWpSarIu7YDUBU3 rpmb+z7cl8bVsE19t3bKrk/98bKoakg/taFBzWkzbIDxIrhfdDPDGXS3V+Vxe1xURGWj WvuJItBvoHNgb2dsWSoRjyDQPEqWuMfS+HSKacI4VjKVxZOhbhF5tnw4zTQlVzNT70vu 5QhsS41hLl//IjuCrBUqLumqai/xWHvj59KY2P8a6gCRhTmEtmSpKeph2yaiiH/rJ0Dt C7UsXmnOaK5uHBz8v+8ZT3DQyJxB9hIkbE9e/9Blb8rqFnm+zzLheVrYV4gTl5EtIqTU yMDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=qRJmN8JDG7m72p3VxsXuKnTc88XHvYZJpC/RFCon9dM=; b=FxGZb8IXLFdd9x71pBuT4XGB6rotsI7em9oP7ZE393exYUEhMgVaKbe7dYbib+Zyqp ycGp5MLwW06HNfO1L6Y/CGo9gGliwTI6C5ofHQV5wjjOuY1fbdLreJGbmcEjpCKJHq2P M9KpCACBbZdr69SX1bKgz2xeWIMDCi4y94VzEsmksSt9g8l1wakwvObeE7jxymdhWTgU bGaFbfopaAPoJgsU+9a3Bt3ycUUdqCHrQhpqJtLbsT9o+Q/IXsZr7EkMCWXBpzBwNc5h 2ijfgHANnYUvuSUVQIppxFtHsavJbZ34XyZ7geeukbWKd9W1D4/NndbA7QrOWd6b+bKH l8ug== X-Gm-Message-State: AOAM531p0ofulIhgyfxtOe9MPkGO+TPRgZPA3e4fopozwbChp/jA2Osy y6XgZYz70fQt/7ZuM3SfzM9PdQ== X-Received: by 2002:a17:903:124a:b0:154:c7a4:9374 with SMTP id u10-20020a170903124a00b00154c7a49374mr7673970plh.68.1648768054442; Thu, 31 Mar 2022 16:07:34 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id s10-20020a056a00178a00b004fda49fb25dsm552215pfg.9.2022.03.31.16.07.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Mar 2022 16:07:33 -0700 (PDT) Date: Thu, 31 Mar 2022 23:07:30 +0000 From: Sean Christopherson To: Zeng Guang Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, Dave Hansen , Tony Luck , Kan Liang , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Kim Phillips , Jarkko Sakkinen , Jethro Beekman , Kai Huang , x86@kernel.org, linux-kernel@vger.kernel.org, Robert Hu , Gao Chao Subject: Re: [PATCH v7 5/8] KVM: x86: Add support for vICR APIC-write VM-Exits in x2APIC mode Message-ID: References: <20220304080725.18135-1-guang.zeng@intel.com> <20220304080725.18135-6-guang.zeng@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220304080725.18135-6-guang.zeng@intel.com> X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 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); } 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 >