From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0CA4EC433DB for ; Thu, 21 Jan 2021 20:21:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C710123A54 for ; Thu, 21 Jan 2021 20:21:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727177AbhAUUVE (ORCPT ); Thu, 21 Jan 2021 15:21:04 -0500 Received: from mga04.intel.com ([192.55.52.120]:49885 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726987AbhAUUTN (ORCPT ); Thu, 21 Jan 2021 15:19:13 -0500 IronPort-SDR: EdP40jXPw+KKSV3cqCCoVhFR0wuD/kbzmYahGjOhdWGHqM0tcOXfR6ncx1qqB99ytQSnFcD+QI v2h7TbUiuFdg== X-IronPort-AV: E=McAfee;i="6000,8403,9871"; a="176764782" X-IronPort-AV: E=Sophos;i="5.79,365,1602572400"; d="scan'208";a="176764782" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Jan 2021 12:16:26 -0800 IronPort-SDR: Xeb6GP6QHS5E+NUN4IsRGZ97r6GOsWNpHXOAkde9SaUDTqrCmzanAvvZHRo2+bsJULzAS4gGR+ RVNS7GFSD8vw== X-IronPort-AV: E=Sophos;i="5.79,365,1602572400"; d="scan'208";a="385443835" Received: from yyu32-mobl1.amr.corp.intel.com (HELO [10.209.46.254]) ([10.209.46.254]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Jan 2021 12:16:24 -0800 Subject: Re: [PATCH v17 08/26] x86/mm: Introduce _PAGE_COW To: Borislav Petkov Cc: 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 , 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 References: <20201229213053.16395-1-yu-cheng.yu@intel.com> <20201229213053.16395-9-yu-cheng.yu@intel.com> <20210121184405.GE32060@zn.tnic> From: "Yu, Yu-cheng" Message-ID: Date: Thu, 21 Jan 2021 12:16:23 -0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.1 MIME-Version: 1.0 In-Reply-To: <20210121184405.GE32060@zn.tnic> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 1/21/2021 10:44 AM, Borislav Petkov wrote: > On Tue, Dec 29, 2020 at 01:30:35PM -0800, Yu-cheng Yu wrote: [...] >> @@ -343,6 +349,16 @@ static inline pte_t pte_mkold(pte_t pte) >> >> static inline pte_t pte_wrprotect(pte_t pte) >> { >> + /* >> + * Blindly clearing _PAGE_RW might accidentally create >> + * a shadow stack PTE (RW=0, Dirty=1). Move the hardware >> + * dirty value to the software bit. >> + */ >> + if (cpu_feature_enabled(X86_FEATURE_SHSTK)) { >> + pte.pte |= (pte.pte & _PAGE_DIRTY) >> _PAGE_BIT_DIRTY << _PAGE_BIT_COW; > > Why the unreadable shifting when you can simply do: > > if (pte.pte & _PAGE_DIRTY) > pte.pte |= _PAGE_COW; > > ? It clears _PAGE_DIRTY and sets _PAGE_COW. That is, if (pte.pte & _PAGE_DIRTY) { pte.pte &= ~_PAGE_DIRTY; pte.pte |= _PAGE_COW; } So, shifting makes resulting code more efficient. >> @@ -434,16 +469,40 @@ static inline pmd_t pmd_mkold(pmd_t pmd) >> >> static inline pmd_t pmd_mkclean(pmd_t pmd) >> { >> - return pmd_clear_flags(pmd, _PAGE_DIRTY); >> + return pmd_clear_flags(pmd, _PAGE_DIRTY_BITS); >> } >> >> static inline pmd_t pmd_wrprotect(pmd_t pmd) >> { >> + /* >> + * Blindly clearing _PAGE_RW might accidentally create >> + * a shadow stack PMD (RW=0, Dirty=1). Move the hardware >> + * dirty value to the software bit. >> + */ >> + if (cpu_feature_enabled(X86_FEATURE_SHSTK)) { >> + pmdval_t v = native_pmd_val(pmd); >> + >> + v |= (v & _PAGE_DIRTY) >> _PAGE_BIT_DIRTY << _PAGE_BIT_COW; > > As above. > >> @@ -488,17 +554,35 @@ static inline pud_t pud_mkold(pud_t pud) >> >> static inline pud_t pud_mkclean(pud_t pud) >> { >> - return pud_clear_flags(pud, _PAGE_DIRTY); >> + return pud_clear_flags(pud, _PAGE_DIRTY_BITS); >> } >> >> static inline pud_t pud_wrprotect(pud_t pud) >> { >> + /* >> + * Blindly clearing _PAGE_RW might accidentally create >> + * a shadow stack PUD (RW=0, Dirty=1). Move the hardware >> + * dirty value to the software bit. >> + */ >> + if (cpu_feature_enabled(X86_FEATURE_SHSTK)) { >> + pudval_t v = native_pud_val(pud); >> + >> + v |= (v & _PAGE_DIRTY) >> _PAGE_BIT_DIRTY << _PAGE_BIT_COW; > > Ditto. > >> @@ -1131,6 +1222,12 @@ extern int pmdp_clear_flush_young(struct vm_area_struct *vma, >> #define pmd_write pmd_write >> static inline int pmd_write(pmd_t pmd) >> { >> + /* >> + * If _PAGE_DIRTY is set, then the PMD must either have _PAGE_RW or >> + * be a shadow stack PMD, which is logically writable. >> + */ >> + if (cpu_feature_enabled(X86_FEATURE_SHSTK)) >> + return pmd_flags(pmd) & (_PAGE_RW | _PAGE_DIRTY); > > else > > >> return pmd_flags(pmd) & _PAGE_RW; >> } >>