749 lines
23 KiB
Diff
749 lines
23 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 04:01:16 +0000
|
|
Subject: [PATCH] refactor(mm): link original final page cache truncate
|
|
|
|
---
|
|
mm/filemap_batch.c | 359 +++++++++++++++++++++++++++++++++++++++++++++
|
|
mm/truncate.c | 352 --------------------------------------------
|
|
2 files changed, 359 insertions(+), 352 deletions(-)
|
|
|
|
diff --git a/mm/filemap_batch.c b/mm/filemap_batch.c
|
|
index ee1c83ce0e533f6223ba0449cad6efb1fc5fc975..b814f937080f1822fcdaf4b2f6f343f17df3c66a 100644
|
|
--- a/mm/filemap_batch.c
|
|
+++ b/mm/filemap_batch.c
|
|
@@ -5,7 +5,9 @@
|
|
#include <linux/dax.h>
|
|
#include <linux/error-injection.h>
|
|
#include <linux/folio_batch.h>
|
|
+#include <linux/highmem.h>
|
|
#include <linux/page_idle.h>
|
|
+#include <linux/rmap.h>
|
|
#include <linux/sched.h>
|
|
#include <linux/sched/mm.h>
|
|
#include <linux/shmem_fs.h>
|
|
@@ -1290,3 +1292,360 @@ unsigned long invalidate_mapping_pages(struct address_space *mapping,
|
|
return mapping_try_invalidate(mapping, start, end, NULL);
|
|
}
|
|
EXPORT_SYMBOL(invalidate_mapping_pages);
|
|
+
|
|
+/*
|
|
+ * If truncate cannot remove the fs-private metadata from the page, the page
|
|
+ * becomes orphaned. It will be left on the LRU and may even be mapped into
|
|
+ * user pagetables if we're racing with filemap_fault().
|
|
+ *
|
|
+ * We need to bail out if page->mapping is no longer equal to the original
|
|
+ * mapping. This happens a) when the VM reclaimed the page while we waited on
|
|
+ * its lock, b) when a concurrent invalidate_mapping_pages got there first and
|
|
+ * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
|
|
+ */
|
|
+static void truncate_cleanup_folio(struct folio *folio)
|
|
+{
|
|
+#ifdef CONFIG_STELA
|
|
+ /* Native file mappings live in i_mmap and have no Linux PTE mapcount. */
|
|
+ unmap_mapping_folio(folio);
|
|
+#else
|
|
+ if (folio_mapped(folio))
|
|
+ unmap_mapping_folio(folio);
|
|
+#endif
|
|
+
|
|
+ if (folio_needs_release(folio))
|
|
+ folio_invalidate(folio, 0, folio_size(folio));
|
|
+
|
|
+ /*
|
|
+ * Some filesystems seem to re-dirty the page even after
|
|
+ * the VM has canceled the dirty bit (eg ext3 journaling).
|
|
+ * Hence dirty accounting check is placed after invalidation.
|
|
+ */
|
|
+ folio_cancel_dirty(folio);
|
|
+}
|
|
+
|
|
+int truncate_inode_folio(struct address_space *mapping, struct folio *folio)
|
|
+{
|
|
+ if (folio->mapping != mapping)
|
|
+ return -EIO;
|
|
+
|
|
+ truncate_cleanup_folio(folio);
|
|
+ filemap_remove_folio(folio);
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+static int folio_split_or_unmap(struct folio *folio, struct page *split_at,
|
|
+ unsigned long min_order)
|
|
+{
|
|
+ int ret;
|
|
+
|
|
+ ret = folio_split(folio, min_order, split_at, NULL);
|
|
+
|
|
+ /*
|
|
+ * If the split fails, unmap the folio, so it will be refaulted
|
|
+ * with PTEs to respect SIGBUS semantics.
|
|
+ *
|
|
+ * Make an exception for shmem/tmpfs that for long time
|
|
+ * intentionally mapped with PMDs across i_size.
|
|
+ */
|
|
+ if (ret && !shmem_mapping(folio->mapping)) {
|
|
+#ifdef CONFIG_STELA
|
|
+ unmap_mapping_folio(folio);
|
|
+#else
|
|
+ try_to_unmap(folio, TTU_SYNC | TTU_SPLIT_HUGE_PMD | TTU_IGNORE_MLOCK);
|
|
+ WARN_ON(folio_mapped(folio));
|
|
+#endif
|
|
+ }
|
|
+
|
|
+ return ret;
|
|
+}
|
|
+
|
|
+/*
|
|
+ * Handle partial folios. The folio may be entirely within the
|
|
+ * range if a split has raced with us. If not, we zero the part of the
|
|
+ * folio that's within the [start, end] range, and then split the folio if
|
|
+ * it's large. split_page_range() will discard pages which now lie beyond
|
|
+ * i_size, and we rely on the caller to discard pages which lie within a
|
|
+ * newly created hole.
|
|
+ *
|
|
+ * Returns false if splitting failed so the caller can avoid
|
|
+ * discarding the entire folio which is stubbornly unsplit.
|
|
+ */
|
|
+bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end)
|
|
+{
|
|
+ loff_t pos = folio_pos(folio);
|
|
+ size_t size = folio_size(folio);
|
|
+ unsigned int offset, length;
|
|
+ struct page *split_at, *split_at2;
|
|
+ unsigned int min_order;
|
|
+
|
|
+ if (pos < start)
|
|
+ offset = start - pos;
|
|
+ else
|
|
+ offset = 0;
|
|
+ if (pos + size <= (u64)end)
|
|
+ length = size - offset;
|
|
+ else
|
|
+ length = end + 1 - pos - offset;
|
|
+
|
|
+ folio_wait_writeback(folio);
|
|
+ if (length == size) {
|
|
+ truncate_inode_folio(folio->mapping, folio);
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ /*
|
|
+ * We may be zeroing pages we're about to discard, but it avoids
|
|
+ * doing a complex calculation here, and then doing the zeroing
|
|
+ * anyway if the page split fails.
|
|
+ */
|
|
+ if (!mapping_inaccessible(folio->mapping))
|
|
+ folio_zero_range(folio, offset, length);
|
|
+
|
|
+ if (folio_needs_release(folio))
|
|
+ folio_invalidate(folio, offset, length);
|
|
+ if (!folio_test_large(folio))
|
|
+ return true;
|
|
+
|
|
+ min_order = mapping_min_folio_order(folio->mapping);
|
|
+ split_at = folio_page(folio, PAGE_ALIGN_DOWN(offset) / PAGE_SIZE);
|
|
+ if (!folio_split_or_unmap(folio, split_at, min_order)) {
|
|
+ /*
|
|
+ * try to split at offset + length to make sure folios within
|
|
+ * the range can be dropped, especially to avoid memory waste
|
|
+ * for shmem truncate
|
|
+ */
|
|
+ struct folio *folio2;
|
|
+
|
|
+ if (offset + length == size)
|
|
+ goto no_split;
|
|
+
|
|
+ split_at2 = folio_page(folio,
|
|
+ PAGE_ALIGN_DOWN(offset + length) / PAGE_SIZE);
|
|
+ folio2 = page_folio(split_at2);
|
|
+
|
|
+ if (!folio_try_get(folio2))
|
|
+ goto no_split;
|
|
+
|
|
+ if (!folio_test_large(folio2))
|
|
+ goto out;
|
|
+
|
|
+ if (!folio_trylock(folio2))
|
|
+ goto out;
|
|
+
|
|
+ /* make sure folio2 is large and does not change its mapping */
|
|
+ if (folio_test_large(folio2) &&
|
|
+ folio2->mapping == folio->mapping)
|
|
+ folio_split_or_unmap(folio2, split_at2, min_order);
|
|
+
|
|
+ folio_unlock(folio2);
|
|
+out:
|
|
+ folio_put(folio2);
|
|
+no_split:
|
|
+ return true;
|
|
+ }
|
|
+ if (folio_test_dirty(folio))
|
|
+ return false;
|
|
+ truncate_inode_folio(folio->mapping, folio);
|
|
+ return true;
|
|
+}
|
|
+
|
|
+/*
|
|
+ * Used to get rid of pages on hardware memory corruption.
|
|
+ */
|
|
+int generic_error_remove_folio(struct address_space *mapping,
|
|
+ struct folio *folio)
|
|
+{
|
|
+ if (!mapping)
|
|
+ return -EINVAL;
|
|
+ /*
|
|
+ * Only punch for normal data pages for now.
|
|
+ * Handling other types like directories would need more auditing.
|
|
+ */
|
|
+ if (!S_ISREG(mapping->host->i_mode))
|
|
+ return -EIO;
|
|
+ return truncate_inode_folio(mapping, folio);
|
|
+}
|
|
+EXPORT_SYMBOL(generic_error_remove_folio);
|
|
+
|
|
+/**
|
|
+ * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets
|
|
+ * @mapping: mapping to truncate
|
|
+ * @lstart: offset from which to truncate
|
|
+ * @lend: offset to which to truncate (inclusive)
|
|
+ *
|
|
+ * Truncate the page cache, removing the pages that are between
|
|
+ * specified offsets (and zeroing out partial pages
|
|
+ * if lstart or lend + 1 is not page aligned).
|
|
+ *
|
|
+ * Truncate takes two passes - the first pass is nonblocking. It will not
|
|
+ * block on page locks and it will not block on writeback. The second pass
|
|
+ * will wait. This is to prevent as much IO as possible in the affected region.
|
|
+ * The first pass will remove most pages, so the search cost of the second pass
|
|
+ * is low.
|
|
+ *
|
|
+ * We pass down the cache-hot hint to the page freeing code. Even if the
|
|
+ * mapping is large, it is probably the case that the final pages are the most
|
|
+ * recently touched, and freeing happens in ascending file offset order.
|
|
+ *
|
|
+ * Note that since ->invalidate_folio() accepts range to invalidate
|
|
+ * truncate_inode_pages_range is able to handle cases where lend + 1 is not
|
|
+ * page aligned properly.
|
|
+ */
|
|
+void truncate_inode_pages_range(struct address_space *mapping,
|
|
+ loff_t lstart, uoff_t lend)
|
|
+{
|
|
+ pgoff_t start; /* inclusive */
|
|
+ pgoff_t end; /* exclusive */
|
|
+ struct folio_batch fbatch;
|
|
+ pgoff_t indices[FOLIO_BATCH_SIZE];
|
|
+ pgoff_t index;
|
|
+ int i;
|
|
+ struct folio *folio;
|
|
+ bool same_folio;
|
|
+
|
|
+ if (mapping_empty(mapping))
|
|
+ return;
|
|
+
|
|
+ /*
|
|
+ * 'start' and 'end' always covers the range of pages to be fully
|
|
+ * truncated. Partial pages are covered with 'partial_start' at the
|
|
+ * start of the range and 'partial_end' at the end of the range.
|
|
+ * Note that 'end' is exclusive while 'lend' is inclusive.
|
|
+ */
|
|
+ start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
|
+ if (lend == -1)
|
|
+ /*
|
|
+ * lend == -1 indicates end-of-file so we have to set 'end'
|
|
+ * to the highest possible pgoff_t and since the type is
|
|
+ * unsigned we're using -1.
|
|
+ */
|
|
+ end = -1;
|
|
+ else
|
|
+ end = (lend + 1) >> PAGE_SHIFT;
|
|
+
|
|
+ folio_batch_init(&fbatch);
|
|
+ index = start;
|
|
+ while (index < end && find_lock_entries(mapping, &index, end - 1,
|
|
+ &fbatch, indices)) {
|
|
+ truncate_folio_batch_exceptionals(mapping, &fbatch, indices);
|
|
+ for (i = 0; i < folio_batch_count(&fbatch); i++)
|
|
+ truncate_cleanup_folio(fbatch.folios[i]);
|
|
+ delete_from_page_cache_batch(mapping, &fbatch);
|
|
+ for (i = 0; i < folio_batch_count(&fbatch); i++)
|
|
+ folio_unlock(fbatch.folios[i]);
|
|
+ folio_batch_release(&fbatch);
|
|
+ cond_resched();
|
|
+ }
|
|
+
|
|
+ same_folio = (lstart >> PAGE_SHIFT) == (lend >> PAGE_SHIFT);
|
|
+ folio = __filemap_get_folio(mapping, lstart >> PAGE_SHIFT, FGP_LOCK, 0);
|
|
+ if (!IS_ERR(folio)) {
|
|
+ same_folio = lend < folio_next_pos(folio);
|
|
+ if (!truncate_inode_partial_folio(folio, lstart, lend)) {
|
|
+ start = folio_next_index(folio);
|
|
+ if (same_folio)
|
|
+ end = folio->index;
|
|
+ }
|
|
+ folio_unlock(folio);
|
|
+ folio_put(folio);
|
|
+ folio = NULL;
|
|
+ }
|
|
+
|
|
+ if (!same_folio) {
|
|
+ folio = __filemap_get_folio(mapping, lend >> PAGE_SHIFT,
|
|
+ FGP_LOCK, 0);
|
|
+ if (!IS_ERR(folio)) {
|
|
+ if (!truncate_inode_partial_folio(folio, lstart, lend))
|
|
+ end = folio->index;
|
|
+ folio_unlock(folio);
|
|
+ folio_put(folio);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ index = start;
|
|
+ while (index < end) {
|
|
+ cond_resched();
|
|
+ if (!find_get_entries(mapping, &index, end - 1, &fbatch,
|
|
+ indices)) {
|
|
+ /* If all gone from start onwards, we're done */
|
|
+ if (index == start)
|
|
+ break;
|
|
+ /* Otherwise restart to make sure all gone */
|
|
+ index = start;
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ for (i = 0; i < folio_batch_count(&fbatch); i++) {
|
|
+ struct folio *folio = fbatch.folios[i];
|
|
+
|
|
+ /* We rely upon deletion not changing folio->index */
|
|
+
|
|
+ if (xa_is_value(folio))
|
|
+ continue;
|
|
+
|
|
+ folio_lock(folio);
|
|
+ VM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio);
|
|
+ folio_wait_writeback(folio);
|
|
+ truncate_inode_folio(mapping, folio);
|
|
+ folio_unlock(folio);
|
|
+ }
|
|
+ truncate_folio_batch_exceptionals(mapping, &fbatch, indices);
|
|
+ folio_batch_release(&fbatch);
|
|
+ }
|
|
+}
|
|
+EXPORT_SYMBOL(truncate_inode_pages_range);
|
|
+
|
|
+/**
|
|
+ * truncate_inode_pages - truncate *all* the pages from an offset
|
|
+ * @mapping: mapping to truncate
|
|
+ * @lstart: offset from which to truncate
|
|
+ *
|
|
+ * Called under (and serialised by) inode->i_rwsem and
|
|
+ * mapping->invalidate_lock.
|
|
+ *
|
|
+ * Note: When this function returns, there can be a page in the process of
|
|
+ * deletion (inside __filemap_remove_folio()) in the specified range. Thus
|
|
+ * mapping->nrpages can be non-zero when this function returns even after
|
|
+ * truncation of the whole mapping.
|
|
+ */
|
|
+void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
|
|
+{
|
|
+ truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
|
|
+}
|
|
+EXPORT_SYMBOL(truncate_inode_pages);
|
|
+
|
|
+/**
|
|
+ * truncate_inode_pages_final - truncate *all* pages before inode dies
|
|
+ * @mapping: mapping to truncate
|
|
+ *
|
|
+ * Called under (and serialized by) inode->i_rwsem.
|
|
+ *
|
|
+ * Filesystems have to use this in the .evict_inode path to inform the
|
|
+ * VM that this is the final truncate and the inode is going away.
|
|
+ */
|
|
+void truncate_inode_pages_final(struct address_space *mapping)
|
|
+{
|
|
+ /*
|
|
+ * Page reclaim can not participate in regular inode lifetime
|
|
+ * management (can't call iput()) and thus can race with the
|
|
+ * inode teardown. Tell it when the address space is exiting,
|
|
+ * so that it does not install eviction information after the
|
|
+ * final truncate has begun.
|
|
+ */
|
|
+ mapping_set_exiting(mapping);
|
|
+
|
|
+ if (!mapping_empty(mapping)) {
|
|
+ /*
|
|
+ * As truncation uses a lockless tree lookup, cycle
|
|
+ * the tree lock to make sure any ongoing tree
|
|
+ * modification that does not see AS_EXITING is
|
|
+ * completed before starting the final truncate.
|
|
+ */
|
|
+ xa_lock_irq(&mapping->i_pages);
|
|
+ xa_unlock_irq(&mapping->i_pages);
|
|
+ }
|
|
+
|
|
+ truncate_inode_pages(mapping, 0);
|
|
+}
|
|
+EXPORT_SYMBOL(truncate_inode_pages_final);
|
|
diff --git a/mm/truncate.c b/mm/truncate.c
|
|
index 61878eacd40b8e550446b89ead0371c1ef836633..5beec9a9b035bc82ed9f50613a2304c72ad602ff 100644
|
|
--- a/mm/truncate.c
|
|
+++ b/mm/truncate.c
|
|
@@ -24,358 +24,6 @@
|
|
#include "internal.h"
|
|
#include "filemap_batch.h"
|
|
|
|
-/*
|
|
- * If truncate cannot remove the fs-private metadata from the page, the page
|
|
- * becomes orphaned. It will be left on the LRU and may even be mapped into
|
|
- * user pagetables if we're racing with filemap_fault().
|
|
- *
|
|
- * We need to bail out if page->mapping is no longer equal to the original
|
|
- * mapping. This happens a) when the VM reclaimed the page while we waited on
|
|
- * its lock, b) when a concurrent invalidate_mapping_pages got there first and
|
|
- * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
|
|
- */
|
|
-static void truncate_cleanup_folio(struct folio *folio)
|
|
-{
|
|
- if (folio_mapped(folio))
|
|
- unmap_mapping_folio(folio);
|
|
-
|
|
- if (folio_needs_release(folio))
|
|
- folio_invalidate(folio, 0, folio_size(folio));
|
|
-
|
|
- /*
|
|
- * Some filesystems seem to re-dirty the page even after
|
|
- * the VM has canceled the dirty bit (eg ext3 journaling).
|
|
- * Hence dirty accounting check is placed after invalidation.
|
|
- */
|
|
- folio_cancel_dirty(folio);
|
|
-}
|
|
-
|
|
-int truncate_inode_folio(struct address_space *mapping, struct folio *folio)
|
|
-{
|
|
- if (folio->mapping != mapping)
|
|
- return -EIO;
|
|
-
|
|
- truncate_cleanup_folio(folio);
|
|
- filemap_remove_folio(folio);
|
|
- return 0;
|
|
-}
|
|
-
|
|
-static int folio_split_or_unmap(struct folio *folio, struct page *split_at,
|
|
- unsigned long min_order)
|
|
-{
|
|
- enum ttu_flags ttu_flags =
|
|
- TTU_SYNC |
|
|
- TTU_SPLIT_HUGE_PMD |
|
|
- TTU_IGNORE_MLOCK;
|
|
- int ret;
|
|
-
|
|
- ret = folio_split(folio, min_order, split_at, NULL);
|
|
-
|
|
- /*
|
|
- * If the split fails, unmap the folio, so it will be refaulted
|
|
- * with PTEs to respect SIGBUS semantics.
|
|
- *
|
|
- * Make an exception for shmem/tmpfs that for long time
|
|
- * intentionally mapped with PMDs across i_size.
|
|
- */
|
|
- if (ret && !shmem_mapping(folio->mapping)) {
|
|
- try_to_unmap(folio, ttu_flags);
|
|
- WARN_ON(folio_mapped(folio));
|
|
- }
|
|
-
|
|
- return ret;
|
|
-}
|
|
-
|
|
-/*
|
|
- * Handle partial folios. The folio may be entirely within the
|
|
- * range if a split has raced with us. If not, we zero the part of the
|
|
- * folio that's within the [start, end] range, and then split the folio if
|
|
- * it's large. split_page_range() will discard pages which now lie beyond
|
|
- * i_size, and we rely on the caller to discard pages which lie within a
|
|
- * newly created hole.
|
|
- *
|
|
- * Returns false if splitting failed so the caller can avoid
|
|
- * discarding the entire folio which is stubbornly unsplit.
|
|
- */
|
|
-bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end)
|
|
-{
|
|
- loff_t pos = folio_pos(folio);
|
|
- size_t size = folio_size(folio);
|
|
- unsigned int offset, length;
|
|
- struct page *split_at, *split_at2;
|
|
- unsigned int min_order;
|
|
-
|
|
- if (pos < start)
|
|
- offset = start - pos;
|
|
- else
|
|
- offset = 0;
|
|
- if (pos + size <= (u64)end)
|
|
- length = size - offset;
|
|
- else
|
|
- length = end + 1 - pos - offset;
|
|
-
|
|
- folio_wait_writeback(folio);
|
|
- if (length == size) {
|
|
- truncate_inode_folio(folio->mapping, folio);
|
|
- return true;
|
|
- }
|
|
-
|
|
- /*
|
|
- * We may be zeroing pages we're about to discard, but it avoids
|
|
- * doing a complex calculation here, and then doing the zeroing
|
|
- * anyway if the page split fails.
|
|
- */
|
|
- if (!mapping_inaccessible(folio->mapping))
|
|
- folio_zero_range(folio, offset, length);
|
|
-
|
|
- if (folio_needs_release(folio))
|
|
- folio_invalidate(folio, offset, length);
|
|
- if (!folio_test_large(folio))
|
|
- return true;
|
|
-
|
|
- min_order = mapping_min_folio_order(folio->mapping);
|
|
- split_at = folio_page(folio, PAGE_ALIGN_DOWN(offset) / PAGE_SIZE);
|
|
- if (!folio_split_or_unmap(folio, split_at, min_order)) {
|
|
- /*
|
|
- * try to split at offset + length to make sure folios within
|
|
- * the range can be dropped, especially to avoid memory waste
|
|
- * for shmem truncate
|
|
- */
|
|
- struct folio *folio2;
|
|
-
|
|
- if (offset + length == size)
|
|
- goto no_split;
|
|
-
|
|
- split_at2 = folio_page(folio,
|
|
- PAGE_ALIGN_DOWN(offset + length) / PAGE_SIZE);
|
|
- folio2 = page_folio(split_at2);
|
|
-
|
|
- if (!folio_try_get(folio2))
|
|
- goto no_split;
|
|
-
|
|
- if (!folio_test_large(folio2))
|
|
- goto out;
|
|
-
|
|
- if (!folio_trylock(folio2))
|
|
- goto out;
|
|
-
|
|
- /* make sure folio2 is large and does not change its mapping */
|
|
- if (folio_test_large(folio2) &&
|
|
- folio2->mapping == folio->mapping)
|
|
- folio_split_or_unmap(folio2, split_at2, min_order);
|
|
-
|
|
- folio_unlock(folio2);
|
|
-out:
|
|
- folio_put(folio2);
|
|
-no_split:
|
|
- return true;
|
|
- }
|
|
- if (folio_test_dirty(folio))
|
|
- return false;
|
|
- truncate_inode_folio(folio->mapping, folio);
|
|
- return true;
|
|
-}
|
|
-
|
|
-/*
|
|
- * Used to get rid of pages on hardware memory corruption.
|
|
- */
|
|
-int generic_error_remove_folio(struct address_space *mapping,
|
|
- struct folio *folio)
|
|
-{
|
|
- if (!mapping)
|
|
- return -EINVAL;
|
|
- /*
|
|
- * Only punch for normal data pages for now.
|
|
- * Handling other types like directories would need more auditing.
|
|
- */
|
|
- if (!S_ISREG(mapping->host->i_mode))
|
|
- return -EIO;
|
|
- return truncate_inode_folio(mapping, folio);
|
|
-}
|
|
-EXPORT_SYMBOL(generic_error_remove_folio);
|
|
-
|
|
-/**
|
|
- * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets
|
|
- * @mapping: mapping to truncate
|
|
- * @lstart: offset from which to truncate
|
|
- * @lend: offset to which to truncate (inclusive)
|
|
- *
|
|
- * Truncate the page cache, removing the pages that are between
|
|
- * specified offsets (and zeroing out partial pages
|
|
- * if lstart or lend + 1 is not page aligned).
|
|
- *
|
|
- * Truncate takes two passes - the first pass is nonblocking. It will not
|
|
- * block on page locks and it will not block on writeback. The second pass
|
|
- * will wait. This is to prevent as much IO as possible in the affected region.
|
|
- * The first pass will remove most pages, so the search cost of the second pass
|
|
- * is low.
|
|
- *
|
|
- * We pass down the cache-hot hint to the page freeing code. Even if the
|
|
- * mapping is large, it is probably the case that the final pages are the most
|
|
- * recently touched, and freeing happens in ascending file offset order.
|
|
- *
|
|
- * Note that since ->invalidate_folio() accepts range to invalidate
|
|
- * truncate_inode_pages_range is able to handle cases where lend + 1 is not
|
|
- * page aligned properly.
|
|
- */
|
|
-void truncate_inode_pages_range(struct address_space *mapping,
|
|
- loff_t lstart, uoff_t lend)
|
|
-{
|
|
- pgoff_t start; /* inclusive */
|
|
- pgoff_t end; /* exclusive */
|
|
- struct folio_batch fbatch;
|
|
- pgoff_t indices[FOLIO_BATCH_SIZE];
|
|
- pgoff_t index;
|
|
- int i;
|
|
- struct folio *folio;
|
|
- bool same_folio;
|
|
-
|
|
- if (mapping_empty(mapping))
|
|
- return;
|
|
-
|
|
- /*
|
|
- * 'start' and 'end' always covers the range of pages to be fully
|
|
- * truncated. Partial pages are covered with 'partial_start' at the
|
|
- * start of the range and 'partial_end' at the end of the range.
|
|
- * Note that 'end' is exclusive while 'lend' is inclusive.
|
|
- */
|
|
- start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
|
- if (lend == -1)
|
|
- /*
|
|
- * lend == -1 indicates end-of-file so we have to set 'end'
|
|
- * to the highest possible pgoff_t and since the type is
|
|
- * unsigned we're using -1.
|
|
- */
|
|
- end = -1;
|
|
- else
|
|
- end = (lend + 1) >> PAGE_SHIFT;
|
|
-
|
|
- folio_batch_init(&fbatch);
|
|
- index = start;
|
|
- while (index < end && find_lock_entries(mapping, &index, end - 1,
|
|
- &fbatch, indices)) {
|
|
- truncate_folio_batch_exceptionals(mapping, &fbatch, indices);
|
|
- for (i = 0; i < folio_batch_count(&fbatch); i++)
|
|
- truncate_cleanup_folio(fbatch.folios[i]);
|
|
- delete_from_page_cache_batch(mapping, &fbatch);
|
|
- for (i = 0; i < folio_batch_count(&fbatch); i++)
|
|
- folio_unlock(fbatch.folios[i]);
|
|
- folio_batch_release(&fbatch);
|
|
- cond_resched();
|
|
- }
|
|
-
|
|
- same_folio = (lstart >> PAGE_SHIFT) == (lend >> PAGE_SHIFT);
|
|
- folio = __filemap_get_folio(mapping, lstart >> PAGE_SHIFT, FGP_LOCK, 0);
|
|
- if (!IS_ERR(folio)) {
|
|
- same_folio = lend < folio_next_pos(folio);
|
|
- if (!truncate_inode_partial_folio(folio, lstart, lend)) {
|
|
- start = folio_next_index(folio);
|
|
- if (same_folio)
|
|
- end = folio->index;
|
|
- }
|
|
- folio_unlock(folio);
|
|
- folio_put(folio);
|
|
- folio = NULL;
|
|
- }
|
|
-
|
|
- if (!same_folio) {
|
|
- folio = __filemap_get_folio(mapping, lend >> PAGE_SHIFT,
|
|
- FGP_LOCK, 0);
|
|
- if (!IS_ERR(folio)) {
|
|
- if (!truncate_inode_partial_folio(folio, lstart, lend))
|
|
- end = folio->index;
|
|
- folio_unlock(folio);
|
|
- folio_put(folio);
|
|
- }
|
|
- }
|
|
-
|
|
- index = start;
|
|
- while (index < end) {
|
|
- cond_resched();
|
|
- if (!find_get_entries(mapping, &index, end - 1, &fbatch,
|
|
- indices)) {
|
|
- /* If all gone from start onwards, we're done */
|
|
- if (index == start)
|
|
- break;
|
|
- /* Otherwise restart to make sure all gone */
|
|
- index = start;
|
|
- continue;
|
|
- }
|
|
-
|
|
- for (i = 0; i < folio_batch_count(&fbatch); i++) {
|
|
- struct folio *folio = fbatch.folios[i];
|
|
-
|
|
- /* We rely upon deletion not changing folio->index */
|
|
-
|
|
- if (xa_is_value(folio))
|
|
- continue;
|
|
-
|
|
- folio_lock(folio);
|
|
- VM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio);
|
|
- folio_wait_writeback(folio);
|
|
- truncate_inode_folio(mapping, folio);
|
|
- folio_unlock(folio);
|
|
- }
|
|
- truncate_folio_batch_exceptionals(mapping, &fbatch, indices);
|
|
- folio_batch_release(&fbatch);
|
|
- }
|
|
-}
|
|
-EXPORT_SYMBOL(truncate_inode_pages_range);
|
|
-
|
|
-/**
|
|
- * truncate_inode_pages - truncate *all* the pages from an offset
|
|
- * @mapping: mapping to truncate
|
|
- * @lstart: offset from which to truncate
|
|
- *
|
|
- * Called under (and serialised by) inode->i_rwsem and
|
|
- * mapping->invalidate_lock.
|
|
- *
|
|
- * Note: When this function returns, there can be a page in the process of
|
|
- * deletion (inside __filemap_remove_folio()) in the specified range. Thus
|
|
- * mapping->nrpages can be non-zero when this function returns even after
|
|
- * truncation of the whole mapping.
|
|
- */
|
|
-void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
|
|
-{
|
|
- truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
|
|
-}
|
|
-EXPORT_SYMBOL(truncate_inode_pages);
|
|
-
|
|
-/**
|
|
- * truncate_inode_pages_final - truncate *all* pages before inode dies
|
|
- * @mapping: mapping to truncate
|
|
- *
|
|
- * Called under (and serialized by) inode->i_rwsem.
|
|
- *
|
|
- * Filesystems have to use this in the .evict_inode path to inform the
|
|
- * VM that this is the final truncate and the inode is going away.
|
|
- */
|
|
-void truncate_inode_pages_final(struct address_space *mapping)
|
|
-{
|
|
- /*
|
|
- * Page reclaim can not participate in regular inode lifetime
|
|
- * management (can't call iput()) and thus can race with the
|
|
- * inode teardown. Tell it when the address space is exiting,
|
|
- * so that it does not install eviction information after the
|
|
- * final truncate has begun.
|
|
- */
|
|
- mapping_set_exiting(mapping);
|
|
-
|
|
- if (!mapping_empty(mapping)) {
|
|
- /*
|
|
- * As truncation uses a lockless tree lookup, cycle
|
|
- * the tree lock to make sure any ongoing tree
|
|
- * modification that does not see AS_EXITING is
|
|
- * completed before starting the final truncate.
|
|
- */
|
|
- xa_lock_irq(&mapping->i_pages);
|
|
- xa_unlock_irq(&mapping->i_pages);
|
|
- }
|
|
-
|
|
- truncate_inode_pages(mapping, 0);
|
|
-}
|
|
-EXPORT_SYMBOL(truncate_inode_pages_final);
|
|
-
|
|
static int folio_launder(struct address_space *mapping, struct folio *folio)
|
|
{
|
|
if (!folio_test_dirty(folio))
|