Re: Validating dma_mmap_coherent() parameters before calling (was Re: WARNING in memtype_reserve)

Jeremy Linton
2020-05-14T11:10:06Z

Hi,

On 5/14/20 1:31 AM, Christoph Hellwig wrote:
> On Thu, May 14, 2020 at 08:27:50AM +0200, Greg KH wrote:
>> On Thu, May 14, 2020 at 08:14:17AM +0200, Christoph Hellwig wrote:
>>> Guys, can you please start formal thread on this? I have no
>>> idea where this came from and what the rationale is. Btw, if the
>>> pfn is crap in dma_direct_mmap then the dma_addr_t passed in is
>>> crap, as it is derived from that. What is the caller, and how is
>>> this triggered?
>>
>>
>> Ok, to summarize, commit 2bef9aed6f0e ("usb: usbfs: correct kernel->user
>> page attribute mismatch") changed a call from remap_pfn_range() to
>> dma_mmap_coherent() for usb data buffers being sent from userspace.
>
> I only need to look at the commit for 3 seconds to tell you that it is
> completely buggy. While using dma_mmap_coherent is fundamentally the
> right thing and absolutely required for dma_alloc_* allocations, USB
> also uses it's own local gen pool allocator or plain kmalloc for not
> DMA capable controller. This need to use remap_pfn_range. I'm pretty
> sure you hit one of those cases.

? The code path in question is usbdev_mmap() and the allocation is done
~13 lines lines before as a usb_alloc_coherent().

>
> The logic should be something like:
>
> if (hcd->localmem_pool || !hcd_uses_dma(hcd))
> remap_pfn_range()
> else
> dma_mmap_coherent()
>

That sort of makes sense, except for the above, and the fact that I
would imagine the dma_mmap_coherent should be dealing with that case.
I'm not really clear about the details of the GCE usb device here, but
my first guess at this was the dma_pgprot() in dma_direct_mmap() is
incorrectly picking a pgprot...