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