Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4832329pxy; Tue, 27 Apr 2021 13:47:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJym6Xwr7lgFdNs9qvLdmuV86KhySpST6FIsXbS1VgyqTyHddhtz7gr0YL9ovX6TaRcsNnw4 X-Received: by 2002:a65:4c89:: with SMTP id m9mr23200245pgt.330.1619556448228; Tue, 27 Apr 2021 13:47:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619556448; cv=none; d=google.com; s=arc-20160816; b=LIFwIAfv9LTWrQtrL93eZs/9VzkwkFeRUab552YD4OPwDO/Lc/u2wRJZzGrsnUD01R 04AmSDapCr36xyJ/t4NXiqozu2W5lTXZ1CVq73CFSrt9jHZR6DumnwxX9VLxPn13ovtp 8eSvvQqKeFXkNmtQL13ZzJtQpMMpmpE2pR0RmttrtST3qk4vgsancAyXKLyEwLRILMSM AerVj0TyV2Hj/CIMOP5BLnte0h2dBOHlwUWQpnsr5LYwooL4kmTfno/51DdV4dM101ba 9mcGhGDej8/if9w0igHzi0z4SvIu1L8a7jA2uBxQ0U9+A44zlchdHgj0ndURhRK9yjSA v4cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=k0oTqKrLUu9s5HOVOybFAuKSiefayA94Fwh6tdjLEVI=; b=AFkcCTcP/Bb/2SpRaCsLKuieCF84e0olJ0gtSqY/6jSX4T7fOZwNfmkRnJ8RpnwqUl w/rNEIPpehxm/T6Xc+8MRiCrPXzBSeq4p98LFxDZg6BK1cfT7sH0ZssGdlFvrK7kutX3 OCrlwoEQo87xidJmJKTSCeD5LMO4fdh/lmr18wq26ra5uL/nn+7SBlPAoHYdUu+TVF29 +d4cdIyZTZgQgYqjLMusr4wr5qnLRftZQ8qCGu/VnmQ8PdMCh5wJhYUxLMCqMnCuA9N0 bdyunqn1r6Ms6pyQsWRq94CbAtfkFo/qwkPlODbhqP2hC24e0saxUmdyL6z6zO5bAPtW R+Lw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id h13si1093287plr.95.2021.04.27.13.47.14; Tue, 27 Apr 2021 13:47:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S239222AbhD0Uph (ORCPT + 99 others); Tue, 27 Apr 2021 16:45:37 -0400 Received: from mga05.intel.com ([192.55.52.43]:31780 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239145AbhD0UpS (ORCPT ); Tue, 27 Apr 2021 16:45:18 -0400 IronPort-SDR: xFOZXerFhaJpJQni3mRPHiWzI7fBoPgpfKoUpxGQK19/m4clE6U9j5KCFA7L4QUxnG/j49qnWj tBlLfA86U/lw== X-IronPort-AV: E=McAfee;i="6200,9189,9967"; a="281922468" X-IronPort-AV: E=Sophos;i="5.82,255,1613462400"; d="scan'208";a="281922468" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Apr 2021 13:44:13 -0700 IronPort-SDR: GiOu6oGZC8O6G8a2/HSiJ96TGKrREMZ7zhgi84VlXsBeJAxvwBZEF7I6QU9NQblIGwA95wFEnf UFoV9HxkYXHw== X-IronPort-AV: E=Sophos;i="5.82,255,1613462400"; d="scan'208";a="465623479" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Apr 2021 13:44:12 -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 , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H.J. Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V. Shankar" , Vedvyas Shanbhogue , Dave Martin , Weijiang Yang , Pengfei Xu , Haitao Huang Cc: Yu-cheng Yu , "Kirill A . Shutemov" Subject: [PATCH v26 11/30] x86/mm: Update pte_modify for _PAGE_COW Date: Tue, 27 Apr 2021 13:42:56 -0700 Message-Id: <20210427204315.24153-12-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210427204315.24153-1-yu-cheng.yu@intel.com> References: <20210427204315.24153-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The read-only and Dirty PTE has been used to indicate copy-on-write pages. However, newer x86 processors also regard a read-only and Dirty PTE as a shadow stack page. In order to separate the two, the software-defined _PAGE_COW is created to replace _PAGE_DIRTY for the copy-on-write case, and pte_*() are updated. Pte_modify() changes a PTE to 'newprot', but it doesn't use the pte_*(). Introduce fixup_dirty_pte(), which sets a dirty PTE, based on _PAGE_RW, to either _PAGE_DIRTY or _PAGE_COW. Apply the same changes to pmd_modify(). Signed-off-by: Yu-cheng Yu Reviewed-by: Kirill A. Shutemov --- arch/x86/include/asm/pgtable.h | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 9c056d5815de..e1739f590ca6 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -799,6 +799,23 @@ static inline pmd_t pmd_mkinvalid(pmd_t pmd) static inline u64 flip_protnone_guard(u64 oldval, u64 val, u64 mask); +static inline pteval_t fixup_dirty_pte(pteval_t pteval) +{ + pte_t pte = __pte(pteval); + + /* + * Fix up potential shadow stack page flags because the RO, Dirty + * PTE is special. + */ + if (cpu_feature_enabled(X86_FEATURE_SHSTK)) { + if (pte_dirty(pte)) { + pte = pte_mkclean(pte); + pte = pte_mkdirty(pte); + } + } + return pte_val(pte); +} + static inline pte_t pte_modify(pte_t pte, pgprot_t newprot) { pteval_t val = pte_val(pte), oldval = val; @@ -809,16 +826,36 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot) */ val &= _PAGE_CHG_MASK; val |= check_pgprot(newprot) & ~_PAGE_CHG_MASK; + val = fixup_dirty_pte(val); val = flip_protnone_guard(oldval, val, PTE_PFN_MASK); return __pte(val); } +static inline int pmd_write(pmd_t pmd); +static inline pmdval_t fixup_dirty_pmd(pmdval_t pmdval) +{ + pmd_t pmd = __pmd(pmdval); + + /* + * Fix up potential shadow stack page flags because the RO, Dirty + * PMD is special. + */ + if (cpu_feature_enabled(X86_FEATURE_SHSTK)) { + if (pmd_dirty(pmd)) { + pmd = pmd_mkclean(pmd); + pmd = pmd_mkdirty(pmd); + } + } + return pmd_val(pmd); +} + static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot) { pmdval_t val = pmd_val(pmd), oldval = val; val &= _HPAGE_CHG_MASK; val |= check_pgprot(newprot) & ~_HPAGE_CHG_MASK; + val = fixup_dirty_pmd(val); val = flip_protnone_guard(oldval, val, PHYSICAL_PMD_PAGE_MASK); return __pmd(val); } -- 2.21.0