blob: cf0dcf89eb69bc4e00d7868972444282838f0509 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * mm/readahead.c - address_space-level file readahead.
4 *
5 * Copyright (C) 2002, Linus Torvalds
6 *
Francois Camie1f8e872008-10-15 22:01:59 -07007 * 09Apr2002 Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Initial version.
9 */
10
11#include <linux/kernel.h>
Ross Zwisler11bd9692016-08-25 15:17:17 -070012#include <linux/dax.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/gfp.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/backing-dev.h>
Andrew Morton8bde37f2006-12-10 02:19:40 -080016#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/pagevec.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020018#include <linux/pagemap.h>
Cong Wang782182e2012-05-29 15:06:43 -070019#include <linux/syscalls.h>
20#include <linux/file.h>
Geliang Tangd72ee912016-01-14 15:22:01 -080021#include <linux/mm_inline.h>
Josef Bacikca47e8c2018-07-03 11:15:03 -040022#include <linux/blk-cgroup.h>
Amir Goldstein3d8f7612018-08-29 08:41:29 +030023#include <linux/fadvise.h>
Matthew Wilcox (Oracle)f2c817b2020-06-01 21:46:58 -070024#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Fabian Frederick29f175d2014-04-07 15:37:55 -070026#include "internal.h"
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/*
29 * Initialise a struct file's readahead state. Assumes that the caller has
30 * memset *ra to zero.
31 */
32void
33file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping)
34{
Christoph Hellwigde1414a2015-01-14 10:42:36 +010035 ra->ra_pages = inode_to_bdi(mapping->host)->ra_pages;
Fengguang Wuf4e6b492007-10-16 01:24:33 -070036 ra->prev_pos = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
Steven Whitehoused41cc702006-01-30 08:53:33 +000038EXPORT_SYMBOL_GPL(file_ra_state_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
David Howells03fb3d22009-04-03 16:42:35 +010040/*
41 * see if a page needs releasing upon read_cache_pages() failure
David Howells266cf652009-04-03 16:42:36 +010042 * - the caller of read_cache_pages() may have set PG_private or PG_fscache
43 * before calling, such as the NFS fs marking pages that are cached locally
44 * on disk, thus we need to give the fs a chance to clean up in the event of
45 * an error
David Howells03fb3d22009-04-03 16:42:35 +010046 */
47static void read_cache_pages_invalidate_page(struct address_space *mapping,
48 struct page *page)
49{
David Howells266cf652009-04-03 16:42:36 +010050 if (page_has_private(page)) {
David Howells03fb3d22009-04-03 16:42:35 +010051 if (!trylock_page(page))
52 BUG();
53 page->mapping = mapping;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030054 do_invalidatepage(page, 0, PAGE_SIZE);
David Howells03fb3d22009-04-03 16:42:35 +010055 page->mapping = NULL;
56 unlock_page(page);
57 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030058 put_page(page);
David Howells03fb3d22009-04-03 16:42:35 +010059}
60
61/*
62 * release a list of pages, invalidating them first if need be
63 */
64static void read_cache_pages_invalidate_pages(struct address_space *mapping,
65 struct list_head *pages)
66{
67 struct page *victim;
68
69 while (!list_empty(pages)) {
Geliang Tangc8ad6302016-01-14 15:20:51 -080070 victim = lru_to_page(pages);
David Howells03fb3d22009-04-03 16:42:35 +010071 list_del(&victim->lru);
72 read_cache_pages_invalidate_page(mapping, victim);
73 }
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/**
Randy Dunlapbd40cdd2006-06-25 05:48:08 -070077 * read_cache_pages - populate an address space with some pages & start reads against them
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * @mapping: the address_space
79 * @pages: The address of a list_head which contains the target pages. These
80 * pages have their ->index populated and are otherwise uninitialised.
81 * @filler: callback routine for filling a single page.
82 * @data: private data for the callback routine.
83 *
84 * Hides the details of the LRU cache etc from the filesystems.
Mike Rapoporta862f682019-03-05 15:48:42 -080085 *
86 * Returns: %0 on success, error return by @filler otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
88int read_cache_pages(struct address_space *mapping, struct list_head *pages,
89 int (*filler)(void *, struct page *), void *data)
90{
91 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 int ret = 0;
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 while (!list_empty(pages)) {
Geliang Tangc8ad6302016-01-14 15:20:51 -080095 page = lru_to_page(pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 list_del(&page->lru);
Michal Hocko063d99b2015-10-15 15:28:24 -070097 if (add_to_page_cache_lru(page, mapping, page->index,
Michal Hocko8a5c7432016-07-26 15:24:53 -070098 readahead_gfp_mask(mapping))) {
David Howells03fb3d22009-04-03 16:42:35 +010099 read_cache_pages_invalidate_page(mapping, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 continue;
101 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300102 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -0700103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 ret = filler(data, page);
Nick Piggineb2be182007-10-16 01:24:57 -0700105 if (unlikely(ret)) {
David Howells03fb3d22009-04-03 16:42:35 +0100106 read_cache_pages_invalidate_pages(mapping, pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 break;
108 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300109 task_io_account_read(PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return ret;
112}
113
114EXPORT_SYMBOL(read_cache_pages);
115
Matthew Wilcox (Oracle)a4d96532020-06-01 21:46:25 -0700116static void read_pages(struct readahead_control *rac, struct list_head *pages,
Matthew Wilcox (Oracle)c1f69252020-06-01 21:46:40 -0700117 bool skip_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Matthew Wilcox (Oracle)a4d96532020-06-01 21:46:25 -0700119 const struct address_space_operations *aops = rac->mapping->a_ops;
Matthew Wilcox (Oracle)c1f69252020-06-01 21:46:40 -0700120 struct page *page;
Jens Axboe5b417b12010-04-19 10:04:38 +0200121 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Matthew Wilcox (Oracle)a4d96532020-06-01 21:46:25 -0700123 if (!readahead_count(rac))
Matthew Wilcox (Oracle)c1f69252020-06-01 21:46:40 -0700124 goto out;
Matthew Wilcox (Oracle)ad4ae1c2020-06-01 21:46:18 -0700125
Jens Axboe5b417b12010-04-19 10:04:38 +0200126 blk_start_plug(&plug);
127
Matthew Wilcox (Oracle)8151b4c2020-06-01 21:46:44 -0700128 if (aops->readahead) {
129 aops->readahead(rac);
130 /* Clean up the remaining pages */
131 while ((page = readahead_page(rac))) {
132 unlock_page(page);
133 put_page(page);
134 }
135 } else if (aops->readpages) {
Matthew Wilcox (Oracle)a4d96532020-06-01 21:46:25 -0700136 aops->readpages(rac->file, rac->mapping, pages,
137 readahead_count(rac));
OGAWA Hirofumi029e3322006-11-02 22:07:06 -0800138 /* Clean up the remaining pages */
139 put_pages_list(pages);
Matthew Wilcox (Oracle)c1f69252020-06-01 21:46:40 -0700140 rac->_index += rac->_nr_pages;
141 rac->_nr_pages = 0;
142 } else {
143 while ((page = readahead_page(rac))) {
Matthew Wilcox (Oracle)a4d96532020-06-01 21:46:25 -0700144 aops->readpage(rac->file, page);
Matthew Wilcox (Oracle)c1f69252020-06-01 21:46:40 -0700145 put_page(page);
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
Jens Axboe5b417b12010-04-19 10:04:38 +0200148
Jens Axboe5b417b12010-04-19 10:04:38 +0200149 blk_finish_plug(&plug);
Matthew Wilcox (Oracle)ad4ae1c2020-06-01 21:46:18 -0700150
151 BUG_ON(!list_empty(pages));
Matthew Wilcox (Oracle)c1f69252020-06-01 21:46:40 -0700152 BUG_ON(readahead_count(rac));
153
154out:
155 if (skip_page)
156 rac->_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Matthew Wilcox (Oracle)2c684232020-06-01 21:46:51 -0700159/**
Matthew Wilcox (Oracle)73bb49d2020-10-15 20:06:14 -0700160 * page_cache_ra_unbounded - Start unchecked readahead.
161 * @ractl: Readahead control.
Matthew Wilcox (Oracle)2c684232020-06-01 21:46:51 -0700162 * @nr_to_read: The number of pages to read.
163 * @lookahead_size: Where to start the next readahead.
164 *
165 * This function is for filesystems to call when they want to start
166 * readahead beyond a file's stated i_size. This is almost certainly
167 * not the function you want to call. Use page_cache_async_readahead()
168 * or page_cache_sync_readahead() instead.
169 *
170 * Context: File is referenced by caller. Mutexes may be held by caller.
171 * May sleep, but will not reenter filesystem to reclaim memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
Matthew Wilcox (Oracle)73bb49d2020-10-15 20:06:14 -0700173void page_cache_ra_unbounded(struct readahead_control *ractl,
174 unsigned long nr_to_read, unsigned long lookahead_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Matthew Wilcox (Oracle)73bb49d2020-10-15 20:06:14 -0700176 struct address_space *mapping = ractl->mapping;
177 unsigned long index = readahead_index(ractl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 LIST_HEAD(page_pool);
Michal Hocko8a5c7432016-07-26 15:24:53 -0700179 gfp_t gfp_mask = readahead_gfp_mask(mapping);
Matthew Wilcox (Oracle)c2c7ad72020-06-01 21:46:32 -0700180 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 /*
Matthew Wilcox (Oracle)f2c817b2020-06-01 21:46:58 -0700183 * Partway through the readahead operation, we will have added
184 * locked pages to the page cache, but will not yet have submitted
185 * them for I/O. Adding another page may need to allocate memory,
186 * which can trigger memory reclaim. Telling the VM we're in
187 * the middle of a filesystem operation will cause it to not
188 * touch file-backed pages, preventing a deadlock. Most (all?)
189 * filesystems already specify __GFP_NOFS in their mapping's
190 * gfp_mask, but let's be explicit here.
191 */
192 unsigned int nofs = memalloc_nofs_save();
193
Jan Kara730633f2021-01-28 19:19:45 +0100194 filemap_invalidate_lock_shared(mapping);
Matthew Wilcox (Oracle)f2c817b2020-06-01 21:46:58 -0700195 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * Preallocate as many pages as we will need.
197 */
Matthew Wilcox (Oracle)c2c7ad72020-06-01 21:46:32 -0700198 for (i = 0; i < nr_to_read; i++) {
Matthew Wilcox (Oracle)0387df12021-03-10 16:06:51 -0500199 struct folio *folio = xa_load(&mapping->i_pages, index + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Matthew Wilcox (Oracle)0387df12021-03-10 16:06:51 -0500201 if (folio && !xa_is_value(folio)) {
Christoph Hellwigb3751e62018-06-01 09:03:06 -0700202 /*
Matthew Wilcox (Oracle)2d8163e2020-06-01 21:46:54 -0700203 * Page already present? Kick off the current batch
204 * of contiguous pages before continuing with the
205 * next batch. This page may be the one we would
206 * have intended to mark as Readahead, but we don't
207 * have a stable reference to this page, and it's
208 * not worth getting one just for that.
Christoph Hellwigb3751e62018-06-01 09:03:06 -0700209 */
Matthew Wilcox (Oracle)73bb49d2020-10-15 20:06:14 -0700210 read_pages(ractl, &page_pool, true);
Matthew Wilcox (Oracle)f615bd52021-04-21 18:09:23 +0100211 i = ractl->_index + ractl->_nr_pages - index - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 continue;
Christoph Hellwigb3751e62018-06-01 09:03:06 -0700213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Matthew Wilcox (Oracle)0387df12021-03-10 16:06:51 -0500215 folio = filemap_alloc_folio(gfp_mask, 0);
216 if (!folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 break;
Matthew Wilcox (Oracle)c1f69252020-06-01 21:46:40 -0700218 if (mapping->a_ops->readpages) {
Matthew Wilcox (Oracle)0387df12021-03-10 16:06:51 -0500219 folio->index = index + i;
220 list_add(&folio->lru, &page_pool);
221 } else if (filemap_add_folio(mapping, folio, index + i,
Matthew Wilcox (Oracle)c1f69252020-06-01 21:46:40 -0700222 gfp_mask) < 0) {
Matthew Wilcox (Oracle)0387df12021-03-10 16:06:51 -0500223 folio_put(folio);
Matthew Wilcox (Oracle)73bb49d2020-10-15 20:06:14 -0700224 read_pages(ractl, &page_pool, true);
Matthew Wilcox (Oracle)f615bd52021-04-21 18:09:23 +0100225 i = ractl->_index + ractl->_nr_pages - index - 1;
Matthew Wilcox (Oracle)c1f69252020-06-01 21:46:40 -0700226 continue;
227 }
Matthew Wilcox (Oracle)c2c7ad72020-06-01 21:46:32 -0700228 if (i == nr_to_read - lookahead_size)
Matthew Wilcox (Oracle)0387df12021-03-10 16:06:51 -0500229 folio_set_readahead(folio);
Matthew Wilcox (Oracle)73bb49d2020-10-15 20:06:14 -0700230 ractl->_nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 /*
234 * Now start the IO. We ignore I/O errors - if the page is not
235 * uptodate then the caller will launch readpage again, and
236 * will then handle the error.
237 */
Matthew Wilcox (Oracle)73bb49d2020-10-15 20:06:14 -0700238 read_pages(ractl, &page_pool, false);
Jan Kara730633f2021-01-28 19:19:45 +0100239 filemap_invalidate_unlock_shared(mapping);
Matthew Wilcox (Oracle)f2c817b2020-06-01 21:46:58 -0700240 memalloc_nofs_restore(nofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
Matthew Wilcox (Oracle)73bb49d2020-10-15 20:06:14 -0700242EXPORT_SYMBOL_GPL(page_cache_ra_unbounded);
Matthew Wilcox (Oracle)2c684232020-06-01 21:46:51 -0700243
244/*
Matthew Wilcox (Oracle)82382872020-10-15 20:06:17 -0700245 * do_page_cache_ra() actually reads a chunk of disk. It allocates
Matthew Wilcox (Oracle)2c684232020-06-01 21:46:51 -0700246 * the pages first, then submits them for I/O. This avoids the very bad
247 * behaviour which would occur if page allocations are causing VM writeback.
248 * We really don't want to intermingle reads and writes like that.
249 */
Matthew Wilcox (Oracle)82382872020-10-15 20:06:17 -0700250void do_page_cache_ra(struct readahead_control *ractl,
251 unsigned long nr_to_read, unsigned long lookahead_size)
Matthew Wilcox (Oracle)2c684232020-06-01 21:46:51 -0700252{
Matthew Wilcox (Oracle)82382872020-10-15 20:06:17 -0700253 struct inode *inode = ractl->mapping->host;
254 unsigned long index = readahead_index(ractl);
Matthew Wilcox (Oracle)2c684232020-06-01 21:46:51 -0700255 loff_t isize = i_size_read(inode);
256 pgoff_t end_index; /* The last page we want to read */
257
258 if (isize == 0)
259 return;
260
261 end_index = (isize - 1) >> PAGE_SHIFT;
262 if (index > end_index)
263 return;
264 /* Don't read past the page containing the last byte of the file */
265 if (nr_to_read > end_index - index)
266 nr_to_read = end_index - index + 1;
267
Matthew Wilcox (Oracle)82382872020-10-15 20:06:17 -0700268 page_cache_ra_unbounded(ractl, nr_to_read, lookahead_size);
Matthew Wilcox (Oracle)2c684232020-06-01 21:46:51 -0700269}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271/*
272 * Chunk the readahead into 2 megabyte units, so that we don't pin too much
273 * memory at once.
274 */
David Howells7b3df3b2020-10-15 20:06:24 -0700275void force_page_cache_ra(struct readahead_control *ractl,
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +0100276 unsigned long nr_to_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
David Howells7b3df3b2020-10-15 20:06:24 -0700278 struct address_space *mapping = ractl->mapping;
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +0100279 struct file_ra_state *ra = ractl->ra;
Jens Axboe9491ae42016-12-12 16:43:26 -0800280 struct backing_dev_info *bdi = inode_to_bdi(mapping->host);
David Howells7b3df3b2020-10-15 20:06:24 -0700281 unsigned long max_pages, index;
Jens Axboe9491ae42016-12-12 16:43:26 -0800282
Matthew Wilcox (Oracle)8151b4c2020-06-01 21:46:44 -0700283 if (unlikely(!mapping->a_ops->readpage && !mapping->a_ops->readpages &&
284 !mapping->a_ops->readahead))
Matthew Wilcox (Oracle)9a428232020-06-01 21:46:10 -0700285 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Jens Axboe9491ae42016-12-12 16:43:26 -0800287 /*
288 * If the request exceeds the readahead window, allow the read to
289 * be up to the optimal hardware IO size
290 */
David Howells7b3df3b2020-10-15 20:06:24 -0700291 index = readahead_index(ractl);
Jens Axboe9491ae42016-12-12 16:43:26 -0800292 max_pages = max_t(unsigned long, bdi->io_pages, ra->ra_pages);
David Howells7b3df3b2020-10-15 20:06:24 -0700293 nr_to_read = min_t(unsigned long, nr_to_read, max_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 while (nr_to_read) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300295 unsigned long this_chunk = (2 * 1024 * 1024) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 if (this_chunk > nr_to_read)
298 this_chunk = nr_to_read;
David Howells7b3df3b2020-10-15 20:06:24 -0700299 ractl->_index = index;
300 do_page_cache_ra(ractl, this_chunk, 0);
Mark Rutland58d56402014-01-29 14:05:51 -0800301
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700302 index += this_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 nr_to_read -= this_chunk;
304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
Fengguang Wu5ce11102007-07-19 01:47:59 -0700307/*
Fengguang Wuc743d962007-07-19 01:48:04 -0700308 * Set the initial window size, round to next power of 2 and square
309 * for small size, x 4 for medium, and x 2 for large
310 * for 128k (32 page) max ra
Lin Fengfb25a772021-11-05 13:43:47 -0700311 * 1-2 page = 16k, 3-4 page 32k, 5-8 page = 64k, > 8 page = 128k initial
Fengguang Wuc743d962007-07-19 01:48:04 -0700312 */
313static unsigned long get_init_ra_size(unsigned long size, unsigned long max)
314{
315 unsigned long newsize = roundup_pow_of_two(size);
316
317 if (newsize <= max / 32)
318 newsize = newsize * 4;
319 else if (newsize <= max / 4)
320 newsize = newsize * 2;
321 else
322 newsize = max;
323
324 return newsize;
325}
326
327/*
Fengguang Wu122a21d2007-07-19 01:48:01 -0700328 * Get the previous window size, ramp it up, and
329 * return it as the new window size.
330 */
Fengguang Wuc743d962007-07-19 01:48:04 -0700331static unsigned long get_next_ra_size(struct file_ra_state *ra,
Gao Xiang20ff1c92018-12-28 00:33:34 -0800332 unsigned long max)
Fengguang Wu122a21d2007-07-19 01:48:01 -0700333{
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700334 unsigned long cur = ra->size;
Fengguang Wu122a21d2007-07-19 01:48:01 -0700335
336 if (cur < max / 16)
Gao Xiang20ff1c92018-12-28 00:33:34 -0800337 return 4 * cur;
338 if (cur <= max / 2)
339 return 2 * cur;
340 return max;
Fengguang Wu122a21d2007-07-19 01:48:01 -0700341}
342
343/*
344 * On-demand readahead design.
345 *
346 * The fields in struct file_ra_state represent the most-recently-executed
347 * readahead attempt:
348 *
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700349 * |<----- async_size ---------|
350 * |------------------- size -------------------->|
351 * |==================#===========================|
352 * ^start ^page marked with PG_readahead
Fengguang Wu122a21d2007-07-19 01:48:01 -0700353 *
354 * To overlap application thinking time and disk I/O time, we do
355 * `readahead pipelining': Do not wait until the application consumed all
356 * readahead pages and stalled on the missing page at readahead_index;
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700357 * Instead, submit an asynchronous readahead I/O as soon as there are
358 * only async_size pages left in the readahead window. Normally async_size
359 * will be equal to size, for maximum pipelining.
Fengguang Wu122a21d2007-07-19 01:48:01 -0700360 *
361 * In interleaved sequential reads, concurrent streams on the same fd can
362 * be invalidating each other's readahead state. So we flag the new readahead
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700363 * page at (start+size-async_size) with PG_readahead, and use it as readahead
Fengguang Wu122a21d2007-07-19 01:48:01 -0700364 * indicator. The flag won't be set on already cached pages, to avoid the
365 * readahead-for-nothing fuss, saving pointless page cache lookups.
366 *
Fengguang Wuf4e6b492007-10-16 01:24:33 -0700367 * prev_pos tracks the last visited byte in the _previous_ read request.
Fengguang Wu122a21d2007-07-19 01:48:01 -0700368 * It should be maintained by the caller, and will be used for detecting
369 * small random reads. Note that the readahead algorithm checks loosely
370 * for sequential patterns. Hence interleaved reads might be served as
371 * sequential ones.
372 *
373 * There is a special-case: if the first page which the application tries to
374 * read happens to be the first page of the file, it is assumed that a linear
375 * read is about to happen and the window is immediately set to the initial size
376 * based on I/O request size and the max_readahead.
377 *
378 * The code ramps up the readahead size aggressively at first, but slow down as
379 * it approaches max_readhead.
380 */
381
382/*
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700383 * Count contiguously cached pages from @index-1 to @index-@max,
Wu Fengguang10be0b32009-06-16 15:31:36 -0700384 * this count is a conservative estimation of
385 * - length of the sequential read sequence, or
386 * - thrashing threshold in memory tight systems
387 */
388static pgoff_t count_history_pages(struct address_space *mapping,
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700389 pgoff_t index, unsigned long max)
Wu Fengguang10be0b32009-06-16 15:31:36 -0700390{
391 pgoff_t head;
392
393 rcu_read_lock();
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700394 head = page_cache_prev_miss(mapping, index - 1, max);
Wu Fengguang10be0b32009-06-16 15:31:36 -0700395 rcu_read_unlock();
396
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700397 return index - 1 - head;
Wu Fengguang10be0b32009-06-16 15:31:36 -0700398}
399
400/*
401 * page cache context based read-ahead
402 */
403static int try_context_readahead(struct address_space *mapping,
404 struct file_ra_state *ra,
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700405 pgoff_t index,
Wu Fengguang10be0b32009-06-16 15:31:36 -0700406 unsigned long req_size,
407 unsigned long max)
408{
409 pgoff_t size;
410
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700411 size = count_history_pages(mapping, index, max);
Wu Fengguang10be0b32009-06-16 15:31:36 -0700412
413 /*
Fengguang Wu2cad4012013-09-11 14:21:47 -0700414 * not enough history pages:
Wu Fengguang10be0b32009-06-16 15:31:36 -0700415 * it could be a random read
416 */
Fengguang Wu2cad4012013-09-11 14:21:47 -0700417 if (size <= req_size)
Wu Fengguang10be0b32009-06-16 15:31:36 -0700418 return 0;
419
420 /*
421 * starts from beginning of file:
422 * it is a strong indication of long-run stream (or whole-file-read)
423 */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700424 if (size >= index)
Wu Fengguang10be0b32009-06-16 15:31:36 -0700425 size *= 2;
426
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700427 ra->start = index;
Fengguang Wu2cad4012013-09-11 14:21:47 -0700428 ra->size = min(size + req_size, max);
429 ra->async_size = 1;
Wu Fengguang10be0b32009-06-16 15:31:36 -0700430
431 return 1;
432}
433
434/*
Fengguang Wu122a21d2007-07-19 01:48:01 -0700435 * A minimal readahead algorithm for trivial sequential/random reads.
436 */
David Howells6e4af692020-10-15 20:06:21 -0700437static void ondemand_readahead(struct readahead_control *ractl,
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +0100438 bool hit_readahead_marker, unsigned long req_size)
Fengguang Wu122a21d2007-07-19 01:48:01 -0700439{
David Howells6e4af692020-10-15 20:06:21 -0700440 struct backing_dev_info *bdi = inode_to_bdi(ractl->mapping->host);
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +0100441 struct file_ra_state *ra = ractl->ra;
Jens Axboe9491ae42016-12-12 16:43:26 -0800442 unsigned long max_pages = ra->ra_pages;
Markus Stockhausendc30b962018-07-27 09:09:53 -0600443 unsigned long add_pages;
David Howells6e4af692020-10-15 20:06:21 -0700444 unsigned long index = readahead_index(ractl);
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700445 pgoff_t prev_index;
Wu Fengguang045a2522009-06-16 15:31:33 -0700446
447 /*
Jens Axboe9491ae42016-12-12 16:43:26 -0800448 * If the request exceeds the readahead window, allow the read to
449 * be up to the optimal hardware IO size
450 */
451 if (req_size > max_pages && bdi->io_pages > max_pages)
452 max_pages = min(req_size, bdi->io_pages);
453
454 /*
Wu Fengguang045a2522009-06-16 15:31:33 -0700455 * start of file
456 */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700457 if (!index)
Wu Fengguang045a2522009-06-16 15:31:33 -0700458 goto initial_readahead;
Fengguang Wu122a21d2007-07-19 01:48:01 -0700459
460 /*
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700461 * It's the expected callback index, assume sequential access.
Fengguang Wu122a21d2007-07-19 01:48:01 -0700462 * Ramp up sizes, and push forward the readahead window.
463 */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700464 if ((index == (ra->start + ra->size - ra->async_size) ||
465 index == (ra->start + ra->size))) {
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700466 ra->start += ra->size;
Jens Axboe9491ae42016-12-12 16:43:26 -0800467 ra->size = get_next_ra_size(ra, max_pages);
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700468 ra->async_size = ra->size;
469 goto readit;
Fengguang Wu122a21d2007-07-19 01:48:01 -0700470 }
471
Fengguang Wu122a21d2007-07-19 01:48:01 -0700472 /*
Fengguang Wu6b10c6c2007-10-16 01:24:34 -0700473 * Hit a marked page without valid readahead state.
474 * E.g. interleaved reads.
475 * Query the pagecache for async_size, which normally equals to
476 * readahead size. Ramp it up and use it as the new readahead size.
477 */
478 if (hit_readahead_marker) {
479 pgoff_t start;
480
Nick Piggin30002ed2008-07-25 19:45:28 -0700481 rcu_read_lock();
David Howells6e4af692020-10-15 20:06:21 -0700482 start = page_cache_next_miss(ractl->mapping, index + 1,
483 max_pages);
Nick Piggin30002ed2008-07-25 19:45:28 -0700484 rcu_read_unlock();
Fengguang Wu6b10c6c2007-10-16 01:24:34 -0700485
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700486 if (!start || start - index > max_pages)
Matthew Wilcox (Oracle)9a428232020-06-01 21:46:10 -0700487 return;
Fengguang Wu6b10c6c2007-10-16 01:24:34 -0700488
489 ra->start = start;
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700490 ra->size = start - index; /* old async_size */
Wu Fengguang160334a2009-06-16 15:31:23 -0700491 ra->size += req_size;
Jens Axboe9491ae42016-12-12 16:43:26 -0800492 ra->size = get_next_ra_size(ra, max_pages);
Fengguang Wu6b10c6c2007-10-16 01:24:34 -0700493 ra->async_size = ra->size;
494 goto readit;
495 }
496
497 /*
Wu Fengguang045a2522009-06-16 15:31:33 -0700498 * oversize read
Fengguang Wu122a21d2007-07-19 01:48:01 -0700499 */
Jens Axboe9491ae42016-12-12 16:43:26 -0800500 if (req_size > max_pages)
Wu Fengguang045a2522009-06-16 15:31:33 -0700501 goto initial_readahead;
502
503 /*
504 * sequential cache miss
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700505 * trivial case: (index - prev_index) == 1
506 * unaligned reads: (index - prev_index) == 0
Wu Fengguang045a2522009-06-16 15:31:33 -0700507 */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700508 prev_index = (unsigned long long)ra->prev_pos >> PAGE_SHIFT;
509 if (index - prev_index <= 1UL)
Wu Fengguang045a2522009-06-16 15:31:33 -0700510 goto initial_readahead;
511
512 /*
Wu Fengguang10be0b32009-06-16 15:31:36 -0700513 * Query the page cache and look for the traces(cached history pages)
514 * that a sequential stream would leave behind.
515 */
David Howells6e4af692020-10-15 20:06:21 -0700516 if (try_context_readahead(ractl->mapping, ra, index, req_size,
517 max_pages))
Wu Fengguang10be0b32009-06-16 15:31:36 -0700518 goto readit;
519
520 /*
Wu Fengguang045a2522009-06-16 15:31:33 -0700521 * standalone, small random read
522 * Read as is, and do not pollute the readahead state.
523 */
David Howells6e4af692020-10-15 20:06:21 -0700524 do_page_cache_ra(ractl, req_size, 0);
Matthew Wilcox (Oracle)9a428232020-06-01 21:46:10 -0700525 return;
Wu Fengguang045a2522009-06-16 15:31:33 -0700526
527initial_readahead:
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700528 ra->start = index;
Jens Axboe9491ae42016-12-12 16:43:26 -0800529 ra->size = get_init_ra_size(req_size, max_pages);
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700530 ra->async_size = ra->size > req_size ? ra->size - req_size : ra->size;
Fengguang Wu122a21d2007-07-19 01:48:01 -0700531
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700532readit:
Wu Fengguang51daa882009-06-16 15:31:24 -0700533 /*
534 * Will this read hit the readahead marker made by itself?
535 * If so, trigger the readahead marker hit now, and merge
536 * the resulted next readahead window into the current one.
Markus Stockhausendc30b962018-07-27 09:09:53 -0600537 * Take care of maximum IO pages as above.
Wu Fengguang51daa882009-06-16 15:31:24 -0700538 */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700539 if (index == ra->start && ra->size == ra->async_size) {
Markus Stockhausendc30b962018-07-27 09:09:53 -0600540 add_pages = get_next_ra_size(ra, max_pages);
541 if (ra->size + add_pages <= max_pages) {
542 ra->async_size = add_pages;
543 ra->size += add_pages;
544 } else {
545 ra->size = max_pages;
546 ra->async_size = max_pages >> 1;
547 }
Wu Fengguang51daa882009-06-16 15:31:24 -0700548 }
549
David Howells6e4af692020-10-15 20:06:21 -0700550 ractl->_index = ra->start;
551 do_page_cache_ra(ractl, ra->size, ra->async_size);
Fengguang Wu122a21d2007-07-19 01:48:01 -0700552}
553
Matthew Wilcox (Oracle)fefa7c42020-10-15 20:06:28 -0700554void page_cache_sync_ra(struct readahead_control *ractl,
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +0100555 unsigned long req_count)
Fengguang Wu122a21d2007-07-19 01:48:01 -0700556{
Jens Axboe324bcf52020-10-17 09:25:52 -0600557 bool do_forced_ra = ractl->file && (ractl->file->f_mode & FMODE_RANDOM);
Fengguang Wu122a21d2007-07-19 01:48:01 -0700558
Jens Axboe324bcf52020-10-17 09:25:52 -0600559 /*
560 * Even if read-ahead is disabled, issue this request as read-ahead
561 * as we'll need it to satisfy the requested range. The forced
562 * read-ahead will do the right thing and limit the read to just the
563 * requested range, which we'll set to 1 page for this case.
564 */
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +0100565 if (!ractl->ra->ra_pages || blk_cgroup_congested()) {
Jens Axboe324bcf52020-10-17 09:25:52 -0600566 if (!ractl->file)
567 return;
568 req_count = 1;
569 do_forced_ra = true;
570 }
Josef Bacikca47e8c2018-07-03 11:15:03 -0400571
Wu Fengguang01414502010-03-05 13:42:03 -0800572 /* be dumb */
Jens Axboe324bcf52020-10-17 09:25:52 -0600573 if (do_forced_ra) {
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +0100574 force_page_cache_ra(ractl, req_count);
Wu Fengguang01414502010-03-05 13:42:03 -0800575 return;
576 }
577
Fengguang Wu122a21d2007-07-19 01:48:01 -0700578 /* do read-ahead */
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +0100579 ondemand_readahead(ractl, false, req_count);
Fengguang Wu122a21d2007-07-19 01:48:01 -0700580}
Matthew Wilcox (Oracle)fefa7c42020-10-15 20:06:28 -0700581EXPORT_SYMBOL_GPL(page_cache_sync_ra);
Rusty Russellcf914a72007-07-19 01:48:08 -0700582
Matthew Wilcox (Oracle)fefa7c42020-10-15 20:06:28 -0700583void page_cache_async_ra(struct readahead_control *ractl,
Matthew Wilcox (Oracle)7836d992021-05-27 12:30:54 -0400584 struct folio *folio, unsigned long req_count)
Rusty Russellcf914a72007-07-19 01:48:08 -0700585{
586 /* no read-ahead */
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +0100587 if (!ractl->ra->ra_pages)
Rusty Russellcf914a72007-07-19 01:48:08 -0700588 return;
589
590 /*
591 * Same bit is used for PG_readahead and PG_reclaim.
592 */
Matthew Wilcox (Oracle)7836d992021-05-27 12:30:54 -0400593 if (folio_test_writeback(folio))
Rusty Russellcf914a72007-07-19 01:48:08 -0700594 return;
595
Matthew Wilcox (Oracle)7836d992021-05-27 12:30:54 -0400596 folio_clear_readahead(folio);
Rusty Russellcf914a72007-07-19 01:48:08 -0700597
598 /*
599 * Defer asynchronous read-ahead on IO congestion.
600 */
Matthew Wilcox (Oracle)fefa7c42020-10-15 20:06:28 -0700601 if (inode_read_congested(ractl->mapping->host))
Rusty Russellcf914a72007-07-19 01:48:08 -0700602 return;
603
Josef Bacikca47e8c2018-07-03 11:15:03 -0400604 if (blk_cgroup_congested())
605 return;
606
Rusty Russellcf914a72007-07-19 01:48:08 -0700607 /* do read-ahead */
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +0100608 ondemand_readahead(ractl, true, req_count);
Rusty Russellcf914a72007-07-19 01:48:08 -0700609}
Matthew Wilcox (Oracle)fefa7c42020-10-15 20:06:28 -0700610EXPORT_SYMBOL_GPL(page_cache_async_ra);
Cong Wang782182e2012-05-29 15:06:43 -0700611
Dominik Brodowskic7b95d52018-03-19 17:51:36 +0100612ssize_t ksys_readahead(int fd, loff_t offset, size_t count)
Cong Wang782182e2012-05-29 15:06:43 -0700613{
614 ssize_t ret;
Al Viro2903ff02012-08-28 12:52:22 -0400615 struct fd f;
Cong Wang782182e2012-05-29 15:06:43 -0700616
617 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400618 f = fdget(fd);
Amir Goldstein3d8f7612018-08-29 08:41:29 +0300619 if (!f.file || !(f.file->f_mode & FMODE_READ))
620 goto out;
621
622 /*
623 * The readahead() syscall is intended to run only on files
624 * that can execute readahead. If readahead is not possible
625 * on this file, then we must return -EINVAL.
626 */
627 ret = -EINVAL;
628 if (!f.file->f_mapping || !f.file->f_mapping->a_ops ||
629 !S_ISREG(file_inode(f.file)->i_mode))
630 goto out;
631
632 ret = vfs_fadvise(f.file, offset, count, POSIX_FADV_WILLNEED);
633out:
634 fdput(f);
Cong Wang782182e2012-05-29 15:06:43 -0700635 return ret;
636}
Dominik Brodowskic7b95d52018-03-19 17:51:36 +0100637
638SYSCALL_DEFINE3(readahead, int, fd, loff_t, offset, size_t, count)
639{
640 return ksys_readahead(fd, offset, count);
641}
David Howells3ca23642020-09-10 14:03:27 +0100642
643/**
644 * readahead_expand - Expand a readahead request
645 * @ractl: The request to be expanded
646 * @new_start: The revised start
647 * @new_len: The revised size of the request
648 *
649 * Attempt to expand a readahead request outwards from the current size to the
650 * specified size by inserting locked pages before and after the current window
651 * to increase the size to the new window. This may involve the insertion of
652 * THPs, in which case the window may get expanded even beyond what was
653 * requested.
654 *
655 * The algorithm will stop if it encounters a conflicting page already in the
656 * pagecache and leave a smaller expansion than requested.
657 *
658 * The caller must check for this by examining the revised @ractl object for a
659 * different expansion than was requested.
660 */
661void readahead_expand(struct readahead_control *ractl,
662 loff_t new_start, size_t new_len)
663{
664 struct address_space *mapping = ractl->mapping;
665 struct file_ra_state *ra = ractl->ra;
666 pgoff_t new_index, new_nr_pages;
667 gfp_t gfp_mask = readahead_gfp_mask(mapping);
668
669 new_index = new_start / PAGE_SIZE;
670
671 /* Expand the leading edge downwards */
672 while (ractl->_index > new_index) {
673 unsigned long index = ractl->_index - 1;
674 struct page *page = xa_load(&mapping->i_pages, index);
675
676 if (page && !xa_is_value(page))
677 return; /* Page apparently present */
678
679 page = __page_cache_alloc(gfp_mask);
680 if (!page)
681 return;
682 if (add_to_page_cache_lru(page, mapping, index, gfp_mask) < 0) {
683 put_page(page);
684 return;
685 }
686
687 ractl->_nr_pages++;
688 ractl->_index = page->index;
689 }
690
691 new_len += new_start - readahead_pos(ractl);
692 new_nr_pages = DIV_ROUND_UP(new_len, PAGE_SIZE);
693
694 /* Expand the trailing edge upwards */
695 while (ractl->_nr_pages < new_nr_pages) {
696 unsigned long index = ractl->_index + ractl->_nr_pages;
697 struct page *page = xa_load(&mapping->i_pages, index);
698
699 if (page && !xa_is_value(page))
700 return; /* Page apparently present */
701
702 page = __page_cache_alloc(gfp_mask);
703 if (!page)
704 return;
705 if (add_to_page_cache_lru(page, mapping, index, gfp_mask) < 0) {
706 put_page(page);
707 return;
708 }
709 ractl->_nr_pages++;
710 if (ra) {
711 ra->size++;
712 ra->async_size++;
713 }
714 }
715}
716EXPORT_SYMBOL(readahead_expand);