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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 035EBC4332F for ; Wed, 9 Feb 2022 22:50:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8A23A6B0075; Wed, 9 Feb 2022 17:50:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8516D6B0078; Wed, 9 Feb 2022 17:50:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6F60E6B007B; Wed, 9 Feb 2022 17:50:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0049.hostedemail.com [216.40.44.49]) by kanga.kvack.org (Postfix) with ESMTP id 610BB6B0075 for ; Wed, 9 Feb 2022 17:50:35 -0500 (EST) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 1DC6398C0A for ; Wed, 9 Feb 2022 22:50:35 +0000 (UTC) X-FDA: 79124737230.25.A205C84 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by imf27.hostedemail.com (Postfix) with ESMTP id E8B1540003 for ; Wed, 9 Feb 2022 22:50:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1644447034; x=1675983034; h=message-id:date:mime-version:to:cc:references:from: subject:in-reply-to:content-transfer-encoding; bh=4Pwqb82pRVxdynG25KCR6umaUemEEUALqmeKoSY7z9M=; b=eDUJ7WBnY6NE2nOHzmVzmmc18kEPwJhTJSvHz18a561zA64zhKQVlbc+ 4M1GkBBA65wHUZQWAAEIHAD1LrgI/29stDp4dwTqVYQThLlWuaku3skQu 2AwcaMtPKI3pHlfCdJnbP4B+A0mZyZ+NBd0pbaUfrZMMQifkdRkTD+D6j cuRY0g/Fjv4D8ZiIsSIsIOTHKBKYSSMdK2LKVoNHAfuZULCUDRU1kVFe8 9SpscqHMGdycZ++NmCDGzxHPAZcwv98oEDkkFO3iUOXl/E3q6LsBdfZlu jIsyeFhRU5Q/+biWzuuKJhLEupq8gE/EPPp9B6uex51zIUWmksd/xewvW g==; X-IronPort-AV: E=McAfee;i="6200,9189,10253"; a="248195556" X-IronPort-AV: E=Sophos;i="5.88,356,1635231600"; d="scan'208";a="248195556" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Feb 2022 14:50:32 -0800 X-IronPort-AV: E=Sophos;i="5.88,356,1635231600"; d="scan'208";a="701445624" Received: from sanvery-mobl.amr.corp.intel.com (HELO [10.212.232.139]) ([10.212.232.139]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Feb 2022 14:50:31 -0800 Message-ID: <74038286-6ff3-7eb2-ea65-2e223a894900@intel.com> Date: Wed, 9 Feb 2022 14:50:27 -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-21-rick.p.edgecombe@intel.com> From: Dave Hansen Subject: Re: [PATCH 20/35] mm: Update can_follow_write_pte() for shadow stack In-Reply-To: <20220130211838.8382-21-rick.p.edgecombe@intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Stat-Signature: chhfkyff653pjy1x4otq8qxfygkwcu4i Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=eDUJ7WBn; spf=none (imf27.hostedemail.com: domain of dave.hansen@intel.com has no SPF policy when checking 192.55.52.120) smtp.mailfrom=dave.hansen@intel.com; dmarc=pass (policy=none) header.from=intel.com X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: E8B1540003 X-HE-Tag: 1644447033-373013 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/30/22 13:18, Rick Edgecombe wrote: > From: Yu-cheng Yu > > Can_follow_write_pte() ensures a read-only page is COWed by checking the > FOLL_COW flag, and uses pte_dirty() to validate the flag is still valid. > > Like a writable data page, a shadow stack page is writable, and becomes > read-only during copy-on-write, I thought we could not have read-only shadow stack pages. What does a read-only shadow stack PTE look like? ;) > but it is always dirty. Thus, in the > can_follow_write_pte() check, it belongs to the writable page case and > should be excluded from the read-only page pte_dirty() check. Apply > the same changes to can_follow_write_pmd(). > > While at it, also split the long line into smaller ones. FWIW, I probably would have had a preparatory patch for this part. The advantage is that if you break existing code, it's a lot easier to figure it out if you have a separate refactoring patch. Also, for a patch like this, the refactoring might result in the same exact binary. It's a pretty good sign that your patch won't cause regressions if it results in the same binary. > diff --git a/mm/gup.c b/mm/gup.c > index f0af462ac1e2..95b7d1084c44 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -464,10 +464,18 @@ static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address, > * FOLL_FORCE can write to even unwritable pte's, but only > * after we've gone through a COW cycle and they are dirty. > */ > -static inline bool can_follow_write_pte(pte_t pte, unsigned int flags) > +static inline bool can_follow_write_pte(pte_t pte, unsigned int flags, > + struct vm_area_struct *vma) > { > - return pte_write(pte) || > - ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte)); > + if (pte_write(pte)) > + return true; > + if ((flags & (FOLL_FORCE | FOLL_COW)) != (FOLL_FORCE | FOLL_COW)) > + return false; > + if (!pte_dirty(pte)) > + return false; > + if (is_shadow_stack_mapping(vma->vm_flags)) > + return false; You had me up until this is_shadow_stack_mapping(). It wasn't mentioned at all in the changelog. Logically, I think it's trying to say that a shadow stack VMA never allows a FOLL_FORCE override. That makes some sense, but it's a pretty big point not to mention in the changelog. > + return true; > } > > static struct page *follow_page_pte(struct vm_area_struct *vma, > @@ -510,7 +518,7 @@ static struct page *follow_page_pte(struct vm_area_struct *vma, > } > if ((flags & FOLL_NUMA) && pte_protnone(pte)) > goto no_page; > - if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) { > + if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags, vma)) { > pte_unmap_unlock(ptep, ptl); > return NULL; > }