From patchwork Thu Jan 19 21:22:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13108796 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 0F469C46467 for ; Thu, 19 Jan 2023 21:23:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F116900003; Thu, 19 Jan 2023 16:23:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 37A4E900002; Thu, 19 Jan 2023 16:23:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1CC40900003; Thu, 19 Jan 2023 16:23:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 02D19900002 for ; Thu, 19 Jan 2023 16:23:57 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id CDB8C12040C for ; Thu, 19 Jan 2023 21:23:56 +0000 (UTC) X-FDA: 80372826072.16.DC2AECF Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf16.hostedemail.com (Postfix) with ESMTP id C9E0D180016 for ; Thu, 19 Jan 2023 21:23:54 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=gGM0ifSG; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674163435; 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=cL1+A7sEmRT22WV4GTGRusW0jLfRlihJHMYsiIHNplM=; b=FSTmOgYvdSwheqbS/0lNGSpmOLHKwcr7N7FzP+2FAWbM6L0+CQIZppzrjPmTu+VkJ7R3Lv 7O23qQ2CkRvA2fwUerMnqBZwJqzgYYFcqAyH+N9cVzabZRduGbKAi1Ik+v9qpVim+fPcW+ S0aILrqIGWcW4kAH6B6YBhyf/ulsX04= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=gGM0ifSG; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 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=1674163435; a=rsa-sha256; cv=none; b=K4ttf4YG1NRyUdW+aWXiidjDwDmTbut9msgWhQocIfTG42R31TkAnyOonnS24VB7lpHOL8 p77KEBwZLCQvPvhwZX12WRuMKARbrXtq4joYqa+D/a7LecFQ6hPphRyDfcV1fLXgIOGtCt fw2LT4x04paBU0MHpcPsQdAhL14fvj4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163434; x=1705699434; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=NJhAPcbhUYo6SN1rH0mb7F3rAzE882GPxl5rAvSEEz0=; b=gGM0ifSGeDrGrb8XwFdzdKI6mtwMt/YCTxjuVOiaArQ3OwekNnZDGH7W vwphd1Je9fcWqhIGrKMT8f54uFNgPmACduHsHg7UjiPmiMxg9LBrfPqvf g/j3defe9T+0I16qBIa+MEGVRX/tKi4btiFCY4acaa7o4BigLOxAh7USF w9DRjXFTabJ0Msh9C4dYT+QAIRI4bzftlWeEhS2fIk+L5nRvK3pjlVp0l nfoLIAcD4W/PzzWzyc80CdI0RiPWxaZ1dbyAhB8Vr9r5REPmu1auyXRNN uMwjeYI/LVk+VuL2fovVejoZJuw2fy049EdA+Q6FI67A7egC1tSSZqOKs w==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119585" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119585" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jan 2023 13:23:54 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139082" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139082" Received: from hossain3-mobl.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.252.128.187]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jan 2023 13:23:52 -0800 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 , 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, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com Cc: rick.p.edgecombe@intel.com, David Hildenbrand , Yu-cheng Yu Subject: [PATCH v5 18/39] mm: Handle faultless write upgrades for shstk Date: Thu, 19 Jan 2023 13:22:56 -0800 Message-Id: <20230119212317.8324-19-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230119212317.8324-1-rick.p.edgecombe@intel.com> References: <20230119212317.8324-1-rick.p.edgecombe@intel.com> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: C9E0D180016 X-Stat-Signature: 8cgg1zxhgxxroywqxa6yutxfqmak53es X-Rspam-User: X-HE-Tag: 1674163434-857141 X-HE-Meta: U2FsdGVkX19PPFHGQjr5ZEyOoumsNmwqNbrVRxwwRDbyF+55YbXiybaTI2ny1Z+1jQJjfPlnQxHYsIjvk1dvmwYnAD1APKSNMxAG79iVQ6fxObCgjVa8kugevLIpP2Z2BYbRW3IktWapj0ZGEQ3gVanMNmsnfj2BVfXS4GdDAovHUf51FFq0DqicqWzKJNFcBSVRgdvQPuLmBY6kZB7qhqHalK7wv1JXv5nS4vwTmpJnl3icfZ09WbsZzl6BtI2R//Jm8u6VB76FfGzwg5hH83vTDF4LRyWYSIW3X5jnE1hmeCW7gWd0LVQkPpwuAHriklgl/l1scpEkd3t/39ufN0ectXIOk4rfNxolKflGETkXRwvk1XEv5zT/TJbBCtJkDQxkf9IIoSveJL1x+gH8YTLIyWLvKcgYoaHEDopB+9OEH+bFtHPZWF5tq2jZGWqQ3gyIZMcP2wgZnh27XeLDU8ctzE6X4bXRYgr3fuhlpxXsFd4MCCxAfTiodS0OSw2/V4KxAl0s5V9n6S9ciobFmc+vQrnre2jyk83uH8GIEnoBQBL5toNeSCgLMQcqMyHpR8/nWm2ENraaN0jyNkX+R5R8Ej/30WS2fF9CmA5ULAXHNsQTV63+KG5TjrEXV9Z6iLP9KNiLusIKK8+4eu1VWEkUoDdjNbRqHS85LlNIkUc1K+xbPV4Ywyo+MhKPOHxvvCiqMXn0NzzkjXs95Gsg8AihmR2PipFlT6z052TrNINo1TCVlAHyrrszyzyXloNLK77vx6YuCiyRKQa8sZjXM5hYfsuVTYjYXZIqw4a4ECt/1wVnloNP4Xg8MjFDqm3wGc7wgpXn6QWcHHNGLIBf121E1bCz3Ar/4KndCijvE+N66OgsQ05VXNY5RejVDYfs2wbYHbSU6dMdlUTJPCPWIHreqkraA7JzI29zYq6fmR7hAt26yODfvo7CyxcQPQEyu676N+EB560LdXZp34r K9/5Rrei ZvQMZRFwtzD2OTgXuTVutA07UGQhAmMoaLjl3ToBn41184oseaUCd8C9rm1PFBwnlxEI84vRHzTU0EyetAk4YqKskyjmuzY+w//bHR0xkwOgLISCDoh/W5VxjXnhfGwaK/T3ZB3gesz9OIEH25zz0BxjNpgi8MiUkO9MSHeGY4/N+3utCysd6b4akDh7Z0zqDBJgphs0HP2q3UwjZROu3nwUHqBOCkaFHiQBSiyzRgwK1KOcqu2yKnzS3m60K+qv2vSNu 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: 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. Since shadow stack memory can be changed from userspace, is both VM_SHADOW_STACK and VM_WRITE. But it should not be made conventionally writable (i.e. pte_mkwrite()). So some code that calls pte_mkwrite() needs to be adjusted. One such case is when memory is made writable without an actual write fault. This happens in some mprotect operations, and also prot_numa faults. In both cases code checks whether it should be made (conventionally) writable by calling vma_wants_manual_pte_write_upgrade(). One way to fix this would be have code actually check if memory is also VM_SHADOW_STACK and in that case call pte_mkwrite_shstk(). But since most memory won't be shadow stack, just have simpler logic and skip this optimization by changing vma_wants_manual_pte_write_upgrade() to not return true for VM_SHADOW_STACK_MEMORY. This will simply handle all cases of this type. Cc: David Hildenbrand Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Reviewed-by: Kirill A. Shutemov Signed-off-by: Rick Edgecombe --- v5: - Update solution after the recent removal of pte_savedwrite() v4: - Add "why" to comments in code (Peterz) 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(). include/linux/mm.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index e15d2fc04007..139a682d243b 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2181,7 +2181,7 @@ static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struct *vma */ if (vma->vm_flags & VM_SHARED) return vma_wants_writenotify(vma, vma->vm_page_prot); - return !!(vma->vm_flags & VM_WRITE); + return (vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHADOW_STACK); } bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,