Blame view

mm/readahead.c 17.3 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>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26

29f175d12   Fabian Frederick   mm/readahead.c: i...
27
  #include "internal.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
29
30
31
32
33
34
  /*
   * 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_...
35
  	ra->ra_pages = inode_to_bdi(mapping->host)->ra_pages;
f4e6b498d   Fengguang Wu   readahead: combin...
36
  	ra->prev_pos = -1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
  }
d41cc702c   Steven Whitehouse   [GFS2] Export fil...
38
  EXPORT_SYMBOL_GPL(file_ra_state_init);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39

03fb3d2af   David Howells   FS-Cache: Release...
40
41
  /*
   * see if a page needs releasing upon read_cache_pages() failure
266cf658e   David Howells   FS-Cache: Recruit...
42
43
44
45
   * - 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...
46
47
48
49
   */
  static void read_cache_pages_invalidate_page(struct address_space *mapping,
  					     struct page *page)
  {
266cf658e   David Howells   FS-Cache: Recruit...
50
  	if (page_has_private(page)) {
03fb3d2af   David Howells   FS-Cache: Release...
51
52
53
  		if (!trylock_page(page))
  			BUG();
  		page->mapping = mapping;
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
54
  		do_invalidatepage(page, 0, PAGE_SIZE);
03fb3d2af   David Howells   FS-Cache: Release...
55
56
57
  		page->mapping = NULL;
  		unlock_page(page);
  	}
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
58
  	put_page(page);
03fb3d2af   David Howells   FS-Cache: Release...
59
60
61
62
63
64
65
66
67
68
69
  }
  
  /*
   * 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...
70
  		victim = lru_to_page(pages);
03fb3d2af   David Howells   FS-Cache: Release...
71
72
73
74
  		list_del(&victim->lru);
  		read_cache_pages_invalidate_page(mapping, victim);
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
  /**
bd40cddae   Randy Dunlap   [PATCH] kernel-do...
76
   * read_cache_pages - populate an address space with some pages & start reads against them
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
79
80
81
82
83
   * @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:...
84
85
   *
   * Returns: %0 on success, error return by @filler otherwise
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
88
89
90
   */
  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
