Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2212534imm; Tue, 10 Jul 2018 15:34:49 -0700 (PDT) X-Google-Smtp-Source: AAOMgpffvQVvFX3nJzAeOwlnd1GhluqbpXu2323vYhYnNwIGCjUw4lXNpduPdY7qZjBEB9R6BlmU X-Received: by 2002:a63:2b15:: with SMTP id r21-v6mr18767954pgr.262.1531262089057; Tue, 10 Jul 2018 15:34:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531262089; cv=none; d=google.com; s=arc-20160816; b=iV9KvxYHAusvrBy0PCUW5aIPYvWKUAliLUmbCVJicrDtJkZIQuWQsWsPBMRVChh+hk q4s/uOqvFeKwppjzSTzHxUmWt8eC84oeBC7/CLDmeRVtFVmfDnG2gIckTHrJfx0Y1n1S ky6pJXmHf4zZmTerr+xuW8dV3faU9k/ELohYlDoRHkRZslnWlxSX6LJXg5RmohdCndxE EWbqmPO3QWlYFg3KzOB8xraNaCdqr3clTzZlGhCPfvTSnWMh2jLMug8NiE6QgWxjPPc0 wuK/O9XtgjIEo/7RQj6eNDvtHP0nmCHr95k1M2fW+TKzjw08zKZzdEXM+UF8y+2isG6b lv0w== 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=wQcGbsB/ofZc6dS2bFwEBoKCU1p+6qlp5orYA5TsQBE=; b=AB3uWSjiLzF2vAn202VjuSAGxfMBY3627FKzMwFRNna57NVZYDR0hO0LvTy8aNJXqj P3QGCGQ9oVo5hONSSl3xhHkkuVKPry4nz4NB6AhjpEqZSz0Cx6fA1z1qc7bTEGBnrdhn 16cx+g+5XnEzuEAKINxgdHvjVMcrIYyZiopvmvY39i/BP75WMid5nfroREyYXDBDNpPX 3JUnHcpJcg7W6QXYm8ECHpnOx1WTbJG838cuz27wdQsRBzwwGbskN5IdH/9G9IBkXOI8 XfJJcvnkQ3y57/aHuyP8Po9jg2Tylow9PayFQ8q4U9OZ5FgGGafr3B8LJWEDhgERWGhv LYLg== 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 c5-v6si17406539pll.75.2018.07.10.15.34.33; Tue, 10 Jul 2018 15:34:49 -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 S1732462AbeGJWcZ (ORCPT + 99 others); Tue, 10 Jul 2018 18:32:25 -0400 Received: from mga12.intel.com ([192.55.52.136]:33779 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732408AbeGJWcY (ORCPT ); Tue, 10 Jul 2018 18:32:24 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 10 Jul 2018 15:31:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,335,1526367600"; d="scan'208";a="70305417" Received: from 2b52.sc.intel.com ([143.183.136.52]) by fmsmga004.fm.intel.com with ESMTP; 10 Jul 2018 15:31:13 -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 v2 14/27] mm: Handle THP/HugeTLB shadow stack page fault Date: Tue, 10 Jul 2018 15:26:26 -0700 Message-Id: <20180710222639.8241-15-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180710222639.8241-1-yu-cheng.yu@intel.com> References: <20180710222639.8241-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 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 | 8 ++++++++ mm/memory.c | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 1cd7c1a57a14..7f3e11d3b64a 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -597,6 +597,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); @@ -1193,6 +1195,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); @@ -1277,6 +1281,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; @@ -1347,6 +1353,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/memory.c b/mm/memory.c index a2695dbc0418..f7c46d61eaea 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4108,7 +4108,13 @@ static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address, if (pmd_protnone(orig_pmd) && vma_is_accessible(vma)) return do_huge_pmd_numa_page(&vmf, orig_pmd); - if (dirty && !pmd_write(orig_pmd)) { + /* + * Shadow stack trans huge PMDs are copy-on-access, + * so wp_huge_pmd() on them no mater if we have a + * write fault or not. + */ + if (is_shstk_mapping(vma->vm_flags) || + (dirty && !pmd_write(orig_pmd))) { ret = wp_huge_pmd(&vmf, orig_pmd); if (!(ret & VM_FAULT_FALLBACK)) return ret; -- 2.17.1