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 | * linux/mm/filemap.c |
| 4 | * |
| 5 | * Copyright (C) 1994-1999 Linus Torvalds |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * This file handles the generic file mmap semantics used by |
| 10 | * most "normal" filesystems (but you don't /have/ to use this: |
| 11 | * the NFS filesystem used to do this differently, for example) |
| 12 | */ |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 13 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/compiler.h> |
Ross Zwisler | f9fe48b | 2016-01-22 15:10:40 -0800 | [diff] [blame] | 15 | #include <linux/dax.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/fs.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 17 | #include <linux/sched/signal.h> |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 18 | #include <linux/uaccess.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 19 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/kernel_stat.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/mm.h> |
| 23 | #include <linux/swap.h> |
| 24 | #include <linux/mman.h> |
| 25 | #include <linux/pagemap.h> |
| 26 | #include <linux/file.h> |
| 27 | #include <linux/uio.h> |
Josef Bacik | cfcbfb1 | 2019-05-13 17:21:04 -0700 | [diff] [blame] | 28 | #include <linux/error-injection.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/hash.h> |
| 30 | #include <linux/writeback.h> |
Linus Torvalds | 5325338 | 2007-10-18 14:47:32 -0700 | [diff] [blame] | 31 | #include <linux/backing-dev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/pagevec.h> |
| 33 | #include <linux/blkdev.h> |
| 34 | #include <linux/security.h> |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 35 | #include <linux/cpuset.h> |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 36 | #include <linux/hugetlb.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 37 | #include <linux/memcontrol.h> |
Dan Magenheimer | c515e1f | 2011-05-26 10:01:43 -0600 | [diff] [blame] | 38 | #include <linux/cleancache.h> |
Mel Gorman | c7df8ad | 2017-11-15 17:37:41 -0800 | [diff] [blame] | 39 | #include <linux/shmem_fs.h> |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 40 | #include <linux/rmap.h> |
Johannes Weiner | b1d29ba | 2018-10-26 15:06:08 -0700 | [diff] [blame] | 41 | #include <linux/delayacct.h> |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 42 | #include <linux/psi.h> |
Ben Dooks | d0e6a58 | 2019-10-18 20:20:20 -0700 | [diff] [blame] | 43 | #include <linux/ramfs.h> |
Nick Piggin | 0f8053a | 2006-03-22 00:08:33 -0800 | [diff] [blame] | 44 | #include "internal.h" |
| 45 | |
Robert Jarzmik | fe0bfaa | 2013-04-29 15:06:10 -0700 | [diff] [blame] | 46 | #define CREATE_TRACE_POINTS |
| 47 | #include <trace/events/filemap.h> |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | * FIXME: remove all knowledge of the buffer layer from the core VM |
| 51 | */ |
Jan Kara | 148f948 | 2009-08-17 19:52:36 +0200 | [diff] [blame] | 52 | #include <linux/buffer_head.h> /* for try_to_free_buffers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #include <asm/mman.h> |
| 55 | |
| 56 | /* |
| 57 | * Shared mappings implemented 30.11.1994. It's not fully working yet, |
| 58 | * though. |
| 59 | * |
| 60 | * Shared mappings now work. 15.8.1995 Bruno. |
| 61 | * |
| 62 | * finished 'unifying' the page and buffer cache and SMP-threaded the |
| 63 | * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com> |
| 64 | * |
| 65 | * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de> |
| 66 | */ |
| 67 | |
| 68 | /* |
| 69 | * Lock ordering: |
| 70 | * |
Davidlohr Bueso | c8c06ef | 2014-12-12 16:54:24 -0800 | [diff] [blame] | 71 | * ->i_mmap_rwsem (truncate_pagecache) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | * ->private_lock (__free_pte->__set_page_dirty_buffers) |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 73 | * ->swap_lock (exclusive_swap_page, others) |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 74 | * ->i_pages lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 76 | * ->i_mutex |
Davidlohr Bueso | c8c06ef | 2014-12-12 16:54:24 -0800 | [diff] [blame] | 77 | * ->i_mmap_rwsem (truncate->unmap_mapping_range) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 79 | * ->mmap_lock |
Davidlohr Bueso | c8c06ef | 2014-12-12 16:54:24 -0800 | [diff] [blame] | 80 | * ->i_mmap_rwsem |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 81 | * ->page_table_lock or pte_lock (various, mainly in memory.c) |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 82 | * ->i_pages lock (arch-dependent flush_dcache_mmap_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 84 | * ->mmap_lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | * ->lock_page (access_process_vm) |
| 86 | * |
Al Viro | ccad236 | 2014-02-11 22:36:48 -0500 | [diff] [blame] | 87 | * ->i_mutex (generic_perform_write) |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 88 | * ->mmap_lock (fault_in_pages_readable->do_page_fault) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | * |
Christoph Hellwig | f758eea | 2011-04-21 18:19:44 -0600 | [diff] [blame] | 90 | * bdi->wb.list_lock |
Dave Chinner | a66979a | 2011-03-22 22:23:41 +1100 | [diff] [blame] | 91 | * sb_lock (fs/fs-writeback.c) |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 92 | * ->i_pages lock (__sync_single_inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | * |
Davidlohr Bueso | c8c06ef | 2014-12-12 16:54:24 -0800 | [diff] [blame] | 94 | * ->i_mmap_rwsem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | * ->anon_vma.lock (vma_adjust) |
| 96 | * |
| 97 | * ->anon_vma.lock |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 98 | * ->page_table_lock or pte_lock (anon_vma_prepare and various) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | * |
Hugh Dickins | b8072f0 | 2005-10-29 18:16:41 -0700 | [diff] [blame] | 100 | * ->page_table_lock or pte_lock |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 101 | * ->swap_lock (try_to_unmap_one) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | * ->private_lock (try_to_unmap_one) |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 103 | * ->i_pages lock (try_to_unmap_one) |
Andrey Ryabinin | f4b7e27 | 2019-03-05 15:49:39 -0800 | [diff] [blame] | 104 | * ->pgdat->lru_lock (follow_page->mark_page_accessed) |
| 105 | * ->pgdat->lru_lock (check_pte_range->isolate_lru_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | * ->private_lock (page_remove_rmap->set_page_dirty) |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 107 | * ->i_pages lock (page_remove_rmap->set_page_dirty) |
Christoph Hellwig | f758eea | 2011-04-21 18:19:44 -0600 | [diff] [blame] | 108 | * bdi.wb->list_lock (page_remove_rmap->set_page_dirty) |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 109 | * ->inode->i_lock (page_remove_rmap->set_page_dirty) |
Johannes Weiner | 81f8c3a | 2016-03-15 14:57:04 -0700 | [diff] [blame] | 110 | * ->memcg->move_lock (page_remove_rmap->lock_page_memcg) |
Christoph Hellwig | f758eea | 2011-04-21 18:19:44 -0600 | [diff] [blame] | 111 | * bdi.wb->list_lock (zap_pte_range->set_page_dirty) |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 112 | * ->inode->i_lock (zap_pte_range->set_page_dirty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | * ->private_lock (zap_pte_range->__set_page_dirty_buffers) |
| 114 | * |
Davidlohr Bueso | c8c06ef | 2014-12-12 16:54:24 -0800 | [diff] [blame] | 115 | * ->i_mmap_rwsem |
Andi Kleen | 9a3c531 | 2012-03-21 16:34:09 -0700 | [diff] [blame] | 116 | * ->tasklist_lock (memory_failure, collect_procs_ao) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | */ |
| 118 | |
Matthew Wilcox | 5c024e6a | 2017-11-21 09:17:59 -0500 | [diff] [blame] | 119 | static void page_cache_delete(struct address_space *mapping, |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 120 | struct page *page, void *shadow) |
| 121 | { |
Matthew Wilcox | 5c024e6a | 2017-11-21 09:17:59 -0500 | [diff] [blame] | 122 | XA_STATE(xas, &mapping->i_pages, page->index); |
| 123 | unsigned int nr = 1; |
Kirill A. Shutemov | c70b647 | 2016-12-12 16:43:17 -0800 | [diff] [blame] | 124 | |
Matthew Wilcox | 5c024e6a | 2017-11-21 09:17:59 -0500 | [diff] [blame] | 125 | mapping_set_update(&xas, mapping); |
| 126 | |
| 127 | /* hugetlb pages are represented by a single entry in the xarray */ |
| 128 | if (!PageHuge(page)) { |
| 129 | xas_set_order(&xas, page->index, compound_order(page)); |
Matthew Wilcox (Oracle) | d8c6546 | 2019-09-23 15:34:30 -0700 | [diff] [blame] | 130 | nr = compound_nr(page); |
Matthew Wilcox | 5c024e6a | 2017-11-21 09:17:59 -0500 | [diff] [blame] | 131 | } |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 132 | |
Kirill A. Shutemov | 8392937 | 2016-07-26 15:26:04 -0700 | [diff] [blame] | 133 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 134 | VM_BUG_ON_PAGE(PageTail(page), page); |
| 135 | VM_BUG_ON_PAGE(nr != 1 && shadow, page); |
Johannes Weiner | 449dd69 | 2014-04-03 14:47:56 -0700 | [diff] [blame] | 136 | |
Matthew Wilcox | 5c024e6a | 2017-11-21 09:17:59 -0500 | [diff] [blame] | 137 | xas_store(&xas, shadow); |
| 138 | xas_init_marks(&xas); |
Johannes Weiner | d3798ae | 2016-10-04 22:02:08 +0200 | [diff] [blame] | 139 | |
Jan Kara | 2300638 | 2017-11-15 17:37:26 -0800 | [diff] [blame] | 140 | page->mapping = NULL; |
| 141 | /* Leave page->index set: truncation lookup relies upon it */ |
| 142 | |
Johannes Weiner | d3798ae | 2016-10-04 22:02:08 +0200 | [diff] [blame] | 143 | if (shadow) { |
| 144 | mapping->nrexceptional += nr; |
| 145 | /* |
| 146 | * Make sure the nrexceptional update is committed before |
| 147 | * the nrpages update so that final truncate racing |
| 148 | * with reclaim does not see both counters 0 at the |
| 149 | * same time and miss a shadow entry. |
| 150 | */ |
| 151 | smp_wmb(); |
| 152 | } |
| 153 | mapping->nrpages -= nr; |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Jan Kara | 5ecc4d8 | 2017-11-15 17:37:29 -0800 | [diff] [blame] | 156 | static void unaccount_page_cache_page(struct address_space *mapping, |
| 157 | struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
Jan Kara | 5ecc4d8 | 2017-11-15 17:37:29 -0800 | [diff] [blame] | 159 | int nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Dan Magenheimer | c515e1f | 2011-05-26 10:01:43 -0600 | [diff] [blame] | 161 | /* |
| 162 | * if we're uptodate, flush out into the cleancache, otherwise |
| 163 | * invalidate any existing cleancache entries. We can't leave |
| 164 | * stale data around in the cleancache once our page is gone |
| 165 | */ |
| 166 | if (PageUptodate(page) && PageMappedToDisk(page)) |
| 167 | cleancache_put_page(page); |
| 168 | else |
Dan Magenheimer | 3167760 | 2011-09-21 11:56:28 -0400 | [diff] [blame] | 169 | cleancache_invalidate_page(mapping, page); |
Dan Magenheimer | c515e1f | 2011-05-26 10:01:43 -0600 | [diff] [blame] | 170 | |
Kirill A. Shutemov | 8392937 | 2016-07-26 15:26:04 -0700 | [diff] [blame] | 171 | VM_BUG_ON_PAGE(PageTail(page), page); |
Hugh Dickins | 06b241f | 2016-03-09 14:08:07 -0800 | [diff] [blame] | 172 | VM_BUG_ON_PAGE(page_mapped(page), page); |
| 173 | if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) { |
| 174 | int mapcount; |
| 175 | |
| 176 | pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n", |
| 177 | current->comm, page_to_pfn(page)); |
| 178 | dump_page(page, "still mapped when deleted"); |
| 179 | dump_stack(); |
| 180 | add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); |
| 181 | |
| 182 | mapcount = page_mapcount(page); |
| 183 | if (mapping_exiting(mapping) && |
| 184 | page_count(page) >= mapcount + 2) { |
| 185 | /* |
| 186 | * All vmas have already been torn down, so it's |
| 187 | * a good bet that actually the page is unmapped, |
| 188 | * and we'd prefer not to leak it: if we're wrong, |
| 189 | * some other bad page check should catch it later. |
| 190 | */ |
| 191 | page_mapcount_reset(page); |
Joonsoo Kim | 6d061f9 | 2016-05-19 17:10:46 -0700 | [diff] [blame] | 192 | page_ref_sub(page, mapcount); |
Hugh Dickins | 06b241f | 2016-03-09 14:08:07 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Jan Kara | 76253fb | 2017-11-15 17:37:22 -0800 | [diff] [blame] | 196 | /* hugetlb pages do not participate in page cache accounting. */ |
Jan Kara | 5ecc4d8 | 2017-11-15 17:37:29 -0800 | [diff] [blame] | 197 | if (PageHuge(page)) |
| 198 | return; |
Jan Kara | 76253fb | 2017-11-15 17:37:22 -0800 | [diff] [blame] | 199 | |
Jan Kara | 5ecc4d8 | 2017-11-15 17:37:29 -0800 | [diff] [blame] | 200 | nr = hpage_nr_pages(page); |
| 201 | |
Johannes Weiner | 0d1c207 | 2020-06-03 16:01:54 -0700 | [diff] [blame] | 202 | __mod_lruvec_page_state(page, NR_FILE_PAGES, -nr); |
Jan Kara | 5ecc4d8 | 2017-11-15 17:37:29 -0800 | [diff] [blame] | 203 | if (PageSwapBacked(page)) { |
Johannes Weiner | 0d1c207 | 2020-06-03 16:01:54 -0700 | [diff] [blame] | 204 | __mod_lruvec_page_state(page, NR_SHMEM, -nr); |
Jan Kara | 5ecc4d8 | 2017-11-15 17:37:29 -0800 | [diff] [blame] | 205 | if (PageTransHuge(page)) |
| 206 | __dec_node_page_state(page, NR_SHMEM_THPS); |
Song Liu | 99cb0db | 2019-09-23 15:38:00 -0700 | [diff] [blame] | 207 | } else if (PageTransHuge(page)) { |
| 208 | __dec_node_page_state(page, NR_FILE_THPS); |
Song Liu | 09d91cd | 2019-09-23 15:38:03 -0700 | [diff] [blame] | 209 | filemap_nr_thps_dec(mapping); |
Jan Kara | 76253fb | 2017-11-15 17:37:22 -0800 | [diff] [blame] | 210 | } |
Jan Kara | 5ecc4d8 | 2017-11-15 17:37:29 -0800 | [diff] [blame] | 211 | |
| 212 | /* |
| 213 | * At this point page must be either written or cleaned by |
| 214 | * truncate. Dirty page here signals a bug and loss of |
| 215 | * unwritten data. |
| 216 | * |
| 217 | * This fixes dirty accounting after removing the page entirely |
| 218 | * but leaves PageDirty set: it has no effect for truncated |
| 219 | * page and anyway will be cleared before returning page into |
| 220 | * buddy allocator. |
| 221 | */ |
| 222 | if (WARN_ON_ONCE(PageDirty(page))) |
| 223 | account_page_cleaned(page, mapping, inode_to_wb(mapping->host)); |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Delete a page from the page cache and free it. Caller has to make |
| 228 | * sure the page is locked and that nobody else uses it - or that usage |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 229 | * is safe. The caller must hold the i_pages lock. |
Jan Kara | 5ecc4d8 | 2017-11-15 17:37:29 -0800 | [diff] [blame] | 230 | */ |
| 231 | void __delete_from_page_cache(struct page *page, void *shadow) |
| 232 | { |
| 233 | struct address_space *mapping = page->mapping; |
| 234 | |
| 235 | trace_mm_filemap_delete_from_page_cache(page); |
| 236 | |
| 237 | unaccount_page_cache_page(mapping, page); |
Matthew Wilcox | 5c024e6a | 2017-11-21 09:17:59 -0500 | [diff] [blame] | 238 | page_cache_delete(mapping, page, shadow); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Jan Kara | 59c66c5 | 2017-11-15 17:37:18 -0800 | [diff] [blame] | 241 | static void page_cache_free_page(struct address_space *mapping, |
| 242 | struct page *page) |
| 243 | { |
| 244 | void (*freepage)(struct page *); |
| 245 | |
| 246 | freepage = mapping->a_ops->freepage; |
| 247 | if (freepage) |
| 248 | freepage(page); |
| 249 | |
| 250 | if (PageTransHuge(page) && !PageHuge(page)) { |
| 251 | page_ref_sub(page, HPAGE_PMD_NR); |
| 252 | VM_BUG_ON_PAGE(page_count(page) <= 0, page); |
| 253 | } else { |
| 254 | put_page(page); |
| 255 | } |
| 256 | } |
| 257 | |
Minchan Kim | 702cfbf | 2011-03-22 16:32:43 -0700 | [diff] [blame] | 258 | /** |
| 259 | * delete_from_page_cache - delete page from page cache |
| 260 | * @page: the page which the kernel is trying to remove from page cache |
| 261 | * |
| 262 | * This must be called only on pages that have been verified to be in the page |
| 263 | * cache and locked. It will never put the page into the free list, the caller |
| 264 | * has a reference on the page. |
| 265 | */ |
| 266 | void delete_from_page_cache(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | { |
Kirill A. Shutemov | 8392937 | 2016-07-26 15:26:04 -0700 | [diff] [blame] | 268 | struct address_space *mapping = page_mapping(page); |
Greg Thelen | c4843a7 | 2015-05-22 17:13:16 -0400 | [diff] [blame] | 269 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Matt Mackall | cd7619d | 2005-05-01 08:59:01 -0700 | [diff] [blame] | 271 | BUG_ON(!PageLocked(page)); |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 272 | xa_lock_irqsave(&mapping->i_pages, flags); |
Johannes Weiner | 62cccb8 | 2016-03-15 14:57:22 -0700 | [diff] [blame] | 273 | __delete_from_page_cache(page, NULL); |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 274 | xa_unlock_irqrestore(&mapping->i_pages, flags); |
Linus Torvalds | 6072d13 | 2010-12-01 13:35:19 -0500 | [diff] [blame] | 275 | |
Jan Kara | 59c66c5 | 2017-11-15 17:37:18 -0800 | [diff] [blame] | 276 | page_cache_free_page(mapping, page); |
Minchan Kim | 97cecb5 | 2011-03-22 16:30:53 -0700 | [diff] [blame] | 277 | } |
| 278 | EXPORT_SYMBOL(delete_from_page_cache); |
| 279 | |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 280 | /* |
Matthew Wilcox | ef8e571 | 2017-12-04 03:59:45 -0500 | [diff] [blame] | 281 | * page_cache_delete_batch - delete several pages from page cache |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 282 | * @mapping: the mapping to which pages belong |
| 283 | * @pvec: pagevec with pages to delete |
| 284 | * |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 285 | * The function walks over mapping->i_pages and removes pages passed in @pvec |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 286 | * from the mapping. The function expects @pvec to be sorted by page index |
| 287 | * and is optimised for it to be dense. |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 288 | * It tolerates holes in @pvec (mapping entries at those indices are not |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 289 | * modified). The function expects only THP head pages to be present in the |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 290 | * @pvec. |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 291 | * |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 292 | * The function expects the i_pages lock to be held. |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 293 | */ |
Matthew Wilcox | ef8e571 | 2017-12-04 03:59:45 -0500 | [diff] [blame] | 294 | static void page_cache_delete_batch(struct address_space *mapping, |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 295 | struct pagevec *pvec) |
| 296 | { |
Matthew Wilcox | ef8e571 | 2017-12-04 03:59:45 -0500 | [diff] [blame] | 297 | XA_STATE(xas, &mapping->i_pages, pvec->pages[0]->index); |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 298 | int total_pages = 0; |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 299 | int i = 0; |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 300 | struct page *page; |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 301 | |
Matthew Wilcox | ef8e571 | 2017-12-04 03:59:45 -0500 | [diff] [blame] | 302 | mapping_set_update(&xas, mapping); |
| 303 | xas_for_each(&xas, page, ULONG_MAX) { |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 304 | if (i >= pagevec_count(pvec)) |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 305 | break; |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 306 | |
| 307 | /* A swap/dax/shadow entry got inserted? Skip it. */ |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 308 | if (xa_is_value(page)) |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 309 | continue; |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 310 | /* |
| 311 | * A page got inserted in our range? Skip it. We have our |
| 312 | * pages locked so they are protected from being removed. |
| 313 | * If we see a page whose index is higher than ours, it |
| 314 | * means our page has been removed, which shouldn't be |
| 315 | * possible because we're holding the PageLock. |
| 316 | */ |
| 317 | if (page != pvec->pages[i]) { |
| 318 | VM_BUG_ON_PAGE(page->index > pvec->pages[i]->index, |
| 319 | page); |
| 320 | continue; |
Linus Torvalds | 69bf4b6 | 2019-07-05 19:55:18 -0700 | [diff] [blame] | 321 | } |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 322 | |
| 323 | WARN_ON_ONCE(!PageLocked(page)); |
| 324 | |
| 325 | if (page->index == xas.xa_index) |
| 326 | page->mapping = NULL; |
| 327 | /* Leave page->index set: truncation lookup relies on it */ |
| 328 | |
| 329 | /* |
| 330 | * Move to the next page in the vector if this is a regular |
| 331 | * page or the index is of the last sub-page of this compound |
| 332 | * page. |
| 333 | */ |
| 334 | if (page->index + compound_nr(page) - 1 == xas.xa_index) |
| 335 | i++; |
Matthew Wilcox | ef8e571 | 2017-12-04 03:59:45 -0500 | [diff] [blame] | 336 | xas_store(&xas, NULL); |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 337 | total_pages++; |
| 338 | } |
| 339 | mapping->nrpages -= total_pages; |
| 340 | } |
| 341 | |
| 342 | void delete_from_page_cache_batch(struct address_space *mapping, |
| 343 | struct pagevec *pvec) |
| 344 | { |
| 345 | int i; |
| 346 | unsigned long flags; |
| 347 | |
| 348 | if (!pagevec_count(pvec)) |
| 349 | return; |
| 350 | |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 351 | xa_lock_irqsave(&mapping->i_pages, flags); |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 352 | for (i = 0; i < pagevec_count(pvec); i++) { |
| 353 | trace_mm_filemap_delete_from_page_cache(pvec->pages[i]); |
| 354 | |
| 355 | unaccount_page_cache_page(mapping, pvec->pages[i]); |
| 356 | } |
Matthew Wilcox | ef8e571 | 2017-12-04 03:59:45 -0500 | [diff] [blame] | 357 | page_cache_delete_batch(mapping, pvec); |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 358 | xa_unlock_irqrestore(&mapping->i_pages, flags); |
Jan Kara | aa65c29 | 2017-11-15 17:37:33 -0800 | [diff] [blame] | 359 | |
| 360 | for (i = 0; i < pagevec_count(pvec); i++) |
| 361 | page_cache_free_page(mapping, pvec->pages[i]); |
| 362 | } |
| 363 | |
Miklos Szeredi | d72d9e2 | 2016-07-29 14:10:57 +0200 | [diff] [blame] | 364 | int filemap_check_errors(struct address_space *mapping) |
Dmitry Monakhov | 865ffef3 | 2013-04-29 15:08:42 -0700 | [diff] [blame] | 365 | { |
| 366 | int ret = 0; |
| 367 | /* Check for outstanding write errors */ |
Jens Axboe | 7fcbbaf | 2014-05-22 11:54:16 -0700 | [diff] [blame] | 368 | if (test_bit(AS_ENOSPC, &mapping->flags) && |
| 369 | test_and_clear_bit(AS_ENOSPC, &mapping->flags)) |
Dmitry Monakhov | 865ffef3 | 2013-04-29 15:08:42 -0700 | [diff] [blame] | 370 | ret = -ENOSPC; |
Jens Axboe | 7fcbbaf | 2014-05-22 11:54:16 -0700 | [diff] [blame] | 371 | if (test_bit(AS_EIO, &mapping->flags) && |
| 372 | test_and_clear_bit(AS_EIO, &mapping->flags)) |
Dmitry Monakhov | 865ffef3 | 2013-04-29 15:08:42 -0700 | [diff] [blame] | 373 | ret = -EIO; |
| 374 | return ret; |
| 375 | } |
Miklos Szeredi | d72d9e2 | 2016-07-29 14:10:57 +0200 | [diff] [blame] | 376 | EXPORT_SYMBOL(filemap_check_errors); |
Dmitry Monakhov | 865ffef3 | 2013-04-29 15:08:42 -0700 | [diff] [blame] | 377 | |
Jeff Layton | 76341ca | 2017-07-06 07:02:22 -0400 | [diff] [blame] | 378 | static int filemap_check_and_keep_errors(struct address_space *mapping) |
| 379 | { |
| 380 | /* Check for outstanding write errors */ |
| 381 | if (test_bit(AS_EIO, &mapping->flags)) |
| 382 | return -EIO; |
| 383 | if (test_bit(AS_ENOSPC, &mapping->flags)) |
| 384 | return -ENOSPC; |
| 385 | return 0; |
| 386 | } |
| 387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | /** |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 389 | * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 390 | * @mapping: address space structure to write |
| 391 | * @start: offset in bytes where the range starts |
Andrew Morton | 469eb4d | 2006-03-24 03:17:45 -0800 | [diff] [blame] | 392 | * @end: offset in bytes where the range ends (inclusive) |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 393 | * @sync_mode: enable synchronous operation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | * |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 395 | * Start writeback against all of a mapping's dirty pages that lie |
| 396 | * within the byte offsets <start, end> inclusive. |
| 397 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 399 | * opposed to a regular memory cleansing writeback. The difference between |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | * these two operations is that if a dirty page/buffer is encountered, it must |
| 401 | * be waited upon, and not just skipped over. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 402 | * |
| 403 | * Return: %0 on success, negative error code otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | */ |
Andrew Morton | ebcf28e | 2006-03-24 03:18:04 -0800 | [diff] [blame] | 405 | int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, |
| 406 | loff_t end, int sync_mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
| 408 | int ret; |
| 409 | struct writeback_control wbc = { |
| 410 | .sync_mode = sync_mode, |
Nick Piggin | 05fe478 | 2009-01-06 14:39:08 -0800 | [diff] [blame] | 411 | .nr_to_write = LONG_MAX, |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 412 | .range_start = start, |
| 413 | .range_end = end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | }; |
| 415 | |
Konstantin Khlebnikov | c3aab9a0 | 2019-09-23 15:34:45 -0700 | [diff] [blame] | 416 | if (!mapping_cap_writeback_dirty(mapping) || |
| 417 | !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | return 0; |
| 419 | |
Tejun Heo | b16b1de | 2015-06-02 08:39:48 -0600 | [diff] [blame] | 420 | wbc_attach_fdatawrite_inode(&wbc, mapping->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | ret = do_writepages(mapping, &wbc); |
Tejun Heo | b16b1de | 2015-06-02 08:39:48 -0600 | [diff] [blame] | 422 | wbc_detach_inode(&wbc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | static inline int __filemap_fdatawrite(struct address_space *mapping, |
| 427 | int sync_mode) |
| 428 | { |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 429 | return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | int filemap_fdatawrite(struct address_space *mapping) |
| 433 | { |
| 434 | return __filemap_fdatawrite(mapping, WB_SYNC_ALL); |
| 435 | } |
| 436 | EXPORT_SYMBOL(filemap_fdatawrite); |
| 437 | |
Jan Kara | f4c0a0f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 438 | int filemap_fdatawrite_range(struct address_space *mapping, loff_t start, |
Andrew Morton | ebcf28e | 2006-03-24 03:18:04 -0800 | [diff] [blame] | 439 | loff_t end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
| 441 | return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL); |
| 442 | } |
Jan Kara | f4c0a0f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 443 | EXPORT_SYMBOL(filemap_fdatawrite_range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 445 | /** |
| 446 | * filemap_flush - mostly a non-blocking flush |
| 447 | * @mapping: target address_space |
| 448 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | * This is a mostly non-blocking flush. Not suitable for data-integrity |
| 450 | * purposes - I/O may not be started against all dirty pages. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 451 | * |
| 452 | * Return: %0 on success, negative error code otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | */ |
| 454 | int filemap_flush(struct address_space *mapping) |
| 455 | { |
| 456 | return __filemap_fdatawrite(mapping, WB_SYNC_NONE); |
| 457 | } |
| 458 | EXPORT_SYMBOL(filemap_flush); |
| 459 | |
Goldwyn Rodrigues | 7fc9e47 | 2017-06-20 07:05:41 -0500 | [diff] [blame] | 460 | /** |
| 461 | * filemap_range_has_page - check if a page exists in range. |
| 462 | * @mapping: address space within which to check |
| 463 | * @start_byte: offset in bytes where the range starts |
| 464 | * @end_byte: offset in bytes where the range ends (inclusive) |
| 465 | * |
| 466 | * Find at least one page in the range supplied, usually used to check if |
| 467 | * direct writing in this range will trigger a writeback. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 468 | * |
| 469 | * Return: %true if at least one page exists in the specified range, |
| 470 | * %false otherwise. |
Goldwyn Rodrigues | 7fc9e47 | 2017-06-20 07:05:41 -0500 | [diff] [blame] | 471 | */ |
| 472 | bool filemap_range_has_page(struct address_space *mapping, |
| 473 | loff_t start_byte, loff_t end_byte) |
| 474 | { |
Jan Kara | f7b6804 | 2017-09-06 16:21:40 -0700 | [diff] [blame] | 475 | struct page *page; |
Matthew Wilcox | 8fa8e53 | 2018-01-16 06:26:49 -0500 | [diff] [blame] | 476 | XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT); |
| 477 | pgoff_t max = end_byte >> PAGE_SHIFT; |
Goldwyn Rodrigues | 7fc9e47 | 2017-06-20 07:05:41 -0500 | [diff] [blame] | 478 | |
| 479 | if (end_byte < start_byte) |
| 480 | return false; |
| 481 | |
Matthew Wilcox | 8fa8e53 | 2018-01-16 06:26:49 -0500 | [diff] [blame] | 482 | rcu_read_lock(); |
| 483 | for (;;) { |
| 484 | page = xas_find(&xas, max); |
| 485 | if (xas_retry(&xas, page)) |
| 486 | continue; |
| 487 | /* Shadow entries don't count */ |
| 488 | if (xa_is_value(page)) |
| 489 | continue; |
| 490 | /* |
| 491 | * We don't need to try to pin this page; we're about to |
| 492 | * release the RCU lock anyway. It is enough to know that |
| 493 | * there was a page here recently. |
| 494 | */ |
| 495 | break; |
| 496 | } |
| 497 | rcu_read_unlock(); |
Goldwyn Rodrigues | 7fc9e47 | 2017-06-20 07:05:41 -0500 | [diff] [blame] | 498 | |
Matthew Wilcox | 8fa8e53 | 2018-01-16 06:26:49 -0500 | [diff] [blame] | 499 | return page != NULL; |
Goldwyn Rodrigues | 7fc9e47 | 2017-06-20 07:05:41 -0500 | [diff] [blame] | 500 | } |
| 501 | EXPORT_SYMBOL(filemap_range_has_page); |
| 502 | |
Jeff Layton | 5e8fcc1 | 2017-07-06 07:02:24 -0400 | [diff] [blame] | 503 | static void __filemap_fdatawait_range(struct address_space *mapping, |
Junichi Nomura | aa750fd | 2015-11-05 18:47:23 -0800 | [diff] [blame] | 504 | loff_t start_byte, loff_t end_byte) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 506 | pgoff_t index = start_byte >> PAGE_SHIFT; |
| 507 | pgoff_t end = end_byte >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | struct pagevec pvec; |
| 509 | int nr_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Christoph Hellwig | 94004ed | 2009-09-30 22:16:33 +0200 | [diff] [blame] | 511 | if (end_byte < start_byte) |
Jeff Layton | 5e8fcc1 | 2017-07-06 07:02:24 -0400 | [diff] [blame] | 512 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 514 | pagevec_init(&pvec); |
Jan Kara | 312e9d2 | 2017-11-15 17:35:05 -0800 | [diff] [blame] | 515 | while (index <= end) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | unsigned i; |
| 517 | |
Jan Kara | 312e9d2 | 2017-11-15 17:35:05 -0800 | [diff] [blame] | 518 | nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, |
Jan Kara | 67fd707 | 2017-11-15 17:35:19 -0800 | [diff] [blame] | 519 | end, PAGECACHE_TAG_WRITEBACK); |
Jan Kara | 312e9d2 | 2017-11-15 17:35:05 -0800 | [diff] [blame] | 520 | if (!nr_pages) |
| 521 | break; |
| 522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | for (i = 0; i < nr_pages; i++) { |
| 524 | struct page *page = pvec.pages[i]; |
| 525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | wait_on_page_writeback(page); |
Jeff Layton | 5e8fcc1 | 2017-07-06 07:02:24 -0400 | [diff] [blame] | 527 | ClearPageError(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
| 529 | pagevec_release(&pvec); |
| 530 | cond_resched(); |
| 531 | } |
Junichi Nomura | aa750fd | 2015-11-05 18:47:23 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /** |
| 535 | * filemap_fdatawait_range - wait for writeback to complete |
| 536 | * @mapping: address space structure to wait for |
| 537 | * @start_byte: offset in bytes where the range starts |
| 538 | * @end_byte: offset in bytes where the range ends (inclusive) |
| 539 | * |
| 540 | * Walk the list of under-writeback pages of the given address space |
| 541 | * in the given range and wait for all of them. Check error status of |
| 542 | * the address space and return it. |
| 543 | * |
| 544 | * Since the error status of the address space is cleared by this function, |
| 545 | * callers are responsible for checking the return value and handling and/or |
| 546 | * reporting the error. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 547 | * |
| 548 | * Return: error status of the address space. |
Junichi Nomura | aa750fd | 2015-11-05 18:47:23 -0800 | [diff] [blame] | 549 | */ |
| 550 | int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte, |
| 551 | loff_t end_byte) |
| 552 | { |
Jeff Layton | 5e8fcc1 | 2017-07-06 07:02:24 -0400 | [diff] [blame] | 553 | __filemap_fdatawait_range(mapping, start_byte, end_byte); |
| 554 | return filemap_check_errors(mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | } |
Jan Kara | d3bccb6f | 2009-08-17 19:30:27 +0200 | [diff] [blame] | 556 | EXPORT_SYMBOL(filemap_fdatawait_range); |
| 557 | |
| 558 | /** |
Ross Zwisler | aa0bfcd | 2019-06-20 17:05:37 -0400 | [diff] [blame] | 559 | * filemap_fdatawait_range_keep_errors - wait for writeback to complete |
| 560 | * @mapping: address space structure to wait for |
| 561 | * @start_byte: offset in bytes where the range starts |
| 562 | * @end_byte: offset in bytes where the range ends (inclusive) |
| 563 | * |
| 564 | * Walk the list of under-writeback pages of the given address space in the |
| 565 | * given range and wait for all of them. Unlike filemap_fdatawait_range(), |
| 566 | * this function does not clear error status of the address space. |
| 567 | * |
| 568 | * Use this function if callers don't handle errors themselves. Expected |
| 569 | * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2), |
| 570 | * fsfreeze(8) |
| 571 | */ |
| 572 | int filemap_fdatawait_range_keep_errors(struct address_space *mapping, |
| 573 | loff_t start_byte, loff_t end_byte) |
| 574 | { |
| 575 | __filemap_fdatawait_range(mapping, start_byte, end_byte); |
| 576 | return filemap_check_and_keep_errors(mapping); |
| 577 | } |
| 578 | EXPORT_SYMBOL(filemap_fdatawait_range_keep_errors); |
| 579 | |
| 580 | /** |
Jeff Layton | a823e45 | 2017-07-28 07:24:43 -0400 | [diff] [blame] | 581 | * file_fdatawait_range - wait for writeback to complete |
| 582 | * @file: file pointing to address space structure to wait for |
| 583 | * @start_byte: offset in bytes where the range starts |
| 584 | * @end_byte: offset in bytes where the range ends (inclusive) |
| 585 | * |
| 586 | * Walk the list of under-writeback pages of the address space that file |
| 587 | * refers to, in the given range and wait for all of them. Check error |
| 588 | * status of the address space vs. the file->f_wb_err cursor and return it. |
| 589 | * |
| 590 | * Since the error status of the file is advanced by this function, |
| 591 | * callers are responsible for checking the return value and handling and/or |
| 592 | * reporting the error. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 593 | * |
| 594 | * Return: error status of the address space vs. the file->f_wb_err cursor. |
Jeff Layton | a823e45 | 2017-07-28 07:24:43 -0400 | [diff] [blame] | 595 | */ |
| 596 | int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte) |
| 597 | { |
| 598 | struct address_space *mapping = file->f_mapping; |
| 599 | |
| 600 | __filemap_fdatawait_range(mapping, start_byte, end_byte); |
| 601 | return file_check_and_advance_wb_err(file); |
| 602 | } |
| 603 | EXPORT_SYMBOL(file_fdatawait_range); |
| 604 | |
| 605 | /** |
Junichi Nomura | aa750fd | 2015-11-05 18:47:23 -0800 | [diff] [blame] | 606 | * filemap_fdatawait_keep_errors - wait for writeback without clearing errors |
| 607 | * @mapping: address space structure to wait for |
| 608 | * |
| 609 | * Walk the list of under-writeback pages of the given address space |
| 610 | * and wait for all of them. Unlike filemap_fdatawait(), this function |
| 611 | * does not clear error status of the address space. |
| 612 | * |
| 613 | * Use this function if callers don't handle errors themselves. Expected |
| 614 | * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2), |
| 615 | * fsfreeze(8) |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 616 | * |
| 617 | * Return: error status of the address space. |
Junichi Nomura | aa750fd | 2015-11-05 18:47:23 -0800 | [diff] [blame] | 618 | */ |
Jeff Layton | 76341ca | 2017-07-06 07:02:22 -0400 | [diff] [blame] | 619 | int filemap_fdatawait_keep_errors(struct address_space *mapping) |
Junichi Nomura | aa750fd | 2015-11-05 18:47:23 -0800 | [diff] [blame] | 620 | { |
Jeff Layton | ffb959b | 2017-07-31 10:29:38 -0400 | [diff] [blame] | 621 | __filemap_fdatawait_range(mapping, 0, LLONG_MAX); |
Jeff Layton | 76341ca | 2017-07-06 07:02:22 -0400 | [diff] [blame] | 622 | return filemap_check_and_keep_errors(mapping); |
Junichi Nomura | aa750fd | 2015-11-05 18:47:23 -0800 | [diff] [blame] | 623 | } |
Jeff Layton | 76341ca | 2017-07-06 07:02:22 -0400 | [diff] [blame] | 624 | EXPORT_SYMBOL(filemap_fdatawait_keep_errors); |
Junichi Nomura | aa750fd | 2015-11-05 18:47:23 -0800 | [diff] [blame] | 625 | |
Konstantin Khlebnikov | 875d91b | 2019-09-23 15:34:48 -0700 | [diff] [blame] | 626 | /* Returns true if writeback might be needed or already in progress. */ |
Jeff Layton | 9326c9b | 2017-07-26 10:21:11 -0400 | [diff] [blame] | 627 | static bool mapping_needs_writeback(struct address_space *mapping) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | { |
Konstantin Khlebnikov | 875d91b | 2019-09-23 15:34:48 -0700 | [diff] [blame] | 629 | if (dax_mapping(mapping)) |
| 630 | return mapping->nrexceptional; |
| 631 | |
| 632 | return mapping->nrpages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 635 | /** |
| 636 | * filemap_write_and_wait_range - write out & wait on a file range |
| 637 | * @mapping: the address_space for the pages |
| 638 | * @lstart: offset in bytes where the range starts |
| 639 | * @lend: offset in bytes where the range ends (inclusive) |
| 640 | * |
Andrew Morton | 469eb4d | 2006-03-24 03:17:45 -0800 | [diff] [blame] | 641 | * Write out and wait upon file offsets lstart->lend, inclusive. |
| 642 | * |
mchehab@s-opensource.com | 0e056eb | 2017-03-30 17:11:36 -0300 | [diff] [blame] | 643 | * Note that @lend is inclusive (describes the last byte to be written) so |
Andrew Morton | 469eb4d | 2006-03-24 03:17:45 -0800 | [diff] [blame] | 644 | * that this function can be used to write to the very end-of-file (end = -1). |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 645 | * |
| 646 | * Return: error status of the address space. |
Andrew Morton | 469eb4d | 2006-03-24 03:17:45 -0800 | [diff] [blame] | 647 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | int filemap_write_and_wait_range(struct address_space *mapping, |
| 649 | loff_t lstart, loff_t lend) |
| 650 | { |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 651 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
Jeff Layton | 9326c9b | 2017-07-26 10:21:11 -0400 | [diff] [blame] | 653 | if (mapping_needs_writeback(mapping)) { |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 654 | err = __filemap_fdatawrite_range(mapping, lstart, lend, |
| 655 | WB_SYNC_ALL); |
Ira Weiny | ddf8f37 | 2020-01-30 22:12:07 -0800 | [diff] [blame] | 656 | /* |
| 657 | * Even if the above returned error, the pages may be |
| 658 | * written partially (e.g. -ENOSPC), so we wait for it. |
| 659 | * But the -EIO is special case, it may indicate the worst |
| 660 | * thing (e.g. bug) happened, so we avoid waiting for it. |
| 661 | */ |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 662 | if (err != -EIO) { |
Christoph Hellwig | 94004ed | 2009-09-30 22:16:33 +0200 | [diff] [blame] | 663 | int err2 = filemap_fdatawait_range(mapping, |
| 664 | lstart, lend); |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 665 | if (!err) |
| 666 | err = err2; |
Jeff Layton | cbeaf95 | 2017-07-06 07:02:23 -0400 | [diff] [blame] | 667 | } else { |
| 668 | /* Clear any previously stored errors */ |
| 669 | filemap_check_errors(mapping); |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 670 | } |
Dmitry Monakhov | 865ffef3 | 2013-04-29 15:08:42 -0700 | [diff] [blame] | 671 | } else { |
| 672 | err = filemap_check_errors(mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | } |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 674 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | } |
Chris Mason | f699558 | 2009-04-15 13:22:37 -0400 | [diff] [blame] | 676 | EXPORT_SYMBOL(filemap_write_and_wait_range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
Jeff Layton | 5660e13 | 2017-07-06 07:02:25 -0400 | [diff] [blame] | 678 | void __filemap_set_wb_err(struct address_space *mapping, int err) |
| 679 | { |
Jeff Layton | 3acdfd2 | 2017-07-24 06:22:15 -0400 | [diff] [blame] | 680 | errseq_t eseq = errseq_set(&mapping->wb_err, err); |
Jeff Layton | 5660e13 | 2017-07-06 07:02:25 -0400 | [diff] [blame] | 681 | |
| 682 | trace_filemap_set_wb_err(mapping, eseq); |
| 683 | } |
| 684 | EXPORT_SYMBOL(__filemap_set_wb_err); |
| 685 | |
| 686 | /** |
| 687 | * file_check_and_advance_wb_err - report wb error (if any) that was previously |
| 688 | * and advance wb_err to current one |
| 689 | * @file: struct file on which the error is being reported |
| 690 | * |
| 691 | * When userland calls fsync (or something like nfsd does the equivalent), we |
| 692 | * want to report any writeback errors that occurred since the last fsync (or |
| 693 | * since the file was opened if there haven't been any). |
| 694 | * |
| 695 | * Grab the wb_err from the mapping. If it matches what we have in the file, |
| 696 | * then just quickly return 0. The file is all caught up. |
| 697 | * |
| 698 | * If it doesn't match, then take the mapping value, set the "seen" flag in |
| 699 | * it and try to swap it into place. If it works, or another task beat us |
| 700 | * to it with the new value, then update the f_wb_err and return the error |
| 701 | * portion. The error at this point must be reported via proper channels |
| 702 | * (a'la fsync, or NFS COMMIT operation, etc.). |
| 703 | * |
| 704 | * While we handle mapping->wb_err with atomic operations, the f_wb_err |
| 705 | * value is protected by the f_lock since we must ensure that it reflects |
| 706 | * the latest value swapped in for this file descriptor. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 707 | * |
| 708 | * Return: %0 on success, negative error code otherwise. |
Jeff Layton | 5660e13 | 2017-07-06 07:02:25 -0400 | [diff] [blame] | 709 | */ |
| 710 | int file_check_and_advance_wb_err(struct file *file) |
| 711 | { |
| 712 | int err = 0; |
| 713 | errseq_t old = READ_ONCE(file->f_wb_err); |
| 714 | struct address_space *mapping = file->f_mapping; |
| 715 | |
| 716 | /* Locklessly handle the common case where nothing has changed */ |
| 717 | if (errseq_check(&mapping->wb_err, old)) { |
| 718 | /* Something changed, must use slow path */ |
| 719 | spin_lock(&file->f_lock); |
| 720 | old = file->f_wb_err; |
| 721 | err = errseq_check_and_advance(&mapping->wb_err, |
| 722 | &file->f_wb_err); |
| 723 | trace_file_check_and_advance_wb_err(file, old); |
| 724 | spin_unlock(&file->f_lock); |
| 725 | } |
Jeff Layton | f4e222c | 2017-10-03 16:15:25 -0700 | [diff] [blame] | 726 | |
| 727 | /* |
| 728 | * We're mostly using this function as a drop in replacement for |
| 729 | * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect |
| 730 | * that the legacy code would have had on these flags. |
| 731 | */ |
| 732 | clear_bit(AS_EIO, &mapping->flags); |
| 733 | clear_bit(AS_ENOSPC, &mapping->flags); |
Jeff Layton | 5660e13 | 2017-07-06 07:02:25 -0400 | [diff] [blame] | 734 | return err; |
| 735 | } |
| 736 | EXPORT_SYMBOL(file_check_and_advance_wb_err); |
| 737 | |
| 738 | /** |
| 739 | * file_write_and_wait_range - write out & wait on a file range |
| 740 | * @file: file pointing to address_space with pages |
| 741 | * @lstart: offset in bytes where the range starts |
| 742 | * @lend: offset in bytes where the range ends (inclusive) |
| 743 | * |
| 744 | * Write out and wait upon file offsets lstart->lend, inclusive. |
| 745 | * |
| 746 | * Note that @lend is inclusive (describes the last byte to be written) so |
| 747 | * that this function can be used to write to the very end-of-file (end = -1). |
| 748 | * |
| 749 | * After writing out and waiting on the data, we check and advance the |
| 750 | * f_wb_err cursor to the latest value, and return any errors detected there. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 751 | * |
| 752 | * Return: %0 on success, negative error code otherwise. |
Jeff Layton | 5660e13 | 2017-07-06 07:02:25 -0400 | [diff] [blame] | 753 | */ |
| 754 | int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend) |
| 755 | { |
| 756 | int err = 0, err2; |
| 757 | struct address_space *mapping = file->f_mapping; |
| 758 | |
Jeff Layton | 9326c9b | 2017-07-26 10:21:11 -0400 | [diff] [blame] | 759 | if (mapping_needs_writeback(mapping)) { |
Jeff Layton | 5660e13 | 2017-07-06 07:02:25 -0400 | [diff] [blame] | 760 | err = __filemap_fdatawrite_range(mapping, lstart, lend, |
| 761 | WB_SYNC_ALL); |
| 762 | /* See comment of filemap_write_and_wait() */ |
| 763 | if (err != -EIO) |
| 764 | __filemap_fdatawait_range(mapping, lstart, lend); |
| 765 | } |
| 766 | err2 = file_check_and_advance_wb_err(file); |
| 767 | if (!err) |
| 768 | err = err2; |
| 769 | return err; |
| 770 | } |
| 771 | EXPORT_SYMBOL(file_write_and_wait_range); |
| 772 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 773 | /** |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 774 | * replace_page_cache_page - replace a pagecache page with a new one |
| 775 | * @old: page to be replaced |
| 776 | * @new: page to replace with |
| 777 | * @gfp_mask: allocation mode |
| 778 | * |
| 779 | * This function replaces a page in the pagecache with a new one. On |
| 780 | * success it acquires the pagecache reference for the new page and |
| 781 | * drops it for the old page. Both the old and new pages must be |
| 782 | * locked. This function does not add the new page to the LRU, the |
| 783 | * caller must do that. |
| 784 | * |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 785 | * The remove + add is atomic. This function cannot fail. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 786 | * |
| 787 | * Return: %0 |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 788 | */ |
| 789 | int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask) |
| 790 | { |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 791 | struct address_space *mapping = old->mapping; |
| 792 | void (*freepage)(struct page *) = mapping->a_ops->freepage; |
| 793 | pgoff_t offset = old->index; |
| 794 | XA_STATE(xas, &mapping->i_pages, offset); |
| 795 | unsigned long flags; |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 796 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 797 | VM_BUG_ON_PAGE(!PageLocked(old), old); |
| 798 | VM_BUG_ON_PAGE(!PageLocked(new), new); |
| 799 | VM_BUG_ON_PAGE(new->mapping, new); |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 800 | |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 801 | get_page(new); |
| 802 | new->mapping = mapping; |
| 803 | new->index = offset; |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 804 | |
Johannes Weiner | 0d1c207 | 2020-06-03 16:01:54 -0700 | [diff] [blame] | 805 | mem_cgroup_migrate(old, new); |
| 806 | |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 807 | xas_lock_irqsave(&xas, flags); |
| 808 | xas_store(&xas, new); |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 809 | |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 810 | old->mapping = NULL; |
| 811 | /* hugetlb pages do not participate in page cache accounting. */ |
| 812 | if (!PageHuge(old)) |
Johannes Weiner | 0d1c207 | 2020-06-03 16:01:54 -0700 | [diff] [blame] | 813 | __dec_lruvec_page_state(old, NR_FILE_PAGES); |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 814 | if (!PageHuge(new)) |
Johannes Weiner | 0d1c207 | 2020-06-03 16:01:54 -0700 | [diff] [blame] | 815 | __inc_lruvec_page_state(new, NR_FILE_PAGES); |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 816 | if (PageSwapBacked(old)) |
Johannes Weiner | 0d1c207 | 2020-06-03 16:01:54 -0700 | [diff] [blame] | 817 | __dec_lruvec_page_state(old, NR_SHMEM); |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 818 | if (PageSwapBacked(new)) |
Johannes Weiner | 0d1c207 | 2020-06-03 16:01:54 -0700 | [diff] [blame] | 819 | __inc_lruvec_page_state(new, NR_SHMEM); |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 820 | xas_unlock_irqrestore(&xas, flags); |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 821 | if (freepage) |
| 822 | freepage(old); |
| 823 | put_page(old); |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 824 | |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 825 | return 0; |
Miklos Szeredi | ef6a3c6 | 2011-03-22 16:30:52 -0700 | [diff] [blame] | 826 | } |
| 827 | EXPORT_SYMBOL_GPL(replace_page_cache_page); |
| 828 | |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 829 | static int __add_to_page_cache_locked(struct page *page, |
| 830 | struct address_space *mapping, |
| 831 | pgoff_t offset, gfp_t gfp_mask, |
| 832 | void **shadowp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | { |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 834 | XA_STATE(xas, &mapping->i_pages, offset); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 835 | int huge = PageHuge(page); |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 836 | int error; |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 837 | void *old; |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 838 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 839 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 840 | VM_BUG_ON_PAGE(PageSwapBacked(page), page); |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 841 | mapping_set_update(&xas, mapping); |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 842 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 843 | get_page(page); |
Kirill A. Shutemov | 66a0c8e | 2013-09-12 15:13:59 -0700 | [diff] [blame] | 844 | page->mapping = mapping; |
| 845 | page->index = offset; |
| 846 | |
Johannes Weiner | 3fea5a4 | 2020-06-03 16:01:41 -0700 | [diff] [blame] | 847 | if (!huge) { |
Johannes Weiner | d9eb1ea | 2020-06-03 16:02:24 -0700 | [diff] [blame] | 848 | error = mem_cgroup_charge(page, current->mm, gfp_mask); |
Johannes Weiner | 3fea5a4 | 2020-06-03 16:01:41 -0700 | [diff] [blame] | 849 | if (error) |
| 850 | goto error; |
| 851 | } |
| 852 | |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 853 | do { |
| 854 | xas_lock_irq(&xas); |
| 855 | old = xas_load(&xas); |
| 856 | if (old && !xa_is_value(old)) |
| 857 | xas_set_err(&xas, -EEXIST); |
| 858 | xas_store(&xas, page); |
| 859 | if (xas_error(&xas)) |
| 860 | goto unlock; |
Michal Hocko | 4165b9b | 2015-06-24 16:57:24 -0700 | [diff] [blame] | 861 | |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 862 | if (xa_is_value(old)) { |
| 863 | mapping->nrexceptional--; |
| 864 | if (shadowp) |
| 865 | *shadowp = old; |
| 866 | } |
| 867 | mapping->nrpages++; |
| 868 | |
| 869 | /* hugetlb pages do not participate in page cache accounting */ |
| 870 | if (!huge) |
Johannes Weiner | 0d1c207 | 2020-06-03 16:01:54 -0700 | [diff] [blame] | 871 | __inc_lruvec_page_state(page, NR_FILE_PAGES); |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 872 | unlock: |
| 873 | xas_unlock_irq(&xas); |
| 874 | } while (xas_nomem(&xas, gfp_mask & GFP_RECLAIM_MASK)); |
| 875 | |
Johannes Weiner | 3fea5a4 | 2020-06-03 16:01:41 -0700 | [diff] [blame] | 876 | if (xas_error(&xas)) { |
| 877 | error = xas_error(&xas); |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 878 | goto error; |
Johannes Weiner | 3fea5a4 | 2020-06-03 16:01:41 -0700 | [diff] [blame] | 879 | } |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 880 | |
Kirill A. Shutemov | 66a0c8e | 2013-09-12 15:13:59 -0700 | [diff] [blame] | 881 | trace_mm_filemap_add_to_page_cache(page); |
| 882 | return 0; |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 883 | error: |
Kirill A. Shutemov | 66a0c8e | 2013-09-12 15:13:59 -0700 | [diff] [blame] | 884 | page->mapping = NULL; |
| 885 | /* Leave page->index set: truncation relies upon it */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 886 | put_page(page); |
Johannes Weiner | 3fea5a4 | 2020-06-03 16:01:41 -0700 | [diff] [blame] | 887 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | } |
Josef Bacik | cfcbfb1 | 2019-05-13 17:21:04 -0700 | [diff] [blame] | 889 | ALLOW_ERROR_INJECTION(__add_to_page_cache_locked, ERRNO); |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 890 | |
| 891 | /** |
| 892 | * add_to_page_cache_locked - add a locked page to the pagecache |
| 893 | * @page: page to add |
| 894 | * @mapping: the page's address_space |
| 895 | * @offset: page index |
| 896 | * @gfp_mask: page allocation mode |
| 897 | * |
| 898 | * This function is used to add a page to the pagecache. It must be locked. |
| 899 | * This function does not add the page to the LRU. The caller must do that. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 900 | * |
| 901 | * Return: %0 on success, negative error code otherwise. |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 902 | */ |
| 903 | int add_to_page_cache_locked(struct page *page, struct address_space *mapping, |
| 904 | pgoff_t offset, gfp_t gfp_mask) |
| 905 | { |
| 906 | return __add_to_page_cache_locked(page, mapping, offset, |
| 907 | gfp_mask, NULL); |
| 908 | } |
Nick Piggin | e286781 | 2008-07-25 19:45:30 -0700 | [diff] [blame] | 909 | EXPORT_SYMBOL(add_to_page_cache_locked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
| 911 | int add_to_page_cache_lru(struct page *page, struct address_space *mapping, |
Al Viro | 6daa0e2 | 2005-10-21 03:18:50 -0400 | [diff] [blame] | 912 | pgoff_t offset, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | { |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 914 | void *shadow = NULL; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 915 | int ret; |
| 916 | |
Kirill A. Shutemov | 48c935a | 2016-01-15 16:51:24 -0800 | [diff] [blame] | 917 | __SetPageLocked(page); |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 918 | ret = __add_to_page_cache_locked(page, mapping, offset, |
| 919 | gfp_mask, &shadow); |
| 920 | if (unlikely(ret)) |
Kirill A. Shutemov | 48c935a | 2016-01-15 16:51:24 -0800 | [diff] [blame] | 921 | __ClearPageLocked(page); |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 922 | else { |
| 923 | /* |
| 924 | * The page might have been evicted from cache only |
| 925 | * recently, in which case it should be activated like |
| 926 | * any other repeatedly accessed page. |
Rik van Riel | f0281a0 | 2016-05-20 16:56:25 -0700 | [diff] [blame] | 927 | * The exception is pages getting rewritten; evicting other |
| 928 | * data from the working set, only to cache data that will |
| 929 | * get overwritten with something else, is a waste of memory. |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 930 | */ |
Johannes Weiner | 1899ad1 | 2018-10-26 15:06:04 -0700 | [diff] [blame] | 931 | WARN_ON_ONCE(PageActive(page)); |
| 932 | if (!(gfp_mask & __GFP_WRITE) && shadow) |
| 933 | workingset_refault(page, shadow); |
Johannes Weiner | a528910 | 2014-04-03 14:47:51 -0700 | [diff] [blame] | 934 | lru_cache_add(page); |
| 935 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | return ret; |
| 937 | } |
Evgeniy Polyakov | 18bc0bb | 2009-02-09 17:02:42 +0300 | [diff] [blame] | 938 | EXPORT_SYMBOL_GPL(add_to_page_cache_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 940 | #ifdef CONFIG_NUMA |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 941 | struct page *__page_cache_alloc(gfp_t gfp) |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 942 | { |
Miao Xie | c0ff745 | 2010-05-24 14:32:08 -0700 | [diff] [blame] | 943 | int n; |
| 944 | struct page *page; |
| 945 | |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 946 | if (cpuset_do_page_mem_spread()) { |
Mel Gorman | cc9a6c8 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 947 | unsigned int cpuset_mems_cookie; |
| 948 | do { |
Mel Gorman | d26914d | 2014-04-03 14:47:24 -0700 | [diff] [blame] | 949 | cpuset_mems_cookie = read_mems_allowed_begin(); |
Mel Gorman | cc9a6c8 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 950 | n = cpuset_mem_spread_node(); |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 951 | page = __alloc_pages_node(n, gfp, 0); |
Mel Gorman | d26914d | 2014-04-03 14:47:24 -0700 | [diff] [blame] | 952 | } while (!page && read_mems_allowed_retry(cpuset_mems_cookie)); |
Mel Gorman | cc9a6c8 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 953 | |
Miao Xie | c0ff745 | 2010-05-24 14:32:08 -0700 | [diff] [blame] | 954 | return page; |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 955 | } |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 956 | return alloc_pages(gfp, 0); |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 957 | } |
Nick Piggin | 2ae8814 | 2006-10-28 10:38:23 -0700 | [diff] [blame] | 958 | EXPORT_SYMBOL(__page_cache_alloc); |
Paul Jackson | 44110fe | 2006-03-24 03:16:04 -0800 | [diff] [blame] | 959 | #endif |
| 960 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | /* |
| 962 | * In order to wait for pages to become available there must be |
| 963 | * waitqueues associated with pages. By using a hash table of |
| 964 | * waitqueues where the bucket discipline is to maintain all |
| 965 | * waiters on the same queue and wake all when any of the pages |
| 966 | * become available, and for the woken contexts to check to be |
| 967 | * sure the appropriate page became available, this saves space |
| 968 | * at a cost of "thundering herd" phenomena during rare hash |
| 969 | * collisions. |
| 970 | */ |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 971 | #define PAGE_WAIT_TABLE_BITS 8 |
| 972 | #define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS) |
| 973 | static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned; |
| 974 | |
| 975 | static wait_queue_head_t *page_waitqueue(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | { |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 977 | return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | } |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 979 | |
| 980 | void __init pagecache_init(void) |
| 981 | { |
| 982 | int i; |
| 983 | |
| 984 | for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++) |
| 985 | init_waitqueue_head(&page_wait_table[i]); |
| 986 | |
| 987 | page_writeback_init(); |
| 988 | } |
| 989 | |
Linus Torvalds | 3510ca2 | 2017-08-27 13:55:12 -0700 | [diff] [blame] | 990 | /* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */ |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 991 | struct wait_page_key { |
| 992 | struct page *page; |
| 993 | int bit_nr; |
| 994 | int page_match; |
| 995 | }; |
| 996 | |
| 997 | struct wait_page_queue { |
| 998 | struct page *page; |
| 999 | int bit_nr; |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 1000 | wait_queue_entry_t wait; |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1001 | }; |
| 1002 | |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 1003 | static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg) |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1004 | { |
Linus Torvalds | 2a9127f | 2020-07-23 10:16:49 -0700 | [diff] [blame^] | 1005 | int ret; |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1006 | struct wait_page_key *key = arg; |
| 1007 | struct wait_page_queue *wait_page |
| 1008 | = container_of(wait, struct wait_page_queue, wait); |
| 1009 | |
| 1010 | if (wait_page->page != key->page) |
| 1011 | return 0; |
| 1012 | key->page_match = 1; |
| 1013 | |
| 1014 | if (wait_page->bit_nr != key->bit_nr) |
| 1015 | return 0; |
Linus Torvalds | 3510ca2 | 2017-08-27 13:55:12 -0700 | [diff] [blame] | 1016 | |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1017 | /* |
Linus Torvalds | 2a9127f | 2020-07-23 10:16:49 -0700 | [diff] [blame^] | 1018 | * If it's an exclusive wait, we get the bit for it, and |
| 1019 | * stop walking if we can't. |
| 1020 | * |
| 1021 | * If it's a non-exclusive wait, then the fact that this |
| 1022 | * wake function was called means that the bit already |
| 1023 | * was cleared, and we don't care if somebody then |
| 1024 | * re-took it. |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1025 | */ |
Linus Torvalds | 2a9127f | 2020-07-23 10:16:49 -0700 | [diff] [blame^] | 1026 | ret = 0; |
| 1027 | if (wait->flags & WQ_FLAG_EXCLUSIVE) { |
| 1028 | if (test_and_set_bit(key->bit_nr, &key->page->flags)) |
| 1029 | return -1; |
| 1030 | ret = 1; |
| 1031 | } |
| 1032 | wait->flags |= WQ_FLAG_WOKEN; |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1033 | |
Linus Torvalds | 2a9127f | 2020-07-23 10:16:49 -0700 | [diff] [blame^] | 1034 | wake_up_state(wait->private, mode); |
| 1035 | |
| 1036 | /* |
| 1037 | * Ok, we have successfully done what we're waiting for, |
| 1038 | * and we can unconditionally remove the wait entry. |
| 1039 | * |
| 1040 | * Note that this has to be the absolute last thing we do, |
| 1041 | * since after list_del_init(&wait->entry) the wait entry |
| 1042 | * might be de-allocated and the process might even have |
| 1043 | * exited. |
| 1044 | * |
| 1045 | * We _really_ should have a "list_del_init_careful()" to |
| 1046 | * properly pair with the unlocked "list_empty_careful()" |
| 1047 | * in finish_wait(). |
| 1048 | */ |
| 1049 | smp_mb(); |
| 1050 | list_del_init(&wait->entry); |
| 1051 | return ret; |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1052 | } |
| 1053 | |
Nicholas Piggin | 74d81bf | 2017-02-22 15:44:41 -0800 | [diff] [blame] | 1054 | static void wake_up_page_bit(struct page *page, int bit_nr) |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1055 | { |
| 1056 | wait_queue_head_t *q = page_waitqueue(page); |
| 1057 | struct wait_page_key key; |
| 1058 | unsigned long flags; |
Tim Chen | 11a19c7 | 2017-08-25 09:13:55 -0700 | [diff] [blame] | 1059 | wait_queue_entry_t bookmark; |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1060 | |
| 1061 | key.page = page; |
| 1062 | key.bit_nr = bit_nr; |
| 1063 | key.page_match = 0; |
| 1064 | |
Tim Chen | 11a19c7 | 2017-08-25 09:13:55 -0700 | [diff] [blame] | 1065 | bookmark.flags = 0; |
| 1066 | bookmark.private = NULL; |
| 1067 | bookmark.func = NULL; |
| 1068 | INIT_LIST_HEAD(&bookmark.entry); |
| 1069 | |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1070 | spin_lock_irqsave(&q->lock, flags); |
Tim Chen | 11a19c7 | 2017-08-25 09:13:55 -0700 | [diff] [blame] | 1071 | __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark); |
| 1072 | |
| 1073 | while (bookmark.flags & WQ_FLAG_BOOKMARK) { |
| 1074 | /* |
| 1075 | * Take a breather from holding the lock, |
| 1076 | * allow pages that finish wake up asynchronously |
| 1077 | * to acquire the lock and remove themselves |
| 1078 | * from wait queue |
| 1079 | */ |
| 1080 | spin_unlock_irqrestore(&q->lock, flags); |
| 1081 | cpu_relax(); |
| 1082 | spin_lock_irqsave(&q->lock, flags); |
| 1083 | __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark); |
| 1084 | } |
| 1085 | |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1086 | /* |
| 1087 | * It is possible for other pages to have collided on the waitqueue |
| 1088 | * hash, so in that case check for a page match. That prevents a long- |
| 1089 | * term waiter |
| 1090 | * |
| 1091 | * It is still possible to miss a case here, when we woke page waiters |
| 1092 | * and removed them from the waitqueue, but there are still other |
| 1093 | * page waiters. |
| 1094 | */ |
| 1095 | if (!waitqueue_active(q) || !key.page_match) { |
| 1096 | ClearPageWaiters(page); |
| 1097 | /* |
| 1098 | * It's possible to miss clearing Waiters here, when we woke |
| 1099 | * our page waiters, but the hashed waitqueue has waiters for |
| 1100 | * other pages on it. |
| 1101 | * |
| 1102 | * That's okay, it's a rare case. The next waker will clear it. |
| 1103 | */ |
| 1104 | } |
| 1105 | spin_unlock_irqrestore(&q->lock, flags); |
| 1106 | } |
Nicholas Piggin | 74d81bf | 2017-02-22 15:44:41 -0800 | [diff] [blame] | 1107 | |
| 1108 | static void wake_up_page(struct page *page, int bit) |
| 1109 | { |
| 1110 | if (!PageWaiters(page)) |
| 1111 | return; |
| 1112 | wake_up_page_bit(page, bit); |
| 1113 | } |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1114 | |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1115 | /* |
| 1116 | * A choice of three behaviors for wait_on_page_bit_common(): |
| 1117 | */ |
| 1118 | enum behavior { |
| 1119 | EXCLUSIVE, /* Hold ref to page and take the bit when woken, like |
| 1120 | * __lock_page() waiting on then setting PG_locked. |
| 1121 | */ |
| 1122 | SHARED, /* Hold ref to page and check the bit when woken, like |
| 1123 | * wait_on_page_writeback() waiting on PG_writeback. |
| 1124 | */ |
| 1125 | DROP, /* Drop ref to page before wait, no check when woken, |
| 1126 | * like put_and_wait_on_page_locked() on PG_locked. |
| 1127 | */ |
| 1128 | }; |
| 1129 | |
Linus Torvalds | 2a9127f | 2020-07-23 10:16:49 -0700 | [diff] [blame^] | 1130 | /* |
| 1131 | * Attempt to check (or get) the page bit, and mark the |
| 1132 | * waiter woken if successful. |
| 1133 | */ |
| 1134 | static inline bool trylock_page_bit_common(struct page *page, int bit_nr, |
| 1135 | struct wait_queue_entry *wait) |
| 1136 | { |
| 1137 | if (wait->flags & WQ_FLAG_EXCLUSIVE) { |
| 1138 | if (test_and_set_bit(bit_nr, &page->flags)) |
| 1139 | return false; |
| 1140 | } else if (test_bit(bit_nr, &page->flags)) |
| 1141 | return false; |
| 1142 | |
| 1143 | wait->flags |= WQ_FLAG_WOKEN; |
| 1144 | return true; |
| 1145 | } |
| 1146 | |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1147 | static inline int wait_on_page_bit_common(wait_queue_head_t *q, |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1148 | struct page *page, int bit_nr, int state, enum behavior behavior) |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1149 | { |
| 1150 | struct wait_page_queue wait_page; |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 1151 | wait_queue_entry_t *wait = &wait_page.wait; |
Johannes Weiner | b1d29ba | 2018-10-26 15:06:08 -0700 | [diff] [blame] | 1152 | bool thrashing = false; |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1153 | bool delayacct = false; |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 1154 | unsigned long pflags; |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1155 | |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 1156 | if (bit_nr == PG_locked && |
Johannes Weiner | b1d29ba | 2018-10-26 15:06:08 -0700 | [diff] [blame] | 1157 | !PageUptodate(page) && PageWorkingset(page)) { |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1158 | if (!PageSwapBacked(page)) { |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 1159 | delayacct_thrashing_start(); |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1160 | delayacct = true; |
| 1161 | } |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 1162 | psi_memstall_enter(&pflags); |
Johannes Weiner | b1d29ba | 2018-10-26 15:06:08 -0700 | [diff] [blame] | 1163 | thrashing = true; |
| 1164 | } |
| 1165 | |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1166 | init_wait(wait); |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1167 | wait->flags = behavior == EXCLUSIVE ? WQ_FLAG_EXCLUSIVE : 0; |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1168 | wait->func = wake_page_function; |
| 1169 | wait_page.page = page; |
| 1170 | wait_page.bit_nr = bit_nr; |
| 1171 | |
Linus Torvalds | 2a9127f | 2020-07-23 10:16:49 -0700 | [diff] [blame^] | 1172 | /* |
| 1173 | * Do one last check whether we can get the |
| 1174 | * page bit synchronously. |
| 1175 | * |
| 1176 | * Do the SetPageWaiters() marking before that |
| 1177 | * to let any waker we _just_ missed know they |
| 1178 | * need to wake us up (otherwise they'll never |
| 1179 | * even go to the slow case that looks at the |
| 1180 | * page queue), and add ourselves to the wait |
| 1181 | * queue if we need to sleep. |
| 1182 | * |
| 1183 | * This part needs to be done under the queue |
| 1184 | * lock to avoid races. |
| 1185 | */ |
| 1186 | spin_lock_irq(&q->lock); |
| 1187 | SetPageWaiters(page); |
| 1188 | if (!trylock_page_bit_common(page, bit_nr, wait)) |
| 1189 | __add_wait_queue_entry_tail(q, wait); |
| 1190 | spin_unlock_irq(&q->lock); |
| 1191 | |
| 1192 | /* |
| 1193 | * From now on, all the logic will be based on |
| 1194 | * the WQ_FLAG_WOKEN flag, and the and the page |
| 1195 | * bit testing (and setting) will be - or has |
| 1196 | * already been - done by the wake function. |
| 1197 | * |
| 1198 | * We can drop our reference to the page. |
| 1199 | */ |
| 1200 | if (behavior == DROP) |
| 1201 | put_page(page); |
| 1202 | |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1203 | for (;;) { |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1204 | set_current_state(state); |
| 1205 | |
Linus Torvalds | 2a9127f | 2020-07-23 10:16:49 -0700 | [diff] [blame^] | 1206 | if (signal_pending_state(state, current)) |
Linus Torvalds | a8b169a | 2017-08-27 16:25:09 -0700 | [diff] [blame] | 1207 | break; |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1208 | |
Linus Torvalds | 2a9127f | 2020-07-23 10:16:49 -0700 | [diff] [blame^] | 1209 | if (wait->flags & WQ_FLAG_WOKEN) |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1210 | break; |
Linus Torvalds | 2a9127f | 2020-07-23 10:16:49 -0700 | [diff] [blame^] | 1211 | |
| 1212 | io_schedule(); |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | finish_wait(q, wait); |
| 1216 | |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 1217 | if (thrashing) { |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1218 | if (delayacct) |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 1219 | delayacct_thrashing_end(); |
| 1220 | psi_memstall_leave(&pflags); |
| 1221 | } |
Johannes Weiner | b1d29ba | 2018-10-26 15:06:08 -0700 | [diff] [blame] | 1222 | |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1223 | /* |
| 1224 | * A signal could leave PageWaiters set. Clearing it here if |
| 1225 | * !waitqueue_active would be possible (by open-coding finish_wait), |
| 1226 | * but still fail to catch it in the case of wait hash collision. We |
| 1227 | * already can fail to clear wait hash collision cases, so don't |
| 1228 | * bother with signals either. |
| 1229 | */ |
| 1230 | |
Linus Torvalds | 2a9127f | 2020-07-23 10:16:49 -0700 | [diff] [blame^] | 1231 | return wait->flags & WQ_FLAG_WOKEN ? 0 : -EINTR; |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1232 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | |
Harvey Harrison | 920c7a5 | 2008-02-04 22:29:26 -0800 | [diff] [blame] | 1234 | void wait_on_page_bit(struct page *page, int bit_nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | { |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1236 | wait_queue_head_t *q = page_waitqueue(page); |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1237 | wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, SHARED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | } |
| 1239 | EXPORT_SYMBOL(wait_on_page_bit); |
| 1240 | |
KOSAKI Motohiro | f62e00c | 2011-05-24 17:11:29 -0700 | [diff] [blame] | 1241 | int wait_on_page_bit_killable(struct page *page, int bit_nr) |
| 1242 | { |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1243 | wait_queue_head_t *q = page_waitqueue(page); |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1244 | return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, SHARED); |
KOSAKI Motohiro | f62e00c | 2011-05-24 17:11:29 -0700 | [diff] [blame] | 1245 | } |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 1246 | EXPORT_SYMBOL(wait_on_page_bit_killable); |
KOSAKI Motohiro | f62e00c | 2011-05-24 17:11:29 -0700 | [diff] [blame] | 1247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | /** |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1249 | * put_and_wait_on_page_locked - Drop a reference and wait for it to be unlocked |
| 1250 | * @page: The page to wait for. |
| 1251 | * |
| 1252 | * The caller should hold a reference on @page. They expect the page to |
| 1253 | * become unlocked relatively soon, but do not wish to hold up migration |
| 1254 | * (for example) by holding the reference while waiting for the page to |
| 1255 | * come unlocked. After this function returns, the caller should not |
| 1256 | * dereference @page. |
| 1257 | */ |
| 1258 | void put_and_wait_on_page_locked(struct page *page) |
| 1259 | { |
| 1260 | wait_queue_head_t *q; |
| 1261 | |
| 1262 | page = compound_head(page); |
| 1263 | q = page_waitqueue(page); |
| 1264 | wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, DROP); |
| 1265 | } |
| 1266 | |
| 1267 | /** |
David Howells | 385e1ca5f | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1268 | * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue |
Randy Dunlap | 697f619 | 2009-04-13 14:39:54 -0700 | [diff] [blame] | 1269 | * @page: Page defining the wait queue of interest |
| 1270 | * @waiter: Waiter to add to the queue |
David Howells | 385e1ca5f | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1271 | * |
| 1272 | * Add an arbitrary @waiter to the wait queue for the nominated @page. |
| 1273 | */ |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 1274 | void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter) |
David Howells | 385e1ca5f | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1275 | { |
| 1276 | wait_queue_head_t *q = page_waitqueue(page); |
| 1277 | unsigned long flags; |
| 1278 | |
| 1279 | spin_lock_irqsave(&q->lock, flags); |
Linus Torvalds | 9c3a815 | 2017-08-28 16:45:40 -0700 | [diff] [blame] | 1280 | __add_wait_queue_entry_tail(q, waiter); |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1281 | SetPageWaiters(page); |
David Howells | 385e1ca5f | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1282 | spin_unlock_irqrestore(&q->lock, flags); |
| 1283 | } |
| 1284 | EXPORT_SYMBOL_GPL(add_page_wait_queue); |
| 1285 | |
Linus Torvalds | b91e130 | 2016-12-27 11:40:38 -0800 | [diff] [blame] | 1286 | #ifndef clear_bit_unlock_is_negative_byte |
| 1287 | |
| 1288 | /* |
| 1289 | * PG_waiters is the high bit in the same byte as PG_lock. |
| 1290 | * |
| 1291 | * On x86 (and on many other architectures), we can clear PG_lock and |
| 1292 | * test the sign bit at the same time. But if the architecture does |
| 1293 | * not support that special operation, we just do this all by hand |
| 1294 | * instead. |
| 1295 | * |
| 1296 | * The read of PG_waiters has to be after (or concurrently with) PG_locked |
Ethon Paul | ffceeb6 | 2020-06-04 16:49:22 -0700 | [diff] [blame] | 1297 | * being cleared, but a memory barrier should be unnecessary since it is |
Linus Torvalds | b91e130 | 2016-12-27 11:40:38 -0800 | [diff] [blame] | 1298 | * in the same byte as PG_locked. |
| 1299 | */ |
| 1300 | static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem) |
| 1301 | { |
| 1302 | clear_bit_unlock(nr, mem); |
| 1303 | /* smp_mb__after_atomic(); */ |
Olof Johansson | 98473f9 | 2016-12-29 14:16:07 -0800 | [diff] [blame] | 1304 | return test_bit(PG_waiters, mem); |
Linus Torvalds | b91e130 | 2016-12-27 11:40:38 -0800 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | #endif |
| 1308 | |
David Howells | 385e1ca5f | 2009-04-03 16:42:39 +0100 | [diff] [blame] | 1309 | /** |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1310 | * unlock_page - unlock a locked page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | * @page: the page |
| 1312 | * |
| 1313 | * Unlocks the page and wakes up sleepers in ___wait_on_page_locked(). |
| 1314 | * Also wakes sleepers in wait_on_page_writeback() because the wakeup |
Masanari Iida | da3dae5 | 2014-09-09 01:27:23 +0900 | [diff] [blame] | 1315 | * mechanism between PageLocked pages and PageWriteback pages is shared. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep. |
| 1317 | * |
Linus Torvalds | b91e130 | 2016-12-27 11:40:38 -0800 | [diff] [blame] | 1318 | * Note that this depends on PG_waiters being the sign bit in the byte |
| 1319 | * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to |
| 1320 | * clear the PG_locked bit and test PG_waiters at the same time fairly |
| 1321 | * portably (architectures that do LL/SC can test any bit, while x86 can |
| 1322 | * test the sign bit). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | */ |
Harvey Harrison | 920c7a5 | 2008-02-04 22:29:26 -0800 | [diff] [blame] | 1324 | void unlock_page(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | { |
Linus Torvalds | b91e130 | 2016-12-27 11:40:38 -0800 | [diff] [blame] | 1326 | BUILD_BUG_ON(PG_waiters != 7); |
Kirill A. Shutemov | 48c935a | 2016-01-15 16:51:24 -0800 | [diff] [blame] | 1327 | page = compound_head(page); |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 1328 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
Linus Torvalds | b91e130 | 2016-12-27 11:40:38 -0800 | [diff] [blame] | 1329 | if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags)) |
| 1330 | wake_up_page_bit(page, PG_locked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | } |
| 1332 | EXPORT_SYMBOL(unlock_page); |
| 1333 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1334 | /** |
| 1335 | * end_page_writeback - end writeback against a page |
| 1336 | * @page: the page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | */ |
| 1338 | void end_page_writeback(struct page *page) |
| 1339 | { |
Mel Gorman | 888cf2d | 2014-06-04 16:10:34 -0700 | [diff] [blame] | 1340 | /* |
| 1341 | * TestClearPageReclaim could be used here but it is an atomic |
| 1342 | * operation and overkill in this particular case. Failing to |
| 1343 | * shuffle a page marked for immediate reclaim is too mild to |
| 1344 | * justify taking an atomic operation penalty at the end of |
| 1345 | * ever page writeback. |
| 1346 | */ |
| 1347 | if (PageReclaim(page)) { |
| 1348 | ClearPageReclaim(page); |
Miklos Szeredi | ac6aadb | 2008-04-28 02:12:38 -0700 | [diff] [blame] | 1349 | rotate_reclaimable_page(page); |
Mel Gorman | 888cf2d | 2014-06-04 16:10:34 -0700 | [diff] [blame] | 1350 | } |
Miklos Szeredi | ac6aadb | 2008-04-28 02:12:38 -0700 | [diff] [blame] | 1351 | |
| 1352 | if (!test_clear_page_writeback(page)) |
| 1353 | BUG(); |
| 1354 | |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 1355 | smp_mb__after_atomic(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | wake_up_page(page, PG_writeback); |
| 1357 | } |
| 1358 | EXPORT_SYMBOL(end_page_writeback); |
| 1359 | |
Matthew Wilcox | 57d9984 | 2014-06-04 16:07:45 -0700 | [diff] [blame] | 1360 | /* |
| 1361 | * After completing I/O on a page, call this routine to update the page |
| 1362 | * flags appropriately |
| 1363 | */ |
Jens Axboe | c11f0c0 | 2016-08-05 08:11:04 -0600 | [diff] [blame] | 1364 | void page_endio(struct page *page, bool is_write, int err) |
Matthew Wilcox | 57d9984 | 2014-06-04 16:07:45 -0700 | [diff] [blame] | 1365 | { |
Jens Axboe | c11f0c0 | 2016-08-05 08:11:04 -0600 | [diff] [blame] | 1366 | if (!is_write) { |
Matthew Wilcox | 57d9984 | 2014-06-04 16:07:45 -0700 | [diff] [blame] | 1367 | if (!err) { |
| 1368 | SetPageUptodate(page); |
| 1369 | } else { |
| 1370 | ClearPageUptodate(page); |
| 1371 | SetPageError(page); |
| 1372 | } |
| 1373 | unlock_page(page); |
Mike Christie | abf5454 | 2016-08-04 14:23:34 -0600 | [diff] [blame] | 1374 | } else { |
Matthew Wilcox | 57d9984 | 2014-06-04 16:07:45 -0700 | [diff] [blame] | 1375 | if (err) { |
Minchan Kim | dd8416c | 2017-02-24 14:59:59 -0800 | [diff] [blame] | 1376 | struct address_space *mapping; |
| 1377 | |
Matthew Wilcox | 57d9984 | 2014-06-04 16:07:45 -0700 | [diff] [blame] | 1378 | SetPageError(page); |
Minchan Kim | dd8416c | 2017-02-24 14:59:59 -0800 | [diff] [blame] | 1379 | mapping = page_mapping(page); |
| 1380 | if (mapping) |
| 1381 | mapping_set_error(mapping, err); |
Matthew Wilcox | 57d9984 | 2014-06-04 16:07:45 -0700 | [diff] [blame] | 1382 | } |
| 1383 | end_page_writeback(page); |
| 1384 | } |
| 1385 | } |
| 1386 | EXPORT_SYMBOL_GPL(page_endio); |
| 1387 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1388 | /** |
| 1389 | * __lock_page - get a lock on the page, assuming we need to sleep to get it |
Randy Dunlap | 8706675 | 2017-02-22 15:44:44 -0800 | [diff] [blame] | 1390 | * @__page: the page to lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | */ |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1392 | void __lock_page(struct page *__page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | { |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1394 | struct page *page = compound_head(__page); |
| 1395 | wait_queue_head_t *q = page_waitqueue(page); |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1396 | wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE, |
| 1397 | EXCLUSIVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | } |
| 1399 | EXPORT_SYMBOL(__lock_page); |
| 1400 | |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1401 | int __lock_page_killable(struct page *__page) |
Matthew Wilcox | 2687a35 | 2007-12-06 11:18:49 -0500 | [diff] [blame] | 1402 | { |
Nicholas Piggin | 6290602 | 2016-12-25 13:00:30 +1000 | [diff] [blame] | 1403 | struct page *page = compound_head(__page); |
| 1404 | wait_queue_head_t *q = page_waitqueue(page); |
Hugh Dickins | 9a1ea43 | 2018-12-28 00:36:14 -0800 | [diff] [blame] | 1405 | return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE, |
| 1406 | EXCLUSIVE); |
Matthew Wilcox | 2687a35 | 2007-12-06 11:18:49 -0500 | [diff] [blame] | 1407 | } |
Evgeniy Polyakov | 18bc0bb | 2009-02-09 17:02:42 +0300 | [diff] [blame] | 1408 | EXPORT_SYMBOL_GPL(__lock_page_killable); |
Matthew Wilcox | 2687a35 | 2007-12-06 11:18:49 -0500 | [diff] [blame] | 1409 | |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 1410 | /* |
| 1411 | * Return values: |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 1412 | * 1 - page is locked; mmap_lock is still held. |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 1413 | * 0 - page is not locked. |
Michel Lespinasse | 3e4e28c | 2020-06-08 21:33:51 -0700 | [diff] [blame] | 1414 | * mmap_lock has been released (mmap_read_unlock(), unless flags had both |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 1415 | * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 1416 | * which case mmap_lock is still held. |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 1417 | * |
| 1418 | * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1 |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 1419 | * with the page locked and the mmap_lock unperturbed. |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 1420 | */ |
Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 1421 | int __lock_page_or_retry(struct page *page, struct mm_struct *mm, |
| 1422 | unsigned int flags) |
| 1423 | { |
Peter Xu | 4064b98 | 2020-04-01 21:08:45 -0700 | [diff] [blame] | 1424 | if (fault_flag_allow_retry_first(flags)) { |
KOSAKI Motohiro | 37b23e0 | 2011-05-24 17:11:30 -0700 | [diff] [blame] | 1425 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 1426 | * CAUTION! In this case, mmap_lock is not released |
KOSAKI Motohiro | 37b23e0 | 2011-05-24 17:11:30 -0700 | [diff] [blame] | 1427 | * even though return 0. |
| 1428 | */ |
| 1429 | if (flags & FAULT_FLAG_RETRY_NOWAIT) |
| 1430 | return 0; |
| 1431 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1432 | mmap_read_unlock(mm); |
KOSAKI Motohiro | 37b23e0 | 2011-05-24 17:11:30 -0700 | [diff] [blame] | 1433 | if (flags & FAULT_FLAG_KILLABLE) |
| 1434 | wait_on_page_locked_killable(page); |
| 1435 | else |
Gleb Natapov | 318b275 | 2011-03-22 16:30:51 -0700 | [diff] [blame] | 1436 | wait_on_page_locked(page); |
Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 1437 | return 0; |
KOSAKI Motohiro | 37b23e0 | 2011-05-24 17:11:30 -0700 | [diff] [blame] | 1438 | } else { |
| 1439 | if (flags & FAULT_FLAG_KILLABLE) { |
| 1440 | int ret; |
| 1441 | |
| 1442 | ret = __lock_page_killable(page); |
| 1443 | if (ret) { |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 1444 | mmap_read_unlock(mm); |
KOSAKI Motohiro | 37b23e0 | 2011-05-24 17:11:30 -0700 | [diff] [blame] | 1445 | return 0; |
| 1446 | } |
| 1447 | } else |
| 1448 | __lock_page(page); |
| 1449 | return 1; |
Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 1450 | } |
| 1451 | } |
| 1452 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1453 | /** |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1454 | * page_cache_next_miss() - Find the next gap in the page cache. |
| 1455 | * @mapping: Mapping. |
| 1456 | * @index: Index. |
| 1457 | * @max_scan: Maximum range to search. |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1458 | * |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1459 | * Search the range [index, min(index + max_scan - 1, ULONG_MAX)] for the |
| 1460 | * gap with the lowest index. |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1461 | * |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1462 | * This function may be called under the rcu_read_lock. However, this will |
| 1463 | * not atomically search a snapshot of the cache at a single point in time. |
| 1464 | * For example, if a gap is created at index 5, then subsequently a gap is |
| 1465 | * created at index 10, page_cache_next_miss covering both indices may |
| 1466 | * return 10 if called under the rcu_read_lock. |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1467 | * |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1468 | * Return: The index of the gap if found, otherwise an index outside the |
| 1469 | * range specified (in which case 'return - index >= max_scan' will be true). |
| 1470 | * In the rare case of index wrap-around, 0 will be returned. |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1471 | */ |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1472 | pgoff_t page_cache_next_miss(struct address_space *mapping, |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1473 | pgoff_t index, unsigned long max_scan) |
| 1474 | { |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1475 | XA_STATE(xas, &mapping->i_pages, index); |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1476 | |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1477 | while (max_scan--) { |
| 1478 | void *entry = xas_next(&xas); |
| 1479 | if (!entry || xa_is_value(entry)) |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1480 | break; |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1481 | if (xas.xa_index == 0) |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1482 | break; |
| 1483 | } |
| 1484 | |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1485 | return xas.xa_index; |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1486 | } |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1487 | EXPORT_SYMBOL(page_cache_next_miss); |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1488 | |
| 1489 | /** |
Laurent Dufour | 2346a56 | 2019-05-13 17:21:29 -0700 | [diff] [blame] | 1490 | * page_cache_prev_miss() - Find the previous gap in the page cache. |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1491 | * @mapping: Mapping. |
| 1492 | * @index: Index. |
| 1493 | * @max_scan: Maximum range to search. |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1494 | * |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1495 | * Search the range [max(index - max_scan + 1, 0), index] for the |
| 1496 | * gap with the highest index. |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1497 | * |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1498 | * This function may be called under the rcu_read_lock. However, this will |
| 1499 | * not atomically search a snapshot of the cache at a single point in time. |
| 1500 | * For example, if a gap is created at index 10, then subsequently a gap is |
| 1501 | * created at index 5, page_cache_prev_miss() covering both indices may |
| 1502 | * return 5 if called under the rcu_read_lock. |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1503 | * |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1504 | * Return: The index of the gap if found, otherwise an index outside the |
| 1505 | * range specified (in which case 'index - return >= max_scan' will be true). |
| 1506 | * In the rare case of wrap-around, ULONG_MAX will be returned. |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1507 | */ |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1508 | pgoff_t page_cache_prev_miss(struct address_space *mapping, |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1509 | pgoff_t index, unsigned long max_scan) |
| 1510 | { |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1511 | XA_STATE(xas, &mapping->i_pages, index); |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1512 | |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1513 | while (max_scan--) { |
| 1514 | void *entry = xas_prev(&xas); |
| 1515 | if (!entry || xa_is_value(entry)) |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1516 | break; |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1517 | if (xas.xa_index == ULONG_MAX) |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1518 | break; |
| 1519 | } |
| 1520 | |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1521 | return xas.xa_index; |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1522 | } |
Matthew Wilcox | 0d3f929 | 2017-11-21 14:07:06 -0500 | [diff] [blame] | 1523 | EXPORT_SYMBOL(page_cache_prev_miss); |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 1524 | |
| 1525 | /** |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1526 | * find_get_entry - find and get a page cache entry |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1527 | * @mapping: the address_space to search |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1528 | * @offset: the page cache index |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1529 | * |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1530 | * Looks up the page cache slot at @mapping & @offset. If there is a |
| 1531 | * page cache page, it is returned with an increased refcount. |
| 1532 | * |
Johannes Weiner | 139b6a6 | 2014-05-06 12:50:05 -0700 | [diff] [blame] | 1533 | * If the slot holds a shadow entry of a previously evicted page, or a |
| 1534 | * swap entry from shmem/tmpfs, it is returned. |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1535 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1536 | * Return: the found page or shadow entry, %NULL if nothing is found. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | */ |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1538 | struct page *find_get_entry(struct address_space *mapping, pgoff_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | { |
Matthew Wilcox | 4c7472c | 2018-05-16 16:12:50 -0400 | [diff] [blame] | 1540 | XA_STATE(xas, &mapping->i_pages, offset); |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1541 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1543 | rcu_read_lock(); |
| 1544 | repeat: |
Matthew Wilcox | 4c7472c | 2018-05-16 16:12:50 -0400 | [diff] [blame] | 1545 | xas_reset(&xas); |
| 1546 | page = xas_load(&xas); |
| 1547 | if (xas_retry(&xas, page)) |
| 1548 | goto repeat; |
| 1549 | /* |
| 1550 | * A shadow entry of a recently evicted page, or a swap entry from |
| 1551 | * shmem/tmpfs. Return it without attempting to raise page count. |
| 1552 | */ |
| 1553 | if (!page || xa_is_value(page)) |
| 1554 | goto out; |
Kirill A. Shutemov | 8392937 | 2016-07-26 15:26:04 -0700 | [diff] [blame] | 1555 | |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1556 | if (!page_cache_get_speculative(page)) |
Matthew Wilcox | 4c7472c | 2018-05-16 16:12:50 -0400 | [diff] [blame] | 1557 | goto repeat; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1558 | |
Matthew Wilcox | 4c7472c | 2018-05-16 16:12:50 -0400 | [diff] [blame] | 1559 | /* |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1560 | * Has the page moved or been split? |
Matthew Wilcox | 4c7472c | 2018-05-16 16:12:50 -0400 | [diff] [blame] | 1561 | * This is part of the lockless pagecache protocol. See |
| 1562 | * include/linux/pagemap.h for details. |
| 1563 | */ |
| 1564 | if (unlikely(page != xas_reload(&xas))) { |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1565 | put_page(page); |
Matthew Wilcox | 4c7472c | 2018-05-16 16:12:50 -0400 | [diff] [blame] | 1566 | goto repeat; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1567 | } |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1568 | page = find_subpage(page, offset); |
Nick Piggin | 27d20fd | 2010-11-11 14:05:19 -0800 | [diff] [blame] | 1569 | out: |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1570 | rcu_read_unlock(); |
| 1571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | return page; |
| 1573 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1575 | /** |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1576 | * find_lock_entry - locate, pin and lock a page cache entry |
| 1577 | * @mapping: the address_space to search |
| 1578 | * @offset: the page cache index |
| 1579 | * |
| 1580 | * Looks up the page cache slot at @mapping & @offset. If there is a |
| 1581 | * page cache page, it is returned locked and with an increased |
| 1582 | * refcount. |
| 1583 | * |
Johannes Weiner | 139b6a6 | 2014-05-06 12:50:05 -0700 | [diff] [blame] | 1584 | * If the slot holds a shadow entry of a previously evicted page, or a |
| 1585 | * swap entry from shmem/tmpfs, it is returned. |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1586 | * |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1587 | * find_lock_entry() may sleep. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1588 | * |
| 1589 | * Return: the found page or shadow entry, %NULL if nothing is found. |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1590 | */ |
| 1591 | struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | { |
| 1593 | struct page *page; |
| 1594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | repeat: |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1596 | page = find_get_entry(mapping, offset); |
Matthew Wilcox | 4c7472c | 2018-05-16 16:12:50 -0400 | [diff] [blame] | 1597 | if (page && !xa_is_value(page)) { |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1598 | lock_page(page); |
| 1599 | /* Has the page been truncated? */ |
Kirill A. Shutemov | 8392937 | 2016-07-26 15:26:04 -0700 | [diff] [blame] | 1600 | if (unlikely(page_mapping(page) != mapping)) { |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1601 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1602 | put_page(page); |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1603 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | } |
Kirill A. Shutemov | 8392937 | 2016-07-26 15:26:04 -0700 | [diff] [blame] | 1605 | VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | return page; |
| 1608 | } |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1609 | EXPORT_SYMBOL(find_lock_entry); |
| 1610 | |
| 1611 | /** |
Matthew Wilcox (Oracle) | 2294b32 | 2020-04-01 21:05:07 -0700 | [diff] [blame] | 1612 | * pagecache_get_page - Find and get a reference to a page. |
| 1613 | * @mapping: The address_space to search. |
| 1614 | * @index: The page index. |
| 1615 | * @fgp_flags: %FGP flags modify how the page is returned. |
| 1616 | * @gfp_mask: Memory allocation flags to use if %FGP_CREAT is specified. |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1617 | * |
Matthew Wilcox (Oracle) | 2294b32 | 2020-04-01 21:05:07 -0700 | [diff] [blame] | 1618 | * Looks up the page cache entry at @mapping & @index. |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1619 | * |
Matthew Wilcox (Oracle) | 2294b32 | 2020-04-01 21:05:07 -0700 | [diff] [blame] | 1620 | * @fgp_flags can be zero or more of these flags: |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1621 | * |
Matthew Wilcox (Oracle) | 2294b32 | 2020-04-01 21:05:07 -0700 | [diff] [blame] | 1622 | * * %FGP_ACCESSED - The page will be marked accessed. |
| 1623 | * * %FGP_LOCK - The page is returned locked. |
| 1624 | * * %FGP_CREAT - If no page is present then a new page is allocated using |
| 1625 | * @gfp_mask and added to the page cache and the VM's LRU list. |
| 1626 | * The page is returned locked and with an increased refcount. |
| 1627 | * * %FGP_FOR_MMAP - The caller wants to do its own locking dance if the |
| 1628 | * page is already in cache. If the page was allocated, unlock it before |
| 1629 | * returning so the caller can do the same dance. |
mchehab@s-opensource.com | 0e056eb | 2017-03-30 17:11:36 -0300 | [diff] [blame] | 1630 | * |
Matthew Wilcox (Oracle) | 2294b32 | 2020-04-01 21:05:07 -0700 | [diff] [blame] | 1631 | * If %FGP_LOCK or %FGP_CREAT are specified then the function may sleep even |
| 1632 | * if the %GFP flags specified for %FGP_CREAT are atomic. |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1633 | * |
| 1634 | * If there is a page cache page, it is returned with an increased refcount. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1635 | * |
Matthew Wilcox (Oracle) | 2294b32 | 2020-04-01 21:05:07 -0700 | [diff] [blame] | 1636 | * Return: The found page or %NULL otherwise. |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1637 | */ |
Matthew Wilcox (Oracle) | 2294b32 | 2020-04-01 21:05:07 -0700 | [diff] [blame] | 1638 | struct page *pagecache_get_page(struct address_space *mapping, pgoff_t index, |
| 1639 | int fgp_flags, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | { |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1641 | struct page *page; |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | repeat: |
Matthew Wilcox (Oracle) | 2294b32 | 2020-04-01 21:05:07 -0700 | [diff] [blame] | 1644 | page = find_get_entry(mapping, index); |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 1645 | if (xa_is_value(page)) |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1646 | page = NULL; |
| 1647 | if (!page) |
| 1648 | goto no_page; |
| 1649 | |
| 1650 | if (fgp_flags & FGP_LOCK) { |
| 1651 | if (fgp_flags & FGP_NOWAIT) { |
| 1652 | if (!trylock_page(page)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1653 | put_page(page); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1654 | return NULL; |
| 1655 | } |
| 1656 | } else { |
| 1657 | lock_page(page); |
| 1658 | } |
| 1659 | |
| 1660 | /* Has the page been truncated? */ |
Song Liu | 3189543 | 2019-09-23 15:37:47 -0700 | [diff] [blame] | 1661 | if (unlikely(compound_head(page)->mapping != mapping)) { |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1662 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1663 | put_page(page); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1664 | goto repeat; |
| 1665 | } |
Matthew Wilcox (Oracle) | 2294b32 | 2020-04-01 21:05:07 -0700 | [diff] [blame] | 1666 | VM_BUG_ON_PAGE(page->index != index, page); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1667 | } |
| 1668 | |
Kirill Tkhai | c16eb00 | 2018-12-28 00:37:35 -0800 | [diff] [blame] | 1669 | if (fgp_flags & FGP_ACCESSED) |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1670 | mark_page_accessed(page); |
| 1671 | |
| 1672 | no_page: |
| 1673 | if (!page && (fgp_flags & FGP_CREAT)) { |
| 1674 | int err; |
| 1675 | if ((fgp_flags & FGP_WRITE) && mapping_cap_account_dirty(mapping)) |
Michal Hocko | 45f87de | 2014-12-29 20:30:35 +0100 | [diff] [blame] | 1676 | gfp_mask |= __GFP_WRITE; |
| 1677 | if (fgp_flags & FGP_NOFS) |
| 1678 | gfp_mask &= ~__GFP_FS; |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1679 | |
Michal Hocko | 45f87de | 2014-12-29 20:30:35 +0100 | [diff] [blame] | 1680 | page = __page_cache_alloc(gfp_mask); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1681 | if (!page) |
| 1682 | return NULL; |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1683 | |
Josef Bacik | a75d4c3 | 2019-03-13 11:44:14 -0700 | [diff] [blame] | 1684 | if (WARN_ON_ONCE(!(fgp_flags & (FGP_LOCK | FGP_FOR_MMAP)))) |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1685 | fgp_flags |= FGP_LOCK; |
| 1686 | |
Hugh Dickins | eb39d61 | 2014-08-06 16:06:43 -0700 | [diff] [blame] | 1687 | /* Init accessed so avoid atomic mark_page_accessed later */ |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1688 | if (fgp_flags & FGP_ACCESSED) |
Hugh Dickins | eb39d61 | 2014-08-06 16:06:43 -0700 | [diff] [blame] | 1689 | __SetPageReferenced(page); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1690 | |
Matthew Wilcox (Oracle) | 2294b32 | 2020-04-01 21:05:07 -0700 | [diff] [blame] | 1691 | err = add_to_page_cache_lru(page, mapping, index, gfp_mask); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1692 | if (unlikely(err)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1693 | put_page(page); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 1694 | page = NULL; |
| 1695 | if (err == -EEXIST) |
| 1696 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | } |
Josef Bacik | a75d4c3 | 2019-03-13 11:44:14 -0700 | [diff] [blame] | 1698 | |
| 1699 | /* |
| 1700 | * add_to_page_cache_lru locks the page, and for mmap we expect |
| 1701 | * an unlocked page. |
| 1702 | */ |
| 1703 | if (page && (fgp_flags & FGP_FOR_MMAP)) |
| 1704 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | } |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | return page; |
| 1708 | } |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 1709 | EXPORT_SYMBOL(pagecache_get_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | |
| 1711 | /** |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1712 | * find_get_entries - gang pagecache lookup |
| 1713 | * @mapping: The address_space to search |
| 1714 | * @start: The starting page cache index |
| 1715 | * @nr_entries: The maximum number of entries |
| 1716 | * @entries: Where the resulting entries are placed |
| 1717 | * @indices: The cache indices corresponding to the entries in @entries |
| 1718 | * |
| 1719 | * find_get_entries() will search for and return a group of up to |
| 1720 | * @nr_entries entries in the mapping. The entries are placed at |
| 1721 | * @entries. find_get_entries() takes a reference against any actual |
| 1722 | * pages it returns. |
| 1723 | * |
| 1724 | * The search returns a group of mapping-contiguous page cache entries |
| 1725 | * with ascending indexes. There may be holes in the indices due to |
| 1726 | * not-present pages. |
| 1727 | * |
Johannes Weiner | 139b6a6 | 2014-05-06 12:50:05 -0700 | [diff] [blame] | 1728 | * Any shadow entries of evicted pages, or swap entries from |
| 1729 | * shmem/tmpfs, are included in the returned array. |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1730 | * |
Hugh Dickins | 71725ed | 2020-04-06 20:07:57 -0700 | [diff] [blame] | 1731 | * If it finds a Transparent Huge Page, head or tail, find_get_entries() |
| 1732 | * stops at that page: the caller is likely to have a better way to handle |
| 1733 | * the compound page as a whole, and then skip its extent, than repeatedly |
| 1734 | * calling find_get_entries() to return all its tails. |
| 1735 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1736 | * Return: the number of pages and shadow entries which were found. |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1737 | */ |
| 1738 | unsigned find_get_entries(struct address_space *mapping, |
| 1739 | pgoff_t start, unsigned int nr_entries, |
| 1740 | struct page **entries, pgoff_t *indices) |
| 1741 | { |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1742 | XA_STATE(xas, &mapping->i_pages, start); |
| 1743 | struct page *page; |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1744 | unsigned int ret = 0; |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1745 | |
| 1746 | if (!nr_entries) |
| 1747 | return 0; |
| 1748 | |
| 1749 | rcu_read_lock(); |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1750 | xas_for_each(&xas, page, ULONG_MAX) { |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1751 | if (xas_retry(&xas, page)) |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1752 | continue; |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1753 | /* |
| 1754 | * A shadow entry of a recently evicted page, a swap |
| 1755 | * entry from shmem/tmpfs or a DAX entry. Return it |
| 1756 | * without attempting to raise page count. |
| 1757 | */ |
| 1758 | if (xa_is_value(page)) |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1759 | goto export; |
Kirill A. Shutemov | 8392937 | 2016-07-26 15:26:04 -0700 | [diff] [blame] | 1760 | |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1761 | if (!page_cache_get_speculative(page)) |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1762 | goto retry; |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1763 | |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1764 | /* Has the page moved or been split? */ |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1765 | if (unlikely(page != xas_reload(&xas))) |
| 1766 | goto put_page; |
| 1767 | |
Hugh Dickins | 71725ed | 2020-04-06 20:07:57 -0700 | [diff] [blame] | 1768 | /* |
| 1769 | * Terminate early on finding a THP, to allow the caller to |
| 1770 | * handle it all at once; but continue if this is hugetlbfs. |
| 1771 | */ |
| 1772 | if (PageTransHuge(page) && !PageHuge(page)) { |
| 1773 | page = find_subpage(page, xas.xa_index); |
| 1774 | nr_entries = ret + 1; |
| 1775 | } |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1776 | export: |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1777 | indices[ret] = xas.xa_index; |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1778 | entries[ret] = page; |
| 1779 | if (++ret == nr_entries) |
| 1780 | break; |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1781 | continue; |
| 1782 | put_page: |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1783 | put_page(page); |
Matthew Wilcox | f280bf0 | 2018-05-16 17:20:45 -0400 | [diff] [blame] | 1784 | retry: |
| 1785 | xas_reset(&xas); |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1786 | } |
| 1787 | rcu_read_unlock(); |
| 1788 | return ret; |
| 1789 | } |
| 1790 | |
| 1791 | /** |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1792 | * find_get_pages_range - gang pagecache lookup |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | * @mapping: The address_space to search |
| 1794 | * @start: The starting page index |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1795 | * @end: The final page index (inclusive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | * @nr_pages: The maximum number of pages |
| 1797 | * @pages: Where the resulting pages are placed |
| 1798 | * |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1799 | * find_get_pages_range() will search for and return a group of up to @nr_pages |
| 1800 | * pages in the mapping starting at index @start and up to index @end |
| 1801 | * (inclusive). The pages are placed at @pages. find_get_pages_range() takes |
| 1802 | * a reference against the returned pages. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | * |
| 1804 | * The search returns a group of mapping-contiguous pages with ascending |
| 1805 | * indexes. There may be holes in the indices due to not-present pages. |
Jan Kara | d72dc8a | 2017-09-06 16:21:18 -0700 | [diff] [blame] | 1806 | * We also update @start to index the next page for the traversal. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1808 | * Return: the number of pages which were found. If this number is |
| 1809 | * smaller than @nr_pages, the end of specified range has been |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1810 | * reached. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | */ |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1812 | unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start, |
| 1813 | pgoff_t end, unsigned int nr_pages, |
| 1814 | struct page **pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | { |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1816 | XA_STATE(xas, &mapping->i_pages, *start); |
| 1817 | struct page *page; |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1818 | unsigned ret = 0; |
| 1819 | |
| 1820 | if (unlikely(!nr_pages)) |
| 1821 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1823 | rcu_read_lock(); |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1824 | xas_for_each(&xas, page, end) { |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1825 | if (xas_retry(&xas, page)) |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1826 | continue; |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1827 | /* Skip over shadow, swap and DAX entries */ |
| 1828 | if (xa_is_value(page)) |
Hugh Dickins | 8079b1c | 2011-08-03 16:21:28 -0700 | [diff] [blame] | 1829 | continue; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1830 | |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1831 | if (!page_cache_get_speculative(page)) |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1832 | goto retry; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1833 | |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1834 | /* Has the page moved or been split? */ |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1835 | if (unlikely(page != xas_reload(&xas))) |
| 1836 | goto put_page; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1837 | |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1838 | pages[ret] = find_subpage(page, xas.xa_index); |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1839 | if (++ret == nr_pages) { |
Yu Zhao | 5d3ee42 | 2019-03-05 15:49:17 -0800 | [diff] [blame] | 1840 | *start = xas.xa_index + 1; |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1841 | goto out; |
| 1842 | } |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1843 | continue; |
| 1844 | put_page: |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1845 | put_page(page); |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1846 | retry: |
| 1847 | xas_reset(&xas); |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1848 | } |
Hugh Dickins | 5b280c0 | 2011-03-22 16:33:07 -0700 | [diff] [blame] | 1849 | |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1850 | /* |
| 1851 | * We come here when there is no page beyond @end. We take care to not |
| 1852 | * overflow the index @start as it confuses some of the callers. This |
Matthew Wilcox | fd1b3ce | 2018-05-16 17:38:56 -0400 | [diff] [blame] | 1853 | * breaks the iteration when there is a page at index -1 but that is |
Jan Kara | b947cee | 2017-09-06 16:21:21 -0700 | [diff] [blame] | 1854 | * already broken anyway. |
| 1855 | */ |
| 1856 | if (end == (pgoff_t)-1) |
| 1857 | *start = (pgoff_t)-1; |
| 1858 | else |
| 1859 | *start = end + 1; |
| 1860 | out: |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1861 | rcu_read_unlock(); |
Jan Kara | d72dc8a | 2017-09-06 16:21:18 -0700 | [diff] [blame] | 1862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | return ret; |
| 1864 | } |
| 1865 | |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 1866 | /** |
| 1867 | * find_get_pages_contig - gang contiguous pagecache lookup |
| 1868 | * @mapping: The address_space to search |
| 1869 | * @index: The starting page index |
| 1870 | * @nr_pages: The maximum number of pages |
| 1871 | * @pages: Where the resulting pages are placed |
| 1872 | * |
| 1873 | * find_get_pages_contig() works exactly like find_get_pages(), except |
| 1874 | * that the returned number of pages are guaranteed to be contiguous. |
| 1875 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1876 | * Return: the number of pages which were found. |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 1877 | */ |
| 1878 | unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index, |
| 1879 | unsigned int nr_pages, struct page **pages) |
| 1880 | { |
Matthew Wilcox | 3ece58a | 2018-05-16 18:00:33 -0400 | [diff] [blame] | 1881 | XA_STATE(xas, &mapping->i_pages, index); |
| 1882 | struct page *page; |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1883 | unsigned int ret = 0; |
| 1884 | |
| 1885 | if (unlikely(!nr_pages)) |
| 1886 | return 0; |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 1887 | |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1888 | rcu_read_lock(); |
Matthew Wilcox | 3ece58a | 2018-05-16 18:00:33 -0400 | [diff] [blame] | 1889 | for (page = xas_load(&xas); page; page = xas_next(&xas)) { |
Matthew Wilcox | 3ece58a | 2018-05-16 18:00:33 -0400 | [diff] [blame] | 1890 | if (xas_retry(&xas, page)) |
| 1891 | continue; |
| 1892 | /* |
| 1893 | * If the entry has been swapped out, we can stop looking. |
| 1894 | * No current caller is looking for DAX entries. |
| 1895 | */ |
| 1896 | if (xa_is_value(page)) |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1897 | break; |
Hugh Dickins | 9d8aa4e | 2011-03-22 16:33:06 -0700 | [diff] [blame] | 1898 | |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1899 | if (!page_cache_get_speculative(page)) |
Matthew Wilcox | 3ece58a | 2018-05-16 18:00:33 -0400 | [diff] [blame] | 1900 | goto retry; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1901 | |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1902 | /* Has the page moved or been split? */ |
Matthew Wilcox | 3ece58a | 2018-05-16 18:00:33 -0400 | [diff] [blame] | 1903 | if (unlikely(page != xas_reload(&xas))) |
| 1904 | goto put_page; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1905 | |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1906 | pages[ret] = find_subpage(page, xas.xa_index); |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1907 | if (++ret == nr_pages) |
| 1908 | break; |
Matthew Wilcox | 3ece58a | 2018-05-16 18:00:33 -0400 | [diff] [blame] | 1909 | continue; |
| 1910 | put_page: |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1911 | put_page(page); |
Matthew Wilcox | 3ece58a | 2018-05-16 18:00:33 -0400 | [diff] [blame] | 1912 | retry: |
| 1913 | xas_reset(&xas); |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 1914 | } |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1915 | rcu_read_unlock(); |
| 1916 | return ret; |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 1917 | } |
David Howells | ef71c15 | 2007-05-09 02:33:44 -0700 | [diff] [blame] | 1918 | EXPORT_SYMBOL(find_get_pages_contig); |
Jens Axboe | ebf4350 | 2006-04-27 08:46:01 +0200 | [diff] [blame] | 1919 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1920 | /** |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1921 | * find_get_pages_range_tag - find and return pages in given range matching @tag |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1922 | * @mapping: the address_space to search |
| 1923 | * @index: the starting page index |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1924 | * @end: The final page index (inclusive) |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1925 | * @tag: the tag index |
| 1926 | * @nr_pages: the maximum number of pages |
| 1927 | * @pages: where the resulting pages are placed |
| 1928 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | * Like find_get_pages, except we only return pages which are tagged with |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 1930 | * @tag. We update @index to index the next page for the traversal. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 1931 | * |
| 1932 | * Return: the number of pages which were found. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | */ |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1934 | unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index, |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1935 | pgoff_t end, xa_mark_t tag, unsigned int nr_pages, |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1936 | struct page **pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | { |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1938 | XA_STATE(xas, &mapping->i_pages, *index); |
| 1939 | struct page *page; |
Konstantin Khlebnikov | 0fc9d10 | 2012-03-28 14:42:54 -0700 | [diff] [blame] | 1940 | unsigned ret = 0; |
| 1941 | |
| 1942 | if (unlikely(!nr_pages)) |
| 1943 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1945 | rcu_read_lock(); |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1946 | xas_for_each_marked(&xas, page, end, tag) { |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1947 | if (xas_retry(&xas, page)) |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1948 | continue; |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1949 | /* |
| 1950 | * Shadow entries should never be tagged, but this iteration |
| 1951 | * is lockless so there is a window for page reclaim to evict |
| 1952 | * a page we saw tagged. Skip over it. |
| 1953 | */ |
| 1954 | if (xa_is_value(page)) |
Johannes Weiner | 139b6a6 | 2014-05-06 12:50:05 -0700 | [diff] [blame] | 1955 | continue; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1956 | |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1957 | if (!page_cache_get_speculative(page)) |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1958 | goto retry; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1959 | |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1960 | /* Has the page moved or been split? */ |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1961 | if (unlikely(page != xas_reload(&xas))) |
| 1962 | goto put_page; |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1963 | |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1964 | pages[ret] = find_subpage(page, xas.xa_index); |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1965 | if (++ret == nr_pages) { |
Yu Zhao | 5d3ee42 | 2019-03-05 15:49:17 -0800 | [diff] [blame] | 1966 | *index = xas.xa_index + 1; |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1967 | goto out; |
| 1968 | } |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1969 | continue; |
| 1970 | put_page: |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 1971 | put_page(page); |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1972 | retry: |
| 1973 | xas_reset(&xas); |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1974 | } |
Hugh Dickins | 5b280c0 | 2011-03-22 16:33:07 -0700 | [diff] [blame] | 1975 | |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1976 | /* |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1977 | * We come here when we got to @end. We take care to not overflow the |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1978 | * index @index as it confuses some of the callers. This breaks the |
Matthew Wilcox | a690697 | 2018-05-16 18:12:54 -0400 | [diff] [blame] | 1979 | * iteration when there is a page at index -1 but that is already |
| 1980 | * broken anyway. |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1981 | */ |
| 1982 | if (end == (pgoff_t)-1) |
| 1983 | *index = (pgoff_t)-1; |
| 1984 | else |
| 1985 | *index = end + 1; |
| 1986 | out: |
Nick Piggin | a60637c | 2008-07-25 19:45:31 -0700 | [diff] [blame] | 1987 | rcu_read_unlock(); |
| 1988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | return ret; |
| 1990 | } |
Jan Kara | 72b045a | 2017-11-15 17:34:33 -0800 | [diff] [blame] | 1991 | EXPORT_SYMBOL(find_get_pages_range_tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | |
Wu Fengguang | 76d42bd | 2006-06-25 05:48:43 -0700 | [diff] [blame] | 1993 | /* |
| 1994 | * CD/DVDs are error prone. When a medium error occurs, the driver may fail |
| 1995 | * a _large_ part of the i/o request. Imagine the worst scenario: |
| 1996 | * |
| 1997 | * ---R__________________________________________B__________ |
| 1998 | * ^ reading here ^ bad block(assume 4k) |
| 1999 | * |
| 2000 | * read(R) => miss => readahead(R...B) => media error => frustrating retries |
| 2001 | * => failing the whole request => read(R) => read(R+1) => |
| 2002 | * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) => |
| 2003 | * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) => |
| 2004 | * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ...... |
| 2005 | * |
| 2006 | * It is going insane. Fix it by quickly scaling down the readahead size. |
| 2007 | */ |
Souptick Joarder | 0f8e2db | 2020-04-01 21:04:50 -0700 | [diff] [blame] | 2008 | static void shrink_readahead_size_eio(struct file_ra_state *ra) |
Wu Fengguang | 76d42bd | 2006-06-25 05:48:43 -0700 | [diff] [blame] | 2009 | { |
Wu Fengguang | 76d42bd | 2006-06-25 05:48:43 -0700 | [diff] [blame] | 2010 | ra->ra_pages /= 4; |
Wu Fengguang | 76d42bd | 2006-06-25 05:48:43 -0700 | [diff] [blame] | 2011 | } |
| 2012 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 2013 | /** |
Christoph Hellwig | 47c27bc | 2017-08-29 16:13:18 +0200 | [diff] [blame] | 2014 | * generic_file_buffered_read - generic file read routine |
| 2015 | * @iocb: the iocb to read |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2016 | * @iter: data destination |
| 2017 | * @written: already copied |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 2018 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | * This is a generic file read routine, and uses the |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 2020 | * mapping->a_ops->readpage() function for the actual low-level stuff. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | * |
| 2022 | * This is really ugly. But the goto's actually try to clarify some |
| 2023 | * of the logic when it comes to error handling etc. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2024 | * |
| 2025 | * Return: |
| 2026 | * * total number of bytes copied, including those the were already @written |
| 2027 | * * negative error code if nothing was copied |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | */ |
Goldwyn Rodrigues | d85dc2e | 2019-08-30 12:09:24 -0500 | [diff] [blame] | 2029 | ssize_t generic_file_buffered_read(struct kiocb *iocb, |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2030 | struct iov_iter *iter, ssize_t written) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | { |
Christoph Hellwig | 47c27bc | 2017-08-29 16:13:18 +0200 | [diff] [blame] | 2032 | struct file *filp = iocb->ki_filp; |
Christoph Hellwig | 36e7891 | 2008-02-08 04:21:24 -0800 | [diff] [blame] | 2033 | struct address_space *mapping = filp->f_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | struct inode *inode = mapping->host; |
Christoph Hellwig | 36e7891 | 2008-02-08 04:21:24 -0800 | [diff] [blame] | 2035 | struct file_ra_state *ra = &filp->f_ra; |
Christoph Hellwig | 47c27bc | 2017-08-29 16:13:18 +0200 | [diff] [blame] | 2036 | loff_t *ppos = &iocb->ki_pos; |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 2037 | pgoff_t index; |
| 2038 | pgoff_t last_index; |
| 2039 | pgoff_t prev_index; |
| 2040 | unsigned long offset; /* offset into pagecache page */ |
Jan Kara | ec0f163 | 2007-05-06 14:49:25 -0700 | [diff] [blame] | 2041 | unsigned int prev_offset; |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2042 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | |
Wei Fang | c2a9737 | 2016-10-07 17:01:52 -0700 | [diff] [blame] | 2044 | if (unlikely(*ppos >= inode->i_sb->s_maxbytes)) |
Linus Torvalds | d05c5f7 | 2016-12-14 12:45:25 -0800 | [diff] [blame] | 2045 | return 0; |
Wei Fang | c2a9737 | 2016-10-07 17:01:52 -0700 | [diff] [blame] | 2046 | iov_iter_truncate(iter, inode->i_sb->s_maxbytes); |
| 2047 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2048 | index = *ppos >> PAGE_SHIFT; |
| 2049 | prev_index = ra->prev_pos >> PAGE_SHIFT; |
| 2050 | prev_offset = ra->prev_pos & (PAGE_SIZE-1); |
| 2051 | last_index = (*ppos + iter->count + PAGE_SIZE-1) >> PAGE_SHIFT; |
| 2052 | offset = *ppos & ~PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | for (;;) { |
| 2055 | struct page *page; |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 2056 | pgoff_t end_index; |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 2057 | loff_t isize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | unsigned long nr, ret; |
| 2059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | find_page: |
Michal Hocko | 5abf186 | 2017-02-03 13:13:29 -0800 | [diff] [blame] | 2062 | if (fatal_signal_pending(current)) { |
| 2063 | error = -EINTR; |
| 2064 | goto out; |
| 2065 | } |
| 2066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | page = find_get_page(mapping, index); |
Fengguang Wu | 3ea89ee | 2007-07-19 01:48:02 -0700 | [diff] [blame] | 2068 | if (!page) { |
Andreas Gruenbacher | 41da51b | 2019-11-21 23:25:07 +0000 | [diff] [blame] | 2069 | if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_NOIO)) |
Milosz Tanski | 3239d83 | 2017-08-29 16:13:19 +0200 | [diff] [blame] | 2070 | goto would_block; |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 2071 | page_cache_sync_readahead(mapping, |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 2072 | ra, filp, |
Fengguang Wu | 3ea89ee | 2007-07-19 01:48:02 -0700 | [diff] [blame] | 2073 | index, last_index - index); |
| 2074 | page = find_get_page(mapping, index); |
| 2075 | if (unlikely(page == NULL)) |
| 2076 | goto no_cached_page; |
| 2077 | } |
| 2078 | if (PageReadahead(page)) { |
Andreas Gruenbacher | 41da51b | 2019-11-21 23:25:07 +0000 | [diff] [blame] | 2079 | if (iocb->ki_flags & IOCB_NOIO) { |
| 2080 | put_page(page); |
| 2081 | goto out; |
| 2082 | } |
Rusty Russell | cf914a7 | 2007-07-19 01:48:08 -0700 | [diff] [blame] | 2083 | page_cache_async_readahead(mapping, |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 2084 | ra, filp, page, |
Fengguang Wu | 3ea89ee | 2007-07-19 01:48:02 -0700 | [diff] [blame] | 2085 | index, last_index - index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | } |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2087 | if (!PageUptodate(page)) { |
Milosz Tanski | 3239d83 | 2017-08-29 16:13:19 +0200 | [diff] [blame] | 2088 | if (iocb->ki_flags & IOCB_NOWAIT) { |
| 2089 | put_page(page); |
| 2090 | goto would_block; |
| 2091 | } |
| 2092 | |
Mel Gorman | ebded02 | 2016-03-15 14:55:39 -0700 | [diff] [blame] | 2093 | /* |
| 2094 | * See comment in do_read_cache_page on why |
| 2095 | * wait_on_page_locked is used to avoid unnecessarily |
| 2096 | * serialisations and why it's safe. |
| 2097 | */ |
Bart Van Assche | c4b209a | 2016-10-07 16:58:33 -0700 | [diff] [blame] | 2098 | error = wait_on_page_locked_killable(page); |
| 2099 | if (unlikely(error)) |
| 2100 | goto readpage_error; |
Mel Gorman | ebded02 | 2016-03-15 14:55:39 -0700 | [diff] [blame] | 2101 | if (PageUptodate(page)) |
| 2102 | goto page_ok; |
| 2103 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2104 | if (inode->i_blkbits == PAGE_SHIFT || |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2105 | !mapping->a_ops->is_partially_uptodate) |
| 2106 | goto page_not_up_to_date; |
Eryu Guan | 6d6d36b | 2016-11-01 15:43:07 +0800 | [diff] [blame] | 2107 | /* pipes can't handle partially uptodate pages */ |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 2108 | if (unlikely(iov_iter_is_pipe(iter))) |
Eryu Guan | 6d6d36b | 2016-11-01 15:43:07 +0800 | [diff] [blame] | 2109 | goto page_not_up_to_date; |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 2110 | if (!trylock_page(page)) |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2111 | goto page_not_up_to_date; |
Dave Hansen | 8d056cb | 2010-11-11 14:05:15 -0800 | [diff] [blame] | 2112 | /* Did it get truncated before we got the lock? */ |
| 2113 | if (!page->mapping) |
| 2114 | goto page_not_up_to_date_locked; |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2115 | if (!mapping->a_ops->is_partially_uptodate(page, |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2116 | offset, iter->count)) |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2117 | goto page_not_up_to_date_locked; |
| 2118 | unlock_page(page); |
| 2119 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | page_ok: |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 2121 | /* |
| 2122 | * i_size must be checked after we know the page is Uptodate. |
| 2123 | * |
| 2124 | * Checking i_size after the check allows us to calculate |
| 2125 | * the correct value for "nr", which means the zero-filled |
| 2126 | * part of the page is not copied back to userspace (unless |
| 2127 | * another truncate extends the file - this is desired though). |
| 2128 | */ |
| 2129 | |
| 2130 | isize = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2131 | end_index = (isize - 1) >> PAGE_SHIFT; |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 2132 | if (unlikely(!isize || index > end_index)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2133 | put_page(page); |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 2134 | goto out; |
| 2135 | } |
| 2136 | |
| 2137 | /* nr is the maximum number of bytes to copy from this page */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2138 | nr = PAGE_SIZE; |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 2139 | if (index == end_index) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2140 | nr = ((isize - 1) & ~PAGE_MASK) + 1; |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 2141 | if (nr <= offset) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2142 | put_page(page); |
NeilBrown | a32ea1e | 2007-07-17 04:03:04 -0700 | [diff] [blame] | 2143 | goto out; |
| 2144 | } |
| 2145 | } |
| 2146 | nr = nr - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | |
| 2148 | /* If users can be writing to this page using arbitrary |
| 2149 | * virtual addresses, take care about potential aliasing |
| 2150 | * before reading the page on the kernel side. |
| 2151 | */ |
| 2152 | if (mapping_writably_mapped(mapping)) |
| 2153 | flush_dcache_page(page); |
| 2154 | |
| 2155 | /* |
Jan Kara | ec0f163 | 2007-05-06 14:49:25 -0700 | [diff] [blame] | 2156 | * When a sequential read accesses a page several times, |
| 2157 | * only mark it as accessed the first time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | */ |
Jan Kara | ec0f163 | 2007-05-06 14:49:25 -0700 | [diff] [blame] | 2159 | if (prev_index != index || offset != prev_offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | mark_page_accessed(page); |
| 2161 | prev_index = index; |
| 2162 | |
| 2163 | /* |
| 2164 | * Ok, we have the page, and it's up-to-date, so |
| 2165 | * now we can copy it to user space... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | */ |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2167 | |
| 2168 | ret = copy_page_to_iter(page, offset, nr, iter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | offset += ret; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2170 | index += offset >> PAGE_SHIFT; |
| 2171 | offset &= ~PAGE_MASK; |
Jan Kara | 6ce745e | 2007-05-06 14:49:26 -0700 | [diff] [blame] | 2172 | prev_offset = offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2174 | put_page(page); |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2175 | written += ret; |
| 2176 | if (!iov_iter_count(iter)) |
| 2177 | goto out; |
| 2178 | if (ret < nr) { |
| 2179 | error = -EFAULT; |
| 2180 | goto out; |
| 2181 | } |
| 2182 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | |
| 2184 | page_not_up_to_date: |
| 2185 | /* Get exclusive access to the page ... */ |
Oleg Nesterov | 8546232 | 2008-06-08 21:20:43 +0400 | [diff] [blame] | 2186 | error = lock_page_killable(page); |
| 2187 | if (unlikely(error)) |
| 2188 | goto readpage_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | |
Hisashi Hifumi | 8ab22b9 | 2008-07-28 15:46:36 -0700 | [diff] [blame] | 2190 | page_not_up_to_date_locked: |
Nick Piggin | da6052f | 2006-09-25 23:31:35 -0700 | [diff] [blame] | 2191 | /* Did it get truncated before we got the lock? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | if (!page->mapping) { |
| 2193 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2194 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | continue; |
| 2196 | } |
| 2197 | |
| 2198 | /* Did somebody else fill it already? */ |
| 2199 | if (PageUptodate(page)) { |
| 2200 | unlock_page(page); |
| 2201 | goto page_ok; |
| 2202 | } |
| 2203 | |
| 2204 | readpage: |
Andreas Gruenbacher | 41da51b | 2019-11-21 23:25:07 +0000 | [diff] [blame] | 2205 | if (iocb->ki_flags & IOCB_NOIO) { |
| 2206 | unlock_page(page); |
| 2207 | put_page(page); |
| 2208 | goto would_block; |
| 2209 | } |
Jeff Moyer | 91803b4 | 2010-05-26 11:49:40 -0400 | [diff] [blame] | 2210 | /* |
| 2211 | * A previous I/O error may have been due to temporary |
| 2212 | * failures, eg. multipath errors. |
| 2213 | * PG_error will be set again if readpage fails. |
| 2214 | */ |
| 2215 | ClearPageError(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | /* Start the actual read. The read will unlock the page. */ |
| 2217 | error = mapping->a_ops->readpage(filp, page); |
| 2218 | |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 2219 | if (unlikely(error)) { |
| 2220 | if (error == AOP_TRUNCATED_PAGE) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2221 | put_page(page); |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2222 | error = 0; |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 2223 | goto find_page; |
| 2224 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | goto readpage_error; |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 2226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | |
| 2228 | if (!PageUptodate(page)) { |
Oleg Nesterov | 8546232 | 2008-06-08 21:20:43 +0400 | [diff] [blame] | 2229 | error = lock_page_killable(page); |
| 2230 | if (unlikely(error)) |
| 2231 | goto readpage_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | if (!PageUptodate(page)) { |
| 2233 | if (page->mapping == NULL) { |
| 2234 | /* |
Christoph Hellwig | 2ecdc82 | 2010-01-26 17:27:20 +0100 | [diff] [blame] | 2235 | * invalidate_mapping_pages got it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | */ |
| 2237 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2238 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | goto find_page; |
| 2240 | } |
| 2241 | unlock_page(page); |
Souptick Joarder | 0f8e2db | 2020-04-01 21:04:50 -0700 | [diff] [blame] | 2242 | shrink_readahead_size_eio(ra); |
Oleg Nesterov | 8546232 | 2008-06-08 21:20:43 +0400 | [diff] [blame] | 2243 | error = -EIO; |
| 2244 | goto readpage_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | } |
| 2246 | unlock_page(page); |
| 2247 | } |
| 2248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | goto page_ok; |
| 2250 | |
| 2251 | readpage_error: |
| 2252 | /* UHHUH! A synchronous read error occurred. Report it */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2253 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | goto out; |
| 2255 | |
| 2256 | no_cached_page: |
| 2257 | /* |
| 2258 | * Ok, it wasn't cached, so we need to create a new |
| 2259 | * page.. |
| 2260 | */ |
Mel Gorman | 453f85d | 2017-11-15 17:38:03 -0800 | [diff] [blame] | 2261 | page = page_cache_alloc(mapping); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2262 | if (!page) { |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2263 | error = -ENOMEM; |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2264 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | } |
Michal Hocko | 6afdb85 | 2015-06-24 16:58:06 -0700 | [diff] [blame] | 2266 | error = add_to_page_cache_lru(page, mapping, index, |
Michal Hocko | c62d255 | 2015-11-06 16:28:49 -0800 | [diff] [blame] | 2267 | mapping_gfp_constraint(mapping, GFP_KERNEL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | if (error) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2269 | put_page(page); |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2270 | if (error == -EEXIST) { |
| 2271 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | goto find_page; |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | goto out; |
| 2275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | goto readpage; |
| 2277 | } |
| 2278 | |
Milosz Tanski | 3239d83 | 2017-08-29 16:13:19 +0200 | [diff] [blame] | 2279 | would_block: |
| 2280 | error = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | out: |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 2282 | ra->prev_pos = prev_index; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2283 | ra->prev_pos <<= PAGE_SHIFT; |
Fengguang Wu | 7ff8107 | 2007-10-16 01:24:35 -0700 | [diff] [blame] | 2284 | ra->prev_pos |= prev_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2286 | *ppos = ((loff_t)index << PAGE_SHIFT) + offset; |
Krishna Kumar | 0c6aa26 | 2008-10-15 22:01:13 -0700 | [diff] [blame] | 2287 | file_accessed(filp); |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2288 | return written ? written : error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | } |
Goldwyn Rodrigues | d85dc2e | 2019-08-30 12:09:24 -0500 | [diff] [blame] | 2290 | EXPORT_SYMBOL_GPL(generic_file_buffered_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 2292 | /** |
Al Viro | 6abd232 | 2014-04-04 14:20:57 -0400 | [diff] [blame] | 2293 | * generic_file_read_iter - generic filesystem read routine |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 2294 | * @iocb: kernel I/O control block |
Al Viro | 6abd232 | 2014-04-04 14:20:57 -0400 | [diff] [blame] | 2295 | * @iter: destination for the data read |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 2296 | * |
Al Viro | 6abd232 | 2014-04-04 14:20:57 -0400 | [diff] [blame] | 2297 | * This is the "read_iter()" routine for all filesystems |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2298 | * that can use the page cache directly. |
Andreas Gruenbacher | 41da51b | 2019-11-21 23:25:07 +0000 | [diff] [blame] | 2299 | * |
| 2300 | * The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall |
| 2301 | * be returned when no data can be read without waiting for I/O requests |
| 2302 | * to complete; it doesn't prevent readahead. |
| 2303 | * |
| 2304 | * The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O |
| 2305 | * requests shall be made for the read or for readahead. When no data |
| 2306 | * can be read, -EAGAIN shall be returned. When readahead would be |
| 2307 | * triggered, a partial, possibly empty read shall be returned. |
| 2308 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2309 | * Return: |
| 2310 | * * number of bytes copied, even for partial reads |
Andreas Gruenbacher | 41da51b | 2019-11-21 23:25:07 +0000 | [diff] [blame] | 2311 | * * negative error code (or 0 if IOCB_NOIO) if nothing was read |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | */ |
| 2313 | ssize_t |
Al Viro | ed978a8 | 2014-03-05 22:53:04 -0500 | [diff] [blame] | 2314 | generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | { |
Nicolai Stange | e7080a4 | 2016-03-25 14:22:14 -0700 | [diff] [blame] | 2316 | size_t count = iov_iter_count(iter); |
Christoph Hellwig | 47c27bc | 2017-08-29 16:13:18 +0200 | [diff] [blame] | 2317 | ssize_t retval = 0; |
Nicolai Stange | e7080a4 | 2016-03-25 14:22:14 -0700 | [diff] [blame] | 2318 | |
| 2319 | if (!count) |
| 2320 | goto out; /* skip atime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | |
Al Viro | 2ba48ce | 2015-04-09 13:52:01 -0400 | [diff] [blame] | 2322 | if (iocb->ki_flags & IOCB_DIRECT) { |
Christoph Hellwig | 47c27bc | 2017-08-29 16:13:18 +0200 | [diff] [blame] | 2323 | struct file *file = iocb->ki_filp; |
Al Viro | ed978a8 | 2014-03-05 22:53:04 -0500 | [diff] [blame] | 2324 | struct address_space *mapping = file->f_mapping; |
| 2325 | struct inode *inode = mapping->host; |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 2326 | loff_t size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | size = i_size_read(inode); |
Goldwyn Rodrigues | 6be96d3 | 2017-06-20 07:05:44 -0500 | [diff] [blame] | 2329 | if (iocb->ki_flags & IOCB_NOWAIT) { |
| 2330 | if (filemap_range_has_page(mapping, iocb->ki_pos, |
| 2331 | iocb->ki_pos + count - 1)) |
| 2332 | return -EAGAIN; |
| 2333 | } else { |
| 2334 | retval = filemap_write_and_wait_range(mapping, |
| 2335 | iocb->ki_pos, |
| 2336 | iocb->ki_pos + count - 1); |
| 2337 | if (retval < 0) |
| 2338 | goto out; |
| 2339 | } |
Al Viro | ed978a8 | 2014-03-05 22:53:04 -0500 | [diff] [blame] | 2340 | |
Christoph Hellwig | 0d5b0cf | 2016-10-03 09:48:08 +1100 | [diff] [blame] | 2341 | file_accessed(file); |
| 2342 | |
Al Viro | 5ecda13 | 2017-04-13 14:13:36 -0400 | [diff] [blame] | 2343 | retval = mapping->a_ops->direct_IO(iocb, iter); |
Al Viro | c3a6902 | 2016-10-10 13:26:27 -0400 | [diff] [blame] | 2344 | if (retval >= 0) { |
Christoph Hellwig | c64fb5c | 2016-04-07 08:51:55 -0700 | [diff] [blame] | 2345 | iocb->ki_pos += retval; |
Al Viro | 5ecda13 | 2017-04-13 14:13:36 -0400 | [diff] [blame] | 2346 | count -= retval; |
Steven Whitehouse | 9fe55ee | 2014-01-24 14:42:22 +0000 | [diff] [blame] | 2347 | } |
Al Viro | 5b47d59 | 2017-05-08 13:54:47 -0400 | [diff] [blame] | 2348 | iov_iter_revert(iter, count - iov_iter_count(iter)); |
Josef Bacik | 66f998f | 2010-05-23 11:00:54 -0400 | [diff] [blame] | 2349 | |
Steven Whitehouse | 9fe55ee | 2014-01-24 14:42:22 +0000 | [diff] [blame] | 2350 | /* |
| 2351 | * Btrfs can have a short DIO read if we encounter |
| 2352 | * compressed extents, so if there was an error, or if |
| 2353 | * we've already read everything we wanted to, or if |
| 2354 | * there was a short read because we hit EOF, go ahead |
| 2355 | * and return. Otherwise fallthrough to buffered io for |
Matthew Wilcox | fbbbad4 | 2015-02-16 15:58:53 -0800 | [diff] [blame] | 2356 | * the rest of the read. Buffered reads will not work for |
| 2357 | * DAX files, so don't bother trying. |
Steven Whitehouse | 9fe55ee | 2014-01-24 14:42:22 +0000 | [diff] [blame] | 2358 | */ |
Al Viro | 5ecda13 | 2017-04-13 14:13:36 -0400 | [diff] [blame] | 2359 | if (retval < 0 || !count || iocb->ki_pos >= size || |
Christoph Hellwig | 0d5b0cf | 2016-10-03 09:48:08 +1100 | [diff] [blame] | 2360 | IS_DAX(inode)) |
Steven Whitehouse | 9fe55ee | 2014-01-24 14:42:22 +0000 | [diff] [blame] | 2361 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | } |
| 2363 | |
Christoph Hellwig | 47c27bc | 2017-08-29 16:13:18 +0200 | [diff] [blame] | 2364 | retval = generic_file_buffered_read(iocb, iter, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | out: |
| 2366 | return retval; |
| 2367 | } |
Al Viro | ed978a8 | 2014-03-05 22:53:04 -0500 | [diff] [blame] | 2368 | EXPORT_SYMBOL(generic_file_read_iter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | #ifdef CONFIG_MMU |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | #define MMAP_LOTSAMISS (100) |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2372 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2373 | * lock_page_maybe_drop_mmap - lock the page, possibly dropping the mmap_lock |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2374 | * @vmf - the vm_fault for this fault. |
| 2375 | * @page - the page to lock. |
| 2376 | * @fpin - the pointer to the file we may pin (or is already pinned). |
| 2377 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2378 | * This works similar to lock_page_or_retry in that it can drop the mmap_lock. |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2379 | * It differs in that it actually returns the page locked if it returns 1 and 0 |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2380 | * if it couldn't lock the page. If we did have to drop the mmap_lock then fpin |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2381 | * will point to the pinned file and needs to be fput()'ed at a later point. |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2382 | */ |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2383 | static int lock_page_maybe_drop_mmap(struct vm_fault *vmf, struct page *page, |
| 2384 | struct file **fpin) |
| 2385 | { |
| 2386 | if (trylock_page(page)) |
| 2387 | return 1; |
| 2388 | |
Linus Torvalds | 8b0f9fa | 2019-03-15 11:26:07 -0700 | [diff] [blame] | 2389 | /* |
| 2390 | * NOTE! This will make us return with VM_FAULT_RETRY, but with |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2391 | * the mmap_lock still held. That's how FAULT_FLAG_RETRY_NOWAIT |
Linus Torvalds | 8b0f9fa | 2019-03-15 11:26:07 -0700 | [diff] [blame] | 2392 | * is supposed to work. We have way too many special cases.. |
| 2393 | */ |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2394 | if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT) |
| 2395 | return 0; |
| 2396 | |
| 2397 | *fpin = maybe_unlock_mmap_for_io(vmf, *fpin); |
| 2398 | if (vmf->flags & FAULT_FLAG_KILLABLE) { |
| 2399 | if (__lock_page_killable(page)) { |
| 2400 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2401 | * We didn't have the right flags to drop the mmap_lock, |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2402 | * but all fault_handlers only check for fatal signals |
| 2403 | * if we return VM_FAULT_RETRY, so we need to drop the |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2404 | * mmap_lock here and return 0 if we don't have a fpin. |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2405 | */ |
| 2406 | if (*fpin == NULL) |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 2407 | mmap_read_unlock(vmf->vma->vm_mm); |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2408 | return 0; |
| 2409 | } |
| 2410 | } else |
| 2411 | __lock_page(page); |
| 2412 | return 1; |
| 2413 | } |
| 2414 | |
| 2415 | |
| 2416 | /* |
| 2417 | * Synchronous readahead happens when we don't even find a page in the page |
| 2418 | * cache at all. We don't want to perform IO under the mmap sem, so if we have |
| 2419 | * to drop the mmap sem we return the file that was pinned in order for us to do |
| 2420 | * that. If we didn't pin a file then we return NULL. The file that is |
| 2421 | * returned needs to be fput()'ed when we're done with it. |
| 2422 | */ |
| 2423 | static struct file *do_sync_mmap_readahead(struct vm_fault *vmf) |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2424 | { |
Josef Bacik | 2a1180f | 2019-03-13 11:44:18 -0700 | [diff] [blame] | 2425 | struct file *file = vmf->vma->vm_file; |
| 2426 | struct file_ra_state *ra = &file->f_ra; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2427 | struct address_space *mapping = file->f_mapping; |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2428 | struct file *fpin = NULL; |
Josef Bacik | 2a1180f | 2019-03-13 11:44:18 -0700 | [diff] [blame] | 2429 | pgoff_t offset = vmf->pgoff; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2430 | |
| 2431 | /* If we don't want any read-ahead, don't bother */ |
Josef Bacik | 2a1180f | 2019-03-13 11:44:18 -0700 | [diff] [blame] | 2432 | if (vmf->vma->vm_flags & VM_RAND_READ) |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2433 | return fpin; |
Wu Fengguang | 275b12b | 2011-05-24 17:12:28 -0700 | [diff] [blame] | 2434 | if (!ra->ra_pages) |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2435 | return fpin; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2436 | |
Josef Bacik | 2a1180f | 2019-03-13 11:44:18 -0700 | [diff] [blame] | 2437 | if (vmf->vma->vm_flags & VM_SEQ_READ) { |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2438 | fpin = maybe_unlock_mmap_for_io(vmf, fpin); |
Wu Fengguang | 7ffc59b | 2009-06-16 15:31:38 -0700 | [diff] [blame] | 2439 | page_cache_sync_readahead(mapping, ra, file, offset, |
| 2440 | ra->ra_pages); |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2441 | return fpin; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2442 | } |
| 2443 | |
Andi Kleen | 207d04b | 2011-05-24 17:12:29 -0700 | [diff] [blame] | 2444 | /* Avoid banging the cache line if not needed */ |
| 2445 | if (ra->mmap_miss < MMAP_LOTSAMISS * 10) |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2446 | ra->mmap_miss++; |
| 2447 | |
| 2448 | /* |
| 2449 | * Do we miss much more than hit in this file? If so, |
| 2450 | * stop bothering with read-ahead. It will only hurt. |
| 2451 | */ |
| 2452 | if (ra->mmap_miss > MMAP_LOTSAMISS) |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2453 | return fpin; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2454 | |
Wu Fengguang | d30a110 | 2009-06-16 15:31:30 -0700 | [diff] [blame] | 2455 | /* |
| 2456 | * mmap read-around |
| 2457 | */ |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2458 | fpin = maybe_unlock_mmap_for_io(vmf, fpin); |
Roman Gushchin | 600e19a | 2015-11-05 18:47:08 -0800 | [diff] [blame] | 2459 | ra->start = max_t(long, 0, offset - ra->ra_pages / 2); |
| 2460 | ra->size = ra->ra_pages; |
| 2461 | ra->async_size = ra->ra_pages / 4; |
Wu Fengguang | 275b12b | 2011-05-24 17:12:28 -0700 | [diff] [blame] | 2462 | ra_submit(ra, mapping, file); |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2463 | return fpin; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | /* |
| 2467 | * Asynchronous readahead happens when we find the page and PG_readahead, |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2468 | * so we want to possibly extend the readahead further. We return the file that |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2469 | * was pinned if we have to drop the mmap_lock in order to do IO. |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2470 | */ |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2471 | static struct file *do_async_mmap_readahead(struct vm_fault *vmf, |
| 2472 | struct page *page) |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2473 | { |
Josef Bacik | 2a1180f | 2019-03-13 11:44:18 -0700 | [diff] [blame] | 2474 | struct file *file = vmf->vma->vm_file; |
| 2475 | struct file_ra_state *ra = &file->f_ra; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2476 | struct address_space *mapping = file->f_mapping; |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2477 | struct file *fpin = NULL; |
Josef Bacik | 2a1180f | 2019-03-13 11:44:18 -0700 | [diff] [blame] | 2478 | pgoff_t offset = vmf->pgoff; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2479 | |
| 2480 | /* If we don't want any read-ahead, don't bother */ |
Jan Kara | 5c72fee | 2020-04-01 21:04:40 -0700 | [diff] [blame] | 2481 | if (vmf->vma->vm_flags & VM_RAND_READ || !ra->ra_pages) |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2482 | return fpin; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2483 | if (ra->mmap_miss > 0) |
| 2484 | ra->mmap_miss--; |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2485 | if (PageReadahead(page)) { |
| 2486 | fpin = maybe_unlock_mmap_for_io(vmf, fpin); |
Wu Fengguang | 2fad6f5 | 2009-06-16 15:31:29 -0700 | [diff] [blame] | 2487 | page_cache_async_readahead(mapping, ra, file, |
| 2488 | page, offset, ra->ra_pages); |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2489 | } |
| 2490 | return fpin; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2491 | } |
| 2492 | |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 2493 | /** |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 2494 | * filemap_fault - read in file data for page fault handling |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 2495 | * @vmf: struct vm_fault containing details of the fault |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 2496 | * |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 2497 | * filemap_fault() is invoked via the vma operations vector for a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | * mapped memory region to read in file data during a page fault. |
| 2499 | * |
| 2500 | * The goto's are kind of ugly, but this streamlines the normal case of having |
| 2501 | * it in the page cache, and handles the special cases reasonably without |
| 2502 | * having a lot of duplicated code. |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 2503 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2504 | * vma->vm_mm->mmap_lock must be held on entry. |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 2505 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2506 | * If our return value has VM_FAULT_RETRY set, it's because the mmap_lock |
Yang Shi | a498583 | 2019-07-11 20:55:29 -0700 | [diff] [blame] | 2507 | * may be dropped before doing I/O or by lock_page_maybe_drop_mmap(). |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 2508 | * |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2509 | * If our return value does not have VM_FAULT_RETRY set, the mmap_lock |
Paul Cassella | 9a95f3c | 2014-08-06 16:07:24 -0700 | [diff] [blame] | 2510 | * has not been released. |
| 2511 | * |
| 2512 | * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2513 | * |
| 2514 | * Return: bitwise-OR of %VM_FAULT_ codes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | */ |
Souptick Joarder | 2bcd645 | 2018-06-07 17:08:00 -0700 | [diff] [blame] | 2516 | vm_fault_t filemap_fault(struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | { |
| 2518 | int error; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 2519 | struct file *file = vmf->vma->vm_file; |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2520 | struct file *fpin = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | struct address_space *mapping = file->f_mapping; |
| 2522 | struct file_ra_state *ra = &file->f_ra; |
| 2523 | struct inode *inode = mapping->host; |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2524 | pgoff_t offset = vmf->pgoff; |
Matthew Wilcox | 9ab2594 | 2017-05-03 14:53:29 -0700 | [diff] [blame] | 2525 | pgoff_t max_off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | struct page *page; |
Souptick Joarder | 2bcd645 | 2018-06-07 17:08:00 -0700 | [diff] [blame] | 2527 | vm_fault_t ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | |
Matthew Wilcox | 9ab2594 | 2017-05-03 14:53:29 -0700 | [diff] [blame] | 2529 | max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); |
| 2530 | if (unlikely(offset >= max_off)) |
Linus Torvalds | 5307cc1 | 2007-10-31 09:19:46 -0700 | [diff] [blame] | 2531 | return VM_FAULT_SIGBUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | /* |
Johannes Weiner | 4942642 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 2534 | * Do we have something in the page cache already? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | */ |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2536 | page = find_get_page(mapping, offset); |
Shaohua Li | 45cac65 | 2012-10-08 16:32:19 -0700 | [diff] [blame] | 2537 | if (likely(page) && !(vmf->flags & FAULT_FLAG_TRIED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 | /* |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2539 | * We found the page, so try async readahead before |
| 2540 | * waiting for the lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | */ |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2542 | fpin = do_async_mmap_readahead(vmf, page); |
Shaohua Li | 45cac65 | 2012-10-08 16:32:19 -0700 | [diff] [blame] | 2543 | } else if (!page) { |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2544 | /* No page in the page cache at all */ |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2545 | count_vm_event(PGMAJFAULT); |
Roman Gushchin | 2262185 | 2017-07-06 15:40:25 -0700 | [diff] [blame] | 2546 | count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT); |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2547 | ret = VM_FAULT_MAJOR; |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2548 | fpin = do_sync_mmap_readahead(vmf); |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2549 | retry_find: |
Josef Bacik | a75d4c3 | 2019-03-13 11:44:14 -0700 | [diff] [blame] | 2550 | page = pagecache_get_page(mapping, offset, |
| 2551 | FGP_CREAT|FGP_FOR_MMAP, |
| 2552 | vmf->gfp_mask); |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2553 | if (!page) { |
| 2554 | if (fpin) |
| 2555 | goto out_retry; |
Matthew Wilcox (Oracle) | e520e93 | 2020-04-01 21:04:53 -0700 | [diff] [blame] | 2556 | return VM_FAULT_OOM; |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2557 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2558 | } |
| 2559 | |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2560 | if (!lock_page_maybe_drop_mmap(vmf, page, &fpin)) |
| 2561 | goto out_retry; |
Michel Lespinasse | b522c94 | 2010-10-26 14:21:56 -0700 | [diff] [blame] | 2562 | |
| 2563 | /* Did it get truncated? */ |
Song Liu | 585e5a7 | 2019-09-23 15:37:44 -0700 | [diff] [blame] | 2564 | if (unlikely(compound_head(page)->mapping != mapping)) { |
Michel Lespinasse | b522c94 | 2010-10-26 14:21:56 -0700 | [diff] [blame] | 2565 | unlock_page(page); |
| 2566 | put_page(page); |
| 2567 | goto retry_find; |
| 2568 | } |
Song Liu | 520e5ba | 2019-09-23 15:37:50 -0700 | [diff] [blame] | 2569 | VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page); |
Michel Lespinasse | b522c94 | 2010-10-26 14:21:56 -0700 | [diff] [blame] | 2570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | /* |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 2572 | * We have a locked page in the page cache, now we need to check |
| 2573 | * that it's up-to-date. If not, it is going to be due to an error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2574 | */ |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 2575 | if (unlikely(!PageUptodate(page))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2576 | goto page_not_uptodate; |
| 2577 | |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2578 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2579 | * We've made it this far and we had to drop our mmap_lock, now is the |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2580 | * time to return to the upper layer and have it re-find the vma and |
| 2581 | * redo the fault. |
| 2582 | */ |
| 2583 | if (fpin) { |
| 2584 | unlock_page(page); |
| 2585 | goto out_retry; |
| 2586 | } |
| 2587 | |
| 2588 | /* |
Linus Torvalds | ef00e08 | 2009-06-16 15:31:25 -0700 | [diff] [blame] | 2589 | * Found the page and have a reference on it. |
| 2590 | * We must recheck i_size under page lock. |
| 2591 | */ |
Matthew Wilcox | 9ab2594 | 2017-05-03 14:53:29 -0700 | [diff] [blame] | 2592 | max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); |
| 2593 | if (unlikely(offset >= max_off)) { |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 2594 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2595 | put_page(page); |
Linus Torvalds | 5307cc1 | 2007-10-31 09:19:46 -0700 | [diff] [blame] | 2596 | return VM_FAULT_SIGBUS; |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 2597 | } |
| 2598 | |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 2599 | vmf->page = page; |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 2600 | return ret | VM_FAULT_LOCKED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | page_not_uptodate: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | /* |
| 2604 | * Umm, take care of errors if the page isn't up-to-date. |
| 2605 | * Try to re-read it _once_. We do this synchronously, |
| 2606 | * because there really aren't any performance issues here |
| 2607 | * and we need to check for errors. |
| 2608 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | ClearPageError(page); |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2610 | fpin = maybe_unlock_mmap_for_io(vmf, fpin); |
Zach Brown | 994fc28c | 2005-12-15 14:28:17 -0800 | [diff] [blame] | 2611 | error = mapping->a_ops->readpage(file, page); |
Miklos Szeredi | 3ef0f72 | 2008-05-14 16:05:37 -0700 | [diff] [blame] | 2612 | if (!error) { |
| 2613 | wait_on_page_locked(page); |
| 2614 | if (!PageUptodate(page)) |
| 2615 | error = -EIO; |
| 2616 | } |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2617 | if (fpin) |
| 2618 | goto out_retry; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2619 | put_page(page); |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 2620 | |
| 2621 | if (!error || error == AOP_TRUNCATED_PAGE) |
| 2622 | goto retry_find; |
| 2623 | |
Souptick Joarder | 0f8e2db | 2020-04-01 21:04:50 -0700 | [diff] [blame] | 2624 | shrink_readahead_size_eio(ra); |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 2625 | return VM_FAULT_SIGBUS; |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2626 | |
| 2627 | out_retry: |
| 2628 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2629 | * We dropped the mmap_lock, we need to return to the fault handler to |
Josef Bacik | 6b4c9f4 | 2019-03-13 11:44:22 -0700 | [diff] [blame] | 2630 | * re-find the vma and come back and find our hopefully still populated |
| 2631 | * page. |
| 2632 | */ |
| 2633 | if (page) |
| 2634 | put_page(page); |
| 2635 | if (fpin) |
| 2636 | fput(fpin); |
| 2637 | return ret | VM_FAULT_RETRY; |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 2638 | } |
| 2639 | EXPORT_SYMBOL(filemap_fault); |
| 2640 | |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2641 | void filemap_map_pages(struct vm_fault *vmf, |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 2642 | pgoff_t start_pgoff, pgoff_t end_pgoff) |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2643 | { |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2644 | struct file *file = vmf->vma->vm_file; |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2645 | struct address_space *mapping = file->f_mapping; |
Kirill A. Shutemov | bae473a | 2016-07-26 15:25:20 -0700 | [diff] [blame] | 2646 | pgoff_t last_pgoff = start_pgoff; |
Matthew Wilcox | 9ab2594 | 2017-05-03 14:53:29 -0700 | [diff] [blame] | 2647 | unsigned long max_idx; |
Matthew Wilcox | 070e807 | 2018-05-17 00:08:30 -0400 | [diff] [blame] | 2648 | XA_STATE(xas, &mapping->i_pages, start_pgoff); |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 2649 | struct page *page; |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2650 | |
| 2651 | rcu_read_lock(); |
Matthew Wilcox | 070e807 | 2018-05-17 00:08:30 -0400 | [diff] [blame] | 2652 | xas_for_each(&xas, page, end_pgoff) { |
| 2653 | if (xas_retry(&xas, page)) |
| 2654 | continue; |
| 2655 | if (xa_is_value(page)) |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2656 | goto next; |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2657 | |
Michal Hocko | e0975b2 | 2018-12-28 00:38:36 -0800 | [diff] [blame] | 2658 | /* |
| 2659 | * Check for a locked page first, as a speculative |
| 2660 | * reference may adversely influence page migration. |
| 2661 | */ |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 2662 | if (PageLocked(page)) |
Michal Hocko | e0975b2 | 2018-12-28 00:38:36 -0800 | [diff] [blame] | 2663 | goto next; |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 2664 | if (!page_cache_get_speculative(page)) |
Matthew Wilcox | 070e807 | 2018-05-17 00:08:30 -0400 | [diff] [blame] | 2665 | goto next; |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2666 | |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 2667 | /* Has the page moved or been split? */ |
Matthew Wilcox | 070e807 | 2018-05-17 00:08:30 -0400 | [diff] [blame] | 2668 | if (unlikely(page != xas_reload(&xas))) |
| 2669 | goto skip; |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 2670 | page = find_subpage(page, xas.xa_index); |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2671 | |
| 2672 | if (!PageUptodate(page) || |
| 2673 | PageReadahead(page) || |
| 2674 | PageHWPoison(page)) |
| 2675 | goto skip; |
| 2676 | if (!trylock_page(page)) |
| 2677 | goto skip; |
| 2678 | |
| 2679 | if (page->mapping != mapping || !PageUptodate(page)) |
| 2680 | goto unlock; |
| 2681 | |
Matthew Wilcox | 9ab2594 | 2017-05-03 14:53:29 -0700 | [diff] [blame] | 2682 | max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE); |
| 2683 | if (page->index >= max_idx) |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2684 | goto unlock; |
| 2685 | |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2686 | if (file->f_ra.mmap_miss > 0) |
| 2687 | file->f_ra.mmap_miss--; |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 2688 | |
Matthew Wilcox | 070e807 | 2018-05-17 00:08:30 -0400 | [diff] [blame] | 2689 | vmf->address += (xas.xa_index - last_pgoff) << PAGE_SHIFT; |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2690 | if (vmf->pte) |
Matthew Wilcox | 070e807 | 2018-05-17 00:08:30 -0400 | [diff] [blame] | 2691 | vmf->pte += xas.xa_index - last_pgoff; |
| 2692 | last_pgoff = xas.xa_index; |
Johannes Weiner | 9d82c69 | 2020-06-03 16:02:04 -0700 | [diff] [blame] | 2693 | if (alloc_set_pte(vmf, page)) |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 2694 | goto unlock; |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2695 | unlock_page(page); |
| 2696 | goto next; |
| 2697 | unlock: |
| 2698 | unlock_page(page); |
| 2699 | skip: |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2700 | put_page(page); |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2701 | next: |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 2702 | /* Huge page is mapped? No need to proceed. */ |
Jan Kara | 82b0f8c | 2016-12-14 15:06:58 -0800 | [diff] [blame] | 2703 | if (pmd_trans_huge(*vmf->pmd)) |
Kirill A. Shutemov | 7267ec00 | 2016-07-26 15:25:23 -0700 | [diff] [blame] | 2704 | break; |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2705 | } |
| 2706 | rcu_read_unlock(); |
| 2707 | } |
| 2708 | EXPORT_SYMBOL(filemap_map_pages); |
| 2709 | |
Souptick Joarder | 2bcd645 | 2018-06-07 17:08:00 -0700 | [diff] [blame] | 2710 | vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf) |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2711 | { |
| 2712 | struct page *page = vmf->page; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 2713 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Souptick Joarder | 2bcd645 | 2018-06-07 17:08:00 -0700 | [diff] [blame] | 2714 | vm_fault_t ret = VM_FAULT_LOCKED; |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2715 | |
Jan Kara | 14da920 | 2012-06-12 16:20:37 +0200 | [diff] [blame] | 2716 | sb_start_pagefault(inode->i_sb); |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 2717 | file_update_time(vmf->vma->vm_file); |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2718 | lock_page(page); |
| 2719 | if (page->mapping != inode->i_mapping) { |
| 2720 | unlock_page(page); |
| 2721 | ret = VM_FAULT_NOPAGE; |
| 2722 | goto out; |
| 2723 | } |
Jan Kara | 14da920 | 2012-06-12 16:20:37 +0200 | [diff] [blame] | 2724 | /* |
| 2725 | * We mark the page dirty already here so that when freeze is in |
| 2726 | * progress, we are guaranteed that writeback during freezing will |
| 2727 | * see the dirty page and writeprotect it again. |
| 2728 | */ |
| 2729 | set_page_dirty(page); |
Darrick J. Wong | 1d1d1a7 | 2013-02-21 16:42:51 -0800 | [diff] [blame] | 2730 | wait_for_stable_page(page); |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2731 | out: |
Jan Kara | 14da920 | 2012-06-12 16:20:37 +0200 | [diff] [blame] | 2732 | sb_end_pagefault(inode->i_sb); |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2733 | return ret; |
| 2734 | } |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2735 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 2736 | const struct vm_operations_struct generic_file_vm_ops = { |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 2737 | .fault = filemap_fault, |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2738 | .map_pages = filemap_map_pages, |
Jan Kara | 4fcf1c6 | 2012-06-12 16:20:29 +0200 | [diff] [blame] | 2739 | .page_mkwrite = filemap_page_mkwrite, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | }; |
| 2741 | |
| 2742 | /* This is used for a general mmap of a disk file */ |
| 2743 | |
| 2744 | int generic_file_mmap(struct file * file, struct vm_area_struct * vma) |
| 2745 | { |
| 2746 | struct address_space *mapping = file->f_mapping; |
| 2747 | |
| 2748 | if (!mapping->a_ops->readpage) |
| 2749 | return -ENOEXEC; |
| 2750 | file_accessed(file); |
| 2751 | vma->vm_ops = &generic_file_vm_ops; |
| 2752 | return 0; |
| 2753 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | |
| 2755 | /* |
| 2756 | * This is for filesystems which do not implement ->writepage. |
| 2757 | */ |
| 2758 | int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma) |
| 2759 | { |
| 2760 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) |
| 2761 | return -EINVAL; |
| 2762 | return generic_file_mmap(file, vma); |
| 2763 | } |
| 2764 | #else |
Souptick Joarder | 4b96a37 | 2018-10-26 15:04:03 -0700 | [diff] [blame] | 2765 | vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf) |
Arnd Bergmann | 4539722 | 2018-04-13 15:35:27 -0700 | [diff] [blame] | 2766 | { |
Souptick Joarder | 4b96a37 | 2018-10-26 15:04:03 -0700 | [diff] [blame] | 2767 | return VM_FAULT_SIGBUS; |
Arnd Bergmann | 4539722 | 2018-04-13 15:35:27 -0700 | [diff] [blame] | 2768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | int generic_file_mmap(struct file * file, struct vm_area_struct * vma) |
| 2770 | { |
| 2771 | return -ENOSYS; |
| 2772 | } |
| 2773 | int generic_file_readonly_mmap(struct file * file, struct vm_area_struct * vma) |
| 2774 | { |
| 2775 | return -ENOSYS; |
| 2776 | } |
| 2777 | #endif /* CONFIG_MMU */ |
| 2778 | |
Arnd Bergmann | 4539722 | 2018-04-13 15:35:27 -0700 | [diff] [blame] | 2779 | EXPORT_SYMBOL(filemap_page_mkwrite); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2780 | EXPORT_SYMBOL(generic_file_mmap); |
| 2781 | EXPORT_SYMBOL(generic_file_readonly_mmap); |
| 2782 | |
Sasha Levin | 67f9fd9 | 2014-04-03 14:48:18 -0700 | [diff] [blame] | 2783 | static struct page *wait_on_page_read(struct page *page) |
| 2784 | { |
| 2785 | if (!IS_ERR(page)) { |
| 2786 | wait_on_page_locked(page); |
| 2787 | if (!PageUptodate(page)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2788 | put_page(page); |
Sasha Levin | 67f9fd9 | 2014-04-03 14:48:18 -0700 | [diff] [blame] | 2789 | page = ERR_PTR(-EIO); |
| 2790 | } |
| 2791 | } |
| 2792 | return page; |
| 2793 | } |
| 2794 | |
Mel Gorman | 32b6352 | 2016-03-15 14:55:36 -0700 | [diff] [blame] | 2795 | static struct page *do_read_cache_page(struct address_space *mapping, |
Fengguang Wu | 57f6b96 | 2007-10-16 01:24:37 -0700 | [diff] [blame] | 2796 | pgoff_t index, |
Hugh Dickins | 5e5358e | 2011-07-25 17:12:23 -0700 | [diff] [blame] | 2797 | int (*filler)(void *, struct page *), |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2798 | void *data, |
| 2799 | gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | { |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2801 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | int err; |
| 2803 | repeat: |
| 2804 | page = find_get_page(mapping, index); |
| 2805 | if (!page) { |
Mel Gorman | 453f85d | 2017-11-15 17:38:03 -0800 | [diff] [blame] | 2806 | page = __page_cache_alloc(gfp); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2807 | if (!page) |
| 2808 | return ERR_PTR(-ENOMEM); |
Dave Kleikamp | e6f67b8 | 2011-12-21 11:05:48 -0600 | [diff] [blame] | 2809 | err = add_to_page_cache_lru(page, mapping, index, gfp); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2810 | if (unlikely(err)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2811 | put_page(page); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 2812 | if (err == -EEXIST) |
| 2813 | goto repeat; |
Matthew Wilcox | 22ecdb4 | 2017-12-04 04:02:00 -0500 | [diff] [blame] | 2814 | /* Presumably ENOMEM for xarray node */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2815 | return ERR_PTR(err); |
| 2816 | } |
Mel Gorman | 32b6352 | 2016-03-15 14:55:36 -0700 | [diff] [blame] | 2817 | |
| 2818 | filler: |
Christoph Hellwig | 6c45b45 | 2019-07-11 20:55:20 -0700 | [diff] [blame] | 2819 | if (filler) |
| 2820 | err = filler(data, page); |
| 2821 | else |
| 2822 | err = mapping->a_ops->readpage(data, page); |
| 2823 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | if (err < 0) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2825 | put_page(page); |
Mel Gorman | 32b6352 | 2016-03-15 14:55:36 -0700 | [diff] [blame] | 2826 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2827 | } |
Mel Gorman | 32b6352 | 2016-03-15 14:55:36 -0700 | [diff] [blame] | 2828 | |
| 2829 | page = wait_on_page_read(page); |
| 2830 | if (IS_ERR(page)) |
| 2831 | return page; |
| 2832 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2834 | if (PageUptodate(page)) |
| 2835 | goto out; |
| 2836 | |
Mel Gorman | ebded02 | 2016-03-15 14:55:39 -0700 | [diff] [blame] | 2837 | /* |
| 2838 | * Page is not up to date and may be locked due one of the following |
| 2839 | * case a: Page is being filled and the page lock is held |
| 2840 | * case b: Read/write error clearing the page uptodate status |
| 2841 | * case c: Truncation in progress (page locked) |
| 2842 | * case d: Reclaim in progress |
| 2843 | * |
| 2844 | * Case a, the page will be up to date when the page is unlocked. |
| 2845 | * There is no need to serialise on the page lock here as the page |
| 2846 | * is pinned so the lock gives no additional protection. Even if the |
| 2847 | * the page is truncated, the data is still valid if PageUptodate as |
| 2848 | * it's a race vs truncate race. |
| 2849 | * Case b, the page will not be up to date |
| 2850 | * Case c, the page may be truncated but in itself, the data may still |
| 2851 | * be valid after IO completes as it's a read vs truncate race. The |
| 2852 | * operation must restart if the page is not uptodate on unlock but |
| 2853 | * otherwise serialising on page lock to stabilise the mapping gives |
| 2854 | * no additional guarantees to the caller as the page lock is |
| 2855 | * released before return. |
| 2856 | * Case d, similar to truncation. If reclaim holds the page lock, it |
| 2857 | * will be a race with remove_mapping that determines if the mapping |
| 2858 | * is valid on unlock but otherwise the data is valid and there is |
| 2859 | * no need to serialise with page lock. |
| 2860 | * |
| 2861 | * As the page lock gives no additional guarantee, we optimistically |
| 2862 | * wait on the page to be unlocked and check if it's up to date and |
| 2863 | * use the page if it is. Otherwise, the page lock is required to |
| 2864 | * distinguish between the different cases. The motivation is that we |
| 2865 | * avoid spurious serialisations and wakeups when multiple processes |
| 2866 | * wait on the same page for IO to complete. |
| 2867 | */ |
| 2868 | wait_on_page_locked(page); |
| 2869 | if (PageUptodate(page)) |
| 2870 | goto out; |
| 2871 | |
| 2872 | /* Distinguish between all the cases under the safety of the lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | lock_page(page); |
Mel Gorman | ebded02 | 2016-03-15 14:55:39 -0700 | [diff] [blame] | 2874 | |
| 2875 | /* Case c or d, restart the operation */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2876 | if (!page->mapping) { |
| 2877 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2878 | put_page(page); |
Mel Gorman | 32b6352 | 2016-03-15 14:55:36 -0700 | [diff] [blame] | 2879 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2880 | } |
Mel Gorman | ebded02 | 2016-03-15 14:55:39 -0700 | [diff] [blame] | 2881 | |
| 2882 | /* Someone else locked and filled the page in a very small window */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | if (PageUptodate(page)) { |
| 2884 | unlock_page(page); |
| 2885 | goto out; |
| 2886 | } |
Xianting Tian | faffdfa | 2020-04-01 21:04:47 -0700 | [diff] [blame] | 2887 | |
| 2888 | /* |
| 2889 | * A previous I/O error may have been due to temporary |
| 2890 | * failures. |
| 2891 | * Clear page error before actual read, PG_error will be |
| 2892 | * set again if read page fails. |
| 2893 | */ |
| 2894 | ClearPageError(page); |
Mel Gorman | 32b6352 | 2016-03-15 14:55:36 -0700 | [diff] [blame] | 2895 | goto filler; |
| 2896 | |
David Howells | c855ff3 | 2007-05-09 13:42:20 +0100 | [diff] [blame] | 2897 | out: |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 2898 | mark_page_accessed(page); |
| 2899 | return page; |
| 2900 | } |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2901 | |
| 2902 | /** |
Sasha Levin | 67f9fd9 | 2014-04-03 14:48:18 -0700 | [diff] [blame] | 2903 | * read_cache_page - read into page cache, fill it if needed |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2904 | * @mapping: the page's address_space |
| 2905 | * @index: the page index |
| 2906 | * @filler: function to perform the read |
Hugh Dickins | 5e5358e | 2011-07-25 17:12:23 -0700 | [diff] [blame] | 2907 | * @data: first arg to filler(data, page) function, often left as NULL |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2908 | * |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2909 | * Read into the page cache. If a page already exists, and PageUptodate() is |
Sasha Levin | 67f9fd9 | 2014-04-03 14:48:18 -0700 | [diff] [blame] | 2910 | * not set, try to fill the page and wait for it to become unlocked. |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2911 | * |
| 2912 | * If the page does not get brought uptodate, return -EIO. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2913 | * |
| 2914 | * Return: up to date page on success, ERR_PTR() on failure. |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2915 | */ |
Sasha Levin | 67f9fd9 | 2014-04-03 14:48:18 -0700 | [diff] [blame] | 2916 | struct page *read_cache_page(struct address_space *mapping, |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2917 | pgoff_t index, |
Hugh Dickins | 5e5358e | 2011-07-25 17:12:23 -0700 | [diff] [blame] | 2918 | int (*filler)(void *, struct page *), |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2919 | void *data) |
| 2920 | { |
Christoph Hellwig | d322a8e | 2019-07-11 20:55:17 -0700 | [diff] [blame] | 2921 | return do_read_cache_page(mapping, index, filler, data, |
| 2922 | mapping_gfp_mask(mapping)); |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2923 | } |
Sasha Levin | 67f9fd9 | 2014-04-03 14:48:18 -0700 | [diff] [blame] | 2924 | EXPORT_SYMBOL(read_cache_page); |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2925 | |
| 2926 | /** |
| 2927 | * read_cache_page_gfp - read into page cache, using specified page allocation flags. |
| 2928 | * @mapping: the page's address_space |
| 2929 | * @index: the page index |
| 2930 | * @gfp: the page allocator flags to use if allocating |
| 2931 | * |
| 2932 | * This is the same as "read_mapping_page(mapping, index, NULL)", but with |
Dave Kleikamp | e6f67b8 | 2011-12-21 11:05:48 -0600 | [diff] [blame] | 2933 | * any new page allocations done using the specified allocation flags. |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2934 | * |
| 2935 | * If the page does not get brought uptodate, return -EIO. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 2936 | * |
| 2937 | * Return: up to date page on success, ERR_PTR() on failure. |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2938 | */ |
| 2939 | struct page *read_cache_page_gfp(struct address_space *mapping, |
| 2940 | pgoff_t index, |
| 2941 | gfp_t gfp) |
| 2942 | { |
Christoph Hellwig | 6c45b45 | 2019-07-11 20:55:20 -0700 | [diff] [blame] | 2943 | return do_read_cache_page(mapping, index, NULL, NULL, gfp); |
Linus Torvalds | 0531b2a | 2010-01-27 09:20:03 -0800 | [diff] [blame] | 2944 | } |
| 2945 | EXPORT_SYMBOL(read_cache_page_gfp); |
| 2946 | |
Nick Piggin | 2f718ff | 2007-10-16 01:24:59 -0700 | [diff] [blame] | 2947 | /* |
Darrick J. Wong | 9fd91a90cb | 2018-10-30 10:40:46 +1100 | [diff] [blame] | 2948 | * Don't operate on ranges the page cache doesn't support, and don't exceed the |
| 2949 | * LFS limits. If pos is under the limit it becomes a short access. If it |
| 2950 | * exceeds the limit we return -EFBIG. |
| 2951 | */ |
Darrick J. Wong | 9fd91a90cb | 2018-10-30 10:40:46 +1100 | [diff] [blame] | 2952 | static int generic_write_check_limits(struct file *file, loff_t pos, |
| 2953 | loff_t *count) |
| 2954 | { |
Amir Goldstein | 646955c | 2019-06-05 08:04:48 -0700 | [diff] [blame] | 2955 | struct inode *inode = file->f_mapping->host; |
| 2956 | loff_t max_size = inode->i_sb->s_maxbytes; |
Darrick J. Wong | 9fd91a90cb | 2018-10-30 10:40:46 +1100 | [diff] [blame] | 2957 | loff_t limit = rlimit(RLIMIT_FSIZE); |
| 2958 | |
| 2959 | if (limit != RLIM_INFINITY) { |
| 2960 | if (pos >= limit) { |
| 2961 | send_sig(SIGXFSZ, current, 0); |
| 2962 | return -EFBIG; |
| 2963 | } |
| 2964 | *count = min(*count, limit - pos); |
| 2965 | } |
| 2966 | |
Amir Goldstein | 646955c | 2019-06-05 08:04:48 -0700 | [diff] [blame] | 2967 | if (!(file->f_flags & O_LARGEFILE)) |
| 2968 | max_size = MAX_NON_LFS; |
| 2969 | |
| 2970 | if (unlikely(pos >= max_size)) |
| 2971 | return -EFBIG; |
| 2972 | |
| 2973 | *count = min(*count, max_size - pos); |
| 2974 | |
| 2975 | return 0; |
Darrick J. Wong | 9fd91a90cb | 2018-10-30 10:40:46 +1100 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2979 | * Performs necessary checks before doing a write |
| 2980 | * |
Randy Dunlap | 485bb99 | 2006-06-23 02:03:49 -0700 | [diff] [blame] | 2981 | * Can adjust writing position or amount of bytes to write. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2982 | * Returns appropriate error code that caller should return or |
| 2983 | * zero in case that write should be allowed. |
| 2984 | */ |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 2985 | inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 | { |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 2987 | struct file *file = iocb->ki_filp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | struct inode *inode = file->f_mapping->host; |
Darrick J. Wong | 9fd91a90cb | 2018-10-30 10:40:46 +1100 | [diff] [blame] | 2989 | loff_t count; |
| 2990 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2991 | |
Darrick J. Wong | dc617f2 | 2019-08-20 07:55:16 -0700 | [diff] [blame] | 2992 | if (IS_SWAPFILE(inode)) |
| 2993 | return -ETXTBSY; |
| 2994 | |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 2995 | if (!iov_iter_count(from)) |
| 2996 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2997 | |
Al Viro | 0fa6b00 | 2015-04-04 04:05:48 -0400 | [diff] [blame] | 2998 | /* FIXME: this is for backwards compatibility with 2.4 */ |
Al Viro | 2ba48ce | 2015-04-09 13:52:01 -0400 | [diff] [blame] | 2999 | if (iocb->ki_flags & IOCB_APPEND) |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 3000 | iocb->ki_pos = i_size_read(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | |
Goldwyn Rodrigues | 6be96d3 | 2017-06-20 07:05:44 -0500 | [diff] [blame] | 3002 | if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT)) |
| 3003 | return -EINVAL; |
| 3004 | |
Darrick J. Wong | 9fd91a90cb | 2018-10-30 10:40:46 +1100 | [diff] [blame] | 3005 | count = iov_iter_count(from); |
| 3006 | ret = generic_write_check_limits(file, iocb->ki_pos, &count); |
| 3007 | if (ret) |
| 3008 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3009 | |
Darrick J. Wong | 9fd91a90cb | 2018-10-30 10:40:46 +1100 | [diff] [blame] | 3010 | iov_iter_truncate(from, count); |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 3011 | return iov_iter_count(from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3012 | } |
| 3013 | EXPORT_SYMBOL(generic_write_checks); |
| 3014 | |
Darrick J. Wong | 1383a7e | 2018-10-30 10:40:31 +1100 | [diff] [blame] | 3015 | /* |
| 3016 | * Performs necessary checks before doing a clone. |
| 3017 | * |
Amir Goldstein | 646955c | 2019-06-05 08:04:48 -0700 | [diff] [blame] | 3018 | * Can adjust amount of bytes to clone via @req_count argument. |
Darrick J. Wong | 1383a7e | 2018-10-30 10:40:31 +1100 | [diff] [blame] | 3019 | * Returns appropriate error code that caller should return or |
| 3020 | * zero in case the clone should be allowed. |
| 3021 | */ |
| 3022 | int generic_remap_checks(struct file *file_in, loff_t pos_in, |
| 3023 | struct file *file_out, loff_t pos_out, |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 3024 | loff_t *req_count, unsigned int remap_flags) |
Darrick J. Wong | 1383a7e | 2018-10-30 10:40:31 +1100 | [diff] [blame] | 3025 | { |
| 3026 | struct inode *inode_in = file_in->f_mapping->host; |
| 3027 | struct inode *inode_out = file_out->f_mapping->host; |
| 3028 | uint64_t count = *req_count; |
| 3029 | uint64_t bcount; |
| 3030 | loff_t size_in, size_out; |
| 3031 | loff_t bs = inode_out->i_sb->s_blocksize; |
Darrick J. Wong | 9fd91a90cb | 2018-10-30 10:40:46 +1100 | [diff] [blame] | 3032 | int ret; |
Darrick J. Wong | 1383a7e | 2018-10-30 10:40:31 +1100 | [diff] [blame] | 3033 | |
| 3034 | /* The start of both ranges must be aligned to an fs block. */ |
| 3035 | if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_out, bs)) |
| 3036 | return -EINVAL; |
| 3037 | |
| 3038 | /* Ensure offsets don't wrap. */ |
| 3039 | if (pos_in + count < pos_in || pos_out + count < pos_out) |
| 3040 | return -EINVAL; |
| 3041 | |
| 3042 | size_in = i_size_read(inode_in); |
| 3043 | size_out = i_size_read(inode_out); |
| 3044 | |
| 3045 | /* Dedupe requires both ranges to be within EOF. */ |
Darrick J. Wong | 3d28193 | 2018-10-30 10:41:34 +1100 | [diff] [blame] | 3046 | if ((remap_flags & REMAP_FILE_DEDUP) && |
Darrick J. Wong | 1383a7e | 2018-10-30 10:40:31 +1100 | [diff] [blame] | 3047 | (pos_in >= size_in || pos_in + count > size_in || |
| 3048 | pos_out >= size_out || pos_out + count > size_out)) |
| 3049 | return -EINVAL; |
| 3050 | |
| 3051 | /* Ensure the infile range is within the infile. */ |
| 3052 | if (pos_in >= size_in) |
| 3053 | return -EINVAL; |
| 3054 | count = min(count, size_in - (uint64_t)pos_in); |
| 3055 | |
Darrick J. Wong | 9fd91a90cb | 2018-10-30 10:40:46 +1100 | [diff] [blame] | 3056 | ret = generic_write_check_limits(file_out, pos_out, &count); |
| 3057 | if (ret) |
| 3058 | return ret; |
| 3059 | |
Darrick J. Wong | 1383a7e | 2018-10-30 10:40:31 +1100 | [diff] [blame] | 3060 | /* |
| 3061 | * If the user wanted us to link to the infile's EOF, round up to the |
| 3062 | * next block boundary for this check. |
| 3063 | * |
| 3064 | * Otherwise, make sure the count is also block-aligned, having |
| 3065 | * already confirmed the starting offsets' block alignment. |
| 3066 | */ |
| 3067 | if (pos_in + count == size_in) { |
| 3068 | bcount = ALIGN(size_in, bs) - pos_in; |
| 3069 | } else { |
| 3070 | if (!IS_ALIGNED(count, bs)) |
Darrick J. Wong | eca3654 | 2018-10-30 10:42:10 +1100 | [diff] [blame] | 3071 | count = ALIGN_DOWN(count, bs); |
Darrick J. Wong | 1383a7e | 2018-10-30 10:40:31 +1100 | [diff] [blame] | 3072 | bcount = count; |
| 3073 | } |
| 3074 | |
| 3075 | /* Don't allow overlapped cloning within the same file. */ |
| 3076 | if (inode_in == inode_out && |
| 3077 | pos_out + bcount > pos_in && |
| 3078 | pos_out < pos_in + bcount) |
| 3079 | return -EINVAL; |
| 3080 | |
Darrick J. Wong | eca3654 | 2018-10-30 10:42:10 +1100 | [diff] [blame] | 3081 | /* |
| 3082 | * We shortened the request but the caller can't deal with that, so |
| 3083 | * bounce the request back to userspace. |
| 3084 | */ |
| 3085 | if (*req_count != count && !(remap_flags & REMAP_FILE_CAN_SHORTEN)) |
Darrick J. Wong | 1383a7e | 2018-10-30 10:40:31 +1100 | [diff] [blame] | 3086 | return -EINVAL; |
| 3087 | |
Darrick J. Wong | eca3654 | 2018-10-30 10:42:10 +1100 | [diff] [blame] | 3088 | *req_count = count; |
Darrick J. Wong | 1383a7e | 2018-10-30 10:40:31 +1100 | [diff] [blame] | 3089 | return 0; |
| 3090 | } |
| 3091 | |
Amir Goldstein | a317135 | 2019-06-05 08:04:48 -0700 | [diff] [blame] | 3092 | |
| 3093 | /* |
| 3094 | * Performs common checks before doing a file copy/clone |
| 3095 | * from @file_in to @file_out. |
| 3096 | */ |
| 3097 | int generic_file_rw_checks(struct file *file_in, struct file *file_out) |
| 3098 | { |
| 3099 | struct inode *inode_in = file_inode(file_in); |
| 3100 | struct inode *inode_out = file_inode(file_out); |
| 3101 | |
| 3102 | /* Don't copy dirs, pipes, sockets... */ |
| 3103 | if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode)) |
| 3104 | return -EISDIR; |
| 3105 | if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode)) |
| 3106 | return -EINVAL; |
| 3107 | |
| 3108 | if (!(file_in->f_mode & FMODE_READ) || |
| 3109 | !(file_out->f_mode & FMODE_WRITE) || |
| 3110 | (file_out->f_flags & O_APPEND)) |
| 3111 | return -EBADF; |
| 3112 | |
| 3113 | return 0; |
| 3114 | } |
| 3115 | |
Amir Goldstein | 96e6e8f | 2019-06-05 08:04:49 -0700 | [diff] [blame] | 3116 | /* |
| 3117 | * Performs necessary checks before doing a file copy |
| 3118 | * |
| 3119 | * Can adjust amount of bytes to copy via @req_count argument. |
| 3120 | * Returns appropriate error code that caller should return or |
| 3121 | * zero in case the copy should be allowed. |
| 3122 | */ |
| 3123 | int generic_copy_file_checks(struct file *file_in, loff_t pos_in, |
| 3124 | struct file *file_out, loff_t pos_out, |
| 3125 | size_t *req_count, unsigned int flags) |
| 3126 | { |
| 3127 | struct inode *inode_in = file_inode(file_in); |
| 3128 | struct inode *inode_out = file_inode(file_out); |
| 3129 | uint64_t count = *req_count; |
| 3130 | loff_t size_in; |
| 3131 | int ret; |
| 3132 | |
| 3133 | ret = generic_file_rw_checks(file_in, file_out); |
| 3134 | if (ret) |
| 3135 | return ret; |
| 3136 | |
| 3137 | /* Don't touch certain kinds of inodes */ |
| 3138 | if (IS_IMMUTABLE(inode_out)) |
| 3139 | return -EPERM; |
| 3140 | |
| 3141 | if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out)) |
| 3142 | return -ETXTBSY; |
| 3143 | |
| 3144 | /* Ensure offsets don't wrap. */ |
| 3145 | if (pos_in + count < pos_in || pos_out + count < pos_out) |
| 3146 | return -EOVERFLOW; |
| 3147 | |
| 3148 | /* Shorten the copy to EOF */ |
| 3149 | size_in = i_size_read(inode_in); |
| 3150 | if (pos_in >= size_in) |
| 3151 | count = 0; |
| 3152 | else |
| 3153 | count = min(count, size_in - (uint64_t)pos_in); |
| 3154 | |
| 3155 | ret = generic_write_check_limits(file_out, pos_out, &count); |
| 3156 | if (ret) |
| 3157 | return ret; |
| 3158 | |
| 3159 | /* Don't allow overlapped copying within the same file. */ |
| 3160 | if (inode_in == inode_out && |
| 3161 | pos_out + count > pos_in && |
| 3162 | pos_out < pos_in + count) |
| 3163 | return -EINVAL; |
| 3164 | |
| 3165 | *req_count = count; |
| 3166 | return 0; |
| 3167 | } |
| 3168 | |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3169 | int pagecache_write_begin(struct file *file, struct address_space *mapping, |
| 3170 | loff_t pos, unsigned len, unsigned flags, |
| 3171 | struct page **pagep, void **fsdata) |
| 3172 | { |
| 3173 | const struct address_space_operations *aops = mapping->a_ops; |
| 3174 | |
Nick Piggin | 4e02ed4 | 2008-10-29 14:00:55 -0700 | [diff] [blame] | 3175 | return aops->write_begin(file, mapping, pos, len, flags, |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3176 | pagep, fsdata); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3177 | } |
| 3178 | EXPORT_SYMBOL(pagecache_write_begin); |
| 3179 | |
| 3180 | int pagecache_write_end(struct file *file, struct address_space *mapping, |
| 3181 | loff_t pos, unsigned len, unsigned copied, |
| 3182 | struct page *page, void *fsdata) |
| 3183 | { |
| 3184 | const struct address_space_operations *aops = mapping->a_ops; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3185 | |
Nick Piggin | 4e02ed4 | 2008-10-29 14:00:55 -0700 | [diff] [blame] | 3186 | return aops->write_end(file, mapping, pos, len, copied, page, fsdata); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3187 | } |
| 3188 | EXPORT_SYMBOL(pagecache_write_end); |
| 3189 | |
Konstantin Khlebnikov | a92853b | 2019-11-30 17:49:44 -0800 | [diff] [blame] | 3190 | /* |
| 3191 | * Warn about a page cache invalidation failure during a direct I/O write. |
| 3192 | */ |
| 3193 | void dio_warn_stale_pagecache(struct file *filp) |
| 3194 | { |
| 3195 | static DEFINE_RATELIMIT_STATE(_rs, 86400 * HZ, DEFAULT_RATELIMIT_BURST); |
| 3196 | char pathname[128]; |
| 3197 | struct inode *inode = file_inode(filp); |
| 3198 | char *path; |
| 3199 | |
| 3200 | errseq_set(&inode->i_mapping->wb_err, -EIO); |
| 3201 | if (__ratelimit(&_rs)) { |
| 3202 | path = file_path(filp, pathname, sizeof(pathname)); |
| 3203 | if (IS_ERR(path)) |
| 3204 | path = "(unknown)"; |
| 3205 | pr_crit("Page cache invalidation failure on direct I/O. Possible data corruption due to collision with buffered I/O!\n"); |
| 3206 | pr_crit("File: %s PID: %d Comm: %.20s\n", path, current->pid, |
| 3207 | current->comm); |
| 3208 | } |
| 3209 | } |
| 3210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3211 | ssize_t |
Christoph Hellwig | 1af5bb4 | 2016-04-07 08:51:56 -0700 | [diff] [blame] | 3212 | generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3213 | { |
| 3214 | struct file *file = iocb->ki_filp; |
| 3215 | struct address_space *mapping = file->f_mapping; |
| 3216 | struct inode *inode = mapping->host; |
Christoph Hellwig | 1af5bb4 | 2016-04-07 08:51:56 -0700 | [diff] [blame] | 3217 | loff_t pos = iocb->ki_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3218 | ssize_t written; |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3219 | size_t write_len; |
| 3220 | pgoff_t end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | |
Al Viro | 0c94933 | 2014-03-22 06:51:37 -0400 | [diff] [blame] | 3222 | write_len = iov_iter_count(from); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3223 | end = (pos + write_len - 1) >> PAGE_SHIFT; |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3224 | |
Goldwyn Rodrigues | 6be96d3 | 2017-06-20 07:05:44 -0500 | [diff] [blame] | 3225 | if (iocb->ki_flags & IOCB_NOWAIT) { |
| 3226 | /* If there are pages to writeback, return */ |
| 3227 | if (filemap_range_has_page(inode->i_mapping, pos, |
zhengbin | 35f12f0 | 2019-03-05 15:44:21 -0800 | [diff] [blame] | 3228 | pos + write_len - 1)) |
Goldwyn Rodrigues | 6be96d3 | 2017-06-20 07:05:44 -0500 | [diff] [blame] | 3229 | return -EAGAIN; |
| 3230 | } else { |
| 3231 | written = filemap_write_and_wait_range(mapping, pos, |
| 3232 | pos + write_len - 1); |
| 3233 | if (written) |
| 3234 | goto out; |
| 3235 | } |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3236 | |
| 3237 | /* |
| 3238 | * After a write we want buffered reads to be sure to go to disk to get |
| 3239 | * the new data. We invalidate clean cached page from the region we're |
| 3240 | * about to write. We do this *before* the write so that we can return |
Hisashi Hifumi | 6ccfa80 | 2008-09-02 14:35:40 -0700 | [diff] [blame] | 3241 | * without clobbering -EIOCBQUEUED from ->direct_IO(). |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3242 | */ |
Andrey Ryabinin | 55635ba | 2017-05-03 14:55:59 -0700 | [diff] [blame] | 3243 | written = invalidate_inode_pages2_range(mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3244 | pos >> PAGE_SHIFT, end); |
Andrey Ryabinin | 55635ba | 2017-05-03 14:55:59 -0700 | [diff] [blame] | 3245 | /* |
| 3246 | * If a page can not be invalidated, return 0 to fall back |
| 3247 | * to buffered write. |
| 3248 | */ |
| 3249 | if (written) { |
| 3250 | if (written == -EBUSY) |
| 3251 | return 0; |
| 3252 | goto out; |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3253 | } |
| 3254 | |
Al Viro | 639a93a5 | 2017-04-13 14:10:15 -0400 | [diff] [blame] | 3255 | written = mapping->a_ops->direct_IO(iocb, from); |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3256 | |
| 3257 | /* |
| 3258 | * Finally, try again to invalidate clean pages which might have been |
| 3259 | * cached by non-direct readahead, or faulted in by get_user_pages() |
| 3260 | * if the source of the write was an mmap'ed region of the file |
| 3261 | * we're writing. Either one is a pretty crazy thing to do, |
| 3262 | * so we don't support it 100%. If this invalidation |
| 3263 | * fails, tough, the write still worked... |
Lukas Czerner | 332391a | 2017-09-21 08:16:29 -0600 | [diff] [blame] | 3264 | * |
| 3265 | * Most of the time we do not need this since dio_complete() will do |
| 3266 | * the invalidation for us. However there are some file systems that |
| 3267 | * do not end up with dio_complete() being called, so let's not break |
Konstantin Khlebnikov | 80c1fe9 | 2019-11-30 17:49:41 -0800 | [diff] [blame] | 3268 | * them by removing it completely. |
| 3269 | * |
Konstantin Khlebnikov | 9266a14 | 2019-11-30 17:49:47 -0800 | [diff] [blame] | 3270 | * Noticeable example is a blkdev_direct_IO(). |
| 3271 | * |
Konstantin Khlebnikov | 80c1fe9 | 2019-11-30 17:49:41 -0800 | [diff] [blame] | 3272 | * Skip invalidation for async writes or if mapping has no pages. |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3273 | */ |
Konstantin Khlebnikov | 9266a14 | 2019-11-30 17:49:47 -0800 | [diff] [blame] | 3274 | if (written > 0 && mapping->nrpages && |
| 3275 | invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT, end)) |
| 3276 | dio_warn_stale_pagecache(file); |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | if (written > 0) { |
Namhyung Kim | 0116651 | 2010-10-26 14:21:58 -0700 | [diff] [blame] | 3279 | pos += written; |
Al Viro | 639a93a5 | 2017-04-13 14:10:15 -0400 | [diff] [blame] | 3280 | write_len -= written; |
Namhyung Kim | 0116651 | 2010-10-26 14:21:58 -0700 | [diff] [blame] | 3281 | if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) { |
| 3282 | i_size_write(inode, pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | mark_inode_dirty(inode); |
| 3284 | } |
Al Viro | 5cb6c6c | 2014-02-11 20:58:20 -0500 | [diff] [blame] | 3285 | iocb->ki_pos = pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3286 | } |
Al Viro | 639a93a5 | 2017-04-13 14:10:15 -0400 | [diff] [blame] | 3287 | iov_iter_revert(from, write_len - iov_iter_count(from)); |
Christoph Hellwig | a969e90 | 2008-07-23 21:27:04 -0700 | [diff] [blame] | 3288 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3289 | return written; |
| 3290 | } |
| 3291 | EXPORT_SYMBOL(generic_file_direct_write); |
| 3292 | |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 3293 | /* |
| 3294 | * Find or create a page at the given pagecache position. Return the locked |
| 3295 | * page. This function is specifically for buffered writes. |
| 3296 | */ |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 3297 | struct page *grab_cache_page_write_begin(struct address_space *mapping, |
| 3298 | pgoff_t index, unsigned flags) |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 3299 | { |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 3300 | struct page *page; |
Johannes Weiner | bbddabe | 2016-05-20 16:56:28 -0700 | [diff] [blame] | 3301 | int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT; |
Johannes Weiner | 0faa70c | 2012-01-10 15:07:53 -0800 | [diff] [blame] | 3302 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 3303 | if (flags & AOP_FLAG_NOFS) |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 3304 | fgp_flags |= FGP_NOFS; |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 3305 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 3306 | page = pagecache_get_page(mapping, index, fgp_flags, |
Michal Hocko | 45f87de | 2014-12-29 20:30:35 +0100 | [diff] [blame] | 3307 | mapping_gfp_mask(mapping)); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 3308 | if (page) |
| 3309 | wait_for_stable_page(page); |
| 3310 | |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 3311 | return page; |
| 3312 | } |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 3313 | EXPORT_SYMBOL(grab_cache_page_write_begin); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 3314 | |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3315 | ssize_t generic_perform_write(struct file *file, |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3316 | struct iov_iter *i, loff_t pos) |
| 3317 | { |
| 3318 | struct address_space *mapping = file->f_mapping; |
| 3319 | const struct address_space_operations *a_ops = mapping->a_ops; |
| 3320 | long status = 0; |
| 3321 | ssize_t written = 0; |
Nick Piggin | 674b892 | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 3322 | unsigned int flags = 0; |
| 3323 | |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3324 | do { |
| 3325 | struct page *page; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3326 | unsigned long offset; /* Offset into pagecache page */ |
| 3327 | unsigned long bytes; /* Bytes to write to page */ |
| 3328 | size_t copied; /* Bytes copied from user */ |
| 3329 | void *fsdata; |
| 3330 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3331 | offset = (pos & (PAGE_SIZE - 1)); |
| 3332 | bytes = min_t(unsigned long, PAGE_SIZE - offset, |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3333 | iov_iter_count(i)); |
| 3334 | |
| 3335 | again: |
Linus Torvalds | 00a3d66 | 2015-10-07 08:32:38 +0100 | [diff] [blame] | 3336 | /* |
| 3337 | * Bring in the user page that we will copy from _first_. |
| 3338 | * Otherwise there's a nasty deadlock on copying from the |
| 3339 | * same page as we're writing to, without it being marked |
| 3340 | * up-to-date. |
| 3341 | * |
| 3342 | * Not only is this an optimisation, but it is also required |
| 3343 | * to check that the address is actually valid, when atomic |
| 3344 | * usercopies are used, below. |
| 3345 | */ |
| 3346 | if (unlikely(iov_iter_fault_in_readable(i, bytes))) { |
| 3347 | status = -EFAULT; |
| 3348 | break; |
| 3349 | } |
| 3350 | |
Jan Kara | 296291c | 2015-10-22 13:32:21 -0700 | [diff] [blame] | 3351 | if (fatal_signal_pending(current)) { |
| 3352 | status = -EINTR; |
| 3353 | break; |
| 3354 | } |
| 3355 | |
Nick Piggin | 674b892 | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 3356 | status = a_ops->write_begin(file, mapping, pos, bytes, flags, |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3357 | &page, &fsdata); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 3358 | if (unlikely(status < 0)) |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3359 | break; |
| 3360 | |
anfei zhou | 931e80e | 2010-02-02 13:44:02 -0800 | [diff] [blame] | 3361 | if (mapping_writably_mapped(mapping)) |
| 3362 | flush_dcache_page(page); |
Linus Torvalds | 00a3d66 | 2015-10-07 08:32:38 +0100 | [diff] [blame] | 3363 | |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3364 | copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3365 | flush_dcache_page(page); |
| 3366 | |
| 3367 | status = a_ops->write_end(file, mapping, pos, bytes, copied, |
| 3368 | page, fsdata); |
| 3369 | if (unlikely(status < 0)) |
| 3370 | break; |
| 3371 | copied = status; |
| 3372 | |
| 3373 | cond_resched(); |
| 3374 | |
Nick Piggin | 124d3b7 | 2008-02-02 15:01:17 +0100 | [diff] [blame] | 3375 | iov_iter_advance(i, copied); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3376 | if (unlikely(copied == 0)) { |
| 3377 | /* |
| 3378 | * If we were unable to copy any data at all, we must |
| 3379 | * fall back to a single segment length write. |
| 3380 | * |
| 3381 | * If we didn't fallback here, we could livelock |
| 3382 | * because not all segments in the iov can be copied at |
| 3383 | * once without a pagefault. |
| 3384 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3385 | bytes = min_t(unsigned long, PAGE_SIZE - offset, |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3386 | iov_iter_single_seg_count(i)); |
| 3387 | goto again; |
| 3388 | } |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3389 | pos += copied; |
| 3390 | written += copied; |
| 3391 | |
| 3392 | balance_dirty_pages_ratelimited(mapping); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3393 | } while (iov_iter_count(i)); |
| 3394 | |
| 3395 | return written ? written : status; |
| 3396 | } |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3397 | EXPORT_SYMBOL(generic_perform_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 3399 | /** |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3400 | * __generic_file_write_iter - write data to a file |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 3401 | * @iocb: IO state structure (file, offset, etc.) |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3402 | * @from: iov_iter with data to write |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 3403 | * |
| 3404 | * This function does all the work needed for actually writing data to a |
| 3405 | * file. It does all basic checks, removes SUID from the file, updates |
| 3406 | * modification times and calls proper subroutines depending on whether we |
| 3407 | * do direct IO or a standard buffered write. |
| 3408 | * |
| 3409 | * It expects i_mutex to be grabbed unless we work on a block device or similar |
| 3410 | * object which does not need locking at all. |
| 3411 | * |
| 3412 | * This function does *not* take care of syncing data in case of O_SYNC write. |
| 3413 | * A caller has to handle it. This is mainly due to the fact that we want to |
| 3414 | * avoid syncing under i_mutex. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 3415 | * |
| 3416 | * Return: |
| 3417 | * * number of bytes written, even for truncated writes |
| 3418 | * * negative error code if no data has been written at all |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 3419 | */ |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3420 | ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3421 | { |
| 3422 | struct file *file = iocb->ki_filp; |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3423 | struct address_space * mapping = file->f_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 | struct inode *inode = mapping->host; |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3425 | ssize_t written = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3426 | ssize_t err; |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3427 | ssize_t status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3429 | /* We can write back this queue in page reclaim */ |
Christoph Hellwig | de1414a | 2015-01-14 10:42:36 +0100 | [diff] [blame] | 3430 | current->backing_dev_info = inode_to_bdi(inode); |
Jan Kara | 5fa8e0a | 2015-05-21 16:05:53 +0200 | [diff] [blame] | 3431 | err = file_remove_privs(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3432 | if (err) |
| 3433 | goto out; |
| 3434 | |
Josef Bacik | c3b2da3 | 2012-03-26 09:59:21 -0400 | [diff] [blame] | 3435 | err = file_update_time(file); |
| 3436 | if (err) |
| 3437 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3438 | |
Al Viro | 2ba48ce | 2015-04-09 13:52:01 -0400 | [diff] [blame] | 3439 | if (iocb->ki_flags & IOCB_DIRECT) { |
Al Viro | 0b8def9 | 2015-04-07 10:22:53 -0400 | [diff] [blame] | 3440 | loff_t pos, endbyte; |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3441 | |
Christoph Hellwig | 1af5bb4 | 2016-04-07 08:51:56 -0700 | [diff] [blame] | 3442 | written = generic_file_direct_write(iocb, from); |
Matthew Wilcox | fbbbad4 | 2015-02-16 15:58:53 -0800 | [diff] [blame] | 3443 | /* |
| 3444 | * If the write stopped short of completing, fall back to |
| 3445 | * buffered writes. Some filesystems do this for writes to |
| 3446 | * holes, for example. For DAX files, a buffered write will |
| 3447 | * not succeed (even if it did, DAX does not handle dirty |
| 3448 | * page-cache pages correctly). |
| 3449 | */ |
Al Viro | 0b8def9 | 2015-04-07 10:22:53 -0400 | [diff] [blame] | 3450 | if (written < 0 || !iov_iter_count(from) || IS_DAX(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | goto out; |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3452 | |
Al Viro | 0b8def9 | 2015-04-07 10:22:53 -0400 | [diff] [blame] | 3453 | status = generic_perform_write(file, from, pos = iocb->ki_pos); |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3454 | /* |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3455 | * If generic_perform_write() returned a synchronous error |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3456 | * then we want to return the number of bytes which were |
| 3457 | * direct-written, or the error code if that was zero. Note |
| 3458 | * that this differs from normal direct-io semantics, which |
| 3459 | * will return -EFOO even if some bytes were written. |
| 3460 | */ |
Al Viro | 60bb452 | 2014-08-08 12:39:16 -0400 | [diff] [blame] | 3461 | if (unlikely(status < 0)) { |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3462 | err = status; |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3463 | goto out; |
| 3464 | } |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3465 | /* |
| 3466 | * We need to ensure that the page cache pages are written to |
| 3467 | * disk and invalidated to preserve the expected O_DIRECT |
| 3468 | * semantics. |
| 3469 | */ |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3470 | endbyte = pos + status - 1; |
Al Viro | 0b8def9 | 2015-04-07 10:22:53 -0400 | [diff] [blame] | 3471 | err = filemap_write_and_wait_range(mapping, pos, endbyte); |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3472 | if (err == 0) { |
Al Viro | 0b8def9 | 2015-04-07 10:22:53 -0400 | [diff] [blame] | 3473 | iocb->ki_pos = endbyte + 1; |
Al Viro | 3b93f91 | 2014-02-11 21:34:08 -0500 | [diff] [blame] | 3474 | written += status; |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3475 | invalidate_mapping_pages(mapping, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3476 | pos >> PAGE_SHIFT, |
| 3477 | endbyte >> PAGE_SHIFT); |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3478 | } else { |
| 3479 | /* |
| 3480 | * We don't know how much we wrote, so just return |
| 3481 | * the number of bytes which were direct-written |
| 3482 | */ |
| 3483 | } |
| 3484 | } else { |
Al Viro | 0b8def9 | 2015-04-07 10:22:53 -0400 | [diff] [blame] | 3485 | written = generic_perform_write(file, from, iocb->ki_pos); |
| 3486 | if (likely(written > 0)) |
| 3487 | iocb->ki_pos += written; |
Jeff Moyer | fb5527e | 2006-10-19 23:28:13 -0700 | [diff] [blame] | 3488 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3489 | out: |
| 3490 | current->backing_dev_info = NULL; |
| 3491 | return written ? written : err; |
| 3492 | } |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3493 | EXPORT_SYMBOL(__generic_file_write_iter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3494 | |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 3495 | /** |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3496 | * generic_file_write_iter - write data to a file |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 3497 | * @iocb: IO state structure |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3498 | * @from: iov_iter with data to write |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 3499 | * |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3500 | * This is a wrapper around __generic_file_write_iter() to be used by most |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 3501 | * filesystems. It takes care of syncing the file in case of O_SYNC file |
| 3502 | * and acquires i_mutex as needed. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 3503 | * Return: |
| 3504 | * * negative error code if no data has been written at all of |
| 3505 | * vfs_fsync_range() failed for a synchronous write |
| 3506 | * * number of bytes written, even for truncated writes |
Jan Kara | e4dd9de | 2009-08-17 18:10:06 +0200 | [diff] [blame] | 3507 | */ |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3508 | ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3509 | { |
| 3510 | struct file *file = iocb->ki_filp; |
Jan Kara | 148f948 | 2009-08-17 19:52:36 +0200 | [diff] [blame] | 3511 | struct inode *inode = file->f_mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3512 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3513 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3514 | inode_lock(inode); |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 3515 | ret = generic_write_checks(iocb, from); |
| 3516 | if (ret > 0) |
Al Viro | 5f380c7 | 2015-04-07 11:28:12 -0400 | [diff] [blame] | 3517 | ret = __generic_file_write_iter(iocb, from); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3518 | inode_unlock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3519 | |
Christoph Hellwig | e259221 | 2016-04-07 08:52:01 -0700 | [diff] [blame] | 3520 | if (ret > 0) |
| 3521 | ret = generic_write_sync(iocb, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3522 | return ret; |
| 3523 | } |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3524 | EXPORT_SYMBOL(generic_file_write_iter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3525 | |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 3526 | /** |
| 3527 | * try_to_release_page() - release old fs-specific metadata on a page |
| 3528 | * |
| 3529 | * @page: the page which the kernel is trying to free |
| 3530 | * @gfp_mask: memory allocation flags (and I/O mode) |
| 3531 | * |
| 3532 | * The address_space is to try to release any data against the page |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 3533 | * (presumably at page->private). |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 3534 | * |
David Howells | 266cf65 | 2009-04-03 16:42:36 +0100 | [diff] [blame] | 3535 | * This may also be called if PG_fscache is set on a page, indicating that the |
| 3536 | * page is known to the local caching routines. |
| 3537 | * |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 3538 | * The @gfp_mask argument specifies whether I/O may be performed to release |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 3539 | * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS). |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 3540 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 3541 | * Return: %1 if the release was successful, otherwise return zero. |
David Howells | cf9a2ae | 2006-08-29 19:05:54 +0100 | [diff] [blame] | 3542 | */ |
| 3543 | int try_to_release_page(struct page *page, gfp_t gfp_mask) |
| 3544 | { |
| 3545 | struct address_space * const mapping = page->mapping; |
| 3546 | |
| 3547 | BUG_ON(!PageLocked(page)); |
| 3548 | if (PageWriteback(page)) |
| 3549 | return 0; |
| 3550 | |
| 3551 | if (mapping && mapping->a_ops->releasepage) |
| 3552 | return mapping->a_ops->releasepage(page, gfp_mask); |
| 3553 | return try_to_free_buffers(page); |
| 3554 | } |
| 3555 | |
| 3556 | EXPORT_SYMBOL(try_to_release_page); |