91
  	int ret = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
  	while (!list_empty(pages)) {
c8ad6302c   Geliang Tang   mm/readahead.c, m...
93
  		page = lru_to_page(pages);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
  		list_del(&page->lru);
063d99b4f   Michal Hocko   mm, fs: obey gfp_...
95
  		if (add_to_page_cache_lru(page, mapping, page->index,
8a5c743e3   Michal Hocko   mm, memcg: use co...
96
  				readahead_gfp_mask(mapping))) {
03fb3d2af   David Howells   FS-Cache: Release...
97
  			read_cache_pages_invalidate_page(mapping, page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
99
  			continue;
  		}
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
100
  		put_page(page);
eb2be1893   Nick Piggin   mm: buffered writ...
101

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
  		ret = filler(data, page);
eb2be1893   Nick Piggin   mm: buffered writ...
103
  		if (unlikely(ret)) {
03fb3d2af   David Howells   FS-Cache: Release...
104
  			read_cache_pages_invalidate_pages(mapping, pages);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
105
106
  			break;
  		}
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
107
  		task_io_account_read(PAGE_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
111
112
  	return ret;
  }
  
  EXPORT_SYMBOL(read_cache_pages);
a4d965366   Matthew Wilcox (Oracle)   mm: use readahead...
113
  static void read_pages(struct readahead_control *rac, struct list_head *pages,
c1f6925e1   Matthew Wilcox (Oracle)   mm: put readahead...
114
  		bool skip_page)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
  {
a4d965366   Matthew Wilcox (Oracle)   mm: use readahead...
116
  	const struct address_space_operations *aops = rac->mapping->a_ops;
c1f6925e1   Matthew Wilcox (Oracle)   mm: put readahead...
117
  	struct page *page;
5b417b187   Jens Axboe   read-ahead: use p...
118
  	struct blk_plug plug;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119

a4d965366   Matthew Wilcox (Oracle)   mm: use readahead...
120
  	if (!readahead_count(rac))
c1f6925e1   Matthew Wilcox (Oracle)   mm: put readahead...
121
  		goto out;
ad4ae1c73   Matthew Wilcox (Oracle)   mm: move readahea...
122

5b417b187   Jens Axboe   read-ahead: use p...
123
  	blk_start_plug(&plug);
8151b4c8b   Matthew Wilcox (Oracle)   mm: add readahead...
124
125
126
127
128
129
130
131
  	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...
132
133
  		aops->readpages(rac->file, rac->mapping, pages,
  				readahead_count(rac));
029e332ea   OGAWA Hirofumi   [PATCH] Cleanup r...
134
135
  		/* Clean up the remaining pages */
  		put_pages_list(pages);
c1f6925e1   Matthew Wilcox (Oracle)   mm: put readahead...
136
137
138
139
  		rac->_index += rac->_nr_pages;
  		rac->_nr_pages = 0;
  	} else {
  		while ((page = readahead_page(rac))) {
a4d965366   Matthew Wilcox (Oracle)   mm: use readahead...
140
  			aops->readpage(rac->file, page);
c1f6925e1   Matthew Wilcox (Oracle)   mm: put readahead...
141
142
  			put_page(page);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143
  	}
5b417b187   Jens Axboe   read-ahead: use p...
144

5b417b187   Jens Axboe   read-ahead: use p...
145
  	blk_finish_plug(&plug);
ad4ae1c73   Matthew Wilcox (Oracle)   mm: move readahea...
146
147
  
  	BUG_ON(!list_empty(pages));
c1f6925e1   Matthew Wilcox (Oracle)   mm: put readahead...
148
149
150
151
152
  	BUG_ON(readahead_count(rac));
  
  out:
  	if (skip_page)
  		rac->_index++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
153
  }
2c684234d   Matthew Wilcox (Oracle)   mm: add page_cach...
154
  /**
73bb49da5   Matthew Wilcox (Oracle)   mm/readahead: mak...
155
156
   * page_cache_ra_unbounded - Start unchecked readahead.
   * @ractl: Readahead control.
2c684234d   Matthew Wilcox (Oracle)   mm: add page_cach...
157
158
159
160
161
162
163
164
165
166
   * @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
167
   */
73bb49da5   Matthew Wilcox (Oracle)   mm/readahead: mak...
168
169
  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
170
  {
73bb49da5   Matthew Wilcox (Oracle)   mm/readahead: mak...
171
172
  	struct address_space *mapping = ractl->mapping;
  	unsigned long index = readahead_index(ractl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
  	LIST_HEAD(page_pool);
8a5c743e3   Michal Hocko   mm, memcg: use co...
174
  	gfp_t gfp_mask = readahead_gfp_mask(mapping);
c2c7ad74b   Matthew Wilcox (Oracle)   mm: rename readah...
175
  	unsigned long i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176
177
  
  	/*
f2c817bed   Matthew Wilcox (Oracle)   mm: use memalloc_...
178
179
180
181
182
183
184
185
186
187
188
189
  	 * 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
190
191
  	 * Preallocate as many pages as we will need.
  	 */
c2c7ad74b   Matthew Wilcox (Oracle)   mm: rename readah...
192
  	for (i = 0; i < nr_to_read; i++) {
b0f31d78c   Matthew Wilcox (Oracle)   mm: move end_inde...
193
  		struct page *page = xa_load(&mapping->i_pages, index + i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194

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

3159f943a   Matthew Wilcox   xarray: Replace e...
197
  		if (page && !xa_is_value(page)) {
b3751e6ab   Christoph Hellwig   mm: split ->readp...
198
  			/*
2d8163e48   Matthew Wilcox (Oracle)   mm: document why ...
199
200
201
202
203
204
  			 * 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...
205
  			 */
73bb49da5   Matthew Wilcox (Oracle)   mm/readahead: mak...
206
  			read_pages(ractl, &page_pool, true);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
207
  			continue;
b3751e6ab   Christoph Hellwig   mm: split ->readp...
208
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209

8a5c743e3   Michal Hocko   mm, memcg: use co...
210
  		page = __page_cache_alloc(gfp_mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
211
212
  		if (!page)
  			break;
c1f6925e1   Matthew Wilcox (Oracle)   mm: put readahead...
213
214
215
216
217
218
  		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...
219
  			read_pages(ractl, &page_pool, true);
c1f6925e1   Matthew Wilcox (Oracle)   mm: put readahead...
220
221
  			continue;
  		}
c2c7ad74b   Matthew Wilcox (Oracle)   mm: rename readah...
222
  		if (i == nr_to_read - lookahead_size)
46fc3e7b4   Fengguang Wu   readahead: add lo...
223
  			SetPageReadahead(page);
73bb49da5   Matthew Wilcox (Oracle)   mm/readahead: mak...
224
  		ractl->_nr_pages++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
226
227
228
229
230
231
  
  	/*
  	 * 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...
232
  	read_pages(ractl, &page_pool, false);
f2c817bed   Matthew Wilcox (Oracle)   mm: use memalloc_...
233
  	memalloc_nofs_restore(nofs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
  }
73bb49da5   Matthew Wilcox (Oracle)   mm/readahead: mak...
235
  EXPORT_SYMBOL_GPL(page_cache_ra_unbounded);
2c684234d   Matthew Wilcox (Oracle)   mm: add page_cach...
236
237
  
  /*
8238287ea   Matthew Wilcox (Oracle)   mm/readahead: mak...
238
   * do_page_cache_ra() actually reads a chunk of disk.  It allocates
2c684234d   Matthew Wilcox (Oracle)   mm: add page_cach...
239
240
241
242
   * 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...
243
244
  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...
245
  {
8238287ea   Matthew Wilcox (Oracle)   mm/readahead: mak...
246
247
  	struct inode *inode = ractl->mapping->host;
  	unsigned long index = readahead_index(ractl);
2c684234d   Matthew Wilcox (Oracle)   mm: add page_cach...
248
249
250
251
252
253
254
255
256
257
258
259
  	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...
260
  	page_cache_ra_unbounded(ractl, nr_to_read, lookahead_size);
2c684234d   Matthew Wilcox (Oracle)   mm: add page_cach...
261
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262
263
264
265
266
  
  /*
   * Chunk the readahead into 2 megabyte units, so that we don't pin too much
   * memory at once.
   */
7b3df3b9a   David Howells   mm/readahead: pas...
267
  void force_page_cache_ra(struct readahead_control *ractl,
b1647dc0d   David Howells   mm/readahead: pas...
268
  		struct file_ra_state *ra, unsigned long nr_to_read)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269
  {
7b3df3b9a   David Howells   mm/readahead: pas...
270
  	struct address_space *mapping = ractl->mapping;
9491ae4aa   Jens Axboe   mm: don't cap req...
271
  	struct backing_dev_info *bdi = inode_to_bdi(mapping->host);
7b3df3b9a   David Howells   mm/readahead: pas...
272
  	unsigned long max_pages, index;
9491ae4aa   Jens Axboe   mm: don't cap req...
273

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

9491ae4aa   Jens Axboe   mm: don't cap req...
278
279
280
281
  	/*
  	 * 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...
282
  	index = readahead_index(ractl);
9491ae4aa   Jens Axboe   mm: don't cap req...
283
  	max_pages = max_t(unsigned long, bdi->io_pages, ra->ra_pages);
7b3df3b9a   David Howells   mm/readahead: pas...
284
  	nr_to_read = min_t(unsigned long, nr_to_read, max_pages);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
285
  	while (nr_to_read) {
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
286
  		unsigned long this_chunk = (2 * 1024 * 1024) / PAGE_SIZE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
288
289
  
  		if (this_chunk > nr_to_read)
  			this_chunk = nr_to_read;
7b3df3b9a   David Howells   mm/readahead: pas...
290
291
  		ractl->_index = index;
  		do_page_cache_ra(ractl, this_chunk, 0);
58d5640eb   Mark Rutland   mm/readahead.c: f...
292

08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
293
  		index += this_chunk;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
294
295
  		nr_to_read -= this_chunk;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
296
  }
5ce1110b9   Fengguang Wu   readahead: data s...
297
  /*
c743d96b6   Fengguang Wu   readahead: remove...
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
   * 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...
318
319
320
   *  Get the previous window size, ramp it up, and
   *  return it as the new window size.
   */
c743d96b6   Fengguang Wu   readahead: remove...
321
  static unsigned long get_next_ra_size(struct file_ra_state *ra,
20ff1c950   Gao Xiang   mm/readahead.c: s...
322
  				      unsigned long max)
122a21d11   Fengguang Wu   readahead: on-dem...
323
  {
f9acc8c7b   Fengguang Wu   readahead: sanify...
324
  	unsigned long cur = ra->size;
122a21d11   Fengguang Wu   readahead: on-dem...
325
326
  
  	if (cur < max / 16)
20ff1c950   Gao Xiang   mm/readahead.c: s...
327
328
329
330
  		return 4 * cur;
  	if (cur <= max / 2)
  		return 2 * cur;
  	return max;
122a21d11   Fengguang Wu   readahead: on-dem...
331
332
333
334
335
336
337
338
  }
  
  /*
   * On-demand readahead design.
   *
   * The fields in struct file_ra_state represent the most-recently-executed
   * readahead attempt:
   *
f9acc8c7b   Fengguang Wu   readahead: sanify...
339
340
341
342
   *                        |<----- async_size ---------|
   *     |------------------- size -------------------->|
   *     |==================#===========================|
   *     ^start             ^page marked with PG_readahead
122a21d11   Fengguang Wu   readahead: on-dem...
343
344
345
346
   *
   * 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...
347
348
349
   * 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...
350
351
352
   *
   * 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...
353
   * page at (start+size-async_size) with PG_readahead, and use it as readahead
122a21d11   Fengguang Wu   readahead: on-dem...
354
355
356
   * 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...
357
   * prev_pos tracks the last visited byte in the _previous_ read request.
122a21d11   Fengguang Wu   readahead: on-dem...
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
   * 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...
373
   * Count contiguously cached pages from @index-1 to @index-@max,
10be0b372   Wu Fengguang   readahead: introd...
374
375
376
377
378
   * 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...
379
  				   pgoff_t index, unsigned long max)
10be0b372   Wu Fengguang   readahead: introd...
380
381
382
383
  {
  	pgoff_t head;
  
  	rcu_read_lock();
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
384
  	head = page_cache_prev_miss(mapping, index - 1, max);
10be0b372   Wu Fengguang   readahead: introd...
385
  	rcu_read_unlock();
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
386
  	return index - 1 - head;
10be0b372   Wu Fengguang   readahead: introd...
387
388
389
390
391
392
393
  }
  
  /*
   * 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...
394
  				 pgoff_t index,
10be0b372   Wu Fengguang   readahead: introd...
395
396
397
398
  				 unsigned long req_size,
  				 unsigned long max)
  {
  	pgoff_t size;
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
399
  	size = count_history_pages(mapping, index, max);
10be0b372   Wu Fengguang   readahead: introd...
400
401
  
  	/*
2cad40180   Fengguang Wu   readahead: make c...
402
  	 * not enough history pages:
10be0b372   Wu Fengguang   readahead: introd...
403
404
  	 * it could be a random read
  	 */
2cad40180   Fengguang Wu   readahead: make c...
405
  	if (size <= req_size)
10be0b372   Wu Fengguang   readahead: introd...
406
407
408
409
410
411
  		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...
412
  	if (size >= index)
10be0b372   Wu Fengguang   readahead: introd...
413
  		size *= 2;
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
414
  	ra->start = index;
2cad40180   Fengguang Wu   readahead: make c...
415
416
  	ra->size = min(size + req_size, max);
  	ra->async_size = 1;
10be0b372   Wu Fengguang   readahead: introd...
417
418
419
420
421
  
  	return 1;
  }
  
  /*
122a21d11   Fengguang Wu   readahead: on-dem...
422
423
   * A minimal readahead algorithm for trivial sequential/random reads.
   */
6e4af69ae   David Howells   mm/readahead: mak...
424
425
  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...
426
  		unsigned long req_size)
122a21d11   Fengguang Wu   readahead: on-dem...
427
  {
6e4af69ae   David Howells   mm/readahead: mak...
428
  	struct backing_dev_info *bdi = inode_to_bdi(ractl->mapping->host);
9491ae4aa   Jens Axboe   mm: don't cap req...
429
  	unsigned long max_pages = ra->ra_pages;
dc30b96ab   Markus Stockhausen   readahead: strict...
430
  	unsigned long add_pages;
6e4af69ae   David Howells   mm/readahead: mak...
431
  	unsigned long index = readahead_index(ractl);
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
432
  	pgoff_t prev_index;
045a2529a   Wu Fengguang   readahead: move t...
433
434
  
  	/*
9491ae4aa   Jens Axboe   mm: don't cap req...
435
436
437
438
439
440
441
  	 * 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...
442
443
  	 * start of file
  	 */
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
444
  	if (!index)
045a2529a   Wu Fengguang   readahead: move t...
445
  		goto initial_readahead;
122a21d11   Fengguang Wu   readahead: on-dem...
446
447
  
  	/*
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
448
  	 * It's the expected callback index, assume sequential access.
122a21d11   Fengguang Wu   readahead: on-dem...
449
450
  	 * Ramp up sizes, and push forward the readahead window.
  	 */
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
451
452
  	if ((index == (ra->start + ra->size - ra->async_size) ||
  	     index == (ra->start + ra->size))) {
f9acc8c7b   Fengguang Wu   readahead: sanify...
453
  		ra->start += ra->size;
9491ae4aa   Jens Axboe   mm: don't cap req...
454
  		ra->size = get_next_ra_size(ra, max_pages);
f9acc8c7b   Fengguang Wu   readahead: sanify...
455
456
  		ra->async_size = ra->size;
  		goto readit;
122a21d11   Fengguang Wu   readahead: on-dem...
457
  	}
122a21d11   Fengguang Wu   readahead: on-dem...
458
  	/*
6b10c6c9f   Fengguang Wu   readahead: basic ...
459
460
461
462
463
464
465
  	 * 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...
466
  		rcu_read_lock();
6e4af69ae   David Howells   mm/readahead: mak...
467
468
  		start = page_cache_next_miss(ractl->mapping, index + 1,
  				max_pages);
30002ed2e   Nick Piggin   mm: readahead sca...
469
  		rcu_read_unlock();
6b10c6c9f   Fengguang Wu   readahead: basic ...
470

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

f9acc8c7b   Fengguang Wu   readahead: sanify...
517
  readit:
51daa88eb   Wu Fengguang   readahead: remove...
518
519
520
521
  	/*
  	 * 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...
522
  	 * Take care of maximum IO pages as above.
51daa88eb   Wu Fengguang   readahead: remove...
523
  	 */
08eb9658a   Matthew Wilcox (Oracle)   mm: rename variou...
524
  	if (index == ra->start && ra->size == ra->async_size) {
dc30b96ab   Markus Stockhausen   readahead: strict...
525
526
527
528
529
530
531
532
  		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...
533
  	}
6e4af69ae   David Howells   mm/readahead: mak...
534
535
  	ractl->_index = ra->start;
  	do_page_cache_ra(ractl, ra->size, ra->async_size);
122a21d11   Fengguang Wu   readahead: on-dem...
536
  }
fefa7c478   Matthew Wilcox (Oracle)   mm/readahead: add...
537
538
  void page_cache_sync_ra(struct readahead_control *ractl,
  		struct file_ra_state *ra, unsigned long req_count)
122a21d11   Fengguang Wu   readahead: on-dem...
539
  {
324bcf54c   Jens Axboe   mm: use limited r...
540
  	bool do_forced_ra = ractl->file && (ractl->file->f_mode & FMODE_RANDOM);
cf914a7d6   Rusty Russell   readahead: split ...
541

324bcf54c   Jens Axboe   mm: use limited r...
542
543
544
545
546
547
548
549
550
551
552
553
  	/*
  	 * 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...
554

0141450f6   Wu Fengguang   readahead: introd...
555
  	/* be dumb */
324bcf54c   Jens Axboe   mm: use limited r...
556
  	if (do_forced_ra) {
b1647dc0d   David Howells   mm/readahead: pas...
557
  		force_page_cache_ra(ractl, ra, req_count);
0141450f6   Wu Fengguang   readahead: introd...
558
559
  		return;
  	}
cf914a7d6   Rusty Russell   readahead: split ...
560
  	/* do read-ahead */
fefa7c478   Matthew Wilcox (Oracle)   mm/readahead: add...
561
  	ondemand_readahead(ractl, ra, false, req_count);
cf914a7d6   Rusty Russell   readahead: split ...
562
  }
fefa7c478   Matthew Wilcox (Oracle)   mm/readahead: add...
563
  EXPORT_SYMBOL_GPL(page_cache_sync_ra);
cf914a7d6   Rusty Russell   readahead: split ...
564

fefa7c478   Matthew Wilcox (Oracle)   mm/readahead: add...
565
566
567
  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 ...
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
  {
  	/* 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...
584
  	if (inode_read_congested(ractl->mapping->host))
cf914a7d6   Rusty Russell   readahead: split ...
585
  		return;
122a21d11   Fengguang Wu   readahead: on-dem...
586

ca47e8c72   Josef Bacik   mm: skip readahea...
587
588
  	if (blk_cgroup_congested())
  		return;
122a21d11   Fengguang Wu   readahead: on-dem...
589
  	/* do read-ahead */
fefa7c478   Matthew Wilcox (Oracle)   mm/readahead: add...
590
  	ondemand_readahead(ractl, ra, true, req_count);
122a21d11   Fengguang Wu   readahead: on-dem...
591
  }
fefa7c478   Matthew Wilcox (Oracle)   mm/readahead: add...
592
  EXPORT_SYMBOL_GPL(page_cache_async_ra);
782182e53   Cong Wang   mm: move readahea...
593

c7b95d515   Dominik Brodowski   mm: add ksys_read...
594
  ssize_t ksys_readahead(int fd, loff_t offset, size_t count)
782182e53   Cong Wang   mm: move readahea...
595
596
  {
  	ssize_t ret;
2903ff019   Al Viro   switch simple cas...
597
  	struct fd f;
782182e53   Cong Wang   mm: move readahea...
598
599
  
  	ret = -EBADF;
2903ff019   Al Viro   switch simple cas...
600
  	f = fdget(fd);
3d8f76153   Amir Goldstein   vfs: implement re...
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
  	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...
617
618
  	return ret;
  }
c7b95d515   Dominik Brodowski   mm: add ksys_read...
619
620
621
622
623
  
  SYSCALL_DEFINE3(readahead, int, fd, loff_t, offset, size_t, count)
  {
  	return ksys_readahead(fd, offset, count);
  }