1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/mm/filemap.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 1994-1999 Linus Torvalds
61da177e4SLinus Torvalds */
71da177e4SLinus Torvalds
81da177e4SLinus Torvalds /*
91da177e4SLinus Torvalds * This file handles the generic file mmap semantics used by
101da177e4SLinus Torvalds * most "normal" filesystems (but you don't /have/ to use this:
111da177e4SLinus Torvalds * the NFS filesystem used to do this differently, for example)
121da177e4SLinus Torvalds */
13b95f1b31SPaul Gortmaker #include <linux/export.h>
141da177e4SLinus Torvalds #include <linux/compiler.h>
15f9fe48beSRoss Zwisler #include <linux/dax.h>
161da177e4SLinus Torvalds #include <linux/fs.h>
173f07c014SIngo Molnar #include <linux/sched/signal.h>
18c22ce143SHiro Yoshioka #include <linux/uaccess.h>
19c59ede7bSRandy.Dunlap #include <linux/capability.h>
201da177e4SLinus Torvalds #include <linux/kernel_stat.h>
215a0e3ad6STejun Heo #include <linux/gfp.h>
221da177e4SLinus Torvalds #include <linux/mm.h>
231da177e4SLinus Torvalds #include <linux/swap.h>
24ffa65753SAlistair Popple #include <linux/swapops.h>
25cf264e13SNhat Pham #include <linux/syscalls.h>
261da177e4SLinus Torvalds #include <linux/mman.h>
271da177e4SLinus Torvalds #include <linux/pagemap.h>
281da177e4SLinus Torvalds #include <linux/file.h>
291da177e4SLinus Torvalds #include <linux/uio.h>
30cfcbfb13SJosef Bacik #include <linux/error-injection.h>
311da177e4SLinus Torvalds #include <linux/hash.h>
321da177e4SLinus Torvalds #include <linux/writeback.h>
3353253383SLinus Torvalds #include <linux/backing-dev.h>
341da177e4SLinus Torvalds #include <linux/pagevec.h>
351da177e4SLinus Torvalds #include <linux/security.h>
3644110fe3SPaul Jackson #include <linux/cpuset.h>
3700501b53SJohannes Weiner #include <linux/hugetlb.h>
388a9f3ccdSBalbir Singh #include <linux/memcontrol.h>
39c7df8ad2SMel Gorman #include <linux/shmem_fs.h>
40f1820361SKirill A. Shutemov #include <linux/rmap.h>
41b1d29ba8SJohannes Weiner #include <linux/delayacct.h>
42eb414681SJohannes Weiner #include <linux/psi.h>
43d0e6a582SBen Dooks #include <linux/ramfs.h>
44b9306a79SYang Shi #include <linux/page_idle.h>
45ffa65753SAlistair Popple #include <linux/migrate.h>
4607073eb0SDavid Howells #include <linux/pipe_fs_i.h>
4707073eb0SDavid Howells #include <linux/splice.h>
48f9ce0be7SKirill A. Shutemov #include <asm/pgalloc.h>
49de591a82SWill Deacon #include <asm/tlbflush.h>
500f8053a5SNick Piggin #include "internal.h"
510f8053a5SNick Piggin
52fe0bfaafSRobert Jarzmik #define CREATE_TRACE_POINTS
53fe0bfaafSRobert Jarzmik #include <trace/events/filemap.h>
54fe0bfaafSRobert Jarzmik
551da177e4SLinus Torvalds /*
561da177e4SLinus Torvalds * FIXME: remove all knowledge of the buffer layer from the core VM
571da177e4SLinus Torvalds */
58148f948bSJan Kara #include <linux/buffer_head.h> /* for try_to_free_buffers */
591da177e4SLinus Torvalds
601da177e4SLinus Torvalds #include <asm/mman.h>
611da177e4SLinus Torvalds
62cf264e13SNhat Pham #include "swap.h"
63cf264e13SNhat Pham
641da177e4SLinus Torvalds /*
651da177e4SLinus Torvalds * Shared mappings implemented 30.11.1994. It's not fully working yet,
661da177e4SLinus Torvalds * though.
671da177e4SLinus Torvalds *
681da177e4SLinus Torvalds * Shared mappings now work. 15.8.1995 Bruno.
691da177e4SLinus Torvalds *
701da177e4SLinus Torvalds * finished 'unifying' the page and buffer cache and SMP-threaded the
711da177e4SLinus Torvalds * page-cache, 21.05.1999, Ingo Molnar <mingo@redhat.com>
721da177e4SLinus Torvalds *
731da177e4SLinus Torvalds * SMP-threaded pagemap-LRU 1999, Andrea Arcangeli <andrea@suse.de>
741da177e4SLinus Torvalds */
751da177e4SLinus Torvalds
761da177e4SLinus Torvalds /*
771da177e4SLinus Torvalds * Lock ordering:
781da177e4SLinus Torvalds *
79c8c06efaSDavidlohr Bueso * ->i_mmap_rwsem (truncate_pagecache)
80e621900aSMatthew Wilcox (Oracle) * ->private_lock (__free_pte->block_dirty_folio)
815d337b91SHugh Dickins * ->swap_lock (exclusive_swap_page, others)
82b93b0163SMatthew Wilcox * ->i_pages lock
831da177e4SLinus Torvalds *
849608703eSJan Kara * ->i_rwsem
85730633f0SJan Kara * ->invalidate_lock (acquired by fs in truncate path)
86c8c06efaSDavidlohr Bueso * ->i_mmap_rwsem (truncate->unmap_mapping_range)
871da177e4SLinus Torvalds *
88c1e8d7c6SMichel Lespinasse * ->mmap_lock
89c8c06efaSDavidlohr Bueso * ->i_mmap_rwsem
90b8072f09SHugh Dickins * ->page_table_lock or pte_lock (various, mainly in memory.c)
91b93b0163SMatthew Wilcox * ->i_pages lock (arch-dependent flush_dcache_mmap_lock)
921da177e4SLinus Torvalds *
93c1e8d7c6SMichel Lespinasse * ->mmap_lock
94730633f0SJan Kara * ->invalidate_lock (filemap_fault)
95730633f0SJan Kara * ->lock_page (filemap_fault, access_process_vm)
961da177e4SLinus Torvalds *
979608703eSJan Kara * ->i_rwsem (generic_perform_write)
98bb523b40SAndreas Gruenbacher * ->mmap_lock (fault_in_readable->do_page_fault)
991da177e4SLinus Torvalds *
100f758eeabSChristoph Hellwig * bdi->wb.list_lock
101a66979abSDave Chinner * sb_lock (fs/fs-writeback.c)
102b93b0163SMatthew Wilcox * ->i_pages lock (__sync_single_inode)
1031da177e4SLinus Torvalds *
104c8c06efaSDavidlohr Bueso * ->i_mmap_rwsem
1050503ea8fSLiam R. Howlett * ->anon_vma.lock (vma_merge)
1061da177e4SLinus Torvalds *
1071da177e4SLinus Torvalds * ->anon_vma.lock
108b8072f09SHugh Dickins * ->page_table_lock or pte_lock (anon_vma_prepare and various)
1091da177e4SLinus Torvalds *
110b8072f09SHugh Dickins * ->page_table_lock or pte_lock
1115d337b91SHugh Dickins * ->swap_lock (try_to_unmap_one)
1121da177e4SLinus Torvalds * ->private_lock (try_to_unmap_one)
113b93b0163SMatthew Wilcox * ->i_pages lock (try_to_unmap_one)
11415b44736SHugh Dickins * ->lruvec->lru_lock (follow_page->mark_page_accessed)
11515b44736SHugh Dickins * ->lruvec->lru_lock (check_pte_range->isolate_lru_page)
1161da177e4SLinus Torvalds * ->private_lock (page_remove_rmap->set_page_dirty)
117b93b0163SMatthew Wilcox * ->i_pages lock (page_remove_rmap->set_page_dirty)
118f758eeabSChristoph Hellwig * bdi.wb->list_lock (page_remove_rmap->set_page_dirty)
119250df6edSDave Chinner * ->inode->i_lock (page_remove_rmap->set_page_dirty)
1206c77b607SKefeng Wang * ->memcg->move_lock (page_remove_rmap->folio_memcg_lock)
121f758eeabSChristoph Hellwig * bdi.wb->list_lock (zap_pte_range->set_page_dirty)
122250df6edSDave Chinner * ->inode->i_lock (zap_pte_range->set_page_dirty)
123e621900aSMatthew Wilcox (Oracle) * ->private_lock (zap_pte_range->block_dirty_folio)
1241da177e4SLinus Torvalds */
1251da177e4SLinus Torvalds
page_cache_delete(struct address_space * mapping,struct folio * folio,void * shadow)1265c024e6aSMatthew Wilcox static void page_cache_delete(struct address_space *mapping,
127a548b615SMatthew Wilcox (Oracle) struct folio *folio, void *shadow)
12891b0abe3SJohannes Weiner {
129a548b615SMatthew Wilcox (Oracle) XA_STATE(xas, &mapping->i_pages, folio->index);
130a548b615SMatthew Wilcox (Oracle) long nr = 1;
131c70b647dSKirill A. Shutemov
1325c024e6aSMatthew Wilcox mapping_set_update(&xas, mapping);
1335c024e6aSMatthew Wilcox
1345c024e6aSMatthew Wilcox /* hugetlb pages are represented by a single entry in the xarray */
135a548b615SMatthew Wilcox (Oracle) if (!folio_test_hugetlb(folio)) {
136a548b615SMatthew Wilcox (Oracle) xas_set_order(&xas, folio->index, folio_order(folio));
137a548b615SMatthew Wilcox (Oracle) nr = folio_nr_pages(folio);
1385c024e6aSMatthew Wilcox }
13991b0abe3SJohannes Weiner
140a548b615SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
141449dd698SJohannes Weiner
1425c024e6aSMatthew Wilcox xas_store(&xas, shadow);
1435c024e6aSMatthew Wilcox xas_init_marks(&xas);
144d3798ae8SJohannes Weiner
145a548b615SMatthew Wilcox (Oracle) folio->mapping = NULL;
1462300638bSJan Kara /* Leave page->index set: truncation lookup relies upon it */
147d3798ae8SJohannes Weiner mapping->nrpages -= nr;
14891b0abe3SJohannes Weiner }
14991b0abe3SJohannes Weiner
filemap_unaccount_folio(struct address_space * mapping,struct folio * folio)150621db488SMatthew Wilcox (Oracle) static void filemap_unaccount_folio(struct address_space *mapping,
151621db488SMatthew Wilcox (Oracle) struct folio *folio)
1521da177e4SLinus Torvalds {
153621db488SMatthew Wilcox (Oracle) long nr;
1541da177e4SLinus Torvalds
155621db488SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_mapped(folio), folio);
156621db488SMatthew Wilcox (Oracle) if (!IS_ENABLED(CONFIG_DEBUG_VM) && unlikely(folio_mapped(folio))) {
15706b241f3SHugh Dickins pr_alert("BUG: Bad page cache in process %s pfn:%05lx\n",
158621db488SMatthew Wilcox (Oracle) current->comm, folio_pfn(folio));
159621db488SMatthew Wilcox (Oracle) dump_page(&folio->page, "still mapped when deleted");
16006b241f3SHugh Dickins dump_stack();
16106b241f3SHugh Dickins add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
16206b241f3SHugh Dickins
16385207ad8SHugh Dickins if (mapping_exiting(mapping) && !folio_test_large(folio)) {
16485207ad8SHugh Dickins int mapcount = page_mapcount(&folio->page);
16585207ad8SHugh Dickins
16685207ad8SHugh Dickins if (folio_ref_count(folio) >= mapcount + 2) {
16706b241f3SHugh Dickins /*
16806b241f3SHugh Dickins * All vmas have already been torn down, so it's
16985207ad8SHugh Dickins * a good bet that actually the page is unmapped
17085207ad8SHugh Dickins * and we'd rather not leak it: if we're wrong,
17185207ad8SHugh Dickins * another bad page check should catch it later.
17206b241f3SHugh Dickins */
173621db488SMatthew Wilcox (Oracle) page_mapcount_reset(&folio->page);
174621db488SMatthew Wilcox (Oracle) folio_ref_sub(folio, mapcount);
17506b241f3SHugh Dickins }
17606b241f3SHugh Dickins }
17785207ad8SHugh Dickins }
17806b241f3SHugh Dickins
179621db488SMatthew Wilcox (Oracle) /* hugetlb folios do not participate in page cache accounting. */
180621db488SMatthew Wilcox (Oracle) if (folio_test_hugetlb(folio))
1815ecc4d85SJan Kara return;
1825ecc4d85SJan Kara
183621db488SMatthew Wilcox (Oracle) nr = folio_nr_pages(folio);
1845ecc4d85SJan Kara
185621db488SMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_FILE_PAGES, -nr);
186621db488SMatthew Wilcox (Oracle) if (folio_test_swapbacked(folio)) {
187621db488SMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_SHMEM, -nr);
188621db488SMatthew Wilcox (Oracle) if (folio_test_pmd_mappable(folio))
189621db488SMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_SHMEM_THPS, -nr);
190621db488SMatthew Wilcox (Oracle) } else if (folio_test_pmd_mappable(folio)) {
191621db488SMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_FILE_THPS, -nr);
19209d91cdaSSong Liu filemap_nr_thps_dec(mapping);
193800d8c63SKirill A. Shutemov }
1943a692790SLinus Torvalds
1953a692790SLinus Torvalds /*
196621db488SMatthew Wilcox (Oracle) * At this point folio must be either written or cleaned by
197621db488SMatthew Wilcox (Oracle) * truncate. Dirty folio here signals a bug and loss of
198566d3362SHugh Dickins * unwritten data - on ordinary filesystems.
1993a692790SLinus Torvalds *
200566d3362SHugh Dickins * But it's harmless on in-memory filesystems like tmpfs; and can
201566d3362SHugh Dickins * occur when a driver which did get_user_pages() sets page dirty
202566d3362SHugh Dickins * before putting it, while the inode is being finally evicted.
203566d3362SHugh Dickins *
204566d3362SHugh Dickins * Below fixes dirty accounting after removing the folio entirely
205621db488SMatthew Wilcox (Oracle) * but leaves the dirty flag set: it has no effect for truncated
206621db488SMatthew Wilcox (Oracle) * folio and anyway will be cleared before returning folio to
20776253fbcSJan Kara * buddy allocator.
2083a692790SLinus Torvalds */
209566d3362SHugh Dickins if (WARN_ON_ONCE(folio_test_dirty(folio) &&
210566d3362SHugh Dickins mapping_can_writeback(mapping)))
211566d3362SHugh Dickins folio_account_cleaned(folio, inode_to_wb(mapping->host));
21276253fbcSJan Kara }
2135ecc4d85SJan Kara
2145ecc4d85SJan Kara /*
2155ecc4d85SJan Kara * Delete a page from the page cache and free it. Caller has to make
2165ecc4d85SJan Kara * sure the page is locked and that nobody else uses it - or that usage
217b93b0163SMatthew Wilcox * is safe. The caller must hold the i_pages lock.
2185ecc4d85SJan Kara */
__filemap_remove_folio(struct folio * folio,void * shadow)219452e9e69SMatthew Wilcox (Oracle) void __filemap_remove_folio(struct folio *folio, void *shadow)
2205ecc4d85SJan Kara {
221452e9e69SMatthew Wilcox (Oracle) struct address_space *mapping = folio->mapping;
2225ecc4d85SJan Kara
223a0580c6fSMatthew Wilcox (Oracle) trace_mm_filemap_delete_from_page_cache(folio);
224621db488SMatthew Wilcox (Oracle) filemap_unaccount_folio(mapping, folio);
225a548b615SMatthew Wilcox (Oracle) page_cache_delete(mapping, folio, shadow);
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds
filemap_free_folio(struct address_space * mapping,struct folio * folio)22878f42660SMatthew Wilcox (Oracle) void filemap_free_folio(struct address_space *mapping, struct folio *folio)
22959c66c5fSJan Kara {
230d2329aa0SMatthew Wilcox (Oracle) void (*free_folio)(struct folio *);
2313abb28e2SMatthew Wilcox (Oracle) int refs = 1;
23259c66c5fSJan Kara
233d2329aa0SMatthew Wilcox (Oracle) free_folio = mapping->a_ops->free_folio;
234d2329aa0SMatthew Wilcox (Oracle) if (free_folio)
235d2329aa0SMatthew Wilcox (Oracle) free_folio(folio);
23659c66c5fSJan Kara
2373abb28e2SMatthew Wilcox (Oracle) if (folio_test_large(folio) && !folio_test_hugetlb(folio))
2383abb28e2SMatthew Wilcox (Oracle) refs = folio_nr_pages(folio);
2393abb28e2SMatthew Wilcox (Oracle) folio_put_refs(folio, refs);
24059c66c5fSJan Kara }
24159c66c5fSJan Kara
242702cfbf9SMinchan Kim /**
243452e9e69SMatthew Wilcox (Oracle) * filemap_remove_folio - Remove folio from page cache.
244452e9e69SMatthew Wilcox (Oracle) * @folio: The folio.
245702cfbf9SMinchan Kim *
246452e9e69SMatthew Wilcox (Oracle) * This must be called only on folios that are locked and have been
247452e9e69SMatthew Wilcox (Oracle) * verified to be in the page cache. It will never put the folio into
248452e9e69SMatthew Wilcox (Oracle) * the free list because the caller has a reference on the page.
249702cfbf9SMinchan Kim */
filemap_remove_folio(struct folio * folio)250452e9e69SMatthew Wilcox (Oracle) void filemap_remove_folio(struct folio *folio)
2511da177e4SLinus Torvalds {
252452e9e69SMatthew Wilcox (Oracle) struct address_space *mapping = folio->mapping;
2531da177e4SLinus Torvalds
254452e9e69SMatthew Wilcox (Oracle) BUG_ON(!folio_test_locked(folio));
25551b8c1feSJohannes Weiner spin_lock(&mapping->host->i_lock);
25630472509SJohannes Weiner xa_lock_irq(&mapping->i_pages);
257452e9e69SMatthew Wilcox (Oracle) __filemap_remove_folio(folio, NULL);
25830472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages);
25951b8c1feSJohannes Weiner if (mapping_shrinkable(mapping))
26051b8c1feSJohannes Weiner inode_add_lru(mapping->host);
26151b8c1feSJohannes Weiner spin_unlock(&mapping->host->i_lock);
2626072d13cSLinus Torvalds
263452e9e69SMatthew Wilcox (Oracle) filemap_free_folio(mapping, folio);
26483929372SKirill A. Shutemov }
26597cecb5aSMinchan Kim
266aa65c29cSJan Kara /*
26751dcbdacSMatthew Wilcox (Oracle) * page_cache_delete_batch - delete several folios from page cache
26851dcbdacSMatthew Wilcox (Oracle) * @mapping: the mapping to which folios belong
26951dcbdacSMatthew Wilcox (Oracle) * @fbatch: batch of folios to delete
270aa65c29cSJan Kara *
27151dcbdacSMatthew Wilcox (Oracle) * The function walks over mapping->i_pages and removes folios passed in
27251dcbdacSMatthew Wilcox (Oracle) * @fbatch from the mapping. The function expects @fbatch to be sorted
27351dcbdacSMatthew Wilcox (Oracle) * by page index and is optimised for it to be dense.
27451dcbdacSMatthew Wilcox (Oracle) * It tolerates holes in @fbatch (mapping entries at those indices are not
27551dcbdacSMatthew Wilcox (Oracle) * modified).
276aa65c29cSJan Kara *
277b93b0163SMatthew Wilcox * The function expects the i_pages lock to be held.
278aa65c29cSJan Kara */
page_cache_delete_batch(struct address_space * mapping,struct folio_batch * fbatch)279ef8e5717SMatthew Wilcox static void page_cache_delete_batch(struct address_space *mapping,
28051dcbdacSMatthew Wilcox (Oracle) struct folio_batch *fbatch)
281aa65c29cSJan Kara {
28251dcbdacSMatthew Wilcox (Oracle) XA_STATE(xas, &mapping->i_pages, fbatch->folios[0]->index);
2836b24ca4aSMatthew Wilcox (Oracle) long total_pages = 0;
2844101196bSMatthew Wilcox (Oracle) int i = 0;
2851afd7ae5SMatthew Wilcox (Oracle) struct folio *folio;
286aa65c29cSJan Kara
287ef8e5717SMatthew Wilcox mapping_set_update(&xas, mapping);
2881afd7ae5SMatthew Wilcox (Oracle) xas_for_each(&xas, folio, ULONG_MAX) {
28951dcbdacSMatthew Wilcox (Oracle) if (i >= folio_batch_count(fbatch))
290aa65c29cSJan Kara break;
2914101196bSMatthew Wilcox (Oracle)
2924101196bSMatthew Wilcox (Oracle) /* A swap/dax/shadow entry got inserted? Skip it. */
2931afd7ae5SMatthew Wilcox (Oracle) if (xa_is_value(folio))
294aa65c29cSJan Kara continue;
295aa65c29cSJan Kara /*
2964101196bSMatthew Wilcox (Oracle) * A page got inserted in our range? Skip it. We have our
2974101196bSMatthew Wilcox (Oracle) * pages locked so they are protected from being removed.
2984101196bSMatthew Wilcox (Oracle) * If we see a page whose index is higher than ours, it
2994101196bSMatthew Wilcox (Oracle) * means our page has been removed, which shouldn't be
3004101196bSMatthew Wilcox (Oracle) * possible because we're holding the PageLock.
301aa65c29cSJan Kara */
30251dcbdacSMatthew Wilcox (Oracle) if (folio != fbatch->folios[i]) {
3031afd7ae5SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio->index >
30451dcbdacSMatthew Wilcox (Oracle) fbatch->folios[i]->index, folio);
305aa65c29cSJan Kara continue;
306ef8e5717SMatthew Wilcox }
3074101196bSMatthew Wilcox (Oracle)
3081afd7ae5SMatthew Wilcox (Oracle) WARN_ON_ONCE(!folio_test_locked(folio));
3094101196bSMatthew Wilcox (Oracle)
3101afd7ae5SMatthew Wilcox (Oracle) folio->mapping = NULL;
31151dcbdacSMatthew Wilcox (Oracle) /* Leave folio->index set: truncation lookup relies on it */
3124101196bSMatthew Wilcox (Oracle)
313aa65c29cSJan Kara i++;
314ef8e5717SMatthew Wilcox xas_store(&xas, NULL);
3156b24ca4aSMatthew Wilcox (Oracle) total_pages += folio_nr_pages(folio);
316aa65c29cSJan Kara }
317aa65c29cSJan Kara mapping->nrpages -= total_pages;
318aa65c29cSJan Kara }
319aa65c29cSJan Kara
delete_from_page_cache_batch(struct address_space * mapping,struct folio_batch * fbatch)320aa65c29cSJan Kara void delete_from_page_cache_batch(struct address_space *mapping,
32151dcbdacSMatthew Wilcox (Oracle) struct folio_batch *fbatch)
322aa65c29cSJan Kara {
323aa65c29cSJan Kara int i;
324aa65c29cSJan Kara
32551dcbdacSMatthew Wilcox (Oracle) if (!folio_batch_count(fbatch))
326aa65c29cSJan Kara return;
327aa65c29cSJan Kara
32851b8c1feSJohannes Weiner spin_lock(&mapping->host->i_lock);
32930472509SJohannes Weiner xa_lock_irq(&mapping->i_pages);
33051dcbdacSMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(fbatch); i++) {
33151dcbdacSMatthew Wilcox (Oracle) struct folio *folio = fbatch->folios[i];
332aa65c29cSJan Kara
333a0580c6fSMatthew Wilcox (Oracle) trace_mm_filemap_delete_from_page_cache(folio);
334a0580c6fSMatthew Wilcox (Oracle) filemap_unaccount_folio(mapping, folio);
335aa65c29cSJan Kara }
33651dcbdacSMatthew Wilcox (Oracle) page_cache_delete_batch(mapping, fbatch);
33730472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages);
33851b8c1feSJohannes Weiner if (mapping_shrinkable(mapping))
33951b8c1feSJohannes Weiner inode_add_lru(mapping->host);
34051b8c1feSJohannes Weiner spin_unlock(&mapping->host->i_lock);
341aa65c29cSJan Kara
34251dcbdacSMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(fbatch); i++)
34351dcbdacSMatthew Wilcox (Oracle) filemap_free_folio(mapping, fbatch->folios[i]);
344aa65c29cSJan Kara }
345aa65c29cSJan Kara
filemap_check_errors(struct address_space * mapping)346d72d9e2aSMiklos Szeredi int filemap_check_errors(struct address_space *mapping)
347865ffef3SDmitry Monakhov {
348865ffef3SDmitry Monakhov int ret = 0;
349865ffef3SDmitry Monakhov /* Check for outstanding write errors */
3507fcbbaf1SJens Axboe if (test_bit(AS_ENOSPC, &mapping->flags) &&
3517fcbbaf1SJens Axboe test_and_clear_bit(AS_ENOSPC, &mapping->flags))
352865ffef3SDmitry Monakhov ret = -ENOSPC;
3537fcbbaf1SJens Axboe if (test_bit(AS_EIO, &mapping->flags) &&
3547fcbbaf1SJens Axboe test_and_clear_bit(AS_EIO, &mapping->flags))
355865ffef3SDmitry Monakhov ret = -EIO;
356865ffef3SDmitry Monakhov return ret;
357865ffef3SDmitry Monakhov }
358d72d9e2aSMiklos Szeredi EXPORT_SYMBOL(filemap_check_errors);
359865ffef3SDmitry Monakhov
filemap_check_and_keep_errors(struct address_space * mapping)36076341cabSJeff Layton static int filemap_check_and_keep_errors(struct address_space *mapping)
36176341cabSJeff Layton {
36276341cabSJeff Layton /* Check for outstanding write errors */
36376341cabSJeff Layton if (test_bit(AS_EIO, &mapping->flags))
36476341cabSJeff Layton return -EIO;
36576341cabSJeff Layton if (test_bit(AS_ENOSPC, &mapping->flags))
36676341cabSJeff Layton return -ENOSPC;
36776341cabSJeff Layton return 0;
36876341cabSJeff Layton }
36976341cabSJeff Layton
3701da177e4SLinus Torvalds /**
3715a798493SJosef Bacik * filemap_fdatawrite_wbc - start writeback on mapping dirty pages in range
3725a798493SJosef Bacik * @mapping: address space structure to write
3735a798493SJosef Bacik * @wbc: the writeback_control controlling the writeout
3745a798493SJosef Bacik *
3755a798493SJosef Bacik * Call writepages on the mapping using the provided wbc to control the
3765a798493SJosef Bacik * writeout.
3775a798493SJosef Bacik *
3785a798493SJosef Bacik * Return: %0 on success, negative error code otherwise.
3795a798493SJosef Bacik */
filemap_fdatawrite_wbc(struct address_space * mapping,struct writeback_control * wbc)3805a798493SJosef Bacik int filemap_fdatawrite_wbc(struct address_space *mapping,
3815a798493SJosef Bacik struct writeback_control *wbc)
3825a798493SJosef Bacik {
3835a798493SJosef Bacik int ret;
3845a798493SJosef Bacik
3855a798493SJosef Bacik if (!mapping_can_writeback(mapping) ||
3865a798493SJosef Bacik !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3875a798493SJosef Bacik return 0;
3885a798493SJosef Bacik
3895a798493SJosef Bacik wbc_attach_fdatawrite_inode(wbc, mapping->host);
3905a798493SJosef Bacik ret = do_writepages(mapping, wbc);
3915a798493SJosef Bacik wbc_detach_inode(wbc);
3925a798493SJosef Bacik return ret;
3935a798493SJosef Bacik }
3945a798493SJosef Bacik EXPORT_SYMBOL(filemap_fdatawrite_wbc);
3955a798493SJosef Bacik
3965a798493SJosef Bacik /**
397485bb99bSRandy Dunlap * __filemap_fdatawrite_range - start writeback on mapping dirty pages in range
3981da177e4SLinus Torvalds * @mapping: address space structure to write
3991da177e4SLinus Torvalds * @start: offset in bytes where the range starts
400469eb4d0SAndrew Morton * @end: offset in bytes where the range ends (inclusive)
40167be2dd1SMartin Waitz * @sync_mode: enable synchronous operation
4021da177e4SLinus Torvalds *
403485bb99bSRandy Dunlap * Start writeback against all of a mapping's dirty pages that lie
404485bb99bSRandy Dunlap * within the byte offsets <start, end> inclusive.
405485bb99bSRandy Dunlap *
4061da177e4SLinus Torvalds * If sync_mode is WB_SYNC_ALL then this is a "data integrity" operation, as
407485bb99bSRandy Dunlap * opposed to a regular memory cleansing writeback. The difference between
4081da177e4SLinus Torvalds * these two operations is that if a dirty page/buffer is encountered, it must
4091da177e4SLinus Torvalds * be waited upon, and not just skipped over.
410a862f68aSMike Rapoport *
411a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise.
4121da177e4SLinus Torvalds */
__filemap_fdatawrite_range(struct address_space * mapping,loff_t start,loff_t end,int sync_mode)413ebcf28e1SAndrew Morton int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
414ebcf28e1SAndrew Morton loff_t end, int sync_mode)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds struct writeback_control wbc = {
4171da177e4SLinus Torvalds .sync_mode = sync_mode,
41805fe478dSNick Piggin .nr_to_write = LONG_MAX,
419111ebb6eSOGAWA Hirofumi .range_start = start,
420111ebb6eSOGAWA Hirofumi .range_end = end,
4211da177e4SLinus Torvalds };
4221da177e4SLinus Torvalds
4235a798493SJosef Bacik return filemap_fdatawrite_wbc(mapping, &wbc);
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds
__filemap_fdatawrite(struct address_space * mapping,int sync_mode)4261da177e4SLinus Torvalds static inline int __filemap_fdatawrite(struct address_space *mapping,
4271da177e4SLinus Torvalds int sync_mode)
4281da177e4SLinus Torvalds {
429111ebb6eSOGAWA Hirofumi return __filemap_fdatawrite_range(mapping, 0, LLONG_MAX, sync_mode);
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds
filemap_fdatawrite(struct address_space * mapping)4321da177e4SLinus Torvalds int filemap_fdatawrite(struct address_space *mapping)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds return __filemap_fdatawrite(mapping, WB_SYNC_ALL);
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds EXPORT_SYMBOL(filemap_fdatawrite);
4371da177e4SLinus Torvalds
filemap_fdatawrite_range(struct address_space * mapping,loff_t start,loff_t end)438f4c0a0fdSJan Kara int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
439ebcf28e1SAndrew Morton loff_t end)
4401da177e4SLinus Torvalds {
4411da177e4SLinus Torvalds return __filemap_fdatawrite_range(mapping, start, end, WB_SYNC_ALL);
4421da177e4SLinus Torvalds }
443f4c0a0fdSJan Kara EXPORT_SYMBOL(filemap_fdatawrite_range);
4441da177e4SLinus Torvalds
445485bb99bSRandy Dunlap /**
446485bb99bSRandy Dunlap * filemap_flush - mostly a non-blocking flush
447485bb99bSRandy Dunlap * @mapping: target address_space
448485bb99bSRandy Dunlap *
4491da177e4SLinus Torvalds * This is a mostly non-blocking flush. Not suitable for data-integrity
4501da177e4SLinus Torvalds * purposes - I/O may not be started against all dirty pages.
451a862f68aSMike Rapoport *
452a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise.
4531da177e4SLinus Torvalds */
filemap_flush(struct address_space * mapping)4541da177e4SLinus Torvalds int filemap_flush(struct address_space *mapping)
4551da177e4SLinus Torvalds {
4561da177e4SLinus Torvalds return __filemap_fdatawrite(mapping, WB_SYNC_NONE);
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds EXPORT_SYMBOL(filemap_flush);
4591da177e4SLinus Torvalds
4607fc9e472SGoldwyn Rodrigues /**
4617fc9e472SGoldwyn Rodrigues * filemap_range_has_page - check if a page exists in range.
4627fc9e472SGoldwyn Rodrigues * @mapping: address space within which to check
4637fc9e472SGoldwyn Rodrigues * @start_byte: offset in bytes where the range starts
4647fc9e472SGoldwyn Rodrigues * @end_byte: offset in bytes where the range ends (inclusive)
4657fc9e472SGoldwyn Rodrigues *
4667fc9e472SGoldwyn Rodrigues * Find at least one page in the range supplied, usually used to check if
4677fc9e472SGoldwyn Rodrigues * direct writing in this range will trigger a writeback.
468a862f68aSMike Rapoport *
469a862f68aSMike Rapoport * Return: %true if at least one page exists in the specified range,
470a862f68aSMike Rapoport * %false otherwise.
4717fc9e472SGoldwyn Rodrigues */
filemap_range_has_page(struct address_space * mapping,loff_t start_byte,loff_t end_byte)4727fc9e472SGoldwyn Rodrigues bool filemap_range_has_page(struct address_space *mapping,
4737fc9e472SGoldwyn Rodrigues loff_t start_byte, loff_t end_byte)
4747fc9e472SGoldwyn Rodrigues {
475eff3b364SMatthew Wilcox (Oracle) struct folio *folio;
4768fa8e538SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT);
4778fa8e538SMatthew Wilcox pgoff_t max = end_byte >> PAGE_SHIFT;
4787fc9e472SGoldwyn Rodrigues
4797fc9e472SGoldwyn Rodrigues if (end_byte < start_byte)
4807fc9e472SGoldwyn Rodrigues return false;
4817fc9e472SGoldwyn Rodrigues
4828fa8e538SMatthew Wilcox rcu_read_lock();
4838fa8e538SMatthew Wilcox for (;;) {
484eff3b364SMatthew Wilcox (Oracle) folio = xas_find(&xas, max);
485eff3b364SMatthew Wilcox (Oracle) if (xas_retry(&xas, folio))
4868fa8e538SMatthew Wilcox continue;
4878fa8e538SMatthew Wilcox /* Shadow entries don't count */
488eff3b364SMatthew Wilcox (Oracle) if (xa_is_value(folio))
4898fa8e538SMatthew Wilcox continue;
4908fa8e538SMatthew Wilcox /*
4918fa8e538SMatthew Wilcox * We don't need to try to pin this page; we're about to
4928fa8e538SMatthew Wilcox * release the RCU lock anyway. It is enough to know that
4938fa8e538SMatthew Wilcox * there was a page here recently.
4948fa8e538SMatthew Wilcox */
4958fa8e538SMatthew Wilcox break;
4968fa8e538SMatthew Wilcox }
4978fa8e538SMatthew Wilcox rcu_read_unlock();
4987fc9e472SGoldwyn Rodrigues
499eff3b364SMatthew Wilcox (Oracle) return folio != NULL;
5007fc9e472SGoldwyn Rodrigues }
5017fc9e472SGoldwyn Rodrigues EXPORT_SYMBOL(filemap_range_has_page);
5027fc9e472SGoldwyn Rodrigues
__filemap_fdatawait_range(struct address_space * mapping,loff_t start_byte,loff_t end_byte)5035e8fcc1aSJeff Layton static void __filemap_fdatawait_range(struct address_space *mapping,
504aa750fd7SJunichi Nomura loff_t start_byte, loff_t end_byte)
5051da177e4SLinus Torvalds {
50609cbfeafSKirill A. Shutemov pgoff_t index = start_byte >> PAGE_SHIFT;
50709cbfeafSKirill A. Shutemov pgoff_t end = end_byte >> PAGE_SHIFT;
5086817ef51SVishal Moola (Oracle) struct folio_batch fbatch;
5096817ef51SVishal Moola (Oracle) unsigned nr_folios;
5101da177e4SLinus Torvalds
5116817ef51SVishal Moola (Oracle) folio_batch_init(&fbatch);
5126817ef51SVishal Moola (Oracle)
513312e9d2fSJan Kara while (index <= end) {
5141da177e4SLinus Torvalds unsigned i;
5151da177e4SLinus Torvalds
5166817ef51SVishal Moola (Oracle) nr_folios = filemap_get_folios_tag(mapping, &index, end,
5176817ef51SVishal Moola (Oracle) PAGECACHE_TAG_WRITEBACK, &fbatch);
5186817ef51SVishal Moola (Oracle)
5196817ef51SVishal Moola (Oracle) if (!nr_folios)
520312e9d2fSJan Kara break;
521312e9d2fSJan Kara
5226817ef51SVishal Moola (Oracle) for (i = 0; i < nr_folios; i++) {
5236817ef51SVishal Moola (Oracle) struct folio *folio = fbatch.folios[i];
5241da177e4SLinus Torvalds
5256817ef51SVishal Moola (Oracle) folio_wait_writeback(folio);
5266817ef51SVishal Moola (Oracle) folio_clear_error(folio);
5271da177e4SLinus Torvalds }
5286817ef51SVishal Moola (Oracle) folio_batch_release(&fbatch);
5291da177e4SLinus Torvalds cond_resched();
5301da177e4SLinus Torvalds }
531aa750fd7SJunichi Nomura }
532aa750fd7SJunichi Nomura
533aa750fd7SJunichi Nomura /**
534aa750fd7SJunichi Nomura * filemap_fdatawait_range - wait for writeback to complete
535aa750fd7SJunichi Nomura * @mapping: address space structure to wait for
536aa750fd7SJunichi Nomura * @start_byte: offset in bytes where the range starts
537aa750fd7SJunichi Nomura * @end_byte: offset in bytes where the range ends (inclusive)
538aa750fd7SJunichi Nomura *
539aa750fd7SJunichi Nomura * Walk the list of under-writeback pages of the given address space
540aa750fd7SJunichi Nomura * in the given range and wait for all of them. Check error status of
541aa750fd7SJunichi Nomura * the address space and return it.
542aa750fd7SJunichi Nomura *
543aa750fd7SJunichi Nomura * Since the error status of the address space is cleared by this function,
544aa750fd7SJunichi Nomura * callers are responsible for checking the return value and handling and/or
545aa750fd7SJunichi Nomura * reporting the error.
546a862f68aSMike Rapoport *
547a862f68aSMike Rapoport * Return: error status of the address space.
548aa750fd7SJunichi Nomura */
filemap_fdatawait_range(struct address_space * mapping,loff_t start_byte,loff_t end_byte)549aa750fd7SJunichi Nomura int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
550aa750fd7SJunichi Nomura loff_t end_byte)
551aa750fd7SJunichi Nomura {
5525e8fcc1aSJeff Layton __filemap_fdatawait_range(mapping, start_byte, end_byte);
5535e8fcc1aSJeff Layton return filemap_check_errors(mapping);
5541da177e4SLinus Torvalds }
555d3bccb6fSJan Kara EXPORT_SYMBOL(filemap_fdatawait_range);
556d3bccb6fSJan Kara
557d3bccb6fSJan Kara /**
558aa0bfcd9SRoss Zwisler * filemap_fdatawait_range_keep_errors - wait for writeback to complete
559aa0bfcd9SRoss Zwisler * @mapping: address space structure to wait for
560aa0bfcd9SRoss Zwisler * @start_byte: offset in bytes where the range starts
561aa0bfcd9SRoss Zwisler * @end_byte: offset in bytes where the range ends (inclusive)
562aa0bfcd9SRoss Zwisler *
563aa0bfcd9SRoss Zwisler * Walk the list of under-writeback pages of the given address space in the
564aa0bfcd9SRoss Zwisler * given range and wait for all of them. Unlike filemap_fdatawait_range(),
565aa0bfcd9SRoss Zwisler * this function does not clear error status of the address space.
566aa0bfcd9SRoss Zwisler *
567aa0bfcd9SRoss Zwisler * Use this function if callers don't handle errors themselves. Expected
568aa0bfcd9SRoss Zwisler * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
569aa0bfcd9SRoss Zwisler * fsfreeze(8)
570aa0bfcd9SRoss Zwisler */
filemap_fdatawait_range_keep_errors(struct address_space * mapping,loff_t start_byte,loff_t end_byte)571aa0bfcd9SRoss Zwisler int filemap_fdatawait_range_keep_errors(struct address_space *mapping,
572aa0bfcd9SRoss Zwisler loff_t start_byte, loff_t end_byte)
573aa0bfcd9SRoss Zwisler {
574aa0bfcd9SRoss Zwisler __filemap_fdatawait_range(mapping, start_byte, end_byte);
575aa0bfcd9SRoss Zwisler return filemap_check_and_keep_errors(mapping);
576aa0bfcd9SRoss Zwisler }
577aa0bfcd9SRoss Zwisler EXPORT_SYMBOL(filemap_fdatawait_range_keep_errors);
578aa0bfcd9SRoss Zwisler
579aa0bfcd9SRoss Zwisler /**
580a823e458SJeff Layton * file_fdatawait_range - wait for writeback to complete
581a823e458SJeff Layton * @file: file pointing to address space structure to wait for
582a823e458SJeff Layton * @start_byte: offset in bytes where the range starts
583a823e458SJeff Layton * @end_byte: offset in bytes where the range ends (inclusive)
584a823e458SJeff Layton *
585a823e458SJeff Layton * Walk the list of under-writeback pages of the address space that file
586a823e458SJeff Layton * refers to, in the given range and wait for all of them. Check error
587a823e458SJeff Layton * status of the address space vs. the file->f_wb_err cursor and return it.
588a823e458SJeff Layton *
589a823e458SJeff Layton * Since the error status of the file is advanced by this function,
590a823e458SJeff Layton * callers are responsible for checking the return value and handling and/or
591a823e458SJeff Layton * reporting the error.
592a862f68aSMike Rapoport *
593a862f68aSMike Rapoport * Return: error status of the address space vs. the file->f_wb_err cursor.
594a823e458SJeff Layton */
file_fdatawait_range(struct file * file,loff_t start_byte,loff_t end_byte)595a823e458SJeff Layton int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
596a823e458SJeff Layton {
597a823e458SJeff Layton struct address_space *mapping = file->f_mapping;
598a823e458SJeff Layton
599a823e458SJeff Layton __filemap_fdatawait_range(mapping, start_byte, end_byte);
600a823e458SJeff Layton return file_check_and_advance_wb_err(file);
601a823e458SJeff Layton }
602a823e458SJeff Layton EXPORT_SYMBOL(file_fdatawait_range);
603a823e458SJeff Layton
604a823e458SJeff Layton /**
605aa750fd7SJunichi Nomura * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
606aa750fd7SJunichi Nomura * @mapping: address space structure to wait for
607aa750fd7SJunichi Nomura *
608aa750fd7SJunichi Nomura * Walk the list of under-writeback pages of the given address space
609aa750fd7SJunichi Nomura * and wait for all of them. Unlike filemap_fdatawait(), this function
610aa750fd7SJunichi Nomura * does not clear error status of the address space.
611aa750fd7SJunichi Nomura *
612aa750fd7SJunichi Nomura * Use this function if callers don't handle errors themselves. Expected
613aa750fd7SJunichi Nomura * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
614aa750fd7SJunichi Nomura * fsfreeze(8)
615a862f68aSMike Rapoport *
616a862f68aSMike Rapoport * Return: error status of the address space.
617aa750fd7SJunichi Nomura */
filemap_fdatawait_keep_errors(struct address_space * mapping)61876341cabSJeff Layton int filemap_fdatawait_keep_errors(struct address_space *mapping)
619aa750fd7SJunichi Nomura {
620ffb959bbSJeff Layton __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
62176341cabSJeff Layton return filemap_check_and_keep_errors(mapping);
622aa750fd7SJunichi Nomura }
62376341cabSJeff Layton EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
624aa750fd7SJunichi Nomura
625875d91b1SKonstantin Khlebnikov /* Returns true if writeback might be needed or already in progress. */
mapping_needs_writeback(struct address_space * mapping)6269326c9b2SJeff Layton static bool mapping_needs_writeback(struct address_space *mapping)
6271da177e4SLinus Torvalds {
628875d91b1SKonstantin Khlebnikov return mapping->nrpages;
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds
filemap_range_has_writeback(struct address_space * mapping,loff_t start_byte,loff_t end_byte)6314bdcd1ddSJens Axboe bool filemap_range_has_writeback(struct address_space *mapping,
632f8ee8909SJens Axboe loff_t start_byte, loff_t end_byte)
633f8ee8909SJens Axboe {
634f8ee8909SJens Axboe XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT);
635f8ee8909SJens Axboe pgoff_t max = end_byte >> PAGE_SHIFT;
636b05f41a1SVishal Moola (Oracle) struct folio *folio;
637f8ee8909SJens Axboe
638f8ee8909SJens Axboe if (end_byte < start_byte)
639f8ee8909SJens Axboe return false;
640f8ee8909SJens Axboe
641f8ee8909SJens Axboe rcu_read_lock();
642b05f41a1SVishal Moola (Oracle) xas_for_each(&xas, folio, max) {
643b05f41a1SVishal Moola (Oracle) if (xas_retry(&xas, folio))
644f8ee8909SJens Axboe continue;
645b05f41a1SVishal Moola (Oracle) if (xa_is_value(folio))
646f8ee8909SJens Axboe continue;
647b05f41a1SVishal Moola (Oracle) if (folio_test_dirty(folio) || folio_test_locked(folio) ||
648b05f41a1SVishal Moola (Oracle) folio_test_writeback(folio))
649f8ee8909SJens Axboe break;
650f8ee8909SJens Axboe }
651f8ee8909SJens Axboe rcu_read_unlock();
652b05f41a1SVishal Moola (Oracle) return folio != NULL;
653f8ee8909SJens Axboe }
6544bdcd1ddSJens Axboe EXPORT_SYMBOL_GPL(filemap_range_has_writeback);
65563135aa3SJens Axboe
65663135aa3SJens Axboe /**
657485bb99bSRandy Dunlap * filemap_write_and_wait_range - write out & wait on a file range
658485bb99bSRandy Dunlap * @mapping: the address_space for the pages
659485bb99bSRandy Dunlap * @lstart: offset in bytes where the range starts
660485bb99bSRandy Dunlap * @lend: offset in bytes where the range ends (inclusive)
661485bb99bSRandy Dunlap *
662469eb4d0SAndrew Morton * Write out and wait upon file offsets lstart->lend, inclusive.
663469eb4d0SAndrew Morton *
6640e056eb5Smchehab@s-opensource.com * Note that @lend is inclusive (describes the last byte to be written) so
665469eb4d0SAndrew Morton * that this function can be used to write to the very end-of-file (end = -1).
666a862f68aSMike Rapoport *
667a862f68aSMike Rapoport * Return: error status of the address space.
668469eb4d0SAndrew Morton */
filemap_write_and_wait_range(struct address_space * mapping,loff_t lstart,loff_t lend)6691da177e4SLinus Torvalds int filemap_write_and_wait_range(struct address_space *mapping,
6701da177e4SLinus Torvalds loff_t lstart, loff_t lend)
6711da177e4SLinus Torvalds {
672ccac11daSMiaohe Lin int err = 0, err2;
6731da177e4SLinus Torvalds
674feeb9b26SBrian Foster if (lend < lstart)
675feeb9b26SBrian Foster return 0;
676feeb9b26SBrian Foster
6779326c9b2SJeff Layton if (mapping_needs_writeback(mapping)) {
67828fd1298SOGAWA Hirofumi err = __filemap_fdatawrite_range(mapping, lstart, lend,
6791da177e4SLinus Torvalds WB_SYNC_ALL);
680ddf8f376SIra Weiny /*
681ddf8f376SIra Weiny * Even if the above returned error, the pages may be
682ddf8f376SIra Weiny * written partially (e.g. -ENOSPC), so we wait for it.
683ddf8f376SIra Weiny * But the -EIO is special case, it may indicate the worst
684ddf8f376SIra Weiny * thing (e.g. bug) happened, so we avoid waiting for it.
685ddf8f376SIra Weiny */
686ccac11daSMiaohe Lin if (err != -EIO)
687ccac11daSMiaohe Lin __filemap_fdatawait_range(mapping, lstart, lend);
688ccac11daSMiaohe Lin }
689ccac11daSMiaohe Lin err2 = filemap_check_errors(mapping);
69028fd1298SOGAWA Hirofumi if (!err)
69128fd1298SOGAWA Hirofumi err = err2;
69228fd1298SOGAWA Hirofumi return err;
6931da177e4SLinus Torvalds }
694f6995585SChris Mason EXPORT_SYMBOL(filemap_write_and_wait_range);
6951da177e4SLinus Torvalds
__filemap_set_wb_err(struct address_space * mapping,int err)6965660e13dSJeff Layton void __filemap_set_wb_err(struct address_space *mapping, int err)
6975660e13dSJeff Layton {
6983acdfd28SJeff Layton errseq_t eseq = errseq_set(&mapping->wb_err, err);
6995660e13dSJeff Layton
7005660e13dSJeff Layton trace_filemap_set_wb_err(mapping, eseq);
7015660e13dSJeff Layton }
7025660e13dSJeff Layton EXPORT_SYMBOL(__filemap_set_wb_err);
7035660e13dSJeff Layton
7045660e13dSJeff Layton /**
7055660e13dSJeff Layton * file_check_and_advance_wb_err - report wb error (if any) that was previously
7065660e13dSJeff Layton * and advance wb_err to current one
7075660e13dSJeff Layton * @file: struct file on which the error is being reported
7085660e13dSJeff Layton *
7095660e13dSJeff Layton * When userland calls fsync (or something like nfsd does the equivalent), we
7105660e13dSJeff Layton * want to report any writeback errors that occurred since the last fsync (or
7115660e13dSJeff Layton * since the file was opened if there haven't been any).
7125660e13dSJeff Layton *
7135660e13dSJeff Layton * Grab the wb_err from the mapping. If it matches what we have in the file,
7145660e13dSJeff Layton * then just quickly return 0. The file is all caught up.
7155660e13dSJeff Layton *
7165660e13dSJeff Layton * If it doesn't match, then take the mapping value, set the "seen" flag in
7175660e13dSJeff Layton * it and try to swap it into place. If it works, or another task beat us
7185660e13dSJeff Layton * to it with the new value, then update the f_wb_err and return the error
7195660e13dSJeff Layton * portion. The error at this point must be reported via proper channels
7205660e13dSJeff Layton * (a'la fsync, or NFS COMMIT operation, etc.).
7215660e13dSJeff Layton *
7225660e13dSJeff Layton * While we handle mapping->wb_err with atomic operations, the f_wb_err
7235660e13dSJeff Layton * value is protected by the f_lock since we must ensure that it reflects
7245660e13dSJeff Layton * the latest value swapped in for this file descriptor.
725a862f68aSMike Rapoport *
726a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise.
7275660e13dSJeff Layton */
file_check_and_advance_wb_err(struct file * file)7285660e13dSJeff Layton int file_check_and_advance_wb_err(struct file *file)
7295660e13dSJeff Layton {
7305660e13dSJeff Layton int err = 0;
7315660e13dSJeff Layton errseq_t old = READ_ONCE(file->f_wb_err);
7325660e13dSJeff Layton struct address_space *mapping = file->f_mapping;
7335660e13dSJeff Layton
7345660e13dSJeff Layton /* Locklessly handle the common case where nothing has changed */
7355660e13dSJeff Layton if (errseq_check(&mapping->wb_err, old)) {
7365660e13dSJeff Layton /* Something changed, must use slow path */
7375660e13dSJeff Layton spin_lock(&file->f_lock);
7385660e13dSJeff Layton old = file->f_wb_err;
7395660e13dSJeff Layton err = errseq_check_and_advance(&mapping->wb_err,
7405660e13dSJeff Layton &file->f_wb_err);
7415660e13dSJeff Layton trace_file_check_and_advance_wb_err(file, old);
7425660e13dSJeff Layton spin_unlock(&file->f_lock);
7435660e13dSJeff Layton }
744f4e222c5SJeff Layton
745f4e222c5SJeff Layton /*
746f4e222c5SJeff Layton * We're mostly using this function as a drop in replacement for
747f4e222c5SJeff Layton * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect
748f4e222c5SJeff Layton * that the legacy code would have had on these flags.
749f4e222c5SJeff Layton */
750f4e222c5SJeff Layton clear_bit(AS_EIO, &mapping->flags);
751f4e222c5SJeff Layton clear_bit(AS_ENOSPC, &mapping->flags);
7525660e13dSJeff Layton return err;
7535660e13dSJeff Layton }
7545660e13dSJeff Layton EXPORT_SYMBOL(file_check_and_advance_wb_err);
7555660e13dSJeff Layton
7565660e13dSJeff Layton /**
7575660e13dSJeff Layton * file_write_and_wait_range - write out & wait on a file range
7585660e13dSJeff Layton * @file: file pointing to address_space with pages
7595660e13dSJeff Layton * @lstart: offset in bytes where the range starts
7605660e13dSJeff Layton * @lend: offset in bytes where the range ends (inclusive)
7615660e13dSJeff Layton *
7625660e13dSJeff Layton * Write out and wait upon file offsets lstart->lend, inclusive.
7635660e13dSJeff Layton *
7645660e13dSJeff Layton * Note that @lend is inclusive (describes the last byte to be written) so
7655660e13dSJeff Layton * that this function can be used to write to the very end-of-file (end = -1).
7665660e13dSJeff Layton *
7675660e13dSJeff Layton * After writing out and waiting on the data, we check and advance the
7685660e13dSJeff Layton * f_wb_err cursor to the latest value, and return any errors detected there.
769a862f68aSMike Rapoport *
770a862f68aSMike Rapoport * Return: %0 on success, negative error code otherwise.
7715660e13dSJeff Layton */
file_write_and_wait_range(struct file * file,loff_t lstart,loff_t lend)7725660e13dSJeff Layton int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
7735660e13dSJeff Layton {
7745660e13dSJeff Layton int err = 0, err2;
7755660e13dSJeff Layton struct address_space *mapping = file->f_mapping;
7765660e13dSJeff Layton
777feeb9b26SBrian Foster if (lend < lstart)
778feeb9b26SBrian Foster return 0;
779feeb9b26SBrian Foster
7809326c9b2SJeff Layton if (mapping_needs_writeback(mapping)) {
7815660e13dSJeff Layton err = __filemap_fdatawrite_range(mapping, lstart, lend,
7825660e13dSJeff Layton WB_SYNC_ALL);
7835660e13dSJeff Layton /* See comment of filemap_write_and_wait() */
7845660e13dSJeff Layton if (err != -EIO)
7855660e13dSJeff Layton __filemap_fdatawait_range(mapping, lstart, lend);
7865660e13dSJeff Layton }
7875660e13dSJeff Layton err2 = file_check_and_advance_wb_err(file);
7885660e13dSJeff Layton if (!err)
7895660e13dSJeff Layton err = err2;
7905660e13dSJeff Layton return err;
7915660e13dSJeff Layton }
7925660e13dSJeff Layton EXPORT_SYMBOL(file_write_and_wait_range);
7935660e13dSJeff Layton
794485bb99bSRandy Dunlap /**
7953720dd6dSVishal Moola (Oracle) * replace_page_cache_folio - replace a pagecache folio with a new one
7963720dd6dSVishal Moola (Oracle) * @old: folio to be replaced
7973720dd6dSVishal Moola (Oracle) * @new: folio to replace with
798ef6a3c63SMiklos Szeredi *
7993720dd6dSVishal Moola (Oracle) * This function replaces a folio in the pagecache with a new one. On
8003720dd6dSVishal Moola (Oracle) * success it acquires the pagecache reference for the new folio and
8013720dd6dSVishal Moola (Oracle) * drops it for the old folio. Both the old and new folios must be
8023720dd6dSVishal Moola (Oracle) * locked. This function does not add the new folio to the LRU, the
803ef6a3c63SMiklos Szeredi * caller must do that.
804ef6a3c63SMiklos Szeredi *
80574d60958SMatthew Wilcox * The remove + add is atomic. This function cannot fail.
806ef6a3c63SMiklos Szeredi */
replace_page_cache_folio(struct folio * old,struct folio * new)8073720dd6dSVishal Moola (Oracle) void replace_page_cache_folio(struct folio *old, struct folio *new)
808ef6a3c63SMiklos Szeredi {
80974d60958SMatthew Wilcox struct address_space *mapping = old->mapping;
810d2329aa0SMatthew Wilcox (Oracle) void (*free_folio)(struct folio *) = mapping->a_ops->free_folio;
81174d60958SMatthew Wilcox pgoff_t offset = old->index;
81274d60958SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, offset);
813ef6a3c63SMiklos Szeredi
8143720dd6dSVishal Moola (Oracle) VM_BUG_ON_FOLIO(!folio_test_locked(old), old);
8153720dd6dSVishal Moola (Oracle) VM_BUG_ON_FOLIO(!folio_test_locked(new), new);
8163720dd6dSVishal Moola (Oracle) VM_BUG_ON_FOLIO(new->mapping, new);
817ef6a3c63SMiklos Szeredi
8183720dd6dSVishal Moola (Oracle) folio_get(new);
819ef6a3c63SMiklos Szeredi new->mapping = mapping;
820ef6a3c63SMiklos Szeredi new->index = offset;
821ef6a3c63SMiklos Szeredi
8223720dd6dSVishal Moola (Oracle) mem_cgroup_migrate(old, new);
8230d1c2072SJohannes Weiner
82430472509SJohannes Weiner xas_lock_irq(&xas);
82574d60958SMatthew Wilcox xas_store(&xas, new);
8264165b9b4SMichal Hocko
82774d60958SMatthew Wilcox old->mapping = NULL;
82874d60958SMatthew Wilcox /* hugetlb pages do not participate in page cache accounting. */
8293720dd6dSVishal Moola (Oracle) if (!folio_test_hugetlb(old))
8303720dd6dSVishal Moola (Oracle) __lruvec_stat_sub_folio(old, NR_FILE_PAGES);
8313720dd6dSVishal Moola (Oracle) if (!folio_test_hugetlb(new))
8323720dd6dSVishal Moola (Oracle) __lruvec_stat_add_folio(new, NR_FILE_PAGES);
8333720dd6dSVishal Moola (Oracle) if (folio_test_swapbacked(old))
8343720dd6dSVishal Moola (Oracle) __lruvec_stat_sub_folio(old, NR_SHMEM);
8353720dd6dSVishal Moola (Oracle) if (folio_test_swapbacked(new))
8363720dd6dSVishal Moola (Oracle) __lruvec_stat_add_folio(new, NR_SHMEM);
83730472509SJohannes Weiner xas_unlock_irq(&xas);
838d2329aa0SMatthew Wilcox (Oracle) if (free_folio)
8393720dd6dSVishal Moola (Oracle) free_folio(old);
8403720dd6dSVishal Moola (Oracle) folio_put(old);
841ef6a3c63SMiklos Szeredi }
8423720dd6dSVishal Moola (Oracle) EXPORT_SYMBOL_GPL(replace_page_cache_folio);
843ef6a3c63SMiklos Szeredi
__filemap_add_folio(struct address_space * mapping,struct folio * folio,pgoff_t index,gfp_t gfp,void ** shadowp)8449dd3d069SMatthew Wilcox (Oracle) noinline int __filemap_add_folio(struct address_space *mapping,
8459dd3d069SMatthew Wilcox (Oracle) struct folio *folio, pgoff_t index, gfp_t gfp, void **shadowp)
8461da177e4SLinus Torvalds {
8479dd3d069SMatthew Wilcox (Oracle) XA_STATE(xas, &mapping->i_pages, index);
8489dd3d069SMatthew Wilcox (Oracle) int huge = folio_test_hugetlb(folio);
849722e9e5aSKairui Song void *alloced_shadow = NULL;
850722e9e5aSKairui Song int alloced_order = 0;
851da74240eSWaiman Long bool charged = false;
852d68eccadSMatthew Wilcox (Oracle) long nr = 1;
853e286781dSNick Piggin
8549dd3d069SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
8559dd3d069SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_swapbacked(folio), folio);
85674d60958SMatthew Wilcox mapping_set_update(&xas, mapping);
857e286781dSNick Piggin
8583fea5a49SJohannes Weiner if (!huge) {
859d68eccadSMatthew Wilcox (Oracle) int error = mem_cgroup_charge(folio, NULL, gfp);
8609dd3d069SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(index & (folio_nr_pages(folio) - 1), folio);
8613fea5a49SJohannes Weiner if (error)
862d68eccadSMatthew Wilcox (Oracle) return error;
863da74240eSWaiman Long charged = true;
864d68eccadSMatthew Wilcox (Oracle) xas_set_order(&xas, index, folio_order(folio));
865d68eccadSMatthew Wilcox (Oracle) nr = folio_nr_pages(folio);
8663fea5a49SJohannes Weiner }
8673fea5a49SJohannes Weiner
868198b62f8SMatthew Wilcox (Oracle) gfp &= GFP_RECLAIM_MASK;
869d68eccadSMatthew Wilcox (Oracle) folio_ref_add(folio, nr);
870d68eccadSMatthew Wilcox (Oracle) folio->mapping = mapping;
871d68eccadSMatthew Wilcox (Oracle) folio->index = xas.xa_index;
872198b62f8SMatthew Wilcox (Oracle)
873722e9e5aSKairui Song for (;;) {
874722e9e5aSKairui Song int order = -1, split_order = 0;
875198b62f8SMatthew Wilcox (Oracle) void *entry, *old = NULL;
876198b62f8SMatthew Wilcox (Oracle)
87774d60958SMatthew Wilcox xas_lock_irq(&xas);
878198b62f8SMatthew Wilcox (Oracle) xas_for_each_conflict(&xas, entry) {
879198b62f8SMatthew Wilcox (Oracle) old = entry;
880198b62f8SMatthew Wilcox (Oracle) if (!xa_is_value(entry)) {
88174d60958SMatthew Wilcox xas_set_err(&xas, -EEXIST);
882198b62f8SMatthew Wilcox (Oracle) goto unlock;
883198b62f8SMatthew Wilcox (Oracle) }
884722e9e5aSKairui Song /*
885722e9e5aSKairui Song * If a larger entry exists,
886722e9e5aSKairui Song * it will be the first and only entry iterated.
887722e9e5aSKairui Song */
888722e9e5aSKairui Song if (order == -1)
889722e9e5aSKairui Song order = xas_get_order(&xas);
890722e9e5aSKairui Song }
891722e9e5aSKairui Song
892722e9e5aSKairui Song /* entry may have changed before we re-acquire the lock */
893722e9e5aSKairui Song if (alloced_order && (old != alloced_shadow || order != alloced_order)) {
894722e9e5aSKairui Song xas_destroy(&xas);
895722e9e5aSKairui Song alloced_order = 0;
896198b62f8SMatthew Wilcox (Oracle) }
897198b62f8SMatthew Wilcox (Oracle)
898198b62f8SMatthew Wilcox (Oracle) if (old) {
899722e9e5aSKairui Song if (order > 0 && order > folio_order(folio)) {
900d68eccadSMatthew Wilcox (Oracle) /* How to handle large swap entries? */
901d68eccadSMatthew Wilcox (Oracle) BUG_ON(shmem_mapping(mapping));
902722e9e5aSKairui Song if (!alloced_order) {
903722e9e5aSKairui Song split_order = order;
904722e9e5aSKairui Song goto unlock;
905722e9e5aSKairui Song }
906198b62f8SMatthew Wilcox (Oracle) xas_split(&xas, old, order);
907198b62f8SMatthew Wilcox (Oracle) xas_reset(&xas);
908198b62f8SMatthew Wilcox (Oracle) }
909722e9e5aSKairui Song if (shadowp)
910722e9e5aSKairui Song *shadowp = old;
911198b62f8SMatthew Wilcox (Oracle) }
912198b62f8SMatthew Wilcox (Oracle)
9139dd3d069SMatthew Wilcox (Oracle) xas_store(&xas, folio);
91474d60958SMatthew Wilcox if (xas_error(&xas))
91574d60958SMatthew Wilcox goto unlock;
9164165b9b4SMichal Hocko
917d68eccadSMatthew Wilcox (Oracle) mapping->nrpages += nr;
91874d60958SMatthew Wilcox
91974d60958SMatthew Wilcox /* hugetlb pages do not participate in page cache accounting */
920d68eccadSMatthew Wilcox (Oracle) if (!huge) {
921d68eccadSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_FILE_PAGES, nr);
922d68eccadSMatthew Wilcox (Oracle) if (folio_test_pmd_mappable(folio))
923d68eccadSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio,
924d68eccadSMatthew Wilcox (Oracle) NR_FILE_THPS, nr);
925d68eccadSMatthew Wilcox (Oracle) }
926722e9e5aSKairui Song
92774d60958SMatthew Wilcox unlock:
92874d60958SMatthew Wilcox xas_unlock_irq(&xas);
929722e9e5aSKairui Song
930722e9e5aSKairui Song /* split needed, alloc here and retry. */
931722e9e5aSKairui Song if (split_order) {
932722e9e5aSKairui Song xas_split_alloc(&xas, old, split_order, gfp);
933722e9e5aSKairui Song if (xas_error(&xas))
934722e9e5aSKairui Song goto error;
935722e9e5aSKairui Song alloced_shadow = old;
936722e9e5aSKairui Song alloced_order = split_order;
937722e9e5aSKairui Song xas_reset(&xas);
938722e9e5aSKairui Song continue;
939722e9e5aSKairui Song }
940722e9e5aSKairui Song
941722e9e5aSKairui Song if (!xas_nomem(&xas, gfp))
942722e9e5aSKairui Song break;
943722e9e5aSKairui Song }
94474d60958SMatthew Wilcox
945d68eccadSMatthew Wilcox (Oracle) if (xas_error(&xas))
94674d60958SMatthew Wilcox goto error;
94774d60958SMatthew Wilcox
948a0580c6fSMatthew Wilcox (Oracle) trace_mm_filemap_add_to_page_cache(folio);
94966a0c8eeSKirill A. Shutemov return 0;
95074d60958SMatthew Wilcox error:
951d68eccadSMatthew Wilcox (Oracle) if (charged)
952d68eccadSMatthew Wilcox (Oracle) mem_cgroup_uncharge(folio);
9539dd3d069SMatthew Wilcox (Oracle) folio->mapping = NULL;
954b85e0effSHugh Dickins /* Leave page->index set: truncation relies upon it */
955d68eccadSMatthew Wilcox (Oracle) folio_put_refs(folio, nr);
956d68eccadSMatthew Wilcox (Oracle) return xas_error(&xas);
9571da177e4SLinus Torvalds }
9589dd3d069SMatthew Wilcox (Oracle) ALLOW_ERROR_INJECTION(__filemap_add_folio, ERRNO);
959a528910eSJohannes Weiner
filemap_add_folio(struct address_space * mapping,struct folio * folio,pgoff_t index,gfp_t gfp)9609dd3d069SMatthew Wilcox (Oracle) int filemap_add_folio(struct address_space *mapping, struct folio *folio,
9619dd3d069SMatthew Wilcox (Oracle) pgoff_t index, gfp_t gfp)
9621da177e4SLinus Torvalds {
963a528910eSJohannes Weiner void *shadow = NULL;
9644f98a2feSRik van Riel int ret;
9654f98a2feSRik van Riel
9669dd3d069SMatthew Wilcox (Oracle) __folio_set_locked(folio);
9679dd3d069SMatthew Wilcox (Oracle) ret = __filemap_add_folio(mapping, folio, index, gfp, &shadow);
968a528910eSJohannes Weiner if (unlikely(ret))
9699dd3d069SMatthew Wilcox (Oracle) __folio_clear_locked(folio);
970a528910eSJohannes Weiner else {
971a528910eSJohannes Weiner /*
9729dd3d069SMatthew Wilcox (Oracle) * The folio might have been evicted from cache only
973a528910eSJohannes Weiner * recently, in which case it should be activated like
9749dd3d069SMatthew Wilcox (Oracle) * any other repeatedly accessed folio.
9759dd3d069SMatthew Wilcox (Oracle) * The exception is folios getting rewritten; evicting other
976f0281a00SRik van Riel * data from the working set, only to cache data that will
977f0281a00SRik van Riel * get overwritten with something else, is a waste of memory.
978a528910eSJohannes Weiner */
9799dd3d069SMatthew Wilcox (Oracle) WARN_ON_ONCE(folio_test_active(folio));
9809dd3d069SMatthew Wilcox (Oracle) if (!(gfp & __GFP_WRITE) && shadow)
9819dd3d069SMatthew Wilcox (Oracle) workingset_refault(folio, shadow);
9829dd3d069SMatthew Wilcox (Oracle) folio_add_lru(folio);
983a528910eSJohannes Weiner }
9841da177e4SLinus Torvalds return ret;
9851da177e4SLinus Torvalds }
9869dd3d069SMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(filemap_add_folio);
9871da177e4SLinus Torvalds
98844110fe3SPaul Jackson #ifdef CONFIG_NUMA
filemap_alloc_folio(gfp_t gfp,unsigned int order)989bb3c579eSMatthew Wilcox (Oracle) struct folio *filemap_alloc_folio(gfp_t gfp, unsigned int order)
99044110fe3SPaul Jackson {
991c0ff7453SMiao Xie int n;
992bb3c579eSMatthew Wilcox (Oracle) struct folio *folio;
993c0ff7453SMiao Xie
99444110fe3SPaul Jackson if (cpuset_do_page_mem_spread()) {
995cc9a6c87SMel Gorman unsigned int cpuset_mems_cookie;
996cc9a6c87SMel Gorman do {
997d26914d1SMel Gorman cpuset_mems_cookie = read_mems_allowed_begin();
998c0ff7453SMiao Xie n = cpuset_mem_spread_node();
999bb3c579eSMatthew Wilcox (Oracle) folio = __folio_alloc_node(gfp, order, n);
1000bb3c579eSMatthew Wilcox (Oracle) } while (!folio && read_mems_allowed_retry(cpuset_mems_cookie));
1001cc9a6c87SMel Gorman
1002bb3c579eSMatthew Wilcox (Oracle) return folio;
100344110fe3SPaul Jackson }
1004bb3c579eSMatthew Wilcox (Oracle) return folio_alloc(gfp, order);
100544110fe3SPaul Jackson }
1006bb3c579eSMatthew Wilcox (Oracle) EXPORT_SYMBOL(filemap_alloc_folio);
100744110fe3SPaul Jackson #endif
100844110fe3SPaul Jackson
10091da177e4SLinus Torvalds /*
10107506ae6aSJan Kara * filemap_invalidate_lock_two - lock invalidate_lock for two mappings
10117506ae6aSJan Kara *
10127506ae6aSJan Kara * Lock exclusively invalidate_lock of any passed mapping that is not NULL.
10137506ae6aSJan Kara *
10147506ae6aSJan Kara * @mapping1: the first mapping to lock
10157506ae6aSJan Kara * @mapping2: the second mapping to lock
10167506ae6aSJan Kara */
filemap_invalidate_lock_two(struct address_space * mapping1,struct address_space * mapping2)10177506ae6aSJan Kara void filemap_invalidate_lock_two(struct address_space *mapping1,
10187506ae6aSJan Kara struct address_space *mapping2)
10197506ae6aSJan Kara {
10207506ae6aSJan Kara if (mapping1 > mapping2)
10217506ae6aSJan Kara swap(mapping1, mapping2);
10227506ae6aSJan Kara if (mapping1)
10237506ae6aSJan Kara down_write(&mapping1->invalidate_lock);
10247506ae6aSJan Kara if (mapping2 && mapping1 != mapping2)
10257506ae6aSJan Kara down_write_nested(&mapping2->invalidate_lock, 1);
10267506ae6aSJan Kara }
10277506ae6aSJan Kara EXPORT_SYMBOL(filemap_invalidate_lock_two);
10287506ae6aSJan Kara
10297506ae6aSJan Kara /*
10307506ae6aSJan Kara * filemap_invalidate_unlock_two - unlock invalidate_lock for two mappings
10317506ae6aSJan Kara *
10327506ae6aSJan Kara * Unlock exclusive invalidate_lock of any passed mapping that is not NULL.
10337506ae6aSJan Kara *
10347506ae6aSJan Kara * @mapping1: the first mapping to unlock
10357506ae6aSJan Kara * @mapping2: the second mapping to unlock
10367506ae6aSJan Kara */
filemap_invalidate_unlock_two(struct address_space * mapping1,struct address_space * mapping2)10377506ae6aSJan Kara void filemap_invalidate_unlock_two(struct address_space *mapping1,
10387506ae6aSJan Kara struct address_space *mapping2)
10397506ae6aSJan Kara {
10407506ae6aSJan Kara if (mapping1)
10417506ae6aSJan Kara up_write(&mapping1->invalidate_lock);
10427506ae6aSJan Kara if (mapping2 && mapping1 != mapping2)
10437506ae6aSJan Kara up_write(&mapping2->invalidate_lock);
10447506ae6aSJan Kara }
10457506ae6aSJan Kara EXPORT_SYMBOL(filemap_invalidate_unlock_two);
10467506ae6aSJan Kara
10477506ae6aSJan Kara /*
10481da177e4SLinus Torvalds * In order to wait for pages to become available there must be
10491da177e4SLinus Torvalds * waitqueues associated with pages. By using a hash table of
10501da177e4SLinus Torvalds * waitqueues where the bucket discipline is to maintain all
10511da177e4SLinus Torvalds * waiters on the same queue and wake all when any of the pages
10521da177e4SLinus Torvalds * become available, and for the woken contexts to check to be
10531da177e4SLinus Torvalds * sure the appropriate page became available, this saves space
10541da177e4SLinus Torvalds * at a cost of "thundering herd" phenomena during rare hash
10551da177e4SLinus Torvalds * collisions.
10561da177e4SLinus Torvalds */
105762906027SNicholas Piggin #define PAGE_WAIT_TABLE_BITS 8
105862906027SNicholas Piggin #define PAGE_WAIT_TABLE_SIZE (1 << PAGE_WAIT_TABLE_BITS)
1059df4d4f12SMatthew Wilcox (Oracle) static wait_queue_head_t folio_wait_table[PAGE_WAIT_TABLE_SIZE] __cacheline_aligned;
106062906027SNicholas Piggin
folio_waitqueue(struct folio * folio)1061df4d4f12SMatthew Wilcox (Oracle) static wait_queue_head_t *folio_waitqueue(struct folio *folio)
10621da177e4SLinus Torvalds {
1063df4d4f12SMatthew Wilcox (Oracle) return &folio_wait_table[hash_ptr(folio, PAGE_WAIT_TABLE_BITS)];
10641da177e4SLinus Torvalds }
106562906027SNicholas Piggin
pagecache_init(void)106662906027SNicholas Piggin void __init pagecache_init(void)
106762906027SNicholas Piggin {
106862906027SNicholas Piggin int i;
106962906027SNicholas Piggin
107062906027SNicholas Piggin for (i = 0; i < PAGE_WAIT_TABLE_SIZE; i++)
1071df4d4f12SMatthew Wilcox (Oracle) init_waitqueue_head(&folio_wait_table[i]);
107262906027SNicholas Piggin
107362906027SNicholas Piggin page_writeback_init();
107462906027SNicholas Piggin }
107562906027SNicholas Piggin
10765ef64cc8SLinus Torvalds /*
10775ef64cc8SLinus Torvalds * The page wait code treats the "wait->flags" somewhat unusually, because
10785868ec26SLinus Torvalds * we have multiple different kinds of waits, not just the usual "exclusive"
10795ef64cc8SLinus Torvalds * one.
10805ef64cc8SLinus Torvalds *
10815ef64cc8SLinus Torvalds * We have:
10825ef64cc8SLinus Torvalds *
10835ef64cc8SLinus Torvalds * (a) no special bits set:
10845ef64cc8SLinus Torvalds *
10855ef64cc8SLinus Torvalds * We're just waiting for the bit to be released, and when a waker
10865ef64cc8SLinus Torvalds * calls the wakeup function, we set WQ_FLAG_WOKEN and wake it up,
10875ef64cc8SLinus Torvalds * and remove it from the wait queue.
10885ef64cc8SLinus Torvalds *
10895ef64cc8SLinus Torvalds * Simple and straightforward.
10905ef64cc8SLinus Torvalds *
10915ef64cc8SLinus Torvalds * (b) WQ_FLAG_EXCLUSIVE:
10925ef64cc8SLinus Torvalds *
10935ef64cc8SLinus Torvalds * The waiter is waiting to get the lock, and only one waiter should
10945ef64cc8SLinus Torvalds * be woken up to avoid any thundering herd behavior. We'll set the
10955ef64cc8SLinus Torvalds * WQ_FLAG_WOKEN bit, wake it up, and remove it from the wait queue.
10965ef64cc8SLinus Torvalds *
10975ef64cc8SLinus Torvalds * This is the traditional exclusive wait.
10985ef64cc8SLinus Torvalds *
10995868ec26SLinus Torvalds * (c) WQ_FLAG_EXCLUSIVE | WQ_FLAG_CUSTOM:
11005ef64cc8SLinus Torvalds *
11015ef64cc8SLinus Torvalds * The waiter is waiting to get the bit, and additionally wants the
11025ef64cc8SLinus Torvalds * lock to be transferred to it for fair lock behavior. If the lock
11035ef64cc8SLinus Torvalds * cannot be taken, we stop walking the wait queue without waking
11045ef64cc8SLinus Torvalds * the waiter.
11055ef64cc8SLinus Torvalds *
11065ef64cc8SLinus Torvalds * This is the "fair lock handoff" case, and in addition to setting
11075ef64cc8SLinus Torvalds * WQ_FLAG_WOKEN, we set WQ_FLAG_DONE to let the waiter easily see
11085ef64cc8SLinus Torvalds * that it now has the lock.
11095ef64cc8SLinus Torvalds */
wake_page_function(wait_queue_entry_t * wait,unsigned mode,int sync,void * arg)1110ac6424b9SIngo Molnar static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *arg)
111162906027SNicholas Piggin {
11125ef64cc8SLinus Torvalds unsigned int flags;
111362906027SNicholas Piggin struct wait_page_key *key = arg;
111462906027SNicholas Piggin struct wait_page_queue *wait_page
111562906027SNicholas Piggin = container_of(wait, struct wait_page_queue, wait);
111662906027SNicholas Piggin
1117cdc8fcb4SLinus Torvalds if (!wake_page_match(wait_page, key))
111862906027SNicholas Piggin return 0;
11193510ca20SLinus Torvalds
11209a1ea439SHugh Dickins /*
11215ef64cc8SLinus Torvalds * If it's a lock handoff wait, we get the bit for it, and
11225ef64cc8SLinus Torvalds * stop walking (and do not wake it up) if we can't.
11239a1ea439SHugh Dickins */
11245ef64cc8SLinus Torvalds flags = wait->flags;
11255ef64cc8SLinus Torvalds if (flags & WQ_FLAG_EXCLUSIVE) {
1126df4d4f12SMatthew Wilcox (Oracle) if (test_bit(key->bit_nr, &key->folio->flags))
11275ef64cc8SLinus Torvalds return -1;
11285ef64cc8SLinus Torvalds if (flags & WQ_FLAG_CUSTOM) {
1129df4d4f12SMatthew Wilcox (Oracle) if (test_and_set_bit(key->bit_nr, &key->folio->flags))
11303510ca20SLinus Torvalds return -1;
11315ef64cc8SLinus Torvalds flags |= WQ_FLAG_DONE;
11322a9127fcSLinus Torvalds }
11335ef64cc8SLinus Torvalds }
113462906027SNicholas Piggin
11355ef64cc8SLinus Torvalds /*
11365ef64cc8SLinus Torvalds * We are holding the wait-queue lock, but the waiter that
11375ef64cc8SLinus Torvalds * is waiting for this will be checking the flags without
11385ef64cc8SLinus Torvalds * any locking.
11395ef64cc8SLinus Torvalds *
11405ef64cc8SLinus Torvalds * So update the flags atomically, and wake up the waiter
11415ef64cc8SLinus Torvalds * afterwards to avoid any races. This store-release pairs
1142101c0bf6SMatthew Wilcox (Oracle) * with the load-acquire in folio_wait_bit_common().
11435ef64cc8SLinus Torvalds */
11445ef64cc8SLinus Torvalds smp_store_release(&wait->flags, flags | WQ_FLAG_WOKEN);
11452a9127fcSLinus Torvalds wake_up_state(wait->private, mode);
11462a9127fcSLinus Torvalds
11472a9127fcSLinus Torvalds /*
11482a9127fcSLinus Torvalds * Ok, we have successfully done what we're waiting for,
11492a9127fcSLinus Torvalds * and we can unconditionally remove the wait entry.
11502a9127fcSLinus Torvalds *
11515ef64cc8SLinus Torvalds * Note that this pairs with the "finish_wait()" in the
11525ef64cc8SLinus Torvalds * waiter, and has to be the absolute last thing we do.
11535ef64cc8SLinus Torvalds * After this list_del_init(&wait->entry) the wait entry
11542a9127fcSLinus Torvalds * might be de-allocated and the process might even have
11552a9127fcSLinus Torvalds * exited.
11562a9127fcSLinus Torvalds */
1157c6fe44d9SLinus Torvalds list_del_init_careful(&wait->entry);
11585ef64cc8SLinus Torvalds return (flags & WQ_FLAG_EXCLUSIVE) != 0;
115962906027SNicholas Piggin }
116062906027SNicholas Piggin
folio_wake_bit(struct folio * folio,int bit_nr)11616974d7c9SMatthew Wilcox (Oracle) static void folio_wake_bit(struct folio *folio, int bit_nr)
116262906027SNicholas Piggin {
1163df4d4f12SMatthew Wilcox (Oracle) wait_queue_head_t *q = folio_waitqueue(folio);
116462906027SNicholas Piggin struct wait_page_key key;
116562906027SNicholas Piggin unsigned long flags;
116611a19c7bSTim Chen wait_queue_entry_t bookmark;
116762906027SNicholas Piggin
1168df4d4f12SMatthew Wilcox (Oracle) key.folio = folio;
116962906027SNicholas Piggin key.bit_nr = bit_nr;
117062906027SNicholas Piggin key.page_match = 0;
117162906027SNicholas Piggin
117211a19c7bSTim Chen bookmark.flags = 0;
117311a19c7bSTim Chen bookmark.private = NULL;
117411a19c7bSTim Chen bookmark.func = NULL;
117511a19c7bSTim Chen INIT_LIST_HEAD(&bookmark.entry);
117611a19c7bSTim Chen
117762906027SNicholas Piggin spin_lock_irqsave(&q->lock, flags);
117811a19c7bSTim Chen __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
117911a19c7bSTim Chen
118011a19c7bSTim Chen while (bookmark.flags & WQ_FLAG_BOOKMARK) {
118111a19c7bSTim Chen /*
118211a19c7bSTim Chen * Take a breather from holding the lock,
118311a19c7bSTim Chen * allow pages that finish wake up asynchronously
118411a19c7bSTim Chen * to acquire the lock and remove themselves
118511a19c7bSTim Chen * from wait queue
118611a19c7bSTim Chen */
118711a19c7bSTim Chen spin_unlock_irqrestore(&q->lock, flags);
118811a19c7bSTim Chen cpu_relax();
118911a19c7bSTim Chen spin_lock_irqsave(&q->lock, flags);
119011a19c7bSTim Chen __wake_up_locked_key_bookmark(q, TASK_NORMAL, &key, &bookmark);
119111a19c7bSTim Chen }
119211a19c7bSTim Chen
119362906027SNicholas Piggin /*
1194bb43b14bSHugh Dickins * It's possible to miss clearing waiters here, when we woke our page
1195bb43b14bSHugh Dickins * waiters, but the hashed waitqueue has waiters for other pages on it.
119662906027SNicholas Piggin * That's okay, it's a rare case. The next waker will clear it.
1197bb43b14bSHugh Dickins *
1198bb43b14bSHugh Dickins * Note that, depending on the page pool (buddy, hugetlb, ZONE_DEVICE,
1199bb43b14bSHugh Dickins * other), the flag may be cleared in the course of freeing the page;
1200bb43b14bSHugh Dickins * but that is not required for correctness.
120162906027SNicholas Piggin */
1202bb43b14bSHugh Dickins if (!waitqueue_active(q) || !key.page_match)
1203bb43b14bSHugh Dickins folio_clear_waiters(folio);
1204bb43b14bSHugh Dickins
120562906027SNicholas Piggin spin_unlock_irqrestore(&q->lock, flags);
120662906027SNicholas Piggin }
120774d81bfaSNicholas Piggin
folio_wake(struct folio * folio,int bit)12084268b480SMatthew Wilcox (Oracle) static void folio_wake(struct folio *folio, int bit)
120974d81bfaSNicholas Piggin {
12104268b480SMatthew Wilcox (Oracle) if (!folio_test_waiters(folio))
121174d81bfaSNicholas Piggin return;
12126974d7c9SMatthew Wilcox (Oracle) folio_wake_bit(folio, bit);
121374d81bfaSNicholas Piggin }
121462906027SNicholas Piggin
12159a1ea439SHugh Dickins /*
1216101c0bf6SMatthew Wilcox (Oracle) * A choice of three behaviors for folio_wait_bit_common():
12179a1ea439SHugh Dickins */
12189a1ea439SHugh Dickins enum behavior {
12199a1ea439SHugh Dickins EXCLUSIVE, /* Hold ref to page and take the bit when woken, like
12207c23c782SMatthew Wilcox (Oracle) * __folio_lock() waiting on then setting PG_locked.
12219a1ea439SHugh Dickins */
12229a1ea439SHugh Dickins SHARED, /* Hold ref to page and check the bit when woken, like
12239f2b04a2SMatthew Wilcox (Oracle) * folio_wait_writeback() waiting on PG_writeback.
12249a1ea439SHugh Dickins */
12259a1ea439SHugh Dickins DROP, /* Drop ref to page before wait, no check when woken,
12269f2b04a2SMatthew Wilcox (Oracle) * like folio_put_wait_locked() on PG_locked.
12279a1ea439SHugh Dickins */
12289a1ea439SHugh Dickins };
12299a1ea439SHugh Dickins
12302a9127fcSLinus Torvalds /*
1231101c0bf6SMatthew Wilcox (Oracle) * Attempt to check (or get) the folio flag, and mark us done
12325ef64cc8SLinus Torvalds * if successful.
12332a9127fcSLinus Torvalds */
folio_trylock_flag(struct folio * folio,int bit_nr,struct wait_queue_entry * wait)1234101c0bf6SMatthew Wilcox (Oracle) static inline bool folio_trylock_flag(struct folio *folio, int bit_nr,
12352a9127fcSLinus Torvalds struct wait_queue_entry *wait)
12362a9127fcSLinus Torvalds {
12372a9127fcSLinus Torvalds if (wait->flags & WQ_FLAG_EXCLUSIVE) {
1238101c0bf6SMatthew Wilcox (Oracle) if (test_and_set_bit(bit_nr, &folio->flags))
12392a9127fcSLinus Torvalds return false;
1240101c0bf6SMatthew Wilcox (Oracle) } else if (test_bit(bit_nr, &folio->flags))
12412a9127fcSLinus Torvalds return false;
12422a9127fcSLinus Torvalds
12435ef64cc8SLinus Torvalds wait->flags |= WQ_FLAG_WOKEN | WQ_FLAG_DONE;
12442a9127fcSLinus Torvalds return true;
12452a9127fcSLinus Torvalds }
12462a9127fcSLinus Torvalds
12475ef64cc8SLinus Torvalds /* How many times do we accept lock stealing from under a waiter? */
12485ef64cc8SLinus Torvalds int sysctl_page_lock_unfairness = 5;
12495ef64cc8SLinus Torvalds
folio_wait_bit_common(struct folio * folio,int bit_nr,int state,enum behavior behavior)1250101c0bf6SMatthew Wilcox (Oracle) static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
1251101c0bf6SMatthew Wilcox (Oracle) int state, enum behavior behavior)
125262906027SNicholas Piggin {
1253df4d4f12SMatthew Wilcox (Oracle) wait_queue_head_t *q = folio_waitqueue(folio);
12545ef64cc8SLinus Torvalds int unfairness = sysctl_page_lock_unfairness;
125562906027SNicholas Piggin struct wait_page_queue wait_page;
1256ac6424b9SIngo Molnar wait_queue_entry_t *wait = &wait_page.wait;
1257b1d29ba8SJohannes Weiner bool thrashing = false;
1258eb414681SJohannes Weiner unsigned long pflags;
1259aa1cf99bSYang Yang bool in_thrashing;
126062906027SNicholas Piggin
1261eb414681SJohannes Weiner if (bit_nr == PG_locked &&
1262101c0bf6SMatthew Wilcox (Oracle) !folio_test_uptodate(folio) && folio_test_workingset(folio)) {
1263aa1cf99bSYang Yang delayacct_thrashing_start(&in_thrashing);
1264eb414681SJohannes Weiner psi_memstall_enter(&pflags);
1265b1d29ba8SJohannes Weiner thrashing = true;
1266b1d29ba8SJohannes Weiner }
1267b1d29ba8SJohannes Weiner
126862906027SNicholas Piggin init_wait(wait);
126962906027SNicholas Piggin wait->func = wake_page_function;
1270df4d4f12SMatthew Wilcox (Oracle) wait_page.folio = folio;
127162906027SNicholas Piggin wait_page.bit_nr = bit_nr;
127262906027SNicholas Piggin
12735ef64cc8SLinus Torvalds repeat:
12745ef64cc8SLinus Torvalds wait->flags = 0;
12755ef64cc8SLinus Torvalds if (behavior == EXCLUSIVE) {
12765ef64cc8SLinus Torvalds wait->flags = WQ_FLAG_EXCLUSIVE;
12775ef64cc8SLinus Torvalds if (--unfairness < 0)
12785ef64cc8SLinus Torvalds wait->flags |= WQ_FLAG_CUSTOM;
12795ef64cc8SLinus Torvalds }
12805ef64cc8SLinus Torvalds
12812a9127fcSLinus Torvalds /*
12822a9127fcSLinus Torvalds * Do one last check whether we can get the
12832a9127fcSLinus Torvalds * page bit synchronously.
12842a9127fcSLinus Torvalds *
1285101c0bf6SMatthew Wilcox (Oracle) * Do the folio_set_waiters() marking before that
12862a9127fcSLinus Torvalds * to let any waker we _just_ missed know they
12872a9127fcSLinus Torvalds * need to wake us up (otherwise they'll never
12882a9127fcSLinus Torvalds * even go to the slow case that looks at the
12892a9127fcSLinus Torvalds * page queue), and add ourselves to the wait
12902a9127fcSLinus Torvalds * queue if we need to sleep.
12912a9127fcSLinus Torvalds *
12922a9127fcSLinus Torvalds * This part needs to be done under the queue
12932a9127fcSLinus Torvalds * lock to avoid races.
12942a9127fcSLinus Torvalds */
129562906027SNicholas Piggin spin_lock_irq(&q->lock);
1296101c0bf6SMatthew Wilcox (Oracle) folio_set_waiters(folio);
1297101c0bf6SMatthew Wilcox (Oracle) if (!folio_trylock_flag(folio, bit_nr, wait))
12982a9127fcSLinus Torvalds __add_wait_queue_entry_tail(q, wait);
129962906027SNicholas Piggin spin_unlock_irq(&q->lock);
130062906027SNicholas Piggin
13012a9127fcSLinus Torvalds /*
13022a9127fcSLinus Torvalds * From now on, all the logic will be based on
13035ef64cc8SLinus Torvalds * the WQ_FLAG_WOKEN and WQ_FLAG_DONE flag, to
13045ef64cc8SLinus Torvalds * see whether the page bit testing has already
13055ef64cc8SLinus Torvalds * been done by the wake function.
13062a9127fcSLinus Torvalds *
1307101c0bf6SMatthew Wilcox (Oracle) * We can drop our reference to the folio.
13082a9127fcSLinus Torvalds */
13099a1ea439SHugh Dickins if (behavior == DROP)
1310101c0bf6SMatthew Wilcox (Oracle) folio_put(folio);
131162906027SNicholas Piggin
13125ef64cc8SLinus Torvalds /*
13135ef64cc8SLinus Torvalds * Note that until the "finish_wait()", or until
13145ef64cc8SLinus Torvalds * we see the WQ_FLAG_WOKEN flag, we need to
13155ef64cc8SLinus Torvalds * be very careful with the 'wait->flags', because
13165ef64cc8SLinus Torvalds * we may race with a waker that sets them.
13175ef64cc8SLinus Torvalds */
13182a9127fcSLinus Torvalds for (;;) {
13195ef64cc8SLinus Torvalds unsigned int flags;
13205ef64cc8SLinus Torvalds
13212a9127fcSLinus Torvalds set_current_state(state);
13222a9127fcSLinus Torvalds
13235ef64cc8SLinus Torvalds /* Loop until we've been woken or interrupted */
13245ef64cc8SLinus Torvalds flags = smp_load_acquire(&wait->flags);
13255ef64cc8SLinus Torvalds if (!(flags & WQ_FLAG_WOKEN)) {
13262a9127fcSLinus Torvalds if (signal_pending_state(state, current))
13272a9127fcSLinus Torvalds break;
13282a9127fcSLinus Torvalds
13299a1ea439SHugh Dickins io_schedule();
13305ef64cc8SLinus Torvalds continue;
133162906027SNicholas Piggin }
133262906027SNicholas Piggin
13335ef64cc8SLinus Torvalds /* If we were non-exclusive, we're done */
13345ef64cc8SLinus Torvalds if (behavior != EXCLUSIVE)
13355ef64cc8SLinus Torvalds break;
13365ef64cc8SLinus Torvalds
13375ef64cc8SLinus Torvalds /* If the waker got the lock for us, we're done */
13385ef64cc8SLinus Torvalds if (flags & WQ_FLAG_DONE)
13395ef64cc8SLinus Torvalds break;
13405ef64cc8SLinus Torvalds
13415ef64cc8SLinus Torvalds /*
13425ef64cc8SLinus Torvalds * Otherwise, if we're getting the lock, we need to
13435ef64cc8SLinus Torvalds * try to get it ourselves.
13445ef64cc8SLinus Torvalds *
13455ef64cc8SLinus Torvalds * And if that fails, we'll have to retry this all.
13465ef64cc8SLinus Torvalds */
1347101c0bf6SMatthew Wilcox (Oracle) if (unlikely(test_and_set_bit(bit_nr, folio_flags(folio, 0))))
13485ef64cc8SLinus Torvalds goto repeat;
13495ef64cc8SLinus Torvalds
13505ef64cc8SLinus Torvalds wait->flags |= WQ_FLAG_DONE;
13515ef64cc8SLinus Torvalds break;
13525ef64cc8SLinus Torvalds }
13535ef64cc8SLinus Torvalds
13545ef64cc8SLinus Torvalds /*
13555ef64cc8SLinus Torvalds * If a signal happened, this 'finish_wait()' may remove the last
1356101c0bf6SMatthew Wilcox (Oracle) * waiter from the wait-queues, but the folio waiters bit will remain
13575ef64cc8SLinus Torvalds * set. That's ok. The next wakeup will take care of it, and trying
13585ef64cc8SLinus Torvalds * to do it here would be difficult and prone to races.
13595ef64cc8SLinus Torvalds */
136062906027SNicholas Piggin finish_wait(q, wait);
136162906027SNicholas Piggin
1362eb414681SJohannes Weiner if (thrashing) {
1363aa1cf99bSYang Yang delayacct_thrashing_end(&in_thrashing);
1364eb414681SJohannes Weiner psi_memstall_leave(&pflags);
1365eb414681SJohannes Weiner }
1366b1d29ba8SJohannes Weiner
136762906027SNicholas Piggin /*
13685ef64cc8SLinus Torvalds * NOTE! The wait->flags weren't stable until we've done the
13695ef64cc8SLinus Torvalds * 'finish_wait()', and we could have exited the loop above due
13705ef64cc8SLinus Torvalds * to a signal, and had a wakeup event happen after the signal
13715ef64cc8SLinus Torvalds * test but before the 'finish_wait()'.
13725ef64cc8SLinus Torvalds *
13735ef64cc8SLinus Torvalds * So only after the finish_wait() can we reliably determine
13745ef64cc8SLinus Torvalds * if we got woken up or not, so we can now figure out the final
13755ef64cc8SLinus Torvalds * return value based on that state without races.
13765ef64cc8SLinus Torvalds *
13775ef64cc8SLinus Torvalds * Also note that WQ_FLAG_WOKEN is sufficient for a non-exclusive
13785ef64cc8SLinus Torvalds * waiter, but an exclusive one requires WQ_FLAG_DONE.
137962906027SNicholas Piggin */
13805ef64cc8SLinus Torvalds if (behavior == EXCLUSIVE)
13815ef64cc8SLinus Torvalds return wait->flags & WQ_FLAG_DONE ? 0 : -EINTR;
138262906027SNicholas Piggin
13832a9127fcSLinus Torvalds return wait->flags & WQ_FLAG_WOKEN ? 0 : -EINTR;
138462906027SNicholas Piggin }
13851da177e4SLinus Torvalds
1386ffa65753SAlistair Popple #ifdef CONFIG_MIGRATION
1387ffa65753SAlistair Popple /**
1388ffa65753SAlistair Popple * migration_entry_wait_on_locked - Wait for a migration entry to be removed
1389ffa65753SAlistair Popple * @entry: migration swap entry.
1390ffa65753SAlistair Popple * @ptl: already locked ptl. This function will drop the lock.
1391ffa65753SAlistair Popple *
1392ffa65753SAlistair Popple * Wait for a migration entry referencing the given page to be removed. This is
1393ffa65753SAlistair Popple * equivalent to put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE) except
1394ffa65753SAlistair Popple * this can be called without taking a reference on the page. Instead this
1395ffa65753SAlistair Popple * should be called while holding the ptl for the migration entry referencing
1396ffa65753SAlistair Popple * the page.
1397ffa65753SAlistair Popple *
13980cb8fd4dSHugh Dickins * Returns after unlocking the ptl.
1399ffa65753SAlistair Popple *
1400ffa65753SAlistair Popple * This follows the same logic as folio_wait_bit_common() so see the comments
1401ffa65753SAlistair Popple * there.
1402ffa65753SAlistair Popple */
migration_entry_wait_on_locked(swp_entry_t entry,spinlock_t * ptl)14030cb8fd4dSHugh Dickins void migration_entry_wait_on_locked(swp_entry_t entry, spinlock_t *ptl)
14040cb8fd4dSHugh Dickins __releases(ptl)
1405ffa65753SAlistair Popple {
1406ffa65753SAlistair Popple struct wait_page_queue wait_page;
1407ffa65753SAlistair Popple wait_queue_entry_t *wait = &wait_page.wait;
1408ffa65753SAlistair Popple bool thrashing = false;
1409ffa65753SAlistair Popple unsigned long pflags;
1410aa1cf99bSYang Yang bool in_thrashing;
1411ffa65753SAlistair Popple wait_queue_head_t *q;
1412ffa65753SAlistair Popple struct folio *folio = page_folio(pfn_swap_entry_to_page(entry));
1413ffa65753SAlistair Popple
1414ffa65753SAlistair Popple q = folio_waitqueue(folio);
1415ffa65753SAlistair Popple if (!folio_test_uptodate(folio) && folio_test_workingset(folio)) {
1416aa1cf99bSYang Yang delayacct_thrashing_start(&in_thrashing);
1417ffa65753SAlistair Popple psi_memstall_enter(&pflags);
1418ffa65753SAlistair Popple thrashing = true;
1419ffa65753SAlistair Popple }
1420ffa65753SAlistair Popple
1421ffa65753SAlistair Popple init_wait(wait);
1422ffa65753SAlistair Popple wait->func = wake_page_function;
1423ffa65753SAlistair Popple wait_page.folio = folio;
1424ffa65753SAlistair Popple wait_page.bit_nr = PG_locked;
1425ffa65753SAlistair Popple wait->flags = 0;
1426ffa65753SAlistair Popple
1427ffa65753SAlistair Popple spin_lock_irq(&q->lock);
1428ffa65753SAlistair Popple folio_set_waiters(folio);
1429ffa65753SAlistair Popple if (!folio_trylock_flag(folio, PG_locked, wait))
1430ffa65753SAlistair Popple __add_wait_queue_entry_tail(q, wait);
1431ffa65753SAlistair Popple spin_unlock_irq(&q->lock);
1432ffa65753SAlistair Popple
1433ffa65753SAlistair Popple /*
1434ffa65753SAlistair Popple * If a migration entry exists for the page the migration path must hold
1435ffa65753SAlistair Popple * a valid reference to the page, and it must take the ptl to remove the
1436ffa65753SAlistair Popple * migration entry. So the page is valid until the ptl is dropped.
1437ffa65753SAlistair Popple */
1438ffa65753SAlistair Popple spin_unlock(ptl);
1439ffa65753SAlistair Popple
1440ffa65753SAlistair Popple for (;;) {
1441ffa65753SAlistair Popple unsigned int flags;
1442ffa65753SAlistair Popple
1443ffa65753SAlistair Popple set_current_state(TASK_UNINTERRUPTIBLE);
1444ffa65753SAlistair Popple
1445ffa65753SAlistair Popple /* Loop until we've been woken or interrupted */
1446ffa65753SAlistair Popple flags = smp_load_acquire(&wait->flags);
1447ffa65753SAlistair Popple if (!(flags & WQ_FLAG_WOKEN)) {
1448ffa65753SAlistair Popple if (signal_pending_state(TASK_UNINTERRUPTIBLE, current))
1449ffa65753SAlistair Popple break;
1450ffa65753SAlistair Popple
1451ffa65753SAlistair Popple io_schedule();
1452ffa65753SAlistair Popple continue;
1453ffa65753SAlistair Popple }
1454ffa65753SAlistair Popple break;
1455ffa65753SAlistair Popple }
1456ffa65753SAlistair Popple
1457ffa65753SAlistair Popple finish_wait(q, wait);
1458ffa65753SAlistair Popple
1459ffa65753SAlistair Popple if (thrashing) {
1460aa1cf99bSYang Yang delayacct_thrashing_end(&in_thrashing);
1461ffa65753SAlistair Popple psi_memstall_leave(&pflags);
1462ffa65753SAlistair Popple }
1463ffa65753SAlistair Popple }
1464ffa65753SAlistair Popple #endif
1465ffa65753SAlistair Popple
folio_wait_bit(struct folio * folio,int bit_nr)1466101c0bf6SMatthew Wilcox (Oracle) void folio_wait_bit(struct folio *folio, int bit_nr)
14671da177e4SLinus Torvalds {
1468101c0bf6SMatthew Wilcox (Oracle) folio_wait_bit_common(folio, bit_nr, TASK_UNINTERRUPTIBLE, SHARED);
14691da177e4SLinus Torvalds }
1470101c0bf6SMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_wait_bit);
14711da177e4SLinus Torvalds
folio_wait_bit_killable(struct folio * folio,int bit_nr)1472101c0bf6SMatthew Wilcox (Oracle) int folio_wait_bit_killable(struct folio *folio, int bit_nr)
1473f62e00ccSKOSAKI Motohiro {
1474101c0bf6SMatthew Wilcox (Oracle) return folio_wait_bit_common(folio, bit_nr, TASK_KILLABLE, SHARED);
1475f62e00ccSKOSAKI Motohiro }
1476101c0bf6SMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_wait_bit_killable);
1477f62e00ccSKOSAKI Motohiro
14781da177e4SLinus Torvalds /**
14799f2b04a2SMatthew Wilcox (Oracle) * folio_put_wait_locked - Drop a reference and wait for it to be unlocked
14809f2b04a2SMatthew Wilcox (Oracle) * @folio: The folio to wait for.
148148054625SMatthew Wilcox (Oracle) * @state: The sleep state (TASK_KILLABLE, TASK_UNINTERRUPTIBLE, etc).
14829a1ea439SHugh Dickins *
14839f2b04a2SMatthew Wilcox (Oracle) * The caller should hold a reference on @folio. They expect the page to
14849a1ea439SHugh Dickins * become unlocked relatively soon, but do not wish to hold up migration
14859f2b04a2SMatthew Wilcox (Oracle) * (for example) by holding the reference while waiting for the folio to
14869a1ea439SHugh Dickins * come unlocked. After this function returns, the caller should not
14879f2b04a2SMatthew Wilcox (Oracle) * dereference @folio.
148848054625SMatthew Wilcox (Oracle) *
14899f2b04a2SMatthew Wilcox (Oracle) * Return: 0 if the folio was unlocked or -EINTR if interrupted by a signal.
14909a1ea439SHugh Dickins */
folio_put_wait_locked(struct folio * folio,int state)1491c195c321SKe Sun static int folio_put_wait_locked(struct folio *folio, int state)
14929a1ea439SHugh Dickins {
14939f2b04a2SMatthew Wilcox (Oracle) return folio_wait_bit_common(folio, PG_locked, state, DROP);
14949a1ea439SHugh Dickins }
14959a1ea439SHugh Dickins
14969a1ea439SHugh Dickins /**
1497df4d4f12SMatthew Wilcox (Oracle) * folio_add_wait_queue - Add an arbitrary waiter to a folio's wait queue
1498df4d4f12SMatthew Wilcox (Oracle) * @folio: Folio defining the wait queue of interest
1499697f619fSRandy Dunlap * @waiter: Waiter to add to the queue
1500385e1ca5SDavid Howells *
1501df4d4f12SMatthew Wilcox (Oracle) * Add an arbitrary @waiter to the wait queue for the nominated @folio.
1502385e1ca5SDavid Howells */
folio_add_wait_queue(struct folio * folio,wait_queue_entry_t * waiter)1503df4d4f12SMatthew Wilcox (Oracle) void folio_add_wait_queue(struct folio *folio, wait_queue_entry_t *waiter)
1504385e1ca5SDavid Howells {
1505df4d4f12SMatthew Wilcox (Oracle) wait_queue_head_t *q = folio_waitqueue(folio);
1506385e1ca5SDavid Howells unsigned long flags;
1507385e1ca5SDavid Howells
1508385e1ca5SDavid Howells spin_lock_irqsave(&q->lock, flags);
15099c3a815fSLinus Torvalds __add_wait_queue_entry_tail(q, waiter);
1510df4d4f12SMatthew Wilcox (Oracle) folio_set_waiters(folio);
1511385e1ca5SDavid Howells spin_unlock_irqrestore(&q->lock, flags);
1512385e1ca5SDavid Howells }
1513df4d4f12SMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(folio_add_wait_queue);
1514385e1ca5SDavid Howells
1515b91e1302SLinus Torvalds #ifndef clear_bit_unlock_is_negative_byte
1516b91e1302SLinus Torvalds
1517b91e1302SLinus Torvalds /*
1518b91e1302SLinus Torvalds * PG_waiters is the high bit in the same byte as PG_lock.
1519b91e1302SLinus Torvalds *
1520b91e1302SLinus Torvalds * On x86 (and on many other architectures), we can clear PG_lock and
1521b91e1302SLinus Torvalds * test the sign bit at the same time. But if the architecture does
1522b91e1302SLinus Torvalds * not support that special operation, we just do this all by hand
1523b91e1302SLinus Torvalds * instead.
1524b91e1302SLinus Torvalds *
1525b91e1302SLinus Torvalds * The read of PG_waiters has to be after (or concurrently with) PG_locked
1526ffceeb62SEthon Paul * being cleared, but a memory barrier should be unnecessary since it is
1527b91e1302SLinus Torvalds * in the same byte as PG_locked.
1528b91e1302SLinus Torvalds */
clear_bit_unlock_is_negative_byte(long nr,volatile void * mem)1529b91e1302SLinus Torvalds static inline bool clear_bit_unlock_is_negative_byte(long nr, volatile void *mem)
1530b91e1302SLinus Torvalds {
1531b91e1302SLinus Torvalds clear_bit_unlock(nr, mem);
1532b91e1302SLinus Torvalds /* smp_mb__after_atomic(); */
153398473f9fSOlof Johansson return test_bit(PG_waiters, mem);
1534b91e1302SLinus Torvalds }
1535b91e1302SLinus Torvalds
1536b91e1302SLinus Torvalds #endif
1537b91e1302SLinus Torvalds
1538385e1ca5SDavid Howells /**
15394e136428SMatthew Wilcox (Oracle) * folio_unlock - Unlock a locked folio.
15404e136428SMatthew Wilcox (Oracle) * @folio: The folio.
15411da177e4SLinus Torvalds *
15424e136428SMatthew Wilcox (Oracle) * Unlocks the folio and wakes up any thread sleeping on the page lock.
15431da177e4SLinus Torvalds *
15444e136428SMatthew Wilcox (Oracle) * Context: May be called from interrupt or process context. May not be
15454e136428SMatthew Wilcox (Oracle) * called from NMI context.
15461da177e4SLinus Torvalds */
folio_unlock(struct folio * folio)15474e136428SMatthew Wilcox (Oracle) void folio_unlock(struct folio *folio)
15481da177e4SLinus Torvalds {
15494e136428SMatthew Wilcox (Oracle) /* Bit 7 allows x86 to check the byte's sign bit */
1550b91e1302SLinus Torvalds BUILD_BUG_ON(PG_waiters != 7);
15514e136428SMatthew Wilcox (Oracle) BUILD_BUG_ON(PG_locked > 7);
15524e136428SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
15534e136428SMatthew Wilcox (Oracle) if (clear_bit_unlock_is_negative_byte(PG_locked, folio_flags(folio, 0)))
15546974d7c9SMatthew Wilcox (Oracle) folio_wake_bit(folio, PG_locked);
15551da177e4SLinus Torvalds }
15564e136428SMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_unlock);
15571da177e4SLinus Torvalds
1558485bb99bSRandy Dunlap /**
1559b47393f8SMatthew Wilcox (Oracle) * folio_end_private_2 - Clear PG_private_2 and wake any waiters.
1560b47393f8SMatthew Wilcox (Oracle) * @folio: The folio.
156173e10dedSDavid Howells *
1562b47393f8SMatthew Wilcox (Oracle) * Clear the PG_private_2 bit on a folio and wake up any sleepers waiting for
1563b47393f8SMatthew Wilcox (Oracle) * it. The folio reference held for PG_private_2 being set is released.
156473e10dedSDavid Howells *
1565b47393f8SMatthew Wilcox (Oracle) * This is, for example, used when a netfs folio is being written to a local
1566b47393f8SMatthew Wilcox (Oracle) * disk cache, thereby allowing writes to the cache for the same folio to be
156773e10dedSDavid Howells * serialised.
156873e10dedSDavid Howells */
folio_end_private_2(struct folio * folio)1569b47393f8SMatthew Wilcox (Oracle) void folio_end_private_2(struct folio *folio)
157073e10dedSDavid Howells {
15716974d7c9SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_test_private_2(folio), folio);
15726974d7c9SMatthew Wilcox (Oracle) clear_bit_unlock(PG_private_2, folio_flags(folio, 0));
15736974d7c9SMatthew Wilcox (Oracle) folio_wake_bit(folio, PG_private_2);
15746974d7c9SMatthew Wilcox (Oracle) folio_put(folio);
157573e10dedSDavid Howells }
1576b47393f8SMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_end_private_2);
157773e10dedSDavid Howells
157873e10dedSDavid Howells /**
1579b47393f8SMatthew Wilcox (Oracle) * folio_wait_private_2 - Wait for PG_private_2 to be cleared on a folio.
1580b47393f8SMatthew Wilcox (Oracle) * @folio: The folio to wait on.
158173e10dedSDavid Howells *
1582b47393f8SMatthew Wilcox (Oracle) * Wait for PG_private_2 (aka PG_fscache) to be cleared on a folio.
158373e10dedSDavid Howells */
folio_wait_private_2(struct folio * folio)1584b47393f8SMatthew Wilcox (Oracle) void folio_wait_private_2(struct folio *folio)
158573e10dedSDavid Howells {
1586101c0bf6SMatthew Wilcox (Oracle) while (folio_test_private_2(folio))
1587101c0bf6SMatthew Wilcox (Oracle) folio_wait_bit(folio, PG_private_2);
158873e10dedSDavid Howells }
1589b47393f8SMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_wait_private_2);
159073e10dedSDavid Howells
159173e10dedSDavid Howells /**
1592b47393f8SMatthew Wilcox (Oracle) * folio_wait_private_2_killable - Wait for PG_private_2 to be cleared on a folio.
1593b47393f8SMatthew Wilcox (Oracle) * @folio: The folio to wait on.
159473e10dedSDavid Howells *
1595b47393f8SMatthew Wilcox (Oracle) * Wait for PG_private_2 (aka PG_fscache) to be cleared on a folio or until a
159673e10dedSDavid Howells * fatal signal is received by the calling task.
159773e10dedSDavid Howells *
159873e10dedSDavid Howells * Return:
159973e10dedSDavid Howells * - 0 if successful.
160073e10dedSDavid Howells * - -EINTR if a fatal signal was encountered.
160173e10dedSDavid Howells */
folio_wait_private_2_killable(struct folio * folio)1602b47393f8SMatthew Wilcox (Oracle) int folio_wait_private_2_killable(struct folio *folio)
160373e10dedSDavid Howells {
160473e10dedSDavid Howells int ret = 0;
160573e10dedSDavid Howells
1606101c0bf6SMatthew Wilcox (Oracle) while (folio_test_private_2(folio)) {
1607101c0bf6SMatthew Wilcox (Oracle) ret = folio_wait_bit_killable(folio, PG_private_2);
160873e10dedSDavid Howells if (ret < 0)
160973e10dedSDavid Howells break;
161073e10dedSDavid Howells }
161173e10dedSDavid Howells
161273e10dedSDavid Howells return ret;
161373e10dedSDavid Howells }
1614b47393f8SMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_wait_private_2_killable);
161573e10dedSDavid Howells
161673e10dedSDavid Howells /**
16174268b480SMatthew Wilcox (Oracle) * folio_end_writeback - End writeback against a folio.
16184268b480SMatthew Wilcox (Oracle) * @folio: The folio.
16191da177e4SLinus Torvalds */
folio_end_writeback(struct folio * folio)16204268b480SMatthew Wilcox (Oracle) void folio_end_writeback(struct folio *folio)
16211da177e4SLinus Torvalds {
1622888cf2dbSMel Gorman /*
16234268b480SMatthew Wilcox (Oracle) * folio_test_clear_reclaim() could be used here but it is an
16244268b480SMatthew Wilcox (Oracle) * atomic operation and overkill in this particular case. Failing
16254268b480SMatthew Wilcox (Oracle) * to shuffle a folio marked for immediate reclaim is too mild
16264268b480SMatthew Wilcox (Oracle) * a gain to justify taking an atomic operation penalty at the
16274268b480SMatthew Wilcox (Oracle) * end of every folio writeback.
1628888cf2dbSMel Gorman */
16294268b480SMatthew Wilcox (Oracle) if (folio_test_reclaim(folio)) {
16304268b480SMatthew Wilcox (Oracle) folio_clear_reclaim(folio);
1631575ced1cSMatthew Wilcox (Oracle) folio_rotate_reclaimable(folio);
1632888cf2dbSMel Gorman }
1633ac6aadb2SMiklos Szeredi
1634073861edSHugh Dickins /*
16354268b480SMatthew Wilcox (Oracle) * Writeback does not hold a folio reference of its own, relying
1636073861edSHugh Dickins * on truncation to wait for the clearing of PG_writeback.
16374268b480SMatthew Wilcox (Oracle) * But here we must make sure that the folio is not freed and
16384268b480SMatthew Wilcox (Oracle) * reused before the folio_wake().
1639073861edSHugh Dickins */
16404268b480SMatthew Wilcox (Oracle) folio_get(folio);
1641269ccca3SMatthew Wilcox (Oracle) if (!__folio_end_writeback(folio))
16421da177e4SLinus Torvalds BUG();
1643ac6aadb2SMiklos Szeredi
16444e857c58SPeter Zijlstra smp_mb__after_atomic();
16454268b480SMatthew Wilcox (Oracle) folio_wake(folio, PG_writeback);
1646512b7931SLinus Torvalds acct_reclaim_writeback(folio);
16474268b480SMatthew Wilcox (Oracle) folio_put(folio);
16481da177e4SLinus Torvalds }
16494268b480SMatthew Wilcox (Oracle) EXPORT_SYMBOL(folio_end_writeback);
16501da177e4SLinus Torvalds
1651485bb99bSRandy Dunlap /**
16527c23c782SMatthew Wilcox (Oracle) * __folio_lock - Get a lock on the folio, assuming we need to sleep to get it.
16537c23c782SMatthew Wilcox (Oracle) * @folio: The folio to lock
16541da177e4SLinus Torvalds */
__folio_lock(struct folio * folio)16557c23c782SMatthew Wilcox (Oracle) void __folio_lock(struct folio *folio)
16561da177e4SLinus Torvalds {
1657101c0bf6SMatthew Wilcox (Oracle) folio_wait_bit_common(folio, PG_locked, TASK_UNINTERRUPTIBLE,
16589a1ea439SHugh Dickins EXCLUSIVE);
16591da177e4SLinus Torvalds }
16607c23c782SMatthew Wilcox (Oracle) EXPORT_SYMBOL(__folio_lock);
16611da177e4SLinus Torvalds
__folio_lock_killable(struct folio * folio)1662af7f29d9SMatthew Wilcox (Oracle) int __folio_lock_killable(struct folio *folio)
16632687a356SMatthew Wilcox {
1664101c0bf6SMatthew Wilcox (Oracle) return folio_wait_bit_common(folio, PG_locked, TASK_KILLABLE,
16659a1ea439SHugh Dickins EXCLUSIVE);
16662687a356SMatthew Wilcox }
1667af7f29d9SMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(__folio_lock_killable);
16682687a356SMatthew Wilcox
__folio_lock_async(struct folio * folio,struct wait_page_queue * wait)1669ffdc8dabSMatthew Wilcox (Oracle) static int __folio_lock_async(struct folio *folio, struct wait_page_queue *wait)
1670dd3e6d50SJens Axboe {
1671df4d4f12SMatthew Wilcox (Oracle) struct wait_queue_head *q = folio_waitqueue(folio);
1672f32b5dd7SMatthew Wilcox (Oracle) int ret = 0;
1673f32b5dd7SMatthew Wilcox (Oracle)
1674df4d4f12SMatthew Wilcox (Oracle) wait->folio = folio;
1675f32b5dd7SMatthew Wilcox (Oracle) wait->bit_nr = PG_locked;
1676f32b5dd7SMatthew Wilcox (Oracle)
1677f32b5dd7SMatthew Wilcox (Oracle) spin_lock_irq(&q->lock);
1678f32b5dd7SMatthew Wilcox (Oracle) __add_wait_queue_entry_tail(q, &wait->wait);
1679ffdc8dabSMatthew Wilcox (Oracle) folio_set_waiters(folio);
1680ffdc8dabSMatthew Wilcox (Oracle) ret = !folio_trylock(folio);
1681f32b5dd7SMatthew Wilcox (Oracle) /*
1682f32b5dd7SMatthew Wilcox (Oracle) * If we were successful now, we know we're still on the
1683f32b5dd7SMatthew Wilcox (Oracle) * waitqueue as we're still under the lock. This means it's
1684f32b5dd7SMatthew Wilcox (Oracle) * safe to remove and return success, we know the callback
1685f32b5dd7SMatthew Wilcox (Oracle) * isn't going to trigger.
1686f32b5dd7SMatthew Wilcox (Oracle) */
1687f32b5dd7SMatthew Wilcox (Oracle) if (!ret)
1688f32b5dd7SMatthew Wilcox (Oracle) __remove_wait_queue(q, &wait->wait);
1689f32b5dd7SMatthew Wilcox (Oracle) else
1690f32b5dd7SMatthew Wilcox (Oracle) ret = -EIOCBQUEUED;
1691f32b5dd7SMatthew Wilcox (Oracle) spin_unlock_irq(&q->lock);
1692f32b5dd7SMatthew Wilcox (Oracle) return ret;
1693dd3e6d50SJens Axboe }
1694dd3e6d50SJens Axboe
16959a95f3cfSPaul Cassella /*
16969a95f3cfSPaul Cassella * Return values:
1697fdc724d6SSuren Baghdasaryan * 0 - folio is locked.
1698fdc724d6SSuren Baghdasaryan * non-zero - folio is not locked.
16991235ccd0SSuren Baghdasaryan * mmap_lock or per-VMA lock has been released (mmap_read_unlock() or
17001235ccd0SSuren Baghdasaryan * vma_end_read()), unless flags had both FAULT_FLAG_ALLOW_RETRY and
17011235ccd0SSuren Baghdasaryan * FAULT_FLAG_RETRY_NOWAIT set, in which case the lock is still held.
17029a95f3cfSPaul Cassella *
1703fdc724d6SSuren Baghdasaryan * If neither ALLOW_RETRY nor KILLABLE are set, will always return 0
17041235ccd0SSuren Baghdasaryan * with the folio locked and the mmap_lock/per-VMA lock is left unperturbed.
17059a95f3cfSPaul Cassella */
__folio_lock_or_retry(struct folio * folio,struct vm_fault * vmf)1706fdc724d6SSuren Baghdasaryan vm_fault_t __folio_lock_or_retry(struct folio *folio, struct vm_fault *vmf)
1707d065bd81SMichel Lespinasse {
1708fdc724d6SSuren Baghdasaryan unsigned int flags = vmf->flags;
1709fdc724d6SSuren Baghdasaryan
17104064b982SPeter Xu if (fault_flag_allow_retry_first(flags)) {
171137b23e05SKOSAKI Motohiro /*
17121235ccd0SSuren Baghdasaryan * CAUTION! In this case, mmap_lock/per-VMA lock is not
17131235ccd0SSuren Baghdasaryan * released even though returning VM_FAULT_RETRY.
171437b23e05SKOSAKI Motohiro */
171537b23e05SKOSAKI Motohiro if (flags & FAULT_FLAG_RETRY_NOWAIT)
1716fdc724d6SSuren Baghdasaryan return VM_FAULT_RETRY;
171737b23e05SKOSAKI Motohiro
17181235ccd0SSuren Baghdasaryan release_fault_lock(vmf);
171937b23e05SKOSAKI Motohiro if (flags & FAULT_FLAG_KILLABLE)
17206baa8d60SMatthew Wilcox (Oracle) folio_wait_locked_killable(folio);
172137b23e05SKOSAKI Motohiro else
17226baa8d60SMatthew Wilcox (Oracle) folio_wait_locked(folio);
1723fdc724d6SSuren Baghdasaryan return VM_FAULT_RETRY;
1724800bca7cSHailong Liu }
172537b23e05SKOSAKI Motohiro if (flags & FAULT_FLAG_KILLABLE) {
17269138e47eSMatthew Wilcox (Oracle) bool ret;
172737b23e05SKOSAKI Motohiro
1728af7f29d9SMatthew Wilcox (Oracle) ret = __folio_lock_killable(folio);
172937b23e05SKOSAKI Motohiro if (ret) {
17301235ccd0SSuren Baghdasaryan release_fault_lock(vmf);
1731fdc724d6SSuren Baghdasaryan return VM_FAULT_RETRY;
173237b23e05SKOSAKI Motohiro }
1733800bca7cSHailong Liu } else {
1734af7f29d9SMatthew Wilcox (Oracle) __folio_lock(folio);
1735d065bd81SMichel Lespinasse }
1736800bca7cSHailong Liu
1737fdc724d6SSuren Baghdasaryan return 0;
1738d065bd81SMichel Lespinasse }
1739d065bd81SMichel Lespinasse
1740485bb99bSRandy Dunlap /**
17410d3f9296SMatthew Wilcox * page_cache_next_miss() - Find the next gap in the page cache.
17420d3f9296SMatthew Wilcox * @mapping: Mapping.
17430d3f9296SMatthew Wilcox * @index: Index.
17440d3f9296SMatthew Wilcox * @max_scan: Maximum range to search.
1745e7b563bbSJohannes Weiner *
17460d3f9296SMatthew Wilcox * Search the range [index, min(index + max_scan - 1, ULONG_MAX)] for the
17470d3f9296SMatthew Wilcox * gap with the lowest index.
1748e7b563bbSJohannes Weiner *
17490d3f9296SMatthew Wilcox * This function may be called under the rcu_read_lock. However, this will
17500d3f9296SMatthew Wilcox * not atomically search a snapshot of the cache at a single point in time.
17510d3f9296SMatthew Wilcox * For example, if a gap is created at index 5, then subsequently a gap is
17520d3f9296SMatthew Wilcox * created at index 10, page_cache_next_miss covering both indices may
17530d3f9296SMatthew Wilcox * return 10 if called under the rcu_read_lock.
1754e7b563bbSJohannes Weiner *
17550d3f9296SMatthew Wilcox * Return: The index of the gap if found, otherwise an index outside the
17560d3f9296SMatthew Wilcox * range specified (in which case 'return - index >= max_scan' will be true).
175716f8eb3eSMike Kravetz * In the rare case of index wrap-around, 0 will be returned.
1758e7b563bbSJohannes Weiner */
page_cache_next_miss(struct address_space * mapping,pgoff_t index,unsigned long max_scan)17590d3f9296SMatthew Wilcox pgoff_t page_cache_next_miss(struct address_space *mapping,
1760e7b563bbSJohannes Weiner pgoff_t index, unsigned long max_scan)
1761e7b563bbSJohannes Weiner {
17620d3f9296SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, index);
1763e7b563bbSJohannes Weiner
17640d3f9296SMatthew Wilcox while (max_scan--) {
17650d3f9296SMatthew Wilcox void *entry = xas_next(&xas);
17660d3f9296SMatthew Wilcox if (!entry || xa_is_value(entry))
176716f8eb3eSMike Kravetz break;
176816f8eb3eSMike Kravetz if (xas.xa_index == 0)
176916f8eb3eSMike Kravetz break;
1770e7b563bbSJohannes Weiner }
1771e7b563bbSJohannes Weiner
177216f8eb3eSMike Kravetz return xas.xa_index;
1773e7b563bbSJohannes Weiner }
17740d3f9296SMatthew Wilcox EXPORT_SYMBOL(page_cache_next_miss);
1775e7b563bbSJohannes Weiner
1776e7b563bbSJohannes Weiner /**
17772346a560SLaurent Dufour * page_cache_prev_miss() - Find the previous gap in the page cache.
17780d3f9296SMatthew Wilcox * @mapping: Mapping.
17790d3f9296SMatthew Wilcox * @index: Index.
17800d3f9296SMatthew Wilcox * @max_scan: Maximum range to search.
1781e7b563bbSJohannes Weiner *
17820d3f9296SMatthew Wilcox * Search the range [max(index - max_scan + 1, 0), index] for the
17830d3f9296SMatthew Wilcox * gap with the highest index.
1784e7b563bbSJohannes Weiner *
17850d3f9296SMatthew Wilcox * This function may be called under the rcu_read_lock. However, this will
17860d3f9296SMatthew Wilcox * not atomically search a snapshot of the cache at a single point in time.
17870d3f9296SMatthew Wilcox * For example, if a gap is created at index 10, then subsequently a gap is
17880d3f9296SMatthew Wilcox * created at index 5, page_cache_prev_miss() covering both indices may
17890d3f9296SMatthew Wilcox * return 5 if called under the rcu_read_lock.
1790e7b563bbSJohannes Weiner *
17910d3f9296SMatthew Wilcox * Return: The index of the gap if found, otherwise an index outside the
17920d3f9296SMatthew Wilcox * range specified (in which case 'index - return >= max_scan' will be true).
179316f8eb3eSMike Kravetz * In the rare case of wrap-around, ULONG_MAX will be returned.
1794e7b563bbSJohannes Weiner */
page_cache_prev_miss(struct address_space * mapping,pgoff_t index,unsigned long max_scan)17950d3f9296SMatthew Wilcox pgoff_t page_cache_prev_miss(struct address_space *mapping,
1796e7b563bbSJohannes Weiner pgoff_t index, unsigned long max_scan)
1797e7b563bbSJohannes Weiner {
17980d3f9296SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, index);
1799e7b563bbSJohannes Weiner
18000d3f9296SMatthew Wilcox while (max_scan--) {
18010d3f9296SMatthew Wilcox void *entry = xas_prev(&xas);
18020d3f9296SMatthew Wilcox if (!entry || xa_is_value(entry))
180316f8eb3eSMike Kravetz break;
180416f8eb3eSMike Kravetz if (xas.xa_index == ULONG_MAX)
180516f8eb3eSMike Kravetz break;
1806e7b563bbSJohannes Weiner }
1807e7b563bbSJohannes Weiner
180816f8eb3eSMike Kravetz return xas.xa_index;
1809e7b563bbSJohannes Weiner }
18100d3f9296SMatthew Wilcox EXPORT_SYMBOL(page_cache_prev_miss);
1811e7b563bbSJohannes Weiner
181244835d20SMatthew Wilcox (Oracle) /*
1813020853b6SMatthew Wilcox (Oracle) * Lockless page cache protocol:
1814020853b6SMatthew Wilcox (Oracle) * On the lookup side:
1815020853b6SMatthew Wilcox (Oracle) * 1. Load the folio from i_pages
1816020853b6SMatthew Wilcox (Oracle) * 2. Increment the refcount if it's not zero
1817020853b6SMatthew Wilcox (Oracle) * 3. If the folio is not found by xas_reload(), put the refcount and retry
1818020853b6SMatthew Wilcox (Oracle) *
1819020853b6SMatthew Wilcox (Oracle) * On the removal side:
1820020853b6SMatthew Wilcox (Oracle) * A. Freeze the page (by zeroing the refcount if nobody else has a reference)
1821020853b6SMatthew Wilcox (Oracle) * B. Remove the page from i_pages
1822020853b6SMatthew Wilcox (Oracle) * C. Return the page to the page allocator
1823020853b6SMatthew Wilcox (Oracle) *
1824020853b6SMatthew Wilcox (Oracle) * This means that any page may have its reference count temporarily
1825020853b6SMatthew Wilcox (Oracle) * increased by a speculative page cache (or fast GUP) lookup as it can
1826020853b6SMatthew Wilcox (Oracle) * be allocated by another user before the RCU grace period expires.
1827020853b6SMatthew Wilcox (Oracle) * Because the refcount temporarily acquired here may end up being the
1828020853b6SMatthew Wilcox (Oracle) * last refcount on the page, any page allocation must be freeable by
1829020853b6SMatthew Wilcox (Oracle) * folio_put().
1830020853b6SMatthew Wilcox (Oracle) */
1831020853b6SMatthew Wilcox (Oracle)
1832020853b6SMatthew Wilcox (Oracle) /*
1833263e721eSChristoph Hellwig * filemap_get_entry - Get a page cache entry.
1834485bb99bSRandy Dunlap * @mapping: the address_space to search
1835a6de4b48SMatthew Wilcox (Oracle) * @index: The page cache index.
1836485bb99bSRandy Dunlap *
1837bca65eeaSMatthew Wilcox (Oracle) * Looks up the page cache entry at @mapping & @index. If it is a folio,
1838bca65eeaSMatthew Wilcox (Oracle) * it is returned with an increased refcount. If it is a shadow entry
1839bca65eeaSMatthew Wilcox (Oracle) * of a previously evicted folio, or a swap entry from shmem/tmpfs,
1840bca65eeaSMatthew Wilcox (Oracle) * it is returned without further action.
18410cd6144aSJohannes Weiner *
1842bca65eeaSMatthew Wilcox (Oracle) * Return: The folio, swap or shadow entry, %NULL if nothing is found.
18431da177e4SLinus Torvalds */
filemap_get_entry(struct address_space * mapping,pgoff_t index)1844263e721eSChristoph Hellwig void *filemap_get_entry(struct address_space *mapping, pgoff_t index)
18451da177e4SLinus Torvalds {
1846a6de4b48SMatthew Wilcox (Oracle) XA_STATE(xas, &mapping->i_pages, index);
1847bca65eeaSMatthew Wilcox (Oracle) struct folio *folio;
18481da177e4SLinus Torvalds
1849a60637c8SNick Piggin rcu_read_lock();
1850a60637c8SNick Piggin repeat:
18514c7472c0SMatthew Wilcox xas_reset(&xas);
1852bca65eeaSMatthew Wilcox (Oracle) folio = xas_load(&xas);
1853bca65eeaSMatthew Wilcox (Oracle) if (xas_retry(&xas, folio))
1854a60637c8SNick Piggin goto repeat;
18558079b1c8SHugh Dickins /*
18564c7472c0SMatthew Wilcox * A shadow entry of a recently evicted page, or a swap entry from
18574c7472c0SMatthew Wilcox * shmem/tmpfs. Return it without attempting to raise page count.
18588079b1c8SHugh Dickins */
1859bca65eeaSMatthew Wilcox (Oracle) if (!folio || xa_is_value(folio))
18608079b1c8SHugh Dickins goto out;
186183929372SKirill A. Shutemov
186216380f52SYang Shi if (!folio_try_get(folio))
1863a60637c8SNick Piggin goto repeat;
1864a60637c8SNick Piggin
1865bca65eeaSMatthew Wilcox (Oracle) if (unlikely(folio != xas_reload(&xas))) {
1866bca65eeaSMatthew Wilcox (Oracle) folio_put(folio);
1867a60637c8SNick Piggin goto repeat;
1868a60637c8SNick Piggin }
186927d20fddSNick Piggin out:
1870a60637c8SNick Piggin rcu_read_unlock();
1871a60637c8SNick Piggin
1872bca65eeaSMatthew Wilcox (Oracle) return folio;
18731da177e4SLinus Torvalds }
18741da177e4SLinus Torvalds
1875485bb99bSRandy Dunlap /**
18763f0c6a07SMatthew Wilcox (Oracle) * __filemap_get_folio - Find and get a reference to a folio.
18772294b32eSMatthew Wilcox (Oracle) * @mapping: The address_space to search.
18782294b32eSMatthew Wilcox (Oracle) * @index: The page index.
18793f0c6a07SMatthew Wilcox (Oracle) * @fgp_flags: %FGP flags modify how the folio is returned.
18803f0c6a07SMatthew Wilcox (Oracle) * @gfp: Memory allocation flags to use if %FGP_CREAT is specified.
18810cd6144aSJohannes Weiner *
18822294b32eSMatthew Wilcox (Oracle) * Looks up the page cache entry at @mapping & @index.
18830cd6144aSJohannes Weiner *
18842294b32eSMatthew Wilcox (Oracle) * If %FGP_LOCK or %FGP_CREAT are specified then the function may sleep even
18852294b32eSMatthew Wilcox (Oracle) * if the %GFP flags specified for %FGP_CREAT are atomic.
18862457aec6SMel Gorman *
1887ffc143dbSMatthew Wilcox (Oracle) * If this function returns a folio, it is returned with an increased refcount.
1888a862f68aSMike Rapoport *
188966dabbb6SChristoph Hellwig * Return: The found folio or an ERR_PTR() otherwise.
18900cd6144aSJohannes Weiner */
__filemap_get_folio(struct address_space * mapping,pgoff_t index,fgf_t fgp_flags,gfp_t gfp)18913f0c6a07SMatthew Wilcox (Oracle) struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
1892ffc143dbSMatthew Wilcox (Oracle) fgf_t fgp_flags, gfp_t gfp)
18931da177e4SLinus Torvalds {
18943f0c6a07SMatthew Wilcox (Oracle) struct folio *folio;
18952457aec6SMel Gorman
18961da177e4SLinus Torvalds repeat:
1897263e721eSChristoph Hellwig folio = filemap_get_entry(mapping, index);
189848c9d113SChristoph Hellwig if (xa_is_value(folio))
18993f0c6a07SMatthew Wilcox (Oracle) folio = NULL;
19003f0c6a07SMatthew Wilcox (Oracle) if (!folio)
19012457aec6SMel Gorman goto no_page;
19022457aec6SMel Gorman
19032457aec6SMel Gorman if (fgp_flags & FGP_LOCK) {
19042457aec6SMel Gorman if (fgp_flags & FGP_NOWAIT) {
19053f0c6a07SMatthew Wilcox (Oracle) if (!folio_trylock(folio)) {
19063f0c6a07SMatthew Wilcox (Oracle) folio_put(folio);
190766dabbb6SChristoph Hellwig return ERR_PTR(-EAGAIN);
19082457aec6SMel Gorman }
19092457aec6SMel Gorman } else {
19103f0c6a07SMatthew Wilcox (Oracle) folio_lock(folio);
19112457aec6SMel Gorman }
19122457aec6SMel Gorman
19132457aec6SMel Gorman /* Has the page been truncated? */
19143f0c6a07SMatthew Wilcox (Oracle) if (unlikely(folio->mapping != mapping)) {
19153f0c6a07SMatthew Wilcox (Oracle) folio_unlock(folio);
19163f0c6a07SMatthew Wilcox (Oracle) folio_put(folio);
19172457aec6SMel Gorman goto repeat;
19182457aec6SMel Gorman }
19193f0c6a07SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_contains(folio, index), folio);
19202457aec6SMel Gorman }
19212457aec6SMel Gorman
1922c16eb000SKirill Tkhai if (fgp_flags & FGP_ACCESSED)
19233f0c6a07SMatthew Wilcox (Oracle) folio_mark_accessed(folio);
1924b9306a79SYang Shi else if (fgp_flags & FGP_WRITE) {
1925b9306a79SYang Shi /* Clear idle flag for buffer write */
19263f0c6a07SMatthew Wilcox (Oracle) if (folio_test_idle(folio))
19273f0c6a07SMatthew Wilcox (Oracle) folio_clear_idle(folio);
1928b9306a79SYang Shi }
19292457aec6SMel Gorman
1930b27652d9SMatthew Wilcox (Oracle) if (fgp_flags & FGP_STABLE)
1931b27652d9SMatthew Wilcox (Oracle) folio_wait_stable(folio);
19322457aec6SMel Gorman no_page:
19333f0c6a07SMatthew Wilcox (Oracle) if (!folio && (fgp_flags & FGP_CREAT)) {
19344f661701SMatthew Wilcox (Oracle) unsigned order = FGF_GET_ORDER(fgp_flags);
19352457aec6SMel Gorman int err;
19364f661701SMatthew Wilcox (Oracle)
1937f56753acSChristoph Hellwig if ((fgp_flags & FGP_WRITE) && mapping_can_writeback(mapping))
19383f0c6a07SMatthew Wilcox (Oracle) gfp |= __GFP_WRITE;
193945f87de5SMichal Hocko if (fgp_flags & FGP_NOFS)
19403f0c6a07SMatthew Wilcox (Oracle) gfp &= ~__GFP_FS;
19410dd316baSJens Axboe if (fgp_flags & FGP_NOWAIT) {
19420dd316baSJens Axboe gfp &= ~GFP_KERNEL;
19430dd316baSJens Axboe gfp |= GFP_NOWAIT | __GFP_NOWARN;
19440dd316baSJens Axboe }
1945a75d4c33SJosef Bacik if (WARN_ON_ONCE(!(fgp_flags & (FGP_LOCK | FGP_FOR_MMAP))))
19462457aec6SMel Gorman fgp_flags |= FGP_LOCK;
19472457aec6SMel Gorman
19484f661701SMatthew Wilcox (Oracle) if (!mapping_large_folio_support(mapping))
19494f661701SMatthew Wilcox (Oracle) order = 0;
19504f661701SMatthew Wilcox (Oracle) if (order > MAX_PAGECACHE_ORDER)
19514f661701SMatthew Wilcox (Oracle) order = MAX_PAGECACHE_ORDER;
19524f661701SMatthew Wilcox (Oracle) /* If we're not aligned, allocate a smaller folio */
19534f661701SMatthew Wilcox (Oracle) if (index & ((1UL << order) - 1))
19544f661701SMatthew Wilcox (Oracle) order = __ffs(index);
19554f661701SMatthew Wilcox (Oracle)
19564f661701SMatthew Wilcox (Oracle) do {
19574f661701SMatthew Wilcox (Oracle) gfp_t alloc_gfp = gfp;
19584f661701SMatthew Wilcox (Oracle)
19594f661701SMatthew Wilcox (Oracle) err = -ENOMEM;
19604f661701SMatthew Wilcox (Oracle) if (order > 0)
19614f661701SMatthew Wilcox (Oracle) alloc_gfp |= __GFP_NORETRY | __GFP_NOWARN;
19624f661701SMatthew Wilcox (Oracle) folio = filemap_alloc_folio(alloc_gfp, order);
19634f661701SMatthew Wilcox (Oracle) if (!folio)
19644f661701SMatthew Wilcox (Oracle) continue;
19654f661701SMatthew Wilcox (Oracle)
1966eb39d618SHugh Dickins /* Init accessed so avoid atomic mark_page_accessed later */
19672457aec6SMel Gorman if (fgp_flags & FGP_ACCESSED)
19683f0c6a07SMatthew Wilcox (Oracle) __folio_set_referenced(folio);
19692457aec6SMel Gorman
19703f0c6a07SMatthew Wilcox (Oracle) err = filemap_add_folio(mapping, folio, index, gfp);
19714f661701SMatthew Wilcox (Oracle) if (!err)
19724f661701SMatthew Wilcox (Oracle) break;
19733f0c6a07SMatthew Wilcox (Oracle) folio_put(folio);
19743f0c6a07SMatthew Wilcox (Oracle) folio = NULL;
19754f661701SMatthew Wilcox (Oracle) } while (order-- > 0);
19764f661701SMatthew Wilcox (Oracle)
1977eb2be189SNick Piggin if (err == -EEXIST)
19781da177e4SLinus Torvalds goto repeat;
19794f661701SMatthew Wilcox (Oracle) if (err)
19804f661701SMatthew Wilcox (Oracle) return ERR_PTR(err);
1981a75d4c33SJosef Bacik /*
19823f0c6a07SMatthew Wilcox (Oracle) * filemap_add_folio locks the page, and for mmap
19833f0c6a07SMatthew Wilcox (Oracle) * we expect an unlocked page.
1984a75d4c33SJosef Bacik */
19853f0c6a07SMatthew Wilcox (Oracle) if (folio && (fgp_flags & FGP_FOR_MMAP))
19863f0c6a07SMatthew Wilcox (Oracle) folio_unlock(folio);
1987eb2be189SNick Piggin }
19882457aec6SMel Gorman
198966dabbb6SChristoph Hellwig if (!folio)
199066dabbb6SChristoph Hellwig return ERR_PTR(-ENOENT);
19913f0c6a07SMatthew Wilcox (Oracle) return folio;
19921da177e4SLinus Torvalds }
19933f0c6a07SMatthew Wilcox (Oracle) EXPORT_SYMBOL(__filemap_get_folio);
19941da177e4SLinus Torvalds
find_get_entry(struct xa_state * xas,pgoff_t max,xa_mark_t mark)1995f5e6429aSMatthew Wilcox (Oracle) static inline struct folio *find_get_entry(struct xa_state *xas, pgoff_t max,
1996c7bad633SMatthew Wilcox (Oracle) xa_mark_t mark)
1997c7bad633SMatthew Wilcox (Oracle) {
1998f5e6429aSMatthew Wilcox (Oracle) struct folio *folio;
1999c7bad633SMatthew Wilcox (Oracle)
2000c7bad633SMatthew Wilcox (Oracle) retry:
2001c7bad633SMatthew Wilcox (Oracle) if (mark == XA_PRESENT)
2002f5e6429aSMatthew Wilcox (Oracle) folio = xas_find(xas, max);
2003c7bad633SMatthew Wilcox (Oracle) else
2004f5e6429aSMatthew Wilcox (Oracle) folio = xas_find_marked(xas, max, mark);
2005c7bad633SMatthew Wilcox (Oracle)
2006f5e6429aSMatthew Wilcox (Oracle) if (xas_retry(xas, folio))
2007c7bad633SMatthew Wilcox (Oracle) goto retry;
2008c7bad633SMatthew Wilcox (Oracle) /*
2009c7bad633SMatthew Wilcox (Oracle) * A shadow entry of a recently evicted page, a swap
2010c7bad633SMatthew Wilcox (Oracle) * entry from shmem/tmpfs or a DAX entry. Return it
2011c7bad633SMatthew Wilcox (Oracle) * without attempting to raise page count.
2012c7bad633SMatthew Wilcox (Oracle) */
2013f5e6429aSMatthew Wilcox (Oracle) if (!folio || xa_is_value(folio))
2014f5e6429aSMatthew Wilcox (Oracle) return folio;
2015c7bad633SMatthew Wilcox (Oracle)
201616380f52SYang Shi if (!folio_try_get(folio))
2017c7bad633SMatthew Wilcox (Oracle) goto reset;
2018c7bad633SMatthew Wilcox (Oracle)
2019f5e6429aSMatthew Wilcox (Oracle) if (unlikely(folio != xas_reload(xas))) {
2020f5e6429aSMatthew Wilcox (Oracle) folio_put(folio);
2021c7bad633SMatthew Wilcox (Oracle) goto reset;
2022c7bad633SMatthew Wilcox (Oracle) }
2023c7bad633SMatthew Wilcox (Oracle)
2024f5e6429aSMatthew Wilcox (Oracle) return folio;
2025c7bad633SMatthew Wilcox (Oracle) reset:
2026c7bad633SMatthew Wilcox (Oracle) xas_reset(xas);
2027c7bad633SMatthew Wilcox (Oracle) goto retry;
2028c7bad633SMatthew Wilcox (Oracle) }
2029c7bad633SMatthew Wilcox (Oracle)
20301da177e4SLinus Torvalds /**
20310cd6144aSJohannes Weiner * find_get_entries - gang pagecache lookup
20320cd6144aSJohannes Weiner * @mapping: The address_space to search
20330cd6144aSJohannes Weiner * @start: The starting page cache index
2034ca122fe4SMatthew Wilcox (Oracle) * @end: The final page index (inclusive).
20350e499ed3SMatthew Wilcox (Oracle) * @fbatch: Where the resulting entries are placed.
20360cd6144aSJohannes Weiner * @indices: The cache indices corresponding to the entries in @entries
20370cd6144aSJohannes Weiner *
2038cf2039afSMatthew Wilcox (Oracle) * find_get_entries() will search for and return a batch of entries in
20390e499ed3SMatthew Wilcox (Oracle) * the mapping. The entries are placed in @fbatch. find_get_entries()
20400e499ed3SMatthew Wilcox (Oracle) * takes a reference on any actual folios it returns.
20410cd6144aSJohannes Weiner *
20420e499ed3SMatthew Wilcox (Oracle) * The entries have ascending indexes. The indices may not be consecutive
20430e499ed3SMatthew Wilcox (Oracle) * due to not-present entries or large folios.
20440cd6144aSJohannes Weiner *
20450e499ed3SMatthew Wilcox (Oracle) * Any shadow entries of evicted folios, or swap entries from
2046139b6a6fSJohannes Weiner * shmem/tmpfs, are included in the returned array.
20470cd6144aSJohannes Weiner *
20480e499ed3SMatthew Wilcox (Oracle) * Return: The number of entries which were found.
20490cd6144aSJohannes Weiner */
find_get_entries(struct address_space * mapping,pgoff_t * start,pgoff_t end,struct folio_batch * fbatch,pgoff_t * indices)20509fb6beeaSVishal Moola (Oracle) unsigned find_get_entries(struct address_space *mapping, pgoff_t *start,
20510e499ed3SMatthew Wilcox (Oracle) pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices)
20520cd6144aSJohannes Weiner {
20539fb6beeaSVishal Moola (Oracle) XA_STATE(xas, &mapping->i_pages, *start);
2054f5e6429aSMatthew Wilcox (Oracle) struct folio *folio;
20550cd6144aSJohannes Weiner
20560cd6144aSJohannes Weiner rcu_read_lock();
2057f5e6429aSMatthew Wilcox (Oracle) while ((folio = find_get_entry(&xas, end, XA_PRESENT)) != NULL) {
20580e499ed3SMatthew Wilcox (Oracle) indices[fbatch->nr] = xas.xa_index;
20590e499ed3SMatthew Wilcox (Oracle) if (!folio_batch_add(fbatch, folio))
20600cd6144aSJohannes Weiner break;
20610cd6144aSJohannes Weiner }
20620cd6144aSJohannes Weiner rcu_read_unlock();
2063cf2039afSMatthew Wilcox (Oracle)
20649fb6beeaSVishal Moola (Oracle) if (folio_batch_count(fbatch)) {
20659fb6beeaSVishal Moola (Oracle) unsigned long nr = 1;
20669fb6beeaSVishal Moola (Oracle) int idx = folio_batch_count(fbatch) - 1;
20679fb6beeaSVishal Moola (Oracle)
20689fb6beeaSVishal Moola (Oracle) folio = fbatch->folios[idx];
20699fb6beeaSVishal Moola (Oracle) if (!xa_is_value(folio) && !folio_test_hugetlb(folio))
20709fb6beeaSVishal Moola (Oracle) nr = folio_nr_pages(folio);
20719fb6beeaSVishal Moola (Oracle) *start = indices[idx] + nr;
20729fb6beeaSVishal Moola (Oracle) }
20730e499ed3SMatthew Wilcox (Oracle) return folio_batch_count(fbatch);
20740cd6144aSJohannes Weiner }
20750cd6144aSJohannes Weiner
20760cd6144aSJohannes Weiner /**
20775c211ba2SMatthew Wilcox (Oracle) * find_lock_entries - Find a batch of pagecache entries.
20785c211ba2SMatthew Wilcox (Oracle) * @mapping: The address_space to search.
20795c211ba2SMatthew Wilcox (Oracle) * @start: The starting page cache index.
20805c211ba2SMatthew Wilcox (Oracle) * @end: The final page index (inclusive).
208151dcbdacSMatthew Wilcox (Oracle) * @fbatch: Where the resulting entries are placed.
208251dcbdacSMatthew Wilcox (Oracle) * @indices: The cache indices of the entries in @fbatch.
20835c211ba2SMatthew Wilcox (Oracle) *
20845c211ba2SMatthew Wilcox (Oracle) * find_lock_entries() will return a batch of entries from @mapping.
2085f5e6429aSMatthew Wilcox (Oracle) * Swap, shadow and DAX entries are included. Folios are returned
2086f5e6429aSMatthew Wilcox (Oracle) * locked and with an incremented refcount. Folios which are locked
2087f5e6429aSMatthew Wilcox (Oracle) * by somebody else or under writeback are skipped. Folios which are
2088f5e6429aSMatthew Wilcox (Oracle) * partially outside the range are not returned.
20895c211ba2SMatthew Wilcox (Oracle) *
20905c211ba2SMatthew Wilcox (Oracle) * The entries have ascending indexes. The indices may not be consecutive
2091f5e6429aSMatthew Wilcox (Oracle) * due to not-present entries, large folios, folios which could not be
2092f5e6429aSMatthew Wilcox (Oracle) * locked or folios under writeback.
20935c211ba2SMatthew Wilcox (Oracle) *
20945c211ba2SMatthew Wilcox (Oracle) * Return: The number of entries which were found.
20955c211ba2SMatthew Wilcox (Oracle) */
find_lock_entries(struct address_space * mapping,pgoff_t * start,pgoff_t end,struct folio_batch * fbatch,pgoff_t * indices)20963392ca12SVishal Moola (Oracle) unsigned find_lock_entries(struct address_space *mapping, pgoff_t *start,
209751dcbdacSMatthew Wilcox (Oracle) pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices)
20985c211ba2SMatthew Wilcox (Oracle) {
20993392ca12SVishal Moola (Oracle) XA_STATE(xas, &mapping->i_pages, *start);
2100f5e6429aSMatthew Wilcox (Oracle) struct folio *folio;
21015c211ba2SMatthew Wilcox (Oracle)
21025c211ba2SMatthew Wilcox (Oracle) rcu_read_lock();
2103f5e6429aSMatthew Wilcox (Oracle) while ((folio = find_get_entry(&xas, end, XA_PRESENT))) {
2104f5e6429aSMatthew Wilcox (Oracle) if (!xa_is_value(folio)) {
21053392ca12SVishal Moola (Oracle) if (folio->index < *start)
21065c211ba2SMatthew Wilcox (Oracle) goto put;
210787b11f86SSidhartha Kumar if (folio_next_index(folio) - 1 > end)
21085c211ba2SMatthew Wilcox (Oracle) goto put;
2109f5e6429aSMatthew Wilcox (Oracle) if (!folio_trylock(folio))
21105c211ba2SMatthew Wilcox (Oracle) goto put;
2111f5e6429aSMatthew Wilcox (Oracle) if (folio->mapping != mapping ||
2112f5e6429aSMatthew Wilcox (Oracle) folio_test_writeback(folio))
21135c211ba2SMatthew Wilcox (Oracle) goto unlock;
2114f5e6429aSMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_contains(folio, xas.xa_index),
2115f5e6429aSMatthew Wilcox (Oracle) folio);
21165c211ba2SMatthew Wilcox (Oracle) }
211751dcbdacSMatthew Wilcox (Oracle) indices[fbatch->nr] = xas.xa_index;
211851dcbdacSMatthew Wilcox (Oracle) if (!folio_batch_add(fbatch, folio))
21195c211ba2SMatthew Wilcox (Oracle) break;
21206b24ca4aSMatthew Wilcox (Oracle) continue;
21215c211ba2SMatthew Wilcox (Oracle) unlock:
2122f5e6429aSMatthew Wilcox (Oracle) folio_unlock(folio);
21235c211ba2SMatthew Wilcox (Oracle) put:
2124f5e6429aSMatthew Wilcox (Oracle) folio_put(folio);
21255c211ba2SMatthew Wilcox (Oracle) }
21265c211ba2SMatthew Wilcox (Oracle) rcu_read_unlock();
21275c211ba2SMatthew Wilcox (Oracle)
21283392ca12SVishal Moola (Oracle) if (folio_batch_count(fbatch)) {
21293392ca12SVishal Moola (Oracle) unsigned long nr = 1;
21303392ca12SVishal Moola (Oracle) int idx = folio_batch_count(fbatch) - 1;
21313392ca12SVishal Moola (Oracle)
21323392ca12SVishal Moola (Oracle) folio = fbatch->folios[idx];
21333392ca12SVishal Moola (Oracle) if (!xa_is_value(folio) && !folio_test_hugetlb(folio))
21343392ca12SVishal Moola (Oracle) nr = folio_nr_pages(folio);
21353392ca12SVishal Moola (Oracle) *start = indices[idx] + nr;
21363392ca12SVishal Moola (Oracle) }
213751dcbdacSMatthew Wilcox (Oracle) return folio_batch_count(fbatch);
21385c211ba2SMatthew Wilcox (Oracle) }
21395c211ba2SMatthew Wilcox (Oracle)
21405c211ba2SMatthew Wilcox (Oracle) /**
2141be0ced5eSMatthew Wilcox (Oracle) * filemap_get_folios - Get a batch of folios
21421da177e4SLinus Torvalds * @mapping: The address_space to search
21431da177e4SLinus Torvalds * @start: The starting page index
2144b947cee4SJan Kara * @end: The final page index (inclusive)
2145be0ced5eSMatthew Wilcox (Oracle) * @fbatch: The batch to fill.
21461da177e4SLinus Torvalds *
2147be0ced5eSMatthew Wilcox (Oracle) * Search for and return a batch of folios in the mapping starting at
2148be0ced5eSMatthew Wilcox (Oracle) * index @start and up to index @end (inclusive). The folios are returned
2149be0ced5eSMatthew Wilcox (Oracle) * in @fbatch with an elevated reference count.
21501da177e4SLinus Torvalds *
2151be0ced5eSMatthew Wilcox (Oracle) * The first folio may start before @start; if it does, it will contain
2152be0ced5eSMatthew Wilcox (Oracle) * @start. The final folio may extend beyond @end; if it does, it will
2153be0ced5eSMatthew Wilcox (Oracle) * contain @end. The folios have ascending indices. There may be gaps
2154be0ced5eSMatthew Wilcox (Oracle) * between the folios if there are indices which have no folio in the
2155be0ced5eSMatthew Wilcox (Oracle) * page cache. If folios are added to or removed from the page cache
2156be0ced5eSMatthew Wilcox (Oracle) * while this is running, they may or may not be found by this call.
21571da177e4SLinus Torvalds *
2158be0ced5eSMatthew Wilcox (Oracle) * Return: The number of folios which were found.
2159be0ced5eSMatthew Wilcox (Oracle) * We also update @start to index the next folio for the traversal.
21601da177e4SLinus Torvalds */
filemap_get_folios(struct address_space * mapping,pgoff_t * start,pgoff_t end,struct folio_batch * fbatch)2161be0ced5eSMatthew Wilcox (Oracle) unsigned filemap_get_folios(struct address_space *mapping, pgoff_t *start,
2162be0ced5eSMatthew Wilcox (Oracle) pgoff_t end, struct folio_batch *fbatch)
21631da177e4SLinus Torvalds {
2164fd1b3ceeSMatthew Wilcox XA_STATE(xas, &mapping->i_pages, *start);
2165f5e6429aSMatthew Wilcox (Oracle) struct folio *folio;
21661da177e4SLinus Torvalds
2167a60637c8SNick Piggin rcu_read_lock();
2168be0ced5eSMatthew Wilcox (Oracle) while ((folio = find_get_entry(&xas, end, XA_PRESENT)) != NULL) {
2169fd1b3ceeSMatthew Wilcox /* Skip over shadow, swap and DAX entries */
2170f5e6429aSMatthew Wilcox (Oracle) if (xa_is_value(folio))
21712cf938aaSMatthew Wilcox continue;
2172be0ced5eSMatthew Wilcox (Oracle) if (!folio_batch_add(fbatch, folio)) {
2173be0ced5eSMatthew Wilcox (Oracle) unsigned long nr = folio_nr_pages(folio);
2174a60637c8SNick Piggin
2175be0ced5eSMatthew Wilcox (Oracle) if (folio_test_hugetlb(folio))
2176be0ced5eSMatthew Wilcox (Oracle) nr = 1;
2177be0ced5eSMatthew Wilcox (Oracle) *start = folio->index + nr;
2178b947cee4SJan Kara goto out;
2179b947cee4SJan Kara }
2180a60637c8SNick Piggin }
21815b280c0cSHugh Dickins
2182b947cee4SJan Kara /*
2183b947cee4SJan Kara * We come here when there is no page beyond @end. We take care to not
2184b947cee4SJan Kara * overflow the index @start as it confuses some of the callers. This
2185fd1b3ceeSMatthew Wilcox * breaks the iteration when there is a page at index -1 but that is
2186b947cee4SJan Kara * already broken anyway.
2187b947cee4SJan Kara */
2188b947cee4SJan Kara if (end == (pgoff_t)-1)
2189b947cee4SJan Kara *start = (pgoff_t)-1;
2190b947cee4SJan Kara else
2191b947cee4SJan Kara *start = end + 1;
2192b947cee4SJan Kara out:
2193a60637c8SNick Piggin rcu_read_unlock();
2194d72dc8a2SJan Kara
2195be0ced5eSMatthew Wilcox (Oracle) return folio_batch_count(fbatch);
2196be0ced5eSMatthew Wilcox (Oracle) }
2197be0ced5eSMatthew Wilcox (Oracle) EXPORT_SYMBOL(filemap_get_folios);
2198be0ced5eSMatthew Wilcox (Oracle)
2199ebf43500SJens Axboe /**
220035b47146SVishal Moola (Oracle) * filemap_get_folios_contig - Get a batch of contiguous folios
2201ebf43500SJens Axboe * @mapping: The address_space to search
220235b47146SVishal Moola (Oracle) * @start: The starting page index
220335b47146SVishal Moola (Oracle) * @end: The final page index (inclusive)
220435b47146SVishal Moola (Oracle) * @fbatch: The batch to fill
2205ebf43500SJens Axboe *
220635b47146SVishal Moola (Oracle) * filemap_get_folios_contig() works exactly like filemap_get_folios(),
220735b47146SVishal Moola (Oracle) * except the returned folios are guaranteed to be contiguous. This may
220835b47146SVishal Moola (Oracle) * not return all contiguous folios if the batch gets filled up.
2209ebf43500SJens Axboe *
221035b47146SVishal Moola (Oracle) * Return: The number of folios found.
221135b47146SVishal Moola (Oracle) * Also update @start to be positioned for traversal of the next folio.
2212ebf43500SJens Axboe */
22130fc9d104SKonstantin Khlebnikov
filemap_get_folios_contig(struct address_space * mapping,pgoff_t * start,pgoff_t end,struct folio_batch * fbatch)221435b47146SVishal Moola (Oracle) unsigned filemap_get_folios_contig(struct address_space *mapping,
221535b47146SVishal Moola (Oracle) pgoff_t *start, pgoff_t end, struct folio_batch *fbatch)
221635b47146SVishal Moola (Oracle) {
221735b47146SVishal Moola (Oracle) XA_STATE(xas, &mapping->i_pages, *start);
221835b47146SVishal Moola (Oracle) unsigned long nr;
221935b47146SVishal Moola (Oracle) struct folio *folio;
2220ebf43500SJens Axboe
2221a60637c8SNick Piggin rcu_read_lock();
222235b47146SVishal Moola (Oracle)
222335b47146SVishal Moola (Oracle) for (folio = xas_load(&xas); folio && xas.xa_index <= end;
222435b47146SVishal Moola (Oracle) folio = xas_next(&xas)) {
2225e1c37722SMatthew Wilcox (Oracle) if (xas_retry(&xas, folio))
22262cf938aaSMatthew Wilcox continue;
22278079b1c8SHugh Dickins /*
22283ece58a2SMatthew Wilcox * If the entry has been swapped out, we can stop looking.
22293ece58a2SMatthew Wilcox * No current caller is looking for DAX entries.
22308079b1c8SHugh Dickins */
2231e1c37722SMatthew Wilcox (Oracle) if (xa_is_value(folio))
223235b47146SVishal Moola (Oracle) goto update_start;
2233a60637c8SNick Piggin
223416380f52SYang Shi if (!folio_try_get(folio))
22353ece58a2SMatthew Wilcox goto retry;
2236a60637c8SNick Piggin
2237e1c37722SMatthew Wilcox (Oracle) if (unlikely(folio != xas_reload(&xas)))
223835b47146SVishal Moola (Oracle) goto put_folio;
2239a60637c8SNick Piggin
224035b47146SVishal Moola (Oracle) if (!folio_batch_add(fbatch, folio)) {
224135b47146SVishal Moola (Oracle) nr = folio_nr_pages(folio);
224235b47146SVishal Moola (Oracle)
224335b47146SVishal Moola (Oracle) if (folio_test_hugetlb(folio))
224435b47146SVishal Moola (Oracle) nr = 1;
224535b47146SVishal Moola (Oracle) *start = folio->index + nr;
224635b47146SVishal Moola (Oracle) goto out;
22476b24ca4aSMatthew Wilcox (Oracle) }
22483ece58a2SMatthew Wilcox continue;
224935b47146SVishal Moola (Oracle) put_folio:
2250e1c37722SMatthew Wilcox (Oracle) folio_put(folio);
225135b47146SVishal Moola (Oracle)
22523ece58a2SMatthew Wilcox retry:
22533ece58a2SMatthew Wilcox xas_reset(&xas);
2254ebf43500SJens Axboe }
225535b47146SVishal Moola (Oracle)
225635b47146SVishal Moola (Oracle) update_start:
225735b47146SVishal Moola (Oracle) nr = folio_batch_count(fbatch);
225835b47146SVishal Moola (Oracle)
225935b47146SVishal Moola (Oracle) if (nr) {
226035b47146SVishal Moola (Oracle) folio = fbatch->folios[nr - 1];
226135b47146SVishal Moola (Oracle) if (folio_test_hugetlb(folio))
226235b47146SVishal Moola (Oracle) *start = folio->index + 1;
226335b47146SVishal Moola (Oracle) else
226487b11f86SSidhartha Kumar *start = folio_next_index(folio);
2265ebf43500SJens Axboe }
226635b47146SVishal Moola (Oracle) out:
226735b47146SVishal Moola (Oracle) rcu_read_unlock();
226835b47146SVishal Moola (Oracle) return folio_batch_count(fbatch);
226935b47146SVishal Moola (Oracle) }
227035b47146SVishal Moola (Oracle) EXPORT_SYMBOL(filemap_get_folios_contig);
2271ebf43500SJens Axboe
2272485bb99bSRandy Dunlap /**
2273247f9e1fSVishal Moola (Oracle) * filemap_get_folios_tag - Get a batch of folios matching @tag
2274247f9e1fSVishal Moola (Oracle) * @mapping: The address_space to search
2275247f9e1fSVishal Moola (Oracle) * @start: The starting page index
227672b045aeSJan Kara * @end: The final page index (inclusive)
2277247f9e1fSVishal Moola (Oracle) * @tag: The tag index
2278247f9e1fSVishal Moola (Oracle) * @fbatch: The batch to fill
2279485bb99bSRandy Dunlap *
2280247f9e1fSVishal Moola (Oracle) * Same as filemap_get_folios(), but only returning folios tagged with @tag.
2281a862f68aSMike Rapoport *
2282247f9e1fSVishal Moola (Oracle) * Return: The number of folios found.
2283247f9e1fSVishal Moola (Oracle) * Also update @start to index the next folio for traversal.
22841da177e4SLinus Torvalds */
filemap_get_folios_tag(struct address_space * mapping,pgoff_t * start,pgoff_t end,xa_mark_t tag,struct folio_batch * fbatch)2285247f9e1fSVishal Moola (Oracle) unsigned filemap_get_folios_tag(struct address_space *mapping, pgoff_t *start,
2286247f9e1fSVishal Moola (Oracle) pgoff_t end, xa_mark_t tag, struct folio_batch *fbatch)
22871da177e4SLinus Torvalds {
2288247f9e1fSVishal Moola (Oracle) XA_STATE(xas, &mapping->i_pages, *start);
2289f5e6429aSMatthew Wilcox (Oracle) struct folio *folio;
22901da177e4SLinus Torvalds
2291a60637c8SNick Piggin rcu_read_lock();
2292247f9e1fSVishal Moola (Oracle) while ((folio = find_get_entry(&xas, end, tag)) != NULL) {
22938079b1c8SHugh Dickins /*
2294a6906972SMatthew Wilcox * Shadow entries should never be tagged, but this iteration
2295a6906972SMatthew Wilcox * is lockless so there is a window for page reclaim to evict
2296a6906972SMatthew Wilcox * a page we saw tagged. Skip over it.
22978079b1c8SHugh Dickins */
2298f5e6429aSMatthew Wilcox (Oracle) if (xa_is_value(folio))
2299139b6a6fSJohannes Weiner continue;
2300247f9e1fSVishal Moola (Oracle) if (!folio_batch_add(fbatch, folio)) {
2301247f9e1fSVishal Moola (Oracle) unsigned long nr = folio_nr_pages(folio);
2302a60637c8SNick Piggin
2303247f9e1fSVishal Moola (Oracle) if (folio_test_hugetlb(folio))
2304247f9e1fSVishal Moola (Oracle) nr = 1;
2305247f9e1fSVishal Moola (Oracle) *start = folio->index + nr;
230672b045aeSJan Kara goto out;
230772b045aeSJan Kara }
2308a60637c8SNick Piggin }
230972b045aeSJan Kara /*
2310247f9e1fSVishal Moola (Oracle) * We come here when there is no page beyond @end. We take care to not
2311247f9e1fSVishal Moola (Oracle) * overflow the index @start as it confuses some of the callers. This
2312247f9e1fSVishal Moola (Oracle) * breaks the iteration when there is a page at index -1 but that is
2313247f9e1fSVishal Moola (Oracle) * already broke anyway.
231472b045aeSJan Kara */
231572b045aeSJan Kara if (end == (pgoff_t)-1)
2316247f9e1fSVishal Moola (Oracle) *start = (pgoff_t)-1;
231772b045aeSJan Kara else
2318247f9e1fSVishal Moola (Oracle) *start = end + 1;
231972b045aeSJan Kara out:
2320a60637c8SNick Piggin rcu_read_unlock();
2321a60637c8SNick Piggin
2322247f9e1fSVishal Moola (Oracle) return folio_batch_count(fbatch);
23231da177e4SLinus Torvalds }
2324247f9e1fSVishal Moola (Oracle) EXPORT_SYMBOL(filemap_get_folios_tag);
23251da177e4SLinus Torvalds
232676d42bd9SWu Fengguang /*
232776d42bd9SWu Fengguang * CD/DVDs are error prone. When a medium error occurs, the driver may fail
232876d42bd9SWu Fengguang * a _large_ part of the i/o request. Imagine the worst scenario:
232976d42bd9SWu Fengguang *
233076d42bd9SWu Fengguang * ---R__________________________________________B__________
233176d42bd9SWu Fengguang * ^ reading here ^ bad block(assume 4k)
233276d42bd9SWu Fengguang *
233376d42bd9SWu Fengguang * read(R) => miss => readahead(R...B) => media error => frustrating retries
233476d42bd9SWu Fengguang * => failing the whole request => read(R) => read(R+1) =>
233576d42bd9SWu Fengguang * readahead(R+1...B+1) => bang => read(R+2) => read(R+3) =>
233676d42bd9SWu Fengguang * readahead(R+3...B+2) => bang => read(R+3) => read(R+4) =>
233776d42bd9SWu Fengguang * readahead(R+4...B+3) => bang => read(R+4) => read(R+5) => ......
233876d42bd9SWu Fengguang *
233976d42bd9SWu Fengguang * It is going insane. Fix it by quickly scaling down the readahead size.
234076d42bd9SWu Fengguang */
shrink_readahead_size_eio(struct file_ra_state * ra)23410f8e2db4SSouptick Joarder static void shrink_readahead_size_eio(struct file_ra_state *ra)
234276d42bd9SWu Fengguang {
234376d42bd9SWu Fengguang ra->ra_pages /= 4;
234476d42bd9SWu Fengguang }
234576d42bd9SWu Fengguang
2346cbd59c48SMatthew Wilcox (Oracle) /*
234725d6a23eSMatthew Wilcox (Oracle) * filemap_get_read_batch - Get a batch of folios for read
2348cbd59c48SMatthew Wilcox (Oracle) *
234925d6a23eSMatthew Wilcox (Oracle) * Get a batch of folios which represent a contiguous range of bytes in
235025d6a23eSMatthew Wilcox (Oracle) * the file. No exceptional entries will be returned. If @index is in
235125d6a23eSMatthew Wilcox (Oracle) * the middle of a folio, the entire folio will be returned. The last
235225d6a23eSMatthew Wilcox (Oracle) * folio in the batch may have the readahead flag set or the uptodate flag
235325d6a23eSMatthew Wilcox (Oracle) * clear so that the caller can take the appropriate action.
2354cbd59c48SMatthew Wilcox (Oracle) */
filemap_get_read_batch(struct address_space * mapping,pgoff_t index,pgoff_t max,struct folio_batch * fbatch)2355cbd59c48SMatthew Wilcox (Oracle) static void filemap_get_read_batch(struct address_space *mapping,
235625d6a23eSMatthew Wilcox (Oracle) pgoff_t index, pgoff_t max, struct folio_batch *fbatch)
2357cbd59c48SMatthew Wilcox (Oracle) {
2358cbd59c48SMatthew Wilcox (Oracle) XA_STATE(xas, &mapping->i_pages, index);
2359bdb72932SMatthew Wilcox (Oracle) struct folio *folio;
2360cbd59c48SMatthew Wilcox (Oracle)
2361cbd59c48SMatthew Wilcox (Oracle) rcu_read_lock();
2362bdb72932SMatthew Wilcox (Oracle) for (folio = xas_load(&xas); folio; folio = xas_next(&xas)) {
2363bdb72932SMatthew Wilcox (Oracle) if (xas_retry(&xas, folio))
2364cbd59c48SMatthew Wilcox (Oracle) continue;
2365bdb72932SMatthew Wilcox (Oracle) if (xas.xa_index > max || xa_is_value(folio))
2366cbd59c48SMatthew Wilcox (Oracle) break;
2367cb995f4eSMatthew Wilcox (Oracle) if (xa_is_sibling(folio))
2368cb995f4eSMatthew Wilcox (Oracle) break;
236916380f52SYang Shi if (!folio_try_get(folio))
2370cbd59c48SMatthew Wilcox (Oracle) goto retry;
2371cbd59c48SMatthew Wilcox (Oracle)
2372bdb72932SMatthew Wilcox (Oracle) if (unlikely(folio != xas_reload(&xas)))
237325d6a23eSMatthew Wilcox (Oracle) goto put_folio;
2374cbd59c48SMatthew Wilcox (Oracle)
237525d6a23eSMatthew Wilcox (Oracle) if (!folio_batch_add(fbatch, folio))
2376cbd59c48SMatthew Wilcox (Oracle) break;
2377bdb72932SMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio))
2378cbd59c48SMatthew Wilcox (Oracle) break;
2379bdb72932SMatthew Wilcox (Oracle) if (folio_test_readahead(folio))
2380cbd59c48SMatthew Wilcox (Oracle) break;
238187b11f86SSidhartha Kumar xas_advance(&xas, folio_next_index(folio) - 1);
2382cbd59c48SMatthew Wilcox (Oracle) continue;
238325d6a23eSMatthew Wilcox (Oracle) put_folio:
2384bdb72932SMatthew Wilcox (Oracle) folio_put(folio);
2385cbd59c48SMatthew Wilcox (Oracle) retry:
2386cbd59c48SMatthew Wilcox (Oracle) xas_reset(&xas);
2387cbd59c48SMatthew Wilcox (Oracle) }
2388cbd59c48SMatthew Wilcox (Oracle) rcu_read_unlock();
2389cbd59c48SMatthew Wilcox (Oracle) }
2390cbd59c48SMatthew Wilcox (Oracle)
filemap_read_folio(struct file * file,filler_t filler,struct folio * folio)2391290e1a32SMatthew Wilcox (Oracle) static int filemap_read_folio(struct file *file, filler_t filler,
23929d427b4eSMatthew Wilcox (Oracle) struct folio *folio)
2393723ef24bSKent Overstreet {
239417604240SChristoph Hellwig bool workingset = folio_test_workingset(folio);
239517604240SChristoph Hellwig unsigned long pflags;
2396723ef24bSKent Overstreet int error;
2397723ef24bSKent Overstreet
2398723ef24bSKent Overstreet /*
239968430303SMatthew Wilcox (Oracle) * A previous I/O error may have been due to temporary failures,
24007e0a1265SMatthew Wilcox (Oracle) * eg. multipath errors. PG_error will be set again if read_folio
240168430303SMatthew Wilcox (Oracle) * fails.
2402723ef24bSKent Overstreet */
24039d427b4eSMatthew Wilcox (Oracle) folio_clear_error(folio);
240417604240SChristoph Hellwig
2405723ef24bSKent Overstreet /* Start the actual read. The read will unlock the page. */
240617604240SChristoph Hellwig if (unlikely(workingset))
240717604240SChristoph Hellwig psi_memstall_enter(&pflags);
2408290e1a32SMatthew Wilcox (Oracle) error = filler(file, folio);
240917604240SChristoph Hellwig if (unlikely(workingset))
241017604240SChristoph Hellwig psi_memstall_leave(&pflags);
241168430303SMatthew Wilcox (Oracle) if (error)
241268430303SMatthew Wilcox (Oracle) return error;
2413723ef24bSKent Overstreet
24149d427b4eSMatthew Wilcox (Oracle) error = folio_wait_locked_killable(folio);
241568430303SMatthew Wilcox (Oracle) if (error)
241668430303SMatthew Wilcox (Oracle) return error;
24179d427b4eSMatthew Wilcox (Oracle) if (folio_test_uptodate(folio))
2418aa1ec2f6SMatthew Wilcox (Oracle) return 0;
2419290e1a32SMatthew Wilcox (Oracle) if (file)
242068430303SMatthew Wilcox (Oracle) shrink_readahead_size_eio(&file->f_ra);
2421aa1ec2f6SMatthew Wilcox (Oracle) return -EIO;
2422723ef24bSKent Overstreet }
2423723ef24bSKent Overstreet
filemap_range_uptodate(struct address_space * mapping,loff_t pos,size_t count,struct folio * folio,bool need_uptodate)2424fce70da3SMatthew Wilcox (Oracle) static bool filemap_range_uptodate(struct address_space *mapping,
2425dd5b9d00SDavid Howells loff_t pos, size_t count, struct folio *folio,
2426dd5b9d00SDavid Howells bool need_uptodate)
2427fce70da3SMatthew Wilcox (Oracle) {
24282fa4eeb8SMatthew Wilcox (Oracle) if (folio_test_uptodate(folio))
2429fce70da3SMatthew Wilcox (Oracle) return true;
2430fce70da3SMatthew Wilcox (Oracle) /* pipes can't handle partially uptodate pages */
2431dd5b9d00SDavid Howells if (need_uptodate)
2432fce70da3SMatthew Wilcox (Oracle) return false;
2433fce70da3SMatthew Wilcox (Oracle) if (!mapping->a_ops->is_partially_uptodate)
2434fce70da3SMatthew Wilcox (Oracle) return false;
24352fa4eeb8SMatthew Wilcox (Oracle) if (mapping->host->i_blkbits >= folio_shift(folio))
2436fce70da3SMatthew Wilcox (Oracle) return false;
2437fce70da3SMatthew Wilcox (Oracle)
24382fa4eeb8SMatthew Wilcox (Oracle) if (folio_pos(folio) > pos) {
24392fa4eeb8SMatthew Wilcox (Oracle) count -= folio_pos(folio) - pos;
2440fce70da3SMatthew Wilcox (Oracle) pos = 0;
2441fce70da3SMatthew Wilcox (Oracle) } else {
24422fa4eeb8SMatthew Wilcox (Oracle) pos -= folio_pos(folio);
2443fce70da3SMatthew Wilcox (Oracle) }
2444fce70da3SMatthew Wilcox (Oracle)
24452e7e80f7SMatthew Wilcox (Oracle) return mapping->a_ops->is_partially_uptodate(folio, pos, count);
2446fce70da3SMatthew Wilcox (Oracle) }
2447fce70da3SMatthew Wilcox (Oracle)
filemap_update_page(struct kiocb * iocb,struct address_space * mapping,size_t count,struct folio * folio,bool need_uptodate)24484612aeefSMatthew Wilcox (Oracle) static int filemap_update_page(struct kiocb *iocb,
2449dd5b9d00SDavid Howells struct address_space *mapping, size_t count,
2450dd5b9d00SDavid Howells struct folio *folio, bool need_uptodate)
2451723ef24bSKent Overstreet {
2452723ef24bSKent Overstreet int error;
2453723ef24bSKent Overstreet
2454730633f0SJan Kara if (iocb->ki_flags & IOCB_NOWAIT) {
2455730633f0SJan Kara if (!filemap_invalidate_trylock_shared(mapping))
245687d1d7b6SMatthew Wilcox (Oracle) return -EAGAIN;
2457730633f0SJan Kara } else {
2458730633f0SJan Kara filemap_invalidate_lock_shared(mapping);
2459730633f0SJan Kara }
2460730633f0SJan Kara
2461ffdc8dabSMatthew Wilcox (Oracle) if (!folio_trylock(folio)) {
2462730633f0SJan Kara error = -EAGAIN;
2463730633f0SJan Kara if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_NOIO))
2464730633f0SJan Kara goto unlock_mapping;
246587d1d7b6SMatthew Wilcox (Oracle) if (!(iocb->ki_flags & IOCB_WAITQ)) {
2466730633f0SJan Kara filemap_invalidate_unlock_shared(mapping);
24679f2b04a2SMatthew Wilcox (Oracle) /*
24689f2b04a2SMatthew Wilcox (Oracle) * This is where we usually end up waiting for a
24699f2b04a2SMatthew Wilcox (Oracle) * previously submitted readahead to finish.
24709f2b04a2SMatthew Wilcox (Oracle) */
24719f2b04a2SMatthew Wilcox (Oracle) folio_put_wait_locked(folio, TASK_KILLABLE);
24724612aeefSMatthew Wilcox (Oracle) return AOP_TRUNCATED_PAGE;
2473bd8a1f36SMatthew Wilcox (Oracle) }
2474ffdc8dabSMatthew Wilcox (Oracle) error = __folio_lock_async(folio, iocb->ki_waitq);
247587d1d7b6SMatthew Wilcox (Oracle) if (error)
2476730633f0SJan Kara goto unlock_mapping;
2477bd8a1f36SMatthew Wilcox (Oracle) }
2478723ef24bSKent Overstreet
2479730633f0SJan Kara error = AOP_TRUNCATED_PAGE;
2480ffdc8dabSMatthew Wilcox (Oracle) if (!folio->mapping)
2481730633f0SJan Kara goto unlock;
2482723ef24bSKent Overstreet
2483fce70da3SMatthew Wilcox (Oracle) error = 0;
2484dd5b9d00SDavid Howells if (filemap_range_uptodate(mapping, iocb->ki_pos, count, folio,
2485dd5b9d00SDavid Howells need_uptodate))
2486fce70da3SMatthew Wilcox (Oracle) goto unlock;
2487fce70da3SMatthew Wilcox (Oracle)
2488fce70da3SMatthew Wilcox (Oracle) error = -EAGAIN;
2489fce70da3SMatthew Wilcox (Oracle) if (iocb->ki_flags & (IOCB_NOIO | IOCB_NOWAIT | IOCB_WAITQ))
2490fce70da3SMatthew Wilcox (Oracle) goto unlock;
2491fce70da3SMatthew Wilcox (Oracle)
2492290e1a32SMatthew Wilcox (Oracle) error = filemap_read_folio(iocb->ki_filp, mapping->a_ops->read_folio,
2493290e1a32SMatthew Wilcox (Oracle) folio);
2494730633f0SJan Kara goto unlock_mapping;
2495fce70da3SMatthew Wilcox (Oracle) unlock:
2496ffdc8dabSMatthew Wilcox (Oracle) folio_unlock(folio);
2497730633f0SJan Kara unlock_mapping:
2498730633f0SJan Kara filemap_invalidate_unlock_shared(mapping);
2499730633f0SJan Kara if (error == AOP_TRUNCATED_PAGE)
2500ffdc8dabSMatthew Wilcox (Oracle) folio_put(folio);
2501fce70da3SMatthew Wilcox (Oracle) return error;
2502723ef24bSKent Overstreet }
2503723ef24bSKent Overstreet
filemap_create_folio(struct file * file,struct address_space * mapping,pgoff_t index,struct folio_batch * fbatch)2504a5d4ad09SMatthew Wilcox (Oracle) static int filemap_create_folio(struct file *file,
2505f253e185SMatthew Wilcox (Oracle) struct address_space *mapping, pgoff_t index,
250625d6a23eSMatthew Wilcox (Oracle) struct folio_batch *fbatch)
2507723ef24bSKent Overstreet {
2508a5d4ad09SMatthew Wilcox (Oracle) struct folio *folio;
2509723ef24bSKent Overstreet int error;
2510723ef24bSKent Overstreet
2511a5d4ad09SMatthew Wilcox (Oracle) folio = filemap_alloc_folio(mapping_gfp_mask(mapping), 0);
2512a5d4ad09SMatthew Wilcox (Oracle) if (!folio)
2513f253e185SMatthew Wilcox (Oracle) return -ENOMEM;
2514723ef24bSKent Overstreet
2515730633f0SJan Kara /*
2516a5d4ad09SMatthew Wilcox (Oracle) * Protect against truncate / hole punch. Grabbing invalidate_lock
2517a5d4ad09SMatthew Wilcox (Oracle) * here assures we cannot instantiate and bring uptodate new
2518a5d4ad09SMatthew Wilcox (Oracle) * pagecache folios after evicting page cache during truncate
2519a5d4ad09SMatthew Wilcox (Oracle) * and before actually freeing blocks. Note that we could
2520a5d4ad09SMatthew Wilcox (Oracle) * release invalidate_lock after inserting the folio into
2521a5d4ad09SMatthew Wilcox (Oracle) * the page cache as the locked folio would then be enough to
2522a5d4ad09SMatthew Wilcox (Oracle) * synchronize with hole punching. But there are code paths
2523a5d4ad09SMatthew Wilcox (Oracle) * such as filemap_update_page() filling in partially uptodate
2524704528d8SMatthew Wilcox (Oracle) * pages or ->readahead() that need to hold invalidate_lock
2525a5d4ad09SMatthew Wilcox (Oracle) * while mapping blocks for IO so let's hold the lock here as
2526a5d4ad09SMatthew Wilcox (Oracle) * well to keep locking rules simple.
2527730633f0SJan Kara */
2528730633f0SJan Kara filemap_invalidate_lock_shared(mapping);
2529a5d4ad09SMatthew Wilcox (Oracle) error = filemap_add_folio(mapping, folio, index,
2530723ef24bSKent Overstreet mapping_gfp_constraint(mapping, GFP_KERNEL));
2531f253e185SMatthew Wilcox (Oracle) if (error == -EEXIST)
2532f253e185SMatthew Wilcox (Oracle) error = AOP_TRUNCATED_PAGE;
2533f253e185SMatthew Wilcox (Oracle) if (error)
2534f253e185SMatthew Wilcox (Oracle) goto error;
2535f253e185SMatthew Wilcox (Oracle)
2536290e1a32SMatthew Wilcox (Oracle) error = filemap_read_folio(file, mapping->a_ops->read_folio, folio);
2537f253e185SMatthew Wilcox (Oracle) if (error)
2538f253e185SMatthew Wilcox (Oracle) goto error;
2539f253e185SMatthew Wilcox (Oracle)
2540730633f0SJan Kara filemap_invalidate_unlock_shared(mapping);
254125d6a23eSMatthew Wilcox (Oracle) folio_batch_add(fbatch, folio);
2542f253e185SMatthew Wilcox (Oracle) return 0;
2543f253e185SMatthew Wilcox (Oracle) error:
2544730633f0SJan Kara filemap_invalidate_unlock_shared(mapping);
2545a5d4ad09SMatthew Wilcox (Oracle) folio_put(folio);
2546f253e185SMatthew Wilcox (Oracle) return error;
2547723ef24bSKent Overstreet }
2548723ef24bSKent Overstreet
filemap_readahead(struct kiocb * iocb,struct file * file,struct address_space * mapping,struct folio * folio,pgoff_t last_index)25495963fe03SMatthew Wilcox (Oracle) static int filemap_readahead(struct kiocb *iocb, struct file *file,
255065bca53bSMatthew Wilcox (Oracle) struct address_space *mapping, struct folio *folio,
25515963fe03SMatthew Wilcox (Oracle) pgoff_t last_index)
25525963fe03SMatthew Wilcox (Oracle) {
255365bca53bSMatthew Wilcox (Oracle) DEFINE_READAHEAD(ractl, file, &file->f_ra, mapping, folio->index);
255465bca53bSMatthew Wilcox (Oracle)
25555963fe03SMatthew Wilcox (Oracle) if (iocb->ki_flags & IOCB_NOIO)
25565963fe03SMatthew Wilcox (Oracle) return -EAGAIN;
255765bca53bSMatthew Wilcox (Oracle) page_cache_async_ra(&ractl, folio, last_index - folio->index);
25585963fe03SMatthew Wilcox (Oracle) return 0;
25595963fe03SMatthew Wilcox (Oracle) }
25605963fe03SMatthew Wilcox (Oracle)
filemap_get_pages(struct kiocb * iocb,size_t count,struct folio_batch * fbatch,bool need_uptodate)2561dd5b9d00SDavid Howells static int filemap_get_pages(struct kiocb *iocb, size_t count,
2562dd5b9d00SDavid Howells struct folio_batch *fbatch, bool need_uptodate)
256306c04442SKent Overstreet {
256406c04442SKent Overstreet struct file *filp = iocb->ki_filp;
256506c04442SKent Overstreet struct address_space *mapping = filp->f_mapping;
256606c04442SKent Overstreet struct file_ra_state *ra = &filp->f_ra;
256706c04442SKent Overstreet pgoff_t index = iocb->ki_pos >> PAGE_SHIFT;
2568cbd59c48SMatthew Wilcox (Oracle) pgoff_t last_index;
256965bca53bSMatthew Wilcox (Oracle) struct folio *folio;
2570cbd59c48SMatthew Wilcox (Oracle) int err = 0;
257106c04442SKent Overstreet
25725956592cSQian Yingjin /* "last_index" is the index of the page beyond the end of the read */
2573dd5b9d00SDavid Howells last_index = DIV_ROUND_UP(iocb->ki_pos + count, PAGE_SIZE);
25742642fca6SMatthew Wilcox (Oracle) retry:
257506c04442SKent Overstreet if (fatal_signal_pending(current))
257606c04442SKent Overstreet return -EINTR;
257706c04442SKent Overstreet
25785956592cSQian Yingjin filemap_get_read_batch(mapping, index, last_index - 1, fbatch);
257925d6a23eSMatthew Wilcox (Oracle) if (!folio_batch_count(fbatch)) {
258006c04442SKent Overstreet if (iocb->ki_flags & IOCB_NOIO)
258106c04442SKent Overstreet return -EAGAIN;
25822642fca6SMatthew Wilcox (Oracle) page_cache_sync_readahead(mapping, ra, filp, index,
25832642fca6SMatthew Wilcox (Oracle) last_index - index);
25845956592cSQian Yingjin filemap_get_read_batch(mapping, index, last_index - 1, fbatch);
25852642fca6SMatthew Wilcox (Oracle) }
258625d6a23eSMatthew Wilcox (Oracle) if (!folio_batch_count(fbatch)) {
2587f253e185SMatthew Wilcox (Oracle) if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_WAITQ))
2588f253e185SMatthew Wilcox (Oracle) return -EAGAIN;
2589a5d4ad09SMatthew Wilcox (Oracle) err = filemap_create_folio(filp, mapping,
259025d6a23eSMatthew Wilcox (Oracle) iocb->ki_pos >> PAGE_SHIFT, fbatch);
2591f253e185SMatthew Wilcox (Oracle) if (err == AOP_TRUNCATED_PAGE)
25922642fca6SMatthew Wilcox (Oracle) goto retry;
2593f253e185SMatthew Wilcox (Oracle) return err;
2594f253e185SMatthew Wilcox (Oracle) }
259506c04442SKent Overstreet
259625d6a23eSMatthew Wilcox (Oracle) folio = fbatch->folios[folio_batch_count(fbatch) - 1];
259765bca53bSMatthew Wilcox (Oracle) if (folio_test_readahead(folio)) {
259865bca53bSMatthew Wilcox (Oracle) err = filemap_readahead(iocb, filp, mapping, folio, last_index);
25992642fca6SMatthew Wilcox (Oracle) if (err)
2600cbd59c48SMatthew Wilcox (Oracle) goto err;
260106c04442SKent Overstreet }
260265bca53bSMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) {
260325d6a23eSMatthew Wilcox (Oracle) if ((iocb->ki_flags & IOCB_WAITQ) &&
260425d6a23eSMatthew Wilcox (Oracle) folio_batch_count(fbatch) > 1)
260587d1d7b6SMatthew Wilcox (Oracle) iocb->ki_flags |= IOCB_NOWAIT;
2606dd5b9d00SDavid Howells err = filemap_update_page(iocb, mapping, count, folio,
2607dd5b9d00SDavid Howells need_uptodate);
26082642fca6SMatthew Wilcox (Oracle) if (err)
26092642fca6SMatthew Wilcox (Oracle) goto err;
261006c04442SKent Overstreet }
261106c04442SKent Overstreet
26122642fca6SMatthew Wilcox (Oracle) return 0;
2613cbd59c48SMatthew Wilcox (Oracle) err:
26142642fca6SMatthew Wilcox (Oracle) if (err < 0)
261565bca53bSMatthew Wilcox (Oracle) folio_put(folio);
261625d6a23eSMatthew Wilcox (Oracle) if (likely(--fbatch->nr))
2617ff993ba1SMatthew Wilcox (Oracle) return 0;
26184612aeefSMatthew Wilcox (Oracle) if (err == AOP_TRUNCATED_PAGE)
26192642fca6SMatthew Wilcox (Oracle) goto retry;
262006c04442SKent Overstreet return err;
262106c04442SKent Overstreet }
262206c04442SKent Overstreet
pos_same_folio(loff_t pos1,loff_t pos2,struct folio * folio)26235ccc944dSMatthew Wilcox (Oracle) static inline bool pos_same_folio(loff_t pos1, loff_t pos2, struct folio *folio)
26245ccc944dSMatthew Wilcox (Oracle) {
26255ccc944dSMatthew Wilcox (Oracle) unsigned int shift = folio_shift(folio);
26265ccc944dSMatthew Wilcox (Oracle)
26275ccc944dSMatthew Wilcox (Oracle) return (pos1 >> shift == pos2 >> shift);
26285ccc944dSMatthew Wilcox (Oracle) }
26295ccc944dSMatthew Wilcox (Oracle)
2630485bb99bSRandy Dunlap /**
263187fa0f3eSChristoph Hellwig * filemap_read - Read data from the page cache.
263287fa0f3eSChristoph Hellwig * @iocb: The iocb to read.
263387fa0f3eSChristoph Hellwig * @iter: Destination for the data.
263487fa0f3eSChristoph Hellwig * @already_read: Number of bytes already read by the caller.
2635485bb99bSRandy Dunlap *
263687fa0f3eSChristoph Hellwig * Copies data from the page cache. If the data is not currently present,
26377e0a1265SMatthew Wilcox (Oracle) * uses the readahead and read_folio address_space operations to fetch it.
26381da177e4SLinus Torvalds *
263987fa0f3eSChristoph Hellwig * Return: Total number of bytes copied, including those already read by
264087fa0f3eSChristoph Hellwig * the caller. If an error happens before any bytes are copied, returns
264187fa0f3eSChristoph Hellwig * a negative error number.
26421da177e4SLinus Torvalds */
filemap_read(struct kiocb * iocb,struct iov_iter * iter,ssize_t already_read)264387fa0f3eSChristoph Hellwig ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
264487fa0f3eSChristoph Hellwig ssize_t already_read)
26451da177e4SLinus Torvalds {
264647c27bc4SChristoph Hellwig struct file *filp = iocb->ki_filp;
264706c04442SKent Overstreet struct file_ra_state *ra = &filp->f_ra;
264836e78914SChristoph Hellwig struct address_space *mapping = filp->f_mapping;
26491da177e4SLinus Torvalds struct inode *inode = mapping->host;
265025d6a23eSMatthew Wilcox (Oracle) struct folio_batch fbatch;
2651ff993ba1SMatthew Wilcox (Oracle) int i, error = 0;
265206c04442SKent Overstreet bool writably_mapped;
265306c04442SKent Overstreet loff_t isize, end_offset;
2654f04d16eeSHaibo Li loff_t last_pos = ra->prev_pos;
26551da177e4SLinus Torvalds
2656723ef24bSKent Overstreet if (unlikely(iocb->ki_pos >= inode->i_sb->s_maxbytes))
2657d05c5f7bSLinus Torvalds return 0;
26583644e2d2SKent Overstreet if (unlikely(!iov_iter_count(iter)))
26593644e2d2SKent Overstreet return 0;
26603644e2d2SKent Overstreet
2661a2746ab3STrond Myklebust iov_iter_truncate(iter, inode->i_sb->s_maxbytes - iocb->ki_pos);
266225d6a23eSMatthew Wilcox (Oracle) folio_batch_init(&fbatch);
2663c2a9737fSWei Fang
266406c04442SKent Overstreet do {
266506c04442SKent Overstreet cond_resched();
26661da177e4SLinus Torvalds
266713bd6914SJens Axboe /*
266813bd6914SJens Axboe * If we've already successfully copied some data, then we
266913bd6914SJens Axboe * can no longer safely return -EIOCBQUEUED. Hence mark
267013bd6914SJens Axboe * an async read NOWAIT at that point.
267113bd6914SJens Axboe */
267287fa0f3eSChristoph Hellwig if ((iocb->ki_flags & IOCB_WAITQ) && already_read)
267313bd6914SJens Axboe iocb->ki_flags |= IOCB_NOWAIT;
267413bd6914SJens Axboe
26758c8387eeSDavid Howells if (unlikely(iocb->ki_pos >= i_size_read(inode)))
26768c8387eeSDavid Howells break;
26778c8387eeSDavid Howells
26783fc40265SDavid Howells error = filemap_get_pages(iocb, iter->count, &fbatch, false);
2679ff993ba1SMatthew Wilcox (Oracle) if (error < 0)
268006c04442SKent Overstreet break;
26815abf186aSMichal Hocko
2682723ef24bSKent Overstreet /*
268306c04442SKent Overstreet * i_size must be checked after we know the pages are Uptodate.
268406c04442SKent Overstreet *
268506c04442SKent Overstreet * Checking i_size after the check allows us to calculate
268606c04442SKent Overstreet * the correct value for "nr", which means the zero-filled
268706c04442SKent Overstreet * part of the page is not copied back to userspace (unless
268806c04442SKent Overstreet * another truncate extends the file - this is desired though).
2689723ef24bSKent Overstreet */
269006c04442SKent Overstreet isize = i_size_read(inode);
269106c04442SKent Overstreet if (unlikely(iocb->ki_pos >= isize))
269225d6a23eSMatthew Wilcox (Oracle) goto put_folios;
269306c04442SKent Overstreet end_offset = min_t(loff_t, isize, iocb->ki_pos + iter->count);
2694c8d317aaSHao Xu
269506c04442SKent Overstreet /*
2696d16eb52cSBaokun Li * Pairs with a barrier in
2697d16eb52cSBaokun Li * block_write_end()->mark_buffer_dirty() or other page
2698d16eb52cSBaokun Li * dirtying routines like iomap_write_end() to ensure
2699d16eb52cSBaokun Li * changes to page contents are visible before we see
2700d16eb52cSBaokun Li * increased inode size.
2701d16eb52cSBaokun Li */
2702d16eb52cSBaokun Li smp_rmb();
2703d16eb52cSBaokun Li
2704d16eb52cSBaokun Li /*
270506c04442SKent Overstreet * Once we start copying data, we don't want to be touching any
270606c04442SKent Overstreet * cachelines that might be contended:
270706c04442SKent Overstreet */
270806c04442SKent Overstreet writably_mapped = mapping_writably_mapped(mapping);
270906c04442SKent Overstreet
271006c04442SKent Overstreet /*
27115ccc944dSMatthew Wilcox (Oracle) * When a read accesses the same folio several times, only
271206c04442SKent Overstreet * mark it as accessed the first time.
271306c04442SKent Overstreet */
2714f04d16eeSHaibo Li if (!pos_same_folio(iocb->ki_pos, last_pos - 1,
27155ccc944dSMatthew Wilcox (Oracle) fbatch.folios[0]))
271625d6a23eSMatthew Wilcox (Oracle) folio_mark_accessed(fbatch.folios[0]);
271706c04442SKent Overstreet
271825d6a23eSMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(&fbatch); i++) {
271925d6a23eSMatthew Wilcox (Oracle) struct folio *folio = fbatch.folios[i];
2720d996fc7fSMatthew Wilcox (Oracle) size_t fsize = folio_size(folio);
2721d996fc7fSMatthew Wilcox (Oracle) size_t offset = iocb->ki_pos & (fsize - 1);
2722cbd59c48SMatthew Wilcox (Oracle) size_t bytes = min_t(loff_t, end_offset - iocb->ki_pos,
2723d996fc7fSMatthew Wilcox (Oracle) fsize - offset);
2724cbd59c48SMatthew Wilcox (Oracle) size_t copied;
272506c04442SKent Overstreet
2726d996fc7fSMatthew Wilcox (Oracle) if (end_offset < folio_pos(folio))
2727cbd59c48SMatthew Wilcox (Oracle) break;
2728cbd59c48SMatthew Wilcox (Oracle) if (i > 0)
2729d996fc7fSMatthew Wilcox (Oracle) folio_mark_accessed(folio);
273006c04442SKent Overstreet /*
2731d996fc7fSMatthew Wilcox (Oracle) * If users can be writing to this folio using arbitrary
2732d996fc7fSMatthew Wilcox (Oracle) * virtual addresses, take care of potential aliasing
2733d996fc7fSMatthew Wilcox (Oracle) * before reading the folio on the kernel side.
273406c04442SKent Overstreet */
2735d996fc7fSMatthew Wilcox (Oracle) if (writably_mapped)
2736d996fc7fSMatthew Wilcox (Oracle) flush_dcache_folio(folio);
273706c04442SKent Overstreet
2738d996fc7fSMatthew Wilcox (Oracle) copied = copy_folio_to_iter(folio, offset, bytes, iter);
273906c04442SKent Overstreet
274087fa0f3eSChristoph Hellwig already_read += copied;
274106c04442SKent Overstreet iocb->ki_pos += copied;
2742f04d16eeSHaibo Li last_pos = iocb->ki_pos;
274306c04442SKent Overstreet
274406c04442SKent Overstreet if (copied < bytes) {
274506c04442SKent Overstreet error = -EFAULT;
274606c04442SKent Overstreet break;
27471da177e4SLinus Torvalds }
27481da177e4SLinus Torvalds }
274925d6a23eSMatthew Wilcox (Oracle) put_folios:
275025d6a23eSMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(&fbatch); i++)
275125d6a23eSMatthew Wilcox (Oracle) folio_put(fbatch.folios[i]);
275225d6a23eSMatthew Wilcox (Oracle) folio_batch_init(&fbatch);
275306c04442SKent Overstreet } while (iov_iter_count(iter) && iocb->ki_pos < isize && !error);
27541da177e4SLinus Torvalds
27551da177e4SLinus Torvalds file_accessed(filp);
2756f04d16eeSHaibo Li ra->prev_pos = last_pos;
275787fa0f3eSChristoph Hellwig return already_read ? already_read : error;
27581da177e4SLinus Torvalds }
275987fa0f3eSChristoph Hellwig EXPORT_SYMBOL_GPL(filemap_read);
27601da177e4SLinus Torvalds
kiocb_write_and_wait(struct kiocb * iocb,size_t count)27613c435a0fSChristoph Hellwig int kiocb_write_and_wait(struct kiocb *iocb, size_t count)
27623c435a0fSChristoph Hellwig {
27633c435a0fSChristoph Hellwig struct address_space *mapping = iocb->ki_filp->f_mapping;
27643c435a0fSChristoph Hellwig loff_t pos = iocb->ki_pos;
27653c435a0fSChristoph Hellwig loff_t end = pos + count - 1;
27663c435a0fSChristoph Hellwig
27673c435a0fSChristoph Hellwig if (iocb->ki_flags & IOCB_NOWAIT) {
27683c435a0fSChristoph Hellwig if (filemap_range_needs_writeback(mapping, pos, end))
27693c435a0fSChristoph Hellwig return -EAGAIN;
27703c435a0fSChristoph Hellwig return 0;
27713c435a0fSChristoph Hellwig }
27723c435a0fSChristoph Hellwig
27733c435a0fSChristoph Hellwig return filemap_write_and_wait_range(mapping, pos, end);
27743c435a0fSChristoph Hellwig }
27753c435a0fSChristoph Hellwig
kiocb_invalidate_pages(struct kiocb * iocb,size_t count)2776e003f74aSChristoph Hellwig int kiocb_invalidate_pages(struct kiocb *iocb, size_t count)
2777e003f74aSChristoph Hellwig {
2778e003f74aSChristoph Hellwig struct address_space *mapping = iocb->ki_filp->f_mapping;
2779e003f74aSChristoph Hellwig loff_t pos = iocb->ki_pos;
2780e003f74aSChristoph Hellwig loff_t end = pos + count - 1;
2781e003f74aSChristoph Hellwig int ret;
2782e003f74aSChristoph Hellwig
2783e003f74aSChristoph Hellwig if (iocb->ki_flags & IOCB_NOWAIT) {
2784e003f74aSChristoph Hellwig /* we could block if there are any pages in the range */
2785e003f74aSChristoph Hellwig if (filemap_range_has_page(mapping, pos, end))
2786e003f74aSChristoph Hellwig return -EAGAIN;
2787e003f74aSChristoph Hellwig } else {
2788e003f74aSChristoph Hellwig ret = filemap_write_and_wait_range(mapping, pos, end);
2789e003f74aSChristoph Hellwig if (ret)
2790e003f74aSChristoph Hellwig return ret;
2791e003f74aSChristoph Hellwig }
2792e003f74aSChristoph Hellwig
2793e003f74aSChristoph Hellwig /*
2794e003f74aSChristoph Hellwig * After a write we want buffered reads to be sure to go to disk to get
2795e003f74aSChristoph Hellwig * the new data. We invalidate clean cached page from the region we're
2796e003f74aSChristoph Hellwig * about to write. We do this *before* the write so that we can return
2797e003f74aSChristoph Hellwig * without clobbering -EIOCBQUEUED from ->direct_IO().
2798e003f74aSChristoph Hellwig */
2799e003f74aSChristoph Hellwig return invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT,
2800e003f74aSChristoph Hellwig end >> PAGE_SHIFT);
2801e003f74aSChristoph Hellwig }
2802e003f74aSChristoph Hellwig
2803485bb99bSRandy Dunlap /**
28046abd2322SAl Viro * generic_file_read_iter - generic filesystem read routine
2805485bb99bSRandy Dunlap * @iocb: kernel I/O control block
28066abd2322SAl Viro * @iter: destination for the data read
2807485bb99bSRandy Dunlap *
28086abd2322SAl Viro * This is the "read_iter()" routine for all filesystems
28091da177e4SLinus Torvalds * that can use the page cache directly.
281041da51bcSAndreas Gruenbacher *
281141da51bcSAndreas Gruenbacher * The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall
281241da51bcSAndreas Gruenbacher * be returned when no data can be read without waiting for I/O requests
281341da51bcSAndreas Gruenbacher * to complete; it doesn't prevent readahead.
281441da51bcSAndreas Gruenbacher *
281541da51bcSAndreas Gruenbacher * The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O
281641da51bcSAndreas Gruenbacher * requests shall be made for the read or for readahead. When no data
281741da51bcSAndreas Gruenbacher * can be read, -EAGAIN shall be returned. When readahead would be
281841da51bcSAndreas Gruenbacher * triggered, a partial, possibly empty read shall be returned.
281941da51bcSAndreas Gruenbacher *
2820a862f68aSMike Rapoport * Return:
2821a862f68aSMike Rapoport * * number of bytes copied, even for partial reads
282241da51bcSAndreas Gruenbacher * * negative error code (or 0 if IOCB_NOIO) if nothing was read
28231da177e4SLinus Torvalds */
28241da177e4SLinus Torvalds ssize_t
generic_file_read_iter(struct kiocb * iocb,struct iov_iter * iter)2825ed978a81SAl Viro generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
28261da177e4SLinus Torvalds {
2827e7080a43SNicolai Stange size_t count = iov_iter_count(iter);
282847c27bc4SChristoph Hellwig ssize_t retval = 0;
2829e7080a43SNicolai Stange
2830e7080a43SNicolai Stange if (!count)
2831826ea860SChristoph Hellwig return 0; /* skip atime */
28321da177e4SLinus Torvalds
28332ba48ce5SAl Viro if (iocb->ki_flags & IOCB_DIRECT) {
283447c27bc4SChristoph Hellwig struct file *file = iocb->ki_filp;
2835ed978a81SAl Viro struct address_space *mapping = file->f_mapping;
2836ed978a81SAl Viro struct inode *inode = mapping->host;
28371da177e4SLinus Torvalds
28383c435a0fSChristoph Hellwig retval = kiocb_write_and_wait(iocb, count);
28390d5b0cf2SChristoph Hellwig if (retval < 0)
2840826ea860SChristoph Hellwig return retval;
28410d5b0cf2SChristoph Hellwig file_accessed(file);
28420d5b0cf2SChristoph Hellwig
28435ecda137SAl Viro retval = mapping->a_ops->direct_IO(iocb, iter);
2844c3a69024SAl Viro if (retval >= 0) {
2845c64fb5c7SChristoph Hellwig iocb->ki_pos += retval;
28465ecda137SAl Viro count -= retval;
284766f998f6SJosef Bacik }
2848ab2125dfSPavel Begunkov if (retval != -EIOCBQUEUED)
28495b47d59aSAl Viro iov_iter_revert(iter, count - iov_iter_count(iter));
285066f998f6SJosef Bacik
285166f998f6SJosef Bacik /*
285266f998f6SJosef Bacik * Btrfs can have a short DIO read if we encounter
285366f998f6SJosef Bacik * compressed extents, so if there was an error, or if
285466f998f6SJosef Bacik * we've already read everything we wanted to, or if
285566f998f6SJosef Bacik * there was a short read because we hit EOF, go ahead
285666f998f6SJosef Bacik * and return. Otherwise fallthrough to buffered io for
2857fbbbad4bSMatthew Wilcox * the rest of the read. Buffered reads will not work for
2858fbbbad4bSMatthew Wilcox * DAX files, so don't bother trying.
285966f998f6SJosef Bacik */
286061d0017eSJens Axboe if (retval < 0 || !count || IS_DAX(inode))
286161d0017eSJens Axboe return retval;
286261d0017eSJens Axboe if (iocb->ki_pos >= i_size_read(inode))
2863826ea860SChristoph Hellwig return retval;
28641da177e4SLinus Torvalds }
28651da177e4SLinus Torvalds
2866826ea860SChristoph Hellwig return filemap_read(iocb, iter, retval);
28671da177e4SLinus Torvalds }
2868ed978a81SAl Viro EXPORT_SYMBOL(generic_file_read_iter);
28691da177e4SLinus Torvalds
287007073eb0SDavid Howells /*
287107073eb0SDavid Howells * Splice subpages from a folio into a pipe.
287207073eb0SDavid Howells */
splice_folio_into_pipe(struct pipe_inode_info * pipe,struct folio * folio,loff_t fpos,size_t size)287307073eb0SDavid Howells size_t splice_folio_into_pipe(struct pipe_inode_info *pipe,
287407073eb0SDavid Howells struct folio *folio, loff_t fpos, size_t size)
287507073eb0SDavid Howells {
287607073eb0SDavid Howells struct page *page;
287707073eb0SDavid Howells size_t spliced = 0, offset = offset_in_folio(folio, fpos);
287807073eb0SDavid Howells
287907073eb0SDavid Howells page = folio_page(folio, offset / PAGE_SIZE);
288007073eb0SDavid Howells size = min(size, folio_size(folio) - offset);
288107073eb0SDavid Howells offset %= PAGE_SIZE;
288207073eb0SDavid Howells
288307073eb0SDavid Howells while (spliced < size &&
288407073eb0SDavid Howells !pipe_full(pipe->head, pipe->tail, pipe->max_usage)) {
288507073eb0SDavid Howells struct pipe_buffer *buf = pipe_head_buf(pipe);
288607073eb0SDavid Howells size_t part = min_t(size_t, PAGE_SIZE - offset, size - spliced);
288707073eb0SDavid Howells
288807073eb0SDavid Howells *buf = (struct pipe_buffer) {
288907073eb0SDavid Howells .ops = &page_cache_pipe_buf_ops,
289007073eb0SDavid Howells .page = page,
289107073eb0SDavid Howells .offset = offset,
289207073eb0SDavid Howells .len = part,
289307073eb0SDavid Howells };
289407073eb0SDavid Howells folio_get(folio);
289507073eb0SDavid Howells pipe->head++;
289607073eb0SDavid Howells page++;
289707073eb0SDavid Howells spliced += part;
289807073eb0SDavid Howells offset = 0;
289907073eb0SDavid Howells }
290007073eb0SDavid Howells
290107073eb0SDavid Howells return spliced;
290207073eb0SDavid Howells }
290307073eb0SDavid Howells
29049eee8bd8SDavid Howells /**
29059eee8bd8SDavid Howells * filemap_splice_read - Splice data from a file's pagecache into a pipe
29069eee8bd8SDavid Howells * @in: The file to read from
29079eee8bd8SDavid Howells * @ppos: Pointer to the file position to read from
29089eee8bd8SDavid Howells * @pipe: The pipe to splice into
29099eee8bd8SDavid Howells * @len: The amount to splice
29109eee8bd8SDavid Howells * @flags: The SPLICE_F_* flags
29119eee8bd8SDavid Howells *
29129eee8bd8SDavid Howells * This function gets folios from a file's pagecache and splices them into the
29139eee8bd8SDavid Howells * pipe. Readahead will be called as necessary to fill more folios. This may
29149eee8bd8SDavid Howells * be used for blockdevs also.
29159eee8bd8SDavid Howells *
29169eee8bd8SDavid Howells * Return: On success, the number of bytes read will be returned and *@ppos
29179eee8bd8SDavid Howells * will be updated if appropriate; 0 will be returned if there is no more data
29189eee8bd8SDavid Howells * to be read; -EAGAIN will be returned if the pipe had no space, and some
29199eee8bd8SDavid Howells * other negative error code will be returned on error. A short read may occur
29209eee8bd8SDavid Howells * if the pipe has insufficient space, we reach the end of the data or we hit a
29219eee8bd8SDavid Howells * hole.
292207073eb0SDavid Howells */
filemap_splice_read(struct file * in,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)292307073eb0SDavid Howells ssize_t filemap_splice_read(struct file *in, loff_t *ppos,
292407073eb0SDavid Howells struct pipe_inode_info *pipe,
292507073eb0SDavid Howells size_t len, unsigned int flags)
292607073eb0SDavid Howells {
292707073eb0SDavid Howells struct folio_batch fbatch;
292807073eb0SDavid Howells struct kiocb iocb;
292907073eb0SDavid Howells size_t total_spliced = 0, used, npages;
293007073eb0SDavid Howells loff_t isize, end_offset;
293107073eb0SDavid Howells bool writably_mapped;
293207073eb0SDavid Howells int i, error = 0;
293307073eb0SDavid Howells
293483aeff88SDavid Howells if (unlikely(*ppos >= in->f_mapping->host->i_sb->s_maxbytes))
293583aeff88SDavid Howells return 0;
293683aeff88SDavid Howells
293707073eb0SDavid Howells init_sync_kiocb(&iocb, in);
293807073eb0SDavid Howells iocb.ki_pos = *ppos;
293907073eb0SDavid Howells
294007073eb0SDavid Howells /* Work out how much data we can actually add into the pipe */
294107073eb0SDavid Howells used = pipe_occupancy(pipe->head, pipe->tail);
294207073eb0SDavid Howells npages = max_t(ssize_t, pipe->max_usage - used, 0);
294307073eb0SDavid Howells len = min_t(size_t, len, npages * PAGE_SIZE);
294407073eb0SDavid Howells
294507073eb0SDavid Howells folio_batch_init(&fbatch);
294607073eb0SDavid Howells
294707073eb0SDavid Howells do {
294807073eb0SDavid Howells cond_resched();
294907073eb0SDavid Howells
2950c3722208SDavid Howells if (*ppos >= i_size_read(in->f_mapping->host))
295107073eb0SDavid Howells break;
295207073eb0SDavid Howells
295307073eb0SDavid Howells iocb.ki_pos = *ppos;
295407073eb0SDavid Howells error = filemap_get_pages(&iocb, len, &fbatch, true);
295507073eb0SDavid Howells if (error < 0)
295607073eb0SDavid Howells break;
295707073eb0SDavid Howells
295807073eb0SDavid Howells /*
295907073eb0SDavid Howells * i_size must be checked after we know the pages are Uptodate.
296007073eb0SDavid Howells *
296107073eb0SDavid Howells * Checking i_size after the check allows us to calculate
296207073eb0SDavid Howells * the correct value for "nr", which means the zero-filled
296307073eb0SDavid Howells * part of the page is not copied back to userspace (unless
296407073eb0SDavid Howells * another truncate extends the file - this is desired though).
296507073eb0SDavid Howells */
2966c3722208SDavid Howells isize = i_size_read(in->f_mapping->host);
296707073eb0SDavid Howells if (unlikely(*ppos >= isize))
296807073eb0SDavid Howells break;
296907073eb0SDavid Howells end_offset = min_t(loff_t, isize, *ppos + len);
297007073eb0SDavid Howells
297107073eb0SDavid Howells /*
297207073eb0SDavid Howells * Once we start copying data, we don't want to be touching any
297307073eb0SDavid Howells * cachelines that might be contended:
297407073eb0SDavid Howells */
297507073eb0SDavid Howells writably_mapped = mapping_writably_mapped(in->f_mapping);
297607073eb0SDavid Howells
297707073eb0SDavid Howells for (i = 0; i < folio_batch_count(&fbatch); i++) {
297807073eb0SDavid Howells struct folio *folio = fbatch.folios[i];
297907073eb0SDavid Howells size_t n;
298007073eb0SDavid Howells
298107073eb0SDavid Howells if (folio_pos(folio) >= end_offset)
298207073eb0SDavid Howells goto out;
298307073eb0SDavid Howells folio_mark_accessed(folio);
298407073eb0SDavid Howells
298507073eb0SDavid Howells /*
298607073eb0SDavid Howells * If users can be writing to this folio using arbitrary
298707073eb0SDavid Howells * virtual addresses, take care of potential aliasing
298807073eb0SDavid Howells * before reading the folio on the kernel side.
298907073eb0SDavid Howells */
299007073eb0SDavid Howells if (writably_mapped)
299107073eb0SDavid Howells flush_dcache_folio(folio);
299207073eb0SDavid Howells
299307073eb0SDavid Howells n = min_t(loff_t, len, isize - *ppos);
299407073eb0SDavid Howells n = splice_folio_into_pipe(pipe, folio, *ppos, n);
299507073eb0SDavid Howells if (!n)
299607073eb0SDavid Howells goto out;
299707073eb0SDavid Howells len -= n;
299807073eb0SDavid Howells total_spliced += n;
299907073eb0SDavid Howells *ppos += n;
300007073eb0SDavid Howells in->f_ra.prev_pos = *ppos;
300107073eb0SDavid Howells if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
300207073eb0SDavid Howells goto out;
300307073eb0SDavid Howells }
300407073eb0SDavid Howells
300507073eb0SDavid Howells folio_batch_release(&fbatch);
300607073eb0SDavid Howells } while (len);
300707073eb0SDavid Howells
300807073eb0SDavid Howells out:
300907073eb0SDavid Howells folio_batch_release(&fbatch);
301007073eb0SDavid Howells file_accessed(in);
301107073eb0SDavid Howells
301207073eb0SDavid Howells return total_spliced ? total_spliced : error;
301307073eb0SDavid Howells }
30147c8e01ebSDavid Howells EXPORT_SYMBOL(filemap_splice_read);
301507073eb0SDavid Howells
folio_seek_hole_data(struct xa_state * xas,struct address_space * mapping,struct folio * folio,loff_t start,loff_t end,bool seek_data)3016f5e6429aSMatthew Wilcox (Oracle) static inline loff_t folio_seek_hole_data(struct xa_state *xas,
3017f5e6429aSMatthew Wilcox (Oracle) struct address_space *mapping, struct folio *folio,
301854fa39acSMatthew Wilcox (Oracle) loff_t start, loff_t end, bool seek_data)
301941139aa4SMatthew Wilcox (Oracle) {
302054fa39acSMatthew Wilcox (Oracle) const struct address_space_operations *ops = mapping->a_ops;
302154fa39acSMatthew Wilcox (Oracle) size_t offset, bsz = i_blocksize(mapping->host);
302254fa39acSMatthew Wilcox (Oracle)
3023f5e6429aSMatthew Wilcox (Oracle) if (xa_is_value(folio) || folio_test_uptodate(folio))
302454fa39acSMatthew Wilcox (Oracle) return seek_data ? start : end;
302554fa39acSMatthew Wilcox (Oracle) if (!ops->is_partially_uptodate)
302654fa39acSMatthew Wilcox (Oracle) return seek_data ? end : start;
302754fa39acSMatthew Wilcox (Oracle)
302854fa39acSMatthew Wilcox (Oracle) xas_pause(xas);
302954fa39acSMatthew Wilcox (Oracle) rcu_read_unlock();
3030f5e6429aSMatthew Wilcox (Oracle) folio_lock(folio);
3031f5e6429aSMatthew Wilcox (Oracle) if (unlikely(folio->mapping != mapping))
303254fa39acSMatthew Wilcox (Oracle) goto unlock;
303354fa39acSMatthew Wilcox (Oracle)
3034f5e6429aSMatthew Wilcox (Oracle) offset = offset_in_folio(folio, start) & ~(bsz - 1);
303554fa39acSMatthew Wilcox (Oracle)
303654fa39acSMatthew Wilcox (Oracle) do {
30372e7e80f7SMatthew Wilcox (Oracle) if (ops->is_partially_uptodate(folio, offset, bsz) ==
3038f5e6429aSMatthew Wilcox (Oracle) seek_data)
303954fa39acSMatthew Wilcox (Oracle) break;
304009528bb1SMarco Nelissen start = (start + bsz) & ~((u64)bsz - 1);
304154fa39acSMatthew Wilcox (Oracle) offset += bsz;
3042f5e6429aSMatthew Wilcox (Oracle) } while (offset < folio_size(folio));
304354fa39acSMatthew Wilcox (Oracle) unlock:
3044f5e6429aSMatthew Wilcox (Oracle) folio_unlock(folio);
304554fa39acSMatthew Wilcox (Oracle) rcu_read_lock();
304654fa39acSMatthew Wilcox (Oracle) return start;
304741139aa4SMatthew Wilcox (Oracle) }
304841139aa4SMatthew Wilcox (Oracle)
seek_folio_size(struct xa_state * xas,struct folio * folio)3049f5e6429aSMatthew Wilcox (Oracle) static inline size_t seek_folio_size(struct xa_state *xas, struct folio *folio)
305041139aa4SMatthew Wilcox (Oracle) {
3051f5e6429aSMatthew Wilcox (Oracle) if (xa_is_value(folio))
305241139aa4SMatthew Wilcox (Oracle) return PAGE_SIZE << xa_get_order(xas->xa, xas->xa_index);
3053f5e6429aSMatthew Wilcox (Oracle) return folio_size(folio);
305441139aa4SMatthew Wilcox (Oracle) }
305541139aa4SMatthew Wilcox (Oracle)
305641139aa4SMatthew Wilcox (Oracle) /**
305741139aa4SMatthew Wilcox (Oracle) * mapping_seek_hole_data - Seek for SEEK_DATA / SEEK_HOLE in the page cache.
305841139aa4SMatthew Wilcox (Oracle) * @mapping: Address space to search.
305941139aa4SMatthew Wilcox (Oracle) * @start: First byte to consider.
306041139aa4SMatthew Wilcox (Oracle) * @end: Limit of search (exclusive).
306141139aa4SMatthew Wilcox (Oracle) * @whence: Either SEEK_HOLE or SEEK_DATA.
306241139aa4SMatthew Wilcox (Oracle) *
306341139aa4SMatthew Wilcox (Oracle) * If the page cache knows which blocks contain holes and which blocks
306441139aa4SMatthew Wilcox (Oracle) * contain data, your filesystem can use this function to implement
306541139aa4SMatthew Wilcox (Oracle) * SEEK_HOLE and SEEK_DATA. This is useful for filesystems which are
306641139aa4SMatthew Wilcox (Oracle) * entirely memory-based such as tmpfs, and filesystems which support
306741139aa4SMatthew Wilcox (Oracle) * unwritten extents.
306841139aa4SMatthew Wilcox (Oracle) *
3069f0953a1bSIngo Molnar * Return: The requested offset on success, or -ENXIO if @whence specifies
307041139aa4SMatthew Wilcox (Oracle) * SEEK_DATA and there is no data after @start. There is an implicit hole
307141139aa4SMatthew Wilcox (Oracle) * after @end - 1, so SEEK_HOLE returns @end if all the bytes between @start
307241139aa4SMatthew Wilcox (Oracle) * and @end contain data.
307341139aa4SMatthew Wilcox (Oracle) */
mapping_seek_hole_data(struct address_space * mapping,loff_t start,loff_t end,int whence)307441139aa4SMatthew Wilcox (Oracle) loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start,
307541139aa4SMatthew Wilcox (Oracle) loff_t end, int whence)
307641139aa4SMatthew Wilcox (Oracle) {
307741139aa4SMatthew Wilcox (Oracle) XA_STATE(xas, &mapping->i_pages, start >> PAGE_SHIFT);
3078ed98b015SHugh Dickins pgoff_t max = (end - 1) >> PAGE_SHIFT;
307941139aa4SMatthew Wilcox (Oracle) bool seek_data = (whence == SEEK_DATA);
3080f5e6429aSMatthew Wilcox (Oracle) struct folio *folio;
308141139aa4SMatthew Wilcox (Oracle)
308241139aa4SMatthew Wilcox (Oracle) if (end <= start)
308341139aa4SMatthew Wilcox (Oracle) return -ENXIO;
308441139aa4SMatthew Wilcox (Oracle)
308541139aa4SMatthew Wilcox (Oracle) rcu_read_lock();
3086f5e6429aSMatthew Wilcox (Oracle) while ((folio = find_get_entry(&xas, max, XA_PRESENT))) {
3087ed98b015SHugh Dickins loff_t pos = (u64)xas.xa_index << PAGE_SHIFT;
3088f5e6429aSMatthew Wilcox (Oracle) size_t seek_size;
308941139aa4SMatthew Wilcox (Oracle)
309041139aa4SMatthew Wilcox (Oracle) if (start < pos) {
309141139aa4SMatthew Wilcox (Oracle) if (!seek_data)
309241139aa4SMatthew Wilcox (Oracle) goto unlock;
309341139aa4SMatthew Wilcox (Oracle) start = pos;
309441139aa4SMatthew Wilcox (Oracle) }
309541139aa4SMatthew Wilcox (Oracle)
3096f5e6429aSMatthew Wilcox (Oracle) seek_size = seek_folio_size(&xas, folio);
3097f5e6429aSMatthew Wilcox (Oracle) pos = round_up((u64)pos + 1, seek_size);
3098f5e6429aSMatthew Wilcox (Oracle) start = folio_seek_hole_data(&xas, mapping, folio, start, pos,
309954fa39acSMatthew Wilcox (Oracle) seek_data);
310054fa39acSMatthew Wilcox (Oracle) if (start < pos)
310141139aa4SMatthew Wilcox (Oracle) goto unlock;
3102ed98b015SHugh Dickins if (start >= end)
3103ed98b015SHugh Dickins break;
3104ed98b015SHugh Dickins if (seek_size > PAGE_SIZE)
3105ed98b015SHugh Dickins xas_set(&xas, pos >> PAGE_SHIFT);
3106f5e6429aSMatthew Wilcox (Oracle) if (!xa_is_value(folio))
3107f5e6429aSMatthew Wilcox (Oracle) folio_put(folio);
310841139aa4SMatthew Wilcox (Oracle) }
310941139aa4SMatthew Wilcox (Oracle) if (seek_data)
3110ed98b015SHugh Dickins start = -ENXIO;
311141139aa4SMatthew Wilcox (Oracle) unlock:
311241139aa4SMatthew Wilcox (Oracle) rcu_read_unlock();
3113f5e6429aSMatthew Wilcox (Oracle) if (folio && !xa_is_value(folio))
3114f5e6429aSMatthew Wilcox (Oracle) folio_put(folio);
311541139aa4SMatthew Wilcox (Oracle) if (start > end)
311641139aa4SMatthew Wilcox (Oracle) return end;
311741139aa4SMatthew Wilcox (Oracle) return start;
311841139aa4SMatthew Wilcox (Oracle) }
311941139aa4SMatthew Wilcox (Oracle)
31201da177e4SLinus Torvalds #ifdef CONFIG_MMU
31211da177e4SLinus Torvalds #define MMAP_LOTSAMISS (100)
31226b4c9f44SJosef Bacik /*
3123e292e6d6SMatthew Wilcox (Oracle) * lock_folio_maybe_drop_mmap - lock the page, possibly dropping the mmap_lock
31246b4c9f44SJosef Bacik * @vmf - the vm_fault for this fault.
3125e292e6d6SMatthew Wilcox (Oracle) * @folio - the folio to lock.
31266b4c9f44SJosef Bacik * @fpin - the pointer to the file we may pin (or is already pinned).
31276b4c9f44SJosef Bacik *
3128e292e6d6SMatthew Wilcox (Oracle) * This works similar to lock_folio_or_retry in that it can drop the
3129e292e6d6SMatthew Wilcox (Oracle) * mmap_lock. It differs in that it actually returns the folio locked
3130e292e6d6SMatthew Wilcox (Oracle) * if it returns 1 and 0 if it couldn't lock the folio. If we did have
3131e292e6d6SMatthew Wilcox (Oracle) * to drop the mmap_lock then fpin will point to the pinned file and
3132e292e6d6SMatthew Wilcox (Oracle) * needs to be fput()'ed at a later point.
31336b4c9f44SJosef Bacik */
lock_folio_maybe_drop_mmap(struct vm_fault * vmf,struct folio * folio,struct file ** fpin)3134e292e6d6SMatthew Wilcox (Oracle) static int lock_folio_maybe_drop_mmap(struct vm_fault *vmf, struct folio *folio,
31356b4c9f44SJosef Bacik struct file **fpin)
31366b4c9f44SJosef Bacik {
31377c23c782SMatthew Wilcox (Oracle) if (folio_trylock(folio))
31386b4c9f44SJosef Bacik return 1;
31396b4c9f44SJosef Bacik
31408b0f9fa2SLinus Torvalds /*
31418b0f9fa2SLinus Torvalds * NOTE! This will make us return with VM_FAULT_RETRY, but with
3142c1e8d7c6SMichel Lespinasse * the mmap_lock still held. That's how FAULT_FLAG_RETRY_NOWAIT
31438b0f9fa2SLinus Torvalds * is supposed to work. We have way too many special cases..
31448b0f9fa2SLinus Torvalds */
31456b4c9f44SJosef Bacik if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
31466b4c9f44SJosef Bacik return 0;
31476b4c9f44SJosef Bacik
31486b4c9f44SJosef Bacik *fpin = maybe_unlock_mmap_for_io(vmf, *fpin);
31496b4c9f44SJosef Bacik if (vmf->flags & FAULT_FLAG_KILLABLE) {
3150af7f29d9SMatthew Wilcox (Oracle) if (__folio_lock_killable(folio)) {
31516b4c9f44SJosef Bacik /*
3152c1e8d7c6SMichel Lespinasse * We didn't have the right flags to drop the mmap_lock,
31536b4c9f44SJosef Bacik * but all fault_handlers only check for fatal signals
31546b4c9f44SJosef Bacik * if we return VM_FAULT_RETRY, so we need to drop the
3155c1e8d7c6SMichel Lespinasse * mmap_lock here and return 0 if we don't have a fpin.
31566b4c9f44SJosef Bacik */
31576b4c9f44SJosef Bacik if (*fpin == NULL)
3158d8ed45c5SMichel Lespinasse mmap_read_unlock(vmf->vma->vm_mm);
31596b4c9f44SJosef Bacik return 0;
31606b4c9f44SJosef Bacik }
31616b4c9f44SJosef Bacik } else
31627c23c782SMatthew Wilcox (Oracle) __folio_lock(folio);
31637c23c782SMatthew Wilcox (Oracle)
31646b4c9f44SJosef Bacik return 1;
31656b4c9f44SJosef Bacik }
31666b4c9f44SJosef Bacik
31676b4c9f44SJosef Bacik /*
31686b4c9f44SJosef Bacik * Synchronous readahead happens when we don't even find a page in the page
31696b4c9f44SJosef Bacik * cache at all. We don't want to perform IO under the mmap sem, so if we have
31706b4c9f44SJosef Bacik * to drop the mmap sem we return the file that was pinned in order for us to do
31716b4c9f44SJosef Bacik * that. If we didn't pin a file then we return NULL. The file that is
31726b4c9f44SJosef Bacik * returned needs to be fput()'ed when we're done with it.
31736b4c9f44SJosef Bacik */
do_sync_mmap_readahead(struct vm_fault * vmf)31746b4c9f44SJosef Bacik static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
3175ef00e08eSLinus Torvalds {
31762a1180f1SJosef Bacik struct file *file = vmf->vma->vm_file;
31772a1180f1SJosef Bacik struct file_ra_state *ra = &file->f_ra;
3178ef00e08eSLinus Torvalds struct address_space *mapping = file->f_mapping;
3179fcd9ae4fSMatthew Wilcox (Oracle) DEFINE_READAHEAD(ractl, file, ra, mapping, vmf->pgoff);
31806b4c9f44SJosef Bacik struct file *fpin = NULL;
3181dcfa24baSMatthew Wilcox (Oracle) unsigned long vm_flags = vmf->vma->vm_flags;
3182e630bfacSKirill A. Shutemov unsigned int mmap_miss;
3183ef00e08eSLinus Torvalds
31844687fdbbSMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE
31854687fdbbSMatthew Wilcox (Oracle) /* Use the readahead code, even if readahead is disabled */
318606b5a69cSGavin Shan if ((vm_flags & VM_HUGEPAGE) && HPAGE_PMD_ORDER <= MAX_PAGECACHE_ORDER) {
31874687fdbbSMatthew Wilcox (Oracle) fpin = maybe_unlock_mmap_for_io(vmf, fpin);
31884687fdbbSMatthew Wilcox (Oracle) ractl._index &= ~((unsigned long)HPAGE_PMD_NR - 1);
31894687fdbbSMatthew Wilcox (Oracle) ra->size = HPAGE_PMD_NR;
31904687fdbbSMatthew Wilcox (Oracle) /*
31914687fdbbSMatthew Wilcox (Oracle) * Fetch two PMD folios, so we get the chance to actually
31924687fdbbSMatthew Wilcox (Oracle) * readahead, unless we've been told not to.
31934687fdbbSMatthew Wilcox (Oracle) */
3194dcfa24baSMatthew Wilcox (Oracle) if (!(vm_flags & VM_RAND_READ))
31954687fdbbSMatthew Wilcox (Oracle) ra->size *= 2;
31964687fdbbSMatthew Wilcox (Oracle) ra->async_size = HPAGE_PMD_NR;
31974687fdbbSMatthew Wilcox (Oracle) page_cache_ra_order(&ractl, ra, HPAGE_PMD_ORDER);
31984687fdbbSMatthew Wilcox (Oracle) return fpin;
31994687fdbbSMatthew Wilcox (Oracle) }
32004687fdbbSMatthew Wilcox (Oracle) #endif
32014687fdbbSMatthew Wilcox (Oracle)
3202ef00e08eSLinus Torvalds /* If we don't want any read-ahead, don't bother */
3203dcfa24baSMatthew Wilcox (Oracle) if (vm_flags & VM_RAND_READ)
32046b4c9f44SJosef Bacik return fpin;
3205275b12bfSWu Fengguang if (!ra->ra_pages)
32066b4c9f44SJosef Bacik return fpin;
3207ef00e08eSLinus Torvalds
3208dcfa24baSMatthew Wilcox (Oracle) if (vm_flags & VM_SEQ_READ) {
32096b4c9f44SJosef Bacik fpin = maybe_unlock_mmap_for_io(vmf, fpin);
3210fcd9ae4fSMatthew Wilcox (Oracle) page_cache_sync_ra(&ractl, ra->ra_pages);
32116b4c9f44SJosef Bacik return fpin;
3212ef00e08eSLinus Torvalds }
3213ef00e08eSLinus Torvalds
3214207d04baSAndi Kleen /* Avoid banging the cache line if not needed */
3215e630bfacSKirill A. Shutemov mmap_miss = READ_ONCE(ra->mmap_miss);
3216e630bfacSKirill A. Shutemov if (mmap_miss < MMAP_LOTSAMISS * 10)
3217e630bfacSKirill A. Shutemov WRITE_ONCE(ra->mmap_miss, ++mmap_miss);
3218ef00e08eSLinus Torvalds
3219ef00e08eSLinus Torvalds /*
3220ef00e08eSLinus Torvalds * Do we miss much more than hit in this file? If so,
3221ef00e08eSLinus Torvalds * stop bothering with read-ahead. It will only hurt.
3222ef00e08eSLinus Torvalds */
3223e630bfacSKirill A. Shutemov if (mmap_miss > MMAP_LOTSAMISS)
32246b4c9f44SJosef Bacik return fpin;
3225ef00e08eSLinus Torvalds
3226d30a1100SWu Fengguang /*
3227d30a1100SWu Fengguang * mmap read-around
3228d30a1100SWu Fengguang */
32296b4c9f44SJosef Bacik fpin = maybe_unlock_mmap_for_io(vmf, fpin);
3230db660d46SDavid Howells ra->start = max_t(long, 0, vmf->pgoff - ra->ra_pages / 2);
3231600e19afSRoman Gushchin ra->size = ra->ra_pages;
3232600e19afSRoman Gushchin ra->async_size = ra->ra_pages / 4;
3233db660d46SDavid Howells ractl._index = ra->start;
323456a4d67cSMatthew Wilcox (Oracle) page_cache_ra_order(&ractl, ra, 0);
32356b4c9f44SJosef Bacik return fpin;
3236ef00e08eSLinus Torvalds }
3237ef00e08eSLinus Torvalds
3238ef00e08eSLinus Torvalds /*
3239ef00e08eSLinus Torvalds * Asynchronous readahead happens when we find the page and PG_readahead,
32406b4c9f44SJosef Bacik * so we want to possibly extend the readahead further. We return the file that
3241c1e8d7c6SMichel Lespinasse * was pinned if we have to drop the mmap_lock in order to do IO.
3242ef00e08eSLinus Torvalds */
do_async_mmap_readahead(struct vm_fault * vmf,struct folio * folio)32436b4c9f44SJosef Bacik static struct file *do_async_mmap_readahead(struct vm_fault *vmf,
324479598cedSMatthew Wilcox (Oracle) struct folio *folio)
3245ef00e08eSLinus Torvalds {
32462a1180f1SJosef Bacik struct file *file = vmf->vma->vm_file;
32472a1180f1SJosef Bacik struct file_ra_state *ra = &file->f_ra;
324879598cedSMatthew Wilcox (Oracle) DEFINE_READAHEAD(ractl, file, ra, file->f_mapping, vmf->pgoff);
32496b4c9f44SJosef Bacik struct file *fpin = NULL;
3250e630bfacSKirill A. Shutemov unsigned int mmap_miss;
3251ef00e08eSLinus Torvalds
3252ef00e08eSLinus Torvalds /* If we don't want any read-ahead, don't bother */
32535c72feeeSJan Kara if (vmf->vma->vm_flags & VM_RAND_READ || !ra->ra_pages)
32546b4c9f44SJosef Bacik return fpin;
325579598cedSMatthew Wilcox (Oracle)
3256e630bfacSKirill A. Shutemov mmap_miss = READ_ONCE(ra->mmap_miss);
3257e630bfacSKirill A. Shutemov if (mmap_miss)
3258e630bfacSKirill A. Shutemov WRITE_ONCE(ra->mmap_miss, --mmap_miss);
325979598cedSMatthew Wilcox (Oracle)
326079598cedSMatthew Wilcox (Oracle) if (folio_test_readahead(folio)) {
32616b4c9f44SJosef Bacik fpin = maybe_unlock_mmap_for_io(vmf, fpin);
326279598cedSMatthew Wilcox (Oracle) page_cache_async_ra(&ractl, folio, ra->ra_pages);
3263ef00e08eSLinus Torvalds }
32646b4c9f44SJosef Bacik return fpin;
32656b4c9f44SJosef Bacik }
3266ef00e08eSLinus Torvalds
3267485bb99bSRandy Dunlap /**
326854cb8821SNick Piggin * filemap_fault - read in file data for page fault handling
3269d0217ac0SNick Piggin * @vmf: struct vm_fault containing details of the fault
3270485bb99bSRandy Dunlap *
327154cb8821SNick Piggin * filemap_fault() is invoked via the vma operations vector for a
32721da177e4SLinus Torvalds * mapped memory region to read in file data during a page fault.
32731da177e4SLinus Torvalds *
32741da177e4SLinus Torvalds * The goto's are kind of ugly, but this streamlines the normal case of having
32751da177e4SLinus Torvalds * it in the page cache, and handles the special cases reasonably without
32761da177e4SLinus Torvalds * having a lot of duplicated code.
32779a95f3cfSPaul Cassella *
3278c1e8d7c6SMichel Lespinasse * vma->vm_mm->mmap_lock must be held on entry.
32799a95f3cfSPaul Cassella *
3280c1e8d7c6SMichel Lespinasse * If our return value has VM_FAULT_RETRY set, it's because the mmap_lock
3281e292e6d6SMatthew Wilcox (Oracle) * may be dropped before doing I/O or by lock_folio_maybe_drop_mmap().
32829a95f3cfSPaul Cassella *
3283c1e8d7c6SMichel Lespinasse * If our return value does not have VM_FAULT_RETRY set, the mmap_lock
32849a95f3cfSPaul Cassella * has not been released.
32859a95f3cfSPaul Cassella *
32869a95f3cfSPaul Cassella * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
3287a862f68aSMike Rapoport *
3288a862f68aSMike Rapoport * Return: bitwise-OR of %VM_FAULT_ codes.
32891da177e4SLinus Torvalds */
filemap_fault(struct vm_fault * vmf)32902bcd6454SSouptick Joarder vm_fault_t filemap_fault(struct vm_fault *vmf)
32911da177e4SLinus Torvalds {
32921da177e4SLinus Torvalds int error;
329311bac800SDave Jiang struct file *file = vmf->vma->vm_file;
32946b4c9f44SJosef Bacik struct file *fpin = NULL;
32951da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping;
32961da177e4SLinus Torvalds struct inode *inode = mapping->host;
3297e292e6d6SMatthew Wilcox (Oracle) pgoff_t max_idx, index = vmf->pgoff;
3298e292e6d6SMatthew Wilcox (Oracle) struct folio *folio;
32992bcd6454SSouptick Joarder vm_fault_t ret = 0;
3300730633f0SJan Kara bool mapping_locked = false;
33011da177e4SLinus Torvalds
3302e292e6d6SMatthew Wilcox (Oracle) max_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3303e292e6d6SMatthew Wilcox (Oracle) if (unlikely(index >= max_idx))
33045307cc1aSLinus Torvalds return VM_FAULT_SIGBUS;
33051da177e4SLinus Torvalds
33061da177e4SLinus Torvalds /*
330749426420SJohannes Weiner * Do we have something in the page cache already?
33081da177e4SLinus Torvalds */
3309e292e6d6SMatthew Wilcox (Oracle) folio = filemap_get_folio(mapping, index);
331066dabbb6SChristoph Hellwig if (likely(!IS_ERR(folio))) {
33113ea89ee8SFengguang Wu /*
3312730633f0SJan Kara * We found the page, so try async readahead before waiting for
3313730633f0SJan Kara * the lock.
33143ea89ee8SFengguang Wu */
3315730633f0SJan Kara if (!(vmf->flags & FAULT_FLAG_TRIED))
331679598cedSMatthew Wilcox (Oracle) fpin = do_async_mmap_readahead(vmf, folio);
3317e292e6d6SMatthew Wilcox (Oracle) if (unlikely(!folio_test_uptodate(folio))) {
3318730633f0SJan Kara filemap_invalidate_lock_shared(mapping);
3319730633f0SJan Kara mapping_locked = true;
3320730633f0SJan Kara }
3321730633f0SJan Kara } else {
3322ef00e08eSLinus Torvalds /* No page in the page cache at all */
3323f8891e5eSChristoph Lameter count_vm_event(PGMAJFAULT);
33242262185cSRoman Gushchin count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
3325ef00e08eSLinus Torvalds ret = VM_FAULT_MAJOR;
33266b4c9f44SJosef Bacik fpin = do_sync_mmap_readahead(vmf);
3327ef00e08eSLinus Torvalds retry_find:
3328730633f0SJan Kara /*
3329e292e6d6SMatthew Wilcox (Oracle) * See comment in filemap_create_folio() why we need
3330730633f0SJan Kara * invalidate_lock
3331730633f0SJan Kara */
3332730633f0SJan Kara if (!mapping_locked) {
3333730633f0SJan Kara filemap_invalidate_lock_shared(mapping);
3334730633f0SJan Kara mapping_locked = true;
3335730633f0SJan Kara }
3336e292e6d6SMatthew Wilcox (Oracle) folio = __filemap_get_folio(mapping, index,
3337a75d4c33SJosef Bacik FGP_CREAT|FGP_FOR_MMAP,
3338a75d4c33SJosef Bacik vmf->gfp_mask);
333966dabbb6SChristoph Hellwig if (IS_ERR(folio)) {
33406b4c9f44SJosef Bacik if (fpin)
33416b4c9f44SJosef Bacik goto out_retry;
3342730633f0SJan Kara filemap_invalidate_unlock_shared(mapping);
3343e520e932SMatthew Wilcox (Oracle) return VM_FAULT_OOM;
33441da177e4SLinus Torvalds }
3345d88c0922SMichel Lespinasse }
3346b522c94dSMichel Lespinasse
3347e292e6d6SMatthew Wilcox (Oracle) if (!lock_folio_maybe_drop_mmap(vmf, folio, &fpin))
33486b4c9f44SJosef Bacik goto out_retry;
33496b4c9f44SJosef Bacik
3350b522c94dSMichel Lespinasse /* Did it get truncated? */
3351e292e6d6SMatthew Wilcox (Oracle) if (unlikely(folio->mapping != mapping)) {
3352e292e6d6SMatthew Wilcox (Oracle) folio_unlock(folio);
3353e292e6d6SMatthew Wilcox (Oracle) folio_put(folio);
3354b522c94dSMichel Lespinasse goto retry_find;
3355b522c94dSMichel Lespinasse }
3356e292e6d6SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_contains(folio, index), folio);
3357b522c94dSMichel Lespinasse
33581da177e4SLinus Torvalds /*
3359d00806b1SNick Piggin * We have a locked page in the page cache, now we need to check
3360d00806b1SNick Piggin * that it's up-to-date. If not, it is going to be due to an error.
33611da177e4SLinus Torvalds */
3362e292e6d6SMatthew Wilcox (Oracle) if (unlikely(!folio_test_uptodate(folio))) {
3363730633f0SJan Kara /*
3364730633f0SJan Kara * The page was in cache and uptodate and now it is not.
3365730633f0SJan Kara * Strange but possible since we didn't hold the page lock all
3366730633f0SJan Kara * the time. Let's drop everything get the invalidate lock and
3367730633f0SJan Kara * try again.
3368730633f0SJan Kara */
3369730633f0SJan Kara if (!mapping_locked) {
3370e292e6d6SMatthew Wilcox (Oracle) folio_unlock(folio);
3371e292e6d6SMatthew Wilcox (Oracle) folio_put(folio);
3372730633f0SJan Kara goto retry_find;
3373730633f0SJan Kara }
33741da177e4SLinus Torvalds goto page_not_uptodate;
3375730633f0SJan Kara }
33761da177e4SLinus Torvalds
3377ef00e08eSLinus Torvalds /*
3378c1e8d7c6SMichel Lespinasse * We've made it this far and we had to drop our mmap_lock, now is the
33796b4c9f44SJosef Bacik * time to return to the upper layer and have it re-find the vma and
33806b4c9f44SJosef Bacik * redo the fault.
33816b4c9f44SJosef Bacik */
33826b4c9f44SJosef Bacik if (fpin) {
3383e292e6d6SMatthew Wilcox (Oracle) folio_unlock(folio);
33846b4c9f44SJosef Bacik goto out_retry;
33856b4c9f44SJosef Bacik }
3386730633f0SJan Kara if (mapping_locked)
3387730633f0SJan Kara filemap_invalidate_unlock_shared(mapping);
33886b4c9f44SJosef Bacik
33896b4c9f44SJosef Bacik /*
3390ef00e08eSLinus Torvalds * Found the page and have a reference on it.
3391ef00e08eSLinus Torvalds * We must recheck i_size under page lock.
3392ef00e08eSLinus Torvalds */
3393e292e6d6SMatthew Wilcox (Oracle) max_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
3394e292e6d6SMatthew Wilcox (Oracle) if (unlikely(index >= max_idx)) {
3395e292e6d6SMatthew Wilcox (Oracle) folio_unlock(folio);
3396e292e6d6SMatthew Wilcox (Oracle) folio_put(folio);
33975307cc1aSLinus Torvalds return VM_FAULT_SIGBUS;
3398d00806b1SNick Piggin }
3399d00806b1SNick Piggin
3400e292e6d6SMatthew Wilcox (Oracle) vmf->page = folio_file_page(folio, index);
340183c54070SNick Piggin return ret | VM_FAULT_LOCKED;
34021da177e4SLinus Torvalds
34031da177e4SLinus Torvalds page_not_uptodate:
34041da177e4SLinus Torvalds /*
34051da177e4SLinus Torvalds * Umm, take care of errors if the page isn't up-to-date.
34061da177e4SLinus Torvalds * Try to re-read it _once_. We do this synchronously,
34071da177e4SLinus Torvalds * because there really aren't any performance issues here
34081da177e4SLinus Torvalds * and we need to check for errors.
34091da177e4SLinus Torvalds */
34106b4c9f44SJosef Bacik fpin = maybe_unlock_mmap_for_io(vmf, fpin);
3411290e1a32SMatthew Wilcox (Oracle) error = filemap_read_folio(file, mapping->a_ops->read_folio, folio);
34126b4c9f44SJosef Bacik if (fpin)
34136b4c9f44SJosef Bacik goto out_retry;
3414e292e6d6SMatthew Wilcox (Oracle) folio_put(folio);
34151da177e4SLinus Torvalds
3416d00806b1SNick Piggin if (!error || error == AOP_TRUNCATED_PAGE)
3417d00806b1SNick Piggin goto retry_find;
3418730633f0SJan Kara filemap_invalidate_unlock_shared(mapping);
3419d00806b1SNick Piggin
3420d0217ac0SNick Piggin return VM_FAULT_SIGBUS;
34216b4c9f44SJosef Bacik
34226b4c9f44SJosef Bacik out_retry:
34236b4c9f44SJosef Bacik /*
3424c1e8d7c6SMichel Lespinasse * We dropped the mmap_lock, we need to return to the fault handler to
34256b4c9f44SJosef Bacik * re-find the vma and come back and find our hopefully still populated
34266b4c9f44SJosef Bacik * page.
34276b4c9f44SJosef Bacik */
342838a55db9SMatthew Wilcox if (!IS_ERR(folio))
3429e292e6d6SMatthew Wilcox (Oracle) folio_put(folio);
3430730633f0SJan Kara if (mapping_locked)
3431730633f0SJan Kara filemap_invalidate_unlock_shared(mapping);
34326b4c9f44SJosef Bacik if (fpin)
34336b4c9f44SJosef Bacik fput(fpin);
34346b4c9f44SJosef Bacik return ret | VM_FAULT_RETRY;
343554cb8821SNick Piggin }
343654cb8821SNick Piggin EXPORT_SYMBOL(filemap_fault);
343754cb8821SNick Piggin
filemap_map_pmd(struct vm_fault * vmf,struct folio * folio,pgoff_t start)34388808ecabSMatthew Wilcox (Oracle) static bool filemap_map_pmd(struct vm_fault *vmf, struct folio *folio,
34398808ecabSMatthew Wilcox (Oracle) pgoff_t start)
3440f9ce0be7SKirill A. Shutemov {
3441f9ce0be7SKirill A. Shutemov struct mm_struct *mm = vmf->vma->vm_mm;
3442f9ce0be7SKirill A. Shutemov
3443f9ce0be7SKirill A. Shutemov /* Huge page is mapped? No need to proceed. */
3444f9ce0be7SKirill A. Shutemov if (pmd_trans_huge(*vmf->pmd)) {
34458808ecabSMatthew Wilcox (Oracle) folio_unlock(folio);
34468808ecabSMatthew Wilcox (Oracle) folio_put(folio);
3447f9ce0be7SKirill A. Shutemov return true;
3448f9ce0be7SKirill A. Shutemov }
3449f9ce0be7SKirill A. Shutemov
34508808ecabSMatthew Wilcox (Oracle) if (pmd_none(*vmf->pmd) && folio_test_pmd_mappable(folio)) {
34518808ecabSMatthew Wilcox (Oracle) struct page *page = folio_file_page(folio, start);
3452f9ce0be7SKirill A. Shutemov vm_fault_t ret = do_set_pmd(vmf, page);
3453f9ce0be7SKirill A. Shutemov if (!ret) {
3454f9ce0be7SKirill A. Shutemov /* The page is mapped successfully, reference consumed. */
34558808ecabSMatthew Wilcox (Oracle) folio_unlock(folio);
3456f9ce0be7SKirill A. Shutemov return true;
3457f9ce0be7SKirill A. Shutemov }
3458f9ce0be7SKirill A. Shutemov }
3459f9ce0be7SKirill A. Shutemov
3460799f90c3SHugh Dickins if (pmd_none(*vmf->pmd) && vmf->prealloc_pte)
346103c4f204SQi Zheng pmd_install(mm, vmf->pmd, &vmf->prealloc_pte);
3462f9ce0be7SKirill A. Shutemov
3463f9ce0be7SKirill A. Shutemov return false;
3464f9ce0be7SKirill A. Shutemov }
3465f9ce0be7SKirill A. Shutemov
next_uptodate_folio(struct xa_state * xas,struct address_space * mapping,pgoff_t end_pgoff)3466de74976eSYin Fengwei static struct folio *next_uptodate_folio(struct xa_state *xas,
3467de74976eSYin Fengwei struct address_space *mapping, pgoff_t end_pgoff)
3468f9ce0be7SKirill A. Shutemov {
3469de74976eSYin Fengwei struct folio *folio = xas_next_entry(xas, end_pgoff);
3470f9ce0be7SKirill A. Shutemov unsigned long max_idx;
3471f9ce0be7SKirill A. Shutemov
3472f9ce0be7SKirill A. Shutemov do {
34739184a307SMatthew Wilcox (Oracle) if (!folio)
3474f9ce0be7SKirill A. Shutemov return NULL;
34759184a307SMatthew Wilcox (Oracle) if (xas_retry(xas, folio))
3476f9ce0be7SKirill A. Shutemov continue;
34779184a307SMatthew Wilcox (Oracle) if (xa_is_value(folio))
3478f9ce0be7SKirill A. Shutemov continue;
34799184a307SMatthew Wilcox (Oracle) if (folio_test_locked(folio))
3480f9ce0be7SKirill A. Shutemov continue;
348116380f52SYang Shi if (!folio_try_get(folio))
3482f9ce0be7SKirill A. Shutemov continue;
3483f9ce0be7SKirill A. Shutemov /* Has the page moved or been split? */
34849184a307SMatthew Wilcox (Oracle) if (unlikely(folio != xas_reload(xas)))
3485f9ce0be7SKirill A. Shutemov goto skip;
34869184a307SMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio) || folio_test_readahead(folio))
3487f9ce0be7SKirill A. Shutemov goto skip;
34889184a307SMatthew Wilcox (Oracle) if (!folio_trylock(folio))
3489f9ce0be7SKirill A. Shutemov goto skip;
34909184a307SMatthew Wilcox (Oracle) if (folio->mapping != mapping)
3491f9ce0be7SKirill A. Shutemov goto unlock;
34929184a307SMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio))
3493f9ce0be7SKirill A. Shutemov goto unlock;
3494f9ce0be7SKirill A. Shutemov max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
3495f9ce0be7SKirill A. Shutemov if (xas->xa_index >= max_idx)
3496f9ce0be7SKirill A. Shutemov goto unlock;
3497820b05e9SMatthew Wilcox (Oracle) return folio;
3498f9ce0be7SKirill A. Shutemov unlock:
34999184a307SMatthew Wilcox (Oracle) folio_unlock(folio);
3500f9ce0be7SKirill A. Shutemov skip:
35019184a307SMatthew Wilcox (Oracle) folio_put(folio);
35029184a307SMatthew Wilcox (Oracle) } while ((folio = xas_next_entry(xas, end_pgoff)) != NULL);
3503f9ce0be7SKirill A. Shutemov
3504f9ce0be7SKirill A. Shutemov return NULL;
3505f9ce0be7SKirill A. Shutemov }
3506f9ce0be7SKirill A. Shutemov
3507de74976eSYin Fengwei /*
3508de74976eSYin Fengwei * Map page range [start_page, start_page + nr_pages) of folio.
3509de74976eSYin Fengwei * start_page is gotten from start by folio_page(folio, start)
3510de74976eSYin Fengwei */
filemap_map_folio_range(struct vm_fault * vmf,struct folio * folio,unsigned long start,unsigned long addr,unsigned int nr_pages,unsigned int * mmap_miss)3511de74976eSYin Fengwei static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
3512de74976eSYin Fengwei struct folio *folio, unsigned long start,
3513c8be0380SYin Fengwei unsigned long addr, unsigned int nr_pages,
3514c8be0380SYin Fengwei unsigned int *mmap_miss)
3515f9ce0be7SKirill A. Shutemov {
3516de74976eSYin Fengwei vm_fault_t ret = 0;
3517de74976eSYin Fengwei struct page *page = folio_page(folio, start);
3518617c28ecSYin Fengwei unsigned int count = 0;
3519617c28ecSYin Fengwei pte_t *old_ptep = vmf->pte;
3520f9ce0be7SKirill A. Shutemov
3521de74976eSYin Fengwei do {
3522617c28ecSYin Fengwei if (PageHWPoison(page + count))
3523617c28ecSYin Fengwei goto skip;
3524de74976eSYin Fengwei
3525c8be0380SYin Fengwei (*mmap_miss)++;
3526de74976eSYin Fengwei
3527de74976eSYin Fengwei /*
3528de74976eSYin Fengwei * NOTE: If there're PTE markers, we'll leave them to be
3529de74976eSYin Fengwei * handled in the specific fault path, and it'll prohibit the
3530de74976eSYin Fengwei * fault-around logic.
3531de74976eSYin Fengwei */
3532617c28ecSYin Fengwei if (!pte_none(vmf->pte[count]))
3533617c28ecSYin Fengwei goto skip;
3534617c28ecSYin Fengwei
3535617c28ecSYin Fengwei count++;
3536de74976eSYin Fengwei continue;
3537617c28ecSYin Fengwei skip:
3538617c28ecSYin Fengwei if (count) {
3539617c28ecSYin Fengwei set_pte_range(vmf, folio, page, count, addr);
3540617c28ecSYin Fengwei folio_ref_add(folio, count);
3541a501a070SMatthew Wilcox (Oracle) if (in_range(vmf->address, addr, count * PAGE_SIZE))
3542de74976eSYin Fengwei ret = VM_FAULT_NOPAGE;
3543f9ce0be7SKirill A. Shutemov }
3544f9ce0be7SKirill A. Shutemov
3545617c28ecSYin Fengwei count++;
3546617c28ecSYin Fengwei page += count;
3547617c28ecSYin Fengwei vmf->pte += count;
3548617c28ecSYin Fengwei addr += count * PAGE_SIZE;
3549617c28ecSYin Fengwei count = 0;
3550617c28ecSYin Fengwei } while (--nr_pages > 0);
3551de74976eSYin Fengwei
3552617c28ecSYin Fengwei if (count) {
3553617c28ecSYin Fengwei set_pte_range(vmf, folio, page, count, addr);
3554617c28ecSYin Fengwei folio_ref_add(folio, count);
3555a501a070SMatthew Wilcox (Oracle) if (in_range(vmf->address, addr, count * PAGE_SIZE))
3556617c28ecSYin Fengwei ret = VM_FAULT_NOPAGE;
3557617c28ecSYin Fengwei }
3558de74976eSYin Fengwei
3559617c28ecSYin Fengwei vmf->pte = old_ptep;
3560c8be0380SYin Fengwei
3561c8be0380SYin Fengwei return ret;
3562c8be0380SYin Fengwei }
3563c8be0380SYin Fengwei
filemap_map_order0_folio(struct vm_fault * vmf,struct folio * folio,unsigned long addr,unsigned int * mmap_miss)3564c8be0380SYin Fengwei static vm_fault_t filemap_map_order0_folio(struct vm_fault *vmf,
3565c8be0380SYin Fengwei struct folio *folio, unsigned long addr,
3566c8be0380SYin Fengwei unsigned int *mmap_miss)
3567c8be0380SYin Fengwei {
3568c8be0380SYin Fengwei vm_fault_t ret = 0;
3569c8be0380SYin Fengwei struct page *page = &folio->page;
3570c8be0380SYin Fengwei
3571c8be0380SYin Fengwei if (PageHWPoison(page))
3572c8be0380SYin Fengwei return ret;
3573c8be0380SYin Fengwei
3574c8be0380SYin Fengwei (*mmap_miss)++;
3575c8be0380SYin Fengwei
3576c8be0380SYin Fengwei /*
3577c8be0380SYin Fengwei * NOTE: If there're PTE markers, we'll leave them to be
3578c8be0380SYin Fengwei * handled in the specific fault path, and it'll prohibit
3579c8be0380SYin Fengwei * the fault-around logic.
3580c8be0380SYin Fengwei */
3581c8be0380SYin Fengwei if (!pte_none(ptep_get(vmf->pte)))
3582c8be0380SYin Fengwei return ret;
3583c8be0380SYin Fengwei
3584c8be0380SYin Fengwei if (vmf->address == addr)
3585c8be0380SYin Fengwei ret = VM_FAULT_NOPAGE;
3586c8be0380SYin Fengwei
3587c8be0380SYin Fengwei set_pte_range(vmf, folio, page, 1, addr);
3588c8be0380SYin Fengwei folio_ref_inc(folio);
3589de74976eSYin Fengwei
3590de74976eSYin Fengwei return ret;
3591f9ce0be7SKirill A. Shutemov }
3592f9ce0be7SKirill A. Shutemov
filemap_map_pages(struct vm_fault * vmf,pgoff_t start_pgoff,pgoff_t end_pgoff)3593f9ce0be7SKirill A. Shutemov vm_fault_t filemap_map_pages(struct vm_fault *vmf,
3594bae473a4SKirill A. Shutemov pgoff_t start_pgoff, pgoff_t end_pgoff)
3595f1820361SKirill A. Shutemov {
3596f9ce0be7SKirill A. Shutemov struct vm_area_struct *vma = vmf->vma;
3597f9ce0be7SKirill A. Shutemov struct file *file = vma->vm_file;
3598f1820361SKirill A. Shutemov struct address_space *mapping = file->f_mapping;
3599bae473a4SKirill A. Shutemov pgoff_t last_pgoff = start_pgoff;
36009d3af4b4SWill Deacon unsigned long addr;
3601070e807cSMatthew Wilcox XA_STATE(xas, &mapping->i_pages, start_pgoff);
3602820b05e9SMatthew Wilcox (Oracle) struct folio *folio;
3603f9ce0be7SKirill A. Shutemov vm_fault_t ret = 0;
3604c8be0380SYin Fengwei unsigned int nr_pages = 0, mmap_miss = 0, mmap_miss_saved;
3605f1820361SKirill A. Shutemov
3606f1820361SKirill A. Shutemov rcu_read_lock();
3607de74976eSYin Fengwei folio = next_uptodate_folio(&xas, mapping, end_pgoff);
3608820b05e9SMatthew Wilcox (Oracle) if (!folio)
3609f9ce0be7SKirill A. Shutemov goto out;
3610f1820361SKirill A. Shutemov
36118808ecabSMatthew Wilcox (Oracle) if (filemap_map_pmd(vmf, folio, start_pgoff)) {
3612f9ce0be7SKirill A. Shutemov ret = VM_FAULT_NOPAGE;
3613f9ce0be7SKirill A. Shutemov goto out;
3614f9ce0be7SKirill A. Shutemov }
3615f1820361SKirill A. Shutemov
36169d3af4b4SWill Deacon addr = vma->vm_start + ((start_pgoff - vma->vm_pgoff) << PAGE_SHIFT);
36179d3af4b4SWill Deacon vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, addr, &vmf->ptl);
361865747aafSHugh Dickins if (!vmf->pte) {
361965747aafSHugh Dickins folio_unlock(folio);
362065747aafSHugh Dickins folio_put(folio);
362165747aafSHugh Dickins goto out;
362265747aafSHugh Dickins }
3623f9ce0be7SKirill A. Shutemov do {
3624de74976eSYin Fengwei unsigned long end;
36257267ec00SKirill A. Shutemov
36269d3af4b4SWill Deacon addr += (xas.xa_index - last_pgoff) << PAGE_SHIFT;
3627070e807cSMatthew Wilcox vmf->pte += xas.xa_index - last_pgoff;
3628070e807cSMatthew Wilcox last_pgoff = xas.xa_index;
3629de74976eSYin Fengwei end = folio->index + folio_nr_pages(folio) - 1;
3630de74976eSYin Fengwei nr_pages = min(end, end_pgoff) - xas.xa_index + 1;
3631f9ce0be7SKirill A. Shutemov
3632c8be0380SYin Fengwei if (!folio_test_large(folio))
3633c8be0380SYin Fengwei ret |= filemap_map_order0_folio(vmf,
3634c8be0380SYin Fengwei folio, addr, &mmap_miss);
3635c8be0380SYin Fengwei else
3636de74976eSYin Fengwei ret |= filemap_map_folio_range(vmf, folio,
3637c8be0380SYin Fengwei xas.xa_index - folio->index, addr,
3638c8be0380SYin Fengwei nr_pages, &mmap_miss);
363946bdb427SWill Deacon
3640820b05e9SMatthew Wilcox (Oracle) folio_unlock(folio);
3641820b05e9SMatthew Wilcox (Oracle) folio_put(folio);
3642c8be0380SYin Fengwei } while ((folio = next_uptodate_folio(&xas, mapping, end_pgoff)) != NULL);
3643f9ce0be7SKirill A. Shutemov pte_unmap_unlock(vmf->pte, vmf->ptl);
3644f9ce0be7SKirill A. Shutemov out:
3645f1820361SKirill A. Shutemov rcu_read_unlock();
3646c8be0380SYin Fengwei
3647c8be0380SYin Fengwei mmap_miss_saved = READ_ONCE(file->f_ra.mmap_miss);
3648c8be0380SYin Fengwei if (mmap_miss >= mmap_miss_saved)
3649c8be0380SYin Fengwei WRITE_ONCE(file->f_ra.mmap_miss, 0);
3650c8be0380SYin Fengwei else
3651c8be0380SYin Fengwei WRITE_ONCE(file->f_ra.mmap_miss, mmap_miss_saved - mmap_miss);
3652c8be0380SYin Fengwei
3653f9ce0be7SKirill A. Shutemov return ret;
3654f1820361SKirill A. Shutemov }
3655f1820361SKirill A. Shutemov EXPORT_SYMBOL(filemap_map_pages);
3656f1820361SKirill A. Shutemov
filemap_page_mkwrite(struct vm_fault * vmf)36572bcd6454SSouptick Joarder vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
36584fcf1c62SJan Kara {
36595df1a672SChristoph Hellwig struct address_space *mapping = vmf->vma->vm_file->f_mapping;
3660960ea971SMatthew Wilcox (Oracle) struct folio *folio = page_folio(vmf->page);
36612bcd6454SSouptick Joarder vm_fault_t ret = VM_FAULT_LOCKED;
36624fcf1c62SJan Kara
36635df1a672SChristoph Hellwig sb_start_pagefault(mapping->host->i_sb);
366411bac800SDave Jiang file_update_time(vmf->vma->vm_file);
3665960ea971SMatthew Wilcox (Oracle) folio_lock(folio);
3666960ea971SMatthew Wilcox (Oracle) if (folio->mapping != mapping) {
3667960ea971SMatthew Wilcox (Oracle) folio_unlock(folio);
36684fcf1c62SJan Kara ret = VM_FAULT_NOPAGE;
36694fcf1c62SJan Kara goto out;
36704fcf1c62SJan Kara }
367114da9200SJan Kara /*
3672960ea971SMatthew Wilcox (Oracle) * We mark the folio dirty already here so that when freeze is in
367314da9200SJan Kara * progress, we are guaranteed that writeback during freezing will
3674960ea971SMatthew Wilcox (Oracle) * see the dirty folio and writeprotect it again.
367514da9200SJan Kara */
3676960ea971SMatthew Wilcox (Oracle) folio_mark_dirty(folio);
3677960ea971SMatthew Wilcox (Oracle) folio_wait_stable(folio);
36784fcf1c62SJan Kara out:
36795df1a672SChristoph Hellwig sb_end_pagefault(mapping->host->i_sb);
36804fcf1c62SJan Kara return ret;
36814fcf1c62SJan Kara }
36824fcf1c62SJan Kara
3683f0f37e2fSAlexey Dobriyan const struct vm_operations_struct generic_file_vm_ops = {
368454cb8821SNick Piggin .fault = filemap_fault,
3685f1820361SKirill A. Shutemov .map_pages = filemap_map_pages,
36864fcf1c62SJan Kara .page_mkwrite = filemap_page_mkwrite,
36871da177e4SLinus Torvalds };
36881da177e4SLinus Torvalds
36891da177e4SLinus Torvalds /* This is used for a general mmap of a disk file */
36901da177e4SLinus Torvalds
generic_file_mmap(struct file * file,struct vm_area_struct * vma)36911da177e4SLinus Torvalds int generic_file_mmap(struct file *file, struct vm_area_struct *vma)
36921da177e4SLinus Torvalds {
36931da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping;
36941da177e4SLinus Torvalds
36957e0a1265SMatthew Wilcox (Oracle) if (!mapping->a_ops->read_folio)
36961da177e4SLinus Torvalds return -ENOEXEC;
36971da177e4SLinus Torvalds file_accessed(file);
36981da177e4SLinus Torvalds vma->vm_ops = &generic_file_vm_ops;
36991da177e4SLinus Torvalds return 0;
37001da177e4SLinus Torvalds }
37011da177e4SLinus Torvalds
37021da177e4SLinus Torvalds /*
37031da177e4SLinus Torvalds * This is for filesystems which do not implement ->writepage.
37041da177e4SLinus Torvalds */
generic_file_readonly_mmap(struct file * file,struct vm_area_struct * vma)37051da177e4SLinus Torvalds int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
37061da177e4SLinus Torvalds {
37071da177e4SLinus Torvalds if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
37081da177e4SLinus Torvalds return -EINVAL;
37091da177e4SLinus Torvalds return generic_file_mmap(file, vma);
37101da177e4SLinus Torvalds }
37111da177e4SLinus Torvalds #else
filemap_page_mkwrite(struct vm_fault * vmf)37124b96a37dSSouptick Joarder vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
371345397228SArnd Bergmann {
37144b96a37dSSouptick Joarder return VM_FAULT_SIGBUS;
371545397228SArnd Bergmann }
generic_file_mmap(struct file * file,struct vm_area_struct * vma)37161da177e4SLinus Torvalds int generic_file_mmap(struct file *file, struct vm_area_struct *vma)
37171da177e4SLinus Torvalds {
37181da177e4SLinus Torvalds return -ENOSYS;
37191da177e4SLinus Torvalds }
generic_file_readonly_mmap(struct file * file,struct vm_area_struct * vma)37201da177e4SLinus Torvalds int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
37211da177e4SLinus Torvalds {
37221da177e4SLinus Torvalds return -ENOSYS;
37231da177e4SLinus Torvalds }
37241da177e4SLinus Torvalds #endif /* CONFIG_MMU */
37251da177e4SLinus Torvalds
372645397228SArnd Bergmann EXPORT_SYMBOL(filemap_page_mkwrite);
37271da177e4SLinus Torvalds EXPORT_SYMBOL(generic_file_mmap);
37281da177e4SLinus Torvalds EXPORT_SYMBOL(generic_file_readonly_mmap);
37291da177e4SLinus Torvalds
do_read_cache_folio(struct address_space * mapping,pgoff_t index,filler_t filler,struct file * file,gfp_t gfp)3730539a3322SMatthew Wilcox (Oracle) static struct folio *do_read_cache_folio(struct address_space *mapping,
3731e9b5b23eSMatthew Wilcox (Oracle) pgoff_t index, filler_t filler, struct file *file, gfp_t gfp)
373267f9fd91SSasha Levin {
3733539a3322SMatthew Wilcox (Oracle) struct folio *folio;
37341da177e4SLinus Torvalds int err;
373507950008SMatthew Wilcox (Oracle)
373607950008SMatthew Wilcox (Oracle) if (!filler)
373707950008SMatthew Wilcox (Oracle) filler = mapping->a_ops->read_folio;
37381da177e4SLinus Torvalds repeat:
3739539a3322SMatthew Wilcox (Oracle) folio = filemap_get_folio(mapping, index);
374066dabbb6SChristoph Hellwig if (IS_ERR(folio)) {
3741539a3322SMatthew Wilcox (Oracle) folio = filemap_alloc_folio(gfp, 0);
3742539a3322SMatthew Wilcox (Oracle) if (!folio)
37431da177e4SLinus Torvalds return ERR_PTR(-ENOMEM);
3744539a3322SMatthew Wilcox (Oracle) err = filemap_add_folio(mapping, folio, index, gfp);
3745eb2be189SNick Piggin if (unlikely(err)) {
3746539a3322SMatthew Wilcox (Oracle) folio_put(folio);
37471da177e4SLinus Torvalds if (err == -EEXIST)
37481da177e4SLinus Torvalds goto repeat;
374922ecdb4fSMatthew Wilcox /* Presumably ENOMEM for xarray node */
37501da177e4SLinus Torvalds return ERR_PTR(err);
37511da177e4SLinus Torvalds }
375232b63529SMel Gorman
37539bc3e869SMatthew Wilcox (Oracle) goto filler;
375432b63529SMel Gorman }
3755539a3322SMatthew Wilcox (Oracle) if (folio_test_uptodate(folio))
37561da177e4SLinus Torvalds goto out;
37571da177e4SLinus Torvalds
375881f4c03bSMatthew Wilcox (Oracle) if (!folio_trylock(folio)) {
375981f4c03bSMatthew Wilcox (Oracle) folio_put_wait_locked(folio, TASK_UNINTERRUPTIBLE);
376081f4c03bSMatthew Wilcox (Oracle) goto repeat;
376181f4c03bSMatthew Wilcox (Oracle) }
3762ebded027SMel Gorman
376381f4c03bSMatthew Wilcox (Oracle) /* Folio was truncated from mapping */
3764539a3322SMatthew Wilcox (Oracle) if (!folio->mapping) {
3765539a3322SMatthew Wilcox (Oracle) folio_unlock(folio);
3766539a3322SMatthew Wilcox (Oracle) folio_put(folio);
376732b63529SMel Gorman goto repeat;
37681da177e4SLinus Torvalds }
3769ebded027SMel Gorman
3770ebded027SMel Gorman /* Someone else locked and filled the page in a very small window */
3771539a3322SMatthew Wilcox (Oracle) if (folio_test_uptodate(folio)) {
3772539a3322SMatthew Wilcox (Oracle) folio_unlock(folio);
37731da177e4SLinus Torvalds goto out;
37741da177e4SLinus Torvalds }
3775faffdfa0SXianting Tian
37769bc3e869SMatthew Wilcox (Oracle) filler:
3777290e1a32SMatthew Wilcox (Oracle) err = filemap_read_folio(file, filler, folio);
37781dfa24a4SMatthew Wilcox (Oracle) if (err) {
37799bc3e869SMatthew Wilcox (Oracle) folio_put(folio);
37801dfa24a4SMatthew Wilcox (Oracle) if (err == AOP_TRUNCATED_PAGE)
37811dfa24a4SMatthew Wilcox (Oracle) goto repeat;
37829bc3e869SMatthew Wilcox (Oracle) return ERR_PTR(err);
37839bc3e869SMatthew Wilcox (Oracle) }
378432b63529SMel Gorman
37851da177e4SLinus Torvalds out:
3786539a3322SMatthew Wilcox (Oracle) folio_mark_accessed(folio);
3787539a3322SMatthew Wilcox (Oracle) return folio;
37886fe6900eSNick Piggin }
37890531b2aaSLinus Torvalds
37900531b2aaSLinus Torvalds /**
3791e9b5b23eSMatthew Wilcox (Oracle) * read_cache_folio - Read into page cache, fill it if needed.
3792e9b5b23eSMatthew Wilcox (Oracle) * @mapping: The address_space to read from.
3793e9b5b23eSMatthew Wilcox (Oracle) * @index: The index to read.
3794e9b5b23eSMatthew Wilcox (Oracle) * @filler: Function to perform the read, or NULL to use aops->read_folio().
3795e9b5b23eSMatthew Wilcox (Oracle) * @file: Passed to filler function, may be NULL if not required.
37960531b2aaSLinus Torvalds *
3797e9b5b23eSMatthew Wilcox (Oracle) * Read one page into the page cache. If it succeeds, the folio returned
3798e9b5b23eSMatthew Wilcox (Oracle) * will contain @index, but it may not be the first page of the folio.
37990531b2aaSLinus Torvalds *
3800e9b5b23eSMatthew Wilcox (Oracle) * If the filler function returns an error, it will be returned to the
3801e9b5b23eSMatthew Wilcox (Oracle) * caller.
3802a862f68aSMike Rapoport *
3803e9b5b23eSMatthew Wilcox (Oracle) * Context: May sleep. Expects mapping->invalidate_lock to be held.
3804e9b5b23eSMatthew Wilcox (Oracle) * Return: An uptodate folio on success, ERR_PTR() on failure.
38050531b2aaSLinus Torvalds */
read_cache_folio(struct address_space * mapping,pgoff_t index,filler_t filler,struct file * file)3806539a3322SMatthew Wilcox (Oracle) struct folio *read_cache_folio(struct address_space *mapping, pgoff_t index,
3807e9b5b23eSMatthew Wilcox (Oracle) filler_t filler, struct file *file)
3808539a3322SMatthew Wilcox (Oracle) {
3809e9b5b23eSMatthew Wilcox (Oracle) return do_read_cache_folio(mapping, index, filler, file,
3810539a3322SMatthew Wilcox (Oracle) mapping_gfp_mask(mapping));
3811539a3322SMatthew Wilcox (Oracle) }
3812539a3322SMatthew Wilcox (Oracle) EXPORT_SYMBOL(read_cache_folio);
3813539a3322SMatthew Wilcox (Oracle)
38143e629597SMatthew Wilcox (Oracle) /**
38153e629597SMatthew Wilcox (Oracle) * mapping_read_folio_gfp - Read into page cache, using specified allocation flags.
38163e629597SMatthew Wilcox (Oracle) * @mapping: The address_space for the folio.
38173e629597SMatthew Wilcox (Oracle) * @index: The index that the allocated folio will contain.
38183e629597SMatthew Wilcox (Oracle) * @gfp: The page allocator flags to use if allocating.
38193e629597SMatthew Wilcox (Oracle) *
38203e629597SMatthew Wilcox (Oracle) * This is the same as "read_cache_folio(mapping, index, NULL, NULL)", but with
38213e629597SMatthew Wilcox (Oracle) * any new memory allocations done using the specified allocation flags.
38223e629597SMatthew Wilcox (Oracle) *
38233e629597SMatthew Wilcox (Oracle) * The most likely error from this function is EIO, but ENOMEM is
38243e629597SMatthew Wilcox (Oracle) * possible and so is EINTR. If ->read_folio returns another error,
38253e629597SMatthew Wilcox (Oracle) * that will be returned to the caller.
38263e629597SMatthew Wilcox (Oracle) *
38273e629597SMatthew Wilcox (Oracle) * The function expects mapping->invalidate_lock to be already held.
38283e629597SMatthew Wilcox (Oracle) *
38293e629597SMatthew Wilcox (Oracle) * Return: Uptodate folio on success, ERR_PTR() on failure.
38303e629597SMatthew Wilcox (Oracle) */
mapping_read_folio_gfp(struct address_space * mapping,pgoff_t index,gfp_t gfp)38313e629597SMatthew Wilcox (Oracle) struct folio *mapping_read_folio_gfp(struct address_space *mapping,
38323e629597SMatthew Wilcox (Oracle) pgoff_t index, gfp_t gfp)
38333e629597SMatthew Wilcox (Oracle) {
38343e629597SMatthew Wilcox (Oracle) return do_read_cache_folio(mapping, index, NULL, NULL, gfp);
38353e629597SMatthew Wilcox (Oracle) }
38363e629597SMatthew Wilcox (Oracle) EXPORT_SYMBOL(mapping_read_folio_gfp);
38373e629597SMatthew Wilcox (Oracle)
do_read_cache_page(struct address_space * mapping,pgoff_t index,filler_t * filler,struct file * file,gfp_t gfp)3838539a3322SMatthew Wilcox (Oracle) static struct page *do_read_cache_page(struct address_space *mapping,
3839e9b5b23eSMatthew Wilcox (Oracle) pgoff_t index, filler_t *filler, struct file *file, gfp_t gfp)
3840539a3322SMatthew Wilcox (Oracle) {
3841539a3322SMatthew Wilcox (Oracle) struct folio *folio;
3842539a3322SMatthew Wilcox (Oracle)
3843e9b5b23eSMatthew Wilcox (Oracle) folio = do_read_cache_folio(mapping, index, filler, file, gfp);
3844539a3322SMatthew Wilcox (Oracle) if (IS_ERR(folio))
3845539a3322SMatthew Wilcox (Oracle) return &folio->page;
3846539a3322SMatthew Wilcox (Oracle) return folio_file_page(folio, index);
3847539a3322SMatthew Wilcox (Oracle) }
3848539a3322SMatthew Wilcox (Oracle)
read_cache_page(struct address_space * mapping,pgoff_t index,filler_t * filler,struct file * file)384967f9fd91SSasha Levin struct page *read_cache_page(struct address_space *mapping,
3850e9b5b23eSMatthew Wilcox (Oracle) pgoff_t index, filler_t *filler, struct file *file)
38510531b2aaSLinus Torvalds {
3852e9b5b23eSMatthew Wilcox (Oracle) return do_read_cache_page(mapping, index, filler, file,
3853d322a8e5SChristoph Hellwig mapping_gfp_mask(mapping));
38540531b2aaSLinus Torvalds }
385567f9fd91SSasha Levin EXPORT_SYMBOL(read_cache_page);
38560531b2aaSLinus Torvalds
38570531b2aaSLinus Torvalds /**
38580531b2aaSLinus Torvalds * read_cache_page_gfp - read into page cache, using specified page allocation flags.
38590531b2aaSLinus Torvalds * @mapping: the page's address_space
38600531b2aaSLinus Torvalds * @index: the page index
38610531b2aaSLinus Torvalds * @gfp: the page allocator flags to use if allocating
38620531b2aaSLinus Torvalds *
38630531b2aaSLinus Torvalds * This is the same as "read_mapping_page(mapping, index, NULL)", but with
3864e6f67b8cSDave Kleikamp * any new page allocations done using the specified allocation flags.
38650531b2aaSLinus Torvalds *
38660531b2aaSLinus Torvalds * If the page does not get brought uptodate, return -EIO.
3867a862f68aSMike Rapoport *
3868730633f0SJan Kara * The function expects mapping->invalidate_lock to be already held.
3869730633f0SJan Kara *
3870a862f68aSMike Rapoport * Return: up to date page on success, ERR_PTR() on failure.
38710531b2aaSLinus Torvalds */
read_cache_page_gfp(struct address_space * mapping,pgoff_t index,gfp_t gfp)38720531b2aaSLinus Torvalds struct page *read_cache_page_gfp(struct address_space *mapping,
38730531b2aaSLinus Torvalds pgoff_t index,
38740531b2aaSLinus Torvalds gfp_t gfp)
38750531b2aaSLinus Torvalds {
38766c45b454SChristoph Hellwig return do_read_cache_page(mapping, index, NULL, NULL, gfp);
38770531b2aaSLinus Torvalds }
38780531b2aaSLinus Torvalds EXPORT_SYMBOL(read_cache_page_gfp);
38790531b2aaSLinus Torvalds
3880a92853b6SKonstantin Khlebnikov /*
3881a92853b6SKonstantin Khlebnikov * Warn about a page cache invalidation failure during a direct I/O write.
3882a92853b6SKonstantin Khlebnikov */
dio_warn_stale_pagecache(struct file * filp)3883c402a9a9SChristoph Hellwig static void dio_warn_stale_pagecache(struct file *filp)
3884a92853b6SKonstantin Khlebnikov {
3885a92853b6SKonstantin Khlebnikov static DEFINE_RATELIMIT_STATE(_rs, 86400 * HZ, DEFAULT_RATELIMIT_BURST);
3886a92853b6SKonstantin Khlebnikov char pathname[128];
3887a92853b6SKonstantin Khlebnikov char *path;
3888a92853b6SKonstantin Khlebnikov
38895df1a672SChristoph Hellwig errseq_set(&filp->f_mapping->wb_err, -EIO);
3890a92853b6SKonstantin Khlebnikov if (__ratelimit(&_rs)) {
3891a92853b6SKonstantin Khlebnikov path = file_path(filp, pathname, sizeof(pathname));
3892a92853b6SKonstantin Khlebnikov if (IS_ERR(path))
3893a92853b6SKonstantin Khlebnikov path = "(unknown)";
3894a92853b6SKonstantin Khlebnikov pr_crit("Page cache invalidation failure on direct I/O. Possible data corruption due to collision with buffered I/O!\n");
3895a92853b6SKonstantin Khlebnikov pr_crit("File: %s PID: %d Comm: %.20s\n", path, current->pid,
3896a92853b6SKonstantin Khlebnikov current->comm);
3897a92853b6SKonstantin Khlebnikov }
3898a92853b6SKonstantin Khlebnikov }
3899a92853b6SKonstantin Khlebnikov
kiocb_invalidate_post_direct_write(struct kiocb * iocb,size_t count)3900c402a9a9SChristoph Hellwig void kiocb_invalidate_post_direct_write(struct kiocb *iocb, size_t count)
3901c402a9a9SChristoph Hellwig {
3902c402a9a9SChristoph Hellwig struct address_space *mapping = iocb->ki_filp->f_mapping;
3903c402a9a9SChristoph Hellwig
3904c402a9a9SChristoph Hellwig if (mapping->nrpages &&
3905c402a9a9SChristoph Hellwig invalidate_inode_pages2_range(mapping,
3906c402a9a9SChristoph Hellwig iocb->ki_pos >> PAGE_SHIFT,
3907c402a9a9SChristoph Hellwig (iocb->ki_pos + count - 1) >> PAGE_SHIFT))
3908c402a9a9SChristoph Hellwig dio_warn_stale_pagecache(iocb->ki_filp);
3909c402a9a9SChristoph Hellwig }
3910c402a9a9SChristoph Hellwig
39111da177e4SLinus Torvalds ssize_t
generic_file_direct_write(struct kiocb * iocb,struct iov_iter * from)39121af5bb49SChristoph Hellwig generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
39131da177e4SLinus Torvalds {
3914c402a9a9SChristoph Hellwig struct address_space *mapping = iocb->ki_filp->f_mapping;
3915c402a9a9SChristoph Hellwig size_t write_len = iov_iter_count(from);
39161da177e4SLinus Torvalds ssize_t written;
39171da177e4SLinus Torvalds
39186ccfa806SHisashi Hifumi /*
39196ccfa806SHisashi Hifumi * If a page can not be invalidated, return 0 to fall back
39206ccfa806SHisashi Hifumi * to buffered write.
39216ccfa806SHisashi Hifumi */
3922e003f74aSChristoph Hellwig written = kiocb_invalidate_pages(iocb, write_len);
39236ccfa806SHisashi Hifumi if (written) {
39246ccfa806SHisashi Hifumi if (written == -EBUSY)
39256ccfa806SHisashi Hifumi return 0;
3926c402a9a9SChristoph Hellwig return written;
3927a969e903SChristoph Hellwig }
3928a969e903SChristoph Hellwig
3929639a93a5SAl Viro written = mapping->a_ops->direct_IO(iocb, from);
3930a969e903SChristoph Hellwig
3931a969e903SChristoph Hellwig /*
3932a969e903SChristoph Hellwig * Finally, try again to invalidate clean pages which might have been
3933a969e903SChristoph Hellwig * cached by non-direct readahead, or faulted in by get_user_pages()
3934a969e903SChristoph Hellwig * if the source of the write was an mmap'ed region of the file
3935a969e903SChristoph Hellwig * we're writing. Either one is a pretty crazy thing to do,
3936a969e903SChristoph Hellwig * so we don't support it 100%. If this invalidation
3937a969e903SChristoph Hellwig * fails, tough, the write still worked...
3938332391a9SLukas Czerner *
3939332391a9SLukas Czerner * Most of the time we do not need this since dio_complete() will do
3940332391a9SLukas Czerner * the invalidation for us. However there are some file systems that
3941332391a9SLukas Czerner * do not end up with dio_complete() being called, so let's not break
394280c1fe90SKonstantin Khlebnikov * them by removing it completely.
394380c1fe90SKonstantin Khlebnikov *
39449266a140SKonstantin Khlebnikov * Noticeable example is a blkdev_direct_IO().
39459266a140SKonstantin Khlebnikov *
394680c1fe90SKonstantin Khlebnikov * Skip invalidation for async writes or if mapping has no pages.
3947a969e903SChristoph Hellwig */
39481da177e4SLinus Torvalds if (written > 0) {
3949c402a9a9SChristoph Hellwig struct inode *inode = mapping->host;
3950c402a9a9SChristoph Hellwig loff_t pos = iocb->ki_pos;
3951c402a9a9SChristoph Hellwig
3952c402a9a9SChristoph Hellwig kiocb_invalidate_post_direct_write(iocb, written);
39530116651cSNamhyung Kim pos += written;
3954639a93a5SAl Viro write_len -= written;
39550116651cSNamhyung Kim if (pos > i_size_read(inode) && !S_ISBLK(inode->i_mode)) {
39560116651cSNamhyung Kim i_size_write(inode, pos);
39571da177e4SLinus Torvalds mark_inode_dirty(inode);
39581da177e4SLinus Torvalds }
39595cb6c6c7SAl Viro iocb->ki_pos = pos;
39601da177e4SLinus Torvalds }
3961ab2125dfSPavel Begunkov if (written != -EIOCBQUEUED)
3962639a93a5SAl Viro iov_iter_revert(from, write_len - iov_iter_count(from));
39631da177e4SLinus Torvalds return written;
39641da177e4SLinus Torvalds }
39651da177e4SLinus Torvalds EXPORT_SYMBOL(generic_file_direct_write);
39661da177e4SLinus Torvalds
generic_perform_write(struct kiocb * iocb,struct iov_iter * i)3967800ba295SMatthew Wilcox (Oracle) ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i)
3968afddba49SNick Piggin {
3969800ba295SMatthew Wilcox (Oracle) struct file *file = iocb->ki_filp;
3970800ba295SMatthew Wilcox (Oracle) loff_t pos = iocb->ki_pos;
3971afddba49SNick Piggin struct address_space *mapping = file->f_mapping;
3972afddba49SNick Piggin const struct address_space_operations *a_ops = mapping->a_ops;
3973afddba49SNick Piggin long status = 0;
3974afddba49SNick Piggin ssize_t written = 0;
3975674b892eSNick Piggin
3976afddba49SNick Piggin do {
3977afddba49SNick Piggin struct page *page;
3978afddba49SNick Piggin unsigned long offset; /* Offset into pagecache page */
3979afddba49SNick Piggin unsigned long bytes; /* Bytes to write to page */
3980afddba49SNick Piggin size_t copied; /* Bytes copied from user */
39811468c6f4SAlexander Potapenko void *fsdata = NULL;
3982afddba49SNick Piggin
398309cbfeafSKirill A. Shutemov offset = (pos & (PAGE_SIZE - 1));
398409cbfeafSKirill A. Shutemov bytes = min_t(unsigned long, PAGE_SIZE - offset,
3985afddba49SNick Piggin iov_iter_count(i));
3986afddba49SNick Piggin
3987afddba49SNick Piggin again:
398800a3d660SLinus Torvalds /*
398900a3d660SLinus Torvalds * Bring in the user page that we will copy from _first_.
399000a3d660SLinus Torvalds * Otherwise there's a nasty deadlock on copying from the
399100a3d660SLinus Torvalds * same page as we're writing to, without it being marked
399200a3d660SLinus Torvalds * up-to-date.
399300a3d660SLinus Torvalds */
3994631f871fSAndreas Gruenbacher if (unlikely(fault_in_iov_iter_readable(i, bytes) == bytes)) {
399500a3d660SLinus Torvalds status = -EFAULT;
399600a3d660SLinus Torvalds break;
399700a3d660SLinus Torvalds }
399800a3d660SLinus Torvalds
3999296291cdSJan Kara if (fatal_signal_pending(current)) {
4000296291cdSJan Kara status = -EINTR;
4001296291cdSJan Kara break;
4002296291cdSJan Kara }
4003296291cdSJan Kara
40049d6b0cd7SMatthew Wilcox (Oracle) status = a_ops->write_begin(file, mapping, pos, bytes,
4005afddba49SNick Piggin &page, &fsdata);
40062457aec6SMel Gorman if (unlikely(status < 0))
4007afddba49SNick Piggin break;
4008afddba49SNick Piggin
4009931e80e4Sanfei zhou if (mapping_writably_mapped(mapping))
4010931e80e4Sanfei zhou flush_dcache_page(page);
401100a3d660SLinus Torvalds
4012f0b65f39SAl Viro copied = copy_page_from_iter_atomic(page, offset, bytes, i);
4013afddba49SNick Piggin flush_dcache_page(page);
4014afddba49SNick Piggin
4015afddba49SNick Piggin status = a_ops->write_end(file, mapping, pos, bytes, copied,
4016afddba49SNick Piggin page, fsdata);
4017f0b65f39SAl Viro if (unlikely(status != copied)) {
4018f0b65f39SAl Viro iov_iter_revert(i, copied - max(status, 0L));
4019afddba49SNick Piggin if (unlikely(status < 0))
4020afddba49SNick Piggin break;
4021f0b65f39SAl Viro }
4022afddba49SNick Piggin cond_resched();
4023afddba49SNick Piggin
4024bc1bb416SAl Viro if (unlikely(status == 0)) {
4025afddba49SNick Piggin /*
4026bc1bb416SAl Viro * A short copy made ->write_end() reject the
4027bc1bb416SAl Viro * thing entirely. Might be memory poisoning
4028bc1bb416SAl Viro * halfway through, might be a race with munmap,
4029bc1bb416SAl Viro * might be severe memory pressure.
4030afddba49SNick Piggin */
4031bc1bb416SAl Viro if (copied)
4032bc1bb416SAl Viro bytes = copied;
4033afddba49SNick Piggin goto again;
4034afddba49SNick Piggin }
4035f0b65f39SAl Viro pos += status;
4036f0b65f39SAl Viro written += status;
4037afddba49SNick Piggin
4038afddba49SNick Piggin balance_dirty_pages_ratelimited(mapping);
4039afddba49SNick Piggin } while (iov_iter_count(i));
4040afddba49SNick Piggin
4041182c25e9SChristoph Hellwig if (!written)
4042182c25e9SChristoph Hellwig return status;
4043182c25e9SChristoph Hellwig iocb->ki_pos += written;
4044182c25e9SChristoph Hellwig return written;
4045afddba49SNick Piggin }
40463b93f911SAl Viro EXPORT_SYMBOL(generic_perform_write);
40471da177e4SLinus Torvalds
4048e4dd9de3SJan Kara /**
40498174202bSAl Viro * __generic_file_write_iter - write data to a file
4050e4dd9de3SJan Kara * @iocb: IO state structure (file, offset, etc.)
40518174202bSAl Viro * @from: iov_iter with data to write
4052e4dd9de3SJan Kara *
4053e4dd9de3SJan Kara * This function does all the work needed for actually writing data to a
4054e4dd9de3SJan Kara * file. It does all basic checks, removes SUID from the file, updates
4055e4dd9de3SJan Kara * modification times and calls proper subroutines depending on whether we
4056e4dd9de3SJan Kara * do direct IO or a standard buffered write.
4057e4dd9de3SJan Kara *
40589608703eSJan Kara * It expects i_rwsem to be grabbed unless we work on a block device or similar
4059e4dd9de3SJan Kara * object which does not need locking at all.
4060e4dd9de3SJan Kara *
4061e4dd9de3SJan Kara * This function does *not* take care of syncing data in case of O_SYNC write.
4062e4dd9de3SJan Kara * A caller has to handle it. This is mainly due to the fact that we want to
40639608703eSJan Kara * avoid syncing under i_rwsem.
4064a862f68aSMike Rapoport *
4065a862f68aSMike Rapoport * Return:
4066a862f68aSMike Rapoport * * number of bytes written, even for truncated writes
4067a862f68aSMike Rapoport * * negative error code if no data has been written at all
4068e4dd9de3SJan Kara */
__generic_file_write_iter(struct kiocb * iocb,struct iov_iter * from)40698174202bSAl Viro ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
40701da177e4SLinus Torvalds {
40711da177e4SLinus Torvalds struct file *file = iocb->ki_filp;
4072fb5527e6SJeff Moyer struct address_space *mapping = file->f_mapping;
40731da177e4SLinus Torvalds struct inode *inode = mapping->host;
407444fff0faSChristoph Hellwig ssize_t ret;
40751da177e4SLinus Torvalds
407644fff0faSChristoph Hellwig ret = file_remove_privs(file);
407744fff0faSChristoph Hellwig if (ret)
407844fff0faSChristoph Hellwig return ret;
40791da177e4SLinus Torvalds
408044fff0faSChristoph Hellwig ret = file_update_time(file);
408144fff0faSChristoph Hellwig if (ret)
408244fff0faSChristoph Hellwig return ret;
40831da177e4SLinus Torvalds
40842ba48ce5SAl Viro if (iocb->ki_flags & IOCB_DIRECT) {
408544fff0faSChristoph Hellwig ret = generic_file_direct_write(iocb, from);
4086fbbbad4bSMatthew Wilcox /*
4087fbbbad4bSMatthew Wilcox * If the write stopped short of completing, fall back to
4088fbbbad4bSMatthew Wilcox * buffered writes. Some filesystems do this for writes to
4089fbbbad4bSMatthew Wilcox * holes, for example. For DAX files, a buffered write will
4090fbbbad4bSMatthew Wilcox * not succeed (even if it did, DAX does not handle dirty
4091fbbbad4bSMatthew Wilcox * page-cache pages correctly).
4092fbbbad4bSMatthew Wilcox */
409344fff0faSChristoph Hellwig if (ret < 0 || !iov_iter_count(from) || IS_DAX(inode))
409444fff0faSChristoph Hellwig return ret;
409544fff0faSChristoph Hellwig return direct_write_fallback(iocb, from, ret,
409644fff0faSChristoph Hellwig generic_perform_write(iocb, from));
409744fff0faSChristoph Hellwig }
40983b93f911SAl Viro
409944fff0faSChristoph Hellwig return generic_perform_write(iocb, from);
41001da177e4SLinus Torvalds }
41018174202bSAl Viro EXPORT_SYMBOL(__generic_file_write_iter);
41021da177e4SLinus Torvalds
4103e4dd9de3SJan Kara /**
41048174202bSAl Viro * generic_file_write_iter - write data to a file
4105e4dd9de3SJan Kara * @iocb: IO state structure
41068174202bSAl Viro * @from: iov_iter with data to write
4107e4dd9de3SJan Kara *
41088174202bSAl Viro * This is a wrapper around __generic_file_write_iter() to be used by most
4109e4dd9de3SJan Kara * filesystems. It takes care of syncing the file in case of O_SYNC file
41109608703eSJan Kara * and acquires i_rwsem as needed.
4111a862f68aSMike Rapoport * Return:
4112a862f68aSMike Rapoport * * negative error code if no data has been written at all of
4113a862f68aSMike Rapoport * vfs_fsync_range() failed for a synchronous write
4114a862f68aSMike Rapoport * * number of bytes written, even for truncated writes
4115e4dd9de3SJan Kara */
generic_file_write_iter(struct kiocb * iocb,struct iov_iter * from)41168174202bSAl Viro ssize_t generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
41171da177e4SLinus Torvalds {
41181da177e4SLinus Torvalds struct file *file = iocb->ki_filp;
4119148f948bSJan Kara struct inode *inode = file->f_mapping->host;
41201da177e4SLinus Torvalds ssize_t ret;
41211da177e4SLinus Torvalds
41225955102cSAl Viro inode_lock(inode);
41233309dd04SAl Viro ret = generic_write_checks(iocb, from);
41243309dd04SAl Viro if (ret > 0)
41258174202bSAl Viro ret = __generic_file_write_iter(iocb, from);
41265955102cSAl Viro inode_unlock(inode);
41271da177e4SLinus Torvalds
4128e2592217SChristoph Hellwig if (ret > 0)
4129e2592217SChristoph Hellwig ret = generic_write_sync(iocb, ret);
41301da177e4SLinus Torvalds return ret;
41311da177e4SLinus Torvalds }
41328174202bSAl Viro EXPORT_SYMBOL(generic_file_write_iter);
41331da177e4SLinus Torvalds
4134cf9a2ae8SDavid Howells /**
413582c50f8bSMatthew Wilcox (Oracle) * filemap_release_folio() - Release fs-specific metadata on a folio.
413682c50f8bSMatthew Wilcox (Oracle) * @folio: The folio which the kernel is trying to free.
413782c50f8bSMatthew Wilcox (Oracle) * @gfp: Memory allocation flags (and I/O mode).
4138cf9a2ae8SDavid Howells *
413982c50f8bSMatthew Wilcox (Oracle) * The address_space is trying to release any data attached to a folio
414082c50f8bSMatthew Wilcox (Oracle) * (presumably at folio->private).
4141cf9a2ae8SDavid Howells *
414282c50f8bSMatthew Wilcox (Oracle) * This will also be called if the private_2 flag is set on a page,
414382c50f8bSMatthew Wilcox (Oracle) * indicating that the folio has other metadata associated with it.
4144cf9a2ae8SDavid Howells *
414582c50f8bSMatthew Wilcox (Oracle) * The @gfp argument specifies whether I/O may be performed to release
414682c50f8bSMatthew Wilcox (Oracle) * this page (__GFP_IO), and whether the call may block
414782c50f8bSMatthew Wilcox (Oracle) * (__GFP_RECLAIM & __GFP_FS).
4148266cf658SDavid Howells *
414982c50f8bSMatthew Wilcox (Oracle) * Return: %true if the release was successful, otherwise %false.
4150cf9a2ae8SDavid Howells */
filemap_release_folio(struct folio * folio,gfp_t gfp)415182c50f8bSMatthew Wilcox (Oracle) bool filemap_release_folio(struct folio *folio, gfp_t gfp)
4152cf9a2ae8SDavid Howells {
415382c50f8bSMatthew Wilcox (Oracle) struct address_space * const mapping = folio->mapping;
4154cf9a2ae8SDavid Howells
415582c50f8bSMatthew Wilcox (Oracle) BUG_ON(!folio_test_locked(folio));
41560201ebf2SDavid Howells if (!folio_needs_release(folio))
41570201ebf2SDavid Howells return true;
415882c50f8bSMatthew Wilcox (Oracle) if (folio_test_writeback(folio))
415982c50f8bSMatthew Wilcox (Oracle) return false;
4160cf9a2ae8SDavid Howells
4161fa29000bSMatthew Wilcox (Oracle) if (mapping && mapping->a_ops->release_folio)
4162fa29000bSMatthew Wilcox (Oracle) return mapping->a_ops->release_folio(folio, gfp);
416368189fefSMatthew Wilcox (Oracle) return try_to_free_buffers(folio);
4164cf9a2ae8SDavid Howells }
416582c50f8bSMatthew Wilcox (Oracle) EXPORT_SYMBOL(filemap_release_folio);
4166cf264e13SNhat Pham
4167cf264e13SNhat Pham #ifdef CONFIG_CACHESTAT_SYSCALL
4168cf264e13SNhat Pham /**
4169cf264e13SNhat Pham * filemap_cachestat() - compute the page cache statistics of a mapping
4170cf264e13SNhat Pham * @mapping: The mapping to compute the statistics for.
4171cf264e13SNhat Pham * @first_index: The starting page cache index.
4172cf264e13SNhat Pham * @last_index: The final page index (inclusive).
4173cf264e13SNhat Pham * @cs: the cachestat struct to write the result to.
4174cf264e13SNhat Pham *
4175cf264e13SNhat Pham * This will query the page cache statistics of a mapping in the
4176cf264e13SNhat Pham * page range of [first_index, last_index] (inclusive). The statistics
4177cf264e13SNhat Pham * queried include: number of dirty pages, number of pages marked for
4178cf264e13SNhat Pham * writeback, and the number of (recently) evicted pages.
4179cf264e13SNhat Pham */
filemap_cachestat(struct address_space * mapping,pgoff_t first_index,pgoff_t last_index,struct cachestat * cs)4180cf264e13SNhat Pham static void filemap_cachestat(struct address_space *mapping,
4181cf264e13SNhat Pham pgoff_t first_index, pgoff_t last_index, struct cachestat *cs)
4182cf264e13SNhat Pham {
4183cf264e13SNhat Pham XA_STATE(xas, &mapping->i_pages, first_index);
4184cf264e13SNhat Pham struct folio *folio;
4185cf264e13SNhat Pham
4186cf264e13SNhat Pham rcu_read_lock();
4187cf264e13SNhat Pham xas_for_each(&xas, folio, last_index) {
4188ba60fdf7SNhat Pham int order;
4189cf264e13SNhat Pham unsigned long nr_pages;
4190cf264e13SNhat Pham pgoff_t folio_first_index, folio_last_index;
4191cf264e13SNhat Pham
4192ba60fdf7SNhat Pham /*
4193ba60fdf7SNhat Pham * Don't deref the folio. It is not pinned, and might
4194ba60fdf7SNhat Pham * get freed (and reused) underneath us.
4195ba60fdf7SNhat Pham *
4196ba60fdf7SNhat Pham * We *could* pin it, but that would be expensive for
4197ba60fdf7SNhat Pham * what should be a fast and lightweight syscall.
4198ba60fdf7SNhat Pham *
4199ba60fdf7SNhat Pham * Instead, derive all information of interest from
4200ba60fdf7SNhat Pham * the rcu-protected xarray.
4201ba60fdf7SNhat Pham */
4202ba60fdf7SNhat Pham
4203cf264e13SNhat Pham if (xas_retry(&xas, folio))
4204cf264e13SNhat Pham continue;
4205cf264e13SNhat Pham
4206ba60fdf7SNhat Pham order = xa_get_order(xas.xa, xas.xa_index);
4207cf264e13SNhat Pham nr_pages = 1 << order;
4208cf264e13SNhat Pham folio_first_index = round_down(xas.xa_index, 1 << order);
4209cf264e13SNhat Pham folio_last_index = folio_first_index + nr_pages - 1;
4210cf264e13SNhat Pham
4211cf264e13SNhat Pham /* Folios might straddle the range boundaries, only count covered pages */
4212cf264e13SNhat Pham if (folio_first_index < first_index)
4213cf264e13SNhat Pham nr_pages -= first_index - folio_first_index;
4214cf264e13SNhat Pham
4215cf264e13SNhat Pham if (folio_last_index > last_index)
4216cf264e13SNhat Pham nr_pages -= folio_last_index - last_index;
4217cf264e13SNhat Pham
4218ba60fdf7SNhat Pham if (xa_is_value(folio)) {
4219ba60fdf7SNhat Pham /* page is evicted */
4220ba60fdf7SNhat Pham void *shadow = (void *)folio;
4221ba60fdf7SNhat Pham bool workingset; /* not used */
4222ba60fdf7SNhat Pham
4223cf264e13SNhat Pham cs->nr_evicted += nr_pages;
4224cf264e13SNhat Pham
4225cf264e13SNhat Pham #ifdef CONFIG_SWAP /* implies CONFIG_MMU */
4226cf264e13SNhat Pham if (shmem_mapping(mapping)) {
4227cf264e13SNhat Pham /* shmem file - in swap cache */
4228cf264e13SNhat Pham swp_entry_t swp = radix_to_swp_entry(folio);
4229cf264e13SNhat Pham
4230b79f9e1fSJohannes Weiner /* swapin error results in poisoned entry */
4231b79f9e1fSJohannes Weiner if (non_swap_entry(swp))
4232b79f9e1fSJohannes Weiner goto resched;
4233b79f9e1fSJohannes Weiner
4234b79f9e1fSJohannes Weiner /*
4235b79f9e1fSJohannes Weiner * Getting a swap entry from the shmem
4236b79f9e1fSJohannes Weiner * inode means we beat
4237b79f9e1fSJohannes Weiner * shmem_unuse(). rcu_read_lock()
4238b79f9e1fSJohannes Weiner * ensures swapoff waits for us before
4239b79f9e1fSJohannes Weiner * freeing the swapper space. However,
4240b79f9e1fSJohannes Weiner * we can race with swapping and
4241b79f9e1fSJohannes Weiner * invalidation, so there might not be
4242b79f9e1fSJohannes Weiner * a shadow in the swapcache (yet).
4243b79f9e1fSJohannes Weiner */
4244cf264e13SNhat Pham shadow = get_shadow_from_swap_cache(swp);
4245b79f9e1fSJohannes Weiner if (!shadow)
4246b79f9e1fSJohannes Weiner goto resched;
4247cf264e13SNhat Pham }
4248cf264e13SNhat Pham #endif
4249cf264e13SNhat Pham if (workingset_test_recent(shadow, true, &workingset))
4250cf264e13SNhat Pham cs->nr_recently_evicted += nr_pages;
4251cf264e13SNhat Pham
4252cf264e13SNhat Pham goto resched;
4253cf264e13SNhat Pham }
4254cf264e13SNhat Pham
4255cf264e13SNhat Pham /* page is in cache */
4256cf264e13SNhat Pham cs->nr_cache += nr_pages;
4257cf264e13SNhat Pham
4258ba60fdf7SNhat Pham if (xas_get_mark(&xas, PAGECACHE_TAG_DIRTY))
4259cf264e13SNhat Pham cs->nr_dirty += nr_pages;
4260cf264e13SNhat Pham
4261ba60fdf7SNhat Pham if (xas_get_mark(&xas, PAGECACHE_TAG_WRITEBACK))
4262cf264e13SNhat Pham cs->nr_writeback += nr_pages;
4263cf264e13SNhat Pham
4264cf264e13SNhat Pham resched:
4265cf264e13SNhat Pham if (need_resched()) {
4266cf264e13SNhat Pham xas_pause(&xas);
4267cf264e13SNhat Pham cond_resched_rcu();
4268cf264e13SNhat Pham }
4269cf264e13SNhat Pham }
4270cf264e13SNhat Pham rcu_read_unlock();
4271cf264e13SNhat Pham }
4272cf264e13SNhat Pham
4273cf264e13SNhat Pham /*
4274*7d6405c1SLinus Torvalds * See mincore: reveal pagecache information only for files
4275*7d6405c1SLinus Torvalds * that the calling process has write access to, or could (if
4276*7d6405c1SLinus Torvalds * tried) open for writing.
4277*7d6405c1SLinus Torvalds */
can_do_cachestat(struct file * f)4278*7d6405c1SLinus Torvalds static inline bool can_do_cachestat(struct file *f)
4279*7d6405c1SLinus Torvalds {
4280*7d6405c1SLinus Torvalds if (f->f_mode & FMODE_WRITE)
4281*7d6405c1SLinus Torvalds return true;
4282*7d6405c1SLinus Torvalds if (inode_owner_or_capable(file_mnt_idmap(f), file_inode(f)))
4283*7d6405c1SLinus Torvalds return true;
4284*7d6405c1SLinus Torvalds return file_permission(f, MAY_WRITE) == 0;
4285*7d6405c1SLinus Torvalds }
4286*7d6405c1SLinus Torvalds
4287*7d6405c1SLinus Torvalds /*
4288cf264e13SNhat Pham * The cachestat(2) system call.
4289cf264e13SNhat Pham *
4290cf264e13SNhat Pham * cachestat() returns the page cache statistics of a file in the
4291cf264e13SNhat Pham * bytes range specified by `off` and `len`: number of cached pages,
4292cf264e13SNhat Pham * number of dirty pages, number of pages marked for writeback,
4293cf264e13SNhat Pham * number of evicted pages, and number of recently evicted pages.
4294cf264e13SNhat Pham *
4295cf264e13SNhat Pham * An evicted page is a page that is previously in the page cache
4296cf264e13SNhat Pham * but has been evicted since. A page is recently evicted if its last
4297cf264e13SNhat Pham * eviction was recent enough that its reentry to the cache would
4298cf264e13SNhat Pham * indicate that it is actively being used by the system, and that
4299cf264e13SNhat Pham * there is memory pressure on the system.
4300cf264e13SNhat Pham *
4301cf264e13SNhat Pham * `off` and `len` must be non-negative integers. If `len` > 0,
4302cf264e13SNhat Pham * the queried range is [`off`, `off` + `len`]. If `len` == 0,
4303cf264e13SNhat Pham * we will query in the range from `off` to the end of the file.
4304cf264e13SNhat Pham *
4305cf264e13SNhat Pham * The `flags` argument is unused for now, but is included for future
4306cf264e13SNhat Pham * extensibility. User should pass 0 (i.e no flag specified).
4307cf264e13SNhat Pham *
4308cf264e13SNhat Pham * Currently, hugetlbfs is not supported.
4309cf264e13SNhat Pham *
4310cf264e13SNhat Pham * Because the status of a page can change after cachestat() checks it
4311cf264e13SNhat Pham * but before it returns to the application, the returned values may
4312cf264e13SNhat Pham * contain stale information.
4313cf264e13SNhat Pham *
4314cf264e13SNhat Pham * return values:
4315cf264e13SNhat Pham * zero - success
4316cf264e13SNhat Pham * -EFAULT - cstat or cstat_range points to an illegal address
4317cf264e13SNhat Pham * -EINVAL - invalid flags
4318cf264e13SNhat Pham * -EBADF - invalid file descriptor
4319cf264e13SNhat Pham * -EOPNOTSUPP - file descriptor is of a hugetlbfs file
4320cf264e13SNhat Pham */
SYSCALL_DEFINE4(cachestat,unsigned int,fd,struct cachestat_range __user *,cstat_range,struct cachestat __user *,cstat,unsigned int,flags)4321cf264e13SNhat Pham SYSCALL_DEFINE4(cachestat, unsigned int, fd,
4322cf264e13SNhat Pham struct cachestat_range __user *, cstat_range,
4323cf264e13SNhat Pham struct cachestat __user *, cstat, unsigned int, flags)
4324cf264e13SNhat Pham {
4325cf264e13SNhat Pham struct fd f = fdget(fd);
4326cf264e13SNhat Pham struct address_space *mapping;
4327cf264e13SNhat Pham struct cachestat_range csr;
4328cf264e13SNhat Pham struct cachestat cs;
4329cf264e13SNhat Pham pgoff_t first_index, last_index;
4330cf264e13SNhat Pham
4331cf264e13SNhat Pham if (!f.file)
4332cf264e13SNhat Pham return -EBADF;
4333cf264e13SNhat Pham
4334cf264e13SNhat Pham if (copy_from_user(&csr, cstat_range,
4335cf264e13SNhat Pham sizeof(struct cachestat_range))) {
4336cf264e13SNhat Pham fdput(f);
4337cf264e13SNhat Pham return -EFAULT;
4338cf264e13SNhat Pham }
4339cf264e13SNhat Pham
4340cf264e13SNhat Pham /* hugetlbfs is not supported */
4341cf264e13SNhat Pham if (is_file_hugepages(f.file)) {
4342cf264e13SNhat Pham fdput(f);
4343cf264e13SNhat Pham return -EOPNOTSUPP;
4344cf264e13SNhat Pham }
4345cf264e13SNhat Pham
4346*7d6405c1SLinus Torvalds if (!can_do_cachestat(f.file)) {
4347*7d6405c1SLinus Torvalds fdput(f);
4348*7d6405c1SLinus Torvalds return -EPERM;
4349*7d6405c1SLinus Torvalds }
4350*7d6405c1SLinus Torvalds
4351cf264e13SNhat Pham if (flags != 0) {
4352cf264e13SNhat Pham fdput(f);
4353cf264e13SNhat Pham return -EINVAL;
4354cf264e13SNhat Pham }
4355cf264e13SNhat Pham
4356cf264e13SNhat Pham first_index = csr.off >> PAGE_SHIFT;
4357cf264e13SNhat Pham last_index =
4358cf264e13SNhat Pham csr.len == 0 ? ULONG_MAX : (csr.off + csr.len - 1) >> PAGE_SHIFT;
4359cf264e13SNhat Pham memset(&cs, 0, sizeof(struct cachestat));
4360cf264e13SNhat Pham mapping = f.file->f_mapping;
4361cf264e13SNhat Pham filemap_cachestat(mapping, first_index, last_index, &cs);
4362cf264e13SNhat Pham fdput(f);
4363cf264e13SNhat Pham
4364cf264e13SNhat Pham if (copy_to_user(cstat, &cs, sizeof(struct cachestat)))
4365cf264e13SNhat Pham return -EFAULT;
4366cf264e13SNhat Pham
4367cf264e13SNhat Pham return 0;
4368cf264e13SNhat Pham }
4369cf264e13SNhat Pham #endif /* CONFIG_CACHESTAT_SYSCALL */
4370