Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] udmabuf: pre-fault in udmabuf_vm_fault() hits VM_BUG_ON on
  existing huge PMD
--

commit 4678e5bd3e35a29d8e3ac62812388b08912dd829
Author: Qiliang Yuan <[email protected]>

udmabuf: map huge page backed ranges with PMDs

This commit adds a ->huge_fault() handler to map PMD-aligned, physically
contiguous udmabuf ranges with huge PMDs, and updates udmabuf_vm_fault() to
skip PMD-mappable ranges when pre-faulting the VMA.

> diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
> index 018937435356e..f9c908f8b53a2 100644
> --- a/drivers/dma-buf/udmabuf.c
> +++ b/drivers/dma-buf/udmabuf.c
[ ... ]
> @@ -61,7 +112,12 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
>       if (ret & VM_FAULT_ERROR)
>               return ret;
>  
> -     /* pre fault */
> +     /*
> +      * Pre-fault the rest of the VMA, except for the ranges that
> +      * udmabuf_vm_huge_fault() maps with a PMD once they are accessed.
> +      */
> +     pmd_ok = thp_vma_allowable_order(vma, vma->vm_flags, TVA_PAGEFAULT,
> +                                      PMD_ORDER);
>       pgoff = vma->vm_pgoff;
>       addr = vma->vm_start;
>  
> @@ -69,6 +125,14 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
>               if (addr == vmf->address)
>                       continue;
>  
> +             if (pmd_ok && IS_ALIGNED(addr, PMD_SIZE) &&
> +                 (vmf->address & PMD_MASK) != addr &&
> +                 udmabuf_pmd_mappable(vma, addr, &pfn)) {
> +                     pgoff += (PMD_SIZE >> PAGE_SHIFT) - 1;
> +                     addr += PMD_SIZE - PAGE_SIZE;
> +                     continue;
> +             }
> +
>               if (WARN_ON(pgoff >= ubuf->pagecount))
>                       break;

[Severity: High]
Can the pre-fault loop in udmabuf_vm_fault() call vmf_insert_pfn() on an
address range where udmabuf_vm_huge_fault() has already installed a huge
PMD?

If a fault installs a huge PMD in one 2 MiB range of the VMA, and userspace
then disables THP for the VMA or mm (via madvise(MADV_NOHUGEPAGE) or
prctl(PR_SET_THP_DISABLE)) before faulting on another unmapped page in the
same VMA, existing huge PMDs are not split or unmapped, but pmd_ok evaluates
to false.  The pre-fault loop then does not skip the already-mapped PMD
range and calls vmf_insert_pfn() -> vmf_insert_pfn_prot() -> insert_pfn() ->
get_locked_pte() -> walk_to_pmd(), hitting VM_BUG_ON(pmd_trans_huge(*pmd))
when CONFIG_DEBUG_VM is enabled (or failing in pte_alloc_map_lock() with
VM_FAULT_OOM and aborting pre-faulting for the rest of the VMA when
CONFIG_DEBUG_VM is disabled).

Without synchronization against the PMD entry state, a concurrent PTE
insertion in udmabuf_vm_fault() can also allocate a PTE page table for a
range while another thread is in udmabuf_vm_huge_fault() ->
vmf_insert_pfn_pmd() -> insert_pmd() on a write fault, causing insert_pmd()
to see !pmd_none(*pmd) with pmd_pfn(*pmd) != pfn and trigger
WARN_ON_ONCE(!is_huge_zero_pmd(*pmd)).

Conversely, when pmd_ok is true, skipping a PMD-mappable range without
checking pmd_none(*pmd) skips ranges that already have a PTE page table
allocated (for example after madvise(MADV_DONTNEED) zaps PTEs without
freeing PTE page tables); because __handle_mm_fault() only calls
create_huge_pmd() when pmd_none(*vmf.pmd) is true, those skipped ranges will
never reach udmabuf_vm_huge_fault() and will each fault back into
udmabuf_vm_fault() and re-scan the VMA.

-- 
Sashiko AI review · 
https://sashiko.dev/#/patchset/[email protected]?part=2

Reply via email to