Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp940051pxj; Fri, 21 May 2021 02:44:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyvRrL4fFhYFXg+5fqb8YFupCYESFOaLASmRgT1ZCIvEXWjbNEdYowUEUIwNr03xYLp/jHM X-Received: by 2002:a17:907:1c20:: with SMTP id nc32mr9388829ejc.105.1621590287016; Fri, 21 May 2021 02:44:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621590287; cv=none; d=google.com; s=arc-20160816; b=cp8J1KCCP6Z3CddMv9fDQdAIMVGP9SrYedMczNtH+gTStU/35CECxjsDkY7Q3E++t2 l2o0eLND3MvmOoVez2fEDzJR37wN0LbAVXwDWndaZWC2jAWNVLTm3rcH1eb1CTNlDd+C V4bTFwISgGNW/XwbIAo/PfHStwFbyjI5LYy+VXAtu/muWhlECRTC7gO6TM0K7IDkZnIL Yg93AP0ikdmcb3GmB8NcdNFreTXusqb+bbmAdIrnVzfbVnU1XpZUBqSWUZTkQD71iv2D /lY8LbeWnE/zbBR1dd0TO3J7HKEoMtSHGs52SjJ6EewWry1SEOn12/iwYmcAucE5W85u X5EA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=vIFxxwYlV9MeWU9exPU/QeZ0iOFgjcwohsSxCpudCh0=; b=WoBwUkCrOwG4THyCUk5AbzfY+1VCI7TLnN0AbvC3fovMmXsdamjBlv2BqeIlZrE8SI KF8m5Mo9Ly2tgDEsmY0Aq/8YUpONTyZGr2PP7Yc7rgfA50Xy6xUsWlSUGc0I/KDjMcOt UL/PVXCfKr+0cx8rSI7k+pDhOMEOhxisyGXpIJ1QlIJgiDRnK5IyROu44vWPk36xR15q MXUdS9OTuPkoC8YC0fhnEZuLJCkBsUQBnveei6RL6hYgkb9Qk6/oSDoZme2aUZn8JHf2 YRqeo7ItSOmwVXG7NpbVmBsuVRQnoOt3fv3ALODQVOrxbFJx0k2uw2JYeejjDsCJCD6Q 9dtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tdvKCKEn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id lv19si6433954ejb.257.2021.05.21.02.44.23; Fri, 21 May 2021 02:44:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tdvKCKEn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S233103AbhEUFVd (ORCPT + 99 others); Fri, 21 May 2021 01:21:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230176AbhEUFVc (ORCPT ); Fri, 21 May 2021 01:21:32 -0400 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53174C061574 for ; Thu, 20 May 2021 22:20:10 -0700 (PDT) Received: by mail-pg1-x52b.google.com with SMTP id j12so13364180pgh.7 for ; Thu, 20 May 2021 22:20:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vIFxxwYlV9MeWU9exPU/QeZ0iOFgjcwohsSxCpudCh0=; b=tdvKCKEn29Lt+RMvyjqEWsRZGBlFJRCPiht+uMCfYKmgDyefOHpNdYylsmhor0v0Bg RvOFP8dVi8iVhzdOq2ZE6PgniqtCf7zzZsIZweSSqBb6xJWHzv2U+9RYzES4bmXHvuzC XJEe6o9F6oKap/9Qn8sl9soEyxn38KcxyJJ9vO8QAzdcqIb7g2gBkXNCraht/FRhyKZN M8ez7XSeEqVArCJBykfgg/cIp/onyUzSxOTATvrbngj465KTZAIPJy26O9yclGRfCc+S E3VKulXAAHq9Spzh1sq3bg1RcT6vJBWrkwUiaq/L/FwkhnuTDWWzdFvbH70BvBEt3Txy w2OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vIFxxwYlV9MeWU9exPU/QeZ0iOFgjcwohsSxCpudCh0=; b=I5J8B0gnnCdQS3wWp4FfyFFTUxPUm80G4/JXGye43WGUHGKY10Udjn65hj2EKZhnj9 /bRnMgJz2Mmspm4eutiHnTKls7MD4I+EIoZ39puO4Hb2gWIzN9aVEuJ2qSOqiNQOjVfO hIr1qXiui0buepjNmvYTzG/f2o0+mmvVicHUW0pl+rND7YTyu/qM7g338KU5573xBwgR Y7gHuAyAz2xBrY1i1sXqumOkgi10ZltyqmNHpQdNzqHANlNdmiy/2PXGvBxGTy+Vi9Hy 638/hBQvrShR7elgqfKIRlfQXqBVGi29xydWJNRL7XuTV2qfI1DCsM5oBRDeKTMV7nC2 LlmA== X-Gm-Message-State: AOAM532jYzODsEMfIyZMG6dYnaFp+XPgza0W7bF4kM3e4G1ZvUoYLq78 SBnMnM+NF/IQK2TKf1PAEkaLZl9wGhKZyJOdBk/SxA== X-Received: by 2002:a63:4f50:: with SMTP id p16mr8045052pgl.40.1621574409590; Thu, 20 May 2021 22:20:09 -0700 (PDT) MIME-Version: 1.0 References: <20210424004645.3950558-1-seanjc@google.com> <20210424004645.3950558-36-seanjc@google.com> In-Reply-To: From: Reiji Watanabe Date: Thu, 20 May 2021 22:19:53 -0700 Message-ID: Subject: Re: [PATCH 35/43] KVM: x86: Move setting of sregs during vCPU RESET/INIT to common x86 To: Sean Christopherson Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 18, 2021 at 2:45 PM Sean Christopherson wrote: > > On Mon, May 17, 2021, Reiji Watanabe wrote: > > > --- a/arch/x86/kvm/svm/svm.c > > > +++ b/arch/x86/kvm/svm/svm.c > > > @@ -1204,12 +1204,6 @@ static void init_vmcb(struct kvm_vcpu *vcpu) > > > init_sys_seg(&save->ldtr, SEG_TYPE_LDT); > > > init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16); > > > > > > - svm_set_cr0(vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET); > > > - svm_set_cr4(vcpu, 0); > > > - svm_set_efer(vcpu, 0); > > > - kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); > > > - vcpu->arch.regs[VCPU_REGS_RIP] = 0x0000fff0; > > > > Reviewed-by: Reiji Watanabe > > > > Those your vCPU RESET/INIT changes look great. > > > > I think the change in init_vmcb() basically assumes that the > > function is called from kvm_vcpu_reset(via svm_vcpu_reset()). > > Although shutdown_interception() directly calls init_mcb(), > > I would think the change doesn't matter for the shutdown > > interception case. > > > > IMHO it would be a bit misleading that a function named 'init_vmcb', > > which is called from other than kvm_vcpu_reset (svm_vcpu_reset()), > > only partially resets the vmcb (probably just to me though). > > It's not just you, that code is funky. If I could go back in time, I would lobby > to not automatically init the VMCB and instead put the vCPU into > KVM_MP_STATE_UNINITIALIZED and force userspace to explicitly INIT or RESET the > vCPU. Even better would be to add KVM_MP_STATE_SHUTDOWN, since technically NMI > can break shutdown (and SMI on Intel CPUs). I see. Adding KVM_MP_STATE_SHUTDOWN sounds right to me given the real CPU's behavior. > Anyways, that ship has sailed, but we might be able to get away with replacing > init_vmcb() with kvm_vcpu_reset(vcpu, true), i.e. effecting a full INIT. That > would ensure the VMCB is consistent with KVM's software model, which I'm guessing > is not true with the direct init_vmcb() call. It would also have some connection > to reality since there exist bare metal platforms that automatically INIT the CPU > if it hits shutdown (maybe only for the BSP?). Yes, calling kvm_vcpu_reset(vcpu, true) sounds better than the direct init_vmcb() call. > Side topic, the NMI thing got me looking through init_vmcb() to see how it > handles the IDT and GDT, and surprise, surprise, it fails to zero IDTR.base and > GDTR.base. I'll add a patch to fix that, and maybe try to consolidate the VMX > and SVM segmentation logic. That's surprising... It seems init_vmcb() was used only for RESET when the function was originally introduced and the entire vmcb was zero-cleared before init_vmcb() was called. So, I would suspect init_vmcb() was originally implemented assuming that all the vmcb fields were zero. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6aa8b732ca01c3d7a54e93f4d701b8aabbe60fb7 Thanks, Reiji