Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp553375pxb; Fri, 15 Oct 2021 10:52:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzf+4jSgn/BmKQhGWAfDzwd7CLcmg8WuwLCdhrcdYrZXqrtLDScD401RDIBtO9HYuyDGUMC X-Received: by 2002:a17:907:1741:: with SMTP id lf1mr8147995ejc.225.1634320344110; Fri, 15 Oct 2021 10:52:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634320344; cv=none; d=google.com; s=arc-20160816; b=n/IvICx5jDLJ4mSg8MucCsSS9RdR+e9eOE3K4+gVtVG83wnQUaggJoIuaisCUEGRsY qEvtPvzFu5MRDMQtQLlYGuqxTuOCIajQn6wFVzfv+IHFgTO7UYyHTtBDt5pApnF4Jvkz fp4jtcCloaHs4RnE5QAc3F4vfTrKnQpgXELxIYJkindEGL+699nWEky1Fhstoo6db/UG alnFCHrCeWREH/Clh6h5D1rPeUe2qjDuLFWSLh80sS/957xldQT+7VpIR1XEpI1E9OQD o8yeR/7t9+ykKacbnkPa90AsttvGiWKxxyV1ookyIH6qFX9UD1X5U0Er14ccqinlMKjz cG6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:dkim-signature:dkim-signature:from; bh=qMD82jDLRghwt3jUSaceVpHhA/FFqOYNqRPnvzBrsR0=; b=nYOfeT3lDcItzvPxqmTgFvcB3m0rKfy5LFQg+pGoBTFZ8ELPqX4C4Rk6pThIPTDbSy C8EBclrEwes78zcgXRYxRANWyVegp/KqLPnj6J8fnJGtrMyrUNh3OgpY7mNgbVxwg+F7 dTycGR+7e8aqJNW3lApCILrYFvQyLk6wP7580k8LnO5/XYGmeP91KOneltwTtTav2OLz O0LdsDLqqj9l9+WqmguJUEKcqIbYgax9r7UMqjiitIpuTHosoTkdRzgFlkAE2bv/coHA zCjUfAdpnpE22w6c9xhDsfN1GOEKRAVrLOKIU22OLNjJ4k7KkFMBG2ESrz2jSRTzOokR Na0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=kdiS5uUN; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s3si6044776edd.342.2021.10.15.10.51.56; Fri, 15 Oct 2021 10:52:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=kdiS5uUN; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237473AbhJOJie (ORCPT + 99 others); Fri, 15 Oct 2021 05:38:34 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:48800 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236690AbhJOJic (ORCPT ); Fri, 15 Oct 2021 05:38:32 -0400 From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1634290585; 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: in-reply-to:in-reply-to:references:references; bh=qMD82jDLRghwt3jUSaceVpHhA/FFqOYNqRPnvzBrsR0=; b=kdiS5uUNNb8ErRkx2YhowReaY5Jp8AFcpGiQnPNF8HccAObqB7VCQE6/lFH+0Egnhn4cVJ 3KuuY+YGtm7LURfj6QEVDA2prln1fjXD6Wqs6T6/roFhR9ipLXqbUjR4nxrYBIB72RRBCU GL7uTzEa1vWgaOmPqsvqXw0wEnTMUqYYxh1EoV2hbOjy7s7hTTPYGmjWkhRezP0kbmbSms JzVWmATTUc1TcLssZIGwkoYg5K9eEX8UQClQn6RhgxOLat5Mq3KjRPsnQFMqtB+ubcqXzE GQRjEDbPAYKA8jk1b3CQDJfnS9+BWW2hXrMbAjOeN65puzY9PIS6kTCF9NTa5Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1634290585; 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: in-reply-to:in-reply-to:references:references; bh=qMD82jDLRghwt3jUSaceVpHhA/FFqOYNqRPnvzBrsR0=; b=jiZNgPmBgqXDEeIgVyWjulnz0GG8uTrrHzpws/Cp0A+R+UbmdoMtf/pkEEGC3bRfOvLnh0 FKAGiFL1KEGM+2Bg== To: Paolo Bonzini , "Liu, Jing2" , LKML Cc: "x86@kernel.org" , "Bae, Chang Seok" , Dave Hansen , Arjan van de Ven , "kvm@vger.kernel.org" , "Nakajima, Jun" , Jing Liu , "seanjc@google.com" , "Cooper, Andrew" Subject: Re: [patch 13/31] x86/fpu: Move KVMs FPU swapping to FPU core In-Reply-To: <87lf2v5shb.ffs@tglx> References: <871r4p9fyh.ffs@tglx> <6bbc5184-a675-1937-eb98-639906a9cf15@redhat.com> <87wnmf66m5.ffs@tglx> <3997787e-402d-4b2b-0f90-4a672c77703f@redhat.com> <87lf2v5shb.ffs@tglx> Date: Fri, 15 Oct 2021 11:36:25 +0200 Message-ID: <87a6ja6352.ffs@tglx> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Paolo, On Thu, Oct 14 2021 at 21:14, Thomas Gleixner wrote: > On Thu, Oct 14 2021 at 17:01, Paolo Bonzini wrote: >>> vcpu_create() >>> >>> fpu_init_fpstate_user(guest_fpu, supported_xcr0) >>> >>> That will (it does not today) do: >>> >>> guest_fpu::__state_perm = supported_xcr0 & xstate_get_group_perm(); >>> >>> The you have the information you need right in the guest FPU. >> >> Good, I wasn't aware of the APIs that will be there. > > Me neither, but that's a pretty obvious consequence of the work I'm > doing for AMX. So I made it up for you. :) let me make some more up for you! If you carefully look at part 2 of the rework, then you might notice that there is a fundamental change which allows to do a real simplification for KVM FPU handling: current->thread.fpu.fpstate is now a pointer. So you can spare one FPU allocation because we can now do: fpu_attach_guest_fpu(supported_xcr0) { guest_fpstate = alloc_fpstate(supported_xcr0); fpu_init_fpstate_user(guest_fpstate, supported_xcr0); current->thread.fpu.guest_fpstate = guest_fpstate; } fpu_swap_kvm_fpu() becomes in the first step: fpu_swap_kvm_fpu(bool enter_guest) { safe_fpregs_to_fpstate(current->thread.fpu.fpstate); swap(current->thread.fpu.fpstate, current->thread.fpu.guest_fpstate); restore_fpregs_from_fpstate(current->thread.fpu.fpstate); } @enter guest will allow to do some sanity checks In a second step: fpu_swap_kvm_fpu(bool enter_guest, u64 guest_needs_features) { possibly_reallocate(enter_guest, guest_needs_features); safe_fpregs_to_fpstate(current->thread.fpu.fpstate); swap(current->thread.fpu.fpstate, current->thread.fpu.guest_fpstate); restore_fpregs_from_fpstate(current->thread.fpu.fpstate); possibly_reallocate(enter_guest, guest_needs_features); } @guest_needs_features is the information which you gather via guest XCR0 and guest XFD. So fpu_swap_kvm_fpu() is going to be the place where reallocation happens and that's good enough for both cases: vcpu_run() fpu_swap_kvm_fpu(); <- 1 while (...) vmenter(); fpu_swap_kvm_fpu(); <- 2 #1 QEMU user space used feature and has already large fpstate #2 Guest requires feature but has not used it yet (XCR0/XFD trapping) See? It's not only correct, it's also simple and truly beautiful. Thanks, tglx