Skip to content

RFC mm: port MADV_VOLATILE and MADV_NONVOLATILE to v7.3-rc1

Hirotaka Yamagata cgroups linux-api linux-arch linux-fsdevel linux-mm linux-trace-kernel lkml 1 version New

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest4 patches 0 repliesNew

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4mm: introduce VM_VOLATILE and MADV_VOLATILE/MADV_NONVOLATILE VMA handlingMAINTAINERS fs/proc/task_mmu.c include/linux/mm.h +7 more+236 -10 replies
2/4mm: add PTE_MARKER_PURGED and TTU_PURGE for purged volatile pagesinclude/linux/leafops.h include/linux/mm_inline.h include/linux/rmap.h +5 more+75 -50 replies
3/4mm/volatile: purge volatile anonymous folios during reclaiminclude/linux/mvolatile.h mm/mvolatile.c mm/vmscan.c+199 -30 replies
4/4mm/volatile: add PGPURGE vmstat counter and purge tracepointinclude/linux/vm_event_item.h include/trace/events/vmscan.h mm/memcontrol.c +2 more+45 -20 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [RFC PATCH v1 0/4] 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