Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp673432pxb; Wed, 3 Feb 2021 15:01:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJx1tvy64bMZivMH95RvOUf8FMolLVQY5AhItxeBP8CVHYNad6n/6xyObQwsgSZhj9E2KZ46 X-Received: by 2002:aa7:c58e:: with SMTP id g14mr5586759edq.318.1612393269622; Wed, 03 Feb 2021 15:01:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612393269; cv=none; d=google.com; s=arc-20160816; b=QLGP5MHI334S5/x0jCKz6TkWlKE4WdTpUCA5+9cp7141Dox/UCOW7QnsJY9kdRJgT6 3gX7b1re7JFm/XStR3eVzMC1VM1F5dsMFeZk2JBho2fF9z5JDXGGiaPIG9qH3FyabVp3 +r+wN93sJatqpKqeg8PpgFZFfyZjyCs4Ar6zz9eVUUTBe024opHnCrDjgEuKIVpdsFC5 dMTNcWC+r07sJuaI35gFnkouWnRa1AR3vLQsC/jCnXel+5F2ibuDtYHFE2l1+WFTcbAO vUnaruHa5AK6ARlQSOEsXj7Cp3Nq+si1uig13akVg4IO3BrGAC4A36AkO36aXbSjnai4 0pLw== 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=0IhzmnPfCNZtVUv8opFt5ZTGfppywnTBx2RCFG0ScyY=; b=B/IAznQ77lBCfxdrC4b7xNK6et1v01IEnituianNC97ecmJORProat1sau1vvPLoSt LCxKSEiFFwvdQMANPjD6mafq58BXtfWdnmiCSBriSE5wv+M6YkU7Adh9JB34X8LimhiR lg2NBT53JDhuxUOZ+5k8xrgvyelkIJVqgXz/RqWObAp71A9W9/VV3SNioIAn8BBUHgf+ W2AvcPQjj3O2oPZPiRv16Rr69zIyWcnEF9Uw5napJdYb5IRHPV0slwStp8Q7pMjjbTFv B2YVoIZIsJ7R3FXh7mtT4WOKUslZ5tkuiQvYyexF0xbtCPIZBjdT5b/HIa247tmQJGvW QTJA== 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 dr2si2079326ejc.663.2021.02.03.15.00.44; Wed, 03 Feb 2021 15:01:09 -0800 (PST) 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 S233328AbhBCW6s (ORCPT + 99 others); Wed, 3 Feb 2021 17:58:48 -0500 Received: from mga06.intel.com ([134.134.136.31]:30210 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233231AbhBCW5v (ORCPT ); Wed, 3 Feb 2021 17:57:51 -0500 IronPort-SDR: kxBvEZUcfjW5uCkp4v1Zvet28KfEbjqoy+biNtsBOuaIf1FHxfHe48NFqdkC1r+G9xVPt7QWNc uS8K7nnz6GOQ== X-IronPort-AV: E=McAfee;i="6000,8403,9884"; a="242642358" X-IronPort-AV: E=Sophos;i="5.79,399,1602572400"; d="scan'208";a="242642358" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 14:56:25 -0800 IronPort-SDR: VxMDgpo8ntl1NOx640K14CRRqjsfKgSI1oiQTtUvFoD6drw3sEFcgbTJmR1I6LERWTFeV2oOI+ b6Z6Liu/264A== X-IronPort-AV: E=Sophos;i="5.79,399,1602572400"; d="scan'208";a="507921105" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 14:56:25 -0800 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 Cc: Yu-cheng Yu Subject: [PATCH v19 10/25] x86/mm: Update pte_modify for _PAGE_COW Date: Wed, 3 Feb 2021 14:55:32 -0800 Message-Id: <20210203225547.32221-11-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210203225547.32221-1-yu-cheng.yu@intel.com> References: <20210203225547.32221-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 --- arch/x86/include/asm/pgtable.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 4b0ec61510dc..4a7fd3e5b7df 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -726,6 +726,21 @@ 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); + + if (cpu_feature_enabled(X86_FEATURE_SHSTK) && pte_dirty(pte)) { + pte = pte_mkclean(pte); + + if (pte_flags(pte) & _PAGE_RW) + pte = pte_set_flags(pte, _PAGE_DIRTY); + else + pte = pte_set_flags(pte, _PAGE_COW); + } + return pte_val(pte); +} + static inline pte_t pte_modify(pte_t pte, pgprot_t newprot) { pteval_t val = pte_val(pte), oldval = val; @@ -736,16 +751,34 @@ 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); + + if (cpu_feature_enabled(X86_FEATURE_SHSTK) && pmd_dirty(pmd)) { + pmd = pmd_mkclean(pmd); + + if (pmd_flags(pmd) & _PAGE_RW) + pmd = pmd_set_flags(pmd, _PAGE_DIRTY); + else + pmd = pmd_set_flags(pmd, _PAGE_COW); + } + 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