Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp5660392pxj; Wed, 23 Jun 2021 06:23:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxO9qYGYV+Up9BwmnpHk57mCUJou4T7zPRLM54ocRt26lsGjJhXM3g67KaaFqEWF0pvpu8a X-Received: by 2002:a05:6402:35d3:: with SMTP id z19mr12241459edc.324.1624454595137; Wed, 23 Jun 2021 06:23:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624454595; cv=none; d=google.com; s=arc-20160816; b=MT8J/XPAO9zNp2gedU4AxLGg0VLZWFEbmwvAkKVAzcKRwSQ0bEMjcoQfBmCJMuF5w5 FUfkrWufsIIG6yXVgK9ec5yJFGsn0/Z0f9kbg2bjCePTGh/SA3VRf2HsFrTeX/QgS6cA Uoyx/JoYkf8sWY6LgvS4hqUxiolRCL2y6ocXuEV+uKmItk2TYnlrgKTy4U8GLvrYlmRu fP1h3HbL5Vy9yAKm3MV40m/cMCBXA5lLxuiDRjXqFV/G9Dnuto4bsCg/lWiKJ8m4V1HW +vM6htCiTgv0j7cP5TrVi3ScMCJZpAEKJVo20N9WM2DfvzvGQ4EJH2G+xah7LfaF42cS BdYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:subject:from :references:cc:to:dkim-signature; bh=Rp1O/CPJY4l8FkaCBOTzb4x+CQmYngEXaAEL0v+9OoY=; b=bMRJoyPn0pGhlTTsUEdAlXtU+Vsx67TFZ8/xtT4vAlql6TS8vw/jXJpc11TCiblybk 8f+gh6+eugAtDdXCgiPtlnGo3xMSu/2ltVxRAxMOq+93NTdvNNWgnoezVmF4sfsYgf9z EOom8+eCmbpMqgnRkeqjs90G3sXZdGYz8RQ2KdD3PjojBUoehTP9bS84uAnR3a7QFb86 lGy1sVwEN7hXoHNhQeuqbGG3tni2Gm3r5RPaSGk69E+QZPx9gGSs4aIfeHaV5hspWGQC HejJ/Fxm7phjRpNby/RwSzuSFYFwBfpzRiaO9nNGLZmg+fLnr7RibDgUaDg1GwPb4DGT Gsrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=J5eFVMDN; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id mm18si12449364ejb.584.2021.06.23.06.22.52; Wed, 23 Jun 2021 06:23:15 -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=@redhat.com header.s=mimecast20190719 header.b=J5eFVMDN; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231129AbhFWNYM (ORCPT + 99 others); Wed, 23 Jun 2021 09:24:12 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:41280 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230286AbhFWNYJ (ORCPT ); Wed, 23 Jun 2021 09:24:09 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1624454511; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Rp1O/CPJY4l8FkaCBOTzb4x+CQmYngEXaAEL0v+9OoY=; b=J5eFVMDN0aGa5+iTV1JLsHv9FDurQt0Ky3XcRe9F3q60ZOPnUVURWPC5XCs4w093Irxt8z iHBpp3yzgJgYlFGIxeH8m335yKdUVct95m9gTHR8uJqL+wSR+qMtt2n1lfIhCLLuBbmksH e4SfUM3Y0X0fzgcHV/fhRBq6OV9//iM= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-482-mXoEFExsMh6_AjRdyxgsMQ-1; Wed, 23 Jun 2021 09:21:50 -0400 X-MC-Unique: mXoEFExsMh6_AjRdyxgsMQ-1 Received: by mail-ej1-f71.google.com with SMTP id w13-20020a170906384db02903d9ad6b26d8so997629ejc.0 for ; Wed, 23 Jun 2021 06:21:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:cc:references:from:subject:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=Rp1O/CPJY4l8FkaCBOTzb4x+CQmYngEXaAEL0v+9OoY=; b=N/HTGC0DXLirAzzQc+1NJqEjiXCEYe1DVa1Tu3aScilNxumKIkgimK10iLUOZVOq2E ynAYfPPVobwKtQuCfGsPXxb6oann6YGLfWieqzDbEy6Kuln/zkgTDAL1mT/ygeQ45TyG FOcP/Ze+/p0OUrFzOIi3juiS7J+4LFVt1fHblj7XiUkQZfl3q1ipzHHA+XFa+J+IN89l 8AdCa8OypjSTt7CiDRkjQFOw90iZ/Y2pDTnh4OvKa4YsMEQhhnEkS7PmSUvs1CZziVwd 4MDYeR7LmCoHl6pUKRbF9TZMyei25GfkOP+B2QG/rUmCQgK9PqOP3k0jkhAUK5+sen0j WHvA== X-Gm-Message-State: AOAM530F96xpySfmaLZgivFb6AhGlsSIgjuHdPhojWSHs0KsiTQn0eQS Dy8Zw4mT5F8Lpqj/96exOj6UJnskbfwxzpq9B/6vIKUHz2wISTCxLn6DEDFCA+/UAvCblG+UhQf /WqdNZTNLUv2itB+fHWyKeyWNGzFixqA27u2ixaEtpyn5mVT6kFxPbwm+j/MmFmTVnBi1BIcbfA lV X-Received: by 2002:aa7:d799:: with SMTP id s25mr12305642edq.161.1624454509096; Wed, 23 Jun 2021 06:21:49 -0700 (PDT) X-Received: by 2002:aa7:d799:: with SMTP id s25mr12305593edq.161.1624454508854; Wed, 23 Jun 2021 06:21:48 -0700 (PDT) Received: from ?IPv6:2001:b07:6468:f312:c8dd:75d4:99ab:290a? ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id ml22sm5967065ejb.93.2021.06.23.06.21.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 23 Jun 2021 06:21:48 -0700 (PDT) To: Maxim Levitsky , Vitaly Kuznetsov , kvm@vger.kernel.org Cc: Sean Christopherson , Wanpeng Li , Jim Mattson , Cathy Avery , Emanuele Giuseppe Esposito , linux-kernel@vger.kernel.org References: <20210623074427.152266-1-vkuznets@redhat.com> <53a9f893cb895f4b52e16c374cbe988607925cdf.camel@redhat.com> From: Paolo Bonzini Subject: Re: [PATCH RFC] KVM: nSVM: Fix L1 state corruption upon return from SMM Message-ID: <01564b34-2476-2098-7ec8-47336922afda@redhat.com> Date: Wed, 23 Jun 2021 15:21:47 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.1 MIME-Version: 1.0 In-Reply-To: <53a9f893cb895f4b52e16c374cbe988607925cdf.camel@redhat.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 23/06/21 15:01, Maxim Levitsky wrote: > I did some homework on this now and I would like to share few my thoughts on this: > > First of all my attention caught the way we intercept the #SMI > (this isn't 100% related to the bug but still worth talking about IMHO) > > A. Bare metal: Looks like SVM allows to intercept SMI, with SVM_EXIT_SMI, > with an intention of then entering the BIOS SMM handler manually using the SMM_CTL msr. ... or just using STGI, which is what happens for KVM. This is in the manual: "The hypervisor may respond to the #VMEXIT(SMI) by executing the STGI instruction, which causes the pending SMI to be taken immediately". It *should* work for KVM to just not intercept SMI, but it adds more complexity for no particular gain. > On bare metal we do set the INTERCEPT_SMI but we emulate the exit as a nop. > I guess on bare metal there are some undocumented bits that BIOS set which > make the CPU to ignore that SMI intercept and still take the #SMI handler, > normally but I wonder if we could still break some motherboard > code due to that. > > B. Nested: If #SMI is intercepted, then it causes nested VMEXIT. > Since KVM does enable SMI intercept, when it runs nested it means that all SMIs > that nested KVM gets are emulated as NOP, and L1's SMI handler is not run. No, this is incorrect. Note that svm_check_nested_events does not clear smi_pending the way vmx_check_nested_events does it for nmi_pending. So the interrupt is still there and will be injected on the next STGI. Paolo > > About the issue that was fixed in this patch. Let me try to understand how > it would work on bare metal: > > 1. A guest is entered. Host state is saved to VM_HSAVE_PA area (or stashed somewhere > in the CPU) > > 2. #SMI (without intercept) happens > > 3. CPU has to exit SVM, and start running the host SMI handler, it loads the SMM > state without touching the VM_HSAVE_PA runs the SMI handler, then once it RSMs, > it restores the guest state from SMM area and continues the guest > > 4. Once a normal VMexit happens, the host state is restored from VM_HSAVE_PA > > So host state indeed can't be saved to VMC01. > > I to be honest think would prefer not to use the L1's hsave area but rather add back our > 'hsave' in KVM and store there the L1 host state on the nested entry always. > > This way we will avoid touching the vmcb01 at all and both solve the issue and > reduce code complexity. > (copying of L1 host state to what basically is L1 guest state area and back > even has a comment to explain why it (was) possible to do so. > (before you discovered that this doesn't work with SMM). > > Thanks again for fixing this bug! > > Best regards, > Maxim Levitsky >