162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * mm/readahead.c - address_space-level file readahead. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2002, Linus Torvalds 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * 09Apr2002 Andrew Morton 862306a36Sopenharmony_ci * Initial version. 962306a36Sopenharmony_ci */ 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci/** 1262306a36Sopenharmony_ci * DOC: Readahead Overview 1362306a36Sopenharmony_ci * 1462306a36Sopenharmony_ci * Readahead is used to read content into the page cache before it is 1562306a36Sopenharmony_ci * explicitly requested by the application. Readahead only ever 1662306a36Sopenharmony_ci * attempts to read folios that are not yet in the page cache. If a 1762306a36Sopenharmony_ci * folio is present but not up-to-date, readahead will not try to read 1862306a36Sopenharmony_ci * it. In that case a simple ->read_folio() will be requested. 1962306a36Sopenharmony_ci * 2062306a36Sopenharmony_ci * Readahead is triggered when an application read request (whether a 2162306a36Sopenharmony_ci * system call or a page fault) finds that the requested folio is not in 2262306a36Sopenharmony_ci * the page cache, or that it is in the page cache and has the 2362306a36Sopenharmony_ci * readahead flag set. This flag indicates that the folio was read 2462306a36Sopenharmony_ci * as part of a previous readahead request and now that it has been 2562306a36Sopenharmony_ci * accessed, it is time for the next readahead. 2662306a36Sopenharmony_ci * 2762306a36Sopenharmony_ci * Each readahead request is partly synchronous read, and partly async 2862306a36Sopenharmony_ci * readahead. This is reflected in the struct file_ra_state which 2962306a36Sopenharmony_ci * contains ->size being the total number of pages, and ->async_size 3062306a36Sopenharmony_ci * which is the number of pages in the async section. The readahead 3162306a36Sopenharmony_ci * flag will be set on the first folio in this async section to trigger 3262306a36Sopenharmony_ci * a subsequent readahead. Once a series of sequential reads has been 3362306a36Sopenharmony_ci * established, there should be no need for a synchronous component and 3462306a36Sopenharmony_ci * all readahead request will be fully asynchronous. 3562306a36Sopenharmony_ci * 3662306a36Sopenharmony_ci * When either of the triggers causes a readahead, three numbers need 3762306a36Sopenharmony_ci * to be determined: the start of the region to read, the size of the 3862306a36Sopenharmony_ci * region, and the size of the async tail. 3962306a36Sopenharmony_ci * 4062306a36Sopenharmony_ci * The start of the region is simply the first page address at or after 4162306a36Sopenharmony_ci * the accessed address, which is not currently populated in the page 4262306a36Sopenharmony_ci * cache. This is found with a simple search in the page cache. 4362306a36Sopenharmony_ci * 4462306a36Sopenharmony_ci * The size of the async tail is determined by subtracting the size that 4562306a36Sopenharmony_ci * was explicitly requested from the determined request size, unless 4662306a36Sopenharmony_ci * this would be less than zero - then zero is used. NOTE THIS 4762306a36Sopenharmony_ci * CALCULATION IS WRONG WHEN THE START OF THE REGION IS NOT THE ACCESSED 4862306a36Sopenharmony_ci * PAGE. ALSO THIS CALCULATION IS NOT USED CONSISTENTLY. 4962306a36Sopenharmony_ci * 5062306a36Sopenharmony_ci * The size of the region is normally determined from the size of the 5162306a36Sopenharmony_ci * previous readahead which loaded the preceding pages. This may be 5262306a36Sopenharmony_ci * discovered from the struct file_ra_state for simple sequential reads, 5362306a36Sopenharmony_ci * or from examining the state of the page cache when multiple 5462306a36Sopenharmony_ci * sequential reads are interleaved. Specifically: where the readahead 5562306a36Sopenharmony_ci * was triggered by the readahead flag, the size of the previous 5662306a36Sopenharmony_ci * readahead is assumed to be the number of pages from the triggering 5762306a36Sopenharmony_ci * page to the start of the new readahead. In these cases, the size of 5862306a36Sopenharmony_ci * the previous readahead is scaled, often doubled, for the new 5962306a36Sopenharmony_ci * readahead, though see get_next_ra_size() for details. 6062306a36Sopenharmony_ci * 6162306a36Sopenharmony_ci * If the size of the previous read cannot be determined, the number of 6262306a36Sopenharmony_ci * preceding pages in the page cache is used to estimate the size of 6362306a36Sopenharmony_ci * a previous read. This estimate could easily be misled by random 6462306a36Sopenharmony_ci * reads being coincidentally adjacent, so it is ignored unless it is 6562306a36Sopenharmony_ci * larger than the current request, and it is not scaled up, unless it 6662306a36Sopenharmony_ci * is at the start of file. 6762306a36Sopenharmony_ci * 6862306a36Sopenharmony_ci * In general readahead is accelerated at the start of the file, as 6962306a36Sopenharmony_ci * reads from there are often sequential. There are other minor 7062306a36Sopenharmony_ci * adjustments to the readahead size in various special cases and these 7162306a36Sopenharmony_ci * are best discovered by reading the code. 7262306a36Sopenharmony_ci * 7362306a36Sopenharmony_ci * The above calculation, based on the previous readahead size, 7462306a36Sopenharmony_ci * determines the size of the readahead, to which any requested read 7562306a36Sopenharmony_ci * size may be added. 7662306a36Sopenharmony_ci * 7762306a36Sopenharmony_ci * Readahead requests are sent to the filesystem using the ->readahead() 7862306a36Sopenharmony_ci * address space operation, for which mpage_readahead() is a canonical 7962306a36Sopenharmony_ci * implementation. ->readahead() should normally initiate reads on all 8062306a36Sopenharmony_ci * folios, but may fail to read any or all folios without causing an I/O 8162306a36Sopenharmony_ci * error. The page cache reading code will issue a ->read_folio() request 8262306a36Sopenharmony_ci * for any folio which ->readahead() did not read, and only an error 8362306a36Sopenharmony_ci * from this will be final. 8462306a36Sopenharmony_ci * 8562306a36Sopenharmony_ci * ->readahead() will generally call readahead_folio() repeatedly to get 8662306a36Sopenharmony_ci * each folio from those prepared for readahead. It may fail to read a 8762306a36Sopenharmony_ci * folio by: 8862306a36Sopenharmony_ci * 8962306a36Sopenharmony_ci * * not calling readahead_folio() sufficiently many times, effectively 9062306a36Sopenharmony_ci * ignoring some folios, as might be appropriate if the path to 9162306a36Sopenharmony_ci * storage is congested. 9262306a36Sopenharmony_ci * 9362306a36Sopenharmony_ci * * failing to actually submit a read request for a given folio, 9462306a36Sopenharmony_ci * possibly due to insufficient resources, or 9562306a36Sopenharmony_ci * 9662306a36Sopenharmony_ci * * getting an error during subsequent processing of a request. 9762306a36Sopenharmony_ci * 9862306a36Sopenharmony_ci * In the last two cases, the folio should be unlocked by the filesystem 9962306a36Sopenharmony_ci * to indicate that the read attempt has failed. In the first case the 10062306a36Sopenharmony_ci * folio will be unlocked by the VFS. 10162306a36Sopenharmony_ci * 10262306a36Sopenharmony_ci * Those folios not in the final ``async_size`` of the request should be 10362306a36Sopenharmony_ci * considered to be important and ->readahead() should not fail them due 10462306a36Sopenharmony_ci * to congestion or temporary resource unavailability, but should wait 10562306a36Sopenharmony_ci * for necessary resources (e.g. memory or indexing information) to 10662306a36Sopenharmony_ci * become available. Folios in the final ``async_size`` may be 10762306a36Sopenharmony_ci * considered less urgent and failure to read them is more acceptable. 10862306a36Sopenharmony_ci * In this case it is best to use filemap_remove_folio() to remove the 10962306a36Sopenharmony_ci * folios from the page cache as is automatically done for folios that 11062306a36Sopenharmony_ci * were not fetched with readahead_folio(). This will allow a 11162306a36Sopenharmony_ci * subsequent synchronous readahead request to try them again. If they 11262306a36Sopenharmony_ci * are left in the page cache, then they will be read individually using 11362306a36Sopenharmony_ci * ->read_folio() which may be less efficient. 11462306a36Sopenharmony_ci */ 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci#include <linux/blkdev.h> 11762306a36Sopenharmony_ci#include <linux/kernel.h> 11862306a36Sopenharmony_ci#include <linux/dax.h> 11962306a36Sopenharmony_ci#include <linux/gfp.h> 12062306a36Sopenharmony_ci#include <linux/export.h> 12162306a36Sopenharmony_ci#include <linux/backing-dev.h> 12262306a36Sopenharmony_ci#include <linux/task_io_accounting_ops.h> 12362306a36Sopenharmony_ci#include <linux/pagemap.h> 12462306a36Sopenharmony_ci#include <linux/psi.h> 12562306a36Sopenharmony_ci#include <linux/syscalls.h> 12662306a36Sopenharmony_ci#include <linux/file.h> 12762306a36Sopenharmony_ci#include <linux/mm_inline.h> 12862306a36Sopenharmony_ci#include <linux/blk-cgroup.h> 12962306a36Sopenharmony_ci#include <linux/fadvise.h> 13062306a36Sopenharmony_ci#include <linux/sched/mm.h> 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci#include "internal.h" 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci/* 13562306a36Sopenharmony_ci * Initialise a struct file's readahead state. Assumes that the caller has 13662306a36Sopenharmony_ci * memset *ra to zero. 13762306a36Sopenharmony_ci */ 13862306a36Sopenharmony_civoid 13962306a36Sopenharmony_cifile_ra_state_init(struct file_ra_state *ra, struct address_space *mapping) 14062306a36Sopenharmony_ci{ 14162306a36Sopenharmony_ci ra->ra_pages = inode_to_bdi(mapping->host)->ra_pages; 14262306a36Sopenharmony_ci ra->prev_pos = -1; 14362306a36Sopenharmony_ci} 14462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(file_ra_state_init); 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_cistatic void read_pages(struct readahead_control *rac) 14762306a36Sopenharmony_ci{ 14862306a36Sopenharmony_ci const struct address_space_operations *aops = rac->mapping->a_ops; 14962306a36Sopenharmony_ci struct folio *folio; 15062306a36Sopenharmony_ci struct blk_plug plug; 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci if (!readahead_count(rac)) 15362306a36Sopenharmony_ci return; 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci if (unlikely(rac->_workingset)) 15662306a36Sopenharmony_ci psi_memstall_enter(&rac->_pflags); 15762306a36Sopenharmony_ci blk_start_plug(&plug); 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci if (aops->readahead) { 16062306a36Sopenharmony_ci aops->readahead(rac); 16162306a36Sopenharmony_ci /* 16262306a36Sopenharmony_ci * Clean up the remaining folios. The sizes in ->ra 16362306a36Sopenharmony_ci * may be used to size the next readahead, so make sure 16462306a36Sopenharmony_ci * they accurately reflect what happened. 16562306a36Sopenharmony_ci */ 16662306a36Sopenharmony_ci while ((folio = readahead_folio(rac)) != NULL) { 16762306a36Sopenharmony_ci unsigned long nr = folio_nr_pages(folio); 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ci folio_get(folio); 17062306a36Sopenharmony_ci rac->ra->size -= nr; 17162306a36Sopenharmony_ci if (rac->ra->async_size >= nr) { 17262306a36Sopenharmony_ci rac->ra->async_size -= nr; 17362306a36Sopenharmony_ci filemap_remove_folio(folio); 17462306a36Sopenharmony_ci } 17562306a36Sopenharmony_ci folio_unlock(folio); 17662306a36Sopenharmony_ci folio_put(folio); 17762306a36Sopenharmony_ci } 17862306a36Sopenharmony_ci } else { 17962306a36Sopenharmony_ci while ((folio = readahead_folio(rac)) != NULL) 18062306a36Sopenharmony_ci aops->read_folio(rac->file, folio); 18162306a36Sopenharmony_ci } 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_ci blk_finish_plug(&plug); 18462306a36Sopenharmony_ci if (unlikely(rac->_workingset)) 18562306a36Sopenharmony_ci psi_memstall_leave(&rac->_pflags); 18662306a36Sopenharmony_ci rac->_workingset = false; 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ci BUG_ON(readahead_count(rac)); 18962306a36Sopenharmony_ci} 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ci/** 19262306a36Sopenharmony_ci * page_cache_ra_unbounded - Start unchecked readahead. 19362306a36Sopenharmony_ci * @ractl: Readahead control. 19462306a36Sopenharmony_ci * @nr_to_read: The number of pages to read. 19562306a36Sopenharmony_ci * @lookahead_size: Where to start the next readahead. 19662306a36Sopenharmony_ci * 19762306a36Sopenharmony_ci * This function is for filesystems to call when they want to start 19862306a36Sopenharmony_ci * readahead beyond a file's stated i_size. This is almost certainly 19962306a36Sopenharmony_ci * not the function you want to call. Use page_cache_async_readahead() 20062306a36Sopenharmony_ci * or page_cache_sync_readahead() instead. 20162306a36Sopenharmony_ci * 20262306a36Sopenharmony_ci * Context: File is referenced by caller. Mutexes may be held by caller. 20362306a36Sopenharmony_ci * May sleep, but will not reenter filesystem to reclaim memory. 20462306a36Sopenharmony_ci */ 20562306a36Sopenharmony_civoid page_cache_ra_unbounded(struct readahead_control *ractl, 20662306a36Sopenharmony_ci unsigned long nr_to_read, unsigned long lookahead_size) 20762306a36Sopenharmony_ci{ 20862306a36Sopenharmony_ci struct address_space *mapping = ractl->mapping; 20962306a36Sopenharmony_ci unsigned long index = readahead_index(ractl); 21062306a36Sopenharmony_ci gfp_t gfp_mask = readahead_gfp_mask(mapping); 21162306a36Sopenharmony_ci unsigned long i; 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ci /* 21462306a36Sopenharmony_ci * Partway through the readahead operation, we will have added 21562306a36Sopenharmony_ci * locked pages to the page cache, but will not yet have submitted 21662306a36Sopenharmony_ci * them for I/O. Adding another page may need to allocate memory, 21762306a36Sopenharmony_ci * which can trigger memory reclaim. Telling the VM we're in 21862306a36Sopenharmony_ci * the middle of a filesystem operation will cause it to not 21962306a36Sopenharmony_ci * touch file-backed pages, preventing a deadlock. Most (all?) 22062306a36Sopenharmony_ci * filesystems already specify __GFP_NOFS in their mapping's 22162306a36Sopenharmony_ci * gfp_mask, but let's be explicit here. 22262306a36Sopenharmony_ci */ 22362306a36Sopenharmony_ci unsigned int nofs = memalloc_nofs_save(); 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci filemap_invalidate_lock_shared(mapping); 22662306a36Sopenharmony_ci /* 22762306a36Sopenharmony_ci * Preallocate as many pages as we will need. 22862306a36Sopenharmony_ci */ 22962306a36Sopenharmony_ci for (i = 0; i < nr_to_read; i++) { 23062306a36Sopenharmony_ci struct folio *folio = xa_load(&mapping->i_pages, index + i); 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci if (folio && !xa_is_value(folio)) { 23362306a36Sopenharmony_ci /* 23462306a36Sopenharmony_ci * Page already present? Kick off the current batch 23562306a36Sopenharmony_ci * of contiguous pages before continuing with the 23662306a36Sopenharmony_ci * next batch. This page may be the one we would 23762306a36Sopenharmony_ci * have intended to mark as Readahead, but we don't 23862306a36Sopenharmony_ci * have a stable reference to this page, and it's 23962306a36Sopenharmony_ci * not worth getting one just for that. 24062306a36Sopenharmony_ci */ 24162306a36Sopenharmony_ci read_pages(ractl); 24262306a36Sopenharmony_ci ractl->_index++; 24362306a36Sopenharmony_ci i = ractl->_index + ractl->_nr_pages - index - 1; 24462306a36Sopenharmony_ci continue; 24562306a36Sopenharmony_ci } 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_ci folio = filemap_alloc_folio(gfp_mask, 0); 24862306a36Sopenharmony_ci if (!folio) 24962306a36Sopenharmony_ci break; 25062306a36Sopenharmony_ci if (filemap_add_folio(mapping, folio, index + i, 25162306a36Sopenharmony_ci gfp_mask) < 0) { 25262306a36Sopenharmony_ci folio_put(folio); 25362306a36Sopenharmony_ci read_pages(ractl); 25462306a36Sopenharmony_ci ractl->_index++; 25562306a36Sopenharmony_ci i = ractl->_index + ractl->_nr_pages - index - 1; 25662306a36Sopenharmony_ci continue; 25762306a36Sopenharmony_ci } 25862306a36Sopenharmony_ci if (i == nr_to_read - lookahead_size) 25962306a36Sopenharmony_ci folio_set_readahead(folio); 26062306a36Sopenharmony_ci ractl->_workingset |= folio_test_workingset(folio); 26162306a36Sopenharmony_ci ractl->_nr_pages++; 26262306a36Sopenharmony_ci } 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci /* 26562306a36Sopenharmony_ci * Now start the IO. We ignore I/O errors - if the folio is not 26662306a36Sopenharmony_ci * uptodate then the caller will launch read_folio again, and 26762306a36Sopenharmony_ci * will then handle the error. 26862306a36Sopenharmony_ci */ 26962306a36Sopenharmony_ci read_pages(ractl); 27062306a36Sopenharmony_ci filemap_invalidate_unlock_shared(mapping); 27162306a36Sopenharmony_ci memalloc_nofs_restore(nofs); 27262306a36Sopenharmony_ci} 27362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(page_cache_ra_unbounded); 27462306a36Sopenharmony_ci 27562306a36Sopenharmony_ci/* 27662306a36Sopenharmony_ci * do_page_cache_ra() actually reads a chunk of disk. It allocates 27762306a36Sopenharmony_ci * the pages first, then submits them for I/O. This avoids the very bad 27862306a36Sopenharmony_ci * behaviour which would occur if page allocations are causing VM writeback. 27962306a36Sopenharmony_ci * We really don't want to intermingle reads and writes like that. 28062306a36Sopenharmony_ci */ 28162306a36Sopenharmony_cistatic void do_page_cache_ra(struct readahead_control *ractl, 28262306a36Sopenharmony_ci unsigned long nr_to_read, unsigned long lookahead_size) 28362306a36Sopenharmony_ci{ 28462306a36Sopenharmony_ci struct inode *inode = ractl->mapping->host; 28562306a36Sopenharmony_ci unsigned long index = readahead_index(ractl); 28662306a36Sopenharmony_ci loff_t isize = i_size_read(inode); 28762306a36Sopenharmony_ci pgoff_t end_index; /* The last page we want to read */ 28862306a36Sopenharmony_ci 28962306a36Sopenharmony_ci if (isize == 0) 29062306a36Sopenharmony_ci return; 29162306a36Sopenharmony_ci 29262306a36Sopenharmony_ci end_index = (isize - 1) >> PAGE_SHIFT; 29362306a36Sopenharmony_ci if (index > end_index) 29462306a36Sopenharmony_ci return; 29562306a36Sopenharmony_ci /* Don't read past the page containing the last byte of the file */ 29662306a36Sopenharmony_ci if (nr_to_read > end_index - index) 29762306a36Sopenharmony_ci nr_to_read = end_index - index + 1; 29862306a36Sopenharmony_ci 29962306a36Sopenharmony_ci page_cache_ra_unbounded(ractl, nr_to_read, lookahead_size); 30062306a36Sopenharmony_ci} 30162306a36Sopenharmony_ci 30262306a36Sopenharmony_ci/* 30362306a36Sopenharmony_ci * Chunk the readahead into 2 megabyte units, so that we don't pin too much 30462306a36Sopenharmony_ci * memory at once. 30562306a36Sopenharmony_ci */ 30662306a36Sopenharmony_civoid force_page_cache_ra(struct readahead_control *ractl, 30762306a36Sopenharmony_ci unsigned long nr_to_read) 30862306a36Sopenharmony_ci{ 30962306a36Sopenharmony_ci struct address_space *mapping = ractl->mapping; 31062306a36Sopenharmony_ci struct file_ra_state *ra = ractl->ra; 31162306a36Sopenharmony_ci struct backing_dev_info *bdi = inode_to_bdi(mapping->host); 31262306a36Sopenharmony_ci unsigned long max_pages, index; 31362306a36Sopenharmony_ci 31462306a36Sopenharmony_ci if (unlikely(!mapping->a_ops->read_folio && !mapping->a_ops->readahead)) 31562306a36Sopenharmony_ci return; 31662306a36Sopenharmony_ci 31762306a36Sopenharmony_ci /* 31862306a36Sopenharmony_ci * If the request exceeds the readahead window, allow the read to 31962306a36Sopenharmony_ci * be up to the optimal hardware IO size 32062306a36Sopenharmony_ci */ 32162306a36Sopenharmony_ci index = readahead_index(ractl); 32262306a36Sopenharmony_ci max_pages = max_t(unsigned long, bdi->io_pages, ra->ra_pages); 32362306a36Sopenharmony_ci nr_to_read = min_t(unsigned long, nr_to_read, max_pages); 32462306a36Sopenharmony_ci while (nr_to_read) { 32562306a36Sopenharmony_ci unsigned long this_chunk = (2 * 1024 * 1024) / PAGE_SIZE; 32662306a36Sopenharmony_ci 32762306a36Sopenharmony_ci if (this_chunk > nr_to_read) 32862306a36Sopenharmony_ci this_chunk = nr_to_read; 32962306a36Sopenharmony_ci ractl->_index = index; 33062306a36Sopenharmony_ci do_page_cache_ra(ractl, this_chunk, 0); 33162306a36Sopenharmony_ci 33262306a36Sopenharmony_ci index += this_chunk; 33362306a36Sopenharmony_ci nr_to_read -= this_chunk; 33462306a36Sopenharmony_ci } 33562306a36Sopenharmony_ci} 33662306a36Sopenharmony_ci 33762306a36Sopenharmony_ci/* 33862306a36Sopenharmony_ci * Set the initial window size, round to next power of 2 and square 33962306a36Sopenharmony_ci * for small size, x 4 for medium, and x 2 for large 34062306a36Sopenharmony_ci * for 128k (32 page) max ra 34162306a36Sopenharmony_ci * 1-2 page = 16k, 3-4 page 32k, 5-8 page = 64k, > 8 page = 128k initial 34262306a36Sopenharmony_ci */ 34362306a36Sopenharmony_cistatic unsigned long get_init_ra_size(unsigned long size, unsigned long max) 34462306a36Sopenharmony_ci{ 34562306a36Sopenharmony_ci unsigned long newsize = roundup_pow_of_two(size); 34662306a36Sopenharmony_ci 34762306a36Sopenharmony_ci if (newsize <= max / 32) 34862306a36Sopenharmony_ci newsize = newsize * 4; 34962306a36Sopenharmony_ci else if (newsize <= max / 4) 35062306a36Sopenharmony_ci newsize = newsize * 2; 35162306a36Sopenharmony_ci else 35262306a36Sopenharmony_ci newsize = max; 35362306a36Sopenharmony_ci 35462306a36Sopenharmony_ci return newsize; 35562306a36Sopenharmony_ci} 35662306a36Sopenharmony_ci 35762306a36Sopenharmony_ci/* 35862306a36Sopenharmony_ci * Get the previous window size, ramp it up, and 35962306a36Sopenharmony_ci * return it as the new window size. 36062306a36Sopenharmony_ci */ 36162306a36Sopenharmony_cistatic unsigned long get_next_ra_size(struct file_ra_state *ra, 36262306a36Sopenharmony_ci unsigned long max) 36362306a36Sopenharmony_ci{ 36462306a36Sopenharmony_ci unsigned long cur = ra->size; 36562306a36Sopenharmony_ci 36662306a36Sopenharmony_ci if (cur < max / 16) 36762306a36Sopenharmony_ci return 4 * cur; 36862306a36Sopenharmony_ci if (cur <= max / 2) 36962306a36Sopenharmony_ci return 2 * cur; 37062306a36Sopenharmony_ci return max; 37162306a36Sopenharmony_ci} 37262306a36Sopenharmony_ci 37362306a36Sopenharmony_ci/* 37462306a36Sopenharmony_ci * On-demand readahead design. 37562306a36Sopenharmony_ci * 37662306a36Sopenharmony_ci * The fields in struct file_ra_state represent the most-recently-executed 37762306a36Sopenharmony_ci * readahead attempt: 37862306a36Sopenharmony_ci * 37962306a36Sopenharmony_ci * |<----- async_size ---------| 38062306a36Sopenharmony_ci * |------------------- size -------------------->| 38162306a36Sopenharmony_ci * |==================#===========================| 38262306a36Sopenharmony_ci * ^start ^page marked with PG_readahead 38362306a36Sopenharmony_ci * 38462306a36Sopenharmony_ci * To overlap application thinking time and disk I/O time, we do 38562306a36Sopenharmony_ci * `readahead pipelining': Do not wait until the application consumed all 38662306a36Sopenharmony_ci * readahead pages and stalled on the missing page at readahead_index; 38762306a36Sopenharmony_ci * Instead, submit an asynchronous readahead I/O as soon as there are 38862306a36Sopenharmony_ci * only async_size pages left in the readahead window. Normally async_size 38962306a36Sopenharmony_ci * will be equal to size, for maximum pipelining. 39062306a36Sopenharmony_ci * 39162306a36Sopenharmony_ci * In interleaved sequential reads, concurrent streams on the same fd can 39262306a36Sopenharmony_ci * be invalidating each other's readahead state. So we flag the new readahead 39362306a36Sopenharmony_ci * page at (start+size-async_size) with PG_readahead, and use it as readahead 39462306a36Sopenharmony_ci * indicator. The flag won't be set on already cached pages, to avoid the 39562306a36Sopenharmony_ci * readahead-for-nothing fuss, saving pointless page cache lookups. 39662306a36Sopenharmony_ci * 39762306a36Sopenharmony_ci * prev_pos tracks the last visited byte in the _previous_ read request. 39862306a36Sopenharmony_ci * It should be maintained by the caller, and will be used for detecting 39962306a36Sopenharmony_ci * small random reads. Note that the readahead algorithm checks loosely 40062306a36Sopenharmony_ci * for sequential patterns. Hence interleaved reads might be served as 40162306a36Sopenharmony_ci * sequential ones. 40262306a36Sopenharmony_ci * 40362306a36Sopenharmony_ci * There is a special-case: if the first page which the application tries to 40462306a36Sopenharmony_ci * read happens to be the first page of the file, it is assumed that a linear 40562306a36Sopenharmony_ci * read is about to happen and the window is immediately set to the initial size 40662306a36Sopenharmony_ci * based on I/O request size and the max_readahead. 40762306a36Sopenharmony_ci * 40862306a36Sopenharmony_ci * The code ramps up the readahead size aggressively at first, but slow down as 40962306a36Sopenharmony_ci * it approaches max_readhead. 41062306a36Sopenharmony_ci */ 41162306a36Sopenharmony_ci 41262306a36Sopenharmony_ci/* 41362306a36Sopenharmony_ci * Count contiguously cached pages from @index-1 to @index-@max, 41462306a36Sopenharmony_ci * this count is a conservative estimation of 41562306a36Sopenharmony_ci * - length of the sequential read sequence, or 41662306a36Sopenharmony_ci * - thrashing threshold in memory tight systems 41762306a36Sopenharmony_ci */ 41862306a36Sopenharmony_cistatic pgoff_t count_history_pages(struct address_space *mapping, 41962306a36Sopenharmony_ci pgoff_t index, unsigned long max) 42062306a36Sopenharmony_ci{ 42162306a36Sopenharmony_ci pgoff_t head; 42262306a36Sopenharmony_ci 42362306a36Sopenharmony_ci rcu_read_lock(); 42462306a36Sopenharmony_ci head = page_cache_prev_miss(mapping, index - 1, max); 42562306a36Sopenharmony_ci rcu_read_unlock(); 42662306a36Sopenharmony_ci 42762306a36Sopenharmony_ci return index - 1 - head; 42862306a36Sopenharmony_ci} 42962306a36Sopenharmony_ci 43062306a36Sopenharmony_ci/* 43162306a36Sopenharmony_ci * page cache context based readahead 43262306a36Sopenharmony_ci */ 43362306a36Sopenharmony_cistatic int try_context_readahead(struct address_space *mapping, 43462306a36Sopenharmony_ci struct file_ra_state *ra, 43562306a36Sopenharmony_ci pgoff_t index, 43662306a36Sopenharmony_ci unsigned long req_size, 43762306a36Sopenharmony_ci unsigned long max) 43862306a36Sopenharmony_ci{ 43962306a36Sopenharmony_ci pgoff_t size; 44062306a36Sopenharmony_ci 44162306a36Sopenharmony_ci size = count_history_pages(mapping, index, max); 44262306a36Sopenharmony_ci 44362306a36Sopenharmony_ci /* 44462306a36Sopenharmony_ci * not enough history pages: 44562306a36Sopenharmony_ci * it could be a random read 44662306a36Sopenharmony_ci */ 44762306a36Sopenharmony_ci if (size <= req_size) 44862306a36Sopenharmony_ci return 0; 44962306a36Sopenharmony_ci 45062306a36Sopenharmony_ci /* 45162306a36Sopenharmony_ci * starts from beginning of file: 45262306a36Sopenharmony_ci * it is a strong indication of long-run stream (or whole-file-read) 45362306a36Sopenharmony_ci */ 45462306a36Sopenharmony_ci if (size >= index) 45562306a36Sopenharmony_ci size *= 2; 45662306a36Sopenharmony_ci 45762306a36Sopenharmony_ci ra->start = index; 45862306a36Sopenharmony_ci ra->size = min(size + req_size, max); 45962306a36Sopenharmony_ci ra->async_size = 1; 46062306a36Sopenharmony_ci 46162306a36Sopenharmony_ci return 1; 46262306a36Sopenharmony_ci} 46362306a36Sopenharmony_ci 46462306a36Sopenharmony_cistatic inline int ra_alloc_folio(struct readahead_control *ractl, pgoff_t index, 46562306a36Sopenharmony_ci pgoff_t mark, unsigned int order, gfp_t gfp) 46662306a36Sopenharmony_ci{ 46762306a36Sopenharmony_ci int err; 46862306a36Sopenharmony_ci struct folio *folio = filemap_alloc_folio(gfp, order); 46962306a36Sopenharmony_ci 47062306a36Sopenharmony_ci if (!folio) 47162306a36Sopenharmony_ci return -ENOMEM; 47262306a36Sopenharmony_ci mark = round_down(mark, 1UL << order); 47362306a36Sopenharmony_ci if (index == mark) 47462306a36Sopenharmony_ci folio_set_readahead(folio); 47562306a36Sopenharmony_ci err = filemap_add_folio(ractl->mapping, folio, index, gfp); 47662306a36Sopenharmony_ci if (err) { 47762306a36Sopenharmony_ci folio_put(folio); 47862306a36Sopenharmony_ci return err; 47962306a36Sopenharmony_ci } 48062306a36Sopenharmony_ci 48162306a36Sopenharmony_ci ractl->_nr_pages += 1UL << order; 48262306a36Sopenharmony_ci ractl->_workingset |= folio_test_workingset(folio); 48362306a36Sopenharmony_ci return 0; 48462306a36Sopenharmony_ci} 48562306a36Sopenharmony_ci 48662306a36Sopenharmony_civoid page_cache_ra_order(struct readahead_control *ractl, 48762306a36Sopenharmony_ci struct file_ra_state *ra, unsigned int new_order) 48862306a36Sopenharmony_ci{ 48962306a36Sopenharmony_ci struct address_space *mapping = ractl->mapping; 49062306a36Sopenharmony_ci pgoff_t index = readahead_index(ractl); 49162306a36Sopenharmony_ci pgoff_t limit = (i_size_read(mapping->host) - 1) >> PAGE_SHIFT; 49262306a36Sopenharmony_ci pgoff_t mark = index + ra->size - ra->async_size; 49362306a36Sopenharmony_ci int err = 0; 49462306a36Sopenharmony_ci gfp_t gfp = readahead_gfp_mask(mapping); 49562306a36Sopenharmony_ci 49662306a36Sopenharmony_ci if (!mapping_large_folio_support(mapping) || ra->size < 4) 49762306a36Sopenharmony_ci goto fallback; 49862306a36Sopenharmony_ci 49962306a36Sopenharmony_ci limit = min(limit, index + ra->size - 1); 50062306a36Sopenharmony_ci 50162306a36Sopenharmony_ci if (new_order < MAX_PAGECACHE_ORDER) { 50262306a36Sopenharmony_ci new_order += 2; 50362306a36Sopenharmony_ci if (new_order > MAX_PAGECACHE_ORDER) 50462306a36Sopenharmony_ci new_order = MAX_PAGECACHE_ORDER; 50562306a36Sopenharmony_ci while ((1 << new_order) > ra->size) 50662306a36Sopenharmony_ci new_order--; 50762306a36Sopenharmony_ci } 50862306a36Sopenharmony_ci 50962306a36Sopenharmony_ci filemap_invalidate_lock_shared(mapping); 51062306a36Sopenharmony_ci while (index <= limit) { 51162306a36Sopenharmony_ci unsigned int order = new_order; 51262306a36Sopenharmony_ci 51362306a36Sopenharmony_ci /* Align with smaller pages if needed */ 51462306a36Sopenharmony_ci if (index & ((1UL << order) - 1)) { 51562306a36Sopenharmony_ci order = __ffs(index); 51662306a36Sopenharmony_ci if (order == 1) 51762306a36Sopenharmony_ci order = 0; 51862306a36Sopenharmony_ci } 51962306a36Sopenharmony_ci /* Don't allocate pages past EOF */ 52062306a36Sopenharmony_ci while (index + (1UL << order) - 1 > limit) { 52162306a36Sopenharmony_ci if (--order == 1) 52262306a36Sopenharmony_ci order = 0; 52362306a36Sopenharmony_ci } 52462306a36Sopenharmony_ci err = ra_alloc_folio(ractl, index, mark, order, gfp); 52562306a36Sopenharmony_ci if (err) 52662306a36Sopenharmony_ci break; 52762306a36Sopenharmony_ci index += 1UL << order; 52862306a36Sopenharmony_ci } 52962306a36Sopenharmony_ci 53062306a36Sopenharmony_ci if (index > limit) { 53162306a36Sopenharmony_ci ra->size += index - limit - 1; 53262306a36Sopenharmony_ci ra->async_size += index - limit - 1; 53362306a36Sopenharmony_ci } 53462306a36Sopenharmony_ci 53562306a36Sopenharmony_ci read_pages(ractl); 53662306a36Sopenharmony_ci filemap_invalidate_unlock_shared(mapping); 53762306a36Sopenharmony_ci 53862306a36Sopenharmony_ci /* 53962306a36Sopenharmony_ci * If there were already pages in the page cache, then we may have 54062306a36Sopenharmony_ci * left some gaps. Let the regular readahead code take care of this 54162306a36Sopenharmony_ci * situation. 54262306a36Sopenharmony_ci */ 54362306a36Sopenharmony_ci if (!err) 54462306a36Sopenharmony_ci return; 54562306a36Sopenharmony_cifallback: 54662306a36Sopenharmony_ci do_page_cache_ra(ractl, ra->size, ra->async_size); 54762306a36Sopenharmony_ci} 54862306a36Sopenharmony_ci 54962306a36Sopenharmony_ci/* 55062306a36Sopenharmony_ci * A minimal readahead algorithm for trivial sequential/random reads. 55162306a36Sopenharmony_ci */ 55262306a36Sopenharmony_cistatic void ondemand_readahead(struct readahead_control *ractl, 55362306a36Sopenharmony_ci struct folio *folio, unsigned long req_size) 55462306a36Sopenharmony_ci{ 55562306a36Sopenharmony_ci struct backing_dev_info *bdi = inode_to_bdi(ractl->mapping->host); 55662306a36Sopenharmony_ci struct file_ra_state *ra = ractl->ra; 55762306a36Sopenharmony_ci unsigned long max_pages = ra->ra_pages; 55862306a36Sopenharmony_ci unsigned long add_pages; 55962306a36Sopenharmony_ci pgoff_t index = readahead_index(ractl); 56062306a36Sopenharmony_ci pgoff_t expected, prev_index; 56162306a36Sopenharmony_ci unsigned int order = folio ? folio_order(folio) : 0; 56262306a36Sopenharmony_ci 56362306a36Sopenharmony_ci /* 56462306a36Sopenharmony_ci * If the request exceeds the readahead window, allow the read to 56562306a36Sopenharmony_ci * be up to the optimal hardware IO size 56662306a36Sopenharmony_ci */ 56762306a36Sopenharmony_ci if (req_size > max_pages && bdi->io_pages > max_pages) 56862306a36Sopenharmony_ci max_pages = min(req_size, bdi->io_pages); 56962306a36Sopenharmony_ci 57062306a36Sopenharmony_ci /* 57162306a36Sopenharmony_ci * start of file 57262306a36Sopenharmony_ci */ 57362306a36Sopenharmony_ci if (!index) 57462306a36Sopenharmony_ci goto initial_readahead; 57562306a36Sopenharmony_ci 57662306a36Sopenharmony_ci /* 57762306a36Sopenharmony_ci * It's the expected callback index, assume sequential access. 57862306a36Sopenharmony_ci * Ramp up sizes, and push forward the readahead window. 57962306a36Sopenharmony_ci */ 58062306a36Sopenharmony_ci expected = round_down(ra->start + ra->size - ra->async_size, 58162306a36Sopenharmony_ci 1UL << order); 58262306a36Sopenharmony_ci if (index == expected || index == (ra->start + ra->size)) { 58362306a36Sopenharmony_ci ra->start += ra->size; 58462306a36Sopenharmony_ci ra->size = get_next_ra_size(ra, max_pages); 58562306a36Sopenharmony_ci ra->async_size = ra->size; 58662306a36Sopenharmony_ci goto readit; 58762306a36Sopenharmony_ci } 58862306a36Sopenharmony_ci 58962306a36Sopenharmony_ci /* 59062306a36Sopenharmony_ci * Hit a marked folio without valid readahead state. 59162306a36Sopenharmony_ci * E.g. interleaved reads. 59262306a36Sopenharmony_ci * Query the pagecache for async_size, which normally equals to 59362306a36Sopenharmony_ci * readahead size. Ramp it up and use it as the new readahead size. 59462306a36Sopenharmony_ci */ 59562306a36Sopenharmony_ci if (folio) { 59662306a36Sopenharmony_ci pgoff_t start; 59762306a36Sopenharmony_ci 59862306a36Sopenharmony_ci rcu_read_lock(); 59962306a36Sopenharmony_ci start = page_cache_next_miss(ractl->mapping, index + 1, 60062306a36Sopenharmony_ci max_pages); 60162306a36Sopenharmony_ci rcu_read_unlock(); 60262306a36Sopenharmony_ci 60362306a36Sopenharmony_ci if (!start || start - index > max_pages) 60462306a36Sopenharmony_ci return; 60562306a36Sopenharmony_ci 60662306a36Sopenharmony_ci ra->start = start; 60762306a36Sopenharmony_ci ra->size = start - index; /* old async_size */ 60862306a36Sopenharmony_ci ra->size += req_size; 60962306a36Sopenharmony_ci ra->size = get_next_ra_size(ra, max_pages); 61062306a36Sopenharmony_ci ra->async_size = ra->size; 61162306a36Sopenharmony_ci goto readit; 61262306a36Sopenharmony_ci } 61362306a36Sopenharmony_ci 61462306a36Sopenharmony_ci /* 61562306a36Sopenharmony_ci * oversize read 61662306a36Sopenharmony_ci */ 61762306a36Sopenharmony_ci if (req_size > max_pages) 61862306a36Sopenharmony_ci goto initial_readahead; 61962306a36Sopenharmony_ci 62062306a36Sopenharmony_ci /* 62162306a36Sopenharmony_ci * sequential cache miss 62262306a36Sopenharmony_ci * trivial case: (index - prev_index) == 1 62362306a36Sopenharmony_ci * unaligned reads: (index - prev_index) == 0 62462306a36Sopenharmony_ci */ 62562306a36Sopenharmony_ci prev_index = (unsigned long long)ra->prev_pos >> PAGE_SHIFT; 62662306a36Sopenharmony_ci if (index - prev_index <= 1UL) 62762306a36Sopenharmony_ci goto initial_readahead; 62862306a36Sopenharmony_ci 62962306a36Sopenharmony_ci /* 63062306a36Sopenharmony_ci * Query the page cache and look for the traces(cached history pages) 63162306a36Sopenharmony_ci * that a sequential stream would leave behind. 63262306a36Sopenharmony_ci */ 63362306a36Sopenharmony_ci if (try_context_readahead(ractl->mapping, ra, index, req_size, 63462306a36Sopenharmony_ci max_pages)) 63562306a36Sopenharmony_ci goto readit; 63662306a36Sopenharmony_ci 63762306a36Sopenharmony_ci /* 63862306a36Sopenharmony_ci * standalone, small random read 63962306a36Sopenharmony_ci * Read as is, and do not pollute the readahead state. 64062306a36Sopenharmony_ci */ 64162306a36Sopenharmony_ci do_page_cache_ra(ractl, req_size, 0); 64262306a36Sopenharmony_ci return; 64362306a36Sopenharmony_ci 64462306a36Sopenharmony_ciinitial_readahead: 64562306a36Sopenharmony_ci ra->start = index; 64662306a36Sopenharmony_ci ra->size = get_init_ra_size(req_size, max_pages); 64762306a36Sopenharmony_ci ra->async_size = ra->size > req_size ? ra->size - req_size : ra->size; 64862306a36Sopenharmony_ci 64962306a36Sopenharmony_cireadit: 65062306a36Sopenharmony_ci /* 65162306a36Sopenharmony_ci * Will this read hit the readahead marker made by itself? 65262306a36Sopenharmony_ci * If so, trigger the readahead marker hit now, and merge 65362306a36Sopenharmony_ci * the resulted next readahead window into the current one. 65462306a36Sopenharmony_ci * Take care of maximum IO pages as above. 65562306a36Sopenharmony_ci */ 65662306a36Sopenharmony_ci if (index == ra->start && ra->size == ra->async_size) { 65762306a36Sopenharmony_ci add_pages = get_next_ra_size(ra, max_pages); 65862306a36Sopenharmony_ci if (ra->size + add_pages <= max_pages) { 65962306a36Sopenharmony_ci ra->async_size = add_pages; 66062306a36Sopenharmony_ci ra->size += add_pages; 66162306a36Sopenharmony_ci } else { 66262306a36Sopenharmony_ci ra->size = max_pages; 66362306a36Sopenharmony_ci ra->async_size = max_pages >> 1; 66462306a36Sopenharmony_ci } 66562306a36Sopenharmony_ci } 66662306a36Sopenharmony_ci 66762306a36Sopenharmony_ci ractl->_index = ra->start; 66862306a36Sopenharmony_ci page_cache_ra_order(ractl, ra, order); 66962306a36Sopenharmony_ci} 67062306a36Sopenharmony_ci 67162306a36Sopenharmony_civoid page_cache_sync_ra(struct readahead_control *ractl, 67262306a36Sopenharmony_ci unsigned long req_count) 67362306a36Sopenharmony_ci{ 67462306a36Sopenharmony_ci bool do_forced_ra = ractl->file && (ractl->file->f_mode & FMODE_RANDOM); 67562306a36Sopenharmony_ci 67662306a36Sopenharmony_ci /* 67762306a36Sopenharmony_ci * Even if readahead is disabled, issue this request as readahead 67862306a36Sopenharmony_ci * as we'll need it to satisfy the requested range. The forced 67962306a36Sopenharmony_ci * readahead will do the right thing and limit the read to just the 68062306a36Sopenharmony_ci * requested range, which we'll set to 1 page for this case. 68162306a36Sopenharmony_ci */ 68262306a36Sopenharmony_ci if (!ractl->ra->ra_pages || blk_cgroup_congested()) { 68362306a36Sopenharmony_ci if (!ractl->file) 68462306a36Sopenharmony_ci return; 68562306a36Sopenharmony_ci req_count = 1; 68662306a36Sopenharmony_ci do_forced_ra = true; 68762306a36Sopenharmony_ci } 68862306a36Sopenharmony_ci 68962306a36Sopenharmony_ci /* be dumb */ 69062306a36Sopenharmony_ci if (do_forced_ra) { 69162306a36Sopenharmony_ci force_page_cache_ra(ractl, req_count); 69262306a36Sopenharmony_ci return; 69362306a36Sopenharmony_ci } 69462306a36Sopenharmony_ci 69562306a36Sopenharmony_ci ondemand_readahead(ractl, NULL, req_count); 69662306a36Sopenharmony_ci} 69762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(page_cache_sync_ra); 69862306a36Sopenharmony_ci 69962306a36Sopenharmony_civoid page_cache_async_ra(struct readahead_control *ractl, 70062306a36Sopenharmony_ci struct folio *folio, unsigned long req_count) 70162306a36Sopenharmony_ci{ 70262306a36Sopenharmony_ci /* no readahead */ 70362306a36Sopenharmony_ci if (!ractl->ra->ra_pages) 70462306a36Sopenharmony_ci return; 70562306a36Sopenharmony_ci 70662306a36Sopenharmony_ci /* 70762306a36Sopenharmony_ci * Same bit is used for PG_readahead and PG_reclaim. 70862306a36Sopenharmony_ci */ 70962306a36Sopenharmony_ci if (folio_test_writeback(folio)) 71062306a36Sopenharmony_ci return; 71162306a36Sopenharmony_ci 71262306a36Sopenharmony_ci folio_clear_readahead(folio); 71362306a36Sopenharmony_ci 71462306a36Sopenharmony_ci if (blk_cgroup_congested()) 71562306a36Sopenharmony_ci return; 71662306a36Sopenharmony_ci 71762306a36Sopenharmony_ci ondemand_readahead(ractl, folio, req_count); 71862306a36Sopenharmony_ci} 71962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(page_cache_async_ra); 72062306a36Sopenharmony_ci 72162306a36Sopenharmony_cissize_t ksys_readahead(int fd, loff_t offset, size_t count) 72262306a36Sopenharmony_ci{ 72362306a36Sopenharmony_ci ssize_t ret; 72462306a36Sopenharmony_ci struct fd f; 72562306a36Sopenharmony_ci 72662306a36Sopenharmony_ci ret = -EBADF; 72762306a36Sopenharmony_ci f = fdget(fd); 72862306a36Sopenharmony_ci if (!f.file || !(f.file->f_mode & FMODE_READ)) 72962306a36Sopenharmony_ci goto out; 73062306a36Sopenharmony_ci 73162306a36Sopenharmony_ci /* 73262306a36Sopenharmony_ci * The readahead() syscall is intended to run only on files 73362306a36Sopenharmony_ci * that can execute readahead. If readahead is not possible 73462306a36Sopenharmony_ci * on this file, then we must return -EINVAL. 73562306a36Sopenharmony_ci */ 73662306a36Sopenharmony_ci ret = -EINVAL; 73762306a36Sopenharmony_ci if (!f.file->f_mapping || !f.file->f_mapping->a_ops || 73862306a36Sopenharmony_ci (!S_ISREG(file_inode(f.file)->i_mode) && 73962306a36Sopenharmony_ci !S_ISBLK(file_inode(f.file)->i_mode))) 74062306a36Sopenharmony_ci goto out; 74162306a36Sopenharmony_ci 74262306a36Sopenharmony_ci ret = vfs_fadvise(f.file, offset, count, POSIX_FADV_WILLNEED); 74362306a36Sopenharmony_ciout: 74462306a36Sopenharmony_ci fdput(f); 74562306a36Sopenharmony_ci return ret; 74662306a36Sopenharmony_ci} 74762306a36Sopenharmony_ci 74862306a36Sopenharmony_ciSYSCALL_DEFINE3(readahead, int, fd, loff_t, offset, size_t, count) 74962306a36Sopenharmony_ci{ 75062306a36Sopenharmony_ci return ksys_readahead(fd, offset, count); 75162306a36Sopenharmony_ci} 75262306a36Sopenharmony_ci 75362306a36Sopenharmony_ci#if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_READAHEAD) 75462306a36Sopenharmony_ciCOMPAT_SYSCALL_DEFINE4(readahead, int, fd, compat_arg_u64_dual(offset), size_t, count) 75562306a36Sopenharmony_ci{ 75662306a36Sopenharmony_ci return ksys_readahead(fd, compat_arg_u64_glue(offset), count); 75762306a36Sopenharmony_ci} 75862306a36Sopenharmony_ci#endif 75962306a36Sopenharmony_ci 76062306a36Sopenharmony_ci/** 76162306a36Sopenharmony_ci * readahead_expand - Expand a readahead request 76262306a36Sopenharmony_ci * @ractl: The request to be expanded 76362306a36Sopenharmony_ci * @new_start: The revised start 76462306a36Sopenharmony_ci * @new_len: The revised size of the request 76562306a36Sopenharmony_ci * 76662306a36Sopenharmony_ci * Attempt to expand a readahead request outwards from the current size to the 76762306a36Sopenharmony_ci * specified size by inserting locked pages before and after the current window 76862306a36Sopenharmony_ci * to increase the size to the new window. This may involve the insertion of 76962306a36Sopenharmony_ci * THPs, in which case the window may get expanded even beyond what was 77062306a36Sopenharmony_ci * requested. 77162306a36Sopenharmony_ci * 77262306a36Sopenharmony_ci * The algorithm will stop if it encounters a conflicting page already in the 77362306a36Sopenharmony_ci * pagecache and leave a smaller expansion than requested. 77462306a36Sopenharmony_ci * 77562306a36Sopenharmony_ci * The caller must check for this by examining the revised @ractl object for a 77662306a36Sopenharmony_ci * different expansion than was requested. 77762306a36Sopenharmony_ci */ 77862306a36Sopenharmony_civoid readahead_expand(struct readahead_control *ractl, 77962306a36Sopenharmony_ci loff_t new_start, size_t new_len) 78062306a36Sopenharmony_ci{ 78162306a36Sopenharmony_ci struct address_space *mapping = ractl->mapping; 78262306a36Sopenharmony_ci struct file_ra_state *ra = ractl->ra; 78362306a36Sopenharmony_ci pgoff_t new_index, new_nr_pages; 78462306a36Sopenharmony_ci gfp_t gfp_mask = readahead_gfp_mask(mapping); 78562306a36Sopenharmony_ci 78662306a36Sopenharmony_ci new_index = new_start / PAGE_SIZE; 78762306a36Sopenharmony_ci 78862306a36Sopenharmony_ci /* Expand the leading edge downwards */ 78962306a36Sopenharmony_ci while (ractl->_index > new_index) { 79062306a36Sopenharmony_ci unsigned long index = ractl->_index - 1; 79162306a36Sopenharmony_ci struct folio *folio = xa_load(&mapping->i_pages, index); 79262306a36Sopenharmony_ci 79362306a36Sopenharmony_ci if (folio && !xa_is_value(folio)) 79462306a36Sopenharmony_ci return; /* Folio apparently present */ 79562306a36Sopenharmony_ci 79662306a36Sopenharmony_ci folio = filemap_alloc_folio(gfp_mask, 0); 79762306a36Sopenharmony_ci if (!folio) 79862306a36Sopenharmony_ci return; 79962306a36Sopenharmony_ci if (filemap_add_folio(mapping, folio, index, gfp_mask) < 0) { 80062306a36Sopenharmony_ci folio_put(folio); 80162306a36Sopenharmony_ci return; 80262306a36Sopenharmony_ci } 80362306a36Sopenharmony_ci if (unlikely(folio_test_workingset(folio)) && 80462306a36Sopenharmony_ci !ractl->_workingset) { 80562306a36Sopenharmony_ci ractl->_workingset = true; 80662306a36Sopenharmony_ci psi_memstall_enter(&ractl->_pflags); 80762306a36Sopenharmony_ci } 80862306a36Sopenharmony_ci ractl->_nr_pages++; 80962306a36Sopenharmony_ci ractl->_index = folio->index; 81062306a36Sopenharmony_ci } 81162306a36Sopenharmony_ci 81262306a36Sopenharmony_ci new_len += new_start - readahead_pos(ractl); 81362306a36Sopenharmony_ci new_nr_pages = DIV_ROUND_UP(new_len, PAGE_SIZE); 81462306a36Sopenharmony_ci 81562306a36Sopenharmony_ci /* Expand the trailing edge upwards */ 81662306a36Sopenharmony_ci while (ractl->_nr_pages < new_nr_pages) { 81762306a36Sopenharmony_ci unsigned long index = ractl->_index + ractl->_nr_pages; 81862306a36Sopenharmony_ci struct folio *folio = xa_load(&mapping->i_pages, index); 81962306a36Sopenharmony_ci 82062306a36Sopenharmony_ci if (folio && !xa_is_value(folio)) 82162306a36Sopenharmony_ci return; /* Folio apparently present */ 82262306a36Sopenharmony_ci 82362306a36Sopenharmony_ci folio = filemap_alloc_folio(gfp_mask, 0); 82462306a36Sopenharmony_ci if (!folio) 82562306a36Sopenharmony_ci return; 82662306a36Sopenharmony_ci if (filemap_add_folio(mapping, folio, index, gfp_mask) < 0) { 82762306a36Sopenharmony_ci folio_put(folio); 82862306a36Sopenharmony_ci return; 82962306a36Sopenharmony_ci } 83062306a36Sopenharmony_ci if (unlikely(folio_test_workingset(folio)) && 83162306a36Sopenharmony_ci !ractl->_workingset) { 83262306a36Sopenharmony_ci ractl->_workingset = true; 83362306a36Sopenharmony_ci psi_memstall_enter(&ractl->_pflags); 83462306a36Sopenharmony_ci } 83562306a36Sopenharmony_ci ractl->_nr_pages++; 83662306a36Sopenharmony_ci if (ra) { 83762306a36Sopenharmony_ci ra->size++; 83862306a36Sopenharmony_ci ra->async_size++; 83962306a36Sopenharmony_ci } 84062306a36Sopenharmony_ci } 84162306a36Sopenharmony_ci} 84262306a36Sopenharmony_ciEXPORT_SYMBOL(readahead_expand); 843