Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp1323783rwe; Thu, 25 Aug 2022 22:08:11 -0700 (PDT) X-Google-Smtp-Source: AA6agR50b7JfyelxUBckqS7J+PQlWZqtdecxJ3uckP/QUvRJdB9mCuRfKLAEInymtcfeqESPu9Vr X-Received: by 2002:a17:907:3d8e:b0:73d:13d8:61fb with SMTP id he14-20020a1709073d8e00b0073d13d861fbmr4396682ejc.731.1661490491571; Thu, 25 Aug 2022 22:08:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661490491; cv=none; d=google.com; s=arc-20160816; b=DnNrT1PMXMfKATy776Z/4ieIMb4w1+Kd3u3RZOIxOLiN3AObfFzRLI20H3hNwXk7uf XzIZyJ90IGUPYEmk+ibXD6y7g2gctLecSsAWjXac5PCGHEKaIuqnWTvQn7PMRUUKgRyC afLCErrCD9k0TaXsexohir+zeHpC79dPLATevNvt79nk4881VfzN0hPfVUFm3lM7/cXL TwK4ta1+rkToYQD8y0uZE7wkIiW4uDrt8Z+EwjEeC0qXlLhZrhlKzm+L1lv9w2YkdOl/ D/b/KPD6JDhqgJsR1LWAWReZCCjyuFhmKOtO2innpoahk5E5YrJGl6pSl55hcMpIriLO Qxuw== 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=baSv0Pn8NKV4CT1RoqMfXK68o2fQcY24x27HhvXMUKI=; b=IkhEAXEnWCLZ9o8ENESJxGNfhHWXATY279mae42LabRvcsM22dT4QmlLrvUGgLkjDi 2VttQMfasV9WZG9PkwN8W765ujkOT7SrlA/fQdHO5jsaeUjVbClmD2LK/nTnuz2AnSsy dQtyXAr9pLSJl/1OSx55RZIHBXF2XyI6lbcIHHZCuCf2ZAuPKUYuiMgj3f//bRVitt9l jkC6d+Z1/kn2u5v7Kpj7PiM0R4sxdtKoml9QsEb3d/dalFFxlQE5CXCP0orTmrIA16qD ctWDiewtR59C/YFFFxr8cLAsEt91apJCAWglO37savdfvHK7NUcUgpWjP6ROaiQVjV3d 0LdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kylehuey.com header.s=google header.b=b8WDemlJ; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i8-20020aa7c708000000b00447a1a08cc8si688089edq.507.2022.08.25.22.07.45; Thu, 25 Aug 2022 22:08:11 -0700 (PDT) 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=b8WDemlJ; 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 S244747AbiHZEsF (ORCPT + 99 others); Fri, 26 Aug 2022 00:48:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244648AbiHZEsC (ORCPT ); Fri, 26 Aug 2022 00:48:02 -0400 Received: from mail-yw1-x1129.google.com (mail-yw1-x1129.google.com [IPv6:2607:f8b0:4864:20::1129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23BC5CE446 for ; Thu, 25 Aug 2022 21:48:01 -0700 (PDT) Received: by mail-yw1-x1129.google.com with SMTP id 00721157ae682-33dbbf69b3dso9279217b3.1 for ; Thu, 25 Aug 2022 21:48:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kylehuey.com; s=google; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc; bh=baSv0Pn8NKV4CT1RoqMfXK68o2fQcY24x27HhvXMUKI=; b=b8WDemlJLBj3xJVS55uRQRlMy8umsRJbhI12sqSfOUsT5xTdCa1HUm4mqB0iqbIr0E zv/DDPBDFLkikkKkFsmxRWayUv0WIvQLVCd/yFWkHr01Sg9cnnspTdtZFbGN5fl4QAw3 wqOtb+LMi6DoOCmdVAB0ZQv4QMZMuimTMJDgdRi8r4d56suCWXh/Pc0c3d3Jnyg5fqGF 3XBfP608utZYOXm5DYkTwVqfL30d0HxuPjvxynt/WqjWbLXImnAIDS8klUgQGRAXe5Ci /kyFUvjCA03Snep08L14NN4YkqpeoVCxlqyGm226cbAb8vn6LifW5PdEXlTjlMIuiQ5j 7H3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=baSv0Pn8NKV4CT1RoqMfXK68o2fQcY24x27HhvXMUKI=; b=bF7oE/wJQMlfqe/AGNf/kjv0FFC8T1+kDaJ+81mgyDGUY8URE/p6c+YjHcUv47qnJd xQNzIfM6pXwPDucSUDQJN5oDuq+yh3+wb3DOu2TE01DeX7FG5xiOXwbVF4grVcdCzJ3j Ff3+ePvjolD9K6NuyvitcNW6bU7vB0AyHSfMuy7WtDrihR9cnToPF8N3t3gXVo0Y05Zq 9a0v5ZkZcP//Q1ywHzELMvVmLmkvPHnxYjF1AKnRzMKxbagxpouJTWQrAdl40yHprljA MA1OPtFTFsb8iJ3ylYA94WogiKNsNTNBrBaA8T1E0OYlbqoNNwXqTn9J2FyjpV9upQRh wYQQ== X-Gm-Message-State: ACgBeo1kW+gyATpQXVJruatVaPqKors/3AdWjUfHc0eQIDJZefBVFmXl xQPbnO7PDPnfQ+E3oSMRkvI7PWMI+RgZH5H6X5V8FQ== X-Received: by 2002:a0d:d0c1:0:b0:33d:77db:eadf with SMTP id s184-20020a0dd0c1000000b0033d77dbeadfmr6920136ywd.26.1661489280272; Thu, 25 Aug 2022 21:48:00 -0700 (PDT) MIME-Version: 1.0 References: <20220808141538.102394-1-khuey@kylehuey.com> <87ilmpzunz.ffs@tglx> In-Reply-To: From: Kyle Huey Date: Thu, 25 Aug 2022 21:47:49 -0700 Message-ID: Subject: Re: [PATCH v5 1/2] x86/fpu: Allow PKRU to be (once again) written by ptrace. To: Sean Christopherson Cc: Thomas Gleixner , Dave Hansen , Borislav Petkov , Ingo Molnar , x86@kernel.org, "H. Peter Anvin" , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, "Robert O'Callahan" , David Manouchehri , Borislav Petkov , kvm@vger.kernel.org, stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" 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,T_SCC_BODY_TEXT_LINE 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 On Thu, Aug 18, 2022 at 2:19 PM Sean Christopherson wrote: > > On Thu, Aug 18, 2022, Kyle Huey wrote: > > On Thu, Aug 18, 2022 at 3:57 AM Thomas Gleixner wrote: > > > On Mon, Aug 08 2022 at 07:15, Kyle Huey wrote: > > > > When management of the PKRU register was moved away from XSTATE, emulation > > > > of PKRU's existence in XSTATE was added for APIs that read XSTATE, but not > > > > for APIs that write XSTATE. This can be seen by running gdb and executing > > > > `p $pkru`, `set $pkru = 42`, and `p $pkru`. On affected kernels (5.14+) the > > > > write to the PKRU register (which gdb performs through ptrace) is ignored. > > > > > > > > There are three relevant APIs: PTRACE_SETREGSET with NT_X86_XSTATE, > > > > sigreturn, and KVM_SET_XSAVE. KVM_SET_XSAVE has its own special handling to > > > > make PKRU writes take effect (in fpu_copy_uabi_to_guest_fpstate). Push that > > > > down into copy_uabi_to_xstate and have PTRACE_SETREGSET with NT_X86_XSTATE > > > > and sigreturn pass in pointers to the appropriate PKRU value. > > > > > > > > This also adds code to initialize the PKRU value to the hardware init value > > > > (namely 0) if the PKRU bit is not set in the XSTATE header to match XRSTOR. > > > > This is a change to the current KVM_SET_XSAVE behavior. > > > > > > You are stating a fact here, but provide 0 justification why this is > > > correct. > > > > Well, the justification is that this *is* the behavior we want for > > ptrace/sigreturn, and it's very likely the existing KVM_SET_XSAVE > > behavior in this edge case is an oversight rather than intentional, > > and in the absence of confirmation that KVM wants the existing > > behavior (the KVM mailing list and maintainer are CCd) one correct > > code path is better than one correct code path and one buggy code > > path. > > Sorry, I missed the KVM-relevant flags. > > Hrm, the current behavior has been KVM ABI for a very long time. > > It's definitely odd because all other components will be initialized due to their > bits being cleared in the header during kvm_load_guest_fpu(), and it probably > wouldn't cause problems in practice as most VMMs likely do "all or nothing" loads. > But, in theory, userspace could save/restore a subset of guest XSTATE and rely on > the kernel not overwriting guest PKRU when its bit is cleared in the header. This seems extremely conservative, but ok. As you note, PKRU is the only XSTATE component you could theoretically do this subset save/restore with in the KVM ABI since all the others really do have their hardware behavior. > All that said, I don't see any reason to force KVM to change at this time, it's > trivial enough to handle KVM's oddities while providing sane behavior for others. > Nullify the pointer in the guest path and then update copy_uabi_to_xstate() to > play nice with a NULL pointer, e.g. > > /* > * 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 (!(kstate->regs.xsave.header.xfeatures & XFEATURE_MASK_PKRU)) > vpkru = NULL; You meant ustate->... here (since this is before the copy now), but yes, ok, I will do that. > return copy_uabi_from_kernel_to_xstate(kstate, ustate, vpkru); - Kyle