Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp2177837pxb; Wed, 9 Feb 2022 12:32:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJx5hqovOaYPVJMiNN8nGC6Dpw2QH03cREllvmteBa9Ew7W60nr6UGt4YKqpogzCAJEOW4cs X-Received: by 2002:a05:6a00:21cb:: with SMTP id t11mr4037536pfj.16.1644438736729; Wed, 09 Feb 2022 12:32:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644438736; cv=none; d=google.com; s=arc-20160816; b=OtDKME0tPnCCe4o+Cn4jsy4Ht54LKw3e8lWIq4LxzO12hTUfO1EJ9Mg+ZptMy3B2sL bVFov+7mOLQbW/xv9B0BPHJF3b4zIuarQVexEU8NqTbaTJM4w1Z5JAIK9BK5INGk8Oau FBidZNbZMLP01vuX0G0F/9hNDCm872plgnTsM66tkjvS7yRey2SsMUxcpBC4dKw75G33 UGIWOUU4TP2fHcOuJ9AikEqHIPkyGmRqJyQYvFqkumNSD9efgoIgpDdOhPCiLsDVrsTY B6jA/ba7OAX/kN0U+RmKO2QznrmzVb+YibNsqzYn5QAPAmuSw65FuXSDxggNN1GhqBNQ A8iA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :from:references:cc:to:content-language:user-agent:mime-version:date :message-id:dkim-signature; bh=23uqM79aNAEYzv/W85ZduUsbrg45lgiV5rKLeXwtvQ0=; b=bxCIRiFGmJHqTylLs2KKVCZ35XFJyXOapuEDjSZmkdYLs7aER61BiZjh6e40+HLVPL VQYVsMCFRv2bz0sBC4T6akGwfnrqbOIn0FkE+491bDYi65sJdQat7sl6ZtkL1PMmZqWb udmCGm4Zas9TL3nLQOFHxS+BEPJwPOmlARiexxgvpqXyYvKhDIQyMCuHAroGv8QUPa/2 reQk22dUuGeXKAhXiwz2lytqC9rSDxHCu/3WxhWumuif9fWRAmwx6ThOymI4ZRQ00zDZ /7M2+B3Ki/4CL5cBUppt4PUx4TtskYMDaH3RBeBTFtEvSo2tTXAz3unW8ibNiYQsNCN+ samw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=neUdap2c; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id t12si16957812pgh.302.2022.02.09.12.32.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 12:32:16 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=neUdap2c; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 89F66E073E36; Wed, 9 Feb 2022 12:07:03 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240007AbiBISj7 (ORCPT + 99 others); Wed, 9 Feb 2022 13:39:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240115AbiBISjg (ORCPT ); Wed, 9 Feb 2022 13:39:36 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF5B0C050CFA; Wed, 9 Feb 2022 10:39:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1644431944; x=1675967944; h=message-id:date:mime-version:to:cc:references:from: subject:in-reply-to:content-transfer-encoding; bh=gzDP3bKlNEdaxBl3I199taLYxCzYF6uSO57sfhfmuKE=; b=neUdap2cEHD8CfDO449futtLaLobtS5EZPb1FNguEXCTWst4YvHgfLGT gi/UxulIzmNrU0YNbCPEqnG0AlxPlqwnXWB6lv+Jk4f/fo8Vlf3Qv74o7 Hdp0+iSJxGUsCySypvZiAauPxQ0DwVXGljmt0Z+0UUlXyb3dfXOK7/c/8 sraiFeI8SNykjrJfWjGj97+uquj7CozNLzW158Qi03tjRwS9q2PA3f6Kv SqMLA++7YYgjuQv5FAW6so5v8SeWxp2zMpPWOv4m9crhCnQ+NQIt2cHmA Gv/GxMhXc5iVJyNRo+Ay1GQDAO7knx0y4NUYiBsVhRy46wgt9WjM2cCtY A==; X-IronPort-AV: E=McAfee;i="6200,9189,10252"; a="312592652" X-IronPort-AV: E=Sophos;i="5.88,356,1635231600"; d="scan'208";a="312592652" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Feb 2022 10:30:41 -0800 X-IronPort-AV: E=Sophos;i="5.88,356,1635231600"; d="scan'208";a="485362361" Received: from sanvery-mobl.amr.corp.intel.com (HELO [10.212.232.139]) ([10.212.232.139]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Feb 2022 10:30:40 -0800 Message-ID: <21f5b129-e52a-74b7-6c8b-2bf0ab3db649@intel.com> Date: Wed, 9 Feb 2022 10:30:37 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Content-Language: en-US To: Rick Edgecombe , 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" , Dave Martin , Weijiang Yang , "Kirill A . Shutemov" , joao.moreira@intel.com, John Allen , kcc@google.com, eranian@google.com Cc: Yu-cheng Yu References: <20220130211838.8382-1-rick.p.edgecombe@intel.com> <20220130211838.8382-13-rick.p.edgecombe@intel.com> From: Dave Hansen Subject: Re: [PATCH 12/35] x86/mm: Update ptep_set_wrprotect() and pmdp_set_wrprotect() for transition from _PAGE_DIRTY to _PAGE_COW In-Reply-To: <20220130211838.8382-13-rick.p.edgecombe@intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,RDNS_NONE,SPF_HELO_NONE, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 1/30/22 13:18, Rick Edgecombe wrote: > From: Yu-cheng Yu > > When Shadow Stack is introduced, [R/O + _PAGE_DIRTY] PTE is reserved for > shadow stack. Copy-on-write PTEs have [R/O + _PAGE_COW]. Another way to refer to these PTEs. In the last patch, it was: "read-only and Dirty PTE" and now: "[R/O + _PAGE_DIRTY]" > When a PTE goes from [R/W + _PAGE_DIRTY] to [R/O + _PAGE_COW], it could > become a transient shadow stack PTE in two cases: > > The first case is that some processors can start a write but end up seeing > a read-only PTE by the time they get to the Dirty bit, creating a transient > shadow stack PTE. However, this will not occur on processors supporting > Shadow Stack, and a TLB flush is not necessary. > > The second case is that when _PAGE_DIRTY is replaced with _PAGE_COW non- > atomically, a transient shadow stack PTE can be created as a result. > Thus, prevent that with cmpxchg. == Background == Shadow stack PTEs always have [Write=0,Dirty=1]. As currently implemented, ptep_set_wrprotect() simply clears _PAGE_RW: (Write=1 -> Write=0). == Problem == This could cause a problem if ptep_set_wrprotect() caused a PTE to transition from: [Write=1,Dirty=1] to [Write=0,Dirty=1] Which would inadvertently create a shadow stack PTE instead of write-protecting it. ptep_set_wrprotect() can not simply check for the Dirty=1 bit because the hardware can set it at any time. == Solution == Perform a compare-and-exchange operation on the PTE to avoid racing with the hardware. The cmpxchg is expected to be more expensive than the existing clear_bit(). Continue using the cheaper clear_bit() on when shadow stacks are not in play. > diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h > index 5c3886f6ccda..e1061b9cba6a 100644 > --- a/arch/x86/include/asm/pgtable.h > +++ b/arch/x86/include/asm/pgtable.h > @@ -1295,6 +1295,24 @@ static inline void ptep_clear(struct mm_struct *mm, unsigned long addr, > static inline void ptep_set_wrprotect(struct mm_struct *mm, > unsigned long addr, pte_t *ptep) > { > + /* > + * If Shadow Stack is enabled, pte_wrprotect() moves _PAGE_DIRTY > + * to _PAGE_COW (see comments at pte_wrprotect()). > + * When a thread reads a RW=1, Dirty=0 PTE and before changing it > + * to RW=0, Dirty=0, another thread could have written to the page > + * and the PTE is RW=1, Dirty=1 now. Use try_cmpxchg() to detect > + * PTE changes and update old_pte, then try again. > + */ I think we can trim that down. We don't need to explain what cmpxchg does or why it loops. That's way too much detail that we don't need. Maybe: /* * Avoid accidentally creating shadow stack PTEs * (Write=0,Dirty=1). Use cmpxchg() to prevent races with * the hardware setting Dirty=1. */ BTW, is it *really* a problem with other threads setting Dirty=1? This is happening under the page table lock on this side at least. > + if (cpu_feature_enabled(X86_FEATURE_SHSTK)) { > + pte_t old_pte, new_pte; > + > + old_pte = READ_ONCE(*ptep); > + do { > + new_pte = pte_wrprotect(old_pte); > + } while (!try_cmpxchg(&ptep->pte, &old_pte.pte, new_pte.pte)); > + > + return; > + } > clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte); > }