From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 78E9B17722 for ; Tue, 2 Jan 2024 22:33:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="dx0KaHAe" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704234782; 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=292EYekmNs4JFV4FXHbDVJ2CzL6/2QZNvmkFFwcUOVs=; b=dx0KaHAeZnvuUSN88tPI+Z1VC+PTGAv45yt3uCHp06sxfHiwhh/tPBT90lJBOAq17qX+EK hhx8c0zO9JzcMsejixKfmfFliUeDCG3M6MbrfTWv4/TN+sDscflTEji05hUNorZTWEA+ES O6MqKOFKXkHgwwnBi025fJVJJDMJsOo= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-685-RNAbnuK4MAi4rH_0Yfxh2g-1; Tue, 02 Jan 2024 17:33:00 -0500 X-MC-Unique: RNAbnuK4MAi4rH_0Yfxh2g-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-3368c707b03so6657028f8f.3 for ; Tue, 02 Jan 2024 14:33:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704234779; x=1704839579; 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=292EYekmNs4JFV4FXHbDVJ2CzL6/2QZNvmkFFwcUOVs=; b=rkV2PM/SvmQ3Nn4VYJkBr7ZjcLTGOvvThz3oVGCYyMCxg2ivFpW2g78zw/DgA6cR1J HivGfG/n1TEFcPty9IqQHJqBWmjd0kMBUu1OZgzl0KRYQeZvpG97ffA9Lp/yQFFZWZJg 5KLN3FB0z8XW2sFd9E9IE92fYSKnOkH6XbIkf651O8uMXY9hvUB9rXEwV40jUcrxUMKy la4BCAqdjDyqeyrvG5NPoH/Qi19SNwpZEGRGzxR6UdBmrBX/CJ/Q9qC9xWP2bZ4eB1PO 8GJKllHSioIXl/J8FUxVLxbox+huYGvl2KsRw6qqBEecskHXR+ksH7wiM7TVeRX49AFK Ky5w== X-Gm-Message-State: AOJu0YxPM3WE45tUBFk6U4eghgxqTV9KTPqoJxAHQWNWkk2mXrBlucQl xby/gSoUNH1UACQXB/LW2XMssbbUrq74WfEc8Wz9qeyyzdngXCg/t+LhzzptyrG6eyQ7sKKmkoo yEql9JzeEzm20I4v7uUmY0aHycAkDVReA X-Received: by 2002:a5d:40c8:0:b0:336:6eba:b0ff with SMTP id b8-20020a5d40c8000000b003366ebab0ffmr9052061wrq.94.1704234779559; Tue, 02 Jan 2024 14:32:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IHGnt3hTwcUmqBp3itxNA3pVpP/rkblmR2Q8SKgX/qEhxCsTHHbd5/xN8eNOIoUVll5LtIx0g== X-Received: by 2002:a5d:40c8:0:b0:336:6eba:b0ff with SMTP id b8-20020a5d40c8000000b003366ebab0ffmr9052052wrq.94.1704234779204; Tue, 02 Jan 2024 14:32:59 -0800 (PST) Received: from starship ([147.235.223.38]) by smtp.gmail.com with ESMTPSA id q28-20020adfab1c000000b0033690139ea5sm27133785wrc.44.2024.01.02.14.32.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 14:32:58 -0800 (PST) Message-ID: Subject: Re: [PATCH v8 06/26] x86/fpu/xstate: Create guest fpstate with guest specific config From: Maxim Levitsky To: Yang Weijiang , seanjc@google.com, pbonzini@redhat.com, dave.hansen@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: peterz@infradead.org, chao.gao@intel.com, rick.p.edgecombe@intel.com, john.allen@amd.com Date: Wed, 03 Jan 2024 00:32:57 +0200 In-Reply-To: <20231221140239.4349-7-weijiang.yang@intel.com> References: <20231221140239.4349-1-weijiang.yang@intel.com> <20231221140239.4349-7-weijiang.yang@intel.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 7bit On Thu, 2023-12-21 at 09:02 -0500, Yang Weijiang wrote: > Use fpu_guest_cfg to calculate guest fpstate settings, open code for > __fpstate_reset() to avoid using kernel FPU config. > > Below configuration steps are currently enforced to get guest fpstate: > 1) Kernel sets up guest FPU settings in fpu__init_system_xstate(). > 2) User space sets vCPU thread group xstate permits via arch_prctl(). > 3) User space creates guest fpstate via __fpu_alloc_init_guest_fpstate() > for vcpu thread. > 4) User space enables guest dynamic xfeatures and re-allocate guest > fpstate. > > By adding kernel dynamic xfeatures in above #1 and #2, guest xstate area > size is expanded to hold (fpu_kernel_cfg.default_features | kernel dynamic > xfeatures | user dynamic xfeatures), then host xsaves/xrstors can operate > for all guest xfeatures. > > The user_* fields remain unchanged for compatibility with KVM uAPIs. > > Signed-off-by: Yang Weijiang > --- > arch/x86/kernel/fpu/core.c | 47 ++++++++++++++++++++++++++++++-------- > 1 file changed, 37 insertions(+), 10 deletions(-) > > diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c > index 976f519721e2..0e0bf151418f 100644 > --- a/arch/x86/kernel/fpu/core.c > +++ b/arch/x86/kernel/fpu/core.c > @@ -250,8 +250,6 @@ void fpu_reset_from_exception_fixup(void) > } > > #if IS_ENABLED(CONFIG_KVM) > -static void __fpstate_reset(struct fpstate *fpstate, u64 xfd); > - > static void fpu_init_guest_permissions(struct fpu_guest *gfpu) > { > struct fpu_state_perm *fpuperm; > @@ -272,25 +270,54 @@ static void fpu_init_guest_permissions(struct fpu_guest *gfpu) > gfpu->perm = perm & ~FPU_GUEST_PERM_LOCKED; > } > > -bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu) > +static struct fpstate *__fpu_alloc_init_guest_fpstate(struct fpu_guest *gfpu) > { > + bool compacted = cpu_feature_enabled(X86_FEATURE_XCOMPACTED); > + unsigned int gfpstate_size, size; > struct fpstate *fpstate; > - unsigned int size; > > - size = fpu_user_cfg.default_size + ALIGN(offsetof(struct fpstate, regs), 64); > + /* > + * fpu_guest_cfg.default_size is initialized to hold all enabled > + * xfeatures except the user dynamic xfeatures. If the user dynamic > + * xfeatures are enabled, the guest fpstate will be re-allocated to > + * hold all guest enabled xfeatures, so omit user dynamic xfeatures > + * here. > + */ > + size = fpu_guest_cfg.default_size + > + ALIGN(offsetof(struct fpstate, regs), 64); > + > fpstate = vzalloc(size); > if (!fpstate) > - return false; > + return NULL; > + /* > + * Initialize sizes and feature masks, use fpu_user_cfg.* > + * for user_* settings for compatibility of exiting uAPIs. > + */ > + fpstate->size = gfpstate_size; > + fpstate->xfeatures = fpu_guest_cfg.default_features; > + fpstate->user_size = fpu_user_cfg.default_size; > + fpstate->user_xfeatures = fpu_user_cfg.default_features; > + fpstate->xfd = 0; > > - /* Leave xfd to 0 (the reset value defined by spec) */ > - __fpstate_reset(fpstate, 0); > fpstate_init_user(fpstate); > fpstate->is_valloc = true; > fpstate->is_guest = true; > > gfpu->fpstate = fpstate; > - gfpu->xfeatures = fpu_user_cfg.default_features; > - gfpu->perm = fpu_user_cfg.default_features; > + gfpu->xfeatures = fpu_guest_cfg.default_features; > + gfpu->perm = fpu_guest_cfg.default_features; > + > + return fpstate; > +} > + > +bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu) > +{ > + struct fpstate *fpstate; > + > + fpstate = __fpu_alloc_init_guest_fpstate(gfpu); > + > + if (!fpstate) > + return false; > > /* > * KVM sets the FP+SSE bits in the XSAVE header when copying FPU state Reviewed-by: Maxim Levitsky Best regards, Maxim Levitsky