diff --git a/include/linux/mvolatile.h b/include/linux/mvolatile.h
index bc8828c8e6c4..bf2625aad07b 100644
--- a/include/linux/mvolatile.h
+++ b/include/linux/mvolatile.h
@@ -2,17 +2,47 @@
#ifndef _LINUX_MVOLATILE_H
#define _LINUX_MVOLATILE_H
+#include <linux/jump_label.h>
#include <linux/mm_types.h>
-#ifdef CONFIG_64BIT
+/**
+ * enum purge_result - outcome of purge_volatile_folio()
+ * @PURGE_NONE: not purgeable, reclaim the folio normally (still locked).
+ * @PURGE_RETRY: purgeable, but a transient reference prevented the purge;
+ * keep the folio for a later attempt (still locked).
+ * @PURGE_DONE: purged; the folio is unlocked, out of the swap cache and its
+ * refcount is frozen to zero.
+ */
+enum purge_result {
+ PURGE_NONE,
+ PURGE_RETRY,
+ PURGE_DONE,
+};
+
+/* mm/mvolatile.o is built only for 64BIT && MMU, see mm/Makefile. */
+#if defined(CONFIG_64BIT) && defined(CONFIG_MMU)
+DECLARE_STATIC_KEY_FALSE(volatile_ranges_key);
+
int madvise_volatile(struct mm_struct *mm, unsigned long start,
unsigned long end, int behavior);
+enum purge_result __purge_volatile_folio(struct folio *folio);
+
+static inline enum purge_result purge_volatile_folio(struct folio *folio)
+{
+ if (!static_branch_unlikely(&volatile_ranges_key))
+ return PURGE_NONE;
+ return __purge_volatile_folio(folio);
+}
#else
static inline int madvise_volatile(struct mm_struct *mm, unsigned long start,
unsigned long end, int behavior)
{
return -EINVAL;
}
+static inline enum purge_result purge_volatile_folio(struct folio *folio)
+{
+ return PURGE_NONE;
+}
#endif
#endif /* _LINUX_MVOLATILE_H */diff --git a/mm/mvolatile.c b/mm/mvolatile.c
index fbe1028aaf5c..c111bc4f111d 100644
--- a/mm/mvolatile.c
+++ b/mm/mvolatile.c
@@ -18,6 +18,46 @@
#include "internal.h"
#include "vma.h"
+/* Enabled by the first MADV_VOLATILE, never disabled. */
+DEFINE_STATIC_KEY_FALSE(volatile_ranges_key);
+
+struct mvolatile_walker {
+ int page_was_purged;
+};
+
+static int mvolatile_check_purged_pte(pte_t *ptep, unsigned long addr,
+ unsigned long next, struct mm_walk *walk)
+{
+ struct mvolatile_walker *vw = walk->private;
+ pte_t pte = ptep_get(ptep);
+
+ if (!pte_present(pte)) {
+ softleaf_t entry = softleaf_from_pte(pte);
+
+ if (softleaf_is_purged_marker(entry)) {
+ vw->page_was_purged = 1;
+
+ /* Clear the marker, the next access maps a zero page. */
+ pte_clear(walk->mm, addr, ptep);
+ }
+ }
+ return 0;
+}
+
+static const struct mm_walk_ops mvolatile_walk_ops = {
+ .pte_entry = mvolatile_check_purged_pte,
+ .walk_lock = PGWALK_RDLOCK,
+};
+
+static int mvolatile_check_purged(struct mm_struct *mm, unsigned long start,
+ unsigned long end)
+{
+ struct mvolatile_walker vw = { .page_was_purged = 0 };
+
+ walk_page_range(mm, start, end, &mvolatile_walk_ops, &vw);
+ return vw.page_was_purged;
+}
+
static int check_volatile_range(struct mm_struct *mm, unsigned long start,
unsigned long end, int behavior)
{@@ -79,14 +119,17 @@ static int check_volatile_range(struct mm_struct *mm, unsigned long start,
* The whole range is checked before any VMA is changed, so unmapped holes,
* unsupported VMAs and VMA splits beyond max_map_count fail with nothing
* changed. Only an allocation failure while splitting or merging a VMA can
- * leave a prefix of the range changed.
+ * leave a prefix of the range changed; %MADV_NONVOLATILE then returns the
+ * error without consuming purged pages, so that a retry reports them.
*
- * Return: 0 on success or a negative errno.
+ * Return: 0 on success, 1 if %MADV_NONVOLATILE found purged pages in the
+ * range, or a negative errno.
*/
int madvise_volatile(struct mm_struct *mm, unsigned long start, unsigned long end, int behavior)
{
VMA_ITERATOR(vmi, mm, start);
struct vm_area_struct *vma, *prev;
+ bool validated = false;
int ret = 0;
if (start >= TASK_SIZE || end > TASK_SIZE)@@ -98,6 +141,7 @@ int madvise_volatile(struct mm_struct *mm, unsigned long start, unsigned long en
ret = check_volatile_range(mm, start, end, behavior);
if (ret)
goto out;
+ validated = true;
vma = vma_iter_load(&vmi);
prev = vma_prev(&vmi);
@@ -137,7 +181,115 @@ int madvise_volatile(struct mm_struct *mm, unsigned long start, unsigned long en
prev = vma;
}
+ if (behavior == MADV_NONVOLATILE)
+ ret = mvolatile_check_purged(mm, start, end);
+
out:
mmap_write_unlock(mm);
+ /* Outside mmap_lock: static_branch_enable() takes cpus_read_lock(). */
+ if (validated && behavior == MADV_VOLATILE &&
+ !static_key_enabled(&volatile_ranges_key))
+ static_branch_enable(&volatile_ranges_key);
return ret;
}
+
+/**
+ * __purge_volatile_folio() - Discard a folio only mapped by volatile VMAs
+ * @folio: locked folio isolated for reclaim
+ *
+ * If every VMA mapping the order-0 anonymous @folio is volatile, replace its
+ * PTEs with PTE_MARKER_PURGED markers, drop its swap cache and freeze its
+ * refcount.
+ *
+ * Context: Caller holds the folio lock; @folio is not on the LRU.
+ * Return: see &enum purge_result.
+ */
+enum purge_result __purge_volatile_folio(struct folio *folio)
+{
+ struct anon_vma *anon_vma;
+ struct anon_vma_chain *avc;
+ pgoff_t pgoff_start, pgoff_end;
+ bool mapped = false;
+
+ VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
+ VM_WARN_ON_ONCE_FOLIO(folio_test_lru(folio), folio);
+
+ if (!folio_test_anon(folio) || folio_test_ksm(folio))
+ return PURGE_NONE;
+
+ /* Like swap-out, keep a folio the pin holder may still be using. */
+ if (folio_maybe_dma_pinned(folio))
+ return PURGE_NONE;
+
+ /*
+ * An earlier attempt may have installed the markers and dropped the
+ * swap cache, and then failed to freeze the refcount. The folio can no
+ * longer be found through its anon_vma, but no PTE or swap entry refers
+ * to it either, so just retry the freeze. Lazyfree folios take their
+ * own path in shrink_folio_list().
+ */
+ if (!folio_mapped(folio)) {
+ if (!folio_test_swapbacked(folio) || folio_test_swapcache(folio) ||
+ folio_test_large(folio))
+ return PURGE_NONE;
+ goto freeze;
+ }
+
+ anon_vma = folio_lock_anon_vma_read(folio, NULL);
+ if (!anon_vma)
+ return PURGE_NONE;
+
+ pgoff_start = folio_pgoff(folio);
+ pgoff_end = pgoff_start + folio_nr_pages(folio) - 1;
+
+ /*
+ * Keep the contents if any VMA mapping the folio, e.g. in a child
+ * after fork(), is not volatile.
+ */
+ anon_rmap_tree_foreach(avc, anon_vma, pgoff_start, pgoff_end) {
+ struct vm_area_struct *vma = avc->vma;
+
+ if (!vma_is_volatile(vma)) {
+ anon_vma_unlock_read(anon_vma);
+ return PURGE_NONE;
+ }
+ mapped = true;
+ }
+ anon_vma_unlock_read(anon_vma);
+
+ /* Not expected for a mapped folio, but never purge without a VMA. */
+ if (!mapped)
+ return PURGE_NONE;
+
+ /* Only order-0 folios are supported for now. */
+ if (folio_test_large(folio))
+ return PURGE_NONE;
+
+ try_to_unmap(folio, TTU_PURGE | TTU_SYNC);
+
+ /*
+ * try_to_unmap_one() backs off if the folio got pinned meanwhile or is
+ * mapped by a VM_LOCKED VMA; in the latter case it restores the mlock,
+ * so the folio goes to the unevictable LRU. Some mappings may already
+ * hold markers, so retry rather than swap the folio out.
+ */
+ if (folio_mapped(folio))
+ return PURGE_RETRY;
+
+ /* Other PTEs still refer to the swap entry, the data must stay. */
+ if (folio_test_swapcache(folio) && !folio_free_swap(folio))
+ return PURGE_NONE;
+
+freeze:
+ /*
+ * Like __remove_mapping(), back off on a transient reference. No new
+ * pin can be taken from here on; the holder of a reference taken
+ * without pinning (speculative or FOLL_GET) keeps the old contents,
+ * as after MADV_DONTNEED.
+ */
+ if (!folio_ref_freeze(folio, 1))
+ return PURGE_RETRY;
+
+ folio_unlock(folio);
+ return PURGE_DONE;
+}diff --git a/mm/vmscan.c b/mm/vmscan.c
index f11491ee9ed5..b72078a37930 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -59,6 +59,7 @@
#include <linux/mmu_notifier.h>
#include <linux/parser.h>
#include <linux/swap_ops.h>
+#include <linux/mvolatile.h>
#include <asm/tlbflush.h>
#include <asm/div64.h>
@@ -1252,6 +1253,19 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
continue;
}
+ /*
+ * Discard folios only mapped by volatile VMAs instead of
+ * allocating swap for them.
+ */
+ switch (purge_volatile_folio(folio)) {
+ case PURGE_DONE:
+ goto free_it;
+ case PURGE_RETRY:
+ goto keep_locked;
+ case PURGE_NONE:
+ break;
+ }
+
/*
* Anonymous process memory has backing store?
* Try to allocate it some swap space here.--
2.56.1.839.g22a8290db9-goog