Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2212128imm; Tue, 10 Jul 2018 15:34:17 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcHlbnwzNR6DvPJVf4t3HbRbbnyEnrGOFvIw79DrRA6H57O/HwFgF489Pim2WsNrfMEzLZh X-Received: by 2002:a17:902:583:: with SMTP id f3-v6mr26466502plf.115.1531262057277; Tue, 10 Jul 2018 15:34:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531262057; cv=none; d=google.com; s=arc-20160816; b=whBFTt3Er/PnDqyKGhchKpAzyYIwEffZQUby2iLtGscWfhedG9tQc0ipDHcepoT97Z +oOzk+wpBMsu9Eo03n5xLiz2RHiOPVHXyNZ0lt45VUDFuLFOCOb2mxjPQ7UpExjAEvsd MVLJnDU/vJTZQAXl9JP0/b/+mxyW6j7Xwby0QHJyIumksTqutNnM735Gz6X9Q7eZL8Jd 1RJ1UOXzrT+WP9Pu/+LK769FhlzRov54rOLLkwPniN2HnuhFoBBcC5cEbcqCh2WbXwjU AhWMoT+IPlV1YrqAknOx5bd9qnUV6GgdkxjoOjWofBIJPHCBrjUDPGeMdABTerH0Yn+H ukcw== 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=G+uoR50F5zAQjWA6t0KFWYH3edurHUJ+sZEWQ2WfnIo=; b=mghqKI6ZipjJ4AMNBv0BuyVo+Sg/RRL7a/IERrunLQojIcF4ShndawalfJGY9jGFbR ErhvsQfUK1BniIhBP9HoTmbhm0pwAQUk9ykx4BC8fDvo6mtHPPS7sILkVBJ4SyASNyRV IrYfvrXvNiaNqnDfC3jrjyCqXNinBtcJSE3mjBzboXoC9U1xBI+w2BfiVcdt5bjBlII7 nV7L8VORlHI8/rOvl0nN0PhXhFbl6pKB9v4Vwdb8VOp54oUm4UDDfxcarDQSyD6YuLJF ppaJhsDHbV1dHX3k5gdjKyqThCpBayuuUabTDassL+YaJW29hweFjZS9FC3QSnCEhx1M ZuXg== 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 r38-v6si17213613pga.381.2018.07.10.15.34.01; Tue, 10 Jul 2018 15:34:17 -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 S1732439AbeGJWcY (ORCPT + 99 others); Tue, 10 Jul 2018 18:32:24 -0400 Received: from mga12.intel.com ([192.55.52.136]:33776 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732355AbeGJWcY (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:13 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,335,1526367600"; d="scan'208";a="70305412" 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 13/27] mm: Handle shadow stack page fault Date: Tue, 10 Jul 2018 15:26:25 -0700 Message-Id: <20180710222639.8241-14-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 When a task does fork(), its shadow stack must be duplicated for the child. However, the child may not actually use all pages of of the copied shadow stack. This patch implements a flow that is similar to copy-on-write of an anonymous page, but for shadow stack memory. A shadow stack PTE needs to be RO and dirty. We use this dirty bit requirement to effect the copying of shadow stack pages. In copy_one_pte(), we clear the dirty bit from the shadow stack PTE. On the next shadow stack access to the PTE, a page fault occurs. At that time, we then copy/re-use the page and fix the PTE. Signed-off-by: Yu-cheng Yu --- mm/memory.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 7206a634270b..a2695dbc0418 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2453,7 +2453,13 @@ static inline void wp_page_reuse(struct vm_fault *vmf) flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); entry = pte_mkyoung(vmf->orig_pte); - entry = maybe_mkwrite(pte_mkdirty(entry), vma); + + if (is_shstk_mapping(vma->vm_flags)) + entry = pte_mkdirty_shstk(entry); + else + entry = pte_mkdirty(entry); + + entry = maybe_mkwrite(entry, vma); if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) update_mmu_cache(vma, vmf->address, vmf->pte); pte_unmap_unlock(vmf->pte, vmf->ptl); @@ -2526,7 +2532,11 @@ static int wp_page_copy(struct vm_fault *vmf) } flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); entry = mk_pte(new_page, vma->vm_page_prot); - entry = maybe_mkwrite(pte_mkdirty(entry), vma); + if (is_shstk_mapping(vma->vm_flags)) + entry = pte_mkdirty_shstk(entry); + else + entry = pte_mkdirty(entry); + entry = maybe_mkwrite(entry, vma); /* * Clear the pte entry and flush it first, before updating the * pte with the new entry. This will avoid a race condition @@ -3201,6 +3211,14 @@ static int do_anonymous_page(struct vm_fault *vmf) mem_cgroup_commit_charge(page, memcg, false, false); lru_cache_add_active_or_unevictable(page, vma); setpte: + /* + * If this is within a shadow stack mapping, mark + * the PTE dirty. We don't use pte_mkdirty(), + * because the PTE must have _PAGE_DIRTY_HW set. + */ + if (is_shstk_mapping(vma->vm_flags)) + entry = pte_mkdirty_shstk(entry); + set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); /* No need to invalidate - it was non-present before */ @@ -3983,6 +4001,14 @@ static int handle_pte_fault(struct vm_fault *vmf) entry = vmf->orig_pte; if (unlikely(!pte_same(*vmf->pte, entry))) goto unlock; + + /* + * Shadow stack PTEs are copy-on-access, so do_wp_page() + * handling on them no matter if we have write fault or not. + */ + if (is_shstk_mapping(vmf->vma->vm_flags)) + return do_wp_page(vmf); + if (vmf->flags & FAULT_FLAG_WRITE) { if (!pte_write(entry)) return do_wp_page(vmf); -- 2.17.1