mm: port MADV_VOLATILE and MADV_NONVOLATILE to v7.3-rc1
Subsystems (16)
cover mm: port MADV_VOLATILE and MADV_NONVOLATILE to v7.3-rc1
This RFC series revives and modernizes the volatile ranges patchset originally proposed by John Stultz and Minchan Kim [1].
Volatile ranges allow userspace caches holding regenerable data (such as decoded image bitmaps or other private caches) to mark private anonymous memory as safe for the kernel to discard under memory pressure, bypassing swap. Such ranges are marked via madvise(MADV_VOLATILE). Before reusing the buffer, userspace calls madvise(MADV_NONVOLATILE), which unmarks the range and returns 1 if any pages in the range were purged while volatile (or 0 if all pages remained intact).
Unlike MADV_FREE, which silently zero-fills dropped pages on re-access, calling MADV_NONVOLATILE before reuse gives userspace deterministic notification of whether cached data was lost without having to touch every page.
If userspace instead accesses a purged page while the range is still volatile, the fault raises SIGBUS; after MADV_NONVOLATILE, the next access to it maps a zero page. Zero-filling on fault was considered, but this series chooses SIGBUS to catch missing MADV_NONVOLATILE calls and partially purged reads instead of silently seeing zeros, and to keep the optimistic read model from earlier series possible so readers can skip per-access MADV_NONVOLATILE/MADV_VOLATILE calls.
Volatile pages share the regular anon LRU, so currently they are not purged when swap is disabled or full. Two alternatives were considered, but neither was satisfactory:
- Keeping volatile pages on a separate LRU: changes eviction order and creates balancing issues against page cache and swap.
- Keeping a linked list of volatile pages only, in addition to the anon
LRU: allows scanning for volatile pages cheaply when swap is full, but making pages volatile then requires scanning, which is expensive.
As a future extension, finer-grained purge reporting could avoid regenerating an entire large buffer when only part of it was purged. Within madvise(2), up to 31 bits of a positive int return value could encode a coarse mask over N equal sub-ranges; configurable granularity or an arbitrary-length bitmap would instead call for a dedicated syscall taking an output bitmap pointer (similar to mincore(2)).
Key changes from the earlier v14 series [1]:
- Use PTE markers (PTE_MARKER_PURGED) instead of consuming a swap type.
- Defer purging in shrink_folio_list() until after checking references (immediately before folio_alloc_swap()), allowing frequently accessed volatile pages to age normally via LRU/MGLRU.
- Port VMA handling to Maple Tree and modern folio APIs.
- Add a PGPURGE vmstat/memcg counter and a tracepoint.
This series was evaluated with Chromium patched to back discardable memory with MADV_VOLATILE/MADV_NONVOLATILE, running the multiwindow-memory-stress benchmark from chromium/web-tests (30 tabs across 5 windows alongside 4 background Chromium instances in an 8 GiB VM with 8 GiB swap). Backing decoded image caches with volatile ranges reduced the software image decode count per run by 18.0% and total software image decode time per run by 24.9% (Renderer4.ImageDecodeTaskDurationUs.Software). Under slab_nomerge, VMA-related slab overhead (vm_area_struct, anon_vma_chain, anon_vma, maple_node) was +228 KiB mean (+1.55%, ~0.003% of ~7.2 GiB total Chromium PSS) and +76 KiB peak (+0.42%).
[1] https://lore.kernel.org/lkml/[email protected]/
("[PATCH 0/4] Volatile Ranges (v14 - madvise reborn edition!)")
Hirotaka Yamagata (4):
mm: introduce VM_VOLATILE and MADV_VOLATILE/MADV_NONVOLATILE VMA
handling
mm: add PTE_MARKER_PURGED and TTU_PURGE for purged volatile pages mm/volatile: purge volatile anonymous folios during reclaim mm/volatile: add PGPURGE vmstat counter and purge tracepoint
MAINTAINERS | 2 + fs/proc/task_mmu.c | 1 + include/linux/leafops.h | 14 ++ include/linux/mm.h | 12 + include/linux/mm_inline.h | 5 +- include/linux/mvolatile.h | 48 ++++ include/linux/rmap.h | 1 + include/linux/swapops.h | 18 +- include/linux/vm_event_item.h | 1 + include/trace/events/mmflags.h | 7 + include/trace/events/vmscan.h | 24 ++ include/uapi/asm-generic/mman-common.h | 3 + mm/Makefile | 2 +- mm/khugepaged.c | 17 ++ mm/madvise.c | 29 +++ mm/memcontrol.c | 1 + mm/memory.c | 5 + mm/mprotect.c | 6 +- mm/mseal.c | 20 ++ mm/mvolatile.c | 311 +++++++++++++++++++++++++ mm/rmap.c | 14 ++ mm/vmscan.c | 14 ++ mm/vmstat.c | 1 + 23 files changed, 550 insertions(+), 6 deletions(-) create mode 100644 include/linux/mvolatile.h create mode 100644 mm/mvolatile.c
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
Signature · 1 line
2.56.0.611.g1030f0b940-goog
RFC PATCH 1/4 mm: introduce VM_VOLATILE and MADV_VOLATILE/MADV_NONVOLATILE VMA handling
Userspace caches that hold regenerable data (such as decoded images or compiled assets) need a way to let the kernel discard cached pages under memory pressure without swap I/O, while being notified whether any pages were discarded when the range is marked active again.
Introduce the MADV_VOLATILE (26) and MADV_NONVOLATILE (27) madvise() behaviors and the corresponding VM_VOLATILE (VMA_VOLATILE_BIT) VMA flag on 64-bit architectures.
Check the whole range in check_volatile_range() before changing any VMA. Unmapped holes fail with -ENOMEM. File-backed, hugetlb, mixedmap and special VMAs fail with -EINVAL. MADV_VOLATILE additionally fails on mlocked VMAs (-EINVAL), because mlocked memory is not purged, and on mseal()ed VMAs (-EPERM), because sealed memory must not be discarded. MADV_NONVOLATILE stays allowed on mlocked and sealed VMAs. Conversely, mseal() fails with -EBUSY and seals nothing if its range contains a volatile page: MADV_NONVOLATILE first, then retry.
The check also fails with -ENOMEM up front if the boundary splits would exceed max_map_count, so the usual failures change nothing. Only an allocation failure inside a split or merge can leave a prefix of the range changed. The start address is untagged, as for the other behaviors.
Disallow MADV_VOLATILE and MADV_NONVOLATILE in process_madvise() (vector_madvise()), as its byte-count return value cannot unambiguously represent the positive purge status (1) returned by MADV_NONVOLATILE.
Also expose the "vo" mnemonic in /proc/<pid>/smaps and mmflags tracepoints.
Based on the earlier volatile ranges series by John Stultz and Minchan Kim.
--- MAINTAINERS | 2 + fs/proc/task_mmu.c | 1 + include/linux/mm.h | 12 +++ include/linux/mvolatile.h | 18 ++++ include/trace/events/mmflags.h | 7 ++ include/uapi/asm-generic/mman-common.h | 3 + mm/Makefile | 2 +- mm/madvise.c | 29 +++++ mm/mseal.c | 20 ++++ mm/mvolatile.c | 143 +++++++++++++++++++++++++ 10 files changed, 236 insertions(+), 1 deletion(-) create mode 100644 include/linux/mvolatile.h create mode 100644 mm/mvolatile.c
MAINTAINERS +2 -0
@@ -17528,8 +17528,10 @@ L: [email protected] S: Maintained W: http://www.linux-mm.org T: git git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm +F: include/linux/mvolatile.h F: include/uapi/asm-generic/mman-common.h F: mm/madvise.c +F: mm/mvolatile.c MEMORY TECHNOLOGY DEVICES (MTD) M: Miquel Raynal <[email protected]>
fs/proc/task_mmu.c +1 -0
@@ -1248,6 +1248,7 @@ static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)#endif #ifdef CONFIG_64BIT [ilog2(VM_SEALED)] = "sl", + [ilog2(VM_VOLATILE)] = "vo",#endif };size_t i;
include/linux/mm.h +12 -0
@@ -355,6 +355,9 @@ enum {DECLARE_VMA_BIT (UFFD_MINOR ,41 ),DECLARE_VMA_BIT (SEALED ,42 ),DECLARE_VMA_BIT (UFFD_RWP ,43 ), +#ifdef CONFIG_64BIT +DECLARE_VMA_BIT (VOLATILE ,44 ), +#endif/* Flags that reuse flags above. */ DECLARE_VMA_BIT_ALIAS (PKEY_BIT0 ,HIGH_ARCH_0 ), DECLARE_VMA_BIT_ALIAS(PKEY_BIT1, HIGH_ARCH_1),
@@ -532,9 +535,13 @@ enum {#ifdef CONFIG_64BIT #define VM_ALLOW_ANY_UNCACHED INIT_VM_FLAG(ALLOW_ANY_UNCACHED)#define VM_SEALED INIT_VM_FLAG(SEALED) +#define VM_VOLATILE INIT_VM_FLAG(VOLATILE) +#define VMA_VOLATILE mk_vma_flags(VMA_VOLATILE_BIT)#else #define VM_ALLOW_ANY_UNCACHED VM_NONE#define VM_SEALED VM_NONE +#define VM_VOLATILE VM_NONE +#define VMA_VOLATILE EMPTY_VMA_FLAGS#endif #if defined(CONFIG_64BIT ) || defined(CONFIG_PPC32 )#define VM_DROPPABLE INIT_VM_FLAG(DROPPABLE)
@@ -1561,6 +1568,11 @@ static inline bool vma_is_anonymous(const struct vm_area_struct *vma)return !vma->vm_ops; } +static inline bool vma_is_volatile (const struct vm_area_struct *vma) +{ +return vma_test_single_mask (vma,VMA_VOLATILE ); +} +/* * Indicate if the VMA is a heap for the given task; for * /proc/PID/maps that is the heap of the main task.
include/linux/mvolatile.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_MVOLATILE_H +#define _LINUX_MVOLATILE_H + +#include <linux/mm_types.h> + +#ifdef CONFIG_64BIT +int madvise_volatile (struct mm_struct *mm,unsigned long start, +unsigned long end,int behavior); +#else +static inline int madvise_volatile (struct mm_struct *mm,unsigned long start, +unsigned long end,int behavior) +{ +return -EINVAL ; +} +#endif + +#endif /* _LINUX_MVOLATILE_H */
include/trace/events/mmflags.h +7 -0
@@ -198,6 +198,12 @@ IF_HAVE_PG_ARCH_3(arch_3)# define IF_HAVE_VM_DROPPABLE (flag, name)#endif +#ifdef CONFIG_64BIT +# define IF_HAVE_VM_VOLATILE (flag, name) {flag, name}, +#else +# define IF_HAVE_VM_VOLATILE (flag, name) +#endif +#define __def_vmaflag_names \ {VM_READ, "read" }, \ {VM_WRITE, "write" }, \
@@ -233,6 +239,7 @@ IF_HAVE_VM_SOFTDIRTY(VM_SOFTDIRTY, "softdirty" ) \ {VM_HUGEPAGE, "hugepage" }, \ {VM_NOHUGEPAGE, "nohugepage" }, \ IF_HAVE_VM_DROPPABLE(VM_DROPPABLE, "droppable" ) \ +IF_HAVE_VM_VOLATILE(VM_VOLATILE, "volatile") \ {VM_MERGEABLE, "mergeable" } \#define show_vma_flags (flags) \
include/uapi/asm-generic/mman-common.h +3 -0
@@ -79,6 +79,9 @@#define MADV_COLLAPSE 25/* Synchronous hugepage collapse */ +#define MADV_VOLATILE 26/* Mark pages as volatile */ +#define MADV_NONVOLATILE 27/* Mark pages non-volatile, report if purged */ +#define MADV_GUARD_INSTALL 102/* fatal signal on access to range */ #define MADV_GUARD_REMOVE 103/* unguard range */
mm/Makefile +1 -1
@@ -45,7 +45,7 @@ mmu-$(CONFIG_MMU) += process_vm_access.o endif ifdef CONFIG_64BIT -mmu-$(CONFIG_MMU) += mseal.o +mmu-$(CONFIG_MMU) += mseal.o mvolatile.o endif obj-y := filemap.o mempool.o oom_kill.o fadvise.o \
mm/madvise.c +29 -0
@@ -33,6 +33,7 @@#include <linux/shmem_fs.h> #include <linux/mmu_notifier.h> #include <linux/swap_ops.h> +#include <linux/mvolatile.h> #include <asm/tlb.h>
@@ -1548,6 +1549,8 @@ madvise_behavior_valid(int behavior)case MADV_KEEPONFORK :case MADV_GUARD_INSTALL :case MADV_GUARD_REMOVE : +case MADV_VOLATILE : +case MADV_NONVOLATILE :#ifdef CONFIG_MEMORY_FAILURE case MADV_SOFT_OFFLINE :case MADV_HWPOISON :
@@ -1967,9 +1970,15 @@ static int madvise_do_behavior(unsigned long start, size_t len_in,* triggering read faults if required * MADV_POPULATE_WRITE - populate (prefault) page tables writable by * triggering write faults if required +* MADV_VOLATILE - mark pages in the given range as volatile, allowing +* the kernel to purge them under memory pressure. +* MADV_NONVOLATILE - mark pages in the given range as non-volatile and +* report if any pages in the range were purged. * * return values: * zero - success +* 1 - success, and at least one page in the range was purged +* (MADV_NONVOLATILE only). * -EINVAL - start + len < 0, start is not page-aligned, * "behavior" is not a valid value, or application * is attempting to release locked or shared pages,
@@ -1999,6 +2008,18 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behif (error || !len_in)return error; +/* +* The volatile behaviors validate the whole range before changing any +* VMA, and MADV_NONVOLATILE can return 1, so they do not use the +* per-VMA walk below. +*/ +if (behavior ==MADV_VOLATILE || behavior ==MADV_NONVOLATILE ) { +if (!PAGE_ALIGNED (len_in)) +return -EINVAL ; + start =get_untagged_addr (mm, start); +return madvise_volatile (mm, start, start + len_in, behavior); + } + error =madvise_lock (&madv_behavior);if (error)return error;
@@ -2028,6 +2049,14 @@ static ssize_t vector_madvise(struct mm_struct *mm, struct iov_iter *iter, .tlb = &tlb, }; +/* +* process_madvise() returns a byte count, which cannot carry the purge +* report of MADV_NONVOLATILE. Reject MADV_VOLATILE as well, it is of no +* use without its counterpart. +*/ +if (behavior ==MADV_VOLATILE || behavior ==MADV_NONVOLATILE ) +return -EINVAL ; + total_len =iov_iter_count (iter); ret =madvise_lock (&madv_behavior);
mm/mseal.c +20 -0
@@ -32,6 +32,24 @@ static bool range_contains_unmapped(unsigned long start, unsigned long end)return prev_end < end; } +/* +* Sealed memory must not be discarded, so refuse to seal volatile memory that +* reclaim may purge: madvise(MADV_NONVOLATILE) first, then retry. Check every +* VMA before sealing any, so that a refused mseal() changes nothing. +*/ +static bool range_contains_volatile (unsigned long start,unsigned long end) +{ +VMA_ITERATOR (vmi, current->mm, start); +struct vm_area_struct *vma; + +for_each_vma_range (vmi, vma, end) { +if (vma_is_volatile (vma)) +return true; + } + +return false; +} +static int __mseal_range (unsigned long start,unsigned long end) {VMA_ITERATOR (vmi, current->mm, start);
@@ -75,6 +93,8 @@ static int mseal_range(unsigned long start, unsigned long end)return err;if (range_contains_unmapped (start, end)) err = -ENOMEM ; +else if (range_contains_volatile (start, end)) + err = -EBUSY ;else err =__mseal_range (start, end);mmap_write_unlock (current->mm);
mm/mvolatile.c new file +143 -0
143 lines not on this page · raw message
Signature · 1 line
2.56.1.839.g22a8290db9-goog
RFC PATCH 2/4 mm: add PTE_MARKER_PURGED and TTU_PURGE for purged volatile pages
In reply to Hirotaka YamagataWhen a volatile anonymous page is discarded under memory pressure, the page table must record that the page was purged so that:
1. Accessing the purged address while still volatile raises SIGBUS.
2. A subsequent MADV_NONVOLATILE call can detect that data in the range was discarded, clear the marker, and notify userspace.
Introduce PTE_MARKER_PURGED (BIT(3)) using the PTE marker infrastructure and wire up its lifecycle across core mm paths:
- Deliver VM_FAULT_SIGBUS in handle_pte_marker() when faulting on a PTE_MARKER_PURGED entry.
- Preserve PTE_MARKER_PURGED across fork() in copy_pte_marker() and ignore it during mprotect() in change_softleaf_pte().
- Drop PTE_MARKER_PURGED unconditionally in zap_nonpresent_ptes() on munmap() or MADV_DONTNEED.
- Add TTU_PURGE to enum ttu_flags so try_to_unmap_one() installs a PTE_MARKER_PURGED entry when unmapping a purged volatile page; like swap-out, it backs off when an anon-exclusive page may be pinned.
- Make khugepaged and MADV_COLLAPSE skip PTE markers (other than uffd-wp) at scan time, so that collapse does not try to swap in purged markers.
--- include/linux/leafops.h | 14 ++++++++++++++ include/linux/mm_inline.h | 5 +++-- include/linux/rmap.h | 1 + include/linux/swapops.h | 18 +++++++++++++++++- mm/khugepaged.c | 17 +++++++++++++++++ mm/memory.c | 5 +++++ mm/mprotect.c | 6 ++++-- mm/rmap.c | 14 ++++++++++++++ 8 files changed, 75 insertions(+), 5 deletions(-)
include/linux/leafops.h +14 -0
@@ -464,6 +464,20 @@ static inline bool softleaf_is_guard_marker(softleaf_t entry)return softleaf_to_marker (entry) &PTE_MARKER_GUARD ; } +/** +* softleaf_is_purged_marker() - Is this leaf entry a purged marker? +* @entry: Leaf entry. +* +* Returns: true if the leaf entry is a purged marker, otherwise false. +*/ +static inline bool softleaf_is_purged_marker (softleaf_t entry) +{ +if (!softleaf_is_marker (entry)) +return false; + +return softleaf_to_marker (entry) &PTE_MARKER_PURGED ; +} +/** * softleaf_is_uffd_wp_marker() - Is this leaf entry a userfautlfd write protect * marker?
include/linux/mm_inline.h +3 -2
@@ -556,8 +556,9 @@ static inline pte_marker copy_pte_marker( softleaf_t entry, struct vm_area_struct *dst_vma) { const pte_marker srcm = softleaf_to_marker(entry); -/* Always copy error entries. */ -pte_marker dstm = srcm & (PTE_MARKER_POISONED |PTE_MARKER_GUARD ); +/* Always copy error and purged entries. */ +pte_marker dstm = srcm & (PTE_MARKER_POISONED |PTE_MARKER_GUARD | +PTE_MARKER_PURGED );/* Only copy PTE markers if UFFD register matches. */ if ((srcm &PTE_MARKER_UFFD_WP ) &&userfaultfd_wp (dst_vma))
include/linux/rmap.h +1 -0
@@ -102,6 +102,7 @@ enum ttu_flags {* do a final flush if necessary */ TTU_RMAP_LOCKED =0x80 ,/* do not grab rmap lock: * caller holds it */ + TTU_PURGE = 0x100, /* convert pte to purged marker entry */ };#ifdef CONFIG_MMU
include/linux/swapops.h +17 -1
@@ -298,7 +298,13 @@ typedef unsigned long pte_marker;* PROT_NONE, rather than if they were a memory hole or equivalent. */ #define PTE_MARKER_GUARD BIT(2) -#define PTE_MARKER_MASK (BIT(3) - 1) +/* +* Indicates that a volatile anonymous page mapped by this PTE was purged +* under memory pressure. MADV_NONVOLATILE clears it and reports the purge; +* see handle_pte_marker() for faults before MADV_NONVOLATILE. +*/ +#define PTE_MARKER_PURGED BIT(3) +#define PTE_MARKER_MASK (BIT(4) - 1)static inline swp_entry_t make_pte_marker_entry (pte_marker marker) {
@@ -320,6 +326,16 @@ static inline swp_entry_t make_guard_swp_entry(void)return make_pte_marker_entry (PTE_MARKER_GUARD ); } +static inline swp_entry_t make_purged_swp_entry (void ) +{ +return make_pte_marker_entry (PTE_MARKER_PURGED ); +} + +static inline pte_t make_purged_entry (void ) +{ +return make_pte_marker (PTE_MARKER_PURGED ); +} +struct page_vma_mapped_walk ;#ifdef CONFIG_ARCH_HAS_PMD_SOFTLEAVES
mm/khugepaged.c +17 -0
@@ -1200,6 +1200,14 @@ static enum scan_result __collapse_huge_page_swapin(struct mm_struct *mm, pte_present(vmf.orig_pte))continue ; +/* Installed since the scan, see collapse_scan_pmd(). */ +if (pte_is_marker (vmf.orig_pte)) { +pte_unmap (pte); +mmap_read_unlock (mm); + result =SCAN_PTE_NON_PRESENT ; + goto out; + } +/* * TODO: Support swapin without leading to further mTHP * collapses. Currently bringing in new pages via swapin may
@@ -1667,6 +1675,15 @@ static enum scan_result collapse_scan_pmd(struct mm_struct *mm,continue ; }if (!pte_present (pteval)) { +/* +* Markers such as purged or guard markers do not map a +* page that could be swapped in; faulting on them only +* raises a signal. uffd-wp markers are handled below. +*/ +if (pte_is_marker (pteval) && !pte_is_uffd_wp_marker (pteval)) { + result =SCAN_PTE_NON_PRESENT ; + goto out_unmap; + }if (++unmapped > max_ptes_swap) { result =SCAN_EXCEED_SWAP_PTE ; count_collapse_event(HPAGE_PMD_ORDER, THP_SCAN_EXCEED_SWAP_PTE,
mm/memory.c +5 -0
@@ -1919,6 +1919,8 @@ static inline int zap_nonpresent_ptes(struct mmu_gather *tlb,*/ if (!zap_drop_markers (details))return 1 ; + }else if (softleaf_is_purged_marker (entry)) { +/* Unlike guard markers, purged markers are always dropped. */ }else if (softleaf_is_hwpoison (entry) ||softleaf_is_poison_marker (entry)) {if (!should_zap_cows (details))
@@ -4736,6 +4738,9 @@ static vm_fault_t handle_pte_marker(struct vm_fault *vmf)if (marker &PTE_MARKER_GUARD )return VM_FAULT_SIGSEGV ; +if (marker &PTE_MARKER_PURGED ) +return VM_FAULT_SIGBUS ; +if (softleaf_is_uffd_wp_marker (entry))return pte_marker_handle_uffd_wp (vmf);
mm/mprotect.c +4 -2
@@ -247,10 +247,12 @@ static long change_softleaf_pte(struct vm_area_struct *vma,/* * Ignore error swap entries unconditionally, * because any access should sigbus/sigsegv -* anyway. +* anyway. Purged markers carry no protection +* information. */ if (softleaf_is_poison_marker (entry) || -softleaf_is_guard_marker (entry)) +softleaf_is_guard_marker (entry) || +softleaf_is_purged_marker (entry))return 0 ;/* * If this is uffd-wp pte marker and we'd like
mm/rmap.c +14 -0
@@ -2359,6 +2359,20 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, pteval =swp_entry_to_pte (make_hwpoison_entry (page));dec_mm_counter (mm,mm_counter (folio));set_pte_at (mm, address, pvmw.pte, pteval); + }else if (flags &TTU_PURGE ) { +/* +* Like swap-out, back off if the page may be pinned. +* See folio_try_share_anon_rmap(): clear PTE first. +*/ +if (PageAnonExclusive (page) && +folio_try_share_anon_rmap_pte (folio, page)) { +set_ptes (mm, address, pvmw.pte, pteval, nr_pages); + goto walk_abort; + } + pteval =make_purged_entry (); +dec_mm_counter (mm,mm_counter (folio)); +set_pte_at (mm, address, pvmw.pte, pteval); + }else if (likely (pte_present (pteval)) &&pte_unused (pteval) && !userfaultfd_armed (vma)) {/*
Signature · 1 line
2.56.1.839.g22a8290db9-goog
RFC PATCH 3/4 mm/volatile: purge volatile anonymous folios during reclaim
In reply to Hirotaka YamagataConnect the VM_VOLATILE VMA state and PTE_MARKER_PURGED entries to page reclaim and MADV_NONVOLATILE:
1. In shrink_folio_list(), once a folio has been selected for reclaim by normal LRU/MGLRU aging, call purge_volatile_folio() before allocating swap space. It returns PURGE_DONE (free the folio), PURGE_RETRY (keep it for a later attempt) or PURGE_NONE (reclaim it normally).
2. In __purge_volatile_folio(), skip non-anonymous and KSM folios. For a mapped folio, walk its anon_vma rmap under the anon_vma read lock and check that every VMA mapping it has VM_VOLATILE set; if one does not (e.g. after fork()), reclaim the folio normally. Otherwise unmap an order-0 folio with TTU_PURGE to install PTE_MARKER_PURGED entries, free its swap cache entry and freeze its refcount so that shrink_folio_list() frees it.
folio_ref_freeze() can fail on a reference taken without pinning (speculative or FOLL_GET references, PFN walkers); such a holder keeps the old contents, as after MADV_DONTNEED. The markers are in place by then, so return PURGE_RETRY and let shrink_folio_list() keep the folio, as it does when __remove_mapping() cannot freeze the refcount, rather than swap it out. On the next attempt the folio is unmapped and out of the swap cache, so the rmap walk is skipped and only the freeze is retried. This also frees any other unmapped, swap-backed anonymous folio outside the swap cache that reclaim meets once volatile ranges are in use; no PTE or swap entry refers to such a folio, so nothing is lost.
A folio that may be DMA-pinned is not purged: __purge_volatile_folio() returns PURGE_NONE for it, so the regular pin handling keeps or activates it, and it is not reported by the mm_purge_volatile_folio tracepoint. try_to_unmap(TTU_PURGE) also backs off on a racing GUP pin, and the folio is kept for a later pass. Mlocked mappings are
respected: try_to_unmap() keeps a folio mapped by a VM_LOCKED VMA and moves it to the unevictable LRU.
3. In madvise_volatile() for MADV_NONVOLATILE, walk the range with walk_page_range() to find and clear PTE_MARKER_PURGED entries, and return 1 if any page in the range was purged. If the call fails after changing part of the range, it returns the error and leaves the markers in place, so a retry reports them.
Guard purge_volatile_folio() with a static branch (volatile_ranges_key) so that systems not using volatile ranges do not pay for it in the reclaim path. The first MADV_VOLATILE that passes validation enables it after dropping mmap_lock. The mvolatile.h declarations depend on CONFIG_64BIT && CONFIG_MMU, matching mm/Makefile.
--- include/linux/mvolatile.h | 32 +++++++- mm/mvolatile.c | 156 +++++++++++++++++++++++++++++++++++++- mm/vmscan.c | 14 ++++ 3 files changed, 199 insertions(+), 3 deletions(-)
include/linux/mvolatile.h +31 -1
@@ -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 */
mm/mvolatile.c +154 -2
187 lines not on this page · raw message
mm/vmscan.c +14 -0
@@ -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.
Signature · 1 line
2.56.1.839.g22a8290db9-goog
RFC PATCH 4/4 mm/volatile: add PGPURGE vmstat counter and purge tracepoint
In reply to Hirotaka YamagataAdd accounting and a tracepoint for volatile folio purging:
- Add the PGPURGE ("pgpurge") vmstat and memcg event item counting the pages discarded by purge_volatile_folio(). Pages are counted once their mappings hold PTE_MARKER_PURGED markers and the swap cache is gone, and are charged to the folio's memcg as well, so memory.stat shows purges per cgroup.
- Add the mm_purge_volatile_folio tracepoint in include/trace/events/vmscan.h recording the folio PFN, page count, and whether the folio was purged or, being a large folio, left to swap.
--- include/linux/vm_event_item.h | 1 + include/trace/events/vmscan.h | 24 ++++++++++++++++++++++++ mm/memcontrol.c | 1 + mm/mvolatile.c | 20 ++++++++++++++++++-- mm/vmstat.c | 1 + 5 files changed, 45 insertions(+), 2 deletions(-)
include/linux/vm_event_item.h +1 -0
@@ -38,6 +38,7 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,PGFREE ,PGACTIVATE ,PGDEACTIVATE ,PGLAZYFREE ,PGFAULT ,PGMAJFAULT ,PGLAZYFREED , +PGPURGE ,PGREUSE ,PGSCAN_DIRECT_THROTTLE , #ifdef CONFIG_NUMA
include/trace/events/vmscan.h +24 -0
@@ -429,6 +429,30 @@ TRACE_EVENT(mm_vmscan_write_folio, show_reclaim_flags(__entry->reclaim_flags)) ); +TRACE_EVENT (mm_purge_volatile_folio, + + TP_PROTO(struct folio *folio, bool purged), + +TP_ARGS (folio, purged), + +TP_STRUCT__entry ( + __field(unsigned long, pfn) + __field(unsigned long, nr_pages) +__field (bool, purged) + ), + +TP_fast_assign ( + __entry->pfn = folio_pfn(folio); + __entry->nr_pages = folio_nr_pages(folio); + __entry->purged = purged; + ), + +TP_printk ("pfn=0x%lx nr_pages=%lu purged=%d" , + __entry->pfn, + __entry->nr_pages, + __entry->purged) +); + TRACE_EVENT(mm_vmscan_reclaim_pages, TP_PROTO(int nid,
mm/memcontrol.c +1 -0
@@ -608,6 +608,7 @@ static const unsigned int memcg_vm_event_stat[] = {PGDEACTIVATE ,PGLAZYFREE ,PGLAZYFREED , +PGPURGE , #ifdef CONFIG_SWAPSWPIN_ZERO , SWPOUT_ZERO,
mm/mvolatile.c +18 -2
@@ -15,6 +15,9 @@#include <linux/swap.h> #include <linux/swapops.h> #include <linux/pagewalk.h> +#include <linux/memcontrol.h> +#include <linux/vmstat.h> +#include <trace/events/vmscan.h> #include "internal.h" #include "vma.h"
@@ -261,9 +264,13 @@ enum purge_result __purge_volatile_folio(struct folio *folio)if (!mapped)return PURGE_NONE ; -/* Only order-0 folios are supported for now. */ +/* +* Only order-0 folios are supported for now. Checked after the +* volatile test so that the tracepoint shows large volatile folios +* falling back to swap. +*/ if (folio_test_large (folio)) -return PURGE_NONE ; + goto fail;try_to_unmap (folio,TTU_PURGE |TTU_SYNC );
@@ -280,6 +287,11 @@ enum purge_result __purge_volatile_folio(struct folio *folio)if (folio_test_swapcache (folio) && !folio_free_swap (folio))return PURGE_NONE ; +/* Count before the freeze, so that a retry is not counted again. */ +count_vm_events (PGPURGE ,folio_nr_pages (folio)); +count_memcg_folio_events (folio,PGPURGE ,folio_nr_pages (folio)); +trace_mm_purge_volatile_folio (folio, true); + freeze:/* * Like __remove_mapping(), back off on a transient reference. No new
@@ -292,4 +304,8 @@ enum purge_result __purge_volatile_folio(struct folio *folio)folio_unlock (folio);return PURGE_DONE ; + +fail: +trace_mm_purge_volatile_folio (folio, false); +return PURGE_NONE ; }
mm/vmstat.c +1 -0
@@ -1347,6 +1347,7 @@ const char * const vmstat_text[] = { [I(PGFAULT)] = "pgfault", [I(PGMAJFAULT)] = "pgmajfault", [I(PGLAZYFREED)] = "pglazyfreed", + [I(PGPURGE)] = "pgpurge", [I(PGREUSE)] = "pgreuse", [I(PGSCAN_DIRECT_THROTTLE)] = "pgscan_direct_throttle",
Signature · 1 line
2.56.1.839.g22a8290db9-goog