Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp824847pxb; Tue, 8 Feb 2022 03:25:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJzebzjb4FPo7Emn9lb8WZo7Mj52afqCVf8Ycy5pI77mnIGnotZeijda3TRPcJCWhm9gVEm/ X-Received: by 2002:a17:907:94d2:: with SMTP id dn18mr3341903ejc.304.1644319542954; Tue, 08 Feb 2022 03:25:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644319542; cv=none; d=google.com; s=arc-20160816; b=vGRuIhQO+s0Ls6ZgIDFNN+eTgycMt/2qVqqd+BMhTcAHbbUgvG58nSEjYVU9rgfUJt XmMq4+9Cqhx7aVS/C1N2hQ5X423XSXwQ9QOG4p545dCABvj5MlrEWdJO6XvO0iNoZdnS LuKOXYhOmtgOE9T8yoPa59d/328CnLpMj8vhjrwLPfU8TfHdacQrMhQpnnX5VZBxB0Uz /REKsQHuH4jqcKltvjx0Pvg+fOOloosxXlQOMWlLIwSzHuNyJHsq8RSqEf8xkkSEfUfZ ETJBEj7oYmBDvgwSECQvjPOMsCJw549RRjuKIVpTDUhOuW0wueMxjVdGPb2pTtFWJRMh dUjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=9T/OvHQML0n7t8483X0s9RB1P7k3PGxeDBNyGeKNXxg=; b=Dzel/rLnP2YplpCrkOug4QP1Uqu0rPoZyr7Jn3pr79QkRueopkXFhHnhkjlck9xLgV NVgPzcmjOIiJfmJ1pEXVXvQDR1ac3clJe8UMGn63x9h8U5WXt5csjHonyaan5eX1mlCA Rtr9fbE2i3tADQj+siN5e8wHp+eP9AWfJe/gO6nvGA0ixAYgwvkuMMOT8JpyVsiVbnA7 EZOh0VeWfEYsEtKiXrhGUKw0uAOiGzi9F3gJS2bo+5+kOyRxWUdXIEaOPV9JOK1eX2Fq 6KNQbR8hOz9NzXdZxhvDmXjX54eJGjvGN2w0ZvbXjSXXg/U+oX9TEsxonYwkrIFrPIn2 hI4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gq1JiwiM; 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 w9si1015915edv.363.2022.02.08.03.25.17; Tue, 08 Feb 2022 03:25:42 -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=gq1JiwiM; 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 S242767AbiBGU1l (ORCPT + 99 others); Mon, 7 Feb 2022 15:27:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233366AbiBGUY1 (ORCPT ); Mon, 7 Feb 2022 15:24:27 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0C72FC0401E1 for ; Mon, 7 Feb 2022 12:24:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1644265465; 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=9T/OvHQML0n7t8483X0s9RB1P7k3PGxeDBNyGeKNXxg=; b=gq1JiwiMhVRICsyRsLLPMuVjgxY3NuFaWNOyW3mIw1C3bCv7egaV8L8z1YeyRdPdG9TAQb h15HpVxTIPRuJ9nWzPi3GKlhQHu5alQQw5exNpK8mf24IsMq3fIDWukQsAm7//3E1VVt2E Bd65DYJYsIcjG+2BY6kE0Y+h+tei39k= Received: from mail-lj1-f200.google.com (mail-lj1-f200.google.com [209.85.208.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-503-QjTbS4zmPG-IIYEL_gW-WA-1; Mon, 07 Feb 2022 15:24:24 -0500 X-MC-Unique: QjTbS4zmPG-IIYEL_gW-WA-1 Received: by mail-lj1-f200.google.com with SMTP id c31-20020a2ebf1f000000b0022d87a28911so4919396ljr.1 for ; Mon, 07 Feb 2022 12:24:24 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=9T/OvHQML0n7t8483X0s9RB1P7k3PGxeDBNyGeKNXxg=; b=trMpr9zZItA2QVjTHq4+9y0TVSEEOTF+WfG0ACCrC6ce8RE3xfh/eG35ILmA9bF/u4 IwgnPbpzZHON4eZ4Z+c12FjL3UPJt5fYRlWWrRqbDU9aYWAI/BCu0IRD0Zp4nGBJ8FKX +tNu6m6AX1gFkWLusJTj161aw0toMn6a+OJXUXt5FHeNV2y+3K8X6aqWjghPeg4BtSgu y6pBb4PnnA8VHcm/2zqVLrwETcfZaPymuTjRWHoI4EKfCLyEQx68NJhz92AXDLUPwZg8 wMbbUGiZuRRonO/c0Tlzk/oMFFieN2fmzoeqlFfP9MZIhxHxZ6Ykl4DaBDbOs6WrQcM7 YYSw== X-Gm-Message-State: AOAM530aZZVzeG8WKbnyVxUGj0obF87FmCuLBIReUGlpN8N/mQV5nRM/ 9WSjY61+tCXTcyzqQdl7Jox+DMjK8E1ubTgt3Mxtf1ccCRhZFhxex959V2+KCpArn3wKv/zdzTw NWeERvEebVaJLZk45N6Hkqabau4xnUQUxgKFOkRUr X-Received: by 2002:a05:651c:1213:: with SMTP id i19mr745449lja.116.1644265463147; Mon, 07 Feb 2022 12:24:23 -0800 (PST) X-Received: by 2002:a05:651c:1213:: with SMTP id i19mr745427lja.116.1644265462882; Mon, 07 Feb 2022 12:24:22 -0800 (PST) MIME-Version: 1.0 References: <20220205081658.562208-1-leobras@redhat.com> <20220205081658.562208-2-leobras@redhat.com> In-Reply-To: From: Leonardo Bras Soares Passos Date: Mon, 7 Feb 2022 17:24:11 -0300 Message-ID: Subject: Re: [PATCH v1 1/2] x86/kvm/fpu: Mask guest fpstate->xfeatures with guest_supported_xcr0 To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" 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 Hello Paolo, On Mon, Feb 7, 2022 at 10:30 AM Paolo Bonzini wrote: > > On 2/5/22 09:16, Leonardo Bras wrote: > > 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. > > According to kernel src, it is instead switched in switch_to() and > > flush_thread(). > > Hi Leonardo, is this an issue when patch 2 is applied? Yes. This issue happens on host/guest context switch, instead of KVM_{GET,SET}_XSAVE, so this bug will be triggered whenever the guest doesn't support PKRU but the host does, without any interference of above IOCTLs. In fact, IIUC, even if we are able to fix the feature bit with KVM_SET_XSAVE, it would come back after another host/guest context switch if we don't fix vcpu->arch.guest_fpu.fpstate->xfeatures. > With this patch, > we have to reason about the effect of calling KVM_SET_CPUID2 twice calls > back to back. I think an "&=" would be wrong in that case. So, you suggest something like this ? vcpu->arch.guest_fpu.fpstate->xfeatures = fpu_user_cfg.default_features & vcpu->arch.guest_supported_xcr0; > > On the other hand, with patch 2 the change is only in the KVM_SET_XSAVE > output, which is much more self-contained. Agree, but they solve different sources of the same issue. Patch 2 will only address a bug that can happen if userspace mistakenly tries to set a feature the guest does not support. > > Thanks, Thank you! Best regards, Leo [...]