All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Qiliang Yuan" <qiliang.yuan@linux.dev>
Cc: dri-devel@lists.freedesktop.org, media-ci@linuxtv.org
Subject: Re: [PATCH v3 2/3] udmabuf: map huge page backed ranges with PMDs
Date: Tue, 01 Dec 2026 17:34:31 +0000	[thread overview]
Message-ID: <sashiko-outbox-243856@kernel.org> (raw)
In-Reply-To: <20261201-feat-udmabuf-huge-pfnmap-v3-2-144cc7a48f09@linux.dev>

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 <qiliang.yuan@linux.dev>

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/20261201-feat-udmabuf-huge-pfnmap-v3-0-144cc7a48f09@linux.dev?part=2

  reply	other threads:[~2026-12-01 17:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 17:16 [PATCH v3 0/3] udmabuf: speed up mmap() of huge page backed buffers Qiliang Yuan
2026-12-01 17:16 ` [PATCH v3 1/3] dma-buf: align large mappings to PMD boundaries Qiliang Yuan
2026-12-01 17:16 ` [PATCH v3 2/3] udmabuf: map huge page backed ranges with PMDs Qiliang Yuan
2026-12-01 17:34   ` sashiko-bot [this message]
2026-12-02  1:17     ` Qiliang Yuan
2026-12-01 17:16 ` [PATCH v3 3/3] selftests/udmabuf: test mapping a huge page backed udmabuf Qiliang Yuan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-243856@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=media-ci@linuxtv.org \
    --cc=qiliang.yuan@linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.