blob: 47afbca1d122e44617bad4afec283ec7b780ec49 [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
NeilBrown84dacdb2022-03-22 14:38:51 -070011/**
12 * DOC: Readahead Overview
13 *
14 * Readahead is used to read content into the page cache before it is
15 * explicitly requested by the application. Readahead only ever
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -040016 * attempts to read folios that are not yet in the page cache. If a
17 * folio is present but not up-to-date, readahead will not try to read
Matthew Wilcox (Oracle)5efe7442022-04-29 08:43:23 -040018 * it. In that case a simple ->read_folio() will be requested.
NeilBrown84dacdb2022-03-22 14:38:51 -070019 *
20 * Readahead is triggered when an application read request (whether a
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -040021 * system call or a page fault) finds that the requested folio is not in
NeilBrown84dacdb2022-03-22 14:38:51 -070022 * the page cache, or that it is in the page cache and has the
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -040023 * readahead flag set. This flag indicates that the folio was read
24 * as part of a previous readahead request and now that it has been
25 * accessed, it is time for the next readahead.
NeilBrown84dacdb2022-03-22 14:38:51 -070026 *
27 * Each readahead request is partly synchronous read, and partly async
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -040028 * readahead. This is reflected in the struct file_ra_state which
29 * contains ->size being the total number of pages, and ->async_size
30 * which is the number of pages in the async section. The readahead
31 * flag will be set on the first folio in this async section to trigger
32 * a subsequent readahead. Once a series of sequential reads has been
NeilBrown84dacdb2022-03-22 14:38:51 -070033 * established, there should be no need for a synchronous component and
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -040034 * all readahead request will be fully asynchronous.
NeilBrown84dacdb2022-03-22 14:38:51 -070035 *
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -040036 * When either of the triggers causes a readahead, three numbers need
37 * to be determined: the start of the region to read, the size of the
38 * region, and the size of the async tail.
NeilBrown84dacdb2022-03-22 14:38:51 -070039 *
40 * The start of the region is simply the first page address at or after
41 * the accessed address, which is not currently populated in the page
42 * cache. This is found with a simple search in the page cache.
43 *
44 * The size of the async tail is determined by subtracting the size that
45 * was explicitly requested from the determined request size, unless
46 * this would be less than zero - then zero is used. NOTE THIS
47 * CALCULATION IS WRONG WHEN THE START OF THE REGION IS NOT THE ACCESSED
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -040048 * PAGE. ALSO THIS CALCULATION IS NOT USED CONSISTENTLY.
NeilBrown84dacdb2022-03-22 14:38:51 -070049 *
50 * The size of the region is normally determined from the size of the
51 * previous readahead which loaded the preceding pages. This may be
52 * discovered from the struct file_ra_state for simple sequential reads,
53 * or from examining the state of the page cache when multiple
54 * sequential reads are interleaved. Specifically: where the readahead
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -040055 * was triggered by the readahead flag, the size of the previous
NeilBrown84dacdb2022-03-22 14:38:51 -070056 * readahead is assumed to be the number of pages from the triggering
57 * page to the start of the new readahead. In these cases, the size of
58 * the previous readahead is scaled, often doubled, for the new
59 * readahead, though see get_next_ra_size() for details.
60 *
61 * If the size of the previous read cannot be determined, the number of
62 * preceding pages in the page cache is used to estimate the size of
63 * a previous read. This estimate could easily be misled by random
64 * reads being coincidentally adjacent, so it is ignored unless it is
65 * larger than the current request, and it is not scaled up, unless it
66 * is at the start of file.
67 *
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -040068 * In general readahead is accelerated at the start of the file, as
NeilBrown84dacdb2022-03-22 14:38:51 -070069 * reads from there are often sequential. There are other minor
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -040070 * adjustments to the readahead size in various special cases and these
NeilBrown84dacdb2022-03-22 14:38:51 -070071 * are best discovered by reading the code.
72 *
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -040073 * The above calculation, based on the previous readahead size,
74 * determines the size of the readahead, to which any requested read
75 * size may be added.
NeilBrown84dacdb2022-03-22 14:38:51 -070076 *
77 * Readahead requests are sent to the filesystem using the ->readahead()
78 * address space operation, for which mpage_readahead() is a canonical
79 * implementation. ->readahead() should normally initiate reads on all
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -040080 * folios, but may fail to read any or all folios without causing an I/O
Matthew Wilcox (Oracle)5efe7442022-04-29 08:43:23 -040081 * error. The page cache reading code will issue a ->read_folio() request
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -040082 * for any folio which ->readahead() did not read, and only an error
NeilBrown84dacdb2022-03-22 14:38:51 -070083 * from this will be final.
84 *
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -040085 * ->readahead() will generally call readahead_folio() repeatedly to get
86 * each folio from those prepared for readahead. It may fail to read a
87 * folio by:
NeilBrown84dacdb2022-03-22 14:38:51 -070088 *
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -040089 * * not calling readahead_folio() sufficiently many times, effectively
90 * ignoring some folios, as might be appropriate if the path to
NeilBrown84dacdb2022-03-22 14:38:51 -070091 * storage is congested.
92 *
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -040093 * * failing to actually submit a read request for a given folio,
NeilBrown84dacdb2022-03-22 14:38:51 -070094 * possibly due to insufficient resources, or
95 *
96 * * getting an error during subsequent processing of a request.
97 *
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -040098 * In the last two cases, the folio should be unlocked by the filesystem
99 * to indicate that the read attempt has failed. In the first case the
100 * folio will be unlocked by the VFS.
NeilBrown84dacdb2022-03-22 14:38:51 -0700101 *
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -0400102 * Those folios not in the final ``async_size`` of the request should be
NeilBrown84dacdb2022-03-22 14:38:51 -0700103 * considered to be important and ->readahead() should not fail them due
104 * to congestion or temporary resource unavailability, but should wait
105 * for necessary resources (e.g. memory or indexing information) to
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -0400106 * become available. Folios in the final ``async_size`` may be
NeilBrown84dacdb2022-03-22 14:38:51 -0700107 * considered less urgent and failure to read them is more acceptable.
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -0400108 * In this case it is best to use filemap_remove_folio() to remove the
109 * folios from the page cache as is automatically done for folios that
110 * were not fetched with readahead_folio(). This will allow a
111 * subsequent synchronous readahead request to try them again. If they
NeilBrown9fd472a2022-03-22 14:38:54 -0700112 * are left in the page cache, then they will be read individually using
Matthew Wilcox (Oracle)5efe7442022-04-29 08:43:23 -0400113 * ->read_folio() which may be less efficient.
NeilBrown84dacdb2022-03-22 14:38:51 -0700114 */
115
Christoph Hellwigc97ab272022-04-20 06:27:19 +0200116#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#include <linux/kernel.h>
Ross Zwisler11bd9692016-08-25 15:17:17 -0700118#include <linux/dax.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900119#include <linux/gfp.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -0400120#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#include <linux/backing-dev.h>
Andrew Morton8bde37f2006-12-10 02:19:40 -0800122#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#include <linux/pagevec.h>
Jens Axboef5ff8422007-09-21 09:19:54 +0200124#include <linux/pagemap.h>
Christoph Hellwig17604242022-09-15 10:41:56 +0100125#include <linux/psi.h>
Cong Wang782182e2012-05-29 15:06:43 -0700126#include <linux/syscalls.h>
127#include <linux/file.h>
Geliang Tangd72ee912016-01-14 15:22:01 -0800128#include <linux/mm_inline.h>
Josef Bacikca47e8c2018-07-03 11:15:03 -0400129#include <linux/blk-cgroup.h>
Amir Goldstein3d8f7612018-08-29 08:41:29 +0300130#include <linux/fadvise.h>
Matthew Wilcox (Oracle)f2c817b2020-06-01 21:46:58 -0700131#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Fabian Frederick29f175d2014-04-07 15:37:55 -0700133#include "internal.h"
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/*
136 * Initialise a struct file's readahead state. Assumes that the caller has
137 * memset *ra to zero.
138 */
139void
140file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping)
141{
Christoph Hellwigde1414a2015-01-14 10:42:36 +0100142 ra->ra_pages = inode_to_bdi(mapping->host)->ra_pages;
Fengguang Wuf4e6b492007-10-16 01:24:33 -0700143 ra->prev_pos = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
Steven Whitehoused41cc702006-01-30 08:53:33 +0000145EXPORT_SYMBOL_GPL(file_ra_state_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Christoph Hellwigb4e089d72022-03-31 05:35:55 -0700147static void read_pages(struct readahead_control *rac)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Matthew Wilcox (Oracle)a4d96532020-06-01 21:46:25 -0700149 const struct address_space_operations *aops = rac->mapping->a_ops;
Matthew Wilcox (Oracle)a42634a2022-03-31 14:15:59 -0400150 struct folio *folio;
Jens Axboe5b417b12010-04-19 10:04:38 +0200151 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Matthew Wilcox (Oracle)a4d96532020-06-01 21:46:25 -0700153 if (!readahead_count(rac))
Christoph Hellwigb4e089d72022-03-31 05:35:55 -0700154 return;
Matthew Wilcox (Oracle)ad4ae1c2020-06-01 21:46:18 -0700155
Christoph Hellwig17604242022-09-15 10:41:56 +0100156 if (unlikely(rac->_workingset))
157 psi_memstall_enter(&rac->_pflags);
Jens Axboe5b417b12010-04-19 10:04:38 +0200158 blk_start_plug(&plug);
159
Matthew Wilcox (Oracle)8151b4c2020-06-01 21:46:44 -0700160 if (aops->readahead) {
161 aops->readahead(rac);
NeilBrown9fd472a2022-03-22 14:38:54 -0700162 /*
Matthew Wilcox (Oracle)a42634a2022-03-31 14:15:59 -0400163 * Clean up the remaining folios. The sizes in ->ra
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -0400164 * may be used to size the next readahead, so make sure
NeilBrown9fd472a2022-03-22 14:38:54 -0700165 * they accurately reflect what happened.
166 */
Matthew Wilcox (Oracle)a42634a2022-03-31 14:15:59 -0400167 while ((folio = readahead_folio(rac)) != NULL) {
168 unsigned long nr = folio_nr_pages(folio);
169
Matthew Wilcox (Oracle)6bf74cd2022-06-07 15:45:53 -0400170 folio_get(folio);
Matthew Wilcox (Oracle)a42634a2022-03-31 14:15:59 -0400171 rac->ra->size -= nr;
172 if (rac->ra->async_size >= nr) {
173 rac->ra->async_size -= nr;
174 filemap_remove_folio(folio);
NeilBrown9fd472a2022-03-22 14:38:54 -0700175 }
Matthew Wilcox (Oracle)a42634a2022-03-31 14:15:59 -0400176 folio_unlock(folio);
Matthew Wilcox (Oracle)6bf74cd2022-06-07 15:45:53 -0400177 folio_put(folio);
Matthew Wilcox (Oracle)8151b4c2020-06-01 21:46:44 -0700178 }
Matthew Wilcox (Oracle)c1f69252020-06-01 21:46:40 -0700179 } else {
Matthew Wilcox (Oracle)5efe7442022-04-29 08:43:23 -0400180 while ((folio = readahead_folio(rac)) != NULL)
Matthew Wilcox (Oracle)7e0a1262022-04-29 11:53:28 -0400181 aops->read_folio(rac->file, folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
Jens Axboe5b417b12010-04-19 10:04:38 +0200183
Jens Axboe5b417b12010-04-19 10:04:38 +0200184 blk_finish_plug(&plug);
Christoph Hellwig17604242022-09-15 10:41:56 +0100185 if (unlikely(rac->_workingset))
186 psi_memstall_leave(&rac->_pflags);
187 rac->_workingset = false;
Matthew Wilcox (Oracle)ad4ae1c2020-06-01 21:46:18 -0700188
Matthew Wilcox (Oracle)c1f69252020-06-01 21:46:40 -0700189 BUG_ON(readahead_count(rac));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Matthew Wilcox (Oracle)2c684232020-06-01 21:46:51 -0700192/**
Matthew Wilcox (Oracle)73bb49d2020-10-15 20:06:14 -0700193 * page_cache_ra_unbounded - Start unchecked readahead.
194 * @ractl: Readahead control.
Matthew Wilcox (Oracle)2c684232020-06-01 21:46:51 -0700195 * @nr_to_read: The number of pages to read.
196 * @lookahead_size: Where to start the next readahead.
197 *
198 * This function is for filesystems to call when they want to start
199 * readahead beyond a file's stated i_size. This is almost certainly
200 * not the function you want to call. Use page_cache_async_readahead()
201 * or page_cache_sync_readahead() instead.
202 *
203 * Context: File is referenced by caller. Mutexes may be held by caller.
204 * May sleep, but will not reenter filesystem to reclaim memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
Matthew Wilcox (Oracle)73bb49d2020-10-15 20:06:14 -0700206void page_cache_ra_unbounded(struct readahead_control *ractl,
207 unsigned long nr_to_read, unsigned long lookahead_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Matthew Wilcox (Oracle)73bb49d2020-10-15 20:06:14 -0700209 struct address_space *mapping = ractl->mapping;
210 unsigned long index = readahead_index(ractl);
Michal Hocko8a5c7432016-07-26 15:24:53 -0700211 gfp_t gfp_mask = readahead_gfp_mask(mapping);
Matthew Wilcox (Oracle)c2c7ad72020-06-01 21:46:32 -0700212 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /*
Matthew Wilcox (Oracle)f2c817b2020-06-01 21:46:58 -0700215 * Partway through the readahead operation, we will have added
216 * locked pages to the page cache, but will not yet have submitted
217 * them for I/O. Adding another page may need to allocate memory,
218 * which can trigger memory reclaim. Telling the VM we're in
219 * the middle of a filesystem operation will cause it to not
220 * touch file-backed pages, preventing a deadlock. Most (all?)
221 * filesystems already specify __GFP_NOFS in their mapping's
222 * gfp_mask, but let's be explicit here.
223 */
224 unsigned int nofs = memalloc_nofs_save();
225
Jan Kara730633f2021-01-28 19:19:45 +0100226 filemap_invalidate_lock_shared(mapping);
Matthew Wilcox (Oracle)f2c817b2020-06-01 21:46:58 -0700227 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 * Preallocate as many pages as we will need.
229 */
Matthew Wilcox (Oracle)c2c7ad72020-06-01 21:46:32 -0700230 for (i = 0; i < nr_to_read; i++) {
Matthew Wilcox (Oracle)0387df12021-03-10 16:06:51 -0500231 struct folio *folio = xa_load(&mapping->i_pages, index + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Matthew Wilcox (Oracle)0387df12021-03-10 16:06:51 -0500233 if (folio && !xa_is_value(folio)) {
Christoph Hellwigb3751e62018-06-01 09:03:06 -0700234 /*
Matthew Wilcox (Oracle)2d8163e2020-06-01 21:46:54 -0700235 * Page already present? Kick off the current batch
236 * of contiguous pages before continuing with the
237 * next batch. This page may be the one we would
238 * have intended to mark as Readahead, but we don't
239 * have a stable reference to this page, and it's
240 * not worth getting one just for that.
Christoph Hellwigb3751e62018-06-01 09:03:06 -0700241 */
Christoph Hellwigb4e089d72022-03-31 05:35:55 -0700242 read_pages(ractl);
243 ractl->_index++;
Matthew Wilcox (Oracle)f615bd52021-04-21 18:09:23 +0100244 i = ractl->_index + ractl->_nr_pages - index - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 continue;
Christoph Hellwigb3751e62018-06-01 09:03:06 -0700246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Matthew Wilcox (Oracle)0387df12021-03-10 16:06:51 -0500248 folio = filemap_alloc_folio(gfp_mask, 0);
249 if (!folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 break;
Matthew Wilcox (Oracle)704528d2022-03-23 21:29:04 -0400251 if (filemap_add_folio(mapping, folio, index + i,
Matthew Wilcox (Oracle)c1f69252020-06-01 21:46:40 -0700252 gfp_mask) < 0) {
Matthew Wilcox (Oracle)0387df12021-03-10 16:06:51 -0500253 folio_put(folio);
Christoph Hellwigb4e089d72022-03-31 05:35:55 -0700254 read_pages(ractl);
255 ractl->_index++;
Matthew Wilcox (Oracle)f615bd52021-04-21 18:09:23 +0100256 i = ractl->_index + ractl->_nr_pages - index - 1;
Matthew Wilcox (Oracle)c1f69252020-06-01 21:46:40 -0700257 continue;
258 }
Matthew Wilcox (Oracle)c2c7ad72020-06-01 21:46:32 -0700259 if (i == nr_to_read - lookahead_size)
Matthew Wilcox (Oracle)0387df12021-03-10 16:06:51 -0500260 folio_set_readahead(folio);
Christoph Hellwig17604242022-09-15 10:41:56 +0100261 ractl->_workingset |= folio_test_workingset(folio);
Matthew Wilcox (Oracle)73bb49d2020-10-15 20:06:14 -0700262 ractl->_nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /*
Matthew Wilcox (Oracle)7e0a1262022-04-29 11:53:28 -0400266 * Now start the IO. We ignore I/O errors - if the folio is not
267 * uptodate then the caller will launch read_folio again, and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * will then handle the error.
269 */
Christoph Hellwigb4e089d72022-03-31 05:35:55 -0700270 read_pages(ractl);
Jan Kara730633f2021-01-28 19:19:45 +0100271 filemap_invalidate_unlock_shared(mapping);
Matthew Wilcox (Oracle)f2c817b2020-06-01 21:46:58 -0700272 memalloc_nofs_restore(nofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
Matthew Wilcox (Oracle)73bb49d2020-10-15 20:06:14 -0700274EXPORT_SYMBOL_GPL(page_cache_ra_unbounded);
Matthew Wilcox (Oracle)2c684232020-06-01 21:46:51 -0700275
276/*
Matthew Wilcox (Oracle)82382872020-10-15 20:06:17 -0700277 * do_page_cache_ra() actually reads a chunk of disk. It allocates
Matthew Wilcox (Oracle)2c684232020-06-01 21:46:51 -0700278 * the pages first, then submits them for I/O. This avoids the very bad
279 * behaviour which would occur if page allocations are causing VM writeback.
280 * We really don't want to intermingle reads and writes like that.
281 */
Matthew Wilcox (Oracle)56a4d672021-07-24 23:26:14 -0400282static void do_page_cache_ra(struct readahead_control *ractl,
Matthew Wilcox (Oracle)82382872020-10-15 20:06:17 -0700283 unsigned long nr_to_read, unsigned long lookahead_size)
Matthew Wilcox (Oracle)2c684232020-06-01 21:46:51 -0700284{
Matthew Wilcox (Oracle)82382872020-10-15 20:06:17 -0700285 struct inode *inode = ractl->mapping->host;
286 unsigned long index = readahead_index(ractl);
Matthew Wilcox (Oracle)2c684232020-06-01 21:46:51 -0700287 loff_t isize = i_size_read(inode);
288 pgoff_t end_index; /* The last page we want to read */
289
290 if (isize == 0)
291 return;
292
293 end_index = (isize - 1) >> PAGE_SHIFT;
294 if (index > end_index)
295 return;
296 /* Don't read past the page containing the last byte of the file */
297 if (nr_to_read > end_index - index)
298 nr_to_read = end_index - index + 1;
299
Matthew Wilcox (Oracle)82382872020-10-15 20:06:17 -0700300 page_cache_ra_unbounded(ractl, nr_to_read, lookahead_size);
Matthew Wilcox (Oracle)2c684232020-06-01 21:46:51 -0700301}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303/*
304 * Chunk the readahead into 2 megabyte units, so that we don't pin too much
305 * memory at once.
306 */
David Howells7b3df3b2020-10-15 20:06:24 -0700307void force_page_cache_ra(struct readahead_control *ractl,
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +0100308 unsigned long nr_to_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
David Howells7b3df3b2020-10-15 20:06:24 -0700310 struct address_space *mapping = ractl->mapping;
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +0100311 struct file_ra_state *ra = ractl->ra;
Jens Axboe9491ae42016-12-12 16:43:26 -0800312 struct backing_dev_info *bdi = inode_to_bdi(mapping->host);
David Howells7b3df3b2020-10-15 20:06:24 -0700313 unsigned long max_pages, index;
Jens Axboe9491ae42016-12-12 16:43:26 -0800314
Matthew Wilcox (Oracle)7e0a1262022-04-29 11:53:28 -0400315 if (unlikely(!mapping->a_ops->read_folio && !mapping->a_ops->readahead))
Matthew Wilcox (Oracle)9a428232020-06-01 21:46:10 -0700316 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Jens Axboe9491ae42016-12-12 16:43:26 -0800318 /*
319 * If the request exceeds the readahead window, allow the read to
320 * be up to the optimal hardware IO size
321 */
David Howells7b3df3b2020-10-15 20:06:24 -0700322 index = readahead_index(ractl);
Jens Axboe9491ae42016-12-12 16:43:26 -0800323 max_pages = max_t(unsigned long, bdi->io_pages, ra->ra_pages);
David Howells7b3df3b2020-10-15 20:06:24 -0700324 nr_to_read = min_t(unsigned long, nr_to_read, max_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 while (nr_to_read) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300326 unsigned long this_chunk = (2 * 1024 * 1024) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 if (this_chunk > nr_to_read)
329 this_chunk = nr_to_read;
David Howells7b3df3b2020-10-15 20:06:24 -0700330 ractl->_index = index;
331 do_page_cache_ra(ractl, this_chunk, 0);
Mark Rutland58d56402014-01-29 14:05:51 -0800332
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700333 index += this_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 nr_to_read -= this_chunk;
335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Fengguang Wu5ce11102007-07-19 01:47:59 -0700338/*
Fengguang Wuc743d962007-07-19 01:48:04 -0700339 * Set the initial window size, round to next power of 2 and square
340 * for small size, x 4 for medium, and x 2 for large
341 * for 128k (32 page) max ra
Lin Fengfb25a772021-11-05 13:43:47 -0700342 * 1-2 page = 16k, 3-4 page 32k, 5-8 page = 64k, > 8 page = 128k initial
Fengguang Wuc743d962007-07-19 01:48:04 -0700343 */
344static unsigned long get_init_ra_size(unsigned long size, unsigned long max)
345{
346 unsigned long newsize = roundup_pow_of_two(size);
347
348 if (newsize <= max / 32)
349 newsize = newsize * 4;
350 else if (newsize <= max / 4)
351 newsize = newsize * 2;
352 else
353 newsize = max;
354
355 return newsize;
356}
357
358/*
Fengguang Wu122a21d2007-07-19 01:48:01 -0700359 * Get the previous window size, ramp it up, and
360 * return it as the new window size.
361 */
Fengguang Wuc743d962007-07-19 01:48:04 -0700362static unsigned long get_next_ra_size(struct file_ra_state *ra,
Gao Xiang20ff1c92018-12-28 00:33:34 -0800363 unsigned long max)
Fengguang Wu122a21d2007-07-19 01:48:01 -0700364{
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700365 unsigned long cur = ra->size;
Fengguang Wu122a21d2007-07-19 01:48:01 -0700366
367 if (cur < max / 16)
Gao Xiang20ff1c92018-12-28 00:33:34 -0800368 return 4 * cur;
369 if (cur <= max / 2)
370 return 2 * cur;
371 return max;
Fengguang Wu122a21d2007-07-19 01:48:01 -0700372}
373
374/*
375 * On-demand readahead design.
376 *
377 * The fields in struct file_ra_state represent the most-recently-executed
378 * readahead attempt:
379 *
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700380 * |<----- async_size ---------|
381 * |------------------- size -------------------->|
382 * |==================#===========================|
383 * ^start ^page marked with PG_readahead
Fengguang Wu122a21d2007-07-19 01:48:01 -0700384 *
385 * To overlap application thinking time and disk I/O time, we do
386 * `readahead pipelining': Do not wait until the application consumed all
387 * readahead pages and stalled on the missing page at readahead_index;
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700388 * Instead, submit an asynchronous readahead I/O as soon as there are
389 * only async_size pages left in the readahead window. Normally async_size
390 * will be equal to size, for maximum pipelining.
Fengguang Wu122a21d2007-07-19 01:48:01 -0700391 *
392 * In interleaved sequential reads, concurrent streams on the same fd can
393 * be invalidating each other's readahead state. So we flag the new readahead
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700394 * page at (start+size-async_size) with PG_readahead, and use it as readahead
Fengguang Wu122a21d2007-07-19 01:48:01 -0700395 * indicator. The flag won't be set on already cached pages, to avoid the
396 * readahead-for-nothing fuss, saving pointless page cache lookups.
397 *
Fengguang Wuf4e6b492007-10-16 01:24:33 -0700398 * prev_pos tracks the last visited byte in the _previous_ read request.
Fengguang Wu122a21d2007-07-19 01:48:01 -0700399 * It should be maintained by the caller, and will be used for detecting
400 * small random reads. Note that the readahead algorithm checks loosely
401 * for sequential patterns. Hence interleaved reads might be served as
402 * sequential ones.
403 *
404 * There is a special-case: if the first page which the application tries to
405 * read happens to be the first page of the file, it is assumed that a linear
406 * read is about to happen and the window is immediately set to the initial size
407 * based on I/O request size and the max_readahead.
408 *
409 * The code ramps up the readahead size aggressively at first, but slow down as
410 * it approaches max_readhead.
411 */
412
413/*
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700414 * Count contiguously cached pages from @index-1 to @index-@max,
Wu Fengguang10be0b32009-06-16 15:31:36 -0700415 * this count is a conservative estimation of
416 * - length of the sequential read sequence, or
417 * - thrashing threshold in memory tight systems
418 */
419static pgoff_t count_history_pages(struct address_space *mapping,
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700420 pgoff_t index, unsigned long max)
Wu Fengguang10be0b32009-06-16 15:31:36 -0700421{
422 pgoff_t head;
423
424 rcu_read_lock();
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700425 head = page_cache_prev_miss(mapping, index - 1, max);
Wu Fengguang10be0b32009-06-16 15:31:36 -0700426 rcu_read_unlock();
427
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700428 return index - 1 - head;
Wu Fengguang10be0b32009-06-16 15:31:36 -0700429}
430
431/*
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -0400432 * page cache context based readahead
Wu Fengguang10be0b32009-06-16 15:31:36 -0700433 */
434static int try_context_readahead(struct address_space *mapping,
435 struct file_ra_state *ra,
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700436 pgoff_t index,
Wu Fengguang10be0b32009-06-16 15:31:36 -0700437 unsigned long req_size,
438 unsigned long max)
439{
440 pgoff_t size;
441
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700442 size = count_history_pages(mapping, index, max);
Wu Fengguang10be0b32009-06-16 15:31:36 -0700443
444 /*
Fengguang Wu2cad4012013-09-11 14:21:47 -0700445 * not enough history pages:
Wu Fengguang10be0b32009-06-16 15:31:36 -0700446 * it could be a random read
447 */
Fengguang Wu2cad4012013-09-11 14:21:47 -0700448 if (size <= req_size)
Wu Fengguang10be0b32009-06-16 15:31:36 -0700449 return 0;
450
451 /*
452 * starts from beginning of file:
453 * it is a strong indication of long-run stream (or whole-file-read)
454 */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700455 if (size >= index)
Wu Fengguang10be0b32009-06-16 15:31:36 -0700456 size *= 2;
457
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700458 ra->start = index;
Fengguang Wu2cad4012013-09-11 14:21:47 -0700459 ra->size = min(size + req_size, max);
460 ra->async_size = 1;
Wu Fengguang10be0b32009-06-16 15:31:36 -0700461
462 return 1;
463}
464
465/*
Matthew Wilcox (Oracle)793917d2020-02-05 11:27:01 -0500466 * There are some parts of the kernel which assume that PMD entries
467 * are exactly HPAGE_PMD_ORDER. Those should be fixed, but until then,
468 * limit the maximum allocation order to PMD size. I'm not aware of any
469 * assumptions about maximum order if THP are disabled, but 8 seems like
470 * a good order (that's 1MB if you're using 4kB pages)
471 */
472#ifdef CONFIG_TRANSPARENT_HUGEPAGE
473#define MAX_PAGECACHE_ORDER HPAGE_PMD_ORDER
474#else
475#define MAX_PAGECACHE_ORDER 8
476#endif
477
478static inline int ra_alloc_folio(struct readahead_control *ractl, pgoff_t index,
479 pgoff_t mark, unsigned int order, gfp_t gfp)
480{
481 int err;
482 struct folio *folio = filemap_alloc_folio(gfp, order);
483
484 if (!folio)
485 return -ENOMEM;
Matthew Wilcox (Oracle)b9ff43d2022-04-27 17:01:28 -0400486 mark = round_up(mark, 1UL << order);
487 if (index == mark)
Matthew Wilcox (Oracle)793917d2020-02-05 11:27:01 -0500488 folio_set_readahead(folio);
489 err = filemap_add_folio(ractl->mapping, folio, index, gfp);
Christoph Hellwig17604242022-09-15 10:41:56 +0100490 if (err) {
Matthew Wilcox (Oracle)793917d2020-02-05 11:27:01 -0500491 folio_put(folio);
Christoph Hellwig17604242022-09-15 10:41:56 +0100492 return err;
493 }
494
495 ractl->_nr_pages += 1UL << order;
496 ractl->_workingset |= folio_test_workingset(folio);
497 return 0;
Matthew Wilcox (Oracle)793917d2020-02-05 11:27:01 -0500498}
499
Matthew Wilcox (Oracle)56a4d672021-07-24 23:26:14 -0400500void page_cache_ra_order(struct readahead_control *ractl,
Matthew Wilcox (Oracle)793917d2020-02-05 11:27:01 -0500501 struct file_ra_state *ra, unsigned int new_order)
502{
503 struct address_space *mapping = ractl->mapping;
504 pgoff_t index = readahead_index(ractl);
505 pgoff_t limit = (i_size_read(mapping->host) - 1) >> PAGE_SHIFT;
506 pgoff_t mark = index + ra->size - ra->async_size;
507 int err = 0;
508 gfp_t gfp = readahead_gfp_mask(mapping);
509
510 if (!mapping_large_folio_support(mapping) || ra->size < 4)
511 goto fallback;
512
513 limit = min(limit, index + ra->size - 1);
514
515 if (new_order < MAX_PAGECACHE_ORDER) {
516 new_order += 2;
517 if (new_order > MAX_PAGECACHE_ORDER)
518 new_order = MAX_PAGECACHE_ORDER;
519 while ((1 << new_order) > ra->size)
520 new_order--;
521 }
522
Alistair Popple00fa15e2022-06-20 19:05:36 +1000523 filemap_invalidate_lock_shared(mapping);
Matthew Wilcox (Oracle)793917d2020-02-05 11:27:01 -0500524 while (index <= limit) {
525 unsigned int order = new_order;
526
527 /* Align with smaller pages if needed */
528 if (index & ((1UL << order) - 1)) {
529 order = __ffs(index);
530 if (order == 1)
531 order = 0;
532 }
533 /* Don't allocate pages past EOF */
534 while (index + (1UL << order) - 1 > limit) {
535 if (--order == 1)
536 order = 0;
537 }
538 err = ra_alloc_folio(ractl, index, mark, order, gfp);
539 if (err)
540 break;
541 index += 1UL << order;
542 }
543
544 if (index > limit) {
545 ra->size += index - limit - 1;
546 ra->async_size += index - limit - 1;
547 }
548
Christoph Hellwigb4e089d72022-03-31 05:35:55 -0700549 read_pages(ractl);
Alistair Popple00fa15e2022-06-20 19:05:36 +1000550 filemap_invalidate_unlock_shared(mapping);
Matthew Wilcox (Oracle)793917d2020-02-05 11:27:01 -0500551
552 /*
553 * If there were already pages in the page cache, then we may have
554 * left some gaps. Let the regular readahead code take care of this
555 * situation.
556 */
557 if (!err)
558 return;
559fallback:
560 do_page_cache_ra(ractl, ra->size, ra->async_size);
561}
562
563/*
Fengguang Wu122a21d2007-07-19 01:48:01 -0700564 * A minimal readahead algorithm for trivial sequential/random reads.
565 */
David Howells6e4af692020-10-15 20:06:21 -0700566static void ondemand_readahead(struct readahead_control *ractl,
Matthew Wilcox (Oracle)793917d2020-02-05 11:27:01 -0500567 struct folio *folio, unsigned long req_size)
Fengguang Wu122a21d2007-07-19 01:48:01 -0700568{
David Howells6e4af692020-10-15 20:06:21 -0700569 struct backing_dev_info *bdi = inode_to_bdi(ractl->mapping->host);
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +0100570 struct file_ra_state *ra = ractl->ra;
Jens Axboe9491ae42016-12-12 16:43:26 -0800571 unsigned long max_pages = ra->ra_pages;
Markus Stockhausendc30b962018-07-27 09:09:53 -0600572 unsigned long add_pages;
Matthew Wilcox (Oracle)b9ff43d2022-04-27 17:01:28 -0400573 pgoff_t index = readahead_index(ractl);
574 pgoff_t expected, prev_index;
575 unsigned int order = folio ? folio_order(folio) : 0;
Wu Fengguang045a2522009-06-16 15:31:33 -0700576
577 /*
Jens Axboe9491ae42016-12-12 16:43:26 -0800578 * If the request exceeds the readahead window, allow the read to
579 * be up to the optimal hardware IO size
580 */
581 if (req_size > max_pages && bdi->io_pages > max_pages)
582 max_pages = min(req_size, bdi->io_pages);
583
584 /*
Wu Fengguang045a2522009-06-16 15:31:33 -0700585 * start of file
586 */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700587 if (!index)
Wu Fengguang045a2522009-06-16 15:31:33 -0700588 goto initial_readahead;
Fengguang Wu122a21d2007-07-19 01:48:01 -0700589
590 /*
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700591 * It's the expected callback index, assume sequential access.
Fengguang Wu122a21d2007-07-19 01:48:01 -0700592 * Ramp up sizes, and push forward the readahead window.
593 */
Matthew Wilcox (Oracle)b9ff43d2022-04-27 17:01:28 -0400594 expected = round_up(ra->start + ra->size - ra->async_size,
595 1UL << order);
596 if (index == expected || index == (ra->start + ra->size)) {
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700597 ra->start += ra->size;
Jens Axboe9491ae42016-12-12 16:43:26 -0800598 ra->size = get_next_ra_size(ra, max_pages);
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700599 ra->async_size = ra->size;
600 goto readit;
Fengguang Wu122a21d2007-07-19 01:48:01 -0700601 }
602
Fengguang Wu122a21d2007-07-19 01:48:01 -0700603 /*
Matthew Wilcox (Oracle)793917d2020-02-05 11:27:01 -0500604 * Hit a marked folio without valid readahead state.
Fengguang Wu6b10c6c2007-10-16 01:24:34 -0700605 * E.g. interleaved reads.
606 * Query the pagecache for async_size, which normally equals to
607 * readahead size. Ramp it up and use it as the new readahead size.
608 */
Matthew Wilcox (Oracle)793917d2020-02-05 11:27:01 -0500609 if (folio) {
Fengguang Wu6b10c6c2007-10-16 01:24:34 -0700610 pgoff_t start;
611
Nick Piggin30002ed2008-07-25 19:45:28 -0700612 rcu_read_lock();
David Howells6e4af692020-10-15 20:06:21 -0700613 start = page_cache_next_miss(ractl->mapping, index + 1,
614 max_pages);
Nick Piggin30002ed2008-07-25 19:45:28 -0700615 rcu_read_unlock();
Fengguang Wu6b10c6c2007-10-16 01:24:34 -0700616
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700617 if (!start || start - index > max_pages)
Matthew Wilcox (Oracle)9a428232020-06-01 21:46:10 -0700618 return;
Fengguang Wu6b10c6c2007-10-16 01:24:34 -0700619
620 ra->start = start;
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700621 ra->size = start - index; /* old async_size */
Wu Fengguang160334a2009-06-16 15:31:23 -0700622 ra->size += req_size;
Jens Axboe9491ae42016-12-12 16:43:26 -0800623 ra->size = get_next_ra_size(ra, max_pages);
Fengguang Wu6b10c6c2007-10-16 01:24:34 -0700624 ra->async_size = ra->size;
625 goto readit;
626 }
627
628 /*
Wu Fengguang045a2522009-06-16 15:31:33 -0700629 * oversize read
Fengguang Wu122a21d2007-07-19 01:48:01 -0700630 */
Jens Axboe9491ae42016-12-12 16:43:26 -0800631 if (req_size > max_pages)
Wu Fengguang045a2522009-06-16 15:31:33 -0700632 goto initial_readahead;
633
634 /*
635 * sequential cache miss
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700636 * trivial case: (index - prev_index) == 1
637 * unaligned reads: (index - prev_index) == 0
Wu Fengguang045a2522009-06-16 15:31:33 -0700638 */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700639 prev_index = (unsigned long long)ra->prev_pos >> PAGE_SHIFT;
640 if (index - prev_index <= 1UL)
Wu Fengguang045a2522009-06-16 15:31:33 -0700641 goto initial_readahead;
642
643 /*
Wu Fengguang10be0b32009-06-16 15:31:36 -0700644 * Query the page cache and look for the traces(cached history pages)
645 * that a sequential stream would leave behind.
646 */
David Howells6e4af692020-10-15 20:06:21 -0700647 if (try_context_readahead(ractl->mapping, ra, index, req_size,
648 max_pages))
Wu Fengguang10be0b32009-06-16 15:31:36 -0700649 goto readit;
650
651 /*
Wu Fengguang045a2522009-06-16 15:31:33 -0700652 * standalone, small random read
653 * Read as is, and do not pollute the readahead state.
654 */
David Howells6e4af692020-10-15 20:06:21 -0700655 do_page_cache_ra(ractl, req_size, 0);
Matthew Wilcox (Oracle)9a428232020-06-01 21:46:10 -0700656 return;
Wu Fengguang045a2522009-06-16 15:31:33 -0700657
658initial_readahead:
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700659 ra->start = index;
Jens Axboe9491ae42016-12-12 16:43:26 -0800660 ra->size = get_init_ra_size(req_size, max_pages);
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700661 ra->async_size = ra->size > req_size ? ra->size - req_size : ra->size;
Fengguang Wu122a21d2007-07-19 01:48:01 -0700662
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700663readit:
Wu Fengguang51daa882009-06-16 15:31:24 -0700664 /*
665 * Will this read hit the readahead marker made by itself?
666 * If so, trigger the readahead marker hit now, and merge
667 * the resulted next readahead window into the current one.
Markus Stockhausendc30b962018-07-27 09:09:53 -0600668 * Take care of maximum IO pages as above.
Wu Fengguang51daa882009-06-16 15:31:24 -0700669 */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700670 if (index == ra->start && ra->size == ra->async_size) {
Markus Stockhausendc30b962018-07-27 09:09:53 -0600671 add_pages = get_next_ra_size(ra, max_pages);
672 if (ra->size + add_pages <= max_pages) {
673 ra->async_size = add_pages;
674 ra->size += add_pages;
675 } else {
676 ra->size = max_pages;
677 ra->async_size = max_pages >> 1;
678 }
Wu Fengguang51daa882009-06-16 15:31:24 -0700679 }
680
David Howells6e4af692020-10-15 20:06:21 -0700681 ractl->_index = ra->start;
Matthew Wilcox (Oracle)b9ff43d2022-04-27 17:01:28 -0400682 page_cache_ra_order(ractl, ra, order);
Fengguang Wu122a21d2007-07-19 01:48:01 -0700683}
684
Matthew Wilcox (Oracle)fefa7c42020-10-15 20:06:28 -0700685void page_cache_sync_ra(struct readahead_control *ractl,
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +0100686 unsigned long req_count)
Fengguang Wu122a21d2007-07-19 01:48:01 -0700687{
Jens Axboe324bcf52020-10-17 09:25:52 -0600688 bool do_forced_ra = ractl->file && (ractl->file->f_mode & FMODE_RANDOM);
Fengguang Wu122a21d2007-07-19 01:48:01 -0700689
Jens Axboe324bcf52020-10-17 09:25:52 -0600690 /*
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -0400691 * Even if readahead is disabled, issue this request as readahead
Jens Axboe324bcf52020-10-17 09:25:52 -0600692 * as we'll need it to satisfy the requested range. The forced
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -0400693 * readahead will do the right thing and limit the read to just the
Jens Axboe324bcf52020-10-17 09:25:52 -0600694 * requested range, which we'll set to 1 page for this case.
695 */
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +0100696 if (!ractl->ra->ra_pages || blk_cgroup_congested()) {
Jens Axboe324bcf52020-10-17 09:25:52 -0600697 if (!ractl->file)
698 return;
699 req_count = 1;
700 do_forced_ra = true;
701 }
Josef Bacikca47e8c2018-07-03 11:15:03 -0400702
Wu Fengguang01414502010-03-05 13:42:03 -0800703 /* be dumb */
Jens Axboe324bcf52020-10-17 09:25:52 -0600704 if (do_forced_ra) {
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +0100705 force_page_cache_ra(ractl, req_count);
Wu Fengguang01414502010-03-05 13:42:03 -0800706 return;
707 }
708
Matthew Wilcox (Oracle)793917d2020-02-05 11:27:01 -0500709 ondemand_readahead(ractl, NULL, req_count);
Fengguang Wu122a21d2007-07-19 01:48:01 -0700710}
Matthew Wilcox (Oracle)fefa7c42020-10-15 20:06:28 -0700711EXPORT_SYMBOL_GPL(page_cache_sync_ra);
Rusty Russellcf914a72007-07-19 01:48:08 -0700712
Matthew Wilcox (Oracle)fefa7c42020-10-15 20:06:28 -0700713void page_cache_async_ra(struct readahead_control *ractl,
Matthew Wilcox (Oracle)7836d992021-05-27 12:30:54 -0400714 struct folio *folio, unsigned long req_count)
Rusty Russellcf914a72007-07-19 01:48:08 -0700715{
Matthew Wilcox (Oracle)1e470282022-03-31 15:02:34 -0400716 /* no readahead */
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +0100717 if (!ractl->ra->ra_pages)
Rusty Russellcf914a72007-07-19 01:48:08 -0700718 return;
719
720 /*
721 * Same bit is used for PG_readahead and PG_reclaim.
722 */
Matthew Wilcox (Oracle)7836d992021-05-27 12:30:54 -0400723 if (folio_test_writeback(folio))
Rusty Russellcf914a72007-07-19 01:48:08 -0700724 return;
725
Matthew Wilcox (Oracle)7836d992021-05-27 12:30:54 -0400726 folio_clear_readahead(folio);
Rusty Russellcf914a72007-07-19 01:48:08 -0700727
Josef Bacikca47e8c2018-07-03 11:15:03 -0400728 if (blk_cgroup_congested())
729 return;
730
Matthew Wilcox (Oracle)793917d2020-02-05 11:27:01 -0500731 ondemand_readahead(ractl, folio, req_count);
Rusty Russellcf914a72007-07-19 01:48:08 -0700732}
Matthew Wilcox (Oracle)fefa7c42020-10-15 20:06:28 -0700733EXPORT_SYMBOL_GPL(page_cache_async_ra);
Cong Wang782182e2012-05-29 15:06:43 -0700734
Dominik Brodowskic7b95d52018-03-19 17:51:36 +0100735ssize_t ksys_readahead(int fd, loff_t offset, size_t count)
Cong Wang782182e2012-05-29 15:06:43 -0700736{
737 ssize_t ret;
Al Viro2903ff02012-08-28 12:52:22 -0400738 struct fd f;
Cong Wang782182e2012-05-29 15:06:43 -0700739
740 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400741 f = fdget(fd);
Amir Goldstein3d8f7612018-08-29 08:41:29 +0300742 if (!f.file || !(f.file->f_mode & FMODE_READ))
743 goto out;
744
745 /*
746 * The readahead() syscall is intended to run only on files
747 * that can execute readahead. If readahead is not possible
748 * on this file, then we must return -EINVAL.
749 */
750 ret = -EINVAL;
751 if (!f.file->f_mapping || !f.file->f_mapping->a_ops ||
752 !S_ISREG(file_inode(f.file)->i_mode))
753 goto out;
754
755 ret = vfs_fadvise(f.file, offset, count, POSIX_FADV_WILLNEED);
756out:
757 fdput(f);
Cong Wang782182e2012-05-29 15:06:43 -0700758 return ret;
759}
Dominik Brodowskic7b95d52018-03-19 17:51:36 +0100760
761SYSCALL_DEFINE3(readahead, int, fd, loff_t, offset, size_t, count)
762{
763 return ksys_readahead(fd, offset, count);
764}
David Howells3ca23642020-09-10 14:03:27 +0100765
Guo Ren59c10c52022-04-05 15:13:05 +0800766#if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_READAHEAD)
767COMPAT_SYSCALL_DEFINE4(readahead, int, fd, compat_arg_u64_dual(offset), size_t, count)
768{
769 return ksys_readahead(fd, compat_arg_u64_glue(offset), count);
770}
771#endif
772
David Howells3ca23642020-09-10 14:03:27 +0100773/**
774 * readahead_expand - Expand a readahead request
775 * @ractl: The request to be expanded
776 * @new_start: The revised start
777 * @new_len: The revised size of the request
778 *
779 * Attempt to expand a readahead request outwards from the current size to the
780 * specified size by inserting locked pages before and after the current window
781 * to increase the size to the new window. This may involve the insertion of
782 * THPs, in which case the window may get expanded even beyond what was
783 * requested.
784 *
785 * The algorithm will stop if it encounters a conflicting page already in the
786 * pagecache and leave a smaller expansion than requested.
787 *
788 * The caller must check for this by examining the revised @ractl object for a
789 * different expansion than was requested.
790 */
791void readahead_expand(struct readahead_control *ractl,
792 loff_t new_start, size_t new_len)
793{
794 struct address_space *mapping = ractl->mapping;
795 struct file_ra_state *ra = ractl->ra;
796 pgoff_t new_index, new_nr_pages;
797 gfp_t gfp_mask = readahead_gfp_mask(mapping);
798
799 new_index = new_start / PAGE_SIZE;
800
801 /* Expand the leading edge downwards */
802 while (ractl->_index > new_index) {
803 unsigned long index = ractl->_index - 1;
Matthew Wilcox (Oracle)11a98042023-01-16 19:39:41 +0000804 struct folio *folio = xa_load(&mapping->i_pages, index);
David Howells3ca23642020-09-10 14:03:27 +0100805
Matthew Wilcox (Oracle)11a98042023-01-16 19:39:41 +0000806 if (folio && !xa_is_value(folio))
807 return; /* Folio apparently present */
David Howells3ca23642020-09-10 14:03:27 +0100808
Matthew Wilcox (Oracle)11a98042023-01-16 19:39:41 +0000809 folio = filemap_alloc_folio(gfp_mask, 0);
810 if (!folio)
David Howells3ca23642020-09-10 14:03:27 +0100811 return;
Matthew Wilcox (Oracle)11a98042023-01-16 19:39:41 +0000812 if (filemap_add_folio(mapping, folio, index, gfp_mask) < 0) {
813 folio_put(folio);
David Howells3ca23642020-09-10 14:03:27 +0100814 return;
815 }
Matthew Wilcox (Oracle)11a98042023-01-16 19:39:41 +0000816 if (unlikely(folio_test_workingset(folio)) &&
817 !ractl->_workingset) {
818 ractl->_workingset = true;
819 psi_memstall_enter(&ractl->_pflags);
820 }
David Howells3ca23642020-09-10 14:03:27 +0100821 ractl->_nr_pages++;
Matthew Wilcox (Oracle)11a98042023-01-16 19:39:41 +0000822 ractl->_index = folio->index;
David Howells3ca23642020-09-10 14:03:27 +0100823 }
824
825 new_len += new_start - readahead_pos(ractl);
826 new_nr_pages = DIV_ROUND_UP(new_len, PAGE_SIZE);
827
828 /* Expand the trailing edge upwards */
829 while (ractl->_nr_pages < new_nr_pages) {
830 unsigned long index = ractl->_index + ractl->_nr_pages;
Matthew Wilcox (Oracle)11a98042023-01-16 19:39:41 +0000831 struct folio *folio = xa_load(&mapping->i_pages, index);
David Howells3ca23642020-09-10 14:03:27 +0100832
Matthew Wilcox (Oracle)11a98042023-01-16 19:39:41 +0000833 if (folio && !xa_is_value(folio))
834 return; /* Folio apparently present */
David Howells3ca23642020-09-10 14:03:27 +0100835
Matthew Wilcox (Oracle)11a98042023-01-16 19:39:41 +0000836 folio = filemap_alloc_folio(gfp_mask, 0);
837 if (!folio)
David Howells3ca23642020-09-10 14:03:27 +0100838 return;
Matthew Wilcox (Oracle)11a98042023-01-16 19:39:41 +0000839 if (filemap_add_folio(mapping, folio, index, gfp_mask) < 0) {
840 folio_put(folio);
David Howells3ca23642020-09-10 14:03:27 +0100841 return;
842 }
Matthew Wilcox (Oracle)11a98042023-01-16 19:39:41 +0000843 if (unlikely(folio_test_workingset(folio)) &&
844 !ractl->_workingset) {
Christoph Hellwig17604242022-09-15 10:41:56 +0100845 ractl->_workingset = true;
846 psi_memstall_enter(&ractl->_pflags);
847 }
David Howells3ca23642020-09-10 14:03:27 +0100848 ractl->_nr_pages++;
849 if (ra) {
850 ra->size++;
851 ra->async_size++;
852 }
853 }
854}
855EXPORT_SYMBOL(readahead_expand);