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 X-Spam-Level: X-Spam-Status: No, score=-12.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6FBF8C433E2 for ; Mon, 14 Sep 2020 13:01:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EAD0420672 for ; Mon, 14 Sep 2020 13:00:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="glycpH4Q" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EAD0420672 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 98AB06B0055; Mon, 14 Sep 2020 09:00:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 917578E0003; Mon, 14 Sep 2020 09:00:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 766FD6B005C; Mon, 14 Sep 2020 09:00:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0240.hostedemail.com [216.40.44.240]) by kanga.kvack.org (Postfix) with ESMTP id 5C5DC6B0055 for ; Mon, 14 Sep 2020 09:00:56 -0400 (EDT) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 54CF91805B2FB for ; Mon, 14 Sep 2020 13:00:55 +0000 (UTC) X-FDA: 77261676870.09.grape36_28176f827108 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin09.hostedemail.com (Postfix) with ESMTP id 4190E18013206 for ; Mon, 14 Sep 2020 13:00:51 +0000 (UTC) X-HE-Tag: grape36_28176f827108 X-Filterd-Recvd-Size: 13019 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf35.hostedemail.com (Postfix) with ESMTP for ; Mon, 14 Sep 2020 13:00:50 +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: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=PMOVyDdKtjZVle1vj+CLG+zoWJ/Fx9rNqdgIHXqb7pc=; b=glycpH4Qa6n1JZz0u6NJ8BbEGS BrENBaCSXSRnFcqYmid9IdPPK9xUrAFYfkFbBFZBbZVz1DTZr8IbUGdp3D7vkcCgfRU2L2p0pXbvH c8LJJtl6tQga73ct2BG4877bo0hmHqxNtB+o/LWLV8BX04P2H8UVMZZr274hyZnAgmFQCeVSdpaaL XuFPElNjsiZ1qqajEveS3lkFvlU8zDj0XK1TtTNHp3iUuS0Nb3snoBAxPKXNAJITsK2DzLR/p2+WN gdQljTY0msCc8ShtwidKRvo/QgMwmuKijVYJCQP02nYeWMwvf6AEijy1Gio8YA/yfpmMdtNgq7+wv 70NUHsEQ==; Received: from willy by casper.infradead.org with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1kHo6B-00030k-02; Mon, 14 Sep 2020 13:00:47 +0000 From: "Matthew Wilcox (Oracle)" To: linux-mm@kvack.org Cc: "Matthew Wilcox (Oracle)" , Andrew Morton , Hugh Dickins , William Kucharski , Johannes Weiner , Jan Kara , Yang Shi , Dave Chinner , linux-kernel@vger.kernel.org Subject: [PATCH v2 11/12] mm/truncate,shmem: Handle truncates that split THPs Date: Mon, 14 Sep 2020 14:00:41 +0100 Message-Id: <20200914130042.11442-12-willy@infradead.org> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20200914130042.11442-1-willy@infradead.org> References: <20200914130042.11442-1-willy@infradead.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4190E18013206 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam03 Content-Transfer-Encoding: quoted-printable 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: Handle THP splitting in the parts of the truncation functions which already handle partial pages. Factor all that code out into a new function called truncate_inode_partial_page(). We lose the easy 'bail out' path if a truncate or hole punch is entirely within a single page. We can add some more complex logic to restore the optimisation if it proves to be worthwhile. Signed-off-by: Matthew Wilcox (Oracle) --- mm/internal.h | 1 + mm/shmem.c | 98 +++++++++++++++-------------------------- mm/truncate.c | 119 +++++++++++++++++++++++++++++++------------------- 3 files changed, 110 insertions(+), 108 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 1c5f4425ed74..d17d9060902f 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -620,4 +620,5 @@ struct migration_target_control { gfp_t gfp_mask; }; =20 +bool truncate_inode_partial_page(struct page *page, loff_t start, loff_t= end); #endif /* __MM_INTERNAL_H */ diff --git a/mm/shmem.c b/mm/shmem.c index b2b94025841a..23bb79ec070f 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -855,32 +855,6 @@ void shmem_unlock_mapping(struct address_space *mapp= ing) } } =20 -/* - * Check whether a hole-punch or truncation needs to split a huge page, - * returning true if no split was required, or the split has been succes= sful. - * - * Eviction (or truncation to 0 size) should never need to split a huge = page; - * but in rare cases might do so, if shmem_undo_range() failed to tryloc= k on - * head, and then succeeded to trylock on tail. - * - * A split can only succeed when there are no additional references on t= he - * huge page: so the split below relies upon find_get_entries() having s= topped - * when it found a subpage of the huge page, without getting further ref= erences. - */ -static bool shmem_punch_compound(struct page *page, pgoff_t start, pgoff= _t end) -{ - if (!PageTransCompound(page)) - return true; - - /* Just proceed to delete a huge page wholly within the range punched *= / - if (PageHead(page) && - page->index >=3D start && page->index + HPAGE_PMD_NR <=3D end) - return true; - - /* Try to split huge page, so we can truly punch the hole or truncate *= / - return split_huge_page(page) >=3D 0; -} - /* * Remove range of pages and swap entries from page cache, and free them= . * If !unfalloc, truncate or punch hole; if unfalloc, undo failed falloc= ate. @@ -892,10 +866,9 @@ static void shmem_undo_range(struct inode *inode, lo= ff_t lstart, loff_t lend, struct shmem_inode_info *info =3D SHMEM_I(inode); pgoff_t start =3D (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; pgoff_t end =3D (lend + 1) >> PAGE_SHIFT; - unsigned int partial_start =3D lstart & (PAGE_SIZE - 1); - unsigned int partial_end =3D (lend + 1) & (PAGE_SIZE - 1); struct pagevec pvec; pgoff_t indices[PAGEVEC_SIZE]; + struct page *page; long nr_swaps_freed =3D 0; pgoff_t index; int i; @@ -908,7 +881,7 @@ static void shmem_undo_range(struct inode *inode, lof= f_t lstart, loff_t lend, while (index < end && find_lock_entries(mapping, index, end - 1, &pvec, indices)) { for (i =3D 0; i < pagevec_count(&pvec); i++) { - struct page *page =3D pvec.pages[i]; + page =3D pvec.pages[i]; =20 index =3D indices[i]; =20 @@ -931,33 +904,39 @@ static void shmem_undo_range(struct inode *inode, l= off_t lstart, loff_t lend, index++; } =20 - if (partial_start) { - struct page *page =3D NULL; - shmem_getpage(inode, start - 1, &page, SGP_READ); - if (page) { - unsigned int top =3D PAGE_SIZE; - if (start > end) { - top =3D partial_end; - partial_end =3D 0; - } - zero_user_segment(page, partial_start, top); - set_page_dirty(page); - unlock_page(page); - put_page(page); + index =3D -1; + if (end !=3D -1 && ((lend + 1) % PAGE_SIZE)) + index =3D lend >> PAGE_SHIFT; + page =3D NULL; + shmem_getpage(inode, lstart >> PAGE_SHIFT, &page, SGP_READ); + if (page) { + bool same_page; + + page =3D thp_head(page); + same_page =3D lend + 1 < page_offset(page) + thp_size(page); + if (same_page) + index =3D -1; + set_page_dirty(page); + if (!truncate_inode_partial_page(page, lstart, lend)) { + start =3D page->index + thp_nr_pages(page); + if (same_page) + end =3D page->index; } + unlock_page(page); + put_page(page); + page =3D NULL; } - if (partial_end) { - struct page *page =3D NULL; + + if (index !=3D -1) shmem_getpage(inode, end, &page, SGP_READ); - if (page) { - zero_user_segment(page, 0, partial_end); - set_page_dirty(page); - unlock_page(page); - put_page(page); - } + if (page) { + page =3D thp_head(page); + set_page_dirty(page); + if (!truncate_inode_partial_page(page, lstart, lend)) + end =3D page->index; + unlock_page(page); + put_page(page); } - if (start >=3D end) - return; =20 index =3D start; while (index < end) { @@ -973,7 +952,7 @@ static void shmem_undo_range(struct inode *inode, lof= f_t lstart, loff_t lend, continue; } for (i =3D 0; i < pagevec_count(&pvec); i++) { - struct page *page =3D pvec.pages[i]; + page =3D pvec.pages[i]; =20 index =3D indices[i]; if (xa_is_value(page)) { @@ -998,18 +977,9 @@ static void shmem_undo_range(struct inode *inode, lo= ff_t lstart, loff_t lend, break; } VM_BUG_ON_PAGE(PageWriteback(page), page); - if (shmem_punch_compound(page, start, end)) - truncate_inode_page(mapping, page); - else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { - /* Wipe the page and don't get stuck */ - clear_highpage(page); - flush_dcache_page(page); - set_page_dirty(page); - if (index < - round_up(start, HPAGE_PMD_NR)) - start =3D index + 1; - } + truncate_inode_page(mapping, page); } + index =3D page->index + thp_nr_pages(page) - 1; unlock_page(page); } pagevec_remove_exceptionals(&pvec); diff --git a/mm/truncate.c b/mm/truncate.c index d62aeffbffcc..06ed2f93069d 100644 --- a/mm/truncate.c +++ b/mm/truncate.c @@ -224,6 +224,53 @@ int truncate_inode_page(struct address_space *mappin= g, struct page *page) return 0; } =20 +/* + * Handle partial (transparent) pages. The page may be entirely within = the + * range if a split has raced with us. If not, we zero the part of the + * page that's within the (start, end] range, and then split the page if + * it's a THP. split_page_range() will discard pages which now lie beyo= nd + * i_size, and we rely on the caller to discard pages which lie within a + * newly created hole. + * + * Returns false if THP splitting failed so the caller can can avoid + * discarding the entire page which is stubbornly unsplit. + */ +bool truncate_inode_partial_page(struct page *page, loff_t start, loff_t= end) +{ + loff_t pos =3D page_offset(page); + unsigned int offset, length; + + if (pos < start) + offset =3D start - pos; + else + offset =3D 0; + length =3D thp_size(page); + if (pos + length <=3D (u64)end) + length =3D length - offset; + else + length =3D end + 1 - pos - offset; + + wait_on_page_writeback(page); + if (length =3D=3D thp_size(page)) { + truncate_inode_page(page->mapping, page); + return true; + } + + /* + * We may be zeroing pages we're about to discard, but it avoids + * doing a complex calculation here, and then doing the zeroing + * anyway if the page split fails. + */ + zero_user(page, offset, length); + + cleancache_invalidate_page(page->mapping, page); + if (page_has_private(page)) + do_invalidatepage(page, offset, length); + if (!PageTransHuge(page)) + return true; + return split_huge_page(page) =3D=3D 0; +} + /* * Used to get rid of pages on hardware memory corruption. */ @@ -288,20 +335,15 @@ void truncate_inode_pages_range(struct address_spac= e *mapping, { pgoff_t start; /* inclusive */ pgoff_t end; /* exclusive */ - unsigned int partial_start; /* inclusive */ - unsigned int partial_end; /* exclusive */ struct pagevec pvec; pgoff_t indices[PAGEVEC_SIZE]; pgoff_t index; int i; + struct page * page; =20 if (mapping->nrpages =3D=3D 0 && mapping->nrexceptional =3D=3D 0) goto out; =20 - /* Offsets within partial pages */ - partial_start =3D lstart & (PAGE_SIZE - 1); - partial_end =3D (lend + 1) & (PAGE_SIZE - 1); - /* * 'start' and 'end' always covers the range of pages to be fully * truncated. Partial pages are covered with 'partial_start' at the @@ -334,48 +376,37 @@ void truncate_inode_pages_range(struct address_spac= e *mapping, cond_resched(); } =20 - if (partial_start) { - struct page *page =3D find_lock_page(mapping, start - 1); - if (page) { - unsigned int top =3D PAGE_SIZE; - if (start > end) { - /* Truncation within a single page */ - top =3D partial_end; - partial_end =3D 0; - } - wait_on_page_writeback(page); - zero_user_segment(page, partial_start, top); - cleancache_invalidate_page(mapping, page); - if (page_has_private(page)) - do_invalidatepage(page, partial_start, - top - partial_start); - unlock_page(page); - put_page(page); + index =3D -1; + if (end !=3D -1 && ((lend + 1) % PAGE_SIZE)) + index =3D lend >> PAGE_SHIFT; + page =3D find_lock_head(mapping, lstart >> PAGE_SHIFT); + if (page) { + bool same_page =3D lend + 1 < page_offset(page) + thp_size(page); + if (same_page) + index =3D -1; + if (!truncate_inode_partial_page(page, lstart, lend)) { + start =3D page->index + thp_nr_pages(page); + if (same_page) + end =3D page->index; } + unlock_page(page); + put_page(page); + page =3D NULL; } - if (partial_end) { - struct page *page =3D find_lock_page(mapping, end); - if (page) { - wait_on_page_writeback(page); - zero_user_segment(page, 0, partial_end); - cleancache_invalidate_page(mapping, page); - if (page_has_private(page)) - do_invalidatepage(page, 0, - partial_end); - unlock_page(page); - put_page(page); - } + + if (index !=3D -1) + page =3D find_lock_head(mapping, index); + if (page) { + if (!truncate_inode_partial_page(page, lstart, lend)) + end =3D page->index; + unlock_page(page); + put_page(page); } - /* - * If the truncation happened within a single page no pages - * will be released, just zeroed, so we can bail out now. - */ - if (start >=3D end) - goto out; =20 index =3D start; - for ( ; ; ) { + while (index < end) { cond_resched(); + if (!find_get_entries(mapping, index, end - 1, &pvec, indices)) { /* If all gone from start onwards, we're done */ @@ -387,7 +418,7 @@ void truncate_inode_pages_range(struct address_space = *mapping, } =20 for (i =3D 0; i < pagevec_count(&pvec); i++) { - struct page *page =3D pvec.pages[i]; + page =3D pvec.pages[i]; =20 /* We rely upon deletion not changing page->index */ index =3D indices[i]; @@ -396,7 +427,7 @@ void truncate_inode_pages_range(struct address_space = *mapping, continue; =20 lock_page(page); - WARN_ON(page_to_index(page) !=3D index); + index =3D page->index + thp_nr_pages(page) - 1; wait_on_page_writeback(page); truncate_inode_page(mapping, page); unlock_page(page); --=20 2.28.0