Re: [PATCH 2/4] mm/huge_memory: skip zone device folios in madvise_free_huge_pmd()

"Lorenzo Stoakes (ARM)" <[email protected]>
Newsgroups dev.linux.lists.damon,org.kernel.vger.linux-kernel,org.kernel.vger.stable,org.kvack.linux-mm
Message-ID <anSOvqN1_Hs190xi@lucifer>
I kinda wish he had some generalised folio handler like:

	struct folio_ops {
		int (*handle_empty_entry)(void *priv);
		int (*handle_normal_folio)(void *priv, struct folio *folio);
		int (*handle_softleaf_pte_entry)(void *priv, const pte_t *ptep);
		int (*handle_zone_folio)(void *priv, struct folio *folio);
		...
	};

Or I don't know an equivalent type of thing, as we keep having these same
patterns emerge in mm that are duplicated in a million places with slight
variations... :)

I wonder if C is expressive enough to get us to something sane though.

On Tue, Jul 28, 2026 at 03:47:12PM -0400, Gregory Price wrote:
> madvise_free_huge_pmd() resolves the folio backing a PMD via pmd_folio()
> and marks it lazyfree without checking for zone device memory.
>
> The surrounding guards do not cover every zone device case:
>  - MADV_FREE only operates on anonymous VMAs (DAX mappings are excluded)
>
>  - !pmd_present() branch rejects device-private and migration entries
>
>  - present zone device PMD (device coherent THP) is not filtered.
>    Unlike vm_normal_page_pmd(), it performs no special/pfnmap check,
>    and would be marked lazyfree here.
>
> Bail out when the folio is a zone device folio.
>
> Fixes: 368076f52ebe ("mm/huge_memory: add device-private THP support to PMD operations")
> Cc: <[email protected]>
> Signed-off-by: Gregory Price (Meta) <[email protected]>

Anyway LGTM so:

Reviewed-by: Lorenzo Stoakes (ARM) <[email protected]>

> ---
>  mm/huge_memory.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 5bd8d4f59a7b..e0ffdeeb3077 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -2338,6 +2338,10 @@ bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
>  	}
>
>  	folio = pmd_folio(orig_pmd);
> +
> +	if (folio_is_zone_device(folio))
> +		goto out;
> +
>  	/*
>  	 * If other processes are mapping this folio, we couldn't discard
>  	 * the folio unless they all do MADV_FREE so let's skip the folio.
> --
> 2.55.0
>

--
Cheers, Lorenzo
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.