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 919BAC433EF for ; Fri, 4 Feb 2022 20:00:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C7748D000F; Fri, 4 Feb 2022 14:59:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 72F848D0018; Fri, 4 Feb 2022 14:59:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 57E896B0099; Fri, 4 Feb 2022 14:59:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0103.hostedemail.com [216.40.44.103]) by kanga.kvack.org (Postfix) with ESMTP id F2B2D8D000D for ; Fri, 4 Feb 2022 14:59:09 -0500 (EST) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id B2C3D99915 for ; Fri, 4 Feb 2022 19:59:09 +0000 (UTC) X-FDA: 79106161218.14.67AF7E3 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf24.hostedemail.com (Postfix) with ESMTP id 5A0B7180004 for ; Fri, 4 Feb 2022 19:59:09 +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=TcquNu2GjfQJyfxq8hgdegm77gZgKaMIuUwOTeONZRk=; b=F9BhQjkRzUZmcxq8EgkR8kYIzr 2AdAWttomEYGMxGUvWiRnTh/sVF00uHtKMlELqdsyPXW3DsUX4TW2vk/JJRtcZoZQnyFp497XAqO/ MID290vHvfvVVi+6F/UhMeAyILcChN1f0zDCvJPf/Km5k6QympUV07QuioYfvLma4vgkVL4ozij4+ lU/n8p+76fqr6Pin08Ad0D8ci49LT6rq6tN4fSziSk7q8CF+cV1Y4qAjAd4y+Xy4QptTl+35+4RVM taKiyjQ6447lAMYFmYl6UR6UgX5XL03ZPqNY2ggl6s2SmG+AtSQE1gyhounEiLS3KBlU7297VApo3 4NrU1Kgg==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1nG4jb-007LrT-U7; Fri, 04 Feb 2022 19:59:08 +0000 From: "Matthew Wilcox (Oracle)" To: linux-mm@kvack.org Cc: "Matthew Wilcox (Oracle)" , linux-kernel@vger.kernel.org Subject: [PATCH 71/75] mm/readahead: Add large folio readahead Date: Fri, 4 Feb 2022 19:58:48 +0000 Message-Id: <20220204195852.1751729-72-willy@infradead.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220204195852.1751729-1-willy@infradead.org> References: <20220204195852.1751729-1-willy@infradead.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 5A0B7180004 X-Rspam-User: nil Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=F9BhQjkR; dmarc=none; spf=none (imf24.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org X-Stat-Signature: nmmwsmyyi4jskbqpwi99sdynzuaxref5 X-Rspamd-Server: rspam08 X-HE-Tag: 1644004749-441933 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: Allocate large folios in the readahead code when the filesystem supports them and it seems worth doing. The heuristic for choosing which folio sizes will surely need some tuning, but this aggressive ramp-up has been good for testing. Signed-off-by: Matthew Wilcox (Oracle) --- mm/readahead.c | 106 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 99 insertions(+), 7 deletions(-) diff --git a/mm/readahead.c b/mm/readahead.c index cf0dcf89eb69..5100eaf5b0ee 100644 --- a/mm/readahead.c +++ b/mm/readahead.c @@ -148,7 +148,7 @@ static void read_pages(struct readahead_control *rac,= struct list_head *pages, =20 blk_finish_plug(&plug); =20 - BUG_ON(!list_empty(pages)); + BUG_ON(pages && !list_empty(pages)); BUG_ON(readahead_count(rac)); =20 out: @@ -431,11 +431,103 @@ static int try_context_readahead(struct address_sp= ace *mapping, return 1; } =20 +/* + * There are some parts of the kernel which assume that PMD entries + * are exactly HPAGE_PMD_ORDER. Those should be fixed, but until then, + * limit the maximum allocation order to PMD size. I'm not aware of any + * assumptions about maximum order if THP are disabled, but 8 seems like + * a good order (that's 1MB if you're using 4kB pages) + */ +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +#define MAX_PAGECACHE_ORDER HPAGE_PMD_ORDER +#else +#define MAX_PAGECACHE_ORDER 8 +#endif + +static inline int ra_alloc_folio(struct readahead_control *ractl, pgoff_= t index, + pgoff_t mark, unsigned int order, gfp_t gfp) +{ + int err; + struct folio *folio =3D filemap_alloc_folio(gfp, order); + + if (!folio) + return -ENOMEM; + if (mark - index < (1UL << order)) + folio_set_readahead(folio); + err =3D filemap_add_folio(ractl->mapping, folio, index, gfp); + if (err) + folio_put(folio); + else + ractl->_nr_pages +=3D 1UL << order; + return err; +} + +static void page_cache_ra_order(struct readahead_control *ractl, + struct file_ra_state *ra, unsigned int new_order) +{ + struct address_space *mapping =3D ractl->mapping; + pgoff_t index =3D readahead_index(ractl); + pgoff_t limit =3D (i_size_read(mapping->host) - 1) >> PAGE_SHIFT; + pgoff_t mark =3D index + ra->size - ra->async_size; + int err =3D 0; + gfp_t gfp =3D readahead_gfp_mask(mapping); + + if (!mapping_large_folio_support(mapping) || ra->size < 4) + goto fallback; + + limit =3D min(limit, index + ra->size - 1); + + if (new_order < MAX_PAGECACHE_ORDER) { + new_order +=3D 2; + if (new_order > MAX_PAGECACHE_ORDER) + new_order =3D MAX_PAGECACHE_ORDER; + while ((1 << new_order) > ra->size) + new_order--; + } + + while (index <=3D limit) { + unsigned int order =3D new_order; + + /* Align with smaller pages if needed */ + if (index & ((1UL << order) - 1)) { + order =3D __ffs(index); + if (order =3D=3D 1) + order =3D 0; + } + /* Don't allocate pages past EOF */ + while (index + (1UL << order) - 1 > limit) { + if (--order =3D=3D 1) + order =3D 0; + } + err =3D ra_alloc_folio(ractl, index, mark, order, gfp); + if (err) + break; + index +=3D 1UL << order; + } + + if (index > limit) { + ra->size +=3D index - limit - 1; + ra->async_size +=3D index - limit - 1; + } + + read_pages(ractl, NULL, false); + + /* + * If there were already pages in the page cache, then we may have + * left some gaps. Let the regular readahead code take care of this + * situation. + */ + if (!err) + return; +fallback: + do_page_cache_ra(ractl, ra->size, ra->async_size); +} + /* * A minimal readahead algorithm for trivial sequential/random reads. */ static void ondemand_readahead(struct readahead_control *ractl, - bool hit_readahead_marker, unsigned long req_size) + struct folio *folio, unsigned long req_size) { struct backing_dev_info *bdi =3D inode_to_bdi(ractl->mapping->host); struct file_ra_state *ra =3D ractl->ra; @@ -470,12 +562,12 @@ static void ondemand_readahead(struct readahead_con= trol *ractl, } =20 /* - * Hit a marked page without valid readahead state. + * Hit a marked folio without valid readahead state. * E.g. interleaved reads. * Query the pagecache for async_size, which normally equals to * readahead size. Ramp it up and use it as the new readahead size. */ - if (hit_readahead_marker) { + if (folio) { pgoff_t start; =20 rcu_read_lock(); @@ -548,7 +640,7 @@ static void ondemand_readahead(struct readahead_contr= ol *ractl, } =20 ractl->_index =3D ra->start; - do_page_cache_ra(ractl, ra->size, ra->async_size); + page_cache_ra_order(ractl, ra, folio ? folio_order(folio) : 0); } =20 void page_cache_sync_ra(struct readahead_control *ractl, @@ -576,7 +668,7 @@ void page_cache_sync_ra(struct readahead_control *rac= tl, } =20 /* do read-ahead */ - ondemand_readahead(ractl, false, req_count); + ondemand_readahead(ractl, NULL, req_count); } EXPORT_SYMBOL_GPL(page_cache_sync_ra); =20 @@ -605,7 +697,7 @@ void page_cache_async_ra(struct readahead_control *ra= ctl, return; =20 /* do read-ahead */ - ondemand_readahead(ractl, true, req_count); + ondemand_readahead(ractl, folio, req_count); } EXPORT_SYMBOL_GPL(page_cache_async_ra); =20 --=20 2.34.1