Blame view

mm/readahead.c 17.5 KB
457c89965   Thomas Gleixner   treewide: Add SPD...
1
  // SPDX-License-Identifier: GPL-2.0-only
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
  /*
   * mm/readahead.c - address_space-level file readahead.
   *
   * Copyright (C) 2002, Linus Torvalds
   *
e1f8e8744   Francois Cami   Remove Andrew Mor...
7
   * 09Apr2002	Andrew Morton
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
9
10
11
   *		Initial version.
   */
  
  #include <linux/kernel.h>
11bd969fd   Ross Zwisler   mm: silently skip...
12
  #include <linux/dax.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
13
  #include <linux/gfp.h>
b95f1b31b   Paul Gortmaker   mm: Map most file...
14
  #include <linux/export.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
16
  #include <linux/blkdev.h>
  #include <linux/backing-dev.h>
8bde37f08   Andrew Morton   [PATCH] io-accoun...
17
  #include <linux/task_io_accounting_ops.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
  #include <linux/pagevec.h>
f5ff8422b   Jens Axboe   Fix warnings with...
19
  #include <linux/pagemap.h>
782182e53   Cong Wang   mm: move readahea...
20
21
  #include <linux/syscalls.h>
  #include <linux/file.h>
d72ee9111   Geliang Tang   mm: move lru_to_p...
22
  #include <linux/mm_inline.h>
ca47e8c72   Josef Bacik   mm: skip readahea...
23
  #include <linux/blk-cgroup.h>
3d8f76153   Amir Goldstein   vfs: implement re...
24
  #include <linux/fadvise.h>
f2c817bed   Matthew Wilcox (Oracle)   mm: use memalloc_...
25
  #include <linux/sched/mm.h>
62e32cf8f   Chris Goldsworthy   ANDROID: mm: Crea...
26
  #include <trace/hooks/mm.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27

29f175d12   Fabian Frederick   mm/readahead.c: i...
28
  #include "internal.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
30
31
32
33
34
35
  /*
   * Initialise a struct file's readahead state.  Assumes that the caller has
   * memset *ra to zero.
   */
  void
  file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping)
  {
de1414a65   Christoph Hellwig   fs: export inode_...
36
  	ra->ra_pages = inode_to_bdi(mapping->host)->ra_pages;
f4e6b498d   Fengguang Wu   readahead: combin...
37
  	ra->prev_pos = -1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
  }
d41cc702c   Steven Whitehouse   [GFS2] Export fil...
39
  EXPORT_SYMBOL_GPL(file_ra_state_init);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40

