Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp95799imm; Thu, 30 Aug 2018 09:07:43 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYbsGtv3UbEQ/YcOA03603tM2ZPAhDEZ3y9MUPZ+q7xmg/mIFhnat2Ah26ZVNYAt2VzO4br X-Received: by 2002:a63:5b1b:: with SMTP id p27-v6mr10346734pgb.322.1535645263804; Thu, 30 Aug 2018 09:07:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535645263; cv=none; d=google.com; s=arc-20160816; b=jxsrlp5P7eUtv0CrndnCkx+Myr8uGBIuNFSY6DSV7cUHvKMUjkTTb4zwFTGL9uD/zJ Y1JNhXxYbPxwfyL2T9CRo2vidxXsFqm1KNwqRI90L244p9hk+TwRW0WBtJudkgbB1X/Q xOYKPc0OgRofXXneuyf560YpodWmOVOmNJDIklkbbi5k/V4pNDO4c2d39QDhbFhIzd7X kcn1K8Mjj3lNve/T+NxJ7HUePvVpGHxjvzBffvo21YLDHdSFt6tBigxMnNj1ZJLwX9Jq 35+zXl5qcB7WzDk2TLlNrBwUeoLCRkarp73YsuQl6RGDN6/NCdLGNBqP8ICo2blHEbCF nzCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:date:cc:to:from:subject:message-id :arc-authentication-results; bh=NMlB8CYcf7I0vPn2AoUqy3aM/nd/mghah8AozkL77+k=; b=rkLHYf5IQ3pTYVhS/Lyo7x93s9rX9FLPES5moDOHLuKH6UdAe6Sjfp9ym3VLtPW5cY lP7ybGz2CFuyE11kwzpZpmMzO0ztRaD3jO0laPO7Mq2fzx0zPITcMcuqXEpRLSU/S5Bd 6JUf47i5A3SXrbPgAEkgppnD/6TvbQ/XdDa/o8i9ycgcfKDN0Hdo1MxLTLCaH3bflJ8M ytxfmu8LOLxsWsmSeTwtHgqVwWtTbGgf6+5M72oCFVGcfQ6hCV/wQf/1jtb6B+TrUL5L tnyZqMZPKcW6XznXt+MyghC80tjwB0Hj9hII2MVQk5REgN4Jyi8ZStlVUxZG9QHpEAXJ quMA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id j19-v6si7154558pgb.623.2018.08.30.09.07.28; Thu, 30 Aug 2018 09:07:43 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1727453AbeH3UJK (ORCPT + 99 others); Thu, 30 Aug 2018 16:09:10 -0400 Received: from mga05.intel.com ([192.55.52.43]:51126 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726592AbeH3UJK (ORCPT ); Thu, 30 Aug 2018 16:09:10 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Aug 2018 09:06:19 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,307,1531810800"; d="scan'208";a="84719735" Received: from 2b52.sc.intel.com ([143.183.136.52]) by fmsmga004.fm.intel.com with ESMTP; 30 Aug 2018 09:06:18 -0700 Message-ID: <1535644924.26689.7.camel@intel.com> Subject: Re: [RFC PATCH v3 12/24] x86/mm: Modify ptep_set_wrprotect and pmdp_set_wrprotect for _PAGE_DIRTY_SW From: Yu-cheng Yu To: Jann Horn Cc: the arch/x86 maintainers , "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , kernel list , linux-doc@vger.kernel.org, Linux-MM , linux-arch , Linux API , Arnd Bergmann , Andy Lutomirski , Balbir Singh , Cyrill Gorcunov , Dave Hansen , Florian Weimer , hjl.tools@gmail.com, Jonathan Corbet , keescook@chromiun.org, Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , ravi.v.shankar@intel.com, vedvyas.shanbhogue@intel.com Date: Thu, 30 Aug 2018 09:02:04 -0700 In-Reply-To: References: <20180830143904.3168-1-yu-cheng.yu@intel.com> <20180830143904.3168-13-yu-cheng.yu@intel.com> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.18.5.2-0ubuntu3.2 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 2018-08-30 at 17:49 +0200, Jann Horn wrote: > On Thu, Aug 30, 2018 at 4:43 PM Yu-cheng Yu > wrote: > > > > > > When Shadow Stack is enabled, the read-only and PAGE_DIRTY_HW PTE > > setting is reserved only for the Shadow Stack.  To track dirty of > > non-Shadow Stack read-only PTEs, we use PAGE_DIRTY_SW. > > > > Update ptep_set_wrprotect() and pmdp_set_wrprotect(). > > > > Signed-off-by: Yu-cheng Yu > > --- > >  arch/x86/include/asm/pgtable.h | 42 > > ++++++++++++++++++++++++++++++++++ > >  1 file changed, 42 insertions(+) > > > > diff --git a/arch/x86/include/asm/pgtable.h > > b/arch/x86/include/asm/pgtable.h > > index 4d50de77ea96..556ef258eeff 100644 > > --- a/arch/x86/include/asm/pgtable.h > > +++ b/arch/x86/include/asm/pgtable.h > > @@ -1203,7 +1203,28 @@ 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) > >  { > > +       pte_t pte; > > + > >         clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte); > > +       pte = *ptep; > > + > > +       /* > > +        * Some processors can start a write, but ending 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 and will not > > occur > > +        * on processors supporting Shadow Stacks.  Without this > > +        * guarantee, a transition to a non-present PTE and flush > > the > > +        * TLB would be needed. > > +        * > > +        * When change a writable PTE to read-only and if the PTE > > has > > +        * _PAGE_DIRTY_HW set, we move that bit to _PAGE_DIRTY_SW > > so > > +        * that the PTE is not a valid Shadow Stack PTE. > > +        */ > > +       pte = pte_move_flags(pte, _PAGE_DIRTY_HW, _PAGE_DIRTY_SW); > > +       set_pte_at(mm, addr, ptep, pte); > >  } > I don't understand why it's okay that you first atomically clear the > RW bit, then atomically switch from DIRTY_HW to DIRTY_SW. Doesn't > that > mean that between the two atomic writes, another core can > incorrectly > see a shadow stack? Yes, we had that concern earlier and checked. On processors supporting Shadow Stacks, that will not happen. Yu-cheng