From patchwork Mon Nov 19 21:47:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu-cheng Yu X-Patchwork-Id: 10689545 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7F90213BB for ; Mon, 19 Nov 2018 21:55:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7199C2A583 for ; Mon, 19 Nov 2018 21:55:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 627A12A6EE; Mon, 19 Nov 2018 21:55:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D363C2A583 for ; Mon, 19 Nov 2018 21:55:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F31116B1C9C; Mon, 19 Nov 2018 16:54:28 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id EB9FB6B1C9D; Mon, 19 Nov 2018 16:54:28 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C966B6B1C9E; Mon, 19 Nov 2018 16:54:28 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) by kanga.kvack.org (Postfix) with ESMTP id 827AD6B1C9C for ; Mon, 19 Nov 2018 16:54:28 -0500 (EST) Received: by mail-pf1-f197.google.com with SMTP id g63-v6so27543532pfc.9 for ; Mon, 19 Nov 2018 13:54:28 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=HLd5GusG71aEYLdIrdzVRcaP/S9spbOy4f1Vn6OuBlU=; b=P5UbOMrT1ElP7aXNZyi2CxS2A6IxcbEZ9uXDRG9C1JwXf0pqz6/5rJKKQqI+0PhunG LNykTBX9ZBPOmbupP8xiS147jKCoE9ju+9hZTaUnOhKf7TmzF0ks2OHY+UPFCihVFIYs 5o3vcDGgB26UdhgTIHiyXjYMCCLBJPZp0SgsOlIqokKetGTcABz3CZOOkDN3/Rtf9OMK /DKKq/TQVsYNJjg8HbR3aikVfzuOyGWQsWTbuzqQAstJ5M3nOIOwZCJ5AGtzbcKiS4Ia efjy5lq1rJ+bTvhHcQecIVMtFSMiobhpwr4KzKu5UlzevJGQB6/GqNV5s42LDBm30J7I bZaw== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Gm-Message-State: AGRZ1gJL5EICAAT5NwB17a0lEzrb1JQCankNUon++HcOl8t5L36jlnUn eqLSI78ee42fVP/BmN9g2iHjheAwaIhi/cA/Fz60C3o4COZy4SKw26aibnsRXIIlk1QW2OP7fhe wKeEog0Yj9OZlVZexAi+I/PMCQRptsklJIzqiMSHQHNgK18nvaB05Z5y8KHFXn+D/vQ== X-Received: by 2002:a63:1c09:: with SMTP id c9mr21370237pgc.200.1542664468215; Mon, 19 Nov 2018 13:54:28 -0800 (PST) X-Google-Smtp-Source: AJdET5faftZYGEAGBkTx/eiMvIGSLXnHdb3Kl9vJNiXvJcfPHuvkJ/qMjYjH7KXD8tTFAYa2zceR X-Received: by 2002:a63:1c09:: with SMTP id c9mr21370213pgc.200.1542664467441; Mon, 19 Nov 2018 13:54:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542664467; cv=none; d=google.com; s=arc-20160816; b=GzkoYOe7LVBKBIdMO6lgcWTLNPOmxcDkNUFt6on+8ixprpfUFIBJqe/DnX988PtByE 4exE85ATDV7p46iw1Mmhkd61uWjWBOnPO7i5/avDLU4i5cLVeM52iYtDMPXM4OGZLVPk LtO2KJFoWJ6x3lKr/a87YlHn+wyxR+9zFGzCpqTf/4M37vEtOZVJlZuZ3dvF35D4AaWc YKAR6KHdRPMIQakrccxSFC4Q9WyfItXZBbI2DlkOApbbKa6eFID1oilvYj9Ohh4gjQhb +9hHEeJqSSLGI/XRkDRjMfPcvESm0pfQXjW3TwWgGN7hxwybXWPrAcBp24IeFbOqyfXz zATg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=HLd5GusG71aEYLdIrdzVRcaP/S9spbOy4f1Vn6OuBlU=; b=EotLB0QcvPxNlHme590gE0jtTO27TgiZNTbF3cD0Yfbwl+je1X2pO8tuplLXZmDPH6 6BF9H6xPywgHrkbRdQ2Vn9LFbypgncpOC96/4kwiCBNZg/bG5q7xAff/JXkJAsHl5qIE JkRBiC/wbDKc1O0k1cPxn6DNAEC7DACfgIlr281cpYTSTL6uRBmVWPPe8DqypcDlObAT 8V1ivdVIYqlYohV6qx+ivkhanNhNuJzzgLwY+ZvSpPi0/26hnV+Mb4eXLi2mDrcYe+af uZVGdhxCrsaDCCpdAnRg8JulW7iRtlRZRbBzf0IE9sOsUhJXXzJrdXtXGhnb84Z3QHo3 wkvQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga11.intel.com (mga11.intel.com. [192.55.52.93]) by mx.google.com with ESMTPS id j7si4446716plb.91.2018.11.19.13.54.27 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 19 Nov 2018 13:54:27 -0800 (PST) Received-SPF: pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.93 as permitted sender) client-ip=192.55.52.93; Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Nov 2018 13:54:26 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,254,1539673200"; d="scan'208";a="93319889" Received: from yyu32-desk1.sc.intel.com ([143.183.136.147]) by orsmga008.jf.intel.com with ESMTP; 19 Nov 2018 13:54:26 -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 , 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 Cc: Yu-cheng Yu Subject: [RFC PATCH v6 16/26] mm: Handle THP/HugeTLB shadow stack page fault Date: Mon, 19 Nov 2018 13:47:59 -0800 Message-Id: <20181119214809.6086-17-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181119214809.6086-1-yu-cheng.yu@intel.com> References: <20181119214809.6086-1-yu-cheng.yu@intel.com> 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: X-Virus-Scanned: ClamAV using ClamSMTP This patch implements THP shadow stack (SHSTK) copying in the same way as in the previous patch for regular PTE. In copy_huge_pmd(), clear the dirty bit from the PMD to cause a page fault upon the next SHSTK access to the PMD. At that time, fix the PMD and copy/re-use the page. Signed-off-by: Yu-cheng Yu --- arch/x86/mm/pgtable.c | 8 ++++++++ include/asm-generic/pgtable.h | 2 ++ mm/huge_memory.c | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index 75dddc3d8451..4275c80f5832 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -897,6 +897,14 @@ inline pte_t pte_set_vma_features(pte_t pte, struct vm_area_struct *vma) return pte; } +inline pmd_t pmd_set_vma_features(pmd_t pmd, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_SHSTK) + return pmd_mkdirty_shstk(pmd); + else + return pmd; +} + inline bool arch_copy_pte_mapping(vm_flags_t vm_flags) { return (vm_flags & VM_SHSTK); diff --git a/include/asm-generic/pgtable.h b/include/asm-generic/pgtable.h index 30ac390fb2d4..b0b375d8bb34 100644 --- a/include/asm-generic/pgtable.h +++ b/include/asm-generic/pgtable.h @@ -1145,9 +1145,11 @@ static inline bool arch_has_pfn_modify_check(void) #ifndef CONFIG_ARCH_HAS_SHSTK #define pte_set_vma_features(pte, vma) pte +#define pmd_set_vma_features(pmd, vma) pmd #define arch_copy_pte_mapping(vma_flags) false #else pte_t pte_set_vma_features(pte_t pte, struct vm_area_struct *vma); +pmd_t pmd_set_vma_features(pmd_t pmd, struct vm_area_struct *vma); bool arch_copy_pte_mapping(vm_flags_t vm_flags); #endif diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 55478ab3c83b..12148a5b60e0 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -597,6 +597,7 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf, entry = mk_huge_pmd(page, vma->vm_page_prot); entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); + entry = pmd_set_vma_features(entry, vma); page_add_new_anon_rmap(page, vma, haddr, true); mem_cgroup_commit_charge(page, memcg, false, true); lru_cache_add_active_or_unevictable(page, vma); @@ -1209,6 +1210,7 @@ static vm_fault_t do_huge_pmd_wp_page_fallback(struct vm_fault *vmf, pte_t entry; entry = mk_pte(pages[i], vma->vm_page_prot); entry = maybe_mkwrite(pte_mkdirty(entry), vma); + entry = pte_set_vma_features(entry, vma); memcg = (void *)page_private(pages[i]); set_page_private(pages[i], 0); page_add_new_anon_rmap(pages[i], vmf->vma, haddr, false); @@ -1293,6 +1295,7 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf, pmd_t orig_pmd) pmd_t entry; entry = pmd_mkyoung(orig_pmd); entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); + entry = pmd_set_vma_features(entry, vma); if (pmdp_set_access_flags(vma, haddr, vmf->pmd, entry, 1)) update_mmu_cache_pmd(vma, vmf->address, vmf->pmd); ret |= VM_FAULT_WRITE; @@ -1365,6 +1368,7 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf, pmd_t orig_pmd) pmd_t entry; entry = mk_huge_pmd(new_page, vma->vm_page_prot); entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); + entry = pmd_set_vma_features(entry, vma); pmdp_huge_clear_flush_notify(vma, haddr, vmf->pmd); page_add_new_anon_rmap(new_page, vma, haddr, true); mem_cgroup_commit_charge(new_page, memcg, false, true);