From patchwork Mon Feb 19 07:47:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Yang, Weijiang" X-Patchwork-Id: 202933 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1136120dyc; Sun, 18 Feb 2024 23:52:55 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVYlebPKgpgjSLf9fHz+FFtxeGv7tXl17JWo2C2YKu6AgltGcqzvbrHZ8bI8V6qP88ysQWK1iBFSWqEJK+n71PpvoByPA== X-Google-Smtp-Source: AGHT+IGYumJyafq3K/AaAMQZREgV4zRBx8b4OhDFHTUnaLx+e18fUU2Nc5CnpXpQWQTetDKteZZI X-Received: by 2002:a05:620a:2611:b0:787:4f15:57f0 with SMTP id z17-20020a05620a261100b007874f1557f0mr8628766qko.13.1708329175257; Sun, 18 Feb 2024 23:52:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708329175; cv=pass; d=google.com; s=arc-20160816; b=OCirESgM1VljOAREL41eN79qpqS/8NEKYyWBtAEiJHZTtawnwIZFPys/wxsvssrEO9 kH4njFQLTWnY+1ldemtaxEM8q0b46Pb9vygRpwRkPV0BuC/YUYxyyqIpHAIwsuDv7+7/ KdWygyVgjAP5/nQ+w28rc1F9Wy8KDD5w2nybtEyEFeAKgX97b48Mz2Yo0rNLT3mF/8NN 4qVV5a2ecbTUv+rWHmKwpoNPmk3EmsvRpeBTyZ+7Gsm+fCfnl0s7J0ftnWw3s+Xe8ACe jJTcuNP9f6RB2Fd96MIyJlMqD0MIgJFyUD+CptG1KgxLW5WoCRmDRY4/p+A+VXXxusVZ Gx1w== ARC-Message-Signature: i=2; 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:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=MUNwqKbLx7lA7xK560Cysrztk3GZxe7jlwLBcN+2QIQ=; fh=6bYtQoKQxNJSYrJA1a9vseXH6qHZpRYO7L/7krtpXA0=; b=wypV1DnU7UFHXwCChsff3JxjBynm/Mb3/3xVWKK+bhZwOfCUDz+Ble4wf7pFYFvxMR Htb7GDeGPZPKPt8SKIwf4S3TrtsTCFzh8Jvld/ojQR4VK3lOxCRhR/OPvh88XoMjuDzL 1brVjvrQNrtKKm9C9mpxxsEWcfj+DRP9FrRrNDFqdXPtOUI+0WDzIrWInxoU/n9TIEPw SNWxRxqEa1hQ/uX8Q57U0L5mGuoswRRLaBoFZ/HjvG2QDBGszw/0lL2OGS4H+o4CSpPm Sy2kkEzEi1NsXB+ZyCldfqlesOzXfNrXJ3w475nt4r0sDgHqQd/J+HqLjtxG76MK/+jM IWAQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=EDshXo79; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-70871-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70871-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id x25-20020ae9f819000000b007871d80eeb7si5712907qkh.171.2024.02.18.23.52.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 23:52:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70871-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=EDshXo79; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-70871-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70871-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 1151B1C214DE for ; Mon, 19 Feb 2024 07:52:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2CF4232C8E; Mon, 19 Feb 2024 07:47:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="EDshXo79" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (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 D4C8423750; Mon, 19 Feb 2024 07:47:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708328869; cv=none; b=POkMNes07+nB9SWkWl+DHLIww5MU+z5dLeggB+Z8kjLY/+/gqpAyHyPKaS/IxEopZIcv3Weo4ZC2ZCv0OwKAhcjYENGXIa9qgSy2l8sjo/VOS+rjt4kJH87wTCGYawmT+tPPoUmobXiIVFbAoOAQAFvF9XmfsyLKXvRc4AGTFbc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708328869; c=relaxed/simple; bh=SkFizbcLL2iXx+O1d7csJZlnwkmoUAktnTokiB88u0A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pUC+M4iNwmh7qQ5Y0eHRPhPU3uwQS0hiUvOAsrPQpwV2W72i0XjnrFM+WgtmNQbPgmOHI/vIwiY9IE3nD4u831kRDIfcmLZ7GhHOBYYXTRT8WmktSI0jJhwTwXepYmts2PTp+YA/DaHHee9nCnWdzuK6ndTXhL3+AjUp0qwuJzc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=EDshXo79; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708328867; x=1739864867; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=SkFizbcLL2iXx+O1d7csJZlnwkmoUAktnTokiB88u0A=; b=EDshXo79qBqb2BX379ViBSeqcpYGrd9Qw2rozun5JpurTF2naqiNdfjP dDtGSj2p2tJhbupXUzEnJxIwoe2DHHPE28+79uduAc6rCnqgH6/ylRfEW ISZ6sQDD5fnaG2hAsVmuna1jxS76XgrUQG0EbIIJ1ELCadAwXV7OyXE9T gvOF9lJBCfIA1loFWOl/lMzKCnh6YQXomYC1Ll2O7xDN7FHgOA2/XDeJw DP5NACTFdQx0psmZVfDvfZ2VfYAYwSCzFuvp9oHk7JuZtbO7J++6wZPD9 tQaSvlE5yZHsSXTVcfB7zCT+9i4SkoC4PmOONi7m4JFPIC6rN6cZCXVZ4 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10988"; a="2535045" X-IronPort-AV: E=Sophos;i="6.06,170,1705392000"; d="scan'208";a="2535045" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2024 23:47:44 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10988"; a="826966075" X-IronPort-AV: E=Sophos;i="6.06,170,1705392000"; d="scan'208";a="826966075" Received: from jf.jf.intel.com (HELO jf.intel.com) ([10.165.9.183]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2024 23:47:43 -0800 From: Yang Weijiang To: seanjc@google.com, pbonzini@redhat.com, dave.hansen@intel.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: peterz@infradead.org, chao.gao@intel.com, rick.p.edgecombe@intel.com, mlevitsk@redhat.com, john.allen@amd.com, weijiang.yang@intel.com Subject: [PATCH v10 06/27] x86/fpu/xstate: Create guest fpstate with guest specific config Date: Sun, 18 Feb 2024 23:47:12 -0800 Message-ID: <20240219074733.122080-7-weijiang.yang@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240219074733.122080-1-weijiang.yang@intel.com> References: <20240219074733.122080-1-weijiang.yang@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791312973391426657 X-GMAIL-MSGID: 1791312973391426657 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 Reviewed-by: Maxim Levitsky Reviewed-by: Rick Edgecombe --- arch/x86/kernel/fpu/core.c | 39 +++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index e8205e261a24..dc2d2641fda7 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -194,8 +194,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; @@ -216,25 +214,48 @@ 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) { 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 = fpu_guest_cfg.default_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) +{ + if (!__fpu_alloc_init_guest_fpstate(gfpu)) + return false; /* * KVM sets the FP+SSE bits in the XSAVE header when copying FPU state