Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp38575imm; Thu, 30 Aug 2018 07:50:04 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZ3p/DViUwhvoLHp1TbhlVPX0LZrx6zfzJRCAD/UWT0ue+fQhDR5qFbR2Z2J1T7s2y2aABD X-Received: by 2002:a62:cac5:: with SMTP id y66-v6mr10799942pfk.187.1535640604582; Thu, 30 Aug 2018 07:50:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535640604; cv=none; d=google.com; s=arc-20160816; b=Qf4kKFDAiPjNurtLqXzYgrgsyS2HdhG7+FJqluf/tSMDgSheJyOWX1nDovXCuPU89n 16Zs2vlJ/uxaYtxjHDyEZ2SpeZZry+1X7s6tvuDhGibbxHqrt9iwlGk8HltVWOG7mnaC 3t6rt6JahHmIE1eQznoI0SGkzmv4GEkBefCOmd6Xrv13UeeWecC4qjRYm+vGFzD0vPhj GzJFKtfqibpQWt1ui4+u0OuJMA/sFdXmJx7zqeyy+RpiFVVCvO09ZPexndoc/NBHe/If le1dkqcI4gqqMXxIjtaLItica3wu+M691pJMD194cfh5v4M083SmTEIoqpyDfnd35/6+ MBUg== 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=EQNscwzpy+cjA0WX6EmQ4Rhmb4cUPgV+Kog5rBSI4mM=; b=TDwrFXDQdQyY+lg089xGS6VtXTyWXnN3RdFXwFr9OYWvvWb+p2udLDp8qKub0jf4/D NXikasnZME6Ben7ft4eYqfsx9dMbnfJIbOt7t+LtJr6rAFEVgwKsOQ59xxms4NdlWb9p 0779Mn1mXA9H7c1EHPXhQAmRWHXgY2KGNZOuW8KJkyBJm376OFDcPcX306J54lyemLLL EkLUa19vrHXy5mALq0vb0h+bABk4/v3gfguEE+qO7X5la8cCH/HHP9bujQqhQ0nhmIBb /e4w0vlNCK0Oxgg1qWBUeD5tXLKntNiYNW8ISxcqh6XgC+/fycCE+1RZRxXFHf1GZuxZ d8/w== 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 v129-v6si7293603pfv.278.2018.08.30.07.49.49; Thu, 30 Aug 2018 07:50:04 -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 S1729582AbeH3SqS (ORCPT + 99 others); Thu, 30 Aug 2018 14:46:18 -0400 Received: from mga01.intel.com ([192.55.52.88]:38433 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729487AbeH3SqS (ORCPT ); Thu, 30 Aug 2018 14:46:18 -0400 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:43 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,307,1531810800"; d="scan'208";a="67186713" 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 12/24] x86/mm: Modify ptep_set_wrprotect and pmdp_set_wrprotect for _PAGE_DIRTY_SW Date: Thu, 30 Aug 2018 07:38:52 -0700 Message-Id: <20180830143904.3168-13-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> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When Shadow Stack is enabled, the read-only and PAGE_DIRTY_HW PTE setting is reserved only for the Shadow Stack. To track dirty of non-Shadow Stack read-only PTEs, we use PAGE_DIRTY_SW. Update ptep_set_wrprotect() and pmdp_set_wrprotect(). Signed-off-by: Yu-cheng Yu --- arch/x86/include/asm/pgtable.h | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 4d50de77ea96..556ef258eeff 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -1203,7 +1203,28 @@ static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm, static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep) { + pte_t pte; + clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte); + pte = *ptep; + + /* + * Some processors can start a write, but ending up seeing + * a read-only PTE by the time they get to the Dirty bit. + * In this case, they will set the Dirty bit, leaving a + * read-only, Dirty PTE which looks like a Shadow Stack PTE. + * + * However, this behavior has been improved and will not occur + * on processors supporting Shadow Stacks. Without this + * guarantee, a transition to a non-present PTE and flush the + * TLB would be needed. + * + * When change a writable PTE to read-only and if the PTE has + * _PAGE_DIRTY_HW set, we move that bit to _PAGE_DIRTY_SW so + * that the PTE is not a valid Shadow Stack PTE. + */ + pte = pte_move_flags(pte, _PAGE_DIRTY_HW, _PAGE_DIRTY_SW); + set_pte_at(mm, addr, ptep, pte); } #define flush_tlb_fix_spurious_fault(vma, address) do { } while (0) @@ -1266,7 +1287,28 @@ static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm, static inline void pmdp_set_wrprotect(struct mm_struct *mm, unsigned long addr, pmd_t *pmdp) { + pmd_t pmd; + clear_bit(_PAGE_BIT_RW, (unsigned long *)pmdp); + pmd = *pmdp; + + /* + * Some processors can start a write, but ending up seeing + * a read-only PTE by the time they get to the Dirty bit. + * In this case, they will set the Dirty bit, leaving a + * read-only, Dirty PTE which looks like a Shadow Stack PTE. + * + * However, this behavior has been improved and will not occur + * on processors supporting Shadow Stacks. Without this + * guarantee, a transition to a non-present PTE and flush the + * TLB would be needed. + * + * When change a writable PTE to read-only and if the PTE has + * _PAGE_DIRTY_HW set, we move that bit to _PAGE_DIRTY_SW so + * that the PTE is not a valid Shadow Stack PTE. + */ + pmd = pmd_move_flags(pmd, _PAGE_DIRTY_HW, _PAGE_DIRTY_SW); + set_pmd_at(mm, addr, pmdp, pmd); } #define pud_write pud_write -- 2.17.1