Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp36198imm; Thu, 30 Aug 2018 07:46:48 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZNbhHjk4CCkWvfE6yltNZ2keS7gtIV0nDmjL3oiSyqI7D5smQO3ddO3d4cQpipw7cAWsjm X-Received: by 2002:a62:da0b:: with SMTP id c11-v6mr10858685pfh.68.1535640408154; Thu, 30 Aug 2018 07:46:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535640408; cv=none; d=google.com; s=arc-20160816; b=LWJxjdKdcTwmIQUq/xIB50E6iGSqK73TohOtGSzv5yimHIKEnVyh/1x3mSVN474fuk vfHiHb3pDvbL4q9J2vAl5diI0kSjDI84UM+a53j8n+E3AQLCHLangr+c0jkvSdtQzQer J3+3HCK3diXQJVTTeHqUxsjlsfgCsdkU5/Tys/p4+Tu77V2zHEBxqTqhqpMKdcYvmxOD p5NO/mbkqV4nIJWSD4qIrUcBh11TRftSKFJzTAFd/z31Db1/49Ozkxa4EPYOf367GRl1 gDEiH9UEXbRSEPEGAhXZiYQzRSZZASw8pnUesmx+9eIba4qq0M4N/2EoafQsXA+kHZ/A y6xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=Bzwo99A4d1pJDwCeY9DYj8i9R2GNeR2Ec6iJjwicYHg=; b=DlhD72OTfrEkw+ZlQak1YvoJpZ6BVMnRkMydJJzhQQyPFygsxTCt212zZ5KxmDL2Yb PRJJHQkhxn9iv2kycf6ENEBO+eU4Np7bmKxdhpaaroA9U/KWu5UN9yNvHm1W5MtnQKXm YLa13Mwbdw9EwEHXdYJcx5UEmOY2ISiuRYu2qIVkM5cUlkeaVgUFh0PFje0HlvXJVd9y 3IEgL11xI63ItEu6d2QXXpjZwuoaTPRxkgp55yWkdkEWTuoRh4Xam+C/Llr8TQ5aIwR+ dJ9awQh/fa9zuwiwVNTc+HM0lXMES5R6Uyh/rmZ2fxbVc3rgx+1OnL5CEQrTv7RXROd5 J+WA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g34-v6si5072829pgm.31.2018.08.30.07.46.32; Thu, 30 Aug 2018 07:46:48 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729640AbeH3SqU (ORCPT + 99 others); Thu, 30 Aug 2018 14:46:20 -0400 Received: from mga01.intel.com ([192.55.52.88]:38433 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729545AbeH3SqU (ORCPT ); Thu, 30 Aug 2018 14:46:20 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Aug 2018 07:43:44 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,307,1531810800"; d="scan'208";a="67186715" Received: from 2b52.sc.intel.com ([143.183.136.52]) by fmsmga008.fm.intel.com with ESMTP; 30 Aug 2018 07:43:41 -0700 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 , Cyrill Gorcunov , Dave Hansen , Florian Weimer , "H.J. Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , "Ravi V. Shankar" , Vedvyas Shanbhogue Cc: Yu-cheng Yu Subject: [RFC PATCH v3 14/24] mm: Handle shadow stack page fault Date: Thu, 30 Aug 2018 07:38:54 -0700 Message-Id: <20180830143904.3168-15-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180830143904.3168-1-yu-cheng.yu@intel.com> References: <20180830143904.3168-1-yu-cheng.yu@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When a task does fork(), its shadow stack must be duplicated for the child. However, the child may not actually use all pages of of the copied shadow stack. This patch implements a flow that is similar to copy-on-write of an anonymous page, but for shadow stack memory. A shadow stack PTE needs to be RO and dirty. We use this dirty bit requirement to effect the copying of shadow stack pages. In copy_one_pte(), we clear the dirty bit from the shadow stack PTE. On the next shadow stack access to the PTE, a page fault occurs. At that time, we then copy/re-use the page and fix the PTE. Signed-off-by: Yu-cheng Yu --- arch/x86/mm/pgtable.c | 10 ++++++++++ include/asm-generic/pgtable.h | 7 +++++++ mm/memory.c | 3 +++ 3 files changed, 20 insertions(+) diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index e848a4811785..c63261128ac3 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -872,3 +872,13 @@ int pmd_free_pte_page(pmd_t *pmd, unsigned long addr) #endif /* CONFIG_X86_64 */ #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ + +#ifdef CONFIG_X86_INTEL_SHADOW_STACK_USER +inline pte_t pte_set_vma_features(pte_t pte, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_SHSTK) + return pte_mkdirty_shstk(pte); + else + return pte; +} +#endif /* CONFIG_X86_INTEL_SHADOW_STACK_USER */ diff --git a/include/asm-generic/pgtable.h b/include/asm-generic/pgtable.h index aa5271717126..558a485617cd 100644 --- a/include/asm-generic/pgtable.h +++ b/include/asm-generic/pgtable.h @@ -1146,6 +1146,13 @@ static inline bool pmd_dirty_hw(pmd_t pmd) { return false; } + +static inline pte_t pte_set_vma_features(pte_t pte, struct vm_area_struct *vma) +{ + return pte; +} +#else +pte_t pte_set_vma_features(pte_t pte, struct vm_area_struct *vma); #endif #endif /* _ASM_GENERIC_PGTABLE_H */ diff --git a/mm/memory.c b/mm/memory.c index c467102a5cbc..9b4e11944b5d 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2462,6 +2462,7 @@ static inline void wp_page_reuse(struct vm_fault *vmf) flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); entry = pte_mkyoung(vmf->orig_pte); entry = maybe_mkwrite(pte_mkdirty(entry), vma); + entry = pte_set_vma_features(entry, vma); if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) update_mmu_cache(vma, vmf->address, vmf->pte); pte_unmap_unlock(vmf->pte, vmf->ptl); @@ -2535,6 +2536,7 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf) flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); entry = mk_pte(new_page, vma->vm_page_prot); entry = maybe_mkwrite(pte_mkdirty(entry), vma); + entry = pte_set_vma_features(entry, vma); /* * Clear the pte entry and flush it first, before updating the * pte with the new entry. This will avoid a race condition @@ -3187,6 +3189,7 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) entry = mk_pte(page, vma->vm_page_prot); if (vma->vm_flags & VM_WRITE) entry = pte_mkwrite(pte_mkdirty(entry)); + entry = pte_set_vma_features(entry, vma); vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address, &vmf->ptl); -- 2.17.1