From patchwork Thu May 27 23:51:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Hansen X-Patchwork-Id: 12285771 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EA416C4707F for ; Thu, 27 May 2021 23:57:36 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8DFD860233 for ; Thu, 27 May 2021 23:57:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8DFD860233 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 124226B006C; Thu, 27 May 2021 19:57:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0D49F6B006E; Thu, 27 May 2021 19:57:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EDCED6B0070; Thu, 27 May 2021 19:57:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0053.hostedemail.com [216.40.44.53]) by kanga.kvack.org (Postfix) with ESMTP id B8B036B006C for ; Thu, 27 May 2021 19:57:35 -0400 (EDT) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 5FD3B181AF5CA for ; Thu, 27 May 2021 23:57:35 +0000 (UTC) X-FDA: 78188675670.10.D88CAB5 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf02.hostedemail.com (Postfix) with ESMTP id 3744341449C5 for ; Thu, 27 May 2021 23:57:29 +0000 (UTC) IronPort-SDR: 5bgoB0yKbqg4bVwf+jo2NVkWPtbIKVB/d9H3jPe+NNEgavOoZajUeXuPxnrDT/qjsCxrkRL79T z2Wl+z25TE7w== X-IronPort-AV: E=McAfee;i="6200,9189,9997"; a="288450809" X-IronPort-AV: E=Sophos;i="5.83,228,1616482800"; d="scan'208";a="288450809" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 May 2021 16:57:31 -0700 IronPort-SDR: EdBbJMdOe+AIAvP+bJlbPTifDhKTmzM3qg8+7htfJwckCp78t3RIrkyvxgwspQIHQK2Xq1uFGA nhkMOHUu2zEQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.83,228,1616482800"; d="scan'208";a="548362372" Received: from viggo.jf.intel.com (HELO localhost.localdomain) ([10.54.77.144]) by fmsmga001.fm.intel.com with ESMTP; 27 May 2021 16:57:31 -0700 Subject: [PATCH 0/5] x86/pkeys: PKRU manipulation bug fixes and cleanups To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org,Dave Hansen ,tglx@linutronix.de,mingo@redhat.com,bp@alien8.de,x86@kernel.org,luto@kernel.org,shuah@kernel.org,babu.moger@amd.com,dave.kleikamp@oracle.com,linuxram@us.ibm.com,bauerman@linux.ibm.com From: Dave Hansen Date: Thu, 27 May 2021 16:51:09 -0700 Message-Id: <20210527235109.B2A9F45F@viggo.jf.intel.com> Authentication-Results: imf02.hostedemail.com; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none); spf=none (imf02.hostedemail.com: domain of dave.hansen@linux.intel.com has no SPF policy when checking 192.55.52.43) smtp.mailfrom=dave.hansen@linux.intel.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 3744341449C5 X-Stat-Signature: ddody1d64qbt5k6emh6np3yqw4bqgqzf X-HE-Tag: 1622159849-891445 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Andy Lutomirski recently noted a probable bug in write_pkru(), but it was unclear if it was user-visible. A recent bug report in related code[1] forced me to take a look. Basically, manipulation of XSAVE state is too unstructured. get_xsave_addr() gives callers the impression they can read and write XSAVE state when there are a lot of pitfalls, like updates to xstate.features bits. As a result, more than one call site screws up the modification of PKRU in the XSAVE buffer. This series fixes that problem up and also hopefully carves out a less error-prone path that can be reused for other XSAVE features. This series: * Moves the PKRU manipulation to a more appropriate location, away from the page table code * Wraps get_xsave_addr() with more structured, less error-prone interfaces. * Conditionally hides a pkey debugfs file, eliminating the need for new runtime checks to work with the new interface. * Add a selftest to make it more likely to catch bugs like this in the future. This improved selftest catches this issue on Intel CPUs. Without the improvement, it only triggers on AMD. This has been lightly tested so far. It probably needs a tested-by from at least the AMD folks before anyone applies it. 1. https://lore.kernel.org/linux-kselftest/b2e0324a-9125-bb34-9e76-81817df27c48@amd.com/ --- arch/x86/include/asm/fpu/internal.h | 8 - arch/x86/include/asm/fpu/xstate.h | 130 +++++++++++++++++++++++++++ arch/x86/include/asm/pgtable.h | 29 ------ arch/x86/include/asm/processor.h | 7 + arch/x86/kernel/cpu/common.c | 6 - arch/x86/kernel/fpu/xstate.c | 2 arch/x86/kernel/process_64.c | 1 arch/x86/kvm/svm/sev.c | 1 arch/x86/kvm/x86.c | 1 arch/x86/mm/pkeys.c | 13 +- tools/testing/selftests/vm/Makefile | 4 tools/testing/selftests/vm/pkey-x86.h | 1 tools/testing/selftests/vm/protection_keys.c | 73 +++++++++++++++ 13 files changed, 227 insertions(+), 49 deletions(-) Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: x86@kernel.org Cc: Andy Lutomirski Cc: Shuah Khan Cc: Babu Moger Cc: Dave Kleikamp Cc: Ram Pai Cc: Thiago Jung Bauermann