From patchwork Tue Nov 15 23:09:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Huey X-Patchwork-Id: 20632 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2987446wru; Tue, 15 Nov 2022 15:14:58 -0800 (PST) X-Google-Smtp-Source: AA0mqf5pJ07P945AU6UEjA/4QxLktJ1Cjvur+kIVbe2S6/qUrKoF0O3CN8MRgBl8fHinhrYIFXQu X-Received: by 2002:a17:90b:3a89:b0:205:ff5b:d27a with SMTP id om9-20020a17090b3a8900b00205ff5bd27amr582770pjb.225.1668554097949; Tue, 15 Nov 2022 15:14:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668554097; cv=none; d=google.com; s=arc-20160816; b=PoyztBb8aa1sS7Dr+fXudB5pJmrqXOMMsrYzjjaMipfSHeNTLaeOcHoprlgiKQ0h+n M8BTET5cb7WGAU162gDHPfmbF68pUPRqDa0mwp6pL0dezlXqE3OUV/HICvTKwAA+rKfm uD2htpZKeiTqRO3vhlqdZKm1zEiaL6tvnEbcUYeXMJHDHhkW9/1ZXX8i9SAihwOJYFWS RmaWKA22DQQytylk6k0ZxYiJmgpA6B6Moi6xjo0fWGHN5+yzhlaa6rMXcEzi+agYfeSa xJxfcnqxtCoS9xV3WdI4J0mGjnnHV2GkeM+RmKVuBsdRfjQW5UkNrpEcNajVLCold63G W9uA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=1qpXWSCDrMjTrrN7DSUZrVNnGlngy6qbcHa+BWaBxfw=; b=X3iR/3PqwEcOXF7h75Jk/7SOwCX+NRolhG/S36QDZPGMa+dbFkuFR7AUchCZgZQRiM ImHd8cFdq6EnCSMi5rHQ+ZRLaNbl4eUu6KdXjAqI44i4ABFP/9rjQqZYaqD6REDcoyIP MkMR8rcmomtcDxBkLuZ4cpxRCk1SmVTsXNMzocHDaHx1hgmOj6DSibHXHeKsSjwgp7mP GoWsglx3DDABJMCfOWNPfTu/bPzpXjmvpLc71257Wl2qCGEOaebrSNah32p/5Vuj60vW 7pZBPwHZW0Ksltu5eiChmc8JG6Bk64qnJmcufBqsfjvH/Rlt6oGXTFjmwinCP9BgksYe Ye/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=by3OpFNk; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 5-20020a630e45000000b0046f9f4f8e08si12832343pgo.156.2022.11.15.15.14.44; Tue, 15 Nov 2022 15:14:57 -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=@kylehuey.com header.s=google header.b=by3OpFNk; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231431AbiKOXMU (ORCPT + 99 others); Tue, 15 Nov 2022 18:12:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238586AbiKOXLk (ORCPT ); Tue, 15 Nov 2022 18:11:40 -0500 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24B112E6A6 for ; Tue, 15 Nov 2022 15:11:40 -0800 (PST) Received: by mail-pj1-x102a.google.com with SMTP id b11so14922112pjp.2 for ; Tue, 15 Nov 2022 15:11:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1qpXWSCDrMjTrrN7DSUZrVNnGlngy6qbcHa+BWaBxfw=; b=by3OpFNkQCvqiF7Z+Ei1ZUihu4ifOPLgyDRqCGJ9mDwSmsppwq5B3L7OSbVs6FSzZq uhL+U8YozGaxewngILbOAgMLv44xfey9tO53DDpqYVyjzvahWzl8o30YKcgVXdT8kNm+ yVYA2KpHsHz+kaX5LE4vA2BwqI/ZXosIYX/iaFNgbaNHzVkjrTPfHjGOFLGbgOY2TEO8 UoZlZ+woOIAoxIsgvz0lPHGOU7pjF/pHTS8lE2M3ixMF41UBdWT/uVG4VyrikJK0hbZp ANMTkWGeITdPtIEs873AmNugHQc01OnW4Tepj+jV9QwKdUq+iq1VRDxQMMnKRRXkkUfl w74A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1qpXWSCDrMjTrrN7DSUZrVNnGlngy6qbcHa+BWaBxfw=; b=VWFwpud+eRny5n9t9kS26HOLtp4o8dwvPtyMPPMLH3E1od4WznXrjrlMoehNkpwQNF Ncj8dlcets1CR0nKMSo74YvkVJmHF/mYmmsK3sFcAgbMSrQOTXJq/uoRXjqGVGmj2SqS 8SGkqn+y9GSk/E41eHGuaa5XdaSA6QfmOYKinV1cmy5fSykrPy7Y5w5RQk80eOITRoOx HXCnqJM2gil8MeFzXCGHk2inanHBlfGWpESDrQNeoSFdvZI8RVyP+xuiLggnxM1TL7Ch ytVbsEo2S9Q4zlx8BjqCEe3x++OduIWqAxxnfUxELcZS2UcwJot9+nYP6TzVRdXmFOph pGWw== X-Gm-Message-State: ANoB5pkgh6hj/Iwb+R00ILocerj/wLOju5vPVQtWM1EK9J0VDItpgf94 fLUHk3+ipANDBG65ARTaw6NR3Q== X-Received: by 2002:a17:902:8649:b0:187:3921:2b2d with SMTP id y9-20020a170902864900b0018739212b2dmr6440688plt.13.1668553899569; Tue, 15 Nov 2022 15:11:39 -0800 (PST) Received: from minbar.home.kylehuey.com (c-71-198-251-229.hsd1.ca.comcast.net. [71.198.251.229]) by smtp.gmail.com with ESMTPSA id f15-20020a62380f000000b0056c360af4e3sm9308372pfa.9.2022.11.15.15.11.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 15:11:39 -0800 (PST) From: Kyle Huey X-Google-Original-From: Kyle Huey To: Dave Hansen Cc: Linus Torvalds , Thomas Gleixner , Borislav Petkov , Ingo Molnar , x86@kernel.org, "H. Peter Anvin" , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra , Sean Christopherson , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Robert O'Callahan , David Manouchehri , Kyle Huey , Borislav Petkov , stable@vger.kernel.org Subject: [PATCH v7 5/6] x86/fpu: Emulate XRSTOR's behavior if the xfeatures PKRU bit is not set Date: Tue, 15 Nov 2022 15:09:31 -0800 Message-Id: <20221115230932.7126-6-khuey@kylehuey.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221115230932.7126-1-khuey@kylehuey.com> References: <20221115230932.7126-1-khuey@kylehuey.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749605782033895245?= X-GMAIL-MSGID: =?utf-8?q?1749605782033895245?= The hardware XRSTOR instruction resets the PKRU register to its hardware init value (namely 0) if the PKRU bit is not set in the xfeatures mask. Emulating that here restores the pre-5.14 behavior for PTRACE_SET_REGSET with NT_X86_XSTATE, and makes sigreturn (which still uses XRSTOR) and ptrace behave identically. KVM has never used XRSTOR and never had this behavior, so KVM opts-out of this emulation by passing a NULL pkru pointer to copy_uabi_to_xstate(). Fixes: e84ba47e313d ("x86/fpu: Hook up PKRU into ptrace()") Signed-off-by: Kyle Huey Cc: Dave Hansen Cc: Thomas Gleixner Cc: Borislav Petkov Cc: stable@vger.kernel.org # 5.14+ --- arch/x86/kernel/fpu/core.c | 8 ++++++++ arch/x86/kernel/fpu/xstate.c | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index 46b935bc87c8..8d0f6019c21d 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -404,6 +404,14 @@ int fpu_copy_uabi_to_guest_fpstate(struct fpu_guest *gfpu, const void *buf, if (ustate->xsave.header.xfeatures & ~xcr0) return -EINVAL; + /* + * Nullify @vpkru to preserve its current value if PKRU's bit isn't set + * in the header. KVM's odd ABI is to leave PKRU untouched in this + * case (all other components are eventually re-initialized). + */ + if (!(ustate->xsave.header.xfeatures & XFEATURE_MASK_PKRU)) + vpkru = NULL; + return copy_uabi_from_kernel_to_xstate(kstate, ustate, vpkru); } EXPORT_SYMBOL_GPL(fpu_copy_uabi_to_guest_fpstate); diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index bebc30c29ed3..193c6e95daa8 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -1219,8 +1219,14 @@ static int copy_from_buffer(void *dst, unsigned int offset, unsigned int size, * it is harmless. * 2. When called from ptrace the PKRU register will be restored from the * thread_struct's pkru field. A pointer to that is passed in @pkru. + * The kernel will restore it manually, so the XRSTOR behavior that resets + * the PKRU register to the hardware init value (0) if the corresponding + * xfeatures bit is not set is emulated here. * 3. When called from KVM the PKRU register will be restored from the vcpu's - * pkru field. A pointer to that is passed in @pkru. + * pkru field. A pointer to that is passed in @pkru. KVM hasn't used + * XRSTOR and hasn't had the PKRU resetting behavior described above. To + * preserve that KVM behavior, it passes NULL for @pkru if the xfeatures + * bit is not set. */ static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, const void __user *ubuf, u32 *pkru) @@ -1277,6 +1283,13 @@ static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, xpkru = __raw_xsave_addr(xsave, XFEATURE_PKRU); *pkru = xpkru->pkru; + } else { + /* + * KVM may pass NULL here to indicate that it does not need + * PKRU updated. + */ + if (pkru) + *pkru = 0; } /*