Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] udmabuf: race between udmabuf_vm_huge_fault() and
  udmabuf_vm_fault()
- [Medium] udmabuf: unclamped pre-fault range via pmd_addr_end() in
  udmabuf_vm_fault()
--

commit ebe4e072f46637cdd8bfd0a954132a421bec39c8
Author: Qiliang Yuan <[email protected]>

udmabuf: map huge page backed ranges with PMDs

This commit adds a ->huge_fault() handler to udmabuf to map PMD-aligned
contiguous PFN ranges with PMDs and restricts PTE pre-faulting in
udmabuf_vm_fault() to a single PMD-sized range.

> diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
> index 018937435356e..dd98d5a1b69fc 100644
> --- a/drivers/dma-buf/udmabuf.c
> +++ b/drivers/dma-buf/udmabuf.c
> @@ -44,12 +44,67 @@ struct udmabuf {
>       struct miscdevice *device;
>  };
>  
> +#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP

[ ... ]

> +static vm_fault_t udmabuf_vm_huge_fault(struct vm_fault *vmf,
> +                                     unsigned int order)
> +{
> +     unsigned long pfn;
> +
> +     if (order != PMD_ORDER ||
> +         !udmabuf_pmd_mappable(vmf->vma, vmf->address & PMD_MASK, &pfn))
> +             return VM_FAULT_FALLBACK;
> +
> +     return vmf_insert_pfn_pmd(vmf, pfn, vmf->flags & FAULT_FLAG_WRITE);
> +}
> +#endif

[Severity: High]
Can udmabuf_vm_huge_fault() and udmabuf_vm_fault() race on the same PMD
entry when the prerequisite "mm: fix races between huge and non-huge
faults in PFN mappings" series is not present in the tree?

Neither fault handler takes a driver lock, and under CONFIG_PER_VMA_LOCK
both run under the per-VMA read lock while prctl(PR_SET_THP_DISABLE) sets
MMF_DISABLE_THP_COMPLETELY without taking per-VMA write locks:

CPU0                                  CPU1
__handle_mm_fault()
  create_huge_pmd()
    udmabuf_vm_huge_fault()
      udmabuf_pmd_mappable()
                                      prctl(PR_SET_THP_DISABLE, 1, ...)
                                      __handle_mm_fault()
                                        handle_pte_fault()
                                          udmabuf_vm_fault()
      vmf_insert_pfn_pmd()
        insert_pmd()
                                            vmf_insert_pfn()
                                              insert_pfn()
                                                get_locked_pte()
                                                  walk_to_pmd()
                                                    VM_BUG_ON(
                                                      pmd_trans_huge(*pmd))

When CONFIG_DEBUG_VM is disabled, get_locked_pte() returns NULL on the
huge PMD and insert_pfn() returns a spurious VM_FAULT_OOM.

If udmabuf_vm_fault() installs a PTE page table first instead,
insert_pmd() sees !pmd_none(*pmd) without checking pmd_trans_huge(*pmd);
on a write fault it compares pmd_pfn(*pmd) against pfn, hits
WARN_ON_ONCE(!is_huge_zero_pmd(*pmd)), and returns VM_FAULT_NOPAGE
instead of VM_FAULT_FALLBACK.

>  static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
>  {
>       struct vm_area_struct *vma = vmf->vma;
>       struct udmabuf *ubuf = vma->vm_private_data;
>       pgoff_t pgoff = vmf->pgoff;
> -     unsigned long addr, pfn;
> +     unsigned long addr, end, pfn;
>       vm_fault_t ret;
>  
>       if (pgoff >= ubuf->pagecount)
> @@ -61,11 +116,16 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
>       if (ret & VM_FAULT_ERROR)
>               return ret;
>  
> -     /* pre fault */
> -     pgoff = vma->vm_pgoff;
> -     addr = vma->vm_start;
> +     /*
> +      * Pre-fault the rest of the PMD-sized range of the fault. Staying
> +      * within its page table keeps the walk off the ranges that
> +      * udmabuf_vm_huge_fault() maps with a PMD.
> +      */
> +     addr = max(vma->vm_start, vmf->address & PMD_MASK);
> +     end = pmd_addr_end(vmf->address, vma->vm_end);

[Severity: Medium]
Does pmd_addr_end(vmf->address, vma->vm_end) fail to clamp end to a
single page table on architectures with folded PMDs?

When include/asm-generic/pgtable-nopmd.h is used, pmd_addr_end(addr, end)
is defined as (end) because it assumes end was already clamped by
pgd_addr_end() -> p4d_addr_end() -> pud_addr_end(), while PMD_MASK and
PMD_SIZE match PGDIR_MASK and PGDIR_SIZE.  As a result, addr is clamped
with PMD_MASK, but end stays at vma->vm_end and the loop pre-faults
across all remaining page tables to the end of the VMA.

Calculating the upper bound explicitly from (vmf->address & PMD_MASK) +
PMD_SIZE (as folio_pte_range() does in mm/memory.c) would clamp the range
on folded-PMD kernels as well.

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

Reply via email to