2 * mm/readahead.c - address_space-level file readahead.
4 * Copyright (C) 2002, Linus Torvalds
6 * 09Apr2002 akpm@zip.com.au
10 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/blkdev.h>
15 #include <linux/backing-dev.h>
16 #include <linux/task_io_accounting_ops.h>
17 #include <linux/pagevec.h>
18 #include <linux/pagemap.h>
20 void default_unplug_io_fn(struct backing_dev_info
*bdi
, struct page
*page
)
23 EXPORT_SYMBOL(default_unplug_io_fn
);
26 * Convienent macros for min/max read-ahead pages.
27 * Note that MAX_RA_PAGES is rounded down, while MIN_RA_PAGES is rounded up.
28 * The latter is necessary for systems with large page size(i.e. 64k).
30 #define MAX_RA_PAGES (VM_MAX_READAHEAD*1024 / PAGE_CACHE_SIZE)
31 #define MIN_RA_PAGES DIV_ROUND_UP(VM_MIN_READAHEAD*1024, PAGE_CACHE_SIZE)
33 struct backing_dev_info default_backing_dev_info
= {
34 .ra_pages
= MAX_RA_PAGES
,
36 .capabilities
= BDI_CAP_MAP_COPY
,
37 .unplug_io_fn
= default_unplug_io_fn
,
39 EXPORT_SYMBOL_GPL(default_backing_dev_info
);
42 * Initialise a struct file's readahead state. Assumes that the caller has
46 file_ra_state_init(struct file_ra_state
*ra
, struct address_space
*mapping
)
48 ra
->ra_pages
= mapping
->backing_dev_info
->ra_pages
;
51 EXPORT_SYMBOL_GPL(file_ra_state_init
);
53 #define list_to_page(head) (list_entry((head)->prev, struct page, lru))
56 * read_cache_pages - populate an address space with some pages & start reads against them
57 * @mapping: the address_space
58 * @pages: The address of a list_head which contains the target pages. These
59 * pages have their ->index populated and are otherwise uninitialised.
60 * @filler: callback routine for filling a single page.
61 * @data: private data for the callback routine.
63 * Hides the details of the LRU cache etc from the filesystems.
65 int read_cache_pages(struct address_space
*mapping
, struct list_head
*pages
,
66 int (*filler
)(void *, struct page
*), void *data
)
69 struct pagevec lru_pvec
;
72 pagevec_init(&lru_pvec
, 0);
74 while (!list_empty(pages
)) {
75 page
= list_to_page(pages
);
77 if (add_to_page_cache(page
, mapping
, page
->index
, GFP_KERNEL
)) {
78 page_cache_release(page
);
81 ret
= filler(data
, page
);
82 if (!pagevec_add(&lru_pvec
, page
))
83 __pagevec_lru_add(&lru_pvec
);
85 put_pages_list(pages
);
88 task_io_account_read(PAGE_CACHE_SIZE
);
90 pagevec_lru_add(&lru_pvec
);
94 EXPORT_SYMBOL(read_cache_pages
);
96 static int read_pages(struct address_space
*mapping
, struct file
*filp
,
97 struct list_head
*pages
, unsigned nr_pages
)
100 struct pagevec lru_pvec
;
103 if (mapping
->a_ops
->readpages
) {
104 ret
= mapping
->a_ops
->readpages(filp
, mapping
, pages
, nr_pages
);
105 /* Clean up the remaining pages */
106 put_pages_list(pages
);
110 pagevec_init(&lru_pvec
, 0);
111 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
112 struct page
*page
= list_to_page(pages
);
113 list_del(&page
->lru
);
114 if (!add_to_page_cache(page
, mapping
,
115 page
->index
, GFP_KERNEL
)) {
116 mapping
->a_ops
->readpage(filp
, page
);
117 if (!pagevec_add(&lru_pvec
, page
))
118 __pagevec_lru_add(&lru_pvec
);
120 page_cache_release(page
);
122 pagevec_lru_add(&lru_pvec
);
129 * do_page_cache_readahead actually reads a chunk of disk. It allocates all
130 * the pages first, then submits them all for I/O. This avoids the very bad
131 * behaviour which would occur if page allocations are causing VM writeback.
132 * We really don't want to intermingle reads and writes like that.
134 * Returns the number of pages requested, or the maximum amount of I/O allowed.
136 * do_page_cache_readahead() returns -1 if it encountered request queue
140 __do_page_cache_readahead(struct address_space
*mapping
, struct file
*filp
,
141 pgoff_t offset
, unsigned long nr_to_read
,
142 unsigned long lookahead_size
)
144 struct inode
*inode
= mapping
->host
;
146 unsigned long end_index
; /* The last page we want to read */
147 LIST_HEAD(page_pool
);
150 loff_t isize
= i_size_read(inode
);
155 end_index
= ((isize
- 1) >> PAGE_CACHE_SHIFT
);
158 * Preallocate as many pages as we will need.
160 read_lock_irq(&mapping
->tree_lock
);
161 for (page_idx
= 0; page_idx
< nr_to_read
; page_idx
++) {
162 pgoff_t page_offset
= offset
+ page_idx
;
164 if (page_offset
> end_index
)
167 page
= radix_tree_lookup(&mapping
->page_tree
, page_offset
);
171 read_unlock_irq(&mapping
->tree_lock
);
172 page
= page_cache_alloc_cold(mapping
);
173 read_lock_irq(&mapping
->tree_lock
);
176 page
->index
= page_offset
;
177 list_add(&page
->lru
, &page_pool
);
178 if (page_idx
== nr_to_read
- lookahead_size
)
179 SetPageReadahead(page
);
182 read_unlock_irq(&mapping
->tree_lock
);
185 * Now start the IO. We ignore I/O errors - if the page is not
186 * uptodate then the caller will launch readpage again, and
187 * will then handle the error.
190 read_pages(mapping
, filp
, &page_pool
, ret
);
191 BUG_ON(!list_empty(&page_pool
));
197 * Chunk the readahead into 2 megabyte units, so that we don't pin too much
200 int force_page_cache_readahead(struct address_space
*mapping
, struct file
*filp
,
201 pgoff_t offset
, unsigned long nr_to_read
)
205 if (unlikely(!mapping
->a_ops
->readpage
&& !mapping
->a_ops
->readpages
))
211 unsigned long this_chunk
= (2 * 1024 * 1024) / PAGE_CACHE_SIZE
;
213 if (this_chunk
> nr_to_read
)
214 this_chunk
= nr_to_read
;
215 err
= __do_page_cache_readahead(mapping
, filp
,
216 offset
, this_chunk
, 0);
222 offset
+= this_chunk
;
223 nr_to_read
-= this_chunk
;
229 * This version skips the IO if the queue is read-congested, and will tell the
230 * block layer to abandon the readahead if request allocation would block.
232 * force_page_cache_readahead() will ignore queue congestion and will block on
235 int do_page_cache_readahead(struct address_space
*mapping
, struct file
*filp
,
236 pgoff_t offset
, unsigned long nr_to_read
)
238 if (bdi_read_congested(mapping
->backing_dev_info
))
241 return __do_page_cache_readahead(mapping
, filp
, offset
, nr_to_read
, 0);
245 * Given a desired number of PAGE_CACHE_SIZE readahead pages, return a
246 * sensible upper limit.
248 unsigned long max_sane_readahead(unsigned long nr
)
250 return min(nr
, (node_page_state(numa_node_id(), NR_INACTIVE
)
251 + node_page_state(numa_node_id(), NR_FREE_PAGES
)) / 2);
255 * Submit IO for the read-ahead request in file_ra_state.
257 static unsigned long ra_submit(struct file_ra_state
*ra
,
258 struct address_space
*mapping
, struct file
*filp
)
262 actual
= __do_page_cache_readahead(mapping
, filp
,
263 ra
->start
, ra
->size
, ra
->async_size
);
269 * Set the initial window size, round to next power of 2 and square
270 * for small size, x 4 for medium, and x 2 for large
271 * for 128k (32 page) max ra
272 * 1-8 page = 32k initial, > 8 page = 128k initial
274 static unsigned long get_init_ra_size(unsigned long size
, unsigned long max
)
276 unsigned long newsize
= roundup_pow_of_two(size
);
278 if (newsize
<= max
/ 32)
279 newsize
= newsize
* 4;
280 else if (newsize
<= max
/ 4)
281 newsize
= newsize
* 2;
289 * Get the previous window size, ramp it up, and
290 * return it as the new window size.
292 static unsigned long get_next_ra_size(struct file_ra_state
*ra
,
295 unsigned long cur
= ra
->size
;
296 unsigned long newsize
;
303 return min(newsize
, max
);
307 * On-demand readahead design.
309 * The fields in struct file_ra_state represent the most-recently-executed
312 * |<----- async_size ---------|
313 * |------------------- size -------------------->|
314 * |==================#===========================|
315 * ^start ^page marked with PG_readahead
317 * To overlap application thinking time and disk I/O time, we do
318 * `readahead pipelining': Do not wait until the application consumed all
319 * readahead pages and stalled on the missing page at readahead_index;
320 * Instead, submit an asynchronous readahead I/O as soon as there are
321 * only async_size pages left in the readahead window. Normally async_size
322 * will be equal to size, for maximum pipelining.
324 * In interleaved sequential reads, concurrent streams on the same fd can
325 * be invalidating each other's readahead state. So we flag the new readahead
326 * page at (start+size-async_size) with PG_readahead, and use it as readahead
327 * indicator. The flag won't be set on already cached pages, to avoid the
328 * readahead-for-nothing fuss, saving pointless page cache lookups.
330 * prev_index tracks the last visited page in the _previous_ read request.
331 * It should be maintained by the caller, and will be used for detecting
332 * small random reads. Note that the readahead algorithm checks loosely
333 * for sequential patterns. Hence interleaved reads might be served as
336 * There is a special-case: if the first page which the application tries to
337 * read happens to be the first page of the file, it is assumed that a linear
338 * read is about to happen and the window is immediately set to the initial size
339 * based on I/O request size and the max_readahead.
341 * The code ramps up the readahead size aggressively at first, but slow down as
342 * it approaches max_readhead.
346 * A minimal readahead algorithm for trivial sequential/random reads.
349 ondemand_readahead(struct address_space
*mapping
,
350 struct file_ra_state
*ra
, struct file
*filp
,
351 bool hit_readahead_marker
, pgoff_t offset
,
352 unsigned long req_size
)
354 unsigned long max
; /* max readahead pages */
358 sequential
= (offset
- ra
->prev_index
<= 1UL) || (req_size
> max
);
361 * It's the expected callback offset, assume sequential access.
362 * Ramp up sizes, and push forward the readahead window.
364 if (offset
&& (offset
== (ra
->start
+ ra
->size
- ra
->async_size
) ||
365 offset
== (ra
->start
+ ra
->size
))) {
366 ra
->start
+= ra
->size
;
367 ra
->size
= get_next_ra_size(ra
, max
);
368 ra
->async_size
= ra
->size
;
373 * Standalone, small read.
374 * Read as is, and do not pollute the readahead state.
376 if (!hit_readahead_marker
&& !sequential
) {
377 return __do_page_cache_readahead(mapping
, filp
,
378 offset
, req_size
, 0);
383 * - first read on start of file
384 * - sequential cache miss
385 * - oversize random read
386 * Start readahead for it.
389 ra
->size
= get_init_ra_size(req_size
, max
);
390 ra
->async_size
= ra
->size
> req_size
? ra
->size
- req_size
: ra
->size
;
393 * Hit on a marked page without valid readahead state.
394 * E.g. interleaved reads.
395 * Not knowing its readahead pos/size, bet on the minimal possible one.
397 if (hit_readahead_marker
) {
399 ra
->size
= get_next_ra_size(ra
, max
);
403 return ra_submit(ra
, mapping
, filp
);
407 * page_cache_sync_readahead - generic file readahead
408 * @mapping: address_space which holds the pagecache and I/O vectors
409 * @ra: file_ra_state which holds the readahead state
410 * @filp: passed on to ->readpage() and ->readpages()
411 * @offset: start offset into @mapping, in pagecache page-sized units
412 * @req_size: hint: total size of the read which the caller is performing in
415 * page_cache_sync_readahead() should be called when a cache miss happened:
416 * it will submit the read. The readahead logic may decide to piggyback more
417 * pages onto the read request if access patterns suggest it will improve
420 void page_cache_sync_readahead(struct address_space
*mapping
,
421 struct file_ra_state
*ra
, struct file
*filp
,
422 pgoff_t offset
, unsigned long req_size
)
429 ondemand_readahead(mapping
, ra
, filp
, false, offset
, req_size
);
431 EXPORT_SYMBOL_GPL(page_cache_sync_readahead
);
434 * page_cache_async_readahead - file readahead for marked pages
435 * @mapping: address_space which holds the pagecache and I/O vectors
436 * @ra: file_ra_state which holds the readahead state
437 * @filp: passed on to ->readpage() and ->readpages()
438 * @page: the page at @offset which has the PG_readahead flag set
439 * @offset: start offset into @mapping, in pagecache page-sized units
440 * @req_size: hint: total size of the read which the caller is performing in
443 * page_cache_async_ondemand() should be called when a page is used which
444 * has the PG_readahead flag: this is a marker to suggest that the application
445 * has used up enough of the readahead window that we should start pulling in
448 page_cache_async_readahead(struct address_space
*mapping
,
449 struct file_ra_state
*ra
, struct file
*filp
,
450 struct page
*page
, pgoff_t offset
,
451 unsigned long req_size
)
458 * Same bit is used for PG_readahead and PG_reclaim.
460 if (PageWriteback(page
))
463 ClearPageReadahead(page
);
466 * Defer asynchronous read-ahead on IO congestion.
468 if (bdi_read_congested(mapping
->backing_dev_info
))
472 ondemand_readahead(mapping
, ra
, filp
, true, offset
, req_size
);
474 EXPORT_SYMBOL_GPL(page_cache_async_readahead
);