Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp619793pxb; Tue, 1 Feb 2022 07:08:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJwjMZYVXVh7F+2or6UysMbR/Q626HKWs9N+9Qt4KHySTYQB/9CrFO5Jgl7mZcVct0uKJhns X-Received: by 2002:a05:6402:348c:: with SMTP id v12mr25834240edc.384.1643728090166; Tue, 01 Feb 2022 07:08:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643728090; cv=none; d=google.com; s=arc-20160816; b=zKGxDzZglQZmrqiBb7aYsYL1VOLqsxWkWzHv1EwM2qGR6agWd4Vghjmop4gqcl/qPR PQqCAWMJeP+Rxbucrrsiik8yrDrIVS5Q8CF9Ly6WtBHZ5sIb22VN5KdOh0+3Fdya+uIa IYd76LtQ+z1/Fxxd8PLM1NeFzHLHgZu3KNxhl1MSvPNqWEyyBA4L3tZSFJQ9DEOsbVdC 1ynoKeE6B7BOKosLlTdJBWKh+mw8HUGV0JrB/AEJAxdFNDjvJkaFqZg6i0KFVg43uQak WceEWj0GE3t2+cz5XeKYG7cKHB7K9fUskhY2UpKNT0bUqy1nuXUhqB0g+v6gdfjke7Dk QSmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=yZT/jq4cfRteVatQDnm1PmlzfYmWNtkkWQz60/dY/a4=; b=R6qemlpRlB/g9nR9fjNGr07CCgF0F15DCmCfXHIWT9KdB1bD9JoqbCtaVHr5BThGjq Xccosu8j03+LfWNOzsXYJwNXGJ351dSC5lF5rRmnFDF2ih88psF8pPozf1PV8OUsnC5l ImtQVG8WTRw9SgZccBGgXI7w9NCG7q8oGUWKpEMCkGzLG4uFtiJmr89CRIZYMx2nkTT1 WNxSUlooUHJa9qTLwkSeihn+VFypNYIPRF9lzTkV05D9Xr9yrV3fSeg1l0sRlVEecTY9 HKOkEwbMFJGquFuQgr6DwuuUMOQFVLoVS01icm/dCXqBgTjlZABQ6hVjfE4Vid8rPEGI mRpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=amZqJmuL; 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=pass (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 qk13si7534591ejc.978.2022.02.01.07.07.44; Tue, 01 Feb 2022 07:08:10 -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; dkim=pass header.i=@intel.com header.s=Intel header.b=amZqJmuL; 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=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356695AbiA3VWZ (ORCPT + 99 others); Sun, 30 Jan 2022 16:22:25 -0500 Received: from mga06.intel.com ([134.134.136.31]:52029 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356487AbiA3VV5 (ORCPT ); Sun, 30 Jan 2022 16:21:57 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643577717; x=1675113717; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=HHh4lkLfyu0d0fqFw9G2b577iSJVRa+u0pjug6+8QXs=; b=amZqJmuLHKZtS2FgwRnWB0MfOwEq1+9sd3lO13l9Cbt6syHJjKjCftZA oREnnzqWUMipU0FI+QUrv7nAWnFSWxA25qzi5LHsKOWpK0pZcYwBOWOLA oI6WORC8v35qhSqXqVs+pzdGOE7Ai59tLd+YCPCMWpUdLAwlrrqjnyyZC EFxAHRU/cQUpSS/cEbE8qnDS+CJ2h/IxXmhXJxA7xVxVJAhKbUdcRmrk/ nRQAWC3+Xr7SqwkdgKYTtcBZMcv5RGzeCPE5EpY6VnVVG+CZhdPSSOPKL 7hS5lOGj9VL/nFwSIXuwZ01iYAxh8qc25tplYgttCKMt0XzFSx0VLmd9Q w==; X-IronPort-AV: E=McAfee;i="6200,9189,10243"; a="308104918" X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="308104918" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2022 13:21:55 -0800 X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="536856751" Received: from avmallar-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.123.171]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2022 13:21:54 -0800 From: Rick Edgecombe 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 , "Kirill A . Shutemov" , joao.moreira@intel.com, John Allen , kcc@google.com, eranian@google.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH 11/35] x86/mm: Update pte_modify for _PAGE_COW Date: Sun, 30 Jan 2022 13:18:14 -0800 Message-Id: <20220130211838.8382-12-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220130211838.8382-1-rick.p.edgecombe@intel.com> References: <20220130211838.8382-1-rick.p.edgecombe@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yu-cheng Yu 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 Signed-off-by: Rick Edgecombe --- 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 a4a75e78a934..5c3886f6ccda 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -773,6 +773,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; @@ -783,16 +800,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.17.1