Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp826865pxb; Tue, 8 Feb 2022 03:28:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJwZG4aqrdM7FoQ3spQ9KkqQ2s4YMu0DCon6+TdSLBJQ/Lb4C5BqQxfiUyk7CqzGUvJ7/KU0 X-Received: by 2002:a05:6402:35ca:: with SMTP id z10mr4062278edc.43.1644319704697; Tue, 08 Feb 2022 03:28:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644319704; cv=none; d=google.com; s=arc-20160816; b=m0334SC9p7jtGiWtOXu3dbDiqn3twyRLMryR7MrWFs5xTDPHZaylmPuUILjLmMIlL2 naEKRPy85QZteiWZlGVR2oFF1uEnMLvfEe/j8b3VoscDi+o2wGyoaxXsNur+ByMym1ke T+h9Y5wbEXenXjvZYg1TZK3U9qpgK+qI+SzyEv4v01Pg+iy35X/ceTriwY4i2tpXxZ7G 0j75hbBf67y+NAPp55Fd0esdyWf1FsxDSXLK3f+KlvkFieOtvE7NhRhGgRIkvcxmGdUJ XPCErhv2olbymf43Gb6ui+0WaY+rDh3zro0oHtF3nPvGbB9L6Xca5C/R26t7qkSuqMYW H+Ag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=ssfxozQsVXkPqd8hURrWGqEOTDmpelF59UyVKkS50EU=; b=FNkgrpskGl2e06ArvxHBzdn8AFFG0B3ZF8v6RSD0C4jm4PaQ2gwX8yAGHLCCAHMJce T0nXbE2F36qJhngICLtjBAXDD+PDpaFh9p0/TkA9FeMXRclkaA7YKHswDL5S7PcJ4WHD g2Df8hfLzqfvWYHCIorWHTEz5oElcNKXcbErFBl8RB+w74UBW1RRXm4RLjz4wpicPFPZ qD9GHrQDePred6LDqAYw7xuB2l3XZWVlpuKzPy7VhyKrgsHO/Diw2SfF9QyJmUOJObwB grkuroHvD5OwzRJMVhiG6pOSn+ok+vWLv9wYnQhyCcJHaAXjARP7JBfzN+f6/SLujHzb 9siA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SXqtqc5d; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hp1si11235822ejc.79.2022.02.08.03.27.59; Tue, 08 Feb 2022 03:28:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SXqtqc5d; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345939AbiBHC2m (ORCPT + 99 others); Mon, 7 Feb 2022 21:28:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345950AbiBHC2j (ORCPT ); Mon, 7 Feb 2022 21:28:39 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E539FC043180 for ; Mon, 7 Feb 2022 18:28:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1644287316; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=ssfxozQsVXkPqd8hURrWGqEOTDmpelF59UyVKkS50EU=; b=SXqtqc5d5ELoKQ7BWKW5Gx/VOQjgrJZDvXghDctvkWX8y04RJE757VojE9FNsdpbCxGwEg HVIXgZrWrsGVQMuWhf1waWU3fWkYdO/9VZvCrsztrdsjGAMLQmFb9vLjZWGNd4WlZcmkdI 15uV2iKPCza+haYTjmESA6CP+o4/k0w= Received: from mail-ot1-f72.google.com (mail-ot1-f72.google.com [209.85.210.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-330-SsqsfNtMPvOINxdmvum7gQ-1; Mon, 07 Feb 2022 21:28:35 -0500 X-MC-Unique: SsqsfNtMPvOINxdmvum7gQ-1 Received: by mail-ot1-f72.google.com with SMTP id l34-20020a0568302b2200b005a22eb442daso7237492otv.15 for ; Mon, 07 Feb 2022 18:28:35 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ssfxozQsVXkPqd8hURrWGqEOTDmpelF59UyVKkS50EU=; b=HVEMnbXAgavfW0Ssz6K/dz65X3opHVPwqL8f1Lzwh815Mbxh8QM3QT5jW1J9h9ZMUE 9gnYv7v2jqKVwl6Yyy+qhwlJqq0DBJkfpitaGHBJ/rDir508JLKsQAjIuvJHWLzlQ2nO qSxNVkXUMm4qyL9v3oQYmVSCB6GA+Wn9MzNPMXxDeUvgrJ1JE33E0QEOh/P9ZSbs0V42 laURe4MMkF1kDZ9u/FX5U6XdvvPpllNJs29DLIOlozRxLtZcuygSATqIjD+IeO41xCsK ldu9WNXKwbUKPSj81zzEav+DEZ6KAeAo/g9lWrM2VBNCxDwxQXiN5d/rodPi5Pvdk0eB ILzw== X-Gm-Message-State: AOAM530lHrna1nc11kkYoPguMUfnXsy76ub8DkKx6Y5eJjxH/ZreWE+x 91Qag3kjMCo52T7JAN0039AzsEi11yr7kh2OSi/4eIvcOF6h5T2SQ+AGQioIWXUxX5baCzOltBu hHI+0VMNd8t+DJErgnVgGrM6l X-Received: by 2002:a05:6808:158f:: with SMTP id t15mr765190oiw.245.1644287313994; Mon, 07 Feb 2022 18:28:33 -0800 (PST) X-Received: by 2002:a05:6808:158f:: with SMTP id t15mr765182oiw.245.1644287313783; Mon, 07 Feb 2022 18:28:33 -0800 (PST) Received: from localhost.localdomain ([2804:431:c7f0:b1af:f10e:1643:81f3:16df]) by smtp.gmail.com with ESMTPSA id y15sm4617016oof.37.2022.02.07.18.28.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 18:28:33 -0800 (PST) From: Leonardo Bras To: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" Cc: Leonardo Bras , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/1] x86/kvm/fpu: Mask guest fpstate->xfeatures with guest_supported_xcr0 Date: Mon, 7 Feb 2022 23:28:10 -0300 Message-Id: <20220208022809.575769-1-leobras@redhat.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org During host/guest switch (like in kvm_arch_vcpu_ioctl_run()), the kernel swaps the fpu between host/guest contexts, by using fpu_swap_kvm_fpstate(). When xsave feature is available, the fpu swap is done by: - xsave(s) instruction, with guest's fpstate->xfeatures as mask, is used to store the current state of the fpu registers to a buffer. - xrstor(s) instruction, with (fpu_kernel_cfg.max_features & XFEATURE_MASK_FPSTATE) as mask, is used to put the buffer into fpu regs. For xsave(s) the mask is used to limit what parts of the fpu regs will be copied to the buffer. Likewise on xrstor(s), the mask is used to limit what parts of the fpu regs will be changed. The mask for xsave(s), the guest's fpstate->xfeatures, is defined on kvm_arch_vcpu_create(), which (in summary) sets it to all features supported by the cpu which are enabled on kernel config. This means that xsave(s) will save to guest buffer all the fpu regs contents the cpu has enabled when the guest is paused, even if they are not used. This would not be an issue, if xrstor(s) would also do that. xrstor(s)'s mask for host/guest swap is basically every valid feature contained in kernel config, except XFEATURE_MASK_PKRU. Accordingto kernel src, it is instead switched in switch_to() and flush_thread(). Then, the following happens with a host supporting PKRU starts a guest that does not support it: 1 - Host has XFEATURE_MASK_PKRU set. 1st switch to guest, 2 - xsave(s) fpu regs to host fpustate (buffer has XFEATURE_MASK_PKRU) 3 - xrstor(s) guest fpustate to fpu regs (fpu regs have XFEATURE_MASK_PKRU) 4 - guest runs, then switch back to host, 5 - xsave(s) fpu regs to guest fpstate (buffer now have XFEATURE_MASK_PKRU) 6 - xrstor(s) host fpstate to fpu regs. 7 - kvm_vcpu_ioctl_x86_get_xsave() copy guest fpstate to userspace (with XFEATURE_MASK_PKRU, which should not be supported by guest vcpu) On 5, even though the guest does not support PKRU, it does have the flag set on guest fpstate, which is transferred to userspace via vcpu ioctl KVM_GET_XSAVE. This becomes a problem when the user decides on migrating the above guest to another machine that does not support PKRU: The new host restores guest's fpu regs to as they were before (xrstor(s)), but since the new host don't support PKRU, a general-protection exception ocurs in xrstor(s) and that crashes the guest. This can be solved by making the guest's fpstate->user_xfeatures only hold values compatible to guest_supported_xcr0. This way, on 7 the only flags copied to userspace will be the ones compatible to guest requirements, and thus there will be no issue during migration. As a bonus, will also fail if userspace tries to set fpu features that are not compatible to the guest configuration. (KVM_SET_XSAVE ioctl) Signed-off-by: Leonardo Bras --- arch/x86/kvm/cpuid.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 28be02adc669..2337079445ba 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -296,6 +296,10 @@ static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) vcpu->arch.guest_supported_xcr0 = cpuid_get_supported_xcr0(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent); + /* Mask out features unsupported by guest */ + vcpu->arch.guest_fpu.fpstate->user_xfeatures = + fpu_user_cfg.default_features & vcpu->arch.guest_supported_xcr0; + kvm_update_pv_runtime(vcpu); vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu); -- 2.35.1