Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp574454rdb; Thu, 2 Nov 2023 11:32:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFivR0Ru3bpiXN8qGhvqCfhTzuH3OPh6UopMzGqPVAaqTLrLkhxPSfk2a/vCKmF2m/5eCM1 X-Received: by 2002:a05:6359:29cc:b0:168:ec64:8298 with SMTP id qf12-20020a05635929cc00b00168ec648298mr15386313rwb.17.1698949943048; Thu, 02 Nov 2023 11:32:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698949943; cv=none; d=google.com; s=arc-20160816; b=U4DFzkCx3ZU2eONQkpXM7FUqxvPXBQlwShgrFY+ing/xu+eJIyy/sI3ax5IWNvH86F 9ScFupJxVkmox8vZj+soVDdetXhBAzzYPI2fah2tntU4Z2Y37X7aZO02dq2s3QKt6rRb 8jQLx/rbqspNhp7EdaW2bVApmLysfTFO2YAvrgb+/kmcRBudrYEZkKl/pnnFdWWF/wYu FlGKm+HDuIcm2vEN13y1wPkEJvD8uNemTvRnwt20dvDjvmibf0PMHuwZc2Q7wV9fMxBt LORDM4ZPcZjok/ts+MXi8ipk99n2fd9CloajhczJjVBJikM8ZlFcYVuSJs/7CMdgLYFB AhhQ== 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 :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature; bh=A7gKntsq0pYbkfeFJCt8Zd0FsZ8RWC0rBYeV+n1CMQE=; fh=mJ0TIX4nXG/bpZwgk02WmB2EtK+oEU7bGXa6nt45zMY=; b=rpEH4uWeLBGD7Eo6VHd/vdY8xNx743/3zE1j42cI1AGCMjkDm91r3m0+4rf/yRhE41 ULJkTrdPr/0dJunE/18Xd76KF76Xlx49eDxPbabqXRmMC8S0V0Nhjqi9KMoFx3z0QQQg EEUpkCmerkRH1FDkZNojITskTXoCfxN3ewipv5VzRil1SOhlYjtK6jz8K904QGannfz1 Ym9QGntVpdaGFjI/GCSkJz6Wy+jMCETGP41X52BcATjj6XrCtv+1GSpeeVLXkJJoIAQC iZajeX4cDYvnpv3GR9riwFjrQNbysqoFcP1lW6mUUEAtGcyOXqpw5zEXp4ymiLnSLusk cLzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fztEhlzu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id 73-20020a63004c000000b005b934643e39si85803pga.599.2023.11.02.11.32.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 11:32:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fztEhlzu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 702808381EC9; Thu, 2 Nov 2023 11:32:01 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346648AbjKBSbu (ORCPT + 99 others); Thu, 2 Nov 2023 14:31:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229534AbjKBSbs (ORCPT ); Thu, 2 Nov 2023 14:31:48 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60167136 for ; Thu, 2 Nov 2023 11:31:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698949865; 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=A7gKntsq0pYbkfeFJCt8Zd0FsZ8RWC0rBYeV+n1CMQE=; b=fztEhlzu9Jmz2wiamBM3SQpokUQI8YN+zY9v4boeI8mkIzhYXvUNj/CIz5uVT/73u2GnRE ZsFXchclDJ26TNGy0Fwo1jODV5lSgofDEgie4jjpuuyzvAe8G5LHyt5U3TCvkIGL8zTBtr sFxkYRb9sckQE3yLOWJ/DPp+fxspWTI= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-28-B2vxhV5IM6qs3zK2uqen6Q-1; Thu, 02 Nov 2023 14:31:04 -0400 X-MC-Unique: B2vxhV5IM6qs3zK2uqen6Q-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-407d3e55927so7610055e9.1 for ; Thu, 02 Nov 2023 11:31:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698949863; x=1699554663; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=A7gKntsq0pYbkfeFJCt8Zd0FsZ8RWC0rBYeV+n1CMQE=; b=Gal/E3t1/xyeAGKHoUIlLIZBCw8lgKNOmY+qArud5JTxdAqJYg0rf7wpqhifyMaMTc XVMCSsosbep4mB0N7QLUECvsuhiJU6qogQlpGlHYrN5EC0+jRVjCbmgvjEsLLk4jorSl yb3Z65q7W24Lf+rm7mK8ZVu7oRnKgCQBr2J8zYIWWuZKoF8inCnForX5cxktoIcbWqa5 +3o7+JIpM/tgb7byfdHsaf/IMd00QwCwFo6euOBlO2X3KdmyY7fUN+j49wlta998WBt4 Ztz17+eC4tSsV296Bfn6h61b0aSVyN+j/sr21ZZrrK3ef1bj7ffhZ0zk7lN55kO9xbxe +b0Q== X-Gm-Message-State: AOJu0Yws60GPNfXsO1W0mEUQNSeKyracOOIomG6CtlcsytCgBssyzpwm v/AtPDpVf+lm3mev2KQZqRqTDZ6N9OYvH9Dg4xiyrDUaIR7QadqhMBu9utQrmRLJGQzRAZJtUAB UWMuEaew8kFgbH++lciWoZ/7T X-Received: by 2002:a5d:60ce:0:b0:32d:9d64:b429 with SMTP id x14-20020a5d60ce000000b0032d9d64b429mr12661644wrt.21.1698949862981; Thu, 02 Nov 2023 11:31:02 -0700 (PDT) X-Received: by 2002:a5d:60ce:0:b0:32d:9d64:b429 with SMTP id x14-20020a5d60ce000000b0032d9d64b429mr12661624wrt.21.1698949862647; Thu, 02 Nov 2023 11:31:02 -0700 (PDT) Received: from starship ([89.237.99.95]) by smtp.gmail.com with ESMTPSA id dd23-20020a0560001e9700b0032f7f4d008dsm21268wrb.20.2023.11.02.11.31.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 11:31:02 -0700 (PDT) Message-ID: Subject: Re: [PATCH v6 11/25] KVM: x86: Report XSS as to-be-saved if there are supported features From: Maxim Levitsky To: Sean Christopherson Cc: Yang Weijiang , pbonzini@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, dave.hansen@intel.com, peterz@infradead.org, chao.gao@intel.com, rick.p.edgecombe@intel.com, john.allen@amd.com Date: Thu, 02 Nov 2023 20:31:00 +0200 In-Reply-To: References: <20230914063325.85503-1-weijiang.yang@intel.com> <20230914063325.85503-12-weijiang.yang@intel.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.3 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Thu, 02 Nov 2023 11:32:01 -0700 (PDT) On Wed, 2023-11-01 at 12:18 -0700, Sean Christopherson wrote: > On Tue, Oct 31, 2023, Maxim Levitsky wrote: > > On Thu, 2023-09-14 at 02:33 -0400, Yang Weijiang wrote: > > > From: Sean Christopherson > > > > > > Add MSR_IA32_XSS to list of MSRs reported to userspace if supported_xss > > > is non-zero, i.e. KVM supports at least one XSS based feature. > > > > I can't believe that CET is the first supervisor feature that KVM supports... > > > > Ah, now I understand why: > > > > 1. XSAVES on AMD can't really be intercepted (other than clearing CR4.OSXSAVE > > bit, which isn't an option if you want to support AVX for example) On VMX > > however you can intercept XSAVES and even intercept it only when it touches > > specific bits of state that you don't want the guest to read/write freely. > > > > 2. Even if it was possible to intercept it, guests use XSAVES on every > > context switch if available and emulating it might be costly. > > > > 3. Emulating XSAVES is also not that easy to do correctly. > > > > However XSAVES touches various MSRs, thus letting the guest use it > > unintercepted means giving access to host MSRs, which might be wrong security > > wise in some cases. > > > > Thus I see that KVM hardcodes the IA32_XSS to 0, and that makes the XSAVES > > work exactly like XSAVE. > > > > And for some features which would benefit from XSAVES state components, > > KVM likely won't even be able to do so due to this limitation. > > (this is allowed thankfully by the CPUID), forcing the guests to use > > rdmsr/wrmsr instead. > > Sort of? KVM doesn't (yet) virtualize PASID, HDC, HWP, or arch LBRs (wow, > there's a lot of stuff getting thrown into XSTATE), so naturally those aren't > supported in XSS. > > KVM does virtualize Processor Trace (PT), but PT is a bit of a special snowflake. > E.g. the host kernel elects NOT to manage PT MSRs via XSTATE, but it would be > possible for KVM to the guest to manage PT MSRs via XSTATE. I must also note that PT doesn't always uses guest physical addresses to write its trace output, because there is a secondary execution control 'Intel PT uses guest physical addresses', however I see that KVM requires it, so yes, we could likely have supported PT xsaves component. > > I suspect the answer to PT is threefold: > > 1. Exposing a feature that isn't "supported" by the host kernel is scary. > 2. No one has pushed for the support, e.g. Linux guests obviously don't complain > about lack of XSS support for PT. > 3. Toggling PT MSR passthrough on XSAVES/XRSTORS accesses would be more complex > and less performant than KVM's current approach. > > Re: #3, KVM does passthrough PT MSRs, but only when the guest is actively using > PT. PT is basically a super fancy PMU feature, and so KVM "needs" to load guest > state as late as possible before VM-Entry, and load host state as early as possible > after VM-Exit. I.e. the context switch happens on *every* entry/exit pair. > Makes sense. > By passing through PT MSRs only when needed, KVM avoids a rather large pile of > RDMSRs and WRMSRs on every entry/exit, as the host values can be kept resident in > hardware so long as the main enable bit is cleared in the guest's control MSR > (which is context switch via a dedicated VMCS field). > > XSAVES isn't subject to MSR intercepts, but KVM could utilize VMX's XSS-exiting > bitmap to effectively intercept reads and writes to PT MSRs. Except that as you > note, KVM would either need to emulate XSAVES (oof) or save/load PT MSRs much more > frequently. > > So it's kind of an emulation thing, but I honestly doubt that emulating XSAVES > was ever seriously considered when KVM support for PT was added. > > CET is different than PT because the MSRs that need to be context switched at > every entry/exit have dedicated VMCS fields. The IA32_PLx_SSP MSRs don't have > VMCS fields, but they are consumed only in privelege level changes, i.e. can be > safely deferred until guest "FPU" state is put. > > > However it is possible to enable IA32_XSS bits in case the msrs XSAVES > > reads/writes can't do harm to the host, and then KVM can context switch these > > MSRs when the guest exits and that is what is done here with CET. > > This isn't really true. It's not a safety or correctness issue so much as it's > a performance issue. True as well, I haven't thought about it from this POV. > E.g. KVM could let the guest use XSS for any virtualized > feature, but it would effectively require context switching related state that > the host needs loaded "immediately" after VM-Exit. And for MSRs, that gets > very expensive without dedicated VMCS fields. Yes, unless allowing setting a MSR via xrstors causes harm to the host, (for example msr that has a physical address in it). Such MSRs cannot be allowed to be set by the guest even for the duration of the guest run, and that means that we cannot pass through the corresponding XSS state component. > > I mean, yeah, it's a correctness thing to not consume guest state in the host > and vice versa, but that's not unique to XSS in any way. > Best regards, Maxim Levitsky