Re: [PATCH 05/13] mm: convert THP VMA flag tests to use vma_flags_t
From: David Hildenbrand (Arm)
Date: Fri Nov 06 2026 - 09:44:49 EST
On 11/5/26 22:30, Lorenzo Stoakes (ARM) wrote:
> Replace use of the legacy vm_flags_t flags with vma_flags_t values where
> VMA flags are tested directly in the THP and khugepaged logic.
>
> The thp_vma_allowable_orders() family of functions, hugepage_madvise() and
> khugepaged_enter_vma() continue to take legacy flags for the time being.
>
> Also update BUG_ON()'s to VM_WARN_ON_ONCE() while here.
>
> No functional change intended.
>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
> ---
> mm/huge_memory.c | 32 ++++++++++++++++----------------
> mm/khugepaged.c | 8 +++++---
> 2 files changed, 21 insertions(+), 19 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 7e589a2fb02d..764e693b422f 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -1257,7 +1257,7 @@ __setup("thp_anon=", setup_thp_anon);
>
> pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
> {
> - if (likely(vma->vm_flags & VM_WRITE))
> + if (likely(vma_test(vma, VMA_WRITE_BIT)))
> pmd = pmd_mkwrite(pmd, vma);
> return pmd;
> }
> @@ -1517,7 +1517,7 @@ vm_fault_t do_huge_pmd_device_private(struct vm_fault *vmf)
> */
> gfp_t vma_thp_gfp_mask(struct vm_area_struct *vma)
> {
> - const bool vma_madvised = vma && (vma->vm_flags & VM_HUGEPAGE);
> + const bool vma_madvised = vma && vma_test(vma, VMA_HUGEPAGE_BIT);
>
> /* Always do synchronous compaction */
> if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
> @@ -1716,10 +1716,9 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, unsigned long pfn,
> * but we need to be consistent with PTEs and architectures that
> * can't support a 'special' bit.
> */
> - BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
> - BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
> - (VM_PFNMAP|VM_MIXEDMAP));
> - BUG_ON((vma->vm_flags & VM_PFNMAP) && vma_is_cow_mapping(vma));
> + VM_WARN_ON_ONCE(!vma_test_any(vma, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT));
> + VM_WARN_ON_ONCE(vma_test_all(vma, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT));
> + VM_WARN_ON_ONCE(vma_test(vma, VMA_PFNMAP_BIT) && vma_is_cow_mapping(vma));
>
> if (unlikely(is_huge_zero_pfn(pfn)))
> return VM_FAULT_SIGBUS;
> @@ -1750,7 +1749,7 @@ EXPORT_SYMBOL_GPL(vmf_insert_folio_pmd);
> #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
> static pud_t maybe_pud_mkwrite(pud_t pud, struct vm_area_struct *vma)
> {
> - if (likely(vma->vm_flags & VM_WRITE))
> + if (likely(vma_test(vma, VMA_WRITE_BIT)))
> pud = pud_mkwrite(pud);
> return pud;
> }
> @@ -1827,10 +1826,9 @@ vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, unsigned long pfn,
> * but we need to be consistent with PTEs and architectures that
> * can't support a 'special' bit.
> */
> - BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
> - BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
> - (VM_PFNMAP|VM_MIXEDMAP));
> - BUG_ON((vma->vm_flags & VM_PFNMAP) && vma_is_cow_mapping(vma));
> + VM_WARN_ON_ONCE(!vma_test_any(vma, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT));
> + VM_WARN_ON_ONCE(vma_test_all(vma, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT));
> + VM_WARN_ON_ONCE(vma_test(vma, VMA_PFNMAP_BIT) && vma_is_cow_mapping(vma));
>
> pfnmap_setup_cachemode_pfn(pfn, &pgprot);
>
> @@ -2262,7 +2260,7 @@ static inline bool can_change_pmd_writable(struct vm_area_struct *vma,
> {
> struct page *page;
>
> - if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
> + if (WARN_ON_ONCE(!vma_test(vma, VMA_WRITE_BIT)))
> return false;
>
> /* Don't touch entries that are not even readable (NUMA hinting). */
> @@ -2277,7 +2275,7 @@ static inline bool can_change_pmd_writable(struct vm_area_struct *vma,
> if (userfaultfd_huge_pmd_wp(vma, pmd))
> return false;
>
> - if (!(vma->vm_flags & VM_SHARED)) {
> + if (!vma_test(vma, VMA_SHARED_BIT)) {
> /* See can_change_pte_writable(). */
> page = vm_normal_page_pmd(vma, addr, pmd);
> return page && PageAnon(page) && PageAnonExclusive(page);
> @@ -3568,7 +3566,8 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
>
> if (pmd_dirty(orig_pmd))
> folio_set_dirty(folio);
> - if (folio_test_dirty(folio) && !(vma->vm_flags & VM_DROPPABLE)) {
> + if (folio_test_dirty(folio) &&
> + !vma_test_single_mask(vma, VMA_DROPPABLE)) {
> folio_set_swapbacked(folio);
> return false;
> }
> @@ -3600,7 +3599,8 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
> */
> if (pmd_dirty(orig_pmd))
> folio_set_dirty(folio);
> - if (folio_test_dirty(folio) && !(vma->vm_flags & VM_DROPPABLE)) {
> + if (folio_test_dirty(folio) &&
> + !vma_test_single_mask(vma, VMA_DROPPABLE)) {
> folio_set_swapbacked(folio);
> set_pmd_at(mm, addr, pmdp, orig_pmd);
> return false;
> @@ -3614,7 +3614,7 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
> folio_remove_rmap_pmd(folio, pmd_page(orig_pmd), vma);
> zap_deposited_table(mm, pmdp);
> add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR);
> - if (vma->vm_flags & VM_LOCKED)
> + if (vma_test(vma, VMA_LOCKED_BIT))
> mlock_drain_local();
> folio_put(folio);
>
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index 0d3bd9e23613..a2c7c6a0768d 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -640,7 +640,8 @@ static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma,
> * If the vma has the VM_DROPPABLE flag, the collapse will
> * preserve the lazyfree property without needing to skip.
> */
> - if (cc->policy.anon_skip_lazyfree && !(vma->vm_flags & VM_DROPPABLE) &&
> + if (cc->policy.anon_skip_lazyfree &&
> + !vma_test_single_mask(vma, VMA_DROPPABLE) &&
> folio_test_lazyfree(folio) && !pte_dirty(pteval)) {
> result = SCAN_PAGE_LAZYFREE;
> goto out;
> @@ -1683,7 +1684,8 @@ static enum scan_result collapse_scan_anon(struct vm_area_struct *vma,
> * If the vma has the VM_DROPPABLE flag, the collapse will
> * preserve the lazyfree property without needing to skip.
> */
> - if (cc->policy.anon_skip_lazyfree && !(vma->vm_flags & VM_DROPPABLE) &&
> + if (cc->policy.anon_skip_lazyfree &&
> + !vma_test_single_mask(vma, VMA_DROPPABLE) &&
> folio_test_lazyfree(folio) && !pte_dirty(pteval)) {
> result = SCAN_PAGE_LAZYFREE;
> failed_pfn = folio_pfn(folio);
> @@ -1948,7 +1950,7 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign
> * So page lock of folio does not protect from it, so we must not drop
> * ptl before pgt_pmd is removed, so uffd private needs pml taken now.
> */
> - if (userfaultfd_armed(vma) && !(vma->vm_flags & VM_SHARED))
> + if (userfaultfd_armed(vma) && !vma_test(vma, VMA_SHARED_BIT))
> pml = pmd_lock(mm, pmd);
A patch in mm/core.git will remove that entirely.
Right now it results in a conflict between mm-new and mm/core.git.
Beat to drop this here entirely. (I can also drop it when applying)
--
Cheers,
David