Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp4556648pxb; Thu, 14 Oct 2021 07:34:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzY+4O8M+eAAbrC3E2m1e3Ls/Wfy4RBK4asnwL2IFDyDL7V0O2LJ1ysWYS0UVGmFxoZst0z X-Received: by 2002:a17:903:2304:b0:13f:2457:11dd with SMTP id d4-20020a170903230400b0013f245711ddmr5309473plh.57.1634222063686; Thu, 14 Oct 2021 07:34:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634222063; cv=none; d=google.com; s=arc-20160816; b=im/fJlp8c0cPZaVI35gc1uTi/35fsZiscnSG/g35dbyQOfj5cHP/rvADMFdC+A2xnb VgflmMFwa/rSXDKXZ2r/VAd1gLwTOWoBlwmBvT7SxTQ9oSyrMoyfIwXOe5dWLHuBesBr PiZdAuziJ4uA3anHEHlrW5+nlODXZsnUygm+Xx9kHuITh6UmbIR0I7UBASzL86Q+X/1W eTEH4kxYFKIC6CBxF1tsSbFYPorIA8Eph/9X+oaHkaQeQ2k4894eH/lxIK4yoRSdWYcm a9CAEaVTLPlQG2ePf96vhMHgwgNBW7CzQoFVH1jBRgKTvjrPb3CHsfWdGy2BSwhHG1oR b7xQ== 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=K+Z9xcyAhlGhiE9Ys3ZMpsS6I+8zaTKKFDoB018524Q=; b=Bz1diUVionMYFbscAqcv2gar5lhdJi2VnR00xuZdQA4ZKTXgr6bCDCxqc71c+ulyxF 6rGZOmchnEUUTraukU07iVNAb1SJ577NU8XoKtO+PQKF47TfHtlbzNF3gdwYtlysnjja kFDGV02jYX3QK9ZGcQkxOpsvLzvTxgm4vABTstePSnSC+6bycU8+4XbW9HwMqU0ccKzP kAu7Q389irR7b/eUWaUrgBOQxlO9w9TQ+Gb9uOHX8JonoKaPlcq8kLK1w9TXwY0zaj9C Cnp5k0uhxFuhhuoj2ENroWIQ9fIM9PMy+2AryN3vFYz0KAxQjRnocziFsGL1kfS9HLCY Wgmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=k+j8vzoY; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=KN60iuzh; 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 q1si4275010pgv.627.2021.10.14.07.34.08; Thu, 14 Oct 2021 07:34:23 -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=k+j8vzoY; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=KN60iuzh; 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 S231747AbhJNOLO (ORCPT + 99 others); Thu, 14 Oct 2021 10:11:14 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:42840 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230177AbhJNOLN (ORCPT ); Thu, 14 Oct 2021 10:11:13 -0400 From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1634220546; 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=K+Z9xcyAhlGhiE9Ys3ZMpsS6I+8zaTKKFDoB018524Q=; b=k+j8vzoYgb9TasLzlp1gxnwy27KKqytkbbQk3yS3iea+SH8k4GVs4e7JadVcy7bpdzp56O yR+YWd3wuismwBBDIeqA/IdvT9ddoXweCn8TBwvutTh3chQHsvgng4pkTnkXcrIj09l1Dk YQ+yeUGReSjo39ocKrT6hQMKIgVBvUnrBiy/WSiB+fZrpLZaxjqkGiU9xwEYbgiGeADmJ8 JS0qoLlYd4m24wSnsM9Oixhu2vP0Stzc1SVNIPz6wWgxCcIkBG14YhU2KLD2MZg0vH4SSD qpxiG15zDJ8AphXsuGt+Zf+joZoOfXzq9svbLHa9VlG+mXMLceYy/GRFDne66Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1634220546; 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=K+Z9xcyAhlGhiE9Ys3ZMpsS6I+8zaTKKFDoB018524Q=; b=KN60iuzhEc02Xp90DjyYuXWP5nfYxfwmaELt+nfeHAbY0dksjM/SdXcytd4cTcbtSVz5SZ YTxYUd+Pnv1ITdDA== 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: <6bbc5184-a675-1937-eb98-639906a9cf15@redhat.com> References: <871r4p9fyh.ffs@tglx> <6bbc5184-a675-1937-eb98-639906a9cf15@redhat.com> Date: Thu, 14 Oct 2021 16:09:06 +0200 Message-ID: <87wnmf66m5.ffs@tglx> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 14 2021 at 11:01, Paolo Bonzini wrote: > On 14/10/21 10:02, Liu, Jing2 wrote: > Based on the input from Andy and Thomas, the new way would be like this: > > 1) host_fpu must always be checked for reallocation in > kvm_load_guest_fpu (or in the FPU functions that it calls, that depends > on the rest of Thomas's patches). That's because arch_prctl can enable > AMX for QEMU at any point after KVM_CREATE_VCPU. No. 1) QEMU starts 2) QEMU requests permissions via prctl() 3) QEMU creates vCPU threads Doing it the other way around makes no sense at all and wont work. > 2) every use of vcpu->arch.guest_supported_xcr0 is changed to only > include those dynamic-feature bits that were enabled via arch_prctl. > That is, something like: > > static u64 kvm_guest_supported_cr0(struct kvm_vcpu *vcpu) > { > return vcpu->arch.guest_supported_xcr0 & > (~xfeatures_mask_user_dynamic | \ > current->thread.fpu.dynamic_state_perm); Bah. You can't get enough from poking in internals, right? 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(); for you. Once. The you have the information you need right in the guest FPU. See? > So something like this pseudocode is called by both XCR0 and XFD writes: > > int kvm_alloc_fpu_dynamic_features(struct kvm_vcpu *vcpu) > { > u64 allowed_dynamic = current->thread.fpu.dynamic_state_perm; That's not a valid assumption. > u64 enabled_dynamic = > vcpu->arch.xcr0 & xfeatures_mask_user_dynamic; > > /* All dynamic features have to be arch_prctl'd first. */ > WARN_ON_ONCE(enabled_dynamic & ~allowed_dynamic); > > if (!vcpu->arch.xfd_passthrough) { > /* All dynamic states will #NM? Wait and see. */ > if ((enabled_dynamic & ~vcpu->arch.xfd) == 0) > return 0; > > kvm_x86_ops.enable_xfd_passthrough(vcpu); > } > > /* current->thread.fpu was already handled by arch_prctl. */ No. current->thread.fpu has the default buffer unless QEMU used AMX or something forced it to allocate a larger buffer. > return fpu_alloc_features(vcpu->guest_fpu, > vcpu->guest_fpu.dynamic_state_perm | enabled_dynamic); This unconditionally calls into that allocation for every XCR0/XFD trap ? > } Also you really should not wait until _all_ dynamic states are cleared in guest XFD. Because a guest which has bit 18 and 19 available but only uses one of them is going to trap on every other context switch due to XFD writes. So you check for (guest_xfd & guest_perm) != guest_perm) and (guest_xr0 & guest_perm) != 0 If both are true, then you reallocate the buffers for _all_ permitted states _and_ set XFD to pass through. Thanks, tglx