blob: 2fd9b2f24025ff69b2f272cfc5aaff8e74d4c9df [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);
Johannes Weiner449dd692014-04-03 14:47:56 -0700138
Matthew Wilcox5c024e6a2017-11-21 09:17:59 -0500139 xas_store(&xas, shadow);
140 xas_init_marks(&xas);
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200141
Matthew Wilcox (Oracle)a548b612021-05-08 00:35:49 -0400142 folio->mapping = NULL;
Jan Kara23006382017-11-15 17:37:26 -0800143 /* Leave page->index set: truncation lookup relies upon it */
Johannes Weinerd3798ae2016-10-04 22:02:08 +0200144 mapping->nrpages -= nr;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700145}
146
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400147static void filemap_unaccount_folio(struct address_space *mapping,
148 struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400150 long nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600152 /*
153 * if we're uptodate, flush out into the cleancache, otherwise
154 * invalidate any existing cleancache entries. We can't leave
155 * stale data around in the cleancache once our page is gone
156 */
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400157 if (folio_test_uptodate(folio) && folio_test_mappedtodisk(folio))
158 cleancache_put_page(&folio->page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600159 else
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400160 cleancache_invalidate_page(mapping, &folio->page);
Dan Magenheimerc515e1f2011-05-26 10:01:43 -0600161
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400162 VM_BUG_ON_FOLIO(folio_mapped(folio), folio);
163 if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(folio_mapped(folio))) {
Hugh Dickins06b241f2016-03-09 14:08:07 -0800164 int mapcount;
165
166 pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400167 current->comm, folio_pfn(folio));
168 dump_page(&folio->page, "still mapped when deleted");
Hugh Dickins06b241f2016-03-09 14:08:07 -0800169 dump_stack();
170 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
171
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400172 mapcount = page_mapcount(&folio->page);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800173 if (mapping_exiting(mapping) &&
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400174 folio_ref_count(folio) >= mapcount + 2) {
Hugh Dickins06b241f2016-03-09 14:08:07 -0800175 /*
176 * All vmas have already been torn down, so it's
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400177 * a good bet that actually the folio is unmapped,
Hugh Dickins06b241f2016-03-09 14:08:07 -0800178 * and we'd prefer not to leak it: if we're wrong,
179 * some other bad page check should catch it later.
180 */
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400181 page_mapcount_reset(&folio->page);
182 folio_ref_sub(folio, mapcount);
Hugh Dickins06b241f2016-03-09 14:08:07 -0800183 }
184 }
185
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400186 /* hugetlb folios do not participate in page cache accounting. */
187 if (folio_test_hugetlb(folio))
Jan Kara5ecc4d82017-11-15 17:37:29 -0800188 return;
Jan Kara76253fb2017-11-15 17:37:22 -0800189
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400190 nr = folio_nr_pages(folio);
Jan Kara5ecc4d82017-11-15 17:37:29 -0800191
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400192 __lruvec_stat_mod_folio(folio, NR_FILE_PAGES, -nr);
193 if (folio_test_swapbacked(folio)) {
194 __lruvec_stat_mod_folio(folio, NR_SHMEM, -nr);
195 if (folio_test_pmd_mappable(folio))
196 __lruvec_stat_mod_folio(folio, NR_SHMEM_THPS, -nr);
197 } else if (folio_test_pmd_mappable(folio)) {
198 __lruvec_stat_mod_folio(folio, NR_FILE_THPS, -nr);
Song Liu09d91cd2019-09-23 15:38:03 -0700199 filemap_nr_thps_dec(mapping);
Jan Kara76253fb2017-11-15 17:37:22 -0800200 }
Jan Kara5ecc4d82017-11-15 17:37:29 -0800201
202 /*
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400203 * At this point folio must be either written or cleaned by
204 * truncate. Dirty folio here signals a bug and loss of
Jan Kara5ecc4d82017-11-15 17:37:29 -0800205 * unwritten data.
206 *
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400207 * This fixes dirty accounting after removing the folio entirely
208 * but leaves the dirty flag set: it has no effect for truncated
209 * folio and anyway will be cleared before returning folio to
Jan Kara5ecc4d82017-11-15 17:37:29 -0800210 * buddy allocator.
211 */
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400212 if (WARN_ON_ONCE(folio_test_dirty(folio)))
213 folio_account_cleaned(folio, mapping,
214 inode_to_wb(mapping->host));
Jan Kara5ecc4d82017-11-15 17:37:29 -0800215}
216
217/*
218 * Delete a page from the page cache and free it. Caller has to make
219 * sure the page is locked and that nobody else uses it - or that usage
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700220 * is safe. The caller must hold the i_pages lock.
Jan Kara5ecc4d82017-11-15 17:37:29 -0800221 */
Matthew Wilcox (Oracle)452e9e62021-05-09 09:33:42 -0400222void __filemap_remove_folio(struct folio *folio, void *shadow)
Jan Kara5ecc4d82017-11-15 17:37:29 -0800223{
Matthew Wilcox (Oracle)452e9e62021-05-09 09:33:42 -0400224 struct address_space *mapping = folio->mapping;
Jan Kara5ecc4d82017-11-15 17:37:29 -0800225
Matthew Wilcox (Oracle)a0580c62021-07-23 09:29:46 -0400226 trace_mm_filemap_delete_from_page_cache(folio);
Matthew Wilcox (Oracle)621db482021-05-08 20:04:05 -0400227 filemap_unaccount_folio(mapping, folio);
Matthew Wilcox (Oracle)a548b612021-05-08 00:35:49 -0400228 page_cache_delete(mapping, folio, shadow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Matthew Wilcox (Oracle)78f42662021-07-28 15:52:34 -0400231void filemap_free_folio(struct address_space *mapping, struct folio *folio)
Jan Kara59c66c52017-11-15 17:37:18 -0800232{
233 void (*freepage)(struct page *);
234
235 freepage = mapping->a_ops->freepage;
236 if (freepage)
Matthew Wilcox (Oracle)452e9e62021-05-09 09:33:42 -0400237 freepage(&folio->page);
Jan Kara59c66c52017-11-15 17:37:18 -0800238
Matthew Wilcox (Oracle)452e9e62021-05-09 09:33:42 -0400239 if (folio_test_large(folio) && !folio_test_hugetlb(folio)) {
240 folio_ref_sub(folio, folio_nr_pages(folio));
241 VM_BUG_ON_FOLIO(folio_ref_count(folio) <= 0, folio);
Jan Kara59c66c52017-11-15 17:37:18 -0800242 } else {
Matthew Wilcox (Oracle)452e9e62021-05-09 09:33:42 -0400243 folio_put(folio);
Jan Kara59c66c52017-11-15 17:37:18 -0800244 }
245}
246
Minchan Kim702cfbf2011-03-22 16:32:43 -0700247/**
Matthew Wilcox (Oracle)452e9e62021-05-09 09:33:42 -0400248 * filemap_remove_folio - Remove folio from page cache.
249 * @folio: The folio.
Minchan Kim702cfbf2011-03-22 16:32:43 -0700250 *
Matthew Wilcox (Oracle)452e9e62021-05-09 09:33:42 -0400251 * This must be called only on folios that are locked and have been
252 * verified to be in the page cache. It will never put the folio into
253 * the free list because the caller has a reference on the page.
Minchan Kim702cfbf2011-03-22 16:32:43 -0700254 */
Matthew Wilcox (Oracle)452e9e62021-05-09 09:33:42 -0400255void filemap_remove_folio(struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Matthew Wilcox (Oracle)452e9e62021-05-09 09:33:42 -0400257 struct address_space *mapping = folio->mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Matthew Wilcox (Oracle)452e9e62021-05-09 09:33:42 -0400259 BUG_ON(!folio_test_locked(folio));
Johannes Weiner51b8c1f2021-11-08 18:31:24 -0800260 spin_lock(&mapping->host->i_lock);
Johannes Weiner30472502021-09-02 14:53:18 -0700261 xa_lock_irq(&mapping->i_pages);
Matthew Wilcox (Oracle)452e9e62021-05-09 09:33:42 -0400262 __filemap_remove_folio(folio, NULL);
Johannes Weiner30472502021-09-02 14:53:18 -0700263 xa_unlock_irq(&mapping->i_pages);
Johannes Weiner51b8c1f2021-11-08 18:31:24 -0800264 if (mapping_shrinkable(mapping))
265 inode_add_lru(mapping->host);
266 spin_unlock(&mapping->host->i_lock);
Linus Torvalds6072d132010-12-01 13:35:19 -0500267
Matthew Wilcox (Oracle)452e9e62021-05-09 09:33:42 -0400268 filemap_free_folio(mapping, folio);
Minchan Kim97cecb52011-03-22 16:30:53 -0700269}
Minchan Kim97cecb52011-03-22 16:30:53 -0700270
Jan Karaaa65c292017-11-15 17:37:33 -0800271/*
Matthew Wilcox (Oracle)51dcbda2021-12-07 14:15:07 -0500272 * page_cache_delete_batch - delete several folios from page cache
273 * @mapping: the mapping to which folios belong
274 * @fbatch: batch of folios to delete
Jan Karaaa65c292017-11-15 17:37:33 -0800275 *
Matthew Wilcox (Oracle)51dcbda2021-12-07 14:15:07 -0500276 * The function walks over mapping->i_pages and removes folios passed in
277 * @fbatch from the mapping. The function expects @fbatch to be sorted
278 * by page index and is optimised for it to be dense.
279 * It tolerates holes in @fbatch (mapping entries at those indices are not
280 * modified).
Jan Karaaa65c292017-11-15 17:37:33 -0800281 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700282 * The function expects the i_pages lock to be held.
Jan Karaaa65c292017-11-15 17:37:33 -0800283 */
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500284static void page_cache_delete_batch(struct address_space *mapping,
Matthew Wilcox (Oracle)51dcbda2021-12-07 14:15:07 -0500285 struct folio_batch *fbatch)
Jan Karaaa65c292017-11-15 17:37:33 -0800286{
Matthew Wilcox (Oracle)51dcbda2021-12-07 14:15:07 -0500287 XA_STATE(xas, &mapping->i_pages, fbatch->folios[0]->index);
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -0400288 long total_pages = 0;
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700289 int i = 0;
Matthew Wilcox (Oracle)1afd7ae2021-03-12 23:13:46 -0500290 struct folio *folio;
Jan Karaaa65c292017-11-15 17:37:33 -0800291
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500292 mapping_set_update(&xas, mapping);
Matthew Wilcox (Oracle)1afd7ae2021-03-12 23:13:46 -0500293 xas_for_each(&xas, folio, ULONG_MAX) {
Matthew Wilcox (Oracle)51dcbda2021-12-07 14:15:07 -0500294 if (i >= folio_batch_count(fbatch))
Jan Karaaa65c292017-11-15 17:37:33 -0800295 break;
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700296
297 /* A swap/dax/shadow entry got inserted? Skip it. */
Matthew Wilcox (Oracle)1afd7ae2021-03-12 23:13:46 -0500298 if (xa_is_value(folio))
Jan Karaaa65c292017-11-15 17:37:33 -0800299 continue;
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700300 /*
301 * A page got inserted in our range? Skip it. We have our
302 * pages locked so they are protected from being removed.
303 * If we see a page whose index is higher than ours, it
304 * means our page has been removed, which shouldn't be
305 * possible because we're holding the PageLock.
306 */
Matthew Wilcox (Oracle)51dcbda2021-12-07 14:15:07 -0500307 if (folio != fbatch->folios[i]) {
Matthew Wilcox (Oracle)1afd7ae2021-03-12 23:13:46 -0500308 VM_BUG_ON_FOLIO(folio->index >
Matthew Wilcox (Oracle)51dcbda2021-12-07 14:15:07 -0500309 fbatch->folios[i]->index, folio);
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700310 continue;
Linus Torvalds69bf4b62019-07-05 19:55:18 -0700311 }
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700312
Matthew Wilcox (Oracle)1afd7ae2021-03-12 23:13:46 -0500313 WARN_ON_ONCE(!folio_test_locked(folio));
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700314
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -0400315 folio->mapping = NULL;
Matthew Wilcox (Oracle)51dcbda2021-12-07 14:15:07 -0500316 /* Leave folio->index set: truncation lookup relies on it */
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700317
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -0400318 i++;
Matthew Wilcoxef8e5712017-12-04 03:59:45 -0500319 xas_store(&xas, NULL);
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -0400320 total_pages += folio_nr_pages(folio);
Jan Karaaa65c292017-11-15 17:37:33 -0800321 }
322 mapping->nrpages -= total_pages;
323}
324
325void delete_from_page_cache_batch(struct address_space *mapping,
Matthew Wilcox (Oracle)51dcbda2021-12-07 14:15:07 -0500326 struct folio_batch *fbatch)
Jan Karaaa65c292017-11-15 17:37:33 -0800327{
328 int i;
Jan Karaaa65c292017-11-15 17:37:33 -0800329
Matthew Wilcox (Oracle)51dcbda2021-12-07 14:15:07 -0500330 if (!folio_batch_count(fbatch))
Jan Karaaa65c292017-11-15 17:37:33 -0800331 return;
332
Johannes Weiner51b8c1f2021-11-08 18:31:24 -0800333 spin_lock(&mapping->host->i_lock);
Johannes Weiner30472502021-09-02 14:53:18 -0700334 xa_lock_irq(&mapping->i_pages);
Matthew Wilcox (Oracle)51dcbda2021-12-07 14:15:07 -0500335 for (i = 0; i < folio_batch_count(fbatch); i++) {
336 struct folio *folio = fbatch->folios[i];
Jan Karaaa65c292017-11-15 17:37:33 -0800337
Matthew Wilcox (Oracle)a0580c62021-07-23 09:29:46 -0400338 trace_mm_filemap_delete_from_page_cache(folio);
339 filemap_unaccount_folio(mapping, folio);
Jan Karaaa65c292017-11-15 17:37:33 -0800340 }
Matthew Wilcox (Oracle)51dcbda2021-12-07 14:15:07 -0500341 page_cache_delete_batch(mapping, fbatch);
Johannes Weiner30472502021-09-02 14:53:18 -0700342 xa_unlock_irq(&mapping->i_pages);
Johannes Weiner51b8c1f2021-11-08 18:31:24 -0800343 if (mapping_shrinkable(mapping))
344 inode_add_lru(mapping->host);
345 spin_unlock(&mapping->host->i_lock);
Jan Karaaa65c292017-11-15 17:37:33 -0800346
Matthew Wilcox (Oracle)51dcbda2021-12-07 14:15:07 -0500347 for (i = 0; i < folio_batch_count(fbatch); i++)
348 filemap_free_folio(mapping, fbatch->folios[i]);
Jan Karaaa65c292017-11-15 17:37:33 -0800349}
350
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200351int filemap_check_errors(struct address_space *mapping)
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700352{
353 int ret = 0;
354 /* Check for outstanding write errors */
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700355 if (test_bit(AS_ENOSPC, &mapping->flags) &&
356 test_and_clear_bit(AS_ENOSPC, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700357 ret = -ENOSPC;
Jens Axboe7fcbbaf2014-05-22 11:54:16 -0700358 if (test_bit(AS_EIO, &mapping->flags) &&
359 test_and_clear_bit(AS_EIO, &mapping->flags))
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700360 ret = -EIO;
361 return ret;
362}
Miklos Szeredid72d9e22016-07-29 14:10:57 +0200363EXPORT_SYMBOL(filemap_check_errors);
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700364
Jeff Layton76341ca2017-07-06 07:02:22 -0400365static int filemap_check_and_keep_errors(struct address_space *mapping)
366{
367 /* Check for outstanding write errors */
368 if (test_bit(AS_EIO, &mapping->flags))
369 return -EIO;
370 if (test_bit(AS_ENOSPC, &mapping->flags))
371 return -ENOSPC;
372 return 0;
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/**
Josef Bacik5a798492021-07-14 14:47:22 -0400376 * filemap_fdatawrite_wbc - start writeback on mapping dirty pages in range
377 * @mapping: address space structure to write
378 * @wbc: the writeback_control controlling the writeout
379 *
380 * Call writepages on the mapping using the provided wbc to control the
381 * writeout.
382 *
383 * Return: %0 on success, negative error code otherwise.
384 */
385int filemap_fdatawrite_wbc(struct address_space *mapping,
386 struct writeback_control *wbc)
387{
388 int ret;
389
390 if (!mapping_can_writeback(mapping) ||
391 !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
392 return 0;
393
394 wbc_attach_fdatawrite_inode(wbc, mapping->host);
395 ret = do_writepages(mapping, wbc);
396 wbc_detach_inode(wbc);
397 return ret;
398}
399EXPORT_SYMBOL(filemap_fdatawrite_wbc);
400
401/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700402 * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
Martin Waitz67be2dd2005-05-01 08:59:26 -0700403 * @mapping: address space structure to write
404 * @start: offset in bytes where the range starts
Andrew Morton469eb4d2006-03-24 03:17:45 -0800405 * @end: offset in bytes where the range ends (inclusive)
Martin Waitz67be2dd2005-05-01 08:59:26 -0700406 * @sync_mode: enable synchronous operation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 *
Randy Dunlap485bb992006-06-23 02:03:49 -0700408 * Start writeback against all of a mapping's dirty pages that lie
409 * within the byte offsets <start, end> inclusive.
410 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
Randy Dunlap485bb992006-06-23 02:03:49 -0700412 * opposed to a regular memory cleansing writeback. The difference between
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * these two operations is that if a dirty page/buffer is encountered, it must
414 * be waited upon, and not just skipped over.
Mike Rapoporta862f682019-03-05 15:48:42 -0800415 *
416 * Return: %0 on success, negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800418int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
419 loff_t end, int sync_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct writeback_control wbc = {
422 .sync_mode = sync_mode,
Nick Piggin05fe4782009-01-06 14:39:08 -0800423 .nr_to_write = LONG_MAX,
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700424 .range_start = start,
425 .range_end = end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 };
427
Josef Bacik5a798492021-07-14 14:47:22 -0400428 return filemap_fdatawrite_wbc(mapping, &wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431static inline int __filemap_fdatawrite(struct address_space *mapping,
432 int sync_mode)
433{
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -0700434 return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437int filemap_fdatawrite(struct address_space *mapping)
438{
439 return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
440}
441EXPORT_SYMBOL(filemap_fdatawrite);
442
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400443int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
Andrew Mortonebcf28e2006-03-24 03:18:04 -0800444 loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
447}
Jan Karaf4c0a0f2008-07-11 19:27:31 -0400448EXPORT_SYMBOL(filemap_fdatawrite_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Randy Dunlap485bb992006-06-23 02:03:49 -0700450/**
451 * filemap_flush - mostly a non-blocking flush
452 * @mapping: target address_space
453 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 * This is a mostly non-blocking flush. Not suitable for data-integrity
455 * purposes - I/O may not be started against all dirty pages.
Mike Rapoporta862f682019-03-05 15:48:42 -0800456 *
457 * Return: %0 on success, negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 */
459int filemap_flush(struct address_space *mapping)
460{
461 return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
462}
463EXPORT_SYMBOL(filemap_flush);
464
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500465/**
466 * filemap_range_has_page - check if a page exists in range.
467 * @mapping: address space within which to check
468 * @start_byte: offset in bytes where the range starts
469 * @end_byte: offset in bytes where the range ends (inclusive)
470 *
471 * Find at least one page in the range supplied, usually used to check if
472 * direct writing in this range will trigger a writeback.
Mike Rapoporta862f682019-03-05 15:48:42 -0800473 *
474 * Return: %true if at least one page exists in the specified range,
475 * %false otherwise.
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500476 */
477bool filemap_range_has_page(struct address_space *mapping,
478 loff_t start_byte, loff_t end_byte)
479{
Jan Karaf7b68042017-09-06 16:21:40 -0700480 struct page *page;
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500481 XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT);
482 pgoff_t max = end_byte >> PAGE_SHIFT;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500483
484 if (end_byte < start_byte)
485 return false;
486
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500487 rcu_read_lock();
488 for (;;) {
489 page = xas_find(&xas, max);
490 if (xas_retry(&xas, page))
491 continue;
492 /* Shadow entries don't count */
493 if (xa_is_value(page))
494 continue;
495 /*
496 * We don't need to try to pin this page; we're about to
497 * release the RCU lock anyway. It is enough to know that
498 * there was a page here recently.
499 */
500 break;
501 }
502 rcu_read_unlock();
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500503
Matthew Wilcox8fa8e532018-01-16 06:26:49 -0500504 return page != NULL;
Goldwyn Rodrigues7fc9e472017-06-20 07:05:41 -0500505}
506EXPORT_SYMBOL(filemap_range_has_page);
507
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400508static void __filemap_fdatawait_range(struct address_space *mapping,
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800509 loff_t start_byte, loff_t end_byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300511 pgoff_t index = start_byte >> PAGE_SHIFT;
512 pgoff_t end = end_byte >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct pagevec pvec;
514 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200516 if (end_byte < start_byte)
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400517 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Mel Gorman86679822017-11-15 17:37:52 -0800519 pagevec_init(&pvec);
Jan Kara312e9d22017-11-15 17:35:05 -0800520 while (index <= end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 unsigned i;
522
Jan Kara312e9d22017-11-15 17:35:05 -0800523 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
Jan Kara67fd7072017-11-15 17:35:19 -0800524 end, PAGECACHE_TAG_WRITEBACK);
Jan Kara312e9d22017-11-15 17:35:05 -0800525 if (!nr_pages)
526 break;
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 for (i = 0; i < nr_pages; i++) {
529 struct page *page = pvec.pages[i];
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 wait_on_page_writeback(page);
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400532 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534 pagevec_release(&pvec);
535 cond_resched();
536 }
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800537}
538
539/**
540 * filemap_fdatawait_range - wait for writeback to complete
541 * @mapping: address space structure to wait for
542 * @start_byte: offset in bytes where the range starts
543 * @end_byte: offset in bytes where the range ends (inclusive)
544 *
545 * Walk the list of under-writeback pages of the given address space
546 * in the given range and wait for all of them. Check error status of
547 * the address space and return it.
548 *
549 * Since the error status of the address space is cleared by this function,
550 * callers are responsible for checking the return value and handling and/or
551 * reporting the error.
Mike Rapoporta862f682019-03-05 15:48:42 -0800552 *
553 * Return: error status of the address space.
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800554 */
555int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
556 loff_t end_byte)
557{
Jeff Layton5e8fcc12017-07-06 07:02:24 -0400558 __filemap_fdatawait_range(mapping, start_byte, end_byte);
559 return filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
Jan Karad3bccb6f2009-08-17 19:30:27 +0200561EXPORT_SYMBOL(filemap_fdatawait_range);
562
563/**
Ross Zwisleraa0bfcd2019-06-20 17:05:37 -0400564 * filemap_fdatawait_range_keep_errors - wait for writeback to complete
565 * @mapping: address space structure to wait for
566 * @start_byte: offset in bytes where the range starts
567 * @end_byte: offset in bytes where the range ends (inclusive)
568 *
569 * Walk the list of under-writeback pages of the given address space in the
570 * given range and wait for all of them. Unlike filemap_fdatawait_range(),
571 * this function does not clear error status of the address space.
572 *
573 * Use this function if callers don't handle errors themselves. Expected
574 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
575 * fsfreeze(8)
576 */
577int filemap_fdatawait_range_keep_errors(struct address_space *mapping,
578 loff_t start_byte, loff_t end_byte)
579{
580 __filemap_fdatawait_range(mapping, start_byte, end_byte);
581 return filemap_check_and_keep_errors(mapping);
582}
583EXPORT_SYMBOL(filemap_fdatawait_range_keep_errors);
584
585/**
Jeff Laytona823e452017-07-28 07:24:43 -0400586 * file_fdatawait_range - wait for writeback to complete
587 * @file: file pointing to address space structure to wait for
588 * @start_byte: offset in bytes where the range starts
589 * @end_byte: offset in bytes where the range ends (inclusive)
590 *
591 * Walk the list of under-writeback pages of the address space that file
592 * refers to, in the given range and wait for all of them. Check error
593 * status of the address space vs. the file->f_wb_err cursor and return it.
594 *
595 * Since the error status of the file is advanced by this function,
596 * callers are responsible for checking the return value and handling and/or
597 * reporting the error.
Mike Rapoporta862f682019-03-05 15:48:42 -0800598 *
599 * Return: error status of the address space vs. the file->f_wb_err cursor.
Jeff Laytona823e452017-07-28 07:24:43 -0400600 */
601int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
602{
603 struct address_space *mapping = file->f_mapping;
604
605 __filemap_fdatawait_range(mapping, start_byte, end_byte);
606 return file_check_and_advance_wb_err(file);
607}
608EXPORT_SYMBOL(file_fdatawait_range);
609
610/**
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800611 * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
612 * @mapping: address space structure to wait for
613 *
614 * Walk the list of under-writeback pages of the given address space
615 * and wait for all of them. Unlike filemap_fdatawait(), this function
616 * does not clear error status of the address space.
617 *
618 * Use this function if callers don't handle errors themselves. Expected
619 * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
620 * fsfreeze(8)
Mike Rapoporta862f682019-03-05 15:48:42 -0800621 *
622 * Return: error status of the address space.
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800623 */
Jeff Layton76341ca2017-07-06 07:02:22 -0400624int filemap_fdatawait_keep_errors(struct address_space *mapping)
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800625{
Jeff Laytonffb959b2017-07-31 10:29:38 -0400626 __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
Jeff Layton76341ca2017-07-06 07:02:22 -0400627 return filemap_check_and_keep_errors(mapping);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800628}
Jeff Layton76341ca2017-07-06 07:02:22 -0400629EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
Junichi Nomuraaa750fd2015-11-05 18:47:23 -0800630
Konstantin Khlebnikov875d91b2019-09-23 15:34:48 -0700631/* Returns true if writeback might be needed or already in progress. */
Jeff Layton9326c9b2017-07-26 10:21:11 -0400632static bool mapping_needs_writeback(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Konstantin Khlebnikov875d91b2019-09-23 15:34:48 -0700634 return mapping->nrpages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Jens Axboe4bdcd1d2021-10-28 08:47:05 -0600637bool filemap_range_has_writeback(struct address_space *mapping,
638 loff_t start_byte, loff_t end_byte)
Jens Axboef8ee8902021-11-05 13:37:13 -0700639{
640 XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT);
641 pgoff_t max = end_byte >> PAGE_SHIFT;
642 struct page *page;
643
644 if (end_byte < start_byte)
645 return false;
646
647 rcu_read_lock();
648 xas_for_each(&xas, page, max) {
649 if (xas_retry(&xas, page))
650 continue;
651 if (xa_is_value(page))
652 continue;
653 if (PageDirty(page) || PageLocked(page) || PageWriteback(page))
654 break;
655 }
656 rcu_read_unlock();
657 return page != NULL;
Jens Axboef8ee8902021-11-05 13:37:13 -0700658}
Jens Axboe4bdcd1d2021-10-28 08:47:05 -0600659EXPORT_SYMBOL_GPL(filemap_range_has_writeback);
Jens Axboe63135aa2021-04-29 22:55:18 -0700660
661/**
Randy Dunlap485bb992006-06-23 02:03:49 -0700662 * filemap_write_and_wait_range - write out & wait on a file range
663 * @mapping: the address_space for the pages
664 * @lstart: offset in bytes where the range starts
665 * @lend: offset in bytes where the range ends (inclusive)
666 *
Andrew Morton469eb4d2006-03-24 03:17:45 -0800667 * Write out and wait upon file offsets lstart->lend, inclusive.
668 *
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -0300669 * Note that @lend is inclusive (describes the last byte to be written) so
Andrew Morton469eb4d2006-03-24 03:17:45 -0800670 * that this function can be used to write to the very end-of-file (end = -1).
Mike Rapoporta862f682019-03-05 15:48:42 -0800671 *
672 * Return: error status of the address space.
Andrew Morton469eb4d2006-03-24 03:17:45 -0800673 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674int filemap_write_and_wait_range(struct address_space *mapping,
675 loff_t lstart, loff_t lend)
676{
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800677 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Jeff Layton9326c9b2017-07-26 10:21:11 -0400679 if (mapping_needs_writeback(mapping)) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800680 err = __filemap_fdatawrite_range(mapping, lstart, lend,
681 WB_SYNC_ALL);
Ira Weinyddf8f372020-01-30 22:12:07 -0800682 /*
683 * Even if the above returned error, the pages may be
684 * written partially (e.g. -ENOSPC), so we wait for it.
685 * But the -EIO is special case, it may indicate the worst
686 * thing (e.g. bug) happened, so we avoid waiting for it.
687 */
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800688 if (err != -EIO) {
Christoph Hellwig94004ed2009-09-30 22:16:33 +0200689 int err2 = filemap_fdatawait_range(mapping,
690 lstart, lend);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800691 if (!err)
692 err = err2;
Jeff Laytoncbeaf952017-07-06 07:02:23 -0400693 } else {
694 /* Clear any previously stored errors */
695 filemap_check_errors(mapping);
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800696 }
Dmitry Monakhov865ffef32013-04-29 15:08:42 -0700697 } else {
698 err = filemap_check_errors(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800700 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
Chris Masonf6995582009-04-15 13:22:37 -0400702EXPORT_SYMBOL(filemap_write_and_wait_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Jeff Layton5660e132017-07-06 07:02:25 -0400704void __filemap_set_wb_err(struct address_space *mapping, int err)
705{
Jeff Layton3acdfd22017-07-24 06:22:15 -0400706 errseq_t eseq = errseq_set(&mapping->wb_err, err);
Jeff Layton5660e132017-07-06 07:02:25 -0400707
708 trace_filemap_set_wb_err(mapping, eseq);
709}
710EXPORT_SYMBOL(__filemap_set_wb_err);
711
712/**
713 * file_check_and_advance_wb_err - report wb error (if any) that was previously
714 * and advance wb_err to current one
715 * @file: struct file on which the error is being reported
716 *
717 * When userland calls fsync (or something like nfsd does the equivalent), we
718 * want to report any writeback errors that occurred since the last fsync (or
719 * since the file was opened if there haven't been any).
720 *
721 * Grab the wb_err from the mapping. If it matches what we have in the file,
722 * then just quickly return 0. The file is all caught up.
723 *
724 * If it doesn't match, then take the mapping value, set the "seen" flag in
725 * it and try to swap it into place. If it works, or another task beat us
726 * to it with the new value, then update the f_wb_err and return the error
727 * portion. The error at this point must be reported via proper channels
728 * (a'la fsync, or NFS COMMIT operation, etc.).
729 *
730 * While we handle mapping->wb_err with atomic operations, the f_wb_err
731 * value is protected by the f_lock since we must ensure that it reflects
732 * the latest value swapped in for this file descriptor.
Mike Rapoporta862f682019-03-05 15:48:42 -0800733 *
734 * Return: %0 on success, negative error code otherwise.
Jeff Layton5660e132017-07-06 07:02:25 -0400735 */
736int file_check_and_advance_wb_err(struct file *file)
737{
738 int err = 0;
739 errseq_t old = READ_ONCE(file->f_wb_err);
740 struct address_space *mapping = file->f_mapping;
741
742 /* Locklessly handle the common case where nothing has changed */
743 if (errseq_check(&mapping->wb_err, old)) {
744 /* Something changed, must use slow path */
745 spin_lock(&file->f_lock);
746 old = file->f_wb_err;
747 err = errseq_check_and_advance(&mapping->wb_err,
748 &file->f_wb_err);
749 trace_file_check_and_advance_wb_err(file, old);
750 spin_unlock(&file->f_lock);
751 }
Jeff Laytonf4e222c2017-10-03 16:15:25 -0700752
753 /*
754 * We're mostly using this function as a drop in replacement for
755 * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect
756 * that the legacy code would have had on these flags.
757 */
758 clear_bit(AS_EIO, &mapping->flags);
759 clear_bit(AS_ENOSPC, &mapping->flags);
Jeff Layton5660e132017-07-06 07:02:25 -0400760 return err;
761}
762EXPORT_SYMBOL(file_check_and_advance_wb_err);
763
764/**
765 * file_write_and_wait_range - write out & wait on a file range
766 * @file: file pointing to address_space with pages
767 * @lstart: offset in bytes where the range starts
768 * @lend: offset in bytes where the range ends (inclusive)
769 *
770 * Write out and wait upon file offsets lstart->lend, inclusive.
771 *
772 * Note that @lend is inclusive (describes the last byte to be written) so
773 * that this function can be used to write to the very end-of-file (end = -1).
774 *
775 * After writing out and waiting on the data, we check and advance the
776 * f_wb_err cursor to the latest value, and return any errors detected there.
Mike Rapoporta862f682019-03-05 15:48:42 -0800777 *
778 * Return: %0 on success, negative error code otherwise.
Jeff Layton5660e132017-07-06 07:02:25 -0400779 */
780int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
781{
782 int err = 0, err2;
783 struct address_space *mapping = file->f_mapping;
784
Jeff Layton9326c9b2017-07-26 10:21:11 -0400785 if (mapping_needs_writeback(mapping)) {
Jeff Layton5660e132017-07-06 07:02:25 -0400786 err = __filemap_fdatawrite_range(mapping, lstart, lend,
787 WB_SYNC_ALL);
788 /* See comment of filemap_write_and_wait() */
789 if (err != -EIO)
790 __filemap_fdatawait_range(mapping, lstart, lend);
791 }
792 err2 = file_check_and_advance_wb_err(file);
793 if (!err)
794 err = err2;
795 return err;
796}
797EXPORT_SYMBOL(file_write_and_wait_range);
798
Randy Dunlap485bb992006-06-23 02:03:49 -0700799/**
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700800 * replace_page_cache_page - replace a pagecache page with a new one
801 * @old: page to be replaced
802 * @new: page to replace with
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700803 *
804 * This function replaces a page in the pagecache with a new one. On
805 * success it acquires the pagecache reference for the new page and
806 * drops it for the old page. Both the old and new pages must be
807 * locked. This function does not add the new page to the LRU, the
808 * caller must do that.
809 *
Matthew Wilcox74d60952017-11-17 10:01:45 -0500810 * The remove + add is atomic. This function cannot fail.
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700811 */
Baolin Wang1f7ef652021-02-24 12:01:42 -0800812void replace_page_cache_page(struct page *old, struct page *new)
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700813{
Matthew Wilcox (Oracle)d21bba22021-05-06 18:14:59 -0400814 struct folio *fold = page_folio(old);
815 struct folio *fnew = page_folio(new);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500816 struct address_space *mapping = old->mapping;
817 void (*freepage)(struct page *) = mapping->a_ops->freepage;
818 pgoff_t offset = old->index;
819 XA_STATE(xas, &mapping->i_pages, offset);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700820
Sasha Levin309381fea2014-01-23 15:52:54 -0800821 VM_BUG_ON_PAGE(!PageLocked(old), old);
822 VM_BUG_ON_PAGE(!PageLocked(new), new);
823 VM_BUG_ON_PAGE(new->mapping, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700824
Matthew Wilcox74d60952017-11-17 10:01:45 -0500825 get_page(new);
826 new->mapping = mapping;
827 new->index = offset;
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700828
Matthew Wilcox (Oracle)d21bba22021-05-06 18:14:59 -0400829 mem_cgroup_migrate(fold, fnew);
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700830
Johannes Weiner30472502021-09-02 14:53:18 -0700831 xas_lock_irq(&xas);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500832 xas_store(&xas, new);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700833
Matthew Wilcox74d60952017-11-17 10:01:45 -0500834 old->mapping = NULL;
835 /* hugetlb pages do not participate in page cache accounting. */
836 if (!PageHuge(old))
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700837 __dec_lruvec_page_state(old, NR_FILE_PAGES);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500838 if (!PageHuge(new))
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700839 __inc_lruvec_page_state(new, NR_FILE_PAGES);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500840 if (PageSwapBacked(old))
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700841 __dec_lruvec_page_state(old, NR_SHMEM);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500842 if (PageSwapBacked(new))
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700843 __inc_lruvec_page_state(new, NR_SHMEM);
Johannes Weiner30472502021-09-02 14:53:18 -0700844 xas_unlock_irq(&xas);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500845 if (freepage)
846 freepage(old);
847 put_page(old);
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700848}
849EXPORT_SYMBOL_GPL(replace_page_cache_page);
850
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500851noinline int __filemap_add_folio(struct address_space *mapping,
852 struct folio *folio, pgoff_t index, gfp_t gfp, void **shadowp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500854 XA_STATE(xas, &mapping->i_pages, index);
855 int huge = folio_test_hugetlb(folio);
Nick Piggine2867812008-07-25 19:45:30 -0700856 int error;
Waiman Longda742402021-02-04 18:32:45 -0800857 bool charged = false;
Nick Piggine2867812008-07-25 19:45:30 -0700858
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500859 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
860 VM_BUG_ON_FOLIO(folio_test_swapbacked(folio), folio);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500861 mapping_set_update(&xas, mapping);
Nick Piggine2867812008-07-25 19:45:30 -0700862
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500863 folio_get(folio);
864 folio->mapping = mapping;
865 folio->index = index;
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700866
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700867 if (!huge) {
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500868 error = mem_cgroup_charge(folio, NULL, gfp);
869 VM_BUG_ON_FOLIO(index & (folio_nr_pages(folio) - 1), folio);
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700870 if (error)
871 goto error;
Waiman Longda742402021-02-04 18:32:45 -0800872 charged = true;
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700873 }
874
Matthew Wilcox (Oracle)198b62f2020-10-15 20:05:20 -0700875 gfp &= GFP_RECLAIM_MASK;
876
Matthew Wilcox74d60952017-11-17 10:01:45 -0500877 do {
Matthew Wilcox (Oracle)198b62f2020-10-15 20:05:20 -0700878 unsigned int order = xa_get_order(xas.xa, xas.xa_index);
879 void *entry, *old = NULL;
880
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500881 if (order > folio_order(folio))
Matthew Wilcox (Oracle)198b62f2020-10-15 20:05:20 -0700882 xas_split_alloc(&xas, xa_load(xas.xa, xas.xa_index),
883 order, gfp);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500884 xas_lock_irq(&xas);
Matthew Wilcox (Oracle)198b62f2020-10-15 20:05:20 -0700885 xas_for_each_conflict(&xas, entry) {
886 old = entry;
887 if (!xa_is_value(entry)) {
888 xas_set_err(&xas, -EEXIST);
889 goto unlock;
890 }
891 }
892
893 if (old) {
894 if (shadowp)
895 *shadowp = old;
896 /* entry may have been split before we acquired lock */
897 order = xa_get_order(xas.xa, xas.xa_index);
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500898 if (order > folio_order(folio)) {
Matthew Wilcox (Oracle)198b62f2020-10-15 20:05:20 -0700899 xas_split(&xas, old, order);
900 xas_reset(&xas);
901 }
902 }
903
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500904 xas_store(&xas, folio);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500905 if (xas_error(&xas))
906 goto unlock;
Michal Hocko4165b9b42015-06-24 16:57:24 -0700907
Matthew Wilcox74d60952017-11-17 10:01:45 -0500908 mapping->nrpages++;
909
910 /* hugetlb pages do not participate in page cache accounting */
911 if (!huge)
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500912 __lruvec_stat_add_folio(folio, NR_FILE_PAGES);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500913unlock:
914 xas_unlock_irq(&xas);
Matthew Wilcox (Oracle)198b62f2020-10-15 20:05:20 -0700915 } while (xas_nomem(&xas, gfp));
Matthew Wilcox74d60952017-11-17 10:01:45 -0500916
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700917 if (xas_error(&xas)) {
918 error = xas_error(&xas);
Waiman Longda742402021-02-04 18:32:45 -0800919 if (charged)
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500920 mem_cgroup_uncharge(folio);
Matthew Wilcox74d60952017-11-17 10:01:45 -0500921 goto error;
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700922 }
Matthew Wilcox74d60952017-11-17 10:01:45 -0500923
Matthew Wilcox (Oracle)a0580c62021-07-23 09:29:46 -0400924 trace_mm_filemap_add_to_page_cache(folio);
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700925 return 0;
Matthew Wilcox74d60952017-11-17 10:01:45 -0500926error:
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500927 folio->mapping = NULL;
Kirill A. Shutemov66a0c8e2013-09-12 15:13:59 -0700928 /* Leave page->index set: truncation relies upon it */
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500929 folio_put(folio);
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700930 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500932ALLOW_ERROR_INJECTION(__filemap_add_folio, ERRNO);
Johannes Weinera5289102014-04-03 14:47:51 -0700933
934/**
935 * add_to_page_cache_locked - add a locked page to the pagecache
936 * @page: page to add
937 * @mapping: the page's address_space
938 * @offset: page index
939 * @gfp_mask: page allocation mode
940 *
941 * This function is used to add a page to the pagecache. It must be locked.
942 * This function does not add the page to the LRU. The caller must do that.
Mike Rapoporta862f682019-03-05 15:48:42 -0800943 *
944 * Return: %0 on success, negative error code otherwise.
Johannes Weinera5289102014-04-03 14:47:51 -0700945 */
946int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
947 pgoff_t offset, gfp_t gfp_mask)
948{
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500949 return __filemap_add_folio(mapping, page_folio(page), offset,
Johannes Weinera5289102014-04-03 14:47:51 -0700950 gfp_mask, NULL);
951}
Nick Piggine2867812008-07-25 19:45:30 -0700952EXPORT_SYMBOL(add_to_page_cache_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500954int filemap_add_folio(struct address_space *mapping, struct folio *folio,
955 pgoff_t index, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Johannes Weinera5289102014-04-03 14:47:51 -0700957 void *shadow = NULL;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700958 int ret;
959
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500960 __folio_set_locked(folio);
961 ret = __filemap_add_folio(mapping, folio, index, gfp, &shadow);
Johannes Weinera5289102014-04-03 14:47:51 -0700962 if (unlikely(ret))
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500963 __folio_clear_locked(folio);
Johannes Weinera5289102014-04-03 14:47:51 -0700964 else {
965 /*
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500966 * The folio might have been evicted from cache only
Johannes Weinera5289102014-04-03 14:47:51 -0700967 * recently, in which case it should be activated like
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500968 * any other repeatedly accessed folio.
969 * The exception is folios getting rewritten; evicting other
Rik van Rielf0281a02016-05-20 16:56:25 -0700970 * data from the working set, only to cache data that will
971 * get overwritten with something else, is a waste of memory.
Johannes Weinera5289102014-04-03 14:47:51 -0700972 */
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500973 WARN_ON_ONCE(folio_test_active(folio));
974 if (!(gfp & __GFP_WRITE) && shadow)
975 workingset_refault(folio, shadow);
976 folio_add_lru(folio);
Johannes Weinera5289102014-04-03 14:47:51 -0700977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return ret;
979}
Matthew Wilcox (Oracle)9dd3d062020-12-08 08:56:28 -0500980EXPORT_SYMBOL_GPL(filemap_add_folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Paul Jackson44110fe2006-03-24 03:16:04 -0800982#ifdef CONFIG_NUMA
Matthew Wilcox (Oracle)bb3c5792020-12-15 23:11:07 -0500983struct folio *filemap_alloc_folio(gfp_t gfp, unsigned int order)
Paul Jackson44110fe2006-03-24 03:16:04 -0800984{
Miao Xiec0ff7452010-05-24 14:32:08 -0700985 int n;
Matthew Wilcox (Oracle)bb3c5792020-12-15 23:11:07 -0500986 struct folio *folio;
Miao Xiec0ff7452010-05-24 14:32:08 -0700987
Paul Jackson44110fe2006-03-24 03:16:04 -0800988 if (cpuset_do_page_mem_spread()) {
Mel Gormancc9a6c82012-03-21 16:34:11 -0700989 unsigned int cpuset_mems_cookie;
990 do {
Mel Gormand26914d2014-04-03 14:47:24 -0700991 cpuset_mems_cookie = read_mems_allowed_begin();
Mel Gormancc9a6c82012-03-21 16:34:11 -0700992 n = cpuset_mem_spread_node();
Matthew Wilcox (Oracle)bb3c5792020-12-15 23:11:07 -0500993 folio = __folio_alloc_node(gfp, order, n);
994 } while (!folio && read_mems_allowed_retry(cpuset_mems_cookie));
Mel Gormancc9a6c82012-03-21 16:34:11 -0700995
Matthew Wilcox (Oracle)bb3c5792020-12-15 23:11:07 -0500996 return folio;
Paul Jackson44110fe2006-03-24 03:16:04 -0800997 }
Matthew Wilcox (Oracle)bb3c5792020-12-15 23:11:07 -0500998 return folio_alloc(gfp, order);
Paul Jackson44110fe2006-03-24 03:16:04 -0800999}
Matthew Wilcox (Oracle)bb3c5792020-12-15 23:11:07 -05001000EXPORT_SYMBOL(filemap_alloc_folio);
Paul Jackson44110fe2006-03-24 03:16:04 -08001001#endif
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003/*
Jan Kara7506ae62021-05-24 13:02:30 +02001004 * filemap_invalidate_lock_two - lock invalidate_lock for two mappings
1005 *
1006 * Lock exclusively invalidate_lock of any passed mapping that is not NULL.
1007 *
1008 * @mapping1: the first mapping to lock
1009 * @mapping2: the second mapping to lock
1010 */
1011void filemap_invalidate_lock_two(struct address_space *mapping1,
1012 struct address_space *mapping2)
1013{
1014 if (mapping1 > mapping2)
1015 swap(mapping1, mapping2);
1016 if (mapping1)
1017 down_write(&mapping1->invalidate_lock);
1018 if (mapping2 && mapping1 != mapping2)
1019 down_write_nested(&mapping2->invalidate_lock, 1);
1020}
1021EXPORT_SYMBOL(filemap_invalidate_lock_two);
1022
1023/*
1024 * filemap_invalidate_unlock_two - unlock invalidate_lock for two mappings
1025 *
1026 * Unlock exclusive invalidate_lock of any passed mapping that is not NULL.
1027 *
1028 * @mapping1: the first mapping to unlock
1029 * @mapping2: the second mapping to unlock
1030 */
1031void filemap_invalidate_unlock_two(struct address_space *mapping1,
1032 struct address_space *mapping2)
1033{
1034 if (mapping1)
1035 up_write(&mapping1->invalidate_lock);
1036 if (mapping2 && mapping1 != mapping2)
1037 up_write(&mapping2->invalidate_lock);
1038}
1039EXPORT_SYMBOL(filemap_invalidate_unlock_two);
1040
1041/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 * In order to wait for pages to become available there must be
1043 * waitqueues associated with pages. By using a hash table of
1044 * waitqueues where the bucket discipline is to maintain all
1045 * waiters on the same queue and wake all when any of the pages
1046 * become available, and for the woken contexts to check to be
1047 * sure the appropriate page became available, this saves space
1048 * at a cost of "thundering herd" phenomena during rare hash
1049 * collisions.
1050 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001051#define PAGE_WAIT_TABLE_BITS 8
1052#define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001053static wait_queue_head_t folio_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
Nicholas Piggin62906022016-12-25 13:00:30 +10001054
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001055static wait_queue_head_t *folio_waitqueue(struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001057 return &folio_wait_table[hash_ptr(folio, PAGE_WAIT_TABLE_BITS)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
Nicholas Piggin62906022016-12-25 13:00:30 +10001059
1060void __init pagecache_init(void)
1061{
1062 int i;
1063
1064 for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001065 init_waitqueue_head(&folio_wait_table[i]);
Nicholas Piggin62906022016-12-25 13:00:30 +10001066
1067 page_writeback_init();
1068}
1069
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001070/*
1071 * The page wait code treats the "wait->flags" somewhat unusually, because
Linus Torvalds5868ec22020-09-20 10:38:47 -07001072 * we have multiple different kinds of waits, not just the usual "exclusive"
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001073 * one.
1074 *
1075 * We have:
1076 *
1077 * (a) no special bits set:
1078 *
1079 * We're just waiting for the bit to be released, and when a waker
1080 * calls the wakeup function, we set WQ_FLAG_WOKEN and wake it up,
1081 * and remove it from the wait queue.
1082 *
1083 * Simple and straightforward.
1084 *
1085 * (b) WQ_FLAG_EXCLUSIVE:
1086 *
1087 * The waiter is waiting to get the lock, and only one waiter should
1088 * be woken up to avoid any thundering herd behavior. We'll set the
1089 * WQ_FLAG_WOKEN bit, wake it up, and remove it from the wait queue.
1090 *
1091 * This is the traditional exclusive wait.
1092 *
Linus Torvalds5868ec22020-09-20 10:38:47 -07001093 * (c) WQ_FLAG_EXCLUSIVE | WQ_FLAG_CUSTOM:
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001094 *
1095 * The waiter is waiting to get the bit, and additionally wants the
1096 * lock to be transferred to it for fair lock behavior. If the lock
1097 * cannot be taken, we stop walking the wait queue without waking
1098 * the waiter.
1099 *
1100 * This is the "fair lock handoff" case, and in addition to setting
1101 * WQ_FLAG_WOKEN, we set WQ_FLAG_DONE to let the waiter easily see
1102 * that it now has the lock.
1103 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001104static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
Nicholas Piggin62906022016-12-25 13:00:30 +10001105{
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001106 unsigned int flags;
Nicholas Piggin62906022016-12-25 13:00:30 +10001107 struct wait_page_key *key = arg;
1108 struct wait_page_queue *wait_page
1109 = container_of(wait, struct wait_page_queue, wait);
1110
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07001111 if (!wake_page_match(wait_page, key))
Nicholas Piggin62906022016-12-25 13:00:30 +10001112 return 0;
Linus Torvalds3510ca22017-08-27 13:55:12 -07001113
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001114 /*
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001115 * If it's a lock handoff wait, we get the bit for it, and
1116 * stop walking (and do not wake it up) if we can't.
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001117 */
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001118 flags = wait->flags;
1119 if (flags & WQ_FLAG_EXCLUSIVE) {
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001120 if (test_bit(key->bit_nr, &key->folio->flags))
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001121 return -1;
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001122 if (flags & WQ_FLAG_CUSTOM) {
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001123 if (test_and_set_bit(key->bit_nr, &key->folio->flags))
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001124 return -1;
1125 flags |= WQ_FLAG_DONE;
1126 }
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001127 }
Nicholas Piggin62906022016-12-25 13:00:30 +10001128
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001129 /*
1130 * We are holding the wait-queue lock, but the waiter that
1131 * is waiting for this will be checking the flags without
1132 * any locking.
1133 *
1134 * So update the flags atomically, and wake up the waiter
1135 * afterwards to avoid any races. This store-release pairs
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001136 * with the load-acquire in folio_wait_bit_common().
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001137 */
1138 smp_store_release(&wait->flags, flags | WQ_FLAG_WOKEN);
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001139 wake_up_state(wait->private, mode);
1140
1141 /*
1142 * Ok, we have successfully done what we're waiting for,
1143 * and we can unconditionally remove the wait entry.
1144 *
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001145 * Note that this pairs with the "finish_wait()" in the
1146 * waiter, and has to be the absolute last thing we do.
1147 * After this list_del_init(&wait->entry) the wait entry
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001148 * might be de-allocated and the process might even have
1149 * exited.
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001150 */
Linus Torvaldsc6fe44d2020-07-23 12:33:41 -07001151 list_del_init_careful(&wait->entry);
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001152 return (flags & WQ_FLAG_EXCLUSIVE) != 0;
Nicholas Piggin62906022016-12-25 13:00:30 +10001153}
1154
Matthew Wilcox (Oracle)6974d7c2021-01-15 17:14:48 -05001155static void folio_wake_bit(struct folio *folio, int bit_nr)
Nicholas Piggin62906022016-12-25 13:00:30 +10001156{
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001157 wait_queue_head_t *q = folio_waitqueue(folio);
Nicholas Piggin62906022016-12-25 13:00:30 +10001158 struct wait_page_key key;
1159 unsigned long flags;
Tim Chen11a19c72017-08-25 09:13:55 -07001160 wait_queue_entry_t bookmark;
Nicholas Piggin62906022016-12-25 13:00:30 +10001161
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001162 key.folio = folio;
Nicholas Piggin62906022016-12-25 13:00:30 +10001163 key.bit_nr = bit_nr;
1164 key.page_match = 0;
1165
Tim Chen11a19c72017-08-25 09:13:55 -07001166 bookmark.flags = 0;
1167 bookmark.private = NULL;
1168 bookmark.func = NULL;
1169 INIT_LIST_HEAD(&bookmark.entry);
1170
Nicholas Piggin62906022016-12-25 13:00:30 +10001171 spin_lock_irqsave(&q->lock, flags);
Tim Chen11a19c72017-08-25 09:13:55 -07001172 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1173
1174 while (bookmark.flags & WQ_FLAG_BOOKMARK) {
1175 /*
1176 * Take a breather from holding the lock,
1177 * allow pages that finish wake up asynchronously
1178 * to acquire the lock and remove themselves
1179 * from wait queue
1180 */
1181 spin_unlock_irqrestore(&q->lock, flags);
1182 cpu_relax();
1183 spin_lock_irqsave(&q->lock, flags);
1184 __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
1185 }
1186
Nicholas Piggin62906022016-12-25 13:00:30 +10001187 /*
1188 * It is possible for other pages to have collided on the waitqueue
1189 * hash, so in that case check for a page match. That prevents a long-
1190 * term waiter
1191 *
1192 * It is still possible to miss a case here, when we woke page waiters
1193 * and removed them from the waitqueue, but there are still other
1194 * page waiters.
1195 */
1196 if (!waitqueue_active(q) || !key.page_match) {
Matthew Wilcox (Oracle)6974d7c2021-01-15 17:14:48 -05001197 folio_clear_waiters(folio);
Nicholas Piggin62906022016-12-25 13:00:30 +10001198 /*
1199 * It's possible to miss clearing Waiters here, when we woke
1200 * our page waiters, but the hashed waitqueue has waiters for
1201 * other pages on it.
1202 *
1203 * That's okay, it's a rare case. The next waker will clear it.
1204 */
1205 }
1206 spin_unlock_irqrestore(&q->lock, flags);
1207}
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001208
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001209static void folio_wake(struct folio *folio, int bit)
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001210{
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001211 if (!folio_test_waiters(folio))
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001212 return;
Matthew Wilcox (Oracle)6974d7c2021-01-15 17:14:48 -05001213 folio_wake_bit(folio, bit);
Nicholas Piggin74d81bf2017-02-22 15:44:41 -08001214}
Nicholas Piggin62906022016-12-25 13:00:30 +10001215
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001216/*
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001217 * A choice of three behaviors for folio_wait_bit_common():
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001218 */
1219enum behavior {
1220 EXCLUSIVE, /* Hold ref to page and take the bit when woken, like
Matthew Wilcox (Oracle)7c23c782021-03-01 19:38:25 -05001221 * __folio_lock() waiting on then setting PG_locked.
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001222 */
1223 SHARED, /* Hold ref to page and check the bit when woken, like
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001224 * folio_wait_writeback() waiting on PG_writeback.
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001225 */
1226 DROP, /* Drop ref to page before wait, no check when woken,
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001227 * like folio_put_wait_locked() on PG_locked.
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001228 */
1229};
1230
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001231/*
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001232 * Attempt to check (or get) the folio flag, and mark us done
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001233 * if successful.
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001234 */
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001235static inline bool folio_trylock_flag(struct folio *folio, int bit_nr,
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001236 struct wait_queue_entry *wait)
1237{
1238 if (wait->flags & WQ_FLAG_EXCLUSIVE) {
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001239 if (test_and_set_bit(bit_nr, &folio->flags))
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001240 return false;
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001241 } else if (test_bit(bit_nr, &folio->flags))
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001242 return false;
1243
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001244 wait->flags |= WQ_FLAG_WOKEN | WQ_FLAG_DONE;
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001245 return true;
1246}
1247
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001248/* How many times do we accept lock stealing from under a waiter? */
1249int sysctl_page_lock_unfairness = 5;
1250
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001251static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
1252 int state, enum behavior behavior)
Nicholas Piggin62906022016-12-25 13:00:30 +10001253{
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001254 wait_queue_head_t *q = folio_waitqueue(folio);
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001255 int unfairness = sysctl_page_lock_unfairness;
Nicholas Piggin62906022016-12-25 13:00:30 +10001256 struct wait_page_queue wait_page;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001257 wait_queue_entry_t *wait = &wait_page.wait;
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001258 bool thrashing = false;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001259 bool delayacct = false;
Johannes Weinereb414682018-10-26 15:06:27 -07001260 unsigned long pflags;
Nicholas Piggin62906022016-12-25 13:00:30 +10001261
Johannes Weinereb414682018-10-26 15:06:27 -07001262 if (bit_nr == PG_locked &&
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001263 !folio_test_uptodate(folio) && folio_test_workingset(folio)) {
1264 if (!folio_test_swapbacked(folio)) {
Johannes Weinereb414682018-10-26 15:06:27 -07001265 delayacct_thrashing_start();
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001266 delayacct = true;
1267 }
Johannes Weinereb414682018-10-26 15:06:27 -07001268 psi_memstall_enter(&pflags);
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001269 thrashing = true;
1270 }
1271
Nicholas Piggin62906022016-12-25 13:00:30 +10001272 init_wait(wait);
1273 wait->func = wake_page_function;
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001274 wait_page.folio = folio;
Nicholas Piggin62906022016-12-25 13:00:30 +10001275 wait_page.bit_nr = bit_nr;
1276
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001277repeat:
1278 wait->flags = 0;
1279 if (behavior == EXCLUSIVE) {
1280 wait->flags = WQ_FLAG_EXCLUSIVE;
1281 if (--unfairness < 0)
1282 wait->flags |= WQ_FLAG_CUSTOM;
1283 }
1284
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001285 /*
1286 * Do one last check whether we can get the
1287 * page bit synchronously.
1288 *
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001289 * Do the folio_set_waiters() marking before that
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001290 * to let any waker we _just_ missed know they
1291 * need to wake us up (otherwise they'll never
1292 * even go to the slow case that looks at the
1293 * page queue), and add ourselves to the wait
1294 * queue if we need to sleep.
1295 *
1296 * This part needs to be done under the queue
1297 * lock to avoid races.
1298 */
1299 spin_lock_irq(&q->lock);
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001300 folio_set_waiters(folio);
1301 if (!folio_trylock_flag(folio, bit_nr, wait))
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001302 __add_wait_queue_entry_tail(q, wait);
1303 spin_unlock_irq(&q->lock);
1304
1305 /*
1306 * From now on, all the logic will be based on
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001307 * the WQ_FLAG_WOKEN and WQ_FLAG_DONE flag, to
1308 * see whether the page bit testing has already
1309 * been done by the wake function.
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001310 *
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001311 * We can drop our reference to the folio.
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001312 */
1313 if (behavior == DROP)
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001314 folio_put(folio);
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001315
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001316 /*
1317 * Note that until the "finish_wait()", or until
1318 * we see the WQ_FLAG_WOKEN flag, we need to
1319 * be very careful with the 'wait->flags', because
1320 * we may race with a waker that sets them.
1321 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001322 for (;;) {
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001323 unsigned int flags;
1324
Nicholas Piggin62906022016-12-25 13:00:30 +10001325 set_current_state(state);
1326
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001327 /* Loop until we've been woken or interrupted */
1328 flags = smp_load_acquire(&wait->flags);
1329 if (!(flags & WQ_FLAG_WOKEN)) {
1330 if (signal_pending_state(state, current))
1331 break;
1332
1333 io_schedule();
1334 continue;
1335 }
1336
1337 /* If we were non-exclusive, we're done */
1338 if (behavior != EXCLUSIVE)
Linus Torvaldsa8b169a2017-08-27 16:25:09 -07001339 break;
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001340
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001341 /* If the waker got the lock for us, we're done */
1342 if (flags & WQ_FLAG_DONE)
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001343 break;
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001344
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001345 /*
1346 * Otherwise, if we're getting the lock, we need to
1347 * try to get it ourselves.
1348 *
1349 * And if that fails, we'll have to retry this all.
1350 */
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001351 if (unlikely(test_and_set_bit(bit_nr, folio_flags(folio, 0))))
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001352 goto repeat;
1353
1354 wait->flags |= WQ_FLAG_DONE;
1355 break;
Nicholas Piggin62906022016-12-25 13:00:30 +10001356 }
1357
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001358 /*
1359 * If a signal happened, this 'finish_wait()' may remove the last
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001360 * waiter from the wait-queues, but the folio waiters bit will remain
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001361 * set. That's ok. The next wakeup will take care of it, and trying
1362 * to do it here would be difficult and prone to races.
1363 */
Nicholas Piggin62906022016-12-25 13:00:30 +10001364 finish_wait(q, wait);
1365
Johannes Weinereb414682018-10-26 15:06:27 -07001366 if (thrashing) {
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001367 if (delayacct)
Johannes Weinereb414682018-10-26 15:06:27 -07001368 delayacct_thrashing_end();
1369 psi_memstall_leave(&pflags);
1370 }
Johannes Weinerb1d29ba2018-10-26 15:06:08 -07001371
Nicholas Piggin62906022016-12-25 13:00:30 +10001372 /*
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001373 * NOTE! The wait->flags weren't stable until we've done the
1374 * 'finish_wait()', and we could have exited the loop above due
1375 * to a signal, and had a wakeup event happen after the signal
1376 * test but before the 'finish_wait()'.
1377 *
1378 * So only after the finish_wait() can we reliably determine
1379 * if we got woken up or not, so we can now figure out the final
1380 * return value based on that state without races.
1381 *
1382 * Also note that WQ_FLAG_WOKEN is sufficient for a non-exclusive
1383 * waiter, but an exclusive one requires WQ_FLAG_DONE.
Nicholas Piggin62906022016-12-25 13:00:30 +10001384 */
Linus Torvalds5ef64cc2020-09-13 14:05:35 -07001385 if (behavior == EXCLUSIVE)
1386 return wait->flags & WQ_FLAG_DONE ? 0 : -EINTR;
Nicholas Piggin62906022016-12-25 13:00:30 +10001387
Linus Torvalds2a9127f2020-07-23 10:16:49 -07001388 return wait->flags & WQ_FLAG_WOKEN ? 0 : -EINTR;
Nicholas Piggin62906022016-12-25 13:00:30 +10001389}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001391void folio_wait_bit(struct folio *folio, int bit_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001393 folio_wait_bit_common(folio, bit_nr, TASK_UNINTERRUPTIBLE, SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001395EXPORT_SYMBOL(folio_wait_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001397int folio_wait_bit_killable(struct folio *folio, int bit_nr)
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001398{
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001399 return folio_wait_bit_common(folio, bit_nr, TASK_KILLABLE, SHARED);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001400}
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001401EXPORT_SYMBOL(folio_wait_bit_killable);
KOSAKI Motohirof62e00c2011-05-24 17:11:29 -07001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403/**
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001404 * folio_put_wait_locked - Drop a reference and wait for it to be unlocked
1405 * @folio: The folio to wait for.
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -08001406 * @state: The sleep state (TASK_KILLABLE, TASK_UNINTERRUPTIBLE, etc).
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001407 *
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001408 * The caller should hold a reference on @folio. They expect the page to
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001409 * become unlocked relatively soon, but do not wish to hold up migration
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001410 * (for example) by holding the reference while waiting for the folio to
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001411 * come unlocked. After this function returns, the caller should not
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001412 * dereference @folio.
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -08001413 *
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001414 * Return: 0 if the folio was unlocked or -EINTR if interrupted by a signal.
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001415 */
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001416int folio_put_wait_locked(struct folio *folio, int state)
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001417{
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04001418 return folio_wait_bit_common(folio, PG_locked, state, DROP);
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001419}
1420
1421/**
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001422 * folio_add_wait_queue - Add an arbitrary waiter to a folio's wait queue
1423 * @folio: Folio defining the wait queue of interest
Randy Dunlap697f6192009-04-13 14:39:54 -07001424 * @waiter: Waiter to add to the queue
David Howells385e1ca5f2009-04-03 16:42:39 +01001425 *
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001426 * Add an arbitrary @waiter to the wait queue for the nominated @folio.
David Howells385e1ca5f2009-04-03 16:42:39 +01001427 */
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001428void folio_add_wait_queue(struct folio *folio, wait_queue_entry_t *waiter)
David Howells385e1ca5f2009-04-03 16:42:39 +01001429{
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001430 wait_queue_head_t *q = folio_waitqueue(folio);
David Howells385e1ca5f2009-04-03 16:42:39 +01001431 unsigned long flags;
1432
1433 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds9c3a8152017-08-28 16:45:40 -07001434 __add_wait_queue_entry_tail(q, waiter);
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001435 folio_set_waiters(folio);
David Howells385e1ca5f2009-04-03 16:42:39 +01001436 spin_unlock_irqrestore(&q->lock, flags);
1437}
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001438EXPORT_SYMBOL_GPL(folio_add_wait_queue);
David Howells385e1ca5f2009-04-03 16:42:39 +01001439
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001440#ifndef clear_bit_unlock_is_negative_byte
1441
1442/*
1443 * PG_waiters is the high bit in the same byte as PG_lock.
1444 *
1445 * On x86 (and on many other architectures), we can clear PG_lock and
1446 * test the sign bit at the same time. But if the architecture does
1447 * not support that special operation, we just do this all by hand
1448 * instead.
1449 *
1450 * The read of PG_waiters has to be after (or concurrently with) PG_locked
Ethon Paulffceeb62020-06-04 16:49:22 -07001451 * being cleared, but a memory barrier should be unnecessary since it is
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001452 * in the same byte as PG_locked.
1453 */
1454static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
1455{
1456 clear_bit_unlock(nr, mem);
1457 /* smp_mb__after_atomic(); */
Olof Johansson98473f92016-12-29 14:16:07 -08001458 return test_bit(PG_waiters, mem);
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001459}
1460
1461#endif
1462
David Howells385e1ca5f2009-04-03 16:42:39 +01001463/**
Matthew Wilcox (Oracle)4e136422020-12-07 15:44:35 -05001464 * folio_unlock - Unlock a locked folio.
1465 * @folio: The folio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 *
Matthew Wilcox (Oracle)4e136422020-12-07 15:44:35 -05001467 * Unlocks the folio and wakes up any thread sleeping on the page lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 *
Matthew Wilcox (Oracle)4e136422020-12-07 15:44:35 -05001469 * Context: May be called from interrupt or process context. May not be
1470 * called from NMI context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 */
Matthew Wilcox (Oracle)4e136422020-12-07 15:44:35 -05001472void folio_unlock(struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Matthew Wilcox (Oracle)4e136422020-12-07 15:44:35 -05001474 /* Bit 7 allows x86 to check the byte's sign bit */
Linus Torvaldsb91e1302016-12-27 11:40:38 -08001475 BUILD_BUG_ON(PG_waiters != 7);
Matthew Wilcox (Oracle)4e136422020-12-07 15:44:35 -05001476 BUILD_BUG_ON(PG_locked > 7);
1477 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
1478 if (clear_bit_unlock_is_negative_byte(PG_locked, folio_flags(folio, 0)))
Matthew Wilcox (Oracle)6974d7c2021-01-15 17:14:48 -05001479 folio_wake_bit(folio, PG_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
Matthew Wilcox (Oracle)4e136422020-12-07 15:44:35 -05001481EXPORT_SYMBOL(folio_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Randy Dunlap485bb992006-06-23 02:03:49 -07001483/**
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001484 * folio_end_private_2 - Clear PG_private_2 and wake any waiters.
1485 * @folio: The folio.
David Howells73e10de2020-02-10 10:00:21 +00001486 *
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001487 * Clear the PG_private_2 bit on a folio and wake up any sleepers waiting for
1488 * it. The folio reference held for PG_private_2 being set is released.
David Howells73e10de2020-02-10 10:00:21 +00001489 *
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001490 * This is, for example, used when a netfs folio is being written to a local
1491 * disk cache, thereby allowing writes to the cache for the same folio to be
David Howells73e10de2020-02-10 10:00:21 +00001492 * serialised.
1493 */
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001494void folio_end_private_2(struct folio *folio)
David Howells73e10de2020-02-10 10:00:21 +00001495{
Matthew Wilcox (Oracle)6974d7c2021-01-15 17:14:48 -05001496 VM_BUG_ON_FOLIO(!folio_test_private_2(folio), folio);
1497 clear_bit_unlock(PG_private_2, folio_flags(folio, 0));
1498 folio_wake_bit(folio, PG_private_2);
1499 folio_put(folio);
David Howells73e10de2020-02-10 10:00:21 +00001500}
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001501EXPORT_SYMBOL(folio_end_private_2);
David Howells73e10de2020-02-10 10:00:21 +00001502
1503/**
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001504 * folio_wait_private_2 - Wait for PG_private_2 to be cleared on a folio.
1505 * @folio: The folio to wait on.
David Howells73e10de2020-02-10 10:00:21 +00001506 *
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001507 * Wait for PG_private_2 (aka PG_fscache) to be cleared on a folio.
David Howells73e10de2020-02-10 10:00:21 +00001508 */
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001509void folio_wait_private_2(struct folio *folio)
David Howells73e10de2020-02-10 10:00:21 +00001510{
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001511 while (folio_test_private_2(folio))
1512 folio_wait_bit(folio, PG_private_2);
David Howells73e10de2020-02-10 10:00:21 +00001513}
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001514EXPORT_SYMBOL(folio_wait_private_2);
David Howells73e10de2020-02-10 10:00:21 +00001515
1516/**
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001517 * folio_wait_private_2_killable - Wait for PG_private_2 to be cleared on a folio.
1518 * @folio: The folio to wait on.
David Howells73e10de2020-02-10 10:00:21 +00001519 *
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001520 * Wait for PG_private_2 (aka PG_fscache) to be cleared on a folio or until a
David Howells73e10de2020-02-10 10:00:21 +00001521 * fatal signal is received by the calling task.
1522 *
1523 * Return:
1524 * - 0 if successful.
1525 * - -EINTR if a fatal signal was encountered.
1526 */
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001527int folio_wait_private_2_killable(struct folio *folio)
David Howells73e10de2020-02-10 10:00:21 +00001528{
1529 int ret = 0;
1530
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001531 while (folio_test_private_2(folio)) {
1532 ret = folio_wait_bit_killable(folio, PG_private_2);
David Howells73e10de2020-02-10 10:00:21 +00001533 if (ret < 0)
1534 break;
1535 }
1536
1537 return ret;
1538}
Matthew Wilcox (Oracle)b47393f2021-04-22 22:58:32 -04001539EXPORT_SYMBOL(folio_wait_private_2_killable);
David Howells73e10de2020-02-10 10:00:21 +00001540
1541/**
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001542 * folio_end_writeback - End writeback against a folio.
1543 * @folio: The folio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 */
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001545void folio_end_writeback(struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Mel Gorman888cf2d2014-06-04 16:10:34 -07001547 /*
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001548 * folio_test_clear_reclaim() could be used here but it is an
1549 * atomic operation and overkill in this particular case. Failing
1550 * to shuffle a folio marked for immediate reclaim is too mild
1551 * a gain to justify taking an atomic operation penalty at the
1552 * end of every folio writeback.
Mel Gorman888cf2d2014-06-04 16:10:34 -07001553 */
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001554 if (folio_test_reclaim(folio)) {
1555 folio_clear_reclaim(folio);
Matthew Wilcox (Oracle)575ced12020-12-08 01:25:39 -05001556 folio_rotate_reclaimable(folio);
Mel Gorman888cf2d2014-06-04 16:10:34 -07001557 }
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001558
Hugh Dickins073861e2020-11-24 08:46:43 -08001559 /*
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001560 * Writeback does not hold a folio reference of its own, relying
Hugh Dickins073861e2020-11-24 08:46:43 -08001561 * on truncation to wait for the clearing of PG_writeback.
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001562 * But here we must make sure that the folio is not freed and
1563 * reused before the folio_wake().
Hugh Dickins073861e2020-11-24 08:46:43 -08001564 */
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001565 folio_get(folio);
Matthew Wilcox (Oracle)269ccca32021-01-15 23:34:16 -05001566 if (!__folio_end_writeback(folio))
Miklos Szerediac6aadb2008-04-28 02:12:38 -07001567 BUG();
1568
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001569 smp_mb__after_atomic();
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001570 folio_wake(folio, PG_writeback);
Linus Torvalds512b79312021-11-06 14:08:17 -07001571 acct_reclaim_writeback(folio);
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001572 folio_put(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
Matthew Wilcox (Oracle)4268b482021-03-03 15:21:55 -05001574EXPORT_SYMBOL(folio_end_writeback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Matthew Wilcox57d99842014-06-04 16:07:45 -07001576/*
1577 * After completing I/O on a page, call this routine to update the page
1578 * flags appropriately
1579 */
Jens Axboec11f0c02016-08-05 08:11:04 -06001580void page_endio(struct page *page, bool is_write, int err)
Matthew Wilcox57d99842014-06-04 16:07:45 -07001581{
Jens Axboec11f0c02016-08-05 08:11:04 -06001582 if (!is_write) {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001583 if (!err) {
1584 SetPageUptodate(page);
1585 } else {
1586 ClearPageUptodate(page);
1587 SetPageError(page);
1588 }
1589 unlock_page(page);
Mike Christieabf54542016-08-04 14:23:34 -06001590 } else {
Matthew Wilcox57d99842014-06-04 16:07:45 -07001591 if (err) {
Minchan Kimdd8416c2017-02-24 14:59:59 -08001592 struct address_space *mapping;
1593
Matthew Wilcox57d99842014-06-04 16:07:45 -07001594 SetPageError(page);
Minchan Kimdd8416c2017-02-24 14:59:59 -08001595 mapping = page_mapping(page);
1596 if (mapping)
1597 mapping_set_error(mapping, err);
Matthew Wilcox57d99842014-06-04 16:07:45 -07001598 }
1599 end_page_writeback(page);
1600 }
1601}
1602EXPORT_SYMBOL_GPL(page_endio);
1603
Randy Dunlap485bb992006-06-23 02:03:49 -07001604/**
Matthew Wilcox (Oracle)7c23c782021-03-01 19:38:25 -05001605 * __folio_lock - Get a lock on the folio, assuming we need to sleep to get it.
1606 * @folio: The folio to lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 */
Matthew Wilcox (Oracle)7c23c782021-03-01 19:38:25 -05001608void __folio_lock(struct folio *folio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001610 folio_wait_bit_common(folio, PG_locked, TASK_UNINTERRUPTIBLE,
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001611 EXCLUSIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
Matthew Wilcox (Oracle)7c23c782021-03-01 19:38:25 -05001613EXPORT_SYMBOL(__folio_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Matthew Wilcox (Oracle)af7f29d2020-12-08 00:07:31 -05001615int __folio_lock_killable(struct folio *folio)
Matthew Wilcox2687a352007-12-06 11:18:49 -05001616{
Matthew Wilcox (Oracle)101c0bf2021-03-04 12:02:54 -05001617 return folio_wait_bit_common(folio, PG_locked, TASK_KILLABLE,
Hugh Dickins9a1ea432018-12-28 00:36:14 -08001618 EXCLUSIVE);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001619}
Matthew Wilcox (Oracle)af7f29d2020-12-08 00:07:31 -05001620EXPORT_SYMBOL_GPL(__folio_lock_killable);
Matthew Wilcox2687a352007-12-06 11:18:49 -05001621
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05001622static int __folio_lock_async(struct folio *folio, struct wait_page_queue *wait)
Jens Axboedd3e6d52020-05-22 09:12:09 -06001623{
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001624 struct wait_queue_head *q = folio_waitqueue(folio);
Matthew Wilcox (Oracle)f32b5dd2021-02-24 12:02:09 -08001625 int ret = 0;
1626
Matthew Wilcox (Oracle)df4d4f122021-01-16 11:22:14 -05001627 wait->folio = folio;
Matthew Wilcox (Oracle)f32b5dd2021-02-24 12:02:09 -08001628 wait->bit_nr = PG_locked;
1629
1630 spin_lock_irq(&q->lock);
1631 __add_wait_queue_entry_tail(q, &wait->wait);
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05001632 folio_set_waiters(folio);
1633 ret = !folio_trylock(folio);
Matthew Wilcox (Oracle)f32b5dd2021-02-24 12:02:09 -08001634 /*
1635 * If we were successful now, we know we're still on the
1636 * waitqueue as we're still under the lock. This means it's
1637 * safe to remove and return success, we know the callback
1638 * isn't going to trigger.
1639 */
1640 if (!ret)
1641 __remove_wait_queue(q, &wait->wait);
1642 else
1643 ret = -EIOCBQUEUED;
1644 spin_unlock_irq(&q->lock);
1645 return ret;
Jens Axboedd3e6d52020-05-22 09:12:09 -06001646}
1647
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001648/*
1649 * Return values:
Matthew Wilcox (Oracle)9138e472021-03-18 21:39:45 -04001650 * true - folio is locked; mmap_lock is still held.
1651 * false - folio is not locked.
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001652 * mmap_lock has been released (mmap_read_unlock(), unless flags had both
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001653 * FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001654 * which case mmap_lock is still held.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001655 *
Matthew Wilcox (Oracle)9138e472021-03-18 21:39:45 -04001656 * If neither ALLOW_RETRY nor KILLABLE are set, will always return true
1657 * with the folio locked and the mmap_lock unperturbed.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07001658 */
Matthew Wilcox (Oracle)9138e472021-03-18 21:39:45 -04001659bool __folio_lock_or_retry(struct folio *folio, struct mm_struct *mm,
Michel Lespinassed065bd82010-10-26 14:21:57 -07001660 unsigned int flags)
1661{
Peter Xu4064b982020-04-01 21:08:45 -07001662 if (fault_flag_allow_retry_first(flags)) {
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001663 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001664 * CAUTION! In this case, mmap_lock is not released
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001665 * even though return 0.
1666 */
1667 if (flags & FAULT_FLAG_RETRY_NOWAIT)
Matthew Wilcox (Oracle)9138e472021-03-18 21:39:45 -04001668 return false;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001669
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001670 mmap_read_unlock(mm);
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001671 if (flags & FAULT_FLAG_KILLABLE)
Matthew Wilcox (Oracle)6baa8d62021-03-04 10:21:02 -05001672 folio_wait_locked_killable(folio);
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001673 else
Matthew Wilcox (Oracle)6baa8d62021-03-04 10:21:02 -05001674 folio_wait_locked(folio);
Matthew Wilcox (Oracle)9138e472021-03-18 21:39:45 -04001675 return false;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001676 }
Hailong Liu800bca72020-12-14 19:05:02 -08001677 if (flags & FAULT_FLAG_KILLABLE) {
Matthew Wilcox (Oracle)9138e472021-03-18 21:39:45 -04001678 bool ret;
Hailong Liu800bca72020-12-14 19:05:02 -08001679
Matthew Wilcox (Oracle)af7f29d2020-12-08 00:07:31 -05001680 ret = __folio_lock_killable(folio);
Hailong Liu800bca72020-12-14 19:05:02 -08001681 if (ret) {
1682 mmap_read_unlock(mm);
Matthew Wilcox (Oracle)9138e472021-03-18 21:39:45 -04001683 return false;
Hailong Liu800bca72020-12-14 19:05:02 -08001684 }
1685 } else {
Matthew Wilcox (Oracle)af7f29d2020-12-08 00:07:31 -05001686 __folio_lock(folio);
Hailong Liu800bca72020-12-14 19:05:02 -08001687 }
Hailong Liu800bca72020-12-14 19:05:02 -08001688
Matthew Wilcox (Oracle)9138e472021-03-18 21:39:45 -04001689 return true;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001690}
1691
Randy Dunlap485bb992006-06-23 02:03:49 -07001692/**
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001693 * page_cache_next_miss() - Find the next gap in the page cache.
1694 * @mapping: Mapping.
1695 * @index: Index.
1696 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001697 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001698 * Search the range [index, min(index + max_scan - 1, ULONG_MAX)] for the
1699 * gap with the lowest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001700 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001701 * This function may be called under the rcu_read_lock. However, this will
1702 * not atomically search a snapshot of the cache at a single point in time.
1703 * For example, if a gap is created at index 5, then subsequently a gap is
1704 * created at index 10, page_cache_next_miss covering both indices may
1705 * return 10 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001706 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001707 * Return: The index of the gap if found, otherwise an index outside the
1708 * range specified (in which case 'return - index >= max_scan' will be true).
1709 * In the rare case of index wrap-around, 0 will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001710 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001711pgoff_t page_cache_next_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001712 pgoff_t index, unsigned long max_scan)
1713{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001714 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001715
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001716 while (max_scan--) {
1717 void *entry = xas_next(&xas);
1718 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001719 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001720 if (xas.xa_index == 0)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001721 break;
1722 }
1723
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001724 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001725}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001726EXPORT_SYMBOL(page_cache_next_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001727
1728/**
Laurent Dufour2346a562019-05-13 17:21:29 -07001729 * page_cache_prev_miss() - Find the previous gap in the page cache.
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001730 * @mapping: Mapping.
1731 * @index: Index.
1732 * @max_scan: Maximum range to search.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001733 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001734 * Search the range [max(index - max_scan + 1, 0), index] for the
1735 * gap with the highest index.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001736 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001737 * This function may be called under the rcu_read_lock. However, this will
1738 * not atomically search a snapshot of the cache at a single point in time.
1739 * For example, if a gap is created at index 10, then subsequently a gap is
1740 * created at index 5, page_cache_prev_miss() covering both indices may
1741 * return 5 if called under the rcu_read_lock.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001742 *
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001743 * Return: The index of the gap if found, otherwise an index outside the
1744 * range specified (in which case 'index - return >= max_scan' will be true).
1745 * In the rare case of wrap-around, ULONG_MAX will be returned.
Johannes Weinere7b563b2014-04-03 14:47:44 -07001746 */
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001747pgoff_t page_cache_prev_miss(struct address_space *mapping,
Johannes Weinere7b563b2014-04-03 14:47:44 -07001748 pgoff_t index, unsigned long max_scan)
1749{
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001750 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001751
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001752 while (max_scan--) {
1753 void *entry = xas_prev(&xas);
1754 if (!entry || xa_is_value(entry))
Johannes Weinere7b563b2014-04-03 14:47:44 -07001755 break;
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001756 if (xas.xa_index == ULONG_MAX)
Johannes Weinere7b563b2014-04-03 14:47:44 -07001757 break;
1758 }
1759
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001760 return xas.xa_index;
Johannes Weinere7b563b2014-04-03 14:47:44 -07001761}
Matthew Wilcox0d3f9292017-11-21 14:07:06 -05001762EXPORT_SYMBOL(page_cache_prev_miss);
Johannes Weinere7b563b2014-04-03 14:47:44 -07001763
Matthew Wilcox (Oracle)44835d22021-02-25 17:15:36 -08001764/*
Matthew Wilcox (Oracle)020853b2021-05-10 16:33:22 -04001765 * Lockless page cache protocol:
1766 * On the lookup side:
1767 * 1. Load the folio from i_pages
1768 * 2. Increment the refcount if it's not zero
1769 * 3. If the folio is not found by xas_reload(), put the refcount and retry
1770 *
1771 * On the removal side:
1772 * A. Freeze the page (by zeroing the refcount if nobody else has a reference)
1773 * B. Remove the page from i_pages
1774 * C. Return the page to the page allocator
1775 *
1776 * This means that any page may have its reference count temporarily
1777 * increased by a speculative page cache (or fast GUP) lookup as it can
1778 * be allocated by another user before the RCU grace period expires.
1779 * Because the refcount temporarily acquired here may end up being the
1780 * last refcount on the page, any page allocation must be freeable by
1781 * folio_put().
1782 */
1783
1784/*
Matthew Wilcox (Oracle)bc5a3012021-02-25 17:15:40 -08001785 * mapping_get_entry - Get a page cache entry.
Randy Dunlap485bb992006-06-23 02:03:49 -07001786 * @mapping: the address_space to search
Matthew Wilcox (Oracle)a6de4b42020-10-13 16:51:34 -07001787 * @index: The page cache index.
Randy Dunlap485bb992006-06-23 02:03:49 -07001788 *
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001789 * Looks up the page cache entry at @mapping & @index. If it is a folio,
1790 * it is returned with an increased refcount. If it is a shadow entry
1791 * of a previously evicted folio, or a swap entry from shmem/tmpfs,
1792 * it is returned without further action.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001793 *
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001794 * Return: The folio, swap or shadow entry, %NULL if nothing is found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 */
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001796static void *mapping_get_entry(struct address_space *mapping, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
Matthew Wilcox (Oracle)a6de4b42020-10-13 16:51:34 -07001798 XA_STATE(xas, &mapping->i_pages, index);
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001799 struct folio *folio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Nick Piggina60637c2008-07-25 19:45:31 -07001801 rcu_read_lock();
1802repeat:
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001803 xas_reset(&xas);
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001804 folio = xas_load(&xas);
1805 if (xas_retry(&xas, folio))
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001806 goto repeat;
1807 /*
1808 * A shadow entry of a recently evicted page, or a swap entry from
1809 * shmem/tmpfs. Return it without attempting to raise page count.
1810 */
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001811 if (!folio || xa_is_value(folio))
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001812 goto out;
Kirill A. Shutemov83929372016-07-26 15:26:04 -07001813
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001814 if (!folio_try_get_rcu(folio))
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001815 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001816
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001817 if (unlikely(folio != xas_reload(&xas))) {
1818 folio_put(folio);
Matthew Wilcox4c7472c2018-05-16 16:12:50 -04001819 goto repeat;
Nick Piggina60637c2008-07-25 19:45:31 -07001820 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001821out:
Nick Piggina60637c2008-07-25 19:45:31 -07001822 rcu_read_unlock();
1823
Matthew Wilcox (Oracle)bca65ee2020-12-15 23:22:38 -05001824 return folio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Randy Dunlap485bb992006-06-23 02:03:49 -07001827/**
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001828 * __filemap_get_folio - Find and get a reference to a folio.
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001829 * @mapping: The address_space to search.
1830 * @index: The page index.
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001831 * @fgp_flags: %FGP flags modify how the folio is returned.
1832 * @gfp: Memory allocation flags to use if %FGP_CREAT is specified.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001833 *
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001834 * Looks up the page cache entry at @mapping & @index.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001835 *
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001836 * @fgp_flags can be zero or more of these flags:
Johannes Weiner0cd61442014-04-03 14:47:46 -07001837 *
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001838 * * %FGP_ACCESSED - The folio will be marked accessed.
1839 * * %FGP_LOCK - The folio is returned locked.
Matthew Wilcox (Oracle)44835d22021-02-25 17:15:36 -08001840 * * %FGP_ENTRY - If there is a shadow / swap / DAX entry, return it
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001841 * instead of allocating a new folio to replace it.
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001842 * * %FGP_CREAT - If no page is present then a new page is allocated using
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001843 * @gfp and added to the page cache and the VM's LRU list.
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001844 * The page is returned locked and with an increased refcount.
1845 * * %FGP_FOR_MMAP - The caller wants to do its own locking dance if the
1846 * page is already in cache. If the page was allocated, unlock it before
1847 * returning so the caller can do the same dance.
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001848 * * %FGP_WRITE - The page will be written to by the caller.
1849 * * %FGP_NOFS - __GFP_FS will get cleared in gfp.
1850 * * %FGP_NOWAIT - Don't get blocked by page lock.
Matthew Wilcox (Oracle)b27652d92020-12-24 12:55:56 -05001851 * * %FGP_STABLE - Wait for the folio to be stable (finished writeback)
mchehab@s-opensource.com0e056eb2017-03-30 17:11:36 -03001852 *
Matthew Wilcox (Oracle)2294b322020-04-01 21:05:07 -07001853 * If %FGP_LOCK or %FGP_CREAT are specified then the function may sleep even
1854 * if the %GFP flags specified for %FGP_CREAT are atomic.
Mel Gorman2457aec2014-06-04 16:10:31 -07001855 *
1856 * If there is a page cache page, it is returned with an increased refcount.
Mike Rapoporta862f682019-03-05 15:48:42 -08001857 *
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001858 * Return: The found folio or %NULL otherwise.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001859 */
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001860struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
1861 int fgp_flags, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001863 struct folio *folio;
Mel Gorman2457aec2014-06-04 16:10:31 -07001864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865repeat:
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001866 folio = mapping_get_entry(mapping, index);
1867 if (xa_is_value(folio)) {
Matthew Wilcox (Oracle)44835d22021-02-25 17:15:36 -08001868 if (fgp_flags & FGP_ENTRY)
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001869 return folio;
1870 folio = NULL;
Matthew Wilcox (Oracle)44835d22021-02-25 17:15:36 -08001871 }
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001872 if (!folio)
Mel Gorman2457aec2014-06-04 16:10:31 -07001873 goto no_page;
1874
1875 if (fgp_flags & FGP_LOCK) {
1876 if (fgp_flags & FGP_NOWAIT) {
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001877 if (!folio_trylock(folio)) {
1878 folio_put(folio);
Mel Gorman2457aec2014-06-04 16:10:31 -07001879 return NULL;
1880 }
1881 } else {
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001882 folio_lock(folio);
Mel Gorman2457aec2014-06-04 16:10:31 -07001883 }
1884
1885 /* Has the page been truncated? */
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001886 if (unlikely(folio->mapping != mapping)) {
1887 folio_unlock(folio);
1888 folio_put(folio);
Mel Gorman2457aec2014-06-04 16:10:31 -07001889 goto repeat;
1890 }
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001891 VM_BUG_ON_FOLIO(!folio_contains(folio, index), folio);
Mel Gorman2457aec2014-06-04 16:10:31 -07001892 }
1893
Kirill Tkhaic16eb002018-12-28 00:37:35 -08001894 if (fgp_flags & FGP_ACCESSED)
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001895 folio_mark_accessed(folio);
Yang Shib9306a72020-08-06 23:19:55 -07001896 else if (fgp_flags & FGP_WRITE) {
1897 /* Clear idle flag for buffer write */
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001898 if (folio_test_idle(folio))
1899 folio_clear_idle(folio);
Yang Shib9306a72020-08-06 23:19:55 -07001900 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001901
Matthew Wilcox (Oracle)b27652d92020-12-24 12:55:56 -05001902 if (fgp_flags & FGP_STABLE)
1903 folio_wait_stable(folio);
Mel Gorman2457aec2014-06-04 16:10:31 -07001904no_page:
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001905 if (!folio && (fgp_flags & FGP_CREAT)) {
Mel Gorman2457aec2014-06-04 16:10:31 -07001906 int err;
Christoph Hellwigf56753a2020-09-24 08:51:40 +02001907 if ((fgp_flags & FGP_WRITE) && mapping_can_writeback(mapping))
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001908 gfp |= __GFP_WRITE;
Michal Hocko45f87de2014-12-29 20:30:35 +01001909 if (fgp_flags & FGP_NOFS)
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001910 gfp &= ~__GFP_FS;
Mel Gorman2457aec2014-06-04 16:10:31 -07001911
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001912 folio = filemap_alloc_folio(gfp, 0);
1913 if (!folio)
Nick Piggineb2be182007-10-16 01:24:57 -07001914 return NULL;
Mel Gorman2457aec2014-06-04 16:10:31 -07001915
Josef Bacika75d4c32019-03-13 11:44:14 -07001916 if (WARN_ON_ONCE(!(fgp_flags & (FGP_LOCK | FGP_FOR_MMAP))))
Mel Gorman2457aec2014-06-04 16:10:31 -07001917 fgp_flags |= FGP_LOCK;
1918
Hugh Dickinseb39d612014-08-06 16:06:43 -07001919 /* Init accessed so avoid atomic mark_page_accessed later */
Mel Gorman2457aec2014-06-04 16:10:31 -07001920 if (fgp_flags & FGP_ACCESSED)
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001921 __folio_set_referenced(folio);
Mel Gorman2457aec2014-06-04 16:10:31 -07001922
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001923 err = filemap_add_folio(mapping, folio, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07001924 if (unlikely(err)) {
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001925 folio_put(folio);
1926 folio = NULL;
Nick Piggineb2be182007-10-16 01:24:57 -07001927 if (err == -EEXIST)
1928 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
Josef Bacika75d4c32019-03-13 11:44:14 -07001930
1931 /*
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001932 * filemap_add_folio locks the page, and for mmap
1933 * we expect an unlocked page.
Josef Bacika75d4c32019-03-13 11:44:14 -07001934 */
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001935 if (folio && (fgp_flags & FGP_FOR_MMAP))
1936 folio_unlock(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001938
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001939 return folio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
Matthew Wilcox (Oracle)3f0c6a02021-03-08 11:45:35 -05001941EXPORT_SYMBOL(__filemap_get_folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05001943static inline struct folio *find_get_entry(struct xa_state *xas, pgoff_t max,
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08001944 xa_mark_t mark)
1945{
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05001946 struct folio *folio;
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08001947
1948retry:
1949 if (mark == XA_PRESENT)
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05001950 folio = xas_find(xas, max);
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08001951 else
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05001952 folio = xas_find_marked(xas, max, mark);
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08001953
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05001954 if (xas_retry(xas, folio))
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08001955 goto retry;
1956 /*
1957 * A shadow entry of a recently evicted page, a swap
1958 * entry from shmem/tmpfs or a DAX entry. Return it
1959 * without attempting to raise page count.
1960 */
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05001961 if (!folio || xa_is_value(folio))
1962 return folio;
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08001963
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05001964 if (!folio_try_get_rcu(folio))
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08001965 goto reset;
1966
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05001967 if (unlikely(folio != xas_reload(xas))) {
1968 folio_put(folio);
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08001969 goto reset;
1970 }
1971
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05001972 return folio;
Matthew Wilcox (Oracle)c7bad632021-02-25 17:15:44 -08001973reset:
1974 xas_reset(xas);
1975 goto retry;
1976}
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978/**
Johannes Weiner0cd61442014-04-03 14:47:46 -07001979 * find_get_entries - gang pagecache lookup
1980 * @mapping: The address_space to search
1981 * @start: The starting page cache index
Matthew Wilcox (Oracle)ca122fe42021-02-25 17:16:00 -08001982 * @end: The final page index (inclusive).
Matthew Wilcox (Oracle)0e499ed2020-09-01 23:17:50 -04001983 * @fbatch: Where the resulting entries are placed.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001984 * @indices: The cache indices corresponding to the entries in @entries
1985 *
Matthew Wilcox (Oracle)cf2039a2021-02-25 17:16:11 -08001986 * find_get_entries() will search for and return a batch of entries in
Matthew Wilcox (Oracle)0e499ed2020-09-01 23:17:50 -04001987 * the mapping. The entries are placed in @fbatch. find_get_entries()
1988 * takes a reference on any actual folios it returns.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001989 *
Matthew Wilcox (Oracle)0e499ed2020-09-01 23:17:50 -04001990 * The entries have ascending indexes. The indices may not be consecutive
1991 * due to not-present entries or large folios.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001992 *
Matthew Wilcox (Oracle)0e499ed2020-09-01 23:17:50 -04001993 * Any shadow entries of evicted folios, or swap entries from
Johannes Weiner139b6a62014-05-06 12:50:05 -07001994 * shmem/tmpfs, are included in the returned array.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001995 *
Matthew Wilcox (Oracle)0e499ed2020-09-01 23:17:50 -04001996 * Return: The number of entries which were found.
Johannes Weiner0cd61442014-04-03 14:47:46 -07001997 */
Matthew Wilcox (Oracle)ca122fe42021-02-25 17:16:00 -08001998unsigned find_get_entries(struct address_space *mapping, pgoff_t start,
Matthew Wilcox (Oracle)0e499ed2020-09-01 23:17:50 -04001999 pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices)
Johannes Weiner0cd61442014-04-03 14:47:46 -07002000{
Matthew Wilcoxf280bf02018-05-16 17:20:45 -04002001 XA_STATE(xas, &mapping->i_pages, start);
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002002 struct folio *folio;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002003
2004 rcu_read_lock();
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002005 while ((folio = find_get_entry(&xas, end, XA_PRESENT)) != NULL) {
Matthew Wilcox (Oracle)0e499ed2020-09-01 23:17:50 -04002006 indices[fbatch->nr] = xas.xa_index;
2007 if (!folio_batch_add(fbatch, folio))
Johannes Weiner0cd61442014-04-03 14:47:46 -07002008 break;
2009 }
2010 rcu_read_unlock();
Matthew Wilcox (Oracle)cf2039a2021-02-25 17:16:11 -08002011
Matthew Wilcox (Oracle)0e499ed2020-09-01 23:17:50 -04002012 return folio_batch_count(fbatch);
Johannes Weiner0cd61442014-04-03 14:47:46 -07002013}
2014
2015/**
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002016 * find_lock_entries - Find a batch of pagecache entries.
2017 * @mapping: The address_space to search.
2018 * @start: The starting page cache index.
2019 * @end: The final page index (inclusive).
Matthew Wilcox (Oracle)51dcbda2021-12-07 14:15:07 -05002020 * @fbatch: Where the resulting entries are placed.
2021 * @indices: The cache indices of the entries in @fbatch.
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002022 *
2023 * find_lock_entries() will return a batch of entries from @mapping.
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002024 * Swap, shadow and DAX entries are included. Folios are returned
2025 * locked and with an incremented refcount. Folios which are locked
2026 * by somebody else or under writeback are skipped. Folios which are
2027 * partially outside the range are not returned.
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002028 *
2029 * The entries have ascending indexes. The indices may not be consecutive
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002030 * due to not-present entries, large folios, folios which could not be
2031 * locked or folios under writeback.
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002032 *
2033 * Return: The number of entries which were found.
2034 */
2035unsigned find_lock_entries(struct address_space *mapping, pgoff_t start,
Matthew Wilcox (Oracle)51dcbda2021-12-07 14:15:07 -05002036 pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices)
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002037{
2038 XA_STATE(xas, &mapping->i_pages, start);
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002039 struct folio *folio;
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002040
2041 rcu_read_lock();
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002042 while ((folio = find_get_entry(&xas, end, XA_PRESENT))) {
2043 if (!xa_is_value(folio)) {
2044 if (folio->index < start)
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002045 goto put;
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002046 if (folio->index + folio_nr_pages(folio) - 1 > end)
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002047 goto put;
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002048 if (!folio_trylock(folio))
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002049 goto put;
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002050 if (folio->mapping != mapping ||
2051 folio_test_writeback(folio))
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002052 goto unlock;
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002053 VM_BUG_ON_FOLIO(!folio_contains(folio, xas.xa_index),
2054 folio);
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002055 }
Matthew Wilcox (Oracle)51dcbda2021-12-07 14:15:07 -05002056 indices[fbatch->nr] = xas.xa_index;
2057 if (!folio_batch_add(fbatch, folio))
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002058 break;
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -04002059 continue;
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002060unlock:
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002061 folio_unlock(folio);
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002062put:
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002063 folio_put(folio);
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002064 }
2065 rcu_read_unlock();
2066
Matthew Wilcox (Oracle)51dcbda2021-12-07 14:15:07 -05002067 return folio_batch_count(fbatch);
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002068}
2069
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -04002070static inline
2071bool folio_more_pages(struct folio *folio, pgoff_t index, pgoff_t max)
2072{
2073 if (!folio_test_large(folio) || folio_test_hugetlb(folio))
2074 return false;
2075 if (index >= max)
2076 return false;
2077 return index < folio->index + folio_nr_pages(folio) - 1;
Matthew Wilcox (Oracle)5c211ba2021-02-25 17:15:56 -08002078}
2079
2080/**
Jan Karab947cee2017-09-06 16:21:21 -07002081 * find_get_pages_range - gang pagecache lookup
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 * @mapping: The address_space to search
2083 * @start: The starting page index
Jan Karab947cee2017-09-06 16:21:21 -07002084 * @end: The final page index (inclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 * @nr_pages: The maximum number of pages
2086 * @pages: Where the resulting pages are placed
2087 *
Jan Karab947cee2017-09-06 16:21:21 -07002088 * find_get_pages_range() will search for and return a group of up to @nr_pages
2089 * pages in the mapping starting at index @start and up to index @end
2090 * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
2091 * a reference against the returned pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 *
2093 * The search returns a group of mapping-contiguous pages with ascending
2094 * indexes. There may be holes in the indices due to not-present pages.
Jan Karad72dc8a2017-09-06 16:21:18 -07002095 * We also update @start to index the next page for the traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 *
Mike Rapoporta862f682019-03-05 15:48:42 -08002097 * Return: the number of pages which were found. If this number is
2098 * smaller than @nr_pages, the end of specified range has been
Jan Karab947cee2017-09-06 16:21:21 -07002099 * reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 */
Jan Karab947cee2017-09-06 16:21:21 -07002101unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
2102 pgoff_t end, unsigned int nr_pages,
2103 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04002105 XA_STATE(xas, &mapping->i_pages, *start);
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002106 struct folio *folio;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07002107 unsigned ret = 0;
2108
2109 if (unlikely(!nr_pages))
2110 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Nick Piggina60637c2008-07-25 19:45:31 -07002112 rcu_read_lock();
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002113 while ((folio = find_get_entry(&xas, end, XA_PRESENT))) {
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04002114 /* Skip over shadow, swap and DAX entries */
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002115 if (xa_is_value(folio))
Hugh Dickins8079b1c2011-08-03 16:21:28 -07002116 continue;
Nick Piggina60637c2008-07-25 19:45:31 -07002117
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -04002118again:
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002119 pages[ret] = folio_file_page(folio, xas.xa_index);
Jan Karab947cee2017-09-06 16:21:21 -07002120 if (++ret == nr_pages) {
Yu Zhao5d3ee422019-03-05 15:49:17 -08002121 *start = xas.xa_index + 1;
Jan Karab947cee2017-09-06 16:21:21 -07002122 goto out;
2123 }
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -04002124 if (folio_more_pages(folio, xas.xa_index, end)) {
2125 xas.xa_index++;
2126 folio_ref_inc(folio);
2127 goto again;
2128 }
Nick Piggina60637c2008-07-25 19:45:31 -07002129 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07002130
Jan Karab947cee2017-09-06 16:21:21 -07002131 /*
2132 * We come here when there is no page beyond @end. We take care to not
2133 * overflow the index @start as it confuses some of the callers. This
Matthew Wilcoxfd1b3ce2018-05-16 17:38:56 -04002134 * breaks the iteration when there is a page at index -1 but that is
Jan Karab947cee2017-09-06 16:21:21 -07002135 * already broken anyway.
2136 */
2137 if (end == (pgoff_t)-1)
2138 *start = (pgoff_t)-1;
2139 else
2140 *start = end + 1;
2141out:
Nick Piggina60637c2008-07-25 19:45:31 -07002142 rcu_read_unlock();
Jan Karad72dc8a2017-09-06 16:21:18 -07002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return ret;
2145}
2146
Jens Axboeebf43502006-04-27 08:46:01 +02002147/**
2148 * find_get_pages_contig - gang contiguous pagecache lookup
2149 * @mapping: The address_space to search
2150 * @index: The starting page index
2151 * @nr_pages: The maximum number of pages
2152 * @pages: Where the resulting pages are placed
2153 *
2154 * find_get_pages_contig() works exactly like find_get_pages(), except
2155 * that the returned number of pages are guaranteed to be contiguous.
2156 *
Mike Rapoporta862f682019-03-05 15:48:42 -08002157 * Return: the number of pages which were found.
Jens Axboeebf43502006-04-27 08:46:01 +02002158 */
2159unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t index,
2160 unsigned int nr_pages, struct page **pages)
2161{
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002162 XA_STATE(xas, &mapping->i_pages, index);
Matthew Wilcox (Oracle)e1c37722021-03-06 16:38:38 -05002163 struct folio *folio;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07002164 unsigned int ret = 0;
2165
2166 if (unlikely(!nr_pages))
2167 return 0;
Jens Axboeebf43502006-04-27 08:46:01 +02002168
Nick Piggina60637c2008-07-25 19:45:31 -07002169 rcu_read_lock();
Matthew Wilcox (Oracle)e1c37722021-03-06 16:38:38 -05002170 for (folio = xas_load(&xas); folio; folio = xas_next(&xas)) {
2171 if (xas_retry(&xas, folio))
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002172 continue;
2173 /*
2174 * If the entry has been swapped out, we can stop looking.
2175 * No current caller is looking for DAX entries.
2176 */
Matthew Wilcox (Oracle)e1c37722021-03-06 16:38:38 -05002177 if (xa_is_value(folio))
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07002178 break;
Hugh Dickins9d8aa4e2011-03-22 16:33:06 -07002179
Matthew Wilcox (Oracle)e1c37722021-03-06 16:38:38 -05002180 if (!folio_try_get_rcu(folio))
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002181 goto retry;
Nick Piggina60637c2008-07-25 19:45:31 -07002182
Matthew Wilcox (Oracle)e1c37722021-03-06 16:38:38 -05002183 if (unlikely(folio != xas_reload(&xas)))
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002184 goto put_page;
Nick Piggina60637c2008-07-25 19:45:31 -07002185
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -04002186again:
2187 pages[ret] = folio_file_page(folio, xas.xa_index);
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07002188 if (++ret == nr_pages)
2189 break;
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -04002190 if (folio_more_pages(folio, xas.xa_index, ULONG_MAX)) {
2191 xas.xa_index++;
2192 folio_ref_inc(folio);
2193 goto again;
2194 }
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002195 continue;
2196put_page:
Matthew Wilcox (Oracle)e1c37722021-03-06 16:38:38 -05002197 folio_put(folio);
Matthew Wilcox3ece58a2018-05-16 18:00:33 -04002198retry:
2199 xas_reset(&xas);
Jens Axboeebf43502006-04-27 08:46:01 +02002200 }
Nick Piggina60637c2008-07-25 19:45:31 -07002201 rcu_read_unlock();
2202 return ret;
Jens Axboeebf43502006-04-27 08:46:01 +02002203}
David Howellsef71c152007-05-09 02:33:44 -07002204EXPORT_SYMBOL(find_get_pages_contig);
Jens Axboeebf43502006-04-27 08:46:01 +02002205
Randy Dunlap485bb992006-06-23 02:03:49 -07002206/**
Matthew Wilcox (Oracle)c49f50d2021-02-25 17:15:25 -08002207 * find_get_pages_range_tag - Find and return head pages matching @tag.
Randy Dunlap485bb992006-06-23 02:03:49 -07002208 * @mapping: the address_space to search
2209 * @index: the starting page index
Jan Kara72b045a2017-11-15 17:34:33 -08002210 * @end: The final page index (inclusive)
Randy Dunlap485bb992006-06-23 02:03:49 -07002211 * @tag: the tag index
2212 * @nr_pages: the maximum number of pages
2213 * @pages: where the resulting pages are placed
2214 *
Matthew Wilcox (Oracle)c49f50d2021-02-25 17:15:25 -08002215 * Like find_get_pages(), except we only return head pages which are tagged
2216 * with @tag. @index is updated to the index immediately after the last
2217 * page we return, ready for the next iteration.
Mike Rapoporta862f682019-03-05 15:48:42 -08002218 *
2219 * Return: the number of pages which were found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 */
Jan Kara72b045a2017-11-15 17:34:33 -08002221unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
Matthew Wilcoxa6906972018-05-16 18:12:54 -04002222 pgoff_t end, xa_mark_t tag, unsigned int nr_pages,
Jan Kara72b045a2017-11-15 17:34:33 -08002223 struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Matthew Wilcoxa6906972018-05-16 18:12:54 -04002225 XA_STATE(xas, &mapping->i_pages, *index);
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002226 struct folio *folio;
Konstantin Khlebnikov0fc9d102012-03-28 14:42:54 -07002227 unsigned ret = 0;
2228
2229 if (unlikely(!nr_pages))
2230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Nick Piggina60637c2008-07-25 19:45:31 -07002232 rcu_read_lock();
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002233 while ((folio = find_get_entry(&xas, end, tag))) {
Matthew Wilcoxa6906972018-05-16 18:12:54 -04002234 /*
2235 * Shadow entries should never be tagged, but this iteration
2236 * is lockless so there is a window for page reclaim to evict
2237 * a page we saw tagged. Skip over it.
2238 */
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002239 if (xa_is_value(folio))
Johannes Weiner139b6a62014-05-06 12:50:05 -07002240 continue;
Nick Piggina60637c2008-07-25 19:45:31 -07002241
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002242 pages[ret] = &folio->page;
Jan Kara72b045a2017-11-15 17:34:33 -08002243 if (++ret == nr_pages) {
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002244 *index = folio->index + folio_nr_pages(folio);
Jan Kara72b045a2017-11-15 17:34:33 -08002245 goto out;
2246 }
Nick Piggina60637c2008-07-25 19:45:31 -07002247 }
Hugh Dickins5b280c02011-03-22 16:33:07 -07002248
Jan Kara72b045a2017-11-15 17:34:33 -08002249 /*
Matthew Wilcoxa6906972018-05-16 18:12:54 -04002250 * We come here when we got to @end. We take care to not overflow the
Jan Kara72b045a2017-11-15 17:34:33 -08002251 * index @index as it confuses some of the callers. This breaks the
Matthew Wilcoxa6906972018-05-16 18:12:54 -04002252 * iteration when there is a page at index -1 but that is already
2253 * broken anyway.
Jan Kara72b045a2017-11-15 17:34:33 -08002254 */
2255 if (end == (pgoff_t)-1)
2256 *index = (pgoff_t)-1;
2257 else
2258 *index = end + 1;
2259out:
Nick Piggina60637c2008-07-25 19:45:31 -07002260 rcu_read_unlock();
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 return ret;
2263}
Jan Kara72b045a2017-11-15 17:34:33 -08002264EXPORT_SYMBOL(find_get_pages_range_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002266/*
2267 * CD/DVDs are error prone. When a medium error occurs, the driver may fail
2268 * a _large_ part of the i/o request. Imagine the worst scenario:
2269 *
2270 * ---R__________________________________________B__________
2271 * ^ reading here ^ bad block(assume 4k)
2272 *
2273 * read(R) => miss => readahead(R...B) => media error => frustrating retries
2274 * => failing the whole request => read(R) => read(R+1) =>
2275 * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
2276 * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
2277 * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
2278 *
2279 * It is going insane. Fix it by quickly scaling down the readahead size.
2280 */
Souptick Joarder0f8e2db2020-04-01 21:04:50 -07002281static void shrink_readahead_size_eio(struct file_ra_state *ra)
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002282{
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002283 ra->ra_pages /= 4;
Wu Fengguang76d42bd2006-06-25 05:48:43 -07002284}
2285
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002286/*
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002287 * filemap_get_read_batch - Get a batch of folios for read
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002288 *
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002289 * Get a batch of folios which represent a contiguous range of bytes in
2290 * the file. No exceptional entries will be returned. If @index is in
2291 * the middle of a folio, the entire folio will be returned. The last
2292 * folio in the batch may have the readahead flag set or the uptodate flag
2293 * clear so that the caller can take the appropriate action.
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002294 */
2295static void filemap_get_read_batch(struct address_space *mapping,
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002296 pgoff_t index, pgoff_t max, struct folio_batch *fbatch)
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002297{
2298 XA_STATE(xas, &mapping->i_pages, index);
Matthew Wilcox (Oracle)bdb72932021-03-05 10:29:41 -05002299 struct folio *folio;
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002300
2301 rcu_read_lock();
Matthew Wilcox (Oracle)bdb72932021-03-05 10:29:41 -05002302 for (folio = xas_load(&xas); folio; folio = xas_next(&xas)) {
2303 if (xas_retry(&xas, folio))
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002304 continue;
Matthew Wilcox (Oracle)bdb72932021-03-05 10:29:41 -05002305 if (xas.xa_index > max || xa_is_value(folio))
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002306 break;
Matthew Wilcox (Oracle)bdb72932021-03-05 10:29:41 -05002307 if (!folio_try_get_rcu(folio))
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002308 goto retry;
2309
Matthew Wilcox (Oracle)bdb72932021-03-05 10:29:41 -05002310 if (unlikely(folio != xas_reload(&xas)))
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002311 goto put_folio;
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002312
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002313 if (!folio_batch_add(fbatch, folio))
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002314 break;
Matthew Wilcox (Oracle)bdb72932021-03-05 10:29:41 -05002315 if (!folio_test_uptodate(folio))
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002316 break;
Matthew Wilcox (Oracle)bdb72932021-03-05 10:29:41 -05002317 if (folio_test_readahead(folio))
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002318 break;
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -04002319 xas_advance(&xas, folio->index + folio_nr_pages(folio) - 1);
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002320 continue;
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002321put_folio:
Matthew Wilcox (Oracle)bdb72932021-03-05 10:29:41 -05002322 folio_put(folio);
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002323retry:
2324 xas_reset(&xas);
2325 }
2326 rcu_read_unlock();
2327}
2328
Matthew Wilcox (Oracle)9d427b42021-03-10 10:19:30 -05002329static int filemap_read_folio(struct file *file, struct address_space *mapping,
2330 struct folio *folio)
Kent Overstreet723ef242020-12-14 19:04:52 -08002331{
Kent Overstreet723ef242020-12-14 19:04:52 -08002332 int error;
2333
Kent Overstreet723ef242020-12-14 19:04:52 -08002334 /*
Matthew Wilcox (Oracle)68430302021-02-24 12:02:15 -08002335 * A previous I/O error may have been due to temporary failures,
2336 * eg. multipath errors. PG_error will be set again if readpage
2337 * fails.
Kent Overstreet723ef242020-12-14 19:04:52 -08002338 */
Matthew Wilcox (Oracle)9d427b42021-03-10 10:19:30 -05002339 folio_clear_error(folio);
Kent Overstreet723ef242020-12-14 19:04:52 -08002340 /* Start the actual read. The read will unlock the page. */
Matthew Wilcox (Oracle)9d427b42021-03-10 10:19:30 -05002341 error = mapping->a_ops->readpage(file, &folio->page);
Matthew Wilcox (Oracle)68430302021-02-24 12:02:15 -08002342 if (error)
2343 return error;
Kent Overstreet723ef242020-12-14 19:04:52 -08002344
Matthew Wilcox (Oracle)9d427b42021-03-10 10:19:30 -05002345 error = folio_wait_locked_killable(folio);
Matthew Wilcox (Oracle)68430302021-02-24 12:02:15 -08002346 if (error)
2347 return error;
Matthew Wilcox (Oracle)9d427b42021-03-10 10:19:30 -05002348 if (folio_test_uptodate(folio))
Matthew Wilcox (Oracle)aa1ec2f2021-02-24 12:02:38 -08002349 return 0;
Matthew Wilcox (Oracle)aa1ec2f2021-02-24 12:02:38 -08002350 shrink_readahead_size_eio(&file->f_ra);
2351 return -EIO;
Kent Overstreet723ef242020-12-14 19:04:52 -08002352}
2353
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002354static bool filemap_range_uptodate(struct address_space *mapping,
Matthew Wilcox (Oracle)2fa4eeb2021-03-10 11:04:19 -05002355 loff_t pos, struct iov_iter *iter, struct folio *folio)
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002356{
2357 int count;
2358
Matthew Wilcox (Oracle)2fa4eeb2021-03-10 11:04:19 -05002359 if (folio_test_uptodate(folio))
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002360 return true;
2361 /* pipes can't handle partially uptodate pages */
2362 if (iov_iter_is_pipe(iter))
2363 return false;
2364 if (!mapping->a_ops->is_partially_uptodate)
2365 return false;
Matthew Wilcox (Oracle)2fa4eeb2021-03-10 11:04:19 -05002366 if (mapping->host->i_blkbits >= folio_shift(folio))
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002367 return false;
2368
2369 count = iter->count;
Matthew Wilcox (Oracle)2fa4eeb2021-03-10 11:04:19 -05002370 if (folio_pos(folio) > pos) {
2371 count -= folio_pos(folio) - pos;
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002372 pos = 0;
2373 } else {
Matthew Wilcox (Oracle)2fa4eeb2021-03-10 11:04:19 -05002374 pos -= folio_pos(folio);
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002375 }
2376
Matthew Wilcox (Oracle)2fa4eeb2021-03-10 11:04:19 -05002377 return mapping->a_ops->is_partially_uptodate(&folio->page, pos, count);
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002378}
2379
Matthew Wilcox (Oracle)4612aee2021-02-24 12:02:22 -08002380static int filemap_update_page(struct kiocb *iocb,
2381 struct address_space *mapping, struct iov_iter *iter,
Matthew Wilcox (Oracle)65bca532021-03-10 14:01:22 -05002382 struct folio *folio)
Kent Overstreet723ef242020-12-14 19:04:52 -08002383{
Kent Overstreet723ef242020-12-14 19:04:52 -08002384 int error;
2385
Jan Kara730633f2021-01-28 19:19:45 +01002386 if (iocb->ki_flags & IOCB_NOWAIT) {
2387 if (!filemap_invalidate_trylock_shared(mapping))
Matthew Wilcox (Oracle)87d1d7b2021-02-24 12:02:25 -08002388 return -EAGAIN;
Jan Kara730633f2021-01-28 19:19:45 +01002389 } else {
2390 filemap_invalidate_lock_shared(mapping);
2391 }
2392
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05002393 if (!folio_trylock(folio)) {
Jan Kara730633f2021-01-28 19:19:45 +01002394 error = -EAGAIN;
2395 if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_NOIO))
2396 goto unlock_mapping;
Matthew Wilcox (Oracle)87d1d7b2021-02-24 12:02:25 -08002397 if (!(iocb->ki_flags & IOCB_WAITQ)) {
Jan Kara730633f2021-01-28 19:19:45 +01002398 filemap_invalidate_unlock_shared(mapping);
Matthew Wilcox (Oracle)9f2b04a2021-08-16 23:36:31 -04002399 /*
2400 * This is where we usually end up waiting for a
2401 * previously submitted readahead to finish.
2402 */
2403 folio_put_wait_locked(folio, TASK_KILLABLE);
Matthew Wilcox (Oracle)4612aee2021-02-24 12:02:22 -08002404 return AOP_TRUNCATED_PAGE;
Matthew Wilcox (Oracle)bd8a1f32021-02-24 12:02:05 -08002405 }
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05002406 error = __folio_lock_async(folio, iocb->ki_waitq);
Matthew Wilcox (Oracle)87d1d7b2021-02-24 12:02:25 -08002407 if (error)
Jan Kara730633f2021-01-28 19:19:45 +01002408 goto unlock_mapping;
Kent Overstreet723ef242020-12-14 19:04:52 -08002409 }
Kent Overstreet723ef242020-12-14 19:04:52 -08002410
Jan Kara730633f2021-01-28 19:19:45 +01002411 error = AOP_TRUNCATED_PAGE;
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05002412 if (!folio->mapping)
Jan Kara730633f2021-01-28 19:19:45 +01002413 goto unlock;
Kent Overstreet723ef242020-12-14 19:04:52 -08002414
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002415 error = 0;
Matthew Wilcox (Oracle)2fa4eeb2021-03-10 11:04:19 -05002416 if (filemap_range_uptodate(mapping, iocb->ki_pos, iter, folio))
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002417 goto unlock;
2418
2419 error = -EAGAIN;
2420 if (iocb->ki_flags & (IOCB_NOIO | IOCB_NOWAIT | IOCB_WAITQ))
2421 goto unlock;
2422
Matthew Wilcox (Oracle)9d427b42021-03-10 10:19:30 -05002423 error = filemap_read_folio(iocb->ki_filp, mapping, folio);
Jan Kara730633f2021-01-28 19:19:45 +01002424 goto unlock_mapping;
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002425unlock:
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05002426 folio_unlock(folio);
Jan Kara730633f2021-01-28 19:19:45 +01002427unlock_mapping:
2428 filemap_invalidate_unlock_shared(mapping);
2429 if (error == AOP_TRUNCATED_PAGE)
Matthew Wilcox (Oracle)ffdc8da2020-12-30 17:58:40 -05002430 folio_put(folio);
Matthew Wilcox (Oracle)fce70da2021-02-24 12:02:28 -08002431 return error;
Kent Overstreet723ef242020-12-14 19:04:52 -08002432}
2433
Matthew Wilcox (Oracle)a5d4ad02021-03-10 10:34:00 -05002434static int filemap_create_folio(struct file *file,
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002435 struct address_space *mapping, pgoff_t index,
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002436 struct folio_batch *fbatch)
Kent Overstreet723ef242020-12-14 19:04:52 -08002437{
Matthew Wilcox (Oracle)a5d4ad02021-03-10 10:34:00 -05002438 struct folio *folio;
Kent Overstreet723ef242020-12-14 19:04:52 -08002439 int error;
2440
Matthew Wilcox (Oracle)a5d4ad02021-03-10 10:34:00 -05002441 folio = filemap_alloc_folio(mapping_gfp_mask(mapping), 0);
2442 if (!folio)
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002443 return -ENOMEM;
Kent Overstreet723ef242020-12-14 19:04:52 -08002444
Jan Kara730633f2021-01-28 19:19:45 +01002445 /*
Matthew Wilcox (Oracle)a5d4ad02021-03-10 10:34:00 -05002446 * Protect against truncate / hole punch. Grabbing invalidate_lock
2447 * here assures we cannot instantiate and bring uptodate new
2448 * pagecache folios after evicting page cache during truncate
2449 * and before actually freeing blocks. Note that we could
2450 * release invalidate_lock after inserting the folio into
2451 * the page cache as the locked folio would then be enough to
2452 * synchronize with hole punching. But there are code paths
2453 * such as filemap_update_page() filling in partially uptodate
2454 * pages or ->readpages() that need to hold invalidate_lock
2455 * while mapping blocks for IO so let's hold the lock here as
2456 * well to keep locking rules simple.
Jan Kara730633f2021-01-28 19:19:45 +01002457 */
2458 filemap_invalidate_lock_shared(mapping);
Matthew Wilcox (Oracle)a5d4ad02021-03-10 10:34:00 -05002459 error = filemap_add_folio(mapping, folio, index,
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002460 mapping_gfp_constraint(mapping, GFP_KERNEL));
2461 if (error == -EEXIST)
2462 error = AOP_TRUNCATED_PAGE;
2463 if (error)
2464 goto error;
2465
Matthew Wilcox (Oracle)a5d4ad02021-03-10 10:34:00 -05002466 error = filemap_read_folio(file, mapping, folio);
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002467 if (error)
2468 goto error;
2469
Jan Kara730633f2021-01-28 19:19:45 +01002470 filemap_invalidate_unlock_shared(mapping);
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002471 folio_batch_add(fbatch, folio);
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002472 return 0;
2473error:
Jan Kara730633f2021-01-28 19:19:45 +01002474 filemap_invalidate_unlock_shared(mapping);
Matthew Wilcox (Oracle)a5d4ad02021-03-10 10:34:00 -05002475 folio_put(folio);
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002476 return error;
Kent Overstreet723ef242020-12-14 19:04:52 -08002477}
2478
Matthew Wilcox (Oracle)5963fe02021-02-24 12:02:32 -08002479static int filemap_readahead(struct kiocb *iocb, struct file *file,
Matthew Wilcox (Oracle)65bca532021-03-10 14:01:22 -05002480 struct address_space *mapping, struct folio *folio,
Matthew Wilcox (Oracle)5963fe02021-02-24 12:02:32 -08002481 pgoff_t last_index)
2482{
Matthew Wilcox (Oracle)65bca532021-03-10 14:01:22 -05002483 DEFINE_READAHEAD(ractl, file, &file->f_ra, mapping, folio->index);
2484
Matthew Wilcox (Oracle)5963fe02021-02-24 12:02:32 -08002485 if (iocb->ki_flags & IOCB_NOIO)
2486 return -EAGAIN;
Matthew Wilcox (Oracle)65bca532021-03-10 14:01:22 -05002487 page_cache_async_ra(&ractl, folio, last_index - folio->index);
Matthew Wilcox (Oracle)5963fe02021-02-24 12:02:32 -08002488 return 0;
2489}
2490
Matthew Wilcox (Oracle)3a6bae42021-02-24 12:01:49 -08002491static int filemap_get_pages(struct kiocb *iocb, struct iov_iter *iter,
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002492 struct folio_batch *fbatch)
Kent Overstreet06c04442020-12-14 19:04:56 -08002493{
2494 struct file *filp = iocb->ki_filp;
2495 struct address_space *mapping = filp->f_mapping;
2496 struct file_ra_state *ra = &filp->f_ra;
2497 pgoff_t index = iocb->ki_pos >> PAGE_SHIFT;
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002498 pgoff_t last_index;
Matthew Wilcox (Oracle)65bca532021-03-10 14:01:22 -05002499 struct folio *folio;
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002500 int err = 0;
Kent Overstreet06c04442020-12-14 19:04:56 -08002501
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002502 last_index = DIV_ROUND_UP(iocb->ki_pos + iter->count, PAGE_SIZE);
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002503retry:
Kent Overstreet06c04442020-12-14 19:04:56 -08002504 if (fatal_signal_pending(current))
2505 return -EINTR;
2506
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002507 filemap_get_read_batch(mapping, index, last_index, fbatch);
2508 if (!folio_batch_count(fbatch)) {
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002509 if (iocb->ki_flags & IOCB_NOIO)
2510 return -EAGAIN;
2511 page_cache_sync_readahead(mapping, ra, filp, index,
2512 last_index - index);
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002513 filemap_get_read_batch(mapping, index, last_index, fbatch);
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002514 }
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002515 if (!folio_batch_count(fbatch)) {
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002516 if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_WAITQ))
2517 return -EAGAIN;
Matthew Wilcox (Oracle)a5d4ad02021-03-10 10:34:00 -05002518 err = filemap_create_folio(filp, mapping,
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002519 iocb->ki_pos >> PAGE_SHIFT, fbatch);
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002520 if (err == AOP_TRUNCATED_PAGE)
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002521 goto retry;
Matthew Wilcox (Oracle)f253e182021-02-24 12:02:18 -08002522 return err;
2523 }
Kent Overstreet06c04442020-12-14 19:04:56 -08002524
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002525 folio = fbatch->folios[folio_batch_count(fbatch) - 1];
Matthew Wilcox (Oracle)65bca532021-03-10 14:01:22 -05002526 if (folio_test_readahead(folio)) {
2527 err = filemap_readahead(iocb, filp, mapping, folio, last_index);
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002528 if (err)
2529 goto err;
2530 }
Matthew Wilcox (Oracle)65bca532021-03-10 14:01:22 -05002531 if (!folio_test_uptodate(folio)) {
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002532 if ((iocb->ki_flags & IOCB_WAITQ) &&
2533 folio_batch_count(fbatch) > 1)
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002534 iocb->ki_flags |= IOCB_NOWAIT;
Matthew Wilcox (Oracle)65bca532021-03-10 14:01:22 -05002535 err = filemap_update_page(iocb, mapping, iter, folio);
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002536 if (err)
2537 goto err;
Kent Overstreet06c04442020-12-14 19:04:56 -08002538 }
2539
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002540 return 0;
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002541err:
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002542 if (err < 0)
Matthew Wilcox (Oracle)65bca532021-03-10 14:01:22 -05002543 folio_put(folio);
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002544 if (likely(--fbatch->nr))
Matthew Wilcox (Oracle)ff993ba2021-02-24 12:01:55 -08002545 return 0;
Matthew Wilcox (Oracle)4612aee2021-02-24 12:02:22 -08002546 if (err == AOP_TRUNCATED_PAGE)
Matthew Wilcox (Oracle)2642fca2021-02-24 12:02:35 -08002547 goto retry;
2548 return err;
Kent Overstreet06c04442020-12-14 19:04:56 -08002549}
2550
Randy Dunlap485bb992006-06-23 02:03:49 -07002551/**
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002552 * filemap_read - Read data from the page cache.
2553 * @iocb: The iocb to read.
2554 * @iter: Destination for the data.
2555 * @already_read: Number of bytes already read by the caller.
Randy Dunlap485bb992006-06-23 02:03:49 -07002556 *
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002557 * Copies data from the page cache. If the data is not currently present,
2558 * uses the readahead and readpage address_space operations to fetch it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 *
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002560 * Return: Total number of bytes copied, including those already read by
2561 * the caller. If an error happens before any bytes are copied, returns
2562 * a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 */
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002564ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
2565 ssize_t already_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002567 struct file *filp = iocb->ki_filp;
Kent Overstreet06c04442020-12-14 19:04:56 -08002568 struct file_ra_state *ra = &filp->f_ra;
Christoph Hellwig36e78912008-02-08 04:21:24 -08002569 struct address_space *mapping = filp->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 struct inode *inode = mapping->host;
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002571 struct folio_batch fbatch;
Matthew Wilcox (Oracle)ff993ba2021-02-24 12:01:55 -08002572 int i, error = 0;
Kent Overstreet06c04442020-12-14 19:04:56 -08002573 bool writably_mapped;
2574 loff_t isize, end_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Kent Overstreet723ef242020-12-14 19:04:52 -08002576 if (unlikely(iocb->ki_pos >= inode->i_sb->s_maxbytes))
Linus Torvaldsd05c5f72016-12-14 12:45:25 -08002577 return 0;
Kent Overstreet3644e2d2020-12-18 04:07:11 -05002578 if (unlikely(!iov_iter_count(iter)))
2579 return 0;
2580
Wei Fangc2a97372016-10-07 17:01:52 -07002581 iov_iter_truncate(iter, inode->i_sb->s_maxbytes);
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002582 folio_batch_init(&fbatch);
Wei Fangc2a97372016-10-07 17:01:52 -07002583
Kent Overstreet06c04442020-12-14 19:04:56 -08002584 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 cond_resched();
Kent Overstreet06c04442020-12-14 19:04:56 -08002586
2587 /*
2588 * If we've already successfully copied some data, then we
2589 * can no longer safely return -EIOCBQUEUED. Hence mark
2590 * an async read NOWAIT at that point.
2591 */
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002592 if ((iocb->ki_flags & IOCB_WAITQ) && already_read)
Kent Overstreet06c04442020-12-14 19:04:56 -08002593 iocb->ki_flags |= IOCB_NOWAIT;
2594
David Howells8c8387e2021-11-05 13:36:49 -07002595 if (unlikely(iocb->ki_pos >= i_size_read(inode)))
2596 break;
2597
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002598 error = filemap_get_pages(iocb, iter, &fbatch);
Matthew Wilcox (Oracle)ff993ba2021-02-24 12:01:55 -08002599 if (error < 0)
Kent Overstreet06c04442020-12-14 19:04:56 -08002600 break;
Michal Hocko5abf1862017-02-03 13:13:29 -08002601
Kent Overstreet723ef242020-12-14 19:04:52 -08002602 /*
Kent Overstreet06c04442020-12-14 19:04:56 -08002603 * i_size must be checked after we know the pages are Uptodate.
2604 *
2605 * Checking i_size after the check allows us to calculate
2606 * the correct value for "nr", which means the zero-filled
2607 * part of the page is not copied back to userspace (unless
2608 * another truncate extends the file - this is desired though).
Kent Overstreet723ef242020-12-14 19:04:52 -08002609 */
Kent Overstreet06c04442020-12-14 19:04:56 -08002610 isize = i_size_read(inode);
2611 if (unlikely(iocb->ki_pos >= isize))
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002612 goto put_folios;
Kent Overstreet06c04442020-12-14 19:04:56 -08002613 end_offset = min_t(loff_t, isize, iocb->ki_pos + iter->count);
2614
Kent Overstreet06c04442020-12-14 19:04:56 -08002615 /*
2616 * Once we start copying data, we don't want to be touching any
2617 * cachelines that might be contended:
2618 */
2619 writably_mapped = mapping_writably_mapped(mapping);
2620
2621 /*
2622 * When a sequential read accesses a page several times, only
2623 * mark it as accessed the first time.
2624 */
2625 if (iocb->ki_pos >> PAGE_SHIFT !=
2626 ra->prev_pos >> PAGE_SHIFT)
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002627 folio_mark_accessed(fbatch.folios[0]);
Kent Overstreet06c04442020-12-14 19:04:56 -08002628
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002629 for (i = 0; i < folio_batch_count(&fbatch); i++) {
2630 struct folio *folio = fbatch.folios[i];
Matthew Wilcox (Oracle)d996fc72021-10-31 22:22:19 -04002631 size_t fsize = folio_size(folio);
2632 size_t offset = iocb->ki_pos & (fsize - 1);
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002633 size_t bytes = min_t(loff_t, end_offset - iocb->ki_pos,
Matthew Wilcox (Oracle)d996fc72021-10-31 22:22:19 -04002634 fsize - offset);
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002635 size_t copied;
Kent Overstreet06c04442020-12-14 19:04:56 -08002636
Matthew Wilcox (Oracle)d996fc72021-10-31 22:22:19 -04002637 if (end_offset < folio_pos(folio))
Matthew Wilcox (Oracle)cbd59c42021-02-24 12:01:59 -08002638 break;
2639 if (i > 0)
Matthew Wilcox (Oracle)d996fc72021-10-31 22:22:19 -04002640 folio_mark_accessed(folio);
Kent Overstreet06c04442020-12-14 19:04:56 -08002641 /*
Matthew Wilcox (Oracle)d996fc72021-10-31 22:22:19 -04002642 * If users can be writing to this folio using arbitrary
2643 * virtual addresses, take care of potential aliasing
2644 * before reading the folio on the kernel side.
Kent Overstreet06c04442020-12-14 19:04:56 -08002645 */
Matthew Wilcox (Oracle)d996fc72021-10-31 22:22:19 -04002646 if (writably_mapped)
2647 flush_dcache_folio(folio);
Kent Overstreet06c04442020-12-14 19:04:56 -08002648
Matthew Wilcox (Oracle)d996fc72021-10-31 22:22:19 -04002649 copied = copy_folio_to_iter(folio, offset, bytes, iter);
Kent Overstreet06c04442020-12-14 19:04:56 -08002650
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002651 already_read += copied;
Kent Overstreet06c04442020-12-14 19:04:56 -08002652 iocb->ki_pos += copied;
2653 ra->prev_pos = iocb->ki_pos;
2654
2655 if (copied < bytes) {
2656 error = -EFAULT;
2657 break;
Kent Overstreet723ef242020-12-14 19:04:52 -08002658 }
Fengguang Wu3ea89ee2007-07-19 01:48:02 -07002659 }
Matthew Wilcox (Oracle)25d6a232021-12-06 15:25:33 -05002660put_folios:
2661 for (i = 0; i < folio_batch_count(&fbatch); i++)
2662 folio_put(fbatch.folios[i]);
2663 folio_batch_init(&fbatch);
Kent Overstreet06c04442020-12-14 19:04:56 -08002664 } while (iov_iter_count(iter) && iocb->ki_pos < isize && !error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Krishna Kumar0c6aa262008-10-15 22:01:13 -07002666 file_accessed(filp);
Kent Overstreet06c04442020-12-14 19:04:56 -08002667
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002668 return already_read ? already_read : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669}
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08002670EXPORT_SYMBOL_GPL(filemap_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Randy Dunlap485bb992006-06-23 02:03:49 -07002672/**
Al Viro6abd2322014-04-04 14:20:57 -04002673 * generic_file_read_iter - generic filesystem read routine
Randy Dunlap485bb992006-06-23 02:03:49 -07002674 * @iocb: kernel I/O control block
Al Viro6abd2322014-04-04 14:20:57 -04002675 * @iter: destination for the data read
Randy Dunlap485bb992006-06-23 02:03:49 -07002676 *
Al Viro6abd2322014-04-04 14:20:57 -04002677 * This is the "read_iter()" routine for all filesystems
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 * that can use the page cache directly.
Andreas Gruenbacher41da51b2019-11-21 23:25:07 +00002679 *
2680 * The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall
2681 * be returned when no data can be read without waiting for I/O requests
2682 * to complete; it doesn't prevent readahead.
2683 *
2684 * The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O
2685 * requests shall be made for the read or for readahead. When no data
2686 * can be read, -EAGAIN shall be returned. When readahead would be
2687 * triggered, a partial, possibly empty read shall be returned.
2688 *
Mike Rapoporta862f682019-03-05 15:48:42 -08002689 * Return:
2690 * * number of bytes copied, even for partial reads
Andreas Gruenbacher41da51b2019-11-21 23:25:07 +00002691 * * negative error code (or 0 if IOCB_NOIO) if nothing was read
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 */
2693ssize_t
Al Viroed978a82014-03-05 22:53:04 -05002694generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
Nicolai Stangee7080a42016-03-25 14:22:14 -07002696 size_t count = iov_iter_count(iter);
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002697 ssize_t retval = 0;
Nicolai Stangee7080a42016-03-25 14:22:14 -07002698
2699 if (!count)
Christoph Hellwig826ea862021-02-24 12:02:45 -08002700 return 0; /* skip atime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Al Viro2ba48ce2015-04-09 13:52:01 -04002702 if (iocb->ki_flags & IOCB_DIRECT) {
Christoph Hellwig47c27bc2017-08-29 16:13:18 +02002703 struct file *file = iocb->ki_filp;
Al Viroed978a82014-03-05 22:53:04 -05002704 struct address_space *mapping = file->f_mapping;
2705 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002707 if (iocb->ki_flags & IOCB_NOWAIT) {
Jens Axboe7a60d6d2021-04-29 22:55:21 -07002708 if (filemap_range_needs_writeback(mapping, iocb->ki_pos,
2709 iocb->ki_pos + count - 1))
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002710 return -EAGAIN;
2711 } else {
2712 retval = filemap_write_and_wait_range(mapping,
2713 iocb->ki_pos,
2714 iocb->ki_pos + count - 1);
2715 if (retval < 0)
Christoph Hellwig826ea862021-02-24 12:02:45 -08002716 return retval;
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05002717 }
Al Viroed978a82014-03-05 22:53:04 -05002718
Christoph Hellwig0d5b0cf2016-10-03 09:48:08 +11002719 file_accessed(file);
2720
Al Viro5ecda132017-04-13 14:13:36 -04002721 retval = mapping->a_ops->direct_IO(iocb, iter);
Al Viroc3a69022016-10-10 13:26:27 -04002722 if (retval >= 0) {
Christoph Hellwigc64fb5c2016-04-07 08:51:55 -07002723 iocb->ki_pos += retval;
Al Viro5ecda132017-04-13 14:13:36 -04002724 count -= retval;
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002725 }
Pavel Begunkovab2125d2021-02-24 12:01:45 -08002726 if (retval != -EIOCBQUEUED)
2727 iov_iter_revert(iter, count - iov_iter_count(iter));
Josef Bacik66f998f2010-05-23 11:00:54 -04002728
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002729 /*
2730 * Btrfs can have a short DIO read if we encounter
2731 * compressed extents, so if there was an error, or if
2732 * we've already read everything we wanted to, or if
2733 * there was a short read because we hit EOF, go ahead
2734 * and return. Otherwise fallthrough to buffered io for
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08002735 * the rest of the read. Buffered reads will not work for
2736 * DAX files, so don't bother trying.
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002737 */
Jens Axboe61d00172021-11-05 13:37:07 -07002738 if (retval < 0 || !count || IS_DAX(inode))
2739 return retval;
2740 if (iocb->ki_pos >= i_size_read(inode))
Christoph Hellwig826ea862021-02-24 12:02:45 -08002741 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 }
2743
Christoph Hellwig826ea862021-02-24 12:02:45 -08002744 return filemap_read(iocb, iter, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
Al Viroed978a82014-03-05 22:53:04 -05002746EXPORT_SYMBOL(generic_file_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002748static inline loff_t folio_seek_hole_data(struct xa_state *xas,
2749 struct address_space *mapping, struct folio *folio,
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -08002750 loff_t start, loff_t end, bool seek_data)
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002751{
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -08002752 const struct address_space_operations *ops = mapping->a_ops;
2753 size_t offset, bsz = i_blocksize(mapping->host);
2754
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002755 if (xa_is_value(folio) || folio_test_uptodate(folio))
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -08002756 return seek_data ? start : end;
2757 if (!ops->is_partially_uptodate)
2758 return seek_data ? end : start;
2759
2760 xas_pause(xas);
2761 rcu_read_unlock();
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002762 folio_lock(folio);
2763 if (unlikely(folio->mapping != mapping))
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -08002764 goto unlock;
2765
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002766 offset = offset_in_folio(folio, start) & ~(bsz - 1);
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -08002767
2768 do {
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002769 if (ops->is_partially_uptodate(&folio->page, offset, bsz) ==
2770 seek_data)
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -08002771 break;
2772 start = (start + bsz) & ~(bsz - 1);
2773 offset += bsz;
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002774 } while (offset < folio_size(folio));
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -08002775unlock:
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002776 folio_unlock(folio);
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -08002777 rcu_read_lock();
2778 return start;
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002779}
2780
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002781static inline size_t seek_folio_size(struct xa_state *xas, struct folio *folio)
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002782{
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002783 if (xa_is_value(folio))
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002784 return PAGE_SIZE << xa_get_order(xas->xa, xas->xa_index);
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002785 return folio_size(folio);
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002786}
2787
2788/**
2789 * mapping_seek_hole_data - Seek for SEEK_DATA / SEEK_HOLE in the page cache.
2790 * @mapping: Address space to search.
2791 * @start: First byte to consider.
2792 * @end: Limit of search (exclusive).
2793 * @whence: Either SEEK_HOLE or SEEK_DATA.
2794 *
2795 * If the page cache knows which blocks contain holes and which blocks
2796 * contain data, your filesystem can use this function to implement
2797 * SEEK_HOLE and SEEK_DATA. This is useful for filesystems which are
2798 * entirely memory-based such as tmpfs, and filesystems which support
2799 * unwritten extents.
2800 *
Ingo Molnarf0953a12021-05-06 18:06:47 -07002801 * Return: The requested offset on success, or -ENXIO if @whence specifies
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002802 * SEEK_DATA and there is no data after @start. There is an implicit hole
2803 * after @end - 1, so SEEK_HOLE returns @end if all the bytes between @start
2804 * and @end contain data.
2805 */
2806loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start,
2807 loff_t end, int whence)
2808{
2809 XA_STATE(xas, &mapping->i_pages, start >> PAGE_SHIFT);
Hugh Dickinsed98b012021-04-23 14:29:00 -07002810 pgoff_t max = (end - 1) >> PAGE_SHIFT;
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002811 bool seek_data = (whence == SEEK_DATA);
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002812 struct folio *folio;
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002813
2814 if (end <= start)
2815 return -ENXIO;
2816
2817 rcu_read_lock();
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002818 while ((folio = find_get_entry(&xas, max, XA_PRESENT))) {
Hugh Dickinsed98b012021-04-23 14:29:00 -07002819 loff_t pos = (u64)xas.xa_index << PAGE_SHIFT;
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002820 size_t seek_size;
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002821
2822 if (start < pos) {
2823 if (!seek_data)
2824 goto unlock;
2825 start = pos;
2826 }
2827
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002828 seek_size = seek_folio_size(&xas, folio);
2829 pos = round_up((u64)pos + 1, seek_size);
2830 start = folio_seek_hole_data(&xas, mapping, folio, start, pos,
Matthew Wilcox (Oracle)54fa39a2021-02-25 17:15:52 -08002831 seek_data);
2832 if (start < pos)
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002833 goto unlock;
Hugh Dickinsed98b012021-04-23 14:29:00 -07002834 if (start >= end)
2835 break;
2836 if (seek_size > PAGE_SIZE)
2837 xas_set(&xas, pos >> PAGE_SHIFT);
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002838 if (!xa_is_value(folio))
2839 folio_put(folio);
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002840 }
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002841 if (seek_data)
Hugh Dickinsed98b012021-04-23 14:29:00 -07002842 start = -ENXIO;
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002843unlock:
2844 rcu_read_unlock();
Matthew Wilcox (Oracle)f5e64292020-12-17 00:12:26 -05002845 if (folio && !xa_is_value(folio))
2846 folio_put(folio);
Matthew Wilcox (Oracle)41139aa2021-02-25 17:15:48 -08002847 if (start > end)
2848 return end;
2849 return start;
2850}
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853#define MMAP_LOTSAMISS (100)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002854/*
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05002855 * lock_folio_maybe_drop_mmap - lock the page, possibly dropping the mmap_lock
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002856 * @vmf - the vm_fault for this fault.
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05002857 * @folio - the folio to lock.
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002858 * @fpin - the pointer to the file we may pin (or is already pinned).
2859 *
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05002860 * This works similar to lock_folio_or_retry in that it can drop the
2861 * mmap_lock. It differs in that it actually returns the folio locked
2862 * if it returns 1 and 0 if it couldn't lock the folio. If we did have
2863 * to drop the mmap_lock then fpin will point to the pinned file and
2864 * needs to be fput()'ed at a later point.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002865 */
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05002866static int lock_folio_maybe_drop_mmap(struct vm_fault *vmf, struct folio *folio,
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002867 struct file **fpin)
2868{
Matthew Wilcox (Oracle)7c23c782021-03-01 19:38:25 -05002869 if (folio_trylock(folio))
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002870 return 1;
2871
Linus Torvalds8b0f9fa2019-03-15 11:26:07 -07002872 /*
2873 * NOTE! This will make us return with VM_FAULT_RETRY, but with
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002874 * the mmap_lock still held. That's how FAULT_FLAG_RETRY_NOWAIT
Linus Torvalds8b0f9fa2019-03-15 11:26:07 -07002875 * is supposed to work. We have way too many special cases..
2876 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002877 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
2878 return 0;
2879
2880 *fpin = maybe_unlock_mmap_for_io(vmf, *fpin);
2881 if (vmf->flags & FAULT_FLAG_KILLABLE) {
Matthew Wilcox (Oracle)af7f29d2020-12-08 00:07:31 -05002882 if (__folio_lock_killable(folio)) {
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002883 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002884 * We didn't have the right flags to drop the mmap_lock,
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002885 * but all fault_handlers only check for fatal signals
2886 * if we return VM_FAULT_RETRY, so we need to drop the
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002887 * mmap_lock here and return 0 if we don't have a fpin.
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002888 */
2889 if (*fpin == NULL)
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002890 mmap_read_unlock(vmf->vma->vm_mm);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002891 return 0;
2892 }
2893 } else
Matthew Wilcox (Oracle)7c23c782021-03-01 19:38:25 -05002894 __folio_lock(folio);
2895
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002896 return 1;
2897}
2898
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002899/*
2900 * Synchronous readahead happens when we don't even find a page in the page
2901 * cache at all. We don't want to perform IO under the mmap sem, so if we have
2902 * to drop the mmap sem we return the file that was pinned in order for us to do
2903 * that. If we didn't pin a file then we return NULL. The file that is
2904 * returned needs to be fput()'ed when we're done with it.
2905 */
2906static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002907{
Josef Bacik2a1180f2019-03-13 11:44:18 -07002908 struct file *file = vmf->vma->vm_file;
2909 struct file_ra_state *ra = &file->f_ra;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002910 struct address_space *mapping = file->f_mapping;
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +01002911 DEFINE_READAHEAD(ractl, file, ra, mapping, vmf->pgoff);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002912 struct file *fpin = NULL;
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07002913 unsigned int mmap_miss;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002914
2915 /* If we don't want any read-ahead, don't bother */
Josef Bacik2a1180f2019-03-13 11:44:18 -07002916 if (vmf->vma->vm_flags & VM_RAND_READ)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002917 return fpin;
Wu Fengguang275b12b2011-05-24 17:12:28 -07002918 if (!ra->ra_pages)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002919 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002920
Josef Bacik2a1180f2019-03-13 11:44:18 -07002921 if (vmf->vma->vm_flags & VM_SEQ_READ) {
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002922 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Matthew Wilcox (Oracle)fcd9ae42021-04-07 21:18:55 +01002923 page_cache_sync_ra(&ractl, ra->ra_pages);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002924 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002925 }
2926
Andi Kleen207d04b2011-05-24 17:12:29 -07002927 /* Avoid banging the cache line if not needed */
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07002928 mmap_miss = READ_ONCE(ra->mmap_miss);
2929 if (mmap_miss < MMAP_LOTSAMISS * 10)
2930 WRITE_ONCE(ra->mmap_miss, ++mmap_miss);
Linus Torvaldsef00e082009-06-16 15:31:25 -07002931
2932 /*
2933 * Do we miss much more than hit in this file? If so,
2934 * stop bothering with read-ahead. It will only hurt.
2935 */
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07002936 if (mmap_miss > MMAP_LOTSAMISS)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002937 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002938
Wu Fengguangd30a1102009-06-16 15:31:30 -07002939 /*
2940 * mmap read-around
2941 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002942 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
David Howellsdb660d42020-10-15 20:06:31 -07002943 ra->start = max_t(long, 0, vmf->pgoff - ra->ra_pages / 2);
Roman Gushchin600e19a2015-11-05 18:47:08 -08002944 ra->size = ra->ra_pages;
2945 ra->async_size = ra->ra_pages / 4;
David Howellsdb660d42020-10-15 20:06:31 -07002946 ractl._index = ra->start;
2947 do_page_cache_ra(&ractl, ra->size, ra->async_size);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002948 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002949}
2950
2951/*
2952 * Asynchronous readahead happens when we find the page and PG_readahead,
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002953 * so we want to possibly extend the readahead further. We return the file that
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002954 * was pinned if we have to drop the mmap_lock in order to do IO.
Linus Torvaldsef00e082009-06-16 15:31:25 -07002955 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002956static struct file *do_async_mmap_readahead(struct vm_fault *vmf,
Matthew Wilcox (Oracle)79598ce2021-07-29 14:57:01 -04002957 struct folio *folio)
Linus Torvaldsef00e082009-06-16 15:31:25 -07002958{
Josef Bacik2a1180f2019-03-13 11:44:18 -07002959 struct file *file = vmf->vma->vm_file;
2960 struct file_ra_state *ra = &file->f_ra;
Matthew Wilcox (Oracle)79598ce2021-07-29 14:57:01 -04002961 DEFINE_READAHEAD(ractl, file, ra, file->f_mapping, vmf->pgoff);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002962 struct file *fpin = NULL;
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07002963 unsigned int mmap_miss;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002964
2965 /* If we don't want any read-ahead, don't bother */
Jan Kara5c72fee2020-04-01 21:04:40 -07002966 if (vmf->vma->vm_flags & VM_RAND_READ || !ra->ra_pages)
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002967 return fpin;
Matthew Wilcox (Oracle)79598ce2021-07-29 14:57:01 -04002968
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07002969 mmap_miss = READ_ONCE(ra->mmap_miss);
2970 if (mmap_miss)
2971 WRITE_ONCE(ra->mmap_miss, --mmap_miss);
Matthew Wilcox (Oracle)79598ce2021-07-29 14:57:01 -04002972
2973 if (folio_test_readahead(folio)) {
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002974 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Matthew Wilcox (Oracle)79598ce2021-07-29 14:57:01 -04002975 page_cache_async_ra(&ractl, folio, ra->ra_pages);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07002976 }
2977 return fpin;
Linus Torvaldsef00e082009-06-16 15:31:25 -07002978}
2979
Randy Dunlap485bb992006-06-23 02:03:49 -07002980/**
Nick Piggin54cb8822007-07-19 01:46:59 -07002981 * filemap_fault - read in file data for page fault handling
Nick Piggind0217ac2007-07-19 01:47:03 -07002982 * @vmf: struct vm_fault containing details of the fault
Randy Dunlap485bb992006-06-23 02:03:49 -07002983 *
Nick Piggin54cb8822007-07-19 01:46:59 -07002984 * filemap_fault() is invoked via the vma operations vector for a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 * mapped memory region to read in file data during a page fault.
2986 *
2987 * The goto's are kind of ugly, but this streamlines the normal case of having
2988 * it in the page cache, and handles the special cases reasonably without
2989 * having a lot of duplicated code.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002990 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002991 * vma->vm_mm->mmap_lock must be held on entry.
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002992 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002993 * If our return value has VM_FAULT_RETRY set, it's because the mmap_lock
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05002994 * may be dropped before doing I/O or by lock_folio_maybe_drop_mmap().
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002995 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002996 * If our return value does not have VM_FAULT_RETRY set, the mmap_lock
Paul Cassella9a95f3c2014-08-06 16:07:24 -07002997 * has not been released.
2998 *
2999 * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
Mike Rapoporta862f682019-03-05 15:48:42 -08003000 *
3001 * Return: bitwise-OR of %VM_FAULT_ codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 */
Souptick Joarder2bcd6452018-06-07 17:08:00 -07003003vm_fault_t filemap_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
3005 int error;
Dave Jiang11bac802017-02-24 14:56:41 -08003006 struct file *file = vmf->vma->vm_file;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003007 struct file *fpin = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 struct inode *inode = mapping->host;
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003010 pgoff_t max_idx, index = vmf->pgoff;
3011 struct folio *folio;
Souptick Joarder2bcd6452018-06-07 17:08:00 -07003012 vm_fault_t ret = 0;
Jan Kara730633f2021-01-28 19:19:45 +01003013 bool mapping_locked = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003015 max_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3016 if (unlikely(index >= max_idx))
Linus Torvalds5307cc12007-10-31 09:19:46 -07003017 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 /*
Johannes Weiner49426422013-10-16 13:46:59 -07003020 * Do we have something in the page cache already?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 */
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003022 folio = filemap_get_folio(mapping, index);
3023 if (likely(folio)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 /*
Jan Kara730633f2021-01-28 19:19:45 +01003025 * We found the page, so try async readahead before waiting for
3026 * the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 */
Jan Kara730633f2021-01-28 19:19:45 +01003028 if (!(vmf->flags & FAULT_FLAG_TRIED))
Matthew Wilcox (Oracle)79598ce2021-07-29 14:57:01 -04003029 fpin = do_async_mmap_readahead(vmf, folio);
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003030 if (unlikely(!folio_test_uptodate(folio))) {
Jan Kara730633f2021-01-28 19:19:45 +01003031 filemap_invalidate_lock_shared(mapping);
3032 mapping_locked = true;
3033 }
3034 } else {
Linus Torvaldsef00e082009-06-16 15:31:25 -07003035 /* No page in the page cache at all */
Linus Torvaldsef00e082009-06-16 15:31:25 -07003036 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07003037 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Linus Torvaldsef00e082009-06-16 15:31:25 -07003038 ret = VM_FAULT_MAJOR;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003039 fpin = do_sync_mmap_readahead(vmf);
Linus Torvaldsef00e082009-06-16 15:31:25 -07003040retry_find:
Jan Kara730633f2021-01-28 19:19:45 +01003041 /*
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003042 * See comment in filemap_create_folio() why we need
Jan Kara730633f2021-01-28 19:19:45 +01003043 * invalidate_lock
3044 */
3045 if (!mapping_locked) {
3046 filemap_invalidate_lock_shared(mapping);
3047 mapping_locked = true;
3048 }
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003049 folio = __filemap_get_folio(mapping, index,
Josef Bacika75d4c32019-03-13 11:44:14 -07003050 FGP_CREAT|FGP_FOR_MMAP,
3051 vmf->gfp_mask);
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003052 if (!folio) {
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003053 if (fpin)
3054 goto out_retry;
Jan Kara730633f2021-01-28 19:19:45 +01003055 filemap_invalidate_unlock_shared(mapping);
Matthew Wilcox (Oracle)e520e932020-04-01 21:04:53 -07003056 return VM_FAULT_OOM;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 }
3059
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003060 if (!lock_folio_maybe_drop_mmap(vmf, folio, &fpin))
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003061 goto out_retry;
Michel Lespinasseb522c942010-10-26 14:21:56 -07003062
3063 /* Did it get truncated? */
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003064 if (unlikely(folio->mapping != mapping)) {
3065 folio_unlock(folio);
3066 folio_put(folio);
Michel Lespinasseb522c942010-10-26 14:21:56 -07003067 goto retry_find;
3068 }
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003069 VM_BUG_ON_FOLIO(!folio_contains(folio, index), folio);
Michel Lespinasseb522c942010-10-26 14:21:56 -07003070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 /*
Nick Piggind00806b2007-07-19 01:46:57 -07003072 * We have a locked page in the page cache, now we need to check
3073 * that it's up-to-date. If not, it is going to be due to an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 */
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003075 if (unlikely(!folio_test_uptodate(folio))) {
Jan Kara730633f2021-01-28 19:19:45 +01003076 /*
3077 * The page was in cache and uptodate and now it is not.
3078 * Strange but possible since we didn't hold the page lock all
3079 * the time. Let's drop everything get the invalidate lock and
3080 * try again.
3081 */
3082 if (!mapping_locked) {
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003083 folio_unlock(folio);
3084 folio_put(folio);
Jan Kara730633f2021-01-28 19:19:45 +01003085 goto retry_find;
3086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 goto page_not_uptodate;
Jan Kara730633f2021-01-28 19:19:45 +01003088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Linus Torvaldsef00e082009-06-16 15:31:25 -07003090 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003091 * We've made it this far and we had to drop our mmap_lock, now is the
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003092 * time to return to the upper layer and have it re-find the vma and
3093 * redo the fault.
3094 */
3095 if (fpin) {
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003096 folio_unlock(folio);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003097 goto out_retry;
3098 }
Jan Kara730633f2021-01-28 19:19:45 +01003099 if (mapping_locked)
3100 filemap_invalidate_unlock_shared(mapping);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003101
3102 /*
Linus Torvaldsef00e082009-06-16 15:31:25 -07003103 * Found the page and have a reference on it.
3104 * We must recheck i_size under page lock.
3105 */
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003106 max_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3107 if (unlikely(index >= max_idx)) {
3108 folio_unlock(folio);
3109 folio_put(folio);
Linus Torvalds5307cc12007-10-31 09:19:46 -07003110 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07003111 }
3112
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003113 vmf->page = folio_file_page(folio, index);
Nick Piggin83c54072007-07-19 01:47:05 -07003114 return ret | VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116page_not_uptodate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 /*
3118 * Umm, take care of errors if the page isn't up-to-date.
3119 * Try to re-read it _once_. We do this synchronously,
3120 * because there really aren't any performance issues here
3121 * and we need to check for errors.
3122 */
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003123 fpin = maybe_unlock_mmap_for_io(vmf, fpin);
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003124 error = filemap_read_folio(file, mapping, folio);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003125 if (fpin)
3126 goto out_retry;
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003127 folio_put(folio);
Nick Piggind00806b2007-07-19 01:46:57 -07003128
3129 if (!error || error == AOP_TRUNCATED_PAGE)
3130 goto retry_find;
Jan Kara730633f2021-01-28 19:19:45 +01003131 filemap_invalidate_unlock_shared(mapping);
Nick Piggind00806b2007-07-19 01:46:57 -07003132
Nick Piggind0217ac2007-07-19 01:47:03 -07003133 return VM_FAULT_SIGBUS;
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003134
3135out_retry:
3136 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003137 * We dropped the mmap_lock, we need to return to the fault handler to
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003138 * re-find the vma and come back and find our hopefully still populated
3139 * page.
3140 */
Matthew Wilcox (Oracle)e292e6d2021-03-10 10:46:41 -05003141 if (folio)
3142 folio_put(folio);
Jan Kara730633f2021-01-28 19:19:45 +01003143 if (mapping_locked)
3144 filemap_invalidate_unlock_shared(mapping);
Josef Bacik6b4c9f42019-03-13 11:44:22 -07003145 if (fpin)
3146 fput(fpin);
3147 return ret | VM_FAULT_RETRY;
Nick Piggin54cb8822007-07-19 01:46:59 -07003148}
3149EXPORT_SYMBOL(filemap_fault);
3150
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003151static bool filemap_map_pmd(struct vm_fault *vmf, struct page *page)
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003152{
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003153 struct mm_struct *mm = vmf->vma->vm_mm;
3154
3155 /* Huge page is mapped? No need to proceed. */
3156 if (pmd_trans_huge(*vmf->pmd)) {
3157 unlock_page(page);
3158 put_page(page);
3159 return true;
3160 }
3161
3162 if (pmd_none(*vmf->pmd) && PageTransHuge(page)) {
Yang Shie0f43fa2021-11-05 13:41:04 -07003163 vm_fault_t ret = do_set_pmd(vmf, page);
3164 if (!ret) {
3165 /* The page is mapped successfully, reference consumed. */
3166 unlock_page(page);
3167 return true;
3168 }
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003169 }
3170
Qi Zheng03c4f202021-11-05 13:38:38 -07003171 if (pmd_none(*vmf->pmd))
3172 pmd_install(mm, vmf->pmd, &vmf->prealloc_pte);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003173
3174 /* See comment in handle_pte_fault() */
3175 if (pmd_devmap_trans_unstable(vmf->pmd)) {
3176 unlock_page(page);
3177 put_page(page);
3178 return true;
3179 }
3180
3181 return false;
3182}
3183
Matthew Wilcox (Oracle)820b05e2021-03-12 23:46:45 -05003184static struct folio *next_uptodate_page(struct folio *folio,
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003185 struct address_space *mapping,
3186 struct xa_state *xas, pgoff_t end_pgoff)
3187{
3188 unsigned long max_idx;
3189
3190 do {
Matthew Wilcox (Oracle)9184a302021-03-12 23:33:43 -05003191 if (!folio)
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003192 return NULL;
Matthew Wilcox (Oracle)9184a302021-03-12 23:33:43 -05003193 if (xas_retry(xas, folio))
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003194 continue;
Matthew Wilcox (Oracle)9184a302021-03-12 23:33:43 -05003195 if (xa_is_value(folio))
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003196 continue;
Matthew Wilcox (Oracle)9184a302021-03-12 23:33:43 -05003197 if (folio_test_locked(folio))
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003198 continue;
Matthew Wilcox (Oracle)9184a302021-03-12 23:33:43 -05003199 if (!folio_try_get_rcu(folio))
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003200 continue;
3201 /* Has the page moved or been split? */
Matthew Wilcox (Oracle)9184a302021-03-12 23:33:43 -05003202 if (unlikely(folio != xas_reload(xas)))
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003203 goto skip;
Matthew Wilcox (Oracle)9184a302021-03-12 23:33:43 -05003204 if (!folio_test_uptodate(folio) || folio_test_readahead(folio))
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003205 goto skip;
Matthew Wilcox (Oracle)9184a302021-03-12 23:33:43 -05003206 if (!folio_trylock(folio))
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003207 goto skip;
Matthew Wilcox (Oracle)9184a302021-03-12 23:33:43 -05003208 if (folio->mapping != mapping)
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003209 goto unlock;
Matthew Wilcox (Oracle)9184a302021-03-12 23:33:43 -05003210 if (!folio_test_uptodate(folio))
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003211 goto unlock;
3212 max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
3213 if (xas->xa_index >= max_idx)
3214 goto unlock;
Matthew Wilcox (Oracle)820b05e2021-03-12 23:46:45 -05003215 return folio;
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003216unlock:
Matthew Wilcox (Oracle)9184a302021-03-12 23:33:43 -05003217 folio_unlock(folio);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003218skip:
Matthew Wilcox (Oracle)9184a302021-03-12 23:33:43 -05003219 folio_put(folio);
3220 } while ((folio = xas_next_entry(xas, end_pgoff)) != NULL);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003221
3222 return NULL;
3223}
3224
Matthew Wilcox (Oracle)820b05e2021-03-12 23:46:45 -05003225static inline struct folio *first_map_page(struct address_space *mapping,
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003226 struct xa_state *xas,
3227 pgoff_t end_pgoff)
3228{
3229 return next_uptodate_page(xas_find(xas, end_pgoff),
3230 mapping, xas, end_pgoff);
3231}
3232
Matthew Wilcox (Oracle)820b05e2021-03-12 23:46:45 -05003233static inline struct folio *next_map_page(struct address_space *mapping,
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003234 struct xa_state *xas,
3235 pgoff_t end_pgoff)
3236{
3237 return next_uptodate_page(xas_next_entry(xas, end_pgoff),
3238 mapping, xas, end_pgoff);
3239}
3240
3241vm_fault_t filemap_map_pages(struct vm_fault *vmf,
3242 pgoff_t start_pgoff, pgoff_t end_pgoff)
3243{
3244 struct vm_area_struct *vma = vmf->vma;
3245 struct file *file = vma->vm_file;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003246 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07003247 pgoff_t last_pgoff = start_pgoff;
Will Deacon9d3af4b2021-01-14 15:24:19 +00003248 unsigned long addr;
Matthew Wilcox070e8072018-05-17 00:08:30 -04003249 XA_STATE(xas, &mapping->i_pages, start_pgoff);
Matthew Wilcox (Oracle)820b05e2021-03-12 23:46:45 -05003250 struct folio *folio;
3251 struct page *page;
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07003252 unsigned int mmap_miss = READ_ONCE(file->f_ra.mmap_miss);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003253 vm_fault_t ret = 0;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003254
3255 rcu_read_lock();
Matthew Wilcox (Oracle)820b05e2021-03-12 23:46:45 -05003256 folio = first_map_page(mapping, &xas, end_pgoff);
3257 if (!folio)
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003258 goto out;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003259
Matthew Wilcox (Oracle)820b05e2021-03-12 23:46:45 -05003260 if (filemap_map_pmd(vmf, &folio->page)) {
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003261 ret = VM_FAULT_NOPAGE;
3262 goto out;
3263 }
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003264
Will Deacon9d3af4b2021-01-14 15:24:19 +00003265 addr = vma->vm_start + ((start_pgoff - vma->vm_pgoff) << PAGE_SHIFT);
3266 vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, addr, &vmf->ptl);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003267 do {
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -04003268again:
Matthew Wilcox (Oracle)820b05e2021-03-12 23:46:45 -05003269 page = folio_file_page(folio, xas.xa_index);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003270 if (PageHWPoison(page))
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003271 goto unlock;
3272
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07003273 if (mmap_miss > 0)
3274 mmap_miss--;
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003275
Will Deacon9d3af4b2021-01-14 15:24:19 +00003276 addr += (xas.xa_index - last_pgoff) << PAGE_SHIFT;
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003277 vmf->pte += xas.xa_index - last_pgoff;
Matthew Wilcox070e8072018-05-17 00:08:30 -04003278 last_pgoff = xas.xa_index;
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003279
3280 if (!pte_none(*vmf->pte))
Kirill A. Shutemov7267ec002016-07-26 15:25:23 -07003281 goto unlock;
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003282
Will Deacon46bdb422020-11-24 18:48:26 +00003283 /* We're about to handle the fault */
Will Deacon9d3af4b2021-01-14 15:24:19 +00003284 if (vmf->address == addr)
Will Deacon46bdb422020-11-24 18:48:26 +00003285 ret = VM_FAULT_NOPAGE;
Will Deacon46bdb422020-11-24 18:48:26 +00003286
Will Deacon9d3af4b2021-01-14 15:24:19 +00003287 do_set_pte(vmf, page, addr);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003288 /* no need to invalidate: a not-present page won't be cached */
Will Deacon9d3af4b2021-01-14 15:24:19 +00003289 update_mmu_cache(vma, addr, vmf->pte);
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -04003290 if (folio_more_pages(folio, xas.xa_index, end_pgoff)) {
3291 xas.xa_index++;
3292 folio_ref_inc(folio);
3293 goto again;
3294 }
Matthew Wilcox (Oracle)820b05e2021-03-12 23:46:45 -05003295 folio_unlock(folio);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003296 continue;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003297unlock:
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -04003298 if (folio_more_pages(folio, xas.xa_index, end_pgoff)) {
3299 xas.xa_index++;
3300 goto again;
3301 }
Matthew Wilcox (Oracle)820b05e2021-03-12 23:46:45 -05003302 folio_unlock(folio);
3303 folio_put(folio);
3304 } while ((folio = next_map_page(mapping, &xas, end_pgoff)) != NULL);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003305 pte_unmap_unlock(vmf->pte, vmf->ptl);
3306out:
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003307 rcu_read_unlock();
Kirill A. Shutemove630bfa2020-08-14 17:31:27 -07003308 WRITE_ONCE(file->f_ra.mmap_miss, mmap_miss);
Kirill A. Shutemovf9ce0be2020-12-19 15:19:23 +03003309 return ret;
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003310}
3311EXPORT_SYMBOL(filemap_map_pages);
3312
Souptick Joarder2bcd6452018-06-07 17:08:00 -07003313vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Jan Kara4fcf1c62012-06-12 16:20:29 +02003314{
Christoph Hellwig5df1a672020-11-16 14:33:37 +01003315 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
Matthew Wilcox (Oracle)960ea972021-03-12 23:57:44 -05003316 struct folio *folio = page_folio(vmf->page);
Souptick Joarder2bcd6452018-06-07 17:08:00 -07003317 vm_fault_t ret = VM_FAULT_LOCKED;
Jan Kara4fcf1c62012-06-12 16:20:29 +02003318
Christoph Hellwig5df1a672020-11-16 14:33:37 +01003319 sb_start_pagefault(mapping->host->i_sb);
Dave Jiang11bac802017-02-24 14:56:41 -08003320 file_update_time(vmf->vma->vm_file);
Matthew Wilcox (Oracle)960ea972021-03-12 23:57:44 -05003321 folio_lock(folio);
3322 if (folio->mapping != mapping) {
3323 folio_unlock(folio);
Jan Kara4fcf1c62012-06-12 16:20:29 +02003324 ret = VM_FAULT_NOPAGE;
3325 goto out;
3326 }
Jan Kara14da9202012-06-12 16:20:37 +02003327 /*
Matthew Wilcox (Oracle)960ea972021-03-12 23:57:44 -05003328 * We mark the folio dirty already here so that when freeze is in
Jan Kara14da9202012-06-12 16:20:37 +02003329 * progress, we are guaranteed that writeback during freezing will
Matthew Wilcox (Oracle)960ea972021-03-12 23:57:44 -05003330 * see the dirty folio and writeprotect it again.
Jan Kara14da9202012-06-12 16:20:37 +02003331 */
Matthew Wilcox (Oracle)960ea972021-03-12 23:57:44 -05003332 folio_mark_dirty(folio);
3333 folio_wait_stable(folio);
Jan Kara4fcf1c62012-06-12 16:20:29 +02003334out:
Christoph Hellwig5df1a672020-11-16 14:33:37 +01003335 sb_end_pagefault(mapping->host->i_sb);
Jan Kara4fcf1c62012-06-12 16:20:29 +02003336 return ret;
3337}
Jan Kara4fcf1c62012-06-12 16:20:29 +02003338
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003339const struct vm_operations_struct generic_file_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003340 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003341 .map_pages = filemap_map_pages,
Jan Kara4fcf1c62012-06-12 16:20:29 +02003342 .page_mkwrite = filemap_page_mkwrite,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343};
3344
3345/* This is used for a general mmap of a disk file */
3346
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -07003347int generic_file_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
3349 struct address_space *mapping = file->f_mapping;
3350
3351 if (!mapping->a_ops->readpage)
3352 return -ENOEXEC;
3353 file_accessed(file);
3354 vma->vm_ops = &generic_file_vm_ops;
3355 return 0;
3356}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
3358/*
3359 * This is for filesystems which do not implement ->writepage.
3360 */
3361int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
3362{
3363 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
3364 return -EINVAL;
3365 return generic_file_mmap(file, vma);
3366}
3367#else
Souptick Joarder4b96a372018-10-26 15:04:03 -07003368vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
Arnd Bergmann453972282018-04-13 15:35:27 -07003369{
Souptick Joarder4b96a372018-10-26 15:04:03 -07003370 return VM_FAULT_SIGBUS;
Arnd Bergmann453972282018-04-13 15:35:27 -07003371}
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -07003372int generic_file_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373{
3374 return -ENOSYS;
3375}
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -07003376int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
3378 return -ENOSYS;
3379}
3380#endif /* CONFIG_MMU */
3381
Arnd Bergmann453972282018-04-13 15:35:27 -07003382EXPORT_SYMBOL(filemap_page_mkwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383EXPORT_SYMBOL(generic_file_mmap);
3384EXPORT_SYMBOL(generic_file_readonly_mmap);
3385
Matthew Wilcox (Oracle)539a3322020-12-16 11:45:30 -05003386static struct folio *do_read_cache_folio(struct address_space *mapping,
3387 pgoff_t index, filler_t filler, void *data, gfp_t gfp)
Sasha Levin67f9fd92014-04-03 14:48:18 -07003388{
Matthew Wilcox (Oracle)539a3322020-12-16 11:45:30 -05003389 struct folio *folio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 int err;
3391repeat:
Matthew Wilcox (Oracle)539a3322020-12-16 11:45:30 -05003392 folio = filemap_get_folio(mapping, index);
3393 if (!folio) {
3394 folio = filemap_alloc_folio(gfp, 0);
3395 if (!folio)
Nick Piggineb2be182007-10-16 01:24:57 -07003396 return ERR_PTR(-ENOMEM);
Matthew Wilcox (Oracle)539a3322020-12-16 11:45:30 -05003397 err = filemap_add_folio(mapping, folio, index, gfp);
Nick Piggineb2be182007-10-16 01:24:57 -07003398 if (unlikely(err)) {
Matthew Wilcox (Oracle)539a3322020-12-16 11:45:30 -05003399 folio_put(folio);
Nick Piggineb2be182007-10-16 01:24:57 -07003400 if (err == -EEXIST)
3401 goto repeat;
Matthew Wilcox22ecdb42017-12-04 04:02:00 -05003402 /* Presumably ENOMEM for xarray node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 return ERR_PTR(err);
3404 }
Mel Gorman32b63522016-03-15 14:55:36 -07003405
3406filler:
Christoph Hellwig6c45b452019-07-11 20:55:20 -07003407 if (filler)
Matthew Wilcox (Oracle)539a3322020-12-16 11:45:30 -05003408 err = filler(data, &folio->page);
Christoph Hellwig6c45b452019-07-11 20:55:20 -07003409 else
Matthew Wilcox (Oracle)539a3322020-12-16 11:45:30 -05003410 err = mapping->a_ops->readpage(data, &folio->page);
Christoph Hellwig6c45b452019-07-11 20:55:20 -07003411
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 if (err < 0) {
Matthew Wilcox (Oracle)539a3322020-12-16 11:45:30 -05003413 folio_put(folio);
Mel Gorman32b63522016-03-15 14:55:36 -07003414 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 }
Mel Gorman32b63522016-03-15 14:55:36 -07003416
Matthew Wilcox (Oracle)539a3322020-12-16 11:45:30 -05003417 folio_wait_locked(folio);
3418 if (!folio_test_uptodate(folio)) {
3419 folio_put(folio);
3420 return ERR_PTR(-EIO);
3421 }
3422
Mel Gorman32b63522016-03-15 14:55:36 -07003423 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 }
Matthew Wilcox (Oracle)539a3322020-12-16 11:45:30 -05003425 if (folio_test_uptodate(folio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 goto out;
3427
Matthew Wilcox (Oracle)81f4c032021-12-23 15:17:28 -05003428 if (!folio_trylock(folio)) {
3429 folio_put_wait_locked(folio, TASK_UNINTERRUPTIBLE);
3430 goto repeat;
3431 }
Mel Gormanebded022016-03-15 14:55:39 -07003432
Matthew Wilcox (Oracle)81f4c032021-12-23 15:17:28 -05003433 /* Folio was truncated from mapping */
Matthew Wilcox (Oracle)539a3322020-12-16 11:45:30 -05003434 if (!folio->mapping) {
3435 folio_unlock(folio);
3436 folio_put(folio);
Mel Gorman32b63522016-03-15 14:55:36 -07003437 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 }
Mel Gormanebded022016-03-15 14:55:39 -07003439
3440 /* Someone else locked and filled the page in a very small window */
Matthew Wilcox (Oracle)539a3322020-12-16 11:45:30 -05003441 if (folio_test_uptodate(folio)) {
3442 folio_unlock(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 goto out;
3444 }
Xianting Tianfaffdfa2020-04-01 21:04:47 -07003445
3446 /*
3447 * A previous I/O error may have been due to temporary
3448 * failures.
3449 * Clear page error before actual read, PG_error will be
3450 * set again if read page fails.
3451 */
Matthew Wilcox (Oracle)539a3322020-12-16 11:45:30 -05003452 folio_clear_error(folio);
Mel Gorman32b63522016-03-15 14:55:36 -07003453 goto filler;
3454
David Howellsc855ff32007-05-09 13:42:20 +01003455out:
Matthew Wilcox (Oracle)539a3322020-12-16 11:45:30 -05003456 folio_mark_accessed(folio);
3457 return folio;
Nick Piggin6fe69002007-05-06 14:49:04 -07003458}
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003459
3460/**
Matthew Wilcox (Oracle)539a3322020-12-16 11:45:30 -05003461 * read_cache_folio - read into page cache, fill it if needed
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003462 * @mapping: the page's address_space
3463 * @index: the page index
3464 * @filler: function to perform the read
Hugh Dickins5e5358e2011-07-25 17:12:23 -07003465 * @data: first arg to filler(data, page) function, often left as NULL
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003466 *
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003467 * Read into the page cache. If a page already exists, and PageUptodate() is
Sasha Levin67f9fd92014-04-03 14:48:18 -07003468 * not set, try to fill the page and wait for it to become unlocked.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003469 *
3470 * If the page does not get brought uptodate, return -EIO.
Mike Rapoporta862f682019-03-05 15:48:42 -08003471 *
Jan Kara730633f2021-01-28 19:19:45 +01003472 * The function expects mapping->invalidate_lock to be already held.
3473 *
Mike Rapoporta862f682019-03-05 15:48:42 -08003474 * Return: up to date page on success, ERR_PTR() on failure.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003475 */
Matthew Wilcox (Oracle)539a3322020-12-16 11:45:30 -05003476struct folio *read_cache_folio(struct address_space *mapping, pgoff_t index,
3477 filler_t filler, void *data)
3478{
3479 return do_read_cache_folio(mapping, index, filler, data,
3480 mapping_gfp_mask(mapping));
3481}
3482EXPORT_SYMBOL(read_cache_folio);
3483
3484static struct page *do_read_cache_page(struct address_space *mapping,
3485 pgoff_t index, filler_t *filler, void *data, gfp_t gfp)
3486{
3487 struct folio *folio;
3488
3489 folio = do_read_cache_folio(mapping, index, filler, data, gfp);
3490 if (IS_ERR(folio))
3491 return &folio->page;
3492 return folio_file_page(folio, index);
3493}
3494
Sasha Levin67f9fd92014-04-03 14:48:18 -07003495struct page *read_cache_page(struct address_space *mapping,
Matthew Wilcox (Oracle)539a3322020-12-16 11:45:30 -05003496 pgoff_t index, filler_t *filler, void *data)
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003497{
Christoph Hellwigd322a8e2019-07-11 20:55:17 -07003498 return do_read_cache_page(mapping, index, filler, data,
3499 mapping_gfp_mask(mapping));
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003500}
Sasha Levin67f9fd92014-04-03 14:48:18 -07003501EXPORT_SYMBOL(read_cache_page);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003502
3503/**
3504 * read_cache_page_gfp - read into page cache, using specified page allocation flags.
3505 * @mapping: the page's address_space
3506 * @index: the page index
3507 * @gfp: the page allocator flags to use if allocating
3508 *
3509 * This is the same as "read_mapping_page(mapping, index, NULL)", but with
Dave Kleikampe6f67b82011-12-21 11:05:48 -06003510 * any new page allocations done using the specified allocation flags.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003511 *
3512 * If the page does not get brought uptodate, return -EIO.
Mike Rapoporta862f682019-03-05 15:48:42 -08003513 *
Jan Kara730633f2021-01-28 19:19:45 +01003514 * The function expects mapping->invalidate_lock to be already held.
3515 *
Mike Rapoporta862f682019-03-05 15:48:42 -08003516 * Return: up to date page on success, ERR_PTR() on failure.
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003517 */
3518struct page *read_cache_page_gfp(struct address_space *mapping,
3519 pgoff_t index,
3520 gfp_t gfp)
3521{
Christoph Hellwig6c45b452019-07-11 20:55:20 -07003522 return do_read_cache_page(mapping, index, NULL, NULL, gfp);
Linus Torvalds0531b2a2010-01-27 09:20:03 -08003523}
3524EXPORT_SYMBOL(read_cache_page_gfp);
3525
Nick Pigginafddba42007-10-16 01:25:01 -07003526int pagecache_write_begin(struct file *file, struct address_space *mapping,
3527 loff_t pos, unsigned len, unsigned flags,
3528 struct page **pagep, void **fsdata)
3529{
3530 const struct address_space_operations *aops = mapping->a_ops;
3531
Nick Piggin4e02ed42008-10-29 14:00:55 -07003532 return aops->write_begin(file, mapping, pos, len, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003533 pagep, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003534}
3535EXPORT_SYMBOL(pagecache_write_begin);
3536
3537int pagecache_write_end(struct file *file, struct address_space *mapping,
3538 loff_t pos, unsigned len, unsigned copied,
3539 struct page *page, void *fsdata)
3540{
3541 const struct address_space_operations *aops = mapping->a_ops;
Nick Pigginafddba42007-10-16 01:25:01 -07003542
Nick Piggin4e02ed42008-10-29 14:00:55 -07003543 return aops->write_end(file, mapping, pos, len, copied, page, fsdata);
Nick Pigginafddba42007-10-16 01:25:01 -07003544}
3545EXPORT_SYMBOL(pagecache_write_end);
3546
Konstantin Khlebnikova92853b2019-11-30 17:49:44 -08003547/*
3548 * Warn about a page cache invalidation failure during a direct I/O write.
3549 */
3550void dio_warn_stale_pagecache(struct file *filp)
3551{
3552 static DEFINE_RATELIMIT_STATE(_rs, 86400 * HZ, DEFAULT_RATELIMIT_BURST);
3553 char pathname[128];
Konstantin Khlebnikova92853b2019-11-30 17:49:44 -08003554 char *path;
3555
Christoph Hellwig5df1a672020-11-16 14:33:37 +01003556 errseq_set(&filp->f_mapping->wb_err, -EIO);
Konstantin Khlebnikova92853b2019-11-30 17:49:44 -08003557 if (__ratelimit(&_rs)) {
3558 path = file_path(filp, pathname, sizeof(pathname));
3559 if (IS_ERR(path))
3560 path = "(unknown)";
3561 pr_crit("Page cache invalidation failure on direct I/O. Possible data corruption due to collision with buffered I/O!\n");
3562 pr_crit("File: %s PID: %d Comm: %.20s\n", path, current->pid,
3563 current->comm);
3564 }
3565}
3566
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567ssize_t
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003568generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569{
3570 struct file *file = iocb->ki_filp;
3571 struct address_space *mapping = file->f_mapping;
3572 struct inode *inode = mapping->host;
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003573 loff_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 ssize_t written;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003575 size_t write_len;
3576 pgoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Al Viro0c949332014-03-22 06:51:37 -04003578 write_len = iov_iter_count(from);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003579 end = (pos + write_len - 1) >> PAGE_SHIFT;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003580
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003581 if (iocb->ki_flags & IOCB_NOWAIT) {
3582 /* If there are pages to writeback, return */
Christoph Hellwig5df1a672020-11-16 14:33:37 +01003583 if (filemap_range_has_page(file->f_mapping, pos,
zhengbin35f12f02019-03-05 15:44:21 -08003584 pos + write_len - 1))
Goldwyn Rodrigues6be96d32017-06-20 07:05:44 -05003585 return -EAGAIN;
3586 } else {
3587 written = filemap_write_and_wait_range(mapping, pos,
3588 pos + write_len - 1);
3589 if (written)
3590 goto out;
3591 }
Christoph Hellwiga969e902008-07-23 21:27:04 -07003592
3593 /*
3594 * After a write we want buffered reads to be sure to go to disk to get
3595 * the new data. We invalidate clean cached page from the region we're
3596 * about to write. We do this *before* the write so that we can return
Hisashi Hifumi6ccfa802008-09-02 14:35:40 -07003597 * without clobbering -EIOCBQUEUED from ->direct_IO().
Christoph Hellwiga969e902008-07-23 21:27:04 -07003598 */
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003599 written = invalidate_inode_pages2_range(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003600 pos >> PAGE_SHIFT, end);
Andrey Ryabinin55635ba2017-05-03 14:55:59 -07003601 /*
3602 * If a page can not be invalidated, return 0 to fall back
3603 * to buffered write.
3604 */
3605 if (written) {
3606 if (written == -EBUSY)
3607 return 0;
3608 goto out;
Christoph Hellwiga969e902008-07-23 21:27:04 -07003609 }
3610
Al Viro639a93a52017-04-13 14:10:15 -04003611 written = mapping->a_ops->direct_IO(iocb, from);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003612
3613 /*
3614 * Finally, try again to invalidate clean pages which might have been
3615 * cached by non-direct readahead, or faulted in by get_user_pages()
3616 * if the source of the write was an mmap'ed region of the file
3617 * we're writing. Either one is a pretty crazy thing to do,
3618 * so we don't support it 100%. If this invalidation
3619 * fails, tough, the write still worked...
Lukas Czerner332391a2017-09-21 08:16:29 -06003620 *
3621 * Most of the time we do not need this since dio_complete() will do
3622 * the invalidation for us. However there are some file systems that
3623 * do not end up with dio_complete() being called, so let's not break
Konstantin Khlebnikov80c1fe92019-11-30 17:49:41 -08003624 * them by removing it completely.
3625 *
Konstantin Khlebnikov9266a142019-11-30 17:49:47 -08003626 * Noticeable example is a blkdev_direct_IO().
3627 *
Konstantin Khlebnikov80c1fe92019-11-30 17:49:41 -08003628 * Skip invalidation for async writes or if mapping has no pages.
Christoph Hellwiga969e902008-07-23 21:27:04 -07003629 */
Konstantin Khlebnikov9266a142019-11-30 17:49:47 -08003630 if (written > 0 && mapping->nrpages &&
3631 invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT, end))
3632 dio_warn_stale_pagecache(file);
Christoph Hellwiga969e902008-07-23 21:27:04 -07003633
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 if (written > 0) {
Namhyung Kim01166512010-10-26 14:21:58 -07003635 pos += written;
Al Viro639a93a52017-04-13 14:10:15 -04003636 write_len -= written;
Namhyung Kim01166512010-10-26 14:21:58 -07003637 if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
3638 i_size_write(inode, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 mark_inode_dirty(inode);
3640 }
Al Viro5cb6c6c2014-02-11 20:58:20 -05003641 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 }
Pavel Begunkovab2125d2021-02-24 12:01:45 -08003643 if (written != -EIOCBQUEUED)
3644 iov_iter_revert(from, write_len - iov_iter_count(from));
Christoph Hellwiga969e902008-07-23 21:27:04 -07003645out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 return written;
3647}
3648EXPORT_SYMBOL(generic_file_direct_write);
3649
Al Viro3b93f912014-02-11 21:34:08 -05003650ssize_t generic_perform_write(struct file *file,
Nick Pigginafddba42007-10-16 01:25:01 -07003651 struct iov_iter *i, loff_t pos)
3652{
3653 struct address_space *mapping = file->f_mapping;
3654 const struct address_space_operations *a_ops = mapping->a_ops;
3655 long status = 0;
3656 ssize_t written = 0;
Nick Piggin674b8922007-10-16 01:25:03 -07003657 unsigned int flags = 0;
3658
Nick Pigginafddba42007-10-16 01:25:01 -07003659 do {
3660 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003661 unsigned long offset; /* Offset into pagecache page */
3662 unsigned long bytes; /* Bytes to write to page */
3663 size_t copied; /* Bytes copied from user */
3664 void *fsdata;
3665
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003666 offset = (pos & (PAGE_SIZE - 1));
3667 bytes = min_t(unsigned long, PAGE_SIZE - offset,
Nick Pigginafddba42007-10-16 01:25:01 -07003668 iov_iter_count(i));
3669
3670again:
Linus Torvalds00a3d662015-10-07 08:32:38 +01003671 /*
3672 * Bring in the user page that we will copy from _first_.
3673 * Otherwise there's a nasty deadlock on copying from the
3674 * same page as we're writing to, without it being marked
3675 * up-to-date.
Linus Torvalds00a3d662015-10-07 08:32:38 +01003676 */
Andreas Gruenbachera6294592021-08-02 14:54:16 +02003677 if (unlikely(fault_in_iov_iter_readable(i, bytes))) {
Linus Torvalds00a3d662015-10-07 08:32:38 +01003678 status = -EFAULT;
3679 break;
3680 }
3681
Jan Kara296291c2015-10-22 13:32:21 -07003682 if (fatal_signal_pending(current)) {
3683 status = -EINTR;
3684 break;
3685 }
3686
Nick Piggin674b8922007-10-16 01:25:03 -07003687 status = a_ops->write_begin(file, mapping, pos, bytes, flags,
Nick Pigginafddba42007-10-16 01:25:01 -07003688 &page, &fsdata);
Mel Gorman2457aec2014-06-04 16:10:31 -07003689 if (unlikely(status < 0))
Nick Pigginafddba42007-10-16 01:25:01 -07003690 break;
3691
anfei zhou931e80e2010-02-02 13:44:02 -08003692 if (mapping_writably_mapped(mapping))
3693 flush_dcache_page(page);
Linus Torvalds00a3d662015-10-07 08:32:38 +01003694
Al Virof0b65f32021-04-30 10:26:41 -04003695 copied = copy_page_from_iter_atomic(page, offset, bytes, i);
Nick Pigginafddba42007-10-16 01:25:01 -07003696 flush_dcache_page(page);
3697
3698 status = a_ops->write_end(file, mapping, pos, bytes, copied,
3699 page, fsdata);
Al Virof0b65f32021-04-30 10:26:41 -04003700 if (unlikely(status != copied)) {
3701 iov_iter_revert(i, copied - max(status, 0L));
3702 if (unlikely(status < 0))
3703 break;
3704 }
Nick Pigginafddba42007-10-16 01:25:01 -07003705 cond_resched();
3706
Al Virobc1bb412021-05-31 00:32:44 -04003707 if (unlikely(status == 0)) {
Nick Pigginafddba42007-10-16 01:25:01 -07003708 /*
Al Virobc1bb412021-05-31 00:32:44 -04003709 * A short copy made ->write_end() reject the
3710 * thing entirely. Might be memory poisoning
3711 * halfway through, might be a race with munmap,
3712 * might be severe memory pressure.
Nick Pigginafddba42007-10-16 01:25:01 -07003713 */
Al Virobc1bb412021-05-31 00:32:44 -04003714 if (copied)
3715 bytes = copied;
Nick Pigginafddba42007-10-16 01:25:01 -07003716 goto again;
3717 }
Al Virof0b65f32021-04-30 10:26:41 -04003718 pos += status;
3719 written += status;
Nick Pigginafddba42007-10-16 01:25:01 -07003720
3721 balance_dirty_pages_ratelimited(mapping);
Nick Pigginafddba42007-10-16 01:25:01 -07003722 } while (iov_iter_count(i));
3723
3724 return written ? written : status;
3725}
Al Viro3b93f912014-02-11 21:34:08 -05003726EXPORT_SYMBOL(generic_perform_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
Jan Karae4dd9de2009-08-17 18:10:06 +02003728/**
Al Viro81742022014-04-03 03:17:43 -04003729 * __generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003730 * @iocb: IO state structure (file, offset, etc.)
Al Viro81742022014-04-03 03:17:43 -04003731 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003732 *
3733 * This function does all the work needed for actually writing data to a
3734 * file. It does all basic checks, removes SUID from the file, updates
3735 * modification times and calls proper subroutines depending on whether we
3736 * do direct IO or a standard buffered write.
3737 *
Jan Kara96087032021-04-12 15:50:21 +02003738 * It expects i_rwsem to be grabbed unless we work on a block device or similar
Jan Karae4dd9de2009-08-17 18:10:06 +02003739 * object which does not need locking at all.
3740 *
3741 * This function does *not* take care of syncing data in case of O_SYNC write.
3742 * A caller has to handle it. This is mainly due to the fact that we want to
Jan Kara96087032021-04-12 15:50:21 +02003743 * avoid syncing under i_rwsem.
Mike Rapoporta862f682019-03-05 15:48:42 -08003744 *
3745 * Return:
3746 * * number of bytes written, even for truncated writes
3747 * * negative error code if no data has been written at all
Jan Karae4dd9de2009-08-17 18:10:06 +02003748 */
Al Viro81742022014-04-03 03:17:43 -04003749ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
3751 struct file *file = iocb->ki_filp;
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -07003752 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 struct inode *inode = mapping->host;
Al Viro3b93f912014-02-11 21:34:08 -05003754 ssize_t written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 ssize_t err;
Al Viro3b93f912014-02-11 21:34:08 -05003756 ssize_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 /* We can write back this queue in page reclaim */
Christoph Hellwigde1414a2015-01-14 10:42:36 +01003759 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02003760 err = file_remove_privs(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 if (err)
3762 goto out;
3763
Josef Bacikc3b2da32012-03-26 09:59:21 -04003764 err = file_update_time(file);
3765 if (err)
3766 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Al Viro2ba48ce2015-04-09 13:52:01 -04003768 if (iocb->ki_flags & IOCB_DIRECT) {
Al Viro0b8def92015-04-07 10:22:53 -04003769 loff_t pos, endbyte;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003770
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07003771 written = generic_file_direct_write(iocb, from);
Matthew Wilcoxfbbbad42015-02-16 15:58:53 -08003772 /*
3773 * If the write stopped short of completing, fall back to
3774 * buffered writes. Some filesystems do this for writes to
3775 * holes, for example. For DAX files, a buffered write will
3776 * not succeed (even if it did, DAX does not handle dirty
3777 * page-cache pages correctly).
3778 */
Al Viro0b8def92015-04-07 10:22:53 -04003779 if (written < 0 || !iov_iter_count(from) || IS_DAX(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 goto out;
Al Viro3b93f912014-02-11 21:34:08 -05003781
Al Viro0b8def92015-04-07 10:22:53 -04003782 status = generic_perform_write(file, from, pos = iocb->ki_pos);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003783 /*
Al Viro3b93f912014-02-11 21:34:08 -05003784 * If generic_perform_write() returned a synchronous error
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003785 * then we want to return the number of bytes which were
3786 * direct-written, or the error code if that was zero. Note
3787 * that this differs from normal direct-io semantics, which
3788 * will return -EFOO even if some bytes were written.
3789 */
Al Viro60bb4522014-08-08 12:39:16 -04003790 if (unlikely(status < 0)) {
Al Viro3b93f912014-02-11 21:34:08 -05003791 err = status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003792 goto out;
3793 }
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003794 /*
3795 * We need to ensure that the page cache pages are written to
3796 * disk and invalidated to preserve the expected O_DIRECT
3797 * semantics.
3798 */
Al Viro3b93f912014-02-11 21:34:08 -05003799 endbyte = pos + status - 1;
Al Viro0b8def92015-04-07 10:22:53 -04003800 err = filemap_write_and_wait_range(mapping, pos, endbyte);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003801 if (err == 0) {
Al Viro0b8def92015-04-07 10:22:53 -04003802 iocb->ki_pos = endbyte + 1;
Al Viro3b93f912014-02-11 21:34:08 -05003803 written += status;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003804 invalidate_mapping_pages(mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003805 pos >> PAGE_SHIFT,
3806 endbyte >> PAGE_SHIFT);
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003807 } else {
3808 /*
3809 * We don't know how much we wrote, so just return
3810 * the number of bytes which were direct-written
3811 */
3812 }
3813 } else {
Al Viro0b8def92015-04-07 10:22:53 -04003814 written = generic_perform_write(file, from, iocb->ki_pos);
3815 if (likely(written > 0))
3816 iocb->ki_pos += written;
Jeff Moyerfb5527e2006-10-19 23:28:13 -07003817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818out:
3819 current->backing_dev_info = NULL;
3820 return written ? written : err;
3821}
Al Viro81742022014-04-03 03:17:43 -04003822EXPORT_SYMBOL(__generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
Jan Karae4dd9de2009-08-17 18:10:06 +02003824/**
Al Viro81742022014-04-03 03:17:43 -04003825 * generic_file_write_iter - write data to a file
Jan Karae4dd9de2009-08-17 18:10:06 +02003826 * @iocb: IO state structure
Al Viro81742022014-04-03 03:17:43 -04003827 * @from: iov_iter with data to write
Jan Karae4dd9de2009-08-17 18:10:06 +02003828 *
Al Viro81742022014-04-03 03:17:43 -04003829 * This is a wrapper around __generic_file_write_iter() to be used by most
Jan Karae4dd9de2009-08-17 18:10:06 +02003830 * filesystems. It takes care of syncing the file in case of O_SYNC file
Jan Kara96087032021-04-12 15:50:21 +02003831 * and acquires i_rwsem as needed.
Mike Rapoporta862f682019-03-05 15:48:42 -08003832 * Return:
3833 * * negative error code if no data has been written at all of
3834 * vfs_fsync_range() failed for a synchronous write
3835 * * number of bytes written, even for truncated writes
Jan Karae4dd9de2009-08-17 18:10:06 +02003836 */
Al Viro81742022014-04-03 03:17:43 -04003837ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838{
3839 struct file *file = iocb->ki_filp;
Jan Kara148f9482009-08-17 19:52:36 +02003840 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
Al Viro59551022016-01-22 15:40:57 -05003843 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04003844 ret = generic_write_checks(iocb, from);
3845 if (ret > 0)
Al Viro5f380c72015-04-07 11:28:12 -04003846 ret = __generic_file_write_iter(iocb, from);
Al Viro59551022016-01-22 15:40:57 -05003847 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
Christoph Hellwige2592212016-04-07 08:52:01 -07003849 if (ret > 0)
3850 ret = generic_write_sync(iocb, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 return ret;
3852}
Al Viro81742022014-04-03 03:17:43 -04003853EXPORT_SYMBOL(generic_file_write_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
David Howellscf9a2ae2006-08-29 19:05:54 +01003855/**
Matthew Wilcox (Oracle)82c50f82021-07-28 15:14:48 -04003856 * filemap_release_folio() - Release fs-specific metadata on a folio.
3857 * @folio: The folio which the kernel is trying to free.
3858 * @gfp: Memory allocation flags (and I/O mode).
David Howellscf9a2ae2006-08-29 19:05:54 +01003859 *
Matthew Wilcox (Oracle)82c50f82021-07-28 15:14:48 -04003860 * The address_space is trying to release any data attached to a folio
3861 * (presumably at folio->private).
David Howellscf9a2ae2006-08-29 19:05:54 +01003862 *
Matthew Wilcox (Oracle)82c50f82021-07-28 15:14:48 -04003863 * This will also be called if the private_2 flag is set on a page,
3864 * indicating that the folio has other metadata associated with it.
David Howellscf9a2ae2006-08-29 19:05:54 +01003865 *
Matthew Wilcox (Oracle)82c50f82021-07-28 15:14:48 -04003866 * The @gfp argument specifies whether I/O may be performed to release
3867 * this page (__GFP_IO), and whether the call may block
3868 * (__GFP_RECLAIM & __GFP_FS).
David Howells266cf652009-04-03 16:42:36 +01003869 *
Matthew Wilcox (Oracle)82c50f82021-07-28 15:14:48 -04003870 * Return: %true if the release was successful, otherwise %false.
David Howellscf9a2ae2006-08-29 19:05:54 +01003871 */
Matthew Wilcox (Oracle)82c50f82021-07-28 15:14:48 -04003872bool filemap_release_folio(struct folio *folio, gfp_t gfp)
David Howellscf9a2ae2006-08-29 19:05:54 +01003873{
Matthew Wilcox (Oracle)82c50f82021-07-28 15:14:48 -04003874 struct address_space * const mapping = folio->mapping;
David Howellscf9a2ae2006-08-29 19:05:54 +01003875
Matthew Wilcox (Oracle)82c50f82021-07-28 15:14:48 -04003876 BUG_ON(!folio_test_locked(folio));
3877 if (folio_test_writeback(folio))
3878 return false;
David Howellscf9a2ae2006-08-29 19:05:54 +01003879
3880 if (mapping && mapping->a_ops->releasepage)
Matthew Wilcox (Oracle)82c50f82021-07-28 15:14:48 -04003881 return mapping->a_ops->releasepage(&folio->page, gfp);
3882 return try_to_free_buffers(&folio->page);
David Howellscf9a2ae2006-08-29 19:05:54 +01003883}
Matthew Wilcox (Oracle)82c50f82021-07-28 15:14:48 -04003884EXPORT_SYMBOL(filemap_release_folio);