Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * mm/readahead.c - address_space-level file readahead. |
| 4 | * |
| 5 | * Copyright (C) 2002, Linus Torvalds |
| 6 | * |
Francois Cami | e1f8e87 | 2008-10-15 22:01:59 -0700 | [diff] [blame] | 7 | * 09Apr2002 Andrew Morton |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Initial version. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
Ross Zwisler | 11bd969 | 2016-08-25 15:17:17 -0700 | [diff] [blame] | 12 | #include <linux/dax.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/gfp.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/blkdev.h> |
| 16 | #include <linux/backing-dev.h> |
Andrew Morton | 8bde37f | 2006-12-10 02:19:40 -0800 | [diff] [blame] | 17 | #include <linux/task_io_accounting_ops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/pagevec.h> |
Jens Axboe | f5ff842 | 2007-09-21 09:19:54 +0200 | [diff] [blame] | 19 | #include <linux/pagemap.h> |
Cong Wang | 782182e | 2012-05-29 15:06:43 -0700 | [diff] [blame] | 20 | #include <linux/syscalls.h> |
| 21 | #include <linux/file.h> |
Geliang Tang | d72ee91 | 2016-01-14 15:22:01 -0800 | [diff] [blame] | 22 | #include <linux/mm_inline.h> |
Josef Bacik | ca47e8c | 2018-07-03 11:15:03 -0400 | [diff] [blame] | 23 | #include <linux/blk-cgroup.h> |
Amir Goldstein | 3d8f761 | 2018-08-29 08:41:29 +0300 | [diff] [blame] | 24 | #include <linux/fadvise.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Fabian Frederick | 29f175d | 2014-04-07 15:37:55 -0700 | [diff] [blame] | 26 | #include "internal.h" |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | /* |
| 29 | * Initialise a struct file's readahead state. Assumes that the caller has |
| 30 | * memset *ra to zero. |
| 31 | */ |
| 32 | void |
| 33 | file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping) |
| 34 | { |
Christoph Hellwig | de1414a | 2015-01-14 10:42:36 +0100 | [diff] [blame] | 35 | ra->ra_pages = inode_to_bdi(mapping->host)->ra_pages; |
Fengguang Wu | f4e6b49 | 2007-10-16 01:24:33 -0700 | [diff] [blame] | 36 | ra->prev_pos = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | } |
Steven Whitehouse | d41cc70 | 2006-01-30 08:53:33 +0000 | [diff] [blame] | 38 | EXPORT_SYMBOL_GPL(file_ra_state_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
David Howells | 03fb3d2 | 2009-04-03 16:42:35 +0100 | [diff] [blame] | 40 | /* |
| 41 | * see if a page needs releasing upon read_cache_pages() failure |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 42 | * - the caller of read_cache_pages() may have set PG_private or PG_fscache |
| 43 | * before calling, such as the NFS fs marking pages that are cached locally |
| 44 | * on disk, thus we need to give the fs a chance to clean up in the event of |
| 45 | * an error |
David Howells | 03fb3d2 | 2009-04-03 16:42:35 +0100 | [diff] [blame] | 46 | */ |
| 47 | static void read_cache_pages_invalidate_page(struct address_space *mapping, |
| 48 | struct page *page) |
| 49 | { |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 50 | if (page_has_private(page)) { |
David Howells | 03fb3d2 | 2009-04-03 16:42:35 +0100 | [diff] [blame] | 51 | if (!trylock_page(page)) |
| 52 | BUG(); |
| 53 | page->mapping = mapping; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 54 | do_invalidatepage(page, 0, PAGE_SIZE); |
David Howells | 03fb3d2 | 2009-04-03 16:42:35 +0100 | [diff] [blame] | 55 | page->mapping = NULL; |
| 56 | unlock_page(page); |
| 57 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 58 | put_page(page); |
David Howells | 03fb3d2 | 2009-04-03 16:42:35 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* |
| 62 | * release a list of pages, invalidating them first if need be |
| 63 | */ |
| 64 | static void read_cache_pages_invalidate_pages(struct address_space *mapping, |
| 65 | struct list_head *pages) |
| 66 | { |
| 67 | struct page *victim; |
| 68 | |
| 69 | while (!list_empty(pages)) { |
Geliang Tang | c8ad630 | 2016-01-14 15:20:51 -0800 | [diff] [blame] | 70 | victim = lru_to_page(pages); |
David Howells | 03fb3d2 | 2009-04-03 16:42:35 +0100 | [diff] [blame] | 71 | list_del(&victim->lru); |
| 72 | read_cache_pages_invalidate_page(mapping, victim); |
| 73 | } |
| 74 | } |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | /** |
Randy Dunlap | bd40cdd | 2006-06-25 05:48:08 -0700 | [diff] [blame] | 77 | * read_cache_pages - populate an address space with some pages & start reads against them |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | * @mapping: the address_space |
| 79 | * @pages: The address of a list_head which contains the target pages. These |
| 80 | * pages have their ->index populated and are otherwise uninitialised. |
| 81 | * @filler: callback routine for filling a single page. |
| 82 | * @data: private data for the callback routine. |
| 83 | * |
| 84 | * Hides the details of the LRU cache etc from the filesystems. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 85 | * |
| 86 | * Returns: %0 on success, error return by @filler otherwise |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | */ |
| 88 | int read_cache_pages(struct address_space *mapping, struct list_head *pages, |
| 89 | int (*filler)(void *, struct page *), void *data) |
| 90 | { |
| 91 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | int ret = 0; |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | while (!list_empty(pages)) { |
Geliang Tang | c8ad630 | 2016-01-14 15:20:51 -0800 | [diff] [blame] | 95 | page = lru_to_page(pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | list_del(&page->lru); |
Michal Hocko | 063d99b | 2015-10-15 15:28:24 -0700 | [diff] [blame] | 97 | if (add_to_page_cache_lru(page, mapping, page->index, |
Michal Hocko | 8a5c743 | 2016-07-26 15:24:53 -0700 | [diff] [blame] | 98 | readahead_gfp_mask(mapping))) { |
David Howells | 03fb3d2 | 2009-04-03 16:42:35 +0100 | [diff] [blame] | 99 | read_cache_pages_invalidate_page(mapping, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | continue; |
| 101 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 102 | put_page(page); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | ret = filler(data, page); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 105 | if (unlikely(ret)) { |
David Howells | 03fb3d2 | 2009-04-03 16:42:35 +0100 | [diff] [blame] | 106 | read_cache_pages_invalidate_pages(mapping, pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | break; |
| 108 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 109 | task_io_account_read(PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | EXPORT_SYMBOL(read_cache_pages); |
| 115 | |
Matthew Wilcox (Oracle) | a4d9653 | 2020-06-01 21:46:25 -0700 | [diff] [blame] | 116 | static void read_pages(struct readahead_control *rac, struct list_head *pages, |
Matthew Wilcox (Oracle) | c1f6925 | 2020-06-01 21:46:40 -0700 | [diff] [blame] | 117 | bool skip_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
Matthew Wilcox (Oracle) | a4d9653 | 2020-06-01 21:46:25 -0700 | [diff] [blame] | 119 | const struct address_space_operations *aops = rac->mapping->a_ops; |
Matthew Wilcox (Oracle) | c1f6925 | 2020-06-01 21:46:40 -0700 | [diff] [blame] | 120 | struct page *page; |
Jens Axboe | 5b417b1 | 2010-04-19 10:04:38 +0200 | [diff] [blame] | 121 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Matthew Wilcox (Oracle) | a4d9653 | 2020-06-01 21:46:25 -0700 | [diff] [blame] | 123 | if (!readahead_count(rac)) |
Matthew Wilcox (Oracle) | c1f6925 | 2020-06-01 21:46:40 -0700 | [diff] [blame] | 124 | goto out; |
Matthew Wilcox (Oracle) | ad4ae1c | 2020-06-01 21:46:18 -0700 | [diff] [blame] | 125 | |
Jens Axboe | 5b417b1 | 2010-04-19 10:04:38 +0200 | [diff] [blame] | 126 | blk_start_plug(&plug); |
| 127 | |
Matthew Wilcox (Oracle) | 8151b4c | 2020-06-01 21:46:44 -0700 | [diff] [blame] | 128 | if (aops->readahead) { |
| 129 | aops->readahead(rac); |
| 130 | /* Clean up the remaining pages */ |
| 131 | while ((page = readahead_page(rac))) { |
| 132 | unlock_page(page); |
| 133 | put_page(page); |
| 134 | } |
| 135 | } else if (aops->readpages) { |
Matthew Wilcox (Oracle) | a4d9653 | 2020-06-01 21:46:25 -0700 | [diff] [blame] | 136 | aops->readpages(rac->file, rac->mapping, pages, |
| 137 | readahead_count(rac)); |
OGAWA Hirofumi | 029e332 | 2006-11-02 22:07:06 -0800 | [diff] [blame] | 138 | /* Clean up the remaining pages */ |
| 139 | put_pages_list(pages); |
Matthew Wilcox (Oracle) | c1f6925 | 2020-06-01 21:46:40 -0700 | [diff] [blame] | 140 | rac->_index += rac->_nr_pages; |
| 141 | rac->_nr_pages = 0; |
| 142 | } else { |
| 143 | while ((page = readahead_page(rac))) { |
Matthew Wilcox (Oracle) | a4d9653 | 2020-06-01 21:46:25 -0700 | [diff] [blame] | 144 | aops->readpage(rac->file, page); |
Matthew Wilcox (Oracle) | c1f6925 | 2020-06-01 21:46:40 -0700 | [diff] [blame] | 145 | put_page(page); |
| 146 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
Jens Axboe | 5b417b1 | 2010-04-19 10:04:38 +0200 | [diff] [blame] | 148 | |
Jens Axboe | 5b417b1 | 2010-04-19 10:04:38 +0200 | [diff] [blame] | 149 | blk_finish_plug(&plug); |
Matthew Wilcox (Oracle) | ad4ae1c | 2020-06-01 21:46:18 -0700 | [diff] [blame] | 150 | |
| 151 | BUG_ON(!list_empty(pages)); |
Matthew Wilcox (Oracle) | c1f6925 | 2020-06-01 21:46:40 -0700 | [diff] [blame] | 152 | BUG_ON(readahead_count(rac)); |
| 153 | |
| 154 | out: |
| 155 | if (skip_page) |
| 156 | rac->_index++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Matthew Wilcox (Oracle) | 2c68423 | 2020-06-01 21:46:51 -0700 | [diff] [blame^] | 159 | /** |
| 160 | * page_cache_readahead_unbounded - Start unchecked readahead. |
| 161 | * @mapping: File address space. |
| 162 | * @file: This instance of the open file; used for authentication. |
| 163 | * @index: First page index to read. |
| 164 | * @nr_to_read: The number of pages to read. |
| 165 | * @lookahead_size: Where to start the next readahead. |
| 166 | * |
| 167 | * This function is for filesystems to call when they want to start |
| 168 | * readahead beyond a file's stated i_size. This is almost certainly |
| 169 | * not the function you want to call. Use page_cache_async_readahead() |
| 170 | * or page_cache_sync_readahead() instead. |
| 171 | * |
| 172 | * Context: File is referenced by caller. Mutexes may be held by caller. |
| 173 | * May sleep, but will not reenter filesystem to reclaim memory. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | */ |
Matthew Wilcox (Oracle) | 2c68423 | 2020-06-01 21:46:51 -0700 | [diff] [blame^] | 175 | void page_cache_readahead_unbounded(struct address_space *mapping, |
| 176 | struct file *file, pgoff_t index, unsigned long nr_to_read, |
Christoph Hellwig | c534aa3 | 2018-06-01 09:03:05 -0700 | [diff] [blame] | 177 | unsigned long lookahead_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | LIST_HEAD(page_pool); |
Michal Hocko | 8a5c743 | 2016-07-26 15:24:53 -0700 | [diff] [blame] | 180 | gfp_t gfp_mask = readahead_gfp_mask(mapping); |
Matthew Wilcox (Oracle) | a4d9653 | 2020-06-01 21:46:25 -0700 | [diff] [blame] | 181 | struct readahead_control rac = { |
| 182 | .mapping = mapping, |
Matthew Wilcox (Oracle) | 2c68423 | 2020-06-01 21:46:51 -0700 | [diff] [blame^] | 183 | .file = file, |
Matthew Wilcox (Oracle) | c1f6925 | 2020-06-01 21:46:40 -0700 | [diff] [blame] | 184 | ._index = index, |
Matthew Wilcox (Oracle) | a4d9653 | 2020-06-01 21:46:25 -0700 | [diff] [blame] | 185 | }; |
Matthew Wilcox (Oracle) | c2c7ad7 | 2020-06-01 21:46:32 -0700 | [diff] [blame] | 186 | unsigned long i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
| 188 | /* |
| 189 | * Preallocate as many pages as we will need. |
| 190 | */ |
Matthew Wilcox (Oracle) | c2c7ad7 | 2020-06-01 21:46:32 -0700 | [diff] [blame] | 191 | for (i = 0; i < nr_to_read; i++) { |
Matthew Wilcox (Oracle) | b0f31d7 | 2020-06-01 21:46:47 -0700 | [diff] [blame] | 192 | struct page *page = xa_load(&mapping->i_pages, index + i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Matthew Wilcox (Oracle) | c1f6925 | 2020-06-01 21:46:40 -0700 | [diff] [blame] | 194 | BUG_ON(index + i != rac._index + rac._nr_pages); |
| 195 | |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 196 | if (page && !xa_is_value(page)) { |
Christoph Hellwig | b3751e6 | 2018-06-01 09:03:06 -0700 | [diff] [blame] | 197 | /* |
| 198 | * Page already present? Kick off the current batch of |
| 199 | * contiguous pages before continuing with the next |
| 200 | * batch. |
| 201 | */ |
Matthew Wilcox (Oracle) | c1f6925 | 2020-06-01 21:46:40 -0700 | [diff] [blame] | 202 | read_pages(&rac, &page_pool, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | continue; |
Christoph Hellwig | b3751e6 | 2018-06-01 09:03:06 -0700 | [diff] [blame] | 204 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Michal Hocko | 8a5c743 | 2016-07-26 15:24:53 -0700 | [diff] [blame] | 206 | page = __page_cache_alloc(gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | if (!page) |
| 208 | break; |
Matthew Wilcox (Oracle) | c1f6925 | 2020-06-01 21:46:40 -0700 | [diff] [blame] | 209 | if (mapping->a_ops->readpages) { |
| 210 | page->index = index + i; |
| 211 | list_add(&page->lru, &page_pool); |
| 212 | } else if (add_to_page_cache_lru(page, mapping, index + i, |
| 213 | gfp_mask) < 0) { |
| 214 | put_page(page); |
| 215 | read_pages(&rac, &page_pool, true); |
| 216 | continue; |
| 217 | } |
Matthew Wilcox (Oracle) | c2c7ad7 | 2020-06-01 21:46:32 -0700 | [diff] [blame] | 218 | if (i == nr_to_read - lookahead_size) |
Fengguang Wu | 46fc3e7 | 2007-07-19 01:47:57 -0700 | [diff] [blame] | 219 | SetPageReadahead(page); |
Matthew Wilcox (Oracle) | a4d9653 | 2020-06-01 21:46:25 -0700 | [diff] [blame] | 220 | rac._nr_pages++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | /* |
| 224 | * Now start the IO. We ignore I/O errors - if the page is not |
| 225 | * uptodate then the caller will launch readpage again, and |
| 226 | * will then handle the error. |
| 227 | */ |
Matthew Wilcox (Oracle) | c1f6925 | 2020-06-01 21:46:40 -0700 | [diff] [blame] | 228 | read_pages(&rac, &page_pool, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
Matthew Wilcox (Oracle) | 2c68423 | 2020-06-01 21:46:51 -0700 | [diff] [blame^] | 230 | EXPORT_SYMBOL_GPL(page_cache_readahead_unbounded); |
| 231 | |
| 232 | /* |
| 233 | * __do_page_cache_readahead() actually reads a chunk of disk. It allocates |
| 234 | * the pages first, then submits them for I/O. This avoids the very bad |
| 235 | * behaviour which would occur if page allocations are causing VM writeback. |
| 236 | * We really don't want to intermingle reads and writes like that. |
| 237 | */ |
| 238 | void __do_page_cache_readahead(struct address_space *mapping, |
| 239 | struct file *file, pgoff_t index, unsigned long nr_to_read, |
| 240 | unsigned long lookahead_size) |
| 241 | { |
| 242 | struct inode *inode = mapping->host; |
| 243 | loff_t isize = i_size_read(inode); |
| 244 | pgoff_t end_index; /* The last page we want to read */ |
| 245 | |
| 246 | if (isize == 0) |
| 247 | return; |
| 248 | |
| 249 | end_index = (isize - 1) >> PAGE_SHIFT; |
| 250 | if (index > end_index) |
| 251 | return; |
| 252 | /* Don't read past the page containing the last byte of the file */ |
| 253 | if (nr_to_read > end_index - index) |
| 254 | nr_to_read = end_index - index + 1; |
| 255 | |
| 256 | page_cache_readahead_unbounded(mapping, file, index, nr_to_read, |
| 257 | lookahead_size); |
| 258 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | /* |
| 261 | * Chunk the readahead into 2 megabyte units, so that we don't pin too much |
| 262 | * memory at once. |
| 263 | */ |
Matthew Wilcox (Oracle) | 9a42823 | 2020-06-01 21:46:10 -0700 | [diff] [blame] | 264 | void force_page_cache_readahead(struct address_space *mapping, |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 265 | struct file *filp, pgoff_t index, unsigned long nr_to_read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
Jens Axboe | 9491ae4 | 2016-12-12 16:43:26 -0800 | [diff] [blame] | 267 | struct backing_dev_info *bdi = inode_to_bdi(mapping->host); |
| 268 | struct file_ra_state *ra = &filp->f_ra; |
| 269 | unsigned long max_pages; |
| 270 | |
Matthew Wilcox (Oracle) | 8151b4c | 2020-06-01 21:46:44 -0700 | [diff] [blame] | 271 | if (unlikely(!mapping->a_ops->readpage && !mapping->a_ops->readpages && |
| 272 | !mapping->a_ops->readahead)) |
Matthew Wilcox (Oracle) | 9a42823 | 2020-06-01 21:46:10 -0700 | [diff] [blame] | 273 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Jens Axboe | 9491ae4 | 2016-12-12 16:43:26 -0800 | [diff] [blame] | 275 | /* |
| 276 | * If the request exceeds the readahead window, allow the read to |
| 277 | * be up to the optimal hardware IO size |
| 278 | */ |
| 279 | max_pages = max_t(unsigned long, bdi->io_pages, ra->ra_pages); |
| 280 | nr_to_read = min(nr_to_read, max_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | while (nr_to_read) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 282 | unsigned long this_chunk = (2 * 1024 * 1024) / PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
| 284 | if (this_chunk > nr_to_read) |
| 285 | this_chunk = nr_to_read; |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 286 | __do_page_cache_readahead(mapping, filp, index, this_chunk, 0); |
Mark Rutland | 58d5640 | 2014-01-29 14:05:51 -0800 | [diff] [blame] | 287 | |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 288 | index += this_chunk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | nr_to_read -= this_chunk; |
| 290 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Fengguang Wu | 5ce1110 | 2007-07-19 01:47:59 -0700 | [diff] [blame] | 293 | /* |
Fengguang Wu | c743d96 | 2007-07-19 01:48:04 -0700 | [diff] [blame] | 294 | * Set the initial window size, round to next power of 2 and square |
| 295 | * for small size, x 4 for medium, and x 2 for large |
| 296 | * for 128k (32 page) max ra |
| 297 | * 1-8 page = 32k initial, > 8 page = 128k initial |
| 298 | */ |
| 299 | static unsigned long get_init_ra_size(unsigned long size, unsigned long max) |
| 300 | { |
| 301 | unsigned long newsize = roundup_pow_of_two(size); |
| 302 | |
| 303 | if (newsize <= max / 32) |
| 304 | newsize = newsize * 4; |
| 305 | else if (newsize <= max / 4) |
| 306 | newsize = newsize * 2; |
| 307 | else |
| 308 | newsize = max; |
| 309 | |
| 310 | return newsize; |
| 311 | } |
| 312 | |
| 313 | /* |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 314 | * Get the previous window size, ramp it up, and |
| 315 | * return it as the new window size. |
| 316 | */ |
Fengguang Wu | c743d96 | 2007-07-19 01:48:04 -0700 | [diff] [blame] | 317 | static unsigned long get_next_ra_size(struct file_ra_state *ra, |
Gao Xiang | 20ff1c9 | 2018-12-28 00:33:34 -0800 | [diff] [blame] | 318 | unsigned long max) |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 319 | { |
Fengguang Wu | f9acc8c | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 320 | unsigned long cur = ra->size; |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 321 | |
| 322 | if (cur < max / 16) |
Gao Xiang | 20ff1c9 | 2018-12-28 00:33:34 -0800 | [diff] [blame] | 323 | return 4 * cur; |
| 324 | if (cur <= max / 2) |
| 325 | return 2 * cur; |
| 326 | return max; |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* |
| 330 | * On-demand readahead design. |
| 331 | * |
| 332 | * The fields in struct file_ra_state represent the most-recently-executed |
| 333 | * readahead attempt: |
| 334 | * |
Fengguang Wu | f9acc8c | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 335 | * |<----- async_size ---------| |
| 336 | * |------------------- size -------------------->| |
| 337 | * |==================#===========================| |
| 338 | * ^start ^page marked with PG_readahead |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 339 | * |
| 340 | * To overlap application thinking time and disk I/O time, we do |
| 341 | * `readahead pipelining': Do not wait until the application consumed all |
| 342 | * readahead pages and stalled on the missing page at readahead_index; |
Fengguang Wu | f9acc8c | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 343 | * Instead, submit an asynchronous readahead I/O as soon as there are |
| 344 | * only async_size pages left in the readahead window. Normally async_size |
| 345 | * will be equal to size, for maximum pipelining. |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 346 | * |
| 347 | * In interleaved sequential reads, concurrent streams on the same fd can |
| 348 | * be invalidating each other's readahead state. So we flag the new readahead |
Fengguang Wu | f9acc8c | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 349 | * page at (start+size-async_size) with PG_readahead, and use it as readahead |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 350 | * indicator. The flag won't be set on already cached pages, to avoid the |
| 351 | * readahead-for-nothing fuss, saving pointless page cache lookups. |
| 352 | * |
Fengguang Wu | f4e6b49 | 2007-10-16 01:24:33 -0700 | [diff] [blame] | 353 | * prev_pos tracks the last visited byte in the _previous_ read request. |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 354 | * It should be maintained by the caller, and will be used for detecting |
| 355 | * small random reads. Note that the readahead algorithm checks loosely |
| 356 | * for sequential patterns. Hence interleaved reads might be served as |
| 357 | * sequential ones. |
| 358 | * |
| 359 | * There is a special-case: if the first page which the application tries to |
| 360 | * read happens to be the first page of the file, it is assumed that a linear |
| 361 | * read is about to happen and the window is immediately set to the initial size |
| 362 | * based on I/O request size and the max_readahead. |
| 363 | * |
| 364 | * The code ramps up the readahead size aggressively at first, but slow down as |
| 365 | * it approaches max_readhead. |
| 366 | */ |
| 367 | |
| 368 | /* |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 369 | * Count contiguously cached pages from @index-1 to @index-@max, |
Wu Fengguang | 10be0b3 | 2009-06-16 15:31:36 -0700 | [diff] [blame] | 370 | * this count is a conservative estimation of |
| 371 | * - length of the sequential read sequence, or |
| 372 | * - thrashing threshold in memory tight systems |
| 373 | */ |
| 374 | static pgoff_t count_history_pages(struct address_space *mapping, |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 375 | pgoff_t index, unsigned long max) |
Wu Fengguang | 10be0b3 | 2009-06-16 15:31:36 -0700 | [diff] [blame] | 376 | { |
| 377 | pgoff_t head; |
| 378 | |
| 379 | rcu_read_lock(); |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 380 | head = page_cache_prev_miss(mapping, index - 1, max); |
Wu Fengguang | 10be0b3 | 2009-06-16 15:31:36 -0700 | [diff] [blame] | 381 | rcu_read_unlock(); |
| 382 | |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 383 | return index - 1 - head; |
Wu Fengguang | 10be0b3 | 2009-06-16 15:31:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /* |
| 387 | * page cache context based read-ahead |
| 388 | */ |
| 389 | static int try_context_readahead(struct address_space *mapping, |
| 390 | struct file_ra_state *ra, |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 391 | pgoff_t index, |
Wu Fengguang | 10be0b3 | 2009-06-16 15:31:36 -0700 | [diff] [blame] | 392 | unsigned long req_size, |
| 393 | unsigned long max) |
| 394 | { |
| 395 | pgoff_t size; |
| 396 | |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 397 | size = count_history_pages(mapping, index, max); |
Wu Fengguang | 10be0b3 | 2009-06-16 15:31:36 -0700 | [diff] [blame] | 398 | |
| 399 | /* |
Fengguang Wu | 2cad401 | 2013-09-11 14:21:47 -0700 | [diff] [blame] | 400 | * not enough history pages: |
Wu Fengguang | 10be0b3 | 2009-06-16 15:31:36 -0700 | [diff] [blame] | 401 | * it could be a random read |
| 402 | */ |
Fengguang Wu | 2cad401 | 2013-09-11 14:21:47 -0700 | [diff] [blame] | 403 | if (size <= req_size) |
Wu Fengguang | 10be0b3 | 2009-06-16 15:31:36 -0700 | [diff] [blame] | 404 | return 0; |
| 405 | |
| 406 | /* |
| 407 | * starts from beginning of file: |
| 408 | * it is a strong indication of long-run stream (or whole-file-read) |
| 409 | */ |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 410 | if (size >= index) |
Wu Fengguang | 10be0b3 | 2009-06-16 15:31:36 -0700 | [diff] [blame] | 411 | size *= 2; |
| 412 | |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 413 | ra->start = index; |
Fengguang Wu | 2cad401 | 2013-09-11 14:21:47 -0700 | [diff] [blame] | 414 | ra->size = min(size + req_size, max); |
| 415 | ra->async_size = 1; |
Wu Fengguang | 10be0b3 | 2009-06-16 15:31:36 -0700 | [diff] [blame] | 416 | |
| 417 | return 1; |
| 418 | } |
| 419 | |
| 420 | /* |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 421 | * A minimal readahead algorithm for trivial sequential/random reads. |
| 422 | */ |
Matthew Wilcox (Oracle) | 9a42823 | 2020-06-01 21:46:10 -0700 | [diff] [blame] | 423 | static void ondemand_readahead(struct address_space *mapping, |
| 424 | struct file_ra_state *ra, struct file *filp, |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 425 | bool hit_readahead_marker, pgoff_t index, |
Matthew Wilcox (Oracle) | 9a42823 | 2020-06-01 21:46:10 -0700 | [diff] [blame] | 426 | unsigned long req_size) |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 427 | { |
Jens Axboe | 9491ae4 | 2016-12-12 16:43:26 -0800 | [diff] [blame] | 428 | struct backing_dev_info *bdi = inode_to_bdi(mapping->host); |
| 429 | unsigned long max_pages = ra->ra_pages; |
Markus Stockhausen | dc30b96 | 2018-07-27 09:09:53 -0600 | [diff] [blame] | 430 | unsigned long add_pages; |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 431 | pgoff_t prev_index; |
Wu Fengguang | 045a252 | 2009-06-16 15:31:33 -0700 | [diff] [blame] | 432 | |
| 433 | /* |
Jens Axboe | 9491ae4 | 2016-12-12 16:43:26 -0800 | [diff] [blame] | 434 | * If the request exceeds the readahead window, allow the read to |
| 435 | * be up to the optimal hardware IO size |
| 436 | */ |
| 437 | if (req_size > max_pages && bdi->io_pages > max_pages) |
| 438 | max_pages = min(req_size, bdi->io_pages); |
| 439 | |
| 440 | /* |
Wu Fengguang | 045a252 | 2009-06-16 15:31:33 -0700 | [diff] [blame] | 441 | * start of file |
| 442 | */ |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 443 | if (!index) |
Wu Fengguang | 045a252 | 2009-06-16 15:31:33 -0700 | [diff] [blame] | 444 | goto initial_readahead; |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 445 | |
| 446 | /* |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 447 | * It's the expected callback index, assume sequential access. |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 448 | * Ramp up sizes, and push forward the readahead window. |
| 449 | */ |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 450 | if ((index == (ra->start + ra->size - ra->async_size) || |
| 451 | index == (ra->start + ra->size))) { |
Fengguang Wu | f9acc8c | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 452 | ra->start += ra->size; |
Jens Axboe | 9491ae4 | 2016-12-12 16:43:26 -0800 | [diff] [blame] | 453 | ra->size = get_next_ra_size(ra, max_pages); |
Fengguang Wu | f9acc8c | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 454 | ra->async_size = ra->size; |
| 455 | goto readit; |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 458 | /* |
Fengguang Wu | 6b10c6c | 2007-10-16 01:24:34 -0700 | [diff] [blame] | 459 | * Hit a marked page without valid readahead state. |
| 460 | * E.g. interleaved reads. |
| 461 | * Query the pagecache for async_size, which normally equals to |
| 462 | * readahead size. Ramp it up and use it as the new readahead size. |
| 463 | */ |
| 464 | if (hit_readahead_marker) { |
| 465 | pgoff_t start; |
| 466 | |
Nick Piggin | 30002ed | 2008-07-25 19:45:28 -0700 | [diff] [blame] | 467 | rcu_read_lock(); |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 468 | start = page_cache_next_miss(mapping, index + 1, max_pages); |
Nick Piggin | 30002ed | 2008-07-25 19:45:28 -0700 | [diff] [blame] | 469 | rcu_read_unlock(); |
Fengguang Wu | 6b10c6c | 2007-10-16 01:24:34 -0700 | [diff] [blame] | 470 | |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 471 | if (!start || start - index > max_pages) |
Matthew Wilcox (Oracle) | 9a42823 | 2020-06-01 21:46:10 -0700 | [diff] [blame] | 472 | return; |
Fengguang Wu | 6b10c6c | 2007-10-16 01:24:34 -0700 | [diff] [blame] | 473 | |
| 474 | ra->start = start; |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 475 | ra->size = start - index; /* old async_size */ |
Wu Fengguang | 160334a | 2009-06-16 15:31:23 -0700 | [diff] [blame] | 476 | ra->size += req_size; |
Jens Axboe | 9491ae4 | 2016-12-12 16:43:26 -0800 | [diff] [blame] | 477 | ra->size = get_next_ra_size(ra, max_pages); |
Fengguang Wu | 6b10c6c | 2007-10-16 01:24:34 -0700 | [diff] [blame] | 478 | ra->async_size = ra->size; |
| 479 | goto readit; |
| 480 | } |
| 481 | |
| 482 | /* |
Wu Fengguang | 045a252 | 2009-06-16 15:31:33 -0700 | [diff] [blame] | 483 | * oversize read |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 484 | */ |
Jens Axboe | 9491ae4 | 2016-12-12 16:43:26 -0800 | [diff] [blame] | 485 | if (req_size > max_pages) |
Wu Fengguang | 045a252 | 2009-06-16 15:31:33 -0700 | [diff] [blame] | 486 | goto initial_readahead; |
| 487 | |
| 488 | /* |
| 489 | * sequential cache miss |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 490 | * trivial case: (index - prev_index) == 1 |
| 491 | * unaligned reads: (index - prev_index) == 0 |
Wu Fengguang | 045a252 | 2009-06-16 15:31:33 -0700 | [diff] [blame] | 492 | */ |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 493 | prev_index = (unsigned long long)ra->prev_pos >> PAGE_SHIFT; |
| 494 | if (index - prev_index <= 1UL) |
Wu Fengguang | 045a252 | 2009-06-16 15:31:33 -0700 | [diff] [blame] | 495 | goto initial_readahead; |
| 496 | |
| 497 | /* |
Wu Fengguang | 10be0b3 | 2009-06-16 15:31:36 -0700 | [diff] [blame] | 498 | * Query the page cache and look for the traces(cached history pages) |
| 499 | * that a sequential stream would leave behind. |
| 500 | */ |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 501 | if (try_context_readahead(mapping, ra, index, req_size, max_pages)) |
Wu Fengguang | 10be0b3 | 2009-06-16 15:31:36 -0700 | [diff] [blame] | 502 | goto readit; |
| 503 | |
| 504 | /* |
Wu Fengguang | 045a252 | 2009-06-16 15:31:33 -0700 | [diff] [blame] | 505 | * standalone, small random read |
| 506 | * Read as is, and do not pollute the readahead state. |
| 507 | */ |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 508 | __do_page_cache_readahead(mapping, filp, index, req_size, 0); |
Matthew Wilcox (Oracle) | 9a42823 | 2020-06-01 21:46:10 -0700 | [diff] [blame] | 509 | return; |
Wu Fengguang | 045a252 | 2009-06-16 15:31:33 -0700 | [diff] [blame] | 510 | |
| 511 | initial_readahead: |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 512 | ra->start = index; |
Jens Axboe | 9491ae4 | 2016-12-12 16:43:26 -0800 | [diff] [blame] | 513 | ra->size = get_init_ra_size(req_size, max_pages); |
Fengguang Wu | f9acc8c | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 514 | ra->async_size = ra->size > req_size ? ra->size - req_size : ra->size; |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 515 | |
Fengguang Wu | f9acc8c | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 516 | readit: |
Wu Fengguang | 51daa88 | 2009-06-16 15:31:24 -0700 | [diff] [blame] | 517 | /* |
| 518 | * Will this read hit the readahead marker made by itself? |
| 519 | * If so, trigger the readahead marker hit now, and merge |
| 520 | * the resulted next readahead window into the current one. |
Markus Stockhausen | dc30b96 | 2018-07-27 09:09:53 -0600 | [diff] [blame] | 521 | * Take care of maximum IO pages as above. |
Wu Fengguang | 51daa88 | 2009-06-16 15:31:24 -0700 | [diff] [blame] | 522 | */ |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 523 | if (index == ra->start && ra->size == ra->async_size) { |
Markus Stockhausen | dc30b96 | 2018-07-27 09:09:53 -0600 | [diff] [blame] | 524 | add_pages = get_next_ra_size(ra, max_pages); |
| 525 | if (ra->size + add_pages <= max_pages) { |
| 526 | ra->async_size = add_pages; |
| 527 | ra->size += add_pages; |
| 528 | } else { |
| 529 | ra->size = max_pages; |
| 530 | ra->async_size = max_pages >> 1; |
| 531 | } |
Wu Fengguang | 51daa88 | 2009-06-16 15:31:24 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Matthew Wilcox (Oracle) | 9a42823 | 2020-06-01 21:46:10 -0700 | [diff] [blame] | 534 | ra_submit(ra, mapping, filp); |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /** |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 538 | * page_cache_sync_readahead - generic file readahead |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 539 | * @mapping: address_space which holds the pagecache and I/O vectors |
| 540 | * @ra: file_ra_state which holds the readahead state |
| 541 | * @filp: passed on to ->readpage() and ->readpages() |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 542 | * @index: Index of first page to be read. |
| 543 | * @req_count: Total number of pages being read by the caller. |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 544 | * |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 545 | * page_cache_sync_readahead() should be called when a cache miss happened: |
| 546 | * it will submit the read. The readahead logic may decide to piggyback more |
| 547 | * pages onto the read request if access patterns suggest it will improve |
| 548 | * performance. |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 549 | */ |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 550 | void page_cache_sync_readahead(struct address_space *mapping, |
| 551 | struct file_ra_state *ra, struct file *filp, |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 552 | pgoff_t index, unsigned long req_count) |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 553 | { |
| 554 | /* no read-ahead */ |
| 555 | if (!ra->ra_pages) |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 556 | return; |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 557 | |
Josef Bacik | ca47e8c | 2018-07-03 11:15:03 -0400 | [diff] [blame] | 558 | if (blk_cgroup_congested()) |
| 559 | return; |
| 560 | |
Wu Fengguang | 0141450 | 2010-03-05 13:42:03 -0800 | [diff] [blame] | 561 | /* be dumb */ |
Wu Fengguang | 70655c0 | 2010-04-06 14:34:53 -0700 | [diff] [blame] | 562 | if (filp && (filp->f_mode & FMODE_RANDOM)) { |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 563 | force_page_cache_readahead(mapping, filp, index, req_count); |
Wu Fengguang | 0141450 | 2010-03-05 13:42:03 -0800 | [diff] [blame] | 564 | return; |
| 565 | } |
| 566 | |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 567 | /* do read-ahead */ |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 568 | ondemand_readahead(mapping, ra, filp, false, index, req_count); |
Fengguang Wu | 122a21d | 2007-07-19 01:48:01 -0700 | [diff] [blame] | 569 | } |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 570 | EXPORT_SYMBOL_GPL(page_cache_sync_readahead); |
| 571 | |
| 572 | /** |
| 573 | * page_cache_async_readahead - file readahead for marked pages |
| 574 | * @mapping: address_space which holds the pagecache and I/O vectors |
| 575 | * @ra: file_ra_state which holds the readahead state |
| 576 | * @filp: passed on to ->readpage() and ->readpages() |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 577 | * @page: The page at @index which triggered the readahead call. |
| 578 | * @index: Index of first page to be read. |
| 579 | * @req_count: Total number of pages being read by the caller. |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 580 | * |
Huang Shijie | bf8abe8 | 2010-05-24 14:32:36 -0700 | [diff] [blame] | 581 | * page_cache_async_readahead() should be called when a page is used which |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 582 | * is marked as PageReadahead; this is a marker to suggest that the application |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 583 | * has used up enough of the readahead window that we should start pulling in |
Randy Dunlap | f7850d9 | 2008-03-19 17:01:02 -0700 | [diff] [blame] | 584 | * more pages. |
| 585 | */ |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 586 | void |
| 587 | page_cache_async_readahead(struct address_space *mapping, |
| 588 | struct file_ra_state *ra, struct file *filp, |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 589 | struct page *page, pgoff_t index, |
| 590 | unsigned long req_count) |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 591 | { |
| 592 | /* no read-ahead */ |
| 593 | if (!ra->ra_pages) |
| 594 | return; |
| 595 | |
| 596 | /* |
| 597 | * Same bit is used for PG_readahead and PG_reclaim. |
| 598 | */ |
| 599 | if (PageWriteback(page)) |
| 600 | return; |
| 601 | |
| 602 | ClearPageReadahead(page); |
| 603 | |
| 604 | /* |
| 605 | * Defer asynchronous read-ahead on IO congestion. |
| 606 | */ |
Tejun Heo | 703c270 | 2015-05-22 17:13:44 -0400 | [diff] [blame] | 607 | if (inode_read_congested(mapping->host)) |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 608 | return; |
| 609 | |
Josef Bacik | ca47e8c | 2018-07-03 11:15:03 -0400 | [diff] [blame] | 610 | if (blk_cgroup_congested()) |
| 611 | return; |
| 612 | |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 613 | /* do read-ahead */ |
Matthew Wilcox (Oracle) | 08eb965 | 2020-06-01 21:46:29 -0700 | [diff] [blame] | 614 | ondemand_readahead(mapping, ra, filp, true, index, req_count); |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 615 | } |
| 616 | EXPORT_SYMBOL_GPL(page_cache_async_readahead); |
Cong Wang | 782182e | 2012-05-29 15:06:43 -0700 | [diff] [blame] | 617 | |
Dominik Brodowski | c7b95d5 | 2018-03-19 17:51:36 +0100 | [diff] [blame] | 618 | ssize_t ksys_readahead(int fd, loff_t offset, size_t count) |
Cong Wang | 782182e | 2012-05-29 15:06:43 -0700 | [diff] [blame] | 619 | { |
| 620 | ssize_t ret; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 621 | struct fd f; |
Cong Wang | 782182e | 2012-05-29 15:06:43 -0700 | [diff] [blame] | 622 | |
| 623 | ret = -EBADF; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 624 | f = fdget(fd); |
Amir Goldstein | 3d8f761 | 2018-08-29 08:41:29 +0300 | [diff] [blame] | 625 | if (!f.file || !(f.file->f_mode & FMODE_READ)) |
| 626 | goto out; |
| 627 | |
| 628 | /* |
| 629 | * The readahead() syscall is intended to run only on files |
| 630 | * that can execute readahead. If readahead is not possible |
| 631 | * on this file, then we must return -EINVAL. |
| 632 | */ |
| 633 | ret = -EINVAL; |
| 634 | if (!f.file->f_mapping || !f.file->f_mapping->a_ops || |
| 635 | !S_ISREG(file_inode(f.file)->i_mode)) |
| 636 | goto out; |
| 637 | |
| 638 | ret = vfs_fadvise(f.file, offset, count, POSIX_FADV_WILLNEED); |
| 639 | out: |
| 640 | fdput(f); |
Cong Wang | 782182e | 2012-05-29 15:06:43 -0700 | [diff] [blame] | 641 | return ret; |
| 642 | } |
Dominik Brodowski | c7b95d5 | 2018-03-19 17:51:36 +0100 | [diff] [blame] | 643 | |
| 644 | SYSCALL_DEFINE3(readahead, int, fd, loff_t, offset, size_t, count) |
| 645 | { |
| 646 | return ksys_readahead(fd, offset, count); |
| 647 | } |