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=ham 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 67DAFC433E0 for ; Mon, 25 Jan 2021 21:27:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D0CD92083E for ; Mon, 25 Jan 2021 21:27:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D0CD92083E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 517708D004D; Mon, 25 Jan 2021 16:27:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4C75B8D0023; Mon, 25 Jan 2021 16:27:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3DE288D004D; Mon, 25 Jan 2021 16:27:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0243.hostedemail.com [216.40.44.243]) by kanga.kvack.org (Postfix) with ESMTP id 283788D0023 for ; Mon, 25 Jan 2021 16:27:58 -0500 (EST) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id E267D8249980 for ; Mon, 25 Jan 2021 21:27:57 +0000 (UTC) X-FDA: 77745584994.09.tooth53_4d0f36f27589 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin09.hostedemail.com (Postfix) with ESMTP id C7A5C180AD820 for ; Mon, 25 Jan 2021 21:27:57 +0000 (UTC) X-HE-Tag: tooth53_4d0f36f27589 X-Filterd-Recvd-Size: 5231 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by imf44.hostedemail.com (Postfix) with ESMTP for ; Mon, 25 Jan 2021 21:27:56 +0000 (UTC) IronPort-SDR: e0gP7OXxj6dEo3PNesvp2dKXaKA3SZK9wYuUsFMfGZTDCnRNfK0Fm4XGYnsGxu4ssran3PVtFs l1YtMwgA53NQ== X-IronPort-AV: E=McAfee;i="6000,8403,9875"; a="166903813" X-IronPort-AV: E=Sophos;i="5.79,374,1602572400"; d="scan'208";a="166903813" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jan 2021 13:27:54 -0800 IronPort-SDR: Zpt6kh4eRCAa6nhreOyas/x36owT9wmvFrJuKFK1hHDfdutyCbNqyanAiEN4kb+/4ZGudwMisH PLl97Bf6SEEg== X-IronPort-AV: E=Sophos;i="5.79,374,1602572400"; d="scan'208";a="429441203" Received: from yyu32-mobl1.amr.corp.intel.com (HELO [10.212.60.232]) ([10.212.60.232]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jan 2021 13:27:52 -0800 Subject: Re: [PATCH v17 11/26] x86/mm: Update ptep_set_wrprotect() and pmdp_set_wrprotect() for transition from _PAGE_DIRTY to _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-12-yu-cheng.yu@intel.com> <20210125182709.GC23290@zn.tnic> From: "Yu, Yu-cheng" Message-ID: <8084836b-4990-90e8-5c9a-97a920f0239e@intel.com> Date: Mon, 25 Jan 2021 13:27:51 -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: <20210125182709.GC23290@zn.tnic> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 1/25/2021 10:27 AM, Borislav Petkov wrote: > On Tue, Dec 29, 2020 at 01:30:38PM -0800, Yu-cheng Yu wrote: [...] >> diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h >> index 666c25ab9564..1c84f1ba32b9 100644 >> --- a/arch/x86/include/asm/pgtable.h >> +++ b/arch/x86/include/asm/pgtable.h >> @@ -1226,6 +1226,32 @@ static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm, >> static inline void ptep_set_wrprotect(struct mm_struct *mm, >> unsigned long addr, pte_t *ptep) >> { >> + /* >> + * Some processors can start a write, but end up seeing a read-only >> + * PTE by the time they get to the Dirty bit. In this case, they >> + * will set the Dirty bit, leaving a read-only, Dirty PTE which >> + * looks like a shadow stack PTE. >> + * >> + * However, this behavior has been improved > > Improved how? Processors supporting Shadow Stack will not set a read-only PTE's dirty bit. I will revise the comments. >> and will not occur on >> + * processors supporting Shadow Stack. Without this guarantee, a > > Which guarantee? That it won't happen on CPUs which support SHSTK? > Yes. >> + * transition to a non-present PTE and flush the TLB would be > > s/flush the TLB/TLB flush/ > >> + * needed. >> + * >> + * When changing a writable PTE to read-only and if the PTE has >> + * _PAGE_DIRTY set, move that bit to _PAGE_COW so that the PTE is >> + * not a shadow stack PTE. >> + */ > > This sentence doesn't belong here as it refers to what pte_wrprotect() > does. You could expand the comment in pte_wrprotect() with this here as > it is better. I will move this paragraph to pte_wrprotect(). > >> + if (cpu_feature_enabled(X86_FEATURE_SHSTK)) { >> + pte_t old_pte, new_pte; >> + >> + do { >> + old_pte = READ_ONCE(*ptep); >> + new_pte = pte_wrprotect(old_pte); > > Maybe I'm missing something but those two can happen outside of the > loop, no? Or is *ptep somehow changing concurrently while the loop is > doing the CMPXCHG and you need to recreate it each time? > > IOW, you can generate upfront and do the empty loop... *ptep can change concurrently. > >> + >> + } while (!try_cmpxchg(&ptep->pte, &old_pte.pte, new_pte.pte)); >> + >> + return; >> + } >> clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte); >> } >> [...]