Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1687608pxb; Fri, 20 Aug 2021 11:21:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx+/dzwVgpZEWXQxJvhhEFUfzoDZVxkgVddRCB52YhuMu5Jxvh8Y6rZO9ED86ZEdmmjzS9t X-Received: by 2002:a92:d351:: with SMTP id a17mr5592487ilh.167.1629483663921; Fri, 20 Aug 2021 11:21:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629483663; cv=none; d=google.com; s=arc-20160816; b=krkpbUN3nMALMXh45zYvTnvO6km4yn6DFrZyiV5zg6G5iuudA7XKwt/DYy/GWbbceJ moUzsVTfCe1PgQmxAwQbTgIE/SeOtZ/VIeONKpcSVzZWgT8dXQ2xSol0Smv5xFYRrey4 +QZehGyxqJnQJAUkRN16B2B/Ix9/fv/ytj29KMaxPLQgVhQ7sex4n8j8RIUwlJQyJslY 2USl2/eLfJTBsIdH9dH4TyDff/e1aMZmG6OSvTEXS8NZO8ORq7dtjy4LrOEWGFkD2boD De70CWhqY7vgT9ZyNGDoUEyNSta3wb76X1QNApDcsDbsG/30MTxXXAdowEaN4iCAwKn8 enaA== 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; bh=GB9aqdBqwghqeWfVQEtgtAl1tpQpkt8ajvSxdEtF1OY=; b=bTmU5MJf5mtFh1VmulKjgJ1uxA9DZ20R0F/wVYxdJ60UOhfvwsc0BhxgJnYBQQbiUx 6LG89rJigOn5BeWJGbH5o8oTLrGv/QWyg/ZeS5u8/hSSr4XMs3FAHG7o4U0h1SpHLvbH SEzHcclfpOmMkC1xM7K6nBdvyqQ8iAO2rhHgLk4Id4AjMQeabFNNVnUJ/3pZ7acIa4MD XXmlSWl/ieeO2odZgsVyyeSehx0u1e6W854gF2OYHzEqI8fEOjEaflIXz2nrSjKb1Vq9 9vqsYJQ/7jpw1xarpRXMNBdEbs9uCm9B9+Jww0krNSz7pwJruUsTqsEmAnhDSge+ecVf zP4g== 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 q4si8415188jaj.45.2021.08.20.11.20.51; Fri, 20 Aug 2021 11:21:03 -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 S238135AbhHTSUU (ORCPT + 99 others); Fri, 20 Aug 2021 14:20:20 -0400 Received: from mga05.intel.com ([192.55.52.43]:22433 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236809AbhHTSTc (ORCPT ); Fri, 20 Aug 2021 14:19:32 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10082"; a="302407806" X-IronPort-AV: E=Sophos;i="5.84,338,1620716400"; d="scan'208";a="302407806" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Aug 2021 11:18:46 -0700 X-IronPort-AV: E=Sophos;i="5.84,338,1620716400"; d="scan'208";a="533074705" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Aug 2021 11:18:46 -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" , Dave Martin , Weijiang Yang , Pengfei Xu , Haitao Huang , Rick P Edgecombe Cc: Yu-cheng Yu , "Kirill A . Shutemov" Subject: [PATCH v29 11/32] x86/mm: Update pte_modify for _PAGE_COW Date: Fri, 20 Aug 2021 11:11:40 -0700 Message-Id: <20210820181201.31490-12-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210820181201.31490-1-yu-cheng.yu@intel.com> References: <20210820181201.31490-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 9f1ba76ed79a..cf7316e968df 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -771,6 +771,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; @@ -781,16 +798,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