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.5 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,URIBL_BLOCKED,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 E895CC2D0A3 for ; Mon, 2 Nov 2020 18:43:23 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4F5F422268 for ; Mon, 2 Nov 2020 18:43:23 +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="feTu5T5f" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4F5F422268 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 F3D2A6B005C; Mon, 2 Nov 2020 13:43:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EEBFB6B0068; Mon, 2 Nov 2020 13:43:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DDB7A6B006C; Mon, 2 Nov 2020 13:43:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0085.hostedemail.com [216.40.44.85]) by kanga.kvack.org (Postfix) with ESMTP id 983B66B0068 for ; Mon, 2 Nov 2020 13:43:21 -0500 (EST) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 3B7B1181AC9C6 for ; Mon, 2 Nov 2020 18:43:21 +0000 (UTC) X-FDA: 77440351002.08.army13_1611a40272b2 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin08.hostedemail.com (Postfix) with ESMTP id 147CA1819E76C for ; Mon, 2 Nov 2020 18:43:21 +0000 (UTC) X-HE-Tag: army13_1611a40272b2 X-Filterd-Recvd-Size: 7331 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf09.hostedemail.com (Postfix) with ESMTP for ; Mon, 2 Nov 2020 18:43:20 +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=5UKc/Mp3kM/zU/rfSseS3aHyFa9ZSPW/UoKsd9ADFZo=; b=feTu5T5feJ6zWA/qLVIF/Oky/w Vw73MyXMDSRqiFT0dZ8OsEhC6QPAguPa6ENwq9UwGL6ktPpxs97kxG+TXQQvrtRK/tt2bZ0A+U4dD QtklTvGihf5gEzdukR6R4zA7AAz8jAna/VizB74sImnXFbYD8Il92xNX+O7ETFy7L2pLDTd7MTaoF bPITaSSP++gdoSio8+4hir/DP8hql42Eg2wrVEiaYHobVP9r8Cs0yjUdVYixIap95y4eHiplBCS8d BDlrNX6nh7b6EfTnWPWeQSGXOSXLWI0+pHjKfkbyKh6BR3oxdpP/LXLXk30HoEcVd2YOuTeQsIxLq iKutK6HA==; Received: from willy by casper.infradead.org with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1kZenU-0006ls-2G; Mon, 02 Nov 2020 18:43:17 +0000 From: "Matthew Wilcox (Oracle)" To: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Cc: "Matthew Wilcox (Oracle)" , hch@lst.de, kent.overstreet@gmail.com Subject: [PATCH 02/17] mm/filemap: Use THPs in generic_file_buffered_read Date: Mon, 2 Nov 2020 18:42:57 +0000 Message-Id: <20201102184312.25926-3-willy@infradead.org> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20201102184312.25926-1-willy@infradead.org> References: <20201102184312.25926-1-willy@infradead.org> MIME-Version: 1.0 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: Add mapping_get_read_thps() which returns the THPs which represent a contiguous array of bytes in the file. It also stops when encountering a page marked as Readahead or !Uptodate (but does return that page) so it can be handled appropriately by filemap_get_pages(). That lets us remove the loop in filemap_get_pages() and check only the last page. Signed-off-by: Matthew Wilcox (Oracle) --- mm/filemap.c | 96 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 70 insertions(+), 26 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 23e3781b3aef..d9636ccf87ff 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2176,6 +2176,48 @@ static int lock_page_for_iocb(struct kiocb *iocb, = struct page *page) return lock_page_killable(page); } =20 +static unsigned mapping_get_read_thps(struct address_space *mapping, + pgoff_t index, unsigned int nr_pages, struct page **pages) +{ + XA_STATE(xas, &mapping->i_pages, index); + struct page *head; + unsigned int ret =3D 0; + + if (unlikely(!nr_pages)) + return 0; + + rcu_read_lock(); + for (head =3D xas_load(&xas); head; head =3D xas_next(&xas)) { + if (xas_retry(&xas, head)) + continue; + if (xa_is_value(head)) + break; + if (!page_cache_get_speculative(head)) + goto retry; + + /* Has the page moved or been split? */ + if (unlikely(head !=3D xas_reload(&xas))) + goto put_page; + + pages[ret++] =3D head; + if (ret =3D=3D nr_pages) + break; + if (!PageUptodate(head)) + break; + if (PageReadahead(head)) + break; + xas.xa_index =3D head->index + thp_nr_pages(head) - 1; + xas.xa_offset =3D (xas.xa_index >> xas.xa_shift) & XA_CHUNK_MASK; + continue; +put_page: + put_page(head); +retry: + xas_reset(&xas); + } + rcu_read_unlock(); + return ret; +} + static struct page *filemap_read_page(struct kiocb *iocb, struct file *f= ilp, struct address_space *mapping, struct page *page) { @@ -2330,14 +2372,14 @@ static int filemap_get_pages(struct kiocb *iocb, = struct iov_iter *iter, struct file_ra_state *ra =3D &filp->f_ra; pgoff_t index =3D iocb->ki_pos >> PAGE_SHIFT; pgoff_t last_index =3D (iocb->ki_pos + iter->count + PAGE_SIZE-1) >> PA= GE_SHIFT; - int i, j, nr_got, err =3D 0; + int nr_got, err =3D 0; =20 nr =3D min_t(unsigned long, last_index - index, nr); find_page: if (fatal_signal_pending(current)) return -EINTR; =20 - nr_got =3D find_get_pages_contig(mapping, index, nr, pages); + nr_got =3D mapping_get_read_thps(mapping, index, nr, pages); if (nr_got) goto got_pages; =20 @@ -2346,7 +2388,7 @@ static int filemap_get_pages(struct kiocb *iocb, st= ruct iov_iter *iter, =20 page_cache_sync_readahead(mapping, ra, filp, index, last_index - index)= ; =20 - nr_got =3D find_get_pages_contig(mapping, index, nr, pages); + nr_got =3D mapping_get_read_thps(mapping, index, nr, pages); if (nr_got) goto got_pages; =20 @@ -2355,20 +2397,19 @@ static int filemap_get_pages(struct kiocb *iocb, = struct iov_iter *iter, if (!IS_ERR_OR_NULL(pages[0])) nr_got =3D 1; got_pages: - for (i =3D 0; i < nr_got; i++) { - struct page *page =3D pages[i]; - pgoff_t pg_index =3D index + i; + if (nr_got > 0) { + struct page *page =3D pages[nr_got - 1]; + pgoff_t pg_index =3D page->index; loff_t pg_pos =3D max(iocb->ki_pos, (loff_t) pg_index << PAGE_SHIFT); loff_t pg_count =3D iocb->ki_pos + iter->count - pg_pos; =20 if (PageReadahead(page)) { if (iocb->ki_flags & IOCB_NOIO) { - for (j =3D i; j < nr_got; j++) - put_page(pages[j]); - nr_got =3D i; + put_page(page); + nr_got--; err =3D -EAGAIN; - break; + goto err; } page_cache_async_readahead(mapping, ra, filp, page, pg_index, last_index - pg_index); @@ -2376,26 +2417,23 @@ static int filemap_get_pages(struct kiocb *iocb, = struct iov_iter *iter, =20 if (!PageUptodate(page)) { if ((iocb->ki_flags & IOCB_NOWAIT) || - ((iocb->ki_flags & IOCB_WAITQ) && i)) { - for (j =3D i; j < nr_got; j++) - put_page(pages[j]); - nr_got =3D i; + ((iocb->ki_flags & IOCB_WAITQ) && nr_got > 1)) { + put_page(page); + nr_got--; err =3D -EAGAIN; - break; + goto err; } =20 page =3D filemap_update_page(iocb, filp, iter, page, pg_pos, pg_count); if (IS_ERR_OR_NULL(page)) { - for (j =3D i + 1; j < nr_got; j++) - put_page(pages[j]); - nr_got =3D i; + nr_got--; err =3D PTR_ERR_OR_ZERO(page); - break; } } } =20 +err: if (likely(nr_got)) return nr_got; if (err) @@ -2502,20 +2540,26 @@ ssize_t generic_file_buffered_read(struct kiocb *= iocb, mark_page_accessed(pages[i]); =20 for (i =3D 0; i < pg_nr; i++) { - unsigned int offset =3D iocb->ki_pos & ~PAGE_MASK; - unsigned int bytes =3D min_t(loff_t, end_offset - iocb->ki_pos, - PAGE_SIZE - offset); - unsigned int copied; + struct page *page =3D pages[i]; + size_t page_size =3D thp_size(page); + size_t offset =3D iocb->ki_pos & (page_size - 1); + size_t bytes =3D min_t(loff_t, end_offset - iocb->ki_pos, + page_size - offset); + size_t copied; =20 /* * If users can be writing to this page using arbitrary * virtual addresses, take care about potential aliasing * before reading the page on the kernel side. */ - if (writably_mapped) - flush_dcache_page(pages[i]); + if (writably_mapped) { + int j; + + for (j =3D 0; j < thp_nr_pages(page); j++) + flush_dcache_page(page + j); + } =20 - copied =3D copy_page_to_iter(pages[i], offset, bytes, iter); + copied =3D copy_page_to_iter(page, offset, bytes, iter); =20 written +=3D copied; iocb->ki_pos +=3D copied; --=20 2.28.0