1721 lines
49 KiB
Diff
1721 lines
49 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Tyler Smith <74526874+tylertms@users.noreply.github.com>
|
|
Date: Wed, 4 Nov 2026 07:33:56 +0000
|
|
Subject: [PATCH] refactor(mm): select original file-backed mapping faults
|
|
|
|
---
|
|
mm/Makefile | 2 +-
|
|
mm/filemap.c | 802 ------------------------------------------
|
|
mm/filemap_mmap.c | 876 ++++++++++++++++++++++++++++++++++++++++++++++
|
|
3 files changed, 877 insertions(+), 803 deletions(-)
|
|
create mode 100644 mm/filemap_mmap.c
|
|
|
|
diff --git a/mm/Makefile b/mm/Makefile
|
|
index da2ec25fce6967f8e4bbaf759ac9eb66e5a568fe..727b6bc498e646c4033f180f517aa48d3e3a8a07 100644
|
|
--- a/mm/Makefile
|
|
+++ b/mm/Makefile
|
|
@@ -48,7 +48,7 @@ ifdef CONFIG_64BIT
|
|
mmu-$(CONFIG_MMU) += mseal.o
|
|
endif
|
|
|
|
-obj-y := filemap.o filemap_mkwrite.o pagecache_init.o folio_wait.o folio_fs.o mempool.o oom_kill.o oom_victim.o oom_adj.o fadvise.o \
|
|
+obj-y := filemap.o filemap_mmap.o filemap_mkwrite.o pagecache_init.o folio_wait.o folio_fs.o mempool.o oom_kill.o oom_victim.o oom_adj.o fadvise.o \
|
|
maccess.o page-writeback.o writeback_io.o writeback_folio.o writeback_domain.o folio-compat.o \
|
|
readahead.o swap.o filemap_batch.o truncate.o vmscan.o reclaim_throttle.o shrinker.o \
|
|
shmem.o util.o mmzone.o lruvec.o vmstat.o vmstat_init.o backing-dev.o backing-dev-inode.o \
|
|
diff --git a/mm/filemap.c b/mm/filemap.c
|
|
index 474b57eb888b270b696fb6af21f9731fa5989580..248589c8bd7a49db32b68a2379075e319128b3ba 100644
|
|
--- a/mm/filemap.c
|
|
+++ b/mm/filemap.c
|
|
@@ -591,808 +591,6 @@ unlock:
|
|
return start;
|
|
}
|
|
|
|
-#ifdef CONFIG_MMU
|
|
-#define MMAP_LOTSAMISS (100)
|
|
-/*
|
|
- * lock_folio_maybe_drop_mmap - lock the page, possibly dropping the mmap_lock
|
|
- * @vmf - the vm_fault for this fault.
|
|
- * @folio - the folio to lock.
|
|
- * @fpin - the pointer to the file we may pin (or is already pinned).
|
|
- *
|
|
- * This works similar to lock_folio_or_retry in that it can drop the
|
|
- * mmap_lock. It differs in that it actually returns the folio locked
|
|
- * if it returns 1 and 0 if it couldn't lock the folio. If we did have
|
|
- * to drop the mmap_lock then fpin will point to the pinned file and
|
|
- * needs to be fput()'ed at a later point.
|
|
- */
|
|
-static int lock_folio_maybe_drop_mmap(struct vm_fault *vmf, struct folio *folio,
|
|
- struct file **fpin)
|
|
-{
|
|
- if (folio_trylock(folio))
|
|
- return 1;
|
|
-
|
|
- /*
|
|
- * NOTE! This will make us return with VM_FAULT_RETRY, but with
|
|
- * the fault lock still held. That's how FAULT_FLAG_RETRY_NOWAIT
|
|
- * is supposed to work. We have way too many special cases..
|
|
- */
|
|
- if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
|
|
- return 0;
|
|
-
|
|
- *fpin = maybe_unlock_mmap_for_io(vmf, *fpin);
|
|
- if (vmf->flags & FAULT_FLAG_KILLABLE) {
|
|
- if (__folio_lock_killable(folio)) {
|
|
- /*
|
|
- * We didn't have the right flags to drop the
|
|
- * fault lock, but all fault_handlers only check
|
|
- * for fatal signals if we return VM_FAULT_RETRY,
|
|
- * so we need to drop the fault lock here and
|
|
- * return 0 if we don't have a fpin.
|
|
- */
|
|
- if (*fpin == NULL)
|
|
- release_fault_lock(vmf);
|
|
- return 0;
|
|
- }
|
|
- } else
|
|
- __folio_lock(folio);
|
|
-
|
|
- return 1;
|
|
-}
|
|
-
|
|
-/*
|
|
- * Synchronous readahead happens when we don't even find a page in the page
|
|
- * cache at all. We don't want to perform IO under the mmap sem, so if we have
|
|
- * to drop the mmap sem we return the file that was pinned in order for us to do
|
|
- * that. If we didn't pin a file then we return NULL. The file that is
|
|
- * returned needs to be fput()'ed when we're done with it.
|
|
- */
|
|
-static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
|
|
-{
|
|
- struct file *file = vmf->vma->vm_file;
|
|
- struct file_ra_state *ra = &file->f_ra;
|
|
- struct address_space *mapping = file->f_mapping;
|
|
- DEFINE_READAHEAD(ractl, file, ra, mapping, vmf->pgoff);
|
|
- struct file *fpin = NULL;
|
|
- vm_flags_t vm_flags = vmf->vma->vm_flags;
|
|
- bool force_thp_readahead = false;
|
|
- unsigned int thp_order = 0;
|
|
- unsigned short mmap_miss;
|
|
-
|
|
- /* Use the readahead code, even if readahead is disabled */
|
|
- if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && (vm_flags & VM_HUGEPAGE)) {
|
|
- /*
|
|
- * Cap max THP order at 2MB: this is the common PMD-sized
|
|
- * hugepage size, and it avoids memory pressure from very
|
|
- * large forced readahead when mapping_max_folio_order() is
|
|
- * high (for example, 128MB with 64K base pages on arm64).
|
|
- */
|
|
- if (mapping_large_folio_support(mapping)) {
|
|
- force_thp_readahead = true;
|
|
- thp_order = min_t(unsigned int,
|
|
- mapping_max_folio_order(mapping),
|
|
- get_order(SZ_2M));
|
|
- }
|
|
- }
|
|
-
|
|
- if (!force_thp_readahead) {
|
|
- /*
|
|
- * If we don't want any read-ahead, don't bother.
|
|
- * VM_EXEC case below is already intended for random access.
|
|
- */
|
|
- if ((vm_flags & (VM_RAND_READ | VM_EXEC)) == VM_RAND_READ)
|
|
- return fpin;
|
|
-
|
|
- if (!ra->ra_pages)
|
|
- return fpin;
|
|
-
|
|
- if (vm_flags & VM_SEQ_READ) {
|
|
- fpin = maybe_unlock_mmap_for_io(vmf, fpin);
|
|
- page_cache_sync_ra(&ractl, ra->ra_pages);
|
|
- return fpin;
|
|
- }
|
|
- }
|
|
-
|
|
- if (!(vm_flags & (VM_SEQ_READ | VM_EXEC))) {
|
|
- /* Avoid banging the cache line if not needed */
|
|
- mmap_miss = READ_ONCE(ra->mmap_miss);
|
|
- if (mmap_miss < MMAP_LOTSAMISS * 10)
|
|
- WRITE_ONCE(ra->mmap_miss, ++mmap_miss);
|
|
-
|
|
- /*
|
|
- * Do we miss much more than hit in this file? If so,
|
|
- * stop bothering with read-ahead. It will only hurt.
|
|
- */
|
|
- if (mmap_miss > MMAP_LOTSAMISS)
|
|
- return fpin;
|
|
- }
|
|
-
|
|
- if (force_thp_readahead) {
|
|
- unsigned long folio_nr_pages = 1UL << thp_order;
|
|
-
|
|
- fpin = maybe_unlock_mmap_for_io(vmf, fpin);
|
|
- ractl._index &= ~(folio_nr_pages - 1);
|
|
- ra->size = folio_nr_pages;
|
|
- /*
|
|
- * Fetch two folios so we get the chance to actually
|
|
- * readahead, unless we've been told not to.
|
|
- */
|
|
- if (!(vm_flags & VM_RAND_READ))
|
|
- ra->size *= 2;
|
|
- ra->async_size = folio_nr_pages;
|
|
- ra->order = thp_order;
|
|
- page_cache_ra_order(&ractl, ra);
|
|
- return fpin;
|
|
- }
|
|
-
|
|
- if (vm_flags & VM_EXEC) {
|
|
- /*
|
|
- * Allow arch to request a preferred minimum folio order for
|
|
- * executable memory. This can often be beneficial to
|
|
- * performance if (e.g.) arm64 can contpte-map the folio.
|
|
- * Executable memory rarely benefits from readahead, due to its
|
|
- * random access nature, so set async_size to 0.
|
|
- *
|
|
- * Limit to the boundaries of the VMA to avoid reading in any
|
|
- * pad that might exist between sections, which would be a waste
|
|
- * of memory.
|
|
- */
|
|
- struct vm_area_struct *vma = vmf->vma;
|
|
- unsigned long start = vma->vm_pgoff;
|
|
- unsigned long end = start + vma_pages(vma);
|
|
- unsigned long ra_end;
|
|
-
|
|
- ra->order = exec_folio_order();
|
|
- ra->start = round_down(vmf->pgoff, 1UL << ra->order);
|
|
- ra->start = max(ra->start, start);
|
|
- ra_end = round_up(ra->start + ra->ra_pages, 1UL << ra->order);
|
|
- ra_end = min(ra_end, end);
|
|
- ra->size = ra_end - ra->start;
|
|
- ra->async_size = 0;
|
|
- } else {
|
|
- /*
|
|
- * mmap read-around
|
|
- */
|
|
- ra->start = max_t(long, 0, vmf->pgoff - ra->ra_pages / 2);
|
|
- ra->size = ra->ra_pages;
|
|
- ra->async_size = ra->ra_pages / 4;
|
|
- ra->order = 0;
|
|
- }
|
|
-
|
|
- fpin = maybe_unlock_mmap_for_io(vmf, fpin);
|
|
- ractl._index = ra->start;
|
|
- page_cache_ra_order(&ractl, ra);
|
|
- return fpin;
|
|
-}
|
|
-
|
|
-/*
|
|
- * Asynchronous readahead happens when we find the page and PG_readahead,
|
|
- * so we want to possibly extend the readahead further. We return the file that
|
|
- * was pinned if we have to drop the mmap_lock in order to do IO.
|
|
- */
|
|
-static struct file *do_async_mmap_readahead(struct vm_fault *vmf,
|
|
- struct folio *folio)
|
|
-{
|
|
- struct file *file = vmf->vma->vm_file;
|
|
- struct file_ra_state *ra = &file->f_ra;
|
|
- DEFINE_READAHEAD(ractl, file, ra, file->f_mapping, vmf->pgoff);
|
|
- struct file *fpin = NULL;
|
|
- unsigned short mmap_miss;
|
|
-
|
|
- /* If we don't want any read-ahead, don't bother */
|
|
- if (vmf->vma->vm_flags & VM_RAND_READ || !ra->ra_pages)
|
|
- return fpin;
|
|
-
|
|
- /*
|
|
- * If the folio is locked, we're likely racing against another fault.
|
|
- * Don't touch the mmap_miss counter to avoid decreasing it multiple
|
|
- * times for a single folio and break the balance with mmap_miss
|
|
- * increase in do_sync_mmap_readahead().
|
|
- *
|
|
- * VM_SEQ_READ and VM_EXEC mappings skip the mmap_miss increment in
|
|
- * do_sync_mmap_readahead(), so skip the decrement here as well to
|
|
- * keep the counter symmetric.
|
|
- */
|
|
- if (likely(!folio_test_locked(folio)) &&
|
|
- !(vmf->vma->vm_flags & (VM_SEQ_READ | VM_EXEC))) {
|
|
- mmap_miss = READ_ONCE(ra->mmap_miss);
|
|
- if (mmap_miss)
|
|
- WRITE_ONCE(ra->mmap_miss, --mmap_miss);
|
|
- }
|
|
-
|
|
- if (folio_test_readahead(folio)) {
|
|
- fpin = maybe_unlock_mmap_for_io(vmf, fpin);
|
|
- page_cache_async_ra(&ractl, folio, ra->ra_pages);
|
|
- }
|
|
- return fpin;
|
|
-}
|
|
-
|
|
-static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf)
|
|
-{
|
|
- struct vm_area_struct *vma = vmf->vma;
|
|
- vm_fault_t ret = 0;
|
|
- pte_t *ptep;
|
|
-
|
|
- /*
|
|
- * We might have COW'ed a pagecache folio and might now have an mlocked
|
|
- * anon folio mapped. The original pagecache folio is not mlocked and
|
|
- * might have been evicted. During a read+clear/modify/write update of
|
|
- * the PTE, such as done in do_numa_page()/change_pte_range(), we
|
|
- * temporarily clear the PTE under PT lock and might detect it here as
|
|
- * "none" when not holding the PT lock.
|
|
- *
|
|
- * Not rechecking the PTE under PT lock could result in an unexpected
|
|
- * major fault in an mlock'ed region. Recheck only for this special
|
|
- * scenario while holding the PT lock, to not degrade non-mlocked
|
|
- * scenarios. Recheck the PTE without PT lock firstly, thereby reducing
|
|
- * the number of times we hold PT lock.
|
|
- */
|
|
- if (!(vma->vm_flags & VM_LOCKED))
|
|
- return 0;
|
|
-
|
|
- if (!(vmf->flags & FAULT_FLAG_ORIG_PTE_VALID))
|
|
- return 0;
|
|
-
|
|
- ptep = pte_offset_map_ro_nolock(vma->vm_mm, vmf->pmd, vmf->address,
|
|
- &vmf->ptl);
|
|
- if (unlikely(!ptep))
|
|
- return VM_FAULT_NOPAGE;
|
|
-
|
|
- if (unlikely(!pte_none(ptep_get_lockless(ptep)))) {
|
|
- ret = VM_FAULT_NOPAGE;
|
|
- } else {
|
|
- spin_lock(vmf->ptl);
|
|
- if (unlikely(!pte_none(ptep_get(ptep))))
|
|
- ret = VM_FAULT_NOPAGE;
|
|
- spin_unlock(vmf->ptl);
|
|
- }
|
|
- pte_unmap(ptep);
|
|
- return ret;
|
|
-}
|
|
-
|
|
-/**
|
|
- * filemap_fault - read in file data for page fault handling
|
|
- * @vmf: struct vm_fault containing details of the fault
|
|
- *
|
|
- * filemap_fault() is invoked via the vma operations vector for a
|
|
- * mapped memory region to read in file data during a page fault.
|
|
- *
|
|
- * The goto's are kind of ugly, but this streamlines the normal case of having
|
|
- * it in the page cache, and handles the special cases reasonably without
|
|
- * having a lot of duplicated code.
|
|
- *
|
|
- * vma->vm_mm->mmap_lock must be held on entry.
|
|
- *
|
|
- * If our return value has VM_FAULT_RETRY set, it's because the mmap_lock
|
|
- * may be dropped before doing I/O or by lock_folio_maybe_drop_mmap().
|
|
- *
|
|
- * If our return value does not have VM_FAULT_RETRY set, the mmap_lock
|
|
- * has not been released.
|
|
- *
|
|
- * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
|
|
- *
|
|
- * Return: bitwise-OR of %VM_FAULT_ codes.
|
|
- */
|
|
-vm_fault_t filemap_fault(struct vm_fault *vmf)
|
|
-{
|
|
- int error;
|
|
- struct file *file = vmf->vma->vm_file;
|
|
- struct file *fpin = NULL;
|
|
- struct address_space *mapping = file->f_mapping;
|
|
- struct inode *inode = mapping->host;
|
|
- pgoff_t max_idx, index = vmf->pgoff;
|
|
- struct folio *folio;
|
|
- vm_fault_t ret = 0;
|
|
- bool mapping_locked = false;
|
|
-
|
|
- max_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
|
|
- if (unlikely(index >= max_idx))
|
|
- return VM_FAULT_SIGBUS;
|
|
-
|
|
- trace_mm_filemap_fault(mapping, index);
|
|
-
|
|
- /*
|
|
- * Do we have something in the page cache already?
|
|
- */
|
|
- folio = filemap_get_folio(mapping, index);
|
|
- if (likely(!IS_ERR(folio))) {
|
|
- /*
|
|
- * We found the page, so try async readahead before waiting for
|
|
- * the lock.
|
|
- */
|
|
- if (!(vmf->flags & FAULT_FLAG_TRIED))
|
|
- fpin = do_async_mmap_readahead(vmf, folio);
|
|
- if (unlikely(!folio_test_uptodate(folio))) {
|
|
- filemap_invalidate_lock_shared(mapping);
|
|
- mapping_locked = true;
|
|
- }
|
|
- } else {
|
|
- ret = filemap_fault_recheck_pte_none(vmf);
|
|
- if (unlikely(ret))
|
|
- return ret;
|
|
-
|
|
- /* No page in the page cache at all */
|
|
- count_vm_event(PGMAJFAULT);
|
|
- count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
|
|
- ret = VM_FAULT_MAJOR;
|
|
- fpin = do_sync_mmap_readahead(vmf);
|
|
-retry_find:
|
|
- /*
|
|
- * See comment in filemap_create_folio() why we need
|
|
- * invalidate_lock
|
|
- */
|
|
- if (!mapping_locked) {
|
|
- filemap_invalidate_lock_shared(mapping);
|
|
- mapping_locked = true;
|
|
- }
|
|
- folio = __filemap_get_folio(mapping, index,
|
|
- FGP_CREAT|FGP_FOR_MMAP,
|
|
- vmf->gfp_mask);
|
|
- if (IS_ERR(folio)) {
|
|
- if (fpin)
|
|
- goto out_retry;
|
|
- filemap_invalidate_unlock_shared(mapping);
|
|
- return VM_FAULT_OOM;
|
|
- }
|
|
- }
|
|
-
|
|
- if (!lock_folio_maybe_drop_mmap(vmf, folio, &fpin))
|
|
- goto out_retry;
|
|
-
|
|
- /* Did it get truncated? */
|
|
- if (unlikely(folio->mapping != mapping)) {
|
|
- folio_unlock(folio);
|
|
- folio_put(folio);
|
|
- goto retry_find;
|
|
- }
|
|
- VM_BUG_ON_FOLIO(!folio_contains(folio, index), folio);
|
|
-
|
|
- /*
|
|
- * We have a locked folio in the page cache, now we need to check
|
|
- * that it's up-to-date. If not, it is going to be due to an error,
|
|
- * or because readahead was otherwise unable to retrieve it.
|
|
- */
|
|
- if (unlikely(!folio_test_uptodate(folio))) {
|
|
- /*
|
|
- * If the invalidate lock is not held, the folio was in cache
|
|
- * and uptodate and now it is not. Strange but possible since we
|
|
- * didn't hold the page lock all the time. Let's drop
|
|
- * everything, get the invalidate lock and try again.
|
|
- */
|
|
- if (!mapping_locked) {
|
|
- folio_unlock(folio);
|
|
- folio_put(folio);
|
|
- goto retry_find;
|
|
- }
|
|
-
|
|
- /*
|
|
- * OK, the folio is really not uptodate. This can be because the
|
|
- * VMA has the VM_RAND_READ flag set, or because an error
|
|
- * arose. Let's read it in directly.
|
|
- */
|
|
- goto page_not_uptodate;
|
|
- }
|
|
-
|
|
- /*
|
|
- * We've made it this far and we had to drop our mmap_lock, now is the
|
|
- * time to return to the upper layer and have it re-find the vma and
|
|
- * redo the fault.
|
|
- */
|
|
- if (fpin) {
|
|
- folio_unlock(folio);
|
|
- goto out_retry;
|
|
- }
|
|
- if (mapping_locked)
|
|
- filemap_invalidate_unlock_shared(mapping);
|
|
-
|
|
- /*
|
|
- * Found the page and have a reference on it.
|
|
- * We must recheck i_size under page lock.
|
|
- */
|
|
- max_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
|
|
- if (unlikely(index >= max_idx)) {
|
|
- folio_unlock(folio);
|
|
- folio_put(folio);
|
|
- return VM_FAULT_SIGBUS;
|
|
- }
|
|
-
|
|
- vmf->page = folio_file_page(folio, index);
|
|
- return ret | VM_FAULT_LOCKED;
|
|
-
|
|
-page_not_uptodate:
|
|
- /*
|
|
- * Umm, take care of errors if the page isn't up-to-date.
|
|
- * Try to re-read it _once_. We do this synchronously,
|
|
- * because there really aren't any performance issues here
|
|
- * and we need to check for errors.
|
|
- */
|
|
- fpin = maybe_unlock_mmap_for_io(vmf, fpin);
|
|
- error = filemap_read_folio(file, mapping->a_ops->read_folio, folio);
|
|
- if (fpin)
|
|
- goto out_retry;
|
|
- folio_put(folio);
|
|
-
|
|
- if (!error || error == AOP_TRUNCATED_PAGE)
|
|
- goto retry_find;
|
|
- filemap_invalidate_unlock_shared(mapping);
|
|
-
|
|
- return VM_FAULT_SIGBUS;
|
|
-
|
|
-out_retry:
|
|
- /*
|
|
- * We dropped the mmap_lock, we need to return to the fault handler to
|
|
- * re-find the vma and come back and find our hopefully still populated
|
|
- * page.
|
|
- */
|
|
- if (!IS_ERR(folio))
|
|
- folio_put(folio);
|
|
- if (mapping_locked)
|
|
- filemap_invalidate_unlock_shared(mapping);
|
|
- if (fpin)
|
|
- fput(fpin);
|
|
- return ret | VM_FAULT_RETRY;
|
|
-}
|
|
-EXPORT_SYMBOL(filemap_fault);
|
|
-
|
|
-static bool filemap_map_pmd(struct vm_fault *vmf, struct folio *folio,
|
|
- pgoff_t start)
|
|
-{
|
|
- struct mm_struct *mm = vmf->vma->vm_mm;
|
|
-
|
|
- /* Huge page is mapped? No need to proceed. */
|
|
- if (pmd_trans_huge(*vmf->pmd)) {
|
|
- folio_unlock(folio);
|
|
- folio_put(folio);
|
|
- return true;
|
|
- }
|
|
-
|
|
- if (pmd_none(*vmf->pmd) && folio_test_pmd_mappable(folio)) {
|
|
- struct page *page = folio_file_page(folio, start);
|
|
- vm_fault_t ret = do_set_pmd(vmf, folio, page);
|
|
- if (!ret) {
|
|
- /* The page is mapped successfully, reference consumed. */
|
|
- folio_unlock(folio);
|
|
- return true;
|
|
- }
|
|
- }
|
|
-
|
|
- if (pmd_none(*vmf->pmd) && vmf->prealloc_pte)
|
|
- pmd_install(mm, vmf->pmd, &vmf->prealloc_pte);
|
|
-
|
|
- return false;
|
|
-}
|
|
-
|
|
-static struct folio *next_uptodate_folio(struct xa_state *xas,
|
|
- struct address_space *mapping, pgoff_t end_pgoff)
|
|
-{
|
|
- struct folio *folio = xas_next_entry(xas, end_pgoff);
|
|
- unsigned long max_idx;
|
|
-
|
|
- do {
|
|
- if (!folio)
|
|
- return NULL;
|
|
- if (xas_retry(xas, folio))
|
|
- continue;
|
|
- if (xa_is_value(folio))
|
|
- continue;
|
|
- if (!folio_try_get(folio))
|
|
- continue;
|
|
- if (folio_test_locked(folio))
|
|
- goto skip;
|
|
- /* Has the page moved or been split? */
|
|
- if (unlikely(folio != xas_reload(xas)))
|
|
- goto skip;
|
|
- if (!folio_test_uptodate(folio) || folio_test_readahead(folio))
|
|
- goto skip;
|
|
- if (!folio_trylock(folio))
|
|
- goto skip;
|
|
- if (folio->mapping != mapping)
|
|
- goto unlock;
|
|
- if (!folio_test_uptodate(folio))
|
|
- goto unlock;
|
|
- max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
|
|
- if (xas->xa_index >= max_idx)
|
|
- goto unlock;
|
|
- return folio;
|
|
-unlock:
|
|
- folio_unlock(folio);
|
|
-skip:
|
|
- folio_put(folio);
|
|
- } while ((folio = xas_next_entry(xas, end_pgoff)) != NULL);
|
|
-
|
|
- return NULL;
|
|
-}
|
|
-
|
|
-/*
|
|
- * Map page range [start_page, start_page + nr_pages) of folio.
|
|
- * start_page is gotten from start by folio_page(folio, start)
|
|
- */
|
|
-static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
|
|
- struct folio *folio, unsigned long start,
|
|
- unsigned long addr, unsigned int nr_pages,
|
|
- unsigned long *rss, pgoff_t file_end)
|
|
-{
|
|
- struct address_space *mapping = folio->mapping;
|
|
- unsigned int ref_from_caller = 1;
|
|
- vm_fault_t ret = 0;
|
|
- struct page *page = folio_page(folio, start);
|
|
- unsigned int count = 0;
|
|
- pte_t *old_ptep = vmf->pte;
|
|
- unsigned long addr0;
|
|
-
|
|
- /*
|
|
- * Map the large folio fully where possible:
|
|
- *
|
|
- * - The folio is fully within size of the file or belong
|
|
- * to shmem/tmpfs;
|
|
- * - The folio doesn't cross VMA boundary;
|
|
- * - The folio doesn't cross page table boundary;
|
|
- */
|
|
- addr0 = addr - start * PAGE_SIZE;
|
|
- if ((file_end >= folio_next_index(folio) || shmem_mapping(mapping)) &&
|
|
- folio_within_vma(folio, vmf->vma) &&
|
|
- (addr0 & PMD_MASK) == ((addr0 + folio_size(folio) - 1) & PMD_MASK)) {
|
|
- vmf->pte -= start;
|
|
- page -= start;
|
|
- addr = addr0;
|
|
- nr_pages = folio_nr_pages(folio);
|
|
- }
|
|
-
|
|
- do {
|
|
- if (PageHWPoison(page + count))
|
|
- goto skip;
|
|
-
|
|
- /*
|
|
- * NOTE: If there're PTE markers, we'll leave them to be
|
|
- * handled in the specific fault path, and it'll prohibit the
|
|
- * fault-around logic.
|
|
- */
|
|
- if (!pte_none(ptep_get(&vmf->pte[count])))
|
|
- goto skip;
|
|
-
|
|
- count++;
|
|
- continue;
|
|
-skip:
|
|
- if (count) {
|
|
- set_pte_range(vmf, folio, page, count, addr);
|
|
- *rss += count;
|
|
- folio_ref_add(folio, count - ref_from_caller);
|
|
- ref_from_caller = 0;
|
|
- if (in_range(vmf->address, addr, count * PAGE_SIZE))
|
|
- ret = VM_FAULT_NOPAGE;
|
|
- }
|
|
-
|
|
- count++;
|
|
- page += count;
|
|
- vmf->pte += count;
|
|
- addr += count * PAGE_SIZE;
|
|
- count = 0;
|
|
- } while (--nr_pages > 0);
|
|
-
|
|
- if (count) {
|
|
- set_pte_range(vmf, folio, page, count, addr);
|
|
- *rss += count;
|
|
- folio_ref_add(folio, count - ref_from_caller);
|
|
- ref_from_caller = 0;
|
|
- if (in_range(vmf->address, addr, count * PAGE_SIZE))
|
|
- ret = VM_FAULT_NOPAGE;
|
|
- }
|
|
-
|
|
- vmf->pte = old_ptep;
|
|
- if (ref_from_caller)
|
|
- /* Locked folios cannot get truncated. */
|
|
- folio_ref_dec(folio);
|
|
-
|
|
- return ret;
|
|
-}
|
|
-
|
|
-static vm_fault_t filemap_map_order0_folio(struct vm_fault *vmf,
|
|
- struct folio *folio, unsigned long addr,
|
|
- unsigned long *rss)
|
|
-{
|
|
- vm_fault_t ret = 0;
|
|
- struct page *page = &folio->page;
|
|
-
|
|
- if (PageHWPoison(page))
|
|
- goto out;
|
|
-
|
|
- /*
|
|
- * NOTE: If there're PTE markers, we'll leave them to be
|
|
- * handled in the specific fault path, and it'll prohibit
|
|
- * the fault-around logic.
|
|
- */
|
|
- if (!pte_none(ptep_get(vmf->pte)))
|
|
- goto out;
|
|
-
|
|
- if (vmf->address == addr)
|
|
- ret = VM_FAULT_NOPAGE;
|
|
-
|
|
- set_pte_range(vmf, folio, page, 1, addr);
|
|
- (*rss)++;
|
|
- return ret;
|
|
-
|
|
-out:
|
|
- /* Locked folios cannot get truncated. */
|
|
- folio_ref_dec(folio);
|
|
- return ret;
|
|
-}
|
|
-
|
|
-vm_fault_t filemap_map_pages(struct vm_fault *vmf,
|
|
- pgoff_t start_pgoff, pgoff_t end_pgoff)
|
|
-{
|
|
- struct vm_area_struct *vma = vmf->vma;
|
|
- struct file *file = vma->vm_file;
|
|
- struct address_space *mapping = file->f_mapping;
|
|
- pgoff_t file_end, last_pgoff = start_pgoff;
|
|
- unsigned long addr;
|
|
- XA_STATE(xas, &mapping->i_pages, start_pgoff);
|
|
- struct folio *folio;
|
|
- vm_fault_t ret = 0;
|
|
- unsigned long rss = 0;
|
|
- unsigned int nr_pages = 0, folio_type;
|
|
-
|
|
- /*
|
|
- * Recalculate end_pgoff based on file_end before calling
|
|
- * next_uptodate_folio() to avoid races with concurrent
|
|
- * truncation.
|
|
- */
|
|
- file_end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE) - 1;
|
|
- end_pgoff = min(end_pgoff, file_end);
|
|
-
|
|
- rcu_read_lock();
|
|
- folio = next_uptodate_folio(&xas, mapping, end_pgoff);
|
|
- if (!folio)
|
|
- goto out;
|
|
-
|
|
- /*
|
|
- * Do not allow to map with PMD across i_size to preserve
|
|
- * SIGBUS semantics.
|
|
- *
|
|
- * Make an exception for shmem/tmpfs that for long time
|
|
- * intentionally mapped with PMDs across i_size.
|
|
- */
|
|
- if ((file_end >= folio_next_index(folio) || shmem_mapping(mapping)) &&
|
|
- filemap_map_pmd(vmf, folio, start_pgoff)) {
|
|
- ret = VM_FAULT_NOPAGE;
|
|
- goto out;
|
|
- }
|
|
-
|
|
- addr = vma->vm_start + ((start_pgoff - vma->vm_pgoff) << PAGE_SHIFT);
|
|
- vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, addr, &vmf->ptl);
|
|
- if (!vmf->pte) {
|
|
- folio_unlock(folio);
|
|
- folio_put(folio);
|
|
- goto out;
|
|
- }
|
|
-
|
|
- folio_type = mm_counter_file(folio);
|
|
- do {
|
|
- unsigned long end;
|
|
- vm_fault_t map_ret;
|
|
-
|
|
- addr += (xas.xa_index - last_pgoff) << PAGE_SHIFT;
|
|
- vmf->pte += xas.xa_index - last_pgoff;
|
|
- last_pgoff = xas.xa_index;
|
|
- end = folio_next_index(folio) - 1;
|
|
- nr_pages = min(end, end_pgoff) - xas.xa_index + 1;
|
|
-
|
|
- if (!folio_test_large(folio)) {
|
|
- map_ret = filemap_map_order0_folio(vmf, folio, addr,
|
|
- &rss);
|
|
- } else {
|
|
- unsigned long start = xas.xa_index - folio->index;
|
|
-
|
|
- map_ret = filemap_map_folio_range(vmf, folio, start,
|
|
- addr, nr_pages, &rss,
|
|
- file_end);
|
|
- }
|
|
- ret |= map_ret;
|
|
-
|
|
- /*
|
|
- * If there are too many folios that are recently evicted
|
|
- * in a file, they will probably continue to be evicted.
|
|
- * In such situation, read-ahead is only a waste of IO.
|
|
- * Don't decrease mmap_miss in this scenario to make sure
|
|
- * we can stop read-ahead.
|
|
- *
|
|
- * VM_SEQ_READ and VM_EXEC mappings skip the mmap_miss
|
|
- * increment in do_sync_mmap_readahead(), so skip the
|
|
- * decrement here as well to keep the counter symmetric.
|
|
- */
|
|
- if ((map_ret & VM_FAULT_NOPAGE) &&
|
|
- !(vmf->flags & FAULT_FLAG_TRIED) &&
|
|
- !folio_test_workingset(folio) &&
|
|
- !(vma->vm_flags & (VM_SEQ_READ | VM_EXEC))) {
|
|
- unsigned short mmap_miss;
|
|
-
|
|
- mmap_miss = READ_ONCE(file->f_ra.mmap_miss);
|
|
- if (mmap_miss)
|
|
- WRITE_ONCE(file->f_ra.mmap_miss,
|
|
- mmap_miss - 1);
|
|
- }
|
|
-
|
|
- folio_unlock(folio);
|
|
- } while ((folio = next_uptodate_folio(&xas, mapping, end_pgoff)) != NULL);
|
|
- add_mm_counter(vma->vm_mm, folio_type, rss);
|
|
- pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
- trace_mm_filemap_map_pages(mapping, start_pgoff, end_pgoff);
|
|
-out:
|
|
- rcu_read_unlock();
|
|
-
|
|
- return ret;
|
|
-}
|
|
-EXPORT_SYMBOL(filemap_map_pages);
|
|
-
|
|
-const struct vm_operations_struct generic_file_vm_ops = {
|
|
- .fault = filemap_fault,
|
|
- .map_pages = filemap_map_pages,
|
|
- .page_mkwrite = filemap_page_mkwrite,
|
|
-};
|
|
-
|
|
-/* This is used for a general mmap of a disk file */
|
|
-
|
|
-int generic_file_mmap(struct file *file, struct vm_area_struct *vma)
|
|
-{
|
|
- struct address_space *mapping = file->f_mapping;
|
|
-
|
|
- if (!mapping->a_ops->read_folio)
|
|
- return -ENOEXEC;
|
|
- file_accessed(file);
|
|
- vma->vm_ops = &generic_file_vm_ops;
|
|
- return 0;
|
|
-}
|
|
-
|
|
-int generic_file_mmap_prepare(struct vm_area_desc *desc)
|
|
-{
|
|
- struct file *file = desc->file;
|
|
- struct address_space *mapping = file->f_mapping;
|
|
-
|
|
- if (!mapping->a_ops->read_folio)
|
|
- return -ENOEXEC;
|
|
- file_accessed(file);
|
|
- desc->vm_ops = &generic_file_vm_ops;
|
|
- return 0;
|
|
-}
|
|
-
|
|
-/*
|
|
- * This is for filesystems which do not implement ->writepage.
|
|
- */
|
|
-int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
|
|
-{
|
|
- if (vma_is_shared_maywrite(vma))
|
|
- return -EINVAL;
|
|
- return generic_file_mmap(file, vma);
|
|
-}
|
|
-
|
|
-int generic_file_readonly_mmap_prepare(struct vm_area_desc *desc)
|
|
-{
|
|
- if (is_shared_maywrite(&desc->vma_flags))
|
|
- return -EINVAL;
|
|
- return generic_file_mmap_prepare(desc);
|
|
-}
|
|
-#else
|
|
-int generic_file_mmap(struct file *file, struct vm_area_struct *vma)
|
|
-{
|
|
- return -ENOSYS;
|
|
-}
|
|
-int generic_file_mmap_prepare(struct vm_area_desc *desc)
|
|
-{
|
|
- return -ENOSYS;
|
|
-}
|
|
-int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
|
|
-{
|
|
- return -ENOSYS;
|
|
-}
|
|
-int generic_file_readonly_mmap_prepare(struct vm_area_desc *desc)
|
|
-{
|
|
- return -ENOSYS;
|
|
-}
|
|
-#endif /* CONFIG_MMU */
|
|
-
|
|
-EXPORT_SYMBOL(generic_file_mmap);
|
|
-EXPORT_SYMBOL(generic_file_mmap_prepare);
|
|
-EXPORT_SYMBOL(generic_file_readonly_mmap);
|
|
-EXPORT_SYMBOL(generic_file_readonly_mmap_prepare);
|
|
-
|
|
/*
|
|
* Warn about a page cache invalidation failure during a direct I/O write.
|
|
*/
|
|
diff --git a/mm/filemap_mmap.c b/mm/filemap_mmap.c
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..686e19d37bfe4707a46ec9b680fb657b6a5bb161
|
|
--- /dev/null
|
|
+++ b/mm/filemap_mmap.c
|
|
@@ -0,0 +1,876 @@
|
|
+// SPDX-License-Identifier: GPL-2.0-only
|
|
+/*
|
|
+ * linux/mm/filemap.c
|
|
+ *
|
|
+ * Copyright (C) 1994-1999 Linus Torvalds
|
|
+ */
|
|
+
|
|
+/*
|
|
+ * This file handles the generic file mmap semantics used by
|
|
+ * most "normal" filesystems (but you don't /have/ to use this:
|
|
+ * the NFS filesystem used to do this differently, for example)
|
|
+ */
|
|
+#include <linux/export.h>
|
|
+#include <linux/compiler.h>
|
|
+#include <linux/dax.h>
|
|
+#include <linux/fs.h>
|
|
+#include <linux/sched/signal.h>
|
|
+#include <linux/uaccess.h>
|
|
+#include <linux/capability.h>
|
|
+#include <linux/kernel_stat.h>
|
|
+#include <linux/gfp.h>
|
|
+#include <linux/mm.h>
|
|
+#include <linux/swap.h>
|
|
+#include <linux/leafops.h>
|
|
+#include <linux/syscalls.h>
|
|
+#include <linux/mman.h>
|
|
+#include <linux/pagemap.h>
|
|
+#include <linux/file.h>
|
|
+#include <linux/uio.h>
|
|
+#include <linux/error-injection.h>
|
|
+#include <linux/hash.h>
|
|
+#include <linux/writeback.h>
|
|
+#include <linux/backing-dev.h>
|
|
+#include <linux/folio_batch.h>
|
|
+#include <linux/security.h>
|
|
+#include <linux/cpuset.h>
|
|
+#include <linux/hugetlb.h>
|
|
+#include <linux/memcontrol.h>
|
|
+#include <linux/shmem_fs.h>
|
|
+#include <linux/rmap.h>
|
|
+#include <linux/delayacct.h>
|
|
+#include <linux/psi.h>
|
|
+#include <linux/ramfs.h>
|
|
+#include <linux/page_idle.h>
|
|
+#include <linux/migrate.h>
|
|
+#include <linux/pipe_fs_i.h>
|
|
+#include <linux/splice.h>
|
|
+#include <linux/rcupdate_wait.h>
|
|
+#include <linux/sched/mm.h>
|
|
+#include <linux/sysctl.h>
|
|
+#include <linux/pgalloc.h>
|
|
+
|
|
+#include <asm/tlbflush.h>
|
|
+#include "internal.h"
|
|
+#include "filemap_batch.h"
|
|
+#include "pagecache_wait.h"
|
|
+
|
|
+#ifdef CONFIG_STELA
|
|
+#include <stela/linux.h>
|
|
+#endif
|
|
+#include <trace/events/filemap.h>
|
|
+
|
|
+#ifdef CONFIG_MMU
|
|
+#define MMAP_LOTSAMISS (100)
|
|
+/*
|
|
+ * lock_folio_maybe_drop_mmap - lock the page, possibly dropping the mmap_lock
|
|
+ * @vmf - the vm_fault for this fault.
|
|
+ * @folio - the folio to lock.
|
|
+ * @fpin - the pointer to the file we may pin (or is already pinned).
|
|
+ *
|
|
+ * This works similar to lock_folio_or_retry in that it can drop the
|
|
+ * mmap_lock. It differs in that it actually returns the folio locked
|
|
+ * if it returns 1 and 0 if it couldn't lock the folio. If we did have
|
|
+ * to drop the mmap_lock then fpin will point to the pinned file and
|
|
+ * needs to be fput()'ed at a later point.
|
|
+ */
|
|
+static int lock_folio_maybe_drop_mmap(struct vm_fault *vmf, struct folio *folio,
|
|
+ struct file **fpin)
|
|
+{
|
|
+ if (folio_trylock(folio))
|
|
+ return 1;
|
|
+
|
|
+ /*
|
|
+ * NOTE! This will make us return with VM_FAULT_RETRY, but with
|
|
+ * the fault lock still held. That's how FAULT_FLAG_RETRY_NOWAIT
|
|
+ * is supposed to work. We have way too many special cases..
|
|
+ */
|
|
+ if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
|
|
+ return 0;
|
|
+
|
|
+ *fpin = maybe_unlock_mmap_for_io(vmf, *fpin);
|
|
+ if (vmf->flags & FAULT_FLAG_KILLABLE) {
|
|
+ if (__folio_lock_killable(folio)) {
|
|
+ /*
|
|
+ * We didn't have the right flags to drop the
|
|
+ * fault lock, but all fault_handlers only check
|
|
+ * for fatal signals if we return VM_FAULT_RETRY,
|
|
+ * so we need to drop the fault lock here and
|
|
+ * return 0 if we don't have a fpin.
|
|
+ */
|
|
+ if (*fpin == NULL)
|
|
+ release_fault_lock(vmf);
|
|
+ return 0;
|
|
+ }
|
|
+ } else
|
|
+ __folio_lock(folio);
|
|
+
|
|
+ return 1;
|
|
+}
|
|
+
|
|
+/*
|
|
+ * Synchronous readahead happens when we don't even find a page in the page
|
|
+ * cache at all. We don't want to perform IO under the mmap sem, so if we have
|
|
+ * to drop the mmap sem we return the file that was pinned in order for us to do
|
|
+ * that. If we didn't pin a file then we return NULL. The file that is
|
|
+ * returned needs to be fput()'ed when we're done with it.
|
|
+ */
|
|
+static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
|
|
+{
|
|
+ struct file *file = vmf->vma->vm_file;
|
|
+ struct file_ra_state *ra = &file->f_ra;
|
|
+ struct address_space *mapping = file->f_mapping;
|
|
+ DEFINE_READAHEAD(ractl, file, ra, mapping, vmf->pgoff);
|
|
+ struct file *fpin = NULL;
|
|
+ vm_flags_t vm_flags = vmf->vma->vm_flags;
|
|
+ bool force_thp_readahead = false;
|
|
+ unsigned int thp_order = 0;
|
|
+ unsigned short mmap_miss;
|
|
+
|
|
+ /* Use the readahead code, even if readahead is disabled */
|
|
+ if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && (vm_flags & VM_HUGEPAGE)) {
|
|
+ /*
|
|
+ * Cap max THP order at 2MB: this is the common PMD-sized
|
|
+ * hugepage size, and it avoids memory pressure from very
|
|
+ * large forced readahead when mapping_max_folio_order() is
|
|
+ * high (for example, 128MB with 64K base pages on arm64).
|
|
+ */
|
|
+ if (mapping_large_folio_support(mapping)) {
|
|
+ force_thp_readahead = true;
|
|
+ thp_order = min_t(unsigned int,
|
|
+ mapping_max_folio_order(mapping),
|
|
+ get_order(SZ_2M));
|
|
+ }
|
|
+ }
|
|
+
|
|
+ if (!force_thp_readahead) {
|
|
+ /*
|
|
+ * If we don't want any read-ahead, don't bother.
|
|
+ * VM_EXEC case below is already intended for random access.
|
|
+ */
|
|
+ if ((vm_flags & (VM_RAND_READ | VM_EXEC)) == VM_RAND_READ)
|
|
+ return fpin;
|
|
+
|
|
+ if (!ra->ra_pages)
|
|
+ return fpin;
|
|
+
|
|
+ if (vm_flags & VM_SEQ_READ) {
|
|
+ fpin = maybe_unlock_mmap_for_io(vmf, fpin);
|
|
+ page_cache_sync_ra(&ractl, ra->ra_pages);
|
|
+ return fpin;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ if (!(vm_flags & (VM_SEQ_READ | VM_EXEC))) {
|
|
+ /* Avoid banging the cache line if not needed */
|
|
+ mmap_miss = READ_ONCE(ra->mmap_miss);
|
|
+ if (mmap_miss < MMAP_LOTSAMISS * 10)
|
|
+ WRITE_ONCE(ra->mmap_miss, ++mmap_miss);
|
|
+
|
|
+ /*
|
|
+ * Do we miss much more than hit in this file? If so,
|
|
+ * stop bothering with read-ahead. It will only hurt.
|
|
+ */
|
|
+ if (mmap_miss > MMAP_LOTSAMISS)
|
|
+ return fpin;
|
|
+ }
|
|
+
|
|
+ if (force_thp_readahead) {
|
|
+ unsigned long folio_nr_pages = 1UL << thp_order;
|
|
+
|
|
+ fpin = maybe_unlock_mmap_for_io(vmf, fpin);
|
|
+ ractl._index &= ~(folio_nr_pages - 1);
|
|
+ ra->size = folio_nr_pages;
|
|
+ /*
|
|
+ * Fetch two folios so we get the chance to actually
|
|
+ * readahead, unless we've been told not to.
|
|
+ */
|
|
+ if (!(vm_flags & VM_RAND_READ))
|
|
+ ra->size *= 2;
|
|
+ ra->async_size = folio_nr_pages;
|
|
+ ra->order = thp_order;
|
|
+ page_cache_ra_order(&ractl, ra);
|
|
+ return fpin;
|
|
+ }
|
|
+
|
|
+ if (vm_flags & VM_EXEC) {
|
|
+ /*
|
|
+ * Allow arch to request a preferred minimum folio order for
|
|
+ * executable memory. This can often be beneficial to
|
|
+ * performance if (e.g.) arm64 can contpte-map the folio.
|
|
+ * Executable memory rarely benefits from readahead, due to its
|
|
+ * random access nature, so set async_size to 0.
|
|
+ *
|
|
+ * Limit to the boundaries of the VMA to avoid reading in any
|
|
+ * pad that might exist between sections, which would be a waste
|
|
+ * of memory.
|
|
+ */
|
|
+ struct vm_area_struct *vma = vmf->vma;
|
|
+ unsigned long start = vma->vm_pgoff;
|
|
+ unsigned long end = start + vma_pages(vma);
|
|
+ unsigned long ra_end;
|
|
+
|
|
+ ra->order = exec_folio_order();
|
|
+ ra->start = round_down(vmf->pgoff, 1UL << ra->order);
|
|
+ ra->start = max(ra->start, start);
|
|
+ ra_end = round_up(ra->start + ra->ra_pages, 1UL << ra->order);
|
|
+ ra_end = min(ra_end, end);
|
|
+ ra->size = ra_end - ra->start;
|
|
+ ra->async_size = 0;
|
|
+ } else {
|
|
+ /*
|
|
+ * mmap read-around
|
|
+ */
|
|
+ ra->start = max_t(long, 0, vmf->pgoff - ra->ra_pages / 2);
|
|
+ ra->size = ra->ra_pages;
|
|
+ ra->async_size = ra->ra_pages / 4;
|
|
+ ra->order = 0;
|
|
+ }
|
|
+
|
|
+ fpin = maybe_unlock_mmap_for_io(vmf, fpin);
|
|
+ ractl._index = ra->start;
|
|
+ page_cache_ra_order(&ractl, ra);
|
|
+ return fpin;
|
|
+}
|
|
+
|
|
+/*
|
|
+ * Asynchronous readahead happens when we find the page and PG_readahead,
|
|
+ * so we want to possibly extend the readahead further. We return the file that
|
|
+ * was pinned if we have to drop the mmap_lock in order to do IO.
|
|
+ */
|
|
+static struct file *do_async_mmap_readahead(struct vm_fault *vmf,
|
|
+ struct folio *folio)
|
|
+{
|
|
+ struct file *file = vmf->vma->vm_file;
|
|
+ struct file_ra_state *ra = &file->f_ra;
|
|
+ DEFINE_READAHEAD(ractl, file, ra, file->f_mapping, vmf->pgoff);
|
|
+ struct file *fpin = NULL;
|
|
+ unsigned short mmap_miss;
|
|
+
|
|
+ /* If we don't want any read-ahead, don't bother */
|
|
+ if (vmf->vma->vm_flags & VM_RAND_READ || !ra->ra_pages)
|
|
+ return fpin;
|
|
+
|
|
+ /*
|
|
+ * If the folio is locked, we're likely racing against another fault.
|
|
+ * Don't touch the mmap_miss counter to avoid decreasing it multiple
|
|
+ * times for a single folio and break the balance with mmap_miss
|
|
+ * increase in do_sync_mmap_readahead().
|
|
+ *
|
|
+ * VM_SEQ_READ and VM_EXEC mappings skip the mmap_miss increment in
|
|
+ * do_sync_mmap_readahead(), so skip the decrement here as well to
|
|
+ * keep the counter symmetric.
|
|
+ */
|
|
+ if (likely(!folio_test_locked(folio)) &&
|
|
+ !(vmf->vma->vm_flags & (VM_SEQ_READ | VM_EXEC))) {
|
|
+ mmap_miss = READ_ONCE(ra->mmap_miss);
|
|
+ if (mmap_miss)
|
|
+ WRITE_ONCE(ra->mmap_miss, --mmap_miss);
|
|
+ }
|
|
+
|
|
+ if (folio_test_readahead(folio)) {
|
|
+ fpin = maybe_unlock_mmap_for_io(vmf, fpin);
|
|
+ page_cache_async_ra(&ractl, folio, ra->ra_pages);
|
|
+ }
|
|
+ return fpin;
|
|
+}
|
|
+
|
|
+static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf)
|
|
+{
|
|
+#ifdef CONFIG_STELA
|
|
+ /* Native client faults supply a reserved address, with no Linux PTE. */
|
|
+ if ((vmf->vma->vm_flags & VM_LOCKED) && (vmf->flags & FAULT_FLAG_ORIG_PTE_VALID)) {
|
|
+ stela_linux_unsupported("file fault needs a Linux page table");
|
|
+ return VM_FAULT_SIGBUS;
|
|
+ }
|
|
+ return 0;
|
|
+#else
|
|
+ struct vm_area_struct *vma = vmf->vma;
|
|
+ vm_fault_t ret = 0;
|
|
+ pte_t *ptep;
|
|
+
|
|
+ /*
|
|
+ * We might have COW'ed a pagecache folio and might now have an mlocked
|
|
+ * anon folio mapped. The original pagecache folio is not mlocked and
|
|
+ * might have been evicted. During a read+clear/modify/write update of
|
|
+ * the PTE, such as done in do_numa_page()/change_pte_range(), we
|
|
+ * temporarily clear the PTE under PT lock and might detect it here as
|
|
+ * "none" when not holding the PT lock.
|
|
+ *
|
|
+ * Not rechecking the PTE under PT lock could result in an unexpected
|
|
+ * major fault in an mlock'ed region. Recheck only for this special
|
|
+ * scenario while holding the PT lock, to not degrade non-mlocked
|
|
+ * scenarios. Recheck the PTE without PT lock firstly, thereby reducing
|
|
+ * the number of times we hold PT lock.
|
|
+ */
|
|
+ if (!(vma->vm_flags & VM_LOCKED))
|
|
+ return 0;
|
|
+
|
|
+ if (!(vmf->flags & FAULT_FLAG_ORIG_PTE_VALID))
|
|
+ return 0;
|
|
+
|
|
+ ptep = pte_offset_map_ro_nolock(vma->vm_mm, vmf->pmd, vmf->address,
|
|
+ &vmf->ptl);
|
|
+ if (unlikely(!ptep))
|
|
+ return VM_FAULT_NOPAGE;
|
|
+
|
|
+ if (unlikely(!pte_none(ptep_get_lockless(ptep)))) {
|
|
+ ret = VM_FAULT_NOPAGE;
|
|
+ } else {
|
|
+ spin_lock(vmf->ptl);
|
|
+ if (unlikely(!pte_none(ptep_get(ptep))))
|
|
+ ret = VM_FAULT_NOPAGE;
|
|
+ spin_unlock(vmf->ptl);
|
|
+ }
|
|
+ pte_unmap(ptep);
|
|
+ return ret;
|
|
+#endif
|
|
+}
|
|
+
|
|
+/**
|
|
+ * filemap_fault - read in file data for page fault handling
|
|
+ * @vmf: struct vm_fault containing details of the fault
|
|
+ *
|
|
+ * filemap_fault() is invoked via the vma operations vector for a
|
|
+ * mapped memory region to read in file data during a page fault.
|
|
+ *
|
|
+ * The goto's are kind of ugly, but this streamlines the normal case of having
|
|
+ * it in the page cache, and handles the special cases reasonably without
|
|
+ * having a lot of duplicated code.
|
|
+ *
|
|
+ * vma->vm_mm->mmap_lock must be held on entry.
|
|
+ *
|
|
+ * If our return value has VM_FAULT_RETRY set, it's because the mmap_lock
|
|
+ * may be dropped before doing I/O or by lock_folio_maybe_drop_mmap().
|
|
+ *
|
|
+ * If our return value does not have VM_FAULT_RETRY set, the mmap_lock
|
|
+ * has not been released.
|
|
+ *
|
|
+ * We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
|
|
+ *
|
|
+ * Return: bitwise-OR of %VM_FAULT_ codes.
|
|
+ */
|
|
+vm_fault_t filemap_fault(struct vm_fault *vmf)
|
|
+{
|
|
+ int error;
|
|
+ struct file *file = vmf->vma->vm_file;
|
|
+ struct file *fpin = NULL;
|
|
+ struct address_space *mapping = file->f_mapping;
|
|
+ struct inode *inode = mapping->host;
|
|
+ pgoff_t max_idx, index = vmf->pgoff;
|
|
+ struct folio *folio;
|
|
+ vm_fault_t ret = 0;
|
|
+ bool mapping_locked = false;
|
|
+
|
|
+ max_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
|
|
+ if (unlikely(index >= max_idx))
|
|
+ return VM_FAULT_SIGBUS;
|
|
+
|
|
+ trace_mm_filemap_fault(mapping, index);
|
|
+
|
|
+ /*
|
|
+ * Do we have something in the page cache already?
|
|
+ */
|
|
+ folio = filemap_get_folio(mapping, index);
|
|
+ if (likely(!IS_ERR(folio))) {
|
|
+ /*
|
|
+ * We found the page, so try async readahead before waiting for
|
|
+ * the lock.
|
|
+ */
|
|
+ if (!(vmf->flags & FAULT_FLAG_TRIED))
|
|
+ fpin = do_async_mmap_readahead(vmf, folio);
|
|
+ if (unlikely(!folio_test_uptodate(folio))) {
|
|
+ filemap_invalidate_lock_shared(mapping);
|
|
+ mapping_locked = true;
|
|
+ }
|
|
+ } else {
|
|
+ ret = filemap_fault_recheck_pte_none(vmf);
|
|
+ if (unlikely(ret))
|
|
+ return ret;
|
|
+
|
|
+ /* No page in the page cache at all */
|
|
+ count_vm_event(PGMAJFAULT);
|
|
+ count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
|
|
+ ret = VM_FAULT_MAJOR;
|
|
+ fpin = do_sync_mmap_readahead(vmf);
|
|
+retry_find:
|
|
+ /*
|
|
+ * See comment in filemap_create_folio() why we need
|
|
+ * invalidate_lock
|
|
+ */
|
|
+ if (!mapping_locked) {
|
|
+ filemap_invalidate_lock_shared(mapping);
|
|
+ mapping_locked = true;
|
|
+ }
|
|
+ folio = __filemap_get_folio(mapping, index,
|
|
+ FGP_CREAT|FGP_FOR_MMAP,
|
|
+ vmf->gfp_mask);
|
|
+ if (IS_ERR(folio)) {
|
|
+ if (fpin)
|
|
+ goto out_retry;
|
|
+ filemap_invalidate_unlock_shared(mapping);
|
|
+ return VM_FAULT_OOM;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ if (!lock_folio_maybe_drop_mmap(vmf, folio, &fpin))
|
|
+ goto out_retry;
|
|
+
|
|
+ /* Did it get truncated? */
|
|
+ if (unlikely(folio->mapping != mapping)) {
|
|
+ folio_unlock(folio);
|
|
+ folio_put(folio);
|
|
+ goto retry_find;
|
|
+ }
|
|
+ VM_BUG_ON_FOLIO(!folio_contains(folio, index), folio);
|
|
+
|
|
+ /*
|
|
+ * We have a locked folio in the page cache, now we need to check
|
|
+ * that it's up-to-date. If not, it is going to be due to an error,
|
|
+ * or because readahead was otherwise unable to retrieve it.
|
|
+ */
|
|
+ if (unlikely(!folio_test_uptodate(folio))) {
|
|
+ /*
|
|
+ * If the invalidate lock is not held, the folio was in cache
|
|
+ * and uptodate and now it is not. Strange but possible since we
|
|
+ * didn't hold the page lock all the time. Let's drop
|
|
+ * everything, get the invalidate lock and try again.
|
|
+ */
|
|
+ if (!mapping_locked) {
|
|
+ folio_unlock(folio);
|
|
+ folio_put(folio);
|
|
+ goto retry_find;
|
|
+ }
|
|
+
|
|
+ /*
|
|
+ * OK, the folio is really not uptodate. This can be because the
|
|
+ * VMA has the VM_RAND_READ flag set, or because an error
|
|
+ * arose. Let's read it in directly.
|
|
+ */
|
|
+ goto page_not_uptodate;
|
|
+ }
|
|
+
|
|
+ /*
|
|
+ * We've made it this far and we had to drop our mmap_lock, now is the
|
|
+ * time to return to the upper layer and have it re-find the vma and
|
|
+ * redo the fault.
|
|
+ */
|
|
+ if (fpin) {
|
|
+ folio_unlock(folio);
|
|
+ goto out_retry;
|
|
+ }
|
|
+ if (mapping_locked)
|
|
+ filemap_invalidate_unlock_shared(mapping);
|
|
+
|
|
+ /*
|
|
+ * Found the page and have a reference on it.
|
|
+ * We must recheck i_size under page lock.
|
|
+ */
|
|
+ max_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
|
|
+ if (unlikely(index >= max_idx)) {
|
|
+ folio_unlock(folio);
|
|
+ folio_put(folio);
|
|
+ return VM_FAULT_SIGBUS;
|
|
+ }
|
|
+
|
|
+ vmf->page = folio_file_page(folio, index);
|
|
+ return ret | VM_FAULT_LOCKED;
|
|
+
|
|
+page_not_uptodate:
|
|
+ /*
|
|
+ * Umm, take care of errors if the page isn't up-to-date.
|
|
+ * Try to re-read it _once_. We do this synchronously,
|
|
+ * because there really aren't any performance issues here
|
|
+ * and we need to check for errors.
|
|
+ */
|
|
+ fpin = maybe_unlock_mmap_for_io(vmf, fpin);
|
|
+ error = filemap_read_folio(file, mapping->a_ops->read_folio, folio);
|
|
+ if (fpin)
|
|
+ goto out_retry;
|
|
+ folio_put(folio);
|
|
+
|
|
+ if (!error || error == AOP_TRUNCATED_PAGE)
|
|
+ goto retry_find;
|
|
+ filemap_invalidate_unlock_shared(mapping);
|
|
+
|
|
+ return VM_FAULT_SIGBUS;
|
|
+
|
|
+out_retry:
|
|
+ /*
|
|
+ * We dropped the mmap_lock, we need to return to the fault handler to
|
|
+ * re-find the vma and come back and find our hopefully still populated
|
|
+ * page.
|
|
+ */
|
|
+ if (!IS_ERR(folio))
|
|
+ folio_put(folio);
|
|
+ if (mapping_locked)
|
|
+ filemap_invalidate_unlock_shared(mapping);
|
|
+ if (fpin)
|
|
+ fput(fpin);
|
|
+ return ret | VM_FAULT_RETRY;
|
|
+}
|
|
+EXPORT_SYMBOL(filemap_fault);
|
|
+
|
|
+#ifndef CONFIG_STELA
|
|
+static bool filemap_map_pmd(struct vm_fault *vmf, struct folio *folio,
|
|
+ pgoff_t start)
|
|
+{
|
|
+ struct mm_struct *mm = vmf->vma->vm_mm;
|
|
+
|
|
+ /* Huge page is mapped? No need to proceed. */
|
|
+ if (pmd_trans_huge(*vmf->pmd)) {
|
|
+ folio_unlock(folio);
|
|
+ folio_put(folio);
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ if (pmd_none(*vmf->pmd) && folio_test_pmd_mappable(folio)) {
|
|
+ struct page *page = folio_file_page(folio, start);
|
|
+ vm_fault_t ret = do_set_pmd(vmf, folio, page);
|
|
+ if (!ret) {
|
|
+ /* The page is mapped successfully, reference consumed. */
|
|
+ folio_unlock(folio);
|
|
+ return true;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ if (pmd_none(*vmf->pmd) && vmf->prealloc_pte)
|
|
+ pmd_install(mm, vmf->pmd, &vmf->prealloc_pte);
|
|
+
|
|
+ return false;
|
|
+}
|
|
+
|
|
+static struct folio *next_uptodate_folio(struct xa_state *xas,
|
|
+ struct address_space *mapping, pgoff_t end_pgoff)
|
|
+{
|
|
+ struct folio *folio = xas_next_entry(xas, end_pgoff);
|
|
+ unsigned long max_idx;
|
|
+
|
|
+ do {
|
|
+ if (!folio)
|
|
+ return NULL;
|
|
+ if (xas_retry(xas, folio))
|
|
+ continue;
|
|
+ if (xa_is_value(folio))
|
|
+ continue;
|
|
+ if (!folio_try_get(folio))
|
|
+ continue;
|
|
+ if (folio_test_locked(folio))
|
|
+ goto skip;
|
|
+ /* Has the page moved or been split? */
|
|
+ if (unlikely(folio != xas_reload(xas)))
|
|
+ goto skip;
|
|
+ if (!folio_test_uptodate(folio) || folio_test_readahead(folio))
|
|
+ goto skip;
|
|
+ if (!folio_trylock(folio))
|
|
+ goto skip;
|
|
+ if (folio->mapping != mapping)
|
|
+ goto unlock;
|
|
+ if (!folio_test_uptodate(folio))
|
|
+ goto unlock;
|
|
+ max_idx = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
|
|
+ if (xas->xa_index >= max_idx)
|
|
+ goto unlock;
|
|
+ return folio;
|
|
+unlock:
|
|
+ folio_unlock(folio);
|
|
+skip:
|
|
+ folio_put(folio);
|
|
+ } while ((folio = xas_next_entry(xas, end_pgoff)) != NULL);
|
|
+
|
|
+ return NULL;
|
|
+}
|
|
+
|
|
+/*
|
|
+ * Map page range [start_page, start_page + nr_pages) of folio.
|
|
+ * start_page is gotten from start by folio_page(folio, start)
|
|
+ */
|
|
+static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
|
|
+ struct folio *folio, unsigned long start,
|
|
+ unsigned long addr, unsigned int nr_pages,
|
|
+ unsigned long *rss, pgoff_t file_end)
|
|
+{
|
|
+ struct address_space *mapping = folio->mapping;
|
|
+ unsigned int ref_from_caller = 1;
|
|
+ vm_fault_t ret = 0;
|
|
+ struct page *page = folio_page(folio, start);
|
|
+ unsigned int count = 0;
|
|
+ pte_t *old_ptep = vmf->pte;
|
|
+ unsigned long addr0;
|
|
+
|
|
+ /*
|
|
+ * Map the large folio fully where possible:
|
|
+ *
|
|
+ * - The folio is fully within size of the file or belong
|
|
+ * to shmem/tmpfs;
|
|
+ * - The folio doesn't cross VMA boundary;
|
|
+ * - The folio doesn't cross page table boundary;
|
|
+ */
|
|
+ addr0 = addr - start * PAGE_SIZE;
|
|
+ if ((file_end >= folio_next_index(folio) || shmem_mapping(mapping)) &&
|
|
+ folio_within_vma(folio, vmf->vma) &&
|
|
+ (addr0 & PMD_MASK) == ((addr0 + folio_size(folio) - 1) & PMD_MASK)) {
|
|
+ vmf->pte -= start;
|
|
+ page -= start;
|
|
+ addr = addr0;
|
|
+ nr_pages = folio_nr_pages(folio);
|
|
+ }
|
|
+
|
|
+ do {
|
|
+ if (PageHWPoison(page + count))
|
|
+ goto skip;
|
|
+
|
|
+ /*
|
|
+ * NOTE: If there're PTE markers, we'll leave them to be
|
|
+ * handled in the specific fault path, and it'll prohibit the
|
|
+ * fault-around logic.
|
|
+ */
|
|
+ if (!pte_none(ptep_get(&vmf->pte[count])))
|
|
+ goto skip;
|
|
+
|
|
+ count++;
|
|
+ continue;
|
|
+skip:
|
|
+ if (count) {
|
|
+ set_pte_range(vmf, folio, page, count, addr);
|
|
+ *rss += count;
|
|
+ folio_ref_add(folio, count - ref_from_caller);
|
|
+ ref_from_caller = 0;
|
|
+ if (in_range(vmf->address, addr, count * PAGE_SIZE))
|
|
+ ret = VM_FAULT_NOPAGE;
|
|
+ }
|
|
+
|
|
+ count++;
|
|
+ page += count;
|
|
+ vmf->pte += count;
|
|
+ addr += count * PAGE_SIZE;
|
|
+ count = 0;
|
|
+ } while (--nr_pages > 0);
|
|
+
|
|
+ if (count) {
|
|
+ set_pte_range(vmf, folio, page, count, addr);
|
|
+ *rss += count;
|
|
+ folio_ref_add(folio, count - ref_from_caller);
|
|
+ ref_from_caller = 0;
|
|
+ if (in_range(vmf->address, addr, count * PAGE_SIZE))
|
|
+ ret = VM_FAULT_NOPAGE;
|
|
+ }
|
|
+
|
|
+ vmf->pte = old_ptep;
|
|
+ if (ref_from_caller)
|
|
+ /* Locked folios cannot get truncated. */
|
|
+ folio_ref_dec(folio);
|
|
+
|
|
+ return ret;
|
|
+}
|
|
+
|
|
+static vm_fault_t filemap_map_order0_folio(struct vm_fault *vmf,
|
|
+ struct folio *folio, unsigned long addr,
|
|
+ unsigned long *rss)
|
|
+{
|
|
+ vm_fault_t ret = 0;
|
|
+ struct page *page = &folio->page;
|
|
+
|
|
+ if (PageHWPoison(page))
|
|
+ goto out;
|
|
+
|
|
+ /*
|
|
+ * NOTE: If there're PTE markers, we'll leave them to be
|
|
+ * handled in the specific fault path, and it'll prohibit
|
|
+ * the fault-around logic.
|
|
+ */
|
|
+ if (!pte_none(ptep_get(vmf->pte)))
|
|
+ goto out;
|
|
+
|
|
+ if (vmf->address == addr)
|
|
+ ret = VM_FAULT_NOPAGE;
|
|
+
|
|
+ set_pte_range(vmf, folio, page, 1, addr);
|
|
+ (*rss)++;
|
|
+ return ret;
|
|
+
|
|
+out:
|
|
+ /* Locked folios cannot get truncated. */
|
|
+ folio_ref_dec(folio);
|
|
+ return ret;
|
|
+}
|
|
+
|
|
+vm_fault_t filemap_map_pages(struct vm_fault *vmf,
|
|
+ pgoff_t start_pgoff, pgoff_t end_pgoff)
|
|
+{
|
|
+ struct vm_area_struct *vma = vmf->vma;
|
|
+ struct file *file = vma->vm_file;
|
|
+ struct address_space *mapping = file->f_mapping;
|
|
+ pgoff_t file_end, last_pgoff = start_pgoff;
|
|
+ unsigned long addr;
|
|
+ XA_STATE(xas, &mapping->i_pages, start_pgoff);
|
|
+ struct folio *folio;
|
|
+ vm_fault_t ret = 0;
|
|
+ unsigned long rss = 0;
|
|
+ unsigned int nr_pages = 0, folio_type;
|
|
+
|
|
+ /*
|
|
+ * Recalculate end_pgoff based on file_end before calling
|
|
+ * next_uptodate_folio() to avoid races with concurrent
|
|
+ * truncation.
|
|
+ */
|
|
+ file_end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE) - 1;
|
|
+ end_pgoff = min(end_pgoff, file_end);
|
|
+
|
|
+ rcu_read_lock();
|
|
+ folio = next_uptodate_folio(&xas, mapping, end_pgoff);
|
|
+ if (!folio)
|
|
+ goto out;
|
|
+
|
|
+ /*
|
|
+ * Do not allow to map with PMD across i_size to preserve
|
|
+ * SIGBUS semantics.
|
|
+ *
|
|
+ * Make an exception for shmem/tmpfs that for long time
|
|
+ * intentionally mapped with PMDs across i_size.
|
|
+ */
|
|
+ if ((file_end >= folio_next_index(folio) || shmem_mapping(mapping)) &&
|
|
+ filemap_map_pmd(vmf, folio, start_pgoff)) {
|
|
+ ret = VM_FAULT_NOPAGE;
|
|
+ goto out;
|
|
+ }
|
|
+
|
|
+ addr = vma->vm_start + ((start_pgoff - vma->vm_pgoff) << PAGE_SHIFT);
|
|
+ vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, addr, &vmf->ptl);
|
|
+ if (!vmf->pte) {
|
|
+ folio_unlock(folio);
|
|
+ folio_put(folio);
|
|
+ goto out;
|
|
+ }
|
|
+
|
|
+ folio_type = mm_counter_file(folio);
|
|
+ do {
|
|
+ unsigned long end;
|
|
+ vm_fault_t map_ret;
|
|
+
|
|
+ addr += (xas.xa_index - last_pgoff) << PAGE_SHIFT;
|
|
+ vmf->pte += xas.xa_index - last_pgoff;
|
|
+ last_pgoff = xas.xa_index;
|
|
+ end = folio_next_index(folio) - 1;
|
|
+ nr_pages = min(end, end_pgoff) - xas.xa_index + 1;
|
|
+
|
|
+ if (!folio_test_large(folio)) {
|
|
+ map_ret = filemap_map_order0_folio(vmf, folio, addr,
|
|
+ &rss);
|
|
+ } else {
|
|
+ unsigned long start = xas.xa_index - folio->index;
|
|
+
|
|
+ map_ret = filemap_map_folio_range(vmf, folio, start,
|
|
+ addr, nr_pages, &rss,
|
|
+ file_end);
|
|
+ }
|
|
+ ret |= map_ret;
|
|
+
|
|
+ /*
|
|
+ * If there are too many folios that are recently evicted
|
|
+ * in a file, they will probably continue to be evicted.
|
|
+ * In such situation, read-ahead is only a waste of IO.
|
|
+ * Don't decrease mmap_miss in this scenario to make sure
|
|
+ * we can stop read-ahead.
|
|
+ *
|
|
+ * VM_SEQ_READ and VM_EXEC mappings skip the mmap_miss
|
|
+ * increment in do_sync_mmap_readahead(), so skip the
|
|
+ * decrement here as well to keep the counter symmetric.
|
|
+ */
|
|
+ if ((map_ret & VM_FAULT_NOPAGE) &&
|
|
+ !(vmf->flags & FAULT_FLAG_TRIED) &&
|
|
+ !folio_test_workingset(folio) &&
|
|
+ !(vma->vm_flags & (VM_SEQ_READ | VM_EXEC))) {
|
|
+ unsigned short mmap_miss;
|
|
+
|
|
+ mmap_miss = READ_ONCE(file->f_ra.mmap_miss);
|
|
+ if (mmap_miss)
|
|
+ WRITE_ONCE(file->f_ra.mmap_miss,
|
|
+ mmap_miss - 1);
|
|
+ }
|
|
+
|
|
+ folio_unlock(folio);
|
|
+ } while ((folio = next_uptodate_folio(&xas, mapping, end_pgoff)) != NULL);
|
|
+ add_mm_counter(vma->vm_mm, folio_type, rss);
|
|
+ pte_unmap_unlock(vmf->pte, vmf->ptl);
|
|
+ trace_mm_filemap_map_pages(mapping, start_pgoff, end_pgoff);
|
|
+out:
|
|
+ rcu_read_unlock();
|
|
+
|
|
+ return ret;
|
|
+}
|
|
+EXPORT_SYMBOL(filemap_map_pages);
|
|
+#endif
|
|
+
|
|
+const struct vm_operations_struct generic_file_vm_ops = {
|
|
+ .fault = filemap_fault,
|
|
+#ifndef CONFIG_STELA
|
|
+ .map_pages = filemap_map_pages,
|
|
+#endif
|
|
+ .page_mkwrite = filemap_page_mkwrite,
|
|
+};
|
|
+
|
|
+/* This is used for a general mmap of a disk file */
|
|
+
|
|
+int generic_file_mmap(struct file *file, struct vm_area_struct *vma)
|
|
+{
|
|
+ struct address_space *mapping = file->f_mapping;
|
|
+
|
|
+ if (!mapping->a_ops->read_folio)
|
|
+ return -ENOEXEC;
|
|
+ file_accessed(file);
|
|
+ vma->vm_ops = &generic_file_vm_ops;
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+int generic_file_mmap_prepare(struct vm_area_desc *desc)
|
|
+{
|
|
+ struct file *file = desc->file;
|
|
+ struct address_space *mapping = file->f_mapping;
|
|
+
|
|
+ if (!mapping->a_ops->read_folio)
|
|
+ return -ENOEXEC;
|
|
+ file_accessed(file);
|
|
+ desc->vm_ops = &generic_file_vm_ops;
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+/*
|
|
+ * This is for filesystems which do not implement ->writepage.
|
|
+ */
|
|
+int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
|
|
+{
|
|
+ if (vma_is_shared_maywrite(vma))
|
|
+ return -EINVAL;
|
|
+ return generic_file_mmap(file, vma);
|
|
+}
|
|
+
|
|
+int generic_file_readonly_mmap_prepare(struct vm_area_desc *desc)
|
|
+{
|
|
+ if (is_shared_maywrite(&desc->vma_flags))
|
|
+ return -EINVAL;
|
|
+ return generic_file_mmap_prepare(desc);
|
|
+}
|
|
+#else
|
|
+int generic_file_mmap(struct file *file, struct vm_area_struct *vma)
|
|
+{
|
|
+ return -ENOSYS;
|
|
+}
|
|
+int generic_file_mmap_prepare(struct vm_area_desc *desc)
|
|
+{
|
|
+ return -ENOSYS;
|
|
+}
|
|
+int generic_file_readonly_mmap(struct file *file, struct vm_area_struct *vma)
|
|
+{
|
|
+ return -ENOSYS;
|
|
+}
|
|
+int generic_file_readonly_mmap_prepare(struct vm_area_desc *desc)
|
|
+{
|
|
+ return -ENOSYS;
|
|
+}
|
|
+#endif /* CONFIG_MMU */
|
|
+
|
|
+EXPORT_SYMBOL(generic_file_mmap);
|
|
+EXPORT_SYMBOL(generic_file_mmap_prepare);
|
|
+EXPORT_SYMBOL(generic_file_readonly_mmap);
|
|
+EXPORT_SYMBOL(generic_file_readonly_mmap_prepare);
|