1 // SPDX-License-Identifier: GPL-2.0-only
3 * mm/readahead.c - address_space-level file readahead.
5 * Copyright (C) 2002, Linus Torvalds
7 * 09Apr2002 Andrew Morton
12 * DOC: Readahead Overview
14 * Readahead is used to read content into the page cache before it is
15 * explicitly requested by the application. Readahead only ever
16 * attempts to read pages that are not yet in the page cache. If a
17 * page is present but not up-to-date, readahead will not try to read
18 * it. In that case a simple ->readpage() will be requested.
20 * Readahead is triggered when an application read request (whether a
21 * systemcall or a page fault) finds that the requested page is not in
22 * the page cache, or that it is in the page cache and has the
23 * %PG_readahead flag set. This flag indicates that the page was loaded
24 * as part of a previous read-ahead request and now that it has been
25 * accessed, it is time for the next read-ahead.
27 * Each readahead request is partly synchronous read, and partly async
28 * read-ahead. This is reflected in the struct file_ra_state which
29 * contains ->size being to total number of pages, and ->async_size
30 * which is the number of pages in the async section. The first page in
31 * this async section will have %PG_readahead set as a trigger for a
32 * subsequent read ahead. Once a series of sequential reads has been
33 * established, there should be no need for a synchronous component and
34 * all read ahead request will be fully asynchronous.
36 * When either of the triggers causes a readahead, three numbers need to
37 * be determined: the start of the region, the size of the region, and
38 * the size of the async tail.
40 * The start of the region is simply the first page address at or after
41 * the accessed address, which is not currently populated in the page
42 * cache. This is found with a simple search in the page cache.
44 * The size of the async tail is determined by subtracting the size that
45 * was explicitly requested from the determined request size, unless
46 * this would be less than zero - then zero is used. NOTE THIS
47 * CALCULATION IS WRONG WHEN THE START OF THE REGION IS NOT THE ACCESSED
50 * The size of the region is normally determined from the size of the
51 * previous readahead which loaded the preceding pages. This may be
52 * discovered from the struct file_ra_state for simple sequential reads,
53 * or from examining the state of the page cache when multiple
54 * sequential reads are interleaved. Specifically: where the readahead
55 * was triggered by the %PG_readahead flag, the size of the previous
56 * readahead is assumed to be the number of pages from the triggering
57 * page to the start of the new readahead. In these cases, the size of
58 * the previous readahead is scaled, often doubled, for the new
59 * readahead, though see get_next_ra_size() for details.
61 * If the size of the previous read cannot be determined, the number of
62 * preceding pages in the page cache is used to estimate the size of
63 * a previous read. This estimate could easily be misled by random
64 * reads being coincidentally adjacent, so it is ignored unless it is
65 * larger than the current request, and it is not scaled up, unless it
66 * is at the start of file.
68 * In general read ahead is accelerated at the start of the file, as
69 * reads from there are often sequential. There are other minor
70 * adjustments to the read ahead size in various special cases and these
71 * are best discovered by reading the code.
73 * The above calculation determines the readahead, to which any requested
74 * read size may be added.
76 * Readahead requests are sent to the filesystem using the ->readahead()
77 * address space operation, for which mpage_readahead() is a canonical
78 * implementation. ->readahead() should normally initiate reads on all
79 * pages, but may fail to read any or all pages without causing an IO
80 * error. The page cache reading code will issue a ->readpage() request
81 * for any page which ->readahead() does not provided, and only an error
82 * from this will be final.
84 * ->readahead() will generally call readahead_page() repeatedly to get
85 * each page from those prepared for read ahead. It may fail to read a
88 * * not calling readahead_page() sufficiently many times, effectively
89 * ignoring some pages, as might be appropriate if the path to
90 * storage is congested.
92 * * failing to actually submit a read request for a given page,
93 * possibly due to insufficient resources, or
95 * * getting an error during subsequent processing of a request.
97 * In the last two cases, the page should be unlocked to indicate that
98 * the read attempt has failed. In the first case the page will be
99 * unlocked by the caller.
101 * Those pages not in the final ``async_size`` of the request should be
102 * considered to be important and ->readahead() should not fail them due
103 * to congestion or temporary resource unavailability, but should wait
104 * for necessary resources (e.g. memory or indexing information) to
105 * become available. Pages in the final ``async_size`` may be
106 * considered less urgent and failure to read them is more acceptable.
107 * In this case it is best to use delete_from_page_cache() to remove the
108 * pages from the page cache as is automatically done for pages that
109 * were not fetched with readahead_page(). This will allow a
110 * subsequent synchronous read ahead request to try them again. If they
111 * are left in the page cache, then they will be read individually using
116 #include <linux/kernel.h>
117 #include <linux/dax.h>
118 #include <linux/gfp.h>
119 #include <linux/export.h>
120 #include <linux/backing-dev.h>
121 #include <linux/task_io_accounting_ops.h>
122 #include <linux/pagevec.h>
123 #include <linux/pagemap.h>
124 #include <linux/syscalls.h>
125 #include <linux/file.h>
126 #include <linux/mm_inline.h>
127 #include <linux/blk-cgroup.h>
128 #include <linux/fadvise.h>
129 #include <linux/sched/mm.h>
131 #include "internal.h"
134 * Initialise a struct file's readahead state. Assumes that the caller has
135 * memset *ra to zero.
138 file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping)
140 ra->ra_pages = inode_to_bdi(mapping->host)->ra_pages;
143 EXPORT_SYMBOL_GPL(file_ra_state_init);
146 * see if a page needs releasing upon read_cache_pages() failure
147 * - the caller of read_cache_pages() may have set PG_private or PG_fscache
148 * before calling, such as the NFS fs marking pages that are cached locally
149 * on disk, thus we need to give the fs a chance to clean up in the event of
152 static void read_cache_pages_invalidate_page(struct address_space *mapping,
155 if (page_has_private(page)) {
156 if (!trylock_page(page))
158 page->mapping = mapping;
159 folio_invalidate(page_folio(page), 0, PAGE_SIZE);
160 page->mapping = NULL;
167 * release a list of pages, invalidating them first if need be
169 static void read_cache_pages_invalidate_pages(struct address_space *mapping,
170 struct list_head *pages)
174 while (!list_empty(pages)) {
175 victim = lru_to_page(pages);
176 list_del(&victim->lru);
177 read_cache_pages_invalidate_page(mapping, victim);
182 * read_cache_pages - populate an address space with some pages & start reads against them
183 * @mapping: the address_space
184 * @pages: The address of a list_head which contains the target pages. These
185 * pages have their ->index populated and are otherwise uninitialised.
186 * @filler: callback routine for filling a single page.
187 * @data: private data for the callback routine.
189 * Hides the details of the LRU cache etc from the filesystems.
191 * Returns: %0 on success, error return by @filler otherwise
193 int read_cache_pages(struct address_space *mapping, struct list_head *pages,
194 int (*filler)(void *, struct page *), void *data)
199 while (!list_empty(pages)) {
200 page = lru_to_page(pages);
201 list_del(&page->lru);
202 if (add_to_page_cache_lru(page, mapping, page->index,
203 readahead_gfp_mask(mapping))) {
204 read_cache_pages_invalidate_page(mapping, page);
209 ret = filler(data, page);
211 read_cache_pages_invalidate_pages(mapping, pages);
214 task_io_account_read(PAGE_SIZE);
219 EXPORT_SYMBOL(read_cache_pages);
221 static void read_pages(struct readahead_control *rac, struct list_head *pages,
224 const struct address_space_operations *aops = rac->mapping->a_ops;
226 struct blk_plug plug;
228 if (!readahead_count(rac))
231 blk_start_plug(&plug);
233 if (aops->readahead) {
234 aops->readahead(rac);
236 * Clean up the remaining pages. The sizes in ->ra
237 * maybe be used to size next read-ahead, so make sure
238 * they accurately reflect what happened.
240 while ((page = readahead_page(rac))) {
242 if (rac->ra->async_size > 0) {
243 rac->ra->async_size -= 1;
244 delete_from_page_cache(page);
249 } else if (aops->readpages) {
250 aops->readpages(rac->file, rac->mapping, pages,
251 readahead_count(rac));
252 /* Clean up the remaining pages */
253 put_pages_list(pages);
254 rac->_index += rac->_nr_pages;
257 while ((page = readahead_page(rac))) {
258 aops->readpage(rac->file, page);
263 blk_finish_plug(&plug);
265 BUG_ON(pages && !list_empty(pages));
266 BUG_ON(readahead_count(rac));
274 * page_cache_ra_unbounded - Start unchecked readahead.
275 * @ractl: Readahead control.
276 * @nr_to_read: The number of pages to read.
277 * @lookahead_size: Where to start the next readahead.
279 * This function is for filesystems to call when they want to start
280 * readahead beyond a file's stated i_size. This is almost certainly
281 * not the function you want to call. Use page_cache_async_readahead()
282 * or page_cache_sync_readahead() instead.
284 * Context: File is referenced by caller. Mutexes may be held by caller.
285 * May sleep, but will not reenter filesystem to reclaim memory.
287 void page_cache_ra_unbounded(struct readahead_control *ractl,
288 unsigned long nr_to_read, unsigned long lookahead_size)
290 struct address_space *mapping = ractl->mapping;
291 unsigned long index = readahead_index(ractl);
292 LIST_HEAD(page_pool);
293 gfp_t gfp_mask = readahead_gfp_mask(mapping);
297 * Partway through the readahead operation, we will have added
298 * locked pages to the page cache, but will not yet have submitted
299 * them for I/O. Adding another page may need to allocate memory,
300 * which can trigger memory reclaim. Telling the VM we're in
301 * the middle of a filesystem operation will cause it to not
302 * touch file-backed pages, preventing a deadlock. Most (all?)
303 * filesystems already specify __GFP_NOFS in their mapping's
304 * gfp_mask, but let's be explicit here.
306 unsigned int nofs = memalloc_nofs_save();
308 filemap_invalidate_lock_shared(mapping);
310 * Preallocate as many pages as we will need.
312 for (i = 0; i < nr_to_read; i++) {
313 struct folio *folio = xa_load(&mapping->i_pages, index + i);
315 if (folio && !xa_is_value(folio)) {
317 * Page already present? Kick off the current batch
318 * of contiguous pages before continuing with the
319 * next batch. This page may be the one we would
320 * have intended to mark as Readahead, but we don't
321 * have a stable reference to this page, and it's
322 * not worth getting one just for that.
324 read_pages(ractl, &page_pool, true);
325 i = ractl->_index + ractl->_nr_pages - index - 1;
329 folio = filemap_alloc_folio(gfp_mask, 0);
332 if (mapping->a_ops->readpages) {
333 folio->index = index + i;
334 list_add(&folio->lru, &page_pool);
335 } else if (filemap_add_folio(mapping, folio, index + i,
338 read_pages(ractl, &page_pool, true);
339 i = ractl->_index + ractl->_nr_pages - index - 1;
342 if (i == nr_to_read - lookahead_size)
343 folio_set_readahead(folio);
348 * Now start the IO. We ignore I/O errors - if the page is not
349 * uptodate then the caller will launch readpage again, and
350 * will then handle the error.
352 read_pages(ractl, &page_pool, false);
353 filemap_invalidate_unlock_shared(mapping);
354 memalloc_nofs_restore(nofs);
356 EXPORT_SYMBOL_GPL(page_cache_ra_unbounded);
359 * do_page_cache_ra() actually reads a chunk of disk. It allocates
360 * the pages first, then submits them for I/O. This avoids the very bad
361 * behaviour which would occur if page allocations are causing VM writeback.
362 * We really don't want to intermingle reads and writes like that.
364 static void do_page_cache_ra(struct readahead_control *ractl,
365 unsigned long nr_to_read, unsigned long lookahead_size)
367 struct inode *inode = ractl->mapping->host;
368 unsigned long index = readahead_index(ractl);
369 loff_t isize = i_size_read(inode);
370 pgoff_t end_index; /* The last page we want to read */
375 end_index = (isize - 1) >> PAGE_SHIFT;
376 if (index > end_index)
378 /* Don't read past the page containing the last byte of the file */
379 if (nr_to_read > end_index - index)
380 nr_to_read = end_index - index + 1;
382 page_cache_ra_unbounded(ractl, nr_to_read, lookahead_size);
386 * Chunk the readahead into 2 megabyte units, so that we don't pin too much
389 void force_page_cache_ra(struct readahead_control *ractl,
390 unsigned long nr_to_read)
392 struct address_space *mapping = ractl->mapping;
393 struct file_ra_state *ra = ractl->ra;
394 struct backing_dev_info *bdi = inode_to_bdi(mapping->host);
395 unsigned long max_pages, index;
397 if (unlikely(!mapping->a_ops->readpage && !mapping->a_ops->readpages &&
398 !mapping->a_ops->readahead))
402 * If the request exceeds the readahead window, allow the read to
403 * be up to the optimal hardware IO size
405 index = readahead_index(ractl);
406 max_pages = max_t(unsigned long, bdi->io_pages, ra->ra_pages);
407 nr_to_read = min_t(unsigned long, nr_to_read, max_pages);
409 unsigned long this_chunk = (2 * 1024 * 1024) / PAGE_SIZE;
411 if (this_chunk > nr_to_read)
412 this_chunk = nr_to_read;
413 ractl->_index = index;
414 do_page_cache_ra(ractl, this_chunk, 0);
417 nr_to_read -= this_chunk;
422 * Set the initial window size, round to next power of 2 and square
423 * for small size, x 4 for medium, and x 2 for large
424 * for 128k (32 page) max ra
425 * 1-2 page = 16k, 3-4 page 32k, 5-8 page = 64k, > 8 page = 128k initial
427 static unsigned long get_init_ra_size(unsigned long size, unsigned long max)
429 unsigned long newsize = roundup_pow_of_two(size);
431 if (newsize <= max / 32)
432 newsize = newsize * 4;
433 else if (newsize <= max / 4)
434 newsize = newsize * 2;
442 * Get the previous window size, ramp it up, and
443 * return it as the new window size.
445 static unsigned long get_next_ra_size(struct file_ra_state *ra,
448 unsigned long cur = ra->size;
458 * On-demand readahead design.
460 * The fields in struct file_ra_state represent the most-recently-executed
463 * |<----- async_size ---------|
464 * |------------------- size -------------------->|
465 * |==================#===========================|
466 * ^start ^page marked with PG_readahead
468 * To overlap application thinking time and disk I/O time, we do
469 * `readahead pipelining': Do not wait until the application consumed all
470 * readahead pages and stalled on the missing page at readahead_index;
471 * Instead, submit an asynchronous readahead I/O as soon as there are
472 * only async_size pages left in the readahead window. Normally async_size
473 * will be equal to size, for maximum pipelining.
475 * In interleaved sequential reads, concurrent streams on the same fd can
476 * be invalidating each other's readahead state. So we flag the new readahead
477 * page at (start+size-async_size) with PG_readahead, and use it as readahead
478 * indicator. The flag won't be set on already cached pages, to avoid the
479 * readahead-for-nothing fuss, saving pointless page cache lookups.
481 * prev_pos tracks the last visited byte in the _previous_ read request.
482 * It should be maintained by the caller, and will be used for detecting
483 * small random reads. Note that the readahead algorithm checks loosely
484 * for sequential patterns. Hence interleaved reads might be served as
487 * There is a special-case: if the first page which the application tries to
488 * read happens to be the first page of the file, it is assumed that a linear
489 * read is about to happen and the window is immediately set to the initial size
490 * based on I/O request size and the max_readahead.
492 * The code ramps up the readahead size aggressively at first, but slow down as
493 * it approaches max_readhead.
497 * Count contiguously cached pages from @index-1 to @index-@max,
498 * this count is a conservative estimation of
499 * - length of the sequential read sequence, or
500 * - thrashing threshold in memory tight systems
502 static pgoff_t count_history_pages(struct address_space *mapping,
503 pgoff_t index, unsigned long max)
508 head = page_cache_prev_miss(mapping, index - 1, max);
511 return index - 1 - head;
515 * page cache context based read-ahead
517 static int try_context_readahead(struct address_space *mapping,
518 struct file_ra_state *ra,
520 unsigned long req_size,
525 size = count_history_pages(mapping, index, max);
528 * not enough history pages:
529 * it could be a random read
531 if (size <= req_size)
535 * starts from beginning of file:
536 * it is a strong indication of long-run stream (or whole-file-read)
542 ra->size = min(size + req_size, max);
549 * There are some parts of the kernel which assume that PMD entries
550 * are exactly HPAGE_PMD_ORDER. Those should be fixed, but until then,
551 * limit the maximum allocation order to PMD size. I'm not aware of any
552 * assumptions about maximum order if THP are disabled, but 8 seems like
553 * a good order (that's 1MB if you're using 4kB pages)
555 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
556 #define MAX_PAGECACHE_ORDER HPAGE_PMD_ORDER
558 #define MAX_PAGECACHE_ORDER 8
561 static inline int ra_alloc_folio(struct readahead_control *ractl, pgoff_t index,
562 pgoff_t mark, unsigned int order, gfp_t gfp)
565 struct folio *folio = filemap_alloc_folio(gfp, order);
569 if (mark - index < (1UL << order))
570 folio_set_readahead(folio);
571 err = filemap_add_folio(ractl->mapping, folio, index, gfp);
575 ractl->_nr_pages += 1UL << order;
579 void page_cache_ra_order(struct readahead_control *ractl,
580 struct file_ra_state *ra, unsigned int new_order)
582 struct address_space *mapping = ractl->mapping;
583 pgoff_t index = readahead_index(ractl);
584 pgoff_t limit = (i_size_read(mapping->host) - 1) >> PAGE_SHIFT;
585 pgoff_t mark = index + ra->size - ra->async_size;
587 gfp_t gfp = readahead_gfp_mask(mapping);
589 if (!mapping_large_folio_support(mapping) || ra->size < 4)
592 limit = min(limit, index + ra->size - 1);
594 if (new_order < MAX_PAGECACHE_ORDER) {
596 if (new_order > MAX_PAGECACHE_ORDER)
597 new_order = MAX_PAGECACHE_ORDER;
598 while ((1 << new_order) > ra->size)
602 while (index <= limit) {
603 unsigned int order = new_order;
605 /* Align with smaller pages if needed */
606 if (index & ((1UL << order) - 1)) {
607 order = __ffs(index);
611 /* Don't allocate pages past EOF */
612 while (index + (1UL << order) - 1 > limit) {
616 err = ra_alloc_folio(ractl, index, mark, order, gfp);
619 index += 1UL << order;
623 ra->size += index - limit - 1;
624 ra->async_size += index - limit - 1;
627 read_pages(ractl, NULL, false);
630 * If there were already pages in the page cache, then we may have
631 * left some gaps. Let the regular readahead code take care of this
637 do_page_cache_ra(ractl, ra->size, ra->async_size);
641 * A minimal readahead algorithm for trivial sequential/random reads.
643 static void ondemand_readahead(struct readahead_control *ractl,
644 struct folio *folio, unsigned long req_size)
646 struct backing_dev_info *bdi = inode_to_bdi(ractl->mapping->host);
647 struct file_ra_state *ra = ractl->ra;
648 unsigned long max_pages = ra->ra_pages;
649 unsigned long add_pages;
650 unsigned long index = readahead_index(ractl);
654 * If the request exceeds the readahead window, allow the read to
655 * be up to the optimal hardware IO size
657 if (req_size > max_pages && bdi->io_pages > max_pages)
658 max_pages = min(req_size, bdi->io_pages);
664 goto initial_readahead;
667 * It's the expected callback index, assume sequential access.
668 * Ramp up sizes, and push forward the readahead window.
670 if ((index == (ra->start + ra->size - ra->async_size) ||
671 index == (ra->start + ra->size))) {
672 ra->start += ra->size;
673 ra->size = get_next_ra_size(ra, max_pages);
674 ra->async_size = ra->size;
679 * Hit a marked folio without valid readahead state.
680 * E.g. interleaved reads.
681 * Query the pagecache for async_size, which normally equals to
682 * readahead size. Ramp it up and use it as the new readahead size.
688 start = page_cache_next_miss(ractl->mapping, index + 1,
692 if (!start || start - index > max_pages)
696 ra->size = start - index; /* old async_size */
697 ra->size += req_size;
698 ra->size = get_next_ra_size(ra, max_pages);
699 ra->async_size = ra->size;
706 if (req_size > max_pages)
707 goto initial_readahead;
710 * sequential cache miss
711 * trivial case: (index - prev_index) == 1
712 * unaligned reads: (index - prev_index) == 0
714 prev_index = (unsigned long long)ra->prev_pos >> PAGE_SHIFT;
715 if (index - prev_index <= 1UL)
716 goto initial_readahead;
719 * Query the page cache and look for the traces(cached history pages)
720 * that a sequential stream would leave behind.
722 if (try_context_readahead(ractl->mapping, ra, index, req_size,
727 * standalone, small random read
728 * Read as is, and do not pollute the readahead state.
730 do_page_cache_ra(ractl, req_size, 0);
735 ra->size = get_init_ra_size(req_size, max_pages);
736 ra->async_size = ra->size > req_size ? ra->size - req_size : ra->size;
740 * Will this read hit the readahead marker made by itself?
741 * If so, trigger the readahead marker hit now, and merge
742 * the resulted next readahead window into the current one.
743 * Take care of maximum IO pages as above.
745 if (index == ra->start && ra->size == ra->async_size) {
746 add_pages = get_next_ra_size(ra, max_pages);
747 if (ra->size + add_pages <= max_pages) {
748 ra->async_size = add_pages;
749 ra->size += add_pages;
751 ra->size = max_pages;
752 ra->async_size = max_pages >> 1;
756 ractl->_index = ra->start;
757 page_cache_ra_order(ractl, ra, folio ? folio_order(folio) : 0);
760 void page_cache_sync_ra(struct readahead_control *ractl,
761 unsigned long req_count)
763 bool do_forced_ra = ractl->file && (ractl->file->f_mode & FMODE_RANDOM);
766 * Even if read-ahead is disabled, issue this request as read-ahead
767 * as we'll need it to satisfy the requested range. The forced
768 * read-ahead will do the right thing and limit the read to just the
769 * requested range, which we'll set to 1 page for this case.
771 if (!ractl->ra->ra_pages || blk_cgroup_congested()) {
780 force_page_cache_ra(ractl, req_count);
785 ondemand_readahead(ractl, NULL, req_count);
787 EXPORT_SYMBOL_GPL(page_cache_sync_ra);
789 void page_cache_async_ra(struct readahead_control *ractl,
790 struct folio *folio, unsigned long req_count)
793 if (!ractl->ra->ra_pages)
797 * Same bit is used for PG_readahead and PG_reclaim.
799 if (folio_test_writeback(folio))
802 folio_clear_readahead(folio);
804 if (blk_cgroup_congested())
808 ondemand_readahead(ractl, folio, req_count);
810 EXPORT_SYMBOL_GPL(page_cache_async_ra);
812 ssize_t ksys_readahead(int fd, loff_t offset, size_t count)
819 if (!f.file || !(f.file->f_mode & FMODE_READ))
823 * The readahead() syscall is intended to run only on files
824 * that can execute readahead. If readahead is not possible
825 * on this file, then we must return -EINVAL.
828 if (!f.file->f_mapping || !f.file->f_mapping->a_ops ||
829 !S_ISREG(file_inode(f.file)->i_mode))
832 ret = vfs_fadvise(f.file, offset, count, POSIX_FADV_WILLNEED);
838 SYSCALL_DEFINE3(readahead, int, fd, loff_t, offset, size_t, count)
840 return ksys_readahead(fd, offset, count);
844 * readahead_expand - Expand a readahead request
845 * @ractl: The request to be expanded
846 * @new_start: The revised start
847 * @new_len: The revised size of the request
849 * Attempt to expand a readahead request outwards from the current size to the
850 * specified size by inserting locked pages before and after the current window
851 * to increase the size to the new window. This may involve the insertion of
852 * THPs, in which case the window may get expanded even beyond what was
855 * The algorithm will stop if it encounters a conflicting page already in the
856 * pagecache and leave a smaller expansion than requested.
858 * The caller must check for this by examining the revised @ractl object for a
859 * different expansion than was requested.
861 void readahead_expand(struct readahead_control *ractl,
862 loff_t new_start, size_t new_len)
864 struct address_space *mapping = ractl->mapping;
865 struct file_ra_state *ra = ractl->ra;
866 pgoff_t new_index, new_nr_pages;
867 gfp_t gfp_mask = readahead_gfp_mask(mapping);
869 new_index = new_start / PAGE_SIZE;
871 /* Expand the leading edge downwards */
872 while (ractl->_index > new_index) {
873 unsigned long index = ractl->_index - 1;
874 struct page *page = xa_load(&mapping->i_pages, index);
876 if (page && !xa_is_value(page))
877 return; /* Page apparently present */
879 page = __page_cache_alloc(gfp_mask);
882 if (add_to_page_cache_lru(page, mapping, index, gfp_mask) < 0) {
888 ractl->_index = page->index;
891 new_len += new_start - readahead_pos(ractl);
892 new_nr_pages = DIV_ROUND_UP(new_len, PAGE_SIZE);
894 /* Expand the trailing edge upwards */
895 while (ractl->_nr_pages < new_nr_pages) {
896 unsigned long index = ractl->_index + ractl->_nr_pages;
897 struct page *page = xa_load(&mapping->i_pages, index);
899 if (page && !xa_is_value(page))
900 return; /* Page apparently present */
902 page = __page_cache_alloc(gfp_mask);
905 if (add_to_page_cache_lru(page, mapping, index, gfp_mask) < 0) {
916 EXPORT_SYMBOL(readahead_expand);