blob: 0fad08331cf441a22323e54fefdeb4608720e4fa [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/mm/filemap.c
4 *
5 * Copyright (C) 1994-1999 Linus Torvalds
6 */
7
8/*
9 * This file handles the generic file mmap semantics used by
10 * most "normal" filesystems (but you don't /have/ to use this:
11 * the NFS filesystem used to do this differently, for example)
12 */
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040013#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/compiler.h>
Ross Zwislerf9fe48b2016-01-22 15:10:40 -080015#include <linux/dax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/fs.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010017#include <linux/sched/signal.h>
Hiro Yoshiokac22ce142006-06-23 02:04:16 -070018#include <linux/uaccess.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080019#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/kernel_stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mm.h>
23#include <linux/swap.h>
24#include <linux/mman.h>
25#include <linux/pagemap.h>
26#include <linux/file.h>
27#include <linux/uio.h>
Josef Bacikcfcbfb12019-05-13 17:21:04 -070028#include <linux/error-injection.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/hash.h>
30#include <linux/writeback.h>
Linus Torvalds53253382007-10-18 14:47:32 -070031#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/pagevec.h>
33#include <linux/blkdev.h>
34#include <linux/security.h>
Paul Jackson44110fe2006-03-24 03:16:04 -080035#include <linux/cpuset.h>
Johannes Weiner00501b52014-08-08 14:19:20 -070036#include <linux/hugetlb.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080037#include <linux/memcontrol.h>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060038#include <linux/cleancache.h>
Mel Gormanc7df8ad2017-11-15 17:37:41 -080039#include <linux/shmem_fs.h>
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070040#include <linux/rmap.h>
Johannes Weinerb1d29ba2018-10-26 15:06:08 -070041#include <linux/delayacct.h>
Johannes Weinereb414682018-10-26 15:06:27 -070042#include <linux/psi.h>
Ben Dooksd0e6a582019-10-18 20:20:20 -070043#include <linux/ramfs.h>
Yang Shib9306a72020-08-06 23:19:55 -070044#include <linux/page_idle.h>
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +030045#include <asm/pgalloc.h>
Will Deaconde591a82021-02-10 11:15:11 +000046#include <asm/tlbflush.h>
Nick Piggin0f8053a2006-03-22 00:08:33 -080047#include "internal.h"
48
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -070049#define CREATE_TRACE_POINTS
50#include <trace/events/filemap.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * FIXME: remove all knowledge of the buffer layer from the core VM
54 */
Jan Kara148f9482009-08-17 19:52:36 +020055#include <linux/buffer_head.h> /* for try_to_free_buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/mman.h>
58
59/*
60 * Shared mappings implemented 30.11.1994. It's not fully working yet,
61 * though.
62 *
63 * Shared mappings now work. 15.8.1995 Bruno.
64 *
65 * finished 'unifying' the page and buffer cache and SMP-threaded the
66 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
67 *
68 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
69 */
70
71/*
72 * Lock ordering:
73 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080074 * ->i_mmap_rwsem (truncate_pagecache)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070076 * ->swap_lock (exclusive_swap_page, others)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070077 * ->i_pages lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 *
Jan Kara96087032021-04-12 15:50:21 +020079 * ->i_rwsem
Jan Kara730633f2021-01-28 19:19:45 +010080 * ->invalidate_lock (acquired by fs in truncate path)
81 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070083 * ->mmap_lock
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080084 * ->i_mmap_rwsem
Hugh Dickinsb8072f02005-10-29 18:16:41 -070085 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070086 * ->i_pages lock (arch-dependent flush_dcache_mmap_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070088 * ->mmap_lock
Jan Kara730633f2021-01-28 19:19:45 +010089 * ->invalidate_lock (filemap_fault)
90 * ->lock_page (filemap_fault, access_process_vm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 *
Jan Kara96087032021-04-12 15:50:21 +020092 * ->i_rwsem (generic_perform_write)
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070093 * ->mmap_lock (fault_in_pages_readable->do_page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060095 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110096 * sb_lock (fs/fs-writeback.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070097 * ->i_pages lock (__sync_single_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080099 * ->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * ->anon_vma.lock (vma_adjust)
101 *
102 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700103 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700105 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -0700106 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * ->private_lock (try_to_unmap_one)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700108 * ->i_pages lock (try_to_unmap_one)
Hugh Dickins15b44732020-12-15 14:21:31 -0800109 * ->lruvec->lru_lock (follow_page->mark_page_accessed)
110 * ->lruvec->lru_lock (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * ->private_lock (page_remove_rmap->set_page_dirty)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700112 * ->i_pages lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600113 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100114 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700115 * ->memcg->move_lock (page_remove_rmap->lock_page_memcg)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600116 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100117 * ->inode->i_lock (zap_pte_range->set_page_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
119 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800120 * ->i_mmap_rwsem
Andi Kleen9a3c5312012-03-21 16:34:09 -0700121 * ->tasklist_lock (memory_failure, collect_procs_ao)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 */
123
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500124static void page_cache_delete(struct address_space *mapping,
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700125 struct page *page, void *shadow)
126{
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500127 XA_STATE(xas, &mapping->i_pages, page->index);
128 unsigned int nr = 1;
Kirill A. Shutemovc70b6472016-12-12 16:43:17 -0800129
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500130 mapping_set_update(&xas, mapping);
131
132 /* hugetlb pages are represented by a single entry in the xarray */
133 if (!PageHuge(page)) {
134 xas_set_order(&xas, page->index, compound_order(page));
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -0700135 nr = compound_nr(page);
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500136 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700137
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700138 VM_BUG_ON_PAGE(!PageLocked(page), page);
139 VM_BUG_ON_PAGE(PageTail(page), page);
140 VM_BUG_ON_PAGE(nr != 1 && shadow, page);
Johannes Weiner449dd692014-04-03 14:47:56 -0700141
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500142 xas_store(&xas, shadow);
143 xas_init_marks(&xas);
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200144
Jan Kara23006382017-11-15 17:37:26 -0800145 page->mapping = NULL;
146 /* Leave page->index set: truncation lookup relies upon it */
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200147 mapping->nrpages -= nr;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700148}
149
Jan Kara5ecc4d82017-11-15 17:37:29 -0800150static void unaccount_page_cache_page(struct address_space *mapping,
151 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Jan Kara5ecc4d82017-11-15 17:37:29 -0800153 int nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600155 /*
156 * if we're uptodate, flush out into the cleancache, otherwise
157 * invalidate any existing cleancache entries. We can't leave
158 * stale data around in the cleancache once our page is gone
159 */
160 if (PageUptodate(page) && PageMappedToDisk(page))
161 cleancache_put_page(page);
162 else
Dan Magenheimer31677602011-09-21 11:56:28 -0400163 cleancache_invalidate_page(mapping, page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600164
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700165 VM_BUG_ON_PAGE(PageTail(page), page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800166 VM_BUG_ON_PAGE(page_mapped(page), page);
167 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(page_mapped(page))) {
168 int mapcount;
169
170 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
171 current->comm, page_to_pfn(page));
172 dump_page(page, "still mapped when deleted");
173 dump_stack();
174 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
175
176 mapcount = page_mapcount(page);
177 if (mapping_exiting(mapping) &&
178 page_count(page) >= mapcount + 2) {
179 /*
180 * All vmas have already been torn down, so it's
181 * a good bet that actually the page is unmapped,
182 * and we'd prefer not to leak it: if we're wrong,
183 * some other bad page check should catch it later.
184 */
185 page_mapcount_reset(page);
Joonsoo Kim6d061f92016-05-19 17:10:46 -0700186 page_ref_sub(page, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800187 }
188 }
189
Jan Kara76253fb2017-11-15 17:37:22 -0800190 /* hugetlb pages do not participate in page cache accounting. */
Jan Kara5ecc4d82017-11-15 17:37:29 -0800191 if (PageHuge(page))
192 return;
Jan Kara76253fb2017-11-15 17:37:22 -0800193
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700194 nr = thp_nr_pages(page);
Jan Kara5ecc4d82017-11-15 17:37:29 -0800195
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700196 __mod_lruvec_page_state(page, NR_FILE_PAGES, -nr);
Jan Kara5ecc4d82017-11-15 17:37:29 -0800197 if (PageSwapBacked(page)) {
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700198 __mod_lruvec_page_state(page, NR_SHMEM, -nr);
Jan Kara5ecc4d82017-11-15 17:37:29 -0800199 if (PageTransHuge(page))
Muchun Song57b28472021-02-24 12:03:31 -0800200 __mod_lruvec_page_state(page, NR_SHMEM_THPS, -nr);
Song Liu99cb0db2019-09-23 15:38:00 -0700201 } else if (PageTransHuge(page)) {
Muchun Songbf9ecea2021-02-24 12:03:27 -0800202 __mod_lruvec_page_state(page, NR_FILE_THPS, -nr);
Song Liu09d91cd2019-09-23 15:38:03 -0700203 filemap_nr_thps_dec(mapping);
Jan Kara76253fb2017-11-15 17:37:22 -0800204 }
Jan Kara5ecc4d82017-11-15 17:37:29 -0800205
206 /*
207 * At this point page must be either written or cleaned by
208 * truncate. Dirty page here signals a bug and loss of
209 * unwritten data.
210 *
211 * This fixes dirty accounting after removing the page entirely
212 * but leaves PageDirty set: it has no effect for truncated
213 * page and anyway will be cleared before returning page into
214 * buddy allocator.
215 */
216 if (WARN_ON_ONCE(PageDirty(page)))
217 account_page_cleaned(page, mapping, inode_to_wb(mapping->host));
218}
219
220/*
221 * Delete a page from the page cache and free it. Caller has to make
222 * sure the page is locked and that nobody else uses it - or that usage
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700223 * is safe. The caller must hold the i_pages lock.
Jan Kara5ecc4d82017-11-15 17:37:29 -0800224 */
225void __delete_from_page_cache(struct page *page, void *shadow)
226{
227 struct address_space *mapping = page->mapping;
228
229 trace_mm_filemap_delete_from_page_cache(page);
230
231 unaccount_page_cache_page(mapping, page);
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500232 page_cache_delete(mapping, page, shadow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Jan Kara59c66c52017-11-15 17:37:18 -0800235static void page_cache_free_page(struct address_space *mapping,
236 struct page *page)
237{
238 void (*freepage)(struct page *);
239
240 freepage = mapping->a_ops->freepage;
241 if (freepage)
242 freepage(page);
243
244 if (PageTransHuge(page) && !PageHuge(page)) {
Matthew Wilcox (Oracle)887b22c2020-10-15 20:05:23 -0700245 page_ref_sub(page, thp_nr_pages(page));
Jan Kara59c66c52017-11-15 17:37:18 -0800246 VM_BUG_ON_PAGE(page_count(page) <= 0, page);
247 } else {
248 put_page(page);
249 }
250}
251
Minchan Kim702cfbf2011-03-22 16:32:43 -0700252/**
253 * delete_from_page_cache - delete page from page cache
254 * @page: the page which the kernel is trying to remove from page cache
255 *
256 * This must be called only on pages that have been verified to be in the page
257 * cache and locked. It will never put the page into the free list, the caller
258 * has a reference on the page.
259 */
260void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700262 struct address_space *mapping = page_mapping(page);
Greg Thelenc4843a72015-05-22 17:13:16 -0400263 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Matt Mackallcd7619d2005-05-01 08:59:01 -0700265 BUG_ON(!PageLocked(page));
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700266 xa_lock_irqsave(&mapping->i_pages, flags);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700267 __delete_from_page_cache(page, NULL);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700268 xa_unlock_irqrestore(&mapping->i_pages, flags);
Linus Torvalds6072d132010-12-01 13:35:19 -0500269
Jan Kara59c66c52017-11-15 17:37:18 -0800270 page_cache_free_page(mapping, page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700271}
272EXPORT_SYMBOL(delete_from_page_cache);
273
Jan Karaaa65c292017-11-15 17:37:33 -0800274/*
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500275 * page_cache_delete_batch - delete several pages from page cache
Jan Karaaa65c292017-11-15 17:37:33 -0800276 * @mapping: the mapping to which pages belong
277 * @pvec: pagevec with pages to delete
278 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700279 * The function walks over mapping->i_pages and removes pages passed in @pvec
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700280 * from the mapping. The function expects @pvec to be sorted by page index
281 * and is optimised for it to be dense.
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700282 * It tolerates holes in @pvec (mapping entries at those indices are not
Jan Karaaa65c292017-11-15 17:37:33 -0800283 * modified). The function expects only THP head pages to be present in the
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700284 * @pvec.
Jan Karaaa65c292017-11-15 17:37:33 -0800285 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700286 * The function expects the i_pages lock to be held.
Jan Karaaa65c292017-11-15 17:37:33 -0800287 */
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500288static void page_cache_delete_batch(struct address_space *mapping,
Jan Karaaa65c292017-11-15 17:37:33 -0800289 struct pagevec *pvec)
290{
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500291 XA_STATE(xas, &mapping->i_pages, pvec->pages[0]->index);
Jan Karaaa65c292017-11-15 17:37:33 -0800292 int total_pages = 0;
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700293 int i = 0;
Jan Karaaa65c292017-11-15 17:37:33 -0800294 struct page *page;
Jan Karaaa65c292017-11-15 17:37:33 -0800295
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500296 mapping_set_update(&xas, mapping);
297 xas_for_each(&xas, page, ULONG_MAX) {
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700298 if (i >= pagevec_count(pvec))
Jan Karaaa65c292017-11-15 17:37:33 -0800299 break;
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700300
301 /* A swap/dax/shadow entry got inserted? Skip it. */
Matthew Wilcox3159f942017-11-03 13:30:42 -0400302 if (xa_is_value(page))
Jan Karaaa65c292017-11-15 17:37:33 -0800303 continue;
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700304 /*
305 * A page got inserted in our range? Skip it. We have our
306 * pages locked so they are protected from being removed.
307 * If we see a page whose index is higher than ours, it
308 * means our page has been removed, which shouldn't be
309 * possible because we're holding the PageLock.
310 */
311 if (page != pvec->pages[i]) {
312 VM_BUG_ON_PAGE(page->index > pvec->pages[i]->index,
313 page);
314 continue;
Linus Torvalds69bf4b62019-07-05 19:55:18 -0700315 }
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700316
317 WARN_ON_ONCE(!PageLocked(page));
318
319 if (page->index == xas.xa_index)
320 page->mapping = NULL;
321 /* Leave page->index set: truncation lookup relies on it */
322
323 /*
324 * Move to the next page in the vector if this is a regular
325 * page or the index is of the last sub-page of this compound
326 * page.
327 */
328 if (page->index + compound_nr(page) - 1 == xas.xa_index)
329 i++;
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500330 xas_store(&xas, NULL);
Jan Karaaa65c292017-11-15 17:37:33 -0800331 total_pages++;
332 }
333 mapping->nrpages -= total_pages;
334}
335
336void delete_from_page_cache_batch(struct address_space *mapping,
337 struct pagevec *pvec)
338{
339 int i;
340 unsigned long flags;
341
342 if (!pagevec_count(pvec))
343 return;
344
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700345 xa_lock_irqsave(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800346 for (i = 0; i < pagevec_count(pvec); i++) {
347 trace_mm_filemap_delete_from_page_cache(pvec->pages[i]);
348
349 unaccount_page_cache_page(mapping, pvec->pages[i]);
350 }
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500351 page_cache_delete_batch(mapping, pvec);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700352 xa_unlock_irqrestore(&mapping->i_pages, flags);
Jan Karaaa65c292017-11-15 17:37:33 -0800353
354 for (i = 0; i < pagevec_count(pvec); i++)
355 page_cache_free_page(mapping, pvec->pages[i]);
356}
357
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200358int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700359{
360 int ret = 0;
361 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700362 if (test_bit(AS_ENOSPC, &mapping->flags) &&
363 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700364 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700365 if (test_bit(AS_EIO, &mapping->flags) &&
366 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700367 ret = -EIO;
368 return ret;
369}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200370EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700371
Jeff Layton76341ca2017-07-06 07:02:22 -0400372static int filemap_check_and_keep_errors(struct address_space *mapping)
373{
374 /* Check for outstanding write errors */
375 if (test_bit(AS_EIO, &mapping->flags))
376 return -EIO;
377 if (test_bit(AS_ENOSPC, &mapping->flags))
378 return -ENOSPC;
379 return 0;
380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700383 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700384 * @mapping: address space structure to write
385 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800386 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700387 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700389 * Start writeback against all of a mapping's dirty pages that lie
390 * within the byte offsets <start, end> inclusive.
391 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700393 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * these two operations is that if a dirty page/buffer is encountered, it must
395 * be waited upon, and not just skipped over.
Mike Rapoporta862f682019-03-05 15:48:42 -0800396 *
397 * Return: %0 on success, negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800399int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
400 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 int ret;
403 struct writeback_control wbc = {
404 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800405 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700406 .range_start = start,
407 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 };
409
Christoph Hellwigf56753a2020-09-24 08:51:40 +0200410 if (!mapping_can_writeback(mapping) ||
Konstantin Khlebnikovc3aab9a02019-09-23 15:34:45 -0700411 !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return 0;
413
Tejun Heob16b1de2015-06-02 08:39:48 -0600414 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 ret = do_writepages(mapping, &wbc);
Tejun Heob16b1de2015-06-02 08:39:48 -0600416 wbc_detach_inode(&wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return ret;
418}
419
420static inline int __filemap_fdatawrite(struct address_space *mapping,
421 int sync_mode)
422{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700423 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426int filemap_fdatawrite(struct address_space *mapping)
427{
428 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
429}
430EXPORT_SYMBOL(filemap_fdatawrite);
431
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400432int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800433 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
436}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400437EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Randy Dunlap485bb992006-06-23 02:03:49 -0700439/**
440 * filemap_flush - mostly a non-blocking flush
441 * @mapping: target address_space
442 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * This is a mostly non-blocking flush. Not suitable for data-integrity
444 * purposes - I/O may not be started against all dirty pages.
Mike Rapoporta862f682019-03-05 15:48:42 -0800445 *
446 * Return: %0 on success, negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 */
448int filemap_flush(struct address_space *mapping)
449{
450 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
451}
452EXPORT_SYMBOL(filemap_flush);
453
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500454/**
455 * filemap_range_has_page - check if a page exists in range.
456 * @mapping: address space within which to check
457 * @start_byte: offset in bytes where the range starts
458 * @end_byte: offset in bytes where the range ends (inclusive)
459 *
460 * Find at least one page in the range supplied, usually used to check if
461 * direct writing in this range will trigger a writeback.
Mike Rapoporta862f682019-03-05 15:48:42 -0800462 *
463 * Return: %true if at least one page exists in the specified range,
464 * %false otherwise.
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500465 */
466bool filemap_range_has_page(struct address_space *mapping,
467 loff_t start_byte, loff_t end_byte)
468{
Jan Karaf7b68042017-09-06 16:21:40 -0700469 struct page *page;
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500470 XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT);
471 pgoff_t max = end_byte >> PAGE_SHIFT;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500472
473 if (end_byte < start_byte)
474 return false;
475
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500476 rcu_read_lock();
477 for (;;) {
478 page = xas_find(&xas, max);
479 if (xas_retry(&xas, page))
480 continue;
481 /* Shadow entries don't count */
482 if (xa_is_value(page))
483 continue;
484 /*
485 * We don't need to try to pin this page; we're about to
486 * release the RCU lock anyway. It is enough to know that
487 * there was a page here recently.
488 */
489 break;
490 }
491 rcu_read_unlock();
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500492
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500493 return page != NULL;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500494}
495EXPORT_SYMBOL(filemap_range_has_page);
496
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400497static void __filemap_fdatawait_range(struct address_space *mapping,
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800498 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300500 pgoff_t index = start_byte >> PAGE_SHIFT;
501 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct pagevec pvec;
503 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200505 if (end_byte < start_byte)
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400506 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Mel Gorman86679822017-11-15 17:37:52 -0800508 pagevec_init(&pvec);
Jan Kara312e9d22017-11-15 17:35:05 -0800509 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 unsigned i;
511
Jan Kara312e9d22017-11-15 17:35:05 -0800512 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara67fd7072017-11-15 17:35:19 -0800513 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara312e9d22017-11-15 17:35:05 -0800514 if (!nr_pages)
515 break;
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 for (i = 0; i < nr_pages; i++) {
518 struct page *page = pvec.pages[i];
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 wait_on_page_writeback(page);
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400521 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523 pagevec_release(&pvec);
524 cond_resched();
525 }
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800526}
527
528/**
529 * filemap_fdatawait_range - wait for writeback to complete
530 * @mapping: address space structure to wait for
531 * @start_byte: offset in bytes where the range starts
532 * @end_byte: offset in bytes where the range ends (inclusive)
533 *
534 * Walk the list of under-writeback pages of the given address space
535 * in the given range and wait for all of them. Check error status of
536 * the address space and return it.
537 *
538 * Since the error status of the address space is cleared by this function,
539 * callers are responsible for checking the return value and handling and/or
540 * reporting the error.
Mike Rapoporta862f682019-03-05 15:48:42 -0800541 *
542 * Return: error status of the address space.
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800543 */
544int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
545 loff_t end_byte)
546{
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400547 __filemap_fdatawait_range(mapping, start_byte, end_byte);
548 return filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200550EXPORT_SYMBOL(filemap_fdatawait_range);
551
552/**
Ross Zwisleraa0bfcd2019-06-20 17:05:37 -0400553 * filemap_fdatawait_range_keep_errors - wait for writeback to complete
554 * @mapping: address space structure to wait for
555 * @start_byte: offset in bytes where the range starts
556 * @end_byte: offset in bytes where the range ends (inclusive)
557 *
558 * Walk the list of under-writeback pages of the given address space in the
559 * given range and wait for all of them. Unlike filemap_fdatawait_range(),
560 * this function does not clear error status of the address space.
561 *
562 * Use this function if callers don't handle errors themselves. Expected
563 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
564 * fsfreeze(8)
565 */
566int filemap_fdatawait_range_keep_errors(struct address_space *mapping,
567 loff_t start_byte, loff_t end_byte)
568{
569 __filemap_fdatawait_range(mapping, start_byte, end_byte);
570 return filemap_check_and_keep_errors(mapping);
571}
572EXPORT_SYMBOL(filemap_fdatawait_range_keep_errors);
573
574/**
Jeff Laytona823e452017-07-28 07:24:43 -0400575 * file_fdatawait_range - wait for writeback to complete
576 * @file: file pointing to address space structure to wait for
577 * @start_byte: offset in bytes where the range starts
578 * @end_byte: offset in bytes where the range ends (inclusive)
579 *
580 * Walk the list of under-writeback pages of the address space that file
581 * refers to, in the given range and wait for all of them. Check error
582 * status of the address space vs. the file->f_wb_err cursor and return it.
583 *
584 * Since the error status of the file is advanced by this function,
585 * callers are responsible for checking the return value and handling and/or
586 * reporting the error.
Mike Rapoporta862f682019-03-05 15:48:42 -0800587 *
588 * Return: error status of the address space vs. the file->f_wb_err cursor.
Jeff Laytona823e452017-07-28 07:24:43 -0400589 */
590int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
591{
592 struct address_space *mapping = file->f_mapping;
593
594 __filemap_fdatawait_range(mapping, start_byte, end_byte);
595 return file_check_and_advance_wb_err(file);
596}
597EXPORT_SYMBOL(file_fdatawait_range);
598
599/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800600 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
601 * @mapping: address space structure to wait for
602 *
603 * Walk the list of under-writeback pages of the given address space
604 * and wait for all of them. Unlike filemap_fdatawait(), this function
605 * does not clear error status of the address space.
606 *
607 * Use this function if callers don't handle errors themselves. Expected
608 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
609 * fsfreeze(8)
Mike Rapoporta862f682019-03-05 15:48:42 -0800610 *
611 * Return: error status of the address space.
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800612 */
Jeff Layton76341ca2017-07-06 07:02:22 -0400613int filemap_fdatawait_keep_errors(struct address_space *mapping)
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800614{
Jeff Laytonffb959b2017-07-31 10:29:38 -0400615 __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
Jeff Layton76341ca2017-07-06 07:02:22 -0400616 return filemap_check_and_keep_errors(mapping);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800617}
Jeff Layton76341ca2017-07-06 07:02:22 -0400618EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800619
Konstantin Khlebnikov875d91b2019-09-23 15:34:48 -0700620/* Returns true if writeback might be needed or already in progress. */
Jeff Layton9326c9b2017-07-26 10:21:11 -0400621static bool mapping_needs_writeback(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Konstantin Khlebnikov875d91b2019-09-23 15:34:48 -0700623 return mapping->nrpages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Randy Dunlap485bb992006-06-23 02:03:49 -0700626/**
Jens Axboe63135aa2021-04-29 22:55:18 -0700627 * filemap_range_needs_writeback - check if range potentially needs writeback
628 * @mapping: address space within which to check
629 * @start_byte: offset in bytes where the range starts
630 * @end_byte: offset in bytes where the range ends (inclusive)
631 *
632 * Find at least one page in the range supplied, usually used to check if
633 * direct writing in this range will trigger a writeback. Used by O_DIRECT
634 * read/write with IOCB_NOWAIT, to see if the caller needs to do
635 * filemap_write_and_wait_range() before proceeding.
636 *
637 * Return: %true if the caller should do filemap_write_and_wait_range() before
638 * doing O_DIRECT to a page in this range, %false otherwise.
639 */
640bool filemap_range_needs_writeback(struct address_space *mapping,
641 loff_t start_byte, loff_t end_byte)
642{
643 XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT);
644 pgoff_t max = end_byte >> PAGE_SHIFT;
645 struct page *page;
646
647 if (!mapping_needs_writeback(mapping))
648 return false;
649 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
650 !mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK))
651 return false;
652 if (end_byte < start_byte)
653 return false;
654
655 rcu_read_lock();
656 xas_for_each(&xas, page, max) {
657 if (xas_retry(&xas, page))
658 continue;
659 if (xa_is_value(page))
660 continue;
661 if (PageDirty(page) || PageLocked(page) || PageWriteback(page))
662 break;
663 }
664 rcu_read_unlock();
665 return page != NULL;
666}
667EXPORT_SYMBOL_GPL(filemap_range_needs_writeback);
668
669/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700670 * filemap_write_and_wait_range - write out & wait on a file range
671 * @mapping: the address_space for the pages
672 * @lstart: offset in bytes where the range starts
673 * @lend: offset in bytes where the range ends (inclusive)
674 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800675 * Write out and wait upon file offsets lstart->lend, inclusive.
676 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300677 * Note that @lend is inclusive (describes the last byte to be written) so
Andrew Morton469eb4d2006-03-24 03:17:45 -0800678 * that this function can be used to write to the very end-of-file (end = -1).
Mike Rapoporta862f682019-03-05 15:48:42 -0800679 *
680 * Return: error status of the address space.
Andrew Morton469eb4d2006-03-24 03:17:45 -0800681 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682int filemap_write_and_wait_range(struct address_space *mapping,
683 loff_t lstart, loff_t lend)
684{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800685 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Jeff Layton9326c9b2017-07-26 10:21:11 -0400687 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800688 err = __filemap_fdatawrite_range(mapping, lstart, lend,
689 WB_SYNC_ALL);
Ira Weinyddf8f372020-01-30 22:12:07 -0800690 /*
691 * Even if the above returned error, the pages may be
692 * written partially (e.g. -ENOSPC), so we wait for it.
693 * But the -EIO is special case, it may indicate the worst
694 * thing (e.g. bug) happened, so we avoid waiting for it.
695 */
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800696 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200697 int err2 = filemap_fdatawait_range(mapping,
698 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800699 if (!err)
700 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400701 } else {
702 /* Clear any previously stored errors */
703 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800704 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700705 } else {
706 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800708 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
Chris Masonf6995582009-04-15 13:22:37 -0400710EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Jeff Layton5660e132017-07-06 07:02:25 -0400712void __filemap_set_wb_err(struct address_space *mapping, int err)
713{
Jeff Layton3acdfd22017-07-24 06:22:15 -0400714 errseq_t eseq = errseq_set(&mapping->wb_err, err);
Jeff Layton5660e132017-07-06 07:02:25 -0400715
716 trace_filemap_set_wb_err(mapping, eseq);
717}
718EXPORT_SYMBOL(__filemap_set_wb_err);
719
720/**
721 * file_check_and_advance_wb_err - report wb error (if any) that was previously
722 * and advance wb_err to current one
723 * @file: struct file on which the error is being reported
724 *
725 * When userland calls fsync (or something like nfsd does the equivalent), we
726 * want to report any writeback errors that occurred since the last fsync (or
727 * since the file was opened if there haven't been any).
728 *
729 * Grab the wb_err from the mapping. If it matches what we have in the file,
730 * then just quickly return 0. The file is all caught up.
731 *
732 * If it doesn't match, then take the mapping value, set the "seen" flag in
733 * it and try to swap it into place. If it works, or another task beat us
734 * to it with the new value, then update the f_wb_err and return the error
735 * portion. The error at this point must be reported via proper channels
736 * (a'la fsync, or NFS COMMIT operation, etc.).
737 *
738 * While we handle mapping->wb_err with atomic operations, the f_wb_err
739 * value is protected by the f_lock since we must ensure that it reflects
740 * the latest value swapped in for this file descriptor.
Mike Rapoporta862f682019-03-05 15:48:42 -0800741 *
742 * Return: %0 on success, negative error code otherwise.
Jeff Layton5660e132017-07-06 07:02:25 -0400743 */
744int file_check_and_advance_wb_err(struct file *file)
745{
746 int err = 0;
747 errseq_t old = READ_ONCE(file->f_wb_err);
748 struct address_space *mapping = file->f_mapping;
749
750 /* Locklessly handle the common case where nothing has changed */
751 if (errseq_check(&mapping->wb_err, old)) {
752 /* Something changed, must use slow path */
753 spin_lock(&file->f_lock);
754 old = file->f_wb_err;
755 err = errseq_check_and_advance(&mapping->wb_err,
756 &file->f_wb_err);
757 trace_file_check_and_advance_wb_err(file, old);
758 spin_unlock(&file->f_lock);
759 }
Jeff Laytonf4e222c2017-10-03 16:15:25 -0700760
761 /*
762 * We're mostly using this function as a drop in replacement for
763 * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect
764 * that the legacy code would have had on these flags.
765 */
766 clear_bit(AS_EIO, &mapping->flags);
767 clear_bit(AS_ENOSPC, &mapping->flags);
Jeff Layton5660e132017-07-06 07:02:25 -0400768 return err;
769}
770EXPORT_SYMBOL(file_check_and_advance_wb_err);
771
772/**
773 * file_write_and_wait_range - write out & wait on a file range
774 * @file: file pointing to address_space with pages
775 * @lstart: offset in bytes where the range starts
776 * @lend: offset in bytes where the range ends (inclusive)
777 *
778 * Write out and wait upon file offsets lstart->lend, inclusive.
779 *
780 * Note that @lend is inclusive (describes the last byte to be written) so
781 * that this function can be used to write to the very end-of-file (end = -1).
782 *
783 * After writing out and waiting on the data, we check and advance the
784 * f_wb_err cursor to the latest value, and return any errors detected there.
Mike Rapoporta862f682019-03-05 15:48:42 -0800785 *
786 * Return: %0 on success, negative error code otherwise.
Jeff Layton5660e132017-07-06 07:02:25 -0400787 */
788int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
789{
790 int err = 0, err2;
791 struct address_space *mapping = file->f_mapping;
792
Jeff Layton9326c9b2017-07-26 10:21:11 -0400793 if (mapping_needs_writeback(mapping)) {
Jeff Layton5660e132017-07-06 07:02:25 -0400794 err = __filemap_fdatawrite_range(mapping, lstart, lend,
795 WB_SYNC_ALL);
796 /* See comment of filemap_write_and_wait() */
797 if (err != -EIO)
798 __filemap_fdatawait_range(mapping, lstart, lend);
799 }
800 err2 = file_check_and_advance_wb_err(file);
801 if (!err)
802 err = err2;
803 return err;
804}
805EXPORT_SYMBOL(file_write_and_wait_range);
806
Randy Dunlap485bb992006-06-23 02:03:49 -0700807/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700808 * replace_page_cache_page - replace a pagecache page with a new one
809 * @old: page to be replaced
810 * @new: page to replace with
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700811 *
812 * This function replaces a page in the pagecache with a new one. On
813 * success it acquires the pagecache reference for the new page and
814 * drops it for the old page. Both the old and new pages must be
815 * locked. This function does not add the new page to the LRU, the
816 * caller must do that.
817 *
Matthew Wilcox74d60952017-11-17 10:01:45 -0500818 * The remove + add is atomic. This function cannot fail.
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700819 */
Baolin Wang1f7ef652021-02-24 12:01:42 -0800820void replace_page_cache_page(struct page *old, struct page *new)
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700821{
Matthew Wilcox74d60952017-11-17 10:01:45 -0500822 struct address_space *mapping = old->mapping;
823 void (*freepage)(struct page *) = mapping->a_ops->freepage;
824 pgoff_t offset = old->index;
825 XA_STATE(xas, &mapping->i_pages, offset);
826 unsigned long flags;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700827
Sasha Levin309381fea2014-01-23 15:52:54 -0800828 VM_BUG_ON_PAGE(!PageLocked(old), old);
829 VM_BUG_ON_PAGE(!PageLocked(new), new);
830 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700831
Matthew Wilcox74d60952017-11-17 10:01:45 -0500832 get_page(new);
833 new->mapping = mapping;
834 new->index = offset;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700835
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700836 mem_cgroup_migrate(old, new);
837
Matthew Wilcox74d60952017-11-17 10:01:45 -0500838 xas_lock_irqsave(&xas, flags);
839 xas_store(&xas, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700840
Matthew Wilcox74d60952017-11-17 10:01:45 -0500841 old->mapping = NULL;
842 /* hugetlb pages do not participate in page cache accounting. */
843 if (!PageHuge(old))
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700844 __dec_lruvec_page_state(old, NR_FILE_PAGES);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500845 if (!PageHuge(new))
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700846 __inc_lruvec_page_state(new, NR_FILE_PAGES);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500847 if (PageSwapBacked(old))
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700848 __dec_lruvec_page_state(old, NR_SHMEM);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500849 if (PageSwapBacked(new))
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700850 __inc_lruvec_page_state(new, NR_SHMEM);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500851 xas_unlock_irqrestore(&xas, flags);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500852 if (freepage)
853 freepage(old);
854 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700855}
856EXPORT_SYMBOL_GPL(replace_page_cache_page);
857
Andrew Morton16c0cc02020-12-11 13:36:27 -0800858noinline int __add_to_page_cache_locked(struct page *page,
Alexei Starovoitov76cd6172020-08-27 15:01:10 -0700859 struct address_space *mapping,
Linus Torvaldsc4cf4982020-10-16 11:31:55 -0700860 pgoff_t offset, gfp_t gfp,
Alexei Starovoitov76cd6172020-08-27 15:01:10 -0700861 void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Matthew Wilcox74d60952017-11-17 10:01:45 -0500863 XA_STATE(xas, &mapping->i_pages, offset);
Johannes Weiner00501b52014-08-08 14:19:20 -0700864 int huge = PageHuge(page);
Nick Piggine2867812008-07-25 19:45:30 -0700865 int error;
Waiman Longda742402021-02-04 18:32:45 -0800866 bool charged = false;
Nick Piggine2867812008-07-25 19:45:30 -0700867
Sasha Levin309381fea2014-01-23 15:52:54 -0800868 VM_BUG_ON_PAGE(!PageLocked(page), page);
869 VM_BUG_ON_PAGE(PageSwapBacked(page), page);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500870 mapping_set_update(&xas, mapping);
Nick Piggine2867812008-07-25 19:45:30 -0700871
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300872 get_page(page);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700873 page->mapping = mapping;
874 page->index = offset;
875
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700876 if (!huge) {
Dan Schatzberg04f94e32021-06-28 19:38:18 -0700877 error = mem_cgroup_charge(page, NULL, gfp);
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700878 if (error)
879 goto error;
Waiman Longda742402021-02-04 18:32:45 -0800880 charged = true;
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700881 }
882
Matthew Wilcox (Oracle)198b62f2020-10-15 20:05:20 -0700883 gfp &= GFP_RECLAIM_MASK;
884
Matthew Wilcox74d60952017-11-17 10:01:45 -0500885 do {
Matthew Wilcox (Oracle)198b62f2020-10-15 20:05:20 -0700886 unsigned int order = xa_get_order(xas.xa, xas.xa_index);
887 void *entry, *old = NULL;
888
889 if (order > thp_order(page))
890 xas_split_alloc(&xas, xa_load(xas.xa, xas.xa_index),
891 order, gfp);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500892 xas_lock_irq(&xas);
Matthew Wilcox (Oracle)198b62f2020-10-15 20:05:20 -0700893 xas_for_each_conflict(&xas, entry) {
894 old = entry;
895 if (!xa_is_value(entry)) {
896 xas_set_err(&xas, -EEXIST);
897 goto unlock;
898 }
899 }
900
901 if (old) {
902 if (shadowp)
903 *shadowp = old;
904 /* entry may have been split before we acquired lock */
905 order = xa_get_order(xas.xa, xas.xa_index);
906 if (order > thp_order(page)) {
907 xas_split(&xas, old, order);
908 xas_reset(&xas);
909 }
910 }
911
Matthew Wilcox74d60952017-11-17 10:01:45 -0500912 xas_store(&xas, page);
913 if (xas_error(&xas))
914 goto unlock;
Michal Hocko4165b9b2015-06-24 16:57:24 -0700915
Matthew Wilcox74d60952017-11-17 10:01:45 -0500916 mapping->nrpages++;
917
918 /* hugetlb pages do not participate in page cache accounting */
919 if (!huge)
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700920 __inc_lruvec_page_state(page, NR_FILE_PAGES);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500921unlock:
922 xas_unlock_irq(&xas);
Matthew Wilcox (Oracle)198b62f2020-10-15 20:05:20 -0700923 } while (xas_nomem(&xas, gfp));
Matthew Wilcox74d60952017-11-17 10:01:45 -0500924
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700925 if (xas_error(&xas)) {
926 error = xas_error(&xas);
Waiman Longda742402021-02-04 18:32:45 -0800927 if (charged)
928 mem_cgroup_uncharge(page);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500929 goto error;
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700930 }
Matthew Wilcox74d60952017-11-17 10:01:45 -0500931
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700932 trace_mm_filemap_add_to_page_cache(page);
933 return 0;
Matthew Wilcox74d60952017-11-17 10:01:45 -0500934error:
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700935 page->mapping = NULL;
936 /* Leave page->index set: truncation relies upon it */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300937 put_page(page);
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700938 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
Josef Bacikcfcbfb12019-05-13 17:21:04 -0700940ALLOW_ERROR_INJECTION(__add_to_page_cache_locked, ERRNO);
Johannes Weinera5289102014-04-03 14:47:51 -0700941
942/**
943 * add_to_page_cache_locked - add a locked page to the pagecache
944 * @page: page to add
945 * @mapping: the page's address_space
946 * @offset: page index
947 * @gfp_mask: page allocation mode
948 *
949 * This function is used to add a page to the pagecache. It must be locked.
950 * This function does not add the page to the LRU. The caller must do that.
Mike Rapoporta862f682019-03-05 15:48:42 -0800951 *
952 * Return: %0 on success, negative error code otherwise.
Johannes Weinera5289102014-04-03 14:47:51 -0700953 */
954int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
955 pgoff_t offset, gfp_t gfp_mask)
956{
957 return __add_to_page_cache_locked(page, mapping, offset,
958 gfp_mask, NULL);
959}
Nick Piggine2867812008-07-25 19:45:30 -0700960EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
Al Viro6daa0e22005-10-21 03:18:50 -0400963 pgoff_t offset, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Johannes Weinera5289102014-04-03 14:47:51 -0700965 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700966 int ret;
967
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800968 __SetPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700969 ret = __add_to_page_cache_locked(page, mapping, offset,
970 gfp_mask, &shadow);
971 if (unlikely(ret))
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -0800972 __ClearPageLocked(page);
Johannes Weinera5289102014-04-03 14:47:51 -0700973 else {
974 /*
975 * The page might have been evicted from cache only
976 * recently, in which case it should be activated like
977 * any other repeatedly accessed page.
Rik van Rielf0281a02016-05-20 16:56:25 -0700978 * The exception is pages getting rewritten; evicting other
979 * data from the working set, only to cache data that will
980 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700981 */
Johannes Weiner1899ad12018-10-26 15:06:04 -0700982 WARN_ON_ONCE(PageActive(page));
983 if (!(gfp_mask & __GFP_WRITE) && shadow)
984 workingset_refault(page, shadow);
Johannes Weinera5289102014-04-03 14:47:51 -0700985 lru_cache_add(page);
986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return ret;
988}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +0300989EXPORT_SYMBOL_GPL(add_to_page_cache_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Paul Jackson44110fe2006-03-24 03:16:04 -0800991#ifdef CONFIG_NUMA
Nick Piggin2ae88142006-10-28 10:38:23 -0700992struct page *__page_cache_alloc(gfp_t gfp)
Paul Jackson44110fe2006-03-24 03:16:04 -0800993{
Miao Xiec0ff7452010-05-24 14:32:08 -0700994 int n;
995 struct page *page;
996
Paul Jackson44110fe2006-03-24 03:16:04 -0800997 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700998 unsigned int cpuset_mems_cookie;
999 do {
Mel Gormand26914d2014-04-03 14:47:24 -07001000 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -07001001 n = cpuset_mem_spread_node();
Vlastimil Babka96db8002015-09-08 15:03:50 -07001002 page = __alloc_pages_node(n, gfp, 0);
Mel Gormand26914d2014-04-03 14:47:24 -07001003 } while (!page && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -07001004
Miao Xiec0ff7452010-05-24 14:32:08 -07001005 return page;
Paul Jackson44110fe2006-03-24 03:16:04 -08001006 }
Nick Piggin2ae88142006-10-28 10:38:23 -07001007 return alloc_pages(gfp, 0);
Paul Jackson44110fe2006-03-24 03:16:04 -08001008}
Nick Piggin2ae88142006-10-28 10:38:23 -07001009EXPORT_SYMBOL(__page_cache_alloc);
Paul Jackson44110fe2006-03-24 03:16:04 -08001010#endif
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012/*
Jan Kara7506ae62021-05-24 13:02:30 +02001013 * filemap_invalidate_lock_two - lock invalidate_lock for two mappings
1014 *
1015 * Lock exclusively invalidate_lock of any passed mapping that is not NULL.
1016 *
1017 * @mapping1: the first mapping to lock
1018 * @mapping2: the second mapping to lock
1019 */
1020void filemap_invalidate_lock_two(struct address_space *mapping1,
1021 struct address_space *mapping2)
1022{
1023 if (mapping1 > mapping2)
1024 swap(mapping1, mapping2);
1025 if (mapping1)
1026 down_write(&mapping1->invalidate_lock);
1027 if (mapping2 && mapping1 != mapping2)
1028 down_write_nested(&mapping2->invalidate_lock, 1);
1029}
1030EXPORT_SYMBOL(filemap_invalidate_lock_two);
1031
1032/*
1033 * filemap_invalidate_unlock_two - unlock invalidate_lock for two mappings
1034 *
1035 * Unlock exclusive invalidate_lock of any passed mapping that is not NULL.
1036 *
1037 * @mapping1: the first mapping to unlock
1038 * @mapping2: the second mapping to unlock
1039 */
1040void filemap_invalidate_unlock_two(struct address_space *mapping1,
1041 struct address_space *mapping2)
1042{
1043 if (mapping1)
1044 up_write(&mapping1->invalidate_lock);
1045 if (mapping2 && mapping1 != mapping2)
1046 up_write(&mapping2->invalidate_lock);
1047}
1048EXPORT_SYMBOL(filemap_invalidate_unlock_two);
1049
1050/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 * In order to wait for pages to become available there must be
1052 * waitqueues associated with pages. By using a hash table of
1053 * waitqueues where the bucket discipline is to maintain all
1054 * waiters on the same queue and wake all when any of the pages
1055 * become available, and for the woken contexts to check to be
1056 * sure the appropriate page became available, this saves space
1057 * at a cost of "thundering herd" phenomena during rare hash
1058 * collisions.
1059 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001060#define PAGE_WAIT_TABLE_BITS 8
1061#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
1062static wait_queue_head_t page_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
1063
1064static wait_queue_head_t *page_waitqueue(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Nicholas Piggin62906022016-12-25 13:00:30 +10001066 return &page_wait_table[hash_ptr(page, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
Nicholas Piggin62906022016-12-25 13:00:30 +10001068
1069void __init pagecache_init(void)
1070{
1071 int i;
1072
1073 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
1074 init_waitqueue_head(&page_wait_table[i]);
1075
1076 page_writeback_init();
1077}
1078
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001079/*
1080 * The page wait code treats the "wait->flags" somewhat unusually, because
Linus Torvalds5868ec22020-09-20 10:38:47 -07001081 * we have multiple different kinds of waits, not just the usual "exclusive"
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001082 * one.
1083 *
1084 * We have:
1085 *
1086 * (a) no special bits set:
1087 *
1088 * We're just waiting for the bit to be released, and when a waker
1089 * calls the wakeup function, we set WQ_FLAG_WOKEN and wake it up,
1090 * and remove it from the wait queue.
1091 *
1092 * Simple and straightforward.
1093 *
1094 * (b) WQ_FLAG_EXCLUSIVE:
1095 *
1096 * The waiter is waiting to get the lock, and only one waiter should
1097 * be woken up to avoid any thundering herd behavior. We'll set the
1098 * WQ_FLAG_WOKEN bit, wake it up, and remove it from the wait queue.
1099 *
1100 * This is the traditional exclusive wait.
1101 *
Linus Torvalds5868ec22020-09-20 10:38:47 -07001102 * (c) WQ_FLAG_EXCLUSIVE | WQ_FLAG_CUSTOM:
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001103 *
1104 * The waiter is waiting to get the bit, and additionally wants the
1105 * lock to be transferred to it for fair lock behavior. If the lock
1106 * cannot be taken, we stop walking the wait queue without waking
1107 * the waiter.
1108 *
1109 * This is the "fair lock handoff" case, and in addition to setting
1110 * WQ_FLAG_WOKEN, we set WQ_FLAG_DONE to let the waiter easily see
1111 * that it now has the lock.
1112 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001113static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
Nicholas Piggin62906022016-12-25 13:00:30 +10001114{
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001115 unsigned int flags;
Nicholas Piggin62906022016-12-25 13:00:30 +10001116 struct wait_page_key *key = arg;
1117 struct wait_page_queue *wait_page
1118 = container_of(wait, struct wait_page_queue, wait);
1119
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07001120 if (!wake_page_match(wait_page, key))
Nicholas Piggin62906022016-12-25 13:00:30 +10001121 return 0;
Linus Torvalds3510ca22017-08-27 13:55:12 -07001122
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001123 /*
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001124 * If it's a lock handoff wait, we get the bit for it, and
1125 * stop walking (and do not wake it up) if we can't.
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001126 */
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001127 flags = wait->flags;
1128 if (flags & WQ_FLAG_EXCLUSIVE) {
1129 if (test_bit(key->bit_nr, &key->page->flags))
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001130 return -1;
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001131 if (flags & WQ_FLAG_CUSTOM) {
1132 if (test_and_set_bit(key->bit_nr, &key->page->flags))
1133 return -1;
1134 flags |= WQ_FLAG_DONE;
1135 }
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001136 }
Nicholas Piggin62906022016-12-25 13:00:30 +10001137
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001138 /*
1139 * We are holding the wait-queue lock, but the waiter that
1140 * is waiting for this will be checking the flags without
1141 * any locking.
1142 *
1143 * So update the flags atomically, and wake up the waiter
1144 * afterwards to avoid any races. This store-release pairs
1145 * with the load-acquire in wait_on_page_bit_common().
1146 */
1147 smp_store_release(&wait->flags, flags | WQ_FLAG_WOKEN);
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001148 wake_up_state(wait->private, mode);
1149
1150 /*
1151 * Ok, we have successfully done what we're waiting for,
1152 * and we can unconditionally remove the wait entry.
1153 *
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001154 * Note that this pairs with the "finish_wait()" in the
1155 * waiter, and has to be the absolute last thing we do.
1156 * After this list_del_init(&wait->entry) the wait entry
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001157 * might be de-allocated and the process might even have
1158 * exited.
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001159 */
Linus Torvaldsc6fe44d2020-07-23 12:33:41 -07001160 list_del_init_careful(&wait->entry);
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001161 return (flags & WQ_FLAG_EXCLUSIVE) != 0;
Nicholas Piggin62906022016-12-25 13:00:30 +10001162}
1163
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001164static void wake_up_page_bit(struct page *page, int bit_nr)
Nicholas Piggin62906022016-12-25 13:00:30 +10001165{
1166 wait_queue_head_t *q = page_waitqueue(page);
1167 struct wait_page_key key;
1168 unsigned long flags;
Tim Chen11a19c72017-08-25 09:13:55 -07001169 wait_queue_entry_t bookmark;
Nicholas Piggin62906022016-12-25 13:00:30 +10001170
1171 key.page = page;
1172 key.bit_nr = bit_nr;
1173 key.page_match = 0;
1174
Tim Chen11a19c72017-08-25 09:13:55 -07001175 bookmark.flags = 0;
1176 bookmark.private = NULL;
1177 bookmark.func = NULL;
1178 INIT_LIST_HEAD(&bookmark.entry);
1179
Nicholas Piggin62906022016-12-25 13:00:30 +10001180 spin_lock_irqsave(&q->lock, flags);
Tim Chen11a19c72017-08-25 09:13:55 -07001181 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1182
1183 while (bookmark.flags & WQ_FLAG_BOOKMARK) {
1184 /*
1185 * Take a breather from holding the lock,
1186 * allow pages that finish wake up asynchronously
1187 * to acquire the lock and remove themselves
1188 * from wait queue
1189 */
1190 spin_unlock_irqrestore(&q->lock, flags);
1191 cpu_relax();
1192 spin_lock_irqsave(&q->lock, flags);
1193 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1194 }
1195
Nicholas Piggin62906022016-12-25 13:00:30 +10001196 /*
1197 * It is possible for other pages to have collided on the waitqueue
1198 * hash, so in that case check for a page match. That prevents a long-
1199 * term waiter
1200 *
1201 * It is still possible to miss a case here, when we woke page waiters
1202 * and removed them from the waitqueue, but there are still other
1203 * page waiters.
1204 */
1205 if (!waitqueue_active(q) || !key.page_match) {
1206 ClearPageWaiters(page);
1207 /*
1208 * It's possible to miss clearing Waiters here, when we woke
1209 * our page waiters, but the hashed waitqueue has waiters for
1210 * other pages on it.
1211 *
1212 * That's okay, it's a rare case. The next waker will clear it.
1213 */
1214 }
1215 spin_unlock_irqrestore(&q->lock, flags);
1216}
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001217
1218static void wake_up_page(struct page *page, int bit)
1219{
1220 if (!PageWaiters(page))
1221 return;
1222 wake_up_page_bit(page, bit);
1223}
Nicholas Piggin62906022016-12-25 13:00:30 +10001224
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001225/*
1226 * A choice of three behaviors for wait_on_page_bit_common():
1227 */
1228enum behavior {
1229 EXCLUSIVE, /* Hold ref to page and take the bit when woken, like
1230 * __lock_page() waiting on then setting PG_locked.
1231 */
1232 SHARED, /* Hold ref to page and check the bit when woken, like
1233 * wait_on_page_writeback() waiting on PG_writeback.
1234 */
1235 DROP, /* Drop ref to page before wait, no check when woken,
1236 * like put_and_wait_on_page_locked() on PG_locked.
1237 */
1238};
1239
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001240/*
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001241 * Attempt to check (or get) the page bit, and mark us done
1242 * if successful.
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001243 */
1244static inline bool trylock_page_bit_common(struct page *page, int bit_nr,
1245 struct wait_queue_entry *wait)
1246{
1247 if (wait->flags & WQ_FLAG_EXCLUSIVE) {
1248 if (test_and_set_bit(bit_nr, &page->flags))
1249 return false;
1250 } else if (test_bit(bit_nr, &page->flags))
1251 return false;
1252
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001253 wait->flags |= WQ_FLAG_WOKEN | WQ_FLAG_DONE;
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001254 return true;
1255}
1256
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001257/* How many times do we accept lock stealing from under a waiter? */
1258int sysctl_page_lock_unfairness = 5;
1259
Nicholas Piggin62906022016-12-25 13:00:30 +10001260static inline int wait_on_page_bit_common(wait_queue_head_t *q,
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001261 struct page *page, int bit_nr, int state, enum behavior behavior)
Nicholas Piggin62906022016-12-25 13:00:30 +10001262{
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001263 int unfairness = sysctl_page_lock_unfairness;
Nicholas Piggin62906022016-12-25 13:00:30 +10001264 struct wait_page_queue wait_page;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001265 wait_queue_entry_t *wait = &wait_page.wait;
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001266 bool thrashing = false;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001267 bool delayacct = false;
Johannes Weinereb414682018-10-26 15:06:27 -07001268 unsigned long pflags;
Nicholas Piggin62906022016-12-25 13:00:30 +10001269
Johannes Weinereb414682018-10-26 15:06:27 -07001270 if (bit_nr == PG_locked &&
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001271 !PageUptodate(page) && PageWorkingset(page)) {
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001272 if (!PageSwapBacked(page)) {
Johannes Weinereb414682018-10-26 15:06:27 -07001273 delayacct_thrashing_start();
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001274 delayacct = true;
1275 }
Johannes Weinereb414682018-10-26 15:06:27 -07001276 psi_memstall_enter(&pflags);
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001277 thrashing = true;
1278 }
1279
Nicholas Piggin62906022016-12-25 13:00:30 +10001280 init_wait(wait);
1281 wait->func = wake_page_function;
1282 wait_page.page = page;
1283 wait_page.bit_nr = bit_nr;
1284
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001285repeat:
1286 wait->flags = 0;
1287 if (behavior == EXCLUSIVE) {
1288 wait->flags = WQ_FLAG_EXCLUSIVE;
1289 if (--unfairness < 0)
1290 wait->flags |= WQ_FLAG_CUSTOM;
1291 }
1292
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001293 /*
1294 * Do one last check whether we can get the
1295 * page bit synchronously.
1296 *
1297 * Do the SetPageWaiters() marking before that
1298 * to let any waker we _just_ missed know they
1299 * need to wake us up (otherwise they'll never
1300 * even go to the slow case that looks at the
1301 * page queue), and add ourselves to the wait
1302 * queue if we need to sleep.
1303 *
1304 * This part needs to be done under the queue
1305 * lock to avoid races.
1306 */
1307 spin_lock_irq(&q->lock);
1308 SetPageWaiters(page);
1309 if (!trylock_page_bit_common(page, bit_nr, wait))
1310 __add_wait_queue_entry_tail(q, wait);
1311 spin_unlock_irq(&q->lock);
1312
1313 /*
1314 * From now on, all the logic will be based on
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001315 * the WQ_FLAG_WOKEN and WQ_FLAG_DONE flag, to
1316 * see whether the page bit testing has already
1317 * been done by the wake function.
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001318 *
1319 * We can drop our reference to the page.
1320 */
1321 if (behavior == DROP)
1322 put_page(page);
1323
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001324 /*
1325 * Note that until the "finish_wait()", or until
1326 * we see the WQ_FLAG_WOKEN flag, we need to
1327 * be very careful with the 'wait->flags', because
1328 * we may race with a waker that sets them.
1329 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001330 for (;;) {
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001331 unsigned int flags;
1332
Nicholas Piggin62906022016-12-25 13:00:30 +10001333 set_current_state(state);
1334
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001335 /* Loop until we've been woken or interrupted */
1336 flags = smp_load_acquire(&wait->flags);
1337 if (!(flags & WQ_FLAG_WOKEN)) {
1338 if (signal_pending_state(state, current))
1339 break;
1340
1341 io_schedule();
1342 continue;
1343 }
1344
1345 /* If we were non-exclusive, we're done */
1346 if (behavior != EXCLUSIVE)
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001347 break;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001348
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001349 /* If the waker got the lock for us, we're done */
1350 if (flags & WQ_FLAG_DONE)
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001351 break;
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001352
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001353 /*
1354 * Otherwise, if we're getting the lock, we need to
1355 * try to get it ourselves.
1356 *
1357 * And if that fails, we'll have to retry this all.
1358 */
1359 if (unlikely(test_and_set_bit(bit_nr, &page->flags)))
1360 goto repeat;
1361
1362 wait->flags |= WQ_FLAG_DONE;
1363 break;
Nicholas Piggin62906022016-12-25 13:00:30 +10001364 }
1365
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001366 /*
1367 * If a signal happened, this 'finish_wait()' may remove the last
1368 * waiter from the wait-queues, but the PageWaiters bit will remain
1369 * set. That's ok. The next wakeup will take care of it, and trying
1370 * to do it here would be difficult and prone to races.
1371 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001372 finish_wait(q, wait);
1373
Johannes Weinereb414682018-10-26 15:06:27 -07001374 if (thrashing) {
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001375 if (delayacct)
Johannes Weinereb414682018-10-26 15:06:27 -07001376 delayacct_thrashing_end();
1377 psi_memstall_leave(&pflags);
1378 }
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001379
Nicholas Piggin62906022016-12-25 13:00:30 +10001380 /*
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001381 * NOTE! The wait->flags weren't stable until we've done the
1382 * 'finish_wait()', and we could have exited the loop above due
1383 * to a signal, and had a wakeup event happen after the signal
1384 * test but before the 'finish_wait()'.
1385 *
1386 * So only after the finish_wait() can we reliably determine
1387 * if we got woken up or not, so we can now figure out the final
1388 * return value based on that state without races.
1389 *
1390 * Also note that WQ_FLAG_WOKEN is sufficient for a non-exclusive
1391 * waiter, but an exclusive one requires WQ_FLAG_DONE.
Nicholas Piggin62906022016-12-25 13:00:30 +10001392 */
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001393 if (behavior == EXCLUSIVE)
1394 return wait->flags & WQ_FLAG_DONE ? 0 : -EINTR;
Nicholas Piggin62906022016-12-25 13:00:30 +10001395
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001396 return wait->flags & WQ_FLAG_WOKEN ? 0 : -EINTR;
Nicholas Piggin62906022016-12-25 13:00:30 +10001397}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Harvey Harrison920c7a52008-02-04 22:29:26 -08001399void wait_on_page_bit(struct page *page, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Nicholas Piggin62906022016-12-25 13:00:30 +10001401 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001402 wait_on_page_bit_common(q, page, bit_nr, TASK_UNINTERRUPTIBLE, SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404EXPORT_SYMBOL(wait_on_page_bit);
1405
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001406int wait_on_page_bit_killable(struct page *page, int bit_nr)
1407{
Nicholas Piggin62906022016-12-25 13:00:30 +10001408 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001409 return wait_on_page_bit_common(q, page, bit_nr, TASK_KILLABLE, SHARED);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001410}
David Howells4343d002017-11-02 15:27:52 +00001411EXPORT_SYMBOL(wait_on_page_bit_killable);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413/**
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001414 * put_and_wait_on_page_locked - Drop a reference and wait for it to be unlocked
1415 * @page: The page to wait for.
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -08001416 * @state: The sleep state (TASK_KILLABLE, TASK_UNINTERRUPTIBLE, etc).
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001417 *
1418 * The caller should hold a reference on @page. They expect the page to
1419 * become unlocked relatively soon, but do not wish to hold up migration
1420 * (for example) by holding the reference while waiting for the page to
1421 * come unlocked. After this function returns, the caller should not
1422 * dereference @page.
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -08001423 *
1424 * Return: 0 if the page was unlocked or -EINTR if interrupted by a signal.
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001425 */
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -08001426int put_and_wait_on_page_locked(struct page *page, int state)
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001427{
1428 wait_queue_head_t *q;
1429
1430 page = compound_head(page);
1431 q = page_waitqueue(page);
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -08001432 return wait_on_page_bit_common(q, page, PG_locked, state, DROP);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001433}
1434
1435/**
David Howells385e1ca5f2009-04-03 16:42:39 +01001436 * add_page_wait_queue - Add an arbitrary waiter to a page's wait queue
Randy Dunlap697f6192009-04-13 14:39:54 -07001437 * @page: Page defining the wait queue of interest
1438 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +01001439 *
1440 * Add an arbitrary @waiter to the wait queue for the nominated @page.
1441 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001442void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter)
David Howells385e1ca5f2009-04-03 16:42:39 +01001443{
1444 wait_queue_head_t *q = page_waitqueue(page);
1445 unsigned long flags;
1446
1447 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds9c3a8152017-08-28 16:45:40 -07001448 __add_wait_queue_entry_tail(q, waiter);
Nicholas Piggin62906022016-12-25 13:00:30 +10001449 SetPageWaiters(page);
David Howells385e1ca5f2009-04-03 16:42:39 +01001450 spin_unlock_irqrestore(&q->lock, flags);
1451}
1452EXPORT_SYMBOL_GPL(add_page_wait_queue);
1453
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001454#ifndef clear_bit_unlock_is_negative_byte
1455
1456/*
1457 * PG_waiters is the high bit in the same byte as PG_lock.
1458 *
1459 * On x86 (and on many other architectures), we can clear PG_lock and
1460 * test the sign bit at the same time. But if the architecture does
1461 * not support that special operation, we just do this all by hand
1462 * instead.
1463 *
1464 * The read of PG_waiters has to be after (or concurrently with) PG_locked
Ethon Paulffceeb62020-06-04 16:49:22 -07001465 * being cleared, but a memory barrier should be unnecessary since it is
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001466 * in the same byte as PG_locked.
1467 */
1468static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
1469{
1470 clear_bit_unlock(nr, mem);
1471 /* smp_mb__after_atomic(); */
Olof Johansson98473f92016-12-29 14:16:07 -08001472 return test_bit(PG_waiters, mem);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001473}
1474
1475#endif
1476
David Howells385e1ca5f2009-04-03 16:42:39 +01001477/**
Randy Dunlap485bb992006-06-23 02:03:49 -07001478 * unlock_page - unlock a locked page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 * @page: the page
1480 *
Miaohe Lin0e9aa672020-10-15 20:09:58 -07001481 * Unlocks the page and wakes up sleepers in wait_on_page_locked().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 * Also wakes sleepers in wait_on_page_writeback() because the wakeup
Masanari Iidada3dae52014-09-09 01:27:23 +09001483 * mechanism between PageLocked pages and PageWriteback pages is shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 * But that's OK - sleepers in wait_on_page_writeback() just go back to sleep.
1485 *
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001486 * Note that this depends on PG_waiters being the sign bit in the byte
1487 * that contains PG_locked - thus the BUILD_BUG_ON(). That allows us to
1488 * clear the PG_locked bit and test PG_waiters at the same time fairly
1489 * portably (architectures that do LL/SC can test any bit, while x86 can
1490 * test the sign bit).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 */
Harvey Harrison920c7a52008-02-04 22:29:26 -08001492void unlock_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001494 BUILD_BUG_ON(PG_waiters != 7);
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001495 page = compound_head(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001496 VM_BUG_ON_PAGE(!PageLocked(page), page);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001497 if (clear_bit_unlock_is_negative_byte(PG_locked, &page->flags))
1498 wake_up_page_bit(page, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500EXPORT_SYMBOL(unlock_page);
1501
Randy Dunlap485bb992006-06-23 02:03:49 -07001502/**
David Howells73e10de2020-02-10 10:00:21 +00001503 * end_page_private_2 - Clear PG_private_2 and release any waiters
1504 * @page: The page
1505 *
1506 * Clear the PG_private_2 bit on a page and wake up any sleepers waiting for
1507 * this. The page ref held for PG_private_2 being set is released.
1508 *
1509 * This is, for example, used when a netfs page is being written to a local
1510 * disk cache, thereby allowing writes to the cache for the same page to be
1511 * serialised.
1512 */
1513void end_page_private_2(struct page *page)
1514{
1515 page = compound_head(page);
1516 VM_BUG_ON_PAGE(!PagePrivate2(page), page);
1517 clear_bit_unlock(PG_private_2, &page->flags);
1518 wake_up_page_bit(page, PG_private_2);
1519 put_page(page);
1520}
1521EXPORT_SYMBOL(end_page_private_2);
1522
1523/**
1524 * wait_on_page_private_2 - Wait for PG_private_2 to be cleared on a page
1525 * @page: The page to wait on
1526 *
1527 * Wait for PG_private_2 (aka PG_fscache) to be cleared on a page.
1528 */
1529void wait_on_page_private_2(struct page *page)
1530{
1531 page = compound_head(page);
1532 while (PagePrivate2(page))
1533 wait_on_page_bit(page, PG_private_2);
1534}
1535EXPORT_SYMBOL(wait_on_page_private_2);
1536
1537/**
1538 * wait_on_page_private_2_killable - Wait for PG_private_2 to be cleared on a page
1539 * @page: The page to wait on
1540 *
1541 * Wait for PG_private_2 (aka PG_fscache) to be cleared on a page or until a
1542 * fatal signal is received by the calling task.
1543 *
1544 * Return:
1545 * - 0 if successful.
1546 * - -EINTR if a fatal signal was encountered.
1547 */
1548int wait_on_page_private_2_killable(struct page *page)
1549{
1550 int ret = 0;
1551
1552 page = compound_head(page);
1553 while (PagePrivate2(page)) {
1554 ret = wait_on_page_bit_killable(page, PG_private_2);
1555 if (ret < 0)
1556 break;
1557 }
1558
1559 return ret;
1560}
1561EXPORT_SYMBOL(wait_on_page_private_2_killable);
1562
1563/**
Randy Dunlap485bb992006-06-23 02:03:49 -07001564 * end_page_writeback - end writeback against a page
1565 * @page: the page
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 */
1567void end_page_writeback(struct page *page)
1568{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001569 /*
1570 * TestClearPageReclaim could be used here but it is an atomic
1571 * operation and overkill in this particular case. Failing to
1572 * shuffle a page marked for immediate reclaim is too mild to
1573 * justify taking an atomic operation penalty at the end of
1574 * ever page writeback.
1575 */
1576 if (PageReclaim(page)) {
1577 ClearPageReclaim(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001578 rotate_reclaimable_page(page);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001579 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001580
Hugh Dickins073861e2020-11-24 08:46:43 -08001581 /*
1582 * Writeback does not hold a page reference of its own, relying
1583 * on truncation to wait for the clearing of PG_writeback.
1584 * But here we must make sure that the page is not freed and
1585 * reused before the wake_up_page().
1586 */
1587 get_page(page);
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001588 if (!test_clear_page_writeback(page))
1589 BUG();
1590
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001591 smp_mb__after_atomic();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 wake_up_page(page, PG_writeback);
Hugh Dickins073861e2020-11-24 08:46:43 -08001593 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595EXPORT_SYMBOL(end_page_writeback);
1596
Matthew Wilcox57d99842014-06-04 16:07:45 -07001597/*
1598 * After completing I/O on a page, call this routine to update the page
1599 * flags appropriately
1600 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001601void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001602{
Jens Axboec11f0c02016-08-05 08:11:04 -06001603 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001604 if (!err) {
1605 SetPageUptodate(page);
1606 } else {
1607 ClearPageUptodate(page);
1608 SetPageError(page);
1609 }
1610 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001611 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001612 if (err) {
Minchan Kimdd8416c2017-02-24 14:59:59 -08001613 struct address_space *mapping;
1614
Matthew Wilcox57d99842014-06-04 16:07:45 -07001615 SetPageError(page);
Minchan Kimdd8416c2017-02-24 14:59:59 -08001616 mapping = page_mapping(page);
1617 if (mapping)
1618 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001619 }
1620 end_page_writeback(page);
1621 }
1622}
1623EXPORT_SYMBOL_GPL(page_endio);
1624
Randy Dunlap485bb992006-06-23 02:03:49 -07001625/**
1626 * __lock_page - get a lock on the page, assuming we need to sleep to get it
Randy Dunlap87066752017-02-22 15:44:44 -08001627 * @__page: the page to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001629void __lock_page(struct page *__page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Nicholas Piggin62906022016-12-25 13:00:30 +10001631 struct page *page = compound_head(__page);
1632 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001633 wait_on_page_bit_common(q, page, PG_locked, TASK_UNINTERRUPTIBLE,
1634 EXCLUSIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636EXPORT_SYMBOL(__lock_page);
1637
Nicholas Piggin62906022016-12-25 13:00:30 +10001638int __lock_page_killable(struct page *__page)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001639{
Nicholas Piggin62906022016-12-25 13:00:30 +10001640 struct page *page = compound_head(__page);
1641 wait_queue_head_t *q = page_waitqueue(page);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001642 return wait_on_page_bit_common(q, page, PG_locked, TASK_KILLABLE,
1643 EXCLUSIVE);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001644}
Evgeniy Polyakov18bc0bb2009-02-09 17:02:42 +03001645EXPORT_SYMBOL_GPL(__lock_page_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001646
Jens Axboedd3e6d52020-05-22 09:12:09 -06001647int __lock_page_async(struct page *page, struct wait_page_queue *wait)
1648{
Matthew Wilcox (Oracle)f32b5dd2021-02-24 12:02:09 -08001649 struct wait_queue_head *q = page_waitqueue(page);
1650 int ret = 0;
1651
1652 wait->page = page;
1653 wait->bit_nr = PG_locked;
1654
1655 spin_lock_irq(&q->lock);
1656 __add_wait_queue_entry_tail(q, &wait->wait);
1657 SetPageWaiters(page);
1658 ret = !trylock_page(page);
1659 /*
1660 * If we were successful now, we know we're still on the
1661 * waitqueue as we're still under the lock. This means it's
1662 * safe to remove and return success, we know the callback
1663 * isn't going to trigger.
1664 */
1665 if (!ret)
1666 __remove_wait_queue(q, &wait->wait);
1667 else
1668 ret = -EIOCBQUEUED;
1669 spin_unlock_irq(&q->lock);
1670 return ret;
Jens Axboedd3e6d52020-05-22 09:12:09 -06001671}
1672
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001673/*
1674 * Return values:
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001675 * 1 - page is locked; mmap_lock is still held.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001676 * 0 - page is not locked.
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001677 * mmap_lock has been released (mmap_read_unlock(), unless flags had both
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001678 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001679 * which case mmap_lock is still held.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001680 *
1681 * If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001682 * with the page locked and the mmap_lock unperturbed.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001683 */
Michel Lespinassed065bd82010-10-26 14:21:57 -07001684int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
1685 unsigned int flags)
1686{
Peter Xu4064b982020-04-01 21:08:45 -07001687 if (fault_flag_allow_retry_first(flags)) {
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001688 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001689 * CAUTION! In this case, mmap_lock is not released
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001690 * even though return 0.
1691 */
1692 if (flags & FAULT_FLAG_RETRY_NOWAIT)
1693 return 0;
1694
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001695 mmap_read_unlock(mm);
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001696 if (flags & FAULT_FLAG_KILLABLE)
1697 wait_on_page_locked_killable(page);
1698 else
Gleb Natapov318b2752011-03-22 16:30:51 -07001699 wait_on_page_locked(page);
Michel Lespinassed065bd82010-10-26 14:21:57 -07001700 return 0;
1701 }
Hailong Liu800bca72020-12-14 19:05:02 -08001702 if (flags & FAULT_FLAG_KILLABLE) {
1703 int ret;
1704
1705 ret = __lock_page_killable(page);
1706 if (ret) {
1707 mmap_read_unlock(mm);
1708 return 0;
1709 }
1710 } else {
1711 __lock_page(page);
1712 }
1713 return 1;
1714
Michel Lespinassed065bd82010-10-26 14:21:57 -07001715}
1716
Randy Dunlap485bb992006-06-23 02:03:49 -07001717/**
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001718 * page_cache_next_miss() - Find the next gap in the page cache.
1719 * @mapping: Mapping.
1720 * @index: Index.
1721 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001722 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001723 * Search the range [index, min(index + max_scan - 1, ULONG_MAX)] for the
1724 * gap with the lowest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001725 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001726 * This function may be called under the rcu_read_lock. However, this will
1727 * not atomically search a snapshot of the cache at a single point in time.
1728 * For example, if a gap is created at index 5, then subsequently a gap is
1729 * created at index 10, page_cache_next_miss covering both indices may
1730 * return 10 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001731 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001732 * Return: The index of the gap if found, otherwise an index outside the
1733 * range specified (in which case 'return - index >= max_scan' will be true).
1734 * In the rare case of index wrap-around, 0 will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001735 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001736pgoff_t page_cache_next_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001737 pgoff_t index, unsigned long max_scan)
1738{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001739 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001740
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001741 while (max_scan--) {
1742 void *entry = xas_next(&xas);
1743 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001744 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001745 if (xas.xa_index == 0)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001746 break;
1747 }
1748
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001749 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001750}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001751EXPORT_SYMBOL(page_cache_next_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001752
1753/**
Laurent Dufour2346a562019-05-13 17:21:29 -07001754 * page_cache_prev_miss() - Find the previous gap in the page cache.
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001755 * @mapping: Mapping.
1756 * @index: Index.
1757 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001758 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001759 * Search the range [max(index - max_scan + 1, 0), index] for the
1760 * gap with the highest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001761 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001762 * This function may be called under the rcu_read_lock. However, this will
1763 * not atomically search a snapshot of the cache at a single point in time.
1764 * For example, if a gap is created at index 10, then subsequently a gap is
1765 * created at index 5, page_cache_prev_miss() covering both indices may
1766 * return 5 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001767 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001768 * Return: The index of the gap if found, otherwise an index outside the
1769 * range specified (in which case 'index - return >= max_scan' will be true).
1770 * In the rare case of wrap-around, ULONG_MAX will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001771 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001772pgoff_t page_cache_prev_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001773 pgoff_t index, unsigned long max_scan)
1774{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001775 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001776
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001777 while (max_scan--) {
1778 void *entry = xas_prev(&xas);
1779 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001780 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001781 if (xas.xa_index == ULONG_MAX)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001782 break;
1783 }
1784
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001785 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001786}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001787EXPORT_SYMBOL(page_cache_prev_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001788
Matthew Wilcox (Oracle)44835d22021-02-25 17:15:36 -08001789/*
Matthew Wilcox (Oracle)bc5a3012021-02-25 17:15:40 -08001790 * mapping_get_entry - Get a page cache entry.
Randy Dunlap485bb992006-06-23 02:03:49 -07001791 * @mapping: the address_space to search
Matthew Wilcox (Oracle)a6de4b42020-10-13 16:51:34 -07001792 * @index: The page cache index.
Randy Dunlap485bb992006-06-23 02:03:49 -07001793 *
Rui Sun4b17f032021-04-29 22:55:38 -07001794 * Looks up the page cache slot at @mapping & @index. If there is a
Matthew Wilcox (Oracle)a6de4b42020-10-13 16:51:34 -07001795 * page cache page, the head page is returned with an increased refcount.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001796 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07001797 * If the slot holds a shadow entry of a previously evicted page, or a
1798 * swap entry from shmem/tmpfs, it is returned.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001799 *
Matthew Wilcox (Oracle)a6de4b42020-10-13 16:51:34 -07001800 * Return: The head page or shadow entry, %NULL if nothing is found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 */
Matthew Wilcox (Oracle)bc5a3012021-02-25 17:15:40 -08001802static struct page *mapping_get_entry(struct address_space *mapping,
1803 pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Matthew Wilcox (Oracle)a6de4b42020-10-13 16:51:34 -07001805 XA_STATE(xas, &mapping->i_pages, index);
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07001806 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Nick Piggina60637c2008-07-25 19:45:31 -07001808 rcu_read_lock();
1809repeat:
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001810 xas_reset(&xas);
1811 page = xas_load(&xas);
1812 if (xas_retry(&xas, page))
1813 goto repeat;
1814 /*
1815 * A shadow entry of a recently evicted page, or a swap entry from
1816 * shmem/tmpfs. Return it without attempting to raise page count.
1817 */
1818 if (!page || xa_is_value(page))
1819 goto out;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001820
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07001821 if (!page_cache_get_speculative(page))
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001822 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001823
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001824 /*
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07001825 * Has the page moved or been split?
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001826 * This is part of the lockless pagecache protocol. See
1827 * include/linux/pagemap.h for details.
1828 */
1829 if (unlikely(page != xas_reload(&xas))) {
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07001830 put_page(page);
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001831 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001832 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001833out:
Nick Piggina60637c2008-07-25 19:45:31 -07001834 rcu_read_unlock();
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return page;
1837}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Randy Dunlap485bb992006-06-23 02:03:49 -07001839/**
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001840 * pagecache_get_page - Find and get a reference to a page.
1841 * @mapping: The address_space to search.
1842 * @index: The page index.
1843 * @fgp_flags: %FGP flags modify how the page is returned.
1844 * @gfp_mask: Memory allocation flags to use if %FGP_CREAT is specified.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001845 *
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001846 * Looks up the page cache entry at @mapping & @index.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001847 *
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001848 * @fgp_flags can be zero or more of these flags:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001849 *
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001850 * * %FGP_ACCESSED - The page will be marked accessed.
1851 * * %FGP_LOCK - The page is returned locked.
Matthew Wilcox (Oracle)a8cf7f22020-10-13 16:51:41 -07001852 * * %FGP_HEAD - If the page is present and a THP, return the head page
1853 * rather than the exact page specified by the index.
Matthew Wilcox (Oracle)44835d22021-02-25 17:15:36 -08001854 * * %FGP_ENTRY - If there is a shadow / swap / DAX entry, return it
1855 * instead of allocating a new page to replace it.
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001856 * * %FGP_CREAT - If no page is present then a new page is allocated using
1857 * @gfp_mask and added to the page cache and the VM's LRU list.
1858 * The page is returned locked and with an increased refcount.
1859 * * %FGP_FOR_MMAP - The caller wants to do its own locking dance if the
1860 * page is already in cache. If the page was allocated, unlock it before
1861 * returning so the caller can do the same dance.
Yang Shi605cad82020-08-06 23:19:58 -07001862 * * %FGP_WRITE - The page will be written
1863 * * %FGP_NOFS - __GFP_FS will get cleared in gfp mask
1864 * * %FGP_NOWAIT - Don't get blocked by page lock
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03001865 *
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001866 * If %FGP_LOCK or %FGP_CREAT are specified then the function may sleep even
1867 * if the %GFP flags specified for %FGP_CREAT are atomic.
Mel Gorman2457aec2014-06-04 16:10:31 -07001868 *
1869 * If there is a page cache page, it is returned with an increased refcount.
Mike Rapoporta862f682019-03-05 15:48:42 -08001870 *
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001871 * Return: The found page or %NULL otherwise.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001872 */
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001873struct page *pagecache_get_page(struct address_space *mapping, pgoff_t index,
1874 int fgp_flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
Nick Piggineb2be182007-10-16 01:24:57 -07001876 struct page *page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878repeat:
Matthew Wilcox (Oracle)bc5a3012021-02-25 17:15:40 -08001879 page = mapping_get_entry(mapping, index);
Matthew Wilcox (Oracle)44835d22021-02-25 17:15:36 -08001880 if (xa_is_value(page)) {
1881 if (fgp_flags & FGP_ENTRY)
1882 return page;
Mel Gorman2457aec2014-06-04 16:10:31 -07001883 page = NULL;
Matthew Wilcox (Oracle)44835d22021-02-25 17:15:36 -08001884 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001885 if (!page)
1886 goto no_page;
1887
1888 if (fgp_flags & FGP_LOCK) {
1889 if (fgp_flags & FGP_NOWAIT) {
1890 if (!trylock_page(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001891 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001892 return NULL;
1893 }
1894 } else {
1895 lock_page(page);
1896 }
1897
1898 /* Has the page been truncated? */
Matthew Wilcox (Oracle)a8cf7f22020-10-13 16:51:41 -07001899 if (unlikely(page->mapping != mapping)) {
Mel Gorman2457aec2014-06-04 16:10:31 -07001900 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001901 put_page(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001902 goto repeat;
1903 }
Matthew Wilcox (Oracle)a8cf7f22020-10-13 16:51:41 -07001904 VM_BUG_ON_PAGE(!thp_contains(page, index), page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001905 }
1906
Kirill Tkhaic16eb002018-12-28 00:37:35 -08001907 if (fgp_flags & FGP_ACCESSED)
Mel Gorman2457aec2014-06-04 16:10:31 -07001908 mark_page_accessed(page);
Yang Shib9306a72020-08-06 23:19:55 -07001909 else if (fgp_flags & FGP_WRITE) {
1910 /* Clear idle flag for buffer write */
1911 if (page_is_idle(page))
1912 clear_page_idle(page);
1913 }
Matthew Wilcox (Oracle)a8cf7f22020-10-13 16:51:41 -07001914 if (!(fgp_flags & FGP_HEAD))
1915 page = find_subpage(page, index);
Mel Gorman2457aec2014-06-04 16:10:31 -07001916
1917no_page:
1918 if (!page && (fgp_flags & FGP_CREAT)) {
1919 int err;
Christoph Hellwigf56753a2020-09-24 08:51:40 +02001920 if ((fgp_flags & FGP_WRITE) && mapping_can_writeback(mapping))
Michal Hocko45f87de2014-12-29 20:30:35 +01001921 gfp_mask |= __GFP_WRITE;
1922 if (fgp_flags & FGP_NOFS)
1923 gfp_mask &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001924
Michal Hocko45f87de2014-12-29 20:30:35 +01001925 page = __page_cache_alloc(gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001926 if (!page)
1927 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001928
Josef Bacika75d4c32019-03-13 11:44:14 -07001929 if (WARN_ON_ONCE(!(fgp_flags & (FGP_LOCK | FGP_FOR_MMAP))))
Mel Gorman2457aec2014-06-04 16:10:31 -07001930 fgp_flags |= FGP_LOCK;
1931
Hugh Dickinseb39d612014-08-06 16:06:43 -07001932 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001933 if (fgp_flags & FGP_ACCESSED)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001934 __SetPageReferenced(page);
Mel Gorman2457aec2014-06-04 16:10:31 -07001935
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001936 err = add_to_page_cache_lru(page, mapping, index, gfp_mask);
Nick Piggineb2be182007-10-16 01:24:57 -07001937 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001938 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07001939 page = NULL;
1940 if (err == -EEXIST)
1941 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
Josef Bacika75d4c32019-03-13 11:44:14 -07001943
1944 /*
1945 * add_to_page_cache_lru locks the page, and for mmap we expect
1946 * an unlocked page.
1947 */
1948 if (page && (fgp_flags & FGP_FOR_MMAP))
1949 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return page;
1953}
Mel Gorman2457aec2014-06-04 16:10:31 -07001954EXPORT_SYMBOL(pagecache_get_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08001956static inline struct page *find_get_entry(struct xa_state *xas, pgoff_t max,
1957 xa_mark_t mark)
1958{
1959 struct page *page;
1960
1961retry:
1962 if (mark == XA_PRESENT)
1963 page = xas_find(xas, max);
1964 else
1965 page = xas_find_marked(xas, max, mark);
1966
1967 if (xas_retry(xas, page))
1968 goto retry;
1969 /*
1970 * A shadow entry of a recently evicted page, a swap
1971 * entry from shmem/tmpfs or a DAX entry. Return it
1972 * without attempting to raise page count.
1973 */
1974 if (!page || xa_is_value(page))
1975 return page;
1976
1977 if (!page_cache_get_speculative(page))
1978 goto reset;
1979
1980 /* Has the page moved or been split? */
1981 if (unlikely(page != xas_reload(xas))) {
1982 put_page(page);
1983 goto reset;
1984 }
1985
1986 return page;
1987reset:
1988 xas_reset(xas);
1989 goto retry;
1990}
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001993 * find_get_entries - gang pagecache lookup
1994 * @mapping: The address_space to search
1995 * @start: The starting page cache index
Matthew Wilcox (Oracle)ca122fe42021-02-25 17:16:00 -08001996 * @end: The final page index (inclusive).
Matthew Wilcox (Oracle)cf2039a2021-02-25 17:16:11 -08001997 * @pvec: Where the resulting entries are placed.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001998 * @indices: The cache indices corresponding to the entries in @entries
1999 *
Matthew Wilcox (Oracle)cf2039a2021-02-25 17:16:11 -08002000 * find_get_entries() will search for and return a batch of entries in
2001 * the mapping. The entries are placed in @pvec. find_get_entries()
2002 * takes a reference on any actual pages it returns.
Johannes Weiner0cd61442014-04-03 14:47:46 -07002003 *
2004 * The search returns a group of mapping-contiguous page cache entries
2005 * with ascending indexes. There may be holes in the indices due to
2006 * not-present pages.
2007 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07002008 * Any shadow entries of evicted pages, or swap entries from
2009 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07002010 *
Hugh Dickins71725ed2020-04-06 20:07:57 -07002011 * If it finds a Transparent Huge Page, head or tail, find_get_entries()
2012 * stops at that page: the caller is likely to have a better way to handle
2013 * the compound page as a whole, and then skip its extent, than repeatedly
2014 * calling find_get_entries() to return all its tails.
2015 *
Mike Rapoporta862f682019-03-05 15:48:42 -08002016 * Return: the number of pages and shadow entries which were found.
Johannes Weiner0cd61442014-04-03 14:47:46 -07002017 */
Matthew Wilcox (Oracle)ca122fe42021-02-25 17:16:00 -08002018unsigned find_get_entries(struct address_space *mapping, pgoff_t start,
Matthew Wilcox (Oracle)cf2039a2021-02-25 17:16:11 -08002019 pgoff_t end, struct pagevec *pvec, pgoff_t *indices)
Johannes Weiner0cd61442014-04-03 14:47:46 -07002020{
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04002021 XA_STATE(xas, &mapping->i_pages, start);
2022 struct page *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002023 unsigned int ret = 0;
Matthew Wilcox (Oracle)cf2039a2021-02-25 17:16:11 -08002024 unsigned nr_entries = PAGEVEC_SIZE;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002025
2026 rcu_read_lock();
Matthew Wilcox (Oracle)ca122fe42021-02-25 17:16:00 -08002027 while ((page = find_get_entry(&xas, end, XA_PRESENT))) {
Hugh Dickins71725ed2020-04-06 20:07:57 -07002028 /*
2029 * Terminate early on finding a THP, to allow the caller to
2030 * handle it all at once; but continue if this is hugetlbfs.
2031 */
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08002032 if (!xa_is_value(page) && PageTransHuge(page) &&
2033 !PageHuge(page)) {
Hugh Dickins71725ed2020-04-06 20:07:57 -07002034 page = find_subpage(page, xas.xa_index);
2035 nr_entries = ret + 1;
2036 }
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08002037
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04002038 indices[ret] = xas.xa_index;
Matthew Wilcox (Oracle)cf2039a2021-02-25 17:16:11 -08002039 pvec->pages[ret] = page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002040 if (++ret == nr_entries)
2041 break;
2042 }
2043 rcu_read_unlock();
Matthew Wilcox (Oracle)cf2039a2021-02-25 17:16:11 -08002044
2045 pvec->nr = ret;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002046 return ret;
2047}
2048
2049/**
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002050 * find_lock_entries - Find a batch of pagecache entries.
2051 * @mapping: The address_space to search.
2052 * @start: The starting page cache index.
2053 * @end: The final page index (inclusive).
2054 * @pvec: Where the resulting entries are placed.
2055 * @indices: The cache indices of the entries in @pvec.
2056 *
2057 * find_lock_entries() will return a batch of entries from @mapping.
2058 * Swap, shadow and DAX entries are included. Pages are returned
2059 * locked and with an incremented refcount. Pages which are locked by
2060 * somebody else or under writeback are skipped. Only the head page of
2061 * a THP is returned. Pages which are partially outside the range are
2062 * not returned.
2063 *
2064 * The entries have ascending indexes. The indices may not be consecutive
2065 * due to not-present entries, THP pages, pages which could not be locked
2066 * or pages under writeback.
2067 *
2068 * Return: The number of entries which were found.
2069 */
2070unsigned find_lock_entries(struct address_space *mapping, pgoff_t start,
2071 pgoff_t end, struct pagevec *pvec, pgoff_t *indices)
2072{
2073 XA_STATE(xas, &mapping->i_pages, start);
2074 struct page *page;
2075
2076 rcu_read_lock();
2077 while ((page = find_get_entry(&xas, end, XA_PRESENT))) {
2078 if (!xa_is_value(page)) {
2079 if (page->index < start)
2080 goto put;
2081 VM_BUG_ON_PAGE(page->index != xas.xa_index, page);
2082 if (page->index + thp_nr_pages(page) - 1 > end)
2083 goto put;
2084 if (!trylock_page(page))
2085 goto put;
2086 if (page->mapping != mapping || PageWriteback(page))
2087 goto unlock;
2088 VM_BUG_ON_PAGE(!thp_contains(page, xas.xa_index),
2089 page);
2090 }
2091 indices[pvec->nr] = xas.xa_index;
2092 if (!pagevec_add(pvec, page))
2093 break;
2094 goto next;
2095unlock:
2096 unlock_page(page);
2097put:
2098 put_page(page);
2099next:
Hugh Dickins2d11e732021-04-23 14:28:57 -07002100 if (!xa_is_value(page) && PageTransHuge(page)) {
2101 unsigned int nr_pages = thp_nr_pages(page);
2102
2103 /* Final THP may cross MAX_LFS_FILESIZE on 32-bit */
2104 xas_set(&xas, page->index + nr_pages);
2105 if (xas.xa_index < nr_pages)
2106 break;
2107 }
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002108 }
2109 rcu_read_unlock();
2110
2111 return pagevec_count(pvec);
2112}
2113
2114/**
Jan Karab947cee2017-09-06 16:21:21 -07002115 * find_get_pages_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 * @mapping: The address_space to search
2117 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07002118 * @end: The final page index (inclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 * @nr_pages: The maximum number of pages
2120 * @pages: Where the resulting pages are placed
2121 *
Jan Karab947cee2017-09-06 16:21:21 -07002122 * find_get_pages_range() will search for and return a group of up to @nr_pages
2123 * pages in the mapping starting at index @start and up to index @end
2124 * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
2125 * a reference against the returned pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 *
2127 * The search returns a group of mapping-contiguous pages with ascending
2128 * indexes. There may be holes in the indices due to not-present pages.
Jan Karad72dc8a2017-09-06 16:21:18 -07002129 * We also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 *
Mike Rapoporta862f682019-03-05 15:48:42 -08002131 * Return: the number of pages which were found. If this number is
2132 * smaller than @nr_pages, the end of specified range has been
Jan Karab947cee2017-09-06 16:21:21 -07002133 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 */
Jan Karab947cee2017-09-06 16:21:21 -07002135unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
2136 pgoff_t end, unsigned int nr_pages,
2137 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04002139 XA_STATE(xas, &mapping->i_pages, *start);
2140 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07002141 unsigned ret = 0;
2142
2143 if (unlikely(!nr_pages))
2144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Nick Piggina60637c2008-07-25 19:45:31 -07002146 rcu_read_lock();
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08002147 while ((page = find_get_entry(&xas, end, XA_PRESENT))) {
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04002148 /* Skip over shadow, swap and DAX entries */
2149 if (xa_is_value(page))
Hugh Dickins8079b1c2011-08-03 16:21:28 -07002150 continue;
Nick Piggina60637c2008-07-25 19:45:31 -07002151
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002152 pages[ret] = find_subpage(page, xas.xa_index);
Jan Karab947cee2017-09-06 16:21:21 -07002153 if (++ret == nr_pages) {
Yu Zhao5d3ee422019-03-05 15:49:17 -08002154 *start = xas.xa_index + 1;
Jan Karab947cee2017-09-06 16:21:21 -07002155 goto out;
2156 }
Nick Piggina60637c2008-07-25 19:45:31 -07002157 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07002158
Jan Karab947cee2017-09-06 16:21:21 -07002159 /*
2160 * We come here when there is no page beyond @end. We take care to not
2161 * overflow the index @start as it confuses some of the callers. This
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04002162 * breaks the iteration when there is a page at index -1 but that is
Jan Karab947cee2017-09-06 16:21:21 -07002163 * already broken anyway.
2164 */
2165 if (end == (pgoff_t)-1)
2166 *start = (pgoff_t)-1;
2167 else
2168 *start = end + 1;
2169out:
Nick Piggina60637c2008-07-25 19:45:31 -07002170 rcu_read_unlock();
Jan Karad72dc8a2017-09-06 16:21:18 -07002171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 return ret;
2173}
2174
Jens Axboeebf43502006-04-27 08:46:01 +02002175/**
2176 * find_get_pages_contig - gang contiguous pagecache lookup
2177 * @mapping: The address_space to search
2178 * @index: The starting page index
2179 * @nr_pages: The maximum number of pages
2180 * @pages: Where the resulting pages are placed
2181 *
2182 * find_get_pages_contig() works exactly like find_get_pages(), except
2183 * that the returned number of pages are guaranteed to be contiguous.
2184 *
Mike Rapoporta862f682019-03-05 15:48:42 -08002185 * Return: the number of pages which were found.
Jens Axboeebf43502006-04-27 08:46:01 +02002186 */
2187unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
2188 unsigned int nr_pages, struct page **pages)
2189{
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002190 XA_STATE(xas, &mapping->i_pages, index);
2191 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07002192 unsigned int ret = 0;
2193
2194 if (unlikely(!nr_pages))
2195 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02002196
Nick Piggina60637c2008-07-25 19:45:31 -07002197 rcu_read_lock();
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002198 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002199 if (xas_retry(&xas, page))
2200 continue;
2201 /*
2202 * If the entry has been swapped out, we can stop looking.
2203 * No current caller is looking for DAX entries.
2204 */
2205 if (xa_is_value(page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07002206 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07002207
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002208 if (!page_cache_get_speculative(page))
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002209 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07002210
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002211 /* Has the page moved or been split? */
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002212 if (unlikely(page != xas_reload(&xas)))
2213 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07002214
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002215 pages[ret] = find_subpage(page, xas.xa_index);
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07002216 if (++ret == nr_pages)
2217 break;
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002218 continue;
2219put_page:
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002220 put_page(page);
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002221retry:
2222 xas_reset(&xas);
Jens Axboeebf43502006-04-27 08:46:01 +02002223 }
Nick Piggina60637c2008-07-25 19:45:31 -07002224 rcu_read_unlock();
2225 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02002226}
David Howellsef71c152007-05-09 02:33:44 -07002227EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02002228
Randy Dunlap485bb992006-06-23 02:03:49 -07002229/**
Matthew Wilcox (Oracle)c49f50d2021-02-25 17:15:25 -08002230 * find_get_pages_range_tag - Find and return head pages matching @tag.
Randy Dunlap485bb992006-06-23 02:03:49 -07002231 * @mapping: the address_space to search
2232 * @index: the starting page index
Jan Kara72b045a2017-11-15 17:34:33 -08002233 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07002234 * @tag: the tag index
2235 * @nr_pages: the maximum number of pages
2236 * @pages: where the resulting pages are placed
2237 *
Matthew Wilcox (Oracle)c49f50d2021-02-25 17:15:25 -08002238 * Like find_get_pages(), except we only return head pages which are tagged
2239 * with @tag. @index is updated to the index immediately after the last
2240 * page we return, ready for the next iteration.
Mike Rapoporta862f682019-03-05 15:48:42 -08002241 *
2242 * Return: the number of pages which were found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 */
Jan Kara72b045a2017-11-15 17:34:33 -08002244unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
Matthew Wilcoxa6906972018-05-16 18:12:54 -04002245 pgoff_t end, xa_mark_t tag, unsigned int nr_pages,
Jan Kara72b045a2017-11-15 17:34:33 -08002246 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
Matthew Wilcoxa6906972018-05-16 18:12:54 -04002248 XA_STATE(xas, &mapping->i_pages, *index);
2249 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07002250 unsigned ret = 0;
2251
2252 if (unlikely(!nr_pages))
2253 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Nick Piggina60637c2008-07-25 19:45:31 -07002255 rcu_read_lock();
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08002256 while ((page = find_get_entry(&xas, end, tag))) {
Matthew Wilcoxa6906972018-05-16 18:12:54 -04002257 /*
2258 * Shadow entries should never be tagged, but this iteration
2259 * is lockless so there is a window for page reclaim to evict
2260 * a page we saw tagged. Skip over it.
2261 */
2262 if (xa_is_value(page))
Johannes Weiner139b6a62014-05-06 12:50:05 -07002263 continue;
Nick Piggina60637c2008-07-25 19:45:31 -07002264
Matthew Wilcox (Oracle)c49f50d2021-02-25 17:15:25 -08002265 pages[ret] = page;
Jan Kara72b045a2017-11-15 17:34:33 -08002266 if (++ret == nr_pages) {
Matthew Wilcox (Oracle)c49f50d2021-02-25 17:15:25 -08002267 *index = page->index + thp_nr_pages(page);
Jan Kara72b045a2017-11-15 17:34:33 -08002268 goto out;
2269 }
Nick Piggina60637c2008-07-25 19:45:31 -07002270 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07002271
Jan Kara72b045a2017-11-15 17:34:33 -08002272 /*
Matthew Wilcoxa6906972018-05-16 18:12:54 -04002273 * We come here when we got to @end. We take care to not overflow the
Jan Kara72b045a2017-11-15 17:34:33 -08002274 * index @index as it confuses some of the callers. This breaks the
Matthew Wilcoxa6906972018-05-16 18:12:54 -04002275 * iteration when there is a page at index -1 but that is already
2276 * broken anyway.
Jan Kara72b045a2017-11-15 17:34:33 -08002277 */
2278 if (end == (pgoff_t)-1)
2279 *index = (pgoff_t)-1;
2280 else
2281 *index = end + 1;
2282out:
Nick Piggina60637c2008-07-25 19:45:31 -07002283 rcu_read_unlock();
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return ret;
2286}
Jan Kara72b045a2017-11-15 17:34:33 -08002287EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002289/*
2290 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
2291 * a _large_ part of the i/o request. Imagine the worst scenario:
2292 *
2293 * ---R__________________________________________B__________
2294 * ^ reading here ^ bad block(assume 4k)
2295 *
2296 * read(R) => miss => readahead(R...B) => media error => frustrating retries
2297 * => failing the whole request => read(R) => read(R+1) =>
2298 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
2299 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
2300 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
2301 *
2302 * It is going insane. Fix it by quickly scaling down the readahead size.
2303 */
Souptick Joarder0f8e2db2020-04-01 21:04:50 -07002304static void shrink_readahead_size_eio(struct file_ra_state *ra)
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002305{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002306 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002307}
2308
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002309/*
2310 * filemap_get_read_batch - Get a batch of pages for read
2311 *
2312 * Get a batch of pages which represent a contiguous range of bytes
2313 * in the file. No tail pages will be returned. If @index is in the
2314 * middle of a THP, the entire THP will be returned. The last page in
2315 * the batch may have Readahead set or be not Uptodate so that the
2316 * caller can take the appropriate action.
2317 */
2318static void filemap_get_read_batch(struct address_space *mapping,
2319 pgoff_t index, pgoff_t max, struct pagevec *pvec)
2320{
2321 XA_STATE(xas, &mapping->i_pages, index);
2322 struct page *head;
2323
2324 rcu_read_lock();
2325 for (head = xas_load(&xas); head; head = xas_next(&xas)) {
2326 if (xas_retry(&xas, head))
2327 continue;
2328 if (xas.xa_index > max || xa_is_value(head))
2329 break;
2330 if (!page_cache_get_speculative(head))
2331 goto retry;
2332
2333 /* Has the page moved or been split? */
2334 if (unlikely(head != xas_reload(&xas)))
2335 goto put_page;
2336
2337 if (!pagevec_add(pvec, head))
2338 break;
2339 if (!PageUptodate(head))
2340 break;
2341 if (PageReadahead(head))
2342 break;
2343 xas.xa_index = head->index + thp_nr_pages(head) - 1;
2344 xas.xa_offset = (xas.xa_index >> xas.xa_shift) & XA_CHUNK_MASK;
2345 continue;
2346put_page:
2347 put_page(head);
2348retry:
2349 xas_reset(&xas);
2350 }
2351 rcu_read_unlock();
2352}
2353
Matthew Wilcox (Oracle)68430302021-02-24 12:02:15 -08002354static int filemap_read_page(struct file *file, struct address_space *mapping,
2355 struct page *page)
Kent Overstreet723ef242020-12-14 19:04:52 -08002356{
Kent Overstreet723ef242020-12-14 19:04:52 -08002357 int error;
2358
Kent Overstreet723ef242020-12-14 19:04:52 -08002359 /*
Matthew Wilcox (Oracle)68430302021-02-24 12:02:15 -08002360 * A previous I/O error may have been due to temporary failures,
2361 * eg. multipath errors. PG_error will be set again if readpage
2362 * fails.
Kent Overstreet723ef242020-12-14 19:04:52 -08002363 */
2364 ClearPageError(page);
2365 /* Start the actual read. The read will unlock the page. */
Matthew Wilcox (Oracle)68430302021-02-24 12:02:15 -08002366 error = mapping->a_ops->readpage(file, page);
2367 if (error)
2368 return error;
Kent Overstreet723ef242020-12-14 19:04:52 -08002369
Matthew Wilcox (Oracle)aa1ec2f2021-02-24 12:02:38 -08002370 error = wait_on_page_locked_killable(page);
Matthew Wilcox (Oracle)68430302021-02-24 12:02:15 -08002371 if (error)
2372 return error;
Matthew Wilcox (Oracle)aa1ec2f2021-02-24 12:02:38 -08002373 if (PageUptodate(page))
2374 return 0;
Matthew Wilcox (Oracle)aa1ec2f2021-02-24 12:02:38 -08002375 shrink_readahead_size_eio(&file->f_ra);
2376 return -EIO;
Kent Overstreet723ef242020-12-14 19:04:52 -08002377}
2378
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002379static bool filemap_range_uptodate(struct address_space *mapping,
2380 loff_t pos, struct iov_iter *iter, struct page *page)
2381{
2382 int count;
2383
2384 if (PageUptodate(page))
2385 return true;
2386 /* pipes can't handle partially uptodate pages */
2387 if (iov_iter_is_pipe(iter))
2388 return false;
2389 if (!mapping->a_ops->is_partially_uptodate)
2390 return false;
2391 if (mapping->host->i_blkbits >= (PAGE_SHIFT + thp_order(page)))
2392 return false;
2393
2394 count = iter->count;
2395 if (page_offset(page) > pos) {
2396 count -= page_offset(page) - pos;
2397 pos = 0;
2398 } else {
2399 pos -= page_offset(page);
2400 }
2401
2402 return mapping->a_ops->is_partially_uptodate(page, pos, count);
2403}
2404
Matthew Wilcox (Oracle)4612aee2021-02-24 12:02:22 -08002405static int filemap_update_page(struct kiocb *iocb,
2406 struct address_space *mapping, struct iov_iter *iter,
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002407 struct page *page)
Kent Overstreet723ef242020-12-14 19:04:52 -08002408{
Kent Overstreet723ef242020-12-14 19:04:52 -08002409 int error;
2410
Jan Kara730633f2021-01-28 19:19:45 +01002411 if (iocb->ki_flags & IOCB_NOWAIT) {
2412 if (!filemap_invalidate_trylock_shared(mapping))
Matthew Wilcox (Oracle)87d1d7b2021-02-24 12:02:25 -08002413 return -EAGAIN;
Jan Kara730633f2021-01-28 19:19:45 +01002414 } else {
2415 filemap_invalidate_lock_shared(mapping);
2416 }
2417
2418 if (!trylock_page(page)) {
2419 error = -EAGAIN;
2420 if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_NOIO))
2421 goto unlock_mapping;
Matthew Wilcox (Oracle)87d1d7b2021-02-24 12:02:25 -08002422 if (!(iocb->ki_flags & IOCB_WAITQ)) {
Jan Kara730633f2021-01-28 19:19:45 +01002423 filemap_invalidate_unlock_shared(mapping);
Matthew Wilcox (Oracle)bd8a1f32021-02-24 12:02:05 -08002424 put_and_wait_on_page_locked(page, TASK_KILLABLE);
Matthew Wilcox (Oracle)4612aee2021-02-24 12:02:22 -08002425 return AOP_TRUNCATED_PAGE;
Matthew Wilcox (Oracle)bd8a1f32021-02-24 12:02:05 -08002426 }
Matthew Wilcox (Oracle)87d1d7b2021-02-24 12:02:25 -08002427 error = __lock_page_async(page, iocb->ki_waitq);
2428 if (error)
Jan Kara730633f2021-01-28 19:19:45 +01002429 goto unlock_mapping;
Kent Overstreet723ef242020-12-14 19:04:52 -08002430 }
Kent Overstreet723ef242020-12-14 19:04:52 -08002431
Jan Kara730633f2021-01-28 19:19:45 +01002432 error = AOP_TRUNCATED_PAGE;
Matthew Wilcox (Oracle)bd8a1f32021-02-24 12:02:05 -08002433 if (!page->mapping)
Jan Kara730633f2021-01-28 19:19:45 +01002434 goto unlock;
Kent Overstreet723ef242020-12-14 19:04:52 -08002435
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002436 error = 0;
2437 if (filemap_range_uptodate(mapping, iocb->ki_pos, iter, page))
2438 goto unlock;
2439
2440 error = -EAGAIN;
2441 if (iocb->ki_flags & (IOCB_NOIO | IOCB_NOWAIT | IOCB_WAITQ))
2442 goto unlock;
2443
Matthew Wilcox (Oracle)68430302021-02-24 12:02:15 -08002444 error = filemap_read_page(iocb->ki_filp, mapping, page);
Jan Kara730633f2021-01-28 19:19:45 +01002445 goto unlock_mapping;
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002446unlock:
2447 unlock_page(page);
Jan Kara730633f2021-01-28 19:19:45 +01002448unlock_mapping:
2449 filemap_invalidate_unlock_shared(mapping);
2450 if (error == AOP_TRUNCATED_PAGE)
2451 put_page(page);
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002452 return error;
Kent Overstreet723ef242020-12-14 19:04:52 -08002453}
2454
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002455static int filemap_create_page(struct file *file,
2456 struct address_space *mapping, pgoff_t index,
2457 struct pagevec *pvec)
Kent Overstreet723ef242020-12-14 19:04:52 -08002458{
Kent Overstreet723ef242020-12-14 19:04:52 -08002459 struct page *page;
2460 int error;
2461
Kent Overstreet723ef242020-12-14 19:04:52 -08002462 page = page_cache_alloc(mapping);
2463 if (!page)
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002464 return -ENOMEM;
Kent Overstreet723ef242020-12-14 19:04:52 -08002465
Jan Kara730633f2021-01-28 19:19:45 +01002466 /*
2467 * Protect against truncate / hole punch. Grabbing invalidate_lock here
2468 * assures we cannot instantiate and bring uptodate new pagecache pages
2469 * after evicting page cache during truncate and before actually
2470 * freeing blocks. Note that we could release invalidate_lock after
2471 * inserting the page into page cache as the locked page would then be
2472 * enough to synchronize with hole punching. But there are code paths
2473 * such as filemap_update_page() filling in partially uptodate pages or
2474 * ->readpages() that need to hold invalidate_lock while mapping blocks
2475 * for IO so let's hold the lock here as well to keep locking rules
2476 * simple.
2477 */
2478 filemap_invalidate_lock_shared(mapping);
Kent Overstreet723ef242020-12-14 19:04:52 -08002479 error = add_to_page_cache_lru(page, mapping, index,
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002480 mapping_gfp_constraint(mapping, GFP_KERNEL));
2481 if (error == -EEXIST)
2482 error = AOP_TRUNCATED_PAGE;
2483 if (error)
2484 goto error;
2485
2486 error = filemap_read_page(file, mapping, page);
2487 if (error)
2488 goto error;
2489
Jan Kara730633f2021-01-28 19:19:45 +01002490 filemap_invalidate_unlock_shared(mapping);
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002491 pagevec_add(pvec, page);
2492 return 0;
2493error:
Jan Kara730633f2021-01-28 19:19:45 +01002494 filemap_invalidate_unlock_shared(mapping);
Matthew Wilcox (Oracle)68430302021-02-24 12:02:15 -08002495 put_page(page);
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002496 return error;
Kent Overstreet723ef242020-12-14 19:04:52 -08002497}
2498
Matthew Wilcox (Oracle)5963fe02021-02-24 12:02:32 -08002499static int filemap_readahead(struct kiocb *iocb, struct file *file,
2500 struct address_space *mapping, struct page *page,
2501 pgoff_t last_index)
2502{
2503 if (iocb->ki_flags & IOCB_NOIO)
2504 return -EAGAIN;
2505 page_cache_async_readahead(mapping, &file->f_ra, file, page,
2506 page->index, last_index - page->index);
2507 return 0;
2508}
2509
Matthew Wilcox (Oracle)3a6bae42021-02-24 12:01:49 -08002510static int filemap_get_pages(struct kiocb *iocb, struct iov_iter *iter,
Matthew Wilcox (Oracle)ff993ba2021-02-24 12:01:55 -08002511 struct pagevec *pvec)
Kent Overstreet06c04442020-12-14 19:04:56 -08002512{
2513 struct file *filp = iocb->ki_filp;
2514 struct address_space *mapping = filp->f_mapping;
2515 struct file_ra_state *ra = &filp->f_ra;
2516 pgoff_t index = iocb->ki_pos >> PAGE_SHIFT;
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002517 pgoff_t last_index;
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002518 struct page *page;
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002519 int err = 0;
Kent Overstreet06c04442020-12-14 19:04:56 -08002520
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002521 last_index = DIV_ROUND_UP(iocb->ki_pos + iter->count, PAGE_SIZE);
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002522retry:
Kent Overstreet06c04442020-12-14 19:04:56 -08002523 if (fatal_signal_pending(current))
2524 return -EINTR;
2525
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002526 filemap_get_read_batch(mapping, index, last_index, pvec);
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002527 if (!pagevec_count(pvec)) {
2528 if (iocb->ki_flags & IOCB_NOIO)
2529 return -EAGAIN;
2530 page_cache_sync_readahead(mapping, ra, filp, index,
2531 last_index - index);
2532 filemap_get_read_batch(mapping, index, last_index, pvec);
2533 }
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002534 if (!pagevec_count(pvec)) {
2535 if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_WAITQ))
2536 return -EAGAIN;
2537 err = filemap_create_page(filp, mapping,
2538 iocb->ki_pos >> PAGE_SHIFT, pvec);
2539 if (err == AOP_TRUNCATED_PAGE)
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002540 goto retry;
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002541 return err;
2542 }
Kent Overstreet06c04442020-12-14 19:04:56 -08002543
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002544 page = pvec->pages[pagevec_count(pvec) - 1];
2545 if (PageReadahead(page)) {
2546 err = filemap_readahead(iocb, filp, mapping, page, last_index);
2547 if (err)
2548 goto err;
2549 }
2550 if (!PageUptodate(page)) {
2551 if ((iocb->ki_flags & IOCB_WAITQ) && pagevec_count(pvec) > 1)
2552 iocb->ki_flags |= IOCB_NOWAIT;
2553 err = filemap_update_page(iocb, mapping, iter, page);
2554 if (err)
2555 goto err;
Kent Overstreet06c04442020-12-14 19:04:56 -08002556 }
2557
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002558 return 0;
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002559err:
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002560 if (err < 0)
2561 put_page(page);
2562 if (likely(--pvec->nr))
Matthew Wilcox (Oracle)ff993ba2021-02-24 12:01:55 -08002563 return 0;
Matthew Wilcox (Oracle)4612aee2021-02-24 12:02:22 -08002564 if (err == AOP_TRUNCATED_PAGE)
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002565 goto retry;
2566 return err;
Kent Overstreet06c04442020-12-14 19:04:56 -08002567}
2568
Randy Dunlap485bb992006-06-23 02:03:49 -07002569/**
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002570 * filemap_read - Read data from the page cache.
2571 * @iocb: The iocb to read.
2572 * @iter: Destination for the data.
2573 * @already_read: Number of bytes already read by the caller.
Randy Dunlap485bb992006-06-23 02:03:49 -07002574 *
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002575 * Copies data from the page cache. If the data is not currently present,
2576 * uses the readahead and readpage address_space operations to fetch it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 *
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002578 * Return: Total number of bytes copied, including those already read by
2579 * the caller. If an error happens before any bytes are copied, returns
2580 * a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 */
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002582ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
2583 ssize_t already_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002585 struct file *filp = iocb->ki_filp;
Kent Overstreet06c04442020-12-14 19:04:56 -08002586 struct file_ra_state *ra = &filp->f_ra;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002587 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 struct inode *inode = mapping->host;
Matthew Wilcox (Oracle)ff993ba2021-02-24 12:01:55 -08002589 struct pagevec pvec;
2590 int i, error = 0;
Kent Overstreet06c04442020-12-14 19:04:56 -08002591 bool writably_mapped;
2592 loff_t isize, end_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Kent Overstreet723ef242020-12-14 19:04:52 -08002594 if (unlikely(iocb->ki_pos >= inode->i_sb->s_maxbytes))
Linus Torvaldsd05c5f72016-12-14 12:45:25 -08002595 return 0;
Kent Overstreet3644e2d2020-12-18 04:07:11 -05002596 if (unlikely(!iov_iter_count(iter)))
2597 return 0;
2598
Wei Fangc2a97372016-10-07 17:01:52 -07002599 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002600 pagevec_init(&pvec);
Wei Fangc2a97372016-10-07 17:01:52 -07002601
Kent Overstreet06c04442020-12-14 19:04:56 -08002602 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 cond_resched();
Kent Overstreet06c04442020-12-14 19:04:56 -08002604
2605 /*
2606 * If we've already successfully copied some data, then we
2607 * can no longer safely return -EIOCBQUEUED. Hence mark
2608 * an async read NOWAIT at that point.
2609 */
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002610 if ((iocb->ki_flags & IOCB_WAITQ) && already_read)
Kent Overstreet06c04442020-12-14 19:04:56 -08002611 iocb->ki_flags |= IOCB_NOWAIT;
2612
Matthew Wilcox (Oracle)ff993ba2021-02-24 12:01:55 -08002613 error = filemap_get_pages(iocb, iter, &pvec);
2614 if (error < 0)
Kent Overstreet06c04442020-12-14 19:04:56 -08002615 break;
Michal Hocko5abf1862017-02-03 13:13:29 -08002616
Kent Overstreet723ef242020-12-14 19:04:52 -08002617 /*
Kent Overstreet06c04442020-12-14 19:04:56 -08002618 * i_size must be checked after we know the pages are Uptodate.
2619 *
2620 * Checking i_size after the check allows us to calculate
2621 * the correct value for "nr", which means the zero-filled
2622 * part of the page is not copied back to userspace (unless
2623 * another truncate extends the file - this is desired though).
Kent Overstreet723ef242020-12-14 19:04:52 -08002624 */
Kent Overstreet06c04442020-12-14 19:04:56 -08002625 isize = i_size_read(inode);
2626 if (unlikely(iocb->ki_pos >= isize))
2627 goto put_pages;
Kent Overstreet06c04442020-12-14 19:04:56 -08002628 end_offset = min_t(loff_t, isize, iocb->ki_pos + iter->count);
2629
Kent Overstreet06c04442020-12-14 19:04:56 -08002630 /*
2631 * Once we start copying data, we don't want to be touching any
2632 * cachelines that might be contended:
2633 */
2634 writably_mapped = mapping_writably_mapped(mapping);
2635
2636 /*
2637 * When a sequential read accesses a page several times, only
2638 * mark it as accessed the first time.
2639 */
2640 if (iocb->ki_pos >> PAGE_SHIFT !=
2641 ra->prev_pos >> PAGE_SHIFT)
Matthew Wilcox (Oracle)ff993ba2021-02-24 12:01:55 -08002642 mark_page_accessed(pvec.pages[0]);
Kent Overstreet06c04442020-12-14 19:04:56 -08002643
Matthew Wilcox (Oracle)ff993ba2021-02-24 12:01:55 -08002644 for (i = 0; i < pagevec_count(&pvec); i++) {
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002645 struct page *page = pvec.pages[i];
2646 size_t page_size = thp_size(page);
2647 size_t offset = iocb->ki_pos & (page_size - 1);
2648 size_t bytes = min_t(loff_t, end_offset - iocb->ki_pos,
2649 page_size - offset);
2650 size_t copied;
Kent Overstreet06c04442020-12-14 19:04:56 -08002651
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002652 if (end_offset < page_offset(page))
2653 break;
2654 if (i > 0)
2655 mark_page_accessed(page);
Kent Overstreet06c04442020-12-14 19:04:56 -08002656 /*
2657 * If users can be writing to this page using arbitrary
2658 * virtual addresses, take care about potential aliasing
2659 * before reading the page on the kernel side.
2660 */
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002661 if (writably_mapped) {
2662 int j;
Kent Overstreet06c04442020-12-14 19:04:56 -08002663
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002664 for (j = 0; j < thp_nr_pages(page); j++)
2665 flush_dcache_page(page + j);
2666 }
2667
2668 copied = copy_page_to_iter(page, offset, bytes, iter);
Kent Overstreet06c04442020-12-14 19:04:56 -08002669
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002670 already_read += copied;
Kent Overstreet06c04442020-12-14 19:04:56 -08002671 iocb->ki_pos += copied;
2672 ra->prev_pos = iocb->ki_pos;
2673
2674 if (copied < bytes) {
2675 error = -EFAULT;
2676 break;
Kent Overstreet723ef242020-12-14 19:04:52 -08002677 }
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002678 }
Kent Overstreet06c04442020-12-14 19:04:56 -08002679put_pages:
Matthew Wilcox (Oracle)ff993ba2021-02-24 12:01:55 -08002680 for (i = 0; i < pagevec_count(&pvec); i++)
2681 put_page(pvec.pages[i]);
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002682 pagevec_reinit(&pvec);
Kent Overstreet06c04442020-12-14 19:04:56 -08002683 } while (iov_iter_count(iter) && iocb->ki_pos < isize && !error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002685 file_accessed(filp);
Kent Overstreet06c04442020-12-14 19:04:56 -08002686
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002687 return already_read ? already_read : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688}
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002689EXPORT_SYMBOL_GPL(filemap_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Randy Dunlap485bb992006-06-23 02:03:49 -07002691/**
Al Viro6abd2322014-04-04 14:20:57 -04002692 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002693 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002694 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002695 *
Al Viro6abd2322014-04-04 14:20:57 -04002696 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 * that can use the page cache directly.
Andreas Gruenbacher41da51b2019-11-21 23:25:07 +00002698 *
2699 * The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall
2700 * be returned when no data can be read without waiting for I/O requests
2701 * to complete; it doesn't prevent readahead.
2702 *
2703 * The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O
2704 * requests shall be made for the read or for readahead. When no data
2705 * can be read, -EAGAIN shall be returned. When readahead would be
2706 * triggered, a partial, possibly empty read shall be returned.
2707 *
Mike Rapoporta862f682019-03-05 15:48:42 -08002708 * Return:
2709 * * number of bytes copied, even for partial reads
Andreas Gruenbacher41da51b2019-11-21 23:25:07 +00002710 * * negative error code (or 0 if IOCB_NOIO) if nothing was read
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 */
2712ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002713generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
Nicolai Stangee7080a42016-03-25 14:22:14 -07002715 size_t count = iov_iter_count(iter);
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002716 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002717
2718 if (!count)
Christoph Hellwig826ea862021-02-24 12:02:45 -08002719 return 0; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Al Viro2ba48ce2015-04-09 13:52:01 -04002721 if (iocb->ki_flags & IOCB_DIRECT) {
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002722 struct file *file = iocb->ki_filp;
Al Viroed978a82014-03-05 22:53:04 -05002723 struct address_space *mapping = file->f_mapping;
2724 struct inode *inode = mapping->host;
Badari Pulavarty543ade12006-09-30 23:28:48 -07002725 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 size = i_size_read(inode);
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002728 if (iocb->ki_flags & IOCB_NOWAIT) {
Jens Axboe7a60d6d2021-04-29 22:55:21 -07002729 if (filemap_range_needs_writeback(mapping, iocb->ki_pos,
2730 iocb->ki_pos + count - 1))
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002731 return -EAGAIN;
2732 } else {
2733 retval = filemap_write_and_wait_range(mapping,
2734 iocb->ki_pos,
2735 iocb->ki_pos + count - 1);
2736 if (retval < 0)
Christoph Hellwig826ea862021-02-24 12:02:45 -08002737 return retval;
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002738 }
Al Viroed978a82014-03-05 22:53:04 -05002739
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002740 file_accessed(file);
2741
Al Viro5ecda132017-04-13 14:13:36 -04002742 retval = mapping->a_ops->direct_IO(iocb, iter);
Al Viroc3a69022016-10-10 13:26:27 -04002743 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002744 iocb->ki_pos += retval;
Al Viro5ecda132017-04-13 14:13:36 -04002745 count -= retval;
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002746 }
Pavel Begunkovab2125d2021-02-24 12:01:45 -08002747 if (retval != -EIOCBQUEUED)
2748 iov_iter_revert(iter, count - iov_iter_count(iter));
Josef Bacik66f998f2010-05-23 11:00:54 -04002749
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002750 /*
2751 * Btrfs can have a short DIO read if we encounter
2752 * compressed extents, so if there was an error, or if
2753 * we've already read everything we wanted to, or if
2754 * there was a short read because we hit EOF, go ahead
2755 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002756 * the rest of the read. Buffered reads will not work for
2757 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002758 */
Al Viro5ecda132017-04-13 14:13:36 -04002759 if (retval < 0 || !count || iocb->ki_pos >= size ||
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002760 IS_DAX(inode))
Christoph Hellwig826ea862021-02-24 12:02:45 -08002761 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
2763
Christoph Hellwig826ea862021-02-24 12:02:45 -08002764 return filemap_read(iocb, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765}
Al Viroed978a82014-03-05 22:53:04 -05002766EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -08002768static inline loff_t page_seek_hole_data(struct xa_state *xas,
2769 struct address_space *mapping, struct page *page,
2770 loff_t start, loff_t end, bool seek_data)
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002771{
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -08002772 const struct address_space_operations *ops = mapping->a_ops;
2773 size_t offset, bsz = i_blocksize(mapping->host);
2774
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002775 if (xa_is_value(page) || PageUptodate(page))
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -08002776 return seek_data ? start : end;
2777 if (!ops->is_partially_uptodate)
2778 return seek_data ? end : start;
2779
2780 xas_pause(xas);
2781 rcu_read_unlock();
2782 lock_page(page);
2783 if (unlikely(page->mapping != mapping))
2784 goto unlock;
2785
2786 offset = offset_in_thp(page, start) & ~(bsz - 1);
2787
2788 do {
2789 if (ops->is_partially_uptodate(page, offset, bsz) == seek_data)
2790 break;
2791 start = (start + bsz) & ~(bsz - 1);
2792 offset += bsz;
2793 } while (offset < thp_size(page));
2794unlock:
2795 unlock_page(page);
2796 rcu_read_lock();
2797 return start;
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002798}
2799
2800static inline
2801unsigned int seek_page_size(struct xa_state *xas, struct page *page)
2802{
2803 if (xa_is_value(page))
2804 return PAGE_SIZE << xa_get_order(xas->xa, xas->xa_index);
2805 return thp_size(page);
2806}
2807
2808/**
2809 * mapping_seek_hole_data - Seek for SEEK_DATA / SEEK_HOLE in the page cache.
2810 * @mapping: Address space to search.
2811 * @start: First byte to consider.
2812 * @end: Limit of search (exclusive).
2813 * @whence: Either SEEK_HOLE or SEEK_DATA.
2814 *
2815 * If the page cache knows which blocks contain holes and which blocks
2816 * contain data, your filesystem can use this function to implement
2817 * SEEK_HOLE and SEEK_DATA. This is useful for filesystems which are
2818 * entirely memory-based such as tmpfs, and filesystems which support
2819 * unwritten extents.
2820 *
Ingo Molnarf0953a12021-05-06 18:06:47 -07002821 * Return: The requested offset on success, or -ENXIO if @whence specifies
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002822 * SEEK_DATA and there is no data after @start. There is an implicit hole
2823 * after @end - 1, so SEEK_HOLE returns @end if all the bytes between @start
2824 * and @end contain data.
2825 */
2826loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start,
2827 loff_t end, int whence)
2828{
2829 XA_STATE(xas, &mapping->i_pages, start >> PAGE_SHIFT);
Hugh Dickinsed98b012021-04-23 14:29:00 -07002830 pgoff_t max = (end - 1) >> PAGE_SHIFT;
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002831 bool seek_data = (whence == SEEK_DATA);
2832 struct page *page;
2833
2834 if (end <= start)
2835 return -ENXIO;
2836
2837 rcu_read_lock();
2838 while ((page = find_get_entry(&xas, max, XA_PRESENT))) {
Hugh Dickinsed98b012021-04-23 14:29:00 -07002839 loff_t pos = (u64)xas.xa_index << PAGE_SHIFT;
2840 unsigned int seek_size;
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002841
2842 if (start < pos) {
2843 if (!seek_data)
2844 goto unlock;
2845 start = pos;
2846 }
2847
Hugh Dickinsed98b012021-04-23 14:29:00 -07002848 seek_size = seek_page_size(&xas, page);
2849 pos = round_up(pos + 1, seek_size);
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -08002850 start = page_seek_hole_data(&xas, mapping, page, start, pos,
2851 seek_data);
2852 if (start < pos)
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002853 goto unlock;
Hugh Dickinsed98b012021-04-23 14:29:00 -07002854 if (start >= end)
2855 break;
2856 if (seek_size > PAGE_SIZE)
2857 xas_set(&xas, pos >> PAGE_SHIFT);
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002858 if (!xa_is_value(page))
2859 put_page(page);
2860 }
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002861 if (seek_data)
Hugh Dickinsed98b012021-04-23 14:29:00 -07002862 start = -ENXIO;
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002863unlock:
2864 rcu_read_unlock();
Hugh Dickinsed98b012021-04-23 14:29:00 -07002865 if (page && !xa_is_value(page))
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002866 put_page(page);
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002867 if (start > end)
2868 return end;
2869 return start;
2870}
2871
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873#define MMAP_LOTSAMISS (100)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002874/*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002875 * lock_page_maybe_drop_mmap - lock the page, possibly dropping the mmap_lock
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002876 * @vmf - the vm_fault for this fault.
2877 * @page - the page to lock.
2878 * @fpin - the pointer to the file we may pin (or is already pinned).
2879 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002880 * This works similar to lock_page_or_retry in that it can drop the mmap_lock.
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002881 * It differs in that it actually returns the page locked if it returns 1 and 0
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002882 * if it couldn't lock the page. If we did have to drop the mmap_lock then fpin
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002883 * will point to the pinned file and needs to be fput()'ed at a later point.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002884 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002885static int lock_page_maybe_drop_mmap(struct vm_fault *vmf, struct page *page,
2886 struct file **fpin)
2887{
2888 if (trylock_page(page))
2889 return 1;
2890
Linus Torvalds8b0f9fa2019-03-15 11:26:07 -07002891 /*
2892 * NOTE! This will make us return with VM_FAULT_RETRY, but with
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002893 * the mmap_lock still held. That's how FAULT_FLAG_RETRY_NOWAIT
Linus Torvalds8b0f9fa2019-03-15 11:26:07 -07002894 * is supposed to work. We have way too many special cases..
2895 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002896 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
2897 return 0;
2898
2899 *fpin = maybe_unlock_mmap_for_io(vmf, *fpin);
2900 if (vmf->flags & FAULT_FLAG_KILLABLE) {
2901 if (__lock_page_killable(page)) {
2902 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002903 * We didn't have the right flags to drop the mmap_lock,
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002904 * but all fault_handlers only check for fatal signals
2905 * if we return VM_FAULT_RETRY, so we need to drop the
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002906 * mmap_lock here and return 0 if we don't have a fpin.
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002907 */
2908 if (*fpin == NULL)
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002909 mmap_read_unlock(vmf->vma->vm_mm);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002910 return 0;
2911 }
2912 } else
2913 __lock_page(page);
2914 return 1;
2915}
2916
2917
2918/*
2919 * Synchronous readahead happens when we don't even find a page in the page
2920 * cache at all. We don't want to perform IO under the mmap sem, so if we have
2921 * to drop the mmap sem we return the file that was pinned in order for us to do
2922 * that. If we didn't pin a file then we return NULL. The file that is
2923 * returned needs to be fput()'ed when we're done with it.
2924 */
2925static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002926{
Josef Bacik2a1180f2019-03-13 11:44:18 -07002927 struct file *file = vmf->vma->vm_file;
2928 struct file_ra_state *ra = &file->f_ra;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002929 struct address_space *mapping = file->f_mapping;
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +01002930 DEFINE_READAHEAD(ractl, file, ra, mapping, vmf->pgoff);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002931 struct file *fpin = NULL;
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07002932 unsigned int mmap_miss;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002933
2934 /* If we don't want any read-ahead, don't bother */
Josef Bacik2a1180f2019-03-13 11:44:18 -07002935 if (vmf->vma->vm_flags & VM_RAND_READ)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002936 return fpin;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002937 if (!ra->ra_pages)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002938 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002939
Josef Bacik2a1180f2019-03-13 11:44:18 -07002940 if (vmf->vma->vm_flags & VM_SEQ_READ) {
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002941 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +01002942 page_cache_sync_ra(&ractl, ra->ra_pages);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002943 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002944 }
2945
Andi Kleen207d04b2011-05-24 17:12:29 -07002946 /* Avoid banging the cache line if not needed */
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07002947 mmap_miss = READ_ONCE(ra->mmap_miss);
2948 if (mmap_miss < MMAP_LOTSAMISS * 10)
2949 WRITE_ONCE(ra->mmap_miss, ++mmap_miss);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002950
2951 /*
2952 * Do we miss much more than hit in this file? If so,
2953 * stop bothering with read-ahead. It will only hurt.
2954 */
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07002955 if (mmap_miss > MMAP_LOTSAMISS)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002956 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002957
Wu Fengguangd30a1102009-06-16 15:31:30 -07002958 /*
2959 * mmap read-around
2960 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002961 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
David Howellsdb660d42020-10-15 20:06:31 -07002962 ra->start = max_t(long, 0, vmf->pgoff - ra->ra_pages / 2);
Roman Gushchin600e19a2015-11-05 18:47:08 -08002963 ra->size = ra->ra_pages;
2964 ra->async_size = ra->ra_pages / 4;
David Howellsdb660d42020-10-15 20:06:31 -07002965 ractl._index = ra->start;
2966 do_page_cache_ra(&ractl, ra->size, ra->async_size);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002967 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002968}
2969
2970/*
2971 * Asynchronous readahead happens when we find the page and PG_readahead,
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002972 * so we want to possibly extend the readahead further. We return the file that
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002973 * was pinned if we have to drop the mmap_lock in order to do IO.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002974 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002975static struct file *do_async_mmap_readahead(struct vm_fault *vmf,
2976 struct page *page)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002977{
Josef Bacik2a1180f2019-03-13 11:44:18 -07002978 struct file *file = vmf->vma->vm_file;
2979 struct file_ra_state *ra = &file->f_ra;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002980 struct address_space *mapping = file->f_mapping;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002981 struct file *fpin = NULL;
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07002982 unsigned int mmap_miss;
Josef Bacik2a1180f2019-03-13 11:44:18 -07002983 pgoff_t offset = vmf->pgoff;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002984
2985 /* If we don't want any read-ahead, don't bother */
Jan Kara5c72fee2020-04-01 21:04:40 -07002986 if (vmf->vma->vm_flags & VM_RAND_READ || !ra->ra_pages)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002987 return fpin;
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07002988 mmap_miss = READ_ONCE(ra->mmap_miss);
2989 if (mmap_miss)
2990 WRITE_ONCE(ra->mmap_miss, --mmap_miss);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002991 if (PageReadahead(page)) {
2992 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Wu Fengguang2fad6f52009-06-16 15:31:29 -07002993 page_cache_async_readahead(mapping, ra, file,
2994 page, offset, ra->ra_pages);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002995 }
2996 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002997}
2998
Randy Dunlap485bb992006-06-23 02:03:49 -07002999/**
Nick Piggin54cb8822007-07-19 01:46:59 -07003000 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07003001 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07003002 *
Nick Piggin54cb8822007-07-19 01:46:59 -07003003 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 * mapped memory region to read in file data during a page fault.
3005 *
3006 * The goto's are kind of ugly, but this streamlines the normal case of having
3007 * it in the page cache, and handles the special cases reasonably without
3008 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003009 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003010 * vma->vm_mm->mmap_lock must be held on entry.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003011 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003012 * If our return value has VM_FAULT_RETRY set, it's because the mmap_lock
Yang Shia4985832019-07-11 20:55:29 -07003013 * may be dropped before doing I/O or by lock_page_maybe_drop_mmap().
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003014 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003015 * If our return value does not have VM_FAULT_RETRY set, the mmap_lock
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003016 * has not been released.
3017 *
3018 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Mike Rapoporta862f682019-03-05 15:48:42 -08003019 *
3020 * Return: bitwise-OR of %VM_FAULT_ codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 */
Souptick Joarder2bcd6452018-06-07 17:08:00 -07003022vm_fault_t filemap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
3024 int error;
Dave Jiang11bac802017-02-24 14:56:41 -08003025 struct file *file = vmf->vma->vm_file;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003026 struct file *fpin = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07003029 pgoff_t offset = vmf->pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07003030 pgoff_t max_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 struct page *page;
Souptick Joarder2bcd6452018-06-07 17:08:00 -07003032 vm_fault_t ret = 0;
Jan Kara730633f2021-01-28 19:19:45 +01003033 bool mapping_locked = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Matthew Wilcox9ab25942017-05-03 14:53:29 -07003035 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3036 if (unlikely(offset >= max_off))
Linus Torvalds5307cc12007-10-31 09:19:46 -07003037 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 /*
Johannes Weiner49426422013-10-16 13:46:59 -07003040 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07003042 page = find_get_page(mapping, offset);
Jan Kara730633f2021-01-28 19:19:45 +01003043 if (likely(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 /*
Jan Kara730633f2021-01-28 19:19:45 +01003045 * We found the page, so try async readahead before waiting for
3046 * the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 */
Jan Kara730633f2021-01-28 19:19:45 +01003048 if (!(vmf->flags & FAULT_FLAG_TRIED))
3049 fpin = do_async_mmap_readahead(vmf, page);
3050 if (unlikely(!PageUptodate(page))) {
3051 filemap_invalidate_lock_shared(mapping);
3052 mapping_locked = true;
3053 }
3054 } else {
Linus Torvaldsef00e082009-06-16 15:31:25 -07003055 /* No page in the page cache at all */
Linus Torvaldsef00e082009-06-16 15:31:25 -07003056 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07003057 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07003058 ret = VM_FAULT_MAJOR;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003059 fpin = do_sync_mmap_readahead(vmf);
Linus Torvaldsef00e082009-06-16 15:31:25 -07003060retry_find:
Jan Kara730633f2021-01-28 19:19:45 +01003061 /*
3062 * See comment in filemap_create_page() why we need
3063 * invalidate_lock
3064 */
3065 if (!mapping_locked) {
3066 filemap_invalidate_lock_shared(mapping);
3067 mapping_locked = true;
3068 }
Josef Bacika75d4c32019-03-13 11:44:14 -07003069 page = pagecache_get_page(mapping, offset,
3070 FGP_CREAT|FGP_FOR_MMAP,
3071 vmf->gfp_mask);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003072 if (!page) {
3073 if (fpin)
3074 goto out_retry;
Jan Kara730633f2021-01-28 19:19:45 +01003075 filemap_invalidate_unlock_shared(mapping);
Matthew Wilcox (Oracle)e520e932020-04-01 21:04:53 -07003076 return VM_FAULT_OOM;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
3079
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003080 if (!lock_page_maybe_drop_mmap(vmf, page, &fpin))
3081 goto out_retry;
Michel Lespinasseb522c942010-10-26 14:21:56 -07003082
3083 /* Did it get truncated? */
Song Liu585e5a72019-09-23 15:37:44 -07003084 if (unlikely(compound_head(page)->mapping != mapping)) {
Michel Lespinasseb522c942010-10-26 14:21:56 -07003085 unlock_page(page);
3086 put_page(page);
3087 goto retry_find;
3088 }
Song Liu520e5ba2019-09-23 15:37:50 -07003089 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07003090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 /*
Nick Piggind00806b2007-07-19 01:46:57 -07003092 * We have a locked page in the page cache, now we need to check
3093 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 */
Jan Kara730633f2021-01-28 19:19:45 +01003095 if (unlikely(!PageUptodate(page))) {
3096 /*
3097 * The page was in cache and uptodate and now it is not.
3098 * Strange but possible since we didn't hold the page lock all
3099 * the time. Let's drop everything get the invalidate lock and
3100 * try again.
3101 */
3102 if (!mapping_locked) {
3103 unlock_page(page);
3104 put_page(page);
3105 goto retry_find;
3106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 goto page_not_uptodate;
Jan Kara730633f2021-01-28 19:19:45 +01003108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Linus Torvaldsef00e082009-06-16 15:31:25 -07003110 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003111 * We've made it this far and we had to drop our mmap_lock, now is the
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003112 * time to return to the upper layer and have it re-find the vma and
3113 * redo the fault.
3114 */
3115 if (fpin) {
3116 unlock_page(page);
3117 goto out_retry;
3118 }
Jan Kara730633f2021-01-28 19:19:45 +01003119 if (mapping_locked)
3120 filemap_invalidate_unlock_shared(mapping);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003121
3122 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07003123 * Found the page and have a reference on it.
3124 * We must recheck i_size under page lock.
3125 */
Matthew Wilcox9ab25942017-05-03 14:53:29 -07003126 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3127 if (unlikely(offset >= max_off)) {
Nick Piggind00806b2007-07-19 01:46:57 -07003128 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003129 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07003130 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07003131 }
3132
Nick Piggind0217ac2007-07-19 01:47:03 -07003133 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07003134 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 /*
3138 * Umm, take care of errors if the page isn't up-to-date.
3139 * Try to re-read it _once_. We do this synchronously,
3140 * because there really aren't any performance issues here
3141 * and we need to check for errors.
3142 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003143 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Matthew Wilcox (Oracle)d31fa862021-04-29 22:55:26 -07003144 error = filemap_read_page(file, mapping, page);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003145 if (fpin)
3146 goto out_retry;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003147 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07003148
3149 if (!error || error == AOP_TRUNCATED_PAGE)
3150 goto retry_find;
Jan Kara730633f2021-01-28 19:19:45 +01003151 filemap_invalidate_unlock_shared(mapping);
Nick Piggind00806b2007-07-19 01:46:57 -07003152
Nick Piggind0217ac2007-07-19 01:47:03 -07003153 return VM_FAULT_SIGBUS;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003154
3155out_retry:
3156 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003157 * We dropped the mmap_lock, we need to return to the fault handler to
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003158 * re-find the vma and come back and find our hopefully still populated
3159 * page.
3160 */
3161 if (page)
3162 put_page(page);
Jan Kara730633f2021-01-28 19:19:45 +01003163 if (mapping_locked)
3164 filemap_invalidate_unlock_shared(mapping);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003165 if (fpin)
3166 fput(fpin);
3167 return ret | VM_FAULT_RETRY;
Nick Piggin54cb8822007-07-19 01:46:59 -07003168}
3169EXPORT_SYMBOL(filemap_fault);
3170
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003171static bool filemap_map_pmd(struct vm_fault *vmf, struct page *page)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003172{
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003173 struct mm_struct *mm = vmf->vma->vm_mm;
3174
3175 /* Huge page is mapped? No need to proceed. */
3176 if (pmd_trans_huge(*vmf->pmd)) {
3177 unlock_page(page);
3178 put_page(page);
3179 return true;
3180 }
3181
3182 if (pmd_none(*vmf->pmd) && PageTransHuge(page)) {
3183 vm_fault_t ret = do_set_pmd(vmf, page);
3184 if (!ret) {
3185 /* The page is mapped successfully, reference consumed. */
3186 unlock_page(page);
3187 return true;
3188 }
3189 }
3190
3191 if (pmd_none(*vmf->pmd)) {
3192 vmf->ptl = pmd_lock(mm, vmf->pmd);
3193 if (likely(pmd_none(*vmf->pmd))) {
3194 mm_inc_nr_ptes(mm);
3195 pmd_populate(mm, vmf->pmd, vmf->prealloc_pte);
3196 vmf->prealloc_pte = NULL;
3197 }
3198 spin_unlock(vmf->ptl);
3199 }
3200
3201 /* See comment in handle_pte_fault() */
3202 if (pmd_devmap_trans_unstable(vmf->pmd)) {
3203 unlock_page(page);
3204 put_page(page);
3205 return true;
3206 }
3207
3208 return false;
3209}
3210
3211static struct page *next_uptodate_page(struct page *page,
3212 struct address_space *mapping,
3213 struct xa_state *xas, pgoff_t end_pgoff)
3214{
3215 unsigned long max_idx;
3216
3217 do {
3218 if (!page)
3219 return NULL;
3220 if (xas_retry(xas, page))
3221 continue;
3222 if (xa_is_value(page))
3223 continue;
3224 if (PageLocked(page))
3225 continue;
3226 if (!page_cache_get_speculative(page))
3227 continue;
3228 /* Has the page moved or been split? */
3229 if (unlikely(page != xas_reload(xas)))
3230 goto skip;
3231 if (!PageUptodate(page) || PageReadahead(page))
3232 goto skip;
3233 if (PageHWPoison(page))
3234 goto skip;
3235 if (!trylock_page(page))
3236 goto skip;
3237 if (page->mapping != mapping)
3238 goto unlock;
3239 if (!PageUptodate(page))
3240 goto unlock;
3241 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
3242 if (xas->xa_index >= max_idx)
3243 goto unlock;
3244 return page;
3245unlock:
3246 unlock_page(page);
3247skip:
3248 put_page(page);
3249 } while ((page = xas_next_entry(xas, end_pgoff)) != NULL);
3250
3251 return NULL;
3252}
3253
3254static inline struct page *first_map_page(struct address_space *mapping,
3255 struct xa_state *xas,
3256 pgoff_t end_pgoff)
3257{
3258 return next_uptodate_page(xas_find(xas, end_pgoff),
3259 mapping, xas, end_pgoff);
3260}
3261
3262static inline struct page *next_map_page(struct address_space *mapping,
3263 struct xa_state *xas,
3264 pgoff_t end_pgoff)
3265{
3266 return next_uptodate_page(xas_next_entry(xas, end_pgoff),
3267 mapping, xas, end_pgoff);
3268}
3269
3270vm_fault_t filemap_map_pages(struct vm_fault *vmf,
3271 pgoff_t start_pgoff, pgoff_t end_pgoff)
3272{
3273 struct vm_area_struct *vma = vmf->vma;
3274 struct file *file = vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003275 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003276 pgoff_t last_pgoff = start_pgoff;
Will Deacon9d3af4b2021-01-14 15:24:19 +00003277 unsigned long addr;
Matthew Wilcox070e8072018-05-17 00:08:30 -04003278 XA_STATE(xas, &mapping->i_pages, start_pgoff);
Matthew Wilcox (Oracle)27a83a62020-10-13 16:51:44 -07003279 struct page *head, *page;
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07003280 unsigned int mmap_miss = READ_ONCE(file->f_ra.mmap_miss);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003281 vm_fault_t ret = 0;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003282
3283 rcu_read_lock();
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003284 head = first_map_page(mapping, &xas, end_pgoff);
3285 if (!head)
3286 goto out;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003287
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003288 if (filemap_map_pmd(vmf, head)) {
3289 ret = VM_FAULT_NOPAGE;
3290 goto out;
3291 }
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003292
Will Deacon9d3af4b2021-01-14 15:24:19 +00003293 addr = vma->vm_start + ((start_pgoff - vma->vm_pgoff) << PAGE_SHIFT);
3294 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, addr, &vmf->ptl);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003295 do {
Matthew Wilcox (Oracle)27a83a62020-10-13 16:51:44 -07003296 page = find_subpage(head, xas.xa_index);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003297 if (PageHWPoison(page))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003298 goto unlock;
3299
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07003300 if (mmap_miss > 0)
3301 mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003302
Will Deacon9d3af4b2021-01-14 15:24:19 +00003303 addr += (xas.xa_index - last_pgoff) << PAGE_SHIFT;
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003304 vmf->pte += xas.xa_index - last_pgoff;
Matthew Wilcox070e8072018-05-17 00:08:30 -04003305 last_pgoff = xas.xa_index;
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003306
3307 if (!pte_none(*vmf->pte))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003308 goto unlock;
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003309
Will Deacon46bdb422020-11-24 18:48:26 +00003310 /* We're about to handle the fault */
Will Deacon9d3af4b2021-01-14 15:24:19 +00003311 if (vmf->address == addr)
Will Deacon46bdb422020-11-24 18:48:26 +00003312 ret = VM_FAULT_NOPAGE;
Will Deacon46bdb422020-11-24 18:48:26 +00003313
Will Deacon9d3af4b2021-01-14 15:24:19 +00003314 do_set_pte(vmf, page, addr);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003315 /* no need to invalidate: a not-present page won't be cached */
Will Deacon9d3af4b2021-01-14 15:24:19 +00003316 update_mmu_cache(vma, addr, vmf->pte);
Matthew Wilcox (Oracle)27a83a62020-10-13 16:51:44 -07003317 unlock_page(head);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003318 continue;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003319unlock:
Matthew Wilcox (Oracle)27a83a62020-10-13 16:51:44 -07003320 unlock_page(head);
Matthew Wilcox (Oracle)27a83a62020-10-13 16:51:44 -07003321 put_page(head);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003322 } while ((head = next_map_page(mapping, &xas, end_pgoff)) != NULL);
3323 pte_unmap_unlock(vmf->pte, vmf->ptl);
3324out:
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003325 rcu_read_unlock();
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07003326 WRITE_ONCE(file->f_ra.mmap_miss, mmap_miss);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003327 return ret;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003328}
3329EXPORT_SYMBOL(filemap_map_pages);
3330
Souptick Joarder2bcd6452018-06-07 17:08:00 -07003331vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Jan Kara4fcf1c62012-06-12 16:20:29 +02003332{
Christoph Hellwig5df1a672020-11-16 14:33:37 +01003333 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
Jan Kara4fcf1c62012-06-12 16:20:29 +02003334 struct page *page = vmf->page;
Souptick Joarder2bcd6452018-06-07 17:08:00 -07003335 vm_fault_t ret = VM_FAULT_LOCKED;
Jan Kara4fcf1c62012-06-12 16:20:29 +02003336
Christoph Hellwig5df1a672020-11-16 14:33:37 +01003337 sb_start_pagefault(mapping->host->i_sb);
Dave Jiang11bac802017-02-24 14:56:41 -08003338 file_update_time(vmf->vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02003339 lock_page(page);
Christoph Hellwig5df1a672020-11-16 14:33:37 +01003340 if (page->mapping != mapping) {
Jan Kara4fcf1c62012-06-12 16:20:29 +02003341 unlock_page(page);
3342 ret = VM_FAULT_NOPAGE;
3343 goto out;
3344 }
Jan Kara14da9202012-06-12 16:20:37 +02003345 /*
3346 * We mark the page dirty already here so that when freeze is in
3347 * progress, we are guaranteed that writeback during freezing will
3348 * see the dirty page and writeprotect it again.
3349 */
3350 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08003351 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02003352out:
Christoph Hellwig5df1a672020-11-16 14:33:37 +01003353 sb_end_pagefault(mapping->host->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02003354 return ret;
3355}
Jan Kara4fcf1c62012-06-12 16:20:29 +02003356
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003357const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003358 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003359 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02003360 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361};
3362
3363/* This is used for a general mmap of a disk file */
3364
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -07003365int generic_file_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366{
3367 struct address_space *mapping = file->f_mapping;
3368
3369 if (!mapping->a_ops->readpage)
3370 return -ENOEXEC;
3371 file_accessed(file);
3372 vma->vm_ops = &generic_file_vm_ops;
3373 return 0;
3374}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
3376/*
3377 * This is for filesystems which do not implement ->writepage.
3378 */
3379int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
3380{
3381 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
3382 return -EINVAL;
3383 return generic_file_mmap(file, vma);
3384}
3385#else
Souptick Joarder4b96a372018-10-26 15:04:03 -07003386vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Arnd Bergmann453972282018-04-13 15:35:27 -07003387{
Souptick Joarder4b96a372018-10-26 15:04:03 -07003388 return VM_FAULT_SIGBUS;
Arnd Bergmann453972282018-04-13 15:35:27 -07003389}
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -07003390int generic_file_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391{
3392 return -ENOSYS;
3393}
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -07003394int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395{
3396 return -ENOSYS;
3397}
3398#endif /* CONFIG_MMU */
3399
Arnd Bergmann453972282018-04-13 15:35:27 -07003400EXPORT_SYMBOL(filemap_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401EXPORT_SYMBOL(generic_file_mmap);
3402EXPORT_SYMBOL(generic_file_readonly_mmap);
3403
Sasha Levin67f9fd92014-04-03 14:48:18 -07003404static struct page *wait_on_page_read(struct page *page)
3405{
3406 if (!IS_ERR(page)) {
3407 wait_on_page_locked(page);
3408 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003409 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07003410 page = ERR_PTR(-EIO);
3411 }
3412 }
3413 return page;
3414}
3415
Mel Gorman32b63522016-03-15 14:55:36 -07003416static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07003417 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07003418 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003419 void *data,
3420 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421{
Nick Piggineb2be182007-10-16 01:24:57 -07003422 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 int err;
3424repeat:
3425 page = find_get_page(mapping, index);
3426 if (!page) {
Mel Gorman453f85d2017-11-15 17:38:03 -08003427 page = __page_cache_alloc(gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07003428 if (!page)
3429 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06003430 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07003431 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003432 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07003433 if (err == -EEXIST)
3434 goto repeat;
Matthew Wilcox22ecdb42017-12-04 04:02:00 -05003435 /* Presumably ENOMEM for xarray node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 return ERR_PTR(err);
3437 }
Mel Gorman32b63522016-03-15 14:55:36 -07003438
3439filler:
Christoph Hellwig6c45b452019-07-11 20:55:20 -07003440 if (filler)
3441 err = filler(data, page);
3442 else
3443 err = mapping->a_ops->readpage(data, page);
3444
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003446 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07003447 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 }
Mel Gorman32b63522016-03-15 14:55:36 -07003449
3450 page = wait_on_page_read(page);
3451 if (IS_ERR(page))
3452 return page;
3453 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 if (PageUptodate(page))
3456 goto out;
3457
Mel Gormanebded022016-03-15 14:55:39 -07003458 /*
Miaohe Lin0e9aa672020-10-15 20:09:58 -07003459 * Page is not up to date and may be locked due to one of the following
Mel Gormanebded022016-03-15 14:55:39 -07003460 * case a: Page is being filled and the page lock is held
3461 * case b: Read/write error clearing the page uptodate status
3462 * case c: Truncation in progress (page locked)
3463 * case d: Reclaim in progress
3464 *
3465 * Case a, the page will be up to date when the page is unlocked.
3466 * There is no need to serialise on the page lock here as the page
3467 * is pinned so the lock gives no additional protection. Even if the
Randy Dunlapce89fddf2020-08-11 18:32:53 -07003468 * page is truncated, the data is still valid if PageUptodate as
Mel Gormanebded022016-03-15 14:55:39 -07003469 * it's a race vs truncate race.
3470 * Case b, the page will not be up to date
3471 * Case c, the page may be truncated but in itself, the data may still
3472 * be valid after IO completes as it's a read vs truncate race. The
3473 * operation must restart if the page is not uptodate on unlock but
3474 * otherwise serialising on page lock to stabilise the mapping gives
3475 * no additional guarantees to the caller as the page lock is
3476 * released before return.
3477 * Case d, similar to truncation. If reclaim holds the page lock, it
3478 * will be a race with remove_mapping that determines if the mapping
3479 * is valid on unlock but otherwise the data is valid and there is
3480 * no need to serialise with page lock.
3481 *
3482 * As the page lock gives no additional guarantee, we optimistically
3483 * wait on the page to be unlocked and check if it's up to date and
3484 * use the page if it is. Otherwise, the page lock is required to
3485 * distinguish between the different cases. The motivation is that we
3486 * avoid spurious serialisations and wakeups when multiple processes
3487 * wait on the same page for IO to complete.
3488 */
3489 wait_on_page_locked(page);
3490 if (PageUptodate(page))
3491 goto out;
3492
3493 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07003495
3496 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 if (!page->mapping) {
3498 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003499 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07003500 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 }
Mel Gormanebded022016-03-15 14:55:39 -07003502
3503 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 if (PageUptodate(page)) {
3505 unlock_page(page);
3506 goto out;
3507 }
Xianting Tianfaffdfa2020-04-01 21:04:47 -07003508
3509 /*
3510 * A previous I/O error may have been due to temporary
3511 * failures.
3512 * Clear page error before actual read, PG_error will be
3513 * set again if read page fails.
3514 */
3515 ClearPageError(page);
Mel Gorman32b63522016-03-15 14:55:36 -07003516 goto filler;
3517
David Howellsc855ff32007-05-09 13:42:20 +01003518out:
Nick Piggin6fe69002007-05-06 14:49:04 -07003519 mark_page_accessed(page);
3520 return page;
3521}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003522
3523/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07003524 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003525 * @mapping: the page's address_space
3526 * @index: the page index
3527 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07003528 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003529 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003530 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07003531 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003532 *
3533 * If the page does not get brought uptodate, return -EIO.
Mike Rapoporta862f682019-03-05 15:48:42 -08003534 *
Jan Kara730633f2021-01-28 19:19:45 +01003535 * The function expects mapping->invalidate_lock to be already held.
3536 *
Mike Rapoporta862f682019-03-05 15:48:42 -08003537 * Return: up to date page on success, ERR_PTR() on failure.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003538 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07003539struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003540 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07003541 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003542 void *data)
3543{
Christoph Hellwigd322a8e2019-07-11 20:55:17 -07003544 return do_read_cache_page(mapping, index, filler, data,
3545 mapping_gfp_mask(mapping));
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003546}
Sasha Levin67f9fd92014-04-03 14:48:18 -07003547EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003548
3549/**
3550 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
3551 * @mapping: the page's address_space
3552 * @index: the page index
3553 * @gfp: the page allocator flags to use if allocating
3554 *
3555 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06003556 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003557 *
3558 * If the page does not get brought uptodate, return -EIO.
Mike Rapoporta862f682019-03-05 15:48:42 -08003559 *
Jan Kara730633f2021-01-28 19:19:45 +01003560 * The function expects mapping->invalidate_lock to be already held.
3561 *
Mike Rapoporta862f682019-03-05 15:48:42 -08003562 * Return: up to date page on success, ERR_PTR() on failure.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003563 */
3564struct page *read_cache_page_gfp(struct address_space *mapping,
3565 pgoff_t index,
3566 gfp_t gfp)
3567{
Christoph Hellwig6c45b452019-07-11 20:55:20 -07003568 return do_read_cache_page(mapping, index, NULL, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003569}
3570EXPORT_SYMBOL(read_cache_page_gfp);
3571
Nick Pigginafddba42007-10-16 01:25:01 -07003572int pagecache_write_begin(struct file *file, struct address_space *mapping,
3573 loff_t pos, unsigned len, unsigned flags,
3574 struct page **pagep, void **fsdata)
3575{
3576 const struct address_space_operations *aops = mapping->a_ops;
3577
Nick Piggin4e02ed42008-10-29 14:00:55 -07003578 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003579 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003580}
3581EXPORT_SYMBOL(pagecache_write_begin);
3582
3583int pagecache_write_end(struct file *file, struct address_space *mapping,
3584 loff_t pos, unsigned len, unsigned copied,
3585 struct page *page, void *fsdata)
3586{
3587 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07003588
Nick Piggin4e02ed42008-10-29 14:00:55 -07003589 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003590}
3591EXPORT_SYMBOL(pagecache_write_end);
3592
Konstantin Khlebnikova92853b2019-11-30 17:49:44 -08003593/*
3594 * Warn about a page cache invalidation failure during a direct I/O write.
3595 */
3596void dio_warn_stale_pagecache(struct file *filp)
3597{
3598 static DEFINE_RATELIMIT_STATE(_rs, 86400 * HZ, DEFAULT_RATELIMIT_BURST);
3599 char pathname[128];
Konstantin Khlebnikova92853b2019-11-30 17:49:44 -08003600 char *path;
3601
Christoph Hellwig5df1a672020-11-16 14:33:37 +01003602 errseq_set(&filp->f_mapping->wb_err, -EIO);
Konstantin Khlebnikova92853b2019-11-30 17:49:44 -08003603 if (__ratelimit(&_rs)) {
3604 path = file_path(filp, pathname, sizeof(pathname));
3605 if (IS_ERR(path))
3606 path = "(unknown)";
3607 pr_crit("Page cache invalidation failure on direct I/O. Possible data corruption due to collision with buffered I/O!\n");
3608 pr_crit("File: %s PID: %d Comm: %.20s\n", path, current->pid,
3609 current->comm);
3610 }
3611}
3612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003614generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615{
3616 struct file *file = iocb->ki_filp;
3617 struct address_space *mapping = file->f_mapping;
3618 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003619 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003621 size_t write_len;
3622 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
Al Viro0c949332014-03-22 06:51:37 -04003624 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003625 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003626
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003627 if (iocb->ki_flags & IOCB_NOWAIT) {
3628 /* If there are pages to writeback, return */
Christoph Hellwig5df1a672020-11-16 14:33:37 +01003629 if (filemap_range_has_page(file->f_mapping, pos,
zhengbin35f12f02019-03-05 15:44:21 -08003630 pos + write_len - 1))
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003631 return -EAGAIN;
3632 } else {
3633 written = filemap_write_and_wait_range(mapping, pos,
3634 pos + write_len - 1);
3635 if (written)
3636 goto out;
3637 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07003638
3639 /*
3640 * After a write we want buffered reads to be sure to go to disk to get
3641 * the new data. We invalidate clean cached page from the region we're
3642 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07003643 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07003644 */
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003645 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003646 pos >> PAGE_SHIFT, end);
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003647 /*
3648 * If a page can not be invalidated, return 0 to fall back
3649 * to buffered write.
3650 */
3651 if (written) {
3652 if (written == -EBUSY)
3653 return 0;
3654 goto out;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003655 }
3656
Al Viro639a93a52017-04-13 14:10:15 -04003657 written = mapping->a_ops->direct_IO(iocb, from);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003658
3659 /*
3660 * Finally, try again to invalidate clean pages which might have been
3661 * cached by non-direct readahead, or faulted in by get_user_pages()
3662 * if the source of the write was an mmap'ed region of the file
3663 * we're writing. Either one is a pretty crazy thing to do,
3664 * so we don't support it 100%. If this invalidation
3665 * fails, tough, the write still worked...
Lukas Czerner332391a2017-09-21 08:16:29 -06003666 *
3667 * Most of the time we do not need this since dio_complete() will do
3668 * the invalidation for us. However there are some file systems that
3669 * do not end up with dio_complete() being called, so let's not break
Konstantin Khlebnikov80c1fe92019-11-30 17:49:41 -08003670 * them by removing it completely.
3671 *
Konstantin Khlebnikov9266a142019-11-30 17:49:47 -08003672 * Noticeable example is a blkdev_direct_IO().
3673 *
Konstantin Khlebnikov80c1fe92019-11-30 17:49:41 -08003674 * Skip invalidation for async writes or if mapping has no pages.
Christoph Hellwiga969e902008-07-23 21:27:04 -07003675 */
Konstantin Khlebnikov9266a142019-11-30 17:49:47 -08003676 if (written > 0 && mapping->nrpages &&
3677 invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT, end))
3678 dio_warn_stale_pagecache(file);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003679
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07003681 pos += written;
Al Viro639a93a52017-04-13 14:10:15 -04003682 write_len -= written;
Namhyung Kim01166512010-10-26 14:21:58 -07003683 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
3684 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 mark_inode_dirty(inode);
3686 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05003687 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 }
Pavel Begunkovab2125d2021-02-24 12:01:45 -08003689 if (written != -EIOCBQUEUED)
3690 iov_iter_revert(from, write_len - iov_iter_count(from));
Christoph Hellwiga969e902008-07-23 21:27:04 -07003691out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 return written;
3693}
3694EXPORT_SYMBOL(generic_file_direct_write);
3695
Nick Piggineb2be182007-10-16 01:24:57 -07003696/*
3697 * Find or create a page at the given pagecache position. Return the locked
3698 * page. This function is specifically for buffered writes.
3699 */
Nick Piggin54566b22009-01-04 12:00:53 -08003700struct page *grab_cache_page_write_begin(struct address_space *mapping,
3701 pgoff_t index, unsigned flags)
Nick Piggineb2be182007-10-16 01:24:57 -07003702{
Nick Piggineb2be182007-10-16 01:24:57 -07003703 struct page *page;
Johannes Weinerbbddabe2016-05-20 16:56:28 -07003704 int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
Johannes Weiner0faa70c2012-01-10 15:07:53 -08003705
Nick Piggin54566b22009-01-04 12:00:53 -08003706 if (flags & AOP_FLAG_NOFS)
Mel Gorman2457aec2014-06-04 16:10:31 -07003707 fgp_flags |= FGP_NOFS;
Nick Piggineb2be182007-10-16 01:24:57 -07003708
Mel Gorman2457aec2014-06-04 16:10:31 -07003709 page = pagecache_get_page(mapping, index, fgp_flags,
Michal Hocko45f87de2014-12-29 20:30:35 +01003710 mapping_gfp_mask(mapping));
Mel Gorman2457aec2014-06-04 16:10:31 -07003711 if (page)
3712 wait_for_stable_page(page);
3713
Nick Piggineb2be182007-10-16 01:24:57 -07003714 return page;
3715}
Nick Piggin54566b22009-01-04 12:00:53 -08003716EXPORT_SYMBOL(grab_cache_page_write_begin);
Nick Piggineb2be182007-10-16 01:24:57 -07003717
Al Viro3b93f912014-02-11 21:34:08 -05003718ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07003719 struct iov_iter *i, loff_t pos)
3720{
3721 struct address_space *mapping = file->f_mapping;
3722 const struct address_space_operations *a_ops = mapping->a_ops;
3723 long status = 0;
3724 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07003725 unsigned int flags = 0;
3726
Nick Pigginafddba42007-10-16 01:25:01 -07003727 do {
3728 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003729 unsigned long offset; /* Offset into pagecache page */
3730 unsigned long bytes; /* Bytes to write to page */
3731 size_t copied; /* Bytes copied from user */
3732 void *fsdata;
3733
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003734 offset = (pos & (PAGE_SIZE - 1));
3735 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003736 iov_iter_count(i));
3737
3738again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01003739 /*
3740 * Bring in the user page that we will copy from _first_.
3741 * Otherwise there's a nasty deadlock on copying from the
3742 * same page as we're writing to, without it being marked
3743 * up-to-date.
Linus Torvalds00a3d662015-10-07 08:32:38 +01003744 */
3745 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
3746 status = -EFAULT;
3747 break;
3748 }
3749
Jan Kara296291c2015-10-22 13:32:21 -07003750 if (fatal_signal_pending(current)) {
3751 status = -EINTR;
3752 break;
3753 }
3754
Nick Piggin674b8922007-10-16 01:25:03 -07003755 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003756 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07003757 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07003758 break;
3759
anfei zhou931e80e2010-02-02 13:44:02 -08003760 if (mapping_writably_mapped(mapping))
3761 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01003762
Al Virof0b65f32021-04-30 10:26:41 -04003763 copied = copy_page_from_iter_atomic(page, offset, bytes, i);
Nick Pigginafddba42007-10-16 01:25:01 -07003764 flush_dcache_page(page);
3765
3766 status = a_ops->write_end(file, mapping, pos, bytes, copied,
3767 page, fsdata);
Al Virof0b65f32021-04-30 10:26:41 -04003768 if (unlikely(status != copied)) {
3769 iov_iter_revert(i, copied - max(status, 0L));
3770 if (unlikely(status < 0))
3771 break;
3772 }
Nick Pigginafddba42007-10-16 01:25:01 -07003773 cond_resched();
3774
Al Virobc1bb412021-05-31 00:32:44 -04003775 if (unlikely(status == 0)) {
Nick Pigginafddba42007-10-16 01:25:01 -07003776 /*
Al Virobc1bb412021-05-31 00:32:44 -04003777 * A short copy made ->write_end() reject the
3778 * thing entirely. Might be memory poisoning
3779 * halfway through, might be a race with munmap,
3780 * might be severe memory pressure.
Nick Pigginafddba42007-10-16 01:25:01 -07003781 */
Al Virobc1bb412021-05-31 00:32:44 -04003782 if (copied)
3783 bytes = copied;
Nick Pigginafddba42007-10-16 01:25:01 -07003784 goto again;
3785 }
Al Virof0b65f32021-04-30 10:26:41 -04003786 pos += status;
3787 written += status;
Nick Pigginafddba42007-10-16 01:25:01 -07003788
3789 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07003790 } while (iov_iter_count(i));
3791
3792 return written ? written : status;
3793}
Al Viro3b93f912014-02-11 21:34:08 -05003794EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
Jan Karae4dd9de2009-08-17 18:10:06 +02003796/**
Al Viro81742022014-04-03 03:17:43 -04003797 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003798 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04003799 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003800 *
3801 * This function does all the work needed for actually writing data to a
3802 * file. It does all basic checks, removes SUID from the file, updates
3803 * modification times and calls proper subroutines depending on whether we
3804 * do direct IO or a standard buffered write.
3805 *
Jan Kara96087032021-04-12 15:50:21 +02003806 * It expects i_rwsem to be grabbed unless we work on a block device or similar
Jan Karae4dd9de2009-08-17 18:10:06 +02003807 * object which does not need locking at all.
3808 *
3809 * This function does *not* take care of syncing data in case of O_SYNC write.
3810 * A caller has to handle it. This is mainly due to the fact that we want to
Jan Kara96087032021-04-12 15:50:21 +02003811 * avoid syncing under i_rwsem.
Mike Rapoporta862f682019-03-05 15:48:42 -08003812 *
3813 * Return:
3814 * * number of bytes written, even for truncated writes
3815 * * negative error code if no data has been written at all
Jan Karae4dd9de2009-08-17 18:10:06 +02003816 */
Al Viro81742022014-04-03 03:17:43 -04003817ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818{
3819 struct file *file = iocb->ki_filp;
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -07003820 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003822 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003824 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003827 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003828 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 if (err)
3830 goto out;
3831
Josef Bacikc3b2da32012-03-26 09:59:21 -04003832 err = file_update_time(file);
3833 if (err)
3834 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
Al Viro2ba48ce2015-04-09 13:52:01 -04003836 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003837 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003838
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003839 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003840 /*
3841 * If the write stopped short of completing, fall back to
3842 * buffered writes. Some filesystems do this for writes to
3843 * holes, for example. For DAX files, a buffered write will
3844 * not succeed (even if it did, DAX does not handle dirty
3845 * page-cache pages correctly).
3846 */
Al Viro0b8def92015-04-07 10:22:53 -04003847 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003849
Al Viro0b8def92015-04-07 10:22:53 -04003850 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003851 /*
Al Viro3b93f912014-02-11 21:34:08 -05003852 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003853 * then we want to return the number of bytes which were
3854 * direct-written, or the error code if that was zero. Note
3855 * that this differs from normal direct-io semantics, which
3856 * will return -EFOO even if some bytes were written.
3857 */
Al Viro60bb4522014-08-08 12:39:16 -04003858 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003859 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003860 goto out;
3861 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003862 /*
3863 * We need to ensure that the page cache pages are written to
3864 * disk and invalidated to preserve the expected O_DIRECT
3865 * semantics.
3866 */
Al Viro3b93f912014-02-11 21:34:08 -05003867 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003868 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003869 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003870 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003871 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003872 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003873 pos >> PAGE_SHIFT,
3874 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003875 } else {
3876 /*
3877 * We don't know how much we wrote, so just return
3878 * the number of bytes which were direct-written
3879 */
3880 }
3881 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003882 written = generic_perform_write(file, from, iocb->ki_pos);
3883 if (likely(written > 0))
3884 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886out:
3887 current->backing_dev_info = NULL;
3888 return written ? written : err;
3889}
Al Viro81742022014-04-03 03:17:43 -04003890EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Jan Karae4dd9de2009-08-17 18:10:06 +02003892/**
Al Viro81742022014-04-03 03:17:43 -04003893 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003894 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003895 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003896 *
Al Viro81742022014-04-03 03:17:43 -04003897 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003898 * filesystems. It takes care of syncing the file in case of O_SYNC file
Jan Kara96087032021-04-12 15:50:21 +02003899 * and acquires i_rwsem as needed.
Mike Rapoporta862f682019-03-05 15:48:42 -08003900 * Return:
3901 * * negative error code if no data has been written at all of
3902 * vfs_fsync_range() failed for a synchronous write
3903 * * number of bytes written, even for truncated writes
Jan Karae4dd9de2009-08-17 18:10:06 +02003904 */
Al Viro81742022014-04-03 03:17:43 -04003905ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906{
3907 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003908 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
Al Viro59551022016-01-22 15:40:57 -05003911 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003912 ret = generic_write_checks(iocb, from);
3913 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003914 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003915 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916
Christoph Hellwige2592212016-04-07 08:52:01 -07003917 if (ret > 0)
3918 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 return ret;
3920}
Al Viro81742022014-04-03 03:17:43 -04003921EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
David Howellscf9a2ae2006-08-29 19:05:54 +01003923/**
3924 * try_to_release_page() - release old fs-specific metadata on a page
3925 *
3926 * @page: the page which the kernel is trying to free
3927 * @gfp_mask: memory allocation flags (and I/O mode)
3928 *
3929 * The address_space is to try to release any data against the page
Mike Rapoporta862f682019-03-05 15:48:42 -08003930 * (presumably at page->private).
David Howellscf9a2ae2006-08-29 19:05:54 +01003931 *
David Howells266cf652009-04-03 16:42:36 +01003932 * This may also be called if PG_fscache is set on a page, indicating that the
3933 * page is known to the local caching routines.
3934 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003935 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08003936 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01003937 *
Mike Rapoporta862f682019-03-05 15:48:42 -08003938 * Return: %1 if the release was successful, otherwise return zero.
David Howellscf9a2ae2006-08-29 19:05:54 +01003939 */
3940int try_to_release_page(struct page *page, gfp_t gfp_mask)
3941{
3942 struct address_space * const mapping = page->mapping;
3943
3944 BUG_ON(!PageLocked(page));
3945 if (PageWriteback(page))
3946 return 0;
3947
3948 if (mapping && mapping->a_ops->releasepage)
3949 return mapping->a_ops->releasepage(page, gfp_mask);
3950 return try_to_free_buffers(page);
3951}
3952
3953EXPORT_SYMBOL(try_to_release_page);