Received: by 2002:a05:7412:f589:b0:e2:908c:2ebd with SMTP id eh9csp945116rdb; Wed, 1 Nov 2023 07:16:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGSi7g7zW4QsEcrngV5BtYjkyT3Y095N4JUPPR2qB44yHTu5PeKKqYdGkZwOtUbyAKgyloI X-Received: by 2002:a05:6808:148b:b0:3a7:5611:a53b with SMTP id e11-20020a056808148b00b003a75611a53bmr19933953oiw.0.1698848182224; Wed, 01 Nov 2023 07:16:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698848182; cv=none; d=google.com; s=arc-20160816; b=fSNLaYPqCgVt7Ifa1G1j45J1ktV+Hkuqvk4rbXr65Mq4dx+/AvTCn45CvYit0CkZxG 1GleLgX/RFTE2P2BniIXJ16KGBqVProJBqKwukCXTnQLDc+/8XNZVV5mbuDMFdtu8FS6 Ij4IYOKjxxMBLz19PqzZU/vq1amwtOYp4cwS0w1z0dG4Da7HepvHYFI6D+YE6ID7YT9w XiQi47pJrx5vp+cypM9pjdIkbPnl9sGcYIQIb1nR10BFOk3+SqV7WKfSMaBvcQRSEPSc lSsp2m96xfNglmcgJDgj2DW8SeuBJI4R70rjz7687E4wTbIqgYHsmiPATyziYmQXcG9z HczA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=V40p05CD6XkzApx4zccf3FWriwPpKLjFHTaz0izqv7Q=; fh=aPMOEzd66aPAKk49Lp2SBFs//sWfyQChpiIZTGnmc0k=; b=HBl8pqVVTkhegcHEtxRwWfE51bY6KoIXUBf1qYrGwxe2OIG+QbvKworFu+118b12mg cDU7V5JO/Y1/+edCciWTq431wWKywOxT00IcVhehCeQaVgk3fSBdwNZjkl7ir0submVM FpKVOWDLu54Q23c1MjEjDmCWxSs/JRXyoiCOrkh/VkCGhamu1Q/rboWa/1ZHtfu8qKaO j6HWyvDKm4cm9LWuscssQ1vdKK785KFNStLAOKzNDlAXR+kNnd3CCPrz0EqpZ9QI92g9 kZrml8wJqXO6dyrr6xEmOPEFQO+rN3OSOKBQ6fzq4WO2s1jE2L9tUHG0mF2WpUwbtz7B IxDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=MsSQD7DE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id bf16-20020a056808191000b003a7f507fa4bsi568156oib.129.2023.11.01.07.16.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 07:16:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=MsSQD7DE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 7C021808A435; Wed, 1 Nov 2023 07:16:19 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344125AbjKAOQN (ORCPT + 99 others); Wed, 1 Nov 2023 10:16:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232094AbjKAOQM (ORCPT ); Wed, 1 Nov 2023 10:16:12 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A18F3E4 for ; Wed, 1 Nov 2023 07:16:04 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d9a541b720aso7274360276.0 for ; Wed, 01 Nov 2023 07:16:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698848164; x=1699452964; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=V40p05CD6XkzApx4zccf3FWriwPpKLjFHTaz0izqv7Q=; b=MsSQD7DE7pplsc0b7BoeVtoc8ndYPkawb7gTlxyJyLi9O5YYPrNnkYh8IMbKS+VXDh mwL2cZTCbqoMUTPSx6FR56vwk3UgFcwWMnmp9/yG0TuojEXct6FXFeiuaAu9B0bu2Yyl KR4Dvc37xbIYikl8atSeY3Jmfdg7+uCUprtupEmJDtavoeQuZCMKXPTxPl8qto349e19 edih/xa/6v7lBZcYIbeu5Q5n1J4bYoKYJGEv5OQz0oYDvtKBoZ+FBPYUg+vgtRw0n4oN xPxuLDw064f89DYj/rxHoG1oIY5mlFIC/VZvISXqrr5GijWrFn5n8JOB4eLoSrKs37it X9mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698848164; x=1699452964; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=V40p05CD6XkzApx4zccf3FWriwPpKLjFHTaz0izqv7Q=; b=AGDjJ/LJ8ht0P2J01fPYLg29wgyaSmuOs+ZR8XRY4MOjTQfFVoctF7r/yb3hhksTPJ QbO4flpK5rbWoJkD4wzsMZxsNSCmI6JahNnQ+7sEg4q7292Lp/gIIJdbjNEFPZjzy26V vIGeiDD677/CnUO1RF1pcanzGYsDSQ9jGS75f+aVDO90FSRuGW3KEdE7e/QndEf3A7jq 5myRsBhjSk9VTUFCwgnnpzkVpOmR84b04GhG1JcLfpA2nzfdrZQEqd0JHzH3EHoww+hO E4U6rVP03GV42N1lEKoNp7soqjW4z3KfJ0JTGtFog99obJ3yQeXWg1KgIukxG3qtTY05 6NHA== X-Gm-Message-State: AOJu0YySerqR0pzZPX6Jo9Z7yN6jtHBaxMFOpdIGTWnGhFab5NUry6jI ct+BWoo2e56lq7Lu66Qg+1be2JCrSbc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:848c:0:b0:d9a:5e10:c34d with SMTP id v12-20020a25848c000000b00d9a5e10c34dmr308204ybk.11.1698848163709; Wed, 01 Nov 2023 07:16:03 -0700 (PDT) Date: Wed, 1 Nov 2023 07:16:02 -0700 In-Reply-To: Mime-Version: 1.0 References: <20230914063325.85503-1-weijiang.yang@intel.com> <20230914063325.85503-7-weijiang.yang@intel.com> <1347cf03-4598-f923-74e4-a3d193d9d2e9@intel.com> Message-ID: Subject: Re: [PATCH v6 06/25] x86/fpu/xstate: Opt-in kernel dynamic bits when calculate guest xstate size From: Sean Christopherson To: Maxim Levitsky Cc: Weijiang Yang , Dave Hansen , pbonzini@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, peterz@infradead.org, chao.gao@intel.com, rick.p.edgecombe@intel.com, john.allen@amd.com Content-Type: text/plain; charset="us-ascii" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.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 (lipwig.vger.email [0.0.0.0]); Wed, 01 Nov 2023 07:16:19 -0700 (PDT) On Tue, Oct 31, 2023, Maxim Levitsky wrote: > On Thu, 2023-10-26 at 10:24 -0700, Sean Christopherson wrote: > > On Wed, Oct 25, 2023, Weijiang Yang wrote: > On top of that I think that applying the same permission approach to guest's > FPU state is not a good fit, because of two reasons: > > 1. The guest FPU state will never be pushed on the signal stack - KVM swaps > back the host FPU state before it returns from the KVM_RUN ioctl. > > Also I think (not sure) that ptrace can only access (FPU) state of a > stopped process, and a stopped vCPU process will also first return to > userspace. Again I might be mistaken here, I never researched this in > depth. > > Assuming that I am correct on these assumptions, the guest FPU state can > only be accessed via KVM_GET_XSAVE/KVM_SET_XSAVE/KVM_GET_XSAVE2 ioctls, > which also returns the userspace portion of the state including optionally > the AMX state, but this ioctl doesn't really need FPU permission > framework, because it is a KVM ABI, and in fact KVM_GET_XSAVE2 was added > exactly because of that: to make sure that userspace is aware that larger > than 4K buffer can be returned. > > 2. Guest FPU state is not even on demand resized (but I can imagine that in > the future we will do this). Just because guest FPU state isn't resized doesn't mean there's no value in requiring userspace to opt-in to allocating 8KiB of data per-vCPU. > And of course, adding permissions for kernel features, that is even worse > idea, which we really shouldn't do. > > > > > If there are no objections, I'll test the below and write a proper changelog. > > > > -- > > From: Sean Christopherson > > Date: Thu, 26 Oct 2023 10:17:33 -0700 > > Subject: [PATCH] x86/fpu/xstate: Always preserve non-user xfeatures/flags in > > __state_perm > > > > Fixes: 781c64bfcb73 ("x86/fpu/xstate: Handle supervisor states in XSTATE permissions") > > Signed-off-by: Sean Christopherson > > --- > > arch/x86/kernel/fpu/xstate.c | 18 +++++++++++------- > > 1 file changed, 11 insertions(+), 7 deletions(-) > > > > diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c > > index ef6906107c54..73f6bc00d178 100644 > > --- a/arch/x86/kernel/fpu/xstate.c > > +++ b/arch/x86/kernel/fpu/xstate.c > > @@ -1601,16 +1601,20 @@ static int __xstate_request_perm(u64 permitted, u64 requested, bool guest) > > if ((permitted & requested) == requested) > > return 0; > > > > - /* Calculate the resulting kernel state size */ > > + /* > > + * Calculate the resulting kernel state size. Note, @permitted also > > + * contains supervisor xfeatures even though supervisor are always > > + * permitted for kernel and guest FPUs, and never permitted for user > > + * FPUs. > > + */ > > mask = permitted | requested; > > - /* Take supervisor states into account on the host */ > > - if (!guest) > > - mask |= xfeatures_mask_supervisor(); > > ksize = xstate_calculate_size(mask, compacted); > > This might not work with kernel dynamic features, because > xfeatures_mask_supervisor() will return all supported supervisor features. I don't understand what you mean by "This". Somewhat of a side topic, I feel very strongly that we should use "guest only" terminology instead of "dynamic". There is nothing dynamic about whether or not XFEATURE_CET_KERNEL is allowed; there's not even a real "decision" beyond checking wheter or not CET is supported. > Therefore at least until we have an actual kernel dynamic feature (a feature > used by the host kernel and not KVM, and which has to be dynamic like AMX), > I suggest that KVM stops using the permission API completely for the guest > FPU state, and just gives all the features it wants to enable right to By "it", I assume you mean userspace? > __fpu_alloc_init_guest_fpstate() (Guest FPU permission API IMHO should be > deprecated and ignored) KVM allocates guest FPU state during KVM_CREATE_VCPU, so not using prctl() would either require KVM to defer allocating guest FPU state until KVM_SET_CPUID{,2}, or would require a VM-scoped KVM ioctl() to let userspace opt-in to Allocating guest FPU state during KVM_SET_CPUID{,2} would get messy, as KVM allows multiple calls to KVM_SET_CPUID{,2} so long as the vCPU hasn't done KVM_RUN. E.g. KVM would need to support actually resizing guest FPU state, which would be extra complexity without any meaningful benefit. The only benefit I can think of for a VM-scoped ioctl() is that it would allow a single process to host multiple VMs with different dynamic xfeature requirements. But such a setup is mostly theoretical. Maybe it'll affect the SEV migration helper at some point? But even that isn't guaranteed. So while I agree that ARCH_GET_XCOMP_GUEST_PERM isn't ideal, practically speaking it's sufficient for all current use cases. Unless a concrete use case comes along, deprecating ARCH_GET_XCOMP_GUEST_PERM in favor of a KVM ioctl() would be churn for both the kernel and userspace without any meaningful benefit, or really even any true change in behavior.