blob: 7ce320854badca6201782763b86c2553c39e8cc8 [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/blkdev.h>
16#include <linux/backing-dev.h>
Andrew Morton8bde37f2006-12-10 02:19:40 -080017#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/pagevec.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020019#include <linux/pagemap.h>
Cong Wang782182e2012-05-29 15:06:43 -070020#include <linux/syscalls.h>
21#include <linux/file.h>
Geliang Tangd72ee912016-01-14 15:22:01 -080022#include <linux/mm_inline.h>
Josef Bacikca47e8c2018-07-03 11:15:03 -040023#include <linux/blk-cgroup.h>
Amir Goldstein3d8f7612018-08-29 08:41:29 +030024#include <linux/fadvise.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,
117 gfp_t gfp)
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;
Jens Axboe5b417b12010-04-19 10:04:38 +0200120 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unsigned page_idx;
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)ad4ae1c2020-06-01 21:46:18 -0700124 return;
125
Jens Axboe5b417b12010-04-19 10:04:38 +0200126 blk_start_plug(&plug);
127
Matthew Wilcox (Oracle)a4d96532020-06-01 21:46:25 -0700128 if (aops->readpages) {
129 aops->readpages(rac->file, rac->mapping, pages,
130 readahead_count(rac));
OGAWA Hirofumi029e3322006-11-02 22:07:06 -0800131 /* Clean up the remaining pages */
132 put_pages_list(pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 goto out;
134 }
135
Matthew Wilcox (Oracle)a4d96532020-06-01 21:46:25 -0700136 for (page_idx = 0; page_idx < readahead_count(rac); page_idx++) {
Geliang Tangc8ad6302016-01-14 15:20:51 -0800137 struct page *page = lru_to_page(pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 list_del(&page->lru);
Matthew Wilcox (Oracle)a4d96532020-06-01 21:46:25 -0700139 if (!add_to_page_cache_lru(page, rac->mapping, page->index,
140 gfp))
141 aops->readpage(rac->file, page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300142 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
Jens Axboe5b417b12010-04-19 10:04:38 +0200144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145out:
Jens Axboe5b417b12010-04-19 10:04:38 +0200146 blk_finish_plug(&plug);
Matthew Wilcox (Oracle)ad4ae1c2020-06-01 21:46:18 -0700147
148 BUG_ON(!list_empty(pages));
Matthew Wilcox (Oracle)a4d96532020-06-01 21:46:25 -0700149 rac->_nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152/*
Christoph Hellwigb3751e62018-06-01 09:03:06 -0700153 * __do_page_cache_readahead() actually reads a chunk of disk. It allocates
154 * the pages first, then submits them for I/O. This avoids the very bad
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * behaviour which would occur if page allocations are causing VM writeback.
156 * We really don't want to intermingle reads and writes like that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
Matthew Wilcox (Oracle)9a428232020-06-01 21:46:10 -0700158void __do_page_cache_readahead(struct address_space *mapping,
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700159 struct file *filp, pgoff_t index, unsigned long nr_to_read,
Christoph Hellwigc534aa32018-06-01 09:03:05 -0700160 unsigned long lookahead_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct inode *inode = mapping->host;
163 struct page *page;
164 unsigned long end_index; /* The last page we want to read */
165 LIST_HEAD(page_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 loff_t isize = i_size_read(inode);
Michal Hocko8a5c7432016-07-26 15:24:53 -0700167 gfp_t gfp_mask = readahead_gfp_mask(mapping);
Matthew Wilcox (Oracle)a4d96532020-06-01 21:46:25 -0700168 struct readahead_control rac = {
169 .mapping = mapping,
170 .file = filp,
171 };
Matthew Wilcox (Oracle)c2c7ad72020-06-01 21:46:32 -0700172 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 if (isize == 0)
Matthew Wilcox (Oracle)9a428232020-06-01 21:46:10 -0700175 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300177 end_index = ((isize - 1) >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 /*
180 * Preallocate as many pages as we will need.
181 */
Matthew Wilcox (Oracle)c2c7ad72020-06-01 21:46:32 -0700182 for (i = 0; i < nr_to_read; i++) {
183 pgoff_t page_offset = index + i;
Fengguang Wuc743d962007-07-19 01:48:04 -0700184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (page_offset > end_index)
186 break;
187
Matthew Wilcox560d4542017-12-04 04:30:18 -0500188 page = xa_load(&mapping->i_pages, page_offset);
Matthew Wilcox3159f942017-11-03 13:30:42 -0400189 if (page && !xa_is_value(page)) {
Christoph Hellwigb3751e62018-06-01 09:03:06 -0700190 /*
191 * Page already present? Kick off the current batch of
192 * contiguous pages before continuing with the next
193 * batch.
194 */
Matthew Wilcox (Oracle)a4d96532020-06-01 21:46:25 -0700195 read_pages(&rac, &page_pool, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 continue;
Christoph Hellwigb3751e62018-06-01 09:03:06 -0700197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Michal Hocko8a5c7432016-07-26 15:24:53 -0700199 page = __page_cache_alloc(gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (!page)
201 break;
202 page->index = page_offset;
203 list_add(&page->lru, &page_pool);
Matthew Wilcox (Oracle)c2c7ad72020-06-01 21:46:32 -0700204 if (i == nr_to_read - lookahead_size)
Fengguang Wu46fc3e72007-07-19 01:47:57 -0700205 SetPageReadahead(page);
Matthew Wilcox (Oracle)a4d96532020-06-01 21:46:25 -0700206 rac._nr_pages++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /*
210 * Now start the IO. We ignore I/O errors - if the page is not
211 * uptodate then the caller will launch readpage again, and
212 * will then handle the error.
213 */
Matthew Wilcox (Oracle)a4d96532020-06-01 21:46:25 -0700214 read_pages(&rac, &page_pool, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/*
218 * Chunk the readahead into 2 megabyte units, so that we don't pin too much
219 * memory at once.
220 */
Matthew Wilcox (Oracle)9a428232020-06-01 21:46:10 -0700221void force_page_cache_readahead(struct address_space *mapping,
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700222 struct file *filp, pgoff_t index, unsigned long nr_to_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Jens Axboe9491ae42016-12-12 16:43:26 -0800224 struct backing_dev_info *bdi = inode_to_bdi(mapping->host);
225 struct file_ra_state *ra = &filp->f_ra;
226 unsigned long max_pages;
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (unlikely(!mapping->a_ops->readpage && !mapping->a_ops->readpages))
Matthew Wilcox (Oracle)9a428232020-06-01 21:46:10 -0700229 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Jens Axboe9491ae42016-12-12 16:43:26 -0800231 /*
232 * If the request exceeds the readahead window, allow the read to
233 * be up to the optimal hardware IO size
234 */
235 max_pages = max_t(unsigned long, bdi->io_pages, ra->ra_pages);
236 nr_to_read = min(nr_to_read, max_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 while (nr_to_read) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300238 unsigned long this_chunk = (2 * 1024 * 1024) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 if (this_chunk > nr_to_read)
241 this_chunk = nr_to_read;
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700242 __do_page_cache_readahead(mapping, filp, index, this_chunk, 0);
Mark Rutland58d56402014-01-29 14:05:51 -0800243
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700244 index += this_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 nr_to_read -= this_chunk;
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Fengguang Wu5ce11102007-07-19 01:47:59 -0700249/*
Fengguang Wuc743d962007-07-19 01:48:04 -0700250 * Set the initial window size, round to next power of 2 and square
251 * for small size, x 4 for medium, and x 2 for large
252 * for 128k (32 page) max ra
253 * 1-8 page = 32k initial, > 8 page = 128k initial
254 */
255static unsigned long get_init_ra_size(unsigned long size, unsigned long max)
256{
257 unsigned long newsize = roundup_pow_of_two(size);
258
259 if (newsize <= max / 32)
260 newsize = newsize * 4;
261 else if (newsize <= max / 4)
262 newsize = newsize * 2;
263 else
264 newsize = max;
265
266 return newsize;
267}
268
269/*
Fengguang Wu122a21d2007-07-19 01:48:01 -0700270 * Get the previous window size, ramp it up, and
271 * return it as the new window size.
272 */
Fengguang Wuc743d962007-07-19 01:48:04 -0700273static unsigned long get_next_ra_size(struct file_ra_state *ra,
Gao Xiang20ff1c92018-12-28 00:33:34 -0800274 unsigned long max)
Fengguang Wu122a21d2007-07-19 01:48:01 -0700275{
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700276 unsigned long cur = ra->size;
Fengguang Wu122a21d2007-07-19 01:48:01 -0700277
278 if (cur < max / 16)
Gao Xiang20ff1c92018-12-28 00:33:34 -0800279 return 4 * cur;
280 if (cur <= max / 2)
281 return 2 * cur;
282 return max;
Fengguang Wu122a21d2007-07-19 01:48:01 -0700283}
284
285/*
286 * On-demand readahead design.
287 *
288 * The fields in struct file_ra_state represent the most-recently-executed
289 * readahead attempt:
290 *
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700291 * |<----- async_size ---------|
292 * |------------------- size -------------------->|
293 * |==================#===========================|
294 * ^start ^page marked with PG_readahead
Fengguang Wu122a21d2007-07-19 01:48:01 -0700295 *
296 * To overlap application thinking time and disk I/O time, we do
297 * `readahead pipelining': Do not wait until the application consumed all
298 * readahead pages and stalled on the missing page at readahead_index;
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700299 * Instead, submit an asynchronous readahead I/O as soon as there are
300 * only async_size pages left in the readahead window. Normally async_size
301 * will be equal to size, for maximum pipelining.
Fengguang Wu122a21d2007-07-19 01:48:01 -0700302 *
303 * In interleaved sequential reads, concurrent streams on the same fd can
304 * be invalidating each other's readahead state. So we flag the new readahead
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700305 * page at (start+size-async_size) with PG_readahead, and use it as readahead
Fengguang Wu122a21d2007-07-19 01:48:01 -0700306 * indicator. The flag won't be set on already cached pages, to avoid the
307 * readahead-for-nothing fuss, saving pointless page cache lookups.
308 *
Fengguang Wuf4e6b492007-10-16 01:24:33 -0700309 * prev_pos tracks the last visited byte in the _previous_ read request.
Fengguang Wu122a21d2007-07-19 01:48:01 -0700310 * It should be maintained by the caller, and will be used for detecting
311 * small random reads. Note that the readahead algorithm checks loosely
312 * for sequential patterns. Hence interleaved reads might be served as
313 * sequential ones.
314 *
315 * There is a special-case: if the first page which the application tries to
316 * read happens to be the first page of the file, it is assumed that a linear
317 * read is about to happen and the window is immediately set to the initial size
318 * based on I/O request size and the max_readahead.
319 *
320 * The code ramps up the readahead size aggressively at first, but slow down as
321 * it approaches max_readhead.
322 */
323
324/*
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700325 * Count contiguously cached pages from @index-1 to @index-@max,
Wu Fengguang10be0b32009-06-16 15:31:36 -0700326 * this count is a conservative estimation of
327 * - length of the sequential read sequence, or
328 * - thrashing threshold in memory tight systems
329 */
330static pgoff_t count_history_pages(struct address_space *mapping,
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700331 pgoff_t index, unsigned long max)
Wu Fengguang10be0b32009-06-16 15:31:36 -0700332{
333 pgoff_t head;
334
335 rcu_read_lock();
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700336 head = page_cache_prev_miss(mapping, index - 1, max);
Wu Fengguang10be0b32009-06-16 15:31:36 -0700337 rcu_read_unlock();
338
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700339 return index - 1 - head;
Wu Fengguang10be0b32009-06-16 15:31:36 -0700340}
341
342/*
343 * page cache context based read-ahead
344 */
345static int try_context_readahead(struct address_space *mapping,
346 struct file_ra_state *ra,
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700347 pgoff_t index,
Wu Fengguang10be0b32009-06-16 15:31:36 -0700348 unsigned long req_size,
349 unsigned long max)
350{
351 pgoff_t size;
352
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700353 size = count_history_pages(mapping, index, max);
Wu Fengguang10be0b32009-06-16 15:31:36 -0700354
355 /*
Fengguang Wu2cad4012013-09-11 14:21:47 -0700356 * not enough history pages:
Wu Fengguang10be0b32009-06-16 15:31:36 -0700357 * it could be a random read
358 */
Fengguang Wu2cad4012013-09-11 14:21:47 -0700359 if (size <= req_size)
Wu Fengguang10be0b32009-06-16 15:31:36 -0700360 return 0;
361
362 /*
363 * starts from beginning of file:
364 * it is a strong indication of long-run stream (or whole-file-read)
365 */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700366 if (size >= index)
Wu Fengguang10be0b32009-06-16 15:31:36 -0700367 size *= 2;
368
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700369 ra->start = index;
Fengguang Wu2cad4012013-09-11 14:21:47 -0700370 ra->size = min(size + req_size, max);
371 ra->async_size = 1;
Wu Fengguang10be0b32009-06-16 15:31:36 -0700372
373 return 1;
374}
375
376/*
Fengguang Wu122a21d2007-07-19 01:48:01 -0700377 * A minimal readahead algorithm for trivial sequential/random reads.
378 */
Matthew Wilcox (Oracle)9a428232020-06-01 21:46:10 -0700379static void ondemand_readahead(struct address_space *mapping,
380 struct file_ra_state *ra, struct file *filp,
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700381 bool hit_readahead_marker, pgoff_t index,
Matthew Wilcox (Oracle)9a428232020-06-01 21:46:10 -0700382 unsigned long req_size)
Fengguang Wu122a21d2007-07-19 01:48:01 -0700383{
Jens Axboe9491ae42016-12-12 16:43:26 -0800384 struct backing_dev_info *bdi = inode_to_bdi(mapping->host);
385 unsigned long max_pages = ra->ra_pages;
Markus Stockhausendc30b962018-07-27 09:09:53 -0600386 unsigned long add_pages;
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700387 pgoff_t prev_index;
Wu Fengguang045a2522009-06-16 15:31:33 -0700388
389 /*
Jens Axboe9491ae42016-12-12 16:43:26 -0800390 * If the request exceeds the readahead window, allow the read to
391 * be up to the optimal hardware IO size
392 */
393 if (req_size > max_pages && bdi->io_pages > max_pages)
394 max_pages = min(req_size, bdi->io_pages);
395
396 /*
Wu Fengguang045a2522009-06-16 15:31:33 -0700397 * start of file
398 */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700399 if (!index)
Wu Fengguang045a2522009-06-16 15:31:33 -0700400 goto initial_readahead;
Fengguang Wu122a21d2007-07-19 01:48:01 -0700401
402 /*
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700403 * It's the expected callback index, assume sequential access.
Fengguang Wu122a21d2007-07-19 01:48:01 -0700404 * Ramp up sizes, and push forward the readahead window.
405 */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700406 if ((index == (ra->start + ra->size - ra->async_size) ||
407 index == (ra->start + ra->size))) {
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700408 ra->start += ra->size;
Jens Axboe9491ae42016-12-12 16:43:26 -0800409 ra->size = get_next_ra_size(ra, max_pages);
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700410 ra->async_size = ra->size;
411 goto readit;
Fengguang Wu122a21d2007-07-19 01:48:01 -0700412 }
413
Fengguang Wu122a21d2007-07-19 01:48:01 -0700414 /*
Fengguang Wu6b10c6c2007-10-16 01:24:34 -0700415 * Hit a marked page without valid readahead state.
416 * E.g. interleaved reads.
417 * Query the pagecache for async_size, which normally equals to
418 * readahead size. Ramp it up and use it as the new readahead size.
419 */
420 if (hit_readahead_marker) {
421 pgoff_t start;
422
Nick Piggin30002ed2008-07-25 19:45:28 -0700423 rcu_read_lock();
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700424 start = page_cache_next_miss(mapping, index + 1, max_pages);
Nick Piggin30002ed2008-07-25 19:45:28 -0700425 rcu_read_unlock();
Fengguang Wu6b10c6c2007-10-16 01:24:34 -0700426
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700427 if (!start || start - index > max_pages)
Matthew Wilcox (Oracle)9a428232020-06-01 21:46:10 -0700428 return;
Fengguang Wu6b10c6c2007-10-16 01:24:34 -0700429
430 ra->start = start;
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700431 ra->size = start - index; /* old async_size */
Wu Fengguang160334a2009-06-16 15:31:23 -0700432 ra->size += req_size;
Jens Axboe9491ae42016-12-12 16:43:26 -0800433 ra->size = get_next_ra_size(ra, max_pages);
Fengguang Wu6b10c6c2007-10-16 01:24:34 -0700434 ra->async_size = ra->size;
435 goto readit;
436 }
437
438 /*
Wu Fengguang045a2522009-06-16 15:31:33 -0700439 * oversize read
Fengguang Wu122a21d2007-07-19 01:48:01 -0700440 */
Jens Axboe9491ae42016-12-12 16:43:26 -0800441 if (req_size > max_pages)
Wu Fengguang045a2522009-06-16 15:31:33 -0700442 goto initial_readahead;
443
444 /*
445 * sequential cache miss
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700446 * trivial case: (index - prev_index) == 1
447 * unaligned reads: (index - prev_index) == 0
Wu Fengguang045a2522009-06-16 15:31:33 -0700448 */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700449 prev_index = (unsigned long long)ra->prev_pos >> PAGE_SHIFT;
450 if (index - prev_index <= 1UL)
Wu Fengguang045a2522009-06-16 15:31:33 -0700451 goto initial_readahead;
452
453 /*
Wu Fengguang10be0b32009-06-16 15:31:36 -0700454 * Query the page cache and look for the traces(cached history pages)
455 * that a sequential stream would leave behind.
456 */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700457 if (try_context_readahead(mapping, ra, index, req_size, max_pages))
Wu Fengguang10be0b32009-06-16 15:31:36 -0700458 goto readit;
459
460 /*
Wu Fengguang045a2522009-06-16 15:31:33 -0700461 * standalone, small random read
462 * Read as is, and do not pollute the readahead state.
463 */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700464 __do_page_cache_readahead(mapping, filp, index, req_size, 0);
Matthew Wilcox (Oracle)9a428232020-06-01 21:46:10 -0700465 return;
Wu Fengguang045a2522009-06-16 15:31:33 -0700466
467initial_readahead:
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700468 ra->start = index;
Jens Axboe9491ae42016-12-12 16:43:26 -0800469 ra->size = get_init_ra_size(req_size, max_pages);
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700470 ra->async_size = ra->size > req_size ? ra->size - req_size : ra->size;
Fengguang Wu122a21d2007-07-19 01:48:01 -0700471
Fengguang Wuf9acc8c2007-07-19 01:48:08 -0700472readit:
Wu Fengguang51daa882009-06-16 15:31:24 -0700473 /*
474 * Will this read hit the readahead marker made by itself?
475 * If so, trigger the readahead marker hit now, and merge
476 * the resulted next readahead window into the current one.
Markus Stockhausendc30b962018-07-27 09:09:53 -0600477 * Take care of maximum IO pages as above.
Wu Fengguang51daa882009-06-16 15:31:24 -0700478 */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700479 if (index == ra->start && ra->size == ra->async_size) {
Markus Stockhausendc30b962018-07-27 09:09:53 -0600480 add_pages = get_next_ra_size(ra, max_pages);
481 if (ra->size + add_pages <= max_pages) {
482 ra->async_size = add_pages;
483 ra->size += add_pages;
484 } else {
485 ra->size = max_pages;
486 ra->async_size = max_pages >> 1;
487 }
Wu Fengguang51daa882009-06-16 15:31:24 -0700488 }
489
Matthew Wilcox (Oracle)9a428232020-06-01 21:46:10 -0700490 ra_submit(ra, mapping, filp);
Fengguang Wu122a21d2007-07-19 01:48:01 -0700491}
492
493/**
Rusty Russellcf914a72007-07-19 01:48:08 -0700494 * page_cache_sync_readahead - generic file readahead
Fengguang Wu122a21d2007-07-19 01:48:01 -0700495 * @mapping: address_space which holds the pagecache and I/O vectors
496 * @ra: file_ra_state which holds the readahead state
497 * @filp: passed on to ->readpage() and ->readpages()
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700498 * @index: Index of first page to be read.
499 * @req_count: Total number of pages being read by the caller.
Fengguang Wu122a21d2007-07-19 01:48:01 -0700500 *
Rusty Russellcf914a72007-07-19 01:48:08 -0700501 * page_cache_sync_readahead() should be called when a cache miss happened:
502 * it will submit the read. The readahead logic may decide to piggyback more
503 * pages onto the read request if access patterns suggest it will improve
504 * performance.
Fengguang Wu122a21d2007-07-19 01:48:01 -0700505 */
Rusty Russellcf914a72007-07-19 01:48:08 -0700506void page_cache_sync_readahead(struct address_space *mapping,
507 struct file_ra_state *ra, struct file *filp,
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700508 pgoff_t index, unsigned long req_count)
Fengguang Wu122a21d2007-07-19 01:48:01 -0700509{
510 /* no read-ahead */
511 if (!ra->ra_pages)
Rusty Russellcf914a72007-07-19 01:48:08 -0700512 return;
Fengguang Wu122a21d2007-07-19 01:48:01 -0700513
Josef Bacikca47e8c2018-07-03 11:15:03 -0400514 if (blk_cgroup_congested())
515 return;
516
Wu Fengguang01414502010-03-05 13:42:03 -0800517 /* be dumb */
Wu Fengguang70655c02010-04-06 14:34:53 -0700518 if (filp && (filp->f_mode & FMODE_RANDOM)) {
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700519 force_page_cache_readahead(mapping, filp, index, req_count);
Wu Fengguang01414502010-03-05 13:42:03 -0800520 return;
521 }
522
Fengguang Wu122a21d2007-07-19 01:48:01 -0700523 /* do read-ahead */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700524 ondemand_readahead(mapping, ra, filp, false, index, req_count);
Fengguang Wu122a21d2007-07-19 01:48:01 -0700525}
Rusty Russellcf914a72007-07-19 01:48:08 -0700526EXPORT_SYMBOL_GPL(page_cache_sync_readahead);
527
528/**
529 * page_cache_async_readahead - file readahead for marked pages
530 * @mapping: address_space which holds the pagecache and I/O vectors
531 * @ra: file_ra_state which holds the readahead state
532 * @filp: passed on to ->readpage() and ->readpages()
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700533 * @page: The page at @index which triggered the readahead call.
534 * @index: Index of first page to be read.
535 * @req_count: Total number of pages being read by the caller.
Rusty Russellcf914a72007-07-19 01:48:08 -0700536 *
Huang Shijiebf8abe82010-05-24 14:32:36 -0700537 * page_cache_async_readahead() should be called when a page is used which
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700538 * is marked as PageReadahead; this is a marker to suggest that the application
Rusty Russellcf914a72007-07-19 01:48:08 -0700539 * has used up enough of the readahead window that we should start pulling in
Randy Dunlapf7850d92008-03-19 17:01:02 -0700540 * more pages.
541 */
Rusty Russellcf914a72007-07-19 01:48:08 -0700542void
543page_cache_async_readahead(struct address_space *mapping,
544 struct file_ra_state *ra, struct file *filp,
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700545 struct page *page, pgoff_t index,
546 unsigned long req_count)
Rusty Russellcf914a72007-07-19 01:48:08 -0700547{
548 /* no read-ahead */
549 if (!ra->ra_pages)
550 return;
551
552 /*
553 * Same bit is used for PG_readahead and PG_reclaim.
554 */
555 if (PageWriteback(page))
556 return;
557
558 ClearPageReadahead(page);
559
560 /*
561 * Defer asynchronous read-ahead on IO congestion.
562 */
Tejun Heo703c2702015-05-22 17:13:44 -0400563 if (inode_read_congested(mapping->host))
Rusty Russellcf914a72007-07-19 01:48:08 -0700564 return;
565
Josef Bacikca47e8c2018-07-03 11:15:03 -0400566 if (blk_cgroup_congested())
567 return;
568
Rusty Russellcf914a72007-07-19 01:48:08 -0700569 /* do read-ahead */
Matthew Wilcox (Oracle)08eb9652020-06-01 21:46:29 -0700570 ondemand_readahead(mapping, ra, filp, true, index, req_count);
Rusty Russellcf914a72007-07-19 01:48:08 -0700571}
572EXPORT_SYMBOL_GPL(page_cache_async_readahead);
Cong Wang782182e2012-05-29 15:06:43 -0700573
Dominik Brodowskic7b95d52018-03-19 17:51:36 +0100574ssize_t ksys_readahead(int fd, loff_t offset, size_t count)
Cong Wang782182e2012-05-29 15:06:43 -0700575{
576 ssize_t ret;
Al Viro2903ff02012-08-28 12:52:22 -0400577 struct fd f;
Cong Wang782182e2012-05-29 15:06:43 -0700578
579 ret = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400580 f = fdget(fd);
Amir Goldstein3d8f7612018-08-29 08:41:29 +0300581 if (!f.file || !(f.file->f_mode & FMODE_READ))
582 goto out;
583
584 /*
585 * The readahead() syscall is intended to run only on files
586 * that can execute readahead. If readahead is not possible
587 * on this file, then we must return -EINVAL.
588 */
589 ret = -EINVAL;
590 if (!f.file->f_mapping || !f.file->f_mapping->a_ops ||
591 !S_ISREG(file_inode(f.file)->i_mode))
592 goto out;
593
594 ret = vfs_fadvise(f.file, offset, count, POSIX_FADV_WILLNEED);
595out:
596 fdput(f);
Cong Wang782182e2012-05-29 15:06:43 -0700597 return ret;
598}
Dominik Brodowskic7b95d52018-03-19 17:51:36 +0100599
600SYSCALL_DEFINE3(readahead, int, fd, loff_t, offset, size_t, count)
601{
602 return ksys_readahead(fd, offset, count);
603}