Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1688572pxb; Fri, 20 Aug 2021 11:22:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyQMsXwHmIMWFW3ZmeDqC3N715FSmtvmi8D3xdqU8rXcsxSdr2Zmfpe5VQH/HH12VSN26Zw X-Received: by 2002:a50:cdd4:: with SMTP id h20mr23953890edj.173.1629483747284; Fri, 20 Aug 2021 11:22:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629483747; cv=none; d=google.com; s=arc-20160816; b=U7cbRj5ujlHJNum51CJlBVZ0ObgM0fDRcd44HlzzG64jBn2OJ6KTDuzAZ0rbYiyPLl h9JDi2q/+bGH4UofTLiiwlR/B9vYLFpxdOgVT39nGLV+4PcA19Is1Wt59dCkKgtse+TS lV5WBcJiJcEkEMdg2U1FAx33/WMfpMxUoGyhmr1pyvkbPyDo/o4fdDSYb9EJu5LOH3Je kGh6Y3Knv/+16P6RqXsoR6fG17jO/mnfRL1UOlJXl5gyuMnrhPfZiRGIzanfKtXKAEfX g/hUaS1Zd7F4LeJIfKOCVTufLfV+UY3pDLe79SdpiqvI5Co6fXuuqukkVzvgtyMYBbXT vNiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=phz1CHm7DY0yFP6VvcsBBIJ/MBvKJJOWVRSFS2uq5Sw=; b=Gg+d+Xbz8lcx+nUHO7KouYlbMPez1nRjP1t7y2eNN/YIt/6lWaB0P3qAna/ksipCKZ +cQuUUaw0AnuaDQaHSyjpsvETasb6rTx2nTyEXeMs0w61RPGQW/G7AbiFSWt8/IhpdkP /HAFq92LaFyLtsBWvUYe3iBSwM27aBhQUIDffXIqX5fwzHE60j+PxhmgTG8XAMQFuPTe 9qvG7BQ7CuvBjGIAcYtOr0Yn4hLwVMcIeixmJLjc8XT1J70zhOfc6A9tc5I9NVrLCGMf PmEQ3wZKyqrZ89zmPwBXgSFQFVwigLiI7Fleh8qkEGSEkumUKQ/3xVXhxaMAJlj0qRbO IMXQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id g17si7606474ejo.93.2021.08.20.11.22.02; Fri, 20 Aug 2021 11:22:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S238469AbhHTSUu (ORCPT + 99 others); Fri, 20 Aug 2021 14:20:50 -0400 Received: from mga05.intel.com ([192.55.52.43]:22426 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238062AbhHTSUS (ORCPT ); Fri, 20 Aug 2021 14:20:18 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10082"; a="302407820" X-IronPort-AV: E=Sophos;i="5.84,338,1620716400"; d="scan'208";a="302407820" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Aug 2021 11:18:49 -0700 X-IronPort-AV: E=Sophos;i="5.84,338,1620716400"; d="scan'208";a="533074728" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Aug 2021 11:18:48 -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 , 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" , Dave Martin , Weijiang Yang , Pengfei Xu , Haitao Huang , Rick P Edgecombe Cc: Yu-cheng Yu , "Kirill A . Shutemov" Subject: [PATCH v29 16/32] x86/mm: Update maybe_mkwrite() for shadow stack Date: Fri, 20 Aug 2021 11:11:45 -0700 Message-Id: <20210820181201.31490-17-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210820181201.31490-1-yu-cheng.yu@intel.com> References: <20210820181201.31490-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When serving a page fault, maybe_mkwrite() makes a PTE writable if its vma has VM_WRITE. A shadow stack vma has VM_SHADOW_STACK. Its PTEs have _PAGE_DIRTY, but not _PAGE_WRITE. In fork(), _PAGE_DIRTY is cleared to cause copy-on-write, and in the page fault handler, _PAGE_DIRTY is restored and the shadow stack page is writable again. Introduce an x86 version of maybe_mkwrite(), which sets proper PTE bits according to VM flags. Apply the same changes to maybe_pmd_mkwrite(). Signed-off-by: Yu-cheng Yu Reviewed-by: Kirill A. Shutemov Cc: Kees Cook --- v29: - Remove likely()'s. --- arch/x86/include/asm/pgtable.h | 6 ++++++ arch/x86/mm/pgtable.c | 20 ++++++++++++++++++++ include/linux/mm.h | 2 ++ mm/huge_memory.c | 2 ++ 4 files changed, 30 insertions(+) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index df4ce715560a..bfe4ea2b652d 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -280,6 +280,9 @@ static inline int pmd_trans_huge(pmd_t pmd) return (pmd_val(pmd) & (_PAGE_PSE|_PAGE_DEVMAP)) == _PAGE_PSE; } +#define maybe_pmd_mkwrite maybe_pmd_mkwrite +extern pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma); + #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD static inline int pud_trans_huge(pud_t pud) { @@ -1632,6 +1635,9 @@ static inline bool arch_faults_on_old_pte(void) return false; } +#define maybe_mkwrite maybe_mkwrite +extern pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma); + #endif /* __ASSEMBLY__ */ #endif /* _ASM_X86_PGTABLE_H */ diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index 3481b35cb4ec..c22c8e9c37e8 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -610,6 +610,26 @@ int pmdp_clear_flush_young(struct vm_area_struct *vma, } #endif +pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_WRITE) + pte = pte_mkwrite(pte); + else if (vma->vm_flags & VM_SHADOW_STACK) + pte = pte_mkwrite_shstk(pte); + return pte; +} + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_WRITE) + pmd = pmd_mkwrite(pmd); + else if (vma->vm_flags & VM_SHADOW_STACK) + pmd = pmd_mkwrite_shstk(pmd); + return pmd; +} +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + /** * reserve_top_address - reserves a hole in the top of kernel address space * @reserve - size of hole to reserve diff --git a/include/linux/mm.h b/include/linux/mm.h index 4a9985e50819..4548f75cef14 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1015,12 +1015,14 @@ void free_compound_page(struct page *page); * pte_mkwrite. But get_user_pages can cause write faults for mappings * that do not have writing enabled, when used by access_process_vm. */ +#ifndef maybe_mkwrite static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) { if (likely(vma->vm_flags & VM_WRITE)) pte = pte_mkwrite(pte); return pte; } +#endif vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page); void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr); diff --git a/mm/huge_memory.c b/mm/huge_memory.c index afff3ac87067..c8dd5913884e 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -491,12 +491,14 @@ static int __init setup_transparent_hugepage(char *str) } __setup("transparent_hugepage=", setup_transparent_hugepage); +#ifndef maybe_pmd_mkwrite pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) { if (likely(vma->vm_flags & VM_WRITE)) pmd = pmd_mkwrite(pmd); return pmd; } +#endif #ifdef CONFIG_MEMCG static inline struct deferred_split *get_deferred_split_queue(struct page *page) -- 2.21.0