From patchwork Fri Nov 4 22:35:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13032658 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E2890C4332F for ; Fri, 4 Nov 2022 22:39:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 994A88E000E; Fri, 4 Nov 2022 18:39:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 859B48E0007; Fri, 4 Nov 2022 18:39:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 636F28E000E; Fri, 4 Nov 2022 18:39:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 470FC8E0007 for ; Fri, 4 Nov 2022 18:39:42 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 1E80F140BEE for ; Fri, 4 Nov 2022 22:39:42 +0000 (UTC) X-FDA: 80097228204.10.1741C04 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by imf14.hostedemail.com (Postfix) with ESMTP id 8BF60100002 for ; Fri, 4 Nov 2022 22:39:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1667601581; x=1699137581; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=4aVCw5ODNp+SkJKgvPAW6TwS6KUC24UPNM4SuYiLUfg=; b=DY5mueriNsSzy8X/nVqMhLLvMcSMl8xtDn5pMZAcQ7z7fIe3vewxFPYn j/gzO946XcBRVmrh4q+7KlCi81x4u0Lc8ncVP6L8uTiRI+s3vwT8DhdY/ wT/x4BJFJLTraopnWPqC/O89qDEWwVBiHFMGPrYaJy32qk4wpILbc7Ozv jB3uej2dxcROvLnOC7VJX52wmyTjrEQIpjVRvE38Go7cgH1pSc+FSkuDX pM12GSJ1Et5bZ4vmDDE8gOGLqHQvo7311dKByC2oI4u0hQTXrUTBziOjN 8GUIFw0b6EN0qvFv1WBDGHzADpHVMz87HIEovuPoLz8GOZFMM0fDWa18J w==; X-IronPort-AV: E=McAfee;i="6500,9779,10521"; a="297559668" X-IronPort-AV: E=Sophos;i="5.96,138,1665471600"; d="scan'208";a="297559668" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Nov 2022 15:39:40 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10521"; a="668514089" X-IronPort-AV: E=Sophos;i="5.96,138,1665471600"; d="scan'208";a="668514089" Received: from adhjerms-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.212.227.68]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Nov 2022 15:39:39 -0700 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V . Shankar" , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v3 20/37] mm/mprotect: Exclude shadow stack from preserve_write Date: Fri, 4 Nov 2022 15:35:47 -0700 Message-Id: <20221104223604.29615-21-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221104223604.29615-1-rick.p.edgecombe@intel.com> References: <20221104223604.29615-1-rick.p.edgecombe@intel.com> ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=DY5mueri; spf=pass (imf14.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 134.134.136.20 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667601581; a=rsa-sha256; cv=none; b=G74Kc7jurUb+fK/rkf1IHNiJV+W3uyqiUglCokh4bN9O2CgxUpqeGP54tAgQK5REvAkYSg AzHGjZRBmfpZqHKMgnZbnnNU/U3U74xEVmpsPvHnqKo4+GZqRcpqHluF1U5V5jctKYUYuX BI4RAq1UK2M+C2QmCfkqQV7HC2GvjP4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1667601581; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references:dkim-signature; bh=opoH7NecdpHP4Fcjl0236tgId5JC2cqlPok8ay5lAPU=; b=C10dE6gP/iEk4k1KDKU8kPiLoaskmdQA3GNXqqbmdR5VTvUQl/Mk3SJGHZYP2gIoBKBSXC 1yyhqyhNpJOITxiDtrgSxJ7udEe6C3Gz4SZYB6p8/Sxth/cesVJdn/x0EcJiv8FTAkSL02 VKxxLAQDwgi13VdZAj75GNlt9ENbjP0= X-Stat-Signature: 9aqeye7zfsw6fhx7iw3jggfj11rp4fdw X-Rspamd-Queue-Id: 8BF60100002 Authentication-Results: imf14.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=DY5mueri; spf=pass (imf14.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 134.134.136.20 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1667601581-627100 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: From: Yu-cheng Yu The x86 Control-flow Enforcement Technology (CET) feature includes a new type of memory called shadow stack. This shadow stack memory has some unusual properties, which requires some core mm changes to function properly. In change_pte_range(), when a PTE is changed for prot_numa, _PAGE_RW is preserved to avoid the additional write fault after the NUMA hinting fault. However, pte_write() now includes both normal writable and shadow stack (Write=0, Dirty=1) PTEs, but the latter does not have _PAGE_RW and has no need to preserve it. Exclude shadow stack from preserve_write test, and apply the same change to change_huge_pmd(). Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Reviewed-by: Kirill A. Shutemov Signed-off-by: Rick Edgecombe --- Yu-cheng v25: - Move is_shadow_stack_mapping() to a separate line. Yu-cheng v24: - Change arch_shadow_stack_mapping() to is_shadow_stack_mapping(). mm/huge_memory.c | 7 +++++++ mm/mprotect.c | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 73b9b78f8cf4..7643a4db1b50 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1803,6 +1803,13 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, return 0; preserve_write = prot_numa && pmd_write(*pmd); + + /* + * Preserve only normal writable huge PMD, but not shadow + * stack (RW=0, Dirty=1). + */ + if (vma->vm_flags & VM_SHADOW_STACK) + preserve_write = false; ret = 1; #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION diff --git a/mm/mprotect.c b/mm/mprotect.c index 668bfaa6ed2a..ea82ce5f38fe 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -115,6 +115,13 @@ static unsigned long change_pte_range(struct mmu_gather *tlb, pte_t ptent; bool preserve_write = prot_numa && pte_write(oldpte); + /* + * Preserve only normal writable PTE, but not shadow + * stack (RW=0, Dirty=1). + */ + if (vma->vm_flags & VM_SHADOW_STACK) + preserve_write = false; + /* * Avoid trapping faults against the zero or KSM * pages. See similar comment in change_huge_pmd.