Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1EBD4C433EF for ; Tue, 14 Dec 2021 02:50:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244832AbhLNCuc (ORCPT ); Mon, 13 Dec 2021 21:50:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244700AbhLNCuZ (ORCPT ); Mon, 13 Dec 2021 21:50:25 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07696C061748; Mon, 13 Dec 2021 18:50:24 -0800 (PST) Message-ID: <20211214024947.878061856@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1639450223; 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: references:references; bh=/QiR8LFIIghIdHuWPiT2Ph3EWm0DI3KfVwkZ9edwUik=; b=z4U6/hKo5Cx2hyMl0XZm6FByhOr0rSn5jEzghHwA+lIYB4hzXl7rb+5se6kG0xQza0hLvu Sn1taBF/DlpbhbOVCTjzDc7+rgHl4QUJHykLz8h/OcRBYkE88PFVsZI7MdoV17odhJe3r7 kwHMvyw5PpFMD3sx4OJvmz6WeYH/OEvO0knZenWhJhxjkpkmFVrzv3uOn6GrVVbmBXROCo iIlv6fm22hpTg1NFz/CE0/tKcvg1i5VqXLq1W8Kvr2jBA3eywOY/toTfmG2pZLmG68dyEz Bk22BA+sH8WwA8QO+l5oVn+6G1k3UfKfxELbyvqHO8VAWT5hEr4vzi+LckiJ0w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1639450223; 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: references:references; bh=/QiR8LFIIghIdHuWPiT2Ph3EWm0DI3KfVwkZ9edwUik=; b=mRUSRdZa2KYJ+7klW4efK6ryMNt4aIbxV/KR4Kd4Bjun1TxyD6EO/7Fr9fkDNQm0igD6j8 Dyja/mqRGO+wVmCQ== From: Thomas Gleixner To: LKML Cc: Jing Liu , Yang Zhong , Paolo Bonzini , x86@kernel.org, kvm@vger.kernel.org, Sean Christoperson , Jin Nakajima , Kevin Tian Subject: [patch 2/6] x86/fpu: Prepare guest FPU for dynamically enabled FPU features References: <20211214022825.563892248@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Tue, 14 Dec 2021 03:50:22 +0100 (CET) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org To support dynamically enabled FPU features for guests prepare the guest pseudo FPU container to keep track of the currently enabled xfeatures and the guest permissions. Signed-off-by: Thomas Gleixner --- arch/x86/include/asm/fpu/types.h | 13 +++++++++++++ arch/x86/kernel/fpu/core.c | 26 +++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) --- a/arch/x86/include/asm/fpu/types.h +++ b/arch/x86/include/asm/fpu/types.h @@ -505,6 +505,19 @@ struct fpu { */ struct fpu_guest { /* + * @xfeatures: xfeature bitmap of features which are + * currently enabled for the guest vCPU. + */ + u64 xfeatures; + + /* + * @perm: xfeature bitmap of features which are + * permitted to be enabled for the guest + * vCPU. + */ + u64 perm; + + /* * @fpstate: Pointer to the allocated guest fpstate */ struct fpstate *fpstate; --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -201,6 +201,26 @@ void fpu_reset_from_exception_fixup(void #if IS_ENABLED(CONFIG_KVM) static void __fpstate_reset(struct fpstate *fpstate); +static void fpu_init_guest_permissions(struct fpu_guest *gfpu) +{ + struct fpu_state_perm *fpuperm; + u64 perm; + + if (!IS_ENABLED(CONFIG_X86_64)) + return; + + spin_lock_irq(¤t->sighand->siglock); + fpuperm = ¤t->group_leader->thread.fpu.guest_perm; + perm = fpuperm->__state_perm; + + /* First fpstate allocation locks down permissions. */ + WRITE_ONCE(fpuperm->__state_perm, perm | FPU_GUEST_PERM_LOCKED); + + spin_unlock_irq(¤t->sighand->siglock); + + gfpu->perm = perm & ~FPU_GUEST_PERM_LOCKED; +} + bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu) { struct fpstate *fpstate; @@ -216,7 +236,11 @@ bool fpu_alloc_guest_fpstate(struct fpu_ fpstate->is_valloc = true; fpstate->is_guest = true; - gfpu->fpstate = fpstate; + gfpu->fpstate = fpstate; + gfpu->xfeatures = fpu_user_cfg.default_features; + gfpu->perm = fpu_user_cfg.default_features; + fpu_init_guest_permissions(gfpu); + return true; } EXPORT_SYMBOL_GPL(fpu_alloc_guest_fpstate);