03fb3d2af   David Howells   FS-Cache: Release...
41
42
  /*
   * see if a page needs releasing upon read_cache_pages() failure
266cf658e   David Howells   FS-Cache: Recruit...
43
44
45
46
   * - the caller of read_cache_pages() may have set PG_private or PG_fscache
   *   before calling, such as the NFS fs marking pages that are cached locally
   *   on disk, thus we need to give the fs a chance to clean up in the event of
   *   an error
03fb3d2af   David Howells   FS-Cache: Release...
47
48
49
50
   */
  static void read_cache_pages_invalidate_page(struct address_space *mapping,
  					     struct page *page)
  {
266cf658e   David Howells   FS-Cache: Recruit...
51
  	if (page_has_private(page)) {
03fb3d2af   David Howells   FS-Cache: Release...
52
53
54
  		if (!trylock_page(page))
  			BUG();
  		page->mapping = mapping;
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
55
  		do_invalidatepage(page, 0, PAGE_SIZE);
03fb3d2af   David Howells   FS-Cache: Release...
56
57
58
  		page->mapping = NULL;
  		unlock_page(page);
  	}
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
59
  	put_page(page);
03fb3d2af   David Howells   FS-Cache: Release...
60
61
62
63
64
65
66
67
68
69
70
  }
  
  /*
   * release a list of pages, invalidating them first if need be
   */
  static void read_cache_pages_invalidate_pages(struct address_space *mapping,
  					      struct list_head *pages)
  {
  	struct page *victim;
  
  	while (!list_empty(pages)) {
c8ad6302c   Geliang Tang   mm/readahead.c, m...
71
  		victim = lru_to_page(pages);
03fb3d2af   David Howells   FS-Cache: Release...
72
73
74
75
  		list_del(&victim->lru);
  		read_cache_pages_invalidate_page(mapping, victim);
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76
  /**
bd40cddae   Randy Dunlap   [PATCH] kernel-do...
77
   * read_cache_pages - populate an address space with some pages & start reads against them
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78
79
80
81
82
83
84
   * @mapping: the address_space
   * @pages: The address of a list_head which contains the target pages.  These
   *   pages have their ->index populated and are otherwise uninitialised.
   * @filler: callback routine for filling a single page.
   * @data: private data for the callback routine.
   *
   * Hides the details of the LRU cache etc from the filesystems.
a862f68a8   Mike Rapoport   docs/core-api/mm:...
85
86
   *
   * Returns: %0 on success, error return by @filler otherwise
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
87
88
89
90
91
   */
  int read_cache_pages(struct address_space *mapping, struct list_head *pages,
  			int (*filler)(void *, struct page *), void *data)
  {
  	struct page *page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
  	int ret = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
  	while (!list_empty(pages)) {
c8ad6302c   Geliang Tang   mm/readahead.c, m...
94
  		page = lru_to_page(pages);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95
  		list_del(&page->lru);
063d99b4f   Michal Hocko   mm, fs: obey gfp_...
96
  		if (add_to_page_cache_lru(page, mapping, page->index,
8a5c743e3   Michal Hocko   mm, memcg: use co...
97
  				readahead_gfp_mask(mapping))) {
03fb3d2af   David Howells   FS-Cache: Release...
98
  			read_cache_pages_invalidate_page(mapping, page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
100
  			continue;
  		}
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
101
  		put_page(page);
eb2be1893   Nick Piggin   mm: buffered writ...
102

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103
  		ret = filler(data, page);
eb2be1893   Nick Piggin   mm: buffered writ...
104
  		if (unlikely(ret)) {
03fb3d2af   David Howells   FS-Cache: Release...
105
  			read_cache_pages_invalidate_pages(mapping, pages);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
107
  			break;
  		}
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
108
  		task_io_account_read(PAGE_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110
111
112
113
  	return ret;
  }
  
  EXPORT_SYMBOL(read_cache_pages);
62e32cf8f   Chris Goldsworthy   ANDROID: mm: Crea...
114
115
116
117
118
119
120
121
  gfp_t readahead_gfp_mask(struct address_space *x)
  {
  	gfp_t mask = mapping_gfp_mask(x) | __GFP_NORETRY | __GFP_NOWARN;
  
  	trace_android_rvh_set_readahead_gfp_mask(&mask);
  	return mask;
  }
  EXPORT_SYMBOL_GPL(readahead_gfp_mask);
a4d965366   Matthew Wilcox (Oracle)   mm: use readahead...
122
  static void read_pages(struct readahead_control *rac, struct list_head *pages,
c1f6925e1   Matthew Wilcox (Oracle)   mm: put readahead...
123
  		bool skip_page)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124
  {
a4d965366   Matthew Wilcox (Oracle)   mm: use readahead...
125
  	const struct address_space_operations *aops = rac->mapping->a_ops;
c1f6925e1   Matthew Wilcox (Oracle)   mm: put readahead...
126
  	struct page *page;
5b417b187   Jens Axboe   read-ahead: use p...
127
  	struct blk_plug plug;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128

a4d965366   Matthew Wilcox (Oracle)   mm: use readahead...
129
  	if (!readahead_count(rac))
c1f6925e1   Matthew Wilcox (Oracle)   mm: put readahead...
130
  		goto out;
ad4ae1c73   Matthew Wilcox (Oracle)   mm: move readahea...
131

5b417b187   Jens Axboe   read-ahead: use p...
132
  	blk_start_plug(&plug);
8151b4c8b   Matthew Wilcox (Oracle)   mm: add readahead...
133
134
135
136
137
138
139
140
  	if (aops->readahead) {
  		aops->readahead(rac);
  		/* Clean up the remaining pages */
  		while ((page = readahead_page(rac))) {
  			unlock_page(page);
  			put_page(page);
  		}
  	} else if (aops->readpages) {
a4d965366   Matthew Wilcox (Oracle)   mm: use readahead...
141
142
  		aops->readpages(rac->file, rac->mapping, pages,
  				readahead_count(rac));
029e332ea   OGAWA Hirofumi   [PATCH] Cleanup r...
143
144
  		/* Clean up the remaining pages */
  		put_pages_list(pages);
c1f6925e1   Matthew Wilcox (Oracle)   mm: put readahead...
145
146
147
148
  		rac->_index += rac->_nr_pages;
  		rac->_nr_pages = 0;
  	} else {
  		while ((page = readahead_page(rac))) {
a4d965366   Matthew Wilcox (Oracle)   mm: use readahead...
149
  			aops->readpage(rac->file, page);
c1f6925e1   Matthew Wilcox (Oracle)   mm: put readahead...
150
151
  			put_page(page);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152
  	}
5b417b187   Jens Axboe   read-ahead: use p...
153

5b417b187   Jens Axboe   read-ahead: use p...
154
  	blk_finish_plug(&plug);
ad4ae1c73   Matthew Wilcox (Oracle)   mm: move readahea...
155
156
  
  	BUG_ON(!list_empty(pages));
c1f6925e1   Matthew Wilcox (Oracle)   mm: put readahead...
157
158
159
160
161
  	BUG_ON(readahead_count(rac));
  
  out:
  	if (skip_page)
  		rac->_index++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162
  }
2c684234d   Matthew Wilcox (Oracle)   mm: add page_cach...
163
  /**
73bb49da5   Matthew Wilcox (Oracle)   mm/readahead: mak...
164
165
   * page_cache_ra_unbounded - Start unchecked readahead.
   * @ractl: Readahead control.
2c684234d   Matthew Wilcox (Oracle)   mm: add page_cach...
166
167
168
169
170
171
172
173
174
175
   * @nr_to_read: The number of pages to read.
   * @lookahead_size: Where to start the next readahead.
   *
   * This function is for filesystems to call when they want to start
   * readahead beyond a file's stated i_size.  This is almost certainly
   * not the function you want to call.  Use page_cache_async_readahead()
   * or page_cache_sync_readahead() instead.
   *
   * Context: File is referenced by caller.  Mutexes may be held by caller.
   * May sleep, but will not reenter filesystem to reclaim memory.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176
   */
73bb49da5   Matthew Wilcox (Oracle)   mm/readahead: mak...
177
178
  void page_cache_ra_unbounded(struct readahead_control *ractl,
  		unsigned long nr_to_read, unsigned long lookahead_size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
  {
73bb49da5   Matthew Wilcox (Oracle)   mm/readahead: mak...
180
181
  	struct address_space *mapping = ractl->mapping;
  	unsigned long index = readahead_index(ractl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
  	LIST_HEAD(page_pool);
8a5c743e3   Michal Hocko   mm, memcg: use co...
183
  	gfp_t gfp_mask = readahead_gfp_mask(mapping);
c2c7ad74b   Matthew Wilcox (Oracle)   mm: rename readah...
184
  	unsigned long i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
185
186
  
  	/*
f2c817bed   Matthew Wilcox (Oracle)   mm: use memalloc_...
187
188
189
190
191
192
193
194
195
196
197
198
  	 * Partway through the readahead operation, we will have added
  	 * locked pages to the page cache, but will not yet have submitted
  	 * them for I/O.  Adding another page may need to allocate memory,
  	 * which can trigger memory reclaim.  Telling the VM we're in
  	 * the middle of a filesystem operation will cause it to not
  	 * touch file-backed pages, preventing a deadlock.  Most (all?)
  	 * filesystems already specify __GFP_NOFS in their mapping's
  	 * gfp_mask, but let's be explicit here.
  	 */
  	unsigned int nofs = memalloc_nofs_save();
  
  	/*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
199
200
  	 * Preallocate as many pages as we will need.
  	 */
c2c7ad74b   Matthew Wilcox (Oracle)   mm: rename readah...
201
  	for (i = 0; i < nr_to_read; i++) {
b0f31d78c   Matthew Wilcox (Oracle)   mm: move end_inde...
202
  		struct page *page = xa_load(&mapping->i_pages, index + i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
203

73bb49da5   Matthew Wilcox (Oracle)   mm/readahead: mak...
204
  		BUG_ON(index + i != ractl->_index + ractl->_nr_pages);
c1f6925e1   Matthew Wilcox (Oracle)   mm: put readahead...
205

3159f943a   Matthew Wilcox   xarray: Replace e...
206
  		if (page && !xa_is_value(page)) {
b3751e6ab   Christoph Hellwig   mm: split ->readp...
207
  			/*
2d8163e48   Matthew Wilcox (Oracle)   mm: document why ...
208
209
210
211
212
213
  			 * Page already present?  Kick off the current batch
  			 * of contiguous pages before continuing with the
  			 * next batch.  This page may be the one we would
  			 * have intended to mark as Readahead, but we don't
  			 * have a stable reference to this page, and it's
  			 * not worth getting one just for that.
b3751e6ab   Christoph Hellwig   mm: split ->readp...
214
  			 */
73bb49da5   Matthew Wilcox (Oracle)   mm/readahead: mak...
215
  			read_pages(ractl, &page_pool, true);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
216
  			continue;
b3751e6ab   Christoph Hellwig   mm: split ->readp...
217
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218

8a5c743e3   Michal Hocko   mm, memcg: use co...
219
  		page = __page_cache_alloc(gfp_mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
221
  		if (!page)
  			break;
c1f6925e1   Matthew Wilcox (Oracle)   mm: put readahead...
222
223
224
225
226
227
  		if (mapping->a_ops->readpages) {
  			page->index = index + i;
  			list_add(&page->lru, &page_pool);
  		} else if (add_to_page_cache_lru(page, mapping, index + i,
  					gfp_mask) < 0) {
  			put_page(page);
73bb49da5   Matthew Wilcox (Oracle)   mm/readahead: mak...
228
  			read_pages(ractl, &page_pool, true);
c1f6925e1   Matthew Wilcox (Oracle)   mm: put readahead...
229
230
  			continue;
  		}
c2c7ad74b   Matthew Wilcox (Oracle)   mm: rename readah...
231
  		if (i == nr_to_read - lookahead_size)
46fc3e7b4   Fengguang Wu   readahead: add lo...
232
  			SetPageReadahead(page);
73bb49da5   Matthew Wilcox (Oracle)   mm/readahead: mak...
233
  		ractl->_nr_pages++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
235
236
237
238
239
240
  
  	/*
  	 * Now start the IO.  We ignore I/O errors - if the page is not
  	 * uptodate then the caller will launch readpage again, and
  	 * will then handle the error.
  	 */
73bb49da5   Matthew Wilcox (Oracle)   mm/readahead: mak...
241
  	read_pages(ractl, &page_pool, false);
f2c817bed   Matthew Wilcox (Oracle)   mm: use memalloc_...
242
  	memalloc_nofs_restore(nofs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
243
  }
73bb49da5   Matthew Wilcox (Oracle)   mm/readahead: mak...
244
  EXPORT_SYMBOL_GPL(page_cache_ra_unbounded);
2c684234d   Matthew Wilcox (Oracle)   mm: add page_cach...
245
246
  
  /*
8238287ea   Matthew Wilcox (Oracle)   mm/readahead: mak...
247
   * do_page_cache_ra() actually reads a chunk of disk.  It allocates
2c684234d   Matthew Wilcox (Oracle)   mm: add page_cach...
248
249
250
251
   * the pages first, then submits them for I/O. This avoids the very bad
   * behaviour which would occur if page allocations are causing VM writeback.
   * We really don't want to intermingle reads and writes like that.
   */
8238287ea   Matthew Wilcox (Oracle)   mm/readahead: mak...
252
253
  void do_page_cache_ra(struct readahead_control *ractl,
  		unsigned long nr_to_read, unsigned long lookahead_size)
2c684234d   Matthew Wilcox (Oracle)   mm: add page_cach...
254
  {
8238287ea   Matthew Wilcox (Oracle)   mm/readahead: mak...
255
256
  	struct inode *inode = ractl->mapping->host;
  	unsigned long index = readahead_index(ractl);
2c684234d   Matthew Wilcox (Oracle)   mm: add page_cach...
257
258
259
260
261
262
263
264
265
266
267
268
  	loff_t isize = i_size_read(inode);
  	pgoff_t end_index;	/* The last page we want to read */
  
  	if (isize == 0)
  		return;
  
  	end_index = (isize - 1) >> PAGE_SHIFT;
  	if (index > end_index)
  		return;
  	/* Don't read past the page containing the last byte of the file */
  	if (nr_to_read > end_index - index)
  		nr_to_read = end_index - index + 1;
8238287ea   Matthew Wilcox (Oracle)   mm/readahead: mak...
269
  	page_cache_ra_unbounded(ractl, nr_to_read, lookahead_size);
2c684234d   Matthew Wilcox (Oracle)   mm: add page_cach...
270
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
271
272
273
274
275
  
  /*
   * Chunk the readahead into 2 megabyte units, so that we don't pin too much
   * memory at once.
   */
7b3df3b9a   David Howells   mm/readahead: pas...
276
  void force_page_cache_ra(struct readahead_control *ractl,
b1647dc0d   David Howells   mm/readahead: pas...
277
  		struct file_ra_state *ra, unsigned long nr_to_read)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278
  {
7b3df3b9a   David Howells   mm/readahead: pas...
279
  	struct address_space *mapping = ractl->mapping;
9491ae4aa   Jens Axboe   mm: don't cap req...
280
  	struct backing_dev_info *bdi = inode_to_bdi(mapping->host);
7b3df3b9a   David Howells   mm/readahead: pas...
281
  	unsigned long max_pages, index;
9491ae4aa   Jens Axboe   mm: don't cap req...
282

8151b4c8b   Matthew Wilcox (Oracle)   mm: add readahead...
283
284
  	if (unlikely(!mapping->a_ops->readpage && !mapping->a_ops->readpages &&
  			!mapping->a_ops->readahead))
9a42823a1   Matthew Wilcox (Oracle)   mm: return void f...
285
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
286

9491ae4aa   Jens Axboe   mm: don't cap req...
287
288
289
290
  	/*
  	 * If the request exceeds the readahead window, allow the read to
  	 * be up to the optimal hardware IO size
  	 */
7b3df3b9a   David Howells   mm/readahead: pas...
291
  	index = readahead_index(ractl);
9491ae4aa   Jens Axboe   mm: don't cap req...
292
  	max_pages = max_t(unsigned long, bdi->io_pages, ra->ra_pages);
7b3df3b9a   David Howells   mm/readahead: pas...
293
  	nr_to_read = min_t(unsigned long, nr_to_read, max_pages);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
294
  	while (nr_to_read) {
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
295
  		unsigned long this_chunk = (2 * 1024 * 1024) / PAGE_SIZE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
296
297
298
  
  		if (this_chunk > nr_to_read)
  			this_chunk = nr_to_read;
7b3df3b9a   David Howells   mm/readahead: pas...
299
300
  		ractl->_index = index;
  		do_page_cache_ra(ractl, this_chunk, 0);
58d5640eb   Mark Rutland   mm/readahead.c: f...
301

08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
302
  		index += this_chunk;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
303
304
  		nr_to_read -= this_chunk;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
305
  }
5ce1110b9   Fengguang Wu   readahead: data s...
306
  /*
c743d96b6   Fengguang Wu   readahead: remove...
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
   * Set the initial window size, round to next power of 2 and square
   * for small size, x 4 for medium, and x 2 for large
   * for 128k (32 page) max ra
   * 1-8 page = 32k initial, > 8 page = 128k initial
   */
  static unsigned long get_init_ra_size(unsigned long size, unsigned long max)
  {
  	unsigned long newsize = roundup_pow_of_two(size);
  
  	if (newsize <= max / 32)
  		newsize = newsize * 4;
  	else if (newsize <= max / 4)
  		newsize = newsize * 2;
  	else
  		newsize = max;
  
  	return newsize;
  }
  
  /*
122a21d11   Fengguang Wu   readahead: on-dem...
327
328
329
   *  Get the previous window size, ramp it up, and
   *  return it as the new window size.
   */
c743d96b6   Fengguang Wu   readahead: remove...
330
  static unsigned long get_next_ra_size(struct file_ra_state *ra,
20ff1c950   Gao Xiang   mm/readahead.c: s...
331
  				      unsigned long max)
122a21d11   Fengguang Wu   readahead: on-dem...
332
  {
f9acc8c7b   Fengguang Wu   readahead: sanify...
333
  	unsigned long cur = ra->size;
122a21d11   Fengguang Wu   readahead: on-dem...
334
335
  
  	if (cur < max / 16)
20ff1c950   Gao Xiang   mm/readahead.c: s...
336
337
338
339
  		return 4 * cur;
  	if (cur <= max / 2)
  		return 2 * cur;
  	return max;
122a21d11   Fengguang Wu   readahead: on-dem...
340
341
342
343
344
345
346
347
  }
  
  /*
   * On-demand readahead design.
   *
   * The fields in struct file_ra_state represent the most-recently-executed
   * readahead attempt:
   *
f9acc8c7b   Fengguang Wu   readahead: sanify...
348
349
350
351
   *                        |<----- async_size ---------|
   *     |------------------- size -------------------->|
   *     |==================#===========================|
   *     ^start             ^page marked with PG_readahead
122a21d11   Fengguang Wu   readahead: on-dem...
352
353
354
355
   *
   * To overlap application thinking time and disk I/O time, we do
   * `readahead pipelining': Do not wait until the application consumed all
   * readahead pages and stalled on the missing page at readahead_index;
f9acc8c7b   Fengguang Wu   readahead: sanify...
356
357
358
   * Instead, submit an asynchronous readahead I/O as soon as there are
   * only async_size pages left in the readahead window. Normally async_size
   * will be equal to size, for maximum pipelining.
122a21d11   Fengguang Wu   readahead: on-dem...
359
360
361
   *
   * In interleaved sequential reads, concurrent streams on the same fd can
   * be invalidating each other's readahead state. So we flag the new readahead
f9acc8c7b   Fengguang Wu   readahead: sanify...
362
   * page at (start+size-async_size) with PG_readahead, and use it as readahead
122a21d11   Fengguang Wu   readahead: on-dem...
363
364
365
   * indicator. The flag won't be set on already cached pages, to avoid the
   * readahead-for-nothing fuss, saving pointless page cache lookups.
   *
f4e6b498d   Fengguang Wu   readahead: combin...
366
   * prev_pos tracks the last visited byte in the _previous_ read request.
122a21d11   Fengguang Wu   readahead: on-dem...
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
   * It should be maintained by the caller, and will be used for detecting
   * small random reads. Note that the readahead algorithm checks loosely
   * for sequential patterns. Hence interleaved reads might be served as
   * sequential ones.
   *
   * There is a special-case: if the first page which the application tries to
   * read happens to be the first page of the file, it is assumed that a linear
   * read is about to happen and the window is immediately set to the initial size
   * based on I/O request size and the max_readahead.
   *
   * The code ramps up the readahead size aggressively at first, but slow down as
   * it approaches max_readhead.
   */
  
  /*
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
382
   * Count contiguously cached pages from @index-1 to @index-@max,
10be0b372   Wu Fengguang   readahead: introd...
383
384
385
386
387
   * this count is a conservative estimation of
   * 	- length of the sequential read sequence, or
   * 	- thrashing threshold in memory tight systems
   */
  static pgoff_t count_history_pages(struct address_space *mapping,
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
388
  				   pgoff_t index, unsigned long max)
10be0b372   Wu Fengguang   readahead: introd...
389
390
391
392
  {
  	pgoff_t head;
  
  	rcu_read_lock();
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
393
  	head = page_cache_prev_miss(mapping, index - 1, max);
10be0b372   Wu Fengguang   readahead: introd...
394
  	rcu_read_unlock();
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
395
  	return index - 1 - head;
10be0b372   Wu Fengguang   readahead: introd...
396
397
398
399
400
401
402
  }
  
  /*
   * page cache context based read-ahead
   */
  static int try_context_readahead(struct address_space *mapping,
  				 struct file_ra_state *ra,
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
403
  				 pgoff_t index,
10be0b372   Wu Fengguang   readahead: introd...
404
405
406
407
  				 unsigned long req_size,
  				 unsigned long max)
  {
  	pgoff_t size;
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
408
  	size = count_history_pages(mapping, index, max);
10be0b372   Wu Fengguang   readahead: introd...
409
410
  
  	/*
2cad40180   Fengguang Wu   readahead: make c...
411
  	 * not enough history pages:
10be0b372   Wu Fengguang   readahead: introd...
412
413
  	 * it could be a random read
  	 */
2cad40180   Fengguang Wu   readahead: make c...
414
  	if (size <= req_size)
10be0b372   Wu Fengguang   readahead: introd...
415
416
417
418
419
420
  		return 0;
  
  	/*
  	 * starts from beginning of file:
  	 * it is a strong indication of long-run stream (or whole-file-read)
  	 */
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
421
  	if (size >= index)
10be0b372   Wu Fengguang   readahead: introd...
422
  		size *= 2;
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
423
  	ra->start = index;
2cad40180   Fengguang Wu   readahead: make c...
424
425
  	ra->size = min(size + req_size, max);
  	ra->async_size = 1;
10be0b372   Wu Fengguang   readahead: introd...
426
427
428
429
430
  
  	return 1;
  }
  
  /*
122a21d11   Fengguang Wu   readahead: on-dem...
431
432
   * A minimal readahead algorithm for trivial sequential/random reads.
   */
6e4af69ae   David Howells   mm/readahead: mak...
433
434
  static void ondemand_readahead(struct readahead_control *ractl,
  		struct file_ra_state *ra, bool hit_readahead_marker,
9a42823a1   Matthew Wilcox (Oracle)   mm: return void f...
435
  		unsigned long req_size)
122a21d11   Fengguang Wu   readahead: on-dem...
436
  {
6e4af69ae   David Howells   mm/readahead: mak...
437
  	struct backing_dev_info *bdi = inode_to_bdi(ractl->mapping->host);
9491ae4aa   Jens Axboe   mm: don't cap req...
438
  	unsigned long max_pages = ra->ra_pages;
dc30b96ab   Markus Stockhausen   readahead: strict...
439
  	unsigned long add_pages;
6e4af69ae   David Howells   mm/readahead: mak...
440
  	unsigned long index = readahead_index(ractl);
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
441
  	pgoff_t prev_index;
045a2529a   Wu Fengguang   readahead: move t...
442
443
  
  	/*
9491ae4aa   Jens Axboe   mm: don't cap req...
444
445
446
447
448
449
450
  	 * If the request exceeds the readahead window, allow the read to
  	 * be up to the optimal hardware IO size
  	 */
  	if (req_size > max_pages && bdi->io_pages > max_pages)
  		max_pages = min(req_size, bdi->io_pages);
  
  	/*
045a2529a   Wu Fengguang   readahead: move t...
451
452
  	 * start of file
  	 */
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
453
  	if (!index)
045a2529a   Wu Fengguang   readahead: move t...
454
  		goto initial_readahead;
122a21d11   Fengguang Wu   readahead: on-dem...
455
456
  
  	/*
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
457
  	 * It's the expected callback index, assume sequential access.
122a21d11   Fengguang Wu   readahead: on-dem...
458
459
  	 * Ramp up sizes, and push forward the readahead window.
  	 */
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
460
461
  	if ((index == (ra->start + ra->size - ra->async_size) ||
  	     index == (ra->start + ra->size))) {
f9acc8c7b   Fengguang Wu   readahead: sanify...
462
  		ra->start += ra->size;
9491ae4aa   Jens Axboe   mm: don't cap req...
463
  		ra->size = get_next_ra_size(ra, max_pages);
f9acc8c7b   Fengguang Wu   readahead: sanify...
464
465
  		ra->async_size = ra->size;
  		goto readit;
122a21d11   Fengguang Wu   readahead: on-dem...
466
  	}
122a21d11   Fengguang Wu   readahead: on-dem...
467
  	/*
6b10c6c9f   Fengguang Wu   readahead: basic ...
468
469
470
471
472
473
474
  	 * Hit a marked page without valid readahead state.
  	 * E.g. interleaved reads.
  	 * Query the pagecache for async_size, which normally equals to
  	 * readahead size. Ramp it up and use it as the new readahead size.
  	 */
  	if (hit_readahead_marker) {
  		pgoff_t start;
30002ed2e   Nick Piggin   mm: readahead sca...
475
  		rcu_read_lock();
6e4af69ae   David Howells   mm/readahead: mak...
476
477
  		start = page_cache_next_miss(ractl->mapping, index + 1,
  				max_pages);
30002ed2e   Nick Piggin   mm: readahead sca...
478
  		rcu_read_unlock();
6b10c6c9f   Fengguang Wu   readahead: basic ...
479

08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
480
  		if (!start || start - index > max_pages)
9a42823a1   Matthew Wilcox (Oracle)   mm: return void f...
481
  			return;
6b10c6c9f   Fengguang Wu   readahead: basic ...
482
483
  
  		ra->start = start;
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
484
  		ra->size = start - index;	/* old async_size */
160334a0c   Wu Fengguang   readahead: increa...
485
  		ra->size += req_size;
9491ae4aa   Jens Axboe   mm: don't cap req...
486
  		ra->size = get_next_ra_size(ra, max_pages);
6b10c6c9f   Fengguang Wu   readahead: basic ...
487
488
489
490
491
  		ra->async_size = ra->size;
  		goto readit;
  	}
  
  	/*
045a2529a   Wu Fengguang   readahead: move t...
492
  	 * oversize read
122a21d11   Fengguang Wu   readahead: on-dem...
493
  	 */
9491ae4aa   Jens Axboe   mm: don't cap req...
494
  	if (req_size > max_pages)
045a2529a   Wu Fengguang   readahead: move t...
495
496
497
498
  		goto initial_readahead;
  
  	/*
  	 * sequential cache miss
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
499
500
  	 * trivial case: (index - prev_index) == 1
  	 * unaligned reads: (index - prev_index) == 0
045a2529a   Wu Fengguang   readahead: move t...
501
  	 */
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
502
503
  	prev_index = (unsigned long long)ra->prev_pos >> PAGE_SHIFT;
  	if (index - prev_index <= 1UL)
045a2529a   Wu Fengguang   readahead: move t...
504
505
506
  		goto initial_readahead;
  
  	/*
10be0b372   Wu Fengguang   readahead: introd...
507
508
509
  	 * Query the page cache and look for the traces(cached history pages)
  	 * that a sequential stream would leave behind.
  	 */
6e4af69ae   David Howells   mm/readahead: mak...
510
511
  	if (try_context_readahead(ractl->mapping, ra, index, req_size,
  			max_pages))
10be0b372   Wu Fengguang   readahead: introd...
512
513
514
  		goto readit;
  
  	/*
045a2529a   Wu Fengguang   readahead: move t...
515
516
517
  	 * standalone, small random read
  	 * Read as is, and do not pollute the readahead state.
  	 */
6e4af69ae   David Howells   mm/readahead: mak...
518
  	do_page_cache_ra(ractl, req_size, 0);
9a42823a1   Matthew Wilcox (Oracle)   mm: return void f...
519
  	return;
045a2529a   Wu Fengguang   readahead: move t...
520
521
  
  initial_readahead:
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
522
  	ra->start = index;
9491ae4aa   Jens Axboe   mm: don't cap req...
523
  	ra->size = get_init_ra_size(req_size, max_pages);
f9acc8c7b   Fengguang Wu   readahead: sanify...
524
  	ra->async_size = ra->size > req_size ? ra->size - req_size : ra->size;
122a21d11   Fengguang Wu   readahead: on-dem...
525

f9acc8c7b   Fengguang Wu   readahead: sanify...
526
  readit:
51daa88eb   Wu Fengguang   readahead: remove...
527
528
529
530
  	/*
  	 * Will this read hit the readahead marker made by itself?
  	 * If so, trigger the readahead marker hit now, and merge
  	 * the resulted next readahead window into the current one.
dc30b96ab   Markus Stockhausen   readahead: strict...
531
  	 * Take care of maximum IO pages as above.
51daa88eb   Wu Fengguang   readahead: remove...
532
  	 */
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
533
  	if (index == ra->start && ra->size == ra->async_size) {
dc30b96ab   Markus Stockhausen   readahead: strict...
534
535
536
537
538
539
540
541
  		add_pages = get_next_ra_size(ra, max_pages);
  		if (ra->size + add_pages <= max_pages) {
  			ra->async_size = add_pages;
  			ra->size += add_pages;
  		} else {
  			ra->size = max_pages;
  			ra->async_size = max_pages >> 1;
  		}
51daa88eb   Wu Fengguang   readahead: remove...
542
  	}
6e4af69ae   David Howells   mm/readahead: mak...
543
544
  	ractl->_index = ra->start;
  	do_page_cache_ra(ractl, ra->size, ra->async_size);
122a21d11   Fengguang Wu   readahead: on-dem...
545
  }
fefa7c478   Matthew Wilcox (Oracle)   mm/readahead: add...
546
547
  void page_cache_sync_ra(struct readahead_control *ractl,
  		struct file_ra_state *ra, unsigned long req_count)
122a21d11   Fengguang Wu   readahead: on-dem...
548
  {
324bcf54c   Jens Axboe   mm: use limited r...
549
  	bool do_forced_ra = ractl->file && (ractl->file->f_mode & FMODE_RANDOM);
cf914a7d6   Rusty Russell   readahead: split ...
550

324bcf54c   Jens Axboe   mm: use limited r...
551
552
553
554
555
556
557
558
559
560
561
562
  	/*
  	 * Even if read-ahead is disabled, issue this request as read-ahead
  	 * as we'll need it to satisfy the requested range. The forced
  	 * read-ahead will do the right thing and limit the read to just the
  	 * requested range, which we'll set to 1 page for this case.
  	 */
  	if (!ra->ra_pages || blk_cgroup_congested()) {
  		if (!ractl->file)
  			return;
  		req_count = 1;
  		do_forced_ra = true;
  	}
ca47e8c72   Josef Bacik   mm: skip readahea...
563

0141450f6   Wu Fengguang   readahead: introd...
564
  	/* be dumb */
324bcf54c   Jens Axboe   mm: use limited r...
565
  	if (do_forced_ra) {
b1647dc0d   David Howells   mm/readahead: pas...
566
  		force_page_cache_ra(ractl, ra, req_count);
0141450f6   Wu Fengguang   readahead: introd...
567
568
  		return;
  	}
cf914a7d6   Rusty Russell   readahead: split ...
569
  	/* do read-ahead */
fefa7c478   Matthew Wilcox (Oracle)   mm/readahead: add...
570
  	ondemand_readahead(ractl, ra, false, req_count);
cf914a7d6   Rusty Russell   readahead: split ...
571
  }
fefa7c478   Matthew Wilcox (Oracle)   mm/readahead: add...
572
  EXPORT_SYMBOL_GPL(page_cache_sync_ra);
cf914a7d6   Rusty Russell   readahead: split ...
573

fefa7c478   Matthew Wilcox (Oracle)   mm/readahead: add...
574
575
576
  void page_cache_async_ra(struct readahead_control *ractl,
  		struct file_ra_state *ra, struct page *page,
  		unsigned long req_count)
cf914a7d6   Rusty Russell   readahead: split ...
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
  {
  	/* no read-ahead */
  	if (!ra->ra_pages)
  		return;
  
  	/*
  	 * Same bit is used for PG_readahead and PG_reclaim.
  	 */
  	if (PageWriteback(page))
  		return;
  
  	ClearPageReadahead(page);
  
  	/*
  	 * Defer asynchronous read-ahead on IO congestion.
  	 */
fefa7c478   Matthew Wilcox (Oracle)   mm/readahead: add...
593
  	if (inode_read_congested(ractl->mapping->host))
cf914a7d6   Rusty Russell   readahead: split ...
594
  		return;
122a21d11   Fengguang Wu   readahead: on-dem...
595

ca47e8c72   Josef Bacik   mm: skip readahea...
596
597
  	if (blk_cgroup_congested())
  		return;
122a21d11   Fengguang Wu   readahead: on-dem...
598
  	/* do read-ahead */
fefa7c478   Matthew Wilcox (Oracle)   mm/readahead: add...
599
  	ondemand_readahead(ractl, ra, true, req_count);
122a21d11   Fengguang Wu   readahead: on-dem...
600
  }
fefa7c478   Matthew Wilcox (Oracle)   mm/readahead: add...
601
  EXPORT_SYMBOL_GPL(page_cache_async_ra);
782182e53   Cong Wang   mm: move readahea...
602

c7b95d515   Dominik Brodowski   mm: add ksys_read...
603
  ssize_t ksys_readahead(int fd, loff_t offset, size_t count)
782182e53   Cong Wang   mm: move readahea...
604
605
  {
  	ssize_t ret;
2903ff019   Al Viro   switch simple cas...
606
  	struct fd f;
782182e53   Cong Wang   mm: move readahea...
607
608
  
  	ret = -EBADF;
2903ff019   Al Viro   switch simple cas...
609
  	f = fdget(fd);
3d8f76153   Amir Goldstein   vfs: implement re...
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
  	if (!f.file || !(f.file->f_mode & FMODE_READ))
  		goto out;
  
  	/*
  	 * The readahead() syscall is intended to run only on files
  	 * that can execute readahead. If readahead is not possible
  	 * on this file, then we must return -EINVAL.
  	 */
  	ret = -EINVAL;
  	if (!f.file->f_mapping || !f.file->f_mapping->a_ops ||
  	    !S_ISREG(file_inode(f.file)->i_mode))
  		goto out;
  
  	ret = vfs_fadvise(f.file, offset, count, POSIX_FADV_WILLNEED);
  out:
  	fdput(f);
782182e53   Cong Wang   mm: move readahea...
626
627
  	return ret;
  }
c7b95d515   Dominik Brodowski   mm: add ksys_read...
628
629
630
631
632
  
  SYSCALL_DEFINE3(readahead, int, fd, loff_t, offset, size_t, count)
  {
  	return ksys_readahead(fd, offset, count);
  }