From patchwork Thu Aug 30 14:38:55 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: 10581853 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 4240514E1 for ; Thu, 30 Aug 2018 14:44:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 30BE62B560 for ; Thu, 30 Aug 2018 14:44:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 23CCC2BC83; Thu, 30 Aug 2018 14:44:46 +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 97CA12B560 for ; Thu, 30 Aug 2018 14:44:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2839B6B5225; Thu, 30 Aug 2018 10:43:49 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 19AA86B5233; Thu, 30 Aug 2018 10:43:48 -0400 (EDT) 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 4E87B6B5230; Thu, 30 Aug 2018 10:43:48 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) by kanga.kvack.org (Postfix) with ESMTP id 527A96B5225 for ; Thu, 30 Aug 2018 10:43:47 -0400 (EDT) Received: by mail-pf1-f199.google.com with SMTP id j15-v6so4833822pff.12 for ; Thu, 30 Aug 2018 07:43:47 -0700 (PDT) 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=tU4mYnDOs/8FLat8c5d4nBt0YTG3mI5RrXgj3u0mNnc=; b=oTmE4LGgdLpZJQsuGZAcDUfOMGf0C34XHoR0WagQ3pITwKNAIKhoL7vR307bbTBG6a AfgMluMUaC96LCL7OY4xcK9fXXIgg8aEa49JDESSFMjRervOJsDsLZ8iuPAUdiSdlIvl pfRf/ZLtlmsK4ebuQ7GU+BHtEJeD1tnsw2n6pwYr2ENAdxZ/uT27WPFWgmvGSw6wcnUH +pyWhOr34lHCXOGn9ACKrX405eKkI10LAAWSDt1PKgQuH8ZywlF8oVyYnpoPM3uISvh3 h9ce2QRiAGdQgv0GVTlsZ3HEBF3KQAk8b4VPzBkosX85RbOkaEdluTPjVktAgy8H2jAH 6zmA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.88 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: APzg51BL9uKkki1SwkrfL9S5oK4DeqM8KpoGqqaIy0H5wt9PQGBqQevQ TPmnDhwqV7yKjnz7ukdckd2ItokYAkLerIpAWth55TWHrMATtPU1oOJSsD6PgsunP+aUeJGvl6f pEG1D1tsp1n2xD5/KY9W6kvWmnPhJZXl6BD3i3mVjSj33I5+SRd4o/HczHEYjy9IRrA== X-Received: by 2002:a63:2c0e:: with SMTP id s14-v6mr8334602pgs.199.1535640226999; Thu, 30 Aug 2018 07:43:46 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYRojN7vI+kPN+MCCOg7n2L5A8HAX1mh/0n8Qhg6PmkQsPNeGkSC8exLpNsUE/vS/clCnZx X-Received: by 2002:a63:2c0e:: with SMTP id s14-v6mr8334539pgs.199.1535640225739; Thu, 30 Aug 2018 07:43:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535640225; cv=none; d=google.com; s=arc-20160816; b=KedmH6IKxcbEpqSTWUXms3bjqlbSAIS8vLBdxDh3Qvs/NRavM1J+hUHu5XafgB6T21 hvVV0EuylbcGP6MmjzpfzifJO381WT/a38IgbXH+rovrxVsivJMWY3Jj9W79Ch0a9Ylf C3CEjo7+9Bqa5vamNchqpGwJahMaWdkcezRGHVSdsOrePXqjq9Q/OPgT9VOlMdLMTgSt WC/Op8lZKfZi0TLWmKFF10v/gYuiV0svnlOwxm3cOzqK6ykYjTf17VM6b0p7li+bamNA tHkufUFI/Dj3iDXMwv90PIJ4C/du6DXEeyc5PB2FWklVE3fF7BayYiv2jyxZsLU/NovN MPtQ== 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 :arc-authentication-results; bh=tU4mYnDOs/8FLat8c5d4nBt0YTG3mI5RrXgj3u0mNnc=; b=dc8eXpIdZ3qfkbrXza5iRIoKbyu8y+9sM5W/jncMpBQhs3OF3+4n5wPhdYrWWy23qv V+YoKK7tyuChbcEfBjb8Fz1VSQJ1uFOBes50lLSSqvrCQ1o5mfNSdM1uGW8yLvkyy5kX DNUti7JRnVYEl1lx1vkGVXE7C42ud2svIKdJZEFRgBKnHz2ni9+jZNCIdlRyIFNUxHmu gNoOqX9taYvZH3xMbpkW5wOx1LKmmKz8F7/IivKKOb+Bf4qDf/YvgKtr0Yz1JvIySvNo Qv9+GbLXgs3ZpjVDuUIMKWMnZeuYnhhN7ItyAva0QIC6VEY+80Lq0xuADvwgbFoQEzI4 ek6w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.88 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga01.intel.com (mga01.intel.com. [192.55.52.88]) by mx.google.com with ESMTPS id z73-v6si6610727pgd.471.2018.08.30.07.43.45 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Aug 2018 07:43:45 -0700 (PDT) Received-SPF: pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.88 as permitted sender) client-ip=192.55.52.88; Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.88 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 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="67186718" 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 15/24] mm: Handle THP/HugeTLB shadow stack page fault Date: Thu, 30 Aug 2018 07:38:55 -0700 Message-Id: <20180830143904.3168-16-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> 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 memory copying in the same way as the previous patch for regular PTE. In copy_huge_pmd(), we clear the dirty bit from the PMD. On the next shadow stack access to the PMD, a page fault occurs. At that time, the page is copied/re-used and the PMD is fixed. Signed-off-by: Yu-cheng Yu --- arch/x86/mm/pgtable.c | 8 ++++++++ include/asm-generic/pgtable.h | 6 ++++++ mm/huge_memory.c | 4 ++++ 3 files changed, 18 insertions(+) diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index c63261128ac3..0ab38bfbedfc 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -881,4 +881,12 @@ inline pte_t pte_set_vma_features(pte_t pte, struct vm_area_struct *vma) else 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; +} #endif /* CONFIG_X86_INTEL_SHADOW_STACK_USER */ diff --git a/include/asm-generic/pgtable.h b/include/asm-generic/pgtable.h index 558a485617cd..0f25186cd38d 100644 --- a/include/asm-generic/pgtable.h +++ b/include/asm-generic/pgtable.h @@ -1151,8 +1151,14 @@ static inline pte_t pte_set_vma_features(pte_t pte, struct vm_area_struct *vma) { return pte; } + +static inline pmd_t pmd_set_vma_features(pmd_t pmd, struct vm_area_struct *vma) +{ + return pmd; +} #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); #endif #endif /* _ASM_GENERIC_PGTABLE_H */ diff --git a/mm/huge_memory.c b/mm/huge_memory.c index c3bc7e9c9a2a..5b4c8f2fb85e 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); @@ -1194,6 +1195,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); @@ -1278,6 +1280,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; @@ -1349,6 +1352,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);