From patchwork Wed Mar 10 22:00:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu-cheng Yu X-Patchwork-Id: 12129565 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=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT 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 31926C433DB for ; Wed, 10 Mar 2021 22:02:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CFD8164E27 for ; Wed, 10 Mar 2021 22:02:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CFD8164E27 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id DCCEA8D022C; Wed, 10 Mar 2021 17:01:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D099B8D022F; Wed, 10 Mar 2021 17:01:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B34B48D022C; Wed, 10 Mar 2021 17:01:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0047.hostedemail.com [216.40.44.47]) by kanga.kvack.org (Postfix) with ESMTP id 96BD98D022B for ; Wed, 10 Mar 2021 17:01:24 -0500 (EST) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 5DDFE3489 for ; Wed, 10 Mar 2021 22:01:24 +0000 (UTC) X-FDA: 77905336488.20.B3771A9 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by imf12.hostedemail.com (Postfix) with ESMTP id D7F5F3D6 for ; Wed, 10 Mar 2021 22:01:19 +0000 (UTC) IronPort-SDR: j6Y8tavFODh0ud168LETKQ27pwfasd/BkIdZ8GhJ8limsegYty9KvYu61k9pfQgDfl1iMVFIVY 6qmWTfnrtLyQ== X-IronPort-AV: E=McAfee;i="6000,8403,9919"; a="167846456" X-IronPort-AV: E=Sophos;i="5.81,238,1610438400"; d="scan'208";a="167846456" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Mar 2021 14:01:17 -0800 IronPort-SDR: pGEGdbO/oLxHtOnGhlWhqWfXV5ZfgoA8mCzm2Hugng3HBk1losY2D9hsI4UZFG8MJgfGznIgb0 IZzfYpjIqKSQ== X-IronPort-AV: E=Sophos;i="5.81,238,1610438400"; d="scan'208";a="403847642" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Mar 2021 14:01:17 -0800 From: Yu-cheng Yu 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" , Vedvyas Shanbhogue , Dave Martin , Weijiang Yang , Pengfei Xu , Haitao Huang Cc: Yu-cheng Yu Subject: [PATCH v22 20/28] mm/mprotect: Exclude shadow stack from preserve_write Date: Wed, 10 Mar 2021 14:00:38 -0800 Message-Id: <20210310220046.15866-21-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210310220046.15866-1-yu-cheng.yu@intel.com> References: <20210310220046.15866-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: D7F5F3D6 X-Stat-Signature: kqq558yjr5jbaz8m33c6nddnmp4ben7u Received-SPF: none (intel.com>: No applicable sender policy available) receiver=imf12; identity=mailfrom; envelope-from=""; helo=mga12.intel.com; client-ip=192.55.52.136 X-HE-DKIM-Result: none/none X-HE-Tag: 1615413679-474319 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: 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 (RW=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(). Signed-off-by: Yu-cheng Yu --- mm/huge_memory.c | 7 ++++++- mm/mprotect.c | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index de8c8b94e840..6b7dc5dd94de 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1816,12 +1816,17 @@ int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, bool prot_numa = cp_flags & MM_CP_PROT_NUMA; bool uffd_wp = cp_flags & MM_CP_UFFD_WP; bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE; + bool shstk = arch_shadow_stack_mapping(vma->vm_flags); ptl = __pmd_trans_huge_lock(pmd, vma); if (!ptl) return 0; - preserve_write = prot_numa && pmd_write(*pmd); + /* + * Preserve only normal writable huge PMD, but not shadow + * stack (RW=0, Dirty=1). + */ + preserve_write = prot_numa && pmd_write(*pmd) && !shstk; ret = 1; #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION diff --git a/mm/mprotect.c b/mm/mprotect.c index c1ce78d688b6..e69278b346a9 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -75,7 +75,14 @@ static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd, oldpte = *pte; if (pte_present(oldpte)) { pte_t ptent; - bool preserve_write = prot_numa && pte_write(oldpte); + bool shstk = arch_shadow_stack_mapping(vma->vm_flags); + bool preserve_write; + + /* + * Preserve only normal writable PTE, but not shadow + * stack (RW=0, Dirty=1). + */ + preserve_write = prot_numa && pte_write(oldpte) && !shstk; /* * Avoid trapping faults against the zero or KSM