Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp4068439pxu; Mon, 12 Oct 2020 08:42:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwZil1dgDZYYDN+dChijQbE505DK4A+6B9RSjcZbbl6FzmXreJ5p6FJ2Ogj3AcrVWyKnGL0 X-Received: by 2002:a17:906:1484:: with SMTP id x4mr28314208ejc.81.1602517323323; Mon, 12 Oct 2020 08:42:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602517323; cv=none; d=google.com; s=arc-20160816; b=zfoRgdbZY52/3kxGekn+jSVc6nf01veBA+2WvltllbHx6e5Kw/8i6Z7c2dcBTTEpih KewaCwQgPBxMAaS6DAXC2r3678pqxa1P9T72Cuv5JvkHAhQjOJxMCXohDNnPK3c0Svpa ocBroVJ/pT7JZrhfAvquV5Eaxine+h9ZEX3Ejnehs3WFMdERz/49XCCbz9b/Q41mIZqX GFGtYtyPE6HdJfcdYXNiQeyRTNKc7iSXSrfc5wbCyLXLuRw8WaSEjjATPvcCw2hUGw+w 2kQadA5YZXWZiVuzN8VCEL72BV+JMVfWYConceNiUEnDP9vGMGMMF4tWSH85m8UYFYwe tsFw== 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=eCCXYhPv5Rw+Qzac1RnQCrk+bjIRd2XTjy9tE1rfOF0=; b=t37mt8s5dcJ4fqpZGXcl2HcFYz4N9ou7PvLdUzDzJyjNamCziNbO6N6YJORDjtVRNn M2jAeLGg4jqwz+Wp9JS5Ot6tjSQE0QyNbFYADhCvZaSmDlQ10K5YVD0NCDd4oUuJHQnw 2X0C5GZsRevD9vDQZx4wcblp5ZbVxJYDQhLzCCcbE9Nm2a7zoUhXO1lQtnlSDJRTeCiQ 9lFiCNSPVaLYlmaKpszPx4R5Yzh12d/1gP7idudSsezodYOaE5fp6Rl51ciFY/tjkOtM nI7nvj9rPL53RioouwSCfx+RLcr+j7cFCe9flET1arEZm/QGZUnNO3mMPuF5ekTH/Jc2 7vBg== 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 t21si12366212edy.511.2020.10.12.08.41.40; Mon, 12 Oct 2020 08:42: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 S2390393AbgJLPkJ (ORCPT + 99 others); Mon, 12 Oct 2020 11:40:09 -0400 Received: from mga05.intel.com ([192.55.52.43]:1323 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390146AbgJLPkG (ORCPT ); Mon, 12 Oct 2020 11:40:06 -0400 IronPort-SDR: Wtcys5DjUfKsv4Q3j1KVIY/jPRrfQv8NLuDRZrSWT1ai/3+KXIK3YEjNblMpJLSf7lbMQuKOuL dxMq0lpIHiZg== X-IronPort-AV: E=McAfee;i="6000,8403,9772"; a="250452667" X-IronPort-AV: E=Sophos;i="5.77,367,1596524400"; d="scan'208";a="250452667" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Oct 2020 08:39:50 -0700 IronPort-SDR: SNezZIX3iQfvqLeFwIsoodTD1ldkQ3bTV+V4ctyUJzDe2+SxxsUwRTsrlWWo324VkU9HRXZ+do jqodP9HXgTbg== X-IronPort-AV: E=Sophos;i="5.77,367,1596524400"; d="scan'208";a="530010833" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Oct 2020 08:39:50 -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 Cc: Yu-cheng Yu Subject: [PATCH v14 10/26] x86/mm: Update pte_modify for _PAGE_COW Date: Mon, 12 Oct 2020 08:38:34 -0700 Message-Id: <20201012153850.26996-11-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20201012153850.26996-1-yu-cheng.yu@intel.com> References: <20201012153850.26996-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 Pte_modify() changes a PTE to 'newprot'. It doesn't use the pte_*() helpers that a previous patch fixed up, so we need a new site. Introduce fixup_dirty_pte() to set the dirty bits based on _PAGE_RW, and 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 ac4ed814be96..8d4c09831e67 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -727,6 +727,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_HW); + 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; @@ -737,16 +752,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_HW); + 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