From patchwork Wed Dec 1 23:17:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Matthew Wilcox (Oracle)" X-Patchwork-Id: 12651299 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 D5D0BC433F5 for ; Wed, 1 Dec 2021 23:18:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1FCAE6B0072; Wed, 1 Dec 2021 18:18:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 187B76B0073; Wed, 1 Dec 2021 18:18:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F41026B0074; Wed, 1 Dec 2021 18:18:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0171.hostedemail.com [216.40.44.171]) by kanga.kvack.org (Postfix) with ESMTP id E18906B0072 for ; Wed, 1 Dec 2021 18:18:24 -0500 (EST) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id A6E138249980 for ; Wed, 1 Dec 2021 23:18:14 +0000 (UTC) X-FDA: 78870790908.19.14151FA Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf15.hostedemail.com (Postfix) with ESMTP id 97F6CD00009D for ; Wed, 1 Dec 2021 23:18:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:In-Reply-To:References; bh=vqhS0MxjHa7zDztzVECBYpbamPlzuJiCatylnQYjPSU=; b=OnSlE2BEqB02YzZY+TLzBhC/14 4oIiN3UTavcfthBa+g5yJ/DzBsWdRLYXdoqwBxRLzsWO4lc1jwPQr5x+bUG8FAc7+I+bSmwLVvYoq KaQPn+fDVIYbpbr6UWqePzWVGRfLidr+fJ0Iev1sdcl8Od0/7oVyyqmvzu99WZ9DfD4wsOb5QIqir HKNcoJA1FTIdcd68Dpo7amgxWCFaC6P+v/N+rAPIocKVtGSXiCpipwRud0v4xDnHdON1UN8bFa4Lm 4zLIIQTJ8fq4+bQ8wS2k41RhhtGlGXcKC5/ynX9XVYllD+Wz6kS0ApzLF/as7Z/127D5Fpjw3SbNW lRwcKNxQ==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1msYrS-001PG4-CE; Wed, 01 Dec 2021 23:18:02 +0000 From: "Matthew Wilcox (Oracle)" To: Linus Torvalds , Jan Kara , Kirill Shutemov , Oleg Nesterov , Christoph Hellwig , Jann Horn , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Mike Kravetz Cc: "Matthew Wilcox (Oracle)" Subject: [5.4 PATCH] mm/gup: Do not force a COW break on file-backed memory Date: Wed, 1 Dec 2021 23:17:57 +0000 Message-Id: <20211201231757.332199-1-willy@infradead.org> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 97F6CD00009D X-Stat-Signature: xyubzxh5upytz36h6hr5i553o7wuu8y8 Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=OnSlE2BE; dmarc=none; spf=none (imf15.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org X-HE-Tag: 1638400693-923614 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: Commit 17839856fd58 ("gup: document and work around "COW can break either way" issue") forces a COW break, even for read-only GUP. This interacts badly with CONFIG_READ_ONLY_THP_FOR_FS as it tries to write to a read-only PMD and follow_trans_huge_pmd() returns NULL which induces an endless loop as __get_user_pages() interprets that as page-not-present, tries to fault it in and retries the follow_page_mask(). The issues fixed by 17839856fd58 don't apply to files. We know which way the COW breaks; the page cache keeps the original and any modifications are private to that process. There's no optimisation that allows a process to reuse a file-backed MAP_PRIVATE page. So we can skip the breaking of the COW for file-backed mappings. This problem only exists in v5.4.y; other stable kernels either predate CONFIG_READ_ONLY_THP_FOR_FS or they include commit a308c71bf1e6 ("mm/gup: Remove enfornced COW mechanism"). Signed-off-by: Matthew Wilcox (Oracle) --- mm/gup.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mm/gup.c b/mm/gup.c index 3ef769529548..d55e02411010 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -176,7 +176,8 @@ static inline bool can_follow_write_pte(pte_t pte, unsigned int flags) */ static inline bool should_force_cow_break(struct vm_area_struct *vma, unsigned int flags) { - return is_cow_mapping(vma->vm_flags) && (flags & FOLL_GET); + return is_cow_mapping(vma->vm_flags) && vma_is_anonymous(vma) && + (flags & FOLL_GET); } static struct page *follow_page_pte(struct vm_area_struct *vma,