Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp52949imm; Thu, 7 Jun 2018 13:39:57 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIM4MMB36JhZODagjPhprylw0UrgvzFtB18FnUgL/9g7OJIxHsQPcjZ/g6XJUhzzftQZPs7 X-Received: by 2002:a62:9bc9:: with SMTP id e70-v6mr3094870pfk.15.1528403996938; Thu, 07 Jun 2018 13:39:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528403996; cv=none; d=google.com; s=arc-20160816; b=MtFlzSxLVr0qe2O4UHdMvzTuXqT1cm30Hbvt2XcNB3DTPzqaFEPZFx47Wv4NQTJsgD u5Uf3pj/wUfsXvskTwfv1WF+Wxe2yTn+23VaT/u+KNtOb/kBBUhM7rSWnOV8whQnjjN7 wxokgY7yJ6+AewJzWm8Oo2/0FtcLPoMy28evIxGlvL27kbv6dxz0bLdQuneHj7XazYKI NvBNnbnFDK2EEKOglqmrmTji1cmSub7OHJRDPTLvRWOSav5GGQy9xsPx4aVXVuFmnWi6 WswapM62WCgTB/ufV2aYQ47FaWDu8edknpWJv2/4Uh6t/hJ8mHD+TdHw5mpPUpVPSlyo CoLQ== 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=p01WtAVX0W7xNpxlaqT4xbPoJnyLAaP6U71R86qwQtc=; b=pwmh85OFbXM0IEsJoomsMJvYdSVaV9iM1gAYR3fCNGWL7ChGYCYqrLFXs3MGDaNQVU xBqjJdmSW+HAAFSseStNDxOs5DmpM2eYuk00f7QS4GWWSR+UaAmKkkLu3QtIQkyG5Ajx +Tx5Qn2Ho4E5UJs+mOIl8zOHf/4NWS5yzm2jP6Zy/uKowXOCYUwemMM3PRyz9OaoEsLW tGJxIbv+leSpoDNoUX4w88P1MgTnaOtvLNzLTXaz05BWo1vYWu7yqniZDDHApzfycB9y CMDvlCGFNhseYhjYM8Un8WUANIYJVA/JQePPNaYEP50dRvXEYDuxHFLczFCfYhbFr3EH hyaQ== 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 m89-v6si11006944pfj.192.2018.06.07.13.39.43; Thu, 07 Jun 2018 13:39:56 -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 S932683AbeFGUjP (ORCPT + 99 others); Thu, 7 Jun 2018 16:39:15 -0400 Received: from mga12.intel.com ([192.55.52.136]:42265 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753220AbeFGUjN (ORCPT ); Thu, 7 Jun 2018 16:39:13 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 07 Jun 2018 13:39:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,488,1520924400"; d="scan'208";a="46099859" Received: from 2b52.sc.intel.com (HELO [143.183.136.51]) ([143.183.136.51]) by fmsmga008.fm.intel.com with ESMTP; 07 Jun 2018 13:39:12 -0700 Message-ID: <1528403761.5265.37.camel@2b52.sc.intel.com> Subject: Re: [PATCH 6/9] x86/mm: Introduce ptep_set_wrprotect_flush and related functions From: Yu-cheng Yu To: Dave Hansen Cc: Andy Lutomirski , LKML , linux-doc@vger.kernel.org, Linux-MM , linux-arch , X86 ML , "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , "H. J. Lu" , "Shanbhogue, Vedvyas" , "Ravi V. Shankar" , Jonathan Corbet , Oleg Nesterov , Arnd Bergmann , mike.kravetz@oracle.com Date: Thu, 07 Jun 2018 13:36:01 -0700 In-Reply-To: <5c39caf1-2198-3c2b-b590-8c38a525747f@linux.intel.com> References: <20180607143705.3531-1-yu-cheng.yu@intel.com> <20180607143705.3531-7-yu-cheng.yu@intel.com> <5c39caf1-2198-3c2b-b590-8c38a525747f@linux.intel.com> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.10.4-0ubuntu2 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 2018-06-07 at 13:29 -0700, Dave Hansen wrote: > On 06/07/2018 09:24 AM, Andy Lutomirski wrote: > > >> +static inline void ptep_set_wrprotect_flush(struct vm_area_struct *vma, > >> + unsigned long addr, pte_t *ptep) > >> +{ > >> + bool rw; > >> + > >> + rw = test_and_clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte); > >> + if (IS_ENABLED(CONFIG_X86_INTEL_SHADOW_STACK_USER)) { > >> + struct mm_struct *mm = vma->vm_mm; > >> + pte_t pte; > >> + > >> + if (rw && (atomic_read(&mm->mm_users) > 1)) > >> + pte = ptep_clear_flush(vma, addr, ptep); > > Why are you clearing the pte? > > I found my notes on the subject. :) > > Here's the sequence that causes the problem. This could happen any time > we try to take a PTE from read-write to read-only. P==Present, W=Write, > D=Dirty: > > CPU0 does a write, sees PTE with P=1,W=1,D=0 > CPU0 decides to set D=1 > CPU1 comes in and sets W=0 > CPU0 does locked operation to set D=1 > CPU0 sees P=1,W=0,D=0 > CPU0 sets back P=1,W=0,D=1 > CPU0 loads P=1,W=0,D=1 into the TLB > CPU0 attempts to continue the write, but sees W=0 in the TLB and a #PF > is generated because of the write fault. > > The problem with this is that we end up with a shadowstack-PTE > (Write=0,Dirty=1) where we didn't want one. This, unfortunately, > imposes extra TLB flushing overhead on the R/W->R/O transitions that > does not exist before shadowstack enabling. > > Yu-cheng, could you please add this to the patch description? I will add that.