blob: bcdc8bb4d2c8cc59fc12ccd636f8ef1b1d1d4742 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/security.h>
Paul Jackson44110fe2006-03-24 03:16:04 -080034#include <linux/cpuset.h>
Johannes Weiner00501b52014-08-08 14:19:20 -070035#include <linux/hugetlb.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080036#include <linux/memcontrol.h>
Dan Magenheimerc515e1f2011-05-26 10:01:43 -060037#include <linux/cleancache.h>
Mel Gormanc7df8ad2017-11-15 17:37:41 -080038#include <linux/shmem_fs.h>
Kirill A. Shutemovf1820362014-04-07 15:37:19 -070039#include <linux/rmap.h>
Johannes Weinerb1d29ba2018-10-26 15:06:08 -070040#include <linux/delayacct.h>
Johannes Weinereb414682018-10-26 15:06:27 -070041#include <linux/psi.h>
Ben Dooksd0e6a582019-10-18 20:20:20 -070042#include <linux/ramfs.h>
Yang Shib9306a72020-08-06 23:19:55 -070043#include <linux/page_idle.h>
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +030044#include <asm/pgalloc.h>
Will Deaconde591a82021-02-10 11:15:11 +000045#include <asm/tlbflush.h>
Nick Piggin0f8053a2006-03-22 00:08:33 -080046#include "internal.h"
47
Robert Jarzmikfe0bfaa2013-04-29 15:06:10 -070048#define CREATE_TRACE_POINTS
49#include <trace/events/filemap.h>
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * FIXME: remove all knowledge of the buffer layer from the core VM
53 */
Jan Kara148f9482009-08-17 19:52:36 +020054#include <linux/buffer_head.h> /* for try_to_free_buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/mman.h>
57
58/*
59 * Shared mappings implemented 30.11.1994. It's not fully working yet,
60 * though.
61 *
62 * Shared mappings now work. 15.8.1995 Bruno.
63 *
64 * finished 'unifying' the page and buffer cache and SMP-threaded the
65 * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
66 *
67 * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
68 */
69
70/*
71 * Lock ordering:
72 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080073 * ->i_mmap_rwsem (truncate_pagecache)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * ->private_lock (__free_pte->__set_page_dirty_buffers)
Hugh Dickins5d337b92005-09-03 15:54:41 -070075 * ->swap_lock (exclusive_swap_page, others)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070076 * ->i_pages lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 *
Jan Kara96087032021-04-12 15:50:21 +020078 * ->i_rwsem
Jan Kara730633f2021-01-28 19:19:45 +010079 * ->invalidate_lock (acquired by fs in truncate path)
80 * ->i_mmap_rwsem (truncate->unmap_mapping_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070082 * ->mmap_lock
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080083 * ->i_mmap_rwsem
Hugh Dickinsb8072f02005-10-29 18:16:41 -070084 * ->page_table_lock or pte_lock (various, mainly in memory.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070085 * ->i_pages lock (arch-dependent flush_dcache_mmap_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070087 * ->mmap_lock
Jan Kara730633f2021-01-28 19:19:45 +010088 * ->invalidate_lock (filemap_fault)
89 * ->lock_page (filemap_fault, access_process_vm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 *
Jan Kara96087032021-04-12 15:50:21 +020091 * ->i_rwsem (generic_perform_write)
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +020092 * ->mmap_lock (fault_in_readable->do_page_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060094 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110095 * sb_lock (fs/fs-writeback.c)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -070096 * ->i_pages lock (__sync_single_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -080098 * ->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * ->anon_vma.lock (vma_adjust)
100 *
101 * ->anon_vma.lock
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700102 * ->page_table_lock or pte_lock (anon_vma_prepare and various)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700104 * ->page_table_lock or pte_lock
Hugh Dickins5d337b92005-09-03 15:54:41 -0700105 * ->swap_lock (try_to_unmap_one)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * ->private_lock (try_to_unmap_one)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700107 * ->i_pages lock (try_to_unmap_one)
Hugh Dickins15b44732020-12-15 14:21:31 -0800108 * ->lruvec->lru_lock (follow_page->mark_page_accessed)
109 * ->lruvec->lru_lock (check_pte_range->isolate_lru_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * ->private_lock (page_remove_rmap->set_page_dirty)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700111 * ->i_pages lock (page_remove_rmap->set_page_dirty)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600112 * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100113 * ->inode->i_lock (page_remove_rmap->set_page_dirty)
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700114 * ->memcg->move_lock (page_remove_rmap->lock_page_memcg)
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600115 * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
Dave Chinner250df6e2011-03-22 22:23:36 +1100116 * ->inode->i_lock (zap_pte_range->set_page_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * ->private_lock (zap_pte_range->__set_page_dirty_buffers)
118 *
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800119 * ->i_mmap_rwsem
Andi Kleen9a3c5312012-03-21 16:34:09 -0700120 * ->tasklist_lock (memory_failure, collect_procs_ao)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
122
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500123static void page_cache_delete(struct address_space *mapping,
Matthew Wilcox (Oracle)a548b612021-05-08 00:35:49 -0400124 struct folio *folio, void *shadow)
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700125{
Matthew Wilcox (Oracle)a548b612021-05-08 00:35:49 -0400126 XA_STATE(xas, &mapping->i_pages, folio->index);
127 long nr = 1;
Kirill A. Shutemovc70b6472016-12-12 16:43:17 -0800128
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500129 mapping_set_update(&xas, mapping);
130
131 /* hugetlb pages are represented by a single entry in the xarray */
Matthew Wilcox (Oracle)a548b612021-05-08 00:35:49 -0400132 if (!folio_test_hugetlb(folio)) {
133 xas_set_order(&xas, folio->index, folio_order(folio));
134 nr = folio_nr_pages(folio);
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500135 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700136
Matthew Wilcox (Oracle)a548b612021-05-08 00:35:49 -0400137 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
138 VM_BUG_ON_FOLIO(nr != 1 && shadow, folio);
Johannes Weiner449dd692014-04-03 14:47:56 -0700139
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500140 xas_store(&xas, shadow);
141 xas_init_marks(&xas);
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200142
Matthew Wilcox (Oracle)a548b612021-05-08 00:35:49 -0400143 folio->mapping = NULL;
Jan Kara23006382017-11-15 17:37:26 -0800144 /* Leave page->index set: truncation lookup relies upon it */
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200145 mapping->nrpages -= nr;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700146}
147
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400148static void filemap_unaccount_folio(struct address_space *mapping,
149 struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400151 long nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600153 /*
154 * if we're uptodate, flush out into the cleancache, otherwise
155 * invalidate any existing cleancache entries. We can't leave
156 * stale data around in the cleancache once our page is gone
157 */
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400158 if (folio_test_uptodate(folio) && folio_test_mappedtodisk(folio))
159 cleancache_put_page(&folio->page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600160 else
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400161 cleancache_invalidate_page(mapping, &folio->page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600162
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400163 VM_BUG_ON_FOLIO(folio_mapped(folio), folio);
164 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(folio_mapped(folio))) {
Hugh Dickins06b241f2016-03-09 14:08:07 -0800165 int mapcount;
166
167 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400168 current->comm, folio_pfn(folio));
169 dump_page(&folio->page, "still mapped when deleted");
Hugh Dickins06b241f2016-03-09 14:08:07 -0800170 dump_stack();
171 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
172
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400173 mapcount = page_mapcount(&folio->page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800174 if (mapping_exiting(mapping) &&
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400175 folio_ref_count(folio) >= mapcount + 2) {
Hugh Dickins06b241f2016-03-09 14:08:07 -0800176 /*
177 * All vmas have already been torn down, so it's
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400178 * a good bet that actually the folio is unmapped,
Hugh Dickins06b241f2016-03-09 14:08:07 -0800179 * and we'd prefer not to leak it: if we're wrong,
180 * some other bad page check should catch it later.
181 */
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400182 page_mapcount_reset(&folio->page);
183 folio_ref_sub(folio, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800184 }
185 }
186
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400187 /* hugetlb folios do not participate in page cache accounting. */
188 if (folio_test_hugetlb(folio))
Jan Kara5ecc4d82017-11-15 17:37:29 -0800189 return;
Jan Kara76253fb2017-11-15 17:37:22 -0800190
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400191 nr = folio_nr_pages(folio);
Jan Kara5ecc4d82017-11-15 17:37:29 -0800192
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400193 __lruvec_stat_mod_folio(folio, NR_FILE_PAGES, -nr);
194 if (folio_test_swapbacked(folio)) {
195 __lruvec_stat_mod_folio(folio, NR_SHMEM, -nr);
196 if (folio_test_pmd_mappable(folio))
197 __lruvec_stat_mod_folio(folio, NR_SHMEM_THPS, -nr);
198 } else if (folio_test_pmd_mappable(folio)) {
199 __lruvec_stat_mod_folio(folio, NR_FILE_THPS, -nr);
Song Liu09d91cd2019-09-23 15:38:03 -0700200 filemap_nr_thps_dec(mapping);
Jan Kara76253fb2017-11-15 17:37:22 -0800201 }
Jan Kara5ecc4d82017-11-15 17:37:29 -0800202
203 /*
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400204 * At this point folio must be either written or cleaned by
205 * truncate. Dirty folio here signals a bug and loss of
Jan Kara5ecc4d82017-11-15 17:37:29 -0800206 * unwritten data.
207 *
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400208 * This fixes dirty accounting after removing the folio entirely
209 * but leaves the dirty flag set: it has no effect for truncated
210 * folio and anyway will be cleared before returning folio to
Jan Kara5ecc4d82017-11-15 17:37:29 -0800211 * buddy allocator.
212 */
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400213 if (WARN_ON_ONCE(folio_test_dirty(folio)))
214 folio_account_cleaned(folio, mapping,
215 inode_to_wb(mapping->host));
Jan Kara5ecc4d82017-11-15 17:37:29 -0800216}
217
218/*
219 * Delete a page from the page cache and free it. Caller has to make
220 * sure the page is locked and that nobody else uses it - or that usage
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700221 * is safe. The caller must hold the i_pages lock.
Jan Kara5ecc4d82017-11-15 17:37:29 -0800222 */
223void __delete_from_page_cache(struct page *page, void *shadow)
224{
Matthew Wilcox (Oracle)a548b612021-05-08 00:35:49 -0400225 struct folio *folio = page_folio(page);
Jan Kara5ecc4d82017-11-15 17:37:29 -0800226 struct address_space *mapping = page->mapping;
227
Matthew Wilcox (Oracle)a0580c62021-07-23 09:29:46 -0400228 trace_mm_filemap_delete_from_page_cache(folio);
Jan Kara5ecc4d82017-11-15 17:37:29 -0800229
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400230 filemap_unaccount_folio(mapping, folio);
Matthew Wilcox (Oracle)a548b612021-05-08 00:35:49 -0400231 page_cache_delete(mapping, folio, shadow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Jan Kara59c66c52017-11-15 17:37:18 -0800234static void page_cache_free_page(struct address_space *mapping,
235 struct page *page)
236{
237 void (*freepage)(struct page *);
238
239 freepage = mapping->a_ops->freepage;
240 if (freepage)
241 freepage(page);
242
243 if (PageTransHuge(page) && !PageHuge(page)) {
Matthew Wilcox (Oracle)887b22c2020-10-15 20:05:23 -0700244 page_ref_sub(page, thp_nr_pages(page));
Jan Kara59c66c52017-11-15 17:37:18 -0800245 VM_BUG_ON_PAGE(page_count(page) <= 0, page);
246 } else {
247 put_page(page);
248 }
249}
250
Minchan Kim702cfbf2011-03-22 16:32:43 -0700251/**
252 * delete_from_page_cache - delete page from page cache
253 * @page: the page which the kernel is trying to remove from page cache
254 *
255 * This must be called only on pages that have been verified to be in the page
256 * cache and locked. It will never put the page into the free list, the caller
257 * has a reference on the page.
258 */
259void delete_from_page_cache(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Kirill A. Shutemov83929372016-07-26 15:26:04 -0700261 struct address_space *mapping = page_mapping(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Matt Mackallcd7619d2005-05-01 08:59:01 -0700263 BUG_ON(!PageLocked(page));
Johannes Weiner51b8c1f2021-11-08 18:31:24 -0800264 spin_lock(&mapping->host->i_lock);
Johannes Weiner30472502021-09-02 14:53:18 -0700265 xa_lock_irq(&mapping->i_pages);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700266 __delete_from_page_cache(page, NULL);
Johannes Weiner30472502021-09-02 14:53:18 -0700267 xa_unlock_irq(&mapping->i_pages);
Johannes Weiner51b8c1f2021-11-08 18:31:24 -0800268 if (mapping_shrinkable(mapping))
269 inode_add_lru(mapping->host);
270 spin_unlock(&mapping->host->i_lock);
Linus Torvalds6072d132010-12-01 13:35:19 -0500271
Jan Kara59c66c52017-11-15 17:37:18 -0800272 page_cache_free_page(mapping, page);
Minchan Kim97cecb52011-03-22 16:30:53 -0700273}
274EXPORT_SYMBOL(delete_from_page_cache);
275
Jan Karaaa65c292017-11-15 17:37:33 -0800276/*
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500277 * page_cache_delete_batch - delete several pages from page cache
Jan Karaaa65c292017-11-15 17:37:33 -0800278 * @mapping: the mapping to which pages belong
279 * @pvec: pagevec with pages to delete
280 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700281 * The function walks over mapping->i_pages and removes pages passed in @pvec
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700282 * from the mapping. The function expects @pvec to be sorted by page index
283 * and is optimised for it to be dense.
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700284 * It tolerates holes in @pvec (mapping entries at those indices are not
Jan Karaaa65c292017-11-15 17:37:33 -0800285 * modified). The function expects only THP head pages to be present in the
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700286 * @pvec.
Jan Karaaa65c292017-11-15 17:37:33 -0800287 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700288 * The function expects the i_pages lock to be held.
Jan Karaaa65c292017-11-15 17:37:33 -0800289 */
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500290static void page_cache_delete_batch(struct address_space *mapping,
Jan Karaaa65c292017-11-15 17:37:33 -0800291 struct pagevec *pvec)
292{
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500293 XA_STATE(xas, &mapping->i_pages, pvec->pages[0]->index);
Jan Karaaa65c292017-11-15 17:37:33 -0800294 int total_pages = 0;
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700295 int i = 0;
Jan Karaaa65c292017-11-15 17:37:33 -0800296 struct page *page;
Jan Karaaa65c292017-11-15 17:37:33 -0800297
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500298 mapping_set_update(&xas, mapping);
299 xas_for_each(&xas, page, ULONG_MAX) {
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700300 if (i >= pagevec_count(pvec))
Jan Karaaa65c292017-11-15 17:37:33 -0800301 break;
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700302
303 /* A swap/dax/shadow entry got inserted? Skip it. */
Matthew Wilcox3159f942017-11-03 13:30:42 -0400304 if (xa_is_value(page))
Jan Karaaa65c292017-11-15 17:37:33 -0800305 continue;
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700306 /*
307 * A page got inserted in our range? Skip it. We have our
308 * pages locked so they are protected from being removed.
309 * If we see a page whose index is higher than ours, it
310 * means our page has been removed, which shouldn't be
311 * possible because we're holding the PageLock.
312 */
313 if (page != pvec->pages[i]) {
314 VM_BUG_ON_PAGE(page->index > pvec->pages[i]->index,
315 page);
316 continue;
Linus Torvalds69bf4b62019-07-05 19:55:18 -0700317 }
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700318
319 WARN_ON_ONCE(!PageLocked(page));
320
321 if (page->index == xas.xa_index)
322 page->mapping = NULL;
323 /* Leave page->index set: truncation lookup relies on it */
324
325 /*
326 * Move to the next page in the vector if this is a regular
327 * page or the index is of the last sub-page of this compound
328 * page.
329 */
330 if (page->index + compound_nr(page) - 1 == xas.xa_index)
331 i++;
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500332 xas_store(&xas, NULL);
Jan Karaaa65c292017-11-15 17:37:33 -0800333 total_pages++;
334 }
335 mapping->nrpages -= total_pages;
336}
337
338void delete_from_page_cache_batch(struct address_space *mapping,
339 struct pagevec *pvec)
340{
341 int i;
Jan Karaaa65c292017-11-15 17:37:33 -0800342
343 if (!pagevec_count(pvec))
344 return;
345
Johannes Weiner51b8c1f2021-11-08 18:31:24 -0800346 spin_lock(&mapping->host->i_lock);
Johannes Weiner30472502021-09-02 14:53:18 -0700347 xa_lock_irq(&mapping->i_pages);
Jan Karaaa65c292017-11-15 17:37:33 -0800348 for (i = 0; i < pagevec_count(pvec); i++) {
Matthew Wilcox (Oracle)a0580c62021-07-23 09:29:46 -0400349 struct folio *folio = page_folio(pvec->pages[i]);
Jan Karaaa65c292017-11-15 17:37:33 -0800350
Matthew Wilcox (Oracle)a0580c62021-07-23 09:29:46 -0400351 trace_mm_filemap_delete_from_page_cache(folio);
352 filemap_unaccount_folio(mapping, folio);
Jan Karaaa65c292017-11-15 17:37:33 -0800353 }
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500354 page_cache_delete_batch(mapping, pvec);
Johannes Weiner30472502021-09-02 14:53:18 -0700355 xa_unlock_irq(&mapping->i_pages);
Johannes Weiner51b8c1f2021-11-08 18:31:24 -0800356 if (mapping_shrinkable(mapping))
357 inode_add_lru(mapping->host);
358 spin_unlock(&mapping->host->i_lock);
Jan Karaaa65c292017-11-15 17:37:33 -0800359
360 for (i = 0; i < pagevec_count(pvec); i++)
361 page_cache_free_page(mapping, pvec->pages[i]);
362}
363
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200364int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700365{
366 int ret = 0;
367 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700368 if (test_bit(AS_ENOSPC, &mapping->flags) &&
369 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700370 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700371 if (test_bit(AS_EIO, &mapping->flags) &&
372 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700373 ret = -EIO;
374 return ret;
375}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200376EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700377
Jeff Layton76341ca2017-07-06 07:02:22 -0400378static int filemap_check_and_keep_errors(struct address_space *mapping)
379{
380 /* Check for outstanding write errors */
381 if (test_bit(AS_EIO, &mapping->flags))
382 return -EIO;
383 if (test_bit(AS_ENOSPC, &mapping->flags))
384 return -ENOSPC;
385 return 0;
386}
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/**
Josef Bacik5a798492021-07-14 14:47:22 -0400389 * filemap_fdatawrite_wbc - start writeback on mapping dirty pages in range
390 * @mapping: address space structure to write
391 * @wbc: the writeback_control controlling the writeout
392 *
393 * Call writepages on the mapping using the provided wbc to control the
394 * writeout.
395 *
396 * Return: %0 on success, negative error code otherwise.
397 */
398int filemap_fdatawrite_wbc(struct address_space *mapping,
399 struct writeback_control *wbc)
400{
401 int ret;
402
403 if (!mapping_can_writeback(mapping) ||
404 !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
405 return 0;
406
407 wbc_attach_fdatawrite_inode(wbc, mapping->host);
408 ret = do_writepages(mapping, wbc);
409 wbc_detach_inode(wbc);
410 return ret;
411}
412EXPORT_SYMBOL(filemap_fdatawrite_wbc);
413
414/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700415 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700416 * @mapping: address space structure to write
417 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800418 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700419 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700421 * Start writeback against all of a mapping's dirty pages that lie
422 * within the byte offsets <start, end> inclusive.
423 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700425 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * these two operations is that if a dirty page/buffer is encountered, it must
427 * be waited upon, and not just skipped over.
Mike Rapoporta862f682019-03-05 15:48:42 -0800428 *
429 * Return: %0 on success, negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800431int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
432 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct writeback_control wbc = {
435 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800436 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700437 .range_start = start,
438 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 };
440
Josef Bacik5a798492021-07-14 14:47:22 -0400441 return filemap_fdatawrite_wbc(mapping, &wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444static inline int __filemap_fdatawrite(struct address_space *mapping,
445 int sync_mode)
446{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700447 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450int filemap_fdatawrite(struct address_space *mapping)
451{
452 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
453}
454EXPORT_SYMBOL(filemap_fdatawrite);
455
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400456int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800457 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
460}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400461EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Randy Dunlap485bb992006-06-23 02:03:49 -0700463/**
464 * filemap_flush - mostly a non-blocking flush
465 * @mapping: target address_space
466 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * This is a mostly non-blocking flush. Not suitable for data-integrity
468 * purposes - I/O may not be started against all dirty pages.
Mike Rapoporta862f682019-03-05 15:48:42 -0800469 *
470 * Return: %0 on success, negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 */
472int filemap_flush(struct address_space *mapping)
473{
474 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
475}
476EXPORT_SYMBOL(filemap_flush);
477
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500478/**
479 * filemap_range_has_page - check if a page exists in range.
480 * @mapping: address space within which to check
481 * @start_byte: offset in bytes where the range starts
482 * @end_byte: offset in bytes where the range ends (inclusive)
483 *
484 * Find at least one page in the range supplied, usually used to check if
485 * direct writing in this range will trigger a writeback.
Mike Rapoporta862f682019-03-05 15:48:42 -0800486 *
487 * Return: %true if at least one page exists in the specified range,
488 * %false otherwise.
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500489 */
490bool filemap_range_has_page(struct address_space *mapping,
491 loff_t start_byte, loff_t end_byte)
492{
Jan Karaf7b68042017-09-06 16:21:40 -0700493 struct page *page;
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500494 XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT);
495 pgoff_t max = end_byte >> PAGE_SHIFT;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500496
497 if (end_byte < start_byte)
498 return false;
499
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500500 rcu_read_lock();
501 for (;;) {
502 page = xas_find(&xas, max);
503 if (xas_retry(&xas, page))
504 continue;
505 /* Shadow entries don't count */
506 if (xa_is_value(page))
507 continue;
508 /*
509 * We don't need to try to pin this page; we're about to
510 * release the RCU lock anyway. It is enough to know that
511 * there was a page here recently.
512 */
513 break;
514 }
515 rcu_read_unlock();
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500516
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500517 return page != NULL;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500518}
519EXPORT_SYMBOL(filemap_range_has_page);
520
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400521static void __filemap_fdatawait_range(struct address_space *mapping,
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800522 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300524 pgoff_t index = start_byte >> PAGE_SHIFT;
525 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 struct pagevec pvec;
527 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200529 if (end_byte < start_byte)
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400530 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Mel Gorman86679822017-11-15 17:37:52 -0800532 pagevec_init(&pvec);
Jan Kara312e9d22017-11-15 17:35:05 -0800533 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 unsigned i;
535
Jan Kara312e9d22017-11-15 17:35:05 -0800536 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara67fd7072017-11-15 17:35:19 -0800537 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara312e9d22017-11-15 17:35:05 -0800538 if (!nr_pages)
539 break;
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 for (i = 0; i < nr_pages; i++) {
542 struct page *page = pvec.pages[i];
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 wait_on_page_writeback(page);
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400545 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547 pagevec_release(&pvec);
548 cond_resched();
549 }
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800550}
551
552/**
553 * filemap_fdatawait_range - 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
559 * in the given range and wait for all of them. Check error status of
560 * the address space and return it.
561 *
562 * Since the error status of the address space is cleared by this function,
563 * callers are responsible for checking the return value and handling and/or
564 * reporting the error.
Mike Rapoporta862f682019-03-05 15:48:42 -0800565 *
566 * Return: error status of the address space.
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800567 */
568int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
569 loff_t end_byte)
570{
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400571 __filemap_fdatawait_range(mapping, start_byte, end_byte);
572 return filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200574EXPORT_SYMBOL(filemap_fdatawait_range);
575
576/**
Ross Zwisleraa0bfcd2019-06-20 17:05:37 -0400577 * filemap_fdatawait_range_keep_errors - wait for writeback to complete
578 * @mapping: address space structure to wait for
579 * @start_byte: offset in bytes where the range starts
580 * @end_byte: offset in bytes where the range ends (inclusive)
581 *
582 * Walk the list of under-writeback pages of the given address space in the
583 * given range and wait for all of them. Unlike filemap_fdatawait_range(),
584 * this function does not clear error status of the address space.
585 *
586 * Use this function if callers don't handle errors themselves. Expected
587 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
588 * fsfreeze(8)
589 */
590int filemap_fdatawait_range_keep_errors(struct address_space *mapping,
591 loff_t start_byte, loff_t end_byte)
592{
593 __filemap_fdatawait_range(mapping, start_byte, end_byte);
594 return filemap_check_and_keep_errors(mapping);
595}
596EXPORT_SYMBOL(filemap_fdatawait_range_keep_errors);
597
598/**
Jeff Laytona823e452017-07-28 07:24:43 -0400599 * file_fdatawait_range - wait for writeback to complete
600 * @file: file pointing to address space structure to wait for
601 * @start_byte: offset in bytes where the range starts
602 * @end_byte: offset in bytes where the range ends (inclusive)
603 *
604 * Walk the list of under-writeback pages of the address space that file
605 * refers to, in the given range and wait for all of them. Check error
606 * status of the address space vs. the file->f_wb_err cursor and return it.
607 *
608 * Since the error status of the file is advanced by this function,
609 * callers are responsible for checking the return value and handling and/or
610 * reporting the error.
Mike Rapoporta862f682019-03-05 15:48:42 -0800611 *
612 * Return: error status of the address space vs. the file->f_wb_err cursor.
Jeff Laytona823e452017-07-28 07:24:43 -0400613 */
614int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
615{
616 struct address_space *mapping = file->f_mapping;
617
618 __filemap_fdatawait_range(mapping, start_byte, end_byte);
619 return file_check_and_advance_wb_err(file);
620}
621EXPORT_SYMBOL(file_fdatawait_range);
622
623/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800624 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
625 * @mapping: address space structure to wait for
626 *
627 * Walk the list of under-writeback pages of the given address space
628 * and wait for all of them. Unlike filemap_fdatawait(), this function
629 * does not clear error status of the address space.
630 *
631 * Use this function if callers don't handle errors themselves. Expected
632 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
633 * fsfreeze(8)
Mike Rapoporta862f682019-03-05 15:48:42 -0800634 *
635 * Return: error status of the address space.
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800636 */
Jeff Layton76341ca2017-07-06 07:02:22 -0400637int filemap_fdatawait_keep_errors(struct address_space *mapping)
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800638{
Jeff Laytonffb959b2017-07-31 10:29:38 -0400639 __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
Jeff Layton76341ca2017-07-06 07:02:22 -0400640 return filemap_check_and_keep_errors(mapping);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800641}
Jeff Layton76341ca2017-07-06 07:02:22 -0400642EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800643
Konstantin Khlebnikov875d91b2019-09-23 15:34:48 -0700644/* Returns true if writeback might be needed or already in progress. */
Jeff Layton9326c9b2017-07-26 10:21:11 -0400645static bool mapping_needs_writeback(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Konstantin Khlebnikov875d91b2019-09-23 15:34:48 -0700647 return mapping->nrpages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Jens Axboef8ee8902021-11-05 13:37:13 -0700650static bool filemap_range_has_writeback(struct address_space *mapping,
651 loff_t start_byte, loff_t end_byte)
652{
653 XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT);
654 pgoff_t max = end_byte >> PAGE_SHIFT;
655 struct page *page;
656
657 if (end_byte < start_byte)
658 return false;
659
660 rcu_read_lock();
661 xas_for_each(&xas, page, max) {
662 if (xas_retry(&xas, page))
663 continue;
664 if (xa_is_value(page))
665 continue;
666 if (PageDirty(page) || PageLocked(page) || PageWriteback(page))
667 break;
668 }
669 rcu_read_unlock();
670 return page != NULL;
671
672}
673
Randy Dunlap485bb992006-06-23 02:03:49 -0700674/**
Jens Axboe63135aa2021-04-29 22:55:18 -0700675 * filemap_range_needs_writeback - check if range potentially needs writeback
676 * @mapping: address space within which to check
677 * @start_byte: offset in bytes where the range starts
678 * @end_byte: offset in bytes where the range ends (inclusive)
679 *
680 * Find at least one page in the range supplied, usually used to check if
681 * direct writing in this range will trigger a writeback. Used by O_DIRECT
682 * read/write with IOCB_NOWAIT, to see if the caller needs to do
683 * filemap_write_and_wait_range() before proceeding.
684 *
685 * Return: %true if the caller should do filemap_write_and_wait_range() before
686 * doing O_DIRECT to a page in this range, %false otherwise.
687 */
688bool filemap_range_needs_writeback(struct address_space *mapping,
689 loff_t start_byte, loff_t end_byte)
690{
Jens Axboe63135aa2021-04-29 22:55:18 -0700691 if (!mapping_needs_writeback(mapping))
692 return false;
693 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
694 !mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK))
695 return false;
Jens Axboef8ee8902021-11-05 13:37:13 -0700696 return filemap_range_has_writeback(mapping, start_byte, end_byte);
Jens Axboe63135aa2021-04-29 22:55:18 -0700697}
698EXPORT_SYMBOL_GPL(filemap_range_needs_writeback);
699
700/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700701 * filemap_write_and_wait_range - write out & wait on a file range
702 * @mapping: the address_space for the pages
703 * @lstart: offset in bytes where the range starts
704 * @lend: offset in bytes where the range ends (inclusive)
705 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800706 * Write out and wait upon file offsets lstart->lend, inclusive.
707 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300708 * Note that @lend is inclusive (describes the last byte to be written) so
Andrew Morton469eb4d2006-03-24 03:17:45 -0800709 * that this function can be used to write to the very end-of-file (end = -1).
Mike Rapoporta862f682019-03-05 15:48:42 -0800710 *
711 * Return: error status of the address space.
Andrew Morton469eb4d2006-03-24 03:17:45 -0800712 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713int filemap_write_and_wait_range(struct address_space *mapping,
714 loff_t lstart, loff_t lend)
715{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800716 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Jeff Layton9326c9b2017-07-26 10:21:11 -0400718 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800719 err = __filemap_fdatawrite_range(mapping, lstart, lend,
720 WB_SYNC_ALL);
Ira Weinyddf8f372020-01-30 22:12:07 -0800721 /*
722 * Even if the above returned error, the pages may be
723 * written partially (e.g. -ENOSPC), so we wait for it.
724 * But the -EIO is special case, it may indicate the worst
725 * thing (e.g. bug) happened, so we avoid waiting for it.
726 */
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800727 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200728 int err2 = filemap_fdatawait_range(mapping,
729 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800730 if (!err)
731 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400732 } else {
733 /* Clear any previously stored errors */
734 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800735 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700736 } else {
737 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800739 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
Chris Masonf6995582009-04-15 13:22:37 -0400741EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Jeff Layton5660e132017-07-06 07:02:25 -0400743void __filemap_set_wb_err(struct address_space *mapping, int err)
744{
Jeff Layton3acdfd22017-07-24 06:22:15 -0400745 errseq_t eseq = errseq_set(&mapping->wb_err, err);
Jeff Layton5660e132017-07-06 07:02:25 -0400746
747 trace_filemap_set_wb_err(mapping, eseq);
748}
749EXPORT_SYMBOL(__filemap_set_wb_err);
750
751/**
752 * file_check_and_advance_wb_err - report wb error (if any) that was previously
753 * and advance wb_err to current one
754 * @file: struct file on which the error is being reported
755 *
756 * When userland calls fsync (or something like nfsd does the equivalent), we
757 * want to report any writeback errors that occurred since the last fsync (or
758 * since the file was opened if there haven't been any).
759 *
760 * Grab the wb_err from the mapping. If it matches what we have in the file,
761 * then just quickly return 0. The file is all caught up.
762 *
763 * If it doesn't match, then take the mapping value, set the "seen" flag in
764 * it and try to swap it into place. If it works, or another task beat us
765 * to it with the new value, then update the f_wb_err and return the error
766 * portion. The error at this point must be reported via proper channels
767 * (a'la fsync, or NFS COMMIT operation, etc.).
768 *
769 * While we handle mapping->wb_err with atomic operations, the f_wb_err
770 * value is protected by the f_lock since we must ensure that it reflects
771 * the latest value swapped in for this file descriptor.
Mike Rapoporta862f682019-03-05 15:48:42 -0800772 *
773 * Return: %0 on success, negative error code otherwise.
Jeff Layton5660e132017-07-06 07:02:25 -0400774 */
775int file_check_and_advance_wb_err(struct file *file)
776{
777 int err = 0;
778 errseq_t old = READ_ONCE(file->f_wb_err);
779 struct address_space *mapping = file->f_mapping;
780
781 /* Locklessly handle the common case where nothing has changed */
782 if (errseq_check(&mapping->wb_err, old)) {
783 /* Something changed, must use slow path */
784 spin_lock(&file->f_lock);
785 old = file->f_wb_err;
786 err = errseq_check_and_advance(&mapping->wb_err,
787 &file->f_wb_err);
788 trace_file_check_and_advance_wb_err(file, old);
789 spin_unlock(&file->f_lock);
790 }
Jeff Laytonf4e222c2017-10-03 16:15:25 -0700791
792 /*
793 * We're mostly using this function as a drop in replacement for
794 * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect
795 * that the legacy code would have had on these flags.
796 */
797 clear_bit(AS_EIO, &mapping->flags);
798 clear_bit(AS_ENOSPC, &mapping->flags);
Jeff Layton5660e132017-07-06 07:02:25 -0400799 return err;
800}
801EXPORT_SYMBOL(file_check_and_advance_wb_err);
802
803/**
804 * file_write_and_wait_range - write out & wait on a file range
805 * @file: file pointing to address_space with pages
806 * @lstart: offset in bytes where the range starts
807 * @lend: offset in bytes where the range ends (inclusive)
808 *
809 * Write out and wait upon file offsets lstart->lend, inclusive.
810 *
811 * Note that @lend is inclusive (describes the last byte to be written) so
812 * that this function can be used to write to the very end-of-file (end = -1).
813 *
814 * After writing out and waiting on the data, we check and advance the
815 * f_wb_err cursor to the latest value, and return any errors detected there.
Mike Rapoporta862f682019-03-05 15:48:42 -0800816 *
817 * Return: %0 on success, negative error code otherwise.
Jeff Layton5660e132017-07-06 07:02:25 -0400818 */
819int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
820{
821 int err = 0, err2;
822 struct address_space *mapping = file->f_mapping;
823
Jeff Layton9326c9b2017-07-26 10:21:11 -0400824 if (mapping_needs_writeback(mapping)) {
Jeff Layton5660e132017-07-06 07:02:25 -0400825 err = __filemap_fdatawrite_range(mapping, lstart, lend,
826 WB_SYNC_ALL);
827 /* See comment of filemap_write_and_wait() */
828 if (err != -EIO)
829 __filemap_fdatawait_range(mapping, lstart, lend);
830 }
831 err2 = file_check_and_advance_wb_err(file);
832 if (!err)
833 err = err2;
834 return err;
835}
836EXPORT_SYMBOL(file_write_and_wait_range);
837
Randy Dunlap485bb992006-06-23 02:03:49 -0700838/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700839 * replace_page_cache_page - replace a pagecache page with a new one
840 * @old: page to be replaced
841 * @new: page to replace with
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700842 *
843 * This function replaces a page in the pagecache with a new one. On
844 * success it acquires the pagecache reference for the new page and
845 * drops it for the old page. Both the old and new pages must be
846 * locked. This function does not add the new page to the LRU, the
847 * caller must do that.
848 *
Matthew Wilcox74d60952017-11-17 10:01:45 -0500849 * The remove + add is atomic. This function cannot fail.
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700850 */
Baolin Wang1f7ef652021-02-24 12:01:42 -0800851void replace_page_cache_page(struct page *old, struct page *new)
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700852{
Matthew Wilcox (Oracle)d21bba22021-05-06 18:14:59 -0400853 struct folio *fold = page_folio(old);
854 struct folio *fnew = page_folio(new);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500855 struct address_space *mapping = old->mapping;
856 void (*freepage)(struct page *) = mapping->a_ops->freepage;
857 pgoff_t offset = old->index;
858 XA_STATE(xas, &mapping->i_pages, offset);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700859
Sasha Levin309381fea2014-01-23 15:52:54 -0800860 VM_BUG_ON_PAGE(!PageLocked(old), old);
861 VM_BUG_ON_PAGE(!PageLocked(new), new);
862 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700863
Matthew Wilcox74d60952017-11-17 10:01:45 -0500864 get_page(new);
865 new->mapping = mapping;
866 new->index = offset;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700867
Matthew Wilcox (Oracle)d21bba22021-05-06 18:14:59 -0400868 mem_cgroup_migrate(fold, fnew);
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700869
Johannes Weiner30472502021-09-02 14:53:18 -0700870 xas_lock_irq(&xas);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500871 xas_store(&xas, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700872
Matthew Wilcox74d60952017-11-17 10:01:45 -0500873 old->mapping = NULL;
874 /* hugetlb pages do not participate in page cache accounting. */
875 if (!PageHuge(old))
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700876 __dec_lruvec_page_state(old, NR_FILE_PAGES);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500877 if (!PageHuge(new))
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700878 __inc_lruvec_page_state(new, NR_FILE_PAGES);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500879 if (PageSwapBacked(old))
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700880 __dec_lruvec_page_state(old, NR_SHMEM);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500881 if (PageSwapBacked(new))
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700882 __inc_lruvec_page_state(new, NR_SHMEM);
Johannes Weiner30472502021-09-02 14:53:18 -0700883 xas_unlock_irq(&xas);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500884 if (freepage)
885 freepage(old);
886 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700887}
888EXPORT_SYMBOL_GPL(replace_page_cache_page);
889
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500890noinline int __filemap_add_folio(struct address_space *mapping,
891 struct folio *folio, pgoff_t index, gfp_t gfp, void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500893 XA_STATE(xas, &mapping->i_pages, index);
894 int huge = folio_test_hugetlb(folio);
Nick Piggine2867812008-07-25 19:45:30 -0700895 int error;
Waiman Longda742402021-02-04 18:32:45 -0800896 bool charged = false;
Nick Piggine2867812008-07-25 19:45:30 -0700897
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500898 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
899 VM_BUG_ON_FOLIO(folio_test_swapbacked(folio), folio);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500900 mapping_set_update(&xas, mapping);
Nick Piggine2867812008-07-25 19:45:30 -0700901
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500902 folio_get(folio);
903 folio->mapping = mapping;
904 folio->index = index;
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700905
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700906 if (!huge) {
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500907 error = mem_cgroup_charge(folio, NULL, gfp);
908 VM_BUG_ON_FOLIO(index & (folio_nr_pages(folio) - 1), folio);
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700909 if (error)
910 goto error;
Waiman Longda742402021-02-04 18:32:45 -0800911 charged = true;
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700912 }
913
Matthew Wilcox (Oracle)198b62f2020-10-15 20:05:20 -0700914 gfp &= GFP_RECLAIM_MASK;
915
Matthew Wilcox74d60952017-11-17 10:01:45 -0500916 do {
Matthew Wilcox (Oracle)198b62f2020-10-15 20:05:20 -0700917 unsigned int order = xa_get_order(xas.xa, xas.xa_index);
918 void *entry, *old = NULL;
919
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500920 if (order > folio_order(folio))
Matthew Wilcox (Oracle)198b62f2020-10-15 20:05:20 -0700921 xas_split_alloc(&xas, xa_load(xas.xa, xas.xa_index),
922 order, gfp);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500923 xas_lock_irq(&xas);
Matthew Wilcox (Oracle)198b62f2020-10-15 20:05:20 -0700924 xas_for_each_conflict(&xas, entry) {
925 old = entry;
926 if (!xa_is_value(entry)) {
927 xas_set_err(&xas, -EEXIST);
928 goto unlock;
929 }
930 }
931
932 if (old) {
933 if (shadowp)
934 *shadowp = old;
935 /* entry may have been split before we acquired lock */
936 order = xa_get_order(xas.xa, xas.xa_index);
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500937 if (order > folio_order(folio)) {
Matthew Wilcox (Oracle)198b62f2020-10-15 20:05:20 -0700938 xas_split(&xas, old, order);
939 xas_reset(&xas);
940 }
941 }
942
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500943 xas_store(&xas, folio);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500944 if (xas_error(&xas))
945 goto unlock;
Michal Hocko4165b9b42015-06-24 16:57:24 -0700946
Matthew Wilcox74d60952017-11-17 10:01:45 -0500947 mapping->nrpages++;
948
949 /* hugetlb pages do not participate in page cache accounting */
950 if (!huge)
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500951 __lruvec_stat_add_folio(folio, NR_FILE_PAGES);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500952unlock:
953 xas_unlock_irq(&xas);
Matthew Wilcox (Oracle)198b62f2020-10-15 20:05:20 -0700954 } while (xas_nomem(&xas, gfp));
Matthew Wilcox74d60952017-11-17 10:01:45 -0500955
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700956 if (xas_error(&xas)) {
957 error = xas_error(&xas);
Waiman Longda742402021-02-04 18:32:45 -0800958 if (charged)
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500959 mem_cgroup_uncharge(folio);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500960 goto error;
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700961 }
Matthew Wilcox74d60952017-11-17 10:01:45 -0500962
Matthew Wilcox (Oracle)a0580c62021-07-23 09:29:46 -0400963 trace_mm_filemap_add_to_page_cache(folio);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700964 return 0;
Matthew Wilcox74d60952017-11-17 10:01:45 -0500965error:
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500966 folio->mapping = NULL;
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700967 /* Leave page->index set: truncation relies upon it */
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500968 folio_put(folio);
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700969 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500971ALLOW_ERROR_INJECTION(__filemap_add_folio, ERRNO);
Johannes Weinera5289102014-04-03 14:47:51 -0700972
973/**
974 * add_to_page_cache_locked - add a locked page to the pagecache
975 * @page: page to add
976 * @mapping: the page's address_space
977 * @offset: page index
978 * @gfp_mask: page allocation mode
979 *
980 * This function is used to add a page to the pagecache. It must be locked.
981 * This function does not add the page to the LRU. The caller must do that.
Mike Rapoporta862f682019-03-05 15:48:42 -0800982 *
983 * Return: %0 on success, negative error code otherwise.
Johannes Weinera5289102014-04-03 14:47:51 -0700984 */
985int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
986 pgoff_t offset, gfp_t gfp_mask)
987{
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500988 return __filemap_add_folio(mapping, page_folio(page), offset,
Johannes Weinera5289102014-04-03 14:47:51 -0700989 gfp_mask, NULL);
990}
Nick Piggine2867812008-07-25 19:45:30 -0700991EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500993int filemap_add_folio(struct address_space *mapping, struct folio *folio,
994 pgoff_t index, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Johannes Weinera5289102014-04-03 14:47:51 -0700996 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700997 int ret;
998
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500999 __folio_set_locked(folio);
1000 ret = __filemap_add_folio(mapping, folio, index, gfp, &shadow);
Johannes Weinera5289102014-04-03 14:47:51 -07001001 if (unlikely(ret))
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -05001002 __folio_clear_locked(folio);
Johannes Weinera5289102014-04-03 14:47:51 -07001003 else {
1004 /*
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -05001005 * The folio might have been evicted from cache only
Johannes Weinera5289102014-04-03 14:47:51 -07001006 * recently, in which case it should be activated like
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -05001007 * any other repeatedly accessed folio.
1008 * The exception is folios getting rewritten; evicting other
Rik van Rielf0281a02016-05-20 16:56:25 -07001009 * data from the working set, only to cache data that will
1010 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -07001011 */
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -05001012 WARN_ON_ONCE(folio_test_active(folio));
1013 if (!(gfp & __GFP_WRITE) && shadow)
1014 workingset_refault(folio, shadow);
1015 folio_add_lru(folio);
Johannes Weinera5289102014-04-03 14:47:51 -07001016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return ret;
1018}
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -05001019EXPORT_SYMBOL_GPL(filemap_add_folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Paul Jackson44110fe2006-03-24 03:16:04 -08001021#ifdef CONFIG_NUMA
Matthew Wilcox (Oracle)bb3c5792020-12-15 23:11:07 -05001022struct folio *filemap_alloc_folio(gfp_t gfp, unsigned int order)
Paul Jackson44110fe2006-03-24 03:16:04 -08001023{
Miao Xiec0ff7452010-05-24 14:32:08 -07001024 int n;
Matthew Wilcox (Oracle)bb3c5792020-12-15 23:11:07 -05001025 struct folio *folio;
Miao Xiec0ff7452010-05-24 14:32:08 -07001026
Paul Jackson44110fe2006-03-24 03:16:04 -08001027 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -07001028 unsigned int cpuset_mems_cookie;
1029 do {
Mel Gormand26914d2014-04-03 14:47:24 -07001030 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -07001031 n = cpuset_mem_spread_node();
Matthew Wilcox (Oracle)bb3c5792020-12-15 23:11:07 -05001032 folio = __folio_alloc_node(gfp, order, n);
1033 } while (!folio && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -07001034
Matthew Wilcox (Oracle)bb3c5792020-12-15 23:11:07 -05001035 return folio;
Paul Jackson44110fe2006-03-24 03:16:04 -08001036 }
Matthew Wilcox (Oracle)bb3c5792020-12-15 23:11:07 -05001037 return folio_alloc(gfp, order);
Paul Jackson44110fe2006-03-24 03:16:04 -08001038}
Matthew Wilcox (Oracle)bb3c5792020-12-15 23:11:07 -05001039EXPORT_SYMBOL(filemap_alloc_folio);
Paul Jackson44110fe2006-03-24 03:16:04 -08001040#endif
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042/*
Jan Kara7506ae62021-05-24 13:02:30 +02001043 * filemap_invalidate_lock_two - lock invalidate_lock for two mappings
1044 *
1045 * Lock exclusively invalidate_lock of any passed mapping that is not NULL.
1046 *
1047 * @mapping1: the first mapping to lock
1048 * @mapping2: the second mapping to lock
1049 */
1050void filemap_invalidate_lock_two(struct address_space *mapping1,
1051 struct address_space *mapping2)
1052{
1053 if (mapping1 > mapping2)
1054 swap(mapping1, mapping2);
1055 if (mapping1)
1056 down_write(&mapping1->invalidate_lock);
1057 if (mapping2 && mapping1 != mapping2)
1058 down_write_nested(&mapping2->invalidate_lock, 1);
1059}
1060EXPORT_SYMBOL(filemap_invalidate_lock_two);
1061
1062/*
1063 * filemap_invalidate_unlock_two - unlock invalidate_lock for two mappings
1064 *
1065 * Unlock exclusive invalidate_lock of any passed mapping that is not NULL.
1066 *
1067 * @mapping1: the first mapping to unlock
1068 * @mapping2: the second mapping to unlock
1069 */
1070void filemap_invalidate_unlock_two(struct address_space *mapping1,
1071 struct address_space *mapping2)
1072{
1073 if (mapping1)
1074 up_write(&mapping1->invalidate_lock);
1075 if (mapping2 && mapping1 != mapping2)
1076 up_write(&mapping2->invalidate_lock);
1077}
1078EXPORT_SYMBOL(filemap_invalidate_unlock_two);
1079
1080/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 * In order to wait for pages to become available there must be
1082 * waitqueues associated with pages. By using a hash table of
1083 * waitqueues where the bucket discipline is to maintain all
1084 * waiters on the same queue and wake all when any of the pages
1085 * become available, and for the woken contexts to check to be
1086 * sure the appropriate page became available, this saves space
1087 * at a cost of "thundering herd" phenomena during rare hash
1088 * collisions.
1089 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001090#define PAGE_WAIT_TABLE_BITS 8
1091#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001092static wait_queue_head_t folio_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
Nicholas Piggin62906022016-12-25 13:00:30 +10001093
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001094static wait_queue_head_t *folio_waitqueue(struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001096 return &folio_wait_table[hash_ptr(folio, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
Nicholas Piggin62906022016-12-25 13:00:30 +10001098
1099void __init pagecache_init(void)
1100{
1101 int i;
1102
1103 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001104 init_waitqueue_head(&folio_wait_table[i]);
Nicholas Piggin62906022016-12-25 13:00:30 +10001105
1106 page_writeback_init();
1107}
1108
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001109/*
1110 * The page wait code treats the "wait->flags" somewhat unusually, because
Linus Torvalds5868ec22020-09-20 10:38:47 -07001111 * we have multiple different kinds of waits, not just the usual "exclusive"
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001112 * one.
1113 *
1114 * We have:
1115 *
1116 * (a) no special bits set:
1117 *
1118 * We're just waiting for the bit to be released, and when a waker
1119 * calls the wakeup function, we set WQ_FLAG_WOKEN and wake it up,
1120 * and remove it from the wait queue.
1121 *
1122 * Simple and straightforward.
1123 *
1124 * (b) WQ_FLAG_EXCLUSIVE:
1125 *
1126 * The waiter is waiting to get the lock, and only one waiter should
1127 * be woken up to avoid any thundering herd behavior. We'll set the
1128 * WQ_FLAG_WOKEN bit, wake it up, and remove it from the wait queue.
1129 *
1130 * This is the traditional exclusive wait.
1131 *
Linus Torvalds5868ec22020-09-20 10:38:47 -07001132 * (c) WQ_FLAG_EXCLUSIVE | WQ_FLAG_CUSTOM:
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001133 *
1134 * The waiter is waiting to get the bit, and additionally wants the
1135 * lock to be transferred to it for fair lock behavior. If the lock
1136 * cannot be taken, we stop walking the wait queue without waking
1137 * the waiter.
1138 *
1139 * This is the "fair lock handoff" case, and in addition to setting
1140 * WQ_FLAG_WOKEN, we set WQ_FLAG_DONE to let the waiter easily see
1141 * that it now has the lock.
1142 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001143static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
Nicholas Piggin62906022016-12-25 13:00:30 +10001144{
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001145 unsigned int flags;
Nicholas Piggin62906022016-12-25 13:00:30 +10001146 struct wait_page_key *key = arg;
1147 struct wait_page_queue *wait_page
1148 = container_of(wait, struct wait_page_queue, wait);
1149
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07001150 if (!wake_page_match(wait_page, key))
Nicholas Piggin62906022016-12-25 13:00:30 +10001151 return 0;
Linus Torvalds3510ca22017-08-27 13:55:12 -07001152
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001153 /*
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001154 * If it's a lock handoff wait, we get the bit for it, and
1155 * stop walking (and do not wake it up) if we can't.
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001156 */
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001157 flags = wait->flags;
1158 if (flags & WQ_FLAG_EXCLUSIVE) {
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001159 if (test_bit(key->bit_nr, &key->folio->flags))
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001160 return -1;
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001161 if (flags & WQ_FLAG_CUSTOM) {
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001162 if (test_and_set_bit(key->bit_nr, &key->folio->flags))
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001163 return -1;
1164 flags |= WQ_FLAG_DONE;
1165 }
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001166 }
Nicholas Piggin62906022016-12-25 13:00:30 +10001167
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001168 /*
1169 * We are holding the wait-queue lock, but the waiter that
1170 * is waiting for this will be checking the flags without
1171 * any locking.
1172 *
1173 * So update the flags atomically, and wake up the waiter
1174 * afterwards to avoid any races. This store-release pairs
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001175 * with the load-acquire in folio_wait_bit_common().
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001176 */
1177 smp_store_release(&wait->flags, flags | WQ_FLAG_WOKEN);
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001178 wake_up_state(wait->private, mode);
1179
1180 /*
1181 * Ok, we have successfully done what we're waiting for,
1182 * and we can unconditionally remove the wait entry.
1183 *
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001184 * Note that this pairs with the "finish_wait()" in the
1185 * waiter, and has to be the absolute last thing we do.
1186 * After this list_del_init(&wait->entry) the wait entry
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001187 * might be de-allocated and the process might even have
1188 * exited.
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001189 */
Linus Torvaldsc6fe44d2020-07-23 12:33:41 -07001190 list_del_init_careful(&wait->entry);
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001191 return (flags & WQ_FLAG_EXCLUSIVE) != 0;
Nicholas Piggin62906022016-12-25 13:00:30 +10001192}
1193
Matthew Wilcox (Oracle)6974d7c2021-01-15 17:14:48 -05001194static void folio_wake_bit(struct folio *folio, int bit_nr)
Nicholas Piggin62906022016-12-25 13:00:30 +10001195{
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001196 wait_queue_head_t *q = folio_waitqueue(folio);
Nicholas Piggin62906022016-12-25 13:00:30 +10001197 struct wait_page_key key;
1198 unsigned long flags;
Tim Chen11a19c72017-08-25 09:13:55 -07001199 wait_queue_entry_t bookmark;
Nicholas Piggin62906022016-12-25 13:00:30 +10001200
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001201 key.folio = folio;
Nicholas Piggin62906022016-12-25 13:00:30 +10001202 key.bit_nr = bit_nr;
1203 key.page_match = 0;
1204
Tim Chen11a19c72017-08-25 09:13:55 -07001205 bookmark.flags = 0;
1206 bookmark.private = NULL;
1207 bookmark.func = NULL;
1208 INIT_LIST_HEAD(&bookmark.entry);
1209
Nicholas Piggin62906022016-12-25 13:00:30 +10001210 spin_lock_irqsave(&q->lock, flags);
Tim Chen11a19c72017-08-25 09:13:55 -07001211 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1212
1213 while (bookmark.flags & WQ_FLAG_BOOKMARK) {
1214 /*
1215 * Take a breather from holding the lock,
1216 * allow pages that finish wake up asynchronously
1217 * to acquire the lock and remove themselves
1218 * from wait queue
1219 */
1220 spin_unlock_irqrestore(&q->lock, flags);
1221 cpu_relax();
1222 spin_lock_irqsave(&q->lock, flags);
1223 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1224 }
1225
Nicholas Piggin62906022016-12-25 13:00:30 +10001226 /*
1227 * It is possible for other pages to have collided on the waitqueue
1228 * hash, so in that case check for a page match. That prevents a long-
1229 * term waiter
1230 *
1231 * It is still possible to miss a case here, when we woke page waiters
1232 * and removed them from the waitqueue, but there are still other
1233 * page waiters.
1234 */
1235 if (!waitqueue_active(q) || !key.page_match) {
Matthew Wilcox (Oracle)6974d7c2021-01-15 17:14:48 -05001236 folio_clear_waiters(folio);
Nicholas Piggin62906022016-12-25 13:00:30 +10001237 /*
1238 * It's possible to miss clearing Waiters here, when we woke
1239 * our page waiters, but the hashed waitqueue has waiters for
1240 * other pages on it.
1241 *
1242 * That's okay, it's a rare case. The next waker will clear it.
1243 */
1244 }
1245 spin_unlock_irqrestore(&q->lock, flags);
1246}
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001247
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001248static void folio_wake(struct folio *folio, int bit)
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001249{
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001250 if (!folio_test_waiters(folio))
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001251 return;
Matthew Wilcox (Oracle)6974d7c2021-01-15 17:14:48 -05001252 folio_wake_bit(folio, bit);
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001253}
Nicholas Piggin62906022016-12-25 13:00:30 +10001254
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001255/*
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001256 * A choice of three behaviors for folio_wait_bit_common():
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001257 */
1258enum behavior {
1259 EXCLUSIVE, /* Hold ref to page and take the bit when woken, like
Matthew Wilcox (Oracle)7c23c782021-03-01 19:38:25 -05001260 * __folio_lock() waiting on then setting PG_locked.
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001261 */
1262 SHARED, /* Hold ref to page and check the bit when woken, like
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001263 * folio_wait_writeback() waiting on PG_writeback.
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001264 */
1265 DROP, /* Drop ref to page before wait, no check when woken,
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001266 * like folio_put_wait_locked() on PG_locked.
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001267 */
1268};
1269
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001270/*
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001271 * Attempt to check (or get) the folio flag, and mark us done
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001272 * if successful.
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001273 */
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001274static inline bool folio_trylock_flag(struct folio *folio, int bit_nr,
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001275 struct wait_queue_entry *wait)
1276{
1277 if (wait->flags & WQ_FLAG_EXCLUSIVE) {
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001278 if (test_and_set_bit(bit_nr, &folio->flags))
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001279 return false;
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001280 } else if (test_bit(bit_nr, &folio->flags))
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001281 return false;
1282
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001283 wait->flags |= WQ_FLAG_WOKEN | WQ_FLAG_DONE;
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001284 return true;
1285}
1286
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001287/* How many times do we accept lock stealing from under a waiter? */
1288int sysctl_page_lock_unfairness = 5;
1289
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001290static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
1291 int state, enum behavior behavior)
Nicholas Piggin62906022016-12-25 13:00:30 +10001292{
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001293 wait_queue_head_t *q = folio_waitqueue(folio);
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001294 int unfairness = sysctl_page_lock_unfairness;
Nicholas Piggin62906022016-12-25 13:00:30 +10001295 struct wait_page_queue wait_page;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001296 wait_queue_entry_t *wait = &wait_page.wait;
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001297 bool thrashing = false;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001298 bool delayacct = false;
Johannes Weinereb414682018-10-26 15:06:27 -07001299 unsigned long pflags;
Nicholas Piggin62906022016-12-25 13:00:30 +10001300
Johannes Weinereb414682018-10-26 15:06:27 -07001301 if (bit_nr == PG_locked &&
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001302 !folio_test_uptodate(folio) && folio_test_workingset(folio)) {
1303 if (!folio_test_swapbacked(folio)) {
Johannes Weinereb414682018-10-26 15:06:27 -07001304 delayacct_thrashing_start();
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001305 delayacct = true;
1306 }
Johannes Weinereb414682018-10-26 15:06:27 -07001307 psi_memstall_enter(&pflags);
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001308 thrashing = true;
1309 }
1310
Nicholas Piggin62906022016-12-25 13:00:30 +10001311 init_wait(wait);
1312 wait->func = wake_page_function;
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001313 wait_page.folio = folio;
Nicholas Piggin62906022016-12-25 13:00:30 +10001314 wait_page.bit_nr = bit_nr;
1315
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001316repeat:
1317 wait->flags = 0;
1318 if (behavior == EXCLUSIVE) {
1319 wait->flags = WQ_FLAG_EXCLUSIVE;
1320 if (--unfairness < 0)
1321 wait->flags |= WQ_FLAG_CUSTOM;
1322 }
1323
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001324 /*
1325 * Do one last check whether we can get the
1326 * page bit synchronously.
1327 *
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001328 * Do the folio_set_waiters() marking before that
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001329 * to let any waker we _just_ missed know they
1330 * need to wake us up (otherwise they'll never
1331 * even go to the slow case that looks at the
1332 * page queue), and add ourselves to the wait
1333 * queue if we need to sleep.
1334 *
1335 * This part needs to be done under the queue
1336 * lock to avoid races.
1337 */
1338 spin_lock_irq(&q->lock);
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001339 folio_set_waiters(folio);
1340 if (!folio_trylock_flag(folio, bit_nr, wait))
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001341 __add_wait_queue_entry_tail(q, wait);
1342 spin_unlock_irq(&q->lock);
1343
1344 /*
1345 * From now on, all the logic will be based on
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001346 * the WQ_FLAG_WOKEN and WQ_FLAG_DONE flag, to
1347 * see whether the page bit testing has already
1348 * been done by the wake function.
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001349 *
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001350 * We can drop our reference to the folio.
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001351 */
1352 if (behavior == DROP)
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001353 folio_put(folio);
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001354
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001355 /*
1356 * Note that until the "finish_wait()", or until
1357 * we see the WQ_FLAG_WOKEN flag, we need to
1358 * be very careful with the 'wait->flags', because
1359 * we may race with a waker that sets them.
1360 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001361 for (;;) {
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001362 unsigned int flags;
1363
Nicholas Piggin62906022016-12-25 13:00:30 +10001364 set_current_state(state);
1365
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001366 /* Loop until we've been woken or interrupted */
1367 flags = smp_load_acquire(&wait->flags);
1368 if (!(flags & WQ_FLAG_WOKEN)) {
1369 if (signal_pending_state(state, current))
1370 break;
1371
1372 io_schedule();
1373 continue;
1374 }
1375
1376 /* If we were non-exclusive, we're done */
1377 if (behavior != EXCLUSIVE)
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001378 break;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001379
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001380 /* If the waker got the lock for us, we're done */
1381 if (flags & WQ_FLAG_DONE)
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001382 break;
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001383
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001384 /*
1385 * Otherwise, if we're getting the lock, we need to
1386 * try to get it ourselves.
1387 *
1388 * And if that fails, we'll have to retry this all.
1389 */
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001390 if (unlikely(test_and_set_bit(bit_nr, folio_flags(folio, 0))))
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001391 goto repeat;
1392
1393 wait->flags |= WQ_FLAG_DONE;
1394 break;
Nicholas Piggin62906022016-12-25 13:00:30 +10001395 }
1396
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001397 /*
1398 * If a signal happened, this 'finish_wait()' may remove the last
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001399 * waiter from the wait-queues, but the folio waiters bit will remain
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001400 * set. That's ok. The next wakeup will take care of it, and trying
1401 * to do it here would be difficult and prone to races.
1402 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001403 finish_wait(q, wait);
1404
Johannes Weinereb414682018-10-26 15:06:27 -07001405 if (thrashing) {
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001406 if (delayacct)
Johannes Weinereb414682018-10-26 15:06:27 -07001407 delayacct_thrashing_end();
1408 psi_memstall_leave(&pflags);
1409 }
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001410
Nicholas Piggin62906022016-12-25 13:00:30 +10001411 /*
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001412 * NOTE! The wait->flags weren't stable until we've done the
1413 * 'finish_wait()', and we could have exited the loop above due
1414 * to a signal, and had a wakeup event happen after the signal
1415 * test but before the 'finish_wait()'.
1416 *
1417 * So only after the finish_wait() can we reliably determine
1418 * if we got woken up or not, so we can now figure out the final
1419 * return value based on that state without races.
1420 *
1421 * Also note that WQ_FLAG_WOKEN is sufficient for a non-exclusive
1422 * waiter, but an exclusive one requires WQ_FLAG_DONE.
Nicholas Piggin62906022016-12-25 13:00:30 +10001423 */
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001424 if (behavior == EXCLUSIVE)
1425 return wait->flags & WQ_FLAG_DONE ? 0 : -EINTR;
Nicholas Piggin62906022016-12-25 13:00:30 +10001426
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001427 return wait->flags & WQ_FLAG_WOKEN ? 0 : -EINTR;
Nicholas Piggin62906022016-12-25 13:00:30 +10001428}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001430void folio_wait_bit(struct folio *folio, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001432 folio_wait_bit_common(folio, bit_nr, TASK_UNINTERRUPTIBLE, SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001434EXPORT_SYMBOL(folio_wait_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001436int folio_wait_bit_killable(struct folio *folio, int bit_nr)
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001437{
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001438 return folio_wait_bit_common(folio, bit_nr, TASK_KILLABLE, SHARED);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001439}
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001440EXPORT_SYMBOL(folio_wait_bit_killable);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442/**
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001443 * folio_put_wait_locked - Drop a reference and wait for it to be unlocked
1444 * @folio: The folio to wait for.
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -08001445 * @state: The sleep state (TASK_KILLABLE, TASK_UNINTERRUPTIBLE, etc).
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001446 *
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001447 * The caller should hold a reference on @folio. They expect the page to
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001448 * become unlocked relatively soon, but do not wish to hold up migration
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001449 * (for example) by holding the reference while waiting for the folio to
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001450 * come unlocked. After this function returns, the caller should not
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001451 * dereference @folio.
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -08001452 *
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001453 * Return: 0 if the folio was unlocked or -EINTR if interrupted by a signal.
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001454 */
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001455int folio_put_wait_locked(struct folio *folio, int state)
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001456{
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001457 return folio_wait_bit_common(folio, PG_locked, state, DROP);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001458}
1459
1460/**
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001461 * folio_add_wait_queue - Add an arbitrary waiter to a folio's wait queue
1462 * @folio: Folio defining the wait queue of interest
Randy Dunlap697f6192009-04-13 14:39:54 -07001463 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +01001464 *
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001465 * Add an arbitrary @waiter to the wait queue for the nominated @folio.
David Howells385e1ca5f2009-04-03 16:42:39 +01001466 */
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001467void folio_add_wait_queue(struct folio *folio, wait_queue_entry_t *waiter)
David Howells385e1ca5f2009-04-03 16:42:39 +01001468{
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001469 wait_queue_head_t *q = folio_waitqueue(folio);
David Howells385e1ca5f2009-04-03 16:42:39 +01001470 unsigned long flags;
1471
1472 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds9c3a8152017-08-28 16:45:40 -07001473 __add_wait_queue_entry_tail(q, waiter);
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001474 folio_set_waiters(folio);
David Howells385e1ca5f2009-04-03 16:42:39 +01001475 spin_unlock_irqrestore(&q->lock, flags);
1476}
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001477EXPORT_SYMBOL_GPL(folio_add_wait_queue);
David Howells385e1ca5f2009-04-03 16:42:39 +01001478
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001479#ifndef clear_bit_unlock_is_negative_byte
1480
1481/*
1482 * PG_waiters is the high bit in the same byte as PG_lock.
1483 *
1484 * On x86 (and on many other architectures), we can clear PG_lock and
1485 * test the sign bit at the same time. But if the architecture does
1486 * not support that special operation, we just do this all by hand
1487 * instead.
1488 *
1489 * The read of PG_waiters has to be after (or concurrently with) PG_locked
Ethon Paulffceeb62020-06-04 16:49:22 -07001490 * being cleared, but a memory barrier should be unnecessary since it is
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001491 * in the same byte as PG_locked.
1492 */
1493static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
1494{
1495 clear_bit_unlock(nr, mem);
1496 /* smp_mb__after_atomic(); */
Olof Johansson98473f92016-12-29 14:16:07 -08001497 return test_bit(PG_waiters, mem);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001498}
1499
1500#endif
1501
David Howells385e1ca5f2009-04-03 16:42:39 +01001502/**
Matthew Wilcox (Oracle)4e136422020-12-07 15:44:35 -05001503 * folio_unlock - Unlock a locked folio.
1504 * @folio: The folio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 *
Matthew Wilcox (Oracle)4e136422020-12-07 15:44:35 -05001506 * Unlocks the folio and wakes up any thread sleeping on the page lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 *
Matthew Wilcox (Oracle)4e136422020-12-07 15:44:35 -05001508 * Context: May be called from interrupt or process context. May not be
1509 * called from NMI context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 */
Matthew Wilcox (Oracle)4e136422020-12-07 15:44:35 -05001511void folio_unlock(struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Matthew Wilcox (Oracle)4e136422020-12-07 15:44:35 -05001513 /* Bit 7 allows x86 to check the byte's sign bit */
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001514 BUILD_BUG_ON(PG_waiters != 7);
Matthew Wilcox (Oracle)4e136422020-12-07 15:44:35 -05001515 BUILD_BUG_ON(PG_locked > 7);
1516 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
1517 if (clear_bit_unlock_is_negative_byte(PG_locked, folio_flags(folio, 0)))
Matthew Wilcox (Oracle)6974d7c2021-01-15 17:14:48 -05001518 folio_wake_bit(folio, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
Matthew Wilcox (Oracle)4e136422020-12-07 15:44:35 -05001520EXPORT_SYMBOL(folio_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Randy Dunlap485bb992006-06-23 02:03:49 -07001522/**
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001523 * folio_end_private_2 - Clear PG_private_2 and wake any waiters.
1524 * @folio: The folio.
David Howells73e10de2020-02-10 10:00:21 +00001525 *
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001526 * Clear the PG_private_2 bit on a folio and wake up any sleepers waiting for
1527 * it. The folio reference held for PG_private_2 being set is released.
David Howells73e10de2020-02-10 10:00:21 +00001528 *
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001529 * This is, for example, used when a netfs folio is being written to a local
1530 * disk cache, thereby allowing writes to the cache for the same folio to be
David Howells73e10de2020-02-10 10:00:21 +00001531 * serialised.
1532 */
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001533void folio_end_private_2(struct folio *folio)
David Howells73e10de2020-02-10 10:00:21 +00001534{
Matthew Wilcox (Oracle)6974d7c2021-01-15 17:14:48 -05001535 VM_BUG_ON_FOLIO(!folio_test_private_2(folio), folio);
1536 clear_bit_unlock(PG_private_2, folio_flags(folio, 0));
1537 folio_wake_bit(folio, PG_private_2);
1538 folio_put(folio);
David Howells73e10de2020-02-10 10:00:21 +00001539}
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001540EXPORT_SYMBOL(folio_end_private_2);
David Howells73e10de2020-02-10 10:00:21 +00001541
1542/**
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001543 * folio_wait_private_2 - Wait for PG_private_2 to be cleared on a folio.
1544 * @folio: The folio to wait on.
David Howells73e10de2020-02-10 10:00:21 +00001545 *
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001546 * Wait for PG_private_2 (aka PG_fscache) to be cleared on a folio.
David Howells73e10de2020-02-10 10:00:21 +00001547 */
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001548void folio_wait_private_2(struct folio *folio)
David Howells73e10de2020-02-10 10:00:21 +00001549{
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001550 while (folio_test_private_2(folio))
1551 folio_wait_bit(folio, PG_private_2);
David Howells73e10de2020-02-10 10:00:21 +00001552}
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001553EXPORT_SYMBOL(folio_wait_private_2);
David Howells73e10de2020-02-10 10:00:21 +00001554
1555/**
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001556 * folio_wait_private_2_killable - Wait for PG_private_2 to be cleared on a folio.
1557 * @folio: The folio to wait on.
David Howells73e10de2020-02-10 10:00:21 +00001558 *
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001559 * Wait for PG_private_2 (aka PG_fscache) to be cleared on a folio or until a
David Howells73e10de2020-02-10 10:00:21 +00001560 * fatal signal is received by the calling task.
1561 *
1562 * Return:
1563 * - 0 if successful.
1564 * - -EINTR if a fatal signal was encountered.
1565 */
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001566int folio_wait_private_2_killable(struct folio *folio)
David Howells73e10de2020-02-10 10:00:21 +00001567{
1568 int ret = 0;
1569
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001570 while (folio_test_private_2(folio)) {
1571 ret = folio_wait_bit_killable(folio, PG_private_2);
David Howells73e10de2020-02-10 10:00:21 +00001572 if (ret < 0)
1573 break;
1574 }
1575
1576 return ret;
1577}
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001578EXPORT_SYMBOL(folio_wait_private_2_killable);
David Howells73e10de2020-02-10 10:00:21 +00001579
1580/**
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001581 * folio_end_writeback - End writeback against a folio.
1582 * @folio: The folio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 */
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001584void folio_end_writeback(struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001586 /*
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001587 * folio_test_clear_reclaim() could be used here but it is an
1588 * atomic operation and overkill in this particular case. Failing
1589 * to shuffle a folio marked for immediate reclaim is too mild
1590 * a gain to justify taking an atomic operation penalty at the
1591 * end of every folio writeback.
Mel Gorman888cf2d2014-06-04 16:10:34 -07001592 */
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001593 if (folio_test_reclaim(folio)) {
1594 folio_clear_reclaim(folio);
Matthew Wilcox (Oracle)575ced12020-12-08 01:25:39 -05001595 folio_rotate_reclaimable(folio);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001596 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001597
Hugh Dickins073861e2020-11-24 08:46:43 -08001598 /*
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001599 * Writeback does not hold a folio reference of its own, relying
Hugh Dickins073861e2020-11-24 08:46:43 -08001600 * on truncation to wait for the clearing of PG_writeback.
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001601 * But here we must make sure that the folio is not freed and
1602 * reused before the folio_wake().
Hugh Dickins073861e2020-11-24 08:46:43 -08001603 */
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001604 folio_get(folio);
Matthew Wilcox (Oracle)269ccca32021-01-15 23:34:16 -05001605 if (!__folio_end_writeback(folio))
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001606 BUG();
1607
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001608 smp_mb__after_atomic();
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001609 folio_wake(folio, PG_writeback);
Linus Torvalds512b79312021-11-06 14:08:17 -07001610 acct_reclaim_writeback(folio);
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001611 folio_put(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001613EXPORT_SYMBOL(folio_end_writeback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Matthew Wilcox57d99842014-06-04 16:07:45 -07001615/*
1616 * After completing I/O on a page, call this routine to update the page
1617 * flags appropriately
1618 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001619void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001620{
Jens Axboec11f0c02016-08-05 08:11:04 -06001621 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001622 if (!err) {
1623 SetPageUptodate(page);
1624 } else {
1625 ClearPageUptodate(page);
1626 SetPageError(page);
1627 }
1628 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001629 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001630 if (err) {
Minchan Kimdd8416c2017-02-24 14:59:59 -08001631 struct address_space *mapping;
1632
Matthew Wilcox57d99842014-06-04 16:07:45 -07001633 SetPageError(page);
Minchan Kimdd8416c2017-02-24 14:59:59 -08001634 mapping = page_mapping(page);
1635 if (mapping)
1636 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001637 }
1638 end_page_writeback(page);
1639 }
1640}
1641EXPORT_SYMBOL_GPL(page_endio);
1642
Randy Dunlap485bb992006-06-23 02:03:49 -07001643/**
Matthew Wilcox (Oracle)7c23c782021-03-01 19:38:25 -05001644 * __folio_lock - Get a lock on the folio, assuming we need to sleep to get it.
1645 * @folio: The folio to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 */
Matthew Wilcox (Oracle)7c23c782021-03-01 19:38:25 -05001647void __folio_lock(struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001649 folio_wait_bit_common(folio, PG_locked, TASK_UNINTERRUPTIBLE,
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001650 EXCLUSIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
Matthew Wilcox (Oracle)7c23c782021-03-01 19:38:25 -05001652EXPORT_SYMBOL(__folio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Matthew Wilcox (Oracle)af7f29d2020-12-08 00:07:31 -05001654int __folio_lock_killable(struct folio *folio)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001655{
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001656 return folio_wait_bit_common(folio, PG_locked, TASK_KILLABLE,
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001657 EXCLUSIVE);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001658}
Matthew Wilcox (Oracle)af7f29d2020-12-08 00:07:31 -05001659EXPORT_SYMBOL_GPL(__folio_lock_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001660
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05001661static int __folio_lock_async(struct folio *folio, struct wait_page_queue *wait)
Jens Axboedd3e6d52020-05-22 09:12:09 -06001662{
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001663 struct wait_queue_head *q = folio_waitqueue(folio);
Matthew Wilcox (Oracle)f32b5dd2021-02-24 12:02:09 -08001664 int ret = 0;
1665
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001666 wait->folio = folio;
Matthew Wilcox (Oracle)f32b5dd2021-02-24 12:02:09 -08001667 wait->bit_nr = PG_locked;
1668
1669 spin_lock_irq(&q->lock);
1670 __add_wait_queue_entry_tail(q, &wait->wait);
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05001671 folio_set_waiters(folio);
1672 ret = !folio_trylock(folio);
Matthew Wilcox (Oracle)f32b5dd2021-02-24 12:02:09 -08001673 /*
1674 * If we were successful now, we know we're still on the
1675 * waitqueue as we're still under the lock. This means it's
1676 * safe to remove and return success, we know the callback
1677 * isn't going to trigger.
1678 */
1679 if (!ret)
1680 __remove_wait_queue(q, &wait->wait);
1681 else
1682 ret = -EIOCBQUEUED;
1683 spin_unlock_irq(&q->lock);
1684 return ret;
Jens Axboedd3e6d52020-05-22 09:12:09 -06001685}
1686
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001687/*
1688 * Return values:
Matthew Wilcox (Oracle)9138e472021-03-18 21:39:45 -04001689 * true - folio is locked; mmap_lock is still held.
1690 * false - folio is not locked.
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001691 * mmap_lock has been released (mmap_read_unlock(), unless flags had both
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001692 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001693 * which case mmap_lock is still held.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001694 *
Matthew Wilcox (Oracle)9138e472021-03-18 21:39:45 -04001695 * If neither ALLOW_RETRY nor KILLABLE are set, will always return true
1696 * with the folio locked and the mmap_lock unperturbed.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001697 */
Matthew Wilcox (Oracle)9138e472021-03-18 21:39:45 -04001698bool __folio_lock_or_retry(struct folio *folio, struct mm_struct *mm,
Michel Lespinassed065bd82010-10-26 14:21:57 -07001699 unsigned int flags)
1700{
Peter Xu4064b982020-04-01 21:08:45 -07001701 if (fault_flag_allow_retry_first(flags)) {
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001702 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001703 * CAUTION! In this case, mmap_lock is not released
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001704 * even though return 0.
1705 */
1706 if (flags & FAULT_FLAG_RETRY_NOWAIT)
Matthew Wilcox (Oracle)9138e472021-03-18 21:39:45 -04001707 return false;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001708
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001709 mmap_read_unlock(mm);
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001710 if (flags & FAULT_FLAG_KILLABLE)
Matthew Wilcox (Oracle)6baa8d62021-03-04 10:21:02 -05001711 folio_wait_locked_killable(folio);
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001712 else
Matthew Wilcox (Oracle)6baa8d62021-03-04 10:21:02 -05001713 folio_wait_locked(folio);
Matthew Wilcox (Oracle)9138e472021-03-18 21:39:45 -04001714 return false;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001715 }
Hailong Liu800bca72020-12-14 19:05:02 -08001716 if (flags & FAULT_FLAG_KILLABLE) {
Matthew Wilcox (Oracle)9138e472021-03-18 21:39:45 -04001717 bool ret;
Hailong Liu800bca72020-12-14 19:05:02 -08001718
Matthew Wilcox (Oracle)af7f29d2020-12-08 00:07:31 -05001719 ret = __folio_lock_killable(folio);
Hailong Liu800bca72020-12-14 19:05:02 -08001720 if (ret) {
1721 mmap_read_unlock(mm);
Matthew Wilcox (Oracle)9138e472021-03-18 21:39:45 -04001722 return false;
Hailong Liu800bca72020-12-14 19:05:02 -08001723 }
1724 } else {
Matthew Wilcox (Oracle)af7f29d2020-12-08 00:07:31 -05001725 __folio_lock(folio);
Hailong Liu800bca72020-12-14 19:05:02 -08001726 }
Hailong Liu800bca72020-12-14 19:05:02 -08001727
Matthew Wilcox (Oracle)9138e472021-03-18 21:39:45 -04001728 return true;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001729}
1730
Randy Dunlap485bb992006-06-23 02:03:49 -07001731/**
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001732 * page_cache_next_miss() - Find the next gap in the page cache.
1733 * @mapping: Mapping.
1734 * @index: Index.
1735 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001736 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001737 * Search the range [index, min(index + max_scan - 1, ULONG_MAX)] for the
1738 * gap with the lowest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001739 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001740 * This function may be called under the rcu_read_lock. However, this will
1741 * not atomically search a snapshot of the cache at a single point in time.
1742 * For example, if a gap is created at index 5, then subsequently a gap is
1743 * created at index 10, page_cache_next_miss covering both indices may
1744 * return 10 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001745 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001746 * Return: The index of the gap if found, otherwise an index outside the
1747 * range specified (in which case 'return - index >= max_scan' will be true).
1748 * In the rare case of index wrap-around, 0 will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001749 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001750pgoff_t page_cache_next_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001751 pgoff_t index, unsigned long max_scan)
1752{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001753 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001754
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001755 while (max_scan--) {
1756 void *entry = xas_next(&xas);
1757 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001758 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001759 if (xas.xa_index == 0)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001760 break;
1761 }
1762
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001763 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001764}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001765EXPORT_SYMBOL(page_cache_next_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001766
1767/**
Laurent Dufour2346a562019-05-13 17:21:29 -07001768 * page_cache_prev_miss() - Find the previous gap in the page cache.
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001769 * @mapping: Mapping.
1770 * @index: Index.
1771 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001772 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001773 * Search the range [max(index - max_scan + 1, 0), index] for the
1774 * gap with the highest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001775 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001776 * This function may be called under the rcu_read_lock. However, this will
1777 * not atomically search a snapshot of the cache at a single point in time.
1778 * For example, if a gap is created at index 10, then subsequently a gap is
1779 * created at index 5, page_cache_prev_miss() covering both indices may
1780 * return 5 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001781 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001782 * Return: The index of the gap if found, otherwise an index outside the
1783 * range specified (in which case 'index - return >= max_scan' will be true).
1784 * In the rare case of wrap-around, ULONG_MAX will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001785 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001786pgoff_t page_cache_prev_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001787 pgoff_t index, unsigned long max_scan)
1788{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001789 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001790
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001791 while (max_scan--) {
1792 void *entry = xas_prev(&xas);
1793 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001794 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001795 if (xas.xa_index == ULONG_MAX)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001796 break;
1797 }
1798
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001799 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001800}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001801EXPORT_SYMBOL(page_cache_prev_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001802
Matthew Wilcox (Oracle)44835d22021-02-25 17:15:36 -08001803/*
Matthew Wilcox (Oracle)020853b2021-05-10 16:33:22 -04001804 * Lockless page cache protocol:
1805 * On the lookup side:
1806 * 1. Load the folio from i_pages
1807 * 2. Increment the refcount if it's not zero
1808 * 3. If the folio is not found by xas_reload(), put the refcount and retry
1809 *
1810 * On the removal side:
1811 * A. Freeze the page (by zeroing the refcount if nobody else has a reference)
1812 * B. Remove the page from i_pages
1813 * C. Return the page to the page allocator
1814 *
1815 * This means that any page may have its reference count temporarily
1816 * increased by a speculative page cache (or fast GUP) lookup as it can
1817 * be allocated by another user before the RCU grace period expires.
1818 * Because the refcount temporarily acquired here may end up being the
1819 * last refcount on the page, any page allocation must be freeable by
1820 * folio_put().
1821 */
1822
1823/*
Matthew Wilcox (Oracle)bc5a3012021-02-25 17:15:40 -08001824 * mapping_get_entry - Get a page cache entry.
Randy Dunlap485bb992006-06-23 02:03:49 -07001825 * @mapping: the address_space to search
Matthew Wilcox (Oracle)a6de4b42020-10-13 16:51:34 -07001826 * @index: The page cache index.
Randy Dunlap485bb992006-06-23 02:03:49 -07001827 *
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001828 * Looks up the page cache entry at @mapping & @index. If it is a folio,
1829 * it is returned with an increased refcount. If it is a shadow entry
1830 * of a previously evicted folio, or a swap entry from shmem/tmpfs,
1831 * it is returned without further action.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001832 *
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001833 * Return: The folio, swap or shadow entry, %NULL if nothing is found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 */
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001835static void *mapping_get_entry(struct address_space *mapping, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Matthew Wilcox (Oracle)a6de4b42020-10-13 16:51:34 -07001837 XA_STATE(xas, &mapping->i_pages, index);
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001838 struct folio *folio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Nick Piggina60637c2008-07-25 19:45:31 -07001840 rcu_read_lock();
1841repeat:
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001842 xas_reset(&xas);
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001843 folio = xas_load(&xas);
1844 if (xas_retry(&xas, folio))
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001845 goto repeat;
1846 /*
1847 * A shadow entry of a recently evicted page, or a swap entry from
1848 * shmem/tmpfs. Return it without attempting to raise page count.
1849 */
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001850 if (!folio || xa_is_value(folio))
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001851 goto out;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001852
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001853 if (!folio_try_get_rcu(folio))
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001854 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001855
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001856 if (unlikely(folio != xas_reload(&xas))) {
1857 folio_put(folio);
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001858 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001859 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001860out:
Nick Piggina60637c2008-07-25 19:45:31 -07001861 rcu_read_unlock();
1862
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001863 return folio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Randy Dunlap485bb992006-06-23 02:03:49 -07001866/**
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001867 * __filemap_get_folio - Find and get a reference to a folio.
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001868 * @mapping: The address_space to search.
1869 * @index: The page index.
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001870 * @fgp_flags: %FGP flags modify how the folio is returned.
1871 * @gfp: Memory allocation flags to use if %FGP_CREAT is specified.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001872 *
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001873 * Looks up the page cache entry at @mapping & @index.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001874 *
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001875 * @fgp_flags can be zero or more of these flags:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001876 *
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001877 * * %FGP_ACCESSED - The folio will be marked accessed.
1878 * * %FGP_LOCK - The folio is returned locked.
Matthew Wilcox (Oracle)44835d22021-02-25 17:15:36 -08001879 * * %FGP_ENTRY - If there is a shadow / swap / DAX entry, return it
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001880 * instead of allocating a new folio to replace it.
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001881 * * %FGP_CREAT - If no page is present then a new page is allocated using
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001882 * @gfp and added to the page cache and the VM's LRU list.
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001883 * The page is returned locked and with an increased refcount.
1884 * * %FGP_FOR_MMAP - The caller wants to do its own locking dance if the
1885 * page is already in cache. If the page was allocated, unlock it before
1886 * returning so the caller can do the same dance.
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001887 * * %FGP_WRITE - The page will be written to by the caller.
1888 * * %FGP_NOFS - __GFP_FS will get cleared in gfp.
1889 * * %FGP_NOWAIT - Don't get blocked by page lock.
Matthew Wilcox (Oracle)b27652d92020-12-24 12:55:56 -05001890 * * %FGP_STABLE - Wait for the folio to be stable (finished writeback)
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03001891 *
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001892 * If %FGP_LOCK or %FGP_CREAT are specified then the function may sleep even
1893 * if the %GFP flags specified for %FGP_CREAT are atomic.
Mel Gorman2457aec2014-06-04 16:10:31 -07001894 *
1895 * If there is a page cache page, it is returned with an increased refcount.
Mike Rapoporta862f682019-03-05 15:48:42 -08001896 *
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001897 * Return: The found folio or %NULL otherwise.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001898 */
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001899struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
1900 int fgp_flags, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001902 struct folio *folio;
Mel Gorman2457aec2014-06-04 16:10:31 -07001903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904repeat:
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001905 folio = mapping_get_entry(mapping, index);
1906 if (xa_is_value(folio)) {
Matthew Wilcox (Oracle)44835d22021-02-25 17:15:36 -08001907 if (fgp_flags & FGP_ENTRY)
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001908 return folio;
1909 folio = NULL;
Matthew Wilcox (Oracle)44835d22021-02-25 17:15:36 -08001910 }
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001911 if (!folio)
Mel Gorman2457aec2014-06-04 16:10:31 -07001912 goto no_page;
1913
1914 if (fgp_flags & FGP_LOCK) {
1915 if (fgp_flags & FGP_NOWAIT) {
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001916 if (!folio_trylock(folio)) {
1917 folio_put(folio);
Mel Gorman2457aec2014-06-04 16:10:31 -07001918 return NULL;
1919 }
1920 } else {
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001921 folio_lock(folio);
Mel Gorman2457aec2014-06-04 16:10:31 -07001922 }
1923
1924 /* Has the page been truncated? */
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001925 if (unlikely(folio->mapping != mapping)) {
1926 folio_unlock(folio);
1927 folio_put(folio);
Mel Gorman2457aec2014-06-04 16:10:31 -07001928 goto repeat;
1929 }
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001930 VM_BUG_ON_FOLIO(!folio_contains(folio, index), folio);
Mel Gorman2457aec2014-06-04 16:10:31 -07001931 }
1932
Kirill Tkhaic16eb002018-12-28 00:37:35 -08001933 if (fgp_flags & FGP_ACCESSED)
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001934 folio_mark_accessed(folio);
Yang Shib9306a72020-08-06 23:19:55 -07001935 else if (fgp_flags & FGP_WRITE) {
1936 /* Clear idle flag for buffer write */
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001937 if (folio_test_idle(folio))
1938 folio_clear_idle(folio);
Yang Shib9306a72020-08-06 23:19:55 -07001939 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001940
Matthew Wilcox (Oracle)b27652d92020-12-24 12:55:56 -05001941 if (fgp_flags & FGP_STABLE)
1942 folio_wait_stable(folio);
Mel Gorman2457aec2014-06-04 16:10:31 -07001943no_page:
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001944 if (!folio && (fgp_flags & FGP_CREAT)) {
Mel Gorman2457aec2014-06-04 16:10:31 -07001945 int err;
Christoph Hellwigf56753a2020-09-24 08:51:40 +02001946 if ((fgp_flags & FGP_WRITE) && mapping_can_writeback(mapping))
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001947 gfp |= __GFP_WRITE;
Michal Hocko45f87de2014-12-29 20:30:35 +01001948 if (fgp_flags & FGP_NOFS)
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001949 gfp &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001950
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001951 folio = filemap_alloc_folio(gfp, 0);
1952 if (!folio)
Nick Piggineb2be182007-10-16 01:24:57 -07001953 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001954
Josef Bacika75d4c32019-03-13 11:44:14 -07001955 if (WARN_ON_ONCE(!(fgp_flags & (FGP_LOCK | FGP_FOR_MMAP))))
Mel Gorman2457aec2014-06-04 16:10:31 -07001956 fgp_flags |= FGP_LOCK;
1957
Hugh Dickinseb39d612014-08-06 16:06:43 -07001958 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001959 if (fgp_flags & FGP_ACCESSED)
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001960 __folio_set_referenced(folio);
Mel Gorman2457aec2014-06-04 16:10:31 -07001961
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001962 err = filemap_add_folio(mapping, folio, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07001963 if (unlikely(err)) {
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001964 folio_put(folio);
1965 folio = NULL;
Nick Piggineb2be182007-10-16 01:24:57 -07001966 if (err == -EEXIST)
1967 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
Josef Bacika75d4c32019-03-13 11:44:14 -07001969
1970 /*
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001971 * filemap_add_folio locks the page, and for mmap
1972 * we expect an unlocked page.
Josef Bacika75d4c32019-03-13 11:44:14 -07001973 */
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001974 if (folio && (fgp_flags & FGP_FOR_MMAP))
1975 folio_unlock(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001977
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001978 return folio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001980EXPORT_SYMBOL(__filemap_get_folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08001982static inline struct page *find_get_entry(struct xa_state *xas, pgoff_t max,
1983 xa_mark_t mark)
1984{
1985 struct page *page;
1986
1987retry:
1988 if (mark == XA_PRESENT)
1989 page = xas_find(xas, max);
1990 else
1991 page = xas_find_marked(xas, max, mark);
1992
1993 if (xas_retry(xas, page))
1994 goto retry;
1995 /*
1996 * A shadow entry of a recently evicted page, a swap
1997 * entry from shmem/tmpfs or a DAX entry. Return it
1998 * without attempting to raise page count.
1999 */
2000 if (!page || xa_is_value(page))
2001 return page;
2002
2003 if (!page_cache_get_speculative(page))
2004 goto reset;
2005
2006 /* Has the page moved or been split? */
2007 if (unlikely(page != xas_reload(xas))) {
2008 put_page(page);
2009 goto reset;
2010 }
2011
2012 return page;
2013reset:
2014 xas_reset(xas);
2015 goto retry;
2016}
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07002019 * find_get_entries - gang pagecache lookup
2020 * @mapping: The address_space to search
2021 * @start: The starting page cache index
Matthew Wilcox (Oracle)ca122fe42021-02-25 17:16:00 -08002022 * @end: The final page index (inclusive).
Matthew Wilcox (Oracle)cf2039a2021-02-25 17:16:11 -08002023 * @pvec: Where the resulting entries are placed.
Johannes Weiner0cd61442014-04-03 14:47:46 -07002024 * @indices: The cache indices corresponding to the entries in @entries
2025 *
Matthew Wilcox (Oracle)cf2039a2021-02-25 17:16:11 -08002026 * find_get_entries() will search for and return a batch of entries in
2027 * the mapping. The entries are placed in @pvec. find_get_entries()
2028 * takes a reference on any actual pages it returns.
Johannes Weiner0cd61442014-04-03 14:47:46 -07002029 *
2030 * The search returns a group of mapping-contiguous page cache entries
2031 * with ascending indexes. There may be holes in the indices due to
2032 * not-present pages.
2033 *
Johannes Weiner139b6a62014-05-06 12:50:05 -07002034 * Any shadow entries of evicted pages, or swap entries from
2035 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07002036 *
Hugh Dickins71725ed2020-04-06 20:07:57 -07002037 * If it finds a Transparent Huge Page, head or tail, find_get_entries()
2038 * stops at that page: the caller is likely to have a better way to handle
2039 * the compound page as a whole, and then skip its extent, than repeatedly
2040 * calling find_get_entries() to return all its tails.
2041 *
Mike Rapoporta862f682019-03-05 15:48:42 -08002042 * Return: the number of pages and shadow entries which were found.
Johannes Weiner0cd61442014-04-03 14:47:46 -07002043 */
Matthew Wilcox (Oracle)ca122fe42021-02-25 17:16:00 -08002044unsigned find_get_entries(struct address_space *mapping, pgoff_t start,
Matthew Wilcox (Oracle)cf2039a2021-02-25 17:16:11 -08002045 pgoff_t end, struct pagevec *pvec, pgoff_t *indices)
Johannes Weiner0cd61442014-04-03 14:47:46 -07002046{
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04002047 XA_STATE(xas, &mapping->i_pages, start);
2048 struct page *page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002049 unsigned int ret = 0;
Matthew Wilcox (Oracle)cf2039a2021-02-25 17:16:11 -08002050 unsigned nr_entries = PAGEVEC_SIZE;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002051
2052 rcu_read_lock();
Matthew Wilcox (Oracle)ca122fe42021-02-25 17:16:00 -08002053 while ((page = find_get_entry(&xas, end, XA_PRESENT))) {
Hugh Dickins71725ed2020-04-06 20:07:57 -07002054 /*
2055 * Terminate early on finding a THP, to allow the caller to
2056 * handle it all at once; but continue if this is hugetlbfs.
2057 */
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08002058 if (!xa_is_value(page) && PageTransHuge(page) &&
2059 !PageHuge(page)) {
Hugh Dickins71725ed2020-04-06 20:07:57 -07002060 page = find_subpage(page, xas.xa_index);
2061 nr_entries = ret + 1;
2062 }
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08002063
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04002064 indices[ret] = xas.xa_index;
Matthew Wilcox (Oracle)cf2039a2021-02-25 17:16:11 -08002065 pvec->pages[ret] = page;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002066 if (++ret == nr_entries)
2067 break;
2068 }
2069 rcu_read_unlock();
Matthew Wilcox (Oracle)cf2039a2021-02-25 17:16:11 -08002070
2071 pvec->nr = ret;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002072 return ret;
2073}
2074
2075/**
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002076 * find_lock_entries - Find a batch of pagecache entries.
2077 * @mapping: The address_space to search.
2078 * @start: The starting page cache index.
2079 * @end: The final page index (inclusive).
2080 * @pvec: Where the resulting entries are placed.
2081 * @indices: The cache indices of the entries in @pvec.
2082 *
2083 * find_lock_entries() will return a batch of entries from @mapping.
2084 * Swap, shadow and DAX entries are included. Pages are returned
2085 * locked and with an incremented refcount. Pages which are locked by
2086 * somebody else or under writeback are skipped. Only the head page of
2087 * a THP is returned. Pages which are partially outside the range are
2088 * not returned.
2089 *
2090 * The entries have ascending indexes. The indices may not be consecutive
2091 * due to not-present entries, THP pages, pages which could not be locked
2092 * or pages under writeback.
2093 *
2094 * Return: The number of entries which were found.
2095 */
2096unsigned find_lock_entries(struct address_space *mapping, pgoff_t start,
2097 pgoff_t end, struct pagevec *pvec, pgoff_t *indices)
2098{
2099 XA_STATE(xas, &mapping->i_pages, start);
2100 struct page *page;
2101
2102 rcu_read_lock();
2103 while ((page = find_get_entry(&xas, end, XA_PRESENT))) {
2104 if (!xa_is_value(page)) {
2105 if (page->index < start)
2106 goto put;
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002107 if (page->index + thp_nr_pages(page) - 1 > end)
2108 goto put;
2109 if (!trylock_page(page))
2110 goto put;
2111 if (page->mapping != mapping || PageWriteback(page))
2112 goto unlock;
2113 VM_BUG_ON_PAGE(!thp_contains(page, xas.xa_index),
2114 page);
2115 }
2116 indices[pvec->nr] = xas.xa_index;
2117 if (!pagevec_add(pvec, page))
2118 break;
2119 goto next;
2120unlock:
2121 unlock_page(page);
2122put:
2123 put_page(page);
2124next:
Hugh Dickins2d11e732021-04-23 14:28:57 -07002125 if (!xa_is_value(page) && PageTransHuge(page)) {
2126 unsigned int nr_pages = thp_nr_pages(page);
2127
2128 /* Final THP may cross MAX_LFS_FILESIZE on 32-bit */
2129 xas_set(&xas, page->index + nr_pages);
2130 if (xas.xa_index < nr_pages)
2131 break;
2132 }
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002133 }
2134 rcu_read_unlock();
2135
2136 return pagevec_count(pvec);
2137}
2138
2139/**
Jan Karab947cee2017-09-06 16:21:21 -07002140 * find_get_pages_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 * @mapping: The address_space to search
2142 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07002143 * @end: The final page index (inclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 * @nr_pages: The maximum number of pages
2145 * @pages: Where the resulting pages are placed
2146 *
Jan Karab947cee2017-09-06 16:21:21 -07002147 * find_get_pages_range() will search for and return a group of up to @nr_pages
2148 * pages in the mapping starting at index @start and up to index @end
2149 * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
2150 * a reference against the returned pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 *
2152 * The search returns a group of mapping-contiguous pages with ascending
2153 * indexes. There may be holes in the indices due to not-present pages.
Jan Karad72dc8a2017-09-06 16:21:18 -07002154 * We also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 *
Mike Rapoporta862f682019-03-05 15:48:42 -08002156 * Return: the number of pages which were found. If this number is
2157 * smaller than @nr_pages, the end of specified range has been
Jan Karab947cee2017-09-06 16:21:21 -07002158 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 */
Jan Karab947cee2017-09-06 16:21:21 -07002160unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
2161 pgoff_t end, unsigned int nr_pages,
2162 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04002164 XA_STATE(xas, &mapping->i_pages, *start);
2165 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07002166 unsigned ret = 0;
2167
2168 if (unlikely(!nr_pages))
2169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Nick Piggina60637c2008-07-25 19:45:31 -07002171 rcu_read_lock();
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08002172 while ((page = find_get_entry(&xas, end, XA_PRESENT))) {
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04002173 /* Skip over shadow, swap and DAX entries */
2174 if (xa_is_value(page))
Hugh Dickins8079b1c2011-08-03 16:21:28 -07002175 continue;
Nick Piggina60637c2008-07-25 19:45:31 -07002176
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002177 pages[ret] = find_subpage(page, xas.xa_index);
Jan Karab947cee2017-09-06 16:21:21 -07002178 if (++ret == nr_pages) {
Yu Zhao5d3ee422019-03-05 15:49:17 -08002179 *start = xas.xa_index + 1;
Jan Karab947cee2017-09-06 16:21:21 -07002180 goto out;
2181 }
Nick Piggina60637c2008-07-25 19:45:31 -07002182 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07002183
Jan Karab947cee2017-09-06 16:21:21 -07002184 /*
2185 * We come here when there is no page beyond @end. We take care to not
2186 * overflow the index @start as it confuses some of the callers. This
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04002187 * breaks the iteration when there is a page at index -1 but that is
Jan Karab947cee2017-09-06 16:21:21 -07002188 * already broken anyway.
2189 */
2190 if (end == (pgoff_t)-1)
2191 *start = (pgoff_t)-1;
2192 else
2193 *start = end + 1;
2194out:
Nick Piggina60637c2008-07-25 19:45:31 -07002195 rcu_read_unlock();
Jan Karad72dc8a2017-09-06 16:21:18 -07002196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return ret;
2198}
2199
Jens Axboeebf43502006-04-27 08:46:01 +02002200/**
2201 * find_get_pages_contig - gang contiguous pagecache lookup
2202 * @mapping: The address_space to search
2203 * @index: The starting page index
2204 * @nr_pages: The maximum number of pages
2205 * @pages: Where the resulting pages are placed
2206 *
2207 * find_get_pages_contig() works exactly like find_get_pages(), except
2208 * that the returned number of pages are guaranteed to be contiguous.
2209 *
Mike Rapoporta862f682019-03-05 15:48:42 -08002210 * Return: the number of pages which were found.
Jens Axboeebf43502006-04-27 08:46:01 +02002211 */
2212unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
2213 unsigned int nr_pages, struct page **pages)
2214{
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002215 XA_STATE(xas, &mapping->i_pages, index);
2216 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07002217 unsigned int ret = 0;
2218
2219 if (unlikely(!nr_pages))
2220 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02002221
Nick Piggina60637c2008-07-25 19:45:31 -07002222 rcu_read_lock();
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002223 for (page = xas_load(&xas); page; page = xas_next(&xas)) {
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002224 if (xas_retry(&xas, page))
2225 continue;
2226 /*
2227 * If the entry has been swapped out, we can stop looking.
2228 * No current caller is looking for DAX entries.
2229 */
2230 if (xa_is_value(page))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07002231 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07002232
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002233 if (!page_cache_get_speculative(page))
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002234 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07002235
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002236 /* Has the page moved or been split? */
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002237 if (unlikely(page != xas_reload(&xas)))
2238 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07002239
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002240 pages[ret] = find_subpage(page, xas.xa_index);
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07002241 if (++ret == nr_pages)
2242 break;
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002243 continue;
2244put_page:
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002245 put_page(page);
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002246retry:
2247 xas_reset(&xas);
Jens Axboeebf43502006-04-27 08:46:01 +02002248 }
Nick Piggina60637c2008-07-25 19:45:31 -07002249 rcu_read_unlock();
2250 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02002251}
David Howellsef71c152007-05-09 02:33:44 -07002252EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02002253
Randy Dunlap485bb992006-06-23 02:03:49 -07002254/**
Matthew Wilcox (Oracle)c49f50d2021-02-25 17:15:25 -08002255 * find_get_pages_range_tag - Find and return head pages matching @tag.
Randy Dunlap485bb992006-06-23 02:03:49 -07002256 * @mapping: the address_space to search
2257 * @index: the starting page index
Jan Kara72b045a2017-11-15 17:34:33 -08002258 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07002259 * @tag: the tag index
2260 * @nr_pages: the maximum number of pages
2261 * @pages: where the resulting pages are placed
2262 *
Matthew Wilcox (Oracle)c49f50d2021-02-25 17:15:25 -08002263 * Like find_get_pages(), except we only return head pages which are tagged
2264 * with @tag. @index is updated to the index immediately after the last
2265 * page we return, ready for the next iteration.
Mike Rapoporta862f682019-03-05 15:48:42 -08002266 *
2267 * Return: the number of pages which were found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 */
Jan Kara72b045a2017-11-15 17:34:33 -08002269unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
Matthew Wilcoxa6906972018-05-16 18:12:54 -04002270 pgoff_t end, xa_mark_t tag, unsigned int nr_pages,
Jan Kara72b045a2017-11-15 17:34:33 -08002271 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
Matthew Wilcoxa6906972018-05-16 18:12:54 -04002273 XA_STATE(xas, &mapping->i_pages, *index);
2274 struct page *page;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07002275 unsigned ret = 0;
2276
2277 if (unlikely(!nr_pages))
2278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Nick Piggina60637c2008-07-25 19:45:31 -07002280 rcu_read_lock();
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08002281 while ((page = find_get_entry(&xas, end, tag))) {
Matthew Wilcoxa6906972018-05-16 18:12:54 -04002282 /*
2283 * Shadow entries should never be tagged, but this iteration
2284 * is lockless so there is a window for page reclaim to evict
2285 * a page we saw tagged. Skip over it.
2286 */
2287 if (xa_is_value(page))
Johannes Weiner139b6a62014-05-06 12:50:05 -07002288 continue;
Nick Piggina60637c2008-07-25 19:45:31 -07002289
Matthew Wilcox (Oracle)c49f50d2021-02-25 17:15:25 -08002290 pages[ret] = page;
Jan Kara72b045a2017-11-15 17:34:33 -08002291 if (++ret == nr_pages) {
Matthew Wilcox (Oracle)c49f50d2021-02-25 17:15:25 -08002292 *index = page->index + thp_nr_pages(page);
Jan Kara72b045a2017-11-15 17:34:33 -08002293 goto out;
2294 }
Nick Piggina60637c2008-07-25 19:45:31 -07002295 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07002296
Jan Kara72b045a2017-11-15 17:34:33 -08002297 /*
Matthew Wilcoxa6906972018-05-16 18:12:54 -04002298 * We come here when we got to @end. We take care to not overflow the
Jan Kara72b045a2017-11-15 17:34:33 -08002299 * index @index as it confuses some of the callers. This breaks the
Matthew Wilcoxa6906972018-05-16 18:12:54 -04002300 * iteration when there is a page at index -1 but that is already
2301 * broken anyway.
Jan Kara72b045a2017-11-15 17:34:33 -08002302 */
2303 if (end == (pgoff_t)-1)
2304 *index = (pgoff_t)-1;
2305 else
2306 *index = end + 1;
2307out:
Nick Piggina60637c2008-07-25 19:45:31 -07002308 rcu_read_unlock();
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return ret;
2311}
Jan Kara72b045a2017-11-15 17:34:33 -08002312EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002314/*
2315 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
2316 * a _large_ part of the i/o request. Imagine the worst scenario:
2317 *
2318 * ---R__________________________________________B__________
2319 * ^ reading here ^ bad block(assume 4k)
2320 *
2321 * read(R) => miss => readahead(R...B) => media error => frustrating retries
2322 * => failing the whole request => read(R) => read(R+1) =>
2323 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
2324 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
2325 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
2326 *
2327 * It is going insane. Fix it by quickly scaling down the readahead size.
2328 */
Souptick Joarder0f8e2db2020-04-01 21:04:50 -07002329static void shrink_readahead_size_eio(struct file_ra_state *ra)
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002330{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002331 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002332}
2333
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002334/*
2335 * filemap_get_read_batch - Get a batch of pages for read
2336 *
2337 * Get a batch of pages which represent a contiguous range of bytes
2338 * in the file. No tail pages will be returned. If @index is in the
2339 * middle of a THP, the entire THP will be returned. The last page in
2340 * the batch may have Readahead set or be not Uptodate so that the
2341 * caller can take the appropriate action.
2342 */
2343static void filemap_get_read_batch(struct address_space *mapping,
2344 pgoff_t index, pgoff_t max, struct pagevec *pvec)
2345{
2346 XA_STATE(xas, &mapping->i_pages, index);
2347 struct page *head;
2348
2349 rcu_read_lock();
2350 for (head = xas_load(&xas); head; head = xas_next(&xas)) {
2351 if (xas_retry(&xas, head))
2352 continue;
2353 if (xas.xa_index > max || xa_is_value(head))
2354 break;
2355 if (!page_cache_get_speculative(head))
2356 goto retry;
2357
2358 /* Has the page moved or been split? */
2359 if (unlikely(head != xas_reload(&xas)))
2360 goto put_page;
2361
2362 if (!pagevec_add(pvec, head))
2363 break;
2364 if (!PageUptodate(head))
2365 break;
2366 if (PageReadahead(head))
2367 break;
2368 xas.xa_index = head->index + thp_nr_pages(head) - 1;
2369 xas.xa_offset = (xas.xa_index >> xas.xa_shift) & XA_CHUNK_MASK;
2370 continue;
2371put_page:
2372 put_page(head);
2373retry:
2374 xas_reset(&xas);
2375 }
2376 rcu_read_unlock();
2377}
2378
Matthew Wilcox (Oracle)68430302021-02-24 12:02:15 -08002379static int filemap_read_page(struct file *file, struct address_space *mapping,
2380 struct page *page)
Kent Overstreet723ef242020-12-14 19:04:52 -08002381{
Kent Overstreet723ef242020-12-14 19:04:52 -08002382 int error;
2383
Kent Overstreet723ef242020-12-14 19:04:52 -08002384 /*
Matthew Wilcox (Oracle)68430302021-02-24 12:02:15 -08002385 * A previous I/O error may have been due to temporary failures,
2386 * eg. multipath errors. PG_error will be set again if readpage
2387 * fails.
Kent Overstreet723ef242020-12-14 19:04:52 -08002388 */
2389 ClearPageError(page);
2390 /* Start the actual read. The read will unlock the page. */
Matthew Wilcox (Oracle)68430302021-02-24 12:02:15 -08002391 error = mapping->a_ops->readpage(file, page);
2392 if (error)
2393 return error;
Kent Overstreet723ef242020-12-14 19:04:52 -08002394
Matthew Wilcox (Oracle)aa1ec2f2021-02-24 12:02:38 -08002395 error = wait_on_page_locked_killable(page);
Matthew Wilcox (Oracle)68430302021-02-24 12:02:15 -08002396 if (error)
2397 return error;
Matthew Wilcox (Oracle)aa1ec2f2021-02-24 12:02:38 -08002398 if (PageUptodate(page))
2399 return 0;
Matthew Wilcox (Oracle)aa1ec2f2021-02-24 12:02:38 -08002400 shrink_readahead_size_eio(&file->f_ra);
2401 return -EIO;
Kent Overstreet723ef242020-12-14 19:04:52 -08002402}
2403
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002404static bool filemap_range_uptodate(struct address_space *mapping,
2405 loff_t pos, struct iov_iter *iter, struct page *page)
2406{
2407 int count;
2408
2409 if (PageUptodate(page))
2410 return true;
2411 /* pipes can't handle partially uptodate pages */
2412 if (iov_iter_is_pipe(iter))
2413 return false;
2414 if (!mapping->a_ops->is_partially_uptodate)
2415 return false;
2416 if (mapping->host->i_blkbits >= (PAGE_SHIFT + thp_order(page)))
2417 return false;
2418
2419 count = iter->count;
2420 if (page_offset(page) > pos) {
2421 count -= page_offset(page) - pos;
2422 pos = 0;
2423 } else {
2424 pos -= page_offset(page);
2425 }
2426
2427 return mapping->a_ops->is_partially_uptodate(page, pos, count);
2428}
2429
Matthew Wilcox (Oracle)4612aee2021-02-24 12:02:22 -08002430static int filemap_update_page(struct kiocb *iocb,
2431 struct address_space *mapping, struct iov_iter *iter,
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002432 struct page *page)
Kent Overstreet723ef242020-12-14 19:04:52 -08002433{
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05002434 struct folio *folio = page_folio(page);
Kent Overstreet723ef242020-12-14 19:04:52 -08002435 int error;
2436
Jan Kara730633f2021-01-28 19:19:45 +01002437 if (iocb->ki_flags & IOCB_NOWAIT) {
2438 if (!filemap_invalidate_trylock_shared(mapping))
Matthew Wilcox (Oracle)87d1d7b2021-02-24 12:02:25 -08002439 return -EAGAIN;
Jan Kara730633f2021-01-28 19:19:45 +01002440 } else {
2441 filemap_invalidate_lock_shared(mapping);
2442 }
2443
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05002444 if (!folio_trylock(folio)) {
Jan Kara730633f2021-01-28 19:19:45 +01002445 error = -EAGAIN;
2446 if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_NOIO))
2447 goto unlock_mapping;
Matthew Wilcox (Oracle)87d1d7b2021-02-24 12:02:25 -08002448 if (!(iocb->ki_flags & IOCB_WAITQ)) {
Jan Kara730633f2021-01-28 19:19:45 +01002449 filemap_invalidate_unlock_shared(mapping);
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04002450 /*
2451 * This is where we usually end up waiting for a
2452 * previously submitted readahead to finish.
2453 */
2454 folio_put_wait_locked(folio, TASK_KILLABLE);
Matthew Wilcox (Oracle)4612aee2021-02-24 12:02:22 -08002455 return AOP_TRUNCATED_PAGE;
Matthew Wilcox (Oracle)bd8a1f32021-02-24 12:02:05 -08002456 }
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05002457 error = __folio_lock_async(folio, iocb->ki_waitq);
Matthew Wilcox (Oracle)87d1d7b2021-02-24 12:02:25 -08002458 if (error)
Jan Kara730633f2021-01-28 19:19:45 +01002459 goto unlock_mapping;
Kent Overstreet723ef242020-12-14 19:04:52 -08002460 }
Kent Overstreet723ef242020-12-14 19:04:52 -08002461
Jan Kara730633f2021-01-28 19:19:45 +01002462 error = AOP_TRUNCATED_PAGE;
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05002463 if (!folio->mapping)
Jan Kara730633f2021-01-28 19:19:45 +01002464 goto unlock;
Kent Overstreet723ef242020-12-14 19:04:52 -08002465
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002466 error = 0;
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05002467 if (filemap_range_uptodate(mapping, iocb->ki_pos, iter, &folio->page))
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002468 goto unlock;
2469
2470 error = -EAGAIN;
2471 if (iocb->ki_flags & (IOCB_NOIO | IOCB_NOWAIT | IOCB_WAITQ))
2472 goto unlock;
2473
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05002474 error = filemap_read_page(iocb->ki_filp, mapping, &folio->page);
Jan Kara730633f2021-01-28 19:19:45 +01002475 goto unlock_mapping;
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002476unlock:
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05002477 folio_unlock(folio);
Jan Kara730633f2021-01-28 19:19:45 +01002478unlock_mapping:
2479 filemap_invalidate_unlock_shared(mapping);
2480 if (error == AOP_TRUNCATED_PAGE)
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05002481 folio_put(folio);
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002482 return error;
Kent Overstreet723ef242020-12-14 19:04:52 -08002483}
2484
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002485static int filemap_create_page(struct file *file,
2486 struct address_space *mapping, pgoff_t index,
2487 struct pagevec *pvec)
Kent Overstreet723ef242020-12-14 19:04:52 -08002488{
Kent Overstreet723ef242020-12-14 19:04:52 -08002489 struct page *page;
2490 int error;
2491
Kent Overstreet723ef242020-12-14 19:04:52 -08002492 page = page_cache_alloc(mapping);
2493 if (!page)
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002494 return -ENOMEM;
Kent Overstreet723ef242020-12-14 19:04:52 -08002495
Jan Kara730633f2021-01-28 19:19:45 +01002496 /*
2497 * Protect against truncate / hole punch. Grabbing invalidate_lock here
2498 * assures we cannot instantiate and bring uptodate new pagecache pages
2499 * after evicting page cache during truncate and before actually
2500 * freeing blocks. Note that we could release invalidate_lock after
2501 * inserting the page into page cache as the locked page would then be
2502 * enough to synchronize with hole punching. But there are code paths
2503 * such as filemap_update_page() filling in partially uptodate pages or
2504 * ->readpages() that need to hold invalidate_lock while mapping blocks
2505 * for IO so let's hold the lock here as well to keep locking rules
2506 * simple.
2507 */
2508 filemap_invalidate_lock_shared(mapping);
Kent Overstreet723ef242020-12-14 19:04:52 -08002509 error = add_to_page_cache_lru(page, mapping, index,
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002510 mapping_gfp_constraint(mapping, GFP_KERNEL));
2511 if (error == -EEXIST)
2512 error = AOP_TRUNCATED_PAGE;
2513 if (error)
2514 goto error;
2515
2516 error = filemap_read_page(file, mapping, page);
2517 if (error)
2518 goto error;
2519
Jan Kara730633f2021-01-28 19:19:45 +01002520 filemap_invalidate_unlock_shared(mapping);
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002521 pagevec_add(pvec, page);
2522 return 0;
2523error:
Jan Kara730633f2021-01-28 19:19:45 +01002524 filemap_invalidate_unlock_shared(mapping);
Matthew Wilcox (Oracle)68430302021-02-24 12:02:15 -08002525 put_page(page);
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002526 return error;
Kent Overstreet723ef242020-12-14 19:04:52 -08002527}
2528
Matthew Wilcox (Oracle)5963fe02021-02-24 12:02:32 -08002529static int filemap_readahead(struct kiocb *iocb, struct file *file,
2530 struct address_space *mapping, struct page *page,
2531 pgoff_t last_index)
2532{
2533 if (iocb->ki_flags & IOCB_NOIO)
2534 return -EAGAIN;
2535 page_cache_async_readahead(mapping, &file->f_ra, file, page,
2536 page->index, last_index - page->index);
2537 return 0;
2538}
2539
Matthew Wilcox (Oracle)3a6bae42021-02-24 12:01:49 -08002540static int filemap_get_pages(struct kiocb *iocb, struct iov_iter *iter,
Matthew Wilcox (Oracle)ff993ba2021-02-24 12:01:55 -08002541 struct pagevec *pvec)
Kent Overstreet06c04442020-12-14 19:04:56 -08002542{
2543 struct file *filp = iocb->ki_filp;
2544 struct address_space *mapping = filp->f_mapping;
2545 struct file_ra_state *ra = &filp->f_ra;
2546 pgoff_t index = iocb->ki_pos >> PAGE_SHIFT;
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002547 pgoff_t last_index;
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002548 struct page *page;
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002549 int err = 0;
Kent Overstreet06c04442020-12-14 19:04:56 -08002550
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002551 last_index = DIV_ROUND_UP(iocb->ki_pos + iter->count, PAGE_SIZE);
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002552retry:
Kent Overstreet06c04442020-12-14 19:04:56 -08002553 if (fatal_signal_pending(current))
2554 return -EINTR;
2555
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002556 filemap_get_read_batch(mapping, index, last_index, pvec);
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002557 if (!pagevec_count(pvec)) {
2558 if (iocb->ki_flags & IOCB_NOIO)
2559 return -EAGAIN;
2560 page_cache_sync_readahead(mapping, ra, filp, index,
2561 last_index - index);
2562 filemap_get_read_batch(mapping, index, last_index, pvec);
2563 }
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002564 if (!pagevec_count(pvec)) {
2565 if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_WAITQ))
2566 return -EAGAIN;
2567 err = filemap_create_page(filp, mapping,
2568 iocb->ki_pos >> PAGE_SHIFT, pvec);
2569 if (err == AOP_TRUNCATED_PAGE)
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002570 goto retry;
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002571 return err;
2572 }
Kent Overstreet06c04442020-12-14 19:04:56 -08002573
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002574 page = pvec->pages[pagevec_count(pvec) - 1];
2575 if (PageReadahead(page)) {
2576 err = filemap_readahead(iocb, filp, mapping, page, last_index);
2577 if (err)
2578 goto err;
2579 }
2580 if (!PageUptodate(page)) {
2581 if ((iocb->ki_flags & IOCB_WAITQ) && pagevec_count(pvec) > 1)
2582 iocb->ki_flags |= IOCB_NOWAIT;
2583 err = filemap_update_page(iocb, mapping, iter, page);
2584 if (err)
2585 goto err;
Kent Overstreet06c04442020-12-14 19:04:56 -08002586 }
2587
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002588 return 0;
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002589err:
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002590 if (err < 0)
2591 put_page(page);
2592 if (likely(--pvec->nr))
Matthew Wilcox (Oracle)ff993ba2021-02-24 12:01:55 -08002593 return 0;
Matthew Wilcox (Oracle)4612aee2021-02-24 12:02:22 -08002594 if (err == AOP_TRUNCATED_PAGE)
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002595 goto retry;
2596 return err;
Kent Overstreet06c04442020-12-14 19:04:56 -08002597}
2598
Randy Dunlap485bb992006-06-23 02:03:49 -07002599/**
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002600 * filemap_read - Read data from the page cache.
2601 * @iocb: The iocb to read.
2602 * @iter: Destination for the data.
2603 * @already_read: Number of bytes already read by the caller.
Randy Dunlap485bb992006-06-23 02:03:49 -07002604 *
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002605 * Copies data from the page cache. If the data is not currently present,
2606 * uses the readahead and readpage address_space operations to fetch it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 *
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002608 * Return: Total number of bytes copied, including those already read by
2609 * the caller. If an error happens before any bytes are copied, returns
2610 * a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 */
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002612ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
2613 ssize_t already_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002615 struct file *filp = iocb->ki_filp;
Kent Overstreet06c04442020-12-14 19:04:56 -08002616 struct file_ra_state *ra = &filp->f_ra;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002617 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 struct inode *inode = mapping->host;
Matthew Wilcox (Oracle)ff993ba2021-02-24 12:01:55 -08002619 struct pagevec pvec;
2620 int i, error = 0;
Kent Overstreet06c04442020-12-14 19:04:56 -08002621 bool writably_mapped;
2622 loff_t isize, end_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Kent Overstreet723ef242020-12-14 19:04:52 -08002624 if (unlikely(iocb->ki_pos >= inode->i_sb->s_maxbytes))
Linus Torvaldsd05c5f72016-12-14 12:45:25 -08002625 return 0;
Kent Overstreet3644e2d2020-12-18 04:07:11 -05002626 if (unlikely(!iov_iter_count(iter)))
2627 return 0;
2628
Wei Fangc2a97372016-10-07 17:01:52 -07002629 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002630 pagevec_init(&pvec);
Wei Fangc2a97372016-10-07 17:01:52 -07002631
Kent Overstreet06c04442020-12-14 19:04:56 -08002632 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 cond_resched();
Kent Overstreet06c04442020-12-14 19:04:56 -08002634
2635 /*
2636 * If we've already successfully copied some data, then we
2637 * can no longer safely return -EIOCBQUEUED. Hence mark
2638 * an async read NOWAIT at that point.
2639 */
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002640 if ((iocb->ki_flags & IOCB_WAITQ) && already_read)
Kent Overstreet06c04442020-12-14 19:04:56 -08002641 iocb->ki_flags |= IOCB_NOWAIT;
2642
David Howells8c8387e2021-11-05 13:36:49 -07002643 if (unlikely(iocb->ki_pos >= i_size_read(inode)))
2644 break;
2645
Matthew Wilcox (Oracle)ff993ba2021-02-24 12:01:55 -08002646 error = filemap_get_pages(iocb, iter, &pvec);
2647 if (error < 0)
Kent Overstreet06c04442020-12-14 19:04:56 -08002648 break;
Michal Hocko5abf1862017-02-03 13:13:29 -08002649
Kent Overstreet723ef242020-12-14 19:04:52 -08002650 /*
Kent Overstreet06c04442020-12-14 19:04:56 -08002651 * i_size must be checked after we know the pages are Uptodate.
2652 *
2653 * Checking i_size after the check allows us to calculate
2654 * the correct value for "nr", which means the zero-filled
2655 * part of the page is not copied back to userspace (unless
2656 * another truncate extends the file - this is desired though).
Kent Overstreet723ef242020-12-14 19:04:52 -08002657 */
Kent Overstreet06c04442020-12-14 19:04:56 -08002658 isize = i_size_read(inode);
2659 if (unlikely(iocb->ki_pos >= isize))
2660 goto put_pages;
Kent Overstreet06c04442020-12-14 19:04:56 -08002661 end_offset = min_t(loff_t, isize, iocb->ki_pos + iter->count);
2662
Kent Overstreet06c04442020-12-14 19:04:56 -08002663 /*
2664 * Once we start copying data, we don't want to be touching any
2665 * cachelines that might be contended:
2666 */
2667 writably_mapped = mapping_writably_mapped(mapping);
2668
2669 /*
2670 * When a sequential read accesses a page several times, only
2671 * mark it as accessed the first time.
2672 */
2673 if (iocb->ki_pos >> PAGE_SHIFT !=
2674 ra->prev_pos >> PAGE_SHIFT)
Matthew Wilcox (Oracle)ff993ba2021-02-24 12:01:55 -08002675 mark_page_accessed(pvec.pages[0]);
Kent Overstreet06c04442020-12-14 19:04:56 -08002676
Matthew Wilcox (Oracle)ff993ba2021-02-24 12:01:55 -08002677 for (i = 0; i < pagevec_count(&pvec); i++) {
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002678 struct page *page = pvec.pages[i];
2679 size_t page_size = thp_size(page);
2680 size_t offset = iocb->ki_pos & (page_size - 1);
2681 size_t bytes = min_t(loff_t, end_offset - iocb->ki_pos,
2682 page_size - offset);
2683 size_t copied;
Kent Overstreet06c04442020-12-14 19:04:56 -08002684
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002685 if (end_offset < page_offset(page))
2686 break;
2687 if (i > 0)
2688 mark_page_accessed(page);
Kent Overstreet06c04442020-12-14 19:04:56 -08002689 /*
2690 * If users can be writing to this page using arbitrary
2691 * virtual addresses, take care about potential aliasing
2692 * before reading the page on the kernel side.
2693 */
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002694 if (writably_mapped) {
2695 int j;
Kent Overstreet06c04442020-12-14 19:04:56 -08002696
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002697 for (j = 0; j < thp_nr_pages(page); j++)
2698 flush_dcache_page(page + j);
2699 }
2700
2701 copied = copy_page_to_iter(page, offset, bytes, iter);
Kent Overstreet06c04442020-12-14 19:04:56 -08002702
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002703 already_read += copied;
Kent Overstreet06c04442020-12-14 19:04:56 -08002704 iocb->ki_pos += copied;
2705 ra->prev_pos = iocb->ki_pos;
2706
2707 if (copied < bytes) {
2708 error = -EFAULT;
2709 break;
Kent Overstreet723ef242020-12-14 19:04:52 -08002710 }
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002711 }
Kent Overstreet06c04442020-12-14 19:04:56 -08002712put_pages:
Matthew Wilcox (Oracle)ff993ba2021-02-24 12:01:55 -08002713 for (i = 0; i < pagevec_count(&pvec); i++)
2714 put_page(pvec.pages[i]);
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002715 pagevec_reinit(&pvec);
Kent Overstreet06c04442020-12-14 19:04:56 -08002716 } while (iov_iter_count(iter) && iocb->ki_pos < isize && !error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002718 file_accessed(filp);
Kent Overstreet06c04442020-12-14 19:04:56 -08002719
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002720 return already_read ? already_read : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002722EXPORT_SYMBOL_GPL(filemap_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Randy Dunlap485bb992006-06-23 02:03:49 -07002724/**
Al Viro6abd2322014-04-04 14:20:57 -04002725 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002726 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002727 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002728 *
Al Viro6abd2322014-04-04 14:20:57 -04002729 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 * that can use the page cache directly.
Andreas Gruenbacher41da51b2019-11-21 23:25:07 +00002731 *
2732 * The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall
2733 * be returned when no data can be read without waiting for I/O requests
2734 * to complete; it doesn't prevent readahead.
2735 *
2736 * The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O
2737 * requests shall be made for the read or for readahead. When no data
2738 * can be read, -EAGAIN shall be returned. When readahead would be
2739 * triggered, a partial, possibly empty read shall be returned.
2740 *
Mike Rapoporta862f682019-03-05 15:48:42 -08002741 * Return:
2742 * * number of bytes copied, even for partial reads
Andreas Gruenbacher41da51b2019-11-21 23:25:07 +00002743 * * negative error code (or 0 if IOCB_NOIO) if nothing was read
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 */
2745ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002746generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747{
Nicolai Stangee7080a42016-03-25 14:22:14 -07002748 size_t count = iov_iter_count(iter);
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002749 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002750
2751 if (!count)
Christoph Hellwig826ea862021-02-24 12:02:45 -08002752 return 0; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Al Viro2ba48ce2015-04-09 13:52:01 -04002754 if (iocb->ki_flags & IOCB_DIRECT) {
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002755 struct file *file = iocb->ki_filp;
Al Viroed978a82014-03-05 22:53:04 -05002756 struct address_space *mapping = file->f_mapping;
2757 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002759 if (iocb->ki_flags & IOCB_NOWAIT) {
Jens Axboe7a60d6d2021-04-29 22:55:21 -07002760 if (filemap_range_needs_writeback(mapping, iocb->ki_pos,
2761 iocb->ki_pos + count - 1))
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002762 return -EAGAIN;
2763 } else {
2764 retval = filemap_write_and_wait_range(mapping,
2765 iocb->ki_pos,
2766 iocb->ki_pos + count - 1);
2767 if (retval < 0)
Christoph Hellwig826ea862021-02-24 12:02:45 -08002768 return retval;
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002769 }
Al Viroed978a82014-03-05 22:53:04 -05002770
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002771 file_accessed(file);
2772
Al Viro5ecda132017-04-13 14:13:36 -04002773 retval = mapping->a_ops->direct_IO(iocb, iter);
Al Viroc3a69022016-10-10 13:26:27 -04002774 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002775 iocb->ki_pos += retval;
Al Viro5ecda132017-04-13 14:13:36 -04002776 count -= retval;
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002777 }
Pavel Begunkovab2125d2021-02-24 12:01:45 -08002778 if (retval != -EIOCBQUEUED)
2779 iov_iter_revert(iter, count - iov_iter_count(iter));
Josef Bacik66f998f2010-05-23 11:00:54 -04002780
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002781 /*
2782 * Btrfs can have a short DIO read if we encounter
2783 * compressed extents, so if there was an error, or if
2784 * we've already read everything we wanted to, or if
2785 * there was a short read because we hit EOF, go ahead
2786 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002787 * the rest of the read. Buffered reads will not work for
2788 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002789 */
Jens Axboe61d00172021-11-05 13:37:07 -07002790 if (retval < 0 || !count || IS_DAX(inode))
2791 return retval;
2792 if (iocb->ki_pos >= i_size_read(inode))
Christoph Hellwig826ea862021-02-24 12:02:45 -08002793 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 }
2795
Christoph Hellwig826ea862021-02-24 12:02:45 -08002796 return filemap_read(iocb, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797}
Al Viroed978a82014-03-05 22:53:04 -05002798EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -08002800static inline loff_t page_seek_hole_data(struct xa_state *xas,
2801 struct address_space *mapping, struct page *page,
2802 loff_t start, loff_t end, bool seek_data)
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002803{
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -08002804 const struct address_space_operations *ops = mapping->a_ops;
2805 size_t offset, bsz = i_blocksize(mapping->host);
2806
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002807 if (xa_is_value(page) || PageUptodate(page))
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -08002808 return seek_data ? start : end;
2809 if (!ops->is_partially_uptodate)
2810 return seek_data ? end : start;
2811
2812 xas_pause(xas);
2813 rcu_read_unlock();
2814 lock_page(page);
2815 if (unlikely(page->mapping != mapping))
2816 goto unlock;
2817
2818 offset = offset_in_thp(page, start) & ~(bsz - 1);
2819
2820 do {
2821 if (ops->is_partially_uptodate(page, offset, bsz) == seek_data)
2822 break;
2823 start = (start + bsz) & ~(bsz - 1);
2824 offset += bsz;
2825 } while (offset < thp_size(page));
2826unlock:
2827 unlock_page(page);
2828 rcu_read_lock();
2829 return start;
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002830}
2831
2832static inline
2833unsigned int seek_page_size(struct xa_state *xas, struct page *page)
2834{
2835 if (xa_is_value(page))
2836 return PAGE_SIZE << xa_get_order(xas->xa, xas->xa_index);
2837 return thp_size(page);
2838}
2839
2840/**
2841 * mapping_seek_hole_data - Seek for SEEK_DATA / SEEK_HOLE in the page cache.
2842 * @mapping: Address space to search.
2843 * @start: First byte to consider.
2844 * @end: Limit of search (exclusive).
2845 * @whence: Either SEEK_HOLE or SEEK_DATA.
2846 *
2847 * If the page cache knows which blocks contain holes and which blocks
2848 * contain data, your filesystem can use this function to implement
2849 * SEEK_HOLE and SEEK_DATA. This is useful for filesystems which are
2850 * entirely memory-based such as tmpfs, and filesystems which support
2851 * unwritten extents.
2852 *
Ingo Molnarf0953a12021-05-06 18:06:47 -07002853 * Return: The requested offset on success, or -ENXIO if @whence specifies
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002854 * SEEK_DATA and there is no data after @start. There is an implicit hole
2855 * after @end - 1, so SEEK_HOLE returns @end if all the bytes between @start
2856 * and @end contain data.
2857 */
2858loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start,
2859 loff_t end, int whence)
2860{
2861 XA_STATE(xas, &mapping->i_pages, start >> PAGE_SHIFT);
Hugh Dickinsed98b012021-04-23 14:29:00 -07002862 pgoff_t max = (end - 1) >> PAGE_SHIFT;
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002863 bool seek_data = (whence == SEEK_DATA);
2864 struct page *page;
2865
2866 if (end <= start)
2867 return -ENXIO;
2868
2869 rcu_read_lock();
2870 while ((page = find_get_entry(&xas, max, XA_PRESENT))) {
Hugh Dickinsed98b012021-04-23 14:29:00 -07002871 loff_t pos = (u64)xas.xa_index << PAGE_SHIFT;
2872 unsigned int seek_size;
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002873
2874 if (start < pos) {
2875 if (!seek_data)
2876 goto unlock;
2877 start = pos;
2878 }
2879
Hugh Dickinsed98b012021-04-23 14:29:00 -07002880 seek_size = seek_page_size(&xas, page);
2881 pos = round_up(pos + 1, seek_size);
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -08002882 start = page_seek_hole_data(&xas, mapping, page, start, pos,
2883 seek_data);
2884 if (start < pos)
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002885 goto unlock;
Hugh Dickinsed98b012021-04-23 14:29:00 -07002886 if (start >= end)
2887 break;
2888 if (seek_size > PAGE_SIZE)
2889 xas_set(&xas, pos >> PAGE_SHIFT);
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002890 if (!xa_is_value(page))
2891 put_page(page);
2892 }
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002893 if (seek_data)
Hugh Dickinsed98b012021-04-23 14:29:00 -07002894 start = -ENXIO;
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002895unlock:
2896 rcu_read_unlock();
Hugh Dickinsed98b012021-04-23 14:29:00 -07002897 if (page && !xa_is_value(page))
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002898 put_page(page);
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002899 if (start > end)
2900 return end;
2901 return start;
2902}
2903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905#define MMAP_LOTSAMISS (100)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002906/*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002907 * lock_page_maybe_drop_mmap - lock the page, possibly dropping the mmap_lock
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002908 * @vmf - the vm_fault for this fault.
2909 * @page - the page to lock.
2910 * @fpin - the pointer to the file we may pin (or is already pinned).
2911 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002912 * This works similar to lock_page_or_retry in that it can drop the mmap_lock.
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002913 * It differs in that it actually returns the page locked if it returns 1 and 0
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002914 * 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 -07002915 * will point to the pinned file and needs to be fput()'ed at a later point.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002916 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002917static int lock_page_maybe_drop_mmap(struct vm_fault *vmf, struct page *page,
2918 struct file **fpin)
2919{
Matthew Wilcox (Oracle)7c23c782021-03-01 19:38:25 -05002920 struct folio *folio = page_folio(page);
2921
2922 if (folio_trylock(folio))
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002923 return 1;
2924
Linus Torvalds8b0f9fa2019-03-15 11:26:07 -07002925 /*
2926 * NOTE! This will make us return with VM_FAULT_RETRY, but with
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002927 * the mmap_lock still held. That's how FAULT_FLAG_RETRY_NOWAIT
Linus Torvalds8b0f9fa2019-03-15 11:26:07 -07002928 * is supposed to work. We have way too many special cases..
2929 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002930 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
2931 return 0;
2932
2933 *fpin = maybe_unlock_mmap_for_io(vmf, *fpin);
2934 if (vmf->flags & FAULT_FLAG_KILLABLE) {
Matthew Wilcox (Oracle)af7f29d2020-12-08 00:07:31 -05002935 if (__folio_lock_killable(folio)) {
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002936 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002937 * We didn't have the right flags to drop the mmap_lock,
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002938 * but all fault_handlers only check for fatal signals
2939 * if we return VM_FAULT_RETRY, so we need to drop the
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002940 * mmap_lock here and return 0 if we don't have a fpin.
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002941 */
2942 if (*fpin == NULL)
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002943 mmap_read_unlock(vmf->vma->vm_mm);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002944 return 0;
2945 }
2946 } else
Matthew Wilcox (Oracle)7c23c782021-03-01 19:38:25 -05002947 __folio_lock(folio);
2948
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002949 return 1;
2950}
2951
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002952/*
2953 * Synchronous readahead happens when we don't even find a page in the page
2954 * cache at all. We don't want to perform IO under the mmap sem, so if we have
2955 * to drop the mmap sem we return the file that was pinned in order for us to do
2956 * that. If we didn't pin a file then we return NULL. The file that is
2957 * returned needs to be fput()'ed when we're done with it.
2958 */
2959static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002960{
Josef Bacik2a1180f2019-03-13 11:44:18 -07002961 struct file *file = vmf->vma->vm_file;
2962 struct file_ra_state *ra = &file->f_ra;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002963 struct address_space *mapping = file->f_mapping;
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +01002964 DEFINE_READAHEAD(ractl, file, ra, mapping, vmf->pgoff);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002965 struct file *fpin = NULL;
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07002966 unsigned int mmap_miss;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002967
2968 /* If we don't want any read-ahead, don't bother */
Josef Bacik2a1180f2019-03-13 11:44:18 -07002969 if (vmf->vma->vm_flags & VM_RAND_READ)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002970 return fpin;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002971 if (!ra->ra_pages)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002972 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002973
Josef Bacik2a1180f2019-03-13 11:44:18 -07002974 if (vmf->vma->vm_flags & VM_SEQ_READ) {
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002975 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +01002976 page_cache_sync_ra(&ractl, ra->ra_pages);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002977 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002978 }
2979
Andi Kleen207d04b2011-05-24 17:12:29 -07002980 /* Avoid banging the cache line if not needed */
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07002981 mmap_miss = READ_ONCE(ra->mmap_miss);
2982 if (mmap_miss < MMAP_LOTSAMISS * 10)
2983 WRITE_ONCE(ra->mmap_miss, ++mmap_miss);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002984
2985 /*
2986 * Do we miss much more than hit in this file? If so,
2987 * stop bothering with read-ahead. It will only hurt.
2988 */
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07002989 if (mmap_miss > MMAP_LOTSAMISS)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002990 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002991
Wu Fengguangd30a1102009-06-16 15:31:30 -07002992 /*
2993 * mmap read-around
2994 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002995 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
David Howellsdb660d42020-10-15 20:06:31 -07002996 ra->start = max_t(long, 0, vmf->pgoff - ra->ra_pages / 2);
Roman Gushchin600e19a2015-11-05 18:47:08 -08002997 ra->size = ra->ra_pages;
2998 ra->async_size = ra->ra_pages / 4;
David Howellsdb660d42020-10-15 20:06:31 -07002999 ractl._index = ra->start;
3000 do_page_cache_ra(&ractl, ra->size, ra->async_size);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003001 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07003002}
3003
3004/*
3005 * Asynchronous readahead happens when we find the page and PG_readahead,
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003006 * so we want to possibly extend the readahead further. We return the file that
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003007 * was pinned if we have to drop the mmap_lock in order to do IO.
Linus Torvaldsef00e082009-06-16 15:31:25 -07003008 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003009static struct file *do_async_mmap_readahead(struct vm_fault *vmf,
3010 struct page *page)
Linus Torvaldsef00e082009-06-16 15:31:25 -07003011{
Josef Bacik2a1180f2019-03-13 11:44:18 -07003012 struct file *file = vmf->vma->vm_file;
3013 struct file_ra_state *ra = &file->f_ra;
Linus Torvaldsef00e082009-06-16 15:31:25 -07003014 struct address_space *mapping = file->f_mapping;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003015 struct file *fpin = NULL;
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07003016 unsigned int mmap_miss;
Josef Bacik2a1180f2019-03-13 11:44:18 -07003017 pgoff_t offset = vmf->pgoff;
Linus Torvaldsef00e082009-06-16 15:31:25 -07003018
3019 /* If we don't want any read-ahead, don't bother */
Jan Kara5c72fee2020-04-01 21:04:40 -07003020 if (vmf->vma->vm_flags & VM_RAND_READ || !ra->ra_pages)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003021 return fpin;
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07003022 mmap_miss = READ_ONCE(ra->mmap_miss);
3023 if (mmap_miss)
3024 WRITE_ONCE(ra->mmap_miss, --mmap_miss);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003025 if (PageReadahead(page)) {
3026 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Wu Fengguang2fad6f52009-06-16 15:31:29 -07003027 page_cache_async_readahead(mapping, ra, file,
3028 page, offset, ra->ra_pages);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003029 }
3030 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07003031}
3032
Randy Dunlap485bb992006-06-23 02:03:49 -07003033/**
Nick Piggin54cb8822007-07-19 01:46:59 -07003034 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07003035 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07003036 *
Nick Piggin54cb8822007-07-19 01:46:59 -07003037 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 * mapped memory region to read in file data during a page fault.
3039 *
3040 * The goto's are kind of ugly, but this streamlines the normal case of having
3041 * it in the page cache, and handles the special cases reasonably without
3042 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003043 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003044 * vma->vm_mm->mmap_lock must be held on entry.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003045 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003046 * If our return value has VM_FAULT_RETRY set, it's because the mmap_lock
Yang Shia4985832019-07-11 20:55:29 -07003047 * may be dropped before doing I/O or by lock_page_maybe_drop_mmap().
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003048 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003049 * If our return value does not have VM_FAULT_RETRY set, the mmap_lock
Paul Cassella9a95f3c2014-08-06 16:07:24 -07003050 * has not been released.
3051 *
3052 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Mike Rapoporta862f682019-03-05 15:48:42 -08003053 *
3054 * Return: bitwise-OR of %VM_FAULT_ codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 */
Souptick Joarder2bcd6452018-06-07 17:08:00 -07003056vm_fault_t filemap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
3058 int error;
Dave Jiang11bac802017-02-24 14:56:41 -08003059 struct file *file = vmf->vma->vm_file;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003060 struct file *fpin = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 struct inode *inode = mapping->host;
Linus Torvaldsef00e082009-06-16 15:31:25 -07003063 pgoff_t offset = vmf->pgoff;
Matthew Wilcox9ab25942017-05-03 14:53:29 -07003064 pgoff_t max_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 struct page *page;
Souptick Joarder2bcd6452018-06-07 17:08:00 -07003066 vm_fault_t ret = 0;
Jan Kara730633f2021-01-28 19:19:45 +01003067 bool mapping_locked = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Matthew Wilcox9ab25942017-05-03 14:53:29 -07003069 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3070 if (unlikely(offset >= max_off))
Linus Torvalds5307cc12007-10-31 09:19:46 -07003071 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 /*
Johannes Weiner49426422013-10-16 13:46:59 -07003074 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 */
Linus Torvaldsef00e082009-06-16 15:31:25 -07003076 page = find_get_page(mapping, offset);
Jan Kara730633f2021-01-28 19:19:45 +01003077 if (likely(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 /*
Jan Kara730633f2021-01-28 19:19:45 +01003079 * We found the page, so try async readahead before waiting for
3080 * the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 */
Jan Kara730633f2021-01-28 19:19:45 +01003082 if (!(vmf->flags & FAULT_FLAG_TRIED))
3083 fpin = do_async_mmap_readahead(vmf, page);
3084 if (unlikely(!PageUptodate(page))) {
3085 filemap_invalidate_lock_shared(mapping);
3086 mapping_locked = true;
3087 }
3088 } else {
Linus Torvaldsef00e082009-06-16 15:31:25 -07003089 /* No page in the page cache at all */
Linus Torvaldsef00e082009-06-16 15:31:25 -07003090 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07003091 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07003092 ret = VM_FAULT_MAJOR;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003093 fpin = do_sync_mmap_readahead(vmf);
Linus Torvaldsef00e082009-06-16 15:31:25 -07003094retry_find:
Jan Kara730633f2021-01-28 19:19:45 +01003095 /*
3096 * See comment in filemap_create_page() why we need
3097 * invalidate_lock
3098 */
3099 if (!mapping_locked) {
3100 filemap_invalidate_lock_shared(mapping);
3101 mapping_locked = true;
3102 }
Josef Bacika75d4c32019-03-13 11:44:14 -07003103 page = pagecache_get_page(mapping, offset,
3104 FGP_CREAT|FGP_FOR_MMAP,
3105 vmf->gfp_mask);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003106 if (!page) {
3107 if (fpin)
3108 goto out_retry;
Jan Kara730633f2021-01-28 19:19:45 +01003109 filemap_invalidate_unlock_shared(mapping);
Matthew Wilcox (Oracle)e520e932020-04-01 21:04:53 -07003110 return VM_FAULT_OOM;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 }
3113
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003114 if (!lock_page_maybe_drop_mmap(vmf, page, &fpin))
3115 goto out_retry;
Michel Lespinasseb522c942010-10-26 14:21:56 -07003116
3117 /* Did it get truncated? */
Song Liu585e5a72019-09-23 15:37:44 -07003118 if (unlikely(compound_head(page)->mapping != mapping)) {
Michel Lespinasseb522c942010-10-26 14:21:56 -07003119 unlock_page(page);
3120 put_page(page);
3121 goto retry_find;
3122 }
Song Liu520e5ba2019-09-23 15:37:50 -07003123 VM_BUG_ON_PAGE(page_to_pgoff(page) != offset, page);
Michel Lespinasseb522c942010-10-26 14:21:56 -07003124
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 /*
Nick Piggind00806b2007-07-19 01:46:57 -07003126 * We have a locked page in the page cache, now we need to check
3127 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 */
Jan Kara730633f2021-01-28 19:19:45 +01003129 if (unlikely(!PageUptodate(page))) {
3130 /*
3131 * The page was in cache and uptodate and now it is not.
3132 * Strange but possible since we didn't hold the page lock all
3133 * the time. Let's drop everything get the invalidate lock and
3134 * try again.
3135 */
3136 if (!mapping_locked) {
3137 unlock_page(page);
3138 put_page(page);
3139 goto retry_find;
3140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 goto page_not_uptodate;
Jan Kara730633f2021-01-28 19:19:45 +01003142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Linus Torvaldsef00e082009-06-16 15:31:25 -07003144 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003145 * We've made it this far and we had to drop our mmap_lock, now is the
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003146 * time to return to the upper layer and have it re-find the vma and
3147 * redo the fault.
3148 */
3149 if (fpin) {
3150 unlock_page(page);
3151 goto out_retry;
3152 }
Jan Kara730633f2021-01-28 19:19:45 +01003153 if (mapping_locked)
3154 filemap_invalidate_unlock_shared(mapping);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003155
3156 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07003157 * Found the page and have a reference on it.
3158 * We must recheck i_size under page lock.
3159 */
Matthew Wilcox9ab25942017-05-03 14:53:29 -07003160 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3161 if (unlikely(offset >= max_off)) {
Nick Piggind00806b2007-07-19 01:46:57 -07003162 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003163 put_page(page);
Linus Torvalds5307cc12007-10-31 09:19:46 -07003164 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07003165 }
3166
Nick Piggind0217ac2007-07-19 01:47:03 -07003167 vmf->page = page;
Nick Piggin83c54072007-07-19 01:47:05 -07003168 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 /*
3172 * Umm, take care of errors if the page isn't up-to-date.
3173 * Try to re-read it _once_. We do this synchronously,
3174 * because there really aren't any performance issues here
3175 * and we need to check for errors.
3176 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003177 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Matthew Wilcox (Oracle)d31fa862021-04-29 22:55:26 -07003178 error = filemap_read_page(file, mapping, page);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003179 if (fpin)
3180 goto out_retry;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003181 put_page(page);
Nick Piggind00806b2007-07-19 01:46:57 -07003182
3183 if (!error || error == AOP_TRUNCATED_PAGE)
3184 goto retry_find;
Jan Kara730633f2021-01-28 19:19:45 +01003185 filemap_invalidate_unlock_shared(mapping);
Nick Piggind00806b2007-07-19 01:46:57 -07003186
Nick Piggind0217ac2007-07-19 01:47:03 -07003187 return VM_FAULT_SIGBUS;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003188
3189out_retry:
3190 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003191 * We dropped the mmap_lock, we need to return to the fault handler to
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003192 * re-find the vma and come back and find our hopefully still populated
3193 * page.
3194 */
3195 if (page)
3196 put_page(page);
Jan Kara730633f2021-01-28 19:19:45 +01003197 if (mapping_locked)
3198 filemap_invalidate_unlock_shared(mapping);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003199 if (fpin)
3200 fput(fpin);
3201 return ret | VM_FAULT_RETRY;
Nick Piggin54cb8822007-07-19 01:46:59 -07003202}
3203EXPORT_SYMBOL(filemap_fault);
3204
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003205static bool filemap_map_pmd(struct vm_fault *vmf, struct page *page)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003206{
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003207 struct mm_struct *mm = vmf->vma->vm_mm;
3208
3209 /* Huge page is mapped? No need to proceed. */
3210 if (pmd_trans_huge(*vmf->pmd)) {
3211 unlock_page(page);
3212 put_page(page);
3213 return true;
3214 }
3215
3216 if (pmd_none(*vmf->pmd) && PageTransHuge(page)) {
Yang Shie0f43fa2021-11-05 13:41:04 -07003217 vm_fault_t ret = do_set_pmd(vmf, page);
3218 if (!ret) {
3219 /* The page is mapped successfully, reference consumed. */
3220 unlock_page(page);
3221 return true;
3222 }
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003223 }
3224
Qi Zheng03c4f202021-11-05 13:38:38 -07003225 if (pmd_none(*vmf->pmd))
3226 pmd_install(mm, vmf->pmd, &vmf->prealloc_pte);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003227
3228 /* See comment in handle_pte_fault() */
3229 if (pmd_devmap_trans_unstable(vmf->pmd)) {
3230 unlock_page(page);
3231 put_page(page);
3232 return true;
3233 }
3234
3235 return false;
3236}
3237
3238static struct page *next_uptodate_page(struct page *page,
3239 struct address_space *mapping,
3240 struct xa_state *xas, pgoff_t end_pgoff)
3241{
3242 unsigned long max_idx;
3243
3244 do {
3245 if (!page)
3246 return NULL;
3247 if (xas_retry(xas, page))
3248 continue;
3249 if (xa_is_value(page))
3250 continue;
3251 if (PageLocked(page))
3252 continue;
3253 if (!page_cache_get_speculative(page))
3254 continue;
3255 /* Has the page moved or been split? */
3256 if (unlikely(page != xas_reload(xas)))
3257 goto skip;
3258 if (!PageUptodate(page) || PageReadahead(page))
3259 goto skip;
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003260 if (!trylock_page(page))
3261 goto skip;
3262 if (page->mapping != mapping)
3263 goto unlock;
3264 if (!PageUptodate(page))
3265 goto unlock;
3266 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
3267 if (xas->xa_index >= max_idx)
3268 goto unlock;
3269 return page;
3270unlock:
3271 unlock_page(page);
3272skip:
3273 put_page(page);
3274 } while ((page = xas_next_entry(xas, end_pgoff)) != NULL);
3275
3276 return NULL;
3277}
3278
3279static inline struct page *first_map_page(struct address_space *mapping,
3280 struct xa_state *xas,
3281 pgoff_t end_pgoff)
3282{
3283 return next_uptodate_page(xas_find(xas, end_pgoff),
3284 mapping, xas, end_pgoff);
3285}
3286
3287static inline struct page *next_map_page(struct address_space *mapping,
3288 struct xa_state *xas,
3289 pgoff_t end_pgoff)
3290{
3291 return next_uptodate_page(xas_next_entry(xas, end_pgoff),
3292 mapping, xas, end_pgoff);
3293}
3294
3295vm_fault_t filemap_map_pages(struct vm_fault *vmf,
3296 pgoff_t start_pgoff, pgoff_t end_pgoff)
3297{
3298 struct vm_area_struct *vma = vmf->vma;
3299 struct file *file = vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003300 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003301 pgoff_t last_pgoff = start_pgoff;
Will Deacon9d3af4b2021-01-14 15:24:19 +00003302 unsigned long addr;
Matthew Wilcox070e8072018-05-17 00:08:30 -04003303 XA_STATE(xas, &mapping->i_pages, start_pgoff);
Matthew Wilcox (Oracle)27a83a62020-10-13 16:51:44 -07003304 struct page *head, *page;
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07003305 unsigned int mmap_miss = READ_ONCE(file->f_ra.mmap_miss);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003306 vm_fault_t ret = 0;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003307
3308 rcu_read_lock();
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003309 head = first_map_page(mapping, &xas, end_pgoff);
3310 if (!head)
3311 goto out;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003312
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003313 if (filemap_map_pmd(vmf, head)) {
3314 ret = VM_FAULT_NOPAGE;
3315 goto out;
3316 }
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003317
Will Deacon9d3af4b2021-01-14 15:24:19 +00003318 addr = vma->vm_start + ((start_pgoff - vma->vm_pgoff) << PAGE_SHIFT);
3319 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, addr, &vmf->ptl);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003320 do {
Matthew Wilcox (Oracle)27a83a62020-10-13 16:51:44 -07003321 page = find_subpage(head, xas.xa_index);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003322 if (PageHWPoison(page))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003323 goto unlock;
3324
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07003325 if (mmap_miss > 0)
3326 mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003327
Will Deacon9d3af4b2021-01-14 15:24:19 +00003328 addr += (xas.xa_index - last_pgoff) << PAGE_SHIFT;
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003329 vmf->pte += xas.xa_index - last_pgoff;
Matthew Wilcox070e8072018-05-17 00:08:30 -04003330 last_pgoff = xas.xa_index;
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003331
3332 if (!pte_none(*vmf->pte))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003333 goto unlock;
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003334
Will Deacon46bdb422020-11-24 18:48:26 +00003335 /* We're about to handle the fault */
Will Deacon9d3af4b2021-01-14 15:24:19 +00003336 if (vmf->address == addr)
Will Deacon46bdb422020-11-24 18:48:26 +00003337 ret = VM_FAULT_NOPAGE;
Will Deacon46bdb422020-11-24 18:48:26 +00003338
Will Deacon9d3af4b2021-01-14 15:24:19 +00003339 do_set_pte(vmf, page, addr);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003340 /* no need to invalidate: a not-present page won't be cached */
Will Deacon9d3af4b2021-01-14 15:24:19 +00003341 update_mmu_cache(vma, addr, vmf->pte);
Matthew Wilcox (Oracle)27a83a62020-10-13 16:51:44 -07003342 unlock_page(head);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003343 continue;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003344unlock:
Matthew Wilcox (Oracle)27a83a62020-10-13 16:51:44 -07003345 unlock_page(head);
Matthew Wilcox (Oracle)27a83a62020-10-13 16:51:44 -07003346 put_page(head);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003347 } while ((head = next_map_page(mapping, &xas, end_pgoff)) != NULL);
3348 pte_unmap_unlock(vmf->pte, vmf->ptl);
3349out:
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003350 rcu_read_unlock();
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07003351 WRITE_ONCE(file->f_ra.mmap_miss, mmap_miss);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003352 return ret;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003353}
3354EXPORT_SYMBOL(filemap_map_pages);
3355
Souptick Joarder2bcd6452018-06-07 17:08:00 -07003356vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Jan Kara4fcf1c62012-06-12 16:20:29 +02003357{
Christoph Hellwig5df1a672020-11-16 14:33:37 +01003358 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
Jan Kara4fcf1c62012-06-12 16:20:29 +02003359 struct page *page = vmf->page;
Souptick Joarder2bcd6452018-06-07 17:08:00 -07003360 vm_fault_t ret = VM_FAULT_LOCKED;
Jan Kara4fcf1c62012-06-12 16:20:29 +02003361
Christoph Hellwig5df1a672020-11-16 14:33:37 +01003362 sb_start_pagefault(mapping->host->i_sb);
Dave Jiang11bac802017-02-24 14:56:41 -08003363 file_update_time(vmf->vma->vm_file);
Jan Kara4fcf1c62012-06-12 16:20:29 +02003364 lock_page(page);
Christoph Hellwig5df1a672020-11-16 14:33:37 +01003365 if (page->mapping != mapping) {
Jan Kara4fcf1c62012-06-12 16:20:29 +02003366 unlock_page(page);
3367 ret = VM_FAULT_NOPAGE;
3368 goto out;
3369 }
Jan Kara14da9202012-06-12 16:20:37 +02003370 /*
3371 * We mark the page dirty already here so that when freeze is in
3372 * progress, we are guaranteed that writeback during freezing will
3373 * see the dirty page and writeprotect it again.
3374 */
3375 set_page_dirty(page);
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08003376 wait_for_stable_page(page);
Jan Kara4fcf1c62012-06-12 16:20:29 +02003377out:
Christoph Hellwig5df1a672020-11-16 14:33:37 +01003378 sb_end_pagefault(mapping->host->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02003379 return ret;
3380}
Jan Kara4fcf1c62012-06-12 16:20:29 +02003381
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003382const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003383 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003384 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02003385 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386};
3387
3388/* This is used for a general mmap of a disk file */
3389
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 struct address_space *mapping = file->f_mapping;
3393
3394 if (!mapping->a_ops->readpage)
3395 return -ENOEXEC;
3396 file_accessed(file);
3397 vma->vm_ops = &generic_file_vm_ops;
3398 return 0;
3399}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
3401/*
3402 * This is for filesystems which do not implement ->writepage.
3403 */
3404int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
3405{
3406 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
3407 return -EINVAL;
3408 return generic_file_mmap(file, vma);
3409}
3410#else
Souptick Joarder4b96a372018-10-26 15:04:03 -07003411vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Arnd Bergmann453972282018-04-13 15:35:27 -07003412{
Souptick Joarder4b96a372018-10-26 15:04:03 -07003413 return VM_FAULT_SIGBUS;
Arnd Bergmann453972282018-04-13 15:35:27 -07003414}
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -07003415int generic_file_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416{
3417 return -ENOSYS;
3418}
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -07003419int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420{
3421 return -ENOSYS;
3422}
3423#endif /* CONFIG_MMU */
3424
Arnd Bergmann453972282018-04-13 15:35:27 -07003425EXPORT_SYMBOL(filemap_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426EXPORT_SYMBOL(generic_file_mmap);
3427EXPORT_SYMBOL(generic_file_readonly_mmap);
3428
Sasha Levin67f9fd92014-04-03 14:48:18 -07003429static struct page *wait_on_page_read(struct page *page)
3430{
3431 if (!IS_ERR(page)) {
3432 wait_on_page_locked(page);
3433 if (!PageUptodate(page)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003434 put_page(page);
Sasha Levin67f9fd92014-04-03 14:48:18 -07003435 page = ERR_PTR(-EIO);
3436 }
3437 }
3438 return page;
3439}
3440
Mel Gorman32b63522016-03-15 14:55:36 -07003441static struct page *do_read_cache_page(struct address_space *mapping,
Fengguang Wu57f6b962007-10-16 01:24:37 -07003442 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07003443 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003444 void *data,
3445 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
Nick Piggineb2be182007-10-16 01:24:57 -07003447 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 int err;
3449repeat:
3450 page = find_get_page(mapping, index);
3451 if (!page) {
Mel Gorman453f85d2017-11-15 17:38:03 -08003452 page = __page_cache_alloc(gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07003453 if (!page)
3454 return ERR_PTR(-ENOMEM);
Dave Kleikampe6f67b82011-12-21 11:05:48 -06003455 err = add_to_page_cache_lru(page, mapping, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07003456 if (unlikely(err)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003457 put_page(page);
Nick Piggineb2be182007-10-16 01:24:57 -07003458 if (err == -EEXIST)
3459 goto repeat;
Matthew Wilcox22ecdb42017-12-04 04:02:00 -05003460 /* Presumably ENOMEM for xarray node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 return ERR_PTR(err);
3462 }
Mel Gorman32b63522016-03-15 14:55:36 -07003463
3464filler:
Christoph Hellwig6c45b452019-07-11 20:55:20 -07003465 if (filler)
3466 err = filler(data, page);
3467 else
3468 err = mapping->a_ops->readpage(data, page);
3469
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 if (err < 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003471 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07003472 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 }
Mel Gorman32b63522016-03-15 14:55:36 -07003474
3475 page = wait_on_page_read(page);
3476 if (IS_ERR(page))
3477 return page;
3478 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 if (PageUptodate(page))
3481 goto out;
3482
Mel Gormanebded022016-03-15 14:55:39 -07003483 /*
Miaohe Lin0e9aa672020-10-15 20:09:58 -07003484 * Page is not up to date and may be locked due to one of the following
Mel Gormanebded022016-03-15 14:55:39 -07003485 * case a: Page is being filled and the page lock is held
3486 * case b: Read/write error clearing the page uptodate status
3487 * case c: Truncation in progress (page locked)
3488 * case d: Reclaim in progress
3489 *
3490 * Case a, the page will be up to date when the page is unlocked.
3491 * There is no need to serialise on the page lock here as the page
3492 * is pinned so the lock gives no additional protection. Even if the
Randy Dunlapce89fddf2020-08-11 18:32:53 -07003493 * page is truncated, the data is still valid if PageUptodate as
Mel Gormanebded022016-03-15 14:55:39 -07003494 * it's a race vs truncate race.
3495 * Case b, the page will not be up to date
3496 * Case c, the page may be truncated but in itself, the data may still
3497 * be valid after IO completes as it's a read vs truncate race. The
3498 * operation must restart if the page is not uptodate on unlock but
3499 * otherwise serialising on page lock to stabilise the mapping gives
3500 * no additional guarantees to the caller as the page lock is
3501 * released before return.
3502 * Case d, similar to truncation. If reclaim holds the page lock, it
3503 * will be a race with remove_mapping that determines if the mapping
3504 * is valid on unlock but otherwise the data is valid and there is
3505 * no need to serialise with page lock.
3506 *
3507 * As the page lock gives no additional guarantee, we optimistically
3508 * wait on the page to be unlocked and check if it's up to date and
3509 * use the page if it is. Otherwise, the page lock is required to
3510 * distinguish between the different cases. The motivation is that we
3511 * avoid spurious serialisations and wakeups when multiple processes
3512 * wait on the same page for IO to complete.
3513 */
3514 wait_on_page_locked(page);
3515 if (PageUptodate(page))
3516 goto out;
3517
3518 /* Distinguish between all the cases under the safety of the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 lock_page(page);
Mel Gormanebded022016-03-15 14:55:39 -07003520
3521 /* Case c or d, restart the operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 if (!page->mapping) {
3523 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003524 put_page(page);
Mel Gorman32b63522016-03-15 14:55:36 -07003525 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 }
Mel Gormanebded022016-03-15 14:55:39 -07003527
3528 /* Someone else locked and filled the page in a very small window */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 if (PageUptodate(page)) {
3530 unlock_page(page);
3531 goto out;
3532 }
Xianting Tianfaffdfa2020-04-01 21:04:47 -07003533
3534 /*
3535 * A previous I/O error may have been due to temporary
3536 * failures.
3537 * Clear page error before actual read, PG_error will be
3538 * set again if read page fails.
3539 */
3540 ClearPageError(page);
Mel Gorman32b63522016-03-15 14:55:36 -07003541 goto filler;
3542
David Howellsc855ff32007-05-09 13:42:20 +01003543out:
Nick Piggin6fe69002007-05-06 14:49:04 -07003544 mark_page_accessed(page);
3545 return page;
3546}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003547
3548/**
Sasha Levin67f9fd92014-04-03 14:48:18 -07003549 * read_cache_page - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003550 * @mapping: the page's address_space
3551 * @index: the page index
3552 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07003553 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003554 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003555 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07003556 * not set, try to fill the page and wait for it to become unlocked.
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 */
Sasha Levin67f9fd92014-04-03 14:48:18 -07003564struct page *read_cache_page(struct address_space *mapping,
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003565 pgoff_t index,
Hugh Dickins5e5358e2011-07-25 17:12:23 -07003566 int (*filler)(void *, struct page *),
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003567 void *data)
3568{
Christoph Hellwigd322a8e2019-07-11 20:55:17 -07003569 return do_read_cache_page(mapping, index, filler, data,
3570 mapping_gfp_mask(mapping));
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003571}
Sasha Levin67f9fd92014-04-03 14:48:18 -07003572EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003573
3574/**
3575 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
3576 * @mapping: the page's address_space
3577 * @index: the page index
3578 * @gfp: the page allocator flags to use if allocating
3579 *
3580 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06003581 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003582 *
3583 * If the page does not get brought uptodate, return -EIO.
Mike Rapoporta862f682019-03-05 15:48:42 -08003584 *
Jan Kara730633f2021-01-28 19:19:45 +01003585 * The function expects mapping->invalidate_lock to be already held.
3586 *
Mike Rapoporta862f682019-03-05 15:48:42 -08003587 * Return: up to date page on success, ERR_PTR() on failure.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003588 */
3589struct page *read_cache_page_gfp(struct address_space *mapping,
3590 pgoff_t index,
3591 gfp_t gfp)
3592{
Christoph Hellwig6c45b452019-07-11 20:55:20 -07003593 return do_read_cache_page(mapping, index, NULL, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003594}
3595EXPORT_SYMBOL(read_cache_page_gfp);
3596
Nick Pigginafddba42007-10-16 01:25:01 -07003597int pagecache_write_begin(struct file *file, struct address_space *mapping,
3598 loff_t pos, unsigned len, unsigned flags,
3599 struct page **pagep, void **fsdata)
3600{
3601 const struct address_space_operations *aops = mapping->a_ops;
3602
Nick Piggin4e02ed42008-10-29 14:00:55 -07003603 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003604 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003605}
3606EXPORT_SYMBOL(pagecache_write_begin);
3607
3608int pagecache_write_end(struct file *file, struct address_space *mapping,
3609 loff_t pos, unsigned len, unsigned copied,
3610 struct page *page, void *fsdata)
3611{
3612 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07003613
Nick Piggin4e02ed42008-10-29 14:00:55 -07003614 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003615}
3616EXPORT_SYMBOL(pagecache_write_end);
3617
Konstantin Khlebnikova92853b2019-11-30 17:49:44 -08003618/*
3619 * Warn about a page cache invalidation failure during a direct I/O write.
3620 */
3621void dio_warn_stale_pagecache(struct file *filp)
3622{
3623 static DEFINE_RATELIMIT_STATE(_rs, 86400 * HZ, DEFAULT_RATELIMIT_BURST);
3624 char pathname[128];
Konstantin Khlebnikova92853b2019-11-30 17:49:44 -08003625 char *path;
3626
Christoph Hellwig5df1a672020-11-16 14:33:37 +01003627 errseq_set(&filp->f_mapping->wb_err, -EIO);
Konstantin Khlebnikova92853b2019-11-30 17:49:44 -08003628 if (__ratelimit(&_rs)) {
3629 path = file_path(filp, pathname, sizeof(pathname));
3630 if (IS_ERR(path))
3631 path = "(unknown)";
3632 pr_crit("Page cache invalidation failure on direct I/O. Possible data corruption due to collision with buffered I/O!\n");
3633 pr_crit("File: %s PID: %d Comm: %.20s\n", path, current->pid,
3634 current->comm);
3635 }
3636}
3637
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003639generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640{
3641 struct file *file = iocb->ki_filp;
3642 struct address_space *mapping = file->f_mapping;
3643 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003644 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003646 size_t write_len;
3647 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
Al Viro0c949332014-03-22 06:51:37 -04003649 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003650 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003651
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003652 if (iocb->ki_flags & IOCB_NOWAIT) {
3653 /* If there are pages to writeback, return */
Christoph Hellwig5df1a672020-11-16 14:33:37 +01003654 if (filemap_range_has_page(file->f_mapping, pos,
zhengbin35f12f02019-03-05 15:44:21 -08003655 pos + write_len - 1))
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003656 return -EAGAIN;
3657 } else {
3658 written = filemap_write_and_wait_range(mapping, pos,
3659 pos + write_len - 1);
3660 if (written)
3661 goto out;
3662 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07003663
3664 /*
3665 * After a write we want buffered reads to be sure to go to disk to get
3666 * the new data. We invalidate clean cached page from the region we're
3667 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07003668 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07003669 */
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003670 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003671 pos >> PAGE_SHIFT, end);
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003672 /*
3673 * If a page can not be invalidated, return 0 to fall back
3674 * to buffered write.
3675 */
3676 if (written) {
3677 if (written == -EBUSY)
3678 return 0;
3679 goto out;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003680 }
3681
Al Viro639a93a52017-04-13 14:10:15 -04003682 written = mapping->a_ops->direct_IO(iocb, from);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003683
3684 /*
3685 * Finally, try again to invalidate clean pages which might have been
3686 * cached by non-direct readahead, or faulted in by get_user_pages()
3687 * if the source of the write was an mmap'ed region of the file
3688 * we're writing. Either one is a pretty crazy thing to do,
3689 * so we don't support it 100%. If this invalidation
3690 * fails, tough, the write still worked...
Lukas Czerner332391a2017-09-21 08:16:29 -06003691 *
3692 * Most of the time we do not need this since dio_complete() will do
3693 * the invalidation for us. However there are some file systems that
3694 * do not end up with dio_complete() being called, so let's not break
Konstantin Khlebnikov80c1fe92019-11-30 17:49:41 -08003695 * them by removing it completely.
3696 *
Konstantin Khlebnikov9266a142019-11-30 17:49:47 -08003697 * Noticeable example is a blkdev_direct_IO().
3698 *
Konstantin Khlebnikov80c1fe92019-11-30 17:49:41 -08003699 * Skip invalidation for async writes or if mapping has no pages.
Christoph Hellwiga969e902008-07-23 21:27:04 -07003700 */
Konstantin Khlebnikov9266a142019-11-30 17:49:47 -08003701 if (written > 0 && mapping->nrpages &&
3702 invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT, end))
3703 dio_warn_stale_pagecache(file);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07003706 pos += written;
Al Viro639a93a52017-04-13 14:10:15 -04003707 write_len -= written;
Namhyung Kim01166512010-10-26 14:21:58 -07003708 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
3709 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 mark_inode_dirty(inode);
3711 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05003712 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 }
Pavel Begunkovab2125d2021-02-24 12:01:45 -08003714 if (written != -EIOCBQUEUED)
3715 iov_iter_revert(from, write_len - iov_iter_count(from));
Christoph Hellwiga969e902008-07-23 21:27:04 -07003716out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 return written;
3718}
3719EXPORT_SYMBOL(generic_file_direct_write);
3720
Al Viro3b93f912014-02-11 21:34:08 -05003721ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07003722 struct iov_iter *i, loff_t pos)
3723{
3724 struct address_space *mapping = file->f_mapping;
3725 const struct address_space_operations *a_ops = mapping->a_ops;
3726 long status = 0;
3727 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07003728 unsigned int flags = 0;
3729
Nick Pigginafddba42007-10-16 01:25:01 -07003730 do {
3731 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003732 unsigned long offset; /* Offset into pagecache page */
3733 unsigned long bytes; /* Bytes to write to page */
3734 size_t copied; /* Bytes copied from user */
3735 void *fsdata;
3736
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003737 offset = (pos & (PAGE_SIZE - 1));
3738 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003739 iov_iter_count(i));
3740
3741again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01003742 /*
3743 * Bring in the user page that we will copy from _first_.
3744 * Otherwise there's a nasty deadlock on copying from the
3745 * same page as we're writing to, without it being marked
3746 * up-to-date.
Linus Torvalds00a3d662015-10-07 08:32:38 +01003747 */
Andreas Gruenbachera6294592021-08-02 14:54:16 +02003748 if (unlikely(fault_in_iov_iter_readable(i, bytes))) {
Linus Torvalds00a3d662015-10-07 08:32:38 +01003749 status = -EFAULT;
3750 break;
3751 }
3752
Jan Kara296291c2015-10-22 13:32:21 -07003753 if (fatal_signal_pending(current)) {
3754 status = -EINTR;
3755 break;
3756 }
3757
Nick Piggin674b8922007-10-16 01:25:03 -07003758 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003759 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07003760 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07003761 break;
3762
anfei zhou931e80e2010-02-02 13:44:02 -08003763 if (mapping_writably_mapped(mapping))
3764 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01003765
Al Virof0b65f32021-04-30 10:26:41 -04003766 copied = copy_page_from_iter_atomic(page, offset, bytes, i);
Nick Pigginafddba42007-10-16 01:25:01 -07003767 flush_dcache_page(page);
3768
3769 status = a_ops->write_end(file, mapping, pos, bytes, copied,
3770 page, fsdata);
Al Virof0b65f32021-04-30 10:26:41 -04003771 if (unlikely(status != copied)) {
3772 iov_iter_revert(i, copied - max(status, 0L));
3773 if (unlikely(status < 0))
3774 break;
3775 }
Nick Pigginafddba42007-10-16 01:25:01 -07003776 cond_resched();
3777
Al Virobc1bb412021-05-31 00:32:44 -04003778 if (unlikely(status == 0)) {
Nick Pigginafddba42007-10-16 01:25:01 -07003779 /*
Al Virobc1bb412021-05-31 00:32:44 -04003780 * A short copy made ->write_end() reject the
3781 * thing entirely. Might be memory poisoning
3782 * halfway through, might be a race with munmap,
3783 * might be severe memory pressure.
Nick Pigginafddba42007-10-16 01:25:01 -07003784 */
Al Virobc1bb412021-05-31 00:32:44 -04003785 if (copied)
3786 bytes = copied;
Nick Pigginafddba42007-10-16 01:25:01 -07003787 goto again;
3788 }
Al Virof0b65f32021-04-30 10:26:41 -04003789 pos += status;
3790 written += status;
Nick Pigginafddba42007-10-16 01:25:01 -07003791
3792 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07003793 } while (iov_iter_count(i));
3794
3795 return written ? written : status;
3796}
Al Viro3b93f912014-02-11 21:34:08 -05003797EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
Jan Karae4dd9de2009-08-17 18:10:06 +02003799/**
Al Viro81742022014-04-03 03:17:43 -04003800 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003801 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04003802 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003803 *
3804 * This function does all the work needed for actually writing data to a
3805 * file. It does all basic checks, removes SUID from the file, updates
3806 * modification times and calls proper subroutines depending on whether we
3807 * do direct IO or a standard buffered write.
3808 *
Jan Kara96087032021-04-12 15:50:21 +02003809 * It expects i_rwsem to be grabbed unless we work on a block device or similar
Jan Karae4dd9de2009-08-17 18:10:06 +02003810 * object which does not need locking at all.
3811 *
3812 * This function does *not* take care of syncing data in case of O_SYNC write.
3813 * A caller has to handle it. This is mainly due to the fact that we want to
Jan Kara96087032021-04-12 15:50:21 +02003814 * avoid syncing under i_rwsem.
Mike Rapoporta862f682019-03-05 15:48:42 -08003815 *
3816 * Return:
3817 * * number of bytes written, even for truncated writes
3818 * * negative error code if no data has been written at all
Jan Karae4dd9de2009-08-17 18:10:06 +02003819 */
Al Viro81742022014-04-03 03:17:43 -04003820ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821{
3822 struct file *file = iocb->ki_filp;
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -07003823 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003825 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003827 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003830 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003831 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 if (err)
3833 goto out;
3834
Josef Bacikc3b2da32012-03-26 09:59:21 -04003835 err = file_update_time(file);
3836 if (err)
3837 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
Al Viro2ba48ce2015-04-09 13:52:01 -04003839 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003840 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003841
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003842 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003843 /*
3844 * If the write stopped short of completing, fall back to
3845 * buffered writes. Some filesystems do this for writes to
3846 * holes, for example. For DAX files, a buffered write will
3847 * not succeed (even if it did, DAX does not handle dirty
3848 * page-cache pages correctly).
3849 */
Al Viro0b8def92015-04-07 10:22:53 -04003850 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003852
Al Viro0b8def92015-04-07 10:22:53 -04003853 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003854 /*
Al Viro3b93f912014-02-11 21:34:08 -05003855 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003856 * then we want to return the number of bytes which were
3857 * direct-written, or the error code if that was zero. Note
3858 * that this differs from normal direct-io semantics, which
3859 * will return -EFOO even if some bytes were written.
3860 */
Al Viro60bb4522014-08-08 12:39:16 -04003861 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003862 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003863 goto out;
3864 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003865 /*
3866 * We need to ensure that the page cache pages are written to
3867 * disk and invalidated to preserve the expected O_DIRECT
3868 * semantics.
3869 */
Al Viro3b93f912014-02-11 21:34:08 -05003870 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003871 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003872 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003873 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003874 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003875 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003876 pos >> PAGE_SHIFT,
3877 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003878 } else {
3879 /*
3880 * We don't know how much we wrote, so just return
3881 * the number of bytes which were direct-written
3882 */
3883 }
3884 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003885 written = generic_perform_write(file, from, iocb->ki_pos);
3886 if (likely(written > 0))
3887 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889out:
3890 current->backing_dev_info = NULL;
3891 return written ? written : err;
3892}
Al Viro81742022014-04-03 03:17:43 -04003893EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894
Jan Karae4dd9de2009-08-17 18:10:06 +02003895/**
Al Viro81742022014-04-03 03:17:43 -04003896 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003897 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003898 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003899 *
Al Viro81742022014-04-03 03:17:43 -04003900 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003901 * filesystems. It takes care of syncing the file in case of O_SYNC file
Jan Kara96087032021-04-12 15:50:21 +02003902 * and acquires i_rwsem as needed.
Mike Rapoporta862f682019-03-05 15:48:42 -08003903 * Return:
3904 * * negative error code if no data has been written at all of
3905 * vfs_fsync_range() failed for a synchronous write
3906 * * number of bytes written, even for truncated writes
Jan Karae4dd9de2009-08-17 18:10:06 +02003907 */
Al Viro81742022014-04-03 03:17:43 -04003908ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909{
3910 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003911 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
Al Viro59551022016-01-22 15:40:57 -05003914 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003915 ret = generic_write_checks(iocb, from);
3916 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003917 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003918 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
Christoph Hellwige2592212016-04-07 08:52:01 -07003920 if (ret > 0)
3921 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 return ret;
3923}
Al Viro81742022014-04-03 03:17:43 -04003924EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
David Howellscf9a2ae2006-08-29 19:05:54 +01003926/**
3927 * try_to_release_page() - release old fs-specific metadata on a page
3928 *
3929 * @page: the page which the kernel is trying to free
3930 * @gfp_mask: memory allocation flags (and I/O mode)
3931 *
3932 * The address_space is to try to release any data against the page
Mike Rapoporta862f682019-03-05 15:48:42 -08003933 * (presumably at page->private).
David Howellscf9a2ae2006-08-29 19:05:54 +01003934 *
David Howells266cf652009-04-03 16:42:36 +01003935 * This may also be called if PG_fscache is set on a page, indicating that the
3936 * page is known to the local caching routines.
3937 *
David Howellscf9a2ae2006-08-29 19:05:54 +01003938 * The @gfp_mask argument specifies whether I/O may be performed to release
Mel Gorman71baba42015-11-06 16:28:28 -08003939 * this page (__GFP_IO), and whether the call may block (__GFP_RECLAIM & __GFP_FS).
David Howellscf9a2ae2006-08-29 19:05:54 +01003940 *
Mike Rapoporta862f682019-03-05 15:48:42 -08003941 * Return: %1 if the release was successful, otherwise return zero.
David Howellscf9a2ae2006-08-29 19:05:54 +01003942 */
3943int try_to_release_page(struct page *page, gfp_t gfp_mask)
3944{
3945 struct address_space * const mapping = page->mapping;
3946
3947 BUG_ON(!PageLocked(page));
3948 if (PageWriteback(page))
3949 return 0;
3950
3951 if (mapping && mapping->a_ops->releasepage)
3952 return mapping->a_ops->releasepage(page, gfp_mask);
3953 return try_to_free_buffers(page);
3954}
3955
3956EXPORT_SYMBOL(try_to_release_page);