From: Hirotaka Yamagata <hiro@pisnp.net>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-api@vger.kernel.org, "Liam R. Howlett" <liam@infradead.org>,
Lorenzo Stoakes <ljs@kernel.org>,
David Hildenbrand <david@kernel.org>,
Chris Li <chrisl@kernel.org>, Kairui Song <kasong@tencent.com>,
Steven Rostedt <rostedt@goodmis.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Arnd Bergmann <arnd@arndb.de>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Hocko <mhocko@kernel.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Shakeel Butt <shakeel.butt@linux.dev>,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
linux-fsdevel@vger.kernel.org,
linux-trace-kernel@vger.kernel.org, linux-arch@vger.kernel.org,
cgroups@vger.kernel.org, John Stultz <jstultz@google.com>,
Minchan Kim <minchan@kernel.org>,
Alexandre Marciano Gimenez <raging@google.com>,
Tatsuyuki Ishi <ishitatsuyuki@google.com>
Subject: [RFC PATCH v1 1/4] mm: introduce VM_VOLATILE and MADV_VOLATILE/MADV_NONVOLATILE VMA handling
Date: Mon, 30 Nov 2026 17:34:04 +0900 [thread overview]
Message-ID: <20261130083407.2669584-2-hiro@pisnp.net> (raw)
In-Reply-To: <20261130083407.2669584-1-hiro@pisnp.net>
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.
Link: https://lore.kernel.org/1398806483-19122-1-git-send-email-john.stultz@linaro.org
Cc: John Stultz <jstultz@google.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Alexandre Marciano Gimenez <raging@google.com>
Cc: Tatsuyuki Ishi <ishitatsuyuki@google.com>
Signed-off-by: Hirotaka Yamagata <hiro@pisnp.net>
---
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
diff --git a/MAINTAINERS b/MAINTAINERS
index 3a19da74d00c..390c2197b3d8 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -17528,8 +17528,10 @@ L: linux-mm@kvack.org
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 <miquel.raynal@bootlin.com>
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 5c54aebe2118..4bd0085f008a 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -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;
diff --git a/include/linux/mm.h b/include/linux/mm.h
index dd09c438fa23..0f9373c40274 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -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.
diff --git a/include/linux/mvolatile.h b/include/linux/mvolatile.h
new file mode 100644
index 000000000000..bc8828c8e6c4
--- /dev/null
+++ b/include/linux/mvolatile.h
@@ -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 */
diff --git a/include/trace/events/mmflags.h b/include/trace/events/mmflags.h
index 935893e5ea53..cda8b8ec7b53 100644
--- a/include/trace/events/mmflags.h
+++ b/include/trace/events/mmflags.h
@@ -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) \
diff --git a/include/uapi/asm-generic/mman-common.h b/include/uapi/asm-generic/mman-common.h
index ef1c27fa3c57..cfaa94aea62a 100644
--- a/include/uapi/asm-generic/mman-common.h
+++ b/include/uapi/asm-generic/mman-common.h
@@ -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 */
diff --git a/mm/Makefile b/mm/Makefile
index e7245cb88c66..b4811b6e7a15 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -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 \
diff --git a/mm/madvise.c b/mm/madvise.c
index 96f2387b2f46..d9d9d5a7221f 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -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 beh
if (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);
diff --git a/mm/mseal.c b/mm/mseal.c
index 7a8ac66dc215..058b2194fb1b 100644
--- a/mm/mseal.c
+++ b/mm/mseal.c
@@ -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);
diff --git a/mm/mvolatile.c b/mm/mvolatile.c
new file mode 100644
index 000000000000..fbe1028aaf5c
--- /dev/null
+++ b/mm/mvolatile.c
@@ -0,0 +1,143 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2014, LG Electronics, Minchan Kim <minchan@kernel.org>
+ * Copyright (C) 2014 Linaro Ltd., John Stultz <john.stultz@linaro.org>
+ */
+#include <linux/syscalls.h>
+#include <linux/mvolatile.h>
+#include <linux/mm_inline.h>
+#include <linux/pagemap.h>
+#include <linux/rmap.h>
+#include <linux/hugetlb.h>
+#include <linux/mmu_notifier.h>
+#include <linux/pgtable.h>
+#include <linux/mman.h>
+#include <linux/swap.h>
+#include <linux/swapops.h>
+#include <linux/pagewalk.h>
+#include "internal.h"
+#include "vma.h"
+
+static int check_volatile_range(struct mm_struct *mm, unsigned long start,
+ unsigned long end, int behavior)
+{
+ VMA_ITERATOR(vmi, mm, start);
+ unsigned long prev_end = start;
+ struct vm_area_struct *vma;
+ bool make_volatile = behavior == MADV_VOLATILE;
+ int splits = 0;
+
+ for_each_vma_range(vmi, vma, end) {
+ if (vma->vm_start > prev_end)
+ return -ENOMEM;
+
+ if (vma_test_any_mask(vma, VMA_SPECIAL_FLAGS) ||
+ vma_test_any(vma, VMA_MIXEDMAP_BIT, VMA_HUGETLB_BIT))
+ return -EINVAL;
+
+ if (vma->vm_file)
+ return -EINVAL;
+
+ /*
+ * Mlocked memory is not purged and sealed memory must not be
+ * discarded, so neither may become volatile. MADV_NONVOLATILE
+ * stays allowed.
+ */
+ if (make_volatile && vma_test(vma, VMA_LOCKED_BIT))
+ return -EINVAL;
+ if (make_volatile && vma_is_sealed(vma))
+ return -EPERM;
+
+ /* Only the first and last VMA can need a split. */
+ if (vma_is_volatile(vma) != make_volatile)
+ splits += (vma->vm_start < start) + (vma->vm_end > end);
+
+ prev_end = vma->vm_end;
+ }
+
+ if (prev_end < end)
+ return -ENOMEM;
+
+ /*
+ * Fail before changing any VMA if the splits would exceed
+ * max_map_count, rather than in split_vma() after earlier VMAs have
+ * been changed.
+ */
+ if (splits && mm->map_count + splits > get_sysctl_max_map_count())
+ return -ENOMEM;
+
+ return 0;
+}
+
+/**
+ * madvise_volatile() - Mark or unmark the VMAs in [start, end) volatile
+ * @mm: target mm_struct
+ * @start: start address, page aligned
+ * @end: end address (exclusive), page aligned
+ * @behavior: %MADV_VOLATILE or %MADV_NONVOLATILE
+ *
+ * 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.
+ *
+ * Return: 0 on success 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;
+ int ret = 0;
+
+ if (start >= TASK_SIZE || end > TASK_SIZE)
+ return -EINVAL;
+
+ if (mmap_write_lock_killable(mm))
+ return -EINTR;
+
+ ret = check_volatile_range(mm, start, end, behavior);
+ if (ret)
+ goto out;
+
+ vma = vma_iter_load(&vmi);
+ prev = vma_prev(&vmi);
+ if (start > vma->vm_start)
+ prev = vma;
+
+ for_each_vma_range(vmi, vma, end) {
+ const unsigned long curr_start = max(vma->vm_start, start);
+ const unsigned long curr_end = min(vma->vm_end, end);
+ vma_flags_t vma_flags = vma->flags;
+
+ if (behavior == MADV_VOLATILE) {
+ if (!vma_is_volatile(vma)) {
+ vma_flags_set(&vma_flags, VMA_VOLATILE_BIT);
+ vma = vma_modify_flags(&vmi, prev, vma, curr_start,
+ curr_end, &vma_flags);
+ if (IS_ERR(vma)) {
+ ret = PTR_ERR(vma);
+ goto out;
+ }
+ vma_start_write(vma);
+ vma_set_flags(vma, VMA_VOLATILE_BIT);
+ }
+ } else { /* MADV_NONVOLATILE */
+ if (vma_is_volatile(vma)) {
+ vma_flags_clear(&vma_flags, VMA_VOLATILE_BIT);
+ vma = vma_modify_flags(&vmi, prev, vma, curr_start,
+ curr_end, &vma_flags);
+ if (IS_ERR(vma)) {
+ ret = PTR_ERR(vma);
+ goto out;
+ }
+ vma_start_write(vma);
+ vma_clear_flags(vma, VMA_VOLATILE_BIT);
+ }
+ }
+ prev = vma;
+ }
+
+out:
+ mmap_write_unlock(mm);
+ return ret;
+}
--
2.56.1.839.g22a8290db9-goog
next prev parent reply other threads:[~2026-11-30 8:36 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 8:34 [RFC PATCH v1 0/4] mm: port MADV_VOLATILE and MADV_NONVOLATILE to v7.3-rc1 Hirotaka Yamagata
2026-11-30 8:34 ` Hirotaka Yamagata [this message]
2026-11-30 8:34 ` [RFC PATCH v1 2/4] mm: add PTE_MARKER_PURGED and TTU_PURGE for purged volatile pages Hirotaka Yamagata
2026-11-30 8:34 ` [RFC PATCH v1 3/4] mm/volatile: purge volatile anonymous folios during reclaim Hirotaka Yamagata
2026-11-30 8:34 ` [RFC PATCH v1 4/4] mm/volatile: add PGPURGE vmstat counter and purge tracepoint Hirotaka Yamagata
2026-11-30 10:39 ` [RFC PATCH v1 0/4] mm: port MADV_VOLATILE and MADV_NONVOLATILE to v7.3-rc1 David Hildenbrand (Arm)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130083407.2669584-2-hiro@pisnp.net \
--to=hiro@pisnp.net \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=cgroups@vger.kernel.org \
--cc=chrisl@kernel.org \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=ishitatsuyuki@google.com \
--cc=jstultz@google.com \
--cc=kasong@tencent.com \
--cc=liam@infradead.org \
--cc=linux-api@vger.kernel.org \
--cc=linux-arch@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=mhiramat@kernel.org \
--cc=mhocko@kernel.org \
--cc=minchan@kernel.org \
--cc=raging@google.com \
--cc=roman.gushchin@linux.dev \
--cc=rostedt@goodmis.org \
--cc=shakeel.butt@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox