From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=49.212.243.145 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796027763; b=gjIU7LE4TH5sPjuq5uLpnDbbukRl4+J8bwM6nZSm8AbmRvP1wMpLmXcJH/C8UYu1rJsB lIt/JKDm9eWMxMOjMwMhEaAlNpQez5OA0PzX7Lbvycd5PzeLbCleqe+TUtxeggrHJXcpc XcghgKCATCKh8BPK4hc4ajeO+x/P6rmoVarTS2rs/M4z8S3Y7PIWMXJOwkTFD2gY8+lQd 6CRKSVCj3HFgDAUSFVfaDcVHvxLOLFyu9qww1q+w+PRQ0vim/ClU68UzWFdJZ7cSkrBa9 vpDmaLhbdDtifse/wy8RraiEetjodlKl/fiNqF5kjuFuIYGYe74dKYwYXDY5Jx2T0fQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796027763; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Anti-Virus-Server:X-Virus-Status; bh=90u9fVyxdH6EsN+Jhgib+0CM36rvdsrUEhbDyX/08Qk=; b=pOX+bgkkZIhgJhg8ZngK24498nL1WN3DMNA2mCjSWjSgnH6Fl4/tAKEnr5MZXco7NzAq pOkJLzNDLC+NKKHhBaKTXh3QSZuomrrjawa8Xy1aTw81ibc/LYeDFl5mkCydZfuVIYh6A MToair1etlvXhtdITdlW8WjpvyDr50VbNlxvJ8mtAdEgefsoEUUJavp5SIgDp+cvlszb1 3Iwq3NudKsyWKnKdLF0R7bsPwTgW6Ftx7Zr4HrwJ08viOZBHDARU2LRgpUGUYqWX1vKpn tn94IGXbN/RjIocSeHsL3wrivMSRz44Sj5tjWKjA3ca4CdGNflnBRvZX5yxOrSe/kfg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=pisnp.net; spf=pass smtp.mailfrom=pisnp.net; dkim=pass header.d=pisnp.net header.i=@pisnp.net header.a=rsa-sha256 header.s=rs20260908 header.b=SxvIK6gK; arc=none smtp.remote-ip=49.212.243.145 Received: from www3605.sakura.ne.jp (www3605.sakura.ne.jp [49.212.243.145]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5FC93606A325; Mon, 30 Nov 2026 08:36:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5FC93606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=pisnp.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pisnp.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pisnp.net header.i=@pisnp.net header.a=rsa-sha256 header.s=rs20260908 header.b=SxvIK6gK Received: from www3605.sakura.ne.jp (localhost [127.0.0.1]) by www3605.sakura.ne.jp (8.16.1/8.16.1) with ESMTP id 6AU8YrdB026379; Mon, 30 Nov 2026 17:34:53 +0900 (JST) (envelope-from hiro@pisnp.net) Received: from hirotakay.c.googlers.com.com (57.134.185.35.bc.googleusercontent.com [35.185.134.57]) (authenticated bits=0) by www3605.sakura.ne.jp (8.16.1/8.16.1) with ESMTPSA id 6AU8YbfQ026202 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Mon, 30 Nov 2026 17:34:52 +0900 (JST) (envelope-from hiro@pisnp.net) DKIM-Signature: a=rsa-sha256; bh=90u9fVyxdH6EsN+Jhgib+0CM36rvdsrUEhbDyX/08Qk=; c=relaxed/relaxed; d=pisnp.net; h=From:Message-ID:To:Subject:Date; s=rs20260908; t=1796027693; v=1; b=SxvIK6gKRttQDiUS0Fe2S/EXM3xbF3YzV2f5sq5DKkSFBupW1WwLoR++BQQ+Rt8D oA46jBOjjBTJiqhRfNBadik+rAxBkunSrkFEMRTHGXl/uqXCfA/C7piDj5NXm/hv JkwfAp48TWG8kTva5BA+OQCnMQTb4ryJ/MEy7iLJgkjJPFzlJvZ+elV4NoE6KveA v8ybI2O8IlMcxalnCtsJzsyn+IpvMoRSNTEpmAdwfmCyrJoEt4PtXat3y5qswUzN gEqN+kM3/4KyOphlgkvzBqUkXqCI3RFxHyRQWc//U5LyrktIULwsEsYiyDzDnqb2 ufziOf4n7pBb0uEvkOzeow== From: Hirotaka Yamagata To: Andrew Morton Cc: linux-api@vger.kernel.org, "Liam R. Howlett" , Lorenzo Stoakes , David Hildenbrand , Chris Li , Kairui Song , Steven Rostedt , Masami Hiramatsu , Arnd Bergmann , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , 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 , Minchan Kim , Alexandre Marciano Gimenez , Tatsuyuki Ishi Subject: [RFC PATCH v1 3/4] mm/volatile: purge volatile anonymous folios during reclaim Date: Mon, 30 Nov 2026 17:34:06 +0900 Message-ID: <20261130083407.2669584-4-hiro@pisnp.net> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog In-Reply-To: <20261130083407.2669584-1-hiro@pisnp.net> References: <20261130083407.2669584-1-hiro@pisnp.net> X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Anti-Virus-Server: fsav115.rs.sakura.ne.jp X-Virus-Status: clean Connect 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. Cc: John Stultz Cc: Minchan Kim Cc: Alexandre Marciano Gimenez Cc: Tatsuyuki Ishi Signed-off-by: Hirotaka Yamagata --- include/linux/mvolatile.h | 32 +++++++- mm/mvolatile.c | 156 +++++++++++++++++++++++++++++++++++++- mm/vmscan.c | 14 ++++ 3 files changed, 199 insertions(+), 3 deletions(-) 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 #include -#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 #include #include +#include #include #include @@ -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