From patchwork Thu Jun 7 14:37:05 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: 10452285 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id EEDC960375 for ; Thu, 7 Jun 2018 14:41:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DF0BB29510 for ; Thu, 7 Jun 2018 14:41:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DC7EB29511; Thu, 7 Jun 2018 14:41:22 +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=unavailable 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 6977529586 for ; Thu, 7 Jun 2018 14:41:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5CA1C6B0271; Thu, 7 Jun 2018 10:40:45 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id F3FA86B0272; Thu, 7 Jun 2018 10:40:44 -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 BB5656B026D; Thu, 7 Jun 2018 10:40:44 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf0-f198.google.com (mail-pf0-f198.google.com [209.85.192.198]) by kanga.kvack.org (Postfix) with ESMTP id 3DF586B0270 for ; Thu, 7 Jun 2018 10:40:44 -0400 (EDT) Received: by mail-pf0-f198.google.com with SMTP id j14-v6so4648529pfn.11 for ; Thu, 07 Jun 2018 07:40:44 -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=TSPHs9ydqq8JNTOQgp5u6L6bnwZ3z11dRvC6SXn4whQ=; b=pZTXcLWdt8wmDVUSC2k2mBPjkaa3w3tvvsf8A4UUt3O4QWkQXHo+vWOrzqYTOdzf9d 0io5kBH7hdKv65Wff+l7sssvz4DLkpCOsaphNR5eEmkRhAyhkpCysvUclHGIslncRqQR jw9SRffBxnqMfk5l5wf0+eJsE65125GGNl9xnp9XWNXBgceeQbvmDxP3J/9L6v5XIxdT MlK6aO5EhqsN7H0sXrYkJiQLKZ+TeU1P+1Agc1Ev0ru56PkdQBHmUoetCLlh9QS6lWUA E27VIPE0Ri0+i01emCIQhyvVna0bWIoQCgwCWOX16Vn0GawTnCVwTvVxpxwCgYyqP9GV ogPA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 134.134.136.126 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: APt69E11mOPbNR91VTF8Hs7G3xaEmcCnBHSnw5BeTjkPsQAkrYz9kYQH SkzdFKbQp7Jhrcx2zp5uJUWqOVTsm5sZnUDQXG7r/8rlNoDnyguNRNYJisPFrZBGmzKB3tXV5kV RyTqgmQJBtXdWVX5pNtrUlaoIJVRbyqiAhG8QH3whp5ewfOCZ4EUVJz84NPT2NASzdg== X-Received: by 2002:a17:902:3001:: with SMTP id u1-v6mr2343837plb.376.1528382443947; Thu, 07 Jun 2018 07:40:43 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIcF2aXT3kTgoKuqA2UMNzglftGx3SctbLDyVlHklaD5RjBUBDf5CYUQdPXBeRDK189qdxA X-Received: by 2002:a17:902:3001:: with SMTP id u1-v6mr2343792plb.376.1528382443245; Thu, 07 Jun 2018 07:40:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528382443; cv=none; d=google.com; s=arc-20160816; b=wCcYUSZ8IU4LU3rOvyk1HsragV42jhhOIwknVqK+Ht85LIUricdCy6EnoZ4Rgob06e seMuN4vGnWciSwpZjQ9EzhXMXS3mNuN8nw/fAOK2kWZfZT/WpyiSEjx8DpaL+GnHg6s9 zJ2dvnV222jLdwZFMfLXtajaggQMk70bDrVNeTyEwlDWlV533OQ0BDzsNvxfrq6+9wHk ovJqq0W+0QA90LZxqSWx/PiGBw7uMG0j5JKggo5W1HJyqw4Xq6GMs/u5A45FEOBSmccy +tL5nDKrYiGhQvG4ilsOzBTiedYyrlWW6FbMucMjaoeUDAcDlnpVimk2JY/V0/Boj+cY OzuQ== 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=TSPHs9ydqq8JNTOQgp5u6L6bnwZ3z11dRvC6SXn4whQ=; b=TX4cTX0TwcFi/OdifFXn1kUzmcy6Jrd5T72WAyP9fjf8eYTBssR7EDbow69QPswi65 B6EJRtI5nzZBFwXKJjgp9yFKocpb/KqFCQNS287IaDdYYwufgPblf8iPECLjpvxX7jrw 7GvDqwTKWxEclHSvHia9sJ0ID7Z4Lk8otBhpwZwNCkZDTlMgJNy18QuQRGPh0bv6fmgc IhR2BiF1RJHiNNrop+82GPUBBC8bcxsV93g2KCUh7E9flPTxnVU6aF1rwPADdRhKAAsR DV0ThI3REgA++RX0pxEFfQwasD4DI7LGkoetirhTIrc+eVgXpnSfTSTFNwZM6KuU1obr oTlQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 134.134.136.126 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga18.intel.com (mga18.intel.com. [134.134.136.126]) by mx.google.com with ESMTPS id b60-v6si54342625plc.270.2018.06.07.07.40.43 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 Jun 2018 07:40:43 -0700 (PDT) Received-SPF: pass (google.com: domain of yu-cheng.yu@intel.com designates 134.134.136.126 as permitted sender) client-ip=134.134.136.126; Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 134.134.136.126 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 orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 07 Jun 2018 07:40:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,486,1520924400"; d="scan'208";a="47542315" Received: from 2b52.sc.intel.com ([143.183.136.51]) by orsmga008.jf.intel.com with ESMTP; 07 Jun 2018 07:40:34 -0700 From: Yu-cheng Yu To: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , "H.J. Lu" , Vedvyas Shanbhogue , "Ravi V. Shankar" , Dave Hansen , Andy Lutomirski , Jonathan Corbet , Oleg Nesterov , Arnd Bergmann , Mike Kravetz Cc: Yu-cheng Yu Subject: [PATCH 9/9] x86/cet: Handle THP/HugeTLB shadow stack page copying Date: Thu, 7 Jun 2018 07:37:05 -0700 Message-Id: <20180607143705.3531-10-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180607143705.3531-1-yu-cheng.yu@intel.com> References: <20180607143705.3531-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 --- mm/huge_memory.c | 10 +++++++++- mm/hugetlb.c | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index a3a1815f8e11..c6e72ccc4274 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -600,6 +600,8 @@ static int __do_huge_pmd_anonymous_page(struct vm_fault *vmf, struct page *page, entry = mk_huge_pmd(page, vma->vm_page_prot); entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); + if (is_shstk_mapping(vma->vm_flags)) + entry = pmd_mkdirty_shstk(entry); 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); @@ -976,7 +978,7 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, mm_inc_nr_ptes(dst_mm); pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable); - pmdp_set_wrprotect(src_mm, addr, src_pmd); + pmdp_set_wrprotect_flush(vma, addr, src_pmd); pmd = pmd_mkold(pmd_wrprotect(pmd)); set_pmd_at(dst_mm, addr, dst_pmd, pmd); @@ -1196,6 +1198,8 @@ static int do_huge_pmd_wp_page_fallback(struct vm_fault *vmf, pmd_t orig_pmd, pte_t entry; entry = mk_pte(pages[i], vma->vm_page_prot); entry = maybe_mkwrite(pte_mkdirty(entry), vma); + if (is_shstk_mapping(vma->vm_flags)) + entry = pte_mkdirty_shstk(entry); memcg = (void *)page_private(pages[i]); set_page_private(pages[i], 0); page_add_new_anon_rmap(pages[i], vmf->vma, haddr, false); @@ -1280,6 +1284,8 @@ int 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); + if (is_shstk_mapping(vma->vm_flags)) + entry = pmd_mkdirty_shstk(entry); if (pmdp_set_access_flags(vma, haddr, vmf->pmd, entry, 1)) update_mmu_cache_pmd(vma, vmf->address, vmf->pmd); ret |= VM_FAULT_WRITE; @@ -1350,6 +1356,8 @@ int 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); + if (is_shstk_mapping(vma->vm_flags)) + entry = pmd_mkdirty_shstk(entry); 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); diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 218679138255..d694cfab9f90 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3293,7 +3293,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, * * See Documentation/vm/mmu_notifier.txt */ - huge_ptep_set_wrprotect(src, addr, src_pte); + huge_ptep_set_wrprotect_flush(vma, addr, src_pte); } entry = huge_ptep_get(src_pte); ptepage = pte_page(entry);