Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp6810819rdb; Tue, 2 Jan 2024 14:33:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IEwXhO+3FSftOZZ1rs5rz4dzYqQMEdiH1WzdIc9ELksaTchX6t7HGxBPttR1w/k4+KKWbhB X-Received: by 2002:a05:6358:e483:b0:175:4f0f:bbaf with SMTP id by3-20020a056358e48300b001754f0fbbafmr1101226rwb.47.1704234795412; Tue, 02 Jan 2024 14:33:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704234795; cv=none; d=google.com; s=arc-20160816; b=HgP99S7iKrOY0ured70++bTrkn0lr9qYWSxhWzhKGOXlUDl+YaugS56venSZpulJvB FtcLYA9pbWyT8YxVnKLF9wwBEy9pjfGan9tFcl4hgAQBpIIqbx84o/RfC39iOxZAYsb+ EG7Zq16AjS3oVHRzFXGlgnY79gAyGXztPGsXmfuwt63ykMmUMazN75LMrr/1A01oYcna n6BvOc8SNC2MbfM+BC1wOVM3fBMA8FTQTmp85kDS7r/I9B2/GmAISvESAORAiCH+OTZV PPuzVxOoORWHp5/ODjfX685/PJkW1ey1ArMV6azvzh80E1ZRphMsmbflT4woCe1m0v/U huPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:user-agent:references:in-reply-to :date:cc:to:from:subject:message-id:dkim-signature; bh=292EYekmNs4JFV4FXHbDVJ2CzL6/2QZNvmkFFwcUOVs=; fh=qm/3G+Adpj/B7TaijAEJbs/udoUfJHgp4MqQlWOh7ts=; b=LigjatbEOso+z/9so+MOiRbc9Bkjjmu5nF/O+U1DeaJAuEfQDZwZKIhX35behxX/RL OHWr6aTnIb2pyhM8S8Mp5biXt6/z546/Xzs292/6ZlgaCP32BsvZe1/yALkIMCYgZPwe 00H9p+4Yj5lrPQn8WO32fvQP7K6IpBqh8QS+HBuSYdyA/3mxRMjXOHXvGE44q13mC1O/ 8ND6BNzir10TeuLW+mgHWNXpczkgHrK2PUyy8ItnqBwFuVomlaMBajucW1+sCJigbDCW 8HkBtAfSlhIotIzQEPBZmbVe/WU5f8rpxy0Zc4ljlObJ14/uajWoZzGms7cDCVvnOlVa 0Hfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dx0KaHAe; spf=pass (google.com: domain of linux-kernel+bounces-14940-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14940-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id s11-20020a056a00178b00b006d9b9603bd8si14789577pfg.334.2024.01.02.14.33.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 14:33:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14940-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dx0KaHAe; spf=pass (google.com: domain of linux-kernel+bounces-14940-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14940-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 280C0B20A42 for ; Tue, 2 Jan 2024 22:33:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A42F517730; Tue, 2 Jan 2024 22:33:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="dx0KaHAe" X-Original-To: linux-kernel@vger.kernel.org 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 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-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